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 /* Parse probepoint definition. */
66 static void parse_perf_probe_probepoint(char *arg, struct probe_point *pp)
72 * perf probe [EVENT=]SRC:LN
73 * perf probe [EVENT=]FUNC[+OFFS|%return][@SRC]
75 * TODO:Group name support
78 ptr = strchr(arg, '=');
79 if (ptr) { /* Event name */
82 ptr = strchr(arg, ':');
83 if (ptr) /* Group name is not supported yet. */
84 semantic_error("Group name is not supported yet.");
85 pp->event = strdup(arg);
89 ptr = strpbrk(arg, ":+@%");
95 /* Check arg is function or file and copy it */
96 if (strchr(arg, '.')) /* File */
97 pp->file = strdup(arg);
99 pp->function = strdup(arg);
100 DIE_IF(pp->file == NULL && pp->function == NULL);
102 /* Parse other options */
106 ptr = strpbrk(arg, ":+@%");
112 case ':': /* Line number */
113 pp->line = strtoul(arg, &tmp, 0);
115 semantic_error("There is non-digit charactor"
118 case '+': /* Byte offset from a symbol */
119 pp->offset = strtoul(arg, &tmp, 0);
121 semantic_error("There is non-digit charactor"
124 case '@': /* File name */
126 semantic_error("SRC@SRC is not allowed.");
127 pp->file = strdup(arg);
128 DIE_IF(pp->file == NULL);
130 semantic_error("@SRC must be the last "
133 case '%': /* Probe places */
134 if (strcmp(arg, "return") == 0) {
136 } else /* Others not supported yet */
137 semantic_error("%%%s is not supported.", arg);
140 DIE_IF("Program has a bug.");
145 /* Exclusion check */
146 if (pp->line && pp->offset)
147 semantic_error("Offset can't be used with line number.");
149 if (!pp->line && pp->file && !pp->function)
150 semantic_error("File always requires line number.");
152 if (pp->offset && !pp->function)
153 semantic_error("Offset requires an entry function.");
155 if (pp->retprobe && !pp->function)
156 semantic_error("Return probe requires an entry function.");
158 if ((pp->offset || pp->line) && pp->retprobe)
159 semantic_error("Offset/Line can't be used with return probe.");
161 pr_debug("symbol:%s file:%s line:%d offset:%d, return:%d\n",
162 pp->function, pp->file, pp->line, pp->offset, pp->retprobe);
165 /* Parse perf-probe event definition */
166 void parse_perf_probe_event(const char *str, struct probe_point *pp,
174 argv = argv_split(str, &argc);
176 die("argv_split failed.");
177 if (argc > MAX_PROBE_ARGS + 1)
178 semantic_error("Too many arguments");
180 /* Parse probe point */
181 parse_perf_probe_probepoint(argv[0], pp);
182 if (pp->file || pp->line)
185 /* Copy arguments and ensure return probe has no C argument */
186 pp->nr_args = argc - 1;
187 pp->args = zalloc(sizeof(char *) * pp->nr_args);
188 for (i = 0; i < pp->nr_args; i++) {
189 pp->args[i] = strdup(argv[i + 1]);
191 die("Failed to copy argument.");
192 if (is_c_varname(pp->args[i])) {
194 semantic_error("You can't specify local"
195 " variable for kretprobe");
203 /* Parse kprobe_events event into struct probe_point */
204 void parse_trace_kprobe_event(const char *str, struct probe_point *pp)
211 pr_debug("Parsing kprobe_events: %s\n", str);
212 argv = argv_split(str, &argc);
214 die("argv_split failed.");
216 semantic_error("Too less arguments.");
218 /* Scan event and group name. */
219 ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]",
220 &pr, (float *)(void *)&pp->group,
221 (float *)(void *)&pp->event);
223 semantic_error("Failed to parse event name: %s", argv[0]);
224 pr_debug("Group:%s Event:%s probe:%c\n", pp->group, pp->event, pr);
226 pp->retprobe = (pr == 'r');
228 /* Scan function name and offset */
229 ret = sscanf(argv[1], "%a[^+]+%d", (float *)(void *)&pp->function,
234 /* kprobe_events doesn't have this information */
238 pp->nr_args = argc - 2;
239 pp->args = zalloc(sizeof(char *) * pp->nr_args);
240 for (i = 0; i < pp->nr_args; i++) {
241 p = strchr(argv[i + 2], '=');
242 if (p) /* We don't need which register is assigned. */
244 pp->args[i] = strdup(argv[i + 2]);
246 die("Failed to copy argument.");
252 int synthesize_perf_probe_event(struct probe_point *pp)
255 char offs[64] = "", line[64] = "";
258 pp->probes[0] = buf = zalloc(MAX_CMDLEN);
260 die("Failed to allocate memory by zalloc.");
262 ret = e_snprintf(offs, 64, "+%d", pp->offset);
267 ret = e_snprintf(line, 64, ":%d", pp->line);
273 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s", pp->function,
274 offs, pp->retprobe ? "%return" : "", line);
276 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", pp->file, line);
281 for (i = 0; i < pp->nr_args; i++) {
282 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
297 int synthesize_trace_kprobe_event(struct probe_point *pp)
302 pp->probes[0] = buf = zalloc(MAX_CMDLEN);
304 die("Failed to allocate memory by zalloc.");
305 ret = e_snprintf(buf, MAX_CMDLEN, "%s+%d", pp->function, pp->offset);
310 for (i = 0; i < pp->nr_args; i++) {
311 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
326 static int open_kprobe_events(int flags, int mode)
331 ret = e_snprintf(buf, PATH_MAX, "%s/../kprobe_events", debugfs_path);
333 die("Failed to make kprobe_events path.");
335 ret = open(buf, flags, mode);
338 die("kprobe_events file does not exist -"
339 " please rebuild with CONFIG_KPROBE_TRACER.");
341 die("Could not open kprobe_events file: %s",
347 /* Get raw string list of current kprobe_events */
348 static struct strlist *get_trace_kprobe_event_rawlist(int fd)
352 char buf[MAX_CMDLEN];
356 sl = strlist__new(true, NULL);
358 fp = fdopen(dup(fd), "r");
360 p = fgets(buf, MAX_CMDLEN, fp);
367 ret = strlist__add(sl, buf);
369 die("strlist__add failed: %s", strerror(-ret));
376 /* Free and zero clear probe_point */
377 static void clear_probe_point(struct probe_point *pp)
389 for (i = 0; i < pp->nr_args; i++)
393 for (i = 0; i < pp->found; i++)
395 memset(pp, 0, sizeof(*pp));
399 static void show_perf_probe_event(const char *event, const char *place,
400 struct probe_point *pp)
405 ret = e_snprintf(buf, 128, "%s:%s", pp->group, event);
407 die("Failed to copy event: %s", strerror(-ret));
408 printf(" %-40s (on %s", buf, place);
410 if (pp->nr_args > 0) {
412 for (i = 0; i < pp->nr_args; i++)
413 printf(" %s", pp->args[i]);
418 /* List up current perf-probe events */
419 void show_perf_probe_events(void)
422 struct probe_point pp;
423 struct strlist *rawlist;
424 struct str_node *ent;
426 fd = open_kprobe_events(O_RDONLY, 0);
427 rawlist = get_trace_kprobe_event_rawlist(fd);
430 strlist__for_each(ent, rawlist) {
431 parse_trace_kprobe_event(ent->s, &pp);
432 /* Synthesize only event probe point */
435 synthesize_perf_probe_event(&pp);
438 show_perf_probe_event(pp.event, pp.probes[0], &pp);
439 clear_probe_point(&pp);
442 strlist__delete(rawlist);
445 /* Get current perf-probe event names */
446 static struct strlist *get_perf_event_names(int fd, bool include_group)
449 struct strlist *sl, *rawlist;
450 struct str_node *ent;
451 struct probe_point pp;
453 memset(&pp, 0, sizeof(pp));
454 rawlist = get_trace_kprobe_event_rawlist(fd);
456 sl = strlist__new(true, NULL);
457 strlist__for_each(ent, rawlist) {
458 parse_trace_kprobe_event(ent->s, &pp);
460 if (e_snprintf(buf, 128, "%s:%s", pp.group,
462 die("Failed to copy group:event name.");
463 strlist__add(sl, buf);
465 strlist__add(sl, pp.event);
466 clear_probe_point(&pp);
469 strlist__delete(rawlist);
474 static void write_trace_kprobe_event(int fd, const char *buf)
478 pr_debug("Writing event: %s\n", buf);
479 ret = write(fd, buf, strlen(buf));
481 die("Failed to write event: %s", strerror(errno));
484 static void get_new_event_name(char *buf, size_t len, const char *base,
485 struct strlist *namelist, bool allow_suffix)
490 ret = e_snprintf(buf, len, "%s", base);
492 die("snprintf() failed: %s", strerror(-ret));
493 if (!strlist__has_entry(namelist, buf))
497 pr_warning("Error: event \"%s\" already exists. "
498 "(Use -f to force duplicates.)\n", base);
499 die("Can't add new event.");
502 /* Try to add suffix */
503 for (i = 1; i < MAX_EVENT_INDEX; i++) {
504 ret = e_snprintf(buf, len, "%s_%d", base, i);
506 die("snprintf() failed: %s", strerror(-ret));
507 if (!strlist__has_entry(namelist, buf))
510 if (i == MAX_EVENT_INDEX)
511 die("Too many events are on the same function.");
514 void add_trace_kprobe_events(struct probe_point *probes, int nr_probes,
518 struct probe_point *pp;
519 char buf[MAX_CMDLEN];
521 struct strlist *namelist;
524 fd = open_kprobe_events(O_RDWR, O_APPEND);
525 /* Get current event names */
526 namelist = get_perf_event_names(fd, false);
528 for (j = 0; j < nr_probes; j++) {
531 pp->event = strdup(pp->function);
533 pp->group = strdup(PERFPROBE_GROUP);
534 DIE_IF(!pp->event || !pp->group);
535 /* If force_add is true, suffix search is allowed */
536 allow_suffix = force_add;
537 for (i = 0; i < pp->found; i++) {
538 /* Get an unused new event name */
539 get_new_event_name(event, 64, pp->event, namelist,
541 snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s\n",
542 pp->retprobe ? 'r' : 'p',
545 write_trace_kprobe_event(fd, buf);
546 printf("Added new event:\n");
547 /* Get the first parameter (probe-point) */
548 sscanf(pp->probes[i], "%s", buf);
549 show_perf_probe_event(event, buf, pp);
550 /* Add added event name to namelist */
551 strlist__add(namelist, event);
553 * Probes after the first probe which comes from same
554 * user input are always allowed to add suffix, because
555 * there might be several addresses corresponding to
561 /* Show how to use the event. */
562 printf("\nYou can now use it on all perf tools, such as:\n\n");
563 printf("\tperf record -e %s:%s -a sleep 1\n\n", PERFPROBE_GROUP, event);
565 strlist__delete(namelist);
569 static void __del_trace_kprobe_event(int fd, struct str_node *ent)
574 /* Convert from perf-probe event to trace-kprobe event */
575 if (e_snprintf(buf, 128, "-:%s", ent->s) < 0)
576 die("Failed to copy event.");
577 p = strchr(buf + 2, ':');
579 die("Internal error: %s should have ':' but not.", ent->s);
582 write_trace_kprobe_event(fd, buf);
583 printf("Remove event: %s\n", ent->s);
586 static void del_trace_kprobe_event(int fd, const char *group,
587 const char *event, struct strlist *namelist)
590 struct str_node *ent, *n;
593 if (e_snprintf(buf, 128, "%s:%s", group, event) < 0)
594 die("Failed to copy event.");
596 if (strpbrk(buf, "*?")) { /* Glob-exp */
597 strlist__for_each_safe(ent, n, namelist)
598 if (strglobmatch(ent->s, buf)) {
600 __del_trace_kprobe_event(fd, ent);
601 strlist__remove(namelist, ent);
604 ent = strlist__find(namelist, buf);
607 __del_trace_kprobe_event(fd, ent);
608 strlist__remove(namelist, ent);
612 pr_info("Info: event \"%s\" does not exist, could not remove it.\n", buf);
615 void del_trace_kprobe_events(struct strlist *dellist)
618 const char *group, *event;
620 struct str_node *ent;
621 struct strlist *namelist;
623 fd = open_kprobe_events(O_RDWR, O_APPEND);
624 /* Get current event names */
625 namelist = get_perf_event_names(fd, true);
627 strlist__for_each(ent, dellist) {
628 str = strdup(ent->s);
630 die("Failed to copy event.");
631 pr_debug("Parsing: %s\n", str);
632 p = strchr(str, ':');
641 pr_debug("Group: %s, Event: %s\n", group, event);
642 del_trace_kprobe_event(fd, group, event, namelist);
645 strlist__delete(namelist);