probe-event.c 14 KB

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