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 list_for_each_entry(dso, &host_machine->dsos.head, node) {
276 if (strncmp(dso->short_name + 1, module,
277 dso->short_name_len - 2) == 0)
280 pr_debug("Failed to find module %s.\n", module);
284 map = host_machine->vmlinux_maps[MAP__FUNCTION];
287 vmlinux_name = symbol_conf.vmlinux_name;
290 ret = dso__load_vmlinux(dso, map, vmlinux_name, false, NULL);
292 ret = dso__load_vmlinux_path(dso, map, NULL);
299 * Some binaries like glibc have special symbols which are on the symbol
300 * table, but not in the debuginfo. If we can find the address of the
301 * symbol from map, we can translate the address back to the probe point.
303 static int find_alternative_probe_point(struct debuginfo *dinfo,
304 struct perf_probe_point *pp,
305 struct perf_probe_point *result,
306 const char *target, bool uprobes)
308 struct map *map = NULL;
313 /* This can work only for function-name based one */
314 if (!pp->function || pp->file)
317 map = get_target_map(target, uprobes);
321 /* Find the address of given function */
322 map__for_each_symbol_by_name(map, pp->function, sym) {
324 address = sym->start;
326 address = map->unmap_ip(map, sym->start);
333 pr_debug("Symbol %s address found : %" PRIx64 "\n",
334 pp->function, address);
336 ret = debuginfo__find_probe_point(dinfo, (unsigned long)address,
339 ret = (!ret) ? -ENOENT : ret;
341 result->offset += pp->offset;
342 result->line += pp->line;
343 result->retprobe = pp->retprobe;
348 put_target_map(map, uprobes);
353 static int get_alternative_probe_event(struct debuginfo *dinfo,
354 struct perf_probe_event *pev,
355 struct perf_probe_point *tmp)
359 memcpy(tmp, &pev->point, sizeof(*tmp));
360 memset(&pev->point, 0, sizeof(pev->point));
361 ret = find_alternative_probe_point(dinfo, tmp, &pev->point,
362 pev->target, pev->uprobes);
364 memcpy(&pev->point, tmp, sizeof(*tmp));
369 static int get_alternative_line_range(struct debuginfo *dinfo,
370 struct line_range *lr,
371 const char *target, bool user)
373 struct perf_probe_point pp = { .function = lr->function,
376 struct perf_probe_point result;
379 memset(&result, 0, sizeof(result));
381 if (lr->end != INT_MAX)
382 len = lr->end - lr->start;
383 ret = find_alternative_probe_point(dinfo, &pp, &result,
386 lr->function = result.function;
387 lr->file = result.file;
388 lr->start = result.line;
389 if (lr->end != INT_MAX)
390 lr->end = lr->start + len;
391 clear_perf_probe_point(&pp);
396 /* Open new debuginfo of given module */
397 static struct debuginfo *open_debuginfo(const char *module, bool silent)
399 const char *path = module;
400 char reason[STRERR_BUFSIZE];
401 struct debuginfo *ret = NULL;
402 struct dso *dso = NULL;
405 if (!module || !strchr(module, '/')) {
406 err = kernel_get_module_dso(module, &dso);
408 if (!dso || dso->load_errno == 0) {
409 if (!strerror_r(-err, reason, STRERR_BUFSIZE))
410 strcpy(reason, "(unknown)");
412 dso__strerror_load(dso, reason, STRERR_BUFSIZE);
414 pr_err("Failed to find the path for %s: %s\n",
415 module ?: "kernel", reason);
418 path = dso->long_name;
420 ret = debuginfo__new(path);
421 if (!ret && !silent) {
422 pr_warning("The %s file has no debug information.\n", path);
423 if (!module || !strtailcmp(path, ".ko"))
424 pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, ");
426 pr_warning("Rebuild with -g, ");
427 pr_warning("or install an appropriate debuginfo package.\n");
432 /* For caching the last debuginfo */
433 static struct debuginfo *debuginfo_cache;
434 static char *debuginfo_cache_path;
436 static struct debuginfo *debuginfo_cache__open(const char *module, bool silent)
438 if ((debuginfo_cache_path && !strcmp(debuginfo_cache_path, module)) ||
439 (!debuginfo_cache_path && !module && debuginfo_cache))
442 /* Copy module path */
443 free(debuginfo_cache_path);
445 debuginfo_cache_path = strdup(module);
446 if (!debuginfo_cache_path) {
447 debuginfo__delete(debuginfo_cache);
448 debuginfo_cache = NULL;
453 debuginfo_cache = open_debuginfo(module, silent);
454 if (!debuginfo_cache)
455 zfree(&debuginfo_cache_path);
457 return debuginfo_cache;
460 static void debuginfo_cache__exit(void)
462 debuginfo__delete(debuginfo_cache);
463 debuginfo_cache = NULL;
464 zfree(&debuginfo_cache_path);
468 static int get_text_start_address(const char *exec, unsigned long *address)
473 int fd, ret = -ENOENT;
475 fd = open(exec, O_RDONLY);
479 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
483 if (gelf_getehdr(elf, &ehdr) == NULL)
486 if (!elf_section_by_name(elf, &ehdr, &shdr, ".text", NULL))
489 *address = shdr.sh_addr - shdr.sh_offset;
497 * Convert trace point to probe point with debuginfo
499 static int find_perf_probe_point_from_dwarf(struct probe_trace_point *tp,
500 struct perf_probe_point *pp,
503 struct debuginfo *dinfo = NULL;
504 unsigned long stext = 0;
505 u64 addr = tp->address;
508 /* convert the address to dwarf address */
514 ret = get_text_start_address(tp->module, &stext);
518 } else if (tp->symbol) {
519 addr = kernel_get_symbol_address_by_name(tp->symbol, false);
525 pr_debug("try to find information at %" PRIx64 " in %s\n", addr,
526 tp->module ? : "kernel");
528 dinfo = debuginfo_cache__open(tp->module, verbose == 0);
530 ret = debuginfo__find_probe_point(dinfo,
531 (unsigned long)addr, pp);
536 pp->retprobe = tp->retprobe;
540 pr_debug("Failed to find corresponding probes from debuginfo.\n");
541 return ret ? : -ENOENT;
544 static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs,
545 int ntevs, const char *exec)
548 unsigned long stext = 0;
553 ret = get_text_start_address(exec, &stext);
557 for (i = 0; i < ntevs && ret >= 0; i++) {
558 /* point.address is the addres of point.symbol + point.offset */
559 tevs[i].point.address -= stext;
560 tevs[i].point.module = strdup(exec);
561 if (!tevs[i].point.module) {
565 tevs[i].uprobes = true;
571 static int add_module_to_probe_trace_events(struct probe_trace_event *tevs,
572 int ntevs, const char *module)
580 tmp = strrchr(module, '/');
582 /* This is a module path -- get the module name */
583 module = strdup(tmp + 1);
586 tmp = strchr(module, '.');
589 tmp = (char *)module; /* For free() */
592 for (i = 0; i < ntevs; i++) {
593 tevs[i].point.module = strdup(module);
594 if (!tevs[i].point.module) {
604 /* Post processing the probe events */
605 static int post_process_probe_trace_events(struct probe_trace_event *tevs,
606 int ntevs, const char *module,
609 struct ref_reloc_sym *reloc_sym;
614 return add_exec_to_probe_trace_events(tevs, ntevs, module);
616 /* Note that currently ref_reloc_sym based probe is not for drivers */
618 return add_module_to_probe_trace_events(tevs, ntevs, module);
620 reloc_sym = kernel_get_ref_reloc_sym();
622 pr_warning("Relocated base symbol is not found!\n");
626 for (i = 0; i < ntevs; i++) {
627 if (!tevs[i].point.address || tevs[i].point.retprobe)
629 /* If we found a wrong one, mark it by NULL symbol */
630 if (kprobe_warn_out_range(tevs[i].point.symbol,
631 tevs[i].point.address)) {
635 tmp = strdup(reloc_sym->name);
639 /* If we have no realname, use symbol for it */
640 if (!tevs[i].point.realname)
641 tevs[i].point.realname = tevs[i].point.symbol;
643 free(tevs[i].point.symbol);
644 tevs[i].point.symbol = tmp;
645 tevs[i].point.offset = tevs[i].point.address -
646 reloc_sym->unrelocated_addr;
651 /* Try to find perf_probe_event with debuginfo */
652 static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
653 struct probe_trace_event **tevs)
655 bool need_dwarf = perf_probe_event_need_dwarf(pev);
656 struct perf_probe_point tmp;
657 struct debuginfo *dinfo;
660 dinfo = open_debuginfo(pev->target, !need_dwarf);
664 pr_debug("Could not open debuginfo. Try to use symbols.\n");
668 pr_debug("Try to find probe point from debuginfo.\n");
669 /* Searching trace events corresponding to a probe event */
670 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
672 if (ntevs == 0) { /* Not found, retry with an alternative */
673 ret = get_alternative_probe_event(dinfo, pev, &tmp);
675 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
677 * Write back to the original probe_event for
678 * setting appropriate (user given) event name
680 clear_perf_probe_point(&pev->point);
681 memcpy(&pev->point, &tmp, sizeof(tmp));
685 debuginfo__delete(dinfo);
687 if (ntevs > 0) { /* Succeeded to find trace events */
688 pr_debug("Found %d probe_trace_events.\n", ntevs);
689 ret = post_process_probe_trace_events(*tevs, ntevs,
690 pev->target, pev->uprobes);
691 if (ret < 0 || ret == ntevs) {
692 clear_probe_trace_events(*tevs, ntevs);
696 return ret < 0 ? ret : ntevs;
701 if (ntevs == 0) { /* No error but failed to find probe point. */
702 pr_warning("Probe point '%s' not found.\n",
703 synthesize_perf_probe_point(&pev->point));
706 /* Error path : ntevs < 0 */
707 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
710 pr_warning("Warning: No dwarf info found in the vmlinux - "
711 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
713 pr_debug("Trying to use symbols.\n");
720 #define LINEBUF_SIZE 256
721 #define NR_ADDITIONAL_LINES 2
723 static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
725 char buf[LINEBUF_SIZE], sbuf[STRERR_BUFSIZE];
726 const char *color = show_num ? "" : PERF_COLOR_BLUE;
727 const char *prefix = NULL;
730 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
735 prefix = show_num ? "%7d " : " ";
736 color_fprintf(stdout, color, prefix, l);
738 color_fprintf(stdout, color, "%s", buf);
740 } while (strchr(buf, '\n') == NULL);
745 pr_warning("File read error: %s\n",
746 strerror_r(errno, sbuf, sizeof(sbuf)));
752 static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
754 int rv = __show_one_line(fp, l, skip, show_num);
756 pr_warning("Source file is shorter than expected.\n");
762 #define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
763 #define show_one_line(f,l) _show_one_line(f,l,false,false)
764 #define skip_one_line(f,l) _show_one_line(f,l,true,false)
765 #define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
768 * Show line-range always requires debuginfo to find source file and
771 static int __show_line_range(struct line_range *lr, const char *module,
776 struct debuginfo *dinfo;
780 char sbuf[STRERR_BUFSIZE];
782 /* Search a line range */
783 dinfo = open_debuginfo(module, false);
787 ret = debuginfo__find_line_range(dinfo, lr);
788 if (!ret) { /* Not found, retry with an alternative */
789 ret = get_alternative_line_range(dinfo, lr, module, user);
791 ret = debuginfo__find_line_range(dinfo, lr);
793 debuginfo__delete(dinfo);
794 if (ret == 0 || ret == -ENOENT) {
795 pr_warning("Specified source line is not found.\n");
797 } else if (ret < 0) {
798 pr_warning("Debuginfo analysis failed.\n");
802 /* Convert source file path */
804 ret = get_real_path(tmp, lr->comp_dir, &lr->path);
806 /* Free old path when new path is assigned */
811 pr_warning("Failed to find source file path.\n");
818 fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
819 lr->start - lr->offset);
821 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
823 fp = fopen(lr->path, "r");
825 pr_warning("Failed to open %s: %s\n", lr->path,
826 strerror_r(errno, sbuf, sizeof(sbuf)));
829 /* Skip to starting line number */
830 while (l < lr->start) {
831 ret = skip_one_line(fp, l++);
836 intlist__for_each(ln, lr->line_list) {
837 for (; ln->i > l; l++) {
838 ret = show_one_line(fp, l - lr->offset);
842 ret = show_one_line_with_num(fp, l++ - lr->offset);
847 if (lr->end == INT_MAX)
848 lr->end = l + NR_ADDITIONAL_LINES;
849 while (l <= lr->end) {
850 ret = show_one_line_or_eof(fp, l++ - lr->offset);
859 int show_line_range(struct line_range *lr, const char *module, bool user)
863 ret = init_symbol_maps(user);
866 ret = __show_line_range(lr, module, user);
872 static int show_available_vars_at(struct debuginfo *dinfo,
873 struct perf_probe_event *pev,
874 struct strfilter *_filter)
878 struct str_node *node;
879 struct variable_list *vls = NULL, *vl;
880 struct perf_probe_point tmp;
883 buf = synthesize_perf_probe_point(&pev->point);
886 pr_debug("Searching variables at %s\n", buf);
888 ret = debuginfo__find_available_vars_at(dinfo, pev, &vls);
889 if (!ret) { /* Not found, retry with an alternative */
890 ret = get_alternative_probe_event(dinfo, pev, &tmp);
892 ret = debuginfo__find_available_vars_at(dinfo, pev,
894 /* Release the old probe_point */
895 clear_perf_probe_point(&tmp);
899 if (ret == 0 || ret == -ENOENT) {
900 pr_err("Failed to find the address of %s\n", buf);
903 pr_warning("Debuginfo analysis failed.\n");
907 /* Some variables are found */
908 fprintf(stdout, "Available variables at %s\n", buf);
909 for (i = 0; i < ret; i++) {
912 * A probe point might be converted to
913 * several trace points.
915 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
917 zfree(&vl->point.symbol);
920 strlist__for_each(node, vl->vars) {
921 var = strchr(node->s, '\t') + 1;
922 if (strfilter__compare(_filter, var)) {
923 fprintf(stdout, "\t\t%s\n", node->s);
927 strlist__delete(vl->vars);
930 fprintf(stdout, "\t\t(No matched variables)\n");
938 /* Show available variables on given probe point */
939 int show_available_vars(struct perf_probe_event *pevs, int npevs,
940 struct strfilter *_filter)
943 struct debuginfo *dinfo;
945 ret = init_symbol_maps(pevs->uprobes);
949 dinfo = open_debuginfo(pevs->target, false);
957 for (i = 0; i < npevs && ret >= 0; i++)
958 ret = show_available_vars_at(dinfo, &pevs[i], _filter);
960 debuginfo__delete(dinfo);
966 #else /* !HAVE_DWARF_SUPPORT */
968 static void debuginfo_cache__exit(void)
973 find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused,
974 struct perf_probe_point *pp __maybe_unused,
975 bool is_kprobe __maybe_unused)
980 static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
981 struct probe_trace_event **tevs __maybe_unused)
983 if (perf_probe_event_need_dwarf(pev)) {
984 pr_warning("Debuginfo-analysis is not supported.\n");
991 int show_line_range(struct line_range *lr __maybe_unused,
992 const char *module __maybe_unused,
993 bool user __maybe_unused)
995 pr_warning("Debuginfo-analysis is not supported.\n");
999 int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
1000 int npevs __maybe_unused,
1001 struct strfilter *filter __maybe_unused)
1003 pr_warning("Debuginfo-analysis is not supported.\n");
1008 void line_range__clear(struct line_range *lr)
1014 intlist__delete(lr->line_list);
1015 memset(lr, 0, sizeof(*lr));
1018 int line_range__init(struct line_range *lr)
1020 memset(lr, 0, sizeof(*lr));
1021 lr->line_list = intlist__new(NULL);
1028 static int parse_line_num(char **ptr, int *val, const char *what)
1030 const char *start = *ptr;
1033 *val = strtol(*ptr, ptr, 0);
1034 if (errno || *ptr == start) {
1035 semantic_error("'%s' is not a valid number.\n", what);
1041 /* Check the name is good for event, group or function */
1042 static bool is_c_func_name(const char *name)
1044 if (!isalpha(*name) && *name != '_')
1046 while (*++name != '\0') {
1047 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
1054 * Stuff 'lr' according to the line range described by 'arg'.
1055 * The line range syntax is described by:
1057 * SRC[:SLN[+NUM|-ELN]]
1058 * FNC[@SRC][:SLN[+NUM|-ELN]]
1060 int parse_line_range_desc(const char *arg, struct line_range *lr)
1062 char *range, *file, *name = strdup(arg);
1071 range = strchr(name, ':');
1075 err = parse_line_num(&range, &lr->start, "start line");
1079 if (*range == '+' || *range == '-') {
1080 const char c = *range++;
1082 err = parse_line_num(&range, &lr->end, "end line");
1087 lr->end += lr->start;
1089 * Adjust the number of lines here.
1090 * If the number of lines == 1, the
1091 * the end of line should be equal to
1092 * the start of line.
1098 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
1101 if (lr->start > lr->end) {
1102 semantic_error("Start line must be smaller"
1103 " than end line.\n");
1106 if (*range != '\0') {
1107 semantic_error("Tailing with invalid str '%s'.\n", range);
1112 file = strchr(name, '@');
1115 lr->file = strdup(++file);
1116 if (lr->file == NULL) {
1120 lr->function = name;
1121 } else if (strchr(name, '/') || strchr(name, '.'))
1123 else if (is_c_func_name(name))/* We reuse it for checking funcname */
1124 lr->function = name;
1125 else { /* Invalid name */
1126 semantic_error("'%s' is not a valid function name.\n", name);
1137 /* Parse probepoint definition. */
1138 static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
1140 struct perf_probe_point *pp = &pev->point;
1143 bool file_spec = false;
1146 * perf probe [EVENT=]SRC[:LN|;PTN]
1147 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
1149 * TODO:Group name support
1154 ptr = strpbrk(arg, ";=@+%");
1155 if (ptr && *ptr == '=') { /* Event name */
1158 if (strchr(arg, ':')) {
1159 semantic_error("Group name is not supported yet.\n");
1162 if (!is_c_func_name(arg)) {
1163 semantic_error("%s is bad for event name -it must "
1164 "follow C symbol-naming rule.\n", arg);
1167 pev->event = strdup(arg);
1168 if (pev->event == NULL)
1175 * Check arg is function or file name and copy it.
1177 * We consider arg to be a file spec if and only if it satisfies
1178 * all of the below criteria::
1179 * - it does not include any of "+@%",
1180 * - it includes one of ":;", and
1181 * - it has a period '.' in the name.
1183 * Otherwise, we consider arg to be a function specification.
1185 if (!strpbrk(arg, "+@%") && (ptr = strpbrk(arg, ";:")) != NULL) {
1186 /* This is a file spec if it includes a '.' before ; or : */
1187 if (memchr(arg, '.', ptr - arg))
1191 ptr = strpbrk(arg, ";:+@%");
1211 * Keep pp->function even if this is absolute address,
1212 * so it can mark whether abs_address is valid.
1213 * Which make 'perf probe lib.bin 0x0' possible.
1215 * Note that checking length of tmp is not needed
1216 * because when we access tmp[1] we know tmp[0] is '0',
1217 * so tmp[1] should always valid (but could be '\0').
1219 if (tmp && !strncmp(tmp, "0x", 2)) {
1220 pp->abs_address = strtoul(pp->function, &tmp, 0);
1222 semantic_error("Invalid absolute address.\n");
1228 /* Parse other options */
1232 if (c == ';') { /* Lazy pattern must be the last part */
1233 pp->lazy_line = strdup(arg);
1234 if (pp->lazy_line == NULL)
1238 ptr = strpbrk(arg, ";:+@%");
1244 case ':': /* Line number */
1245 pp->line = strtoul(arg, &tmp, 0);
1247 semantic_error("There is non-digit char"
1248 " in line number.\n");
1252 case '+': /* Byte offset from a symbol */
1253 pp->offset = strtoul(arg, &tmp, 0);
1255 semantic_error("There is non-digit character"
1260 case '@': /* File name */
1262 semantic_error("SRC@SRC is not allowed.\n");
1265 pp->file = strdup(arg);
1266 if (pp->file == NULL)
1269 case '%': /* Probe places */
1270 if (strcmp(arg, "return") == 0) {
1272 } else { /* Others not supported yet */
1273 semantic_error("%%%s is not supported.\n", arg);
1277 default: /* Buggy case */
1278 pr_err("This program has a bug at %s:%d.\n",
1279 __FILE__, __LINE__);
1285 /* Exclusion check */
1286 if (pp->lazy_line && pp->line) {
1287 semantic_error("Lazy pattern can't be used with"
1292 if (pp->lazy_line && pp->offset) {
1293 semantic_error("Lazy pattern can't be used with offset.\n");
1297 if (pp->line && pp->offset) {
1298 semantic_error("Offset can't be used with line number.\n");
1302 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
1303 semantic_error("File always requires line number or "
1308 if (pp->offset && !pp->function) {
1309 semantic_error("Offset requires an entry function.\n");
1313 if (pp->retprobe && !pp->function) {
1314 semantic_error("Return probe requires an entry function.\n");
1318 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
1319 semantic_error("Offset/Line/Lazy pattern can't be used with "
1324 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
1325 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
1330 /* Parse perf-probe event argument */
1331 static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
1333 char *tmp, *goodname;
1334 struct perf_probe_arg_field **fieldp;
1336 pr_debug("parsing arg: %s into ", str);
1338 tmp = strchr(str, '=');
1340 arg->name = strndup(str, tmp - str);
1341 if (arg->name == NULL)
1343 pr_debug("name:%s ", arg->name);
1347 tmp = strchr(str, ':');
1348 if (tmp) { /* Type setting */
1350 arg->type = strdup(tmp + 1);
1351 if (arg->type == NULL)
1353 pr_debug("type:%s ", arg->type);
1356 tmp = strpbrk(str, "-.[");
1357 if (!is_c_varname(str) || !tmp) {
1358 /* A variable, register, symbol or special value */
1359 arg->var = strdup(str);
1360 if (arg->var == NULL)
1362 pr_debug("%s\n", arg->var);
1366 /* Structure fields or array element */
1367 arg->var = strndup(str, tmp - str);
1368 if (arg->var == NULL)
1370 goodname = arg->var;
1371 pr_debug("%s, ", arg->var);
1372 fieldp = &arg->field;
1375 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
1376 if (*fieldp == NULL)
1378 if (*tmp == '[') { /* Array */
1380 (*fieldp)->index = strtol(str + 1, &tmp, 0);
1381 (*fieldp)->ref = true;
1382 if (*tmp != ']' || tmp == str + 1) {
1383 semantic_error("Array index must be a"
1390 } else { /* Structure */
1393 (*fieldp)->ref = false;
1394 } else if (tmp[1] == '>') {
1396 (*fieldp)->ref = true;
1398 semantic_error("Argument parse error: %s\n",
1402 tmp = strpbrk(str, "-.[");
1405 (*fieldp)->name = strndup(str, tmp - str);
1406 if ((*fieldp)->name == NULL)
1409 goodname = (*fieldp)->name;
1410 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
1411 fieldp = &(*fieldp)->next;
1414 (*fieldp)->name = strdup(str);
1415 if ((*fieldp)->name == NULL)
1418 goodname = (*fieldp)->name;
1419 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
1421 /* If no name is specified, set the last field name (not array index)*/
1423 arg->name = strdup(goodname);
1424 if (arg->name == NULL)
1430 /* Parse perf-probe event command */
1431 int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
1434 int argc, i, ret = 0;
1436 argv = argv_split(cmd, &argc);
1438 pr_debug("Failed to split arguments.\n");
1441 if (argc - 1 > MAX_PROBE_ARGS) {
1442 semantic_error("Too many probe arguments (%d).\n", argc - 1);
1446 /* Parse probe point */
1447 ret = parse_perf_probe_point(argv[0], pev);
1451 /* Copy arguments and ensure return probe has no C argument */
1452 pev->nargs = argc - 1;
1453 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1454 if (pev->args == NULL) {
1458 for (i = 0; i < pev->nargs && ret >= 0; i++) {
1459 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1461 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
1462 semantic_error("You can't specify local variable for"
1473 /* Return true if this perf_probe_event requires debuginfo */
1474 bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
1478 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1481 for (i = 0; i < pev->nargs; i++)
1482 if (is_c_varname(pev->args[i].var))
1488 /* Parse probe_events event into struct probe_point */
1489 int parse_probe_trace_command(const char *cmd, struct probe_trace_event *tev)
1491 struct probe_trace_point *tp = &tev->point;
1494 char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
1498 pr_debug("Parsing probe_events: %s\n", cmd);
1499 argv = argv_split(cmd, &argc);
1501 pr_debug("Failed to split arguments.\n");
1505 semantic_error("Too few probe arguments.\n");
1510 /* Scan event and group name. */
1511 argv0_str = strdup(argv[0]);
1512 if (argv0_str == NULL) {
1516 fmt1_str = strtok_r(argv0_str, ":", &fmt);
1517 fmt2_str = strtok_r(NULL, "/", &fmt);
1518 fmt3_str = strtok_r(NULL, " \t", &fmt);
1519 if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
1520 || fmt3_str == NULL) {
1521 semantic_error("Failed to parse event name: %s\n", argv[0]);
1526 tev->group = strdup(fmt2_str);
1527 tev->event = strdup(fmt3_str);
1528 if (tev->group == NULL || tev->event == NULL) {
1532 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
1534 tp->retprobe = (pr == 'r');
1536 /* Scan module name(if there), function name and offset */
1537 p = strchr(argv[1], ':');
1539 tp->module = strndup(argv[1], p - argv[1]);
1543 fmt1_str = strtok_r(p, "+", &fmt);
1544 /* only the address started with 0x */
1545 if (fmt1_str[0] == '0') {
1547 * Fix a special case:
1548 * if address == 0, kernel reports something like:
1549 * p:probe_libc/abs_0 /lib/libc-2.18.so:0x (null) arg1=%ax
1550 * Newer kernel may fix that, but we want to
1551 * support old kernel also.
1553 if (strcmp(fmt1_str, "0x") == 0) {
1554 if (!argv[2] || strcmp(argv[2], "(null)")) {
1561 for (i = 2; argv[i + 1] != NULL; i++)
1562 argv[i] = argv[i + 1];
1567 tp->address = strtoul(fmt1_str, NULL, 0);
1569 /* Only the symbol-based probe has offset */
1570 tp->symbol = strdup(fmt1_str);
1571 if (tp->symbol == NULL) {
1575 fmt2_str = strtok_r(NULL, "", &fmt);
1576 if (fmt2_str == NULL)
1579 tp->offset = strtoul(fmt2_str, NULL, 10);
1582 tev->nargs = argc - 2;
1583 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
1584 if (tev->args == NULL) {
1588 for (i = 0; i < tev->nargs; i++) {
1589 p = strchr(argv[i + 2], '=');
1590 if (p) /* We don't need which register is assigned. */
1594 tev->args[i].name = strdup(argv[i + 2]);
1595 /* TODO: parse regs and offset */
1596 tev->args[i].value = strdup(p);
1597 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1609 /* Compose only probe arg */
1610 int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
1612 struct perf_probe_arg_field *field = pa->field;
1616 if (pa->name && pa->var)
1617 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
1619 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
1626 if (field->name[0] == '[')
1627 ret = e_snprintf(tmp, len, "%s", field->name);
1629 ret = e_snprintf(tmp, len, "%s%s",
1630 field->ref ? "->" : ".", field->name);
1635 field = field->next;
1639 ret = e_snprintf(tmp, len, ":%s", pa->type);
1648 pr_debug("Failed to synthesize perf probe argument: %d\n", ret);
1652 /* Compose only probe point (not argument) */
1653 static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
1656 char offs[32] = "", line[32] = "", file[32] = "";
1659 buf = zalloc(MAX_CMDLEN);
1665 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
1670 ret = e_snprintf(line, 32, ":%d", pp->line);
1678 tmp = strchr(pp->file + len - 30, '/');
1679 tmp = tmp ? tmp + 1 : pp->file + len - 30;
1681 ret = e_snprintf(file, 32, "@%s", tmp);
1687 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
1688 offs, pp->retprobe ? "%return" : "", line,
1691 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
1697 pr_debug("Failed to synthesize perf probe point: %d\n", ret);
1703 char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1708 buf = synthesize_perf_probe_point(&pev->point);
1713 for (i = 0; i < pev->nargs; i++) {
1714 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
1727 static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
1728 char **buf, size_t *buflen,
1733 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
1739 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
1751 static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
1752 char *buf, size_t buflen)
1754 struct probe_trace_arg_ref *ref = arg->ref;
1758 /* Argument name or separator */
1760 ret = e_snprintf(buf, buflen, " %s=", arg->name);
1762 ret = e_snprintf(buf, buflen, " ");
1768 /* Special case: @XXX */
1769 if (arg->value[0] == '@' && arg->ref)
1772 /* Dereferencing arguments */
1774 depth = __synthesize_probe_trace_arg_ref(ref, &buf,
1780 /* Print argument value */
1781 if (arg->value[0] == '@' && arg->ref)
1782 ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
1785 ret = e_snprintf(buf, buflen, "%s", arg->value);
1793 ret = e_snprintf(buf, buflen, ")");
1799 /* Print argument type */
1801 ret = e_snprintf(buf, buflen, ":%s", arg->type);
1810 char *synthesize_probe_trace_command(struct probe_trace_event *tev)
1812 struct probe_trace_point *tp = &tev->point;
1816 buf = zalloc(MAX_CMDLEN);
1820 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
1821 tev->group, tev->event);
1825 /* Uprobes must have tp->module */
1826 if (tev->uprobes && !tp->module)
1829 * If tp->address == 0, then this point must be a
1830 * absolute address uprobe.
1831 * try_to_find_absolute_address() should have made
1832 * tp->symbol to "0x0".
1834 if (tev->uprobes && !tp->address) {
1835 if (!tp->symbol || strcmp(tp->symbol, "0x0"))
1839 /* Use the tp->address for uprobes */
1841 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s:0x%lx",
1842 tp->module, tp->address);
1843 else if (!strncmp(tp->symbol, "0x", 2))
1844 /* Absolute address. See try_to_find_absolute_address() */
1845 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s%s0x%lx",
1846 tp->module ?: "", tp->module ? ":" : "",
1849 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s%s%s+%lu",
1850 tp->module ?: "", tp->module ? ":" : "",
1851 tp->symbol, tp->offset);
1857 for (i = 0; i < tev->nargs; i++) {
1858 ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
1871 static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
1872 struct perf_probe_point *pp,
1875 struct symbol *sym = NULL;
1877 u64 addr = tp->address;
1881 map = dso__new_map(tp->module);
1884 sym = map__find_symbol(map, addr, NULL);
1887 addr = kernel_get_symbol_address_by_name(tp->symbol, true);
1890 sym = __find_kernel_function(addr, &map);
1896 pp->retprobe = tp->retprobe;
1897 pp->offset = addr - map->unmap_ip(map, sym->start);
1898 pp->function = strdup(sym->name);
1899 ret = pp->function ? 0 : -ENOMEM;
1902 if (map && !is_kprobe) {
1909 static int convert_to_perf_probe_point(struct probe_trace_point *tp,
1910 struct perf_probe_point *pp,
1916 ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe);
1919 ret = find_perf_probe_point_from_map(tp, pp, is_kprobe);
1923 pr_debug("Failed to find probe point from both of dwarf and map.\n");
1926 pp->function = strdup(tp->symbol);
1927 pp->offset = tp->offset;
1929 ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address);
1932 pp->function = strdup(buf);
1935 if (pp->function == NULL)
1938 pp->retprobe = tp->retprobe;
1943 static int convert_to_perf_probe_event(struct probe_trace_event *tev,
1944 struct perf_probe_event *pev, bool is_kprobe)
1949 /* Convert event/group name */
1950 pev->event = strdup(tev->event);
1951 pev->group = strdup(tev->group);
1952 if (pev->event == NULL || pev->group == NULL)
1955 /* Convert trace_point to probe_point */
1956 ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
1960 /* Convert trace_arg to probe_arg */
1961 pev->nargs = tev->nargs;
1962 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1963 if (pev->args == NULL)
1965 for (i = 0; i < tev->nargs && ret >= 0; i++) {
1966 if (tev->args[i].name)
1967 pev->args[i].name = strdup(tev->args[i].name);
1969 ret = synthesize_probe_trace_arg(&tev->args[i],
1971 pev->args[i].name = strdup(buf);
1973 if (pev->args[i].name == NULL && ret >= 0)
1978 clear_perf_probe_event(pev);
1983 void clear_perf_probe_event(struct perf_probe_event *pev)
1985 struct perf_probe_arg_field *field, *next;
1991 clear_perf_probe_point(&pev->point);
1993 for (i = 0; i < pev->nargs; i++) {
1994 free(pev->args[i].name);
1995 free(pev->args[i].var);
1996 free(pev->args[i].type);
1997 field = pev->args[i].field;
2000 zfree(&field->name);
2006 memset(pev, 0, sizeof(*pev));
2009 void clear_probe_trace_event(struct probe_trace_event *tev)
2011 struct probe_trace_arg_ref *ref, *next;
2016 free(tev->point.symbol);
2017 free(tev->point.realname);
2018 free(tev->point.module);
2019 for (i = 0; i < tev->nargs; i++) {
2020 free(tev->args[i].name);
2021 free(tev->args[i].value);
2022 free(tev->args[i].type);
2023 ref = tev->args[i].ref;
2031 memset(tev, 0, sizeof(*tev));
2034 struct kprobe_blacklist_node {
2035 struct list_head list;
2036 unsigned long start;
2041 static void kprobe_blacklist__delete(struct list_head *blacklist)
2043 struct kprobe_blacklist_node *node;
2045 while (!list_empty(blacklist)) {
2046 node = list_first_entry(blacklist,
2047 struct kprobe_blacklist_node, list);
2048 list_del(&node->list);
2054 static int kprobe_blacklist__load(struct list_head *blacklist)
2056 struct kprobe_blacklist_node *node;
2057 const char *__debugfs = debugfs_find_mountpoint();
2058 char buf[PATH_MAX], *p;
2062 if (__debugfs == NULL)
2065 ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs);
2069 fp = fopen(buf, "r");
2074 while (fgets(buf, PATH_MAX, fp)) {
2075 node = zalloc(sizeof(*node));
2080 INIT_LIST_HEAD(&node->list);
2081 list_add_tail(&node->list, blacklist);
2082 if (sscanf(buf, "0x%lx-0x%lx", &node->start, &node->end) != 2) {
2086 p = strchr(buf, '\t');
2089 if (p[strlen(p) - 1] == '\n')
2090 p[strlen(p) - 1] = '\0';
2092 p = (char *)"unknown";
2093 node->symbol = strdup(p);
2094 if (!node->symbol) {
2098 pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
2099 node->start, node->end, node->symbol);
2103 kprobe_blacklist__delete(blacklist);
2109 static struct kprobe_blacklist_node *
2110 kprobe_blacklist__find_by_address(struct list_head *blacklist,
2111 unsigned long address)
2113 struct kprobe_blacklist_node *node;
2115 list_for_each_entry(node, blacklist, list) {
2116 if (node->start <= address && address <= node->end)
2123 static LIST_HEAD(kprobe_blacklist);
2125 static void kprobe_blacklist__init(void)
2127 if (!list_empty(&kprobe_blacklist))
2130 if (kprobe_blacklist__load(&kprobe_blacklist) < 0)
2131 pr_debug("No kprobe blacklist support, ignored\n");
2134 static void kprobe_blacklist__release(void)
2136 kprobe_blacklist__delete(&kprobe_blacklist);
2139 static bool kprobe_blacklist__listed(unsigned long address)
2141 return !!kprobe_blacklist__find_by_address(&kprobe_blacklist, address);
2144 static int perf_probe_event__sprintf(const char *group, const char *event,
2145 struct perf_probe_event *pev,
2147 struct strbuf *result)
2153 /* Synthesize only event probe point */
2154 place = synthesize_perf_probe_point(&pev->point);
2158 ret = e_snprintf(buf, 128, "%s:%s", group, event);
2162 strbuf_addf(result, " %-20s (on %s", buf, place);
2164 strbuf_addf(result, " in %s", module);
2166 if (pev->nargs > 0) {
2167 strbuf_addstr(result, " with");
2168 for (i = 0; i < pev->nargs; i++) {
2169 ret = synthesize_perf_probe_arg(&pev->args[i],
2173 strbuf_addf(result, " %s", buf);
2176 strbuf_addch(result, ')');
2183 static int show_perf_probe_event(const char *group, const char *event,
2184 struct perf_probe_event *pev,
2185 const char *module, bool use_stdout)
2187 struct strbuf buf = STRBUF_INIT;
2190 ret = perf_probe_event__sprintf(group, event, pev, module, &buf);
2193 printf("%s\n", buf.buf);
2195 pr_info("%s\n", buf.buf);
2197 strbuf_release(&buf);
2202 static bool filter_probe_trace_event(struct probe_trace_event *tev,
2203 struct strfilter *filter)
2207 /* At first, check the event name itself */
2208 if (strfilter__compare(filter, tev->event))
2211 /* Next, check the combination of name and group */
2212 if (e_snprintf(tmp, 128, "%s:%s", tev->group, tev->event) < 0)
2214 return strfilter__compare(filter, tmp);
2217 static int __show_perf_probe_events(int fd, bool is_kprobe,
2218 struct strfilter *filter)
2221 struct probe_trace_event tev;
2222 struct perf_probe_event pev;
2223 struct strlist *rawlist;
2224 struct str_node *ent;
2226 memset(&tev, 0, sizeof(tev));
2227 memset(&pev, 0, sizeof(pev));
2229 rawlist = probe_file__get_rawlist(fd);
2233 strlist__for_each(ent, rawlist) {
2234 ret = parse_probe_trace_command(ent->s, &tev);
2236 if (!filter_probe_trace_event(&tev, filter))
2238 ret = convert_to_perf_probe_event(&tev, &pev,
2242 ret = show_perf_probe_event(pev.group, pev.event,
2243 &pev, tev.point.module,
2247 clear_perf_probe_event(&pev);
2248 clear_probe_trace_event(&tev);
2252 strlist__delete(rawlist);
2253 /* Cleanup cached debuginfo if needed */
2254 debuginfo_cache__exit();
2259 /* List up current perf-probe events */
2260 int show_perf_probe_events(struct strfilter *filter)
2262 int kp_fd, up_fd, ret;
2266 ret = init_symbol_maps(false);
2270 ret = probe_file__open_both(&kp_fd, &up_fd, 0);
2275 ret = __show_perf_probe_events(kp_fd, true, filter);
2276 if (up_fd >= 0 && ret >= 0)
2277 ret = __show_perf_probe_events(up_fd, false, filter);
2287 static int get_new_event_name(char *buf, size_t len, const char *base,
2288 struct strlist *namelist, bool allow_suffix)
2297 ret = e_snprintf(buf, len, "%s", base);
2299 pr_debug("snprintf() failed: %d\n", ret);
2302 /* Cut off the postfixes (e.g. .const, .isra)*/
2303 p = strchr(buf, '.');
2306 if (!strlist__has_entry(namelist, buf))
2309 if (!allow_suffix) {
2310 pr_warning("Error: event \"%s\" already exists. "
2311 "(Use -f to force duplicates.)\n", base);
2315 /* Try to add suffix */
2316 for (i = 1; i < MAX_EVENT_INDEX; i++) {
2317 ret = e_snprintf(buf, len, "%s_%d", base, i);
2319 pr_debug("snprintf() failed: %d\n", ret);
2322 if (!strlist__has_entry(namelist, buf))
2325 if (i == MAX_EVENT_INDEX) {
2326 pr_warning("Too many events are on the same function.\n");
2333 /* Warn if the current kernel's uprobe implementation is old */
2334 static void warn_uprobe_event_compat(struct probe_trace_event *tev)
2337 char *buf = synthesize_probe_trace_command(tev);
2339 /* Old uprobe event doesn't support memory dereference */
2340 if (!tev->uprobes || tev->nargs == 0 || !buf)
2343 for (i = 0; i < tev->nargs; i++)
2344 if (strglobmatch(tev->args[i].value, "[$@+-]*")) {
2345 pr_warning("Please upgrade your kernel to at least "
2346 "3.14 to have access to feature %s\n",
2347 tev->args[i].value);
2354 /* Set new name from original perf_probe_event and namelist */
2355 static int probe_trace_event__set_name(struct probe_trace_event *tev,
2356 struct perf_probe_event *pev,
2357 struct strlist *namelist,
2360 const char *event, *group;
2367 if (pev->point.function &&
2368 (strncmp(pev->point.function, "0x", 2) != 0) &&
2369 !strisglob(pev->point.function))
2370 event = pev->point.function;
2372 event = tev->point.realname;
2376 group = PERFPROBE_GROUP;
2378 /* Get an unused new event name */
2379 ret = get_new_event_name(buf, 64, event,
2380 namelist, allow_suffix);
2386 tev->event = strdup(event);
2387 tev->group = strdup(group);
2388 if (tev->event == NULL || tev->group == NULL)
2391 /* Add added event name to namelist */
2392 strlist__add(namelist, event);
2396 static int __add_probe_trace_events(struct perf_probe_event *pev,
2397 struct probe_trace_event *tevs,
2398 int ntevs, bool allow_suffix)
2401 struct probe_trace_event *tev = NULL;
2402 const char *event = NULL, *group = NULL;
2403 struct strlist *namelist;
2405 fd = probe_file__open(PF_FL_RW | (pev->uprobes ? PF_FL_UPROBE : 0));
2409 /* Get current event names */
2410 namelist = probe_file__get_namelist(fd);
2412 pr_debug("Failed to get current event list.\n");
2418 pr_info("Added new event%s\n", (ntevs > 1) ? "s:" : ":");
2419 for (i = 0; i < ntevs; i++) {
2421 /* Skip if the symbol is out of .text or blacklisted */
2422 if (!tev->point.symbol)
2425 /* Set new name for tev (and update namelist) */
2426 ret = probe_trace_event__set_name(tev, pev, namelist,
2431 ret = probe_file__add_event(fd, tev);
2435 /* We use tev's name for showing new events */
2436 show_perf_probe_event(tev->group, tev->event, pev,
2437 tev->point.module, false);
2438 /* Save the last valid name */
2443 * Probes after the first probe which comes from same
2444 * user input are always allowed to add suffix, because
2445 * there might be several addresses corresponding to
2448 allow_suffix = true;
2450 if (ret == -EINVAL && pev->uprobes)
2451 warn_uprobe_event_compat(tev);
2453 /* Note that it is possible to skip all events because of blacklist */
2454 if (ret >= 0 && event) {
2455 /* Show how to use the event. */
2456 pr_info("\nYou can now use it in all perf tools, such as:\n\n");
2457 pr_info("\tperf record -e %s:%s -aR sleep 1\n\n", group, event);
2460 strlist__delete(namelist);
2466 static int find_probe_functions(struct map *map, char *name,
2467 struct symbol **syms)
2471 struct rb_node *tmp;
2473 if (map__load(map, NULL) < 0)
2476 map__for_each_symbol(map, sym, tmp) {
2477 if (strglobmatch(sym->name, name)) {
2479 if (syms && found < probe_conf.max_probes)
2480 syms[found - 1] = sym;
2487 #define strdup_or_goto(str, label) \
2488 ({ char *__p = strdup(str); if (!__p) goto label; __p; })
2490 void __weak arch__fix_tev_from_maps(struct perf_probe_event *pev __maybe_unused,
2491 struct probe_trace_event *tev __maybe_unused,
2492 struct map *map __maybe_unused) { }
2495 * Find probe function addresses from map.
2496 * Return an error or the number of found probe_trace_event
2498 static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
2499 struct probe_trace_event **tevs)
2501 struct map *map = NULL;
2502 struct ref_reloc_sym *reloc_sym = NULL;
2504 struct symbol **syms = NULL;
2505 struct probe_trace_event *tev;
2506 struct perf_probe_point *pp = &pev->point;
2507 struct probe_trace_point *tp;
2508 int num_matched_functions;
2509 int ret, i, j, skipped = 0;
2511 map = get_target_map(pev->target, pev->uprobes);
2517 syms = malloc(sizeof(struct symbol *) * probe_conf.max_probes);
2524 * Load matched symbols: Since the different local symbols may have
2525 * same name but different addresses, this lists all the symbols.
2527 num_matched_functions = find_probe_functions(map, pp->function, syms);
2528 if (num_matched_functions == 0) {
2529 pr_err("Failed to find symbol %s in %s\n", pp->function,
2530 pev->target ? : "kernel");
2533 } else if (num_matched_functions > probe_conf.max_probes) {
2534 pr_err("Too many functions matched in %s\n",
2535 pev->target ? : "kernel");
2540 if (!pev->uprobes && !pp->retprobe) {
2541 reloc_sym = kernel_get_ref_reloc_sym();
2543 pr_warning("Relocated base symbol is not found!\n");
2549 /* Setup result trace-probe-events */
2550 *tevs = zalloc(sizeof(*tev) * num_matched_functions);
2558 for (j = 0; j < num_matched_functions; j++) {
2561 tev = (*tevs) + ret;
2563 if (ret == num_matched_functions) {
2564 pr_warning("Too many symbols are listed. Skip it.\n");
2569 if (pp->offset > sym->end - sym->start) {
2570 pr_warning("Offset %ld is bigger than the size of %s\n",
2571 pp->offset, sym->name);
2575 /* Add one probe point */
2576 tp->address = map->unmap_ip(map, sym->start) + pp->offset;
2577 /* If we found a wrong one, mark it by NULL symbol */
2578 if (!pev->uprobes &&
2579 kprobe_warn_out_range(sym->name, tp->address)) {
2580 tp->symbol = NULL; /* Skip it */
2582 } else if (reloc_sym) {
2583 tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
2584 tp->offset = tp->address - reloc_sym->addr;
2586 tp->symbol = strdup_or_goto(sym->name, nomem_out);
2587 tp->offset = pp->offset;
2589 tp->realname = strdup_or_goto(sym->name, nomem_out);
2591 tp->retprobe = pp->retprobe;
2593 tev->point.module = strdup_or_goto(pev->target,
2595 tev->uprobes = pev->uprobes;
2596 tev->nargs = pev->nargs;
2598 tev->args = zalloc(sizeof(struct probe_trace_arg) *
2600 if (tev->args == NULL)
2603 for (i = 0; i < tev->nargs; i++) {
2604 if (pev->args[i].name)
2606 strdup_or_goto(pev->args[i].name,
2609 tev->args[i].value = strdup_or_goto(pev->args[i].var,
2611 if (pev->args[i].type)
2613 strdup_or_goto(pev->args[i].type,
2616 arch__fix_tev_from_maps(pev, tev, map);
2618 if (ret == skipped) {
2624 put_target_map(map, pev->uprobes);
2631 clear_probe_trace_events(*tevs, num_matched_functions);
2636 static int try_to_find_absolute_address(struct perf_probe_event *pev,
2637 struct probe_trace_event **tevs)
2639 struct perf_probe_point *pp = &pev->point;
2640 struct probe_trace_event *tev;
2641 struct probe_trace_point *tp;
2644 if (!(pev->point.function && !strncmp(pev->point.function, "0x", 2)))
2646 if (perf_probe_event_need_dwarf(pev))
2650 * This is 'perf probe /lib/libc.so 0xabcd'. Try to probe at
2653 * Only one tev can be generated by this.
2655 *tevs = zalloc(sizeof(*tev));
2663 * Don't use tp->offset, use address directly, because
2664 * in synthesize_probe_trace_command() address cannot be
2667 tp->address = pev->point.abs_address;
2668 tp->retprobe = pp->retprobe;
2669 tev->uprobes = pev->uprobes;
2673 * Give it a '0x' leading symbol name.
2674 * In __add_probe_trace_events, a NULL symbol is interpreted as
2677 if (asprintf(&tp->symbol, "0x%lx", tp->address) < 0)
2680 /* For kprobe, check range */
2681 if ((!tev->uprobes) &&
2682 (kprobe_warn_out_range(tev->point.symbol,
2683 tev->point.address))) {
2688 if (asprintf(&tp->realname, "abs_%lx", tp->address) < 0)
2692 tp->module = strdup(pev->target);
2698 tev->group = strdup(pev->group);
2704 tev->event = strdup(pev->event);
2709 tev->nargs = pev->nargs;
2710 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
2715 for (i = 0; i < tev->nargs; i++)
2716 copy_to_probe_trace_arg(&tev->args[i], &pev->args[i]);
2722 clear_probe_trace_events(*tevs, 1);
2728 bool __weak arch__prefers_symtab(void) { return false; }
2730 static int convert_to_probe_trace_events(struct perf_probe_event *pev,
2731 struct probe_trace_event **tevs)
2735 if (pev->uprobes && !pev->group) {
2736 /* Replace group name if not given */
2737 ret = convert_exec_to_group(pev->target, &pev->group);
2739 pr_warning("Failed to make a group name.\n");
2744 ret = try_to_find_absolute_address(pev, tevs);
2748 if (arch__prefers_symtab() && !perf_probe_event_need_dwarf(pev)) {
2749 ret = find_probe_trace_events_from_map(pev, tevs);
2751 return ret; /* Found in symbol table */
2754 /* Convert perf_probe_event with debuginfo */
2755 ret = try_to_find_probe_trace_events(pev, tevs);
2757 return ret; /* Found in debuginfo or got an error */
2759 return find_probe_trace_events_from_map(pev, tevs);
2762 struct __event_package {
2763 struct perf_probe_event *pev;
2764 struct probe_trace_event *tevs;
2768 int add_perf_probe_events(struct perf_probe_event *pevs, int npevs)
2771 struct __event_package *pkgs;
2774 pkgs = zalloc(sizeof(struct __event_package) * npevs);
2779 ret = init_symbol_maps(pevs->uprobes);
2785 /* Loop 1: convert all events */
2786 for (i = 0; i < npevs; i++) {
2787 pkgs[i].pev = &pevs[i];
2788 /* Init kprobe blacklist if needed */
2789 if (!pkgs[i].pev->uprobes)
2790 kprobe_blacklist__init();
2791 /* Convert with or without debuginfo */
2792 ret = convert_to_probe_trace_events(pkgs[i].pev,
2796 pkgs[i].ntevs = ret;
2798 /* This just release blacklist only if allocated */
2799 kprobe_blacklist__release();
2801 /* Loop 2: add all events */
2802 for (i = 0; i < npevs; i++) {
2803 ret = __add_probe_trace_events(pkgs[i].pev, pkgs[i].tevs,
2805 probe_conf.force_add);
2810 /* Loop 3: cleanup and free trace events */
2811 for (i = 0; i < npevs; i++) {
2812 for (j = 0; j < pkgs[i].ntevs; j++)
2813 clear_probe_trace_event(&pkgs[i].tevs[j]);
2814 zfree(&pkgs[i].tevs);
2822 int del_perf_probe_events(struct strfilter *filter)
2824 int ret, ret2, ufd = -1, kfd = -1;
2825 char *str = strfilter__string(filter);
2830 pr_debug("Delete filter: \'%s\'\n", str);
2832 /* Get current event names */
2833 ret = probe_file__open_both(&kfd, &ufd, PF_FL_RW);
2837 ret = probe_file__del_events(kfd, filter);
2838 if (ret < 0 && ret != -ENOENT)
2841 ret2 = probe_file__del_events(ufd, filter);
2842 if (ret2 < 0 && ret2 != -ENOENT) {
2846 if (ret == -ENOENT && ret2 == -ENOENT)
2847 pr_debug("\"%s\" does not hit any event.\n", str);
2848 /* Note that this is silently ignored */
2862 /* TODO: don't use a global variable for filter ... */
2863 static struct strfilter *available_func_filter;
2866 * If a symbol corresponds to a function with global binding and
2867 * matches filter return 0. For all others return 1.
2869 static int filter_available_functions(struct map *map __maybe_unused,
2872 if (strfilter__compare(available_func_filter, sym->name))
2877 int show_available_funcs(const char *target, struct strfilter *_filter,
2883 ret = init_symbol_maps(user);
2887 /* Get a symbol map */
2889 map = dso__new_map(target);
2891 map = kernel_get_module_map(target);
2893 pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
2897 /* Load symbols with given filter */
2898 available_func_filter = _filter;
2899 if (map__load(map, filter_available_functions)) {
2900 pr_err("Failed to load symbols in %s\n", (target) ? : "kernel");
2903 if (!dso__sorted_by_name(map->dso, map->type))
2904 dso__sort_by_name(map->dso, map->type);
2906 /* Show all (filtered) symbols */
2908 dso__fprintf_symbols_by_name(map->dso, map->type, stdout);
2918 int copy_to_probe_trace_arg(struct probe_trace_arg *tvar,
2919 struct perf_probe_arg *pvar)
2921 tvar->value = strdup(pvar->var);
2922 if (tvar->value == NULL)
2925 tvar->type = strdup(pvar->type);
2926 if (tvar->type == NULL)
2930 tvar->name = strdup(pvar->name);
2931 if (tvar->name == NULL)