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 #define semantic_error(msg ...) pr_err("Semantic error :" msg)
58 int e_snprintf(char *str, size_t size, const char *format, ...)
63 ret = vsnprintf(str, size, format, ap);
70 static struct machine *host_machine;
72 /* Initialize symbol maps and path of vmlinux/modules */
73 int init_probe_symbol_maps(bool user_only)
77 symbol_conf.allow_aliases = true;
78 ret = symbol__init(NULL);
80 pr_debug("Failed to init symbol map.\n");
84 if (host_machine || user_only) /* already initialized */
87 if (symbol_conf.vmlinux_name)
88 pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
90 host_machine = machine__new_host();
92 pr_debug("machine__new_host() failed.\n");
98 pr_warning("Failed to init vmlinux path.\n");
102 void exit_probe_symbol_maps(void)
104 machine__delete(host_machine);
109 static struct ref_reloc_sym *kernel_get_ref_reloc_sym(struct map **pmap)
112 struct map *map = machine__kernel_map(host_machine);
114 if (map__load(map) < 0)
117 kmap = map__kmap(map);
124 return kmap->ref_reloc_sym;
127 static int kernel_get_symbol_address_by_name(const char *name, u64 *addr,
128 bool reloc, bool reladdr)
130 struct ref_reloc_sym *reloc_sym;
134 /* ref_reloc_sym is just a label. Need a special fix*/
135 reloc_sym = kernel_get_ref_reloc_sym(&map);
136 if (reloc_sym && strcmp(name, reloc_sym->name) == 0)
137 *addr = (!map__reloc(map) || reloc) ? reloc_sym->addr :
138 reloc_sym->unrelocated_addr;
140 sym = machine__find_kernel_symbol_by_name(host_machine, name, &map);
143 *addr = map__unmap_ip(map, sym->start) -
144 ((reloc) ? 0 : map__reloc(map)) -
145 ((reladdr) ? map__start(map) : 0);
150 static struct map *kernel_get_module_map(const char *module)
152 struct maps *maps = machine__kernel_maps(host_machine);
153 struct map_rb_node *pos;
155 /* A file path -- this is an offline module */
156 if (module && strchr(module, '/'))
157 return dso__new_map(module);
160 struct map *map = machine__kernel_map(host_machine);
162 return map__get(map);
165 maps__for_each_entry(maps, pos) {
166 /* short_name is "[module]" */
167 struct dso *dso = map__dso(pos->map);
168 const char *short_name = dso->short_name;
169 u16 short_name_len = dso->short_name_len;
171 if (strncmp(short_name + 1, module,
172 short_name_len - 2) == 0 &&
173 module[short_name_len - 2] == '\0') {
174 return map__get(pos->map);
180 struct map *get_target_map(const char *target, struct nsinfo *nsi, bool user)
182 /* Init maps of given executable or kernel */
187 map = dso__new_map(target);
188 dso = map ? map__dso(map) : NULL;
190 mutex_lock(&dso->lock);
191 nsinfo__put(dso->nsinfo);
192 dso->nsinfo = nsinfo__get(nsi);
193 mutex_unlock(&dso->lock);
197 return kernel_get_module_map(target);
201 static int convert_exec_to_group(const char *exec, char **result)
203 char *ptr1, *ptr2, *exec_copy;
207 exec_copy = strdup(exec);
211 ptr1 = basename(exec_copy);
217 for (ptr2 = ptr1; *ptr2 != '\0'; ptr2++) {
218 if (!isalnum(*ptr2) && *ptr2 != '_') {
224 ret = e_snprintf(buf, 64, "%s_%s", PERFPROBE_GROUP, ptr1);
228 *result = strdup(buf);
229 ret = *result ? 0 : -ENOMEM;
236 static void clear_perf_probe_point(struct perf_probe_point *pp)
239 zfree(&pp->function);
240 zfree(&pp->lazy_line);
243 static void clear_probe_trace_events(struct probe_trace_event *tevs, int ntevs)
247 for (i = 0; i < ntevs; i++)
248 clear_probe_trace_event(tevs + i);
251 static bool kprobe_blacklist__listed(u64 address);
252 static bool kprobe_warn_out_range(const char *symbol, u64 address)
257 map = kernel_get_module_map(NULL);
259 ret = address <= map__start(map) || map__end(map) < address;
261 pr_warning("%s is out of .text, skip it.\n", symbol);
264 if (!ret && kprobe_blacklist__listed(address)) {
265 pr_warning("%s is blacklisted function, skip it.\n", symbol);
273 * @module can be module name of module file path. In case of path,
274 * inspect elf and find out what is actual module name.
275 * Caller has to free mod_name after using it.
277 static char *find_module_name(const char *module)
285 char *mod_name = NULL;
288 fd = open(module, O_RDONLY);
292 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
296 if (gelf_getehdr(elf, &ehdr) == NULL)
299 sec = elf_section_by_name(elf, &ehdr, &shdr,
300 ".gnu.linkonce.this_module", NULL);
304 data = elf_getdata(sec, NULL);
305 if (!data || !data->d_buf)
310 * '.gnu.linkonce.this_module' section of kernel module elf directly
311 * maps to 'struct module' from linux/module.h. This section contains
312 * actual module name which will be used by kernel after loading it.
313 * But, we cannot use 'struct module' here since linux/module.h is not
314 * exposed to user-space. Offset of 'name' has remained same from long
315 * time, so hardcoding it here.
317 if (ehdr.e_ident[EI_CLASS] == ELFCLASS32)
319 else /* expect ELFCLASS64 by default */
322 mod_name = strdup((char *)data->d_buf + name_offset);
331 #ifdef HAVE_DWARF_SUPPORT
333 static int kernel_get_module_dso(const char *module, struct dso **pdso)
337 const char *vmlinux_name;
341 char module_name[128];
343 snprintf(module_name, sizeof(module_name), "[%s]", module);
344 map = maps__find_by_name(machine__kernel_maps(host_machine), module_name);
349 pr_debug("Failed to find module %s.\n", module);
353 map = machine__kernel_map(host_machine);
355 if (!dso->has_build_id)
356 dso__read_running_kernel_build_id(dso, host_machine);
358 vmlinux_name = symbol_conf.vmlinux_name;
361 ret = dso__load_vmlinux(dso, map, vmlinux_name, false);
363 ret = dso__load_vmlinux_path(dso, map);
370 * Some binaries like glibc have special symbols which are on the symbol
371 * table, but not in the debuginfo. If we can find the address of the
372 * symbol from map, we can translate the address back to the probe point.
374 static int find_alternative_probe_point(struct debuginfo *dinfo,
375 struct perf_probe_point *pp,
376 struct perf_probe_point *result,
377 const char *target, struct nsinfo *nsi,
380 struct map *map = NULL;
386 /* This can work only for function-name based one */
387 if (!pp->function || pp->file)
390 map = get_target_map(target, nsi, uprobes);
394 /* Find the address of given function */
395 map__for_each_symbol_by_name(map, pp->function, sym, idx) {
397 address = sym->start;
398 if (sym->type == STT_GNU_IFUNC)
399 pr_warning("Warning: The probe function (%s) is a GNU indirect function.\n"
400 "Consider identifying the final function used at run time and set the probe directly on that.\n",
403 address = map__unmap_ip(map, sym->start) - map__reloc(map);
410 pr_debug("Symbol %s address found : %" PRIx64 "\n",
411 pp->function, address);
413 ret = debuginfo__find_probe_point(dinfo, address, result);
415 ret = (!ret) ? -ENOENT : ret;
417 result->offset += pp->offset;
418 result->line += pp->line;
419 result->retprobe = pp->retprobe;
429 static int get_alternative_probe_event(struct debuginfo *dinfo,
430 struct perf_probe_event *pev,
431 struct perf_probe_point *tmp)
435 memcpy(tmp, &pev->point, sizeof(*tmp));
436 memset(&pev->point, 0, sizeof(pev->point));
437 ret = find_alternative_probe_point(dinfo, tmp, &pev->point, pev->target,
438 pev->nsi, pev->uprobes);
440 memcpy(&pev->point, tmp, sizeof(*tmp));
445 static int get_alternative_line_range(struct debuginfo *dinfo,
446 struct line_range *lr,
447 const char *target, bool user)
449 struct perf_probe_point pp = { .function = lr->function,
452 struct perf_probe_point result;
455 memset(&result, 0, sizeof(result));
457 if (lr->end != INT_MAX)
458 len = lr->end - lr->start;
459 ret = find_alternative_probe_point(dinfo, &pp, &result,
462 lr->function = result.function;
463 lr->file = result.file;
464 lr->start = result.line;
465 if (lr->end != INT_MAX)
466 lr->end = lr->start + len;
467 clear_perf_probe_point(&pp);
472 #ifdef HAVE_DEBUGINFOD_SUPPORT
473 static struct debuginfo *open_from_debuginfod(struct dso *dso, struct nsinfo *nsi,
476 debuginfod_client *c = debuginfod_begin();
477 char sbuild_id[SBUILD_ID_SIZE + 1];
478 struct debuginfo *ret = NULL;
486 build_id__sprintf(&dso->bid, sbuild_id);
487 fd = debuginfod_find_debuginfo(c, (const unsigned char *)sbuild_id,
494 pr_debug("Failed to find debuginfo in debuginfod.\n");
498 pr_debug("Load debuginfo from debuginfod (%s)\n", path);
500 nsinfo__mountns_enter(nsi, &nsc);
501 ret = debuginfo__new((const char *)path);
502 nsinfo__mountns_exit(&nsc);
507 struct debuginfo *open_from_debuginfod(struct dso *dso __maybe_unused,
508 struct nsinfo *nsi __maybe_unused,
509 bool silent __maybe_unused)
515 /* Open new debuginfo of given module */
516 static struct debuginfo *open_debuginfo(const char *module, struct nsinfo *nsi,
519 const char *path = module;
520 char reason[STRERR_BUFSIZE];
521 struct debuginfo *ret = NULL;
522 struct dso *dso = NULL;
526 if (!module || !strchr(module, '/')) {
527 err = kernel_get_module_dso(module, &dso);
529 if (!dso || dso->load_errno == 0) {
530 if (!str_error_r(-err, reason, STRERR_BUFSIZE))
531 strcpy(reason, "(unknown)");
533 dso__strerror_load(dso, reason, STRERR_BUFSIZE);
535 ret = open_from_debuginfod(dso, nsi, silent);
540 pr_err("Module %s is not loaded, please specify its full path name.\n", module);
542 pr_err("Failed to find the path for the kernel: %s\n", reason);
546 path = dso->long_name;
548 nsinfo__mountns_enter(nsi, &nsc);
549 ret = debuginfo__new(path);
550 if (!ret && !silent) {
551 pr_warning("The %s file has no debug information.\n", path);
552 if (!module || !strtailcmp(path, ".ko"))
553 pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, ");
555 pr_warning("Rebuild with -g, ");
556 pr_warning("or install an appropriate debuginfo package.\n");
558 nsinfo__mountns_exit(&nsc);
562 /* For caching the last debuginfo */
563 static struct debuginfo *debuginfo_cache;
564 static char *debuginfo_cache_path;
566 static struct debuginfo *debuginfo_cache__open(const char *module, bool silent)
568 const char *path = module;
570 /* If the module is NULL, it should be the kernel. */
574 if (debuginfo_cache_path && !strcmp(debuginfo_cache_path, path))
577 /* Copy module path */
578 free(debuginfo_cache_path);
579 debuginfo_cache_path = strdup(path);
580 if (!debuginfo_cache_path) {
581 debuginfo__delete(debuginfo_cache);
582 debuginfo_cache = NULL;
586 debuginfo_cache = open_debuginfo(module, NULL, silent);
587 if (!debuginfo_cache)
588 zfree(&debuginfo_cache_path);
590 return debuginfo_cache;
593 static void debuginfo_cache__exit(void)
595 debuginfo__delete(debuginfo_cache);
596 debuginfo_cache = NULL;
597 zfree(&debuginfo_cache_path);
601 static int get_text_start_address(const char *exec, u64 *address,
607 int fd, ret = -ENOENT;
610 nsinfo__mountns_enter(nsi, &nsc);
611 fd = open(exec, O_RDONLY);
612 nsinfo__mountns_exit(&nsc);
616 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
622 if (gelf_getehdr(elf, &ehdr) == NULL)
625 if (!elf_section_by_name(elf, &ehdr, &shdr, ".text", NULL))
628 *address = shdr.sh_addr - shdr.sh_offset;
639 * Convert trace point to probe point with debuginfo
641 static int find_perf_probe_point_from_dwarf(struct probe_trace_point *tp,
642 struct perf_probe_point *pp,
645 struct debuginfo *dinfo = NULL;
647 u64 addr = tp->address;
650 /* convert the address to dwarf address */
656 ret = get_text_start_address(tp->module, &stext, NULL);
660 } else if (tp->symbol) {
661 /* If the module is given, this returns relative address */
662 ret = kernel_get_symbol_address_by_name(tp->symbol, &addr,
663 false, !!tp->module);
669 pr_debug("try to find information at %" PRIx64 " in %s\n", addr,
670 tp->module ? : "kernel");
672 dinfo = debuginfo_cache__open(tp->module, verbose <= 0);
674 ret = debuginfo__find_probe_point(dinfo, addr, pp);
679 pp->retprobe = tp->retprobe;
683 pr_debug("Failed to find corresponding probes from debuginfo.\n");
684 return ret ? : -ENOENT;
687 /* Adjust symbol name and address */
688 static int post_process_probe_trace_point(struct probe_trace_point *tp,
689 struct map *map, u64 offs)
692 u64 addr = tp->address - offs;
694 sym = map__find_symbol(map, addr);
697 * If the address is in the inittext section, map can not
698 * find it. Ignore it if we are probing offline kernel.
700 return (symbol_conf.ignore_vmlinux_buildid) ? 0 : -ENOENT;
703 if (strcmp(sym->name, tp->symbol)) {
704 /* If we have no realname, use symbol for it */
706 tp->realname = tp->symbol;
709 tp->symbol = strdup(sym->name);
713 tp->offset = addr - sym->start;
720 * Rename DWARF symbols to ELF symbols -- gcc sometimes optimizes functions
721 * and generate new symbols with suffixes such as .constprop.N or .isra.N
722 * etc. Since those symbols are not recorded in DWARF, we have to find
723 * correct generated symbols from offline ELF binary.
724 * For online kernel or uprobes we don't need this because those are
725 * rebased on _text, or already a section relative address.
728 post_process_offline_probe_trace_events(struct probe_trace_event *tevs,
729 int ntevs, const char *pathname)
735 /* Prepare a map for offline binary */
736 map = dso__new_map(pathname);
737 if (!map || get_text_start_address(pathname, &stext, NULL) < 0) {
738 pr_warning("Failed to get ELF symbols for %s\n", pathname);
742 for (i = 0; i < ntevs; i++) {
743 ret = post_process_probe_trace_point(&tevs[i].point,
753 static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs,
754 int ntevs, const char *exec,
763 ret = get_text_start_address(exec, &stext, nsi);
767 for (i = 0; i < ntevs && ret >= 0; i++) {
768 /* point.address is the address of point.symbol + point.offset */
769 tevs[i].point.address -= stext;
770 tevs[i].point.module = strdup(exec);
771 if (!tevs[i].point.module) {
775 tevs[i].uprobes = true;
782 post_process_module_probe_trace_events(struct probe_trace_event *tevs,
783 int ntevs, const char *module,
784 struct debuginfo *dinfo)
786 Dwarf_Addr text_offs = 0;
788 char *mod_name = NULL;
794 map = get_target_map(module, NULL, false);
795 if (!map || debuginfo__get_text_offset(dinfo, &text_offs, true) < 0) {
796 pr_warning("Failed to get ELF symbols for %s\n", module);
800 mod_name = find_module_name(module);
801 for (i = 0; i < ntevs; i++) {
802 ret = post_process_probe_trace_point(&tevs[i].point,
806 tevs[i].point.module =
807 strdup(mod_name ? mod_name : module);
808 if (!tevs[i].point.module) {
821 post_process_kernel_probe_trace_events(struct probe_trace_event *tevs,
824 struct ref_reloc_sym *reloc_sym;
829 /* Skip post process if the target is an offline kernel */
830 if (symbol_conf.ignore_vmlinux_buildid)
831 return post_process_offline_probe_trace_events(tevs, ntevs,
832 symbol_conf.vmlinux_name);
834 reloc_sym = kernel_get_ref_reloc_sym(&map);
836 pr_warning("Relocated base symbol is not found! "
837 "Check /proc/sys/kernel/kptr_restrict\n"
838 "and /proc/sys/kernel/perf_event_paranoid. "
839 "Or run as privileged perf user.\n\n");
843 for (i = 0; i < ntevs; i++) {
844 if (!tevs[i].point.address)
846 if (tevs[i].point.retprobe && !kretprobe_offset_is_supported())
849 * If we found a wrong one, mark it by NULL symbol.
850 * Since addresses in debuginfo is same as objdump, we need
851 * to convert it to addresses on memory.
853 if (kprobe_warn_out_range(tevs[i].point.symbol,
854 map__objdump_2mem(map, tevs[i].point.address))) {
858 tmp = strdup(reloc_sym->name);
862 /* If we have no realname, use symbol for it */
863 if (!tevs[i].point.realname)
864 tevs[i].point.realname = tevs[i].point.symbol;
866 free(tevs[i].point.symbol);
867 tevs[i].point.symbol = tmp;
868 tevs[i].point.offset = tevs[i].point.address -
869 (map__reloc(map) ? reloc_sym->unrelocated_addr :
876 arch__post_process_probe_trace_events(struct perf_probe_event *pev __maybe_unused,
877 int ntevs __maybe_unused)
881 /* Post processing the probe events */
882 static int post_process_probe_trace_events(struct perf_probe_event *pev,
883 struct probe_trace_event *tevs,
884 int ntevs, const char *module,
885 bool uprobe, struct debuginfo *dinfo)
890 ret = add_exec_to_probe_trace_events(tevs, ntevs, module,
893 /* Currently ref_reloc_sym based probe is not for drivers */
894 ret = post_process_module_probe_trace_events(tevs, ntevs,
897 ret = post_process_kernel_probe_trace_events(tevs, ntevs);
900 arch__post_process_probe_trace_events(pev, ntevs);
905 /* Try to find perf_probe_event with debuginfo */
906 static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
907 struct probe_trace_event **tevs)
909 bool need_dwarf = perf_probe_event_need_dwarf(pev);
910 struct perf_probe_point tmp;
911 struct debuginfo *dinfo;
914 /* Workaround for gcc #98776 issue.
915 * Perf failed to add kretprobe event with debuginfo of vmlinux which is
916 * compiled by gcc with -fpatchable-function-entry option enabled. The
917 * same issue with kernel module. The retprobe doesn`t need debuginfo.
918 * This workaround solution use map to query the probe function address
919 * for retprobe event.
921 if (pev->point.retprobe)
924 dinfo = open_debuginfo(pev->target, pev->nsi, !need_dwarf);
928 pr_debug("Could not open debuginfo. Try to use symbols.\n");
932 pr_debug("Try to find probe point from debuginfo.\n");
933 /* Searching trace events corresponding to a probe event */
934 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
936 if (ntevs == 0) { /* Not found, retry with an alternative */
937 ret = get_alternative_probe_event(dinfo, pev, &tmp);
939 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
941 * Write back to the original probe_event for
942 * setting appropriate (user given) event name
944 clear_perf_probe_point(&pev->point);
945 memcpy(&pev->point, &tmp, sizeof(tmp));
949 if (ntevs > 0) { /* Succeeded to find trace events */
950 pr_debug("Found %d probe_trace_events.\n", ntevs);
951 ret = post_process_probe_trace_events(pev, *tevs, ntevs,
952 pev->target, pev->uprobes, dinfo);
953 if (ret < 0 || ret == ntevs) {
954 pr_debug("Post processing failed or all events are skipped. (%d)\n", ret);
955 clear_probe_trace_events(*tevs, ntevs);
961 debuginfo__delete(dinfo);
963 if (ntevs == 0) { /* No error but failed to find probe point. */
964 char *probe_point = synthesize_perf_probe_point(&pev->point);
965 pr_warning("Probe point '%s' not found.\n", probe_point);
968 } else if (ntevs < 0) {
969 /* Error path : ntevs < 0 */
970 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
972 pr_warning("Warning: No dwarf info found in the vmlinux - "
973 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
975 pr_debug("Trying to use symbols.\n");
982 #define LINEBUF_SIZE 256
983 #define NR_ADDITIONAL_LINES 2
985 static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
987 char buf[LINEBUF_SIZE], sbuf[STRERR_BUFSIZE];
988 const char *color = show_num ? "" : PERF_COLOR_BLUE;
989 const char *prefix = NULL;
992 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
997 prefix = show_num ? "%7d " : " ";
998 color_fprintf(stdout, color, prefix, l);
1000 color_fprintf(stdout, color, "%s", buf);
1002 } while (strchr(buf, '\n') == NULL);
1007 pr_warning("File read error: %s\n",
1008 str_error_r(errno, sbuf, sizeof(sbuf)));
1014 static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
1016 int rv = __show_one_line(fp, l, skip, show_num);
1018 pr_warning("Source file is shorter than expected.\n");
1024 #define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
1025 #define show_one_line(f,l) _show_one_line(f,l,false,false)
1026 #define skip_one_line(f,l) _show_one_line(f,l,true,false)
1027 #define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
1030 * Show line-range always requires debuginfo to find source file and
1033 static int __show_line_range(struct line_range *lr, const char *module,
1036 struct build_id bid;
1038 struct int_node *ln;
1039 struct debuginfo *dinfo;
1043 char sbuf[STRERR_BUFSIZE];
1044 char sbuild_id[SBUILD_ID_SIZE] = "";
1046 /* Search a line range */
1047 dinfo = open_debuginfo(module, NULL, false);
1051 ret = debuginfo__find_line_range(dinfo, lr);
1052 if (!ret) { /* Not found, retry with an alternative */
1053 ret = get_alternative_line_range(dinfo, lr, module, user);
1055 ret = debuginfo__find_line_range(dinfo, lr);
1057 if (dinfo->build_id) {
1058 build_id__init(&bid, dinfo->build_id, BUILD_ID_SIZE);
1059 build_id__sprintf(&bid, sbuild_id);
1061 debuginfo__delete(dinfo);
1062 if (ret == 0 || ret == -ENOENT) {
1063 pr_warning("Specified source line is not found.\n");
1065 } else if (ret < 0) {
1066 pr_warning("Debuginfo analysis failed.\n");
1070 /* Convert source file path */
1072 ret = find_source_path(tmp, sbuild_id, lr->comp_dir, &lr->path);
1074 /* Free old path when new path is assigned */
1075 if (tmp != lr->path)
1079 pr_warning("Failed to find source file path.\n");
1086 fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
1087 lr->start - lr->offset);
1089 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
1091 fp = fopen(lr->path, "r");
1093 pr_warning("Failed to open %s: %s\n", lr->path,
1094 str_error_r(errno, sbuf, sizeof(sbuf)));
1097 /* Skip to starting line number */
1098 while (l < lr->start) {
1099 ret = skip_one_line(fp, l++);
1104 intlist__for_each_entry(ln, lr->line_list) {
1105 for (; ln->i > (unsigned long)l; l++) {
1106 ret = show_one_line(fp, l - lr->offset);
1110 ret = show_one_line_with_num(fp, l++ - lr->offset);
1115 if (lr->end == INT_MAX)
1116 lr->end = l + NR_ADDITIONAL_LINES;
1117 while (l <= lr->end) {
1118 ret = show_one_line_or_eof(fp, l++ - lr->offset);
1127 int show_line_range(struct line_range *lr, const char *module,
1128 struct nsinfo *nsi, bool user)
1131 struct nscookie nsc;
1133 ret = init_probe_symbol_maps(user);
1136 nsinfo__mountns_enter(nsi, &nsc);
1137 ret = __show_line_range(lr, module, user);
1138 nsinfo__mountns_exit(&nsc);
1139 exit_probe_symbol_maps();
1144 static int show_available_vars_at(struct debuginfo *dinfo,
1145 struct perf_probe_event *pev,
1146 struct strfilter *_filter)
1150 struct str_node *node;
1151 struct variable_list *vls = NULL, *vl;
1152 struct perf_probe_point tmp;
1155 buf = synthesize_perf_probe_point(&pev->point);
1158 pr_debug("Searching variables at %s\n", buf);
1160 ret = debuginfo__find_available_vars_at(dinfo, pev, &vls);
1161 if (!ret) { /* Not found, retry with an alternative */
1162 ret = get_alternative_probe_event(dinfo, pev, &tmp);
1164 ret = debuginfo__find_available_vars_at(dinfo, pev,
1166 /* Release the old probe_point */
1167 clear_perf_probe_point(&tmp);
1171 if (ret == 0 || ret == -ENOENT) {
1172 pr_err("Failed to find the address of %s\n", buf);
1175 pr_warning("Debuginfo analysis failed.\n");
1179 /* Some variables are found */
1180 fprintf(stdout, "Available variables at %s\n", buf);
1181 for (i = 0; i < ret; i++) {
1184 * A probe point might be converted to
1185 * several trace points.
1187 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
1189 zfree(&vl->point.symbol);
1192 strlist__for_each_entry(node, vl->vars) {
1193 var = strchr(node->s, '\t') + 1;
1194 if (strfilter__compare(_filter, var)) {
1195 fprintf(stdout, "\t\t%s\n", node->s);
1199 strlist__delete(vl->vars);
1202 fprintf(stdout, "\t\t(No matched variables)\n");
1210 /* Show available variables on given probe point */
1211 int show_available_vars(struct perf_probe_event *pevs, int npevs,
1212 struct strfilter *_filter)
1215 struct debuginfo *dinfo;
1217 ret = init_probe_symbol_maps(pevs->uprobes);
1221 dinfo = open_debuginfo(pevs->target, pevs->nsi, false);
1229 for (i = 0; i < npevs && ret >= 0; i++)
1230 ret = show_available_vars_at(dinfo, &pevs[i], _filter);
1232 debuginfo__delete(dinfo);
1234 exit_probe_symbol_maps();
1238 #else /* !HAVE_DWARF_SUPPORT */
1240 static void debuginfo_cache__exit(void)
1245 find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused,
1246 struct perf_probe_point *pp __maybe_unused,
1247 bool is_kprobe __maybe_unused)
1252 static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
1253 struct probe_trace_event **tevs __maybe_unused)
1255 if (perf_probe_event_need_dwarf(pev)) {
1256 pr_warning("Debuginfo-analysis is not supported.\n");
1263 int show_line_range(struct line_range *lr __maybe_unused,
1264 const char *module __maybe_unused,
1265 struct nsinfo *nsi __maybe_unused,
1266 bool user __maybe_unused)
1268 pr_warning("Debuginfo-analysis is not supported.\n");
1272 int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
1273 int npevs __maybe_unused,
1274 struct strfilter *filter __maybe_unused)
1276 pr_warning("Debuginfo-analysis is not supported.\n");
1281 void line_range__clear(struct line_range *lr)
1283 zfree(&lr->function);
1286 zfree(&lr->comp_dir);
1287 intlist__delete(lr->line_list);
1290 int line_range__init(struct line_range *lr)
1292 memset(lr, 0, sizeof(*lr));
1293 lr->line_list = intlist__new(NULL);
1300 static int parse_line_num(char **ptr, int *val, const char *what)
1302 const char *start = *ptr;
1305 *val = strtol(*ptr, ptr, 0);
1306 if (errno || *ptr == start) {
1307 semantic_error("'%s' is not a valid number.\n", what);
1313 /* Check the name is good for event, group or function */
1314 static bool is_c_func_name(const char *name)
1316 if (!isalpha(*name) && *name != '_')
1318 while (*++name != '\0') {
1319 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
1326 * Stuff 'lr' according to the line range described by 'arg'.
1327 * The line range syntax is described by:
1329 * SRC[:SLN[+NUM|-ELN]]
1330 * FNC[@SRC][:SLN[+NUM|-ELN]]
1332 int parse_line_range_desc(const char *arg, struct line_range *lr)
1334 char *range, *file, *name = strdup(arg);
1343 range = strchr(name, ':');
1347 err = parse_line_num(&range, &lr->start, "start line");
1351 if (*range == '+' || *range == '-') {
1352 const char c = *range++;
1354 err = parse_line_num(&range, &lr->end, "end line");
1359 lr->end += lr->start;
1361 * Adjust the number of lines here.
1362 * If the number of lines == 1, the
1363 * end of line should be equal to
1364 * the start of line.
1370 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
1373 if (lr->start > lr->end) {
1374 semantic_error("Start line must be smaller"
1375 " than end line.\n");
1378 if (*range != '\0') {
1379 semantic_error("Tailing with invalid str '%s'.\n", range);
1384 file = strchr(name, '@');
1387 lr->file = strdup(++file);
1388 if (lr->file == NULL) {
1392 lr->function = name;
1393 } else if (strchr(name, '/') || strchr(name, '.'))
1395 else if (is_c_func_name(name))/* We reuse it for checking funcname */
1396 lr->function = name;
1397 else { /* Invalid name */
1398 semantic_error("'%s' is not a valid function name.\n", name);
1409 static int parse_perf_probe_event_name(char **arg, struct perf_probe_event *pev)
1413 ptr = strpbrk_esc(*arg, ":");
1416 if (!pev->sdt && !is_c_func_name(*arg))
1418 pev->group = strdup_esc(*arg);
1425 pev->event = strdup_esc(*arg);
1426 if (pev->event == NULL)
1429 if (!pev->sdt && !is_c_func_name(pev->event)) {
1433 semantic_error("%s is bad for event name -it must "
1434 "follow C symbol-naming rule.\n", *arg);
1440 /* Parse probepoint definition. */
1441 static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
1443 struct perf_probe_point *pp = &pev->point;
1446 bool file_spec = false;
1451 * perf probe [GRP:][EVENT=]SRC[:LN|;PTN]
1452 * perf probe [GRP:][EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
1453 * perf probe %[GRP:]SDT_EVENT
1458 if (is_sdt_event(arg)) {
1464 ptr = strpbrk_esc(arg, ";=@+%");
1468 semantic_error("%s must be an SDT name.\n",
1472 /* This must be a target file name or build id */
1473 tmp = build_id_cache__complement(ptr + 1);
1475 pev->target = build_id_cache__origname(tmp);
1478 pev->target = strdup_esc(ptr + 1);
1483 ret = parse_perf_probe_event_name(&arg, pev);
1485 if (asprintf(&pev->point.function, "%%%s", pev->event) < 0)
1491 if (ptr && *ptr == '=') { /* Event name */
1494 ret = parse_perf_probe_event_name(&arg, pev);
1502 * Check arg is function or file name and copy it.
1504 * We consider arg to be a file spec if and only if it satisfies
1505 * all of the below criteria::
1506 * - it does not include any of "+@%",
1507 * - it includes one of ":;", and
1508 * - it has a period '.' in the name.
1510 * Otherwise, we consider arg to be a function specification.
1512 if (!strpbrk_esc(arg, "+@%")) {
1513 ptr = strpbrk_esc(arg, ";:");
1514 /* This is a file spec if it includes a '.' before ; or : */
1515 if (ptr && memchr(arg, '.', ptr - arg))
1519 ptr = strpbrk_esc(arg, ";:+@%");
1528 tmp = strdup_esc(arg);
1539 * Keep pp->function even if this is absolute address,
1540 * so it can mark whether abs_address is valid.
1541 * Which make 'perf probe lib.bin 0x0' possible.
1543 * Note that checking length of tmp is not needed
1544 * because when we access tmp[1] we know tmp[0] is '0',
1545 * so tmp[1] should always valid (but could be '\0').
1547 if (tmp && !strncmp(tmp, "0x", 2)) {
1548 pp->abs_address = strtoull(pp->function, &tmp, 0);
1550 semantic_error("Invalid absolute address.\n");
1556 /* Parse other options */
1560 if (c == ';') { /* Lazy pattern must be the last part */
1561 pp->lazy_line = strdup(arg); /* let leave escapes */
1562 if (pp->lazy_line == NULL)
1566 ptr = strpbrk_esc(arg, ";:+@%");
1572 case ':': /* Line number */
1573 pp->line = strtoul(arg, &tmp, 0);
1575 semantic_error("There is non-digit char"
1576 " in line number.\n");
1580 case '+': /* Byte offset from a symbol */
1581 pp->offset = strtoul(arg, &tmp, 0);
1583 semantic_error("There is non-digit character"
1588 case '@': /* File name */
1590 semantic_error("SRC@SRC is not allowed.\n");
1593 pp->file = strdup_esc(arg);
1594 if (pp->file == NULL)
1597 case '%': /* Probe places */
1598 if (strcmp(arg, "return") == 0) {
1600 } else { /* Others not supported yet */
1601 semantic_error("%%%s is not supported.\n", arg);
1605 default: /* Buggy case */
1606 pr_err("This program has a bug at %s:%d.\n",
1607 __FILE__, __LINE__);
1613 /* Exclusion check */
1614 if (pp->lazy_line && pp->line) {
1615 semantic_error("Lazy pattern can't be used with"
1620 if (pp->lazy_line && pp->offset) {
1621 semantic_error("Lazy pattern can't be used with offset.\n");
1625 if (pp->line && pp->offset) {
1626 semantic_error("Offset can't be used with line number.\n");
1630 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
1631 semantic_error("File always requires line number or "
1636 if (pp->offset && !pp->function) {
1637 semantic_error("Offset requires an entry function.\n");
1641 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
1642 semantic_error("Offset/Line/Lazy pattern can't be used with "
1647 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
1648 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
1653 /* Parse perf-probe event argument */
1654 static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
1656 char *tmp, *goodname;
1657 struct perf_probe_arg_field **fieldp;
1659 pr_debug("parsing arg: %s into ", str);
1661 tmp = strchr(str, '=');
1663 arg->name = strndup(str, tmp - str);
1664 if (arg->name == NULL)
1666 pr_debug("name:%s ", arg->name);
1670 tmp = strchr(str, '@');
1671 if (tmp && tmp != str && !strcmp(tmp + 1, "user")) { /* user attr */
1672 if (!user_access_is_supported()) {
1673 semantic_error("ftrace does not support user access\n");
1677 arg->user_access = true;
1678 pr_debug("user_access ");
1681 tmp = strchr(str, ':');
1682 if (tmp) { /* Type setting */
1684 arg->type = strdup(tmp + 1);
1685 if (arg->type == NULL)
1687 pr_debug("type:%s ", arg->type);
1690 tmp = strpbrk(str, "-.[");
1691 if (!is_c_varname(str) || !tmp) {
1692 /* A variable, register, symbol or special value */
1693 arg->var = strdup(str);
1694 if (arg->var == NULL)
1696 pr_debug("%s\n", arg->var);
1700 /* Structure fields or array element */
1701 arg->var = strndup(str, tmp - str);
1702 if (arg->var == NULL)
1704 goodname = arg->var;
1705 pr_debug("%s, ", arg->var);
1706 fieldp = &arg->field;
1709 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
1710 if (*fieldp == NULL)
1712 if (*tmp == '[') { /* Array */
1714 (*fieldp)->index = strtol(str + 1, &tmp, 0);
1715 (*fieldp)->ref = true;
1716 if (*tmp != ']' || tmp == str + 1) {
1717 semantic_error("Array index must be a"
1724 } else { /* Structure */
1727 (*fieldp)->ref = false;
1728 } else if (tmp[1] == '>') {
1730 (*fieldp)->ref = true;
1732 semantic_error("Argument parse error: %s\n",
1736 tmp = strpbrk(str, "-.[");
1739 (*fieldp)->name = strndup(str, tmp - str);
1740 if ((*fieldp)->name == NULL)
1743 goodname = (*fieldp)->name;
1744 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
1745 fieldp = &(*fieldp)->next;
1748 (*fieldp)->name = strdup(str);
1749 if ((*fieldp)->name == NULL)
1752 goodname = (*fieldp)->name;
1753 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
1755 /* If no name is specified, set the last field name (not array index)*/
1757 arg->name = strdup(goodname);
1758 if (arg->name == NULL)
1764 /* Parse perf-probe event command */
1765 int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
1768 int argc, i, ret = 0;
1770 argv = argv_split(cmd, &argc);
1772 pr_debug("Failed to split arguments.\n");
1775 if (argc - 1 > MAX_PROBE_ARGS) {
1776 semantic_error("Too many probe arguments (%d).\n", argc - 1);
1780 /* Parse probe point */
1781 ret = parse_perf_probe_point(argv[0], pev);
1785 /* Generate event name if needed */
1786 if (!pev->event && pev->point.function && pev->point.line
1787 && !pev->point.lazy_line && !pev->point.offset) {
1788 if (asprintf(&pev->event, "%s_L%d", pev->point.function,
1789 pev->point.line) < 0) {
1795 /* Copy arguments and ensure return probe has no C argument */
1796 pev->nargs = argc - 1;
1797 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1798 if (pev->args == NULL) {
1802 for (i = 0; i < pev->nargs && ret >= 0; i++) {
1803 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1805 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
1806 semantic_error("You can't specify local variable for"
1817 /* Returns true if *any* ARG is either C variable, $params or $vars. */
1818 bool perf_probe_with_var(struct perf_probe_event *pev)
1822 for (i = 0; i < pev->nargs; i++)
1823 if (is_c_varname(pev->args[i].var) ||
1824 !strcmp(pev->args[i].var, PROBE_ARG_PARAMS) ||
1825 !strcmp(pev->args[i].var, PROBE_ARG_VARS))
1830 /* Return true if this perf_probe_event requires debuginfo */
1831 bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
1833 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1836 if (perf_probe_with_var(pev))
1842 /* Parse probe_events event into struct probe_point */
1843 int parse_probe_trace_command(const char *cmd, struct probe_trace_event *tev)
1845 struct probe_trace_point *tp = &tev->point;
1848 char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
1852 pr_debug("Parsing probe_events: %s\n", cmd);
1853 argv = argv_split(cmd, &argc);
1855 pr_debug("Failed to split arguments.\n");
1859 semantic_error("Too few probe arguments.\n");
1864 /* Scan event and group name. */
1865 argv0_str = strdup(argv[0]);
1866 if (argv0_str == NULL) {
1870 fmt1_str = strtok_r(argv0_str, ":", &fmt);
1871 fmt2_str = strtok_r(NULL, "/", &fmt);
1872 fmt3_str = strtok_r(NULL, " \t", &fmt);
1873 if (fmt1_str == NULL || fmt2_str == NULL || fmt3_str == NULL) {
1874 semantic_error("Failed to parse event name: %s\n", argv[0]);
1879 tev->group = strdup(fmt2_str);
1880 tev->event = strdup(fmt3_str);
1881 if (tev->group == NULL || tev->event == NULL) {
1885 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
1887 tp->retprobe = (pr == 'r');
1889 /* Scan module name(if there), function name and offset */
1890 p = strchr(argv[1], ':');
1892 tp->module = strndup(argv[1], p - argv[1]);
1897 tev->uprobes = (tp->module[0] == '/');
1901 fmt1_str = strtok_r(p, "+", &fmt);
1902 /* only the address started with 0x */
1903 if (fmt1_str[0] == '0') {
1905 * Fix a special case:
1906 * if address == 0, kernel reports something like:
1907 * p:probe_libc/abs_0 /lib/libc-2.18.so:0x (null) arg1=%ax
1908 * Newer kernel may fix that, but we want to
1909 * support old kernel also.
1911 if (strcmp(fmt1_str, "0x") == 0) {
1912 if (!argv[2] || strcmp(argv[2], "(null)")) {
1919 for (i = 2; argv[i + 1] != NULL; i++)
1920 argv[i] = argv[i + 1];
1925 tp->address = strtoull(fmt1_str, NULL, 0);
1927 /* Only the symbol-based probe has offset */
1928 tp->symbol = strdup(fmt1_str);
1929 if (tp->symbol == NULL) {
1933 fmt2_str = strtok_r(NULL, "", &fmt);
1934 if (fmt2_str == NULL)
1937 tp->offset = strtoul(fmt2_str, NULL, 10);
1941 fmt2_str = strchr(p, '(');
1943 tp->ref_ctr_offset = strtoul(fmt2_str + 1, NULL, 0);
1946 tev->nargs = argc - 2;
1947 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
1948 if (tev->args == NULL) {
1952 for (i = 0; i < tev->nargs; i++) {
1953 p = strchr(argv[i + 2], '=');
1954 if (p) /* We don't need which register is assigned. */
1958 tev->args[i].name = strdup(argv[i + 2]);
1959 /* TODO: parse regs and offset */
1960 tev->args[i].value = strdup(p);
1961 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1973 /* Compose only probe arg */
1974 char *synthesize_perf_probe_arg(struct perf_probe_arg *pa)
1976 struct perf_probe_arg_field *field = pa->field;
1981 if (strbuf_init(&buf, 64) < 0)
1984 if (pa->name && pa->var)
1985 err = strbuf_addf(&buf, "%s=%s", pa->name, pa->var);
1987 err = strbuf_addstr(&buf, pa->name ?: pa->var);
1992 if (field->name[0] == '[')
1993 err = strbuf_addstr(&buf, field->name);
1995 err = strbuf_addf(&buf, "%s%s", field->ref ? "->" : ".",
1997 field = field->next;
2003 if (strbuf_addf(&buf, ":%s", pa->type) < 0)
2006 ret = strbuf_detach(&buf, NULL);
2008 strbuf_release(&buf);
2012 /* Compose only probe point (not argument) */
2013 char *synthesize_perf_probe_point(struct perf_probe_point *pp)
2016 char *tmp, *ret = NULL;
2019 if (strbuf_init(&buf, 64) < 0)
2023 if (strbuf_addstr(&buf, pp->function) < 0)
2026 err = strbuf_addf(&buf, "+%lu", pp->offset);
2028 err = strbuf_addf(&buf, ":%d", pp->line);
2029 else if (pp->retprobe)
2030 err = strbuf_addstr(&buf, "%return");
2038 tmp = strchr(pp->file + len - 30, '/');
2039 tmp = tmp ? tmp + 1 : pp->file + len - 30;
2041 err = strbuf_addf(&buf, "@%s", tmp);
2042 if (!err && !pp->function && pp->line)
2043 err = strbuf_addf(&buf, ":%d", pp->line);
2046 ret = strbuf_detach(&buf, NULL);
2048 strbuf_release(&buf);
2052 char *synthesize_perf_probe_command(struct perf_probe_event *pev)
2055 char *tmp, *ret = NULL;
2058 if (strbuf_init(&buf, 64))
2061 if (strbuf_addf(&buf, "%s:%s=", pev->group ?: PERFPROBE_GROUP,
2065 tmp = synthesize_perf_probe_point(&pev->point);
2066 if (!tmp || strbuf_addstr(&buf, tmp) < 0)
2070 for (i = 0; i < pev->nargs; i++) {
2071 tmp = synthesize_perf_probe_arg(pev->args + i);
2072 if (!tmp || strbuf_addf(&buf, " %s", tmp) < 0)
2077 ret = strbuf_detach(&buf, NULL);
2079 strbuf_release(&buf);
2083 static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
2084 struct strbuf *buf, int depth)
2088 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
2093 if (ref->user_access)
2094 err = strbuf_addf(buf, "%s%ld(", "+u", ref->offset);
2096 err = strbuf_addf(buf, "%+ld(", ref->offset);
2097 return (err < 0) ? err : depth;
2100 static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
2103 struct probe_trace_arg_ref *ref = arg->ref;
2106 /* Argument name or separator */
2108 err = strbuf_addf(buf, " %s=", arg->name);
2110 err = strbuf_addch(buf, ' ');
2114 /* Special case: @XXX */
2115 if (arg->value[0] == '@' && arg->ref)
2118 /* Dereferencing arguments */
2120 depth = __synthesize_probe_trace_arg_ref(ref, buf, 1);
2125 /* Print argument value */
2126 if (arg->value[0] == '@' && arg->ref)
2127 err = strbuf_addf(buf, "%s%+ld", arg->value, arg->ref->offset);
2129 err = strbuf_addstr(buf, arg->value);
2132 while (!err && depth--)
2133 err = strbuf_addch(buf, ')');
2135 /* Print argument type */
2136 if (!err && arg->type)
2137 err = strbuf_addf(buf, ":%s", arg->type);
2143 synthesize_probe_trace_args(struct probe_trace_event *tev, struct strbuf *buf)
2147 for (i = 0; i < tev->nargs && ret >= 0; i++)
2148 ret = synthesize_probe_trace_arg(&tev->args[i], buf);
2154 synthesize_uprobe_trace_def(struct probe_trace_point *tp, struct strbuf *buf)
2158 /* Uprobes must have tp->module */
2162 * If tp->address == 0, then this point must be a
2163 * absolute address uprobe.
2164 * try_to_find_absolute_address() should have made
2165 * tp->symbol to "0x0".
2167 if (!tp->address && (!tp->symbol || strcmp(tp->symbol, "0x0")))
2170 /* Use the tp->address for uprobes */
2171 err = strbuf_addf(buf, "%s:0x%" PRIx64, tp->module, tp->address);
2173 if (err >= 0 && tp->ref_ctr_offset) {
2174 if (!uprobe_ref_ctr_is_supported())
2176 err = strbuf_addf(buf, "(0x%lx)", tp->ref_ctr_offset);
2178 return err >= 0 ? 0 : err;
2182 synthesize_kprobe_trace_def(struct probe_trace_point *tp, struct strbuf *buf)
2184 if (!strncmp(tp->symbol, "0x", 2)) {
2185 /* Absolute address. See try_to_find_absolute_address() */
2186 return strbuf_addf(buf, "%s%s0x%" PRIx64, tp->module ?: "",
2187 tp->module ? ":" : "", tp->address);
2189 return strbuf_addf(buf, "%s%s%s+%lu", tp->module ?: "",
2190 tp->module ? ":" : "", tp->symbol, tp->offset);
2194 char *synthesize_probe_trace_command(struct probe_trace_event *tev)
2196 struct probe_trace_point *tp = &tev->point;
2201 if (strbuf_init(&buf, 32) < 0)
2204 if (strbuf_addf(&buf, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
2205 tev->group, tev->event) < 0)
2209 err = synthesize_uprobe_trace_def(tp, &buf);
2211 err = synthesize_kprobe_trace_def(tp, &buf);
2214 err = synthesize_probe_trace_args(tev, &buf);
2217 ret = strbuf_detach(&buf, NULL);
2219 strbuf_release(&buf);
2223 static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
2224 struct perf_probe_point *pp,
2227 struct symbol *sym = NULL;
2228 struct map *map = NULL;
2229 u64 addr = tp->address;
2233 map = dso__new_map(tp->module);
2236 sym = map__find_symbol(map, addr);
2238 if (tp->symbol && !addr) {
2239 if (kernel_get_symbol_address_by_name(tp->symbol,
2240 &addr, true, false) < 0)
2245 sym = machine__find_kernel_symbol(host_machine, addr, &map);
2252 pp->retprobe = tp->retprobe;
2253 pp->offset = addr - map__unmap_ip(map, sym->start);
2254 pp->function = strdup(sym->name);
2255 ret = pp->function ? 0 : -ENOMEM;
2258 if (map && !is_kprobe) {
2265 static int convert_to_perf_probe_point(struct probe_trace_point *tp,
2266 struct perf_probe_point *pp,
2272 ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe);
2275 ret = find_perf_probe_point_from_map(tp, pp, is_kprobe);
2279 pr_debug("Failed to find probe point from both of dwarf and map.\n");
2282 pp->function = strdup(tp->symbol);
2283 pp->offset = tp->offset;
2285 ret = e_snprintf(buf, 128, "0x%" PRIx64, tp->address);
2288 pp->function = strdup(buf);
2291 if (pp->function == NULL)
2294 pp->retprobe = tp->retprobe;
2299 static int convert_to_perf_probe_event(struct probe_trace_event *tev,
2300 struct perf_probe_event *pev, bool is_kprobe)
2302 struct strbuf buf = STRBUF_INIT;
2305 /* Convert event/group name */
2306 pev->event = strdup(tev->event);
2307 pev->group = strdup(tev->group);
2308 if (pev->event == NULL || pev->group == NULL)
2311 /* Convert trace_point to probe_point */
2312 ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
2316 /* Convert trace_arg to probe_arg */
2317 pev->nargs = tev->nargs;
2318 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
2319 if (pev->args == NULL)
2321 for (i = 0; i < tev->nargs && ret >= 0; i++) {
2322 if (tev->args[i].name)
2323 pev->args[i].name = strdup(tev->args[i].name);
2325 if ((ret = strbuf_init(&buf, 32)) < 0)
2327 ret = synthesize_probe_trace_arg(&tev->args[i], &buf);
2328 pev->args[i].name = strbuf_detach(&buf, NULL);
2330 if (pev->args[i].name == NULL && ret >= 0)
2335 clear_perf_probe_event(pev);
2340 void clear_perf_probe_event(struct perf_probe_event *pev)
2342 struct perf_probe_arg_field *field, *next;
2347 zfree(&pev->target);
2348 clear_perf_probe_point(&pev->point);
2350 for (i = 0; i < pev->nargs; i++) {
2351 zfree(&pev->args[i].name);
2352 zfree(&pev->args[i].var);
2353 zfree(&pev->args[i].type);
2354 field = pev->args[i].field;
2357 zfree(&field->name);
2366 #define strdup_or_goto(str, label) \
2367 ({ char *__p = NULL; if (str && !(__p = strdup(str))) goto label; __p; })
2369 static int perf_probe_point__copy(struct perf_probe_point *dst,
2370 struct perf_probe_point *src)
2372 dst->file = strdup_or_goto(src->file, out_err);
2373 dst->function = strdup_or_goto(src->function, out_err);
2374 dst->lazy_line = strdup_or_goto(src->lazy_line, out_err);
2375 dst->line = src->line;
2376 dst->retprobe = src->retprobe;
2377 dst->offset = src->offset;
2381 clear_perf_probe_point(dst);
2385 static int perf_probe_arg__copy(struct perf_probe_arg *dst,
2386 struct perf_probe_arg *src)
2388 struct perf_probe_arg_field *field, **ppfield;
2390 dst->name = strdup_or_goto(src->name, out_err);
2391 dst->var = strdup_or_goto(src->var, out_err);
2392 dst->type = strdup_or_goto(src->type, out_err);
2395 ppfield = &(dst->field);
2397 *ppfield = zalloc(sizeof(*field));
2400 (*ppfield)->name = strdup_or_goto(field->name, out_err);
2401 (*ppfield)->index = field->index;
2402 (*ppfield)->ref = field->ref;
2403 field = field->next;
2404 ppfield = &((*ppfield)->next);
2411 int perf_probe_event__copy(struct perf_probe_event *dst,
2412 struct perf_probe_event *src)
2416 dst->event = strdup_or_goto(src->event, out_err);
2417 dst->group = strdup_or_goto(src->group, out_err);
2418 dst->target = strdup_or_goto(src->target, out_err);
2419 dst->uprobes = src->uprobes;
2421 if (perf_probe_point__copy(&dst->point, &src->point) < 0)
2424 dst->args = zalloc(sizeof(struct perf_probe_arg) * src->nargs);
2427 dst->nargs = src->nargs;
2429 for (i = 0; i < src->nargs; i++)
2430 if (perf_probe_arg__copy(&dst->args[i], &src->args[i]) < 0)
2435 clear_perf_probe_event(dst);
2439 void clear_probe_trace_event(struct probe_trace_event *tev)
2441 struct probe_trace_arg_ref *ref, *next;
2446 zfree(&tev->point.symbol);
2447 zfree(&tev->point.realname);
2448 zfree(&tev->point.module);
2449 for (i = 0; i < tev->nargs; i++) {
2450 zfree(&tev->args[i].name);
2451 zfree(&tev->args[i].value);
2452 zfree(&tev->args[i].type);
2453 ref = tev->args[i].ref;
2464 struct kprobe_blacklist_node {
2465 struct list_head list;
2471 static void kprobe_blacklist__delete(struct list_head *blacklist)
2473 struct kprobe_blacklist_node *node;
2475 while (!list_empty(blacklist)) {
2476 node = list_first_entry(blacklist,
2477 struct kprobe_blacklist_node, list);
2478 list_del_init(&node->list);
2479 zfree(&node->symbol);
2484 static int kprobe_blacklist__load(struct list_head *blacklist)
2486 struct kprobe_blacklist_node *node;
2487 const char *__debugfs = debugfs__mountpoint();
2488 char buf[PATH_MAX], *p;
2492 if (__debugfs == NULL)
2495 ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs);
2499 fp = fopen(buf, "r");
2504 while (fgets(buf, PATH_MAX, fp)) {
2505 node = zalloc(sizeof(*node));
2510 INIT_LIST_HEAD(&node->list);
2511 list_add_tail(&node->list, blacklist);
2512 if (sscanf(buf, "0x%" PRIx64 "-0x%" PRIx64, &node->start, &node->end) != 2) {
2516 p = strchr(buf, '\t');
2519 if (p[strlen(p) - 1] == '\n')
2520 p[strlen(p) - 1] = '\0';
2522 p = (char *)"unknown";
2523 node->symbol = strdup(p);
2524 if (!node->symbol) {
2528 pr_debug2("Blacklist: 0x%" PRIx64 "-0x%" PRIx64 ", %s\n",
2529 node->start, node->end, node->symbol);
2533 kprobe_blacklist__delete(blacklist);
2539 static struct kprobe_blacklist_node *
2540 kprobe_blacklist__find_by_address(struct list_head *blacklist, u64 address)
2542 struct kprobe_blacklist_node *node;
2544 list_for_each_entry(node, blacklist, list) {
2545 if (node->start <= address && address < node->end)
2552 static LIST_HEAD(kprobe_blacklist);
2554 static void kprobe_blacklist__init(void)
2556 if (!list_empty(&kprobe_blacklist))
2559 if (kprobe_blacklist__load(&kprobe_blacklist) < 0)
2560 pr_debug("No kprobe blacklist support, ignored\n");
2563 static void kprobe_blacklist__release(void)
2565 kprobe_blacklist__delete(&kprobe_blacklist);
2568 static bool kprobe_blacklist__listed(u64 address)
2570 return !!kprobe_blacklist__find_by_address(&kprobe_blacklist, address);
2573 static int perf_probe_event__sprintf(const char *group, const char *event,
2574 struct perf_probe_event *pev,
2576 struct strbuf *result)
2581 if (asprintf(&buf, "%s:%s", group, event) < 0)
2583 ret = strbuf_addf(result, " %-20s (on ", buf);
2588 /* Synthesize only event probe point */
2589 buf = synthesize_perf_probe_point(&pev->point);
2592 ret = strbuf_addstr(result, buf);
2596 ret = strbuf_addf(result, " in %s", module);
2598 if (!ret && pev->nargs > 0) {
2599 ret = strbuf_add(result, " with", 5);
2600 for (i = 0; !ret && i < pev->nargs; i++) {
2601 buf = synthesize_perf_probe_arg(&pev->args[i]);
2604 ret = strbuf_addf(result, " %s", buf);
2609 ret = strbuf_addch(result, ')');
2615 int show_perf_probe_event(const char *group, const char *event,
2616 struct perf_probe_event *pev,
2617 const char *module, bool use_stdout)
2619 struct strbuf buf = STRBUF_INIT;
2622 ret = perf_probe_event__sprintf(group, event, pev, module, &buf);
2625 printf("%s\n", buf.buf);
2627 pr_info("%s\n", buf.buf);
2629 strbuf_release(&buf);
2634 static bool filter_probe_trace_event(struct probe_trace_event *tev,
2635 struct strfilter *filter)
2639 /* At first, check the event name itself */
2640 if (strfilter__compare(filter, tev->event))
2643 /* Next, check the combination of name and group */
2644 if (e_snprintf(tmp, 128, "%s:%s", tev->group, tev->event) < 0)
2646 return strfilter__compare(filter, tmp);
2649 static int __show_perf_probe_events(int fd, bool is_kprobe,
2650 struct strfilter *filter)
2653 struct probe_trace_event tev;
2654 struct perf_probe_event pev;
2655 struct strlist *rawlist;
2656 struct str_node *ent;
2658 memset(&tev, 0, sizeof(tev));
2659 memset(&pev, 0, sizeof(pev));
2661 rawlist = probe_file__get_rawlist(fd);
2665 strlist__for_each_entry(ent, rawlist) {
2666 ret = parse_probe_trace_command(ent->s, &tev);
2668 if (!filter_probe_trace_event(&tev, filter))
2670 ret = convert_to_perf_probe_event(&tev, &pev,
2674 ret = show_perf_probe_event(pev.group, pev.event,
2675 &pev, tev.point.module,
2679 clear_perf_probe_event(&pev);
2680 clear_probe_trace_event(&tev);
2684 strlist__delete(rawlist);
2685 /* Cleanup cached debuginfo if needed */
2686 debuginfo_cache__exit();
2691 /* List up current perf-probe events */
2692 int show_perf_probe_events(struct strfilter *filter)
2694 int kp_fd, up_fd, ret;
2698 if (probe_conf.cache)
2699 return probe_cache__show_all_caches(filter);
2701 ret = init_probe_symbol_maps(false);
2705 ret = probe_file__open_both(&kp_fd, &up_fd, 0);
2710 ret = __show_perf_probe_events(kp_fd, true, filter);
2711 if (up_fd >= 0 && ret >= 0)
2712 ret = __show_perf_probe_events(up_fd, false, filter);
2717 exit_probe_symbol_maps();
2722 static int get_new_event_name(char *buf, size_t len, const char *base,
2723 struct strlist *namelist, bool ret_event,
2731 nbase = strdup(base);
2735 /* Cut off the dot suffixes (e.g. .const, .isra) and version suffixes */
2736 p = strpbrk(nbase, ".@");
2737 if (p && p != nbase)
2740 /* Try no suffix number */
2741 ret = e_snprintf(buf, len, "%s%s", nbase, ret_event ? "__return" : "");
2743 pr_debug("snprintf() failed: %d\n", ret);
2746 if (!strlist__has_entry(namelist, buf))
2749 if (!allow_suffix) {
2750 pr_warning("Error: event \"%s\" already exists.\n"
2751 " Hint: Remove existing event by 'perf probe -d'\n"
2752 " or force duplicates by 'perf probe -f'\n"
2753 " or set 'force=yes' in BPF source.\n",
2759 /* Try to add suffix */
2760 for (i = 1; i < MAX_EVENT_INDEX; i++) {
2761 ret = e_snprintf(buf, len, "%s_%d", nbase, i);
2763 pr_debug("snprintf() failed: %d\n", ret);
2766 if (!strlist__has_entry(namelist, buf))
2769 if (i == MAX_EVENT_INDEX) {
2770 pr_warning("Too many events are on the same function.\n");
2777 /* Final validation */
2778 if (ret >= 0 && !is_c_func_name(buf)) {
2779 pr_warning("Internal error: \"%s\" is an invalid event name.\n",
2787 /* Warn if the current kernel's uprobe implementation is old */
2788 static void warn_uprobe_event_compat(struct probe_trace_event *tev)
2791 char *buf = synthesize_probe_trace_command(tev);
2792 struct probe_trace_point *tp = &tev->point;
2794 if (tp->ref_ctr_offset && !uprobe_ref_ctr_is_supported()) {
2795 pr_warning("A semaphore is associated with %s:%s and "
2796 "seems your kernel doesn't support it.\n",
2797 tev->group, tev->event);
2800 /* Old uprobe event doesn't support memory dereference */
2801 if (!tev->uprobes || tev->nargs == 0 || !buf)
2804 for (i = 0; i < tev->nargs; i++) {
2805 if (strchr(tev->args[i].value, '@')) {
2806 pr_warning("%s accesses a variable by symbol name, but that is not supported for user application probe.\n",
2807 tev->args[i].value);
2810 if (strglobmatch(tev->args[i].value, "[$+-]*")) {
2811 pr_warning("Please upgrade your kernel to at least 3.14 to have access to feature %s\n",
2812 tev->args[i].value);
2820 /* Set new name from original perf_probe_event and namelist */
2821 static int probe_trace_event__set_name(struct probe_trace_event *tev,
2822 struct perf_probe_event *pev,
2823 struct strlist *namelist,
2826 const char *event, *group;
2830 /* If probe_event or trace_event already have the name, reuse it */
2831 if (pev->event && !pev->sdt)
2833 else if (tev->event)
2836 /* Or generate new one from probe point */
2837 if (pev->point.function &&
2838 (strncmp(pev->point.function, "0x", 2) != 0) &&
2839 !strisglob(pev->point.function))
2840 event = pev->point.function;
2842 event = tev->point.realname;
2844 if (pev->group && !pev->sdt)
2846 else if (tev->group)
2849 group = PERFPROBE_GROUP;
2851 /* Get an unused new event name */
2852 ret = get_new_event_name(buf, 64, event, namelist,
2853 tev->point.retprobe, allow_suffix);
2859 tev->event = strdup(event);
2860 tev->group = strdup(group);
2861 if (tev->event == NULL || tev->group == NULL)
2865 * Add new event name to namelist if multiprobe event is NOT
2866 * supported, since we have to use new event name for following
2867 * probes in that case.
2869 if (!multiprobe_event_is_supported())
2870 strlist__add(namelist, event);
2874 static int __open_probe_file_and_namelist(bool uprobe,
2875 struct strlist **namelist)
2879 fd = probe_file__open(PF_FL_RW | (uprobe ? PF_FL_UPROBE : 0));
2883 /* Get current event names */
2884 *namelist = probe_file__get_namelist(fd);
2886 pr_debug("Failed to get current event list.\n");
2893 static int __add_probe_trace_events(struct perf_probe_event *pev,
2894 struct probe_trace_event *tevs,
2895 int ntevs, bool allow_suffix)
2897 int i, fd[2] = {-1, -1}, up, ret;
2898 struct probe_trace_event *tev = NULL;
2899 struct probe_cache *cache = NULL;
2900 struct strlist *namelist[2] = {NULL, NULL};
2901 struct nscookie nsc;
2903 up = pev->uprobes ? 1 : 0;
2904 fd[up] = __open_probe_file_and_namelist(up, &namelist[up]);
2909 for (i = 0; i < ntevs; i++) {
2911 up = tev->uprobes ? 1 : 0;
2912 if (fd[up] == -1) { /* Open the kprobe/uprobe_events */
2913 fd[up] = __open_probe_file_and_namelist(up,
2918 /* Skip if the symbol is out of .text or blacklisted */
2919 if (!tev->point.symbol && !pev->uprobes)
2922 /* Set new name for tev (and update namelist) */
2923 ret = probe_trace_event__set_name(tev, pev, namelist[up],
2928 nsinfo__mountns_enter(pev->nsi, &nsc);
2929 ret = probe_file__add_event(fd[up], tev);
2930 nsinfo__mountns_exit(&nsc);
2935 * Probes after the first probe which comes from same
2936 * user input are always allowed to add suffix, because
2937 * there might be several addresses corresponding to
2940 allow_suffix = true;
2942 if (ret == -EINVAL && pev->uprobes)
2943 warn_uprobe_event_compat(tev);
2944 if (ret == 0 && probe_conf.cache) {
2945 cache = probe_cache__new(pev->target, pev->nsi);
2947 probe_cache__add_entry(cache, pev, tevs, ntevs) < 0 ||
2948 probe_cache__commit(cache) < 0)
2949 pr_warning("Failed to add event to probe cache\n");
2950 probe_cache__delete(cache);
2954 for (up = 0; up < 2; up++) {
2955 strlist__delete(namelist[up]);
2962 static int find_probe_functions(struct map *map, char *name,
2963 struct symbol **syms)
2967 struct rb_node *tmp;
2968 const char *norm, *ver;
2970 bool cut_version = true;
2972 if (map__load(map) < 0)
2973 return -EACCES; /* Possible permission error to load symbols */
2975 /* If user gives a version, don't cut off the version from symbols */
2976 if (strchr(name, '@'))
2977 cut_version = false;
2979 map__for_each_symbol(map, sym, tmp) {
2980 norm = arch__normalize_symbol_name(sym->name);
2985 /* We don't care about default symbol or not */
2986 ver = strchr(norm, '@');
2988 buf = strndup(norm, ver - norm);
2995 if (strglobmatch(norm, name)) {
2997 if (syms && found < probe_conf.max_probes)
2998 syms[found - 1] = sym;
3007 void __weak arch__fix_tev_from_maps(struct perf_probe_event *pev __maybe_unused,
3008 struct probe_trace_event *tev __maybe_unused,
3009 struct map *map __maybe_unused,
3010 struct symbol *sym __maybe_unused) { }
3013 static void pr_kallsyms_access_error(void)
3015 pr_err("Please ensure you can read the /proc/kallsyms symbol addresses.\n"
3016 "If /proc/sys/kernel/kptr_restrict is '2', you can not read\n"
3017 "kernel symbol addresses even if you are a superuser. Please change\n"
3018 "it to '1'. If kptr_restrict is '1', the superuser can read the\n"
3019 "symbol addresses.\n"
3020 "In that case, please run this command again with sudo.\n");
3024 * Find probe function addresses from map.
3025 * Return an error or the number of found probe_trace_event
3027 static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
3028 struct probe_trace_event **tevs)
3030 struct map *map = NULL;
3031 struct ref_reloc_sym *reloc_sym = NULL;
3033 struct symbol **syms = NULL;
3034 struct probe_trace_event *tev;
3035 struct perf_probe_point *pp = &pev->point;
3036 struct probe_trace_point *tp;
3037 int num_matched_functions;
3038 int ret, i, j, skipped = 0;
3041 map = get_target_map(pev->target, pev->nsi, pev->uprobes);
3047 syms = malloc(sizeof(struct symbol *) * probe_conf.max_probes);
3054 * Load matched symbols: Since the different local symbols may have
3055 * same name but different addresses, this lists all the symbols.
3057 num_matched_functions = find_probe_functions(map, pp->function, syms);
3058 if (num_matched_functions <= 0) {
3059 if (num_matched_functions == -EACCES) {
3060 pr_err("Failed to load symbols from %s\n",
3061 pev->target ?: "/proc/kallsyms");
3063 pr_err("Please ensure the file is not stripped.\n");
3065 pr_kallsyms_access_error();
3067 pr_err("Failed to find symbol %s in %s\n", pp->function,
3068 pev->target ? : "kernel");
3071 } else if (num_matched_functions > probe_conf.max_probes) {
3072 pr_err("Too many functions matched in %s\n",
3073 pev->target ? : "kernel");
3078 /* Note that the symbols in the kmodule are not relocated */
3079 if (!pev->uprobes && !pev->target &&
3080 (!pp->retprobe || kretprobe_offset_is_supported())) {
3081 reloc_sym = kernel_get_ref_reloc_sym(NULL);
3083 pr_warning("Relocated base symbol is not found! "
3084 "Check /proc/sys/kernel/kptr_restrict\n"
3085 "and /proc/sys/kernel/perf_event_paranoid. "
3086 "Or run as privileged perf user.\n\n");
3092 /* Setup result trace-probe-events */
3093 *tevs = zalloc(sizeof(*tev) * num_matched_functions);
3101 for (j = 0; j < num_matched_functions; j++) {
3104 if (sym->type != STT_FUNC)
3107 /* There can be duplicated symbols in the map */
3108 for (i = 0; i < j; i++)
3109 if (sym->start == syms[i]->start) {
3110 pr_debug("Found duplicated symbol %s @ %" PRIx64 "\n",
3111 sym->name, sym->start);
3117 tev = (*tevs) + ret;
3119 if (ret == num_matched_functions) {
3120 pr_warning("Too many symbols are listed. Skip it.\n");
3125 if (pp->offset > sym->end - sym->start) {
3126 pr_warning("Offset %ld is bigger than the size of %s\n",
3127 pp->offset, sym->name);
3131 /* Add one probe point */
3132 tp->address = map__unmap_ip(map, sym->start) + pp->offset;
3134 /* Check the kprobe (not in module) is within .text */
3135 if (!pev->uprobes && !pev->target &&
3136 kprobe_warn_out_range(sym->name, tp->address)) {
3137 tp->symbol = NULL; /* Skip it */
3139 } else if (reloc_sym) {
3140 tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
3141 tp->offset = tp->address - reloc_sym->addr;
3143 tp->symbol = strdup_or_goto(sym->name, nomem_out);
3144 tp->offset = pp->offset;
3146 tp->realname = strdup_or_goto(sym->name, nomem_out);
3148 tp->retprobe = pp->retprobe;
3151 tev->point.module = strdup_or_goto(pev->target,
3154 mod_name = find_module_name(pev->target);
3156 strdup(mod_name ? mod_name : pev->target);
3158 if (!tev->point.module)
3162 tev->uprobes = pev->uprobes;
3163 tev->nargs = pev->nargs;
3165 tev->args = zalloc(sizeof(struct probe_trace_arg) *
3167 if (tev->args == NULL)
3170 for (i = 0; i < tev->nargs; i++) {
3171 if (pev->args[i].name)
3173 strdup_or_goto(pev->args[i].name,
3176 tev->args[i].value = strdup_or_goto(pev->args[i].var,
3178 if (pev->args[i].type)
3180 strdup_or_goto(pev->args[i].type,
3183 arch__fix_tev_from_maps(pev, tev, map, sym);
3185 if (ret == skipped) {
3198 clear_probe_trace_events(*tevs, num_matched_functions);
3203 static int try_to_find_absolute_address(struct perf_probe_event *pev,
3204 struct probe_trace_event **tevs)
3206 struct perf_probe_point *pp = &pev->point;
3207 struct probe_trace_event *tev;
3208 struct probe_trace_point *tp;
3211 if (!(pev->point.function && !strncmp(pev->point.function, "0x", 2)))
3213 if (perf_probe_event_need_dwarf(pev))
3217 * This is 'perf probe /lib/libc.so 0xabcd'. Try to probe at
3220 * Only one tev can be generated by this.
3222 *tevs = zalloc(sizeof(*tev));
3230 * Don't use tp->offset, use address directly, because
3231 * in synthesize_probe_trace_command() address cannot be
3234 tp->address = pev->point.abs_address;
3235 tp->retprobe = pp->retprobe;
3236 tev->uprobes = pev->uprobes;
3240 * Give it a '0x' leading symbol name.
3241 * In __add_probe_trace_events, a NULL symbol is interpreted as
3244 if (asprintf(&tp->symbol, "0x%" PRIx64, tp->address) < 0)
3247 /* For kprobe, check range */
3248 if ((!tev->uprobes) &&
3249 (kprobe_warn_out_range(tev->point.symbol,
3250 tev->point.address))) {
3255 if (asprintf(&tp->realname, "abs_%" PRIx64, tp->address) < 0)
3259 tp->module = strdup(pev->target);
3265 tev->group = strdup(pev->group);
3271 tev->event = strdup(pev->event);
3276 tev->nargs = pev->nargs;
3277 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
3281 for (i = 0; i < tev->nargs; i++)
3282 copy_to_probe_trace_arg(&tev->args[i], &pev->args[i]);
3287 clear_probe_trace_events(*tevs, 1);
3292 /* Concatenate two arrays */
3293 static void *memcat(void *a, size_t sz_a, void *b, size_t sz_b)
3297 ret = malloc(sz_a + sz_b);
3299 memcpy(ret, a, sz_a);
3300 memcpy(ret + sz_a, b, sz_b);
3306 concat_probe_trace_events(struct probe_trace_event **tevs, int *ntevs,
3307 struct probe_trace_event **tevs2, int ntevs2)
3309 struct probe_trace_event *new_tevs;
3319 if (*ntevs + ntevs2 > probe_conf.max_probes)
3322 /* Concatenate the array of probe_trace_event */
3323 new_tevs = memcat(*tevs, (*ntevs) * sizeof(**tevs),
3324 *tevs2, ntevs2 * sizeof(**tevs2));
3334 clear_probe_trace_events(*tevs2, ntevs2);
3341 * Try to find probe_trace_event from given probe caches. Return the number
3342 * of cached events found, if an error occurs return the error.
3344 static int find_cached_events(struct perf_probe_event *pev,
3345 struct probe_trace_event **tevs,
3348 struct probe_cache *cache;
3349 struct probe_cache_entry *entry;
3350 struct probe_trace_event *tmp_tevs = NULL;
3354 cache = probe_cache__new(target, pev->nsi);
3355 /* Return 0 ("not found") if the target has no probe cache. */
3359 for_each_probe_cache_entry(entry, cache) {
3360 /* Skip the cache entry which has no name */
3361 if (!entry->pev.event || !entry->pev.group)
3363 if ((!pev->group || strglobmatch(entry->pev.group, pev->group)) &&
3364 strglobmatch(entry->pev.event, pev->event)) {
3365 ret = probe_cache_entry__get_event(entry, &tmp_tevs);
3367 ret = concat_probe_trace_events(tevs, &ntevs,
3373 probe_cache__delete(cache);
3375 clear_probe_trace_events(*tevs, ntevs);
3379 if (ntevs > 0 && target && target[0] == '/')
3380 pev->uprobes = true;
3386 /* Try to find probe_trace_event from all probe caches */
3387 static int find_cached_events_all(struct perf_probe_event *pev,
3388 struct probe_trace_event **tevs)
3390 struct probe_trace_event *tmp_tevs = NULL;
3391 struct strlist *bidlist;
3392 struct str_node *nd;
3397 /* Get the buildid list of all valid caches */
3398 bidlist = build_id_cache__list_all(true);
3401 pr_debug("Failed to get buildids: %d\n", ret);
3406 strlist__for_each_entry(nd, bidlist) {
3407 pathname = build_id_cache__origname(nd->s);
3408 ret = find_cached_events(pev, &tmp_tevs, pathname);
3409 /* In the case of cnt == 0, we just skip it */
3411 ret = concat_probe_trace_events(tevs, &ntevs,
3417 strlist__delete(bidlist);
3420 clear_probe_trace_events(*tevs, ntevs);
3428 static int find_probe_trace_events_from_cache(struct perf_probe_event *pev,
3429 struct probe_trace_event **tevs)
3431 struct probe_cache *cache;
3432 struct probe_cache_entry *entry;
3433 struct probe_trace_event *tev;
3434 struct str_node *node;
3438 /* For SDT/cached events, we use special search functions */
3440 return find_cached_events_all(pev, tevs);
3442 return find_cached_events(pev, tevs, pev->target);
3444 cache = probe_cache__new(pev->target, pev->nsi);
3448 entry = probe_cache__find(cache, pev);
3450 /* SDT must be in the cache */
3451 ret = pev->sdt ? -ENOENT : 0;
3455 ret = strlist__nr_entries(entry->tevlist);
3456 if (ret > probe_conf.max_probes) {
3457 pr_debug("Too many entries matched in the cache of %s\n",
3458 pev->target ? : "kernel");
3463 *tevs = zalloc(ret * sizeof(*tev));
3470 strlist__for_each_entry(node, entry->tevlist) {
3471 tev = &(*tevs)[i++];
3472 ret = parse_probe_trace_command(node->s, tev);
3475 /* Set the uprobes attribute as same as original */
3476 tev->uprobes = pev->uprobes;
3481 probe_cache__delete(cache);
3485 static int convert_to_probe_trace_events(struct perf_probe_event *pev,
3486 struct probe_trace_event **tevs)
3490 if (!pev->group && !pev->sdt) {
3491 /* Set group name if not given */
3492 if (!pev->uprobes) {
3493 pev->group = strdup(PERFPROBE_GROUP);
3494 ret = pev->group ? 0 : -ENOMEM;
3496 ret = convert_exec_to_group(pev->target, &pev->group);
3498 pr_warning("Failed to make a group name.\n");
3503 ret = try_to_find_absolute_address(pev, tevs);
3507 /* At first, we need to lookup cache entry */
3508 ret = find_probe_trace_events_from_cache(pev, tevs);
3509 if (ret > 0 || pev->sdt) /* SDT can be found only in the cache */
3510 return ret == 0 ? -ENOENT : ret; /* Found in probe cache */
3512 /* Convert perf_probe_event with debuginfo */
3513 ret = try_to_find_probe_trace_events(pev, tevs);
3515 return ret; /* Found in debuginfo or got an error */
3517 return find_probe_trace_events_from_map(pev, tevs);
3520 int convert_perf_probe_events(struct perf_probe_event *pevs, int npevs)
3524 /* Loop 1: convert all events */
3525 for (i = 0; i < npevs; i++) {
3526 /* Init kprobe blacklist if needed */
3527 if (!pevs[i].uprobes)
3528 kprobe_blacklist__init();
3529 /* Convert with or without debuginfo */
3530 ret = convert_to_probe_trace_events(&pevs[i], &pevs[i].tevs);
3533 pevs[i].ntevs = ret;
3535 /* This just release blacklist only if allocated */
3536 kprobe_blacklist__release();
3541 static int show_probe_trace_event(struct probe_trace_event *tev)
3543 char *buf = synthesize_probe_trace_command(tev);
3546 pr_debug("Failed to synthesize probe trace event.\n");
3550 /* Showing definition always go stdout */
3551 printf("%s\n", buf);
3557 int show_probe_trace_events(struct perf_probe_event *pevs, int npevs)
3559 struct strlist *namelist = strlist__new(NULL, NULL);
3560 struct probe_trace_event *tev;
3561 struct perf_probe_event *pev;
3567 for (j = 0; j < npevs && !ret; j++) {
3569 for (i = 0; i < pev->ntevs && !ret; i++) {
3570 tev = &pev->tevs[i];
3571 /* Skip if the symbol is out of .text or blacklisted */
3572 if (!tev->point.symbol && !pev->uprobes)
3575 /* Set new name for tev (and update namelist) */
3576 ret = probe_trace_event__set_name(tev, pev,
3579 ret = show_probe_trace_event(tev);
3582 strlist__delete(namelist);
3587 static int show_bootconfig_event(struct probe_trace_event *tev)
3589 struct probe_trace_point *tp = &tev->point;
3594 if (strbuf_init(&buf, 32) < 0)
3597 err = synthesize_kprobe_trace_def(tp, &buf);
3599 err = synthesize_probe_trace_args(tev, &buf);
3601 ret = strbuf_detach(&buf, NULL);
3602 strbuf_release(&buf);
3605 printf("'%s'", ret);
3612 int show_bootconfig_events(struct perf_probe_event *pevs, int npevs)
3614 struct strlist *namelist = strlist__new(NULL, NULL);
3615 struct probe_trace_event *tev;
3616 struct perf_probe_event *pev;
3617 char *cur_name = NULL;
3623 for (j = 0; j < npevs && !ret; j++) {
3625 if (pev->group && strcmp(pev->group, "probe"))
3626 pr_warning("WARN: Group name %s is ignored\n", pev->group);
3628 pr_warning("ERROR: Bootconfig doesn't support uprobes\n");
3632 for (i = 0; i < pev->ntevs && !ret; i++) {
3633 tev = &pev->tevs[i];
3634 /* Skip if the symbol is out of .text or blacklisted */
3635 if (!tev->point.symbol && !pev->uprobes)
3638 /* Set new name for tev (and update namelist) */
3639 ret = probe_trace_event__set_name(tev, pev,
3644 if (!cur_name || strcmp(cur_name, tev->event)) {
3645 printf("%sftrace.event.kprobes.%s.probe = ",
3646 cur_name ? "\n" : "", tev->event);
3647 cur_name = tev->event;
3650 ret = show_bootconfig_event(tev);
3654 strlist__delete(namelist);
3659 int apply_perf_probe_events(struct perf_probe_event *pevs, int npevs)
3663 /* Loop 2: add all events */
3664 for (i = 0; i < npevs; i++) {
3665 ret = __add_probe_trace_events(&pevs[i], pevs[i].tevs,
3667 probe_conf.force_add);
3674 void cleanup_perf_probe_events(struct perf_probe_event *pevs, int npevs)
3677 struct perf_probe_event *pev;
3679 /* Loop 3: cleanup and free trace events */
3680 for (i = 0; i < npevs; i++) {
3682 for (j = 0; j < pevs[i].ntevs; j++)
3683 clear_probe_trace_event(&pevs[i].tevs[j]);
3684 zfree(&pevs[i].tevs);
3686 nsinfo__zput(pev->nsi);
3687 clear_perf_probe_event(&pevs[i]);
3691 int add_perf_probe_events(struct perf_probe_event *pevs, int npevs)
3695 ret = init_probe_symbol_maps(pevs->uprobes);
3699 ret = convert_perf_probe_events(pevs, npevs);
3701 ret = apply_perf_probe_events(pevs, npevs);
3703 cleanup_perf_probe_events(pevs, npevs);
3705 exit_probe_symbol_maps();
3709 int del_perf_probe_events(struct strfilter *filter)
3711 int ret, ret2, ufd = -1, kfd = -1;
3712 char *str = strfilter__string(filter);
3717 /* Get current event names */
3718 ret = probe_file__open_both(&kfd, &ufd, PF_FL_RW);
3722 ret = probe_file__del_events(kfd, filter);
3723 if (ret < 0 && ret != -ENOENT)
3726 ret2 = probe_file__del_events(ufd, filter);
3727 if (ret2 < 0 && ret2 != -ENOENT) {
3744 int show_available_funcs(const char *target, struct nsinfo *nsi,
3745 struct strfilter *_filter, bool user)
3751 ret = init_probe_symbol_maps(user);
3755 /* Get a symbol map */
3756 map = get_target_map(target, nsi, user);
3758 pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
3762 ret = map__load(map);
3765 char *str = strfilter__string(_filter);
3766 pr_err("Failed to find symbols matched to \"%s\"\n",
3770 pr_err("Failed to load symbols in %s\n",
3771 (target) ? : "kernel");
3774 dso = map__dso(map);
3775 dso__sort_by_name(dso);
3777 /* Show all (filtered) symbols */
3780 for (size_t i = 0; i < dso->symbol_names_len; i++) {
3781 struct symbol *pos = dso->symbol_names[i];
3783 if (strfilter__compare(_filter, pos->name))
3784 printf("%s\n", pos->name);
3788 exit_probe_symbol_maps();
3793 int copy_to_probe_trace_arg(struct probe_trace_arg *tvar,
3794 struct perf_probe_arg *pvar)
3796 tvar->value = strdup(pvar->var);
3797 if (tvar->value == NULL)
3800 tvar->type = strdup(pvar->type);
3801 if (tvar->type == NULL)
3805 tvar->name = strdup(pvar->name);
3806 if (tvar->name == NULL)