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|;PTN]
123 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
125 * TODO:Group name support
128 ptr = strpbrk(arg, ";=@+%");
129 if (ptr && *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 if (c == ';') { /* Lazy pattern must be the last part */
160 pp->lazy_line = strdup(arg);
163 ptr = strpbrk(arg, ";:+@%");
169 case ':': /* Line number */
170 pp->line = strtoul(arg, &tmp, 0);
172 semantic_error("There is non-digit char"
175 case '+': /* Byte offset from a symbol */
176 pp->offset = strtoul(arg, &tmp, 0);
178 semantic_error("There is non-digit character"
181 case '@': /* File name */
183 semantic_error("SRC@SRC is not allowed.");
184 pp->file = strdup(arg);
185 DIE_IF(pp->file == NULL);
187 case '%': /* Probe places */
188 if (strcmp(arg, "return") == 0) {
190 } else /* Others not supported yet */
191 semantic_error("%%%s is not supported.", arg);
194 DIE_IF("Program has a bug.");
199 /* Exclusion check */
200 if (pp->lazy_line && pp->line)
201 semantic_error("Lazy pattern can't be used with line number.");
203 if (pp->lazy_line && pp->offset)
204 semantic_error("Lazy pattern can't be used with offset.");
206 if (pp->line && pp->offset)
207 semantic_error("Offset can't be used with line number.");
209 if (!pp->line && !pp->lazy_line && pp->file && !pp->function)
210 semantic_error("File always requires line number or "
213 if (pp->offset && !pp->function)
214 semantic_error("Offset requires an entry function.");
216 if (pp->retprobe && !pp->function)
217 semantic_error("Return probe requires an entry function.");
219 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe)
220 semantic_error("Offset/Line/Lazy pattern can't be used with "
223 pr_debug("symbol:%s file:%s line:%d offset:%d return:%d lazy:%s\n",
224 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
228 /* Parse perf-probe event definition */
229 void parse_perf_probe_event(const char *str, struct probe_point *pp,
237 argv = argv_split(str, &argc);
239 die("argv_split failed.");
240 if (argc > MAX_PROBE_ARGS + 1)
241 semantic_error("Too many arguments");
243 /* Parse probe point */
244 parse_perf_probe_probepoint(argv[0], pp);
245 if (pp->file || pp->line || pp->lazy_line)
248 /* Copy arguments and ensure return probe has no C argument */
249 pp->nr_args = argc - 1;
250 pp->args = zalloc(sizeof(char *) * pp->nr_args);
251 for (i = 0; i < pp->nr_args; i++) {
252 pp->args[i] = strdup(argv[i + 1]);
254 die("Failed to copy argument.");
255 if (is_c_varname(pp->args[i])) {
257 semantic_error("You can't specify local"
258 " variable for kretprobe");
266 /* Parse kprobe_events event into struct probe_point */
267 void parse_trace_kprobe_event(const char *str, struct probe_point *pp)
274 pr_debug("Parsing kprobe_events: %s\n", str);
275 argv = argv_split(str, &argc);
277 die("argv_split failed.");
279 semantic_error("Too less arguments.");
281 /* Scan event and group name. */
282 ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]",
283 &pr, (float *)(void *)&pp->group,
284 (float *)(void *)&pp->event);
286 semantic_error("Failed to parse event name: %s", argv[0]);
287 pr_debug("Group:%s Event:%s probe:%c\n", pp->group, pp->event, pr);
289 pp->retprobe = (pr == 'r');
291 /* Scan function name and offset */
292 ret = sscanf(argv[1], "%a[^+]+%d", (float *)(void *)&pp->function,
297 /* kprobe_events doesn't have this information */
301 pp->nr_args = argc - 2;
302 pp->args = zalloc(sizeof(char *) * pp->nr_args);
303 for (i = 0; i < pp->nr_args; i++) {
304 p = strchr(argv[i + 2], '=');
305 if (p) /* We don't need which register is assigned. */
307 pp->args[i] = strdup(argv[i + 2]);
309 die("Failed to copy argument.");
315 /* Synthesize only probe point (not argument) */
316 int synthesize_perf_probe_point(struct probe_point *pp)
319 char offs[64] = "", line[64] = "";
322 pp->probes[0] = buf = zalloc(MAX_CMDLEN);
325 die("Failed to allocate memory by zalloc.");
327 ret = e_snprintf(offs, 64, "+%d", pp->offset);
332 ret = e_snprintf(line, 64, ":%d", pp->line);
338 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s", pp->function,
339 offs, pp->retprobe ? "%return" : "", line);
341 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", pp->file, line);
345 pp->probes[0] = NULL;
351 int synthesize_perf_probe_event(struct probe_point *pp)
356 len = synthesize_perf_probe_point(pp);
361 for (i = 0; i < pp->nr_args; i++) {
362 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
373 pp->probes[0] = NULL;
378 int synthesize_trace_kprobe_event(struct probe_point *pp)
383 pp->probes[0] = buf = zalloc(MAX_CMDLEN);
385 die("Failed to allocate memory by zalloc.");
386 ret = e_snprintf(buf, MAX_CMDLEN, "%s+%d", pp->function, pp->offset);
391 for (i = 0; i < pp->nr_args; i++) {
392 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
403 pp->probes[0] = NULL;
408 static int open_kprobe_events(int flags, int mode)
413 ret = e_snprintf(buf, PATH_MAX, "%s/../kprobe_events", debugfs_path);
415 die("Failed to make kprobe_events path.");
417 ret = open(buf, flags, mode);
420 die("kprobe_events file does not exist -"
421 " please rebuild with CONFIG_KPROBE_EVENT.");
423 die("Could not open kprobe_events file: %s",
429 /* Get raw string list of current kprobe_events */
430 static struct strlist *get_trace_kprobe_event_rawlist(int fd)
434 char buf[MAX_CMDLEN];
438 sl = strlist__new(true, NULL);
440 fp = fdopen(dup(fd), "r");
442 p = fgets(buf, MAX_CMDLEN, fp);
449 ret = strlist__add(sl, buf);
451 die("strlist__add failed: %s", strerror(-ret));
458 /* Free and zero clear probe_point */
459 static void clear_probe_point(struct probe_point *pp)
473 for (i = 0; i < pp->nr_args; i++)
477 for (i = 0; i < pp->found; i++)
479 memset(pp, 0, sizeof(*pp));
483 static void show_perf_probe_event(const char *event, const char *place,
484 struct probe_point *pp)
489 ret = e_snprintf(buf, 128, "%s:%s", pp->group, event);
491 die("Failed to copy event: %s", strerror(-ret));
492 printf(" %-40s (on %s", buf, place);
494 if (pp->nr_args > 0) {
496 for (i = 0; i < pp->nr_args; i++)
497 printf(" %s", pp->args[i]);
502 /* List up current perf-probe events */
503 void show_perf_probe_events(void)
506 struct probe_point pp;
507 struct strlist *rawlist;
508 struct str_node *ent;
511 memset(&pp, 0, sizeof(pp));
513 fd = open_kprobe_events(O_RDONLY, 0);
514 rawlist = get_trace_kprobe_event_rawlist(fd);
517 strlist__for_each(ent, rawlist) {
518 parse_trace_kprobe_event(ent->s, &pp);
519 /* Synthesize only event probe point */
520 synthesize_perf_probe_point(&pp);
522 show_perf_probe_event(pp.event, pp.probes[0], &pp);
523 clear_probe_point(&pp);
526 strlist__delete(rawlist);
529 /* Get current perf-probe event names */
530 static struct strlist *get_perf_event_names(int fd, bool include_group)
533 struct strlist *sl, *rawlist;
534 struct str_node *ent;
535 struct probe_point pp;
537 memset(&pp, 0, sizeof(pp));
538 rawlist = get_trace_kprobe_event_rawlist(fd);
540 sl = strlist__new(true, NULL);
541 strlist__for_each(ent, rawlist) {
542 parse_trace_kprobe_event(ent->s, &pp);
544 if (e_snprintf(buf, 128, "%s:%s", pp.group,
546 die("Failed to copy group:event name.");
547 strlist__add(sl, buf);
549 strlist__add(sl, pp.event);
550 clear_probe_point(&pp);
553 strlist__delete(rawlist);
558 static void write_trace_kprobe_event(int fd, const char *buf)
562 pr_debug("Writing event: %s\n", buf);
563 ret = write(fd, buf, strlen(buf));
565 die("Failed to write event: %s", strerror(errno));
568 static void get_new_event_name(char *buf, size_t len, const char *base,
569 struct strlist *namelist, bool allow_suffix)
574 ret = e_snprintf(buf, len, "%s", base);
576 die("snprintf() failed: %s", strerror(-ret));
577 if (!strlist__has_entry(namelist, buf))
581 pr_warning("Error: event \"%s\" already exists. "
582 "(Use -f to force duplicates.)\n", base);
583 die("Can't add new event.");
586 /* Try to add suffix */
587 for (i = 1; i < MAX_EVENT_INDEX; i++) {
588 ret = e_snprintf(buf, len, "%s_%d", base, i);
590 die("snprintf() failed: %s", strerror(-ret));
591 if (!strlist__has_entry(namelist, buf))
594 if (i == MAX_EVENT_INDEX)
595 die("Too many events are on the same function.");
598 void add_trace_kprobe_events(struct probe_point *probes, int nr_probes,
602 struct probe_point *pp;
603 char buf[MAX_CMDLEN];
605 struct strlist *namelist;
608 fd = open_kprobe_events(O_RDWR, O_APPEND);
609 /* Get current event names */
610 namelist = get_perf_event_names(fd, false);
612 for (j = 0; j < nr_probes; j++) {
615 pp->event = strdup(pp->function);
617 pp->group = strdup(PERFPROBE_GROUP);
618 DIE_IF(!pp->event || !pp->group);
619 /* If force_add is true, suffix search is allowed */
620 allow_suffix = force_add;
621 for (i = 0; i < pp->found; i++) {
622 /* Get an unused new event name */
623 get_new_event_name(event, 64, pp->event, namelist,
625 snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s\n",
626 pp->retprobe ? 'r' : 'p',
629 write_trace_kprobe_event(fd, buf);
630 printf("Added new event:\n");
631 /* Get the first parameter (probe-point) */
632 sscanf(pp->probes[i], "%s", buf);
633 show_perf_probe_event(event, buf, pp);
634 /* Add added event name to namelist */
635 strlist__add(namelist, event);
637 * Probes after the first probe which comes from same
638 * user input are always allowed to add suffix, because
639 * there might be several addresses corresponding to
645 /* Show how to use the event. */
646 printf("\nYou can now use it on all perf tools, such as:\n\n");
647 printf("\tperf record -e %s:%s -a sleep 1\n\n", PERFPROBE_GROUP, event);
649 strlist__delete(namelist);
653 static void __del_trace_kprobe_event(int fd, struct str_node *ent)
658 /* Convert from perf-probe event to trace-kprobe event */
659 if (e_snprintf(buf, 128, "-:%s", ent->s) < 0)
660 die("Failed to copy event.");
661 p = strchr(buf + 2, ':');
663 die("Internal error: %s should have ':' but not.", ent->s);
666 write_trace_kprobe_event(fd, buf);
667 printf("Remove event: %s\n", ent->s);
670 static void del_trace_kprobe_event(int fd, const char *group,
671 const char *event, struct strlist *namelist)
674 struct str_node *ent, *n;
677 if (e_snprintf(buf, 128, "%s:%s", group, event) < 0)
678 die("Failed to copy event.");
680 if (strpbrk(buf, "*?")) { /* Glob-exp */
681 strlist__for_each_safe(ent, n, namelist)
682 if (strglobmatch(ent->s, buf)) {
684 __del_trace_kprobe_event(fd, ent);
685 strlist__remove(namelist, ent);
688 ent = strlist__find(namelist, buf);
691 __del_trace_kprobe_event(fd, ent);
692 strlist__remove(namelist, ent);
696 pr_info("Info: event \"%s\" does not exist, could not remove it.\n", buf);
699 void del_trace_kprobe_events(struct strlist *dellist)
702 const char *group, *event;
704 struct str_node *ent;
705 struct strlist *namelist;
707 fd = open_kprobe_events(O_RDWR, O_APPEND);
708 /* Get current event names */
709 namelist = get_perf_event_names(fd, true);
711 strlist__for_each(ent, dellist) {
712 str = strdup(ent->s);
714 die("Failed to copy event.");
715 pr_debug("Parsing: %s\n", str);
716 p = strchr(str, ':');
725 pr_debug("Group: %s, Event: %s\n", group, event);
726 del_trace_kprobe_event(fd, group, event, namelist);
729 strlist__delete(namelist);
733 #define LINEBUF_SIZE 256
734 #define NR_ADDITIONAL_LINES 2
736 static void show_one_line(FILE *fp, unsigned int l, bool skip, bool show_num)
738 char buf[LINEBUF_SIZE];
739 const char *color = PERF_COLOR_BLUE;
741 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
745 fprintf(stdout, "%7u %s", l, buf);
747 color_fprintf(stdout, color, " %s", buf);
750 while (strlen(buf) == LINEBUF_SIZE - 1 &&
751 buf[LINEBUF_SIZE - 2] != '\n') {
752 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
756 fprintf(stdout, "%s", buf);
758 color_fprintf(stdout, color, "%s", buf);
764 die("Source file is shorter than expected.");
766 die("File read error: %s", strerror(errno));
769 void show_line_range(struct line_range *lr)
772 struct line_node *ln;
778 fprintf(stdout, "<%s:%d>\n", lr->function,
779 lr->start - lr->offset);
781 fprintf(stdout, "<%s:%d>\n", lr->file, lr->start);
783 fp = fopen(lr->path, "r");
785 die("Failed to open %s: %s", lr->path, strerror(errno));
786 /* Skip to starting line number */
787 while (l < lr->start)
788 show_one_line(fp, l++, true, false);
790 list_for_each_entry(ln, &lr->line_list, list) {
792 show_one_line(fp, (l++) - lr->offset, false, false);
793 show_one_line(fp, (l++) - lr->offset, false, true);
796 if (lr->end == INT_MAX)
797 lr->end = l + NR_ADDITIONAL_LINES;
798 while (l < lr->end && !feof(fp))
799 show_one_line(fp, (l++) - lr->offset, false, false);