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.
22 #include <sys/utsname.h>
23 #include <sys/types.h>
43 #include <api/fs/debugfs.h>
44 #include "trace-event.h" /* For __maybe_unused */
45 #include "probe-event.h"
46 #include "probe-finder.h"
49 #define MAX_CMDLEN 256
50 #define PERFPROBE_GROUP "probe"
52 bool probe_event_dry_run; /* Dry run flag */
54 #define semantic_error(msg ...) pr_err("Semantic error :" msg)
56 /* If there is no space to write, returns -E2BIG. */
57 static int e_snprintf(char *str, size_t size, const char *format, ...)
58 __attribute__((format(printf, 3, 4)));
60 static int e_snprintf(char *str, size_t size, const char *format, ...)
65 ret = vsnprintf(str, size, format, ap);
72 static char *synthesize_perf_probe_point(struct perf_probe_point *pp);
73 static void clear_probe_trace_event(struct probe_trace_event *tev);
74 static struct machine *host_machine;
76 /* Initialize symbol maps and path of vmlinux/modules */
77 static int init_symbol_maps(bool user_only)
81 symbol_conf.sort_by_name = true;
82 ret = symbol__init(NULL);
84 pr_debug("Failed to init symbol map.\n");
88 if (host_machine || user_only) /* already initialized */
91 if (symbol_conf.vmlinux_name)
92 pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
94 host_machine = machine__new_host();
96 pr_debug("machine__new_host() failed.\n");
102 pr_warning("Failed to init vmlinux path.\n");
106 static void exit_symbol_maps(void)
109 machine__delete(host_machine);
115 static struct symbol *__find_kernel_function_by_name(const char *name,
118 return machine__find_kernel_function_by_name(host_machine, name, mapp,
122 static struct symbol *__find_kernel_function(u64 addr, struct map **mapp)
124 return machine__find_kernel_function(host_machine, addr, mapp, NULL);
127 static struct ref_reloc_sym *kernel_get_ref_reloc_sym(void)
129 /* kmap->ref_reloc_sym should be set if host_machine is initialized */
132 if (map__load(host_machine->vmlinux_maps[MAP__FUNCTION], NULL) < 0)
135 kmap = map__kmap(host_machine->vmlinux_maps[MAP__FUNCTION]);
136 return kmap->ref_reloc_sym;
139 static u64 kernel_get_symbol_address_by_name(const char *name, bool reloc)
141 struct ref_reloc_sym *reloc_sym;
145 /* ref_reloc_sym is just a label. Need a special fix*/
146 reloc_sym = kernel_get_ref_reloc_sym();
147 if (reloc_sym && strcmp(name, reloc_sym->name) == 0)
148 return (reloc) ? reloc_sym->addr : reloc_sym->unrelocated_addr;
150 sym = __find_kernel_function_by_name(name, &map);
152 return map->unmap_ip(map, sym->start) -
153 (reloc) ? 0 : map->reloc;
158 static struct map *kernel_get_module_map(const char *module)
161 struct map_groups *grp = &host_machine->kmaps;
163 /* A file path -- this is an offline module */
164 if (module && strchr(module, '/'))
165 return machine__new_module(host_machine, 0, module);
170 for (nd = rb_first(&grp->maps[MAP__FUNCTION]); nd; nd = rb_next(nd)) {
171 struct map *pos = rb_entry(nd, struct map, rb_node);
172 if (strncmp(pos->dso->short_name + 1, module,
173 pos->dso->short_name_len - 2) == 0) {
180 static struct dso *kernel_get_module_dso(const char *module)
184 const char *vmlinux_name;
187 list_for_each_entry(dso, &host_machine->kernel_dsos.head,
189 if (strncmp(dso->short_name + 1, module,
190 dso->short_name_len - 2) == 0)
193 pr_debug("Failed to find module %s.\n", module);
197 map = host_machine->vmlinux_maps[MAP__FUNCTION];
200 vmlinux_name = symbol_conf.vmlinux_name;
202 if (dso__load_vmlinux(dso, map, vmlinux_name, false, NULL) <= 0)
205 if (dso__load_vmlinux_path(dso, map, NULL) <= 0) {
206 pr_debug("Failed to load kernel map.\n");
214 const char *kernel_get_module_path(const char *module)
216 struct dso *dso = kernel_get_module_dso(module);
217 return (dso) ? dso->long_name : NULL;
220 static int convert_exec_to_group(const char *exec, char **result)
222 char *ptr1, *ptr2, *exec_copy;
226 exec_copy = strdup(exec);
230 ptr1 = basename(exec_copy);
236 ptr2 = strpbrk(ptr1, "-._");
239 ret = e_snprintf(buf, 64, "%s_%s", PERFPROBE_GROUP, ptr1);
243 *result = strdup(buf);
244 ret = *result ? 0 : -ENOMEM;
251 static void clear_probe_trace_events(struct probe_trace_event *tevs, int ntevs)
255 for (i = 0; i < ntevs; i++)
256 clear_probe_trace_event(tevs + i);
259 #ifdef HAVE_DWARF_SUPPORT
261 /* Open new debuginfo of given module */
262 static struct debuginfo *open_debuginfo(const char *module, bool silent)
264 const char *path = module;
265 struct debuginfo *ret;
267 if (!module || !strchr(module, '/')) {
268 path = kernel_get_module_path(module);
271 pr_err("Failed to find path of %s module.\n",
276 ret = debuginfo__new(path);
277 if (!ret && !silent) {
278 pr_warning("The %s file has no debug information.\n", path);
279 if (!module || !strtailcmp(path, ".ko"))
280 pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, ");
282 pr_warning("Rebuild with -g, ");
283 pr_warning("or install an appropriate debuginfo package.\n");
289 static int get_text_start_address(const char *exec, unsigned long *address)
294 int fd, ret = -ENOENT;
296 fd = open(exec, O_RDONLY);
300 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
304 if (gelf_getehdr(elf, &ehdr) == NULL)
307 if (!elf_section_by_name(elf, &ehdr, &shdr, ".text", NULL))
310 *address = shdr.sh_addr - shdr.sh_offset;
318 * Convert trace point to probe point with debuginfo
320 static int find_perf_probe_point_from_dwarf(struct probe_trace_point *tp,
321 struct perf_probe_point *pp,
324 struct debuginfo *dinfo = NULL;
325 unsigned long stext = 0;
326 u64 addr = tp->address;
329 /* convert the address to dwarf address */
335 ret = get_text_start_address(tp->module, &stext);
340 addr = kernel_get_symbol_address_by_name(tp->symbol, false);
346 pr_debug("try to find information at %" PRIx64 " in %s\n", addr,
347 tp->module ? : "kernel");
349 dinfo = open_debuginfo(tp->module, verbose == 0);
351 ret = debuginfo__find_probe_point(dinfo,
352 (unsigned long)addr, pp);
353 debuginfo__delete(dinfo);
358 pp->retprobe = tp->retprobe;
362 pr_debug("Failed to find corresponding probes from debuginfo.\n");
363 return ret ? : -ENOENT;
366 static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs,
367 int ntevs, const char *exec)
370 unsigned long stext = 0;
375 ret = get_text_start_address(exec, &stext);
379 for (i = 0; i < ntevs && ret >= 0; i++) {
380 /* point.address is the addres of point.symbol + point.offset */
381 tevs[i].point.address -= stext;
382 tevs[i].point.module = strdup(exec);
383 if (!tevs[i].point.module) {
387 tevs[i].uprobes = true;
393 static int add_module_to_probe_trace_events(struct probe_trace_event *tevs,
394 int ntevs, const char *module)
402 tmp = strrchr(module, '/');
404 /* This is a module path -- get the module name */
405 module = strdup(tmp + 1);
408 tmp = strchr(module, '.');
411 tmp = (char *)module; /* For free() */
414 for (i = 0; i < ntevs; i++) {
415 tevs[i].point.module = strdup(module);
416 if (!tevs[i].point.module) {
426 /* Post processing the probe events */
427 static int post_process_probe_trace_events(struct probe_trace_event *tevs,
428 int ntevs, const char *module,
431 struct ref_reloc_sym *reloc_sym;
436 return add_exec_to_probe_trace_events(tevs, ntevs, module);
438 /* Note that currently ref_reloc_sym based probe is not for drivers */
440 return add_module_to_probe_trace_events(tevs, ntevs, module);
442 reloc_sym = kernel_get_ref_reloc_sym();
444 pr_warning("Relocated base symbol is not found!\n");
448 for (i = 0; i < ntevs; i++) {
449 if (tevs[i].point.address && !tevs[i].point.retprobe) {
450 tmp = strdup(reloc_sym->name);
453 free(tevs[i].point.symbol);
454 tevs[i].point.symbol = tmp;
455 tevs[i].point.offset = tevs[i].point.address -
456 reloc_sym->unrelocated_addr;
462 /* Try to find perf_probe_event with debuginfo */
463 static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
464 struct probe_trace_event **tevs,
465 int max_tevs, const char *target)
467 bool need_dwarf = perf_probe_event_need_dwarf(pev);
468 struct debuginfo *dinfo;
471 dinfo = open_debuginfo(target, !need_dwarf);
476 pr_debug("Could not open debuginfo. Try to use symbols.\n");
480 pr_debug("Try to find probe point from debuginfo.\n");
481 /* Searching trace events corresponding to a probe event */
482 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs, max_tevs);
484 debuginfo__delete(dinfo);
486 if (ntevs > 0) { /* Succeeded to find trace events */
487 pr_debug("Found %d probe_trace_events.\n", ntevs);
488 ret = post_process_probe_trace_events(*tevs, ntevs,
489 target, pev->uprobes);
491 clear_probe_trace_events(*tevs, ntevs);
494 return ret < 0 ? ret : ntevs;
497 if (ntevs == 0) { /* No error but failed to find probe point. */
498 pr_warning("Probe point '%s' not found in debuginfo.\n",
499 synthesize_perf_probe_point(&pev->point));
504 /* Error path : ntevs < 0 */
505 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
506 if (ntevs == -EBADF) {
507 pr_warning("Warning: No dwarf info found in the vmlinux - "
508 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
510 pr_debug("Trying to use symbols.\n");
518 * Find a src file from a DWARF tag path. Prepend optional source path prefix
519 * and chop off leading directories that do not exist. Result is passed back as
520 * a newly allocated path on success.
521 * Return 0 if file was found and readable, -errno otherwise.
523 static int get_real_path(const char *raw_path, const char *comp_dir,
526 const char *prefix = symbol_conf.source_prefix;
529 if (raw_path[0] != '/' && comp_dir)
530 /* If not an absolute path, try to use comp_dir */
533 if (access(raw_path, R_OK) == 0) {
534 *new_path = strdup(raw_path);
541 *new_path = malloc((strlen(prefix) + strlen(raw_path) + 2));
546 sprintf(*new_path, "%s/%s", prefix, raw_path);
548 if (access(*new_path, R_OK) == 0)
551 if (!symbol_conf.source_prefix)
552 /* In case of searching comp_dir, don't retry */
560 raw_path = strchr(++raw_path, '/');
574 #define LINEBUF_SIZE 256
575 #define NR_ADDITIONAL_LINES 2
577 static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
579 char buf[LINEBUF_SIZE], sbuf[STRERR_BUFSIZE];
580 const char *color = show_num ? "" : PERF_COLOR_BLUE;
581 const char *prefix = NULL;
584 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
589 prefix = show_num ? "%7d " : " ";
590 color_fprintf(stdout, color, prefix, l);
592 color_fprintf(stdout, color, "%s", buf);
594 } while (strchr(buf, '\n') == NULL);
599 pr_warning("File read error: %s\n",
600 strerror_r(errno, sbuf, sizeof(sbuf)));
606 static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
608 int rv = __show_one_line(fp, l, skip, show_num);
610 pr_warning("Source file is shorter than expected.\n");
616 #define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
617 #define show_one_line(f,l) _show_one_line(f,l,false,false)
618 #define skip_one_line(f,l) _show_one_line(f,l,true,false)
619 #define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
622 * Show line-range always requires debuginfo to find source file and
625 static int __show_line_range(struct line_range *lr, const char *module)
629 struct debuginfo *dinfo;
633 char sbuf[STRERR_BUFSIZE];
635 /* Search a line range */
636 dinfo = open_debuginfo(module, false);
640 ret = debuginfo__find_line_range(dinfo, lr);
641 debuginfo__delete(dinfo);
642 if (ret == 0 || ret == -ENOENT) {
643 pr_warning("Specified source line is not found.\n");
645 } else if (ret < 0) {
646 pr_warning("Debuginfo analysis failed.\n");
650 /* Convert source file path */
652 ret = get_real_path(tmp, lr->comp_dir, &lr->path);
653 free(tmp); /* Free old path */
655 pr_warning("Failed to find source file path.\n");
662 fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
663 lr->start - lr->offset);
665 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
667 fp = fopen(lr->path, "r");
669 pr_warning("Failed to open %s: %s\n", lr->path,
670 strerror_r(errno, sbuf, sizeof(sbuf)));
673 /* Skip to starting line number */
674 while (l < lr->start) {
675 ret = skip_one_line(fp, l++);
680 intlist__for_each(ln, lr->line_list) {
681 for (; ln->i > l; l++) {
682 ret = show_one_line(fp, l - lr->offset);
686 ret = show_one_line_with_num(fp, l++ - lr->offset);
691 if (lr->end == INT_MAX)
692 lr->end = l + NR_ADDITIONAL_LINES;
693 while (l <= lr->end) {
694 ret = show_one_line_or_eof(fp, l++ - lr->offset);
703 int show_line_range(struct line_range *lr, const char *module, bool user)
707 ret = init_symbol_maps(user);
710 ret = __show_line_range(lr, module);
716 static int show_available_vars_at(struct debuginfo *dinfo,
717 struct perf_probe_event *pev,
718 int max_vls, struct strfilter *_filter,
723 struct str_node *node;
724 struct variable_list *vls = NULL, *vl;
727 buf = synthesize_perf_probe_point(&pev->point);
730 pr_debug("Searching variables at %s\n", buf);
732 ret = debuginfo__find_available_vars_at(dinfo, pev, &vls,
735 if (ret == 0 || ret == -ENOENT) {
736 pr_err("Failed to find the address of %s\n", buf);
739 pr_warning("Debuginfo analysis failed.\n");
743 /* Some variables are found */
744 fprintf(stdout, "Available variables at %s\n", buf);
745 for (i = 0; i < ret; i++) {
748 * A probe point might be converted to
749 * several trace points.
751 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
753 zfree(&vl->point.symbol);
756 strlist__for_each(node, vl->vars) {
757 var = strchr(node->s, '\t') + 1;
758 if (strfilter__compare(_filter, var)) {
759 fprintf(stdout, "\t\t%s\n", node->s);
763 strlist__delete(vl->vars);
766 fprintf(stdout, "\t\t(No matched variables)\n");
774 /* Show available variables on given probe point */
775 int show_available_vars(struct perf_probe_event *pevs, int npevs,
776 int max_vls, const char *module,
777 struct strfilter *_filter, bool externs)
780 struct debuginfo *dinfo;
782 ret = init_symbol_maps(pevs->uprobes);
786 dinfo = open_debuginfo(module, false);
794 for (i = 0; i < npevs && ret >= 0; i++)
795 ret = show_available_vars_at(dinfo, &pevs[i], max_vls, _filter,
798 debuginfo__delete(dinfo);
804 #else /* !HAVE_DWARF_SUPPORT */
807 find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused,
808 struct perf_probe_point *pp __maybe_unused,
809 bool is_kprobe __maybe_unused)
814 static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
815 struct probe_trace_event **tevs __maybe_unused,
816 int max_tevs __maybe_unused,
817 const char *target __maybe_unused)
819 if (perf_probe_event_need_dwarf(pev)) {
820 pr_warning("Debuginfo-analysis is not supported.\n");
827 int show_line_range(struct line_range *lr __maybe_unused,
828 const char *module __maybe_unused,
829 bool user __maybe_unused)
831 pr_warning("Debuginfo-analysis is not supported.\n");
835 int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
836 int npevs __maybe_unused, int max_vls __maybe_unused,
837 const char *module __maybe_unused,
838 struct strfilter *filter __maybe_unused,
839 bool externs __maybe_unused)
841 pr_warning("Debuginfo-analysis is not supported.\n");
846 void line_range__clear(struct line_range *lr)
852 intlist__delete(lr->line_list);
853 memset(lr, 0, sizeof(*lr));
856 int line_range__init(struct line_range *lr)
858 memset(lr, 0, sizeof(*lr));
859 lr->line_list = intlist__new(NULL);
866 static int parse_line_num(char **ptr, int *val, const char *what)
868 const char *start = *ptr;
871 *val = strtol(*ptr, ptr, 0);
872 if (errno || *ptr == start) {
873 semantic_error("'%s' is not a valid number.\n", what);
880 * Stuff 'lr' according to the line range described by 'arg'.
881 * The line range syntax is described by:
883 * SRC[:SLN[+NUM|-ELN]]
884 * FNC[@SRC][:SLN[+NUM|-ELN]]
886 int parse_line_range_desc(const char *arg, struct line_range *lr)
888 char *range, *file, *name = strdup(arg);
897 range = strchr(name, ':');
901 err = parse_line_num(&range, &lr->start, "start line");
905 if (*range == '+' || *range == '-') {
906 const char c = *range++;
908 err = parse_line_num(&range, &lr->end, "end line");
913 lr->end += lr->start;
915 * Adjust the number of lines here.
916 * If the number of lines == 1, the
917 * the end of line should be equal to
924 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
927 if (lr->start > lr->end) {
928 semantic_error("Start line must be smaller"
929 " than end line.\n");
932 if (*range != '\0') {
933 semantic_error("Tailing with invalid str '%s'.\n", range);
938 file = strchr(name, '@');
941 lr->file = strdup(++file);
942 if (lr->file == NULL) {
947 } else if (strchr(name, '.'))
958 /* Check the name is good for event/group */
959 static bool check_event_name(const char *name)
961 if (!isalpha(*name) && *name != '_')
963 while (*++name != '\0') {
964 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
970 /* Parse probepoint definition. */
971 static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
973 struct perf_probe_point *pp = &pev->point;
978 * perf probe [EVENT=]SRC[:LN|;PTN]
979 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
981 * TODO:Group name support
984 ptr = strpbrk(arg, ";=@+%");
985 if (ptr && *ptr == '=') { /* Event name */
988 if (strchr(arg, ':')) {
989 semantic_error("Group name is not supported yet.\n");
992 if (!check_event_name(arg)) {
993 semantic_error("%s is bad for event name -it must "
994 "follow C symbol-naming rule.\n", arg);
997 pev->event = strdup(arg);
998 if (pev->event == NULL)
1004 ptr = strpbrk(arg, ";:+@%");
1014 /* Check arg is function or file and copy it */
1015 if (strchr(tmp, '.')) /* File */
1020 /* Parse other options */
1024 if (c == ';') { /* Lazy pattern must be the last part */
1025 pp->lazy_line = strdup(arg);
1026 if (pp->lazy_line == NULL)
1030 ptr = strpbrk(arg, ";:+@%");
1036 case ':': /* Line number */
1037 pp->line = strtoul(arg, &tmp, 0);
1039 semantic_error("There is non-digit char"
1040 " in line number.\n");
1044 case '+': /* Byte offset from a symbol */
1045 pp->offset = strtoul(arg, &tmp, 0);
1047 semantic_error("There is non-digit character"
1052 case '@': /* File name */
1054 semantic_error("SRC@SRC is not allowed.\n");
1057 pp->file = strdup(arg);
1058 if (pp->file == NULL)
1061 case '%': /* Probe places */
1062 if (strcmp(arg, "return") == 0) {
1064 } else { /* Others not supported yet */
1065 semantic_error("%%%s is not supported.\n", arg);
1069 default: /* Buggy case */
1070 pr_err("This program has a bug at %s:%d.\n",
1071 __FILE__, __LINE__);
1077 /* Exclusion check */
1078 if (pp->lazy_line && pp->line) {
1079 semantic_error("Lazy pattern can't be used with"
1084 if (pp->lazy_line && pp->offset) {
1085 semantic_error("Lazy pattern can't be used with offset.\n");
1089 if (pp->line && pp->offset) {
1090 semantic_error("Offset can't be used with line number.\n");
1094 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
1095 semantic_error("File always requires line number or "
1100 if (pp->offset && !pp->function) {
1101 semantic_error("Offset requires an entry function.\n");
1105 if (pp->retprobe && !pp->function) {
1106 semantic_error("Return probe requires an entry function.\n");
1110 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
1111 semantic_error("Offset/Line/Lazy pattern can't be used with "
1116 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
1117 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
1122 /* Parse perf-probe event argument */
1123 static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
1125 char *tmp, *goodname;
1126 struct perf_probe_arg_field **fieldp;
1128 pr_debug("parsing arg: %s into ", str);
1130 tmp = strchr(str, '=');
1132 arg->name = strndup(str, tmp - str);
1133 if (arg->name == NULL)
1135 pr_debug("name:%s ", arg->name);
1139 tmp = strchr(str, ':');
1140 if (tmp) { /* Type setting */
1142 arg->type = strdup(tmp + 1);
1143 if (arg->type == NULL)
1145 pr_debug("type:%s ", arg->type);
1148 tmp = strpbrk(str, "-.[");
1149 if (!is_c_varname(str) || !tmp) {
1150 /* A variable, register, symbol or special value */
1151 arg->var = strdup(str);
1152 if (arg->var == NULL)
1154 pr_debug("%s\n", arg->var);
1158 /* Structure fields or array element */
1159 arg->var = strndup(str, tmp - str);
1160 if (arg->var == NULL)
1162 goodname = arg->var;
1163 pr_debug("%s, ", arg->var);
1164 fieldp = &arg->field;
1167 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
1168 if (*fieldp == NULL)
1170 if (*tmp == '[') { /* Array */
1172 (*fieldp)->index = strtol(str + 1, &tmp, 0);
1173 (*fieldp)->ref = true;
1174 if (*tmp != ']' || tmp == str + 1) {
1175 semantic_error("Array index must be a"
1182 } else { /* Structure */
1185 (*fieldp)->ref = false;
1186 } else if (tmp[1] == '>') {
1188 (*fieldp)->ref = true;
1190 semantic_error("Argument parse error: %s\n",
1194 tmp = strpbrk(str, "-.[");
1197 (*fieldp)->name = strndup(str, tmp - str);
1198 if ((*fieldp)->name == NULL)
1201 goodname = (*fieldp)->name;
1202 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
1203 fieldp = &(*fieldp)->next;
1206 (*fieldp)->name = strdup(str);
1207 if ((*fieldp)->name == NULL)
1210 goodname = (*fieldp)->name;
1211 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
1213 /* If no name is specified, set the last field name (not array index)*/
1215 arg->name = strdup(goodname);
1216 if (arg->name == NULL)
1222 /* Parse perf-probe event command */
1223 int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
1226 int argc, i, ret = 0;
1228 argv = argv_split(cmd, &argc);
1230 pr_debug("Failed to split arguments.\n");
1233 if (argc - 1 > MAX_PROBE_ARGS) {
1234 semantic_error("Too many probe arguments (%d).\n", argc - 1);
1238 /* Parse probe point */
1239 ret = parse_perf_probe_point(argv[0], pev);
1243 /* Copy arguments and ensure return probe has no C argument */
1244 pev->nargs = argc - 1;
1245 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1246 if (pev->args == NULL) {
1250 for (i = 0; i < pev->nargs && ret >= 0; i++) {
1251 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1253 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
1254 semantic_error("You can't specify local variable for"
1265 /* Return true if this perf_probe_event requires debuginfo */
1266 bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
1270 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1273 for (i = 0; i < pev->nargs; i++)
1274 if (is_c_varname(pev->args[i].var))
1280 /* Parse probe_events event into struct probe_point */
1281 static int parse_probe_trace_command(const char *cmd,
1282 struct probe_trace_event *tev)
1284 struct probe_trace_point *tp = &tev->point;
1287 char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
1291 pr_debug("Parsing probe_events: %s\n", cmd);
1292 argv = argv_split(cmd, &argc);
1294 pr_debug("Failed to split arguments.\n");
1298 semantic_error("Too few probe arguments.\n");
1303 /* Scan event and group name. */
1304 argv0_str = strdup(argv[0]);
1305 if (argv0_str == NULL) {
1309 fmt1_str = strtok_r(argv0_str, ":", &fmt);
1310 fmt2_str = strtok_r(NULL, "/", &fmt);
1311 fmt3_str = strtok_r(NULL, " \t", &fmt);
1312 if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
1313 || fmt3_str == NULL) {
1314 semantic_error("Failed to parse event name: %s\n", argv[0]);
1319 tev->group = strdup(fmt2_str);
1320 tev->event = strdup(fmt3_str);
1321 if (tev->group == NULL || tev->event == NULL) {
1325 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
1327 tp->retprobe = (pr == 'r');
1329 /* Scan module name(if there), function name and offset */
1330 p = strchr(argv[1], ':');
1332 tp->module = strndup(argv[1], p - argv[1]);
1336 fmt1_str = strtok_r(p, "+", &fmt);
1337 if (fmt1_str[0] == '0') /* only the address started with 0x */
1338 tp->address = strtoul(fmt1_str, NULL, 0);
1340 /* Only the symbol-based probe has offset */
1341 tp->symbol = strdup(fmt1_str);
1342 if (tp->symbol == NULL) {
1346 fmt2_str = strtok_r(NULL, "", &fmt);
1347 if (fmt2_str == NULL)
1350 tp->offset = strtoul(fmt2_str, NULL, 10);
1353 tev->nargs = argc - 2;
1354 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
1355 if (tev->args == NULL) {
1359 for (i = 0; i < tev->nargs; i++) {
1360 p = strchr(argv[i + 2], '=');
1361 if (p) /* We don't need which register is assigned. */
1365 tev->args[i].name = strdup(argv[i + 2]);
1366 /* TODO: parse regs and offset */
1367 tev->args[i].value = strdup(p);
1368 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1380 /* Compose only probe arg */
1381 int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
1383 struct perf_probe_arg_field *field = pa->field;
1387 if (pa->name && pa->var)
1388 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
1390 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
1397 if (field->name[0] == '[')
1398 ret = e_snprintf(tmp, len, "%s", field->name);
1400 ret = e_snprintf(tmp, len, "%s%s",
1401 field->ref ? "->" : ".", field->name);
1406 field = field->next;
1410 ret = e_snprintf(tmp, len, ":%s", pa->type);
1419 pr_debug("Failed to synthesize perf probe argument: %d\n", ret);
1423 /* Compose only probe point (not argument) */
1424 static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
1427 char offs[32] = "", line[32] = "", file[32] = "";
1430 buf = zalloc(MAX_CMDLEN);
1436 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
1441 ret = e_snprintf(line, 32, ":%d", pp->line);
1449 tmp = strchr(pp->file + len - 30, '/');
1450 tmp = tmp ? tmp + 1 : pp->file + len - 30;
1452 ret = e_snprintf(file, 32, "@%s", tmp);
1458 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
1459 offs, pp->retprobe ? "%return" : "", line,
1462 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
1468 pr_debug("Failed to synthesize perf probe point: %d\n", ret);
1474 char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1479 buf = synthesize_perf_probe_point(&pev->point);
1484 for (i = 0; i < pev->nargs; i++) {
1485 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
1498 static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
1499 char **buf, size_t *buflen,
1504 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
1510 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
1522 static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
1523 char *buf, size_t buflen)
1525 struct probe_trace_arg_ref *ref = arg->ref;
1529 /* Argument name or separator */
1531 ret = e_snprintf(buf, buflen, " %s=", arg->name);
1533 ret = e_snprintf(buf, buflen, " ");
1539 /* Special case: @XXX */
1540 if (arg->value[0] == '@' && arg->ref)
1543 /* Dereferencing arguments */
1545 depth = __synthesize_probe_trace_arg_ref(ref, &buf,
1551 /* Print argument value */
1552 if (arg->value[0] == '@' && arg->ref)
1553 ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
1556 ret = e_snprintf(buf, buflen, "%s", arg->value);
1564 ret = e_snprintf(buf, buflen, ")");
1570 /* Print argument type */
1572 ret = e_snprintf(buf, buflen, ":%s", arg->type);
1581 char *synthesize_probe_trace_command(struct probe_trace_event *tev)
1583 struct probe_trace_point *tp = &tev->point;
1587 buf = zalloc(MAX_CMDLEN);
1591 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
1592 tev->group, tev->event);
1596 /* Uprobes must have tp->address and tp->module */
1597 if (tev->uprobes && (!tp->address || !tp->module))
1600 /* Use the tp->address for uprobes */
1602 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s:0x%lx",
1603 tp->module, tp->address);
1605 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s%s%s+%lu",
1606 tp->module ?: "", tp->module ? ":" : "",
1607 tp->symbol, tp->offset);
1613 for (i = 0; i < tev->nargs; i++) {
1614 ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
1627 static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
1628 struct perf_probe_point *pp,
1631 struct symbol *sym = NULL;
1637 map = dso__new_map(tp->module);
1641 sym = map__find_symbol(map, addr, NULL);
1643 addr = kernel_get_symbol_address_by_name(tp->symbol, true);
1646 sym = __find_kernel_function(addr, &map);
1652 pp->retprobe = tp->retprobe;
1653 pp->offset = addr - map->unmap_ip(map, sym->start);
1654 pp->function = strdup(sym->name);
1655 ret = pp->function ? 0 : -ENOMEM;
1658 if (map && !is_kprobe) {
1659 dso__delete(map->dso);
1666 static int convert_to_perf_probe_point(struct probe_trace_point *tp,
1667 struct perf_probe_point *pp,
1673 ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe);
1676 ret = find_perf_probe_point_from_map(tp, pp, is_kprobe);
1680 pr_debug("Failed to find probe point from both of dwarf and map.\n");
1683 pp->function = strdup(tp->symbol);
1684 pp->offset = tp->offset;
1685 } else if (!tp->module && !is_kprobe) {
1686 ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address);
1689 pp->function = strdup(buf);
1692 if (pp->function == NULL)
1695 pp->retprobe = tp->retprobe;
1700 static int convert_to_perf_probe_event(struct probe_trace_event *tev,
1701 struct perf_probe_event *pev, bool is_kprobe)
1706 /* Convert event/group name */
1707 pev->event = strdup(tev->event);
1708 pev->group = strdup(tev->group);
1709 if (pev->event == NULL || pev->group == NULL)
1712 /* Convert trace_point to probe_point */
1713 ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
1717 /* Convert trace_arg to probe_arg */
1718 pev->nargs = tev->nargs;
1719 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1720 if (pev->args == NULL)
1722 for (i = 0; i < tev->nargs && ret >= 0; i++) {
1723 if (tev->args[i].name)
1724 pev->args[i].name = strdup(tev->args[i].name);
1726 ret = synthesize_probe_trace_arg(&tev->args[i],
1728 pev->args[i].name = strdup(buf);
1730 if (pev->args[i].name == NULL && ret >= 0)
1735 clear_perf_probe_event(pev);
1740 void clear_perf_probe_event(struct perf_probe_event *pev)
1742 struct perf_probe_point *pp = &pev->point;
1743 struct perf_probe_arg_field *field, *next;
1750 free(pp->lazy_line);
1752 for (i = 0; i < pev->nargs; i++) {
1753 free(pev->args[i].name);
1754 free(pev->args[i].var);
1755 free(pev->args[i].type);
1756 field = pev->args[i].field;
1759 zfree(&field->name);
1765 memset(pev, 0, sizeof(*pev));
1768 static void clear_probe_trace_event(struct probe_trace_event *tev)
1770 struct probe_trace_arg_ref *ref, *next;
1775 free(tev->point.symbol);
1776 free(tev->point.module);
1777 for (i = 0; i < tev->nargs; i++) {
1778 free(tev->args[i].name);
1779 free(tev->args[i].value);
1780 free(tev->args[i].type);
1781 ref = tev->args[i].ref;
1789 memset(tev, 0, sizeof(*tev));
1792 static void print_open_warning(int err, bool is_kprobe)
1794 char sbuf[STRERR_BUFSIZE];
1796 if (err == -ENOENT) {
1800 config = "CONFIG_UPROBE_EVENTS";
1802 config = "CONFIG_KPROBE_EVENTS";
1804 pr_warning("%cprobe_events file does not exist"
1805 " - please rebuild kernel with %s.\n",
1806 is_kprobe ? 'k' : 'u', config);
1807 } else if (err == -ENOTSUP)
1808 pr_warning("Debugfs is not mounted.\n");
1810 pr_warning("Failed to open %cprobe_events: %s\n",
1811 is_kprobe ? 'k' : 'u',
1812 strerror_r(-err, sbuf, sizeof(sbuf)));
1815 static void print_both_open_warning(int kerr, int uerr)
1817 /* Both kprobes and uprobes are disabled, warn it. */
1818 if (kerr == -ENOTSUP && uerr == -ENOTSUP)
1819 pr_warning("Debugfs is not mounted.\n");
1820 else if (kerr == -ENOENT && uerr == -ENOENT)
1821 pr_warning("Please rebuild kernel with CONFIG_KPROBE_EVENTS "
1822 "or/and CONFIG_UPROBE_EVENTS.\n");
1824 char sbuf[STRERR_BUFSIZE];
1825 pr_warning("Failed to open kprobe events: %s.\n",
1826 strerror_r(-kerr, sbuf, sizeof(sbuf)));
1827 pr_warning("Failed to open uprobe events: %s.\n",
1828 strerror_r(-uerr, sbuf, sizeof(sbuf)));
1832 static int open_probe_events(const char *trace_file, bool readwrite)
1835 const char *__debugfs;
1838 __debugfs = debugfs_find_mountpoint();
1839 if (__debugfs == NULL)
1842 ret = e_snprintf(buf, PATH_MAX, "%s/%s", __debugfs, trace_file);
1844 pr_debug("Opening %s write=%d\n", buf, readwrite);
1845 if (readwrite && !probe_event_dry_run)
1846 ret = open(buf, O_RDWR, O_APPEND);
1848 ret = open(buf, O_RDONLY, 0);
1856 static int open_kprobe_events(bool readwrite)
1858 return open_probe_events("tracing/kprobe_events", readwrite);
1861 static int open_uprobe_events(bool readwrite)
1863 return open_probe_events("tracing/uprobe_events", readwrite);
1866 /* Get raw string list of current kprobe_events or uprobe_events */
1867 static struct strlist *get_probe_trace_command_rawlist(int fd)
1871 char buf[MAX_CMDLEN];
1875 sl = strlist__new(true, NULL);
1877 fp = fdopen(dup(fd), "r");
1879 p = fgets(buf, MAX_CMDLEN, fp);
1883 idx = strlen(p) - 1;
1886 ret = strlist__add(sl, buf);
1888 pr_debug("strlist__add failed (%d)\n", ret);
1889 strlist__delete(sl);
1899 static int show_perf_probe_event(struct perf_probe_event *pev,
1906 /* Synthesize only event probe point */
1907 place = synthesize_perf_probe_point(&pev->point);
1911 ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
1915 pr_info(" %-20s (on %s", buf, place);
1917 pr_info(" in %s", module);
1919 if (pev->nargs > 0) {
1921 for (i = 0; i < pev->nargs; i++) {
1922 ret = synthesize_perf_probe_arg(&pev->args[i],
1926 pr_info(" %s", buf);
1934 static int __show_perf_probe_events(int fd, bool is_kprobe)
1937 struct probe_trace_event tev;
1938 struct perf_probe_event pev;
1939 struct strlist *rawlist;
1940 struct str_node *ent;
1942 memset(&tev, 0, sizeof(tev));
1943 memset(&pev, 0, sizeof(pev));
1945 rawlist = get_probe_trace_command_rawlist(fd);
1949 strlist__for_each(ent, rawlist) {
1950 ret = parse_probe_trace_command(ent->s, &tev);
1952 ret = convert_to_perf_probe_event(&tev, &pev,
1955 ret = show_perf_probe_event(&pev,
1958 clear_perf_probe_event(&pev);
1959 clear_probe_trace_event(&tev);
1963 strlist__delete(rawlist);
1968 /* List up current perf-probe events */
1969 int show_perf_probe_events(void)
1971 int kp_fd, up_fd, ret;
1975 ret = init_symbol_maps(false);
1979 kp_fd = open_kprobe_events(false);
1981 ret = __show_perf_probe_events(kp_fd, true);
1987 up_fd = open_uprobe_events(false);
1988 if (kp_fd < 0 && up_fd < 0) {
1989 print_both_open_warning(kp_fd, up_fd);
1995 ret = __show_perf_probe_events(up_fd, false);
2003 /* Get current perf-probe event names */
2004 static struct strlist *get_probe_trace_event_names(int fd, bool include_group)
2007 struct strlist *sl, *rawlist;
2008 struct str_node *ent;
2009 struct probe_trace_event tev;
2012 memset(&tev, 0, sizeof(tev));
2013 rawlist = get_probe_trace_command_rawlist(fd);
2016 sl = strlist__new(true, NULL);
2017 strlist__for_each(ent, rawlist) {
2018 ret = parse_probe_trace_command(ent->s, &tev);
2021 if (include_group) {
2022 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
2025 ret = strlist__add(sl, buf);
2027 ret = strlist__add(sl, tev.event);
2028 clear_probe_trace_event(&tev);
2032 strlist__delete(rawlist);
2035 strlist__delete(sl);
2041 static int write_probe_trace_event(int fd, struct probe_trace_event *tev)
2044 char *buf = synthesize_probe_trace_command(tev);
2045 char sbuf[STRERR_BUFSIZE];
2048 pr_debug("Failed to synthesize probe trace event.\n");
2052 pr_debug("Writing event: %s\n", buf);
2053 if (!probe_event_dry_run) {
2054 ret = write(fd, buf, strlen(buf));
2057 pr_warning("Failed to write event: %s\n",
2058 strerror_r(errno, sbuf, sizeof(sbuf)));
2065 static int get_new_event_name(char *buf, size_t len, const char *base,
2066 struct strlist *namelist, bool allow_suffix)
2071 ret = e_snprintf(buf, len, "%s", base);
2073 pr_debug("snprintf() failed: %d\n", ret);
2076 if (!strlist__has_entry(namelist, buf))
2079 if (!allow_suffix) {
2080 pr_warning("Error: event \"%s\" already exists. "
2081 "(Use -f to force duplicates.)\n", base);
2085 /* Try to add suffix */
2086 for (i = 1; i < MAX_EVENT_INDEX; i++) {
2087 ret = e_snprintf(buf, len, "%s_%d", base, i);
2089 pr_debug("snprintf() failed: %d\n", ret);
2092 if (!strlist__has_entry(namelist, buf))
2095 if (i == MAX_EVENT_INDEX) {
2096 pr_warning("Too many events are on the same function.\n");
2103 static int __add_probe_trace_events(struct perf_probe_event *pev,
2104 struct probe_trace_event *tevs,
2105 int ntevs, bool allow_suffix)
2108 struct probe_trace_event *tev = NULL;
2110 const char *event, *group;
2111 struct strlist *namelist;
2114 fd = open_uprobe_events(true);
2116 fd = open_kprobe_events(true);
2119 print_open_warning(fd, !pev->uprobes);
2123 /* Get current event names */
2124 namelist = get_probe_trace_event_names(fd, false);
2126 pr_debug("Failed to get current event list.\n");
2131 pr_info("Added new event%s\n", (ntevs > 1) ? "s:" : ":");
2132 for (i = 0; i < ntevs; i++) {
2137 if (pev->point.function)
2138 event = pev->point.function;
2140 event = tev->point.symbol;
2144 group = PERFPROBE_GROUP;
2146 /* Get an unused new event name */
2147 ret = get_new_event_name(buf, 64, event,
2148 namelist, allow_suffix);
2153 tev->event = strdup(event);
2154 tev->group = strdup(group);
2155 if (tev->event == NULL || tev->group == NULL) {
2159 ret = write_probe_trace_event(fd, tev);
2162 /* Add added event name to namelist */
2163 strlist__add(namelist, event);
2165 /* Trick here - save current event/group */
2168 pev->event = tev->event;
2169 pev->group = tev->group;
2170 show_perf_probe_event(pev, tev->point.module);
2171 /* Trick here - restore current event/group */
2172 pev->event = (char *)event;
2173 pev->group = (char *)group;
2176 * Probes after the first probe which comes from same
2177 * user input are always allowed to add suffix, because
2178 * there might be several addresses corresponding to
2181 allow_suffix = true;
2185 /* Show how to use the event. */
2186 pr_info("\nYou can now use it in all perf tools, such as:\n\n");
2187 pr_info("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group,
2191 strlist__delete(namelist);
2196 static int find_probe_functions(struct map *map, char *name)
2201 map__for_each_symbol_by_name(map, name, sym) {
2202 if (sym->binding == STB_GLOBAL || sym->binding == STB_LOCAL)
2209 #define strdup_or_goto(str, label) \
2210 ({ char *__p = strdup(str); if (!__p) goto label; __p; })
2213 * Find probe function addresses from map.
2214 * Return an error or the number of found probe_trace_event
2216 static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
2217 struct probe_trace_event **tevs,
2218 int max_tevs, const char *target)
2220 struct map *map = NULL;
2221 struct kmap *kmap = NULL;
2222 struct ref_reloc_sym *reloc_sym = NULL;
2224 struct probe_trace_event *tev;
2225 struct perf_probe_point *pp = &pev->point;
2226 struct probe_trace_point *tp;
2227 int num_matched_functions;
2230 /* Init maps of given executable or kernel */
2232 map = dso__new_map(target);
2234 map = kernel_get_module_map(target);
2241 * Load matched symbols: Since the different local symbols may have
2242 * same name but different addresses, this lists all the symbols.
2244 num_matched_functions = find_probe_functions(map, pp->function);
2245 if (num_matched_functions == 0) {
2246 pr_err("Failed to find symbol %s in %s\n", pp->function,
2247 target ? : "kernel");
2250 } else if (num_matched_functions > max_tevs) {
2251 pr_err("Too many functions matched in %s\n",
2252 target ? : "kernel");
2257 if (!pev->uprobes && !pp->retprobe) {
2258 kmap = map__kmap(map);
2259 reloc_sym = kmap->ref_reloc_sym;
2261 pr_warning("Relocated base symbol is not found!\n");
2267 /* Setup result trace-probe-events */
2268 *tevs = zalloc(sizeof(*tev) * num_matched_functions);
2276 map__for_each_symbol_by_name(map, pp->function, sym) {
2277 tev = (*tevs) + ret;
2279 if (ret == num_matched_functions) {
2280 pr_warning("Too many symbols are listed. Skip it.\n");
2285 if (pp->offset > sym->end - sym->start) {
2286 pr_warning("Offset %ld is bigger than the size of %s\n",
2287 pp->offset, sym->name);
2291 /* Add one probe point */
2292 tp->address = map->unmap_ip(map, sym->start) + pp->offset;
2294 tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
2295 tp->offset = tp->address - reloc_sym->addr;
2297 tp->symbol = strdup_or_goto(sym->name, nomem_out);
2298 tp->offset = pp->offset;
2300 tp->retprobe = pp->retprobe;
2302 tev->point.module = strdup_or_goto(target, nomem_out);
2303 tev->uprobes = pev->uprobes;
2304 tev->nargs = pev->nargs;
2306 tev->args = zalloc(sizeof(struct probe_trace_arg) *
2308 if (tev->args == NULL)
2311 for (i = 0; i < tev->nargs; i++) {
2312 if (pev->args[i].name)
2314 strdup_or_goto(pev->args[i].name,
2317 tev->args[i].value = strdup_or_goto(pev->args[i].var,
2319 if (pev->args[i].type)
2321 strdup_or_goto(pev->args[i].type,
2327 if (map && pev->uprobes) {
2328 /* Only when using uprobe(exec) map needs to be released */
2329 dso__delete(map->dso);
2337 clear_probe_trace_events(*tevs, num_matched_functions);
2342 static int convert_to_probe_trace_events(struct perf_probe_event *pev,
2343 struct probe_trace_event **tevs,
2344 int max_tevs, const char *target)
2348 if (pev->uprobes && !pev->group) {
2349 /* Replace group name if not given */
2350 ret = convert_exec_to_group(target, &pev->group);
2352 pr_warning("Failed to make a group name.\n");
2357 /* Convert perf_probe_event with debuginfo */
2358 ret = try_to_find_probe_trace_events(pev, tevs, max_tevs, target);
2360 return ret; /* Found in debuginfo or got an error */
2362 return find_probe_trace_events_from_map(pev, tevs, max_tevs, target);
2365 struct __event_package {
2366 struct perf_probe_event *pev;
2367 struct probe_trace_event *tevs;
2371 int add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
2372 int max_tevs, const char *target, bool force_add)
2375 struct __event_package *pkgs;
2378 pkgs = zalloc(sizeof(struct __event_package) * npevs);
2383 ret = init_symbol_maps(pevs->uprobes);
2389 /* Loop 1: convert all events */
2390 for (i = 0; i < npevs; i++) {
2391 pkgs[i].pev = &pevs[i];
2392 /* Convert with or without debuginfo */
2393 ret = convert_to_probe_trace_events(pkgs[i].pev,
2399 pkgs[i].ntevs = ret;
2402 /* Loop 2: add all events */
2403 for (i = 0; i < npevs; i++) {
2404 ret = __add_probe_trace_events(pkgs[i].pev, pkgs[i].tevs,
2405 pkgs[i].ntevs, force_add);
2410 /* Loop 3: cleanup and free trace events */
2411 for (i = 0; i < npevs; i++) {
2412 for (j = 0; j < pkgs[i].ntevs; j++)
2413 clear_probe_trace_event(&pkgs[i].tevs[j]);
2414 zfree(&pkgs[i].tevs);
2422 static int __del_trace_probe_event(int fd, struct str_node *ent)
2428 /* Convert from perf-probe event to trace-probe event */
2429 ret = e_snprintf(buf, 128, "-:%s", ent->s);
2433 p = strchr(buf + 2, ':');
2435 pr_debug("Internal error: %s should have ':' but not.\n",
2442 pr_debug("Writing event: %s\n", buf);
2443 ret = write(fd, buf, strlen(buf));
2449 pr_info("Removed event: %s\n", ent->s);
2452 pr_warning("Failed to delete event: %s\n",
2453 strerror_r(-ret, buf, sizeof(buf)));
2457 static int del_trace_probe_event(int fd, const char *buf,
2458 struct strlist *namelist)
2460 struct str_node *ent, *n;
2463 if (strpbrk(buf, "*?")) { /* Glob-exp */
2464 strlist__for_each_safe(ent, n, namelist)
2465 if (strglobmatch(ent->s, buf)) {
2466 ret = __del_trace_probe_event(fd, ent);
2469 strlist__remove(namelist, ent);
2472 ent = strlist__find(namelist, buf);
2474 ret = __del_trace_probe_event(fd, ent);
2476 strlist__remove(namelist, ent);
2483 int del_perf_probe_events(struct strlist *dellist)
2485 int ret = -1, ufd = -1, kfd = -1;
2487 const char *group, *event;
2489 struct str_node *ent;
2490 struct strlist *namelist = NULL, *unamelist = NULL;
2492 /* Get current event names */
2493 kfd = open_kprobe_events(true);
2495 namelist = get_probe_trace_event_names(kfd, true);
2497 ufd = open_uprobe_events(true);
2499 unamelist = get_probe_trace_event_names(ufd, true);
2501 if (kfd < 0 && ufd < 0) {
2502 print_both_open_warning(kfd, ufd);
2506 if (namelist == NULL && unamelist == NULL)
2509 strlist__for_each(ent, dellist) {
2510 str = strdup(ent->s);
2515 pr_debug("Parsing: %s\n", str);
2516 p = strchr(str, ':');
2526 ret = e_snprintf(buf, 128, "%s:%s", group, event);
2528 pr_err("Failed to copy event.");
2533 pr_debug("Group: %s, Event: %s\n", group, event);
2536 ret = del_trace_probe_event(kfd, buf, namelist);
2538 if (unamelist && ret != 0)
2539 ret = del_trace_probe_event(ufd, buf, unamelist);
2542 pr_info("Info: Event \"%s\" does not exist.\n", buf);
2549 strlist__delete(namelist);
2554 strlist__delete(unamelist);
2561 /* TODO: don't use a global variable for filter ... */
2562 static struct strfilter *available_func_filter;
2565 * If a symbol corresponds to a function with global binding and
2566 * matches filter return 0. For all others return 1.
2568 static int filter_available_functions(struct map *map __maybe_unused,
2571 if ((sym->binding == STB_GLOBAL || sym->binding == STB_LOCAL) &&
2572 strfilter__compare(available_func_filter, sym->name))
2577 int show_available_funcs(const char *target, struct strfilter *_filter,
2583 ret = init_symbol_maps(user);
2587 /* Get a symbol map */
2589 map = dso__new_map(target);
2591 map = kernel_get_module_map(target);
2593 pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
2597 /* Load symbols with given filter */
2598 available_func_filter = _filter;
2599 if (map__load(map, filter_available_functions)) {
2600 pr_err("Failed to load symbols in %s\n", (target) ? : "kernel");
2603 if (!dso__sorted_by_name(map->dso, map->type))
2604 dso__sort_by_name(map->dso, map->type);
2606 /* Show all (filtered) symbols */
2608 dso__fprintf_symbols_by_name(map->dso, map->type, stdout);
2611 dso__delete(map->dso);