probe-event.c 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911
  1. /*
  2. * probe-event.c : perf-probe definition to probe_events format converter
  3. *
  4. * Written by Masami Hiramatsu <mhiramat@redhat.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. *
  20. */
  21. #include <sys/utsname.h>
  22. #include <sys/types.h>
  23. #include <sys/stat.h>
  24. #include <fcntl.h>
  25. #include <errno.h>
  26. #include <stdio.h>
  27. #include <unistd.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <stdarg.h>
  31. #include <limits.h>
  32. #include <elf.h>
  33. #include "util.h"
  34. #include "event.h"
  35. #include "strlist.h"
  36. #include "debug.h"
  37. #include "cache.h"
  38. #include "color.h"
  39. #include "symbol.h"
  40. #include "thread.h"
  41. #include <api/fs/debugfs.h>
  42. #include <api/fs/tracefs.h>
  43. #include "trace-event.h" /* For __maybe_unused */
  44. #include "probe-event.h"
  45. #include "probe-finder.h"
  46. #include "session.h"
  47. #define MAX_CMDLEN 256
  48. #define PERFPROBE_GROUP "probe"
  49. bool probe_event_dry_run; /* Dry run flag */
  50. struct probe_conf probe_conf;
  51. #define semantic_error(msg ...) pr_err("Semantic error :" msg)
  52. /* If there is no space to write, returns -E2BIG. */
  53. static int e_snprintf(char *str, size_t size, const char *format, ...)
  54. __attribute__((format(printf, 3, 4)));
  55. static int e_snprintf(char *str, size_t size, const char *format, ...)
  56. {
  57. int ret;
  58. va_list ap;
  59. va_start(ap, format);
  60. ret = vsnprintf(str, size, format, ap);
  61. va_end(ap);
  62. if (ret >= (int)size)
  63. ret = -E2BIG;
  64. return ret;
  65. }
  66. static char *synthesize_perf_probe_point(struct perf_probe_point *pp);
  67. static void clear_probe_trace_event(struct probe_trace_event *tev);
  68. static struct machine *host_machine;
  69. /* Initialize symbol maps and path of vmlinux/modules */
  70. static int init_symbol_maps(bool user_only)
  71. {
  72. int ret;
  73. symbol_conf.sort_by_name = true;
  74. symbol_conf.allow_aliases = true;
  75. ret = symbol__init(NULL);
  76. if (ret < 0) {
  77. pr_debug("Failed to init symbol map.\n");
  78. goto out;
  79. }
  80. if (host_machine || user_only) /* already initialized */
  81. return 0;
  82. if (symbol_conf.vmlinux_name)
  83. pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
  84. host_machine = machine__new_host();
  85. if (!host_machine) {
  86. pr_debug("machine__new_host() failed.\n");
  87. symbol__exit();
  88. ret = -1;
  89. }
  90. out:
  91. if (ret < 0)
  92. pr_warning("Failed to init vmlinux path.\n");
  93. return ret;
  94. }
  95. static void exit_symbol_maps(void)
  96. {
  97. if (host_machine) {
  98. machine__delete(host_machine);
  99. host_machine = NULL;
  100. }
  101. symbol__exit();
  102. }
  103. static struct symbol *__find_kernel_function_by_name(const char *name,
  104. struct map **mapp)
  105. {
  106. return machine__find_kernel_function_by_name(host_machine, name, mapp,
  107. NULL);
  108. }
  109. static struct symbol *__find_kernel_function(u64 addr, struct map **mapp)
  110. {
  111. return machine__find_kernel_function(host_machine, addr, mapp, NULL);
  112. }
  113. static struct ref_reloc_sym *kernel_get_ref_reloc_sym(void)
  114. {
  115. /* kmap->ref_reloc_sym should be set if host_machine is initialized */
  116. struct kmap *kmap;
  117. if (map__load(host_machine->vmlinux_maps[MAP__FUNCTION], NULL) < 0)
  118. return NULL;
  119. kmap = map__kmap(host_machine->vmlinux_maps[MAP__FUNCTION]);
  120. if (!kmap)
  121. return NULL;
  122. return kmap->ref_reloc_sym;
  123. }
  124. static u64 kernel_get_symbol_address_by_name(const char *name, bool reloc)
  125. {
  126. struct ref_reloc_sym *reloc_sym;
  127. struct symbol *sym;
  128. struct map *map;
  129. /* ref_reloc_sym is just a label. Need a special fix*/
  130. reloc_sym = kernel_get_ref_reloc_sym();
  131. if (reloc_sym && strcmp(name, reloc_sym->name) == 0)
  132. return (reloc) ? reloc_sym->addr : reloc_sym->unrelocated_addr;
  133. else {
  134. sym = __find_kernel_function_by_name(name, &map);
  135. if (sym)
  136. return map->unmap_ip(map, sym->start) -
  137. ((reloc) ? 0 : map->reloc);
  138. }
  139. return 0;
  140. }
  141. static struct map *kernel_get_module_map(const char *module)
  142. {
  143. struct map_groups *grp = &host_machine->kmaps;
  144. struct maps *maps = &grp->maps[MAP__FUNCTION];
  145. struct map *pos;
  146. /* A file path -- this is an offline module */
  147. if (module && strchr(module, '/'))
  148. return machine__new_module(host_machine, 0, module);
  149. if (!module)
  150. module = "kernel";
  151. for (pos = maps__first(maps); pos; pos = map__next(pos)) {
  152. if (strncmp(pos->dso->short_name + 1, module,
  153. pos->dso->short_name_len - 2) == 0) {
  154. return pos;
  155. }
  156. }
  157. return NULL;
  158. }
  159. static struct map *get_target_map(const char *target, bool user)
  160. {
  161. /* Init maps of given executable or kernel */
  162. if (user)
  163. return dso__new_map(target);
  164. else
  165. return kernel_get_module_map(target);
  166. }
  167. static void put_target_map(struct map *map, bool user)
  168. {
  169. if (map && user) {
  170. /* Only the user map needs to be released */
  171. map__put(map);
  172. }
  173. }
  174. static int convert_exec_to_group(const char *exec, char **result)
  175. {
  176. char *ptr1, *ptr2, *exec_copy;
  177. char buf[64];
  178. int ret;
  179. exec_copy = strdup(exec);
  180. if (!exec_copy)
  181. return -ENOMEM;
  182. ptr1 = basename(exec_copy);
  183. if (!ptr1) {
  184. ret = -EINVAL;
  185. goto out;
  186. }
  187. ptr2 = strpbrk(ptr1, "-._");
  188. if (ptr2)
  189. *ptr2 = '\0';
  190. ret = e_snprintf(buf, 64, "%s_%s", PERFPROBE_GROUP, ptr1);
  191. if (ret < 0)
  192. goto out;
  193. *result = strdup(buf);
  194. ret = *result ? 0 : -ENOMEM;
  195. out:
  196. free(exec_copy);
  197. return ret;
  198. }
  199. static void clear_perf_probe_point(struct perf_probe_point *pp)
  200. {
  201. free(pp->file);
  202. free(pp->function);
  203. free(pp->lazy_line);
  204. }
  205. static void clear_probe_trace_events(struct probe_trace_event *tevs, int ntevs)
  206. {
  207. int i;
  208. for (i = 0; i < ntevs; i++)
  209. clear_probe_trace_event(tevs + i);
  210. }
  211. #ifdef HAVE_DWARF_SUPPORT
  212. static int kernel_get_module_dso(const char *module, struct dso **pdso)
  213. {
  214. struct dso *dso;
  215. struct map *map;
  216. const char *vmlinux_name;
  217. int ret = 0;
  218. if (module) {
  219. list_for_each_entry(dso, &host_machine->dsos.head, node) {
  220. if (!dso->kernel)
  221. continue;
  222. if (strncmp(dso->short_name + 1, module,
  223. dso->short_name_len - 2) == 0)
  224. goto found;
  225. }
  226. pr_debug("Failed to find module %s.\n", module);
  227. return -ENOENT;
  228. }
  229. map = host_machine->vmlinux_maps[MAP__FUNCTION];
  230. dso = map->dso;
  231. vmlinux_name = symbol_conf.vmlinux_name;
  232. dso->load_errno = 0;
  233. if (vmlinux_name)
  234. ret = dso__load_vmlinux(dso, map, vmlinux_name, false, NULL);
  235. else
  236. ret = dso__load_vmlinux_path(dso, map, NULL);
  237. found:
  238. *pdso = dso;
  239. return ret;
  240. }
  241. /*
  242. * Some binaries like glibc have special symbols which are on the symbol
  243. * table, but not in the debuginfo. If we can find the address of the
  244. * symbol from map, we can translate the address back to the probe point.
  245. */
  246. static int find_alternative_probe_point(struct debuginfo *dinfo,
  247. struct perf_probe_point *pp,
  248. struct perf_probe_point *result,
  249. const char *target, bool uprobes)
  250. {
  251. struct map *map = NULL;
  252. struct symbol *sym;
  253. u64 address = 0;
  254. int ret = -ENOENT;
  255. /* This can work only for function-name based one */
  256. if (!pp->function || pp->file)
  257. return -ENOTSUP;
  258. map = get_target_map(target, uprobes);
  259. if (!map)
  260. return -EINVAL;
  261. /* Find the address of given function */
  262. map__for_each_symbol_by_name(map, pp->function, sym) {
  263. if (uprobes)
  264. address = sym->start;
  265. else
  266. address = map->unmap_ip(map, sym->start);
  267. break;
  268. }
  269. if (!address) {
  270. ret = -ENOENT;
  271. goto out;
  272. }
  273. pr_debug("Symbol %s address found : %" PRIx64 "\n",
  274. pp->function, address);
  275. ret = debuginfo__find_probe_point(dinfo, (unsigned long)address,
  276. result);
  277. if (ret <= 0)
  278. ret = (!ret) ? -ENOENT : ret;
  279. else {
  280. result->offset += pp->offset;
  281. result->line += pp->line;
  282. result->retprobe = pp->retprobe;
  283. ret = 0;
  284. }
  285. out:
  286. put_target_map(map, uprobes);
  287. return ret;
  288. }
  289. static int get_alternative_probe_event(struct debuginfo *dinfo,
  290. struct perf_probe_event *pev,
  291. struct perf_probe_point *tmp)
  292. {
  293. int ret;
  294. memcpy(tmp, &pev->point, sizeof(*tmp));
  295. memset(&pev->point, 0, sizeof(pev->point));
  296. ret = find_alternative_probe_point(dinfo, tmp, &pev->point,
  297. pev->target, pev->uprobes);
  298. if (ret < 0)
  299. memcpy(&pev->point, tmp, sizeof(*tmp));
  300. return ret;
  301. }
  302. static int get_alternative_line_range(struct debuginfo *dinfo,
  303. struct line_range *lr,
  304. const char *target, bool user)
  305. {
  306. struct perf_probe_point pp = { .function = lr->function,
  307. .file = lr->file,
  308. .line = lr->start };
  309. struct perf_probe_point result;
  310. int ret, len = 0;
  311. memset(&result, 0, sizeof(result));
  312. if (lr->end != INT_MAX)
  313. len = lr->end - lr->start;
  314. ret = find_alternative_probe_point(dinfo, &pp, &result,
  315. target, user);
  316. if (!ret) {
  317. lr->function = result.function;
  318. lr->file = result.file;
  319. lr->start = result.line;
  320. if (lr->end != INT_MAX)
  321. lr->end = lr->start + len;
  322. clear_perf_probe_point(&pp);
  323. }
  324. return ret;
  325. }
  326. /* Open new debuginfo of given module */
  327. static struct debuginfo *open_debuginfo(const char *module, bool silent)
  328. {
  329. const char *path = module;
  330. char reason[STRERR_BUFSIZE];
  331. struct debuginfo *ret = NULL;
  332. struct dso *dso = NULL;
  333. int err;
  334. if (!module || !strchr(module, '/')) {
  335. err = kernel_get_module_dso(module, &dso);
  336. if (err < 0) {
  337. if (!dso || dso->load_errno == 0) {
  338. if (!strerror_r(-err, reason, STRERR_BUFSIZE))
  339. strcpy(reason, "(unknown)");
  340. } else
  341. dso__strerror_load(dso, reason, STRERR_BUFSIZE);
  342. if (!silent)
  343. pr_err("Failed to find the path for %s: %s\n",
  344. module ?: "kernel", reason);
  345. return NULL;
  346. }
  347. path = dso->long_name;
  348. }
  349. ret = debuginfo__new(path);
  350. if (!ret && !silent) {
  351. pr_warning("The %s file has no debug information.\n", path);
  352. if (!module || !strtailcmp(path, ".ko"))
  353. pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, ");
  354. else
  355. pr_warning("Rebuild with -g, ");
  356. pr_warning("or install an appropriate debuginfo package.\n");
  357. }
  358. return ret;
  359. }
  360. static int get_text_start_address(const char *exec, unsigned long *address)
  361. {
  362. Elf *elf;
  363. GElf_Ehdr ehdr;
  364. GElf_Shdr shdr;
  365. int fd, ret = -ENOENT;
  366. fd = open(exec, O_RDONLY);
  367. if (fd < 0)
  368. return -errno;
  369. elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
  370. if (elf == NULL)
  371. return -EINVAL;
  372. if (gelf_getehdr(elf, &ehdr) == NULL)
  373. goto out;
  374. if (!elf_section_by_name(elf, &ehdr, &shdr, ".text", NULL))
  375. goto out;
  376. *address = shdr.sh_addr - shdr.sh_offset;
  377. ret = 0;
  378. out:
  379. elf_end(elf);
  380. return ret;
  381. }
  382. /*
  383. * Convert trace point to probe point with debuginfo
  384. */
  385. static int find_perf_probe_point_from_dwarf(struct probe_trace_point *tp,
  386. struct perf_probe_point *pp,
  387. bool is_kprobe)
  388. {
  389. struct debuginfo *dinfo = NULL;
  390. unsigned long stext = 0;
  391. u64 addr = tp->address;
  392. int ret = -ENOENT;
  393. /* convert the address to dwarf address */
  394. if (!is_kprobe) {
  395. if (!addr) {
  396. ret = -EINVAL;
  397. goto error;
  398. }
  399. ret = get_text_start_address(tp->module, &stext);
  400. if (ret < 0)
  401. goto error;
  402. addr += stext;
  403. } else {
  404. addr = kernel_get_symbol_address_by_name(tp->symbol, false);
  405. if (addr == 0)
  406. goto error;
  407. addr += tp->offset;
  408. }
  409. pr_debug("try to find information at %" PRIx64 " in %s\n", addr,
  410. tp->module ? : "kernel");
  411. dinfo = open_debuginfo(tp->module, verbose == 0);
  412. if (dinfo) {
  413. ret = debuginfo__find_probe_point(dinfo,
  414. (unsigned long)addr, pp);
  415. debuginfo__delete(dinfo);
  416. } else
  417. ret = -ENOENT;
  418. if (ret > 0) {
  419. pp->retprobe = tp->retprobe;
  420. return 0;
  421. }
  422. error:
  423. pr_debug("Failed to find corresponding probes from debuginfo.\n");
  424. return ret ? : -ENOENT;
  425. }
  426. static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs,
  427. int ntevs, const char *exec)
  428. {
  429. int i, ret = 0;
  430. unsigned long stext = 0;
  431. if (!exec)
  432. return 0;
  433. ret = get_text_start_address(exec, &stext);
  434. if (ret < 0)
  435. return ret;
  436. for (i = 0; i < ntevs && ret >= 0; i++) {
  437. /* point.address is the addres of point.symbol + point.offset */
  438. tevs[i].point.address -= stext;
  439. tevs[i].point.module = strdup(exec);
  440. if (!tevs[i].point.module) {
  441. ret = -ENOMEM;
  442. break;
  443. }
  444. tevs[i].uprobes = true;
  445. }
  446. return ret;
  447. }
  448. static int add_module_to_probe_trace_events(struct probe_trace_event *tevs,
  449. int ntevs, const char *module)
  450. {
  451. int i, ret = 0;
  452. char *tmp;
  453. if (!module)
  454. return 0;
  455. tmp = strrchr(module, '/');
  456. if (tmp) {
  457. /* This is a module path -- get the module name */
  458. module = strdup(tmp + 1);
  459. if (!module)
  460. return -ENOMEM;
  461. tmp = strchr(module, '.');
  462. if (tmp)
  463. *tmp = '\0';
  464. tmp = (char *)module; /* For free() */
  465. }
  466. for (i = 0; i < ntevs; i++) {
  467. tevs[i].point.module = strdup(module);
  468. if (!tevs[i].point.module) {
  469. ret = -ENOMEM;
  470. break;
  471. }
  472. }
  473. free(tmp);
  474. return ret;
  475. }
  476. /* Post processing the probe events */
  477. static int post_process_probe_trace_events(struct probe_trace_event *tevs,
  478. int ntevs, const char *module,
  479. bool uprobe)
  480. {
  481. struct ref_reloc_sym *reloc_sym;
  482. u64 etext_addr;
  483. char *tmp;
  484. int i, skipped = 0;
  485. if (uprobe)
  486. return add_exec_to_probe_trace_events(tevs, ntevs, module);
  487. /* Note that currently ref_reloc_sym based probe is not for drivers */
  488. if (module)
  489. return add_module_to_probe_trace_events(tevs, ntevs, module);
  490. reloc_sym = kernel_get_ref_reloc_sym();
  491. if (!reloc_sym) {
  492. pr_warning("Relocated base symbol is not found!\n");
  493. return -EINVAL;
  494. }
  495. /* Get the address of _etext for checking non-probable text symbol */
  496. etext_addr = kernel_get_symbol_address_by_name("_etext", false);
  497. for (i = 0; i < ntevs; i++) {
  498. if (tevs[i].point.address && !tevs[i].point.retprobe) {
  499. /* If we found a wrong one, mark it by NULL symbol */
  500. if (etext_addr < tevs[i].point.address) {
  501. pr_warning("%s+%lu is out of .text, skip it.\n",
  502. tevs[i].point.symbol, tevs[i].point.offset);
  503. tmp = NULL;
  504. skipped++;
  505. } else {
  506. tmp = strdup(reloc_sym->name);
  507. if (!tmp)
  508. return -ENOMEM;
  509. }
  510. /* If we have no realname, use symbol for it */
  511. if (!tevs[i].point.realname)
  512. tevs[i].point.realname = tevs[i].point.symbol;
  513. else
  514. free(tevs[i].point.symbol);
  515. tevs[i].point.symbol = tmp;
  516. tevs[i].point.offset = tevs[i].point.address -
  517. reloc_sym->unrelocated_addr;
  518. }
  519. }
  520. return skipped;
  521. }
  522. /* Try to find perf_probe_event with debuginfo */
  523. static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
  524. struct probe_trace_event **tevs)
  525. {
  526. bool need_dwarf = perf_probe_event_need_dwarf(pev);
  527. struct perf_probe_point tmp;
  528. struct debuginfo *dinfo;
  529. int ntevs, ret = 0;
  530. dinfo = open_debuginfo(pev->target, !need_dwarf);
  531. if (!dinfo) {
  532. if (need_dwarf)
  533. return -ENOENT;
  534. pr_debug("Could not open debuginfo. Try to use symbols.\n");
  535. return 0;
  536. }
  537. pr_debug("Try to find probe point from debuginfo.\n");
  538. /* Searching trace events corresponding to a probe event */
  539. ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
  540. if (ntevs == 0) { /* Not found, retry with an alternative */
  541. ret = get_alternative_probe_event(dinfo, pev, &tmp);
  542. if (!ret) {
  543. ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
  544. /*
  545. * Write back to the original probe_event for
  546. * setting appropriate (user given) event name
  547. */
  548. clear_perf_probe_point(&pev->point);
  549. memcpy(&pev->point, &tmp, sizeof(tmp));
  550. }
  551. }
  552. debuginfo__delete(dinfo);
  553. if (ntevs > 0) { /* Succeeded to find trace events */
  554. pr_debug("Found %d probe_trace_events.\n", ntevs);
  555. ret = post_process_probe_trace_events(*tevs, ntevs,
  556. pev->target, pev->uprobes);
  557. if (ret < 0 || ret == ntevs) {
  558. clear_probe_trace_events(*tevs, ntevs);
  559. zfree(tevs);
  560. }
  561. if (ret != ntevs)
  562. return ret < 0 ? ret : ntevs;
  563. ntevs = 0;
  564. /* Fall through */
  565. }
  566. if (ntevs == 0) { /* No error but failed to find probe point. */
  567. pr_warning("Probe point '%s' not found.\n",
  568. synthesize_perf_probe_point(&pev->point));
  569. return -ENOENT;
  570. }
  571. /* Error path : ntevs < 0 */
  572. pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
  573. if (ntevs == -EBADF) {
  574. pr_warning("Warning: No dwarf info found in the vmlinux - "
  575. "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
  576. if (!need_dwarf) {
  577. pr_debug("Trying to use symbols.\n");
  578. return 0;
  579. }
  580. }
  581. return ntevs;
  582. }
  583. #define LINEBUF_SIZE 256
  584. #define NR_ADDITIONAL_LINES 2
  585. static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
  586. {
  587. char buf[LINEBUF_SIZE], sbuf[STRERR_BUFSIZE];
  588. const char *color = show_num ? "" : PERF_COLOR_BLUE;
  589. const char *prefix = NULL;
  590. do {
  591. if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
  592. goto error;
  593. if (skip)
  594. continue;
  595. if (!prefix) {
  596. prefix = show_num ? "%7d " : " ";
  597. color_fprintf(stdout, color, prefix, l);
  598. }
  599. color_fprintf(stdout, color, "%s", buf);
  600. } while (strchr(buf, '\n') == NULL);
  601. return 1;
  602. error:
  603. if (ferror(fp)) {
  604. pr_warning("File read error: %s\n",
  605. strerror_r(errno, sbuf, sizeof(sbuf)));
  606. return -1;
  607. }
  608. return 0;
  609. }
  610. static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
  611. {
  612. int rv = __show_one_line(fp, l, skip, show_num);
  613. if (rv == 0) {
  614. pr_warning("Source file is shorter than expected.\n");
  615. rv = -1;
  616. }
  617. return rv;
  618. }
  619. #define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
  620. #define show_one_line(f,l) _show_one_line(f,l,false,false)
  621. #define skip_one_line(f,l) _show_one_line(f,l,true,false)
  622. #define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
  623. /*
  624. * Show line-range always requires debuginfo to find source file and
  625. * line number.
  626. */
  627. static int __show_line_range(struct line_range *lr, const char *module,
  628. bool user)
  629. {
  630. int l = 1;
  631. struct int_node *ln;
  632. struct debuginfo *dinfo;
  633. FILE *fp;
  634. int ret;
  635. char *tmp;
  636. char sbuf[STRERR_BUFSIZE];
  637. /* Search a line range */
  638. dinfo = open_debuginfo(module, false);
  639. if (!dinfo)
  640. return -ENOENT;
  641. ret = debuginfo__find_line_range(dinfo, lr);
  642. if (!ret) { /* Not found, retry with an alternative */
  643. ret = get_alternative_line_range(dinfo, lr, module, user);
  644. if (!ret)
  645. ret = debuginfo__find_line_range(dinfo, lr);
  646. }
  647. debuginfo__delete(dinfo);
  648. if (ret == 0 || ret == -ENOENT) {
  649. pr_warning("Specified source line is not found.\n");
  650. return -ENOENT;
  651. } else if (ret < 0) {
  652. pr_warning("Debuginfo analysis failed.\n");
  653. return ret;
  654. }
  655. /* Convert source file path */
  656. tmp = lr->path;
  657. ret = get_real_path(tmp, lr->comp_dir, &lr->path);
  658. /* Free old path when new path is assigned */
  659. if (tmp != lr->path)
  660. free(tmp);
  661. if (ret < 0) {
  662. pr_warning("Failed to find source file path.\n");
  663. return ret;
  664. }
  665. setup_pager();
  666. if (lr->function)
  667. fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
  668. lr->start - lr->offset);
  669. else
  670. fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
  671. fp = fopen(lr->path, "r");
  672. if (fp == NULL) {
  673. pr_warning("Failed to open %s: %s\n", lr->path,
  674. strerror_r(errno, sbuf, sizeof(sbuf)));
  675. return -errno;
  676. }
  677. /* Skip to starting line number */
  678. while (l < lr->start) {
  679. ret = skip_one_line(fp, l++);
  680. if (ret < 0)
  681. goto end;
  682. }
  683. intlist__for_each(ln, lr->line_list) {
  684. for (; ln->i > l; l++) {
  685. ret = show_one_line(fp, l - lr->offset);
  686. if (ret < 0)
  687. goto end;
  688. }
  689. ret = show_one_line_with_num(fp, l++ - lr->offset);
  690. if (ret < 0)
  691. goto end;
  692. }
  693. if (lr->end == INT_MAX)
  694. lr->end = l + NR_ADDITIONAL_LINES;
  695. while (l <= lr->end) {
  696. ret = show_one_line_or_eof(fp, l++ - lr->offset);
  697. if (ret <= 0)
  698. break;
  699. }
  700. end:
  701. fclose(fp);
  702. return ret;
  703. }
  704. int show_line_range(struct line_range *lr, const char *module, bool user)
  705. {
  706. int ret;
  707. ret = init_symbol_maps(user);
  708. if (ret < 0)
  709. return ret;
  710. ret = __show_line_range(lr, module, user);
  711. exit_symbol_maps();
  712. return ret;
  713. }
  714. static int show_available_vars_at(struct debuginfo *dinfo,
  715. struct perf_probe_event *pev,
  716. struct strfilter *_filter)
  717. {
  718. char *buf;
  719. int ret, i, nvars;
  720. struct str_node *node;
  721. struct variable_list *vls = NULL, *vl;
  722. struct perf_probe_point tmp;
  723. const char *var;
  724. buf = synthesize_perf_probe_point(&pev->point);
  725. if (!buf)
  726. return -EINVAL;
  727. pr_debug("Searching variables at %s\n", buf);
  728. ret = debuginfo__find_available_vars_at(dinfo, pev, &vls);
  729. if (!ret) { /* Not found, retry with an alternative */
  730. ret = get_alternative_probe_event(dinfo, pev, &tmp);
  731. if (!ret) {
  732. ret = debuginfo__find_available_vars_at(dinfo, pev,
  733. &vls);
  734. /* Release the old probe_point */
  735. clear_perf_probe_point(&tmp);
  736. }
  737. }
  738. if (ret <= 0) {
  739. if (ret == 0 || ret == -ENOENT) {
  740. pr_err("Failed to find the address of %s\n", buf);
  741. ret = -ENOENT;
  742. } else
  743. pr_warning("Debuginfo analysis failed.\n");
  744. goto end;
  745. }
  746. /* Some variables are found */
  747. fprintf(stdout, "Available variables at %s\n", buf);
  748. for (i = 0; i < ret; i++) {
  749. vl = &vls[i];
  750. /*
  751. * A probe point might be converted to
  752. * several trace points.
  753. */
  754. fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
  755. vl->point.offset);
  756. zfree(&vl->point.symbol);
  757. nvars = 0;
  758. if (vl->vars) {
  759. strlist__for_each(node, vl->vars) {
  760. var = strchr(node->s, '\t') + 1;
  761. if (strfilter__compare(_filter, var)) {
  762. fprintf(stdout, "\t\t%s\n", node->s);
  763. nvars++;
  764. }
  765. }
  766. strlist__delete(vl->vars);
  767. }
  768. if (nvars == 0)
  769. fprintf(stdout, "\t\t(No matched variables)\n");
  770. }
  771. free(vls);
  772. end:
  773. free(buf);
  774. return ret;
  775. }
  776. /* Show available variables on given probe point */
  777. int show_available_vars(struct perf_probe_event *pevs, int npevs,
  778. struct strfilter *_filter)
  779. {
  780. int i, ret = 0;
  781. struct debuginfo *dinfo;
  782. ret = init_symbol_maps(pevs->uprobes);
  783. if (ret < 0)
  784. return ret;
  785. dinfo = open_debuginfo(pevs->target, false);
  786. if (!dinfo) {
  787. ret = -ENOENT;
  788. goto out;
  789. }
  790. setup_pager();
  791. for (i = 0; i < npevs && ret >= 0; i++)
  792. ret = show_available_vars_at(dinfo, &pevs[i], _filter);
  793. debuginfo__delete(dinfo);
  794. out:
  795. exit_symbol_maps();
  796. return ret;
  797. }
  798. #else /* !HAVE_DWARF_SUPPORT */
  799. static int
  800. find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused,
  801. struct perf_probe_point *pp __maybe_unused,
  802. bool is_kprobe __maybe_unused)
  803. {
  804. return -ENOSYS;
  805. }
  806. static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
  807. struct probe_trace_event **tevs __maybe_unused)
  808. {
  809. if (perf_probe_event_need_dwarf(pev)) {
  810. pr_warning("Debuginfo-analysis is not supported.\n");
  811. return -ENOSYS;
  812. }
  813. return 0;
  814. }
  815. int show_line_range(struct line_range *lr __maybe_unused,
  816. const char *module __maybe_unused,
  817. bool user __maybe_unused)
  818. {
  819. pr_warning("Debuginfo-analysis is not supported.\n");
  820. return -ENOSYS;
  821. }
  822. int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
  823. int npevs __maybe_unused,
  824. struct strfilter *filter __maybe_unused)
  825. {
  826. pr_warning("Debuginfo-analysis is not supported.\n");
  827. return -ENOSYS;
  828. }
  829. #endif
  830. void line_range__clear(struct line_range *lr)
  831. {
  832. free(lr->function);
  833. free(lr->file);
  834. free(lr->path);
  835. free(lr->comp_dir);
  836. intlist__delete(lr->line_list);
  837. memset(lr, 0, sizeof(*lr));
  838. }
  839. int line_range__init(struct line_range *lr)
  840. {
  841. memset(lr, 0, sizeof(*lr));
  842. lr->line_list = intlist__new(NULL);
  843. if (!lr->line_list)
  844. return -ENOMEM;
  845. else
  846. return 0;
  847. }
  848. static int parse_line_num(char **ptr, int *val, const char *what)
  849. {
  850. const char *start = *ptr;
  851. errno = 0;
  852. *val = strtol(*ptr, ptr, 0);
  853. if (errno || *ptr == start) {
  854. semantic_error("'%s' is not a valid number.\n", what);
  855. return -EINVAL;
  856. }
  857. return 0;
  858. }
  859. /* Check the name is good for event, group or function */
  860. static bool is_c_func_name(const char *name)
  861. {
  862. if (!isalpha(*name) && *name != '_')
  863. return false;
  864. while (*++name != '\0') {
  865. if (!isalpha(*name) && !isdigit(*name) && *name != '_')
  866. return false;
  867. }
  868. return true;
  869. }
  870. /*
  871. * Stuff 'lr' according to the line range described by 'arg'.
  872. * The line range syntax is described by:
  873. *
  874. * SRC[:SLN[+NUM|-ELN]]
  875. * FNC[@SRC][:SLN[+NUM|-ELN]]
  876. */
  877. int parse_line_range_desc(const char *arg, struct line_range *lr)
  878. {
  879. char *range, *file, *name = strdup(arg);
  880. int err;
  881. if (!name)
  882. return -ENOMEM;
  883. lr->start = 0;
  884. lr->end = INT_MAX;
  885. range = strchr(name, ':');
  886. if (range) {
  887. *range++ = '\0';
  888. err = parse_line_num(&range, &lr->start, "start line");
  889. if (err)
  890. goto err;
  891. if (*range == '+' || *range == '-') {
  892. const char c = *range++;
  893. err = parse_line_num(&range, &lr->end, "end line");
  894. if (err)
  895. goto err;
  896. if (c == '+') {
  897. lr->end += lr->start;
  898. /*
  899. * Adjust the number of lines here.
  900. * If the number of lines == 1, the
  901. * the end of line should be equal to
  902. * the start of line.
  903. */
  904. lr->end--;
  905. }
  906. }
  907. pr_debug("Line range is %d to %d\n", lr->start, lr->end);
  908. err = -EINVAL;
  909. if (lr->start > lr->end) {
  910. semantic_error("Start line must be smaller"
  911. " than end line.\n");
  912. goto err;
  913. }
  914. if (*range != '\0') {
  915. semantic_error("Tailing with invalid str '%s'.\n", range);
  916. goto err;
  917. }
  918. }
  919. file = strchr(name, '@');
  920. if (file) {
  921. *file = '\0';
  922. lr->file = strdup(++file);
  923. if (lr->file == NULL) {
  924. err = -ENOMEM;
  925. goto err;
  926. }
  927. lr->function = name;
  928. } else if (strchr(name, '/') || strchr(name, '.'))
  929. lr->file = name;
  930. else if (is_c_func_name(name))/* We reuse it for checking funcname */
  931. lr->function = name;
  932. else { /* Invalid name */
  933. semantic_error("'%s' is not a valid function name.\n", name);
  934. err = -EINVAL;
  935. goto err;
  936. }
  937. return 0;
  938. err:
  939. free(name);
  940. return err;
  941. }
  942. /* Parse probepoint definition. */
  943. static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
  944. {
  945. struct perf_probe_point *pp = &pev->point;
  946. char *ptr, *tmp;
  947. char c, nc = 0;
  948. bool file_spec = false;
  949. /*
  950. * <Syntax>
  951. * perf probe [EVENT=]SRC[:LN|;PTN]
  952. * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
  953. *
  954. * TODO:Group name support
  955. */
  956. if (!arg)
  957. return -EINVAL;
  958. ptr = strpbrk(arg, ";=@+%");
  959. if (ptr && *ptr == '=') { /* Event name */
  960. *ptr = '\0';
  961. tmp = ptr + 1;
  962. if (strchr(arg, ':')) {
  963. semantic_error("Group name is not supported yet.\n");
  964. return -ENOTSUP;
  965. }
  966. if (!is_c_func_name(arg)) {
  967. semantic_error("%s is bad for event name -it must "
  968. "follow C symbol-naming rule.\n", arg);
  969. return -EINVAL;
  970. }
  971. pev->event = strdup(arg);
  972. if (pev->event == NULL)
  973. return -ENOMEM;
  974. pev->group = NULL;
  975. arg = tmp;
  976. }
  977. /*
  978. * Check arg is function or file name and copy it.
  979. *
  980. * We consider arg to be a file spec if and only if it satisfies
  981. * all of the below criteria::
  982. * - it does not include any of "+@%",
  983. * - it includes one of ":;", and
  984. * - it has a period '.' in the name.
  985. *
  986. * Otherwise, we consider arg to be a function specification.
  987. */
  988. if (!strpbrk(arg, "+@%") && (ptr = strpbrk(arg, ";:")) != NULL) {
  989. /* This is a file spec if it includes a '.' before ; or : */
  990. if (memchr(arg, '.', ptr - arg))
  991. file_spec = true;
  992. }
  993. ptr = strpbrk(arg, ";:+@%");
  994. if (ptr) {
  995. nc = *ptr;
  996. *ptr++ = '\0';
  997. }
  998. tmp = strdup(arg);
  999. if (tmp == NULL)
  1000. return -ENOMEM;
  1001. if (file_spec)
  1002. pp->file = tmp;
  1003. else
  1004. pp->function = tmp;
  1005. /* Parse other options */
  1006. while (ptr) {
  1007. arg = ptr;
  1008. c = nc;
  1009. if (c == ';') { /* Lazy pattern must be the last part */
  1010. pp->lazy_line = strdup(arg);
  1011. if (pp->lazy_line == NULL)
  1012. return -ENOMEM;
  1013. break;
  1014. }
  1015. ptr = strpbrk(arg, ";:+@%");
  1016. if (ptr) {
  1017. nc = *ptr;
  1018. *ptr++ = '\0';
  1019. }
  1020. switch (c) {
  1021. case ':': /* Line number */
  1022. pp->line = strtoul(arg, &tmp, 0);
  1023. if (*tmp != '\0') {
  1024. semantic_error("There is non-digit char"
  1025. " in line number.\n");
  1026. return -EINVAL;
  1027. }
  1028. break;
  1029. case '+': /* Byte offset from a symbol */
  1030. pp->offset = strtoul(arg, &tmp, 0);
  1031. if (*tmp != '\0') {
  1032. semantic_error("There is non-digit character"
  1033. " in offset.\n");
  1034. return -EINVAL;
  1035. }
  1036. break;
  1037. case '@': /* File name */
  1038. if (pp->file) {
  1039. semantic_error("SRC@SRC is not allowed.\n");
  1040. return -EINVAL;
  1041. }
  1042. pp->file = strdup(arg);
  1043. if (pp->file == NULL)
  1044. return -ENOMEM;
  1045. break;
  1046. case '%': /* Probe places */
  1047. if (strcmp(arg, "return") == 0) {
  1048. pp->retprobe = 1;
  1049. } else { /* Others not supported yet */
  1050. semantic_error("%%%s is not supported.\n", arg);
  1051. return -ENOTSUP;
  1052. }
  1053. break;
  1054. default: /* Buggy case */
  1055. pr_err("This program has a bug at %s:%d.\n",
  1056. __FILE__, __LINE__);
  1057. return -ENOTSUP;
  1058. break;
  1059. }
  1060. }
  1061. /* Exclusion check */
  1062. if (pp->lazy_line && pp->line) {
  1063. semantic_error("Lazy pattern can't be used with"
  1064. " line number.\n");
  1065. return -EINVAL;
  1066. }
  1067. if (pp->lazy_line && pp->offset) {
  1068. semantic_error("Lazy pattern can't be used with offset.\n");
  1069. return -EINVAL;
  1070. }
  1071. if (pp->line && pp->offset) {
  1072. semantic_error("Offset can't be used with line number.\n");
  1073. return -EINVAL;
  1074. }
  1075. if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
  1076. semantic_error("File always requires line number or "
  1077. "lazy pattern.\n");
  1078. return -EINVAL;
  1079. }
  1080. if (pp->offset && !pp->function) {
  1081. semantic_error("Offset requires an entry function.\n");
  1082. return -EINVAL;
  1083. }
  1084. if (pp->retprobe && !pp->function) {
  1085. semantic_error("Return probe requires an entry function.\n");
  1086. return -EINVAL;
  1087. }
  1088. if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
  1089. semantic_error("Offset/Line/Lazy pattern can't be used with "
  1090. "return probe.\n");
  1091. return -EINVAL;
  1092. }
  1093. pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
  1094. pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
  1095. pp->lazy_line);
  1096. return 0;
  1097. }
  1098. /* Parse perf-probe event argument */
  1099. static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
  1100. {
  1101. char *tmp, *goodname;
  1102. struct perf_probe_arg_field **fieldp;
  1103. pr_debug("parsing arg: %s into ", str);
  1104. tmp = strchr(str, '=');
  1105. if (tmp) {
  1106. arg->name = strndup(str, tmp - str);
  1107. if (arg->name == NULL)
  1108. return -ENOMEM;
  1109. pr_debug("name:%s ", arg->name);
  1110. str = tmp + 1;
  1111. }
  1112. tmp = strchr(str, ':');
  1113. if (tmp) { /* Type setting */
  1114. *tmp = '\0';
  1115. arg->type = strdup(tmp + 1);
  1116. if (arg->type == NULL)
  1117. return -ENOMEM;
  1118. pr_debug("type:%s ", arg->type);
  1119. }
  1120. tmp = strpbrk(str, "-.[");
  1121. if (!is_c_varname(str) || !tmp) {
  1122. /* A variable, register, symbol or special value */
  1123. arg->var = strdup(str);
  1124. if (arg->var == NULL)
  1125. return -ENOMEM;
  1126. pr_debug("%s\n", arg->var);
  1127. return 0;
  1128. }
  1129. /* Structure fields or array element */
  1130. arg->var = strndup(str, tmp - str);
  1131. if (arg->var == NULL)
  1132. return -ENOMEM;
  1133. goodname = arg->var;
  1134. pr_debug("%s, ", arg->var);
  1135. fieldp = &arg->field;
  1136. do {
  1137. *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
  1138. if (*fieldp == NULL)
  1139. return -ENOMEM;
  1140. if (*tmp == '[') { /* Array */
  1141. str = tmp;
  1142. (*fieldp)->index = strtol(str + 1, &tmp, 0);
  1143. (*fieldp)->ref = true;
  1144. if (*tmp != ']' || tmp == str + 1) {
  1145. semantic_error("Array index must be a"
  1146. " number.\n");
  1147. return -EINVAL;
  1148. }
  1149. tmp++;
  1150. if (*tmp == '\0')
  1151. tmp = NULL;
  1152. } else { /* Structure */
  1153. if (*tmp == '.') {
  1154. str = tmp + 1;
  1155. (*fieldp)->ref = false;
  1156. } else if (tmp[1] == '>') {
  1157. str = tmp + 2;
  1158. (*fieldp)->ref = true;
  1159. } else {
  1160. semantic_error("Argument parse error: %s\n",
  1161. str);
  1162. return -EINVAL;
  1163. }
  1164. tmp = strpbrk(str, "-.[");
  1165. }
  1166. if (tmp) {
  1167. (*fieldp)->name = strndup(str, tmp - str);
  1168. if ((*fieldp)->name == NULL)
  1169. return -ENOMEM;
  1170. if (*str != '[')
  1171. goodname = (*fieldp)->name;
  1172. pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
  1173. fieldp = &(*fieldp)->next;
  1174. }
  1175. } while (tmp);
  1176. (*fieldp)->name = strdup(str);
  1177. if ((*fieldp)->name == NULL)
  1178. return -ENOMEM;
  1179. if (*str != '[')
  1180. goodname = (*fieldp)->name;
  1181. pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
  1182. /* If no name is specified, set the last field name (not array index)*/
  1183. if (!arg->name) {
  1184. arg->name = strdup(goodname);
  1185. if (arg->name == NULL)
  1186. return -ENOMEM;
  1187. }
  1188. return 0;
  1189. }
  1190. /* Parse perf-probe event command */
  1191. int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
  1192. {
  1193. char **argv;
  1194. int argc, i, ret = 0;
  1195. argv = argv_split(cmd, &argc);
  1196. if (!argv) {
  1197. pr_debug("Failed to split arguments.\n");
  1198. return -ENOMEM;
  1199. }
  1200. if (argc - 1 > MAX_PROBE_ARGS) {
  1201. semantic_error("Too many probe arguments (%d).\n", argc - 1);
  1202. ret = -ERANGE;
  1203. goto out;
  1204. }
  1205. /* Parse probe point */
  1206. ret = parse_perf_probe_point(argv[0], pev);
  1207. if (ret < 0)
  1208. goto out;
  1209. /* Copy arguments and ensure return probe has no C argument */
  1210. pev->nargs = argc - 1;
  1211. pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
  1212. if (pev->args == NULL) {
  1213. ret = -ENOMEM;
  1214. goto out;
  1215. }
  1216. for (i = 0; i < pev->nargs && ret >= 0; i++) {
  1217. ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
  1218. if (ret >= 0 &&
  1219. is_c_varname(pev->args[i].var) && pev->point.retprobe) {
  1220. semantic_error("You can't specify local variable for"
  1221. " kretprobe.\n");
  1222. ret = -EINVAL;
  1223. }
  1224. }
  1225. out:
  1226. argv_free(argv);
  1227. return ret;
  1228. }
  1229. /* Return true if this perf_probe_event requires debuginfo */
  1230. bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
  1231. {
  1232. int i;
  1233. if (pev->point.file || pev->point.line || pev->point.lazy_line)
  1234. return true;
  1235. for (i = 0; i < pev->nargs; i++)
  1236. if (is_c_varname(pev->args[i].var))
  1237. return true;
  1238. return false;
  1239. }
  1240. /* Parse probe_events event into struct probe_point */
  1241. static int parse_probe_trace_command(const char *cmd,
  1242. struct probe_trace_event *tev)
  1243. {
  1244. struct probe_trace_point *tp = &tev->point;
  1245. char pr;
  1246. char *p;
  1247. char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
  1248. int ret, i, argc;
  1249. char **argv;
  1250. pr_debug("Parsing probe_events: %s\n", cmd);
  1251. argv = argv_split(cmd, &argc);
  1252. if (!argv) {
  1253. pr_debug("Failed to split arguments.\n");
  1254. return -ENOMEM;
  1255. }
  1256. if (argc < 2) {
  1257. semantic_error("Too few probe arguments.\n");
  1258. ret = -ERANGE;
  1259. goto out;
  1260. }
  1261. /* Scan event and group name. */
  1262. argv0_str = strdup(argv[0]);
  1263. if (argv0_str == NULL) {
  1264. ret = -ENOMEM;
  1265. goto out;
  1266. }
  1267. fmt1_str = strtok_r(argv0_str, ":", &fmt);
  1268. fmt2_str = strtok_r(NULL, "/", &fmt);
  1269. fmt3_str = strtok_r(NULL, " \t", &fmt);
  1270. if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
  1271. || fmt3_str == NULL) {
  1272. semantic_error("Failed to parse event name: %s\n", argv[0]);
  1273. ret = -EINVAL;
  1274. goto out;
  1275. }
  1276. pr = fmt1_str[0];
  1277. tev->group = strdup(fmt2_str);
  1278. tev->event = strdup(fmt3_str);
  1279. if (tev->group == NULL || tev->event == NULL) {
  1280. ret = -ENOMEM;
  1281. goto out;
  1282. }
  1283. pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
  1284. tp->retprobe = (pr == 'r');
  1285. /* Scan module name(if there), function name and offset */
  1286. p = strchr(argv[1], ':');
  1287. if (p) {
  1288. tp->module = strndup(argv[1], p - argv[1]);
  1289. p++;
  1290. } else
  1291. p = argv[1];
  1292. fmt1_str = strtok_r(p, "+", &fmt);
  1293. if (fmt1_str[0] == '0') /* only the address started with 0x */
  1294. tp->address = strtoul(fmt1_str, NULL, 0);
  1295. else {
  1296. /* Only the symbol-based probe has offset */
  1297. tp->symbol = strdup(fmt1_str);
  1298. if (tp->symbol == NULL) {
  1299. ret = -ENOMEM;
  1300. goto out;
  1301. }
  1302. fmt2_str = strtok_r(NULL, "", &fmt);
  1303. if (fmt2_str == NULL)
  1304. tp->offset = 0;
  1305. else
  1306. tp->offset = strtoul(fmt2_str, NULL, 10);
  1307. }
  1308. tev->nargs = argc - 2;
  1309. tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
  1310. if (tev->args == NULL) {
  1311. ret = -ENOMEM;
  1312. goto out;
  1313. }
  1314. for (i = 0; i < tev->nargs; i++) {
  1315. p = strchr(argv[i + 2], '=');
  1316. if (p) /* We don't need which register is assigned. */
  1317. *p++ = '\0';
  1318. else
  1319. p = argv[i + 2];
  1320. tev->args[i].name = strdup(argv[i + 2]);
  1321. /* TODO: parse regs and offset */
  1322. tev->args[i].value = strdup(p);
  1323. if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
  1324. ret = -ENOMEM;
  1325. goto out;
  1326. }
  1327. }
  1328. ret = 0;
  1329. out:
  1330. free(argv0_str);
  1331. argv_free(argv);
  1332. return ret;
  1333. }
  1334. /* Compose only probe arg */
  1335. int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
  1336. {
  1337. struct perf_probe_arg_field *field = pa->field;
  1338. int ret;
  1339. char *tmp = buf;
  1340. if (pa->name && pa->var)
  1341. ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
  1342. else
  1343. ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
  1344. if (ret <= 0)
  1345. goto error;
  1346. tmp += ret;
  1347. len -= ret;
  1348. while (field) {
  1349. if (field->name[0] == '[')
  1350. ret = e_snprintf(tmp, len, "%s", field->name);
  1351. else
  1352. ret = e_snprintf(tmp, len, "%s%s",
  1353. field->ref ? "->" : ".", field->name);
  1354. if (ret <= 0)
  1355. goto error;
  1356. tmp += ret;
  1357. len -= ret;
  1358. field = field->next;
  1359. }
  1360. if (pa->type) {
  1361. ret = e_snprintf(tmp, len, ":%s", pa->type);
  1362. if (ret <= 0)
  1363. goto error;
  1364. tmp += ret;
  1365. len -= ret;
  1366. }
  1367. return tmp - buf;
  1368. error:
  1369. pr_debug("Failed to synthesize perf probe argument: %d\n", ret);
  1370. return ret;
  1371. }
  1372. /* Compose only probe point (not argument) */
  1373. static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
  1374. {
  1375. char *buf, *tmp;
  1376. char offs[32] = "", line[32] = "", file[32] = "";
  1377. int ret, len;
  1378. buf = zalloc(MAX_CMDLEN);
  1379. if (buf == NULL) {
  1380. ret = -ENOMEM;
  1381. goto error;
  1382. }
  1383. if (pp->offset) {
  1384. ret = e_snprintf(offs, 32, "+%lu", pp->offset);
  1385. if (ret <= 0)
  1386. goto error;
  1387. }
  1388. if (pp->line) {
  1389. ret = e_snprintf(line, 32, ":%d", pp->line);
  1390. if (ret <= 0)
  1391. goto error;
  1392. }
  1393. if (pp->file) {
  1394. tmp = pp->file;
  1395. len = strlen(tmp);
  1396. if (len > 30) {
  1397. tmp = strchr(pp->file + len - 30, '/');
  1398. tmp = tmp ? tmp + 1 : pp->file + len - 30;
  1399. }
  1400. ret = e_snprintf(file, 32, "@%s", tmp);
  1401. if (ret <= 0)
  1402. goto error;
  1403. }
  1404. if (pp->function)
  1405. ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
  1406. offs, pp->retprobe ? "%return" : "", line,
  1407. file);
  1408. else
  1409. ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
  1410. if (ret <= 0)
  1411. goto error;
  1412. return buf;
  1413. error:
  1414. pr_debug("Failed to synthesize perf probe point: %d\n", ret);
  1415. free(buf);
  1416. return NULL;
  1417. }
  1418. #if 0
  1419. char *synthesize_perf_probe_command(struct perf_probe_event *pev)
  1420. {
  1421. char *buf;
  1422. int i, len, ret;
  1423. buf = synthesize_perf_probe_point(&pev->point);
  1424. if (!buf)
  1425. return NULL;
  1426. len = strlen(buf);
  1427. for (i = 0; i < pev->nargs; i++) {
  1428. ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
  1429. pev->args[i].name);
  1430. if (ret <= 0) {
  1431. free(buf);
  1432. return NULL;
  1433. }
  1434. len += ret;
  1435. }
  1436. return buf;
  1437. }
  1438. #endif
  1439. static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
  1440. char **buf, size_t *buflen,
  1441. int depth)
  1442. {
  1443. int ret;
  1444. if (ref->next) {
  1445. depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
  1446. buflen, depth + 1);
  1447. if (depth < 0)
  1448. goto out;
  1449. }
  1450. ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
  1451. if (ret < 0)
  1452. depth = ret;
  1453. else {
  1454. *buf += ret;
  1455. *buflen -= ret;
  1456. }
  1457. out:
  1458. return depth;
  1459. }
  1460. static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
  1461. char *buf, size_t buflen)
  1462. {
  1463. struct probe_trace_arg_ref *ref = arg->ref;
  1464. int ret, depth = 0;
  1465. char *tmp = buf;
  1466. /* Argument name or separator */
  1467. if (arg->name)
  1468. ret = e_snprintf(buf, buflen, " %s=", arg->name);
  1469. else
  1470. ret = e_snprintf(buf, buflen, " ");
  1471. if (ret < 0)
  1472. return ret;
  1473. buf += ret;
  1474. buflen -= ret;
  1475. /* Special case: @XXX */
  1476. if (arg->value[0] == '@' && arg->ref)
  1477. ref = ref->next;
  1478. /* Dereferencing arguments */
  1479. if (ref) {
  1480. depth = __synthesize_probe_trace_arg_ref(ref, &buf,
  1481. &buflen, 1);
  1482. if (depth < 0)
  1483. return depth;
  1484. }
  1485. /* Print argument value */
  1486. if (arg->value[0] == '@' && arg->ref)
  1487. ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
  1488. arg->ref->offset);
  1489. else
  1490. ret = e_snprintf(buf, buflen, "%s", arg->value);
  1491. if (ret < 0)
  1492. return ret;
  1493. buf += ret;
  1494. buflen -= ret;
  1495. /* Closing */
  1496. while (depth--) {
  1497. ret = e_snprintf(buf, buflen, ")");
  1498. if (ret < 0)
  1499. return ret;
  1500. buf += ret;
  1501. buflen -= ret;
  1502. }
  1503. /* Print argument type */
  1504. if (arg->type) {
  1505. ret = e_snprintf(buf, buflen, ":%s", arg->type);
  1506. if (ret <= 0)
  1507. return ret;
  1508. buf += ret;
  1509. }
  1510. return buf - tmp;
  1511. }
  1512. char *synthesize_probe_trace_command(struct probe_trace_event *tev)
  1513. {
  1514. struct probe_trace_point *tp = &tev->point;
  1515. char *buf;
  1516. int i, len, ret;
  1517. buf = zalloc(MAX_CMDLEN);
  1518. if (buf == NULL)
  1519. return NULL;
  1520. len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
  1521. tev->group, tev->event);
  1522. if (len <= 0)
  1523. goto error;
  1524. /* Uprobes must have tp->address and tp->module */
  1525. if (tev->uprobes && (!tp->address || !tp->module))
  1526. goto error;
  1527. /* Use the tp->address for uprobes */
  1528. if (tev->uprobes)
  1529. ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s:0x%lx",
  1530. tp->module, tp->address);
  1531. else
  1532. ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s%s%s+%lu",
  1533. tp->module ?: "", tp->module ? ":" : "",
  1534. tp->symbol, tp->offset);
  1535. if (ret <= 0)
  1536. goto error;
  1537. len += ret;
  1538. for (i = 0; i < tev->nargs; i++) {
  1539. ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
  1540. MAX_CMDLEN - len);
  1541. if (ret <= 0)
  1542. goto error;
  1543. len += ret;
  1544. }
  1545. return buf;
  1546. error:
  1547. free(buf);
  1548. return NULL;
  1549. }
  1550. static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
  1551. struct perf_probe_point *pp,
  1552. bool is_kprobe)
  1553. {
  1554. struct symbol *sym = NULL;
  1555. struct map *map;
  1556. u64 addr;
  1557. int ret = -ENOENT;
  1558. if (!is_kprobe) {
  1559. map = dso__new_map(tp->module);
  1560. if (!map)
  1561. goto out;
  1562. addr = tp->address;
  1563. sym = map__find_symbol(map, addr, NULL);
  1564. } else {
  1565. addr = kernel_get_symbol_address_by_name(tp->symbol, true);
  1566. if (addr) {
  1567. addr += tp->offset;
  1568. sym = __find_kernel_function(addr, &map);
  1569. }
  1570. }
  1571. if (!sym)
  1572. goto out;
  1573. pp->retprobe = tp->retprobe;
  1574. pp->offset = addr - map->unmap_ip(map, sym->start);
  1575. pp->function = strdup(sym->name);
  1576. ret = pp->function ? 0 : -ENOMEM;
  1577. out:
  1578. if (map && !is_kprobe) {
  1579. map__put(map);
  1580. }
  1581. return ret;
  1582. }
  1583. static int convert_to_perf_probe_point(struct probe_trace_point *tp,
  1584. struct perf_probe_point *pp,
  1585. bool is_kprobe)
  1586. {
  1587. char buf[128];
  1588. int ret;
  1589. ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe);
  1590. if (!ret)
  1591. return 0;
  1592. ret = find_perf_probe_point_from_map(tp, pp, is_kprobe);
  1593. if (!ret)
  1594. return 0;
  1595. pr_debug("Failed to find probe point from both of dwarf and map.\n");
  1596. if (tp->symbol) {
  1597. pp->function = strdup(tp->symbol);
  1598. pp->offset = tp->offset;
  1599. } else if (!tp->module && !is_kprobe) {
  1600. ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address);
  1601. if (ret < 0)
  1602. return ret;
  1603. pp->function = strdup(buf);
  1604. pp->offset = 0;
  1605. }
  1606. if (pp->function == NULL)
  1607. return -ENOMEM;
  1608. pp->retprobe = tp->retprobe;
  1609. return 0;
  1610. }
  1611. static int convert_to_perf_probe_event(struct probe_trace_event *tev,
  1612. struct perf_probe_event *pev, bool is_kprobe)
  1613. {
  1614. char buf[64] = "";
  1615. int i, ret;
  1616. /* Convert event/group name */
  1617. pev->event = strdup(tev->event);
  1618. pev->group = strdup(tev->group);
  1619. if (pev->event == NULL || pev->group == NULL)
  1620. return -ENOMEM;
  1621. /* Convert trace_point to probe_point */
  1622. ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
  1623. if (ret < 0)
  1624. return ret;
  1625. /* Convert trace_arg to probe_arg */
  1626. pev->nargs = tev->nargs;
  1627. pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
  1628. if (pev->args == NULL)
  1629. return -ENOMEM;
  1630. for (i = 0; i < tev->nargs && ret >= 0; i++) {
  1631. if (tev->args[i].name)
  1632. pev->args[i].name = strdup(tev->args[i].name);
  1633. else {
  1634. ret = synthesize_probe_trace_arg(&tev->args[i],
  1635. buf, 64);
  1636. pev->args[i].name = strdup(buf);
  1637. }
  1638. if (pev->args[i].name == NULL && ret >= 0)
  1639. ret = -ENOMEM;
  1640. }
  1641. if (ret < 0)
  1642. clear_perf_probe_event(pev);
  1643. return ret;
  1644. }
  1645. void clear_perf_probe_event(struct perf_probe_event *pev)
  1646. {
  1647. struct perf_probe_arg_field *field, *next;
  1648. int i;
  1649. free(pev->event);
  1650. free(pev->group);
  1651. free(pev->target);
  1652. clear_perf_probe_point(&pev->point);
  1653. for (i = 0; i < pev->nargs; i++) {
  1654. free(pev->args[i].name);
  1655. free(pev->args[i].var);
  1656. free(pev->args[i].type);
  1657. field = pev->args[i].field;
  1658. while (field) {
  1659. next = field->next;
  1660. zfree(&field->name);
  1661. free(field);
  1662. field = next;
  1663. }
  1664. }
  1665. free(pev->args);
  1666. memset(pev, 0, sizeof(*pev));
  1667. }
  1668. static void clear_probe_trace_event(struct probe_trace_event *tev)
  1669. {
  1670. struct probe_trace_arg_ref *ref, *next;
  1671. int i;
  1672. free(tev->event);
  1673. free(tev->group);
  1674. free(tev->point.symbol);
  1675. free(tev->point.realname);
  1676. free(tev->point.module);
  1677. for (i = 0; i < tev->nargs; i++) {
  1678. free(tev->args[i].name);
  1679. free(tev->args[i].value);
  1680. free(tev->args[i].type);
  1681. ref = tev->args[i].ref;
  1682. while (ref) {
  1683. next = ref->next;
  1684. free(ref);
  1685. ref = next;
  1686. }
  1687. }
  1688. free(tev->args);
  1689. memset(tev, 0, sizeof(*tev));
  1690. }
  1691. static void print_open_warning(int err, bool is_kprobe)
  1692. {
  1693. char sbuf[STRERR_BUFSIZE];
  1694. if (err == -ENOENT) {
  1695. const char *config;
  1696. if (!is_kprobe)
  1697. config = "CONFIG_UPROBE_EVENTS";
  1698. else
  1699. config = "CONFIG_KPROBE_EVENTS";
  1700. pr_warning("%cprobe_events file does not exist"
  1701. " - please rebuild kernel with %s.\n",
  1702. is_kprobe ? 'k' : 'u', config);
  1703. } else if (err == -ENOTSUP)
  1704. pr_warning("Tracefs or debugfs is not mounted.\n");
  1705. else
  1706. pr_warning("Failed to open %cprobe_events: %s\n",
  1707. is_kprobe ? 'k' : 'u',
  1708. strerror_r(-err, sbuf, sizeof(sbuf)));
  1709. }
  1710. static void print_both_open_warning(int kerr, int uerr)
  1711. {
  1712. /* Both kprobes and uprobes are disabled, warn it. */
  1713. if (kerr == -ENOTSUP && uerr == -ENOTSUP)
  1714. pr_warning("Tracefs or debugfs is not mounted.\n");
  1715. else if (kerr == -ENOENT && uerr == -ENOENT)
  1716. pr_warning("Please rebuild kernel with CONFIG_KPROBE_EVENTS "
  1717. "or/and CONFIG_UPROBE_EVENTS.\n");
  1718. else {
  1719. char sbuf[STRERR_BUFSIZE];
  1720. pr_warning("Failed to open kprobe events: %s.\n",
  1721. strerror_r(-kerr, sbuf, sizeof(sbuf)));
  1722. pr_warning("Failed to open uprobe events: %s.\n",
  1723. strerror_r(-uerr, sbuf, sizeof(sbuf)));
  1724. }
  1725. }
  1726. static int open_probe_events(const char *trace_file, bool readwrite)
  1727. {
  1728. char buf[PATH_MAX];
  1729. const char *__debugfs;
  1730. const char *tracing_dir = "";
  1731. int ret;
  1732. __debugfs = tracefs_find_mountpoint();
  1733. if (__debugfs == NULL) {
  1734. tracing_dir = "tracing/";
  1735. __debugfs = debugfs_find_mountpoint();
  1736. if (__debugfs == NULL)
  1737. return -ENOTSUP;
  1738. }
  1739. ret = e_snprintf(buf, PATH_MAX, "%s/%s%s",
  1740. __debugfs, tracing_dir, trace_file);
  1741. if (ret >= 0) {
  1742. pr_debug("Opening %s write=%d\n", buf, readwrite);
  1743. if (readwrite && !probe_event_dry_run)
  1744. ret = open(buf, O_RDWR | O_APPEND, 0);
  1745. else
  1746. ret = open(buf, O_RDONLY, 0);
  1747. if (ret < 0)
  1748. ret = -errno;
  1749. }
  1750. return ret;
  1751. }
  1752. static int open_kprobe_events(bool readwrite)
  1753. {
  1754. return open_probe_events("kprobe_events", readwrite);
  1755. }
  1756. static int open_uprobe_events(bool readwrite)
  1757. {
  1758. return open_probe_events("uprobe_events", readwrite);
  1759. }
  1760. /* Get raw string list of current kprobe_events or uprobe_events */
  1761. static struct strlist *get_probe_trace_command_rawlist(int fd)
  1762. {
  1763. int ret, idx;
  1764. FILE *fp;
  1765. char buf[MAX_CMDLEN];
  1766. char *p;
  1767. struct strlist *sl;
  1768. sl = strlist__new(true, NULL);
  1769. fp = fdopen(dup(fd), "r");
  1770. while (!feof(fp)) {
  1771. p = fgets(buf, MAX_CMDLEN, fp);
  1772. if (!p)
  1773. break;
  1774. idx = strlen(p) - 1;
  1775. if (p[idx] == '\n')
  1776. p[idx] = '\0';
  1777. ret = strlist__add(sl, buf);
  1778. if (ret < 0) {
  1779. pr_debug("strlist__add failed (%d)\n", ret);
  1780. strlist__delete(sl);
  1781. return NULL;
  1782. }
  1783. }
  1784. fclose(fp);
  1785. return sl;
  1786. }
  1787. struct kprobe_blacklist_node {
  1788. struct list_head list;
  1789. unsigned long start;
  1790. unsigned long end;
  1791. char *symbol;
  1792. };
  1793. static void kprobe_blacklist__delete(struct list_head *blacklist)
  1794. {
  1795. struct kprobe_blacklist_node *node;
  1796. while (!list_empty(blacklist)) {
  1797. node = list_first_entry(blacklist,
  1798. struct kprobe_blacklist_node, list);
  1799. list_del(&node->list);
  1800. free(node->symbol);
  1801. free(node);
  1802. }
  1803. }
  1804. static int kprobe_blacklist__load(struct list_head *blacklist)
  1805. {
  1806. struct kprobe_blacklist_node *node;
  1807. const char *__debugfs = debugfs_find_mountpoint();
  1808. char buf[PATH_MAX], *p;
  1809. FILE *fp;
  1810. int ret;
  1811. if (__debugfs == NULL)
  1812. return -ENOTSUP;
  1813. ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs);
  1814. if (ret < 0)
  1815. return ret;
  1816. fp = fopen(buf, "r");
  1817. if (!fp)
  1818. return -errno;
  1819. ret = 0;
  1820. while (fgets(buf, PATH_MAX, fp)) {
  1821. node = zalloc(sizeof(*node));
  1822. if (!node) {
  1823. ret = -ENOMEM;
  1824. break;
  1825. }
  1826. INIT_LIST_HEAD(&node->list);
  1827. list_add_tail(&node->list, blacklist);
  1828. if (sscanf(buf, "0x%lx-0x%lx", &node->start, &node->end) != 2) {
  1829. ret = -EINVAL;
  1830. break;
  1831. }
  1832. p = strchr(buf, '\t');
  1833. if (p) {
  1834. p++;
  1835. if (p[strlen(p) - 1] == '\n')
  1836. p[strlen(p) - 1] = '\0';
  1837. } else
  1838. p = (char *)"unknown";
  1839. node->symbol = strdup(p);
  1840. if (!node->symbol) {
  1841. ret = -ENOMEM;
  1842. break;
  1843. }
  1844. pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
  1845. node->start, node->end, node->symbol);
  1846. ret++;
  1847. }
  1848. if (ret < 0)
  1849. kprobe_blacklist__delete(blacklist);
  1850. fclose(fp);
  1851. return ret;
  1852. }
  1853. static struct kprobe_blacklist_node *
  1854. kprobe_blacklist__find_by_address(struct list_head *blacklist,
  1855. unsigned long address)
  1856. {
  1857. struct kprobe_blacklist_node *node;
  1858. list_for_each_entry(node, blacklist, list) {
  1859. if (node->start <= address && address <= node->end)
  1860. return node;
  1861. }
  1862. return NULL;
  1863. }
  1864. /* Show an event */
  1865. static int show_perf_probe_event(struct perf_probe_event *pev,
  1866. const char *module)
  1867. {
  1868. int i, ret;
  1869. char buf[128];
  1870. char *place;
  1871. /* Synthesize only event probe point */
  1872. place = synthesize_perf_probe_point(&pev->point);
  1873. if (!place)
  1874. return -EINVAL;
  1875. ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
  1876. if (ret < 0)
  1877. return ret;
  1878. pr_info(" %-20s (on %s", buf, place);
  1879. if (module)
  1880. pr_info(" in %s", module);
  1881. if (pev->nargs > 0) {
  1882. pr_info(" with");
  1883. for (i = 0; i < pev->nargs; i++) {
  1884. ret = synthesize_perf_probe_arg(&pev->args[i],
  1885. buf, 128);
  1886. if (ret < 0)
  1887. break;
  1888. pr_info(" %s", buf);
  1889. }
  1890. }
  1891. pr_info(")\n");
  1892. free(place);
  1893. return ret;
  1894. }
  1895. static bool filter_probe_trace_event(struct probe_trace_event *tev,
  1896. struct strfilter *filter)
  1897. {
  1898. char tmp[128];
  1899. /* At first, check the event name itself */
  1900. if (strfilter__compare(filter, tev->event))
  1901. return true;
  1902. /* Next, check the combination of name and group */
  1903. if (e_snprintf(tmp, 128, "%s:%s", tev->group, tev->event) < 0)
  1904. return false;
  1905. return strfilter__compare(filter, tmp);
  1906. }
  1907. static int __show_perf_probe_events(int fd, bool is_kprobe,
  1908. struct strfilter *filter)
  1909. {
  1910. int ret = 0;
  1911. struct probe_trace_event tev;
  1912. struct perf_probe_event pev;
  1913. struct strlist *rawlist;
  1914. struct str_node *ent;
  1915. memset(&tev, 0, sizeof(tev));
  1916. memset(&pev, 0, sizeof(pev));
  1917. rawlist = get_probe_trace_command_rawlist(fd);
  1918. if (!rawlist)
  1919. return -ENOMEM;
  1920. strlist__for_each(ent, rawlist) {
  1921. ret = parse_probe_trace_command(ent->s, &tev);
  1922. if (ret >= 0) {
  1923. if (!filter_probe_trace_event(&tev, filter))
  1924. goto next;
  1925. ret = convert_to_perf_probe_event(&tev, &pev,
  1926. is_kprobe);
  1927. if (ret >= 0)
  1928. ret = show_perf_probe_event(&pev,
  1929. tev.point.module);
  1930. }
  1931. next:
  1932. clear_perf_probe_event(&pev);
  1933. clear_probe_trace_event(&tev);
  1934. if (ret < 0)
  1935. break;
  1936. }
  1937. strlist__delete(rawlist);
  1938. return ret;
  1939. }
  1940. /* List up current perf-probe events */
  1941. int show_perf_probe_events(struct strfilter *filter)
  1942. {
  1943. int kp_fd, up_fd, ret;
  1944. setup_pager();
  1945. ret = init_symbol_maps(false);
  1946. if (ret < 0)
  1947. return ret;
  1948. kp_fd = open_kprobe_events(false);
  1949. if (kp_fd >= 0) {
  1950. ret = __show_perf_probe_events(kp_fd, true, filter);
  1951. close(kp_fd);
  1952. if (ret < 0)
  1953. goto out;
  1954. }
  1955. up_fd = open_uprobe_events(false);
  1956. if (kp_fd < 0 && up_fd < 0) {
  1957. print_both_open_warning(kp_fd, up_fd);
  1958. ret = kp_fd;
  1959. goto out;
  1960. }
  1961. if (up_fd >= 0) {
  1962. ret = __show_perf_probe_events(up_fd, false, filter);
  1963. close(up_fd);
  1964. }
  1965. out:
  1966. exit_symbol_maps();
  1967. return ret;
  1968. }
  1969. /* Get current perf-probe event names */
  1970. static struct strlist *get_probe_trace_event_names(int fd, bool include_group)
  1971. {
  1972. char buf[128];
  1973. struct strlist *sl, *rawlist;
  1974. struct str_node *ent;
  1975. struct probe_trace_event tev;
  1976. int ret = 0;
  1977. memset(&tev, 0, sizeof(tev));
  1978. rawlist = get_probe_trace_command_rawlist(fd);
  1979. if (!rawlist)
  1980. return NULL;
  1981. sl = strlist__new(true, NULL);
  1982. strlist__for_each(ent, rawlist) {
  1983. ret = parse_probe_trace_command(ent->s, &tev);
  1984. if (ret < 0)
  1985. break;
  1986. if (include_group) {
  1987. ret = e_snprintf(buf, 128, "%s:%s", tev.group,
  1988. tev.event);
  1989. if (ret >= 0)
  1990. ret = strlist__add(sl, buf);
  1991. } else
  1992. ret = strlist__add(sl, tev.event);
  1993. clear_probe_trace_event(&tev);
  1994. if (ret < 0)
  1995. break;
  1996. }
  1997. strlist__delete(rawlist);
  1998. if (ret < 0) {
  1999. strlist__delete(sl);
  2000. return NULL;
  2001. }
  2002. return sl;
  2003. }
  2004. static int write_probe_trace_event(int fd, struct probe_trace_event *tev)
  2005. {
  2006. int ret = 0;
  2007. char *buf = synthesize_probe_trace_command(tev);
  2008. char sbuf[STRERR_BUFSIZE];
  2009. if (!buf) {
  2010. pr_debug("Failed to synthesize probe trace event.\n");
  2011. return -EINVAL;
  2012. }
  2013. pr_debug("Writing event: %s\n", buf);
  2014. if (!probe_event_dry_run) {
  2015. ret = write(fd, buf, strlen(buf));
  2016. if (ret <= 0) {
  2017. ret = -errno;
  2018. pr_warning("Failed to write event: %s\n",
  2019. strerror_r(errno, sbuf, sizeof(sbuf)));
  2020. }
  2021. }
  2022. free(buf);
  2023. return ret;
  2024. }
  2025. static int get_new_event_name(char *buf, size_t len, const char *base,
  2026. struct strlist *namelist, bool allow_suffix)
  2027. {
  2028. int i, ret;
  2029. if (*base == '.')
  2030. base++;
  2031. /* Try no suffix */
  2032. ret = e_snprintf(buf, len, "%s", base);
  2033. if (ret < 0) {
  2034. pr_debug("snprintf() failed: %d\n", ret);
  2035. return ret;
  2036. }
  2037. if (!strlist__has_entry(namelist, buf))
  2038. return 0;
  2039. if (!allow_suffix) {
  2040. pr_warning("Error: event \"%s\" already exists. "
  2041. "(Use -f to force duplicates.)\n", base);
  2042. return -EEXIST;
  2043. }
  2044. /* Try to add suffix */
  2045. for (i = 1; i < MAX_EVENT_INDEX; i++) {
  2046. ret = e_snprintf(buf, len, "%s_%d", base, i);
  2047. if (ret < 0) {
  2048. pr_debug("snprintf() failed: %d\n", ret);
  2049. return ret;
  2050. }
  2051. if (!strlist__has_entry(namelist, buf))
  2052. break;
  2053. }
  2054. if (i == MAX_EVENT_INDEX) {
  2055. pr_warning("Too many events are on the same function.\n");
  2056. ret = -ERANGE;
  2057. }
  2058. return ret;
  2059. }
  2060. /* Warn if the current kernel's uprobe implementation is old */
  2061. static void warn_uprobe_event_compat(struct probe_trace_event *tev)
  2062. {
  2063. int i;
  2064. char *buf = synthesize_probe_trace_command(tev);
  2065. /* Old uprobe event doesn't support memory dereference */
  2066. if (!tev->uprobes || tev->nargs == 0 || !buf)
  2067. goto out;
  2068. for (i = 0; i < tev->nargs; i++)
  2069. if (strglobmatch(tev->args[i].value, "[$@+-]*")) {
  2070. pr_warning("Please upgrade your kernel to at least "
  2071. "3.14 to have access to feature %s\n",
  2072. tev->args[i].value);
  2073. break;
  2074. }
  2075. out:
  2076. free(buf);
  2077. }
  2078. static int __add_probe_trace_events(struct perf_probe_event *pev,
  2079. struct probe_trace_event *tevs,
  2080. int ntevs, bool allow_suffix)
  2081. {
  2082. int i, fd, ret;
  2083. struct probe_trace_event *tev = NULL;
  2084. char buf[64];
  2085. const char *event, *group;
  2086. struct strlist *namelist;
  2087. LIST_HEAD(blacklist);
  2088. struct kprobe_blacklist_node *node;
  2089. bool safename;
  2090. if (pev->uprobes)
  2091. fd = open_uprobe_events(true);
  2092. else
  2093. fd = open_kprobe_events(true);
  2094. if (fd < 0) {
  2095. print_open_warning(fd, !pev->uprobes);
  2096. return fd;
  2097. }
  2098. /* Get current event names */
  2099. namelist = get_probe_trace_event_names(fd, false);
  2100. if (!namelist) {
  2101. pr_debug("Failed to get current event list.\n");
  2102. ret = -ENOMEM;
  2103. goto close_out;
  2104. }
  2105. /* Get kprobe blacklist if exists */
  2106. if (!pev->uprobes) {
  2107. ret = kprobe_blacklist__load(&blacklist);
  2108. if (ret < 0)
  2109. pr_debug("No kprobe blacklist support, ignored\n");
  2110. }
  2111. safename = (pev->point.function && !strisglob(pev->point.function));
  2112. ret = 0;
  2113. pr_info("Added new event%s\n", (ntevs > 1) ? "s:" : ":");
  2114. for (i = 0; i < ntevs; i++) {
  2115. tev = &tevs[i];
  2116. /* Skip if the symbol is out of .text (marked previously) */
  2117. if (!tev->point.symbol)
  2118. continue;
  2119. /* Ensure that the address is NOT blacklisted */
  2120. node = kprobe_blacklist__find_by_address(&blacklist,
  2121. tev->point.address);
  2122. if (node) {
  2123. pr_warning("Warning: Skipped probing on blacklisted function: %s\n", node->symbol);
  2124. continue;
  2125. }
  2126. if (pev->event)
  2127. event = pev->event;
  2128. else
  2129. if (safename)
  2130. event = pev->point.function;
  2131. else
  2132. event = tev->point.realname;
  2133. if (pev->group)
  2134. group = pev->group;
  2135. else
  2136. group = PERFPROBE_GROUP;
  2137. /* Get an unused new event name */
  2138. ret = get_new_event_name(buf, 64, event,
  2139. namelist, allow_suffix);
  2140. if (ret < 0)
  2141. break;
  2142. event = buf;
  2143. tev->event = strdup(event);
  2144. tev->group = strdup(group);
  2145. if (tev->event == NULL || tev->group == NULL) {
  2146. ret = -ENOMEM;
  2147. break;
  2148. }
  2149. ret = write_probe_trace_event(fd, tev);
  2150. if (ret < 0)
  2151. break;
  2152. /* Add added event name to namelist */
  2153. strlist__add(namelist, event);
  2154. /* Trick here - save current event/group */
  2155. event = pev->event;
  2156. group = pev->group;
  2157. pev->event = tev->event;
  2158. pev->group = tev->group;
  2159. show_perf_probe_event(pev, tev->point.module);
  2160. /* Trick here - restore current event/group */
  2161. pev->event = (char *)event;
  2162. pev->group = (char *)group;
  2163. /*
  2164. * Probes after the first probe which comes from same
  2165. * user input are always allowed to add suffix, because
  2166. * there might be several addresses corresponding to
  2167. * one code line.
  2168. */
  2169. allow_suffix = true;
  2170. }
  2171. if (ret == -EINVAL && pev->uprobes)
  2172. warn_uprobe_event_compat(tev);
  2173. /* Note that it is possible to skip all events because of blacklist */
  2174. if (ret >= 0 && tev->event) {
  2175. /* Show how to use the event. */
  2176. pr_info("\nYou can now use it in all perf tools, such as:\n\n");
  2177. pr_info("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group,
  2178. tev->event);
  2179. }
  2180. kprobe_blacklist__delete(&blacklist);
  2181. strlist__delete(namelist);
  2182. close_out:
  2183. close(fd);
  2184. return ret;
  2185. }
  2186. static int find_probe_functions(struct map *map, char *name,
  2187. struct symbol **syms)
  2188. {
  2189. int found = 0;
  2190. struct symbol *sym;
  2191. struct rb_node *tmp;
  2192. if (map__load(map, NULL) < 0)
  2193. return 0;
  2194. map__for_each_symbol(map, sym, tmp) {
  2195. if (strglobmatch(sym->name, name)) {
  2196. found++;
  2197. if (syms && found < probe_conf.max_probes)
  2198. syms[found - 1] = sym;
  2199. }
  2200. }
  2201. return found;
  2202. }
  2203. #define strdup_or_goto(str, label) \
  2204. ({ char *__p = strdup(str); if (!__p) goto label; __p; })
  2205. void __weak arch__fix_tev_from_maps(struct perf_probe_event *pev __maybe_unused,
  2206. struct probe_trace_event *tev __maybe_unused,
  2207. struct map *map __maybe_unused) { }
  2208. /*
  2209. * Find probe function addresses from map.
  2210. * Return an error or the number of found probe_trace_event
  2211. */
  2212. static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
  2213. struct probe_trace_event **tevs)
  2214. {
  2215. struct map *map = NULL;
  2216. struct ref_reloc_sym *reloc_sym = NULL;
  2217. struct symbol *sym;
  2218. struct symbol **syms = NULL;
  2219. struct probe_trace_event *tev;
  2220. struct perf_probe_point *pp = &pev->point;
  2221. struct probe_trace_point *tp;
  2222. int num_matched_functions;
  2223. int ret, i, j;
  2224. map = get_target_map(pev->target, pev->uprobes);
  2225. if (!map) {
  2226. ret = -EINVAL;
  2227. goto out;
  2228. }
  2229. syms = malloc(sizeof(struct symbol *) * probe_conf.max_probes);
  2230. if (!syms) {
  2231. ret = -ENOMEM;
  2232. goto out;
  2233. }
  2234. /*
  2235. * Load matched symbols: Since the different local symbols may have
  2236. * same name but different addresses, this lists all the symbols.
  2237. */
  2238. num_matched_functions = find_probe_functions(map, pp->function, syms);
  2239. if (num_matched_functions == 0) {
  2240. pr_err("Failed to find symbol %s in %s\n", pp->function,
  2241. pev->target ? : "kernel");
  2242. ret = -ENOENT;
  2243. goto out;
  2244. } else if (num_matched_functions > probe_conf.max_probes) {
  2245. pr_err("Too many functions matched in %s\n",
  2246. pev->target ? : "kernel");
  2247. ret = -E2BIG;
  2248. goto out;
  2249. }
  2250. if (!pev->uprobes && !pp->retprobe) {
  2251. reloc_sym = kernel_get_ref_reloc_sym();
  2252. if (!reloc_sym) {
  2253. pr_warning("Relocated base symbol is not found!\n");
  2254. ret = -EINVAL;
  2255. goto out;
  2256. }
  2257. }
  2258. /* Setup result trace-probe-events */
  2259. *tevs = zalloc(sizeof(*tev) * num_matched_functions);
  2260. if (!*tevs) {
  2261. ret = -ENOMEM;
  2262. goto out;
  2263. }
  2264. ret = 0;
  2265. for (j = 0; j < num_matched_functions; j++) {
  2266. sym = syms[j];
  2267. tev = (*tevs) + ret;
  2268. tp = &tev->point;
  2269. if (ret == num_matched_functions) {
  2270. pr_warning("Too many symbols are listed. Skip it.\n");
  2271. break;
  2272. }
  2273. ret++;
  2274. if (pp->offset > sym->end - sym->start) {
  2275. pr_warning("Offset %ld is bigger than the size of %s\n",
  2276. pp->offset, sym->name);
  2277. ret = -ENOENT;
  2278. goto err_out;
  2279. }
  2280. /* Add one probe point */
  2281. tp->address = map->unmap_ip(map, sym->start) + pp->offset;
  2282. if (reloc_sym) {
  2283. tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
  2284. tp->offset = tp->address - reloc_sym->addr;
  2285. } else {
  2286. tp->symbol = strdup_or_goto(sym->name, nomem_out);
  2287. tp->offset = pp->offset;
  2288. }
  2289. tp->realname = strdup_or_goto(sym->name, nomem_out);
  2290. tp->retprobe = pp->retprobe;
  2291. if (pev->target)
  2292. tev->point.module = strdup_or_goto(pev->target,
  2293. nomem_out);
  2294. tev->uprobes = pev->uprobes;
  2295. tev->nargs = pev->nargs;
  2296. if (tev->nargs) {
  2297. tev->args = zalloc(sizeof(struct probe_trace_arg) *
  2298. tev->nargs);
  2299. if (tev->args == NULL)
  2300. goto nomem_out;
  2301. }
  2302. for (i = 0; i < tev->nargs; i++) {
  2303. if (pev->args[i].name)
  2304. tev->args[i].name =
  2305. strdup_or_goto(pev->args[i].name,
  2306. nomem_out);
  2307. tev->args[i].value = strdup_or_goto(pev->args[i].var,
  2308. nomem_out);
  2309. if (pev->args[i].type)
  2310. tev->args[i].type =
  2311. strdup_or_goto(pev->args[i].type,
  2312. nomem_out);
  2313. }
  2314. arch__fix_tev_from_maps(pev, tev, map);
  2315. }
  2316. out:
  2317. put_target_map(map, pev->uprobes);
  2318. free(syms);
  2319. return ret;
  2320. nomem_out:
  2321. ret = -ENOMEM;
  2322. err_out:
  2323. clear_probe_trace_events(*tevs, num_matched_functions);
  2324. zfree(tevs);
  2325. goto out;
  2326. }
  2327. bool __weak arch__prefers_symtab(void) { return false; }
  2328. static int convert_to_probe_trace_events(struct perf_probe_event *pev,
  2329. struct probe_trace_event **tevs)
  2330. {
  2331. int ret;
  2332. if (pev->uprobes && !pev->group) {
  2333. /* Replace group name if not given */
  2334. ret = convert_exec_to_group(pev->target, &pev->group);
  2335. if (ret != 0) {
  2336. pr_warning("Failed to make a group name.\n");
  2337. return ret;
  2338. }
  2339. }
  2340. if (arch__prefers_symtab() && !perf_probe_event_need_dwarf(pev)) {
  2341. ret = find_probe_trace_events_from_map(pev, tevs);
  2342. if (ret > 0)
  2343. return ret; /* Found in symbol table */
  2344. }
  2345. /* Convert perf_probe_event with debuginfo */
  2346. ret = try_to_find_probe_trace_events(pev, tevs);
  2347. if (ret != 0)
  2348. return ret; /* Found in debuginfo or got an error */
  2349. return find_probe_trace_events_from_map(pev, tevs);
  2350. }
  2351. struct __event_package {
  2352. struct perf_probe_event *pev;
  2353. struct probe_trace_event *tevs;
  2354. int ntevs;
  2355. };
  2356. int add_perf_probe_events(struct perf_probe_event *pevs, int npevs)
  2357. {
  2358. int i, j, ret;
  2359. struct __event_package *pkgs;
  2360. ret = 0;
  2361. pkgs = zalloc(sizeof(struct __event_package) * npevs);
  2362. if (pkgs == NULL)
  2363. return -ENOMEM;
  2364. ret = init_symbol_maps(pevs->uprobes);
  2365. if (ret < 0) {
  2366. free(pkgs);
  2367. return ret;
  2368. }
  2369. /* Loop 1: convert all events */
  2370. for (i = 0; i < npevs; i++) {
  2371. pkgs[i].pev = &pevs[i];
  2372. /* Convert with or without debuginfo */
  2373. ret = convert_to_probe_trace_events(pkgs[i].pev,
  2374. &pkgs[i].tevs);
  2375. if (ret < 0)
  2376. goto end;
  2377. pkgs[i].ntevs = ret;
  2378. }
  2379. /* Loop 2: add all events */
  2380. for (i = 0; i < npevs; i++) {
  2381. ret = __add_probe_trace_events(pkgs[i].pev, pkgs[i].tevs,
  2382. pkgs[i].ntevs,
  2383. probe_conf.force_add);
  2384. if (ret < 0)
  2385. break;
  2386. }
  2387. end:
  2388. /* Loop 3: cleanup and free trace events */
  2389. for (i = 0; i < npevs; i++) {
  2390. for (j = 0; j < pkgs[i].ntevs; j++)
  2391. clear_probe_trace_event(&pkgs[i].tevs[j]);
  2392. zfree(&pkgs[i].tevs);
  2393. }
  2394. free(pkgs);
  2395. exit_symbol_maps();
  2396. return ret;
  2397. }
  2398. static int __del_trace_probe_event(int fd, struct str_node *ent)
  2399. {
  2400. char *p;
  2401. char buf[128];
  2402. int ret;
  2403. /* Convert from perf-probe event to trace-probe event */
  2404. ret = e_snprintf(buf, 128, "-:%s", ent->s);
  2405. if (ret < 0)
  2406. goto error;
  2407. p = strchr(buf + 2, ':');
  2408. if (!p) {
  2409. pr_debug("Internal error: %s should have ':' but not.\n",
  2410. ent->s);
  2411. ret = -ENOTSUP;
  2412. goto error;
  2413. }
  2414. *p = '/';
  2415. pr_debug("Writing event: %s\n", buf);
  2416. ret = write(fd, buf, strlen(buf));
  2417. if (ret < 0) {
  2418. ret = -errno;
  2419. goto error;
  2420. }
  2421. pr_info("Removed event: %s\n", ent->s);
  2422. return 0;
  2423. error:
  2424. pr_warning("Failed to delete event: %s\n",
  2425. strerror_r(-ret, buf, sizeof(buf)));
  2426. return ret;
  2427. }
  2428. static int del_trace_probe_events(int fd, struct strfilter *filter,
  2429. struct strlist *namelist)
  2430. {
  2431. struct str_node *ent;
  2432. const char *p;
  2433. int ret = -ENOENT;
  2434. if (!namelist)
  2435. return -ENOENT;
  2436. strlist__for_each(ent, namelist) {
  2437. p = strchr(ent->s, ':');
  2438. if ((p && strfilter__compare(filter, p + 1)) ||
  2439. strfilter__compare(filter, ent->s)) {
  2440. ret = __del_trace_probe_event(fd, ent);
  2441. if (ret < 0)
  2442. break;
  2443. }
  2444. }
  2445. return ret;
  2446. }
  2447. int del_perf_probe_events(struct strfilter *filter)
  2448. {
  2449. int ret, ret2, ufd = -1, kfd = -1;
  2450. struct strlist *namelist = NULL, *unamelist = NULL;
  2451. char *str = strfilter__string(filter);
  2452. if (!str)
  2453. return -EINVAL;
  2454. pr_debug("Delete filter: \'%s\'\n", str);
  2455. /* Get current event names */
  2456. kfd = open_kprobe_events(true);
  2457. if (kfd >= 0)
  2458. namelist = get_probe_trace_event_names(kfd, true);
  2459. ufd = open_uprobe_events(true);
  2460. if (ufd >= 0)
  2461. unamelist = get_probe_trace_event_names(ufd, true);
  2462. if (kfd < 0 && ufd < 0) {
  2463. print_both_open_warning(kfd, ufd);
  2464. ret = kfd;
  2465. goto error;
  2466. }
  2467. ret = del_trace_probe_events(kfd, filter, namelist);
  2468. if (ret < 0 && ret != -ENOENT)
  2469. goto error;
  2470. ret2 = del_trace_probe_events(ufd, filter, unamelist);
  2471. if (ret2 < 0 && ret2 != -ENOENT) {
  2472. ret = ret2;
  2473. goto error;
  2474. }
  2475. if (ret == -ENOENT && ret2 == -ENOENT)
  2476. pr_debug("\"%s\" does not hit any event.\n", str);
  2477. /* Note that this is silently ignored */
  2478. ret = 0;
  2479. error:
  2480. if (kfd >= 0) {
  2481. strlist__delete(namelist);
  2482. close(kfd);
  2483. }
  2484. if (ufd >= 0) {
  2485. strlist__delete(unamelist);
  2486. close(ufd);
  2487. }
  2488. free(str);
  2489. return ret;
  2490. }
  2491. /* TODO: don't use a global variable for filter ... */
  2492. static struct strfilter *available_func_filter;
  2493. /*
  2494. * If a symbol corresponds to a function with global binding and
  2495. * matches filter return 0. For all others return 1.
  2496. */
  2497. static int filter_available_functions(struct map *map __maybe_unused,
  2498. struct symbol *sym)
  2499. {
  2500. if (strfilter__compare(available_func_filter, sym->name))
  2501. return 0;
  2502. return 1;
  2503. }
  2504. int show_available_funcs(const char *target, struct strfilter *_filter,
  2505. bool user)
  2506. {
  2507. struct map *map;
  2508. int ret;
  2509. ret = init_symbol_maps(user);
  2510. if (ret < 0)
  2511. return ret;
  2512. /* Get a symbol map */
  2513. if (user)
  2514. map = dso__new_map(target);
  2515. else
  2516. map = kernel_get_module_map(target);
  2517. if (!map) {
  2518. pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
  2519. return -EINVAL;
  2520. }
  2521. /* Load symbols with given filter */
  2522. available_func_filter = _filter;
  2523. if (map__load(map, filter_available_functions)) {
  2524. pr_err("Failed to load symbols in %s\n", (target) ? : "kernel");
  2525. goto end;
  2526. }
  2527. if (!dso__sorted_by_name(map->dso, map->type))
  2528. dso__sort_by_name(map->dso, map->type);
  2529. /* Show all (filtered) symbols */
  2530. setup_pager();
  2531. dso__fprintf_symbols_by_name(map->dso, map->type, stdout);
  2532. end:
  2533. if (user) {
  2534. map__put(map);
  2535. }
  2536. exit_symbol_maps();
  2537. return ret;
  2538. }