probe-event.c 28 KB

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