1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * probe-event.c : perf-probe definition to probe_events format converter
5 * Written by Masami Hiramatsu <mhiramat@redhat.com>
9 #include <sys/utsname.h>
10 #include <sys/types.h>
24 #include "namespaces.h"
26 #include "strfilter.h"
34 #include <api/fs/fs.h>
35 #include "trace-event.h" /* For __maybe_unused */
36 #include "probe-event.h"
37 #include "probe-finder.h"
38 #include "probe-file.h"
43 #include <subcmd/pager.h>
44 #include <linux/ctype.h>
45 #include <linux/zalloc.h>
47 #ifdef HAVE_DEBUGINFOD_SUPPORT
48 #include <elfutils/debuginfod.h>
51 #define PERFPROBE_GROUP "probe"
53 bool probe_event_dry_run; /* Dry run flag */
54 struct probe_conf probe_conf = { .magic_num = DEFAULT_PROBE_MAGIC_NUM };
56 static char *synthesize_perf_probe_point(struct perf_probe_point *pp);
58 #define semantic_error(msg ...) pr_err("Semantic error :" msg)
60 int e_snprintf(char *str, size_t size, const char *format, ...)
65 ret = vsnprintf(str, size, format, ap);
72 static struct machine *host_machine;
74 /* Initialize symbol maps and path of vmlinux/modules */
75 int init_probe_symbol_maps(bool user_only)
79 symbol_conf.allow_aliases = true;
80 ret = symbol__init(NULL);
82 pr_debug("Failed to init symbol map.\n");
86 if (host_machine || user_only) /* already initialized */
89 if (symbol_conf.vmlinux_name)
90 pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
92 host_machine = machine__new_host();
94 pr_debug("machine__new_host() failed.\n");
100 pr_warning("Failed to init vmlinux path.\n");
104 void exit_probe_symbol_maps(void)
106 machine__delete(host_machine);
111 static struct ref_reloc_sym *kernel_get_ref_reloc_sym(struct map **pmap)
114 struct map *map = machine__kernel_map(host_machine);
116 if (map__load(map) < 0)
119 kmap = map__kmap(map);
126 return kmap->ref_reloc_sym;
129 static int kernel_get_symbol_address_by_name(const char *name, u64 *addr,
130 bool reloc, bool reladdr)
132 struct ref_reloc_sym *reloc_sym;
136 /* ref_reloc_sym is just a label. Need a special fix*/
137 reloc_sym = kernel_get_ref_reloc_sym(&map);
138 if (reloc_sym && strcmp(name, reloc_sym->name) == 0)
139 *addr = (!map__reloc(map) || reloc) ? reloc_sym->addr :
140 reloc_sym->unrelocated_addr;
142 sym = machine__find_kernel_symbol_by_name(host_machine, name, &map);
145 *addr = map__unmap_ip(map, sym->start) -
146 ((reloc) ? 0 : map__reloc(map)) -
147 ((reladdr) ? map__start(map) : 0);
152 struct kernel_get_module_map_cb_args {
157 static int kernel_get_module_map_cb(struct map *map, void *data)
159 struct kernel_get_module_map_cb_args *args = data;
160 struct dso *dso = map__dso(map);
161 const char *short_name = dso->short_name; /* short_name is "[module]" */
162 u16 short_name_len = dso->short_name_len;
164 if (strncmp(short_name + 1, args->module, short_name_len - 2) == 0 &&
165 args->module[short_name_len - 2] == '\0') {
166 args->result = map__get(map);
172 static struct map *kernel_get_module_map(const char *module)
174 struct kernel_get_module_map_cb_args args = {
179 /* A file path -- this is an offline module */
180 if (module && strchr(module, '/'))
181 return dso__new_map(module);
184 struct map *map = machine__kernel_map(host_machine);
186 return map__get(map);
189 maps__for_each_map(machine__kernel_maps(host_machine), kernel_get_module_map_cb, &args);
194 struct map *get_target_map(const char *target, struct nsinfo *nsi, bool user)
196 /* Init maps of given executable or kernel */
201 map = dso__new_map(target);
202 dso = map ? map__dso(map) : NULL;
204 mutex_lock(&dso->lock);
205 nsinfo__put(dso->nsinfo);
206 dso->nsinfo = nsinfo__get(nsi);
207 mutex_unlock(&dso->lock);
211 return kernel_get_module_map(target);
215 static int convert_exec_to_group(const char *exec, char **result)
217 char *ptr1, *ptr2, *exec_copy;
221 exec_copy = strdup(exec);
225 ptr1 = basename(exec_copy);
231 for (ptr2 = ptr1; *ptr2 != '\0'; ptr2++) {
232 if (!isalnum(*ptr2) && *ptr2 != '_') {
238 ret = e_snprintf(buf, 64, "%s_%s", PERFPROBE_GROUP, ptr1);
242 *result = strdup(buf);
243 ret = *result ? 0 : -ENOMEM;
250 static void clear_perf_probe_point(struct perf_probe_point *pp)
253 zfree(&pp->function);
254 zfree(&pp->lazy_line);
257 static void clear_probe_trace_events(struct probe_trace_event *tevs, int ntevs)
261 for (i = 0; i < ntevs; i++)
262 clear_probe_trace_event(tevs + i);
265 static bool kprobe_blacklist__listed(u64 address);
266 static bool kprobe_warn_out_range(const char *symbol, u64 address)
271 map = kernel_get_module_map(NULL);
273 ret = address <= map__start(map) || map__end(map) < address;
275 pr_warning("%s is out of .text, skip it.\n", symbol);
278 if (!ret && kprobe_blacklist__listed(address)) {
279 pr_warning("%s is blacklisted function, skip it.\n", symbol);
287 * @module can be module name of module file path. In case of path,
288 * inspect elf and find out what is actual module name.
289 * Caller has to free mod_name after using it.
291 static char *find_module_name(const char *module)
299 char *mod_name = NULL;
302 fd = open(module, O_RDONLY);
306 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
310 if (gelf_getehdr(elf, &ehdr) == NULL)
313 sec = elf_section_by_name(elf, &ehdr, &shdr,
314 ".gnu.linkonce.this_module", NULL);
318 data = elf_getdata(sec, NULL);
319 if (!data || !data->d_buf)
324 * '.gnu.linkonce.this_module' section of kernel module elf directly
325 * maps to 'struct module' from linux/module.h. This section contains
326 * actual module name which will be used by kernel after loading it.
327 * But, we cannot use 'struct module' here since linux/module.h is not
328 * exposed to user-space. Offset of 'name' has remained same from long
329 * time, so hardcoding it here.
331 if (ehdr.e_ident[EI_CLASS] == ELFCLASS32)
333 else /* expect ELFCLASS64 by default */
336 mod_name = strdup((char *)data->d_buf + name_offset);
345 #ifdef HAVE_DWARF_SUPPORT
347 static int kernel_get_module_dso(const char *module, struct dso **pdso)
351 const char *vmlinux_name;
355 char module_name[128];
357 snprintf(module_name, sizeof(module_name), "[%s]", module);
358 map = maps__find_by_name(machine__kernel_maps(host_machine), module_name);
363 pr_debug("Failed to find module %s.\n", module);
367 map = machine__kernel_map(host_machine);
369 if (!dso->has_build_id)
370 dso__read_running_kernel_build_id(dso, host_machine);
372 vmlinux_name = symbol_conf.vmlinux_name;
375 ret = dso__load_vmlinux(dso, map, vmlinux_name, false);
377 ret = dso__load_vmlinux_path(dso, map);
384 * Some binaries like glibc have special symbols which are on the symbol
385 * table, but not in the debuginfo. If we can find the address of the
386 * symbol from map, we can translate the address back to the probe point.
388 static int find_alternative_probe_point(struct debuginfo *dinfo,
389 struct perf_probe_point *pp,
390 struct perf_probe_point *result,
391 const char *target, struct nsinfo *nsi,
394 struct map *map = NULL;
400 /* This can work only for function-name based one */
401 if (!pp->function || pp->file)
404 map = get_target_map(target, nsi, uprobes);
408 /* Find the address of given function */
409 map__for_each_symbol_by_name(map, pp->function, sym, idx) {
411 address = sym->start;
412 if (sym->type == STT_GNU_IFUNC)
413 pr_warning("Warning: The probe function (%s) is a GNU indirect function.\n"
414 "Consider identifying the final function used at run time and set the probe directly on that.\n",
417 address = map__unmap_ip(map, sym->start) - map__reloc(map);
424 pr_debug("Symbol %s address found : %" PRIx64 "\n",
425 pp->function, address);
427 ret = debuginfo__find_probe_point(dinfo, address, result);
429 ret = (!ret) ? -ENOENT : ret;
431 result->offset += pp->offset;
432 result->line += pp->line;
433 result->retprobe = pp->retprobe;
443 static int get_alternative_probe_event(struct debuginfo *dinfo,
444 struct perf_probe_event *pev,
445 struct perf_probe_point *tmp)
449 memcpy(tmp, &pev->point, sizeof(*tmp));
450 memset(&pev->point, 0, sizeof(pev->point));
451 ret = find_alternative_probe_point(dinfo, tmp, &pev->point, pev->target,
452 pev->nsi, pev->uprobes);
454 memcpy(&pev->point, tmp, sizeof(*tmp));
459 static int get_alternative_line_range(struct debuginfo *dinfo,
460 struct line_range *lr,
461 const char *target, bool user)
463 struct perf_probe_point pp = { .function = lr->function,
466 struct perf_probe_point result;
469 memset(&result, 0, sizeof(result));
471 if (lr->end != INT_MAX)
472 len = lr->end - lr->start;
473 ret = find_alternative_probe_point(dinfo, &pp, &result,
476 lr->function = result.function;
477 lr->file = result.file;
478 lr->start = result.line;
479 if (lr->end != INT_MAX)
480 lr->end = lr->start + len;
481 clear_perf_probe_point(&pp);
486 #ifdef HAVE_DEBUGINFOD_SUPPORT
487 static struct debuginfo *open_from_debuginfod(struct dso *dso, struct nsinfo *nsi,
490 debuginfod_client *c = debuginfod_begin();
491 char sbuild_id[SBUILD_ID_SIZE + 1];
492 struct debuginfo *ret = NULL;
500 build_id__sprintf(&dso->bid, sbuild_id);
501 fd = debuginfod_find_debuginfo(c, (const unsigned char *)sbuild_id,
508 pr_debug("Failed to find debuginfo in debuginfod.\n");
512 pr_debug("Load debuginfo from debuginfod (%s)\n", path);
514 nsinfo__mountns_enter(nsi, &nsc);
515 ret = debuginfo__new((const char *)path);
516 nsinfo__mountns_exit(&nsc);
521 struct debuginfo *open_from_debuginfod(struct dso *dso __maybe_unused,
522 struct nsinfo *nsi __maybe_unused,
523 bool silent __maybe_unused)
529 /* Open new debuginfo of given module */
530 static struct debuginfo *open_debuginfo(const char *module, struct nsinfo *nsi,
533 const char *path = module;
534 char reason[STRERR_BUFSIZE];
535 struct debuginfo *ret = NULL;
536 struct dso *dso = NULL;
540 if (!module || !strchr(module, '/')) {
541 err = kernel_get_module_dso(module, &dso);
543 if (!dso || dso->load_errno == 0) {
544 if (!str_error_r(-err, reason, STRERR_BUFSIZE))
545 strcpy(reason, "(unknown)");
547 dso__strerror_load(dso, reason, STRERR_BUFSIZE);
549 ret = open_from_debuginfod(dso, nsi, silent);
554 pr_err("Module %s is not loaded, please specify its full path name.\n", module);
556 pr_err("Failed to find the path for the kernel: %s\n", reason);
560 path = dso->long_name;
562 nsinfo__mountns_enter(nsi, &nsc);
563 ret = debuginfo__new(path);
564 if (!ret && !silent) {
565 pr_warning("The %s file has no debug information.\n", path);
566 if (!module || !strtailcmp(path, ".ko"))
567 pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, ");
569 pr_warning("Rebuild with -g, ");
570 pr_warning("or install an appropriate debuginfo package.\n");
572 nsinfo__mountns_exit(&nsc);
576 /* For caching the last debuginfo */
577 static struct debuginfo *debuginfo_cache;
578 static char *debuginfo_cache_path;
580 static struct debuginfo *debuginfo_cache__open(const char *module, bool silent)
582 const char *path = module;
584 /* If the module is NULL, it should be the kernel. */
588 if (debuginfo_cache_path && !strcmp(debuginfo_cache_path, path))
591 /* Copy module path */
592 free(debuginfo_cache_path);
593 debuginfo_cache_path = strdup(path);
594 if (!debuginfo_cache_path) {
595 debuginfo__delete(debuginfo_cache);
596 debuginfo_cache = NULL;
600 debuginfo_cache = open_debuginfo(module, NULL, silent);
601 if (!debuginfo_cache)
602 zfree(&debuginfo_cache_path);
604 return debuginfo_cache;
607 static void debuginfo_cache__exit(void)
609 debuginfo__delete(debuginfo_cache);
610 debuginfo_cache = NULL;
611 zfree(&debuginfo_cache_path);
615 static int get_text_start_address(const char *exec, u64 *address,
621 int fd, ret = -ENOENT;
624 nsinfo__mountns_enter(nsi, &nsc);
625 fd = open(exec, O_RDONLY);
626 nsinfo__mountns_exit(&nsc);
630 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
636 if (gelf_getehdr(elf, &ehdr) == NULL)
639 if (!elf_section_by_name(elf, &ehdr, &shdr, ".text", NULL))
642 *address = shdr.sh_addr - shdr.sh_offset;
653 * Convert trace point to probe point with debuginfo
655 static int find_perf_probe_point_from_dwarf(struct probe_trace_point *tp,
656 struct perf_probe_point *pp,
659 struct debuginfo *dinfo = NULL;
661 u64 addr = tp->address;
664 /* convert the address to dwarf address */
670 ret = get_text_start_address(tp->module, &stext, NULL);
674 } else if (tp->symbol) {
675 /* If the module is given, this returns relative address */
676 ret = kernel_get_symbol_address_by_name(tp->symbol, &addr,
677 false, !!tp->module);
683 pr_debug("try to find information at %" PRIx64 " in %s\n", addr,
684 tp->module ? : "kernel");
686 dinfo = debuginfo_cache__open(tp->module, verbose <= 0);
688 ret = debuginfo__find_probe_point(dinfo, addr, pp);
693 pp->retprobe = tp->retprobe;
697 pr_debug("Failed to find corresponding probes from debuginfo.\n");
698 return ret ? : -ENOENT;
701 /* Adjust symbol name and address */
702 static int post_process_probe_trace_point(struct probe_trace_point *tp,
703 struct map *map, u64 offs)
706 u64 addr = tp->address - offs;
708 sym = map__find_symbol(map, addr);
711 * If the address is in the inittext section, map can not
712 * find it. Ignore it if we are probing offline kernel.
714 return (symbol_conf.ignore_vmlinux_buildid) ? 0 : -ENOENT;
717 if (strcmp(sym->name, tp->symbol)) {
718 /* If we have no realname, use symbol for it */
720 tp->realname = tp->symbol;
723 tp->symbol = strdup(sym->name);
727 tp->offset = addr - sym->start;
734 * Rename DWARF symbols to ELF symbols -- gcc sometimes optimizes functions
735 * and generate new symbols with suffixes such as .constprop.N or .isra.N
736 * etc. Since those symbols are not recorded in DWARF, we have to find
737 * correct generated symbols from offline ELF binary.
738 * For online kernel or uprobes we don't need this because those are
739 * rebased on _text, or already a section relative address.
742 post_process_offline_probe_trace_events(struct probe_trace_event *tevs,
743 int ntevs, const char *pathname)
749 /* Prepare a map for offline binary */
750 map = dso__new_map(pathname);
751 if (!map || get_text_start_address(pathname, &stext, NULL) < 0) {
752 pr_warning("Failed to get ELF symbols for %s\n", pathname);
756 for (i = 0; i < ntevs; i++) {
757 ret = post_process_probe_trace_point(&tevs[i].point,
767 static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs,
768 int ntevs, const char *exec,
777 ret = get_text_start_address(exec, &stext, nsi);
781 for (i = 0; i < ntevs && ret >= 0; i++) {
782 /* point.address is the address of point.symbol + point.offset */
783 tevs[i].point.address -= stext;
784 tevs[i].point.module = strdup(exec);
785 if (!tevs[i].point.module) {
789 tevs[i].uprobes = true;
796 post_process_module_probe_trace_events(struct probe_trace_event *tevs,
797 int ntevs, const char *module,
798 struct debuginfo *dinfo)
800 Dwarf_Addr text_offs = 0;
802 char *mod_name = NULL;
808 map = get_target_map(module, NULL, false);
809 if (!map || debuginfo__get_text_offset(dinfo, &text_offs, true) < 0) {
810 pr_warning("Failed to get ELF symbols for %s\n", module);
814 mod_name = find_module_name(module);
815 for (i = 0; i < ntevs; i++) {
816 ret = post_process_probe_trace_point(&tevs[i].point,
820 tevs[i].point.module =
821 strdup(mod_name ? mod_name : module);
822 if (!tevs[i].point.module) {
835 post_process_kernel_probe_trace_events(struct probe_trace_event *tevs,
838 struct ref_reloc_sym *reloc_sym;
843 /* Skip post process if the target is an offline kernel */
844 if (symbol_conf.ignore_vmlinux_buildid)
845 return post_process_offline_probe_trace_events(tevs, ntevs,
846 symbol_conf.vmlinux_name);
848 reloc_sym = kernel_get_ref_reloc_sym(&map);
850 pr_warning("Relocated base symbol is not found! "
851 "Check /proc/sys/kernel/kptr_restrict\n"
852 "and /proc/sys/kernel/perf_event_paranoid. "
853 "Or run as privileged perf user.\n\n");
857 for (i = 0; i < ntevs; i++) {
858 if (!tevs[i].point.address)
860 if (tevs[i].point.retprobe && !kretprobe_offset_is_supported())
863 * If we found a wrong one, mark it by NULL symbol.
864 * Since addresses in debuginfo is same as objdump, we need
865 * to convert it to addresses on memory.
867 if (kprobe_warn_out_range(tevs[i].point.symbol,
868 map__objdump_2mem(map, tevs[i].point.address))) {
872 tmp = strdup(reloc_sym->name);
876 /* If we have no realname, use symbol for it */
877 if (!tevs[i].point.realname)
878 tevs[i].point.realname = tevs[i].point.symbol;
880 free(tevs[i].point.symbol);
881 tevs[i].point.symbol = tmp;
882 tevs[i].point.offset = tevs[i].point.address -
883 (map__reloc(map) ? reloc_sym->unrelocated_addr :
890 arch__post_process_probe_trace_events(struct perf_probe_event *pev __maybe_unused,
891 int ntevs __maybe_unused)
895 /* Post processing the probe events */
896 static int post_process_probe_trace_events(struct perf_probe_event *pev,
897 struct probe_trace_event *tevs,
898 int ntevs, const char *module,
899 bool uprobe, struct debuginfo *dinfo)
904 ret = add_exec_to_probe_trace_events(tevs, ntevs, module,
907 /* Currently ref_reloc_sym based probe is not for drivers */
908 ret = post_process_module_probe_trace_events(tevs, ntevs,
911 ret = post_process_kernel_probe_trace_events(tevs, ntevs);
914 arch__post_process_probe_trace_events(pev, ntevs);
919 /* Try to find perf_probe_event with debuginfo */
920 static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
921 struct probe_trace_event **tevs)
923 bool need_dwarf = perf_probe_event_need_dwarf(pev);
924 struct perf_probe_point tmp;
925 struct debuginfo *dinfo;
928 /* Workaround for gcc #98776 issue.
929 * Perf failed to add kretprobe event with debuginfo of vmlinux which is
930 * compiled by gcc with -fpatchable-function-entry option enabled. The
931 * same issue with kernel module. The retprobe doesn`t need debuginfo.
932 * This workaround solution use map to query the probe function address
933 * for retprobe event.
935 if (pev->point.retprobe)
938 dinfo = open_debuginfo(pev->target, pev->nsi, !need_dwarf);
942 pr_debug("Could not open debuginfo. Try to use symbols.\n");
946 pr_debug("Try to find probe point from debuginfo.\n");
947 /* Searching trace events corresponding to a probe event */
948 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
950 if (ntevs == 0) { /* Not found, retry with an alternative */
951 ret = get_alternative_probe_event(dinfo, pev, &tmp);
953 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
955 * Write back to the original probe_event for
956 * setting appropriate (user given) event name
958 clear_perf_probe_point(&pev->point);
959 memcpy(&pev->point, &tmp, sizeof(tmp));
963 if (ntevs > 0) { /* Succeeded to find trace events */
964 pr_debug("Found %d probe_trace_events.\n", ntevs);
965 ret = post_process_probe_trace_events(pev, *tevs, ntevs,
966 pev->target, pev->uprobes, dinfo);
967 if (ret < 0 || ret == ntevs) {
968 pr_debug("Post processing failed or all events are skipped. (%d)\n", ret);
969 clear_probe_trace_events(*tevs, ntevs);
975 debuginfo__delete(dinfo);
977 if (ntevs == 0) { /* No error but failed to find probe point. */
978 char *probe_point = synthesize_perf_probe_point(&pev->point);
979 pr_warning("Probe point '%s' not found.\n", probe_point);
982 } else if (ntevs < 0) {
983 /* Error path : ntevs < 0 */
984 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
986 pr_warning("Warning: No dwarf info found in the vmlinux - "
987 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
989 pr_debug("Trying to use symbols.\n");
996 #define LINEBUF_SIZE 256
997 #define NR_ADDITIONAL_LINES 2
999 static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
1001 char buf[LINEBUF_SIZE], sbuf[STRERR_BUFSIZE];
1002 const char *color = show_num ? "" : PERF_COLOR_BLUE;
1003 const char *prefix = NULL;
1006 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
1011 prefix = show_num ? "%7d " : " ";
1012 color_fprintf(stdout, color, prefix, l);
1014 color_fprintf(stdout, color, "%s", buf);
1016 } while (strchr(buf, '\n') == NULL);
1021 pr_warning("File read error: %s\n",
1022 str_error_r(errno, sbuf, sizeof(sbuf)));
1028 static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
1030 int rv = __show_one_line(fp, l, skip, show_num);
1032 pr_warning("Source file is shorter than expected.\n");
1038 #define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
1039 #define show_one_line(f,l) _show_one_line(f,l,false,false)
1040 #define skip_one_line(f,l) _show_one_line(f,l,true,false)
1041 #define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
1044 * Show line-range always requires debuginfo to find source file and
1047 static int __show_line_range(struct line_range *lr, const char *module,
1050 struct build_id bid;
1052 struct int_node *ln;
1053 struct debuginfo *dinfo;
1057 char sbuf[STRERR_BUFSIZE];
1058 char sbuild_id[SBUILD_ID_SIZE] = "";
1060 /* Search a line range */
1061 dinfo = open_debuginfo(module, NULL, false);
1065 ret = debuginfo__find_line_range(dinfo, lr);
1066 if (!ret) { /* Not found, retry with an alternative */
1067 ret = get_alternative_line_range(dinfo, lr, module, user);
1069 ret = debuginfo__find_line_range(dinfo, lr);
1071 if (dinfo->build_id) {
1072 build_id__init(&bid, dinfo->build_id, BUILD_ID_SIZE);
1073 build_id__sprintf(&bid, sbuild_id);
1075 debuginfo__delete(dinfo);
1076 if (ret == 0 || ret == -ENOENT) {
1077 pr_warning("Specified source line is not found.\n");
1079 } else if (ret < 0) {
1080 pr_warning("Debuginfo analysis failed.\n");
1084 /* Convert source file path */
1086 ret = find_source_path(tmp, sbuild_id, lr->comp_dir, &lr->path);
1088 /* Free old path when new path is assigned */
1089 if (tmp != lr->path)
1093 pr_warning("Failed to find source file path.\n");
1100 fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
1101 lr->start - lr->offset);
1103 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
1105 fp = fopen(lr->path, "r");
1107 pr_warning("Failed to open %s: %s\n", lr->path,
1108 str_error_r(errno, sbuf, sizeof(sbuf)));
1111 /* Skip to starting line number */
1112 while (l < lr->start) {
1113 ret = skip_one_line(fp, l++);
1118 intlist__for_each_entry(ln, lr->line_list) {
1119 for (; ln->i > (unsigned long)l; l++) {
1120 ret = show_one_line(fp, l - lr->offset);
1124 ret = show_one_line_with_num(fp, l++ - lr->offset);
1129 if (lr->end == INT_MAX)
1130 lr->end = l + NR_ADDITIONAL_LINES;
1131 while (l <= lr->end) {
1132 ret = show_one_line_or_eof(fp, l++ - lr->offset);
1141 int show_line_range(struct line_range *lr, const char *module,
1142 struct nsinfo *nsi, bool user)
1145 struct nscookie nsc;
1147 ret = init_probe_symbol_maps(user);
1150 nsinfo__mountns_enter(nsi, &nsc);
1151 ret = __show_line_range(lr, module, user);
1152 nsinfo__mountns_exit(&nsc);
1153 exit_probe_symbol_maps();
1158 static int show_available_vars_at(struct debuginfo *dinfo,
1159 struct perf_probe_event *pev,
1160 struct strfilter *_filter)
1164 struct str_node *node;
1165 struct variable_list *vls = NULL, *vl;
1166 struct perf_probe_point tmp;
1169 buf = synthesize_perf_probe_point(&pev->point);
1172 pr_debug("Searching variables at %s\n", buf);
1174 ret = debuginfo__find_available_vars_at(dinfo, pev, &vls);
1175 if (!ret) { /* Not found, retry with an alternative */
1176 ret = get_alternative_probe_event(dinfo, pev, &tmp);
1178 ret = debuginfo__find_available_vars_at(dinfo, pev,
1180 /* Release the old probe_point */
1181 clear_perf_probe_point(&tmp);
1185 if (ret == 0 || ret == -ENOENT) {
1186 pr_err("Failed to find the address of %s\n", buf);
1189 pr_warning("Debuginfo analysis failed.\n");
1193 /* Some variables are found */
1194 fprintf(stdout, "Available variables at %s\n", buf);
1195 for (i = 0; i < ret; i++) {
1198 * A probe point might be converted to
1199 * several trace points.
1201 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
1203 zfree(&vl->point.symbol);
1206 strlist__for_each_entry(node, vl->vars) {
1207 var = strchr(node->s, '\t') + 1;
1208 if (strfilter__compare(_filter, var)) {
1209 fprintf(stdout, "\t\t%s\n", node->s);
1213 strlist__delete(vl->vars);
1216 fprintf(stdout, "\t\t(No matched variables)\n");
1224 /* Show available variables on given probe point */
1225 int show_available_vars(struct perf_probe_event *pevs, int npevs,
1226 struct strfilter *_filter)
1229 struct debuginfo *dinfo;
1231 ret = init_probe_symbol_maps(pevs->uprobes);
1235 dinfo = open_debuginfo(pevs->target, pevs->nsi, false);
1243 for (i = 0; i < npevs && ret >= 0; i++)
1244 ret = show_available_vars_at(dinfo, &pevs[i], _filter);
1246 debuginfo__delete(dinfo);
1248 exit_probe_symbol_maps();
1252 #else /* !HAVE_DWARF_SUPPORT */
1254 static void debuginfo_cache__exit(void)
1259 find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused,
1260 struct perf_probe_point *pp __maybe_unused,
1261 bool is_kprobe __maybe_unused)
1266 static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
1267 struct probe_trace_event **tevs __maybe_unused)
1269 if (perf_probe_event_need_dwarf(pev)) {
1270 pr_warning("Debuginfo-analysis is not supported.\n");
1277 int show_line_range(struct line_range *lr __maybe_unused,
1278 const char *module __maybe_unused,
1279 struct nsinfo *nsi __maybe_unused,
1280 bool user __maybe_unused)
1282 pr_warning("Debuginfo-analysis is not supported.\n");
1286 int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
1287 int npevs __maybe_unused,
1288 struct strfilter *filter __maybe_unused)
1290 pr_warning("Debuginfo-analysis is not supported.\n");
1295 void line_range__clear(struct line_range *lr)
1297 zfree(&lr->function);
1300 zfree(&lr->comp_dir);
1301 intlist__delete(lr->line_list);
1304 int line_range__init(struct line_range *lr)
1306 memset(lr, 0, sizeof(*lr));
1307 lr->line_list = intlist__new(NULL);
1314 static int parse_line_num(char **ptr, int *val, const char *what)
1316 const char *start = *ptr;
1319 *val = strtol(*ptr, ptr, 0);
1320 if (errno || *ptr == start) {
1321 semantic_error("'%s' is not a valid number.\n", what);
1327 /* Check the name is good for event, group or function */
1328 static bool is_c_func_name(const char *name)
1330 if (!isalpha(*name) && *name != '_')
1332 while (*++name != '\0') {
1333 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
1340 * Stuff 'lr' according to the line range described by 'arg'.
1341 * The line range syntax is described by:
1343 * SRC[:SLN[+NUM|-ELN]]
1344 * FNC[@SRC][:SLN[+NUM|-ELN]]
1346 int parse_line_range_desc(const char *arg, struct line_range *lr)
1348 char *range, *file, *name = strdup(arg);
1357 range = strchr(name, ':');
1361 err = parse_line_num(&range, &lr->start, "start line");
1365 if (*range == '+' || *range == '-') {
1366 const char c = *range++;
1368 err = parse_line_num(&range, &lr->end, "end line");
1373 lr->end += lr->start;
1375 * Adjust the number of lines here.
1376 * If the number of lines == 1, the
1377 * end of line should be equal to
1378 * the start of line.
1384 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
1387 if (lr->start > lr->end) {
1388 semantic_error("Start line must be smaller"
1389 " than end line.\n");
1392 if (*range != '\0') {
1393 semantic_error("Tailing with invalid str '%s'.\n", range);
1398 file = strchr(name, '@');
1401 lr->file = strdup(++file);
1402 if (lr->file == NULL) {
1406 lr->function = name;
1407 } else if (strchr(name, '/') || strchr(name, '.'))
1409 else if (is_c_func_name(name))/* We reuse it for checking funcname */
1410 lr->function = name;
1411 else { /* Invalid name */
1412 semantic_error("'%s' is not a valid function name.\n", name);
1423 static int parse_perf_probe_event_name(char **arg, struct perf_probe_event *pev)
1427 ptr = strpbrk_esc(*arg, ":");
1430 if (!pev->sdt && !is_c_func_name(*arg))
1432 pev->group = strdup_esc(*arg);
1439 pev->event = strdup_esc(*arg);
1440 if (pev->event == NULL)
1443 if (!pev->sdt && !is_c_func_name(pev->event)) {
1447 semantic_error("%s is bad for event name -it must "
1448 "follow C symbol-naming rule.\n", *arg);
1454 /* Parse probepoint definition. */
1455 static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
1457 struct perf_probe_point *pp = &pev->point;
1460 bool file_spec = false;
1465 * perf probe [GRP:][EVENT=]SRC[:LN|;PTN]
1466 * perf probe [GRP:][EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
1467 * perf probe %[GRP:]SDT_EVENT
1472 if (is_sdt_event(arg)) {
1478 ptr = strpbrk_esc(arg, ";=@+%");
1482 semantic_error("%s must be an SDT name.\n",
1486 /* This must be a target file name or build id */
1487 tmp = build_id_cache__complement(ptr + 1);
1489 pev->target = build_id_cache__origname(tmp);
1492 pev->target = strdup_esc(ptr + 1);
1497 ret = parse_perf_probe_event_name(&arg, pev);
1499 if (asprintf(&pev->point.function, "%%%s", pev->event) < 0)
1505 if (ptr && *ptr == '=') { /* Event name */
1508 ret = parse_perf_probe_event_name(&arg, pev);
1516 * Check arg is function or file name and copy it.
1518 * We consider arg to be a file spec if and only if it satisfies
1519 * all of the below criteria::
1520 * - it does not include any of "+@%",
1521 * - it includes one of ":;", and
1522 * - it has a period '.' in the name.
1524 * Otherwise, we consider arg to be a function specification.
1526 if (!strpbrk_esc(arg, "+@%")) {
1527 ptr = strpbrk_esc(arg, ";:");
1528 /* This is a file spec if it includes a '.' before ; or : */
1529 if (ptr && memchr(arg, '.', ptr - arg))
1533 ptr = strpbrk_esc(arg, ";:+@%");
1542 tmp = strdup_esc(arg);
1553 * Keep pp->function even if this is absolute address,
1554 * so it can mark whether abs_address is valid.
1555 * Which make 'perf probe lib.bin 0x0' possible.
1557 * Note that checking length of tmp is not needed
1558 * because when we access tmp[1] we know tmp[0] is '0',
1559 * so tmp[1] should always valid (but could be '\0').
1561 if (tmp && !strncmp(tmp, "0x", 2)) {
1562 pp->abs_address = strtoull(pp->function, &tmp, 0);
1564 semantic_error("Invalid absolute address.\n");
1570 /* Parse other options */
1574 if (c == ';') { /* Lazy pattern must be the last part */
1575 pp->lazy_line = strdup(arg); /* let leave escapes */
1576 if (pp->lazy_line == NULL)
1580 ptr = strpbrk_esc(arg, ";:+@%");
1586 case ':': /* Line number */
1587 pp->line = strtoul(arg, &tmp, 0);
1589 semantic_error("There is non-digit char"
1590 " in line number.\n");
1594 case '+': /* Byte offset from a symbol */
1595 pp->offset = strtoul(arg, &tmp, 0);
1597 semantic_error("There is non-digit character"
1602 case '@': /* File name */
1604 semantic_error("SRC@SRC is not allowed.\n");
1607 pp->file = strdup_esc(arg);
1608 if (pp->file == NULL)
1611 case '%': /* Probe places */
1612 if (strcmp(arg, "return") == 0) {
1614 } else { /* Others not supported yet */
1615 semantic_error("%%%s is not supported.\n", arg);
1619 default: /* Buggy case */
1620 pr_err("This program has a bug at %s:%d.\n",
1621 __FILE__, __LINE__);
1627 /* Exclusion check */
1628 if (pp->lazy_line && pp->line) {
1629 semantic_error("Lazy pattern can't be used with"
1634 if (pp->lazy_line && pp->offset) {
1635 semantic_error("Lazy pattern can't be used with offset.\n");
1639 if (pp->line && pp->offset) {
1640 semantic_error("Offset can't be used with line number.\n");
1644 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
1645 semantic_error("File always requires line number or "
1650 if (pp->offset && !pp->function) {
1651 semantic_error("Offset requires an entry function.\n");
1655 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
1656 semantic_error("Offset/Line/Lazy pattern can't be used with "
1661 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
1662 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
1667 /* Parse perf-probe event argument */
1668 static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
1670 char *tmp, *goodname;
1671 struct perf_probe_arg_field **fieldp;
1673 pr_debug("parsing arg: %s into ", str);
1675 tmp = strchr(str, '=');
1677 arg->name = strndup(str, tmp - str);
1678 if (arg->name == NULL)
1680 pr_debug("name:%s ", arg->name);
1684 tmp = strchr(str, '@');
1685 if (tmp && tmp != str && !strcmp(tmp + 1, "user")) { /* user attr */
1686 if (!user_access_is_supported()) {
1687 semantic_error("ftrace does not support user access\n");
1691 arg->user_access = true;
1692 pr_debug("user_access ");
1695 tmp = strchr(str, ':');
1696 if (tmp) { /* Type setting */
1698 arg->type = strdup(tmp + 1);
1699 if (arg->type == NULL)
1701 pr_debug("type:%s ", arg->type);
1704 tmp = strpbrk(str, "-.[");
1705 if (!is_c_varname(str) || !tmp) {
1706 /* A variable, register, symbol or special value */
1707 arg->var = strdup(str);
1708 if (arg->var == NULL)
1710 pr_debug("%s\n", arg->var);
1714 /* Structure fields or array element */
1715 arg->var = strndup(str, tmp - str);
1716 if (arg->var == NULL)
1718 goodname = arg->var;
1719 pr_debug("%s, ", arg->var);
1720 fieldp = &arg->field;
1723 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
1724 if (*fieldp == NULL)
1726 if (*tmp == '[') { /* Array */
1728 (*fieldp)->index = strtol(str + 1, &tmp, 0);
1729 (*fieldp)->ref = true;
1730 if (*tmp != ']' || tmp == str + 1) {
1731 semantic_error("Array index must be a"
1738 } else { /* Structure */
1741 (*fieldp)->ref = false;
1742 } else if (tmp[1] == '>') {
1744 (*fieldp)->ref = true;
1746 semantic_error("Argument parse error: %s\n",
1750 tmp = strpbrk(str, "-.[");
1753 (*fieldp)->name = strndup(str, tmp - str);
1754 if ((*fieldp)->name == NULL)
1757 goodname = (*fieldp)->name;
1758 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
1759 fieldp = &(*fieldp)->next;
1762 (*fieldp)->name = strdup(str);
1763 if ((*fieldp)->name == NULL)
1766 goodname = (*fieldp)->name;
1767 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
1769 /* If no name is specified, set the last field name (not array index)*/
1771 arg->name = strdup(goodname);
1772 if (arg->name == NULL)
1778 /* Parse perf-probe event command */
1779 int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
1782 int argc, i, ret = 0;
1784 argv = argv_split(cmd, &argc);
1786 pr_debug("Failed to split arguments.\n");
1789 if (argc - 1 > MAX_PROBE_ARGS) {
1790 semantic_error("Too many probe arguments (%d).\n", argc - 1);
1794 /* Parse probe point */
1795 ret = parse_perf_probe_point(argv[0], pev);
1799 /* Generate event name if needed */
1800 if (!pev->event && pev->point.function && pev->point.line
1801 && !pev->point.lazy_line && !pev->point.offset) {
1802 if (asprintf(&pev->event, "%s_L%d", pev->point.function,
1803 pev->point.line) < 0) {
1809 /* Copy arguments and ensure return probe has no C argument */
1810 pev->nargs = argc - 1;
1811 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1812 if (pev->args == NULL) {
1816 for (i = 0; i < pev->nargs && ret >= 0; i++) {
1817 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1819 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
1820 semantic_error("You can't specify local variable for"
1831 /* Returns true if *any* ARG is either C variable, $params or $vars. */
1832 bool perf_probe_with_var(struct perf_probe_event *pev)
1836 for (i = 0; i < pev->nargs; i++)
1837 if (is_c_varname(pev->args[i].var) ||
1838 !strcmp(pev->args[i].var, PROBE_ARG_PARAMS) ||
1839 !strcmp(pev->args[i].var, PROBE_ARG_VARS))
1844 /* Return true if this perf_probe_event requires debuginfo */
1845 bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
1847 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1850 if (perf_probe_with_var(pev))
1856 /* Parse probe_events event into struct probe_point */
1857 int parse_probe_trace_command(const char *cmd, struct probe_trace_event *tev)
1859 struct probe_trace_point *tp = &tev->point;
1862 char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
1866 pr_debug("Parsing probe_events: %s\n", cmd);
1867 argv = argv_split(cmd, &argc);
1869 pr_debug("Failed to split arguments.\n");
1873 semantic_error("Too few probe arguments.\n");
1878 /* Scan event and group name. */
1879 argv0_str = strdup(argv[0]);
1880 if (argv0_str == NULL) {
1884 fmt1_str = strtok_r(argv0_str, ":", &fmt);
1885 fmt2_str = strtok_r(NULL, "/", &fmt);
1886 fmt3_str = strtok_r(NULL, " \t", &fmt);
1887 if (fmt1_str == NULL || fmt2_str == NULL || fmt3_str == NULL) {
1888 semantic_error("Failed to parse event name: %s\n", argv[0]);
1893 tev->group = strdup(fmt2_str);
1894 tev->event = strdup(fmt3_str);
1895 if (tev->group == NULL || tev->event == NULL) {
1899 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
1901 tp->retprobe = (pr == 'r');
1903 /* Scan module name(if there), function name and offset */
1904 p = strchr(argv[1], ':');
1906 tp->module = strndup(argv[1], p - argv[1]);
1911 tev->uprobes = (tp->module[0] == '/');
1915 fmt1_str = strtok_r(p, "+", &fmt);
1916 /* only the address started with 0x */
1917 if (fmt1_str[0] == '0') {
1919 * Fix a special case:
1920 * if address == 0, kernel reports something like:
1921 * p:probe_libc/abs_0 /lib/libc-2.18.so:0x (null) arg1=%ax
1922 * Newer kernel may fix that, but we want to
1923 * support old kernel also.
1925 if (strcmp(fmt1_str, "0x") == 0) {
1926 if (!argv[2] || strcmp(argv[2], "(null)")) {
1933 for (i = 2; argv[i + 1] != NULL; i++)
1934 argv[i] = argv[i + 1];
1939 tp->address = strtoull(fmt1_str, NULL, 0);
1941 /* Only the symbol-based probe has offset */
1942 tp->symbol = strdup(fmt1_str);
1943 if (tp->symbol == NULL) {
1947 fmt2_str = strtok_r(NULL, "", &fmt);
1948 if (fmt2_str == NULL)
1951 tp->offset = strtoul(fmt2_str, NULL, 10);
1955 fmt2_str = strchr(p, '(');
1957 tp->ref_ctr_offset = strtoul(fmt2_str + 1, NULL, 0);
1960 tev->nargs = argc - 2;
1961 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
1962 if (tev->args == NULL) {
1966 for (i = 0; i < tev->nargs; i++) {
1967 p = strchr(argv[i + 2], '=');
1968 if (p) /* We don't need which register is assigned. */
1972 tev->args[i].name = strdup(argv[i + 2]);
1973 /* TODO: parse regs and offset */
1974 tev->args[i].value = strdup(p);
1975 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1987 /* Compose only probe arg */
1988 char *synthesize_perf_probe_arg(struct perf_probe_arg *pa)
1990 struct perf_probe_arg_field *field = pa->field;
1995 if (strbuf_init(&buf, 64) < 0)
1998 if (pa->name && pa->var)
1999 err = strbuf_addf(&buf, "%s=%s", pa->name, pa->var);
2001 err = strbuf_addstr(&buf, pa->name ?: pa->var);
2006 if (field->name[0] == '[')
2007 err = strbuf_addstr(&buf, field->name);
2009 err = strbuf_addf(&buf, "%s%s", field->ref ? "->" : ".",
2011 field = field->next;
2017 if (strbuf_addf(&buf, ":%s", pa->type) < 0)
2020 ret = strbuf_detach(&buf, NULL);
2022 strbuf_release(&buf);
2026 /* Compose only probe point (not argument) */
2027 static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
2030 char *tmp, *ret = NULL;
2033 if (strbuf_init(&buf, 64) < 0)
2037 if (strbuf_addstr(&buf, pp->function) < 0)
2040 err = strbuf_addf(&buf, "+%lu", pp->offset);
2042 err = strbuf_addf(&buf, ":%d", pp->line);
2043 else if (pp->retprobe)
2044 err = strbuf_addstr(&buf, "%return");
2052 tmp = strchr(pp->file + len - 30, '/');
2053 tmp = tmp ? tmp + 1 : pp->file + len - 30;
2055 err = strbuf_addf(&buf, "@%s", tmp);
2056 if (!err && !pp->function && pp->line)
2057 err = strbuf_addf(&buf, ":%d", pp->line);
2060 ret = strbuf_detach(&buf, NULL);
2062 strbuf_release(&buf);
2066 char *synthesize_perf_probe_command(struct perf_probe_event *pev)
2069 char *tmp, *ret = NULL;
2072 if (strbuf_init(&buf, 64))
2075 if (strbuf_addf(&buf, "%s:%s=", pev->group ?: PERFPROBE_GROUP,
2079 tmp = synthesize_perf_probe_point(&pev->point);
2080 if (!tmp || strbuf_addstr(&buf, tmp) < 0) {
2086 for (i = 0; i < pev->nargs; i++) {
2087 tmp = synthesize_perf_probe_arg(pev->args + i);
2088 if (!tmp || strbuf_addf(&buf, " %s", tmp) < 0) {
2095 ret = strbuf_detach(&buf, NULL);
2097 strbuf_release(&buf);
2101 static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
2102 struct strbuf *buf, int depth)
2106 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
2111 if (ref->user_access)
2112 err = strbuf_addf(buf, "%s%ld(", "+u", ref->offset);
2114 err = strbuf_addf(buf, "%+ld(", ref->offset);
2115 return (err < 0) ? err : depth;
2118 static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
2121 struct probe_trace_arg_ref *ref = arg->ref;
2124 /* Argument name or separator */
2126 err = strbuf_addf(buf, " %s=", arg->name);
2128 err = strbuf_addch(buf, ' ');
2132 /* Special case: @XXX */
2133 if (arg->value[0] == '@' && arg->ref)
2136 /* Dereferencing arguments */
2138 depth = __synthesize_probe_trace_arg_ref(ref, buf, 1);
2143 /* Print argument value */
2144 if (arg->value[0] == '@' && arg->ref)
2145 err = strbuf_addf(buf, "%s%+ld", arg->value, arg->ref->offset);
2147 err = strbuf_addstr(buf, arg->value);
2150 while (!err && depth--)
2151 err = strbuf_addch(buf, ')');
2153 /* Print argument type */
2154 if (!err && arg->type)
2155 err = strbuf_addf(buf, ":%s", arg->type);
2161 synthesize_probe_trace_args(struct probe_trace_event *tev, struct strbuf *buf)
2165 for (i = 0; i < tev->nargs && ret >= 0; i++)
2166 ret = synthesize_probe_trace_arg(&tev->args[i], buf);
2172 synthesize_uprobe_trace_def(struct probe_trace_point *tp, struct strbuf *buf)
2176 /* Uprobes must have tp->module */
2180 * If tp->address == 0, then this point must be a
2181 * absolute address uprobe.
2182 * try_to_find_absolute_address() should have made
2183 * tp->symbol to "0x0".
2185 if (!tp->address && (!tp->symbol || strcmp(tp->symbol, "0x0")))
2188 /* Use the tp->address for uprobes */
2189 err = strbuf_addf(buf, "%s:0x%" PRIx64, tp->module, tp->address);
2191 if (err >= 0 && tp->ref_ctr_offset) {
2192 if (!uprobe_ref_ctr_is_supported())
2194 err = strbuf_addf(buf, "(0x%lx)", tp->ref_ctr_offset);
2196 return err >= 0 ? 0 : err;
2200 synthesize_kprobe_trace_def(struct probe_trace_point *tp, struct strbuf *buf)
2202 if (!strncmp(tp->symbol, "0x", 2)) {
2203 /* Absolute address. See try_to_find_absolute_address() */
2204 return strbuf_addf(buf, "%s%s0x%" PRIx64, tp->module ?: "",
2205 tp->module ? ":" : "", tp->address);
2207 return strbuf_addf(buf, "%s%s%s+%lu", tp->module ?: "",
2208 tp->module ? ":" : "", tp->symbol, tp->offset);
2212 char *synthesize_probe_trace_command(struct probe_trace_event *tev)
2214 struct probe_trace_point *tp = &tev->point;
2219 if (strbuf_init(&buf, 32) < 0)
2222 if (strbuf_addf(&buf, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
2223 tev->group, tev->event) < 0)
2227 err = synthesize_uprobe_trace_def(tp, &buf);
2229 err = synthesize_kprobe_trace_def(tp, &buf);
2232 err = synthesize_probe_trace_args(tev, &buf);
2235 ret = strbuf_detach(&buf, NULL);
2237 strbuf_release(&buf);
2241 static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
2242 struct perf_probe_point *pp,
2245 struct symbol *sym = NULL;
2246 struct map *map = NULL;
2247 u64 addr = tp->address;
2251 map = dso__new_map(tp->module);
2254 sym = map__find_symbol(map, addr);
2256 if (tp->symbol && !addr) {
2257 if (kernel_get_symbol_address_by_name(tp->symbol,
2258 &addr, true, false) < 0)
2263 sym = machine__find_kernel_symbol(host_machine, addr, &map);
2270 pp->retprobe = tp->retprobe;
2271 pp->offset = addr - map__unmap_ip(map, sym->start);
2272 pp->function = strdup(sym->name);
2273 ret = pp->function ? 0 : -ENOMEM;
2276 if (map && !is_kprobe) {
2283 static int convert_to_perf_probe_point(struct probe_trace_point *tp,
2284 struct perf_probe_point *pp,
2290 ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe);
2293 ret = find_perf_probe_point_from_map(tp, pp, is_kprobe);
2297 pr_debug("Failed to find probe point from both of dwarf and map.\n");
2300 pp->function = strdup(tp->symbol);
2301 pp->offset = tp->offset;
2303 ret = e_snprintf(buf, 128, "0x%" PRIx64, tp->address);
2306 pp->function = strdup(buf);
2309 if (pp->function == NULL)
2312 pp->retprobe = tp->retprobe;
2317 static int convert_to_perf_probe_event(struct probe_trace_event *tev,
2318 struct perf_probe_event *pev, bool is_kprobe)
2320 struct strbuf buf = STRBUF_INIT;
2323 /* Convert event/group name */
2324 pev->event = strdup(tev->event);
2325 pev->group = strdup(tev->group);
2326 if (pev->event == NULL || pev->group == NULL)
2329 /* Convert trace_point to probe_point */
2330 ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
2334 /* Convert trace_arg to probe_arg */
2335 pev->nargs = tev->nargs;
2336 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
2337 if (pev->args == NULL)
2339 for (i = 0; i < tev->nargs && ret >= 0; i++) {
2340 if (tev->args[i].name)
2341 pev->args[i].name = strdup(tev->args[i].name);
2343 if ((ret = strbuf_init(&buf, 32)) < 0)
2345 ret = synthesize_probe_trace_arg(&tev->args[i], &buf);
2346 pev->args[i].name = strbuf_detach(&buf, NULL);
2348 if (pev->args[i].name == NULL && ret >= 0)
2353 clear_perf_probe_event(pev);
2358 void clear_perf_probe_event(struct perf_probe_event *pev)
2360 struct perf_probe_arg_field *field, *next;
2365 zfree(&pev->target);
2366 clear_perf_probe_point(&pev->point);
2368 for (i = 0; i < pev->nargs; i++) {
2369 zfree(&pev->args[i].name);
2370 zfree(&pev->args[i].var);
2371 zfree(&pev->args[i].type);
2372 field = pev->args[i].field;
2375 zfree(&field->name);
2384 #define strdup_or_goto(str, label) \
2385 ({ char *__p = NULL; if (str && !(__p = strdup(str))) goto label; __p; })
2387 static int perf_probe_point__copy(struct perf_probe_point *dst,
2388 struct perf_probe_point *src)
2390 dst->file = strdup_or_goto(src->file, out_err);
2391 dst->function = strdup_or_goto(src->function, out_err);
2392 dst->lazy_line = strdup_or_goto(src->lazy_line, out_err);
2393 dst->line = src->line;
2394 dst->retprobe = src->retprobe;
2395 dst->offset = src->offset;
2399 clear_perf_probe_point(dst);
2403 static int perf_probe_arg__copy(struct perf_probe_arg *dst,
2404 struct perf_probe_arg *src)
2406 struct perf_probe_arg_field *field, **ppfield;
2408 dst->name = strdup_or_goto(src->name, out_err);
2409 dst->var = strdup_or_goto(src->var, out_err);
2410 dst->type = strdup_or_goto(src->type, out_err);
2413 ppfield = &(dst->field);
2415 *ppfield = zalloc(sizeof(*field));
2418 (*ppfield)->name = strdup_or_goto(field->name, out_err);
2419 (*ppfield)->index = field->index;
2420 (*ppfield)->ref = field->ref;
2421 field = field->next;
2422 ppfield = &((*ppfield)->next);
2429 int perf_probe_event__copy(struct perf_probe_event *dst,
2430 struct perf_probe_event *src)
2434 dst->event = strdup_or_goto(src->event, out_err);
2435 dst->group = strdup_or_goto(src->group, out_err);
2436 dst->target = strdup_or_goto(src->target, out_err);
2437 dst->uprobes = src->uprobes;
2439 if (perf_probe_point__copy(&dst->point, &src->point) < 0)
2442 dst->args = zalloc(sizeof(struct perf_probe_arg) * src->nargs);
2445 dst->nargs = src->nargs;
2447 for (i = 0; i < src->nargs; i++)
2448 if (perf_probe_arg__copy(&dst->args[i], &src->args[i]) < 0)
2453 clear_perf_probe_event(dst);
2457 void clear_probe_trace_event(struct probe_trace_event *tev)
2459 struct probe_trace_arg_ref *ref, *next;
2464 zfree(&tev->point.symbol);
2465 zfree(&tev->point.realname);
2466 zfree(&tev->point.module);
2467 for (i = 0; i < tev->nargs; i++) {
2468 zfree(&tev->args[i].name);
2469 zfree(&tev->args[i].value);
2470 zfree(&tev->args[i].type);
2471 ref = tev->args[i].ref;
2482 struct kprobe_blacklist_node {
2483 struct list_head list;
2489 static void kprobe_blacklist__delete(struct list_head *blacklist)
2491 struct kprobe_blacklist_node *node;
2493 while (!list_empty(blacklist)) {
2494 node = list_first_entry(blacklist,
2495 struct kprobe_blacklist_node, list);
2496 list_del_init(&node->list);
2497 zfree(&node->symbol);
2502 static int kprobe_blacklist__load(struct list_head *blacklist)
2504 struct kprobe_blacklist_node *node;
2505 const char *__debugfs = debugfs__mountpoint();
2506 char buf[PATH_MAX], *p;
2510 if (__debugfs == NULL)
2513 ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs);
2517 fp = fopen(buf, "r");
2522 while (fgets(buf, PATH_MAX, fp)) {
2523 node = zalloc(sizeof(*node));
2528 INIT_LIST_HEAD(&node->list);
2529 list_add_tail(&node->list, blacklist);
2530 if (sscanf(buf, "0x%" PRIx64 "-0x%" PRIx64, &node->start, &node->end) != 2) {
2534 p = strchr(buf, '\t');
2537 if (p[strlen(p) - 1] == '\n')
2538 p[strlen(p) - 1] = '\0';
2540 p = (char *)"unknown";
2541 node->symbol = strdup(p);
2542 if (!node->symbol) {
2546 pr_debug2("Blacklist: 0x%" PRIx64 "-0x%" PRIx64 ", %s\n",
2547 node->start, node->end, node->symbol);
2551 kprobe_blacklist__delete(blacklist);
2557 static struct kprobe_blacklist_node *
2558 kprobe_blacklist__find_by_address(struct list_head *blacklist, u64 address)
2560 struct kprobe_blacklist_node *node;
2562 list_for_each_entry(node, blacklist, list) {
2563 if (node->start <= address && address < node->end)
2570 static LIST_HEAD(kprobe_blacklist);
2572 static void kprobe_blacklist__init(void)
2574 if (!list_empty(&kprobe_blacklist))
2577 if (kprobe_blacklist__load(&kprobe_blacklist) < 0)
2578 pr_debug("No kprobe blacklist support, ignored\n");
2581 static void kprobe_blacklist__release(void)
2583 kprobe_blacklist__delete(&kprobe_blacklist);
2586 static bool kprobe_blacklist__listed(u64 address)
2588 return !!kprobe_blacklist__find_by_address(&kprobe_blacklist, address);
2591 static int perf_probe_event__sprintf(const char *group, const char *event,
2592 struct perf_probe_event *pev,
2594 struct strbuf *result)
2599 if (asprintf(&buf, "%s:%s", group, event) < 0)
2601 ret = strbuf_addf(result, " %-20s (on ", buf);
2606 /* Synthesize only event probe point */
2607 buf = synthesize_perf_probe_point(&pev->point);
2610 ret = strbuf_addstr(result, buf);
2614 ret = strbuf_addf(result, " in %s", module);
2616 if (!ret && pev->nargs > 0) {
2617 ret = strbuf_add(result, " with", 5);
2618 for (i = 0; !ret && i < pev->nargs; i++) {
2619 buf = synthesize_perf_probe_arg(&pev->args[i]);
2622 ret = strbuf_addf(result, " %s", buf);
2627 ret = strbuf_addch(result, ')');
2633 int show_perf_probe_event(const char *group, const char *event,
2634 struct perf_probe_event *pev,
2635 const char *module, bool use_stdout)
2637 struct strbuf buf = STRBUF_INIT;
2640 ret = perf_probe_event__sprintf(group, event, pev, module, &buf);
2643 printf("%s\n", buf.buf);
2645 pr_info("%s\n", buf.buf);
2647 strbuf_release(&buf);
2652 static bool filter_probe_trace_event(struct probe_trace_event *tev,
2653 struct strfilter *filter)
2657 /* At first, check the event name itself */
2658 if (strfilter__compare(filter, tev->event))
2661 /* Next, check the combination of name and group */
2662 if (e_snprintf(tmp, 128, "%s:%s", tev->group, tev->event) < 0)
2664 return strfilter__compare(filter, tmp);
2667 static int __show_perf_probe_events(int fd, bool is_kprobe,
2668 struct strfilter *filter)
2671 struct probe_trace_event tev;
2672 struct perf_probe_event pev;
2673 struct strlist *rawlist;
2674 struct str_node *ent;
2676 memset(&tev, 0, sizeof(tev));
2677 memset(&pev, 0, sizeof(pev));
2679 rawlist = probe_file__get_rawlist(fd);
2683 strlist__for_each_entry(ent, rawlist) {
2684 ret = parse_probe_trace_command(ent->s, &tev);
2686 if (!filter_probe_trace_event(&tev, filter))
2688 ret = convert_to_perf_probe_event(&tev, &pev,
2692 ret = show_perf_probe_event(pev.group, pev.event,
2693 &pev, tev.point.module,
2697 clear_perf_probe_event(&pev);
2698 clear_probe_trace_event(&tev);
2702 strlist__delete(rawlist);
2703 /* Cleanup cached debuginfo if needed */
2704 debuginfo_cache__exit();
2709 /* List up current perf-probe events */
2710 int show_perf_probe_events(struct strfilter *filter)
2712 int kp_fd, up_fd, ret;
2716 if (probe_conf.cache)
2717 return probe_cache__show_all_caches(filter);
2719 ret = init_probe_symbol_maps(false);
2723 ret = probe_file__open_both(&kp_fd, &up_fd, 0);
2728 ret = __show_perf_probe_events(kp_fd, true, filter);
2729 if (up_fd >= 0 && ret >= 0)
2730 ret = __show_perf_probe_events(up_fd, false, filter);
2735 exit_probe_symbol_maps();
2740 static int get_new_event_name(char *buf, size_t len, const char *base,
2741 struct strlist *namelist, bool ret_event,
2749 nbase = strdup(base);
2753 /* Cut off the dot suffixes (e.g. .const, .isra) and version suffixes */
2754 p = strpbrk(nbase, ".@");
2755 if (p && p != nbase)
2758 /* Try no suffix number */
2759 ret = e_snprintf(buf, len, "%s%s", nbase, ret_event ? "__return" : "");
2761 pr_debug("snprintf() failed: %d\n", ret);
2764 if (!strlist__has_entry(namelist, buf))
2767 if (!allow_suffix) {
2768 pr_warning("Error: event \"%s\" already exists.\n"
2769 " Hint: Remove existing event by 'perf probe -d'\n"
2770 " or force duplicates by 'perf probe -f'\n"
2771 " or set 'force=yes' in BPF source.\n",
2777 /* Try to add suffix */
2778 for (i = 1; i < MAX_EVENT_INDEX; i++) {
2779 ret = e_snprintf(buf, len, "%s_%d", nbase, i);
2781 pr_debug("snprintf() failed: %d\n", ret);
2784 if (!strlist__has_entry(namelist, buf))
2787 if (i == MAX_EVENT_INDEX) {
2788 pr_warning("Too many events are on the same function.\n");
2795 /* Final validation */
2796 if (ret >= 0 && !is_c_func_name(buf)) {
2797 pr_warning("Internal error: \"%s\" is an invalid event name.\n",
2805 /* Warn if the current kernel's uprobe implementation is old */
2806 static void warn_uprobe_event_compat(struct probe_trace_event *tev)
2809 char *buf = synthesize_probe_trace_command(tev);
2810 struct probe_trace_point *tp = &tev->point;
2812 if (tp->ref_ctr_offset && !uprobe_ref_ctr_is_supported()) {
2813 pr_warning("A semaphore is associated with %s:%s and "
2814 "seems your kernel doesn't support it.\n",
2815 tev->group, tev->event);
2818 /* Old uprobe event doesn't support memory dereference */
2819 if (!tev->uprobes || tev->nargs == 0 || !buf)
2822 for (i = 0; i < tev->nargs; i++) {
2823 if (strchr(tev->args[i].value, '@')) {
2824 pr_warning("%s accesses a variable by symbol name, but that is not supported for user application probe.\n",
2825 tev->args[i].value);
2828 if (strglobmatch(tev->args[i].value, "[$+-]*")) {
2829 pr_warning("Please upgrade your kernel to at least 3.14 to have access to feature %s\n",
2830 tev->args[i].value);
2838 /* Set new name from original perf_probe_event and namelist */
2839 static int probe_trace_event__set_name(struct probe_trace_event *tev,
2840 struct perf_probe_event *pev,
2841 struct strlist *namelist,
2844 const char *event, *group;
2848 /* If probe_event or trace_event already have the name, reuse it */
2849 if (pev->event && !pev->sdt)
2851 else if (tev->event)
2854 /* Or generate new one from probe point */
2855 if (pev->point.function &&
2856 (strncmp(pev->point.function, "0x", 2) != 0) &&
2857 !strisglob(pev->point.function))
2858 event = pev->point.function;
2860 event = tev->point.realname;
2862 if (pev->group && !pev->sdt)
2864 else if (tev->group)
2867 group = PERFPROBE_GROUP;
2869 /* Get an unused new event name */
2870 ret = get_new_event_name(buf, 64, event, namelist,
2871 tev->point.retprobe, allow_suffix);
2877 tev->event = strdup(event);
2878 tev->group = strdup(group);
2879 if (tev->event == NULL || tev->group == NULL)
2883 * Add new event name to namelist if multiprobe event is NOT
2884 * supported, since we have to use new event name for following
2885 * probes in that case.
2887 if (!multiprobe_event_is_supported())
2888 strlist__add(namelist, event);
2892 static int __open_probe_file_and_namelist(bool uprobe,
2893 struct strlist **namelist)
2897 fd = probe_file__open(PF_FL_RW | (uprobe ? PF_FL_UPROBE : 0));
2901 /* Get current event names */
2902 *namelist = probe_file__get_namelist(fd);
2904 pr_debug("Failed to get current event list.\n");
2911 static int __add_probe_trace_events(struct perf_probe_event *pev,
2912 struct probe_trace_event *tevs,
2913 int ntevs, bool allow_suffix)
2915 int i, fd[2] = {-1, -1}, up, ret;
2916 struct probe_trace_event *tev = NULL;
2917 struct probe_cache *cache = NULL;
2918 struct strlist *namelist[2] = {NULL, NULL};
2919 struct nscookie nsc;
2921 up = pev->uprobes ? 1 : 0;
2922 fd[up] = __open_probe_file_and_namelist(up, &namelist[up]);
2927 for (i = 0; i < ntevs; i++) {
2929 up = tev->uprobes ? 1 : 0;
2930 if (fd[up] == -1) { /* Open the kprobe/uprobe_events */
2931 fd[up] = __open_probe_file_and_namelist(up,
2936 /* Skip if the symbol is out of .text or blacklisted */
2937 if (!tev->point.symbol && !pev->uprobes)
2940 /* Set new name for tev (and update namelist) */
2941 ret = probe_trace_event__set_name(tev, pev, namelist[up],
2946 nsinfo__mountns_enter(pev->nsi, &nsc);
2947 ret = probe_file__add_event(fd[up], tev);
2948 nsinfo__mountns_exit(&nsc);
2953 * Probes after the first probe which comes from same
2954 * user input are always allowed to add suffix, because
2955 * there might be several addresses corresponding to
2958 allow_suffix = true;
2960 if (ret == -EINVAL && pev->uprobes)
2961 warn_uprobe_event_compat(tev);
2962 if (ret == 0 && probe_conf.cache) {
2963 cache = probe_cache__new(pev->target, pev->nsi);
2965 probe_cache__add_entry(cache, pev, tevs, ntevs) < 0 ||
2966 probe_cache__commit(cache) < 0)
2967 pr_warning("Failed to add event to probe cache\n");
2968 probe_cache__delete(cache);
2972 for (up = 0; up < 2; up++) {
2973 strlist__delete(namelist[up]);
2980 static int find_probe_functions(struct map *map, char *name,
2981 struct symbol **syms)
2985 struct rb_node *tmp;
2986 const char *norm, *ver;
2988 bool cut_version = true;
2990 if (map__load(map) < 0)
2991 return -EACCES; /* Possible permission error to load symbols */
2993 /* If user gives a version, don't cut off the version from symbols */
2994 if (strchr(name, '@'))
2995 cut_version = false;
2997 map__for_each_symbol(map, sym, tmp) {
2998 norm = arch__normalize_symbol_name(sym->name);
3003 /* We don't care about default symbol or not */
3004 ver = strchr(norm, '@');
3006 buf = strndup(norm, ver - norm);
3013 if (strglobmatch(norm, name)) {
3015 if (syms && found < probe_conf.max_probes)
3016 syms[found - 1] = sym;
3025 void __weak arch__fix_tev_from_maps(struct perf_probe_event *pev __maybe_unused,
3026 struct probe_trace_event *tev __maybe_unused,
3027 struct map *map __maybe_unused,
3028 struct symbol *sym __maybe_unused) { }
3031 static void pr_kallsyms_access_error(void)
3033 pr_err("Please ensure you can read the /proc/kallsyms symbol addresses.\n"
3034 "If /proc/sys/kernel/kptr_restrict is '2', you can not read\n"
3035 "kernel symbol addresses even if you are a superuser. Please change\n"
3036 "it to '1'. If kptr_restrict is '1', the superuser can read the\n"
3037 "symbol addresses.\n"
3038 "In that case, please run this command again with sudo.\n");
3042 * Find probe function addresses from map.
3043 * Return an error or the number of found probe_trace_event
3045 static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
3046 struct probe_trace_event **tevs)
3048 struct map *map = NULL;
3049 struct ref_reloc_sym *reloc_sym = NULL;
3051 struct symbol **syms = NULL;
3052 struct probe_trace_event *tev;
3053 struct perf_probe_point *pp = &pev->point;
3054 struct probe_trace_point *tp;
3055 int num_matched_functions;
3056 int ret, i, j, skipped = 0;
3059 map = get_target_map(pev->target, pev->nsi, pev->uprobes);
3065 syms = malloc(sizeof(struct symbol *) * probe_conf.max_probes);
3072 * Load matched symbols: Since the different local symbols may have
3073 * same name but different addresses, this lists all the symbols.
3075 num_matched_functions = find_probe_functions(map, pp->function, syms);
3076 if (num_matched_functions <= 0) {
3077 if (num_matched_functions == -EACCES) {
3078 pr_err("Failed to load symbols from %s\n",
3079 pev->target ?: "/proc/kallsyms");
3081 pr_err("Please ensure the file is not stripped.\n");
3083 pr_kallsyms_access_error();
3085 pr_err("Failed to find symbol %s in %s\n", pp->function,
3086 pev->target ? : "kernel");
3089 } else if (num_matched_functions > probe_conf.max_probes) {
3090 pr_err("Too many functions matched in %s\n",
3091 pev->target ? : "kernel");
3096 /* Note that the symbols in the kmodule are not relocated */
3097 if (!pev->uprobes && !pev->target &&
3098 (!pp->retprobe || kretprobe_offset_is_supported())) {
3099 reloc_sym = kernel_get_ref_reloc_sym(NULL);
3101 pr_warning("Relocated base symbol is not found! "
3102 "Check /proc/sys/kernel/kptr_restrict\n"
3103 "and /proc/sys/kernel/perf_event_paranoid. "
3104 "Or run as privileged perf user.\n\n");
3110 /* Setup result trace-probe-events */
3111 *tevs = zalloc(sizeof(*tev) * num_matched_functions);
3119 for (j = 0; j < num_matched_functions; j++) {
3122 if (sym->type != STT_FUNC)
3125 /* There can be duplicated symbols in the map */
3126 for (i = 0; i < j; i++)
3127 if (sym->start == syms[i]->start) {
3128 pr_debug("Found duplicated symbol %s @ %" PRIx64 "\n",
3129 sym->name, sym->start);
3135 tev = (*tevs) + ret;
3137 if (ret == num_matched_functions) {
3138 pr_warning("Too many symbols are listed. Skip it.\n");
3143 if (pp->offset > sym->end - sym->start) {
3144 pr_warning("Offset %ld is bigger than the size of %s\n",
3145 pp->offset, sym->name);
3149 /* Add one probe point */
3150 tp->address = map__unmap_ip(map, sym->start) + pp->offset;
3152 /* Check the kprobe (not in module) is within .text */
3153 if (!pev->uprobes && !pev->target &&
3154 kprobe_warn_out_range(sym->name, tp->address)) {
3155 tp->symbol = NULL; /* Skip it */
3157 } else if (reloc_sym) {
3158 tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
3159 tp->offset = tp->address - reloc_sym->addr;
3161 tp->symbol = strdup_or_goto(sym->name, nomem_out);
3162 tp->offset = pp->offset;
3164 tp->realname = strdup_or_goto(sym->name, nomem_out);
3166 tp->retprobe = pp->retprobe;
3169 tev->point.module = strdup_or_goto(pev->target,
3172 mod_name = find_module_name(pev->target);
3174 strdup(mod_name ? mod_name : pev->target);
3176 if (!tev->point.module)
3180 tev->uprobes = pev->uprobes;
3181 tev->nargs = pev->nargs;
3183 tev->args = zalloc(sizeof(struct probe_trace_arg) *
3185 if (tev->args == NULL)
3188 for (i = 0; i < tev->nargs; i++) {
3189 if (pev->args[i].name)
3191 strdup_or_goto(pev->args[i].name,
3194 tev->args[i].value = strdup_or_goto(pev->args[i].var,
3196 if (pev->args[i].type)
3198 strdup_or_goto(pev->args[i].type,
3201 arch__fix_tev_from_maps(pev, tev, map, sym);
3203 if (ret == skipped) {
3216 clear_probe_trace_events(*tevs, num_matched_functions);
3221 static int try_to_find_absolute_address(struct perf_probe_event *pev,
3222 struct probe_trace_event **tevs)
3224 struct perf_probe_point *pp = &pev->point;
3225 struct probe_trace_event *tev;
3226 struct probe_trace_point *tp;
3229 if (!(pev->point.function && !strncmp(pev->point.function, "0x", 2)))
3231 if (perf_probe_event_need_dwarf(pev))
3235 * This is 'perf probe /lib/libc.so 0xabcd'. Try to probe at
3238 * Only one tev can be generated by this.
3240 *tevs = zalloc(sizeof(*tev));
3248 * Don't use tp->offset, use address directly, because
3249 * in synthesize_probe_trace_command() address cannot be
3252 tp->address = pev->point.abs_address;
3253 tp->retprobe = pp->retprobe;
3254 tev->uprobes = pev->uprobes;
3258 * Give it a '0x' leading symbol name.
3259 * In __add_probe_trace_events, a NULL symbol is interpreted as
3262 if (asprintf(&tp->symbol, "0x%" PRIx64, tp->address) < 0)
3265 /* For kprobe, check range */
3266 if ((!tev->uprobes) &&
3267 (kprobe_warn_out_range(tev->point.symbol,
3268 tev->point.address))) {
3273 if (asprintf(&tp->realname, "abs_%" PRIx64, tp->address) < 0)
3277 tp->module = strdup(pev->target);
3283 tev->group = strdup(pev->group);
3289 tev->event = strdup(pev->event);
3294 tev->nargs = pev->nargs;
3295 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
3299 for (i = 0; i < tev->nargs; i++)
3300 copy_to_probe_trace_arg(&tev->args[i], &pev->args[i]);
3305 clear_probe_trace_events(*tevs, 1);
3310 /* Concatenate two arrays */
3311 static void *memcat(void *a, size_t sz_a, void *b, size_t sz_b)
3315 ret = malloc(sz_a + sz_b);
3317 memcpy(ret, a, sz_a);
3318 memcpy(ret + sz_a, b, sz_b);
3324 concat_probe_trace_events(struct probe_trace_event **tevs, int *ntevs,
3325 struct probe_trace_event **tevs2, int ntevs2)
3327 struct probe_trace_event *new_tevs;
3337 if (*ntevs + ntevs2 > probe_conf.max_probes)
3340 /* Concatenate the array of probe_trace_event */
3341 new_tevs = memcat(*tevs, (*ntevs) * sizeof(**tevs),
3342 *tevs2, ntevs2 * sizeof(**tevs2));
3352 clear_probe_trace_events(*tevs2, ntevs2);
3359 * Try to find probe_trace_event from given probe caches. Return the number
3360 * of cached events found, if an error occurs return the error.
3362 static int find_cached_events(struct perf_probe_event *pev,
3363 struct probe_trace_event **tevs,
3366 struct probe_cache *cache;
3367 struct probe_cache_entry *entry;
3368 struct probe_trace_event *tmp_tevs = NULL;
3372 cache = probe_cache__new(target, pev->nsi);
3373 /* Return 0 ("not found") if the target has no probe cache. */
3377 for_each_probe_cache_entry(entry, cache) {
3378 /* Skip the cache entry which has no name */
3379 if (!entry->pev.event || !entry->pev.group)
3381 if ((!pev->group || strglobmatch(entry->pev.group, pev->group)) &&
3382 strglobmatch(entry->pev.event, pev->event)) {
3383 ret = probe_cache_entry__get_event(entry, &tmp_tevs);
3385 ret = concat_probe_trace_events(tevs, &ntevs,
3391 probe_cache__delete(cache);
3393 clear_probe_trace_events(*tevs, ntevs);
3397 if (ntevs > 0 && target && target[0] == '/')
3398 pev->uprobes = true;
3404 /* Try to find probe_trace_event from all probe caches */
3405 static int find_cached_events_all(struct perf_probe_event *pev,
3406 struct probe_trace_event **tevs)
3408 struct probe_trace_event *tmp_tevs = NULL;
3409 struct strlist *bidlist;
3410 struct str_node *nd;
3415 /* Get the buildid list of all valid caches */
3416 bidlist = build_id_cache__list_all(true);
3419 pr_debug("Failed to get buildids: %d\n", ret);
3424 strlist__for_each_entry(nd, bidlist) {
3425 pathname = build_id_cache__origname(nd->s);
3426 ret = find_cached_events(pev, &tmp_tevs, pathname);
3427 /* In the case of cnt == 0, we just skip it */
3429 ret = concat_probe_trace_events(tevs, &ntevs,
3435 strlist__delete(bidlist);
3438 clear_probe_trace_events(*tevs, ntevs);
3446 static int find_probe_trace_events_from_cache(struct perf_probe_event *pev,
3447 struct probe_trace_event **tevs)
3449 struct probe_cache *cache;
3450 struct probe_cache_entry *entry;
3451 struct probe_trace_event *tev;
3452 struct str_node *node;
3456 /* For SDT/cached events, we use special search functions */
3458 return find_cached_events_all(pev, tevs);
3460 return find_cached_events(pev, tevs, pev->target);
3462 cache = probe_cache__new(pev->target, pev->nsi);
3466 entry = probe_cache__find(cache, pev);
3468 /* SDT must be in the cache */
3469 ret = pev->sdt ? -ENOENT : 0;
3473 ret = strlist__nr_entries(entry->tevlist);
3474 if (ret > probe_conf.max_probes) {
3475 pr_debug("Too many entries matched in the cache of %s\n",
3476 pev->target ? : "kernel");
3481 *tevs = zalloc(ret * sizeof(*tev));
3488 strlist__for_each_entry(node, entry->tevlist) {
3489 tev = &(*tevs)[i++];
3490 ret = parse_probe_trace_command(node->s, tev);
3493 /* Set the uprobes attribute as same as original */
3494 tev->uprobes = pev->uprobes;
3499 probe_cache__delete(cache);
3503 static int convert_to_probe_trace_events(struct perf_probe_event *pev,
3504 struct probe_trace_event **tevs)
3508 if (!pev->group && !pev->sdt) {
3509 /* Set group name if not given */
3510 if (!pev->uprobes) {
3511 pev->group = strdup(PERFPROBE_GROUP);
3512 ret = pev->group ? 0 : -ENOMEM;
3514 ret = convert_exec_to_group(pev->target, &pev->group);
3516 pr_warning("Failed to make a group name.\n");
3521 ret = try_to_find_absolute_address(pev, tevs);
3525 /* At first, we need to lookup cache entry */
3526 ret = find_probe_trace_events_from_cache(pev, tevs);
3527 if (ret > 0 || pev->sdt) /* SDT can be found only in the cache */
3528 return ret == 0 ? -ENOENT : ret; /* Found in probe cache */
3530 /* Convert perf_probe_event with debuginfo */
3531 ret = try_to_find_probe_trace_events(pev, tevs);
3533 return ret; /* Found in debuginfo or got an error */
3535 return find_probe_trace_events_from_map(pev, tevs);
3538 int convert_perf_probe_events(struct perf_probe_event *pevs, int npevs)
3542 /* Loop 1: convert all events */
3543 for (i = 0; i < npevs; i++) {
3544 /* Init kprobe blacklist if needed */
3545 if (!pevs[i].uprobes)
3546 kprobe_blacklist__init();
3547 /* Convert with or without debuginfo */
3548 ret = convert_to_probe_trace_events(&pevs[i], &pevs[i].tevs);
3551 pevs[i].ntevs = ret;
3553 /* This just release blacklist only if allocated */
3554 kprobe_blacklist__release();
3559 static int show_probe_trace_event(struct probe_trace_event *tev)
3561 char *buf = synthesize_probe_trace_command(tev);
3564 pr_debug("Failed to synthesize probe trace event.\n");
3568 /* Showing definition always go stdout */
3569 printf("%s\n", buf);
3575 int show_probe_trace_events(struct perf_probe_event *pevs, int npevs)
3577 struct strlist *namelist = strlist__new(NULL, NULL);
3578 struct probe_trace_event *tev;
3579 struct perf_probe_event *pev;
3585 for (j = 0; j < npevs && !ret; j++) {
3587 for (i = 0; i < pev->ntevs && !ret; i++) {
3588 tev = &pev->tevs[i];
3589 /* Skip if the symbol is out of .text or blacklisted */
3590 if (!tev->point.symbol && !pev->uprobes)
3593 /* Set new name for tev (and update namelist) */
3594 ret = probe_trace_event__set_name(tev, pev,
3597 ret = show_probe_trace_event(tev);
3600 strlist__delete(namelist);
3605 static int show_bootconfig_event(struct probe_trace_event *tev)
3607 struct probe_trace_point *tp = &tev->point;
3612 if (strbuf_init(&buf, 32) < 0)
3615 err = synthesize_kprobe_trace_def(tp, &buf);
3617 err = synthesize_probe_trace_args(tev, &buf);
3619 ret = strbuf_detach(&buf, NULL);
3620 strbuf_release(&buf);
3623 printf("'%s'", ret);
3630 int show_bootconfig_events(struct perf_probe_event *pevs, int npevs)
3632 struct strlist *namelist = strlist__new(NULL, NULL);
3633 struct probe_trace_event *tev;
3634 struct perf_probe_event *pev;
3635 char *cur_name = NULL;
3641 for (j = 0; j < npevs && !ret; j++) {
3643 if (pev->group && strcmp(pev->group, "probe"))
3644 pr_warning("WARN: Group name %s is ignored\n", pev->group);
3646 pr_warning("ERROR: Bootconfig doesn't support uprobes\n");
3650 for (i = 0; i < pev->ntevs && !ret; i++) {
3651 tev = &pev->tevs[i];
3652 /* Skip if the symbol is out of .text or blacklisted */
3653 if (!tev->point.symbol && !pev->uprobes)
3656 /* Set new name for tev (and update namelist) */
3657 ret = probe_trace_event__set_name(tev, pev,
3662 if (!cur_name || strcmp(cur_name, tev->event)) {
3663 printf("%sftrace.event.kprobes.%s.probe = ",
3664 cur_name ? "\n" : "", tev->event);
3665 cur_name = tev->event;
3668 ret = show_bootconfig_event(tev);
3672 strlist__delete(namelist);
3677 int apply_perf_probe_events(struct perf_probe_event *pevs, int npevs)
3681 /* Loop 2: add all events */
3682 for (i = 0; i < npevs; i++) {
3683 ret = __add_probe_trace_events(&pevs[i], pevs[i].tevs,
3685 probe_conf.force_add);
3692 void cleanup_perf_probe_events(struct perf_probe_event *pevs, int npevs)
3695 struct perf_probe_event *pev;
3697 /* Loop 3: cleanup and free trace events */
3698 for (i = 0; i < npevs; i++) {
3700 for (j = 0; j < pevs[i].ntevs; j++)
3701 clear_probe_trace_event(&pevs[i].tevs[j]);
3702 zfree(&pevs[i].tevs);
3704 nsinfo__zput(pev->nsi);
3705 clear_perf_probe_event(&pevs[i]);
3709 int add_perf_probe_events(struct perf_probe_event *pevs, int npevs)
3713 ret = init_probe_symbol_maps(pevs->uprobes);
3717 ret = convert_perf_probe_events(pevs, npevs);
3719 ret = apply_perf_probe_events(pevs, npevs);
3721 cleanup_perf_probe_events(pevs, npevs);
3723 exit_probe_symbol_maps();
3727 int del_perf_probe_events(struct strfilter *filter)
3729 int ret, ret2, ufd = -1, kfd = -1;
3730 char *str = strfilter__string(filter);
3735 /* Get current event names */
3736 ret = probe_file__open_both(&kfd, &ufd, PF_FL_RW);
3740 ret = probe_file__del_events(kfd, filter);
3741 if (ret < 0 && ret != -ENOENT)
3744 ret2 = probe_file__del_events(ufd, filter);
3745 if (ret2 < 0 && ret2 != -ENOENT) {
3762 int show_available_funcs(const char *target, struct nsinfo *nsi,
3763 struct strfilter *_filter, bool user)
3769 ret = init_probe_symbol_maps(user);
3773 /* Get a symbol map */
3774 map = get_target_map(target, nsi, user);
3776 pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
3780 ret = map__load(map);
3783 char *str = strfilter__string(_filter);
3784 pr_err("Failed to find symbols matched to \"%s\"\n",
3788 pr_err("Failed to load symbols in %s\n",
3789 (target) ? : "kernel");
3792 dso = map__dso(map);
3793 dso__sort_by_name(dso);
3795 /* Show all (filtered) symbols */
3798 for (size_t i = 0; i < dso->symbol_names_len; i++) {
3799 struct symbol *pos = dso->symbol_names[i];
3801 if (strfilter__compare(_filter, pos->name))
3802 printf("%s\n", pos->name);
3806 exit_probe_symbol_maps();
3811 int copy_to_probe_trace_arg(struct probe_trace_arg *tvar,
3812 struct perf_probe_arg *pvar)
3814 tvar->value = strdup(pvar->var);
3815 if (tvar->value == NULL)
3818 tvar->type = strdup(pvar->type);
3819 if (tvar->type == NULL)
3823 tvar->name = strdup(pvar->name);
3824 if (tvar->name == NULL)