2 * probe-event.c : perf-probe definition to probe_events format converter
4 * Written by Masami Hiramatsu <mhiramat@redhat.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #include <sys/utsname.h>
24 #include <sys/types.h>
44 #include <api/fs/fs.h>
45 #include "trace-event.h" /* For __maybe_unused */
46 #include "probe-event.h"
47 #include "probe-finder.h"
48 #include "probe-file.h"
52 #include "sane_ctype.h"
54 #define PERFPROBE_GROUP "probe"
56 bool probe_event_dry_run; /* Dry run flag */
57 struct probe_conf probe_conf;
59 #define semantic_error(msg ...) pr_err("Semantic error :" msg)
61 int e_snprintf(char *str, size_t size, const char *format, ...)
66 ret = vsnprintf(str, size, format, ap);
73 static struct machine *host_machine;
75 /* Initialize symbol maps and path of vmlinux/modules */
76 int init_probe_symbol_maps(bool user_only)
80 symbol_conf.sort_by_name = true;
81 symbol_conf.allow_aliases = true;
82 ret = symbol__init(NULL);
84 pr_debug("Failed to init symbol map.\n");
88 if (host_machine || user_only) /* already initialized */
91 if (symbol_conf.vmlinux_name)
92 pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
94 host_machine = machine__new_host();
96 pr_debug("machine__new_host() failed.\n");
102 pr_warning("Failed to init vmlinux path.\n");
106 void exit_probe_symbol_maps(void)
108 machine__delete(host_machine);
113 static struct symbol *__find_kernel_function_by_name(const char *name,
116 return machine__find_kernel_function_by_name(host_machine, name, mapp);
119 static struct symbol *__find_kernel_function(u64 addr, struct map **mapp)
121 return machine__find_kernel_function(host_machine, addr, mapp);
124 static struct ref_reloc_sym *kernel_get_ref_reloc_sym(void)
126 /* kmap->ref_reloc_sym should be set if host_machine is initialized */
128 struct map *map = machine__kernel_map(host_machine);
130 if (map__load(map) < 0)
133 kmap = map__kmap(map);
136 return kmap->ref_reloc_sym;
139 static int kernel_get_symbol_address_by_name(const char *name, u64 *addr,
140 bool reloc, bool reladdr)
142 struct ref_reloc_sym *reloc_sym;
146 /* ref_reloc_sym is just a label. Need a special fix*/
147 reloc_sym = kernel_get_ref_reloc_sym();
148 if (reloc_sym && strcmp(name, reloc_sym->name) == 0)
149 *addr = (reloc) ? reloc_sym->addr : reloc_sym->unrelocated_addr;
151 sym = __find_kernel_function_by_name(name, &map);
154 *addr = map->unmap_ip(map, sym->start) -
155 ((reloc) ? 0 : map->reloc) -
156 ((reladdr) ? map->start : 0);
161 static struct map *kernel_get_module_map(const char *module)
163 struct map_groups *grp = &host_machine->kmaps;
164 struct maps *maps = &grp->maps[MAP__FUNCTION];
167 /* A file path -- this is an offline module */
168 if (module && strchr(module, '/'))
169 return dso__new_map(module);
174 for (pos = maps__first(maps); pos; pos = map__next(pos)) {
175 /* short_name is "[module]" */
176 if (strncmp(pos->dso->short_name + 1, module,
177 pos->dso->short_name_len - 2) == 0 &&
178 module[pos->dso->short_name_len - 2] == '\0') {
186 struct map *get_target_map(const char *target, bool user)
188 /* Init maps of given executable or kernel */
190 return dso__new_map(target);
192 return kernel_get_module_map(target);
195 static int convert_exec_to_group(const char *exec, char **result)
197 char *ptr1, *ptr2, *exec_copy;
201 exec_copy = strdup(exec);
205 ptr1 = basename(exec_copy);
211 for (ptr2 = ptr1; *ptr2 != '\0'; ptr2++) {
212 if (!isalnum(*ptr2) && *ptr2 != '_') {
218 ret = e_snprintf(buf, 64, "%s_%s", PERFPROBE_GROUP, ptr1);
222 *result = strdup(buf);
223 ret = *result ? 0 : -ENOMEM;
230 static void clear_perf_probe_point(struct perf_probe_point *pp)
237 static void clear_probe_trace_events(struct probe_trace_event *tevs, int ntevs)
241 for (i = 0; i < ntevs; i++)
242 clear_probe_trace_event(tevs + i);
245 static bool kprobe_blacklist__listed(unsigned long address);
246 static bool kprobe_warn_out_range(const char *symbol, unsigned long address)
251 /* Get the address of _etext for checking non-probable text symbol */
252 ret = kernel_get_symbol_address_by_name("_etext", &etext_addr,
255 if (ret == 0 && etext_addr < address)
256 pr_warning("%s is out of .text, skip it.\n", symbol);
257 else if (kprobe_blacklist__listed(address))
258 pr_warning("%s is blacklisted function, skip it.\n", symbol);
266 * @module can be module name of module file path. In case of path,
267 * inspect elf and find out what is actual module name.
268 * Caller has to free mod_name after using it.
270 static char *find_module_name(const char *module)
278 char *mod_name = NULL;
281 fd = open(module, O_RDONLY);
285 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
289 if (gelf_getehdr(elf, &ehdr) == NULL)
292 sec = elf_section_by_name(elf, &ehdr, &shdr,
293 ".gnu.linkonce.this_module", NULL);
297 data = elf_getdata(sec, NULL);
298 if (!data || !data->d_buf)
303 * '.gnu.linkonce.this_module' section of kernel module elf directly
304 * maps to 'struct module' from linux/module.h. This section contains
305 * actual module name which will be used by kernel after loading it.
306 * But, we cannot use 'struct module' here since linux/module.h is not
307 * exposed to user-space. Offset of 'name' has remained same from long
308 * time, so hardcoding it here.
310 if (ehdr.e_ident[EI_CLASS] == ELFCLASS32)
312 else /* expect ELFCLASS64 by default */
315 mod_name = strdup((char *)data->d_buf + name_offset);
324 #ifdef HAVE_DWARF_SUPPORT
326 static int kernel_get_module_dso(const char *module, struct dso **pdso)
330 const char *vmlinux_name;
334 char module_name[128];
336 snprintf(module_name, sizeof(module_name), "[%s]", module);
337 map = map_groups__find_by_name(&host_machine->kmaps, MAP__FUNCTION, module_name);
342 pr_debug("Failed to find module %s.\n", module);
346 map = machine__kernel_map(host_machine);
349 vmlinux_name = symbol_conf.vmlinux_name;
352 ret = dso__load_vmlinux(dso, map, vmlinux_name, false);
354 ret = dso__load_vmlinux_path(dso, map);
361 * Some binaries like glibc have special symbols which are on the symbol
362 * table, but not in the debuginfo. If we can find the address of the
363 * symbol from map, we can translate the address back to the probe point.
365 static int find_alternative_probe_point(struct debuginfo *dinfo,
366 struct perf_probe_point *pp,
367 struct perf_probe_point *result,
368 const char *target, bool uprobes)
370 struct map *map = NULL;
375 /* This can work only for function-name based one */
376 if (!pp->function || pp->file)
379 map = get_target_map(target, uprobes);
383 /* Find the address of given function */
384 map__for_each_symbol_by_name(map, pp->function, sym) {
386 address = sym->start;
388 address = map->unmap_ip(map, sym->start) - map->reloc;
395 pr_debug("Symbol %s address found : %" PRIx64 "\n",
396 pp->function, address);
398 ret = debuginfo__find_probe_point(dinfo, (unsigned long)address,
401 ret = (!ret) ? -ENOENT : ret;
403 result->offset += pp->offset;
404 result->line += pp->line;
405 result->retprobe = pp->retprobe;
415 static int get_alternative_probe_event(struct debuginfo *dinfo,
416 struct perf_probe_event *pev,
417 struct perf_probe_point *tmp)
421 memcpy(tmp, &pev->point, sizeof(*tmp));
422 memset(&pev->point, 0, sizeof(pev->point));
423 ret = find_alternative_probe_point(dinfo, tmp, &pev->point,
424 pev->target, pev->uprobes);
426 memcpy(&pev->point, tmp, sizeof(*tmp));
431 static int get_alternative_line_range(struct debuginfo *dinfo,
432 struct line_range *lr,
433 const char *target, bool user)
435 struct perf_probe_point pp = { .function = lr->function,
438 struct perf_probe_point result;
441 memset(&result, 0, sizeof(result));
443 if (lr->end != INT_MAX)
444 len = lr->end - lr->start;
445 ret = find_alternative_probe_point(dinfo, &pp, &result,
448 lr->function = result.function;
449 lr->file = result.file;
450 lr->start = result.line;
451 if (lr->end != INT_MAX)
452 lr->end = lr->start + len;
453 clear_perf_probe_point(&pp);
458 /* Open new debuginfo of given module */
459 static struct debuginfo *open_debuginfo(const char *module, bool silent)
461 const char *path = module;
462 char reason[STRERR_BUFSIZE];
463 struct debuginfo *ret = NULL;
464 struct dso *dso = NULL;
467 if (!module || !strchr(module, '/')) {
468 err = kernel_get_module_dso(module, &dso);
470 if (!dso || dso->load_errno == 0) {
471 if (!str_error_r(-err, reason, STRERR_BUFSIZE))
472 strcpy(reason, "(unknown)");
474 dso__strerror_load(dso, reason, STRERR_BUFSIZE);
476 pr_err("Failed to find the path for %s: %s\n",
477 module ?: "kernel", reason);
480 path = dso->long_name;
482 ret = debuginfo__new(path);
483 if (!ret && !silent) {
484 pr_warning("The %s file has no debug information.\n", path);
485 if (!module || !strtailcmp(path, ".ko"))
486 pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, ");
488 pr_warning("Rebuild with -g, ");
489 pr_warning("or install an appropriate debuginfo package.\n");
494 /* For caching the last debuginfo */
495 static struct debuginfo *debuginfo_cache;
496 static char *debuginfo_cache_path;
498 static struct debuginfo *debuginfo_cache__open(const char *module, bool silent)
500 const char *path = module;
502 /* If the module is NULL, it should be the kernel. */
506 if (debuginfo_cache_path && !strcmp(debuginfo_cache_path, path))
509 /* Copy module path */
510 free(debuginfo_cache_path);
511 debuginfo_cache_path = strdup(path);
512 if (!debuginfo_cache_path) {
513 debuginfo__delete(debuginfo_cache);
514 debuginfo_cache = NULL;
518 debuginfo_cache = open_debuginfo(module, silent);
519 if (!debuginfo_cache)
520 zfree(&debuginfo_cache_path);
522 return debuginfo_cache;
525 static void debuginfo_cache__exit(void)
527 debuginfo__delete(debuginfo_cache);
528 debuginfo_cache = NULL;
529 zfree(&debuginfo_cache_path);
533 static int get_text_start_address(const char *exec, unsigned long *address)
538 int fd, ret = -ENOENT;
540 fd = open(exec, O_RDONLY);
544 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
550 if (gelf_getehdr(elf, &ehdr) == NULL)
553 if (!elf_section_by_name(elf, &ehdr, &shdr, ".text", NULL))
556 *address = shdr.sh_addr - shdr.sh_offset;
567 * Convert trace point to probe point with debuginfo
569 static int find_perf_probe_point_from_dwarf(struct probe_trace_point *tp,
570 struct perf_probe_point *pp,
573 struct debuginfo *dinfo = NULL;
574 unsigned long stext = 0;
575 u64 addr = tp->address;
578 /* convert the address to dwarf address */
584 ret = get_text_start_address(tp->module, &stext);
588 } else if (tp->symbol) {
589 /* If the module is given, this returns relative address */
590 ret = kernel_get_symbol_address_by_name(tp->symbol, &addr,
591 false, !!tp->module);
597 pr_debug("try to find information at %" PRIx64 " in %s\n", addr,
598 tp->module ? : "kernel");
600 dinfo = debuginfo_cache__open(tp->module, verbose <= 0);
602 ret = debuginfo__find_probe_point(dinfo,
603 (unsigned long)addr, pp);
608 pp->retprobe = tp->retprobe;
612 pr_debug("Failed to find corresponding probes from debuginfo.\n");
613 return ret ? : -ENOENT;
616 /* Adjust symbol name and address */
617 static int post_process_probe_trace_point(struct probe_trace_point *tp,
618 struct map *map, unsigned long offs)
621 u64 addr = tp->address + tp->offset - offs;
623 sym = map__find_symbol(map, addr);
627 if (strcmp(sym->name, tp->symbol)) {
628 /* If we have no realname, use symbol for it */
630 tp->realname = tp->symbol;
633 tp->symbol = strdup(sym->name);
637 tp->offset = addr - sym->start;
644 * Rename DWARF symbols to ELF symbols -- gcc sometimes optimizes functions
645 * and generate new symbols with suffixes such as .constprop.N or .isra.N
646 * etc. Since those symbols are not recorded in DWARF, we have to find
647 * correct generated symbols from offline ELF binary.
648 * For online kernel or uprobes we don't need this because those are
649 * rebased on _text, or already a section relative address.
652 post_process_offline_probe_trace_events(struct probe_trace_event *tevs,
653 int ntevs, const char *pathname)
656 unsigned long stext = 0;
659 /* Prepare a map for offline binary */
660 map = dso__new_map(pathname);
661 if (!map || get_text_start_address(pathname, &stext) < 0) {
662 pr_warning("Failed to get ELF symbols for %s\n", pathname);
666 for (i = 0; i < ntevs; i++) {
667 ret = post_process_probe_trace_point(&tevs[i].point,
677 static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs,
678 int ntevs, const char *exec)
681 unsigned long stext = 0;
686 ret = get_text_start_address(exec, &stext);
690 for (i = 0; i < ntevs && ret >= 0; i++) {
691 /* point.address is the addres of point.symbol + point.offset */
692 tevs[i].point.address -= stext;
693 tevs[i].point.module = strdup(exec);
694 if (!tevs[i].point.module) {
698 tevs[i].uprobes = true;
705 post_process_module_probe_trace_events(struct probe_trace_event *tevs,
706 int ntevs, const char *module,
707 struct debuginfo *dinfo)
709 Dwarf_Addr text_offs = 0;
711 char *mod_name = NULL;
717 map = get_target_map(module, false);
718 if (!map || debuginfo__get_text_offset(dinfo, &text_offs, true) < 0) {
719 pr_warning("Failed to get ELF symbols for %s\n", module);
723 mod_name = find_module_name(module);
724 for (i = 0; i < ntevs; i++) {
725 ret = post_process_probe_trace_point(&tevs[i].point,
726 map, (unsigned long)text_offs);
729 tevs[i].point.module =
730 strdup(mod_name ? mod_name : module);
731 if (!tevs[i].point.module) {
744 post_process_kernel_probe_trace_events(struct probe_trace_event *tevs,
747 struct ref_reloc_sym *reloc_sym;
751 /* Skip post process if the target is an offline kernel */
752 if (symbol_conf.ignore_vmlinux_buildid)
753 return post_process_offline_probe_trace_events(tevs, ntevs,
754 symbol_conf.vmlinux_name);
756 reloc_sym = kernel_get_ref_reloc_sym();
758 pr_warning("Relocated base symbol is not found!\n");
762 for (i = 0; i < ntevs; i++) {
763 if (!tevs[i].point.address)
765 if (tevs[i].point.retprobe && !kretprobe_offset_is_supported())
767 /* If we found a wrong one, mark it by NULL symbol */
768 if (kprobe_warn_out_range(tevs[i].point.symbol,
769 tevs[i].point.address)) {
773 tmp = strdup(reloc_sym->name);
777 /* If we have no realname, use symbol for it */
778 if (!tevs[i].point.realname)
779 tevs[i].point.realname = tevs[i].point.symbol;
781 free(tevs[i].point.symbol);
782 tevs[i].point.symbol = tmp;
783 tevs[i].point.offset = tevs[i].point.address -
784 reloc_sym->unrelocated_addr;
790 arch__post_process_probe_trace_events(struct perf_probe_event *pev __maybe_unused,
791 int ntevs __maybe_unused)
795 /* Post processing the probe events */
796 static int post_process_probe_trace_events(struct perf_probe_event *pev,
797 struct probe_trace_event *tevs,
798 int ntevs, const char *module,
799 bool uprobe, struct debuginfo *dinfo)
804 ret = add_exec_to_probe_trace_events(tevs, ntevs, module);
806 /* Currently ref_reloc_sym based probe is not for drivers */
807 ret = post_process_module_probe_trace_events(tevs, ntevs,
810 ret = post_process_kernel_probe_trace_events(tevs, ntevs);
813 arch__post_process_probe_trace_events(pev, ntevs);
818 /* Try to find perf_probe_event with debuginfo */
819 static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
820 struct probe_trace_event **tevs)
822 bool need_dwarf = perf_probe_event_need_dwarf(pev);
823 struct perf_probe_point tmp;
824 struct debuginfo *dinfo;
827 dinfo = open_debuginfo(pev->target, !need_dwarf);
831 pr_debug("Could not open debuginfo. Try to use symbols.\n");
835 pr_debug("Try to find probe point from debuginfo.\n");
836 /* Searching trace events corresponding to a probe event */
837 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
839 if (ntevs == 0) { /* Not found, retry with an alternative */
840 ret = get_alternative_probe_event(dinfo, pev, &tmp);
842 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
844 * Write back to the original probe_event for
845 * setting appropriate (user given) event name
847 clear_perf_probe_point(&pev->point);
848 memcpy(&pev->point, &tmp, sizeof(tmp));
852 if (ntevs > 0) { /* Succeeded to find trace events */
853 pr_debug("Found %d probe_trace_events.\n", ntevs);
854 ret = post_process_probe_trace_events(pev, *tevs, ntevs,
855 pev->target, pev->uprobes, dinfo);
856 if (ret < 0 || ret == ntevs) {
857 pr_debug("Post processing failed or all events are skipped. (%d)\n", ret);
858 clear_probe_trace_events(*tevs, ntevs);
864 debuginfo__delete(dinfo);
866 if (ntevs == 0) { /* No error but failed to find probe point. */
867 pr_warning("Probe point '%s' not found.\n",
868 synthesize_perf_probe_point(&pev->point));
870 } else if (ntevs < 0) {
871 /* Error path : ntevs < 0 */
872 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
874 pr_warning("Warning: No dwarf info found in the vmlinux - "
875 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
877 pr_debug("Trying to use symbols.\n");
884 #define LINEBUF_SIZE 256
885 #define NR_ADDITIONAL_LINES 2
887 static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
889 char buf[LINEBUF_SIZE], sbuf[STRERR_BUFSIZE];
890 const char *color = show_num ? "" : PERF_COLOR_BLUE;
891 const char *prefix = NULL;
894 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
899 prefix = show_num ? "%7d " : " ";
900 color_fprintf(stdout, color, prefix, l);
902 color_fprintf(stdout, color, "%s", buf);
904 } while (strchr(buf, '\n') == NULL);
909 pr_warning("File read error: %s\n",
910 str_error_r(errno, sbuf, sizeof(sbuf)));
916 static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
918 int rv = __show_one_line(fp, l, skip, show_num);
920 pr_warning("Source file is shorter than expected.\n");
926 #define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
927 #define show_one_line(f,l) _show_one_line(f,l,false,false)
928 #define skip_one_line(f,l) _show_one_line(f,l,true,false)
929 #define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
932 * Show line-range always requires debuginfo to find source file and
935 static int __show_line_range(struct line_range *lr, const char *module,
940 struct debuginfo *dinfo;
944 char sbuf[STRERR_BUFSIZE];
946 /* Search a line range */
947 dinfo = open_debuginfo(module, false);
951 ret = debuginfo__find_line_range(dinfo, lr);
952 if (!ret) { /* Not found, retry with an alternative */
953 ret = get_alternative_line_range(dinfo, lr, module, user);
955 ret = debuginfo__find_line_range(dinfo, lr);
957 debuginfo__delete(dinfo);
958 if (ret == 0 || ret == -ENOENT) {
959 pr_warning("Specified source line is not found.\n");
961 } else if (ret < 0) {
962 pr_warning("Debuginfo analysis failed.\n");
966 /* Convert source file path */
968 ret = get_real_path(tmp, lr->comp_dir, &lr->path);
970 /* Free old path when new path is assigned */
975 pr_warning("Failed to find source file path.\n");
982 fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
983 lr->start - lr->offset);
985 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
987 fp = fopen(lr->path, "r");
989 pr_warning("Failed to open %s: %s\n", lr->path,
990 str_error_r(errno, sbuf, sizeof(sbuf)));
993 /* Skip to starting line number */
994 while (l < lr->start) {
995 ret = skip_one_line(fp, l++);
1000 intlist__for_each_entry(ln, lr->line_list) {
1001 for (; ln->i > l; l++) {
1002 ret = show_one_line(fp, l - lr->offset);
1006 ret = show_one_line_with_num(fp, l++ - lr->offset);
1011 if (lr->end == INT_MAX)
1012 lr->end = l + NR_ADDITIONAL_LINES;
1013 while (l <= lr->end) {
1014 ret = show_one_line_or_eof(fp, l++ - lr->offset);
1023 int show_line_range(struct line_range *lr, const char *module, bool user)
1027 ret = init_probe_symbol_maps(user);
1030 ret = __show_line_range(lr, module, user);
1031 exit_probe_symbol_maps();
1036 static int show_available_vars_at(struct debuginfo *dinfo,
1037 struct perf_probe_event *pev,
1038 struct strfilter *_filter)
1042 struct str_node *node;
1043 struct variable_list *vls = NULL, *vl;
1044 struct perf_probe_point tmp;
1047 buf = synthesize_perf_probe_point(&pev->point);
1050 pr_debug("Searching variables at %s\n", buf);
1052 ret = debuginfo__find_available_vars_at(dinfo, pev, &vls);
1053 if (!ret) { /* Not found, retry with an alternative */
1054 ret = get_alternative_probe_event(dinfo, pev, &tmp);
1056 ret = debuginfo__find_available_vars_at(dinfo, pev,
1058 /* Release the old probe_point */
1059 clear_perf_probe_point(&tmp);
1063 if (ret == 0 || ret == -ENOENT) {
1064 pr_err("Failed to find the address of %s\n", buf);
1067 pr_warning("Debuginfo analysis failed.\n");
1071 /* Some variables are found */
1072 fprintf(stdout, "Available variables at %s\n", buf);
1073 for (i = 0; i < ret; i++) {
1076 * A probe point might be converted to
1077 * several trace points.
1079 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
1081 zfree(&vl->point.symbol);
1084 strlist__for_each_entry(node, vl->vars) {
1085 var = strchr(node->s, '\t') + 1;
1086 if (strfilter__compare(_filter, var)) {
1087 fprintf(stdout, "\t\t%s\n", node->s);
1091 strlist__delete(vl->vars);
1094 fprintf(stdout, "\t\t(No matched variables)\n");
1102 /* Show available variables on given probe point */
1103 int show_available_vars(struct perf_probe_event *pevs, int npevs,
1104 struct strfilter *_filter)
1107 struct debuginfo *dinfo;
1109 ret = init_probe_symbol_maps(pevs->uprobes);
1113 dinfo = open_debuginfo(pevs->target, false);
1121 for (i = 0; i < npevs && ret >= 0; i++)
1122 ret = show_available_vars_at(dinfo, &pevs[i], _filter);
1124 debuginfo__delete(dinfo);
1126 exit_probe_symbol_maps();
1130 #else /* !HAVE_DWARF_SUPPORT */
1132 static void debuginfo_cache__exit(void)
1137 find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused,
1138 struct perf_probe_point *pp __maybe_unused,
1139 bool is_kprobe __maybe_unused)
1144 static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
1145 struct probe_trace_event **tevs __maybe_unused)
1147 if (perf_probe_event_need_dwarf(pev)) {
1148 pr_warning("Debuginfo-analysis is not supported.\n");
1155 int show_line_range(struct line_range *lr __maybe_unused,
1156 const char *module __maybe_unused,
1157 bool user __maybe_unused)
1159 pr_warning("Debuginfo-analysis is not supported.\n");
1163 int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
1164 int npevs __maybe_unused,
1165 struct strfilter *filter __maybe_unused)
1167 pr_warning("Debuginfo-analysis is not supported.\n");
1172 void line_range__clear(struct line_range *lr)
1178 intlist__delete(lr->line_list);
1179 memset(lr, 0, sizeof(*lr));
1182 int line_range__init(struct line_range *lr)
1184 memset(lr, 0, sizeof(*lr));
1185 lr->line_list = intlist__new(NULL);
1192 static int parse_line_num(char **ptr, int *val, const char *what)
1194 const char *start = *ptr;
1197 *val = strtol(*ptr, ptr, 0);
1198 if (errno || *ptr == start) {
1199 semantic_error("'%s' is not a valid number.\n", what);
1205 /* Check the name is good for event, group or function */
1206 static bool is_c_func_name(const char *name)
1208 if (!isalpha(*name) && *name != '_')
1210 while (*++name != '\0') {
1211 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
1218 * Stuff 'lr' according to the line range described by 'arg'.
1219 * The line range syntax is described by:
1221 * SRC[:SLN[+NUM|-ELN]]
1222 * FNC[@SRC][:SLN[+NUM|-ELN]]
1224 int parse_line_range_desc(const char *arg, struct line_range *lr)
1226 char *range, *file, *name = strdup(arg);
1235 range = strchr(name, ':');
1239 err = parse_line_num(&range, &lr->start, "start line");
1243 if (*range == '+' || *range == '-') {
1244 const char c = *range++;
1246 err = parse_line_num(&range, &lr->end, "end line");
1251 lr->end += lr->start;
1253 * Adjust the number of lines here.
1254 * If the number of lines == 1, the
1255 * the end of line should be equal to
1256 * the start of line.
1262 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
1265 if (lr->start > lr->end) {
1266 semantic_error("Start line must be smaller"
1267 " than end line.\n");
1270 if (*range != '\0') {
1271 semantic_error("Tailing with invalid str '%s'.\n", range);
1276 file = strchr(name, '@');
1279 lr->file = strdup(++file);
1280 if (lr->file == NULL) {
1284 lr->function = name;
1285 } else if (strchr(name, '/') || strchr(name, '.'))
1287 else if (is_c_func_name(name))/* We reuse it for checking funcname */
1288 lr->function = name;
1289 else { /* Invalid name */
1290 semantic_error("'%s' is not a valid function name.\n", name);
1301 static int parse_perf_probe_event_name(char **arg, struct perf_probe_event *pev)
1305 ptr = strchr(*arg, ':');
1308 if (!pev->sdt && !is_c_func_name(*arg))
1310 pev->group = strdup(*arg);
1316 if (!pev->sdt && !is_c_func_name(*arg)) {
1318 semantic_error("%s is bad for event name -it must "
1319 "follow C symbol-naming rule.\n", *arg);
1322 pev->event = strdup(*arg);
1323 if (pev->event == NULL)
1329 /* Parse probepoint definition. */
1330 static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
1332 struct perf_probe_point *pp = &pev->point;
1335 bool file_spec = false;
1340 * perf probe [GRP:][EVENT=]SRC[:LN|;PTN]
1341 * perf probe [GRP:][EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
1342 * perf probe %[GRP:]SDT_EVENT
1347 if (is_sdt_event(arg)) {
1353 ptr = strpbrk(arg, ";=@+%");
1357 semantic_error("%s must be an SDT name.\n",
1361 /* This must be a target file name or build id */
1362 tmp = build_id_cache__complement(ptr + 1);
1364 pev->target = build_id_cache__origname(tmp);
1367 pev->target = strdup(ptr + 1);
1372 ret = parse_perf_probe_event_name(&arg, pev);
1374 if (asprintf(&pev->point.function, "%%%s", pev->event) < 0)
1380 if (ptr && *ptr == '=') { /* Event name */
1383 ret = parse_perf_probe_event_name(&arg, pev);
1391 * Check arg is function or file name and copy it.
1393 * We consider arg to be a file spec if and only if it satisfies
1394 * all of the below criteria::
1395 * - it does not include any of "+@%",
1396 * - it includes one of ":;", and
1397 * - it has a period '.' in the name.
1399 * Otherwise, we consider arg to be a function specification.
1401 if (!strpbrk(arg, "+@%") && (ptr = strpbrk(arg, ";:")) != NULL) {
1402 /* This is a file spec if it includes a '.' before ; or : */
1403 if (memchr(arg, '.', ptr - arg))
1407 ptr = strpbrk(arg, ";:+@%");
1427 * Keep pp->function even if this is absolute address,
1428 * so it can mark whether abs_address is valid.
1429 * Which make 'perf probe lib.bin 0x0' possible.
1431 * Note that checking length of tmp is not needed
1432 * because when we access tmp[1] we know tmp[0] is '0',
1433 * so tmp[1] should always valid (but could be '\0').
1435 if (tmp && !strncmp(tmp, "0x", 2)) {
1436 pp->abs_address = strtoul(pp->function, &tmp, 0);
1438 semantic_error("Invalid absolute address.\n");
1444 /* Parse other options */
1448 if (c == ';') { /* Lazy pattern must be the last part */
1449 pp->lazy_line = strdup(arg);
1450 if (pp->lazy_line == NULL)
1454 ptr = strpbrk(arg, ";:+@%");
1460 case ':': /* Line number */
1461 pp->line = strtoul(arg, &tmp, 0);
1463 semantic_error("There is non-digit char"
1464 " in line number.\n");
1468 case '+': /* Byte offset from a symbol */
1469 pp->offset = strtoul(arg, &tmp, 0);
1471 semantic_error("There is non-digit character"
1476 case '@': /* File name */
1478 semantic_error("SRC@SRC is not allowed.\n");
1481 pp->file = strdup(arg);
1482 if (pp->file == NULL)
1485 case '%': /* Probe places */
1486 if (strcmp(arg, "return") == 0) {
1488 } else { /* Others not supported yet */
1489 semantic_error("%%%s is not supported.\n", arg);
1493 default: /* Buggy case */
1494 pr_err("This program has a bug at %s:%d.\n",
1495 __FILE__, __LINE__);
1501 /* Exclusion check */
1502 if (pp->lazy_line && pp->line) {
1503 semantic_error("Lazy pattern can't be used with"
1508 if (pp->lazy_line && pp->offset) {
1509 semantic_error("Lazy pattern can't be used with offset.\n");
1513 if (pp->line && pp->offset) {
1514 semantic_error("Offset can't be used with line number.\n");
1518 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
1519 semantic_error("File always requires line number or "
1524 if (pp->offset && !pp->function) {
1525 semantic_error("Offset requires an entry function.\n");
1529 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
1530 semantic_error("Offset/Line/Lazy pattern can't be used with "
1535 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
1536 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
1541 /* Parse perf-probe event argument */
1542 static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
1544 char *tmp, *goodname;
1545 struct perf_probe_arg_field **fieldp;
1547 pr_debug("parsing arg: %s into ", str);
1549 tmp = strchr(str, '=');
1551 arg->name = strndup(str, tmp - str);
1552 if (arg->name == NULL)
1554 pr_debug("name:%s ", arg->name);
1558 tmp = strchr(str, ':');
1559 if (tmp) { /* Type setting */
1561 arg->type = strdup(tmp + 1);
1562 if (arg->type == NULL)
1564 pr_debug("type:%s ", arg->type);
1567 tmp = strpbrk(str, "-.[");
1568 if (!is_c_varname(str) || !tmp) {
1569 /* A variable, register, symbol or special value */
1570 arg->var = strdup(str);
1571 if (arg->var == NULL)
1573 pr_debug("%s\n", arg->var);
1577 /* Structure fields or array element */
1578 arg->var = strndup(str, tmp - str);
1579 if (arg->var == NULL)
1581 goodname = arg->var;
1582 pr_debug("%s, ", arg->var);
1583 fieldp = &arg->field;
1586 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
1587 if (*fieldp == NULL)
1589 if (*tmp == '[') { /* Array */
1591 (*fieldp)->index = strtol(str + 1, &tmp, 0);
1592 (*fieldp)->ref = true;
1593 if (*tmp != ']' || tmp == str + 1) {
1594 semantic_error("Array index must be a"
1601 } else { /* Structure */
1604 (*fieldp)->ref = false;
1605 } else if (tmp[1] == '>') {
1607 (*fieldp)->ref = true;
1609 semantic_error("Argument parse error: %s\n",
1613 tmp = strpbrk(str, "-.[");
1616 (*fieldp)->name = strndup(str, tmp - str);
1617 if ((*fieldp)->name == NULL)
1620 goodname = (*fieldp)->name;
1621 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
1622 fieldp = &(*fieldp)->next;
1625 (*fieldp)->name = strdup(str);
1626 if ((*fieldp)->name == NULL)
1629 goodname = (*fieldp)->name;
1630 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
1632 /* If no name is specified, set the last field name (not array index)*/
1634 arg->name = strdup(goodname);
1635 if (arg->name == NULL)
1641 /* Parse perf-probe event command */
1642 int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
1645 int argc, i, ret = 0;
1647 argv = argv_split(cmd, &argc);
1649 pr_debug("Failed to split arguments.\n");
1652 if (argc - 1 > MAX_PROBE_ARGS) {
1653 semantic_error("Too many probe arguments (%d).\n", argc - 1);
1657 /* Parse probe point */
1658 ret = parse_perf_probe_point(argv[0], pev);
1662 /* Copy arguments and ensure return probe has no C argument */
1663 pev->nargs = argc - 1;
1664 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1665 if (pev->args == NULL) {
1669 for (i = 0; i < pev->nargs && ret >= 0; i++) {
1670 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1672 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
1673 semantic_error("You can't specify local variable for"
1684 /* Returns true if *any* ARG is either C variable, $params or $vars. */
1685 bool perf_probe_with_var(struct perf_probe_event *pev)
1689 for (i = 0; i < pev->nargs; i++)
1690 if (is_c_varname(pev->args[i].var) ||
1691 !strcmp(pev->args[i].var, PROBE_ARG_PARAMS) ||
1692 !strcmp(pev->args[i].var, PROBE_ARG_VARS))
1697 /* Return true if this perf_probe_event requires debuginfo */
1698 bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
1700 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1703 if (perf_probe_with_var(pev))
1709 /* Parse probe_events event into struct probe_point */
1710 int parse_probe_trace_command(const char *cmd, struct probe_trace_event *tev)
1712 struct probe_trace_point *tp = &tev->point;
1715 char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
1719 pr_debug("Parsing probe_events: %s\n", cmd);
1720 argv = argv_split(cmd, &argc);
1722 pr_debug("Failed to split arguments.\n");
1726 semantic_error("Too few probe arguments.\n");
1731 /* Scan event and group name. */
1732 argv0_str = strdup(argv[0]);
1733 if (argv0_str == NULL) {
1737 fmt1_str = strtok_r(argv0_str, ":", &fmt);
1738 fmt2_str = strtok_r(NULL, "/", &fmt);
1739 fmt3_str = strtok_r(NULL, " \t", &fmt);
1740 if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
1741 || fmt3_str == NULL) {
1742 semantic_error("Failed to parse event name: %s\n", argv[0]);
1747 tev->group = strdup(fmt2_str);
1748 tev->event = strdup(fmt3_str);
1749 if (tev->group == NULL || tev->event == NULL) {
1753 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
1755 tp->retprobe = (pr == 'r');
1757 /* Scan module name(if there), function name and offset */
1758 p = strchr(argv[1], ':');
1760 tp->module = strndup(argv[1], p - argv[1]);
1765 tev->uprobes = (tp->module[0] == '/');
1769 fmt1_str = strtok_r(p, "+", &fmt);
1770 /* only the address started with 0x */
1771 if (fmt1_str[0] == '0') {
1773 * Fix a special case:
1774 * if address == 0, kernel reports something like:
1775 * p:probe_libc/abs_0 /lib/libc-2.18.so:0x (null) arg1=%ax
1776 * Newer kernel may fix that, but we want to
1777 * support old kernel also.
1779 if (strcmp(fmt1_str, "0x") == 0) {
1780 if (!argv[2] || strcmp(argv[2], "(null)")) {
1787 for (i = 2; argv[i + 1] != NULL; i++)
1788 argv[i] = argv[i + 1];
1793 tp->address = strtoul(fmt1_str, NULL, 0);
1795 /* Only the symbol-based probe has offset */
1796 tp->symbol = strdup(fmt1_str);
1797 if (tp->symbol == NULL) {
1801 fmt2_str = strtok_r(NULL, "", &fmt);
1802 if (fmt2_str == NULL)
1805 tp->offset = strtoul(fmt2_str, NULL, 10);
1808 tev->nargs = argc - 2;
1809 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
1810 if (tev->args == NULL) {
1814 for (i = 0; i < tev->nargs; i++) {
1815 p = strchr(argv[i + 2], '=');
1816 if (p) /* We don't need which register is assigned. */
1820 tev->args[i].name = strdup(argv[i + 2]);
1821 /* TODO: parse regs and offset */
1822 tev->args[i].value = strdup(p);
1823 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1835 /* Compose only probe arg */
1836 char *synthesize_perf_probe_arg(struct perf_probe_arg *pa)
1838 struct perf_probe_arg_field *field = pa->field;
1843 if (strbuf_init(&buf, 64) < 0)
1846 if (pa->name && pa->var)
1847 err = strbuf_addf(&buf, "%s=%s", pa->name, pa->var);
1849 err = strbuf_addstr(&buf, pa->name ?: pa->var);
1854 if (field->name[0] == '[')
1855 err = strbuf_addstr(&buf, field->name);
1857 err = strbuf_addf(&buf, "%s%s", field->ref ? "->" : ".",
1859 field = field->next;
1865 if (strbuf_addf(&buf, ":%s", pa->type) < 0)
1868 ret = strbuf_detach(&buf, NULL);
1870 strbuf_release(&buf);
1874 /* Compose only probe point (not argument) */
1875 char *synthesize_perf_probe_point(struct perf_probe_point *pp)
1878 char *tmp, *ret = NULL;
1881 if (strbuf_init(&buf, 64) < 0)
1885 if (strbuf_addstr(&buf, pp->function) < 0)
1888 err = strbuf_addf(&buf, "+%lu", pp->offset);
1890 err = strbuf_addf(&buf, ":%d", pp->line);
1891 else if (pp->retprobe)
1892 err = strbuf_addstr(&buf, "%return");
1900 tmp = strchr(pp->file + len - 30, '/');
1901 tmp = tmp ? tmp + 1 : pp->file + len - 30;
1903 err = strbuf_addf(&buf, "@%s", tmp);
1904 if (!err && !pp->function && pp->line)
1905 err = strbuf_addf(&buf, ":%d", pp->line);
1908 ret = strbuf_detach(&buf, NULL);
1910 strbuf_release(&buf);
1914 char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1917 char *tmp, *ret = NULL;
1920 if (strbuf_init(&buf, 64))
1923 if (strbuf_addf(&buf, "%s:%s=", pev->group ?: PERFPROBE_GROUP,
1927 tmp = synthesize_perf_probe_point(&pev->point);
1928 if (!tmp || strbuf_addstr(&buf, tmp) < 0)
1932 for (i = 0; i < pev->nargs; i++) {
1933 tmp = synthesize_perf_probe_arg(pev->args + i);
1934 if (!tmp || strbuf_addf(&buf, " %s", tmp) < 0)
1939 ret = strbuf_detach(&buf, NULL);
1941 strbuf_release(&buf);
1945 static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
1946 struct strbuf *buf, int depth)
1950 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
1955 err = strbuf_addf(buf, "%+ld(", ref->offset);
1956 return (err < 0) ? err : depth;
1959 static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
1962 struct probe_trace_arg_ref *ref = arg->ref;
1965 /* Argument name or separator */
1967 err = strbuf_addf(buf, " %s=", arg->name);
1969 err = strbuf_addch(buf, ' ');
1973 /* Special case: @XXX */
1974 if (arg->value[0] == '@' && arg->ref)
1977 /* Dereferencing arguments */
1979 depth = __synthesize_probe_trace_arg_ref(ref, buf, 1);
1984 /* Print argument value */
1985 if (arg->value[0] == '@' && arg->ref)
1986 err = strbuf_addf(buf, "%s%+ld", arg->value, arg->ref->offset);
1988 err = strbuf_addstr(buf, arg->value);
1991 while (!err && depth--)
1992 err = strbuf_addch(buf, ')');
1994 /* Print argument type */
1995 if (!err && arg->type)
1996 err = strbuf_addf(buf, ":%s", arg->type);
2001 char *synthesize_probe_trace_command(struct probe_trace_event *tev)
2003 struct probe_trace_point *tp = &tev->point;
2008 /* Uprobes must have tp->module */
2009 if (tev->uprobes && !tp->module)
2012 if (strbuf_init(&buf, 32) < 0)
2015 if (strbuf_addf(&buf, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
2016 tev->group, tev->event) < 0)
2019 * If tp->address == 0, then this point must be a
2020 * absolute address uprobe.
2021 * try_to_find_absolute_address() should have made
2022 * tp->symbol to "0x0".
2024 if (tev->uprobes && !tp->address) {
2025 if (!tp->symbol || strcmp(tp->symbol, "0x0"))
2029 /* Use the tp->address for uprobes */
2031 err = strbuf_addf(&buf, "%s:0x%lx", tp->module, tp->address);
2032 else if (!strncmp(tp->symbol, "0x", 2))
2033 /* Absolute address. See try_to_find_absolute_address() */
2034 err = strbuf_addf(&buf, "%s%s0x%lx", tp->module ?: "",
2035 tp->module ? ":" : "", tp->address);
2037 err = strbuf_addf(&buf, "%s%s%s+%lu", tp->module ?: "",
2038 tp->module ? ":" : "", tp->symbol, tp->offset);
2042 for (i = 0; i < tev->nargs; i++)
2043 if (synthesize_probe_trace_arg(&tev->args[i], &buf) < 0)
2046 ret = strbuf_detach(&buf, NULL);
2048 strbuf_release(&buf);
2052 static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
2053 struct perf_probe_point *pp,
2056 struct symbol *sym = NULL;
2057 struct map *map = NULL;
2058 u64 addr = tp->address;
2062 map = dso__new_map(tp->module);
2065 sym = map__find_symbol(map, addr);
2067 if (tp->symbol && !addr) {
2068 if (kernel_get_symbol_address_by_name(tp->symbol,
2069 &addr, true, false) < 0)
2074 sym = __find_kernel_function(addr, &map);
2081 pp->retprobe = tp->retprobe;
2082 pp->offset = addr - map->unmap_ip(map, sym->start);
2083 pp->function = strdup(sym->name);
2084 ret = pp->function ? 0 : -ENOMEM;
2087 if (map && !is_kprobe) {
2094 static int convert_to_perf_probe_point(struct probe_trace_point *tp,
2095 struct perf_probe_point *pp,
2101 ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe);
2104 ret = find_perf_probe_point_from_map(tp, pp, is_kprobe);
2108 pr_debug("Failed to find probe point from both of dwarf and map.\n");
2111 pp->function = strdup(tp->symbol);
2112 pp->offset = tp->offset;
2114 ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address);
2117 pp->function = strdup(buf);
2120 if (pp->function == NULL)
2123 pp->retprobe = tp->retprobe;
2128 static int convert_to_perf_probe_event(struct probe_trace_event *tev,
2129 struct perf_probe_event *pev, bool is_kprobe)
2131 struct strbuf buf = STRBUF_INIT;
2134 /* Convert event/group name */
2135 pev->event = strdup(tev->event);
2136 pev->group = strdup(tev->group);
2137 if (pev->event == NULL || pev->group == NULL)
2140 /* Convert trace_point to probe_point */
2141 ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
2145 /* Convert trace_arg to probe_arg */
2146 pev->nargs = tev->nargs;
2147 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
2148 if (pev->args == NULL)
2150 for (i = 0; i < tev->nargs && ret >= 0; i++) {
2151 if (tev->args[i].name)
2152 pev->args[i].name = strdup(tev->args[i].name);
2154 if ((ret = strbuf_init(&buf, 32)) < 0)
2156 ret = synthesize_probe_trace_arg(&tev->args[i], &buf);
2157 pev->args[i].name = strbuf_detach(&buf, NULL);
2159 if (pev->args[i].name == NULL && ret >= 0)
2164 clear_perf_probe_event(pev);
2169 void clear_perf_probe_event(struct perf_probe_event *pev)
2171 struct perf_probe_arg_field *field, *next;
2177 clear_perf_probe_point(&pev->point);
2179 for (i = 0; i < pev->nargs; i++) {
2180 free(pev->args[i].name);
2181 free(pev->args[i].var);
2182 free(pev->args[i].type);
2183 field = pev->args[i].field;
2186 zfree(&field->name);
2192 memset(pev, 0, sizeof(*pev));
2195 #define strdup_or_goto(str, label) \
2196 ({ char *__p = NULL; if (str && !(__p = strdup(str))) goto label; __p; })
2198 static int perf_probe_point__copy(struct perf_probe_point *dst,
2199 struct perf_probe_point *src)
2201 dst->file = strdup_or_goto(src->file, out_err);
2202 dst->function = strdup_or_goto(src->function, out_err);
2203 dst->lazy_line = strdup_or_goto(src->lazy_line, out_err);
2204 dst->line = src->line;
2205 dst->retprobe = src->retprobe;
2206 dst->offset = src->offset;
2210 clear_perf_probe_point(dst);
2214 static int perf_probe_arg__copy(struct perf_probe_arg *dst,
2215 struct perf_probe_arg *src)
2217 struct perf_probe_arg_field *field, **ppfield;
2219 dst->name = strdup_or_goto(src->name, out_err);
2220 dst->var = strdup_or_goto(src->var, out_err);
2221 dst->type = strdup_or_goto(src->type, out_err);
2224 ppfield = &(dst->field);
2226 *ppfield = zalloc(sizeof(*field));
2229 (*ppfield)->name = strdup_or_goto(field->name, out_err);
2230 (*ppfield)->index = field->index;
2231 (*ppfield)->ref = field->ref;
2232 field = field->next;
2233 ppfield = &((*ppfield)->next);
2240 int perf_probe_event__copy(struct perf_probe_event *dst,
2241 struct perf_probe_event *src)
2245 dst->event = strdup_or_goto(src->event, out_err);
2246 dst->group = strdup_or_goto(src->group, out_err);
2247 dst->target = strdup_or_goto(src->target, out_err);
2248 dst->uprobes = src->uprobes;
2250 if (perf_probe_point__copy(&dst->point, &src->point) < 0)
2253 dst->args = zalloc(sizeof(struct perf_probe_arg) * src->nargs);
2256 dst->nargs = src->nargs;
2258 for (i = 0; i < src->nargs; i++)
2259 if (perf_probe_arg__copy(&dst->args[i], &src->args[i]) < 0)
2264 clear_perf_probe_event(dst);
2268 void clear_probe_trace_event(struct probe_trace_event *tev)
2270 struct probe_trace_arg_ref *ref, *next;
2275 free(tev->point.symbol);
2276 free(tev->point.realname);
2277 free(tev->point.module);
2278 for (i = 0; i < tev->nargs; i++) {
2279 free(tev->args[i].name);
2280 free(tev->args[i].value);
2281 free(tev->args[i].type);
2282 ref = tev->args[i].ref;
2290 memset(tev, 0, sizeof(*tev));
2293 struct kprobe_blacklist_node {
2294 struct list_head list;
2295 unsigned long start;
2300 static void kprobe_blacklist__delete(struct list_head *blacklist)
2302 struct kprobe_blacklist_node *node;
2304 while (!list_empty(blacklist)) {
2305 node = list_first_entry(blacklist,
2306 struct kprobe_blacklist_node, list);
2307 list_del(&node->list);
2313 static int kprobe_blacklist__load(struct list_head *blacklist)
2315 struct kprobe_blacklist_node *node;
2316 const char *__debugfs = debugfs__mountpoint();
2317 char buf[PATH_MAX], *p;
2321 if (__debugfs == NULL)
2324 ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs);
2328 fp = fopen(buf, "r");
2333 while (fgets(buf, PATH_MAX, fp)) {
2334 node = zalloc(sizeof(*node));
2339 INIT_LIST_HEAD(&node->list);
2340 list_add_tail(&node->list, blacklist);
2341 if (sscanf(buf, "0x%lx-0x%lx", &node->start, &node->end) != 2) {
2345 p = strchr(buf, '\t');
2348 if (p[strlen(p) - 1] == '\n')
2349 p[strlen(p) - 1] = '\0';
2351 p = (char *)"unknown";
2352 node->symbol = strdup(p);
2353 if (!node->symbol) {
2357 pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
2358 node->start, node->end, node->symbol);
2362 kprobe_blacklist__delete(blacklist);
2368 static struct kprobe_blacklist_node *
2369 kprobe_blacklist__find_by_address(struct list_head *blacklist,
2370 unsigned long address)
2372 struct kprobe_blacklist_node *node;
2374 list_for_each_entry(node, blacklist, list) {
2375 if (node->start <= address && address <= node->end)
2382 static LIST_HEAD(kprobe_blacklist);
2384 static void kprobe_blacklist__init(void)
2386 if (!list_empty(&kprobe_blacklist))
2389 if (kprobe_blacklist__load(&kprobe_blacklist) < 0)
2390 pr_debug("No kprobe blacklist support, ignored\n");
2393 static void kprobe_blacklist__release(void)
2395 kprobe_blacklist__delete(&kprobe_blacklist);
2398 static bool kprobe_blacklist__listed(unsigned long address)
2400 return !!kprobe_blacklist__find_by_address(&kprobe_blacklist, address);
2403 static int perf_probe_event__sprintf(const char *group, const char *event,
2404 struct perf_probe_event *pev,
2406 struct strbuf *result)
2411 if (asprintf(&buf, "%s:%s", group, event) < 0)
2413 ret = strbuf_addf(result, " %-20s (on ", buf);
2418 /* Synthesize only event probe point */
2419 buf = synthesize_perf_probe_point(&pev->point);
2422 ret = strbuf_addstr(result, buf);
2426 ret = strbuf_addf(result, " in %s", module);
2428 if (!ret && pev->nargs > 0) {
2429 ret = strbuf_add(result, " with", 5);
2430 for (i = 0; !ret && i < pev->nargs; i++) {
2431 buf = synthesize_perf_probe_arg(&pev->args[i]);
2434 ret = strbuf_addf(result, " %s", buf);
2439 ret = strbuf_addch(result, ')');
2445 int show_perf_probe_event(const char *group, const char *event,
2446 struct perf_probe_event *pev,
2447 const char *module, bool use_stdout)
2449 struct strbuf buf = STRBUF_INIT;
2452 ret = perf_probe_event__sprintf(group, event, pev, module, &buf);
2455 printf("%s\n", buf.buf);
2457 pr_info("%s\n", buf.buf);
2459 strbuf_release(&buf);
2464 static bool filter_probe_trace_event(struct probe_trace_event *tev,
2465 struct strfilter *filter)
2469 /* At first, check the event name itself */
2470 if (strfilter__compare(filter, tev->event))
2473 /* Next, check the combination of name and group */
2474 if (e_snprintf(tmp, 128, "%s:%s", tev->group, tev->event) < 0)
2476 return strfilter__compare(filter, tmp);
2479 static int __show_perf_probe_events(int fd, bool is_kprobe,
2480 struct strfilter *filter)
2483 struct probe_trace_event tev;
2484 struct perf_probe_event pev;
2485 struct strlist *rawlist;
2486 struct str_node *ent;
2488 memset(&tev, 0, sizeof(tev));
2489 memset(&pev, 0, sizeof(pev));
2491 rawlist = probe_file__get_rawlist(fd);
2495 strlist__for_each_entry(ent, rawlist) {
2496 ret = parse_probe_trace_command(ent->s, &tev);
2498 if (!filter_probe_trace_event(&tev, filter))
2500 ret = convert_to_perf_probe_event(&tev, &pev,
2504 ret = show_perf_probe_event(pev.group, pev.event,
2505 &pev, tev.point.module,
2509 clear_perf_probe_event(&pev);
2510 clear_probe_trace_event(&tev);
2514 strlist__delete(rawlist);
2515 /* Cleanup cached debuginfo if needed */
2516 debuginfo_cache__exit();
2521 /* List up current perf-probe events */
2522 int show_perf_probe_events(struct strfilter *filter)
2524 int kp_fd, up_fd, ret;
2528 if (probe_conf.cache)
2529 return probe_cache__show_all_caches(filter);
2531 ret = init_probe_symbol_maps(false);
2535 ret = probe_file__open_both(&kp_fd, &up_fd, 0);
2540 ret = __show_perf_probe_events(kp_fd, true, filter);
2541 if (up_fd >= 0 && ret >= 0)
2542 ret = __show_perf_probe_events(up_fd, false, filter);
2547 exit_probe_symbol_maps();
2552 static int get_new_event_name(char *buf, size_t len, const char *base,
2553 struct strlist *namelist, bool allow_suffix)
2560 nbase = strdup(base);
2564 /* Cut off the dot suffixes (e.g. .const, .isra)*/
2565 p = strchr(nbase, '.');
2566 if (p && p != nbase)
2569 /* Try no suffix number */
2570 ret = e_snprintf(buf, len, "%s", nbase);
2572 pr_debug("snprintf() failed: %d\n", ret);
2575 if (!strlist__has_entry(namelist, buf))
2578 if (!allow_suffix) {
2579 pr_warning("Error: event \"%s\" already exists.\n"
2580 " Hint: Remove existing event by 'perf probe -d'\n"
2581 " or force duplicates by 'perf probe -f'\n"
2582 " or set 'force=yes' in BPF source.\n",
2588 /* Try to add suffix */
2589 for (i = 1; i < MAX_EVENT_INDEX; i++) {
2590 ret = e_snprintf(buf, len, "%s_%d", nbase, i);
2592 pr_debug("snprintf() failed: %d\n", ret);
2595 if (!strlist__has_entry(namelist, buf))
2598 if (i == MAX_EVENT_INDEX) {
2599 pr_warning("Too many events are on the same function.\n");
2608 /* Warn if the current kernel's uprobe implementation is old */
2609 static void warn_uprobe_event_compat(struct probe_trace_event *tev)
2612 char *buf = synthesize_probe_trace_command(tev);
2614 /* Old uprobe event doesn't support memory dereference */
2615 if (!tev->uprobes || tev->nargs == 0 || !buf)
2618 for (i = 0; i < tev->nargs; i++)
2619 if (strglobmatch(tev->args[i].value, "[$@+-]*")) {
2620 pr_warning("Please upgrade your kernel to at least "
2621 "3.14 to have access to feature %s\n",
2622 tev->args[i].value);
2629 /* Set new name from original perf_probe_event and namelist */
2630 static int probe_trace_event__set_name(struct probe_trace_event *tev,
2631 struct perf_probe_event *pev,
2632 struct strlist *namelist,
2635 const char *event, *group;
2639 /* If probe_event or trace_event already have the name, reuse it */
2640 if (pev->event && !pev->sdt)
2642 else if (tev->event)
2645 /* Or generate new one from probe point */
2646 if (pev->point.function &&
2647 (strncmp(pev->point.function, "0x", 2) != 0) &&
2648 !strisglob(pev->point.function))
2649 event = pev->point.function;
2651 event = tev->point.realname;
2653 if (pev->group && !pev->sdt)
2655 else if (tev->group)
2658 group = PERFPROBE_GROUP;
2660 /* Get an unused new event name */
2661 ret = get_new_event_name(buf, 64, event,
2662 namelist, allow_suffix);
2668 tev->event = strdup(event);
2669 tev->group = strdup(group);
2670 if (tev->event == NULL || tev->group == NULL)
2673 /* Add added event name to namelist */
2674 strlist__add(namelist, event);
2678 static int __open_probe_file_and_namelist(bool uprobe,
2679 struct strlist **namelist)
2683 fd = probe_file__open(PF_FL_RW | (uprobe ? PF_FL_UPROBE : 0));
2687 /* Get current event names */
2688 *namelist = probe_file__get_namelist(fd);
2690 pr_debug("Failed to get current event list.\n");
2697 static int __add_probe_trace_events(struct perf_probe_event *pev,
2698 struct probe_trace_event *tevs,
2699 int ntevs, bool allow_suffix)
2701 int i, fd[2] = {-1, -1}, up, ret;
2702 struct probe_trace_event *tev = NULL;
2703 struct probe_cache *cache = NULL;
2704 struct strlist *namelist[2] = {NULL, NULL};
2706 up = pev->uprobes ? 1 : 0;
2707 fd[up] = __open_probe_file_and_namelist(up, &namelist[up]);
2712 for (i = 0; i < ntevs; i++) {
2714 up = tev->uprobes ? 1 : 0;
2715 if (fd[up] == -1) { /* Open the kprobe/uprobe_events */
2716 fd[up] = __open_probe_file_and_namelist(up,
2721 /* Skip if the symbol is out of .text or blacklisted */
2722 if (!tev->point.symbol && !pev->uprobes)
2725 /* Set new name for tev (and update namelist) */
2726 ret = probe_trace_event__set_name(tev, pev, namelist[up],
2731 ret = probe_file__add_event(fd[up], tev);
2736 * Probes after the first probe which comes from same
2737 * user input are always allowed to add suffix, because
2738 * there might be several addresses corresponding to
2741 allow_suffix = true;
2743 if (ret == -EINVAL && pev->uprobes)
2744 warn_uprobe_event_compat(tev);
2745 if (ret == 0 && probe_conf.cache) {
2746 cache = probe_cache__new(pev->target);
2748 probe_cache__add_entry(cache, pev, tevs, ntevs) < 0 ||
2749 probe_cache__commit(cache) < 0)
2750 pr_warning("Failed to add event to probe cache\n");
2751 probe_cache__delete(cache);
2755 for (up = 0; up < 2; up++) {
2756 strlist__delete(namelist[up]);
2763 static int find_probe_functions(struct map *map, char *name,
2764 struct symbol **syms)
2768 struct rb_node *tmp;
2770 if (map__load(map) < 0)
2773 map__for_each_symbol(map, sym, tmp) {
2774 if (strglobmatch(sym->name, name)) {
2776 if (syms && found < probe_conf.max_probes)
2777 syms[found - 1] = sym;
2784 void __weak arch__fix_tev_from_maps(struct perf_probe_event *pev __maybe_unused,
2785 struct probe_trace_event *tev __maybe_unused,
2786 struct map *map __maybe_unused,
2787 struct symbol *sym __maybe_unused) { }
2790 * Find probe function addresses from map.
2791 * Return an error or the number of found probe_trace_event
2793 static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
2794 struct probe_trace_event **tevs)
2796 struct map *map = NULL;
2797 struct ref_reloc_sym *reloc_sym = NULL;
2799 struct symbol **syms = NULL;
2800 struct probe_trace_event *tev;
2801 struct perf_probe_point *pp = &pev->point;
2802 struct probe_trace_point *tp;
2803 int num_matched_functions;
2804 int ret, i, j, skipped = 0;
2807 map = get_target_map(pev->target, pev->uprobes);
2813 syms = malloc(sizeof(struct symbol *) * probe_conf.max_probes);
2820 * Load matched symbols: Since the different local symbols may have
2821 * same name but different addresses, this lists all the symbols.
2823 num_matched_functions = find_probe_functions(map, pp->function, syms);
2824 if (num_matched_functions == 0) {
2825 pr_err("Failed to find symbol %s in %s\n", pp->function,
2826 pev->target ? : "kernel");
2829 } else if (num_matched_functions > probe_conf.max_probes) {
2830 pr_err("Too many functions matched in %s\n",
2831 pev->target ? : "kernel");
2836 /* Note that the symbols in the kmodule are not relocated */
2837 if (!pev->uprobes && !pev->target &&
2838 (!pp->retprobe || kretprobe_offset_is_supported())) {
2839 reloc_sym = kernel_get_ref_reloc_sym();
2841 pr_warning("Relocated base symbol is not found!\n");
2847 /* Setup result trace-probe-events */
2848 *tevs = zalloc(sizeof(*tev) * num_matched_functions);
2856 for (j = 0; j < num_matched_functions; j++) {
2859 tev = (*tevs) + ret;
2861 if (ret == num_matched_functions) {
2862 pr_warning("Too many symbols are listed. Skip it.\n");
2867 if (pp->offset > sym->end - sym->start) {
2868 pr_warning("Offset %ld is bigger than the size of %s\n",
2869 pp->offset, sym->name);
2873 /* Add one probe point */
2874 tp->address = map->unmap_ip(map, sym->start) + pp->offset;
2876 /* Check the kprobe (not in module) is within .text */
2877 if (!pev->uprobes && !pev->target &&
2878 kprobe_warn_out_range(sym->name, tp->address)) {
2879 tp->symbol = NULL; /* Skip it */
2881 } else if (reloc_sym) {
2882 tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
2883 tp->offset = tp->address - reloc_sym->addr;
2885 tp->symbol = strdup_or_goto(sym->name, nomem_out);
2886 tp->offset = pp->offset;
2888 tp->realname = strdup_or_goto(sym->name, nomem_out);
2890 tp->retprobe = pp->retprobe;
2893 tev->point.module = strdup_or_goto(pev->target,
2896 mod_name = find_module_name(pev->target);
2898 strdup(mod_name ? mod_name : pev->target);
2900 if (!tev->point.module)
2904 tev->uprobes = pev->uprobes;
2905 tev->nargs = pev->nargs;
2907 tev->args = zalloc(sizeof(struct probe_trace_arg) *
2909 if (tev->args == NULL)
2912 for (i = 0; i < tev->nargs; i++) {
2913 if (pev->args[i].name)
2915 strdup_or_goto(pev->args[i].name,
2918 tev->args[i].value = strdup_or_goto(pev->args[i].var,
2920 if (pev->args[i].type)
2922 strdup_or_goto(pev->args[i].type,
2925 arch__fix_tev_from_maps(pev, tev, map, sym);
2927 if (ret == skipped) {
2940 clear_probe_trace_events(*tevs, num_matched_functions);
2945 static int try_to_find_absolute_address(struct perf_probe_event *pev,
2946 struct probe_trace_event **tevs)
2948 struct perf_probe_point *pp = &pev->point;
2949 struct probe_trace_event *tev;
2950 struct probe_trace_point *tp;
2953 if (!(pev->point.function && !strncmp(pev->point.function, "0x", 2)))
2955 if (perf_probe_event_need_dwarf(pev))
2959 * This is 'perf probe /lib/libc.so 0xabcd'. Try to probe at
2962 * Only one tev can be generated by this.
2964 *tevs = zalloc(sizeof(*tev));
2972 * Don't use tp->offset, use address directly, because
2973 * in synthesize_probe_trace_command() address cannot be
2976 tp->address = pev->point.abs_address;
2977 tp->retprobe = pp->retprobe;
2978 tev->uprobes = pev->uprobes;
2982 * Give it a '0x' leading symbol name.
2983 * In __add_probe_trace_events, a NULL symbol is interpreted as
2986 if (asprintf(&tp->symbol, "0x%lx", tp->address) < 0)
2989 /* For kprobe, check range */
2990 if ((!tev->uprobes) &&
2991 (kprobe_warn_out_range(tev->point.symbol,
2992 tev->point.address))) {
2997 if (asprintf(&tp->realname, "abs_%lx", tp->address) < 0)
3001 tp->module = strdup(pev->target);
3007 tev->group = strdup(pev->group);
3013 tev->event = strdup(pev->event);
3018 tev->nargs = pev->nargs;
3019 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
3023 for (i = 0; i < tev->nargs; i++)
3024 copy_to_probe_trace_arg(&tev->args[i], &pev->args[i]);
3029 clear_probe_trace_events(*tevs, 1);
3034 /* Concatinate two arrays */
3035 static void *memcat(void *a, size_t sz_a, void *b, size_t sz_b)
3039 ret = malloc(sz_a + sz_b);
3041 memcpy(ret, a, sz_a);
3042 memcpy(ret + sz_a, b, sz_b);
3048 concat_probe_trace_events(struct probe_trace_event **tevs, int *ntevs,
3049 struct probe_trace_event **tevs2, int ntevs2)
3051 struct probe_trace_event *new_tevs;
3061 if (*ntevs + ntevs2 > probe_conf.max_probes)
3064 /* Concatinate the array of probe_trace_event */
3065 new_tevs = memcat(*tevs, (*ntevs) * sizeof(**tevs),
3066 *tevs2, ntevs2 * sizeof(**tevs2));
3076 clear_probe_trace_events(*tevs2, ntevs2);
3083 * Try to find probe_trace_event from given probe caches. Return the number
3084 * of cached events found, if an error occurs return the error.
3086 static int find_cached_events(struct perf_probe_event *pev,
3087 struct probe_trace_event **tevs,
3090 struct probe_cache *cache;
3091 struct probe_cache_entry *entry;
3092 struct probe_trace_event *tmp_tevs = NULL;
3096 cache = probe_cache__new(target);
3097 /* Return 0 ("not found") if the target has no probe cache. */
3101 for_each_probe_cache_entry(entry, cache) {
3102 /* Skip the cache entry which has no name */
3103 if (!entry->pev.event || !entry->pev.group)
3105 if ((!pev->group || strglobmatch(entry->pev.group, pev->group)) &&
3106 strglobmatch(entry->pev.event, pev->event)) {
3107 ret = probe_cache_entry__get_event(entry, &tmp_tevs);
3109 ret = concat_probe_trace_events(tevs, &ntevs,
3115 probe_cache__delete(cache);
3117 clear_probe_trace_events(*tevs, ntevs);
3121 if (ntevs > 0 && target && target[0] == '/')
3122 pev->uprobes = true;
3128 /* Try to find probe_trace_event from all probe caches */
3129 static int find_cached_events_all(struct perf_probe_event *pev,
3130 struct probe_trace_event **tevs)
3132 struct probe_trace_event *tmp_tevs = NULL;
3133 struct strlist *bidlist;
3134 struct str_node *nd;
3139 /* Get the buildid list of all valid caches */
3140 bidlist = build_id_cache__list_all(true);
3143 pr_debug("Failed to get buildids: %d\n", ret);
3148 strlist__for_each_entry(nd, bidlist) {
3149 pathname = build_id_cache__origname(nd->s);
3150 ret = find_cached_events(pev, &tmp_tevs, pathname);
3151 /* In the case of cnt == 0, we just skip it */
3153 ret = concat_probe_trace_events(tevs, &ntevs,
3159 strlist__delete(bidlist);
3162 clear_probe_trace_events(*tevs, ntevs);
3170 static int find_probe_trace_events_from_cache(struct perf_probe_event *pev,
3171 struct probe_trace_event **tevs)
3173 struct probe_cache *cache;
3174 struct probe_cache_entry *entry;
3175 struct probe_trace_event *tev;
3176 struct str_node *node;
3180 /* For SDT/cached events, we use special search functions */
3182 return find_cached_events_all(pev, tevs);
3184 return find_cached_events(pev, tevs, pev->target);
3186 cache = probe_cache__new(pev->target);
3190 entry = probe_cache__find(cache, pev);
3192 /* SDT must be in the cache */
3193 ret = pev->sdt ? -ENOENT : 0;
3197 ret = strlist__nr_entries(entry->tevlist);
3198 if (ret > probe_conf.max_probes) {
3199 pr_debug("Too many entries matched in the cache of %s\n",
3200 pev->target ? : "kernel");
3205 *tevs = zalloc(ret * sizeof(*tev));
3212 strlist__for_each_entry(node, entry->tevlist) {
3213 tev = &(*tevs)[i++];
3214 ret = parse_probe_trace_command(node->s, tev);
3217 /* Set the uprobes attribute as same as original */
3218 tev->uprobes = pev->uprobes;
3223 probe_cache__delete(cache);
3227 static int convert_to_probe_trace_events(struct perf_probe_event *pev,
3228 struct probe_trace_event **tevs)
3232 if (!pev->group && !pev->sdt) {
3233 /* Set group name if not given */
3234 if (!pev->uprobes) {
3235 pev->group = strdup(PERFPROBE_GROUP);
3236 ret = pev->group ? 0 : -ENOMEM;
3238 ret = convert_exec_to_group(pev->target, &pev->group);
3240 pr_warning("Failed to make a group name.\n");
3245 ret = try_to_find_absolute_address(pev, tevs);
3249 /* At first, we need to lookup cache entry */
3250 ret = find_probe_trace_events_from_cache(pev, tevs);
3251 if (ret > 0 || pev->sdt) /* SDT can be found only in the cache */
3252 return ret == 0 ? -ENOENT : ret; /* Found in probe cache */
3254 /* Convert perf_probe_event with debuginfo */
3255 ret = try_to_find_probe_trace_events(pev, tevs);
3257 return ret; /* Found in debuginfo or got an error */
3259 return find_probe_trace_events_from_map(pev, tevs);
3262 int convert_perf_probe_events(struct perf_probe_event *pevs, int npevs)
3266 /* Loop 1: convert all events */
3267 for (i = 0; i < npevs; i++) {
3268 /* Init kprobe blacklist if needed */
3269 if (!pevs[i].uprobes)
3270 kprobe_blacklist__init();
3271 /* Convert with or without debuginfo */
3272 ret = convert_to_probe_trace_events(&pevs[i], &pevs[i].tevs);
3275 pevs[i].ntevs = ret;
3277 /* This just release blacklist only if allocated */
3278 kprobe_blacklist__release();
3283 static int show_probe_trace_event(struct probe_trace_event *tev)
3285 char *buf = synthesize_probe_trace_command(tev);
3288 pr_debug("Failed to synthesize probe trace event.\n");
3292 /* Showing definition always go stdout */
3293 printf("%s\n", buf);
3299 int show_probe_trace_events(struct perf_probe_event *pevs, int npevs)
3301 struct strlist *namelist = strlist__new(NULL, NULL);
3302 struct probe_trace_event *tev;
3303 struct perf_probe_event *pev;
3309 for (j = 0; j < npevs && !ret; j++) {
3311 for (i = 0; i < pev->ntevs && !ret; i++) {
3312 tev = &pev->tevs[i];
3313 /* Skip if the symbol is out of .text or blacklisted */
3314 if (!tev->point.symbol && !pev->uprobes)
3317 /* Set new name for tev (and update namelist) */
3318 ret = probe_trace_event__set_name(tev, pev,
3321 ret = show_probe_trace_event(tev);
3324 strlist__delete(namelist);
3329 int apply_perf_probe_events(struct perf_probe_event *pevs, int npevs)
3333 /* Loop 2: add all events */
3334 for (i = 0; i < npevs; i++) {
3335 ret = __add_probe_trace_events(&pevs[i], pevs[i].tevs,
3337 probe_conf.force_add);
3344 void cleanup_perf_probe_events(struct perf_probe_event *pevs, int npevs)
3348 /* Loop 3: cleanup and free trace events */
3349 for (i = 0; i < npevs; i++) {
3350 for (j = 0; j < pevs[i].ntevs; j++)
3351 clear_probe_trace_event(&pevs[i].tevs[j]);
3352 zfree(&pevs[i].tevs);
3354 clear_perf_probe_event(&pevs[i]);
3358 int add_perf_probe_events(struct perf_probe_event *pevs, int npevs)
3362 ret = init_probe_symbol_maps(pevs->uprobes);
3366 ret = convert_perf_probe_events(pevs, npevs);
3368 ret = apply_perf_probe_events(pevs, npevs);
3370 cleanup_perf_probe_events(pevs, npevs);
3372 exit_probe_symbol_maps();
3376 int del_perf_probe_events(struct strfilter *filter)
3378 int ret, ret2, ufd = -1, kfd = -1;
3379 char *str = strfilter__string(filter);
3384 /* Get current event names */
3385 ret = probe_file__open_both(&kfd, &ufd, PF_FL_RW);
3389 ret = probe_file__del_events(kfd, filter);
3390 if (ret < 0 && ret != -ENOENT)
3393 ret2 = probe_file__del_events(ufd, filter);
3394 if (ret2 < 0 && ret2 != -ENOENT) {
3411 int show_available_funcs(const char *target, struct strfilter *_filter,
3418 ret = init_probe_symbol_maps(user);
3422 /* Get a symbol map */
3423 map = get_target_map(target, user);
3425 pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
3429 ret = map__load(map);
3432 char *str = strfilter__string(_filter);
3433 pr_err("Failed to find symbols matched to \"%s\"\n",
3437 pr_err("Failed to load symbols in %s\n",
3438 (target) ? : "kernel");
3441 if (!dso__sorted_by_name(map->dso, map->type))
3442 dso__sort_by_name(map->dso, map->type);
3444 /* Show all (filtered) symbols */
3447 for (nd = rb_first(&map->dso->symbol_names[map->type]); nd; nd = rb_next(nd)) {
3448 struct symbol_name_rb_node *pos = rb_entry(nd, struct symbol_name_rb_node, rb_node);
3450 if (strfilter__compare(_filter, pos->sym.name))
3451 printf("%s\n", pos->sym.name);
3456 exit_probe_symbol_maps();
3461 int copy_to_probe_trace_arg(struct probe_trace_arg *tvar,
3462 struct perf_probe_arg *pvar)
3464 tvar->value = strdup(pvar->var);
3465 if (tvar->value == NULL)
3468 tvar->type = strdup(pvar->type);
3469 if (tvar->type == NULL)
3473 tvar->name = strdup(pvar->name);
3474 if (tvar->name == NULL)