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"
50 #define MAX_CMDLEN 256
51 #define PERFPROBE_GROUP "probe"
53 bool probe_event_dry_run; /* Dry run flag */
54 struct probe_conf probe_conf;
56 #define semantic_error(msg ...) pr_err("Semantic error :" msg)
58 /* If there is no space to write, returns -E2BIG. */
59 static int e_snprintf(char *str, size_t size, const char *format, ...)
60 __attribute__((format(printf, 3, 4)));
62 static int e_snprintf(char *str, size_t size, const char *format, ...)
67 ret = vsnprintf(str, size, format, ap);
74 static char *synthesize_perf_probe_point(struct perf_probe_point *pp);
75 static void clear_probe_trace_event(struct probe_trace_event *tev);
76 static struct machine *host_machine;
78 /* Initialize symbol maps and path of vmlinux/modules */
79 static int init_symbol_maps(bool user_only)
83 symbol_conf.sort_by_name = true;
84 symbol_conf.allow_aliases = true;
85 ret = symbol__init(NULL);
87 pr_debug("Failed to init symbol map.\n");
91 if (host_machine || user_only) /* already initialized */
94 if (symbol_conf.vmlinux_name)
95 pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
97 host_machine = machine__new_host();
99 pr_debug("machine__new_host() failed.\n");
105 pr_warning("Failed to init vmlinux path.\n");
109 static void exit_symbol_maps(void)
112 machine__delete(host_machine);
118 static struct symbol *__find_kernel_function_by_name(const char *name,
121 return machine__find_kernel_function_by_name(host_machine, name, mapp,
125 static struct symbol *__find_kernel_function(u64 addr, struct map **mapp)
127 return machine__find_kernel_function(host_machine, addr, mapp, NULL);
130 static struct ref_reloc_sym *kernel_get_ref_reloc_sym(void)
132 /* kmap->ref_reloc_sym should be set if host_machine is initialized */
135 if (map__load(host_machine->vmlinux_maps[MAP__FUNCTION], NULL) < 0)
138 kmap = map__kmap(host_machine->vmlinux_maps[MAP__FUNCTION]);
141 return kmap->ref_reloc_sym;
144 static u64 kernel_get_symbol_address_by_name(const char *name, bool reloc)
146 struct ref_reloc_sym *reloc_sym;
150 /* ref_reloc_sym is just a label. Need a special fix*/
151 reloc_sym = kernel_get_ref_reloc_sym();
152 if (reloc_sym && strcmp(name, reloc_sym->name) == 0)
153 return (reloc) ? reloc_sym->addr : reloc_sym->unrelocated_addr;
155 sym = __find_kernel_function_by_name(name, &map);
157 return map->unmap_ip(map, sym->start) -
158 ((reloc) ? 0 : map->reloc);
163 static struct map *kernel_get_module_map(const char *module)
165 struct map_groups *grp = &host_machine->kmaps;
166 struct maps *maps = &grp->maps[MAP__FUNCTION];
169 /* A file path -- this is an offline module */
170 if (module && strchr(module, '/'))
171 return machine__findnew_module_map(host_machine, 0, module);
176 for (pos = maps__first(maps); pos; pos = map__next(pos)) {
177 if (strncmp(pos->dso->short_name + 1, module,
178 pos->dso->short_name_len - 2) == 0) {
185 static struct map *get_target_map(const char *target, bool user)
187 /* Init maps of given executable or kernel */
189 return dso__new_map(target);
191 return kernel_get_module_map(target);
194 static void put_target_map(struct map *map, bool user)
197 /* Only the user map needs to be released */
203 static int convert_exec_to_group(const char *exec, char **result)
205 char *ptr1, *ptr2, *exec_copy;
209 exec_copy = strdup(exec);
213 ptr1 = basename(exec_copy);
219 ptr2 = strpbrk(ptr1, "-._");
222 ret = e_snprintf(buf, 64, "%s_%s", PERFPROBE_GROUP, ptr1);
226 *result = strdup(buf);
227 ret = *result ? 0 : -ENOMEM;
234 static void clear_perf_probe_point(struct perf_probe_point *pp)
241 static void clear_probe_trace_events(struct probe_trace_event *tevs, int ntevs)
245 for (i = 0; i < ntevs; i++)
246 clear_probe_trace_event(tevs + i);
249 static bool kprobe_blacklist__listed(unsigned long address);
250 static bool kprobe_warn_out_range(const char *symbol, unsigned long address)
254 /* Get the address of _etext for checking non-probable text symbol */
255 etext_addr = kernel_get_symbol_address_by_name("_etext", false);
257 if (etext_addr != 0 && etext_addr < address)
258 pr_warning("%s is out of .text, skip it.\n", symbol);
259 else if (kprobe_blacklist__listed(address))
260 pr_warning("%s is blacklisted function, skip it.\n", symbol);
267 #ifdef HAVE_DWARF_SUPPORT
269 static int kernel_get_module_dso(const char *module, struct dso **pdso)
273 const char *vmlinux_name;
277 list_for_each_entry(dso, &host_machine->dsos.head, node) {
280 if (strncmp(dso->short_name + 1, module,
281 dso->short_name_len - 2) == 0)
284 pr_debug("Failed to find module %s.\n", module);
288 map = host_machine->vmlinux_maps[MAP__FUNCTION];
291 vmlinux_name = symbol_conf.vmlinux_name;
294 ret = dso__load_vmlinux(dso, map, vmlinux_name, false, NULL);
296 ret = dso__load_vmlinux_path(dso, map, NULL);
303 * Some binaries like glibc have special symbols which are on the symbol
304 * table, but not in the debuginfo. If we can find the address of the
305 * symbol from map, we can translate the address back to the probe point.
307 static int find_alternative_probe_point(struct debuginfo *dinfo,
308 struct perf_probe_point *pp,
309 struct perf_probe_point *result,
310 const char *target, bool uprobes)
312 struct map *map = NULL;
317 /* This can work only for function-name based one */
318 if (!pp->function || pp->file)
321 map = get_target_map(target, uprobes);
325 /* Find the address of given function */
326 map__for_each_symbol_by_name(map, pp->function, sym) {
328 address = sym->start;
330 address = map->unmap_ip(map, sym->start);
337 pr_debug("Symbol %s address found : %" PRIx64 "\n",
338 pp->function, address);
340 ret = debuginfo__find_probe_point(dinfo, (unsigned long)address,
343 ret = (!ret) ? -ENOENT : ret;
345 result->offset += pp->offset;
346 result->line += pp->line;
347 result->retprobe = pp->retprobe;
352 put_target_map(map, uprobes);
357 static int get_alternative_probe_event(struct debuginfo *dinfo,
358 struct perf_probe_event *pev,
359 struct perf_probe_point *tmp)
363 memcpy(tmp, &pev->point, sizeof(*tmp));
364 memset(&pev->point, 0, sizeof(pev->point));
365 ret = find_alternative_probe_point(dinfo, tmp, &pev->point,
366 pev->target, pev->uprobes);
368 memcpy(&pev->point, tmp, sizeof(*tmp));
373 static int get_alternative_line_range(struct debuginfo *dinfo,
374 struct line_range *lr,
375 const char *target, bool user)
377 struct perf_probe_point pp = { .function = lr->function,
380 struct perf_probe_point result;
383 memset(&result, 0, sizeof(result));
385 if (lr->end != INT_MAX)
386 len = lr->end - lr->start;
387 ret = find_alternative_probe_point(dinfo, &pp, &result,
390 lr->function = result.function;
391 lr->file = result.file;
392 lr->start = result.line;
393 if (lr->end != INT_MAX)
394 lr->end = lr->start + len;
395 clear_perf_probe_point(&pp);
400 /* Open new debuginfo of given module */
401 static struct debuginfo *open_debuginfo(const char *module, bool silent)
403 const char *path = module;
404 char reason[STRERR_BUFSIZE];
405 struct debuginfo *ret = NULL;
406 struct dso *dso = NULL;
409 if (!module || !strchr(module, '/')) {
410 err = kernel_get_module_dso(module, &dso);
412 if (!dso || dso->load_errno == 0) {
413 if (!strerror_r(-err, reason, STRERR_BUFSIZE))
414 strcpy(reason, "(unknown)");
416 dso__strerror_load(dso, reason, STRERR_BUFSIZE);
418 pr_err("Failed to find the path for %s: %s\n",
419 module ?: "kernel", reason);
422 path = dso->long_name;
424 ret = debuginfo__new(path);
425 if (!ret && !silent) {
426 pr_warning("The %s file has no debug information.\n", path);
427 if (!module || !strtailcmp(path, ".ko"))
428 pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, ");
430 pr_warning("Rebuild with -g, ");
431 pr_warning("or install an appropriate debuginfo package.\n");
436 /* For caching the last debuginfo */
437 static struct debuginfo *debuginfo_cache;
438 static char *debuginfo_cache_path;
440 static struct debuginfo *debuginfo_cache__open(const char *module, bool silent)
442 if ((debuginfo_cache_path && !strcmp(debuginfo_cache_path, module)) ||
443 (!debuginfo_cache_path && !module && debuginfo_cache))
446 /* Copy module path */
447 free(debuginfo_cache_path);
449 debuginfo_cache_path = strdup(module);
450 if (!debuginfo_cache_path) {
451 debuginfo__delete(debuginfo_cache);
452 debuginfo_cache = NULL;
457 debuginfo_cache = open_debuginfo(module, silent);
458 if (!debuginfo_cache)
459 zfree(&debuginfo_cache_path);
461 return debuginfo_cache;
464 static void debuginfo_cache__exit(void)
466 debuginfo__delete(debuginfo_cache);
467 debuginfo_cache = NULL;
468 zfree(&debuginfo_cache_path);
472 static int get_text_start_address(const char *exec, unsigned long *address)
477 int fd, ret = -ENOENT;
479 fd = open(exec, O_RDONLY);
483 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
487 if (gelf_getehdr(elf, &ehdr) == NULL)
490 if (!elf_section_by_name(elf, &ehdr, &shdr, ".text", NULL))
493 *address = shdr.sh_addr - shdr.sh_offset;
501 * Convert trace point to probe point with debuginfo
503 static int find_perf_probe_point_from_dwarf(struct probe_trace_point *tp,
504 struct perf_probe_point *pp,
507 struct debuginfo *dinfo = NULL;
508 unsigned long stext = 0;
509 u64 addr = tp->address;
512 /* convert the address to dwarf address */
518 ret = get_text_start_address(tp->module, &stext);
523 addr = kernel_get_symbol_address_by_name(tp->symbol, false);
529 pr_debug("try to find information at %" PRIx64 " in %s\n", addr,
530 tp->module ? : "kernel");
532 dinfo = debuginfo_cache__open(tp->module, verbose == 0);
534 ret = debuginfo__find_probe_point(dinfo,
535 (unsigned long)addr, pp);
540 pp->retprobe = tp->retprobe;
544 pr_debug("Failed to find corresponding probes from debuginfo.\n");
545 return ret ? : -ENOENT;
548 static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs,
549 int ntevs, const char *exec)
552 unsigned long stext = 0;
557 ret = get_text_start_address(exec, &stext);
561 for (i = 0; i < ntevs && ret >= 0; i++) {
562 /* point.address is the addres of point.symbol + point.offset */
563 tevs[i].point.address -= stext;
564 tevs[i].point.module = strdup(exec);
565 if (!tevs[i].point.module) {
569 tevs[i].uprobes = true;
575 static int add_module_to_probe_trace_events(struct probe_trace_event *tevs,
576 int ntevs, const char *module)
584 tmp = strrchr(module, '/');
586 /* This is a module path -- get the module name */
587 module = strdup(tmp + 1);
590 tmp = strchr(module, '.');
593 tmp = (char *)module; /* For free() */
596 for (i = 0; i < ntevs; i++) {
597 tevs[i].point.module = strdup(module);
598 if (!tevs[i].point.module) {
608 /* Post processing the probe events */
609 static int post_process_probe_trace_events(struct probe_trace_event *tevs,
610 int ntevs, const char *module,
613 struct ref_reloc_sym *reloc_sym;
618 return add_exec_to_probe_trace_events(tevs, ntevs, module);
620 /* Note that currently ref_reloc_sym based probe is not for drivers */
622 return add_module_to_probe_trace_events(tevs, ntevs, module);
624 reloc_sym = kernel_get_ref_reloc_sym();
626 pr_warning("Relocated base symbol is not found!\n");
630 for (i = 0; i < ntevs; i++) {
631 if (!tevs[i].point.address || tevs[i].point.retprobe)
633 /* If we found a wrong one, mark it by NULL symbol */
634 if (kprobe_warn_out_range(tevs[i].point.symbol,
635 tevs[i].point.address)) {
639 tmp = strdup(reloc_sym->name);
643 /* If we have no realname, use symbol for it */
644 if (!tevs[i].point.realname)
645 tevs[i].point.realname = tevs[i].point.symbol;
647 free(tevs[i].point.symbol);
648 tevs[i].point.symbol = tmp;
649 tevs[i].point.offset = tevs[i].point.address -
650 reloc_sym->unrelocated_addr;
655 /* Try to find perf_probe_event with debuginfo */
656 static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
657 struct probe_trace_event **tevs)
659 bool need_dwarf = perf_probe_event_need_dwarf(pev);
660 struct perf_probe_point tmp;
661 struct debuginfo *dinfo;
664 dinfo = open_debuginfo(pev->target, !need_dwarf);
668 pr_debug("Could not open debuginfo. Try to use symbols.\n");
672 pr_debug("Try to find probe point from debuginfo.\n");
673 /* Searching trace events corresponding to a probe event */
674 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
676 if (ntevs == 0) { /* Not found, retry with an alternative */
677 ret = get_alternative_probe_event(dinfo, pev, &tmp);
679 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
681 * Write back to the original probe_event for
682 * setting appropriate (user given) event name
684 clear_perf_probe_point(&pev->point);
685 memcpy(&pev->point, &tmp, sizeof(tmp));
689 debuginfo__delete(dinfo);
691 if (ntevs > 0) { /* Succeeded to find trace events */
692 pr_debug("Found %d probe_trace_events.\n", ntevs);
693 ret = post_process_probe_trace_events(*tevs, ntevs,
694 pev->target, pev->uprobes);
695 if (ret < 0 || ret == ntevs) {
696 clear_probe_trace_events(*tevs, ntevs);
700 return ret < 0 ? ret : ntevs;
705 if (ntevs == 0) { /* No error but failed to find probe point. */
706 pr_warning("Probe point '%s' not found.\n",
707 synthesize_perf_probe_point(&pev->point));
710 /* Error path : ntevs < 0 */
711 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
712 if (ntevs == -EBADF) {
713 pr_warning("Warning: No dwarf info found in the vmlinux - "
714 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
716 pr_debug("Trying to use symbols.\n");
723 #define LINEBUF_SIZE 256
724 #define NR_ADDITIONAL_LINES 2
726 static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
728 char buf[LINEBUF_SIZE], sbuf[STRERR_BUFSIZE];
729 const char *color = show_num ? "" : PERF_COLOR_BLUE;
730 const char *prefix = NULL;
733 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
738 prefix = show_num ? "%7d " : " ";
739 color_fprintf(stdout, color, prefix, l);
741 color_fprintf(stdout, color, "%s", buf);
743 } while (strchr(buf, '\n') == NULL);
748 pr_warning("File read error: %s\n",
749 strerror_r(errno, sbuf, sizeof(sbuf)));
755 static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
757 int rv = __show_one_line(fp, l, skip, show_num);
759 pr_warning("Source file is shorter than expected.\n");
765 #define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
766 #define show_one_line(f,l) _show_one_line(f,l,false,false)
767 #define skip_one_line(f,l) _show_one_line(f,l,true,false)
768 #define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
771 * Show line-range always requires debuginfo to find source file and
774 static int __show_line_range(struct line_range *lr, const char *module,
779 struct debuginfo *dinfo;
783 char sbuf[STRERR_BUFSIZE];
785 /* Search a line range */
786 dinfo = open_debuginfo(module, false);
790 ret = debuginfo__find_line_range(dinfo, lr);
791 if (!ret) { /* Not found, retry with an alternative */
792 ret = get_alternative_line_range(dinfo, lr, module, user);
794 ret = debuginfo__find_line_range(dinfo, lr);
796 debuginfo__delete(dinfo);
797 if (ret == 0 || ret == -ENOENT) {
798 pr_warning("Specified source line is not found.\n");
800 } else if (ret < 0) {
801 pr_warning("Debuginfo analysis failed.\n");
805 /* Convert source file path */
807 ret = get_real_path(tmp, lr->comp_dir, &lr->path);
809 /* Free old path when new path is assigned */
814 pr_warning("Failed to find source file path.\n");
821 fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
822 lr->start - lr->offset);
824 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
826 fp = fopen(lr->path, "r");
828 pr_warning("Failed to open %s: %s\n", lr->path,
829 strerror_r(errno, sbuf, sizeof(sbuf)));
832 /* Skip to starting line number */
833 while (l < lr->start) {
834 ret = skip_one_line(fp, l++);
839 intlist__for_each(ln, lr->line_list) {
840 for (; ln->i > l; l++) {
841 ret = show_one_line(fp, l - lr->offset);
845 ret = show_one_line_with_num(fp, l++ - lr->offset);
850 if (lr->end == INT_MAX)
851 lr->end = l + NR_ADDITIONAL_LINES;
852 while (l <= lr->end) {
853 ret = show_one_line_or_eof(fp, l++ - lr->offset);
862 int show_line_range(struct line_range *lr, const char *module, bool user)
866 ret = init_symbol_maps(user);
869 ret = __show_line_range(lr, module, user);
875 static int show_available_vars_at(struct debuginfo *dinfo,
876 struct perf_probe_event *pev,
877 struct strfilter *_filter)
881 struct str_node *node;
882 struct variable_list *vls = NULL, *vl;
883 struct perf_probe_point tmp;
886 buf = synthesize_perf_probe_point(&pev->point);
889 pr_debug("Searching variables at %s\n", buf);
891 ret = debuginfo__find_available_vars_at(dinfo, pev, &vls);
892 if (!ret) { /* Not found, retry with an alternative */
893 ret = get_alternative_probe_event(dinfo, pev, &tmp);
895 ret = debuginfo__find_available_vars_at(dinfo, pev,
897 /* Release the old probe_point */
898 clear_perf_probe_point(&tmp);
902 if (ret == 0 || ret == -ENOENT) {
903 pr_err("Failed to find the address of %s\n", buf);
906 pr_warning("Debuginfo analysis failed.\n");
910 /* Some variables are found */
911 fprintf(stdout, "Available variables at %s\n", buf);
912 for (i = 0; i < ret; i++) {
915 * A probe point might be converted to
916 * several trace points.
918 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
920 zfree(&vl->point.symbol);
923 strlist__for_each(node, vl->vars) {
924 var = strchr(node->s, '\t') + 1;
925 if (strfilter__compare(_filter, var)) {
926 fprintf(stdout, "\t\t%s\n", node->s);
930 strlist__delete(vl->vars);
933 fprintf(stdout, "\t\t(No matched variables)\n");
941 /* Show available variables on given probe point */
942 int show_available_vars(struct perf_probe_event *pevs, int npevs,
943 struct strfilter *_filter)
946 struct debuginfo *dinfo;
948 ret = init_symbol_maps(pevs->uprobes);
952 dinfo = open_debuginfo(pevs->target, false);
960 for (i = 0; i < npevs && ret >= 0; i++)
961 ret = show_available_vars_at(dinfo, &pevs[i], _filter);
963 debuginfo__delete(dinfo);
969 #else /* !HAVE_DWARF_SUPPORT */
971 static void debuginfo_cache__exit(void)
976 find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused,
977 struct perf_probe_point *pp __maybe_unused,
978 bool is_kprobe __maybe_unused)
983 static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
984 struct probe_trace_event **tevs __maybe_unused)
986 if (perf_probe_event_need_dwarf(pev)) {
987 pr_warning("Debuginfo-analysis is not supported.\n");
994 int show_line_range(struct line_range *lr __maybe_unused,
995 const char *module __maybe_unused,
996 bool user __maybe_unused)
998 pr_warning("Debuginfo-analysis is not supported.\n");
1002 int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
1003 int npevs __maybe_unused,
1004 struct strfilter *filter __maybe_unused)
1006 pr_warning("Debuginfo-analysis is not supported.\n");
1011 void line_range__clear(struct line_range *lr)
1017 intlist__delete(lr->line_list);
1018 memset(lr, 0, sizeof(*lr));
1021 int line_range__init(struct line_range *lr)
1023 memset(lr, 0, sizeof(*lr));
1024 lr->line_list = intlist__new(NULL);
1031 static int parse_line_num(char **ptr, int *val, const char *what)
1033 const char *start = *ptr;
1036 *val = strtol(*ptr, ptr, 0);
1037 if (errno || *ptr == start) {
1038 semantic_error("'%s' is not a valid number.\n", what);
1044 /* Check the name is good for event, group or function */
1045 static bool is_c_func_name(const char *name)
1047 if (!isalpha(*name) && *name != '_')
1049 while (*++name != '\0') {
1050 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
1057 * Stuff 'lr' according to the line range described by 'arg'.
1058 * The line range syntax is described by:
1060 * SRC[:SLN[+NUM|-ELN]]
1061 * FNC[@SRC][:SLN[+NUM|-ELN]]
1063 int parse_line_range_desc(const char *arg, struct line_range *lr)
1065 char *range, *file, *name = strdup(arg);
1074 range = strchr(name, ':');
1078 err = parse_line_num(&range, &lr->start, "start line");
1082 if (*range == '+' || *range == '-') {
1083 const char c = *range++;
1085 err = parse_line_num(&range, &lr->end, "end line");
1090 lr->end += lr->start;
1092 * Adjust the number of lines here.
1093 * If the number of lines == 1, the
1094 * the end of line should be equal to
1095 * the start of line.
1101 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
1104 if (lr->start > lr->end) {
1105 semantic_error("Start line must be smaller"
1106 " than end line.\n");
1109 if (*range != '\0') {
1110 semantic_error("Tailing with invalid str '%s'.\n", range);
1115 file = strchr(name, '@');
1118 lr->file = strdup(++file);
1119 if (lr->file == NULL) {
1123 lr->function = name;
1124 } else if (strchr(name, '/') || strchr(name, '.'))
1126 else if (is_c_func_name(name))/* We reuse it for checking funcname */
1127 lr->function = name;
1128 else { /* Invalid name */
1129 semantic_error("'%s' is not a valid function name.\n", name);
1140 /* Parse probepoint definition. */
1141 static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
1143 struct perf_probe_point *pp = &pev->point;
1146 bool file_spec = false;
1149 * perf probe [EVENT=]SRC[:LN|;PTN]
1150 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
1152 * TODO:Group name support
1157 ptr = strpbrk(arg, ";=@+%");
1158 if (ptr && *ptr == '=') { /* Event name */
1161 if (strchr(arg, ':')) {
1162 semantic_error("Group name is not supported yet.\n");
1165 if (!is_c_func_name(arg)) {
1166 semantic_error("%s is bad for event name -it must "
1167 "follow C symbol-naming rule.\n", arg);
1170 pev->event = strdup(arg);
1171 if (pev->event == NULL)
1178 * Check arg is function or file name and copy it.
1180 * We consider arg to be a file spec if and only if it satisfies
1181 * all of the below criteria::
1182 * - it does not include any of "+@%",
1183 * - it includes one of ":;", and
1184 * - it has a period '.' in the name.
1186 * Otherwise, we consider arg to be a function specification.
1188 if (!strpbrk(arg, "+@%") && (ptr = strpbrk(arg, ";:")) != NULL) {
1189 /* This is a file spec if it includes a '.' before ; or : */
1190 if (memchr(arg, '.', ptr - arg))
1194 ptr = strpbrk(arg, ";:+@%");
1209 /* Parse other options */
1213 if (c == ';') { /* Lazy pattern must be the last part */
1214 pp->lazy_line = strdup(arg);
1215 if (pp->lazy_line == NULL)
1219 ptr = strpbrk(arg, ";:+@%");
1225 case ':': /* Line number */
1226 pp->line = strtoul(arg, &tmp, 0);
1228 semantic_error("There is non-digit char"
1229 " in line number.\n");
1233 case '+': /* Byte offset from a symbol */
1234 pp->offset = strtoul(arg, &tmp, 0);
1236 semantic_error("There is non-digit character"
1241 case '@': /* File name */
1243 semantic_error("SRC@SRC is not allowed.\n");
1246 pp->file = strdup(arg);
1247 if (pp->file == NULL)
1250 case '%': /* Probe places */
1251 if (strcmp(arg, "return") == 0) {
1253 } else { /* Others not supported yet */
1254 semantic_error("%%%s is not supported.\n", arg);
1258 default: /* Buggy case */
1259 pr_err("This program has a bug at %s:%d.\n",
1260 __FILE__, __LINE__);
1266 /* Exclusion check */
1267 if (pp->lazy_line && pp->line) {
1268 semantic_error("Lazy pattern can't be used with"
1273 if (pp->lazy_line && pp->offset) {
1274 semantic_error("Lazy pattern can't be used with offset.\n");
1278 if (pp->line && pp->offset) {
1279 semantic_error("Offset can't be used with line number.\n");
1283 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
1284 semantic_error("File always requires line number or "
1289 if (pp->offset && !pp->function) {
1290 semantic_error("Offset requires an entry function.\n");
1294 if (pp->retprobe && !pp->function) {
1295 semantic_error("Return probe requires an entry function.\n");
1299 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
1300 semantic_error("Offset/Line/Lazy pattern can't be used with "
1305 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
1306 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
1311 /* Parse perf-probe event argument */
1312 static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
1314 char *tmp, *goodname;
1315 struct perf_probe_arg_field **fieldp;
1317 pr_debug("parsing arg: %s into ", str);
1319 tmp = strchr(str, '=');
1321 arg->name = strndup(str, tmp - str);
1322 if (arg->name == NULL)
1324 pr_debug("name:%s ", arg->name);
1328 tmp = strchr(str, ':');
1329 if (tmp) { /* Type setting */
1331 arg->type = strdup(tmp + 1);
1332 if (arg->type == NULL)
1334 pr_debug("type:%s ", arg->type);
1337 tmp = strpbrk(str, "-.[");
1338 if (!is_c_varname(str) || !tmp) {
1339 /* A variable, register, symbol or special value */
1340 arg->var = strdup(str);
1341 if (arg->var == NULL)
1343 pr_debug("%s\n", arg->var);
1347 /* Structure fields or array element */
1348 arg->var = strndup(str, tmp - str);
1349 if (arg->var == NULL)
1351 goodname = arg->var;
1352 pr_debug("%s, ", arg->var);
1353 fieldp = &arg->field;
1356 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
1357 if (*fieldp == NULL)
1359 if (*tmp == '[') { /* Array */
1361 (*fieldp)->index = strtol(str + 1, &tmp, 0);
1362 (*fieldp)->ref = true;
1363 if (*tmp != ']' || tmp == str + 1) {
1364 semantic_error("Array index must be a"
1371 } else { /* Structure */
1374 (*fieldp)->ref = false;
1375 } else if (tmp[1] == '>') {
1377 (*fieldp)->ref = true;
1379 semantic_error("Argument parse error: %s\n",
1383 tmp = strpbrk(str, "-.[");
1386 (*fieldp)->name = strndup(str, tmp - str);
1387 if ((*fieldp)->name == NULL)
1390 goodname = (*fieldp)->name;
1391 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
1392 fieldp = &(*fieldp)->next;
1395 (*fieldp)->name = strdup(str);
1396 if ((*fieldp)->name == NULL)
1399 goodname = (*fieldp)->name;
1400 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
1402 /* If no name is specified, set the last field name (not array index)*/
1404 arg->name = strdup(goodname);
1405 if (arg->name == NULL)
1411 /* Parse perf-probe event command */
1412 int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
1415 int argc, i, ret = 0;
1417 argv = argv_split(cmd, &argc);
1419 pr_debug("Failed to split arguments.\n");
1422 if (argc - 1 > MAX_PROBE_ARGS) {
1423 semantic_error("Too many probe arguments (%d).\n", argc - 1);
1427 /* Parse probe point */
1428 ret = parse_perf_probe_point(argv[0], pev);
1432 /* Copy arguments and ensure return probe has no C argument */
1433 pev->nargs = argc - 1;
1434 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1435 if (pev->args == NULL) {
1439 for (i = 0; i < pev->nargs && ret >= 0; i++) {
1440 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1442 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
1443 semantic_error("You can't specify local variable for"
1454 /* Return true if this perf_probe_event requires debuginfo */
1455 bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
1459 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1462 for (i = 0; i < pev->nargs; i++)
1463 if (is_c_varname(pev->args[i].var))
1469 /* Parse probe_events event into struct probe_point */
1470 static int parse_probe_trace_command(const char *cmd,
1471 struct probe_trace_event *tev)
1473 struct probe_trace_point *tp = &tev->point;
1476 char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
1480 pr_debug("Parsing probe_events: %s\n", cmd);
1481 argv = argv_split(cmd, &argc);
1483 pr_debug("Failed to split arguments.\n");
1487 semantic_error("Too few probe arguments.\n");
1492 /* Scan event and group name. */
1493 argv0_str = strdup(argv[0]);
1494 if (argv0_str == NULL) {
1498 fmt1_str = strtok_r(argv0_str, ":", &fmt);
1499 fmt2_str = strtok_r(NULL, "/", &fmt);
1500 fmt3_str = strtok_r(NULL, " \t", &fmt);
1501 if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
1502 || fmt3_str == NULL) {
1503 semantic_error("Failed to parse event name: %s\n", argv[0]);
1508 tev->group = strdup(fmt2_str);
1509 tev->event = strdup(fmt3_str);
1510 if (tev->group == NULL || tev->event == NULL) {
1514 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
1516 tp->retprobe = (pr == 'r');
1518 /* Scan module name(if there), function name and offset */
1519 p = strchr(argv[1], ':');
1521 tp->module = strndup(argv[1], p - argv[1]);
1525 fmt1_str = strtok_r(p, "+", &fmt);
1526 if (fmt1_str[0] == '0') /* only the address started with 0x */
1527 tp->address = strtoul(fmt1_str, NULL, 0);
1529 /* Only the symbol-based probe has offset */
1530 tp->symbol = strdup(fmt1_str);
1531 if (tp->symbol == NULL) {
1535 fmt2_str = strtok_r(NULL, "", &fmt);
1536 if (fmt2_str == NULL)
1539 tp->offset = strtoul(fmt2_str, NULL, 10);
1542 tev->nargs = argc - 2;
1543 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
1544 if (tev->args == NULL) {
1548 for (i = 0; i < tev->nargs; i++) {
1549 p = strchr(argv[i + 2], '=');
1550 if (p) /* We don't need which register is assigned. */
1554 tev->args[i].name = strdup(argv[i + 2]);
1555 /* TODO: parse regs and offset */
1556 tev->args[i].value = strdup(p);
1557 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1569 /* Compose only probe arg */
1570 int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
1572 struct perf_probe_arg_field *field = pa->field;
1576 if (pa->name && pa->var)
1577 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
1579 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
1586 if (field->name[0] == '[')
1587 ret = e_snprintf(tmp, len, "%s", field->name);
1589 ret = e_snprintf(tmp, len, "%s%s",
1590 field->ref ? "->" : ".", field->name);
1595 field = field->next;
1599 ret = e_snprintf(tmp, len, ":%s", pa->type);
1608 pr_debug("Failed to synthesize perf probe argument: %d\n", ret);
1612 /* Compose only probe point (not argument) */
1613 static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
1616 char offs[32] = "", line[32] = "", file[32] = "";
1619 buf = zalloc(MAX_CMDLEN);
1625 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
1630 ret = e_snprintf(line, 32, ":%d", pp->line);
1638 tmp = strchr(pp->file + len - 30, '/');
1639 tmp = tmp ? tmp + 1 : pp->file + len - 30;
1641 ret = e_snprintf(file, 32, "@%s", tmp);
1647 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
1648 offs, pp->retprobe ? "%return" : "", line,
1651 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
1657 pr_debug("Failed to synthesize perf probe point: %d\n", ret);
1663 char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1668 buf = synthesize_perf_probe_point(&pev->point);
1673 for (i = 0; i < pev->nargs; i++) {
1674 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
1687 static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
1688 char **buf, size_t *buflen,
1693 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
1699 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
1711 static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
1712 char *buf, size_t buflen)
1714 struct probe_trace_arg_ref *ref = arg->ref;
1718 /* Argument name or separator */
1720 ret = e_snprintf(buf, buflen, " %s=", arg->name);
1722 ret = e_snprintf(buf, buflen, " ");
1728 /* Special case: @XXX */
1729 if (arg->value[0] == '@' && arg->ref)
1732 /* Dereferencing arguments */
1734 depth = __synthesize_probe_trace_arg_ref(ref, &buf,
1740 /* Print argument value */
1741 if (arg->value[0] == '@' && arg->ref)
1742 ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
1745 ret = e_snprintf(buf, buflen, "%s", arg->value);
1753 ret = e_snprintf(buf, buflen, ")");
1759 /* Print argument type */
1761 ret = e_snprintf(buf, buflen, ":%s", arg->type);
1770 char *synthesize_probe_trace_command(struct probe_trace_event *tev)
1772 struct probe_trace_point *tp = &tev->point;
1776 buf = zalloc(MAX_CMDLEN);
1780 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
1781 tev->group, tev->event);
1785 /* Uprobes must have tp->address and tp->module */
1786 if (tev->uprobes && (!tp->address || !tp->module))
1789 /* Use the tp->address for uprobes */
1791 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s:0x%lx",
1792 tp->module, tp->address);
1794 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s%s%s+%lu",
1795 tp->module ?: "", tp->module ? ":" : "",
1796 tp->symbol, tp->offset);
1802 for (i = 0; i < tev->nargs; i++) {
1803 ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
1816 static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
1817 struct perf_probe_point *pp,
1820 struct symbol *sym = NULL;
1826 map = dso__new_map(tp->module);
1830 sym = map__find_symbol(map, addr, NULL);
1832 addr = kernel_get_symbol_address_by_name(tp->symbol, true);
1835 sym = __find_kernel_function(addr, &map);
1841 pp->retprobe = tp->retprobe;
1842 pp->offset = addr - map->unmap_ip(map, sym->start);
1843 pp->function = strdup(sym->name);
1844 ret = pp->function ? 0 : -ENOMEM;
1847 if (map && !is_kprobe) {
1854 static int convert_to_perf_probe_point(struct probe_trace_point *tp,
1855 struct perf_probe_point *pp,
1861 ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe);
1864 ret = find_perf_probe_point_from_map(tp, pp, is_kprobe);
1868 pr_debug("Failed to find probe point from both of dwarf and map.\n");
1871 pp->function = strdup(tp->symbol);
1872 pp->offset = tp->offset;
1873 } else if (!tp->module && !is_kprobe) {
1874 ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address);
1877 pp->function = strdup(buf);
1880 if (pp->function == NULL)
1883 pp->retprobe = tp->retprobe;
1888 static int convert_to_perf_probe_event(struct probe_trace_event *tev,
1889 struct perf_probe_event *pev, bool is_kprobe)
1894 /* Convert event/group name */
1895 pev->event = strdup(tev->event);
1896 pev->group = strdup(tev->group);
1897 if (pev->event == NULL || pev->group == NULL)
1900 /* Convert trace_point to probe_point */
1901 ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
1905 /* Convert trace_arg to probe_arg */
1906 pev->nargs = tev->nargs;
1907 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1908 if (pev->args == NULL)
1910 for (i = 0; i < tev->nargs && ret >= 0; i++) {
1911 if (tev->args[i].name)
1912 pev->args[i].name = strdup(tev->args[i].name);
1914 ret = synthesize_probe_trace_arg(&tev->args[i],
1916 pev->args[i].name = strdup(buf);
1918 if (pev->args[i].name == NULL && ret >= 0)
1923 clear_perf_probe_event(pev);
1928 void clear_perf_probe_event(struct perf_probe_event *pev)
1930 struct perf_probe_arg_field *field, *next;
1936 clear_perf_probe_point(&pev->point);
1938 for (i = 0; i < pev->nargs; i++) {
1939 free(pev->args[i].name);
1940 free(pev->args[i].var);
1941 free(pev->args[i].type);
1942 field = pev->args[i].field;
1945 zfree(&field->name);
1951 memset(pev, 0, sizeof(*pev));
1954 static void clear_probe_trace_event(struct probe_trace_event *tev)
1956 struct probe_trace_arg_ref *ref, *next;
1961 free(tev->point.symbol);
1962 free(tev->point.realname);
1963 free(tev->point.module);
1964 for (i = 0; i < tev->nargs; i++) {
1965 free(tev->args[i].name);
1966 free(tev->args[i].value);
1967 free(tev->args[i].type);
1968 ref = tev->args[i].ref;
1976 memset(tev, 0, sizeof(*tev));
1979 static void print_open_warning(int err, bool is_kprobe)
1981 char sbuf[STRERR_BUFSIZE];
1983 if (err == -ENOENT) {
1987 config = "CONFIG_UPROBE_EVENTS";
1989 config = "CONFIG_KPROBE_EVENTS";
1991 pr_warning("%cprobe_events file does not exist"
1992 " - please rebuild kernel with %s.\n",
1993 is_kprobe ? 'k' : 'u', config);
1994 } else if (err == -ENOTSUP)
1995 pr_warning("Tracefs or debugfs is not mounted.\n");
1997 pr_warning("Failed to open %cprobe_events: %s\n",
1998 is_kprobe ? 'k' : 'u',
1999 strerror_r(-err, sbuf, sizeof(sbuf)));
2002 static void print_both_open_warning(int kerr, int uerr)
2004 /* Both kprobes and uprobes are disabled, warn it. */
2005 if (kerr == -ENOTSUP && uerr == -ENOTSUP)
2006 pr_warning("Tracefs or debugfs is not mounted.\n");
2007 else if (kerr == -ENOENT && uerr == -ENOENT)
2008 pr_warning("Please rebuild kernel with CONFIG_KPROBE_EVENTS "
2009 "or/and CONFIG_UPROBE_EVENTS.\n");
2011 char sbuf[STRERR_BUFSIZE];
2012 pr_warning("Failed to open kprobe events: %s.\n",
2013 strerror_r(-kerr, sbuf, sizeof(sbuf)));
2014 pr_warning("Failed to open uprobe events: %s.\n",
2015 strerror_r(-uerr, sbuf, sizeof(sbuf)));
2019 static int open_probe_events(const char *trace_file, bool readwrite)
2022 const char *__debugfs;
2023 const char *tracing_dir = "";
2026 __debugfs = tracefs_find_mountpoint();
2027 if (__debugfs == NULL) {
2028 tracing_dir = "tracing/";
2030 __debugfs = debugfs_find_mountpoint();
2031 if (__debugfs == NULL)
2035 ret = e_snprintf(buf, PATH_MAX, "%s/%s%s",
2036 __debugfs, tracing_dir, trace_file);
2038 pr_debug("Opening %s write=%d\n", buf, readwrite);
2039 if (readwrite && !probe_event_dry_run)
2040 ret = open(buf, O_RDWR | O_APPEND, 0);
2042 ret = open(buf, O_RDONLY, 0);
2050 static int open_kprobe_events(bool readwrite)
2052 return open_probe_events("kprobe_events", readwrite);
2055 static int open_uprobe_events(bool readwrite)
2057 return open_probe_events("uprobe_events", readwrite);
2060 /* Get raw string list of current kprobe_events or uprobe_events */
2061 static struct strlist *get_probe_trace_command_rawlist(int fd)
2065 char buf[MAX_CMDLEN];
2069 sl = strlist__new(true, NULL);
2071 fp = fdopen(dup(fd), "r");
2073 p = fgets(buf, MAX_CMDLEN, fp);
2077 idx = strlen(p) - 1;
2080 ret = strlist__add(sl, buf);
2082 pr_debug("strlist__add failed (%d)\n", ret);
2083 strlist__delete(sl);
2092 struct kprobe_blacklist_node {
2093 struct list_head list;
2094 unsigned long start;
2099 static void kprobe_blacklist__delete(struct list_head *blacklist)
2101 struct kprobe_blacklist_node *node;
2103 while (!list_empty(blacklist)) {
2104 node = list_first_entry(blacklist,
2105 struct kprobe_blacklist_node, list);
2106 list_del(&node->list);
2112 static int kprobe_blacklist__load(struct list_head *blacklist)
2114 struct kprobe_blacklist_node *node;
2115 const char *__debugfs = debugfs_find_mountpoint();
2116 char buf[PATH_MAX], *p;
2120 if (__debugfs == NULL)
2123 ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs);
2127 fp = fopen(buf, "r");
2132 while (fgets(buf, PATH_MAX, fp)) {
2133 node = zalloc(sizeof(*node));
2138 INIT_LIST_HEAD(&node->list);
2139 list_add_tail(&node->list, blacklist);
2140 if (sscanf(buf, "0x%lx-0x%lx", &node->start, &node->end) != 2) {
2144 p = strchr(buf, '\t');
2147 if (p[strlen(p) - 1] == '\n')
2148 p[strlen(p) - 1] = '\0';
2150 p = (char *)"unknown";
2151 node->symbol = strdup(p);
2152 if (!node->symbol) {
2156 pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
2157 node->start, node->end, node->symbol);
2161 kprobe_blacklist__delete(blacklist);
2167 static struct kprobe_blacklist_node *
2168 kprobe_blacklist__find_by_address(struct list_head *blacklist,
2169 unsigned long address)
2171 struct kprobe_blacklist_node *node;
2173 list_for_each_entry(node, blacklist, list) {
2174 if (node->start <= address && address <= node->end)
2181 static LIST_HEAD(kprobe_blacklist);
2183 static void kprobe_blacklist__init(void)
2185 if (!list_empty(&kprobe_blacklist))
2188 if (kprobe_blacklist__load(&kprobe_blacklist) < 0)
2189 pr_debug("No kprobe blacklist support, ignored\n");
2192 static void kprobe_blacklist__release(void)
2194 kprobe_blacklist__delete(&kprobe_blacklist);
2197 static bool kprobe_blacklist__listed(unsigned long address)
2199 return !!kprobe_blacklist__find_by_address(&kprobe_blacklist, address);
2202 static int perf_probe_event__sprintf(const char *group, const char *event,
2203 struct perf_probe_event *pev,
2205 struct strbuf *result)
2211 /* Synthesize only event probe point */
2212 place = synthesize_perf_probe_point(&pev->point);
2216 ret = e_snprintf(buf, 128, "%s:%s", group, event);
2220 strbuf_addf(result, " %-20s (on %s", buf, place);
2222 strbuf_addf(result, " in %s", module);
2224 if (pev->nargs > 0) {
2225 strbuf_addstr(result, " with");
2226 for (i = 0; i < pev->nargs; i++) {
2227 ret = synthesize_perf_probe_arg(&pev->args[i],
2231 strbuf_addf(result, " %s", buf);
2234 strbuf_addch(result, ')');
2241 static int show_perf_probe_event(const char *group, const char *event,
2242 struct perf_probe_event *pev,
2243 const char *module, bool use_stdout)
2245 struct strbuf buf = STRBUF_INIT;
2248 ret = perf_probe_event__sprintf(group, event, pev, module, &buf);
2251 printf("%s\n", buf.buf);
2253 pr_info("%s\n", buf.buf);
2255 strbuf_release(&buf);
2260 static bool filter_probe_trace_event(struct probe_trace_event *tev,
2261 struct strfilter *filter)
2265 /* At first, check the event name itself */
2266 if (strfilter__compare(filter, tev->event))
2269 /* Next, check the combination of name and group */
2270 if (e_snprintf(tmp, 128, "%s:%s", tev->group, tev->event) < 0)
2272 return strfilter__compare(filter, tmp);
2275 static int __show_perf_probe_events(int fd, bool is_kprobe,
2276 struct strfilter *filter)
2279 struct probe_trace_event tev;
2280 struct perf_probe_event pev;
2281 struct strlist *rawlist;
2282 struct str_node *ent;
2284 memset(&tev, 0, sizeof(tev));
2285 memset(&pev, 0, sizeof(pev));
2287 rawlist = get_probe_trace_command_rawlist(fd);
2291 strlist__for_each(ent, rawlist) {
2292 ret = parse_probe_trace_command(ent->s, &tev);
2294 if (!filter_probe_trace_event(&tev, filter))
2296 ret = convert_to_perf_probe_event(&tev, &pev,
2300 ret = show_perf_probe_event(pev.group, pev.event,
2301 &pev, tev.point.module,
2305 clear_perf_probe_event(&pev);
2306 clear_probe_trace_event(&tev);
2310 strlist__delete(rawlist);
2311 /* Cleanup cached debuginfo if needed */
2312 debuginfo_cache__exit();
2317 /* List up current perf-probe events */
2318 int show_perf_probe_events(struct strfilter *filter)
2320 int kp_fd, up_fd, ret;
2324 ret = init_symbol_maps(false);
2328 kp_fd = open_kprobe_events(false);
2330 ret = __show_perf_probe_events(kp_fd, true, filter);
2336 up_fd = open_uprobe_events(false);
2337 if (kp_fd < 0 && up_fd < 0) {
2338 print_both_open_warning(kp_fd, up_fd);
2344 ret = __show_perf_probe_events(up_fd, false, filter);
2352 /* Get current perf-probe event names */
2353 static struct strlist *get_probe_trace_event_names(int fd, bool include_group)
2356 struct strlist *sl, *rawlist;
2357 struct str_node *ent;
2358 struct probe_trace_event tev;
2361 memset(&tev, 0, sizeof(tev));
2362 rawlist = get_probe_trace_command_rawlist(fd);
2365 sl = strlist__new(true, NULL);
2366 strlist__for_each(ent, rawlist) {
2367 ret = parse_probe_trace_command(ent->s, &tev);
2370 if (include_group) {
2371 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
2374 ret = strlist__add(sl, buf);
2376 ret = strlist__add(sl, tev.event);
2377 clear_probe_trace_event(&tev);
2381 strlist__delete(rawlist);
2384 strlist__delete(sl);
2390 static int write_probe_trace_event(int fd, struct probe_trace_event *tev)
2393 char *buf = synthesize_probe_trace_command(tev);
2394 char sbuf[STRERR_BUFSIZE];
2397 pr_debug("Failed to synthesize probe trace event.\n");
2401 pr_debug("Writing event: %s\n", buf);
2402 if (!probe_event_dry_run) {
2403 ret = write(fd, buf, strlen(buf));
2406 pr_warning("Failed to write event: %s\n",
2407 strerror_r(errno, sbuf, sizeof(sbuf)));
2414 static int get_new_event_name(char *buf, size_t len, const char *base,
2415 struct strlist *namelist, bool allow_suffix)
2424 ret = e_snprintf(buf, len, "%s", base);
2426 pr_debug("snprintf() failed: %d\n", ret);
2429 /* Cut off the postfixes (e.g. .const, .isra)*/
2430 p = strchr(buf, '.');
2433 if (!strlist__has_entry(namelist, buf))
2436 if (!allow_suffix) {
2437 pr_warning("Error: event \"%s\" already exists. "
2438 "(Use -f to force duplicates.)\n", base);
2442 /* Try to add suffix */
2443 for (i = 1; i < MAX_EVENT_INDEX; i++) {
2444 ret = e_snprintf(buf, len, "%s_%d", base, i);
2446 pr_debug("snprintf() failed: %d\n", ret);
2449 if (!strlist__has_entry(namelist, buf))
2452 if (i == MAX_EVENT_INDEX) {
2453 pr_warning("Too many events are on the same function.\n");
2460 /* Warn if the current kernel's uprobe implementation is old */
2461 static void warn_uprobe_event_compat(struct probe_trace_event *tev)
2464 char *buf = synthesize_probe_trace_command(tev);
2466 /* Old uprobe event doesn't support memory dereference */
2467 if (!tev->uprobes || tev->nargs == 0 || !buf)
2470 for (i = 0; i < tev->nargs; i++)
2471 if (strglobmatch(tev->args[i].value, "[$@+-]*")) {
2472 pr_warning("Please upgrade your kernel to at least "
2473 "3.14 to have access to feature %s\n",
2474 tev->args[i].value);
2481 static int __add_probe_trace_events(struct perf_probe_event *pev,
2482 struct probe_trace_event *tevs,
2483 int ntevs, bool allow_suffix)
2486 struct probe_trace_event *tev = NULL;
2488 const char *event = NULL, *group = NULL;
2489 struct strlist *namelist;
2493 fd = open_uprobe_events(true);
2495 fd = open_kprobe_events(true);
2498 print_open_warning(fd, !pev->uprobes);
2502 /* Get current event names */
2503 namelist = get_probe_trace_event_names(fd, false);
2505 pr_debug("Failed to get current event list.\n");
2510 safename = (pev->point.function && !strisglob(pev->point.function));
2512 pr_info("Added new event%s\n", (ntevs > 1) ? "s:" : ":");
2513 for (i = 0; i < ntevs; i++) {
2515 /* Skip if the symbol is out of .text or blacklisted */
2516 if (!tev->point.symbol)
2523 event = pev->point.function;
2525 event = tev->point.realname;
2529 group = PERFPROBE_GROUP;
2531 /* Get an unused new event name */
2532 ret = get_new_event_name(buf, 64, event,
2533 namelist, allow_suffix);
2538 tev->event = strdup(event);
2539 tev->group = strdup(group);
2540 if (tev->event == NULL || tev->group == NULL) {
2544 ret = write_probe_trace_event(fd, tev);
2547 /* Add added event name to namelist */
2548 strlist__add(namelist, event);
2550 /* We use tev's name for showing new events */
2551 show_perf_probe_event(tev->group, tev->event, pev,
2552 tev->point.module, false);
2553 /* Save the last valid name */
2558 * Probes after the first probe which comes from same
2559 * user input are always allowed to add suffix, because
2560 * there might be several addresses corresponding to
2563 allow_suffix = true;
2565 if (ret == -EINVAL && pev->uprobes)
2566 warn_uprobe_event_compat(tev);
2568 /* Note that it is possible to skip all events because of blacklist */
2569 if (ret >= 0 && event) {
2570 /* Show how to use the event. */
2571 pr_info("\nYou can now use it in all perf tools, such as:\n\n");
2572 pr_info("\tperf record -e %s:%s -aR sleep 1\n\n", group, event);
2575 strlist__delete(namelist);
2581 static int find_probe_functions(struct map *map, char *name,
2582 struct symbol **syms)
2586 struct rb_node *tmp;
2588 if (map__load(map, NULL) < 0)
2591 map__for_each_symbol(map, sym, tmp) {
2592 if (strglobmatch(sym->name, name)) {
2594 if (syms && found < probe_conf.max_probes)
2595 syms[found - 1] = sym;
2602 #define strdup_or_goto(str, label) \
2603 ({ char *__p = strdup(str); if (!__p) goto label; __p; })
2605 void __weak arch__fix_tev_from_maps(struct perf_probe_event *pev __maybe_unused,
2606 struct probe_trace_event *tev __maybe_unused,
2607 struct map *map __maybe_unused) { }
2610 * Find probe function addresses from map.
2611 * Return an error or the number of found probe_trace_event
2613 static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
2614 struct probe_trace_event **tevs)
2616 struct map *map = NULL;
2617 struct ref_reloc_sym *reloc_sym = NULL;
2619 struct symbol **syms = NULL;
2620 struct probe_trace_event *tev;
2621 struct perf_probe_point *pp = &pev->point;
2622 struct probe_trace_point *tp;
2623 int num_matched_functions;
2624 int ret, i, j, skipped = 0;
2626 map = get_target_map(pev->target, pev->uprobes);
2632 syms = malloc(sizeof(struct symbol *) * probe_conf.max_probes);
2639 * Load matched symbols: Since the different local symbols may have
2640 * same name but different addresses, this lists all the symbols.
2642 num_matched_functions = find_probe_functions(map, pp->function, syms);
2643 if (num_matched_functions == 0) {
2644 pr_err("Failed to find symbol %s in %s\n", pp->function,
2645 pev->target ? : "kernel");
2648 } else if (num_matched_functions > probe_conf.max_probes) {
2649 pr_err("Too many functions matched in %s\n",
2650 pev->target ? : "kernel");
2655 if (!pev->uprobes && !pp->retprobe) {
2656 reloc_sym = kernel_get_ref_reloc_sym();
2658 pr_warning("Relocated base symbol is not found!\n");
2664 /* Setup result trace-probe-events */
2665 *tevs = zalloc(sizeof(*tev) * num_matched_functions);
2673 for (j = 0; j < num_matched_functions; j++) {
2676 tev = (*tevs) + ret;
2678 if (ret == num_matched_functions) {
2679 pr_warning("Too many symbols are listed. Skip it.\n");
2684 if (pp->offset > sym->end - sym->start) {
2685 pr_warning("Offset %ld is bigger than the size of %s\n",
2686 pp->offset, sym->name);
2690 /* Add one probe point */
2691 tp->address = map->unmap_ip(map, sym->start) + pp->offset;
2692 /* If we found a wrong one, mark it by NULL symbol */
2693 if (!pev->uprobes &&
2694 kprobe_warn_out_range(sym->name, tp->address)) {
2695 tp->symbol = NULL; /* Skip it */
2697 } else if (reloc_sym) {
2698 tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
2699 tp->offset = tp->address - reloc_sym->addr;
2701 tp->symbol = strdup_or_goto(sym->name, nomem_out);
2702 tp->offset = pp->offset;
2704 tp->realname = strdup_or_goto(sym->name, nomem_out);
2706 tp->retprobe = pp->retprobe;
2708 tev->point.module = strdup_or_goto(pev->target,
2710 tev->uprobes = pev->uprobes;
2711 tev->nargs = pev->nargs;
2713 tev->args = zalloc(sizeof(struct probe_trace_arg) *
2715 if (tev->args == NULL)
2718 for (i = 0; i < tev->nargs; i++) {
2719 if (pev->args[i].name)
2721 strdup_or_goto(pev->args[i].name,
2724 tev->args[i].value = strdup_or_goto(pev->args[i].var,
2726 if (pev->args[i].type)
2728 strdup_or_goto(pev->args[i].type,
2731 arch__fix_tev_from_maps(pev, tev, map);
2733 if (ret == skipped) {
2739 put_target_map(map, pev->uprobes);
2746 clear_probe_trace_events(*tevs, num_matched_functions);
2751 bool __weak arch__prefers_symtab(void) { return false; }
2753 static int convert_to_probe_trace_events(struct perf_probe_event *pev,
2754 struct probe_trace_event **tevs)
2758 if (pev->uprobes && !pev->group) {
2759 /* Replace group name if not given */
2760 ret = convert_exec_to_group(pev->target, &pev->group);
2762 pr_warning("Failed to make a group name.\n");
2767 if (arch__prefers_symtab() && !perf_probe_event_need_dwarf(pev)) {
2768 ret = find_probe_trace_events_from_map(pev, tevs);
2770 return ret; /* Found in symbol table */
2773 /* Convert perf_probe_event with debuginfo */
2774 ret = try_to_find_probe_trace_events(pev, tevs);
2776 return ret; /* Found in debuginfo or got an error */
2778 return find_probe_trace_events_from_map(pev, tevs);
2781 struct __event_package {
2782 struct perf_probe_event *pev;
2783 struct probe_trace_event *tevs;
2787 int add_perf_probe_events(struct perf_probe_event *pevs, int npevs)
2790 struct __event_package *pkgs;
2793 pkgs = zalloc(sizeof(struct __event_package) * npevs);
2798 ret = init_symbol_maps(pevs->uprobes);
2804 /* Loop 1: convert all events */
2805 for (i = 0; i < npevs; i++) {
2806 pkgs[i].pev = &pevs[i];
2807 /* Init kprobe blacklist if needed */
2808 if (!pkgs[i].pev->uprobes)
2809 kprobe_blacklist__init();
2810 /* Convert with or without debuginfo */
2811 ret = convert_to_probe_trace_events(pkgs[i].pev,
2815 pkgs[i].ntevs = ret;
2817 /* This just release blacklist only if allocated */
2818 kprobe_blacklist__release();
2820 /* Loop 2: add all events */
2821 for (i = 0; i < npevs; i++) {
2822 ret = __add_probe_trace_events(pkgs[i].pev, pkgs[i].tevs,
2824 probe_conf.force_add);
2829 /* Loop 3: cleanup and free trace events */
2830 for (i = 0; i < npevs; i++) {
2831 for (j = 0; j < pkgs[i].ntevs; j++)
2832 clear_probe_trace_event(&pkgs[i].tevs[j]);
2833 zfree(&pkgs[i].tevs);
2841 static int __del_trace_probe_event(int fd, struct str_node *ent)
2847 /* Convert from perf-probe event to trace-probe event */
2848 ret = e_snprintf(buf, 128, "-:%s", ent->s);
2852 p = strchr(buf + 2, ':');
2854 pr_debug("Internal error: %s should have ':' but not.\n",
2861 pr_debug("Writing event: %s\n", buf);
2862 ret = write(fd, buf, strlen(buf));
2868 pr_info("Removed event: %s\n", ent->s);
2871 pr_warning("Failed to delete event: %s\n",
2872 strerror_r(-ret, buf, sizeof(buf)));
2876 static int del_trace_probe_events(int fd, struct strfilter *filter,
2877 struct strlist *namelist)
2879 struct str_node *ent;
2886 strlist__for_each(ent, namelist) {
2887 p = strchr(ent->s, ':');
2888 if ((p && strfilter__compare(filter, p + 1)) ||
2889 strfilter__compare(filter, ent->s)) {
2890 ret = __del_trace_probe_event(fd, ent);
2899 int del_perf_probe_events(struct strfilter *filter)
2901 int ret, ret2, ufd = -1, kfd = -1;
2902 struct strlist *namelist = NULL, *unamelist = NULL;
2903 char *str = strfilter__string(filter);
2908 pr_debug("Delete filter: \'%s\'\n", str);
2910 /* Get current event names */
2911 kfd = open_kprobe_events(true);
2913 namelist = get_probe_trace_event_names(kfd, true);
2915 ufd = open_uprobe_events(true);
2917 unamelist = get_probe_trace_event_names(ufd, true);
2919 if (kfd < 0 && ufd < 0) {
2920 print_both_open_warning(kfd, ufd);
2925 ret = del_trace_probe_events(kfd, filter, namelist);
2926 if (ret < 0 && ret != -ENOENT)
2929 ret2 = del_trace_probe_events(ufd, filter, unamelist);
2930 if (ret2 < 0 && ret2 != -ENOENT) {
2934 if (ret == -ENOENT && ret2 == -ENOENT)
2935 pr_debug("\"%s\" does not hit any event.\n", str);
2936 /* Note that this is silently ignored */
2941 strlist__delete(namelist);
2946 strlist__delete(unamelist);
2954 /* TODO: don't use a global variable for filter ... */
2955 static struct strfilter *available_func_filter;
2958 * If a symbol corresponds to a function with global binding and
2959 * matches filter return 0. For all others return 1.
2961 static int filter_available_functions(struct map *map __maybe_unused,
2964 if (strfilter__compare(available_func_filter, sym->name))
2969 int show_available_funcs(const char *target, struct strfilter *_filter,
2975 ret = init_symbol_maps(user);
2979 /* Get a symbol map */
2981 map = dso__new_map(target);
2983 map = kernel_get_module_map(target);
2985 pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
2989 /* Load symbols with given filter */
2990 available_func_filter = _filter;
2991 if (map__load(map, filter_available_functions)) {
2992 pr_err("Failed to load symbols in %s\n", (target) ? : "kernel");
2995 if (!dso__sorted_by_name(map->dso, map->type))
2996 dso__sort_by_name(map->dso, map->type);
2998 /* Show all (filtered) symbols */
3000 dso__fprintf_symbols_by_name(map->dso, map->type, stdout);