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) {
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.\n",
499 synthesize_perf_probe_point(&pev->point));
502 /* Error path : ntevs < 0 */
503 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
504 if (ntevs == -EBADF) {
505 pr_warning("Warning: No dwarf info found in the vmlinux - "
506 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
508 pr_debug("Trying to use symbols.\n");
516 * Find a src file from a DWARF tag path. Prepend optional source path prefix
517 * and chop off leading directories that do not exist. Result is passed back as
518 * a newly allocated path on success.
519 * Return 0 if file was found and readable, -errno otherwise.
521 static int get_real_path(const char *raw_path, const char *comp_dir,
524 const char *prefix = symbol_conf.source_prefix;
527 if (raw_path[0] != '/' && comp_dir)
528 /* If not an absolute path, try to use comp_dir */
531 if (access(raw_path, R_OK) == 0) {
532 *new_path = strdup(raw_path);
539 *new_path = malloc((strlen(prefix) + strlen(raw_path) + 2));
544 sprintf(*new_path, "%s/%s", prefix, raw_path);
546 if (access(*new_path, R_OK) == 0)
549 if (!symbol_conf.source_prefix)
550 /* In case of searching comp_dir, don't retry */
558 raw_path = strchr(++raw_path, '/');
572 #define LINEBUF_SIZE 256
573 #define NR_ADDITIONAL_LINES 2
575 static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
577 char buf[LINEBUF_SIZE], sbuf[STRERR_BUFSIZE];
578 const char *color = show_num ? "" : PERF_COLOR_BLUE;
579 const char *prefix = NULL;
582 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
587 prefix = show_num ? "%7d " : " ";
588 color_fprintf(stdout, color, prefix, l);
590 color_fprintf(stdout, color, "%s", buf);
592 } while (strchr(buf, '\n') == NULL);
597 pr_warning("File read error: %s\n",
598 strerror_r(errno, sbuf, sizeof(sbuf)));
604 static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
606 int rv = __show_one_line(fp, l, skip, show_num);
608 pr_warning("Source file is shorter than expected.\n");
614 #define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
615 #define show_one_line(f,l) _show_one_line(f,l,false,false)
616 #define skip_one_line(f,l) _show_one_line(f,l,true,false)
617 #define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
620 * Show line-range always requires debuginfo to find source file and
623 static int __show_line_range(struct line_range *lr, const char *module)
627 struct debuginfo *dinfo;
631 char sbuf[STRERR_BUFSIZE];
633 /* Search a line range */
634 dinfo = open_debuginfo(module, false);
638 ret = debuginfo__find_line_range(dinfo, lr);
639 debuginfo__delete(dinfo);
640 if (ret == 0 || ret == -ENOENT) {
641 pr_warning("Specified source line is not found.\n");
643 } else if (ret < 0) {
644 pr_warning("Debuginfo analysis failed.\n");
648 /* Convert source file path */
650 ret = get_real_path(tmp, lr->comp_dir, &lr->path);
651 free(tmp); /* Free old path */
653 pr_warning("Failed to find source file path.\n");
660 fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
661 lr->start - lr->offset);
663 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
665 fp = fopen(lr->path, "r");
667 pr_warning("Failed to open %s: %s\n", lr->path,
668 strerror_r(errno, sbuf, sizeof(sbuf)));
671 /* Skip to starting line number */
672 while (l < lr->start) {
673 ret = skip_one_line(fp, l++);
678 intlist__for_each(ln, lr->line_list) {
679 for (; ln->i > l; l++) {
680 ret = show_one_line(fp, l - lr->offset);
684 ret = show_one_line_with_num(fp, l++ - lr->offset);
689 if (lr->end == INT_MAX)
690 lr->end = l + NR_ADDITIONAL_LINES;
691 while (l <= lr->end) {
692 ret = show_one_line_or_eof(fp, l++ - lr->offset);
701 int show_line_range(struct line_range *lr, const char *module, bool user)
705 ret = init_symbol_maps(user);
708 ret = __show_line_range(lr, module);
714 static int show_available_vars_at(struct debuginfo *dinfo,
715 struct perf_probe_event *pev,
716 int max_vls, struct strfilter *_filter,
721 struct str_node *node;
722 struct variable_list *vls = NULL, *vl;
725 buf = synthesize_perf_probe_point(&pev->point);
728 pr_debug("Searching variables at %s\n", buf);
730 ret = debuginfo__find_available_vars_at(dinfo, pev, &vls,
733 if (ret == 0 || ret == -ENOENT) {
734 pr_err("Failed to find the address of %s\n", buf);
737 pr_warning("Debuginfo analysis failed.\n");
741 /* Some variables are found */
742 fprintf(stdout, "Available variables at %s\n", buf);
743 for (i = 0; i < ret; i++) {
746 * A probe point might be converted to
747 * several trace points.
749 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
751 zfree(&vl->point.symbol);
754 strlist__for_each(node, vl->vars) {
755 var = strchr(node->s, '\t') + 1;
756 if (strfilter__compare(_filter, var)) {
757 fprintf(stdout, "\t\t%s\n", node->s);
761 strlist__delete(vl->vars);
764 fprintf(stdout, "\t\t(No matched variables)\n");
772 /* Show available variables on given probe point */
773 int show_available_vars(struct perf_probe_event *pevs, int npevs,
774 int max_vls, const char *module,
775 struct strfilter *_filter, bool externs)
778 struct debuginfo *dinfo;
780 ret = init_symbol_maps(pevs->uprobes);
784 dinfo = open_debuginfo(module, false);
792 for (i = 0; i < npevs && ret >= 0; i++)
793 ret = show_available_vars_at(dinfo, &pevs[i], max_vls, _filter,
796 debuginfo__delete(dinfo);
802 #else /* !HAVE_DWARF_SUPPORT */
805 find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused,
806 struct perf_probe_point *pp __maybe_unused,
807 bool is_kprobe __maybe_unused)
812 static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
813 struct probe_trace_event **tevs __maybe_unused,
814 int max_tevs __maybe_unused,
815 const char *target __maybe_unused)
817 if (perf_probe_event_need_dwarf(pev)) {
818 pr_warning("Debuginfo-analysis is not supported.\n");
825 int show_line_range(struct line_range *lr __maybe_unused,
826 const char *module __maybe_unused,
827 bool user __maybe_unused)
829 pr_warning("Debuginfo-analysis is not supported.\n");
833 int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
834 int npevs __maybe_unused, int max_vls __maybe_unused,
835 const char *module __maybe_unused,
836 struct strfilter *filter __maybe_unused,
837 bool externs __maybe_unused)
839 pr_warning("Debuginfo-analysis is not supported.\n");
844 void line_range__clear(struct line_range *lr)
850 intlist__delete(lr->line_list);
851 memset(lr, 0, sizeof(*lr));
854 int line_range__init(struct line_range *lr)
856 memset(lr, 0, sizeof(*lr));
857 lr->line_list = intlist__new(NULL);
864 static int parse_line_num(char **ptr, int *val, const char *what)
866 const char *start = *ptr;
869 *val = strtol(*ptr, ptr, 0);
870 if (errno || *ptr == start) {
871 semantic_error("'%s' is not a valid number.\n", what);
878 * Stuff 'lr' according to the line range described by 'arg'.
879 * The line range syntax is described by:
881 * SRC[:SLN[+NUM|-ELN]]
882 * FNC[@SRC][:SLN[+NUM|-ELN]]
884 int parse_line_range_desc(const char *arg, struct line_range *lr)
886 char *range, *file, *name = strdup(arg);
895 range = strchr(name, ':');
899 err = parse_line_num(&range, &lr->start, "start line");
903 if (*range == '+' || *range == '-') {
904 const char c = *range++;
906 err = parse_line_num(&range, &lr->end, "end line");
911 lr->end += lr->start;
913 * Adjust the number of lines here.
914 * If the number of lines == 1, the
915 * the end of line should be equal to
922 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
925 if (lr->start > lr->end) {
926 semantic_error("Start line must be smaller"
927 " than end line.\n");
930 if (*range != '\0') {
931 semantic_error("Tailing with invalid str '%s'.\n", range);
936 file = strchr(name, '@');
939 lr->file = strdup(++file);
940 if (lr->file == NULL) {
945 } else if (strchr(name, '.'))
956 /* Check the name is good for event/group */
957 static bool check_event_name(const char *name)
959 if (!isalpha(*name) && *name != '_')
961 while (*++name != '\0') {
962 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
968 /* Parse probepoint definition. */
969 static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
971 struct perf_probe_point *pp = &pev->point;
976 * perf probe [EVENT=]SRC[:LN|;PTN]
977 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
979 * TODO:Group name support
982 ptr = strpbrk(arg, ";=@+%");
983 if (ptr && *ptr == '=') { /* Event name */
986 if (strchr(arg, ':')) {
987 semantic_error("Group name is not supported yet.\n");
990 if (!check_event_name(arg)) {
991 semantic_error("%s is bad for event name -it must "
992 "follow C symbol-naming rule.\n", arg);
995 pev->event = strdup(arg);
996 if (pev->event == NULL)
1002 ptr = strpbrk(arg, ";:+@%");
1012 /* Check arg is function or file and copy it */
1013 if (strchr(tmp, '.')) /* File */
1018 /* Parse other options */
1022 if (c == ';') { /* Lazy pattern must be the last part */
1023 pp->lazy_line = strdup(arg);
1024 if (pp->lazy_line == NULL)
1028 ptr = strpbrk(arg, ";:+@%");
1034 case ':': /* Line number */
1035 pp->line = strtoul(arg, &tmp, 0);
1037 semantic_error("There is non-digit char"
1038 " in line number.\n");
1042 case '+': /* Byte offset from a symbol */
1043 pp->offset = strtoul(arg, &tmp, 0);
1045 semantic_error("There is non-digit character"
1050 case '@': /* File name */
1052 semantic_error("SRC@SRC is not allowed.\n");
1055 pp->file = strdup(arg);
1056 if (pp->file == NULL)
1059 case '%': /* Probe places */
1060 if (strcmp(arg, "return") == 0) {
1062 } else { /* Others not supported yet */
1063 semantic_error("%%%s is not supported.\n", arg);
1067 default: /* Buggy case */
1068 pr_err("This program has a bug at %s:%d.\n",
1069 __FILE__, __LINE__);
1075 /* Exclusion check */
1076 if (pp->lazy_line && pp->line) {
1077 semantic_error("Lazy pattern can't be used with"
1082 if (pp->lazy_line && pp->offset) {
1083 semantic_error("Lazy pattern can't be used with offset.\n");
1087 if (pp->line && pp->offset) {
1088 semantic_error("Offset can't be used with line number.\n");
1092 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
1093 semantic_error("File always requires line number or "
1098 if (pp->offset && !pp->function) {
1099 semantic_error("Offset requires an entry function.\n");
1103 if (pp->retprobe && !pp->function) {
1104 semantic_error("Return probe requires an entry function.\n");
1108 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
1109 semantic_error("Offset/Line/Lazy pattern can't be used with "
1114 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
1115 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
1120 /* Parse perf-probe event argument */
1121 static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
1123 char *tmp, *goodname;
1124 struct perf_probe_arg_field **fieldp;
1126 pr_debug("parsing arg: %s into ", str);
1128 tmp = strchr(str, '=');
1130 arg->name = strndup(str, tmp - str);
1131 if (arg->name == NULL)
1133 pr_debug("name:%s ", arg->name);
1137 tmp = strchr(str, ':');
1138 if (tmp) { /* Type setting */
1140 arg->type = strdup(tmp + 1);
1141 if (arg->type == NULL)
1143 pr_debug("type:%s ", arg->type);
1146 tmp = strpbrk(str, "-.[");
1147 if (!is_c_varname(str) || !tmp) {
1148 /* A variable, register, symbol or special value */
1149 arg->var = strdup(str);
1150 if (arg->var == NULL)
1152 pr_debug("%s\n", arg->var);
1156 /* Structure fields or array element */
1157 arg->var = strndup(str, tmp - str);
1158 if (arg->var == NULL)
1160 goodname = arg->var;
1161 pr_debug("%s, ", arg->var);
1162 fieldp = &arg->field;
1165 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
1166 if (*fieldp == NULL)
1168 if (*tmp == '[') { /* Array */
1170 (*fieldp)->index = strtol(str + 1, &tmp, 0);
1171 (*fieldp)->ref = true;
1172 if (*tmp != ']' || tmp == str + 1) {
1173 semantic_error("Array index must be a"
1180 } else { /* Structure */
1183 (*fieldp)->ref = false;
1184 } else if (tmp[1] == '>') {
1186 (*fieldp)->ref = true;
1188 semantic_error("Argument parse error: %s\n",
1192 tmp = strpbrk(str, "-.[");
1195 (*fieldp)->name = strndup(str, tmp - str);
1196 if ((*fieldp)->name == NULL)
1199 goodname = (*fieldp)->name;
1200 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
1201 fieldp = &(*fieldp)->next;
1204 (*fieldp)->name = strdup(str);
1205 if ((*fieldp)->name == NULL)
1208 goodname = (*fieldp)->name;
1209 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
1211 /* If no name is specified, set the last field name (not array index)*/
1213 arg->name = strdup(goodname);
1214 if (arg->name == NULL)
1220 /* Parse perf-probe event command */
1221 int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
1224 int argc, i, ret = 0;
1226 argv = argv_split(cmd, &argc);
1228 pr_debug("Failed to split arguments.\n");
1231 if (argc - 1 > MAX_PROBE_ARGS) {
1232 semantic_error("Too many probe arguments (%d).\n", argc - 1);
1236 /* Parse probe point */
1237 ret = parse_perf_probe_point(argv[0], pev);
1241 /* Copy arguments and ensure return probe has no C argument */
1242 pev->nargs = argc - 1;
1243 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1244 if (pev->args == NULL) {
1248 for (i = 0; i < pev->nargs && ret >= 0; i++) {
1249 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1251 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
1252 semantic_error("You can't specify local variable for"
1263 /* Return true if this perf_probe_event requires debuginfo */
1264 bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
1268 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1271 for (i = 0; i < pev->nargs; i++)
1272 if (is_c_varname(pev->args[i].var))
1278 /* Parse probe_events event into struct probe_point */
1279 static int parse_probe_trace_command(const char *cmd,
1280 struct probe_trace_event *tev)
1282 struct probe_trace_point *tp = &tev->point;
1285 char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
1289 pr_debug("Parsing probe_events: %s\n", cmd);
1290 argv = argv_split(cmd, &argc);
1292 pr_debug("Failed to split arguments.\n");
1296 semantic_error("Too few probe arguments.\n");
1301 /* Scan event and group name. */
1302 argv0_str = strdup(argv[0]);
1303 if (argv0_str == NULL) {
1307 fmt1_str = strtok_r(argv0_str, ":", &fmt);
1308 fmt2_str = strtok_r(NULL, "/", &fmt);
1309 fmt3_str = strtok_r(NULL, " \t", &fmt);
1310 if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
1311 || fmt3_str == NULL) {
1312 semantic_error("Failed to parse event name: %s\n", argv[0]);
1317 tev->group = strdup(fmt2_str);
1318 tev->event = strdup(fmt3_str);
1319 if (tev->group == NULL || tev->event == NULL) {
1323 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
1325 tp->retprobe = (pr == 'r');
1327 /* Scan module name(if there), function name and offset */
1328 p = strchr(argv[1], ':');
1330 tp->module = strndup(argv[1], p - argv[1]);
1334 fmt1_str = strtok_r(p, "+", &fmt);
1335 if (fmt1_str[0] == '0') /* only the address started with 0x */
1336 tp->address = strtoul(fmt1_str, NULL, 0);
1338 /* Only the symbol-based probe has offset */
1339 tp->symbol = strdup(fmt1_str);
1340 if (tp->symbol == NULL) {
1344 fmt2_str = strtok_r(NULL, "", &fmt);
1345 if (fmt2_str == NULL)
1348 tp->offset = strtoul(fmt2_str, NULL, 10);
1351 tev->nargs = argc - 2;
1352 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
1353 if (tev->args == NULL) {
1357 for (i = 0; i < tev->nargs; i++) {
1358 p = strchr(argv[i + 2], '=');
1359 if (p) /* We don't need which register is assigned. */
1363 tev->args[i].name = strdup(argv[i + 2]);
1364 /* TODO: parse regs and offset */
1365 tev->args[i].value = strdup(p);
1366 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1378 /* Compose only probe arg */
1379 int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
1381 struct perf_probe_arg_field *field = pa->field;
1385 if (pa->name && pa->var)
1386 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
1388 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
1395 if (field->name[0] == '[')
1396 ret = e_snprintf(tmp, len, "%s", field->name);
1398 ret = e_snprintf(tmp, len, "%s%s",
1399 field->ref ? "->" : ".", field->name);
1404 field = field->next;
1408 ret = e_snprintf(tmp, len, ":%s", pa->type);
1417 pr_debug("Failed to synthesize perf probe argument: %d\n", ret);
1421 /* Compose only probe point (not argument) */
1422 static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
1425 char offs[32] = "", line[32] = "", file[32] = "";
1428 buf = zalloc(MAX_CMDLEN);
1434 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
1439 ret = e_snprintf(line, 32, ":%d", pp->line);
1447 tmp = strchr(pp->file + len - 30, '/');
1448 tmp = tmp ? tmp + 1 : pp->file + len - 30;
1450 ret = e_snprintf(file, 32, "@%s", tmp);
1456 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
1457 offs, pp->retprobe ? "%return" : "", line,
1460 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
1466 pr_debug("Failed to synthesize perf probe point: %d\n", ret);
1472 char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1477 buf = synthesize_perf_probe_point(&pev->point);
1482 for (i = 0; i < pev->nargs; i++) {
1483 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
1496 static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
1497 char **buf, size_t *buflen,
1502 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
1508 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
1520 static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
1521 char *buf, size_t buflen)
1523 struct probe_trace_arg_ref *ref = arg->ref;
1527 /* Argument name or separator */
1529 ret = e_snprintf(buf, buflen, " %s=", arg->name);
1531 ret = e_snprintf(buf, buflen, " ");
1537 /* Special case: @XXX */
1538 if (arg->value[0] == '@' && arg->ref)
1541 /* Dereferencing arguments */
1543 depth = __synthesize_probe_trace_arg_ref(ref, &buf,
1549 /* Print argument value */
1550 if (arg->value[0] == '@' && arg->ref)
1551 ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
1554 ret = e_snprintf(buf, buflen, "%s", arg->value);
1562 ret = e_snprintf(buf, buflen, ")");
1568 /* Print argument type */
1570 ret = e_snprintf(buf, buflen, ":%s", arg->type);
1579 char *synthesize_probe_trace_command(struct probe_trace_event *tev)
1581 struct probe_trace_point *tp = &tev->point;
1585 buf = zalloc(MAX_CMDLEN);
1589 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
1590 tev->group, tev->event);
1594 /* Uprobes must have tp->address and tp->module */
1595 if (tev->uprobes && (!tp->address || !tp->module))
1598 /* Use the tp->address for uprobes */
1600 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s:0x%lx",
1601 tp->module, tp->address);
1603 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s%s%s+%lu",
1604 tp->module ?: "", tp->module ? ":" : "",
1605 tp->symbol, tp->offset);
1611 for (i = 0; i < tev->nargs; i++) {
1612 ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
1625 static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
1626 struct perf_probe_point *pp,
1629 struct symbol *sym = NULL;
1635 map = dso__new_map(tp->module);
1639 sym = map__find_symbol(map, addr, NULL);
1641 addr = kernel_get_symbol_address_by_name(tp->symbol, true);
1644 sym = __find_kernel_function(addr, &map);
1650 pp->retprobe = tp->retprobe;
1651 pp->offset = addr - map->unmap_ip(map, sym->start);
1652 pp->function = strdup(sym->name);
1653 ret = pp->function ? 0 : -ENOMEM;
1656 if (map && !is_kprobe) {
1657 dso__delete(map->dso);
1664 static int convert_to_perf_probe_point(struct probe_trace_point *tp,
1665 struct perf_probe_point *pp,
1671 ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe);
1674 ret = find_perf_probe_point_from_map(tp, pp, is_kprobe);
1678 pr_debug("Failed to find probe point from both of dwarf and map.\n");
1681 pp->function = strdup(tp->symbol);
1682 pp->offset = tp->offset;
1683 } else if (!tp->module && !is_kprobe) {
1684 ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address);
1687 pp->function = strdup(buf);
1690 if (pp->function == NULL)
1693 pp->retprobe = tp->retprobe;
1698 static int convert_to_perf_probe_event(struct probe_trace_event *tev,
1699 struct perf_probe_event *pev, bool is_kprobe)
1704 /* Convert event/group name */
1705 pev->event = strdup(tev->event);
1706 pev->group = strdup(tev->group);
1707 if (pev->event == NULL || pev->group == NULL)
1710 /* Convert trace_point to probe_point */
1711 ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
1715 /* Convert trace_arg to probe_arg */
1716 pev->nargs = tev->nargs;
1717 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1718 if (pev->args == NULL)
1720 for (i = 0; i < tev->nargs && ret >= 0; i++) {
1721 if (tev->args[i].name)
1722 pev->args[i].name = strdup(tev->args[i].name);
1724 ret = synthesize_probe_trace_arg(&tev->args[i],
1726 pev->args[i].name = strdup(buf);
1728 if (pev->args[i].name == NULL && ret >= 0)
1733 clear_perf_probe_event(pev);
1738 void clear_perf_probe_event(struct perf_probe_event *pev)
1740 struct perf_probe_point *pp = &pev->point;
1741 struct perf_probe_arg_field *field, *next;
1748 free(pp->lazy_line);
1750 for (i = 0; i < pev->nargs; i++) {
1751 free(pev->args[i].name);
1752 free(pev->args[i].var);
1753 free(pev->args[i].type);
1754 field = pev->args[i].field;
1757 zfree(&field->name);
1763 memset(pev, 0, sizeof(*pev));
1766 static void clear_probe_trace_event(struct probe_trace_event *tev)
1768 struct probe_trace_arg_ref *ref, *next;
1773 free(tev->point.symbol);
1774 free(tev->point.module);
1775 for (i = 0; i < tev->nargs; i++) {
1776 free(tev->args[i].name);
1777 free(tev->args[i].value);
1778 free(tev->args[i].type);
1779 ref = tev->args[i].ref;
1787 memset(tev, 0, sizeof(*tev));
1790 static void print_open_warning(int err, bool is_kprobe)
1792 char sbuf[STRERR_BUFSIZE];
1794 if (err == -ENOENT) {
1798 config = "CONFIG_UPROBE_EVENTS";
1800 config = "CONFIG_KPROBE_EVENTS";
1802 pr_warning("%cprobe_events file does not exist"
1803 " - please rebuild kernel with %s.\n",
1804 is_kprobe ? 'k' : 'u', config);
1805 } else if (err == -ENOTSUP)
1806 pr_warning("Debugfs is not mounted.\n");
1808 pr_warning("Failed to open %cprobe_events: %s\n",
1809 is_kprobe ? 'k' : 'u',
1810 strerror_r(-err, sbuf, sizeof(sbuf)));
1813 static void print_both_open_warning(int kerr, int uerr)
1815 /* Both kprobes and uprobes are disabled, warn it. */
1816 if (kerr == -ENOTSUP && uerr == -ENOTSUP)
1817 pr_warning("Debugfs is not mounted.\n");
1818 else if (kerr == -ENOENT && uerr == -ENOENT)
1819 pr_warning("Please rebuild kernel with CONFIG_KPROBE_EVENTS "
1820 "or/and CONFIG_UPROBE_EVENTS.\n");
1822 char sbuf[STRERR_BUFSIZE];
1823 pr_warning("Failed to open kprobe events: %s.\n",
1824 strerror_r(-kerr, sbuf, sizeof(sbuf)));
1825 pr_warning("Failed to open uprobe events: %s.\n",
1826 strerror_r(-uerr, sbuf, sizeof(sbuf)));
1830 static int open_probe_events(const char *trace_file, bool readwrite)
1833 const char *__debugfs;
1836 __debugfs = debugfs_find_mountpoint();
1837 if (__debugfs == NULL)
1840 ret = e_snprintf(buf, PATH_MAX, "%s/%s", __debugfs, trace_file);
1842 pr_debug("Opening %s write=%d\n", buf, readwrite);
1843 if (readwrite && !probe_event_dry_run)
1844 ret = open(buf, O_RDWR, O_APPEND);
1846 ret = open(buf, O_RDONLY, 0);
1854 static int open_kprobe_events(bool readwrite)
1856 return open_probe_events("tracing/kprobe_events", readwrite);
1859 static int open_uprobe_events(bool readwrite)
1861 return open_probe_events("tracing/uprobe_events", readwrite);
1864 /* Get raw string list of current kprobe_events or uprobe_events */
1865 static struct strlist *get_probe_trace_command_rawlist(int fd)
1869 char buf[MAX_CMDLEN];
1873 sl = strlist__new(true, NULL);
1875 fp = fdopen(dup(fd), "r");
1877 p = fgets(buf, MAX_CMDLEN, fp);
1881 idx = strlen(p) - 1;
1884 ret = strlist__add(sl, buf);
1886 pr_debug("strlist__add failed (%d)\n", ret);
1887 strlist__delete(sl);
1897 static int show_perf_probe_event(struct perf_probe_event *pev,
1904 /* Synthesize only event probe point */
1905 place = synthesize_perf_probe_point(&pev->point);
1909 ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
1913 pr_info(" %-20s (on %s", buf, place);
1915 pr_info(" in %s", module);
1917 if (pev->nargs > 0) {
1919 for (i = 0; i < pev->nargs; i++) {
1920 ret = synthesize_perf_probe_arg(&pev->args[i],
1924 pr_info(" %s", buf);
1932 static int __show_perf_probe_events(int fd, bool is_kprobe)
1935 struct probe_trace_event tev;
1936 struct perf_probe_event pev;
1937 struct strlist *rawlist;
1938 struct str_node *ent;
1940 memset(&tev, 0, sizeof(tev));
1941 memset(&pev, 0, sizeof(pev));
1943 rawlist = get_probe_trace_command_rawlist(fd);
1947 strlist__for_each(ent, rawlist) {
1948 ret = parse_probe_trace_command(ent->s, &tev);
1950 ret = convert_to_perf_probe_event(&tev, &pev,
1953 ret = show_perf_probe_event(&pev,
1956 clear_perf_probe_event(&pev);
1957 clear_probe_trace_event(&tev);
1961 strlist__delete(rawlist);
1966 /* List up current perf-probe events */
1967 int show_perf_probe_events(void)
1969 int kp_fd, up_fd, ret;
1973 ret = init_symbol_maps(false);
1977 kp_fd = open_kprobe_events(false);
1979 ret = __show_perf_probe_events(kp_fd, true);
1985 up_fd = open_uprobe_events(false);
1986 if (kp_fd < 0 && up_fd < 0) {
1987 print_both_open_warning(kp_fd, up_fd);
1993 ret = __show_perf_probe_events(up_fd, false);
2001 /* Get current perf-probe event names */
2002 static struct strlist *get_probe_trace_event_names(int fd, bool include_group)
2005 struct strlist *sl, *rawlist;
2006 struct str_node *ent;
2007 struct probe_trace_event tev;
2010 memset(&tev, 0, sizeof(tev));
2011 rawlist = get_probe_trace_command_rawlist(fd);
2014 sl = strlist__new(true, NULL);
2015 strlist__for_each(ent, rawlist) {
2016 ret = parse_probe_trace_command(ent->s, &tev);
2019 if (include_group) {
2020 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
2023 ret = strlist__add(sl, buf);
2025 ret = strlist__add(sl, tev.event);
2026 clear_probe_trace_event(&tev);
2030 strlist__delete(rawlist);
2033 strlist__delete(sl);
2039 static int write_probe_trace_event(int fd, struct probe_trace_event *tev)
2042 char *buf = synthesize_probe_trace_command(tev);
2043 char sbuf[STRERR_BUFSIZE];
2046 pr_debug("Failed to synthesize probe trace event.\n");
2050 pr_debug("Writing event: %s\n", buf);
2051 if (!probe_event_dry_run) {
2052 ret = write(fd, buf, strlen(buf));
2054 pr_warning("Failed to write event: %s\n",
2055 strerror_r(errno, sbuf, sizeof(sbuf)));
2061 static int get_new_event_name(char *buf, size_t len, const char *base,
2062 struct strlist *namelist, bool allow_suffix)
2067 ret = e_snprintf(buf, len, "%s", base);
2069 pr_debug("snprintf() failed: %d\n", ret);
2072 if (!strlist__has_entry(namelist, buf))
2075 if (!allow_suffix) {
2076 pr_warning("Error: event \"%s\" already exists. "
2077 "(Use -f to force duplicates.)\n", base);
2081 /* Try to add suffix */
2082 for (i = 1; i < MAX_EVENT_INDEX; i++) {
2083 ret = e_snprintf(buf, len, "%s_%d", base, i);
2085 pr_debug("snprintf() failed: %d\n", ret);
2088 if (!strlist__has_entry(namelist, buf))
2091 if (i == MAX_EVENT_INDEX) {
2092 pr_warning("Too many events are on the same function.\n");
2099 static int __add_probe_trace_events(struct perf_probe_event *pev,
2100 struct probe_trace_event *tevs,
2101 int ntevs, bool allow_suffix)
2104 struct probe_trace_event *tev = NULL;
2106 const char *event, *group;
2107 struct strlist *namelist;
2110 fd = open_uprobe_events(true);
2112 fd = open_kprobe_events(true);
2115 print_open_warning(fd, !pev->uprobes);
2119 /* Get current event names */
2120 namelist = get_probe_trace_event_names(fd, false);
2122 pr_debug("Failed to get current event list.\n");
2127 pr_info("Added new event%s\n", (ntevs > 1) ? "s:" : ":");
2128 for (i = 0; i < ntevs; i++) {
2133 if (pev->point.function)
2134 event = pev->point.function;
2136 event = tev->point.symbol;
2140 group = PERFPROBE_GROUP;
2142 /* Get an unused new event name */
2143 ret = get_new_event_name(buf, 64, event,
2144 namelist, allow_suffix);
2149 tev->event = strdup(event);
2150 tev->group = strdup(group);
2151 if (tev->event == NULL || tev->group == NULL) {
2155 ret = write_probe_trace_event(fd, tev);
2158 /* Add added event name to namelist */
2159 strlist__add(namelist, event);
2161 /* Trick here - save current event/group */
2164 pev->event = tev->event;
2165 pev->group = tev->group;
2166 show_perf_probe_event(pev, tev->point.module);
2167 /* Trick here - restore current event/group */
2168 pev->event = (char *)event;
2169 pev->group = (char *)group;
2172 * Probes after the first probe which comes from same
2173 * user input are always allowed to add suffix, because
2174 * there might be several addresses corresponding to
2177 allow_suffix = true;
2181 /* Show how to use the event. */
2182 pr_info("\nYou can now use it in all perf tools, such as:\n\n");
2183 pr_info("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group,
2187 strlist__delete(namelist);
2192 static char *looking_function_name;
2193 static int num_matched_functions;
2195 static int probe_function_filter(struct map *map __maybe_unused,
2198 if ((sym->binding == STB_GLOBAL || sym->binding == STB_LOCAL) &&
2199 strcmp(looking_function_name, sym->name) == 0) {
2200 num_matched_functions++;
2206 #define strdup_or_goto(str, label) \
2207 ({ char *__p = strdup(str); if (!__p) goto label; __p; })
2210 * Find probe function addresses from map.
2211 * Return an error or the number of found probe_trace_event
2213 static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
2214 struct probe_trace_event **tevs,
2215 int max_tevs, const char *target)
2217 struct map *map = NULL;
2218 struct kmap *kmap = NULL;
2219 struct ref_reloc_sym *reloc_sym = NULL;
2222 struct probe_trace_event *tev;
2223 struct perf_probe_point *pp = &pev->point;
2224 struct probe_trace_point *tp;
2227 /* Init maps of given executable or kernel */
2229 map = dso__new_map(target);
2231 map = kernel_get_module_map(target);
2238 * Load matched symbols: Since the different local symbols may have
2239 * same name but different addresses, this lists all the symbols.
2241 num_matched_functions = 0;
2242 looking_function_name = pp->function;
2243 ret = map__load(map, probe_function_filter);
2244 if (ret || num_matched_functions == 0) {
2245 pr_err("Failed to find symbol %s in %s\n", pp->function,
2246 target ? : "kernel");
2249 } else if (num_matched_functions > max_tevs) {
2250 pr_err("Too many functions matched in %s\n",
2251 target ? : "kernel");
2256 if (!pev->uprobes) {
2257 kmap = map__kmap(map);
2258 reloc_sym = kmap->ref_reloc_sym;
2260 pr_warning("Relocated base symbol is not found!\n");
2266 /* Setup result trace-probe-events */
2267 *tevs = zalloc(sizeof(*tev) * num_matched_functions);
2274 map__for_each_symbol(map, sym, nd) {
2275 tev = (*tevs) + ret;
2277 if (ret == num_matched_functions) {
2278 pr_warning("Too many symbols are listed. Skip it.\n");
2283 if (pp->offset > sym->end - sym->start) {
2284 pr_warning("Offset %ld is bigger than the size of %s\n",
2285 pp->offset, sym->name);
2289 /* Add one probe point */
2290 tp->address = map->unmap_ip(map, sym->start) + pp->offset;
2292 tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
2293 tp->offset = tp->address - reloc_sym->addr;
2295 tp->symbol = strdup_or_goto(sym->name, nomem_out);
2296 tp->offset = pp->offset;
2298 tp->retprobe = pp->retprobe;
2300 tev->point.module = strdup_or_goto(target, nomem_out);
2301 tev->uprobes = pev->uprobes;
2302 tev->nargs = pev->nargs;
2304 tev->args = zalloc(sizeof(struct probe_trace_arg) *
2306 if (tev->args == NULL)
2309 for (i = 0; i < tev->nargs; i++) {
2310 if (pev->args[i].name)
2312 strdup_or_goto(pev->args[i].name,
2315 tev->args[i].value = strdup_or_goto(pev->args[i].var,
2317 if (pev->args[i].type)
2319 strdup_or_goto(pev->args[i].type,
2325 if (map && pev->uprobes) {
2326 /* Only when using uprobe(exec) map needs to be released */
2327 dso__delete(map->dso);
2335 clear_probe_trace_events(*tevs, num_matched_functions);
2340 static int convert_to_probe_trace_events(struct perf_probe_event *pev,
2341 struct probe_trace_event **tevs,
2342 int max_tevs, const char *target)
2346 if (pev->uprobes && !pev->group) {
2347 /* Replace group name if not given */
2348 ret = convert_exec_to_group(target, &pev->group);
2350 pr_warning("Failed to make a group name.\n");
2355 /* Convert perf_probe_event with debuginfo */
2356 ret = try_to_find_probe_trace_events(pev, tevs, max_tevs, target);
2358 return ret; /* Found in debuginfo or got an error */
2360 return find_probe_trace_events_from_map(pev, tevs, max_tevs, target);
2363 struct __event_package {
2364 struct perf_probe_event *pev;
2365 struct probe_trace_event *tevs;
2369 int add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
2370 int max_tevs, const char *target, bool force_add)
2373 struct __event_package *pkgs;
2376 pkgs = zalloc(sizeof(struct __event_package) * npevs);
2381 ret = init_symbol_maps(pevs->uprobes);
2387 /* Loop 1: convert all events */
2388 for (i = 0; i < npevs; i++) {
2389 pkgs[i].pev = &pevs[i];
2390 /* Convert with or without debuginfo */
2391 ret = convert_to_probe_trace_events(pkgs[i].pev,
2397 pkgs[i].ntevs = ret;
2400 /* Loop 2: add all events */
2401 for (i = 0; i < npevs; i++) {
2402 ret = __add_probe_trace_events(pkgs[i].pev, pkgs[i].tevs,
2403 pkgs[i].ntevs, force_add);
2408 /* Loop 3: cleanup and free trace events */
2409 for (i = 0; i < npevs; i++) {
2410 for (j = 0; j < pkgs[i].ntevs; j++)
2411 clear_probe_trace_event(&pkgs[i].tevs[j]);
2412 zfree(&pkgs[i].tevs);
2420 static int __del_trace_probe_event(int fd, struct str_node *ent)
2426 /* Convert from perf-probe event to trace-probe event */
2427 ret = e_snprintf(buf, 128, "-:%s", ent->s);
2431 p = strchr(buf + 2, ':');
2433 pr_debug("Internal error: %s should have ':' but not.\n",
2440 pr_debug("Writing event: %s\n", buf);
2441 ret = write(fd, buf, strlen(buf));
2447 pr_info("Removed event: %s\n", ent->s);
2450 pr_warning("Failed to delete event: %s\n",
2451 strerror_r(-ret, buf, sizeof(buf)));
2455 static int del_trace_probe_event(int fd, const char *buf,
2456 struct strlist *namelist)
2458 struct str_node *ent, *n;
2461 if (strpbrk(buf, "*?")) { /* Glob-exp */
2462 strlist__for_each_safe(ent, n, namelist)
2463 if (strglobmatch(ent->s, buf)) {
2464 ret = __del_trace_probe_event(fd, ent);
2467 strlist__remove(namelist, ent);
2470 ent = strlist__find(namelist, buf);
2472 ret = __del_trace_probe_event(fd, ent);
2474 strlist__remove(namelist, ent);
2481 int del_perf_probe_events(struct strlist *dellist)
2483 int ret = -1, ufd = -1, kfd = -1;
2485 const char *group, *event;
2487 struct str_node *ent;
2488 struct strlist *namelist = NULL, *unamelist = NULL;
2490 /* Get current event names */
2491 kfd = open_kprobe_events(true);
2493 namelist = get_probe_trace_event_names(kfd, true);
2495 ufd = open_uprobe_events(true);
2497 unamelist = get_probe_trace_event_names(ufd, true);
2499 if (kfd < 0 && ufd < 0) {
2500 print_both_open_warning(kfd, ufd);
2504 if (namelist == NULL && unamelist == NULL)
2507 strlist__for_each(ent, dellist) {
2508 str = strdup(ent->s);
2513 pr_debug("Parsing: %s\n", str);
2514 p = strchr(str, ':');
2524 ret = e_snprintf(buf, 128, "%s:%s", group, event);
2526 pr_err("Failed to copy event.");
2531 pr_debug("Group: %s, Event: %s\n", group, event);
2534 ret = del_trace_probe_event(kfd, buf, namelist);
2536 if (unamelist && ret != 0)
2537 ret = del_trace_probe_event(ufd, buf, unamelist);
2540 pr_info("Info: Event \"%s\" does not exist.\n", buf);
2547 strlist__delete(namelist);
2552 strlist__delete(unamelist);
2559 /* TODO: don't use a global variable for filter ... */
2560 static struct strfilter *available_func_filter;
2563 * If a symbol corresponds to a function with global binding and
2564 * matches filter return 0. For all others return 1.
2566 static int filter_available_functions(struct map *map __maybe_unused,
2569 if ((sym->binding == STB_GLOBAL || sym->binding == STB_LOCAL) &&
2570 strfilter__compare(available_func_filter, sym->name))
2575 int show_available_funcs(const char *target, struct strfilter *_filter,
2581 ret = init_symbol_maps(user);
2585 /* Get a symbol map */
2587 map = dso__new_map(target);
2589 map = kernel_get_module_map(target);
2591 pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
2595 /* Load symbols with given filter */
2596 available_func_filter = _filter;
2597 if (map__load(map, filter_available_functions)) {
2598 pr_err("Failed to load symbols in %s\n", (target) ? : "kernel");
2601 if (!dso__sorted_by_name(map->dso, map->type))
2602 dso__sort_by_name(map->dso, map->type);
2604 /* Show all (filtered) symbols */
2606 dso__fprintf_symbols_by_name(map->dso, map->type, stdout);
2609 dso__delete(map->dso);