probe-event.c 42 KB

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