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>
46 #include "trace-event.h" /* For __unused */
47 #include "probe-event.h"
48 #include "probe-finder.h"
50 #define MAX_CMDLEN 256
51 #define MAX_PROBE_ARGS 128
52 #define PERFPROBE_GROUP "probe"
54 bool probe_event_dry_run; /* Dry run flag */
56 #define semantic_error(msg ...) pr_err("Semantic error :" msg)
58 /* If there is no space to write, returns -E2BIG. */
59 static int e_snprintf(char *str, size_t size, const char *format, ...)
60 __attribute__((format(printf, 3, 4)));
62 static int e_snprintf(char *str, size_t size, const char *format, ...)
67 ret = vsnprintf(str, size, format, ap);
74 static char *synthesize_perf_probe_point(struct perf_probe_point *pp);
75 static struct machine machine;
77 /* Initialize symbol maps and path of vmlinux */
78 static int init_vmlinux(void)
83 symbol_conf.sort_by_name = true;
84 if (symbol_conf.vmlinux_name == NULL)
85 symbol_conf.try_vmlinux_path = true;
87 pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
90 pr_debug("Failed to init symbol map.\n");
94 ret = machine__init(&machine, "/", 0);
98 kernel = dso__new_kernel(symbol_conf.vmlinux_name);
100 die("Failed to create kernel dso.");
102 ret = __machine__create_kernel_maps(&machine, kernel);
104 pr_debug("Failed to create kernel maps.\n");
108 pr_warning("Failed to init vmlinux path.\n");
113 static int open_vmlinux(void)
115 if (map__load(machine.vmlinux_maps[MAP__FUNCTION], NULL) < 0) {
116 pr_debug("Failed to load kernel map.\n");
119 pr_debug("Try to open %s\n", machine.vmlinux_maps[MAP__FUNCTION]->dso->long_name);
120 return open(machine.vmlinux_maps[MAP__FUNCTION]->dso->long_name, O_RDONLY);
123 /* Convert trace point to probe point with debuginfo */
124 static int convert_to_perf_probe_point(struct kprobe_trace_point *tp,
125 struct perf_probe_point *pp)
128 int fd, ret = -ENOENT;
130 sym = map__find_symbol_by_name(machine.vmlinux_maps[MAP__FUNCTION],
135 ret = find_perf_probe_point(fd,
136 sym->start + tp->offset, pp);
141 pr_debug("Failed to find corresponding probes from "
142 "debuginfo. Use kprobe event information.\n");
143 pp->function = strdup(tp->symbol);
144 if (pp->function == NULL)
146 pp->offset = tp->offset;
148 pp->retprobe = tp->retprobe;
153 /* Try to find perf_probe_event with debuginfo */
154 static int try_to_find_kprobe_trace_events(struct perf_probe_event *pev,
155 struct kprobe_trace_event **tevs,
158 bool need_dwarf = perf_probe_event_need_dwarf(pev);
164 pr_warning("Failed to open debuginfo file.\n");
167 pr_debug("Could not open vmlinux. Try to use symbols.\n");
171 /* Searching trace events corresponding to probe event */
172 ntevs = find_kprobe_trace_events(fd, pev, tevs, max_tevs);
175 if (ntevs > 0) { /* Succeeded to find trace events */
176 pr_debug("find %d kprobe_trace_events.\n", ntevs);
180 if (ntevs == 0) { /* No error but failed to find probe point. */
181 pr_warning("Probe point '%s' not found.\n",
182 synthesize_perf_probe_point(&pev->point));
185 /* Error path : ntevs < 0 */
186 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
187 if (ntevs == -EBADF) {
188 pr_warning("Warning: No dwarf info found in the vmlinux - "
189 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
191 pr_debug("Trying to use symbols.\nn");
199 * Find a src file from a DWARF tag path. Prepend optional source path prefix
200 * and chop off leading directories that do not exist. Result is passed back as
201 * a newly allocated path on success.
202 * Return 0 if file was found and readable, -errno otherwise.
204 static int get_real_path(const char *raw_path, char **new_path)
206 if (!symbol_conf.source_prefix) {
207 if (access(raw_path, R_OK) == 0) {
208 *new_path = strdup(raw_path);
214 *new_path = malloc((strlen(symbol_conf.source_prefix) +
215 strlen(raw_path) + 2));
220 sprintf(*new_path, "%s/%s", symbol_conf.source_prefix,
223 if (access(*new_path, R_OK) == 0)
231 raw_path = strchr(++raw_path, '/');
247 #define LINEBUF_SIZE 256
248 #define NR_ADDITIONAL_LINES 2
250 static int show_one_line(FILE *fp, int l, bool skip, bool show_num)
252 char buf[LINEBUF_SIZE];
253 const char *color = PERF_COLOR_BLUE;
255 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
259 fprintf(stdout, "%7d %s", l, buf);
261 color_fprintf(stdout, color, " %s", buf);
264 while (strlen(buf) == LINEBUF_SIZE - 1 &&
265 buf[LINEBUF_SIZE - 2] != '\n') {
266 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
270 fprintf(stdout, "%s", buf);
272 color_fprintf(stdout, color, "%s", buf);
279 pr_warning("Source file is shorter than expected.\n");
281 pr_warning("File read error: %s\n", strerror(errno));
287 * Show line-range always requires debuginfo to find source file and
290 int show_line_range(struct line_range *lr)
293 struct line_node *ln;
298 /* Search a line range */
299 ret = init_vmlinux();
305 pr_warning("Failed to open debuginfo file.\n");
309 ret = find_line_range(fd, lr);
312 pr_warning("Specified source line is not found.\n");
314 } else if (ret < 0) {
315 pr_warning("Debuginfo analysis failed. (%d)\n", ret);
319 /* Convert source file path */
321 ret = get_real_path(tmp, &lr->path);
322 free(tmp); /* Free old path */
324 pr_warning("Failed to find source file. (%d)\n", ret);
331 fprintf(stdout, "<%s:%d>\n", lr->function,
332 lr->start - lr->offset);
334 fprintf(stdout, "<%s:%d>\n", lr->file, lr->start);
336 fp = fopen(lr->path, "r");
338 pr_warning("Failed to open %s: %s\n", lr->path,
342 /* Skip to starting line number */
343 while (l < lr->start && ret >= 0)
344 ret = show_one_line(fp, l++, true, false);
348 list_for_each_entry(ln, &lr->line_list, list) {
349 while (ln->line > l && ret >= 0)
350 ret = show_one_line(fp, (l++) - lr->offset,
353 ret = show_one_line(fp, (l++) - lr->offset,
359 if (lr->end == INT_MAX)
360 lr->end = l + NR_ADDITIONAL_LINES;
361 while (l <= lr->end && !feof(fp) && ret >= 0)
362 ret = show_one_line(fp, (l++) - lr->offset, false, false);
368 #else /* !DWARF_SUPPORT */
370 static int convert_to_perf_probe_point(struct kprobe_trace_point *tp,
371 struct perf_probe_point *pp)
373 pp->function = strdup(tp->symbol);
374 if (pp->function == NULL)
376 pp->offset = tp->offset;
377 pp->retprobe = tp->retprobe;
382 static int try_to_find_kprobe_trace_events(struct perf_probe_event *pev,
383 struct kprobe_trace_event **tevs __unused,
384 int max_tevs __unused)
386 if (perf_probe_event_need_dwarf(pev)) {
387 pr_warning("Debuginfo-analysis is not supported.\n");
393 int show_line_range(struct line_range *lr __unused)
395 pr_warning("Debuginfo-analysis is not supported.\n");
401 int parse_line_range_desc(const char *arg, struct line_range *lr)
408 * FUNC[:SLN[+NUM|-ELN]]
410 ptr = strchr(arg, ':');
412 lr->start = (int)strtoul(ptr + 1, &tmp, 0);
414 lr->end = lr->start + (int)strtoul(tmp + 1, &tmp, 0);
416 * Adjust the number of lines here.
417 * If the number of lines == 1, the
418 * the end of line should be equal to
421 } else if (*tmp == '-')
422 lr->end = (int)strtoul(tmp + 1, &tmp, 0);
425 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
426 if (lr->start > lr->end) {
427 semantic_error("Start line must be smaller"
428 " than end line.\n");
432 semantic_error("Tailing with invalid character '%d'.\n",
436 tmp = strndup(arg, (ptr - arg));
445 if (strchr(tmp, '.'))
453 /* Check the name is good for event/group */
454 static bool check_event_name(const char *name)
456 if (!isalpha(*name) && *name != '_')
458 while (*++name != '\0') {
459 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
465 /* Parse probepoint definition. */
466 static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
468 struct perf_probe_point *pp = &pev->point;
473 * perf probe [EVENT=]SRC[:LN|;PTN]
474 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
476 * TODO:Group name support
479 ptr = strpbrk(arg, ";=@+%");
480 if (ptr && *ptr == '=') { /* Event name */
483 if (strchr(arg, ':')) {
484 semantic_error("Group name is not supported yet.\n");
487 if (!check_event_name(arg)) {
488 semantic_error("%s is bad for event name -it must "
489 "follow C symbol-naming rule.\n", arg);
492 pev->event = strdup(arg);
493 if (pev->event == NULL)
499 ptr = strpbrk(arg, ";:+@%");
509 /* Check arg is function or file and copy it */
510 if (strchr(tmp, '.')) /* File */
515 /* Parse other options */
519 if (c == ';') { /* Lazy pattern must be the last part */
520 pp->lazy_line = strdup(arg);
521 if (pp->lazy_line == NULL)
525 ptr = strpbrk(arg, ";:+@%");
531 case ':': /* Line number */
532 pp->line = strtoul(arg, &tmp, 0);
534 semantic_error("There is non-digit char"
535 " in line number.\n");
539 case '+': /* Byte offset from a symbol */
540 pp->offset = strtoul(arg, &tmp, 0);
542 semantic_error("There is non-digit character"
547 case '@': /* File name */
549 semantic_error("SRC@SRC is not allowed.\n");
552 pp->file = strdup(arg);
553 if (pp->file == NULL)
556 case '%': /* Probe places */
557 if (strcmp(arg, "return") == 0) {
559 } else { /* Others not supported yet */
560 semantic_error("%%%s is not supported.\n", arg);
564 default: /* Buggy case */
565 pr_err("This program has a bug at %s:%d.\n",
572 /* Exclusion check */
573 if (pp->lazy_line && pp->line) {
574 semantic_error("Lazy pattern can't be used with line number.");
578 if (pp->lazy_line && pp->offset) {
579 semantic_error("Lazy pattern can't be used with offset.");
583 if (pp->line && pp->offset) {
584 semantic_error("Offset can't be used with line number.");
588 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
589 semantic_error("File always requires line number or "
594 if (pp->offset && !pp->function) {
595 semantic_error("Offset requires an entry function.");
599 if (pp->retprobe && !pp->function) {
600 semantic_error("Return probe requires an entry function.");
604 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
605 semantic_error("Offset/Line/Lazy pattern can't be used with "
610 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
611 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
616 /* Parse perf-probe event argument */
617 static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
619 char *tmp, *goodname;
620 struct perf_probe_arg_field **fieldp;
622 pr_debug("parsing arg: %s into ", str);
624 tmp = strchr(str, '=');
626 arg->name = strndup(str, tmp - str);
627 if (arg->name == NULL)
629 pr_debug("name:%s ", arg->name);
633 tmp = strchr(str, ':');
634 if (tmp) { /* Type setting */
636 arg->type = strdup(tmp + 1);
637 if (arg->type == NULL)
639 pr_debug("type:%s ", arg->type);
642 tmp = strpbrk(str, "-.[");
643 if (!is_c_varname(str) || !tmp) {
644 /* A variable, register, symbol or special value */
645 arg->var = strdup(str);
646 if (arg->var == NULL)
648 pr_debug("%s\n", arg->var);
652 /* Structure fields or array element */
653 arg->var = strndup(str, tmp - str);
654 if (arg->var == NULL)
657 pr_debug("%s, ", arg->var);
658 fieldp = &arg->field;
661 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
664 if (*tmp == '[') { /* Array */
666 (*fieldp)->index = strtol(str + 1, &tmp, 0);
667 (*fieldp)->ref = true;
668 if (*tmp != ']' || tmp == str + 1) {
669 semantic_error("Array index must be a"
676 } else { /* Structure */
679 (*fieldp)->ref = false;
680 } else if (tmp[1] == '>') {
682 (*fieldp)->ref = true;
684 semantic_error("Argument parse error: %s\n",
688 tmp = strpbrk(str, "-.[");
691 (*fieldp)->name = strndup(str, tmp - str);
692 if ((*fieldp)->name == NULL)
695 goodname = (*fieldp)->name;
696 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
697 fieldp = &(*fieldp)->next;
700 (*fieldp)->name = strdup(str);
701 if ((*fieldp)->name == NULL)
704 goodname = (*fieldp)->name;
705 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
707 /* If no name is specified, set the last field name (not array index)*/
709 arg->name = strdup(goodname);
710 if (arg->name == NULL)
716 /* Parse perf-probe event command */
717 int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
720 int argc, i, ret = 0;
722 argv = argv_split(cmd, &argc);
724 pr_debug("Failed to split arguments.\n");
727 if (argc - 1 > MAX_PROBE_ARGS) {
728 semantic_error("Too many probe arguments (%d).\n", argc - 1);
732 /* Parse probe point */
733 ret = parse_perf_probe_point(argv[0], pev);
737 /* Copy arguments and ensure return probe has no C argument */
738 pev->nargs = argc - 1;
739 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
740 if (pev->args == NULL) {
744 for (i = 0; i < pev->nargs && ret >= 0; i++) {
745 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
747 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
748 semantic_error("You can't specify local variable for"
759 /* Return true if this perf_probe_event requires debuginfo */
760 bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
764 if (pev->point.file || pev->point.line || pev->point.lazy_line)
767 for (i = 0; i < pev->nargs; i++)
768 if (is_c_varname(pev->args[i].var))
774 /* Parse kprobe_events event into struct probe_point */
775 int parse_kprobe_trace_command(const char *cmd, struct kprobe_trace_event *tev)
777 struct kprobe_trace_point *tp = &tev->point;
783 pr_debug("Parsing kprobe_events: %s\n", cmd);
784 argv = argv_split(cmd, &argc);
786 pr_debug("Failed to split arguments.\n");
790 semantic_error("Too few probe arguments.\n");
795 /* Scan event and group name. */
796 ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]",
797 &pr, (float *)(void *)&tev->group,
798 (float *)(void *)&tev->event);
800 semantic_error("Failed to parse event name: %s\n", argv[0]);
804 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
806 tp->retprobe = (pr == 'r');
808 /* Scan function name and offset */
809 ret = sscanf(argv[1], "%a[^+]+%lu", (float *)(void *)&tp->symbol,
814 tev->nargs = argc - 2;
815 tev->args = zalloc(sizeof(struct kprobe_trace_arg) * tev->nargs);
816 if (tev->args == NULL) {
820 for (i = 0; i < tev->nargs; i++) {
821 p = strchr(argv[i + 2], '=');
822 if (p) /* We don't need which register is assigned. */
826 tev->args[i].name = strdup(argv[i + 2]);
827 /* TODO: parse regs and offset */
828 tev->args[i].value = strdup(p);
829 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
840 /* Compose only probe arg */
841 int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
843 struct perf_probe_arg_field *field = pa->field;
847 if (pa->name && pa->var)
848 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
850 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
857 if (field->name[0] == '[')
858 ret = e_snprintf(tmp, len, "%s", field->name);
860 ret = e_snprintf(tmp, len, "%s%s",
861 field->ref ? "->" : ".", field->name);
870 ret = e_snprintf(tmp, len, ":%s", pa->type);
879 pr_debug("Failed to synthesize perf probe argument: %s",
884 /* Compose only probe point (not argument) */
885 static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
888 char offs[32] = "", line[32] = "", file[32] = "";
891 buf = zalloc(MAX_CMDLEN);
897 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
902 ret = e_snprintf(line, 32, ":%d", pp->line);
907 len = strlen(pp->file) - 31;
910 tmp = strchr(pp->file + len, '/');
912 tmp = pp->file + len;
913 ret = e_snprintf(file, 32, "@%s", tmp + 1);
919 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
920 offs, pp->retprobe ? "%return" : "", line,
923 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
929 pr_debug("Failed to synthesize perf probe point: %s",
937 char *synthesize_perf_probe_command(struct perf_probe_event *pev)
942 buf = synthesize_perf_probe_point(&pev->point);
947 for (i = 0; i < pev->nargs; i++) {
948 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
961 static int __synthesize_kprobe_trace_arg_ref(struct kprobe_trace_arg_ref *ref,
962 char **buf, size_t *buflen,
967 depth = __synthesize_kprobe_trace_arg_ref(ref->next, buf,
973 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
985 static int synthesize_kprobe_trace_arg(struct kprobe_trace_arg *arg,
986 char *buf, size_t buflen)
988 struct kprobe_trace_arg_ref *ref = arg->ref;
992 /* Argument name or separator */
994 ret = e_snprintf(buf, buflen, " %s=", arg->name);
996 ret = e_snprintf(buf, buflen, " ");
1002 /* Special case: @XXX */
1003 if (arg->value[0] == '@' && arg->ref)
1006 /* Dereferencing arguments */
1008 depth = __synthesize_kprobe_trace_arg_ref(ref, &buf,
1014 /* Print argument value */
1015 if (arg->value[0] == '@' && arg->ref)
1016 ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
1019 ret = e_snprintf(buf, buflen, "%s", arg->value);
1027 ret = e_snprintf(buf, buflen, ")");
1033 /* Print argument type */
1035 ret = e_snprintf(buf, buflen, ":%s", arg->type);
1044 char *synthesize_kprobe_trace_command(struct kprobe_trace_event *tev)
1046 struct kprobe_trace_point *tp = &tev->point;
1050 buf = zalloc(MAX_CMDLEN);
1054 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s+%lu",
1055 tp->retprobe ? 'r' : 'p',
1056 tev->group, tev->event,
1057 tp->symbol, tp->offset);
1061 for (i = 0; i < tev->nargs; i++) {
1062 ret = synthesize_kprobe_trace_arg(&tev->args[i], buf + len,
1075 int convert_to_perf_probe_event(struct kprobe_trace_event *tev,
1076 struct perf_probe_event *pev)
1081 /* Convert event/group name */
1082 pev->event = strdup(tev->event);
1083 pev->group = strdup(tev->group);
1084 if (pev->event == NULL || pev->group == NULL)
1087 /* Convert trace_point to probe_point */
1088 ret = convert_to_perf_probe_point(&tev->point, &pev->point);
1092 /* Convert trace_arg to probe_arg */
1093 pev->nargs = tev->nargs;
1094 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1095 if (pev->args == NULL)
1097 for (i = 0; i < tev->nargs && ret >= 0; i++) {
1098 if (tev->args[i].name)
1099 pev->args[i].name = strdup(tev->args[i].name);
1101 ret = synthesize_kprobe_trace_arg(&tev->args[i],
1103 pev->args[i].name = strdup(buf);
1105 if (pev->args[i].name == NULL && ret >= 0)
1110 clear_perf_probe_event(pev);
1115 void clear_perf_probe_event(struct perf_probe_event *pev)
1117 struct perf_probe_point *pp = &pev->point;
1118 struct perf_probe_arg_field *field, *next;
1130 free(pp->lazy_line);
1131 for (i = 0; i < pev->nargs; i++) {
1132 if (pev->args[i].name)
1133 free(pev->args[i].name);
1134 if (pev->args[i].var)
1135 free(pev->args[i].var);
1136 if (pev->args[i].type)
1137 free(pev->args[i].type);
1138 field = pev->args[i].field;
1149 memset(pev, 0, sizeof(*pev));
1152 void clear_kprobe_trace_event(struct kprobe_trace_event *tev)
1154 struct kprobe_trace_arg_ref *ref, *next;
1161 if (tev->point.symbol)
1162 free(tev->point.symbol);
1163 for (i = 0; i < tev->nargs; i++) {
1164 if (tev->args[i].name)
1165 free(tev->args[i].name);
1166 if (tev->args[i].value)
1167 free(tev->args[i].value);
1168 if (tev->args[i].type)
1169 free(tev->args[i].type);
1170 ref = tev->args[i].ref;
1179 memset(tev, 0, sizeof(*tev));
1182 static int open_kprobe_events(bool readwrite)
1185 const char *__debugfs;
1188 __debugfs = debugfs_find_mountpoint();
1189 if (__debugfs == NULL) {
1190 pr_warning("Debugfs is not mounted.\n");
1194 ret = e_snprintf(buf, PATH_MAX, "%stracing/kprobe_events", __debugfs);
1196 pr_debug("Opening %s write=%d\n", buf, readwrite);
1197 if (readwrite && !probe_event_dry_run)
1198 ret = open(buf, O_RDWR, O_APPEND);
1200 ret = open(buf, O_RDONLY, 0);
1204 if (errno == ENOENT)
1205 pr_warning("kprobe_events file does not exist - please"
1206 " rebuild kernel with CONFIG_KPROBE_EVENT.\n");
1208 pr_warning("Failed to open kprobe_events file: %s\n",
1214 /* Get raw string list of current kprobe_events */
1215 static struct strlist *get_kprobe_trace_command_rawlist(int fd)
1219 char buf[MAX_CMDLEN];
1223 sl = strlist__new(true, NULL);
1225 fp = fdopen(dup(fd), "r");
1227 p = fgets(buf, MAX_CMDLEN, fp);
1231 idx = strlen(p) - 1;
1234 ret = strlist__add(sl, buf);
1236 pr_debug("strlist__add failed: %s\n", strerror(-ret));
1237 strlist__delete(sl);
1247 static int show_perf_probe_event(struct perf_probe_event *pev)
1253 /* Synthesize only event probe point */
1254 place = synthesize_perf_probe_point(&pev->point);
1258 ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
1262 printf(" %-20s (on %s", buf, place);
1264 if (pev->nargs > 0) {
1266 for (i = 0; i < pev->nargs; i++) {
1267 ret = synthesize_perf_probe_arg(&pev->args[i],
1279 /* List up current perf-probe events */
1280 int show_perf_probe_events(void)
1283 struct kprobe_trace_event tev;
1284 struct perf_probe_event pev;
1285 struct strlist *rawlist;
1286 struct str_node *ent;
1289 ret = init_vmlinux();
1293 memset(&tev, 0, sizeof(tev));
1294 memset(&pev, 0, sizeof(pev));
1296 fd = open_kprobe_events(false);
1300 rawlist = get_kprobe_trace_command_rawlist(fd);
1305 strlist__for_each(ent, rawlist) {
1306 ret = parse_kprobe_trace_command(ent->s, &tev);
1308 ret = convert_to_perf_probe_event(&tev, &pev);
1310 ret = show_perf_probe_event(&pev);
1312 clear_perf_probe_event(&pev);
1313 clear_kprobe_trace_event(&tev);
1317 strlist__delete(rawlist);
1322 /* Get current perf-probe event names */
1323 static struct strlist *get_kprobe_trace_event_names(int fd, bool include_group)
1326 struct strlist *sl, *rawlist;
1327 struct str_node *ent;
1328 struct kprobe_trace_event tev;
1331 memset(&tev, 0, sizeof(tev));
1333 rawlist = get_kprobe_trace_command_rawlist(fd);
1334 sl = strlist__new(true, NULL);
1335 strlist__for_each(ent, rawlist) {
1336 ret = parse_kprobe_trace_command(ent->s, &tev);
1339 if (include_group) {
1340 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
1343 ret = strlist__add(sl, buf);
1345 ret = strlist__add(sl, tev.event);
1346 clear_kprobe_trace_event(&tev);
1350 strlist__delete(rawlist);
1353 strlist__delete(sl);
1359 static int write_kprobe_trace_event(int fd, struct kprobe_trace_event *tev)
1362 char *buf = synthesize_kprobe_trace_command(tev);
1365 pr_debug("Failed to synthesize kprobe trace event.\n");
1369 pr_debug("Writing event: %s\n", buf);
1370 if (!probe_event_dry_run) {
1371 ret = write(fd, buf, strlen(buf));
1373 pr_warning("Failed to write event: %s\n",
1380 static int get_new_event_name(char *buf, size_t len, const char *base,
1381 struct strlist *namelist, bool allow_suffix)
1386 ret = e_snprintf(buf, len, "%s", base);
1388 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1391 if (!strlist__has_entry(namelist, buf))
1394 if (!allow_suffix) {
1395 pr_warning("Error: event \"%s\" already exists. "
1396 "(Use -f to force duplicates.)\n", base);
1400 /* Try to add suffix */
1401 for (i = 1; i < MAX_EVENT_INDEX; i++) {
1402 ret = e_snprintf(buf, len, "%s_%d", base, i);
1404 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1407 if (!strlist__has_entry(namelist, buf))
1410 if (i == MAX_EVENT_INDEX) {
1411 pr_warning("Too many events are on the same function.\n");
1418 static int __add_kprobe_trace_events(struct perf_probe_event *pev,
1419 struct kprobe_trace_event *tevs,
1420 int ntevs, bool allow_suffix)
1423 struct kprobe_trace_event *tev = NULL;
1425 const char *event, *group;
1426 struct strlist *namelist;
1428 fd = open_kprobe_events(true);
1431 /* Get current event names */
1432 namelist = get_kprobe_trace_event_names(fd, false);
1434 pr_debug("Failed to get current event list.\n");
1439 printf("Add new event%s\n", (ntevs > 1) ? "s:" : ":");
1440 for (i = 0; i < ntevs; i++) {
1445 if (pev->point.function)
1446 event = pev->point.function;
1448 event = tev->point.symbol;
1452 group = PERFPROBE_GROUP;
1454 /* Get an unused new event name */
1455 ret = get_new_event_name(buf, 64, event,
1456 namelist, allow_suffix);
1461 tev->event = strdup(event);
1462 tev->group = strdup(group);
1463 if (tev->event == NULL || tev->group == NULL) {
1467 ret = write_kprobe_trace_event(fd, tev);
1470 /* Add added event name to namelist */
1471 strlist__add(namelist, event);
1473 /* Trick here - save current event/group */
1476 pev->event = tev->event;
1477 pev->group = tev->group;
1478 show_perf_probe_event(pev);
1479 /* Trick here - restore current event/group */
1480 pev->event = (char *)event;
1481 pev->group = (char *)group;
1484 * Probes after the first probe which comes from same
1485 * user input are always allowed to add suffix, because
1486 * there might be several addresses corresponding to
1489 allow_suffix = true;
1493 /* Show how to use the event. */
1494 printf("\nYou can now use it on all perf tools, such as:\n\n");
1495 printf("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group,
1499 strlist__delete(namelist);
1504 static int convert_to_kprobe_trace_events(struct perf_probe_event *pev,
1505 struct kprobe_trace_event **tevs,
1510 struct kprobe_trace_event *tev;
1512 /* Convert perf_probe_event with debuginfo */
1513 ret = try_to_find_kprobe_trace_events(pev, tevs, max_tevs);
1517 /* Allocate trace event buffer */
1518 tev = *tevs = zalloc(sizeof(struct kprobe_trace_event));
1522 /* Copy parameters */
1523 tev->point.symbol = strdup(pev->point.function);
1524 if (tev->point.symbol == NULL) {
1528 tev->point.offset = pev->point.offset;
1529 tev->nargs = pev->nargs;
1531 tev->args = zalloc(sizeof(struct kprobe_trace_arg)
1533 if (tev->args == NULL) {
1537 for (i = 0; i < tev->nargs; i++) {
1538 if (pev->args[i].name) {
1539 tev->args[i].name = strdup(pev->args[i].name);
1540 if (tev->args[i].name == NULL) {
1545 tev->args[i].value = strdup(pev->args[i].var);
1546 if (tev->args[i].value == NULL) {
1550 if (pev->args[i].type) {
1551 tev->args[i].type = strdup(pev->args[i].type);
1552 if (tev->args[i].type == NULL) {
1560 /* Currently just checking function name from symbol map */
1561 sym = map__find_symbol_by_name(machine.vmlinux_maps[MAP__FUNCTION],
1562 tev->point.symbol, NULL);
1564 pr_warning("Kernel symbol \'%s\' not found.\n",
1572 clear_kprobe_trace_event(tev);
1578 struct __event_package {
1579 struct perf_probe_event *pev;
1580 struct kprobe_trace_event *tevs;
1584 int add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
1585 bool force_add, int max_tevs)
1588 struct __event_package *pkgs;
1590 pkgs = zalloc(sizeof(struct __event_package) * npevs);
1594 /* Init vmlinux path */
1595 ret = init_vmlinux();
1599 /* Loop 1: convert all events */
1600 for (i = 0; i < npevs; i++) {
1601 pkgs[i].pev = &pevs[i];
1602 /* Convert with or without debuginfo */
1603 ret = convert_to_kprobe_trace_events(pkgs[i].pev,
1604 &pkgs[i].tevs, max_tevs);
1607 pkgs[i].ntevs = ret;
1610 /* Loop 2: add all events */
1611 for (i = 0; i < npevs && ret >= 0; i++)
1612 ret = __add_kprobe_trace_events(pkgs[i].pev, pkgs[i].tevs,
1613 pkgs[i].ntevs, force_add);
1615 /* Loop 3: cleanup trace events */
1616 for (i = 0; i < npevs; i++)
1617 for (j = 0; j < pkgs[i].ntevs; j++)
1618 clear_kprobe_trace_event(&pkgs[i].tevs[j]);
1623 static int __del_trace_kprobe_event(int fd, struct str_node *ent)
1629 /* Convert from perf-probe event to trace-kprobe event */
1630 ret = e_snprintf(buf, 128, "-:%s", ent->s);
1634 p = strchr(buf + 2, ':');
1636 pr_debug("Internal error: %s should have ':' but not.\n",
1643 pr_debug("Writing event: %s\n", buf);
1644 ret = write(fd, buf, strlen(buf));
1648 printf("Remove event: %s\n", ent->s);
1651 pr_warning("Failed to delete event: %s\n", strerror(-ret));
1655 static int del_trace_kprobe_event(int fd, const char *group,
1656 const char *event, struct strlist *namelist)
1659 struct str_node *ent, *n;
1660 int found = 0, ret = 0;
1662 ret = e_snprintf(buf, 128, "%s:%s", group, event);
1664 pr_err("Failed to copy event.");
1668 if (strpbrk(buf, "*?")) { /* Glob-exp */
1669 strlist__for_each_safe(ent, n, namelist)
1670 if (strglobmatch(ent->s, buf)) {
1672 ret = __del_trace_kprobe_event(fd, ent);
1675 strlist__remove(namelist, ent);
1678 ent = strlist__find(namelist, buf);
1681 ret = __del_trace_kprobe_event(fd, ent);
1683 strlist__remove(namelist, ent);
1686 if (found == 0 && ret >= 0)
1687 pr_info("Info: Event \"%s\" does not exist.\n", buf);
1692 int del_perf_probe_events(struct strlist *dellist)
1695 const char *group, *event;
1697 struct str_node *ent;
1698 struct strlist *namelist;
1700 fd = open_kprobe_events(true);
1704 /* Get current event names */
1705 namelist = get_kprobe_trace_event_names(fd, true);
1706 if (namelist == NULL)
1709 strlist__for_each(ent, dellist) {
1710 str = strdup(ent->s);
1715 pr_debug("Parsing: %s\n", str);
1716 p = strchr(str, ':');
1725 pr_debug("Group: %s, Event: %s\n", group, event);
1726 ret = del_trace_kprobe_event(fd, group, event, namelist);
1731 strlist__delete(namelist);