1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * probe-event.c : perf-probe definition to probe_events format converter
5 * Written by Masami Hiramatsu <mhiramat@redhat.com>
9 #include <sys/utsname.h>
10 #include <sys/types.h>
25 #include "namespaces.h"
27 #include "strfilter.h"
35 #include <api/fs/fs.h>
36 #include "trace-event.h" /* For __maybe_unused */
37 #include "probe-event.h"
38 #include "probe-finder.h"
39 #include "probe-file.h"
44 #include <subcmd/pager.h>
45 #include <linux/ctype.h>
46 #include <linux/zalloc.h>
48 #ifdef HAVE_DEBUGINFOD_SUPPORT
49 #include <elfutils/debuginfod.h>
52 #define PERFPROBE_GROUP "probe"
54 bool probe_event_dry_run
; /* Dry run flag */
55 struct probe_conf probe_conf
= { .magic_num
= DEFAULT_PROBE_MAGIC_NUM
};
57 static char *synthesize_perf_probe_point(struct perf_probe_point
*pp
);
59 #define semantic_error(msg ...) pr_err("Semantic error :" msg)
61 int e_snprintf(char *str
, size_t size
, const char *format
, ...)
66 ret
= vsnprintf(str
, size
, format
, ap
);
73 static struct machine
*host_machine
;
75 /* Initialize symbol maps and path of vmlinux/modules */
76 int init_probe_symbol_maps(bool user_only
)
80 symbol_conf
.allow_aliases
= true;
81 ret
= symbol__init(NULL
);
83 pr_debug("Failed to init symbol map.\n");
87 if (host_machine
|| user_only
) /* already initialized */
90 if (symbol_conf
.vmlinux_name
)
91 pr_debug("Use vmlinux: %s\n", symbol_conf
.vmlinux_name
);
93 host_machine
= machine__new_host();
95 pr_debug("machine__new_host() failed.\n");
101 pr_warning("Failed to init vmlinux path.\n");
105 void exit_probe_symbol_maps(void)
107 machine__delete(host_machine
);
112 static struct ref_reloc_sym
*kernel_get_ref_reloc_sym(struct map
**pmap
)
115 struct map
*map
= machine__kernel_map(host_machine
);
117 if (map__load(map
) < 0)
120 kmap
= map__kmap(map
);
127 return kmap
->ref_reloc_sym
;
130 static int kernel_get_symbol_address_by_name(const char *name
, u64
*addr
,
131 bool reloc
, bool reladdr
)
133 struct ref_reloc_sym
*reloc_sym
;
137 /* ref_reloc_sym is just a label. Need a special fix*/
138 reloc_sym
= kernel_get_ref_reloc_sym(&map
);
139 if (reloc_sym
&& strcmp(name
, reloc_sym
->name
) == 0)
140 *addr
= (!map__reloc(map
) || reloc
) ? reloc_sym
->addr
:
141 reloc_sym
->unrelocated_addr
;
143 sym
= machine__find_kernel_symbol_by_name(host_machine
, name
, &map
);
146 *addr
= map__unmap_ip(map
, sym
->start
) -
147 ((reloc
) ? 0 : map__reloc(map
)) -
148 ((reladdr
) ? map__start(map
) : 0);
153 struct kernel_get_module_map_cb_args
{
158 static int kernel_get_module_map_cb(struct map
*map
, void *data
)
160 struct kernel_get_module_map_cb_args
*args
= data
;
161 struct dso
*dso
= map__dso(map
);
162 const char *short_name
= dso__short_name(dso
);
163 u16 short_name_len
= dso__short_name_len(dso
);
165 if (strncmp(short_name
+ 1, args
->module
, short_name_len
- 2) == 0 &&
166 args
->module
[short_name_len
- 2] == '\0') {
167 args
->result
= map__get(map
);
173 static struct map
*kernel_get_module_map(const char *module
)
175 struct kernel_get_module_map_cb_args args
= {
180 /* A file path -- this is an offline module */
181 if (module
&& strchr(module
, '/'))
182 return dso__new_map(module
);
185 struct map
*map
= machine__kernel_map(host_machine
);
187 return map__get(map
);
190 maps__for_each_map(machine__kernel_maps(host_machine
), kernel_get_module_map_cb
, &args
);
195 struct map
*get_target_map(const char *target
, struct nsinfo
*nsi
, bool user
)
197 /* Init maps of given executable or kernel */
202 map
= dso__new_map(target
);
203 dso
= map
? map__dso(map
) : NULL
;
205 mutex_lock(dso__lock(dso
));
206 dso__set_nsinfo(dso
, nsinfo__get(nsi
));
207 mutex_unlock(dso__lock(dso
));
211 return kernel_get_module_map(target
);
215 static int convert_exec_to_group(const char *exec
, char **result
)
217 char *ptr1
, *ptr2
, *exec_copy
;
221 exec_copy
= strdup(exec
);
225 ptr1
= basename(exec_copy
);
231 for (ptr2
= ptr1
; *ptr2
!= '\0'; ptr2
++) {
232 if (!isalnum(*ptr2
) && *ptr2
!= '_') {
238 ret
= e_snprintf(buf
, sizeof(buf
), "%s_%s", PERFPROBE_GROUP
, ptr1
);
242 *result
= strdup(buf
);
243 ret
= *result
? 0 : -ENOMEM
;
250 static void clear_perf_probe_point(struct perf_probe_point
*pp
)
253 zfree(&pp
->function
);
254 zfree(&pp
->lazy_line
);
257 static void clear_probe_trace_events(struct probe_trace_event
*tevs
, int ntevs
)
261 for (i
= 0; i
< ntevs
; i
++)
262 clear_probe_trace_event(tevs
+ i
);
265 static bool kprobe_blacklist__listed(u64 address
);
266 static bool kprobe_warn_out_range(const char *symbol
, u64 address
)
271 map
= kernel_get_module_map(NULL
);
273 ret
= address
<= map__start(map
) || map__end(map
) < address
;
275 pr_warning("%s is out of .text, skip it.\n", symbol
);
278 if (!ret
&& kprobe_blacklist__listed(address
)) {
279 pr_warning("%s is blacklisted function, skip it.\n", symbol
);
287 * @module can be module name of module file path. In case of path,
288 * inspect elf and find out what is actual module name.
289 * Caller has to free mod_name after using it.
291 static char *find_module_name(const char *module
)
299 char *mod_name
= NULL
;
302 fd
= open(module
, O_RDONLY
);
306 elf
= elf_begin(fd
, PERF_ELF_C_READ_MMAP
, NULL
);
310 if (gelf_getehdr(elf
, &ehdr
) == NULL
)
313 sec
= elf_section_by_name(elf
, &ehdr
, &shdr
,
314 ".gnu.linkonce.this_module", NULL
);
318 data
= elf_getdata(sec
, NULL
);
319 if (!data
|| !data
->d_buf
)
324 * '.gnu.linkonce.this_module' section of kernel module elf directly
325 * maps to 'struct module' from linux/module.h. This section contains
326 * actual module name which will be used by kernel after loading it.
327 * But, we cannot use 'struct module' here since linux/module.h is not
328 * exposed to user-space. Offset of 'name' has remained same from long
329 * time, so hardcoding it here.
331 if (ehdr
.e_ident
[EI_CLASS
] == ELFCLASS32
)
333 else /* expect ELFCLASS64 by default */
336 mod_name
= strdup((char *)data
->d_buf
+ name_offset
);
345 #ifdef HAVE_DWARF_SUPPORT
347 static int kernel_get_module_dso(const char *module
, struct dso
**pdso
)
351 const char *vmlinux_name
;
355 char module_name
[128];
357 snprintf(module_name
, sizeof(module_name
), "[%s]", module
);
358 map
= maps__find_by_name(machine__kernel_maps(host_machine
), module_name
);
364 pr_debug("Failed to find module %s.\n", module
);
368 map
= machine__kernel_map(host_machine
);
370 if (!dso__has_build_id(dso
))
371 dso__read_running_kernel_build_id(dso
, host_machine
);
373 vmlinux_name
= symbol_conf
.vmlinux_name
;
374 *dso__load_errno(dso
) = 0;
376 ret
= dso__load_vmlinux(dso
, map
, vmlinux_name
, false);
378 ret
= dso__load_vmlinux_path(dso
, map
);
385 * Some binaries like glibc have special symbols which are on the symbol
386 * table, but not in the debuginfo. If we can find the address of the
387 * symbol from map, we can translate the address back to the probe point.
389 static int find_alternative_probe_point(struct debuginfo
*dinfo
,
390 struct perf_probe_point
*pp
,
391 struct perf_probe_point
*result
,
392 const char *target
, struct nsinfo
*nsi
,
395 struct map
*map
= NULL
;
401 /* This can work only for function-name based one */
402 if (!pp
->function
|| pp
->file
)
405 map
= get_target_map(target
, nsi
, uprobes
);
409 /* Find the address of given function */
410 map__for_each_symbol_by_name(map
, pp
->function
, sym
, idx
) {
412 address
= sym
->start
;
413 if (sym
->type
== STT_GNU_IFUNC
)
414 pr_warning("Warning: The probe function (%s) is a GNU indirect function.\n"
415 "Consider identifying the final function used at run time and set the probe directly on that.\n",
418 address
= map__unmap_ip(map
, sym
->start
) - map__reloc(map
);
425 pr_debug("Symbol %s address found : %" PRIx64
"\n",
426 pp
->function
, address
);
428 ret
= debuginfo__find_probe_point(dinfo
, address
, result
);
430 ret
= (!ret
) ? -ENOENT
: ret
;
432 result
->offset
+= pp
->offset
;
433 result
->line
+= pp
->line
;
434 result
->retprobe
= pp
->retprobe
;
444 static int get_alternative_probe_event(struct debuginfo
*dinfo
,
445 struct perf_probe_event
*pev
,
446 struct perf_probe_point
*tmp
)
450 memcpy(tmp
, &pev
->point
, sizeof(*tmp
));
451 memset(&pev
->point
, 0, sizeof(pev
->point
));
452 ret
= find_alternative_probe_point(dinfo
, tmp
, &pev
->point
, pev
->target
,
453 pev
->nsi
, pev
->uprobes
);
455 memcpy(&pev
->point
, tmp
, sizeof(*tmp
));
460 static int get_alternative_line_range(struct debuginfo
*dinfo
,
461 struct line_range
*lr
,
462 const char *target
, bool user
)
464 struct perf_probe_point pp
= { .function
= lr
->function
,
467 struct perf_probe_point result
;
470 memset(&result
, 0, sizeof(result
));
472 if (lr
->end
!= INT_MAX
)
473 len
= lr
->end
- lr
->start
;
474 ret
= find_alternative_probe_point(dinfo
, &pp
, &result
,
477 lr
->function
= result
.function
;
478 lr
->file
= result
.file
;
479 lr
->start
= result
.line
;
480 if (lr
->end
!= INT_MAX
)
481 lr
->end
= lr
->start
+ len
;
482 clear_perf_probe_point(&pp
);
487 #ifdef HAVE_DEBUGINFOD_SUPPORT
488 static struct debuginfo
*open_from_debuginfod(struct dso
*dso
, struct nsinfo
*nsi
,
491 debuginfod_client
*c
= debuginfod_begin();
492 char sbuild_id
[SBUILD_ID_SIZE
+ 1];
493 struct debuginfo
*ret
= NULL
;
501 build_id__sprintf(dso__bid(dso
), sbuild_id
);
502 fd
= debuginfod_find_debuginfo(c
, (const unsigned char *)sbuild_id
,
509 pr_debug("Failed to find debuginfo in debuginfod.\n");
513 pr_debug("Load debuginfo from debuginfod (%s)\n", path
);
515 nsinfo__mountns_enter(nsi
, &nsc
);
516 ret
= debuginfo__new((const char *)path
);
517 nsinfo__mountns_exit(&nsc
);
522 struct debuginfo
*open_from_debuginfod(struct dso
*dso __maybe_unused
,
523 struct nsinfo
*nsi __maybe_unused
,
524 bool silent __maybe_unused
)
530 /* Open new debuginfo of given module */
531 static struct debuginfo
*open_debuginfo(const char *module
, struct nsinfo
*nsi
,
534 const char *path
= module
;
535 char reason
[STRERR_BUFSIZE
];
536 struct debuginfo
*ret
= NULL
;
537 struct dso
*dso
= NULL
;
541 if (!module
|| !strchr(module
, '/')) {
542 err
= kernel_get_module_dso(module
, &dso
);
544 if (!dso
|| *dso__load_errno(dso
) == 0) {
545 if (!str_error_r(-err
, reason
, STRERR_BUFSIZE
))
546 strcpy(reason
, "(unknown)");
548 dso__strerror_load(dso
, reason
, STRERR_BUFSIZE
);
550 ret
= open_from_debuginfod(dso
, nsi
, silent
);
555 pr_err("Module %s is not loaded, please specify its full path name.\n", module
);
557 pr_err("Failed to find the path for the kernel: %s\n", reason
);
561 path
= dso__long_name(dso
);
563 nsinfo__mountns_enter(nsi
, &nsc
);
564 ret
= debuginfo__new(path
);
565 if (!ret
&& !silent
) {
566 pr_warning("The %s file has no debug information.\n", path
);
567 if (!module
|| !strtailcmp(path
, ".ko"))
568 pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, ");
570 pr_warning("Rebuild with -g, ");
571 pr_warning("or install an appropriate debuginfo package.\n");
573 nsinfo__mountns_exit(&nsc
);
577 /* For caching the last debuginfo */
578 static struct debuginfo
*debuginfo_cache
;
579 static char *debuginfo_cache_path
;
581 static struct debuginfo
*debuginfo_cache__open(const char *module
, bool silent
)
583 const char *path
= module
;
585 /* If the module is NULL, it should be the kernel. */
589 if (debuginfo_cache_path
&& !strcmp(debuginfo_cache_path
, path
))
592 /* Copy module path */
593 free(debuginfo_cache_path
);
594 debuginfo_cache_path
= strdup(path
);
595 if (!debuginfo_cache_path
) {
596 debuginfo__delete(debuginfo_cache
);
597 debuginfo_cache
= NULL
;
601 debuginfo_cache
= open_debuginfo(module
, NULL
, silent
);
602 if (!debuginfo_cache
)
603 zfree(&debuginfo_cache_path
);
605 return debuginfo_cache
;
608 static void debuginfo_cache__exit(void)
610 debuginfo__delete(debuginfo_cache
);
611 debuginfo_cache
= NULL
;
612 zfree(&debuginfo_cache_path
);
616 static int get_text_start_address(const char *exec
, u64
*address
,
622 int fd
, ret
= -ENOENT
;
625 nsinfo__mountns_enter(nsi
, &nsc
);
626 fd
= open(exec
, O_RDONLY
);
627 nsinfo__mountns_exit(&nsc
);
631 elf
= elf_begin(fd
, PERF_ELF_C_READ_MMAP
, NULL
);
637 if (gelf_getehdr(elf
, &ehdr
) == NULL
)
640 if (!elf_section_by_name(elf
, &ehdr
, &shdr
, ".text", NULL
))
643 *address
= shdr
.sh_addr
- shdr
.sh_offset
;
654 * Convert trace point to probe point with debuginfo
656 static int find_perf_probe_point_from_dwarf(struct probe_trace_point
*tp
,
657 struct perf_probe_point
*pp
,
660 struct debuginfo
*dinfo
= NULL
;
662 u64 addr
= tp
->address
;
665 /* convert the address to dwarf address */
671 ret
= get_text_start_address(tp
->module
, &stext
, NULL
);
675 } else if (tp
->symbol
) {
676 /* If the module is given, this returns relative address */
677 ret
= kernel_get_symbol_address_by_name(tp
->symbol
, &addr
,
678 false, !!tp
->module
);
684 pr_debug("try to find information at %" PRIx64
" in %s\n", addr
,
685 tp
->module
? : "kernel");
687 dinfo
= debuginfo_cache__open(tp
->module
, verbose
<= 0);
689 ret
= debuginfo__find_probe_point(dinfo
, addr
, pp
);
694 pp
->retprobe
= tp
->retprobe
;
698 pr_debug("Failed to find corresponding probes from debuginfo.\n");
699 return ret
? : -ENOENT
;
702 /* Adjust symbol name and address */
703 static int post_process_probe_trace_point(struct probe_trace_point
*tp
,
704 struct map
*map
, u64 offs
)
707 u64 addr
= tp
->address
- offs
;
709 sym
= map__find_symbol(map
, addr
);
712 * If the address is in the inittext section, map can not
713 * find it. Ignore it if we are probing offline kernel.
715 return (symbol_conf
.ignore_vmlinux_buildid
) ? 0 : -ENOENT
;
718 if (strcmp(sym
->name
, tp
->symbol
)) {
719 /* If we have no realname, use symbol for it */
721 tp
->realname
= tp
->symbol
;
724 tp
->symbol
= strdup(sym
->name
);
728 tp
->offset
= addr
- sym
->start
;
735 * Rename DWARF symbols to ELF symbols -- gcc sometimes optimizes functions
736 * and generate new symbols with suffixes such as .constprop.N or .isra.N
737 * etc. Since those symbols are not recorded in DWARF, we have to find
738 * correct generated symbols from offline ELF binary.
739 * For online kernel or uprobes we don't need this because those are
740 * rebased on _text, or already a section relative address.
743 post_process_offline_probe_trace_events(struct probe_trace_event
*tevs
,
744 int ntevs
, const char *pathname
)
750 /* Prepare a map for offline binary */
751 map
= dso__new_map(pathname
);
752 if (!map
|| get_text_start_address(pathname
, &stext
, NULL
) < 0) {
753 pr_warning("Failed to get ELF symbols for %s\n", pathname
);
757 for (i
= 0; i
< ntevs
; i
++) {
758 ret
= post_process_probe_trace_point(&tevs
[i
].point
,
768 static int add_exec_to_probe_trace_events(struct probe_trace_event
*tevs
,
769 int ntevs
, const char *exec
,
778 ret
= get_text_start_address(exec
, &stext
, nsi
);
782 for (i
= 0; i
< ntevs
&& ret
>= 0; i
++) {
783 /* point.address is the address of point.symbol + point.offset */
784 tevs
[i
].point
.address
-= stext
;
785 tevs
[i
].point
.module
= strdup(exec
);
786 if (!tevs
[i
].point
.module
) {
790 tevs
[i
].uprobes
= true;
797 post_process_module_probe_trace_events(struct probe_trace_event
*tevs
,
798 int ntevs
, const char *module
,
799 struct debuginfo
*dinfo
)
801 Dwarf_Addr text_offs
= 0;
803 char *mod_name
= NULL
;
809 map
= get_target_map(module
, NULL
, false);
810 if (!map
|| debuginfo__get_text_offset(dinfo
, &text_offs
, true) < 0) {
811 pr_warning("Failed to get ELF symbols for %s\n", module
);
815 mod_name
= find_module_name(module
);
816 for (i
= 0; i
< ntevs
; i
++) {
817 ret
= post_process_probe_trace_point(&tevs
[i
].point
,
821 tevs
[i
].point
.module
=
822 strdup(mod_name
? mod_name
: module
);
823 if (!tevs
[i
].point
.module
) {
836 post_process_kernel_probe_trace_events(struct probe_trace_event
*tevs
,
839 struct ref_reloc_sym
*reloc_sym
;
844 /* Skip post process if the target is an offline kernel */
845 if (symbol_conf
.ignore_vmlinux_buildid
)
846 return post_process_offline_probe_trace_events(tevs
, ntevs
,
847 symbol_conf
.vmlinux_name
);
849 reloc_sym
= kernel_get_ref_reloc_sym(&map
);
851 pr_warning("Relocated base symbol is not found! "
852 "Check /proc/sys/kernel/kptr_restrict\n"
853 "and /proc/sys/kernel/perf_event_paranoid. "
854 "Or run as privileged perf user.\n\n");
858 for (i
= 0; i
< ntevs
; i
++) {
859 if (!tevs
[i
].point
.address
)
861 if (tevs
[i
].point
.retprobe
&& !kretprobe_offset_is_supported())
864 * If we found a wrong one, mark it by NULL symbol.
865 * Since addresses in debuginfo is same as objdump, we need
866 * to convert it to addresses on memory.
868 if (kprobe_warn_out_range(tevs
[i
].point
.symbol
,
869 map__objdump_2mem(map
, tevs
[i
].point
.address
))) {
873 tmp
= strdup(reloc_sym
->name
);
877 /* If we have no realname, use symbol for it */
878 if (!tevs
[i
].point
.realname
)
879 tevs
[i
].point
.realname
= tevs
[i
].point
.symbol
;
881 free(tevs
[i
].point
.symbol
);
882 tevs
[i
].point
.symbol
= tmp
;
883 tevs
[i
].point
.offset
= tevs
[i
].point
.address
-
884 (map__reloc(map
) ? reloc_sym
->unrelocated_addr
:
891 arch__post_process_probe_trace_events(struct perf_probe_event
*pev __maybe_unused
,
892 int ntevs __maybe_unused
)
896 /* Post processing the probe events */
897 static int post_process_probe_trace_events(struct perf_probe_event
*pev
,
898 struct probe_trace_event
*tevs
,
899 int ntevs
, const char *module
,
900 bool uprobe
, struct debuginfo
*dinfo
)
905 ret
= add_exec_to_probe_trace_events(tevs
, ntevs
, module
,
908 /* Currently ref_reloc_sym based probe is not for drivers */
909 ret
= post_process_module_probe_trace_events(tevs
, ntevs
,
912 ret
= post_process_kernel_probe_trace_events(tevs
, ntevs
);
915 arch__post_process_probe_trace_events(pev
, ntevs
);
920 /* Try to find perf_probe_event with debuginfo */
921 static int try_to_find_probe_trace_events(struct perf_probe_event
*pev
,
922 struct probe_trace_event
**tevs
)
924 bool need_dwarf
= perf_probe_event_need_dwarf(pev
);
925 struct perf_probe_point tmp
;
926 struct debuginfo
*dinfo
;
929 /* Workaround for gcc #98776 issue.
930 * Perf failed to add kretprobe event with debuginfo of vmlinux which is
931 * compiled by gcc with -fpatchable-function-entry option enabled. The
932 * same issue with kernel module. The retprobe doesn`t need debuginfo.
933 * This workaround solution use map to query the probe function address
934 * for retprobe event.
936 if (pev
->point
.retprobe
)
939 dinfo
= open_debuginfo(pev
->target
, pev
->nsi
, !need_dwarf
);
943 pr_debug("Could not open debuginfo. Try to use symbols.\n");
947 pr_debug("Try to find probe point from debuginfo.\n");
948 /* Searching trace events corresponding to a probe event */
949 ntevs
= debuginfo__find_trace_events(dinfo
, pev
, tevs
);
951 if (ntevs
== 0) { /* Not found, retry with an alternative */
952 ret
= get_alternative_probe_event(dinfo
, pev
, &tmp
);
954 ntevs
= debuginfo__find_trace_events(dinfo
, pev
, tevs
);
956 * Write back to the original probe_event for
957 * setting appropriate (user given) event name
959 clear_perf_probe_point(&pev
->point
);
960 memcpy(&pev
->point
, &tmp
, sizeof(tmp
));
964 if (ntevs
> 0) { /* Succeeded to find trace events */
965 pr_debug("Found %d probe_trace_events.\n", ntevs
);
966 ret
= post_process_probe_trace_events(pev
, *tevs
, ntevs
,
967 pev
->target
, pev
->uprobes
, dinfo
);
968 if (ret
< 0 || ret
== ntevs
) {
969 pr_debug("Post processing failed or all events are skipped. (%d)\n", ret
);
970 clear_probe_trace_events(*tevs
, ntevs
);
976 debuginfo__delete(dinfo
);
978 if (ntevs
== 0) { /* No error but failed to find probe point. */
979 char *probe_point
= synthesize_perf_probe_point(&pev
->point
);
980 pr_warning("Probe point '%s' not found.\n", probe_point
);
983 } else if (ntevs
< 0) {
984 /* Error path : ntevs < 0 */
985 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs
);
987 pr_warning("Warning: No dwarf info found in the vmlinux - "
988 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
990 pr_debug("Trying to use symbols.\n");
997 #define LINEBUF_SIZE 256
998 #define NR_ADDITIONAL_LINES 2
1000 static int __show_one_line(FILE *fp
, int l
, bool skip
, bool show_num
)
1002 char buf
[LINEBUF_SIZE
], sbuf
[STRERR_BUFSIZE
];
1003 const char *color
= show_num
? "" : PERF_COLOR_BLUE
;
1004 const char *prefix
= NULL
;
1007 if (fgets(buf
, LINEBUF_SIZE
, fp
) == NULL
)
1012 prefix
= show_num
? "%7d " : " ";
1013 color_fprintf(stdout
, color
, prefix
, l
);
1015 color_fprintf(stdout
, color
, "%s", buf
);
1017 } while (strchr(buf
, '\n') == NULL
);
1022 pr_warning("File read error: %s\n",
1023 str_error_r(errno
, sbuf
, sizeof(sbuf
)));
1029 static int _show_one_line(FILE *fp
, int l
, bool skip
, bool show_num
)
1031 int rv
= __show_one_line(fp
, l
, skip
, show_num
);
1033 pr_warning("Source file is shorter than expected.\n");
1039 #define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
1040 #define show_one_line(f,l) _show_one_line(f,l,false,false)
1041 #define skip_one_line(f,l) _show_one_line(f,l,true,false)
1042 #define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
1045 * Show line-range always requires debuginfo to find source file and
1048 static int __show_line_range(struct line_range
*lr
, const char *module
,
1051 struct build_id bid
;
1053 struct int_node
*ln
;
1054 struct debuginfo
*dinfo
;
1058 char sbuf
[STRERR_BUFSIZE
];
1059 char sbuild_id
[SBUILD_ID_SIZE
] = "";
1061 /* Search a line range */
1062 dinfo
= open_debuginfo(module
, NULL
, false);
1066 ret
= debuginfo__find_line_range(dinfo
, lr
);
1067 if (!ret
) { /* Not found, retry with an alternative */
1068 ret
= get_alternative_line_range(dinfo
, lr
, module
, user
);
1070 ret
= debuginfo__find_line_range(dinfo
, lr
);
1072 if (dinfo
->build_id
) {
1073 build_id__init(&bid
, dinfo
->build_id
, BUILD_ID_SIZE
);
1074 build_id__sprintf(&bid
, sbuild_id
);
1076 debuginfo__delete(dinfo
);
1077 if (ret
== 0 || ret
== -ENOENT
) {
1078 pr_warning("Specified source line is not found.\n");
1080 } else if (ret
< 0) {
1081 pr_warning("Debuginfo analysis failed.\n");
1085 /* Convert source file path */
1087 ret
= find_source_path(tmp
, sbuild_id
, lr
->comp_dir
, &lr
->path
);
1089 /* Free old path when new path is assigned */
1090 if (tmp
!= lr
->path
)
1094 pr_warning("Failed to find source file path.\n");
1101 fprintf(stdout
, "<%s@%s:%d>\n", lr
->function
, lr
->path
,
1102 lr
->start
- lr
->offset
);
1104 fprintf(stdout
, "<%s:%d>\n", lr
->path
, lr
->start
);
1106 fp
= fopen(lr
->path
, "r");
1108 pr_warning("Failed to open %s: %s\n", lr
->path
,
1109 str_error_r(errno
, sbuf
, sizeof(sbuf
)));
1112 /* Skip to starting line number */
1113 while (l
< lr
->start
) {
1114 ret
= skip_one_line(fp
, l
++);
1119 intlist__for_each_entry(ln
, lr
->line_list
) {
1120 for (; ln
->i
> (unsigned long)l
; l
++) {
1121 ret
= show_one_line(fp
, l
- lr
->offset
);
1125 ret
= show_one_line_with_num(fp
, l
++ - lr
->offset
);
1130 if (lr
->end
== INT_MAX
)
1131 lr
->end
= l
+ NR_ADDITIONAL_LINES
;
1132 while (l
<= lr
->end
) {
1133 ret
= show_one_line_or_eof(fp
, l
++ - lr
->offset
);
1142 int show_line_range(struct line_range
*lr
, const char *module
,
1143 struct nsinfo
*nsi
, bool user
)
1146 struct nscookie nsc
;
1148 ret
= init_probe_symbol_maps(user
);
1151 nsinfo__mountns_enter(nsi
, &nsc
);
1152 ret
= __show_line_range(lr
, module
, user
);
1153 nsinfo__mountns_exit(&nsc
);
1154 exit_probe_symbol_maps();
1159 static int show_available_vars_at(struct debuginfo
*dinfo
,
1160 struct perf_probe_event
*pev
,
1161 struct strfilter
*_filter
)
1165 struct str_node
*node
;
1166 struct variable_list
*vls
= NULL
, *vl
;
1167 struct perf_probe_point tmp
;
1170 buf
= synthesize_perf_probe_point(&pev
->point
);
1173 pr_debug("Searching variables at %s\n", buf
);
1175 ret
= debuginfo__find_available_vars_at(dinfo
, pev
, &vls
);
1176 if (!ret
) { /* Not found, retry with an alternative */
1177 ret
= get_alternative_probe_event(dinfo
, pev
, &tmp
);
1179 ret
= debuginfo__find_available_vars_at(dinfo
, pev
,
1181 /* Release the old probe_point */
1182 clear_perf_probe_point(&tmp
);
1186 if (ret
== 0 || ret
== -ENOENT
) {
1187 pr_err("Failed to find the address of %s\n", buf
);
1190 pr_warning("Debuginfo analysis failed.\n");
1194 /* Some variables are found */
1195 fprintf(stdout
, "Available variables at %s\n", buf
);
1196 for (i
= 0; i
< ret
; i
++) {
1199 * A probe point might be converted to
1200 * several trace points.
1202 fprintf(stdout
, "\t@<%s+%lu>\n", vl
->point
.symbol
,
1204 zfree(&vl
->point
.symbol
);
1207 strlist__for_each_entry(node
, vl
->vars
) {
1208 var
= strchr(node
->s
, '\t') + 1;
1209 if (strfilter__compare(_filter
, var
)) {
1210 fprintf(stdout
, "\t\t%s\n", node
->s
);
1214 strlist__delete(vl
->vars
);
1217 fprintf(stdout
, "\t\t(No matched variables)\n");
1225 /* Show available variables on given probe point */
1226 int show_available_vars(struct perf_probe_event
*pevs
, int npevs
,
1227 struct strfilter
*_filter
)
1230 struct debuginfo
*dinfo
;
1232 ret
= init_probe_symbol_maps(pevs
->uprobes
);
1236 dinfo
= open_debuginfo(pevs
->target
, pevs
->nsi
, false);
1244 for (i
= 0; i
< npevs
&& ret
>= 0; i
++)
1245 ret
= show_available_vars_at(dinfo
, &pevs
[i
], _filter
);
1247 debuginfo__delete(dinfo
);
1249 exit_probe_symbol_maps();
1253 #else /* !HAVE_DWARF_SUPPORT */
1255 static void debuginfo_cache__exit(void)
1260 find_perf_probe_point_from_dwarf(struct probe_trace_point
*tp __maybe_unused
,
1261 struct perf_probe_point
*pp __maybe_unused
,
1262 bool is_kprobe __maybe_unused
)
1267 static int try_to_find_probe_trace_events(struct perf_probe_event
*pev
,
1268 struct probe_trace_event
**tevs __maybe_unused
)
1270 if (perf_probe_event_need_dwarf(pev
)) {
1271 pr_warning("Debuginfo-analysis is not supported.\n");
1278 int show_line_range(struct line_range
*lr __maybe_unused
,
1279 const char *module __maybe_unused
,
1280 struct nsinfo
*nsi __maybe_unused
,
1281 bool user __maybe_unused
)
1283 pr_warning("Debuginfo-analysis is not supported.\n");
1287 int show_available_vars(struct perf_probe_event
*pevs __maybe_unused
,
1288 int npevs __maybe_unused
,
1289 struct strfilter
*filter __maybe_unused
)
1291 pr_warning("Debuginfo-analysis is not supported.\n");
1296 void line_range__clear(struct line_range
*lr
)
1298 zfree(&lr
->function
);
1301 zfree(&lr
->comp_dir
);
1302 intlist__delete(lr
->line_list
);
1305 int line_range__init(struct line_range
*lr
)
1307 memset(lr
, 0, sizeof(*lr
));
1308 lr
->line_list
= intlist__new(NULL
);
1315 static int parse_line_num(char **ptr
, int *val
, const char *what
)
1317 const char *start
= *ptr
;
1320 *val
= strtol(*ptr
, ptr
, 0);
1321 if (errno
|| *ptr
== start
) {
1322 semantic_error("'%s' is not a valid number.\n", what
);
1328 /* Check the name is good for event, group or function */
1329 static bool is_c_func_name(const char *name
)
1331 if (!isalpha(*name
) && *name
!= '_')
1333 while (*++name
!= '\0') {
1334 if (!isalpha(*name
) && !isdigit(*name
) && *name
!= '_')
1341 * Stuff 'lr' according to the line range described by 'arg'.
1342 * The line range syntax is described by:
1344 * SRC[:SLN[+NUM|-ELN]]
1345 * FNC[@SRC][:SLN[+NUM|-ELN]]
1347 int parse_line_range_desc(const char *arg
, struct line_range
*lr
)
1349 char *range
, *file
, *name
= strdup(arg
);
1358 range
= strchr(name
, ':');
1362 err
= parse_line_num(&range
, &lr
->start
, "start line");
1366 if (*range
== '+' || *range
== '-') {
1367 const char c
= *range
++;
1369 err
= parse_line_num(&range
, &lr
->end
, "end line");
1374 lr
->end
+= lr
->start
;
1376 * Adjust the number of lines here.
1377 * If the number of lines == 1, the
1378 * end of line should be equal to
1379 * the start of line.
1385 pr_debug("Line range is %d to %d\n", lr
->start
, lr
->end
);
1388 if (lr
->start
> lr
->end
) {
1389 semantic_error("Start line must be smaller"
1390 " than end line.\n");
1393 if (*range
!= '\0') {
1394 semantic_error("Tailing with invalid str '%s'.\n", range
);
1399 file
= strchr(name
, '@');
1402 lr
->file
= strdup(++file
);
1403 if (lr
->file
== NULL
) {
1407 lr
->function
= name
;
1408 } else if (strchr(name
, '/') || strchr(name
, '.'))
1410 else if (is_c_func_name(name
))/* We reuse it for checking funcname */
1411 lr
->function
= name
;
1412 else { /* Invalid name */
1413 semantic_error("'%s' is not a valid function name.\n", name
);
1424 static int parse_perf_probe_event_name(char **arg
, struct perf_probe_event
*pev
)
1428 ptr
= strpbrk_esc(*arg
, ":");
1431 if (!pev
->sdt
&& !is_c_func_name(*arg
))
1433 pev
->group
= strdup_esc(*arg
);
1440 pev
->event
= strdup_esc(*arg
);
1441 if (pev
->event
== NULL
)
1444 if (!pev
->sdt
&& !is_c_func_name(pev
->event
)) {
1448 semantic_error("%s is bad for event name -it must "
1449 "follow C symbol-naming rule.\n", *arg
);
1455 /* Parse probepoint definition. */
1456 static int parse_perf_probe_point(char *arg
, struct perf_probe_event
*pev
)
1458 struct perf_probe_point
*pp
= &pev
->point
;
1461 bool file_spec
= false;
1466 * perf probe [GRP:][EVENT=]SRC[:LN|;PTN]
1467 * perf probe [GRP:][EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
1468 * perf probe %[GRP:]SDT_EVENT
1473 if (is_sdt_event(arg
)) {
1479 ptr
= strpbrk_esc(arg
, ";=@+%");
1483 semantic_error("%s must be an SDT name.\n",
1487 /* This must be a target file name or build id */
1488 tmp
= build_id_cache__complement(ptr
+ 1);
1490 pev
->target
= build_id_cache__origname(tmp
);
1493 pev
->target
= strdup_esc(ptr
+ 1);
1498 ret
= parse_perf_probe_event_name(&arg
, pev
);
1500 if (asprintf(&pev
->point
.function
, "%%%s", pev
->event
) < 0)
1506 if (ptr
&& *ptr
== '=') { /* Event name */
1509 ret
= parse_perf_probe_event_name(&arg
, pev
);
1517 * Check arg is function or file name and copy it.
1519 * We consider arg to be a file spec if and only if it satisfies
1520 * all of the below criteria::
1521 * - it does not include any of "+@%",
1522 * - it includes one of ":;", and
1523 * - it has a period '.' in the name.
1525 * Otherwise, we consider arg to be a function specification.
1527 if (!strpbrk_esc(arg
, "+@%")) {
1528 ptr
= strpbrk_esc(arg
, ";:");
1529 /* This is a file spec if it includes a '.' before ; or : */
1530 if (ptr
&& memchr(arg
, '.', ptr
- arg
))
1534 ptr
= strpbrk_esc(arg
, ";:+@%");
1543 tmp
= strdup_esc(arg
);
1554 * Keep pp->function even if this is absolute address,
1555 * so it can mark whether abs_address is valid.
1556 * Which make 'perf probe lib.bin 0x0' possible.
1558 * Note that checking length of tmp is not needed
1559 * because when we access tmp[1] we know tmp[0] is '0',
1560 * so tmp[1] should always valid (but could be '\0').
1562 if (tmp
&& !strncmp(tmp
, "0x", 2)) {
1563 pp
->abs_address
= strtoull(pp
->function
, &tmp
, 0);
1565 semantic_error("Invalid absolute address.\n");
1571 /* Parse other options */
1575 if (c
== ';') { /* Lazy pattern must be the last part */
1576 pp
->lazy_line
= strdup(arg
); /* let leave escapes */
1577 if (pp
->lazy_line
== NULL
)
1581 ptr
= strpbrk_esc(arg
, ";:+@%");
1587 case ':': /* Line number */
1588 pp
->line
= strtoul(arg
, &tmp
, 0);
1590 semantic_error("There is non-digit char"
1591 " in line number.\n");
1595 case '+': /* Byte offset from a symbol */
1596 pp
->offset
= strtoul(arg
, &tmp
, 0);
1598 semantic_error("There is non-digit character"
1603 case '@': /* File name */
1605 semantic_error("SRC@SRC is not allowed.\n");
1608 pp
->file
= strdup_esc(arg
);
1609 if (pp
->file
== NULL
)
1612 case '%': /* Probe places */
1613 if (strcmp(arg
, "return") == 0) {
1615 } else { /* Others not supported yet */
1616 semantic_error("%%%s is not supported.\n", arg
);
1620 default: /* Buggy case */
1621 pr_err("This program has a bug at %s:%d.\n",
1622 __FILE__
, __LINE__
);
1628 /* Exclusion check */
1629 if (pp
->lazy_line
&& pp
->line
) {
1630 semantic_error("Lazy pattern can't be used with"
1635 if (pp
->lazy_line
&& pp
->offset
) {
1636 semantic_error("Lazy pattern can't be used with offset.\n");
1640 if (pp
->line
&& pp
->offset
) {
1641 semantic_error("Offset can't be used with line number.\n");
1645 if (!pp
->line
&& !pp
->lazy_line
&& pp
->file
&& !pp
->function
) {
1646 semantic_error("File always requires line number or "
1651 if (pp
->offset
&& !pp
->function
) {
1652 semantic_error("Offset requires an entry function.\n");
1656 if ((pp
->offset
|| pp
->line
|| pp
->lazy_line
) && pp
->retprobe
) {
1657 semantic_error("Offset/Line/Lazy pattern can't be used with "
1662 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
1663 pp
->function
, pp
->file
, pp
->line
, pp
->offset
, pp
->retprobe
,
1668 /* Parse perf-probe event argument */
1669 static int parse_perf_probe_arg(char *str
, struct perf_probe_arg
*arg
)
1671 char *tmp
, *goodname
;
1672 struct perf_probe_arg_field
**fieldp
;
1674 pr_debug("parsing arg: %s into ", str
);
1676 tmp
= strchr(str
, '=');
1678 arg
->name
= strndup(str
, tmp
- str
);
1679 if (arg
->name
== NULL
)
1681 pr_debug("name:%s ", arg
->name
);
1685 tmp
= strchr(str
, '@');
1686 if (tmp
&& tmp
!= str
&& !strcmp(tmp
+ 1, "user")) { /* user attr */
1687 if (!user_access_is_supported()) {
1688 semantic_error("ftrace does not support user access\n");
1692 arg
->user_access
= true;
1693 pr_debug("user_access ");
1696 tmp
= strchr(str
, ':');
1697 if (tmp
) { /* Type setting */
1699 arg
->type
= strdup(tmp
+ 1);
1700 if (arg
->type
== NULL
)
1702 pr_debug("type:%s ", arg
->type
);
1705 tmp
= strpbrk(str
, "-.[");
1706 if (!is_c_varname(str
) || !tmp
) {
1707 /* A variable, register, symbol or special value */
1708 arg
->var
= strdup(str
);
1709 if (arg
->var
== NULL
)
1711 pr_debug("%s\n", arg
->var
);
1715 /* Structure fields or array element */
1716 arg
->var
= strndup(str
, tmp
- str
);
1717 if (arg
->var
== NULL
)
1719 goodname
= arg
->var
;
1720 pr_debug("%s, ", arg
->var
);
1721 fieldp
= &arg
->field
;
1724 *fieldp
= zalloc(sizeof(struct perf_probe_arg_field
));
1725 if (*fieldp
== NULL
)
1727 if (*tmp
== '[') { /* Array */
1729 (*fieldp
)->index
= strtol(str
+ 1, &tmp
, 0);
1730 (*fieldp
)->ref
= true;
1731 if (*tmp
!= ']' || tmp
== str
+ 1) {
1732 semantic_error("Array index must be a"
1739 } else { /* Structure */
1742 (*fieldp
)->ref
= false;
1743 } else if (tmp
[1] == '>') {
1745 (*fieldp
)->ref
= true;
1747 semantic_error("Argument parse error: %s\n",
1751 tmp
= strpbrk(str
, "-.[");
1754 (*fieldp
)->name
= strndup(str
, tmp
- str
);
1755 if ((*fieldp
)->name
== NULL
)
1758 goodname
= (*fieldp
)->name
;
1759 pr_debug("%s(%d), ", (*fieldp
)->name
, (*fieldp
)->ref
);
1760 fieldp
= &(*fieldp
)->next
;
1763 (*fieldp
)->name
= strdup(str
);
1764 if ((*fieldp
)->name
== NULL
)
1767 goodname
= (*fieldp
)->name
;
1768 pr_debug("%s(%d)\n", (*fieldp
)->name
, (*fieldp
)->ref
);
1770 /* If no name is specified, set the last field name (not array index)*/
1772 arg
->name
= strdup(goodname
);
1773 if (arg
->name
== NULL
)
1779 /* Parse perf-probe event command */
1780 int parse_perf_probe_command(const char *cmd
, struct perf_probe_event
*pev
)
1783 int argc
, i
, ret
= 0;
1785 argv
= argv_split(cmd
, &argc
);
1787 pr_debug("Failed to split arguments.\n");
1790 if (argc
- 1 > MAX_PROBE_ARGS
) {
1791 semantic_error("Too many probe arguments (%d).\n", argc
- 1);
1795 /* Parse probe point */
1796 ret
= parse_perf_probe_point(argv
[0], pev
);
1800 /* Generate event name if needed */
1801 if (!pev
->event
&& pev
->point
.function
&& pev
->point
.line
1802 && !pev
->point
.lazy_line
&& !pev
->point
.offset
) {
1803 if (asprintf(&pev
->event
, "%s_L%d", pev
->point
.function
,
1804 pev
->point
.line
) < 0) {
1810 /* Copy arguments and ensure return probe has no C argument */
1811 pev
->nargs
= argc
- 1;
1812 pev
->args
= zalloc(sizeof(struct perf_probe_arg
) * pev
->nargs
);
1813 if (pev
->args
== NULL
) {
1817 for (i
= 0; i
< pev
->nargs
&& ret
>= 0; i
++) {
1818 ret
= parse_perf_probe_arg(argv
[i
+ 1], &pev
->args
[i
]);
1820 is_c_varname(pev
->args
[i
].var
) && pev
->point
.retprobe
) {
1821 semantic_error("You can't specify local variable for"
1832 /* Returns true if *any* ARG is either C variable, $params or $vars. */
1833 bool perf_probe_with_var(struct perf_probe_event
*pev
)
1837 for (i
= 0; i
< pev
->nargs
; i
++)
1838 if (is_c_varname(pev
->args
[i
].var
) ||
1839 !strcmp(pev
->args
[i
].var
, PROBE_ARG_PARAMS
) ||
1840 !strcmp(pev
->args
[i
].var
, PROBE_ARG_VARS
))
1845 /* Return true if this perf_probe_event requires debuginfo */
1846 bool perf_probe_event_need_dwarf(struct perf_probe_event
*pev
)
1848 if (pev
->point
.file
|| pev
->point
.line
|| pev
->point
.lazy_line
)
1851 if (perf_probe_with_var(pev
))
1857 /* Parse probe_events event into struct probe_point */
1858 int parse_probe_trace_command(const char *cmd
, struct probe_trace_event
*tev
)
1860 struct probe_trace_point
*tp
= &tev
->point
;
1863 char *argv0_str
= NULL
, *fmt
, *fmt1_str
, *fmt2_str
, *fmt3_str
;
1867 pr_debug("Parsing probe_events: %s\n", cmd
);
1868 argv
= argv_split(cmd
, &argc
);
1870 pr_debug("Failed to split arguments.\n");
1874 semantic_error("Too few probe arguments.\n");
1879 /* Scan event and group name. */
1880 argv0_str
= strdup(argv
[0]);
1881 if (argv0_str
== NULL
) {
1885 fmt1_str
= strtok_r(argv0_str
, ":", &fmt
);
1886 fmt2_str
= strtok_r(NULL
, "/", &fmt
);
1887 fmt3_str
= strtok_r(NULL
, " \t", &fmt
);
1888 if (fmt1_str
== NULL
|| fmt2_str
== NULL
|| fmt3_str
== NULL
) {
1889 semantic_error("Failed to parse event name: %s\n", argv
[0]);
1894 tev
->group
= strdup(fmt2_str
);
1895 tev
->event
= strdup(fmt3_str
);
1896 if (tev
->group
== NULL
|| tev
->event
== NULL
) {
1900 pr_debug("Group:%s Event:%s probe:%c\n", tev
->group
, tev
->event
, pr
);
1902 tp
->retprobe
= (pr
== 'r');
1904 /* Scan module name(if there), function name and offset */
1905 p
= strchr(argv
[1], ':');
1907 tp
->module
= strndup(argv
[1], p
- argv
[1]);
1912 tev
->uprobes
= (tp
->module
[0] == '/');
1916 fmt1_str
= strtok_r(p
, "+", &fmt
);
1917 /* only the address started with 0x */
1918 if (fmt1_str
[0] == '0') {
1920 * Fix a special case:
1921 * if address == 0, kernel reports something like:
1922 * p:probe_libc/abs_0 /lib/libc-2.18.so:0x (null) arg1=%ax
1923 * Newer kernel may fix that, but we want to
1924 * support old kernel also.
1926 if (strcmp(fmt1_str
, "0x") == 0) {
1927 if (!argv
[2] || strcmp(argv
[2], "(null)")) {
1934 for (i
= 2; argv
[i
+ 1] != NULL
; i
++)
1935 argv
[i
] = argv
[i
+ 1];
1940 tp
->address
= strtoull(fmt1_str
, NULL
, 0);
1942 /* Only the symbol-based probe has offset */
1943 tp
->symbol
= strdup(fmt1_str
);
1944 if (tp
->symbol
== NULL
) {
1948 fmt2_str
= strtok_r(NULL
, "", &fmt
);
1949 if (fmt2_str
== NULL
)
1952 tp
->offset
= strtoul(fmt2_str
, NULL
, 10);
1956 fmt2_str
= strchr(p
, '(');
1958 tp
->ref_ctr_offset
= strtoul(fmt2_str
+ 1, NULL
, 0);
1961 tev
->nargs
= argc
- 2;
1962 tev
->args
= zalloc(sizeof(struct probe_trace_arg
) * tev
->nargs
);
1963 if (tev
->args
== NULL
) {
1967 for (i
= 0; i
< tev
->nargs
; i
++) {
1968 p
= strchr(argv
[i
+ 2], '=');
1969 if (p
) /* We don't need which register is assigned. */
1973 tev
->args
[i
].name
= strdup(argv
[i
+ 2]);
1974 /* TODO: parse regs and offset */
1975 tev
->args
[i
].value
= strdup(p
);
1976 if (tev
->args
[i
].name
== NULL
|| tev
->args
[i
].value
== NULL
) {
1988 /* Compose only probe arg */
1989 char *synthesize_perf_probe_arg(struct perf_probe_arg
*pa
)
1991 struct perf_probe_arg_field
*field
= pa
->field
;
1996 if (strbuf_init(&buf
, 64) < 0)
1999 if (pa
->name
&& pa
->var
)
2000 err
= strbuf_addf(&buf
, "%s=%s", pa
->name
, pa
->var
);
2002 err
= strbuf_addstr(&buf
, pa
->name
?: pa
->var
);
2007 if (field
->name
[0] == '[')
2008 err
= strbuf_addstr(&buf
, field
->name
);
2010 err
= strbuf_addf(&buf
, "%s%s", field
->ref
? "->" : ".",
2012 field
= field
->next
;
2018 if (strbuf_addf(&buf
, ":%s", pa
->type
) < 0)
2021 ret
= strbuf_detach(&buf
, NULL
);
2023 strbuf_release(&buf
);
2027 /* Compose only probe point (not argument) */
2028 static char *synthesize_perf_probe_point(struct perf_probe_point
*pp
)
2031 char *tmp
, *ret
= NULL
;
2034 if (strbuf_init(&buf
, 64) < 0)
2038 if (strbuf_addstr(&buf
, pp
->function
) < 0)
2041 err
= strbuf_addf(&buf
, "+%lu", pp
->offset
);
2043 err
= strbuf_addf(&buf
, ":%d", pp
->line
);
2044 else if (pp
->retprobe
)
2045 err
= strbuf_addstr(&buf
, "%return");
2053 tmp
= strchr(pp
->file
+ len
- 30, '/');
2054 tmp
= tmp
? tmp
+ 1 : pp
->file
+ len
- 30;
2056 err
= strbuf_addf(&buf
, "@%s", tmp
);
2057 if (!err
&& !pp
->function
&& pp
->line
)
2058 err
= strbuf_addf(&buf
, ":%d", pp
->line
);
2061 ret
= strbuf_detach(&buf
, NULL
);
2063 strbuf_release(&buf
);
2067 char *synthesize_perf_probe_command(struct perf_probe_event
*pev
)
2070 char *tmp
, *ret
= NULL
;
2073 if (strbuf_init(&buf
, 64))
2076 if (strbuf_addf(&buf
, "%s:%s=", pev
->group
?: PERFPROBE_GROUP
,
2080 tmp
= synthesize_perf_probe_point(&pev
->point
);
2081 if (!tmp
|| strbuf_addstr(&buf
, tmp
) < 0) {
2087 for (i
= 0; i
< pev
->nargs
; i
++) {
2088 tmp
= synthesize_perf_probe_arg(pev
->args
+ i
);
2089 if (!tmp
|| strbuf_addf(&buf
, " %s", tmp
) < 0) {
2096 ret
= strbuf_detach(&buf
, NULL
);
2098 strbuf_release(&buf
);
2102 static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref
*ref
,
2103 struct strbuf
*buf
, int depth
)
2107 depth
= __synthesize_probe_trace_arg_ref(ref
->next
, buf
,
2112 if (ref
->user_access
)
2113 err
= strbuf_addf(buf
, "%s%ld(", "+u", ref
->offset
);
2115 err
= strbuf_addf(buf
, "%+ld(", ref
->offset
);
2116 return (err
< 0) ? err
: depth
;
2119 static int synthesize_probe_trace_arg(struct probe_trace_arg
*arg
,
2122 struct probe_trace_arg_ref
*ref
= arg
->ref
;
2125 /* Argument name or separator */
2127 err
= strbuf_addf(buf
, " %s=", arg
->name
);
2129 err
= strbuf_addch(buf
, ' ');
2133 /* Special case: @XXX */
2134 if (arg
->value
[0] == '@' && arg
->ref
)
2137 /* Dereferencing arguments */
2139 depth
= __synthesize_probe_trace_arg_ref(ref
, buf
, 1);
2144 /* Print argument value */
2145 if (arg
->value
[0] == '@' && arg
->ref
)
2146 err
= strbuf_addf(buf
, "%s%+ld", arg
->value
, arg
->ref
->offset
);
2148 err
= strbuf_addstr(buf
, arg
->value
);
2151 while (!err
&& depth
--)
2152 err
= strbuf_addch(buf
, ')');
2154 /* Print argument type */
2155 if (!err
&& arg
->type
)
2156 err
= strbuf_addf(buf
, ":%s", arg
->type
);
2162 synthesize_probe_trace_args(struct probe_trace_event
*tev
, struct strbuf
*buf
)
2166 for (i
= 0; i
< tev
->nargs
&& ret
>= 0; i
++)
2167 ret
= synthesize_probe_trace_arg(&tev
->args
[i
], buf
);
2173 synthesize_uprobe_trace_def(struct probe_trace_point
*tp
, struct strbuf
*buf
)
2177 /* Uprobes must have tp->module */
2181 * If tp->address == 0, then this point must be a
2182 * absolute address uprobe.
2183 * try_to_find_absolute_address() should have made
2184 * tp->symbol to "0x0".
2186 if (!tp
->address
&& (!tp
->symbol
|| strcmp(tp
->symbol
, "0x0")))
2189 /* Use the tp->address for uprobes */
2190 err
= strbuf_addf(buf
, "%s:0x%" PRIx64
, tp
->module
, tp
->address
);
2192 if (err
>= 0 && tp
->ref_ctr_offset
) {
2193 if (!uprobe_ref_ctr_is_supported())
2195 err
= strbuf_addf(buf
, "(0x%lx)", tp
->ref_ctr_offset
);
2197 return err
>= 0 ? 0 : err
;
2201 synthesize_kprobe_trace_def(struct probe_trace_point
*tp
, struct strbuf
*buf
)
2203 if (!strncmp(tp
->symbol
, "0x", 2)) {
2204 /* Absolute address. See try_to_find_absolute_address() */
2205 return strbuf_addf(buf
, "%s%s0x%" PRIx64
, tp
->module
?: "",
2206 tp
->module
? ":" : "", tp
->address
);
2208 return strbuf_addf(buf
, "%s%s%s+%lu", tp
->module
?: "",
2209 tp
->module
? ":" : "", tp
->symbol
, tp
->offset
);
2213 char *synthesize_probe_trace_command(struct probe_trace_event
*tev
)
2215 struct probe_trace_point
*tp
= &tev
->point
;
2220 if (strbuf_init(&buf
, 32) < 0)
2223 if (strbuf_addf(&buf
, "%c:%s/%s ", tp
->retprobe
? 'r' : 'p',
2224 tev
->group
, tev
->event
) < 0)
2228 err
= synthesize_uprobe_trace_def(tp
, &buf
);
2230 err
= synthesize_kprobe_trace_def(tp
, &buf
);
2233 err
= synthesize_probe_trace_args(tev
, &buf
);
2236 ret
= strbuf_detach(&buf
, NULL
);
2238 strbuf_release(&buf
);
2242 static int find_perf_probe_point_from_map(struct probe_trace_point
*tp
,
2243 struct perf_probe_point
*pp
,
2246 struct symbol
*sym
= NULL
;
2247 struct map
*map
= NULL
;
2248 u64 addr
= tp
->address
;
2252 map
= dso__new_map(tp
->module
);
2255 sym
= map__find_symbol(map
, addr
);
2257 if (tp
->symbol
&& !addr
) {
2258 if (kernel_get_symbol_address_by_name(tp
->symbol
,
2259 &addr
, true, false) < 0)
2264 sym
= machine__find_kernel_symbol(host_machine
, addr
, &map
);
2271 pp
->retprobe
= tp
->retprobe
;
2272 pp
->offset
= addr
- map__unmap_ip(map
, sym
->start
);
2273 pp
->function
= strdup(sym
->name
);
2274 ret
= pp
->function
? 0 : -ENOMEM
;
2282 static int convert_to_perf_probe_point(struct probe_trace_point
*tp
,
2283 struct perf_probe_point
*pp
,
2289 ret
= find_perf_probe_point_from_dwarf(tp
, pp
, is_kprobe
);
2292 ret
= find_perf_probe_point_from_map(tp
, pp
, is_kprobe
);
2296 pr_debug("Failed to find probe point from both of dwarf and map.\n");
2299 pp
->function
= strdup(tp
->symbol
);
2300 pp
->offset
= tp
->offset
;
2302 ret
= e_snprintf(buf
, 128, "0x%" PRIx64
, tp
->address
);
2305 pp
->function
= strdup(buf
);
2308 if (pp
->function
== NULL
)
2311 pp
->retprobe
= tp
->retprobe
;
2316 static int convert_to_perf_probe_event(struct probe_trace_event
*tev
,
2317 struct perf_probe_event
*pev
, bool is_kprobe
)
2319 struct strbuf buf
= STRBUF_INIT
;
2322 /* Convert event/group name */
2323 pev
->event
= strdup(tev
->event
);
2324 pev
->group
= strdup(tev
->group
);
2325 if (pev
->event
== NULL
|| pev
->group
== NULL
)
2328 /* Convert trace_point to probe_point */
2329 ret
= convert_to_perf_probe_point(&tev
->point
, &pev
->point
, is_kprobe
);
2333 /* Convert trace_arg to probe_arg */
2334 pev
->nargs
= tev
->nargs
;
2335 pev
->args
= zalloc(sizeof(struct perf_probe_arg
) * pev
->nargs
);
2336 if (pev
->args
== NULL
)
2338 for (i
= 0; i
< tev
->nargs
&& ret
>= 0; i
++) {
2339 if (tev
->args
[i
].name
)
2340 pev
->args
[i
].name
= strdup(tev
->args
[i
].name
);
2342 if ((ret
= strbuf_init(&buf
, 32)) < 0)
2344 ret
= synthesize_probe_trace_arg(&tev
->args
[i
], &buf
);
2345 pev
->args
[i
].name
= strbuf_detach(&buf
, NULL
);
2347 if (pev
->args
[i
].name
== NULL
&& ret
>= 0)
2352 clear_perf_probe_event(pev
);
2357 void clear_perf_probe_event(struct perf_probe_event
*pev
)
2359 struct perf_probe_arg_field
*field
, *next
;
2364 zfree(&pev
->target
);
2365 clear_perf_probe_point(&pev
->point
);
2367 for (i
= 0; i
< pev
->nargs
; i
++) {
2368 zfree(&pev
->args
[i
].name
);
2369 zfree(&pev
->args
[i
].var
);
2370 zfree(&pev
->args
[i
].type
);
2371 field
= pev
->args
[i
].field
;
2374 zfree(&field
->name
);
2383 #define strdup_or_goto(str, label) \
2384 ({ char *__p = NULL; if (str && !(__p = strdup(str))) goto label; __p; })
2386 static int perf_probe_point__copy(struct perf_probe_point
*dst
,
2387 struct perf_probe_point
*src
)
2389 dst
->file
= strdup_or_goto(src
->file
, out_err
);
2390 dst
->function
= strdup_or_goto(src
->function
, out_err
);
2391 dst
->lazy_line
= strdup_or_goto(src
->lazy_line
, out_err
);
2392 dst
->line
= src
->line
;
2393 dst
->retprobe
= src
->retprobe
;
2394 dst
->offset
= src
->offset
;
2398 clear_perf_probe_point(dst
);
2402 static int perf_probe_arg__copy(struct perf_probe_arg
*dst
,
2403 struct perf_probe_arg
*src
)
2405 struct perf_probe_arg_field
*field
, **ppfield
;
2407 dst
->name
= strdup_or_goto(src
->name
, out_err
);
2408 dst
->var
= strdup_or_goto(src
->var
, out_err
);
2409 dst
->type
= strdup_or_goto(src
->type
, out_err
);
2412 ppfield
= &(dst
->field
);
2414 *ppfield
= zalloc(sizeof(*field
));
2417 (*ppfield
)->name
= strdup_or_goto(field
->name
, out_err
);
2418 (*ppfield
)->index
= field
->index
;
2419 (*ppfield
)->ref
= field
->ref
;
2420 field
= field
->next
;
2421 ppfield
= &((*ppfield
)->next
);
2428 int perf_probe_event__copy(struct perf_probe_event
*dst
,
2429 struct perf_probe_event
*src
)
2433 dst
->event
= strdup_or_goto(src
->event
, out_err
);
2434 dst
->group
= strdup_or_goto(src
->group
, out_err
);
2435 dst
->target
= strdup_or_goto(src
->target
, out_err
);
2436 dst
->uprobes
= src
->uprobes
;
2438 if (perf_probe_point__copy(&dst
->point
, &src
->point
) < 0)
2441 dst
->args
= zalloc(sizeof(struct perf_probe_arg
) * src
->nargs
);
2444 dst
->nargs
= src
->nargs
;
2446 for (i
= 0; i
< src
->nargs
; i
++)
2447 if (perf_probe_arg__copy(&dst
->args
[i
], &src
->args
[i
]) < 0)
2452 clear_perf_probe_event(dst
);
2456 void clear_probe_trace_event(struct probe_trace_event
*tev
)
2458 struct probe_trace_arg_ref
*ref
, *next
;
2463 zfree(&tev
->point
.symbol
);
2464 zfree(&tev
->point
.realname
);
2465 zfree(&tev
->point
.module
);
2466 for (i
= 0; i
< tev
->nargs
; i
++) {
2467 zfree(&tev
->args
[i
].name
);
2468 zfree(&tev
->args
[i
].value
);
2469 zfree(&tev
->args
[i
].type
);
2470 ref
= tev
->args
[i
].ref
;
2481 struct kprobe_blacklist_node
{
2482 struct list_head list
;
2488 static void kprobe_blacklist__delete(struct list_head
*blacklist
)
2490 struct kprobe_blacklist_node
*node
;
2492 while (!list_empty(blacklist
)) {
2493 node
= list_first_entry(blacklist
,
2494 struct kprobe_blacklist_node
, list
);
2495 list_del_init(&node
->list
);
2496 zfree(&node
->symbol
);
2501 static int kprobe_blacklist__load(struct list_head
*blacklist
)
2503 struct kprobe_blacklist_node
*node
;
2504 const char *__debugfs
= debugfs__mountpoint();
2505 char buf
[PATH_MAX
], *p
;
2509 if (__debugfs
== NULL
)
2512 ret
= e_snprintf(buf
, PATH_MAX
, "%s/kprobes/blacklist", __debugfs
);
2516 fp
= fopen(buf
, "r");
2521 while (fgets(buf
, PATH_MAX
, fp
)) {
2522 node
= zalloc(sizeof(*node
));
2527 INIT_LIST_HEAD(&node
->list
);
2528 list_add_tail(&node
->list
, blacklist
);
2529 if (sscanf(buf
, "0x%" PRIx64
"-0x%" PRIx64
, &node
->start
, &node
->end
) != 2) {
2533 p
= strchr(buf
, '\t');
2536 if (p
[strlen(p
) - 1] == '\n')
2537 p
[strlen(p
) - 1] = '\0';
2539 p
= (char *)"unknown";
2540 node
->symbol
= strdup(p
);
2541 if (!node
->symbol
) {
2545 pr_debug2("Blacklist: 0x%" PRIx64
"-0x%" PRIx64
", %s\n",
2546 node
->start
, node
->end
, node
->symbol
);
2550 kprobe_blacklist__delete(blacklist
);
2556 static struct kprobe_blacklist_node
*
2557 kprobe_blacklist__find_by_address(struct list_head
*blacklist
, u64 address
)
2559 struct kprobe_blacklist_node
*node
;
2561 list_for_each_entry(node
, blacklist
, list
) {
2562 if (node
->start
<= address
&& address
< node
->end
)
2569 static LIST_HEAD(kprobe_blacklist
);
2571 static void kprobe_blacklist__init(void)
2573 if (!list_empty(&kprobe_blacklist
))
2576 if (kprobe_blacklist__load(&kprobe_blacklist
) < 0)
2577 pr_debug("No kprobe blacklist support, ignored\n");
2580 static void kprobe_blacklist__release(void)
2582 kprobe_blacklist__delete(&kprobe_blacklist
);
2585 static bool kprobe_blacklist__listed(u64 address
)
2587 return !!kprobe_blacklist__find_by_address(&kprobe_blacklist
, address
);
2590 static int perf_probe_event__sprintf(const char *group
, const char *event
,
2591 struct perf_probe_event
*pev
,
2593 struct strbuf
*result
)
2598 if (asprintf(&buf
, "%s:%s", group
, event
) < 0)
2600 ret
= strbuf_addf(result
, " %-20s (on ", buf
);
2605 /* Synthesize only event probe point */
2606 buf
= synthesize_perf_probe_point(&pev
->point
);
2609 ret
= strbuf_addstr(result
, buf
);
2613 ret
= strbuf_addf(result
, " in %s", module
);
2615 if (!ret
&& pev
->nargs
> 0) {
2616 ret
= strbuf_add(result
, " with", 5);
2617 for (i
= 0; !ret
&& i
< pev
->nargs
; i
++) {
2618 buf
= synthesize_perf_probe_arg(&pev
->args
[i
]);
2621 ret
= strbuf_addf(result
, " %s", buf
);
2626 ret
= strbuf_addch(result
, ')');
2632 int show_perf_probe_event(const char *group
, const char *event
,
2633 struct perf_probe_event
*pev
,
2634 const char *module
, bool use_stdout
)
2636 struct strbuf buf
= STRBUF_INIT
;
2639 ret
= perf_probe_event__sprintf(group
, event
, pev
, module
, &buf
);
2642 printf("%s\n", buf
.buf
);
2644 pr_info("%s\n", buf
.buf
);
2646 strbuf_release(&buf
);
2651 static bool filter_probe_trace_event(struct probe_trace_event
*tev
,
2652 struct strfilter
*filter
)
2656 /* At first, check the event name itself */
2657 if (strfilter__compare(filter
, tev
->event
))
2660 /* Next, check the combination of name and group */
2661 if (e_snprintf(tmp
, 128, "%s:%s", tev
->group
, tev
->event
) < 0)
2663 return strfilter__compare(filter
, tmp
);
2666 static int __show_perf_probe_events(int fd
, bool is_kprobe
,
2667 struct strfilter
*filter
)
2670 struct probe_trace_event tev
;
2671 struct perf_probe_event pev
;
2672 struct strlist
*rawlist
;
2673 struct str_node
*ent
;
2675 memset(&tev
, 0, sizeof(tev
));
2676 memset(&pev
, 0, sizeof(pev
));
2678 rawlist
= probe_file__get_rawlist(fd
);
2682 strlist__for_each_entry(ent
, rawlist
) {
2683 ret
= parse_probe_trace_command(ent
->s
, &tev
);
2685 if (!filter_probe_trace_event(&tev
, filter
))
2687 ret
= convert_to_perf_probe_event(&tev
, &pev
,
2691 ret
= show_perf_probe_event(pev
.group
, pev
.event
,
2692 &pev
, tev
.point
.module
,
2696 clear_perf_probe_event(&pev
);
2697 clear_probe_trace_event(&tev
);
2701 strlist__delete(rawlist
);
2702 /* Cleanup cached debuginfo if needed */
2703 debuginfo_cache__exit();
2708 /* List up current perf-probe events */
2709 int show_perf_probe_events(struct strfilter
*filter
)
2711 int kp_fd
, up_fd
, ret
;
2715 if (probe_conf
.cache
)
2716 return probe_cache__show_all_caches(filter
);
2718 ret
= init_probe_symbol_maps(false);
2722 ret
= probe_file__open_both(&kp_fd
, &up_fd
, 0);
2727 ret
= __show_perf_probe_events(kp_fd
, true, filter
);
2728 if (up_fd
>= 0 && ret
>= 0)
2729 ret
= __show_perf_probe_events(up_fd
, false, filter
);
2734 exit_probe_symbol_maps();
2739 static int get_new_event_name(char *buf
, size_t len
, const char *base
,
2740 struct strlist
*namelist
, bool ret_event
,
2748 nbase
= strdup(base
);
2752 /* Cut off the dot suffixes (e.g. .const, .isra) and version suffixes */
2753 p
= strpbrk(nbase
, ".@");
2754 if (p
&& p
!= nbase
)
2757 /* Try no suffix number */
2758 ret
= e_snprintf(buf
, len
, "%s%s", nbase
, ret_event
? "__return" : "");
2760 pr_warning("snprintf() failed: %d; the event name nbase='%s' is too long\n", ret
, nbase
);
2763 if (!strlist__has_entry(namelist
, buf
))
2766 if (!allow_suffix
) {
2767 pr_warning("Error: event \"%s\" already exists.\n"
2768 " Hint: Remove existing event by 'perf probe -d'\n"
2769 " or force duplicates by 'perf probe -f'\n"
2770 " or set 'force=yes' in BPF source.\n",
2776 /* Try to add suffix */
2777 for (i
= 1; i
< MAX_EVENT_INDEX
; i
++) {
2778 ret
= e_snprintf(buf
, len
, "%s_%d", nbase
, i
);
2780 pr_debug("snprintf() failed: %d\n", ret
);
2783 if (!strlist__has_entry(namelist
, buf
))
2786 if (i
== MAX_EVENT_INDEX
) {
2787 pr_warning("Too many events are on the same function.\n");
2794 /* Final validation */
2795 if (ret
>= 0 && !is_c_func_name(buf
)) {
2796 pr_warning("Internal error: \"%s\" is an invalid event name.\n",
2804 /* Warn if the current kernel's uprobe implementation is old */
2805 static void warn_uprobe_event_compat(struct probe_trace_event
*tev
)
2808 char *buf
= synthesize_probe_trace_command(tev
);
2809 struct probe_trace_point
*tp
= &tev
->point
;
2811 if (tp
->ref_ctr_offset
&& !uprobe_ref_ctr_is_supported()) {
2812 pr_warning("A semaphore is associated with %s:%s and "
2813 "seems your kernel doesn't support it.\n",
2814 tev
->group
, tev
->event
);
2817 /* Old uprobe event doesn't support memory dereference */
2818 if (!tev
->uprobes
|| tev
->nargs
== 0 || !buf
)
2821 for (i
= 0; i
< tev
->nargs
; i
++) {
2822 if (strchr(tev
->args
[i
].value
, '@')) {
2823 pr_warning("%s accesses a variable by symbol name, but that is not supported for user application probe.\n",
2824 tev
->args
[i
].value
);
2827 if (strglobmatch(tev
->args
[i
].value
, "[$+-]*")) {
2828 pr_warning("Please upgrade your kernel to at least 3.14 to have access to feature %s\n",
2829 tev
->args
[i
].value
);
2837 /* Set new name from original perf_probe_event and namelist */
2838 static int probe_trace_event__set_name(struct probe_trace_event
*tev
,
2839 struct perf_probe_event
*pev
,
2840 struct strlist
*namelist
,
2843 const char *event
, *group
;
2847 /* If probe_event or trace_event already have the name, reuse it */
2848 if (pev
->event
&& !pev
->sdt
)
2850 else if (tev
->event
)
2853 /* Or generate new one from probe point */
2854 if (pev
->point
.function
&&
2855 (strncmp(pev
->point
.function
, "0x", 2) != 0) &&
2856 !strisglob(pev
->point
.function
))
2857 event
= pev
->point
.function
;
2859 event
= tev
->point
.realname
;
2861 if (pev
->group
&& !pev
->sdt
)
2863 else if (tev
->group
)
2866 group
= PERFPROBE_GROUP
;
2868 /* Get an unused new event name */
2869 ret
= get_new_event_name(buf
, sizeof(buf
), event
, namelist
,
2870 tev
->point
.retprobe
, allow_suffix
);
2876 tev
->event
= strdup(event
);
2877 tev
->group
= strdup(group
);
2878 if (tev
->event
== NULL
|| tev
->group
== NULL
)
2882 * Add new event name to namelist if multiprobe event is NOT
2883 * supported, since we have to use new event name for following
2884 * probes in that case.
2886 if (!multiprobe_event_is_supported())
2887 strlist__add(namelist
, event
);
2891 static int __open_probe_file_and_namelist(bool uprobe
,
2892 struct strlist
**namelist
)
2896 fd
= probe_file__open(PF_FL_RW
| (uprobe
? PF_FL_UPROBE
: 0));
2900 /* Get current event names */
2901 *namelist
= probe_file__get_namelist(fd
);
2903 pr_debug("Failed to get current event list.\n");
2910 static int __add_probe_trace_events(struct perf_probe_event
*pev
,
2911 struct probe_trace_event
*tevs
,
2912 int ntevs
, bool allow_suffix
)
2914 int i
, fd
[2] = {-1, -1}, up
, ret
;
2915 struct probe_trace_event
*tev
= NULL
;
2916 struct probe_cache
*cache
= NULL
;
2917 struct strlist
*namelist
[2] = {NULL
, NULL
};
2918 struct nscookie nsc
;
2920 up
= pev
->uprobes
? 1 : 0;
2921 fd
[up
] = __open_probe_file_and_namelist(up
, &namelist
[up
]);
2926 for (i
= 0; i
< ntevs
; i
++) {
2928 up
= tev
->uprobes
? 1 : 0;
2929 if (fd
[up
] == -1) { /* Open the kprobe/uprobe_events */
2930 fd
[up
] = __open_probe_file_and_namelist(up
,
2935 /* Skip if the symbol is out of .text or blacklisted */
2936 if (!tev
->point
.symbol
&& !pev
->uprobes
)
2939 /* Set new name for tev (and update namelist) */
2940 ret
= probe_trace_event__set_name(tev
, pev
, namelist
[up
],
2945 nsinfo__mountns_enter(pev
->nsi
, &nsc
);
2946 ret
= probe_file__add_event(fd
[up
], tev
);
2947 nsinfo__mountns_exit(&nsc
);
2952 * Probes after the first probe which comes from same
2953 * user input are always allowed to add suffix, because
2954 * there might be several addresses corresponding to
2957 allow_suffix
= true;
2959 if (ret
== -EINVAL
&& pev
->uprobes
)
2960 warn_uprobe_event_compat(tev
);
2961 if (ret
== 0 && probe_conf
.cache
) {
2962 cache
= probe_cache__new(pev
->target
, pev
->nsi
);
2964 probe_cache__add_entry(cache
, pev
, tevs
, ntevs
) < 0 ||
2965 probe_cache__commit(cache
) < 0)
2966 pr_warning("Failed to add event to probe cache\n");
2967 probe_cache__delete(cache
);
2971 for (up
= 0; up
< 2; up
++) {
2972 strlist__delete(namelist
[up
]);
2979 static int find_probe_functions(struct map
*map
, char *name
,
2980 struct symbol
**syms
)
2984 struct rb_node
*tmp
;
2985 const char *norm
, *ver
;
2987 bool cut_version
= true;
2989 if (map__load(map
) < 0)
2990 return -EACCES
; /* Possible permission error to load symbols */
2992 /* If user gives a version, don't cut off the version from symbols */
2993 if (strchr(name
, '@'))
2994 cut_version
= false;
2996 map__for_each_symbol(map
, sym
, tmp
) {
2997 norm
= arch__normalize_symbol_name(sym
->name
);
3002 /* We don't care about default symbol or not */
3003 ver
= strchr(norm
, '@');
3005 buf
= strndup(norm
, ver
- norm
);
3012 if (strglobmatch(norm
, name
)) {
3014 if (syms
&& found
< probe_conf
.max_probes
)
3015 syms
[found
- 1] = sym
;
3024 void __weak
arch__fix_tev_from_maps(struct perf_probe_event
*pev __maybe_unused
,
3025 struct probe_trace_event
*tev __maybe_unused
,
3026 struct map
*map __maybe_unused
,
3027 struct symbol
*sym __maybe_unused
) { }
3030 static void pr_kallsyms_access_error(void)
3032 pr_err("Please ensure you can read the /proc/kallsyms symbol addresses.\n"
3033 "If /proc/sys/kernel/kptr_restrict is '2', you can not read\n"
3034 "kernel symbol addresses even if you are a superuser. Please change\n"
3035 "it to '1'. If kptr_restrict is '1', the superuser can read the\n"
3036 "symbol addresses.\n"
3037 "In that case, please run this command again with sudo.\n");
3041 * Find probe function addresses from map.
3042 * Return an error or the number of found probe_trace_event
3044 static int find_probe_trace_events_from_map(struct perf_probe_event
*pev
,
3045 struct probe_trace_event
**tevs
)
3047 struct map
*map
= NULL
;
3048 struct ref_reloc_sym
*reloc_sym
= NULL
;
3050 struct symbol
**syms
= NULL
;
3051 struct probe_trace_event
*tev
;
3052 struct perf_probe_point
*pp
= &pev
->point
;
3053 struct probe_trace_point
*tp
;
3054 int num_matched_functions
;
3055 int ret
, i
, j
, skipped
= 0;
3058 map
= get_target_map(pev
->target
, pev
->nsi
, pev
->uprobes
);
3064 syms
= malloc(sizeof(struct symbol
*) * probe_conf
.max_probes
);
3071 * Load matched symbols: Since the different local symbols may have
3072 * same name but different addresses, this lists all the symbols.
3074 num_matched_functions
= find_probe_functions(map
, pp
->function
, syms
);
3075 if (num_matched_functions
<= 0) {
3076 if (num_matched_functions
== -EACCES
) {
3077 pr_err("Failed to load symbols from %s\n",
3078 pev
->target
?: "/proc/kallsyms");
3080 pr_err("Please ensure the file is not stripped.\n");
3082 pr_kallsyms_access_error();
3084 pr_err("Failed to find symbol %s in %s\n", pp
->function
,
3085 pev
->target
? : "kernel");
3088 } else if (num_matched_functions
> probe_conf
.max_probes
) {
3089 pr_err("Too many functions matched in %s\n",
3090 pev
->target
? : "kernel");
3095 /* Note that the symbols in the kmodule are not relocated */
3096 if (!pev
->uprobes
&& !pev
->target
&&
3097 (!pp
->retprobe
|| kretprobe_offset_is_supported())) {
3098 reloc_sym
= kernel_get_ref_reloc_sym(NULL
);
3100 pr_warning("Relocated base symbol is not found! "
3101 "Check /proc/sys/kernel/kptr_restrict\n"
3102 "and /proc/sys/kernel/perf_event_paranoid. "
3103 "Or run as privileged perf user.\n\n");
3109 /* Setup result trace-probe-events */
3110 *tevs
= zalloc(sizeof(*tev
) * num_matched_functions
);
3118 for (j
= 0; j
< num_matched_functions
; j
++) {
3121 if (sym
->type
!= STT_FUNC
)
3124 /* There can be duplicated symbols in the map */
3125 for (i
= 0; i
< j
; i
++)
3126 if (sym
->start
== syms
[i
]->start
) {
3127 pr_debug("Found duplicated symbol %s @ %" PRIx64
"\n",
3128 sym
->name
, sym
->start
);
3134 tev
= (*tevs
) + ret
;
3136 if (ret
== num_matched_functions
) {
3137 pr_warning("Too many symbols are listed. Skip it.\n");
3142 if (pp
->offset
> sym
->end
- sym
->start
) {
3143 pr_warning("Offset %ld is bigger than the size of %s\n",
3144 pp
->offset
, sym
->name
);
3148 /* Add one probe point */
3149 tp
->address
= map__unmap_ip(map
, sym
->start
) + pp
->offset
;
3151 /* Check the kprobe (not in module) is within .text */
3152 if (!pev
->uprobes
&& !pev
->target
&&
3153 kprobe_warn_out_range(sym
->name
, tp
->address
)) {
3154 tp
->symbol
= NULL
; /* Skip it */
3156 } else if (reloc_sym
) {
3157 tp
->symbol
= strdup_or_goto(reloc_sym
->name
, nomem_out
);
3158 tp
->offset
= tp
->address
- reloc_sym
->addr
;
3160 tp
->symbol
= strdup_or_goto(sym
->name
, nomem_out
);
3161 tp
->offset
= pp
->offset
;
3163 tp
->realname
= strdup_or_goto(sym
->name
, nomem_out
);
3165 tp
->retprobe
= pp
->retprobe
;
3168 tev
->point
.module
= strdup_or_goto(pev
->target
,
3171 mod_name
= find_module_name(pev
->target
);
3173 strdup(mod_name
? mod_name
: pev
->target
);
3175 if (!tev
->point
.module
)
3179 tev
->uprobes
= pev
->uprobes
;
3180 tev
->nargs
= pev
->nargs
;
3182 tev
->args
= zalloc(sizeof(struct probe_trace_arg
) *
3184 if (tev
->args
== NULL
)
3187 for (i
= 0; i
< tev
->nargs
; i
++) {
3188 if (pev
->args
[i
].name
)
3190 strdup_or_goto(pev
->args
[i
].name
,
3193 tev
->args
[i
].value
= strdup_or_goto(pev
->args
[i
].var
,
3195 if (pev
->args
[i
].type
)
3197 strdup_or_goto(pev
->args
[i
].type
,
3200 arch__fix_tev_from_maps(pev
, tev
, map
, sym
);
3202 if (ret
== skipped
) {
3215 clear_probe_trace_events(*tevs
, num_matched_functions
);
3220 static int try_to_find_absolute_address(struct perf_probe_event
*pev
,
3221 struct probe_trace_event
**tevs
)
3223 struct perf_probe_point
*pp
= &pev
->point
;
3224 struct probe_trace_event
*tev
;
3225 struct probe_trace_point
*tp
;
3228 if (!(pev
->point
.function
&& !strncmp(pev
->point
.function
, "0x", 2)))
3230 if (perf_probe_event_need_dwarf(pev
))
3234 * This is 'perf probe /lib/libc.so 0xabcd'. Try to probe at
3237 * Only one tev can be generated by this.
3239 *tevs
= zalloc(sizeof(*tev
));
3247 * Don't use tp->offset, use address directly, because
3248 * in synthesize_probe_trace_command() address cannot be
3251 tp
->address
= pev
->point
.abs_address
;
3252 tp
->retprobe
= pp
->retprobe
;
3253 tev
->uprobes
= pev
->uprobes
;
3257 * Give it a '0x' leading symbol name.
3258 * In __add_probe_trace_events, a NULL symbol is interpreted as
3261 if (asprintf(&tp
->symbol
, "0x%" PRIx64
, tp
->address
) < 0)
3264 /* For kprobe, check range */
3265 if ((!tev
->uprobes
) &&
3266 (kprobe_warn_out_range(tev
->point
.symbol
,
3267 tev
->point
.address
))) {
3272 if (asprintf(&tp
->realname
, "abs_%" PRIx64
, tp
->address
) < 0)
3276 tp
->module
= strdup(pev
->target
);
3282 tev
->group
= strdup(pev
->group
);
3288 tev
->event
= strdup(pev
->event
);
3293 tev
->nargs
= pev
->nargs
;
3294 tev
->args
= zalloc(sizeof(struct probe_trace_arg
) * tev
->nargs
);
3298 for (i
= 0; i
< tev
->nargs
; i
++)
3299 copy_to_probe_trace_arg(&tev
->args
[i
], &pev
->args
[i
]);
3304 clear_probe_trace_events(*tevs
, 1);
3309 /* Concatenate two arrays */
3310 static void *memcat(void *a
, size_t sz_a
, void *b
, size_t sz_b
)
3314 ret
= malloc(sz_a
+ sz_b
);
3316 memcpy(ret
, a
, sz_a
);
3317 memcpy(ret
+ sz_a
, b
, sz_b
);
3323 concat_probe_trace_events(struct probe_trace_event
**tevs
, int *ntevs
,
3324 struct probe_trace_event
**tevs2
, int ntevs2
)
3326 struct probe_trace_event
*new_tevs
;
3336 if (*ntevs
+ ntevs2
> probe_conf
.max_probes
)
3339 /* Concatenate the array of probe_trace_event */
3340 new_tevs
= memcat(*tevs
, (*ntevs
) * sizeof(**tevs
),
3341 *tevs2
, ntevs2
* sizeof(**tevs2
));
3351 clear_probe_trace_events(*tevs2
, ntevs2
);
3358 * Try to find probe_trace_event from given probe caches. Return the number
3359 * of cached events found, if an error occurs return the error.
3361 static int find_cached_events(struct perf_probe_event
*pev
,
3362 struct probe_trace_event
**tevs
,
3365 struct probe_cache
*cache
;
3366 struct probe_cache_entry
*entry
;
3367 struct probe_trace_event
*tmp_tevs
= NULL
;
3371 cache
= probe_cache__new(target
, pev
->nsi
);
3372 /* Return 0 ("not found") if the target has no probe cache. */
3376 for_each_probe_cache_entry(entry
, cache
) {
3377 /* Skip the cache entry which has no name */
3378 if (!entry
->pev
.event
|| !entry
->pev
.group
)
3380 if ((!pev
->group
|| strglobmatch(entry
->pev
.group
, pev
->group
)) &&
3381 strglobmatch(entry
->pev
.event
, pev
->event
)) {
3382 ret
= probe_cache_entry__get_event(entry
, &tmp_tevs
);
3384 ret
= concat_probe_trace_events(tevs
, &ntevs
,
3390 probe_cache__delete(cache
);
3392 clear_probe_trace_events(*tevs
, ntevs
);
3396 if (ntevs
> 0 && target
&& target
[0] == '/')
3397 pev
->uprobes
= true;
3403 /* Try to find probe_trace_event from all probe caches */
3404 static int find_cached_events_all(struct perf_probe_event
*pev
,
3405 struct probe_trace_event
**tevs
)
3407 struct probe_trace_event
*tmp_tevs
= NULL
;
3408 struct strlist
*bidlist
;
3409 struct str_node
*nd
;
3414 /* Get the buildid list of all valid caches */
3415 bidlist
= build_id_cache__list_all(true);
3418 pr_debug("Failed to get buildids: %d\n", ret
);
3423 strlist__for_each_entry(nd
, bidlist
) {
3424 pathname
= build_id_cache__origname(nd
->s
);
3425 ret
= find_cached_events(pev
, &tmp_tevs
, pathname
);
3426 /* In the case of cnt == 0, we just skip it */
3428 ret
= concat_probe_trace_events(tevs
, &ntevs
,
3434 strlist__delete(bidlist
);
3437 clear_probe_trace_events(*tevs
, ntevs
);
3445 static int find_probe_trace_events_from_cache(struct perf_probe_event
*pev
,
3446 struct probe_trace_event
**tevs
)
3448 struct probe_cache
*cache
;
3449 struct probe_cache_entry
*entry
;
3450 struct probe_trace_event
*tev
;
3451 struct str_node
*node
;
3455 /* For SDT/cached events, we use special search functions */
3457 return find_cached_events_all(pev
, tevs
);
3459 return find_cached_events(pev
, tevs
, pev
->target
);
3461 cache
= probe_cache__new(pev
->target
, pev
->nsi
);
3465 entry
= probe_cache__find(cache
, pev
);
3467 /* SDT must be in the cache */
3468 ret
= pev
->sdt
? -ENOENT
: 0;
3472 ret
= strlist__nr_entries(entry
->tevlist
);
3473 if (ret
> probe_conf
.max_probes
) {
3474 pr_debug("Too many entries matched in the cache of %s\n",
3475 pev
->target
? : "kernel");
3480 *tevs
= zalloc(ret
* sizeof(*tev
));
3487 strlist__for_each_entry(node
, entry
->tevlist
) {
3488 tev
= &(*tevs
)[i
++];
3489 ret
= parse_probe_trace_command(node
->s
, tev
);
3492 /* Set the uprobes attribute as same as original */
3493 tev
->uprobes
= pev
->uprobes
;
3498 probe_cache__delete(cache
);
3502 static int convert_to_probe_trace_events(struct perf_probe_event
*pev
,
3503 struct probe_trace_event
**tevs
)
3507 if (!pev
->group
&& !pev
->sdt
) {
3508 /* Set group name if not given */
3509 if (!pev
->uprobes
) {
3510 pev
->group
= strdup(PERFPROBE_GROUP
);
3511 ret
= pev
->group
? 0 : -ENOMEM
;
3513 ret
= convert_exec_to_group(pev
->target
, &pev
->group
);
3515 pr_warning("Failed to make a group name.\n");
3520 ret
= try_to_find_absolute_address(pev
, tevs
);
3524 /* At first, we need to lookup cache entry */
3525 ret
= find_probe_trace_events_from_cache(pev
, tevs
);
3526 if (ret
> 0 || pev
->sdt
) /* SDT can be found only in the cache */
3527 return ret
== 0 ? -ENOENT
: ret
; /* Found in probe cache */
3529 /* Convert perf_probe_event with debuginfo */
3530 ret
= try_to_find_probe_trace_events(pev
, tevs
);
3532 return ret
; /* Found in debuginfo or got an error */
3534 return find_probe_trace_events_from_map(pev
, tevs
);
3537 int convert_perf_probe_events(struct perf_probe_event
*pevs
, int npevs
)
3541 /* Loop 1: convert all events */
3542 for (i
= 0; i
< npevs
; i
++) {
3543 /* Init kprobe blacklist if needed */
3544 if (!pevs
[i
].uprobes
)
3545 kprobe_blacklist__init();
3546 /* Convert with or without debuginfo */
3547 ret
= convert_to_probe_trace_events(&pevs
[i
], &pevs
[i
].tevs
);
3550 pevs
[i
].ntevs
= ret
;
3552 /* This just release blacklist only if allocated */
3553 kprobe_blacklist__release();
3558 static int show_probe_trace_event(struct probe_trace_event
*tev
)
3560 char *buf
= synthesize_probe_trace_command(tev
);
3563 pr_debug("Failed to synthesize probe trace event.\n");
3567 /* Showing definition always go stdout */
3568 printf("%s\n", buf
);
3574 int show_probe_trace_events(struct perf_probe_event
*pevs
, int npevs
)
3576 struct strlist
*namelist
= strlist__new(NULL
, NULL
);
3577 struct probe_trace_event
*tev
;
3578 struct perf_probe_event
*pev
;
3584 for (j
= 0; j
< npevs
&& !ret
; j
++) {
3586 for (i
= 0; i
< pev
->ntevs
&& !ret
; i
++) {
3587 tev
= &pev
->tevs
[i
];
3588 /* Skip if the symbol is out of .text or blacklisted */
3589 if (!tev
->point
.symbol
&& !pev
->uprobes
)
3592 /* Set new name for tev (and update namelist) */
3593 ret
= probe_trace_event__set_name(tev
, pev
,
3596 ret
= show_probe_trace_event(tev
);
3599 strlist__delete(namelist
);
3604 static int show_bootconfig_event(struct probe_trace_event
*tev
)
3606 struct probe_trace_point
*tp
= &tev
->point
;
3611 if (strbuf_init(&buf
, 32) < 0)
3614 err
= synthesize_kprobe_trace_def(tp
, &buf
);
3616 err
= synthesize_probe_trace_args(tev
, &buf
);
3618 ret
= strbuf_detach(&buf
, NULL
);
3619 strbuf_release(&buf
);
3622 printf("'%s'", ret
);
3629 int show_bootconfig_events(struct perf_probe_event
*pevs
, int npevs
)
3631 struct strlist
*namelist
= strlist__new(NULL
, NULL
);
3632 struct probe_trace_event
*tev
;
3633 struct perf_probe_event
*pev
;
3634 char *cur_name
= NULL
;
3640 for (j
= 0; j
< npevs
&& !ret
; j
++) {
3642 if (pev
->group
&& strcmp(pev
->group
, "probe"))
3643 pr_warning("WARN: Group name %s is ignored\n", pev
->group
);
3645 pr_warning("ERROR: Bootconfig doesn't support uprobes\n");
3649 for (i
= 0; i
< pev
->ntevs
&& !ret
; i
++) {
3650 tev
= &pev
->tevs
[i
];
3651 /* Skip if the symbol is out of .text or blacklisted */
3652 if (!tev
->point
.symbol
&& !pev
->uprobes
)
3655 /* Set new name for tev (and update namelist) */
3656 ret
= probe_trace_event__set_name(tev
, pev
,
3661 if (!cur_name
|| strcmp(cur_name
, tev
->event
)) {
3662 printf("%sftrace.event.kprobes.%s.probe = ",
3663 cur_name
? "\n" : "", tev
->event
);
3664 cur_name
= tev
->event
;
3667 ret
= show_bootconfig_event(tev
);
3671 strlist__delete(namelist
);
3676 int apply_perf_probe_events(struct perf_probe_event
*pevs
, int npevs
)
3680 /* Loop 2: add all events */
3681 for (i
= 0; i
< npevs
; i
++) {
3682 ret
= __add_probe_trace_events(&pevs
[i
], pevs
[i
].tevs
,
3684 probe_conf
.force_add
);
3691 void cleanup_perf_probe_events(struct perf_probe_event
*pevs
, int npevs
)
3694 struct perf_probe_event
*pev
;
3696 /* Loop 3: cleanup and free trace events */
3697 for (i
= 0; i
< npevs
; i
++) {
3699 for (j
= 0; j
< pevs
[i
].ntevs
; j
++)
3700 clear_probe_trace_event(&pevs
[i
].tevs
[j
]);
3701 zfree(&pevs
[i
].tevs
);
3703 nsinfo__zput(pev
->nsi
);
3704 clear_perf_probe_event(&pevs
[i
]);
3708 int add_perf_probe_events(struct perf_probe_event
*pevs
, int npevs
)
3712 ret
= init_probe_symbol_maps(pevs
->uprobes
);
3716 ret
= convert_perf_probe_events(pevs
, npevs
);
3718 ret
= apply_perf_probe_events(pevs
, npevs
);
3720 cleanup_perf_probe_events(pevs
, npevs
);
3722 exit_probe_symbol_maps();
3726 int del_perf_probe_events(struct strfilter
*filter
)
3728 int ret
, ret2
, ufd
= -1, kfd
= -1;
3729 char *str
= strfilter__string(filter
);
3734 /* Get current event names */
3735 ret
= probe_file__open_both(&kfd
, &ufd
, PF_FL_RW
);
3739 ret
= probe_file__del_events(kfd
, filter
);
3740 if (ret
< 0 && ret
!= -ENOENT
)
3743 ret2
= probe_file__del_events(ufd
, filter
);
3744 if (ret2
< 0 && ret2
!= -ENOENT
) {
3761 int show_available_funcs(const char *target
, struct nsinfo
*nsi
,
3762 struct strfilter
*_filter
, bool user
)
3768 ret
= init_probe_symbol_maps(user
);
3772 /* Get a symbol map */
3773 map
= get_target_map(target
, nsi
, user
);
3775 pr_err("Failed to get a map for %s\n", (target
) ? : "kernel");
3779 ret
= map__load(map
);
3782 char *str
= strfilter__string(_filter
);
3783 pr_err("Failed to find symbols matched to \"%s\"\n",
3787 pr_err("Failed to load symbols in %s\n",
3788 (target
) ? : "kernel");
3791 dso
= map__dso(map
);
3792 dso__sort_by_name(dso
);
3794 /* Show all (filtered) symbols */
3797 for (size_t i
= 0; i
< dso__symbol_names_len(dso
); i
++) {
3798 struct symbol
*pos
= dso__symbol_names(dso
)[i
];
3800 if (strfilter__compare(_filter
, pos
->name
))
3801 printf("%s\n", pos
->name
);
3805 exit_probe_symbol_maps();
3810 int copy_to_probe_trace_arg(struct probe_trace_arg
*tvar
,
3811 struct perf_probe_arg
*pvar
)
3813 tvar
->value
= strdup(pvar
->var
);
3814 if (tvar
->value
== NULL
)
3817 tvar
->type
= strdup(pvar
->type
);
3818 if (tvar
->type
== NULL
)
3822 tvar
->name
= strdup(pvar
->name
);
3823 if (tvar
->name
== NULL
)