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.sort_by_name = true;
78 symbol_conf.allow_aliases = true;
79 ret = symbol__init(NULL);
81 pr_debug("Failed to init symbol map.\n");
85 if (host_machine || user_only) /* already initialized */
88 if (symbol_conf.vmlinux_name)
89 pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
91 host_machine = machine__new_host();
93 pr_debug("machine__new_host() failed.\n");
99 pr_warning("Failed to init vmlinux path.\n");
103 void exit_probe_symbol_maps(void)
105 machine__delete(host_machine);
110 static struct ref_reloc_sym *kernel_get_ref_reloc_sym(struct map **pmap)
113 struct map *map = machine__kernel_map(host_machine);
115 if (map__load(map) < 0)
118 kmap = map__kmap(map);
125 return kmap->ref_reloc_sym;
128 static int kernel_get_symbol_address_by_name(const char *name, u64 *addr,
129 bool reloc, bool reladdr)
131 struct ref_reloc_sym *reloc_sym;
135 /* ref_reloc_sym is just a label. Need a special fix*/
136 reloc_sym = kernel_get_ref_reloc_sym(&map);
137 if (reloc_sym && strcmp(name, reloc_sym->name) == 0)
138 *addr = (!map->reloc || reloc) ? reloc_sym->addr :
139 reloc_sym->unrelocated_addr;
141 sym = machine__find_kernel_symbol_by_name(host_machine, name, &map);
144 *addr = map__unmap_ip(map, sym->start) -
145 ((reloc) ? 0 : map->reloc) -
146 ((reladdr) ? map__start(map) : 0);
151 static struct map *kernel_get_module_map(const char *module)
153 struct maps *maps = machine__kernel_maps(host_machine);
154 struct map_rb_node *pos;
156 /* A file path -- this is an offline module */
157 if (module && strchr(module, '/'))
158 return dso__new_map(module);
161 struct map *map = machine__kernel_map(host_machine);
163 return map__get(map);
166 maps__for_each_entry(maps, pos) {
167 /* short_name is "[module]" */
168 struct dso *dso = map__dso(pos->map);
169 const char *short_name = dso->short_name;
170 u16 short_name_len = dso->short_name_len;
172 if (strncmp(short_name + 1, module,
173 short_name_len - 2) == 0 &&
174 module[short_name_len - 2] == '\0') {
175 return map__get(pos->map);
181 struct map *get_target_map(const char *target, struct nsinfo *nsi, bool user)
183 /* Init maps of given executable or kernel */
188 map = dso__new_map(target);
189 dso = map ? map__dso(map) : NULL;
191 mutex_lock(&dso->lock);
192 nsinfo__put(dso->nsinfo);
193 dso->nsinfo = nsinfo__get(nsi);
194 mutex_unlock(&dso->lock);
198 return kernel_get_module_map(target);
202 static int convert_exec_to_group(const char *exec, char **result)
204 char *ptr1, *ptr2, *exec_copy;
208 exec_copy = strdup(exec);
212 ptr1 = basename(exec_copy);
218 for (ptr2 = ptr1; *ptr2 != '\0'; ptr2++) {
219 if (!isalnum(*ptr2) && *ptr2 != '_') {
225 ret = e_snprintf(buf, 64, "%s_%s", PERFPROBE_GROUP, ptr1);
229 *result = strdup(buf);
230 ret = *result ? 0 : -ENOMEM;
237 static void clear_perf_probe_point(struct perf_probe_point *pp)
240 zfree(&pp->function);
241 zfree(&pp->lazy_line);
244 static void clear_probe_trace_events(struct probe_trace_event *tevs, int ntevs)
248 for (i = 0; i < ntevs; i++)
249 clear_probe_trace_event(tevs + i);
252 static bool kprobe_blacklist__listed(u64 address);
253 static bool kprobe_warn_out_range(const char *symbol, u64 address)
258 map = kernel_get_module_map(NULL);
260 ret = address <= map__start(map) || map__end(map) < address;
262 pr_warning("%s is out of .text, skip it.\n", symbol);
265 if (!ret && kprobe_blacklist__listed(address)) {
266 pr_warning("%s is blacklisted function, skip it.\n", symbol);
274 * @module can be module name of module file path. In case of path,
275 * inspect elf and find out what is actual module name.
276 * Caller has to free mod_name after using it.
278 static char *find_module_name(const char *module)
286 char *mod_name = NULL;
289 fd = open(module, O_RDONLY);
293 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
297 if (gelf_getehdr(elf, &ehdr) == NULL)
300 sec = elf_section_by_name(elf, &ehdr, &shdr,
301 ".gnu.linkonce.this_module", NULL);
305 data = elf_getdata(sec, NULL);
306 if (!data || !data->d_buf)
311 * '.gnu.linkonce.this_module' section of kernel module elf directly
312 * maps to 'struct module' from linux/module.h. This section contains
313 * actual module name which will be used by kernel after loading it.
314 * But, we cannot use 'struct module' here since linux/module.h is not
315 * exposed to user-space. Offset of 'name' has remained same from long
316 * time, so hardcoding it here.
318 if (ehdr.e_ident[EI_CLASS] == ELFCLASS32)
320 else /* expect ELFCLASS64 by default */
323 mod_name = strdup((char *)data->d_buf + name_offset);
332 #ifdef HAVE_DWARF_SUPPORT
334 static int kernel_get_module_dso(const char *module, struct dso **pdso)
338 const char *vmlinux_name;
342 char module_name[128];
344 snprintf(module_name, sizeof(module_name), "[%s]", module);
345 map = maps__find_by_name(machine__kernel_maps(host_machine), module_name);
350 pr_debug("Failed to find module %s.\n", module);
354 map = machine__kernel_map(host_machine);
356 if (!dso->has_build_id)
357 dso__read_running_kernel_build_id(dso, host_machine);
359 vmlinux_name = symbol_conf.vmlinux_name;
362 ret = dso__load_vmlinux(dso, map, vmlinux_name, false);
364 ret = dso__load_vmlinux_path(dso, map);
371 * Some binaries like glibc have special symbols which are on the symbol
372 * table, but not in the debuginfo. If we can find the address of the
373 * symbol from map, we can translate the address back to the probe point.
375 static int find_alternative_probe_point(struct debuginfo *dinfo,
376 struct perf_probe_point *pp,
377 struct perf_probe_point *result,
378 const char *target, struct nsinfo *nsi,
381 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) {
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;
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 ? 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 pr_warning("Probe point '%s' not found.\n",
965 synthesize_perf_probe_point(&pev->point));
967 } else if (ntevs < 0) {
968 /* Error path : ntevs < 0 */
969 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
971 pr_warning("Warning: No dwarf info found in the vmlinux - "
972 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
974 pr_debug("Trying to use symbols.\n");
981 #define LINEBUF_SIZE 256
982 #define NR_ADDITIONAL_LINES 2
984 static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
986 char buf[LINEBUF_SIZE], sbuf[STRERR_BUFSIZE];
987 const char *color = show_num ? "" : PERF_COLOR_BLUE;
988 const char *prefix = NULL;
991 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
996 prefix = show_num ? "%7d " : " ";
997 color_fprintf(stdout, color, prefix, l);
999 color_fprintf(stdout, color, "%s", buf);
1001 } while (strchr(buf, '\n') == NULL);
1006 pr_warning("File read error: %s\n",
1007 str_error_r(errno, sbuf, sizeof(sbuf)));
1013 static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
1015 int rv = __show_one_line(fp, l, skip, show_num);
1017 pr_warning("Source file is shorter than expected.\n");
1023 #define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
1024 #define show_one_line(f,l) _show_one_line(f,l,false,false)
1025 #define skip_one_line(f,l) _show_one_line(f,l,true,false)
1026 #define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
1029 * Show line-range always requires debuginfo to find source file and
1032 static int __show_line_range(struct line_range *lr, const char *module,
1035 struct build_id bid;
1037 struct int_node *ln;
1038 struct debuginfo *dinfo;
1042 char sbuf[STRERR_BUFSIZE];
1043 char sbuild_id[SBUILD_ID_SIZE] = "";
1045 /* Search a line range */
1046 dinfo = open_debuginfo(module, NULL, false);
1050 ret = debuginfo__find_line_range(dinfo, lr);
1051 if (!ret) { /* Not found, retry with an alternative */
1052 ret = get_alternative_line_range(dinfo, lr, module, user);
1054 ret = debuginfo__find_line_range(dinfo, lr);
1056 if (dinfo->build_id) {
1057 build_id__init(&bid, dinfo->build_id, BUILD_ID_SIZE);
1058 build_id__sprintf(&bid, sbuild_id);
1060 debuginfo__delete(dinfo);
1061 if (ret == 0 || ret == -ENOENT) {
1062 pr_warning("Specified source line is not found.\n");
1064 } else if (ret < 0) {
1065 pr_warning("Debuginfo analysis failed.\n");
1069 /* Convert source file path */
1071 ret = find_source_path(tmp, sbuild_id, lr->comp_dir, &lr->path);
1073 /* Free old path when new path is assigned */
1074 if (tmp != lr->path)
1078 pr_warning("Failed to find source file path.\n");
1085 fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
1086 lr->start - lr->offset);
1088 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
1090 fp = fopen(lr->path, "r");
1092 pr_warning("Failed to open %s: %s\n", lr->path,
1093 str_error_r(errno, sbuf, sizeof(sbuf)));
1096 /* Skip to starting line number */
1097 while (l < lr->start) {
1098 ret = skip_one_line(fp, l++);
1103 intlist__for_each_entry(ln, lr->line_list) {
1104 for (; ln->i > (unsigned long)l; l++) {
1105 ret = show_one_line(fp, l - lr->offset);
1109 ret = show_one_line_with_num(fp, l++ - lr->offset);
1114 if (lr->end == INT_MAX)
1115 lr->end = l + NR_ADDITIONAL_LINES;
1116 while (l <= lr->end) {
1117 ret = show_one_line_or_eof(fp, l++ - lr->offset);
1126 int show_line_range(struct line_range *lr, const char *module,
1127 struct nsinfo *nsi, bool user)
1130 struct nscookie nsc;
1132 ret = init_probe_symbol_maps(user);
1135 nsinfo__mountns_enter(nsi, &nsc);
1136 ret = __show_line_range(lr, module, user);
1137 nsinfo__mountns_exit(&nsc);
1138 exit_probe_symbol_maps();
1143 static int show_available_vars_at(struct debuginfo *dinfo,
1144 struct perf_probe_event *pev,
1145 struct strfilter *_filter)
1149 struct str_node *node;
1150 struct variable_list *vls = NULL, *vl;
1151 struct perf_probe_point tmp;
1154 buf = synthesize_perf_probe_point(&pev->point);
1157 pr_debug("Searching variables at %s\n", buf);
1159 ret = debuginfo__find_available_vars_at(dinfo, pev, &vls);
1160 if (!ret) { /* Not found, retry with an alternative */
1161 ret = get_alternative_probe_event(dinfo, pev, &tmp);
1163 ret = debuginfo__find_available_vars_at(dinfo, pev,
1165 /* Release the old probe_point */
1166 clear_perf_probe_point(&tmp);
1170 if (ret == 0 || ret == -ENOENT) {
1171 pr_err("Failed to find the address of %s\n", buf);
1174 pr_warning("Debuginfo analysis failed.\n");
1178 /* Some variables are found */
1179 fprintf(stdout, "Available variables at %s\n", buf);
1180 for (i = 0; i < ret; i++) {
1183 * A probe point might be converted to
1184 * several trace points.
1186 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
1188 zfree(&vl->point.symbol);
1191 strlist__for_each_entry(node, vl->vars) {
1192 var = strchr(node->s, '\t') + 1;
1193 if (strfilter__compare(_filter, var)) {
1194 fprintf(stdout, "\t\t%s\n", node->s);
1198 strlist__delete(vl->vars);
1201 fprintf(stdout, "\t\t(No matched variables)\n");
1209 /* Show available variables on given probe point */
1210 int show_available_vars(struct perf_probe_event *pevs, int npevs,
1211 struct strfilter *_filter)
1214 struct debuginfo *dinfo;
1216 ret = init_probe_symbol_maps(pevs->uprobes);
1220 dinfo = open_debuginfo(pevs->target, pevs->nsi, false);
1228 for (i = 0; i < npevs && ret >= 0; i++)
1229 ret = show_available_vars_at(dinfo, &pevs[i], _filter);
1231 debuginfo__delete(dinfo);
1233 exit_probe_symbol_maps();
1237 #else /* !HAVE_DWARF_SUPPORT */
1239 static void debuginfo_cache__exit(void)
1244 find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused,
1245 struct perf_probe_point *pp __maybe_unused,
1246 bool is_kprobe __maybe_unused)
1251 static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
1252 struct probe_trace_event **tevs __maybe_unused)
1254 if (perf_probe_event_need_dwarf(pev)) {
1255 pr_warning("Debuginfo-analysis is not supported.\n");
1262 int show_line_range(struct line_range *lr __maybe_unused,
1263 const char *module __maybe_unused,
1264 struct nsinfo *nsi __maybe_unused,
1265 bool user __maybe_unused)
1267 pr_warning("Debuginfo-analysis is not supported.\n");
1271 int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
1272 int npevs __maybe_unused,
1273 struct strfilter *filter __maybe_unused)
1275 pr_warning("Debuginfo-analysis is not supported.\n");
1280 void line_range__clear(struct line_range *lr)
1282 zfree(&lr->function);
1285 zfree(&lr->comp_dir);
1286 intlist__delete(lr->line_list);
1289 int line_range__init(struct line_range *lr)
1291 memset(lr, 0, sizeof(*lr));
1292 lr->line_list = intlist__new(NULL);
1299 static int parse_line_num(char **ptr, int *val, const char *what)
1301 const char *start = *ptr;
1304 *val = strtol(*ptr, ptr, 0);
1305 if (errno || *ptr == start) {
1306 semantic_error("'%s' is not a valid number.\n", what);
1312 /* Check the name is good for event, group or function */
1313 static bool is_c_func_name(const char *name)
1315 if (!isalpha(*name) && *name != '_')
1317 while (*++name != '\0') {
1318 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
1325 * Stuff 'lr' according to the line range described by 'arg'.
1326 * The line range syntax is described by:
1328 * SRC[:SLN[+NUM|-ELN]]
1329 * FNC[@SRC][:SLN[+NUM|-ELN]]
1331 int parse_line_range_desc(const char *arg, struct line_range *lr)
1333 char *range, *file, *name = strdup(arg);
1342 range = strchr(name, ':');
1346 err = parse_line_num(&range, &lr->start, "start line");
1350 if (*range == '+' || *range == '-') {
1351 const char c = *range++;
1353 err = parse_line_num(&range, &lr->end, "end line");
1358 lr->end += lr->start;
1360 * Adjust the number of lines here.
1361 * If the number of lines == 1, the
1362 * end of line should be equal to
1363 * the start of line.
1369 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
1372 if (lr->start > lr->end) {
1373 semantic_error("Start line must be smaller"
1374 " than end line.\n");
1377 if (*range != '\0') {
1378 semantic_error("Tailing with invalid str '%s'.\n", range);
1383 file = strchr(name, '@');
1386 lr->file = strdup(++file);
1387 if (lr->file == NULL) {
1391 lr->function = name;
1392 } else if (strchr(name, '/') || strchr(name, '.'))
1394 else if (is_c_func_name(name))/* We reuse it for checking funcname */
1395 lr->function = name;
1396 else { /* Invalid name */
1397 semantic_error("'%s' is not a valid function name.\n", name);
1408 static int parse_perf_probe_event_name(char **arg, struct perf_probe_event *pev)
1412 ptr = strpbrk_esc(*arg, ":");
1415 if (!pev->sdt && !is_c_func_name(*arg))
1417 pev->group = strdup_esc(*arg);
1424 pev->event = strdup_esc(*arg);
1425 if (pev->event == NULL)
1428 if (!pev->sdt && !is_c_func_name(pev->event)) {
1432 semantic_error("%s is bad for event name -it must "
1433 "follow C symbol-naming rule.\n", *arg);
1439 /* Parse probepoint definition. */
1440 static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
1442 struct perf_probe_point *pp = &pev->point;
1445 bool file_spec = false;
1450 * perf probe [GRP:][EVENT=]SRC[:LN|;PTN]
1451 * perf probe [GRP:][EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
1452 * perf probe %[GRP:]SDT_EVENT
1457 if (is_sdt_event(arg)) {
1463 ptr = strpbrk_esc(arg, ";=@+%");
1467 semantic_error("%s must be an SDT name.\n",
1471 /* This must be a target file name or build id */
1472 tmp = build_id_cache__complement(ptr + 1);
1474 pev->target = build_id_cache__origname(tmp);
1477 pev->target = strdup_esc(ptr + 1);
1482 ret = parse_perf_probe_event_name(&arg, pev);
1484 if (asprintf(&pev->point.function, "%%%s", pev->event) < 0)
1490 if (ptr && *ptr == '=') { /* Event name */
1493 ret = parse_perf_probe_event_name(&arg, pev);
1501 * Check arg is function or file name and copy it.
1503 * We consider arg to be a file spec if and only if it satisfies
1504 * all of the below criteria::
1505 * - it does not include any of "+@%",
1506 * - it includes one of ":;", and
1507 * - it has a period '.' in the name.
1509 * Otherwise, we consider arg to be a function specification.
1511 if (!strpbrk_esc(arg, "+@%")) {
1512 ptr = strpbrk_esc(arg, ";:");
1513 /* This is a file spec if it includes a '.' before ; or : */
1514 if (ptr && memchr(arg, '.', ptr - arg))
1518 ptr = strpbrk_esc(arg, ";:+@%");
1527 tmp = strdup_esc(arg);
1538 * Keep pp->function even if this is absolute address,
1539 * so it can mark whether abs_address is valid.
1540 * Which make 'perf probe lib.bin 0x0' possible.
1542 * Note that checking length of tmp is not needed
1543 * because when we access tmp[1] we know tmp[0] is '0',
1544 * so tmp[1] should always valid (but could be '\0').
1546 if (tmp && !strncmp(tmp, "0x", 2)) {
1547 pp->abs_address = strtoull(pp->function, &tmp, 0);
1549 semantic_error("Invalid absolute address.\n");
1555 /* Parse other options */
1559 if (c == ';') { /* Lazy pattern must be the last part */
1560 pp->lazy_line = strdup(arg); /* let leave escapes */
1561 if (pp->lazy_line == NULL)
1565 ptr = strpbrk_esc(arg, ";:+@%");
1571 case ':': /* Line number */
1572 pp->line = strtoul(arg, &tmp, 0);
1574 semantic_error("There is non-digit char"
1575 " in line number.\n");
1579 case '+': /* Byte offset from a symbol */
1580 pp->offset = strtoul(arg, &tmp, 0);
1582 semantic_error("There is non-digit character"
1587 case '@': /* File name */
1589 semantic_error("SRC@SRC is not allowed.\n");
1592 pp->file = strdup_esc(arg);
1593 if (pp->file == NULL)
1596 case '%': /* Probe places */
1597 if (strcmp(arg, "return") == 0) {
1599 } else { /* Others not supported yet */
1600 semantic_error("%%%s is not supported.\n", arg);
1604 default: /* Buggy case */
1605 pr_err("This program has a bug at %s:%d.\n",
1606 __FILE__, __LINE__);
1612 /* Exclusion check */
1613 if (pp->lazy_line && pp->line) {
1614 semantic_error("Lazy pattern can't be used with"
1619 if (pp->lazy_line && pp->offset) {
1620 semantic_error("Lazy pattern can't be used with offset.\n");
1624 if (pp->line && pp->offset) {
1625 semantic_error("Offset can't be used with line number.\n");
1629 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
1630 semantic_error("File always requires line number or "
1635 if (pp->offset && !pp->function) {
1636 semantic_error("Offset requires an entry function.\n");
1640 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
1641 semantic_error("Offset/Line/Lazy pattern can't be used with "
1646 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
1647 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
1652 /* Parse perf-probe event argument */
1653 static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
1655 char *tmp, *goodname;
1656 struct perf_probe_arg_field **fieldp;
1658 pr_debug("parsing arg: %s into ", str);
1660 tmp = strchr(str, '=');
1662 arg->name = strndup(str, tmp - str);
1663 if (arg->name == NULL)
1665 pr_debug("name:%s ", arg->name);
1669 tmp = strchr(str, '@');
1670 if (tmp && tmp != str && !strcmp(tmp + 1, "user")) { /* user attr */
1671 if (!user_access_is_supported()) {
1672 semantic_error("ftrace does not support user access\n");
1676 arg->user_access = true;
1677 pr_debug("user_access ");
1680 tmp = strchr(str, ':');
1681 if (tmp) { /* Type setting */
1683 arg->type = strdup(tmp + 1);
1684 if (arg->type == NULL)
1686 pr_debug("type:%s ", arg->type);
1689 tmp = strpbrk(str, "-.[");
1690 if (!is_c_varname(str) || !tmp) {
1691 /* A variable, register, symbol or special value */
1692 arg->var = strdup(str);
1693 if (arg->var == NULL)
1695 pr_debug("%s\n", arg->var);
1699 /* Structure fields or array element */
1700 arg->var = strndup(str, tmp - str);
1701 if (arg->var == NULL)
1703 goodname = arg->var;
1704 pr_debug("%s, ", arg->var);
1705 fieldp = &arg->field;
1708 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
1709 if (*fieldp == NULL)
1711 if (*tmp == '[') { /* Array */
1713 (*fieldp)->index = strtol(str + 1, &tmp, 0);
1714 (*fieldp)->ref = true;
1715 if (*tmp != ']' || tmp == str + 1) {
1716 semantic_error("Array index must be a"
1723 } else { /* Structure */
1726 (*fieldp)->ref = false;
1727 } else if (tmp[1] == '>') {
1729 (*fieldp)->ref = true;
1731 semantic_error("Argument parse error: %s\n",
1735 tmp = strpbrk(str, "-.[");
1738 (*fieldp)->name = strndup(str, tmp - str);
1739 if ((*fieldp)->name == NULL)
1742 goodname = (*fieldp)->name;
1743 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
1744 fieldp = &(*fieldp)->next;
1747 (*fieldp)->name = strdup(str);
1748 if ((*fieldp)->name == NULL)
1751 goodname = (*fieldp)->name;
1752 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
1754 /* If no name is specified, set the last field name (not array index)*/
1756 arg->name = strdup(goodname);
1757 if (arg->name == NULL)
1763 /* Parse perf-probe event command */
1764 int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
1767 int argc, i, ret = 0;
1769 argv = argv_split(cmd, &argc);
1771 pr_debug("Failed to split arguments.\n");
1774 if (argc - 1 > MAX_PROBE_ARGS) {
1775 semantic_error("Too many probe arguments (%d).\n", argc - 1);
1779 /* Parse probe point */
1780 ret = parse_perf_probe_point(argv[0], pev);
1784 /* Generate event name if needed */
1785 if (!pev->event && pev->point.function && pev->point.line
1786 && !pev->point.lazy_line && !pev->point.offset) {
1787 if (asprintf(&pev->event, "%s_L%d", pev->point.function,
1788 pev->point.line) < 0) {
1794 /* Copy arguments and ensure return probe has no C argument */
1795 pev->nargs = argc - 1;
1796 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1797 if (pev->args == NULL) {
1801 for (i = 0; i < pev->nargs && ret >= 0; i++) {
1802 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1804 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
1805 semantic_error("You can't specify local variable for"
1816 /* Returns true if *any* ARG is either C variable, $params or $vars. */
1817 bool perf_probe_with_var(struct perf_probe_event *pev)
1821 for (i = 0; i < pev->nargs; i++)
1822 if (is_c_varname(pev->args[i].var) ||
1823 !strcmp(pev->args[i].var, PROBE_ARG_PARAMS) ||
1824 !strcmp(pev->args[i].var, PROBE_ARG_VARS))
1829 /* Return true if this perf_probe_event requires debuginfo */
1830 bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
1832 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1835 if (perf_probe_with_var(pev))
1841 /* Parse probe_events event into struct probe_point */
1842 int parse_probe_trace_command(const char *cmd, struct probe_trace_event *tev)
1844 struct probe_trace_point *tp = &tev->point;
1847 char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
1851 pr_debug("Parsing probe_events: %s\n", cmd);
1852 argv = argv_split(cmd, &argc);
1854 pr_debug("Failed to split arguments.\n");
1858 semantic_error("Too few probe arguments.\n");
1863 /* Scan event and group name. */
1864 argv0_str = strdup(argv[0]);
1865 if (argv0_str == NULL) {
1869 fmt1_str = strtok_r(argv0_str, ":", &fmt);
1870 fmt2_str = strtok_r(NULL, "/", &fmt);
1871 fmt3_str = strtok_r(NULL, " \t", &fmt);
1872 if (fmt1_str == NULL || fmt2_str == NULL || fmt3_str == NULL) {
1873 semantic_error("Failed to parse event name: %s\n", argv[0]);
1878 tev->group = strdup(fmt2_str);
1879 tev->event = strdup(fmt3_str);
1880 if (tev->group == NULL || tev->event == NULL) {
1884 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
1886 tp->retprobe = (pr == 'r');
1888 /* Scan module name(if there), function name and offset */
1889 p = strchr(argv[1], ':');
1891 tp->module = strndup(argv[1], p - argv[1]);
1896 tev->uprobes = (tp->module[0] == '/');
1900 fmt1_str = strtok_r(p, "+", &fmt);
1901 /* only the address started with 0x */
1902 if (fmt1_str[0] == '0') {
1904 * Fix a special case:
1905 * if address == 0, kernel reports something like:
1906 * p:probe_libc/abs_0 /lib/libc-2.18.so:0x (null) arg1=%ax
1907 * Newer kernel may fix that, but we want to
1908 * support old kernel also.
1910 if (strcmp(fmt1_str, "0x") == 0) {
1911 if (!argv[2] || strcmp(argv[2], "(null)")) {
1918 for (i = 2; argv[i + 1] != NULL; i++)
1919 argv[i] = argv[i + 1];
1924 tp->address = strtoull(fmt1_str, NULL, 0);
1926 /* Only the symbol-based probe has offset */
1927 tp->symbol = strdup(fmt1_str);
1928 if (tp->symbol == NULL) {
1932 fmt2_str = strtok_r(NULL, "", &fmt);
1933 if (fmt2_str == NULL)
1936 tp->offset = strtoul(fmt2_str, NULL, 10);
1940 fmt2_str = strchr(p, '(');
1942 tp->ref_ctr_offset = strtoul(fmt2_str + 1, NULL, 0);
1945 tev->nargs = argc - 2;
1946 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
1947 if (tev->args == NULL) {
1951 for (i = 0; i < tev->nargs; i++) {
1952 p = strchr(argv[i + 2], '=');
1953 if (p) /* We don't need which register is assigned. */
1957 tev->args[i].name = strdup(argv[i + 2]);
1958 /* TODO: parse regs and offset */
1959 tev->args[i].value = strdup(p);
1960 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1972 /* Compose only probe arg */
1973 char *synthesize_perf_probe_arg(struct perf_probe_arg *pa)
1975 struct perf_probe_arg_field *field = pa->field;
1980 if (strbuf_init(&buf, 64) < 0)
1983 if (pa->name && pa->var)
1984 err = strbuf_addf(&buf, "%s=%s", pa->name, pa->var);
1986 err = strbuf_addstr(&buf, pa->name ?: pa->var);
1991 if (field->name[0] == '[')
1992 err = strbuf_addstr(&buf, field->name);
1994 err = strbuf_addf(&buf, "%s%s", field->ref ? "->" : ".",
1996 field = field->next;
2002 if (strbuf_addf(&buf, ":%s", pa->type) < 0)
2005 ret = strbuf_detach(&buf, NULL);
2007 strbuf_release(&buf);
2011 /* Compose only probe point (not argument) */
2012 char *synthesize_perf_probe_point(struct perf_probe_point *pp)
2015 char *tmp, *ret = NULL;
2018 if (strbuf_init(&buf, 64) < 0)
2022 if (strbuf_addstr(&buf, pp->function) < 0)
2025 err = strbuf_addf(&buf, "+%lu", pp->offset);
2027 err = strbuf_addf(&buf, ":%d", pp->line);
2028 else if (pp->retprobe)
2029 err = strbuf_addstr(&buf, "%return");
2037 tmp = strchr(pp->file + len - 30, '/');
2038 tmp = tmp ? tmp + 1 : pp->file + len - 30;
2040 err = strbuf_addf(&buf, "@%s", tmp);
2041 if (!err && !pp->function && pp->line)
2042 err = strbuf_addf(&buf, ":%d", pp->line);
2045 ret = strbuf_detach(&buf, NULL);
2047 strbuf_release(&buf);
2051 char *synthesize_perf_probe_command(struct perf_probe_event *pev)
2054 char *tmp, *ret = NULL;
2057 if (strbuf_init(&buf, 64))
2060 if (strbuf_addf(&buf, "%s:%s=", pev->group ?: PERFPROBE_GROUP,
2064 tmp = synthesize_perf_probe_point(&pev->point);
2065 if (!tmp || strbuf_addstr(&buf, tmp) < 0)
2069 for (i = 0; i < pev->nargs; i++) {
2070 tmp = synthesize_perf_probe_arg(pev->args + i);
2071 if (!tmp || strbuf_addf(&buf, " %s", tmp) < 0)
2076 ret = strbuf_detach(&buf, NULL);
2078 strbuf_release(&buf);
2082 static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
2083 struct strbuf *buf, int depth)
2087 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
2092 if (ref->user_access)
2093 err = strbuf_addf(buf, "%s%ld(", "+u", ref->offset);
2095 err = strbuf_addf(buf, "%+ld(", ref->offset);
2096 return (err < 0) ? err : depth;
2099 static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
2102 struct probe_trace_arg_ref *ref = arg->ref;
2105 /* Argument name or separator */
2107 err = strbuf_addf(buf, " %s=", arg->name);
2109 err = strbuf_addch(buf, ' ');
2113 /* Special case: @XXX */
2114 if (arg->value[0] == '@' && arg->ref)
2117 /* Dereferencing arguments */
2119 depth = __synthesize_probe_trace_arg_ref(ref, buf, 1);
2124 /* Print argument value */
2125 if (arg->value[0] == '@' && arg->ref)
2126 err = strbuf_addf(buf, "%s%+ld", arg->value, arg->ref->offset);
2128 err = strbuf_addstr(buf, arg->value);
2131 while (!err && depth--)
2132 err = strbuf_addch(buf, ')');
2134 /* Print argument type */
2135 if (!err && arg->type)
2136 err = strbuf_addf(buf, ":%s", arg->type);
2142 synthesize_probe_trace_args(struct probe_trace_event *tev, struct strbuf *buf)
2146 for (i = 0; i < tev->nargs && ret >= 0; i++)
2147 ret = synthesize_probe_trace_arg(&tev->args[i], buf);
2153 synthesize_uprobe_trace_def(struct probe_trace_point *tp, struct strbuf *buf)
2157 /* Uprobes must have tp->module */
2161 * If tp->address == 0, then this point must be a
2162 * absolute address uprobe.
2163 * try_to_find_absolute_address() should have made
2164 * tp->symbol to "0x0".
2166 if (!tp->address && (!tp->symbol || strcmp(tp->symbol, "0x0")))
2169 /* Use the tp->address for uprobes */
2170 err = strbuf_addf(buf, "%s:0x%" PRIx64, tp->module, tp->address);
2172 if (err >= 0 && tp->ref_ctr_offset) {
2173 if (!uprobe_ref_ctr_is_supported())
2175 err = strbuf_addf(buf, "(0x%lx)", tp->ref_ctr_offset);
2177 return err >= 0 ? 0 : err;
2181 synthesize_kprobe_trace_def(struct probe_trace_point *tp, struct strbuf *buf)
2183 if (!strncmp(tp->symbol, "0x", 2)) {
2184 /* Absolute address. See try_to_find_absolute_address() */
2185 return strbuf_addf(buf, "%s%s0x%" PRIx64, tp->module ?: "",
2186 tp->module ? ":" : "", tp->address);
2188 return strbuf_addf(buf, "%s%s%s+%lu", tp->module ?: "",
2189 tp->module ? ":" : "", tp->symbol, tp->offset);
2193 char *synthesize_probe_trace_command(struct probe_trace_event *tev)
2195 struct probe_trace_point *tp = &tev->point;
2200 if (strbuf_init(&buf, 32) < 0)
2203 if (strbuf_addf(&buf, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
2204 tev->group, tev->event) < 0)
2208 err = synthesize_uprobe_trace_def(tp, &buf);
2210 err = synthesize_kprobe_trace_def(tp, &buf);
2213 err = synthesize_probe_trace_args(tev, &buf);
2216 ret = strbuf_detach(&buf, NULL);
2218 strbuf_release(&buf);
2222 static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
2223 struct perf_probe_point *pp,
2226 struct symbol *sym = NULL;
2227 struct map *map = NULL;
2228 u64 addr = tp->address;
2232 map = dso__new_map(tp->module);
2235 sym = map__find_symbol(map, addr);
2237 if (tp->symbol && !addr) {
2238 if (kernel_get_symbol_address_by_name(tp->symbol,
2239 &addr, true, false) < 0)
2244 sym = machine__find_kernel_symbol(host_machine, addr, &map);
2251 pp->retprobe = tp->retprobe;
2252 pp->offset = addr - map__unmap_ip(map, sym->start);
2253 pp->function = strdup(sym->name);
2254 ret = pp->function ? 0 : -ENOMEM;
2257 if (map && !is_kprobe) {
2264 static int convert_to_perf_probe_point(struct probe_trace_point *tp,
2265 struct perf_probe_point *pp,
2271 ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe);
2274 ret = find_perf_probe_point_from_map(tp, pp, is_kprobe);
2278 pr_debug("Failed to find probe point from both of dwarf and map.\n");
2281 pp->function = strdup(tp->symbol);
2282 pp->offset = tp->offset;
2284 ret = e_snprintf(buf, 128, "0x%" PRIx64, tp->address);
2287 pp->function = strdup(buf);
2290 if (pp->function == NULL)
2293 pp->retprobe = tp->retprobe;
2298 static int convert_to_perf_probe_event(struct probe_trace_event *tev,
2299 struct perf_probe_event *pev, bool is_kprobe)
2301 struct strbuf buf = STRBUF_INIT;
2304 /* Convert event/group name */
2305 pev->event = strdup(tev->event);
2306 pev->group = strdup(tev->group);
2307 if (pev->event == NULL || pev->group == NULL)
2310 /* Convert trace_point to probe_point */
2311 ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
2315 /* Convert trace_arg to probe_arg */
2316 pev->nargs = tev->nargs;
2317 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
2318 if (pev->args == NULL)
2320 for (i = 0; i < tev->nargs && ret >= 0; i++) {
2321 if (tev->args[i].name)
2322 pev->args[i].name = strdup(tev->args[i].name);
2324 if ((ret = strbuf_init(&buf, 32)) < 0)
2326 ret = synthesize_probe_trace_arg(&tev->args[i], &buf);
2327 pev->args[i].name = strbuf_detach(&buf, NULL);
2329 if (pev->args[i].name == NULL && ret >= 0)
2334 clear_perf_probe_event(pev);
2339 void clear_perf_probe_event(struct perf_probe_event *pev)
2341 struct perf_probe_arg_field *field, *next;
2346 zfree(&pev->target);
2347 clear_perf_probe_point(&pev->point);
2349 for (i = 0; i < pev->nargs; i++) {
2350 zfree(&pev->args[i].name);
2351 zfree(&pev->args[i].var);
2352 zfree(&pev->args[i].type);
2353 field = pev->args[i].field;
2356 zfree(&field->name);
2365 #define strdup_or_goto(str, label) \
2366 ({ char *__p = NULL; if (str && !(__p = strdup(str))) goto label; __p; })
2368 static int perf_probe_point__copy(struct perf_probe_point *dst,
2369 struct perf_probe_point *src)
2371 dst->file = strdup_or_goto(src->file, out_err);
2372 dst->function = strdup_or_goto(src->function, out_err);
2373 dst->lazy_line = strdup_or_goto(src->lazy_line, out_err);
2374 dst->line = src->line;
2375 dst->retprobe = src->retprobe;
2376 dst->offset = src->offset;
2380 clear_perf_probe_point(dst);
2384 static int perf_probe_arg__copy(struct perf_probe_arg *dst,
2385 struct perf_probe_arg *src)
2387 struct perf_probe_arg_field *field, **ppfield;
2389 dst->name = strdup_or_goto(src->name, out_err);
2390 dst->var = strdup_or_goto(src->var, out_err);
2391 dst->type = strdup_or_goto(src->type, out_err);
2394 ppfield = &(dst->field);
2396 *ppfield = zalloc(sizeof(*field));
2399 (*ppfield)->name = strdup_or_goto(field->name, out_err);
2400 (*ppfield)->index = field->index;
2401 (*ppfield)->ref = field->ref;
2402 field = field->next;
2403 ppfield = &((*ppfield)->next);
2410 int perf_probe_event__copy(struct perf_probe_event *dst,
2411 struct perf_probe_event *src)
2415 dst->event = strdup_or_goto(src->event, out_err);
2416 dst->group = strdup_or_goto(src->group, out_err);
2417 dst->target = strdup_or_goto(src->target, out_err);
2418 dst->uprobes = src->uprobes;
2420 if (perf_probe_point__copy(&dst->point, &src->point) < 0)
2423 dst->args = zalloc(sizeof(struct perf_probe_arg) * src->nargs);
2426 dst->nargs = src->nargs;
2428 for (i = 0; i < src->nargs; i++)
2429 if (perf_probe_arg__copy(&dst->args[i], &src->args[i]) < 0)
2434 clear_perf_probe_event(dst);
2438 void clear_probe_trace_event(struct probe_trace_event *tev)
2440 struct probe_trace_arg_ref *ref, *next;
2445 zfree(&tev->point.symbol);
2446 zfree(&tev->point.realname);
2447 zfree(&tev->point.module);
2448 for (i = 0; i < tev->nargs; i++) {
2449 zfree(&tev->args[i].name);
2450 zfree(&tev->args[i].value);
2451 zfree(&tev->args[i].type);
2452 ref = tev->args[i].ref;
2463 struct kprobe_blacklist_node {
2464 struct list_head list;
2470 static void kprobe_blacklist__delete(struct list_head *blacklist)
2472 struct kprobe_blacklist_node *node;
2474 while (!list_empty(blacklist)) {
2475 node = list_first_entry(blacklist,
2476 struct kprobe_blacklist_node, list);
2477 list_del_init(&node->list);
2478 zfree(&node->symbol);
2483 static int kprobe_blacklist__load(struct list_head *blacklist)
2485 struct kprobe_blacklist_node *node;
2486 const char *__debugfs = debugfs__mountpoint();
2487 char buf[PATH_MAX], *p;
2491 if (__debugfs == NULL)
2494 ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs);
2498 fp = fopen(buf, "r");
2503 while (fgets(buf, PATH_MAX, fp)) {
2504 node = zalloc(sizeof(*node));
2509 INIT_LIST_HEAD(&node->list);
2510 list_add_tail(&node->list, blacklist);
2511 if (sscanf(buf, "0x%" PRIx64 "-0x%" PRIx64, &node->start, &node->end) != 2) {
2515 p = strchr(buf, '\t');
2518 if (p[strlen(p) - 1] == '\n')
2519 p[strlen(p) - 1] = '\0';
2521 p = (char *)"unknown";
2522 node->symbol = strdup(p);
2523 if (!node->symbol) {
2527 pr_debug2("Blacklist: 0x%" PRIx64 "-0x%" PRIx64 ", %s\n",
2528 node->start, node->end, node->symbol);
2532 kprobe_blacklist__delete(blacklist);
2538 static struct kprobe_blacklist_node *
2539 kprobe_blacklist__find_by_address(struct list_head *blacklist, u64 address)
2541 struct kprobe_blacklist_node *node;
2543 list_for_each_entry(node, blacklist, list) {
2544 if (node->start <= address && address < node->end)
2551 static LIST_HEAD(kprobe_blacklist);
2553 static void kprobe_blacklist__init(void)
2555 if (!list_empty(&kprobe_blacklist))
2558 if (kprobe_blacklist__load(&kprobe_blacklist) < 0)
2559 pr_debug("No kprobe blacklist support, ignored\n");
2562 static void kprobe_blacklist__release(void)
2564 kprobe_blacklist__delete(&kprobe_blacklist);
2567 static bool kprobe_blacklist__listed(u64 address)
2569 return !!kprobe_blacklist__find_by_address(&kprobe_blacklist, address);
2572 static int perf_probe_event__sprintf(const char *group, const char *event,
2573 struct perf_probe_event *pev,
2575 struct strbuf *result)
2580 if (asprintf(&buf, "%s:%s", group, event) < 0)
2582 ret = strbuf_addf(result, " %-20s (on ", buf);
2587 /* Synthesize only event probe point */
2588 buf = synthesize_perf_probe_point(&pev->point);
2591 ret = strbuf_addstr(result, buf);
2595 ret = strbuf_addf(result, " in %s", module);
2597 if (!ret && pev->nargs > 0) {
2598 ret = strbuf_add(result, " with", 5);
2599 for (i = 0; !ret && i < pev->nargs; i++) {
2600 buf = synthesize_perf_probe_arg(&pev->args[i]);
2603 ret = strbuf_addf(result, " %s", buf);
2608 ret = strbuf_addch(result, ')');
2614 int show_perf_probe_event(const char *group, const char *event,
2615 struct perf_probe_event *pev,
2616 const char *module, bool use_stdout)
2618 struct strbuf buf = STRBUF_INIT;
2621 ret = perf_probe_event__sprintf(group, event, pev, module, &buf);
2624 printf("%s\n", buf.buf);
2626 pr_info("%s\n", buf.buf);
2628 strbuf_release(&buf);
2633 static bool filter_probe_trace_event(struct probe_trace_event *tev,
2634 struct strfilter *filter)
2638 /* At first, check the event name itself */
2639 if (strfilter__compare(filter, tev->event))
2642 /* Next, check the combination of name and group */
2643 if (e_snprintf(tmp, 128, "%s:%s", tev->group, tev->event) < 0)
2645 return strfilter__compare(filter, tmp);
2648 static int __show_perf_probe_events(int fd, bool is_kprobe,
2649 struct strfilter *filter)
2652 struct probe_trace_event tev;
2653 struct perf_probe_event pev;
2654 struct strlist *rawlist;
2655 struct str_node *ent;
2657 memset(&tev, 0, sizeof(tev));
2658 memset(&pev, 0, sizeof(pev));
2660 rawlist = probe_file__get_rawlist(fd);
2664 strlist__for_each_entry(ent, rawlist) {
2665 ret = parse_probe_trace_command(ent->s, &tev);
2667 if (!filter_probe_trace_event(&tev, filter))
2669 ret = convert_to_perf_probe_event(&tev, &pev,
2673 ret = show_perf_probe_event(pev.group, pev.event,
2674 &pev, tev.point.module,
2678 clear_perf_probe_event(&pev);
2679 clear_probe_trace_event(&tev);
2683 strlist__delete(rawlist);
2684 /* Cleanup cached debuginfo if needed */
2685 debuginfo_cache__exit();
2690 /* List up current perf-probe events */
2691 int show_perf_probe_events(struct strfilter *filter)
2693 int kp_fd, up_fd, ret;
2697 if (probe_conf.cache)
2698 return probe_cache__show_all_caches(filter);
2700 ret = init_probe_symbol_maps(false);
2704 ret = probe_file__open_both(&kp_fd, &up_fd, 0);
2709 ret = __show_perf_probe_events(kp_fd, true, filter);
2710 if (up_fd >= 0 && ret >= 0)
2711 ret = __show_perf_probe_events(up_fd, false, filter);
2716 exit_probe_symbol_maps();
2721 static int get_new_event_name(char *buf, size_t len, const char *base,
2722 struct strlist *namelist, bool ret_event,
2730 nbase = strdup(base);
2734 /* Cut off the dot suffixes (e.g. .const, .isra) and version suffixes */
2735 p = strpbrk(nbase, ".@");
2736 if (p && p != nbase)
2739 /* Try no suffix number */
2740 ret = e_snprintf(buf, len, "%s%s", nbase, ret_event ? "__return" : "");
2742 pr_debug("snprintf() failed: %d\n", ret);
2745 if (!strlist__has_entry(namelist, buf))
2748 if (!allow_suffix) {
2749 pr_warning("Error: event \"%s\" already exists.\n"
2750 " Hint: Remove existing event by 'perf probe -d'\n"
2751 " or force duplicates by 'perf probe -f'\n"
2752 " or set 'force=yes' in BPF source.\n",
2758 /* Try to add suffix */
2759 for (i = 1; i < MAX_EVENT_INDEX; i++) {
2760 ret = e_snprintf(buf, len, "%s_%d", nbase, i);
2762 pr_debug("snprintf() failed: %d\n", ret);
2765 if (!strlist__has_entry(namelist, buf))
2768 if (i == MAX_EVENT_INDEX) {
2769 pr_warning("Too many events are on the same function.\n");
2776 /* Final validation */
2777 if (ret >= 0 && !is_c_func_name(buf)) {
2778 pr_warning("Internal error: \"%s\" is an invalid event name.\n",
2786 /* Warn if the current kernel's uprobe implementation is old */
2787 static void warn_uprobe_event_compat(struct probe_trace_event *tev)
2790 char *buf = synthesize_probe_trace_command(tev);
2791 struct probe_trace_point *tp = &tev->point;
2793 if (tp->ref_ctr_offset && !uprobe_ref_ctr_is_supported()) {
2794 pr_warning("A semaphore is associated with %s:%s and "
2795 "seems your kernel doesn't support it.\n",
2796 tev->group, tev->event);
2799 /* Old uprobe event doesn't support memory dereference */
2800 if (!tev->uprobes || tev->nargs == 0 || !buf)
2803 for (i = 0; i < tev->nargs; i++)
2804 if (strglobmatch(tev->args[i].value, "[$@+-]*")) {
2805 pr_warning("Please upgrade your kernel to at least "
2806 "3.14 to have access to feature %s\n",
2807 tev->args[i].value);
2814 /* Set new name from original perf_probe_event and namelist */
2815 static int probe_trace_event__set_name(struct probe_trace_event *tev,
2816 struct perf_probe_event *pev,
2817 struct strlist *namelist,
2820 const char *event, *group;
2824 /* If probe_event or trace_event already have the name, reuse it */
2825 if (pev->event && !pev->sdt)
2827 else if (tev->event)
2830 /* Or generate new one from probe point */
2831 if (pev->point.function &&
2832 (strncmp(pev->point.function, "0x", 2) != 0) &&
2833 !strisglob(pev->point.function))
2834 event = pev->point.function;
2836 event = tev->point.realname;
2838 if (pev->group && !pev->sdt)
2840 else if (tev->group)
2843 group = PERFPROBE_GROUP;
2845 /* Get an unused new event name */
2846 ret = get_new_event_name(buf, 64, event, namelist,
2847 tev->point.retprobe, allow_suffix);
2853 tev->event = strdup(event);
2854 tev->group = strdup(group);
2855 if (tev->event == NULL || tev->group == NULL)
2859 * Add new event name to namelist if multiprobe event is NOT
2860 * supported, since we have to use new event name for following
2861 * probes in that case.
2863 if (!multiprobe_event_is_supported())
2864 strlist__add(namelist, event);
2868 static int __open_probe_file_and_namelist(bool uprobe,
2869 struct strlist **namelist)
2873 fd = probe_file__open(PF_FL_RW | (uprobe ? PF_FL_UPROBE : 0));
2877 /* Get current event names */
2878 *namelist = probe_file__get_namelist(fd);
2880 pr_debug("Failed to get current event list.\n");
2887 static int __add_probe_trace_events(struct perf_probe_event *pev,
2888 struct probe_trace_event *tevs,
2889 int ntevs, bool allow_suffix)
2891 int i, fd[2] = {-1, -1}, up, ret;
2892 struct probe_trace_event *tev = NULL;
2893 struct probe_cache *cache = NULL;
2894 struct strlist *namelist[2] = {NULL, NULL};
2895 struct nscookie nsc;
2897 up = pev->uprobes ? 1 : 0;
2898 fd[up] = __open_probe_file_and_namelist(up, &namelist[up]);
2903 for (i = 0; i < ntevs; i++) {
2905 up = tev->uprobes ? 1 : 0;
2906 if (fd[up] == -1) { /* Open the kprobe/uprobe_events */
2907 fd[up] = __open_probe_file_and_namelist(up,
2912 /* Skip if the symbol is out of .text or blacklisted */
2913 if (!tev->point.symbol && !pev->uprobes)
2916 /* Set new name for tev (and update namelist) */
2917 ret = probe_trace_event__set_name(tev, pev, namelist[up],
2922 nsinfo__mountns_enter(pev->nsi, &nsc);
2923 ret = probe_file__add_event(fd[up], tev);
2924 nsinfo__mountns_exit(&nsc);
2929 * Probes after the first probe which comes from same
2930 * user input are always allowed to add suffix, because
2931 * there might be several addresses corresponding to
2934 allow_suffix = true;
2936 if (ret == -EINVAL && pev->uprobes)
2937 warn_uprobe_event_compat(tev);
2938 if (ret == 0 && probe_conf.cache) {
2939 cache = probe_cache__new(pev->target, pev->nsi);
2941 probe_cache__add_entry(cache, pev, tevs, ntevs) < 0 ||
2942 probe_cache__commit(cache) < 0)
2943 pr_warning("Failed to add event to probe cache\n");
2944 probe_cache__delete(cache);
2948 for (up = 0; up < 2; up++) {
2949 strlist__delete(namelist[up]);
2956 static int find_probe_functions(struct map *map, char *name,
2957 struct symbol **syms)
2961 struct rb_node *tmp;
2962 const char *norm, *ver;
2964 bool cut_version = true;
2966 if (map__load(map) < 0)
2967 return -EACCES; /* Possible permission error to load symbols */
2969 /* If user gives a version, don't cut off the version from symbols */
2970 if (strchr(name, '@'))
2971 cut_version = false;
2973 map__for_each_symbol(map, sym, tmp) {
2974 norm = arch__normalize_symbol_name(sym->name);
2979 /* We don't care about default symbol or not */
2980 ver = strchr(norm, '@');
2982 buf = strndup(norm, ver - norm);
2989 if (strglobmatch(norm, name)) {
2991 if (syms && found < probe_conf.max_probes)
2992 syms[found - 1] = sym;
3001 void __weak arch__fix_tev_from_maps(struct perf_probe_event *pev __maybe_unused,
3002 struct probe_trace_event *tev __maybe_unused,
3003 struct map *map __maybe_unused,
3004 struct symbol *sym __maybe_unused) { }
3007 static void pr_kallsyms_access_error(void)
3009 pr_err("Please ensure you can read the /proc/kallsyms symbol addresses.\n"
3010 "If /proc/sys/kernel/kptr_restrict is '2', you can not read\n"
3011 "kernel symbol addresses even if you are a superuser. Please change\n"
3012 "it to '1'. If kptr_restrict is '1', the superuser can read the\n"
3013 "symbol addresses.\n"
3014 "In that case, please run this command again with sudo.\n");
3018 * Find probe function addresses from map.
3019 * Return an error or the number of found probe_trace_event
3021 static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
3022 struct probe_trace_event **tevs)
3024 struct map *map = NULL;
3025 struct ref_reloc_sym *reloc_sym = NULL;
3027 struct symbol **syms = NULL;
3028 struct probe_trace_event *tev;
3029 struct perf_probe_point *pp = &pev->point;
3030 struct probe_trace_point *tp;
3031 int num_matched_functions;
3032 int ret, i, j, skipped = 0;
3035 map = get_target_map(pev->target, pev->nsi, pev->uprobes);
3041 syms = malloc(sizeof(struct symbol *) * probe_conf.max_probes);
3048 * Load matched symbols: Since the different local symbols may have
3049 * same name but different addresses, this lists all the symbols.
3051 num_matched_functions = find_probe_functions(map, pp->function, syms);
3052 if (num_matched_functions <= 0) {
3053 if (num_matched_functions == -EACCES) {
3054 pr_err("Failed to load symbols from %s\n",
3055 pev->target ?: "/proc/kallsyms");
3057 pr_err("Please ensure the file is not stripped.\n");
3059 pr_kallsyms_access_error();
3061 pr_err("Failed to find symbol %s in %s\n", pp->function,
3062 pev->target ? : "kernel");
3065 } else if (num_matched_functions > probe_conf.max_probes) {
3066 pr_err("Too many functions matched in %s\n",
3067 pev->target ? : "kernel");
3072 /* Note that the symbols in the kmodule are not relocated */
3073 if (!pev->uprobes && !pev->target &&
3074 (!pp->retprobe || kretprobe_offset_is_supported())) {
3075 reloc_sym = kernel_get_ref_reloc_sym(NULL);
3077 pr_warning("Relocated base symbol is not found! "
3078 "Check /proc/sys/kernel/kptr_restrict\n"
3079 "and /proc/sys/kernel/perf_event_paranoid. "
3080 "Or run as privileged perf user.\n\n");
3086 /* Setup result trace-probe-events */
3087 *tevs = zalloc(sizeof(*tev) * num_matched_functions);
3095 for (j = 0; j < num_matched_functions; j++) {
3098 if (sym->type != STT_FUNC)
3101 /* There can be duplicated symbols in the map */
3102 for (i = 0; i < j; i++)
3103 if (sym->start == syms[i]->start) {
3104 pr_debug("Found duplicated symbol %s @ %" PRIx64 "\n",
3105 sym->name, sym->start);
3111 tev = (*tevs) + ret;
3113 if (ret == num_matched_functions) {
3114 pr_warning("Too many symbols are listed. Skip it.\n");
3119 if (pp->offset > sym->end - sym->start) {
3120 pr_warning("Offset %ld is bigger than the size of %s\n",
3121 pp->offset, sym->name);
3125 /* Add one probe point */
3126 tp->address = map__unmap_ip(map, sym->start) + pp->offset;
3128 /* Check the kprobe (not in module) is within .text */
3129 if (!pev->uprobes && !pev->target &&
3130 kprobe_warn_out_range(sym->name, tp->address)) {
3131 tp->symbol = NULL; /* Skip it */
3133 } else if (reloc_sym) {
3134 tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
3135 tp->offset = tp->address - reloc_sym->addr;
3137 tp->symbol = strdup_or_goto(sym->name, nomem_out);
3138 tp->offset = pp->offset;
3140 tp->realname = strdup_or_goto(sym->name, nomem_out);
3142 tp->retprobe = pp->retprobe;
3145 tev->point.module = strdup_or_goto(pev->target,
3148 mod_name = find_module_name(pev->target);
3150 strdup(mod_name ? mod_name : pev->target);
3152 if (!tev->point.module)
3156 tev->uprobes = pev->uprobes;
3157 tev->nargs = pev->nargs;
3159 tev->args = zalloc(sizeof(struct probe_trace_arg) *
3161 if (tev->args == NULL)
3164 for (i = 0; i < tev->nargs; i++) {
3165 if (pev->args[i].name)
3167 strdup_or_goto(pev->args[i].name,
3170 tev->args[i].value = strdup_or_goto(pev->args[i].var,
3172 if (pev->args[i].type)
3174 strdup_or_goto(pev->args[i].type,
3177 arch__fix_tev_from_maps(pev, tev, map, sym);
3179 if (ret == skipped) {
3192 clear_probe_trace_events(*tevs, num_matched_functions);
3197 static int try_to_find_absolute_address(struct perf_probe_event *pev,
3198 struct probe_trace_event **tevs)
3200 struct perf_probe_point *pp = &pev->point;
3201 struct probe_trace_event *tev;
3202 struct probe_trace_point *tp;
3205 if (!(pev->point.function && !strncmp(pev->point.function, "0x", 2)))
3207 if (perf_probe_event_need_dwarf(pev))
3211 * This is 'perf probe /lib/libc.so 0xabcd'. Try to probe at
3214 * Only one tev can be generated by this.
3216 *tevs = zalloc(sizeof(*tev));
3224 * Don't use tp->offset, use address directly, because
3225 * in synthesize_probe_trace_command() address cannot be
3228 tp->address = pev->point.abs_address;
3229 tp->retprobe = pp->retprobe;
3230 tev->uprobes = pev->uprobes;
3234 * Give it a '0x' leading symbol name.
3235 * In __add_probe_trace_events, a NULL symbol is interpreted as
3238 if (asprintf(&tp->symbol, "0x%" PRIx64, tp->address) < 0)
3241 /* For kprobe, check range */
3242 if ((!tev->uprobes) &&
3243 (kprobe_warn_out_range(tev->point.symbol,
3244 tev->point.address))) {
3249 if (asprintf(&tp->realname, "abs_%" PRIx64, tp->address) < 0)
3253 tp->module = strdup(pev->target);
3259 tev->group = strdup(pev->group);
3265 tev->event = strdup(pev->event);
3270 tev->nargs = pev->nargs;
3271 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
3275 for (i = 0; i < tev->nargs; i++)
3276 copy_to_probe_trace_arg(&tev->args[i], &pev->args[i]);
3281 clear_probe_trace_events(*tevs, 1);
3286 /* Concatenate two arrays */
3287 static void *memcat(void *a, size_t sz_a, void *b, size_t sz_b)
3291 ret = malloc(sz_a + sz_b);
3293 memcpy(ret, a, sz_a);
3294 memcpy(ret + sz_a, b, sz_b);
3300 concat_probe_trace_events(struct probe_trace_event **tevs, int *ntevs,
3301 struct probe_trace_event **tevs2, int ntevs2)
3303 struct probe_trace_event *new_tevs;
3313 if (*ntevs + ntevs2 > probe_conf.max_probes)
3316 /* Concatenate the array of probe_trace_event */
3317 new_tevs = memcat(*tevs, (*ntevs) * sizeof(**tevs),
3318 *tevs2, ntevs2 * sizeof(**tevs2));
3328 clear_probe_trace_events(*tevs2, ntevs2);
3335 * Try to find probe_trace_event from given probe caches. Return the number
3336 * of cached events found, if an error occurs return the error.
3338 static int find_cached_events(struct perf_probe_event *pev,
3339 struct probe_trace_event **tevs,
3342 struct probe_cache *cache;
3343 struct probe_cache_entry *entry;
3344 struct probe_trace_event *tmp_tevs = NULL;
3348 cache = probe_cache__new(target, pev->nsi);
3349 /* Return 0 ("not found") if the target has no probe cache. */
3353 for_each_probe_cache_entry(entry, cache) {
3354 /* Skip the cache entry which has no name */
3355 if (!entry->pev.event || !entry->pev.group)
3357 if ((!pev->group || strglobmatch(entry->pev.group, pev->group)) &&
3358 strglobmatch(entry->pev.event, pev->event)) {
3359 ret = probe_cache_entry__get_event(entry, &tmp_tevs);
3361 ret = concat_probe_trace_events(tevs, &ntevs,
3367 probe_cache__delete(cache);
3369 clear_probe_trace_events(*tevs, ntevs);
3373 if (ntevs > 0 && target && target[0] == '/')
3374 pev->uprobes = true;
3380 /* Try to find probe_trace_event from all probe caches */
3381 static int find_cached_events_all(struct perf_probe_event *pev,
3382 struct probe_trace_event **tevs)
3384 struct probe_trace_event *tmp_tevs = NULL;
3385 struct strlist *bidlist;
3386 struct str_node *nd;
3391 /* Get the buildid list of all valid caches */
3392 bidlist = build_id_cache__list_all(true);
3395 pr_debug("Failed to get buildids: %d\n", ret);
3400 strlist__for_each_entry(nd, bidlist) {
3401 pathname = build_id_cache__origname(nd->s);
3402 ret = find_cached_events(pev, &tmp_tevs, pathname);
3403 /* In the case of cnt == 0, we just skip it */
3405 ret = concat_probe_trace_events(tevs, &ntevs,
3411 strlist__delete(bidlist);
3414 clear_probe_trace_events(*tevs, ntevs);
3422 static int find_probe_trace_events_from_cache(struct perf_probe_event *pev,
3423 struct probe_trace_event **tevs)
3425 struct probe_cache *cache;
3426 struct probe_cache_entry *entry;
3427 struct probe_trace_event *tev;
3428 struct str_node *node;
3432 /* For SDT/cached events, we use special search functions */
3434 return find_cached_events_all(pev, tevs);
3436 return find_cached_events(pev, tevs, pev->target);
3438 cache = probe_cache__new(pev->target, pev->nsi);
3442 entry = probe_cache__find(cache, pev);
3444 /* SDT must be in the cache */
3445 ret = pev->sdt ? -ENOENT : 0;
3449 ret = strlist__nr_entries(entry->tevlist);
3450 if (ret > probe_conf.max_probes) {
3451 pr_debug("Too many entries matched in the cache of %s\n",
3452 pev->target ? : "kernel");
3457 *tevs = zalloc(ret * sizeof(*tev));
3464 strlist__for_each_entry(node, entry->tevlist) {
3465 tev = &(*tevs)[i++];
3466 ret = parse_probe_trace_command(node->s, tev);
3469 /* Set the uprobes attribute as same as original */
3470 tev->uprobes = pev->uprobes;
3475 probe_cache__delete(cache);
3479 static int convert_to_probe_trace_events(struct perf_probe_event *pev,
3480 struct probe_trace_event **tevs)
3484 if (!pev->group && !pev->sdt) {
3485 /* Set group name if not given */
3486 if (!pev->uprobes) {
3487 pev->group = strdup(PERFPROBE_GROUP);
3488 ret = pev->group ? 0 : -ENOMEM;
3490 ret = convert_exec_to_group(pev->target, &pev->group);
3492 pr_warning("Failed to make a group name.\n");
3497 ret = try_to_find_absolute_address(pev, tevs);
3501 /* At first, we need to lookup cache entry */
3502 ret = find_probe_trace_events_from_cache(pev, tevs);
3503 if (ret > 0 || pev->sdt) /* SDT can be found only in the cache */
3504 return ret == 0 ? -ENOENT : ret; /* Found in probe cache */
3506 /* Convert perf_probe_event with debuginfo */
3507 ret = try_to_find_probe_trace_events(pev, tevs);
3509 return ret; /* Found in debuginfo or got an error */
3511 return find_probe_trace_events_from_map(pev, tevs);
3514 int convert_perf_probe_events(struct perf_probe_event *pevs, int npevs)
3518 /* Loop 1: convert all events */
3519 for (i = 0; i < npevs; i++) {
3520 /* Init kprobe blacklist if needed */
3521 if (!pevs[i].uprobes)
3522 kprobe_blacklist__init();
3523 /* Convert with or without debuginfo */
3524 ret = convert_to_probe_trace_events(&pevs[i], &pevs[i].tevs);
3527 pevs[i].ntevs = ret;
3529 /* This just release blacklist only if allocated */
3530 kprobe_blacklist__release();
3535 static int show_probe_trace_event(struct probe_trace_event *tev)
3537 char *buf = synthesize_probe_trace_command(tev);
3540 pr_debug("Failed to synthesize probe trace event.\n");
3544 /* Showing definition always go stdout */
3545 printf("%s\n", buf);
3551 int show_probe_trace_events(struct perf_probe_event *pevs, int npevs)
3553 struct strlist *namelist = strlist__new(NULL, NULL);
3554 struct probe_trace_event *tev;
3555 struct perf_probe_event *pev;
3561 for (j = 0; j < npevs && !ret; j++) {
3563 for (i = 0; i < pev->ntevs && !ret; i++) {
3564 tev = &pev->tevs[i];
3565 /* Skip if the symbol is out of .text or blacklisted */
3566 if (!tev->point.symbol && !pev->uprobes)
3569 /* Set new name for tev (and update namelist) */
3570 ret = probe_trace_event__set_name(tev, pev,
3573 ret = show_probe_trace_event(tev);
3576 strlist__delete(namelist);
3581 static int show_bootconfig_event(struct probe_trace_event *tev)
3583 struct probe_trace_point *tp = &tev->point;
3588 if (strbuf_init(&buf, 32) < 0)
3591 err = synthesize_kprobe_trace_def(tp, &buf);
3593 err = synthesize_probe_trace_args(tev, &buf);
3595 ret = strbuf_detach(&buf, NULL);
3596 strbuf_release(&buf);
3599 printf("'%s'", ret);
3606 int show_bootconfig_events(struct perf_probe_event *pevs, int npevs)
3608 struct strlist *namelist = strlist__new(NULL, NULL);
3609 struct probe_trace_event *tev;
3610 struct perf_probe_event *pev;
3611 char *cur_name = NULL;
3617 for (j = 0; j < npevs && !ret; j++) {
3619 if (pev->group && strcmp(pev->group, "probe"))
3620 pr_warning("WARN: Group name %s is ignored\n", pev->group);
3622 pr_warning("ERROR: Bootconfig doesn't support uprobes\n");
3626 for (i = 0; i < pev->ntevs && !ret; i++) {
3627 tev = &pev->tevs[i];
3628 /* Skip if the symbol is out of .text or blacklisted */
3629 if (!tev->point.symbol && !pev->uprobes)
3632 /* Set new name for tev (and update namelist) */
3633 ret = probe_trace_event__set_name(tev, pev,
3638 if (!cur_name || strcmp(cur_name, tev->event)) {
3639 printf("%sftrace.event.kprobes.%s.probe = ",
3640 cur_name ? "\n" : "", tev->event);
3641 cur_name = tev->event;
3644 ret = show_bootconfig_event(tev);
3648 strlist__delete(namelist);
3653 int apply_perf_probe_events(struct perf_probe_event *pevs, int npevs)
3657 /* Loop 2: add all events */
3658 for (i = 0; i < npevs; i++) {
3659 ret = __add_probe_trace_events(&pevs[i], pevs[i].tevs,
3661 probe_conf.force_add);
3668 void cleanup_perf_probe_events(struct perf_probe_event *pevs, int npevs)
3671 struct perf_probe_event *pev;
3673 /* Loop 3: cleanup and free trace events */
3674 for (i = 0; i < npevs; i++) {
3676 for (j = 0; j < pevs[i].ntevs; j++)
3677 clear_probe_trace_event(&pevs[i].tevs[j]);
3678 zfree(&pevs[i].tevs);
3680 nsinfo__zput(pev->nsi);
3681 clear_perf_probe_event(&pevs[i]);
3685 int add_perf_probe_events(struct perf_probe_event *pevs, int npevs)
3689 ret = init_probe_symbol_maps(pevs->uprobes);
3693 ret = convert_perf_probe_events(pevs, npevs);
3695 ret = apply_perf_probe_events(pevs, npevs);
3697 cleanup_perf_probe_events(pevs, npevs);
3699 exit_probe_symbol_maps();
3703 int del_perf_probe_events(struct strfilter *filter)
3705 int ret, ret2, ufd = -1, kfd = -1;
3706 char *str = strfilter__string(filter);
3711 /* Get current event names */
3712 ret = probe_file__open_both(&kfd, &ufd, PF_FL_RW);
3716 ret = probe_file__del_events(kfd, filter);
3717 if (ret < 0 && ret != -ENOENT)
3720 ret2 = probe_file__del_events(ufd, filter);
3721 if (ret2 < 0 && ret2 != -ENOENT) {
3738 int show_available_funcs(const char *target, struct nsinfo *nsi,
3739 struct strfilter *_filter, bool user)
3746 ret = init_probe_symbol_maps(user);
3750 /* Get a symbol map */
3751 map = get_target_map(target, nsi, user);
3753 pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
3757 ret = map__load(map);
3760 char *str = strfilter__string(_filter);
3761 pr_err("Failed to find symbols matched to \"%s\"\n",
3765 pr_err("Failed to load symbols in %s\n",
3766 (target) ? : "kernel");
3769 dso = map__dso(map);
3770 if (!dso__sorted_by_name(dso))
3771 dso__sort_by_name(dso);
3773 /* Show all (filtered) symbols */
3776 for (nd = rb_first_cached(&dso->symbol_names); nd; nd = rb_next(nd)) {
3777 struct symbol_name_rb_node *pos = rb_entry(nd, struct symbol_name_rb_node, rb_node);
3779 if (strfilter__compare(_filter, pos->sym.name))
3780 printf("%s\n", pos->sym.name);
3784 exit_probe_symbol_maps();
3789 int copy_to_probe_trace_arg(struct probe_trace_arg *tvar,
3790 struct perf_probe_arg *pvar)
3792 tvar->value = strdup(pvar->var);
3793 if (tvar->value == NULL)
3796 tvar->type = strdup(pvar->type);
3797 if (tvar->type == NULL)
3801 tvar->name = strdup(pvar->name);
3802 if (tvar->name == NULL)