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 dinfo = open_debuginfo(pev->target, pev->nsi, !need_dwarf);
903 pr_debug("Could not open debuginfo. Try to use symbols.\n");
907 pr_debug("Try to find probe point from debuginfo.\n");
908 /* Searching trace events corresponding to a probe event */
909 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
911 if (ntevs == 0) { /* Not found, retry with an alternative */
912 ret = get_alternative_probe_event(dinfo, pev, &tmp);
914 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
916 * Write back to the original probe_event for
917 * setting appropriate (user given) event name
919 clear_perf_probe_point(&pev->point);
920 memcpy(&pev->point, &tmp, sizeof(tmp));
924 if (ntevs > 0) { /* Succeeded to find trace events */
925 pr_debug("Found %d probe_trace_events.\n", ntevs);
926 ret = post_process_probe_trace_events(pev, *tevs, ntevs,
927 pev->target, pev->uprobes, dinfo);
928 if (ret < 0 || ret == ntevs) {
929 pr_debug("Post processing failed or all events are skipped. (%d)\n", ret);
930 clear_probe_trace_events(*tevs, ntevs);
936 debuginfo__delete(dinfo);
938 if (ntevs == 0) { /* No error but failed to find probe point. */
939 pr_warning("Probe point '%s' not found.\n",
940 synthesize_perf_probe_point(&pev->point));
942 } else if (ntevs < 0) {
943 /* Error path : ntevs < 0 */
944 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
946 pr_warning("Warning: No dwarf info found in the vmlinux - "
947 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
949 pr_debug("Trying to use symbols.\n");
956 #define LINEBUF_SIZE 256
957 #define NR_ADDITIONAL_LINES 2
959 static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
961 char buf[LINEBUF_SIZE], sbuf[STRERR_BUFSIZE];
962 const char *color = show_num ? "" : PERF_COLOR_BLUE;
963 const char *prefix = NULL;
966 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
971 prefix = show_num ? "%7d " : " ";
972 color_fprintf(stdout, color, prefix, l);
974 color_fprintf(stdout, color, "%s", buf);
976 } while (strchr(buf, '\n') == NULL);
981 pr_warning("File read error: %s\n",
982 str_error_r(errno, sbuf, sizeof(sbuf)));
988 static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
990 int rv = __show_one_line(fp, l, skip, show_num);
992 pr_warning("Source file is shorter than expected.\n");
998 #define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
999 #define show_one_line(f,l) _show_one_line(f,l,false,false)
1000 #define skip_one_line(f,l) _show_one_line(f,l,true,false)
1001 #define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
1004 * Show line-range always requires debuginfo to find source file and
1007 static int __show_line_range(struct line_range *lr, const char *module,
1010 struct build_id bid;
1012 struct int_node *ln;
1013 struct debuginfo *dinfo;
1017 char sbuf[STRERR_BUFSIZE];
1018 char sbuild_id[SBUILD_ID_SIZE] = "";
1020 /* Search a line range */
1021 dinfo = open_debuginfo(module, NULL, false);
1025 ret = debuginfo__find_line_range(dinfo, lr);
1026 if (!ret) { /* Not found, retry with an alternative */
1027 ret = get_alternative_line_range(dinfo, lr, module, user);
1029 ret = debuginfo__find_line_range(dinfo, lr);
1031 if (dinfo->build_id) {
1032 build_id__init(&bid, dinfo->build_id, BUILD_ID_SIZE);
1033 build_id__sprintf(&bid, sbuild_id);
1035 debuginfo__delete(dinfo);
1036 if (ret == 0 || ret == -ENOENT) {
1037 pr_warning("Specified source line is not found.\n");
1039 } else if (ret < 0) {
1040 pr_warning("Debuginfo analysis failed.\n");
1044 /* Convert source file path */
1046 ret = find_source_path(tmp, sbuild_id, lr->comp_dir, &lr->path);
1048 /* Free old path when new path is assigned */
1049 if (tmp != lr->path)
1053 pr_warning("Failed to find source file path.\n");
1060 fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
1061 lr->start - lr->offset);
1063 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
1065 fp = fopen(lr->path, "r");
1067 pr_warning("Failed to open %s: %s\n", lr->path,
1068 str_error_r(errno, sbuf, sizeof(sbuf)));
1071 /* Skip to starting line number */
1072 while (l < lr->start) {
1073 ret = skip_one_line(fp, l++);
1078 intlist__for_each_entry(ln, lr->line_list) {
1079 for (; ln->i > l; l++) {
1080 ret = show_one_line(fp, l - lr->offset);
1084 ret = show_one_line_with_num(fp, l++ - lr->offset);
1089 if (lr->end == INT_MAX)
1090 lr->end = l + NR_ADDITIONAL_LINES;
1091 while (l <= lr->end) {
1092 ret = show_one_line_or_eof(fp, l++ - lr->offset);
1101 int show_line_range(struct line_range *lr, const char *module,
1102 struct nsinfo *nsi, bool user)
1105 struct nscookie nsc;
1107 ret = init_probe_symbol_maps(user);
1110 nsinfo__mountns_enter(nsi, &nsc);
1111 ret = __show_line_range(lr, module, user);
1112 nsinfo__mountns_exit(&nsc);
1113 exit_probe_symbol_maps();
1118 static int show_available_vars_at(struct debuginfo *dinfo,
1119 struct perf_probe_event *pev,
1120 struct strfilter *_filter)
1124 struct str_node *node;
1125 struct variable_list *vls = NULL, *vl;
1126 struct perf_probe_point tmp;
1129 buf = synthesize_perf_probe_point(&pev->point);
1132 pr_debug("Searching variables at %s\n", buf);
1134 ret = debuginfo__find_available_vars_at(dinfo, pev, &vls);
1135 if (!ret) { /* Not found, retry with an alternative */
1136 ret = get_alternative_probe_event(dinfo, pev, &tmp);
1138 ret = debuginfo__find_available_vars_at(dinfo, pev,
1140 /* Release the old probe_point */
1141 clear_perf_probe_point(&tmp);
1145 if (ret == 0 || ret == -ENOENT) {
1146 pr_err("Failed to find the address of %s\n", buf);
1149 pr_warning("Debuginfo analysis failed.\n");
1153 /* Some variables are found */
1154 fprintf(stdout, "Available variables at %s\n", buf);
1155 for (i = 0; i < ret; i++) {
1158 * A probe point might be converted to
1159 * several trace points.
1161 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
1163 zfree(&vl->point.symbol);
1166 strlist__for_each_entry(node, vl->vars) {
1167 var = strchr(node->s, '\t') + 1;
1168 if (strfilter__compare(_filter, var)) {
1169 fprintf(stdout, "\t\t%s\n", node->s);
1173 strlist__delete(vl->vars);
1176 fprintf(stdout, "\t\t(No matched variables)\n");
1184 /* Show available variables on given probe point */
1185 int show_available_vars(struct perf_probe_event *pevs, int npevs,
1186 struct strfilter *_filter)
1189 struct debuginfo *dinfo;
1191 ret = init_probe_symbol_maps(pevs->uprobes);
1195 dinfo = open_debuginfo(pevs->target, pevs->nsi, false);
1203 for (i = 0; i < npevs && ret >= 0; i++)
1204 ret = show_available_vars_at(dinfo, &pevs[i], _filter);
1206 debuginfo__delete(dinfo);
1208 exit_probe_symbol_maps();
1212 #else /* !HAVE_DWARF_SUPPORT */
1214 static void debuginfo_cache__exit(void)
1219 find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused,
1220 struct perf_probe_point *pp __maybe_unused,
1221 bool is_kprobe __maybe_unused)
1226 static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
1227 struct probe_trace_event **tevs __maybe_unused)
1229 if (perf_probe_event_need_dwarf(pev)) {
1230 pr_warning("Debuginfo-analysis is not supported.\n");
1237 int show_line_range(struct line_range *lr __maybe_unused,
1238 const char *module __maybe_unused,
1239 struct nsinfo *nsi __maybe_unused,
1240 bool user __maybe_unused)
1242 pr_warning("Debuginfo-analysis is not supported.\n");
1246 int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
1247 int npevs __maybe_unused,
1248 struct strfilter *filter __maybe_unused)
1250 pr_warning("Debuginfo-analysis is not supported.\n");
1255 void line_range__clear(struct line_range *lr)
1257 zfree(&lr->function);
1260 zfree(&lr->comp_dir);
1261 intlist__delete(lr->line_list);
1264 int line_range__init(struct line_range *lr)
1266 memset(lr, 0, sizeof(*lr));
1267 lr->line_list = intlist__new(NULL);
1274 static int parse_line_num(char **ptr, int *val, const char *what)
1276 const char *start = *ptr;
1279 *val = strtol(*ptr, ptr, 0);
1280 if (errno || *ptr == start) {
1281 semantic_error("'%s' is not a valid number.\n", what);
1287 /* Check the name is good for event, group or function */
1288 static bool is_c_func_name(const char *name)
1290 if (!isalpha(*name) && *name != '_')
1292 while (*++name != '\0') {
1293 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
1300 * Stuff 'lr' according to the line range described by 'arg'.
1301 * The line range syntax is described by:
1303 * SRC[:SLN[+NUM|-ELN]]
1304 * FNC[@SRC][:SLN[+NUM|-ELN]]
1306 int parse_line_range_desc(const char *arg, struct line_range *lr)
1308 char *range, *file, *name = strdup(arg);
1317 range = strchr(name, ':');
1321 err = parse_line_num(&range, &lr->start, "start line");
1325 if (*range == '+' || *range == '-') {
1326 const char c = *range++;
1328 err = parse_line_num(&range, &lr->end, "end line");
1333 lr->end += lr->start;
1335 * Adjust the number of lines here.
1336 * If the number of lines == 1, the
1337 * the end of line should be equal to
1338 * the start of line.
1344 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
1347 if (lr->start > lr->end) {
1348 semantic_error("Start line must be smaller"
1349 " than end line.\n");
1352 if (*range != '\0') {
1353 semantic_error("Tailing with invalid str '%s'.\n", range);
1358 file = strchr(name, '@');
1361 lr->file = strdup(++file);
1362 if (lr->file == NULL) {
1366 lr->function = name;
1367 } else if (strchr(name, '/') || strchr(name, '.'))
1369 else if (is_c_func_name(name))/* We reuse it for checking funcname */
1370 lr->function = name;
1371 else { /* Invalid name */
1372 semantic_error("'%s' is not a valid function name.\n", name);
1383 static int parse_perf_probe_event_name(char **arg, struct perf_probe_event *pev)
1387 ptr = strpbrk_esc(*arg, ":");
1390 if (!pev->sdt && !is_c_func_name(*arg))
1392 pev->group = strdup_esc(*arg);
1399 pev->event = strdup_esc(*arg);
1400 if (pev->event == NULL)
1403 if (!pev->sdt && !is_c_func_name(pev->event)) {
1407 semantic_error("%s is bad for event name -it must "
1408 "follow C symbol-naming rule.\n", *arg);
1414 /* Parse probepoint definition. */
1415 static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
1417 struct perf_probe_point *pp = &pev->point;
1420 bool file_spec = false;
1425 * perf probe [GRP:][EVENT=]SRC[:LN|;PTN]
1426 * perf probe [GRP:][EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
1427 * perf probe %[GRP:]SDT_EVENT
1432 if (is_sdt_event(arg)) {
1438 ptr = strpbrk_esc(arg, ";=@+%");
1442 semantic_error("%s must be an SDT name.\n",
1446 /* This must be a target file name or build id */
1447 tmp = build_id_cache__complement(ptr + 1);
1449 pev->target = build_id_cache__origname(tmp);
1452 pev->target = strdup_esc(ptr + 1);
1457 ret = parse_perf_probe_event_name(&arg, pev);
1459 if (asprintf(&pev->point.function, "%%%s", pev->event) < 0)
1465 if (ptr && *ptr == '=') { /* Event name */
1468 ret = parse_perf_probe_event_name(&arg, pev);
1476 * Check arg is function or file name and copy it.
1478 * We consider arg to be a file spec if and only if it satisfies
1479 * all of the below criteria::
1480 * - it does not include any of "+@%",
1481 * - it includes one of ":;", and
1482 * - it has a period '.' in the name.
1484 * Otherwise, we consider arg to be a function specification.
1486 if (!strpbrk_esc(arg, "+@%")) {
1487 ptr = strpbrk_esc(arg, ";:");
1488 /* This is a file spec if it includes a '.' before ; or : */
1489 if (ptr && memchr(arg, '.', ptr - arg))
1493 ptr = strpbrk_esc(arg, ";:+@%");
1502 tmp = strdup_esc(arg);
1513 * Keep pp->function even if this is absolute address,
1514 * so it can mark whether abs_address is valid.
1515 * Which make 'perf probe lib.bin 0x0' possible.
1517 * Note that checking length of tmp is not needed
1518 * because when we access tmp[1] we know tmp[0] is '0',
1519 * so tmp[1] should always valid (but could be '\0').
1521 if (tmp && !strncmp(tmp, "0x", 2)) {
1522 pp->abs_address = strtoul(pp->function, &tmp, 0);
1524 semantic_error("Invalid absolute address.\n");
1530 /* Parse other options */
1534 if (c == ';') { /* Lazy pattern must be the last part */
1535 pp->lazy_line = strdup(arg); /* let leave escapes */
1536 if (pp->lazy_line == NULL)
1540 ptr = strpbrk_esc(arg, ";:+@%");
1546 case ':': /* Line number */
1547 pp->line = strtoul(arg, &tmp, 0);
1549 semantic_error("There is non-digit char"
1550 " in line number.\n");
1554 case '+': /* Byte offset from a symbol */
1555 pp->offset = strtoul(arg, &tmp, 0);
1557 semantic_error("There is non-digit character"
1562 case '@': /* File name */
1564 semantic_error("SRC@SRC is not allowed.\n");
1567 pp->file = strdup_esc(arg);
1568 if (pp->file == NULL)
1571 case '%': /* Probe places */
1572 if (strcmp(arg, "return") == 0) {
1574 } else { /* Others not supported yet */
1575 semantic_error("%%%s is not supported.\n", arg);
1579 default: /* Buggy case */
1580 pr_err("This program has a bug at %s:%d.\n",
1581 __FILE__, __LINE__);
1587 /* Exclusion check */
1588 if (pp->lazy_line && pp->line) {
1589 semantic_error("Lazy pattern can't be used with"
1594 if (pp->lazy_line && pp->offset) {
1595 semantic_error("Lazy pattern can't be used with offset.\n");
1599 if (pp->line && pp->offset) {
1600 semantic_error("Offset can't be used with line number.\n");
1604 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
1605 semantic_error("File always requires line number or "
1610 if (pp->offset && !pp->function) {
1611 semantic_error("Offset requires an entry function.\n");
1615 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
1616 semantic_error("Offset/Line/Lazy pattern can't be used with "
1621 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
1622 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
1627 /* Parse perf-probe event argument */
1628 static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
1630 char *tmp, *goodname;
1631 struct perf_probe_arg_field **fieldp;
1633 pr_debug("parsing arg: %s into ", str);
1635 tmp = strchr(str, '=');
1637 arg->name = strndup(str, tmp - str);
1638 if (arg->name == NULL)
1640 pr_debug("name:%s ", arg->name);
1644 tmp = strchr(str, '@');
1645 if (tmp && tmp != str && !strcmp(tmp + 1, "user")) { /* user attr */
1646 if (!user_access_is_supported()) {
1647 semantic_error("ftrace does not support user access\n");
1651 arg->user_access = true;
1652 pr_debug("user_access ");
1655 tmp = strchr(str, ':');
1656 if (tmp) { /* Type setting */
1658 arg->type = strdup(tmp + 1);
1659 if (arg->type == NULL)
1661 pr_debug("type:%s ", arg->type);
1664 tmp = strpbrk(str, "-.[");
1665 if (!is_c_varname(str) || !tmp) {
1666 /* A variable, register, symbol or special value */
1667 arg->var = strdup(str);
1668 if (arg->var == NULL)
1670 pr_debug("%s\n", arg->var);
1674 /* Structure fields or array element */
1675 arg->var = strndup(str, tmp - str);
1676 if (arg->var == NULL)
1678 goodname = arg->var;
1679 pr_debug("%s, ", arg->var);
1680 fieldp = &arg->field;
1683 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
1684 if (*fieldp == NULL)
1686 if (*tmp == '[') { /* Array */
1688 (*fieldp)->index = strtol(str + 1, &tmp, 0);
1689 (*fieldp)->ref = true;
1690 if (*tmp != ']' || tmp == str + 1) {
1691 semantic_error("Array index must be a"
1698 } else { /* Structure */
1701 (*fieldp)->ref = false;
1702 } else if (tmp[1] == '>') {
1704 (*fieldp)->ref = true;
1706 semantic_error("Argument parse error: %s\n",
1710 tmp = strpbrk(str, "-.[");
1713 (*fieldp)->name = strndup(str, tmp - str);
1714 if ((*fieldp)->name == NULL)
1717 goodname = (*fieldp)->name;
1718 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
1719 fieldp = &(*fieldp)->next;
1722 (*fieldp)->name = strdup(str);
1723 if ((*fieldp)->name == NULL)
1726 goodname = (*fieldp)->name;
1727 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
1729 /* If no name is specified, set the last field name (not array index)*/
1731 arg->name = strdup(goodname);
1732 if (arg->name == NULL)
1738 /* Parse perf-probe event command */
1739 int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
1742 int argc, i, ret = 0;
1744 argv = argv_split(cmd, &argc);
1746 pr_debug("Failed to split arguments.\n");
1749 if (argc - 1 > MAX_PROBE_ARGS) {
1750 semantic_error("Too many probe arguments (%d).\n", argc - 1);
1754 /* Parse probe point */
1755 ret = parse_perf_probe_point(argv[0], pev);
1759 /* Generate event name if needed */
1760 if (!pev->event && pev->point.function && pev->point.line
1761 && !pev->point.lazy_line && !pev->point.offset) {
1762 if (asprintf(&pev->event, "%s_L%d", pev->point.function,
1763 pev->point.line) < 0) {
1769 /* Copy arguments and ensure return probe has no C argument */
1770 pev->nargs = argc - 1;
1771 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1772 if (pev->args == NULL) {
1776 for (i = 0; i < pev->nargs && ret >= 0; i++) {
1777 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1779 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
1780 semantic_error("You can't specify local variable for"
1791 /* Returns true if *any* ARG is either C variable, $params or $vars. */
1792 bool perf_probe_with_var(struct perf_probe_event *pev)
1796 for (i = 0; i < pev->nargs; i++)
1797 if (is_c_varname(pev->args[i].var) ||
1798 !strcmp(pev->args[i].var, PROBE_ARG_PARAMS) ||
1799 !strcmp(pev->args[i].var, PROBE_ARG_VARS))
1804 /* Return true if this perf_probe_event requires debuginfo */
1805 bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
1807 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1810 if (perf_probe_with_var(pev))
1816 /* Parse probe_events event into struct probe_point */
1817 int parse_probe_trace_command(const char *cmd, struct probe_trace_event *tev)
1819 struct probe_trace_point *tp = &tev->point;
1822 char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
1826 pr_debug("Parsing probe_events: %s\n", cmd);
1827 argv = argv_split(cmd, &argc);
1829 pr_debug("Failed to split arguments.\n");
1833 semantic_error("Too few probe arguments.\n");
1838 /* Scan event and group name. */
1839 argv0_str = strdup(argv[0]);
1840 if (argv0_str == NULL) {
1844 fmt1_str = strtok_r(argv0_str, ":", &fmt);
1845 fmt2_str = strtok_r(NULL, "/", &fmt);
1846 fmt3_str = strtok_r(NULL, " \t", &fmt);
1847 if (fmt1_str == NULL || fmt2_str == NULL || fmt3_str == NULL) {
1848 semantic_error("Failed to parse event name: %s\n", argv[0]);
1853 tev->group = strdup(fmt2_str);
1854 tev->event = strdup(fmt3_str);
1855 if (tev->group == NULL || tev->event == NULL) {
1859 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
1861 tp->retprobe = (pr == 'r');
1863 /* Scan module name(if there), function name and offset */
1864 p = strchr(argv[1], ':');
1866 tp->module = strndup(argv[1], p - argv[1]);
1871 tev->uprobes = (tp->module[0] == '/');
1875 fmt1_str = strtok_r(p, "+", &fmt);
1876 /* only the address started with 0x */
1877 if (fmt1_str[0] == '0') {
1879 * Fix a special case:
1880 * if address == 0, kernel reports something like:
1881 * p:probe_libc/abs_0 /lib/libc-2.18.so:0x (null) arg1=%ax
1882 * Newer kernel may fix that, but we want to
1883 * support old kernel also.
1885 if (strcmp(fmt1_str, "0x") == 0) {
1886 if (!argv[2] || strcmp(argv[2], "(null)")) {
1893 for (i = 2; argv[i + 1] != NULL; i++)
1894 argv[i] = argv[i + 1];
1899 tp->address = strtoul(fmt1_str, NULL, 0);
1901 /* Only the symbol-based probe has offset */
1902 tp->symbol = strdup(fmt1_str);
1903 if (tp->symbol == NULL) {
1907 fmt2_str = strtok_r(NULL, "", &fmt);
1908 if (fmt2_str == NULL)
1911 tp->offset = strtoul(fmt2_str, NULL, 10);
1915 fmt2_str = strchr(p, '(');
1917 tp->ref_ctr_offset = strtoul(fmt2_str + 1, NULL, 0);
1920 tev->nargs = argc - 2;
1921 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
1922 if (tev->args == NULL) {
1926 for (i = 0; i < tev->nargs; i++) {
1927 p = strchr(argv[i + 2], '=');
1928 if (p) /* We don't need which register is assigned. */
1932 tev->args[i].name = strdup(argv[i + 2]);
1933 /* TODO: parse regs and offset */
1934 tev->args[i].value = strdup(p);
1935 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1947 /* Compose only probe arg */
1948 char *synthesize_perf_probe_arg(struct perf_probe_arg *pa)
1950 struct perf_probe_arg_field *field = pa->field;
1955 if (strbuf_init(&buf, 64) < 0)
1958 if (pa->name && pa->var)
1959 err = strbuf_addf(&buf, "%s=%s", pa->name, pa->var);
1961 err = strbuf_addstr(&buf, pa->name ?: pa->var);
1966 if (field->name[0] == '[')
1967 err = strbuf_addstr(&buf, field->name);
1969 err = strbuf_addf(&buf, "%s%s", field->ref ? "->" : ".",
1971 field = field->next;
1977 if (strbuf_addf(&buf, ":%s", pa->type) < 0)
1980 ret = strbuf_detach(&buf, NULL);
1982 strbuf_release(&buf);
1986 /* Compose only probe point (not argument) */
1987 char *synthesize_perf_probe_point(struct perf_probe_point *pp)
1990 char *tmp, *ret = NULL;
1993 if (strbuf_init(&buf, 64) < 0)
1997 if (strbuf_addstr(&buf, pp->function) < 0)
2000 err = strbuf_addf(&buf, "+%lu", pp->offset);
2002 err = strbuf_addf(&buf, ":%d", pp->line);
2003 else if (pp->retprobe)
2004 err = strbuf_addstr(&buf, "%return");
2012 tmp = strchr(pp->file + len - 30, '/');
2013 tmp = tmp ? tmp + 1 : pp->file + len - 30;
2015 err = strbuf_addf(&buf, "@%s", tmp);
2016 if (!err && !pp->function && pp->line)
2017 err = strbuf_addf(&buf, ":%d", pp->line);
2020 ret = strbuf_detach(&buf, NULL);
2022 strbuf_release(&buf);
2026 char *synthesize_perf_probe_command(struct perf_probe_event *pev)
2029 char *tmp, *ret = NULL;
2032 if (strbuf_init(&buf, 64))
2035 if (strbuf_addf(&buf, "%s:%s=", pev->group ?: PERFPROBE_GROUP,
2039 tmp = synthesize_perf_probe_point(&pev->point);
2040 if (!tmp || strbuf_addstr(&buf, tmp) < 0)
2044 for (i = 0; i < pev->nargs; i++) {
2045 tmp = synthesize_perf_probe_arg(pev->args + i);
2046 if (!tmp || strbuf_addf(&buf, " %s", tmp) < 0)
2051 ret = strbuf_detach(&buf, NULL);
2053 strbuf_release(&buf);
2057 static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
2058 struct strbuf *buf, int depth)
2062 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
2067 if (ref->user_access)
2068 err = strbuf_addf(buf, "%s%ld(", "+u", ref->offset);
2070 err = strbuf_addf(buf, "%+ld(", ref->offset);
2071 return (err < 0) ? err : depth;
2074 static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
2077 struct probe_trace_arg_ref *ref = arg->ref;
2080 /* Argument name or separator */
2082 err = strbuf_addf(buf, " %s=", arg->name);
2084 err = strbuf_addch(buf, ' ');
2088 /* Special case: @XXX */
2089 if (arg->value[0] == '@' && arg->ref)
2092 /* Dereferencing arguments */
2094 depth = __synthesize_probe_trace_arg_ref(ref, buf, 1);
2099 /* Print argument value */
2100 if (arg->value[0] == '@' && arg->ref)
2101 err = strbuf_addf(buf, "%s%+ld", arg->value, arg->ref->offset);
2103 err = strbuf_addstr(buf, arg->value);
2106 while (!err && depth--)
2107 err = strbuf_addch(buf, ')');
2109 /* Print argument type */
2110 if (!err && arg->type)
2111 err = strbuf_addf(buf, ":%s", arg->type);
2117 synthesize_uprobe_trace_def(struct probe_trace_event *tev, struct strbuf *buf)
2119 struct probe_trace_point *tp = &tev->point;
2122 err = strbuf_addf(buf, "%s:0x%lx", tp->module, tp->address);
2124 if (err >= 0 && tp->ref_ctr_offset) {
2125 if (!uprobe_ref_ctr_is_supported())
2127 err = strbuf_addf(buf, "(0x%lx)", tp->ref_ctr_offset);
2129 return err >= 0 ? 0 : -1;
2132 char *synthesize_probe_trace_command(struct probe_trace_event *tev)
2134 struct probe_trace_point *tp = &tev->point;
2139 /* Uprobes must have tp->module */
2140 if (tev->uprobes && !tp->module)
2143 if (strbuf_init(&buf, 32) < 0)
2146 if (strbuf_addf(&buf, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
2147 tev->group, tev->event) < 0)
2150 * If tp->address == 0, then this point must be a
2151 * absolute address uprobe.
2152 * try_to_find_absolute_address() should have made
2153 * tp->symbol to "0x0".
2155 if (tev->uprobes && !tp->address) {
2156 if (!tp->symbol || strcmp(tp->symbol, "0x0"))
2160 /* Use the tp->address for uprobes */
2162 err = synthesize_uprobe_trace_def(tev, &buf);
2163 } else if (!strncmp(tp->symbol, "0x", 2)) {
2164 /* Absolute address. See try_to_find_absolute_address() */
2165 err = strbuf_addf(&buf, "%s%s0x%lx", tp->module ?: "",
2166 tp->module ? ":" : "", tp->address);
2168 err = strbuf_addf(&buf, "%s%s%s+%lu", tp->module ?: "",
2169 tp->module ? ":" : "", tp->symbol, tp->offset);
2175 for (i = 0; i < tev->nargs; i++)
2176 if (synthesize_probe_trace_arg(&tev->args[i], &buf) < 0)
2179 ret = strbuf_detach(&buf, NULL);
2181 strbuf_release(&buf);
2185 static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
2186 struct perf_probe_point *pp,
2189 struct symbol *sym = NULL;
2190 struct map *map = NULL;
2191 u64 addr = tp->address;
2195 map = dso__new_map(tp->module);
2198 sym = map__find_symbol(map, addr);
2200 if (tp->symbol && !addr) {
2201 if (kernel_get_symbol_address_by_name(tp->symbol,
2202 &addr, true, false) < 0)
2207 sym = machine__find_kernel_symbol(host_machine, addr, &map);
2214 pp->retprobe = tp->retprobe;
2215 pp->offset = addr - map->unmap_ip(map, sym->start);
2216 pp->function = strdup(sym->name);
2217 ret = pp->function ? 0 : -ENOMEM;
2220 if (map && !is_kprobe) {
2227 static int convert_to_perf_probe_point(struct probe_trace_point *tp,
2228 struct perf_probe_point *pp,
2234 ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe);
2237 ret = find_perf_probe_point_from_map(tp, pp, is_kprobe);
2241 pr_debug("Failed to find probe point from both of dwarf and map.\n");
2244 pp->function = strdup(tp->symbol);
2245 pp->offset = tp->offset;
2247 ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address);
2250 pp->function = strdup(buf);
2253 if (pp->function == NULL)
2256 pp->retprobe = tp->retprobe;
2261 static int convert_to_perf_probe_event(struct probe_trace_event *tev,
2262 struct perf_probe_event *pev, bool is_kprobe)
2264 struct strbuf buf = STRBUF_INIT;
2267 /* Convert event/group name */
2268 pev->event = strdup(tev->event);
2269 pev->group = strdup(tev->group);
2270 if (pev->event == NULL || pev->group == NULL)
2273 /* Convert trace_point to probe_point */
2274 ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
2278 /* Convert trace_arg to probe_arg */
2279 pev->nargs = tev->nargs;
2280 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
2281 if (pev->args == NULL)
2283 for (i = 0; i < tev->nargs && ret >= 0; i++) {
2284 if (tev->args[i].name)
2285 pev->args[i].name = strdup(tev->args[i].name);
2287 if ((ret = strbuf_init(&buf, 32)) < 0)
2289 ret = synthesize_probe_trace_arg(&tev->args[i], &buf);
2290 pev->args[i].name = strbuf_detach(&buf, NULL);
2292 if (pev->args[i].name == NULL && ret >= 0)
2297 clear_perf_probe_event(pev);
2302 void clear_perf_probe_event(struct perf_probe_event *pev)
2304 struct perf_probe_arg_field *field, *next;
2309 zfree(&pev->target);
2310 clear_perf_probe_point(&pev->point);
2312 for (i = 0; i < pev->nargs; i++) {
2313 zfree(&pev->args[i].name);
2314 zfree(&pev->args[i].var);
2315 zfree(&pev->args[i].type);
2316 field = pev->args[i].field;
2319 zfree(&field->name);
2328 #define strdup_or_goto(str, label) \
2329 ({ char *__p = NULL; if (str && !(__p = strdup(str))) goto label; __p; })
2331 static int perf_probe_point__copy(struct perf_probe_point *dst,
2332 struct perf_probe_point *src)
2334 dst->file = strdup_or_goto(src->file, out_err);
2335 dst->function = strdup_or_goto(src->function, out_err);
2336 dst->lazy_line = strdup_or_goto(src->lazy_line, out_err);
2337 dst->line = src->line;
2338 dst->retprobe = src->retprobe;
2339 dst->offset = src->offset;
2343 clear_perf_probe_point(dst);
2347 static int perf_probe_arg__copy(struct perf_probe_arg *dst,
2348 struct perf_probe_arg *src)
2350 struct perf_probe_arg_field *field, **ppfield;
2352 dst->name = strdup_or_goto(src->name, out_err);
2353 dst->var = strdup_or_goto(src->var, out_err);
2354 dst->type = strdup_or_goto(src->type, out_err);
2357 ppfield = &(dst->field);
2359 *ppfield = zalloc(sizeof(*field));
2362 (*ppfield)->name = strdup_or_goto(field->name, out_err);
2363 (*ppfield)->index = field->index;
2364 (*ppfield)->ref = field->ref;
2365 field = field->next;
2366 ppfield = &((*ppfield)->next);
2373 int perf_probe_event__copy(struct perf_probe_event *dst,
2374 struct perf_probe_event *src)
2378 dst->event = strdup_or_goto(src->event, out_err);
2379 dst->group = strdup_or_goto(src->group, out_err);
2380 dst->target = strdup_or_goto(src->target, out_err);
2381 dst->uprobes = src->uprobes;
2383 if (perf_probe_point__copy(&dst->point, &src->point) < 0)
2386 dst->args = zalloc(sizeof(struct perf_probe_arg) * src->nargs);
2389 dst->nargs = src->nargs;
2391 for (i = 0; i < src->nargs; i++)
2392 if (perf_probe_arg__copy(&dst->args[i], &src->args[i]) < 0)
2397 clear_perf_probe_event(dst);
2401 void clear_probe_trace_event(struct probe_trace_event *tev)
2403 struct probe_trace_arg_ref *ref, *next;
2408 zfree(&tev->point.symbol);
2409 zfree(&tev->point.realname);
2410 zfree(&tev->point.module);
2411 for (i = 0; i < tev->nargs; i++) {
2412 zfree(&tev->args[i].name);
2413 zfree(&tev->args[i].value);
2414 zfree(&tev->args[i].type);
2415 ref = tev->args[i].ref;
2426 struct kprobe_blacklist_node {
2427 struct list_head list;
2428 unsigned long start;
2433 static void kprobe_blacklist__delete(struct list_head *blacklist)
2435 struct kprobe_blacklist_node *node;
2437 while (!list_empty(blacklist)) {
2438 node = list_first_entry(blacklist,
2439 struct kprobe_blacklist_node, list);
2440 list_del_init(&node->list);
2441 zfree(&node->symbol);
2446 static int kprobe_blacklist__load(struct list_head *blacklist)
2448 struct kprobe_blacklist_node *node;
2449 const char *__debugfs = debugfs__mountpoint();
2450 char buf[PATH_MAX], *p;
2454 if (__debugfs == NULL)
2457 ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs);
2461 fp = fopen(buf, "r");
2466 while (fgets(buf, PATH_MAX, fp)) {
2467 node = zalloc(sizeof(*node));
2472 INIT_LIST_HEAD(&node->list);
2473 list_add_tail(&node->list, blacklist);
2474 if (sscanf(buf, "0x%lx-0x%lx", &node->start, &node->end) != 2) {
2478 p = strchr(buf, '\t');
2481 if (p[strlen(p) - 1] == '\n')
2482 p[strlen(p) - 1] = '\0';
2484 p = (char *)"unknown";
2485 node->symbol = strdup(p);
2486 if (!node->symbol) {
2490 pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
2491 node->start, node->end, node->symbol);
2495 kprobe_blacklist__delete(blacklist);
2501 static struct kprobe_blacklist_node *
2502 kprobe_blacklist__find_by_address(struct list_head *blacklist,
2503 unsigned long address)
2505 struct kprobe_blacklist_node *node;
2507 list_for_each_entry(node, blacklist, list) {
2508 if (node->start <= address && address < node->end)
2515 static LIST_HEAD(kprobe_blacklist);
2517 static void kprobe_blacklist__init(void)
2519 if (!list_empty(&kprobe_blacklist))
2522 if (kprobe_blacklist__load(&kprobe_blacklist) < 0)
2523 pr_debug("No kprobe blacklist support, ignored\n");
2526 static void kprobe_blacklist__release(void)
2528 kprobe_blacklist__delete(&kprobe_blacklist);
2531 static bool kprobe_blacklist__listed(unsigned long address)
2533 return !!kprobe_blacklist__find_by_address(&kprobe_blacklist, address);
2536 static int perf_probe_event__sprintf(const char *group, const char *event,
2537 struct perf_probe_event *pev,
2539 struct strbuf *result)
2544 if (asprintf(&buf, "%s:%s", group, event) < 0)
2546 ret = strbuf_addf(result, " %-20s (on ", buf);
2551 /* Synthesize only event probe point */
2552 buf = synthesize_perf_probe_point(&pev->point);
2555 ret = strbuf_addstr(result, buf);
2559 ret = strbuf_addf(result, " in %s", module);
2561 if (!ret && pev->nargs > 0) {
2562 ret = strbuf_add(result, " with", 5);
2563 for (i = 0; !ret && i < pev->nargs; i++) {
2564 buf = synthesize_perf_probe_arg(&pev->args[i]);
2567 ret = strbuf_addf(result, " %s", buf);
2572 ret = strbuf_addch(result, ')');
2578 int show_perf_probe_event(const char *group, const char *event,
2579 struct perf_probe_event *pev,
2580 const char *module, bool use_stdout)
2582 struct strbuf buf = STRBUF_INIT;
2585 ret = perf_probe_event__sprintf(group, event, pev, module, &buf);
2588 printf("%s\n", buf.buf);
2590 pr_info("%s\n", buf.buf);
2592 strbuf_release(&buf);
2597 static bool filter_probe_trace_event(struct probe_trace_event *tev,
2598 struct strfilter *filter)
2602 /* At first, check the event name itself */
2603 if (strfilter__compare(filter, tev->event))
2606 /* Next, check the combination of name and group */
2607 if (e_snprintf(tmp, 128, "%s:%s", tev->group, tev->event) < 0)
2609 return strfilter__compare(filter, tmp);
2612 static int __show_perf_probe_events(int fd, bool is_kprobe,
2613 struct strfilter *filter)
2616 struct probe_trace_event tev;
2617 struct perf_probe_event pev;
2618 struct strlist *rawlist;
2619 struct str_node *ent;
2621 memset(&tev, 0, sizeof(tev));
2622 memset(&pev, 0, sizeof(pev));
2624 rawlist = probe_file__get_rawlist(fd);
2628 strlist__for_each_entry(ent, rawlist) {
2629 ret = parse_probe_trace_command(ent->s, &tev);
2631 if (!filter_probe_trace_event(&tev, filter))
2633 ret = convert_to_perf_probe_event(&tev, &pev,
2637 ret = show_perf_probe_event(pev.group, pev.event,
2638 &pev, tev.point.module,
2642 clear_perf_probe_event(&pev);
2643 clear_probe_trace_event(&tev);
2647 strlist__delete(rawlist);
2648 /* Cleanup cached debuginfo if needed */
2649 debuginfo_cache__exit();
2654 /* List up current perf-probe events */
2655 int show_perf_probe_events(struct strfilter *filter)
2657 int kp_fd, up_fd, ret;
2661 if (probe_conf.cache)
2662 return probe_cache__show_all_caches(filter);
2664 ret = init_probe_symbol_maps(false);
2668 ret = probe_file__open_both(&kp_fd, &up_fd, 0);
2673 ret = __show_perf_probe_events(kp_fd, true, filter);
2674 if (up_fd >= 0 && ret >= 0)
2675 ret = __show_perf_probe_events(up_fd, false, filter);
2680 exit_probe_symbol_maps();
2685 static int get_new_event_name(char *buf, size_t len, const char *base,
2686 struct strlist *namelist, bool ret_event,
2694 nbase = strdup(base);
2698 /* Cut off the dot suffixes (e.g. .const, .isra) and version suffixes */
2699 p = strpbrk(nbase, ".@");
2700 if (p && p != nbase)
2703 /* Try no suffix number */
2704 ret = e_snprintf(buf, len, "%s%s", nbase, ret_event ? "__return" : "");
2706 pr_debug("snprintf() failed: %d\n", ret);
2709 if (!strlist__has_entry(namelist, buf))
2712 if (!allow_suffix) {
2713 pr_warning("Error: event \"%s\" already exists.\n"
2714 " Hint: Remove existing event by 'perf probe -d'\n"
2715 " or force duplicates by 'perf probe -f'\n"
2716 " or set 'force=yes' in BPF source.\n",
2722 /* Try to add suffix */
2723 for (i = 1; i < MAX_EVENT_INDEX; i++) {
2724 ret = e_snprintf(buf, len, "%s_%d", nbase, i);
2726 pr_debug("snprintf() failed: %d\n", ret);
2729 if (!strlist__has_entry(namelist, buf))
2732 if (i == MAX_EVENT_INDEX) {
2733 pr_warning("Too many events are on the same function.\n");
2740 /* Final validation */
2741 if (ret >= 0 && !is_c_func_name(buf)) {
2742 pr_warning("Internal error: \"%s\" is an invalid event name.\n",
2750 /* Warn if the current kernel's uprobe implementation is old */
2751 static void warn_uprobe_event_compat(struct probe_trace_event *tev)
2754 char *buf = synthesize_probe_trace_command(tev);
2755 struct probe_trace_point *tp = &tev->point;
2757 if (tp->ref_ctr_offset && !uprobe_ref_ctr_is_supported()) {
2758 pr_warning("A semaphore is associated with %s:%s and "
2759 "seems your kernel doesn't support it.\n",
2760 tev->group, tev->event);
2763 /* Old uprobe event doesn't support memory dereference */
2764 if (!tev->uprobes || tev->nargs == 0 || !buf)
2767 for (i = 0; i < tev->nargs; i++)
2768 if (strglobmatch(tev->args[i].value, "[$@+-]*")) {
2769 pr_warning("Please upgrade your kernel to at least "
2770 "3.14 to have access to feature %s\n",
2771 tev->args[i].value);
2778 /* Set new name from original perf_probe_event and namelist */
2779 static int probe_trace_event__set_name(struct probe_trace_event *tev,
2780 struct perf_probe_event *pev,
2781 struct strlist *namelist,
2784 const char *event, *group;
2788 /* If probe_event or trace_event already have the name, reuse it */
2789 if (pev->event && !pev->sdt)
2791 else if (tev->event)
2794 /* Or generate new one from probe point */
2795 if (pev->point.function &&
2796 (strncmp(pev->point.function, "0x", 2) != 0) &&
2797 !strisglob(pev->point.function))
2798 event = pev->point.function;
2800 event = tev->point.realname;
2802 if (pev->group && !pev->sdt)
2804 else if (tev->group)
2807 group = PERFPROBE_GROUP;
2809 /* Get an unused new event name */
2810 ret = get_new_event_name(buf, 64, event, namelist,
2811 tev->point.retprobe, allow_suffix);
2817 tev->event = strdup(event);
2818 tev->group = strdup(group);
2819 if (tev->event == NULL || tev->group == NULL)
2823 * Add new event name to namelist if multiprobe event is NOT
2824 * supported, since we have to use new event name for following
2825 * probes in that case.
2827 if (!multiprobe_event_is_supported())
2828 strlist__add(namelist, event);
2832 static int __open_probe_file_and_namelist(bool uprobe,
2833 struct strlist **namelist)
2837 fd = probe_file__open(PF_FL_RW | (uprobe ? PF_FL_UPROBE : 0));
2841 /* Get current event names */
2842 *namelist = probe_file__get_namelist(fd);
2844 pr_debug("Failed to get current event list.\n");
2851 static int __add_probe_trace_events(struct perf_probe_event *pev,
2852 struct probe_trace_event *tevs,
2853 int ntevs, bool allow_suffix)
2855 int i, fd[2] = {-1, -1}, up, ret;
2856 struct probe_trace_event *tev = NULL;
2857 struct probe_cache *cache = NULL;
2858 struct strlist *namelist[2] = {NULL, NULL};
2859 struct nscookie nsc;
2861 up = pev->uprobes ? 1 : 0;
2862 fd[up] = __open_probe_file_and_namelist(up, &namelist[up]);
2867 for (i = 0; i < ntevs; i++) {
2869 up = tev->uprobes ? 1 : 0;
2870 if (fd[up] == -1) { /* Open the kprobe/uprobe_events */
2871 fd[up] = __open_probe_file_and_namelist(up,
2876 /* Skip if the symbol is out of .text or blacklisted */
2877 if (!tev->point.symbol && !pev->uprobes)
2880 /* Set new name for tev (and update namelist) */
2881 ret = probe_trace_event__set_name(tev, pev, namelist[up],
2886 nsinfo__mountns_enter(pev->nsi, &nsc);
2887 ret = probe_file__add_event(fd[up], tev);
2888 nsinfo__mountns_exit(&nsc);
2893 * Probes after the first probe which comes from same
2894 * user input are always allowed to add suffix, because
2895 * there might be several addresses corresponding to
2898 allow_suffix = true;
2900 if (ret == -EINVAL && pev->uprobes)
2901 warn_uprobe_event_compat(tev);
2902 if (ret == 0 && probe_conf.cache) {
2903 cache = probe_cache__new(pev->target, pev->nsi);
2905 probe_cache__add_entry(cache, pev, tevs, ntevs) < 0 ||
2906 probe_cache__commit(cache) < 0)
2907 pr_warning("Failed to add event to probe cache\n");
2908 probe_cache__delete(cache);
2912 for (up = 0; up < 2; up++) {
2913 strlist__delete(namelist[up]);
2920 static int find_probe_functions(struct map *map, char *name,
2921 struct symbol **syms)
2925 struct rb_node *tmp;
2926 const char *norm, *ver;
2928 bool cut_version = true;
2930 if (map__load(map) < 0)
2933 /* If user gives a version, don't cut off the version from symbols */
2934 if (strchr(name, '@'))
2935 cut_version = false;
2937 map__for_each_symbol(map, sym, tmp) {
2938 norm = arch__normalize_symbol_name(sym->name);
2943 /* We don't care about default symbol or not */
2944 ver = strchr(norm, '@');
2946 buf = strndup(norm, ver - norm);
2953 if (strglobmatch(norm, name)) {
2955 if (syms && found < probe_conf.max_probes)
2956 syms[found - 1] = sym;
2965 void __weak arch__fix_tev_from_maps(struct perf_probe_event *pev __maybe_unused,
2966 struct probe_trace_event *tev __maybe_unused,
2967 struct map *map __maybe_unused,
2968 struct symbol *sym __maybe_unused) { }
2971 * Find probe function addresses from map.
2972 * Return an error or the number of found probe_trace_event
2974 static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
2975 struct probe_trace_event **tevs)
2977 struct map *map = NULL;
2978 struct ref_reloc_sym *reloc_sym = NULL;
2980 struct symbol **syms = NULL;
2981 struct probe_trace_event *tev;
2982 struct perf_probe_point *pp = &pev->point;
2983 struct probe_trace_point *tp;
2984 int num_matched_functions;
2985 int ret, i, j, skipped = 0;
2988 map = get_target_map(pev->target, pev->nsi, pev->uprobes);
2994 syms = malloc(sizeof(struct symbol *) * probe_conf.max_probes);
3001 * Load matched symbols: Since the different local symbols may have
3002 * same name but different addresses, this lists all the symbols.
3004 num_matched_functions = find_probe_functions(map, pp->function, syms);
3005 if (num_matched_functions <= 0) {
3006 pr_err("Failed to find symbol %s in %s\n", pp->function,
3007 pev->target ? : "kernel");
3010 } else if (num_matched_functions > probe_conf.max_probes) {
3011 pr_err("Too many functions matched in %s\n",
3012 pev->target ? : "kernel");
3017 /* Note that the symbols in the kmodule are not relocated */
3018 if (!pev->uprobes && !pev->target &&
3019 (!pp->retprobe || kretprobe_offset_is_supported())) {
3020 reloc_sym = kernel_get_ref_reloc_sym(NULL);
3022 pr_warning("Relocated base symbol is not found!\n");
3028 /* Setup result trace-probe-events */
3029 *tevs = zalloc(sizeof(*tev) * num_matched_functions);
3037 for (j = 0; j < num_matched_functions; j++) {
3040 if (sym->type != STT_FUNC)
3043 /* There can be duplicated symbols in the map */
3044 for (i = 0; i < j; i++)
3045 if (sym->start == syms[i]->start) {
3046 pr_debug("Found duplicated symbol %s @ %" PRIx64 "\n",
3047 sym->name, sym->start);
3053 tev = (*tevs) + ret;
3055 if (ret == num_matched_functions) {
3056 pr_warning("Too many symbols are listed. Skip it.\n");
3061 if (pp->offset > sym->end - sym->start) {
3062 pr_warning("Offset %ld is bigger than the size of %s\n",
3063 pp->offset, sym->name);
3067 /* Add one probe point */
3068 tp->address = map->unmap_ip(map, sym->start) + pp->offset;
3070 /* Check the kprobe (not in module) is within .text */
3071 if (!pev->uprobes && !pev->target &&
3072 kprobe_warn_out_range(sym->name, tp->address)) {
3073 tp->symbol = NULL; /* Skip it */
3075 } else if (reloc_sym) {
3076 tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
3077 tp->offset = tp->address - reloc_sym->addr;
3079 tp->symbol = strdup_or_goto(sym->name, nomem_out);
3080 tp->offset = pp->offset;
3082 tp->realname = strdup_or_goto(sym->name, nomem_out);
3084 tp->retprobe = pp->retprobe;
3087 tev->point.module = strdup_or_goto(pev->target,
3090 mod_name = find_module_name(pev->target);
3092 strdup(mod_name ? mod_name : pev->target);
3094 if (!tev->point.module)
3098 tev->uprobes = pev->uprobes;
3099 tev->nargs = pev->nargs;
3101 tev->args = zalloc(sizeof(struct probe_trace_arg) *
3103 if (tev->args == NULL)
3106 for (i = 0; i < tev->nargs; i++) {
3107 if (pev->args[i].name)
3109 strdup_or_goto(pev->args[i].name,
3112 tev->args[i].value = strdup_or_goto(pev->args[i].var,
3114 if (pev->args[i].type)
3116 strdup_or_goto(pev->args[i].type,
3119 arch__fix_tev_from_maps(pev, tev, map, sym);
3121 if (ret == skipped) {
3134 clear_probe_trace_events(*tevs, num_matched_functions);
3139 static int try_to_find_absolute_address(struct perf_probe_event *pev,
3140 struct probe_trace_event **tevs)
3142 struct perf_probe_point *pp = &pev->point;
3143 struct probe_trace_event *tev;
3144 struct probe_trace_point *tp;
3147 if (!(pev->point.function && !strncmp(pev->point.function, "0x", 2)))
3149 if (perf_probe_event_need_dwarf(pev))
3153 * This is 'perf probe /lib/libc.so 0xabcd'. Try to probe at
3156 * Only one tev can be generated by this.
3158 *tevs = zalloc(sizeof(*tev));
3166 * Don't use tp->offset, use address directly, because
3167 * in synthesize_probe_trace_command() address cannot be
3170 tp->address = pev->point.abs_address;
3171 tp->retprobe = pp->retprobe;
3172 tev->uprobes = pev->uprobes;
3176 * Give it a '0x' leading symbol name.
3177 * In __add_probe_trace_events, a NULL symbol is interpreted as
3180 if (asprintf(&tp->symbol, "0x%lx", tp->address) < 0)
3183 /* For kprobe, check range */
3184 if ((!tev->uprobes) &&
3185 (kprobe_warn_out_range(tev->point.symbol,
3186 tev->point.address))) {
3191 if (asprintf(&tp->realname, "abs_%lx", tp->address) < 0)
3195 tp->module = strdup(pev->target);
3201 tev->group = strdup(pev->group);
3207 tev->event = strdup(pev->event);
3212 tev->nargs = pev->nargs;
3213 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
3217 for (i = 0; i < tev->nargs; i++)
3218 copy_to_probe_trace_arg(&tev->args[i], &pev->args[i]);
3223 clear_probe_trace_events(*tevs, 1);
3228 /* Concatinate two arrays */
3229 static void *memcat(void *a, size_t sz_a, void *b, size_t sz_b)
3233 ret = malloc(sz_a + sz_b);
3235 memcpy(ret, a, sz_a);
3236 memcpy(ret + sz_a, b, sz_b);
3242 concat_probe_trace_events(struct probe_trace_event **tevs, int *ntevs,
3243 struct probe_trace_event **tevs2, int ntevs2)
3245 struct probe_trace_event *new_tevs;
3255 if (*ntevs + ntevs2 > probe_conf.max_probes)
3258 /* Concatinate the array of probe_trace_event */
3259 new_tevs = memcat(*tevs, (*ntevs) * sizeof(**tevs),
3260 *tevs2, ntevs2 * sizeof(**tevs2));
3270 clear_probe_trace_events(*tevs2, ntevs2);
3277 * Try to find probe_trace_event from given probe caches. Return the number
3278 * of cached events found, if an error occurs return the error.
3280 static int find_cached_events(struct perf_probe_event *pev,
3281 struct probe_trace_event **tevs,
3284 struct probe_cache *cache;
3285 struct probe_cache_entry *entry;
3286 struct probe_trace_event *tmp_tevs = NULL;
3290 cache = probe_cache__new(target, pev->nsi);
3291 /* Return 0 ("not found") if the target has no probe cache. */
3295 for_each_probe_cache_entry(entry, cache) {
3296 /* Skip the cache entry which has no name */
3297 if (!entry->pev.event || !entry->pev.group)
3299 if ((!pev->group || strglobmatch(entry->pev.group, pev->group)) &&
3300 strglobmatch(entry->pev.event, pev->event)) {
3301 ret = probe_cache_entry__get_event(entry, &tmp_tevs);
3303 ret = concat_probe_trace_events(tevs, &ntevs,
3309 probe_cache__delete(cache);
3311 clear_probe_trace_events(*tevs, ntevs);
3315 if (ntevs > 0 && target && target[0] == '/')
3316 pev->uprobes = true;
3322 /* Try to find probe_trace_event from all probe caches */
3323 static int find_cached_events_all(struct perf_probe_event *pev,
3324 struct probe_trace_event **tevs)
3326 struct probe_trace_event *tmp_tevs = NULL;
3327 struct strlist *bidlist;
3328 struct str_node *nd;
3333 /* Get the buildid list of all valid caches */
3334 bidlist = build_id_cache__list_all(true);
3337 pr_debug("Failed to get buildids: %d\n", ret);
3342 strlist__for_each_entry(nd, bidlist) {
3343 pathname = build_id_cache__origname(nd->s);
3344 ret = find_cached_events(pev, &tmp_tevs, pathname);
3345 /* In the case of cnt == 0, we just skip it */
3347 ret = concat_probe_trace_events(tevs, &ntevs,
3353 strlist__delete(bidlist);
3356 clear_probe_trace_events(*tevs, ntevs);
3364 static int find_probe_trace_events_from_cache(struct perf_probe_event *pev,
3365 struct probe_trace_event **tevs)
3367 struct probe_cache *cache;
3368 struct probe_cache_entry *entry;
3369 struct probe_trace_event *tev;
3370 struct str_node *node;
3374 /* For SDT/cached events, we use special search functions */
3376 return find_cached_events_all(pev, tevs);
3378 return find_cached_events(pev, tevs, pev->target);
3380 cache = probe_cache__new(pev->target, pev->nsi);
3384 entry = probe_cache__find(cache, pev);
3386 /* SDT must be in the cache */
3387 ret = pev->sdt ? -ENOENT : 0;
3391 ret = strlist__nr_entries(entry->tevlist);
3392 if (ret > probe_conf.max_probes) {
3393 pr_debug("Too many entries matched in the cache of %s\n",
3394 pev->target ? : "kernel");
3399 *tevs = zalloc(ret * sizeof(*tev));
3406 strlist__for_each_entry(node, entry->tevlist) {
3407 tev = &(*tevs)[i++];
3408 ret = parse_probe_trace_command(node->s, tev);
3411 /* Set the uprobes attribute as same as original */
3412 tev->uprobes = pev->uprobes;
3417 probe_cache__delete(cache);
3421 static int convert_to_probe_trace_events(struct perf_probe_event *pev,
3422 struct probe_trace_event **tevs)
3426 if (!pev->group && !pev->sdt) {
3427 /* Set group name if not given */
3428 if (!pev->uprobes) {
3429 pev->group = strdup(PERFPROBE_GROUP);
3430 ret = pev->group ? 0 : -ENOMEM;
3432 ret = convert_exec_to_group(pev->target, &pev->group);
3434 pr_warning("Failed to make a group name.\n");
3439 ret = try_to_find_absolute_address(pev, tevs);
3443 /* At first, we need to lookup cache entry */
3444 ret = find_probe_trace_events_from_cache(pev, tevs);
3445 if (ret > 0 || pev->sdt) /* SDT can be found only in the cache */
3446 return ret == 0 ? -ENOENT : ret; /* Found in probe cache */
3448 /* Convert perf_probe_event with debuginfo */
3449 ret = try_to_find_probe_trace_events(pev, tevs);
3451 return ret; /* Found in debuginfo or got an error */
3453 return find_probe_trace_events_from_map(pev, tevs);
3456 int convert_perf_probe_events(struct perf_probe_event *pevs, int npevs)
3460 /* Loop 1: convert all events */
3461 for (i = 0; i < npevs; i++) {
3462 /* Init kprobe blacklist if needed */
3463 if (!pevs[i].uprobes)
3464 kprobe_blacklist__init();
3465 /* Convert with or without debuginfo */
3466 ret = convert_to_probe_trace_events(&pevs[i], &pevs[i].tevs);
3469 pevs[i].ntevs = ret;
3471 /* This just release blacklist only if allocated */
3472 kprobe_blacklist__release();
3477 static int show_probe_trace_event(struct probe_trace_event *tev)
3479 char *buf = synthesize_probe_trace_command(tev);
3482 pr_debug("Failed to synthesize probe trace event.\n");
3486 /* Showing definition always go stdout */
3487 printf("%s\n", buf);
3493 int show_probe_trace_events(struct perf_probe_event *pevs, int npevs)
3495 struct strlist *namelist = strlist__new(NULL, NULL);
3496 struct probe_trace_event *tev;
3497 struct perf_probe_event *pev;
3503 for (j = 0; j < npevs && !ret; j++) {
3505 for (i = 0; i < pev->ntevs && !ret; i++) {
3506 tev = &pev->tevs[i];
3507 /* Skip if the symbol is out of .text or blacklisted */
3508 if (!tev->point.symbol && !pev->uprobes)
3511 /* Set new name for tev (and update namelist) */
3512 ret = probe_trace_event__set_name(tev, pev,
3515 ret = show_probe_trace_event(tev);
3518 strlist__delete(namelist);
3523 int apply_perf_probe_events(struct perf_probe_event *pevs, int npevs)
3527 /* Loop 2: add all events */
3528 for (i = 0; i < npevs; i++) {
3529 ret = __add_probe_trace_events(&pevs[i], pevs[i].tevs,
3531 probe_conf.force_add);
3538 void cleanup_perf_probe_events(struct perf_probe_event *pevs, int npevs)
3541 struct perf_probe_event *pev;
3543 /* Loop 3: cleanup and free trace events */
3544 for (i = 0; i < npevs; i++) {
3546 for (j = 0; j < pevs[i].ntevs; j++)
3547 clear_probe_trace_event(&pevs[i].tevs[j]);
3548 zfree(&pevs[i].tevs);
3550 nsinfo__zput(pev->nsi);
3551 clear_perf_probe_event(&pevs[i]);
3555 int add_perf_probe_events(struct perf_probe_event *pevs, int npevs)
3559 ret = init_probe_symbol_maps(pevs->uprobes);
3563 ret = convert_perf_probe_events(pevs, npevs);
3565 ret = apply_perf_probe_events(pevs, npevs);
3567 cleanup_perf_probe_events(pevs, npevs);
3569 exit_probe_symbol_maps();
3573 int del_perf_probe_events(struct strfilter *filter)
3575 int ret, ret2, ufd = -1, kfd = -1;
3576 char *str = strfilter__string(filter);
3581 /* Get current event names */
3582 ret = probe_file__open_both(&kfd, &ufd, PF_FL_RW);
3586 ret = probe_file__del_events(kfd, filter);
3587 if (ret < 0 && ret != -ENOENT)
3590 ret2 = probe_file__del_events(ufd, filter);
3591 if (ret2 < 0 && ret2 != -ENOENT) {
3608 int show_available_funcs(const char *target, struct nsinfo *nsi,
3609 struct strfilter *_filter, bool user)
3615 ret = init_probe_symbol_maps(user);
3619 /* Get a symbol map */
3620 map = get_target_map(target, nsi, user);
3622 pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
3626 ret = map__load(map);
3629 char *str = strfilter__string(_filter);
3630 pr_err("Failed to find symbols matched to \"%s\"\n",
3634 pr_err("Failed to load symbols in %s\n",
3635 (target) ? : "kernel");
3638 if (!dso__sorted_by_name(map->dso))
3639 dso__sort_by_name(map->dso);
3641 /* Show all (filtered) symbols */
3644 for (nd = rb_first_cached(&map->dso->symbol_names); nd;
3646 struct symbol_name_rb_node *pos = rb_entry(nd, struct symbol_name_rb_node, rb_node);
3648 if (strfilter__compare(_filter, pos->sym.name))
3649 printf("%s\n", pos->sym.name);
3653 exit_probe_symbol_maps();
3658 int copy_to_probe_trace_arg(struct probe_trace_arg *tvar,
3659 struct perf_probe_arg *pvar)
3661 tvar->value = strdup(pvar->var);
3662 if (tvar->value == NULL)
3665 tvar->type = strdup(pvar->type);
3666 if (tvar->type == NULL)
3670 tvar->name = strdup(pvar->name);
3671 if (tvar->name == NULL)