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.
22 #include <sys/utsname.h>
23 #include <sys/types.h>
43 #include <api/fs/fs.h>
44 #include "trace-event.h" /* For __maybe_unused */
45 #include "probe-event.h"
46 #include "probe-finder.h"
47 #include "probe-file.h"
50 #define MAX_CMDLEN 256
51 #define PERFPROBE_GROUP "probe"
53 bool probe_event_dry_run; /* Dry run flag */
54 struct probe_conf probe_conf;
56 #define semantic_error(msg ...) pr_err("Semantic error :" msg)
58 int e_snprintf(char *str, size_t size, const char *format, ...)
63 ret = vsnprintf(str, size, format, ap);
70 static struct machine *host_machine;
72 /* Initialize symbol maps and path of vmlinux/modules */
73 int init_probe_symbol_maps(bool user_only)
77 symbol_conf.sort_by_name = true;
78 symbol_conf.allow_aliases = true;
79 ret = symbol__init(NULL);
81 pr_debug("Failed to init symbol map.\n");
85 if (host_machine || user_only) /* already initialized */
88 if (symbol_conf.vmlinux_name)
89 pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
91 host_machine = machine__new_host();
93 pr_debug("machine__new_host() failed.\n");
99 pr_warning("Failed to init vmlinux path.\n");
103 void exit_probe_symbol_maps(void)
105 machine__delete(host_machine);
110 static struct symbol *__find_kernel_function_by_name(const char *name,
113 return machine__find_kernel_function_by_name(host_machine, name, mapp);
116 static struct symbol *__find_kernel_function(u64 addr, struct map **mapp)
118 return machine__find_kernel_function(host_machine, addr, mapp);
121 static struct ref_reloc_sym *kernel_get_ref_reloc_sym(void)
123 /* kmap->ref_reloc_sym should be set if host_machine is initialized */
125 struct map *map = machine__kernel_map(host_machine);
127 if (map__load(map) < 0)
130 kmap = map__kmap(map);
133 return kmap->ref_reloc_sym;
136 static int kernel_get_symbol_address_by_name(const char *name, u64 *addr,
137 bool reloc, bool reladdr)
139 struct ref_reloc_sym *reloc_sym;
143 /* ref_reloc_sym is just a label. Need a special fix*/
144 reloc_sym = kernel_get_ref_reloc_sym();
145 if (reloc_sym && strcmp(name, reloc_sym->name) == 0)
146 *addr = (reloc) ? reloc_sym->addr : reloc_sym->unrelocated_addr;
148 sym = __find_kernel_function_by_name(name, &map);
151 *addr = map->unmap_ip(map, sym->start) -
152 ((reloc) ? 0 : map->reloc) -
153 ((reladdr) ? map->start : 0);
158 static struct map *kernel_get_module_map(const char *module)
160 struct map_groups *grp = &host_machine->kmaps;
161 struct maps *maps = &grp->maps[MAP__FUNCTION];
164 /* A file path -- this is an offline module */
165 if (module && strchr(module, '/'))
166 return machine__findnew_module_map(host_machine, 0, module);
171 for (pos = maps__first(maps); pos; pos = map__next(pos)) {
172 /* short_name is "[module]" */
173 if (strncmp(pos->dso->short_name + 1, module,
174 pos->dso->short_name_len - 2) == 0 &&
175 module[pos->dso->short_name_len - 2] == '\0') {
182 struct map *get_target_map(const char *target, bool user)
184 /* Init maps of given executable or kernel */
186 return dso__new_map(target);
188 return kernel_get_module_map(target);
191 static void put_target_map(struct map *map, bool user)
194 /* Only the user map needs to be released */
200 static int convert_exec_to_group(const char *exec, char **result)
202 char *ptr1, *ptr2, *exec_copy;
206 exec_copy = strdup(exec);
210 ptr1 = basename(exec_copy);
216 for (ptr2 = ptr1; ptr2 != '\0'; ptr2++) {
217 if (!isalnum(*ptr2) && *ptr2 != '_') {
223 ret = e_snprintf(buf, 64, "%s_%s", PERFPROBE_GROUP, ptr1);
227 *result = strdup(buf);
228 ret = *result ? 0 : -ENOMEM;
235 static void clear_perf_probe_point(struct perf_probe_point *pp)
242 static void clear_probe_trace_events(struct probe_trace_event *tevs, int ntevs)
246 for (i = 0; i < ntevs; i++)
247 clear_probe_trace_event(tevs + i);
250 static bool kprobe_blacklist__listed(unsigned long address);
251 static bool kprobe_warn_out_range(const char *symbol, unsigned long address)
256 /* Get the address of _etext for checking non-probable text symbol */
257 ret = kernel_get_symbol_address_by_name("_etext", &etext_addr,
260 if (ret == 0 && etext_addr < address)
261 pr_warning("%s is out of .text, skip it.\n", symbol);
262 else if (kprobe_blacklist__listed(address))
263 pr_warning("%s is blacklisted function, skip it.\n", symbol);
272 * '.gnu.linkonce.this_module' section of kernel module elf directly
273 * maps to 'struct module' from linux/module.h. This section contains
274 * actual module name which will be used by kernel after loading it.
275 * But, we cannot use 'struct module' here since linux/module.h is not
276 * exposed to user-space. Offset of 'name' has remained same from long
277 * time, so hardcoding it here.
280 #define MOD_NAME_OFFSET 24
282 #define MOD_NAME_OFFSET 12
286 * @module can be module name of module file path. In case of path,
287 * inspect elf and find out what is actual module name.
288 * Caller has to free mod_name after using it.
290 static char *find_module_name(const char *module)
298 char *mod_name = NULL;
300 fd = open(module, O_RDONLY);
304 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
308 if (gelf_getehdr(elf, &ehdr) == NULL)
311 sec = elf_section_by_name(elf, &ehdr, &shdr,
312 ".gnu.linkonce.this_module", NULL);
316 data = elf_getdata(sec, NULL);
317 if (!data || !data->d_buf)
320 mod_name = strdup((char *)data->d_buf + MOD_NAME_OFFSET);
329 #ifdef HAVE_DWARF_SUPPORT
331 static int kernel_get_module_dso(const char *module, struct dso **pdso)
335 const char *vmlinux_name;
339 char module_name[128];
341 snprintf(module_name, sizeof(module_name), "[%s]", module);
342 map = map_groups__find_by_name(&host_machine->kmaps, MAP__FUNCTION, module_name);
347 pr_debug("Failed to find module %s.\n", module);
351 map = machine__kernel_map(host_machine);
354 vmlinux_name = symbol_conf.vmlinux_name;
357 ret = dso__load_vmlinux(dso, map, vmlinux_name, false);
359 ret = dso__load_vmlinux_path(dso, map);
366 * Some binaries like glibc have special symbols which are on the symbol
367 * table, but not in the debuginfo. If we can find the address of the
368 * symbol from map, we can translate the address back to the probe point.
370 static int find_alternative_probe_point(struct debuginfo *dinfo,
371 struct perf_probe_point *pp,
372 struct perf_probe_point *result,
373 const char *target, bool uprobes)
375 struct map *map = NULL;
380 /* This can work only for function-name based one */
381 if (!pp->function || pp->file)
384 map = get_target_map(target, uprobes);
388 /* Find the address of given function */
389 map__for_each_symbol_by_name(map, pp->function, sym) {
391 address = sym->start;
393 address = map->unmap_ip(map, sym->start) - map->reloc;
400 pr_debug("Symbol %s address found : %" PRIx64 "\n",
401 pp->function, address);
403 ret = debuginfo__find_probe_point(dinfo, (unsigned long)address,
406 ret = (!ret) ? -ENOENT : ret;
408 result->offset += pp->offset;
409 result->line += pp->line;
410 result->retprobe = pp->retprobe;
415 put_target_map(map, uprobes);
420 static int get_alternative_probe_event(struct debuginfo *dinfo,
421 struct perf_probe_event *pev,
422 struct perf_probe_point *tmp)
426 memcpy(tmp, &pev->point, sizeof(*tmp));
427 memset(&pev->point, 0, sizeof(pev->point));
428 ret = find_alternative_probe_point(dinfo, tmp, &pev->point,
429 pev->target, pev->uprobes);
431 memcpy(&pev->point, tmp, sizeof(*tmp));
436 static int get_alternative_line_range(struct debuginfo *dinfo,
437 struct line_range *lr,
438 const char *target, bool user)
440 struct perf_probe_point pp = { .function = lr->function,
443 struct perf_probe_point result;
446 memset(&result, 0, sizeof(result));
448 if (lr->end != INT_MAX)
449 len = lr->end - lr->start;
450 ret = find_alternative_probe_point(dinfo, &pp, &result,
453 lr->function = result.function;
454 lr->file = result.file;
455 lr->start = result.line;
456 if (lr->end != INT_MAX)
457 lr->end = lr->start + len;
458 clear_perf_probe_point(&pp);
463 /* Open new debuginfo of given module */
464 static struct debuginfo *open_debuginfo(const char *module, bool silent)
466 const char *path = module;
467 char reason[STRERR_BUFSIZE];
468 struct debuginfo *ret = NULL;
469 struct dso *dso = NULL;
472 if (!module || !strchr(module, '/')) {
473 err = kernel_get_module_dso(module, &dso);
475 if (!dso || dso->load_errno == 0) {
476 if (!str_error_r(-err, reason, STRERR_BUFSIZE))
477 strcpy(reason, "(unknown)");
479 dso__strerror_load(dso, reason, STRERR_BUFSIZE);
481 pr_err("Failed to find the path for %s: %s\n",
482 module ?: "kernel", reason);
485 path = dso->long_name;
487 ret = debuginfo__new(path);
488 if (!ret && !silent) {
489 pr_warning("The %s file has no debug information.\n", path);
490 if (!module || !strtailcmp(path, ".ko"))
491 pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, ");
493 pr_warning("Rebuild with -g, ");
494 pr_warning("or install an appropriate debuginfo package.\n");
499 /* For caching the last debuginfo */
500 static struct debuginfo *debuginfo_cache;
501 static char *debuginfo_cache_path;
503 static struct debuginfo *debuginfo_cache__open(const char *module, bool silent)
505 const char *path = module;
507 /* If the module is NULL, it should be the kernel. */
511 if (debuginfo_cache_path && !strcmp(debuginfo_cache_path, path))
514 /* Copy module path */
515 free(debuginfo_cache_path);
516 debuginfo_cache_path = strdup(path);
517 if (!debuginfo_cache_path) {
518 debuginfo__delete(debuginfo_cache);
519 debuginfo_cache = NULL;
523 debuginfo_cache = open_debuginfo(module, silent);
524 if (!debuginfo_cache)
525 zfree(&debuginfo_cache_path);
527 return debuginfo_cache;
530 static void debuginfo_cache__exit(void)
532 debuginfo__delete(debuginfo_cache);
533 debuginfo_cache = NULL;
534 zfree(&debuginfo_cache_path);
538 static int get_text_start_address(const char *exec, unsigned long *address)
543 int fd, ret = -ENOENT;
545 fd = open(exec, O_RDONLY);
549 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
555 if (gelf_getehdr(elf, &ehdr) == NULL)
558 if (!elf_section_by_name(elf, &ehdr, &shdr, ".text", NULL))
561 *address = shdr.sh_addr - shdr.sh_offset;
572 * Convert trace point to probe point with debuginfo
574 static int find_perf_probe_point_from_dwarf(struct probe_trace_point *tp,
575 struct perf_probe_point *pp,
578 struct debuginfo *dinfo = NULL;
579 unsigned long stext = 0;
580 u64 addr = tp->address;
583 /* convert the address to dwarf address */
589 ret = get_text_start_address(tp->module, &stext);
593 } else if (tp->symbol) {
594 /* If the module is given, this returns relative address */
595 ret = kernel_get_symbol_address_by_name(tp->symbol, &addr,
596 false, !!tp->module);
602 pr_debug("try to find information at %" PRIx64 " in %s\n", addr,
603 tp->module ? : "kernel");
605 dinfo = debuginfo_cache__open(tp->module, verbose == 0);
607 ret = debuginfo__find_probe_point(dinfo,
608 (unsigned long)addr, pp);
613 pp->retprobe = tp->retprobe;
617 pr_debug("Failed to find corresponding probes from debuginfo.\n");
618 return ret ? : -ENOENT;
621 static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs,
622 int ntevs, const char *exec)
625 unsigned long stext = 0;
630 ret = get_text_start_address(exec, &stext);
634 for (i = 0; i < ntevs && ret >= 0; i++) {
635 /* point.address is the addres of point.symbol + point.offset */
636 tevs[i].point.address -= stext;
637 tevs[i].point.module = strdup(exec);
638 if (!tevs[i].point.module) {
642 tevs[i].uprobes = true;
648 static int add_module_to_probe_trace_events(struct probe_trace_event *tevs,
649 int ntevs, const char *module)
652 char *mod_name = NULL;
657 mod_name = find_module_name(module);
659 for (i = 0; i < ntevs; i++) {
660 tevs[i].point.module =
661 strdup(mod_name ? mod_name : module);
662 if (!tevs[i].point.module) {
673 post_process_kernel_probe_trace_events(struct probe_trace_event *tevs,
676 struct ref_reloc_sym *reloc_sym;
680 /* Skip post process if the target is an offline kernel */
681 if (symbol_conf.ignore_vmlinux_buildid)
684 reloc_sym = kernel_get_ref_reloc_sym();
686 pr_warning("Relocated base symbol is not found!\n");
690 for (i = 0; i < ntevs; i++) {
691 if (!tevs[i].point.address || tevs[i].point.retprobe)
693 /* If we found a wrong one, mark it by NULL symbol */
694 if (kprobe_warn_out_range(tevs[i].point.symbol,
695 tevs[i].point.address)) {
699 tmp = strdup(reloc_sym->name);
703 /* If we have no realname, use symbol for it */
704 if (!tevs[i].point.realname)
705 tevs[i].point.realname = tevs[i].point.symbol;
707 free(tevs[i].point.symbol);
708 tevs[i].point.symbol = tmp;
709 tevs[i].point.offset = tevs[i].point.address -
710 reloc_sym->unrelocated_addr;
716 arch__post_process_probe_trace_events(struct perf_probe_event *pev __maybe_unused,
717 int ntevs __maybe_unused)
721 /* Post processing the probe events */
722 static int post_process_probe_trace_events(struct perf_probe_event *pev,
723 struct probe_trace_event *tevs,
724 int ntevs, const char *module,
730 ret = add_exec_to_probe_trace_events(tevs, ntevs, module);
732 /* Currently ref_reloc_sym based probe is not for drivers */
733 ret = add_module_to_probe_trace_events(tevs, ntevs, module);
735 ret = post_process_kernel_probe_trace_events(tevs, ntevs);
738 arch__post_process_probe_trace_events(pev, ntevs);
743 /* Try to find perf_probe_event with debuginfo */
744 static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
745 struct probe_trace_event **tevs)
747 bool need_dwarf = perf_probe_event_need_dwarf(pev);
748 struct perf_probe_point tmp;
749 struct debuginfo *dinfo;
752 dinfo = open_debuginfo(pev->target, !need_dwarf);
756 pr_debug("Could not open debuginfo. Try to use symbols.\n");
760 pr_debug("Try to find probe point from debuginfo.\n");
761 /* Searching trace events corresponding to a probe event */
762 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
764 if (ntevs == 0) { /* Not found, retry with an alternative */
765 ret = get_alternative_probe_event(dinfo, pev, &tmp);
767 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
769 * Write back to the original probe_event for
770 * setting appropriate (user given) event name
772 clear_perf_probe_point(&pev->point);
773 memcpy(&pev->point, &tmp, sizeof(tmp));
777 debuginfo__delete(dinfo);
779 if (ntevs > 0) { /* Succeeded to find trace events */
780 pr_debug("Found %d probe_trace_events.\n", ntevs);
781 ret = post_process_probe_trace_events(pev, *tevs, ntevs,
782 pev->target, pev->uprobes);
783 if (ret < 0 || ret == ntevs) {
784 clear_probe_trace_events(*tevs, ntevs);
788 return ret < 0 ? ret : ntevs;
793 if (ntevs == 0) { /* No error but failed to find probe point. */
794 pr_warning("Probe point '%s' not found.\n",
795 synthesize_perf_probe_point(&pev->point));
798 /* Error path : ntevs < 0 */
799 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
802 pr_warning("Warning: No dwarf info found in the vmlinux - "
803 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
805 pr_debug("Trying to use symbols.\n");
812 #define LINEBUF_SIZE 256
813 #define NR_ADDITIONAL_LINES 2
815 static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
817 char buf[LINEBUF_SIZE], sbuf[STRERR_BUFSIZE];
818 const char *color = show_num ? "" : PERF_COLOR_BLUE;
819 const char *prefix = NULL;
822 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
827 prefix = show_num ? "%7d " : " ";
828 color_fprintf(stdout, color, prefix, l);
830 color_fprintf(stdout, color, "%s", buf);
832 } while (strchr(buf, '\n') == NULL);
837 pr_warning("File read error: %s\n",
838 str_error_r(errno, sbuf, sizeof(sbuf)));
844 static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
846 int rv = __show_one_line(fp, l, skip, show_num);
848 pr_warning("Source file is shorter than expected.\n");
854 #define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
855 #define show_one_line(f,l) _show_one_line(f,l,false,false)
856 #define skip_one_line(f,l) _show_one_line(f,l,true,false)
857 #define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
860 * Show line-range always requires debuginfo to find source file and
863 static int __show_line_range(struct line_range *lr, const char *module,
868 struct debuginfo *dinfo;
872 char sbuf[STRERR_BUFSIZE];
874 /* Search a line range */
875 dinfo = open_debuginfo(module, false);
879 ret = debuginfo__find_line_range(dinfo, lr);
880 if (!ret) { /* Not found, retry with an alternative */
881 ret = get_alternative_line_range(dinfo, lr, module, user);
883 ret = debuginfo__find_line_range(dinfo, lr);
885 debuginfo__delete(dinfo);
886 if (ret == 0 || ret == -ENOENT) {
887 pr_warning("Specified source line is not found.\n");
889 } else if (ret < 0) {
890 pr_warning("Debuginfo analysis failed.\n");
894 /* Convert source file path */
896 ret = get_real_path(tmp, lr->comp_dir, &lr->path);
898 /* Free old path when new path is assigned */
903 pr_warning("Failed to find source file path.\n");
910 fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
911 lr->start - lr->offset);
913 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
915 fp = fopen(lr->path, "r");
917 pr_warning("Failed to open %s: %s\n", lr->path,
918 str_error_r(errno, sbuf, sizeof(sbuf)));
921 /* Skip to starting line number */
922 while (l < lr->start) {
923 ret = skip_one_line(fp, l++);
928 intlist__for_each_entry(ln, lr->line_list) {
929 for (; ln->i > l; l++) {
930 ret = show_one_line(fp, l - lr->offset);
934 ret = show_one_line_with_num(fp, l++ - lr->offset);
939 if (lr->end == INT_MAX)
940 lr->end = l + NR_ADDITIONAL_LINES;
941 while (l <= lr->end) {
942 ret = show_one_line_or_eof(fp, l++ - lr->offset);
951 int show_line_range(struct line_range *lr, const char *module, bool user)
955 ret = init_probe_symbol_maps(user);
958 ret = __show_line_range(lr, module, user);
959 exit_probe_symbol_maps();
964 static int show_available_vars_at(struct debuginfo *dinfo,
965 struct perf_probe_event *pev,
966 struct strfilter *_filter)
970 struct str_node *node;
971 struct variable_list *vls = NULL, *vl;
972 struct perf_probe_point tmp;
975 buf = synthesize_perf_probe_point(&pev->point);
978 pr_debug("Searching variables at %s\n", buf);
980 ret = debuginfo__find_available_vars_at(dinfo, pev, &vls);
981 if (!ret) { /* Not found, retry with an alternative */
982 ret = get_alternative_probe_event(dinfo, pev, &tmp);
984 ret = debuginfo__find_available_vars_at(dinfo, pev,
986 /* Release the old probe_point */
987 clear_perf_probe_point(&tmp);
991 if (ret == 0 || ret == -ENOENT) {
992 pr_err("Failed to find the address of %s\n", buf);
995 pr_warning("Debuginfo analysis failed.\n");
999 /* Some variables are found */
1000 fprintf(stdout, "Available variables at %s\n", buf);
1001 for (i = 0; i < ret; i++) {
1004 * A probe point might be converted to
1005 * several trace points.
1007 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
1009 zfree(&vl->point.symbol);
1012 strlist__for_each_entry(node, vl->vars) {
1013 var = strchr(node->s, '\t') + 1;
1014 if (strfilter__compare(_filter, var)) {
1015 fprintf(stdout, "\t\t%s\n", node->s);
1019 strlist__delete(vl->vars);
1022 fprintf(stdout, "\t\t(No matched variables)\n");
1030 /* Show available variables on given probe point */
1031 int show_available_vars(struct perf_probe_event *pevs, int npevs,
1032 struct strfilter *_filter)
1035 struct debuginfo *dinfo;
1037 ret = init_probe_symbol_maps(pevs->uprobes);
1041 dinfo = open_debuginfo(pevs->target, false);
1049 for (i = 0; i < npevs && ret >= 0; i++)
1050 ret = show_available_vars_at(dinfo, &pevs[i], _filter);
1052 debuginfo__delete(dinfo);
1054 exit_probe_symbol_maps();
1058 #else /* !HAVE_DWARF_SUPPORT */
1060 static void debuginfo_cache__exit(void)
1065 find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused,
1066 struct perf_probe_point *pp __maybe_unused,
1067 bool is_kprobe __maybe_unused)
1072 static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
1073 struct probe_trace_event **tevs __maybe_unused)
1075 if (perf_probe_event_need_dwarf(pev)) {
1076 pr_warning("Debuginfo-analysis is not supported.\n");
1083 int show_line_range(struct line_range *lr __maybe_unused,
1084 const char *module __maybe_unused,
1085 bool user __maybe_unused)
1087 pr_warning("Debuginfo-analysis is not supported.\n");
1091 int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
1092 int npevs __maybe_unused,
1093 struct strfilter *filter __maybe_unused)
1095 pr_warning("Debuginfo-analysis is not supported.\n");
1100 void line_range__clear(struct line_range *lr)
1106 intlist__delete(lr->line_list);
1107 memset(lr, 0, sizeof(*lr));
1110 int line_range__init(struct line_range *lr)
1112 memset(lr, 0, sizeof(*lr));
1113 lr->line_list = intlist__new(NULL);
1120 static int parse_line_num(char **ptr, int *val, const char *what)
1122 const char *start = *ptr;
1125 *val = strtol(*ptr, ptr, 0);
1126 if (errno || *ptr == start) {
1127 semantic_error("'%s' is not a valid number.\n", what);
1133 /* Check the name is good for event, group or function */
1134 static bool is_c_func_name(const char *name)
1136 if (!isalpha(*name) && *name != '_')
1138 while (*++name != '\0') {
1139 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
1146 * Stuff 'lr' according to the line range described by 'arg'.
1147 * The line range syntax is described by:
1149 * SRC[:SLN[+NUM|-ELN]]
1150 * FNC[@SRC][:SLN[+NUM|-ELN]]
1152 int parse_line_range_desc(const char *arg, struct line_range *lr)
1154 char *range, *file, *name = strdup(arg);
1163 range = strchr(name, ':');
1167 err = parse_line_num(&range, &lr->start, "start line");
1171 if (*range == '+' || *range == '-') {
1172 const char c = *range++;
1174 err = parse_line_num(&range, &lr->end, "end line");
1179 lr->end += lr->start;
1181 * Adjust the number of lines here.
1182 * If the number of lines == 1, the
1183 * the end of line should be equal to
1184 * the start of line.
1190 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
1193 if (lr->start > lr->end) {
1194 semantic_error("Start line must be smaller"
1195 " than end line.\n");
1198 if (*range != '\0') {
1199 semantic_error("Tailing with invalid str '%s'.\n", range);
1204 file = strchr(name, '@');
1207 lr->file = strdup(++file);
1208 if (lr->file == NULL) {
1212 lr->function = name;
1213 } else if (strchr(name, '/') || strchr(name, '.'))
1215 else if (is_c_func_name(name))/* We reuse it for checking funcname */
1216 lr->function = name;
1217 else { /* Invalid name */
1218 semantic_error("'%s' is not a valid function name.\n", name);
1229 static int parse_perf_probe_event_name(char **arg, struct perf_probe_event *pev)
1233 ptr = strchr(*arg, ':');
1236 if (!pev->sdt && !is_c_func_name(*arg))
1238 pev->group = strdup(*arg);
1244 if (!pev->sdt && !is_c_func_name(*arg)) {
1246 semantic_error("%s is bad for event name -it must "
1247 "follow C symbol-naming rule.\n", *arg);
1250 pev->event = strdup(*arg);
1251 if (pev->event == NULL)
1257 /* Parse probepoint definition. */
1258 static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
1260 struct perf_probe_point *pp = &pev->point;
1263 bool file_spec = false;
1268 * perf probe [GRP:][EVENT=]SRC[:LN|;PTN]
1269 * perf probe [GRP:][EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
1270 * perf probe %[GRP:]SDT_EVENT
1276 * If the probe point starts with '%',
1277 * or starts with "sdt_" and has a ':' but no '=',
1278 * then it should be a SDT/cached probe point.
1280 if (arg[0] == '%' ||
1281 (!strncmp(arg, "sdt_", 4) &&
1282 !!strchr(arg, ':') && !strchr(arg, '='))) {
1288 ptr = strpbrk(arg, ";=@+%");
1292 semantic_error("%s must be an SDT name.\n",
1296 /* This must be a target file name or build id */
1297 tmp = build_id_cache__complement(ptr + 1);
1299 pev->target = build_id_cache__origname(tmp);
1302 pev->target = strdup(ptr + 1);
1307 ret = parse_perf_probe_event_name(&arg, pev);
1309 if (asprintf(&pev->point.function, "%%%s", pev->event) < 0)
1315 if (ptr && *ptr == '=') { /* Event name */
1318 ret = parse_perf_probe_event_name(&arg, pev);
1326 * Check arg is function or file name and copy it.
1328 * We consider arg to be a file spec if and only if it satisfies
1329 * all of the below criteria::
1330 * - it does not include any of "+@%",
1331 * - it includes one of ":;", and
1332 * - it has a period '.' in the name.
1334 * Otherwise, we consider arg to be a function specification.
1336 if (!strpbrk(arg, "+@%") && (ptr = strpbrk(arg, ";:")) != NULL) {
1337 /* This is a file spec if it includes a '.' before ; or : */
1338 if (memchr(arg, '.', ptr - arg))
1342 ptr = strpbrk(arg, ";:+@%");
1362 * Keep pp->function even if this is absolute address,
1363 * so it can mark whether abs_address is valid.
1364 * Which make 'perf probe lib.bin 0x0' possible.
1366 * Note that checking length of tmp is not needed
1367 * because when we access tmp[1] we know tmp[0] is '0',
1368 * so tmp[1] should always valid (but could be '\0').
1370 if (tmp && !strncmp(tmp, "0x", 2)) {
1371 pp->abs_address = strtoul(pp->function, &tmp, 0);
1373 semantic_error("Invalid absolute address.\n");
1379 /* Parse other options */
1383 if (c == ';') { /* Lazy pattern must be the last part */
1384 pp->lazy_line = strdup(arg);
1385 if (pp->lazy_line == NULL)
1389 ptr = strpbrk(arg, ";:+@%");
1395 case ':': /* Line number */
1396 pp->line = strtoul(arg, &tmp, 0);
1398 semantic_error("There is non-digit char"
1399 " in line number.\n");
1403 case '+': /* Byte offset from a symbol */
1404 pp->offset = strtoul(arg, &tmp, 0);
1406 semantic_error("There is non-digit character"
1411 case '@': /* File name */
1413 semantic_error("SRC@SRC is not allowed.\n");
1416 pp->file = strdup(arg);
1417 if (pp->file == NULL)
1420 case '%': /* Probe places */
1421 if (strcmp(arg, "return") == 0) {
1423 } else { /* Others not supported yet */
1424 semantic_error("%%%s is not supported.\n", arg);
1428 default: /* Buggy case */
1429 pr_err("This program has a bug at %s:%d.\n",
1430 __FILE__, __LINE__);
1436 /* Exclusion check */
1437 if (pp->lazy_line && pp->line) {
1438 semantic_error("Lazy pattern can't be used with"
1443 if (pp->lazy_line && pp->offset) {
1444 semantic_error("Lazy pattern can't be used with offset.\n");
1448 if (pp->line && pp->offset) {
1449 semantic_error("Offset can't be used with line number.\n");
1453 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
1454 semantic_error("File always requires line number or "
1459 if (pp->offset && !pp->function) {
1460 semantic_error("Offset requires an entry function.\n");
1464 if (pp->retprobe && !pp->function) {
1465 semantic_error("Return probe requires an entry function.\n");
1469 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
1470 semantic_error("Offset/Line/Lazy pattern can't be used with "
1475 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
1476 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
1481 /* Parse perf-probe event argument */
1482 static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
1484 char *tmp, *goodname;
1485 struct perf_probe_arg_field **fieldp;
1487 pr_debug("parsing arg: %s into ", str);
1489 tmp = strchr(str, '=');
1491 arg->name = strndup(str, tmp - str);
1492 if (arg->name == NULL)
1494 pr_debug("name:%s ", arg->name);
1498 tmp = strchr(str, ':');
1499 if (tmp) { /* Type setting */
1501 arg->type = strdup(tmp + 1);
1502 if (arg->type == NULL)
1504 pr_debug("type:%s ", arg->type);
1507 tmp = strpbrk(str, "-.[");
1508 if (!is_c_varname(str) || !tmp) {
1509 /* A variable, register, symbol or special value */
1510 arg->var = strdup(str);
1511 if (arg->var == NULL)
1513 pr_debug("%s\n", arg->var);
1517 /* Structure fields or array element */
1518 arg->var = strndup(str, tmp - str);
1519 if (arg->var == NULL)
1521 goodname = arg->var;
1522 pr_debug("%s, ", arg->var);
1523 fieldp = &arg->field;
1526 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
1527 if (*fieldp == NULL)
1529 if (*tmp == '[') { /* Array */
1531 (*fieldp)->index = strtol(str + 1, &tmp, 0);
1532 (*fieldp)->ref = true;
1533 if (*tmp != ']' || tmp == str + 1) {
1534 semantic_error("Array index must be a"
1541 } else { /* Structure */
1544 (*fieldp)->ref = false;
1545 } else if (tmp[1] == '>') {
1547 (*fieldp)->ref = true;
1549 semantic_error("Argument parse error: %s\n",
1553 tmp = strpbrk(str, "-.[");
1556 (*fieldp)->name = strndup(str, tmp - str);
1557 if ((*fieldp)->name == NULL)
1560 goodname = (*fieldp)->name;
1561 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
1562 fieldp = &(*fieldp)->next;
1565 (*fieldp)->name = strdup(str);
1566 if ((*fieldp)->name == NULL)
1569 goodname = (*fieldp)->name;
1570 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
1572 /* If no name is specified, set the last field name (not array index)*/
1574 arg->name = strdup(goodname);
1575 if (arg->name == NULL)
1581 /* Parse perf-probe event command */
1582 int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
1585 int argc, i, ret = 0;
1587 argv = argv_split(cmd, &argc);
1589 pr_debug("Failed to split arguments.\n");
1592 if (argc - 1 > MAX_PROBE_ARGS) {
1593 semantic_error("Too many probe arguments (%d).\n", argc - 1);
1597 /* Parse probe point */
1598 ret = parse_perf_probe_point(argv[0], pev);
1602 /* Copy arguments and ensure return probe has no C argument */
1603 pev->nargs = argc - 1;
1604 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1605 if (pev->args == NULL) {
1609 for (i = 0; i < pev->nargs && ret >= 0; i++) {
1610 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1612 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
1613 semantic_error("You can't specify local variable for"
1624 /* Returns true if *any* ARG is either C variable, $params or $vars. */
1625 bool perf_probe_with_var(struct perf_probe_event *pev)
1629 for (i = 0; i < pev->nargs; i++)
1630 if (is_c_varname(pev->args[i].var) ||
1631 !strcmp(pev->args[i].var, PROBE_ARG_PARAMS) ||
1632 !strcmp(pev->args[i].var, PROBE_ARG_VARS))
1637 /* Return true if this perf_probe_event requires debuginfo */
1638 bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
1640 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1643 if (perf_probe_with_var(pev))
1649 /* Parse probe_events event into struct probe_point */
1650 int parse_probe_trace_command(const char *cmd, struct probe_trace_event *tev)
1652 struct probe_trace_point *tp = &tev->point;
1655 char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
1659 pr_debug("Parsing probe_events: %s\n", cmd);
1660 argv = argv_split(cmd, &argc);
1662 pr_debug("Failed to split arguments.\n");
1666 semantic_error("Too few probe arguments.\n");
1671 /* Scan event and group name. */
1672 argv0_str = strdup(argv[0]);
1673 if (argv0_str == NULL) {
1677 fmt1_str = strtok_r(argv0_str, ":", &fmt);
1678 fmt2_str = strtok_r(NULL, "/", &fmt);
1679 fmt3_str = strtok_r(NULL, " \t", &fmt);
1680 if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
1681 || fmt3_str == NULL) {
1682 semantic_error("Failed to parse event name: %s\n", argv[0]);
1687 tev->group = strdup(fmt2_str);
1688 tev->event = strdup(fmt3_str);
1689 if (tev->group == NULL || tev->event == NULL) {
1693 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
1695 tp->retprobe = (pr == 'r');
1697 /* Scan module name(if there), function name and offset */
1698 p = strchr(argv[1], ':');
1700 tp->module = strndup(argv[1], p - argv[1]);
1705 tev->uprobes = (tp->module[0] == '/');
1709 fmt1_str = strtok_r(p, "+", &fmt);
1710 /* only the address started with 0x */
1711 if (fmt1_str[0] == '0') {
1713 * Fix a special case:
1714 * if address == 0, kernel reports something like:
1715 * p:probe_libc/abs_0 /lib/libc-2.18.so:0x (null) arg1=%ax
1716 * Newer kernel may fix that, but we want to
1717 * support old kernel also.
1719 if (strcmp(fmt1_str, "0x") == 0) {
1720 if (!argv[2] || strcmp(argv[2], "(null)")) {
1727 for (i = 2; argv[i + 1] != NULL; i++)
1728 argv[i] = argv[i + 1];
1733 tp->address = strtoul(fmt1_str, NULL, 0);
1735 /* Only the symbol-based probe has offset */
1736 tp->symbol = strdup(fmt1_str);
1737 if (tp->symbol == NULL) {
1741 fmt2_str = strtok_r(NULL, "", &fmt);
1742 if (fmt2_str == NULL)
1745 tp->offset = strtoul(fmt2_str, NULL, 10);
1748 tev->nargs = argc - 2;
1749 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
1750 if (tev->args == NULL) {
1754 for (i = 0; i < tev->nargs; i++) {
1755 p = strchr(argv[i + 2], '=');
1756 if (p) /* We don't need which register is assigned. */
1760 tev->args[i].name = strdup(argv[i + 2]);
1761 /* TODO: parse regs and offset */
1762 tev->args[i].value = strdup(p);
1763 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1775 /* Compose only probe arg */
1776 char *synthesize_perf_probe_arg(struct perf_probe_arg *pa)
1778 struct perf_probe_arg_field *field = pa->field;
1783 if (strbuf_init(&buf, 64) < 0)
1786 if (pa->name && pa->var)
1787 err = strbuf_addf(&buf, "%s=%s", pa->name, pa->var);
1789 err = strbuf_addstr(&buf, pa->name ?: pa->var);
1794 if (field->name[0] == '[')
1795 err = strbuf_addstr(&buf, field->name);
1797 err = strbuf_addf(&buf, "%s%s", field->ref ? "->" : ".",
1799 field = field->next;
1805 if (strbuf_addf(&buf, ":%s", pa->type) < 0)
1808 ret = strbuf_detach(&buf, NULL);
1810 strbuf_release(&buf);
1814 /* Compose only probe point (not argument) */
1815 char *synthesize_perf_probe_point(struct perf_probe_point *pp)
1818 char *tmp, *ret = NULL;
1821 if (strbuf_init(&buf, 64) < 0)
1825 if (strbuf_addstr(&buf, pp->function) < 0)
1828 err = strbuf_addf(&buf, "+%lu", pp->offset);
1830 err = strbuf_addf(&buf, ":%d", pp->line);
1831 else if (pp->retprobe)
1832 err = strbuf_addstr(&buf, "%return");
1840 tmp = strchr(pp->file + len - 30, '/');
1841 tmp = tmp ? tmp + 1 : pp->file + len - 30;
1843 err = strbuf_addf(&buf, "@%s", tmp);
1844 if (!err && !pp->function && pp->line)
1845 err = strbuf_addf(&buf, ":%d", pp->line);
1848 ret = strbuf_detach(&buf, NULL);
1850 strbuf_release(&buf);
1854 char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1857 char *tmp, *ret = NULL;
1860 if (strbuf_init(&buf, 64))
1863 if (strbuf_addf(&buf, "%s:%s=", pev->group ?: PERFPROBE_GROUP,
1867 tmp = synthesize_perf_probe_point(&pev->point);
1868 if (!tmp || strbuf_addstr(&buf, tmp) < 0)
1872 for (i = 0; i < pev->nargs; i++) {
1873 tmp = synthesize_perf_probe_arg(pev->args + i);
1874 if (!tmp || strbuf_addf(&buf, " %s", tmp) < 0)
1879 ret = strbuf_detach(&buf, NULL);
1881 strbuf_release(&buf);
1885 static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
1886 struct strbuf *buf, int depth)
1890 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
1895 err = strbuf_addf(buf, "%+ld(", ref->offset);
1896 return (err < 0) ? err : depth;
1899 static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
1902 struct probe_trace_arg_ref *ref = arg->ref;
1905 /* Argument name or separator */
1907 err = strbuf_addf(buf, " %s=", arg->name);
1909 err = strbuf_addch(buf, ' ');
1913 /* Special case: @XXX */
1914 if (arg->value[0] == '@' && arg->ref)
1917 /* Dereferencing arguments */
1919 depth = __synthesize_probe_trace_arg_ref(ref, buf, 1);
1924 /* Print argument value */
1925 if (arg->value[0] == '@' && arg->ref)
1926 err = strbuf_addf(buf, "%s%+ld", arg->value, arg->ref->offset);
1928 err = strbuf_addstr(buf, arg->value);
1931 while (!err && depth--)
1932 err = strbuf_addch(buf, ')');
1934 /* Print argument type */
1935 if (!err && arg->type)
1936 err = strbuf_addf(buf, ":%s", arg->type);
1941 char *synthesize_probe_trace_command(struct probe_trace_event *tev)
1943 struct probe_trace_point *tp = &tev->point;
1948 /* Uprobes must have tp->module */
1949 if (tev->uprobes && !tp->module)
1952 if (strbuf_init(&buf, 32) < 0)
1955 if (strbuf_addf(&buf, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
1956 tev->group, tev->event) < 0)
1959 * If tp->address == 0, then this point must be a
1960 * absolute address uprobe.
1961 * try_to_find_absolute_address() should have made
1962 * tp->symbol to "0x0".
1964 if (tev->uprobes && !tp->address) {
1965 if (!tp->symbol || strcmp(tp->symbol, "0x0"))
1969 /* Use the tp->address for uprobes */
1971 err = strbuf_addf(&buf, "%s:0x%lx", tp->module, tp->address);
1972 else if (!strncmp(tp->symbol, "0x", 2))
1973 /* Absolute address. See try_to_find_absolute_address() */
1974 err = strbuf_addf(&buf, "%s%s0x%lx", tp->module ?: "",
1975 tp->module ? ":" : "", tp->address);
1977 err = strbuf_addf(&buf, "%s%s%s+%lu", tp->module ?: "",
1978 tp->module ? ":" : "", tp->symbol, tp->offset);
1982 for (i = 0; i < tev->nargs; i++)
1983 if (synthesize_probe_trace_arg(&tev->args[i], &buf) < 0)
1986 ret = strbuf_detach(&buf, NULL);
1988 strbuf_release(&buf);
1992 static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
1993 struct perf_probe_point *pp,
1996 struct symbol *sym = NULL;
1998 u64 addr = tp->address;
2002 map = dso__new_map(tp->module);
2005 sym = map__find_symbol(map, addr);
2007 if (tp->symbol && !addr) {
2008 if (kernel_get_symbol_address_by_name(tp->symbol,
2009 &addr, true, false) < 0)
2014 sym = __find_kernel_function(addr, &map);
2021 pp->retprobe = tp->retprobe;
2022 pp->offset = addr - map->unmap_ip(map, sym->start);
2023 pp->function = strdup(sym->name);
2024 ret = pp->function ? 0 : -ENOMEM;
2027 if (map && !is_kprobe) {
2034 static int convert_to_perf_probe_point(struct probe_trace_point *tp,
2035 struct perf_probe_point *pp,
2041 ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe);
2044 ret = find_perf_probe_point_from_map(tp, pp, is_kprobe);
2048 pr_debug("Failed to find probe point from both of dwarf and map.\n");
2051 pp->function = strdup(tp->symbol);
2052 pp->offset = tp->offset;
2054 ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address);
2057 pp->function = strdup(buf);
2060 if (pp->function == NULL)
2063 pp->retprobe = tp->retprobe;
2068 static int convert_to_perf_probe_event(struct probe_trace_event *tev,
2069 struct perf_probe_event *pev, bool is_kprobe)
2071 struct strbuf buf = STRBUF_INIT;
2074 /* Convert event/group name */
2075 pev->event = strdup(tev->event);
2076 pev->group = strdup(tev->group);
2077 if (pev->event == NULL || pev->group == NULL)
2080 /* Convert trace_point to probe_point */
2081 ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
2085 /* Convert trace_arg to probe_arg */
2086 pev->nargs = tev->nargs;
2087 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
2088 if (pev->args == NULL)
2090 for (i = 0; i < tev->nargs && ret >= 0; i++) {
2091 if (tev->args[i].name)
2092 pev->args[i].name = strdup(tev->args[i].name);
2094 if ((ret = strbuf_init(&buf, 32)) < 0)
2096 ret = synthesize_probe_trace_arg(&tev->args[i], &buf);
2097 pev->args[i].name = strbuf_detach(&buf, NULL);
2099 if (pev->args[i].name == NULL && ret >= 0)
2104 clear_perf_probe_event(pev);
2109 void clear_perf_probe_event(struct perf_probe_event *pev)
2111 struct perf_probe_arg_field *field, *next;
2117 clear_perf_probe_point(&pev->point);
2119 for (i = 0; i < pev->nargs; i++) {
2120 free(pev->args[i].name);
2121 free(pev->args[i].var);
2122 free(pev->args[i].type);
2123 field = pev->args[i].field;
2126 zfree(&field->name);
2132 memset(pev, 0, sizeof(*pev));
2135 #define strdup_or_goto(str, label) \
2136 ({ char *__p = NULL; if (str && !(__p = strdup(str))) goto label; __p; })
2138 static int perf_probe_point__copy(struct perf_probe_point *dst,
2139 struct perf_probe_point *src)
2141 dst->file = strdup_or_goto(src->file, out_err);
2142 dst->function = strdup_or_goto(src->function, out_err);
2143 dst->lazy_line = strdup_or_goto(src->lazy_line, out_err);
2144 dst->line = src->line;
2145 dst->retprobe = src->retprobe;
2146 dst->offset = src->offset;
2150 clear_perf_probe_point(dst);
2154 static int perf_probe_arg__copy(struct perf_probe_arg *dst,
2155 struct perf_probe_arg *src)
2157 struct perf_probe_arg_field *field, **ppfield;
2159 dst->name = strdup_or_goto(src->name, out_err);
2160 dst->var = strdup_or_goto(src->var, out_err);
2161 dst->type = strdup_or_goto(src->type, out_err);
2164 ppfield = &(dst->field);
2166 *ppfield = zalloc(sizeof(*field));
2169 (*ppfield)->name = strdup_or_goto(field->name, out_err);
2170 (*ppfield)->index = field->index;
2171 (*ppfield)->ref = field->ref;
2172 field = field->next;
2173 ppfield = &((*ppfield)->next);
2180 int perf_probe_event__copy(struct perf_probe_event *dst,
2181 struct perf_probe_event *src)
2185 dst->event = strdup_or_goto(src->event, out_err);
2186 dst->group = strdup_or_goto(src->group, out_err);
2187 dst->target = strdup_or_goto(src->target, out_err);
2188 dst->uprobes = src->uprobes;
2190 if (perf_probe_point__copy(&dst->point, &src->point) < 0)
2193 dst->args = zalloc(sizeof(struct perf_probe_arg) * src->nargs);
2196 dst->nargs = src->nargs;
2198 for (i = 0; i < src->nargs; i++)
2199 if (perf_probe_arg__copy(&dst->args[i], &src->args[i]) < 0)
2204 clear_perf_probe_event(dst);
2208 void clear_probe_trace_event(struct probe_trace_event *tev)
2210 struct probe_trace_arg_ref *ref, *next;
2215 free(tev->point.symbol);
2216 free(tev->point.realname);
2217 free(tev->point.module);
2218 for (i = 0; i < tev->nargs; i++) {
2219 free(tev->args[i].name);
2220 free(tev->args[i].value);
2221 free(tev->args[i].type);
2222 ref = tev->args[i].ref;
2230 memset(tev, 0, sizeof(*tev));
2233 struct kprobe_blacklist_node {
2234 struct list_head list;
2235 unsigned long start;
2240 static void kprobe_blacklist__delete(struct list_head *blacklist)
2242 struct kprobe_blacklist_node *node;
2244 while (!list_empty(blacklist)) {
2245 node = list_first_entry(blacklist,
2246 struct kprobe_blacklist_node, list);
2247 list_del(&node->list);
2253 static int kprobe_blacklist__load(struct list_head *blacklist)
2255 struct kprobe_blacklist_node *node;
2256 const char *__debugfs = debugfs__mountpoint();
2257 char buf[PATH_MAX], *p;
2261 if (__debugfs == NULL)
2264 ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs);
2268 fp = fopen(buf, "r");
2273 while (fgets(buf, PATH_MAX, fp)) {
2274 node = zalloc(sizeof(*node));
2279 INIT_LIST_HEAD(&node->list);
2280 list_add_tail(&node->list, blacklist);
2281 if (sscanf(buf, "0x%lx-0x%lx", &node->start, &node->end) != 2) {
2285 p = strchr(buf, '\t');
2288 if (p[strlen(p) - 1] == '\n')
2289 p[strlen(p) - 1] = '\0';
2291 p = (char *)"unknown";
2292 node->symbol = strdup(p);
2293 if (!node->symbol) {
2297 pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
2298 node->start, node->end, node->symbol);
2302 kprobe_blacklist__delete(blacklist);
2308 static struct kprobe_blacklist_node *
2309 kprobe_blacklist__find_by_address(struct list_head *blacklist,
2310 unsigned long address)
2312 struct kprobe_blacklist_node *node;
2314 list_for_each_entry(node, blacklist, list) {
2315 if (node->start <= address && address <= node->end)
2322 static LIST_HEAD(kprobe_blacklist);
2324 static void kprobe_blacklist__init(void)
2326 if (!list_empty(&kprobe_blacklist))
2329 if (kprobe_blacklist__load(&kprobe_blacklist) < 0)
2330 pr_debug("No kprobe blacklist support, ignored\n");
2333 static void kprobe_blacklist__release(void)
2335 kprobe_blacklist__delete(&kprobe_blacklist);
2338 static bool kprobe_blacklist__listed(unsigned long address)
2340 return !!kprobe_blacklist__find_by_address(&kprobe_blacklist, address);
2343 static int perf_probe_event__sprintf(const char *group, const char *event,
2344 struct perf_probe_event *pev,
2346 struct strbuf *result)
2351 if (asprintf(&buf, "%s:%s", group, event) < 0)
2353 ret = strbuf_addf(result, " %-20s (on ", buf);
2358 /* Synthesize only event probe point */
2359 buf = synthesize_perf_probe_point(&pev->point);
2362 ret = strbuf_addstr(result, buf);
2366 ret = strbuf_addf(result, " in %s", module);
2368 if (!ret && pev->nargs > 0) {
2369 ret = strbuf_add(result, " with", 5);
2370 for (i = 0; !ret && i < pev->nargs; i++) {
2371 buf = synthesize_perf_probe_arg(&pev->args[i]);
2374 ret = strbuf_addf(result, " %s", buf);
2379 ret = strbuf_addch(result, ')');
2385 int show_perf_probe_event(const char *group, const char *event,
2386 struct perf_probe_event *pev,
2387 const char *module, bool use_stdout)
2389 struct strbuf buf = STRBUF_INIT;
2392 ret = perf_probe_event__sprintf(group, event, pev, module, &buf);
2395 printf("%s\n", buf.buf);
2397 pr_info("%s\n", buf.buf);
2399 strbuf_release(&buf);
2404 static bool filter_probe_trace_event(struct probe_trace_event *tev,
2405 struct strfilter *filter)
2409 /* At first, check the event name itself */
2410 if (strfilter__compare(filter, tev->event))
2413 /* Next, check the combination of name and group */
2414 if (e_snprintf(tmp, 128, "%s:%s", tev->group, tev->event) < 0)
2416 return strfilter__compare(filter, tmp);
2419 static int __show_perf_probe_events(int fd, bool is_kprobe,
2420 struct strfilter *filter)
2423 struct probe_trace_event tev;
2424 struct perf_probe_event pev;
2425 struct strlist *rawlist;
2426 struct str_node *ent;
2428 memset(&tev, 0, sizeof(tev));
2429 memset(&pev, 0, sizeof(pev));
2431 rawlist = probe_file__get_rawlist(fd);
2435 strlist__for_each_entry(ent, rawlist) {
2436 ret = parse_probe_trace_command(ent->s, &tev);
2438 if (!filter_probe_trace_event(&tev, filter))
2440 ret = convert_to_perf_probe_event(&tev, &pev,
2444 ret = show_perf_probe_event(pev.group, pev.event,
2445 &pev, tev.point.module,
2449 clear_perf_probe_event(&pev);
2450 clear_probe_trace_event(&tev);
2454 strlist__delete(rawlist);
2455 /* Cleanup cached debuginfo if needed */
2456 debuginfo_cache__exit();
2461 /* List up current perf-probe events */
2462 int show_perf_probe_events(struct strfilter *filter)
2464 int kp_fd, up_fd, ret;
2468 if (probe_conf.cache)
2469 return probe_cache__show_all_caches(filter);
2471 ret = init_probe_symbol_maps(false);
2475 ret = probe_file__open_both(&kp_fd, &up_fd, 0);
2480 ret = __show_perf_probe_events(kp_fd, true, filter);
2481 if (up_fd >= 0 && ret >= 0)
2482 ret = __show_perf_probe_events(up_fd, false, filter);
2487 exit_probe_symbol_maps();
2492 static int get_new_event_name(char *buf, size_t len, const char *base,
2493 struct strlist *namelist, bool allow_suffix)
2500 nbase = strdup(base);
2504 /* Cut off the dot suffixes (e.g. .const, .isra)*/
2505 p = strchr(nbase, '.');
2506 if (p && p != nbase)
2509 /* Try no suffix number */
2510 ret = e_snprintf(buf, len, "%s", nbase);
2512 pr_debug("snprintf() failed: %d\n", ret);
2515 if (!strlist__has_entry(namelist, buf))
2518 if (!allow_suffix) {
2519 pr_warning("Error: event \"%s\" already exists.\n"
2520 " Hint: Remove existing event by 'perf probe -d'\n"
2521 " or force duplicates by 'perf probe -f'\n"
2522 " or set 'force=yes' in BPF source.\n",
2528 /* Try to add suffix */
2529 for (i = 1; i < MAX_EVENT_INDEX; i++) {
2530 ret = e_snprintf(buf, len, "%s_%d", nbase, i);
2532 pr_debug("snprintf() failed: %d\n", ret);
2535 if (!strlist__has_entry(namelist, buf))
2538 if (i == MAX_EVENT_INDEX) {
2539 pr_warning("Too many events are on the same function.\n");
2548 /* Warn if the current kernel's uprobe implementation is old */
2549 static void warn_uprobe_event_compat(struct probe_trace_event *tev)
2552 char *buf = synthesize_probe_trace_command(tev);
2554 /* Old uprobe event doesn't support memory dereference */
2555 if (!tev->uprobes || tev->nargs == 0 || !buf)
2558 for (i = 0; i < tev->nargs; i++)
2559 if (strglobmatch(tev->args[i].value, "[$@+-]*")) {
2560 pr_warning("Please upgrade your kernel to at least "
2561 "3.14 to have access to feature %s\n",
2562 tev->args[i].value);
2569 /* Set new name from original perf_probe_event and namelist */
2570 static int probe_trace_event__set_name(struct probe_trace_event *tev,
2571 struct perf_probe_event *pev,
2572 struct strlist *namelist,
2575 const char *event, *group;
2579 /* If probe_event or trace_event already have the name, reuse it */
2580 if (pev->event && !pev->sdt)
2582 else if (tev->event)
2585 /* Or generate new one from probe point */
2586 if (pev->point.function &&
2587 (strncmp(pev->point.function, "0x", 2) != 0) &&
2588 !strisglob(pev->point.function))
2589 event = pev->point.function;
2591 event = tev->point.realname;
2593 if (pev->group && !pev->sdt)
2595 else if (tev->group)
2598 group = PERFPROBE_GROUP;
2600 /* Get an unused new event name */
2601 ret = get_new_event_name(buf, 64, event,
2602 namelist, allow_suffix);
2608 tev->event = strdup(event);
2609 tev->group = strdup(group);
2610 if (tev->event == NULL || tev->group == NULL)
2613 /* Add added event name to namelist */
2614 strlist__add(namelist, event);
2618 static int __open_probe_file_and_namelist(bool uprobe,
2619 struct strlist **namelist)
2623 fd = probe_file__open(PF_FL_RW | (uprobe ? PF_FL_UPROBE : 0));
2627 /* Get current event names */
2628 *namelist = probe_file__get_namelist(fd);
2630 pr_debug("Failed to get current event list.\n");
2637 static int __add_probe_trace_events(struct perf_probe_event *pev,
2638 struct probe_trace_event *tevs,
2639 int ntevs, bool allow_suffix)
2641 int i, fd[2] = {-1, -1}, up, ret;
2642 struct probe_trace_event *tev = NULL;
2643 struct probe_cache *cache = NULL;
2644 struct strlist *namelist[2] = {NULL, NULL};
2646 up = pev->uprobes ? 1 : 0;
2647 fd[up] = __open_probe_file_and_namelist(up, &namelist[up]);
2652 for (i = 0; i < ntevs; i++) {
2654 up = tev->uprobes ? 1 : 0;
2655 if (fd[up] == -1) { /* Open the kprobe/uprobe_events */
2656 fd[up] = __open_probe_file_and_namelist(up,
2661 /* Skip if the symbol is out of .text or blacklisted */
2662 if (!tev->point.symbol && !pev->uprobes)
2665 /* Set new name for tev (and update namelist) */
2666 ret = probe_trace_event__set_name(tev, pev, namelist[up],
2671 ret = probe_file__add_event(fd[up], tev);
2676 * Probes after the first probe which comes from same
2677 * user input are always allowed to add suffix, because
2678 * there might be several addresses corresponding to
2681 allow_suffix = true;
2683 if (ret == -EINVAL && pev->uprobes)
2684 warn_uprobe_event_compat(tev);
2685 if (ret == 0 && probe_conf.cache) {
2686 cache = probe_cache__new(pev->target);
2688 probe_cache__add_entry(cache, pev, tevs, ntevs) < 0 ||
2689 probe_cache__commit(cache) < 0)
2690 pr_warning("Failed to add event to probe cache\n");
2691 probe_cache__delete(cache);
2695 for (up = 0; up < 2; up++) {
2696 strlist__delete(namelist[up]);
2703 static int find_probe_functions(struct map *map, char *name,
2704 struct symbol **syms)
2708 struct rb_node *tmp;
2710 if (map__load(map) < 0)
2713 map__for_each_symbol(map, sym, tmp) {
2714 if (strglobmatch(sym->name, name)) {
2716 if (syms && found < probe_conf.max_probes)
2717 syms[found - 1] = sym;
2724 void __weak arch__fix_tev_from_maps(struct perf_probe_event *pev __maybe_unused,
2725 struct probe_trace_event *tev __maybe_unused,
2726 struct map *map __maybe_unused,
2727 struct symbol *sym __maybe_unused) { }
2730 * Find probe function addresses from map.
2731 * Return an error or the number of found probe_trace_event
2733 static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
2734 struct probe_trace_event **tevs)
2736 struct map *map = NULL;
2737 struct ref_reloc_sym *reloc_sym = NULL;
2739 struct symbol **syms = NULL;
2740 struct probe_trace_event *tev;
2741 struct perf_probe_point *pp = &pev->point;
2742 struct probe_trace_point *tp;
2743 int num_matched_functions;
2744 int ret, i, j, skipped = 0;
2747 map = get_target_map(pev->target, pev->uprobes);
2753 syms = malloc(sizeof(struct symbol *) * probe_conf.max_probes);
2760 * Load matched symbols: Since the different local symbols may have
2761 * same name but different addresses, this lists all the symbols.
2763 num_matched_functions = find_probe_functions(map, pp->function, syms);
2764 if (num_matched_functions == 0) {
2765 pr_err("Failed to find symbol %s in %s\n", pp->function,
2766 pev->target ? : "kernel");
2769 } else if (num_matched_functions > probe_conf.max_probes) {
2770 pr_err("Too many functions matched in %s\n",
2771 pev->target ? : "kernel");
2776 /* Note that the symbols in the kmodule are not relocated */
2777 if (!pev->uprobes && !pp->retprobe && !pev->target) {
2778 reloc_sym = kernel_get_ref_reloc_sym();
2780 pr_warning("Relocated base symbol is not found!\n");
2786 /* Setup result trace-probe-events */
2787 *tevs = zalloc(sizeof(*tev) * num_matched_functions);
2795 for (j = 0; j < num_matched_functions; j++) {
2798 tev = (*tevs) + ret;
2800 if (ret == num_matched_functions) {
2801 pr_warning("Too many symbols are listed. Skip it.\n");
2806 if (pp->offset > sym->end - sym->start) {
2807 pr_warning("Offset %ld is bigger than the size of %s\n",
2808 pp->offset, sym->name);
2812 /* Add one probe point */
2813 tp->address = map->unmap_ip(map, sym->start) + pp->offset;
2815 /* Check the kprobe (not in module) is within .text */
2816 if (!pev->uprobes && !pev->target &&
2817 kprobe_warn_out_range(sym->name, tp->address)) {
2818 tp->symbol = NULL; /* Skip it */
2820 } else if (reloc_sym) {
2821 tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
2822 tp->offset = tp->address - reloc_sym->addr;
2824 tp->symbol = strdup_or_goto(sym->name, nomem_out);
2825 tp->offset = pp->offset;
2827 tp->realname = strdup_or_goto(sym->name, nomem_out);
2829 tp->retprobe = pp->retprobe;
2832 tev->point.module = strdup_or_goto(pev->target,
2835 mod_name = find_module_name(pev->target);
2837 strdup(mod_name ? mod_name : pev->target);
2839 if (!tev->point.module)
2843 tev->uprobes = pev->uprobes;
2844 tev->nargs = pev->nargs;
2846 tev->args = zalloc(sizeof(struct probe_trace_arg) *
2848 if (tev->args == NULL)
2851 for (i = 0; i < tev->nargs; i++) {
2852 if (pev->args[i].name)
2854 strdup_or_goto(pev->args[i].name,
2857 tev->args[i].value = strdup_or_goto(pev->args[i].var,
2859 if (pev->args[i].type)
2861 strdup_or_goto(pev->args[i].type,
2864 arch__fix_tev_from_maps(pev, tev, map, sym);
2866 if (ret == skipped) {
2872 put_target_map(map, pev->uprobes);
2879 clear_probe_trace_events(*tevs, num_matched_functions);
2884 static int try_to_find_absolute_address(struct perf_probe_event *pev,
2885 struct probe_trace_event **tevs)
2887 struct perf_probe_point *pp = &pev->point;
2888 struct probe_trace_event *tev;
2889 struct probe_trace_point *tp;
2892 if (!(pev->point.function && !strncmp(pev->point.function, "0x", 2)))
2894 if (perf_probe_event_need_dwarf(pev))
2898 * This is 'perf probe /lib/libc.so 0xabcd'. Try to probe at
2901 * Only one tev can be generated by this.
2903 *tevs = zalloc(sizeof(*tev));
2911 * Don't use tp->offset, use address directly, because
2912 * in synthesize_probe_trace_command() address cannot be
2915 tp->address = pev->point.abs_address;
2916 tp->retprobe = pp->retprobe;
2917 tev->uprobes = pev->uprobes;
2921 * Give it a '0x' leading symbol name.
2922 * In __add_probe_trace_events, a NULL symbol is interpreted as
2925 if (asprintf(&tp->symbol, "0x%lx", tp->address) < 0)
2928 /* For kprobe, check range */
2929 if ((!tev->uprobes) &&
2930 (kprobe_warn_out_range(tev->point.symbol,
2931 tev->point.address))) {
2936 if (asprintf(&tp->realname, "abs_%lx", tp->address) < 0)
2940 tp->module = strdup(pev->target);
2946 tev->group = strdup(pev->group);
2952 tev->event = strdup(pev->event);
2957 tev->nargs = pev->nargs;
2958 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
2963 for (i = 0; i < tev->nargs; i++)
2964 copy_to_probe_trace_arg(&tev->args[i], &pev->args[i]);
2970 clear_probe_trace_events(*tevs, 1);
2976 /* Concatinate two arrays */
2977 static void *memcat(void *a, size_t sz_a, void *b, size_t sz_b)
2981 ret = malloc(sz_a + sz_b);
2983 memcpy(ret, a, sz_a);
2984 memcpy(ret + sz_a, b, sz_b);
2990 concat_probe_trace_events(struct probe_trace_event **tevs, int *ntevs,
2991 struct probe_trace_event **tevs2, int ntevs2)
2993 struct probe_trace_event *new_tevs;
3003 if (*ntevs + ntevs2 > probe_conf.max_probes)
3006 /* Concatinate the array of probe_trace_event */
3007 new_tevs = memcat(*tevs, (*ntevs) * sizeof(**tevs),
3008 *tevs2, ntevs2 * sizeof(**tevs2));
3018 clear_probe_trace_events(*tevs2, ntevs2);
3025 * Try to find probe_trace_event from given probe caches. Return the number
3026 * of cached events found, if an error occurs return the error.
3028 static int find_cached_events(struct perf_probe_event *pev,
3029 struct probe_trace_event **tevs,
3032 struct probe_cache *cache;
3033 struct probe_cache_entry *entry;
3034 struct probe_trace_event *tmp_tevs = NULL;
3038 cache = probe_cache__new(target);
3039 /* Return 0 ("not found") if the target has no probe cache. */
3043 for_each_probe_cache_entry(entry, cache) {
3044 /* Skip the cache entry which has no name */
3045 if (!entry->pev.event || !entry->pev.group)
3047 if ((!pev->group || strglobmatch(entry->pev.group, pev->group)) &&
3048 strglobmatch(entry->pev.event, pev->event)) {
3049 ret = probe_cache_entry__get_event(entry, &tmp_tevs);
3051 ret = concat_probe_trace_events(tevs, &ntevs,
3057 probe_cache__delete(cache);
3059 clear_probe_trace_events(*tevs, ntevs);
3063 if (ntevs > 0 && target && target[0] == '/')
3064 pev->uprobes = true;
3070 /* Try to find probe_trace_event from all probe caches */
3071 static int find_cached_events_all(struct perf_probe_event *pev,
3072 struct probe_trace_event **tevs)
3074 struct probe_trace_event *tmp_tevs = NULL;
3075 struct strlist *bidlist;
3076 struct str_node *nd;
3081 /* Get the buildid list of all valid caches */
3082 bidlist = build_id_cache__list_all(true);
3085 pr_debug("Failed to get buildids: %d\n", ret);
3090 strlist__for_each_entry(nd, bidlist) {
3091 pathname = build_id_cache__origname(nd->s);
3092 ret = find_cached_events(pev, &tmp_tevs, pathname);
3093 /* In the case of cnt == 0, we just skip it */
3095 ret = concat_probe_trace_events(tevs, &ntevs,
3101 strlist__delete(bidlist);
3104 clear_probe_trace_events(*tevs, ntevs);
3112 static int find_probe_trace_events_from_cache(struct perf_probe_event *pev,
3113 struct probe_trace_event **tevs)
3115 struct probe_cache *cache;
3116 struct probe_cache_entry *entry;
3117 struct probe_trace_event *tev;
3118 struct str_node *node;
3122 /* For SDT/cached events, we use special search functions */
3124 return find_cached_events_all(pev, tevs);
3126 return find_cached_events(pev, tevs, pev->target);
3128 cache = probe_cache__new(pev->target);
3132 entry = probe_cache__find(cache, pev);
3134 /* SDT must be in the cache */
3135 ret = pev->sdt ? -ENOENT : 0;
3139 ret = strlist__nr_entries(entry->tevlist);
3140 if (ret > probe_conf.max_probes) {
3141 pr_debug("Too many entries matched in the cache of %s\n",
3142 pev->target ? : "kernel");
3147 *tevs = zalloc(ret * sizeof(*tev));
3154 strlist__for_each_entry(node, entry->tevlist) {
3155 tev = &(*tevs)[i++];
3156 ret = parse_probe_trace_command(node->s, tev);
3159 /* Set the uprobes attribute as same as original */
3160 tev->uprobes = pev->uprobes;
3165 probe_cache__delete(cache);
3169 static int convert_to_probe_trace_events(struct perf_probe_event *pev,
3170 struct probe_trace_event **tevs)
3174 if (!pev->group && !pev->sdt) {
3175 /* Set group name if not given */
3176 if (!pev->uprobes) {
3177 pev->group = strdup(PERFPROBE_GROUP);
3178 ret = pev->group ? 0 : -ENOMEM;
3180 ret = convert_exec_to_group(pev->target, &pev->group);
3182 pr_warning("Failed to make a group name.\n");
3187 ret = try_to_find_absolute_address(pev, tevs);
3191 /* At first, we need to lookup cache entry */
3192 ret = find_probe_trace_events_from_cache(pev, tevs);
3193 if (ret > 0 || pev->sdt) /* SDT can be found only in the cache */
3194 return ret == 0 ? -ENOENT : ret; /* Found in probe cache */
3196 /* Convert perf_probe_event with debuginfo */
3197 ret = try_to_find_probe_trace_events(pev, tevs);
3199 return ret; /* Found in debuginfo or got an error */
3201 return find_probe_trace_events_from_map(pev, tevs);
3204 int convert_perf_probe_events(struct perf_probe_event *pevs, int npevs)
3208 /* Loop 1: convert all events */
3209 for (i = 0; i < npevs; i++) {
3210 /* Init kprobe blacklist if needed */
3211 if (!pevs[i].uprobes)
3212 kprobe_blacklist__init();
3213 /* Convert with or without debuginfo */
3214 ret = convert_to_probe_trace_events(&pevs[i], &pevs[i].tevs);
3217 pevs[i].ntevs = ret;
3219 /* This just release blacklist only if allocated */
3220 kprobe_blacklist__release();
3225 static int show_probe_trace_event(struct probe_trace_event *tev)
3227 char *buf = synthesize_probe_trace_command(tev);
3230 pr_debug("Failed to synthesize probe trace event.\n");
3234 /* Showing definition always go stdout */
3235 printf("%s\n", buf);
3241 int show_probe_trace_events(struct perf_probe_event *pevs, int npevs)
3243 struct strlist *namelist = strlist__new(NULL, NULL);
3244 struct probe_trace_event *tev;
3245 struct perf_probe_event *pev;
3251 for (j = 0; j < npevs && !ret; j++) {
3253 for (i = 0; i < pev->ntevs && !ret; i++) {
3254 tev = &pev->tevs[i];
3255 /* Skip if the symbol is out of .text or blacklisted */
3256 if (!tev->point.symbol && !pev->uprobes)
3259 /* Set new name for tev (and update namelist) */
3260 ret = probe_trace_event__set_name(tev, pev,
3263 ret = show_probe_trace_event(tev);
3266 strlist__delete(namelist);
3271 int apply_perf_probe_events(struct perf_probe_event *pevs, int npevs)
3275 /* Loop 2: add all events */
3276 for (i = 0; i < npevs; i++) {
3277 ret = __add_probe_trace_events(&pevs[i], pevs[i].tevs,
3279 probe_conf.force_add);
3286 void cleanup_perf_probe_events(struct perf_probe_event *pevs, int npevs)
3290 /* Loop 3: cleanup and free trace events */
3291 for (i = 0; i < npevs; i++) {
3292 for (j = 0; j < pevs[i].ntevs; j++)
3293 clear_probe_trace_event(&pevs[i].tevs[j]);
3294 zfree(&pevs[i].tevs);
3296 clear_perf_probe_event(&pevs[i]);
3300 int add_perf_probe_events(struct perf_probe_event *pevs, int npevs)
3304 ret = init_probe_symbol_maps(pevs->uprobes);
3308 ret = convert_perf_probe_events(pevs, npevs);
3310 ret = apply_perf_probe_events(pevs, npevs);
3312 cleanup_perf_probe_events(pevs, npevs);
3314 exit_probe_symbol_maps();
3318 int del_perf_probe_events(struct strfilter *filter)
3320 int ret, ret2, ufd = -1, kfd = -1;
3321 char *str = strfilter__string(filter);
3326 /* Get current event names */
3327 ret = probe_file__open_both(&kfd, &ufd, PF_FL_RW);
3331 ret = probe_file__del_events(kfd, filter);
3332 if (ret < 0 && ret != -ENOENT)
3335 ret2 = probe_file__del_events(ufd, filter);
3336 if (ret2 < 0 && ret2 != -ENOENT) {
3353 int show_available_funcs(const char *target, struct strfilter *_filter,
3360 ret = init_probe_symbol_maps(user);
3364 /* Get a symbol map */
3366 map = dso__new_map(target);
3368 map = kernel_get_module_map(target);
3370 pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
3374 ret = map__load(map);
3377 char *str = strfilter__string(_filter);
3378 pr_err("Failed to find symbols matched to \"%s\"\n",
3382 pr_err("Failed to load symbols in %s\n",
3383 (target) ? : "kernel");
3386 if (!dso__sorted_by_name(map->dso, map->type))
3387 dso__sort_by_name(map->dso, map->type);
3389 /* Show all (filtered) symbols */
3392 for (nd = rb_first(&map->dso->symbol_names[map->type]); nd; nd = rb_next(nd)) {
3393 struct symbol_name_rb_node *pos = rb_entry(nd, struct symbol_name_rb_node, rb_node);
3395 if (strfilter__compare(_filter, pos->sym.name))
3396 printf("%s\n", pos->sym.name);
3403 exit_probe_symbol_maps();
3408 int copy_to_probe_trace_arg(struct probe_trace_arg *tvar,
3409 struct perf_probe_arg *pvar)
3411 tvar->value = strdup(pvar->var);
3412 if (tvar->value == NULL)
3415 tvar->type = strdup(pvar->type);
3416 if (tvar->type == NULL)
3420 tvar->name = strdup(pvar->name);
3421 if (tvar->name == NULL)