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 int parse_perf_probe_event(const char *str, struct probe_point *pp)
156 int argc, i, need_dwarf = 0;
158 argv = argv_split(str, &argc);
160 die("argv_split failed.");
161 if (argc > MAX_PROBE_ARGS + 1)
162 semantic_error("Too many arguments");
164 /* Parse probe point */
165 parse_perf_probe_probepoint(argv[0], pp);
166 if (pp->file || pp->line)
169 /* Copy arguments and ensure return probe has no C argument */
170 pp->nr_args = argc - 1;
171 pp->args = zalloc(sizeof(char *) * pp->nr_args);
172 for (i = 0; i < pp->nr_args; i++) {
173 pp->args[i] = strdup(argv[i + 1]);
175 die("Failed to copy argument.");
176 if (is_c_varname(pp->args[i])) {
178 semantic_error("You can't specify local"
179 " variable for kretprobe");
188 /* Parse kprobe_events event into struct probe_point */
189 void parse_trace_kprobe_event(const char *str, char **group, char **event,
190 struct probe_point *pp)
197 pr_debug("Parsing kprobe_events: %s\n", str);
198 argv = argv_split(str, &argc);
200 die("argv_split failed.");
202 semantic_error("Too less arguments.");
204 /* Scan event and group name. */
205 ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]",
206 &pr, (float *)(void *)group, (float *)(void *)event);
208 semantic_error("Failed to parse event name: %s", argv[0]);
209 pr_debug("Group:%s Event:%s probe:%c\n", *group, *event, pr);
214 pp->retprobe = (pr == 'r');
216 /* Scan function name and offset */
217 ret = sscanf(argv[1], "%a[^+]+%d", (float *)(void *)&pp->function, &pp->offset);
221 /* kprobe_events doesn't have this information */
225 pp->nr_args = argc - 2;
226 pp->args = zalloc(sizeof(char *) * pp->nr_args);
227 for (i = 0; i < pp->nr_args; i++) {
228 p = strchr(argv[i + 2], '=');
229 if (p) /* We don't need which register is assigned. */
231 pp->args[i] = strdup(argv[i + 2]);
233 die("Failed to copy argument.");
240 int synthesize_perf_probe_event(struct probe_point *pp)
243 char offs[64] = "", line[64] = "";
246 pp->probes[0] = buf = zalloc(MAX_CMDLEN);
248 die("Failed to allocate memory by zalloc.");
250 ret = e_snprintf(offs, 64, "+%d", pp->offset);
255 ret = e_snprintf(line, 64, ":%d", pp->line);
261 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s", pp->function,
262 offs, pp->retprobe ? "%return" : "", line);
264 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", pp->file, line);
269 for (i = 0; i < pp->nr_args; i++) {
270 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
285 int synthesize_trace_kprobe_event(struct probe_point *pp)
290 pp->probes[0] = buf = zalloc(MAX_CMDLEN);
292 die("Failed to allocate memory by zalloc.");
293 ret = e_snprintf(buf, MAX_CMDLEN, "%s+%d", pp->function, pp->offset);
298 for (i = 0; i < pp->nr_args; i++) {
299 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
314 static int open_kprobe_events(int flags, int mode)
319 ret = e_snprintf(buf, PATH_MAX, "%s/../kprobe_events", debugfs_path);
321 die("Failed to make kprobe_events path.");
323 ret = open(buf, flags, mode);
326 die("kprobe_events file does not exist -"
327 " please rebuild with CONFIG_KPROBE_TRACER.");
329 die("Could not open kprobe_events file: %s",
335 /* Get raw string list of current kprobe_events */
336 static struct strlist *get_trace_kprobe_event_rawlist(int fd)
340 char buf[MAX_CMDLEN];
344 sl = strlist__new(true, NULL);
346 fp = fdopen(dup(fd), "r");
348 p = fgets(buf, MAX_CMDLEN, fp);
355 ret = strlist__add(sl, buf);
357 die("strlist__add failed: %s", strerror(-ret));
364 /* Free and zero clear probe_point */
365 static void clear_probe_point(struct probe_point *pp)
373 for (i = 0; i < pp->nr_args; i++)
377 for (i = 0; i < pp->found; i++)
379 memset(pp, 0, sizeof(pp));
383 static void show_perf_probe_event(const char *group, const char *event,
384 const char *place, struct probe_point *pp)
389 e_snprintf(buf, 128, "%s:%s", group, event);
390 printf(" %-40s (on %s", buf, place);
392 if (pp->nr_args > 0) {
394 for (i = 0; i < pp->nr_args; i++)
395 printf(" %s", pp->args[i]);
400 /* List up current perf-probe events */
401 void show_perf_probe_events(void)
406 struct probe_point pp;
407 struct strlist *rawlist;
408 struct str_node *ent;
410 fd = open_kprobe_events(O_RDONLY, 0);
411 rawlist = get_trace_kprobe_event_rawlist(fd);
414 for (i = 0; i < strlist__nr_entries(rawlist); i++) {
415 ent = strlist__entry(rawlist, i);
416 parse_trace_kprobe_event(ent->s, &group, &event, &pp);
417 /* Synthesize only event probe point */
420 synthesize_perf_probe_event(&pp);
423 show_perf_probe_event(group, event, pp.probes[0], &pp);
426 clear_probe_point(&pp);
429 strlist__delete(rawlist);
432 /* Get current perf-probe event names */
433 static struct strlist *get_perf_event_names(int fd, bool include_group)
438 struct strlist *sl, *rawlist;
439 struct str_node *ent;
441 rawlist = get_trace_kprobe_event_rawlist(fd);
443 sl = strlist__new(true, NULL);
444 for (i = 0; i < strlist__nr_entries(rawlist); i++) {
445 ent = strlist__entry(rawlist, i);
446 parse_trace_kprobe_event(ent->s, &group, &event, NULL);
448 if (e_snprintf(buf, 128, "%s:%s", group, event) < 0)
449 die("Failed to copy group:event name.");
450 strlist__add(sl, buf);
452 strlist__add(sl, event);
457 strlist__delete(rawlist);
462 static void write_trace_kprobe_event(int fd, const char *buf)
466 pr_debug("Writing event: %s\n", buf);
467 ret = write(fd, buf, strlen(buf));
469 die("Failed to write event: %s", strerror(errno));
472 static void get_new_event_name(char *buf, size_t len, const char *base,
473 struct strlist *namelist)
478 ret = e_snprintf(buf, len, "%s", base);
480 die("snprintf() failed: %s", strerror(-ret));
481 if (!strlist__has_entry(namelist, buf))
484 /* Try to add suffix */
485 for (i = 1; i < MAX_EVENT_INDEX; i++) {
486 ret = e_snprintf(buf, len, "%s_%d", base, i);
488 die("snprintf() failed: %s", strerror(-ret));
489 if (!strlist__has_entry(namelist, buf))
492 if (i == MAX_EVENT_INDEX)
493 die("Too many events are on the same function.");
496 void add_trace_kprobe_events(struct probe_point *probes, int nr_probes)
499 struct probe_point *pp;
500 char buf[MAX_CMDLEN];
502 struct strlist *namelist;
504 fd = open_kprobe_events(O_RDWR, O_APPEND);
505 /* Get current event names */
506 namelist = get_perf_event_names(fd, false);
508 for (j = 0; j < nr_probes; j++) {
510 for (i = 0; i < pp->found; i++) {
511 /* Get an unused new event name */
512 get_new_event_name(event, 64, pp->function, namelist);
513 snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s\n",
514 pp->retprobe ? 'r' : 'p',
515 PERFPROBE_GROUP, event,
517 write_trace_kprobe_event(fd, buf);
518 printf("Added new event:\n");
519 /* Get the first parameter (probe-point) */
520 sscanf(pp->probes[i], "%s", buf);
521 show_perf_probe_event(PERFPROBE_GROUP, event,
523 /* Add added event name to namelist */
524 strlist__add(namelist, event);
527 /* Show how to use the event. */
528 printf("\nYou can now use it on all perf tools, such as:\n\n");
529 printf("\tperf record -e %s:%s -a sleep 1\n\n", PERFPROBE_GROUP, event);
531 strlist__delete(namelist);
535 static void del_trace_kprobe_event(int fd, const char *group,
536 const char *event, struct strlist *namelist)
540 if (e_snprintf(buf, 128, "%s:%s", group, event) < 0)
541 die("Failed to copy event.");
542 if (!strlist__has_entry(namelist, buf)) {
543 pr_warning("Warning: event \"%s\" is not found.\n", buf);
546 /* Convert from perf-probe event to trace-kprobe event */
547 if (e_snprintf(buf, 128, "-:%s/%s", group, event) < 0)
548 die("Failed to copy event.");
550 write_trace_kprobe_event(fd, buf);
551 printf("Remove event: %s:%s\n", group, event);
554 void del_trace_kprobe_events(struct strlist *dellist)
558 const char *group, *event;
560 struct str_node *ent;
561 struct strlist *namelist;
563 fd = open_kprobe_events(O_RDWR, O_APPEND);
564 /* Get current event names */
565 namelist = get_perf_event_names(fd, true);
567 for (i = 0; i < strlist__nr_entries(dellist); i++) {
568 ent = strlist__entry(dellist, i);
569 str = strdup(ent->s);
571 die("Failed to copy event.");
572 p = strchr(str, ':');
578 group = PERFPROBE_GROUP;
581 del_trace_kprobe_event(fd, group, event, namelist);
584 strlist__delete(namelist);