2 * probe-event.c : perf-probe definition to probe_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);
124 * Convert trace point to probe point with debuginfo
125 * Currently only handles kprobes.
127 static int kprobe_convert_to_perf_probe(struct probe_trace_point *tp,
128 struct perf_probe_point *pp)
131 int fd, ret = -ENOENT;
133 sym = map__find_symbol_by_name(machine.vmlinux_maps[MAP__FUNCTION],
138 ret = find_perf_probe_point(fd,
139 sym->start + tp->offset, pp);
144 pr_debug("Failed to find corresponding probes from "
145 "debuginfo. Use kprobe event information.\n");
146 pp->function = strdup(tp->symbol);
147 if (pp->function == NULL)
149 pp->offset = tp->offset;
151 pp->retprobe = tp->retprobe;
156 /* Try to find perf_probe_event with debuginfo */
157 static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
158 struct probe_trace_event **tevs,
161 bool need_dwarf = perf_probe_event_need_dwarf(pev);
167 pr_warning("Failed to open debuginfo file.\n");
170 pr_debug("Could not open vmlinux. Try to use symbols.\n");
174 /* Searching trace events corresponding to probe event */
175 ntevs = find_probe_trace_events(fd, pev, tevs, max_tevs);
178 if (ntevs > 0) { /* Succeeded to find trace events */
179 pr_debug("find %d probe_trace_events.\n", ntevs);
183 if (ntevs == 0) { /* No error but failed to find probe point. */
184 pr_warning("Probe point '%s' not found.\n",
185 synthesize_perf_probe_point(&pev->point));
188 /* Error path : ntevs < 0 */
189 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
190 if (ntevs == -EBADF) {
191 pr_warning("Warning: No dwarf info found in the vmlinux - "
192 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
194 pr_debug("Trying to use symbols.\nn");
202 * Find a src file from a DWARF tag path. Prepend optional source path prefix
203 * and chop off leading directories that do not exist. Result is passed back as
204 * a newly allocated path on success.
205 * Return 0 if file was found and readable, -errno otherwise.
207 static int get_real_path(const char *raw_path, const char *comp_dir,
210 const char *prefix = symbol_conf.source_prefix;
213 if (raw_path[0] != '/' && comp_dir)
214 /* If not an absolute path, try to use comp_dir */
217 if (access(raw_path, R_OK) == 0) {
218 *new_path = strdup(raw_path);
225 *new_path = malloc((strlen(prefix) + strlen(raw_path) + 2));
230 sprintf(*new_path, "%s/%s", prefix, raw_path);
232 if (access(*new_path, R_OK) == 0)
235 if (!symbol_conf.source_prefix)
236 /* In case of searching comp_dir, don't retry */
244 raw_path = strchr(++raw_path, '/');
260 #define LINEBUF_SIZE 256
261 #define NR_ADDITIONAL_LINES 2
263 static int show_one_line(FILE *fp, int l, bool skip, bool show_num)
265 char buf[LINEBUF_SIZE];
266 const char *color = PERF_COLOR_BLUE;
268 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
272 fprintf(stdout, "%7d %s", l, buf);
274 color_fprintf(stdout, color, " %s", buf);
277 while (strlen(buf) == LINEBUF_SIZE - 1 &&
278 buf[LINEBUF_SIZE - 2] != '\n') {
279 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
283 fprintf(stdout, "%s", buf);
285 color_fprintf(stdout, color, "%s", buf);
292 pr_warning("Source file is shorter than expected.\n");
294 pr_warning("File read error: %s\n", strerror(errno));
300 * Show line-range always requires debuginfo to find source file and
303 int show_line_range(struct line_range *lr)
306 struct line_node *ln;
311 /* Search a line range */
312 ret = init_vmlinux();
318 pr_warning("Failed to open debuginfo file.\n");
322 ret = find_line_range(fd, lr);
325 pr_warning("Specified source line is not found.\n");
327 } else if (ret < 0) {
328 pr_warning("Debuginfo analysis failed. (%d)\n", ret);
332 /* Convert source file path */
334 ret = get_real_path(tmp, lr->comp_dir, &lr->path);
335 free(tmp); /* Free old path */
337 pr_warning("Failed to find source file. (%d)\n", ret);
344 fprintf(stdout, "<%s:%d>\n", lr->function,
345 lr->start - lr->offset);
347 fprintf(stdout, "<%s:%d>\n", lr->file, lr->start);
349 fp = fopen(lr->path, "r");
351 pr_warning("Failed to open %s: %s\n", lr->path,
355 /* Skip to starting line number */
356 while (l < lr->start && ret >= 0)
357 ret = show_one_line(fp, l++, true, false);
361 list_for_each_entry(ln, &lr->line_list, list) {
362 while (ln->line > l && ret >= 0)
363 ret = show_one_line(fp, (l++) - lr->offset,
366 ret = show_one_line(fp, (l++) - lr->offset,
372 if (lr->end == INT_MAX)
373 lr->end = l + NR_ADDITIONAL_LINES;
374 while (l <= lr->end && !feof(fp) && ret >= 0)
375 ret = show_one_line(fp, (l++) - lr->offset, false, false);
381 #else /* !DWARF_SUPPORT */
383 static int kprobe_convert_to_perf_probe(struct probe_trace_point *tp,
384 struct perf_probe_point *pp)
386 pp->function = strdup(tp->symbol);
387 if (pp->function == NULL)
389 pp->offset = tp->offset;
390 pp->retprobe = tp->retprobe;
395 static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
396 struct probe_trace_event **tevs __unused,
397 int max_tevs __unused)
399 if (perf_probe_event_need_dwarf(pev)) {
400 pr_warning("Debuginfo-analysis is not supported.\n");
406 int show_line_range(struct line_range *lr __unused)
408 pr_warning("Debuginfo-analysis is not supported.\n");
414 int parse_line_range_desc(const char *arg, struct line_range *lr)
421 * FUNC[:SLN[+NUM|-ELN]]
423 ptr = strchr(arg, ':');
425 lr->start = (int)strtoul(ptr + 1, &tmp, 0);
427 lr->end = lr->start + (int)strtoul(tmp + 1, &tmp, 0);
429 * Adjust the number of lines here.
430 * If the number of lines == 1, the
431 * the end of line should be equal to
434 } else if (*tmp == '-')
435 lr->end = (int)strtoul(tmp + 1, &tmp, 0);
438 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
439 if (lr->start > lr->end) {
440 semantic_error("Start line must be smaller"
441 " than end line.\n");
445 semantic_error("Tailing with invalid character '%d'.\n",
449 tmp = strndup(arg, (ptr - arg));
458 if (strchr(tmp, '.'))
466 /* Check the name is good for event/group */
467 static bool check_event_name(const char *name)
469 if (!isalpha(*name) && *name != '_')
471 while (*++name != '\0') {
472 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
478 /* Parse probepoint definition. */
479 static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
481 struct perf_probe_point *pp = &pev->point;
486 * perf probe [EVENT=]SRC[:LN|;PTN]
487 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
489 * TODO:Group name support
492 ptr = strpbrk(arg, ";=@+%");
493 if (ptr && *ptr == '=') { /* Event name */
496 if (strchr(arg, ':')) {
497 semantic_error("Group name is not supported yet.\n");
500 if (!check_event_name(arg)) {
501 semantic_error("%s is bad for event name -it must "
502 "follow C symbol-naming rule.\n", arg);
505 pev->event = strdup(arg);
506 if (pev->event == NULL)
512 ptr = strpbrk(arg, ";:+@%");
522 /* Check arg is function or file and copy it */
523 if (strchr(tmp, '.')) /* File */
528 /* Parse other options */
532 if (c == ';') { /* Lazy pattern must be the last part */
533 pp->lazy_line = strdup(arg);
534 if (pp->lazy_line == NULL)
538 ptr = strpbrk(arg, ";:+@%");
544 case ':': /* Line number */
545 pp->line = strtoul(arg, &tmp, 0);
547 semantic_error("There is non-digit char"
548 " in line number.\n");
552 case '+': /* Byte offset from a symbol */
553 pp->offset = strtoul(arg, &tmp, 0);
555 semantic_error("There is non-digit character"
560 case '@': /* File name */
562 semantic_error("SRC@SRC is not allowed.\n");
565 pp->file = strdup(arg);
566 if (pp->file == NULL)
569 case '%': /* Probe places */
570 if (strcmp(arg, "return") == 0) {
572 } else { /* Others not supported yet */
573 semantic_error("%%%s is not supported.\n", arg);
577 default: /* Buggy case */
578 pr_err("This program has a bug at %s:%d.\n",
585 /* Exclusion check */
586 if (pp->lazy_line && pp->line) {
587 semantic_error("Lazy pattern can't be used with line number.");
591 if (pp->lazy_line && pp->offset) {
592 semantic_error("Lazy pattern can't be used with offset.");
596 if (pp->line && pp->offset) {
597 semantic_error("Offset can't be used with line number.");
601 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
602 semantic_error("File always requires line number or "
607 if (pp->offset && !pp->function) {
608 semantic_error("Offset requires an entry function.");
612 if (pp->retprobe && !pp->function) {
613 semantic_error("Return probe requires an entry function.");
617 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
618 semantic_error("Offset/Line/Lazy pattern can't be used with "
623 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
624 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
629 /* Parse perf-probe event argument */
630 static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
632 char *tmp, *goodname;
633 struct perf_probe_arg_field **fieldp;
635 pr_debug("parsing arg: %s into ", str);
637 tmp = strchr(str, '=');
639 arg->name = strndup(str, tmp - str);
640 if (arg->name == NULL)
642 pr_debug("name:%s ", arg->name);
646 tmp = strchr(str, ':');
647 if (tmp) { /* Type setting */
649 arg->type = strdup(tmp + 1);
650 if (arg->type == NULL)
652 pr_debug("type:%s ", arg->type);
655 tmp = strpbrk(str, "-.[");
656 if (!is_c_varname(str) || !tmp) {
657 /* A variable, register, symbol or special value */
658 arg->var = strdup(str);
659 if (arg->var == NULL)
661 pr_debug("%s\n", arg->var);
665 /* Structure fields or array element */
666 arg->var = strndup(str, tmp - str);
667 if (arg->var == NULL)
670 pr_debug("%s, ", arg->var);
671 fieldp = &arg->field;
674 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
677 if (*tmp == '[') { /* Array */
679 (*fieldp)->index = strtol(str + 1, &tmp, 0);
680 (*fieldp)->ref = true;
681 if (*tmp != ']' || tmp == str + 1) {
682 semantic_error("Array index must be a"
689 } else { /* Structure */
692 (*fieldp)->ref = false;
693 } else if (tmp[1] == '>') {
695 (*fieldp)->ref = true;
697 semantic_error("Argument parse error: %s\n",
701 tmp = strpbrk(str, "-.[");
704 (*fieldp)->name = strndup(str, tmp - str);
705 if ((*fieldp)->name == NULL)
708 goodname = (*fieldp)->name;
709 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
710 fieldp = &(*fieldp)->next;
713 (*fieldp)->name = strdup(str);
714 if ((*fieldp)->name == NULL)
717 goodname = (*fieldp)->name;
718 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
720 /* If no name is specified, set the last field name (not array index)*/
722 arg->name = strdup(goodname);
723 if (arg->name == NULL)
729 /* Parse perf-probe event command */
730 int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
733 int argc, i, ret = 0;
735 argv = argv_split(cmd, &argc);
737 pr_debug("Failed to split arguments.\n");
740 if (argc - 1 > MAX_PROBE_ARGS) {
741 semantic_error("Too many probe arguments (%d).\n", argc - 1);
745 /* Parse probe point */
746 ret = parse_perf_probe_point(argv[0], pev);
750 /* Copy arguments and ensure return probe has no C argument */
751 pev->nargs = argc - 1;
752 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
753 if (pev->args == NULL) {
757 for (i = 0; i < pev->nargs && ret >= 0; i++) {
758 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
760 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
761 semantic_error("You can't specify local variable for"
772 /* Return true if this perf_probe_event requires debuginfo */
773 bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
777 if (pev->point.file || pev->point.line || pev->point.lazy_line)
780 for (i = 0; i < pev->nargs; i++)
781 if (is_c_varname(pev->args[i].var))
787 /* Parse probe_events event into struct probe_point */
788 static int parse_probe_trace_command(const char *cmd,
789 struct probe_trace_event *tev)
791 struct probe_trace_point *tp = &tev->point;
797 pr_debug("Parsing probe_events: %s\n", cmd);
798 argv = argv_split(cmd, &argc);
800 pr_debug("Failed to split arguments.\n");
804 semantic_error("Too few probe arguments.\n");
809 /* Scan event and group name. */
810 ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]",
811 &pr, (float *)(void *)&tev->group,
812 (float *)(void *)&tev->event);
814 semantic_error("Failed to parse event name: %s\n", argv[0]);
818 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
820 tp->retprobe = (pr == 'r');
822 /* Scan function name and offset */
823 ret = sscanf(argv[1], "%a[^+]+%lu", (float *)(void *)&tp->symbol,
828 tev->nargs = argc - 2;
829 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
830 if (tev->args == NULL) {
834 for (i = 0; i < tev->nargs; i++) {
835 p = strchr(argv[i + 2], '=');
836 if (p) /* We don't need which register is assigned. */
840 tev->args[i].name = strdup(argv[i + 2]);
841 /* TODO: parse regs and offset */
842 tev->args[i].value = strdup(p);
843 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
854 /* Compose only probe arg */
855 int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
857 struct perf_probe_arg_field *field = pa->field;
861 if (pa->name && pa->var)
862 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
864 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
871 if (field->name[0] == '[')
872 ret = e_snprintf(tmp, len, "%s", field->name);
874 ret = e_snprintf(tmp, len, "%s%s",
875 field->ref ? "->" : ".", field->name);
884 ret = e_snprintf(tmp, len, ":%s", pa->type);
893 pr_debug("Failed to synthesize perf probe argument: %s",
898 /* Compose only probe point (not argument) */
899 static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
902 char offs[32] = "", line[32] = "", file[32] = "";
905 buf = zalloc(MAX_CMDLEN);
911 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
916 ret = e_snprintf(line, 32, ":%d", pp->line);
921 len = strlen(pp->file) - 31;
924 tmp = strchr(pp->file + len, '/');
926 tmp = pp->file + len;
927 ret = e_snprintf(file, 32, "@%s", tmp + 1);
933 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
934 offs, pp->retprobe ? "%return" : "", line,
937 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
943 pr_debug("Failed to synthesize perf probe point: %s",
951 char *synthesize_perf_probe_command(struct perf_probe_event *pev)
956 buf = synthesize_perf_probe_point(&pev->point);
961 for (i = 0; i < pev->nargs; i++) {
962 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
975 static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
976 char **buf, size_t *buflen,
981 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
987 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
999 static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
1000 char *buf, size_t buflen)
1002 struct probe_trace_arg_ref *ref = arg->ref;
1006 /* Argument name or separator */
1008 ret = e_snprintf(buf, buflen, " %s=", arg->name);
1010 ret = e_snprintf(buf, buflen, " ");
1016 /* Special case: @XXX */
1017 if (arg->value[0] == '@' && arg->ref)
1020 /* Dereferencing arguments */
1022 depth = __synthesize_probe_trace_arg_ref(ref, &buf,
1028 /* Print argument value */
1029 if (arg->value[0] == '@' && arg->ref)
1030 ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
1033 ret = e_snprintf(buf, buflen, "%s", arg->value);
1041 ret = e_snprintf(buf, buflen, ")");
1047 /* Print argument type */
1049 ret = e_snprintf(buf, buflen, ":%s", arg->type);
1058 char *synthesize_probe_trace_command(struct probe_trace_event *tev)
1060 struct probe_trace_point *tp = &tev->point;
1064 buf = zalloc(MAX_CMDLEN);
1068 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s+%lu",
1069 tp->retprobe ? 'r' : 'p',
1070 tev->group, tev->event,
1071 tp->symbol, tp->offset);
1075 for (i = 0; i < tev->nargs; i++) {
1076 ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
1089 static int convert_to_perf_probe_event(struct probe_trace_event *tev,
1090 struct perf_probe_event *pev)
1095 /* Convert event/group name */
1096 pev->event = strdup(tev->event);
1097 pev->group = strdup(tev->group);
1098 if (pev->event == NULL || pev->group == NULL)
1101 /* Convert trace_point to probe_point */
1102 ret = kprobe_convert_to_perf_probe(&tev->point, &pev->point);
1106 /* Convert trace_arg to probe_arg */
1107 pev->nargs = tev->nargs;
1108 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1109 if (pev->args == NULL)
1111 for (i = 0; i < tev->nargs && ret >= 0; i++) {
1112 if (tev->args[i].name)
1113 pev->args[i].name = strdup(tev->args[i].name);
1115 ret = synthesize_probe_trace_arg(&tev->args[i],
1117 pev->args[i].name = strdup(buf);
1119 if (pev->args[i].name == NULL && ret >= 0)
1124 clear_perf_probe_event(pev);
1129 void clear_perf_probe_event(struct perf_probe_event *pev)
1131 struct perf_probe_point *pp = &pev->point;
1132 struct perf_probe_arg_field *field, *next;
1144 free(pp->lazy_line);
1145 for (i = 0; i < pev->nargs; i++) {
1146 if (pev->args[i].name)
1147 free(pev->args[i].name);
1148 if (pev->args[i].var)
1149 free(pev->args[i].var);
1150 if (pev->args[i].type)
1151 free(pev->args[i].type);
1152 field = pev->args[i].field;
1163 memset(pev, 0, sizeof(*pev));
1166 static void clear_probe_trace_event(struct probe_trace_event *tev)
1168 struct probe_trace_arg_ref *ref, *next;
1175 if (tev->point.symbol)
1176 free(tev->point.symbol);
1177 for (i = 0; i < tev->nargs; i++) {
1178 if (tev->args[i].name)
1179 free(tev->args[i].name);
1180 if (tev->args[i].value)
1181 free(tev->args[i].value);
1182 if (tev->args[i].type)
1183 free(tev->args[i].type);
1184 ref = tev->args[i].ref;
1193 memset(tev, 0, sizeof(*tev));
1196 static int open_kprobe_events(bool readwrite)
1199 const char *__debugfs;
1202 __debugfs = debugfs_find_mountpoint();
1203 if (__debugfs == NULL) {
1204 pr_warning("Debugfs is not mounted.\n");
1208 ret = e_snprintf(buf, PATH_MAX, "%stracing/kprobe_events", __debugfs);
1210 pr_debug("Opening %s write=%d\n", buf, readwrite);
1211 if (readwrite && !probe_event_dry_run)
1212 ret = open(buf, O_RDWR, O_APPEND);
1214 ret = open(buf, O_RDONLY, 0);
1218 if (errno == ENOENT)
1219 pr_warning("kprobe_events file does not exist - please"
1220 " rebuild kernel with CONFIG_KPROBE_EVENT.\n");
1222 pr_warning("Failed to open kprobe_events file: %s\n",
1228 /* Get raw string list of current kprobe_events */
1229 static struct strlist *get_probe_trace_command_rawlist(int fd)
1233 char buf[MAX_CMDLEN];
1237 sl = strlist__new(true, NULL);
1239 fp = fdopen(dup(fd), "r");
1241 p = fgets(buf, MAX_CMDLEN, fp);
1245 idx = strlen(p) - 1;
1248 ret = strlist__add(sl, buf);
1250 pr_debug("strlist__add failed: %s\n", strerror(-ret));
1251 strlist__delete(sl);
1261 static int show_perf_probe_event(struct perf_probe_event *pev)
1267 /* Synthesize only event probe point */
1268 place = synthesize_perf_probe_point(&pev->point);
1272 ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
1276 printf(" %-20s (on %s", buf, place);
1278 if (pev->nargs > 0) {
1280 for (i = 0; i < pev->nargs; i++) {
1281 ret = synthesize_perf_probe_arg(&pev->args[i],
1293 /* List up current perf-probe events */
1294 int show_perf_probe_events(void)
1297 struct probe_trace_event tev;
1298 struct perf_probe_event pev;
1299 struct strlist *rawlist;
1300 struct str_node *ent;
1303 ret = init_vmlinux();
1307 memset(&tev, 0, sizeof(tev));
1308 memset(&pev, 0, sizeof(pev));
1310 fd = open_kprobe_events(false);
1314 rawlist = get_probe_trace_command_rawlist(fd);
1319 strlist__for_each(ent, rawlist) {
1320 ret = parse_probe_trace_command(ent->s, &tev);
1322 ret = convert_to_perf_probe_event(&tev, &pev);
1324 ret = show_perf_probe_event(&pev);
1326 clear_perf_probe_event(&pev);
1327 clear_probe_trace_event(&tev);
1331 strlist__delete(rawlist);
1336 /* Get current perf-probe event names */
1337 static struct strlist *get_probe_trace_event_names(int fd, bool include_group)
1340 struct strlist *sl, *rawlist;
1341 struct str_node *ent;
1342 struct probe_trace_event tev;
1345 memset(&tev, 0, sizeof(tev));
1346 rawlist = get_probe_trace_command_rawlist(fd);
1347 sl = strlist__new(true, NULL);
1348 strlist__for_each(ent, rawlist) {
1349 ret = parse_probe_trace_command(ent->s, &tev);
1352 if (include_group) {
1353 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
1356 ret = strlist__add(sl, buf);
1358 ret = strlist__add(sl, tev.event);
1359 clear_probe_trace_event(&tev);
1363 strlist__delete(rawlist);
1366 strlist__delete(sl);
1372 static int write_probe_trace_event(int fd, struct probe_trace_event *tev)
1375 char *buf = synthesize_probe_trace_command(tev);
1378 pr_debug("Failed to synthesize probe trace event.\n");
1382 pr_debug("Writing event: %s\n", buf);
1383 if (!probe_event_dry_run) {
1384 ret = write(fd, buf, strlen(buf));
1386 pr_warning("Failed to write event: %s\n",
1393 static int get_new_event_name(char *buf, size_t len, const char *base,
1394 struct strlist *namelist, bool allow_suffix)
1399 ret = e_snprintf(buf, len, "%s", base);
1401 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1404 if (!strlist__has_entry(namelist, buf))
1407 if (!allow_suffix) {
1408 pr_warning("Error: event \"%s\" already exists. "
1409 "(Use -f to force duplicates.)\n", base);
1413 /* Try to add suffix */
1414 for (i = 1; i < MAX_EVENT_INDEX; i++) {
1415 ret = e_snprintf(buf, len, "%s_%d", base, i);
1417 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1420 if (!strlist__has_entry(namelist, buf))
1423 if (i == MAX_EVENT_INDEX) {
1424 pr_warning("Too many events are on the same function.\n");
1431 static int __add_probe_trace_events(struct perf_probe_event *pev,
1432 struct probe_trace_event *tevs,
1433 int ntevs, bool allow_suffix)
1436 struct probe_trace_event *tev = NULL;
1438 const char *event, *group;
1439 struct strlist *namelist;
1441 fd = open_kprobe_events(true);
1444 /* Get current event names */
1445 namelist = get_probe_trace_event_names(fd, false);
1447 pr_debug("Failed to get current event list.\n");
1452 printf("Add new event%s\n", (ntevs > 1) ? "s:" : ":");
1453 for (i = 0; i < ntevs; i++) {
1458 if (pev->point.function)
1459 event = pev->point.function;
1461 event = tev->point.symbol;
1465 group = PERFPROBE_GROUP;
1467 /* Get an unused new event name */
1468 ret = get_new_event_name(buf, 64, event,
1469 namelist, allow_suffix);
1474 tev->event = strdup(event);
1475 tev->group = strdup(group);
1476 if (tev->event == NULL || tev->group == NULL) {
1480 ret = write_probe_trace_event(fd, tev);
1483 /* Add added event name to namelist */
1484 strlist__add(namelist, event);
1486 /* Trick here - save current event/group */
1489 pev->event = tev->event;
1490 pev->group = tev->group;
1491 show_perf_probe_event(pev);
1492 /* Trick here - restore current event/group */
1493 pev->event = (char *)event;
1494 pev->group = (char *)group;
1497 * Probes after the first probe which comes from same
1498 * user input are always allowed to add suffix, because
1499 * there might be several addresses corresponding to
1502 allow_suffix = true;
1506 /* Show how to use the event. */
1507 printf("\nYou can now use it on all perf tools, such as:\n\n");
1508 printf("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group,
1512 strlist__delete(namelist);
1517 static int convert_to_probe_trace_events(struct perf_probe_event *pev,
1518 struct probe_trace_event **tevs,
1523 struct probe_trace_event *tev;
1525 /* Convert perf_probe_event with debuginfo */
1526 ret = try_to_find_probe_trace_events(pev, tevs, max_tevs);
1530 /* Allocate trace event buffer */
1531 tev = *tevs = zalloc(sizeof(struct probe_trace_event));
1535 /* Copy parameters */
1536 tev->point.symbol = strdup(pev->point.function);
1537 if (tev->point.symbol == NULL) {
1541 tev->point.offset = pev->point.offset;
1542 tev->point.retprobe = pev->point.retprobe;
1543 tev->nargs = pev->nargs;
1545 tev->args = zalloc(sizeof(struct probe_trace_arg)
1547 if (tev->args == NULL) {
1551 for (i = 0; i < tev->nargs; i++) {
1552 if (pev->args[i].name) {
1553 tev->args[i].name = strdup(pev->args[i].name);
1554 if (tev->args[i].name == NULL) {
1559 tev->args[i].value = strdup(pev->args[i].var);
1560 if (tev->args[i].value == NULL) {
1564 if (pev->args[i].type) {
1565 tev->args[i].type = strdup(pev->args[i].type);
1566 if (tev->args[i].type == NULL) {
1574 /* Currently just checking function name from symbol map */
1575 sym = map__find_symbol_by_name(machine.vmlinux_maps[MAP__FUNCTION],
1576 tev->point.symbol, NULL);
1578 pr_warning("Kernel symbol \'%s\' not found.\n",
1586 clear_probe_trace_event(tev);
1592 struct __event_package {
1593 struct perf_probe_event *pev;
1594 struct probe_trace_event *tevs;
1598 int add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
1599 bool force_add, int max_tevs)
1602 struct __event_package *pkgs;
1604 pkgs = zalloc(sizeof(struct __event_package) * npevs);
1608 /* Init vmlinux path */
1609 ret = init_vmlinux();
1615 /* Loop 1: convert all events */
1616 for (i = 0; i < npevs; i++) {
1617 pkgs[i].pev = &pevs[i];
1618 /* Convert with or without debuginfo */
1619 ret = convert_to_probe_trace_events(pkgs[i].pev,
1620 &pkgs[i].tevs, max_tevs);
1623 pkgs[i].ntevs = ret;
1626 /* Loop 2: add all events */
1627 for (i = 0; i < npevs && ret >= 0; i++)
1628 ret = __add_probe_trace_events(pkgs[i].pev, pkgs[i].tevs,
1629 pkgs[i].ntevs, force_add);
1631 /* Loop 3: cleanup and free trace events */
1632 for (i = 0; i < npevs; i++) {
1633 for (j = 0; j < pkgs[i].ntevs; j++)
1634 clear_probe_trace_event(&pkgs[i].tevs[j]);
1642 static int __del_trace_probe_event(int fd, struct str_node *ent)
1648 /* Convert from perf-probe event to trace-probe event */
1649 ret = e_snprintf(buf, 128, "-:%s", ent->s);
1653 p = strchr(buf + 2, ':');
1655 pr_debug("Internal error: %s should have ':' but not.\n",
1662 pr_debug("Writing event: %s\n", buf);
1663 ret = write(fd, buf, strlen(buf));
1667 printf("Remove event: %s\n", ent->s);
1670 pr_warning("Failed to delete event: %s\n", strerror(-ret));
1674 static int del_trace_probe_event(int fd, const char *group,
1675 const char *event, struct strlist *namelist)
1678 struct str_node *ent, *n;
1679 int found = 0, ret = 0;
1681 ret = e_snprintf(buf, 128, "%s:%s", group, event);
1683 pr_err("Failed to copy event.");
1687 if (strpbrk(buf, "*?")) { /* Glob-exp */
1688 strlist__for_each_safe(ent, n, namelist)
1689 if (strglobmatch(ent->s, buf)) {
1691 ret = __del_trace_probe_event(fd, ent);
1694 strlist__remove(namelist, ent);
1697 ent = strlist__find(namelist, buf);
1700 ret = __del_trace_probe_event(fd, ent);
1702 strlist__remove(namelist, ent);
1705 if (found == 0 && ret >= 0)
1706 pr_info("Info: Event \"%s\" does not exist.\n", buf);
1711 int del_perf_probe_events(struct strlist *dellist)
1714 const char *group, *event;
1716 struct str_node *ent;
1717 struct strlist *namelist;
1719 fd = open_kprobe_events(true);
1723 /* Get current event names */
1724 namelist = get_probe_trace_event_names(fd, true);
1725 if (namelist == NULL)
1728 strlist__for_each(ent, dellist) {
1729 str = strdup(ent->s);
1734 pr_debug("Parsing: %s\n", str);
1735 p = strchr(str, ':');
1744 pr_debug("Group: %s, Event: %s\n", group, event);
1745 ret = del_trace_probe_event(fd, group, event, namelist);
1750 strlist__delete(namelist);