2 * probe-event.c : perf-probe definition to probe_events format converter
4 * Written by Masami Hiramatsu <mhiramat@redhat.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #include <sys/utsname.h>
24 #include <sys/types.h>
39 #include "strfilter.h"
45 #include <api/fs/fs.h>
46 #include "trace-event.h" /* For __maybe_unused */
47 #include "probe-event.h"
48 #include "probe-finder.h"
49 #include "probe-file.h"
53 #include "sane_ctype.h"
55 #define PERFPROBE_GROUP "probe"
57 bool probe_event_dry_run; /* Dry run flag */
58 struct probe_conf probe_conf;
60 #define semantic_error(msg ...) pr_err("Semantic error :" msg)
62 int e_snprintf(char *str, size_t size, const char *format, ...)
67 ret = vsnprintf(str, size, format, ap);
74 static struct machine *host_machine;
76 /* Initialize symbol maps and path of vmlinux/modules */
77 int init_probe_symbol_maps(bool user_only)
81 symbol_conf.sort_by_name = true;
82 symbol_conf.allow_aliases = true;
83 ret = symbol__init(NULL);
85 pr_debug("Failed to init symbol map.\n");
89 if (host_machine || user_only) /* already initialized */
92 if (symbol_conf.vmlinux_name)
93 pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
95 host_machine = machine__new_host();
97 pr_debug("machine__new_host() failed.\n");
103 pr_warning("Failed to init vmlinux path.\n");
107 void exit_probe_symbol_maps(void)
109 machine__delete(host_machine);
114 static struct ref_reloc_sym *kernel_get_ref_reloc_sym(void)
116 /* kmap->ref_reloc_sym should be set if host_machine is initialized */
118 struct map *map = machine__kernel_map(host_machine);
120 if (map__load(map) < 0)
123 kmap = map__kmap(map);
126 return kmap->ref_reloc_sym;
129 static int kernel_get_symbol_address_by_name(const char *name, u64 *addr,
130 bool reloc, bool reladdr)
132 struct ref_reloc_sym *reloc_sym;
136 /* ref_reloc_sym is just a label. Need a special fix*/
137 reloc_sym = kernel_get_ref_reloc_sym();
138 if (reloc_sym && strcmp(name, reloc_sym->name) == 0)
139 *addr = (reloc) ? reloc_sym->addr : reloc_sym->unrelocated_addr;
141 sym = machine__find_kernel_symbol_by_name(host_machine, name, &map);
144 *addr = map->unmap_ip(map, sym->start) -
145 ((reloc) ? 0 : map->reloc) -
146 ((reladdr) ? map->start : 0);
151 static struct map *kernel_get_module_map(const char *module)
153 struct maps *maps = machine__kernel_maps(host_machine);
156 /* A file path -- this is an offline module */
157 if (module && strchr(module, '/'))
158 return dso__new_map(module);
163 for (pos = maps__first(maps); pos; pos = map__next(pos)) {
164 /* short_name is "[module]" */
165 if (strncmp(pos->dso->short_name + 1, module,
166 pos->dso->short_name_len - 2) == 0 &&
167 module[pos->dso->short_name_len - 2] == '\0') {
168 return map__get(pos);
174 struct map *get_target_map(const char *target, struct nsinfo *nsi, bool user)
176 /* Init maps of given executable or kernel */
180 map = dso__new_map(target);
182 map->dso->nsinfo = nsinfo__get(nsi);
185 return kernel_get_module_map(target);
189 static int convert_exec_to_group(const char *exec, char **result)
191 char *ptr1, *ptr2, *exec_copy;
195 exec_copy = strdup(exec);
199 ptr1 = basename(exec_copy);
205 for (ptr2 = ptr1; *ptr2 != '\0'; ptr2++) {
206 if (!isalnum(*ptr2) && *ptr2 != '_') {
212 ret = e_snprintf(buf, 64, "%s_%s", PERFPROBE_GROUP, ptr1);
216 *result = strdup(buf);
217 ret = *result ? 0 : -ENOMEM;
224 static void clear_perf_probe_point(struct perf_probe_point *pp)
231 static void clear_probe_trace_events(struct probe_trace_event *tevs, int ntevs)
235 for (i = 0; i < ntevs; i++)
236 clear_probe_trace_event(tevs + i);
239 static bool kprobe_blacklist__listed(unsigned long address);
240 static bool kprobe_warn_out_range(const char *symbol, unsigned long address)
245 /* Get the address of _etext for checking non-probable text symbol */
246 ret = kernel_get_symbol_address_by_name("_etext", &etext_addr,
249 if (ret == 0 && etext_addr < address)
250 pr_warning("%s is out of .text, skip it.\n", symbol);
251 else if (kprobe_blacklist__listed(address))
252 pr_warning("%s is blacklisted function, skip it.\n", symbol);
260 * @module can be module name of module file path. In case of path,
261 * inspect elf and find out what is actual module name.
262 * Caller has to free mod_name after using it.
264 static char *find_module_name(const char *module)
272 char *mod_name = NULL;
275 fd = open(module, O_RDONLY);
279 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
283 if (gelf_getehdr(elf, &ehdr) == NULL)
286 sec = elf_section_by_name(elf, &ehdr, &shdr,
287 ".gnu.linkonce.this_module", NULL);
291 data = elf_getdata(sec, NULL);
292 if (!data || !data->d_buf)
297 * '.gnu.linkonce.this_module' section of kernel module elf directly
298 * maps to 'struct module' from linux/module.h. This section contains
299 * actual module name which will be used by kernel after loading it.
300 * But, we cannot use 'struct module' here since linux/module.h is not
301 * exposed to user-space. Offset of 'name' has remained same from long
302 * time, so hardcoding it here.
304 if (ehdr.e_ident[EI_CLASS] == ELFCLASS32)
306 else /* expect ELFCLASS64 by default */
309 mod_name = strdup((char *)data->d_buf + name_offset);
318 #ifdef HAVE_DWARF_SUPPORT
320 static int kernel_get_module_dso(const char *module, struct dso **pdso)
324 const char *vmlinux_name;
328 char module_name[128];
330 snprintf(module_name, sizeof(module_name), "[%s]", module);
331 map = map_groups__find_by_name(&host_machine->kmaps, module_name);
336 pr_debug("Failed to find module %s.\n", module);
340 map = machine__kernel_map(host_machine);
343 vmlinux_name = symbol_conf.vmlinux_name;
346 ret = dso__load_vmlinux(dso, map, vmlinux_name, false);
348 ret = dso__load_vmlinux_path(dso, map);
355 * Some binaries like glibc have special symbols which are on the symbol
356 * table, but not in the debuginfo. If we can find the address of the
357 * symbol from map, we can translate the address back to the probe point.
359 static int find_alternative_probe_point(struct debuginfo *dinfo,
360 struct perf_probe_point *pp,
361 struct perf_probe_point *result,
362 const char *target, struct nsinfo *nsi,
365 struct map *map = NULL;
370 /* This can work only for function-name based one */
371 if (!pp->function || pp->file)
374 map = get_target_map(target, nsi, uprobes);
378 /* Find the address of given function */
379 map__for_each_symbol_by_name(map, pp->function, sym) {
381 address = sym->start;
383 address = map->unmap_ip(map, sym->start) - map->reloc;
390 pr_debug("Symbol %s address found : %" PRIx64 "\n",
391 pp->function, address);
393 ret = debuginfo__find_probe_point(dinfo, (unsigned long)address,
396 ret = (!ret) ? -ENOENT : ret;
398 result->offset += pp->offset;
399 result->line += pp->line;
400 result->retprobe = pp->retprobe;
410 static int get_alternative_probe_event(struct debuginfo *dinfo,
411 struct perf_probe_event *pev,
412 struct perf_probe_point *tmp)
416 memcpy(tmp, &pev->point, sizeof(*tmp));
417 memset(&pev->point, 0, sizeof(pev->point));
418 ret = find_alternative_probe_point(dinfo, tmp, &pev->point, pev->target,
419 pev->nsi, pev->uprobes);
421 memcpy(&pev->point, tmp, sizeof(*tmp));
426 static int get_alternative_line_range(struct debuginfo *dinfo,
427 struct line_range *lr,
428 const char *target, bool user)
430 struct perf_probe_point pp = { .function = lr->function,
433 struct perf_probe_point result;
436 memset(&result, 0, sizeof(result));
438 if (lr->end != INT_MAX)
439 len = lr->end - lr->start;
440 ret = find_alternative_probe_point(dinfo, &pp, &result,
443 lr->function = result.function;
444 lr->file = result.file;
445 lr->start = result.line;
446 if (lr->end != INT_MAX)
447 lr->end = lr->start + len;
448 clear_perf_probe_point(&pp);
453 /* Open new debuginfo of given module */
454 static struct debuginfo *open_debuginfo(const char *module, struct nsinfo *nsi,
457 const char *path = module;
458 char reason[STRERR_BUFSIZE];
459 struct debuginfo *ret = NULL;
460 struct dso *dso = NULL;
464 if (!module || !strchr(module, '/')) {
465 err = kernel_get_module_dso(module, &dso);
467 if (!dso || dso->load_errno == 0) {
468 if (!str_error_r(-err, reason, STRERR_BUFSIZE))
469 strcpy(reason, "(unknown)");
471 dso__strerror_load(dso, reason, STRERR_BUFSIZE);
473 pr_err("Failed to find the path for %s: %s\n",
474 module ?: "kernel", reason);
477 path = dso->long_name;
479 nsinfo__mountns_enter(nsi, &nsc);
480 ret = debuginfo__new(path);
481 if (!ret && !silent) {
482 pr_warning("The %s file has no debug information.\n", path);
483 if (!module || !strtailcmp(path, ".ko"))
484 pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, ");
486 pr_warning("Rebuild with -g, ");
487 pr_warning("or install an appropriate debuginfo package.\n");
489 nsinfo__mountns_exit(&nsc);
493 /* For caching the last debuginfo */
494 static struct debuginfo *debuginfo_cache;
495 static char *debuginfo_cache_path;
497 static struct debuginfo *debuginfo_cache__open(const char *module, bool silent)
499 const char *path = module;
501 /* If the module is NULL, it should be the kernel. */
505 if (debuginfo_cache_path && !strcmp(debuginfo_cache_path, path))
508 /* Copy module path */
509 free(debuginfo_cache_path);
510 debuginfo_cache_path = strdup(path);
511 if (!debuginfo_cache_path) {
512 debuginfo__delete(debuginfo_cache);
513 debuginfo_cache = NULL;
517 debuginfo_cache = open_debuginfo(module, NULL, silent);
518 if (!debuginfo_cache)
519 zfree(&debuginfo_cache_path);
521 return debuginfo_cache;
524 static void debuginfo_cache__exit(void)
526 debuginfo__delete(debuginfo_cache);
527 debuginfo_cache = NULL;
528 zfree(&debuginfo_cache_path);
532 static int get_text_start_address(const char *exec, unsigned long *address,
538 int fd, ret = -ENOENT;
541 nsinfo__mountns_enter(nsi, &nsc);
542 fd = open(exec, O_RDONLY);
543 nsinfo__mountns_exit(&nsc);
547 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
553 if (gelf_getehdr(elf, &ehdr) == NULL)
556 if (!elf_section_by_name(elf, &ehdr, &shdr, ".text", NULL))
559 *address = shdr.sh_addr - shdr.sh_offset;
570 * Convert trace point to probe point with debuginfo
572 static int find_perf_probe_point_from_dwarf(struct probe_trace_point *tp,
573 struct perf_probe_point *pp,
576 struct debuginfo *dinfo = NULL;
577 unsigned long stext = 0;
578 u64 addr = tp->address;
581 /* convert the address to dwarf address */
587 ret = get_text_start_address(tp->module, &stext, NULL);
591 } else if (tp->symbol) {
592 /* If the module is given, this returns relative address */
593 ret = kernel_get_symbol_address_by_name(tp->symbol, &addr,
594 false, !!tp->module);
600 pr_debug("try to find information at %" PRIx64 " in %s\n", addr,
601 tp->module ? : "kernel");
603 dinfo = debuginfo_cache__open(tp->module, verbose <= 0);
605 ret = debuginfo__find_probe_point(dinfo,
606 (unsigned long)addr, pp);
611 pp->retprobe = tp->retprobe;
615 pr_debug("Failed to find corresponding probes from debuginfo.\n");
616 return ret ? : -ENOENT;
619 /* Adjust symbol name and address */
620 static int post_process_probe_trace_point(struct probe_trace_point *tp,
621 struct map *map, unsigned long offs)
624 u64 addr = tp->address - offs;
626 sym = map__find_symbol(map, addr);
630 if (strcmp(sym->name, tp->symbol)) {
631 /* If we have no realname, use symbol for it */
633 tp->realname = tp->symbol;
636 tp->symbol = strdup(sym->name);
640 tp->offset = addr - sym->start;
647 * Rename DWARF symbols to ELF symbols -- gcc sometimes optimizes functions
648 * and generate new symbols with suffixes such as .constprop.N or .isra.N
649 * etc. Since those symbols are not recorded in DWARF, we have to find
650 * correct generated symbols from offline ELF binary.
651 * For online kernel or uprobes we don't need this because those are
652 * rebased on _text, or already a section relative address.
655 post_process_offline_probe_trace_events(struct probe_trace_event *tevs,
656 int ntevs, const char *pathname)
659 unsigned long stext = 0;
662 /* Prepare a map for offline binary */
663 map = dso__new_map(pathname);
664 if (!map || get_text_start_address(pathname, &stext, NULL) < 0) {
665 pr_warning("Failed to get ELF symbols for %s\n", pathname);
669 for (i = 0; i < ntevs; i++) {
670 ret = post_process_probe_trace_point(&tevs[i].point,
680 static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs,
681 int ntevs, const char *exec,
685 unsigned long stext = 0;
690 ret = get_text_start_address(exec, &stext, nsi);
694 for (i = 0; i < ntevs && ret >= 0; i++) {
695 /* point.address is the address of point.symbol + point.offset */
696 tevs[i].point.address -= stext;
697 tevs[i].point.module = strdup(exec);
698 if (!tevs[i].point.module) {
702 tevs[i].uprobes = true;
709 post_process_module_probe_trace_events(struct probe_trace_event *tevs,
710 int ntevs, const char *module,
711 struct debuginfo *dinfo)
713 Dwarf_Addr text_offs = 0;
715 char *mod_name = NULL;
721 map = get_target_map(module, NULL, false);
722 if (!map || debuginfo__get_text_offset(dinfo, &text_offs, true) < 0) {
723 pr_warning("Failed to get ELF symbols for %s\n", module);
727 mod_name = find_module_name(module);
728 for (i = 0; i < ntevs; i++) {
729 ret = post_process_probe_trace_point(&tevs[i].point,
730 map, (unsigned long)text_offs);
733 tevs[i].point.module =
734 strdup(mod_name ? mod_name : module);
735 if (!tevs[i].point.module) {
748 post_process_kernel_probe_trace_events(struct probe_trace_event *tevs,
751 struct ref_reloc_sym *reloc_sym;
755 /* Skip post process if the target is an offline kernel */
756 if (symbol_conf.ignore_vmlinux_buildid)
757 return post_process_offline_probe_trace_events(tevs, ntevs,
758 symbol_conf.vmlinux_name);
760 reloc_sym = kernel_get_ref_reloc_sym();
762 pr_warning("Relocated base symbol is not found!\n");
766 for (i = 0; i < ntevs; i++) {
767 if (!tevs[i].point.address)
769 if (tevs[i].point.retprobe && !kretprobe_offset_is_supported())
771 /* If we found a wrong one, mark it by NULL symbol */
772 if (kprobe_warn_out_range(tevs[i].point.symbol,
773 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,
811 /* Currently ref_reloc_sym based probe is not for drivers */
812 ret = post_process_module_probe_trace_events(tevs, ntevs,
815 ret = post_process_kernel_probe_trace_events(tevs, ntevs);
818 arch__post_process_probe_trace_events(pev, ntevs);
823 /* Try to find perf_probe_event with debuginfo */
824 static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
825 struct probe_trace_event **tevs)
827 bool need_dwarf = perf_probe_event_need_dwarf(pev);
828 struct perf_probe_point tmp;
829 struct debuginfo *dinfo;
832 dinfo = open_debuginfo(pev->target, pev->nsi, !need_dwarf);
836 pr_debug("Could not open debuginfo. Try to use symbols.\n");
840 pr_debug("Try to find probe point from debuginfo.\n");
841 /* Searching trace events corresponding to a probe event */
842 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
844 if (ntevs == 0) { /* Not found, retry with an alternative */
845 ret = get_alternative_probe_event(dinfo, pev, &tmp);
847 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
849 * Write back to the original probe_event for
850 * setting appropriate (user given) event name
852 clear_perf_probe_point(&pev->point);
853 memcpy(&pev->point, &tmp, sizeof(tmp));
857 if (ntevs > 0) { /* Succeeded to find trace events */
858 pr_debug("Found %d probe_trace_events.\n", ntevs);
859 ret = post_process_probe_trace_events(pev, *tevs, ntevs,
860 pev->target, pev->uprobes, dinfo);
861 if (ret < 0 || ret == ntevs) {
862 pr_debug("Post processing failed or all events are skipped. (%d)\n", ret);
863 clear_probe_trace_events(*tevs, ntevs);
869 debuginfo__delete(dinfo);
871 if (ntevs == 0) { /* No error but failed to find probe point. */
872 pr_warning("Probe point '%s' not found.\n",
873 synthesize_perf_probe_point(&pev->point));
875 } else if (ntevs < 0) {
876 /* Error path : ntevs < 0 */
877 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
879 pr_warning("Warning: No dwarf info found in the vmlinux - "
880 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
882 pr_debug("Trying to use symbols.\n");
889 #define LINEBUF_SIZE 256
890 #define NR_ADDITIONAL_LINES 2
892 static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
894 char buf[LINEBUF_SIZE], sbuf[STRERR_BUFSIZE];
895 const char *color = show_num ? "" : PERF_COLOR_BLUE;
896 const char *prefix = NULL;
899 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
904 prefix = show_num ? "%7d " : " ";
905 color_fprintf(stdout, color, prefix, l);
907 color_fprintf(stdout, color, "%s", buf);
909 } while (strchr(buf, '\n') == NULL);
914 pr_warning("File read error: %s\n",
915 str_error_r(errno, sbuf, sizeof(sbuf)));
921 static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
923 int rv = __show_one_line(fp, l, skip, show_num);
925 pr_warning("Source file is shorter than expected.\n");
931 #define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
932 #define show_one_line(f,l) _show_one_line(f,l,false,false)
933 #define skip_one_line(f,l) _show_one_line(f,l,true,false)
934 #define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
937 * Show line-range always requires debuginfo to find source file and
940 static int __show_line_range(struct line_range *lr, const char *module,
945 struct debuginfo *dinfo;
949 char sbuf[STRERR_BUFSIZE];
951 /* Search a line range */
952 dinfo = open_debuginfo(module, NULL, false);
956 ret = debuginfo__find_line_range(dinfo, lr);
957 if (!ret) { /* Not found, retry with an alternative */
958 ret = get_alternative_line_range(dinfo, lr, module, user);
960 ret = debuginfo__find_line_range(dinfo, lr);
962 debuginfo__delete(dinfo);
963 if (ret == 0 || ret == -ENOENT) {
964 pr_warning("Specified source line is not found.\n");
966 } else if (ret < 0) {
967 pr_warning("Debuginfo analysis failed.\n");
971 /* Convert source file path */
973 ret = get_real_path(tmp, lr->comp_dir, &lr->path);
975 /* Free old path when new path is assigned */
980 pr_warning("Failed to find source file path.\n");
987 fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
988 lr->start - lr->offset);
990 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
992 fp = fopen(lr->path, "r");
994 pr_warning("Failed to open %s: %s\n", lr->path,
995 str_error_r(errno, sbuf, sizeof(sbuf)));
998 /* Skip to starting line number */
999 while (l < lr->start) {
1000 ret = skip_one_line(fp, l++);
1005 intlist__for_each_entry(ln, lr->line_list) {
1006 for (; ln->i > l; l++) {
1007 ret = show_one_line(fp, l - lr->offset);
1011 ret = show_one_line_with_num(fp, l++ - lr->offset);
1016 if (lr->end == INT_MAX)
1017 lr->end = l + NR_ADDITIONAL_LINES;
1018 while (l <= lr->end) {
1019 ret = show_one_line_or_eof(fp, l++ - lr->offset);
1028 int show_line_range(struct line_range *lr, const char *module,
1029 struct nsinfo *nsi, bool user)
1032 struct nscookie nsc;
1034 ret = init_probe_symbol_maps(user);
1037 nsinfo__mountns_enter(nsi, &nsc);
1038 ret = __show_line_range(lr, module, user);
1039 nsinfo__mountns_exit(&nsc);
1040 exit_probe_symbol_maps();
1045 static int show_available_vars_at(struct debuginfo *dinfo,
1046 struct perf_probe_event *pev,
1047 struct strfilter *_filter)
1051 struct str_node *node;
1052 struct variable_list *vls = NULL, *vl;
1053 struct perf_probe_point tmp;
1056 buf = synthesize_perf_probe_point(&pev->point);
1059 pr_debug("Searching variables at %s\n", buf);
1061 ret = debuginfo__find_available_vars_at(dinfo, pev, &vls);
1062 if (!ret) { /* Not found, retry with an alternative */
1063 ret = get_alternative_probe_event(dinfo, pev, &tmp);
1065 ret = debuginfo__find_available_vars_at(dinfo, pev,
1067 /* Release the old probe_point */
1068 clear_perf_probe_point(&tmp);
1072 if (ret == 0 || ret == -ENOENT) {
1073 pr_err("Failed to find the address of %s\n", buf);
1076 pr_warning("Debuginfo analysis failed.\n");
1080 /* Some variables are found */
1081 fprintf(stdout, "Available variables at %s\n", buf);
1082 for (i = 0; i < ret; i++) {
1085 * A probe point might be converted to
1086 * several trace points.
1088 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
1090 zfree(&vl->point.symbol);
1093 strlist__for_each_entry(node, vl->vars) {
1094 var = strchr(node->s, '\t') + 1;
1095 if (strfilter__compare(_filter, var)) {
1096 fprintf(stdout, "\t\t%s\n", node->s);
1100 strlist__delete(vl->vars);
1103 fprintf(stdout, "\t\t(No matched variables)\n");
1111 /* Show available variables on given probe point */
1112 int show_available_vars(struct perf_probe_event *pevs, int npevs,
1113 struct strfilter *_filter)
1116 struct debuginfo *dinfo;
1118 ret = init_probe_symbol_maps(pevs->uprobes);
1122 dinfo = open_debuginfo(pevs->target, pevs->nsi, false);
1130 for (i = 0; i < npevs && ret >= 0; i++)
1131 ret = show_available_vars_at(dinfo, &pevs[i], _filter);
1133 debuginfo__delete(dinfo);
1135 exit_probe_symbol_maps();
1139 #else /* !HAVE_DWARF_SUPPORT */
1141 static void debuginfo_cache__exit(void)
1146 find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused,
1147 struct perf_probe_point *pp __maybe_unused,
1148 bool is_kprobe __maybe_unused)
1153 static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
1154 struct probe_trace_event **tevs __maybe_unused)
1156 if (perf_probe_event_need_dwarf(pev)) {
1157 pr_warning("Debuginfo-analysis is not supported.\n");
1164 int show_line_range(struct line_range *lr __maybe_unused,
1165 const char *module __maybe_unused,
1166 struct nsinfo *nsi __maybe_unused,
1167 bool user __maybe_unused)
1169 pr_warning("Debuginfo-analysis is not supported.\n");
1173 int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
1174 int npevs __maybe_unused,
1175 struct strfilter *filter __maybe_unused)
1177 pr_warning("Debuginfo-analysis is not supported.\n");
1182 void line_range__clear(struct line_range *lr)
1188 intlist__delete(lr->line_list);
1189 memset(lr, 0, sizeof(*lr));
1192 int line_range__init(struct line_range *lr)
1194 memset(lr, 0, sizeof(*lr));
1195 lr->line_list = intlist__new(NULL);
1202 static int parse_line_num(char **ptr, int *val, const char *what)
1204 const char *start = *ptr;
1207 *val = strtol(*ptr, ptr, 0);
1208 if (errno || *ptr == start) {
1209 semantic_error("'%s' is not a valid number.\n", what);
1215 /* Check the name is good for event, group or function */
1216 static bool is_c_func_name(const char *name)
1218 if (!isalpha(*name) && *name != '_')
1220 while (*++name != '\0') {
1221 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
1228 * Stuff 'lr' according to the line range described by 'arg'.
1229 * The line range syntax is described by:
1231 * SRC[:SLN[+NUM|-ELN]]
1232 * FNC[@SRC][:SLN[+NUM|-ELN]]
1234 int parse_line_range_desc(const char *arg, struct line_range *lr)
1236 char *range, *file, *name = strdup(arg);
1245 range = strchr(name, ':');
1249 err = parse_line_num(&range, &lr->start, "start line");
1253 if (*range == '+' || *range == '-') {
1254 const char c = *range++;
1256 err = parse_line_num(&range, &lr->end, "end line");
1261 lr->end += lr->start;
1263 * Adjust the number of lines here.
1264 * If the number of lines == 1, the
1265 * the end of line should be equal to
1266 * the start of line.
1272 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
1275 if (lr->start > lr->end) {
1276 semantic_error("Start line must be smaller"
1277 " than end line.\n");
1280 if (*range != '\0') {
1281 semantic_error("Tailing with invalid str '%s'.\n", range);
1286 file = strchr(name, '@');
1289 lr->file = strdup(++file);
1290 if (lr->file == NULL) {
1294 lr->function = name;
1295 } else if (strchr(name, '/') || strchr(name, '.'))
1297 else if (is_c_func_name(name))/* We reuse it for checking funcname */
1298 lr->function = name;
1299 else { /* Invalid name */
1300 semantic_error("'%s' is not a valid function name.\n", name);
1311 static int parse_perf_probe_event_name(char **arg, struct perf_probe_event *pev)
1315 ptr = strpbrk_esc(*arg, ":");
1318 if (!pev->sdt && !is_c_func_name(*arg))
1320 pev->group = strdup_esc(*arg);
1327 pev->event = strdup_esc(*arg);
1328 if (pev->event == NULL)
1331 if (!pev->sdt && !is_c_func_name(pev->event)) {
1335 semantic_error("%s is bad for event name -it must "
1336 "follow C symbol-naming rule.\n", *arg);
1342 /* Parse probepoint definition. */
1343 static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
1345 struct perf_probe_point *pp = &pev->point;
1348 bool file_spec = false;
1353 * perf probe [GRP:][EVENT=]SRC[:LN|;PTN]
1354 * perf probe [GRP:][EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
1355 * perf probe %[GRP:]SDT_EVENT
1360 if (is_sdt_event(arg)) {
1366 ptr = strpbrk_esc(arg, ";=@+%");
1370 semantic_error("%s must be an SDT name.\n",
1374 /* This must be a target file name or build id */
1375 tmp = build_id_cache__complement(ptr + 1);
1377 pev->target = build_id_cache__origname(tmp);
1380 pev->target = strdup_esc(ptr + 1);
1385 ret = parse_perf_probe_event_name(&arg, pev);
1387 if (asprintf(&pev->point.function, "%%%s", pev->event) < 0)
1393 if (ptr && *ptr == '=') { /* Event name */
1396 ret = parse_perf_probe_event_name(&arg, pev);
1404 * Check arg is function or file name and copy it.
1406 * We consider arg to be a file spec if and only if it satisfies
1407 * all of the below criteria::
1408 * - it does not include any of "+@%",
1409 * - it includes one of ":;", and
1410 * - it has a period '.' in the name.
1412 * Otherwise, we consider arg to be a function specification.
1414 if (!strpbrk_esc(arg, "+@%")) {
1415 ptr = strpbrk_esc(arg, ";:");
1416 /* This is a file spec if it includes a '.' before ; or : */
1417 if (ptr && memchr(arg, '.', ptr - arg))
1421 ptr = strpbrk_esc(arg, ";:+@%");
1430 tmp = strdup_esc(arg);
1441 * Keep pp->function even if this is absolute address,
1442 * so it can mark whether abs_address is valid.
1443 * Which make 'perf probe lib.bin 0x0' possible.
1445 * Note that checking length of tmp is not needed
1446 * because when we access tmp[1] we know tmp[0] is '0',
1447 * so tmp[1] should always valid (but could be '\0').
1449 if (tmp && !strncmp(tmp, "0x", 2)) {
1450 pp->abs_address = strtoul(pp->function, &tmp, 0);
1452 semantic_error("Invalid absolute address.\n");
1458 /* Parse other options */
1462 if (c == ';') { /* Lazy pattern must be the last part */
1463 pp->lazy_line = strdup(arg); /* let leave escapes */
1464 if (pp->lazy_line == NULL)
1468 ptr = strpbrk_esc(arg, ";:+@%");
1474 case ':': /* Line number */
1475 pp->line = strtoul(arg, &tmp, 0);
1477 semantic_error("There is non-digit char"
1478 " in line number.\n");
1482 case '+': /* Byte offset from a symbol */
1483 pp->offset = strtoul(arg, &tmp, 0);
1485 semantic_error("There is non-digit character"
1490 case '@': /* File name */
1492 semantic_error("SRC@SRC is not allowed.\n");
1495 pp->file = strdup_esc(arg);
1496 if (pp->file == NULL)
1499 case '%': /* Probe places */
1500 if (strcmp(arg, "return") == 0) {
1502 } else { /* Others not supported yet */
1503 semantic_error("%%%s is not supported.\n", arg);
1507 default: /* Buggy case */
1508 pr_err("This program has a bug at %s:%d.\n",
1509 __FILE__, __LINE__);
1515 /* Exclusion check */
1516 if (pp->lazy_line && pp->line) {
1517 semantic_error("Lazy pattern can't be used with"
1522 if (pp->lazy_line && pp->offset) {
1523 semantic_error("Lazy pattern can't be used with offset.\n");
1527 if (pp->line && pp->offset) {
1528 semantic_error("Offset can't be used with line number.\n");
1532 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
1533 semantic_error("File always requires line number or "
1538 if (pp->offset && !pp->function) {
1539 semantic_error("Offset requires an entry function.\n");
1543 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
1544 semantic_error("Offset/Line/Lazy pattern can't be used with "
1549 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
1550 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
1555 /* Parse perf-probe event argument */
1556 static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
1558 char *tmp, *goodname;
1559 struct perf_probe_arg_field **fieldp;
1561 pr_debug("parsing arg: %s into ", str);
1563 tmp = strchr(str, '=');
1565 arg->name = strndup(str, tmp - str);
1566 if (arg->name == NULL)
1568 pr_debug("name:%s ", arg->name);
1572 tmp = strchr(str, ':');
1573 if (tmp) { /* Type setting */
1575 arg->type = strdup(tmp + 1);
1576 if (arg->type == NULL)
1578 pr_debug("type:%s ", arg->type);
1581 tmp = strpbrk(str, "-.[");
1582 if (!is_c_varname(str) || !tmp) {
1583 /* A variable, register, symbol or special value */
1584 arg->var = strdup(str);
1585 if (arg->var == NULL)
1587 pr_debug("%s\n", arg->var);
1591 /* Structure fields or array element */
1592 arg->var = strndup(str, tmp - str);
1593 if (arg->var == NULL)
1595 goodname = arg->var;
1596 pr_debug("%s, ", arg->var);
1597 fieldp = &arg->field;
1600 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
1601 if (*fieldp == NULL)
1603 if (*tmp == '[') { /* Array */
1605 (*fieldp)->index = strtol(str + 1, &tmp, 0);
1606 (*fieldp)->ref = true;
1607 if (*tmp != ']' || tmp == str + 1) {
1608 semantic_error("Array index must be a"
1615 } else { /* Structure */
1618 (*fieldp)->ref = false;
1619 } else if (tmp[1] == '>') {
1621 (*fieldp)->ref = true;
1623 semantic_error("Argument parse error: %s\n",
1627 tmp = strpbrk(str, "-.[");
1630 (*fieldp)->name = strndup(str, tmp - str);
1631 if ((*fieldp)->name == NULL)
1634 goodname = (*fieldp)->name;
1635 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
1636 fieldp = &(*fieldp)->next;
1639 (*fieldp)->name = strdup(str);
1640 if ((*fieldp)->name == NULL)
1643 goodname = (*fieldp)->name;
1644 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
1646 /* If no name is specified, set the last field name (not array index)*/
1648 arg->name = strdup(goodname);
1649 if (arg->name == NULL)
1655 /* Parse perf-probe event command */
1656 int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
1659 int argc, i, ret = 0;
1661 argv = argv_split(cmd, &argc);
1663 pr_debug("Failed to split arguments.\n");
1666 if (argc - 1 > MAX_PROBE_ARGS) {
1667 semantic_error("Too many probe arguments (%d).\n", argc - 1);
1671 /* Parse probe point */
1672 ret = parse_perf_probe_point(argv[0], pev);
1676 /* Copy arguments and ensure return probe has no C argument */
1677 pev->nargs = argc - 1;
1678 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1679 if (pev->args == NULL) {
1683 for (i = 0; i < pev->nargs && ret >= 0; i++) {
1684 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1686 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
1687 semantic_error("You can't specify local variable for"
1698 /* Returns true if *any* ARG is either C variable, $params or $vars. */
1699 bool perf_probe_with_var(struct perf_probe_event *pev)
1703 for (i = 0; i < pev->nargs; i++)
1704 if (is_c_varname(pev->args[i].var) ||
1705 !strcmp(pev->args[i].var, PROBE_ARG_PARAMS) ||
1706 !strcmp(pev->args[i].var, PROBE_ARG_VARS))
1711 /* Return true if this perf_probe_event requires debuginfo */
1712 bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
1714 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1717 if (perf_probe_with_var(pev))
1723 /* Parse probe_events event into struct probe_point */
1724 int parse_probe_trace_command(const char *cmd, struct probe_trace_event *tev)
1726 struct probe_trace_point *tp = &tev->point;
1729 char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
1733 pr_debug("Parsing probe_events: %s\n", cmd);
1734 argv = argv_split(cmd, &argc);
1736 pr_debug("Failed to split arguments.\n");
1740 semantic_error("Too few probe arguments.\n");
1745 /* Scan event and group name. */
1746 argv0_str = strdup(argv[0]);
1747 if (argv0_str == NULL) {
1751 fmt1_str = strtok_r(argv0_str, ":", &fmt);
1752 fmt2_str = strtok_r(NULL, "/", &fmt);
1753 fmt3_str = strtok_r(NULL, " \t", &fmt);
1754 if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
1755 || fmt3_str == NULL) {
1756 semantic_error("Failed to parse event name: %s\n", argv[0]);
1761 tev->group = strdup(fmt2_str);
1762 tev->event = strdup(fmt3_str);
1763 if (tev->group == NULL || tev->event == NULL) {
1767 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
1769 tp->retprobe = (pr == 'r');
1771 /* Scan module name(if there), function name and offset */
1772 p = strchr(argv[1], ':');
1774 tp->module = strndup(argv[1], p - argv[1]);
1779 tev->uprobes = (tp->module[0] == '/');
1783 fmt1_str = strtok_r(p, "+", &fmt);
1784 /* only the address started with 0x */
1785 if (fmt1_str[0] == '0') {
1787 * Fix a special case:
1788 * if address == 0, kernel reports something like:
1789 * p:probe_libc/abs_0 /lib/libc-2.18.so:0x (null) arg1=%ax
1790 * Newer kernel may fix that, but we want to
1791 * support old kernel also.
1793 if (strcmp(fmt1_str, "0x") == 0) {
1794 if (!argv[2] || strcmp(argv[2], "(null)")) {
1801 for (i = 2; argv[i + 1] != NULL; i++)
1802 argv[i] = argv[i + 1];
1807 tp->address = strtoul(fmt1_str, NULL, 0);
1809 /* Only the symbol-based probe has offset */
1810 tp->symbol = strdup(fmt1_str);
1811 if (tp->symbol == NULL) {
1815 fmt2_str = strtok_r(NULL, "", &fmt);
1816 if (fmt2_str == NULL)
1819 tp->offset = strtoul(fmt2_str, NULL, 10);
1823 fmt2_str = strchr(p, '(');
1825 tp->ref_ctr_offset = strtoul(fmt2_str + 1, NULL, 0);
1828 tev->nargs = argc - 2;
1829 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
1830 if (tev->args == NULL) {
1834 for (i = 0; i < tev->nargs; i++) {
1835 p = strchr(argv[i + 2], '=');
1836 if (p) /* We don't need which register is assigned. */
1840 tev->args[i].name = strdup(argv[i + 2]);
1841 /* TODO: parse regs and offset */
1842 tev->args[i].value = strdup(p);
1843 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1855 /* Compose only probe arg */
1856 char *synthesize_perf_probe_arg(struct perf_probe_arg *pa)
1858 struct perf_probe_arg_field *field = pa->field;
1863 if (strbuf_init(&buf, 64) < 0)
1866 if (pa->name && pa->var)
1867 err = strbuf_addf(&buf, "%s=%s", pa->name, pa->var);
1869 err = strbuf_addstr(&buf, pa->name ?: pa->var);
1874 if (field->name[0] == '[')
1875 err = strbuf_addstr(&buf, field->name);
1877 err = strbuf_addf(&buf, "%s%s", field->ref ? "->" : ".",
1879 field = field->next;
1885 if (strbuf_addf(&buf, ":%s", pa->type) < 0)
1888 ret = strbuf_detach(&buf, NULL);
1890 strbuf_release(&buf);
1894 /* Compose only probe point (not argument) */
1895 char *synthesize_perf_probe_point(struct perf_probe_point *pp)
1898 char *tmp, *ret = NULL;
1901 if (strbuf_init(&buf, 64) < 0)
1905 if (strbuf_addstr(&buf, pp->function) < 0)
1908 err = strbuf_addf(&buf, "+%lu", pp->offset);
1910 err = strbuf_addf(&buf, ":%d", pp->line);
1911 else if (pp->retprobe)
1912 err = strbuf_addstr(&buf, "%return");
1920 tmp = strchr(pp->file + len - 30, '/');
1921 tmp = tmp ? tmp + 1 : pp->file + len - 30;
1923 err = strbuf_addf(&buf, "@%s", tmp);
1924 if (!err && !pp->function && pp->line)
1925 err = strbuf_addf(&buf, ":%d", pp->line);
1928 ret = strbuf_detach(&buf, NULL);
1930 strbuf_release(&buf);
1934 char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1937 char *tmp, *ret = NULL;
1940 if (strbuf_init(&buf, 64))
1943 if (strbuf_addf(&buf, "%s:%s=", pev->group ?: PERFPROBE_GROUP,
1947 tmp = synthesize_perf_probe_point(&pev->point);
1948 if (!tmp || strbuf_addstr(&buf, tmp) < 0)
1952 for (i = 0; i < pev->nargs; i++) {
1953 tmp = synthesize_perf_probe_arg(pev->args + i);
1954 if (!tmp || strbuf_addf(&buf, " %s", tmp) < 0)
1959 ret = strbuf_detach(&buf, NULL);
1961 strbuf_release(&buf);
1965 static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
1966 struct strbuf *buf, int depth)
1970 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
1975 err = strbuf_addf(buf, "%+ld(", ref->offset);
1976 return (err < 0) ? err : depth;
1979 static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
1982 struct probe_trace_arg_ref *ref = arg->ref;
1985 /* Argument name or separator */
1987 err = strbuf_addf(buf, " %s=", arg->name);
1989 err = strbuf_addch(buf, ' ');
1993 /* Special case: @XXX */
1994 if (arg->value[0] == '@' && arg->ref)
1997 /* Dereferencing arguments */
1999 depth = __synthesize_probe_trace_arg_ref(ref, buf, 1);
2004 /* Print argument value */
2005 if (arg->value[0] == '@' && arg->ref)
2006 err = strbuf_addf(buf, "%s%+ld", arg->value, arg->ref->offset);
2008 err = strbuf_addstr(buf, arg->value);
2011 while (!err && depth--)
2012 err = strbuf_addch(buf, ')');
2014 /* Print argument type */
2015 if (!err && arg->type)
2016 err = strbuf_addf(buf, ":%s", arg->type);
2022 synthesize_uprobe_trace_def(struct probe_trace_event *tev, struct strbuf *buf)
2024 struct probe_trace_point *tp = &tev->point;
2027 err = strbuf_addf(buf, "%s:0x%lx", tp->module, tp->address);
2029 if (err >= 0 && tp->ref_ctr_offset) {
2030 if (!uprobe_ref_ctr_is_supported())
2032 err = strbuf_addf(buf, "(0x%lx)", tp->ref_ctr_offset);
2034 return err >= 0 ? 0 : -1;
2037 char *synthesize_probe_trace_command(struct probe_trace_event *tev)
2039 struct probe_trace_point *tp = &tev->point;
2044 /* Uprobes must have tp->module */
2045 if (tev->uprobes && !tp->module)
2048 if (strbuf_init(&buf, 32) < 0)
2051 if (strbuf_addf(&buf, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
2052 tev->group, tev->event) < 0)
2055 * If tp->address == 0, then this point must be a
2056 * absolute address uprobe.
2057 * try_to_find_absolute_address() should have made
2058 * tp->symbol to "0x0".
2060 if (tev->uprobes && !tp->address) {
2061 if (!tp->symbol || strcmp(tp->symbol, "0x0"))
2065 /* Use the tp->address for uprobes */
2067 err = synthesize_uprobe_trace_def(tev, &buf);
2068 } else if (!strncmp(tp->symbol, "0x", 2)) {
2069 /* Absolute address. See try_to_find_absolute_address() */
2070 err = strbuf_addf(&buf, "%s%s0x%lx", tp->module ?: "",
2071 tp->module ? ":" : "", tp->address);
2073 err = strbuf_addf(&buf, "%s%s%s+%lu", tp->module ?: "",
2074 tp->module ? ":" : "", tp->symbol, tp->offset);
2080 for (i = 0; i < tev->nargs; i++)
2081 if (synthesize_probe_trace_arg(&tev->args[i], &buf) < 0)
2084 ret = strbuf_detach(&buf, NULL);
2086 strbuf_release(&buf);
2090 static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
2091 struct perf_probe_point *pp,
2094 struct symbol *sym = NULL;
2095 struct map *map = NULL;
2096 u64 addr = tp->address;
2100 map = dso__new_map(tp->module);
2103 sym = map__find_symbol(map, addr);
2105 if (tp->symbol && !addr) {
2106 if (kernel_get_symbol_address_by_name(tp->symbol,
2107 &addr, true, false) < 0)
2112 sym = machine__find_kernel_symbol(host_machine, addr, &map);
2119 pp->retprobe = tp->retprobe;
2120 pp->offset = addr - map->unmap_ip(map, sym->start);
2121 pp->function = strdup(sym->name);
2122 ret = pp->function ? 0 : -ENOMEM;
2125 if (map && !is_kprobe) {
2132 static int convert_to_perf_probe_point(struct probe_trace_point *tp,
2133 struct perf_probe_point *pp,
2139 ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe);
2142 ret = find_perf_probe_point_from_map(tp, pp, is_kprobe);
2146 pr_debug("Failed to find probe point from both of dwarf and map.\n");
2149 pp->function = strdup(tp->symbol);
2150 pp->offset = tp->offset;
2152 ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address);
2155 pp->function = strdup(buf);
2158 if (pp->function == NULL)
2161 pp->retprobe = tp->retprobe;
2166 static int convert_to_perf_probe_event(struct probe_trace_event *tev,
2167 struct perf_probe_event *pev, bool is_kprobe)
2169 struct strbuf buf = STRBUF_INIT;
2172 /* Convert event/group name */
2173 pev->event = strdup(tev->event);
2174 pev->group = strdup(tev->group);
2175 if (pev->event == NULL || pev->group == NULL)
2178 /* Convert trace_point to probe_point */
2179 ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
2183 /* Convert trace_arg to probe_arg */
2184 pev->nargs = tev->nargs;
2185 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
2186 if (pev->args == NULL)
2188 for (i = 0; i < tev->nargs && ret >= 0; i++) {
2189 if (tev->args[i].name)
2190 pev->args[i].name = strdup(tev->args[i].name);
2192 if ((ret = strbuf_init(&buf, 32)) < 0)
2194 ret = synthesize_probe_trace_arg(&tev->args[i], &buf);
2195 pev->args[i].name = strbuf_detach(&buf, NULL);
2197 if (pev->args[i].name == NULL && ret >= 0)
2202 clear_perf_probe_event(pev);
2207 void clear_perf_probe_event(struct perf_probe_event *pev)
2209 struct perf_probe_arg_field *field, *next;
2215 clear_perf_probe_point(&pev->point);
2217 for (i = 0; i < pev->nargs; i++) {
2218 free(pev->args[i].name);
2219 free(pev->args[i].var);
2220 free(pev->args[i].type);
2221 field = pev->args[i].field;
2224 zfree(&field->name);
2230 memset(pev, 0, sizeof(*pev));
2233 #define strdup_or_goto(str, label) \
2234 ({ char *__p = NULL; if (str && !(__p = strdup(str))) goto label; __p; })
2236 static int perf_probe_point__copy(struct perf_probe_point *dst,
2237 struct perf_probe_point *src)
2239 dst->file = strdup_or_goto(src->file, out_err);
2240 dst->function = strdup_or_goto(src->function, out_err);
2241 dst->lazy_line = strdup_or_goto(src->lazy_line, out_err);
2242 dst->line = src->line;
2243 dst->retprobe = src->retprobe;
2244 dst->offset = src->offset;
2248 clear_perf_probe_point(dst);
2252 static int perf_probe_arg__copy(struct perf_probe_arg *dst,
2253 struct perf_probe_arg *src)
2255 struct perf_probe_arg_field *field, **ppfield;
2257 dst->name = strdup_or_goto(src->name, out_err);
2258 dst->var = strdup_or_goto(src->var, out_err);
2259 dst->type = strdup_or_goto(src->type, out_err);
2262 ppfield = &(dst->field);
2264 *ppfield = zalloc(sizeof(*field));
2267 (*ppfield)->name = strdup_or_goto(field->name, out_err);
2268 (*ppfield)->index = field->index;
2269 (*ppfield)->ref = field->ref;
2270 field = field->next;
2271 ppfield = &((*ppfield)->next);
2278 int perf_probe_event__copy(struct perf_probe_event *dst,
2279 struct perf_probe_event *src)
2283 dst->event = strdup_or_goto(src->event, out_err);
2284 dst->group = strdup_or_goto(src->group, out_err);
2285 dst->target = strdup_or_goto(src->target, out_err);
2286 dst->uprobes = src->uprobes;
2288 if (perf_probe_point__copy(&dst->point, &src->point) < 0)
2291 dst->args = zalloc(sizeof(struct perf_probe_arg) * src->nargs);
2294 dst->nargs = src->nargs;
2296 for (i = 0; i < src->nargs; i++)
2297 if (perf_probe_arg__copy(&dst->args[i], &src->args[i]) < 0)
2302 clear_perf_probe_event(dst);
2306 void clear_probe_trace_event(struct probe_trace_event *tev)
2308 struct probe_trace_arg_ref *ref, *next;
2313 free(tev->point.symbol);
2314 free(tev->point.realname);
2315 free(tev->point.module);
2316 for (i = 0; i < tev->nargs; i++) {
2317 free(tev->args[i].name);
2318 free(tev->args[i].value);
2319 free(tev->args[i].type);
2320 ref = tev->args[i].ref;
2328 memset(tev, 0, sizeof(*tev));
2331 struct kprobe_blacklist_node {
2332 struct list_head list;
2333 unsigned long start;
2338 static void kprobe_blacklist__delete(struct list_head *blacklist)
2340 struct kprobe_blacklist_node *node;
2342 while (!list_empty(blacklist)) {
2343 node = list_first_entry(blacklist,
2344 struct kprobe_blacklist_node, list);
2345 list_del(&node->list);
2351 static int kprobe_blacklist__load(struct list_head *blacklist)
2353 struct kprobe_blacklist_node *node;
2354 const char *__debugfs = debugfs__mountpoint();
2355 char buf[PATH_MAX], *p;
2359 if (__debugfs == NULL)
2362 ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs);
2366 fp = fopen(buf, "r");
2371 while (fgets(buf, PATH_MAX, fp)) {
2372 node = zalloc(sizeof(*node));
2377 INIT_LIST_HEAD(&node->list);
2378 list_add_tail(&node->list, blacklist);
2379 if (sscanf(buf, "0x%lx-0x%lx", &node->start, &node->end) != 2) {
2383 p = strchr(buf, '\t');
2386 if (p[strlen(p) - 1] == '\n')
2387 p[strlen(p) - 1] = '\0';
2389 p = (char *)"unknown";
2390 node->symbol = strdup(p);
2391 if (!node->symbol) {
2395 pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
2396 node->start, node->end, node->symbol);
2400 kprobe_blacklist__delete(blacklist);
2406 static struct kprobe_blacklist_node *
2407 kprobe_blacklist__find_by_address(struct list_head *blacklist,
2408 unsigned long address)
2410 struct kprobe_blacklist_node *node;
2412 list_for_each_entry(node, blacklist, list) {
2413 if (node->start <= address && address < node->end)
2420 static LIST_HEAD(kprobe_blacklist);
2422 static void kprobe_blacklist__init(void)
2424 if (!list_empty(&kprobe_blacklist))
2427 if (kprobe_blacklist__load(&kprobe_blacklist) < 0)
2428 pr_debug("No kprobe blacklist support, ignored\n");
2431 static void kprobe_blacklist__release(void)
2433 kprobe_blacklist__delete(&kprobe_blacklist);
2436 static bool kprobe_blacklist__listed(unsigned long address)
2438 return !!kprobe_blacklist__find_by_address(&kprobe_blacklist, address);
2441 static int perf_probe_event__sprintf(const char *group, const char *event,
2442 struct perf_probe_event *pev,
2444 struct strbuf *result)
2449 if (asprintf(&buf, "%s:%s", group, event) < 0)
2451 ret = strbuf_addf(result, " %-20s (on ", buf);
2456 /* Synthesize only event probe point */
2457 buf = synthesize_perf_probe_point(&pev->point);
2460 ret = strbuf_addstr(result, buf);
2464 ret = strbuf_addf(result, " in %s", module);
2466 if (!ret && pev->nargs > 0) {
2467 ret = strbuf_add(result, " with", 5);
2468 for (i = 0; !ret && i < pev->nargs; i++) {
2469 buf = synthesize_perf_probe_arg(&pev->args[i]);
2472 ret = strbuf_addf(result, " %s", buf);
2477 ret = strbuf_addch(result, ')');
2483 int show_perf_probe_event(const char *group, const char *event,
2484 struct perf_probe_event *pev,
2485 const char *module, bool use_stdout)
2487 struct strbuf buf = STRBUF_INIT;
2490 ret = perf_probe_event__sprintf(group, event, pev, module, &buf);
2493 printf("%s\n", buf.buf);
2495 pr_info("%s\n", buf.buf);
2497 strbuf_release(&buf);
2502 static bool filter_probe_trace_event(struct probe_trace_event *tev,
2503 struct strfilter *filter)
2507 /* At first, check the event name itself */
2508 if (strfilter__compare(filter, tev->event))
2511 /* Next, check the combination of name and group */
2512 if (e_snprintf(tmp, 128, "%s:%s", tev->group, tev->event) < 0)
2514 return strfilter__compare(filter, tmp);
2517 static int __show_perf_probe_events(int fd, bool is_kprobe,
2518 struct strfilter *filter)
2521 struct probe_trace_event tev;
2522 struct perf_probe_event pev;
2523 struct strlist *rawlist;
2524 struct str_node *ent;
2526 memset(&tev, 0, sizeof(tev));
2527 memset(&pev, 0, sizeof(pev));
2529 rawlist = probe_file__get_rawlist(fd);
2533 strlist__for_each_entry(ent, rawlist) {
2534 ret = parse_probe_trace_command(ent->s, &tev);
2536 if (!filter_probe_trace_event(&tev, filter))
2538 ret = convert_to_perf_probe_event(&tev, &pev,
2542 ret = show_perf_probe_event(pev.group, pev.event,
2543 &pev, tev.point.module,
2547 clear_perf_probe_event(&pev);
2548 clear_probe_trace_event(&tev);
2552 strlist__delete(rawlist);
2553 /* Cleanup cached debuginfo if needed */
2554 debuginfo_cache__exit();
2559 /* List up current perf-probe events */
2560 int show_perf_probe_events(struct strfilter *filter)
2562 int kp_fd, up_fd, ret;
2566 if (probe_conf.cache)
2567 return probe_cache__show_all_caches(filter);
2569 ret = init_probe_symbol_maps(false);
2573 ret = probe_file__open_both(&kp_fd, &up_fd, 0);
2578 ret = __show_perf_probe_events(kp_fd, true, filter);
2579 if (up_fd >= 0 && ret >= 0)
2580 ret = __show_perf_probe_events(up_fd, false, filter);
2585 exit_probe_symbol_maps();
2590 static int get_new_event_name(char *buf, size_t len, const char *base,
2591 struct strlist *namelist, bool ret_event,
2599 nbase = strdup(base);
2603 /* Cut off the dot suffixes (e.g. .const, .isra) and version suffixes */
2604 p = strpbrk(nbase, ".@");
2605 if (p && p != nbase)
2608 /* Try no suffix number */
2609 ret = e_snprintf(buf, len, "%s%s", nbase, ret_event ? "__return" : "");
2611 pr_debug("snprintf() failed: %d\n", ret);
2614 if (!strlist__has_entry(namelist, buf))
2617 if (!allow_suffix) {
2618 pr_warning("Error: event \"%s\" already exists.\n"
2619 " Hint: Remove existing event by 'perf probe -d'\n"
2620 " or force duplicates by 'perf probe -f'\n"
2621 " or set 'force=yes' in BPF source.\n",
2627 /* Try to add suffix */
2628 for (i = 1; i < MAX_EVENT_INDEX; i++) {
2629 ret = e_snprintf(buf, len, "%s_%d", nbase, i);
2631 pr_debug("snprintf() failed: %d\n", ret);
2634 if (!strlist__has_entry(namelist, buf))
2637 if (i == MAX_EVENT_INDEX) {
2638 pr_warning("Too many events are on the same function.\n");
2645 /* Final validation */
2646 if (ret >= 0 && !is_c_func_name(buf)) {
2647 pr_warning("Internal error: \"%s\" is an invalid event name.\n",
2655 /* Warn if the current kernel's uprobe implementation is old */
2656 static void warn_uprobe_event_compat(struct probe_trace_event *tev)
2659 char *buf = synthesize_probe_trace_command(tev);
2660 struct probe_trace_point *tp = &tev->point;
2662 if (tp->ref_ctr_offset && !uprobe_ref_ctr_is_supported()) {
2663 pr_warning("A semaphore is associated with %s:%s and "
2664 "seems your kernel doesn't support it.\n",
2665 tev->group, tev->event);
2668 /* Old uprobe event doesn't support memory dereference */
2669 if (!tev->uprobes || tev->nargs == 0 || !buf)
2672 for (i = 0; i < tev->nargs; i++)
2673 if (strglobmatch(tev->args[i].value, "[$@+-]*")) {
2674 pr_warning("Please upgrade your kernel to at least "
2675 "3.14 to have access to feature %s\n",
2676 tev->args[i].value);
2683 /* Set new name from original perf_probe_event and namelist */
2684 static int probe_trace_event__set_name(struct probe_trace_event *tev,
2685 struct perf_probe_event *pev,
2686 struct strlist *namelist,
2689 const char *event, *group;
2693 /* If probe_event or trace_event already have the name, reuse it */
2694 if (pev->event && !pev->sdt)
2696 else if (tev->event)
2699 /* Or generate new one from probe point */
2700 if (pev->point.function &&
2701 (strncmp(pev->point.function, "0x", 2) != 0) &&
2702 !strisglob(pev->point.function))
2703 event = pev->point.function;
2705 event = tev->point.realname;
2707 if (pev->group && !pev->sdt)
2709 else if (tev->group)
2712 group = PERFPROBE_GROUP;
2714 /* Get an unused new event name */
2715 ret = get_new_event_name(buf, 64, event, namelist,
2716 tev->point.retprobe, allow_suffix);
2722 tev->event = strdup(event);
2723 tev->group = strdup(group);
2724 if (tev->event == NULL || tev->group == NULL)
2727 /* Add added event name to namelist */
2728 strlist__add(namelist, event);
2732 static int __open_probe_file_and_namelist(bool uprobe,
2733 struct strlist **namelist)
2737 fd = probe_file__open(PF_FL_RW | (uprobe ? PF_FL_UPROBE : 0));
2741 /* Get current event names */
2742 *namelist = probe_file__get_namelist(fd);
2744 pr_debug("Failed to get current event list.\n");
2751 static int __add_probe_trace_events(struct perf_probe_event *pev,
2752 struct probe_trace_event *tevs,
2753 int ntevs, bool allow_suffix)
2755 int i, fd[2] = {-1, -1}, up, ret;
2756 struct probe_trace_event *tev = NULL;
2757 struct probe_cache *cache = NULL;
2758 struct strlist *namelist[2] = {NULL, NULL};
2759 struct nscookie nsc;
2761 up = pev->uprobes ? 1 : 0;
2762 fd[up] = __open_probe_file_and_namelist(up, &namelist[up]);
2767 for (i = 0; i < ntevs; i++) {
2769 up = tev->uprobes ? 1 : 0;
2770 if (fd[up] == -1) { /* Open the kprobe/uprobe_events */
2771 fd[up] = __open_probe_file_and_namelist(up,
2776 /* Skip if the symbol is out of .text or blacklisted */
2777 if (!tev->point.symbol && !pev->uprobes)
2780 /* Set new name for tev (and update namelist) */
2781 ret = probe_trace_event__set_name(tev, pev, namelist[up],
2786 nsinfo__mountns_enter(pev->nsi, &nsc);
2787 ret = probe_file__add_event(fd[up], tev);
2788 nsinfo__mountns_exit(&nsc);
2793 * Probes after the first probe which comes from same
2794 * user input are always allowed to add suffix, because
2795 * there might be several addresses corresponding to
2798 allow_suffix = true;
2800 if (ret == -EINVAL && pev->uprobes)
2801 warn_uprobe_event_compat(tev);
2802 if (ret == 0 && probe_conf.cache) {
2803 cache = probe_cache__new(pev->target, pev->nsi);
2805 probe_cache__add_entry(cache, pev, tevs, ntevs) < 0 ||
2806 probe_cache__commit(cache) < 0)
2807 pr_warning("Failed to add event to probe cache\n");
2808 probe_cache__delete(cache);
2812 for (up = 0; up < 2; up++) {
2813 strlist__delete(namelist[up]);
2820 static int find_probe_functions(struct map *map, char *name,
2821 struct symbol **syms)
2825 struct rb_node *tmp;
2826 const char *norm, *ver;
2828 bool cut_version = true;
2830 if (map__load(map) < 0)
2833 /* If user gives a version, don't cut off the version from symbols */
2834 if (strchr(name, '@'))
2835 cut_version = false;
2837 map__for_each_symbol(map, sym, tmp) {
2838 norm = arch__normalize_symbol_name(sym->name);
2843 /* We don't care about default symbol or not */
2844 ver = strchr(norm, '@');
2846 buf = strndup(norm, ver - norm);
2853 if (strglobmatch(norm, name)) {
2855 if (syms && found < probe_conf.max_probes)
2856 syms[found - 1] = sym;
2865 void __weak arch__fix_tev_from_maps(struct perf_probe_event *pev __maybe_unused,
2866 struct probe_trace_event *tev __maybe_unused,
2867 struct map *map __maybe_unused,
2868 struct symbol *sym __maybe_unused) { }
2871 * Find probe function addresses from map.
2872 * Return an error or the number of found probe_trace_event
2874 static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
2875 struct probe_trace_event **tevs)
2877 struct map *map = NULL;
2878 struct ref_reloc_sym *reloc_sym = NULL;
2880 struct symbol **syms = NULL;
2881 struct probe_trace_event *tev;
2882 struct perf_probe_point *pp = &pev->point;
2883 struct probe_trace_point *tp;
2884 int num_matched_functions;
2885 int ret, i, j, skipped = 0;
2888 map = get_target_map(pev->target, pev->nsi, pev->uprobes);
2894 syms = malloc(sizeof(struct symbol *) * probe_conf.max_probes);
2901 * Load matched symbols: Since the different local symbols may have
2902 * same name but different addresses, this lists all the symbols.
2904 num_matched_functions = find_probe_functions(map, pp->function, syms);
2905 if (num_matched_functions <= 0) {
2906 pr_err("Failed to find symbol %s in %s\n", pp->function,
2907 pev->target ? : "kernel");
2910 } else if (num_matched_functions > probe_conf.max_probes) {
2911 pr_err("Too many functions matched in %s\n",
2912 pev->target ? : "kernel");
2917 /* Note that the symbols in the kmodule are not relocated */
2918 if (!pev->uprobes && !pev->target &&
2919 (!pp->retprobe || kretprobe_offset_is_supported())) {
2920 reloc_sym = kernel_get_ref_reloc_sym();
2922 pr_warning("Relocated base symbol is not found!\n");
2928 /* Setup result trace-probe-events */
2929 *tevs = zalloc(sizeof(*tev) * num_matched_functions);
2937 for (j = 0; j < num_matched_functions; j++) {
2940 tev = (*tevs) + ret;
2942 if (ret == num_matched_functions) {
2943 pr_warning("Too many symbols are listed. Skip it.\n");
2948 if (pp->offset > sym->end - sym->start) {
2949 pr_warning("Offset %ld is bigger than the size of %s\n",
2950 pp->offset, sym->name);
2954 /* Add one probe point */
2955 tp->address = map->unmap_ip(map, sym->start) + pp->offset;
2957 /* Check the kprobe (not in module) is within .text */
2958 if (!pev->uprobes && !pev->target &&
2959 kprobe_warn_out_range(sym->name, tp->address)) {
2960 tp->symbol = NULL; /* Skip it */
2962 } else if (reloc_sym) {
2963 tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
2964 tp->offset = tp->address - reloc_sym->addr;
2966 tp->symbol = strdup_or_goto(sym->name, nomem_out);
2967 tp->offset = pp->offset;
2969 tp->realname = strdup_or_goto(sym->name, nomem_out);
2971 tp->retprobe = pp->retprobe;
2974 tev->point.module = strdup_or_goto(pev->target,
2977 mod_name = find_module_name(pev->target);
2979 strdup(mod_name ? mod_name : pev->target);
2981 if (!tev->point.module)
2985 tev->uprobes = pev->uprobes;
2986 tev->nargs = pev->nargs;
2988 tev->args = zalloc(sizeof(struct probe_trace_arg) *
2990 if (tev->args == NULL)
2993 for (i = 0; i < tev->nargs; i++) {
2994 if (pev->args[i].name)
2996 strdup_or_goto(pev->args[i].name,
2999 tev->args[i].value = strdup_or_goto(pev->args[i].var,
3001 if (pev->args[i].type)
3003 strdup_or_goto(pev->args[i].type,
3006 arch__fix_tev_from_maps(pev, tev, map, sym);
3008 if (ret == skipped) {
3021 clear_probe_trace_events(*tevs, num_matched_functions);
3026 static int try_to_find_absolute_address(struct perf_probe_event *pev,
3027 struct probe_trace_event **tevs)
3029 struct perf_probe_point *pp = &pev->point;
3030 struct probe_trace_event *tev;
3031 struct probe_trace_point *tp;
3034 if (!(pev->point.function && !strncmp(pev->point.function, "0x", 2)))
3036 if (perf_probe_event_need_dwarf(pev))
3040 * This is 'perf probe /lib/libc.so 0xabcd'. Try to probe at
3043 * Only one tev can be generated by this.
3045 *tevs = zalloc(sizeof(*tev));
3053 * Don't use tp->offset, use address directly, because
3054 * in synthesize_probe_trace_command() address cannot be
3057 tp->address = pev->point.abs_address;
3058 tp->retprobe = pp->retprobe;
3059 tev->uprobes = pev->uprobes;
3063 * Give it a '0x' leading symbol name.
3064 * In __add_probe_trace_events, a NULL symbol is interpreted as
3067 if (asprintf(&tp->symbol, "0x%lx", tp->address) < 0)
3070 /* For kprobe, check range */
3071 if ((!tev->uprobes) &&
3072 (kprobe_warn_out_range(tev->point.symbol,
3073 tev->point.address))) {
3078 if (asprintf(&tp->realname, "abs_%lx", tp->address) < 0)
3082 tp->module = strdup(pev->target);
3088 tev->group = strdup(pev->group);
3094 tev->event = strdup(pev->event);
3099 tev->nargs = pev->nargs;
3100 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
3104 for (i = 0; i < tev->nargs; i++)
3105 copy_to_probe_trace_arg(&tev->args[i], &pev->args[i]);
3110 clear_probe_trace_events(*tevs, 1);
3115 /* Concatinate two arrays */
3116 static void *memcat(void *a, size_t sz_a, void *b, size_t sz_b)
3120 ret = malloc(sz_a + sz_b);
3122 memcpy(ret, a, sz_a);
3123 memcpy(ret + sz_a, b, sz_b);
3129 concat_probe_trace_events(struct probe_trace_event **tevs, int *ntevs,
3130 struct probe_trace_event **tevs2, int ntevs2)
3132 struct probe_trace_event *new_tevs;
3142 if (*ntevs + ntevs2 > probe_conf.max_probes)
3145 /* Concatinate the array of probe_trace_event */
3146 new_tevs = memcat(*tevs, (*ntevs) * sizeof(**tevs),
3147 *tevs2, ntevs2 * sizeof(**tevs2));
3157 clear_probe_trace_events(*tevs2, ntevs2);
3164 * Try to find probe_trace_event from given probe caches. Return the number
3165 * of cached events found, if an error occurs return the error.
3167 static int find_cached_events(struct perf_probe_event *pev,
3168 struct probe_trace_event **tevs,
3171 struct probe_cache *cache;
3172 struct probe_cache_entry *entry;
3173 struct probe_trace_event *tmp_tevs = NULL;
3177 cache = probe_cache__new(target, pev->nsi);
3178 /* Return 0 ("not found") if the target has no probe cache. */
3182 for_each_probe_cache_entry(entry, cache) {
3183 /* Skip the cache entry which has no name */
3184 if (!entry->pev.event || !entry->pev.group)
3186 if ((!pev->group || strglobmatch(entry->pev.group, pev->group)) &&
3187 strglobmatch(entry->pev.event, pev->event)) {
3188 ret = probe_cache_entry__get_event(entry, &tmp_tevs);
3190 ret = concat_probe_trace_events(tevs, &ntevs,
3196 probe_cache__delete(cache);
3198 clear_probe_trace_events(*tevs, ntevs);
3202 if (ntevs > 0 && target && target[0] == '/')
3203 pev->uprobes = true;
3209 /* Try to find probe_trace_event from all probe caches */
3210 static int find_cached_events_all(struct perf_probe_event *pev,
3211 struct probe_trace_event **tevs)
3213 struct probe_trace_event *tmp_tevs = NULL;
3214 struct strlist *bidlist;
3215 struct str_node *nd;
3220 /* Get the buildid list of all valid caches */
3221 bidlist = build_id_cache__list_all(true);
3224 pr_debug("Failed to get buildids: %d\n", ret);
3229 strlist__for_each_entry(nd, bidlist) {
3230 pathname = build_id_cache__origname(nd->s);
3231 ret = find_cached_events(pev, &tmp_tevs, pathname);
3232 /* In the case of cnt == 0, we just skip it */
3234 ret = concat_probe_trace_events(tevs, &ntevs,
3240 strlist__delete(bidlist);
3243 clear_probe_trace_events(*tevs, ntevs);
3251 static int find_probe_trace_events_from_cache(struct perf_probe_event *pev,
3252 struct probe_trace_event **tevs)
3254 struct probe_cache *cache;
3255 struct probe_cache_entry *entry;
3256 struct probe_trace_event *tev;
3257 struct str_node *node;
3261 /* For SDT/cached events, we use special search functions */
3263 return find_cached_events_all(pev, tevs);
3265 return find_cached_events(pev, tevs, pev->target);
3267 cache = probe_cache__new(pev->target, pev->nsi);
3271 entry = probe_cache__find(cache, pev);
3273 /* SDT must be in the cache */
3274 ret = pev->sdt ? -ENOENT : 0;
3278 ret = strlist__nr_entries(entry->tevlist);
3279 if (ret > probe_conf.max_probes) {
3280 pr_debug("Too many entries matched in the cache of %s\n",
3281 pev->target ? : "kernel");
3286 *tevs = zalloc(ret * sizeof(*tev));
3293 strlist__for_each_entry(node, entry->tevlist) {
3294 tev = &(*tevs)[i++];
3295 ret = parse_probe_trace_command(node->s, tev);
3298 /* Set the uprobes attribute as same as original */
3299 tev->uprobes = pev->uprobes;
3304 probe_cache__delete(cache);
3308 static int convert_to_probe_trace_events(struct perf_probe_event *pev,
3309 struct probe_trace_event **tevs)
3313 if (!pev->group && !pev->sdt) {
3314 /* Set group name if not given */
3315 if (!pev->uprobes) {
3316 pev->group = strdup(PERFPROBE_GROUP);
3317 ret = pev->group ? 0 : -ENOMEM;
3319 ret = convert_exec_to_group(pev->target, &pev->group);
3321 pr_warning("Failed to make a group name.\n");
3326 ret = try_to_find_absolute_address(pev, tevs);
3330 /* At first, we need to lookup cache entry */
3331 ret = find_probe_trace_events_from_cache(pev, tevs);
3332 if (ret > 0 || pev->sdt) /* SDT can be found only in the cache */
3333 return ret == 0 ? -ENOENT : ret; /* Found in probe cache */
3335 /* Convert perf_probe_event with debuginfo */
3336 ret = try_to_find_probe_trace_events(pev, tevs);
3338 return ret; /* Found in debuginfo or got an error */
3340 return find_probe_trace_events_from_map(pev, tevs);
3343 int convert_perf_probe_events(struct perf_probe_event *pevs, int npevs)
3347 /* Loop 1: convert all events */
3348 for (i = 0; i < npevs; i++) {
3349 /* Init kprobe blacklist if needed */
3350 if (!pevs[i].uprobes)
3351 kprobe_blacklist__init();
3352 /* Convert with or without debuginfo */
3353 ret = convert_to_probe_trace_events(&pevs[i], &pevs[i].tevs);
3356 pevs[i].ntevs = ret;
3358 /* This just release blacklist only if allocated */
3359 kprobe_blacklist__release();
3364 static int show_probe_trace_event(struct probe_trace_event *tev)
3366 char *buf = synthesize_probe_trace_command(tev);
3369 pr_debug("Failed to synthesize probe trace event.\n");
3373 /* Showing definition always go stdout */
3374 printf("%s\n", buf);
3380 int show_probe_trace_events(struct perf_probe_event *pevs, int npevs)
3382 struct strlist *namelist = strlist__new(NULL, NULL);
3383 struct probe_trace_event *tev;
3384 struct perf_probe_event *pev;
3390 for (j = 0; j < npevs && !ret; j++) {
3392 for (i = 0; i < pev->ntevs && !ret; i++) {
3393 tev = &pev->tevs[i];
3394 /* Skip if the symbol is out of .text or blacklisted */
3395 if (!tev->point.symbol && !pev->uprobes)
3398 /* Set new name for tev (and update namelist) */
3399 ret = probe_trace_event__set_name(tev, pev,
3402 ret = show_probe_trace_event(tev);
3405 strlist__delete(namelist);
3410 int apply_perf_probe_events(struct perf_probe_event *pevs, int npevs)
3414 /* Loop 2: add all events */
3415 for (i = 0; i < npevs; i++) {
3416 ret = __add_probe_trace_events(&pevs[i], pevs[i].tevs,
3418 probe_conf.force_add);
3425 void cleanup_perf_probe_events(struct perf_probe_event *pevs, int npevs)
3428 struct perf_probe_event *pev;
3430 /* Loop 3: cleanup and free trace events */
3431 for (i = 0; i < npevs; i++) {
3433 for (j = 0; j < pevs[i].ntevs; j++)
3434 clear_probe_trace_event(&pevs[i].tevs[j]);
3435 zfree(&pevs[i].tevs);
3437 nsinfo__zput(pev->nsi);
3438 clear_perf_probe_event(&pevs[i]);
3442 int add_perf_probe_events(struct perf_probe_event *pevs, int npevs)
3446 ret = init_probe_symbol_maps(pevs->uprobes);
3450 ret = convert_perf_probe_events(pevs, npevs);
3452 ret = apply_perf_probe_events(pevs, npevs);
3454 cleanup_perf_probe_events(pevs, npevs);
3456 exit_probe_symbol_maps();
3460 int del_perf_probe_events(struct strfilter *filter)
3462 int ret, ret2, ufd = -1, kfd = -1;
3463 char *str = strfilter__string(filter);
3468 /* Get current event names */
3469 ret = probe_file__open_both(&kfd, &ufd, PF_FL_RW);
3473 ret = probe_file__del_events(kfd, filter);
3474 if (ret < 0 && ret != -ENOENT)
3477 ret2 = probe_file__del_events(ufd, filter);
3478 if (ret2 < 0 && ret2 != -ENOENT) {
3495 int show_available_funcs(const char *target, struct nsinfo *nsi,
3496 struct strfilter *_filter, bool user)
3502 ret = init_probe_symbol_maps(user);
3506 /* Get a symbol map */
3507 map = get_target_map(target, nsi, user);
3509 pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
3513 ret = map__load(map);
3516 char *str = strfilter__string(_filter);
3517 pr_err("Failed to find symbols matched to \"%s\"\n",
3521 pr_err("Failed to load symbols in %s\n",
3522 (target) ? : "kernel");
3525 if (!dso__sorted_by_name(map->dso))
3526 dso__sort_by_name(map->dso);
3528 /* Show all (filtered) symbols */
3531 for (nd = rb_first(&map->dso->symbol_names); nd; nd = rb_next(nd)) {
3532 struct symbol_name_rb_node *pos = rb_entry(nd, struct symbol_name_rb_node, rb_node);
3534 if (strfilter__compare(_filter, pos->sym.name))
3535 printf("%s\n", pos->sym.name);
3539 exit_probe_symbol_maps();
3544 int copy_to_probe_trace_arg(struct probe_trace_arg *tvar,
3545 struct perf_probe_arg *pvar)
3547 tvar->value = strdup(pvar->var);
3548 if (tvar->value == NULL)
3551 tvar->type = strdup(pvar->type);
3552 if (tvar->type == NULL)
3556 tvar->name = strdup(pvar->name);
3557 if (tvar->name == NULL)