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)
73 * perf probe FUNC[+OFFS|%return][@SRC]
76 ptr = strpbrk(arg, ":+@%");
82 /* Check arg is function or file and copy it */
83 if (strchr(arg, '.')) /* File */
84 pp->file = strdup(arg);
86 pp->function = strdup(arg);
87 DIE_IF(pp->file == NULL && pp->function == NULL);
89 /* Parse other options */
93 ptr = strpbrk(arg, ":+@%");
99 case ':': /* Line number */
100 pp->line = strtoul(arg, &tmp, 0);
102 semantic_error("There is non-digit charactor"
105 case '+': /* Byte offset from a symbol */
106 pp->offset = strtoul(arg, &tmp, 0);
108 semantic_error("There is non-digit charactor"
111 case '@': /* File name */
113 semantic_error("SRC@SRC is not allowed.");
114 pp->file = strdup(arg);
115 DIE_IF(pp->file == NULL);
117 semantic_error("@SRC must be the last "
120 case '%': /* Probe places */
121 if (strcmp(arg, "return") == 0) {
123 } else /* Others not supported yet */
124 semantic_error("%%%s is not supported.", arg);
127 DIE_IF("Program has a bug.");
132 /* Exclusion check */
133 if (pp->line && pp->offset)
134 semantic_error("Offset can't be used with line number.");
136 if (!pp->line && pp->file && !pp->function)
137 semantic_error("File always requires line number.");
139 if (pp->offset && !pp->function)
140 semantic_error("Offset requires an entry function.");
142 if (pp->retprobe && !pp->function)
143 semantic_error("Return probe requires an entry function.");
145 if ((pp->offset || pp->line) && pp->retprobe)
146 semantic_error("Offset/Line can't be used with return probe.");
148 pr_debug("symbol:%s file:%s line:%d offset:%d, return:%d\n",
149 pp->function, pp->file, pp->line, pp->offset, pp->retprobe);
152 /* Parse perf-probe event definition */
153 void parse_perf_probe_event(const char *str, struct probe_point *pp,
161 argv = argv_split(str, &argc);
163 die("argv_split failed.");
164 if (argc > MAX_PROBE_ARGS + 1)
165 semantic_error("Too many arguments");
167 /* Parse probe point */
168 parse_perf_probe_probepoint(argv[0], pp);
169 if (pp->file || pp->line)
172 /* Copy arguments and ensure return probe has no C argument */
173 pp->nr_args = argc - 1;
174 pp->args = zalloc(sizeof(char *) * pp->nr_args);
175 for (i = 0; i < pp->nr_args; i++) {
176 pp->args[i] = strdup(argv[i + 1]);
178 die("Failed to copy argument.");
179 if (is_c_varname(pp->args[i])) {
181 semantic_error("You can't specify local"
182 " variable for kretprobe");
190 /* Parse kprobe_events event into struct probe_point */
191 void parse_trace_kprobe_event(const char *str, char **group, char **event,
192 struct probe_point *pp)
199 pr_debug("Parsing kprobe_events: %s\n", str);
200 argv = argv_split(str, &argc);
202 die("argv_split failed.");
204 semantic_error("Too less arguments.");
206 /* Scan event and group name. */
207 ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]",
208 &pr, (float *)(void *)group, (float *)(void *)event);
210 semantic_error("Failed to parse event name: %s", argv[0]);
211 pr_debug("Group:%s Event:%s probe:%c\n", *group, *event, pr);
216 pp->retprobe = (pr == 'r');
218 /* Scan function name and offset */
219 ret = sscanf(argv[1], "%a[^+]+%d", (float *)(void *)&pp->function, &pp->offset);
223 /* kprobe_events doesn't have this information */
227 pp->nr_args = argc - 2;
228 pp->args = zalloc(sizeof(char *) * pp->nr_args);
229 for (i = 0; i < pp->nr_args; i++) {
230 p = strchr(argv[i + 2], '=');
231 if (p) /* We don't need which register is assigned. */
233 pp->args[i] = strdup(argv[i + 2]);
235 die("Failed to copy argument.");
242 int synthesize_perf_probe_event(struct probe_point *pp)
245 char offs[64] = "", line[64] = "";
248 pp->probes[0] = buf = zalloc(MAX_CMDLEN);
250 die("Failed to allocate memory by zalloc.");
252 ret = e_snprintf(offs, 64, "+%d", pp->offset);
257 ret = e_snprintf(line, 64, ":%d", pp->line);
263 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s", pp->function,
264 offs, pp->retprobe ? "%return" : "", line);
266 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", pp->file, line);
271 for (i = 0; i < pp->nr_args; i++) {
272 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
287 int synthesize_trace_kprobe_event(struct probe_point *pp)
292 pp->probes[0] = buf = zalloc(MAX_CMDLEN);
294 die("Failed to allocate memory by zalloc.");
295 ret = e_snprintf(buf, MAX_CMDLEN, "%s+%d", pp->function, pp->offset);
300 for (i = 0; i < pp->nr_args; i++) {
301 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
316 static int open_kprobe_events(int flags, int mode)
321 ret = e_snprintf(buf, PATH_MAX, "%s/../kprobe_events", debugfs_path);
323 die("Failed to make kprobe_events path.");
325 ret = open(buf, flags, mode);
328 die("kprobe_events file does not exist -"
329 " please rebuild with CONFIG_KPROBE_TRACER.");
331 die("Could not open kprobe_events file: %s",
337 /* Get raw string list of current kprobe_events */
338 static struct strlist *get_trace_kprobe_event_rawlist(int fd)
342 char buf[MAX_CMDLEN];
346 sl = strlist__new(true, NULL);
348 fp = fdopen(dup(fd), "r");
350 p = fgets(buf, MAX_CMDLEN, fp);
357 ret = strlist__add(sl, buf);
359 die("strlist__add failed: %s", strerror(-ret));
366 /* Free and zero clear probe_point */
367 static void clear_probe_point(struct probe_point *pp)
375 for (i = 0; i < pp->nr_args; i++)
379 for (i = 0; i < pp->found; i++)
381 memset(pp, 0, sizeof(*pp));
385 static void show_perf_probe_event(const char *group, const char *event,
386 const char *place, struct probe_point *pp)
391 ret = e_snprintf(buf, 128, "%s:%s", group, event);
393 die("Failed to copy event: %s", strerror(-ret));
394 printf(" %-40s (on %s", buf, place);
396 if (pp->nr_args > 0) {
398 for (i = 0; i < pp->nr_args; i++)
399 printf(" %s", pp->args[i]);
404 /* List up current perf-probe events */
405 void show_perf_probe_events(void)
410 struct probe_point pp;
411 struct strlist *rawlist;
412 struct str_node *ent;
414 fd = open_kprobe_events(O_RDONLY, 0);
415 rawlist = get_trace_kprobe_event_rawlist(fd);
418 for (i = 0; i < strlist__nr_entries(rawlist); i++) {
419 ent = strlist__entry(rawlist, i);
420 parse_trace_kprobe_event(ent->s, &group, &event, &pp);
421 /* Synthesize only event probe point */
424 synthesize_perf_probe_event(&pp);
427 show_perf_probe_event(group, event, pp.probes[0], &pp);
430 clear_probe_point(&pp);
433 strlist__delete(rawlist);
436 /* Get current perf-probe event names */
437 static struct strlist *get_perf_event_names(int fd, bool include_group)
442 struct strlist *sl, *rawlist;
443 struct str_node *ent;
445 rawlist = get_trace_kprobe_event_rawlist(fd);
447 sl = strlist__new(true, NULL);
448 for (i = 0; i < strlist__nr_entries(rawlist); i++) {
449 ent = strlist__entry(rawlist, i);
450 parse_trace_kprobe_event(ent->s, &group, &event, NULL);
452 if (e_snprintf(buf, 128, "%s:%s", group, event) < 0)
453 die("Failed to copy group:event name.");
454 strlist__add(sl, buf);
456 strlist__add(sl, event);
461 strlist__delete(rawlist);
466 static void write_trace_kprobe_event(int fd, const char *buf)
470 pr_debug("Writing event: %s\n", buf);
471 ret = write(fd, buf, strlen(buf));
473 die("Failed to write event: %s", strerror(errno));
476 static void get_new_event_name(char *buf, size_t len, const char *base,
477 struct strlist *namelist)
482 ret = e_snprintf(buf, len, "%s", base);
484 die("snprintf() failed: %s", strerror(-ret));
485 if (!strlist__has_entry(namelist, buf))
488 /* Try to add suffix */
489 for (i = 1; i < MAX_EVENT_INDEX; i++) {
490 ret = e_snprintf(buf, len, "%s_%d", base, i);
492 die("snprintf() failed: %s", strerror(-ret));
493 if (!strlist__has_entry(namelist, buf))
496 if (i == MAX_EVENT_INDEX)
497 die("Too many events are on the same function.");
500 void add_trace_kprobe_events(struct probe_point *probes, int nr_probes)
503 struct probe_point *pp;
504 char buf[MAX_CMDLEN];
506 struct strlist *namelist;
508 fd = open_kprobe_events(O_RDWR, O_APPEND);
509 /* Get current event names */
510 namelist = get_perf_event_names(fd, false);
512 for (j = 0; j < nr_probes; j++) {
514 for (i = 0; i < pp->found; i++) {
515 /* Get an unused new event name */
516 get_new_event_name(event, 64, pp->function, namelist);
517 snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s\n",
518 pp->retprobe ? 'r' : 'p',
519 PERFPROBE_GROUP, event,
521 write_trace_kprobe_event(fd, buf);
522 printf("Added new event:\n");
523 /* Get the first parameter (probe-point) */
524 sscanf(pp->probes[i], "%s", buf);
525 show_perf_probe_event(PERFPROBE_GROUP, event,
527 /* Add added event name to namelist */
528 strlist__add(namelist, event);
531 /* Show how to use the event. */
532 printf("\nYou can now use it on all perf tools, such as:\n\n");
533 printf("\tperf record -e %s:%s -a sleep 1\n\n", PERFPROBE_GROUP, event);
535 strlist__delete(namelist);
539 static void del_trace_kprobe_event(int fd, const char *group,
540 const char *event, struct strlist *namelist)
544 if (e_snprintf(buf, 128, "%s:%s", group, event) < 0)
545 die("Failed to copy event.");
546 if (!strlist__has_entry(namelist, buf)) {
547 pr_warning("Warning: event \"%s\" is not found.\n", buf);
550 /* Convert from perf-probe event to trace-kprobe event */
551 if (e_snprintf(buf, 128, "-:%s/%s", group, event) < 0)
552 die("Failed to copy event.");
554 write_trace_kprobe_event(fd, buf);
555 printf("Remove event: %s:%s\n", group, event);
558 void del_trace_kprobe_events(struct strlist *dellist)
562 const char *group, *event;
564 struct str_node *ent;
565 struct strlist *namelist;
567 fd = open_kprobe_events(O_RDWR, O_APPEND);
568 /* Get current event names */
569 namelist = get_perf_event_names(fd, true);
571 for (i = 0; i < strlist__nr_entries(dellist); i++) {
572 ent = strlist__entry(dellist, i);
573 str = strdup(ent->s);
575 die("Failed to copy event.");
576 p = strchr(str, ':');
582 group = PERFPROBE_GROUP;
585 del_trace_kprobe_event(fd, group, event, namelist);
588 strlist__delete(namelist);