probe-event.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374
  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 "debugfs.h"
  42. #include "trace-event.h" /* For __maybe_unused */
  43. #include "probe-event.h"
  44. #include "probe-finder.h"
  45. #include "session.h"
  46. #define MAX_CMDLEN 256
  47. #define MAX_PROBE_ARGS 128
  48. #define PERFPROBE_GROUP "probe"
  49. bool probe_event_dry_run; /* Dry run flag */
  50. #define semantic_error(msg ...) pr_err("Semantic error :" msg)
  51. /* If there is no space to write, returns -E2BIG. */
  52. static int e_snprintf(char *str, size_t size, const char *format, ...)
  53. __attribute__((format(printf, 3, 4)));
  54. static int e_snprintf(char *str, size_t size, const char *format, ...)
  55. {
  56. int ret;
  57. va_list ap;
  58. va_start(ap, format);
  59. ret = vsnprintf(str, size, format, ap);
  60. va_end(ap);
  61. if (ret >= (int)size)
  62. ret = -E2BIG;
  63. return ret;
  64. }
  65. static char *synthesize_perf_probe_point(struct perf_probe_point *pp);
  66. static int convert_name_to_addr(struct perf_probe_event *pev,
  67. const char *exec);
  68. static struct machine machine;
  69. /* Initialize symbol maps and path of vmlinux/modules */
  70. static int init_vmlinux(void)
  71. {
  72. int ret;
  73. symbol_conf.sort_by_name = true;
  74. if (symbol_conf.vmlinux_name == NULL)
  75. symbol_conf.try_vmlinux_path = true;
  76. else
  77. pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
  78. ret = symbol__init();
  79. if (ret < 0) {
  80. pr_debug("Failed to init symbol map.\n");
  81. goto out;
  82. }
  83. ret = machine__init(&machine, "", HOST_KERNEL_ID);
  84. if (ret < 0)
  85. goto out;
  86. if (machine__create_kernel_maps(&machine) < 0) {
  87. pr_debug("machine__create_kernel_maps() failed.\n");
  88. goto out;
  89. }
  90. out:
  91. if (ret < 0)
  92. pr_warning("Failed to init vmlinux path.\n");
  93. return ret;
  94. }
  95. static struct symbol *__find_kernel_function_by_name(const char *name,
  96. struct map **mapp)
  97. {
  98. return machine__find_kernel_function_by_name(&machine, name, mapp,
  99. NULL);
  100. }
  101. static struct map *kernel_get_module_map(const char *module)
  102. {
  103. struct rb_node *nd;
  104. struct map_groups *grp = &machine.kmaps;
  105. /* A file path -- this is an offline module */
  106. if (module && strchr(module, '/'))
  107. return machine__new_module(&machine, 0, module);
  108. if (!module)
  109. module = "kernel";
  110. for (nd = rb_first(&grp->maps[MAP__FUNCTION]); nd; nd = rb_next(nd)) {
  111. struct map *pos = rb_entry(nd, struct map, rb_node);
  112. if (strncmp(pos->dso->short_name + 1, module,
  113. pos->dso->short_name_len - 2) == 0) {
  114. return pos;
  115. }
  116. }
  117. return NULL;
  118. }
  119. static struct dso *kernel_get_module_dso(const char *module)
  120. {
  121. struct dso *dso;
  122. struct map *map;
  123. const char *vmlinux_name;
  124. if (module) {
  125. list_for_each_entry(dso, &machine.kernel_dsos, node) {
  126. if (strncmp(dso->short_name + 1, module,
  127. dso->short_name_len - 2) == 0)
  128. goto found;
  129. }
  130. pr_debug("Failed to find module %s.\n", module);
  131. return NULL;
  132. }
  133. map = machine.vmlinux_maps[MAP__FUNCTION];
  134. dso = map->dso;
  135. vmlinux_name = symbol_conf.vmlinux_name;
  136. if (vmlinux_name) {
  137. if (dso__load_vmlinux(dso, map, vmlinux_name, NULL) <= 0)
  138. return NULL;
  139. } else {
  140. if (dso__load_vmlinux_path(dso, map, NULL) <= 0) {
  141. pr_debug("Failed to load kernel map.\n");
  142. return NULL;
  143. }
  144. }
  145. found:
  146. return dso;
  147. }
  148. const char *kernel_get_module_path(const char *module)
  149. {
  150. struct dso *dso = kernel_get_module_dso(module);
  151. return (dso) ? dso->long_name : NULL;
  152. }
  153. static int init_user_exec(void)
  154. {
  155. int ret = 0;
  156. symbol_conf.try_vmlinux_path = false;
  157. symbol_conf.sort_by_name = true;
  158. ret = symbol__init();
  159. if (ret < 0)
  160. pr_debug("Failed to init symbol map.\n");
  161. return ret;
  162. }
  163. static int convert_to_perf_probe_point(struct probe_trace_point *tp,
  164. struct perf_probe_point *pp)
  165. {
  166. pp->function = strdup(tp->symbol);
  167. if (pp->function == NULL)
  168. return -ENOMEM;
  169. pp->offset = tp->offset;
  170. pp->retprobe = tp->retprobe;
  171. return 0;
  172. }
  173. #ifdef DWARF_SUPPORT
  174. /* Open new debuginfo of given module */
  175. static struct debuginfo *open_debuginfo(const char *module)
  176. {
  177. const char *path;
  178. /* A file path -- this is an offline module */
  179. if (module && strchr(module, '/'))
  180. path = module;
  181. else {
  182. path = kernel_get_module_path(module);
  183. if (!path) {
  184. pr_err("Failed to find path of %s module.\n",
  185. module ?: "kernel");
  186. return NULL;
  187. }
  188. }
  189. return debuginfo__new(path);
  190. }
  191. /*
  192. * Convert trace point to probe point with debuginfo
  193. * Currently only handles kprobes.
  194. */
  195. static int kprobe_convert_to_perf_probe(struct probe_trace_point *tp,
  196. struct perf_probe_point *pp)
  197. {
  198. struct symbol *sym;
  199. struct map *map;
  200. u64 addr;
  201. int ret = -ENOENT;
  202. struct debuginfo *dinfo;
  203. sym = __find_kernel_function_by_name(tp->symbol, &map);
  204. if (sym) {
  205. addr = map->unmap_ip(map, sym->start + tp->offset);
  206. pr_debug("try to find %s+%ld@%" PRIx64 "\n", tp->symbol,
  207. tp->offset, addr);
  208. dinfo = debuginfo__new_online_kernel(addr);
  209. if (dinfo) {
  210. ret = debuginfo__find_probe_point(dinfo,
  211. (unsigned long)addr, pp);
  212. debuginfo__delete(dinfo);
  213. } else {
  214. pr_debug("Failed to open debuginfo at 0x%" PRIx64 "\n",
  215. addr);
  216. ret = -ENOENT;
  217. }
  218. }
  219. if (ret <= 0) {
  220. pr_debug("Failed to find corresponding probes from "
  221. "debuginfo. Use kprobe event information.\n");
  222. return convert_to_perf_probe_point(tp, pp);
  223. }
  224. pp->retprobe = tp->retprobe;
  225. return 0;
  226. }
  227. static int add_module_to_probe_trace_events(struct probe_trace_event *tevs,
  228. int ntevs, const char *module)
  229. {
  230. int i, ret = 0;
  231. char *tmp;
  232. if (!module)
  233. return 0;
  234. tmp = strrchr(module, '/');
  235. if (tmp) {
  236. /* This is a module path -- get the module name */
  237. module = strdup(tmp + 1);
  238. if (!module)
  239. return -ENOMEM;
  240. tmp = strchr(module, '.');
  241. if (tmp)
  242. *tmp = '\0';
  243. tmp = (char *)module; /* For free() */
  244. }
  245. for (i = 0; i < ntevs; i++) {
  246. tevs[i].point.module = strdup(module);
  247. if (!tevs[i].point.module) {
  248. ret = -ENOMEM;
  249. break;
  250. }
  251. }
  252. if (tmp)
  253. free(tmp);
  254. return ret;
  255. }
  256. /* Try to find perf_probe_event with debuginfo */
  257. static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
  258. struct probe_trace_event **tevs,
  259. int max_tevs, const char *target)
  260. {
  261. bool need_dwarf = perf_probe_event_need_dwarf(pev);
  262. struct debuginfo *dinfo;
  263. int ntevs, ret = 0;
  264. if (pev->uprobes) {
  265. if (need_dwarf) {
  266. pr_warning("Debuginfo-analysis is not yet supported"
  267. " with -x/--exec option.\n");
  268. return -ENOSYS;
  269. }
  270. return convert_name_to_addr(pev, target);
  271. }
  272. dinfo = open_debuginfo(target);
  273. if (!dinfo) {
  274. if (need_dwarf) {
  275. pr_warning("Failed to open debuginfo file.\n");
  276. return -ENOENT;
  277. }
  278. pr_debug("Could not open debuginfo. Try to use symbols.\n");
  279. return 0;
  280. }
  281. /* Searching trace events corresponding to a probe event */
  282. ntevs = debuginfo__find_trace_events(dinfo, pev, tevs, max_tevs);
  283. debuginfo__delete(dinfo);
  284. if (ntevs > 0) { /* Succeeded to find trace events */
  285. pr_debug("find %d probe_trace_events.\n", ntevs);
  286. if (target)
  287. ret = add_module_to_probe_trace_events(*tevs, ntevs,
  288. target);
  289. return ret < 0 ? ret : ntevs;
  290. }
  291. if (ntevs == 0) { /* No error but failed to find probe point. */
  292. pr_warning("Probe point '%s' not found.\n",
  293. synthesize_perf_probe_point(&pev->point));
  294. return -ENOENT;
  295. }
  296. /* Error path : ntevs < 0 */
  297. pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
  298. if (ntevs == -EBADF) {
  299. pr_warning("Warning: No dwarf info found in the vmlinux - "
  300. "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
  301. if (!need_dwarf) {
  302. pr_debug("Trying to use symbols.\n");
  303. return 0;
  304. }
  305. }
  306. return ntevs;
  307. }
  308. /*
  309. * Find a src file from a DWARF tag path. Prepend optional source path prefix
  310. * and chop off leading directories that do not exist. Result is passed back as
  311. * a newly allocated path on success.
  312. * Return 0 if file was found and readable, -errno otherwise.
  313. */
  314. static int get_real_path(const char *raw_path, const char *comp_dir,
  315. char **new_path)
  316. {
  317. const char *prefix = symbol_conf.source_prefix;
  318. if (!prefix) {
  319. if (raw_path[0] != '/' && comp_dir)
  320. /* If not an absolute path, try to use comp_dir */
  321. prefix = comp_dir;
  322. else {
  323. if (access(raw_path, R_OK) == 0) {
  324. *new_path = strdup(raw_path);
  325. return 0;
  326. } else
  327. return -errno;
  328. }
  329. }
  330. *new_path = malloc((strlen(prefix) + strlen(raw_path) + 2));
  331. if (!*new_path)
  332. return -ENOMEM;
  333. for (;;) {
  334. sprintf(*new_path, "%s/%s", prefix, raw_path);
  335. if (access(*new_path, R_OK) == 0)
  336. return 0;
  337. if (!symbol_conf.source_prefix)
  338. /* In case of searching comp_dir, don't retry */
  339. return -errno;
  340. switch (errno) {
  341. case ENAMETOOLONG:
  342. case ENOENT:
  343. case EROFS:
  344. case EFAULT:
  345. raw_path = strchr(++raw_path, '/');
  346. if (!raw_path) {
  347. free(*new_path);
  348. *new_path = NULL;
  349. return -ENOENT;
  350. }
  351. continue;
  352. default:
  353. free(*new_path);
  354. *new_path = NULL;
  355. return -errno;
  356. }
  357. }
  358. }
  359. #define LINEBUF_SIZE 256
  360. #define NR_ADDITIONAL_LINES 2
  361. static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
  362. {
  363. char buf[LINEBUF_SIZE];
  364. const char *color = show_num ? "" : PERF_COLOR_BLUE;
  365. const char *prefix = NULL;
  366. do {
  367. if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
  368. goto error;
  369. if (skip)
  370. continue;
  371. if (!prefix) {
  372. prefix = show_num ? "%7d " : " ";
  373. color_fprintf(stdout, color, prefix, l);
  374. }
  375. color_fprintf(stdout, color, "%s", buf);
  376. } while (strchr(buf, '\n') == NULL);
  377. return 1;
  378. error:
  379. if (ferror(fp)) {
  380. pr_warning("File read error: %s\n", strerror(errno));
  381. return -1;
  382. }
  383. return 0;
  384. }
  385. static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
  386. {
  387. int rv = __show_one_line(fp, l, skip, show_num);
  388. if (rv == 0) {
  389. pr_warning("Source file is shorter than expected.\n");
  390. rv = -1;
  391. }
  392. return rv;
  393. }
  394. #define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
  395. #define show_one_line(f,l) _show_one_line(f,l,false,false)
  396. #define skip_one_line(f,l) _show_one_line(f,l,true,false)
  397. #define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
  398. /*
  399. * Show line-range always requires debuginfo to find source file and
  400. * line number.
  401. */
  402. int show_line_range(struct line_range *lr, const char *module)
  403. {
  404. int l = 1;
  405. struct line_node *ln;
  406. struct debuginfo *dinfo;
  407. FILE *fp;
  408. int ret;
  409. char *tmp;
  410. /* Search a line range */
  411. ret = init_vmlinux();
  412. if (ret < 0)
  413. return ret;
  414. dinfo = open_debuginfo(module);
  415. if (!dinfo) {
  416. pr_warning("Failed to open debuginfo file.\n");
  417. return -ENOENT;
  418. }
  419. ret = debuginfo__find_line_range(dinfo, lr);
  420. debuginfo__delete(dinfo);
  421. if (ret == 0) {
  422. pr_warning("Specified source line is not found.\n");
  423. return -ENOENT;
  424. } else if (ret < 0) {
  425. pr_warning("Debuginfo analysis failed. (%d)\n", ret);
  426. return ret;
  427. }
  428. /* Convert source file path */
  429. tmp = lr->path;
  430. ret = get_real_path(tmp, lr->comp_dir, &lr->path);
  431. free(tmp); /* Free old path */
  432. if (ret < 0) {
  433. pr_warning("Failed to find source file. (%d)\n", ret);
  434. return ret;
  435. }
  436. setup_pager();
  437. if (lr->function)
  438. fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
  439. lr->start - lr->offset);
  440. else
  441. fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
  442. fp = fopen(lr->path, "r");
  443. if (fp == NULL) {
  444. pr_warning("Failed to open %s: %s\n", lr->path,
  445. strerror(errno));
  446. return -errno;
  447. }
  448. /* Skip to starting line number */
  449. while (l < lr->start) {
  450. ret = skip_one_line(fp, l++);
  451. if (ret < 0)
  452. goto end;
  453. }
  454. list_for_each_entry(ln, &lr->line_list, list) {
  455. for (; ln->line > l; l++) {
  456. ret = show_one_line(fp, l - lr->offset);
  457. if (ret < 0)
  458. goto end;
  459. }
  460. ret = show_one_line_with_num(fp, l++ - lr->offset);
  461. if (ret < 0)
  462. goto end;
  463. }
  464. if (lr->end == INT_MAX)
  465. lr->end = l + NR_ADDITIONAL_LINES;
  466. while (l <= lr->end) {
  467. ret = show_one_line_or_eof(fp, l++ - lr->offset);
  468. if (ret <= 0)
  469. break;
  470. }
  471. end:
  472. fclose(fp);
  473. return ret;
  474. }
  475. static int show_available_vars_at(struct debuginfo *dinfo,
  476. struct perf_probe_event *pev,
  477. int max_vls, struct strfilter *_filter,
  478. bool externs)
  479. {
  480. char *buf;
  481. int ret, i, nvars;
  482. struct str_node *node;
  483. struct variable_list *vls = NULL, *vl;
  484. const char *var;
  485. buf = synthesize_perf_probe_point(&pev->point);
  486. if (!buf)
  487. return -EINVAL;
  488. pr_debug("Searching variables at %s\n", buf);
  489. ret = debuginfo__find_available_vars_at(dinfo, pev, &vls,
  490. max_vls, externs);
  491. if (ret <= 0) {
  492. pr_err("Failed to find variables at %s (%d)\n", buf, ret);
  493. goto end;
  494. }
  495. /* Some variables are found */
  496. fprintf(stdout, "Available variables at %s\n", buf);
  497. for (i = 0; i < ret; i++) {
  498. vl = &vls[i];
  499. /*
  500. * A probe point might be converted to
  501. * several trace points.
  502. */
  503. fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
  504. vl->point.offset);
  505. free(vl->point.symbol);
  506. nvars = 0;
  507. if (vl->vars) {
  508. strlist__for_each(node, vl->vars) {
  509. var = strchr(node->s, '\t') + 1;
  510. if (strfilter__compare(_filter, var)) {
  511. fprintf(stdout, "\t\t%s\n", node->s);
  512. nvars++;
  513. }
  514. }
  515. strlist__delete(vl->vars);
  516. }
  517. if (nvars == 0)
  518. fprintf(stdout, "\t\t(No matched variables)\n");
  519. }
  520. free(vls);
  521. end:
  522. free(buf);
  523. return ret;
  524. }
  525. /* Show available variables on given probe point */
  526. int show_available_vars(struct perf_probe_event *pevs, int npevs,
  527. int max_vls, const char *module,
  528. struct strfilter *_filter, bool externs)
  529. {
  530. int i, ret = 0;
  531. struct debuginfo *dinfo;
  532. ret = init_vmlinux();
  533. if (ret < 0)
  534. return ret;
  535. dinfo = open_debuginfo(module);
  536. if (!dinfo) {
  537. pr_warning("Failed to open debuginfo file.\n");
  538. return -ENOENT;
  539. }
  540. setup_pager();
  541. for (i = 0; i < npevs && ret >= 0; i++)
  542. ret = show_available_vars_at(dinfo, &pevs[i], max_vls, _filter,
  543. externs);
  544. debuginfo__delete(dinfo);
  545. return ret;
  546. }
  547. #else /* !DWARF_SUPPORT */
  548. static int kprobe_convert_to_perf_probe(struct probe_trace_point *tp,
  549. struct perf_probe_point *pp)
  550. {
  551. struct symbol *sym;
  552. sym = __find_kernel_function_by_name(tp->symbol, NULL);
  553. if (!sym) {
  554. pr_err("Failed to find symbol %s in kernel.\n", tp->symbol);
  555. return -ENOENT;
  556. }
  557. return convert_to_perf_probe_point(tp, pp);
  558. }
  559. static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
  560. struct probe_trace_event **tevs __maybe_unused,
  561. int max_tevs __maybe_unused, const char *target)
  562. {
  563. if (perf_probe_event_need_dwarf(pev)) {
  564. pr_warning("Debuginfo-analysis is not supported.\n");
  565. return -ENOSYS;
  566. }
  567. if (pev->uprobes)
  568. return convert_name_to_addr(pev, target);
  569. return 0;
  570. }
  571. int show_line_range(struct line_range *lr __maybe_unused,
  572. const char *module __maybe_unused)
  573. {
  574. pr_warning("Debuginfo-analysis is not supported.\n");
  575. return -ENOSYS;
  576. }
  577. int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
  578. int npevs __maybe_unused, int max_vls __maybe_unused,
  579. const char *module __maybe_unused,
  580. struct strfilter *filter __maybe_unused,
  581. bool externs __maybe_unused)
  582. {
  583. pr_warning("Debuginfo-analysis is not supported.\n");
  584. return -ENOSYS;
  585. }
  586. #endif
  587. static int parse_line_num(char **ptr, int *val, const char *what)
  588. {
  589. const char *start = *ptr;
  590. errno = 0;
  591. *val = strtol(*ptr, ptr, 0);
  592. if (errno || *ptr == start) {
  593. semantic_error("'%s' is not a valid number.\n", what);
  594. return -EINVAL;
  595. }
  596. return 0;
  597. }
  598. /*
  599. * Stuff 'lr' according to the line range described by 'arg'.
  600. * The line range syntax is described by:
  601. *
  602. * SRC[:SLN[+NUM|-ELN]]
  603. * FNC[@SRC][:SLN[+NUM|-ELN]]
  604. */
  605. int parse_line_range_desc(const char *arg, struct line_range *lr)
  606. {
  607. char *range, *file, *name = strdup(arg);
  608. int err;
  609. if (!name)
  610. return -ENOMEM;
  611. lr->start = 0;
  612. lr->end = INT_MAX;
  613. range = strchr(name, ':');
  614. if (range) {
  615. *range++ = '\0';
  616. err = parse_line_num(&range, &lr->start, "start line");
  617. if (err)
  618. goto err;
  619. if (*range == '+' || *range == '-') {
  620. const char c = *range++;
  621. err = parse_line_num(&range, &lr->end, "end line");
  622. if (err)
  623. goto err;
  624. if (c == '+') {
  625. lr->end += lr->start;
  626. /*
  627. * Adjust the number of lines here.
  628. * If the number of lines == 1, the
  629. * the end of line should be equal to
  630. * the start of line.
  631. */
  632. lr->end--;
  633. }
  634. }
  635. pr_debug("Line range is %d to %d\n", lr->start, lr->end);
  636. err = -EINVAL;
  637. if (lr->start > lr->end) {
  638. semantic_error("Start line must be smaller"
  639. " than end line.\n");
  640. goto err;
  641. }
  642. if (*range != '\0') {
  643. semantic_error("Tailing with invalid str '%s'.\n", range);
  644. goto err;
  645. }
  646. }
  647. file = strchr(name, '@');
  648. if (file) {
  649. *file = '\0';
  650. lr->file = strdup(++file);
  651. if (lr->file == NULL) {
  652. err = -ENOMEM;
  653. goto err;
  654. }
  655. lr->function = name;
  656. } else if (strchr(name, '.'))
  657. lr->file = name;
  658. else
  659. lr->function = name;
  660. return 0;
  661. err:
  662. free(name);
  663. return err;
  664. }
  665. /* Check the name is good for event/group */
  666. static bool check_event_name(const char *name)
  667. {
  668. if (!isalpha(*name) && *name != '_')
  669. return false;
  670. while (*++name != '\0') {
  671. if (!isalpha(*name) && !isdigit(*name) && *name != '_')
  672. return false;
  673. }
  674. return true;
  675. }
  676. /* Parse probepoint definition. */
  677. static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
  678. {
  679. struct perf_probe_point *pp = &pev->point;
  680. char *ptr, *tmp;
  681. char c, nc = 0;
  682. /*
  683. * <Syntax>
  684. * perf probe [EVENT=]SRC[:LN|;PTN]
  685. * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
  686. *
  687. * TODO:Group name support
  688. */
  689. ptr = strpbrk(arg, ";=@+%");
  690. if (ptr && *ptr == '=') { /* Event name */
  691. *ptr = '\0';
  692. tmp = ptr + 1;
  693. if (strchr(arg, ':')) {
  694. semantic_error("Group name is not supported yet.\n");
  695. return -ENOTSUP;
  696. }
  697. if (!check_event_name(arg)) {
  698. semantic_error("%s is bad for event name -it must "
  699. "follow C symbol-naming rule.\n", arg);
  700. return -EINVAL;
  701. }
  702. pev->event = strdup(arg);
  703. if (pev->event == NULL)
  704. return -ENOMEM;
  705. pev->group = NULL;
  706. arg = tmp;
  707. }
  708. ptr = strpbrk(arg, ";:+@%");
  709. if (ptr) {
  710. nc = *ptr;
  711. *ptr++ = '\0';
  712. }
  713. tmp = strdup(arg);
  714. if (tmp == NULL)
  715. return -ENOMEM;
  716. /* Check arg is function or file and copy it */
  717. if (strchr(tmp, '.')) /* File */
  718. pp->file = tmp;
  719. else /* Function */
  720. pp->function = tmp;
  721. /* Parse other options */
  722. while (ptr) {
  723. arg = ptr;
  724. c = nc;
  725. if (c == ';') { /* Lazy pattern must be the last part */
  726. pp->lazy_line = strdup(arg);
  727. if (pp->lazy_line == NULL)
  728. return -ENOMEM;
  729. break;
  730. }
  731. ptr = strpbrk(arg, ";:+@%");
  732. if (ptr) {
  733. nc = *ptr;
  734. *ptr++ = '\0';
  735. }
  736. switch (c) {
  737. case ':': /* Line number */
  738. pp->line = strtoul(arg, &tmp, 0);
  739. if (*tmp != '\0') {
  740. semantic_error("There is non-digit char"
  741. " in line number.\n");
  742. return -EINVAL;
  743. }
  744. break;
  745. case '+': /* Byte offset from a symbol */
  746. pp->offset = strtoul(arg, &tmp, 0);
  747. if (*tmp != '\0') {
  748. semantic_error("There is non-digit character"
  749. " in offset.\n");
  750. return -EINVAL;
  751. }
  752. break;
  753. case '@': /* File name */
  754. if (pp->file) {
  755. semantic_error("SRC@SRC is not allowed.\n");
  756. return -EINVAL;
  757. }
  758. pp->file = strdup(arg);
  759. if (pp->file == NULL)
  760. return -ENOMEM;
  761. break;
  762. case '%': /* Probe places */
  763. if (strcmp(arg, "return") == 0) {
  764. pp->retprobe = 1;
  765. } else { /* Others not supported yet */
  766. semantic_error("%%%s is not supported.\n", arg);
  767. return -ENOTSUP;
  768. }
  769. break;
  770. default: /* Buggy case */
  771. pr_err("This program has a bug at %s:%d.\n",
  772. __FILE__, __LINE__);
  773. return -ENOTSUP;
  774. break;
  775. }
  776. }
  777. /* Exclusion check */
  778. if (pp->lazy_line && pp->line) {
  779. semantic_error("Lazy pattern can't be used with"
  780. " line number.\n");
  781. return -EINVAL;
  782. }
  783. if (pp->lazy_line && pp->offset) {
  784. semantic_error("Lazy pattern can't be used with offset.\n");
  785. return -EINVAL;
  786. }
  787. if (pp->line && pp->offset) {
  788. semantic_error("Offset can't be used with line number.\n");
  789. return -EINVAL;
  790. }
  791. if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
  792. semantic_error("File always requires line number or "
  793. "lazy pattern.\n");
  794. return -EINVAL;
  795. }
  796. if (pp->offset && !pp->function) {
  797. semantic_error("Offset requires an entry function.\n");
  798. return -EINVAL;
  799. }
  800. if (pp->retprobe && !pp->function) {
  801. semantic_error("Return probe requires an entry function.\n");
  802. return -EINVAL;
  803. }
  804. if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
  805. semantic_error("Offset/Line/Lazy pattern can't be used with "
  806. "return probe.\n");
  807. return -EINVAL;
  808. }
  809. pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
  810. pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
  811. pp->lazy_line);
  812. return 0;
  813. }
  814. /* Parse perf-probe event argument */
  815. static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
  816. {
  817. char *tmp, *goodname;
  818. struct perf_probe_arg_field **fieldp;
  819. pr_debug("parsing arg: %s into ", str);
  820. tmp = strchr(str, '=');
  821. if (tmp) {
  822. arg->name = strndup(str, tmp - str);
  823. if (arg->name == NULL)
  824. return -ENOMEM;
  825. pr_debug("name:%s ", arg->name);
  826. str = tmp + 1;
  827. }
  828. tmp = strchr(str, ':');
  829. if (tmp) { /* Type setting */
  830. *tmp = '\0';
  831. arg->type = strdup(tmp + 1);
  832. if (arg->type == NULL)
  833. return -ENOMEM;
  834. pr_debug("type:%s ", arg->type);
  835. }
  836. tmp = strpbrk(str, "-.[");
  837. if (!is_c_varname(str) || !tmp) {
  838. /* A variable, register, symbol or special value */
  839. arg->var = strdup(str);
  840. if (arg->var == NULL)
  841. return -ENOMEM;
  842. pr_debug("%s\n", arg->var);
  843. return 0;
  844. }
  845. /* Structure fields or array element */
  846. arg->var = strndup(str, tmp - str);
  847. if (arg->var == NULL)
  848. return -ENOMEM;
  849. goodname = arg->var;
  850. pr_debug("%s, ", arg->var);
  851. fieldp = &arg->field;
  852. do {
  853. *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
  854. if (*fieldp == NULL)
  855. return -ENOMEM;
  856. if (*tmp == '[') { /* Array */
  857. str = tmp;
  858. (*fieldp)->index = strtol(str + 1, &tmp, 0);
  859. (*fieldp)->ref = true;
  860. if (*tmp != ']' || tmp == str + 1) {
  861. semantic_error("Array index must be a"
  862. " number.\n");
  863. return -EINVAL;
  864. }
  865. tmp++;
  866. if (*tmp == '\0')
  867. tmp = NULL;
  868. } else { /* Structure */
  869. if (*tmp == '.') {
  870. str = tmp + 1;
  871. (*fieldp)->ref = false;
  872. } else if (tmp[1] == '>') {
  873. str = tmp + 2;
  874. (*fieldp)->ref = true;
  875. } else {
  876. semantic_error("Argument parse error: %s\n",
  877. str);
  878. return -EINVAL;
  879. }
  880. tmp = strpbrk(str, "-.[");
  881. }
  882. if (tmp) {
  883. (*fieldp)->name = strndup(str, tmp - str);
  884. if ((*fieldp)->name == NULL)
  885. return -ENOMEM;
  886. if (*str != '[')
  887. goodname = (*fieldp)->name;
  888. pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
  889. fieldp = &(*fieldp)->next;
  890. }
  891. } while (tmp);
  892. (*fieldp)->name = strdup(str);
  893. if ((*fieldp)->name == NULL)
  894. return -ENOMEM;
  895. if (*str != '[')
  896. goodname = (*fieldp)->name;
  897. pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
  898. /* If no name is specified, set the last field name (not array index)*/
  899. if (!arg->name) {
  900. arg->name = strdup(goodname);
  901. if (arg->name == NULL)
  902. return -ENOMEM;
  903. }
  904. return 0;
  905. }
  906. /* Parse perf-probe event command */
  907. int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
  908. {
  909. char **argv;
  910. int argc, i, ret = 0;
  911. argv = argv_split(cmd, &argc);
  912. if (!argv) {
  913. pr_debug("Failed to split arguments.\n");
  914. return -ENOMEM;
  915. }
  916. if (argc - 1 > MAX_PROBE_ARGS) {
  917. semantic_error("Too many probe arguments (%d).\n", argc - 1);
  918. ret = -ERANGE;
  919. goto out;
  920. }
  921. /* Parse probe point */
  922. ret = parse_perf_probe_point(argv[0], pev);
  923. if (ret < 0)
  924. goto out;
  925. /* Copy arguments and ensure return probe has no C argument */
  926. pev->nargs = argc - 1;
  927. pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
  928. if (pev->args == NULL) {
  929. ret = -ENOMEM;
  930. goto out;
  931. }
  932. for (i = 0; i < pev->nargs && ret >= 0; i++) {
  933. ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
  934. if (ret >= 0 &&
  935. is_c_varname(pev->args[i].var) && pev->point.retprobe) {
  936. semantic_error("You can't specify local variable for"
  937. " kretprobe.\n");
  938. ret = -EINVAL;
  939. }
  940. }
  941. out:
  942. argv_free(argv);
  943. return ret;
  944. }
  945. /* Return true if this perf_probe_event requires debuginfo */
  946. bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
  947. {
  948. int i;
  949. if (pev->point.file || pev->point.line || pev->point.lazy_line)
  950. return true;
  951. for (i = 0; i < pev->nargs; i++)
  952. if (is_c_varname(pev->args[i].var))
  953. return true;
  954. return false;
  955. }
  956. /* Parse probe_events event into struct probe_point */
  957. static int parse_probe_trace_command(const char *cmd,
  958. struct probe_trace_event *tev)
  959. {
  960. struct probe_trace_point *tp = &tev->point;
  961. char pr;
  962. char *p;
  963. char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
  964. int ret, i, argc;
  965. char **argv;
  966. pr_debug("Parsing probe_events: %s\n", cmd);
  967. argv = argv_split(cmd, &argc);
  968. if (!argv) {
  969. pr_debug("Failed to split arguments.\n");
  970. return -ENOMEM;
  971. }
  972. if (argc < 2) {
  973. semantic_error("Too few probe arguments.\n");
  974. ret = -ERANGE;
  975. goto out;
  976. }
  977. /* Scan event and group name. */
  978. argv0_str = strdup(argv[0]);
  979. if (argv0_str == NULL) {
  980. ret = -ENOMEM;
  981. goto out;
  982. }
  983. fmt1_str = strtok_r(argv0_str, ":", &fmt);
  984. fmt2_str = strtok_r(NULL, "/", &fmt);
  985. fmt3_str = strtok_r(NULL, " \t", &fmt);
  986. if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
  987. || fmt3_str == NULL) {
  988. semantic_error("Failed to parse event name: %s\n", argv[0]);
  989. ret = -EINVAL;
  990. goto out;
  991. }
  992. pr = fmt1_str[0];
  993. tev->group = strdup(fmt2_str);
  994. tev->event = strdup(fmt3_str);
  995. if (tev->group == NULL || tev->event == NULL) {
  996. ret = -ENOMEM;
  997. goto out;
  998. }
  999. pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
  1000. tp->retprobe = (pr == 'r');
  1001. /* Scan module name(if there), function name and offset */
  1002. p = strchr(argv[1], ':');
  1003. if (p) {
  1004. tp->module = strndup(argv[1], p - argv[1]);
  1005. p++;
  1006. } else
  1007. p = argv[1];
  1008. fmt1_str = strtok_r(p, "+", &fmt);
  1009. tp->symbol = strdup(fmt1_str);
  1010. if (tp->symbol == NULL) {
  1011. ret = -ENOMEM;
  1012. goto out;
  1013. }
  1014. fmt2_str = strtok_r(NULL, "", &fmt);
  1015. if (fmt2_str == NULL)
  1016. tp->offset = 0;
  1017. else
  1018. tp->offset = strtoul(fmt2_str, NULL, 10);
  1019. tev->nargs = argc - 2;
  1020. tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
  1021. if (tev->args == NULL) {
  1022. ret = -ENOMEM;
  1023. goto out;
  1024. }
  1025. for (i = 0; i < tev->nargs; i++) {
  1026. p = strchr(argv[i + 2], '=');
  1027. if (p) /* We don't need which register is assigned. */
  1028. *p++ = '\0';
  1029. else
  1030. p = argv[i + 2];
  1031. tev->args[i].name = strdup(argv[i + 2]);
  1032. /* TODO: parse regs and offset */
  1033. tev->args[i].value = strdup(p);
  1034. if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
  1035. ret = -ENOMEM;
  1036. goto out;
  1037. }
  1038. }
  1039. ret = 0;
  1040. out:
  1041. free(argv0_str);
  1042. argv_free(argv);
  1043. return ret;
  1044. }
  1045. /* Compose only probe arg */
  1046. int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
  1047. {
  1048. struct perf_probe_arg_field *field = pa->field;
  1049. int ret;
  1050. char *tmp = buf;
  1051. if (pa->name && pa->var)
  1052. ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
  1053. else
  1054. ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
  1055. if (ret <= 0)
  1056. goto error;
  1057. tmp += ret;
  1058. len -= ret;
  1059. while (field) {
  1060. if (field->name[0] == '[')
  1061. ret = e_snprintf(tmp, len, "%s", field->name);
  1062. else
  1063. ret = e_snprintf(tmp, len, "%s%s",
  1064. field->ref ? "->" : ".", field->name);
  1065. if (ret <= 0)
  1066. goto error;
  1067. tmp += ret;
  1068. len -= ret;
  1069. field = field->next;
  1070. }
  1071. if (pa->type) {
  1072. ret = e_snprintf(tmp, len, ":%s", pa->type);
  1073. if (ret <= 0)
  1074. goto error;
  1075. tmp += ret;
  1076. len -= ret;
  1077. }
  1078. return tmp - buf;
  1079. error:
  1080. pr_debug("Failed to synthesize perf probe argument: %s\n",
  1081. strerror(-ret));
  1082. return ret;
  1083. }
  1084. /* Compose only probe point (not argument) */
  1085. static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
  1086. {
  1087. char *buf, *tmp;
  1088. char offs[32] = "", line[32] = "", file[32] = "";
  1089. int ret, len;
  1090. buf = zalloc(MAX_CMDLEN);
  1091. if (buf == NULL) {
  1092. ret = -ENOMEM;
  1093. goto error;
  1094. }
  1095. if (pp->offset) {
  1096. ret = e_snprintf(offs, 32, "+%lu", pp->offset);
  1097. if (ret <= 0)
  1098. goto error;
  1099. }
  1100. if (pp->line) {
  1101. ret = e_snprintf(line, 32, ":%d", pp->line);
  1102. if (ret <= 0)
  1103. goto error;
  1104. }
  1105. if (pp->file) {
  1106. tmp = pp->file;
  1107. len = strlen(tmp);
  1108. if (len > 30) {
  1109. tmp = strchr(pp->file + len - 30, '/');
  1110. tmp = tmp ? tmp + 1 : pp->file + len - 30;
  1111. }
  1112. ret = e_snprintf(file, 32, "@%s", tmp);
  1113. if (ret <= 0)
  1114. goto error;
  1115. }
  1116. if (pp->function)
  1117. ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
  1118. offs, pp->retprobe ? "%return" : "", line,
  1119. file);
  1120. else
  1121. ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
  1122. if (ret <= 0)
  1123. goto error;
  1124. return buf;
  1125. error:
  1126. pr_debug("Failed to synthesize perf probe point: %s\n",
  1127. strerror(-ret));
  1128. if (buf)
  1129. free(buf);
  1130. return NULL;
  1131. }
  1132. #if 0
  1133. char *synthesize_perf_probe_command(struct perf_probe_event *pev)
  1134. {
  1135. char *buf;
  1136. int i, len, ret;
  1137. buf = synthesize_perf_probe_point(&pev->point);
  1138. if (!buf)
  1139. return NULL;
  1140. len = strlen(buf);
  1141. for (i = 0; i < pev->nargs; i++) {
  1142. ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
  1143. pev->args[i].name);
  1144. if (ret <= 0) {
  1145. free(buf);
  1146. return NULL;
  1147. }
  1148. len += ret;
  1149. }
  1150. return buf;
  1151. }
  1152. #endif
  1153. static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
  1154. char **buf, size_t *buflen,
  1155. int depth)
  1156. {
  1157. int ret;
  1158. if (ref->next) {
  1159. depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
  1160. buflen, depth + 1);
  1161. if (depth < 0)
  1162. goto out;
  1163. }
  1164. ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
  1165. if (ret < 0)
  1166. depth = ret;
  1167. else {
  1168. *buf += ret;
  1169. *buflen -= ret;
  1170. }
  1171. out:
  1172. return depth;
  1173. }
  1174. static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
  1175. char *buf, size_t buflen)
  1176. {
  1177. struct probe_trace_arg_ref *ref = arg->ref;
  1178. int ret, depth = 0;
  1179. char *tmp = buf;
  1180. /* Argument name or separator */
  1181. if (arg->name)
  1182. ret = e_snprintf(buf, buflen, " %s=", arg->name);
  1183. else
  1184. ret = e_snprintf(buf, buflen, " ");
  1185. if (ret < 0)
  1186. return ret;
  1187. buf += ret;
  1188. buflen -= ret;
  1189. /* Special case: @XXX */
  1190. if (arg->value[0] == '@' && arg->ref)
  1191. ref = ref->next;
  1192. /* Dereferencing arguments */
  1193. if (ref) {
  1194. depth = __synthesize_probe_trace_arg_ref(ref, &buf,
  1195. &buflen, 1);
  1196. if (depth < 0)
  1197. return depth;
  1198. }
  1199. /* Print argument value */
  1200. if (arg->value[0] == '@' && arg->ref)
  1201. ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
  1202. arg->ref->offset);
  1203. else
  1204. ret = e_snprintf(buf, buflen, "%s", arg->value);
  1205. if (ret < 0)
  1206. return ret;
  1207. buf += ret;
  1208. buflen -= ret;
  1209. /* Closing */
  1210. while (depth--) {
  1211. ret = e_snprintf(buf, buflen, ")");
  1212. if (ret < 0)
  1213. return ret;
  1214. buf += ret;
  1215. buflen -= ret;
  1216. }
  1217. /* Print argument type */
  1218. if (arg->type) {
  1219. ret = e_snprintf(buf, buflen, ":%s", arg->type);
  1220. if (ret <= 0)
  1221. return ret;
  1222. buf += ret;
  1223. }
  1224. return buf - tmp;
  1225. }
  1226. char *synthesize_probe_trace_command(struct probe_trace_event *tev)
  1227. {
  1228. struct probe_trace_point *tp = &tev->point;
  1229. char *buf;
  1230. int i, len, ret;
  1231. buf = zalloc(MAX_CMDLEN);
  1232. if (buf == NULL)
  1233. return NULL;
  1234. if (tev->uprobes)
  1235. len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s:%s",
  1236. tp->retprobe ? 'r' : 'p',
  1237. tev->group, tev->event,
  1238. tp->module, tp->symbol);
  1239. else
  1240. len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s%s%s+%lu",
  1241. tp->retprobe ? 'r' : 'p',
  1242. tev->group, tev->event,
  1243. tp->module ?: "", tp->module ? ":" : "",
  1244. tp->symbol, tp->offset);
  1245. if (len <= 0)
  1246. goto error;
  1247. for (i = 0; i < tev->nargs; i++) {
  1248. ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
  1249. MAX_CMDLEN - len);
  1250. if (ret <= 0)
  1251. goto error;
  1252. len += ret;
  1253. }
  1254. return buf;
  1255. error:
  1256. free(buf);
  1257. return NULL;
  1258. }
  1259. static int convert_to_perf_probe_event(struct probe_trace_event *tev,
  1260. struct perf_probe_event *pev, bool is_kprobe)
  1261. {
  1262. char buf[64] = "";
  1263. int i, ret;
  1264. /* Convert event/group name */
  1265. pev->event = strdup(tev->event);
  1266. pev->group = strdup(tev->group);
  1267. if (pev->event == NULL || pev->group == NULL)
  1268. return -ENOMEM;
  1269. /* Convert trace_point to probe_point */
  1270. if (is_kprobe)
  1271. ret = kprobe_convert_to_perf_probe(&tev->point, &pev->point);
  1272. else
  1273. ret = convert_to_perf_probe_point(&tev->point, &pev->point);
  1274. if (ret < 0)
  1275. return ret;
  1276. /* Convert trace_arg to probe_arg */
  1277. pev->nargs = tev->nargs;
  1278. pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
  1279. if (pev->args == NULL)
  1280. return -ENOMEM;
  1281. for (i = 0; i < tev->nargs && ret >= 0; i++) {
  1282. if (tev->args[i].name)
  1283. pev->args[i].name = strdup(tev->args[i].name);
  1284. else {
  1285. ret = synthesize_probe_trace_arg(&tev->args[i],
  1286. buf, 64);
  1287. pev->args[i].name = strdup(buf);
  1288. }
  1289. if (pev->args[i].name == NULL && ret >= 0)
  1290. ret = -ENOMEM;
  1291. }
  1292. if (ret < 0)
  1293. clear_perf_probe_event(pev);
  1294. return ret;
  1295. }
  1296. void clear_perf_probe_event(struct perf_probe_event *pev)
  1297. {
  1298. struct perf_probe_point *pp = &pev->point;
  1299. struct perf_probe_arg_field *field, *next;
  1300. int i;
  1301. if (pev->event)
  1302. free(pev->event);
  1303. if (pev->group)
  1304. free(pev->group);
  1305. if (pp->file)
  1306. free(pp->file);
  1307. if (pp->function)
  1308. free(pp->function);
  1309. if (pp->lazy_line)
  1310. free(pp->lazy_line);
  1311. for (i = 0; i < pev->nargs; i++) {
  1312. if (pev->args[i].name)
  1313. free(pev->args[i].name);
  1314. if (pev->args[i].var)
  1315. free(pev->args[i].var);
  1316. if (pev->args[i].type)
  1317. free(pev->args[i].type);
  1318. field = pev->args[i].field;
  1319. while (field) {
  1320. next = field->next;
  1321. if (field->name)
  1322. free(field->name);
  1323. free(field);
  1324. field = next;
  1325. }
  1326. }
  1327. if (pev->args)
  1328. free(pev->args);
  1329. memset(pev, 0, sizeof(*pev));
  1330. }
  1331. static void clear_probe_trace_event(struct probe_trace_event *tev)
  1332. {
  1333. struct probe_trace_arg_ref *ref, *next;
  1334. int i;
  1335. if (tev->event)
  1336. free(tev->event);
  1337. if (tev->group)
  1338. free(tev->group);
  1339. if (tev->point.symbol)
  1340. free(tev->point.symbol);
  1341. if (tev->point.module)
  1342. free(tev->point.module);
  1343. for (i = 0; i < tev->nargs; i++) {
  1344. if (tev->args[i].name)
  1345. free(tev->args[i].name);
  1346. if (tev->args[i].value)
  1347. free(tev->args[i].value);
  1348. if (tev->args[i].type)
  1349. free(tev->args[i].type);
  1350. ref = tev->args[i].ref;
  1351. while (ref) {
  1352. next = ref->next;
  1353. free(ref);
  1354. ref = next;
  1355. }
  1356. }
  1357. if (tev->args)
  1358. free(tev->args);
  1359. memset(tev, 0, sizeof(*tev));
  1360. }
  1361. static void print_warn_msg(const char *file, bool is_kprobe)
  1362. {
  1363. if (errno == ENOENT) {
  1364. const char *config;
  1365. if (!is_kprobe)
  1366. config = "CONFIG_UPROBE_EVENTS";
  1367. else
  1368. config = "CONFIG_KPROBE_EVENTS";
  1369. pr_warning("%s file does not exist - please rebuild kernel"
  1370. " with %s.\n", file, config);
  1371. } else
  1372. pr_warning("Failed to open %s file: %s\n", file,
  1373. strerror(errno));
  1374. }
  1375. static int open_probe_events(const char *trace_file, bool readwrite,
  1376. bool is_kprobe)
  1377. {
  1378. char buf[PATH_MAX];
  1379. const char *__debugfs;
  1380. int ret;
  1381. __debugfs = debugfs_find_mountpoint();
  1382. if (__debugfs == NULL) {
  1383. pr_warning("Debugfs is not mounted.\n");
  1384. return -ENOENT;
  1385. }
  1386. ret = e_snprintf(buf, PATH_MAX, "%s/%s", __debugfs, trace_file);
  1387. if (ret >= 0) {
  1388. pr_debug("Opening %s write=%d\n", buf, readwrite);
  1389. if (readwrite && !probe_event_dry_run)
  1390. ret = open(buf, O_RDWR, O_APPEND);
  1391. else
  1392. ret = open(buf, O_RDONLY, 0);
  1393. if (ret < 0)
  1394. print_warn_msg(buf, is_kprobe);
  1395. }
  1396. return ret;
  1397. }
  1398. static int open_kprobe_events(bool readwrite)
  1399. {
  1400. return open_probe_events("tracing/kprobe_events", readwrite, true);
  1401. }
  1402. static int open_uprobe_events(bool readwrite)
  1403. {
  1404. return open_probe_events("tracing/uprobe_events", readwrite, false);
  1405. }
  1406. /* Get raw string list of current kprobe_events or uprobe_events */
  1407. static struct strlist *get_probe_trace_command_rawlist(int fd)
  1408. {
  1409. int ret, idx;
  1410. FILE *fp;
  1411. char buf[MAX_CMDLEN];
  1412. char *p;
  1413. struct strlist *sl;
  1414. sl = strlist__new(true, NULL);
  1415. fp = fdopen(dup(fd), "r");
  1416. while (!feof(fp)) {
  1417. p = fgets(buf, MAX_CMDLEN, fp);
  1418. if (!p)
  1419. break;
  1420. idx = strlen(p) - 1;
  1421. if (p[idx] == '\n')
  1422. p[idx] = '\0';
  1423. ret = strlist__add(sl, buf);
  1424. if (ret < 0) {
  1425. pr_debug("strlist__add failed: %s\n", strerror(-ret));
  1426. strlist__delete(sl);
  1427. return NULL;
  1428. }
  1429. }
  1430. fclose(fp);
  1431. return sl;
  1432. }
  1433. /* Show an event */
  1434. static int show_perf_probe_event(struct perf_probe_event *pev)
  1435. {
  1436. int i, ret;
  1437. char buf[128];
  1438. char *place;
  1439. /* Synthesize only event probe point */
  1440. place = synthesize_perf_probe_point(&pev->point);
  1441. if (!place)
  1442. return -EINVAL;
  1443. ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
  1444. if (ret < 0)
  1445. return ret;
  1446. printf(" %-20s (on %s", buf, place);
  1447. if (pev->nargs > 0) {
  1448. printf(" with");
  1449. for (i = 0; i < pev->nargs; i++) {
  1450. ret = synthesize_perf_probe_arg(&pev->args[i],
  1451. buf, 128);
  1452. if (ret < 0)
  1453. break;
  1454. printf(" %s", buf);
  1455. }
  1456. }
  1457. printf(")\n");
  1458. free(place);
  1459. return ret;
  1460. }
  1461. static int __show_perf_probe_events(int fd, bool is_kprobe)
  1462. {
  1463. int ret = 0;
  1464. struct probe_trace_event tev;
  1465. struct perf_probe_event pev;
  1466. struct strlist *rawlist;
  1467. struct str_node *ent;
  1468. memset(&tev, 0, sizeof(tev));
  1469. memset(&pev, 0, sizeof(pev));
  1470. rawlist = get_probe_trace_command_rawlist(fd);
  1471. if (!rawlist)
  1472. return -ENOENT;
  1473. strlist__for_each(ent, rawlist) {
  1474. ret = parse_probe_trace_command(ent->s, &tev);
  1475. if (ret >= 0) {
  1476. ret = convert_to_perf_probe_event(&tev, &pev,
  1477. is_kprobe);
  1478. if (ret >= 0)
  1479. ret = show_perf_probe_event(&pev);
  1480. }
  1481. clear_perf_probe_event(&pev);
  1482. clear_probe_trace_event(&tev);
  1483. if (ret < 0)
  1484. break;
  1485. }
  1486. strlist__delete(rawlist);
  1487. return ret;
  1488. }
  1489. /* List up current perf-probe events */
  1490. int show_perf_probe_events(void)
  1491. {
  1492. int fd, ret;
  1493. setup_pager();
  1494. fd = open_kprobe_events(false);
  1495. if (fd < 0)
  1496. return fd;
  1497. ret = init_vmlinux();
  1498. if (ret < 0)
  1499. return ret;
  1500. ret = __show_perf_probe_events(fd, true);
  1501. close(fd);
  1502. fd = open_uprobe_events(false);
  1503. if (fd >= 0) {
  1504. ret = __show_perf_probe_events(fd, false);
  1505. close(fd);
  1506. }
  1507. return ret;
  1508. }
  1509. /* Get current perf-probe event names */
  1510. static struct strlist *get_probe_trace_event_names(int fd, bool include_group)
  1511. {
  1512. char buf[128];
  1513. struct strlist *sl, *rawlist;
  1514. struct str_node *ent;
  1515. struct probe_trace_event tev;
  1516. int ret = 0;
  1517. memset(&tev, 0, sizeof(tev));
  1518. rawlist = get_probe_trace_command_rawlist(fd);
  1519. sl = strlist__new(true, NULL);
  1520. strlist__for_each(ent, rawlist) {
  1521. ret = parse_probe_trace_command(ent->s, &tev);
  1522. if (ret < 0)
  1523. break;
  1524. if (include_group) {
  1525. ret = e_snprintf(buf, 128, "%s:%s", tev.group,
  1526. tev.event);
  1527. if (ret >= 0)
  1528. ret = strlist__add(sl, buf);
  1529. } else
  1530. ret = strlist__add(sl, tev.event);
  1531. clear_probe_trace_event(&tev);
  1532. if (ret < 0)
  1533. break;
  1534. }
  1535. strlist__delete(rawlist);
  1536. if (ret < 0) {
  1537. strlist__delete(sl);
  1538. return NULL;
  1539. }
  1540. return sl;
  1541. }
  1542. static int write_probe_trace_event(int fd, struct probe_trace_event *tev)
  1543. {
  1544. int ret = 0;
  1545. char *buf = synthesize_probe_trace_command(tev);
  1546. if (!buf) {
  1547. pr_debug("Failed to synthesize probe trace event.\n");
  1548. return -EINVAL;
  1549. }
  1550. pr_debug("Writing event: %s\n", buf);
  1551. if (!probe_event_dry_run) {
  1552. ret = write(fd, buf, strlen(buf));
  1553. if (ret <= 0)
  1554. pr_warning("Failed to write event: %s\n",
  1555. strerror(errno));
  1556. }
  1557. free(buf);
  1558. return ret;
  1559. }
  1560. static int get_new_event_name(char *buf, size_t len, const char *base,
  1561. struct strlist *namelist, bool allow_suffix)
  1562. {
  1563. int i, ret;
  1564. /* Try no suffix */
  1565. ret = e_snprintf(buf, len, "%s", base);
  1566. if (ret < 0) {
  1567. pr_debug("snprintf() failed: %s\n", strerror(-ret));
  1568. return ret;
  1569. }
  1570. if (!strlist__has_entry(namelist, buf))
  1571. return 0;
  1572. if (!allow_suffix) {
  1573. pr_warning("Error: event \"%s\" already exists. "
  1574. "(Use -f to force duplicates.)\n", base);
  1575. return -EEXIST;
  1576. }
  1577. /* Try to add suffix */
  1578. for (i = 1; i < MAX_EVENT_INDEX; i++) {
  1579. ret = e_snprintf(buf, len, "%s_%d", base, i);
  1580. if (ret < 0) {
  1581. pr_debug("snprintf() failed: %s\n", strerror(-ret));
  1582. return ret;
  1583. }
  1584. if (!strlist__has_entry(namelist, buf))
  1585. break;
  1586. }
  1587. if (i == MAX_EVENT_INDEX) {
  1588. pr_warning("Too many events are on the same function.\n");
  1589. ret = -ERANGE;
  1590. }
  1591. return ret;
  1592. }
  1593. static int __add_probe_trace_events(struct perf_probe_event *pev,
  1594. struct probe_trace_event *tevs,
  1595. int ntevs, bool allow_suffix)
  1596. {
  1597. int i, fd, ret;
  1598. struct probe_trace_event *tev = NULL;
  1599. char buf[64];
  1600. const char *event, *group;
  1601. struct strlist *namelist;
  1602. if (pev->uprobes)
  1603. fd = open_uprobe_events(true);
  1604. else
  1605. fd = open_kprobe_events(true);
  1606. if (fd < 0)
  1607. return fd;
  1608. /* Get current event names */
  1609. namelist = get_probe_trace_event_names(fd, false);
  1610. if (!namelist) {
  1611. pr_debug("Failed to get current event list.\n");
  1612. return -EIO;
  1613. }
  1614. ret = 0;
  1615. printf("Added new event%s\n", (ntevs > 1) ? "s:" : ":");
  1616. for (i = 0; i < ntevs; i++) {
  1617. tev = &tevs[i];
  1618. if (pev->event)
  1619. event = pev->event;
  1620. else
  1621. if (pev->point.function)
  1622. event = pev->point.function;
  1623. else
  1624. event = tev->point.symbol;
  1625. if (pev->group)
  1626. group = pev->group;
  1627. else
  1628. group = PERFPROBE_GROUP;
  1629. /* Get an unused new event name */
  1630. ret = get_new_event_name(buf, 64, event,
  1631. namelist, allow_suffix);
  1632. if (ret < 0)
  1633. break;
  1634. event = buf;
  1635. tev->event = strdup(event);
  1636. tev->group = strdup(group);
  1637. if (tev->event == NULL || tev->group == NULL) {
  1638. ret = -ENOMEM;
  1639. break;
  1640. }
  1641. ret = write_probe_trace_event(fd, tev);
  1642. if (ret < 0)
  1643. break;
  1644. /* Add added event name to namelist */
  1645. strlist__add(namelist, event);
  1646. /* Trick here - save current event/group */
  1647. event = pev->event;
  1648. group = pev->group;
  1649. pev->event = tev->event;
  1650. pev->group = tev->group;
  1651. show_perf_probe_event(pev);
  1652. /* Trick here - restore current event/group */
  1653. pev->event = (char *)event;
  1654. pev->group = (char *)group;
  1655. /*
  1656. * Probes after the first probe which comes from same
  1657. * user input are always allowed to add suffix, because
  1658. * there might be several addresses corresponding to
  1659. * one code line.
  1660. */
  1661. allow_suffix = true;
  1662. }
  1663. if (ret >= 0) {
  1664. /* Show how to use the event. */
  1665. printf("\nYou can now use it in all perf tools, such as:\n\n");
  1666. printf("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group,
  1667. tev->event);
  1668. }
  1669. strlist__delete(namelist);
  1670. close(fd);
  1671. return ret;
  1672. }
  1673. static int convert_to_probe_trace_events(struct perf_probe_event *pev,
  1674. struct probe_trace_event **tevs,
  1675. int max_tevs, const char *target)
  1676. {
  1677. struct symbol *sym;
  1678. int ret = 0, i;
  1679. struct probe_trace_event *tev;
  1680. /* Convert perf_probe_event with debuginfo */
  1681. ret = try_to_find_probe_trace_events(pev, tevs, max_tevs, target);
  1682. if (ret != 0)
  1683. return ret; /* Found in debuginfo or got an error */
  1684. /* Allocate trace event buffer */
  1685. tev = *tevs = zalloc(sizeof(struct probe_trace_event));
  1686. if (tev == NULL)
  1687. return -ENOMEM;
  1688. /* Copy parameters */
  1689. tev->point.symbol = strdup(pev->point.function);
  1690. if (tev->point.symbol == NULL) {
  1691. ret = -ENOMEM;
  1692. goto error;
  1693. }
  1694. if (target) {
  1695. tev->point.module = strdup(target);
  1696. if (tev->point.module == NULL) {
  1697. ret = -ENOMEM;
  1698. goto error;
  1699. }
  1700. }
  1701. tev->point.offset = pev->point.offset;
  1702. tev->point.retprobe = pev->point.retprobe;
  1703. tev->nargs = pev->nargs;
  1704. tev->uprobes = pev->uprobes;
  1705. if (tev->nargs) {
  1706. tev->args = zalloc(sizeof(struct probe_trace_arg)
  1707. * tev->nargs);
  1708. if (tev->args == NULL) {
  1709. ret = -ENOMEM;
  1710. goto error;
  1711. }
  1712. for (i = 0; i < tev->nargs; i++) {
  1713. if (pev->args[i].name) {
  1714. tev->args[i].name = strdup(pev->args[i].name);
  1715. if (tev->args[i].name == NULL) {
  1716. ret = -ENOMEM;
  1717. goto error;
  1718. }
  1719. }
  1720. tev->args[i].value = strdup(pev->args[i].var);
  1721. if (tev->args[i].value == NULL) {
  1722. ret = -ENOMEM;
  1723. goto error;
  1724. }
  1725. if (pev->args[i].type) {
  1726. tev->args[i].type = strdup(pev->args[i].type);
  1727. if (tev->args[i].type == NULL) {
  1728. ret = -ENOMEM;
  1729. goto error;
  1730. }
  1731. }
  1732. }
  1733. }
  1734. if (pev->uprobes)
  1735. return 1;
  1736. /* Currently just checking function name from symbol map */
  1737. sym = __find_kernel_function_by_name(tev->point.symbol, NULL);
  1738. if (!sym) {
  1739. pr_warning("Kernel symbol \'%s\' not found.\n",
  1740. tev->point.symbol);
  1741. ret = -ENOENT;
  1742. goto error;
  1743. } else if (tev->point.offset > sym->end - sym->start) {
  1744. pr_warning("Offset specified is greater than size of %s\n",
  1745. tev->point.symbol);
  1746. ret = -ENOENT;
  1747. goto error;
  1748. }
  1749. return 1;
  1750. error:
  1751. clear_probe_trace_event(tev);
  1752. free(tev);
  1753. *tevs = NULL;
  1754. return ret;
  1755. }
  1756. struct __event_package {
  1757. struct perf_probe_event *pev;
  1758. struct probe_trace_event *tevs;
  1759. int ntevs;
  1760. };
  1761. int add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
  1762. int max_tevs, const char *target, bool force_add)
  1763. {
  1764. int i, j, ret;
  1765. struct __event_package *pkgs;
  1766. ret = 0;
  1767. pkgs = zalloc(sizeof(struct __event_package) * npevs);
  1768. if (pkgs == NULL)
  1769. return -ENOMEM;
  1770. if (!pevs->uprobes)
  1771. /* Init vmlinux path */
  1772. ret = init_vmlinux();
  1773. else
  1774. ret = init_user_exec();
  1775. if (ret < 0) {
  1776. free(pkgs);
  1777. return ret;
  1778. }
  1779. /* Loop 1: convert all events */
  1780. for (i = 0; i < npevs; i++) {
  1781. pkgs[i].pev = &pevs[i];
  1782. /* Convert with or without debuginfo */
  1783. ret = convert_to_probe_trace_events(pkgs[i].pev,
  1784. &pkgs[i].tevs,
  1785. max_tevs,
  1786. target);
  1787. if (ret < 0)
  1788. goto end;
  1789. pkgs[i].ntevs = ret;
  1790. }
  1791. /* Loop 2: add all events */
  1792. for (i = 0; i < npevs; i++) {
  1793. ret = __add_probe_trace_events(pkgs[i].pev, pkgs[i].tevs,
  1794. pkgs[i].ntevs, force_add);
  1795. if (ret < 0)
  1796. break;
  1797. }
  1798. end:
  1799. /* Loop 3: cleanup and free trace events */
  1800. for (i = 0; i < npevs; i++) {
  1801. for (j = 0; j < pkgs[i].ntevs; j++)
  1802. clear_probe_trace_event(&pkgs[i].tevs[j]);
  1803. free(pkgs[i].tevs);
  1804. }
  1805. free(pkgs);
  1806. return ret;
  1807. }
  1808. static int __del_trace_probe_event(int fd, struct str_node *ent)
  1809. {
  1810. char *p;
  1811. char buf[128];
  1812. int ret;
  1813. /* Convert from perf-probe event to trace-probe event */
  1814. ret = e_snprintf(buf, 128, "-:%s", ent->s);
  1815. if (ret < 0)
  1816. goto error;
  1817. p = strchr(buf + 2, ':');
  1818. if (!p) {
  1819. pr_debug("Internal error: %s should have ':' but not.\n",
  1820. ent->s);
  1821. ret = -ENOTSUP;
  1822. goto error;
  1823. }
  1824. *p = '/';
  1825. pr_debug("Writing event: %s\n", buf);
  1826. ret = write(fd, buf, strlen(buf));
  1827. if (ret < 0) {
  1828. ret = -errno;
  1829. goto error;
  1830. }
  1831. printf("Removed event: %s\n", ent->s);
  1832. return 0;
  1833. error:
  1834. pr_warning("Failed to delete event: %s\n", strerror(-ret));
  1835. return ret;
  1836. }
  1837. static int del_trace_probe_event(int fd, const char *buf,
  1838. struct strlist *namelist)
  1839. {
  1840. struct str_node *ent, *n;
  1841. int ret = -1;
  1842. if (strpbrk(buf, "*?")) { /* Glob-exp */
  1843. strlist__for_each_safe(ent, n, namelist)
  1844. if (strglobmatch(ent->s, buf)) {
  1845. ret = __del_trace_probe_event(fd, ent);
  1846. if (ret < 0)
  1847. break;
  1848. strlist__remove(namelist, ent);
  1849. }
  1850. } else {
  1851. ent = strlist__find(namelist, buf);
  1852. if (ent) {
  1853. ret = __del_trace_probe_event(fd, ent);
  1854. if (ret >= 0)
  1855. strlist__remove(namelist, ent);
  1856. }
  1857. }
  1858. return ret;
  1859. }
  1860. int del_perf_probe_events(struct strlist *dellist)
  1861. {
  1862. int ret = -1, ufd = -1, kfd = -1;
  1863. char buf[128];
  1864. const char *group, *event;
  1865. char *p, *str;
  1866. struct str_node *ent;
  1867. struct strlist *namelist = NULL, *unamelist = NULL;
  1868. /* Get current event names */
  1869. kfd = open_kprobe_events(true);
  1870. if (kfd < 0)
  1871. return kfd;
  1872. namelist = get_probe_trace_event_names(kfd, true);
  1873. ufd = open_uprobe_events(true);
  1874. if (ufd >= 0)
  1875. unamelist = get_probe_trace_event_names(ufd, true);
  1876. if (namelist == NULL && unamelist == NULL)
  1877. goto error;
  1878. strlist__for_each(ent, dellist) {
  1879. str = strdup(ent->s);
  1880. if (str == NULL) {
  1881. ret = -ENOMEM;
  1882. goto error;
  1883. }
  1884. pr_debug("Parsing: %s\n", str);
  1885. p = strchr(str, ':');
  1886. if (p) {
  1887. group = str;
  1888. *p = '\0';
  1889. event = p + 1;
  1890. } else {
  1891. group = "*";
  1892. event = str;
  1893. }
  1894. ret = e_snprintf(buf, 128, "%s:%s", group, event);
  1895. if (ret < 0) {
  1896. pr_err("Failed to copy event.");
  1897. free(str);
  1898. goto error;
  1899. }
  1900. pr_debug("Group: %s, Event: %s\n", group, event);
  1901. if (namelist)
  1902. ret = del_trace_probe_event(kfd, buf, namelist);
  1903. if (unamelist && ret != 0)
  1904. ret = del_trace_probe_event(ufd, buf, unamelist);
  1905. if (ret != 0)
  1906. pr_info("Info: Event \"%s\" does not exist.\n", buf);
  1907. free(str);
  1908. }
  1909. error:
  1910. if (kfd >= 0) {
  1911. strlist__delete(namelist);
  1912. close(kfd);
  1913. }
  1914. if (ufd >= 0) {
  1915. strlist__delete(unamelist);
  1916. close(ufd);
  1917. }
  1918. return ret;
  1919. }
  1920. /* TODO: don't use a global variable for filter ... */
  1921. static struct strfilter *available_func_filter;
  1922. /*
  1923. * If a symbol corresponds to a function with global binding and
  1924. * matches filter return 0. For all others return 1.
  1925. */
  1926. static int filter_available_functions(struct map *map __maybe_unused,
  1927. struct symbol *sym)
  1928. {
  1929. if (sym->binding == STB_GLOBAL &&
  1930. strfilter__compare(available_func_filter, sym->name))
  1931. return 0;
  1932. return 1;
  1933. }
  1934. static int __show_available_funcs(struct map *map)
  1935. {
  1936. if (map__load(map, filter_available_functions)) {
  1937. pr_err("Failed to load map.\n");
  1938. return -EINVAL;
  1939. }
  1940. if (!dso__sorted_by_name(map->dso, map->type))
  1941. dso__sort_by_name(map->dso, map->type);
  1942. dso__fprintf_symbols_by_name(map->dso, map->type, stdout);
  1943. return 0;
  1944. }
  1945. static int available_kernel_funcs(const char *module)
  1946. {
  1947. struct map *map;
  1948. int ret;
  1949. ret = init_vmlinux();
  1950. if (ret < 0)
  1951. return ret;
  1952. map = kernel_get_module_map(module);
  1953. if (!map) {
  1954. pr_err("Failed to find %s map.\n", (module) ? : "kernel");
  1955. return -EINVAL;
  1956. }
  1957. return __show_available_funcs(map);
  1958. }
  1959. static int available_user_funcs(const char *target)
  1960. {
  1961. struct map *map;
  1962. int ret;
  1963. ret = init_user_exec();
  1964. if (ret < 0)
  1965. return ret;
  1966. map = dso__new_map(target);
  1967. ret = __show_available_funcs(map);
  1968. dso__delete(map->dso);
  1969. map__delete(map);
  1970. return ret;
  1971. }
  1972. int show_available_funcs(const char *target, struct strfilter *_filter,
  1973. bool user)
  1974. {
  1975. setup_pager();
  1976. available_func_filter = _filter;
  1977. if (!user)
  1978. return available_kernel_funcs(target);
  1979. return available_user_funcs(target);
  1980. }
  1981. /*
  1982. * uprobe_events only accepts address:
  1983. * Convert function and any offset to address
  1984. */
  1985. static int convert_name_to_addr(struct perf_probe_event *pev, const char *exec)
  1986. {
  1987. struct perf_probe_point *pp = &pev->point;
  1988. struct symbol *sym;
  1989. struct map *map = NULL;
  1990. char *function = NULL, *name = NULL;
  1991. int ret = -EINVAL;
  1992. unsigned long long vaddr = 0;
  1993. if (!pp->function) {
  1994. pr_warning("No function specified for uprobes");
  1995. goto out;
  1996. }
  1997. function = strdup(pp->function);
  1998. if (!function) {
  1999. pr_warning("Failed to allocate memory by strdup.\n");
  2000. ret = -ENOMEM;
  2001. goto out;
  2002. }
  2003. name = realpath(exec, NULL);
  2004. if (!name) {
  2005. pr_warning("Cannot find realpath for %s.\n", exec);
  2006. goto out;
  2007. }
  2008. map = dso__new_map(name);
  2009. if (!map) {
  2010. pr_warning("Cannot find appropriate DSO for %s.\n", exec);
  2011. goto out;
  2012. }
  2013. available_func_filter = strfilter__new(function, NULL);
  2014. if (map__load(map, filter_available_functions)) {
  2015. pr_err("Failed to load map.\n");
  2016. goto out;
  2017. }
  2018. sym = map__find_symbol_by_name(map, function, NULL);
  2019. if (!sym) {
  2020. pr_warning("Cannot find %s in DSO %s\n", function, exec);
  2021. goto out;
  2022. }
  2023. if (map->start > sym->start)
  2024. vaddr = map->start;
  2025. vaddr += sym->start + pp->offset + map->pgoff;
  2026. pp->offset = 0;
  2027. if (!pev->event) {
  2028. pev->event = function;
  2029. function = NULL;
  2030. }
  2031. if (!pev->group) {
  2032. char *ptr1, *ptr2, *exec_copy;
  2033. pev->group = zalloc(sizeof(char *) * 64);
  2034. exec_copy = strdup(exec);
  2035. if (!exec_copy) {
  2036. ret = -ENOMEM;
  2037. pr_warning("Failed to copy exec string.\n");
  2038. goto out;
  2039. }
  2040. ptr1 = strdup(basename(exec_copy));
  2041. if (ptr1) {
  2042. ptr2 = strpbrk(ptr1, "-._");
  2043. if (ptr2)
  2044. *ptr2 = '\0';
  2045. e_snprintf(pev->group, 64, "%s_%s", PERFPROBE_GROUP,
  2046. ptr1);
  2047. free(ptr1);
  2048. }
  2049. free(exec_copy);
  2050. }
  2051. free(pp->function);
  2052. pp->function = zalloc(sizeof(char *) * MAX_PROBE_ARGS);
  2053. if (!pp->function) {
  2054. ret = -ENOMEM;
  2055. pr_warning("Failed to allocate memory by zalloc.\n");
  2056. goto out;
  2057. }
  2058. e_snprintf(pp->function, MAX_PROBE_ARGS, "0x%llx", vaddr);
  2059. ret = 0;
  2060. out:
  2061. if (map) {
  2062. dso__delete(map->dso);
  2063. map__delete(map);
  2064. }
  2065. if (function)
  2066. free(function);
  2067. if (name)
  2068. free(name);
  2069. return ret;
  2070. }