probe-event.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  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 "event.h"
  35. #include "string.h"
  36. #include "strlist.h"
  37. #include "debug.h"
  38. #include "cache.h"
  39. #include "parse-events.h" /* For debugfs_path */
  40. #include "probe-event.h"
  41. #define MAX_CMDLEN 256
  42. #define MAX_PROBE_ARGS 128
  43. #define PERFPROBE_GROUP "probe"
  44. #define semantic_error(msg ...) die("Semantic error :" msg)
  45. /* If there is no space to write, returns -E2BIG. */
  46. static int e_snprintf(char *str, size_t size, const char *format, ...)
  47. __attribute__((format(printf, 3, 4)));
  48. static int e_snprintf(char *str, size_t size, const char *format, ...)
  49. {
  50. int ret;
  51. va_list ap;
  52. va_start(ap, format);
  53. ret = vsnprintf(str, size, format, ap);
  54. va_end(ap);
  55. if (ret >= (int)size)
  56. ret = -E2BIG;
  57. return ret;
  58. }
  59. /* Check the name is good for event/group */
  60. static bool check_event_name(const char *name)
  61. {
  62. if (!isalpha(*name) && *name != '_')
  63. return false;
  64. while (*++name != '\0') {
  65. if (!isalpha(*name) && !isdigit(*name) && *name != '_')
  66. return false;
  67. }
  68. return true;
  69. }
  70. /* Parse probepoint definition. */
  71. static void parse_perf_probe_probepoint(char *arg, struct probe_point *pp)
  72. {
  73. char *ptr, *tmp;
  74. char c, nc = 0;
  75. /*
  76. * <Syntax>
  77. * perf probe [EVENT=]SRC:LN
  78. * perf probe [EVENT=]FUNC[+OFFS|%return][@SRC]
  79. *
  80. * TODO:Group name support
  81. */
  82. ptr = strchr(arg, '=');
  83. if (ptr) { /* Event name */
  84. *ptr = '\0';
  85. tmp = ptr + 1;
  86. ptr = strchr(arg, ':');
  87. if (ptr) /* Group name is not supported yet. */
  88. semantic_error("Group name is not supported yet.");
  89. if (!check_event_name(arg))
  90. semantic_error("%s is bad for event name -it must "
  91. "follow C symbol-naming rule.", arg);
  92. pp->event = strdup(arg);
  93. arg = tmp;
  94. }
  95. ptr = strpbrk(arg, ":+@%");
  96. if (ptr) {
  97. nc = *ptr;
  98. *ptr++ = '\0';
  99. }
  100. /* Check arg is function or file and copy it */
  101. if (strchr(arg, '.')) /* File */
  102. pp->file = strdup(arg);
  103. else /* Function */
  104. pp->function = strdup(arg);
  105. DIE_IF(pp->file == NULL && pp->function == NULL);
  106. /* Parse other options */
  107. while (ptr) {
  108. arg = ptr;
  109. c = nc;
  110. ptr = strpbrk(arg, ":+@%");
  111. if (ptr) {
  112. nc = *ptr;
  113. *ptr++ = '\0';
  114. }
  115. switch (c) {
  116. case ':': /* Line number */
  117. pp->line = strtoul(arg, &tmp, 0);
  118. if (*tmp != '\0')
  119. semantic_error("There is non-digit charactor"
  120. " in line number.");
  121. break;
  122. case '+': /* Byte offset from a symbol */
  123. pp->offset = strtoul(arg, &tmp, 0);
  124. if (*tmp != '\0')
  125. semantic_error("There is non-digit charactor"
  126. " in offset.");
  127. break;
  128. case '@': /* File name */
  129. if (pp->file)
  130. semantic_error("SRC@SRC is not allowed.");
  131. pp->file = strdup(arg);
  132. DIE_IF(pp->file == NULL);
  133. if (ptr)
  134. semantic_error("@SRC must be the last "
  135. "option.");
  136. break;
  137. case '%': /* Probe places */
  138. if (strcmp(arg, "return") == 0) {
  139. pp->retprobe = 1;
  140. } else /* Others not supported yet */
  141. semantic_error("%%%s is not supported.", arg);
  142. break;
  143. default:
  144. DIE_IF("Program has a bug.");
  145. break;
  146. }
  147. }
  148. /* Exclusion check */
  149. if (pp->line && pp->offset)
  150. semantic_error("Offset can't be used with line number.");
  151. if (!pp->line && pp->file && !pp->function)
  152. semantic_error("File always requires line number.");
  153. if (pp->offset && !pp->function)
  154. semantic_error("Offset requires an entry function.");
  155. if (pp->retprobe && !pp->function)
  156. semantic_error("Return probe requires an entry function.");
  157. if ((pp->offset || pp->line) && pp->retprobe)
  158. semantic_error("Offset/Line can't be used with return probe.");
  159. pr_debug("symbol:%s file:%s line:%d offset:%d, return:%d\n",
  160. pp->function, pp->file, pp->line, pp->offset, pp->retprobe);
  161. }
  162. /* Parse perf-probe event definition */
  163. void parse_perf_probe_event(const char *str, struct probe_point *pp,
  164. bool *need_dwarf)
  165. {
  166. char **argv;
  167. int argc, i;
  168. *need_dwarf = false;
  169. argv = argv_split(str, &argc);
  170. if (!argv)
  171. die("argv_split failed.");
  172. if (argc > MAX_PROBE_ARGS + 1)
  173. semantic_error("Too many arguments");
  174. /* Parse probe point */
  175. parse_perf_probe_probepoint(argv[0], pp);
  176. if (pp->file || pp->line)
  177. *need_dwarf = true;
  178. /* Copy arguments and ensure return probe has no C argument */
  179. pp->nr_args = argc - 1;
  180. pp->args = zalloc(sizeof(char *) * pp->nr_args);
  181. for (i = 0; i < pp->nr_args; i++) {
  182. pp->args[i] = strdup(argv[i + 1]);
  183. if (!pp->args[i])
  184. die("Failed to copy argument.");
  185. if (is_c_varname(pp->args[i])) {
  186. if (pp->retprobe)
  187. semantic_error("You can't specify local"
  188. " variable for kretprobe");
  189. *need_dwarf = true;
  190. }
  191. }
  192. argv_free(argv);
  193. }
  194. /* Parse kprobe_events event into struct probe_point */
  195. void parse_trace_kprobe_event(const char *str, struct probe_point *pp)
  196. {
  197. char pr;
  198. char *p;
  199. int ret, i, argc;
  200. char **argv;
  201. pr_debug("Parsing kprobe_events: %s\n", str);
  202. argv = argv_split(str, &argc);
  203. if (!argv)
  204. die("argv_split failed.");
  205. if (argc < 2)
  206. semantic_error("Too less arguments.");
  207. /* Scan event and group name. */
  208. ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]",
  209. &pr, (float *)(void *)&pp->group,
  210. (float *)(void *)&pp->event);
  211. if (ret != 3)
  212. semantic_error("Failed to parse event name: %s", argv[0]);
  213. pr_debug("Group:%s Event:%s probe:%c\n", pp->group, pp->event, pr);
  214. pp->retprobe = (pr == 'r');
  215. /* Scan function name and offset */
  216. ret = sscanf(argv[1], "%a[^+]+%d", (float *)(void *)&pp->function,
  217. &pp->offset);
  218. if (ret == 1)
  219. pp->offset = 0;
  220. /* kprobe_events doesn't have this information */
  221. pp->line = 0;
  222. pp->file = NULL;
  223. pp->nr_args = argc - 2;
  224. pp->args = zalloc(sizeof(char *) * pp->nr_args);
  225. for (i = 0; i < pp->nr_args; i++) {
  226. p = strchr(argv[i + 2], '=');
  227. if (p) /* We don't need which register is assigned. */
  228. *p = '\0';
  229. pp->args[i] = strdup(argv[i + 2]);
  230. if (!pp->args[i])
  231. die("Failed to copy argument.");
  232. }
  233. argv_free(argv);
  234. }
  235. /* Synthesize only probe point (not argument) */
  236. int synthesize_perf_probe_point(struct probe_point *pp)
  237. {
  238. char *buf;
  239. char offs[64] = "", line[64] = "";
  240. int ret;
  241. pp->probes[0] = buf = zalloc(MAX_CMDLEN);
  242. if (!buf)
  243. die("Failed to allocate memory by zalloc.");
  244. if (pp->offset) {
  245. ret = e_snprintf(offs, 64, "+%d", pp->offset);
  246. if (ret <= 0)
  247. goto error;
  248. }
  249. if (pp->line) {
  250. ret = e_snprintf(line, 64, ":%d", pp->line);
  251. if (ret <= 0)
  252. goto error;
  253. }
  254. if (pp->function)
  255. ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s", pp->function,
  256. offs, pp->retprobe ? "%return" : "", line);
  257. else
  258. ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", pp->file, line);
  259. if (ret <= 0) {
  260. error:
  261. free(pp->probes[0]);
  262. pp->probes[0] = NULL;
  263. }
  264. return ret;
  265. }
  266. int synthesize_perf_probe_event(struct probe_point *pp)
  267. {
  268. char *buf;
  269. int i, len, ret;
  270. len = synthesize_perf_probe_point(pp);
  271. if (len < 0)
  272. return 0;
  273. buf = pp->probes[0];
  274. for (i = 0; i < pp->nr_args; i++) {
  275. ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
  276. pp->args[i]);
  277. if (ret <= 0)
  278. goto error;
  279. len += ret;
  280. }
  281. pp->found = 1;
  282. return pp->found;
  283. error:
  284. free(pp->probes[0]);
  285. pp->probes[0] = NULL;
  286. return ret;
  287. }
  288. int synthesize_trace_kprobe_event(struct probe_point *pp)
  289. {
  290. char *buf;
  291. int i, len, ret;
  292. pp->probes[0] = buf = zalloc(MAX_CMDLEN);
  293. if (!buf)
  294. die("Failed to allocate memory by zalloc.");
  295. ret = e_snprintf(buf, MAX_CMDLEN, "%s+%d", pp->function, pp->offset);
  296. if (ret <= 0)
  297. goto error;
  298. len = ret;
  299. for (i = 0; i < pp->nr_args; i++) {
  300. ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
  301. pp->args[i]);
  302. if (ret <= 0)
  303. goto error;
  304. len += ret;
  305. }
  306. pp->found = 1;
  307. return pp->found;
  308. error:
  309. free(pp->probes[0]);
  310. pp->probes[0] = NULL;
  311. return ret;
  312. }
  313. static int open_kprobe_events(int flags, int mode)
  314. {
  315. char buf[PATH_MAX];
  316. int ret;
  317. ret = e_snprintf(buf, PATH_MAX, "%s/../kprobe_events", debugfs_path);
  318. if (ret < 0)
  319. die("Failed to make kprobe_events path.");
  320. ret = open(buf, flags, mode);
  321. if (ret < 0) {
  322. if (errno == ENOENT)
  323. die("kprobe_events file does not exist -"
  324. " please rebuild with CONFIG_KPROBE_EVENT.");
  325. else
  326. die("Could not open kprobe_events file: %s",
  327. strerror(errno));
  328. }
  329. return ret;
  330. }
  331. /* Get raw string list of current kprobe_events */
  332. static struct strlist *get_trace_kprobe_event_rawlist(int fd)
  333. {
  334. int ret, idx;
  335. FILE *fp;
  336. char buf[MAX_CMDLEN];
  337. char *p;
  338. struct strlist *sl;
  339. sl = strlist__new(true, NULL);
  340. fp = fdopen(dup(fd), "r");
  341. while (!feof(fp)) {
  342. p = fgets(buf, MAX_CMDLEN, fp);
  343. if (!p)
  344. break;
  345. idx = strlen(p) - 1;
  346. if (p[idx] == '\n')
  347. p[idx] = '\0';
  348. ret = strlist__add(sl, buf);
  349. if (ret < 0)
  350. die("strlist__add failed: %s", strerror(-ret));
  351. }
  352. fclose(fp);
  353. return sl;
  354. }
  355. /* Free and zero clear probe_point */
  356. static void clear_probe_point(struct probe_point *pp)
  357. {
  358. int i;
  359. if (pp->event)
  360. free(pp->event);
  361. if (pp->group)
  362. free(pp->group);
  363. if (pp->function)
  364. free(pp->function);
  365. if (pp->file)
  366. free(pp->file);
  367. for (i = 0; i < pp->nr_args; i++)
  368. free(pp->args[i]);
  369. if (pp->args)
  370. free(pp->args);
  371. for (i = 0; i < pp->found; i++)
  372. free(pp->probes[i]);
  373. memset(pp, 0, sizeof(*pp));
  374. }
  375. /* Show an event */
  376. static void show_perf_probe_event(const char *event, const char *place,
  377. struct probe_point *pp)
  378. {
  379. int i, ret;
  380. char buf[128];
  381. ret = e_snprintf(buf, 128, "%s:%s", pp->group, event);
  382. if (ret < 0)
  383. die("Failed to copy event: %s", strerror(-ret));
  384. printf(" %-40s (on %s", buf, place);
  385. if (pp->nr_args > 0) {
  386. printf(" with");
  387. for (i = 0; i < pp->nr_args; i++)
  388. printf(" %s", pp->args[i]);
  389. }
  390. printf(")\n");
  391. }
  392. /* List up current perf-probe events */
  393. void show_perf_probe_events(void)
  394. {
  395. int fd;
  396. struct probe_point pp;
  397. struct strlist *rawlist;
  398. struct str_node *ent;
  399. setup_pager();
  400. fd = open_kprobe_events(O_RDONLY, 0);
  401. rawlist = get_trace_kprobe_event_rawlist(fd);
  402. close(fd);
  403. strlist__for_each(ent, rawlist) {
  404. parse_trace_kprobe_event(ent->s, &pp);
  405. /* Synthesize only event probe point */
  406. synthesize_perf_probe_point(&pp);
  407. /* Show an event */
  408. show_perf_probe_event(pp.event, pp.probes[0], &pp);
  409. clear_probe_point(&pp);
  410. }
  411. strlist__delete(rawlist);
  412. }
  413. /* Get current perf-probe event names */
  414. static struct strlist *get_perf_event_names(int fd, bool include_group)
  415. {
  416. char buf[128];
  417. struct strlist *sl, *rawlist;
  418. struct str_node *ent;
  419. struct probe_point pp;
  420. memset(&pp, 0, sizeof(pp));
  421. rawlist = get_trace_kprobe_event_rawlist(fd);
  422. sl = strlist__new(true, NULL);
  423. strlist__for_each(ent, rawlist) {
  424. parse_trace_kprobe_event(ent->s, &pp);
  425. if (include_group) {
  426. if (e_snprintf(buf, 128, "%s:%s", pp.group,
  427. pp.event) < 0)
  428. die("Failed to copy group:event name.");
  429. strlist__add(sl, buf);
  430. } else
  431. strlist__add(sl, pp.event);
  432. clear_probe_point(&pp);
  433. }
  434. strlist__delete(rawlist);
  435. return sl;
  436. }
  437. static void write_trace_kprobe_event(int fd, const char *buf)
  438. {
  439. int ret;
  440. pr_debug("Writing event: %s\n", buf);
  441. ret = write(fd, buf, strlen(buf));
  442. if (ret <= 0)
  443. die("Failed to write event: %s", strerror(errno));
  444. }
  445. static void get_new_event_name(char *buf, size_t len, const char *base,
  446. struct strlist *namelist, bool allow_suffix)
  447. {
  448. int i, ret;
  449. /* Try no suffix */
  450. ret = e_snprintf(buf, len, "%s", base);
  451. if (ret < 0)
  452. die("snprintf() failed: %s", strerror(-ret));
  453. if (!strlist__has_entry(namelist, buf))
  454. return;
  455. if (!allow_suffix) {
  456. pr_warning("Error: event \"%s\" already exists. "
  457. "(Use -f to force duplicates.)\n", base);
  458. die("Can't add new event.");
  459. }
  460. /* Try to add suffix */
  461. for (i = 1; i < MAX_EVENT_INDEX; i++) {
  462. ret = e_snprintf(buf, len, "%s_%d", base, i);
  463. if (ret < 0)
  464. die("snprintf() failed: %s", strerror(-ret));
  465. if (!strlist__has_entry(namelist, buf))
  466. break;
  467. }
  468. if (i == MAX_EVENT_INDEX)
  469. die("Too many events are on the same function.");
  470. }
  471. void add_trace_kprobe_events(struct probe_point *probes, int nr_probes,
  472. bool force_add)
  473. {
  474. int i, j, fd;
  475. struct probe_point *pp;
  476. char buf[MAX_CMDLEN];
  477. char event[64];
  478. struct strlist *namelist;
  479. bool allow_suffix;
  480. fd = open_kprobe_events(O_RDWR, O_APPEND);
  481. /* Get current event names */
  482. namelist = get_perf_event_names(fd, false);
  483. for (j = 0; j < nr_probes; j++) {
  484. pp = probes + j;
  485. if (!pp->event)
  486. pp->event = strdup(pp->function);
  487. if (!pp->group)
  488. pp->group = strdup(PERFPROBE_GROUP);
  489. DIE_IF(!pp->event || !pp->group);
  490. /* If force_add is true, suffix search is allowed */
  491. allow_suffix = force_add;
  492. for (i = 0; i < pp->found; i++) {
  493. /* Get an unused new event name */
  494. get_new_event_name(event, 64, pp->event, namelist,
  495. allow_suffix);
  496. snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s\n",
  497. pp->retprobe ? 'r' : 'p',
  498. pp->group, event,
  499. pp->probes[i]);
  500. write_trace_kprobe_event(fd, buf);
  501. printf("Added new event:\n");
  502. /* Get the first parameter (probe-point) */
  503. sscanf(pp->probes[i], "%s", buf);
  504. show_perf_probe_event(event, buf, pp);
  505. /* Add added event name to namelist */
  506. strlist__add(namelist, event);
  507. /*
  508. * Probes after the first probe which comes from same
  509. * user input are always allowed to add suffix, because
  510. * there might be several addresses corresponding to
  511. * one code line.
  512. */
  513. allow_suffix = true;
  514. }
  515. }
  516. /* Show how to use the event. */
  517. printf("\nYou can now use it on all perf tools, such as:\n\n");
  518. printf("\tperf record -e %s:%s -a sleep 1\n\n", PERFPROBE_GROUP, event);
  519. strlist__delete(namelist);
  520. close(fd);
  521. }
  522. static void __del_trace_kprobe_event(int fd, struct str_node *ent)
  523. {
  524. char *p;
  525. char buf[128];
  526. /* Convert from perf-probe event to trace-kprobe event */
  527. if (e_snprintf(buf, 128, "-:%s", ent->s) < 0)
  528. die("Failed to copy event.");
  529. p = strchr(buf + 2, ':');
  530. if (!p)
  531. die("Internal error: %s should have ':' but not.", ent->s);
  532. *p = '/';
  533. write_trace_kprobe_event(fd, buf);
  534. printf("Remove event: %s\n", ent->s);
  535. }
  536. static void del_trace_kprobe_event(int fd, const char *group,
  537. const char *event, struct strlist *namelist)
  538. {
  539. char buf[128];
  540. struct str_node *ent, *n;
  541. int found = 0;
  542. if (e_snprintf(buf, 128, "%s:%s", group, event) < 0)
  543. die("Failed to copy event.");
  544. if (strpbrk(buf, "*?")) { /* Glob-exp */
  545. strlist__for_each_safe(ent, n, namelist)
  546. if (strglobmatch(ent->s, buf)) {
  547. found++;
  548. __del_trace_kprobe_event(fd, ent);
  549. strlist__remove(namelist, ent);
  550. }
  551. } else {
  552. ent = strlist__find(namelist, buf);
  553. if (ent) {
  554. found++;
  555. __del_trace_kprobe_event(fd, ent);
  556. strlist__remove(namelist, ent);
  557. }
  558. }
  559. if (found == 0)
  560. pr_info("Info: event \"%s\" does not exist, could not remove it.\n", buf);
  561. }
  562. void del_trace_kprobe_events(struct strlist *dellist)
  563. {
  564. int fd;
  565. const char *group, *event;
  566. char *p, *str;
  567. struct str_node *ent;
  568. struct strlist *namelist;
  569. fd = open_kprobe_events(O_RDWR, O_APPEND);
  570. /* Get current event names */
  571. namelist = get_perf_event_names(fd, true);
  572. strlist__for_each(ent, dellist) {
  573. str = strdup(ent->s);
  574. if (!str)
  575. die("Failed to copy event.");
  576. pr_debug("Parsing: %s\n", str);
  577. p = strchr(str, ':');
  578. if (p) {
  579. group = str;
  580. *p = '\0';
  581. event = p + 1;
  582. } else {
  583. group = "*";
  584. event = str;
  585. }
  586. pr_debug("Group: %s, Event: %s\n", group, event);
  587. del_trace_kprobe_event(fd, group, event, namelist);
  588. free(str);
  589. }
  590. strlist__delete(namelist);
  591. close(fd);
  592. }