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 #ifdef HAVE_DWARF_SUPPORT
251 static int kernel_get_module_dso(const char *module, struct dso **pdso)
255 const char *vmlinux_name;
259 list_for_each_entry(dso, &host_machine->dsos.head, node) {
262 if (strncmp(dso->short_name + 1, module,
263 dso->short_name_len - 2) == 0)
266 pr_debug("Failed to find module %s.\n", module);
270 map = host_machine->vmlinux_maps[MAP__FUNCTION];
273 vmlinux_name = symbol_conf.vmlinux_name;
276 ret = dso__load_vmlinux(dso, map, vmlinux_name, false, NULL);
278 ret = dso__load_vmlinux_path(dso, map, NULL);
285 * Some binaries like glibc have special symbols which are on the symbol
286 * table, but not in the debuginfo. If we can find the address of the
287 * symbol from map, we can translate the address back to the probe point.
289 static int find_alternative_probe_point(struct debuginfo *dinfo,
290 struct perf_probe_point *pp,
291 struct perf_probe_point *result,
292 const char *target, bool uprobes)
294 struct map *map = NULL;
299 /* This can work only for function-name based one */
300 if (!pp->function || pp->file)
303 map = get_target_map(target, uprobes);
307 /* Find the address of given function */
308 map__for_each_symbol_by_name(map, pp->function, sym) {
310 address = sym->start;
312 address = map->unmap_ip(map, sym->start);
319 pr_debug("Symbol %s address found : %" PRIx64 "\n",
320 pp->function, address);
322 ret = debuginfo__find_probe_point(dinfo, (unsigned long)address,
325 ret = (!ret) ? -ENOENT : ret;
327 result->offset += pp->offset;
328 result->line += pp->line;
329 result->retprobe = pp->retprobe;
334 put_target_map(map, uprobes);
339 static int get_alternative_probe_event(struct debuginfo *dinfo,
340 struct perf_probe_event *pev,
341 struct perf_probe_point *tmp)
345 memcpy(tmp, &pev->point, sizeof(*tmp));
346 memset(&pev->point, 0, sizeof(pev->point));
347 ret = find_alternative_probe_point(dinfo, tmp, &pev->point,
348 pev->target, pev->uprobes);
350 memcpy(&pev->point, tmp, sizeof(*tmp));
355 static int get_alternative_line_range(struct debuginfo *dinfo,
356 struct line_range *lr,
357 const char *target, bool user)
359 struct perf_probe_point pp = { .function = lr->function,
362 struct perf_probe_point result;
365 memset(&result, 0, sizeof(result));
367 if (lr->end != INT_MAX)
368 len = lr->end - lr->start;
369 ret = find_alternative_probe_point(dinfo, &pp, &result,
372 lr->function = result.function;
373 lr->file = result.file;
374 lr->start = result.line;
375 if (lr->end != INT_MAX)
376 lr->end = lr->start + len;
377 clear_perf_probe_point(&pp);
382 /* Open new debuginfo of given module */
383 static struct debuginfo *open_debuginfo(const char *module, bool silent)
385 const char *path = module;
386 char reason[STRERR_BUFSIZE];
387 struct debuginfo *ret = NULL;
388 struct dso *dso = NULL;
391 if (!module || !strchr(module, '/')) {
392 err = kernel_get_module_dso(module, &dso);
394 if (!dso || dso->load_errno == 0) {
395 if (!strerror_r(-err, reason, STRERR_BUFSIZE))
396 strcpy(reason, "(unknown)");
398 dso__strerror_load(dso, reason, STRERR_BUFSIZE);
400 pr_err("Failed to find the path for %s: %s\n",
401 module ?: "kernel", reason);
404 path = dso->long_name;
406 ret = debuginfo__new(path);
407 if (!ret && !silent) {
408 pr_warning("The %s file has no debug information.\n", path);
409 if (!module || !strtailcmp(path, ".ko"))
410 pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, ");
412 pr_warning("Rebuild with -g, ");
413 pr_warning("or install an appropriate debuginfo package.\n");
419 static int get_text_start_address(const char *exec, unsigned long *address)
424 int fd, ret = -ENOENT;
426 fd = open(exec, O_RDONLY);
430 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
434 if (gelf_getehdr(elf, &ehdr) == NULL)
437 if (!elf_section_by_name(elf, &ehdr, &shdr, ".text", NULL))
440 *address = shdr.sh_addr - shdr.sh_offset;
448 * Convert trace point to probe point with debuginfo
450 static int find_perf_probe_point_from_dwarf(struct probe_trace_point *tp,
451 struct perf_probe_point *pp,
454 struct debuginfo *dinfo = NULL;
455 unsigned long stext = 0;
456 u64 addr = tp->address;
459 /* convert the address to dwarf address */
465 ret = get_text_start_address(tp->module, &stext);
470 addr = kernel_get_symbol_address_by_name(tp->symbol, false);
476 pr_debug("try to find information at %" PRIx64 " in %s\n", addr,
477 tp->module ? : "kernel");
479 dinfo = open_debuginfo(tp->module, verbose == 0);
481 ret = debuginfo__find_probe_point(dinfo,
482 (unsigned long)addr, pp);
483 debuginfo__delete(dinfo);
488 pp->retprobe = tp->retprobe;
492 pr_debug("Failed to find corresponding probes from debuginfo.\n");
493 return ret ? : -ENOENT;
496 static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs,
497 int ntevs, const char *exec)
500 unsigned long stext = 0;
505 ret = get_text_start_address(exec, &stext);
509 for (i = 0; i < ntevs && ret >= 0; i++) {
510 /* point.address is the addres of point.symbol + point.offset */
511 tevs[i].point.address -= stext;
512 tevs[i].point.module = strdup(exec);
513 if (!tevs[i].point.module) {
517 tevs[i].uprobes = true;
523 static int add_module_to_probe_trace_events(struct probe_trace_event *tevs,
524 int ntevs, const char *module)
532 tmp = strrchr(module, '/');
534 /* This is a module path -- get the module name */
535 module = strdup(tmp + 1);
538 tmp = strchr(module, '.');
541 tmp = (char *)module; /* For free() */
544 for (i = 0; i < ntevs; i++) {
545 tevs[i].point.module = strdup(module);
546 if (!tevs[i].point.module) {
556 /* Post processing the probe events */
557 static int post_process_probe_trace_events(struct probe_trace_event *tevs,
558 int ntevs, const char *module,
561 struct ref_reloc_sym *reloc_sym;
567 return add_exec_to_probe_trace_events(tevs, ntevs, module);
569 /* Note that currently ref_reloc_sym based probe is not for drivers */
571 return add_module_to_probe_trace_events(tevs, ntevs, module);
573 reloc_sym = kernel_get_ref_reloc_sym();
575 pr_warning("Relocated base symbol is not found!\n");
578 /* Get the address of _etext for checking non-probable text symbol */
579 etext_addr = kernel_get_symbol_address_by_name("_etext", false);
581 for (i = 0; i < ntevs; i++) {
582 if (tevs[i].point.address && !tevs[i].point.retprobe) {
583 /* If we found a wrong one, mark it by NULL symbol */
584 if (etext_addr < tevs[i].point.address) {
585 pr_warning("%s+%lu is out of .text, skip it.\n",
586 tevs[i].point.symbol, tevs[i].point.offset);
590 tmp = strdup(reloc_sym->name);
594 /* If we have no realname, use symbol for it */
595 if (!tevs[i].point.realname)
596 tevs[i].point.realname = tevs[i].point.symbol;
598 free(tevs[i].point.symbol);
599 tevs[i].point.symbol = tmp;
600 tevs[i].point.offset = tevs[i].point.address -
601 reloc_sym->unrelocated_addr;
607 /* Try to find perf_probe_event with debuginfo */
608 static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
609 struct probe_trace_event **tevs)
611 bool need_dwarf = perf_probe_event_need_dwarf(pev);
612 struct perf_probe_point tmp;
613 struct debuginfo *dinfo;
616 dinfo = open_debuginfo(pev->target, !need_dwarf);
620 pr_debug("Could not open debuginfo. Try to use symbols.\n");
624 pr_debug("Try to find probe point from debuginfo.\n");
625 /* Searching trace events corresponding to a probe event */
626 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
628 if (ntevs == 0) { /* Not found, retry with an alternative */
629 ret = get_alternative_probe_event(dinfo, pev, &tmp);
631 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
633 * Write back to the original probe_event for
634 * setting appropriate (user given) event name
636 clear_perf_probe_point(&pev->point);
637 memcpy(&pev->point, &tmp, sizeof(tmp));
641 debuginfo__delete(dinfo);
643 if (ntevs > 0) { /* Succeeded to find trace events */
644 pr_debug("Found %d probe_trace_events.\n", ntevs);
645 ret = post_process_probe_trace_events(*tevs, ntevs,
646 pev->target, pev->uprobes);
647 if (ret < 0 || ret == ntevs) {
648 clear_probe_trace_events(*tevs, ntevs);
652 return ret < 0 ? ret : ntevs;
657 if (ntevs == 0) { /* No error but failed to find probe point. */
658 pr_warning("Probe point '%s' not found.\n",
659 synthesize_perf_probe_point(&pev->point));
662 /* Error path : ntevs < 0 */
663 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
664 if (ntevs == -EBADF) {
665 pr_warning("Warning: No dwarf info found in the vmlinux - "
666 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
668 pr_debug("Trying to use symbols.\n");
675 #define LINEBUF_SIZE 256
676 #define NR_ADDITIONAL_LINES 2
678 static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
680 char buf[LINEBUF_SIZE], sbuf[STRERR_BUFSIZE];
681 const char *color = show_num ? "" : PERF_COLOR_BLUE;
682 const char *prefix = NULL;
685 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
690 prefix = show_num ? "%7d " : " ";
691 color_fprintf(stdout, color, prefix, l);
693 color_fprintf(stdout, color, "%s", buf);
695 } while (strchr(buf, '\n') == NULL);
700 pr_warning("File read error: %s\n",
701 strerror_r(errno, sbuf, sizeof(sbuf)));
707 static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
709 int rv = __show_one_line(fp, l, skip, show_num);
711 pr_warning("Source file is shorter than expected.\n");
717 #define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
718 #define show_one_line(f,l) _show_one_line(f,l,false,false)
719 #define skip_one_line(f,l) _show_one_line(f,l,true,false)
720 #define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
723 * Show line-range always requires debuginfo to find source file and
726 static int __show_line_range(struct line_range *lr, const char *module,
731 struct debuginfo *dinfo;
735 char sbuf[STRERR_BUFSIZE];
737 /* Search a line range */
738 dinfo = open_debuginfo(module, false);
742 ret = debuginfo__find_line_range(dinfo, lr);
743 if (!ret) { /* Not found, retry with an alternative */
744 ret = get_alternative_line_range(dinfo, lr, module, user);
746 ret = debuginfo__find_line_range(dinfo, lr);
748 debuginfo__delete(dinfo);
749 if (ret == 0 || ret == -ENOENT) {
750 pr_warning("Specified source line is not found.\n");
752 } else if (ret < 0) {
753 pr_warning("Debuginfo analysis failed.\n");
757 /* Convert source file path */
759 ret = get_real_path(tmp, lr->comp_dir, &lr->path);
761 /* Free old path when new path is assigned */
766 pr_warning("Failed to find source file path.\n");
773 fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
774 lr->start - lr->offset);
776 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
778 fp = fopen(lr->path, "r");
780 pr_warning("Failed to open %s: %s\n", lr->path,
781 strerror_r(errno, sbuf, sizeof(sbuf)));
784 /* Skip to starting line number */
785 while (l < lr->start) {
786 ret = skip_one_line(fp, l++);
791 intlist__for_each(ln, lr->line_list) {
792 for (; ln->i > l; l++) {
793 ret = show_one_line(fp, l - lr->offset);
797 ret = show_one_line_with_num(fp, l++ - lr->offset);
802 if (lr->end == INT_MAX)
803 lr->end = l + NR_ADDITIONAL_LINES;
804 while (l <= lr->end) {
805 ret = show_one_line_or_eof(fp, l++ - lr->offset);
814 int show_line_range(struct line_range *lr, const char *module, bool user)
818 ret = init_symbol_maps(user);
821 ret = __show_line_range(lr, module, user);
827 static int show_available_vars_at(struct debuginfo *dinfo,
828 struct perf_probe_event *pev,
829 struct strfilter *_filter)
833 struct str_node *node;
834 struct variable_list *vls = NULL, *vl;
835 struct perf_probe_point tmp;
838 buf = synthesize_perf_probe_point(&pev->point);
841 pr_debug("Searching variables at %s\n", buf);
843 ret = debuginfo__find_available_vars_at(dinfo, pev, &vls);
844 if (!ret) { /* Not found, retry with an alternative */
845 ret = get_alternative_probe_event(dinfo, pev, &tmp);
847 ret = debuginfo__find_available_vars_at(dinfo, pev,
849 /* Release the old probe_point */
850 clear_perf_probe_point(&tmp);
854 if (ret == 0 || ret == -ENOENT) {
855 pr_err("Failed to find the address of %s\n", buf);
858 pr_warning("Debuginfo analysis failed.\n");
862 /* Some variables are found */
863 fprintf(stdout, "Available variables at %s\n", buf);
864 for (i = 0; i < ret; i++) {
867 * A probe point might be converted to
868 * several trace points.
870 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
872 zfree(&vl->point.symbol);
875 strlist__for_each(node, vl->vars) {
876 var = strchr(node->s, '\t') + 1;
877 if (strfilter__compare(_filter, var)) {
878 fprintf(stdout, "\t\t%s\n", node->s);
882 strlist__delete(vl->vars);
885 fprintf(stdout, "\t\t(No matched variables)\n");
893 /* Show available variables on given probe point */
894 int show_available_vars(struct perf_probe_event *pevs, int npevs,
895 struct strfilter *_filter)
898 struct debuginfo *dinfo;
900 ret = init_symbol_maps(pevs->uprobes);
904 dinfo = open_debuginfo(pevs->target, false);
912 for (i = 0; i < npevs && ret >= 0; i++)
913 ret = show_available_vars_at(dinfo, &pevs[i], _filter);
915 debuginfo__delete(dinfo);
921 #else /* !HAVE_DWARF_SUPPORT */
924 find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused,
925 struct perf_probe_point *pp __maybe_unused,
926 bool is_kprobe __maybe_unused)
931 static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
932 struct probe_trace_event **tevs __maybe_unused)
934 if (perf_probe_event_need_dwarf(pev)) {
935 pr_warning("Debuginfo-analysis is not supported.\n");
942 int show_line_range(struct line_range *lr __maybe_unused,
943 const char *module __maybe_unused,
944 bool user __maybe_unused)
946 pr_warning("Debuginfo-analysis is not supported.\n");
950 int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
951 int npevs __maybe_unused,
952 struct strfilter *filter __maybe_unused)
954 pr_warning("Debuginfo-analysis is not supported.\n");
959 void line_range__clear(struct line_range *lr)
965 intlist__delete(lr->line_list);
966 memset(lr, 0, sizeof(*lr));
969 int line_range__init(struct line_range *lr)
971 memset(lr, 0, sizeof(*lr));
972 lr->line_list = intlist__new(NULL);
979 static int parse_line_num(char **ptr, int *val, const char *what)
981 const char *start = *ptr;
984 *val = strtol(*ptr, ptr, 0);
985 if (errno || *ptr == start) {
986 semantic_error("'%s' is not a valid number.\n", what);
992 /* Check the name is good for event, group or function */
993 static bool is_c_func_name(const char *name)
995 if (!isalpha(*name) && *name != '_')
997 while (*++name != '\0') {
998 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
1005 * Stuff 'lr' according to the line range described by 'arg'.
1006 * The line range syntax is described by:
1008 * SRC[:SLN[+NUM|-ELN]]
1009 * FNC[@SRC][:SLN[+NUM|-ELN]]
1011 int parse_line_range_desc(const char *arg, struct line_range *lr)
1013 char *range, *file, *name = strdup(arg);
1022 range = strchr(name, ':');
1026 err = parse_line_num(&range, &lr->start, "start line");
1030 if (*range == '+' || *range == '-') {
1031 const char c = *range++;
1033 err = parse_line_num(&range, &lr->end, "end line");
1038 lr->end += lr->start;
1040 * Adjust the number of lines here.
1041 * If the number of lines == 1, the
1042 * the end of line should be equal to
1043 * the start of line.
1049 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
1052 if (lr->start > lr->end) {
1053 semantic_error("Start line must be smaller"
1054 " than end line.\n");
1057 if (*range != '\0') {
1058 semantic_error("Tailing with invalid str '%s'.\n", range);
1063 file = strchr(name, '@');
1066 lr->file = strdup(++file);
1067 if (lr->file == NULL) {
1071 lr->function = name;
1072 } else if (strchr(name, '/') || strchr(name, '.'))
1074 else if (is_c_func_name(name))/* We reuse it for checking funcname */
1075 lr->function = name;
1076 else { /* Invalid name */
1077 semantic_error("'%s' is not a valid function name.\n", name);
1088 /* Parse probepoint definition. */
1089 static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
1091 struct perf_probe_point *pp = &pev->point;
1094 bool file_spec = false;
1097 * perf probe [EVENT=]SRC[:LN|;PTN]
1098 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
1100 * TODO:Group name support
1105 ptr = strpbrk(arg, ";=@+%");
1106 if (ptr && *ptr == '=') { /* Event name */
1109 if (strchr(arg, ':')) {
1110 semantic_error("Group name is not supported yet.\n");
1113 if (!is_c_func_name(arg)) {
1114 semantic_error("%s is bad for event name -it must "
1115 "follow C symbol-naming rule.\n", arg);
1118 pev->event = strdup(arg);
1119 if (pev->event == NULL)
1126 * Check arg is function or file name and copy it.
1128 * We consider arg to be a file spec if and only if it satisfies
1129 * all of the below criteria::
1130 * - it does not include any of "+@%",
1131 * - it includes one of ":;", and
1132 * - it has a period '.' in the name.
1134 * Otherwise, we consider arg to be a function specification.
1136 if (!strpbrk(arg, "+@%") && (ptr = strpbrk(arg, ";:")) != NULL) {
1137 /* This is a file spec if it includes a '.' before ; or : */
1138 if (memchr(arg, '.', ptr - arg))
1142 ptr = strpbrk(arg, ";:+@%");
1157 /* Parse other options */
1161 if (c == ';') { /* Lazy pattern must be the last part */
1162 pp->lazy_line = strdup(arg);
1163 if (pp->lazy_line == NULL)
1167 ptr = strpbrk(arg, ";:+@%");
1173 case ':': /* Line number */
1174 pp->line = strtoul(arg, &tmp, 0);
1176 semantic_error("There is non-digit char"
1177 " in line number.\n");
1181 case '+': /* Byte offset from a symbol */
1182 pp->offset = strtoul(arg, &tmp, 0);
1184 semantic_error("There is non-digit character"
1189 case '@': /* File name */
1191 semantic_error("SRC@SRC is not allowed.\n");
1194 pp->file = strdup(arg);
1195 if (pp->file == NULL)
1198 case '%': /* Probe places */
1199 if (strcmp(arg, "return") == 0) {
1201 } else { /* Others not supported yet */
1202 semantic_error("%%%s is not supported.\n", arg);
1206 default: /* Buggy case */
1207 pr_err("This program has a bug at %s:%d.\n",
1208 __FILE__, __LINE__);
1214 /* Exclusion check */
1215 if (pp->lazy_line && pp->line) {
1216 semantic_error("Lazy pattern can't be used with"
1221 if (pp->lazy_line && pp->offset) {
1222 semantic_error("Lazy pattern can't be used with offset.\n");
1226 if (pp->line && pp->offset) {
1227 semantic_error("Offset can't be used with line number.\n");
1231 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
1232 semantic_error("File always requires line number or "
1237 if (pp->offset && !pp->function) {
1238 semantic_error("Offset requires an entry function.\n");
1242 if (pp->retprobe && !pp->function) {
1243 semantic_error("Return probe requires an entry function.\n");
1247 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
1248 semantic_error("Offset/Line/Lazy pattern can't be used with "
1253 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
1254 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
1259 /* Parse perf-probe event argument */
1260 static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
1262 char *tmp, *goodname;
1263 struct perf_probe_arg_field **fieldp;
1265 pr_debug("parsing arg: %s into ", str);
1267 tmp = strchr(str, '=');
1269 arg->name = strndup(str, tmp - str);
1270 if (arg->name == NULL)
1272 pr_debug("name:%s ", arg->name);
1276 tmp = strchr(str, ':');
1277 if (tmp) { /* Type setting */
1279 arg->type = strdup(tmp + 1);
1280 if (arg->type == NULL)
1282 pr_debug("type:%s ", arg->type);
1285 tmp = strpbrk(str, "-.[");
1286 if (!is_c_varname(str) || !tmp) {
1287 /* A variable, register, symbol or special value */
1288 arg->var = strdup(str);
1289 if (arg->var == NULL)
1291 pr_debug("%s\n", arg->var);
1295 /* Structure fields or array element */
1296 arg->var = strndup(str, tmp - str);
1297 if (arg->var == NULL)
1299 goodname = arg->var;
1300 pr_debug("%s, ", arg->var);
1301 fieldp = &arg->field;
1304 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
1305 if (*fieldp == NULL)
1307 if (*tmp == '[') { /* Array */
1309 (*fieldp)->index = strtol(str + 1, &tmp, 0);
1310 (*fieldp)->ref = true;
1311 if (*tmp != ']' || tmp == str + 1) {
1312 semantic_error("Array index must be a"
1319 } else { /* Structure */
1322 (*fieldp)->ref = false;
1323 } else if (tmp[1] == '>') {
1325 (*fieldp)->ref = true;
1327 semantic_error("Argument parse error: %s\n",
1331 tmp = strpbrk(str, "-.[");
1334 (*fieldp)->name = strndup(str, tmp - str);
1335 if ((*fieldp)->name == NULL)
1338 goodname = (*fieldp)->name;
1339 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
1340 fieldp = &(*fieldp)->next;
1343 (*fieldp)->name = strdup(str);
1344 if ((*fieldp)->name == NULL)
1347 goodname = (*fieldp)->name;
1348 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
1350 /* If no name is specified, set the last field name (not array index)*/
1352 arg->name = strdup(goodname);
1353 if (arg->name == NULL)
1359 /* Parse perf-probe event command */
1360 int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
1363 int argc, i, ret = 0;
1365 argv = argv_split(cmd, &argc);
1367 pr_debug("Failed to split arguments.\n");
1370 if (argc - 1 > MAX_PROBE_ARGS) {
1371 semantic_error("Too many probe arguments (%d).\n", argc - 1);
1375 /* Parse probe point */
1376 ret = parse_perf_probe_point(argv[0], pev);
1380 /* Copy arguments and ensure return probe has no C argument */
1381 pev->nargs = argc - 1;
1382 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1383 if (pev->args == NULL) {
1387 for (i = 0; i < pev->nargs && ret >= 0; i++) {
1388 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1390 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
1391 semantic_error("You can't specify local variable for"
1402 /* Return true if this perf_probe_event requires debuginfo */
1403 bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
1407 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1410 for (i = 0; i < pev->nargs; i++)
1411 if (is_c_varname(pev->args[i].var))
1417 /* Parse probe_events event into struct probe_point */
1418 static int parse_probe_trace_command(const char *cmd,
1419 struct probe_trace_event *tev)
1421 struct probe_trace_point *tp = &tev->point;
1424 char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
1428 pr_debug("Parsing probe_events: %s\n", cmd);
1429 argv = argv_split(cmd, &argc);
1431 pr_debug("Failed to split arguments.\n");
1435 semantic_error("Too few probe arguments.\n");
1440 /* Scan event and group name. */
1441 argv0_str = strdup(argv[0]);
1442 if (argv0_str == NULL) {
1446 fmt1_str = strtok_r(argv0_str, ":", &fmt);
1447 fmt2_str = strtok_r(NULL, "/", &fmt);
1448 fmt3_str = strtok_r(NULL, " \t", &fmt);
1449 if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
1450 || fmt3_str == NULL) {
1451 semantic_error("Failed to parse event name: %s\n", argv[0]);
1456 tev->group = strdup(fmt2_str);
1457 tev->event = strdup(fmt3_str);
1458 if (tev->group == NULL || tev->event == NULL) {
1462 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
1464 tp->retprobe = (pr == 'r');
1466 /* Scan module name(if there), function name and offset */
1467 p = strchr(argv[1], ':');
1469 tp->module = strndup(argv[1], p - argv[1]);
1473 fmt1_str = strtok_r(p, "+", &fmt);
1474 if (fmt1_str[0] == '0') /* only the address started with 0x */
1475 tp->address = strtoul(fmt1_str, NULL, 0);
1477 /* Only the symbol-based probe has offset */
1478 tp->symbol = strdup(fmt1_str);
1479 if (tp->symbol == NULL) {
1483 fmt2_str = strtok_r(NULL, "", &fmt);
1484 if (fmt2_str == NULL)
1487 tp->offset = strtoul(fmt2_str, NULL, 10);
1490 tev->nargs = argc - 2;
1491 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
1492 if (tev->args == NULL) {
1496 for (i = 0; i < tev->nargs; i++) {
1497 p = strchr(argv[i + 2], '=');
1498 if (p) /* We don't need which register is assigned. */
1502 tev->args[i].name = strdup(argv[i + 2]);
1503 /* TODO: parse regs and offset */
1504 tev->args[i].value = strdup(p);
1505 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1517 /* Compose only probe arg */
1518 int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
1520 struct perf_probe_arg_field *field = pa->field;
1524 if (pa->name && pa->var)
1525 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
1527 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
1534 if (field->name[0] == '[')
1535 ret = e_snprintf(tmp, len, "%s", field->name);
1537 ret = e_snprintf(tmp, len, "%s%s",
1538 field->ref ? "->" : ".", field->name);
1543 field = field->next;
1547 ret = e_snprintf(tmp, len, ":%s", pa->type);
1556 pr_debug("Failed to synthesize perf probe argument: %d\n", ret);
1560 /* Compose only probe point (not argument) */
1561 static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
1564 char offs[32] = "", line[32] = "", file[32] = "";
1567 buf = zalloc(MAX_CMDLEN);
1573 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
1578 ret = e_snprintf(line, 32, ":%d", pp->line);
1586 tmp = strchr(pp->file + len - 30, '/');
1587 tmp = tmp ? tmp + 1 : pp->file + len - 30;
1589 ret = e_snprintf(file, 32, "@%s", tmp);
1595 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
1596 offs, pp->retprobe ? "%return" : "", line,
1599 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
1605 pr_debug("Failed to synthesize perf probe point: %d\n", ret);
1611 char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1616 buf = synthesize_perf_probe_point(&pev->point);
1621 for (i = 0; i < pev->nargs; i++) {
1622 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
1635 static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
1636 char **buf, size_t *buflen,
1641 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
1647 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
1659 static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
1660 char *buf, size_t buflen)
1662 struct probe_trace_arg_ref *ref = arg->ref;
1666 /* Argument name or separator */
1668 ret = e_snprintf(buf, buflen, " %s=", arg->name);
1670 ret = e_snprintf(buf, buflen, " ");
1676 /* Special case: @XXX */
1677 if (arg->value[0] == '@' && arg->ref)
1680 /* Dereferencing arguments */
1682 depth = __synthesize_probe_trace_arg_ref(ref, &buf,
1688 /* Print argument value */
1689 if (arg->value[0] == '@' && arg->ref)
1690 ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
1693 ret = e_snprintf(buf, buflen, "%s", arg->value);
1701 ret = e_snprintf(buf, buflen, ")");
1707 /* Print argument type */
1709 ret = e_snprintf(buf, buflen, ":%s", arg->type);
1718 char *synthesize_probe_trace_command(struct probe_trace_event *tev)
1720 struct probe_trace_point *tp = &tev->point;
1724 buf = zalloc(MAX_CMDLEN);
1728 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
1729 tev->group, tev->event);
1733 /* Uprobes must have tp->address and tp->module */
1734 if (tev->uprobes && (!tp->address || !tp->module))
1737 /* Use the tp->address for uprobes */
1739 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s:0x%lx",
1740 tp->module, tp->address);
1742 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s%s%s+%lu",
1743 tp->module ?: "", tp->module ? ":" : "",
1744 tp->symbol, tp->offset);
1750 for (i = 0; i < tev->nargs; i++) {
1751 ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
1764 static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
1765 struct perf_probe_point *pp,
1768 struct symbol *sym = NULL;
1774 map = dso__new_map(tp->module);
1778 sym = map__find_symbol(map, addr, NULL);
1780 addr = kernel_get_symbol_address_by_name(tp->symbol, true);
1783 sym = __find_kernel_function(addr, &map);
1789 pp->retprobe = tp->retprobe;
1790 pp->offset = addr - map->unmap_ip(map, sym->start);
1791 pp->function = strdup(sym->name);
1792 ret = pp->function ? 0 : -ENOMEM;
1795 if (map && !is_kprobe) {
1802 static int convert_to_perf_probe_point(struct probe_trace_point *tp,
1803 struct perf_probe_point *pp,
1809 ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe);
1812 ret = find_perf_probe_point_from_map(tp, pp, is_kprobe);
1816 pr_debug("Failed to find probe point from both of dwarf and map.\n");
1819 pp->function = strdup(tp->symbol);
1820 pp->offset = tp->offset;
1821 } else if (!tp->module && !is_kprobe) {
1822 ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address);
1825 pp->function = strdup(buf);
1828 if (pp->function == NULL)
1831 pp->retprobe = tp->retprobe;
1836 static int convert_to_perf_probe_event(struct probe_trace_event *tev,
1837 struct perf_probe_event *pev, bool is_kprobe)
1842 /* Convert event/group name */
1843 pev->event = strdup(tev->event);
1844 pev->group = strdup(tev->group);
1845 if (pev->event == NULL || pev->group == NULL)
1848 /* Convert trace_point to probe_point */
1849 ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
1853 /* Convert trace_arg to probe_arg */
1854 pev->nargs = tev->nargs;
1855 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1856 if (pev->args == NULL)
1858 for (i = 0; i < tev->nargs && ret >= 0; i++) {
1859 if (tev->args[i].name)
1860 pev->args[i].name = strdup(tev->args[i].name);
1862 ret = synthesize_probe_trace_arg(&tev->args[i],
1864 pev->args[i].name = strdup(buf);
1866 if (pev->args[i].name == NULL && ret >= 0)
1871 clear_perf_probe_event(pev);
1876 void clear_perf_probe_event(struct perf_probe_event *pev)
1878 struct perf_probe_arg_field *field, *next;
1884 clear_perf_probe_point(&pev->point);
1886 for (i = 0; i < pev->nargs; i++) {
1887 free(pev->args[i].name);
1888 free(pev->args[i].var);
1889 free(pev->args[i].type);
1890 field = pev->args[i].field;
1893 zfree(&field->name);
1899 memset(pev, 0, sizeof(*pev));
1902 static void clear_probe_trace_event(struct probe_trace_event *tev)
1904 struct probe_trace_arg_ref *ref, *next;
1909 free(tev->point.symbol);
1910 free(tev->point.realname);
1911 free(tev->point.module);
1912 for (i = 0; i < tev->nargs; i++) {
1913 free(tev->args[i].name);
1914 free(tev->args[i].value);
1915 free(tev->args[i].type);
1916 ref = tev->args[i].ref;
1924 memset(tev, 0, sizeof(*tev));
1927 static void print_open_warning(int err, bool is_kprobe)
1929 char sbuf[STRERR_BUFSIZE];
1931 if (err == -ENOENT) {
1935 config = "CONFIG_UPROBE_EVENTS";
1937 config = "CONFIG_KPROBE_EVENTS";
1939 pr_warning("%cprobe_events file does not exist"
1940 " - please rebuild kernel with %s.\n",
1941 is_kprobe ? 'k' : 'u', config);
1942 } else if (err == -ENOTSUP)
1943 pr_warning("Tracefs or debugfs is not mounted.\n");
1945 pr_warning("Failed to open %cprobe_events: %s\n",
1946 is_kprobe ? 'k' : 'u',
1947 strerror_r(-err, sbuf, sizeof(sbuf)));
1950 static void print_both_open_warning(int kerr, int uerr)
1952 /* Both kprobes and uprobes are disabled, warn it. */
1953 if (kerr == -ENOTSUP && uerr == -ENOTSUP)
1954 pr_warning("Tracefs or debugfs is not mounted.\n");
1955 else if (kerr == -ENOENT && uerr == -ENOENT)
1956 pr_warning("Please rebuild kernel with CONFIG_KPROBE_EVENTS "
1957 "or/and CONFIG_UPROBE_EVENTS.\n");
1959 char sbuf[STRERR_BUFSIZE];
1960 pr_warning("Failed to open kprobe events: %s.\n",
1961 strerror_r(-kerr, sbuf, sizeof(sbuf)));
1962 pr_warning("Failed to open uprobe events: %s.\n",
1963 strerror_r(-uerr, sbuf, sizeof(sbuf)));
1967 static int open_probe_events(const char *trace_file, bool readwrite)
1970 const char *__debugfs;
1971 const char *tracing_dir = "";
1974 __debugfs = tracefs_find_mountpoint();
1975 if (__debugfs == NULL) {
1976 tracing_dir = "tracing/";
1978 __debugfs = debugfs_find_mountpoint();
1979 if (__debugfs == NULL)
1983 ret = e_snprintf(buf, PATH_MAX, "%s/%s%s",
1984 __debugfs, tracing_dir, trace_file);
1986 pr_debug("Opening %s write=%d\n", buf, readwrite);
1987 if (readwrite && !probe_event_dry_run)
1988 ret = open(buf, O_RDWR | O_APPEND, 0);
1990 ret = open(buf, O_RDONLY, 0);
1998 static int open_kprobe_events(bool readwrite)
2000 return open_probe_events("kprobe_events", readwrite);
2003 static int open_uprobe_events(bool readwrite)
2005 return open_probe_events("uprobe_events", readwrite);
2008 /* Get raw string list of current kprobe_events or uprobe_events */
2009 static struct strlist *get_probe_trace_command_rawlist(int fd)
2013 char buf[MAX_CMDLEN];
2017 sl = strlist__new(true, NULL);
2019 fp = fdopen(dup(fd), "r");
2021 p = fgets(buf, MAX_CMDLEN, fp);
2025 idx = strlen(p) - 1;
2028 ret = strlist__add(sl, buf);
2030 pr_debug("strlist__add failed (%d)\n", ret);
2031 strlist__delete(sl);
2040 struct kprobe_blacklist_node {
2041 struct list_head list;
2042 unsigned long start;
2047 static void kprobe_blacklist__delete(struct list_head *blacklist)
2049 struct kprobe_blacklist_node *node;
2051 while (!list_empty(blacklist)) {
2052 node = list_first_entry(blacklist,
2053 struct kprobe_blacklist_node, list);
2054 list_del(&node->list);
2060 static int kprobe_blacklist__load(struct list_head *blacklist)
2062 struct kprobe_blacklist_node *node;
2063 const char *__debugfs = debugfs_find_mountpoint();
2064 char buf[PATH_MAX], *p;
2068 if (__debugfs == NULL)
2071 ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs);
2075 fp = fopen(buf, "r");
2080 while (fgets(buf, PATH_MAX, fp)) {
2081 node = zalloc(sizeof(*node));
2086 INIT_LIST_HEAD(&node->list);
2087 list_add_tail(&node->list, blacklist);
2088 if (sscanf(buf, "0x%lx-0x%lx", &node->start, &node->end) != 2) {
2092 p = strchr(buf, '\t');
2095 if (p[strlen(p) - 1] == '\n')
2096 p[strlen(p) - 1] = '\0';
2098 p = (char *)"unknown";
2099 node->symbol = strdup(p);
2100 if (!node->symbol) {
2104 pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
2105 node->start, node->end, node->symbol);
2109 kprobe_blacklist__delete(blacklist);
2115 static struct kprobe_blacklist_node *
2116 kprobe_blacklist__find_by_address(struct list_head *blacklist,
2117 unsigned long address)
2119 struct kprobe_blacklist_node *node;
2121 list_for_each_entry(node, blacklist, list) {
2122 if (node->start <= address && address <= node->end)
2129 static int perf_probe_event__sprintf(struct perf_probe_event *pev,
2131 struct strbuf *result)
2137 /* Synthesize only event probe point */
2138 place = synthesize_perf_probe_point(&pev->point);
2142 ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
2146 strbuf_addf(result, " %-20s (on %s", buf, place);
2148 strbuf_addf(result, " in %s", module);
2150 if (pev->nargs > 0) {
2151 strbuf_addstr(result, " with");
2152 for (i = 0; i < pev->nargs; i++) {
2153 ret = synthesize_perf_probe_arg(&pev->args[i],
2157 strbuf_addf(result, " %s", buf);
2160 strbuf_addch(result, ')');
2167 static int show_perf_probe_event(struct perf_probe_event *pev,
2168 const char *module, bool use_stdout)
2170 struct strbuf buf = STRBUF_INIT;
2173 ret = perf_probe_event__sprintf(pev, module, &buf);
2176 printf("%s\n", buf.buf);
2178 pr_info("%s\n", buf.buf);
2180 strbuf_release(&buf);
2185 static bool filter_probe_trace_event(struct probe_trace_event *tev,
2186 struct strfilter *filter)
2190 /* At first, check the event name itself */
2191 if (strfilter__compare(filter, tev->event))
2194 /* Next, check the combination of name and group */
2195 if (e_snprintf(tmp, 128, "%s:%s", tev->group, tev->event) < 0)
2197 return strfilter__compare(filter, tmp);
2200 static int __show_perf_probe_events(int fd, bool is_kprobe,
2201 struct strfilter *filter)
2204 struct probe_trace_event tev;
2205 struct perf_probe_event pev;
2206 struct strlist *rawlist;
2207 struct str_node *ent;
2209 memset(&tev, 0, sizeof(tev));
2210 memset(&pev, 0, sizeof(pev));
2212 rawlist = get_probe_trace_command_rawlist(fd);
2216 strlist__for_each(ent, rawlist) {
2217 ret = parse_probe_trace_command(ent->s, &tev);
2219 if (!filter_probe_trace_event(&tev, filter))
2221 ret = convert_to_perf_probe_event(&tev, &pev,
2225 ret = show_perf_probe_event(&pev, tev.point.module,
2229 clear_perf_probe_event(&pev);
2230 clear_probe_trace_event(&tev);
2234 strlist__delete(rawlist);
2239 /* List up current perf-probe events */
2240 int show_perf_probe_events(struct strfilter *filter)
2242 int kp_fd, up_fd, ret;
2246 ret = init_symbol_maps(false);
2250 kp_fd = open_kprobe_events(false);
2252 ret = __show_perf_probe_events(kp_fd, true, filter);
2258 up_fd = open_uprobe_events(false);
2259 if (kp_fd < 0 && up_fd < 0) {
2260 print_both_open_warning(kp_fd, up_fd);
2266 ret = __show_perf_probe_events(up_fd, false, filter);
2274 /* Get current perf-probe event names */
2275 static struct strlist *get_probe_trace_event_names(int fd, bool include_group)
2278 struct strlist *sl, *rawlist;
2279 struct str_node *ent;
2280 struct probe_trace_event tev;
2283 memset(&tev, 0, sizeof(tev));
2284 rawlist = get_probe_trace_command_rawlist(fd);
2287 sl = strlist__new(true, NULL);
2288 strlist__for_each(ent, rawlist) {
2289 ret = parse_probe_trace_command(ent->s, &tev);
2292 if (include_group) {
2293 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
2296 ret = strlist__add(sl, buf);
2298 ret = strlist__add(sl, tev.event);
2299 clear_probe_trace_event(&tev);
2303 strlist__delete(rawlist);
2306 strlist__delete(sl);
2312 static int write_probe_trace_event(int fd, struct probe_trace_event *tev)
2315 char *buf = synthesize_probe_trace_command(tev);
2316 char sbuf[STRERR_BUFSIZE];
2319 pr_debug("Failed to synthesize probe trace event.\n");
2323 pr_debug("Writing event: %s\n", buf);
2324 if (!probe_event_dry_run) {
2325 ret = write(fd, buf, strlen(buf));
2328 pr_warning("Failed to write event: %s\n",
2329 strerror_r(errno, sbuf, sizeof(sbuf)));
2336 static int get_new_event_name(char *buf, size_t len, const char *base,
2337 struct strlist *namelist, bool allow_suffix)
2346 ret = e_snprintf(buf, len, "%s", base);
2348 pr_debug("snprintf() failed: %d\n", ret);
2351 /* Cut off the postfixes (e.g. .const, .isra)*/
2352 p = strchr(buf, '.');
2355 if (!strlist__has_entry(namelist, buf))
2358 if (!allow_suffix) {
2359 pr_warning("Error: event \"%s\" already exists. "
2360 "(Use -f to force duplicates.)\n", base);
2364 /* Try to add suffix */
2365 for (i = 1; i < MAX_EVENT_INDEX; i++) {
2366 ret = e_snprintf(buf, len, "%s_%d", base, i);
2368 pr_debug("snprintf() failed: %d\n", ret);
2371 if (!strlist__has_entry(namelist, buf))
2374 if (i == MAX_EVENT_INDEX) {
2375 pr_warning("Too many events are on the same function.\n");
2382 /* Warn if the current kernel's uprobe implementation is old */
2383 static void warn_uprobe_event_compat(struct probe_trace_event *tev)
2386 char *buf = synthesize_probe_trace_command(tev);
2388 /* Old uprobe event doesn't support memory dereference */
2389 if (!tev->uprobes || tev->nargs == 0 || !buf)
2392 for (i = 0; i < tev->nargs; i++)
2393 if (strglobmatch(tev->args[i].value, "[$@+-]*")) {
2394 pr_warning("Please upgrade your kernel to at least "
2395 "3.14 to have access to feature %s\n",
2396 tev->args[i].value);
2403 static int __add_probe_trace_events(struct perf_probe_event *pev,
2404 struct probe_trace_event *tevs,
2405 int ntevs, bool allow_suffix)
2408 struct probe_trace_event *tev = NULL;
2410 const char *event, *group;
2411 struct strlist *namelist;
2412 LIST_HEAD(blacklist);
2413 struct kprobe_blacklist_node *node;
2417 fd = open_uprobe_events(true);
2419 fd = open_kprobe_events(true);
2422 print_open_warning(fd, !pev->uprobes);
2426 /* Get current event names */
2427 namelist = get_probe_trace_event_names(fd, false);
2429 pr_debug("Failed to get current event list.\n");
2433 /* Get kprobe blacklist if exists */
2434 if (!pev->uprobes) {
2435 ret = kprobe_blacklist__load(&blacklist);
2437 pr_debug("No kprobe blacklist support, ignored\n");
2440 safename = (pev->point.function && !strisglob(pev->point.function));
2442 pr_info("Added new event%s\n", (ntevs > 1) ? "s:" : ":");
2443 for (i = 0; i < ntevs; i++) {
2445 /* Skip if the symbol is out of .text (marked previously) */
2446 if (!tev->point.symbol)
2448 /* Ensure that the address is NOT blacklisted */
2449 node = kprobe_blacklist__find_by_address(&blacklist,
2450 tev->point.address);
2452 pr_warning("Warning: Skipped probing on blacklisted function: %s\n", node->symbol);
2460 event = pev->point.function;
2462 event = tev->point.realname;
2466 group = PERFPROBE_GROUP;
2468 /* Get an unused new event name */
2469 ret = get_new_event_name(buf, 64, event,
2470 namelist, allow_suffix);
2475 tev->event = strdup(event);
2476 tev->group = strdup(group);
2477 if (tev->event == NULL || tev->group == NULL) {
2481 ret = write_probe_trace_event(fd, tev);
2484 /* Add added event name to namelist */
2485 strlist__add(namelist, event);
2487 /* Trick here - save current event/group */
2490 pev->event = tev->event;
2491 pev->group = tev->group;
2492 show_perf_probe_event(pev, tev->point.module, false);
2493 /* Trick here - restore current event/group */
2494 pev->event = (char *)event;
2495 pev->group = (char *)group;
2498 * Probes after the first probe which comes from same
2499 * user input are always allowed to add suffix, because
2500 * there might be several addresses corresponding to
2503 allow_suffix = true;
2505 if (ret == -EINVAL && pev->uprobes)
2506 warn_uprobe_event_compat(tev);
2508 /* Note that it is possible to skip all events because of blacklist */
2509 if (ret >= 0 && tev->event) {
2510 /* Show how to use the event. */
2511 pr_info("\nYou can now use it in all perf tools, such as:\n\n");
2512 pr_info("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group,
2516 kprobe_blacklist__delete(&blacklist);
2517 strlist__delete(namelist);
2523 static int find_probe_functions(struct map *map, char *name,
2524 struct symbol **syms)
2528 struct rb_node *tmp;
2530 if (map__load(map, NULL) < 0)
2533 map__for_each_symbol(map, sym, tmp) {
2534 if (strglobmatch(sym->name, name)) {
2536 if (syms && found < probe_conf.max_probes)
2537 syms[found - 1] = sym;
2544 #define strdup_or_goto(str, label) \
2545 ({ char *__p = strdup(str); if (!__p) goto label; __p; })
2547 void __weak arch__fix_tev_from_maps(struct perf_probe_event *pev __maybe_unused,
2548 struct probe_trace_event *tev __maybe_unused,
2549 struct map *map __maybe_unused) { }
2552 * Find probe function addresses from map.
2553 * Return an error or the number of found probe_trace_event
2555 static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
2556 struct probe_trace_event **tevs)
2558 struct map *map = NULL;
2559 struct ref_reloc_sym *reloc_sym = NULL;
2561 struct symbol **syms = NULL;
2562 struct probe_trace_event *tev;
2563 struct perf_probe_point *pp = &pev->point;
2564 struct probe_trace_point *tp;
2565 int num_matched_functions;
2568 map = get_target_map(pev->target, pev->uprobes);
2574 syms = malloc(sizeof(struct symbol *) * probe_conf.max_probes);
2581 * Load matched symbols: Since the different local symbols may have
2582 * same name but different addresses, this lists all the symbols.
2584 num_matched_functions = find_probe_functions(map, pp->function, syms);
2585 if (num_matched_functions == 0) {
2586 pr_err("Failed to find symbol %s in %s\n", pp->function,
2587 pev->target ? : "kernel");
2590 } else if (num_matched_functions > probe_conf.max_probes) {
2591 pr_err("Too many functions matched in %s\n",
2592 pev->target ? : "kernel");
2597 if (!pev->uprobes && !pp->retprobe) {
2598 reloc_sym = kernel_get_ref_reloc_sym();
2600 pr_warning("Relocated base symbol is not found!\n");
2606 /* Setup result trace-probe-events */
2607 *tevs = zalloc(sizeof(*tev) * num_matched_functions);
2615 for (j = 0; j < num_matched_functions; j++) {
2618 tev = (*tevs) + ret;
2620 if (ret == num_matched_functions) {
2621 pr_warning("Too many symbols are listed. Skip it.\n");
2626 if (pp->offset > sym->end - sym->start) {
2627 pr_warning("Offset %ld is bigger than the size of %s\n",
2628 pp->offset, sym->name);
2632 /* Add one probe point */
2633 tp->address = map->unmap_ip(map, sym->start) + pp->offset;
2635 tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
2636 tp->offset = tp->address - reloc_sym->addr;
2638 tp->symbol = strdup_or_goto(sym->name, nomem_out);
2639 tp->offset = pp->offset;
2641 tp->realname = strdup_or_goto(sym->name, nomem_out);
2643 tp->retprobe = pp->retprobe;
2645 tev->point.module = strdup_or_goto(pev->target,
2647 tev->uprobes = pev->uprobes;
2648 tev->nargs = pev->nargs;
2650 tev->args = zalloc(sizeof(struct probe_trace_arg) *
2652 if (tev->args == NULL)
2655 for (i = 0; i < tev->nargs; i++) {
2656 if (pev->args[i].name)
2658 strdup_or_goto(pev->args[i].name,
2661 tev->args[i].value = strdup_or_goto(pev->args[i].var,
2663 if (pev->args[i].type)
2665 strdup_or_goto(pev->args[i].type,
2668 arch__fix_tev_from_maps(pev, tev, map);
2672 put_target_map(map, pev->uprobes);
2679 clear_probe_trace_events(*tevs, num_matched_functions);
2684 bool __weak arch__prefers_symtab(void) { return false; }
2686 static int convert_to_probe_trace_events(struct perf_probe_event *pev,
2687 struct probe_trace_event **tevs)
2691 if (pev->uprobes && !pev->group) {
2692 /* Replace group name if not given */
2693 ret = convert_exec_to_group(pev->target, &pev->group);
2695 pr_warning("Failed to make a group name.\n");
2700 if (arch__prefers_symtab() && !perf_probe_event_need_dwarf(pev)) {
2701 ret = find_probe_trace_events_from_map(pev, tevs);
2703 return ret; /* Found in symbol table */
2706 /* Convert perf_probe_event with debuginfo */
2707 ret = try_to_find_probe_trace_events(pev, tevs);
2709 return ret; /* Found in debuginfo or got an error */
2711 return find_probe_trace_events_from_map(pev, tevs);
2714 struct __event_package {
2715 struct perf_probe_event *pev;
2716 struct probe_trace_event *tevs;
2720 int add_perf_probe_events(struct perf_probe_event *pevs, int npevs)
2723 struct __event_package *pkgs;
2726 pkgs = zalloc(sizeof(struct __event_package) * npevs);
2731 ret = init_symbol_maps(pevs->uprobes);
2737 /* Loop 1: convert all events */
2738 for (i = 0; i < npevs; i++) {
2739 pkgs[i].pev = &pevs[i];
2740 /* Convert with or without debuginfo */
2741 ret = convert_to_probe_trace_events(pkgs[i].pev,
2745 pkgs[i].ntevs = ret;
2748 /* Loop 2: add all events */
2749 for (i = 0; i < npevs; i++) {
2750 ret = __add_probe_trace_events(pkgs[i].pev, pkgs[i].tevs,
2752 probe_conf.force_add);
2757 /* Loop 3: cleanup and free trace events */
2758 for (i = 0; i < npevs; i++) {
2759 for (j = 0; j < pkgs[i].ntevs; j++)
2760 clear_probe_trace_event(&pkgs[i].tevs[j]);
2761 zfree(&pkgs[i].tevs);
2769 static int __del_trace_probe_event(int fd, struct str_node *ent)
2775 /* Convert from perf-probe event to trace-probe event */
2776 ret = e_snprintf(buf, 128, "-:%s", ent->s);
2780 p = strchr(buf + 2, ':');
2782 pr_debug("Internal error: %s should have ':' but not.\n",
2789 pr_debug("Writing event: %s\n", buf);
2790 ret = write(fd, buf, strlen(buf));
2796 pr_info("Removed event: %s\n", ent->s);
2799 pr_warning("Failed to delete event: %s\n",
2800 strerror_r(-ret, buf, sizeof(buf)));
2804 static int del_trace_probe_events(int fd, struct strfilter *filter,
2805 struct strlist *namelist)
2807 struct str_node *ent;
2814 strlist__for_each(ent, namelist) {
2815 p = strchr(ent->s, ':');
2816 if ((p && strfilter__compare(filter, p + 1)) ||
2817 strfilter__compare(filter, ent->s)) {
2818 ret = __del_trace_probe_event(fd, ent);
2827 int del_perf_probe_events(struct strfilter *filter)
2829 int ret, ret2, ufd = -1, kfd = -1;
2830 struct strlist *namelist = NULL, *unamelist = NULL;
2831 char *str = strfilter__string(filter);
2836 pr_debug("Delete filter: \'%s\'\n", str);
2838 /* Get current event names */
2839 kfd = open_kprobe_events(true);
2841 namelist = get_probe_trace_event_names(kfd, true);
2843 ufd = open_uprobe_events(true);
2845 unamelist = get_probe_trace_event_names(ufd, true);
2847 if (kfd < 0 && ufd < 0) {
2848 print_both_open_warning(kfd, ufd);
2853 ret = del_trace_probe_events(kfd, filter, namelist);
2854 if (ret < 0 && ret != -ENOENT)
2857 ret2 = del_trace_probe_events(ufd, filter, unamelist);
2858 if (ret2 < 0 && ret2 != -ENOENT) {
2862 if (ret == -ENOENT && ret2 == -ENOENT)
2863 pr_debug("\"%s\" does not hit any event.\n", str);
2864 /* Note that this is silently ignored */
2869 strlist__delete(namelist);
2874 strlist__delete(unamelist);
2882 /* TODO: don't use a global variable for filter ... */
2883 static struct strfilter *available_func_filter;
2886 * If a symbol corresponds to a function with global binding and
2887 * matches filter return 0. For all others return 1.
2889 static int filter_available_functions(struct map *map __maybe_unused,
2892 if (strfilter__compare(available_func_filter, sym->name))
2897 int show_available_funcs(const char *target, struct strfilter *_filter,
2903 ret = init_symbol_maps(user);
2907 /* Get a symbol map */
2909 map = dso__new_map(target);
2911 map = kernel_get_module_map(target);
2913 pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
2917 /* Load symbols with given filter */
2918 available_func_filter = _filter;
2919 if (map__load(map, filter_available_functions)) {
2920 pr_err("Failed to load symbols in %s\n", (target) ? : "kernel");
2923 if (!dso__sorted_by_name(map->dso, map->type))
2924 dso__sort_by_name(map->dso, map->type);
2926 /* Show all (filtered) symbols */
2928 dso__fprintf_symbols_by_name(map->dso, map->type, stdout);