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,
156 bool need_dwarf = perf_probe_event_need_dwarf(pev);
162 pr_warning("Failed to open debuginfo file.\n");
165 pr_debug("Could not open vmlinux. Try to use symbols.\n");
169 /* Searching trace events corresponding to probe event */
170 ntevs = find_kprobe_trace_events(fd, pev, tevs, max_tevs);
173 if (ntevs > 0) { /* Succeeded to find trace events */
174 pr_debug("find %d kprobe_trace_events.\n", ntevs);
178 if (ntevs == 0) { /* No error but failed to find probe point. */
179 pr_warning("Probe point '%s' not found.\n",
180 synthesize_perf_probe_point(&pev->point));
183 /* Error path : ntevs < 0 */
184 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
185 if (ntevs == -EBADF) {
186 pr_warning("Warning: No dwarf info found in the vmlinux - "
187 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
189 pr_debug("Trying to use symbols.\nn");
196 #define LINEBUF_SIZE 256
197 #define NR_ADDITIONAL_LINES 2
199 static int show_one_line(FILE *fp, int l, bool skip, bool show_num)
201 char buf[LINEBUF_SIZE];
202 const char *color = PERF_COLOR_BLUE;
204 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
208 fprintf(stdout, "%7d %s", l, buf);
210 color_fprintf(stdout, color, " %s", buf);
213 while (strlen(buf) == LINEBUF_SIZE - 1 &&
214 buf[LINEBUF_SIZE - 2] != '\n') {
215 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
219 fprintf(stdout, "%s", buf);
221 color_fprintf(stdout, color, "%s", buf);
228 pr_warning("Source file is shorter than expected.\n");
230 pr_warning("File read error: %s\n", strerror(errno));
236 * Show line-range always requires debuginfo to find source file and
239 int show_line_range(struct line_range *lr)
242 struct line_node *ln;
246 /* Search a line range */
247 ret = init_vmlinux();
253 pr_warning("Failed to open debuginfo file.\n");
257 ret = find_line_range(fd, lr);
260 pr_warning("Specified source line is not found.\n");
262 } else if (ret < 0) {
263 pr_warning("Debuginfo analysis failed. (%d)\n", ret);
270 fprintf(stdout, "<%s:%d>\n", lr->function,
271 lr->start - lr->offset);
273 fprintf(stdout, "<%s:%d>\n", lr->file, lr->start);
275 fp = fopen(lr->path, "r");
277 pr_warning("Failed to open %s: %s\n", lr->path,
281 /* Skip to starting line number */
282 while (l < lr->start && ret >= 0)
283 ret = show_one_line(fp, l++, true, false);
287 list_for_each_entry(ln, &lr->line_list, list) {
288 while (ln->line > l && ret >= 0)
289 ret = show_one_line(fp, (l++) - lr->offset,
292 ret = show_one_line(fp, (l++) - lr->offset,
298 if (lr->end == INT_MAX)
299 lr->end = l + NR_ADDITIONAL_LINES;
300 while (l <= lr->end && !feof(fp) && ret >= 0)
301 ret = show_one_line(fp, (l++) - lr->offset, false, false);
307 #else /* !DWARF_SUPPORT */
309 static int convert_to_perf_probe_point(struct kprobe_trace_point *tp,
310 struct perf_probe_point *pp)
312 pp->function = strdup(tp->symbol);
313 if (pp->function == NULL)
315 pp->offset = tp->offset;
316 pp->retprobe = tp->retprobe;
321 static int try_to_find_kprobe_trace_events(struct perf_probe_event *pev,
322 struct kprobe_trace_event **tevs __unused,
323 int max_tevs __unused)
325 if (perf_probe_event_need_dwarf(pev)) {
326 pr_warning("Debuginfo-analysis is not supported.\n");
332 int show_line_range(struct line_range *lr __unused)
334 pr_warning("Debuginfo-analysis is not supported.\n");
340 int parse_line_range_desc(const char *arg, struct line_range *lr)
347 * FUNC[:SLN[+NUM|-ELN]]
349 ptr = strchr(arg, ':');
351 lr->start = (int)strtoul(ptr + 1, &tmp, 0);
353 lr->end = lr->start + (int)strtoul(tmp + 1, &tmp, 0);
355 * Adjust the number of lines here.
356 * If the number of lines == 1, the
357 * the end of line should be equal to
360 } else if (*tmp == '-')
361 lr->end = (int)strtoul(tmp + 1, &tmp, 0);
364 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
365 if (lr->start > lr->end) {
366 semantic_error("Start line must be smaller"
367 " than end line.\n");
371 semantic_error("Tailing with invalid character '%d'.\n",
375 tmp = strndup(arg, (ptr - arg));
384 if (strchr(tmp, '.'))
392 /* Check the name is good for event/group */
393 static bool check_event_name(const char *name)
395 if (!isalpha(*name) && *name != '_')
397 while (*++name != '\0') {
398 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
404 /* Parse probepoint definition. */
405 static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
407 struct perf_probe_point *pp = &pev->point;
412 * perf probe [EVENT=]SRC[:LN|;PTN]
413 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
415 * TODO:Group name support
418 ptr = strpbrk(arg, ";=@+%");
419 if (ptr && *ptr == '=') { /* Event name */
422 if (strchr(arg, ':')) {
423 semantic_error("Group name is not supported yet.\n");
426 if (!check_event_name(arg)) {
427 semantic_error("%s is bad for event name -it must "
428 "follow C symbol-naming rule.\n", arg);
431 pev->event = strdup(arg);
432 if (pev->event == NULL)
438 ptr = strpbrk(arg, ";:+@%");
448 /* Check arg is function or file and copy it */
449 if (strchr(tmp, '.')) /* File */
454 /* Parse other options */
458 if (c == ';') { /* Lazy pattern must be the last part */
459 pp->lazy_line = strdup(arg);
460 if (pp->lazy_line == NULL)
464 ptr = strpbrk(arg, ";:+@%");
470 case ':': /* Line number */
471 pp->line = strtoul(arg, &tmp, 0);
473 semantic_error("There is non-digit char"
474 " in line number.\n");
478 case '+': /* Byte offset from a symbol */
479 pp->offset = strtoul(arg, &tmp, 0);
481 semantic_error("There is non-digit character"
486 case '@': /* File name */
488 semantic_error("SRC@SRC is not allowed.\n");
491 pp->file = strdup(arg);
492 if (pp->file == NULL)
495 case '%': /* Probe places */
496 if (strcmp(arg, "return") == 0) {
498 } else { /* Others not supported yet */
499 semantic_error("%%%s is not supported.\n", arg);
503 default: /* Buggy case */
504 pr_err("This program has a bug at %s:%d.\n",
511 /* Exclusion check */
512 if (pp->lazy_line && pp->line) {
513 semantic_error("Lazy pattern can't be used with line number.");
517 if (pp->lazy_line && pp->offset) {
518 semantic_error("Lazy pattern can't be used with offset.");
522 if (pp->line && pp->offset) {
523 semantic_error("Offset can't be used with line number.");
527 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
528 semantic_error("File always requires line number or "
533 if (pp->offset && !pp->function) {
534 semantic_error("Offset requires an entry function.");
538 if (pp->retprobe && !pp->function) {
539 semantic_error("Return probe requires an entry function.");
543 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
544 semantic_error("Offset/Line/Lazy pattern can't be used with "
549 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
550 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
555 /* Parse perf-probe event argument */
556 static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
559 struct perf_probe_arg_field **fieldp;
561 pr_debug("parsing arg: %s into ", str);
563 tmp = strchr(str, '=');
565 arg->name = strndup(str, tmp - str);
566 if (arg->name == NULL)
568 pr_debug("name:%s ", arg->name);
572 tmp = strchr(str, ':');
573 if (tmp) { /* Type setting */
575 arg->type = strdup(tmp + 1);
576 if (arg->type == NULL)
578 pr_debug("type:%s ", arg->type);
581 tmp = strpbrk(str, "-.");
582 if (!is_c_varname(str) || !tmp) {
583 /* A variable, register, symbol or special value */
584 arg->var = strdup(str);
585 if (arg->var == NULL)
587 pr_debug("%s\n", arg->var);
591 /* Structure fields */
592 arg->var = strndup(str, tmp - str);
593 if (arg->var == NULL)
595 pr_debug("%s, ", arg->var);
596 fieldp = &arg->field;
599 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
604 (*fieldp)->ref = false;
605 } else if (tmp[1] == '>') {
607 (*fieldp)->ref = true;
609 semantic_error("Argument parse error: %s\n", str);
613 tmp = strpbrk(str, "-.");
615 (*fieldp)->name = strndup(str, tmp - str);
616 if ((*fieldp)->name == NULL)
618 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
619 fieldp = &(*fieldp)->next;
622 (*fieldp)->name = strdup(str);
623 if ((*fieldp)->name == NULL)
625 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
627 /* If no name is specified, set the last field name */
629 arg->name = strdup((*fieldp)->name);
630 if (arg->name == NULL)
636 /* Parse perf-probe event command */
637 int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
640 int argc, i, ret = 0;
642 argv = argv_split(cmd, &argc);
644 pr_debug("Failed to split arguments.\n");
647 if (argc - 1 > MAX_PROBE_ARGS) {
648 semantic_error("Too many probe arguments (%d).\n", argc - 1);
652 /* Parse probe point */
653 ret = parse_perf_probe_point(argv[0], pev);
657 /* Copy arguments and ensure return probe has no C argument */
658 pev->nargs = argc - 1;
659 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
660 if (pev->args == NULL) {
664 for (i = 0; i < pev->nargs && ret >= 0; i++) {
665 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
667 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
668 semantic_error("You can't specify local variable for"
679 /* Return true if this perf_probe_event requires debuginfo */
680 bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
684 if (pev->point.file || pev->point.line || pev->point.lazy_line)
687 for (i = 0; i < pev->nargs; i++)
688 if (is_c_varname(pev->args[i].var))
694 /* Parse kprobe_events event into struct probe_point */
695 int parse_kprobe_trace_command(const char *cmd, struct kprobe_trace_event *tev)
697 struct kprobe_trace_point *tp = &tev->point;
703 pr_debug("Parsing kprobe_events: %s\n", cmd);
704 argv = argv_split(cmd, &argc);
706 pr_debug("Failed to split arguments.\n");
710 semantic_error("Too few probe arguments.\n");
715 /* Scan event and group name. */
716 ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]",
717 &pr, (float *)(void *)&tev->group,
718 (float *)(void *)&tev->event);
720 semantic_error("Failed to parse event name: %s\n", argv[0]);
724 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
726 tp->retprobe = (pr == 'r');
728 /* Scan function name and offset */
729 ret = sscanf(argv[1], "%a[^+]+%lu", (float *)(void *)&tp->symbol,
734 tev->nargs = argc - 2;
735 tev->args = zalloc(sizeof(struct kprobe_trace_arg) * tev->nargs);
736 if (tev->args == NULL) {
740 for (i = 0; i < tev->nargs; i++) {
741 p = strchr(argv[i + 2], '=');
742 if (p) /* We don't need which register is assigned. */
746 tev->args[i].name = strdup(argv[i + 2]);
747 /* TODO: parse regs and offset */
748 tev->args[i].value = strdup(p);
749 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
760 /* Compose only probe arg */
761 int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
763 struct perf_probe_arg_field *field = pa->field;
767 if (pa->name && pa->var)
768 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
770 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
777 ret = e_snprintf(tmp, len, "%s%s", field->ref ? "->" : ".",
787 ret = e_snprintf(tmp, len, ":%s", pa->type);
796 pr_debug("Failed to synthesize perf probe argument: %s",
801 /* Compose only probe point (not argument) */
802 static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
805 char offs[32] = "", line[32] = "", file[32] = "";
808 buf = zalloc(MAX_CMDLEN);
814 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
819 ret = e_snprintf(line, 32, ":%d", pp->line);
824 len = strlen(pp->file) - 31;
827 tmp = strchr(pp->file + len, '/');
829 tmp = pp->file + len;
830 ret = e_snprintf(file, 32, "@%s", tmp + 1);
836 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
837 offs, pp->retprobe ? "%return" : "", line,
840 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
846 pr_debug("Failed to synthesize perf probe point: %s",
854 char *synthesize_perf_probe_command(struct perf_probe_event *pev)
859 buf = synthesize_perf_probe_point(&pev->point);
864 for (i = 0; i < pev->nargs; i++) {
865 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
878 static int __synthesize_kprobe_trace_arg_ref(struct kprobe_trace_arg_ref *ref,
879 char **buf, size_t *buflen,
884 depth = __synthesize_kprobe_trace_arg_ref(ref->next, buf,
890 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
902 static int synthesize_kprobe_trace_arg(struct kprobe_trace_arg *arg,
903 char *buf, size_t buflen)
908 /* Argument name or separator */
910 ret = e_snprintf(buf, buflen, " %s=", arg->name);
912 ret = e_snprintf(buf, buflen, " ");
918 /* Dereferencing arguments */
920 depth = __synthesize_kprobe_trace_arg_ref(arg->ref, &buf,
926 /* Print argument value */
927 ret = e_snprintf(buf, buflen, "%s", arg->value);
935 ret = e_snprintf(buf, buflen, ")");
941 /* Print argument type */
943 ret = e_snprintf(buf, buflen, ":%s", arg->type);
952 char *synthesize_kprobe_trace_command(struct kprobe_trace_event *tev)
954 struct kprobe_trace_point *tp = &tev->point;
958 buf = zalloc(MAX_CMDLEN);
962 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s+%lu",
963 tp->retprobe ? 'r' : 'p',
964 tev->group, tev->event,
965 tp->symbol, tp->offset);
969 for (i = 0; i < tev->nargs; i++) {
970 ret = synthesize_kprobe_trace_arg(&tev->args[i], buf + len,
983 int convert_to_perf_probe_event(struct kprobe_trace_event *tev,
984 struct perf_probe_event *pev)
989 /* Convert event/group name */
990 pev->event = strdup(tev->event);
991 pev->group = strdup(tev->group);
992 if (pev->event == NULL || pev->group == NULL)
995 /* Convert trace_point to probe_point */
996 ret = convert_to_perf_probe_point(&tev->point, &pev->point);
1000 /* Convert trace_arg to probe_arg */
1001 pev->nargs = tev->nargs;
1002 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1003 if (pev->args == NULL)
1005 for (i = 0; i < tev->nargs && ret >= 0; i++) {
1006 if (tev->args[i].name)
1007 pev->args[i].name = strdup(tev->args[i].name);
1009 ret = synthesize_kprobe_trace_arg(&tev->args[i],
1011 pev->args[i].name = strdup(buf);
1013 if (pev->args[i].name == NULL && ret >= 0)
1018 clear_perf_probe_event(pev);
1023 void clear_perf_probe_event(struct perf_probe_event *pev)
1025 struct perf_probe_point *pp = &pev->point;
1026 struct perf_probe_arg_field *field, *next;
1038 free(pp->lazy_line);
1039 for (i = 0; i < pev->nargs; i++) {
1040 if (pev->args[i].name)
1041 free(pev->args[i].name);
1042 if (pev->args[i].var)
1043 free(pev->args[i].var);
1044 if (pev->args[i].type)
1045 free(pev->args[i].type);
1046 field = pev->args[i].field;
1057 memset(pev, 0, sizeof(*pev));
1060 void clear_kprobe_trace_event(struct kprobe_trace_event *tev)
1062 struct kprobe_trace_arg_ref *ref, *next;
1069 if (tev->point.symbol)
1070 free(tev->point.symbol);
1071 for (i = 0; i < tev->nargs; i++) {
1072 if (tev->args[i].name)
1073 free(tev->args[i].name);
1074 if (tev->args[i].value)
1075 free(tev->args[i].value);
1076 if (tev->args[i].type)
1077 free(tev->args[i].type);
1078 ref = tev->args[i].ref;
1087 memset(tev, 0, sizeof(*tev));
1090 static int open_kprobe_events(bool readwrite)
1093 const char *__debugfs;
1096 __debugfs = debugfs_find_mountpoint();
1097 if (__debugfs == NULL) {
1098 pr_warning("Debugfs is not mounted.\n");
1102 ret = e_snprintf(buf, PATH_MAX, "%stracing/kprobe_events", __debugfs);
1104 pr_debug("Opening %s write=%d\n", buf, readwrite);
1105 if (readwrite && !probe_event_dry_run)
1106 ret = open(buf, O_RDWR, O_APPEND);
1108 ret = open(buf, O_RDONLY, 0);
1112 if (errno == ENOENT)
1113 pr_warning("kprobe_events file does not exist - please"
1114 " rebuild kernel with CONFIG_KPROBE_EVENT.\n");
1116 pr_warning("Failed to open kprobe_events file: %s\n",
1122 /* Get raw string list of current kprobe_events */
1123 static struct strlist *get_kprobe_trace_command_rawlist(int fd)
1127 char buf[MAX_CMDLEN];
1131 sl = strlist__new(true, NULL);
1133 fp = fdopen(dup(fd), "r");
1135 p = fgets(buf, MAX_CMDLEN, fp);
1139 idx = strlen(p) - 1;
1142 ret = strlist__add(sl, buf);
1144 pr_debug("strlist__add failed: %s\n", strerror(-ret));
1145 strlist__delete(sl);
1155 static int show_perf_probe_event(struct perf_probe_event *pev)
1161 /* Synthesize only event probe point */
1162 place = synthesize_perf_probe_point(&pev->point);
1166 ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
1170 printf(" %-20s (on %s", buf, place);
1172 if (pev->nargs > 0) {
1174 for (i = 0; i < pev->nargs; i++) {
1175 ret = synthesize_perf_probe_arg(&pev->args[i],
1187 /* List up current perf-probe events */
1188 int show_perf_probe_events(void)
1191 struct kprobe_trace_event tev;
1192 struct perf_probe_event pev;
1193 struct strlist *rawlist;
1194 struct str_node *ent;
1197 ret = init_vmlinux();
1201 memset(&tev, 0, sizeof(tev));
1202 memset(&pev, 0, sizeof(pev));
1204 fd = open_kprobe_events(false);
1208 rawlist = get_kprobe_trace_command_rawlist(fd);
1213 strlist__for_each(ent, rawlist) {
1214 ret = parse_kprobe_trace_command(ent->s, &tev);
1216 ret = convert_to_perf_probe_event(&tev, &pev);
1218 ret = show_perf_probe_event(&pev);
1220 clear_perf_probe_event(&pev);
1221 clear_kprobe_trace_event(&tev);
1225 strlist__delete(rawlist);
1230 /* Get current perf-probe event names */
1231 static struct strlist *get_kprobe_trace_event_names(int fd, bool include_group)
1234 struct strlist *sl, *rawlist;
1235 struct str_node *ent;
1236 struct kprobe_trace_event tev;
1239 memset(&tev, 0, sizeof(tev));
1241 rawlist = get_kprobe_trace_command_rawlist(fd);
1242 sl = strlist__new(true, NULL);
1243 strlist__for_each(ent, rawlist) {
1244 ret = parse_kprobe_trace_command(ent->s, &tev);
1247 if (include_group) {
1248 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
1251 ret = strlist__add(sl, buf);
1253 ret = strlist__add(sl, tev.event);
1254 clear_kprobe_trace_event(&tev);
1258 strlist__delete(rawlist);
1261 strlist__delete(sl);
1267 static int write_kprobe_trace_event(int fd, struct kprobe_trace_event *tev)
1270 char *buf = synthesize_kprobe_trace_command(tev);
1273 pr_debug("Failed to synthesize kprobe trace event.\n");
1277 pr_debug("Writing event: %s\n", buf);
1278 if (!probe_event_dry_run) {
1279 ret = write(fd, buf, strlen(buf));
1281 pr_warning("Failed to write event: %s\n",
1288 static int get_new_event_name(char *buf, size_t len, const char *base,
1289 struct strlist *namelist, bool allow_suffix)
1294 ret = e_snprintf(buf, len, "%s", base);
1296 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1299 if (!strlist__has_entry(namelist, buf))
1302 if (!allow_suffix) {
1303 pr_warning("Error: event \"%s\" already exists. "
1304 "(Use -f to force duplicates.)\n", base);
1308 /* Try to add suffix */
1309 for (i = 1; i < MAX_EVENT_INDEX; i++) {
1310 ret = e_snprintf(buf, len, "%s_%d", base, i);
1312 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1315 if (!strlist__has_entry(namelist, buf))
1318 if (i == MAX_EVENT_INDEX) {
1319 pr_warning("Too many events are on the same function.\n");
1326 static int __add_kprobe_trace_events(struct perf_probe_event *pev,
1327 struct kprobe_trace_event *tevs,
1328 int ntevs, bool allow_suffix)
1331 struct kprobe_trace_event *tev = NULL;
1333 const char *event, *group;
1334 struct strlist *namelist;
1336 fd = open_kprobe_events(true);
1339 /* Get current event names */
1340 namelist = get_kprobe_trace_event_names(fd, false);
1342 pr_debug("Failed to get current event list.\n");
1347 printf("Add new event%s\n", (ntevs > 1) ? "s:" : ":");
1348 for (i = 0; i < ntevs; i++) {
1353 if (pev->point.function)
1354 event = pev->point.function;
1356 event = tev->point.symbol;
1360 group = PERFPROBE_GROUP;
1362 /* Get an unused new event name */
1363 ret = get_new_event_name(buf, 64, event,
1364 namelist, allow_suffix);
1369 tev->event = strdup(event);
1370 tev->group = strdup(group);
1371 if (tev->event == NULL || tev->group == NULL) {
1375 ret = write_kprobe_trace_event(fd, tev);
1378 /* Add added event name to namelist */
1379 strlist__add(namelist, event);
1381 /* Trick here - save current event/group */
1384 pev->event = tev->event;
1385 pev->group = tev->group;
1386 show_perf_probe_event(pev);
1387 /* Trick here - restore current event/group */
1388 pev->event = (char *)event;
1389 pev->group = (char *)group;
1392 * Probes after the first probe which comes from same
1393 * user input are always allowed to add suffix, because
1394 * there might be several addresses corresponding to
1397 allow_suffix = true;
1401 /* Show how to use the event. */
1402 printf("\nYou can now use it on all perf tools, such as:\n\n");
1403 printf("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group,
1407 strlist__delete(namelist);
1412 static int convert_to_kprobe_trace_events(struct perf_probe_event *pev,
1413 struct kprobe_trace_event **tevs,
1418 struct kprobe_trace_event *tev;
1420 /* Convert perf_probe_event with debuginfo */
1421 ret = try_to_find_kprobe_trace_events(pev, tevs, max_tevs);
1425 /* Allocate trace event buffer */
1426 tev = *tevs = zalloc(sizeof(struct kprobe_trace_event));
1430 /* Copy parameters */
1431 tev->point.symbol = strdup(pev->point.function);
1432 if (tev->point.symbol == NULL) {
1436 tev->point.offset = pev->point.offset;
1437 tev->nargs = pev->nargs;
1439 tev->args = zalloc(sizeof(struct kprobe_trace_arg)
1441 if (tev->args == NULL) {
1445 for (i = 0; i < tev->nargs; i++) {
1446 if (pev->args[i].name) {
1447 tev->args[i].name = strdup(pev->args[i].name);
1448 if (tev->args[i].name == NULL) {
1453 tev->args[i].value = strdup(pev->args[i].var);
1454 if (tev->args[i].value == NULL) {
1458 if (pev->args[i].type) {
1459 tev->args[i].type = strdup(pev->args[i].type);
1460 if (tev->args[i].type == NULL) {
1468 /* Currently just checking function name from symbol map */
1469 sym = map__find_symbol_by_name(kmaps[MAP__FUNCTION],
1470 tev->point.symbol, NULL);
1472 pr_warning("Kernel symbol \'%s\' not found.\n",
1480 clear_kprobe_trace_event(tev);
1486 struct __event_package {
1487 struct perf_probe_event *pev;
1488 struct kprobe_trace_event *tevs;
1492 int add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
1493 bool force_add, int max_tevs)
1496 struct __event_package *pkgs;
1498 pkgs = zalloc(sizeof(struct __event_package) * npevs);
1502 /* Init vmlinux path */
1503 ret = init_vmlinux();
1507 /* Loop 1: convert all events */
1508 for (i = 0; i < npevs; i++) {
1509 pkgs[i].pev = &pevs[i];
1510 /* Convert with or without debuginfo */
1511 ret = convert_to_kprobe_trace_events(pkgs[i].pev,
1512 &pkgs[i].tevs, max_tevs);
1515 pkgs[i].ntevs = ret;
1518 /* Loop 2: add all events */
1519 for (i = 0; i < npevs && ret >= 0; i++)
1520 ret = __add_kprobe_trace_events(pkgs[i].pev, pkgs[i].tevs,
1521 pkgs[i].ntevs, force_add);
1523 /* Loop 3: cleanup trace events */
1524 for (i = 0; i < npevs; i++)
1525 for (j = 0; j < pkgs[i].ntevs; j++)
1526 clear_kprobe_trace_event(&pkgs[i].tevs[j]);
1531 static int __del_trace_kprobe_event(int fd, struct str_node *ent)
1537 /* Convert from perf-probe event to trace-kprobe event */
1538 ret = e_snprintf(buf, 128, "-:%s", ent->s);
1542 p = strchr(buf + 2, ':');
1544 pr_debug("Internal error: %s should have ':' but not.\n",
1551 pr_debug("Writing event: %s\n", buf);
1552 ret = write(fd, buf, strlen(buf));
1556 printf("Remove event: %s\n", ent->s);
1559 pr_warning("Failed to delete event: %s\n", strerror(-ret));
1563 static int del_trace_kprobe_event(int fd, const char *group,
1564 const char *event, struct strlist *namelist)
1567 struct str_node *ent, *n;
1568 int found = 0, ret = 0;
1570 ret = e_snprintf(buf, 128, "%s:%s", group, event);
1572 pr_err("Failed to copy event.");
1576 if (strpbrk(buf, "*?")) { /* Glob-exp */
1577 strlist__for_each_safe(ent, n, namelist)
1578 if (strglobmatch(ent->s, buf)) {
1580 ret = __del_trace_kprobe_event(fd, ent);
1583 strlist__remove(namelist, ent);
1586 ent = strlist__find(namelist, buf);
1589 ret = __del_trace_kprobe_event(fd, ent);
1591 strlist__remove(namelist, ent);
1594 if (found == 0 && ret >= 0)
1595 pr_info("Info: Event \"%s\" does not exist.\n", buf);
1600 int del_perf_probe_events(struct strlist *dellist)
1603 const char *group, *event;
1605 struct str_node *ent;
1606 struct strlist *namelist;
1608 fd = open_kprobe_events(true);
1612 /* Get current event names */
1613 namelist = get_kprobe_trace_event_names(fd, true);
1614 if (namelist == NULL)
1617 strlist__for_each(ent, dellist) {
1618 str = strdup(ent->s);
1623 pr_debug("Parsing: %s\n", str);
1624 p = strchr(str, ':');
1633 pr_debug("Group: %s, Event: %s\n", group, event);
1634 ret = del_trace_kprobe_event(fd, group, event, namelist);
1639 strlist__delete(namelist);