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>
42 #include "parse-events.h" /* For debugfs_path */
43 #include "probe-event.h"
45 #define MAX_CMDLEN 256
46 #define MAX_PROBE_ARGS 128
47 #define PERFPROBE_GROUP "probe"
49 #define semantic_error(msg ...) die("Semantic error :" msg)
51 /* If there is no space to write, returns -E2BIG. */
52 static int e_snprintf(char *str, size_t size, const char *format, ...)
53 __attribute__((format(printf, 3, 4)));
55 static int e_snprintf(char *str, size_t size, const char *format, ...)
60 ret = vsnprintf(str, size, format, ap);
67 void parse_line_range_desc(const char *arg, struct line_range *lr)
74 * FUNC[:SLN[+NUM|-ELN]]
76 ptr = strchr(arg, ':');
78 lr->start = (unsigned int)strtoul(ptr + 1, &tmp, 0);
80 lr->end = lr->start + (unsigned int)strtoul(tmp + 1,
83 lr->end = (unsigned int)strtoul(tmp + 1, &tmp, 0);
86 pr_debug("Line range is %u to %u\n", lr->start, lr->end);
87 if (lr->end && lr->start > lr->end)
88 semantic_error("Start line must be smaller"
91 semantic_error("Tailing with invalid character '%d'.",
93 tmp = strndup(arg, (ptr - arg));
103 /* Check the name is good for event/group */
104 static bool check_event_name(const char *name)
106 if (!isalpha(*name) && *name != '_')
108 while (*++name != '\0') {
109 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
115 /* Parse probepoint definition. */
116 static void parse_perf_probe_probepoint(char *arg, struct probe_point *pp)
122 * perf probe [EVENT=]SRC:LN
123 * perf probe [EVENT=]FUNC[+OFFS|%return][@SRC]
125 * TODO:Group name support
128 ptr = strchr(arg, '=');
129 if (ptr) { /* Event name */
132 ptr = strchr(arg, ':');
133 if (ptr) /* Group name is not supported yet. */
134 semantic_error("Group name is not supported yet.");
135 if (!check_event_name(arg))
136 semantic_error("%s is bad for event name -it must "
137 "follow C symbol-naming rule.", arg);
138 pp->event = strdup(arg);
142 ptr = strpbrk(arg, ":+@%");
148 /* Check arg is function or file and copy it */
149 if (strchr(arg, '.')) /* File */
150 pp->file = strdup(arg);
152 pp->function = strdup(arg);
153 DIE_IF(pp->file == NULL && pp->function == NULL);
155 /* Parse other options */
159 ptr = strpbrk(arg, ":+@%");
165 case ':': /* Line number */
166 pp->line = strtoul(arg, &tmp, 0);
168 semantic_error("There is non-digit charactor"
171 case '+': /* Byte offset from a symbol */
172 pp->offset = strtoul(arg, &tmp, 0);
174 semantic_error("There is non-digit charactor"
177 case '@': /* File name */
179 semantic_error("SRC@SRC is not allowed.");
180 pp->file = strdup(arg);
181 DIE_IF(pp->file == NULL);
183 semantic_error("@SRC must be the last "
186 case '%': /* Probe places */
187 if (strcmp(arg, "return") == 0) {
189 } else /* Others not supported yet */
190 semantic_error("%%%s is not supported.", arg);
193 DIE_IF("Program has a bug.");
198 /* Exclusion check */
199 if (pp->line && pp->offset)
200 semantic_error("Offset can't be used with line number.");
202 if (!pp->line && pp->file && !pp->function)
203 semantic_error("File always requires line number.");
205 if (pp->offset && !pp->function)
206 semantic_error("Offset requires an entry function.");
208 if (pp->retprobe && !pp->function)
209 semantic_error("Return probe requires an entry function.");
211 if ((pp->offset || pp->line) && pp->retprobe)
212 semantic_error("Offset/Line can't be used with return probe.");
214 pr_debug("symbol:%s file:%s line:%d offset:%d, return:%d\n",
215 pp->function, pp->file, pp->line, pp->offset, pp->retprobe);
218 /* Parse perf-probe event definition */
219 void parse_perf_probe_event(const char *str, struct probe_point *pp,
227 argv = argv_split(str, &argc);
229 die("argv_split failed.");
230 if (argc > MAX_PROBE_ARGS + 1)
231 semantic_error("Too many arguments");
233 /* Parse probe point */
234 parse_perf_probe_probepoint(argv[0], pp);
235 if (pp->file || pp->line)
238 /* Copy arguments and ensure return probe has no C argument */
239 pp->nr_args = argc - 1;
240 pp->args = zalloc(sizeof(char *) * pp->nr_args);
241 for (i = 0; i < pp->nr_args; i++) {
242 pp->args[i] = strdup(argv[i + 1]);
244 die("Failed to copy argument.");
245 if (is_c_varname(pp->args[i])) {
247 semantic_error("You can't specify local"
248 " variable for kretprobe");
256 /* Parse kprobe_events event into struct probe_point */
257 void parse_trace_kprobe_event(const char *str, struct probe_point *pp)
264 pr_debug("Parsing kprobe_events: %s\n", str);
265 argv = argv_split(str, &argc);
267 die("argv_split failed.");
269 semantic_error("Too less arguments.");
271 /* Scan event and group name. */
272 ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]",
273 &pr, (float *)(void *)&pp->group,
274 (float *)(void *)&pp->event);
276 semantic_error("Failed to parse event name: %s", argv[0]);
277 pr_debug("Group:%s Event:%s probe:%c\n", pp->group, pp->event, pr);
279 pp->retprobe = (pr == 'r');
281 /* Scan function name and offset */
282 ret = sscanf(argv[1], "%a[^+]+%d", (float *)(void *)&pp->function,
287 /* kprobe_events doesn't have this information */
291 pp->nr_args = argc - 2;
292 pp->args = zalloc(sizeof(char *) * pp->nr_args);
293 for (i = 0; i < pp->nr_args; i++) {
294 p = strchr(argv[i + 2], '=');
295 if (p) /* We don't need which register is assigned. */
297 pp->args[i] = strdup(argv[i + 2]);
299 die("Failed to copy argument.");
305 /* Synthesize only probe point (not argument) */
306 int synthesize_perf_probe_point(struct probe_point *pp)
309 char offs[64] = "", line[64] = "";
312 pp->probes[0] = buf = zalloc(MAX_CMDLEN);
315 die("Failed to allocate memory by zalloc.");
317 ret = e_snprintf(offs, 64, "+%d", pp->offset);
322 ret = e_snprintf(line, 64, ":%d", pp->line);
328 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s", pp->function,
329 offs, pp->retprobe ? "%return" : "", line);
331 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", pp->file, line);
335 pp->probes[0] = NULL;
341 int synthesize_perf_probe_event(struct probe_point *pp)
346 len = synthesize_perf_probe_point(pp);
351 for (i = 0; i < pp->nr_args; i++) {
352 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
363 pp->probes[0] = NULL;
368 int synthesize_trace_kprobe_event(struct probe_point *pp)
373 pp->probes[0] = buf = zalloc(MAX_CMDLEN);
375 die("Failed to allocate memory by zalloc.");
376 ret = e_snprintf(buf, MAX_CMDLEN, "%s+%d", pp->function, pp->offset);
381 for (i = 0; i < pp->nr_args; i++) {
382 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
393 pp->probes[0] = NULL;
398 static int open_kprobe_events(int flags, int mode)
403 ret = e_snprintf(buf, PATH_MAX, "%s/../kprobe_events", debugfs_path);
405 die("Failed to make kprobe_events path.");
407 ret = open(buf, flags, mode);
410 die("kprobe_events file does not exist -"
411 " please rebuild with CONFIG_KPROBE_EVENT.");
413 die("Could not open kprobe_events file: %s",
419 /* Get raw string list of current kprobe_events */
420 static struct strlist *get_trace_kprobe_event_rawlist(int fd)
424 char buf[MAX_CMDLEN];
428 sl = strlist__new(true, NULL);
430 fp = fdopen(dup(fd), "r");
432 p = fgets(buf, MAX_CMDLEN, fp);
439 ret = strlist__add(sl, buf);
441 die("strlist__add failed: %s", strerror(-ret));
448 /* Free and zero clear probe_point */
449 static void clear_probe_point(struct probe_point *pp)
461 for (i = 0; i < pp->nr_args; i++)
465 for (i = 0; i < pp->found; i++)
467 memset(pp, 0, sizeof(*pp));
471 static void show_perf_probe_event(const char *event, const char *place,
472 struct probe_point *pp)
477 ret = e_snprintf(buf, 128, "%s:%s", pp->group, event);
479 die("Failed to copy event: %s", strerror(-ret));
480 printf(" %-40s (on %s", buf, place);
482 if (pp->nr_args > 0) {
484 for (i = 0; i < pp->nr_args; i++)
485 printf(" %s", pp->args[i]);
490 /* List up current perf-probe events */
491 void show_perf_probe_events(void)
494 struct probe_point pp;
495 struct strlist *rawlist;
496 struct str_node *ent;
500 memset(&pp, 0, sizeof(pp));
501 fd = open_kprobe_events(O_RDONLY, 0);
502 rawlist = get_trace_kprobe_event_rawlist(fd);
505 strlist__for_each(ent, rawlist) {
506 parse_trace_kprobe_event(ent->s, &pp);
507 /* Synthesize only event probe point */
508 synthesize_perf_probe_point(&pp);
510 show_perf_probe_event(pp.event, pp.probes[0], &pp);
511 clear_probe_point(&pp);
514 strlist__delete(rawlist);
517 /* Get current perf-probe event names */
518 static struct strlist *get_perf_event_names(int fd, bool include_group)
521 struct strlist *sl, *rawlist;
522 struct str_node *ent;
523 struct probe_point pp;
525 memset(&pp, 0, sizeof(pp));
526 rawlist = get_trace_kprobe_event_rawlist(fd);
528 sl = strlist__new(true, NULL);
529 strlist__for_each(ent, rawlist) {
530 parse_trace_kprobe_event(ent->s, &pp);
532 if (e_snprintf(buf, 128, "%s:%s", pp.group,
534 die("Failed to copy group:event name.");
535 strlist__add(sl, buf);
537 strlist__add(sl, pp.event);
538 clear_probe_point(&pp);
541 strlist__delete(rawlist);
546 static void write_trace_kprobe_event(int fd, const char *buf)
550 pr_debug("Writing event: %s\n", buf);
551 ret = write(fd, buf, strlen(buf));
553 die("Failed to write event: %s", strerror(errno));
556 static void get_new_event_name(char *buf, size_t len, const char *base,
557 struct strlist *namelist, bool allow_suffix)
562 ret = e_snprintf(buf, len, "%s", base);
564 die("snprintf() failed: %s", strerror(-ret));
565 if (!strlist__has_entry(namelist, buf))
569 pr_warning("Error: event \"%s\" already exists. "
570 "(Use -f to force duplicates.)\n", base);
571 die("Can't add new event.");
574 /* Try to add suffix */
575 for (i = 1; i < MAX_EVENT_INDEX; i++) {
576 ret = e_snprintf(buf, len, "%s_%d", base, i);
578 die("snprintf() failed: %s", strerror(-ret));
579 if (!strlist__has_entry(namelist, buf))
582 if (i == MAX_EVENT_INDEX)
583 die("Too many events are on the same function.");
586 void add_trace_kprobe_events(struct probe_point *probes, int nr_probes,
590 struct probe_point *pp;
591 char buf[MAX_CMDLEN];
593 struct strlist *namelist;
596 fd = open_kprobe_events(O_RDWR, O_APPEND);
597 /* Get current event names */
598 namelist = get_perf_event_names(fd, false);
600 for (j = 0; j < nr_probes; j++) {
603 pp->event = strdup(pp->function);
605 pp->group = strdup(PERFPROBE_GROUP);
606 DIE_IF(!pp->event || !pp->group);
607 /* If force_add is true, suffix search is allowed */
608 allow_suffix = force_add;
609 for (i = 0; i < pp->found; i++) {
610 /* Get an unused new event name */
611 get_new_event_name(event, 64, pp->event, namelist,
613 snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s\n",
614 pp->retprobe ? 'r' : 'p',
617 write_trace_kprobe_event(fd, buf);
618 printf("Added new event:\n");
619 /* Get the first parameter (probe-point) */
620 sscanf(pp->probes[i], "%s", buf);
621 show_perf_probe_event(event, buf, pp);
622 /* Add added event name to namelist */
623 strlist__add(namelist, event);
625 * Probes after the first probe which comes from same
626 * user input are always allowed to add suffix, because
627 * there might be several addresses corresponding to
633 /* Show how to use the event. */
634 printf("\nYou can now use it on all perf tools, such as:\n\n");
635 printf("\tperf record -e %s:%s -a sleep 1\n\n", PERFPROBE_GROUP, event);
637 strlist__delete(namelist);
641 static void __del_trace_kprobe_event(int fd, struct str_node *ent)
646 /* Convert from perf-probe event to trace-kprobe event */
647 if (e_snprintf(buf, 128, "-:%s", ent->s) < 0)
648 die("Failed to copy event.");
649 p = strchr(buf + 2, ':');
651 die("Internal error: %s should have ':' but not.", ent->s);
654 write_trace_kprobe_event(fd, buf);
655 printf("Remove event: %s\n", ent->s);
658 static void del_trace_kprobe_event(int fd, const char *group,
659 const char *event, struct strlist *namelist)
662 struct str_node *ent, *n;
665 if (e_snprintf(buf, 128, "%s:%s", group, event) < 0)
666 die("Failed to copy event.");
668 if (strpbrk(buf, "*?")) { /* Glob-exp */
669 strlist__for_each_safe(ent, n, namelist)
670 if (strglobmatch(ent->s, buf)) {
672 __del_trace_kprobe_event(fd, ent);
673 strlist__remove(namelist, ent);
676 ent = strlist__find(namelist, buf);
679 __del_trace_kprobe_event(fd, ent);
680 strlist__remove(namelist, ent);
684 pr_info("Info: event \"%s\" does not exist, could not remove it.\n", buf);
687 void del_trace_kprobe_events(struct strlist *dellist)
690 const char *group, *event;
692 struct str_node *ent;
693 struct strlist *namelist;
695 fd = open_kprobe_events(O_RDWR, O_APPEND);
696 /* Get current event names */
697 namelist = get_perf_event_names(fd, true);
699 strlist__for_each(ent, dellist) {
700 str = strdup(ent->s);
702 die("Failed to copy event.");
703 pr_debug("Parsing: %s\n", str);
704 p = strchr(str, ':');
713 pr_debug("Group: %s, Event: %s\n", group, event);
714 del_trace_kprobe_event(fd, group, event, namelist);
717 strlist__delete(namelist);
721 #define LINEBUF_SIZE 256
723 static void show_one_line(FILE *fp, unsigned int l, bool skip, bool show_num)
725 char buf[LINEBUF_SIZE];
726 const char *color = PERF_COLOR_BLUE;
728 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
732 fprintf(stdout, "%7u %s", l, buf);
734 color_fprintf(stdout, color, " %s", buf);
737 while (strlen(buf) == LINEBUF_SIZE - 1 &&
738 buf[LINEBUF_SIZE - 2] != '\n') {
739 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
743 fprintf(stdout, "%s", buf);
745 color_fprintf(stdout, color, "%s", buf);
751 die("Source file is shorter than expected.");
753 die("File read error: %s", strerror(errno));
756 void show_line_range(struct line_range *lr)
759 struct line_node *ln;
765 fprintf(stdout, "<%s:%d>\n", lr->function,
766 lr->start - lr->offset);
768 fprintf(stdout, "<%s:%d>\n", lr->file, lr->start);
770 fp = fopen(lr->path, "r");
772 die("Failed to open %s: %s", lr->path, strerror(errno));
773 /* Skip to starting line number */
774 while (l < lr->start)
775 show_one_line(fp, l++, true, false);
777 list_for_each_entry(ln, &lr->line_list, list) {
779 show_one_line(fp, (l++) - lr->offset, false, false);
780 show_one_line(fp, (l++) - lr->offset, false, true);