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 map_groups kmap_groups;
76 static struct map *kmaps[MAP__NR_TYPES];
78 /* Initialize symbol maps and path of vmlinux */
79 static int init_vmlinux(void)
84 symbol_conf.sort_by_name = true;
85 if (symbol_conf.vmlinux_name == NULL)
86 symbol_conf.try_vmlinux_path = true;
88 pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
91 pr_debug("Failed to init symbol map.\n");
95 kernel = dso__new_kernel(symbol_conf.vmlinux_name);
97 die("Failed to create kernel dso.");
99 map_groups__init(&kmap_groups);
100 ret = __map_groups__create_kernel_maps(&kmap_groups, kmaps, kernel);
102 pr_debug("Failed to create kernel maps.\n");
106 pr_warning("Failed to init vmlinux path.\n");
111 static int open_vmlinux(void)
113 if (map__load(kmaps[MAP__FUNCTION], NULL) < 0) {
114 pr_debug("Failed to load kernel map.\n");
117 pr_debug("Try to open %s\n", kmaps[MAP__FUNCTION]->dso->long_name);
118 return open(kmaps[MAP__FUNCTION]->dso->long_name, O_RDONLY);
121 /* Convert trace point to probe point with debuginfo */
122 static int convert_to_perf_probe_point(struct kprobe_trace_point *tp,
123 struct perf_probe_point *pp)
126 int fd, ret = -ENOENT;
128 sym = map__find_symbol_by_name(kmaps[MAP__FUNCTION],
133 ret = find_perf_probe_point(fd,
134 sym->start + tp->offset, pp);
139 pr_debug("Failed to find corresponding probes from "
140 "debuginfo. Use kprobe event information.\n");
141 pp->function = strdup(tp->symbol);
142 if (pp->function == NULL)
144 pp->offset = tp->offset;
146 pp->retprobe = tp->retprobe;
151 /* Try to find perf_probe_event with debuginfo */
152 static int try_to_find_kprobe_trace_events(struct perf_probe_event *pev,
153 struct kprobe_trace_event **tevs)
155 bool need_dwarf = perf_probe_event_need_dwarf(pev);
161 pr_warning("Failed to open debuginfo file.\n");
164 pr_debug("Could not open vmlinux. Try to use symbols.\n");
168 /* Searching trace events corresponding to probe event */
169 ntevs = find_kprobe_trace_events(fd, pev, tevs);
172 if (ntevs > 0) { /* Succeeded to find trace events */
173 pr_debug("find %d kprobe_trace_events.\n", ntevs);
177 if (ntevs == 0) { /* No error but failed to find probe point. */
178 pr_warning("Probe point '%s' not found.\n",
179 synthesize_perf_probe_point(&pev->point));
182 /* Error path : ntevs < 0 */
185 pr_warning("No dwarf info found in the vmlinux - "
186 "please rebuild with CONFIG_DEBUG_INFO=y.\n");
189 pr_debug("An error occurred in debuginfo analysis."
190 " Try to use symbols.\n");
194 #define LINEBUF_SIZE 256
195 #define NR_ADDITIONAL_LINES 2
197 static int show_one_line(FILE *fp, int l, bool skip, bool show_num)
199 char buf[LINEBUF_SIZE];
200 const char *color = PERF_COLOR_BLUE;
202 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
206 fprintf(stdout, "%7d %s", l, buf);
208 color_fprintf(stdout, color, " %s", buf);
211 while (strlen(buf) == LINEBUF_SIZE - 1 &&
212 buf[LINEBUF_SIZE - 2] != '\n') {
213 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
217 fprintf(stdout, "%s", buf);
219 color_fprintf(stdout, color, "%s", buf);
226 pr_warning("Source file is shorter than expected.\n");
228 pr_warning("File read error: %s\n", strerror(errno));
234 * Show line-range always requires debuginfo to find source file and
237 int show_line_range(struct line_range *lr)
240 struct line_node *ln;
244 /* Search a line range */
245 ret = init_vmlinux();
251 pr_warning("Failed to open debuginfo file.\n");
255 ret = find_line_range(fd, lr);
258 pr_warning("Specified source line is not found.\n");
260 } else if (ret < 0) {
261 pr_warning("Debuginfo analysis failed. (%d)\n", ret);
268 fprintf(stdout, "<%s:%d>\n", lr->function,
269 lr->start - lr->offset);
271 fprintf(stdout, "<%s:%d>\n", lr->file, lr->start);
273 fp = fopen(lr->path, "r");
275 pr_warning("Failed to open %s: %s\n", lr->path,
279 /* Skip to starting line number */
280 while (l < lr->start && ret >= 0)
281 ret = show_one_line(fp, l++, true, false);
285 list_for_each_entry(ln, &lr->line_list, list) {
286 while (ln->line > l && ret >= 0)
287 ret = show_one_line(fp, (l++) - lr->offset,
290 ret = show_one_line(fp, (l++) - lr->offset,
296 if (lr->end == INT_MAX)
297 lr->end = l + NR_ADDITIONAL_LINES;
298 while (l <= lr->end && !feof(fp) && ret >= 0)
299 ret = show_one_line(fp, (l++) - lr->offset, false, false);
305 #else /* !DWARF_SUPPORT */
307 static int convert_to_perf_probe_point(struct kprobe_trace_point *tp,
308 struct perf_probe_point *pp)
310 pp->function = strdup(tp->symbol);
311 if (pp->function == NULL)
313 pp->offset = tp->offset;
314 pp->retprobe = tp->retprobe;
319 static int try_to_find_kprobe_trace_events(struct perf_probe_event *pev,
320 struct kprobe_trace_event **tevs __unused)
322 if (perf_probe_event_need_dwarf(pev)) {
323 pr_warning("Debuginfo-analysis is not supported.\n");
329 int show_line_range(struct line_range *lr __unused)
331 pr_warning("Debuginfo-analysis is not supported.\n");
337 int parse_line_range_desc(const char *arg, struct line_range *lr)
344 * FUNC[:SLN[+NUM|-ELN]]
346 ptr = strchr(arg, ':');
348 lr->start = (int)strtoul(ptr + 1, &tmp, 0);
350 lr->end = lr->start + (int)strtoul(tmp + 1, &tmp, 0);
352 * Adjust the number of lines here.
353 * If the number of lines == 1, the
354 * the end of line should be equal to
357 } else if (*tmp == '-')
358 lr->end = (int)strtoul(tmp + 1, &tmp, 0);
361 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
362 if (lr->start > lr->end) {
363 semantic_error("Start line must be smaller"
364 " than end line.\n");
368 semantic_error("Tailing with invalid character '%d'.\n",
372 tmp = strndup(arg, (ptr - arg));
381 if (strchr(tmp, '.'))
389 /* Check the name is good for event/group */
390 static bool check_event_name(const char *name)
392 if (!isalpha(*name) && *name != '_')
394 while (*++name != '\0') {
395 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
401 /* Parse probepoint definition. */
402 static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
404 struct perf_probe_point *pp = &pev->point;
409 * perf probe [EVENT=]SRC[:LN|;PTN]
410 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
412 * TODO:Group name support
415 ptr = strpbrk(arg, ";=@+%");
416 if (ptr && *ptr == '=') { /* Event name */
419 if (strchr(arg, ':')) {
420 semantic_error("Group name is not supported yet.\n");
423 if (!check_event_name(arg)) {
424 semantic_error("%s is bad for event name -it must "
425 "follow C symbol-naming rule.\n", arg);
428 pev->event = strdup(arg);
429 if (pev->event == NULL)
435 ptr = strpbrk(arg, ";:+@%");
445 /* Check arg is function or file and copy it */
446 if (strchr(tmp, '.')) /* File */
451 /* Parse other options */
455 if (c == ';') { /* Lazy pattern must be the last part */
456 pp->lazy_line = strdup(arg);
457 if (pp->lazy_line == NULL)
461 ptr = strpbrk(arg, ";:+@%");
467 case ':': /* Line number */
468 pp->line = strtoul(arg, &tmp, 0);
470 semantic_error("There is non-digit char"
471 " in line number.\n");
475 case '+': /* Byte offset from a symbol */
476 pp->offset = strtoul(arg, &tmp, 0);
478 semantic_error("There is non-digit character"
483 case '@': /* File name */
485 semantic_error("SRC@SRC is not allowed.\n");
488 pp->file = strdup(arg);
489 if (pp->file == NULL)
492 case '%': /* Probe places */
493 if (strcmp(arg, "return") == 0) {
495 } else { /* Others not supported yet */
496 semantic_error("%%%s is not supported.\n", arg);
500 default: /* Buggy case */
501 pr_err("This program has a bug at %s:%d.\n",
508 /* Exclusion check */
509 if (pp->lazy_line && pp->line) {
510 semantic_error("Lazy pattern can't be used with line number.");
514 if (pp->lazy_line && pp->offset) {
515 semantic_error("Lazy pattern can't be used with offset.");
519 if (pp->line && pp->offset) {
520 semantic_error("Offset can't be used with line number.");
524 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
525 semantic_error("File always requires line number or "
530 if (pp->offset && !pp->function) {
531 semantic_error("Offset requires an entry function.");
535 if (pp->retprobe && !pp->function) {
536 semantic_error("Return probe requires an entry function.");
540 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
541 semantic_error("Offset/Line/Lazy pattern can't be used with "
546 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
547 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
552 /* Parse perf-probe event argument */
553 static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
556 struct perf_probe_arg_field **fieldp;
558 pr_debug("parsing arg: %s into ", str);
560 tmp = strchr(str, '=');
562 arg->name = strndup(str, tmp - str);
563 if (arg->name == NULL)
565 pr_debug("name:%s ", arg->name);
569 tmp = strchr(str, ':');
570 if (tmp) { /* Type setting */
572 arg->type = strdup(tmp + 1);
573 if (arg->type == NULL)
575 pr_debug("type:%s ", arg->type);
578 tmp = strpbrk(str, "-.");
579 if (!is_c_varname(str) || !tmp) {
580 /* A variable, register, symbol or special value */
581 arg->var = strdup(str);
582 if (arg->var == NULL)
584 pr_debug("%s\n", arg->var);
588 /* Structure fields */
589 arg->var = strndup(str, tmp - str);
590 if (arg->var == NULL)
592 pr_debug("%s, ", arg->var);
593 fieldp = &arg->field;
596 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
601 (*fieldp)->ref = false;
602 } else if (tmp[1] == '>') {
604 (*fieldp)->ref = true;
606 semantic_error("Argument parse error: %s\n", str);
610 tmp = strpbrk(str, "-.");
612 (*fieldp)->name = strndup(str, tmp - str);
613 if ((*fieldp)->name == NULL)
615 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
616 fieldp = &(*fieldp)->next;
619 (*fieldp)->name = strdup(str);
620 if ((*fieldp)->name == NULL)
622 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
624 /* If no name is specified, set the last field name */
626 arg->name = strdup((*fieldp)->name);
627 if (arg->name == NULL)
633 /* Parse perf-probe event command */
634 int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
637 int argc, i, ret = 0;
639 argv = argv_split(cmd, &argc);
641 pr_debug("Failed to split arguments.\n");
644 if (argc - 1 > MAX_PROBE_ARGS) {
645 semantic_error("Too many probe arguments (%d).\n", argc - 1);
649 /* Parse probe point */
650 ret = parse_perf_probe_point(argv[0], pev);
654 /* Copy arguments and ensure return probe has no C argument */
655 pev->nargs = argc - 1;
656 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
657 if (pev->args == NULL) {
661 for (i = 0; i < pev->nargs && ret >= 0; i++) {
662 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
664 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
665 semantic_error("You can't specify local variable for"
676 /* Return true if this perf_probe_event requires debuginfo */
677 bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
681 if (pev->point.file || pev->point.line || pev->point.lazy_line)
684 for (i = 0; i < pev->nargs; i++)
685 if (is_c_varname(pev->args[i].var))
691 /* Parse kprobe_events event into struct probe_point */
692 int parse_kprobe_trace_command(const char *cmd, struct kprobe_trace_event *tev)
694 struct kprobe_trace_point *tp = &tev->point;
700 pr_debug("Parsing kprobe_events: %s\n", cmd);
701 argv = argv_split(cmd, &argc);
703 pr_debug("Failed to split arguments.\n");
707 semantic_error("Too few probe arguments.\n");
712 /* Scan event and group name. */
713 ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]",
714 &pr, (float *)(void *)&tev->group,
715 (float *)(void *)&tev->event);
717 semantic_error("Failed to parse event name: %s\n", argv[0]);
721 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
723 tp->retprobe = (pr == 'r');
725 /* Scan function name and offset */
726 ret = sscanf(argv[1], "%a[^+]+%lu", (float *)(void *)&tp->symbol,
731 tev->nargs = argc - 2;
732 tev->args = zalloc(sizeof(struct kprobe_trace_arg) * tev->nargs);
733 if (tev->args == NULL) {
737 for (i = 0; i < tev->nargs; i++) {
738 p = strchr(argv[i + 2], '=');
739 if (p) /* We don't need which register is assigned. */
743 tev->args[i].name = strdup(argv[i + 2]);
744 /* TODO: parse regs and offset */
745 tev->args[i].value = strdup(p);
746 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
757 /* Compose only probe arg */
758 int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
760 struct perf_probe_arg_field *field = pa->field;
764 if (pa->name && pa->var)
765 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
767 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
774 ret = e_snprintf(tmp, len, "%s%s", field->ref ? "->" : ".",
784 ret = e_snprintf(tmp, len, ":%s", pa->type);
793 pr_debug("Failed to synthesize perf probe argument: %s",
798 /* Compose only probe point (not argument) */
799 static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
802 char offs[32] = "", line[32] = "", file[32] = "";
805 buf = zalloc(MAX_CMDLEN);
811 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
816 ret = e_snprintf(line, 32, ":%d", pp->line);
821 len = strlen(pp->file) - 31;
824 tmp = strchr(pp->file + len, '/');
826 tmp = pp->file + len;
827 ret = e_snprintf(file, 32, "@%s", tmp + 1);
833 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
834 offs, pp->retprobe ? "%return" : "", line,
837 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
843 pr_debug("Failed to synthesize perf probe point: %s",
851 char *synthesize_perf_probe_command(struct perf_probe_event *pev)
856 buf = synthesize_perf_probe_point(&pev->point);
861 for (i = 0; i < pev->nargs; i++) {
862 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
875 static int __synthesize_kprobe_trace_arg_ref(struct kprobe_trace_arg_ref *ref,
876 char **buf, size_t *buflen,
881 depth = __synthesize_kprobe_trace_arg_ref(ref->next, buf,
887 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
899 static int synthesize_kprobe_trace_arg(struct kprobe_trace_arg *arg,
900 char *buf, size_t buflen)
905 /* Argument name or separator */
907 ret = e_snprintf(buf, buflen, " %s=", arg->name);
909 ret = e_snprintf(buf, buflen, " ");
915 /* Dereferencing arguments */
917 depth = __synthesize_kprobe_trace_arg_ref(arg->ref, &buf,
923 /* Print argument value */
924 ret = e_snprintf(buf, buflen, "%s", arg->value);
932 ret = e_snprintf(buf, buflen, ")");
938 /* Print argument type */
940 ret = e_snprintf(buf, buflen, ":%s", arg->type);
949 char *synthesize_kprobe_trace_command(struct kprobe_trace_event *tev)
951 struct kprobe_trace_point *tp = &tev->point;
955 buf = zalloc(MAX_CMDLEN);
959 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s+%lu",
960 tp->retprobe ? 'r' : 'p',
961 tev->group, tev->event,
962 tp->symbol, tp->offset);
966 for (i = 0; i < tev->nargs; i++) {
967 ret = synthesize_kprobe_trace_arg(&tev->args[i], buf + len,
980 int convert_to_perf_probe_event(struct kprobe_trace_event *tev,
981 struct perf_probe_event *pev)
986 /* Convert event/group name */
987 pev->event = strdup(tev->event);
988 pev->group = strdup(tev->group);
989 if (pev->event == NULL || pev->group == NULL)
992 /* Convert trace_point to probe_point */
993 ret = convert_to_perf_probe_point(&tev->point, &pev->point);
997 /* Convert trace_arg to probe_arg */
998 pev->nargs = tev->nargs;
999 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1000 if (pev->args == NULL)
1002 for (i = 0; i < tev->nargs && ret >= 0; i++) {
1003 if (tev->args[i].name)
1004 pev->args[i].name = strdup(tev->args[i].name);
1006 ret = synthesize_kprobe_trace_arg(&tev->args[i],
1008 pev->args[i].name = strdup(buf);
1010 if (pev->args[i].name == NULL && ret >= 0)
1015 clear_perf_probe_event(pev);
1020 void clear_perf_probe_event(struct perf_probe_event *pev)
1022 struct perf_probe_point *pp = &pev->point;
1023 struct perf_probe_arg_field *field, *next;
1035 free(pp->lazy_line);
1036 for (i = 0; i < pev->nargs; i++) {
1037 if (pev->args[i].name)
1038 free(pev->args[i].name);
1039 if (pev->args[i].var)
1040 free(pev->args[i].var);
1041 if (pev->args[i].type)
1042 free(pev->args[i].type);
1043 field = pev->args[i].field;
1054 memset(pev, 0, sizeof(*pev));
1057 void clear_kprobe_trace_event(struct kprobe_trace_event *tev)
1059 struct kprobe_trace_arg_ref *ref, *next;
1066 if (tev->point.symbol)
1067 free(tev->point.symbol);
1068 for (i = 0; i < tev->nargs; i++) {
1069 if (tev->args[i].name)
1070 free(tev->args[i].name);
1071 if (tev->args[i].value)
1072 free(tev->args[i].value);
1073 if (tev->args[i].type)
1074 free(tev->args[i].type);
1075 ref = tev->args[i].ref;
1084 memset(tev, 0, sizeof(*tev));
1087 static int open_kprobe_events(bool readwrite)
1090 const char *__debugfs;
1093 __debugfs = debugfs_find_mountpoint();
1094 if (__debugfs == NULL) {
1095 pr_warning("Debugfs is not mounted.\n");
1099 ret = e_snprintf(buf, PATH_MAX, "%stracing/kprobe_events", __debugfs);
1101 pr_debug("Opening %s write=%d\n", buf, readwrite);
1102 if (readwrite && !probe_event_dry_run)
1103 ret = open(buf, O_RDWR, O_APPEND);
1105 ret = open(buf, O_RDONLY, 0);
1109 if (errno == ENOENT)
1110 pr_warning("kprobe_events file does not exist - please"
1111 " rebuild kernel with CONFIG_KPROBE_EVENT.\n");
1113 pr_warning("Failed to open kprobe_events file: %s\n",
1119 /* Get raw string list of current kprobe_events */
1120 static struct strlist *get_kprobe_trace_command_rawlist(int fd)
1124 char buf[MAX_CMDLEN];
1128 sl = strlist__new(true, NULL);
1130 fp = fdopen(dup(fd), "r");
1132 p = fgets(buf, MAX_CMDLEN, fp);
1136 idx = strlen(p) - 1;
1139 ret = strlist__add(sl, buf);
1141 pr_debug("strlist__add failed: %s\n", strerror(-ret));
1142 strlist__delete(sl);
1152 static int show_perf_probe_event(struct perf_probe_event *pev)
1158 /* Synthesize only event probe point */
1159 place = synthesize_perf_probe_point(&pev->point);
1163 ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
1167 printf(" %-20s (on %s", buf, place);
1169 if (pev->nargs > 0) {
1171 for (i = 0; i < pev->nargs; i++) {
1172 ret = synthesize_perf_probe_arg(&pev->args[i],
1184 /* List up current perf-probe events */
1185 int show_perf_probe_events(void)
1188 struct kprobe_trace_event tev;
1189 struct perf_probe_event pev;
1190 struct strlist *rawlist;
1191 struct str_node *ent;
1194 ret = init_vmlinux();
1198 memset(&tev, 0, sizeof(tev));
1199 memset(&pev, 0, sizeof(pev));
1201 fd = open_kprobe_events(false);
1205 rawlist = get_kprobe_trace_command_rawlist(fd);
1210 strlist__for_each(ent, rawlist) {
1211 ret = parse_kprobe_trace_command(ent->s, &tev);
1213 ret = convert_to_perf_probe_event(&tev, &pev);
1215 ret = show_perf_probe_event(&pev);
1217 clear_perf_probe_event(&pev);
1218 clear_kprobe_trace_event(&tev);
1222 strlist__delete(rawlist);
1227 /* Get current perf-probe event names */
1228 static struct strlist *get_kprobe_trace_event_names(int fd, bool include_group)
1231 struct strlist *sl, *rawlist;
1232 struct str_node *ent;
1233 struct kprobe_trace_event tev;
1236 memset(&tev, 0, sizeof(tev));
1238 rawlist = get_kprobe_trace_command_rawlist(fd);
1239 sl = strlist__new(true, NULL);
1240 strlist__for_each(ent, rawlist) {
1241 ret = parse_kprobe_trace_command(ent->s, &tev);
1244 if (include_group) {
1245 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
1248 ret = strlist__add(sl, buf);
1250 ret = strlist__add(sl, tev.event);
1251 clear_kprobe_trace_event(&tev);
1255 strlist__delete(rawlist);
1258 strlist__delete(sl);
1264 static int write_kprobe_trace_event(int fd, struct kprobe_trace_event *tev)
1267 char *buf = synthesize_kprobe_trace_command(tev);
1270 pr_debug("Failed to synthesize kprobe trace event.\n");
1274 pr_debug("Writing event: %s\n", buf);
1275 if (!probe_event_dry_run) {
1276 ret = write(fd, buf, strlen(buf));
1278 pr_warning("Failed to write event: %s\n",
1285 static int get_new_event_name(char *buf, size_t len, const char *base,
1286 struct strlist *namelist, bool allow_suffix)
1291 ret = e_snprintf(buf, len, "%s", base);
1293 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1296 if (!strlist__has_entry(namelist, buf))
1299 if (!allow_suffix) {
1300 pr_warning("Error: event \"%s\" already exists. "
1301 "(Use -f to force duplicates.)\n", base);
1305 /* Try to add suffix */
1306 for (i = 1; i < MAX_EVENT_INDEX; i++) {
1307 ret = e_snprintf(buf, len, "%s_%d", base, i);
1309 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1312 if (!strlist__has_entry(namelist, buf))
1315 if (i == MAX_EVENT_INDEX) {
1316 pr_warning("Too many events are on the same function.\n");
1323 static int __add_kprobe_trace_events(struct perf_probe_event *pev,
1324 struct kprobe_trace_event *tevs,
1325 int ntevs, bool allow_suffix)
1328 struct kprobe_trace_event *tev = NULL;
1330 const char *event, *group;
1331 struct strlist *namelist;
1333 fd = open_kprobe_events(true);
1336 /* Get current event names */
1337 namelist = get_kprobe_trace_event_names(fd, false);
1339 pr_debug("Failed to get current event list.\n");
1344 printf("Add new event%s\n", (ntevs > 1) ? "s:" : ":");
1345 for (i = 0; i < ntevs; i++) {
1350 if (pev->point.function)
1351 event = pev->point.function;
1353 event = tev->point.symbol;
1357 group = PERFPROBE_GROUP;
1359 /* Get an unused new event name */
1360 ret = get_new_event_name(buf, 64, event,
1361 namelist, allow_suffix);
1366 tev->event = strdup(event);
1367 tev->group = strdup(group);
1368 if (tev->event == NULL || tev->group == NULL) {
1372 ret = write_kprobe_trace_event(fd, tev);
1375 /* Add added event name to namelist */
1376 strlist__add(namelist, event);
1378 /* Trick here - save current event/group */
1381 pev->event = tev->event;
1382 pev->group = tev->group;
1383 show_perf_probe_event(pev);
1384 /* Trick here - restore current event/group */
1385 pev->event = (char *)event;
1386 pev->group = (char *)group;
1389 * Probes after the first probe which comes from same
1390 * user input are always allowed to add suffix, because
1391 * there might be several addresses corresponding to
1394 allow_suffix = true;
1398 /* Show how to use the event. */
1399 printf("\nYou can now use it on all perf tools, such as:\n\n");
1400 printf("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group,
1404 strlist__delete(namelist);
1409 static int convert_to_kprobe_trace_events(struct perf_probe_event *pev,
1410 struct kprobe_trace_event **tevs)
1414 struct kprobe_trace_event *tev;
1416 /* Convert perf_probe_event with debuginfo */
1417 ret = try_to_find_kprobe_trace_events(pev, tevs);
1421 /* Allocate trace event buffer */
1422 tev = *tevs = zalloc(sizeof(struct kprobe_trace_event));
1426 /* Copy parameters */
1427 tev->point.symbol = strdup(pev->point.function);
1428 if (tev->point.symbol == NULL) {
1432 tev->point.offset = pev->point.offset;
1433 tev->nargs = pev->nargs;
1435 tev->args = zalloc(sizeof(struct kprobe_trace_arg)
1437 if (tev->args == NULL) {
1441 for (i = 0; i < tev->nargs; i++) {
1442 if (pev->args[i].name) {
1443 tev->args[i].name = strdup(pev->args[i].name);
1444 if (tev->args[i].name == NULL) {
1449 tev->args[i].value = strdup(pev->args[i].var);
1450 if (tev->args[i].value == NULL) {
1454 if (pev->args[i].type) {
1455 tev->args[i].type = strdup(pev->args[i].type);
1456 if (tev->args[i].type == NULL) {
1464 /* Currently just checking function name from symbol map */
1465 sym = map__find_symbol_by_name(kmaps[MAP__FUNCTION],
1466 tev->point.symbol, NULL);
1468 pr_warning("Kernel symbol \'%s\' not found.\n",
1476 clear_kprobe_trace_event(tev);
1482 struct __event_package {
1483 struct perf_probe_event *pev;
1484 struct kprobe_trace_event *tevs;
1488 int add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
1492 struct __event_package *pkgs;
1494 pkgs = zalloc(sizeof(struct __event_package) * npevs);
1498 /* Init vmlinux path */
1499 ret = init_vmlinux();
1503 /* Loop 1: convert all events */
1504 for (i = 0; i < npevs; i++) {
1505 pkgs[i].pev = &pevs[i];
1506 /* Convert with or without debuginfo */
1507 ret = convert_to_kprobe_trace_events(pkgs[i].pev,
1511 pkgs[i].ntevs = ret;
1514 /* Loop 2: add all events */
1515 for (i = 0; i < npevs && ret >= 0; i++)
1516 ret = __add_kprobe_trace_events(pkgs[i].pev, pkgs[i].tevs,
1517 pkgs[i].ntevs, force_add);
1519 /* Loop 3: cleanup trace events */
1520 for (i = 0; i < npevs; i++)
1521 for (j = 0; j < pkgs[i].ntevs; j++)
1522 clear_kprobe_trace_event(&pkgs[i].tevs[j]);
1527 static int __del_trace_kprobe_event(int fd, struct str_node *ent)
1533 /* Convert from perf-probe event to trace-kprobe event */
1534 ret = e_snprintf(buf, 128, "-:%s", ent->s);
1538 p = strchr(buf + 2, ':');
1540 pr_debug("Internal error: %s should have ':' but not.\n",
1547 pr_debug("Writing event: %s\n", buf);
1548 ret = write(fd, buf, strlen(buf));
1552 printf("Remove event: %s\n", ent->s);
1555 pr_warning("Failed to delete event: %s\n", strerror(-ret));
1559 static int del_trace_kprobe_event(int fd, const char *group,
1560 const char *event, struct strlist *namelist)
1563 struct str_node *ent, *n;
1564 int found = 0, ret = 0;
1566 ret = e_snprintf(buf, 128, "%s:%s", group, event);
1568 pr_err("Failed to copy event.");
1572 if (strpbrk(buf, "*?")) { /* Glob-exp */
1573 strlist__for_each_safe(ent, n, namelist)
1574 if (strglobmatch(ent->s, buf)) {
1576 ret = __del_trace_kprobe_event(fd, ent);
1579 strlist__remove(namelist, ent);
1582 ent = strlist__find(namelist, buf);
1585 ret = __del_trace_kprobe_event(fd, ent);
1587 strlist__remove(namelist, ent);
1590 if (found == 0 && ret >= 0)
1591 pr_info("Info: Event \"%s\" does not exist.\n", buf);
1596 int del_perf_probe_events(struct strlist *dellist)
1599 const char *group, *event;
1601 struct str_node *ent;
1602 struct strlist *namelist;
1604 fd = open_kprobe_events(true);
1608 /* Get current event names */
1609 namelist = get_kprobe_trace_event_names(fd, true);
1610 if (namelist == NULL)
1613 strlist__for_each(ent, dellist) {
1614 str = strdup(ent->s);
1619 pr_debug("Parsing: %s\n", str);
1620 p = strchr(str, ':');
1629 pr_debug("Group: %s, Event: %s\n", group, event);
1630 ret = del_trace_kprobe_event(fd, group, event, namelist);
1635 strlist__delete(namelist);