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(struct map **pmap)
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);
137 return kmap->ref_reloc_sym;
140 static int kernel_get_symbol_address_by_name(const char *name, u64 *addr,
141 bool reloc, bool reladdr)
143 struct ref_reloc_sym *reloc_sym;
147 /* ref_reloc_sym is just a label. Need a special fix*/
148 reloc_sym = kernel_get_ref_reloc_sym(NULL);
149 if (reloc_sym && strcmp(name, reloc_sym->name) == 0)
150 *addr = (reloc) ? reloc_sym->addr : reloc_sym->unrelocated_addr;
152 sym = __find_kernel_function_by_name(name, &map);
155 *addr = map->unmap_ip(map, sym->start) -
156 ((reloc) ? 0 : map->reloc) -
157 ((reladdr) ? map->start : 0);
162 static struct map *kernel_get_module_map(const char *module)
164 struct map_groups *grp = &host_machine->kmaps;
165 struct maps *maps = &grp->maps[MAP__FUNCTION];
168 /* A file path -- this is an offline module */
169 if (module && strchr(module, '/'))
170 return dso__new_map(module);
175 for (pos = maps__first(maps); pos; pos = map__next(pos)) {
176 /* short_name is "[module]" */
177 if (strncmp(pos->dso->short_name + 1, module,
178 pos->dso->short_name_len - 2) == 0 &&
179 module[pos->dso->short_name_len - 2] == '\0') {
187 struct map *get_target_map(const char *target, bool user)
189 /* Init maps of given executable or kernel */
191 return dso__new_map(target);
193 return kernel_get_module_map(target);
196 static int convert_exec_to_group(const char *exec, char **result)
198 char *ptr1, *ptr2, *exec_copy;
202 exec_copy = strdup(exec);
206 ptr1 = basename(exec_copy);
212 for (ptr2 = ptr1; *ptr2 != '\0'; ptr2++) {
213 if (!isalnum(*ptr2) && *ptr2 != '_') {
219 ret = e_snprintf(buf, 64, "%s_%s", PERFPROBE_GROUP, ptr1);
223 *result = strdup(buf);
224 ret = *result ? 0 : -ENOMEM;
231 static void clear_perf_probe_point(struct perf_probe_point *pp)
238 static void clear_probe_trace_events(struct probe_trace_event *tevs, int ntevs)
242 for (i = 0; i < ntevs; i++)
243 clear_probe_trace_event(tevs + i);
246 static bool kprobe_blacklist__listed(unsigned long address);
247 static bool kprobe_warn_out_range(const char *symbol, unsigned long address)
252 /* Get the address of _etext for checking non-probable text symbol */
253 ret = kernel_get_symbol_address_by_name("_etext", &etext_addr,
256 if (ret == 0 && etext_addr < address)
257 pr_warning("%s is out of .text, skip it.\n", symbol);
258 else if (kprobe_blacklist__listed(address))
259 pr_warning("%s is blacklisted function, skip it.\n", symbol);
267 * @module can be module name of module file path. In case of path,
268 * inspect elf and find out what is actual module name.
269 * Caller has to free mod_name after using it.
271 static char *find_module_name(const char *module)
279 char *mod_name = NULL;
282 fd = open(module, O_RDONLY);
286 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
290 if (gelf_getehdr(elf, &ehdr) == NULL)
293 sec = elf_section_by_name(elf, &ehdr, &shdr,
294 ".gnu.linkonce.this_module", NULL);
298 data = elf_getdata(sec, NULL);
299 if (!data || !data->d_buf)
304 * '.gnu.linkonce.this_module' section of kernel module elf directly
305 * maps to 'struct module' from linux/module.h. This section contains
306 * actual module name which will be used by kernel after loading it.
307 * But, we cannot use 'struct module' here since linux/module.h is not
308 * exposed to user-space. Offset of 'name' has remained same from long
309 * time, so hardcoding it here.
311 if (ehdr.e_ident[EI_CLASS] == ELFCLASS32)
313 else /* expect ELFCLASS64 by default */
316 mod_name = strdup((char *)data->d_buf + name_offset);
325 #ifdef HAVE_DWARF_SUPPORT
327 static int kernel_get_module_dso(const char *module, struct dso **pdso)
331 const char *vmlinux_name;
335 char module_name[128];
337 snprintf(module_name, sizeof(module_name), "[%s]", module);
338 map = map_groups__find_by_name(&host_machine->kmaps, MAP__FUNCTION, module_name);
343 pr_debug("Failed to find module %s.\n", module);
347 map = machine__kernel_map(host_machine);
350 vmlinux_name = symbol_conf.vmlinux_name;
353 ret = dso__load_vmlinux(dso, map, vmlinux_name, false);
355 ret = dso__load_vmlinux_path(dso, map);
362 * Some binaries like glibc have special symbols which are on the symbol
363 * table, but not in the debuginfo. If we can find the address of the
364 * symbol from map, we can translate the address back to the probe point.
366 static int find_alternative_probe_point(struct debuginfo *dinfo,
367 struct perf_probe_point *pp,
368 struct perf_probe_point *result,
369 const char *target, bool uprobes)
371 struct map *map = NULL;
376 /* This can work only for function-name based one */
377 if (!pp->function || pp->file)
380 map = get_target_map(target, uprobes);
384 /* Find the address of given function */
385 map__for_each_symbol_by_name(map, pp->function, sym) {
387 address = sym->start;
389 address = map->unmap_ip(map, sym->start) - map->reloc;
396 pr_debug("Symbol %s address found : %" PRIx64 "\n",
397 pp->function, address);
399 ret = debuginfo__find_probe_point(dinfo, (unsigned long)address,
402 ret = (!ret) ? -ENOENT : ret;
404 result->offset += pp->offset;
405 result->line += pp->line;
406 result->retprobe = pp->retprobe;
416 static int get_alternative_probe_event(struct debuginfo *dinfo,
417 struct perf_probe_event *pev,
418 struct perf_probe_point *tmp)
422 memcpy(tmp, &pev->point, sizeof(*tmp));
423 memset(&pev->point, 0, sizeof(pev->point));
424 ret = find_alternative_probe_point(dinfo, tmp, &pev->point,
425 pev->target, pev->uprobes);
427 memcpy(&pev->point, tmp, sizeof(*tmp));
432 static int get_alternative_line_range(struct debuginfo *dinfo,
433 struct line_range *lr,
434 const char *target, bool user)
436 struct perf_probe_point pp = { .function = lr->function,
439 struct perf_probe_point result;
442 memset(&result, 0, sizeof(result));
444 if (lr->end != INT_MAX)
445 len = lr->end - lr->start;
446 ret = find_alternative_probe_point(dinfo, &pp, &result,
449 lr->function = result.function;
450 lr->file = result.file;
451 lr->start = result.line;
452 if (lr->end != INT_MAX)
453 lr->end = lr->start + len;
454 clear_perf_probe_point(&pp);
459 /* Open new debuginfo of given module */
460 static struct debuginfo *open_debuginfo(const char *module, bool silent)
462 const char *path = module;
463 char reason[STRERR_BUFSIZE];
464 struct debuginfo *ret = NULL;
465 struct dso *dso = NULL;
468 if (!module || !strchr(module, '/')) {
469 err = kernel_get_module_dso(module, &dso);
471 if (!dso || dso->load_errno == 0) {
472 if (!str_error_r(-err, reason, STRERR_BUFSIZE))
473 strcpy(reason, "(unknown)");
475 dso__strerror_load(dso, reason, STRERR_BUFSIZE);
477 pr_err("Failed to find the path for %s: %s\n",
478 module ?: "kernel", reason);
481 path = dso->long_name;
483 ret = debuginfo__new(path);
484 if (!ret && !silent) {
485 pr_warning("The %s file has no debug information.\n", path);
486 if (!module || !strtailcmp(path, ".ko"))
487 pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, ");
489 pr_warning("Rebuild with -g, ");
490 pr_warning("or install an appropriate debuginfo package.\n");
495 /* For caching the last debuginfo */
496 static struct debuginfo *debuginfo_cache;
497 static char *debuginfo_cache_path;
499 static struct debuginfo *debuginfo_cache__open(const char *module, bool silent)
501 const char *path = module;
503 /* If the module is NULL, it should be the kernel. */
507 if (debuginfo_cache_path && !strcmp(debuginfo_cache_path, path))
510 /* Copy module path */
511 free(debuginfo_cache_path);
512 debuginfo_cache_path = strdup(path);
513 if (!debuginfo_cache_path) {
514 debuginfo__delete(debuginfo_cache);
515 debuginfo_cache = NULL;
519 debuginfo_cache = open_debuginfo(module, silent);
520 if (!debuginfo_cache)
521 zfree(&debuginfo_cache_path);
523 return debuginfo_cache;
526 static void debuginfo_cache__exit(void)
528 debuginfo__delete(debuginfo_cache);
529 debuginfo_cache = NULL;
530 zfree(&debuginfo_cache_path);
534 static int get_text_start_address(const char *exec, unsigned long *address)
539 int fd, ret = -ENOENT;
541 fd = open(exec, O_RDONLY);
545 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
551 if (gelf_getehdr(elf, &ehdr) == NULL)
554 if (!elf_section_by_name(elf, &ehdr, &shdr, ".text", NULL))
557 *address = shdr.sh_addr - shdr.sh_offset;
568 * Convert trace point to probe point with debuginfo
570 static int find_perf_probe_point_from_dwarf(struct probe_trace_point *tp,
571 struct perf_probe_point *pp,
574 struct debuginfo *dinfo = NULL;
575 unsigned long stext = 0;
576 u64 addr = tp->address;
579 /* convert the address to dwarf address */
585 ret = get_text_start_address(tp->module, &stext);
589 } else if (tp->symbol) {
590 /* If the module is given, this returns relative address */
591 ret = kernel_get_symbol_address_by_name(tp->symbol, &addr,
592 false, !!tp->module);
598 pr_debug("try to find information at %" PRIx64 " in %s\n", addr,
599 tp->module ? : "kernel");
601 dinfo = debuginfo_cache__open(tp->module, verbose == 0);
603 ret = debuginfo__find_probe_point(dinfo,
604 (unsigned long)addr, pp);
609 pp->retprobe = tp->retprobe;
613 pr_debug("Failed to find corresponding probes from debuginfo.\n");
614 return ret ? : -ENOENT;
617 /* Adjust symbol name and address */
618 static int post_process_probe_trace_point(struct probe_trace_point *tp,
619 struct map *map, unsigned long offs)
622 u64 addr = tp->address - offs;
624 sym = map__find_symbol(map, addr);
628 if (strcmp(sym->name, tp->symbol)) {
629 /* If we have no realname, use symbol for it */
631 tp->realname = tp->symbol;
634 tp->symbol = strdup(sym->name);
638 tp->offset = addr - sym->start;
645 * Rename DWARF symbols to ELF symbols -- gcc sometimes optimizes functions
646 * and generate new symbols with suffixes such as .constprop.N or .isra.N
647 * etc. Since those symbols are not recorded in DWARF, we have to find
648 * correct generated symbols from offline ELF binary.
649 * For online kernel or uprobes we don't need this because those are
650 * rebased on _text, or already a section relative address.
653 post_process_offline_probe_trace_events(struct probe_trace_event *tevs,
654 int ntevs, const char *pathname)
657 unsigned long stext = 0;
660 /* Prepare a map for offline binary */
661 map = dso__new_map(pathname);
662 if (!map || get_text_start_address(pathname, &stext) < 0) {
663 pr_warning("Failed to get ELF symbols for %s\n", pathname);
667 for (i = 0; i < ntevs; i++) {
668 ret = post_process_probe_trace_point(&tevs[i].point,
678 static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs,
679 int ntevs, const char *exec)
682 unsigned long stext = 0;
687 ret = get_text_start_address(exec, &stext);
691 for (i = 0; i < ntevs && ret >= 0; i++) {
692 /* point.address is the addres of point.symbol + point.offset */
693 tevs[i].point.address -= stext;
694 tevs[i].point.module = strdup(exec);
695 if (!tevs[i].point.module) {
699 tevs[i].uprobes = true;
706 post_process_module_probe_trace_events(struct probe_trace_event *tevs,
707 int ntevs, const char *module,
708 struct debuginfo *dinfo)
710 Dwarf_Addr text_offs = 0;
712 char *mod_name = NULL;
718 map = get_target_map(module, false);
719 if (!map || debuginfo__get_text_offset(dinfo, &text_offs, true) < 0) {
720 pr_warning("Failed to get ELF symbols for %s\n", module);
724 mod_name = find_module_name(module);
725 for (i = 0; i < ntevs; i++) {
726 ret = post_process_probe_trace_point(&tevs[i].point,
727 map, (unsigned long)text_offs);
730 tevs[i].point.module =
731 strdup(mod_name ? mod_name : module);
732 if (!tevs[i].point.module) {
745 post_process_kernel_probe_trace_events(struct probe_trace_event *tevs,
748 struct ref_reloc_sym *reloc_sym;
753 /* Skip post process if the target is an offline kernel */
754 if (symbol_conf.ignore_vmlinux_buildid)
755 return post_process_offline_probe_trace_events(tevs, ntevs,
756 symbol_conf.vmlinux_name);
758 reloc_sym = kernel_get_ref_reloc_sym(&map);
760 pr_warning("Relocated base symbol is not found!\n");
764 for (i = 0; i < ntevs; i++) {
765 if (!tevs[i].point.address || tevs[i].point.retprobe)
768 * If we found a wrong one, mark it by NULL symbol.
769 * Since addresses in debuginfo is same as objdump, we need
770 * to convert it to addresses on memory.
772 if (kprobe_warn_out_range(tevs[i].point.symbol,
773 map__objdump_2mem(map, tevs[i].point.address))) {
777 tmp = strdup(reloc_sym->name);
781 /* If we have no realname, use symbol for it */
782 if (!tevs[i].point.realname)
783 tevs[i].point.realname = tevs[i].point.symbol;
785 free(tevs[i].point.symbol);
786 tevs[i].point.symbol = tmp;
787 tevs[i].point.offset = tevs[i].point.address -
788 reloc_sym->unrelocated_addr;
794 arch__post_process_probe_trace_events(struct perf_probe_event *pev __maybe_unused,
795 int ntevs __maybe_unused)
799 /* Post processing the probe events */
800 static int post_process_probe_trace_events(struct perf_probe_event *pev,
801 struct probe_trace_event *tevs,
802 int ntevs, const char *module,
803 bool uprobe, struct debuginfo *dinfo)
808 ret = add_exec_to_probe_trace_events(tevs, ntevs, module);
810 /* Currently ref_reloc_sym based probe is not for drivers */
811 ret = post_process_module_probe_trace_events(tevs, ntevs,
814 ret = post_process_kernel_probe_trace_events(tevs, ntevs);
817 arch__post_process_probe_trace_events(pev, ntevs);
822 /* Try to find perf_probe_event with debuginfo */
823 static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
824 struct probe_trace_event **tevs)
826 bool need_dwarf = perf_probe_event_need_dwarf(pev);
827 struct perf_probe_point tmp;
828 struct debuginfo *dinfo;
831 dinfo = open_debuginfo(pev->target, !need_dwarf);
835 pr_debug("Could not open debuginfo. Try to use symbols.\n");
839 pr_debug("Try to find probe point from debuginfo.\n");
840 /* Searching trace events corresponding to a probe event */
841 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
843 if (ntevs == 0) { /* Not found, retry with an alternative */
844 ret = get_alternative_probe_event(dinfo, pev, &tmp);
846 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
848 * Write back to the original probe_event for
849 * setting appropriate (user given) event name
851 clear_perf_probe_point(&pev->point);
852 memcpy(&pev->point, &tmp, sizeof(tmp));
856 if (ntevs > 0) { /* Succeeded to find trace events */
857 pr_debug("Found %d probe_trace_events.\n", ntevs);
858 ret = post_process_probe_trace_events(pev, *tevs, ntevs,
859 pev->target, pev->uprobes, dinfo);
860 if (ret < 0 || ret == ntevs) {
861 pr_debug("Post processing failed or all events are skipped. (%d)\n", ret);
862 clear_probe_trace_events(*tevs, ntevs);
868 debuginfo__delete(dinfo);
870 if (ntevs == 0) { /* No error but failed to find probe point. */
871 pr_warning("Probe point '%s' not found.\n",
872 synthesize_perf_probe_point(&pev->point));
874 } else if (ntevs < 0) {
875 /* Error path : ntevs < 0 */
876 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
878 pr_warning("Warning: No dwarf info found in the vmlinux - "
879 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
881 pr_debug("Trying to use symbols.\n");
888 #define LINEBUF_SIZE 256
889 #define NR_ADDITIONAL_LINES 2
891 static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
893 char buf[LINEBUF_SIZE], sbuf[STRERR_BUFSIZE];
894 const char *color = show_num ? "" : PERF_COLOR_BLUE;
895 const char *prefix = NULL;
898 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
903 prefix = show_num ? "%7d " : " ";
904 color_fprintf(stdout, color, prefix, l);
906 color_fprintf(stdout, color, "%s", buf);
908 } while (strchr(buf, '\n') == NULL);
913 pr_warning("File read error: %s\n",
914 str_error_r(errno, sbuf, sizeof(sbuf)));
920 static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
922 int rv = __show_one_line(fp, l, skip, show_num);
924 pr_warning("Source file is shorter than expected.\n");
930 #define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
931 #define show_one_line(f,l) _show_one_line(f,l,false,false)
932 #define skip_one_line(f,l) _show_one_line(f,l,true,false)
933 #define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
936 * Show line-range always requires debuginfo to find source file and
939 static int __show_line_range(struct line_range *lr, const char *module,
944 struct debuginfo *dinfo;
948 char sbuf[STRERR_BUFSIZE];
950 /* Search a line range */
951 dinfo = open_debuginfo(module, false);
955 ret = debuginfo__find_line_range(dinfo, lr);
956 if (!ret) { /* Not found, retry with an alternative */
957 ret = get_alternative_line_range(dinfo, lr, module, user);
959 ret = debuginfo__find_line_range(dinfo, lr);
961 debuginfo__delete(dinfo);
962 if (ret == 0 || ret == -ENOENT) {
963 pr_warning("Specified source line is not found.\n");
965 } else if (ret < 0) {
966 pr_warning("Debuginfo analysis failed.\n");
970 /* Convert source file path */
972 ret = get_real_path(tmp, lr->comp_dir, &lr->path);
974 /* Free old path when new path is assigned */
979 pr_warning("Failed to find source file path.\n");
986 fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
987 lr->start - lr->offset);
989 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
991 fp = fopen(lr->path, "r");
993 pr_warning("Failed to open %s: %s\n", lr->path,
994 str_error_r(errno, sbuf, sizeof(sbuf)));
997 /* Skip to starting line number */
998 while (l < lr->start) {
999 ret = skip_one_line(fp, l++);
1004 intlist__for_each_entry(ln, lr->line_list) {
1005 for (; ln->i > l; l++) {
1006 ret = show_one_line(fp, l - lr->offset);
1010 ret = show_one_line_with_num(fp, l++ - lr->offset);
1015 if (lr->end == INT_MAX)
1016 lr->end = l + NR_ADDITIONAL_LINES;
1017 while (l <= lr->end) {
1018 ret = show_one_line_or_eof(fp, l++ - lr->offset);
1027 int show_line_range(struct line_range *lr, const char *module, bool user)
1031 ret = init_probe_symbol_maps(user);
1034 ret = __show_line_range(lr, module, user);
1035 exit_probe_symbol_maps();
1040 static int show_available_vars_at(struct debuginfo *dinfo,
1041 struct perf_probe_event *pev,
1042 struct strfilter *_filter)
1046 struct str_node *node;
1047 struct variable_list *vls = NULL, *vl;
1048 struct perf_probe_point tmp;
1051 buf = synthesize_perf_probe_point(&pev->point);
1054 pr_debug("Searching variables at %s\n", buf);
1056 ret = debuginfo__find_available_vars_at(dinfo, pev, &vls);
1057 if (!ret) { /* Not found, retry with an alternative */
1058 ret = get_alternative_probe_event(dinfo, pev, &tmp);
1060 ret = debuginfo__find_available_vars_at(dinfo, pev,
1062 /* Release the old probe_point */
1063 clear_perf_probe_point(&tmp);
1067 if (ret == 0 || ret == -ENOENT) {
1068 pr_err("Failed to find the address of %s\n", buf);
1071 pr_warning("Debuginfo analysis failed.\n");
1075 /* Some variables are found */
1076 fprintf(stdout, "Available variables at %s\n", buf);
1077 for (i = 0; i < ret; i++) {
1080 * A probe point might be converted to
1081 * several trace points.
1083 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
1085 zfree(&vl->point.symbol);
1088 strlist__for_each_entry(node, vl->vars) {
1089 var = strchr(node->s, '\t') + 1;
1090 if (strfilter__compare(_filter, var)) {
1091 fprintf(stdout, "\t\t%s\n", node->s);
1095 strlist__delete(vl->vars);
1098 fprintf(stdout, "\t\t(No matched variables)\n");
1106 /* Show available variables on given probe point */
1107 int show_available_vars(struct perf_probe_event *pevs, int npevs,
1108 struct strfilter *_filter)
1111 struct debuginfo *dinfo;
1113 ret = init_probe_symbol_maps(pevs->uprobes);
1117 dinfo = open_debuginfo(pevs->target, false);
1125 for (i = 0; i < npevs && ret >= 0; i++)
1126 ret = show_available_vars_at(dinfo, &pevs[i], _filter);
1128 debuginfo__delete(dinfo);
1130 exit_probe_symbol_maps();
1134 #else /* !HAVE_DWARF_SUPPORT */
1136 static void debuginfo_cache__exit(void)
1141 find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused,
1142 struct perf_probe_point *pp __maybe_unused,
1143 bool is_kprobe __maybe_unused)
1148 static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
1149 struct probe_trace_event **tevs __maybe_unused)
1151 if (perf_probe_event_need_dwarf(pev)) {
1152 pr_warning("Debuginfo-analysis is not supported.\n");
1159 int show_line_range(struct line_range *lr __maybe_unused,
1160 const char *module __maybe_unused,
1161 bool user __maybe_unused)
1163 pr_warning("Debuginfo-analysis is not supported.\n");
1167 int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
1168 int npevs __maybe_unused,
1169 struct strfilter *filter __maybe_unused)
1171 pr_warning("Debuginfo-analysis is not supported.\n");
1176 void line_range__clear(struct line_range *lr)
1182 intlist__delete(lr->line_list);
1183 memset(lr, 0, sizeof(*lr));
1186 int line_range__init(struct line_range *lr)
1188 memset(lr, 0, sizeof(*lr));
1189 lr->line_list = intlist__new(NULL);
1196 static int parse_line_num(char **ptr, int *val, const char *what)
1198 const char *start = *ptr;
1201 *val = strtol(*ptr, ptr, 0);
1202 if (errno || *ptr == start) {
1203 semantic_error("'%s' is not a valid number.\n", what);
1209 /* Check the name is good for event, group or function */
1210 static bool is_c_func_name(const char *name)
1212 if (!isalpha(*name) && *name != '_')
1214 while (*++name != '\0') {
1215 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
1222 * Stuff 'lr' according to the line range described by 'arg'.
1223 * The line range syntax is described by:
1225 * SRC[:SLN[+NUM|-ELN]]
1226 * FNC[@SRC][:SLN[+NUM|-ELN]]
1228 int parse_line_range_desc(const char *arg, struct line_range *lr)
1230 char *range, *file, *name = strdup(arg);
1239 range = strchr(name, ':');
1243 err = parse_line_num(&range, &lr->start, "start line");
1247 if (*range == '+' || *range == '-') {
1248 const char c = *range++;
1250 err = parse_line_num(&range, &lr->end, "end line");
1255 lr->end += lr->start;
1257 * Adjust the number of lines here.
1258 * If the number of lines == 1, the
1259 * the end of line should be equal to
1260 * the start of line.
1266 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
1269 if (lr->start > lr->end) {
1270 semantic_error("Start line must be smaller"
1271 " than end line.\n");
1274 if (*range != '\0') {
1275 semantic_error("Tailing with invalid str '%s'.\n", range);
1280 file = strchr(name, '@');
1283 lr->file = strdup(++file);
1284 if (lr->file == NULL) {
1288 lr->function = name;
1289 } else if (strchr(name, '/') || strchr(name, '.'))
1291 else if (is_c_func_name(name))/* We reuse it for checking funcname */
1292 lr->function = name;
1293 else { /* Invalid name */
1294 semantic_error("'%s' is not a valid function name.\n", name);
1305 static int parse_perf_probe_event_name(char **arg, struct perf_probe_event *pev)
1309 ptr = strchr(*arg, ':');
1312 if (!pev->sdt && !is_c_func_name(*arg))
1314 pev->group = strdup(*arg);
1320 if (!pev->sdt && !is_c_func_name(*arg)) {
1322 semantic_error("%s is bad for event name -it must "
1323 "follow C symbol-naming rule.\n", *arg);
1326 pev->event = strdup(*arg);
1327 if (pev->event == NULL)
1333 /* Parse probepoint definition. */
1334 static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
1336 struct perf_probe_point *pp = &pev->point;
1339 bool file_spec = false;
1344 * perf probe [GRP:][EVENT=]SRC[:LN|;PTN]
1345 * perf probe [GRP:][EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
1346 * perf probe %[GRP:]SDT_EVENT
1352 * If the probe point starts with '%',
1353 * or starts with "sdt_" and has a ':' but no '=',
1354 * then it should be a SDT/cached probe point.
1356 if (arg[0] == '%' ||
1357 (!strncmp(arg, "sdt_", 4) &&
1358 !!strchr(arg, ':') && !strchr(arg, '='))) {
1364 ptr = strpbrk(arg, ";=@+%");
1368 semantic_error("%s must be an SDT name.\n",
1372 /* This must be a target file name or build id */
1373 tmp = build_id_cache__complement(ptr + 1);
1375 pev->target = build_id_cache__origname(tmp);
1378 pev->target = strdup(ptr + 1);
1383 ret = parse_perf_probe_event_name(&arg, pev);
1385 if (asprintf(&pev->point.function, "%%%s", pev->event) < 0)
1391 if (ptr && *ptr == '=') { /* Event name */
1394 ret = parse_perf_probe_event_name(&arg, pev);
1402 * Check arg is function or file name and copy it.
1404 * We consider arg to be a file spec if and only if it satisfies
1405 * all of the below criteria::
1406 * - it does not include any of "+@%",
1407 * - it includes one of ":;", and
1408 * - it has a period '.' in the name.
1410 * Otherwise, we consider arg to be a function specification.
1412 if (!strpbrk(arg, "+@%") && (ptr = strpbrk(arg, ";:")) != NULL) {
1413 /* This is a file spec if it includes a '.' before ; or : */
1414 if (memchr(arg, '.', ptr - arg))
1418 ptr = strpbrk(arg, ";:+@%");
1438 * Keep pp->function even if this is absolute address,
1439 * so it can mark whether abs_address is valid.
1440 * Which make 'perf probe lib.bin 0x0' possible.
1442 * Note that checking length of tmp is not needed
1443 * because when we access tmp[1] we know tmp[0] is '0',
1444 * so tmp[1] should always valid (but could be '\0').
1446 if (tmp && !strncmp(tmp, "0x", 2)) {
1447 pp->abs_address = strtoul(pp->function, &tmp, 0);
1449 semantic_error("Invalid absolute address.\n");
1455 /* Parse other options */
1459 if (c == ';') { /* Lazy pattern must be the last part */
1460 pp->lazy_line = strdup(arg);
1461 if (pp->lazy_line == NULL)
1465 ptr = strpbrk(arg, ";:+@%");
1471 case ':': /* Line number */
1472 pp->line = strtoul(arg, &tmp, 0);
1474 semantic_error("There is non-digit char"
1475 " in line number.\n");
1479 case '+': /* Byte offset from a symbol */
1480 pp->offset = strtoul(arg, &tmp, 0);
1482 semantic_error("There is non-digit character"
1487 case '@': /* File name */
1489 semantic_error("SRC@SRC is not allowed.\n");
1492 pp->file = strdup(arg);
1493 if (pp->file == NULL)
1496 case '%': /* Probe places */
1497 if (strcmp(arg, "return") == 0) {
1499 } else { /* Others not supported yet */
1500 semantic_error("%%%s is not supported.\n", arg);
1504 default: /* Buggy case */
1505 pr_err("This program has a bug at %s:%d.\n",
1506 __FILE__, __LINE__);
1512 /* Exclusion check */
1513 if (pp->lazy_line && pp->line) {
1514 semantic_error("Lazy pattern can't be used with"
1519 if (pp->lazy_line && pp->offset) {
1520 semantic_error("Lazy pattern can't be used with offset.\n");
1524 if (pp->line && pp->offset) {
1525 semantic_error("Offset can't be used with line number.\n");
1529 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
1530 semantic_error("File always requires line number or "
1535 if (pp->offset && !pp->function) {
1536 semantic_error("Offset requires an entry function.\n");
1540 if (pp->retprobe && !pp->function) {
1541 semantic_error("Return probe requires an entry function.\n");
1545 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
1546 semantic_error("Offset/Line/Lazy pattern can't be used with "
1551 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
1552 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
1557 /* Parse perf-probe event argument */
1558 static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
1560 char *tmp, *goodname;
1561 struct perf_probe_arg_field **fieldp;
1563 pr_debug("parsing arg: %s into ", str);
1565 tmp = strchr(str, '=');
1567 arg->name = strndup(str, tmp - str);
1568 if (arg->name == NULL)
1570 pr_debug("name:%s ", arg->name);
1574 tmp = strchr(str, ':');
1575 if (tmp) { /* Type setting */
1577 arg->type = strdup(tmp + 1);
1578 if (arg->type == NULL)
1580 pr_debug("type:%s ", arg->type);
1583 tmp = strpbrk(str, "-.[");
1584 if (!is_c_varname(str) || !tmp) {
1585 /* A variable, register, symbol or special value */
1586 arg->var = strdup(str);
1587 if (arg->var == NULL)
1589 pr_debug("%s\n", arg->var);
1593 /* Structure fields or array element */
1594 arg->var = strndup(str, tmp - str);
1595 if (arg->var == NULL)
1597 goodname = arg->var;
1598 pr_debug("%s, ", arg->var);
1599 fieldp = &arg->field;
1602 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
1603 if (*fieldp == NULL)
1605 if (*tmp == '[') { /* Array */
1607 (*fieldp)->index = strtol(str + 1, &tmp, 0);
1608 (*fieldp)->ref = true;
1609 if (*tmp != ']' || tmp == str + 1) {
1610 semantic_error("Array index must be a"
1617 } else { /* Structure */
1620 (*fieldp)->ref = false;
1621 } else if (tmp[1] == '>') {
1623 (*fieldp)->ref = true;
1625 semantic_error("Argument parse error: %s\n",
1629 tmp = strpbrk(str, "-.[");
1632 (*fieldp)->name = strndup(str, tmp - str);
1633 if ((*fieldp)->name == NULL)
1636 goodname = (*fieldp)->name;
1637 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
1638 fieldp = &(*fieldp)->next;
1641 (*fieldp)->name = strdup(str);
1642 if ((*fieldp)->name == NULL)
1645 goodname = (*fieldp)->name;
1646 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
1648 /* If no name is specified, set the last field name (not array index)*/
1650 arg->name = strdup(goodname);
1651 if (arg->name == NULL)
1657 /* Parse perf-probe event command */
1658 int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
1661 int argc, i, ret = 0;
1663 argv = argv_split(cmd, &argc);
1665 pr_debug("Failed to split arguments.\n");
1668 if (argc - 1 > MAX_PROBE_ARGS) {
1669 semantic_error("Too many probe arguments (%d).\n", argc - 1);
1673 /* Parse probe point */
1674 ret = parse_perf_probe_point(argv[0], pev);
1678 /* Copy arguments and ensure return probe has no C argument */
1679 pev->nargs = argc - 1;
1680 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1681 if (pev->args == NULL) {
1685 for (i = 0; i < pev->nargs && ret >= 0; i++) {
1686 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1688 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
1689 semantic_error("You can't specify local variable for"
1700 /* Returns true if *any* ARG is either C variable, $params or $vars. */
1701 bool perf_probe_with_var(struct perf_probe_event *pev)
1705 for (i = 0; i < pev->nargs; i++)
1706 if (is_c_varname(pev->args[i].var) ||
1707 !strcmp(pev->args[i].var, PROBE_ARG_PARAMS) ||
1708 !strcmp(pev->args[i].var, PROBE_ARG_VARS))
1713 /* Return true if this perf_probe_event requires debuginfo */
1714 bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
1716 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1719 if (perf_probe_with_var(pev))
1725 /* Parse probe_events event into struct probe_point */
1726 int parse_probe_trace_command(const char *cmd, struct probe_trace_event *tev)
1728 struct probe_trace_point *tp = &tev->point;
1731 char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
1735 pr_debug("Parsing probe_events: %s\n", cmd);
1736 argv = argv_split(cmd, &argc);
1738 pr_debug("Failed to split arguments.\n");
1742 semantic_error("Too few probe arguments.\n");
1747 /* Scan event and group name. */
1748 argv0_str = strdup(argv[0]);
1749 if (argv0_str == NULL) {
1753 fmt1_str = strtok_r(argv0_str, ":", &fmt);
1754 fmt2_str = strtok_r(NULL, "/", &fmt);
1755 fmt3_str = strtok_r(NULL, " \t", &fmt);
1756 if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
1757 || fmt3_str == NULL) {
1758 semantic_error("Failed to parse event name: %s\n", argv[0]);
1763 tev->group = strdup(fmt2_str);
1764 tev->event = strdup(fmt3_str);
1765 if (tev->group == NULL || tev->event == NULL) {
1769 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
1771 tp->retprobe = (pr == 'r');
1773 /* Scan module name(if there), function name and offset */
1774 p = strchr(argv[1], ':');
1776 tp->module = strndup(argv[1], p - argv[1]);
1781 tev->uprobes = (tp->module[0] == '/');
1785 fmt1_str = strtok_r(p, "+", &fmt);
1786 /* only the address started with 0x */
1787 if (fmt1_str[0] == '0') {
1789 * Fix a special case:
1790 * if address == 0, kernel reports something like:
1791 * p:probe_libc/abs_0 /lib/libc-2.18.so:0x (null) arg1=%ax
1792 * Newer kernel may fix that, but we want to
1793 * support old kernel also.
1795 if (strcmp(fmt1_str, "0x") == 0) {
1796 if (!argv[2] || strcmp(argv[2], "(null)")) {
1803 for (i = 2; argv[i + 1] != NULL; i++)
1804 argv[i] = argv[i + 1];
1809 tp->address = strtoul(fmt1_str, NULL, 0);
1811 /* Only the symbol-based probe has offset */
1812 tp->symbol = strdup(fmt1_str);
1813 if (tp->symbol == NULL) {
1817 fmt2_str = strtok_r(NULL, "", &fmt);
1818 if (fmt2_str == NULL)
1821 tp->offset = strtoul(fmt2_str, NULL, 10);
1824 tev->nargs = argc - 2;
1825 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
1826 if (tev->args == NULL) {
1830 for (i = 0; i < tev->nargs; i++) {
1831 p = strchr(argv[i + 2], '=');
1832 if (p) /* We don't need which register is assigned. */
1836 tev->args[i].name = strdup(argv[i + 2]);
1837 /* TODO: parse regs and offset */
1838 tev->args[i].value = strdup(p);
1839 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1851 /* Compose only probe arg */
1852 char *synthesize_perf_probe_arg(struct perf_probe_arg *pa)
1854 struct perf_probe_arg_field *field = pa->field;
1859 if (strbuf_init(&buf, 64) < 0)
1862 if (pa->name && pa->var)
1863 err = strbuf_addf(&buf, "%s=%s", pa->name, pa->var);
1865 err = strbuf_addstr(&buf, pa->name ?: pa->var);
1870 if (field->name[0] == '[')
1871 err = strbuf_addstr(&buf, field->name);
1873 err = strbuf_addf(&buf, "%s%s", field->ref ? "->" : ".",
1875 field = field->next;
1881 if (strbuf_addf(&buf, ":%s", pa->type) < 0)
1884 ret = strbuf_detach(&buf, NULL);
1886 strbuf_release(&buf);
1890 /* Compose only probe point (not argument) */
1891 char *synthesize_perf_probe_point(struct perf_probe_point *pp)
1894 char *tmp, *ret = NULL;
1897 if (strbuf_init(&buf, 64) < 0)
1901 if (strbuf_addstr(&buf, pp->function) < 0)
1904 err = strbuf_addf(&buf, "+%lu", pp->offset);
1906 err = strbuf_addf(&buf, ":%d", pp->line);
1907 else if (pp->retprobe)
1908 err = strbuf_addstr(&buf, "%return");
1916 tmp = strchr(pp->file + len - 30, '/');
1917 tmp = tmp ? tmp + 1 : pp->file + len - 30;
1919 err = strbuf_addf(&buf, "@%s", tmp);
1920 if (!err && !pp->function && pp->line)
1921 err = strbuf_addf(&buf, ":%d", pp->line);
1924 ret = strbuf_detach(&buf, NULL);
1926 strbuf_release(&buf);
1930 char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1933 char *tmp, *ret = NULL;
1936 if (strbuf_init(&buf, 64))
1939 if (strbuf_addf(&buf, "%s:%s=", pev->group ?: PERFPROBE_GROUP,
1943 tmp = synthesize_perf_probe_point(&pev->point);
1944 if (!tmp || strbuf_addstr(&buf, tmp) < 0)
1948 for (i = 0; i < pev->nargs; i++) {
1949 tmp = synthesize_perf_probe_arg(pev->args + i);
1950 if (!tmp || strbuf_addf(&buf, " %s", tmp) < 0)
1955 ret = strbuf_detach(&buf, NULL);
1957 strbuf_release(&buf);
1961 static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
1962 struct strbuf *buf, int depth)
1966 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
1971 err = strbuf_addf(buf, "%+ld(", ref->offset);
1972 return (err < 0) ? err : depth;
1975 static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
1978 struct probe_trace_arg_ref *ref = arg->ref;
1981 /* Argument name or separator */
1983 err = strbuf_addf(buf, " %s=", arg->name);
1985 err = strbuf_addch(buf, ' ');
1989 /* Special case: @XXX */
1990 if (arg->value[0] == '@' && arg->ref)
1993 /* Dereferencing arguments */
1995 depth = __synthesize_probe_trace_arg_ref(ref, buf, 1);
2000 /* Print argument value */
2001 if (arg->value[0] == '@' && arg->ref)
2002 err = strbuf_addf(buf, "%s%+ld", arg->value, arg->ref->offset);
2004 err = strbuf_addstr(buf, arg->value);
2007 while (!err && depth--)
2008 err = strbuf_addch(buf, ')');
2010 /* Print argument type */
2011 if (!err && arg->type)
2012 err = strbuf_addf(buf, ":%s", arg->type);
2017 char *synthesize_probe_trace_command(struct probe_trace_event *tev)
2019 struct probe_trace_point *tp = &tev->point;
2024 /* Uprobes must have tp->module */
2025 if (tev->uprobes && !tp->module)
2028 if (strbuf_init(&buf, 32) < 0)
2031 if (strbuf_addf(&buf, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
2032 tev->group, tev->event) < 0)
2035 * If tp->address == 0, then this point must be a
2036 * absolute address uprobe.
2037 * try_to_find_absolute_address() should have made
2038 * tp->symbol to "0x0".
2040 if (tev->uprobes && !tp->address) {
2041 if (!tp->symbol || strcmp(tp->symbol, "0x0"))
2045 /* Use the tp->address for uprobes */
2047 err = strbuf_addf(&buf, "%s:0x%lx", tp->module, tp->address);
2048 else if (!strncmp(tp->symbol, "0x", 2))
2049 /* Absolute address. See try_to_find_absolute_address() */
2050 err = strbuf_addf(&buf, "%s%s0x%lx", tp->module ?: "",
2051 tp->module ? ":" : "", tp->address);
2053 err = strbuf_addf(&buf, "%s%s%s+%lu", tp->module ?: "",
2054 tp->module ? ":" : "", tp->symbol, tp->offset);
2058 for (i = 0; i < tev->nargs; i++)
2059 if (synthesize_probe_trace_arg(&tev->args[i], &buf) < 0)
2062 ret = strbuf_detach(&buf, NULL);
2064 strbuf_release(&buf);
2068 static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
2069 struct perf_probe_point *pp,
2072 struct symbol *sym = NULL;
2074 u64 addr = tp->address;
2078 map = dso__new_map(tp->module);
2081 sym = map__find_symbol(map, addr);
2083 if (tp->symbol && !addr) {
2084 if (kernel_get_symbol_address_by_name(tp->symbol,
2085 &addr, true, false) < 0)
2090 sym = __find_kernel_function(addr, &map);
2097 pp->retprobe = tp->retprobe;
2098 pp->offset = addr - map->unmap_ip(map, sym->start);
2099 pp->function = strdup(sym->name);
2100 ret = pp->function ? 0 : -ENOMEM;
2103 if (map && !is_kprobe) {
2110 static int convert_to_perf_probe_point(struct probe_trace_point *tp,
2111 struct perf_probe_point *pp,
2117 ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe);
2120 ret = find_perf_probe_point_from_map(tp, pp, is_kprobe);
2124 pr_debug("Failed to find probe point from both of dwarf and map.\n");
2127 pp->function = strdup(tp->symbol);
2128 pp->offset = tp->offset;
2130 ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address);
2133 pp->function = strdup(buf);
2136 if (pp->function == NULL)
2139 pp->retprobe = tp->retprobe;
2144 static int convert_to_perf_probe_event(struct probe_trace_event *tev,
2145 struct perf_probe_event *pev, bool is_kprobe)
2147 struct strbuf buf = STRBUF_INIT;
2150 /* Convert event/group name */
2151 pev->event = strdup(tev->event);
2152 pev->group = strdup(tev->group);
2153 if (pev->event == NULL || pev->group == NULL)
2156 /* Convert trace_point to probe_point */
2157 ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
2161 /* Convert trace_arg to probe_arg */
2162 pev->nargs = tev->nargs;
2163 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
2164 if (pev->args == NULL)
2166 for (i = 0; i < tev->nargs && ret >= 0; i++) {
2167 if (tev->args[i].name)
2168 pev->args[i].name = strdup(tev->args[i].name);
2170 if ((ret = strbuf_init(&buf, 32)) < 0)
2172 ret = synthesize_probe_trace_arg(&tev->args[i], &buf);
2173 pev->args[i].name = strbuf_detach(&buf, NULL);
2175 if (pev->args[i].name == NULL && ret >= 0)
2180 clear_perf_probe_event(pev);
2185 void clear_perf_probe_event(struct perf_probe_event *pev)
2187 struct perf_probe_arg_field *field, *next;
2193 clear_perf_probe_point(&pev->point);
2195 for (i = 0; i < pev->nargs; i++) {
2196 free(pev->args[i].name);
2197 free(pev->args[i].var);
2198 free(pev->args[i].type);
2199 field = pev->args[i].field;
2202 zfree(&field->name);
2208 memset(pev, 0, sizeof(*pev));
2211 #define strdup_or_goto(str, label) \
2212 ({ char *__p = NULL; if (str && !(__p = strdup(str))) goto label; __p; })
2214 static int perf_probe_point__copy(struct perf_probe_point *dst,
2215 struct perf_probe_point *src)
2217 dst->file = strdup_or_goto(src->file, out_err);
2218 dst->function = strdup_or_goto(src->function, out_err);
2219 dst->lazy_line = strdup_or_goto(src->lazy_line, out_err);
2220 dst->line = src->line;
2221 dst->retprobe = src->retprobe;
2222 dst->offset = src->offset;
2226 clear_perf_probe_point(dst);
2230 static int perf_probe_arg__copy(struct perf_probe_arg *dst,
2231 struct perf_probe_arg *src)
2233 struct perf_probe_arg_field *field, **ppfield;
2235 dst->name = strdup_or_goto(src->name, out_err);
2236 dst->var = strdup_or_goto(src->var, out_err);
2237 dst->type = strdup_or_goto(src->type, out_err);
2240 ppfield = &(dst->field);
2242 *ppfield = zalloc(sizeof(*field));
2245 (*ppfield)->name = strdup_or_goto(field->name, out_err);
2246 (*ppfield)->index = field->index;
2247 (*ppfield)->ref = field->ref;
2248 field = field->next;
2249 ppfield = &((*ppfield)->next);
2256 int perf_probe_event__copy(struct perf_probe_event *dst,
2257 struct perf_probe_event *src)
2261 dst->event = strdup_or_goto(src->event, out_err);
2262 dst->group = strdup_or_goto(src->group, out_err);
2263 dst->target = strdup_or_goto(src->target, out_err);
2264 dst->uprobes = src->uprobes;
2266 if (perf_probe_point__copy(&dst->point, &src->point) < 0)
2269 dst->args = zalloc(sizeof(struct perf_probe_arg) * src->nargs);
2272 dst->nargs = src->nargs;
2274 for (i = 0; i < src->nargs; i++)
2275 if (perf_probe_arg__copy(&dst->args[i], &src->args[i]) < 0)
2280 clear_perf_probe_event(dst);
2284 void clear_probe_trace_event(struct probe_trace_event *tev)
2286 struct probe_trace_arg_ref *ref, *next;
2291 free(tev->point.symbol);
2292 free(tev->point.realname);
2293 free(tev->point.module);
2294 for (i = 0; i < tev->nargs; i++) {
2295 free(tev->args[i].name);
2296 free(tev->args[i].value);
2297 free(tev->args[i].type);
2298 ref = tev->args[i].ref;
2306 memset(tev, 0, sizeof(*tev));
2309 struct kprobe_blacklist_node {
2310 struct list_head list;
2311 unsigned long start;
2316 static void kprobe_blacklist__delete(struct list_head *blacklist)
2318 struct kprobe_blacklist_node *node;
2320 while (!list_empty(blacklist)) {
2321 node = list_first_entry(blacklist,
2322 struct kprobe_blacklist_node, list);
2323 list_del(&node->list);
2329 static int kprobe_blacklist__load(struct list_head *blacklist)
2331 struct kprobe_blacklist_node *node;
2332 const char *__debugfs = debugfs__mountpoint();
2333 char buf[PATH_MAX], *p;
2337 if (__debugfs == NULL)
2340 ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs);
2344 fp = fopen(buf, "r");
2349 while (fgets(buf, PATH_MAX, fp)) {
2350 node = zalloc(sizeof(*node));
2355 INIT_LIST_HEAD(&node->list);
2356 list_add_tail(&node->list, blacklist);
2357 if (sscanf(buf, "0x%lx-0x%lx", &node->start, &node->end) != 2) {
2361 p = strchr(buf, '\t');
2364 if (p[strlen(p) - 1] == '\n')
2365 p[strlen(p) - 1] = '\0';
2367 p = (char *)"unknown";
2368 node->symbol = strdup(p);
2369 if (!node->symbol) {
2373 pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
2374 node->start, node->end, node->symbol);
2378 kprobe_blacklist__delete(blacklist);
2384 static struct kprobe_blacklist_node *
2385 kprobe_blacklist__find_by_address(struct list_head *blacklist,
2386 unsigned long address)
2388 struct kprobe_blacklist_node *node;
2390 list_for_each_entry(node, blacklist, list) {
2391 if (node->start <= address && address <= node->end)
2398 static LIST_HEAD(kprobe_blacklist);
2400 static void kprobe_blacklist__init(void)
2402 if (!list_empty(&kprobe_blacklist))
2405 if (kprobe_blacklist__load(&kprobe_blacklist) < 0)
2406 pr_debug("No kprobe blacklist support, ignored\n");
2409 static void kprobe_blacklist__release(void)
2411 kprobe_blacklist__delete(&kprobe_blacklist);
2414 static bool kprobe_blacklist__listed(unsigned long address)
2416 return !!kprobe_blacklist__find_by_address(&kprobe_blacklist, address);
2419 static int perf_probe_event__sprintf(const char *group, const char *event,
2420 struct perf_probe_event *pev,
2422 struct strbuf *result)
2427 if (asprintf(&buf, "%s:%s", group, event) < 0)
2429 ret = strbuf_addf(result, " %-20s (on ", buf);
2434 /* Synthesize only event probe point */
2435 buf = synthesize_perf_probe_point(&pev->point);
2438 ret = strbuf_addstr(result, buf);
2442 ret = strbuf_addf(result, " in %s", module);
2444 if (!ret && pev->nargs > 0) {
2445 ret = strbuf_add(result, " with", 5);
2446 for (i = 0; !ret && i < pev->nargs; i++) {
2447 buf = synthesize_perf_probe_arg(&pev->args[i]);
2450 ret = strbuf_addf(result, " %s", buf);
2455 ret = strbuf_addch(result, ')');
2461 int show_perf_probe_event(const char *group, const char *event,
2462 struct perf_probe_event *pev,
2463 const char *module, bool use_stdout)
2465 struct strbuf buf = STRBUF_INIT;
2468 ret = perf_probe_event__sprintf(group, event, pev, module, &buf);
2471 printf("%s\n", buf.buf);
2473 pr_info("%s\n", buf.buf);
2475 strbuf_release(&buf);
2480 static bool filter_probe_trace_event(struct probe_trace_event *tev,
2481 struct strfilter *filter)
2485 /* At first, check the event name itself */
2486 if (strfilter__compare(filter, tev->event))
2489 /* Next, check the combination of name and group */
2490 if (e_snprintf(tmp, 128, "%s:%s", tev->group, tev->event) < 0)
2492 return strfilter__compare(filter, tmp);
2495 static int __show_perf_probe_events(int fd, bool is_kprobe,
2496 struct strfilter *filter)
2499 struct probe_trace_event tev;
2500 struct perf_probe_event pev;
2501 struct strlist *rawlist;
2502 struct str_node *ent;
2504 memset(&tev, 0, sizeof(tev));
2505 memset(&pev, 0, sizeof(pev));
2507 rawlist = probe_file__get_rawlist(fd);
2511 strlist__for_each_entry(ent, rawlist) {
2512 ret = parse_probe_trace_command(ent->s, &tev);
2514 if (!filter_probe_trace_event(&tev, filter))
2516 ret = convert_to_perf_probe_event(&tev, &pev,
2520 ret = show_perf_probe_event(pev.group, pev.event,
2521 &pev, tev.point.module,
2525 clear_perf_probe_event(&pev);
2526 clear_probe_trace_event(&tev);
2530 strlist__delete(rawlist);
2531 /* Cleanup cached debuginfo if needed */
2532 debuginfo_cache__exit();
2537 /* List up current perf-probe events */
2538 int show_perf_probe_events(struct strfilter *filter)
2540 int kp_fd, up_fd, ret;
2544 if (probe_conf.cache)
2545 return probe_cache__show_all_caches(filter);
2547 ret = init_probe_symbol_maps(false);
2551 ret = probe_file__open_both(&kp_fd, &up_fd, 0);
2556 ret = __show_perf_probe_events(kp_fd, true, filter);
2557 if (up_fd >= 0 && ret >= 0)
2558 ret = __show_perf_probe_events(up_fd, false, filter);
2563 exit_probe_symbol_maps();
2568 static int get_new_event_name(char *buf, size_t len, const char *base,
2569 struct strlist *namelist, bool allow_suffix)
2576 nbase = strdup(base);
2580 /* Cut off the dot suffixes (e.g. .const, .isra)*/
2581 p = strchr(nbase, '.');
2582 if (p && p != nbase)
2585 /* Try no suffix number */
2586 ret = e_snprintf(buf, len, "%s", nbase);
2588 pr_debug("snprintf() failed: %d\n", ret);
2591 if (!strlist__has_entry(namelist, buf))
2594 if (!allow_suffix) {
2595 pr_warning("Error: event \"%s\" already exists.\n"
2596 " Hint: Remove existing event by 'perf probe -d'\n"
2597 " or force duplicates by 'perf probe -f'\n"
2598 " or set 'force=yes' in BPF source.\n",
2604 /* Try to add suffix */
2605 for (i = 1; i < MAX_EVENT_INDEX; i++) {
2606 ret = e_snprintf(buf, len, "%s_%d", nbase, i);
2608 pr_debug("snprintf() failed: %d\n", ret);
2611 if (!strlist__has_entry(namelist, buf))
2614 if (i == MAX_EVENT_INDEX) {
2615 pr_warning("Too many events are on the same function.\n");
2622 /* Final validation */
2623 if (ret >= 0 && !is_c_func_name(buf)) {
2624 pr_warning("Internal error: \"%s\" is an invalid event name.\n",
2632 /* Warn if the current kernel's uprobe implementation is old */
2633 static void warn_uprobe_event_compat(struct probe_trace_event *tev)
2636 char *buf = synthesize_probe_trace_command(tev);
2638 /* Old uprobe event doesn't support memory dereference */
2639 if (!tev->uprobes || tev->nargs == 0 || !buf)
2642 for (i = 0; i < tev->nargs; i++)
2643 if (strglobmatch(tev->args[i].value, "[$@+-]*")) {
2644 pr_warning("Please upgrade your kernel to at least "
2645 "3.14 to have access to feature %s\n",
2646 tev->args[i].value);
2653 /* Set new name from original perf_probe_event and namelist */
2654 static int probe_trace_event__set_name(struct probe_trace_event *tev,
2655 struct perf_probe_event *pev,
2656 struct strlist *namelist,
2659 const char *event, *group;
2663 /* If probe_event or trace_event already have the name, reuse it */
2664 if (pev->event && !pev->sdt)
2666 else if (tev->event)
2669 /* Or generate new one from probe point */
2670 if (pev->point.function &&
2671 (strncmp(pev->point.function, "0x", 2) != 0) &&
2672 !strisglob(pev->point.function))
2673 event = pev->point.function;
2675 event = tev->point.realname;
2677 if (pev->group && !pev->sdt)
2679 else if (tev->group)
2682 group = PERFPROBE_GROUP;
2684 /* Get an unused new event name */
2685 ret = get_new_event_name(buf, 64, event,
2686 namelist, allow_suffix);
2692 tev->event = strdup(event);
2693 tev->group = strdup(group);
2694 if (tev->event == NULL || tev->group == NULL)
2697 /* Add added event name to namelist */
2698 strlist__add(namelist, event);
2702 static int __open_probe_file_and_namelist(bool uprobe,
2703 struct strlist **namelist)
2707 fd = probe_file__open(PF_FL_RW | (uprobe ? PF_FL_UPROBE : 0));
2711 /* Get current event names */
2712 *namelist = probe_file__get_namelist(fd);
2714 pr_debug("Failed to get current event list.\n");
2721 static int __add_probe_trace_events(struct perf_probe_event *pev,
2722 struct probe_trace_event *tevs,
2723 int ntevs, bool allow_suffix)
2725 int i, fd[2] = {-1, -1}, up, ret;
2726 struct probe_trace_event *tev = NULL;
2727 struct probe_cache *cache = NULL;
2728 struct strlist *namelist[2] = {NULL, NULL};
2730 up = pev->uprobes ? 1 : 0;
2731 fd[up] = __open_probe_file_and_namelist(up, &namelist[up]);
2736 for (i = 0; i < ntevs; i++) {
2738 up = tev->uprobes ? 1 : 0;
2739 if (fd[up] == -1) { /* Open the kprobe/uprobe_events */
2740 fd[up] = __open_probe_file_and_namelist(up,
2745 /* Skip if the symbol is out of .text or blacklisted */
2746 if (!tev->point.symbol && !pev->uprobes)
2749 /* Set new name for tev (and update namelist) */
2750 ret = probe_trace_event__set_name(tev, pev, namelist[up],
2755 ret = probe_file__add_event(fd[up], tev);
2760 * Probes after the first probe which comes from same
2761 * user input are always allowed to add suffix, because
2762 * there might be several addresses corresponding to
2765 allow_suffix = true;
2767 if (ret == -EINVAL && pev->uprobes)
2768 warn_uprobe_event_compat(tev);
2769 if (ret == 0 && probe_conf.cache) {
2770 cache = probe_cache__new(pev->target);
2772 probe_cache__add_entry(cache, pev, tevs, ntevs) < 0 ||
2773 probe_cache__commit(cache) < 0)
2774 pr_warning("Failed to add event to probe cache\n");
2775 probe_cache__delete(cache);
2779 for (up = 0; up < 2; up++) {
2780 strlist__delete(namelist[up]);
2787 static int find_probe_functions(struct map *map, char *name,
2788 struct symbol **syms)
2792 struct rb_node *tmp;
2794 if (map__load(map) < 0)
2797 map__for_each_symbol(map, sym, tmp) {
2798 if (strglobmatch(sym->name, name)) {
2800 if (syms && found < probe_conf.max_probes)
2801 syms[found - 1] = sym;
2808 void __weak arch__fix_tev_from_maps(struct perf_probe_event *pev __maybe_unused,
2809 struct probe_trace_event *tev __maybe_unused,
2810 struct map *map __maybe_unused,
2811 struct symbol *sym __maybe_unused) { }
2814 * Find probe function addresses from map.
2815 * Return an error or the number of found probe_trace_event
2817 static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
2818 struct probe_trace_event **tevs)
2820 struct map *map = NULL;
2821 struct ref_reloc_sym *reloc_sym = NULL;
2823 struct symbol **syms = NULL;
2824 struct probe_trace_event *tev;
2825 struct perf_probe_point *pp = &pev->point;
2826 struct probe_trace_point *tp;
2827 int num_matched_functions;
2828 int ret, i, j, skipped = 0;
2831 map = get_target_map(pev->target, pev->uprobes);
2837 syms = malloc(sizeof(struct symbol *) * probe_conf.max_probes);
2844 * Load matched symbols: Since the different local symbols may have
2845 * same name but different addresses, this lists all the symbols.
2847 num_matched_functions = find_probe_functions(map, pp->function, syms);
2848 if (num_matched_functions == 0) {
2849 pr_err("Failed to find symbol %s in %s\n", pp->function,
2850 pev->target ? : "kernel");
2853 } else if (num_matched_functions > probe_conf.max_probes) {
2854 pr_err("Too many functions matched in %s\n",
2855 pev->target ? : "kernel");
2860 /* Note that the symbols in the kmodule are not relocated */
2861 if (!pev->uprobes && !pp->retprobe && !pev->target) {
2862 reloc_sym = kernel_get_ref_reloc_sym(NULL);
2864 pr_warning("Relocated base symbol is not found!\n");
2870 /* Setup result trace-probe-events */
2871 *tevs = zalloc(sizeof(*tev) * num_matched_functions);
2879 for (j = 0; j < num_matched_functions; j++) {
2882 tev = (*tevs) + ret;
2884 if (ret == num_matched_functions) {
2885 pr_warning("Too many symbols are listed. Skip it.\n");
2890 if (pp->offset > sym->end - sym->start) {
2891 pr_warning("Offset %ld is bigger than the size of %s\n",
2892 pp->offset, sym->name);
2896 /* Add one probe point */
2897 tp->address = map->unmap_ip(map, sym->start) + pp->offset;
2899 /* Check the kprobe (not in module) is within .text */
2900 if (!pev->uprobes && !pev->target &&
2901 kprobe_warn_out_range(sym->name, tp->address)) {
2902 tp->symbol = NULL; /* Skip it */
2904 } else if (reloc_sym) {
2905 tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
2906 tp->offset = tp->address - reloc_sym->addr;
2908 tp->symbol = strdup_or_goto(sym->name, nomem_out);
2909 tp->offset = pp->offset;
2911 tp->realname = strdup_or_goto(sym->name, nomem_out);
2913 tp->retprobe = pp->retprobe;
2916 tev->point.module = strdup_or_goto(pev->target,
2919 mod_name = find_module_name(pev->target);
2921 strdup(mod_name ? mod_name : pev->target);
2923 if (!tev->point.module)
2927 tev->uprobes = pev->uprobes;
2928 tev->nargs = pev->nargs;
2930 tev->args = zalloc(sizeof(struct probe_trace_arg) *
2932 if (tev->args == NULL)
2935 for (i = 0; i < tev->nargs; i++) {
2936 if (pev->args[i].name)
2938 strdup_or_goto(pev->args[i].name,
2941 tev->args[i].value = strdup_or_goto(pev->args[i].var,
2943 if (pev->args[i].type)
2945 strdup_or_goto(pev->args[i].type,
2948 arch__fix_tev_from_maps(pev, tev, map, sym);
2950 if (ret == skipped) {
2963 clear_probe_trace_events(*tevs, num_matched_functions);
2968 static int try_to_find_absolute_address(struct perf_probe_event *pev,
2969 struct probe_trace_event **tevs)
2971 struct perf_probe_point *pp = &pev->point;
2972 struct probe_trace_event *tev;
2973 struct probe_trace_point *tp;
2976 if (!(pev->point.function && !strncmp(pev->point.function, "0x", 2)))
2978 if (perf_probe_event_need_dwarf(pev))
2982 * This is 'perf probe /lib/libc.so 0xabcd'. Try to probe at
2985 * Only one tev can be generated by this.
2987 *tevs = zalloc(sizeof(*tev));
2995 * Don't use tp->offset, use address directly, because
2996 * in synthesize_probe_trace_command() address cannot be
2999 tp->address = pev->point.abs_address;
3000 tp->retprobe = pp->retprobe;
3001 tev->uprobes = pev->uprobes;
3005 * Give it a '0x' leading symbol name.
3006 * In __add_probe_trace_events, a NULL symbol is interpreted as
3009 if (asprintf(&tp->symbol, "0x%lx", tp->address) < 0)
3012 /* For kprobe, check range */
3013 if ((!tev->uprobes) &&
3014 (kprobe_warn_out_range(tev->point.symbol,
3015 tev->point.address))) {
3020 if (asprintf(&tp->realname, "abs_%lx", tp->address) < 0)
3024 tp->module = strdup(pev->target);
3030 tev->group = strdup(pev->group);
3036 tev->event = strdup(pev->event);
3041 tev->nargs = pev->nargs;
3042 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
3047 for (i = 0; i < tev->nargs; i++)
3048 copy_to_probe_trace_arg(&tev->args[i], &pev->args[i]);
3054 clear_probe_trace_events(*tevs, 1);
3060 /* Concatinate two arrays */
3061 static void *memcat(void *a, size_t sz_a, void *b, size_t sz_b)
3065 ret = malloc(sz_a + sz_b);
3067 memcpy(ret, a, sz_a);
3068 memcpy(ret + sz_a, b, sz_b);
3074 concat_probe_trace_events(struct probe_trace_event **tevs, int *ntevs,
3075 struct probe_trace_event **tevs2, int ntevs2)
3077 struct probe_trace_event *new_tevs;
3087 if (*ntevs + ntevs2 > probe_conf.max_probes)
3090 /* Concatinate the array of probe_trace_event */
3091 new_tevs = memcat(*tevs, (*ntevs) * sizeof(**tevs),
3092 *tevs2, ntevs2 * sizeof(**tevs2));
3102 clear_probe_trace_events(*tevs2, ntevs2);
3109 * Try to find probe_trace_event from given probe caches. Return the number
3110 * of cached events found, if an error occurs return the error.
3112 static int find_cached_events(struct perf_probe_event *pev,
3113 struct probe_trace_event **tevs,
3116 struct probe_cache *cache;
3117 struct probe_cache_entry *entry;
3118 struct probe_trace_event *tmp_tevs = NULL;
3122 cache = probe_cache__new(target);
3123 /* Return 0 ("not found") if the target has no probe cache. */
3127 for_each_probe_cache_entry(entry, cache) {
3128 /* Skip the cache entry which has no name */
3129 if (!entry->pev.event || !entry->pev.group)
3131 if ((!pev->group || strglobmatch(entry->pev.group, pev->group)) &&
3132 strglobmatch(entry->pev.event, pev->event)) {
3133 ret = probe_cache_entry__get_event(entry, &tmp_tevs);
3135 ret = concat_probe_trace_events(tevs, &ntevs,
3141 probe_cache__delete(cache);
3143 clear_probe_trace_events(*tevs, ntevs);
3147 if (ntevs > 0 && target && target[0] == '/')
3148 pev->uprobes = true;
3154 /* Try to find probe_trace_event from all probe caches */
3155 static int find_cached_events_all(struct perf_probe_event *pev,
3156 struct probe_trace_event **tevs)
3158 struct probe_trace_event *tmp_tevs = NULL;
3159 struct strlist *bidlist;
3160 struct str_node *nd;
3165 /* Get the buildid list of all valid caches */
3166 bidlist = build_id_cache__list_all(true);
3169 pr_debug("Failed to get buildids: %d\n", ret);
3174 strlist__for_each_entry(nd, bidlist) {
3175 pathname = build_id_cache__origname(nd->s);
3176 ret = find_cached_events(pev, &tmp_tevs, pathname);
3177 /* In the case of cnt == 0, we just skip it */
3179 ret = concat_probe_trace_events(tevs, &ntevs,
3185 strlist__delete(bidlist);
3188 clear_probe_trace_events(*tevs, ntevs);
3196 static int find_probe_trace_events_from_cache(struct perf_probe_event *pev,
3197 struct probe_trace_event **tevs)
3199 struct probe_cache *cache;
3200 struct probe_cache_entry *entry;
3201 struct probe_trace_event *tev;
3202 struct str_node *node;
3206 /* For SDT/cached events, we use special search functions */
3208 return find_cached_events_all(pev, tevs);
3210 return find_cached_events(pev, tevs, pev->target);
3212 cache = probe_cache__new(pev->target);
3216 entry = probe_cache__find(cache, pev);
3218 /* SDT must be in the cache */
3219 ret = pev->sdt ? -ENOENT : 0;
3223 ret = strlist__nr_entries(entry->tevlist);
3224 if (ret > probe_conf.max_probes) {
3225 pr_debug("Too many entries matched in the cache of %s\n",
3226 pev->target ? : "kernel");
3231 *tevs = zalloc(ret * sizeof(*tev));
3238 strlist__for_each_entry(node, entry->tevlist) {
3239 tev = &(*tevs)[i++];
3240 ret = parse_probe_trace_command(node->s, tev);
3243 /* Set the uprobes attribute as same as original */
3244 tev->uprobes = pev->uprobes;
3249 probe_cache__delete(cache);
3253 static int convert_to_probe_trace_events(struct perf_probe_event *pev,
3254 struct probe_trace_event **tevs)
3258 if (!pev->group && !pev->sdt) {
3259 /* Set group name if not given */
3260 if (!pev->uprobes) {
3261 pev->group = strdup(PERFPROBE_GROUP);
3262 ret = pev->group ? 0 : -ENOMEM;
3264 ret = convert_exec_to_group(pev->target, &pev->group);
3266 pr_warning("Failed to make a group name.\n");
3271 ret = try_to_find_absolute_address(pev, tevs);
3275 /* At first, we need to lookup cache entry */
3276 ret = find_probe_trace_events_from_cache(pev, tevs);
3277 if (ret > 0 || pev->sdt) /* SDT can be found only in the cache */
3278 return ret == 0 ? -ENOENT : ret; /* Found in probe cache */
3280 /* Convert perf_probe_event with debuginfo */
3281 ret = try_to_find_probe_trace_events(pev, tevs);
3283 return ret; /* Found in debuginfo or got an error */
3285 return find_probe_trace_events_from_map(pev, tevs);
3288 int convert_perf_probe_events(struct perf_probe_event *pevs, int npevs)
3292 /* Loop 1: convert all events */
3293 for (i = 0; i < npevs; i++) {
3294 /* Init kprobe blacklist if needed */
3295 if (!pevs[i].uprobes)
3296 kprobe_blacklist__init();
3297 /* Convert with or without debuginfo */
3298 ret = convert_to_probe_trace_events(&pevs[i], &pevs[i].tevs);
3301 pevs[i].ntevs = ret;
3303 /* This just release blacklist only if allocated */
3304 kprobe_blacklist__release();
3309 static int show_probe_trace_event(struct probe_trace_event *tev)
3311 char *buf = synthesize_probe_trace_command(tev);
3314 pr_debug("Failed to synthesize probe trace event.\n");
3318 /* Showing definition always go stdout */
3319 printf("%s\n", buf);
3325 int show_probe_trace_events(struct perf_probe_event *pevs, int npevs)
3327 struct strlist *namelist = strlist__new(NULL, NULL);
3328 struct probe_trace_event *tev;
3329 struct perf_probe_event *pev;
3335 for (j = 0; j < npevs && !ret; j++) {
3337 for (i = 0; i < pev->ntevs && !ret; i++) {
3338 tev = &pev->tevs[i];
3339 /* Skip if the symbol is out of .text or blacklisted */
3340 if (!tev->point.symbol && !pev->uprobes)
3343 /* Set new name for tev (and update namelist) */
3344 ret = probe_trace_event__set_name(tev, pev,
3347 ret = show_probe_trace_event(tev);
3350 strlist__delete(namelist);
3355 int apply_perf_probe_events(struct perf_probe_event *pevs, int npevs)
3359 /* Loop 2: add all events */
3360 for (i = 0; i < npevs; i++) {
3361 ret = __add_probe_trace_events(&pevs[i], pevs[i].tevs,
3363 probe_conf.force_add);
3370 void cleanup_perf_probe_events(struct perf_probe_event *pevs, int npevs)
3374 /* Loop 3: cleanup and free trace events */
3375 for (i = 0; i < npevs; i++) {
3376 for (j = 0; j < pevs[i].ntevs; j++)
3377 clear_probe_trace_event(&pevs[i].tevs[j]);
3378 zfree(&pevs[i].tevs);
3380 clear_perf_probe_event(&pevs[i]);
3384 int add_perf_probe_events(struct perf_probe_event *pevs, int npevs)
3388 ret = init_probe_symbol_maps(pevs->uprobes);
3392 ret = convert_perf_probe_events(pevs, npevs);
3394 ret = apply_perf_probe_events(pevs, npevs);
3396 cleanup_perf_probe_events(pevs, npevs);
3398 exit_probe_symbol_maps();
3402 int del_perf_probe_events(struct strfilter *filter)
3404 int ret, ret2, ufd = -1, kfd = -1;
3405 char *str = strfilter__string(filter);
3410 /* Get current event names */
3411 ret = probe_file__open_both(&kfd, &ufd, PF_FL_RW);
3415 ret = probe_file__del_events(kfd, filter);
3416 if (ret < 0 && ret != -ENOENT)
3419 ret2 = probe_file__del_events(ufd, filter);
3420 if (ret2 < 0 && ret2 != -ENOENT) {
3437 int show_available_funcs(const char *target, struct strfilter *_filter,
3444 ret = init_probe_symbol_maps(user);
3448 /* Get a symbol map */
3449 map = get_target_map(target, user);
3451 pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
3455 ret = map__load(map);
3458 char *str = strfilter__string(_filter);
3459 pr_err("Failed to find symbols matched to \"%s\"\n",
3463 pr_err("Failed to load symbols in %s\n",
3464 (target) ? : "kernel");
3467 if (!dso__sorted_by_name(map->dso, map->type))
3468 dso__sort_by_name(map->dso, map->type);
3470 /* Show all (filtered) symbols */
3473 for (nd = rb_first(&map->dso->symbol_names[map->type]); nd; nd = rb_next(nd)) {
3474 struct symbol_name_rb_node *pos = rb_entry(nd, struct symbol_name_rb_node, rb_node);
3476 if (strfilter__compare(_filter, pos->sym.name))
3477 printf("%s\n", pos->sym.name);
3482 exit_probe_symbol_maps();
3487 int copy_to_probe_trace_arg(struct probe_trace_arg *tvar,
3488 struct perf_probe_arg *pvar)
3490 tvar->value = strdup(pvar->var);
3491 if (tvar->value == NULL)
3494 tvar->type = strdup(pvar->type);
3495 if (tvar->type == NULL)
3499 tvar->name = strdup(pvar->name);
3500 if (tvar->name == NULL)