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 char *synthesize_perf_probe_point(struct perf_probe_point *pp);
71 static struct machine *host_machine;
73 /* Initialize symbol maps and path of vmlinux/modules */
74 int init_probe_symbol_maps(bool user_only)
78 symbol_conf.sort_by_name = true;
79 symbol_conf.allow_aliases = true;
80 ret = symbol__init(NULL);
82 pr_debug("Failed to init symbol map.\n");
86 if (host_machine || user_only) /* already initialized */
89 if (symbol_conf.vmlinux_name)
90 pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
92 host_machine = machine__new_host();
94 pr_debug("machine__new_host() failed.\n");
100 pr_warning("Failed to init vmlinux path.\n");
104 void exit_probe_symbol_maps(void)
107 machine__delete(host_machine);
113 static struct symbol *__find_kernel_function_by_name(const char *name,
116 return machine__find_kernel_function_by_name(host_machine, name, mapp,
120 static struct symbol *__find_kernel_function(u64 addr, struct map **mapp)
122 return machine__find_kernel_function(host_machine, addr, mapp, NULL);
125 static struct ref_reloc_sym *kernel_get_ref_reloc_sym(struct map **pmap)
127 /* kmap->ref_reloc_sym should be set if host_machine is initialized */
129 struct map *map = machine__kernel_map(host_machine);
131 if (map__load(map, NULL) < 0)
134 kmap = map__kmap(map);
141 return kmap->ref_reloc_sym;
144 static int kernel_get_symbol_address_by_name(const char *name, u64 *addr,
145 bool reloc, bool reladdr)
147 struct ref_reloc_sym *reloc_sym;
151 /* ref_reloc_sym is just a label. Need a special fix*/
152 reloc_sym = kernel_get_ref_reloc_sym(NULL);
153 if (reloc_sym && strcmp(name, reloc_sym->name) == 0)
154 *addr = (reloc) ? reloc_sym->addr : reloc_sym->unrelocated_addr;
156 sym = __find_kernel_function_by_name(name, &map);
159 *addr = map->unmap_ip(map, sym->start) -
160 ((reloc) ? 0 : map->reloc) -
161 ((reladdr) ? map->start : 0);
166 static struct map *kernel_get_module_map(const char *module)
168 struct map_groups *grp = &host_machine->kmaps;
169 struct maps *maps = &grp->maps[MAP__FUNCTION];
172 /* A file path -- this is an offline module */
173 if (module && strchr(module, '/'))
174 return machine__findnew_module_map(host_machine, 0, module);
179 for (pos = maps__first(maps); pos; pos = map__next(pos)) {
180 if (strncmp(pos->dso->short_name + 1, module,
181 pos->dso->short_name_len - 2) == 0) {
188 static struct map *get_target_map(const char *target, bool user)
190 /* Init maps of given executable or kernel */
192 return dso__new_map(target);
194 return kernel_get_module_map(target);
197 static void put_target_map(struct map *map, bool user)
200 /* Only the user map needs to be released */
206 static int convert_exec_to_group(const char *exec, char **result)
208 char *ptr1, *ptr2, *exec_copy;
212 exec_copy = strdup(exec);
216 ptr1 = basename(exec_copy);
222 ptr2 = strpbrk(ptr1, "-._");
225 ret = e_snprintf(buf, 64, "%s_%s", PERFPROBE_GROUP, ptr1);
229 *result = strdup(buf);
230 ret = *result ? 0 : -ENOMEM;
237 static void clear_perf_probe_point(struct perf_probe_point *pp)
244 static void clear_probe_trace_events(struct probe_trace_event *tevs, int ntevs)
248 for (i = 0; i < ntevs; i++)
249 clear_probe_trace_event(tevs + i);
252 static bool kprobe_blacklist__listed(unsigned long address);
253 static bool kprobe_warn_out_range(const char *symbol, unsigned long address)
258 /* Get the address of _etext for checking non-probable text symbol */
259 ret = kernel_get_symbol_address_by_name("_etext", &etext_addr,
262 if (ret == 0 && etext_addr < address)
263 pr_warning("%s is out of .text, skip it.\n", symbol);
264 else if (kprobe_blacklist__listed(address))
265 pr_warning("%s is blacklisted function, skip it.\n", symbol);
272 #ifdef HAVE_DWARF_SUPPORT
274 static int kernel_get_module_dso(const char *module, struct dso **pdso)
278 const char *vmlinux_name;
282 char module_name[128];
284 snprintf(module_name, sizeof(module_name), "[%s]", module);
285 map = map_groups__find_by_name(&host_machine->kmaps, MAP__FUNCTION, module_name);
290 pr_debug("Failed to find module %s.\n", module);
294 map = machine__kernel_map(host_machine);
297 vmlinux_name = symbol_conf.vmlinux_name;
300 ret = dso__load_vmlinux(dso, map, vmlinux_name, false, NULL);
302 ret = dso__load_vmlinux_path(dso, map, NULL);
309 * Some binaries like glibc have special symbols which are on the symbol
310 * table, but not in the debuginfo. If we can find the address of the
311 * symbol from map, we can translate the address back to the probe point.
313 static int find_alternative_probe_point(struct debuginfo *dinfo,
314 struct perf_probe_point *pp,
315 struct perf_probe_point *result,
316 const char *target, bool uprobes)
318 struct map *map = NULL;
323 /* This can work only for function-name based one */
324 if (!pp->function || pp->file)
327 map = get_target_map(target, uprobes);
331 /* Find the address of given function */
332 map__for_each_symbol_by_name(map, pp->function, sym) {
334 address = sym->start;
336 address = map->unmap_ip(map, sym->start);
343 pr_debug("Symbol %s address found : %" PRIx64 "\n",
344 pp->function, address);
346 ret = debuginfo__find_probe_point(dinfo, (unsigned long)address,
349 ret = (!ret) ? -ENOENT : ret;
351 result->offset += pp->offset;
352 result->line += pp->line;
353 result->retprobe = pp->retprobe;
358 put_target_map(map, uprobes);
363 static int get_alternative_probe_event(struct debuginfo *dinfo,
364 struct perf_probe_event *pev,
365 struct perf_probe_point *tmp)
369 memcpy(tmp, &pev->point, sizeof(*tmp));
370 memset(&pev->point, 0, sizeof(pev->point));
371 ret = find_alternative_probe_point(dinfo, tmp, &pev->point,
372 pev->target, pev->uprobes);
374 memcpy(&pev->point, tmp, sizeof(*tmp));
379 static int get_alternative_line_range(struct debuginfo *dinfo,
380 struct line_range *lr,
381 const char *target, bool user)
383 struct perf_probe_point pp = { .function = lr->function,
386 struct perf_probe_point result;
389 memset(&result, 0, sizeof(result));
391 if (lr->end != INT_MAX)
392 len = lr->end - lr->start;
393 ret = find_alternative_probe_point(dinfo, &pp, &result,
396 lr->function = result.function;
397 lr->file = result.file;
398 lr->start = result.line;
399 if (lr->end != INT_MAX)
400 lr->end = lr->start + len;
401 clear_perf_probe_point(&pp);
406 /* Open new debuginfo of given module */
407 static struct debuginfo *open_debuginfo(const char *module, bool silent)
409 const char *path = module;
410 char reason[STRERR_BUFSIZE];
411 struct debuginfo *ret = NULL;
412 struct dso *dso = NULL;
415 if (!module || !strchr(module, '/')) {
416 err = kernel_get_module_dso(module, &dso);
418 if (!dso || dso->load_errno == 0) {
419 if (!strerror_r(-err, reason, STRERR_BUFSIZE))
420 strcpy(reason, "(unknown)");
422 dso__strerror_load(dso, reason, STRERR_BUFSIZE);
424 pr_err("Failed to find the path for %s: %s\n",
425 module ?: "kernel", reason);
428 path = dso->long_name;
430 ret = debuginfo__new(path);
431 if (!ret && !silent) {
432 pr_warning("The %s file has no debug information.\n", path);
433 if (!module || !strtailcmp(path, ".ko"))
434 pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, ");
436 pr_warning("Rebuild with -g, ");
437 pr_warning("or install an appropriate debuginfo package.\n");
442 /* For caching the last debuginfo */
443 static struct debuginfo *debuginfo_cache;
444 static char *debuginfo_cache_path;
446 static struct debuginfo *debuginfo_cache__open(const char *module, bool silent)
448 const char *path = module;
450 /* If the module is NULL, it should be the kernel. */
454 if (debuginfo_cache_path && !strcmp(debuginfo_cache_path, path))
457 /* Copy module path */
458 free(debuginfo_cache_path);
459 debuginfo_cache_path = strdup(path);
460 if (!debuginfo_cache_path) {
461 debuginfo__delete(debuginfo_cache);
462 debuginfo_cache = NULL;
466 debuginfo_cache = open_debuginfo(module, silent);
467 if (!debuginfo_cache)
468 zfree(&debuginfo_cache_path);
470 return debuginfo_cache;
473 static void debuginfo_cache__exit(void)
475 debuginfo__delete(debuginfo_cache);
476 debuginfo_cache = NULL;
477 zfree(&debuginfo_cache_path);
481 static int get_text_start_address(const char *exec, unsigned long *address)
486 int fd, ret = -ENOENT;
488 fd = open(exec, O_RDONLY);
492 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
496 if (gelf_getehdr(elf, &ehdr) == NULL)
499 if (!elf_section_by_name(elf, &ehdr, &shdr, ".text", NULL))
502 *address = shdr.sh_addr - shdr.sh_offset;
510 * Convert trace point to probe point with debuginfo
512 static int find_perf_probe_point_from_dwarf(struct probe_trace_point *tp,
513 struct perf_probe_point *pp,
516 struct debuginfo *dinfo = NULL;
517 unsigned long stext = 0;
518 u64 addr = tp->address;
521 /* convert the address to dwarf address */
527 ret = get_text_start_address(tp->module, &stext);
531 } else if (tp->symbol) {
532 /* If the module is given, this returns relative address */
533 ret = kernel_get_symbol_address_by_name(tp->symbol, &addr,
534 false, !!tp->module);
540 pr_debug("try to find information at %" PRIx64 " in %s\n", addr,
541 tp->module ? : "kernel");
543 dinfo = debuginfo_cache__open(tp->module, verbose == 0);
545 ret = debuginfo__find_probe_point(dinfo,
546 (unsigned long)addr, pp);
551 pp->retprobe = tp->retprobe;
555 pr_debug("Failed to find corresponding probes from debuginfo.\n");
556 return ret ? : -ENOENT;
559 static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs,
560 int ntevs, const char *exec)
563 unsigned long stext = 0;
568 ret = get_text_start_address(exec, &stext);
572 for (i = 0; i < ntevs && ret >= 0; i++) {
573 /* point.address is the addres of point.symbol + point.offset */
574 tevs[i].point.address -= stext;
575 tevs[i].point.module = strdup(exec);
576 if (!tevs[i].point.module) {
580 tevs[i].uprobes = true;
586 static int add_module_to_probe_trace_events(struct probe_trace_event *tevs,
587 int ntevs, const char *module)
595 tmp = strrchr(module, '/');
597 /* This is a module path -- get the module name */
598 module = strdup(tmp + 1);
601 tmp = strchr(module, '.');
604 tmp = (char *)module; /* For free() */
607 for (i = 0; i < ntevs; i++) {
608 tevs[i].point.module = strdup(module);
609 if (!tevs[i].point.module) {
619 /* Post processing the probe events */
620 static int post_process_probe_trace_events(struct probe_trace_event *tevs,
621 int ntevs, const char *module,
624 struct ref_reloc_sym *reloc_sym;
630 return add_exec_to_probe_trace_events(tevs, ntevs, module);
632 /* Note that currently ref_reloc_sym based probe is not for drivers */
634 return add_module_to_probe_trace_events(tevs, ntevs, module);
636 reloc_sym = kernel_get_ref_reloc_sym(&map);
638 pr_warning("Relocated base symbol is not found!\n");
642 for (i = 0; i < ntevs; i++) {
643 if (!tevs[i].point.address || tevs[i].point.retprobe)
646 * If we found a wrong one, mark it by NULL symbol.
647 * Since addresses in debuginfo is same as objdump, we need
648 * to convert it to addresses on memory.
650 if (kprobe_warn_out_range(tevs[i].point.symbol,
651 map__objdump_2mem(map, tevs[i].point.address))) {
655 tmp = strdup(reloc_sym->name);
659 /* If we have no realname, use symbol for it */
660 if (!tevs[i].point.realname)
661 tevs[i].point.realname = tevs[i].point.symbol;
663 free(tevs[i].point.symbol);
664 tevs[i].point.symbol = tmp;
665 tevs[i].point.offset = tevs[i].point.address -
666 reloc_sym->unrelocated_addr;
671 /* Try to find perf_probe_event with debuginfo */
672 static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
673 struct probe_trace_event **tevs)
675 bool need_dwarf = perf_probe_event_need_dwarf(pev);
676 struct perf_probe_point tmp;
677 struct debuginfo *dinfo;
680 dinfo = open_debuginfo(pev->target, !need_dwarf);
684 pr_debug("Could not open debuginfo. Try to use symbols.\n");
688 pr_debug("Try to find probe point from debuginfo.\n");
689 /* Searching trace events corresponding to a probe event */
690 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
692 if (ntevs == 0) { /* Not found, retry with an alternative */
693 ret = get_alternative_probe_event(dinfo, pev, &tmp);
695 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
697 * Write back to the original probe_event for
698 * setting appropriate (user given) event name
700 clear_perf_probe_point(&pev->point);
701 memcpy(&pev->point, &tmp, sizeof(tmp));
705 debuginfo__delete(dinfo);
707 if (ntevs > 0) { /* Succeeded to find trace events */
708 pr_debug("Found %d probe_trace_events.\n", ntevs);
709 ret = post_process_probe_trace_events(*tevs, ntevs,
710 pev->target, pev->uprobes);
711 if (ret < 0 || ret == ntevs) {
712 clear_probe_trace_events(*tevs, ntevs);
716 return ret < 0 ? ret : ntevs;
721 if (ntevs == 0) { /* No error but failed to find probe point. */
722 pr_warning("Probe point '%s' not found.\n",
723 synthesize_perf_probe_point(&pev->point));
726 /* Error path : ntevs < 0 */
727 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
730 pr_warning("Warning: No dwarf info found in the vmlinux - "
731 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
733 pr_debug("Trying to use symbols.\n");
740 #define LINEBUF_SIZE 256
741 #define NR_ADDITIONAL_LINES 2
743 static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
745 char buf[LINEBUF_SIZE], sbuf[STRERR_BUFSIZE];
746 const char *color = show_num ? "" : PERF_COLOR_BLUE;
747 const char *prefix = NULL;
750 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
755 prefix = show_num ? "%7d " : " ";
756 color_fprintf(stdout, color, prefix, l);
758 color_fprintf(stdout, color, "%s", buf);
760 } while (strchr(buf, '\n') == NULL);
765 pr_warning("File read error: %s\n",
766 strerror_r(errno, sbuf, sizeof(sbuf)));
772 static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
774 int rv = __show_one_line(fp, l, skip, show_num);
776 pr_warning("Source file is shorter than expected.\n");
782 #define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
783 #define show_one_line(f,l) _show_one_line(f,l,false,false)
784 #define skip_one_line(f,l) _show_one_line(f,l,true,false)
785 #define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
788 * Show line-range always requires debuginfo to find source file and
791 static int __show_line_range(struct line_range *lr, const char *module,
796 struct debuginfo *dinfo;
800 char sbuf[STRERR_BUFSIZE];
802 /* Search a line range */
803 dinfo = open_debuginfo(module, false);
807 ret = debuginfo__find_line_range(dinfo, lr);
808 if (!ret) { /* Not found, retry with an alternative */
809 ret = get_alternative_line_range(dinfo, lr, module, user);
811 ret = debuginfo__find_line_range(dinfo, lr);
813 debuginfo__delete(dinfo);
814 if (ret == 0 || ret == -ENOENT) {
815 pr_warning("Specified source line is not found.\n");
817 } else if (ret < 0) {
818 pr_warning("Debuginfo analysis failed.\n");
822 /* Convert source file path */
824 ret = get_real_path(tmp, lr->comp_dir, &lr->path);
826 /* Free old path when new path is assigned */
831 pr_warning("Failed to find source file path.\n");
838 fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
839 lr->start - lr->offset);
841 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
843 fp = fopen(lr->path, "r");
845 pr_warning("Failed to open %s: %s\n", lr->path,
846 strerror_r(errno, sbuf, sizeof(sbuf)));
849 /* Skip to starting line number */
850 while (l < lr->start) {
851 ret = skip_one_line(fp, l++);
856 intlist__for_each(ln, lr->line_list) {
857 for (; ln->i > l; l++) {
858 ret = show_one_line(fp, l - lr->offset);
862 ret = show_one_line_with_num(fp, l++ - lr->offset);
867 if (lr->end == INT_MAX)
868 lr->end = l + NR_ADDITIONAL_LINES;
869 while (l <= lr->end) {
870 ret = show_one_line_or_eof(fp, l++ - lr->offset);
879 int show_line_range(struct line_range *lr, const char *module, bool user)
883 ret = init_probe_symbol_maps(user);
886 ret = __show_line_range(lr, module, user);
887 exit_probe_symbol_maps();
892 static int show_available_vars_at(struct debuginfo *dinfo,
893 struct perf_probe_event *pev,
894 struct strfilter *_filter)
898 struct str_node *node;
899 struct variable_list *vls = NULL, *vl;
900 struct perf_probe_point tmp;
903 buf = synthesize_perf_probe_point(&pev->point);
906 pr_debug("Searching variables at %s\n", buf);
908 ret = debuginfo__find_available_vars_at(dinfo, pev, &vls);
909 if (!ret) { /* Not found, retry with an alternative */
910 ret = get_alternative_probe_event(dinfo, pev, &tmp);
912 ret = debuginfo__find_available_vars_at(dinfo, pev,
914 /* Release the old probe_point */
915 clear_perf_probe_point(&tmp);
919 if (ret == 0 || ret == -ENOENT) {
920 pr_err("Failed to find the address of %s\n", buf);
923 pr_warning("Debuginfo analysis failed.\n");
927 /* Some variables are found */
928 fprintf(stdout, "Available variables at %s\n", buf);
929 for (i = 0; i < ret; i++) {
932 * A probe point might be converted to
933 * several trace points.
935 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
937 zfree(&vl->point.symbol);
940 strlist__for_each(node, vl->vars) {
941 var = strchr(node->s, '\t') + 1;
942 if (strfilter__compare(_filter, var)) {
943 fprintf(stdout, "\t\t%s\n", node->s);
947 strlist__delete(vl->vars);
950 fprintf(stdout, "\t\t(No matched variables)\n");
958 /* Show available variables on given probe point */
959 int show_available_vars(struct perf_probe_event *pevs, int npevs,
960 struct strfilter *_filter)
963 struct debuginfo *dinfo;
965 ret = init_probe_symbol_maps(pevs->uprobes);
969 dinfo = open_debuginfo(pevs->target, false);
977 for (i = 0; i < npevs && ret >= 0; i++)
978 ret = show_available_vars_at(dinfo, &pevs[i], _filter);
980 debuginfo__delete(dinfo);
982 exit_probe_symbol_maps();
986 #else /* !HAVE_DWARF_SUPPORT */
988 static void debuginfo_cache__exit(void)
993 find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused,
994 struct perf_probe_point *pp __maybe_unused,
995 bool is_kprobe __maybe_unused)
1000 static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
1001 struct probe_trace_event **tevs __maybe_unused)
1003 if (perf_probe_event_need_dwarf(pev)) {
1004 pr_warning("Debuginfo-analysis is not supported.\n");
1011 int show_line_range(struct line_range *lr __maybe_unused,
1012 const char *module __maybe_unused,
1013 bool user __maybe_unused)
1015 pr_warning("Debuginfo-analysis is not supported.\n");
1019 int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
1020 int npevs __maybe_unused,
1021 struct strfilter *filter __maybe_unused)
1023 pr_warning("Debuginfo-analysis is not supported.\n");
1028 void line_range__clear(struct line_range *lr)
1034 intlist__delete(lr->line_list);
1035 memset(lr, 0, sizeof(*lr));
1038 int line_range__init(struct line_range *lr)
1040 memset(lr, 0, sizeof(*lr));
1041 lr->line_list = intlist__new(NULL);
1048 static int parse_line_num(char **ptr, int *val, const char *what)
1050 const char *start = *ptr;
1053 *val = strtol(*ptr, ptr, 0);
1054 if (errno || *ptr == start) {
1055 semantic_error("'%s' is not a valid number.\n", what);
1061 /* Check the name is good for event, group or function */
1062 static bool is_c_func_name(const char *name)
1064 if (!isalpha(*name) && *name != '_')
1066 while (*++name != '\0') {
1067 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
1074 * Stuff 'lr' according to the line range described by 'arg'.
1075 * The line range syntax is described by:
1077 * SRC[:SLN[+NUM|-ELN]]
1078 * FNC[@SRC][:SLN[+NUM|-ELN]]
1080 int parse_line_range_desc(const char *arg, struct line_range *lr)
1082 char *range, *file, *name = strdup(arg);
1091 range = strchr(name, ':');
1095 err = parse_line_num(&range, &lr->start, "start line");
1099 if (*range == '+' || *range == '-') {
1100 const char c = *range++;
1102 err = parse_line_num(&range, &lr->end, "end line");
1107 lr->end += lr->start;
1109 * Adjust the number of lines here.
1110 * If the number of lines == 1, the
1111 * the end of line should be equal to
1112 * the start of line.
1118 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
1121 if (lr->start > lr->end) {
1122 semantic_error("Start line must be smaller"
1123 " than end line.\n");
1126 if (*range != '\0') {
1127 semantic_error("Tailing with invalid str '%s'.\n", range);
1132 file = strchr(name, '@');
1135 lr->file = strdup(++file);
1136 if (lr->file == NULL) {
1140 lr->function = name;
1141 } else if (strchr(name, '/') || strchr(name, '.'))
1143 else if (is_c_func_name(name))/* We reuse it for checking funcname */
1144 lr->function = name;
1145 else { /* Invalid name */
1146 semantic_error("'%s' is not a valid function name.\n", name);
1157 /* Parse probepoint definition. */
1158 static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
1160 struct perf_probe_point *pp = &pev->point;
1163 bool file_spec = false;
1166 * perf probe [EVENT=]SRC[:LN|;PTN]
1167 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
1169 * TODO:Group name support
1174 ptr = strpbrk(arg, ";=@+%");
1175 if (ptr && *ptr == '=') { /* Event name */
1178 if (strchr(arg, ':')) {
1179 semantic_error("Group name is not supported yet.\n");
1182 if (!is_c_func_name(arg)) {
1183 semantic_error("%s is bad for event name -it must "
1184 "follow C symbol-naming rule.\n", arg);
1187 pev->event = strdup(arg);
1188 if (pev->event == NULL)
1195 * Check arg is function or file name and copy it.
1197 * We consider arg to be a file spec if and only if it satisfies
1198 * all of the below criteria::
1199 * - it does not include any of "+@%",
1200 * - it includes one of ":;", and
1201 * - it has a period '.' in the name.
1203 * Otherwise, we consider arg to be a function specification.
1205 if (!strpbrk(arg, "+@%") && (ptr = strpbrk(arg, ";:")) != NULL) {
1206 /* This is a file spec if it includes a '.' before ; or : */
1207 if (memchr(arg, '.', ptr - arg))
1211 ptr = strpbrk(arg, ";:+@%");
1231 * Keep pp->function even if this is absolute address,
1232 * so it can mark whether abs_address is valid.
1233 * Which make 'perf probe lib.bin 0x0' possible.
1235 * Note that checking length of tmp is not needed
1236 * because when we access tmp[1] we know tmp[0] is '0',
1237 * so tmp[1] should always valid (but could be '\0').
1239 if (tmp && !strncmp(tmp, "0x", 2)) {
1240 pp->abs_address = strtoul(pp->function, &tmp, 0);
1242 semantic_error("Invalid absolute address.\n");
1248 /* Parse other options */
1252 if (c == ';') { /* Lazy pattern must be the last part */
1253 pp->lazy_line = strdup(arg);
1254 if (pp->lazy_line == NULL)
1258 ptr = strpbrk(arg, ";:+@%");
1264 case ':': /* Line number */
1265 pp->line = strtoul(arg, &tmp, 0);
1267 semantic_error("There is non-digit char"
1268 " in line number.\n");
1272 case '+': /* Byte offset from a symbol */
1273 pp->offset = strtoul(arg, &tmp, 0);
1275 semantic_error("There is non-digit character"
1280 case '@': /* File name */
1282 semantic_error("SRC@SRC is not allowed.\n");
1285 pp->file = strdup(arg);
1286 if (pp->file == NULL)
1289 case '%': /* Probe places */
1290 if (strcmp(arg, "return") == 0) {
1292 } else { /* Others not supported yet */
1293 semantic_error("%%%s is not supported.\n", arg);
1297 default: /* Buggy case */
1298 pr_err("This program has a bug at %s:%d.\n",
1299 __FILE__, __LINE__);
1305 /* Exclusion check */
1306 if (pp->lazy_line && pp->line) {
1307 semantic_error("Lazy pattern can't be used with"
1312 if (pp->lazy_line && pp->offset) {
1313 semantic_error("Lazy pattern can't be used with offset.\n");
1317 if (pp->line && pp->offset) {
1318 semantic_error("Offset can't be used with line number.\n");
1322 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
1323 semantic_error("File always requires line number or "
1328 if (pp->offset && !pp->function) {
1329 semantic_error("Offset requires an entry function.\n");
1333 if (pp->retprobe && !pp->function) {
1334 semantic_error("Return probe requires an entry function.\n");
1338 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
1339 semantic_error("Offset/Line/Lazy pattern can't be used with "
1344 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
1345 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
1350 /* Parse perf-probe event argument */
1351 static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
1353 char *tmp, *goodname;
1354 struct perf_probe_arg_field **fieldp;
1356 pr_debug("parsing arg: %s into ", str);
1358 tmp = strchr(str, '=');
1360 arg->name = strndup(str, tmp - str);
1361 if (arg->name == NULL)
1363 pr_debug("name:%s ", arg->name);
1367 tmp = strchr(str, ':');
1368 if (tmp) { /* Type setting */
1370 arg->type = strdup(tmp + 1);
1371 if (arg->type == NULL)
1373 pr_debug("type:%s ", arg->type);
1376 tmp = strpbrk(str, "-.[");
1377 if (!is_c_varname(str) || !tmp) {
1378 /* A variable, register, symbol or special value */
1379 arg->var = strdup(str);
1380 if (arg->var == NULL)
1382 pr_debug("%s\n", arg->var);
1386 /* Structure fields or array element */
1387 arg->var = strndup(str, tmp - str);
1388 if (arg->var == NULL)
1390 goodname = arg->var;
1391 pr_debug("%s, ", arg->var);
1392 fieldp = &arg->field;
1395 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
1396 if (*fieldp == NULL)
1398 if (*tmp == '[') { /* Array */
1400 (*fieldp)->index = strtol(str + 1, &tmp, 0);
1401 (*fieldp)->ref = true;
1402 if (*tmp != ']' || tmp == str + 1) {
1403 semantic_error("Array index must be a"
1410 } else { /* Structure */
1413 (*fieldp)->ref = false;
1414 } else if (tmp[1] == '>') {
1416 (*fieldp)->ref = true;
1418 semantic_error("Argument parse error: %s\n",
1422 tmp = strpbrk(str, "-.[");
1425 (*fieldp)->name = strndup(str, tmp - str);
1426 if ((*fieldp)->name == NULL)
1429 goodname = (*fieldp)->name;
1430 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
1431 fieldp = &(*fieldp)->next;
1434 (*fieldp)->name = strdup(str);
1435 if ((*fieldp)->name == NULL)
1438 goodname = (*fieldp)->name;
1439 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
1441 /* If no name is specified, set the last field name (not array index)*/
1443 arg->name = strdup(goodname);
1444 if (arg->name == NULL)
1450 /* Parse perf-probe event command */
1451 int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
1454 int argc, i, ret = 0;
1456 argv = argv_split(cmd, &argc);
1458 pr_debug("Failed to split arguments.\n");
1461 if (argc - 1 > MAX_PROBE_ARGS) {
1462 semantic_error("Too many probe arguments (%d).\n", argc - 1);
1466 /* Parse probe point */
1467 ret = parse_perf_probe_point(argv[0], pev);
1471 /* Copy arguments and ensure return probe has no C argument */
1472 pev->nargs = argc - 1;
1473 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1474 if (pev->args == NULL) {
1478 for (i = 0; i < pev->nargs && ret >= 0; i++) {
1479 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1481 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
1482 semantic_error("You can't specify local variable for"
1493 /* Return true if this perf_probe_event requires debuginfo */
1494 bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
1498 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1501 for (i = 0; i < pev->nargs; i++)
1502 if (is_c_varname(pev->args[i].var))
1508 /* Parse probe_events event into struct probe_point */
1509 int parse_probe_trace_command(const char *cmd, struct probe_trace_event *tev)
1511 struct probe_trace_point *tp = &tev->point;
1514 char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
1518 pr_debug("Parsing probe_events: %s\n", cmd);
1519 argv = argv_split(cmd, &argc);
1521 pr_debug("Failed to split arguments.\n");
1525 semantic_error("Too few probe arguments.\n");
1530 /* Scan event and group name. */
1531 argv0_str = strdup(argv[0]);
1532 if (argv0_str == NULL) {
1536 fmt1_str = strtok_r(argv0_str, ":", &fmt);
1537 fmt2_str = strtok_r(NULL, "/", &fmt);
1538 fmt3_str = strtok_r(NULL, " \t", &fmt);
1539 if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
1540 || fmt3_str == NULL) {
1541 semantic_error("Failed to parse event name: %s\n", argv[0]);
1546 tev->group = strdup(fmt2_str);
1547 tev->event = strdup(fmt3_str);
1548 if (tev->group == NULL || tev->event == NULL) {
1552 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
1554 tp->retprobe = (pr == 'r');
1556 /* Scan module name(if there), function name and offset */
1557 p = strchr(argv[1], ':');
1559 tp->module = strndup(argv[1], p - argv[1]);
1563 fmt1_str = strtok_r(p, "+", &fmt);
1564 /* only the address started with 0x */
1565 if (fmt1_str[0] == '0') {
1567 * Fix a special case:
1568 * if address == 0, kernel reports something like:
1569 * p:probe_libc/abs_0 /lib/libc-2.18.so:0x (null) arg1=%ax
1570 * Newer kernel may fix that, but we want to
1571 * support old kernel also.
1573 if (strcmp(fmt1_str, "0x") == 0) {
1574 if (!argv[2] || strcmp(argv[2], "(null)")) {
1581 for (i = 2; argv[i + 1] != NULL; i++)
1582 argv[i] = argv[i + 1];
1587 tp->address = strtoul(fmt1_str, NULL, 0);
1589 /* Only the symbol-based probe has offset */
1590 tp->symbol = strdup(fmt1_str);
1591 if (tp->symbol == NULL) {
1595 fmt2_str = strtok_r(NULL, "", &fmt);
1596 if (fmt2_str == NULL)
1599 tp->offset = strtoul(fmt2_str, NULL, 10);
1602 tev->nargs = argc - 2;
1603 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
1604 if (tev->args == NULL) {
1608 for (i = 0; i < tev->nargs; i++) {
1609 p = strchr(argv[i + 2], '=');
1610 if (p) /* We don't need which register is assigned. */
1614 tev->args[i].name = strdup(argv[i + 2]);
1615 /* TODO: parse regs and offset */
1616 tev->args[i].value = strdup(p);
1617 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1629 /* Compose only probe arg */
1630 int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
1632 struct perf_probe_arg_field *field = pa->field;
1636 if (pa->name && pa->var)
1637 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
1639 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
1646 if (field->name[0] == '[')
1647 ret = e_snprintf(tmp, len, "%s", field->name);
1649 ret = e_snprintf(tmp, len, "%s%s",
1650 field->ref ? "->" : ".", field->name);
1655 field = field->next;
1659 ret = e_snprintf(tmp, len, ":%s", pa->type);
1668 pr_debug("Failed to synthesize perf probe argument: %d\n", ret);
1672 /* Compose only probe point (not argument) */
1673 static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
1676 char offs[32] = "", line[32] = "", file[32] = "";
1679 buf = zalloc(MAX_CMDLEN);
1685 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
1690 ret = e_snprintf(line, 32, ":%d", pp->line);
1698 tmp = strchr(pp->file + len - 30, '/');
1699 tmp = tmp ? tmp + 1 : pp->file + len - 30;
1701 ret = e_snprintf(file, 32, "@%s", tmp);
1707 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
1708 offs, pp->retprobe ? "%return" : "", line,
1711 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
1717 pr_debug("Failed to synthesize perf probe point: %d\n", ret);
1723 char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1728 buf = synthesize_perf_probe_point(&pev->point);
1733 for (i = 0; i < pev->nargs; i++) {
1734 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
1747 static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
1748 char **buf, size_t *buflen,
1753 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
1759 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
1771 static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
1772 char *buf, size_t buflen)
1774 struct probe_trace_arg_ref *ref = arg->ref;
1778 /* Argument name or separator */
1780 ret = e_snprintf(buf, buflen, " %s=", arg->name);
1782 ret = e_snprintf(buf, buflen, " ");
1788 /* Special case: @XXX */
1789 if (arg->value[0] == '@' && arg->ref)
1792 /* Dereferencing arguments */
1794 depth = __synthesize_probe_trace_arg_ref(ref, &buf,
1800 /* Print argument value */
1801 if (arg->value[0] == '@' && arg->ref)
1802 ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
1805 ret = e_snprintf(buf, buflen, "%s", arg->value);
1813 ret = e_snprintf(buf, buflen, ")");
1819 /* Print argument type */
1821 ret = e_snprintf(buf, buflen, ":%s", arg->type);
1830 char *synthesize_probe_trace_command(struct probe_trace_event *tev)
1832 struct probe_trace_point *tp = &tev->point;
1836 buf = zalloc(MAX_CMDLEN);
1840 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
1841 tev->group, tev->event);
1845 /* Uprobes must have tp->module */
1846 if (tev->uprobes && !tp->module)
1849 * If tp->address == 0, then this point must be a
1850 * absolute address uprobe.
1851 * try_to_find_absolute_address() should have made
1852 * tp->symbol to "0x0".
1854 if (tev->uprobes && !tp->address) {
1855 if (!tp->symbol || strcmp(tp->symbol, "0x0"))
1859 /* Use the tp->address for uprobes */
1861 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s:0x%lx",
1862 tp->module, tp->address);
1863 else if (!strncmp(tp->symbol, "0x", 2))
1864 /* Absolute address. See try_to_find_absolute_address() */
1865 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s%s0x%lx",
1866 tp->module ?: "", tp->module ? ":" : "",
1869 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s%s%s+%lu",
1870 tp->module ?: "", tp->module ? ":" : "",
1871 tp->symbol, tp->offset);
1877 for (i = 0; i < tev->nargs; i++) {
1878 ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
1891 static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
1892 struct perf_probe_point *pp,
1895 struct symbol *sym = NULL;
1897 u64 addr = tp->address;
1901 map = dso__new_map(tp->module);
1904 sym = map__find_symbol(map, addr, NULL);
1906 if (tp->symbol && !addr) {
1907 if (kernel_get_symbol_address_by_name(tp->symbol,
1908 &addr, true, false) < 0)
1913 sym = __find_kernel_function(addr, &map);
1920 pp->retprobe = tp->retprobe;
1921 pp->offset = addr - map->unmap_ip(map, sym->start);
1922 pp->function = strdup(sym->name);
1923 ret = pp->function ? 0 : -ENOMEM;
1926 if (map && !is_kprobe) {
1933 static int convert_to_perf_probe_point(struct probe_trace_point *tp,
1934 struct perf_probe_point *pp,
1940 ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe);
1943 ret = find_perf_probe_point_from_map(tp, pp, is_kprobe);
1947 pr_debug("Failed to find probe point from both of dwarf and map.\n");
1950 pp->function = strdup(tp->symbol);
1951 pp->offset = tp->offset;
1953 ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address);
1956 pp->function = strdup(buf);
1959 if (pp->function == NULL)
1962 pp->retprobe = tp->retprobe;
1967 static int convert_to_perf_probe_event(struct probe_trace_event *tev,
1968 struct perf_probe_event *pev, bool is_kprobe)
1973 /* Convert event/group name */
1974 pev->event = strdup(tev->event);
1975 pev->group = strdup(tev->group);
1976 if (pev->event == NULL || pev->group == NULL)
1979 /* Convert trace_point to probe_point */
1980 ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
1984 /* Convert trace_arg to probe_arg */
1985 pev->nargs = tev->nargs;
1986 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1987 if (pev->args == NULL)
1989 for (i = 0; i < tev->nargs && ret >= 0; i++) {
1990 if (tev->args[i].name)
1991 pev->args[i].name = strdup(tev->args[i].name);
1993 ret = synthesize_probe_trace_arg(&tev->args[i],
1995 pev->args[i].name = strdup(buf);
1997 if (pev->args[i].name == NULL && ret >= 0)
2002 clear_perf_probe_event(pev);
2007 void clear_perf_probe_event(struct perf_probe_event *pev)
2009 struct perf_probe_arg_field *field, *next;
2015 clear_perf_probe_point(&pev->point);
2017 for (i = 0; i < pev->nargs; i++) {
2018 free(pev->args[i].name);
2019 free(pev->args[i].var);
2020 free(pev->args[i].type);
2021 field = pev->args[i].field;
2024 zfree(&field->name);
2030 memset(pev, 0, sizeof(*pev));
2033 void clear_probe_trace_event(struct probe_trace_event *tev)
2035 struct probe_trace_arg_ref *ref, *next;
2040 free(tev->point.symbol);
2041 free(tev->point.realname);
2042 free(tev->point.module);
2043 for (i = 0; i < tev->nargs; i++) {
2044 free(tev->args[i].name);
2045 free(tev->args[i].value);
2046 free(tev->args[i].type);
2047 ref = tev->args[i].ref;
2055 memset(tev, 0, sizeof(*tev));
2058 struct kprobe_blacklist_node {
2059 struct list_head list;
2060 unsigned long start;
2065 static void kprobe_blacklist__delete(struct list_head *blacklist)
2067 struct kprobe_blacklist_node *node;
2069 while (!list_empty(blacklist)) {
2070 node = list_first_entry(blacklist,
2071 struct kprobe_blacklist_node, list);
2072 list_del(&node->list);
2078 static int kprobe_blacklist__load(struct list_head *blacklist)
2080 struct kprobe_blacklist_node *node;
2081 const char *__debugfs = debugfs__mountpoint();
2082 char buf[PATH_MAX], *p;
2086 if (__debugfs == NULL)
2089 ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs);
2093 fp = fopen(buf, "r");
2098 while (fgets(buf, PATH_MAX, fp)) {
2099 node = zalloc(sizeof(*node));
2104 INIT_LIST_HEAD(&node->list);
2105 list_add_tail(&node->list, blacklist);
2106 if (sscanf(buf, "0x%lx-0x%lx", &node->start, &node->end) != 2) {
2110 p = strchr(buf, '\t');
2113 if (p[strlen(p) - 1] == '\n')
2114 p[strlen(p) - 1] = '\0';
2116 p = (char *)"unknown";
2117 node->symbol = strdup(p);
2118 if (!node->symbol) {
2122 pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
2123 node->start, node->end, node->symbol);
2127 kprobe_blacklist__delete(blacklist);
2133 static struct kprobe_blacklist_node *
2134 kprobe_blacklist__find_by_address(struct list_head *blacklist,
2135 unsigned long address)
2137 struct kprobe_blacklist_node *node;
2139 list_for_each_entry(node, blacklist, list) {
2140 if (node->start <= address && address <= node->end)
2147 static LIST_HEAD(kprobe_blacklist);
2149 static void kprobe_blacklist__init(void)
2151 if (!list_empty(&kprobe_blacklist))
2154 if (kprobe_blacklist__load(&kprobe_blacklist) < 0)
2155 pr_debug("No kprobe blacklist support, ignored\n");
2158 static void kprobe_blacklist__release(void)
2160 kprobe_blacklist__delete(&kprobe_blacklist);
2163 static bool kprobe_blacklist__listed(unsigned long address)
2165 return !!kprobe_blacklist__find_by_address(&kprobe_blacklist, address);
2168 static int perf_probe_event__sprintf(const char *group, const char *event,
2169 struct perf_probe_event *pev,
2171 struct strbuf *result)
2177 /* Synthesize only event probe point */
2178 place = synthesize_perf_probe_point(&pev->point);
2182 ret = e_snprintf(buf, 128, "%s:%s", group, event);
2186 strbuf_addf(result, " %-20s (on %s", buf, place);
2188 strbuf_addf(result, " in %s", module);
2190 if (pev->nargs > 0) {
2191 strbuf_addstr(result, " with");
2192 for (i = 0; i < pev->nargs; i++) {
2193 ret = synthesize_perf_probe_arg(&pev->args[i],
2197 strbuf_addf(result, " %s", buf);
2200 strbuf_addch(result, ')');
2207 int show_perf_probe_event(const char *group, const char *event,
2208 struct perf_probe_event *pev,
2209 const char *module, bool use_stdout)
2211 struct strbuf buf = STRBUF_INIT;
2214 ret = perf_probe_event__sprintf(group, event, pev, module, &buf);
2217 printf("%s\n", buf.buf);
2219 pr_info("%s\n", buf.buf);
2221 strbuf_release(&buf);
2226 static bool filter_probe_trace_event(struct probe_trace_event *tev,
2227 struct strfilter *filter)
2231 /* At first, check the event name itself */
2232 if (strfilter__compare(filter, tev->event))
2235 /* Next, check the combination of name and group */
2236 if (e_snprintf(tmp, 128, "%s:%s", tev->group, tev->event) < 0)
2238 return strfilter__compare(filter, tmp);
2241 static int __show_perf_probe_events(int fd, bool is_kprobe,
2242 struct strfilter *filter)
2245 struct probe_trace_event tev;
2246 struct perf_probe_event pev;
2247 struct strlist *rawlist;
2248 struct str_node *ent;
2250 memset(&tev, 0, sizeof(tev));
2251 memset(&pev, 0, sizeof(pev));
2253 rawlist = probe_file__get_rawlist(fd);
2257 strlist__for_each(ent, rawlist) {
2258 ret = parse_probe_trace_command(ent->s, &tev);
2260 if (!filter_probe_trace_event(&tev, filter))
2262 ret = convert_to_perf_probe_event(&tev, &pev,
2266 ret = show_perf_probe_event(pev.group, pev.event,
2267 &pev, tev.point.module,
2271 clear_perf_probe_event(&pev);
2272 clear_probe_trace_event(&tev);
2276 strlist__delete(rawlist);
2277 /* Cleanup cached debuginfo if needed */
2278 debuginfo_cache__exit();
2283 /* List up current perf-probe events */
2284 int show_perf_probe_events(struct strfilter *filter)
2286 int kp_fd, up_fd, ret;
2290 ret = init_probe_symbol_maps(false);
2294 ret = probe_file__open_both(&kp_fd, &up_fd, 0);
2299 ret = __show_perf_probe_events(kp_fd, true, filter);
2300 if (up_fd >= 0 && ret >= 0)
2301 ret = __show_perf_probe_events(up_fd, false, filter);
2306 exit_probe_symbol_maps();
2311 static int get_new_event_name(char *buf, size_t len, const char *base,
2312 struct strlist *namelist, bool allow_suffix)
2319 nbase = strdup(base);
2323 /* Cut off the dot suffixes (e.g. .const, .isra)*/
2324 p = strchr(nbase, '.');
2325 if (p && p != nbase)
2328 /* Try no suffix number */
2329 ret = e_snprintf(buf, len, "%s", nbase);
2331 pr_debug("snprintf() failed: %d\n", ret);
2334 if (!strlist__has_entry(namelist, buf))
2337 if (!allow_suffix) {
2338 pr_warning("Error: event \"%s\" already exists. "
2339 "(Use -f to force duplicates.)\n", buf);
2344 /* Try to add suffix */
2345 for (i = 1; i < MAX_EVENT_INDEX; i++) {
2346 ret = e_snprintf(buf, len, "%s_%d", nbase, i);
2348 pr_debug("snprintf() failed: %d\n", ret);
2351 if (!strlist__has_entry(namelist, buf))
2354 if (i == MAX_EVENT_INDEX) {
2355 pr_warning("Too many events are on the same function.\n");
2362 /* Final validation */
2363 if (ret >= 0 && !is_c_func_name(buf)) {
2364 pr_warning("Internal error: \"%s\" is an invalid event name.\n",
2372 /* Warn if the current kernel's uprobe implementation is old */
2373 static void warn_uprobe_event_compat(struct probe_trace_event *tev)
2376 char *buf = synthesize_probe_trace_command(tev);
2378 /* Old uprobe event doesn't support memory dereference */
2379 if (!tev->uprobes || tev->nargs == 0 || !buf)
2382 for (i = 0; i < tev->nargs; i++)
2383 if (strglobmatch(tev->args[i].value, "[$@+-]*")) {
2384 pr_warning("Please upgrade your kernel to at least "
2385 "3.14 to have access to feature %s\n",
2386 tev->args[i].value);
2393 /* Set new name from original perf_probe_event and namelist */
2394 static int probe_trace_event__set_name(struct probe_trace_event *tev,
2395 struct perf_probe_event *pev,
2396 struct strlist *namelist,
2399 const char *event, *group;
2406 if (pev->point.function &&
2407 (strncmp(pev->point.function, "0x", 2) != 0) &&
2408 !strisglob(pev->point.function))
2409 event = pev->point.function;
2411 event = tev->point.realname;
2415 group = PERFPROBE_GROUP;
2417 /* Get an unused new event name */
2418 ret = get_new_event_name(buf, 64, event,
2419 namelist, allow_suffix);
2425 tev->event = strdup(event);
2426 tev->group = strdup(group);
2427 if (tev->event == NULL || tev->group == NULL)
2430 /* Add added event name to namelist */
2431 strlist__add(namelist, event);
2435 static int __add_probe_trace_events(struct perf_probe_event *pev,
2436 struct probe_trace_event *tevs,
2437 int ntevs, bool allow_suffix)
2440 struct probe_trace_event *tev = NULL;
2441 struct strlist *namelist;
2443 fd = probe_file__open(PF_FL_RW | (pev->uprobes ? PF_FL_UPROBE : 0));
2447 /* Get current event names */
2448 namelist = probe_file__get_namelist(fd);
2450 pr_debug("Failed to get current event list.\n");
2456 for (i = 0; i < ntevs; i++) {
2458 /* Skip if the symbol is out of .text or blacklisted */
2459 if (!tev->point.symbol)
2462 /* Set new name for tev (and update namelist) */
2463 ret = probe_trace_event__set_name(tev, pev, namelist,
2468 ret = probe_file__add_event(fd, tev);
2473 * Probes after the first probe which comes from same
2474 * user input are always allowed to add suffix, because
2475 * there might be several addresses corresponding to
2478 allow_suffix = true;
2480 if (ret == -EINVAL && pev->uprobes)
2481 warn_uprobe_event_compat(tev);
2483 strlist__delete(namelist);
2489 static int find_probe_functions(struct map *map, char *name,
2490 struct symbol **syms)
2494 struct rb_node *tmp;
2496 if (map__load(map, NULL) < 0)
2499 map__for_each_symbol(map, sym, tmp) {
2500 if (strglobmatch(sym->name, name)) {
2502 if (syms && found < probe_conf.max_probes)
2503 syms[found - 1] = sym;
2510 #define strdup_or_goto(str, label) \
2511 ({ char *__p = strdup(str); if (!__p) goto label; __p; })
2513 void __weak arch__fix_tev_from_maps(struct perf_probe_event *pev __maybe_unused,
2514 struct probe_trace_event *tev __maybe_unused,
2515 struct map *map __maybe_unused) { }
2518 * Find probe function addresses from map.
2519 * Return an error or the number of found probe_trace_event
2521 static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
2522 struct probe_trace_event **tevs)
2524 struct map *map = NULL;
2525 struct ref_reloc_sym *reloc_sym = NULL;
2527 struct symbol **syms = NULL;
2528 struct probe_trace_event *tev;
2529 struct perf_probe_point *pp = &pev->point;
2530 struct probe_trace_point *tp;
2531 int num_matched_functions;
2532 int ret, i, j, skipped = 0;
2534 map = get_target_map(pev->target, pev->uprobes);
2540 syms = malloc(sizeof(struct symbol *) * probe_conf.max_probes);
2547 * Load matched symbols: Since the different local symbols may have
2548 * same name but different addresses, this lists all the symbols.
2550 num_matched_functions = find_probe_functions(map, pp->function, syms);
2551 if (num_matched_functions == 0) {
2552 pr_err("Failed to find symbol %s in %s\n", pp->function,
2553 pev->target ? : "kernel");
2556 } else if (num_matched_functions > probe_conf.max_probes) {
2557 pr_err("Too many functions matched in %s\n",
2558 pev->target ? : "kernel");
2563 /* Note that the symbols in the kmodule are not relocated */
2564 if (!pev->uprobes && !pp->retprobe && !pev->target) {
2565 reloc_sym = kernel_get_ref_reloc_sym(NULL);
2567 pr_warning("Relocated base symbol is not found!\n");
2573 /* Setup result trace-probe-events */
2574 *tevs = zalloc(sizeof(*tev) * num_matched_functions);
2582 for (j = 0; j < num_matched_functions; j++) {
2585 tev = (*tevs) + ret;
2587 if (ret == num_matched_functions) {
2588 pr_warning("Too many symbols are listed. Skip it.\n");
2593 if (pp->offset > sym->end - sym->start) {
2594 pr_warning("Offset %ld is bigger than the size of %s\n",
2595 pp->offset, sym->name);
2599 /* Add one probe point */
2600 tp->address = map->unmap_ip(map, sym->start) + pp->offset;
2602 /* Check the kprobe (not in module) is within .text */
2603 if (!pev->uprobes && !pev->target &&
2604 kprobe_warn_out_range(sym->name, tp->address)) {
2605 tp->symbol = NULL; /* Skip it */
2607 } else if (reloc_sym) {
2608 tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
2609 tp->offset = tp->address - reloc_sym->addr;
2611 tp->symbol = strdup_or_goto(sym->name, nomem_out);
2612 tp->offset = pp->offset;
2614 tp->realname = strdup_or_goto(sym->name, nomem_out);
2616 tp->retprobe = pp->retprobe;
2618 tev->point.module = strdup_or_goto(pev->target,
2620 tev->uprobes = pev->uprobes;
2621 tev->nargs = pev->nargs;
2623 tev->args = zalloc(sizeof(struct probe_trace_arg) *
2625 if (tev->args == NULL)
2628 for (i = 0; i < tev->nargs; i++) {
2629 if (pev->args[i].name)
2631 strdup_or_goto(pev->args[i].name,
2634 tev->args[i].value = strdup_or_goto(pev->args[i].var,
2636 if (pev->args[i].type)
2638 strdup_or_goto(pev->args[i].type,
2641 arch__fix_tev_from_maps(pev, tev, map);
2643 if (ret == skipped) {
2649 put_target_map(map, pev->uprobes);
2656 clear_probe_trace_events(*tevs, num_matched_functions);
2661 static int try_to_find_absolute_address(struct perf_probe_event *pev,
2662 struct probe_trace_event **tevs)
2664 struct perf_probe_point *pp = &pev->point;
2665 struct probe_trace_event *tev;
2666 struct probe_trace_point *tp;
2669 if (!(pev->point.function && !strncmp(pev->point.function, "0x", 2)))
2671 if (perf_probe_event_need_dwarf(pev))
2675 * This is 'perf probe /lib/libc.so 0xabcd'. Try to probe at
2678 * Only one tev can be generated by this.
2680 *tevs = zalloc(sizeof(*tev));
2688 * Don't use tp->offset, use address directly, because
2689 * in synthesize_probe_trace_command() address cannot be
2692 tp->address = pev->point.abs_address;
2693 tp->retprobe = pp->retprobe;
2694 tev->uprobes = pev->uprobes;
2698 * Give it a '0x' leading symbol name.
2699 * In __add_probe_trace_events, a NULL symbol is interpreted as
2702 if (asprintf(&tp->symbol, "0x%lx", tp->address) < 0)
2705 /* For kprobe, check range */
2706 if ((!tev->uprobes) &&
2707 (kprobe_warn_out_range(tev->point.symbol,
2708 tev->point.address))) {
2713 if (asprintf(&tp->realname, "abs_%lx", tp->address) < 0)
2717 tp->module = strdup(pev->target);
2723 tev->group = strdup(pev->group);
2729 tev->event = strdup(pev->event);
2734 tev->nargs = pev->nargs;
2735 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
2740 for (i = 0; i < tev->nargs; i++)
2741 copy_to_probe_trace_arg(&tev->args[i], &pev->args[i]);
2747 clear_probe_trace_events(*tevs, 1);
2753 bool __weak arch__prefers_symtab(void) { return false; }
2755 static int convert_to_probe_trace_events(struct perf_probe_event *pev,
2756 struct probe_trace_event **tevs)
2760 if (pev->uprobes && !pev->group) {
2761 /* Replace group name if not given */
2762 ret = convert_exec_to_group(pev->target, &pev->group);
2764 pr_warning("Failed to make a group name.\n");
2769 ret = try_to_find_absolute_address(pev, tevs);
2773 if (arch__prefers_symtab() && !perf_probe_event_need_dwarf(pev)) {
2774 ret = find_probe_trace_events_from_map(pev, tevs);
2776 return ret; /* Found in symbol table */
2779 /* Convert perf_probe_event with debuginfo */
2780 ret = try_to_find_probe_trace_events(pev, tevs);
2782 return ret; /* Found in debuginfo or got an error */
2784 return find_probe_trace_events_from_map(pev, tevs);
2787 int convert_perf_probe_events(struct perf_probe_event *pevs, int npevs)
2791 /* Loop 1: convert all events */
2792 for (i = 0; i < npevs; i++) {
2793 /* Init kprobe blacklist if needed */
2794 if (!pevs[i].uprobes)
2795 kprobe_blacklist__init();
2796 /* Convert with or without debuginfo */
2797 ret = convert_to_probe_trace_events(&pevs[i], &pevs[i].tevs);
2800 pevs[i].ntevs = ret;
2802 /* This just release blacklist only if allocated */
2803 kprobe_blacklist__release();
2808 int apply_perf_probe_events(struct perf_probe_event *pevs, int npevs)
2812 /* Loop 2: add all events */
2813 for (i = 0; i < npevs; i++) {
2814 ret = __add_probe_trace_events(&pevs[i], pevs[i].tevs,
2816 probe_conf.force_add);
2823 void cleanup_perf_probe_events(struct perf_probe_event *pevs, int npevs)
2827 /* Loop 3: cleanup and free trace events */
2828 for (i = 0; i < npevs; i++) {
2829 for (j = 0; j < pevs[i].ntevs; j++)
2830 clear_probe_trace_event(&pevs[i].tevs[j]);
2831 zfree(&pevs[i].tevs);
2833 clear_perf_probe_event(&pevs[i]);
2837 int add_perf_probe_events(struct perf_probe_event *pevs, int npevs)
2841 ret = init_probe_symbol_maps(pevs->uprobes);
2845 ret = convert_perf_probe_events(pevs, npevs);
2847 ret = apply_perf_probe_events(pevs, npevs);
2849 cleanup_perf_probe_events(pevs, npevs);
2851 exit_probe_symbol_maps();
2855 int del_perf_probe_events(struct strfilter *filter)
2857 int ret, ret2, ufd = -1, kfd = -1;
2858 char *str = strfilter__string(filter);
2863 /* Get current event names */
2864 ret = probe_file__open_both(&kfd, &ufd, PF_FL_RW);
2868 ret = probe_file__del_events(kfd, filter);
2869 if (ret < 0 && ret != -ENOENT)
2872 ret2 = probe_file__del_events(ufd, filter);
2873 if (ret2 < 0 && ret2 != -ENOENT) {
2890 /* TODO: don't use a global variable for filter ... */
2891 static struct strfilter *available_func_filter;
2894 * If a symbol corresponds to a function with global binding and
2895 * matches filter return 0. For all others return 1.
2897 static int filter_available_functions(struct map *map __maybe_unused,
2900 if (strfilter__compare(available_func_filter, sym->name))
2905 int show_available_funcs(const char *target, struct strfilter *_filter,
2911 ret = init_probe_symbol_maps(user);
2915 /* Get a symbol map */
2917 map = dso__new_map(target);
2919 map = kernel_get_module_map(target);
2921 pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
2925 /* Load symbols with given filter */
2926 available_func_filter = _filter;
2927 if (map__load(map, filter_available_functions)) {
2928 pr_err("Failed to load symbols in %s\n", (target) ? : "kernel");
2931 if (!dso__sorted_by_name(map->dso, map->type))
2932 dso__sort_by_name(map->dso, map->type);
2934 /* Show all (filtered) symbols */
2936 dso__fprintf_symbols_by_name(map->dso, map->type, stdout);
2941 exit_probe_symbol_maps();
2946 int copy_to_probe_trace_arg(struct probe_trace_arg *tvar,
2947 struct perf_probe_arg *pvar)
2949 tvar->value = strdup(pvar->var);
2950 if (tvar->value == NULL)
2953 tvar->type = strdup(pvar->type);
2954 if (tvar->type == NULL)
2958 tvar->name = strdup(pvar->name);
2959 if (tvar->name == NULL)