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)
166 struct map_groups *grp = &host_machine->kmaps;
168 /* A file path -- this is an offline module */
169 if (module && strchr(module, '/'))
170 return machine__new_module(host_machine, 0, module);
175 for (nd = rb_first(&grp->maps[MAP__FUNCTION]); nd; nd = rb_next(nd)) {
176 struct map *pos = rb_entry(nd, struct map, rb_node);
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 struct dso *kernel_get_module_dso(const char *module)
207 const char *vmlinux_name;
210 list_for_each_entry(dso, &host_machine->kernel_dsos.head,
212 if (strncmp(dso->short_name + 1, module,
213 dso->short_name_len - 2) == 0)
216 pr_debug("Failed to find module %s.\n", module);
220 map = host_machine->vmlinux_maps[MAP__FUNCTION];
223 vmlinux_name = symbol_conf.vmlinux_name;
225 if (dso__load_vmlinux(dso, map, vmlinux_name, false, NULL) <= 0)
228 if (dso__load_vmlinux_path(dso, map, NULL) <= 0) {
229 pr_debug("Failed to load kernel map.\n");
237 const char *kernel_get_module_path(const char *module)
239 struct dso *dso = kernel_get_module_dso(module);
240 return (dso) ? dso->long_name : NULL;
243 static int convert_exec_to_group(const char *exec, char **result)
245 char *ptr1, *ptr2, *exec_copy;
249 exec_copy = strdup(exec);
253 ptr1 = basename(exec_copy);
259 ptr2 = strpbrk(ptr1, "-._");
262 ret = e_snprintf(buf, 64, "%s_%s", PERFPROBE_GROUP, ptr1);
266 *result = strdup(buf);
267 ret = *result ? 0 : -ENOMEM;
274 static void clear_perf_probe_point(struct perf_probe_point *pp)
281 static void clear_probe_trace_events(struct probe_trace_event *tevs, int ntevs)
285 for (i = 0; i < ntevs; i++)
286 clear_probe_trace_event(tevs + i);
289 #ifdef HAVE_DWARF_SUPPORT
291 * Some binaries like glibc have special symbols which are on the symbol
292 * table, but not in the debuginfo. If we can find the address of the
293 * symbol from map, we can translate the address back to the probe point.
295 static int find_alternative_probe_point(struct debuginfo *dinfo,
296 struct perf_probe_point *pp,
297 struct perf_probe_point *result,
298 const char *target, bool uprobes)
300 struct map *map = NULL;
305 /* This can work only for function-name based one */
306 if (!pp->function || pp->file)
309 map = get_target_map(target, uprobes);
313 /* Find the address of given function */
314 map__for_each_symbol_by_name(map, pp->function, sym) {
316 address = sym->start;
318 address = map->unmap_ip(map, sym->start);
325 pr_debug("Symbol %s address found : %" PRIx64 "\n",
326 pp->function, address);
328 ret = debuginfo__find_probe_point(dinfo, (unsigned long)address,
331 ret = (!ret) ? -ENOENT : ret;
333 result->offset += pp->offset;
334 result->line += pp->line;
335 result->retprobe = pp->retprobe;
340 put_target_map(map, uprobes);
345 static int get_alternative_probe_event(struct debuginfo *dinfo,
346 struct perf_probe_event *pev,
347 struct perf_probe_point *tmp)
351 memcpy(tmp, &pev->point, sizeof(*tmp));
352 memset(&pev->point, 0, sizeof(pev->point));
353 ret = find_alternative_probe_point(dinfo, tmp, &pev->point,
354 pev->target, pev->uprobes);
356 memcpy(&pev->point, tmp, sizeof(*tmp));
361 static int get_alternative_line_range(struct debuginfo *dinfo,
362 struct line_range *lr,
363 const char *target, bool user)
365 struct perf_probe_point pp = { .function = lr->function,
368 struct perf_probe_point result;
371 memset(&result, 0, sizeof(result));
373 if (lr->end != INT_MAX)
374 len = lr->end - lr->start;
375 ret = find_alternative_probe_point(dinfo, &pp, &result,
378 lr->function = result.function;
379 lr->file = result.file;
380 lr->start = result.line;
381 if (lr->end != INT_MAX)
382 lr->end = lr->start + len;
383 clear_perf_probe_point(&pp);
388 /* Open new debuginfo of given module */
389 static struct debuginfo *open_debuginfo(const char *module, bool silent)
391 const char *path = module;
392 struct debuginfo *ret;
394 if (!module || !strchr(module, '/')) {
395 path = kernel_get_module_path(module);
398 pr_err("Failed to find path of %s module.\n",
403 ret = debuginfo__new(path);
404 if (!ret && !silent) {
405 pr_warning("The %s file has no debug information.\n", path);
406 if (!module || !strtailcmp(path, ".ko"))
407 pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, ");
409 pr_warning("Rebuild with -g, ");
410 pr_warning("or install an appropriate debuginfo package.\n");
416 static int get_text_start_address(const char *exec, unsigned long *address)
421 int fd, ret = -ENOENT;
423 fd = open(exec, O_RDONLY);
427 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
431 if (gelf_getehdr(elf, &ehdr) == NULL)
434 if (!elf_section_by_name(elf, &ehdr, &shdr, ".text", NULL))
437 *address = shdr.sh_addr - shdr.sh_offset;
445 * Convert trace point to probe point with debuginfo
447 static int find_perf_probe_point_from_dwarf(struct probe_trace_point *tp,
448 struct perf_probe_point *pp,
451 struct debuginfo *dinfo = NULL;
452 unsigned long stext = 0;
453 u64 addr = tp->address;
456 /* convert the address to dwarf address */
462 ret = get_text_start_address(tp->module, &stext);
467 addr = kernel_get_symbol_address_by_name(tp->symbol, false);
473 pr_debug("try to find information at %" PRIx64 " in %s\n", addr,
474 tp->module ? : "kernel");
476 dinfo = open_debuginfo(tp->module, verbose == 0);
478 ret = debuginfo__find_probe_point(dinfo,
479 (unsigned long)addr, pp);
480 debuginfo__delete(dinfo);
485 pp->retprobe = tp->retprobe;
489 pr_debug("Failed to find corresponding probes from debuginfo.\n");
490 return ret ? : -ENOENT;
493 static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs,
494 int ntevs, const char *exec)
497 unsigned long stext = 0;
502 ret = get_text_start_address(exec, &stext);
506 for (i = 0; i < ntevs && ret >= 0; i++) {
507 /* point.address is the addres of point.symbol + point.offset */
508 tevs[i].point.address -= stext;
509 tevs[i].point.module = strdup(exec);
510 if (!tevs[i].point.module) {
514 tevs[i].uprobes = true;
520 static int add_module_to_probe_trace_events(struct probe_trace_event *tevs,
521 int ntevs, const char *module)
529 tmp = strrchr(module, '/');
531 /* This is a module path -- get the module name */
532 module = strdup(tmp + 1);
535 tmp = strchr(module, '.');
538 tmp = (char *)module; /* For free() */
541 for (i = 0; i < ntevs; i++) {
542 tevs[i].point.module = strdup(module);
543 if (!tevs[i].point.module) {
553 /* Post processing the probe events */
554 static int post_process_probe_trace_events(struct probe_trace_event *tevs,
555 int ntevs, const char *module,
558 struct ref_reloc_sym *reloc_sym;
564 return add_exec_to_probe_trace_events(tevs, ntevs, module);
566 /* Note that currently ref_reloc_sym based probe is not for drivers */
568 return add_module_to_probe_trace_events(tevs, ntevs, module);
570 reloc_sym = kernel_get_ref_reloc_sym();
572 pr_warning("Relocated base symbol is not found!\n");
575 /* Get the address of _etext for checking non-probable text symbol */
576 etext_addr = kernel_get_symbol_address_by_name("_etext", false);
578 for (i = 0; i < ntevs; i++) {
579 if (tevs[i].point.address && !tevs[i].point.retprobe) {
580 /* If we found a wrong one, mark it by NULL symbol */
581 if (etext_addr < tevs[i].point.address) {
582 pr_warning("%s+%lu is out of .text, skip it.\n",
583 tevs[i].point.symbol, tevs[i].point.offset);
587 tmp = strdup(reloc_sym->name);
591 /* If we have no realname, use symbol for it */
592 if (!tevs[i].point.realname)
593 tevs[i].point.realname = tevs[i].point.symbol;
595 free(tevs[i].point.symbol);
596 tevs[i].point.symbol = tmp;
597 tevs[i].point.offset = tevs[i].point.address -
598 reloc_sym->unrelocated_addr;
604 /* Try to find perf_probe_event with debuginfo */
605 static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
606 struct probe_trace_event **tevs)
608 bool need_dwarf = perf_probe_event_need_dwarf(pev);
609 struct perf_probe_point tmp;
610 struct debuginfo *dinfo;
613 dinfo = open_debuginfo(pev->target, !need_dwarf);
617 pr_debug("Could not open debuginfo. Try to use symbols.\n");
621 pr_debug("Try to find probe point from debuginfo.\n");
622 /* Searching trace events corresponding to a probe event */
623 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
625 if (ntevs == 0) { /* Not found, retry with an alternative */
626 ret = get_alternative_probe_event(dinfo, pev, &tmp);
628 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
630 * Write back to the original probe_event for
631 * setting appropriate (user given) event name
633 clear_perf_probe_point(&pev->point);
634 memcpy(&pev->point, &tmp, sizeof(tmp));
638 debuginfo__delete(dinfo);
640 if (ntevs > 0) { /* Succeeded to find trace events */
641 pr_debug("Found %d probe_trace_events.\n", ntevs);
642 ret = post_process_probe_trace_events(*tevs, ntevs,
643 pev->target, pev->uprobes);
644 if (ret < 0 || ret == ntevs) {
645 clear_probe_trace_events(*tevs, ntevs);
649 return ret < 0 ? ret : ntevs;
654 if (ntevs == 0) { /* No error but failed to find probe point. */
655 pr_warning("Probe point '%s' not found.\n",
656 synthesize_perf_probe_point(&pev->point));
659 /* Error path : ntevs < 0 */
660 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
661 if (ntevs == -EBADF) {
662 pr_warning("Warning: No dwarf info found in the vmlinux - "
663 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
665 pr_debug("Trying to use symbols.\n");
672 #define LINEBUF_SIZE 256
673 #define NR_ADDITIONAL_LINES 2
675 static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
677 char buf[LINEBUF_SIZE], sbuf[STRERR_BUFSIZE];
678 const char *color = show_num ? "" : PERF_COLOR_BLUE;
679 const char *prefix = NULL;
682 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
687 prefix = show_num ? "%7d " : " ";
688 color_fprintf(stdout, color, prefix, l);
690 color_fprintf(stdout, color, "%s", buf);
692 } while (strchr(buf, '\n') == NULL);
697 pr_warning("File read error: %s\n",
698 strerror_r(errno, sbuf, sizeof(sbuf)));
704 static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
706 int rv = __show_one_line(fp, l, skip, show_num);
708 pr_warning("Source file is shorter than expected.\n");
714 #define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
715 #define show_one_line(f,l) _show_one_line(f,l,false,false)
716 #define skip_one_line(f,l) _show_one_line(f,l,true,false)
717 #define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
720 * Show line-range always requires debuginfo to find source file and
723 static int __show_line_range(struct line_range *lr, const char *module,
728 struct debuginfo *dinfo;
732 char sbuf[STRERR_BUFSIZE];
734 /* Search a line range */
735 dinfo = open_debuginfo(module, false);
739 ret = debuginfo__find_line_range(dinfo, lr);
740 if (!ret) { /* Not found, retry with an alternative */
741 ret = get_alternative_line_range(dinfo, lr, module, user);
743 ret = debuginfo__find_line_range(dinfo, lr);
745 debuginfo__delete(dinfo);
746 if (ret == 0 || ret == -ENOENT) {
747 pr_warning("Specified source line is not found.\n");
749 } else if (ret < 0) {
750 pr_warning("Debuginfo analysis failed.\n");
754 /* Convert source file path */
756 ret = get_real_path(tmp, lr->comp_dir, &lr->path);
758 /* Free old path when new path is assigned */
763 pr_warning("Failed to find source file path.\n");
770 fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
771 lr->start - lr->offset);
773 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
775 fp = fopen(lr->path, "r");
777 pr_warning("Failed to open %s: %s\n", lr->path,
778 strerror_r(errno, sbuf, sizeof(sbuf)));
781 /* Skip to starting line number */
782 while (l < lr->start) {
783 ret = skip_one_line(fp, l++);
788 intlist__for_each(ln, lr->line_list) {
789 for (; ln->i > l; l++) {
790 ret = show_one_line(fp, l - lr->offset);
794 ret = show_one_line_with_num(fp, l++ - lr->offset);
799 if (lr->end == INT_MAX)
800 lr->end = l + NR_ADDITIONAL_LINES;
801 while (l <= lr->end) {
802 ret = show_one_line_or_eof(fp, l++ - lr->offset);
811 int show_line_range(struct line_range *lr, const char *module, bool user)
815 ret = init_symbol_maps(user);
818 ret = __show_line_range(lr, module, user);
824 static int show_available_vars_at(struct debuginfo *dinfo,
825 struct perf_probe_event *pev,
826 struct strfilter *_filter)
830 struct str_node *node;
831 struct variable_list *vls = NULL, *vl;
832 struct perf_probe_point tmp;
835 buf = synthesize_perf_probe_point(&pev->point);
838 pr_debug("Searching variables at %s\n", buf);
840 ret = debuginfo__find_available_vars_at(dinfo, pev, &vls);
841 if (!ret) { /* Not found, retry with an alternative */
842 ret = get_alternative_probe_event(dinfo, pev, &tmp);
844 ret = debuginfo__find_available_vars_at(dinfo, pev,
846 /* Release the old probe_point */
847 clear_perf_probe_point(&tmp);
851 if (ret == 0 || ret == -ENOENT) {
852 pr_err("Failed to find the address of %s\n", buf);
855 pr_warning("Debuginfo analysis failed.\n");
859 /* Some variables are found */
860 fprintf(stdout, "Available variables at %s\n", buf);
861 for (i = 0; i < ret; i++) {
864 * A probe point might be converted to
865 * several trace points.
867 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
869 zfree(&vl->point.symbol);
872 strlist__for_each(node, vl->vars) {
873 var = strchr(node->s, '\t') + 1;
874 if (strfilter__compare(_filter, var)) {
875 fprintf(stdout, "\t\t%s\n", node->s);
879 strlist__delete(vl->vars);
882 fprintf(stdout, "\t\t(No matched variables)\n");
890 /* Show available variables on given probe point */
891 int show_available_vars(struct perf_probe_event *pevs, int npevs,
892 struct strfilter *_filter)
895 struct debuginfo *dinfo;
897 ret = init_symbol_maps(pevs->uprobes);
901 dinfo = open_debuginfo(pevs->target, false);
909 for (i = 0; i < npevs && ret >= 0; i++)
910 ret = show_available_vars_at(dinfo, &pevs[i], _filter);
912 debuginfo__delete(dinfo);
918 #else /* !HAVE_DWARF_SUPPORT */
921 find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused,
922 struct perf_probe_point *pp __maybe_unused,
923 bool is_kprobe __maybe_unused)
928 static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
929 struct probe_trace_event **tevs __maybe_unused)
931 if (perf_probe_event_need_dwarf(pev)) {
932 pr_warning("Debuginfo-analysis is not supported.\n");
939 int show_line_range(struct line_range *lr __maybe_unused,
940 const char *module __maybe_unused,
941 bool user __maybe_unused)
943 pr_warning("Debuginfo-analysis is not supported.\n");
947 int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
948 int npevs __maybe_unused,
949 struct strfilter *filter __maybe_unused)
951 pr_warning("Debuginfo-analysis is not supported.\n");
956 void line_range__clear(struct line_range *lr)
962 intlist__delete(lr->line_list);
963 memset(lr, 0, sizeof(*lr));
966 int line_range__init(struct line_range *lr)
968 memset(lr, 0, sizeof(*lr));
969 lr->line_list = intlist__new(NULL);
976 static int parse_line_num(char **ptr, int *val, const char *what)
978 const char *start = *ptr;
981 *val = strtol(*ptr, ptr, 0);
982 if (errno || *ptr == start) {
983 semantic_error("'%s' is not a valid number.\n", what);
989 /* Check the name is good for event, group or function */
990 static bool is_c_func_name(const char *name)
992 if (!isalpha(*name) && *name != '_')
994 while (*++name != '\0') {
995 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
1002 * Stuff 'lr' according to the line range described by 'arg'.
1003 * The line range syntax is described by:
1005 * SRC[:SLN[+NUM|-ELN]]
1006 * FNC[@SRC][:SLN[+NUM|-ELN]]
1008 int parse_line_range_desc(const char *arg, struct line_range *lr)
1010 char *range, *file, *name = strdup(arg);
1019 range = strchr(name, ':');
1023 err = parse_line_num(&range, &lr->start, "start line");
1027 if (*range == '+' || *range == '-') {
1028 const char c = *range++;
1030 err = parse_line_num(&range, &lr->end, "end line");
1035 lr->end += lr->start;
1037 * Adjust the number of lines here.
1038 * If the number of lines == 1, the
1039 * the end of line should be equal to
1040 * the start of line.
1046 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
1049 if (lr->start > lr->end) {
1050 semantic_error("Start line must be smaller"
1051 " than end line.\n");
1054 if (*range != '\0') {
1055 semantic_error("Tailing with invalid str '%s'.\n", range);
1060 file = strchr(name, '@');
1063 lr->file = strdup(++file);
1064 if (lr->file == NULL) {
1068 lr->function = name;
1069 } else if (strchr(name, '/') || strchr(name, '.'))
1071 else if (is_c_func_name(name))/* We reuse it for checking funcname */
1072 lr->function = name;
1073 else { /* Invalid name */
1074 semantic_error("'%s' is not a valid function name.\n", name);
1085 /* Parse probepoint definition. */
1086 static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
1088 struct perf_probe_point *pp = &pev->point;
1091 bool file_spec = false;
1094 * perf probe [EVENT=]SRC[:LN|;PTN]
1095 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
1097 * TODO:Group name support
1102 ptr = strpbrk(arg, ";=@+%");
1103 if (ptr && *ptr == '=') { /* Event name */
1106 if (strchr(arg, ':')) {
1107 semantic_error("Group name is not supported yet.\n");
1110 if (!is_c_func_name(arg)) {
1111 semantic_error("%s is bad for event name -it must "
1112 "follow C symbol-naming rule.\n", arg);
1115 pev->event = strdup(arg);
1116 if (pev->event == NULL)
1123 * Check arg is function or file name and copy it.
1125 * We consider arg to be a file spec if and only if it satisfies
1126 * all of the below criteria::
1127 * - it does not include any of "+@%",
1128 * - it includes one of ":;", and
1129 * - it has a period '.' in the name.
1131 * Otherwise, we consider arg to be a function specification.
1133 if (!strpbrk(arg, "+@%") && (ptr = strpbrk(arg, ";:")) != NULL) {
1134 /* This is a file spec if it includes a '.' before ; or : */
1135 if (memchr(arg, '.', ptr - arg))
1139 ptr = strpbrk(arg, ";:+@%");
1154 /* Parse other options */
1158 if (c == ';') { /* Lazy pattern must be the last part */
1159 pp->lazy_line = strdup(arg);
1160 if (pp->lazy_line == NULL)
1164 ptr = strpbrk(arg, ";:+@%");
1170 case ':': /* Line number */
1171 pp->line = strtoul(arg, &tmp, 0);
1173 semantic_error("There is non-digit char"
1174 " in line number.\n");
1178 case '+': /* Byte offset from a symbol */
1179 pp->offset = strtoul(arg, &tmp, 0);
1181 semantic_error("There is non-digit character"
1186 case '@': /* File name */
1188 semantic_error("SRC@SRC is not allowed.\n");
1191 pp->file = strdup(arg);
1192 if (pp->file == NULL)
1195 case '%': /* Probe places */
1196 if (strcmp(arg, "return") == 0) {
1198 } else { /* Others not supported yet */
1199 semantic_error("%%%s is not supported.\n", arg);
1203 default: /* Buggy case */
1204 pr_err("This program has a bug at %s:%d.\n",
1205 __FILE__, __LINE__);
1211 /* Exclusion check */
1212 if (pp->lazy_line && pp->line) {
1213 semantic_error("Lazy pattern can't be used with"
1218 if (pp->lazy_line && pp->offset) {
1219 semantic_error("Lazy pattern can't be used with offset.\n");
1223 if (pp->line && pp->offset) {
1224 semantic_error("Offset can't be used with line number.\n");
1228 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
1229 semantic_error("File always requires line number or "
1234 if (pp->offset && !pp->function) {
1235 semantic_error("Offset requires an entry function.\n");
1239 if (pp->retprobe && !pp->function) {
1240 semantic_error("Return probe requires an entry function.\n");
1244 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
1245 semantic_error("Offset/Line/Lazy pattern can't be used with "
1250 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
1251 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
1256 /* Parse perf-probe event argument */
1257 static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
1259 char *tmp, *goodname;
1260 struct perf_probe_arg_field **fieldp;
1262 pr_debug("parsing arg: %s into ", str);
1264 tmp = strchr(str, '=');
1266 arg->name = strndup(str, tmp - str);
1267 if (arg->name == NULL)
1269 pr_debug("name:%s ", arg->name);
1273 tmp = strchr(str, ':');
1274 if (tmp) { /* Type setting */
1276 arg->type = strdup(tmp + 1);
1277 if (arg->type == NULL)
1279 pr_debug("type:%s ", arg->type);
1282 tmp = strpbrk(str, "-.[");
1283 if (!is_c_varname(str) || !tmp) {
1284 /* A variable, register, symbol or special value */
1285 arg->var = strdup(str);
1286 if (arg->var == NULL)
1288 pr_debug("%s\n", arg->var);
1292 /* Structure fields or array element */
1293 arg->var = strndup(str, tmp - str);
1294 if (arg->var == NULL)
1296 goodname = arg->var;
1297 pr_debug("%s, ", arg->var);
1298 fieldp = &arg->field;
1301 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
1302 if (*fieldp == NULL)
1304 if (*tmp == '[') { /* Array */
1306 (*fieldp)->index = strtol(str + 1, &tmp, 0);
1307 (*fieldp)->ref = true;
1308 if (*tmp != ']' || tmp == str + 1) {
1309 semantic_error("Array index must be a"
1316 } else { /* Structure */
1319 (*fieldp)->ref = false;
1320 } else if (tmp[1] == '>') {
1322 (*fieldp)->ref = true;
1324 semantic_error("Argument parse error: %s\n",
1328 tmp = strpbrk(str, "-.[");
1331 (*fieldp)->name = strndup(str, tmp - str);
1332 if ((*fieldp)->name == NULL)
1335 goodname = (*fieldp)->name;
1336 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
1337 fieldp = &(*fieldp)->next;
1340 (*fieldp)->name = strdup(str);
1341 if ((*fieldp)->name == NULL)
1344 goodname = (*fieldp)->name;
1345 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
1347 /* If no name is specified, set the last field name (not array index)*/
1349 arg->name = strdup(goodname);
1350 if (arg->name == NULL)
1356 /* Parse perf-probe event command */
1357 int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
1360 int argc, i, ret = 0;
1362 argv = argv_split(cmd, &argc);
1364 pr_debug("Failed to split arguments.\n");
1367 if (argc - 1 > MAX_PROBE_ARGS) {
1368 semantic_error("Too many probe arguments (%d).\n", argc - 1);
1372 /* Parse probe point */
1373 ret = parse_perf_probe_point(argv[0], pev);
1377 /* Copy arguments and ensure return probe has no C argument */
1378 pev->nargs = argc - 1;
1379 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1380 if (pev->args == NULL) {
1384 for (i = 0; i < pev->nargs && ret >= 0; i++) {
1385 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1387 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
1388 semantic_error("You can't specify local variable for"
1399 /* Return true if this perf_probe_event requires debuginfo */
1400 bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
1404 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1407 for (i = 0; i < pev->nargs; i++)
1408 if (is_c_varname(pev->args[i].var))
1414 /* Parse probe_events event into struct probe_point */
1415 static int parse_probe_trace_command(const char *cmd,
1416 struct probe_trace_event *tev)
1418 struct probe_trace_point *tp = &tev->point;
1421 char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
1425 pr_debug("Parsing probe_events: %s\n", cmd);
1426 argv = argv_split(cmd, &argc);
1428 pr_debug("Failed to split arguments.\n");
1432 semantic_error("Too few probe arguments.\n");
1437 /* Scan event and group name. */
1438 argv0_str = strdup(argv[0]);
1439 if (argv0_str == NULL) {
1443 fmt1_str = strtok_r(argv0_str, ":", &fmt);
1444 fmt2_str = strtok_r(NULL, "/", &fmt);
1445 fmt3_str = strtok_r(NULL, " \t", &fmt);
1446 if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
1447 || fmt3_str == NULL) {
1448 semantic_error("Failed to parse event name: %s\n", argv[0]);
1453 tev->group = strdup(fmt2_str);
1454 tev->event = strdup(fmt3_str);
1455 if (tev->group == NULL || tev->event == NULL) {
1459 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
1461 tp->retprobe = (pr == 'r');
1463 /* Scan module name(if there), function name and offset */
1464 p = strchr(argv[1], ':');
1466 tp->module = strndup(argv[1], p - argv[1]);
1470 fmt1_str = strtok_r(p, "+", &fmt);
1471 if (fmt1_str[0] == '0') /* only the address started with 0x */
1472 tp->address = strtoul(fmt1_str, NULL, 0);
1474 /* Only the symbol-based probe has offset */
1475 tp->symbol = strdup(fmt1_str);
1476 if (tp->symbol == NULL) {
1480 fmt2_str = strtok_r(NULL, "", &fmt);
1481 if (fmt2_str == NULL)
1484 tp->offset = strtoul(fmt2_str, NULL, 10);
1487 tev->nargs = argc - 2;
1488 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
1489 if (tev->args == NULL) {
1493 for (i = 0; i < tev->nargs; i++) {
1494 p = strchr(argv[i + 2], '=');
1495 if (p) /* We don't need which register is assigned. */
1499 tev->args[i].name = strdup(argv[i + 2]);
1500 /* TODO: parse regs and offset */
1501 tev->args[i].value = strdup(p);
1502 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1514 /* Compose only probe arg */
1515 int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
1517 struct perf_probe_arg_field *field = pa->field;
1521 if (pa->name && pa->var)
1522 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
1524 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
1531 if (field->name[0] == '[')
1532 ret = e_snprintf(tmp, len, "%s", field->name);
1534 ret = e_snprintf(tmp, len, "%s%s",
1535 field->ref ? "->" : ".", field->name);
1540 field = field->next;
1544 ret = e_snprintf(tmp, len, ":%s", pa->type);
1553 pr_debug("Failed to synthesize perf probe argument: %d\n", ret);
1557 /* Compose only probe point (not argument) */
1558 static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
1561 char offs[32] = "", line[32] = "", file[32] = "";
1564 buf = zalloc(MAX_CMDLEN);
1570 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
1575 ret = e_snprintf(line, 32, ":%d", pp->line);
1583 tmp = strchr(pp->file + len - 30, '/');
1584 tmp = tmp ? tmp + 1 : pp->file + len - 30;
1586 ret = e_snprintf(file, 32, "@%s", tmp);
1592 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
1593 offs, pp->retprobe ? "%return" : "", line,
1596 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
1602 pr_debug("Failed to synthesize perf probe point: %d\n", ret);
1608 char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1613 buf = synthesize_perf_probe_point(&pev->point);
1618 for (i = 0; i < pev->nargs; i++) {
1619 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
1632 static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
1633 char **buf, size_t *buflen,
1638 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
1644 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
1656 static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
1657 char *buf, size_t buflen)
1659 struct probe_trace_arg_ref *ref = arg->ref;
1663 /* Argument name or separator */
1665 ret = e_snprintf(buf, buflen, " %s=", arg->name);
1667 ret = e_snprintf(buf, buflen, " ");
1673 /* Special case: @XXX */
1674 if (arg->value[0] == '@' && arg->ref)
1677 /* Dereferencing arguments */
1679 depth = __synthesize_probe_trace_arg_ref(ref, &buf,
1685 /* Print argument value */
1686 if (arg->value[0] == '@' && arg->ref)
1687 ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
1690 ret = e_snprintf(buf, buflen, "%s", arg->value);
1698 ret = e_snprintf(buf, buflen, ")");
1704 /* Print argument type */
1706 ret = e_snprintf(buf, buflen, ":%s", arg->type);
1715 char *synthesize_probe_trace_command(struct probe_trace_event *tev)
1717 struct probe_trace_point *tp = &tev->point;
1721 buf = zalloc(MAX_CMDLEN);
1725 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
1726 tev->group, tev->event);
1730 /* Uprobes must have tp->address and tp->module */
1731 if (tev->uprobes && (!tp->address || !tp->module))
1734 /* Use the tp->address for uprobes */
1736 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s:0x%lx",
1737 tp->module, tp->address);
1739 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s%s%s+%lu",
1740 tp->module ?: "", tp->module ? ":" : "",
1741 tp->symbol, tp->offset);
1747 for (i = 0; i < tev->nargs; i++) {
1748 ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
1761 static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
1762 struct perf_probe_point *pp,
1765 struct symbol *sym = NULL;
1771 map = dso__new_map(tp->module);
1775 sym = map__find_symbol(map, addr, NULL);
1777 addr = kernel_get_symbol_address_by_name(tp->symbol, true);
1780 sym = __find_kernel_function(addr, &map);
1786 pp->retprobe = tp->retprobe;
1787 pp->offset = addr - map->unmap_ip(map, sym->start);
1788 pp->function = strdup(sym->name);
1789 ret = pp->function ? 0 : -ENOMEM;
1792 if (map && !is_kprobe) {
1799 static int convert_to_perf_probe_point(struct probe_trace_point *tp,
1800 struct perf_probe_point *pp,
1806 ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe);
1809 ret = find_perf_probe_point_from_map(tp, pp, is_kprobe);
1813 pr_debug("Failed to find probe point from both of dwarf and map.\n");
1816 pp->function = strdup(tp->symbol);
1817 pp->offset = tp->offset;
1818 } else if (!tp->module && !is_kprobe) {
1819 ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address);
1822 pp->function = strdup(buf);
1825 if (pp->function == NULL)
1828 pp->retprobe = tp->retprobe;
1833 static int convert_to_perf_probe_event(struct probe_trace_event *tev,
1834 struct perf_probe_event *pev, bool is_kprobe)
1839 /* Convert event/group name */
1840 pev->event = strdup(tev->event);
1841 pev->group = strdup(tev->group);
1842 if (pev->event == NULL || pev->group == NULL)
1845 /* Convert trace_point to probe_point */
1846 ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
1850 /* Convert trace_arg to probe_arg */
1851 pev->nargs = tev->nargs;
1852 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1853 if (pev->args == NULL)
1855 for (i = 0; i < tev->nargs && ret >= 0; i++) {
1856 if (tev->args[i].name)
1857 pev->args[i].name = strdup(tev->args[i].name);
1859 ret = synthesize_probe_trace_arg(&tev->args[i],
1861 pev->args[i].name = strdup(buf);
1863 if (pev->args[i].name == NULL && ret >= 0)
1868 clear_perf_probe_event(pev);
1873 void clear_perf_probe_event(struct perf_probe_event *pev)
1875 struct perf_probe_arg_field *field, *next;
1881 clear_perf_probe_point(&pev->point);
1883 for (i = 0; i < pev->nargs; i++) {
1884 free(pev->args[i].name);
1885 free(pev->args[i].var);
1886 free(pev->args[i].type);
1887 field = pev->args[i].field;
1890 zfree(&field->name);
1896 memset(pev, 0, sizeof(*pev));
1899 static void clear_probe_trace_event(struct probe_trace_event *tev)
1901 struct probe_trace_arg_ref *ref, *next;
1906 free(tev->point.symbol);
1907 free(tev->point.realname);
1908 free(tev->point.module);
1909 for (i = 0; i < tev->nargs; i++) {
1910 free(tev->args[i].name);
1911 free(tev->args[i].value);
1912 free(tev->args[i].type);
1913 ref = tev->args[i].ref;
1921 memset(tev, 0, sizeof(*tev));
1924 static void print_open_warning(int err, bool is_kprobe)
1926 char sbuf[STRERR_BUFSIZE];
1928 if (err == -ENOENT) {
1932 config = "CONFIG_UPROBE_EVENTS";
1934 config = "CONFIG_KPROBE_EVENTS";
1936 pr_warning("%cprobe_events file does not exist"
1937 " - please rebuild kernel with %s.\n",
1938 is_kprobe ? 'k' : 'u', config);
1939 } else if (err == -ENOTSUP)
1940 pr_warning("Tracefs or debugfs is not mounted.\n");
1942 pr_warning("Failed to open %cprobe_events: %s\n",
1943 is_kprobe ? 'k' : 'u',
1944 strerror_r(-err, sbuf, sizeof(sbuf)));
1947 static void print_both_open_warning(int kerr, int uerr)
1949 /* Both kprobes and uprobes are disabled, warn it. */
1950 if (kerr == -ENOTSUP && uerr == -ENOTSUP)
1951 pr_warning("Tracefs or debugfs is not mounted.\n");
1952 else if (kerr == -ENOENT && uerr == -ENOENT)
1953 pr_warning("Please rebuild kernel with CONFIG_KPROBE_EVENTS "
1954 "or/and CONFIG_UPROBE_EVENTS.\n");
1956 char sbuf[STRERR_BUFSIZE];
1957 pr_warning("Failed to open kprobe events: %s.\n",
1958 strerror_r(-kerr, sbuf, sizeof(sbuf)));
1959 pr_warning("Failed to open uprobe events: %s.\n",
1960 strerror_r(-uerr, sbuf, sizeof(sbuf)));
1964 static int open_probe_events(const char *trace_file, bool readwrite)
1967 const char *__debugfs;
1968 const char *tracing_dir = "";
1971 __debugfs = tracefs_find_mountpoint();
1972 if (__debugfs == NULL) {
1973 tracing_dir = "tracing/";
1975 __debugfs = debugfs_find_mountpoint();
1976 if (__debugfs == NULL)
1980 ret = e_snprintf(buf, PATH_MAX, "%s/%s%s",
1981 __debugfs, tracing_dir, trace_file);
1983 pr_debug("Opening %s write=%d\n", buf, readwrite);
1984 if (readwrite && !probe_event_dry_run)
1985 ret = open(buf, O_RDWR | O_APPEND, 0);
1987 ret = open(buf, O_RDONLY, 0);
1995 static int open_kprobe_events(bool readwrite)
1997 return open_probe_events("kprobe_events", readwrite);
2000 static int open_uprobe_events(bool readwrite)
2002 return open_probe_events("uprobe_events", readwrite);
2005 /* Get raw string list of current kprobe_events or uprobe_events */
2006 static struct strlist *get_probe_trace_command_rawlist(int fd)
2010 char buf[MAX_CMDLEN];
2014 sl = strlist__new(true, NULL);
2016 fp = fdopen(dup(fd), "r");
2018 p = fgets(buf, MAX_CMDLEN, fp);
2022 idx = strlen(p) - 1;
2025 ret = strlist__add(sl, buf);
2027 pr_debug("strlist__add failed (%d)\n", ret);
2028 strlist__delete(sl);
2037 struct kprobe_blacklist_node {
2038 struct list_head list;
2039 unsigned long start;
2044 static void kprobe_blacklist__delete(struct list_head *blacklist)
2046 struct kprobe_blacklist_node *node;
2048 while (!list_empty(blacklist)) {
2049 node = list_first_entry(blacklist,
2050 struct kprobe_blacklist_node, list);
2051 list_del(&node->list);
2057 static int kprobe_blacklist__load(struct list_head *blacklist)
2059 struct kprobe_blacklist_node *node;
2060 const char *__debugfs = debugfs_find_mountpoint();
2061 char buf[PATH_MAX], *p;
2065 if (__debugfs == NULL)
2068 ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs);
2072 fp = fopen(buf, "r");
2077 while (fgets(buf, PATH_MAX, fp)) {
2078 node = zalloc(sizeof(*node));
2083 INIT_LIST_HEAD(&node->list);
2084 list_add_tail(&node->list, blacklist);
2085 if (sscanf(buf, "0x%lx-0x%lx", &node->start, &node->end) != 2) {
2089 p = strchr(buf, '\t');
2092 if (p[strlen(p) - 1] == '\n')
2093 p[strlen(p) - 1] = '\0';
2095 p = (char *)"unknown";
2096 node->symbol = strdup(p);
2097 if (!node->symbol) {
2101 pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
2102 node->start, node->end, node->symbol);
2106 kprobe_blacklist__delete(blacklist);
2112 static struct kprobe_blacklist_node *
2113 kprobe_blacklist__find_by_address(struct list_head *blacklist,
2114 unsigned long address)
2116 struct kprobe_blacklist_node *node;
2118 list_for_each_entry(node, blacklist, list) {
2119 if (node->start <= address && address <= node->end)
2127 static int show_perf_probe_event(struct perf_probe_event *pev,
2134 /* Synthesize only event probe point */
2135 place = synthesize_perf_probe_point(&pev->point);
2139 ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
2143 pr_info(" %-20s (on %s", buf, place);
2145 pr_info(" in %s", module);
2147 if (pev->nargs > 0) {
2149 for (i = 0; i < pev->nargs; i++) {
2150 ret = synthesize_perf_probe_arg(&pev->args[i],
2154 pr_info(" %s", buf);
2162 static bool filter_probe_trace_event(struct probe_trace_event *tev,
2163 struct strfilter *filter)
2167 /* At first, check the event name itself */
2168 if (strfilter__compare(filter, tev->event))
2171 /* Next, check the combination of name and group */
2172 if (e_snprintf(tmp, 128, "%s:%s", tev->group, tev->event) < 0)
2174 return strfilter__compare(filter, tmp);
2177 static int __show_perf_probe_events(int fd, bool is_kprobe,
2178 struct strfilter *filter)
2181 struct probe_trace_event tev;
2182 struct perf_probe_event pev;
2183 struct strlist *rawlist;
2184 struct str_node *ent;
2186 memset(&tev, 0, sizeof(tev));
2187 memset(&pev, 0, sizeof(pev));
2189 rawlist = get_probe_trace_command_rawlist(fd);
2193 strlist__for_each(ent, rawlist) {
2194 ret = parse_probe_trace_command(ent->s, &tev);
2196 if (!filter_probe_trace_event(&tev, filter))
2198 ret = convert_to_perf_probe_event(&tev, &pev,
2201 ret = show_perf_probe_event(&pev,
2205 clear_perf_probe_event(&pev);
2206 clear_probe_trace_event(&tev);
2210 strlist__delete(rawlist);
2215 /* List up current perf-probe events */
2216 int show_perf_probe_events(struct strfilter *filter)
2218 int kp_fd, up_fd, ret;
2222 ret = init_symbol_maps(false);
2226 kp_fd = open_kprobe_events(false);
2228 ret = __show_perf_probe_events(kp_fd, true, filter);
2234 up_fd = open_uprobe_events(false);
2235 if (kp_fd < 0 && up_fd < 0) {
2236 print_both_open_warning(kp_fd, up_fd);
2242 ret = __show_perf_probe_events(up_fd, false, filter);
2250 /* Get current perf-probe event names */
2251 static struct strlist *get_probe_trace_event_names(int fd, bool include_group)
2254 struct strlist *sl, *rawlist;
2255 struct str_node *ent;
2256 struct probe_trace_event tev;
2259 memset(&tev, 0, sizeof(tev));
2260 rawlist = get_probe_trace_command_rawlist(fd);
2263 sl = strlist__new(true, NULL);
2264 strlist__for_each(ent, rawlist) {
2265 ret = parse_probe_trace_command(ent->s, &tev);
2268 if (include_group) {
2269 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
2272 ret = strlist__add(sl, buf);
2274 ret = strlist__add(sl, tev.event);
2275 clear_probe_trace_event(&tev);
2279 strlist__delete(rawlist);
2282 strlist__delete(sl);
2288 static int write_probe_trace_event(int fd, struct probe_trace_event *tev)
2291 char *buf = synthesize_probe_trace_command(tev);
2292 char sbuf[STRERR_BUFSIZE];
2295 pr_debug("Failed to synthesize probe trace event.\n");
2299 pr_debug("Writing event: %s\n", buf);
2300 if (!probe_event_dry_run) {
2301 ret = write(fd, buf, strlen(buf));
2304 pr_warning("Failed to write event: %s\n",
2305 strerror_r(errno, sbuf, sizeof(sbuf)));
2312 static int get_new_event_name(char *buf, size_t len, const char *base,
2313 struct strlist *namelist, bool allow_suffix)
2321 ret = e_snprintf(buf, len, "%s", base);
2323 pr_debug("snprintf() failed: %d\n", ret);
2326 if (!strlist__has_entry(namelist, buf))
2329 if (!allow_suffix) {
2330 pr_warning("Error: event \"%s\" already exists. "
2331 "(Use -f to force duplicates.)\n", base);
2335 /* Try to add suffix */
2336 for (i = 1; i < MAX_EVENT_INDEX; i++) {
2337 ret = e_snprintf(buf, len, "%s_%d", base, i);
2339 pr_debug("snprintf() failed: %d\n", ret);
2342 if (!strlist__has_entry(namelist, buf))
2345 if (i == MAX_EVENT_INDEX) {
2346 pr_warning("Too many events are on the same function.\n");
2353 /* Warn if the current kernel's uprobe implementation is old */
2354 static void warn_uprobe_event_compat(struct probe_trace_event *tev)
2357 char *buf = synthesize_probe_trace_command(tev);
2359 /* Old uprobe event doesn't support memory dereference */
2360 if (!tev->uprobes || tev->nargs == 0 || !buf)
2363 for (i = 0; i < tev->nargs; i++)
2364 if (strglobmatch(tev->args[i].value, "[$@+-]*")) {
2365 pr_warning("Please upgrade your kernel to at least "
2366 "3.14 to have access to feature %s\n",
2367 tev->args[i].value);
2374 static int __add_probe_trace_events(struct perf_probe_event *pev,
2375 struct probe_trace_event *tevs,
2376 int ntevs, bool allow_suffix)
2379 struct probe_trace_event *tev = NULL;
2381 const char *event, *group;
2382 struct strlist *namelist;
2383 LIST_HEAD(blacklist);
2384 struct kprobe_blacklist_node *node;
2388 fd = open_uprobe_events(true);
2390 fd = open_kprobe_events(true);
2393 print_open_warning(fd, !pev->uprobes);
2397 /* Get current event names */
2398 namelist = get_probe_trace_event_names(fd, false);
2400 pr_debug("Failed to get current event list.\n");
2404 /* Get kprobe blacklist if exists */
2405 if (!pev->uprobes) {
2406 ret = kprobe_blacklist__load(&blacklist);
2408 pr_debug("No kprobe blacklist support, ignored\n");
2411 safename = (pev->point.function && !strisglob(pev->point.function));
2413 pr_info("Added new event%s\n", (ntevs > 1) ? "s:" : ":");
2414 for (i = 0; i < ntevs; i++) {
2416 /* Skip if the symbol is out of .text (marked previously) */
2417 if (!tev->point.symbol)
2419 /* Ensure that the address is NOT blacklisted */
2420 node = kprobe_blacklist__find_by_address(&blacklist,
2421 tev->point.address);
2423 pr_warning("Warning: Skipped probing on blacklisted function: %s\n", node->symbol);
2431 event = pev->point.function;
2433 event = tev->point.realname;
2437 group = PERFPROBE_GROUP;
2439 /* Get an unused new event name */
2440 ret = get_new_event_name(buf, 64, event,
2441 namelist, allow_suffix);
2446 tev->event = strdup(event);
2447 tev->group = strdup(group);
2448 if (tev->event == NULL || tev->group == NULL) {
2452 ret = write_probe_trace_event(fd, tev);
2455 /* Add added event name to namelist */
2456 strlist__add(namelist, event);
2458 /* Trick here - save current event/group */
2461 pev->event = tev->event;
2462 pev->group = tev->group;
2463 show_perf_probe_event(pev, tev->point.module);
2464 /* Trick here - restore current event/group */
2465 pev->event = (char *)event;
2466 pev->group = (char *)group;
2469 * Probes after the first probe which comes from same
2470 * user input are always allowed to add suffix, because
2471 * there might be several addresses corresponding to
2474 allow_suffix = true;
2476 if (ret == -EINVAL && pev->uprobes)
2477 warn_uprobe_event_compat(tev);
2479 /* Note that it is possible to skip all events because of blacklist */
2480 if (ret >= 0 && tev->event) {
2481 /* Show how to use the event. */
2482 pr_info("\nYou can now use it in all perf tools, such as:\n\n");
2483 pr_info("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group,
2487 kprobe_blacklist__delete(&blacklist);
2488 strlist__delete(namelist);
2494 static int find_probe_functions(struct map *map, char *name)
2498 struct rb_node *tmp;
2500 if (map__load(map, NULL) < 0)
2503 map__for_each_symbol(map, sym, tmp) {
2504 if (strglobmatch(sym->name, name))
2511 #define strdup_or_goto(str, label) \
2512 ({ char *__p = strdup(str); if (!__p) goto label; __p; })
2514 void __weak arch__fix_tev_from_maps(struct perf_probe_event *pev __maybe_unused,
2515 struct probe_trace_event *tev __maybe_unused,
2516 struct map *map __maybe_unused) { }
2519 * Find probe function addresses from map.
2520 * Return an error or the number of found probe_trace_event
2522 static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
2523 struct probe_trace_event **tevs)
2525 struct map *map = NULL;
2526 struct ref_reloc_sym *reloc_sym = NULL;
2528 struct probe_trace_event *tev;
2529 struct perf_probe_point *pp = &pev->point;
2530 struct probe_trace_point *tp;
2531 int num_matched_functions;
2534 map = get_target_map(pev->target, pev->uprobes);
2541 * Load matched symbols: Since the different local symbols may have
2542 * same name but different addresses, this lists all the symbols.
2544 num_matched_functions = find_probe_functions(map, pp->function);
2545 if (num_matched_functions == 0) {
2546 pr_err("Failed to find symbol %s in %s\n", pp->function,
2547 pev->target ? : "kernel");
2550 } else if (num_matched_functions > probe_conf.max_probes) {
2551 pr_err("Too many functions matched in %s\n",
2552 pev->target ? : "kernel");
2557 if (!pev->uprobes && !pp->retprobe) {
2558 reloc_sym = kernel_get_ref_reloc_sym();
2560 pr_warning("Relocated base symbol is not found!\n");
2566 /* Setup result trace-probe-events */
2567 *tevs = zalloc(sizeof(*tev) * num_matched_functions);
2575 map__for_each_symbol_by_name(map, pp->function, sym) {
2576 tev = (*tevs) + ret;
2578 if (ret == num_matched_functions) {
2579 pr_warning("Too many symbols are listed. Skip it.\n");
2584 if (pp->offset > sym->end - sym->start) {
2585 pr_warning("Offset %ld is bigger than the size of %s\n",
2586 pp->offset, sym->name);
2590 /* Add one probe point */
2591 tp->address = map->unmap_ip(map, sym->start) + pp->offset;
2593 tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
2594 tp->offset = tp->address - reloc_sym->addr;
2596 tp->symbol = strdup_or_goto(sym->name, nomem_out);
2597 tp->offset = pp->offset;
2599 tp->retprobe = pp->retprobe;
2601 tev->point.module = strdup_or_goto(pev->target,
2603 tev->uprobes = pev->uprobes;
2604 tev->nargs = pev->nargs;
2606 tev->args = zalloc(sizeof(struct probe_trace_arg) *
2608 if (tev->args == NULL)
2611 for (i = 0; i < tev->nargs; i++) {
2612 if (pev->args[i].name)
2614 strdup_or_goto(pev->args[i].name,
2617 tev->args[i].value = strdup_or_goto(pev->args[i].var,
2619 if (pev->args[i].type)
2621 strdup_or_goto(pev->args[i].type,
2624 arch__fix_tev_from_maps(pev, tev, map);
2628 put_target_map(map, pev->uprobes);
2634 clear_probe_trace_events(*tevs, num_matched_functions);
2639 bool __weak arch__prefers_symtab(void) { return false; }
2641 static int convert_to_probe_trace_events(struct perf_probe_event *pev,
2642 struct probe_trace_event **tevs)
2646 if (pev->uprobes && !pev->group) {
2647 /* Replace group name if not given */
2648 ret = convert_exec_to_group(pev->target, &pev->group);
2650 pr_warning("Failed to make a group name.\n");
2655 if (arch__prefers_symtab() && !perf_probe_event_need_dwarf(pev)) {
2656 ret = find_probe_trace_events_from_map(pev, tevs);
2658 return ret; /* Found in symbol table */
2661 /* Convert perf_probe_event with debuginfo */
2662 ret = try_to_find_probe_trace_events(pev, tevs);
2664 return ret; /* Found in debuginfo or got an error */
2666 return find_probe_trace_events_from_map(pev, tevs);
2669 struct __event_package {
2670 struct perf_probe_event *pev;
2671 struct probe_trace_event *tevs;
2675 int add_perf_probe_events(struct perf_probe_event *pevs, int npevs)
2678 struct __event_package *pkgs;
2681 pkgs = zalloc(sizeof(struct __event_package) * npevs);
2686 ret = init_symbol_maps(pevs->uprobes);
2692 /* Loop 1: convert all events */
2693 for (i = 0; i < npevs; i++) {
2694 pkgs[i].pev = &pevs[i];
2695 /* Convert with or without debuginfo */
2696 ret = convert_to_probe_trace_events(pkgs[i].pev,
2700 pkgs[i].ntevs = ret;
2703 /* Loop 2: add all events */
2704 for (i = 0; i < npevs; i++) {
2705 ret = __add_probe_trace_events(pkgs[i].pev, pkgs[i].tevs,
2707 probe_conf.force_add);
2712 /* Loop 3: cleanup and free trace events */
2713 for (i = 0; i < npevs; i++) {
2714 for (j = 0; j < pkgs[i].ntevs; j++)
2715 clear_probe_trace_event(&pkgs[i].tevs[j]);
2716 zfree(&pkgs[i].tevs);
2724 static int __del_trace_probe_event(int fd, struct str_node *ent)
2730 /* Convert from perf-probe event to trace-probe event */
2731 ret = e_snprintf(buf, 128, "-:%s", ent->s);
2735 p = strchr(buf + 2, ':');
2737 pr_debug("Internal error: %s should have ':' but not.\n",
2744 pr_debug("Writing event: %s\n", buf);
2745 ret = write(fd, buf, strlen(buf));
2751 pr_info("Removed event: %s\n", ent->s);
2754 pr_warning("Failed to delete event: %s\n",
2755 strerror_r(-ret, buf, sizeof(buf)));
2759 static int del_trace_probe_events(int fd, struct strfilter *filter,
2760 struct strlist *namelist)
2762 struct str_node *ent;
2769 strlist__for_each(ent, namelist) {
2770 p = strchr(ent->s, ':');
2771 if ((p && strfilter__compare(filter, p + 1)) ||
2772 strfilter__compare(filter, ent->s)) {
2773 ret = __del_trace_probe_event(fd, ent);
2782 int del_perf_probe_events(struct strfilter *filter)
2784 int ret, ret2, ufd = -1, kfd = -1;
2785 struct strlist *namelist = NULL, *unamelist = NULL;
2786 char *str = strfilter__string(filter);
2791 pr_debug("Delete filter: \'%s\'\n", str);
2793 /* Get current event names */
2794 kfd = open_kprobe_events(true);
2796 namelist = get_probe_trace_event_names(kfd, true);
2798 ufd = open_uprobe_events(true);
2800 unamelist = get_probe_trace_event_names(ufd, true);
2802 if (kfd < 0 && ufd < 0) {
2803 print_both_open_warning(kfd, ufd);
2808 ret = del_trace_probe_events(kfd, filter, namelist);
2809 if (ret < 0 && ret != -ENOENT)
2812 ret2 = del_trace_probe_events(ufd, filter, unamelist);
2813 if (ret2 < 0 && ret2 != -ENOENT)
2815 else if (ret == -ENOENT && ret2 == -ENOENT) {
2816 pr_debug("\"%s\" does not hit any event.\n", str);
2817 /* Note that this is silently ignored */
2823 strlist__delete(namelist);
2828 strlist__delete(unamelist);
2836 /* TODO: don't use a global variable for filter ... */
2837 static struct strfilter *available_func_filter;
2840 * If a symbol corresponds to a function with global binding and
2841 * matches filter return 0. For all others return 1.
2843 static int filter_available_functions(struct map *map __maybe_unused,
2846 if (strfilter__compare(available_func_filter, sym->name))
2851 int show_available_funcs(const char *target, struct strfilter *_filter,
2857 ret = init_symbol_maps(user);
2861 /* Get a symbol map */
2863 map = dso__new_map(target);
2865 map = kernel_get_module_map(target);
2867 pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
2871 /* Load symbols with given filter */
2872 available_func_filter = _filter;
2873 if (map__load(map, filter_available_functions)) {
2874 pr_err("Failed to load symbols in %s\n", (target) ? : "kernel");
2877 if (!dso__sorted_by_name(map->dso, map->type))
2878 dso__sort_by_name(map->dso, map->type);
2880 /* Show all (filtered) symbols */
2882 dso__fprintf_symbols_by_name(map->dso, map->type, stdout);