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