probe-event.c 14 KB

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