probe-event.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261
  1. /*
  2. * probe-event.c : perf-probe definition to kprobe_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 "trace-event.h" /* For __unused */
  44. #include "parse-events.h" /* For debugfs_path */
  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 ...) die("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 map_groups kmap_groups;
  68. static struct map *kmaps[MAP__NR_TYPES];
  69. /* Initialize symbol maps and path of vmlinux */
  70. static void init_vmlinux(void)
  71. {
  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. if (symbol__init() < 0)
  78. die("Failed to init symbol map.");
  79. map_groups__init(&kmap_groups);
  80. if (map_groups__create_kernel_maps(&kmap_groups, kmaps) < 0)
  81. die("Failed to create kernel maps.");
  82. }
  83. #ifdef DWARF_SUPPORT
  84. static int open_vmlinux(void)
  85. {
  86. if (map__load(kmaps[MAP__FUNCTION], NULL) < 0) {
  87. pr_debug("Failed to load kernel map.\n");
  88. return -EINVAL;
  89. }
  90. pr_debug("Try to open %s\n", kmaps[MAP__FUNCTION]->dso->long_name);
  91. return open(kmaps[MAP__FUNCTION]->dso->long_name, O_RDONLY);
  92. }
  93. static void convert_to_perf_probe_point(struct kprobe_trace_point *tp,
  94. struct perf_probe_point *pp)
  95. {
  96. struct symbol *sym;
  97. int fd, ret = 0;
  98. sym = map__find_symbol_by_name(kmaps[MAP__FUNCTION],
  99. tp->symbol, NULL);
  100. if (sym) {
  101. fd = open_vmlinux();
  102. ret = find_perf_probe_point(fd, sym->start + tp->offset, pp);
  103. close(fd);
  104. }
  105. if (ret <= 0) {
  106. pp->function = xstrdup(tp->symbol);
  107. pp->offset = tp->offset;
  108. }
  109. pp->retprobe = tp->retprobe;
  110. }
  111. /* Try to find perf_probe_event with debuginfo */
  112. static int try_to_find_kprobe_trace_events(struct perf_probe_event *pev,
  113. struct kprobe_trace_event **tevs)
  114. {
  115. bool need_dwarf = perf_probe_event_need_dwarf(pev);
  116. int fd, ntevs;
  117. fd = open_vmlinux();
  118. if (fd < 0) {
  119. if (need_dwarf)
  120. die("Could not open debuginfo file.");
  121. pr_debug("Could not open vmlinux. Try to use symbols.\n");
  122. return 0;
  123. }
  124. /* Searching trace events corresponding to probe event */
  125. ntevs = find_kprobe_trace_events(fd, pev, tevs);
  126. close(fd);
  127. if (ntevs > 0) /* Succeeded to find trace events */
  128. return ntevs;
  129. if (ntevs == 0) /* No error but failed to find probe point. */
  130. die("Probe point '%s' not found. - probe not added.",
  131. synthesize_perf_probe_point(&pev->point));
  132. /* Error path */
  133. if (need_dwarf) {
  134. if (ntevs == -ENOENT)
  135. pr_warning("No dwarf info found in the vmlinux - "
  136. "please rebuild with CONFIG_DEBUG_INFO=y.\n");
  137. die("Could not analyze debuginfo.");
  138. }
  139. pr_debug("An error occurred in debuginfo analysis."
  140. " Try to use symbols.\n");
  141. return 0;
  142. }
  143. #define LINEBUF_SIZE 256
  144. #define NR_ADDITIONAL_LINES 2
  145. static void show_one_line(FILE *fp, unsigned int l, bool skip, bool show_num)
  146. {
  147. char buf[LINEBUF_SIZE];
  148. const char *color = PERF_COLOR_BLUE;
  149. if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
  150. goto error;
  151. if (!skip) {
  152. if (show_num)
  153. fprintf(stdout, "%7u %s", l, buf);
  154. else
  155. color_fprintf(stdout, color, " %s", buf);
  156. }
  157. while (strlen(buf) == LINEBUF_SIZE - 1 &&
  158. buf[LINEBUF_SIZE - 2] != '\n') {
  159. if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
  160. goto error;
  161. if (!skip) {
  162. if (show_num)
  163. fprintf(stdout, "%s", buf);
  164. else
  165. color_fprintf(stdout, color, "%s", buf);
  166. }
  167. }
  168. return;
  169. error:
  170. if (feof(fp))
  171. die("Source file is shorter than expected.");
  172. else
  173. die("File read error: %s", strerror(errno));
  174. }
  175. /*
  176. * Show line-range always requires debuginfo to find source file and
  177. * line number.
  178. */
  179. void show_line_range(struct line_range *lr)
  180. {
  181. unsigned int l = 1;
  182. struct line_node *ln;
  183. FILE *fp;
  184. int fd, ret;
  185. /* Search a line range */
  186. init_vmlinux();
  187. fd = open_vmlinux();
  188. if (fd < 0)
  189. die("Could not open debuginfo file.");
  190. ret = find_line_range(fd, lr);
  191. if (ret <= 0)
  192. die("Source line is not found.\n");
  193. close(fd);
  194. setup_pager();
  195. if (lr->function)
  196. fprintf(stdout, "<%s:%d>\n", lr->function,
  197. lr->start - lr->offset);
  198. else
  199. fprintf(stdout, "<%s:%d>\n", lr->file, lr->start);
  200. fp = fopen(lr->path, "r");
  201. if (fp == NULL)
  202. die("Failed to open %s: %s", lr->path, strerror(errno));
  203. /* Skip to starting line number */
  204. while (l < lr->start)
  205. show_one_line(fp, l++, true, false);
  206. list_for_each_entry(ln, &lr->line_list, list) {
  207. while (ln->line > l)
  208. show_one_line(fp, (l++) - lr->offset, false, false);
  209. show_one_line(fp, (l++) - lr->offset, false, true);
  210. }
  211. if (lr->end == INT_MAX)
  212. lr->end = l + NR_ADDITIONAL_LINES;
  213. while (l < lr->end && !feof(fp))
  214. show_one_line(fp, (l++) - lr->offset, false, false);
  215. fclose(fp);
  216. }
  217. #else /* !DWARF_SUPPORT */
  218. static void convert_to_perf_probe_point(struct kprobe_trace_point *tp,
  219. struct perf_probe_point *pp)
  220. {
  221. pp->function = xstrdup(tp->symbol);
  222. pp->offset = tp->offset;
  223. pp->retprobe = tp->retprobe;
  224. }
  225. static int try_to_find_kprobe_trace_events(struct perf_probe_event *pev,
  226. struct kprobe_trace_event **tevs __unused)
  227. {
  228. if (perf_probe_event_need_dwarf(pev))
  229. die("Debuginfo-analysis is not supported");
  230. return 0;
  231. }
  232. void show_line_range(struct line_range *lr __unused)
  233. {
  234. die("Debuginfo-analysis is not supported");
  235. }
  236. #endif
  237. void parse_line_range_desc(const char *arg, struct line_range *lr)
  238. {
  239. const char *ptr;
  240. char *tmp;
  241. /*
  242. * <Syntax>
  243. * SRC:SLN[+NUM|-ELN]
  244. * FUNC[:SLN[+NUM|-ELN]]
  245. */
  246. ptr = strchr(arg, ':');
  247. if (ptr) {
  248. lr->start = (unsigned int)strtoul(ptr + 1, &tmp, 0);
  249. if (*tmp == '+')
  250. lr->end = lr->start + (unsigned int)strtoul(tmp + 1,
  251. &tmp, 0);
  252. else if (*tmp == '-')
  253. lr->end = (unsigned int)strtoul(tmp + 1, &tmp, 0);
  254. else
  255. lr->end = 0;
  256. pr_debug("Line range is %u to %u\n", lr->start, lr->end);
  257. if (lr->end && lr->start > lr->end)
  258. semantic_error("Start line must be smaller"
  259. " than end line.");
  260. if (*tmp != '\0')
  261. semantic_error("Tailing with invalid character '%d'.",
  262. *tmp);
  263. tmp = xstrndup(arg, (ptr - arg));
  264. } else
  265. tmp = xstrdup(arg);
  266. if (strchr(tmp, '.'))
  267. lr->file = tmp;
  268. else
  269. lr->function = tmp;
  270. }
  271. /* Check the name is good for event/group */
  272. static bool check_event_name(const char *name)
  273. {
  274. if (!isalpha(*name) && *name != '_')
  275. return false;
  276. while (*++name != '\0') {
  277. if (!isalpha(*name) && !isdigit(*name) && *name != '_')
  278. return false;
  279. }
  280. return true;
  281. }
  282. /* Parse probepoint definition. */
  283. static void parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
  284. {
  285. struct perf_probe_point *pp = &pev->point;
  286. char *ptr, *tmp;
  287. char c, nc = 0;
  288. /*
  289. * <Syntax>
  290. * perf probe [EVENT=]SRC[:LN|;PTN]
  291. * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
  292. *
  293. * TODO:Group name support
  294. */
  295. ptr = strpbrk(arg, ";=@+%");
  296. if (ptr && *ptr == '=') { /* Event name */
  297. *ptr = '\0';
  298. tmp = ptr + 1;
  299. ptr = strchr(arg, ':');
  300. if (ptr) /* Group name is not supported yet. */
  301. semantic_error("Group name is not supported yet.");
  302. if (!check_event_name(arg))
  303. semantic_error("%s is bad for event name -it must "
  304. "follow C symbol-naming rule.", arg);
  305. pev->event = xstrdup(arg);
  306. pev->group = NULL;
  307. arg = tmp;
  308. }
  309. ptr = strpbrk(arg, ";:+@%");
  310. if (ptr) {
  311. nc = *ptr;
  312. *ptr++ = '\0';
  313. }
  314. /* Check arg is function or file and copy it */
  315. if (strchr(arg, '.')) /* File */
  316. pp->file = xstrdup(arg);
  317. else /* Function */
  318. pp->function = xstrdup(arg);
  319. /* Parse other options */
  320. while (ptr) {
  321. arg = ptr;
  322. c = nc;
  323. if (c == ';') { /* Lazy pattern must be the last part */
  324. pp->lazy_line = xstrdup(arg);
  325. break;
  326. }
  327. ptr = strpbrk(arg, ";:+@%");
  328. if (ptr) {
  329. nc = *ptr;
  330. *ptr++ = '\0';
  331. }
  332. switch (c) {
  333. case ':': /* Line number */
  334. pp->line = strtoul(arg, &tmp, 0);
  335. if (*tmp != '\0')
  336. semantic_error("There is non-digit char"
  337. " in line number.");
  338. break;
  339. case '+': /* Byte offset from a symbol */
  340. pp->offset = strtoul(arg, &tmp, 0);
  341. if (*tmp != '\0')
  342. semantic_error("There is non-digit character"
  343. " in offset.");
  344. break;
  345. case '@': /* File name */
  346. if (pp->file)
  347. semantic_error("SRC@SRC is not allowed.");
  348. pp->file = xstrdup(arg);
  349. break;
  350. case '%': /* Probe places */
  351. if (strcmp(arg, "return") == 0) {
  352. pp->retprobe = 1;
  353. } else /* Others not supported yet */
  354. semantic_error("%%%s is not supported.", arg);
  355. break;
  356. default:
  357. DIE_IF("Program has a bug.");
  358. break;
  359. }
  360. }
  361. /* Exclusion check */
  362. if (pp->lazy_line && pp->line)
  363. semantic_error("Lazy pattern can't be used with line number.");
  364. if (pp->lazy_line && pp->offset)
  365. semantic_error("Lazy pattern can't be used with offset.");
  366. if (pp->line && pp->offset)
  367. semantic_error("Offset can't be used with line number.");
  368. if (!pp->line && !pp->lazy_line && pp->file && !pp->function)
  369. semantic_error("File always requires line number or "
  370. "lazy pattern.");
  371. if (pp->offset && !pp->function)
  372. semantic_error("Offset requires an entry function.");
  373. if (pp->retprobe && !pp->function)
  374. semantic_error("Return probe requires an entry function.");
  375. if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe)
  376. semantic_error("Offset/Line/Lazy pattern can't be used with "
  377. "return probe.");
  378. pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
  379. pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
  380. pp->lazy_line);
  381. }
  382. /* Parse perf-probe event argument */
  383. static void parse_perf_probe_arg(const char *str, struct perf_probe_arg *arg)
  384. {
  385. char *tmp;
  386. struct perf_probe_arg_field **fieldp;
  387. pr_debug("parsing arg: %s into ", str);
  388. tmp = strchr(str, '=');
  389. if (tmp) {
  390. arg->name = xstrndup(str, tmp - str);
  391. str = tmp + 1;
  392. }
  393. tmp = strpbrk(str, "-.");
  394. if (!is_c_varname(str) || !tmp) {
  395. /* A variable, register, symbol or special value */
  396. arg->var = xstrdup(str);
  397. pr_debug("%s\n", arg->var);
  398. return;
  399. }
  400. /* Structure fields */
  401. arg->var = xstrndup(str, tmp - str);
  402. pr_debug("%s, ", arg->var);
  403. fieldp = &arg->field;
  404. do {
  405. *fieldp = xzalloc(sizeof(struct perf_probe_arg_field));
  406. if (*tmp == '.') {
  407. str = tmp + 1;
  408. (*fieldp)->ref = false;
  409. } else if (tmp[1] == '>') {
  410. str = tmp + 2;
  411. (*fieldp)->ref = true;
  412. } else
  413. semantic_error("Argument parse error: %s", str);
  414. tmp = strpbrk(str, "-.");
  415. if (tmp) {
  416. (*fieldp)->name = xstrndup(str, tmp - str);
  417. pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
  418. fieldp = &(*fieldp)->next;
  419. }
  420. } while (tmp);
  421. (*fieldp)->name = xstrdup(str);
  422. pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
  423. }
  424. /* Parse perf-probe event command */
  425. void parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
  426. {
  427. char **argv;
  428. int argc, i;
  429. argv = argv_split(cmd, &argc);
  430. if (!argv)
  431. die("argv_split failed.");
  432. if (argc > MAX_PROBE_ARGS + 1)
  433. semantic_error("Too many arguments");
  434. /* Parse probe point */
  435. parse_perf_probe_point(argv[0], pev);
  436. /* Copy arguments and ensure return probe has no C argument */
  437. pev->nargs = argc - 1;
  438. pev->args = xzalloc(sizeof(struct perf_probe_arg) * pev->nargs);
  439. for (i = 0; i < pev->nargs; i++) {
  440. parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
  441. if (is_c_varname(pev->args[i].var) && pev->point.retprobe)
  442. semantic_error("You can't specify local variable for"
  443. " kretprobe");
  444. }
  445. argv_free(argv);
  446. }
  447. /* Return true if this perf_probe_event requires debuginfo */
  448. bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
  449. {
  450. int i;
  451. if (pev->point.file || pev->point.line || pev->point.lazy_line)
  452. return true;
  453. for (i = 0; i < pev->nargs; i++)
  454. if (is_c_varname(pev->args[i].var))
  455. return true;
  456. return false;
  457. }
  458. /* Parse kprobe_events event into struct probe_point */
  459. void parse_kprobe_trace_command(const char *cmd, struct kprobe_trace_event *tev)
  460. {
  461. struct kprobe_trace_point *tp = &tev->point;
  462. char pr;
  463. char *p;
  464. int ret, i, argc;
  465. char **argv;
  466. pr_debug("Parsing kprobe_events: %s\n", cmd);
  467. argv = argv_split(cmd, &argc);
  468. if (!argv)
  469. die("argv_split failed.");
  470. if (argc < 2)
  471. semantic_error("Too less arguments.");
  472. /* Scan event and group name. */
  473. ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]",
  474. &pr, (float *)(void *)&tev->group,
  475. (float *)(void *)&tev->event);
  476. if (ret != 3)
  477. semantic_error("Failed to parse event name: %s", argv[0]);
  478. pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
  479. tp->retprobe = (pr == 'r');
  480. /* Scan function name and offset */
  481. ret = sscanf(argv[1], "%a[^+]+%lu", (float *)(void *)&tp->symbol,
  482. &tp->offset);
  483. if (ret == 1)
  484. tp->offset = 0;
  485. tev->nargs = argc - 2;
  486. tev->args = xzalloc(sizeof(struct kprobe_trace_arg) * tev->nargs);
  487. for (i = 0; i < tev->nargs; i++) {
  488. p = strchr(argv[i + 2], '=');
  489. if (p) /* We don't need which register is assigned. */
  490. *p++ = '\0';
  491. else
  492. p = argv[i + 2];
  493. tev->args[i].name = xstrdup(argv[i + 2]);
  494. /* TODO: parse regs and offset */
  495. tev->args[i].value = xstrdup(p);
  496. }
  497. argv_free(argv);
  498. }
  499. /* Compose only probe arg */
  500. int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
  501. {
  502. struct perf_probe_arg_field *field = pa->field;
  503. int ret;
  504. char *tmp = buf;
  505. if (pa->name && pa->var)
  506. ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
  507. else
  508. ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
  509. if (ret <= 0)
  510. goto error;
  511. tmp += ret;
  512. len -= ret;
  513. while (field) {
  514. ret = e_snprintf(tmp, len, "%s%s", field->ref ? "->" : ".",
  515. field->name);
  516. if (ret <= 0)
  517. goto error;
  518. tmp += ret;
  519. len -= ret;
  520. field = field->next;
  521. }
  522. return tmp - buf;
  523. error:
  524. die("Failed to synthesize perf probe argument: %s", strerror(-ret));
  525. }
  526. /* Compose only probe point (not argument) */
  527. static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
  528. {
  529. char *buf, *tmp;
  530. char offs[32] = "", line[32] = "", file[32] = "";
  531. int ret, len;
  532. buf = xzalloc(MAX_CMDLEN);
  533. if (pp->offset) {
  534. ret = e_snprintf(offs, 32, "+%lu", pp->offset);
  535. if (ret <= 0)
  536. goto error;
  537. }
  538. if (pp->line) {
  539. ret = e_snprintf(line, 32, ":%d", pp->line);
  540. if (ret <= 0)
  541. goto error;
  542. }
  543. if (pp->file) {
  544. len = strlen(pp->file) - 32;
  545. if (len < 0)
  546. len = 0;
  547. tmp = strchr(pp->file + len, '/');
  548. if (!tmp)
  549. tmp = pp->file + len - 1;
  550. ret = e_snprintf(file, 32, "@%s", tmp + 1);
  551. if (ret <= 0)
  552. goto error;
  553. }
  554. if (pp->function)
  555. ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
  556. offs, pp->retprobe ? "%return" : "", line,
  557. file);
  558. else
  559. ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
  560. if (ret <= 0)
  561. goto error;
  562. return buf;
  563. error:
  564. die("Failed to synthesize perf probe point: %s", strerror(-ret));
  565. }
  566. #if 0
  567. char *synthesize_perf_probe_command(struct perf_probe_event *pev)
  568. {
  569. char *buf;
  570. int i, len, ret;
  571. buf = synthesize_perf_probe_point(&pev->point);
  572. if (!buf)
  573. return NULL;
  574. len = strlen(buf);
  575. for (i = 0; i < pev->nargs; i++) {
  576. ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
  577. pev->args[i].name);
  578. if (ret <= 0) {
  579. free(buf);
  580. return NULL;
  581. }
  582. len += ret;
  583. }
  584. return buf;
  585. }
  586. #endif
  587. static int __synthesize_kprobe_trace_arg_ref(struct kprobe_trace_arg_ref *ref,
  588. char **buf, size_t *buflen,
  589. int depth)
  590. {
  591. int ret;
  592. if (ref->next) {
  593. depth = __synthesize_kprobe_trace_arg_ref(ref->next, buf,
  594. buflen, depth + 1);
  595. if (depth < 0)
  596. goto out;
  597. }
  598. ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
  599. if (ret < 0)
  600. depth = ret;
  601. else {
  602. *buf += ret;
  603. *buflen -= ret;
  604. }
  605. out:
  606. return depth;
  607. }
  608. static int synthesize_kprobe_trace_arg(struct kprobe_trace_arg *arg,
  609. char *buf, size_t buflen)
  610. {
  611. int ret, depth = 0;
  612. char *tmp = buf;
  613. /* Argument name or separator */
  614. if (arg->name)
  615. ret = e_snprintf(buf, buflen, " %s=", arg->name);
  616. else
  617. ret = e_snprintf(buf, buflen, " ");
  618. if (ret < 0)
  619. return ret;
  620. buf += ret;
  621. buflen -= ret;
  622. /* Dereferencing arguments */
  623. if (arg->ref) {
  624. depth = __synthesize_kprobe_trace_arg_ref(arg->ref, &buf,
  625. &buflen, 1);
  626. if (depth < 0)
  627. return depth;
  628. }
  629. /* Print argument value */
  630. ret = e_snprintf(buf, buflen, "%s", arg->value);
  631. if (ret < 0)
  632. return ret;
  633. buf += ret;
  634. buflen -= ret;
  635. /* Closing */
  636. while (depth--) {
  637. ret = e_snprintf(buf, buflen, ")");
  638. if (ret < 0)
  639. return ret;
  640. buf += ret;
  641. buflen -= ret;
  642. }
  643. return buf - tmp;
  644. }
  645. char *synthesize_kprobe_trace_command(struct kprobe_trace_event *tev)
  646. {
  647. struct kprobe_trace_point *tp = &tev->point;
  648. char *buf;
  649. int i, len, ret;
  650. buf = xzalloc(MAX_CMDLEN);
  651. len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s+%lu",
  652. tp->retprobe ? 'r' : 'p',
  653. tev->group, tev->event,
  654. tp->symbol, tp->offset);
  655. if (len <= 0)
  656. goto error;
  657. for (i = 0; i < tev->nargs; i++) {
  658. ret = synthesize_kprobe_trace_arg(&tev->args[i], buf + len,
  659. MAX_CMDLEN - len);
  660. if (ret <= 0)
  661. goto error;
  662. len += ret;
  663. }
  664. return buf;
  665. error:
  666. free(buf);
  667. return NULL;
  668. }
  669. void convert_to_perf_probe_event(struct kprobe_trace_event *tev,
  670. struct perf_probe_event *pev)
  671. {
  672. char buf[64];
  673. int i;
  674. /* Convert event/group name */
  675. pev->event = xstrdup(tev->event);
  676. pev->group = xstrdup(tev->group);
  677. /* Convert trace_point to probe_point */
  678. convert_to_perf_probe_point(&tev->point, &pev->point);
  679. /* Convert trace_arg to probe_arg */
  680. pev->nargs = tev->nargs;
  681. pev->args = xzalloc(sizeof(struct perf_probe_arg) * pev->nargs);
  682. for (i = 0; i < tev->nargs; i++)
  683. if (tev->args[i].name)
  684. pev->args[i].name = xstrdup(tev->args[i].name);
  685. else {
  686. synthesize_kprobe_trace_arg(&tev->args[i], buf, 64);
  687. pev->args[i].name = xstrdup(buf);
  688. }
  689. }
  690. void clear_perf_probe_event(struct perf_probe_event *pev)
  691. {
  692. struct perf_probe_point *pp = &pev->point;
  693. struct perf_probe_arg_field *field, *next;
  694. int i;
  695. if (pev->event)
  696. free(pev->event);
  697. if (pev->group)
  698. free(pev->group);
  699. if (pp->file)
  700. free(pp->file);
  701. if (pp->function)
  702. free(pp->function);
  703. if (pp->lazy_line)
  704. free(pp->lazy_line);
  705. for (i = 0; i < pev->nargs; i++) {
  706. if (pev->args[i].name)
  707. free(pev->args[i].name);
  708. if (pev->args[i].var)
  709. free(pev->args[i].var);
  710. field = pev->args[i].field;
  711. while (field) {
  712. next = field->next;
  713. if (field->name)
  714. free(field->name);
  715. free(field);
  716. field = next;
  717. }
  718. }
  719. if (pev->args)
  720. free(pev->args);
  721. memset(pev, 0, sizeof(*pev));
  722. }
  723. void clear_kprobe_trace_event(struct kprobe_trace_event *tev)
  724. {
  725. struct kprobe_trace_arg_ref *ref, *next;
  726. int i;
  727. if (tev->event)
  728. free(tev->event);
  729. if (tev->group)
  730. free(tev->group);
  731. if (tev->point.symbol)
  732. free(tev->point.symbol);
  733. for (i = 0; i < tev->nargs; i++) {
  734. if (tev->args[i].name)
  735. free(tev->args[i].name);
  736. if (tev->args[i].value)
  737. free(tev->args[i].value);
  738. ref = tev->args[i].ref;
  739. while (ref) {
  740. next = ref->next;
  741. free(ref);
  742. ref = next;
  743. }
  744. }
  745. if (tev->args)
  746. free(tev->args);
  747. memset(tev, 0, sizeof(*tev));
  748. }
  749. static int open_kprobe_events(bool readwrite)
  750. {
  751. char buf[PATH_MAX];
  752. int ret;
  753. ret = e_snprintf(buf, PATH_MAX, "%s/../kprobe_events", debugfs_path);
  754. if (ret < 0)
  755. die("Failed to make kprobe_events path.");
  756. if (readwrite && !probe_event_dry_run)
  757. ret = open(buf, O_RDWR, O_APPEND);
  758. else
  759. ret = open(buf, O_RDONLY, 0);
  760. if (ret < 0) {
  761. if (errno == ENOENT)
  762. die("kprobe_events file does not exist -"
  763. " please rebuild with CONFIG_KPROBE_EVENT.");
  764. else
  765. die("Could not open kprobe_events file: %s",
  766. strerror(errno));
  767. }
  768. return ret;
  769. }
  770. /* Get raw string list of current kprobe_events */
  771. static struct strlist *get_kprobe_trace_command_rawlist(int fd)
  772. {
  773. int ret, idx;
  774. FILE *fp;
  775. char buf[MAX_CMDLEN];
  776. char *p;
  777. struct strlist *sl;
  778. sl = strlist__new(true, NULL);
  779. fp = fdopen(dup(fd), "r");
  780. while (!feof(fp)) {
  781. p = fgets(buf, MAX_CMDLEN, fp);
  782. if (!p)
  783. break;
  784. idx = strlen(p) - 1;
  785. if (p[idx] == '\n')
  786. p[idx] = '\0';
  787. ret = strlist__add(sl, buf);
  788. if (ret < 0)
  789. die("strlist__add failed: %s", strerror(-ret));
  790. }
  791. fclose(fp);
  792. return sl;
  793. }
  794. /* Show an event */
  795. static void show_perf_probe_event(struct perf_probe_event *pev)
  796. {
  797. int i, ret;
  798. char buf[128];
  799. char *place;
  800. /* Synthesize only event probe point */
  801. place = synthesize_perf_probe_point(&pev->point);
  802. ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
  803. if (ret < 0)
  804. die("Failed to copy event: %s", strerror(-ret));
  805. printf(" %-20s (on %s", buf, place);
  806. if (pev->nargs > 0) {
  807. printf(" with");
  808. for (i = 0; i < pev->nargs; i++) {
  809. synthesize_perf_probe_arg(&pev->args[i], buf, 128);
  810. printf(" %s", buf);
  811. }
  812. }
  813. printf(")\n");
  814. free(place);
  815. }
  816. /* List up current perf-probe events */
  817. void show_perf_probe_events(void)
  818. {
  819. int fd;
  820. struct kprobe_trace_event tev;
  821. struct perf_probe_event pev;
  822. struct strlist *rawlist;
  823. struct str_node *ent;
  824. setup_pager();
  825. init_vmlinux();
  826. memset(&tev, 0, sizeof(tev));
  827. memset(&pev, 0, sizeof(pev));
  828. fd = open_kprobe_events(false);
  829. rawlist = get_kprobe_trace_command_rawlist(fd);
  830. close(fd);
  831. strlist__for_each(ent, rawlist) {
  832. parse_kprobe_trace_command(ent->s, &tev);
  833. convert_to_perf_probe_event(&tev, &pev);
  834. /* Show an event */
  835. show_perf_probe_event(&pev);
  836. clear_perf_probe_event(&pev);
  837. clear_kprobe_trace_event(&tev);
  838. }
  839. strlist__delete(rawlist);
  840. }
  841. /* Get current perf-probe event names */
  842. static struct strlist *get_kprobe_trace_event_names(int fd, bool include_group)
  843. {
  844. char buf[128];
  845. struct strlist *sl, *rawlist;
  846. struct str_node *ent;
  847. struct kprobe_trace_event tev;
  848. memset(&tev, 0, sizeof(tev));
  849. rawlist = get_kprobe_trace_command_rawlist(fd);
  850. sl = strlist__new(true, NULL);
  851. strlist__for_each(ent, rawlist) {
  852. parse_kprobe_trace_command(ent->s, &tev);
  853. if (include_group) {
  854. if (e_snprintf(buf, 128, "%s:%s", tev.group,
  855. tev.event) < 0)
  856. die("Failed to copy group:event name.");
  857. strlist__add(sl, buf);
  858. } else
  859. strlist__add(sl, tev.event);
  860. clear_kprobe_trace_event(&tev);
  861. }
  862. strlist__delete(rawlist);
  863. return sl;
  864. }
  865. static void write_kprobe_trace_event(int fd, struct kprobe_trace_event *tev)
  866. {
  867. int ret;
  868. char *buf = synthesize_kprobe_trace_command(tev);
  869. pr_debug("Writing event: %s\n", buf);
  870. if (!probe_event_dry_run) {
  871. ret = write(fd, buf, strlen(buf));
  872. if (ret <= 0)
  873. die("Failed to write event: %s", strerror(errno));
  874. }
  875. free(buf);
  876. }
  877. static void get_new_event_name(char *buf, size_t len, const char *base,
  878. struct strlist *namelist, bool allow_suffix)
  879. {
  880. int i, ret;
  881. /* Try no suffix */
  882. ret = e_snprintf(buf, len, "%s", base);
  883. if (ret < 0)
  884. die("snprintf() failed: %s", strerror(-ret));
  885. if (!strlist__has_entry(namelist, buf))
  886. return;
  887. if (!allow_suffix) {
  888. pr_warning("Error: event \"%s\" already exists. "
  889. "(Use -f to force duplicates.)\n", base);
  890. die("Can't add new event.");
  891. }
  892. /* Try to add suffix */
  893. for (i = 1; i < MAX_EVENT_INDEX; i++) {
  894. ret = e_snprintf(buf, len, "%s_%d", base, i);
  895. if (ret < 0)
  896. die("snprintf() failed: %s", strerror(-ret));
  897. if (!strlist__has_entry(namelist, buf))
  898. break;
  899. }
  900. if (i == MAX_EVENT_INDEX)
  901. die("Too many events are on the same function.");
  902. }
  903. static void __add_kprobe_trace_events(struct perf_probe_event *pev,
  904. struct kprobe_trace_event *tevs,
  905. int ntevs, bool allow_suffix)
  906. {
  907. int i, fd;
  908. struct kprobe_trace_event *tev = NULL;
  909. char buf[64];
  910. const char *event, *group;
  911. struct strlist *namelist;
  912. fd = open_kprobe_events(true);
  913. /* Get current event names */
  914. namelist = get_kprobe_trace_event_names(fd, false);
  915. printf("Add new event%s\n", (ntevs > 1) ? "s:" : ":");
  916. for (i = 0; i < ntevs; i++) {
  917. tev = &tevs[i];
  918. if (pev->event)
  919. event = pev->event;
  920. else
  921. if (pev->point.function)
  922. event = pev->point.function;
  923. else
  924. event = tev->point.symbol;
  925. if (pev->group)
  926. group = pev->group;
  927. else
  928. group = PERFPROBE_GROUP;
  929. /* Get an unused new event name */
  930. get_new_event_name(buf, 64, event, namelist, allow_suffix);
  931. event = buf;
  932. tev->event = xstrdup(event);
  933. tev->group = xstrdup(group);
  934. write_kprobe_trace_event(fd, tev);
  935. /* Add added event name to namelist */
  936. strlist__add(namelist, event);
  937. /* Trick here - save current event/group */
  938. event = pev->event;
  939. group = pev->group;
  940. pev->event = tev->event;
  941. pev->group = tev->group;
  942. show_perf_probe_event(pev);
  943. /* Trick here - restore current event/group */
  944. pev->event = (char *)event;
  945. pev->group = (char *)group;
  946. /*
  947. * Probes after the first probe which comes from same
  948. * user input are always allowed to add suffix, because
  949. * there might be several addresses corresponding to
  950. * one code line.
  951. */
  952. allow_suffix = true;
  953. }
  954. /* Show how to use the event. */
  955. printf("\nYou can now use it on all perf tools, such as:\n\n");
  956. printf("\tperf record -e %s:%s -a sleep 1\n\n", tev->group, tev->event);
  957. strlist__delete(namelist);
  958. close(fd);
  959. }
  960. static int convert_to_kprobe_trace_events(struct perf_probe_event *pev,
  961. struct kprobe_trace_event **tevs)
  962. {
  963. struct symbol *sym;
  964. int ntevs = 0, i;
  965. struct kprobe_trace_event *tev;
  966. /* Convert perf_probe_event with debuginfo */
  967. ntevs = try_to_find_kprobe_trace_events(pev, tevs);
  968. if (ntevs > 0)
  969. return ntevs;
  970. /* Allocate trace event buffer */
  971. ntevs = 1;
  972. tev = *tevs = xzalloc(sizeof(struct kprobe_trace_event));
  973. /* Copy parameters */
  974. tev->point.symbol = xstrdup(pev->point.function);
  975. tev->point.offset = pev->point.offset;
  976. tev->nargs = pev->nargs;
  977. if (tev->nargs) {
  978. tev->args = xzalloc(sizeof(struct kprobe_trace_arg)
  979. * tev->nargs);
  980. for (i = 0; i < tev->nargs; i++) {
  981. if (pev->args[i].name)
  982. tev->args[i].name = xstrdup(pev->args[i].name);
  983. tev->args[i].value = xstrdup(pev->args[i].var);
  984. }
  985. }
  986. /* Currently just checking function name from symbol map */
  987. sym = map__find_symbol_by_name(kmaps[MAP__FUNCTION],
  988. tev->point.symbol, NULL);
  989. if (!sym)
  990. die("Kernel symbol \'%s\' not found - probe not added.",
  991. tev->point.symbol);
  992. return ntevs;
  993. }
  994. struct __event_package {
  995. struct perf_probe_event *pev;
  996. struct kprobe_trace_event *tevs;
  997. int ntevs;
  998. };
  999. void add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
  1000. bool force_add)
  1001. {
  1002. int i;
  1003. struct __event_package *pkgs;
  1004. pkgs = xzalloc(sizeof(struct __event_package) * npevs);
  1005. /* Init vmlinux path */
  1006. init_vmlinux();
  1007. /* Loop 1: convert all events */
  1008. for (i = 0; i < npevs; i++) {
  1009. pkgs[i].pev = &pevs[i];
  1010. /* Convert with or without debuginfo */
  1011. pkgs[i].ntevs = convert_to_kprobe_trace_events(pkgs[i].pev,
  1012. &pkgs[i].tevs);
  1013. }
  1014. /* Loop 2: add all events */
  1015. for (i = 0; i < npevs; i++)
  1016. __add_kprobe_trace_events(pkgs[i].pev, pkgs[i].tevs,
  1017. pkgs[i].ntevs, force_add);
  1018. /* TODO: cleanup all trace events? */
  1019. }
  1020. static void __del_trace_kprobe_event(int fd, struct str_node *ent)
  1021. {
  1022. char *p;
  1023. char buf[128];
  1024. int ret;
  1025. /* Convert from perf-probe event to trace-kprobe event */
  1026. if (e_snprintf(buf, 128, "-:%s", ent->s) < 0)
  1027. die("Failed to copy event.");
  1028. p = strchr(buf + 2, ':');
  1029. if (!p)
  1030. die("Internal error: %s should have ':' but not.", ent->s);
  1031. *p = '/';
  1032. pr_debug("Writing event: %s\n", buf);
  1033. ret = write(fd, buf, strlen(buf));
  1034. if (ret <= 0)
  1035. die("Failed to write event: %s", strerror(errno));
  1036. printf("Remove event: %s\n", ent->s);
  1037. }
  1038. static void del_trace_kprobe_event(int fd, const char *group,
  1039. const char *event, struct strlist *namelist)
  1040. {
  1041. char buf[128];
  1042. struct str_node *ent, *n;
  1043. int found = 0;
  1044. if (e_snprintf(buf, 128, "%s:%s", group, event) < 0)
  1045. die("Failed to copy event.");
  1046. if (strpbrk(buf, "*?")) { /* Glob-exp */
  1047. strlist__for_each_safe(ent, n, namelist)
  1048. if (strglobmatch(ent->s, buf)) {
  1049. found++;
  1050. __del_trace_kprobe_event(fd, ent);
  1051. strlist__remove(namelist, ent);
  1052. }
  1053. } else {
  1054. ent = strlist__find(namelist, buf);
  1055. if (ent) {
  1056. found++;
  1057. __del_trace_kprobe_event(fd, ent);
  1058. strlist__remove(namelist, ent);
  1059. }
  1060. }
  1061. if (found == 0)
  1062. pr_info("Info: event \"%s\" does not exist, could not remove it.\n", buf);
  1063. }
  1064. void del_perf_probe_events(struct strlist *dellist)
  1065. {
  1066. int fd;
  1067. const char *group, *event;
  1068. char *p, *str;
  1069. struct str_node *ent;
  1070. struct strlist *namelist;
  1071. fd = open_kprobe_events(true);
  1072. /* Get current event names */
  1073. namelist = get_kprobe_trace_event_names(fd, true);
  1074. strlist__for_each(ent, dellist) {
  1075. str = xstrdup(ent->s);
  1076. pr_debug("Parsing: %s\n", str);
  1077. p = strchr(str, ':');
  1078. if (p) {
  1079. group = str;
  1080. *p = '\0';
  1081. event = p + 1;
  1082. } else {
  1083. group = "*";
  1084. event = str;
  1085. }
  1086. pr_debug("Group: %s, Event: %s\n", group, event);
  1087. del_trace_kprobe_event(fd, group, event, namelist);
  1088. free(str);
  1089. }
  1090. strlist__delete(namelist);
  1091. close(fd);
  1092. }