]>
git.ipfire.org Git - thirdparty/linux.git/blob - tools/perf/util/probe-event.c
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>
24 #include "namespaces.h"
26 #include "strfilter.h"
31 #include "map_groups.h"
34 #include <api/fs/fs.h>
35 #include "trace-event.h" /* For __maybe_unused */
36 #include "probe-event.h"
37 #include "probe-finder.h"
38 #include "probe-file.h"
42 #include "sane_ctype.h"
44 #define PERFPROBE_GROUP "probe"
46 bool probe_event_dry_run
; /* Dry run flag */
47 struct probe_conf probe_conf
;
49 #define semantic_error(msg ...) pr_err("Semantic error :" msg)
51 int e_snprintf(char *str
, size_t size
, const char *format
, ...)
56 ret
= vsnprintf(str
, size
, format
, ap
);
63 static struct machine
*host_machine
;
65 /* Initialize symbol maps and path of vmlinux/modules */
66 int init_probe_symbol_maps(bool user_only
)
70 symbol_conf
.sort_by_name
= true;
71 symbol_conf
.allow_aliases
= true;
72 ret
= symbol__init(NULL
);
74 pr_debug("Failed to init symbol map.\n");
78 if (host_machine
|| user_only
) /* already initialized */
81 if (symbol_conf
.vmlinux_name
)
82 pr_debug("Use vmlinux: %s\n", symbol_conf
.vmlinux_name
);
84 host_machine
= machine__new_host();
86 pr_debug("machine__new_host() failed.\n");
92 pr_warning("Failed to init vmlinux path.\n");
96 void exit_probe_symbol_maps(void)
98 machine__delete(host_machine
);
103 static struct ref_reloc_sym
*kernel_get_ref_reloc_sym(void)
105 /* kmap->ref_reloc_sym should be set if host_machine is initialized */
107 struct map
*map
= machine__kernel_map(host_machine
);
109 if (map__load(map
) < 0)
112 kmap
= map__kmap(map
);
115 return kmap
->ref_reloc_sym
;
118 static int kernel_get_symbol_address_by_name(const char *name
, u64
*addr
,
119 bool reloc
, bool reladdr
)
121 struct ref_reloc_sym
*reloc_sym
;
125 /* ref_reloc_sym is just a label. Need a special fix*/
126 reloc_sym
= kernel_get_ref_reloc_sym();
127 if (reloc_sym
&& strcmp(name
, reloc_sym
->name
) == 0)
128 *addr
= (reloc
) ? reloc_sym
->addr
: reloc_sym
->unrelocated_addr
;
130 sym
= machine__find_kernel_symbol_by_name(host_machine
, name
, &map
);
133 *addr
= map
->unmap_ip(map
, sym
->start
) -
134 ((reloc
) ? 0 : map
->reloc
) -
135 ((reladdr
) ? map
->start
: 0);
140 static struct map
*kernel_get_module_map(const char *module
)
142 struct maps
*maps
= machine__kernel_maps(host_machine
);
145 /* A file path -- this is an offline module */
146 if (module
&& strchr(module
, '/'))
147 return dso__new_map(module
);
150 pos
= machine__kernel_map(host_machine
);
151 return map__get(pos
);
154 for (pos
= maps__first(maps
); pos
; pos
= map__next(pos
)) {
155 /* short_name is "[module]" */
156 if (strncmp(pos
->dso
->short_name
+ 1, module
,
157 pos
->dso
->short_name_len
- 2) == 0 &&
158 module
[pos
->dso
->short_name_len
- 2] == '\0') {
159 return map__get(pos
);
165 struct map
*get_target_map(const char *target
, struct nsinfo
*nsi
, bool user
)
167 /* Init maps of given executable or kernel */
171 map
= dso__new_map(target
);
173 map
->dso
->nsinfo
= nsinfo__get(nsi
);
176 return kernel_get_module_map(target
);
180 static int convert_exec_to_group(const char *exec
, char **result
)
182 char *ptr1
, *ptr2
, *exec_copy
;
186 exec_copy
= strdup(exec
);
190 ptr1
= basename(exec_copy
);
196 for (ptr2
= ptr1
; *ptr2
!= '\0'; ptr2
++) {
197 if (!isalnum(*ptr2
) && *ptr2
!= '_') {
203 ret
= e_snprintf(buf
, 64, "%s_%s", PERFPROBE_GROUP
, ptr1
);
207 *result
= strdup(buf
);
208 ret
= *result
? 0 : -ENOMEM
;
215 static void clear_perf_probe_point(struct perf_probe_point
*pp
)
222 static void clear_probe_trace_events(struct probe_trace_event
*tevs
, int ntevs
)
226 for (i
= 0; i
< ntevs
; i
++)
227 clear_probe_trace_event(tevs
+ i
);
230 static bool kprobe_blacklist__listed(unsigned long address
);
231 static bool kprobe_warn_out_range(const char *symbol
, unsigned long address
)
236 /* Get the address of _etext for checking non-probable text symbol */
237 ret
= kernel_get_symbol_address_by_name("_etext", &etext_addr
,
240 if (ret
== 0 && etext_addr
< address
)
241 pr_warning("%s is out of .text, skip it.\n", symbol
);
242 else if (kprobe_blacklist__listed(address
))
243 pr_warning("%s is blacklisted function, skip it.\n", symbol
);
251 * @module can be module name of module file path. In case of path,
252 * inspect elf and find out what is actual module name.
253 * Caller has to free mod_name after using it.
255 static char *find_module_name(const char *module
)
263 char *mod_name
= NULL
;
266 fd
= open(module
, O_RDONLY
);
270 elf
= elf_begin(fd
, PERF_ELF_C_READ_MMAP
, NULL
);
274 if (gelf_getehdr(elf
, &ehdr
) == NULL
)
277 sec
= elf_section_by_name(elf
, &ehdr
, &shdr
,
278 ".gnu.linkonce.this_module", NULL
);
282 data
= elf_getdata(sec
, NULL
);
283 if (!data
|| !data
->d_buf
)
288 * '.gnu.linkonce.this_module' section of kernel module elf directly
289 * maps to 'struct module' from linux/module.h. This section contains
290 * actual module name which will be used by kernel after loading it.
291 * But, we cannot use 'struct module' here since linux/module.h is not
292 * exposed to user-space. Offset of 'name' has remained same from long
293 * time, so hardcoding it here.
295 if (ehdr
.e_ident
[EI_CLASS
] == ELFCLASS32
)
297 else /* expect ELFCLASS64 by default */
300 mod_name
= strdup((char *)data
->d_buf
+ name_offset
);
309 #ifdef HAVE_DWARF_SUPPORT
311 static int kernel_get_module_dso(const char *module
, struct dso
**pdso
)
315 const char *vmlinux_name
;
319 char module_name
[128];
321 snprintf(module_name
, sizeof(module_name
), "[%s]", module
);
322 map
= map_groups__find_by_name(&host_machine
->kmaps
, module_name
);
327 pr_debug("Failed to find module %s.\n", module
);
331 map
= machine__kernel_map(host_machine
);
334 vmlinux_name
= symbol_conf
.vmlinux_name
;
337 ret
= dso__load_vmlinux(dso
, map
, vmlinux_name
, false);
339 ret
= dso__load_vmlinux_path(dso
, map
);
346 * Some binaries like glibc have special symbols which are on the symbol
347 * table, but not in the debuginfo. If we can find the address of the
348 * symbol from map, we can translate the address back to the probe point.
350 static int find_alternative_probe_point(struct debuginfo
*dinfo
,
351 struct perf_probe_point
*pp
,
352 struct perf_probe_point
*result
,
353 const char *target
, struct nsinfo
*nsi
,
356 struct map
*map
= NULL
;
361 /* This can work only for function-name based one */
362 if (!pp
->function
|| pp
->file
)
365 map
= get_target_map(target
, nsi
, uprobes
);
369 /* Find the address of given function */
370 map__for_each_symbol_by_name(map
, pp
->function
, sym
) {
372 address
= sym
->start
;
374 address
= map
->unmap_ip(map
, sym
->start
) - map
->reloc
;
381 pr_debug("Symbol %s address found : %" PRIx64
"\n",
382 pp
->function
, address
);
384 ret
= debuginfo__find_probe_point(dinfo
, (unsigned long)address
,
387 ret
= (!ret
) ? -ENOENT
: ret
;
389 result
->offset
+= pp
->offset
;
390 result
->line
+= pp
->line
;
391 result
->retprobe
= pp
->retprobe
;
401 static int get_alternative_probe_event(struct debuginfo
*dinfo
,
402 struct perf_probe_event
*pev
,
403 struct perf_probe_point
*tmp
)
407 memcpy(tmp
, &pev
->point
, sizeof(*tmp
));
408 memset(&pev
->point
, 0, sizeof(pev
->point
));
409 ret
= find_alternative_probe_point(dinfo
, tmp
, &pev
->point
, pev
->target
,
410 pev
->nsi
, pev
->uprobes
);
412 memcpy(&pev
->point
, tmp
, sizeof(*tmp
));
417 static int get_alternative_line_range(struct debuginfo
*dinfo
,
418 struct line_range
*lr
,
419 const char *target
, bool user
)
421 struct perf_probe_point pp
= { .function
= lr
->function
,
424 struct perf_probe_point result
;
427 memset(&result
, 0, sizeof(result
));
429 if (lr
->end
!= INT_MAX
)
430 len
= lr
->end
- lr
->start
;
431 ret
= find_alternative_probe_point(dinfo
, &pp
, &result
,
434 lr
->function
= result
.function
;
435 lr
->file
= result
.file
;
436 lr
->start
= result
.line
;
437 if (lr
->end
!= INT_MAX
)
438 lr
->end
= lr
->start
+ len
;
439 clear_perf_probe_point(&pp
);
444 /* Open new debuginfo of given module */
445 static struct debuginfo
*open_debuginfo(const char *module
, struct nsinfo
*nsi
,
448 const char *path
= module
;
449 char reason
[STRERR_BUFSIZE
];
450 struct debuginfo
*ret
= NULL
;
451 struct dso
*dso
= NULL
;
455 if (!module
|| !strchr(module
, '/')) {
456 err
= kernel_get_module_dso(module
, &dso
);
458 if (!dso
|| dso
->load_errno
== 0) {
459 if (!str_error_r(-err
, reason
, STRERR_BUFSIZE
))
460 strcpy(reason
, "(unknown)");
462 dso__strerror_load(dso
, reason
, STRERR_BUFSIZE
);
465 pr_err("Module %s is not loaded, please specify its full path name.\n", module
);
467 pr_err("Failed to find the path for the kernel: %s\n", reason
);
471 path
= dso
->long_name
;
473 nsinfo__mountns_enter(nsi
, &nsc
);
474 ret
= debuginfo__new(path
);
475 if (!ret
&& !silent
) {
476 pr_warning("The %s file has no debug information.\n", path
);
477 if (!module
|| !strtailcmp(path
, ".ko"))
478 pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, ");
480 pr_warning("Rebuild with -g, ");
481 pr_warning("or install an appropriate debuginfo package.\n");
483 nsinfo__mountns_exit(&nsc
);
487 /* For caching the last debuginfo */
488 static struct debuginfo
*debuginfo_cache
;
489 static char *debuginfo_cache_path
;
491 static struct debuginfo
*debuginfo_cache__open(const char *module
, bool silent
)
493 const char *path
= module
;
495 /* If the module is NULL, it should be the kernel. */
499 if (debuginfo_cache_path
&& !strcmp(debuginfo_cache_path
, path
))
502 /* Copy module path */
503 free(debuginfo_cache_path
);
504 debuginfo_cache_path
= strdup(path
);
505 if (!debuginfo_cache_path
) {
506 debuginfo__delete(debuginfo_cache
);
507 debuginfo_cache
= NULL
;
511 debuginfo_cache
= open_debuginfo(module
, NULL
, silent
);
512 if (!debuginfo_cache
)
513 zfree(&debuginfo_cache_path
);
515 return debuginfo_cache
;
518 static void debuginfo_cache__exit(void)
520 debuginfo__delete(debuginfo_cache
);
521 debuginfo_cache
= NULL
;
522 zfree(&debuginfo_cache_path
);
526 static int get_text_start_address(const char *exec
, unsigned long *address
,
532 int fd
, ret
= -ENOENT
;
535 nsinfo__mountns_enter(nsi
, &nsc
);
536 fd
= open(exec
, O_RDONLY
);
537 nsinfo__mountns_exit(&nsc
);
541 elf
= elf_begin(fd
, PERF_ELF_C_READ_MMAP
, NULL
);
547 if (gelf_getehdr(elf
, &ehdr
) == NULL
)
550 if (!elf_section_by_name(elf
, &ehdr
, &shdr
, ".text", NULL
))
553 *address
= shdr
.sh_addr
- shdr
.sh_offset
;
564 * Convert trace point to probe point with debuginfo
566 static int find_perf_probe_point_from_dwarf(struct probe_trace_point
*tp
,
567 struct perf_probe_point
*pp
,
570 struct debuginfo
*dinfo
= NULL
;
571 unsigned long stext
= 0;
572 u64 addr
= tp
->address
;
575 /* convert the address to dwarf address */
581 ret
= get_text_start_address(tp
->module
, &stext
, NULL
);
585 } else if (tp
->symbol
) {
586 /* If the module is given, this returns relative address */
587 ret
= kernel_get_symbol_address_by_name(tp
->symbol
, &addr
,
588 false, !!tp
->module
);
594 pr_debug("try to find information at %" PRIx64
" in %s\n", addr
,
595 tp
->module
? : "kernel");
597 dinfo
= debuginfo_cache__open(tp
->module
, verbose
<= 0);
599 ret
= debuginfo__find_probe_point(dinfo
,
600 (unsigned long)addr
, pp
);
605 pp
->retprobe
= tp
->retprobe
;
609 pr_debug("Failed to find corresponding probes from debuginfo.\n");
610 return ret
? : -ENOENT
;
613 /* Adjust symbol name and address */
614 static int post_process_probe_trace_point(struct probe_trace_point
*tp
,
615 struct map
*map
, unsigned long offs
)
618 u64 addr
= tp
->address
- offs
;
620 sym
= map__find_symbol(map
, addr
);
624 if (strcmp(sym
->name
, tp
->symbol
)) {
625 /* If we have no realname, use symbol for it */
627 tp
->realname
= tp
->symbol
;
630 tp
->symbol
= strdup(sym
->name
);
634 tp
->offset
= addr
- sym
->start
;
641 * Rename DWARF symbols to ELF symbols -- gcc sometimes optimizes functions
642 * and generate new symbols with suffixes such as .constprop.N or .isra.N
643 * etc. Since those symbols are not recorded in DWARF, we have to find
644 * correct generated symbols from offline ELF binary.
645 * For online kernel or uprobes we don't need this because those are
646 * rebased on _text, or already a section relative address.
649 post_process_offline_probe_trace_events(struct probe_trace_event
*tevs
,
650 int ntevs
, const char *pathname
)
653 unsigned long stext
= 0;
656 /* Prepare a map for offline binary */
657 map
= dso__new_map(pathname
);
658 if (!map
|| get_text_start_address(pathname
, &stext
, NULL
) < 0) {
659 pr_warning("Failed to get ELF symbols for %s\n", pathname
);
663 for (i
= 0; i
< ntevs
; i
++) {
664 ret
= post_process_probe_trace_point(&tevs
[i
].point
,
674 static int add_exec_to_probe_trace_events(struct probe_trace_event
*tevs
,
675 int ntevs
, const char *exec
,
679 unsigned long stext
= 0;
684 ret
= get_text_start_address(exec
, &stext
, nsi
);
688 for (i
= 0; i
< ntevs
&& ret
>= 0; i
++) {
689 /* point.address is the address of point.symbol + point.offset */
690 tevs
[i
].point
.address
-= stext
;
691 tevs
[i
].point
.module
= strdup(exec
);
692 if (!tevs
[i
].point
.module
) {
696 tevs
[i
].uprobes
= true;
703 post_process_module_probe_trace_events(struct probe_trace_event
*tevs
,
704 int ntevs
, const char *module
,
705 struct debuginfo
*dinfo
)
707 Dwarf_Addr text_offs
= 0;
709 char *mod_name
= NULL
;
715 map
= get_target_map(module
, NULL
, false);
716 if (!map
|| debuginfo__get_text_offset(dinfo
, &text_offs
, true) < 0) {
717 pr_warning("Failed to get ELF symbols for %s\n", module
);
721 mod_name
= find_module_name(module
);
722 for (i
= 0; i
< ntevs
; i
++) {
723 ret
= post_process_probe_trace_point(&tevs
[i
].point
,
724 map
, (unsigned long)text_offs
);
727 tevs
[i
].point
.module
=
728 strdup(mod_name
? mod_name
: module
);
729 if (!tevs
[i
].point
.module
) {
742 post_process_kernel_probe_trace_events(struct probe_trace_event
*tevs
,
745 struct ref_reloc_sym
*reloc_sym
;
749 /* Skip post process if the target is an offline kernel */
750 if (symbol_conf
.ignore_vmlinux_buildid
)
751 return post_process_offline_probe_trace_events(tevs
, ntevs
,
752 symbol_conf
.vmlinux_name
);
754 reloc_sym
= kernel_get_ref_reloc_sym();
756 pr_warning("Relocated base symbol is not found!\n");
760 for (i
= 0; i
< ntevs
; i
++) {
761 if (!tevs
[i
].point
.address
)
763 if (tevs
[i
].point
.retprobe
&& !kretprobe_offset_is_supported())
765 /* If we found a wrong one, mark it by NULL symbol */
766 if (kprobe_warn_out_range(tevs
[i
].point
.symbol
,
767 tevs
[i
].point
.address
)) {
771 tmp
= strdup(reloc_sym
->name
);
775 /* If we have no realname, use symbol for it */
776 if (!tevs
[i
].point
.realname
)
777 tevs
[i
].point
.realname
= tevs
[i
].point
.symbol
;
779 free(tevs
[i
].point
.symbol
);
780 tevs
[i
].point
.symbol
= tmp
;
781 tevs
[i
].point
.offset
= tevs
[i
].point
.address
-
782 reloc_sym
->unrelocated_addr
;
788 arch__post_process_probe_trace_events(struct perf_probe_event
*pev __maybe_unused
,
789 int ntevs __maybe_unused
)
793 /* Post processing the probe events */
794 static int post_process_probe_trace_events(struct perf_probe_event
*pev
,
795 struct probe_trace_event
*tevs
,
796 int ntevs
, const char *module
,
797 bool uprobe
, struct debuginfo
*dinfo
)
802 ret
= add_exec_to_probe_trace_events(tevs
, ntevs
, module
,
805 /* Currently ref_reloc_sym based probe is not for drivers */
806 ret
= post_process_module_probe_trace_events(tevs
, ntevs
,
809 ret
= post_process_kernel_probe_trace_events(tevs
, ntevs
);
812 arch__post_process_probe_trace_events(pev
, ntevs
);
817 /* Try to find perf_probe_event with debuginfo */
818 static int try_to_find_probe_trace_events(struct perf_probe_event
*pev
,
819 struct probe_trace_event
**tevs
)
821 bool need_dwarf
= perf_probe_event_need_dwarf(pev
);
822 struct perf_probe_point tmp
;
823 struct debuginfo
*dinfo
;
826 dinfo
= open_debuginfo(pev
->target
, pev
->nsi
, !need_dwarf
);
830 pr_debug("Could not open debuginfo. Try to use symbols.\n");
834 pr_debug("Try to find probe point from debuginfo.\n");
835 /* Searching trace events corresponding to a probe event */
836 ntevs
= debuginfo__find_trace_events(dinfo
, pev
, tevs
);
838 if (ntevs
== 0) { /* Not found, retry with an alternative */
839 ret
= get_alternative_probe_event(dinfo
, pev
, &tmp
);
841 ntevs
= debuginfo__find_trace_events(dinfo
, pev
, tevs
);
843 * Write back to the original probe_event for
844 * setting appropriate (user given) event name
846 clear_perf_probe_point(&pev
->point
);
847 memcpy(&pev
->point
, &tmp
, sizeof(tmp
));
851 if (ntevs
> 0) { /* Succeeded to find trace events */
852 pr_debug("Found %d probe_trace_events.\n", ntevs
);
853 ret
= post_process_probe_trace_events(pev
, *tevs
, ntevs
,
854 pev
->target
, pev
->uprobes
, dinfo
);
855 if (ret
< 0 || ret
== ntevs
) {
856 pr_debug("Post processing failed or all events are skipped. (%d)\n", ret
);
857 clear_probe_trace_events(*tevs
, ntevs
);
863 debuginfo__delete(dinfo
);
865 if (ntevs
== 0) { /* No error but failed to find probe point. */
866 pr_warning("Probe point '%s' not found.\n",
867 synthesize_perf_probe_point(&pev
->point
));
869 } else if (ntevs
< 0) {
870 /* Error path : ntevs < 0 */
871 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs
);
873 pr_warning("Warning: No dwarf info found in the vmlinux - "
874 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
876 pr_debug("Trying to use symbols.\n");
883 #define LINEBUF_SIZE 256
884 #define NR_ADDITIONAL_LINES 2
886 static int __show_one_line(FILE *fp
, int l
, bool skip
, bool show_num
)
888 char buf
[LINEBUF_SIZE
], sbuf
[STRERR_BUFSIZE
];
889 const char *color
= show_num
? "" : PERF_COLOR_BLUE
;
890 const char *prefix
= NULL
;
893 if (fgets(buf
, LINEBUF_SIZE
, fp
) == NULL
)
898 prefix
= show_num
? "%7d " : " ";
899 color_fprintf(stdout
, color
, prefix
, l
);
901 color_fprintf(stdout
, color
, "%s", buf
);
903 } while (strchr(buf
, '\n') == NULL
);
908 pr_warning("File read error: %s\n",
909 str_error_r(errno
, sbuf
, sizeof(sbuf
)));
915 static int _show_one_line(FILE *fp
, int l
, bool skip
, bool show_num
)
917 int rv
= __show_one_line(fp
, l
, skip
, show_num
);
919 pr_warning("Source file is shorter than expected.\n");
925 #define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
926 #define show_one_line(f,l) _show_one_line(f,l,false,false)
927 #define skip_one_line(f,l) _show_one_line(f,l,true,false)
928 #define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
931 * Show line-range always requires debuginfo to find source file and
934 static int __show_line_range(struct line_range
*lr
, const char *module
,
939 struct debuginfo
*dinfo
;
943 char sbuf
[STRERR_BUFSIZE
];
945 /* Search a line range */
946 dinfo
= open_debuginfo(module
, NULL
, false);
950 ret
= debuginfo__find_line_range(dinfo
, lr
);
951 if (!ret
) { /* Not found, retry with an alternative */
952 ret
= get_alternative_line_range(dinfo
, lr
, module
, user
);
954 ret
= debuginfo__find_line_range(dinfo
, lr
);
956 debuginfo__delete(dinfo
);
957 if (ret
== 0 || ret
== -ENOENT
) {
958 pr_warning("Specified source line is not found.\n");
960 } else if (ret
< 0) {
961 pr_warning("Debuginfo analysis failed.\n");
965 /* Convert source file path */
967 ret
= get_real_path(tmp
, lr
->comp_dir
, &lr
->path
);
969 /* Free old path when new path is assigned */
974 pr_warning("Failed to find source file path.\n");
981 fprintf(stdout
, "<%s@%s:%d>\n", lr
->function
, lr
->path
,
982 lr
->start
- lr
->offset
);
984 fprintf(stdout
, "<%s:%d>\n", lr
->path
, lr
->start
);
986 fp
= fopen(lr
->path
, "r");
988 pr_warning("Failed to open %s: %s\n", lr
->path
,
989 str_error_r(errno
, sbuf
, sizeof(sbuf
)));
992 /* Skip to starting line number */
993 while (l
< lr
->start
) {
994 ret
= skip_one_line(fp
, l
++);
999 intlist__for_each_entry(ln
, lr
->line_list
) {
1000 for (; ln
->i
> l
; l
++) {
1001 ret
= show_one_line(fp
, l
- lr
->offset
);
1005 ret
= show_one_line_with_num(fp
, l
++ - lr
->offset
);
1010 if (lr
->end
== INT_MAX
)
1011 lr
->end
= l
+ NR_ADDITIONAL_LINES
;
1012 while (l
<= lr
->end
) {
1013 ret
= show_one_line_or_eof(fp
, l
++ - lr
->offset
);
1022 int show_line_range(struct line_range
*lr
, const char *module
,
1023 struct nsinfo
*nsi
, bool user
)
1026 struct nscookie nsc
;
1028 ret
= init_probe_symbol_maps(user
);
1031 nsinfo__mountns_enter(nsi
, &nsc
);
1032 ret
= __show_line_range(lr
, module
, user
);
1033 nsinfo__mountns_exit(&nsc
);
1034 exit_probe_symbol_maps();
1039 static int show_available_vars_at(struct debuginfo
*dinfo
,
1040 struct perf_probe_event
*pev
,
1041 struct strfilter
*_filter
)
1045 struct str_node
*node
;
1046 struct variable_list
*vls
= NULL
, *vl
;
1047 struct perf_probe_point tmp
;
1050 buf
= synthesize_perf_probe_point(&pev
->point
);
1053 pr_debug("Searching variables at %s\n", buf
);
1055 ret
= debuginfo__find_available_vars_at(dinfo
, pev
, &vls
);
1056 if (!ret
) { /* Not found, retry with an alternative */
1057 ret
= get_alternative_probe_event(dinfo
, pev
, &tmp
);
1059 ret
= debuginfo__find_available_vars_at(dinfo
, pev
,
1061 /* Release the old probe_point */
1062 clear_perf_probe_point(&tmp
);
1066 if (ret
== 0 || ret
== -ENOENT
) {
1067 pr_err("Failed to find the address of %s\n", buf
);
1070 pr_warning("Debuginfo analysis failed.\n");
1074 /* Some variables are found */
1075 fprintf(stdout
, "Available variables at %s\n", buf
);
1076 for (i
= 0; i
< ret
; i
++) {
1079 * A probe point might be converted to
1080 * several trace points.
1082 fprintf(stdout
, "\t@<%s+%lu>\n", vl
->point
.symbol
,
1084 zfree(&vl
->point
.symbol
);
1087 strlist__for_each_entry(node
, vl
->vars
) {
1088 var
= strchr(node
->s
, '\t') + 1;
1089 if (strfilter__compare(_filter
, var
)) {
1090 fprintf(stdout
, "\t\t%s\n", node
->s
);
1094 strlist__delete(vl
->vars
);
1097 fprintf(stdout
, "\t\t(No matched variables)\n");
1105 /* Show available variables on given probe point */
1106 int show_available_vars(struct perf_probe_event
*pevs
, int npevs
,
1107 struct strfilter
*_filter
)
1110 struct debuginfo
*dinfo
;
1112 ret
= init_probe_symbol_maps(pevs
->uprobes
);
1116 dinfo
= open_debuginfo(pevs
->target
, pevs
->nsi
, false);
1124 for (i
= 0; i
< npevs
&& ret
>= 0; i
++)
1125 ret
= show_available_vars_at(dinfo
, &pevs
[i
], _filter
);
1127 debuginfo__delete(dinfo
);
1129 exit_probe_symbol_maps();
1133 #else /* !HAVE_DWARF_SUPPORT */
1135 static void debuginfo_cache__exit(void)
1140 find_perf_probe_point_from_dwarf(struct probe_trace_point
*tp __maybe_unused
,
1141 struct perf_probe_point
*pp __maybe_unused
,
1142 bool is_kprobe __maybe_unused
)
1147 static int try_to_find_probe_trace_events(struct perf_probe_event
*pev
,
1148 struct probe_trace_event
**tevs __maybe_unused
)
1150 if (perf_probe_event_need_dwarf(pev
)) {
1151 pr_warning("Debuginfo-analysis is not supported.\n");
1158 int show_line_range(struct line_range
*lr __maybe_unused
,
1159 const char *module __maybe_unused
,
1160 struct nsinfo
*nsi __maybe_unused
,
1161 bool user __maybe_unused
)
1163 pr_warning("Debuginfo-analysis is not supported.\n");
1167 int show_available_vars(struct perf_probe_event
*pevs __maybe_unused
,
1168 int npevs __maybe_unused
,
1169 struct strfilter
*filter __maybe_unused
)
1171 pr_warning("Debuginfo-analysis is not supported.\n");
1176 void line_range__clear(struct line_range
*lr
)
1182 intlist__delete(lr
->line_list
);
1183 memset(lr
, 0, sizeof(*lr
));
1186 int line_range__init(struct line_range
*lr
)
1188 memset(lr
, 0, sizeof(*lr
));
1189 lr
->line_list
= intlist__new(NULL
);
1196 static int parse_line_num(char **ptr
, int *val
, const char *what
)
1198 const char *start
= *ptr
;
1201 *val
= strtol(*ptr
, ptr
, 0);
1202 if (errno
|| *ptr
== start
) {
1203 semantic_error("'%s' is not a valid number.\n", what
);
1209 /* Check the name is good for event, group or function */
1210 static bool is_c_func_name(const char *name
)
1212 if (!isalpha(*name
) && *name
!= '_')
1214 while (*++name
!= '\0') {
1215 if (!isalpha(*name
) && !isdigit(*name
) && *name
!= '_')
1222 * Stuff 'lr' according to the line range described by 'arg'.
1223 * The line range syntax is described by:
1225 * SRC[:SLN[+NUM|-ELN]]
1226 * FNC[@SRC][:SLN[+NUM|-ELN]]
1228 int parse_line_range_desc(const char *arg
, struct line_range
*lr
)
1230 char *range
, *file
, *name
= strdup(arg
);
1239 range
= strchr(name
, ':');
1243 err
= parse_line_num(&range
, &lr
->start
, "start line");
1247 if (*range
== '+' || *range
== '-') {
1248 const char c
= *range
++;
1250 err
= parse_line_num(&range
, &lr
->end
, "end line");
1255 lr
->end
+= lr
->start
;
1257 * Adjust the number of lines here.
1258 * If the number of lines == 1, the
1259 * the end of line should be equal to
1260 * the start of line.
1266 pr_debug("Line range is %d to %d\n", lr
->start
, lr
->end
);
1269 if (lr
->start
> lr
->end
) {
1270 semantic_error("Start line must be smaller"
1271 " than end line.\n");
1274 if (*range
!= '\0') {
1275 semantic_error("Tailing with invalid str '%s'.\n", range
);
1280 file
= strchr(name
, '@');
1283 lr
->file
= strdup(++file
);
1284 if (lr
->file
== NULL
) {
1288 lr
->function
= name
;
1289 } else if (strchr(name
, '/') || strchr(name
, '.'))
1291 else if (is_c_func_name(name
))/* We reuse it for checking funcname */
1292 lr
->function
= name
;
1293 else { /* Invalid name */
1294 semantic_error("'%s' is not a valid function name.\n", name
);
1305 static int parse_perf_probe_event_name(char **arg
, struct perf_probe_event
*pev
)
1309 ptr
= strpbrk_esc(*arg
, ":");
1312 if (!pev
->sdt
&& !is_c_func_name(*arg
))
1314 pev
->group
= strdup_esc(*arg
);
1321 pev
->event
= strdup_esc(*arg
);
1322 if (pev
->event
== NULL
)
1325 if (!pev
->sdt
&& !is_c_func_name(pev
->event
)) {
1329 semantic_error("%s is bad for event name -it must "
1330 "follow C symbol-naming rule.\n", *arg
);
1336 /* Parse probepoint definition. */
1337 static int parse_perf_probe_point(char *arg
, struct perf_probe_event
*pev
)
1339 struct perf_probe_point
*pp
= &pev
->point
;
1342 bool file_spec
= false;
1347 * perf probe [GRP:][EVENT=]SRC[:LN|;PTN]
1348 * perf probe [GRP:][EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
1349 * perf probe %[GRP:]SDT_EVENT
1354 if (is_sdt_event(arg
)) {
1360 ptr
= strpbrk_esc(arg
, ";=@+%");
1364 semantic_error("%s must be an SDT name.\n",
1368 /* This must be a target file name or build id */
1369 tmp
= build_id_cache__complement(ptr
+ 1);
1371 pev
->target
= build_id_cache__origname(tmp
);
1374 pev
->target
= strdup_esc(ptr
+ 1);
1379 ret
= parse_perf_probe_event_name(&arg
, pev
);
1381 if (asprintf(&pev
->point
.function
, "%%%s", pev
->event
) < 0)
1387 if (ptr
&& *ptr
== '=') { /* Event name */
1390 ret
= parse_perf_probe_event_name(&arg
, pev
);
1398 * Check arg is function or file name and copy it.
1400 * We consider arg to be a file spec if and only if it satisfies
1401 * all of the below criteria::
1402 * - it does not include any of "+@%",
1403 * - it includes one of ":;", and
1404 * - it has a period '.' in the name.
1406 * Otherwise, we consider arg to be a function specification.
1408 if (!strpbrk_esc(arg
, "+@%")) {
1409 ptr
= strpbrk_esc(arg
, ";:");
1410 /* This is a file spec if it includes a '.' before ; or : */
1411 if (ptr
&& memchr(arg
, '.', ptr
- arg
))
1415 ptr
= strpbrk_esc(arg
, ";:+@%");
1424 tmp
= strdup_esc(arg
);
1435 * Keep pp->function even if this is absolute address,
1436 * so it can mark whether abs_address is valid.
1437 * Which make 'perf probe lib.bin 0x0' possible.
1439 * Note that checking length of tmp is not needed
1440 * because when we access tmp[1] we know tmp[0] is '0',
1441 * so tmp[1] should always valid (but could be '\0').
1443 if (tmp
&& !strncmp(tmp
, "0x", 2)) {
1444 pp
->abs_address
= strtoul(pp
->function
, &tmp
, 0);
1446 semantic_error("Invalid absolute address.\n");
1452 /* Parse other options */
1456 if (c
== ';') { /* Lazy pattern must be the last part */
1457 pp
->lazy_line
= strdup(arg
); /* let leave escapes */
1458 if (pp
->lazy_line
== NULL
)
1462 ptr
= strpbrk_esc(arg
, ";:+@%");
1468 case ':': /* Line number */
1469 pp
->line
= strtoul(arg
, &tmp
, 0);
1471 semantic_error("There is non-digit char"
1472 " in line number.\n");
1476 case '+': /* Byte offset from a symbol */
1477 pp
->offset
= strtoul(arg
, &tmp
, 0);
1479 semantic_error("There is non-digit character"
1484 case '@': /* File name */
1486 semantic_error("SRC@SRC is not allowed.\n");
1489 pp
->file
= strdup_esc(arg
);
1490 if (pp
->file
== NULL
)
1493 case '%': /* Probe places */
1494 if (strcmp(arg
, "return") == 0) {
1496 } else { /* Others not supported yet */
1497 semantic_error("%%%s is not supported.\n", arg
);
1501 default: /* Buggy case */
1502 pr_err("This program has a bug at %s:%d.\n",
1503 __FILE__
, __LINE__
);
1509 /* Exclusion check */
1510 if (pp
->lazy_line
&& pp
->line
) {
1511 semantic_error("Lazy pattern can't be used with"
1516 if (pp
->lazy_line
&& pp
->offset
) {
1517 semantic_error("Lazy pattern can't be used with offset.\n");
1521 if (pp
->line
&& pp
->offset
) {
1522 semantic_error("Offset can't be used with line number.\n");
1526 if (!pp
->line
&& !pp
->lazy_line
&& pp
->file
&& !pp
->function
) {
1527 semantic_error("File always requires line number or "
1532 if (pp
->offset
&& !pp
->function
) {
1533 semantic_error("Offset requires an entry function.\n");
1537 if ((pp
->offset
|| pp
->line
|| pp
->lazy_line
) && pp
->retprobe
) {
1538 semantic_error("Offset/Line/Lazy pattern can't be used with "
1543 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
1544 pp
->function
, pp
->file
, pp
->line
, pp
->offset
, pp
->retprobe
,
1549 /* Parse perf-probe event argument */
1550 static int parse_perf_probe_arg(char *str
, struct perf_probe_arg
*arg
)
1552 char *tmp
, *goodname
;
1553 struct perf_probe_arg_field
**fieldp
;
1555 pr_debug("parsing arg: %s into ", str
);
1557 tmp
= strchr(str
, '=');
1559 arg
->name
= strndup(str
, tmp
- str
);
1560 if (arg
->name
== NULL
)
1562 pr_debug("name:%s ", arg
->name
);
1566 tmp
= strchr(str
, ':');
1567 if (tmp
) { /* Type setting */
1569 arg
->type
= strdup(tmp
+ 1);
1570 if (arg
->type
== NULL
)
1572 pr_debug("type:%s ", arg
->type
);
1575 tmp
= strpbrk(str
, "-.[");
1576 if (!is_c_varname(str
) || !tmp
) {
1577 /* A variable, register, symbol or special value */
1578 arg
->var
= strdup(str
);
1579 if (arg
->var
== NULL
)
1581 pr_debug("%s\n", arg
->var
);
1585 /* Structure fields or array element */
1586 arg
->var
= strndup(str
, tmp
- str
);
1587 if (arg
->var
== NULL
)
1589 goodname
= arg
->var
;
1590 pr_debug("%s, ", arg
->var
);
1591 fieldp
= &arg
->field
;
1594 *fieldp
= zalloc(sizeof(struct perf_probe_arg_field
));
1595 if (*fieldp
== NULL
)
1597 if (*tmp
== '[') { /* Array */
1599 (*fieldp
)->index
= strtol(str
+ 1, &tmp
, 0);
1600 (*fieldp
)->ref
= true;
1601 if (*tmp
!= ']' || tmp
== str
+ 1) {
1602 semantic_error("Array index must be a"
1609 } else { /* Structure */
1612 (*fieldp
)->ref
= false;
1613 } else if (tmp
[1] == '>') {
1615 (*fieldp
)->ref
= true;
1617 semantic_error("Argument parse error: %s\n",
1621 tmp
= strpbrk(str
, "-.[");
1624 (*fieldp
)->name
= strndup(str
, tmp
- str
);
1625 if ((*fieldp
)->name
== NULL
)
1628 goodname
= (*fieldp
)->name
;
1629 pr_debug("%s(%d), ", (*fieldp
)->name
, (*fieldp
)->ref
);
1630 fieldp
= &(*fieldp
)->next
;
1633 (*fieldp
)->name
= strdup(str
);
1634 if ((*fieldp
)->name
== NULL
)
1637 goodname
= (*fieldp
)->name
;
1638 pr_debug("%s(%d)\n", (*fieldp
)->name
, (*fieldp
)->ref
);
1640 /* If no name is specified, set the last field name (not array index)*/
1642 arg
->name
= strdup(goodname
);
1643 if (arg
->name
== NULL
)
1649 /* Parse perf-probe event command */
1650 int parse_perf_probe_command(const char *cmd
, struct perf_probe_event
*pev
)
1653 int argc
, i
, ret
= 0;
1655 argv
= argv_split(cmd
, &argc
);
1657 pr_debug("Failed to split arguments.\n");
1660 if (argc
- 1 > MAX_PROBE_ARGS
) {
1661 semantic_error("Too many probe arguments (%d).\n", argc
- 1);
1665 /* Parse probe point */
1666 ret
= parse_perf_probe_point(argv
[0], pev
);
1670 /* Copy arguments and ensure return probe has no C argument */
1671 pev
->nargs
= argc
- 1;
1672 pev
->args
= zalloc(sizeof(struct perf_probe_arg
) * pev
->nargs
);
1673 if (pev
->args
== NULL
) {
1677 for (i
= 0; i
< pev
->nargs
&& ret
>= 0; i
++) {
1678 ret
= parse_perf_probe_arg(argv
[i
+ 1], &pev
->args
[i
]);
1680 is_c_varname(pev
->args
[i
].var
) && pev
->point
.retprobe
) {
1681 semantic_error("You can't specify local variable for"
1692 /* Returns true if *any* ARG is either C variable, $params or $vars. */
1693 bool perf_probe_with_var(struct perf_probe_event
*pev
)
1697 for (i
= 0; i
< pev
->nargs
; i
++)
1698 if (is_c_varname(pev
->args
[i
].var
) ||
1699 !strcmp(pev
->args
[i
].var
, PROBE_ARG_PARAMS
) ||
1700 !strcmp(pev
->args
[i
].var
, PROBE_ARG_VARS
))
1705 /* Return true if this perf_probe_event requires debuginfo */
1706 bool perf_probe_event_need_dwarf(struct perf_probe_event
*pev
)
1708 if (pev
->point
.file
|| pev
->point
.line
|| pev
->point
.lazy_line
)
1711 if (perf_probe_with_var(pev
))
1717 /* Parse probe_events event into struct probe_point */
1718 int parse_probe_trace_command(const char *cmd
, struct probe_trace_event
*tev
)
1720 struct probe_trace_point
*tp
= &tev
->point
;
1723 char *argv0_str
= NULL
, *fmt
, *fmt1_str
, *fmt2_str
, *fmt3_str
;
1727 pr_debug("Parsing probe_events: %s\n", cmd
);
1728 argv
= argv_split(cmd
, &argc
);
1730 pr_debug("Failed to split arguments.\n");
1734 semantic_error("Too few probe arguments.\n");
1739 /* Scan event and group name. */
1740 argv0_str
= strdup(argv
[0]);
1741 if (argv0_str
== NULL
) {
1745 fmt1_str
= strtok_r(argv0_str
, ":", &fmt
);
1746 fmt2_str
= strtok_r(NULL
, "/", &fmt
);
1747 fmt3_str
= strtok_r(NULL
, " \t", &fmt
);
1748 if (fmt1_str
== NULL
|| strlen(fmt1_str
) != 1 || fmt2_str
== NULL
1749 || fmt3_str
== NULL
) {
1750 semantic_error("Failed to parse event name: %s\n", argv
[0]);
1755 tev
->group
= strdup(fmt2_str
);
1756 tev
->event
= strdup(fmt3_str
);
1757 if (tev
->group
== NULL
|| tev
->event
== NULL
) {
1761 pr_debug("Group:%s Event:%s probe:%c\n", tev
->group
, tev
->event
, pr
);
1763 tp
->retprobe
= (pr
== 'r');
1765 /* Scan module name(if there), function name and offset */
1766 p
= strchr(argv
[1], ':');
1768 tp
->module
= strndup(argv
[1], p
- argv
[1]);
1773 tev
->uprobes
= (tp
->module
[0] == '/');
1777 fmt1_str
= strtok_r(p
, "+", &fmt
);
1778 /* only the address started with 0x */
1779 if (fmt1_str
[0] == '0') {
1781 * Fix a special case:
1782 * if address == 0, kernel reports something like:
1783 * p:probe_libc/abs_0 /lib/libc-2.18.so:0x (null) arg1=%ax
1784 * Newer kernel may fix that, but we want to
1785 * support old kernel also.
1787 if (strcmp(fmt1_str
, "0x") == 0) {
1788 if (!argv
[2] || strcmp(argv
[2], "(null)")) {
1795 for (i
= 2; argv
[i
+ 1] != NULL
; i
++)
1796 argv
[i
] = argv
[i
+ 1];
1801 tp
->address
= strtoul(fmt1_str
, NULL
, 0);
1803 /* Only the symbol-based probe has offset */
1804 tp
->symbol
= strdup(fmt1_str
);
1805 if (tp
->symbol
== NULL
) {
1809 fmt2_str
= strtok_r(NULL
, "", &fmt
);
1810 if (fmt2_str
== NULL
)
1813 tp
->offset
= strtoul(fmt2_str
, NULL
, 10);
1817 fmt2_str
= strchr(p
, '(');
1819 tp
->ref_ctr_offset
= strtoul(fmt2_str
+ 1, NULL
, 0);
1822 tev
->nargs
= argc
- 2;
1823 tev
->args
= zalloc(sizeof(struct probe_trace_arg
) * tev
->nargs
);
1824 if (tev
->args
== NULL
) {
1828 for (i
= 0; i
< tev
->nargs
; i
++) {
1829 p
= strchr(argv
[i
+ 2], '=');
1830 if (p
) /* We don't need which register is assigned. */
1834 tev
->args
[i
].name
= strdup(argv
[i
+ 2]);
1835 /* TODO: parse regs and offset */
1836 tev
->args
[i
].value
= strdup(p
);
1837 if (tev
->args
[i
].name
== NULL
|| tev
->args
[i
].value
== NULL
) {
1849 /* Compose only probe arg */
1850 char *synthesize_perf_probe_arg(struct perf_probe_arg
*pa
)
1852 struct perf_probe_arg_field
*field
= pa
->field
;
1857 if (strbuf_init(&buf
, 64) < 0)
1860 if (pa
->name
&& pa
->var
)
1861 err
= strbuf_addf(&buf
, "%s=%s", pa
->name
, pa
->var
);
1863 err
= strbuf_addstr(&buf
, pa
->name
?: pa
->var
);
1868 if (field
->name
[0] == '[')
1869 err
= strbuf_addstr(&buf
, field
->name
);
1871 err
= strbuf_addf(&buf
, "%s%s", field
->ref
? "->" : ".",
1873 field
= field
->next
;
1879 if (strbuf_addf(&buf
, ":%s", pa
->type
) < 0)
1882 ret
= strbuf_detach(&buf
, NULL
);
1884 strbuf_release(&buf
);
1888 /* Compose only probe point (not argument) */
1889 char *synthesize_perf_probe_point(struct perf_probe_point
*pp
)
1892 char *tmp
, *ret
= NULL
;
1895 if (strbuf_init(&buf
, 64) < 0)
1899 if (strbuf_addstr(&buf
, pp
->function
) < 0)
1902 err
= strbuf_addf(&buf
, "+%lu", pp
->offset
);
1904 err
= strbuf_addf(&buf
, ":%d", pp
->line
);
1905 else if (pp
->retprobe
)
1906 err
= strbuf_addstr(&buf
, "%return");
1914 tmp
= strchr(pp
->file
+ len
- 30, '/');
1915 tmp
= tmp
? tmp
+ 1 : pp
->file
+ len
- 30;
1917 err
= strbuf_addf(&buf
, "@%s", tmp
);
1918 if (!err
&& !pp
->function
&& pp
->line
)
1919 err
= strbuf_addf(&buf
, ":%d", pp
->line
);
1922 ret
= strbuf_detach(&buf
, NULL
);
1924 strbuf_release(&buf
);
1928 char *synthesize_perf_probe_command(struct perf_probe_event
*pev
)
1931 char *tmp
, *ret
= NULL
;
1934 if (strbuf_init(&buf
, 64))
1937 if (strbuf_addf(&buf
, "%s:%s=", pev
->group
?: PERFPROBE_GROUP
,
1941 tmp
= synthesize_perf_probe_point(&pev
->point
);
1942 if (!tmp
|| strbuf_addstr(&buf
, tmp
) < 0)
1946 for (i
= 0; i
< pev
->nargs
; i
++) {
1947 tmp
= synthesize_perf_probe_arg(pev
->args
+ i
);
1948 if (!tmp
|| strbuf_addf(&buf
, " %s", tmp
) < 0)
1953 ret
= strbuf_detach(&buf
, NULL
);
1955 strbuf_release(&buf
);
1959 static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref
*ref
,
1960 struct strbuf
*buf
, int depth
)
1964 depth
= __synthesize_probe_trace_arg_ref(ref
->next
, buf
,
1969 err
= strbuf_addf(buf
, "%+ld(", ref
->offset
);
1970 return (err
< 0) ? err
: depth
;
1973 static int synthesize_probe_trace_arg(struct probe_trace_arg
*arg
,
1976 struct probe_trace_arg_ref
*ref
= arg
->ref
;
1979 /* Argument name or separator */
1981 err
= strbuf_addf(buf
, " %s=", arg
->name
);
1983 err
= strbuf_addch(buf
, ' ');
1987 /* Special case: @XXX */
1988 if (arg
->value
[0] == '@' && arg
->ref
)
1991 /* Dereferencing arguments */
1993 depth
= __synthesize_probe_trace_arg_ref(ref
, buf
, 1);
1998 /* Print argument value */
1999 if (arg
->value
[0] == '@' && arg
->ref
)
2000 err
= strbuf_addf(buf
, "%s%+ld", arg
->value
, arg
->ref
->offset
);
2002 err
= strbuf_addstr(buf
, arg
->value
);
2005 while (!err
&& depth
--)
2006 err
= strbuf_addch(buf
, ')');
2008 /* Print argument type */
2009 if (!err
&& arg
->type
)
2010 err
= strbuf_addf(buf
, ":%s", arg
->type
);
2016 synthesize_uprobe_trace_def(struct probe_trace_event
*tev
, struct strbuf
*buf
)
2018 struct probe_trace_point
*tp
= &tev
->point
;
2021 err
= strbuf_addf(buf
, "%s:0x%lx", tp
->module
, tp
->address
);
2023 if (err
>= 0 && tp
->ref_ctr_offset
) {
2024 if (!uprobe_ref_ctr_is_supported())
2026 err
= strbuf_addf(buf
, "(0x%lx)", tp
->ref_ctr_offset
);
2028 return err
>= 0 ? 0 : -1;
2031 char *synthesize_probe_trace_command(struct probe_trace_event
*tev
)
2033 struct probe_trace_point
*tp
= &tev
->point
;
2038 /* Uprobes must have tp->module */
2039 if (tev
->uprobes
&& !tp
->module
)
2042 if (strbuf_init(&buf
, 32) < 0)
2045 if (strbuf_addf(&buf
, "%c:%s/%s ", tp
->retprobe
? 'r' : 'p',
2046 tev
->group
, tev
->event
) < 0)
2049 * If tp->address == 0, then this point must be a
2050 * absolute address uprobe.
2051 * try_to_find_absolute_address() should have made
2052 * tp->symbol to "0x0".
2054 if (tev
->uprobes
&& !tp
->address
) {
2055 if (!tp
->symbol
|| strcmp(tp
->symbol
, "0x0"))
2059 /* Use the tp->address for uprobes */
2061 err
= synthesize_uprobe_trace_def(tev
, &buf
);
2062 } else if (!strncmp(tp
->symbol
, "0x", 2)) {
2063 /* Absolute address. See try_to_find_absolute_address() */
2064 err
= strbuf_addf(&buf
, "%s%s0x%lx", tp
->module
?: "",
2065 tp
->module
? ":" : "", tp
->address
);
2067 err
= strbuf_addf(&buf
, "%s%s%s+%lu", tp
->module
?: "",
2068 tp
->module
? ":" : "", tp
->symbol
, tp
->offset
);
2074 for (i
= 0; i
< tev
->nargs
; i
++)
2075 if (synthesize_probe_trace_arg(&tev
->args
[i
], &buf
) < 0)
2078 ret
= strbuf_detach(&buf
, NULL
);
2080 strbuf_release(&buf
);
2084 static int find_perf_probe_point_from_map(struct probe_trace_point
*tp
,
2085 struct perf_probe_point
*pp
,
2088 struct symbol
*sym
= NULL
;
2089 struct map
*map
= NULL
;
2090 u64 addr
= tp
->address
;
2094 map
= dso__new_map(tp
->module
);
2097 sym
= map__find_symbol(map
, addr
);
2099 if (tp
->symbol
&& !addr
) {
2100 if (kernel_get_symbol_address_by_name(tp
->symbol
,
2101 &addr
, true, false) < 0)
2106 sym
= machine__find_kernel_symbol(host_machine
, addr
, &map
);
2113 pp
->retprobe
= tp
->retprobe
;
2114 pp
->offset
= addr
- map
->unmap_ip(map
, sym
->start
);
2115 pp
->function
= strdup(sym
->name
);
2116 ret
= pp
->function
? 0 : -ENOMEM
;
2119 if (map
&& !is_kprobe
) {
2126 static int convert_to_perf_probe_point(struct probe_trace_point
*tp
,
2127 struct perf_probe_point
*pp
,
2133 ret
= find_perf_probe_point_from_dwarf(tp
, pp
, is_kprobe
);
2136 ret
= find_perf_probe_point_from_map(tp
, pp
, is_kprobe
);
2140 pr_debug("Failed to find probe point from both of dwarf and map.\n");
2143 pp
->function
= strdup(tp
->symbol
);
2144 pp
->offset
= tp
->offset
;
2146 ret
= e_snprintf(buf
, 128, "0x%" PRIx64
, (u64
)tp
->address
);
2149 pp
->function
= strdup(buf
);
2152 if (pp
->function
== NULL
)
2155 pp
->retprobe
= tp
->retprobe
;
2160 static int convert_to_perf_probe_event(struct probe_trace_event
*tev
,
2161 struct perf_probe_event
*pev
, bool is_kprobe
)
2163 struct strbuf buf
= STRBUF_INIT
;
2166 /* Convert event/group name */
2167 pev
->event
= strdup(tev
->event
);
2168 pev
->group
= strdup(tev
->group
);
2169 if (pev
->event
== NULL
|| pev
->group
== NULL
)
2172 /* Convert trace_point to probe_point */
2173 ret
= convert_to_perf_probe_point(&tev
->point
, &pev
->point
, is_kprobe
);
2177 /* Convert trace_arg to probe_arg */
2178 pev
->nargs
= tev
->nargs
;
2179 pev
->args
= zalloc(sizeof(struct perf_probe_arg
) * pev
->nargs
);
2180 if (pev
->args
== NULL
)
2182 for (i
= 0; i
< tev
->nargs
&& ret
>= 0; i
++) {
2183 if (tev
->args
[i
].name
)
2184 pev
->args
[i
].name
= strdup(tev
->args
[i
].name
);
2186 if ((ret
= strbuf_init(&buf
, 32)) < 0)
2188 ret
= synthesize_probe_trace_arg(&tev
->args
[i
], &buf
);
2189 pev
->args
[i
].name
= strbuf_detach(&buf
, NULL
);
2191 if (pev
->args
[i
].name
== NULL
&& ret
>= 0)
2196 clear_perf_probe_event(pev
);
2201 void clear_perf_probe_event(struct perf_probe_event
*pev
)
2203 struct perf_probe_arg_field
*field
, *next
;
2209 clear_perf_probe_point(&pev
->point
);
2211 for (i
= 0; i
< pev
->nargs
; i
++) {
2212 free(pev
->args
[i
].name
);
2213 free(pev
->args
[i
].var
);
2214 free(pev
->args
[i
].type
);
2215 field
= pev
->args
[i
].field
;
2218 zfree(&field
->name
);
2224 memset(pev
, 0, sizeof(*pev
));
2227 #define strdup_or_goto(str, label) \
2228 ({ char *__p = NULL; if (str && !(__p = strdup(str))) goto label; __p; })
2230 static int perf_probe_point__copy(struct perf_probe_point
*dst
,
2231 struct perf_probe_point
*src
)
2233 dst
->file
= strdup_or_goto(src
->file
, out_err
);
2234 dst
->function
= strdup_or_goto(src
->function
, out_err
);
2235 dst
->lazy_line
= strdup_or_goto(src
->lazy_line
, out_err
);
2236 dst
->line
= src
->line
;
2237 dst
->retprobe
= src
->retprobe
;
2238 dst
->offset
= src
->offset
;
2242 clear_perf_probe_point(dst
);
2246 static int perf_probe_arg__copy(struct perf_probe_arg
*dst
,
2247 struct perf_probe_arg
*src
)
2249 struct perf_probe_arg_field
*field
, **ppfield
;
2251 dst
->name
= strdup_or_goto(src
->name
, out_err
);
2252 dst
->var
= strdup_or_goto(src
->var
, out_err
);
2253 dst
->type
= strdup_or_goto(src
->type
, out_err
);
2256 ppfield
= &(dst
->field
);
2258 *ppfield
= zalloc(sizeof(*field
));
2261 (*ppfield
)->name
= strdup_or_goto(field
->name
, out_err
);
2262 (*ppfield
)->index
= field
->index
;
2263 (*ppfield
)->ref
= field
->ref
;
2264 field
= field
->next
;
2265 ppfield
= &((*ppfield
)->next
);
2272 int perf_probe_event__copy(struct perf_probe_event
*dst
,
2273 struct perf_probe_event
*src
)
2277 dst
->event
= strdup_or_goto(src
->event
, out_err
);
2278 dst
->group
= strdup_or_goto(src
->group
, out_err
);
2279 dst
->target
= strdup_or_goto(src
->target
, out_err
);
2280 dst
->uprobes
= src
->uprobes
;
2282 if (perf_probe_point__copy(&dst
->point
, &src
->point
) < 0)
2285 dst
->args
= zalloc(sizeof(struct perf_probe_arg
) * src
->nargs
);
2288 dst
->nargs
= src
->nargs
;
2290 for (i
= 0; i
< src
->nargs
; i
++)
2291 if (perf_probe_arg__copy(&dst
->args
[i
], &src
->args
[i
]) < 0)
2296 clear_perf_probe_event(dst
);
2300 void clear_probe_trace_event(struct probe_trace_event
*tev
)
2302 struct probe_trace_arg_ref
*ref
, *next
;
2307 free(tev
->point
.symbol
);
2308 free(tev
->point
.realname
);
2309 free(tev
->point
.module
);
2310 for (i
= 0; i
< tev
->nargs
; i
++) {
2311 free(tev
->args
[i
].name
);
2312 free(tev
->args
[i
].value
);
2313 free(tev
->args
[i
].type
);
2314 ref
= tev
->args
[i
].ref
;
2322 memset(tev
, 0, sizeof(*tev
));
2325 struct kprobe_blacklist_node
{
2326 struct list_head list
;
2327 unsigned long start
;
2332 static void kprobe_blacklist__delete(struct list_head
*blacklist
)
2334 struct kprobe_blacklist_node
*node
;
2336 while (!list_empty(blacklist
)) {
2337 node
= list_first_entry(blacklist
,
2338 struct kprobe_blacklist_node
, list
);
2339 list_del(&node
->list
);
2345 static int kprobe_blacklist__load(struct list_head
*blacklist
)
2347 struct kprobe_blacklist_node
*node
;
2348 const char *__debugfs
= debugfs__mountpoint();
2349 char buf
[PATH_MAX
], *p
;
2353 if (__debugfs
== NULL
)
2356 ret
= e_snprintf(buf
, PATH_MAX
, "%s/kprobes/blacklist", __debugfs
);
2360 fp
= fopen(buf
, "r");
2365 while (fgets(buf
, PATH_MAX
, fp
)) {
2366 node
= zalloc(sizeof(*node
));
2371 INIT_LIST_HEAD(&node
->list
);
2372 list_add_tail(&node
->list
, blacklist
);
2373 if (sscanf(buf
, "0x%lx-0x%lx", &node
->start
, &node
->end
) != 2) {
2377 p
= strchr(buf
, '\t');
2380 if (p
[strlen(p
) - 1] == '\n')
2381 p
[strlen(p
) - 1] = '\0';
2383 p
= (char *)"unknown";
2384 node
->symbol
= strdup(p
);
2385 if (!node
->symbol
) {
2389 pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
2390 node
->start
, node
->end
, node
->symbol
);
2394 kprobe_blacklist__delete(blacklist
);
2400 static struct kprobe_blacklist_node
*
2401 kprobe_blacklist__find_by_address(struct list_head
*blacklist
,
2402 unsigned long address
)
2404 struct kprobe_blacklist_node
*node
;
2406 list_for_each_entry(node
, blacklist
, list
) {
2407 if (node
->start
<= address
&& address
< node
->end
)
2414 static LIST_HEAD(kprobe_blacklist
);
2416 static void kprobe_blacklist__init(void)
2418 if (!list_empty(&kprobe_blacklist
))
2421 if (kprobe_blacklist__load(&kprobe_blacklist
) < 0)
2422 pr_debug("No kprobe blacklist support, ignored\n");
2425 static void kprobe_blacklist__release(void)
2427 kprobe_blacklist__delete(&kprobe_blacklist
);
2430 static bool kprobe_blacklist__listed(unsigned long address
)
2432 return !!kprobe_blacklist__find_by_address(&kprobe_blacklist
, address
);
2435 static int perf_probe_event__sprintf(const char *group
, const char *event
,
2436 struct perf_probe_event
*pev
,
2438 struct strbuf
*result
)
2443 if (asprintf(&buf
, "%s:%s", group
, event
) < 0)
2445 ret
= strbuf_addf(result
, " %-20s (on ", buf
);
2450 /* Synthesize only event probe point */
2451 buf
= synthesize_perf_probe_point(&pev
->point
);
2454 ret
= strbuf_addstr(result
, buf
);
2458 ret
= strbuf_addf(result
, " in %s", module
);
2460 if (!ret
&& pev
->nargs
> 0) {
2461 ret
= strbuf_add(result
, " with", 5);
2462 for (i
= 0; !ret
&& i
< pev
->nargs
; i
++) {
2463 buf
= synthesize_perf_probe_arg(&pev
->args
[i
]);
2466 ret
= strbuf_addf(result
, " %s", buf
);
2471 ret
= strbuf_addch(result
, ')');
2477 int show_perf_probe_event(const char *group
, const char *event
,
2478 struct perf_probe_event
*pev
,
2479 const char *module
, bool use_stdout
)
2481 struct strbuf buf
= STRBUF_INIT
;
2484 ret
= perf_probe_event__sprintf(group
, event
, pev
, module
, &buf
);
2487 printf("%s\n", buf
.buf
);
2489 pr_info("%s\n", buf
.buf
);
2491 strbuf_release(&buf
);
2496 static bool filter_probe_trace_event(struct probe_trace_event
*tev
,
2497 struct strfilter
*filter
)
2501 /* At first, check the event name itself */
2502 if (strfilter__compare(filter
, tev
->event
))
2505 /* Next, check the combination of name and group */
2506 if (e_snprintf(tmp
, 128, "%s:%s", tev
->group
, tev
->event
) < 0)
2508 return strfilter__compare(filter
, tmp
);
2511 static int __show_perf_probe_events(int fd
, bool is_kprobe
,
2512 struct strfilter
*filter
)
2515 struct probe_trace_event tev
;
2516 struct perf_probe_event pev
;
2517 struct strlist
*rawlist
;
2518 struct str_node
*ent
;
2520 memset(&tev
, 0, sizeof(tev
));
2521 memset(&pev
, 0, sizeof(pev
));
2523 rawlist
= probe_file__get_rawlist(fd
);
2527 strlist__for_each_entry(ent
, rawlist
) {
2528 ret
= parse_probe_trace_command(ent
->s
, &tev
);
2530 if (!filter_probe_trace_event(&tev
, filter
))
2532 ret
= convert_to_perf_probe_event(&tev
, &pev
,
2536 ret
= show_perf_probe_event(pev
.group
, pev
.event
,
2537 &pev
, tev
.point
.module
,
2541 clear_perf_probe_event(&pev
);
2542 clear_probe_trace_event(&tev
);
2546 strlist__delete(rawlist
);
2547 /* Cleanup cached debuginfo if needed */
2548 debuginfo_cache__exit();
2553 /* List up current perf-probe events */
2554 int show_perf_probe_events(struct strfilter
*filter
)
2556 int kp_fd
, up_fd
, ret
;
2560 if (probe_conf
.cache
)
2561 return probe_cache__show_all_caches(filter
);
2563 ret
= init_probe_symbol_maps(false);
2567 ret
= probe_file__open_both(&kp_fd
, &up_fd
, 0);
2572 ret
= __show_perf_probe_events(kp_fd
, true, filter
);
2573 if (up_fd
>= 0 && ret
>= 0)
2574 ret
= __show_perf_probe_events(up_fd
, false, filter
);
2579 exit_probe_symbol_maps();
2584 static int get_new_event_name(char *buf
, size_t len
, const char *base
,
2585 struct strlist
*namelist
, bool ret_event
,
2593 nbase
= strdup(base
);
2597 /* Cut off the dot suffixes (e.g. .const, .isra) and version suffixes */
2598 p
= strpbrk(nbase
, ".@");
2599 if (p
&& p
!= nbase
)
2602 /* Try no suffix number */
2603 ret
= e_snprintf(buf
, len
, "%s%s", nbase
, ret_event
? "__return" : "");
2605 pr_debug("snprintf() failed: %d\n", ret
);
2608 if (!strlist__has_entry(namelist
, buf
))
2611 if (!allow_suffix
) {
2612 pr_warning("Error: event \"%s\" already exists.\n"
2613 " Hint: Remove existing event by 'perf probe -d'\n"
2614 " or force duplicates by 'perf probe -f'\n"
2615 " or set 'force=yes' in BPF source.\n",
2621 /* Try to add suffix */
2622 for (i
= 1; i
< MAX_EVENT_INDEX
; i
++) {
2623 ret
= e_snprintf(buf
, len
, "%s_%d", nbase
, i
);
2625 pr_debug("snprintf() failed: %d\n", ret
);
2628 if (!strlist__has_entry(namelist
, buf
))
2631 if (i
== MAX_EVENT_INDEX
) {
2632 pr_warning("Too many events are on the same function.\n");
2639 /* Final validation */
2640 if (ret
>= 0 && !is_c_func_name(buf
)) {
2641 pr_warning("Internal error: \"%s\" is an invalid event name.\n",
2649 /* Warn if the current kernel's uprobe implementation is old */
2650 static void warn_uprobe_event_compat(struct probe_trace_event
*tev
)
2653 char *buf
= synthesize_probe_trace_command(tev
);
2654 struct probe_trace_point
*tp
= &tev
->point
;
2656 if (tp
->ref_ctr_offset
&& !uprobe_ref_ctr_is_supported()) {
2657 pr_warning("A semaphore is associated with %s:%s and "
2658 "seems your kernel doesn't support it.\n",
2659 tev
->group
, tev
->event
);
2662 /* Old uprobe event doesn't support memory dereference */
2663 if (!tev
->uprobes
|| tev
->nargs
== 0 || !buf
)
2666 for (i
= 0; i
< tev
->nargs
; i
++)
2667 if (strglobmatch(tev
->args
[i
].value
, "[$@+-]*")) {
2668 pr_warning("Please upgrade your kernel to at least "
2669 "3.14 to have access to feature %s\n",
2670 tev
->args
[i
].value
);
2677 /* Set new name from original perf_probe_event and namelist */
2678 static int probe_trace_event__set_name(struct probe_trace_event
*tev
,
2679 struct perf_probe_event
*pev
,
2680 struct strlist
*namelist
,
2683 const char *event
, *group
;
2687 /* If probe_event or trace_event already have the name, reuse it */
2688 if (pev
->event
&& !pev
->sdt
)
2690 else if (tev
->event
)
2693 /* Or generate new one from probe point */
2694 if (pev
->point
.function
&&
2695 (strncmp(pev
->point
.function
, "0x", 2) != 0) &&
2696 !strisglob(pev
->point
.function
))
2697 event
= pev
->point
.function
;
2699 event
= tev
->point
.realname
;
2701 if (pev
->group
&& !pev
->sdt
)
2703 else if (tev
->group
)
2706 group
= PERFPROBE_GROUP
;
2708 /* Get an unused new event name */
2709 ret
= get_new_event_name(buf
, 64, event
, namelist
,
2710 tev
->point
.retprobe
, allow_suffix
);
2716 tev
->event
= strdup(event
);
2717 tev
->group
= strdup(group
);
2718 if (tev
->event
== NULL
|| tev
->group
== NULL
)
2721 /* Add added event name to namelist */
2722 strlist__add(namelist
, event
);
2726 static int __open_probe_file_and_namelist(bool uprobe
,
2727 struct strlist
**namelist
)
2731 fd
= probe_file__open(PF_FL_RW
| (uprobe
? PF_FL_UPROBE
: 0));
2735 /* Get current event names */
2736 *namelist
= probe_file__get_namelist(fd
);
2738 pr_debug("Failed to get current event list.\n");
2745 static int __add_probe_trace_events(struct perf_probe_event
*pev
,
2746 struct probe_trace_event
*tevs
,
2747 int ntevs
, bool allow_suffix
)
2749 int i
, fd
[2] = {-1, -1}, up
, ret
;
2750 struct probe_trace_event
*tev
= NULL
;
2751 struct probe_cache
*cache
= NULL
;
2752 struct strlist
*namelist
[2] = {NULL
, NULL
};
2753 struct nscookie nsc
;
2755 up
= pev
->uprobes
? 1 : 0;
2756 fd
[up
] = __open_probe_file_and_namelist(up
, &namelist
[up
]);
2761 for (i
= 0; i
< ntevs
; i
++) {
2763 up
= tev
->uprobes
? 1 : 0;
2764 if (fd
[up
] == -1) { /* Open the kprobe/uprobe_events */
2765 fd
[up
] = __open_probe_file_and_namelist(up
,
2770 /* Skip if the symbol is out of .text or blacklisted */
2771 if (!tev
->point
.symbol
&& !pev
->uprobes
)
2774 /* Set new name for tev (and update namelist) */
2775 ret
= probe_trace_event__set_name(tev
, pev
, namelist
[up
],
2780 nsinfo__mountns_enter(pev
->nsi
, &nsc
);
2781 ret
= probe_file__add_event(fd
[up
], tev
);
2782 nsinfo__mountns_exit(&nsc
);
2787 * Probes after the first probe which comes from same
2788 * user input are always allowed to add suffix, because
2789 * there might be several addresses corresponding to
2792 allow_suffix
= true;
2794 if (ret
== -EINVAL
&& pev
->uprobes
)
2795 warn_uprobe_event_compat(tev
);
2796 if (ret
== 0 && probe_conf
.cache
) {
2797 cache
= probe_cache__new(pev
->target
, pev
->nsi
);
2799 probe_cache__add_entry(cache
, pev
, tevs
, ntevs
) < 0 ||
2800 probe_cache__commit(cache
) < 0)
2801 pr_warning("Failed to add event to probe cache\n");
2802 probe_cache__delete(cache
);
2806 for (up
= 0; up
< 2; up
++) {
2807 strlist__delete(namelist
[up
]);
2814 static int find_probe_functions(struct map
*map
, char *name
,
2815 struct symbol
**syms
)
2819 struct rb_node
*tmp
;
2820 const char *norm
, *ver
;
2822 bool cut_version
= true;
2824 if (map__load(map
) < 0)
2827 /* If user gives a version, don't cut off the version from symbols */
2828 if (strchr(name
, '@'))
2829 cut_version
= false;
2831 map__for_each_symbol(map
, sym
, tmp
) {
2832 norm
= arch__normalize_symbol_name(sym
->name
);
2837 /* We don't care about default symbol or not */
2838 ver
= strchr(norm
, '@');
2840 buf
= strndup(norm
, ver
- norm
);
2847 if (strglobmatch(norm
, name
)) {
2849 if (syms
&& found
< probe_conf
.max_probes
)
2850 syms
[found
- 1] = sym
;
2859 void __weak
arch__fix_tev_from_maps(struct perf_probe_event
*pev __maybe_unused
,
2860 struct probe_trace_event
*tev __maybe_unused
,
2861 struct map
*map __maybe_unused
,
2862 struct symbol
*sym __maybe_unused
) { }
2865 * Find probe function addresses from map.
2866 * Return an error or the number of found probe_trace_event
2868 static int find_probe_trace_events_from_map(struct perf_probe_event
*pev
,
2869 struct probe_trace_event
**tevs
)
2871 struct map
*map
= NULL
;
2872 struct ref_reloc_sym
*reloc_sym
= NULL
;
2874 struct symbol
**syms
= NULL
;
2875 struct probe_trace_event
*tev
;
2876 struct perf_probe_point
*pp
= &pev
->point
;
2877 struct probe_trace_point
*tp
;
2878 int num_matched_functions
;
2879 int ret
, i
, j
, skipped
= 0;
2882 map
= get_target_map(pev
->target
, pev
->nsi
, pev
->uprobes
);
2888 syms
= malloc(sizeof(struct symbol
*) * probe_conf
.max_probes
);
2895 * Load matched symbols: Since the different local symbols may have
2896 * same name but different addresses, this lists all the symbols.
2898 num_matched_functions
= find_probe_functions(map
, pp
->function
, syms
);
2899 if (num_matched_functions
<= 0) {
2900 pr_err("Failed to find symbol %s in %s\n", pp
->function
,
2901 pev
->target
? : "kernel");
2904 } else if (num_matched_functions
> probe_conf
.max_probes
) {
2905 pr_err("Too many functions matched in %s\n",
2906 pev
->target
? : "kernel");
2911 /* Note that the symbols in the kmodule are not relocated */
2912 if (!pev
->uprobes
&& !pev
->target
&&
2913 (!pp
->retprobe
|| kretprobe_offset_is_supported())) {
2914 reloc_sym
= kernel_get_ref_reloc_sym();
2916 pr_warning("Relocated base symbol is not found!\n");
2922 /* Setup result trace-probe-events */
2923 *tevs
= zalloc(sizeof(*tev
) * num_matched_functions
);
2931 for (j
= 0; j
< num_matched_functions
; j
++) {
2934 tev
= (*tevs
) + ret
;
2936 if (ret
== num_matched_functions
) {
2937 pr_warning("Too many symbols are listed. Skip it.\n");
2942 if (pp
->offset
> sym
->end
- sym
->start
) {
2943 pr_warning("Offset %ld is bigger than the size of %s\n",
2944 pp
->offset
, sym
->name
);
2948 /* Add one probe point */
2949 tp
->address
= map
->unmap_ip(map
, sym
->start
) + pp
->offset
;
2951 /* Check the kprobe (not in module) is within .text */
2952 if (!pev
->uprobes
&& !pev
->target
&&
2953 kprobe_warn_out_range(sym
->name
, tp
->address
)) {
2954 tp
->symbol
= NULL
; /* Skip it */
2956 } else if (reloc_sym
) {
2957 tp
->symbol
= strdup_or_goto(reloc_sym
->name
, nomem_out
);
2958 tp
->offset
= tp
->address
- reloc_sym
->addr
;
2960 tp
->symbol
= strdup_or_goto(sym
->name
, nomem_out
);
2961 tp
->offset
= pp
->offset
;
2963 tp
->realname
= strdup_or_goto(sym
->name
, nomem_out
);
2965 tp
->retprobe
= pp
->retprobe
;
2968 tev
->point
.module
= strdup_or_goto(pev
->target
,
2971 mod_name
= find_module_name(pev
->target
);
2973 strdup(mod_name
? mod_name
: pev
->target
);
2975 if (!tev
->point
.module
)
2979 tev
->uprobes
= pev
->uprobes
;
2980 tev
->nargs
= pev
->nargs
;
2982 tev
->args
= zalloc(sizeof(struct probe_trace_arg
) *
2984 if (tev
->args
== NULL
)
2987 for (i
= 0; i
< tev
->nargs
; i
++) {
2988 if (pev
->args
[i
].name
)
2990 strdup_or_goto(pev
->args
[i
].name
,
2993 tev
->args
[i
].value
= strdup_or_goto(pev
->args
[i
].var
,
2995 if (pev
->args
[i
].type
)
2997 strdup_or_goto(pev
->args
[i
].type
,
3000 arch__fix_tev_from_maps(pev
, tev
, map
, sym
);
3002 if (ret
== skipped
) {
3015 clear_probe_trace_events(*tevs
, num_matched_functions
);
3020 static int try_to_find_absolute_address(struct perf_probe_event
*pev
,
3021 struct probe_trace_event
**tevs
)
3023 struct perf_probe_point
*pp
= &pev
->point
;
3024 struct probe_trace_event
*tev
;
3025 struct probe_trace_point
*tp
;
3028 if (!(pev
->point
.function
&& !strncmp(pev
->point
.function
, "0x", 2)))
3030 if (perf_probe_event_need_dwarf(pev
))
3034 * This is 'perf probe /lib/libc.so 0xabcd'. Try to probe at
3037 * Only one tev can be generated by this.
3039 *tevs
= zalloc(sizeof(*tev
));
3047 * Don't use tp->offset, use address directly, because
3048 * in synthesize_probe_trace_command() address cannot be
3051 tp
->address
= pev
->point
.abs_address
;
3052 tp
->retprobe
= pp
->retprobe
;
3053 tev
->uprobes
= pev
->uprobes
;
3057 * Give it a '0x' leading symbol name.
3058 * In __add_probe_trace_events, a NULL symbol is interpreted as
3061 if (asprintf(&tp
->symbol
, "0x%lx", tp
->address
) < 0)
3064 /* For kprobe, check range */
3065 if ((!tev
->uprobes
) &&
3066 (kprobe_warn_out_range(tev
->point
.symbol
,
3067 tev
->point
.address
))) {
3072 if (asprintf(&tp
->realname
, "abs_%lx", tp
->address
) < 0)
3076 tp
->module
= strdup(pev
->target
);
3082 tev
->group
= strdup(pev
->group
);
3088 tev
->event
= strdup(pev
->event
);
3093 tev
->nargs
= pev
->nargs
;
3094 tev
->args
= zalloc(sizeof(struct probe_trace_arg
) * tev
->nargs
);
3098 for (i
= 0; i
< tev
->nargs
; i
++)
3099 copy_to_probe_trace_arg(&tev
->args
[i
], &pev
->args
[i
]);
3104 clear_probe_trace_events(*tevs
, 1);
3109 /* Concatinate two arrays */
3110 static void *memcat(void *a
, size_t sz_a
, void *b
, size_t sz_b
)
3114 ret
= malloc(sz_a
+ sz_b
);
3116 memcpy(ret
, a
, sz_a
);
3117 memcpy(ret
+ sz_a
, b
, sz_b
);
3123 concat_probe_trace_events(struct probe_trace_event
**tevs
, int *ntevs
,
3124 struct probe_trace_event
**tevs2
, int ntevs2
)
3126 struct probe_trace_event
*new_tevs
;
3136 if (*ntevs
+ ntevs2
> probe_conf
.max_probes
)
3139 /* Concatinate the array of probe_trace_event */
3140 new_tevs
= memcat(*tevs
, (*ntevs
) * sizeof(**tevs
),
3141 *tevs2
, ntevs2
* sizeof(**tevs2
));
3151 clear_probe_trace_events(*tevs2
, ntevs2
);
3158 * Try to find probe_trace_event from given probe caches. Return the number
3159 * of cached events found, if an error occurs return the error.
3161 static int find_cached_events(struct perf_probe_event
*pev
,
3162 struct probe_trace_event
**tevs
,
3165 struct probe_cache
*cache
;
3166 struct probe_cache_entry
*entry
;
3167 struct probe_trace_event
*tmp_tevs
= NULL
;
3171 cache
= probe_cache__new(target
, pev
->nsi
);
3172 /* Return 0 ("not found") if the target has no probe cache. */
3176 for_each_probe_cache_entry(entry
, cache
) {
3177 /* Skip the cache entry which has no name */
3178 if (!entry
->pev
.event
|| !entry
->pev
.group
)
3180 if ((!pev
->group
|| strglobmatch(entry
->pev
.group
, pev
->group
)) &&
3181 strglobmatch(entry
->pev
.event
, pev
->event
)) {
3182 ret
= probe_cache_entry__get_event(entry
, &tmp_tevs
);
3184 ret
= concat_probe_trace_events(tevs
, &ntevs
,
3190 probe_cache__delete(cache
);
3192 clear_probe_trace_events(*tevs
, ntevs
);
3196 if (ntevs
> 0 && target
&& target
[0] == '/')
3197 pev
->uprobes
= true;
3203 /* Try to find probe_trace_event from all probe caches */
3204 static int find_cached_events_all(struct perf_probe_event
*pev
,
3205 struct probe_trace_event
**tevs
)
3207 struct probe_trace_event
*tmp_tevs
= NULL
;
3208 struct strlist
*bidlist
;
3209 struct str_node
*nd
;
3214 /* Get the buildid list of all valid caches */
3215 bidlist
= build_id_cache__list_all(true);
3218 pr_debug("Failed to get buildids: %d\n", ret
);
3223 strlist__for_each_entry(nd
, bidlist
) {
3224 pathname
= build_id_cache__origname(nd
->s
);
3225 ret
= find_cached_events(pev
, &tmp_tevs
, pathname
);
3226 /* In the case of cnt == 0, we just skip it */
3228 ret
= concat_probe_trace_events(tevs
, &ntevs
,
3234 strlist__delete(bidlist
);
3237 clear_probe_trace_events(*tevs
, ntevs
);
3245 static int find_probe_trace_events_from_cache(struct perf_probe_event
*pev
,
3246 struct probe_trace_event
**tevs
)
3248 struct probe_cache
*cache
;
3249 struct probe_cache_entry
*entry
;
3250 struct probe_trace_event
*tev
;
3251 struct str_node
*node
;
3255 /* For SDT/cached events, we use special search functions */
3257 return find_cached_events_all(pev
, tevs
);
3259 return find_cached_events(pev
, tevs
, pev
->target
);
3261 cache
= probe_cache__new(pev
->target
, pev
->nsi
);
3265 entry
= probe_cache__find(cache
, pev
);
3267 /* SDT must be in the cache */
3268 ret
= pev
->sdt
? -ENOENT
: 0;
3272 ret
= strlist__nr_entries(entry
->tevlist
);
3273 if (ret
> probe_conf
.max_probes
) {
3274 pr_debug("Too many entries matched in the cache of %s\n",
3275 pev
->target
? : "kernel");
3280 *tevs
= zalloc(ret
* sizeof(*tev
));
3287 strlist__for_each_entry(node
, entry
->tevlist
) {
3288 tev
= &(*tevs
)[i
++];
3289 ret
= parse_probe_trace_command(node
->s
, tev
);
3292 /* Set the uprobes attribute as same as original */
3293 tev
->uprobes
= pev
->uprobes
;
3298 probe_cache__delete(cache
);
3302 static int convert_to_probe_trace_events(struct perf_probe_event
*pev
,
3303 struct probe_trace_event
**tevs
)
3307 if (!pev
->group
&& !pev
->sdt
) {
3308 /* Set group name if not given */
3309 if (!pev
->uprobes
) {
3310 pev
->group
= strdup(PERFPROBE_GROUP
);
3311 ret
= pev
->group
? 0 : -ENOMEM
;
3313 ret
= convert_exec_to_group(pev
->target
, &pev
->group
);
3315 pr_warning("Failed to make a group name.\n");
3320 ret
= try_to_find_absolute_address(pev
, tevs
);
3324 /* At first, we need to lookup cache entry */
3325 ret
= find_probe_trace_events_from_cache(pev
, tevs
);
3326 if (ret
> 0 || pev
->sdt
) /* SDT can be found only in the cache */
3327 return ret
== 0 ? -ENOENT
: ret
; /* Found in probe cache */
3329 /* Convert perf_probe_event with debuginfo */
3330 ret
= try_to_find_probe_trace_events(pev
, tevs
);
3332 return ret
; /* Found in debuginfo or got an error */
3334 return find_probe_trace_events_from_map(pev
, tevs
);
3337 int convert_perf_probe_events(struct perf_probe_event
*pevs
, int npevs
)
3341 /* Loop 1: convert all events */
3342 for (i
= 0; i
< npevs
; i
++) {
3343 /* Init kprobe blacklist if needed */
3344 if (!pevs
[i
].uprobes
)
3345 kprobe_blacklist__init();
3346 /* Convert with or without debuginfo */
3347 ret
= convert_to_probe_trace_events(&pevs
[i
], &pevs
[i
].tevs
);
3350 pevs
[i
].ntevs
= ret
;
3352 /* This just release blacklist only if allocated */
3353 kprobe_blacklist__release();
3358 static int show_probe_trace_event(struct probe_trace_event
*tev
)
3360 char *buf
= synthesize_probe_trace_command(tev
);
3363 pr_debug("Failed to synthesize probe trace event.\n");
3367 /* Showing definition always go stdout */
3368 printf("%s\n", buf
);
3374 int show_probe_trace_events(struct perf_probe_event
*pevs
, int npevs
)
3376 struct strlist
*namelist
= strlist__new(NULL
, NULL
);
3377 struct probe_trace_event
*tev
;
3378 struct perf_probe_event
*pev
;
3384 for (j
= 0; j
< npevs
&& !ret
; j
++) {
3386 for (i
= 0; i
< pev
->ntevs
&& !ret
; i
++) {
3387 tev
= &pev
->tevs
[i
];
3388 /* Skip if the symbol is out of .text or blacklisted */
3389 if (!tev
->point
.symbol
&& !pev
->uprobes
)
3392 /* Set new name for tev (and update namelist) */
3393 ret
= probe_trace_event__set_name(tev
, pev
,
3396 ret
= show_probe_trace_event(tev
);
3399 strlist__delete(namelist
);
3404 int apply_perf_probe_events(struct perf_probe_event
*pevs
, int npevs
)
3408 /* Loop 2: add all events */
3409 for (i
= 0; i
< npevs
; i
++) {
3410 ret
= __add_probe_trace_events(&pevs
[i
], pevs
[i
].tevs
,
3412 probe_conf
.force_add
);
3419 void cleanup_perf_probe_events(struct perf_probe_event
*pevs
, int npevs
)
3422 struct perf_probe_event
*pev
;
3424 /* Loop 3: cleanup and free trace events */
3425 for (i
= 0; i
< npevs
; i
++) {
3427 for (j
= 0; j
< pevs
[i
].ntevs
; j
++)
3428 clear_probe_trace_event(&pevs
[i
].tevs
[j
]);
3429 zfree(&pevs
[i
].tevs
);
3431 nsinfo__zput(pev
->nsi
);
3432 clear_perf_probe_event(&pevs
[i
]);
3436 int add_perf_probe_events(struct perf_probe_event
*pevs
, int npevs
)
3440 ret
= init_probe_symbol_maps(pevs
->uprobes
);
3444 ret
= convert_perf_probe_events(pevs
, npevs
);
3446 ret
= apply_perf_probe_events(pevs
, npevs
);
3448 cleanup_perf_probe_events(pevs
, npevs
);
3450 exit_probe_symbol_maps();
3454 int del_perf_probe_events(struct strfilter
*filter
)
3456 int ret
, ret2
, ufd
= -1, kfd
= -1;
3457 char *str
= strfilter__string(filter
);
3462 /* Get current event names */
3463 ret
= probe_file__open_both(&kfd
, &ufd
, PF_FL_RW
);
3467 ret
= probe_file__del_events(kfd
, filter
);
3468 if (ret
< 0 && ret
!= -ENOENT
)
3471 ret2
= probe_file__del_events(ufd
, filter
);
3472 if (ret2
< 0 && ret2
!= -ENOENT
) {
3489 int show_available_funcs(const char *target
, struct nsinfo
*nsi
,
3490 struct strfilter
*_filter
, bool user
)
3496 ret
= init_probe_symbol_maps(user
);
3500 /* Get a symbol map */
3501 map
= get_target_map(target
, nsi
, user
);
3503 pr_err("Failed to get a map for %s\n", (target
) ? : "kernel");
3507 ret
= map__load(map
);
3510 char *str
= strfilter__string(_filter
);
3511 pr_err("Failed to find symbols matched to \"%s\"\n",
3515 pr_err("Failed to load symbols in %s\n",
3516 (target
) ? : "kernel");
3519 if (!dso__sorted_by_name(map
->dso
))
3520 dso__sort_by_name(map
->dso
);
3522 /* Show all (filtered) symbols */
3525 for (nd
= rb_first_cached(&map
->dso
->symbol_names
); nd
;
3527 struct symbol_name_rb_node
*pos
= rb_entry(nd
, struct symbol_name_rb_node
, rb_node
);
3529 if (strfilter__compare(_filter
, pos
->sym
.name
))
3530 printf("%s\n", pos
->sym
.name
);
3534 exit_probe_symbol_maps();
3539 int copy_to_probe_trace_arg(struct probe_trace_arg
*tvar
,
3540 struct perf_probe_arg
*pvar
)
3542 tvar
->value
= strdup(pvar
->var
);
3543 if (tvar
->value
== NULL
)
3546 tvar
->type
= strdup(pvar
->type
);
3547 if (tvar
->type
== NULL
)
3551 tvar
->name
= strdup(pvar
->name
);
3552 if (tvar
->name
== NULL
)