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>
47 #include "trace-event.h" /* For __unused */
48 #include "probe-event.h"
49 #include "probe-finder.h"
51 #define MAX_CMDLEN 256
52 #define MAX_PROBE_ARGS 128
53 #define PERFPROBE_GROUP "probe"
55 bool probe_event_dry_run; /* Dry run flag */
57 #define semantic_error(msg ...) pr_err("Semantic error :" msg)
59 /* If there is no space to write, returns -E2BIG. */
60 static int e_snprintf(char *str, size_t size, const char *format, ...)
61 __attribute__((format(printf, 3, 4)));
63 static int e_snprintf(char *str, size_t size, const char *format, ...)
68 ret = vsnprintf(str, size, format, ap);
75 static char *synthesize_perf_probe_point(struct perf_probe_point *pp);
76 static struct machine machine;
78 /* Initialize symbol maps and path of vmlinux/modules */
79 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, "", HOST_KERNEL_ID);
98 if (machine__create_kernel_maps(&machine) < 0) {
99 pr_debug("machine__create_kernel_maps() failed.\n");
104 pr_warning("Failed to init vmlinux path.\n");
108 static struct symbol *__find_kernel_function_by_name(const char *name,
111 return machine__find_kernel_function_by_name(&machine, name, mapp,
115 static struct map *kernel_get_module_map(const char *module)
118 struct map_groups *grp = &machine.kmaps;
123 for (nd = rb_first(&grp->maps[MAP__FUNCTION]); nd; nd = rb_next(nd)) {
124 struct map *pos = rb_entry(nd, struct map, rb_node);
125 if (strncmp(pos->dso->short_name + 1, module,
126 pos->dso->short_name_len - 2) == 0) {
133 static struct dso *kernel_get_module_dso(const char *module)
137 const char *vmlinux_name;
140 list_for_each_entry(dso, &machine.kernel_dsos, node) {
141 if (strncmp(dso->short_name + 1, module,
142 dso->short_name_len - 2) == 0)
145 pr_debug("Failed to find module %s.\n", module);
149 map = machine.vmlinux_maps[MAP__FUNCTION];
152 vmlinux_name = symbol_conf.vmlinux_name;
154 if (dso__load_vmlinux(dso, map, vmlinux_name, NULL) <= 0)
157 if (dso__load_vmlinux_path(dso, map, NULL) <= 0) {
158 pr_debug("Failed to load kernel map.\n");
166 const char *kernel_get_module_path(const char *module)
168 struct dso *dso = kernel_get_module_dso(module);
169 return (dso) ? dso->long_name : NULL;
173 static int open_vmlinux(const char *module)
175 const char *path = kernel_get_module_path(module);
177 pr_err("Failed to find path of %s module.\n",
181 pr_debug("Try to open %s\n", path);
182 return open(path, O_RDONLY);
186 * Convert trace point to probe point with debuginfo
187 * Currently only handles kprobes.
189 static int kprobe_convert_to_perf_probe(struct probe_trace_point *tp,
190 struct perf_probe_point *pp)
197 sym = __find_kernel_function_by_name(tp->symbol, &map);
199 addr = map->unmap_ip(map, sym->start + tp->offset);
200 pr_debug("try to find %s+%ld@%" PRIx64 "\n", tp->symbol,
202 ret = find_perf_probe_point((unsigned long)addr, pp);
205 pr_debug("Failed to find corresponding probes from "
206 "debuginfo. Use kprobe event information.\n");
207 pp->function = strdup(tp->symbol);
208 if (pp->function == NULL)
210 pp->offset = tp->offset;
212 pp->retprobe = tp->retprobe;
217 /* Try to find perf_probe_event with debuginfo */
218 static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
219 struct probe_trace_event **tevs,
220 int max_tevs, const char *module)
222 bool need_dwarf = perf_probe_event_need_dwarf(pev);
225 fd = open_vmlinux(module);
228 pr_warning("Failed to open debuginfo file.\n");
231 pr_debug("Could not open vmlinux. Try to use symbols.\n");
235 /* Searching trace events corresponding to probe event */
236 ntevs = find_probe_trace_events(fd, pev, tevs, max_tevs);
239 if (ntevs > 0) { /* Succeeded to find trace events */
240 pr_debug("find %d probe_trace_events.\n", ntevs);
244 if (ntevs == 0) { /* No error but failed to find probe point. */
245 pr_warning("Probe point '%s' not found.\n",
246 synthesize_perf_probe_point(&pev->point));
249 /* Error path : ntevs < 0 */
250 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
251 if (ntevs == -EBADF) {
252 pr_warning("Warning: No dwarf info found in the vmlinux - "
253 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
255 pr_debug("Trying to use symbols.\n");
263 * Find a src file from a DWARF tag path. Prepend optional source path prefix
264 * and chop off leading directories that do not exist. Result is passed back as
265 * a newly allocated path on success.
266 * Return 0 if file was found and readable, -errno otherwise.
268 static int get_real_path(const char *raw_path, const char *comp_dir,
271 const char *prefix = symbol_conf.source_prefix;
274 if (raw_path[0] != '/' && comp_dir)
275 /* If not an absolute path, try to use comp_dir */
278 if (access(raw_path, R_OK) == 0) {
279 *new_path = strdup(raw_path);
286 *new_path = malloc((strlen(prefix) + strlen(raw_path) + 2));
291 sprintf(*new_path, "%s/%s", prefix, raw_path);
293 if (access(*new_path, R_OK) == 0)
296 if (!symbol_conf.source_prefix)
297 /* In case of searching comp_dir, don't retry */
305 raw_path = strchr(++raw_path, '/');
321 #define LINEBUF_SIZE 256
322 #define NR_ADDITIONAL_LINES 2
324 static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
326 char buf[LINEBUF_SIZE];
327 const char *color = show_num ? "" : PERF_COLOR_BLUE;
328 const char *prefix = NULL;
331 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
336 prefix = show_num ? "%7d " : " ";
337 color_fprintf(stdout, color, prefix, l);
339 color_fprintf(stdout, color, "%s", buf);
341 } while (strchr(buf, '\n') == NULL);
346 pr_warning("File read error: %s\n", strerror(errno));
352 static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
354 int rv = __show_one_line(fp, l, skip, show_num);
356 pr_warning("Source file is shorter than expected.\n");
362 #define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
363 #define show_one_line(f,l) _show_one_line(f,l,false,false)
364 #define skip_one_line(f,l) _show_one_line(f,l,true,false)
365 #define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
368 * Show line-range always requires debuginfo to find source file and
371 int show_line_range(struct line_range *lr, const char *module)
374 struct line_node *ln;
379 /* Search a line range */
380 ret = init_vmlinux();
384 fd = open_vmlinux(module);
386 pr_warning("Failed to open debuginfo file.\n");
390 ret = find_line_range(fd, lr);
393 pr_warning("Specified source line is not found.\n");
395 } else if (ret < 0) {
396 pr_warning("Debuginfo analysis failed. (%d)\n", ret);
400 /* Convert source file path */
402 ret = get_real_path(tmp, lr->comp_dir, &lr->path);
403 free(tmp); /* Free old path */
405 pr_warning("Failed to find source file. (%d)\n", ret);
412 fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
413 lr->start - lr->offset);
415 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
417 fp = fopen(lr->path, "r");
419 pr_warning("Failed to open %s: %s\n", lr->path,
423 /* Skip to starting line number */
424 while (l < lr->start) {
425 ret = skip_one_line(fp, l++);
430 list_for_each_entry(ln, &lr->line_list, list) {
431 for (; ln->line > l; l++) {
432 ret = show_one_line(fp, l - lr->offset);
436 ret = show_one_line_with_num(fp, l++ - lr->offset);
441 if (lr->end == INT_MAX)
442 lr->end = l + NR_ADDITIONAL_LINES;
443 while (l <= lr->end) {
444 ret = show_one_line_or_eof(fp, l++ - lr->offset);
453 static int show_available_vars_at(int fd, struct perf_probe_event *pev,
454 int max_vls, struct strfilter *_filter,
459 struct str_node *node;
460 struct variable_list *vls = NULL, *vl;
463 buf = synthesize_perf_probe_point(&pev->point);
466 pr_debug("Searching variables at %s\n", buf);
468 ret = find_available_vars_at(fd, pev, &vls, max_vls, externs);
470 pr_err("Failed to find variables at %s (%d)\n", buf, ret);
473 /* Some variables are found */
474 fprintf(stdout, "Available variables at %s\n", buf);
475 for (i = 0; i < ret; i++) {
478 * A probe point might be converted to
479 * several trace points.
481 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
483 free(vl->point.symbol);
486 strlist__for_each(node, vl->vars) {
487 var = strchr(node->s, '\t') + 1;
488 if (strfilter__compare(_filter, var)) {
489 fprintf(stdout, "\t\t%s\n", node->s);
493 strlist__delete(vl->vars);
496 fprintf(stdout, "\t\t(No matched variables)\n");
504 /* Show available variables on given probe point */
505 int show_available_vars(struct perf_probe_event *pevs, int npevs,
506 int max_vls, const char *module,
507 struct strfilter *_filter, bool externs)
511 ret = init_vmlinux();
515 fd = open_vmlinux(module);
517 pr_warning("Failed to open debug information file.\n");
523 for (i = 0; i < npevs && ret >= 0; i++)
524 ret = show_available_vars_at(fd, &pevs[i], max_vls, _filter,
531 #else /* !DWARF_SUPPORT */
533 static int kprobe_convert_to_perf_probe(struct probe_trace_point *tp,
534 struct perf_probe_point *pp)
538 sym = __find_kernel_function_by_name(tp->symbol, NULL);
540 pr_err("Failed to find symbol %s in kernel.\n", tp->symbol);
543 pp->function = strdup(tp->symbol);
544 if (pp->function == NULL)
546 pp->offset = tp->offset;
547 pp->retprobe = tp->retprobe;
552 static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
553 struct probe_trace_event **tevs __unused,
554 int max_tevs __unused, const char *mod __unused)
556 if (perf_probe_event_need_dwarf(pev)) {
557 pr_warning("Debuginfo-analysis is not supported.\n");
563 int show_line_range(struct line_range *lr __unused, const char *module __unused)
565 pr_warning("Debuginfo-analysis is not supported.\n");
569 int show_available_vars(struct perf_probe_event *pevs __unused,
570 int npevs __unused, int max_vls __unused,
571 const char *module __unused,
572 struct strfilter *filter __unused,
573 bool externs __unused)
575 pr_warning("Debuginfo-analysis is not supported.\n");
580 static int parse_line_num(char **ptr, int *val, const char *what)
582 const char *start = *ptr;
585 *val = strtol(*ptr, ptr, 0);
586 if (errno || *ptr == start) {
587 semantic_error("'%s' is not a valid number.\n", what);
594 * Stuff 'lr' according to the line range described by 'arg'.
595 * The line range syntax is described by:
597 * SRC[:SLN[+NUM|-ELN]]
598 * FNC[@SRC][:SLN[+NUM|-ELN]]
600 int parse_line_range_desc(const char *arg, struct line_range *lr)
602 char *range, *file, *name = strdup(arg);
611 range = strchr(name, ':');
615 err = parse_line_num(&range, &lr->start, "start line");
619 if (*range == '+' || *range == '-') {
620 const char c = *range++;
622 err = parse_line_num(&range, &lr->end, "end line");
627 lr->end += lr->start;
629 * Adjust the number of lines here.
630 * If the number of lines == 1, the
631 * the end of line should be equal to
638 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
641 if (lr->start > lr->end) {
642 semantic_error("Start line must be smaller"
643 " than end line.\n");
646 if (*range != '\0') {
647 semantic_error("Tailing with invalid str '%s'.\n", range);
652 file = strchr(name, '@');
655 lr->file = strdup(++file);
656 if (lr->file == NULL) {
661 } else if (strchr(name, '.'))
672 /* Check the name is good for event/group */
673 static bool check_event_name(const char *name)
675 if (!isalpha(*name) && *name != '_')
677 while (*++name != '\0') {
678 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
684 /* Parse probepoint definition. */
685 static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
687 struct perf_probe_point *pp = &pev->point;
692 * perf probe [EVENT=]SRC[:LN|;PTN]
693 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
695 * TODO:Group name support
698 ptr = strpbrk(arg, ";=@+%");
699 if (ptr && *ptr == '=') { /* Event name */
702 if (strchr(arg, ':')) {
703 semantic_error("Group name is not supported yet.\n");
706 if (!check_event_name(arg)) {
707 semantic_error("%s is bad for event name -it must "
708 "follow C symbol-naming rule.\n", arg);
711 pev->event = strdup(arg);
712 if (pev->event == NULL)
718 ptr = strpbrk(arg, ";:+@%");
728 /* Check arg is function or file and copy it */
729 if (strchr(tmp, '.')) /* File */
734 /* Parse other options */
738 if (c == ';') { /* Lazy pattern must be the last part */
739 pp->lazy_line = strdup(arg);
740 if (pp->lazy_line == NULL)
744 ptr = strpbrk(arg, ";:+@%");
750 case ':': /* Line number */
751 pp->line = strtoul(arg, &tmp, 0);
753 semantic_error("There is non-digit char"
754 " in line number.\n");
758 case '+': /* Byte offset from a symbol */
759 pp->offset = strtoul(arg, &tmp, 0);
761 semantic_error("There is non-digit character"
766 case '@': /* File name */
768 semantic_error("SRC@SRC is not allowed.\n");
771 pp->file = strdup(arg);
772 if (pp->file == NULL)
775 case '%': /* Probe places */
776 if (strcmp(arg, "return") == 0) {
778 } else { /* Others not supported yet */
779 semantic_error("%%%s is not supported.\n", arg);
783 default: /* Buggy case */
784 pr_err("This program has a bug at %s:%d.\n",
791 /* Exclusion check */
792 if (pp->lazy_line && pp->line) {
793 semantic_error("Lazy pattern can't be used with"
798 if (pp->lazy_line && pp->offset) {
799 semantic_error("Lazy pattern can't be used with offset.\n");
803 if (pp->line && pp->offset) {
804 semantic_error("Offset can't be used with line number.\n");
808 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
809 semantic_error("File always requires line number or "
814 if (pp->offset && !pp->function) {
815 semantic_error("Offset requires an entry function.\n");
819 if (pp->retprobe && !pp->function) {
820 semantic_error("Return probe requires an entry function.\n");
824 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
825 semantic_error("Offset/Line/Lazy pattern can't be used with "
830 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
831 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
836 /* Parse perf-probe event argument */
837 static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
839 char *tmp, *goodname;
840 struct perf_probe_arg_field **fieldp;
842 pr_debug("parsing arg: %s into ", str);
844 tmp = strchr(str, '=');
846 arg->name = strndup(str, tmp - str);
847 if (arg->name == NULL)
849 pr_debug("name:%s ", arg->name);
853 tmp = strchr(str, ':');
854 if (tmp) { /* Type setting */
856 arg->type = strdup(tmp + 1);
857 if (arg->type == NULL)
859 pr_debug("type:%s ", arg->type);
862 tmp = strpbrk(str, "-.[");
863 if (!is_c_varname(str) || !tmp) {
864 /* A variable, register, symbol or special value */
865 arg->var = strdup(str);
866 if (arg->var == NULL)
868 pr_debug("%s\n", arg->var);
872 /* Structure fields or array element */
873 arg->var = strndup(str, tmp - str);
874 if (arg->var == NULL)
877 pr_debug("%s, ", arg->var);
878 fieldp = &arg->field;
881 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
884 if (*tmp == '[') { /* Array */
886 (*fieldp)->index = strtol(str + 1, &tmp, 0);
887 (*fieldp)->ref = true;
888 if (*tmp != ']' || tmp == str + 1) {
889 semantic_error("Array index must be a"
896 } else { /* Structure */
899 (*fieldp)->ref = false;
900 } else if (tmp[1] == '>') {
902 (*fieldp)->ref = true;
904 semantic_error("Argument parse error: %s\n",
908 tmp = strpbrk(str, "-.[");
911 (*fieldp)->name = strndup(str, tmp - str);
912 if ((*fieldp)->name == NULL)
915 goodname = (*fieldp)->name;
916 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
917 fieldp = &(*fieldp)->next;
920 (*fieldp)->name = strdup(str);
921 if ((*fieldp)->name == NULL)
924 goodname = (*fieldp)->name;
925 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
927 /* If no name is specified, set the last field name (not array index)*/
929 arg->name = strdup(goodname);
930 if (arg->name == NULL)
936 /* Parse perf-probe event command */
937 int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
940 int argc, i, ret = 0;
942 argv = argv_split(cmd, &argc);
944 pr_debug("Failed to split arguments.\n");
947 if (argc - 1 > MAX_PROBE_ARGS) {
948 semantic_error("Too many probe arguments (%d).\n", argc - 1);
952 /* Parse probe point */
953 ret = parse_perf_probe_point(argv[0], pev);
957 /* Copy arguments and ensure return probe has no C argument */
958 pev->nargs = argc - 1;
959 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
960 if (pev->args == NULL) {
964 for (i = 0; i < pev->nargs && ret >= 0; i++) {
965 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
967 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
968 semantic_error("You can't specify local variable for"
979 /* Return true if this perf_probe_event requires debuginfo */
980 bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
984 if (pev->point.file || pev->point.line || pev->point.lazy_line)
987 for (i = 0; i < pev->nargs; i++)
988 if (is_c_varname(pev->args[i].var))
994 /* Parse probe_events event into struct probe_point */
995 static int parse_probe_trace_command(const char *cmd,
996 struct probe_trace_event *tev)
998 struct probe_trace_point *tp = &tev->point;
1004 pr_debug("Parsing probe_events: %s\n", cmd);
1005 argv = argv_split(cmd, &argc);
1007 pr_debug("Failed to split arguments.\n");
1011 semantic_error("Too few probe arguments.\n");
1016 /* Scan event and group name. */
1017 ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]",
1018 &pr, (float *)(void *)&tev->group,
1019 (float *)(void *)&tev->event);
1021 semantic_error("Failed to parse event name: %s\n", argv[0]);
1025 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
1027 tp->retprobe = (pr == 'r');
1029 /* Scan function name and offset */
1030 ret = sscanf(argv[1], "%a[^+]+%lu", (float *)(void *)&tp->symbol,
1035 tev->nargs = argc - 2;
1036 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
1037 if (tev->args == NULL) {
1041 for (i = 0; i < tev->nargs; i++) {
1042 p = strchr(argv[i + 2], '=');
1043 if (p) /* We don't need which register is assigned. */
1047 tev->args[i].name = strdup(argv[i + 2]);
1048 /* TODO: parse regs and offset */
1049 tev->args[i].value = strdup(p);
1050 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1061 /* Compose only probe arg */
1062 int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
1064 struct perf_probe_arg_field *field = pa->field;
1068 if (pa->name && pa->var)
1069 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
1071 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
1078 if (field->name[0] == '[')
1079 ret = e_snprintf(tmp, len, "%s", field->name);
1081 ret = e_snprintf(tmp, len, "%s%s",
1082 field->ref ? "->" : ".", field->name);
1087 field = field->next;
1091 ret = e_snprintf(tmp, len, ":%s", pa->type);
1100 pr_debug("Failed to synthesize perf probe argument: %s\n",
1105 /* Compose only probe point (not argument) */
1106 static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
1109 char offs[32] = "", line[32] = "", file[32] = "";
1112 buf = zalloc(MAX_CMDLEN);
1118 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
1123 ret = e_snprintf(line, 32, ":%d", pp->line);
1131 tmp = strchr(pp->file + len - 30, '/');
1132 tmp = tmp ? tmp + 1 : pp->file + len - 30;
1134 ret = e_snprintf(file, 32, "@%s", tmp);
1140 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
1141 offs, pp->retprobe ? "%return" : "", line,
1144 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
1150 pr_debug("Failed to synthesize perf probe point: %s\n",
1158 char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1163 buf = synthesize_perf_probe_point(&pev->point);
1168 for (i = 0; i < pev->nargs; i++) {
1169 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
1182 static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
1183 char **buf, size_t *buflen,
1188 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
1194 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
1206 static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
1207 char *buf, size_t buflen)
1209 struct probe_trace_arg_ref *ref = arg->ref;
1213 /* Argument name or separator */
1215 ret = e_snprintf(buf, buflen, " %s=", arg->name);
1217 ret = e_snprintf(buf, buflen, " ");
1223 /* Special case: @XXX */
1224 if (arg->value[0] == '@' && arg->ref)
1227 /* Dereferencing arguments */
1229 depth = __synthesize_probe_trace_arg_ref(ref, &buf,
1235 /* Print argument value */
1236 if (arg->value[0] == '@' && arg->ref)
1237 ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
1240 ret = e_snprintf(buf, buflen, "%s", arg->value);
1248 ret = e_snprintf(buf, buflen, ")");
1254 /* Print argument type */
1256 ret = e_snprintf(buf, buflen, ":%s", arg->type);
1265 char *synthesize_probe_trace_command(struct probe_trace_event *tev)
1267 struct probe_trace_point *tp = &tev->point;
1271 buf = zalloc(MAX_CMDLEN);
1275 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s+%lu",
1276 tp->retprobe ? 'r' : 'p',
1277 tev->group, tev->event,
1278 tp->symbol, tp->offset);
1282 for (i = 0; i < tev->nargs; i++) {
1283 ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
1296 static int convert_to_perf_probe_event(struct probe_trace_event *tev,
1297 struct perf_probe_event *pev)
1302 /* Convert event/group name */
1303 pev->event = strdup(tev->event);
1304 pev->group = strdup(tev->group);
1305 if (pev->event == NULL || pev->group == NULL)
1308 /* Convert trace_point to probe_point */
1309 ret = kprobe_convert_to_perf_probe(&tev->point, &pev->point);
1313 /* Convert trace_arg to probe_arg */
1314 pev->nargs = tev->nargs;
1315 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1316 if (pev->args == NULL)
1318 for (i = 0; i < tev->nargs && ret >= 0; i++) {
1319 if (tev->args[i].name)
1320 pev->args[i].name = strdup(tev->args[i].name);
1322 ret = synthesize_probe_trace_arg(&tev->args[i],
1324 pev->args[i].name = strdup(buf);
1326 if (pev->args[i].name == NULL && ret >= 0)
1331 clear_perf_probe_event(pev);
1336 void clear_perf_probe_event(struct perf_probe_event *pev)
1338 struct perf_probe_point *pp = &pev->point;
1339 struct perf_probe_arg_field *field, *next;
1351 free(pp->lazy_line);
1352 for (i = 0; i < pev->nargs; i++) {
1353 if (pev->args[i].name)
1354 free(pev->args[i].name);
1355 if (pev->args[i].var)
1356 free(pev->args[i].var);
1357 if (pev->args[i].type)
1358 free(pev->args[i].type);
1359 field = pev->args[i].field;
1370 memset(pev, 0, sizeof(*pev));
1373 static void clear_probe_trace_event(struct probe_trace_event *tev)
1375 struct probe_trace_arg_ref *ref, *next;
1382 if (tev->point.symbol)
1383 free(tev->point.symbol);
1384 for (i = 0; i < tev->nargs; i++) {
1385 if (tev->args[i].name)
1386 free(tev->args[i].name);
1387 if (tev->args[i].value)
1388 free(tev->args[i].value);
1389 if (tev->args[i].type)
1390 free(tev->args[i].type);
1391 ref = tev->args[i].ref;
1400 memset(tev, 0, sizeof(*tev));
1403 static int open_kprobe_events(bool readwrite)
1406 const char *__debugfs;
1409 __debugfs = debugfs_find_mountpoint();
1410 if (__debugfs == NULL) {
1411 pr_warning("Debugfs is not mounted.\n");
1415 ret = e_snprintf(buf, PATH_MAX, "%stracing/kprobe_events", __debugfs);
1417 pr_debug("Opening %s write=%d\n", buf, readwrite);
1418 if (readwrite && !probe_event_dry_run)
1419 ret = open(buf, O_RDWR, O_APPEND);
1421 ret = open(buf, O_RDONLY, 0);
1425 if (errno == ENOENT)
1426 pr_warning("kprobe_events file does not exist - please"
1427 " rebuild kernel with CONFIG_KPROBE_EVENT.\n");
1429 pr_warning("Failed to open kprobe_events file: %s\n",
1435 /* Get raw string list of current kprobe_events */
1436 static struct strlist *get_probe_trace_command_rawlist(int fd)
1440 char buf[MAX_CMDLEN];
1444 sl = strlist__new(true, NULL);
1446 fp = fdopen(dup(fd), "r");
1448 p = fgets(buf, MAX_CMDLEN, fp);
1452 idx = strlen(p) - 1;
1455 ret = strlist__add(sl, buf);
1457 pr_debug("strlist__add failed: %s\n", strerror(-ret));
1458 strlist__delete(sl);
1468 static int show_perf_probe_event(struct perf_probe_event *pev)
1474 /* Synthesize only event probe point */
1475 place = synthesize_perf_probe_point(&pev->point);
1479 ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
1483 printf(" %-20s (on %s", buf, place);
1485 if (pev->nargs > 0) {
1487 for (i = 0; i < pev->nargs; i++) {
1488 ret = synthesize_perf_probe_arg(&pev->args[i],
1500 /* List up current perf-probe events */
1501 int show_perf_probe_events(void)
1504 struct probe_trace_event tev;
1505 struct perf_probe_event pev;
1506 struct strlist *rawlist;
1507 struct str_node *ent;
1510 ret = init_vmlinux();
1514 memset(&tev, 0, sizeof(tev));
1515 memset(&pev, 0, sizeof(pev));
1517 fd = open_kprobe_events(false);
1521 rawlist = get_probe_trace_command_rawlist(fd);
1526 strlist__for_each(ent, rawlist) {
1527 ret = parse_probe_trace_command(ent->s, &tev);
1529 ret = convert_to_perf_probe_event(&tev, &pev);
1531 ret = show_perf_probe_event(&pev);
1533 clear_perf_probe_event(&pev);
1534 clear_probe_trace_event(&tev);
1538 strlist__delete(rawlist);
1543 /* Get current perf-probe event names */
1544 static struct strlist *get_probe_trace_event_names(int fd, bool include_group)
1547 struct strlist *sl, *rawlist;
1548 struct str_node *ent;
1549 struct probe_trace_event tev;
1552 memset(&tev, 0, sizeof(tev));
1553 rawlist = get_probe_trace_command_rawlist(fd);
1554 sl = strlist__new(true, NULL);
1555 strlist__for_each(ent, rawlist) {
1556 ret = parse_probe_trace_command(ent->s, &tev);
1559 if (include_group) {
1560 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
1563 ret = strlist__add(sl, buf);
1565 ret = strlist__add(sl, tev.event);
1566 clear_probe_trace_event(&tev);
1570 strlist__delete(rawlist);
1573 strlist__delete(sl);
1579 static int write_probe_trace_event(int fd, struct probe_trace_event *tev)
1582 char *buf = synthesize_probe_trace_command(tev);
1585 pr_debug("Failed to synthesize probe trace event.\n");
1589 pr_debug("Writing event: %s\n", buf);
1590 if (!probe_event_dry_run) {
1591 ret = write(fd, buf, strlen(buf));
1593 pr_warning("Failed to write event: %s\n",
1600 static int get_new_event_name(char *buf, size_t len, const char *base,
1601 struct strlist *namelist, bool allow_suffix)
1606 ret = e_snprintf(buf, len, "%s", base);
1608 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1611 if (!strlist__has_entry(namelist, buf))
1614 if (!allow_suffix) {
1615 pr_warning("Error: event \"%s\" already exists. "
1616 "(Use -f to force duplicates.)\n", base);
1620 /* Try to add suffix */
1621 for (i = 1; i < MAX_EVENT_INDEX; i++) {
1622 ret = e_snprintf(buf, len, "%s_%d", base, i);
1624 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1627 if (!strlist__has_entry(namelist, buf))
1630 if (i == MAX_EVENT_INDEX) {
1631 pr_warning("Too many events are on the same function.\n");
1638 static int __add_probe_trace_events(struct perf_probe_event *pev,
1639 struct probe_trace_event *tevs,
1640 int ntevs, bool allow_suffix)
1643 struct probe_trace_event *tev = NULL;
1645 const char *event, *group;
1646 struct strlist *namelist;
1648 fd = open_kprobe_events(true);
1651 /* Get current event names */
1652 namelist = get_probe_trace_event_names(fd, false);
1654 pr_debug("Failed to get current event list.\n");
1659 printf("Add new event%s\n", (ntevs > 1) ? "s:" : ":");
1660 for (i = 0; i < ntevs; i++) {
1665 if (pev->point.function)
1666 event = pev->point.function;
1668 event = tev->point.symbol;
1672 group = PERFPROBE_GROUP;
1674 /* Get an unused new event name */
1675 ret = get_new_event_name(buf, 64, event,
1676 namelist, allow_suffix);
1681 tev->event = strdup(event);
1682 tev->group = strdup(group);
1683 if (tev->event == NULL || tev->group == NULL) {
1687 ret = write_probe_trace_event(fd, tev);
1690 /* Add added event name to namelist */
1691 strlist__add(namelist, event);
1693 /* Trick here - save current event/group */
1696 pev->event = tev->event;
1697 pev->group = tev->group;
1698 show_perf_probe_event(pev);
1699 /* Trick here - restore current event/group */
1700 pev->event = (char *)event;
1701 pev->group = (char *)group;
1704 * Probes after the first probe which comes from same
1705 * user input are always allowed to add suffix, because
1706 * there might be several addresses corresponding to
1709 allow_suffix = true;
1713 /* Show how to use the event. */
1714 printf("\nYou can now use it on all perf tools, such as:\n\n");
1715 printf("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group,
1719 strlist__delete(namelist);
1724 static int convert_to_probe_trace_events(struct perf_probe_event *pev,
1725 struct probe_trace_event **tevs,
1726 int max_tevs, const char *module)
1730 struct probe_trace_event *tev;
1732 /* Convert perf_probe_event with debuginfo */
1733 ret = try_to_find_probe_trace_events(pev, tevs, max_tevs, module);
1737 /* Allocate trace event buffer */
1738 tev = *tevs = zalloc(sizeof(struct probe_trace_event));
1742 /* Copy parameters */
1743 tev->point.symbol = strdup(pev->point.function);
1744 if (tev->point.symbol == NULL) {
1748 tev->point.offset = pev->point.offset;
1749 tev->point.retprobe = pev->point.retprobe;
1750 tev->nargs = pev->nargs;
1752 tev->args = zalloc(sizeof(struct probe_trace_arg)
1754 if (tev->args == NULL) {
1758 for (i = 0; i < tev->nargs; i++) {
1759 if (pev->args[i].name) {
1760 tev->args[i].name = strdup(pev->args[i].name);
1761 if (tev->args[i].name == NULL) {
1766 tev->args[i].value = strdup(pev->args[i].var);
1767 if (tev->args[i].value == NULL) {
1771 if (pev->args[i].type) {
1772 tev->args[i].type = strdup(pev->args[i].type);
1773 if (tev->args[i].type == NULL) {
1781 /* Currently just checking function name from symbol map */
1782 sym = __find_kernel_function_by_name(tev->point.symbol, NULL);
1784 pr_warning("Kernel symbol \'%s\' not found.\n",
1792 clear_probe_trace_event(tev);
1798 struct __event_package {
1799 struct perf_probe_event *pev;
1800 struct probe_trace_event *tevs;
1804 int add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
1805 int max_tevs, const char *module, bool force_add)
1808 struct __event_package *pkgs;
1810 pkgs = zalloc(sizeof(struct __event_package) * npevs);
1814 /* Init vmlinux path */
1815 ret = init_vmlinux();
1821 /* Loop 1: convert all events */
1822 for (i = 0; i < npevs; i++) {
1823 pkgs[i].pev = &pevs[i];
1824 /* Convert with or without debuginfo */
1825 ret = convert_to_probe_trace_events(pkgs[i].pev,
1831 pkgs[i].ntevs = ret;
1834 /* Loop 2: add all events */
1835 for (i = 0; i < npevs && ret >= 0; i++)
1836 ret = __add_probe_trace_events(pkgs[i].pev, pkgs[i].tevs,
1837 pkgs[i].ntevs, force_add);
1839 /* Loop 3: cleanup and free trace events */
1840 for (i = 0; i < npevs; i++) {
1841 for (j = 0; j < pkgs[i].ntevs; j++)
1842 clear_probe_trace_event(&pkgs[i].tevs[j]);
1850 static int __del_trace_probe_event(int fd, struct str_node *ent)
1856 /* Convert from perf-probe event to trace-probe event */
1857 ret = e_snprintf(buf, 128, "-:%s", ent->s);
1861 p = strchr(buf + 2, ':');
1863 pr_debug("Internal error: %s should have ':' but not.\n",
1870 pr_debug("Writing event: %s\n", buf);
1871 ret = write(fd, buf, strlen(buf));
1875 printf("Remove event: %s\n", ent->s);
1878 pr_warning("Failed to delete event: %s\n", strerror(-ret));
1882 static int del_trace_probe_event(int fd, const char *group,
1883 const char *event, struct strlist *namelist)
1886 struct str_node *ent, *n;
1887 int found = 0, ret = 0;
1889 ret = e_snprintf(buf, 128, "%s:%s", group, event);
1891 pr_err("Failed to copy event.\n");
1895 if (strpbrk(buf, "*?")) { /* Glob-exp */
1896 strlist__for_each_safe(ent, n, namelist)
1897 if (strglobmatch(ent->s, buf)) {
1899 ret = __del_trace_probe_event(fd, ent);
1902 strlist__remove(namelist, ent);
1905 ent = strlist__find(namelist, buf);
1908 ret = __del_trace_probe_event(fd, ent);
1910 strlist__remove(namelist, ent);
1913 if (found == 0 && ret >= 0)
1914 pr_info("Info: Event \"%s\" does not exist.\n", buf);
1919 int del_perf_probe_events(struct strlist *dellist)
1922 const char *group, *event;
1924 struct str_node *ent;
1925 struct strlist *namelist;
1927 fd = open_kprobe_events(true);
1931 /* Get current event names */
1932 namelist = get_probe_trace_event_names(fd, true);
1933 if (namelist == NULL)
1936 strlist__for_each(ent, dellist) {
1937 str = strdup(ent->s);
1942 pr_debug("Parsing: %s\n", str);
1943 p = strchr(str, ':');
1952 pr_debug("Group: %s, Event: %s\n", group, event);
1953 ret = del_trace_probe_event(fd, group, event, namelist);
1958 strlist__delete(namelist);
1963 /* TODO: don't use a global variable for filter ... */
1964 static struct strfilter *available_func_filter;
1967 * If a symbol corresponds to a function with global binding and
1968 * matches filter return 0. For all others return 1.
1970 static int filter_available_functions(struct map *map __unused,
1973 if (sym->binding == STB_GLOBAL &&
1974 strfilter__compare(available_func_filter, sym->name))
1979 int show_available_funcs(const char *module, struct strfilter *_filter)
1986 ret = init_vmlinux();
1990 map = kernel_get_module_map(module);
1992 pr_err("Failed to find %s map.\n", (module) ? : "kernel");
1995 available_func_filter = _filter;
1996 if (map__load(map, filter_available_functions)) {
1997 pr_err("Failed to load map.\n");
2000 if (!dso__sorted_by_name(map->dso, map->type))
2001 dso__sort_by_name(map->dso, map->type);
2003 dso__fprintf_symbols_by_name(map->dso, map->type, stdout);