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>
40 #include "parse-events.h" /* For debugfs_path */
41 #include "probe-event.h"
43 #define MAX_CMDLEN 256
44 #define MAX_PROBE_ARGS 128
45 #define PERFPROBE_GROUP "probe"
47 #define semantic_error(msg ...) die("Semantic error :" msg)
49 /* If there is no space to write, returns -E2BIG. */
50 static int e_snprintf(char *str, size_t size, const char *format, ...)
51 __attribute__((format(printf, 3, 4)));
53 static int e_snprintf(char *str, size_t size, const char *format, ...)
58 ret = vsnprintf(str, size, format, ap);
65 /* Check the name is good for event/group */
66 static bool check_event_name(const char *name)
68 if (!isalpha(*name) && *name != '_')
70 while (*++name != '\0') {
71 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
77 /* Parse probepoint definition. */
78 static void parse_perf_probe_probepoint(char *arg, struct probe_point *pp)
84 * perf probe [EVENT=]SRC:LN
85 * perf probe [EVENT=]FUNC[+OFFS|%return][@SRC]
87 * TODO:Group name support
90 ptr = strchr(arg, '=');
91 if (ptr) { /* Event name */
94 ptr = strchr(arg, ':');
95 if (ptr) /* Group name is not supported yet. */
96 semantic_error("Group name is not supported yet.");
97 if (!check_event_name(arg))
98 semantic_error("%s is bad for event name -it must "
99 "follow C symbol-naming rule.", arg);
100 pp->event = strdup(arg);
104 ptr = strpbrk(arg, ":+@%");
110 /* Check arg is function or file and copy it */
111 if (strchr(arg, '.')) /* File */
112 pp->file = strdup(arg);
114 pp->function = strdup(arg);
115 DIE_IF(pp->file == NULL && pp->function == NULL);
117 /* Parse other options */
121 ptr = strpbrk(arg, ":+@%");
127 case ':': /* Line number */
128 pp->line = strtoul(arg, &tmp, 0);
130 semantic_error("There is non-digit charactor"
133 case '+': /* Byte offset from a symbol */
134 pp->offset = strtoul(arg, &tmp, 0);
136 semantic_error("There is non-digit charactor"
139 case '@': /* File name */
141 semantic_error("SRC@SRC is not allowed.");
142 pp->file = strdup(arg);
143 DIE_IF(pp->file == NULL);
145 semantic_error("@SRC must be the last "
148 case '%': /* Probe places */
149 if (strcmp(arg, "return") == 0) {
151 } else /* Others not supported yet */
152 semantic_error("%%%s is not supported.", arg);
155 DIE_IF("Program has a bug.");
160 /* Exclusion check */
161 if (pp->line && pp->offset)
162 semantic_error("Offset can't be used with line number.");
164 if (!pp->line && pp->file && !pp->function)
165 semantic_error("File always requires line number.");
167 if (pp->offset && !pp->function)
168 semantic_error("Offset requires an entry function.");
170 if (pp->retprobe && !pp->function)
171 semantic_error("Return probe requires an entry function.");
173 if ((pp->offset || pp->line) && pp->retprobe)
174 semantic_error("Offset/Line can't be used with return probe.");
176 pr_debug("symbol:%s file:%s line:%d offset:%d, return:%d\n",
177 pp->function, pp->file, pp->line, pp->offset, pp->retprobe);
180 /* Parse perf-probe event definition */
181 void parse_perf_probe_event(const char *str, struct probe_point *pp,
189 argv = argv_split(str, &argc);
191 die("argv_split failed.");
192 if (argc > MAX_PROBE_ARGS + 1)
193 semantic_error("Too many arguments");
195 /* Parse probe point */
196 parse_perf_probe_probepoint(argv[0], pp);
197 if (pp->file || pp->line)
200 /* Copy arguments and ensure return probe has no C argument */
201 pp->nr_args = argc - 1;
202 pp->args = zalloc(sizeof(char *) * pp->nr_args);
203 for (i = 0; i < pp->nr_args; i++) {
204 pp->args[i] = strdup(argv[i + 1]);
206 die("Failed to copy argument.");
207 if (is_c_varname(pp->args[i])) {
209 semantic_error("You can't specify local"
210 " variable for kretprobe");
218 /* Parse kprobe_events event into struct probe_point */
219 void parse_trace_kprobe_event(const char *str, struct probe_point *pp)
226 pr_debug("Parsing kprobe_events: %s\n", str);
227 argv = argv_split(str, &argc);
229 die("argv_split failed.");
231 semantic_error("Too less arguments.");
233 /* Scan event and group name. */
234 ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]",
235 &pr, (float *)(void *)&pp->group,
236 (float *)(void *)&pp->event);
238 semantic_error("Failed to parse event name: %s", argv[0]);
239 pr_debug("Group:%s Event:%s probe:%c\n", pp->group, pp->event, pr);
241 pp->retprobe = (pr == 'r');
243 /* Scan function name and offset */
244 ret = sscanf(argv[1], "%a[^+]+%d", (float *)(void *)&pp->function,
249 /* kprobe_events doesn't have this information */
253 pp->nr_args = argc - 2;
254 pp->args = zalloc(sizeof(char *) * pp->nr_args);
255 for (i = 0; i < pp->nr_args; i++) {
256 p = strchr(argv[i + 2], '=');
257 if (p) /* We don't need which register is assigned. */
259 pp->args[i] = strdup(argv[i + 2]);
261 die("Failed to copy argument.");
267 /* Synthesize only probe point (not argument) */
268 int synthesize_perf_probe_point(struct probe_point *pp)
271 char offs[64] = "", line[64] = "";
274 pp->probes[0] = buf = zalloc(MAX_CMDLEN);
276 die("Failed to allocate memory by zalloc.");
278 ret = e_snprintf(offs, 64, "+%d", pp->offset);
283 ret = e_snprintf(line, 64, ":%d", pp->line);
289 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s", pp->function,
290 offs, pp->retprobe ? "%return" : "", line);
292 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", pp->file, line);
296 pp->probes[0] = NULL;
301 int synthesize_perf_probe_event(struct probe_point *pp)
306 len = synthesize_perf_probe_point(pp);
311 for (i = 0; i < pp->nr_args; i++) {
312 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
323 pp->probes[0] = NULL;
328 int synthesize_trace_kprobe_event(struct probe_point *pp)
333 pp->probes[0] = buf = zalloc(MAX_CMDLEN);
335 die("Failed to allocate memory by zalloc.");
336 ret = e_snprintf(buf, MAX_CMDLEN, "%s+%d", pp->function, pp->offset);
341 for (i = 0; i < pp->nr_args; i++) {
342 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
353 pp->probes[0] = NULL;
358 static int open_kprobe_events(int flags, int mode)
363 ret = e_snprintf(buf, PATH_MAX, "%s/../kprobe_events", debugfs_path);
365 die("Failed to make kprobe_events path.");
367 ret = open(buf, flags, mode);
370 die("kprobe_events file does not exist -"
371 " please rebuild with CONFIG_KPROBE_TRACER.");
373 die("Could not open kprobe_events file: %s",
379 /* Get raw string list of current kprobe_events */
380 static struct strlist *get_trace_kprobe_event_rawlist(int fd)
384 char buf[MAX_CMDLEN];
388 sl = strlist__new(true, NULL);
390 fp = fdopen(dup(fd), "r");
392 p = fgets(buf, MAX_CMDLEN, fp);
399 ret = strlist__add(sl, buf);
401 die("strlist__add failed: %s", strerror(-ret));
408 /* Free and zero clear probe_point */
409 static void clear_probe_point(struct probe_point *pp)
421 for (i = 0; i < pp->nr_args; i++)
425 for (i = 0; i < pp->found; i++)
427 memset(pp, 0, sizeof(*pp));
431 static void show_perf_probe_event(const char *event, const char *place,
432 struct probe_point *pp)
437 ret = e_snprintf(buf, 128, "%s:%s", pp->group, event);
439 die("Failed to copy event: %s", strerror(-ret));
440 printf(" %-40s (on %s", buf, place);
442 if (pp->nr_args > 0) {
444 for (i = 0; i < pp->nr_args; i++)
445 printf(" %s", pp->args[i]);
450 /* List up current perf-probe events */
451 void show_perf_probe_events(void)
454 struct probe_point pp;
455 struct strlist *rawlist;
456 struct str_node *ent;
458 fd = open_kprobe_events(O_RDONLY, 0);
459 rawlist = get_trace_kprobe_event_rawlist(fd);
462 strlist__for_each(ent, rawlist) {
463 parse_trace_kprobe_event(ent->s, &pp);
464 /* Synthesize only event probe point */
465 synthesize_perf_probe_point(&pp);
467 show_perf_probe_event(pp.event, pp.probes[0], &pp);
468 clear_probe_point(&pp);
471 strlist__delete(rawlist);
474 /* Get current perf-probe event names */
475 static struct strlist *get_perf_event_names(int fd, bool include_group)
478 struct strlist *sl, *rawlist;
479 struct str_node *ent;
480 struct probe_point pp;
482 memset(&pp, 0, sizeof(pp));
483 rawlist = get_trace_kprobe_event_rawlist(fd);
485 sl = strlist__new(true, NULL);
486 strlist__for_each(ent, rawlist) {
487 parse_trace_kprobe_event(ent->s, &pp);
489 if (e_snprintf(buf, 128, "%s:%s", pp.group,
491 die("Failed to copy group:event name.");
492 strlist__add(sl, buf);
494 strlist__add(sl, pp.event);
495 clear_probe_point(&pp);
498 strlist__delete(rawlist);
503 static void write_trace_kprobe_event(int fd, const char *buf)
507 pr_debug("Writing event: %s\n", buf);
508 ret = write(fd, buf, strlen(buf));
510 die("Failed to write event: %s", strerror(errno));
513 static void get_new_event_name(char *buf, size_t len, const char *base,
514 struct strlist *namelist, bool allow_suffix)
519 ret = e_snprintf(buf, len, "%s", base);
521 die("snprintf() failed: %s", strerror(-ret));
522 if (!strlist__has_entry(namelist, buf))
526 pr_warning("Error: event \"%s\" already exists. "
527 "(Use -f to force duplicates.)\n", base);
528 die("Can't add new event.");
531 /* Try to add suffix */
532 for (i = 1; i < MAX_EVENT_INDEX; i++) {
533 ret = e_snprintf(buf, len, "%s_%d", base, i);
535 die("snprintf() failed: %s", strerror(-ret));
536 if (!strlist__has_entry(namelist, buf))
539 if (i == MAX_EVENT_INDEX)
540 die("Too many events are on the same function.");
543 void add_trace_kprobe_events(struct probe_point *probes, int nr_probes,
547 struct probe_point *pp;
548 char buf[MAX_CMDLEN];
550 struct strlist *namelist;
553 fd = open_kprobe_events(O_RDWR, O_APPEND);
554 /* Get current event names */
555 namelist = get_perf_event_names(fd, false);
557 for (j = 0; j < nr_probes; j++) {
560 pp->event = strdup(pp->function);
562 pp->group = strdup(PERFPROBE_GROUP);
563 DIE_IF(!pp->event || !pp->group);
564 /* If force_add is true, suffix search is allowed */
565 allow_suffix = force_add;
566 for (i = 0; i < pp->found; i++) {
567 /* Get an unused new event name */
568 get_new_event_name(event, 64, pp->event, namelist,
570 snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s\n",
571 pp->retprobe ? 'r' : 'p',
574 write_trace_kprobe_event(fd, buf);
575 printf("Added new event:\n");
576 /* Get the first parameter (probe-point) */
577 sscanf(pp->probes[i], "%s", buf);
578 show_perf_probe_event(event, buf, pp);
579 /* Add added event name to namelist */
580 strlist__add(namelist, event);
582 * Probes after the first probe which comes from same
583 * user input are always allowed to add suffix, because
584 * there might be several addresses corresponding to
590 /* Show how to use the event. */
591 printf("\nYou can now use it on all perf tools, such as:\n\n");
592 printf("\tperf record -e %s:%s -a sleep 1\n\n", PERFPROBE_GROUP, event);
594 strlist__delete(namelist);
598 static void __del_trace_kprobe_event(int fd, struct str_node *ent)
603 /* Convert from perf-probe event to trace-kprobe event */
604 if (e_snprintf(buf, 128, "-:%s", ent->s) < 0)
605 die("Failed to copy event.");
606 p = strchr(buf + 2, ':');
608 die("Internal error: %s should have ':' but not.", ent->s);
611 write_trace_kprobe_event(fd, buf);
612 printf("Remove event: %s\n", ent->s);
615 static void del_trace_kprobe_event(int fd, const char *group,
616 const char *event, struct strlist *namelist)
619 struct str_node *ent, *n;
622 if (e_snprintf(buf, 128, "%s:%s", group, event) < 0)
623 die("Failed to copy event.");
625 if (strpbrk(buf, "*?")) { /* Glob-exp */
626 strlist__for_each_safe(ent, n, namelist)
627 if (strglobmatch(ent->s, buf)) {
629 __del_trace_kprobe_event(fd, ent);
630 strlist__remove(namelist, ent);
633 ent = strlist__find(namelist, buf);
636 __del_trace_kprobe_event(fd, ent);
637 strlist__remove(namelist, ent);
641 pr_info("Info: event \"%s\" does not exist, could not remove it.\n", buf);
644 void del_trace_kprobe_events(struct strlist *dellist)
647 const char *group, *event;
649 struct str_node *ent;
650 struct strlist *namelist;
652 fd = open_kprobe_events(O_RDWR, O_APPEND);
653 /* Get current event names */
654 namelist = get_perf_event_names(fd, true);
656 strlist__for_each(ent, dellist) {
657 str = strdup(ent->s);
659 die("Failed to copy event.");
660 pr_debug("Parsing: %s\n", str);
661 p = strchr(str, ':');
670 pr_debug("Group: %s, Event: %s\n", group, event);
671 del_trace_kprobe_event(fd, group, event, namelist);
674 strlist__delete(namelist);