probe-event.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  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 command */
  238. void parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
  239. {
  240. char **argv;
  241. int argc, i;
  242. argv = argv_split(cmd, &argc);
  243. if (!argv)
  244. die("argv_split failed.");
  245. if (argc > MAX_PROBE_ARGS + 1)
  246. semantic_error("Too many arguments");
  247. /* Parse probe point */
  248. parse_perf_probe_point(argv[0], pev);
  249. /* Copy arguments and ensure return probe has no C argument */
  250. pev->nargs = argc - 1;
  251. pev->args = xzalloc(sizeof(struct perf_probe_arg) * pev->nargs);
  252. for (i = 0; i < pev->nargs; i++) {
  253. pev->args[i].name = xstrdup(argv[i + 1]);
  254. if (is_c_varname(pev->args[i].name) && pev->point.retprobe)
  255. semantic_error("You can't specify local variable for"
  256. " kretprobe");
  257. }
  258. argv_free(argv);
  259. }
  260. /* Return true if this perf_probe_event requires debuginfo */
  261. bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
  262. {
  263. int i;
  264. if (pev->point.file || pev->point.line || pev->point.lazy_line)
  265. return true;
  266. for (i = 0; i < pev->nargs; i++)
  267. if (is_c_varname(pev->args[i].name))
  268. return true;
  269. return false;
  270. }
  271. /* Parse kprobe_events event into struct probe_point */
  272. void parse_kprobe_trace_command(const char *cmd, struct kprobe_trace_event *tev)
  273. {
  274. struct kprobe_trace_point *tp = &tev->point;
  275. char pr;
  276. char *p;
  277. int ret, i, argc;
  278. char **argv;
  279. pr_debug("Parsing kprobe_events: %s\n", cmd);
  280. argv = argv_split(cmd, &argc);
  281. if (!argv)
  282. die("argv_split failed.");
  283. if (argc < 2)
  284. semantic_error("Too less arguments.");
  285. /* Scan event and group name. */
  286. ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]",
  287. &pr, (float *)(void *)&tev->group,
  288. (float *)(void *)&tev->event);
  289. if (ret != 3)
  290. semantic_error("Failed to parse event name: %s", argv[0]);
  291. pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
  292. tp->retprobe = (pr == 'r');
  293. /* Scan function name and offset */
  294. ret = sscanf(argv[1], "%a[^+]+%lu", (float *)(void *)&tp->symbol,
  295. &tp->offset);
  296. if (ret == 1)
  297. tp->offset = 0;
  298. tev->nargs = argc - 2;
  299. tev->args = xzalloc(sizeof(struct kprobe_trace_arg) * tev->nargs);
  300. for (i = 0; i < tev->nargs; i++) {
  301. p = strchr(argv[i + 2], '=');
  302. if (p) /* We don't need which register is assigned. */
  303. *p++ = '\0';
  304. else
  305. p = argv[i + 2];
  306. tev->args[i].name = xstrdup(argv[i + 2]);
  307. /* TODO: parse regs and offset */
  308. tev->args[i].value = xstrdup(p);
  309. }
  310. argv_free(argv);
  311. }
  312. /* Compose only probe point (not argument) */
  313. static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
  314. {
  315. char *buf;
  316. char offs[64] = "", line[64] = "";
  317. int ret;
  318. buf = xzalloc(MAX_CMDLEN);
  319. if (pp->offset) {
  320. ret = e_snprintf(offs, 64, "+%lu", pp->offset);
  321. if (ret <= 0)
  322. goto error;
  323. }
  324. if (pp->line) {
  325. ret = e_snprintf(line, 64, ":%d", pp->line);
  326. if (ret <= 0)
  327. goto error;
  328. }
  329. if (pp->function)
  330. ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s", pp->function,
  331. offs, pp->retprobe ? "%return" : "", line);
  332. else
  333. ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", pp->file, line);
  334. if (ret <= 0)
  335. goto error;
  336. return buf;
  337. error:
  338. die("Failed to synthesize perf probe point: %s", strerror(-ret));
  339. }
  340. #if 0
  341. char *synthesize_perf_probe_command(struct perf_probe_event *pev)
  342. {
  343. char *buf;
  344. int i, len, ret;
  345. buf = synthesize_perf_probe_point(&pev->point);
  346. if (!buf)
  347. return NULL;
  348. len = strlen(buf);
  349. for (i = 0; i < pev->nargs; i++) {
  350. ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
  351. pev->args[i].name);
  352. if (ret <= 0) {
  353. free(buf);
  354. return NULL;
  355. }
  356. len += ret;
  357. }
  358. return buf;
  359. }
  360. #endif
  361. static int __synthesize_kprobe_trace_arg_ref(struct kprobe_trace_arg_ref *ref,
  362. char **buf, size_t *buflen,
  363. int depth)
  364. {
  365. int ret;
  366. if (ref->next) {
  367. depth = __synthesize_kprobe_trace_arg_ref(ref->next, buf,
  368. buflen, depth + 1);
  369. if (depth < 0)
  370. goto out;
  371. }
  372. ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
  373. if (ret < 0)
  374. depth = ret;
  375. else {
  376. *buf += ret;
  377. *buflen -= ret;
  378. }
  379. out:
  380. return depth;
  381. }
  382. static int synthesize_kprobe_trace_arg(struct kprobe_trace_arg *arg,
  383. char *buf, size_t buflen)
  384. {
  385. int ret, depth = 0;
  386. char *tmp = buf;
  387. /* Argument name or separator */
  388. if (arg->name)
  389. ret = e_snprintf(buf, buflen, " %s=", arg->name);
  390. else
  391. ret = e_snprintf(buf, buflen, " ");
  392. if (ret < 0)
  393. return ret;
  394. buf += ret;
  395. buflen -= ret;
  396. /* Dereferencing arguments */
  397. if (arg->ref) {
  398. depth = __synthesize_kprobe_trace_arg_ref(arg->ref, &buf,
  399. &buflen, 1);
  400. if (depth < 0)
  401. return depth;
  402. }
  403. /* Print argument value */
  404. ret = e_snprintf(buf, buflen, "%s", arg->value);
  405. if (ret < 0)
  406. return ret;
  407. buf += ret;
  408. buflen -= ret;
  409. /* Closing */
  410. while (depth--) {
  411. ret = e_snprintf(buf, buflen, ")");
  412. if (ret < 0)
  413. return ret;
  414. buf += ret;
  415. buflen -= ret;
  416. }
  417. return buf - tmp;
  418. }
  419. char *synthesize_kprobe_trace_command(struct kprobe_trace_event *tev)
  420. {
  421. struct kprobe_trace_point *tp = &tev->point;
  422. char *buf;
  423. int i, len, ret;
  424. buf = xzalloc(MAX_CMDLEN);
  425. len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s+%lu",
  426. tp->retprobe ? 'r' : 'p',
  427. tev->group, tev->event,
  428. tp->symbol, tp->offset);
  429. if (len <= 0)
  430. goto error;
  431. for (i = 0; i < tev->nargs; i++) {
  432. ret = synthesize_kprobe_trace_arg(&tev->args[i], buf + len,
  433. MAX_CMDLEN - len);
  434. if (ret <= 0)
  435. goto error;
  436. len += ret;
  437. }
  438. return buf;
  439. error:
  440. free(buf);
  441. return NULL;
  442. }
  443. void convert_to_perf_probe_event(struct kprobe_trace_event *tev,
  444. struct perf_probe_event *pev)
  445. {
  446. char buf[64];
  447. int i;
  448. pev->event = xstrdup(tev->event);
  449. pev->group = xstrdup(tev->group);
  450. /* Convert trace_point to probe_point */
  451. pev->point.function = xstrdup(tev->point.symbol);
  452. pev->point.offset = tev->point.offset;
  453. pev->point.retprobe = tev->point.retprobe;
  454. /* Convert trace_arg to probe_arg */
  455. pev->nargs = tev->nargs;
  456. pev->args = xzalloc(sizeof(struct perf_probe_arg) * pev->nargs);
  457. for (i = 0; i < tev->nargs; i++)
  458. if (tev->args[i].name)
  459. pev->args[i].name = xstrdup(tev->args[i].name);
  460. else {
  461. synthesize_kprobe_trace_arg(&tev->args[i], buf, 64);
  462. pev->args[i].name = xstrdup(buf);
  463. }
  464. }
  465. void clear_perf_probe_event(struct perf_probe_event *pev)
  466. {
  467. struct perf_probe_point *pp = &pev->point;
  468. int i;
  469. if (pev->event)
  470. free(pev->event);
  471. if (pev->group)
  472. free(pev->group);
  473. if (pp->file)
  474. free(pp->file);
  475. if (pp->function)
  476. free(pp->function);
  477. if (pp->lazy_line)
  478. free(pp->lazy_line);
  479. for (i = 0; i < pev->nargs; i++)
  480. if (pev->args[i].name)
  481. free(pev->args[i].name);
  482. if (pev->args)
  483. free(pev->args);
  484. memset(pev, 0, sizeof(*pev));
  485. }
  486. void clear_kprobe_trace_event(struct kprobe_trace_event *tev)
  487. {
  488. struct kprobe_trace_arg_ref *ref, *next;
  489. int i;
  490. if (tev->event)
  491. free(tev->event);
  492. if (tev->group)
  493. free(tev->group);
  494. if (tev->point.symbol)
  495. free(tev->point.symbol);
  496. for (i = 0; i < tev->nargs; i++) {
  497. if (tev->args[i].name)
  498. free(tev->args[i].name);
  499. if (tev->args[i].value)
  500. free(tev->args[i].value);
  501. ref = tev->args[i].ref;
  502. while (ref) {
  503. next = ref->next;
  504. free(ref);
  505. ref = next;
  506. }
  507. }
  508. if (tev->args)
  509. free(tev->args);
  510. memset(tev, 0, sizeof(*tev));
  511. }
  512. static int open_kprobe_events(bool readwrite)
  513. {
  514. char buf[PATH_MAX];
  515. int ret;
  516. ret = e_snprintf(buf, PATH_MAX, "%s/../kprobe_events", debugfs_path);
  517. if (ret < 0)
  518. die("Failed to make kprobe_events path.");
  519. if (readwrite && !probe_event_dry_run)
  520. ret = open(buf, O_RDWR, O_APPEND);
  521. else
  522. ret = open(buf, O_RDONLY, 0);
  523. if (ret < 0) {
  524. if (errno == ENOENT)
  525. die("kprobe_events file does not exist -"
  526. " please rebuild with CONFIG_KPROBE_EVENT.");
  527. else
  528. die("Could not open kprobe_events file: %s",
  529. strerror(errno));
  530. }
  531. return ret;
  532. }
  533. /* Get raw string list of current kprobe_events */
  534. static struct strlist *get_kprobe_trace_command_rawlist(int fd)
  535. {
  536. int ret, idx;
  537. FILE *fp;
  538. char buf[MAX_CMDLEN];
  539. char *p;
  540. struct strlist *sl;
  541. sl = strlist__new(true, NULL);
  542. fp = fdopen(dup(fd), "r");
  543. while (!feof(fp)) {
  544. p = fgets(buf, MAX_CMDLEN, fp);
  545. if (!p)
  546. break;
  547. idx = strlen(p) - 1;
  548. if (p[idx] == '\n')
  549. p[idx] = '\0';
  550. ret = strlist__add(sl, buf);
  551. if (ret < 0)
  552. die("strlist__add failed: %s", strerror(-ret));
  553. }
  554. fclose(fp);
  555. return sl;
  556. }
  557. /* Show an event */
  558. static void show_perf_probe_event(struct perf_probe_event *pev)
  559. {
  560. int i, ret;
  561. char buf[128];
  562. char *place;
  563. /* Synthesize only event probe point */
  564. place = synthesize_perf_probe_point(&pev->point);
  565. ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
  566. if (ret < 0)
  567. die("Failed to copy event: %s", strerror(-ret));
  568. printf(" %-40s (on %s", buf, place);
  569. if (pev->nargs > 0) {
  570. printf(" with");
  571. for (i = 0; i < pev->nargs; i++)
  572. printf(" %s", pev->args[i].name);
  573. }
  574. printf(")\n");
  575. free(place);
  576. }
  577. /* List up current perf-probe events */
  578. void show_perf_probe_events(void)
  579. {
  580. int fd;
  581. struct kprobe_trace_event tev;
  582. struct perf_probe_event pev;
  583. struct strlist *rawlist;
  584. struct str_node *ent;
  585. setup_pager();
  586. memset(&tev, 0, sizeof(tev));
  587. memset(&pev, 0, sizeof(pev));
  588. fd = open_kprobe_events(false);
  589. rawlist = get_kprobe_trace_command_rawlist(fd);
  590. close(fd);
  591. strlist__for_each(ent, rawlist) {
  592. parse_kprobe_trace_command(ent->s, &tev);
  593. convert_to_perf_probe_event(&tev, &pev);
  594. /* Show an event */
  595. show_perf_probe_event(&pev);
  596. clear_perf_probe_event(&pev);
  597. clear_kprobe_trace_event(&tev);
  598. }
  599. strlist__delete(rawlist);
  600. }
  601. /* Get current perf-probe event names */
  602. static struct strlist *get_kprobe_trace_event_names(int fd, bool include_group)
  603. {
  604. char buf[128];
  605. struct strlist *sl, *rawlist;
  606. struct str_node *ent;
  607. struct kprobe_trace_event tev;
  608. memset(&tev, 0, sizeof(tev));
  609. rawlist = get_kprobe_trace_command_rawlist(fd);
  610. sl = strlist__new(true, NULL);
  611. strlist__for_each(ent, rawlist) {
  612. parse_kprobe_trace_command(ent->s, &tev);
  613. if (include_group) {
  614. if (e_snprintf(buf, 128, "%s:%s", tev.group,
  615. tev.event) < 0)
  616. die("Failed to copy group:event name.");
  617. strlist__add(sl, buf);
  618. } else
  619. strlist__add(sl, tev.event);
  620. clear_kprobe_trace_event(&tev);
  621. }
  622. strlist__delete(rawlist);
  623. return sl;
  624. }
  625. static void write_kprobe_trace_event(int fd, struct kprobe_trace_event *tev)
  626. {
  627. int ret;
  628. char *buf = synthesize_kprobe_trace_command(tev);
  629. pr_debug("Writing event: %s\n", buf);
  630. if (!probe_event_dry_run) {
  631. ret = write(fd, buf, strlen(buf));
  632. if (ret <= 0)
  633. die("Failed to write event: %s", strerror(errno));
  634. }
  635. free(buf);
  636. }
  637. static void get_new_event_name(char *buf, size_t len, const char *base,
  638. struct strlist *namelist, bool allow_suffix)
  639. {
  640. int i, ret;
  641. /* Try no suffix */
  642. ret = e_snprintf(buf, len, "%s", base);
  643. if (ret < 0)
  644. die("snprintf() failed: %s", strerror(-ret));
  645. if (!strlist__has_entry(namelist, buf))
  646. return;
  647. if (!allow_suffix) {
  648. pr_warning("Error: event \"%s\" already exists. "
  649. "(Use -f to force duplicates.)\n", base);
  650. die("Can't add new event.");
  651. }
  652. /* Try to add suffix */
  653. for (i = 1; i < MAX_EVENT_INDEX; i++) {
  654. ret = e_snprintf(buf, len, "%s_%d", base, i);
  655. if (ret < 0)
  656. die("snprintf() failed: %s", strerror(-ret));
  657. if (!strlist__has_entry(namelist, buf))
  658. break;
  659. }
  660. if (i == MAX_EVENT_INDEX)
  661. die("Too many events are on the same function.");
  662. }
  663. static void __add_kprobe_trace_events(struct perf_probe_event *pev,
  664. struct kprobe_trace_event *tevs,
  665. int ntevs, bool allow_suffix)
  666. {
  667. int i, fd;
  668. struct kprobe_trace_event *tev;
  669. char buf[64];
  670. const char *event, *group;
  671. struct strlist *namelist;
  672. fd = open_kprobe_events(true);
  673. /* Get current event names */
  674. namelist = get_kprobe_trace_event_names(fd, false);
  675. printf("Add new event%s\n", (ntevs > 1) ? "s:" : ":");
  676. for (i = 0; i < ntevs; i++) {
  677. tev = &tevs[i];
  678. if (pev->event)
  679. event = pev->event;
  680. else
  681. if (pev->point.function)
  682. event = pev->point.function;
  683. else
  684. event = tev->point.symbol;
  685. if (pev->group)
  686. group = pev->group;
  687. else
  688. group = PERFPROBE_GROUP;
  689. /* Get an unused new event name */
  690. get_new_event_name(buf, 64, event, namelist, allow_suffix);
  691. event = buf;
  692. tev->event = xstrdup(event);
  693. tev->group = xstrdup(group);
  694. write_kprobe_trace_event(fd, tev);
  695. /* Add added event name to namelist */
  696. strlist__add(namelist, event);
  697. /* Trick here - save current event/group */
  698. event = pev->event;
  699. group = pev->group;
  700. pev->event = tev->event;
  701. pev->group = tev->group;
  702. show_perf_probe_event(pev);
  703. /* Trick here - restore current event/group */
  704. pev->event = (char *)event;
  705. pev->group = (char *)group;
  706. /*
  707. * Probes after the first probe which comes from same
  708. * user input are always allowed to add suffix, because
  709. * there might be several addresses corresponding to
  710. * one code line.
  711. */
  712. allow_suffix = true;
  713. }
  714. /* Show how to use the event. */
  715. printf("\nYou can now use it on all perf tools, such as:\n\n");
  716. printf("\tperf record -e %s:%s -a sleep 1\n\n", tev->group, tev->event);
  717. strlist__delete(namelist);
  718. close(fd);
  719. }
  720. static int convert_to_kprobe_trace_events(struct perf_probe_event *pev,
  721. struct kprobe_trace_event **tevs)
  722. {
  723. struct symbol *sym;
  724. bool need_dwarf;
  725. #ifndef NO_DWARF_SUPPORT
  726. int fd;
  727. #endif
  728. int ntevs = 0, i;
  729. struct kprobe_trace_event *tev;
  730. need_dwarf = perf_probe_event_need_dwarf(pev);
  731. if (need_dwarf)
  732. #ifdef NO_DWARF_SUPPORT
  733. die("Debuginfo-analysis is not supported");
  734. #else /* !NO_DWARF_SUPPORT */
  735. pr_debug("Some probes require debuginfo.\n");
  736. fd = open_vmlinux();
  737. if (fd < 0) {
  738. if (need_dwarf)
  739. die("Could not open debuginfo file.");
  740. pr_debug("Could not open vmlinux/module file."
  741. " Try to use symbols.\n");
  742. goto end_dwarf;
  743. }
  744. /* Searching probe points */
  745. ntevs = find_kprobe_trace_events(fd, pev, tevs);
  746. if (ntevs > 0) /* Found */
  747. goto found;
  748. if (ntevs == 0) /* No error but failed to find probe point. */
  749. die("Probe point '%s' not found. - probe not added.",
  750. synthesize_perf_probe_point(&pev->point));
  751. /* Error path */
  752. if (need_dwarf) {
  753. if (ntevs == -ENOENT)
  754. pr_warning("No dwarf info found in the vmlinux - please rebuild with CONFIG_DEBUG_INFO=y.\n");
  755. die("Could not analyze debuginfo.");
  756. }
  757. pr_debug("An error occurred in debuginfo analysis."
  758. " Try to use symbols.\n");
  759. end_dwarf:
  760. #endif /* !NO_DWARF_SUPPORT */
  761. /* Allocate trace event buffer */
  762. ntevs = 1;
  763. tev = *tevs = xzalloc(sizeof(struct kprobe_trace_event));
  764. /* Copy parameters */
  765. tev->point.symbol = xstrdup(pev->point.function);
  766. tev->point.offset = pev->point.offset;
  767. tev->nargs = pev->nargs;
  768. if (tev->nargs) {
  769. tev->args = xzalloc(sizeof(struct kprobe_trace_arg)
  770. * tev->nargs);
  771. for (i = 0; i < tev->nargs; i++)
  772. tev->args[i].value = xstrdup(pev->args[i].name);
  773. }
  774. /* Currently just checking function name from symbol map */
  775. sym = map__find_symbol_by_name(kmaps[MAP__FUNCTION],
  776. tev->point.symbol, NULL);
  777. if (!sym)
  778. die("Kernel symbol \'%s\' not found - probe not added.",
  779. tev->point.symbol);
  780. found:
  781. close(fd);
  782. return ntevs;
  783. }
  784. struct __event_package {
  785. struct perf_probe_event *pev;
  786. struct kprobe_trace_event *tevs;
  787. int ntevs;
  788. };
  789. void add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
  790. bool force_add)
  791. {
  792. int i;
  793. struct __event_package *pkgs;
  794. pkgs = xzalloc(sizeof(struct __event_package) * npevs);
  795. /* Init vmlinux path */
  796. init_vmlinux();
  797. /* Loop 1: convert all events */
  798. for (i = 0; i < npevs; i++) {
  799. pkgs[i].pev = &pevs[i];
  800. /* Convert with or without debuginfo */
  801. pkgs[i].ntevs = convert_to_kprobe_trace_events(pkgs[i].pev,
  802. &pkgs[i].tevs);
  803. }
  804. /* Loop 2: add all events */
  805. for (i = 0; i < npevs; i++)
  806. __add_kprobe_trace_events(pkgs[i].pev, pkgs[i].tevs,
  807. pkgs[i].ntevs, force_add);
  808. /* TODO: cleanup all trace events? */
  809. }
  810. static void __del_trace_kprobe_event(int fd, struct str_node *ent)
  811. {
  812. char *p;
  813. char buf[128];
  814. int ret;
  815. /* Convert from perf-probe event to trace-kprobe event */
  816. if (e_snprintf(buf, 128, "-:%s", ent->s) < 0)
  817. die("Failed to copy event.");
  818. p = strchr(buf + 2, ':');
  819. if (!p)
  820. die("Internal error: %s should have ':' but not.", ent->s);
  821. *p = '/';
  822. pr_debug("Writing event: %s\n", buf);
  823. ret = write(fd, buf, strlen(buf));
  824. if (ret <= 0)
  825. die("Failed to write event: %s", strerror(errno));
  826. printf("Remove event: %s\n", ent->s);
  827. }
  828. static void del_trace_kprobe_event(int fd, const char *group,
  829. const char *event, struct strlist *namelist)
  830. {
  831. char buf[128];
  832. struct str_node *ent, *n;
  833. int found = 0;
  834. if (e_snprintf(buf, 128, "%s:%s", group, event) < 0)
  835. die("Failed to copy event.");
  836. if (strpbrk(buf, "*?")) { /* Glob-exp */
  837. strlist__for_each_safe(ent, n, namelist)
  838. if (strglobmatch(ent->s, buf)) {
  839. found++;
  840. __del_trace_kprobe_event(fd, ent);
  841. strlist__remove(namelist, ent);
  842. }
  843. } else {
  844. ent = strlist__find(namelist, buf);
  845. if (ent) {
  846. found++;
  847. __del_trace_kprobe_event(fd, ent);
  848. strlist__remove(namelist, ent);
  849. }
  850. }
  851. if (found == 0)
  852. pr_info("Info: event \"%s\" does not exist, could not remove it.\n", buf);
  853. }
  854. void del_perf_probe_events(struct strlist *dellist)
  855. {
  856. int fd;
  857. const char *group, *event;
  858. char *p, *str;
  859. struct str_node *ent;
  860. struct strlist *namelist;
  861. fd = open_kprobe_events(true);
  862. /* Get current event names */
  863. namelist = get_kprobe_trace_event_names(fd, true);
  864. strlist__for_each(ent, dellist) {
  865. str = xstrdup(ent->s);
  866. pr_debug("Parsing: %s\n", str);
  867. p = strchr(str, ':');
  868. if (p) {
  869. group = str;
  870. *p = '\0';
  871. event = p + 1;
  872. } else {
  873. group = "*";
  874. event = str;
  875. }
  876. pr_debug("Group: %s, Event: %s\n", group, event);
  877. del_trace_kprobe_event(fd, group, event, namelist);
  878. free(str);
  879. }
  880. strlist__delete(namelist);
  881. close(fd);
  882. }
  883. #define LINEBUF_SIZE 256
  884. #define NR_ADDITIONAL_LINES 2
  885. static void show_one_line(FILE *fp, unsigned int l, bool skip, bool show_num)
  886. {
  887. char buf[LINEBUF_SIZE];
  888. const char *color = PERF_COLOR_BLUE;
  889. if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
  890. goto error;
  891. if (!skip) {
  892. if (show_num)
  893. fprintf(stdout, "%7u %s", l, buf);
  894. else
  895. color_fprintf(stdout, color, " %s", buf);
  896. }
  897. while (strlen(buf) == LINEBUF_SIZE - 1 &&
  898. buf[LINEBUF_SIZE - 2] != '\n') {
  899. if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
  900. goto error;
  901. if (!skip) {
  902. if (show_num)
  903. fprintf(stdout, "%s", buf);
  904. else
  905. color_fprintf(stdout, color, "%s", buf);
  906. }
  907. }
  908. return;
  909. error:
  910. if (feof(fp))
  911. die("Source file is shorter than expected.");
  912. else
  913. die("File read error: %s", strerror(errno));
  914. }
  915. void show_line_range(struct line_range *lr)
  916. {
  917. unsigned int l = 1;
  918. struct line_node *ln;
  919. FILE *fp;
  920. int fd, ret;
  921. /* Search a line range */
  922. init_vmlinux();
  923. fd = open_vmlinux();
  924. if (fd < 0)
  925. die("Could not open debuginfo file.");
  926. ret = find_line_range(fd, lr);
  927. if (ret <= 0)
  928. die("Source line is not found.\n");
  929. close(fd);
  930. setup_pager();
  931. if (lr->function)
  932. fprintf(stdout, "<%s:%d>\n", lr->function,
  933. lr->start - lr->offset);
  934. else
  935. fprintf(stdout, "<%s:%d>\n", lr->file, lr->start);
  936. fp = fopen(lr->path, "r");
  937. if (fp == NULL)
  938. die("Failed to open %s: %s", lr->path, strerror(errno));
  939. /* Skip to starting line number */
  940. while (l < lr->start)
  941. show_one_line(fp, l++, true, false);
  942. list_for_each_entry(ln, &lr->line_list, list) {
  943. while (ln->line > l)
  944. show_one_line(fp, (l++) - lr->offset, false, false);
  945. show_one_line(fp, (l++) - lr->offset, false, true);
  946. }
  947. if (lr->end == INT_MAX)
  948. lr->end = l + NR_ADDITIONAL_LINES;
  949. while (l < lr->end && !feof(fp))
  950. show_one_line(fp, (l++) - lr->offset, false, false);
  951. fclose(fp);
  952. }