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/debugfs.h>
44 #include <api/fs/tracefs.h>
45 #include "trace-event.h" /* For __maybe_unused */
46 #include "probe-event.h"
47 #include "probe-finder.h"
48 #include "probe-file.h"
51 #define MAX_CMDLEN 256
52 #define PERFPROBE_GROUP "probe"
54 bool probe_event_dry_run; /* Dry run flag */
55 struct probe_conf probe_conf;
57 #define semantic_error(msg ...) pr_err("Semantic error :" msg)
59 int e_snprintf(char *str, size_t size, const char *format, ...)
64 ret = vsnprintf(str, size, format, ap);
71 static char *synthesize_perf_probe_point(struct perf_probe_point *pp);
72 static struct machine *host_machine;
74 /* Initialize symbol maps and path of vmlinux/modules */
75 static int init_symbol_maps(bool user_only)
79 symbol_conf.sort_by_name = true;
80 symbol_conf.allow_aliases = true;
81 ret = symbol__init(NULL);
83 pr_debug("Failed to init symbol map.\n");
87 if (host_machine || user_only) /* already initialized */
90 if (symbol_conf.vmlinux_name)
91 pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
93 host_machine = machine__new_host();
95 pr_debug("machine__new_host() failed.\n");
101 pr_warning("Failed to init vmlinux path.\n");
105 static void exit_symbol_maps(void)
108 machine__delete(host_machine);
114 static struct symbol *__find_kernel_function_by_name(const char *name,
117 return machine__find_kernel_function_by_name(host_machine, name, mapp,
121 static struct symbol *__find_kernel_function(u64 addr, struct map **mapp)
123 return machine__find_kernel_function(host_machine, addr, mapp, NULL);
126 static struct ref_reloc_sym *kernel_get_ref_reloc_sym(void)
128 /* kmap->ref_reloc_sym should be set if host_machine is initialized */
131 if (map__load(host_machine->vmlinux_maps[MAP__FUNCTION], NULL) < 0)
134 kmap = map__kmap(host_machine->vmlinux_maps[MAP__FUNCTION]);
137 return kmap->ref_reloc_sym;
140 static u64 kernel_get_symbol_address_by_name(const char *name, bool reloc)
142 struct ref_reloc_sym *reloc_sym;
146 /* ref_reloc_sym is just a label. Need a special fix*/
147 reloc_sym = kernel_get_ref_reloc_sym();
148 if (reloc_sym && strcmp(name, reloc_sym->name) == 0)
149 return (reloc) ? reloc_sym->addr : reloc_sym->unrelocated_addr;
151 sym = __find_kernel_function_by_name(name, &map);
153 return map->unmap_ip(map, sym->start) -
154 ((reloc) ? 0 : map->reloc);
159 static struct map *kernel_get_module_map(const char *module)
161 struct map_groups *grp = &host_machine->kmaps;
162 struct maps *maps = &grp->maps[MAP__FUNCTION];
165 /* A file path -- this is an offline module */
166 if (module && strchr(module, '/'))
167 return machine__findnew_module_map(host_machine, 0, module);
172 for (pos = maps__first(maps); pos; pos = map__next(pos)) {
173 if (strncmp(pos->dso->short_name + 1, module,
174 pos->dso->short_name_len - 2) == 0) {
181 static struct map *get_target_map(const char *target, bool user)
183 /* Init maps of given executable or kernel */
185 return dso__new_map(target);
187 return kernel_get_module_map(target);
190 static void put_target_map(struct map *map, bool user)
193 /* Only the user map needs to be released */
199 static int convert_exec_to_group(const char *exec, char **result)
201 char *ptr1, *ptr2, *exec_copy;
205 exec_copy = strdup(exec);
209 ptr1 = basename(exec_copy);
215 ptr2 = strpbrk(ptr1, "-._");
218 ret = e_snprintf(buf, 64, "%s_%s", PERFPROBE_GROUP, ptr1);
222 *result = strdup(buf);
223 ret = *result ? 0 : -ENOMEM;
230 static void clear_perf_probe_point(struct perf_probe_point *pp)
237 static void clear_probe_trace_events(struct probe_trace_event *tevs, int ntevs)
241 for (i = 0; i < ntevs; i++)
242 clear_probe_trace_event(tevs + i);
245 static bool kprobe_blacklist__listed(unsigned long address);
246 static bool kprobe_warn_out_range(const char *symbol, unsigned long address)
250 /* Get the address of _etext for checking non-probable text symbol */
251 etext_addr = kernel_get_symbol_address_by_name("_etext", false);
253 if (etext_addr != 0 && etext_addr < address)
254 pr_warning("%s is out of .text, skip it.\n", symbol);
255 else if (kprobe_blacklist__listed(address))
256 pr_warning("%s is blacklisted function, skip it.\n", symbol);
263 #ifdef HAVE_DWARF_SUPPORT
265 static int kernel_get_module_dso(const char *module, struct dso **pdso)
269 const char *vmlinux_name;
273 char module_name[128];
275 snprintf(module_name, sizeof(module_name), "[%s]", module);
276 map = map_groups__find_by_name(&host_machine->kmaps, MAP__FUNCTION, module_name);
281 pr_debug("Failed to find module %s.\n", module);
285 map = host_machine->vmlinux_maps[MAP__FUNCTION];
288 vmlinux_name = symbol_conf.vmlinux_name;
291 ret = dso__load_vmlinux(dso, map, vmlinux_name, false, NULL);
293 ret = dso__load_vmlinux_path(dso, map, NULL);
300 * Some binaries like glibc have special symbols which are on the symbol
301 * table, but not in the debuginfo. If we can find the address of the
302 * symbol from map, we can translate the address back to the probe point.
304 static int find_alternative_probe_point(struct debuginfo *dinfo,
305 struct perf_probe_point *pp,
306 struct perf_probe_point *result,
307 const char *target, bool uprobes)
309 struct map *map = NULL;
314 /* This can work only for function-name based one */
315 if (!pp->function || pp->file)
318 map = get_target_map(target, uprobes);
322 /* Find the address of given function */
323 map__for_each_symbol_by_name(map, pp->function, sym) {
325 address = sym->start;
327 address = map->unmap_ip(map, sym->start);
334 pr_debug("Symbol %s address found : %" PRIx64 "\n",
335 pp->function, address);
337 ret = debuginfo__find_probe_point(dinfo, (unsigned long)address,
340 ret = (!ret) ? -ENOENT : ret;
342 result->offset += pp->offset;
343 result->line += pp->line;
344 result->retprobe = pp->retprobe;
349 put_target_map(map, uprobes);
354 static int get_alternative_probe_event(struct debuginfo *dinfo,
355 struct perf_probe_event *pev,
356 struct perf_probe_point *tmp)
360 memcpy(tmp, &pev->point, sizeof(*tmp));
361 memset(&pev->point, 0, sizeof(pev->point));
362 ret = find_alternative_probe_point(dinfo, tmp, &pev->point,
363 pev->target, pev->uprobes);
365 memcpy(&pev->point, tmp, sizeof(*tmp));
370 static int get_alternative_line_range(struct debuginfo *dinfo,
371 struct line_range *lr,
372 const char *target, bool user)
374 struct perf_probe_point pp = { .function = lr->function,
377 struct perf_probe_point result;
380 memset(&result, 0, sizeof(result));
382 if (lr->end != INT_MAX)
383 len = lr->end - lr->start;
384 ret = find_alternative_probe_point(dinfo, &pp, &result,
387 lr->function = result.function;
388 lr->file = result.file;
389 lr->start = result.line;
390 if (lr->end != INT_MAX)
391 lr->end = lr->start + len;
392 clear_perf_probe_point(&pp);
397 /* Open new debuginfo of given module */
398 static struct debuginfo *open_debuginfo(const char *module, bool silent)
400 const char *path = module;
401 char reason[STRERR_BUFSIZE];
402 struct debuginfo *ret = NULL;
403 struct dso *dso = NULL;
406 if (!module || !strchr(module, '/')) {
407 err = kernel_get_module_dso(module, &dso);
409 if (!dso || dso->load_errno == 0) {
410 if (!strerror_r(-err, reason, STRERR_BUFSIZE))
411 strcpy(reason, "(unknown)");
413 dso__strerror_load(dso, reason, STRERR_BUFSIZE);
415 pr_err("Failed to find the path for %s: %s\n",
416 module ?: "kernel", reason);
419 path = dso->long_name;
421 ret = debuginfo__new(path);
422 if (!ret && !silent) {
423 pr_warning("The %s file has no debug information.\n", path);
424 if (!module || !strtailcmp(path, ".ko"))
425 pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, ");
427 pr_warning("Rebuild with -g, ");
428 pr_warning("or install an appropriate debuginfo package.\n");
433 /* For caching the last debuginfo */
434 static struct debuginfo *debuginfo_cache;
435 static char *debuginfo_cache_path;
437 static struct debuginfo *debuginfo_cache__open(const char *module, bool silent)
439 if ((debuginfo_cache_path && !strcmp(debuginfo_cache_path, module)) ||
440 (!debuginfo_cache_path && !module && debuginfo_cache))
443 /* Copy module path */
444 free(debuginfo_cache_path);
446 debuginfo_cache_path = strdup(module);
447 if (!debuginfo_cache_path) {
448 debuginfo__delete(debuginfo_cache);
449 debuginfo_cache = NULL;
454 debuginfo_cache = open_debuginfo(module, silent);
455 if (!debuginfo_cache)
456 zfree(&debuginfo_cache_path);
458 return debuginfo_cache;
461 static void debuginfo_cache__exit(void)
463 debuginfo__delete(debuginfo_cache);
464 debuginfo_cache = NULL;
465 zfree(&debuginfo_cache_path);
469 static int get_text_start_address(const char *exec, unsigned long *address)
474 int fd, ret = -ENOENT;
476 fd = open(exec, O_RDONLY);
480 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
484 if (gelf_getehdr(elf, &ehdr) == NULL)
487 if (!elf_section_by_name(elf, &ehdr, &shdr, ".text", NULL))
490 *address = shdr.sh_addr - shdr.sh_offset;
498 * Convert trace point to probe point with debuginfo
500 static int find_perf_probe_point_from_dwarf(struct probe_trace_point *tp,
501 struct perf_probe_point *pp,
504 struct debuginfo *dinfo = NULL;
505 unsigned long stext = 0;
506 u64 addr = tp->address;
509 /* convert the address to dwarf address */
515 ret = get_text_start_address(tp->module, &stext);
519 } else if (tp->symbol) {
520 addr = kernel_get_symbol_address_by_name(tp->symbol, false);
526 pr_debug("try to find information at %" PRIx64 " in %s\n", addr,
527 tp->module ? : "kernel");
529 dinfo = debuginfo_cache__open(tp->module, verbose == 0);
531 ret = debuginfo__find_probe_point(dinfo,
532 (unsigned long)addr, pp);
537 pp->retprobe = tp->retprobe;
541 pr_debug("Failed to find corresponding probes from debuginfo.\n");
542 return ret ? : -ENOENT;
545 static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs,
546 int ntevs, const char *exec)
549 unsigned long stext = 0;
554 ret = get_text_start_address(exec, &stext);
558 for (i = 0; i < ntevs && ret >= 0; i++) {
559 /* point.address is the addres of point.symbol + point.offset */
560 tevs[i].point.address -= stext;
561 tevs[i].point.module = strdup(exec);
562 if (!tevs[i].point.module) {
566 tevs[i].uprobes = true;
572 static int add_module_to_probe_trace_events(struct probe_trace_event *tevs,
573 int ntevs, const char *module)
581 tmp = strrchr(module, '/');
583 /* This is a module path -- get the module name */
584 module = strdup(tmp + 1);
587 tmp = strchr(module, '.');
590 tmp = (char *)module; /* For free() */
593 for (i = 0; i < ntevs; i++) {
594 tevs[i].point.module = strdup(module);
595 if (!tevs[i].point.module) {
605 /* Post processing the probe events */
606 static int post_process_probe_trace_events(struct probe_trace_event *tevs,
607 int ntevs, const char *module,
610 struct ref_reloc_sym *reloc_sym;
615 return add_exec_to_probe_trace_events(tevs, ntevs, module);
617 /* Note that currently ref_reloc_sym based probe is not for drivers */
619 return add_module_to_probe_trace_events(tevs, ntevs, module);
621 reloc_sym = kernel_get_ref_reloc_sym();
623 pr_warning("Relocated base symbol is not found!\n");
627 for (i = 0; i < ntevs; i++) {
628 if (!tevs[i].point.address || tevs[i].point.retprobe)
630 /* If we found a wrong one, mark it by NULL symbol */
631 if (kprobe_warn_out_range(tevs[i].point.symbol,
632 tevs[i].point.address)) {
636 tmp = strdup(reloc_sym->name);
640 /* If we have no realname, use symbol for it */
641 if (!tevs[i].point.realname)
642 tevs[i].point.realname = tevs[i].point.symbol;
644 free(tevs[i].point.symbol);
645 tevs[i].point.symbol = tmp;
646 tevs[i].point.offset = tevs[i].point.address -
647 reloc_sym->unrelocated_addr;
652 /* Try to find perf_probe_event with debuginfo */
653 static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
654 struct probe_trace_event **tevs)
656 bool need_dwarf = perf_probe_event_need_dwarf(pev);
657 struct perf_probe_point tmp;
658 struct debuginfo *dinfo;
661 dinfo = open_debuginfo(pev->target, !need_dwarf);
665 pr_debug("Could not open debuginfo. Try to use symbols.\n");
669 pr_debug("Try to find probe point from debuginfo.\n");
670 /* Searching trace events corresponding to a probe event */
671 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
673 if (ntevs == 0) { /* Not found, retry with an alternative */
674 ret = get_alternative_probe_event(dinfo, pev, &tmp);
676 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
678 * Write back to the original probe_event for
679 * setting appropriate (user given) event name
681 clear_perf_probe_point(&pev->point);
682 memcpy(&pev->point, &tmp, sizeof(tmp));
686 debuginfo__delete(dinfo);
688 if (ntevs > 0) { /* Succeeded to find trace events */
689 pr_debug("Found %d probe_trace_events.\n", ntevs);
690 ret = post_process_probe_trace_events(*tevs, ntevs,
691 pev->target, pev->uprobes);
692 if (ret < 0 || ret == ntevs) {
693 clear_probe_trace_events(*tevs, ntevs);
697 return ret < 0 ? ret : ntevs;
702 if (ntevs == 0) { /* No error but failed to find probe point. */
703 pr_warning("Probe point '%s' not found.\n",
704 synthesize_perf_probe_point(&pev->point));
707 /* Error path : ntevs < 0 */
708 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
711 pr_warning("Warning: No dwarf info found in the vmlinux - "
712 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
714 pr_debug("Trying to use symbols.\n");
721 #define LINEBUF_SIZE 256
722 #define NR_ADDITIONAL_LINES 2
724 static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
726 char buf[LINEBUF_SIZE], sbuf[STRERR_BUFSIZE];
727 const char *color = show_num ? "" : PERF_COLOR_BLUE;
728 const char *prefix = NULL;
731 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
736 prefix = show_num ? "%7d " : " ";
737 color_fprintf(stdout, color, prefix, l);
739 color_fprintf(stdout, color, "%s", buf);
741 } while (strchr(buf, '\n') == NULL);
746 pr_warning("File read error: %s\n",
747 strerror_r(errno, sbuf, sizeof(sbuf)));
753 static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
755 int rv = __show_one_line(fp, l, skip, show_num);
757 pr_warning("Source file is shorter than expected.\n");
763 #define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
764 #define show_one_line(f,l) _show_one_line(f,l,false,false)
765 #define skip_one_line(f,l) _show_one_line(f,l,true,false)
766 #define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
769 * Show line-range always requires debuginfo to find source file and
772 static int __show_line_range(struct line_range *lr, const char *module,
777 struct debuginfo *dinfo;
781 char sbuf[STRERR_BUFSIZE];
783 /* Search a line range */
784 dinfo = open_debuginfo(module, false);
788 ret = debuginfo__find_line_range(dinfo, lr);
789 if (!ret) { /* Not found, retry with an alternative */
790 ret = get_alternative_line_range(dinfo, lr, module, user);
792 ret = debuginfo__find_line_range(dinfo, lr);
794 debuginfo__delete(dinfo);
795 if (ret == 0 || ret == -ENOENT) {
796 pr_warning("Specified source line is not found.\n");
798 } else if (ret < 0) {
799 pr_warning("Debuginfo analysis failed.\n");
803 /* Convert source file path */
805 ret = get_real_path(tmp, lr->comp_dir, &lr->path);
807 /* Free old path when new path is assigned */
812 pr_warning("Failed to find source file path.\n");
819 fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
820 lr->start - lr->offset);
822 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
824 fp = fopen(lr->path, "r");
826 pr_warning("Failed to open %s: %s\n", lr->path,
827 strerror_r(errno, sbuf, sizeof(sbuf)));
830 /* Skip to starting line number */
831 while (l < lr->start) {
832 ret = skip_one_line(fp, l++);
837 intlist__for_each(ln, lr->line_list) {
838 for (; ln->i > l; l++) {
839 ret = show_one_line(fp, l - lr->offset);
843 ret = show_one_line_with_num(fp, l++ - lr->offset);
848 if (lr->end == INT_MAX)
849 lr->end = l + NR_ADDITIONAL_LINES;
850 while (l <= lr->end) {
851 ret = show_one_line_or_eof(fp, l++ - lr->offset);
860 int show_line_range(struct line_range *lr, const char *module, bool user)
864 ret = init_symbol_maps(user);
867 ret = __show_line_range(lr, module, user);
873 static int show_available_vars_at(struct debuginfo *dinfo,
874 struct perf_probe_event *pev,
875 struct strfilter *_filter)
879 struct str_node *node;
880 struct variable_list *vls = NULL, *vl;
881 struct perf_probe_point tmp;
884 buf = synthesize_perf_probe_point(&pev->point);
887 pr_debug("Searching variables at %s\n", buf);
889 ret = debuginfo__find_available_vars_at(dinfo, pev, &vls);
890 if (!ret) { /* Not found, retry with an alternative */
891 ret = get_alternative_probe_event(dinfo, pev, &tmp);
893 ret = debuginfo__find_available_vars_at(dinfo, pev,
895 /* Release the old probe_point */
896 clear_perf_probe_point(&tmp);
900 if (ret == 0 || ret == -ENOENT) {
901 pr_err("Failed to find the address of %s\n", buf);
904 pr_warning("Debuginfo analysis failed.\n");
908 /* Some variables are found */
909 fprintf(stdout, "Available variables at %s\n", buf);
910 for (i = 0; i < ret; i++) {
913 * A probe point might be converted to
914 * several trace points.
916 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
918 zfree(&vl->point.symbol);
921 strlist__for_each(node, vl->vars) {
922 var = strchr(node->s, '\t') + 1;
923 if (strfilter__compare(_filter, var)) {
924 fprintf(stdout, "\t\t%s\n", node->s);
928 strlist__delete(vl->vars);
931 fprintf(stdout, "\t\t(No matched variables)\n");
939 /* Show available variables on given probe point */
940 int show_available_vars(struct perf_probe_event *pevs, int npevs,
941 struct strfilter *_filter)
944 struct debuginfo *dinfo;
946 ret = init_symbol_maps(pevs->uprobes);
950 dinfo = open_debuginfo(pevs->target, false);
958 for (i = 0; i < npevs && ret >= 0; i++)
959 ret = show_available_vars_at(dinfo, &pevs[i], _filter);
961 debuginfo__delete(dinfo);
967 #else /* !HAVE_DWARF_SUPPORT */
969 static void debuginfo_cache__exit(void)
974 find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused,
975 struct perf_probe_point *pp __maybe_unused,
976 bool is_kprobe __maybe_unused)
981 static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
982 struct probe_trace_event **tevs __maybe_unused)
984 if (perf_probe_event_need_dwarf(pev)) {
985 pr_warning("Debuginfo-analysis is not supported.\n");
992 int show_line_range(struct line_range *lr __maybe_unused,
993 const char *module __maybe_unused,
994 bool user __maybe_unused)
996 pr_warning("Debuginfo-analysis is not supported.\n");
1000 int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
1001 int npevs __maybe_unused,
1002 struct strfilter *filter __maybe_unused)
1004 pr_warning("Debuginfo-analysis is not supported.\n");
1009 void line_range__clear(struct line_range *lr)
1015 intlist__delete(lr->line_list);
1016 memset(lr, 0, sizeof(*lr));
1019 int line_range__init(struct line_range *lr)
1021 memset(lr, 0, sizeof(*lr));
1022 lr->line_list = intlist__new(NULL);
1029 static int parse_line_num(char **ptr, int *val, const char *what)
1031 const char *start = *ptr;
1034 *val = strtol(*ptr, ptr, 0);
1035 if (errno || *ptr == start) {
1036 semantic_error("'%s' is not a valid number.\n", what);
1042 /* Check the name is good for event, group or function */
1043 static bool is_c_func_name(const char *name)
1045 if (!isalpha(*name) && *name != '_')
1047 while (*++name != '\0') {
1048 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
1055 * Stuff 'lr' according to the line range described by 'arg'.
1056 * The line range syntax is described by:
1058 * SRC[:SLN[+NUM|-ELN]]
1059 * FNC[@SRC][:SLN[+NUM|-ELN]]
1061 int parse_line_range_desc(const char *arg, struct line_range *lr)
1063 char *range, *file, *name = strdup(arg);
1072 range = strchr(name, ':');
1076 err = parse_line_num(&range, &lr->start, "start line");
1080 if (*range == '+' || *range == '-') {
1081 const char c = *range++;
1083 err = parse_line_num(&range, &lr->end, "end line");
1088 lr->end += lr->start;
1090 * Adjust the number of lines here.
1091 * If the number of lines == 1, the
1092 * the end of line should be equal to
1093 * the start of line.
1099 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
1102 if (lr->start > lr->end) {
1103 semantic_error("Start line must be smaller"
1104 " than end line.\n");
1107 if (*range != '\0') {
1108 semantic_error("Tailing with invalid str '%s'.\n", range);
1113 file = strchr(name, '@');
1116 lr->file = strdup(++file);
1117 if (lr->file == NULL) {
1121 lr->function = name;
1122 } else if (strchr(name, '/') || strchr(name, '.'))
1124 else if (is_c_func_name(name))/* We reuse it for checking funcname */
1125 lr->function = name;
1126 else { /* Invalid name */
1127 semantic_error("'%s' is not a valid function name.\n", name);
1138 /* Parse probepoint definition. */
1139 static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
1141 struct perf_probe_point *pp = &pev->point;
1144 bool file_spec = false;
1147 * perf probe [EVENT=]SRC[:LN|;PTN]
1148 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
1150 * TODO:Group name support
1155 ptr = strpbrk(arg, ";=@+%");
1156 if (ptr && *ptr == '=') { /* Event name */
1159 if (strchr(arg, ':')) {
1160 semantic_error("Group name is not supported yet.\n");
1163 if (!is_c_func_name(arg)) {
1164 semantic_error("%s is bad for event name -it must "
1165 "follow C symbol-naming rule.\n", arg);
1168 pev->event = strdup(arg);
1169 if (pev->event == NULL)
1176 * Check arg is function or file name and copy it.
1178 * We consider arg to be a file spec if and only if it satisfies
1179 * all of the below criteria::
1180 * - it does not include any of "+@%",
1181 * - it includes one of ":;", and
1182 * - it has a period '.' in the name.
1184 * Otherwise, we consider arg to be a function specification.
1186 if (!strpbrk(arg, "+@%") && (ptr = strpbrk(arg, ";:")) != NULL) {
1187 /* This is a file spec if it includes a '.' before ; or : */
1188 if (memchr(arg, '.', ptr - arg))
1192 ptr = strpbrk(arg, ";:+@%");
1212 * Keep pp->function even if this is absolute address,
1213 * so it can mark whether abs_address is valid.
1214 * Which make 'perf probe lib.bin 0x0' possible.
1216 * Note that checking length of tmp is not needed
1217 * because when we access tmp[1] we know tmp[0] is '0',
1218 * so tmp[1] should always valid (but could be '\0').
1220 if (tmp && !strncmp(tmp, "0x", 2)) {
1221 pp->abs_address = strtoul(pp->function, &tmp, 0);
1223 semantic_error("Invalid absolute address.\n");
1229 /* Parse other options */
1233 if (c == ';') { /* Lazy pattern must be the last part */
1234 pp->lazy_line = strdup(arg);
1235 if (pp->lazy_line == NULL)
1239 ptr = strpbrk(arg, ";:+@%");
1245 case ':': /* Line number */
1246 pp->line = strtoul(arg, &tmp, 0);
1248 semantic_error("There is non-digit char"
1249 " in line number.\n");
1253 case '+': /* Byte offset from a symbol */
1254 pp->offset = strtoul(arg, &tmp, 0);
1256 semantic_error("There is non-digit character"
1261 case '@': /* File name */
1263 semantic_error("SRC@SRC is not allowed.\n");
1266 pp->file = strdup(arg);
1267 if (pp->file == NULL)
1270 case '%': /* Probe places */
1271 if (strcmp(arg, "return") == 0) {
1273 } else { /* Others not supported yet */
1274 semantic_error("%%%s is not supported.\n", arg);
1278 default: /* Buggy case */
1279 pr_err("This program has a bug at %s:%d.\n",
1280 __FILE__, __LINE__);
1286 /* Exclusion check */
1287 if (pp->lazy_line && pp->line) {
1288 semantic_error("Lazy pattern can't be used with"
1293 if (pp->lazy_line && pp->offset) {
1294 semantic_error("Lazy pattern can't be used with offset.\n");
1298 if (pp->line && pp->offset) {
1299 semantic_error("Offset can't be used with line number.\n");
1303 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
1304 semantic_error("File always requires line number or "
1309 if (pp->offset && !pp->function) {
1310 semantic_error("Offset requires an entry function.\n");
1314 if (pp->retprobe && !pp->function) {
1315 semantic_error("Return probe requires an entry function.\n");
1319 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
1320 semantic_error("Offset/Line/Lazy pattern can't be used with "
1325 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
1326 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
1331 /* Parse perf-probe event argument */
1332 static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
1334 char *tmp, *goodname;
1335 struct perf_probe_arg_field **fieldp;
1337 pr_debug("parsing arg: %s into ", str);
1339 tmp = strchr(str, '=');
1341 arg->name = strndup(str, tmp - str);
1342 if (arg->name == NULL)
1344 pr_debug("name:%s ", arg->name);
1348 tmp = strchr(str, ':');
1349 if (tmp) { /* Type setting */
1351 arg->type = strdup(tmp + 1);
1352 if (arg->type == NULL)
1354 pr_debug("type:%s ", arg->type);
1357 tmp = strpbrk(str, "-.[");
1358 if (!is_c_varname(str) || !tmp) {
1359 /* A variable, register, symbol or special value */
1360 arg->var = strdup(str);
1361 if (arg->var == NULL)
1363 pr_debug("%s\n", arg->var);
1367 /* Structure fields or array element */
1368 arg->var = strndup(str, tmp - str);
1369 if (arg->var == NULL)
1371 goodname = arg->var;
1372 pr_debug("%s, ", arg->var);
1373 fieldp = &arg->field;
1376 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
1377 if (*fieldp == NULL)
1379 if (*tmp == '[') { /* Array */
1381 (*fieldp)->index = strtol(str + 1, &tmp, 0);
1382 (*fieldp)->ref = true;
1383 if (*tmp != ']' || tmp == str + 1) {
1384 semantic_error("Array index must be a"
1391 } else { /* Structure */
1394 (*fieldp)->ref = false;
1395 } else if (tmp[1] == '>') {
1397 (*fieldp)->ref = true;
1399 semantic_error("Argument parse error: %s\n",
1403 tmp = strpbrk(str, "-.[");
1406 (*fieldp)->name = strndup(str, tmp - str);
1407 if ((*fieldp)->name == NULL)
1410 goodname = (*fieldp)->name;
1411 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
1412 fieldp = &(*fieldp)->next;
1415 (*fieldp)->name = strdup(str);
1416 if ((*fieldp)->name == NULL)
1419 goodname = (*fieldp)->name;
1420 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
1422 /* If no name is specified, set the last field name (not array index)*/
1424 arg->name = strdup(goodname);
1425 if (arg->name == NULL)
1431 /* Parse perf-probe event command */
1432 int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
1435 int argc, i, ret = 0;
1437 argv = argv_split(cmd, &argc);
1439 pr_debug("Failed to split arguments.\n");
1442 if (argc - 1 > MAX_PROBE_ARGS) {
1443 semantic_error("Too many probe arguments (%d).\n", argc - 1);
1447 /* Parse probe point */
1448 ret = parse_perf_probe_point(argv[0], pev);
1452 /* Copy arguments and ensure return probe has no C argument */
1453 pev->nargs = argc - 1;
1454 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1455 if (pev->args == NULL) {
1459 for (i = 0; i < pev->nargs && ret >= 0; i++) {
1460 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1462 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
1463 semantic_error("You can't specify local variable for"
1474 /* Return true if this perf_probe_event requires debuginfo */
1475 bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
1479 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1482 for (i = 0; i < pev->nargs; i++)
1483 if (is_c_varname(pev->args[i].var))
1489 /* Parse probe_events event into struct probe_point */
1490 int parse_probe_trace_command(const char *cmd, struct probe_trace_event *tev)
1492 struct probe_trace_point *tp = &tev->point;
1495 char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
1499 pr_debug("Parsing probe_events: %s\n", cmd);
1500 argv = argv_split(cmd, &argc);
1502 pr_debug("Failed to split arguments.\n");
1506 semantic_error("Too few probe arguments.\n");
1511 /* Scan event and group name. */
1512 argv0_str = strdup(argv[0]);
1513 if (argv0_str == NULL) {
1517 fmt1_str = strtok_r(argv0_str, ":", &fmt);
1518 fmt2_str = strtok_r(NULL, "/", &fmt);
1519 fmt3_str = strtok_r(NULL, " \t", &fmt);
1520 if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
1521 || fmt3_str == NULL) {
1522 semantic_error("Failed to parse event name: %s\n", argv[0]);
1527 tev->group = strdup(fmt2_str);
1528 tev->event = strdup(fmt3_str);
1529 if (tev->group == NULL || tev->event == NULL) {
1533 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
1535 tp->retprobe = (pr == 'r');
1537 /* Scan module name(if there), function name and offset */
1538 p = strchr(argv[1], ':');
1540 tp->module = strndup(argv[1], p - argv[1]);
1544 fmt1_str = strtok_r(p, "+", &fmt);
1545 /* only the address started with 0x */
1546 if (fmt1_str[0] == '0') {
1548 * Fix a special case:
1549 * if address == 0, kernel reports something like:
1550 * p:probe_libc/abs_0 /lib/libc-2.18.so:0x (null) arg1=%ax
1551 * Newer kernel may fix that, but we want to
1552 * support old kernel also.
1554 if (strcmp(fmt1_str, "0x") == 0) {
1555 if (!argv[2] || strcmp(argv[2], "(null)")) {
1562 for (i = 2; argv[i + 1] != NULL; i++)
1563 argv[i] = argv[i + 1];
1568 tp->address = strtoul(fmt1_str, NULL, 0);
1570 /* Only the symbol-based probe has offset */
1571 tp->symbol = strdup(fmt1_str);
1572 if (tp->symbol == NULL) {
1576 fmt2_str = strtok_r(NULL, "", &fmt);
1577 if (fmt2_str == NULL)
1580 tp->offset = strtoul(fmt2_str, NULL, 10);
1583 tev->nargs = argc - 2;
1584 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
1585 if (tev->args == NULL) {
1589 for (i = 0; i < tev->nargs; i++) {
1590 p = strchr(argv[i + 2], '=');
1591 if (p) /* We don't need which register is assigned. */
1595 tev->args[i].name = strdup(argv[i + 2]);
1596 /* TODO: parse regs and offset */
1597 tev->args[i].value = strdup(p);
1598 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1610 /* Compose only probe arg */
1611 int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
1613 struct perf_probe_arg_field *field = pa->field;
1617 if (pa->name && pa->var)
1618 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
1620 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
1627 if (field->name[0] == '[')
1628 ret = e_snprintf(tmp, len, "%s", field->name);
1630 ret = e_snprintf(tmp, len, "%s%s",
1631 field->ref ? "->" : ".", field->name);
1636 field = field->next;
1640 ret = e_snprintf(tmp, len, ":%s", pa->type);
1649 pr_debug("Failed to synthesize perf probe argument: %d\n", ret);
1653 /* Compose only probe point (not argument) */
1654 static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
1657 char offs[32] = "", line[32] = "", file[32] = "";
1660 buf = zalloc(MAX_CMDLEN);
1666 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
1671 ret = e_snprintf(line, 32, ":%d", pp->line);
1679 tmp = strchr(pp->file + len - 30, '/');
1680 tmp = tmp ? tmp + 1 : pp->file + len - 30;
1682 ret = e_snprintf(file, 32, "@%s", tmp);
1688 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
1689 offs, pp->retprobe ? "%return" : "", line,
1692 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
1698 pr_debug("Failed to synthesize perf probe point: %d\n", ret);
1704 char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1709 buf = synthesize_perf_probe_point(&pev->point);
1714 for (i = 0; i < pev->nargs; i++) {
1715 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
1728 static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
1729 char **buf, size_t *buflen,
1734 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
1740 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
1752 static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
1753 char *buf, size_t buflen)
1755 struct probe_trace_arg_ref *ref = arg->ref;
1759 /* Argument name or separator */
1761 ret = e_snprintf(buf, buflen, " %s=", arg->name);
1763 ret = e_snprintf(buf, buflen, " ");
1769 /* Special case: @XXX */
1770 if (arg->value[0] == '@' && arg->ref)
1773 /* Dereferencing arguments */
1775 depth = __synthesize_probe_trace_arg_ref(ref, &buf,
1781 /* Print argument value */
1782 if (arg->value[0] == '@' && arg->ref)
1783 ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
1786 ret = e_snprintf(buf, buflen, "%s", arg->value);
1794 ret = e_snprintf(buf, buflen, ")");
1800 /* Print argument type */
1802 ret = e_snprintf(buf, buflen, ":%s", arg->type);
1811 char *synthesize_probe_trace_command(struct probe_trace_event *tev)
1813 struct probe_trace_point *tp = &tev->point;
1817 buf = zalloc(MAX_CMDLEN);
1821 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
1822 tev->group, tev->event);
1826 /* Uprobes must have tp->module */
1827 if (tev->uprobes && !tp->module)
1830 * If tp->address == 0, then this point must be a
1831 * absolute address uprobe.
1832 * try_to_find_absolute_address() should have made
1833 * tp->symbol to "0x0".
1835 if (tev->uprobes && !tp->address) {
1836 if (!tp->symbol || strcmp(tp->symbol, "0x0"))
1840 /* Use the tp->address for uprobes */
1842 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s:0x%lx",
1843 tp->module, tp->address);
1844 else if (!strncmp(tp->symbol, "0x", 2))
1845 /* Absolute address. See try_to_find_absolute_address() */
1846 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s%s0x%lx",
1847 tp->module ?: "", tp->module ? ":" : "",
1850 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s%s%s+%lu",
1851 tp->module ?: "", tp->module ? ":" : "",
1852 tp->symbol, tp->offset);
1858 for (i = 0; i < tev->nargs; i++) {
1859 ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
1872 static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
1873 struct perf_probe_point *pp,
1876 struct symbol *sym = NULL;
1878 u64 addr = tp->address;
1882 map = dso__new_map(tp->module);
1885 sym = map__find_symbol(map, addr, NULL);
1888 addr = kernel_get_symbol_address_by_name(tp->symbol, true);
1891 sym = __find_kernel_function(addr, &map);
1897 pp->retprobe = tp->retprobe;
1898 pp->offset = addr - map->unmap_ip(map, sym->start);
1899 pp->function = strdup(sym->name);
1900 ret = pp->function ? 0 : -ENOMEM;
1903 if (map && !is_kprobe) {
1910 static int convert_to_perf_probe_point(struct probe_trace_point *tp,
1911 struct perf_probe_point *pp,
1917 ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe);
1920 ret = find_perf_probe_point_from_map(tp, pp, is_kprobe);
1924 pr_debug("Failed to find probe point from both of dwarf and map.\n");
1927 pp->function = strdup(tp->symbol);
1928 pp->offset = tp->offset;
1930 ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address);
1933 pp->function = strdup(buf);
1936 if (pp->function == NULL)
1939 pp->retprobe = tp->retprobe;
1944 static int convert_to_perf_probe_event(struct probe_trace_event *tev,
1945 struct perf_probe_event *pev, bool is_kprobe)
1950 /* Convert event/group name */
1951 pev->event = strdup(tev->event);
1952 pev->group = strdup(tev->group);
1953 if (pev->event == NULL || pev->group == NULL)
1956 /* Convert trace_point to probe_point */
1957 ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
1961 /* Convert trace_arg to probe_arg */
1962 pev->nargs = tev->nargs;
1963 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1964 if (pev->args == NULL)
1966 for (i = 0; i < tev->nargs && ret >= 0; i++) {
1967 if (tev->args[i].name)
1968 pev->args[i].name = strdup(tev->args[i].name);
1970 ret = synthesize_probe_trace_arg(&tev->args[i],
1972 pev->args[i].name = strdup(buf);
1974 if (pev->args[i].name == NULL && ret >= 0)
1979 clear_perf_probe_event(pev);
1984 void clear_perf_probe_event(struct perf_probe_event *pev)
1986 struct perf_probe_arg_field *field, *next;
1992 clear_perf_probe_point(&pev->point);
1994 for (i = 0; i < pev->nargs; i++) {
1995 free(pev->args[i].name);
1996 free(pev->args[i].var);
1997 free(pev->args[i].type);
1998 field = pev->args[i].field;
2001 zfree(&field->name);
2007 memset(pev, 0, sizeof(*pev));
2010 void clear_probe_trace_event(struct probe_trace_event *tev)
2012 struct probe_trace_arg_ref *ref, *next;
2017 free(tev->point.symbol);
2018 free(tev->point.realname);
2019 free(tev->point.module);
2020 for (i = 0; i < tev->nargs; i++) {
2021 free(tev->args[i].name);
2022 free(tev->args[i].value);
2023 free(tev->args[i].type);
2024 ref = tev->args[i].ref;
2032 memset(tev, 0, sizeof(*tev));
2035 struct kprobe_blacklist_node {
2036 struct list_head list;
2037 unsigned long start;
2042 static void kprobe_blacklist__delete(struct list_head *blacklist)
2044 struct kprobe_blacklist_node *node;
2046 while (!list_empty(blacklist)) {
2047 node = list_first_entry(blacklist,
2048 struct kprobe_blacklist_node, list);
2049 list_del(&node->list);
2055 static int kprobe_blacklist__load(struct list_head *blacklist)
2057 struct kprobe_blacklist_node *node;
2058 const char *__debugfs = debugfs_find_mountpoint();
2059 char buf[PATH_MAX], *p;
2063 if (__debugfs == NULL)
2066 ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs);
2070 fp = fopen(buf, "r");
2075 while (fgets(buf, PATH_MAX, fp)) {
2076 node = zalloc(sizeof(*node));
2081 INIT_LIST_HEAD(&node->list);
2082 list_add_tail(&node->list, blacklist);
2083 if (sscanf(buf, "0x%lx-0x%lx", &node->start, &node->end) != 2) {
2087 p = strchr(buf, '\t');
2090 if (p[strlen(p) - 1] == '\n')
2091 p[strlen(p) - 1] = '\0';
2093 p = (char *)"unknown";
2094 node->symbol = strdup(p);
2095 if (!node->symbol) {
2099 pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
2100 node->start, node->end, node->symbol);
2104 kprobe_blacklist__delete(blacklist);
2110 static struct kprobe_blacklist_node *
2111 kprobe_blacklist__find_by_address(struct list_head *blacklist,
2112 unsigned long address)
2114 struct kprobe_blacklist_node *node;
2116 list_for_each_entry(node, blacklist, list) {
2117 if (node->start <= address && address <= node->end)
2124 static LIST_HEAD(kprobe_blacklist);
2126 static void kprobe_blacklist__init(void)
2128 if (!list_empty(&kprobe_blacklist))
2131 if (kprobe_blacklist__load(&kprobe_blacklist) < 0)
2132 pr_debug("No kprobe blacklist support, ignored\n");
2135 static void kprobe_blacklist__release(void)
2137 kprobe_blacklist__delete(&kprobe_blacklist);
2140 static bool kprobe_blacklist__listed(unsigned long address)
2142 return !!kprobe_blacklist__find_by_address(&kprobe_blacklist, address);
2145 static int perf_probe_event__sprintf(const char *group, const char *event,
2146 struct perf_probe_event *pev,
2148 struct strbuf *result)
2154 /* Synthesize only event probe point */
2155 place = synthesize_perf_probe_point(&pev->point);
2159 ret = e_snprintf(buf, 128, "%s:%s", group, event);
2163 strbuf_addf(result, " %-20s (on %s", buf, place);
2165 strbuf_addf(result, " in %s", module);
2167 if (pev->nargs > 0) {
2168 strbuf_addstr(result, " with");
2169 for (i = 0; i < pev->nargs; i++) {
2170 ret = synthesize_perf_probe_arg(&pev->args[i],
2174 strbuf_addf(result, " %s", buf);
2177 strbuf_addch(result, ')');
2184 static int show_perf_probe_event(const char *group, const char *event,
2185 struct perf_probe_event *pev,
2186 const char *module, bool use_stdout)
2188 struct strbuf buf = STRBUF_INIT;
2191 ret = perf_probe_event__sprintf(group, event, pev, module, &buf);
2194 printf("%s\n", buf.buf);
2196 pr_info("%s\n", buf.buf);
2198 strbuf_release(&buf);
2203 static bool filter_probe_trace_event(struct probe_trace_event *tev,
2204 struct strfilter *filter)
2208 /* At first, check the event name itself */
2209 if (strfilter__compare(filter, tev->event))
2212 /* Next, check the combination of name and group */
2213 if (e_snprintf(tmp, 128, "%s:%s", tev->group, tev->event) < 0)
2215 return strfilter__compare(filter, tmp);
2218 static int __show_perf_probe_events(int fd, bool is_kprobe,
2219 struct strfilter *filter)
2222 struct probe_trace_event tev;
2223 struct perf_probe_event pev;
2224 struct strlist *rawlist;
2225 struct str_node *ent;
2227 memset(&tev, 0, sizeof(tev));
2228 memset(&pev, 0, sizeof(pev));
2230 rawlist = probe_file__get_rawlist(fd);
2234 strlist__for_each(ent, rawlist) {
2235 ret = parse_probe_trace_command(ent->s, &tev);
2237 if (!filter_probe_trace_event(&tev, filter))
2239 ret = convert_to_perf_probe_event(&tev, &pev,
2243 ret = show_perf_probe_event(pev.group, pev.event,
2244 &pev, tev.point.module,
2248 clear_perf_probe_event(&pev);
2249 clear_probe_trace_event(&tev);
2253 strlist__delete(rawlist);
2254 /* Cleanup cached debuginfo if needed */
2255 debuginfo_cache__exit();
2260 /* List up current perf-probe events */
2261 int show_perf_probe_events(struct strfilter *filter)
2263 int kp_fd, up_fd, ret;
2267 ret = init_symbol_maps(false);
2271 ret = probe_file__open_both(&kp_fd, &up_fd, 0);
2276 ret = __show_perf_probe_events(kp_fd, true, filter);
2277 if (up_fd >= 0 && ret >= 0)
2278 ret = __show_perf_probe_events(up_fd, false, filter);
2288 static int get_new_event_name(char *buf, size_t len, const char *base,
2289 struct strlist *namelist, bool allow_suffix)
2298 ret = e_snprintf(buf, len, "%s", base);
2300 pr_debug("snprintf() failed: %d\n", ret);
2303 /* Cut off the postfixes (e.g. .const, .isra)*/
2304 p = strchr(buf, '.');
2307 if (!strlist__has_entry(namelist, buf))
2310 if (!allow_suffix) {
2311 pr_warning("Error: event \"%s\" already exists. "
2312 "(Use -f to force duplicates.)\n", base);
2316 /* Try to add suffix */
2317 for (i = 1; i < MAX_EVENT_INDEX; i++) {
2318 ret = e_snprintf(buf, len, "%s_%d", base, i);
2320 pr_debug("snprintf() failed: %d\n", ret);
2323 if (!strlist__has_entry(namelist, buf))
2326 if (i == MAX_EVENT_INDEX) {
2327 pr_warning("Too many events are on the same function.\n");
2334 /* Warn if the current kernel's uprobe implementation is old */
2335 static void warn_uprobe_event_compat(struct probe_trace_event *tev)
2338 char *buf = synthesize_probe_trace_command(tev);
2340 /* Old uprobe event doesn't support memory dereference */
2341 if (!tev->uprobes || tev->nargs == 0 || !buf)
2344 for (i = 0; i < tev->nargs; i++)
2345 if (strglobmatch(tev->args[i].value, "[$@+-]*")) {
2346 pr_warning("Please upgrade your kernel to at least "
2347 "3.14 to have access to feature %s\n",
2348 tev->args[i].value);
2355 /* Set new name from original perf_probe_event and namelist */
2356 static int probe_trace_event__set_name(struct probe_trace_event *tev,
2357 struct perf_probe_event *pev,
2358 struct strlist *namelist,
2361 const char *event, *group;
2368 if (pev->point.function &&
2369 (strncmp(pev->point.function, "0x", 2) != 0) &&
2370 !strisglob(pev->point.function))
2371 event = pev->point.function;
2373 event = tev->point.realname;
2377 group = PERFPROBE_GROUP;
2379 /* Get an unused new event name */
2380 ret = get_new_event_name(buf, 64, event,
2381 namelist, allow_suffix);
2387 tev->event = strdup(event);
2388 tev->group = strdup(group);
2389 if (tev->event == NULL || tev->group == NULL)
2392 /* Add added event name to namelist */
2393 strlist__add(namelist, event);
2397 static int __add_probe_trace_events(struct perf_probe_event *pev,
2398 struct probe_trace_event *tevs,
2399 int ntevs, bool allow_suffix)
2402 struct probe_trace_event *tev = NULL;
2403 const char *event = NULL, *group = NULL;
2404 struct strlist *namelist;
2406 fd = probe_file__open(PF_FL_RW | (pev->uprobes ? PF_FL_UPROBE : 0));
2410 /* Get current event names */
2411 namelist = probe_file__get_namelist(fd);
2413 pr_debug("Failed to get current event list.\n");
2419 pr_info("Added new event%s\n", (ntevs > 1) ? "s:" : ":");
2420 for (i = 0; i < ntevs; i++) {
2422 /* Skip if the symbol is out of .text or blacklisted */
2423 if (!tev->point.symbol)
2426 /* Set new name for tev (and update namelist) */
2427 ret = probe_trace_event__set_name(tev, pev, namelist,
2432 ret = probe_file__add_event(fd, tev);
2436 /* We use tev's name for showing new events */
2437 show_perf_probe_event(tev->group, tev->event, pev,
2438 tev->point.module, false);
2439 /* Save the last valid name */
2444 * Probes after the first probe which comes from same
2445 * user input are always allowed to add suffix, because
2446 * there might be several addresses corresponding to
2449 allow_suffix = true;
2451 if (ret == -EINVAL && pev->uprobes)
2452 warn_uprobe_event_compat(tev);
2454 /* Note that it is possible to skip all events because of blacklist */
2455 if (ret >= 0 && event) {
2456 /* Show how to use the event. */
2457 pr_info("\nYou can now use it in all perf tools, such as:\n\n");
2458 pr_info("\tperf record -e %s:%s -aR sleep 1\n\n", group, event);
2461 strlist__delete(namelist);
2467 static int find_probe_functions(struct map *map, char *name,
2468 struct symbol **syms)
2472 struct rb_node *tmp;
2474 if (map__load(map, NULL) < 0)
2477 map__for_each_symbol(map, sym, tmp) {
2478 if (strglobmatch(sym->name, name)) {
2480 if (syms && found < probe_conf.max_probes)
2481 syms[found - 1] = sym;
2488 #define strdup_or_goto(str, label) \
2489 ({ char *__p = strdup(str); if (!__p) goto label; __p; })
2491 void __weak arch__fix_tev_from_maps(struct perf_probe_event *pev __maybe_unused,
2492 struct probe_trace_event *tev __maybe_unused,
2493 struct map *map __maybe_unused) { }
2496 * Find probe function addresses from map.
2497 * Return an error or the number of found probe_trace_event
2499 static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
2500 struct probe_trace_event **tevs)
2502 struct map *map = NULL;
2503 struct ref_reloc_sym *reloc_sym = NULL;
2505 struct symbol **syms = NULL;
2506 struct probe_trace_event *tev;
2507 struct perf_probe_point *pp = &pev->point;
2508 struct probe_trace_point *tp;
2509 int num_matched_functions;
2510 int ret, i, j, skipped = 0;
2512 map = get_target_map(pev->target, pev->uprobes);
2518 syms = malloc(sizeof(struct symbol *) * probe_conf.max_probes);
2525 * Load matched symbols: Since the different local symbols may have
2526 * same name but different addresses, this lists all the symbols.
2528 num_matched_functions = find_probe_functions(map, pp->function, syms);
2529 if (num_matched_functions == 0) {
2530 pr_err("Failed to find symbol %s in %s\n", pp->function,
2531 pev->target ? : "kernel");
2534 } else if (num_matched_functions > probe_conf.max_probes) {
2535 pr_err("Too many functions matched in %s\n",
2536 pev->target ? : "kernel");
2541 if (!pev->uprobes && !pp->retprobe) {
2542 reloc_sym = kernel_get_ref_reloc_sym();
2544 pr_warning("Relocated base symbol is not found!\n");
2550 /* Setup result trace-probe-events */
2551 *tevs = zalloc(sizeof(*tev) * num_matched_functions);
2559 for (j = 0; j < num_matched_functions; j++) {
2562 tev = (*tevs) + ret;
2564 if (ret == num_matched_functions) {
2565 pr_warning("Too many symbols are listed. Skip it.\n");
2570 if (pp->offset > sym->end - sym->start) {
2571 pr_warning("Offset %ld is bigger than the size of %s\n",
2572 pp->offset, sym->name);
2576 /* Add one probe point */
2577 tp->address = map->unmap_ip(map, sym->start) + pp->offset;
2578 /* If we found a wrong one, mark it by NULL symbol */
2579 if (!pev->uprobes &&
2580 kprobe_warn_out_range(sym->name, tp->address)) {
2581 tp->symbol = NULL; /* Skip it */
2583 } else if (reloc_sym) {
2584 tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
2585 tp->offset = tp->address - reloc_sym->addr;
2587 tp->symbol = strdup_or_goto(sym->name, nomem_out);
2588 tp->offset = pp->offset;
2590 tp->realname = strdup_or_goto(sym->name, nomem_out);
2592 tp->retprobe = pp->retprobe;
2594 tev->point.module = strdup_or_goto(pev->target,
2596 tev->uprobes = pev->uprobes;
2597 tev->nargs = pev->nargs;
2599 tev->args = zalloc(sizeof(struct probe_trace_arg) *
2601 if (tev->args == NULL)
2604 for (i = 0; i < tev->nargs; i++) {
2605 if (pev->args[i].name)
2607 strdup_or_goto(pev->args[i].name,
2610 tev->args[i].value = strdup_or_goto(pev->args[i].var,
2612 if (pev->args[i].type)
2614 strdup_or_goto(pev->args[i].type,
2617 arch__fix_tev_from_maps(pev, tev, map);
2619 if (ret == skipped) {
2625 put_target_map(map, pev->uprobes);
2632 clear_probe_trace_events(*tevs, num_matched_functions);
2637 static int try_to_find_absolute_address(struct perf_probe_event *pev,
2638 struct probe_trace_event **tevs)
2640 struct perf_probe_point *pp = &pev->point;
2641 struct probe_trace_event *tev;
2642 struct probe_trace_point *tp;
2645 if (!(pev->point.function && !strncmp(pev->point.function, "0x", 2)))
2647 if (perf_probe_event_need_dwarf(pev))
2651 * This is 'perf probe /lib/libc.so 0xabcd'. Try to probe at
2654 * Only one tev can be generated by this.
2656 *tevs = zalloc(sizeof(*tev));
2664 * Don't use tp->offset, use address directly, because
2665 * in synthesize_probe_trace_command() address cannot be
2668 tp->address = pev->point.abs_address;
2669 tp->retprobe = pp->retprobe;
2670 tev->uprobes = pev->uprobes;
2674 * Give it a '0x' leading symbol name.
2675 * In __add_probe_trace_events, a NULL symbol is interpreted as
2678 if (asprintf(&tp->symbol, "0x%lx", tp->address) < 0)
2681 /* For kprobe, check range */
2682 if ((!tev->uprobes) &&
2683 (kprobe_warn_out_range(tev->point.symbol,
2684 tev->point.address))) {
2689 if (asprintf(&tp->realname, "abs_%lx", tp->address) < 0)
2693 tp->module = strdup(pev->target);
2699 tev->group = strdup(pev->group);
2705 tev->event = strdup(pev->event);
2710 tev->nargs = pev->nargs;
2711 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
2716 for (i = 0; i < tev->nargs; i++)
2717 copy_to_probe_trace_arg(&tev->args[i], &pev->args[i]);
2723 clear_probe_trace_events(*tevs, 1);
2729 bool __weak arch__prefers_symtab(void) { return false; }
2731 static int convert_to_probe_trace_events(struct perf_probe_event *pev,
2732 struct probe_trace_event **tevs)
2736 if (pev->uprobes && !pev->group) {
2737 /* Replace group name if not given */
2738 ret = convert_exec_to_group(pev->target, &pev->group);
2740 pr_warning("Failed to make a group name.\n");
2745 ret = try_to_find_absolute_address(pev, tevs);
2749 if (arch__prefers_symtab() && !perf_probe_event_need_dwarf(pev)) {
2750 ret = find_probe_trace_events_from_map(pev, tevs);
2752 return ret; /* Found in symbol table */
2755 /* Convert perf_probe_event with debuginfo */
2756 ret = try_to_find_probe_trace_events(pev, tevs);
2758 return ret; /* Found in debuginfo or got an error */
2760 return find_probe_trace_events_from_map(pev, tevs);
2763 struct __event_package {
2764 struct perf_probe_event *pev;
2765 struct probe_trace_event *tevs;
2769 int add_perf_probe_events(struct perf_probe_event *pevs, int npevs)
2772 struct __event_package *pkgs;
2775 pkgs = zalloc(sizeof(struct __event_package) * npevs);
2780 ret = init_symbol_maps(pevs->uprobes);
2786 /* Loop 1: convert all events */
2787 for (i = 0; i < npevs; i++) {
2788 pkgs[i].pev = &pevs[i];
2789 /* Init kprobe blacklist if needed */
2790 if (!pkgs[i].pev->uprobes)
2791 kprobe_blacklist__init();
2792 /* Convert with or without debuginfo */
2793 ret = convert_to_probe_trace_events(pkgs[i].pev,
2797 pkgs[i].ntevs = ret;
2799 /* This just release blacklist only if allocated */
2800 kprobe_blacklist__release();
2802 /* Loop 2: add all events */
2803 for (i = 0; i < npevs; i++) {
2804 ret = __add_probe_trace_events(pkgs[i].pev, pkgs[i].tevs,
2806 probe_conf.force_add);
2811 /* Loop 3: cleanup and free trace events */
2812 for (i = 0; i < npevs; i++) {
2813 for (j = 0; j < pkgs[i].ntevs; j++)
2814 clear_probe_trace_event(&pkgs[i].tevs[j]);
2815 zfree(&pkgs[i].tevs);
2823 int del_perf_probe_events(struct strfilter *filter)
2825 int ret, ret2, ufd = -1, kfd = -1;
2826 char *str = strfilter__string(filter);
2831 pr_debug("Delete filter: \'%s\'\n", str);
2833 /* Get current event names */
2834 ret = probe_file__open_both(&kfd, &ufd, PF_FL_RW);
2838 ret = probe_file__del_events(kfd, filter);
2839 if (ret < 0 && ret != -ENOENT)
2842 ret2 = probe_file__del_events(ufd, filter);
2843 if (ret2 < 0 && ret2 != -ENOENT) {
2847 if (ret == -ENOENT && ret2 == -ENOENT)
2848 pr_debug("\"%s\" does not hit any event.\n", str);
2849 /* Note that this is silently ignored */
2863 /* TODO: don't use a global variable for filter ... */
2864 static struct strfilter *available_func_filter;
2867 * If a symbol corresponds to a function with global binding and
2868 * matches filter return 0. For all others return 1.
2870 static int filter_available_functions(struct map *map __maybe_unused,
2873 if (strfilter__compare(available_func_filter, sym->name))
2878 int show_available_funcs(const char *target, struct strfilter *_filter,
2884 ret = init_symbol_maps(user);
2888 /* Get a symbol map */
2890 map = dso__new_map(target);
2892 map = kernel_get_module_map(target);
2894 pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
2898 /* Load symbols with given filter */
2899 available_func_filter = _filter;
2900 if (map__load(map, filter_available_functions)) {
2901 pr_err("Failed to load symbols in %s\n", (target) ? : "kernel");
2904 if (!dso__sorted_by_name(map->dso, map->type))
2905 dso__sort_by_name(map->dso, map->type);
2907 /* Show all (filtered) symbols */
2909 dso__fprintf_symbols_by_name(map->dso, map->type, stdout);
2919 int copy_to_probe_trace_arg(struct probe_trace_arg *tvar,
2920 struct perf_probe_arg *pvar)
2922 tvar->value = strdup(pvar->var);
2923 if (tvar->value == NULL)
2926 tvar->type = strdup(pvar->type);
2927 if (tvar->type == NULL)
2931 tvar->name = strdup(pvar->name);
2932 if (tvar->name == NULL)