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"
33 #include <api/fs/fs.h>
34 #include "trace-event.h" /* For __maybe_unused */
35 #include "probe-event.h"
36 #include "probe-finder.h"
37 #include "probe-file.h"
42 #include <subcmd/pager.h>
43 #include <linux/ctype.h>
44 #include <linux/zalloc.h>
46 #ifdef HAVE_DEBUGINFOD_SUPPORT
47 #include <elfutils/debuginfod.h>
50 #define PERFPROBE_GROUP "probe"
52 bool probe_event_dry_run; /* Dry run flag */
53 struct probe_conf probe_conf = { .magic_num = DEFAULT_PROBE_MAGIC_NUM };
55 #define semantic_error(msg ...) pr_err("Semantic error :" msg)
57 int e_snprintf(char *str, size_t size, const char *format, ...)
62 ret = vsnprintf(str, size, format, ap);
69 static struct machine *host_machine;
71 /* Initialize symbol maps and path of vmlinux/modules */
72 int init_probe_symbol_maps(bool user_only)
76 symbol_conf.sort_by_name = true;
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)
111 /* kmap->ref_reloc_sym should be set if host_machine is initialized */
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 : 0);
151 static struct map *kernel_get_module_map(const char *module)
153 struct maps *maps = machine__kernel_maps(host_machine);
156 /* A file path -- this is an offline module */
157 if (module && strchr(module, '/'))
158 return dso__new_map(module);
161 pos = machine__kernel_map(host_machine);
162 return map__get(pos);
165 maps__for_each_entry(maps, pos) {
166 /* short_name is "[module]" */
167 if (strncmp(pos->dso->short_name + 1, module,
168 pos->dso->short_name_len - 2) == 0 &&
169 module[pos->dso->short_name_len - 2] == '\0') {
170 return map__get(pos);
176 struct map *get_target_map(const char *target, struct nsinfo *nsi, bool user)
178 /* Init maps of given executable or kernel */
182 map = dso__new_map(target);
184 map->dso->nsinfo = nsinfo__get(nsi);
187 return kernel_get_module_map(target);
191 static int convert_exec_to_group(const char *exec, char **result)
193 char *ptr1, *ptr2, *exec_copy;
197 exec_copy = strdup(exec);
201 ptr1 = basename(exec_copy);
207 for (ptr2 = ptr1; *ptr2 != '\0'; ptr2++) {
208 if (!isalnum(*ptr2) && *ptr2 != '_') {
214 ret = e_snprintf(buf, 64, "%s_%s", PERFPROBE_GROUP, ptr1);
218 *result = strdup(buf);
219 ret = *result ? 0 : -ENOMEM;
226 static void clear_perf_probe_point(struct perf_probe_point *pp)
229 zfree(&pp->function);
230 zfree(&pp->lazy_line);
233 static void clear_probe_trace_events(struct probe_trace_event *tevs, int ntevs)
237 for (i = 0; i < ntevs; i++)
238 clear_probe_trace_event(tevs + i);
241 static bool kprobe_blacklist__listed(unsigned long address);
242 static bool kprobe_warn_out_range(const char *symbol, unsigned long address)
247 map = kernel_get_module_map(NULL);
249 ret = address <= map->start || map->end < address;
251 pr_warning("%s is out of .text, skip it.\n", symbol);
254 if (!ret && kprobe_blacklist__listed(address)) {
255 pr_warning("%s is blacklisted function, skip it.\n", symbol);
263 * @module can be module name of module file path. In case of path,
264 * inspect elf and find out what is actual module name.
265 * Caller has to free mod_name after using it.
267 static char *find_module_name(const char *module)
275 char *mod_name = NULL;
278 fd = open(module, O_RDONLY);
282 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
286 if (gelf_getehdr(elf, &ehdr) == NULL)
289 sec = elf_section_by_name(elf, &ehdr, &shdr,
290 ".gnu.linkonce.this_module", NULL);
294 data = elf_getdata(sec, NULL);
295 if (!data || !data->d_buf)
300 * '.gnu.linkonce.this_module' section of kernel module elf directly
301 * maps to 'struct module' from linux/module.h. This section contains
302 * actual module name which will be used by kernel after loading it.
303 * But, we cannot use 'struct module' here since linux/module.h is not
304 * exposed to user-space. Offset of 'name' has remained same from long
305 * time, so hardcoding it here.
307 if (ehdr.e_ident[EI_CLASS] == ELFCLASS32)
309 else /* expect ELFCLASS64 by default */
312 mod_name = strdup((char *)data->d_buf + name_offset);
321 #ifdef HAVE_DWARF_SUPPORT
323 static int kernel_get_module_dso(const char *module, struct dso **pdso)
327 const char *vmlinux_name;
331 char module_name[128];
333 snprintf(module_name, sizeof(module_name), "[%s]", module);
334 map = maps__find_by_name(&host_machine->kmaps, module_name);
339 pr_debug("Failed to find module %s.\n", module);
343 map = machine__kernel_map(host_machine);
345 if (!dso->has_build_id)
346 dso__read_running_kernel_build_id(dso, host_machine);
348 vmlinux_name = symbol_conf.vmlinux_name;
351 ret = dso__load_vmlinux(dso, map, vmlinux_name, false);
353 ret = dso__load_vmlinux_path(dso, map);
360 * Some binaries like glibc have special symbols which are on the symbol
361 * table, but not in the debuginfo. If we can find the address of the
362 * symbol from map, we can translate the address back to the probe point.
364 static int find_alternative_probe_point(struct debuginfo *dinfo,
365 struct perf_probe_point *pp,
366 struct perf_probe_point *result,
367 const char *target, struct nsinfo *nsi,
370 struct map *map = NULL;
375 /* This can work only for function-name based one */
376 if (!pp->function || pp->file)
379 map = get_target_map(target, nsi, uprobes);
383 /* Find the address of given function */
384 map__for_each_symbol_by_name(map, pp->function, sym) {
386 address = sym->start;
387 if (sym->type == STT_GNU_IFUNC)
388 pr_warning("Warning: The probe function (%s) is a GNU indirect function.\n"
389 "Consider identifying the final function used at run time and set the probe directly on that.\n",
392 address = map->unmap_ip(map, sym->start) - map->reloc;
399 pr_debug("Symbol %s address found : %" PRIx64 "\n",
400 pp->function, address);
402 ret = debuginfo__find_probe_point(dinfo, (unsigned long)address,
405 ret = (!ret) ? -ENOENT : ret;
407 result->offset += pp->offset;
408 result->line += pp->line;
409 result->retprobe = pp->retprobe;
419 static int get_alternative_probe_event(struct debuginfo *dinfo,
420 struct perf_probe_event *pev,
421 struct perf_probe_point *tmp)
425 memcpy(tmp, &pev->point, sizeof(*tmp));
426 memset(&pev->point, 0, sizeof(pev->point));
427 ret = find_alternative_probe_point(dinfo, tmp, &pev->point, pev->target,
428 pev->nsi, pev->uprobes);
430 memcpy(&pev->point, tmp, sizeof(*tmp));
435 static int get_alternative_line_range(struct debuginfo *dinfo,
436 struct line_range *lr,
437 const char *target, bool user)
439 struct perf_probe_point pp = { .function = lr->function,
442 struct perf_probe_point result;
445 memset(&result, 0, sizeof(result));
447 if (lr->end != INT_MAX)
448 len = lr->end - lr->start;
449 ret = find_alternative_probe_point(dinfo, &pp, &result,
452 lr->function = result.function;
453 lr->file = result.file;
454 lr->start = result.line;
455 if (lr->end != INT_MAX)
456 lr->end = lr->start + len;
457 clear_perf_probe_point(&pp);
462 #ifdef HAVE_DEBUGINFOD_SUPPORT
463 static struct debuginfo *open_from_debuginfod(struct dso *dso, struct nsinfo *nsi,
466 debuginfod_client *c = debuginfod_begin();
467 char sbuild_id[SBUILD_ID_SIZE + 1];
468 struct debuginfo *ret = NULL;
476 build_id__sprintf(&dso->bid, sbuild_id);
477 fd = debuginfod_find_debuginfo(c, (const unsigned char *)sbuild_id,
484 pr_debug("Failed to find debuginfo in debuginfod.\n");
488 pr_debug("Load debuginfo from debuginfod (%s)\n", path);
490 nsinfo__mountns_enter(nsi, &nsc);
491 ret = debuginfo__new((const char *)path);
492 nsinfo__mountns_exit(&nsc);
497 struct debuginfo *open_from_debuginfod(struct dso *dso __maybe_unused,
498 struct nsinfo *nsi __maybe_unused,
499 bool silent __maybe_unused)
505 /* Open new debuginfo of given module */
506 static struct debuginfo *open_debuginfo(const char *module, struct nsinfo *nsi,
509 const char *path = module;
510 char reason[STRERR_BUFSIZE];
511 struct debuginfo *ret = NULL;
512 struct dso *dso = NULL;
516 if (!module || !strchr(module, '/')) {
517 err = kernel_get_module_dso(module, &dso);
519 if (!dso || dso->load_errno == 0) {
520 if (!str_error_r(-err, reason, STRERR_BUFSIZE))
521 strcpy(reason, "(unknown)");
523 dso__strerror_load(dso, reason, STRERR_BUFSIZE);
525 ret = open_from_debuginfod(dso, nsi, silent);
530 pr_err("Module %s is not loaded, please specify its full path name.\n", module);
532 pr_err("Failed to find the path for the kernel: %s\n", reason);
536 path = dso->long_name;
538 nsinfo__mountns_enter(nsi, &nsc);
539 ret = debuginfo__new(path);
540 if (!ret && !silent) {
541 pr_warning("The %s file has no debug information.\n", path);
542 if (!module || !strtailcmp(path, ".ko"))
543 pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, ");
545 pr_warning("Rebuild with -g, ");
546 pr_warning("or install an appropriate debuginfo package.\n");
548 nsinfo__mountns_exit(&nsc);
552 /* For caching the last debuginfo */
553 static struct debuginfo *debuginfo_cache;
554 static char *debuginfo_cache_path;
556 static struct debuginfo *debuginfo_cache__open(const char *module, bool silent)
558 const char *path = module;
560 /* If the module is NULL, it should be the kernel. */
564 if (debuginfo_cache_path && !strcmp(debuginfo_cache_path, path))
567 /* Copy module path */
568 free(debuginfo_cache_path);
569 debuginfo_cache_path = strdup(path);
570 if (!debuginfo_cache_path) {
571 debuginfo__delete(debuginfo_cache);
572 debuginfo_cache = NULL;
576 debuginfo_cache = open_debuginfo(module, NULL, silent);
577 if (!debuginfo_cache)
578 zfree(&debuginfo_cache_path);
580 return debuginfo_cache;
583 static void debuginfo_cache__exit(void)
585 debuginfo__delete(debuginfo_cache);
586 debuginfo_cache = NULL;
587 zfree(&debuginfo_cache_path);
591 static int get_text_start_address(const char *exec, unsigned long *address,
597 int fd, ret = -ENOENT;
600 nsinfo__mountns_enter(nsi, &nsc);
601 fd = open(exec, O_RDONLY);
602 nsinfo__mountns_exit(&nsc);
606 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
612 if (gelf_getehdr(elf, &ehdr) == NULL)
615 if (!elf_section_by_name(elf, &ehdr, &shdr, ".text", NULL))
618 *address = shdr.sh_addr - shdr.sh_offset;
629 * Convert trace point to probe point with debuginfo
631 static int find_perf_probe_point_from_dwarf(struct probe_trace_point *tp,
632 struct perf_probe_point *pp,
635 struct debuginfo *dinfo = NULL;
636 unsigned long stext = 0;
637 u64 addr = tp->address;
640 /* convert the address to dwarf address */
646 ret = get_text_start_address(tp->module, &stext, NULL);
650 } else if (tp->symbol) {
651 /* If the module is given, this returns relative address */
652 ret = kernel_get_symbol_address_by_name(tp->symbol, &addr,
653 false, !!tp->module);
659 pr_debug("try to find information at %" PRIx64 " in %s\n", addr,
660 tp->module ? : "kernel");
662 dinfo = debuginfo_cache__open(tp->module, verbose <= 0);
664 ret = debuginfo__find_probe_point(dinfo,
665 (unsigned long)addr, pp);
670 pp->retprobe = tp->retprobe;
674 pr_debug("Failed to find corresponding probes from debuginfo.\n");
675 return ret ? : -ENOENT;
678 /* Adjust symbol name and address */
679 static int post_process_probe_trace_point(struct probe_trace_point *tp,
680 struct map *map, unsigned long offs)
683 u64 addr = tp->address - offs;
685 sym = map__find_symbol(map, addr);
689 if (strcmp(sym->name, tp->symbol)) {
690 /* If we have no realname, use symbol for it */
692 tp->realname = tp->symbol;
695 tp->symbol = strdup(sym->name);
699 tp->offset = addr - sym->start;
706 * Rename DWARF symbols to ELF symbols -- gcc sometimes optimizes functions
707 * and generate new symbols with suffixes such as .constprop.N or .isra.N
708 * etc. Since those symbols are not recorded in DWARF, we have to find
709 * correct generated symbols from offline ELF binary.
710 * For online kernel or uprobes we don't need this because those are
711 * rebased on _text, or already a section relative address.
714 post_process_offline_probe_trace_events(struct probe_trace_event *tevs,
715 int ntevs, const char *pathname)
718 unsigned long stext = 0;
721 /* Prepare a map for offline binary */
722 map = dso__new_map(pathname);
723 if (!map || get_text_start_address(pathname, &stext, NULL) < 0) {
724 pr_warning("Failed to get ELF symbols for %s\n", pathname);
728 for (i = 0; i < ntevs; i++) {
729 ret = post_process_probe_trace_point(&tevs[i].point,
739 static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs,
740 int ntevs, const char *exec,
744 unsigned long stext = 0;
749 ret = get_text_start_address(exec, &stext, nsi);
753 for (i = 0; i < ntevs && ret >= 0; i++) {
754 /* point.address is the address of point.symbol + point.offset */
755 tevs[i].point.address -= stext;
756 tevs[i].point.module = strdup(exec);
757 if (!tevs[i].point.module) {
761 tevs[i].uprobes = true;
768 post_process_module_probe_trace_events(struct probe_trace_event *tevs,
769 int ntevs, const char *module,
770 struct debuginfo *dinfo)
772 Dwarf_Addr text_offs = 0;
774 char *mod_name = NULL;
780 map = get_target_map(module, NULL, false);
781 if (!map || debuginfo__get_text_offset(dinfo, &text_offs, true) < 0) {
782 pr_warning("Failed to get ELF symbols for %s\n", module);
786 mod_name = find_module_name(module);
787 for (i = 0; i < ntevs; i++) {
788 ret = post_process_probe_trace_point(&tevs[i].point,
789 map, (unsigned long)text_offs);
792 tevs[i].point.module =
793 strdup(mod_name ? mod_name : module);
794 if (!tevs[i].point.module) {
807 post_process_kernel_probe_trace_events(struct probe_trace_event *tevs,
810 struct ref_reloc_sym *reloc_sym;
815 /* Skip post process if the target is an offline kernel */
816 if (symbol_conf.ignore_vmlinux_buildid)
817 return post_process_offline_probe_trace_events(tevs, ntevs,
818 symbol_conf.vmlinux_name);
820 reloc_sym = kernel_get_ref_reloc_sym(&map);
822 pr_warning("Relocated base symbol is not found!\n");
826 for (i = 0; i < ntevs; i++) {
827 if (!tevs[i].point.address)
829 if (tevs[i].point.retprobe && !kretprobe_offset_is_supported())
832 * If we found a wrong one, mark it by NULL symbol.
833 * Since addresses in debuginfo is same as objdump, we need
834 * to convert it to addresses on memory.
836 if (kprobe_warn_out_range(tevs[i].point.symbol,
837 map__objdump_2mem(map, tevs[i].point.address))) {
841 tmp = strdup(reloc_sym->name);
845 /* If we have no realname, use symbol for it */
846 if (!tevs[i].point.realname)
847 tevs[i].point.realname = tevs[i].point.symbol;
849 free(tevs[i].point.symbol);
850 tevs[i].point.symbol = tmp;
851 tevs[i].point.offset = tevs[i].point.address -
852 (map->reloc ? reloc_sym->unrelocated_addr :
859 arch__post_process_probe_trace_events(struct perf_probe_event *pev __maybe_unused,
860 int ntevs __maybe_unused)
864 /* Post processing the probe events */
865 static int post_process_probe_trace_events(struct perf_probe_event *pev,
866 struct probe_trace_event *tevs,
867 int ntevs, const char *module,
868 bool uprobe, struct debuginfo *dinfo)
873 ret = add_exec_to_probe_trace_events(tevs, ntevs, module,
876 /* Currently ref_reloc_sym based probe is not for drivers */
877 ret = post_process_module_probe_trace_events(tevs, ntevs,
880 ret = post_process_kernel_probe_trace_events(tevs, ntevs);
883 arch__post_process_probe_trace_events(pev, ntevs);
888 /* Try to find perf_probe_event with debuginfo */
889 static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
890 struct probe_trace_event **tevs)
892 bool need_dwarf = perf_probe_event_need_dwarf(pev);
893 struct perf_probe_point tmp;
894 struct debuginfo *dinfo;
897 /* Workaround for gcc #98776 issue.
898 * Perf failed to add kretprobe event with debuginfo of vmlinux which is
899 * compiled by gcc with -fpatchable-function-entry option enabled. The
900 * same issue with kernel module. The retprobe doesn`t need debuginfo.
901 * This workaround solution use map to query the probe function address
902 * for retprobe event.
904 if (pev->point.retprobe)
907 dinfo = open_debuginfo(pev->target, pev->nsi, !need_dwarf);
911 pr_debug("Could not open debuginfo. Try to use symbols.\n");
915 pr_debug("Try to find probe point from debuginfo.\n");
916 /* Searching trace events corresponding to a probe event */
917 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
919 if (ntevs == 0) { /* Not found, retry with an alternative */
920 ret = get_alternative_probe_event(dinfo, pev, &tmp);
922 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
924 * Write back to the original probe_event for
925 * setting appropriate (user given) event name
927 clear_perf_probe_point(&pev->point);
928 memcpy(&pev->point, &tmp, sizeof(tmp));
932 if (ntevs > 0) { /* Succeeded to find trace events */
933 pr_debug("Found %d probe_trace_events.\n", ntevs);
934 ret = post_process_probe_trace_events(pev, *tevs, ntevs,
935 pev->target, pev->uprobes, dinfo);
936 if (ret < 0 || ret == ntevs) {
937 pr_debug("Post processing failed or all events are skipped. (%d)\n", ret);
938 clear_probe_trace_events(*tevs, ntevs);
944 debuginfo__delete(dinfo);
946 if (ntevs == 0) { /* No error but failed to find probe point. */
947 pr_warning("Probe point '%s' not found.\n",
948 synthesize_perf_probe_point(&pev->point));
950 } else if (ntevs < 0) {
951 /* Error path : ntevs < 0 */
952 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
954 pr_warning("Warning: No dwarf info found in the vmlinux - "
955 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
957 pr_debug("Trying to use symbols.\n");
964 #define LINEBUF_SIZE 256
965 #define NR_ADDITIONAL_LINES 2
967 static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
969 char buf[LINEBUF_SIZE], sbuf[STRERR_BUFSIZE];
970 const char *color = show_num ? "" : PERF_COLOR_BLUE;
971 const char *prefix = NULL;
974 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
979 prefix = show_num ? "%7d " : " ";
980 color_fprintf(stdout, color, prefix, l);
982 color_fprintf(stdout, color, "%s", buf);
984 } while (strchr(buf, '\n') == NULL);
989 pr_warning("File read error: %s\n",
990 str_error_r(errno, sbuf, sizeof(sbuf)));
996 static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
998 int rv = __show_one_line(fp, l, skip, show_num);
1000 pr_warning("Source file is shorter than expected.\n");
1006 #define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
1007 #define show_one_line(f,l) _show_one_line(f,l,false,false)
1008 #define skip_one_line(f,l) _show_one_line(f,l,true,false)
1009 #define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
1012 * Show line-range always requires debuginfo to find source file and
1015 static int __show_line_range(struct line_range *lr, const char *module,
1018 struct build_id bid;
1020 struct int_node *ln;
1021 struct debuginfo *dinfo;
1025 char sbuf[STRERR_BUFSIZE];
1026 char sbuild_id[SBUILD_ID_SIZE] = "";
1028 /* Search a line range */
1029 dinfo = open_debuginfo(module, NULL, false);
1033 ret = debuginfo__find_line_range(dinfo, lr);
1034 if (!ret) { /* Not found, retry with an alternative */
1035 ret = get_alternative_line_range(dinfo, lr, module, user);
1037 ret = debuginfo__find_line_range(dinfo, lr);
1039 if (dinfo->build_id) {
1040 build_id__init(&bid, dinfo->build_id, BUILD_ID_SIZE);
1041 build_id__sprintf(&bid, sbuild_id);
1043 debuginfo__delete(dinfo);
1044 if (ret == 0 || ret == -ENOENT) {
1045 pr_warning("Specified source line is not found.\n");
1047 } else if (ret < 0) {
1048 pr_warning("Debuginfo analysis failed.\n");
1052 /* Convert source file path */
1054 ret = find_source_path(tmp, sbuild_id, lr->comp_dir, &lr->path);
1056 /* Free old path when new path is assigned */
1057 if (tmp != lr->path)
1061 pr_warning("Failed to find source file path.\n");
1068 fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
1069 lr->start - lr->offset);
1071 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
1073 fp = fopen(lr->path, "r");
1075 pr_warning("Failed to open %s: %s\n", lr->path,
1076 str_error_r(errno, sbuf, sizeof(sbuf)));
1079 /* Skip to starting line number */
1080 while (l < lr->start) {
1081 ret = skip_one_line(fp, l++);
1086 intlist__for_each_entry(ln, lr->line_list) {
1087 for (; ln->i > (unsigned long)l; l++) {
1088 ret = show_one_line(fp, l - lr->offset);
1092 ret = show_one_line_with_num(fp, l++ - lr->offset);
1097 if (lr->end == INT_MAX)
1098 lr->end = l + NR_ADDITIONAL_LINES;
1099 while (l <= lr->end) {
1100 ret = show_one_line_or_eof(fp, l++ - lr->offset);
1109 int show_line_range(struct line_range *lr, const char *module,
1110 struct nsinfo *nsi, bool user)
1113 struct nscookie nsc;
1115 ret = init_probe_symbol_maps(user);
1118 nsinfo__mountns_enter(nsi, &nsc);
1119 ret = __show_line_range(lr, module, user);
1120 nsinfo__mountns_exit(&nsc);
1121 exit_probe_symbol_maps();
1126 static int show_available_vars_at(struct debuginfo *dinfo,
1127 struct perf_probe_event *pev,
1128 struct strfilter *_filter)
1132 struct str_node *node;
1133 struct variable_list *vls = NULL, *vl;
1134 struct perf_probe_point tmp;
1137 buf = synthesize_perf_probe_point(&pev->point);
1140 pr_debug("Searching variables at %s\n", buf);
1142 ret = debuginfo__find_available_vars_at(dinfo, pev, &vls);
1143 if (!ret) { /* Not found, retry with an alternative */
1144 ret = get_alternative_probe_event(dinfo, pev, &tmp);
1146 ret = debuginfo__find_available_vars_at(dinfo, pev,
1148 /* Release the old probe_point */
1149 clear_perf_probe_point(&tmp);
1153 if (ret == 0 || ret == -ENOENT) {
1154 pr_err("Failed to find the address of %s\n", buf);
1157 pr_warning("Debuginfo analysis failed.\n");
1161 /* Some variables are found */
1162 fprintf(stdout, "Available variables at %s\n", buf);
1163 for (i = 0; i < ret; i++) {
1166 * A probe point might be converted to
1167 * several trace points.
1169 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
1171 zfree(&vl->point.symbol);
1174 strlist__for_each_entry(node, vl->vars) {
1175 var = strchr(node->s, '\t') + 1;
1176 if (strfilter__compare(_filter, var)) {
1177 fprintf(stdout, "\t\t%s\n", node->s);
1181 strlist__delete(vl->vars);
1184 fprintf(stdout, "\t\t(No matched variables)\n");
1192 /* Show available variables on given probe point */
1193 int show_available_vars(struct perf_probe_event *pevs, int npevs,
1194 struct strfilter *_filter)
1197 struct debuginfo *dinfo;
1199 ret = init_probe_symbol_maps(pevs->uprobes);
1203 dinfo = open_debuginfo(pevs->target, pevs->nsi, false);
1211 for (i = 0; i < npevs && ret >= 0; i++)
1212 ret = show_available_vars_at(dinfo, &pevs[i], _filter);
1214 debuginfo__delete(dinfo);
1216 exit_probe_symbol_maps();
1220 #else /* !HAVE_DWARF_SUPPORT */
1222 static void debuginfo_cache__exit(void)
1227 find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused,
1228 struct perf_probe_point *pp __maybe_unused,
1229 bool is_kprobe __maybe_unused)
1234 static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
1235 struct probe_trace_event **tevs __maybe_unused)
1237 if (perf_probe_event_need_dwarf(pev)) {
1238 pr_warning("Debuginfo-analysis is not supported.\n");
1245 int show_line_range(struct line_range *lr __maybe_unused,
1246 const char *module __maybe_unused,
1247 struct nsinfo *nsi __maybe_unused,
1248 bool user __maybe_unused)
1250 pr_warning("Debuginfo-analysis is not supported.\n");
1254 int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
1255 int npevs __maybe_unused,
1256 struct strfilter *filter __maybe_unused)
1258 pr_warning("Debuginfo-analysis is not supported.\n");
1263 void line_range__clear(struct line_range *lr)
1265 zfree(&lr->function);
1268 zfree(&lr->comp_dir);
1269 intlist__delete(lr->line_list);
1272 int line_range__init(struct line_range *lr)
1274 memset(lr, 0, sizeof(*lr));
1275 lr->line_list = intlist__new(NULL);
1282 static int parse_line_num(char **ptr, int *val, const char *what)
1284 const char *start = *ptr;
1287 *val = strtol(*ptr, ptr, 0);
1288 if (errno || *ptr == start) {
1289 semantic_error("'%s' is not a valid number.\n", what);
1295 /* Check the name is good for event, group or function */
1296 static bool is_c_func_name(const char *name)
1298 if (!isalpha(*name) && *name != '_')
1300 while (*++name != '\0') {
1301 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
1308 * Stuff 'lr' according to the line range described by 'arg'.
1309 * The line range syntax is described by:
1311 * SRC[:SLN[+NUM|-ELN]]
1312 * FNC[@SRC][:SLN[+NUM|-ELN]]
1314 int parse_line_range_desc(const char *arg, struct line_range *lr)
1316 char *range, *file, *name = strdup(arg);
1325 range = strchr(name, ':');
1329 err = parse_line_num(&range, &lr->start, "start line");
1333 if (*range == '+' || *range == '-') {
1334 const char c = *range++;
1336 err = parse_line_num(&range, &lr->end, "end line");
1341 lr->end += lr->start;
1343 * Adjust the number of lines here.
1344 * If the number of lines == 1, the
1345 * the end of line should be equal to
1346 * the start of line.
1352 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
1355 if (lr->start > lr->end) {
1356 semantic_error("Start line must be smaller"
1357 " than end line.\n");
1360 if (*range != '\0') {
1361 semantic_error("Tailing with invalid str '%s'.\n", range);
1366 file = strchr(name, '@');
1369 lr->file = strdup(++file);
1370 if (lr->file == NULL) {
1374 lr->function = name;
1375 } else if (strchr(name, '/') || strchr(name, '.'))
1377 else if (is_c_func_name(name))/* We reuse it for checking funcname */
1378 lr->function = name;
1379 else { /* Invalid name */
1380 semantic_error("'%s' is not a valid function name.\n", name);
1391 static int parse_perf_probe_event_name(char **arg, struct perf_probe_event *pev)
1395 ptr = strpbrk_esc(*arg, ":");
1398 if (!pev->sdt && !is_c_func_name(*arg))
1400 pev->group = strdup_esc(*arg);
1407 pev->event = strdup_esc(*arg);
1408 if (pev->event == NULL)
1411 if (!pev->sdt && !is_c_func_name(pev->event)) {
1415 semantic_error("%s is bad for event name -it must "
1416 "follow C symbol-naming rule.\n", *arg);
1422 /* Parse probepoint definition. */
1423 static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
1425 struct perf_probe_point *pp = &pev->point;
1428 bool file_spec = false;
1433 * perf probe [GRP:][EVENT=]SRC[:LN|;PTN]
1434 * perf probe [GRP:][EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
1435 * perf probe %[GRP:]SDT_EVENT
1440 if (is_sdt_event(arg)) {
1446 ptr = strpbrk_esc(arg, ";=@+%");
1450 semantic_error("%s must be an SDT name.\n",
1454 /* This must be a target file name or build id */
1455 tmp = build_id_cache__complement(ptr + 1);
1457 pev->target = build_id_cache__origname(tmp);
1460 pev->target = strdup_esc(ptr + 1);
1465 ret = parse_perf_probe_event_name(&arg, pev);
1467 if (asprintf(&pev->point.function, "%%%s", pev->event) < 0)
1473 if (ptr && *ptr == '=') { /* Event name */
1476 ret = parse_perf_probe_event_name(&arg, pev);
1484 * Check arg is function or file name and copy it.
1486 * We consider arg to be a file spec if and only if it satisfies
1487 * all of the below criteria::
1488 * - it does not include any of "+@%",
1489 * - it includes one of ":;", and
1490 * - it has a period '.' in the name.
1492 * Otherwise, we consider arg to be a function specification.
1494 if (!strpbrk_esc(arg, "+@%")) {
1495 ptr = strpbrk_esc(arg, ";:");
1496 /* This is a file spec if it includes a '.' before ; or : */
1497 if (ptr && memchr(arg, '.', ptr - arg))
1501 ptr = strpbrk_esc(arg, ";:+@%");
1510 tmp = strdup_esc(arg);
1521 * Keep pp->function even if this is absolute address,
1522 * so it can mark whether abs_address is valid.
1523 * Which make 'perf probe lib.bin 0x0' possible.
1525 * Note that checking length of tmp is not needed
1526 * because when we access tmp[1] we know tmp[0] is '0',
1527 * so tmp[1] should always valid (but could be '\0').
1529 if (tmp && !strncmp(tmp, "0x", 2)) {
1530 pp->abs_address = strtoul(pp->function, &tmp, 0);
1532 semantic_error("Invalid absolute address.\n");
1538 /* Parse other options */
1542 if (c == ';') { /* Lazy pattern must be the last part */
1543 pp->lazy_line = strdup(arg); /* let leave escapes */
1544 if (pp->lazy_line == NULL)
1548 ptr = strpbrk_esc(arg, ";:+@%");
1554 case ':': /* Line number */
1555 pp->line = strtoul(arg, &tmp, 0);
1557 semantic_error("There is non-digit char"
1558 " in line number.\n");
1562 case '+': /* Byte offset from a symbol */
1563 pp->offset = strtoul(arg, &tmp, 0);
1565 semantic_error("There is non-digit character"
1570 case '@': /* File name */
1572 semantic_error("SRC@SRC is not allowed.\n");
1575 pp->file = strdup_esc(arg);
1576 if (pp->file == NULL)
1579 case '%': /* Probe places */
1580 if (strcmp(arg, "return") == 0) {
1582 } else { /* Others not supported yet */
1583 semantic_error("%%%s is not supported.\n", arg);
1587 default: /* Buggy case */
1588 pr_err("This program has a bug at %s:%d.\n",
1589 __FILE__, __LINE__);
1595 /* Exclusion check */
1596 if (pp->lazy_line && pp->line) {
1597 semantic_error("Lazy pattern can't be used with"
1602 if (pp->lazy_line && pp->offset) {
1603 semantic_error("Lazy pattern can't be used with offset.\n");
1607 if (pp->line && pp->offset) {
1608 semantic_error("Offset can't be used with line number.\n");
1612 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
1613 semantic_error("File always requires line number or "
1618 if (pp->offset && !pp->function) {
1619 semantic_error("Offset requires an entry function.\n");
1623 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
1624 semantic_error("Offset/Line/Lazy pattern can't be used with "
1629 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
1630 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
1635 /* Parse perf-probe event argument */
1636 static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
1638 char *tmp, *goodname;
1639 struct perf_probe_arg_field **fieldp;
1641 pr_debug("parsing arg: %s into ", str);
1643 tmp = strchr(str, '=');
1645 arg->name = strndup(str, tmp - str);
1646 if (arg->name == NULL)
1648 pr_debug("name:%s ", arg->name);
1652 tmp = strchr(str, '@');
1653 if (tmp && tmp != str && !strcmp(tmp + 1, "user")) { /* user attr */
1654 if (!user_access_is_supported()) {
1655 semantic_error("ftrace does not support user access\n");
1659 arg->user_access = true;
1660 pr_debug("user_access ");
1663 tmp = strchr(str, ':');
1664 if (tmp) { /* Type setting */
1666 arg->type = strdup(tmp + 1);
1667 if (arg->type == NULL)
1669 pr_debug("type:%s ", arg->type);
1672 tmp = strpbrk(str, "-.[");
1673 if (!is_c_varname(str) || !tmp) {
1674 /* A variable, register, symbol or special value */
1675 arg->var = strdup(str);
1676 if (arg->var == NULL)
1678 pr_debug("%s\n", arg->var);
1682 /* Structure fields or array element */
1683 arg->var = strndup(str, tmp - str);
1684 if (arg->var == NULL)
1686 goodname = arg->var;
1687 pr_debug("%s, ", arg->var);
1688 fieldp = &arg->field;
1691 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
1692 if (*fieldp == NULL)
1694 if (*tmp == '[') { /* Array */
1696 (*fieldp)->index = strtol(str + 1, &tmp, 0);
1697 (*fieldp)->ref = true;
1698 if (*tmp != ']' || tmp == str + 1) {
1699 semantic_error("Array index must be a"
1706 } else { /* Structure */
1709 (*fieldp)->ref = false;
1710 } else if (tmp[1] == '>') {
1712 (*fieldp)->ref = true;
1714 semantic_error("Argument parse error: %s\n",
1718 tmp = strpbrk(str, "-.[");
1721 (*fieldp)->name = strndup(str, tmp - str);
1722 if ((*fieldp)->name == NULL)
1725 goodname = (*fieldp)->name;
1726 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
1727 fieldp = &(*fieldp)->next;
1730 (*fieldp)->name = strdup(str);
1731 if ((*fieldp)->name == NULL)
1734 goodname = (*fieldp)->name;
1735 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
1737 /* If no name is specified, set the last field name (not array index)*/
1739 arg->name = strdup(goodname);
1740 if (arg->name == NULL)
1746 /* Parse perf-probe event command */
1747 int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
1750 int argc, i, ret = 0;
1752 argv = argv_split(cmd, &argc);
1754 pr_debug("Failed to split arguments.\n");
1757 if (argc - 1 > MAX_PROBE_ARGS) {
1758 semantic_error("Too many probe arguments (%d).\n", argc - 1);
1762 /* Parse probe point */
1763 ret = parse_perf_probe_point(argv[0], pev);
1767 /* Generate event name if needed */
1768 if (!pev->event && pev->point.function && pev->point.line
1769 && !pev->point.lazy_line && !pev->point.offset) {
1770 if (asprintf(&pev->event, "%s_L%d", pev->point.function,
1771 pev->point.line) < 0)
1775 /* Copy arguments and ensure return probe has no C argument */
1776 pev->nargs = argc - 1;
1777 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1778 if (pev->args == NULL) {
1782 for (i = 0; i < pev->nargs && ret >= 0; i++) {
1783 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1785 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
1786 semantic_error("You can't specify local variable for"
1797 /* Returns true if *any* ARG is either C variable, $params or $vars. */
1798 bool perf_probe_with_var(struct perf_probe_event *pev)
1802 for (i = 0; i < pev->nargs; i++)
1803 if (is_c_varname(pev->args[i].var) ||
1804 !strcmp(pev->args[i].var, PROBE_ARG_PARAMS) ||
1805 !strcmp(pev->args[i].var, PROBE_ARG_VARS))
1810 /* Return true if this perf_probe_event requires debuginfo */
1811 bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
1813 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1816 if (perf_probe_with_var(pev))
1822 /* Parse probe_events event into struct probe_point */
1823 int parse_probe_trace_command(const char *cmd, struct probe_trace_event *tev)
1825 struct probe_trace_point *tp = &tev->point;
1828 char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
1832 pr_debug("Parsing probe_events: %s\n", cmd);
1833 argv = argv_split(cmd, &argc);
1835 pr_debug("Failed to split arguments.\n");
1839 semantic_error("Too few probe arguments.\n");
1844 /* Scan event and group name. */
1845 argv0_str = strdup(argv[0]);
1846 if (argv0_str == NULL) {
1850 fmt1_str = strtok_r(argv0_str, ":", &fmt);
1851 fmt2_str = strtok_r(NULL, "/", &fmt);
1852 fmt3_str = strtok_r(NULL, " \t", &fmt);
1853 if (fmt1_str == NULL || fmt2_str == NULL || fmt3_str == NULL) {
1854 semantic_error("Failed to parse event name: %s\n", argv[0]);
1859 tev->group = strdup(fmt2_str);
1860 tev->event = strdup(fmt3_str);
1861 if (tev->group == NULL || tev->event == NULL) {
1865 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
1867 tp->retprobe = (pr == 'r');
1869 /* Scan module name(if there), function name and offset */
1870 p = strchr(argv[1], ':');
1872 tp->module = strndup(argv[1], p - argv[1]);
1877 tev->uprobes = (tp->module[0] == '/');
1881 fmt1_str = strtok_r(p, "+", &fmt);
1882 /* only the address started with 0x */
1883 if (fmt1_str[0] == '0') {
1885 * Fix a special case:
1886 * if address == 0, kernel reports something like:
1887 * p:probe_libc/abs_0 /lib/libc-2.18.so:0x (null) arg1=%ax
1888 * Newer kernel may fix that, but we want to
1889 * support old kernel also.
1891 if (strcmp(fmt1_str, "0x") == 0) {
1892 if (!argv[2] || strcmp(argv[2], "(null)")) {
1899 for (i = 2; argv[i + 1] != NULL; i++)
1900 argv[i] = argv[i + 1];
1905 tp->address = strtoul(fmt1_str, NULL, 0);
1907 /* Only the symbol-based probe has offset */
1908 tp->symbol = strdup(fmt1_str);
1909 if (tp->symbol == NULL) {
1913 fmt2_str = strtok_r(NULL, "", &fmt);
1914 if (fmt2_str == NULL)
1917 tp->offset = strtoul(fmt2_str, NULL, 10);
1921 fmt2_str = strchr(p, '(');
1923 tp->ref_ctr_offset = strtoul(fmt2_str + 1, NULL, 0);
1926 tev->nargs = argc - 2;
1927 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
1928 if (tev->args == NULL) {
1932 for (i = 0; i < tev->nargs; i++) {
1933 p = strchr(argv[i + 2], '=');
1934 if (p) /* We don't need which register is assigned. */
1938 tev->args[i].name = strdup(argv[i + 2]);
1939 /* TODO: parse regs and offset */
1940 tev->args[i].value = strdup(p);
1941 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1953 /* Compose only probe arg */
1954 char *synthesize_perf_probe_arg(struct perf_probe_arg *pa)
1956 struct perf_probe_arg_field *field = pa->field;
1961 if (strbuf_init(&buf, 64) < 0)
1964 if (pa->name && pa->var)
1965 err = strbuf_addf(&buf, "%s=%s", pa->name, pa->var);
1967 err = strbuf_addstr(&buf, pa->name ?: pa->var);
1972 if (field->name[0] == '[')
1973 err = strbuf_addstr(&buf, field->name);
1975 err = strbuf_addf(&buf, "%s%s", field->ref ? "->" : ".",
1977 field = field->next;
1983 if (strbuf_addf(&buf, ":%s", pa->type) < 0)
1986 ret = strbuf_detach(&buf, NULL);
1988 strbuf_release(&buf);
1992 /* Compose only probe point (not argument) */
1993 char *synthesize_perf_probe_point(struct perf_probe_point *pp)
1996 char *tmp, *ret = NULL;
1999 if (strbuf_init(&buf, 64) < 0)
2003 if (strbuf_addstr(&buf, pp->function) < 0)
2006 err = strbuf_addf(&buf, "+%lu", pp->offset);
2008 err = strbuf_addf(&buf, ":%d", pp->line);
2009 else if (pp->retprobe)
2010 err = strbuf_addstr(&buf, "%return");
2018 tmp = strchr(pp->file + len - 30, '/');
2019 tmp = tmp ? tmp + 1 : pp->file + len - 30;
2021 err = strbuf_addf(&buf, "@%s", tmp);
2022 if (!err && !pp->function && pp->line)
2023 err = strbuf_addf(&buf, ":%d", pp->line);
2026 ret = strbuf_detach(&buf, NULL);
2028 strbuf_release(&buf);
2032 char *synthesize_perf_probe_command(struct perf_probe_event *pev)
2035 char *tmp, *ret = NULL;
2038 if (strbuf_init(&buf, 64))
2041 if (strbuf_addf(&buf, "%s:%s=", pev->group ?: PERFPROBE_GROUP,
2045 tmp = synthesize_perf_probe_point(&pev->point);
2046 if (!tmp || strbuf_addstr(&buf, tmp) < 0)
2050 for (i = 0; i < pev->nargs; i++) {
2051 tmp = synthesize_perf_probe_arg(pev->args + i);
2052 if (!tmp || strbuf_addf(&buf, " %s", tmp) < 0)
2057 ret = strbuf_detach(&buf, NULL);
2059 strbuf_release(&buf);
2063 static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
2064 struct strbuf *buf, int depth)
2068 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
2073 if (ref->user_access)
2074 err = strbuf_addf(buf, "%s%ld(", "+u", ref->offset);
2076 err = strbuf_addf(buf, "%+ld(", ref->offset);
2077 return (err < 0) ? err : depth;
2080 static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
2083 struct probe_trace_arg_ref *ref = arg->ref;
2086 /* Argument name or separator */
2088 err = strbuf_addf(buf, " %s=", arg->name);
2090 err = strbuf_addch(buf, ' ');
2094 /* Special case: @XXX */
2095 if (arg->value[0] == '@' && arg->ref)
2098 /* Dereferencing arguments */
2100 depth = __synthesize_probe_trace_arg_ref(ref, buf, 1);
2105 /* Print argument value */
2106 if (arg->value[0] == '@' && arg->ref)
2107 err = strbuf_addf(buf, "%s%+ld", arg->value, arg->ref->offset);
2109 err = strbuf_addstr(buf, arg->value);
2112 while (!err && depth--)
2113 err = strbuf_addch(buf, ')');
2115 /* Print argument type */
2116 if (!err && arg->type)
2117 err = strbuf_addf(buf, ":%s", arg->type);
2123 synthesize_uprobe_trace_def(struct probe_trace_event *tev, struct strbuf *buf)
2125 struct probe_trace_point *tp = &tev->point;
2128 err = strbuf_addf(buf, "%s:0x%lx", tp->module, tp->address);
2130 if (err >= 0 && tp->ref_ctr_offset) {
2131 if (!uprobe_ref_ctr_is_supported())
2133 err = strbuf_addf(buf, "(0x%lx)", tp->ref_ctr_offset);
2135 return err >= 0 ? 0 : -1;
2138 char *synthesize_probe_trace_command(struct probe_trace_event *tev)
2140 struct probe_trace_point *tp = &tev->point;
2145 /* Uprobes must have tp->module */
2146 if (tev->uprobes && !tp->module)
2149 if (strbuf_init(&buf, 32) < 0)
2152 if (strbuf_addf(&buf, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
2153 tev->group, tev->event) < 0)
2156 * If tp->address == 0, then this point must be a
2157 * absolute address uprobe.
2158 * try_to_find_absolute_address() should have made
2159 * tp->symbol to "0x0".
2161 if (tev->uprobes && !tp->address) {
2162 if (!tp->symbol || strcmp(tp->symbol, "0x0"))
2166 /* Use the tp->address for uprobes */
2168 err = synthesize_uprobe_trace_def(tev, &buf);
2169 } else if (!strncmp(tp->symbol, "0x", 2)) {
2170 /* Absolute address. See try_to_find_absolute_address() */
2171 err = strbuf_addf(&buf, "%s%s0x%lx", tp->module ?: "",
2172 tp->module ? ":" : "", tp->address);
2174 err = strbuf_addf(&buf, "%s%s%s+%lu", tp->module ?: "",
2175 tp->module ? ":" : "", tp->symbol, tp->offset);
2181 for (i = 0; i < tev->nargs; i++)
2182 if (synthesize_probe_trace_arg(&tev->args[i], &buf) < 0)
2185 ret = strbuf_detach(&buf, NULL);
2187 strbuf_release(&buf);
2191 static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
2192 struct perf_probe_point *pp,
2195 struct symbol *sym = NULL;
2196 struct map *map = NULL;
2197 u64 addr = tp->address;
2201 map = dso__new_map(tp->module);
2204 sym = map__find_symbol(map, addr);
2206 if (tp->symbol && !addr) {
2207 if (kernel_get_symbol_address_by_name(tp->symbol,
2208 &addr, true, false) < 0)
2213 sym = machine__find_kernel_symbol(host_machine, addr, &map);
2220 pp->retprobe = tp->retprobe;
2221 pp->offset = addr - map->unmap_ip(map, sym->start);
2222 pp->function = strdup(sym->name);
2223 ret = pp->function ? 0 : -ENOMEM;
2226 if (map && !is_kprobe) {
2233 static int convert_to_perf_probe_point(struct probe_trace_point *tp,
2234 struct perf_probe_point *pp,
2240 ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe);
2243 ret = find_perf_probe_point_from_map(tp, pp, is_kprobe);
2247 pr_debug("Failed to find probe point from both of dwarf and map.\n");
2250 pp->function = strdup(tp->symbol);
2251 pp->offset = tp->offset;
2253 ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address);
2256 pp->function = strdup(buf);
2259 if (pp->function == NULL)
2262 pp->retprobe = tp->retprobe;
2267 static int convert_to_perf_probe_event(struct probe_trace_event *tev,
2268 struct perf_probe_event *pev, bool is_kprobe)
2270 struct strbuf buf = STRBUF_INIT;
2273 /* Convert event/group name */
2274 pev->event = strdup(tev->event);
2275 pev->group = strdup(tev->group);
2276 if (pev->event == NULL || pev->group == NULL)
2279 /* Convert trace_point to probe_point */
2280 ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
2284 /* Convert trace_arg to probe_arg */
2285 pev->nargs = tev->nargs;
2286 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
2287 if (pev->args == NULL)
2289 for (i = 0; i < tev->nargs && ret >= 0; i++) {
2290 if (tev->args[i].name)
2291 pev->args[i].name = strdup(tev->args[i].name);
2293 if ((ret = strbuf_init(&buf, 32)) < 0)
2295 ret = synthesize_probe_trace_arg(&tev->args[i], &buf);
2296 pev->args[i].name = strbuf_detach(&buf, NULL);
2298 if (pev->args[i].name == NULL && ret >= 0)
2303 clear_perf_probe_event(pev);
2308 void clear_perf_probe_event(struct perf_probe_event *pev)
2310 struct perf_probe_arg_field *field, *next;
2315 zfree(&pev->target);
2316 clear_perf_probe_point(&pev->point);
2318 for (i = 0; i < pev->nargs; i++) {
2319 zfree(&pev->args[i].name);
2320 zfree(&pev->args[i].var);
2321 zfree(&pev->args[i].type);
2322 field = pev->args[i].field;
2325 zfree(&field->name);
2334 #define strdup_or_goto(str, label) \
2335 ({ char *__p = NULL; if (str && !(__p = strdup(str))) goto label; __p; })
2337 static int perf_probe_point__copy(struct perf_probe_point *dst,
2338 struct perf_probe_point *src)
2340 dst->file = strdup_or_goto(src->file, out_err);
2341 dst->function = strdup_or_goto(src->function, out_err);
2342 dst->lazy_line = strdup_or_goto(src->lazy_line, out_err);
2343 dst->line = src->line;
2344 dst->retprobe = src->retprobe;
2345 dst->offset = src->offset;
2349 clear_perf_probe_point(dst);
2353 static int perf_probe_arg__copy(struct perf_probe_arg *dst,
2354 struct perf_probe_arg *src)
2356 struct perf_probe_arg_field *field, **ppfield;
2358 dst->name = strdup_or_goto(src->name, out_err);
2359 dst->var = strdup_or_goto(src->var, out_err);
2360 dst->type = strdup_or_goto(src->type, out_err);
2363 ppfield = &(dst->field);
2365 *ppfield = zalloc(sizeof(*field));
2368 (*ppfield)->name = strdup_or_goto(field->name, out_err);
2369 (*ppfield)->index = field->index;
2370 (*ppfield)->ref = field->ref;
2371 field = field->next;
2372 ppfield = &((*ppfield)->next);
2379 int perf_probe_event__copy(struct perf_probe_event *dst,
2380 struct perf_probe_event *src)
2384 dst->event = strdup_or_goto(src->event, out_err);
2385 dst->group = strdup_or_goto(src->group, out_err);
2386 dst->target = strdup_or_goto(src->target, out_err);
2387 dst->uprobes = src->uprobes;
2389 if (perf_probe_point__copy(&dst->point, &src->point) < 0)
2392 dst->args = zalloc(sizeof(struct perf_probe_arg) * src->nargs);
2395 dst->nargs = src->nargs;
2397 for (i = 0; i < src->nargs; i++)
2398 if (perf_probe_arg__copy(&dst->args[i], &src->args[i]) < 0)
2403 clear_perf_probe_event(dst);
2407 void clear_probe_trace_event(struct probe_trace_event *tev)
2409 struct probe_trace_arg_ref *ref, *next;
2414 zfree(&tev->point.symbol);
2415 zfree(&tev->point.realname);
2416 zfree(&tev->point.module);
2417 for (i = 0; i < tev->nargs; i++) {
2418 zfree(&tev->args[i].name);
2419 zfree(&tev->args[i].value);
2420 zfree(&tev->args[i].type);
2421 ref = tev->args[i].ref;
2432 struct kprobe_blacklist_node {
2433 struct list_head list;
2434 unsigned long start;
2439 static void kprobe_blacklist__delete(struct list_head *blacklist)
2441 struct kprobe_blacklist_node *node;
2443 while (!list_empty(blacklist)) {
2444 node = list_first_entry(blacklist,
2445 struct kprobe_blacklist_node, list);
2446 list_del_init(&node->list);
2447 zfree(&node->symbol);
2452 static int kprobe_blacklist__load(struct list_head *blacklist)
2454 struct kprobe_blacklist_node *node;
2455 const char *__debugfs = debugfs__mountpoint();
2456 char buf[PATH_MAX], *p;
2460 if (__debugfs == NULL)
2463 ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs);
2467 fp = fopen(buf, "r");
2472 while (fgets(buf, PATH_MAX, fp)) {
2473 node = zalloc(sizeof(*node));
2478 INIT_LIST_HEAD(&node->list);
2479 list_add_tail(&node->list, blacklist);
2480 if (sscanf(buf, "0x%lx-0x%lx", &node->start, &node->end) != 2) {
2484 p = strchr(buf, '\t');
2487 if (p[strlen(p) - 1] == '\n')
2488 p[strlen(p) - 1] = '\0';
2490 p = (char *)"unknown";
2491 node->symbol = strdup(p);
2492 if (!node->symbol) {
2496 pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
2497 node->start, node->end, node->symbol);
2501 kprobe_blacklist__delete(blacklist);
2507 static struct kprobe_blacklist_node *
2508 kprobe_blacklist__find_by_address(struct list_head *blacklist,
2509 unsigned long address)
2511 struct kprobe_blacklist_node *node;
2513 list_for_each_entry(node, blacklist, list) {
2514 if (node->start <= address && address < node->end)
2521 static LIST_HEAD(kprobe_blacklist);
2523 static void kprobe_blacklist__init(void)
2525 if (!list_empty(&kprobe_blacklist))
2528 if (kprobe_blacklist__load(&kprobe_blacklist) < 0)
2529 pr_debug("No kprobe blacklist support, ignored\n");
2532 static void kprobe_blacklist__release(void)
2534 kprobe_blacklist__delete(&kprobe_blacklist);
2537 static bool kprobe_blacklist__listed(unsigned long address)
2539 return !!kprobe_blacklist__find_by_address(&kprobe_blacklist, address);
2542 static int perf_probe_event__sprintf(const char *group, const char *event,
2543 struct perf_probe_event *pev,
2545 struct strbuf *result)
2550 if (asprintf(&buf, "%s:%s", group, event) < 0)
2552 ret = strbuf_addf(result, " %-20s (on ", buf);
2557 /* Synthesize only event probe point */
2558 buf = synthesize_perf_probe_point(&pev->point);
2561 ret = strbuf_addstr(result, buf);
2565 ret = strbuf_addf(result, " in %s", module);
2567 if (!ret && pev->nargs > 0) {
2568 ret = strbuf_add(result, " with", 5);
2569 for (i = 0; !ret && i < pev->nargs; i++) {
2570 buf = synthesize_perf_probe_arg(&pev->args[i]);
2573 ret = strbuf_addf(result, " %s", buf);
2578 ret = strbuf_addch(result, ')');
2584 int show_perf_probe_event(const char *group, const char *event,
2585 struct perf_probe_event *pev,
2586 const char *module, bool use_stdout)
2588 struct strbuf buf = STRBUF_INIT;
2591 ret = perf_probe_event__sprintf(group, event, pev, module, &buf);
2594 printf("%s\n", buf.buf);
2596 pr_info("%s\n", buf.buf);
2598 strbuf_release(&buf);
2603 static bool filter_probe_trace_event(struct probe_trace_event *tev,
2604 struct strfilter *filter)
2608 /* At first, check the event name itself */
2609 if (strfilter__compare(filter, tev->event))
2612 /* Next, check the combination of name and group */
2613 if (e_snprintf(tmp, 128, "%s:%s", tev->group, tev->event) < 0)
2615 return strfilter__compare(filter, tmp);
2618 static int __show_perf_probe_events(int fd, bool is_kprobe,
2619 struct strfilter *filter)
2622 struct probe_trace_event tev;
2623 struct perf_probe_event pev;
2624 struct strlist *rawlist;
2625 struct str_node *ent;
2627 memset(&tev, 0, sizeof(tev));
2628 memset(&pev, 0, sizeof(pev));
2630 rawlist = probe_file__get_rawlist(fd);
2634 strlist__for_each_entry(ent, rawlist) {
2635 ret = parse_probe_trace_command(ent->s, &tev);
2637 if (!filter_probe_trace_event(&tev, filter))
2639 ret = convert_to_perf_probe_event(&tev, &pev,
2643 ret = show_perf_probe_event(pev.group, pev.event,
2644 &pev, tev.point.module,
2648 clear_perf_probe_event(&pev);
2649 clear_probe_trace_event(&tev);
2653 strlist__delete(rawlist);
2654 /* Cleanup cached debuginfo if needed */
2655 debuginfo_cache__exit();
2660 /* List up current perf-probe events */
2661 int show_perf_probe_events(struct strfilter *filter)
2663 int kp_fd, up_fd, ret;
2667 if (probe_conf.cache)
2668 return probe_cache__show_all_caches(filter);
2670 ret = init_probe_symbol_maps(false);
2674 ret = probe_file__open_both(&kp_fd, &up_fd, 0);
2679 ret = __show_perf_probe_events(kp_fd, true, filter);
2680 if (up_fd >= 0 && ret >= 0)
2681 ret = __show_perf_probe_events(up_fd, false, filter);
2686 exit_probe_symbol_maps();
2691 static int get_new_event_name(char *buf, size_t len, const char *base,
2692 struct strlist *namelist, bool ret_event,
2700 nbase = strdup(base);
2704 /* Cut off the dot suffixes (e.g. .const, .isra) and version suffixes */
2705 p = strpbrk(nbase, ".@");
2706 if (p && p != nbase)
2709 /* Try no suffix number */
2710 ret = e_snprintf(buf, len, "%s%s", nbase, ret_event ? "__return" : "");
2712 pr_debug("snprintf() failed: %d\n", ret);
2715 if (!strlist__has_entry(namelist, buf))
2718 if (!allow_suffix) {
2719 pr_warning("Error: event \"%s\" already exists.\n"
2720 " Hint: Remove existing event by 'perf probe -d'\n"
2721 " or force duplicates by 'perf probe -f'\n"
2722 " or set 'force=yes' in BPF source.\n",
2728 /* Try to add suffix */
2729 for (i = 1; i < MAX_EVENT_INDEX; i++) {
2730 ret = e_snprintf(buf, len, "%s_%d", nbase, i);
2732 pr_debug("snprintf() failed: %d\n", ret);
2735 if (!strlist__has_entry(namelist, buf))
2738 if (i == MAX_EVENT_INDEX) {
2739 pr_warning("Too many events are on the same function.\n");
2746 /* Final validation */
2747 if (ret >= 0 && !is_c_func_name(buf)) {
2748 pr_warning("Internal error: \"%s\" is an invalid event name.\n",
2756 /* Warn if the current kernel's uprobe implementation is old */
2757 static void warn_uprobe_event_compat(struct probe_trace_event *tev)
2760 char *buf = synthesize_probe_trace_command(tev);
2761 struct probe_trace_point *tp = &tev->point;
2763 if (tp->ref_ctr_offset && !uprobe_ref_ctr_is_supported()) {
2764 pr_warning("A semaphore is associated with %s:%s and "
2765 "seems your kernel doesn't support it.\n",
2766 tev->group, tev->event);
2769 /* Old uprobe event doesn't support memory dereference */
2770 if (!tev->uprobes || tev->nargs == 0 || !buf)
2773 for (i = 0; i < tev->nargs; i++)
2774 if (strglobmatch(tev->args[i].value, "[$@+-]*")) {
2775 pr_warning("Please upgrade your kernel to at least "
2776 "3.14 to have access to feature %s\n",
2777 tev->args[i].value);
2784 /* Set new name from original perf_probe_event and namelist */
2785 static int probe_trace_event__set_name(struct probe_trace_event *tev,
2786 struct perf_probe_event *pev,
2787 struct strlist *namelist,
2790 const char *event, *group;
2794 /* If probe_event or trace_event already have the name, reuse it */
2795 if (pev->event && !pev->sdt)
2797 else if (tev->event)
2800 /* Or generate new one from probe point */
2801 if (pev->point.function &&
2802 (strncmp(pev->point.function, "0x", 2) != 0) &&
2803 !strisglob(pev->point.function))
2804 event = pev->point.function;
2806 event = tev->point.realname;
2808 if (pev->group && !pev->sdt)
2810 else if (tev->group)
2813 group = PERFPROBE_GROUP;
2815 /* Get an unused new event name */
2816 ret = get_new_event_name(buf, 64, event, namelist,
2817 tev->point.retprobe, allow_suffix);
2823 tev->event = strdup(event);
2824 tev->group = strdup(group);
2825 if (tev->event == NULL || tev->group == NULL)
2829 * Add new event name to namelist if multiprobe event is NOT
2830 * supported, since we have to use new event name for following
2831 * probes in that case.
2833 if (!multiprobe_event_is_supported())
2834 strlist__add(namelist, event);
2838 static int __open_probe_file_and_namelist(bool uprobe,
2839 struct strlist **namelist)
2843 fd = probe_file__open(PF_FL_RW | (uprobe ? PF_FL_UPROBE : 0));
2847 /* Get current event names */
2848 *namelist = probe_file__get_namelist(fd);
2850 pr_debug("Failed to get current event list.\n");
2857 static int __add_probe_trace_events(struct perf_probe_event *pev,
2858 struct probe_trace_event *tevs,
2859 int ntevs, bool allow_suffix)
2861 int i, fd[2] = {-1, -1}, up, ret;
2862 struct probe_trace_event *tev = NULL;
2863 struct probe_cache *cache = NULL;
2864 struct strlist *namelist[2] = {NULL, NULL};
2865 struct nscookie nsc;
2867 up = pev->uprobes ? 1 : 0;
2868 fd[up] = __open_probe_file_and_namelist(up, &namelist[up]);
2873 for (i = 0; i < ntevs; i++) {
2875 up = tev->uprobes ? 1 : 0;
2876 if (fd[up] == -1) { /* Open the kprobe/uprobe_events */
2877 fd[up] = __open_probe_file_and_namelist(up,
2882 /* Skip if the symbol is out of .text or blacklisted */
2883 if (!tev->point.symbol && !pev->uprobes)
2886 /* Set new name for tev (and update namelist) */
2887 ret = probe_trace_event__set_name(tev, pev, namelist[up],
2892 nsinfo__mountns_enter(pev->nsi, &nsc);
2893 ret = probe_file__add_event(fd[up], tev);
2894 nsinfo__mountns_exit(&nsc);
2899 * Probes after the first probe which comes from same
2900 * user input are always allowed to add suffix, because
2901 * there might be several addresses corresponding to
2904 allow_suffix = true;
2906 if (ret == -EINVAL && pev->uprobes)
2907 warn_uprobe_event_compat(tev);
2908 if (ret == 0 && probe_conf.cache) {
2909 cache = probe_cache__new(pev->target, pev->nsi);
2911 probe_cache__add_entry(cache, pev, tevs, ntevs) < 0 ||
2912 probe_cache__commit(cache) < 0)
2913 pr_warning("Failed to add event to probe cache\n");
2914 probe_cache__delete(cache);
2918 for (up = 0; up < 2; up++) {
2919 strlist__delete(namelist[up]);
2926 static int find_probe_functions(struct map *map, char *name,
2927 struct symbol **syms)
2931 struct rb_node *tmp;
2932 const char *norm, *ver;
2934 bool cut_version = true;
2936 if (map__load(map) < 0)
2939 /* If user gives a version, don't cut off the version from symbols */
2940 if (strchr(name, '@'))
2941 cut_version = false;
2943 map__for_each_symbol(map, sym, tmp) {
2944 norm = arch__normalize_symbol_name(sym->name);
2949 /* We don't care about default symbol or not */
2950 ver = strchr(norm, '@');
2952 buf = strndup(norm, ver - norm);
2959 if (strglobmatch(norm, name)) {
2961 if (syms && found < probe_conf.max_probes)
2962 syms[found - 1] = sym;
2971 void __weak arch__fix_tev_from_maps(struct perf_probe_event *pev __maybe_unused,
2972 struct probe_trace_event *tev __maybe_unused,
2973 struct map *map __maybe_unused,
2974 struct symbol *sym __maybe_unused) { }
2977 * Find probe function addresses from map.
2978 * Return an error or the number of found probe_trace_event
2980 static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
2981 struct probe_trace_event **tevs)
2983 struct map *map = NULL;
2984 struct ref_reloc_sym *reloc_sym = NULL;
2986 struct symbol **syms = NULL;
2987 struct probe_trace_event *tev;
2988 struct perf_probe_point *pp = &pev->point;
2989 struct probe_trace_point *tp;
2990 int num_matched_functions;
2991 int ret, i, j, skipped = 0;
2994 map = get_target_map(pev->target, pev->nsi, pev->uprobes);
3000 syms = malloc(sizeof(struct symbol *) * probe_conf.max_probes);
3007 * Load matched symbols: Since the different local symbols may have
3008 * same name but different addresses, this lists all the symbols.
3010 num_matched_functions = find_probe_functions(map, pp->function, syms);
3011 if (num_matched_functions <= 0) {
3012 pr_err("Failed to find symbol %s in %s\n", pp->function,
3013 pev->target ? : "kernel");
3016 } else if (num_matched_functions > probe_conf.max_probes) {
3017 pr_err("Too many functions matched in %s\n",
3018 pev->target ? : "kernel");
3023 /* Note that the symbols in the kmodule are not relocated */
3024 if (!pev->uprobes && !pev->target &&
3025 (!pp->retprobe || kretprobe_offset_is_supported())) {
3026 reloc_sym = kernel_get_ref_reloc_sym(NULL);
3028 pr_warning("Relocated base symbol is not found!\n");
3034 /* Setup result trace-probe-events */
3035 *tevs = zalloc(sizeof(*tev) * num_matched_functions);
3043 for (j = 0; j < num_matched_functions; j++) {
3046 /* There can be duplicated symbols in the map */
3047 for (i = 0; i < j; i++)
3048 if (sym->start == syms[i]->start) {
3049 pr_debug("Found duplicated symbol %s @ %" PRIx64 "\n",
3050 sym->name, sym->start);
3056 tev = (*tevs) + ret;
3058 if (ret == num_matched_functions) {
3059 pr_warning("Too many symbols are listed. Skip it.\n");
3064 if (pp->offset > sym->end - sym->start) {
3065 pr_warning("Offset %ld is bigger than the size of %s\n",
3066 pp->offset, sym->name);
3070 /* Add one probe point */
3071 tp->address = map->unmap_ip(map, sym->start) + pp->offset;
3073 /* Check the kprobe (not in module) is within .text */
3074 if (!pev->uprobes && !pev->target &&
3075 kprobe_warn_out_range(sym->name, tp->address)) {
3076 tp->symbol = NULL; /* Skip it */
3078 } else if (reloc_sym) {
3079 tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
3080 tp->offset = tp->address - reloc_sym->addr;
3082 tp->symbol = strdup_or_goto(sym->name, nomem_out);
3083 tp->offset = pp->offset;
3085 tp->realname = strdup_or_goto(sym->name, nomem_out);
3087 tp->retprobe = pp->retprobe;
3090 tev->point.module = strdup_or_goto(pev->target,
3093 mod_name = find_module_name(pev->target);
3095 strdup(mod_name ? mod_name : pev->target);
3097 if (!tev->point.module)
3101 tev->uprobes = pev->uprobes;
3102 tev->nargs = pev->nargs;
3104 tev->args = zalloc(sizeof(struct probe_trace_arg) *
3106 if (tev->args == NULL)
3109 for (i = 0; i < tev->nargs; i++) {
3110 if (pev->args[i].name)
3112 strdup_or_goto(pev->args[i].name,
3115 tev->args[i].value = strdup_or_goto(pev->args[i].var,
3117 if (pev->args[i].type)
3119 strdup_or_goto(pev->args[i].type,
3122 arch__fix_tev_from_maps(pev, tev, map, sym);
3124 if (ret == skipped) {
3137 clear_probe_trace_events(*tevs, num_matched_functions);
3142 static int try_to_find_absolute_address(struct perf_probe_event *pev,
3143 struct probe_trace_event **tevs)
3145 struct perf_probe_point *pp = &pev->point;
3146 struct probe_trace_event *tev;
3147 struct probe_trace_point *tp;
3150 if (!(pev->point.function && !strncmp(pev->point.function, "0x", 2)))
3152 if (perf_probe_event_need_dwarf(pev))
3156 * This is 'perf probe /lib/libc.so 0xabcd'. Try to probe at
3159 * Only one tev can be generated by this.
3161 *tevs = zalloc(sizeof(*tev));
3169 * Don't use tp->offset, use address directly, because
3170 * in synthesize_probe_trace_command() address cannot be
3173 tp->address = pev->point.abs_address;
3174 tp->retprobe = pp->retprobe;
3175 tev->uprobes = pev->uprobes;
3179 * Give it a '0x' leading symbol name.
3180 * In __add_probe_trace_events, a NULL symbol is interpreted as
3183 if (asprintf(&tp->symbol, "0x%lx", tp->address) < 0)
3186 /* For kprobe, check range */
3187 if ((!tev->uprobes) &&
3188 (kprobe_warn_out_range(tev->point.symbol,
3189 tev->point.address))) {
3194 if (asprintf(&tp->realname, "abs_%lx", tp->address) < 0)
3198 tp->module = strdup(pev->target);
3204 tev->group = strdup(pev->group);
3210 tev->event = strdup(pev->event);
3215 tev->nargs = pev->nargs;
3216 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
3220 for (i = 0; i < tev->nargs; i++)
3221 copy_to_probe_trace_arg(&tev->args[i], &pev->args[i]);
3226 clear_probe_trace_events(*tevs, 1);
3231 /* Concatinate two arrays */
3232 static void *memcat(void *a, size_t sz_a, void *b, size_t sz_b)
3236 ret = malloc(sz_a + sz_b);
3238 memcpy(ret, a, sz_a);
3239 memcpy(ret + sz_a, b, sz_b);
3245 concat_probe_trace_events(struct probe_trace_event **tevs, int *ntevs,
3246 struct probe_trace_event **tevs2, int ntevs2)
3248 struct probe_trace_event *new_tevs;
3258 if (*ntevs + ntevs2 > probe_conf.max_probes)
3261 /* Concatinate the array of probe_trace_event */
3262 new_tevs = memcat(*tevs, (*ntevs) * sizeof(**tevs),
3263 *tevs2, ntevs2 * sizeof(**tevs2));
3273 clear_probe_trace_events(*tevs2, ntevs2);
3280 * Try to find probe_trace_event from given probe caches. Return the number
3281 * of cached events found, if an error occurs return the error.
3283 static int find_cached_events(struct perf_probe_event *pev,
3284 struct probe_trace_event **tevs,
3287 struct probe_cache *cache;
3288 struct probe_cache_entry *entry;
3289 struct probe_trace_event *tmp_tevs = NULL;
3293 cache = probe_cache__new(target, pev->nsi);
3294 /* Return 0 ("not found") if the target has no probe cache. */
3298 for_each_probe_cache_entry(entry, cache) {
3299 /* Skip the cache entry which has no name */
3300 if (!entry->pev.event || !entry->pev.group)
3302 if ((!pev->group || strglobmatch(entry->pev.group, pev->group)) &&
3303 strglobmatch(entry->pev.event, pev->event)) {
3304 ret = probe_cache_entry__get_event(entry, &tmp_tevs);
3306 ret = concat_probe_trace_events(tevs, &ntevs,
3312 probe_cache__delete(cache);
3314 clear_probe_trace_events(*tevs, ntevs);
3318 if (ntevs > 0 && target && target[0] == '/')
3319 pev->uprobes = true;
3325 /* Try to find probe_trace_event from all probe caches */
3326 static int find_cached_events_all(struct perf_probe_event *pev,
3327 struct probe_trace_event **tevs)
3329 struct probe_trace_event *tmp_tevs = NULL;
3330 struct strlist *bidlist;
3331 struct str_node *nd;
3336 /* Get the buildid list of all valid caches */
3337 bidlist = build_id_cache__list_all(true);
3340 pr_debug("Failed to get buildids: %d\n", ret);
3345 strlist__for_each_entry(nd, bidlist) {
3346 pathname = build_id_cache__origname(nd->s);
3347 ret = find_cached_events(pev, &tmp_tevs, pathname);
3348 /* In the case of cnt == 0, we just skip it */
3350 ret = concat_probe_trace_events(tevs, &ntevs,
3356 strlist__delete(bidlist);
3359 clear_probe_trace_events(*tevs, ntevs);
3367 static int find_probe_trace_events_from_cache(struct perf_probe_event *pev,
3368 struct probe_trace_event **tevs)
3370 struct probe_cache *cache;
3371 struct probe_cache_entry *entry;
3372 struct probe_trace_event *tev;
3373 struct str_node *node;
3377 /* For SDT/cached events, we use special search functions */
3379 return find_cached_events_all(pev, tevs);
3381 return find_cached_events(pev, tevs, pev->target);
3383 cache = probe_cache__new(pev->target, pev->nsi);
3387 entry = probe_cache__find(cache, pev);
3389 /* SDT must be in the cache */
3390 ret = pev->sdt ? -ENOENT : 0;
3394 ret = strlist__nr_entries(entry->tevlist);
3395 if (ret > probe_conf.max_probes) {
3396 pr_debug("Too many entries matched in the cache of %s\n",
3397 pev->target ? : "kernel");
3402 *tevs = zalloc(ret * sizeof(*tev));
3409 strlist__for_each_entry(node, entry->tevlist) {
3410 tev = &(*tevs)[i++];
3411 ret = parse_probe_trace_command(node->s, tev);
3414 /* Set the uprobes attribute as same as original */
3415 tev->uprobes = pev->uprobes;
3420 probe_cache__delete(cache);
3424 static int convert_to_probe_trace_events(struct perf_probe_event *pev,
3425 struct probe_trace_event **tevs)
3429 if (!pev->group && !pev->sdt) {
3430 /* Set group name if not given */
3431 if (!pev->uprobes) {
3432 pev->group = strdup(PERFPROBE_GROUP);
3433 ret = pev->group ? 0 : -ENOMEM;
3435 ret = convert_exec_to_group(pev->target, &pev->group);
3437 pr_warning("Failed to make a group name.\n");
3442 ret = try_to_find_absolute_address(pev, tevs);
3446 /* At first, we need to lookup cache entry */
3447 ret = find_probe_trace_events_from_cache(pev, tevs);
3448 if (ret > 0 || pev->sdt) /* SDT can be found only in the cache */
3449 return ret == 0 ? -ENOENT : ret; /* Found in probe cache */
3451 /* Convert perf_probe_event with debuginfo */
3452 ret = try_to_find_probe_trace_events(pev, tevs);
3454 return ret; /* Found in debuginfo or got an error */
3456 return find_probe_trace_events_from_map(pev, tevs);
3459 int convert_perf_probe_events(struct perf_probe_event *pevs, int npevs)
3463 /* Loop 1: convert all events */
3464 for (i = 0; i < npevs; i++) {
3465 /* Init kprobe blacklist if needed */
3466 if (!pevs[i].uprobes)
3467 kprobe_blacklist__init();
3468 /* Convert with or without debuginfo */
3469 ret = convert_to_probe_trace_events(&pevs[i], &pevs[i].tevs);
3472 pevs[i].ntevs = ret;
3474 /* This just release blacklist only if allocated */
3475 kprobe_blacklist__release();
3480 static int show_probe_trace_event(struct probe_trace_event *tev)
3482 char *buf = synthesize_probe_trace_command(tev);
3485 pr_debug("Failed to synthesize probe trace event.\n");
3489 /* Showing definition always go stdout */
3490 printf("%s\n", buf);
3496 int show_probe_trace_events(struct perf_probe_event *pevs, int npevs)
3498 struct strlist *namelist = strlist__new(NULL, NULL);
3499 struct probe_trace_event *tev;
3500 struct perf_probe_event *pev;
3506 for (j = 0; j < npevs && !ret; j++) {
3508 for (i = 0; i < pev->ntevs && !ret; i++) {
3509 tev = &pev->tevs[i];
3510 /* Skip if the symbol is out of .text or blacklisted */
3511 if (!tev->point.symbol && !pev->uprobes)
3514 /* Set new name for tev (and update namelist) */
3515 ret = probe_trace_event__set_name(tev, pev,
3518 ret = show_probe_trace_event(tev);
3521 strlist__delete(namelist);
3526 int apply_perf_probe_events(struct perf_probe_event *pevs, int npevs)
3530 /* Loop 2: add all events */
3531 for (i = 0; i < npevs; i++) {
3532 ret = __add_probe_trace_events(&pevs[i], pevs[i].tevs,
3534 probe_conf.force_add);
3541 void cleanup_perf_probe_events(struct perf_probe_event *pevs, int npevs)
3544 struct perf_probe_event *pev;
3546 /* Loop 3: cleanup and free trace events */
3547 for (i = 0; i < npevs; i++) {
3549 for (j = 0; j < pevs[i].ntevs; j++)
3550 clear_probe_trace_event(&pevs[i].tevs[j]);
3551 zfree(&pevs[i].tevs);
3553 nsinfo__zput(pev->nsi);
3554 clear_perf_probe_event(&pevs[i]);
3558 int add_perf_probe_events(struct perf_probe_event *pevs, int npevs)
3562 ret = init_probe_symbol_maps(pevs->uprobes);
3566 ret = convert_perf_probe_events(pevs, npevs);
3568 ret = apply_perf_probe_events(pevs, npevs);
3570 cleanup_perf_probe_events(pevs, npevs);
3572 exit_probe_symbol_maps();
3576 int del_perf_probe_events(struct strfilter *filter)
3578 int ret, ret2, ufd = -1, kfd = -1;
3579 char *str = strfilter__string(filter);
3584 /* Get current event names */
3585 ret = probe_file__open_both(&kfd, &ufd, PF_FL_RW);
3589 ret = probe_file__del_events(kfd, filter);
3590 if (ret < 0 && ret != -ENOENT)
3593 ret2 = probe_file__del_events(ufd, filter);
3594 if (ret2 < 0 && ret2 != -ENOENT) {
3611 int show_available_funcs(const char *target, struct nsinfo *nsi,
3612 struct strfilter *_filter, bool user)
3618 ret = init_probe_symbol_maps(user);
3622 /* Get a symbol map */
3623 map = get_target_map(target, nsi, user);
3625 pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
3629 ret = map__load(map);
3632 char *str = strfilter__string(_filter);
3633 pr_err("Failed to find symbols matched to \"%s\"\n",
3637 pr_err("Failed to load symbols in %s\n",
3638 (target) ? : "kernel");
3641 if (!dso__sorted_by_name(map->dso))
3642 dso__sort_by_name(map->dso);
3644 /* Show all (filtered) symbols */
3647 for (nd = rb_first_cached(&map->dso->symbol_names); nd;
3649 struct symbol_name_rb_node *pos = rb_entry(nd, struct symbol_name_rb_node, rb_node);
3651 if (strfilter__compare(_filter, pos->sym.name))
3652 printf("%s\n", pos->sym.name);
3656 exit_probe_symbol_maps();
3661 int copy_to_probe_trace_arg(struct probe_trace_arg *tvar,
3662 struct perf_probe_arg *pvar)
3664 tvar->value = strdup(pvar->var);
3665 if (tvar->value == NULL)
3668 tvar->type = strdup(pvar->type);
3669 if (tvar->type == NULL)
3673 tvar->name = strdup(pvar->name);
3674 if (tvar->name == NULL)