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);
183 if (map && map->dso) {
184 nsinfo__put(map->dso->nsinfo);
185 map->dso->nsinfo = nsinfo__get(nsi);
189 return kernel_get_module_map(target);
193 static int convert_exec_to_group(const char *exec, char **result)
195 char *ptr1, *ptr2, *exec_copy;
199 exec_copy = strdup(exec);
203 ptr1 = basename(exec_copy);
209 for (ptr2 = ptr1; *ptr2 != '\0'; ptr2++) {
210 if (!isalnum(*ptr2) && *ptr2 != '_') {
216 ret = e_snprintf(buf, 64, "%s_%s", PERFPROBE_GROUP, ptr1);
220 *result = strdup(buf);
221 ret = *result ? 0 : -ENOMEM;
228 static void clear_perf_probe_point(struct perf_probe_point *pp)
231 zfree(&pp->function);
232 zfree(&pp->lazy_line);
235 static void clear_probe_trace_events(struct probe_trace_event *tevs, int ntevs)
239 for (i = 0; i < ntevs; i++)
240 clear_probe_trace_event(tevs + i);
243 static bool kprobe_blacklist__listed(unsigned long address);
244 static bool kprobe_warn_out_range(const char *symbol, unsigned long address)
249 map = kernel_get_module_map(NULL);
251 ret = address <= map->start || map->end < address;
253 pr_warning("%s is out of .text, skip it.\n", symbol);
256 if (!ret && kprobe_blacklist__listed(address)) {
257 pr_warning("%s is blacklisted function, skip it.\n", symbol);
265 * @module can be module name of module file path. In case of path,
266 * inspect elf and find out what is actual module name.
267 * Caller has to free mod_name after using it.
269 static char *find_module_name(const char *module)
277 char *mod_name = NULL;
280 fd = open(module, O_RDONLY);
284 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
288 if (gelf_getehdr(elf, &ehdr) == NULL)
291 sec = elf_section_by_name(elf, &ehdr, &shdr,
292 ".gnu.linkonce.this_module", NULL);
296 data = elf_getdata(sec, NULL);
297 if (!data || !data->d_buf)
302 * '.gnu.linkonce.this_module' section of kernel module elf directly
303 * maps to 'struct module' from linux/module.h. This section contains
304 * actual module name which will be used by kernel after loading it.
305 * But, we cannot use 'struct module' here since linux/module.h is not
306 * exposed to user-space. Offset of 'name' has remained same from long
307 * time, so hardcoding it here.
309 if (ehdr.e_ident[EI_CLASS] == ELFCLASS32)
311 else /* expect ELFCLASS64 by default */
314 mod_name = strdup((char *)data->d_buf + name_offset);
323 #ifdef HAVE_DWARF_SUPPORT
325 static int kernel_get_module_dso(const char *module, struct dso **pdso)
329 const char *vmlinux_name;
333 char module_name[128];
335 snprintf(module_name, sizeof(module_name), "[%s]", module);
336 map = maps__find_by_name(&host_machine->kmaps, module_name);
341 pr_debug("Failed to find module %s.\n", module);
345 map = machine__kernel_map(host_machine);
347 if (!dso->has_build_id)
348 dso__read_running_kernel_build_id(dso, host_machine);
350 vmlinux_name = symbol_conf.vmlinux_name;
353 ret = dso__load_vmlinux(dso, map, vmlinux_name, false);
355 ret = dso__load_vmlinux_path(dso, map);
362 * Some binaries like glibc have special symbols which are on the symbol
363 * table, but not in the debuginfo. If we can find the address of the
364 * symbol from map, we can translate the address back to the probe point.
366 static int find_alternative_probe_point(struct debuginfo *dinfo,
367 struct perf_probe_point *pp,
368 struct perf_probe_point *result,
369 const char *target, struct nsinfo *nsi,
372 struct map *map = NULL;
377 /* This can work only for function-name based one */
378 if (!pp->function || pp->file)
381 map = get_target_map(target, nsi, uprobes);
385 /* Find the address of given function */
386 map__for_each_symbol_by_name(map, pp->function, sym) {
388 address = sym->start;
389 if (sym->type == STT_GNU_IFUNC)
390 pr_warning("Warning: The probe function (%s) is a GNU indirect function.\n"
391 "Consider identifying the final function used at run time and set the probe directly on that.\n",
394 address = map->unmap_ip(map, sym->start) - map->reloc;
401 pr_debug("Symbol %s address found : %" PRIx64 "\n",
402 pp->function, address);
404 ret = debuginfo__find_probe_point(dinfo, (unsigned long)address,
407 ret = (!ret) ? -ENOENT : ret;
409 result->offset += pp->offset;
410 result->line += pp->line;
411 result->retprobe = pp->retprobe;
421 static int get_alternative_probe_event(struct debuginfo *dinfo,
422 struct perf_probe_event *pev,
423 struct perf_probe_point *tmp)
427 memcpy(tmp, &pev->point, sizeof(*tmp));
428 memset(&pev->point, 0, sizeof(pev->point));
429 ret = find_alternative_probe_point(dinfo, tmp, &pev->point, pev->target,
430 pev->nsi, pev->uprobes);
432 memcpy(&pev->point, tmp, sizeof(*tmp));
437 static int get_alternative_line_range(struct debuginfo *dinfo,
438 struct line_range *lr,
439 const char *target, bool user)
441 struct perf_probe_point pp = { .function = lr->function,
444 struct perf_probe_point result;
447 memset(&result, 0, sizeof(result));
449 if (lr->end != INT_MAX)
450 len = lr->end - lr->start;
451 ret = find_alternative_probe_point(dinfo, &pp, &result,
454 lr->function = result.function;
455 lr->file = result.file;
456 lr->start = result.line;
457 if (lr->end != INT_MAX)
458 lr->end = lr->start + len;
459 clear_perf_probe_point(&pp);
464 #ifdef HAVE_DEBUGINFOD_SUPPORT
465 static struct debuginfo *open_from_debuginfod(struct dso *dso, struct nsinfo *nsi,
468 debuginfod_client *c = debuginfod_begin();
469 char sbuild_id[SBUILD_ID_SIZE + 1];
470 struct debuginfo *ret = NULL;
478 build_id__sprintf(&dso->bid, sbuild_id);
479 fd = debuginfod_find_debuginfo(c, (const unsigned char *)sbuild_id,
486 pr_debug("Failed to find debuginfo in debuginfod.\n");
490 pr_debug("Load debuginfo from debuginfod (%s)\n", path);
492 nsinfo__mountns_enter(nsi, &nsc);
493 ret = debuginfo__new((const char *)path);
494 nsinfo__mountns_exit(&nsc);
499 struct debuginfo *open_from_debuginfod(struct dso *dso __maybe_unused,
500 struct nsinfo *nsi __maybe_unused,
501 bool silent __maybe_unused)
507 /* Open new debuginfo of given module */
508 static struct debuginfo *open_debuginfo(const char *module, struct nsinfo *nsi,
511 const char *path = module;
512 char reason[STRERR_BUFSIZE];
513 struct debuginfo *ret = NULL;
514 struct dso *dso = NULL;
518 if (!module || !strchr(module, '/')) {
519 err = kernel_get_module_dso(module, &dso);
521 if (!dso || dso->load_errno == 0) {
522 if (!str_error_r(-err, reason, STRERR_BUFSIZE))
523 strcpy(reason, "(unknown)");
525 dso__strerror_load(dso, reason, STRERR_BUFSIZE);
527 ret = open_from_debuginfod(dso, nsi, silent);
532 pr_err("Module %s is not loaded, please specify its full path name.\n", module);
534 pr_err("Failed to find the path for the kernel: %s\n", reason);
538 path = dso->long_name;
540 nsinfo__mountns_enter(nsi, &nsc);
541 ret = debuginfo__new(path);
542 if (!ret && !silent) {
543 pr_warning("The %s file has no debug information.\n", path);
544 if (!module || !strtailcmp(path, ".ko"))
545 pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, ");
547 pr_warning("Rebuild with -g, ");
548 pr_warning("or install an appropriate debuginfo package.\n");
550 nsinfo__mountns_exit(&nsc);
554 /* For caching the last debuginfo */
555 static struct debuginfo *debuginfo_cache;
556 static char *debuginfo_cache_path;
558 static struct debuginfo *debuginfo_cache__open(const char *module, bool silent)
560 const char *path = module;
562 /* If the module is NULL, it should be the kernel. */
566 if (debuginfo_cache_path && !strcmp(debuginfo_cache_path, path))
569 /* Copy module path */
570 free(debuginfo_cache_path);
571 debuginfo_cache_path = strdup(path);
572 if (!debuginfo_cache_path) {
573 debuginfo__delete(debuginfo_cache);
574 debuginfo_cache = NULL;
578 debuginfo_cache = open_debuginfo(module, NULL, silent);
579 if (!debuginfo_cache)
580 zfree(&debuginfo_cache_path);
582 return debuginfo_cache;
585 static void debuginfo_cache__exit(void)
587 debuginfo__delete(debuginfo_cache);
588 debuginfo_cache = NULL;
589 zfree(&debuginfo_cache_path);
593 static int get_text_start_address(const char *exec, unsigned long *address,
599 int fd, ret = -ENOENT;
602 nsinfo__mountns_enter(nsi, &nsc);
603 fd = open(exec, O_RDONLY);
604 nsinfo__mountns_exit(&nsc);
608 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
614 if (gelf_getehdr(elf, &ehdr) == NULL)
617 if (!elf_section_by_name(elf, &ehdr, &shdr, ".text", NULL))
620 *address = shdr.sh_addr - shdr.sh_offset;
631 * Convert trace point to probe point with debuginfo
633 static int find_perf_probe_point_from_dwarf(struct probe_trace_point *tp,
634 struct perf_probe_point *pp,
637 struct debuginfo *dinfo = NULL;
638 unsigned long stext = 0;
639 u64 addr = tp->address;
642 /* convert the address to dwarf address */
648 ret = get_text_start_address(tp->module, &stext, NULL);
652 } else if (tp->symbol) {
653 /* If the module is given, this returns relative address */
654 ret = kernel_get_symbol_address_by_name(tp->symbol, &addr,
655 false, !!tp->module);
661 pr_debug("try to find information at %" PRIx64 " in %s\n", addr,
662 tp->module ? : "kernel");
664 dinfo = debuginfo_cache__open(tp->module, verbose <= 0);
666 ret = debuginfo__find_probe_point(dinfo,
667 (unsigned long)addr, pp);
672 pp->retprobe = tp->retprobe;
676 pr_debug("Failed to find corresponding probes from debuginfo.\n");
677 return ret ? : -ENOENT;
680 /* Adjust symbol name and address */
681 static int post_process_probe_trace_point(struct probe_trace_point *tp,
682 struct map *map, unsigned long offs)
685 u64 addr = tp->address - offs;
687 sym = map__find_symbol(map, addr);
691 if (strcmp(sym->name, tp->symbol)) {
692 /* If we have no realname, use symbol for it */
694 tp->realname = tp->symbol;
697 tp->symbol = strdup(sym->name);
701 tp->offset = addr - sym->start;
708 * Rename DWARF symbols to ELF symbols -- gcc sometimes optimizes functions
709 * and generate new symbols with suffixes such as .constprop.N or .isra.N
710 * etc. Since those symbols are not recorded in DWARF, we have to find
711 * correct generated symbols from offline ELF binary.
712 * For online kernel or uprobes we don't need this because those are
713 * rebased on _text, or already a section relative address.
716 post_process_offline_probe_trace_events(struct probe_trace_event *tevs,
717 int ntevs, const char *pathname)
720 unsigned long stext = 0;
723 /* Prepare a map for offline binary */
724 map = dso__new_map(pathname);
725 if (!map || get_text_start_address(pathname, &stext, NULL) < 0) {
726 pr_warning("Failed to get ELF symbols for %s\n", pathname);
730 for (i = 0; i < ntevs; i++) {
731 ret = post_process_probe_trace_point(&tevs[i].point,
741 static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs,
742 int ntevs, const char *exec,
746 unsigned long stext = 0;
751 ret = get_text_start_address(exec, &stext, nsi);
755 for (i = 0; i < ntevs && ret >= 0; i++) {
756 /* point.address is the address of point.symbol + point.offset */
757 tevs[i].point.address -= stext;
758 tevs[i].point.module = strdup(exec);
759 if (!tevs[i].point.module) {
763 tevs[i].uprobes = true;
770 post_process_module_probe_trace_events(struct probe_trace_event *tevs,
771 int ntevs, const char *module,
772 struct debuginfo *dinfo)
774 Dwarf_Addr text_offs = 0;
776 char *mod_name = NULL;
782 map = get_target_map(module, NULL, false);
783 if (!map || debuginfo__get_text_offset(dinfo, &text_offs, true) < 0) {
784 pr_warning("Failed to get ELF symbols for %s\n", module);
788 mod_name = find_module_name(module);
789 for (i = 0; i < ntevs; i++) {
790 ret = post_process_probe_trace_point(&tevs[i].point,
791 map, (unsigned long)text_offs);
794 tevs[i].point.module =
795 strdup(mod_name ? mod_name : module);
796 if (!tevs[i].point.module) {
809 post_process_kernel_probe_trace_events(struct probe_trace_event *tevs,
812 struct ref_reloc_sym *reloc_sym;
817 /* Skip post process if the target is an offline kernel */
818 if (symbol_conf.ignore_vmlinux_buildid)
819 return post_process_offline_probe_trace_events(tevs, ntevs,
820 symbol_conf.vmlinux_name);
822 reloc_sym = kernel_get_ref_reloc_sym(&map);
824 pr_warning("Relocated base symbol is not found!\n");
828 for (i = 0; i < ntevs; i++) {
829 if (!tevs[i].point.address)
831 if (tevs[i].point.retprobe && !kretprobe_offset_is_supported())
834 * If we found a wrong one, mark it by NULL symbol.
835 * Since addresses in debuginfo is same as objdump, we need
836 * to convert it to addresses on memory.
838 if (kprobe_warn_out_range(tevs[i].point.symbol,
839 map__objdump_2mem(map, tevs[i].point.address))) {
843 tmp = strdup(reloc_sym->name);
847 /* If we have no realname, use symbol for it */
848 if (!tevs[i].point.realname)
849 tevs[i].point.realname = tevs[i].point.symbol;
851 free(tevs[i].point.symbol);
852 tevs[i].point.symbol = tmp;
853 tevs[i].point.offset = tevs[i].point.address -
854 (map->reloc ? reloc_sym->unrelocated_addr :
861 arch__post_process_probe_trace_events(struct perf_probe_event *pev __maybe_unused,
862 int ntevs __maybe_unused)
866 /* Post processing the probe events */
867 static int post_process_probe_trace_events(struct perf_probe_event *pev,
868 struct probe_trace_event *tevs,
869 int ntevs, const char *module,
870 bool uprobe, struct debuginfo *dinfo)
875 ret = add_exec_to_probe_trace_events(tevs, ntevs, module,
878 /* Currently ref_reloc_sym based probe is not for drivers */
879 ret = post_process_module_probe_trace_events(tevs, ntevs,
882 ret = post_process_kernel_probe_trace_events(tevs, ntevs);
885 arch__post_process_probe_trace_events(pev, ntevs);
890 /* Try to find perf_probe_event with debuginfo */
891 static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
892 struct probe_trace_event **tevs)
894 bool need_dwarf = perf_probe_event_need_dwarf(pev);
895 struct perf_probe_point tmp;
896 struct debuginfo *dinfo;
899 /* Workaround for gcc #98776 issue.
900 * Perf failed to add kretprobe event with debuginfo of vmlinux which is
901 * compiled by gcc with -fpatchable-function-entry option enabled. The
902 * same issue with kernel module. The retprobe doesn`t need debuginfo.
903 * This workaround solution use map to query the probe function address
904 * for retprobe event.
906 if (pev->point.retprobe)
909 dinfo = open_debuginfo(pev->target, pev->nsi, !need_dwarf);
913 pr_debug("Could not open debuginfo. Try to use symbols.\n");
917 pr_debug("Try to find probe point from debuginfo.\n");
918 /* Searching trace events corresponding to a probe event */
919 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
921 if (ntevs == 0) { /* Not found, retry with an alternative */
922 ret = get_alternative_probe_event(dinfo, pev, &tmp);
924 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
926 * Write back to the original probe_event for
927 * setting appropriate (user given) event name
929 clear_perf_probe_point(&pev->point);
930 memcpy(&pev->point, &tmp, sizeof(tmp));
934 if (ntevs > 0) { /* Succeeded to find trace events */
935 pr_debug("Found %d probe_trace_events.\n", ntevs);
936 ret = post_process_probe_trace_events(pev, *tevs, ntevs,
937 pev->target, pev->uprobes, dinfo);
938 if (ret < 0 || ret == ntevs) {
939 pr_debug("Post processing failed or all events are skipped. (%d)\n", ret);
940 clear_probe_trace_events(*tevs, ntevs);
946 debuginfo__delete(dinfo);
948 if (ntevs == 0) { /* No error but failed to find probe point. */
949 pr_warning("Probe point '%s' not found.\n",
950 synthesize_perf_probe_point(&pev->point));
952 } else if (ntevs < 0) {
953 /* Error path : ntevs < 0 */
954 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
956 pr_warning("Warning: No dwarf info found in the vmlinux - "
957 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
959 pr_debug("Trying to use symbols.\n");
966 #define LINEBUF_SIZE 256
967 #define NR_ADDITIONAL_LINES 2
969 static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
971 char buf[LINEBUF_SIZE], sbuf[STRERR_BUFSIZE];
972 const char *color = show_num ? "" : PERF_COLOR_BLUE;
973 const char *prefix = NULL;
976 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
981 prefix = show_num ? "%7d " : " ";
982 color_fprintf(stdout, color, prefix, l);
984 color_fprintf(stdout, color, "%s", buf);
986 } while (strchr(buf, '\n') == NULL);
991 pr_warning("File read error: %s\n",
992 str_error_r(errno, sbuf, sizeof(sbuf)));
998 static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
1000 int rv = __show_one_line(fp, l, skip, show_num);
1002 pr_warning("Source file is shorter than expected.\n");
1008 #define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
1009 #define show_one_line(f,l) _show_one_line(f,l,false,false)
1010 #define skip_one_line(f,l) _show_one_line(f,l,true,false)
1011 #define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
1014 * Show line-range always requires debuginfo to find source file and
1017 static int __show_line_range(struct line_range *lr, const char *module,
1020 struct build_id bid;
1022 struct int_node *ln;
1023 struct debuginfo *dinfo;
1027 char sbuf[STRERR_BUFSIZE];
1028 char sbuild_id[SBUILD_ID_SIZE] = "";
1030 /* Search a line range */
1031 dinfo = open_debuginfo(module, NULL, false);
1035 ret = debuginfo__find_line_range(dinfo, lr);
1036 if (!ret) { /* Not found, retry with an alternative */
1037 ret = get_alternative_line_range(dinfo, lr, module, user);
1039 ret = debuginfo__find_line_range(dinfo, lr);
1041 if (dinfo->build_id) {
1042 build_id__init(&bid, dinfo->build_id, BUILD_ID_SIZE);
1043 build_id__sprintf(&bid, sbuild_id);
1045 debuginfo__delete(dinfo);
1046 if (ret == 0 || ret == -ENOENT) {
1047 pr_warning("Specified source line is not found.\n");
1049 } else if (ret < 0) {
1050 pr_warning("Debuginfo analysis failed.\n");
1054 /* Convert source file path */
1056 ret = find_source_path(tmp, sbuild_id, lr->comp_dir, &lr->path);
1058 /* Free old path when new path is assigned */
1059 if (tmp != lr->path)
1063 pr_warning("Failed to find source file path.\n");
1070 fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
1071 lr->start - lr->offset);
1073 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
1075 fp = fopen(lr->path, "r");
1077 pr_warning("Failed to open %s: %s\n", lr->path,
1078 str_error_r(errno, sbuf, sizeof(sbuf)));
1081 /* Skip to starting line number */
1082 while (l < lr->start) {
1083 ret = skip_one_line(fp, l++);
1088 intlist__for_each_entry(ln, lr->line_list) {
1089 for (; ln->i > (unsigned long)l; l++) {
1090 ret = show_one_line(fp, l - lr->offset);
1094 ret = show_one_line_with_num(fp, l++ - lr->offset);
1099 if (lr->end == INT_MAX)
1100 lr->end = l + NR_ADDITIONAL_LINES;
1101 while (l <= lr->end) {
1102 ret = show_one_line_or_eof(fp, l++ - lr->offset);
1111 int show_line_range(struct line_range *lr, const char *module,
1112 struct nsinfo *nsi, bool user)
1115 struct nscookie nsc;
1117 ret = init_probe_symbol_maps(user);
1120 nsinfo__mountns_enter(nsi, &nsc);
1121 ret = __show_line_range(lr, module, user);
1122 nsinfo__mountns_exit(&nsc);
1123 exit_probe_symbol_maps();
1128 static int show_available_vars_at(struct debuginfo *dinfo,
1129 struct perf_probe_event *pev,
1130 struct strfilter *_filter)
1134 struct str_node *node;
1135 struct variable_list *vls = NULL, *vl;
1136 struct perf_probe_point tmp;
1139 buf = synthesize_perf_probe_point(&pev->point);
1142 pr_debug("Searching variables at %s\n", buf);
1144 ret = debuginfo__find_available_vars_at(dinfo, pev, &vls);
1145 if (!ret) { /* Not found, retry with an alternative */
1146 ret = get_alternative_probe_event(dinfo, pev, &tmp);
1148 ret = debuginfo__find_available_vars_at(dinfo, pev,
1150 /* Release the old probe_point */
1151 clear_perf_probe_point(&tmp);
1155 if (ret == 0 || ret == -ENOENT) {
1156 pr_err("Failed to find the address of %s\n", buf);
1159 pr_warning("Debuginfo analysis failed.\n");
1163 /* Some variables are found */
1164 fprintf(stdout, "Available variables at %s\n", buf);
1165 for (i = 0; i < ret; i++) {
1168 * A probe point might be converted to
1169 * several trace points.
1171 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
1173 zfree(&vl->point.symbol);
1176 strlist__for_each_entry(node, vl->vars) {
1177 var = strchr(node->s, '\t') + 1;
1178 if (strfilter__compare(_filter, var)) {
1179 fprintf(stdout, "\t\t%s\n", node->s);
1183 strlist__delete(vl->vars);
1186 fprintf(stdout, "\t\t(No matched variables)\n");
1194 /* Show available variables on given probe point */
1195 int show_available_vars(struct perf_probe_event *pevs, int npevs,
1196 struct strfilter *_filter)
1199 struct debuginfo *dinfo;
1201 ret = init_probe_symbol_maps(pevs->uprobes);
1205 dinfo = open_debuginfo(pevs->target, pevs->nsi, false);
1213 for (i = 0; i < npevs && ret >= 0; i++)
1214 ret = show_available_vars_at(dinfo, &pevs[i], _filter);
1216 debuginfo__delete(dinfo);
1218 exit_probe_symbol_maps();
1222 #else /* !HAVE_DWARF_SUPPORT */
1224 static void debuginfo_cache__exit(void)
1229 find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused,
1230 struct perf_probe_point *pp __maybe_unused,
1231 bool is_kprobe __maybe_unused)
1236 static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
1237 struct probe_trace_event **tevs __maybe_unused)
1239 if (perf_probe_event_need_dwarf(pev)) {
1240 pr_warning("Debuginfo-analysis is not supported.\n");
1247 int show_line_range(struct line_range *lr __maybe_unused,
1248 const char *module __maybe_unused,
1249 struct nsinfo *nsi __maybe_unused,
1250 bool user __maybe_unused)
1252 pr_warning("Debuginfo-analysis is not supported.\n");
1256 int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
1257 int npevs __maybe_unused,
1258 struct strfilter *filter __maybe_unused)
1260 pr_warning("Debuginfo-analysis is not supported.\n");
1265 void line_range__clear(struct line_range *lr)
1267 zfree(&lr->function);
1270 zfree(&lr->comp_dir);
1271 intlist__delete(lr->line_list);
1274 int line_range__init(struct line_range *lr)
1276 memset(lr, 0, sizeof(*lr));
1277 lr->line_list = intlist__new(NULL);
1284 static int parse_line_num(char **ptr, int *val, const char *what)
1286 const char *start = *ptr;
1289 *val = strtol(*ptr, ptr, 0);
1290 if (errno || *ptr == start) {
1291 semantic_error("'%s' is not a valid number.\n", what);
1297 /* Check the name is good for event, group or function */
1298 static bool is_c_func_name(const char *name)
1300 if (!isalpha(*name) && *name != '_')
1302 while (*++name != '\0') {
1303 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
1310 * Stuff 'lr' according to the line range described by 'arg'.
1311 * The line range syntax is described by:
1313 * SRC[:SLN[+NUM|-ELN]]
1314 * FNC[@SRC][:SLN[+NUM|-ELN]]
1316 int parse_line_range_desc(const char *arg, struct line_range *lr)
1318 char *range, *file, *name = strdup(arg);
1327 range = strchr(name, ':');
1331 err = parse_line_num(&range, &lr->start, "start line");
1335 if (*range == '+' || *range == '-') {
1336 const char c = *range++;
1338 err = parse_line_num(&range, &lr->end, "end line");
1343 lr->end += lr->start;
1345 * Adjust the number of lines here.
1346 * If the number of lines == 1, the
1347 * the end of line should be equal to
1348 * the start of line.
1354 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
1357 if (lr->start > lr->end) {
1358 semantic_error("Start line must be smaller"
1359 " than end line.\n");
1362 if (*range != '\0') {
1363 semantic_error("Tailing with invalid str '%s'.\n", range);
1368 file = strchr(name, '@');
1371 lr->file = strdup(++file);
1372 if (lr->file == NULL) {
1376 lr->function = name;
1377 } else if (strchr(name, '/') || strchr(name, '.'))
1379 else if (is_c_func_name(name))/* We reuse it for checking funcname */
1380 lr->function = name;
1381 else { /* Invalid name */
1382 semantic_error("'%s' is not a valid function name.\n", name);
1393 static int parse_perf_probe_event_name(char **arg, struct perf_probe_event *pev)
1397 ptr = strpbrk_esc(*arg, ":");
1400 if (!pev->sdt && !is_c_func_name(*arg))
1402 pev->group = strdup_esc(*arg);
1409 pev->event = strdup_esc(*arg);
1410 if (pev->event == NULL)
1413 if (!pev->sdt && !is_c_func_name(pev->event)) {
1417 semantic_error("%s is bad for event name -it must "
1418 "follow C symbol-naming rule.\n", *arg);
1424 /* Parse probepoint definition. */
1425 static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
1427 struct perf_probe_point *pp = &pev->point;
1430 bool file_spec = false;
1435 * perf probe [GRP:][EVENT=]SRC[:LN|;PTN]
1436 * perf probe [GRP:][EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
1437 * perf probe %[GRP:]SDT_EVENT
1442 if (is_sdt_event(arg)) {
1448 ptr = strpbrk_esc(arg, ";=@+%");
1452 semantic_error("%s must be an SDT name.\n",
1456 /* This must be a target file name or build id */
1457 tmp = build_id_cache__complement(ptr + 1);
1459 pev->target = build_id_cache__origname(tmp);
1462 pev->target = strdup_esc(ptr + 1);
1467 ret = parse_perf_probe_event_name(&arg, pev);
1469 if (asprintf(&pev->point.function, "%%%s", pev->event) < 0)
1475 if (ptr && *ptr == '=') { /* Event name */
1478 ret = parse_perf_probe_event_name(&arg, pev);
1486 * Check arg is function or file name and copy it.
1488 * We consider arg to be a file spec if and only if it satisfies
1489 * all of the below criteria::
1490 * - it does not include any of "+@%",
1491 * - it includes one of ":;", and
1492 * - it has a period '.' in the name.
1494 * Otherwise, we consider arg to be a function specification.
1496 if (!strpbrk_esc(arg, "+@%")) {
1497 ptr = strpbrk_esc(arg, ";:");
1498 /* This is a file spec if it includes a '.' before ; or : */
1499 if (ptr && memchr(arg, '.', ptr - arg))
1503 ptr = strpbrk_esc(arg, ";:+@%");
1512 tmp = strdup_esc(arg);
1523 * Keep pp->function even if this is absolute address,
1524 * so it can mark whether abs_address is valid.
1525 * Which make 'perf probe lib.bin 0x0' possible.
1527 * Note that checking length of tmp is not needed
1528 * because when we access tmp[1] we know tmp[0] is '0',
1529 * so tmp[1] should always valid (but could be '\0').
1531 if (tmp && !strncmp(tmp, "0x", 2)) {
1532 pp->abs_address = strtoul(pp->function, &tmp, 0);
1534 semantic_error("Invalid absolute address.\n");
1540 /* Parse other options */
1544 if (c == ';') { /* Lazy pattern must be the last part */
1545 pp->lazy_line = strdup(arg); /* let leave escapes */
1546 if (pp->lazy_line == NULL)
1550 ptr = strpbrk_esc(arg, ";:+@%");
1556 case ':': /* Line number */
1557 pp->line = strtoul(arg, &tmp, 0);
1559 semantic_error("There is non-digit char"
1560 " in line number.\n");
1564 case '+': /* Byte offset from a symbol */
1565 pp->offset = strtoul(arg, &tmp, 0);
1567 semantic_error("There is non-digit character"
1572 case '@': /* File name */
1574 semantic_error("SRC@SRC is not allowed.\n");
1577 pp->file = strdup_esc(arg);
1578 if (pp->file == NULL)
1581 case '%': /* Probe places */
1582 if (strcmp(arg, "return") == 0) {
1584 } else { /* Others not supported yet */
1585 semantic_error("%%%s is not supported.\n", arg);
1589 default: /* Buggy case */
1590 pr_err("This program has a bug at %s:%d.\n",
1591 __FILE__, __LINE__);
1597 /* Exclusion check */
1598 if (pp->lazy_line && pp->line) {
1599 semantic_error("Lazy pattern can't be used with"
1604 if (pp->lazy_line && pp->offset) {
1605 semantic_error("Lazy pattern can't be used with offset.\n");
1609 if (pp->line && pp->offset) {
1610 semantic_error("Offset can't be used with line number.\n");
1614 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
1615 semantic_error("File always requires line number or "
1620 if (pp->offset && !pp->function) {
1621 semantic_error("Offset requires an entry function.\n");
1625 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
1626 semantic_error("Offset/Line/Lazy pattern can't be used with "
1631 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
1632 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
1637 /* Parse perf-probe event argument */
1638 static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
1640 char *tmp, *goodname;
1641 struct perf_probe_arg_field **fieldp;
1643 pr_debug("parsing arg: %s into ", str);
1645 tmp = strchr(str, '=');
1647 arg->name = strndup(str, tmp - str);
1648 if (arg->name == NULL)
1650 pr_debug("name:%s ", arg->name);
1654 tmp = strchr(str, '@');
1655 if (tmp && tmp != str && !strcmp(tmp + 1, "user")) { /* user attr */
1656 if (!user_access_is_supported()) {
1657 semantic_error("ftrace does not support user access\n");
1661 arg->user_access = true;
1662 pr_debug("user_access ");
1665 tmp = strchr(str, ':');
1666 if (tmp) { /* Type setting */
1668 arg->type = strdup(tmp + 1);
1669 if (arg->type == NULL)
1671 pr_debug("type:%s ", arg->type);
1674 tmp = strpbrk(str, "-.[");
1675 if (!is_c_varname(str) || !tmp) {
1676 /* A variable, register, symbol or special value */
1677 arg->var = strdup(str);
1678 if (arg->var == NULL)
1680 pr_debug("%s\n", arg->var);
1684 /* Structure fields or array element */
1685 arg->var = strndup(str, tmp - str);
1686 if (arg->var == NULL)
1688 goodname = arg->var;
1689 pr_debug("%s, ", arg->var);
1690 fieldp = &arg->field;
1693 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
1694 if (*fieldp == NULL)
1696 if (*tmp == '[') { /* Array */
1698 (*fieldp)->index = strtol(str + 1, &tmp, 0);
1699 (*fieldp)->ref = true;
1700 if (*tmp != ']' || tmp == str + 1) {
1701 semantic_error("Array index must be a"
1708 } else { /* Structure */
1711 (*fieldp)->ref = false;
1712 } else if (tmp[1] == '>') {
1714 (*fieldp)->ref = true;
1716 semantic_error("Argument parse error: %s\n",
1720 tmp = strpbrk(str, "-.[");
1723 (*fieldp)->name = strndup(str, tmp - str);
1724 if ((*fieldp)->name == NULL)
1727 goodname = (*fieldp)->name;
1728 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
1729 fieldp = &(*fieldp)->next;
1732 (*fieldp)->name = strdup(str);
1733 if ((*fieldp)->name == NULL)
1736 goodname = (*fieldp)->name;
1737 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
1739 /* If no name is specified, set the last field name (not array index)*/
1741 arg->name = strdup(goodname);
1742 if (arg->name == NULL)
1748 /* Parse perf-probe event command */
1749 int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
1752 int argc, i, ret = 0;
1754 argv = argv_split(cmd, &argc);
1756 pr_debug("Failed to split arguments.\n");
1759 if (argc - 1 > MAX_PROBE_ARGS) {
1760 semantic_error("Too many probe arguments (%d).\n", argc - 1);
1764 /* Parse probe point */
1765 ret = parse_perf_probe_point(argv[0], pev);
1769 /* Generate event name if needed */
1770 if (!pev->event && pev->point.function && pev->point.line
1771 && !pev->point.lazy_line && !pev->point.offset) {
1772 if (asprintf(&pev->event, "%s_L%d", pev->point.function,
1773 pev->point.line) < 0)
1777 /* Copy arguments and ensure return probe has no C argument */
1778 pev->nargs = argc - 1;
1779 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1780 if (pev->args == NULL) {
1784 for (i = 0; i < pev->nargs && ret >= 0; i++) {
1785 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1787 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
1788 semantic_error("You can't specify local variable for"
1799 /* Returns true if *any* ARG is either C variable, $params or $vars. */
1800 bool perf_probe_with_var(struct perf_probe_event *pev)
1804 for (i = 0; i < pev->nargs; i++)
1805 if (is_c_varname(pev->args[i].var) ||
1806 !strcmp(pev->args[i].var, PROBE_ARG_PARAMS) ||
1807 !strcmp(pev->args[i].var, PROBE_ARG_VARS))
1812 /* Return true if this perf_probe_event requires debuginfo */
1813 bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
1815 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1818 if (perf_probe_with_var(pev))
1824 /* Parse probe_events event into struct probe_point */
1825 int parse_probe_trace_command(const char *cmd, struct probe_trace_event *tev)
1827 struct probe_trace_point *tp = &tev->point;
1830 char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
1834 pr_debug("Parsing probe_events: %s\n", cmd);
1835 argv = argv_split(cmd, &argc);
1837 pr_debug("Failed to split arguments.\n");
1841 semantic_error("Too few probe arguments.\n");
1846 /* Scan event and group name. */
1847 argv0_str = strdup(argv[0]);
1848 if (argv0_str == NULL) {
1852 fmt1_str = strtok_r(argv0_str, ":", &fmt);
1853 fmt2_str = strtok_r(NULL, "/", &fmt);
1854 fmt3_str = strtok_r(NULL, " \t", &fmt);
1855 if (fmt1_str == NULL || fmt2_str == NULL || fmt3_str == NULL) {
1856 semantic_error("Failed to parse event name: %s\n", argv[0]);
1861 tev->group = strdup(fmt2_str);
1862 tev->event = strdup(fmt3_str);
1863 if (tev->group == NULL || tev->event == NULL) {
1867 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
1869 tp->retprobe = (pr == 'r');
1871 /* Scan module name(if there), function name and offset */
1872 p = strchr(argv[1], ':');
1874 tp->module = strndup(argv[1], p - argv[1]);
1879 tev->uprobes = (tp->module[0] == '/');
1883 fmt1_str = strtok_r(p, "+", &fmt);
1884 /* only the address started with 0x */
1885 if (fmt1_str[0] == '0') {
1887 * Fix a special case:
1888 * if address == 0, kernel reports something like:
1889 * p:probe_libc/abs_0 /lib/libc-2.18.so:0x (null) arg1=%ax
1890 * Newer kernel may fix that, but we want to
1891 * support old kernel also.
1893 if (strcmp(fmt1_str, "0x") == 0) {
1894 if (!argv[2] || strcmp(argv[2], "(null)")) {
1901 for (i = 2; argv[i + 1] != NULL; i++)
1902 argv[i] = argv[i + 1];
1907 tp->address = strtoul(fmt1_str, NULL, 0);
1909 /* Only the symbol-based probe has offset */
1910 tp->symbol = strdup(fmt1_str);
1911 if (tp->symbol == NULL) {
1915 fmt2_str = strtok_r(NULL, "", &fmt);
1916 if (fmt2_str == NULL)
1919 tp->offset = strtoul(fmt2_str, NULL, 10);
1923 fmt2_str = strchr(p, '(');
1925 tp->ref_ctr_offset = strtoul(fmt2_str + 1, NULL, 0);
1928 tev->nargs = argc - 2;
1929 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
1930 if (tev->args == NULL) {
1934 for (i = 0; i < tev->nargs; i++) {
1935 p = strchr(argv[i + 2], '=');
1936 if (p) /* We don't need which register is assigned. */
1940 tev->args[i].name = strdup(argv[i + 2]);
1941 /* TODO: parse regs and offset */
1942 tev->args[i].value = strdup(p);
1943 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1955 /* Compose only probe arg */
1956 char *synthesize_perf_probe_arg(struct perf_probe_arg *pa)
1958 struct perf_probe_arg_field *field = pa->field;
1963 if (strbuf_init(&buf, 64) < 0)
1966 if (pa->name && pa->var)
1967 err = strbuf_addf(&buf, "%s=%s", pa->name, pa->var);
1969 err = strbuf_addstr(&buf, pa->name ?: pa->var);
1974 if (field->name[0] == '[')
1975 err = strbuf_addstr(&buf, field->name);
1977 err = strbuf_addf(&buf, "%s%s", field->ref ? "->" : ".",
1979 field = field->next;
1985 if (strbuf_addf(&buf, ":%s", pa->type) < 0)
1988 ret = strbuf_detach(&buf, NULL);
1990 strbuf_release(&buf);
1994 /* Compose only probe point (not argument) */
1995 char *synthesize_perf_probe_point(struct perf_probe_point *pp)
1998 char *tmp, *ret = NULL;
2001 if (strbuf_init(&buf, 64) < 0)
2005 if (strbuf_addstr(&buf, pp->function) < 0)
2008 err = strbuf_addf(&buf, "+%lu", pp->offset);
2010 err = strbuf_addf(&buf, ":%d", pp->line);
2011 else if (pp->retprobe)
2012 err = strbuf_addstr(&buf, "%return");
2020 tmp = strchr(pp->file + len - 30, '/');
2021 tmp = tmp ? tmp + 1 : pp->file + len - 30;
2023 err = strbuf_addf(&buf, "@%s", tmp);
2024 if (!err && !pp->function && pp->line)
2025 err = strbuf_addf(&buf, ":%d", pp->line);
2028 ret = strbuf_detach(&buf, NULL);
2030 strbuf_release(&buf);
2034 char *synthesize_perf_probe_command(struct perf_probe_event *pev)
2037 char *tmp, *ret = NULL;
2040 if (strbuf_init(&buf, 64))
2043 if (strbuf_addf(&buf, "%s:%s=", pev->group ?: PERFPROBE_GROUP,
2047 tmp = synthesize_perf_probe_point(&pev->point);
2048 if (!tmp || strbuf_addstr(&buf, tmp) < 0)
2052 for (i = 0; i < pev->nargs; i++) {
2053 tmp = synthesize_perf_probe_arg(pev->args + i);
2054 if (!tmp || strbuf_addf(&buf, " %s", tmp) < 0)
2059 ret = strbuf_detach(&buf, NULL);
2061 strbuf_release(&buf);
2065 static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
2066 struct strbuf *buf, int depth)
2070 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
2075 if (ref->user_access)
2076 err = strbuf_addf(buf, "%s%ld(", "+u", ref->offset);
2078 err = strbuf_addf(buf, "%+ld(", ref->offset);
2079 return (err < 0) ? err : depth;
2082 static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
2085 struct probe_trace_arg_ref *ref = arg->ref;
2088 /* Argument name or separator */
2090 err = strbuf_addf(buf, " %s=", arg->name);
2092 err = strbuf_addch(buf, ' ');
2096 /* Special case: @XXX */
2097 if (arg->value[0] == '@' && arg->ref)
2100 /* Dereferencing arguments */
2102 depth = __synthesize_probe_trace_arg_ref(ref, buf, 1);
2107 /* Print argument value */
2108 if (arg->value[0] == '@' && arg->ref)
2109 err = strbuf_addf(buf, "%s%+ld", arg->value, arg->ref->offset);
2111 err = strbuf_addstr(buf, arg->value);
2114 while (!err && depth--)
2115 err = strbuf_addch(buf, ')');
2117 /* Print argument type */
2118 if (!err && arg->type)
2119 err = strbuf_addf(buf, ":%s", arg->type);
2125 synthesize_uprobe_trace_def(struct probe_trace_event *tev, struct strbuf *buf)
2127 struct probe_trace_point *tp = &tev->point;
2130 err = strbuf_addf(buf, "%s:0x%lx", tp->module, tp->address);
2132 if (err >= 0 && tp->ref_ctr_offset) {
2133 if (!uprobe_ref_ctr_is_supported())
2135 err = strbuf_addf(buf, "(0x%lx)", tp->ref_ctr_offset);
2137 return err >= 0 ? 0 : -1;
2140 char *synthesize_probe_trace_command(struct probe_trace_event *tev)
2142 struct probe_trace_point *tp = &tev->point;
2147 /* Uprobes must have tp->module */
2148 if (tev->uprobes && !tp->module)
2151 if (strbuf_init(&buf, 32) < 0)
2154 if (strbuf_addf(&buf, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
2155 tev->group, tev->event) < 0)
2158 * If tp->address == 0, then this point must be a
2159 * absolute address uprobe.
2160 * try_to_find_absolute_address() should have made
2161 * tp->symbol to "0x0".
2163 if (tev->uprobes && !tp->address) {
2164 if (!tp->symbol || strcmp(tp->symbol, "0x0"))
2168 /* Use the tp->address for uprobes */
2170 err = synthesize_uprobe_trace_def(tev, &buf);
2171 } else if (!strncmp(tp->symbol, "0x", 2)) {
2172 /* Absolute address. See try_to_find_absolute_address() */
2173 err = strbuf_addf(&buf, "%s%s0x%lx", tp->module ?: "",
2174 tp->module ? ":" : "", tp->address);
2176 err = strbuf_addf(&buf, "%s%s%s+%lu", tp->module ?: "",
2177 tp->module ? ":" : "", tp->symbol, tp->offset);
2183 for (i = 0; i < tev->nargs; i++)
2184 if (synthesize_probe_trace_arg(&tev->args[i], &buf) < 0)
2187 ret = strbuf_detach(&buf, NULL);
2189 strbuf_release(&buf);
2193 static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
2194 struct perf_probe_point *pp,
2197 struct symbol *sym = NULL;
2198 struct map *map = NULL;
2199 u64 addr = tp->address;
2203 map = dso__new_map(tp->module);
2206 sym = map__find_symbol(map, addr);
2208 if (tp->symbol && !addr) {
2209 if (kernel_get_symbol_address_by_name(tp->symbol,
2210 &addr, true, false) < 0)
2215 sym = machine__find_kernel_symbol(host_machine, addr, &map);
2222 pp->retprobe = tp->retprobe;
2223 pp->offset = addr - map->unmap_ip(map, sym->start);
2224 pp->function = strdup(sym->name);
2225 ret = pp->function ? 0 : -ENOMEM;
2228 if (map && !is_kprobe) {
2235 static int convert_to_perf_probe_point(struct probe_trace_point *tp,
2236 struct perf_probe_point *pp,
2242 ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe);
2245 ret = find_perf_probe_point_from_map(tp, pp, is_kprobe);
2249 pr_debug("Failed to find probe point from both of dwarf and map.\n");
2252 pp->function = strdup(tp->symbol);
2253 pp->offset = tp->offset;
2255 ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address);
2258 pp->function = strdup(buf);
2261 if (pp->function == NULL)
2264 pp->retprobe = tp->retprobe;
2269 static int convert_to_perf_probe_event(struct probe_trace_event *tev,
2270 struct perf_probe_event *pev, bool is_kprobe)
2272 struct strbuf buf = STRBUF_INIT;
2275 /* Convert event/group name */
2276 pev->event = strdup(tev->event);
2277 pev->group = strdup(tev->group);
2278 if (pev->event == NULL || pev->group == NULL)
2281 /* Convert trace_point to probe_point */
2282 ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
2286 /* Convert trace_arg to probe_arg */
2287 pev->nargs = tev->nargs;
2288 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
2289 if (pev->args == NULL)
2291 for (i = 0; i < tev->nargs && ret >= 0; i++) {
2292 if (tev->args[i].name)
2293 pev->args[i].name = strdup(tev->args[i].name);
2295 if ((ret = strbuf_init(&buf, 32)) < 0)
2297 ret = synthesize_probe_trace_arg(&tev->args[i], &buf);
2298 pev->args[i].name = strbuf_detach(&buf, NULL);
2300 if (pev->args[i].name == NULL && ret >= 0)
2305 clear_perf_probe_event(pev);
2310 void clear_perf_probe_event(struct perf_probe_event *pev)
2312 struct perf_probe_arg_field *field, *next;
2317 zfree(&pev->target);
2318 clear_perf_probe_point(&pev->point);
2320 for (i = 0; i < pev->nargs; i++) {
2321 zfree(&pev->args[i].name);
2322 zfree(&pev->args[i].var);
2323 zfree(&pev->args[i].type);
2324 field = pev->args[i].field;
2327 zfree(&field->name);
2336 #define strdup_or_goto(str, label) \
2337 ({ char *__p = NULL; if (str && !(__p = strdup(str))) goto label; __p; })
2339 static int perf_probe_point__copy(struct perf_probe_point *dst,
2340 struct perf_probe_point *src)
2342 dst->file = strdup_or_goto(src->file, out_err);
2343 dst->function = strdup_or_goto(src->function, out_err);
2344 dst->lazy_line = strdup_or_goto(src->lazy_line, out_err);
2345 dst->line = src->line;
2346 dst->retprobe = src->retprobe;
2347 dst->offset = src->offset;
2351 clear_perf_probe_point(dst);
2355 static int perf_probe_arg__copy(struct perf_probe_arg *dst,
2356 struct perf_probe_arg *src)
2358 struct perf_probe_arg_field *field, **ppfield;
2360 dst->name = strdup_or_goto(src->name, out_err);
2361 dst->var = strdup_or_goto(src->var, out_err);
2362 dst->type = strdup_or_goto(src->type, out_err);
2365 ppfield = &(dst->field);
2367 *ppfield = zalloc(sizeof(*field));
2370 (*ppfield)->name = strdup_or_goto(field->name, out_err);
2371 (*ppfield)->index = field->index;
2372 (*ppfield)->ref = field->ref;
2373 field = field->next;
2374 ppfield = &((*ppfield)->next);
2381 int perf_probe_event__copy(struct perf_probe_event *dst,
2382 struct perf_probe_event *src)
2386 dst->event = strdup_or_goto(src->event, out_err);
2387 dst->group = strdup_or_goto(src->group, out_err);
2388 dst->target = strdup_or_goto(src->target, out_err);
2389 dst->uprobes = src->uprobes;
2391 if (perf_probe_point__copy(&dst->point, &src->point) < 0)
2394 dst->args = zalloc(sizeof(struct perf_probe_arg) * src->nargs);
2397 dst->nargs = src->nargs;
2399 for (i = 0; i < src->nargs; i++)
2400 if (perf_probe_arg__copy(&dst->args[i], &src->args[i]) < 0)
2405 clear_perf_probe_event(dst);
2409 void clear_probe_trace_event(struct probe_trace_event *tev)
2411 struct probe_trace_arg_ref *ref, *next;
2416 zfree(&tev->point.symbol);
2417 zfree(&tev->point.realname);
2418 zfree(&tev->point.module);
2419 for (i = 0; i < tev->nargs; i++) {
2420 zfree(&tev->args[i].name);
2421 zfree(&tev->args[i].value);
2422 zfree(&tev->args[i].type);
2423 ref = tev->args[i].ref;
2434 struct kprobe_blacklist_node {
2435 struct list_head list;
2436 unsigned long start;
2441 static void kprobe_blacklist__delete(struct list_head *blacklist)
2443 struct kprobe_blacklist_node *node;
2445 while (!list_empty(blacklist)) {
2446 node = list_first_entry(blacklist,
2447 struct kprobe_blacklist_node, list);
2448 list_del_init(&node->list);
2449 zfree(&node->symbol);
2454 static int kprobe_blacklist__load(struct list_head *blacklist)
2456 struct kprobe_blacklist_node *node;
2457 const char *__debugfs = debugfs__mountpoint();
2458 char buf[PATH_MAX], *p;
2462 if (__debugfs == NULL)
2465 ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs);
2469 fp = fopen(buf, "r");
2474 while (fgets(buf, PATH_MAX, fp)) {
2475 node = zalloc(sizeof(*node));
2480 INIT_LIST_HEAD(&node->list);
2481 list_add_tail(&node->list, blacklist);
2482 if (sscanf(buf, "0x%lx-0x%lx", &node->start, &node->end) != 2) {
2486 p = strchr(buf, '\t');
2489 if (p[strlen(p) - 1] == '\n')
2490 p[strlen(p) - 1] = '\0';
2492 p = (char *)"unknown";
2493 node->symbol = strdup(p);
2494 if (!node->symbol) {
2498 pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
2499 node->start, node->end, node->symbol);
2503 kprobe_blacklist__delete(blacklist);
2509 static struct kprobe_blacklist_node *
2510 kprobe_blacklist__find_by_address(struct list_head *blacklist,
2511 unsigned long address)
2513 struct kprobe_blacklist_node *node;
2515 list_for_each_entry(node, blacklist, list) {
2516 if (node->start <= address && address < node->end)
2523 static LIST_HEAD(kprobe_blacklist);
2525 static void kprobe_blacklist__init(void)
2527 if (!list_empty(&kprobe_blacklist))
2530 if (kprobe_blacklist__load(&kprobe_blacklist) < 0)
2531 pr_debug("No kprobe blacklist support, ignored\n");
2534 static void kprobe_blacklist__release(void)
2536 kprobe_blacklist__delete(&kprobe_blacklist);
2539 static bool kprobe_blacklist__listed(unsigned long address)
2541 return !!kprobe_blacklist__find_by_address(&kprobe_blacklist, address);
2544 static int perf_probe_event__sprintf(const char *group, const char *event,
2545 struct perf_probe_event *pev,
2547 struct strbuf *result)
2552 if (asprintf(&buf, "%s:%s", group, event) < 0)
2554 ret = strbuf_addf(result, " %-20s (on ", buf);
2559 /* Synthesize only event probe point */
2560 buf = synthesize_perf_probe_point(&pev->point);
2563 ret = strbuf_addstr(result, buf);
2567 ret = strbuf_addf(result, " in %s", module);
2569 if (!ret && pev->nargs > 0) {
2570 ret = strbuf_add(result, " with", 5);
2571 for (i = 0; !ret && i < pev->nargs; i++) {
2572 buf = synthesize_perf_probe_arg(&pev->args[i]);
2575 ret = strbuf_addf(result, " %s", buf);
2580 ret = strbuf_addch(result, ')');
2586 int show_perf_probe_event(const char *group, const char *event,
2587 struct perf_probe_event *pev,
2588 const char *module, bool use_stdout)
2590 struct strbuf buf = STRBUF_INIT;
2593 ret = perf_probe_event__sprintf(group, event, pev, module, &buf);
2596 printf("%s\n", buf.buf);
2598 pr_info("%s\n", buf.buf);
2600 strbuf_release(&buf);
2605 static bool filter_probe_trace_event(struct probe_trace_event *tev,
2606 struct strfilter *filter)
2610 /* At first, check the event name itself */
2611 if (strfilter__compare(filter, tev->event))
2614 /* Next, check the combination of name and group */
2615 if (e_snprintf(tmp, 128, "%s:%s", tev->group, tev->event) < 0)
2617 return strfilter__compare(filter, tmp);
2620 static int __show_perf_probe_events(int fd, bool is_kprobe,
2621 struct strfilter *filter)
2624 struct probe_trace_event tev;
2625 struct perf_probe_event pev;
2626 struct strlist *rawlist;
2627 struct str_node *ent;
2629 memset(&tev, 0, sizeof(tev));
2630 memset(&pev, 0, sizeof(pev));
2632 rawlist = probe_file__get_rawlist(fd);
2636 strlist__for_each_entry(ent, rawlist) {
2637 ret = parse_probe_trace_command(ent->s, &tev);
2639 if (!filter_probe_trace_event(&tev, filter))
2641 ret = convert_to_perf_probe_event(&tev, &pev,
2645 ret = show_perf_probe_event(pev.group, pev.event,
2646 &pev, tev.point.module,
2650 clear_perf_probe_event(&pev);
2651 clear_probe_trace_event(&tev);
2655 strlist__delete(rawlist);
2656 /* Cleanup cached debuginfo if needed */
2657 debuginfo_cache__exit();
2662 /* List up current perf-probe events */
2663 int show_perf_probe_events(struct strfilter *filter)
2665 int kp_fd, up_fd, ret;
2669 if (probe_conf.cache)
2670 return probe_cache__show_all_caches(filter);
2672 ret = init_probe_symbol_maps(false);
2676 ret = probe_file__open_both(&kp_fd, &up_fd, 0);
2681 ret = __show_perf_probe_events(kp_fd, true, filter);
2682 if (up_fd >= 0 && ret >= 0)
2683 ret = __show_perf_probe_events(up_fd, false, filter);
2688 exit_probe_symbol_maps();
2693 static int get_new_event_name(char *buf, size_t len, const char *base,
2694 struct strlist *namelist, bool ret_event,
2702 nbase = strdup(base);
2706 /* Cut off the dot suffixes (e.g. .const, .isra) and version suffixes */
2707 p = strpbrk(nbase, ".@");
2708 if (p && p != nbase)
2711 /* Try no suffix number */
2712 ret = e_snprintf(buf, len, "%s%s", nbase, ret_event ? "__return" : "");
2714 pr_debug("snprintf() failed: %d\n", ret);
2717 if (!strlist__has_entry(namelist, buf))
2720 if (!allow_suffix) {
2721 pr_warning("Error: event \"%s\" already exists.\n"
2722 " Hint: Remove existing event by 'perf probe -d'\n"
2723 " or force duplicates by 'perf probe -f'\n"
2724 " or set 'force=yes' in BPF source.\n",
2730 /* Try to add suffix */
2731 for (i = 1; i < MAX_EVENT_INDEX; i++) {
2732 ret = e_snprintf(buf, len, "%s_%d", nbase, i);
2734 pr_debug("snprintf() failed: %d\n", ret);
2737 if (!strlist__has_entry(namelist, buf))
2740 if (i == MAX_EVENT_INDEX) {
2741 pr_warning("Too many events are on the same function.\n");
2748 /* Final validation */
2749 if (ret >= 0 && !is_c_func_name(buf)) {
2750 pr_warning("Internal error: \"%s\" is an invalid event name.\n",
2758 /* Warn if the current kernel's uprobe implementation is old */
2759 static void warn_uprobe_event_compat(struct probe_trace_event *tev)
2762 char *buf = synthesize_probe_trace_command(tev);
2763 struct probe_trace_point *tp = &tev->point;
2765 if (tp->ref_ctr_offset && !uprobe_ref_ctr_is_supported()) {
2766 pr_warning("A semaphore is associated with %s:%s and "
2767 "seems your kernel doesn't support it.\n",
2768 tev->group, tev->event);
2771 /* Old uprobe event doesn't support memory dereference */
2772 if (!tev->uprobes || tev->nargs == 0 || !buf)
2775 for (i = 0; i < tev->nargs; i++)
2776 if (strglobmatch(tev->args[i].value, "[$@+-]*")) {
2777 pr_warning("Please upgrade your kernel to at least "
2778 "3.14 to have access to feature %s\n",
2779 tev->args[i].value);
2786 /* Set new name from original perf_probe_event and namelist */
2787 static int probe_trace_event__set_name(struct probe_trace_event *tev,
2788 struct perf_probe_event *pev,
2789 struct strlist *namelist,
2792 const char *event, *group;
2796 /* If probe_event or trace_event already have the name, reuse it */
2797 if (pev->event && !pev->sdt)
2799 else if (tev->event)
2802 /* Or generate new one from probe point */
2803 if (pev->point.function &&
2804 (strncmp(pev->point.function, "0x", 2) != 0) &&
2805 !strisglob(pev->point.function))
2806 event = pev->point.function;
2808 event = tev->point.realname;
2810 if (pev->group && !pev->sdt)
2812 else if (tev->group)
2815 group = PERFPROBE_GROUP;
2817 /* Get an unused new event name */
2818 ret = get_new_event_name(buf, 64, event, namelist,
2819 tev->point.retprobe, allow_suffix);
2825 tev->event = strdup(event);
2826 tev->group = strdup(group);
2827 if (tev->event == NULL || tev->group == NULL)
2831 * Add new event name to namelist if multiprobe event is NOT
2832 * supported, since we have to use new event name for following
2833 * probes in that case.
2835 if (!multiprobe_event_is_supported())
2836 strlist__add(namelist, event);
2840 static int __open_probe_file_and_namelist(bool uprobe,
2841 struct strlist **namelist)
2845 fd = probe_file__open(PF_FL_RW | (uprobe ? PF_FL_UPROBE : 0));
2849 /* Get current event names */
2850 *namelist = probe_file__get_namelist(fd);
2852 pr_debug("Failed to get current event list.\n");
2859 static int __add_probe_trace_events(struct perf_probe_event *pev,
2860 struct probe_trace_event *tevs,
2861 int ntevs, bool allow_suffix)
2863 int i, fd[2] = {-1, -1}, up, ret;
2864 struct probe_trace_event *tev = NULL;
2865 struct probe_cache *cache = NULL;
2866 struct strlist *namelist[2] = {NULL, NULL};
2867 struct nscookie nsc;
2869 up = pev->uprobes ? 1 : 0;
2870 fd[up] = __open_probe_file_and_namelist(up, &namelist[up]);
2875 for (i = 0; i < ntevs; i++) {
2877 up = tev->uprobes ? 1 : 0;
2878 if (fd[up] == -1) { /* Open the kprobe/uprobe_events */
2879 fd[up] = __open_probe_file_and_namelist(up,
2884 /* Skip if the symbol is out of .text or blacklisted */
2885 if (!tev->point.symbol && !pev->uprobes)
2888 /* Set new name for tev (and update namelist) */
2889 ret = probe_trace_event__set_name(tev, pev, namelist[up],
2894 nsinfo__mountns_enter(pev->nsi, &nsc);
2895 ret = probe_file__add_event(fd[up], tev);
2896 nsinfo__mountns_exit(&nsc);
2901 * Probes after the first probe which comes from same
2902 * user input are always allowed to add suffix, because
2903 * there might be several addresses corresponding to
2906 allow_suffix = true;
2908 if (ret == -EINVAL && pev->uprobes)
2909 warn_uprobe_event_compat(tev);
2910 if (ret == 0 && probe_conf.cache) {
2911 cache = probe_cache__new(pev->target, pev->nsi);
2913 probe_cache__add_entry(cache, pev, tevs, ntevs) < 0 ||
2914 probe_cache__commit(cache) < 0)
2915 pr_warning("Failed to add event to probe cache\n");
2916 probe_cache__delete(cache);
2920 for (up = 0; up < 2; up++) {
2921 strlist__delete(namelist[up]);
2928 static int find_probe_functions(struct map *map, char *name,
2929 struct symbol **syms)
2933 struct rb_node *tmp;
2934 const char *norm, *ver;
2936 bool cut_version = true;
2938 if (map__load(map) < 0)
2941 /* If user gives a version, don't cut off the version from symbols */
2942 if (strchr(name, '@'))
2943 cut_version = false;
2945 map__for_each_symbol(map, sym, tmp) {
2946 norm = arch__normalize_symbol_name(sym->name);
2951 /* We don't care about default symbol or not */
2952 ver = strchr(norm, '@');
2954 buf = strndup(norm, ver - norm);
2961 if (strglobmatch(norm, name)) {
2963 if (syms && found < probe_conf.max_probes)
2964 syms[found - 1] = sym;
2973 void __weak arch__fix_tev_from_maps(struct perf_probe_event *pev __maybe_unused,
2974 struct probe_trace_event *tev __maybe_unused,
2975 struct map *map __maybe_unused,
2976 struct symbol *sym __maybe_unused) { }
2979 * Find probe function addresses from map.
2980 * Return an error or the number of found probe_trace_event
2982 static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
2983 struct probe_trace_event **tevs)
2985 struct map *map = NULL;
2986 struct ref_reloc_sym *reloc_sym = NULL;
2988 struct symbol **syms = NULL;
2989 struct probe_trace_event *tev;
2990 struct perf_probe_point *pp = &pev->point;
2991 struct probe_trace_point *tp;
2992 int num_matched_functions;
2993 int ret, i, j, skipped = 0;
2996 map = get_target_map(pev->target, pev->nsi, pev->uprobes);
3002 syms = malloc(sizeof(struct symbol *) * probe_conf.max_probes);
3009 * Load matched symbols: Since the different local symbols may have
3010 * same name but different addresses, this lists all the symbols.
3012 num_matched_functions = find_probe_functions(map, pp->function, syms);
3013 if (num_matched_functions <= 0) {
3014 pr_err("Failed to find symbol %s in %s\n", pp->function,
3015 pev->target ? : "kernel");
3018 } else if (num_matched_functions > probe_conf.max_probes) {
3019 pr_err("Too many functions matched in %s\n",
3020 pev->target ? : "kernel");
3025 /* Note that the symbols in the kmodule are not relocated */
3026 if (!pev->uprobes && !pev->target &&
3027 (!pp->retprobe || kretprobe_offset_is_supported())) {
3028 reloc_sym = kernel_get_ref_reloc_sym(NULL);
3030 pr_warning("Relocated base symbol is not found!\n");
3036 /* Setup result trace-probe-events */
3037 *tevs = zalloc(sizeof(*tev) * num_matched_functions);
3045 for (j = 0; j < num_matched_functions; j++) {
3048 /* There can be duplicated symbols in the map */
3049 for (i = 0; i < j; i++)
3050 if (sym->start == syms[i]->start) {
3051 pr_debug("Found duplicated symbol %s @ %" PRIx64 "\n",
3052 sym->name, sym->start);
3058 tev = (*tevs) + ret;
3060 if (ret == num_matched_functions) {
3061 pr_warning("Too many symbols are listed. Skip it.\n");
3066 if (pp->offset > sym->end - sym->start) {
3067 pr_warning("Offset %ld is bigger than the size of %s\n",
3068 pp->offset, sym->name);
3072 /* Add one probe point */
3073 tp->address = map->unmap_ip(map, sym->start) + pp->offset;
3075 /* Check the kprobe (not in module) is within .text */
3076 if (!pev->uprobes && !pev->target &&
3077 kprobe_warn_out_range(sym->name, tp->address)) {
3078 tp->symbol = NULL; /* Skip it */
3080 } else if (reloc_sym) {
3081 tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
3082 tp->offset = tp->address - reloc_sym->addr;
3084 tp->symbol = strdup_or_goto(sym->name, nomem_out);
3085 tp->offset = pp->offset;
3087 tp->realname = strdup_or_goto(sym->name, nomem_out);
3089 tp->retprobe = pp->retprobe;
3092 tev->point.module = strdup_or_goto(pev->target,
3095 mod_name = find_module_name(pev->target);
3097 strdup(mod_name ? mod_name : pev->target);
3099 if (!tev->point.module)
3103 tev->uprobes = pev->uprobes;
3104 tev->nargs = pev->nargs;
3106 tev->args = zalloc(sizeof(struct probe_trace_arg) *
3108 if (tev->args == NULL)
3111 for (i = 0; i < tev->nargs; i++) {
3112 if (pev->args[i].name)
3114 strdup_or_goto(pev->args[i].name,
3117 tev->args[i].value = strdup_or_goto(pev->args[i].var,
3119 if (pev->args[i].type)
3121 strdup_or_goto(pev->args[i].type,
3124 arch__fix_tev_from_maps(pev, tev, map, sym);
3126 if (ret == skipped) {
3139 clear_probe_trace_events(*tevs, num_matched_functions);
3144 static int try_to_find_absolute_address(struct perf_probe_event *pev,
3145 struct probe_trace_event **tevs)
3147 struct perf_probe_point *pp = &pev->point;
3148 struct probe_trace_event *tev;
3149 struct probe_trace_point *tp;
3152 if (!(pev->point.function && !strncmp(pev->point.function, "0x", 2)))
3154 if (perf_probe_event_need_dwarf(pev))
3158 * This is 'perf probe /lib/libc.so 0xabcd'. Try to probe at
3161 * Only one tev can be generated by this.
3163 *tevs = zalloc(sizeof(*tev));
3171 * Don't use tp->offset, use address directly, because
3172 * in synthesize_probe_trace_command() address cannot be
3175 tp->address = pev->point.abs_address;
3176 tp->retprobe = pp->retprobe;
3177 tev->uprobes = pev->uprobes;
3181 * Give it a '0x' leading symbol name.
3182 * In __add_probe_trace_events, a NULL symbol is interpreted as
3185 if (asprintf(&tp->symbol, "0x%lx", tp->address) < 0)
3188 /* For kprobe, check range */
3189 if ((!tev->uprobes) &&
3190 (kprobe_warn_out_range(tev->point.symbol,
3191 tev->point.address))) {
3196 if (asprintf(&tp->realname, "abs_%lx", tp->address) < 0)
3200 tp->module = strdup(pev->target);
3206 tev->group = strdup(pev->group);
3212 tev->event = strdup(pev->event);
3217 tev->nargs = pev->nargs;
3218 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
3222 for (i = 0; i < tev->nargs; i++)
3223 copy_to_probe_trace_arg(&tev->args[i], &pev->args[i]);
3228 clear_probe_trace_events(*tevs, 1);
3233 /* Concatenate two arrays */
3234 static void *memcat(void *a, size_t sz_a, void *b, size_t sz_b)
3238 ret = malloc(sz_a + sz_b);
3240 memcpy(ret, a, sz_a);
3241 memcpy(ret + sz_a, b, sz_b);
3247 concat_probe_trace_events(struct probe_trace_event **tevs, int *ntevs,
3248 struct probe_trace_event **tevs2, int ntevs2)
3250 struct probe_trace_event *new_tevs;
3260 if (*ntevs + ntevs2 > probe_conf.max_probes)
3263 /* Concatenate the array of probe_trace_event */
3264 new_tevs = memcat(*tevs, (*ntevs) * sizeof(**tevs),
3265 *tevs2, ntevs2 * sizeof(**tevs2));
3275 clear_probe_trace_events(*tevs2, ntevs2);
3282 * Try to find probe_trace_event from given probe caches. Return the number
3283 * of cached events found, if an error occurs return the error.
3285 static int find_cached_events(struct perf_probe_event *pev,
3286 struct probe_trace_event **tevs,
3289 struct probe_cache *cache;
3290 struct probe_cache_entry *entry;
3291 struct probe_trace_event *tmp_tevs = NULL;
3295 cache = probe_cache__new(target, pev->nsi);
3296 /* Return 0 ("not found") if the target has no probe cache. */
3300 for_each_probe_cache_entry(entry, cache) {
3301 /* Skip the cache entry which has no name */
3302 if (!entry->pev.event || !entry->pev.group)
3304 if ((!pev->group || strglobmatch(entry->pev.group, pev->group)) &&
3305 strglobmatch(entry->pev.event, pev->event)) {
3306 ret = probe_cache_entry__get_event(entry, &tmp_tevs);
3308 ret = concat_probe_trace_events(tevs, &ntevs,
3314 probe_cache__delete(cache);
3316 clear_probe_trace_events(*tevs, ntevs);
3320 if (ntevs > 0 && target && target[0] == '/')
3321 pev->uprobes = true;
3327 /* Try to find probe_trace_event from all probe caches */
3328 static int find_cached_events_all(struct perf_probe_event *pev,
3329 struct probe_trace_event **tevs)
3331 struct probe_trace_event *tmp_tevs = NULL;
3332 struct strlist *bidlist;
3333 struct str_node *nd;
3338 /* Get the buildid list of all valid caches */
3339 bidlist = build_id_cache__list_all(true);
3342 pr_debug("Failed to get buildids: %d\n", ret);
3347 strlist__for_each_entry(nd, bidlist) {
3348 pathname = build_id_cache__origname(nd->s);
3349 ret = find_cached_events(pev, &tmp_tevs, pathname);
3350 /* In the case of cnt == 0, we just skip it */
3352 ret = concat_probe_trace_events(tevs, &ntevs,
3358 strlist__delete(bidlist);
3361 clear_probe_trace_events(*tevs, ntevs);
3369 static int find_probe_trace_events_from_cache(struct perf_probe_event *pev,
3370 struct probe_trace_event **tevs)
3372 struct probe_cache *cache;
3373 struct probe_cache_entry *entry;
3374 struct probe_trace_event *tev;
3375 struct str_node *node;
3379 /* For SDT/cached events, we use special search functions */
3381 return find_cached_events_all(pev, tevs);
3383 return find_cached_events(pev, tevs, pev->target);
3385 cache = probe_cache__new(pev->target, pev->nsi);
3389 entry = probe_cache__find(cache, pev);
3391 /* SDT must be in the cache */
3392 ret = pev->sdt ? -ENOENT : 0;
3396 ret = strlist__nr_entries(entry->tevlist);
3397 if (ret > probe_conf.max_probes) {
3398 pr_debug("Too many entries matched in the cache of %s\n",
3399 pev->target ? : "kernel");
3404 *tevs = zalloc(ret * sizeof(*tev));
3411 strlist__for_each_entry(node, entry->tevlist) {
3412 tev = &(*tevs)[i++];
3413 ret = parse_probe_trace_command(node->s, tev);
3416 /* Set the uprobes attribute as same as original */
3417 tev->uprobes = pev->uprobes;
3422 probe_cache__delete(cache);
3426 static int convert_to_probe_trace_events(struct perf_probe_event *pev,
3427 struct probe_trace_event **tevs)
3431 if (!pev->group && !pev->sdt) {
3432 /* Set group name if not given */
3433 if (!pev->uprobes) {
3434 pev->group = strdup(PERFPROBE_GROUP);
3435 ret = pev->group ? 0 : -ENOMEM;
3437 ret = convert_exec_to_group(pev->target, &pev->group);
3439 pr_warning("Failed to make a group name.\n");
3444 ret = try_to_find_absolute_address(pev, tevs);
3448 /* At first, we need to lookup cache entry */
3449 ret = find_probe_trace_events_from_cache(pev, tevs);
3450 if (ret > 0 || pev->sdt) /* SDT can be found only in the cache */
3451 return ret == 0 ? -ENOENT : ret; /* Found in probe cache */
3453 /* Convert perf_probe_event with debuginfo */
3454 ret = try_to_find_probe_trace_events(pev, tevs);
3456 return ret; /* Found in debuginfo or got an error */
3458 return find_probe_trace_events_from_map(pev, tevs);
3461 int convert_perf_probe_events(struct perf_probe_event *pevs, int npevs)
3465 /* Loop 1: convert all events */
3466 for (i = 0; i < npevs; i++) {
3467 /* Init kprobe blacklist if needed */
3468 if (!pevs[i].uprobes)
3469 kprobe_blacklist__init();
3470 /* Convert with or without debuginfo */
3471 ret = convert_to_probe_trace_events(&pevs[i], &pevs[i].tevs);
3474 pevs[i].ntevs = ret;
3476 /* This just release blacklist only if allocated */
3477 kprobe_blacklist__release();
3482 static int show_probe_trace_event(struct probe_trace_event *tev)
3484 char *buf = synthesize_probe_trace_command(tev);
3487 pr_debug("Failed to synthesize probe trace event.\n");
3491 /* Showing definition always go stdout */
3492 printf("%s\n", buf);
3498 int show_probe_trace_events(struct perf_probe_event *pevs, int npevs)
3500 struct strlist *namelist = strlist__new(NULL, NULL);
3501 struct probe_trace_event *tev;
3502 struct perf_probe_event *pev;
3508 for (j = 0; j < npevs && !ret; j++) {
3510 for (i = 0; i < pev->ntevs && !ret; i++) {
3511 tev = &pev->tevs[i];
3512 /* Skip if the symbol is out of .text or blacklisted */
3513 if (!tev->point.symbol && !pev->uprobes)
3516 /* Set new name for tev (and update namelist) */
3517 ret = probe_trace_event__set_name(tev, pev,
3520 ret = show_probe_trace_event(tev);
3523 strlist__delete(namelist);
3528 int apply_perf_probe_events(struct perf_probe_event *pevs, int npevs)
3532 /* Loop 2: add all events */
3533 for (i = 0; i < npevs; i++) {
3534 ret = __add_probe_trace_events(&pevs[i], pevs[i].tevs,
3536 probe_conf.force_add);
3543 void cleanup_perf_probe_events(struct perf_probe_event *pevs, int npevs)
3546 struct perf_probe_event *pev;
3548 /* Loop 3: cleanup and free trace events */
3549 for (i = 0; i < npevs; i++) {
3551 for (j = 0; j < pevs[i].ntevs; j++)
3552 clear_probe_trace_event(&pevs[i].tevs[j]);
3553 zfree(&pevs[i].tevs);
3555 nsinfo__zput(pev->nsi);
3556 clear_perf_probe_event(&pevs[i]);
3560 int add_perf_probe_events(struct perf_probe_event *pevs, int npevs)
3564 ret = init_probe_symbol_maps(pevs->uprobes);
3568 ret = convert_perf_probe_events(pevs, npevs);
3570 ret = apply_perf_probe_events(pevs, npevs);
3572 cleanup_perf_probe_events(pevs, npevs);
3574 exit_probe_symbol_maps();
3578 int del_perf_probe_events(struct strfilter *filter)
3580 int ret, ret2, ufd = -1, kfd = -1;
3581 char *str = strfilter__string(filter);
3586 /* Get current event names */
3587 ret = probe_file__open_both(&kfd, &ufd, PF_FL_RW);
3591 ret = probe_file__del_events(kfd, filter);
3592 if (ret < 0 && ret != -ENOENT)
3595 ret2 = probe_file__del_events(ufd, filter);
3596 if (ret2 < 0 && ret2 != -ENOENT) {
3613 int show_available_funcs(const char *target, struct nsinfo *nsi,
3614 struct strfilter *_filter, bool user)
3620 ret = init_probe_symbol_maps(user);
3624 /* Get a symbol map */
3625 map = get_target_map(target, nsi, user);
3627 pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
3631 ret = map__load(map);
3634 char *str = strfilter__string(_filter);
3635 pr_err("Failed to find symbols matched to \"%s\"\n",
3639 pr_err("Failed to load symbols in %s\n",
3640 (target) ? : "kernel");
3643 if (!dso__sorted_by_name(map->dso))
3644 dso__sort_by_name(map->dso);
3646 /* Show all (filtered) symbols */
3649 for (nd = rb_first_cached(&map->dso->symbol_names); nd;
3651 struct symbol_name_rb_node *pos = rb_entry(nd, struct symbol_name_rb_node, rb_node);
3653 if (strfilter__compare(_filter, pos->sym.name))
3654 printf("%s\n", pos->sym.name);
3658 exit_probe_symbol_maps();
3663 int copy_to_probe_trace_arg(struct probe_trace_arg *tvar,
3664 struct perf_probe_arg *pvar)
3666 tvar->value = strdup(pvar->var);
3667 if (tvar->value == NULL)
3670 tvar->type = strdup(pvar->type);
3671 if (tvar->type == NULL)
3675 tvar->name = strdup(pvar->name);
3676 if (tvar->name == NULL)