2 * probe-event.c : perf-probe definition to kprobe_events format converter
4 * Written by Masami Hiramatsu <mhiramat@redhat.com>
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.
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.
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.
23 #include <sys/utsname.h>
24 #include <sys/types.h>
41 #include "parse-events.h" /* For debugfs_path */
42 #include "probe-event.h"
44 #define MAX_CMDLEN 256
45 #define MAX_PROBE_ARGS 128
46 #define PERFPROBE_GROUP "probe"
48 #define semantic_error(msg ...) die("Semantic error :" msg)
50 /* If there is no space to write, returns -E2BIG. */
51 static int e_snprintf(char *str, size_t size, const char *format, ...)
52 __attribute__((format(printf, 3, 4)));
54 static int e_snprintf(char *str, size_t size, const char *format, ...)
59 ret = vsnprintf(str, size, format, ap);
66 /* Check the name is good for event/group */
67 static bool check_event_name(const char *name)
69 if (!isalpha(*name) && *name != '_')
71 while (*++name != '\0') {
72 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
78 /* Parse probepoint definition. */
79 static void parse_perf_probe_probepoint(char *arg, struct probe_point *pp)
85 * perf probe [EVENT=]SRC:LN
86 * perf probe [EVENT=]FUNC[+OFFS|%return][@SRC]
88 * TODO:Group name support
91 ptr = strchr(arg, '=');
92 if (ptr) { /* Event name */
95 ptr = strchr(arg, ':');
96 if (ptr) /* Group name is not supported yet. */
97 semantic_error("Group name is not supported yet.");
98 if (!check_event_name(arg))
99 semantic_error("%s is bad for event name -it must "
100 "follow C symbol-naming rule.", arg);
101 pp->event = strdup(arg);
105 ptr = strpbrk(arg, ":+@%");
111 /* Check arg is function or file and copy it */
112 if (strchr(arg, '.')) /* File */
113 pp->file = strdup(arg);
115 pp->function = strdup(arg);
116 DIE_IF(pp->file == NULL && pp->function == NULL);
118 /* Parse other options */
122 ptr = strpbrk(arg, ":+@%");
128 case ':': /* Line number */
129 pp->line = strtoul(arg, &tmp, 0);
131 semantic_error("There is non-digit charactor"
134 case '+': /* Byte offset from a symbol */
135 pp->offset = strtoul(arg, &tmp, 0);
137 semantic_error("There is non-digit charactor"
140 case '@': /* File name */
142 semantic_error("SRC@SRC is not allowed.");
143 pp->file = strdup(arg);
144 DIE_IF(pp->file == NULL);
146 semantic_error("@SRC must be the last "
149 case '%': /* Probe places */
150 if (strcmp(arg, "return") == 0) {
152 } else /* Others not supported yet */
153 semantic_error("%%%s is not supported.", arg);
156 DIE_IF("Program has a bug.");
161 /* Exclusion check */
162 if (pp->line && pp->offset)
163 semantic_error("Offset can't be used with line number.");
165 if (!pp->line && pp->file && !pp->function)
166 semantic_error("File always requires line number.");
168 if (pp->offset && !pp->function)
169 semantic_error("Offset requires an entry function.");
171 if (pp->retprobe && !pp->function)
172 semantic_error("Return probe requires an entry function.");
174 if ((pp->offset || pp->line) && pp->retprobe)
175 semantic_error("Offset/Line can't be used with return probe.");
177 pr_debug("symbol:%s file:%s line:%d offset:%d, return:%d\n",
178 pp->function, pp->file, pp->line, pp->offset, pp->retprobe);
181 /* Parse perf-probe event definition */
182 void parse_perf_probe_event(const char *str, struct probe_point *pp,
190 argv = argv_split(str, &argc);
192 die("argv_split failed.");
193 if (argc > MAX_PROBE_ARGS + 1)
194 semantic_error("Too many arguments");
196 /* Parse probe point */
197 parse_perf_probe_probepoint(argv[0], pp);
198 if (pp->file || pp->line)
201 /* Copy arguments and ensure return probe has no C argument */
202 pp->nr_args = argc - 1;
203 pp->args = zalloc(sizeof(char *) * pp->nr_args);
204 for (i = 0; i < pp->nr_args; i++) {
205 pp->args[i] = strdup(argv[i + 1]);
207 die("Failed to copy argument.");
208 if (is_c_varname(pp->args[i])) {
210 semantic_error("You can't specify local"
211 " variable for kretprobe");
219 /* Parse kprobe_events event into struct probe_point */
220 void parse_trace_kprobe_event(const char *str, struct probe_point *pp)
227 pr_debug("Parsing kprobe_events: %s\n", str);
228 argv = argv_split(str, &argc);
230 die("argv_split failed.");
232 semantic_error("Too less arguments.");
234 /* Scan event and group name. */
235 ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]",
236 &pr, (float *)(void *)&pp->group,
237 (float *)(void *)&pp->event);
239 semantic_error("Failed to parse event name: %s", argv[0]);
240 pr_debug("Group:%s Event:%s probe:%c\n", pp->group, pp->event, pr);
242 pp->retprobe = (pr == 'r');
244 /* Scan function name and offset */
245 ret = sscanf(argv[1], "%a[^+]+%d", (float *)(void *)&pp->function,
250 /* kprobe_events doesn't have this information */
254 pp->nr_args = argc - 2;
255 pp->args = zalloc(sizeof(char *) * pp->nr_args);
256 for (i = 0; i < pp->nr_args; i++) {
257 p = strchr(argv[i + 2], '=');
258 if (p) /* We don't need which register is assigned. */
260 pp->args[i] = strdup(argv[i + 2]);
262 die("Failed to copy argument.");
268 /* Synthesize only probe point (not argument) */
269 int synthesize_perf_probe_point(struct probe_point *pp)
272 char offs[64] = "", line[64] = "";
275 pp->probes[0] = buf = zalloc(MAX_CMDLEN);
277 die("Failed to allocate memory by zalloc.");
279 ret = e_snprintf(offs, 64, "+%d", pp->offset);
284 ret = e_snprintf(line, 64, ":%d", pp->line);
290 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s", pp->function,
291 offs, pp->retprobe ? "%return" : "", line);
293 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", pp->file, line);
297 pp->probes[0] = NULL;
302 int synthesize_perf_probe_event(struct probe_point *pp)
307 len = synthesize_perf_probe_point(pp);
312 for (i = 0; i < pp->nr_args; i++) {
313 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
324 pp->probes[0] = NULL;
329 int synthesize_trace_kprobe_event(struct probe_point *pp)
334 pp->probes[0] = buf = zalloc(MAX_CMDLEN);
336 die("Failed to allocate memory by zalloc.");
337 ret = e_snprintf(buf, MAX_CMDLEN, "%s+%d", pp->function, pp->offset);
342 for (i = 0; i < pp->nr_args; i++) {
343 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
354 pp->probes[0] = NULL;
359 static int open_kprobe_events(int flags, int mode)
364 ret = e_snprintf(buf, PATH_MAX, "%s/../kprobe_events", debugfs_path);
366 die("Failed to make kprobe_events path.");
368 ret = open(buf, flags, mode);
371 die("kprobe_events file does not exist -"
372 " please rebuild with CONFIG_KPROBE_EVENT.");
374 die("Could not open kprobe_events file: %s",
380 /* Get raw string list of current kprobe_events */
381 static struct strlist *get_trace_kprobe_event_rawlist(int fd)
385 char buf[MAX_CMDLEN];
389 sl = strlist__new(true, NULL);
391 fp = fdopen(dup(fd), "r");
393 p = fgets(buf, MAX_CMDLEN, fp);
400 ret = strlist__add(sl, buf);
402 die("strlist__add failed: %s", strerror(-ret));
409 /* Free and zero clear probe_point */
410 static void clear_probe_point(struct probe_point *pp)
422 for (i = 0; i < pp->nr_args; i++)
426 for (i = 0; i < pp->found; i++)
428 memset(pp, 0, sizeof(*pp));
432 static void show_perf_probe_event(const char *event, const char *place,
433 struct probe_point *pp)
438 ret = e_snprintf(buf, 128, "%s:%s", pp->group, event);
440 die("Failed to copy event: %s", strerror(-ret));
441 printf(" %-40s (on %s", buf, place);
443 if (pp->nr_args > 0) {
445 for (i = 0; i < pp->nr_args; i++)
446 printf(" %s", pp->args[i]);
451 /* List up current perf-probe events */
452 void show_perf_probe_events(void)
455 struct probe_point pp;
456 struct strlist *rawlist;
457 struct str_node *ent;
461 fd = open_kprobe_events(O_RDONLY, 0);
462 rawlist = get_trace_kprobe_event_rawlist(fd);
465 strlist__for_each(ent, rawlist) {
466 parse_trace_kprobe_event(ent->s, &pp);
467 /* Synthesize only event probe point */
468 synthesize_perf_probe_point(&pp);
470 show_perf_probe_event(pp.event, pp.probes[0], &pp);
471 clear_probe_point(&pp);
474 strlist__delete(rawlist);
477 /* Get current perf-probe event names */
478 static struct strlist *get_perf_event_names(int fd, bool include_group)
481 struct strlist *sl, *rawlist;
482 struct str_node *ent;
483 struct probe_point pp;
485 memset(&pp, 0, sizeof(pp));
486 rawlist = get_trace_kprobe_event_rawlist(fd);
488 sl = strlist__new(true, NULL);
489 strlist__for_each(ent, rawlist) {
490 parse_trace_kprobe_event(ent->s, &pp);
492 if (e_snprintf(buf, 128, "%s:%s", pp.group,
494 die("Failed to copy group:event name.");
495 strlist__add(sl, buf);
497 strlist__add(sl, pp.event);
498 clear_probe_point(&pp);
501 strlist__delete(rawlist);
506 static void write_trace_kprobe_event(int fd, const char *buf)
510 pr_debug("Writing event: %s\n", buf);
511 ret = write(fd, buf, strlen(buf));
513 die("Failed to write event: %s", strerror(errno));
516 static void get_new_event_name(char *buf, size_t len, const char *base,
517 struct strlist *namelist, bool allow_suffix)
522 ret = e_snprintf(buf, len, "%s", base);
524 die("snprintf() failed: %s", strerror(-ret));
525 if (!strlist__has_entry(namelist, buf))
529 pr_warning("Error: event \"%s\" already exists. "
530 "(Use -f to force duplicates.)\n", base);
531 die("Can't add new event.");
534 /* Try to add suffix */
535 for (i = 1; i < MAX_EVENT_INDEX; i++) {
536 ret = e_snprintf(buf, len, "%s_%d", base, i);
538 die("snprintf() failed: %s", strerror(-ret));
539 if (!strlist__has_entry(namelist, buf))
542 if (i == MAX_EVENT_INDEX)
543 die("Too many events are on the same function.");
546 void add_trace_kprobe_events(struct probe_point *probes, int nr_probes,
550 struct probe_point *pp;
551 char buf[MAX_CMDLEN];
553 struct strlist *namelist;
556 fd = open_kprobe_events(O_RDWR, O_APPEND);
557 /* Get current event names */
558 namelist = get_perf_event_names(fd, false);
560 for (j = 0; j < nr_probes; j++) {
563 pp->event = strdup(pp->function);
565 pp->group = strdup(PERFPROBE_GROUP);
566 DIE_IF(!pp->event || !pp->group);
567 /* If force_add is true, suffix search is allowed */
568 allow_suffix = force_add;
569 for (i = 0; i < pp->found; i++) {
570 /* Get an unused new event name */
571 get_new_event_name(event, 64, pp->event, namelist,
573 snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s\n",
574 pp->retprobe ? 'r' : 'p',
577 write_trace_kprobe_event(fd, buf);
578 printf("Added new event:\n");
579 /* Get the first parameter (probe-point) */
580 sscanf(pp->probes[i], "%s", buf);
581 show_perf_probe_event(event, buf, pp);
582 /* Add added event name to namelist */
583 strlist__add(namelist, event);
585 * Probes after the first probe which comes from same
586 * user input are always allowed to add suffix, because
587 * there might be several addresses corresponding to
593 /* Show how to use the event. */
594 printf("\nYou can now use it on all perf tools, such as:\n\n");
595 printf("\tperf record -e %s:%s -a sleep 1\n\n", PERFPROBE_GROUP, event);
597 strlist__delete(namelist);
601 static void __del_trace_kprobe_event(int fd, struct str_node *ent)
606 /* Convert from perf-probe event to trace-kprobe event */
607 if (e_snprintf(buf, 128, "-:%s", ent->s) < 0)
608 die("Failed to copy event.");
609 p = strchr(buf + 2, ':');
611 die("Internal error: %s should have ':' but not.", ent->s);
614 write_trace_kprobe_event(fd, buf);
615 printf("Remove event: %s\n", ent->s);
618 static void del_trace_kprobe_event(int fd, const char *group,
619 const char *event, struct strlist *namelist)
622 struct str_node *ent, *n;
625 if (e_snprintf(buf, 128, "%s:%s", group, event) < 0)
626 die("Failed to copy event.");
628 if (strpbrk(buf, "*?")) { /* Glob-exp */
629 strlist__for_each_safe(ent, n, namelist)
630 if (strglobmatch(ent->s, buf)) {
632 __del_trace_kprobe_event(fd, ent);
633 strlist__remove(namelist, ent);
636 ent = strlist__find(namelist, buf);
639 __del_trace_kprobe_event(fd, ent);
640 strlist__remove(namelist, ent);
644 pr_info("Info: event \"%s\" does not exist, could not remove it.\n", buf);
647 void del_trace_kprobe_events(struct strlist *dellist)
650 const char *group, *event;
652 struct str_node *ent;
653 struct strlist *namelist;
655 fd = open_kprobe_events(O_RDWR, O_APPEND);
656 /* Get current event names */
657 namelist = get_perf_event_names(fd, true);
659 strlist__for_each(ent, dellist) {
660 str = strdup(ent->s);
662 die("Failed to copy event.");
663 pr_debug("Parsing: %s\n", str);
664 p = strchr(str, ':');
673 pr_debug("Group: %s, Event: %s\n", group, event);
674 del_trace_kprobe_event(fd, group, event, namelist);
677 strlist__delete(namelist);