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>
23 #include "namespaces.h"
25 #include "strfilter.h"
30 #include "map_groups.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"
41 #include <linux/ctype.h>
42 #include <linux/zalloc.h>
44 #define PERFPROBE_GROUP "probe"
46 bool probe_event_dry_run; /* Dry run flag */
47 struct probe_conf probe_conf;
49 #define semantic_error(msg ...) pr_err("Semantic error :" msg)
51 int e_snprintf(char *str, size_t size, const char *format, ...)
56 ret = vsnprintf(str, size, format, ap);
63 static struct machine *host_machine;
65 /* Initialize symbol maps and path of vmlinux/modules */
66 int init_probe_symbol_maps(bool user_only)
70 symbol_conf.sort_by_name = true;
71 symbol_conf.allow_aliases = true;
72 ret = symbol__init(NULL);
74 pr_debug("Failed to init symbol map.\n");
78 if (host_machine || user_only) /* already initialized */
81 if (symbol_conf.vmlinux_name)
82 pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
84 host_machine = machine__new_host();
86 pr_debug("machine__new_host() failed.\n");
92 pr_warning("Failed to init vmlinux path.\n");
96 void exit_probe_symbol_maps(void)
98 machine__delete(host_machine);
103 static struct ref_reloc_sym *kernel_get_ref_reloc_sym(void)
105 /* kmap->ref_reloc_sym should be set if host_machine is initialized */
107 struct map *map = machine__kernel_map(host_machine);
109 if (map__load(map) < 0)
112 kmap = map__kmap(map);
115 return kmap->ref_reloc_sym;
118 static int kernel_get_symbol_address_by_name(const char *name, u64 *addr,
119 bool reloc, bool reladdr)
121 struct ref_reloc_sym *reloc_sym;
125 /* ref_reloc_sym is just a label. Need a special fix*/
126 reloc_sym = kernel_get_ref_reloc_sym();
127 if (reloc_sym && strcmp(name, reloc_sym->name) == 0)
128 *addr = (reloc) ? reloc_sym->addr : reloc_sym->unrelocated_addr;
130 sym = machine__find_kernel_symbol_by_name(host_machine, name, &map);
133 *addr = map->unmap_ip(map, sym->start) -
134 ((reloc) ? 0 : map->reloc) -
135 ((reladdr) ? map->start : 0);
140 static struct map *kernel_get_module_map(const char *module)
142 struct maps *maps = machine__kernel_maps(host_machine);
145 /* A file path -- this is an offline module */
146 if (module && strchr(module, '/'))
147 return dso__new_map(module);
150 pos = machine__kernel_map(host_machine);
151 return map__get(pos);
154 for (pos = maps__first(maps); pos; pos = map__next(pos)) {
155 /* short_name is "[module]" */
156 if (strncmp(pos->dso->short_name + 1, module,
157 pos->dso->short_name_len - 2) == 0 &&
158 module[pos->dso->short_name_len - 2] == '\0') {
159 return map__get(pos);
165 struct map *get_target_map(const char *target, struct nsinfo *nsi, bool user)
167 /* Init maps of given executable or kernel */
171 map = dso__new_map(target);
173 map->dso->nsinfo = nsinfo__get(nsi);
176 return kernel_get_module_map(target);
180 static int convert_exec_to_group(const char *exec, char **result)
182 char *ptr1, *ptr2, *exec_copy;
186 exec_copy = strdup(exec);
190 ptr1 = basename(exec_copy);
196 for (ptr2 = ptr1; *ptr2 != '\0'; ptr2++) {
197 if (!isalnum(*ptr2) && *ptr2 != '_') {
203 ret = e_snprintf(buf, 64, "%s_%s", PERFPROBE_GROUP, ptr1);
207 *result = strdup(buf);
208 ret = *result ? 0 : -ENOMEM;
215 static void clear_perf_probe_point(struct perf_probe_point *pp)
218 zfree(&pp->function);
219 zfree(&pp->lazy_line);
222 static void clear_probe_trace_events(struct probe_trace_event *tevs, int ntevs)
226 for (i = 0; i < ntevs; i++)
227 clear_probe_trace_event(tevs + i);
230 static bool kprobe_blacklist__listed(unsigned long address);
231 static bool kprobe_warn_out_range(const char *symbol, unsigned long address)
236 /* Get the address of _etext for checking non-probable text symbol */
237 ret = kernel_get_symbol_address_by_name("_etext", &etext_addr,
240 if (ret == 0 && etext_addr < address)
241 pr_warning("%s is out of .text, skip it.\n", symbol);
242 else if (kprobe_blacklist__listed(address))
243 pr_warning("%s is blacklisted function, skip it.\n", symbol);
251 * @module can be module name of module file path. In case of path,
252 * inspect elf and find out what is actual module name.
253 * Caller has to free mod_name after using it.
255 static char *find_module_name(const char *module)
263 char *mod_name = NULL;
266 fd = open(module, O_RDONLY);
270 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
274 if (gelf_getehdr(elf, &ehdr) == NULL)
277 sec = elf_section_by_name(elf, &ehdr, &shdr,
278 ".gnu.linkonce.this_module", NULL);
282 data = elf_getdata(sec, NULL);
283 if (!data || !data->d_buf)
288 * '.gnu.linkonce.this_module' section of kernel module elf directly
289 * maps to 'struct module' from linux/module.h. This section contains
290 * actual module name which will be used by kernel after loading it.
291 * But, we cannot use 'struct module' here since linux/module.h is not
292 * exposed to user-space. Offset of 'name' has remained same from long
293 * time, so hardcoding it here.
295 if (ehdr.e_ident[EI_CLASS] == ELFCLASS32)
297 else /* expect ELFCLASS64 by default */
300 mod_name = strdup((char *)data->d_buf + name_offset);
309 #ifdef HAVE_DWARF_SUPPORT
311 static int kernel_get_module_dso(const char *module, struct dso **pdso)
315 const char *vmlinux_name;
319 char module_name[128];
321 snprintf(module_name, sizeof(module_name), "[%s]", module);
322 map = map_groups__find_by_name(&host_machine->kmaps, module_name);
327 pr_debug("Failed to find module %s.\n", module);
331 map = machine__kernel_map(host_machine);
334 vmlinux_name = symbol_conf.vmlinux_name;
337 ret = dso__load_vmlinux(dso, map, vmlinux_name, false);
339 ret = dso__load_vmlinux_path(dso, map);
346 * Some binaries like glibc have special symbols which are on the symbol
347 * table, but not in the debuginfo. If we can find the address of the
348 * symbol from map, we can translate the address back to the probe point.
350 static int find_alternative_probe_point(struct debuginfo *dinfo,
351 struct perf_probe_point *pp,
352 struct perf_probe_point *result,
353 const char *target, struct nsinfo *nsi,
356 struct map *map = NULL;
361 /* This can work only for function-name based one */
362 if (!pp->function || pp->file)
365 map = get_target_map(target, nsi, uprobes);
369 /* Find the address of given function */
370 map__for_each_symbol_by_name(map, pp->function, sym) {
372 address = sym->start;
374 address = map->unmap_ip(map, sym->start) - map->reloc;
381 pr_debug("Symbol %s address found : %" PRIx64 "\n",
382 pp->function, address);
384 ret = debuginfo__find_probe_point(dinfo, (unsigned long)address,
387 ret = (!ret) ? -ENOENT : ret;
389 result->offset += pp->offset;
390 result->line += pp->line;
391 result->retprobe = pp->retprobe;
401 static int get_alternative_probe_event(struct debuginfo *dinfo,
402 struct perf_probe_event *pev,
403 struct perf_probe_point *tmp)
407 memcpy(tmp, &pev->point, sizeof(*tmp));
408 memset(&pev->point, 0, sizeof(pev->point));
409 ret = find_alternative_probe_point(dinfo, tmp, &pev->point, pev->target,
410 pev->nsi, pev->uprobes);
412 memcpy(&pev->point, tmp, sizeof(*tmp));
417 static int get_alternative_line_range(struct debuginfo *dinfo,
418 struct line_range *lr,
419 const char *target, bool user)
421 struct perf_probe_point pp = { .function = lr->function,
424 struct perf_probe_point result;
427 memset(&result, 0, sizeof(result));
429 if (lr->end != INT_MAX)
430 len = lr->end - lr->start;
431 ret = find_alternative_probe_point(dinfo, &pp, &result,
434 lr->function = result.function;
435 lr->file = result.file;
436 lr->start = result.line;
437 if (lr->end != INT_MAX)
438 lr->end = lr->start + len;
439 clear_perf_probe_point(&pp);
444 /* Open new debuginfo of given module */
445 static struct debuginfo *open_debuginfo(const char *module, struct nsinfo *nsi,
448 const char *path = module;
449 char reason[STRERR_BUFSIZE];
450 struct debuginfo *ret = NULL;
451 struct dso *dso = NULL;
455 if (!module || !strchr(module, '/')) {
456 err = kernel_get_module_dso(module, &dso);
458 if (!dso || dso->load_errno == 0) {
459 if (!str_error_r(-err, reason, STRERR_BUFSIZE))
460 strcpy(reason, "(unknown)");
462 dso__strerror_load(dso, reason, STRERR_BUFSIZE);
465 pr_err("Module %s is not loaded, please specify its full path name.\n", module);
467 pr_err("Failed to find the path for the kernel: %s\n", reason);
471 path = dso->long_name;
473 nsinfo__mountns_enter(nsi, &nsc);
474 ret = debuginfo__new(path);
475 if (!ret && !silent) {
476 pr_warning("The %s file has no debug information.\n", path);
477 if (!module || !strtailcmp(path, ".ko"))
478 pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, ");
480 pr_warning("Rebuild with -g, ");
481 pr_warning("or install an appropriate debuginfo package.\n");
483 nsinfo__mountns_exit(&nsc);
487 /* For caching the last debuginfo */
488 static struct debuginfo *debuginfo_cache;
489 static char *debuginfo_cache_path;
491 static struct debuginfo *debuginfo_cache__open(const char *module, bool silent)
493 const char *path = module;
495 /* If the module is NULL, it should be the kernel. */
499 if (debuginfo_cache_path && !strcmp(debuginfo_cache_path, path))
502 /* Copy module path */
503 free(debuginfo_cache_path);
504 debuginfo_cache_path = strdup(path);
505 if (!debuginfo_cache_path) {
506 debuginfo__delete(debuginfo_cache);
507 debuginfo_cache = NULL;
511 debuginfo_cache = open_debuginfo(module, NULL, silent);
512 if (!debuginfo_cache)
513 zfree(&debuginfo_cache_path);
515 return debuginfo_cache;
518 static void debuginfo_cache__exit(void)
520 debuginfo__delete(debuginfo_cache);
521 debuginfo_cache = NULL;
522 zfree(&debuginfo_cache_path);
526 static int get_text_start_address(const char *exec, unsigned long *address,
532 int fd, ret = -ENOENT;
535 nsinfo__mountns_enter(nsi, &nsc);
536 fd = open(exec, O_RDONLY);
537 nsinfo__mountns_exit(&nsc);
541 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
547 if (gelf_getehdr(elf, &ehdr) == NULL)
550 if (!elf_section_by_name(elf, &ehdr, &shdr, ".text", NULL))
553 *address = shdr.sh_addr - shdr.sh_offset;
564 * Convert trace point to probe point with debuginfo
566 static int find_perf_probe_point_from_dwarf(struct probe_trace_point *tp,
567 struct perf_probe_point *pp,
570 struct debuginfo *dinfo = NULL;
571 unsigned long stext = 0;
572 u64 addr = tp->address;
575 /* convert the address to dwarf address */
581 ret = get_text_start_address(tp->module, &stext, NULL);
585 } else if (tp->symbol) {
586 /* If the module is given, this returns relative address */
587 ret = kernel_get_symbol_address_by_name(tp->symbol, &addr,
588 false, !!tp->module);
594 pr_debug("try to find information at %" PRIx64 " in %s\n", addr,
595 tp->module ? : "kernel");
597 dinfo = debuginfo_cache__open(tp->module, verbose <= 0);
599 ret = debuginfo__find_probe_point(dinfo,
600 (unsigned long)addr, pp);
605 pp->retprobe = tp->retprobe;
609 pr_debug("Failed to find corresponding probes from debuginfo.\n");
610 return ret ? : -ENOENT;
613 /* Adjust symbol name and address */
614 static int post_process_probe_trace_point(struct probe_trace_point *tp,
615 struct map *map, unsigned long offs)
618 u64 addr = tp->address - offs;
620 sym = map__find_symbol(map, addr);
624 if (strcmp(sym->name, tp->symbol)) {
625 /* If we have no realname, use symbol for it */
627 tp->realname = tp->symbol;
630 tp->symbol = strdup(sym->name);
634 tp->offset = addr - sym->start;
641 * Rename DWARF symbols to ELF symbols -- gcc sometimes optimizes functions
642 * and generate new symbols with suffixes such as .constprop.N or .isra.N
643 * etc. Since those symbols are not recorded in DWARF, we have to find
644 * correct generated symbols from offline ELF binary.
645 * For online kernel or uprobes we don't need this because those are
646 * rebased on _text, or already a section relative address.
649 post_process_offline_probe_trace_events(struct probe_trace_event *tevs,
650 int ntevs, const char *pathname)
653 unsigned long stext = 0;
656 /* Prepare a map for offline binary */
657 map = dso__new_map(pathname);
658 if (!map || get_text_start_address(pathname, &stext, NULL) < 0) {
659 pr_warning("Failed to get ELF symbols for %s\n", pathname);
663 for (i = 0; i < ntevs; i++) {
664 ret = post_process_probe_trace_point(&tevs[i].point,
674 static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs,
675 int ntevs, const char *exec,
679 unsigned long stext = 0;
684 ret = get_text_start_address(exec, &stext, nsi);
688 for (i = 0; i < ntevs && ret >= 0; i++) {
689 /* point.address is the address of point.symbol + point.offset */
690 tevs[i].point.address -= stext;
691 tevs[i].point.module = strdup(exec);
692 if (!tevs[i].point.module) {
696 tevs[i].uprobes = true;
703 post_process_module_probe_trace_events(struct probe_trace_event *tevs,
704 int ntevs, const char *module,
705 struct debuginfo *dinfo)
707 Dwarf_Addr text_offs = 0;
709 char *mod_name = NULL;
715 map = get_target_map(module, NULL, false);
716 if (!map || debuginfo__get_text_offset(dinfo, &text_offs, true) < 0) {
717 pr_warning("Failed to get ELF symbols for %s\n", module);
721 mod_name = find_module_name(module);
722 for (i = 0; i < ntevs; i++) {
723 ret = post_process_probe_trace_point(&tevs[i].point,
724 map, (unsigned long)text_offs);
727 tevs[i].point.module =
728 strdup(mod_name ? mod_name : module);
729 if (!tevs[i].point.module) {
742 post_process_kernel_probe_trace_events(struct probe_trace_event *tevs,
745 struct ref_reloc_sym *reloc_sym;
749 /* Skip post process if the target is an offline kernel */
750 if (symbol_conf.ignore_vmlinux_buildid)
751 return post_process_offline_probe_trace_events(tevs, ntevs,
752 symbol_conf.vmlinux_name);
754 reloc_sym = kernel_get_ref_reloc_sym();
756 pr_warning("Relocated base symbol is not found!\n");
760 for (i = 0; i < ntevs; i++) {
761 if (!tevs[i].point.address)
763 if (tevs[i].point.retprobe && !kretprobe_offset_is_supported())
765 /* If we found a wrong one, mark it by NULL symbol */
766 if (kprobe_warn_out_range(tevs[i].point.symbol,
767 tevs[i].point.address)) {
771 tmp = strdup(reloc_sym->name);
775 /* If we have no realname, use symbol for it */
776 if (!tevs[i].point.realname)
777 tevs[i].point.realname = tevs[i].point.symbol;
779 free(tevs[i].point.symbol);
780 tevs[i].point.symbol = tmp;
781 tevs[i].point.offset = tevs[i].point.address -
782 reloc_sym->unrelocated_addr;
788 arch__post_process_probe_trace_events(struct perf_probe_event *pev __maybe_unused,
789 int ntevs __maybe_unused)
793 /* Post processing the probe events */
794 static int post_process_probe_trace_events(struct perf_probe_event *pev,
795 struct probe_trace_event *tevs,
796 int ntevs, const char *module,
797 bool uprobe, struct debuginfo *dinfo)
802 ret = add_exec_to_probe_trace_events(tevs, ntevs, module,
805 /* Currently ref_reloc_sym based probe is not for drivers */
806 ret = post_process_module_probe_trace_events(tevs, ntevs,
809 ret = post_process_kernel_probe_trace_events(tevs, ntevs);
812 arch__post_process_probe_trace_events(pev, ntevs);
817 /* Try to find perf_probe_event with debuginfo */
818 static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
819 struct probe_trace_event **tevs)
821 bool need_dwarf = perf_probe_event_need_dwarf(pev);
822 struct perf_probe_point tmp;
823 struct debuginfo *dinfo;
826 dinfo = open_debuginfo(pev->target, pev->nsi, !need_dwarf);
830 pr_debug("Could not open debuginfo. Try to use symbols.\n");
834 pr_debug("Try to find probe point from debuginfo.\n");
835 /* Searching trace events corresponding to a probe event */
836 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
838 if (ntevs == 0) { /* Not found, retry with an alternative */
839 ret = get_alternative_probe_event(dinfo, pev, &tmp);
841 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
843 * Write back to the original probe_event for
844 * setting appropriate (user given) event name
846 clear_perf_probe_point(&pev->point);
847 memcpy(&pev->point, &tmp, sizeof(tmp));
851 if (ntevs > 0) { /* Succeeded to find trace events */
852 pr_debug("Found %d probe_trace_events.\n", ntevs);
853 ret = post_process_probe_trace_events(pev, *tevs, ntevs,
854 pev->target, pev->uprobes, dinfo);
855 if (ret < 0 || ret == ntevs) {
856 pr_debug("Post processing failed or all events are skipped. (%d)\n", ret);
857 clear_probe_trace_events(*tevs, ntevs);
863 debuginfo__delete(dinfo);
865 if (ntevs == 0) { /* No error but failed to find probe point. */
866 pr_warning("Probe point '%s' not found.\n",
867 synthesize_perf_probe_point(&pev->point));
869 } else if (ntevs < 0) {
870 /* Error path : ntevs < 0 */
871 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
873 pr_warning("Warning: No dwarf info found in the vmlinux - "
874 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
876 pr_debug("Trying to use symbols.\n");
883 #define LINEBUF_SIZE 256
884 #define NR_ADDITIONAL_LINES 2
886 static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
888 char buf[LINEBUF_SIZE], sbuf[STRERR_BUFSIZE];
889 const char *color = show_num ? "" : PERF_COLOR_BLUE;
890 const char *prefix = NULL;
893 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
898 prefix = show_num ? "%7d " : " ";
899 color_fprintf(stdout, color, prefix, l);
901 color_fprintf(stdout, color, "%s", buf);
903 } while (strchr(buf, '\n') == NULL);
908 pr_warning("File read error: %s\n",
909 str_error_r(errno, sbuf, sizeof(sbuf)));
915 static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
917 int rv = __show_one_line(fp, l, skip, show_num);
919 pr_warning("Source file is shorter than expected.\n");
925 #define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
926 #define show_one_line(f,l) _show_one_line(f,l,false,false)
927 #define skip_one_line(f,l) _show_one_line(f,l,true,false)
928 #define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
931 * Show line-range always requires debuginfo to find source file and
934 static int __show_line_range(struct line_range *lr, const char *module,
939 struct debuginfo *dinfo;
943 char sbuf[STRERR_BUFSIZE];
945 /* Search a line range */
946 dinfo = open_debuginfo(module, NULL, false);
950 ret = debuginfo__find_line_range(dinfo, lr);
951 if (!ret) { /* Not found, retry with an alternative */
952 ret = get_alternative_line_range(dinfo, lr, module, user);
954 ret = debuginfo__find_line_range(dinfo, lr);
956 debuginfo__delete(dinfo);
957 if (ret == 0 || ret == -ENOENT) {
958 pr_warning("Specified source line is not found.\n");
960 } else if (ret < 0) {
961 pr_warning("Debuginfo analysis failed.\n");
965 /* Convert source file path */
967 ret = get_real_path(tmp, lr->comp_dir, &lr->path);
969 /* Free old path when new path is assigned */
974 pr_warning("Failed to find source file path.\n");
981 fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
982 lr->start - lr->offset);
984 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
986 fp = fopen(lr->path, "r");
988 pr_warning("Failed to open %s: %s\n", lr->path,
989 str_error_r(errno, sbuf, sizeof(sbuf)));
992 /* Skip to starting line number */
993 while (l < lr->start) {
994 ret = skip_one_line(fp, l++);
999 intlist__for_each_entry(ln, lr->line_list) {
1000 for (; ln->i > l; l++) {
1001 ret = show_one_line(fp, l - lr->offset);
1005 ret = show_one_line_with_num(fp, l++ - lr->offset);
1010 if (lr->end == INT_MAX)
1011 lr->end = l + NR_ADDITIONAL_LINES;
1012 while (l <= lr->end) {
1013 ret = show_one_line_or_eof(fp, l++ - lr->offset);
1022 int show_line_range(struct line_range *lr, const char *module,
1023 struct nsinfo *nsi, bool user)
1026 struct nscookie nsc;
1028 ret = init_probe_symbol_maps(user);
1031 nsinfo__mountns_enter(nsi, &nsc);
1032 ret = __show_line_range(lr, module, user);
1033 nsinfo__mountns_exit(&nsc);
1034 exit_probe_symbol_maps();
1039 static int show_available_vars_at(struct debuginfo *dinfo,
1040 struct perf_probe_event *pev,
1041 struct strfilter *_filter)
1045 struct str_node *node;
1046 struct variable_list *vls = NULL, *vl;
1047 struct perf_probe_point tmp;
1050 buf = synthesize_perf_probe_point(&pev->point);
1053 pr_debug("Searching variables at %s\n", buf);
1055 ret = debuginfo__find_available_vars_at(dinfo, pev, &vls);
1056 if (!ret) { /* Not found, retry with an alternative */
1057 ret = get_alternative_probe_event(dinfo, pev, &tmp);
1059 ret = debuginfo__find_available_vars_at(dinfo, pev,
1061 /* Release the old probe_point */
1062 clear_perf_probe_point(&tmp);
1066 if (ret == 0 || ret == -ENOENT) {
1067 pr_err("Failed to find the address of %s\n", buf);
1070 pr_warning("Debuginfo analysis failed.\n");
1074 /* Some variables are found */
1075 fprintf(stdout, "Available variables at %s\n", buf);
1076 for (i = 0; i < ret; i++) {
1079 * A probe point might be converted to
1080 * several trace points.
1082 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
1084 zfree(&vl->point.symbol);
1087 strlist__for_each_entry(node, vl->vars) {
1088 var = strchr(node->s, '\t') + 1;
1089 if (strfilter__compare(_filter, var)) {
1090 fprintf(stdout, "\t\t%s\n", node->s);
1094 strlist__delete(vl->vars);
1097 fprintf(stdout, "\t\t(No matched variables)\n");
1105 /* Show available variables on given probe point */
1106 int show_available_vars(struct perf_probe_event *pevs, int npevs,
1107 struct strfilter *_filter)
1110 struct debuginfo *dinfo;
1112 ret = init_probe_symbol_maps(pevs->uprobes);
1116 dinfo = open_debuginfo(pevs->target, pevs->nsi, false);
1124 for (i = 0; i < npevs && ret >= 0; i++)
1125 ret = show_available_vars_at(dinfo, &pevs[i], _filter);
1127 debuginfo__delete(dinfo);
1129 exit_probe_symbol_maps();
1133 #else /* !HAVE_DWARF_SUPPORT */
1135 static void debuginfo_cache__exit(void)
1140 find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused,
1141 struct perf_probe_point *pp __maybe_unused,
1142 bool is_kprobe __maybe_unused)
1147 static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
1148 struct probe_trace_event **tevs __maybe_unused)
1150 if (perf_probe_event_need_dwarf(pev)) {
1151 pr_warning("Debuginfo-analysis is not supported.\n");
1158 int show_line_range(struct line_range *lr __maybe_unused,
1159 const char *module __maybe_unused,
1160 struct nsinfo *nsi __maybe_unused,
1161 bool user __maybe_unused)
1163 pr_warning("Debuginfo-analysis is not supported.\n");
1167 int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
1168 int npevs __maybe_unused,
1169 struct strfilter *filter __maybe_unused)
1171 pr_warning("Debuginfo-analysis is not supported.\n");
1176 void line_range__clear(struct line_range *lr)
1178 zfree(&lr->function);
1181 zfree(&lr->comp_dir);
1182 intlist__delete(lr->line_list);
1185 int line_range__init(struct line_range *lr)
1187 memset(lr, 0, sizeof(*lr));
1188 lr->line_list = intlist__new(NULL);
1195 static int parse_line_num(char **ptr, int *val, const char *what)
1197 const char *start = *ptr;
1200 *val = strtol(*ptr, ptr, 0);
1201 if (errno || *ptr == start) {
1202 semantic_error("'%s' is not a valid number.\n", what);
1208 /* Check the name is good for event, group or function */
1209 static bool is_c_func_name(const char *name)
1211 if (!isalpha(*name) && *name != '_')
1213 while (*++name != '\0') {
1214 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
1221 * Stuff 'lr' according to the line range described by 'arg'.
1222 * The line range syntax is described by:
1224 * SRC[:SLN[+NUM|-ELN]]
1225 * FNC[@SRC][:SLN[+NUM|-ELN]]
1227 int parse_line_range_desc(const char *arg, struct line_range *lr)
1229 char *range, *file, *name = strdup(arg);
1238 range = strchr(name, ':');
1242 err = parse_line_num(&range, &lr->start, "start line");
1246 if (*range == '+' || *range == '-') {
1247 const char c = *range++;
1249 err = parse_line_num(&range, &lr->end, "end line");
1254 lr->end += lr->start;
1256 * Adjust the number of lines here.
1257 * If the number of lines == 1, the
1258 * the end of line should be equal to
1259 * the start of line.
1265 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
1268 if (lr->start > lr->end) {
1269 semantic_error("Start line must be smaller"
1270 " than end line.\n");
1273 if (*range != '\0') {
1274 semantic_error("Tailing with invalid str '%s'.\n", range);
1279 file = strchr(name, '@');
1282 lr->file = strdup(++file);
1283 if (lr->file == NULL) {
1287 lr->function = name;
1288 } else if (strchr(name, '/') || strchr(name, '.'))
1290 else if (is_c_func_name(name))/* We reuse it for checking funcname */
1291 lr->function = name;
1292 else { /* Invalid name */
1293 semantic_error("'%s' is not a valid function name.\n", name);
1304 static int parse_perf_probe_event_name(char **arg, struct perf_probe_event *pev)
1308 ptr = strpbrk_esc(*arg, ":");
1311 if (!pev->sdt && !is_c_func_name(*arg))
1313 pev->group = strdup_esc(*arg);
1320 pev->event = strdup_esc(*arg);
1321 if (pev->event == NULL)
1324 if (!pev->sdt && !is_c_func_name(pev->event)) {
1328 semantic_error("%s is bad for event name -it must "
1329 "follow C symbol-naming rule.\n", *arg);
1335 /* Parse probepoint definition. */
1336 static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
1338 struct perf_probe_point *pp = &pev->point;
1341 bool file_spec = false;
1346 * perf probe [GRP:][EVENT=]SRC[:LN|;PTN]
1347 * perf probe [GRP:][EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
1348 * perf probe %[GRP:]SDT_EVENT
1353 if (is_sdt_event(arg)) {
1359 ptr = strpbrk_esc(arg, ";=@+%");
1363 semantic_error("%s must be an SDT name.\n",
1367 /* This must be a target file name or build id */
1368 tmp = build_id_cache__complement(ptr + 1);
1370 pev->target = build_id_cache__origname(tmp);
1373 pev->target = strdup_esc(ptr + 1);
1378 ret = parse_perf_probe_event_name(&arg, pev);
1380 if (asprintf(&pev->point.function, "%%%s", pev->event) < 0)
1386 if (ptr && *ptr == '=') { /* Event name */
1389 ret = parse_perf_probe_event_name(&arg, pev);
1397 * Check arg is function or file name and copy it.
1399 * We consider arg to be a file spec if and only if it satisfies
1400 * all of the below criteria::
1401 * - it does not include any of "+@%",
1402 * - it includes one of ":;", and
1403 * - it has a period '.' in the name.
1405 * Otherwise, we consider arg to be a function specification.
1407 if (!strpbrk_esc(arg, "+@%")) {
1408 ptr = strpbrk_esc(arg, ";:");
1409 /* This is a file spec if it includes a '.' before ; or : */
1410 if (ptr && memchr(arg, '.', ptr - arg))
1414 ptr = strpbrk_esc(arg, ";:+@%");
1423 tmp = strdup_esc(arg);
1434 * Keep pp->function even if this is absolute address,
1435 * so it can mark whether abs_address is valid.
1436 * Which make 'perf probe lib.bin 0x0' possible.
1438 * Note that checking length of tmp is not needed
1439 * because when we access tmp[1] we know tmp[0] is '0',
1440 * so tmp[1] should always valid (but could be '\0').
1442 if (tmp && !strncmp(tmp, "0x", 2)) {
1443 pp->abs_address = strtoul(pp->function, &tmp, 0);
1445 semantic_error("Invalid absolute address.\n");
1451 /* Parse other options */
1455 if (c == ';') { /* Lazy pattern must be the last part */
1456 pp->lazy_line = strdup(arg); /* let leave escapes */
1457 if (pp->lazy_line == NULL)
1461 ptr = strpbrk_esc(arg, ";:+@%");
1467 case ':': /* Line number */
1468 pp->line = strtoul(arg, &tmp, 0);
1470 semantic_error("There is non-digit char"
1471 " in line number.\n");
1475 case '+': /* Byte offset from a symbol */
1476 pp->offset = strtoul(arg, &tmp, 0);
1478 semantic_error("There is non-digit character"
1483 case '@': /* File name */
1485 semantic_error("SRC@SRC is not allowed.\n");
1488 pp->file = strdup_esc(arg);
1489 if (pp->file == NULL)
1492 case '%': /* Probe places */
1493 if (strcmp(arg, "return") == 0) {
1495 } else { /* Others not supported yet */
1496 semantic_error("%%%s is not supported.\n", arg);
1500 default: /* Buggy case */
1501 pr_err("This program has a bug at %s:%d.\n",
1502 __FILE__, __LINE__);
1508 /* Exclusion check */
1509 if (pp->lazy_line && pp->line) {
1510 semantic_error("Lazy pattern can't be used with"
1515 if (pp->lazy_line && pp->offset) {
1516 semantic_error("Lazy pattern can't be used with offset.\n");
1520 if (pp->line && pp->offset) {
1521 semantic_error("Offset can't be used with line number.\n");
1525 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
1526 semantic_error("File always requires line number or "
1531 if (pp->offset && !pp->function) {
1532 semantic_error("Offset requires an entry function.\n");
1536 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
1537 semantic_error("Offset/Line/Lazy pattern can't be used with "
1542 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
1543 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
1548 /* Parse perf-probe event argument */
1549 static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
1551 char *tmp, *goodname;
1552 struct perf_probe_arg_field **fieldp;
1554 pr_debug("parsing arg: %s into ", str);
1556 tmp = strchr(str, '=');
1558 arg->name = strndup(str, tmp - str);
1559 if (arg->name == NULL)
1561 pr_debug("name:%s ", arg->name);
1565 tmp = strchr(str, '@');
1566 if (tmp && tmp != str && strcmp(tmp + 1, "user")) { /* user attr */
1567 if (!user_access_is_supported()) {
1568 semantic_error("ftrace does not support user access\n");
1572 arg->user_access = true;
1573 pr_debug("user_access ");
1576 tmp = strchr(str, ':');
1577 if (tmp) { /* Type setting */
1579 arg->type = strdup(tmp + 1);
1580 if (arg->type == NULL)
1582 pr_debug("type:%s ", arg->type);
1585 tmp = strpbrk(str, "-.[");
1586 if (!is_c_varname(str) || !tmp) {
1587 /* A variable, register, symbol or special value */
1588 arg->var = strdup(str);
1589 if (arg->var == NULL)
1591 pr_debug("%s\n", arg->var);
1595 /* Structure fields or array element */
1596 arg->var = strndup(str, tmp - str);
1597 if (arg->var == NULL)
1599 goodname = arg->var;
1600 pr_debug("%s, ", arg->var);
1601 fieldp = &arg->field;
1604 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
1605 if (*fieldp == NULL)
1607 if (*tmp == '[') { /* Array */
1609 (*fieldp)->index = strtol(str + 1, &tmp, 0);
1610 (*fieldp)->ref = true;
1611 if (*tmp != ']' || tmp == str + 1) {
1612 semantic_error("Array index must be a"
1619 } else { /* Structure */
1622 (*fieldp)->ref = false;
1623 } else if (tmp[1] == '>') {
1625 (*fieldp)->ref = true;
1627 semantic_error("Argument parse error: %s\n",
1631 tmp = strpbrk(str, "-.[");
1634 (*fieldp)->name = strndup(str, tmp - str);
1635 if ((*fieldp)->name == NULL)
1638 goodname = (*fieldp)->name;
1639 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
1640 fieldp = &(*fieldp)->next;
1643 (*fieldp)->name = strdup(str);
1644 if ((*fieldp)->name == NULL)
1647 goodname = (*fieldp)->name;
1648 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
1650 /* If no name is specified, set the last field name (not array index)*/
1652 arg->name = strdup(goodname);
1653 if (arg->name == NULL)
1659 /* Parse perf-probe event command */
1660 int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
1663 int argc, i, ret = 0;
1665 argv = argv_split(cmd, &argc);
1667 pr_debug("Failed to split arguments.\n");
1670 if (argc - 1 > MAX_PROBE_ARGS) {
1671 semantic_error("Too many probe arguments (%d).\n", argc - 1);
1675 /* Parse probe point */
1676 ret = parse_perf_probe_point(argv[0], pev);
1680 /* Copy arguments and ensure return probe has no C argument */
1681 pev->nargs = argc - 1;
1682 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1683 if (pev->args == NULL) {
1687 for (i = 0; i < pev->nargs && ret >= 0; i++) {
1688 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1690 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
1691 semantic_error("You can't specify local variable for"
1702 /* Returns true if *any* ARG is either C variable, $params or $vars. */
1703 bool perf_probe_with_var(struct perf_probe_event *pev)
1707 for (i = 0; i < pev->nargs; i++)
1708 if (is_c_varname(pev->args[i].var) ||
1709 !strcmp(pev->args[i].var, PROBE_ARG_PARAMS) ||
1710 !strcmp(pev->args[i].var, PROBE_ARG_VARS))
1715 /* Return true if this perf_probe_event requires debuginfo */
1716 bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
1718 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1721 if (perf_probe_with_var(pev))
1727 /* Parse probe_events event into struct probe_point */
1728 int parse_probe_trace_command(const char *cmd, struct probe_trace_event *tev)
1730 struct probe_trace_point *tp = &tev->point;
1733 char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
1737 pr_debug("Parsing probe_events: %s\n", cmd);
1738 argv = argv_split(cmd, &argc);
1740 pr_debug("Failed to split arguments.\n");
1744 semantic_error("Too few probe arguments.\n");
1749 /* Scan event and group name. */
1750 argv0_str = strdup(argv[0]);
1751 if (argv0_str == NULL) {
1755 fmt1_str = strtok_r(argv0_str, ":", &fmt);
1756 fmt2_str = strtok_r(NULL, "/", &fmt);
1757 fmt3_str = strtok_r(NULL, " \t", &fmt);
1758 if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
1759 || fmt3_str == NULL) {
1760 semantic_error("Failed to parse event name: %s\n", argv[0]);
1765 tev->group = strdup(fmt2_str);
1766 tev->event = strdup(fmt3_str);
1767 if (tev->group == NULL || tev->event == NULL) {
1771 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
1773 tp->retprobe = (pr == 'r');
1775 /* Scan module name(if there), function name and offset */
1776 p = strchr(argv[1], ':');
1778 tp->module = strndup(argv[1], p - argv[1]);
1783 tev->uprobes = (tp->module[0] == '/');
1787 fmt1_str = strtok_r(p, "+", &fmt);
1788 /* only the address started with 0x */
1789 if (fmt1_str[0] == '0') {
1791 * Fix a special case:
1792 * if address == 0, kernel reports something like:
1793 * p:probe_libc/abs_0 /lib/libc-2.18.so:0x (null) arg1=%ax
1794 * Newer kernel may fix that, but we want to
1795 * support old kernel also.
1797 if (strcmp(fmt1_str, "0x") == 0) {
1798 if (!argv[2] || strcmp(argv[2], "(null)")) {
1805 for (i = 2; argv[i + 1] != NULL; i++)
1806 argv[i] = argv[i + 1];
1811 tp->address = strtoul(fmt1_str, NULL, 0);
1813 /* Only the symbol-based probe has offset */
1814 tp->symbol = strdup(fmt1_str);
1815 if (tp->symbol == NULL) {
1819 fmt2_str = strtok_r(NULL, "", &fmt);
1820 if (fmt2_str == NULL)
1823 tp->offset = strtoul(fmt2_str, NULL, 10);
1827 fmt2_str = strchr(p, '(');
1829 tp->ref_ctr_offset = strtoul(fmt2_str + 1, NULL, 0);
1832 tev->nargs = argc - 2;
1833 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
1834 if (tev->args == NULL) {
1838 for (i = 0; i < tev->nargs; i++) {
1839 p = strchr(argv[i + 2], '=');
1840 if (p) /* We don't need which register is assigned. */
1844 tev->args[i].name = strdup(argv[i + 2]);
1845 /* TODO: parse regs and offset */
1846 tev->args[i].value = strdup(p);
1847 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1859 /* Compose only probe arg */
1860 char *synthesize_perf_probe_arg(struct perf_probe_arg *pa)
1862 struct perf_probe_arg_field *field = pa->field;
1867 if (strbuf_init(&buf, 64) < 0)
1870 if (pa->name && pa->var)
1871 err = strbuf_addf(&buf, "%s=%s", pa->name, pa->var);
1873 err = strbuf_addstr(&buf, pa->name ?: pa->var);
1878 if (field->name[0] == '[')
1879 err = strbuf_addstr(&buf, field->name);
1881 err = strbuf_addf(&buf, "%s%s", field->ref ? "->" : ".",
1883 field = field->next;
1889 if (strbuf_addf(&buf, ":%s", pa->type) < 0)
1892 ret = strbuf_detach(&buf, NULL);
1894 strbuf_release(&buf);
1898 /* Compose only probe point (not argument) */
1899 char *synthesize_perf_probe_point(struct perf_probe_point *pp)
1902 char *tmp, *ret = NULL;
1905 if (strbuf_init(&buf, 64) < 0)
1909 if (strbuf_addstr(&buf, pp->function) < 0)
1912 err = strbuf_addf(&buf, "+%lu", pp->offset);
1914 err = strbuf_addf(&buf, ":%d", pp->line);
1915 else if (pp->retprobe)
1916 err = strbuf_addstr(&buf, "%return");
1924 tmp = strchr(pp->file + len - 30, '/');
1925 tmp = tmp ? tmp + 1 : pp->file + len - 30;
1927 err = strbuf_addf(&buf, "@%s", tmp);
1928 if (!err && !pp->function && pp->line)
1929 err = strbuf_addf(&buf, ":%d", pp->line);
1932 ret = strbuf_detach(&buf, NULL);
1934 strbuf_release(&buf);
1938 char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1941 char *tmp, *ret = NULL;
1944 if (strbuf_init(&buf, 64))
1947 if (strbuf_addf(&buf, "%s:%s=", pev->group ?: PERFPROBE_GROUP,
1951 tmp = synthesize_perf_probe_point(&pev->point);
1952 if (!tmp || strbuf_addstr(&buf, tmp) < 0)
1956 for (i = 0; i < pev->nargs; i++) {
1957 tmp = synthesize_perf_probe_arg(pev->args + i);
1958 if (!tmp || strbuf_addf(&buf, " %s", tmp) < 0)
1963 ret = strbuf_detach(&buf, NULL);
1965 strbuf_release(&buf);
1969 static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
1970 struct strbuf *buf, int depth)
1974 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
1979 err = strbuf_addf(buf, "%+ld(", ref->offset);
1980 return (err < 0) ? err : depth;
1983 static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
1986 struct probe_trace_arg_ref *ref = arg->ref;
1989 /* Argument name or separator */
1991 err = strbuf_addf(buf, " %s=", arg->name);
1993 err = strbuf_addch(buf, ' ');
1997 /* Special case: @XXX */
1998 if (arg->value[0] == '@' && arg->ref)
2001 /* Dereferencing arguments */
2003 depth = __synthesize_probe_trace_arg_ref(ref, buf, 1);
2008 /* Print argument value */
2009 if (arg->value[0] == '@' && arg->ref)
2010 err = strbuf_addf(buf, "%s%+ld", arg->value, arg->ref->offset);
2012 err = strbuf_addstr(buf, arg->value);
2015 while (!err && depth--)
2016 err = strbuf_addch(buf, ')');
2018 /* Print argument type */
2019 if (!err && arg->type)
2020 err = strbuf_addf(buf, ":%s", arg->type);
2026 synthesize_uprobe_trace_def(struct probe_trace_event *tev, struct strbuf *buf)
2028 struct probe_trace_point *tp = &tev->point;
2031 err = strbuf_addf(buf, "%s:0x%lx", tp->module, tp->address);
2033 if (err >= 0 && tp->ref_ctr_offset) {
2034 if (!uprobe_ref_ctr_is_supported())
2036 err = strbuf_addf(buf, "(0x%lx)", tp->ref_ctr_offset);
2038 return err >= 0 ? 0 : -1;
2041 char *synthesize_probe_trace_command(struct probe_trace_event *tev)
2043 struct probe_trace_point *tp = &tev->point;
2048 /* Uprobes must have tp->module */
2049 if (tev->uprobes && !tp->module)
2052 if (strbuf_init(&buf, 32) < 0)
2055 if (strbuf_addf(&buf, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
2056 tev->group, tev->event) < 0)
2059 * If tp->address == 0, then this point must be a
2060 * absolute address uprobe.
2061 * try_to_find_absolute_address() should have made
2062 * tp->symbol to "0x0".
2064 if (tev->uprobes && !tp->address) {
2065 if (!tp->symbol || strcmp(tp->symbol, "0x0"))
2069 /* Use the tp->address for uprobes */
2071 err = synthesize_uprobe_trace_def(tev, &buf);
2072 } else if (!strncmp(tp->symbol, "0x", 2)) {
2073 /* Absolute address. See try_to_find_absolute_address() */
2074 err = strbuf_addf(&buf, "%s%s0x%lx", tp->module ?: "",
2075 tp->module ? ":" : "", tp->address);
2077 err = strbuf_addf(&buf, "%s%s%s+%lu", tp->module ?: "",
2078 tp->module ? ":" : "", tp->symbol, tp->offset);
2084 for (i = 0; i < tev->nargs; i++)
2085 if (synthesize_probe_trace_arg(&tev->args[i], &buf) < 0)
2088 ret = strbuf_detach(&buf, NULL);
2090 strbuf_release(&buf);
2094 static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
2095 struct perf_probe_point *pp,
2098 struct symbol *sym = NULL;
2099 struct map *map = NULL;
2100 u64 addr = tp->address;
2104 map = dso__new_map(tp->module);
2107 sym = map__find_symbol(map, addr);
2109 if (tp->symbol && !addr) {
2110 if (kernel_get_symbol_address_by_name(tp->symbol,
2111 &addr, true, false) < 0)
2116 sym = machine__find_kernel_symbol(host_machine, addr, &map);
2123 pp->retprobe = tp->retprobe;
2124 pp->offset = addr - map->unmap_ip(map, sym->start);
2125 pp->function = strdup(sym->name);
2126 ret = pp->function ? 0 : -ENOMEM;
2129 if (map && !is_kprobe) {
2136 static int convert_to_perf_probe_point(struct probe_trace_point *tp,
2137 struct perf_probe_point *pp,
2143 ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe);
2146 ret = find_perf_probe_point_from_map(tp, pp, is_kprobe);
2150 pr_debug("Failed to find probe point from both of dwarf and map.\n");
2153 pp->function = strdup(tp->symbol);
2154 pp->offset = tp->offset;
2156 ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address);
2159 pp->function = strdup(buf);
2162 if (pp->function == NULL)
2165 pp->retprobe = tp->retprobe;
2170 static int convert_to_perf_probe_event(struct probe_trace_event *tev,
2171 struct perf_probe_event *pev, bool is_kprobe)
2173 struct strbuf buf = STRBUF_INIT;
2176 /* Convert event/group name */
2177 pev->event = strdup(tev->event);
2178 pev->group = strdup(tev->group);
2179 if (pev->event == NULL || pev->group == NULL)
2182 /* Convert trace_point to probe_point */
2183 ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
2187 /* Convert trace_arg to probe_arg */
2188 pev->nargs = tev->nargs;
2189 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
2190 if (pev->args == NULL)
2192 for (i = 0; i < tev->nargs && ret >= 0; i++) {
2193 if (tev->args[i].name)
2194 pev->args[i].name = strdup(tev->args[i].name);
2196 if ((ret = strbuf_init(&buf, 32)) < 0)
2198 ret = synthesize_probe_trace_arg(&tev->args[i], &buf);
2199 pev->args[i].name = strbuf_detach(&buf, NULL);
2201 if (pev->args[i].name == NULL && ret >= 0)
2206 clear_perf_probe_event(pev);
2211 void clear_perf_probe_event(struct perf_probe_event *pev)
2213 struct perf_probe_arg_field *field, *next;
2218 zfree(&pev->target);
2219 clear_perf_probe_point(&pev->point);
2221 for (i = 0; i < pev->nargs; i++) {
2222 zfree(&pev->args[i].name);
2223 zfree(&pev->args[i].var);
2224 zfree(&pev->args[i].type);
2225 field = pev->args[i].field;
2228 zfree(&field->name);
2237 #define strdup_or_goto(str, label) \
2238 ({ char *__p = NULL; if (str && !(__p = strdup(str))) goto label; __p; })
2240 static int perf_probe_point__copy(struct perf_probe_point *dst,
2241 struct perf_probe_point *src)
2243 dst->file = strdup_or_goto(src->file, out_err);
2244 dst->function = strdup_or_goto(src->function, out_err);
2245 dst->lazy_line = strdup_or_goto(src->lazy_line, out_err);
2246 dst->line = src->line;
2247 dst->retprobe = src->retprobe;
2248 dst->offset = src->offset;
2252 clear_perf_probe_point(dst);
2256 static int perf_probe_arg__copy(struct perf_probe_arg *dst,
2257 struct perf_probe_arg *src)
2259 struct perf_probe_arg_field *field, **ppfield;
2261 dst->name = strdup_or_goto(src->name, out_err);
2262 dst->var = strdup_or_goto(src->var, out_err);
2263 dst->type = strdup_or_goto(src->type, out_err);
2266 ppfield = &(dst->field);
2268 *ppfield = zalloc(sizeof(*field));
2271 (*ppfield)->name = strdup_or_goto(field->name, out_err);
2272 (*ppfield)->index = field->index;
2273 (*ppfield)->ref = field->ref;
2274 field = field->next;
2275 ppfield = &((*ppfield)->next);
2282 int perf_probe_event__copy(struct perf_probe_event *dst,
2283 struct perf_probe_event *src)
2287 dst->event = strdup_or_goto(src->event, out_err);
2288 dst->group = strdup_or_goto(src->group, out_err);
2289 dst->target = strdup_or_goto(src->target, out_err);
2290 dst->uprobes = src->uprobes;
2292 if (perf_probe_point__copy(&dst->point, &src->point) < 0)
2295 dst->args = zalloc(sizeof(struct perf_probe_arg) * src->nargs);
2298 dst->nargs = src->nargs;
2300 for (i = 0; i < src->nargs; i++)
2301 if (perf_probe_arg__copy(&dst->args[i], &src->args[i]) < 0)
2306 clear_perf_probe_event(dst);
2310 void clear_probe_trace_event(struct probe_trace_event *tev)
2312 struct probe_trace_arg_ref *ref, *next;
2317 zfree(&tev->point.symbol);
2318 zfree(&tev->point.realname);
2319 zfree(&tev->point.module);
2320 for (i = 0; i < tev->nargs; i++) {
2321 zfree(&tev->args[i].name);
2322 zfree(&tev->args[i].value);
2323 zfree(&tev->args[i].type);
2324 ref = tev->args[i].ref;
2334 struct kprobe_blacklist_node {
2335 struct list_head list;
2336 unsigned long start;
2341 static void kprobe_blacklist__delete(struct list_head *blacklist)
2343 struct kprobe_blacklist_node *node;
2345 while (!list_empty(blacklist)) {
2346 node = list_first_entry(blacklist,
2347 struct kprobe_blacklist_node, list);
2348 list_del_init(&node->list);
2349 zfree(&node->symbol);
2354 static int kprobe_blacklist__load(struct list_head *blacklist)
2356 struct kprobe_blacklist_node *node;
2357 const char *__debugfs = debugfs__mountpoint();
2358 char buf[PATH_MAX], *p;
2362 if (__debugfs == NULL)
2365 ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs);
2369 fp = fopen(buf, "r");
2374 while (fgets(buf, PATH_MAX, fp)) {
2375 node = zalloc(sizeof(*node));
2380 INIT_LIST_HEAD(&node->list);
2381 list_add_tail(&node->list, blacklist);
2382 if (sscanf(buf, "0x%lx-0x%lx", &node->start, &node->end) != 2) {
2386 p = strchr(buf, '\t');
2389 if (p[strlen(p) - 1] == '\n')
2390 p[strlen(p) - 1] = '\0';
2392 p = (char *)"unknown";
2393 node->symbol = strdup(p);
2394 if (!node->symbol) {
2398 pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
2399 node->start, node->end, node->symbol);
2403 kprobe_blacklist__delete(blacklist);
2409 static struct kprobe_blacklist_node *
2410 kprobe_blacklist__find_by_address(struct list_head *blacklist,
2411 unsigned long address)
2413 struct kprobe_blacklist_node *node;
2415 list_for_each_entry(node, blacklist, list) {
2416 if (node->start <= address && address < node->end)
2423 static LIST_HEAD(kprobe_blacklist);
2425 static void kprobe_blacklist__init(void)
2427 if (!list_empty(&kprobe_blacklist))
2430 if (kprobe_blacklist__load(&kprobe_blacklist) < 0)
2431 pr_debug("No kprobe blacklist support, ignored\n");
2434 static void kprobe_blacklist__release(void)
2436 kprobe_blacklist__delete(&kprobe_blacklist);
2439 static bool kprobe_blacklist__listed(unsigned long address)
2441 return !!kprobe_blacklist__find_by_address(&kprobe_blacklist, address);
2444 static int perf_probe_event__sprintf(const char *group, const char *event,
2445 struct perf_probe_event *pev,
2447 struct strbuf *result)
2452 if (asprintf(&buf, "%s:%s", group, event) < 0)
2454 ret = strbuf_addf(result, " %-20s (on ", buf);
2459 /* Synthesize only event probe point */
2460 buf = synthesize_perf_probe_point(&pev->point);
2463 ret = strbuf_addstr(result, buf);
2467 ret = strbuf_addf(result, " in %s", module);
2469 if (!ret && pev->nargs > 0) {
2470 ret = strbuf_add(result, " with", 5);
2471 for (i = 0; !ret && i < pev->nargs; i++) {
2472 buf = synthesize_perf_probe_arg(&pev->args[i]);
2475 ret = strbuf_addf(result, " %s", buf);
2480 ret = strbuf_addch(result, ')');
2486 int show_perf_probe_event(const char *group, const char *event,
2487 struct perf_probe_event *pev,
2488 const char *module, bool use_stdout)
2490 struct strbuf buf = STRBUF_INIT;
2493 ret = perf_probe_event__sprintf(group, event, pev, module, &buf);
2496 printf("%s\n", buf.buf);
2498 pr_info("%s\n", buf.buf);
2500 strbuf_release(&buf);
2505 static bool filter_probe_trace_event(struct probe_trace_event *tev,
2506 struct strfilter *filter)
2510 /* At first, check the event name itself */
2511 if (strfilter__compare(filter, tev->event))
2514 /* Next, check the combination of name and group */
2515 if (e_snprintf(tmp, 128, "%s:%s", tev->group, tev->event) < 0)
2517 return strfilter__compare(filter, tmp);
2520 static int __show_perf_probe_events(int fd, bool is_kprobe,
2521 struct strfilter *filter)
2524 struct probe_trace_event tev;
2525 struct perf_probe_event pev;
2526 struct strlist *rawlist;
2527 struct str_node *ent;
2529 memset(&tev, 0, sizeof(tev));
2530 memset(&pev, 0, sizeof(pev));
2532 rawlist = probe_file__get_rawlist(fd);
2536 strlist__for_each_entry(ent, rawlist) {
2537 ret = parse_probe_trace_command(ent->s, &tev);
2539 if (!filter_probe_trace_event(&tev, filter))
2541 ret = convert_to_perf_probe_event(&tev, &pev,
2545 ret = show_perf_probe_event(pev.group, pev.event,
2546 &pev, tev.point.module,
2550 clear_perf_probe_event(&pev);
2551 clear_probe_trace_event(&tev);
2555 strlist__delete(rawlist);
2556 /* Cleanup cached debuginfo if needed */
2557 debuginfo_cache__exit();
2562 /* List up current perf-probe events */
2563 int show_perf_probe_events(struct strfilter *filter)
2565 int kp_fd, up_fd, ret;
2569 if (probe_conf.cache)
2570 return probe_cache__show_all_caches(filter);
2572 ret = init_probe_symbol_maps(false);
2576 ret = probe_file__open_both(&kp_fd, &up_fd, 0);
2581 ret = __show_perf_probe_events(kp_fd, true, filter);
2582 if (up_fd >= 0 && ret >= 0)
2583 ret = __show_perf_probe_events(up_fd, false, filter);
2588 exit_probe_symbol_maps();
2593 static int get_new_event_name(char *buf, size_t len, const char *base,
2594 struct strlist *namelist, bool ret_event,
2602 nbase = strdup(base);
2606 /* Cut off the dot suffixes (e.g. .const, .isra) and version suffixes */
2607 p = strpbrk(nbase, ".@");
2608 if (p && p != nbase)
2611 /* Try no suffix number */
2612 ret = e_snprintf(buf, len, "%s%s", nbase, ret_event ? "__return" : "");
2614 pr_debug("snprintf() failed: %d\n", ret);
2617 if (!strlist__has_entry(namelist, buf))
2620 if (!allow_suffix) {
2621 pr_warning("Error: event \"%s\" already exists.\n"
2622 " Hint: Remove existing event by 'perf probe -d'\n"
2623 " or force duplicates by 'perf probe -f'\n"
2624 " or set 'force=yes' in BPF source.\n",
2630 /* Try to add suffix */
2631 for (i = 1; i < MAX_EVENT_INDEX; i++) {
2632 ret = e_snprintf(buf, len, "%s_%d", nbase, i);
2634 pr_debug("snprintf() failed: %d\n", ret);
2637 if (!strlist__has_entry(namelist, buf))
2640 if (i == MAX_EVENT_INDEX) {
2641 pr_warning("Too many events are on the same function.\n");
2648 /* Final validation */
2649 if (ret >= 0 && !is_c_func_name(buf)) {
2650 pr_warning("Internal error: \"%s\" is an invalid event name.\n",
2658 /* Warn if the current kernel's uprobe implementation is old */
2659 static void warn_uprobe_event_compat(struct probe_trace_event *tev)
2662 char *buf = synthesize_probe_trace_command(tev);
2663 struct probe_trace_point *tp = &tev->point;
2665 if (tp->ref_ctr_offset && !uprobe_ref_ctr_is_supported()) {
2666 pr_warning("A semaphore is associated with %s:%s and "
2667 "seems your kernel doesn't support it.\n",
2668 tev->group, tev->event);
2671 /* Old uprobe event doesn't support memory dereference */
2672 if (!tev->uprobes || tev->nargs == 0 || !buf)
2675 for (i = 0; i < tev->nargs; i++)
2676 if (strglobmatch(tev->args[i].value, "[$@+-]*")) {
2677 pr_warning("Please upgrade your kernel to at least "
2678 "3.14 to have access to feature %s\n",
2679 tev->args[i].value);
2686 /* Set new name from original perf_probe_event and namelist */
2687 static int probe_trace_event__set_name(struct probe_trace_event *tev,
2688 struct perf_probe_event *pev,
2689 struct strlist *namelist,
2692 const char *event, *group;
2696 /* If probe_event or trace_event already have the name, reuse it */
2697 if (pev->event && !pev->sdt)
2699 else if (tev->event)
2702 /* Or generate new one from probe point */
2703 if (pev->point.function &&
2704 (strncmp(pev->point.function, "0x", 2) != 0) &&
2705 !strisglob(pev->point.function))
2706 event = pev->point.function;
2708 event = tev->point.realname;
2710 if (pev->group && !pev->sdt)
2712 else if (tev->group)
2715 group = PERFPROBE_GROUP;
2717 /* Get an unused new event name */
2718 ret = get_new_event_name(buf, 64, event, namelist,
2719 tev->point.retprobe, allow_suffix);
2725 tev->event = strdup(event);
2726 tev->group = strdup(group);
2727 if (tev->event == NULL || tev->group == NULL)
2730 /* Add added event name to namelist */
2731 strlist__add(namelist, event);
2735 static int __open_probe_file_and_namelist(bool uprobe,
2736 struct strlist **namelist)
2740 fd = probe_file__open(PF_FL_RW | (uprobe ? PF_FL_UPROBE : 0));
2744 /* Get current event names */
2745 *namelist = probe_file__get_namelist(fd);
2747 pr_debug("Failed to get current event list.\n");
2754 static int __add_probe_trace_events(struct perf_probe_event *pev,
2755 struct probe_trace_event *tevs,
2756 int ntevs, bool allow_suffix)
2758 int i, fd[2] = {-1, -1}, up, ret;
2759 struct probe_trace_event *tev = NULL;
2760 struct probe_cache *cache = NULL;
2761 struct strlist *namelist[2] = {NULL, NULL};
2762 struct nscookie nsc;
2764 up = pev->uprobes ? 1 : 0;
2765 fd[up] = __open_probe_file_and_namelist(up, &namelist[up]);
2770 for (i = 0; i < ntevs; i++) {
2772 up = tev->uprobes ? 1 : 0;
2773 if (fd[up] == -1) { /* Open the kprobe/uprobe_events */
2774 fd[up] = __open_probe_file_and_namelist(up,
2779 /* Skip if the symbol is out of .text or blacklisted */
2780 if (!tev->point.symbol && !pev->uprobes)
2783 /* Set new name for tev (and update namelist) */
2784 ret = probe_trace_event__set_name(tev, pev, namelist[up],
2789 nsinfo__mountns_enter(pev->nsi, &nsc);
2790 ret = probe_file__add_event(fd[up], tev);
2791 nsinfo__mountns_exit(&nsc);
2796 * Probes after the first probe which comes from same
2797 * user input are always allowed to add suffix, because
2798 * there might be several addresses corresponding to
2801 allow_suffix = true;
2803 if (ret == -EINVAL && pev->uprobes)
2804 warn_uprobe_event_compat(tev);
2805 if (ret == 0 && probe_conf.cache) {
2806 cache = probe_cache__new(pev->target, pev->nsi);
2808 probe_cache__add_entry(cache, pev, tevs, ntevs) < 0 ||
2809 probe_cache__commit(cache) < 0)
2810 pr_warning("Failed to add event to probe cache\n");
2811 probe_cache__delete(cache);
2815 for (up = 0; up < 2; up++) {
2816 strlist__delete(namelist[up]);
2823 static int find_probe_functions(struct map *map, char *name,
2824 struct symbol **syms)
2828 struct rb_node *tmp;
2829 const char *norm, *ver;
2831 bool cut_version = true;
2833 if (map__load(map) < 0)
2836 /* If user gives a version, don't cut off the version from symbols */
2837 if (strchr(name, '@'))
2838 cut_version = false;
2840 map__for_each_symbol(map, sym, tmp) {
2841 norm = arch__normalize_symbol_name(sym->name);
2846 /* We don't care about default symbol or not */
2847 ver = strchr(norm, '@');
2849 buf = strndup(norm, ver - norm);
2856 if (strglobmatch(norm, name)) {
2858 if (syms && found < probe_conf.max_probes)
2859 syms[found - 1] = sym;
2868 void __weak arch__fix_tev_from_maps(struct perf_probe_event *pev __maybe_unused,
2869 struct probe_trace_event *tev __maybe_unused,
2870 struct map *map __maybe_unused,
2871 struct symbol *sym __maybe_unused) { }
2874 * Find probe function addresses from map.
2875 * Return an error or the number of found probe_trace_event
2877 static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
2878 struct probe_trace_event **tevs)
2880 struct map *map = NULL;
2881 struct ref_reloc_sym *reloc_sym = NULL;
2883 struct symbol **syms = NULL;
2884 struct probe_trace_event *tev;
2885 struct perf_probe_point *pp = &pev->point;
2886 struct probe_trace_point *tp;
2887 int num_matched_functions;
2888 int ret, i, j, skipped = 0;
2891 map = get_target_map(pev->target, pev->nsi, pev->uprobes);
2897 syms = malloc(sizeof(struct symbol *) * probe_conf.max_probes);
2904 * Load matched symbols: Since the different local symbols may have
2905 * same name but different addresses, this lists all the symbols.
2907 num_matched_functions = find_probe_functions(map, pp->function, syms);
2908 if (num_matched_functions <= 0) {
2909 pr_err("Failed to find symbol %s in %s\n", pp->function,
2910 pev->target ? : "kernel");
2913 } else if (num_matched_functions > probe_conf.max_probes) {
2914 pr_err("Too many functions matched in %s\n",
2915 pev->target ? : "kernel");
2920 /* Note that the symbols in the kmodule are not relocated */
2921 if (!pev->uprobes && !pev->target &&
2922 (!pp->retprobe || kretprobe_offset_is_supported())) {
2923 reloc_sym = kernel_get_ref_reloc_sym();
2925 pr_warning("Relocated base symbol is not found!\n");
2931 /* Setup result trace-probe-events */
2932 *tevs = zalloc(sizeof(*tev) * num_matched_functions);
2940 for (j = 0; j < num_matched_functions; j++) {
2943 tev = (*tevs) + ret;
2945 if (ret == num_matched_functions) {
2946 pr_warning("Too many symbols are listed. Skip it.\n");
2951 if (pp->offset > sym->end - sym->start) {
2952 pr_warning("Offset %ld is bigger than the size of %s\n",
2953 pp->offset, sym->name);
2957 /* Add one probe point */
2958 tp->address = map->unmap_ip(map, sym->start) + pp->offset;
2960 /* Check the kprobe (not in module) is within .text */
2961 if (!pev->uprobes && !pev->target &&
2962 kprobe_warn_out_range(sym->name, tp->address)) {
2963 tp->symbol = NULL; /* Skip it */
2965 } else if (reloc_sym) {
2966 tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
2967 tp->offset = tp->address - reloc_sym->addr;
2969 tp->symbol = strdup_or_goto(sym->name, nomem_out);
2970 tp->offset = pp->offset;
2972 tp->realname = strdup_or_goto(sym->name, nomem_out);
2974 tp->retprobe = pp->retprobe;
2977 tev->point.module = strdup_or_goto(pev->target,
2980 mod_name = find_module_name(pev->target);
2982 strdup(mod_name ? mod_name : pev->target);
2984 if (!tev->point.module)
2988 tev->uprobes = pev->uprobes;
2989 tev->nargs = pev->nargs;
2991 tev->args = zalloc(sizeof(struct probe_trace_arg) *
2993 if (tev->args == NULL)
2996 for (i = 0; i < tev->nargs; i++) {
2997 if (pev->args[i].name)
2999 strdup_or_goto(pev->args[i].name,
3002 tev->args[i].value = strdup_or_goto(pev->args[i].var,
3004 if (pev->args[i].type)
3006 strdup_or_goto(pev->args[i].type,
3009 arch__fix_tev_from_maps(pev, tev, map, sym);
3011 if (ret == skipped) {
3024 clear_probe_trace_events(*tevs, num_matched_functions);
3029 static int try_to_find_absolute_address(struct perf_probe_event *pev,
3030 struct probe_trace_event **tevs)
3032 struct perf_probe_point *pp = &pev->point;
3033 struct probe_trace_event *tev;
3034 struct probe_trace_point *tp;
3037 if (!(pev->point.function && !strncmp(pev->point.function, "0x", 2)))
3039 if (perf_probe_event_need_dwarf(pev))
3043 * This is 'perf probe /lib/libc.so 0xabcd'. Try to probe at
3046 * Only one tev can be generated by this.
3048 *tevs = zalloc(sizeof(*tev));
3056 * Don't use tp->offset, use address directly, because
3057 * in synthesize_probe_trace_command() address cannot be
3060 tp->address = pev->point.abs_address;
3061 tp->retprobe = pp->retprobe;
3062 tev->uprobes = pev->uprobes;
3066 * Give it a '0x' leading symbol name.
3067 * In __add_probe_trace_events, a NULL symbol is interpreted as
3070 if (asprintf(&tp->symbol, "0x%lx", tp->address) < 0)
3073 /* For kprobe, check range */
3074 if ((!tev->uprobes) &&
3075 (kprobe_warn_out_range(tev->point.symbol,
3076 tev->point.address))) {
3081 if (asprintf(&tp->realname, "abs_%lx", tp->address) < 0)
3085 tp->module = strdup(pev->target);
3091 tev->group = strdup(pev->group);
3097 tev->event = strdup(pev->event);
3102 tev->nargs = pev->nargs;
3103 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
3107 for (i = 0; i < tev->nargs; i++)
3108 copy_to_probe_trace_arg(&tev->args[i], &pev->args[i]);
3113 clear_probe_trace_events(*tevs, 1);
3118 /* Concatinate two arrays */
3119 static void *memcat(void *a, size_t sz_a, void *b, size_t sz_b)
3123 ret = malloc(sz_a + sz_b);
3125 memcpy(ret, a, sz_a);
3126 memcpy(ret + sz_a, b, sz_b);
3132 concat_probe_trace_events(struct probe_trace_event **tevs, int *ntevs,
3133 struct probe_trace_event **tevs2, int ntevs2)
3135 struct probe_trace_event *new_tevs;
3145 if (*ntevs + ntevs2 > probe_conf.max_probes)
3148 /* Concatinate the array of probe_trace_event */
3149 new_tevs = memcat(*tevs, (*ntevs) * sizeof(**tevs),
3150 *tevs2, ntevs2 * sizeof(**tevs2));
3160 clear_probe_trace_events(*tevs2, ntevs2);
3167 * Try to find probe_trace_event from given probe caches. Return the number
3168 * of cached events found, if an error occurs return the error.
3170 static int find_cached_events(struct perf_probe_event *pev,
3171 struct probe_trace_event **tevs,
3174 struct probe_cache *cache;
3175 struct probe_cache_entry *entry;
3176 struct probe_trace_event *tmp_tevs = NULL;
3180 cache = probe_cache__new(target, pev->nsi);
3181 /* Return 0 ("not found") if the target has no probe cache. */
3185 for_each_probe_cache_entry(entry, cache) {
3186 /* Skip the cache entry which has no name */
3187 if (!entry->pev.event || !entry->pev.group)
3189 if ((!pev->group || strglobmatch(entry->pev.group, pev->group)) &&
3190 strglobmatch(entry->pev.event, pev->event)) {
3191 ret = probe_cache_entry__get_event(entry, &tmp_tevs);
3193 ret = concat_probe_trace_events(tevs, &ntevs,
3199 probe_cache__delete(cache);
3201 clear_probe_trace_events(*tevs, ntevs);
3205 if (ntevs > 0 && target && target[0] == '/')
3206 pev->uprobes = true;
3212 /* Try to find probe_trace_event from all probe caches */
3213 static int find_cached_events_all(struct perf_probe_event *pev,
3214 struct probe_trace_event **tevs)
3216 struct probe_trace_event *tmp_tevs = NULL;
3217 struct strlist *bidlist;
3218 struct str_node *nd;
3223 /* Get the buildid list of all valid caches */
3224 bidlist = build_id_cache__list_all(true);
3227 pr_debug("Failed to get buildids: %d\n", ret);
3232 strlist__for_each_entry(nd, bidlist) {
3233 pathname = build_id_cache__origname(nd->s);
3234 ret = find_cached_events(pev, &tmp_tevs, pathname);
3235 /* In the case of cnt == 0, we just skip it */
3237 ret = concat_probe_trace_events(tevs, &ntevs,
3243 strlist__delete(bidlist);
3246 clear_probe_trace_events(*tevs, ntevs);
3254 static int find_probe_trace_events_from_cache(struct perf_probe_event *pev,
3255 struct probe_trace_event **tevs)
3257 struct probe_cache *cache;
3258 struct probe_cache_entry *entry;
3259 struct probe_trace_event *tev;
3260 struct str_node *node;
3264 /* For SDT/cached events, we use special search functions */
3266 return find_cached_events_all(pev, tevs);
3268 return find_cached_events(pev, tevs, pev->target);
3270 cache = probe_cache__new(pev->target, pev->nsi);
3274 entry = probe_cache__find(cache, pev);
3276 /* SDT must be in the cache */
3277 ret = pev->sdt ? -ENOENT : 0;
3281 ret = strlist__nr_entries(entry->tevlist);
3282 if (ret > probe_conf.max_probes) {
3283 pr_debug("Too many entries matched in the cache of %s\n",
3284 pev->target ? : "kernel");
3289 *tevs = zalloc(ret * sizeof(*tev));
3296 strlist__for_each_entry(node, entry->tevlist) {
3297 tev = &(*tevs)[i++];
3298 ret = parse_probe_trace_command(node->s, tev);
3301 /* Set the uprobes attribute as same as original */
3302 tev->uprobes = pev->uprobes;
3307 probe_cache__delete(cache);
3311 static int convert_to_probe_trace_events(struct perf_probe_event *pev,
3312 struct probe_trace_event **tevs)
3316 if (!pev->group && !pev->sdt) {
3317 /* Set group name if not given */
3318 if (!pev->uprobes) {
3319 pev->group = strdup(PERFPROBE_GROUP);
3320 ret = pev->group ? 0 : -ENOMEM;
3322 ret = convert_exec_to_group(pev->target, &pev->group);
3324 pr_warning("Failed to make a group name.\n");
3329 ret = try_to_find_absolute_address(pev, tevs);
3333 /* At first, we need to lookup cache entry */
3334 ret = find_probe_trace_events_from_cache(pev, tevs);
3335 if (ret > 0 || pev->sdt) /* SDT can be found only in the cache */
3336 return ret == 0 ? -ENOENT : ret; /* Found in probe cache */
3338 /* Convert perf_probe_event with debuginfo */
3339 ret = try_to_find_probe_trace_events(pev, tevs);
3341 return ret; /* Found in debuginfo or got an error */
3343 return find_probe_trace_events_from_map(pev, tevs);
3346 int convert_perf_probe_events(struct perf_probe_event *pevs, int npevs)
3350 /* Loop 1: convert all events */
3351 for (i = 0; i < npevs; i++) {
3352 /* Init kprobe blacklist if needed */
3353 if (!pevs[i].uprobes)
3354 kprobe_blacklist__init();
3355 /* Convert with or without debuginfo */
3356 ret = convert_to_probe_trace_events(&pevs[i], &pevs[i].tevs);
3359 pevs[i].ntevs = ret;
3361 /* This just release blacklist only if allocated */
3362 kprobe_blacklist__release();
3367 static int show_probe_trace_event(struct probe_trace_event *tev)
3369 char *buf = synthesize_probe_trace_command(tev);
3372 pr_debug("Failed to synthesize probe trace event.\n");
3376 /* Showing definition always go stdout */
3377 printf("%s\n", buf);
3383 int show_probe_trace_events(struct perf_probe_event *pevs, int npevs)
3385 struct strlist *namelist = strlist__new(NULL, NULL);
3386 struct probe_trace_event *tev;
3387 struct perf_probe_event *pev;
3393 for (j = 0; j < npevs && !ret; j++) {
3395 for (i = 0; i < pev->ntevs && !ret; i++) {
3396 tev = &pev->tevs[i];
3397 /* Skip if the symbol is out of .text or blacklisted */
3398 if (!tev->point.symbol && !pev->uprobes)
3401 /* Set new name for tev (and update namelist) */
3402 ret = probe_trace_event__set_name(tev, pev,
3405 ret = show_probe_trace_event(tev);
3408 strlist__delete(namelist);
3413 int apply_perf_probe_events(struct perf_probe_event *pevs, int npevs)
3417 /* Loop 2: add all events */
3418 for (i = 0; i < npevs; i++) {
3419 ret = __add_probe_trace_events(&pevs[i], pevs[i].tevs,
3421 probe_conf.force_add);
3428 void cleanup_perf_probe_events(struct perf_probe_event *pevs, int npevs)
3431 struct perf_probe_event *pev;
3433 /* Loop 3: cleanup and free trace events */
3434 for (i = 0; i < npevs; i++) {
3436 for (j = 0; j < pevs[i].ntevs; j++)
3437 clear_probe_trace_event(&pevs[i].tevs[j]);
3438 zfree(&pevs[i].tevs);
3440 nsinfo__zput(pev->nsi);
3441 clear_perf_probe_event(&pevs[i]);
3445 int add_perf_probe_events(struct perf_probe_event *pevs, int npevs)
3449 ret = init_probe_symbol_maps(pevs->uprobes);
3453 ret = convert_perf_probe_events(pevs, npevs);
3455 ret = apply_perf_probe_events(pevs, npevs);
3457 cleanup_perf_probe_events(pevs, npevs);
3459 exit_probe_symbol_maps();
3463 int del_perf_probe_events(struct strfilter *filter)
3465 int ret, ret2, ufd = -1, kfd = -1;
3466 char *str = strfilter__string(filter);
3471 /* Get current event names */
3472 ret = probe_file__open_both(&kfd, &ufd, PF_FL_RW);
3476 ret = probe_file__del_events(kfd, filter);
3477 if (ret < 0 && ret != -ENOENT)
3480 ret2 = probe_file__del_events(ufd, filter);
3481 if (ret2 < 0 && ret2 != -ENOENT) {
3498 int show_available_funcs(const char *target, struct nsinfo *nsi,
3499 struct strfilter *_filter, bool user)
3505 ret = init_probe_symbol_maps(user);
3509 /* Get a symbol map */
3510 map = get_target_map(target, nsi, user);
3512 pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
3516 ret = map__load(map);
3519 char *str = strfilter__string(_filter);
3520 pr_err("Failed to find symbols matched to \"%s\"\n",
3524 pr_err("Failed to load symbols in %s\n",
3525 (target) ? : "kernel");
3528 if (!dso__sorted_by_name(map->dso))
3529 dso__sort_by_name(map->dso);
3531 /* Show all (filtered) symbols */
3534 for (nd = rb_first_cached(&map->dso->symbol_names); nd;
3536 struct symbol_name_rb_node *pos = rb_entry(nd, struct symbol_name_rb_node, rb_node);
3538 if (strfilter__compare(_filter, pos->sym.name))
3539 printf("%s\n", pos->sym.name);
3543 exit_probe_symbol_maps();
3548 int copy_to_probe_trace_arg(struct probe_trace_arg *tvar,
3549 struct perf_probe_arg *pvar)
3551 tvar->value = strdup(pvar->var);
3552 if (tvar->value == NULL)
3555 tvar->type = strdup(pvar->type);
3556 if (tvar->type == NULL)
3560 tvar->name = strdup(pvar->name);
3561 if (tvar->name == NULL)