probe-event.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  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. if (!buf)
  242. die("Failed to allocate memory by zalloc.");
  243. if (pp->offset) {
  244. ret = e_snprintf(offs, 64, "+%d", pp->offset);
  245. if (ret <= 0)
  246. goto error;
  247. }
  248. if (pp->line) {
  249. ret = e_snprintf(line, 64, ":%d", pp->line);
  250. if (ret <= 0)
  251. goto error;
  252. }
  253. if (pp->function)
  254. ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s", pp->function,
  255. offs, pp->retprobe ? "%return" : "", line);
  256. else
  257. ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", pp->file, line);
  258. if (ret <= 0) {
  259. error:
  260. free(pp->probes[0]);
  261. pp->probes[0] = NULL;
  262. }
  263. return ret;
  264. }
  265. int synthesize_perf_probe_event(struct probe_point *pp)
  266. {
  267. char *buf;
  268. int i, len, ret;
  269. len = synthesize_perf_probe_point(pp);
  270. if (len < 0)
  271. return 0;
  272. buf = pp->probes[0];
  273. for (i = 0; i < pp->nr_args; i++) {
  274. ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
  275. pp->args[i]);
  276. if (ret <= 0)
  277. goto error;
  278. len += ret;
  279. }
  280. pp->found = 1;
  281. return pp->found;
  282. error:
  283. free(pp->probes[0]);
  284. pp->probes[0] = NULL;
  285. return ret;
  286. }
  287. int synthesize_trace_kprobe_event(struct probe_point *pp)
  288. {
  289. char *buf;
  290. int i, len, ret;
  291. pp->probes[0] = buf = zalloc(MAX_CMDLEN);
  292. if (!buf)
  293. die("Failed to allocate memory by zalloc.");
  294. ret = e_snprintf(buf, MAX_CMDLEN, "%s+%d", pp->function, pp->offset);
  295. if (ret <= 0)
  296. goto error;
  297. len = ret;
  298. for (i = 0; i < pp->nr_args; i++) {
  299. ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
  300. pp->args[i]);
  301. if (ret <= 0)
  302. goto error;
  303. len += ret;
  304. }
  305. pp->found = 1;
  306. return pp->found;
  307. error:
  308. free(pp->probes[0]);
  309. pp->probes[0] = NULL;
  310. return ret;
  311. }
  312. static int open_kprobe_events(int flags, int mode)
  313. {
  314. char buf[PATH_MAX];
  315. int ret;
  316. ret = e_snprintf(buf, PATH_MAX, "%s/../kprobe_events", debugfs_path);
  317. if (ret < 0)
  318. die("Failed to make kprobe_events path.");
  319. ret = open(buf, flags, mode);
  320. if (ret < 0) {
  321. if (errno == ENOENT)
  322. die("kprobe_events file does not exist -"
  323. " please rebuild with CONFIG_KPROBE_TRACER.");
  324. else
  325. die("Could not open kprobe_events file: %s",
  326. strerror(errno));
  327. }
  328. return ret;
  329. }
  330. /* Get raw string list of current kprobe_events */
  331. static struct strlist *get_trace_kprobe_event_rawlist(int fd)
  332. {
  333. int ret, idx;
  334. FILE *fp;
  335. char buf[MAX_CMDLEN];
  336. char *p;
  337. struct strlist *sl;
  338. sl = strlist__new(true, NULL);
  339. fp = fdopen(dup(fd), "r");
  340. while (!feof(fp)) {
  341. p = fgets(buf, MAX_CMDLEN, fp);
  342. if (!p)
  343. break;
  344. idx = strlen(p) - 1;
  345. if (p[idx] == '\n')
  346. p[idx] = '\0';
  347. ret = strlist__add(sl, buf);
  348. if (ret < 0)
  349. die("strlist__add failed: %s", strerror(-ret));
  350. }
  351. fclose(fp);
  352. return sl;
  353. }
  354. /* Free and zero clear probe_point */
  355. static void clear_probe_point(struct probe_point *pp)
  356. {
  357. int i;
  358. if (pp->event)
  359. free(pp->event);
  360. if (pp->group)
  361. free(pp->group);
  362. if (pp->function)
  363. free(pp->function);
  364. if (pp->file)
  365. free(pp->file);
  366. for (i = 0; i < pp->nr_args; i++)
  367. free(pp->args[i]);
  368. if (pp->args)
  369. free(pp->args);
  370. for (i = 0; i < pp->found; i++)
  371. free(pp->probes[i]);
  372. memset(pp, 0, sizeof(*pp));
  373. }
  374. /* Show an event */
  375. static void show_perf_probe_event(const char *event, const char *place,
  376. struct probe_point *pp)
  377. {
  378. int i, ret;
  379. char buf[128];
  380. ret = e_snprintf(buf, 128, "%s:%s", pp->group, event);
  381. if (ret < 0)
  382. die("Failed to copy event: %s", strerror(-ret));
  383. printf(" %-40s (on %s", buf, place);
  384. if (pp->nr_args > 0) {
  385. printf(" with");
  386. for (i = 0; i < pp->nr_args; i++)
  387. printf(" %s", pp->args[i]);
  388. }
  389. printf(")\n");
  390. }
  391. /* List up current perf-probe events */
  392. void show_perf_probe_events(void)
  393. {
  394. int fd;
  395. struct probe_point pp;
  396. struct strlist *rawlist;
  397. struct str_node *ent;
  398. fd = open_kprobe_events(O_RDONLY, 0);
  399. rawlist = get_trace_kprobe_event_rawlist(fd);
  400. close(fd);
  401. strlist__for_each(ent, rawlist) {
  402. parse_trace_kprobe_event(ent->s, &pp);
  403. /* Synthesize only event probe point */
  404. synthesize_perf_probe_point(&pp);
  405. /* Show an event */
  406. show_perf_probe_event(pp.event, pp.probes[0], &pp);
  407. clear_probe_point(&pp);
  408. }
  409. strlist__delete(rawlist);
  410. }
  411. /* Get current perf-probe event names */
  412. static struct strlist *get_perf_event_names(int fd, bool include_group)
  413. {
  414. char buf[128];
  415. struct strlist *sl, *rawlist;
  416. struct str_node *ent;
  417. struct probe_point pp;
  418. memset(&pp, 0, sizeof(pp));
  419. rawlist = get_trace_kprobe_event_rawlist(fd);
  420. sl = strlist__new(true, NULL);
  421. strlist__for_each(ent, rawlist) {
  422. parse_trace_kprobe_event(ent->s, &pp);
  423. if (include_group) {
  424. if (e_snprintf(buf, 128, "%s:%s", pp.group,
  425. pp.event) < 0)
  426. die("Failed to copy group:event name.");
  427. strlist__add(sl, buf);
  428. } else
  429. strlist__add(sl, pp.event);
  430. clear_probe_point(&pp);
  431. }
  432. strlist__delete(rawlist);
  433. return sl;
  434. }
  435. static void write_trace_kprobe_event(int fd, const char *buf)
  436. {
  437. int ret;
  438. pr_debug("Writing event: %s\n", buf);
  439. ret = write(fd, buf, strlen(buf));
  440. if (ret <= 0)
  441. die("Failed to write event: %s", strerror(errno));
  442. }
  443. static void get_new_event_name(char *buf, size_t len, const char *base,
  444. struct strlist *namelist, bool allow_suffix)
  445. {
  446. int i, ret;
  447. /* Try no suffix */
  448. ret = e_snprintf(buf, len, "%s", base);
  449. if (ret < 0)
  450. die("snprintf() failed: %s", strerror(-ret));
  451. if (!strlist__has_entry(namelist, buf))
  452. return;
  453. if (!allow_suffix) {
  454. pr_warning("Error: event \"%s\" already exists. "
  455. "(Use -f to force duplicates.)\n", base);
  456. die("Can't add new event.");
  457. }
  458. /* Try to add suffix */
  459. for (i = 1; i < MAX_EVENT_INDEX; i++) {
  460. ret = e_snprintf(buf, len, "%s_%d", base, i);
  461. if (ret < 0)
  462. die("snprintf() failed: %s", strerror(-ret));
  463. if (!strlist__has_entry(namelist, buf))
  464. break;
  465. }
  466. if (i == MAX_EVENT_INDEX)
  467. die("Too many events are on the same function.");
  468. }
  469. void add_trace_kprobe_events(struct probe_point *probes, int nr_probes,
  470. bool force_add)
  471. {
  472. int i, j, fd;
  473. struct probe_point *pp;
  474. char buf[MAX_CMDLEN];
  475. char event[64];
  476. struct strlist *namelist;
  477. bool allow_suffix;
  478. fd = open_kprobe_events(O_RDWR, O_APPEND);
  479. /* Get current event names */
  480. namelist = get_perf_event_names(fd, false);
  481. for (j = 0; j < nr_probes; j++) {
  482. pp = probes + j;
  483. if (!pp->event)
  484. pp->event = strdup(pp->function);
  485. if (!pp->group)
  486. pp->group = strdup(PERFPROBE_GROUP);
  487. DIE_IF(!pp->event || !pp->group);
  488. /* If force_add is true, suffix search is allowed */
  489. allow_suffix = force_add;
  490. for (i = 0; i < pp->found; i++) {
  491. /* Get an unused new event name */
  492. get_new_event_name(event, 64, pp->event, namelist,
  493. allow_suffix);
  494. snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s\n",
  495. pp->retprobe ? 'r' : 'p',
  496. pp->group, event,
  497. pp->probes[i]);
  498. write_trace_kprobe_event(fd, buf);
  499. printf("Added new event:\n");
  500. /* Get the first parameter (probe-point) */
  501. sscanf(pp->probes[i], "%s", buf);
  502. show_perf_probe_event(event, buf, pp);
  503. /* Add added event name to namelist */
  504. strlist__add(namelist, event);
  505. /*
  506. * Probes after the first probe which comes from same
  507. * user input are always allowed to add suffix, because
  508. * there might be several addresses corresponding to
  509. * one code line.
  510. */
  511. allow_suffix = true;
  512. }
  513. }
  514. /* Show how to use the event. */
  515. printf("\nYou can now use it on all perf tools, such as:\n\n");
  516. printf("\tperf record -e %s:%s -a sleep 1\n\n", PERFPROBE_GROUP, event);
  517. strlist__delete(namelist);
  518. close(fd);
  519. }
  520. static void __del_trace_kprobe_event(int fd, struct str_node *ent)
  521. {
  522. char *p;
  523. char buf[128];
  524. /* Convert from perf-probe event to trace-kprobe event */
  525. if (e_snprintf(buf, 128, "-:%s", ent->s) < 0)
  526. die("Failed to copy event.");
  527. p = strchr(buf + 2, ':');
  528. if (!p)
  529. die("Internal error: %s should have ':' but not.", ent->s);
  530. *p = '/';
  531. write_trace_kprobe_event(fd, buf);
  532. printf("Remove event: %s\n", ent->s);
  533. }
  534. static void del_trace_kprobe_event(int fd, const char *group,
  535. const char *event, struct strlist *namelist)
  536. {
  537. char buf[128];
  538. struct str_node *ent, *n;
  539. int found = 0;
  540. if (e_snprintf(buf, 128, "%s:%s", group, event) < 0)
  541. die("Failed to copy event.");
  542. if (strpbrk(buf, "*?")) { /* Glob-exp */
  543. strlist__for_each_safe(ent, n, namelist)
  544. if (strglobmatch(ent->s, buf)) {
  545. found++;
  546. __del_trace_kprobe_event(fd, ent);
  547. strlist__remove(namelist, ent);
  548. }
  549. } else {
  550. ent = strlist__find(namelist, buf);
  551. if (ent) {
  552. found++;
  553. __del_trace_kprobe_event(fd, ent);
  554. strlist__remove(namelist, ent);
  555. }
  556. }
  557. if (found == 0)
  558. pr_info("Info: event \"%s\" does not exist, could not remove it.\n", buf);
  559. }
  560. void del_trace_kprobe_events(struct strlist *dellist)
  561. {
  562. int fd;
  563. const char *group, *event;
  564. char *p, *str;
  565. struct str_node *ent;
  566. struct strlist *namelist;
  567. fd = open_kprobe_events(O_RDWR, O_APPEND);
  568. /* Get current event names */
  569. namelist = get_perf_event_names(fd, true);
  570. strlist__for_each(ent, dellist) {
  571. str = strdup(ent->s);
  572. if (!str)
  573. die("Failed to copy event.");
  574. pr_debug("Parsing: %s\n", str);
  575. p = strchr(str, ':');
  576. if (p) {
  577. group = str;
  578. *p = '\0';
  579. event = p + 1;
  580. } else {
  581. group = "*";
  582. event = str;
  583. }
  584. pr_debug("Group: %s, Event: %s\n", group, event);
  585. del_trace_kprobe_event(fd, group, event, namelist);
  586. free(str);
  587. }
  588. strlist__delete(namelist);
  589. close(fd);
  590. }