probe-event.c 15 KB

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