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"
33 #include <api/fs/fs.h>
34 #include "trace-event.h" /* For __maybe_unused */
35 #include "probe-event.h"
36 #include "probe-finder.h"
37 #include "probe-file.h"
42 #include <subcmd/pager.h>
43 #include <linux/ctype.h>
44 #include <linux/zalloc.h>
46 #define PERFPROBE_GROUP "probe"
48 bool probe_event_dry_run
; /* Dry run flag */
49 struct probe_conf probe_conf
= { .magic_num
= DEFAULT_PROBE_MAGIC_NUM
};
51 #define semantic_error(msg ...) pr_err("Semantic error :" msg)
53 int e_snprintf(char *str
, size_t size
, const char *format
, ...)
58 ret
= vsnprintf(str
, size
, format
, ap
);
65 static struct machine
*host_machine
;
67 /* Initialize symbol maps and path of vmlinux/modules */
68 int init_probe_symbol_maps(bool user_only
)
72 symbol_conf
.sort_by_name
= true;
73 symbol_conf
.allow_aliases
= true;
74 ret
= symbol__init(NULL
);
76 pr_debug("Failed to init symbol map.\n");
80 if (host_machine
|| user_only
) /* already initialized */
83 if (symbol_conf
.vmlinux_name
)
84 pr_debug("Use vmlinux: %s\n", symbol_conf
.vmlinux_name
);
86 host_machine
= machine__new_host();
88 pr_debug("machine__new_host() failed.\n");
94 pr_warning("Failed to init vmlinux path.\n");
98 void exit_probe_symbol_maps(void)
100 machine__delete(host_machine
);
105 static struct ref_reloc_sym
*kernel_get_ref_reloc_sym(struct map
**pmap
)
107 /* kmap->ref_reloc_sym should be set if host_machine is initialized */
109 struct map
*map
= machine__kernel_map(host_machine
);
111 if (map__load(map
) < 0)
114 kmap
= map__kmap(map
);
121 return kmap
->ref_reloc_sym
;
124 static int kernel_get_symbol_address_by_name(const char *name
, u64
*addr
,
125 bool reloc
, bool reladdr
)
127 struct ref_reloc_sym
*reloc_sym
;
131 /* ref_reloc_sym is just a label. Need a special fix*/
132 reloc_sym
= kernel_get_ref_reloc_sym(NULL
);
133 if (reloc_sym
&& strcmp(name
, reloc_sym
->name
) == 0)
134 *addr
= (reloc
) ? reloc_sym
->addr
: reloc_sym
->unrelocated_addr
;
136 sym
= machine__find_kernel_symbol_by_name(host_machine
, name
, &map
);
139 *addr
= map
->unmap_ip(map
, sym
->start
) -
140 ((reloc
) ? 0 : map
->reloc
) -
141 ((reladdr
) ? map
->start
: 0);
146 static struct map
*kernel_get_module_map(const char *module
)
148 struct maps
*maps
= machine__kernel_maps(host_machine
);
151 /* A file path -- this is an offline module */
152 if (module
&& strchr(module
, '/'))
153 return dso__new_map(module
);
156 pos
= machine__kernel_map(host_machine
);
157 return map__get(pos
);
160 maps__for_each_entry(maps
, pos
) {
161 /* short_name is "[module]" */
162 if (strncmp(pos
->dso
->short_name
+ 1, module
,
163 pos
->dso
->short_name_len
- 2) == 0 &&
164 module
[pos
->dso
->short_name_len
- 2] == '\0') {
165 return map__get(pos
);
171 struct map
*get_target_map(const char *target
, struct nsinfo
*nsi
, bool user
)
173 /* Init maps of given executable or kernel */
177 map
= dso__new_map(target
);
179 map
->dso
->nsinfo
= nsinfo__get(nsi
);
182 return kernel_get_module_map(target
);
186 static int convert_exec_to_group(const char *exec
, char **result
)
188 char *ptr1
, *ptr2
, *exec_copy
;
192 exec_copy
= strdup(exec
);
196 ptr1
= basename(exec_copy
);
202 for (ptr2
= ptr1
; *ptr2
!= '\0'; ptr2
++) {
203 if (!isalnum(*ptr2
) && *ptr2
!= '_') {
209 ret
= e_snprintf(buf
, 64, "%s_%s", PERFPROBE_GROUP
, ptr1
);
213 *result
= strdup(buf
);
214 ret
= *result
? 0 : -ENOMEM
;
221 static void clear_perf_probe_point(struct perf_probe_point
*pp
)
224 zfree(&pp
->function
);
225 zfree(&pp
->lazy_line
);
228 static void clear_probe_trace_events(struct probe_trace_event
*tevs
, int ntevs
)
232 for (i
= 0; i
< ntevs
; i
++)
233 clear_probe_trace_event(tevs
+ i
);
236 static bool kprobe_blacklist__listed(unsigned long address
);
237 static bool kprobe_warn_out_range(const char *symbol
, unsigned long address
)
242 map
= kernel_get_module_map(NULL
);
244 ret
= address
<= map
->start
|| map
->end
< address
;
246 pr_warning("%s is out of .text, skip it.\n", symbol
);
249 if (!ret
&& kprobe_blacklist__listed(address
)) {
250 pr_warning("%s is blacklisted function, skip it.\n", symbol
);
258 * @module can be module name of module file path. In case of path,
259 * inspect elf and find out what is actual module name.
260 * Caller has to free mod_name after using it.
262 static char *find_module_name(const char *module
)
270 char *mod_name
= NULL
;
273 fd
= open(module
, O_RDONLY
);
277 elf
= elf_begin(fd
, PERF_ELF_C_READ_MMAP
, NULL
);
281 if (gelf_getehdr(elf
, &ehdr
) == NULL
)
284 sec
= elf_section_by_name(elf
, &ehdr
, &shdr
,
285 ".gnu.linkonce.this_module", NULL
);
289 data
= elf_getdata(sec
, NULL
);
290 if (!data
|| !data
->d_buf
)
295 * '.gnu.linkonce.this_module' section of kernel module elf directly
296 * maps to 'struct module' from linux/module.h. This section contains
297 * actual module name which will be used by kernel after loading it.
298 * But, we cannot use 'struct module' here since linux/module.h is not
299 * exposed to user-space. Offset of 'name' has remained same from long
300 * time, so hardcoding it here.
302 if (ehdr
.e_ident
[EI_CLASS
] == ELFCLASS32
)
304 else /* expect ELFCLASS64 by default */
307 mod_name
= strdup((char *)data
->d_buf
+ name_offset
);
316 #ifdef HAVE_DWARF_SUPPORT
318 static int kernel_get_module_dso(const char *module
, struct dso
**pdso
)
322 const char *vmlinux_name
;
326 char module_name
[128];
328 snprintf(module_name
, sizeof(module_name
), "[%s]", module
);
329 map
= maps__find_by_name(&host_machine
->kmaps
, module_name
);
334 pr_debug("Failed to find module %s.\n", module
);
338 map
= machine__kernel_map(host_machine
);
341 vmlinux_name
= symbol_conf
.vmlinux_name
;
344 ret
= dso__load_vmlinux(dso
, map
, vmlinux_name
, false);
346 ret
= dso__load_vmlinux_path(dso
, map
);
353 * Some binaries like glibc have special symbols which are on the symbol
354 * table, but not in the debuginfo. If we can find the address of the
355 * symbol from map, we can translate the address back to the probe point.
357 static int find_alternative_probe_point(struct debuginfo
*dinfo
,
358 struct perf_probe_point
*pp
,
359 struct perf_probe_point
*result
,
360 const char *target
, struct nsinfo
*nsi
,
363 struct map
*map
= NULL
;
368 /* This can work only for function-name based one */
369 if (!pp
->function
|| pp
->file
)
372 map
= get_target_map(target
, nsi
, uprobes
);
376 /* Find the address of given function */
377 map__for_each_symbol_by_name(map
, pp
->function
, sym
) {
379 address
= sym
->start
;
380 if (sym
->type
== STT_GNU_IFUNC
)
381 pr_warning("Warning: The probe function (%s) is a GNU indirect function.\n"
382 "Consider identifying the final function used at run time and set the probe directly on that.\n",
385 address
= map
->unmap_ip(map
, sym
->start
) - map
->reloc
;
392 pr_debug("Symbol %s address found : %" PRIx64
"\n",
393 pp
->function
, address
);
395 ret
= debuginfo__find_probe_point(dinfo
, (unsigned long)address
,
398 ret
= (!ret
) ? -ENOENT
: ret
;
400 result
->offset
+= pp
->offset
;
401 result
->line
+= pp
->line
;
402 result
->retprobe
= pp
->retprobe
;
412 static int get_alternative_probe_event(struct debuginfo
*dinfo
,
413 struct perf_probe_event
*pev
,
414 struct perf_probe_point
*tmp
)
418 memcpy(tmp
, &pev
->point
, sizeof(*tmp
));
419 memset(&pev
->point
, 0, sizeof(pev
->point
));
420 ret
= find_alternative_probe_point(dinfo
, tmp
, &pev
->point
, pev
->target
,
421 pev
->nsi
, pev
->uprobes
);
423 memcpy(&pev
->point
, tmp
, sizeof(*tmp
));
428 static int get_alternative_line_range(struct debuginfo
*dinfo
,
429 struct line_range
*lr
,
430 const char *target
, bool user
)
432 struct perf_probe_point pp
= { .function
= lr
->function
,
435 struct perf_probe_point result
;
438 memset(&result
, 0, sizeof(result
));
440 if (lr
->end
!= INT_MAX
)
441 len
= lr
->end
- lr
->start
;
442 ret
= find_alternative_probe_point(dinfo
, &pp
, &result
,
445 lr
->function
= result
.function
;
446 lr
->file
= result
.file
;
447 lr
->start
= result
.line
;
448 if (lr
->end
!= INT_MAX
)
449 lr
->end
= lr
->start
+ len
;
450 clear_perf_probe_point(&pp
);
455 /* Open new debuginfo of given module */
456 static struct debuginfo
*open_debuginfo(const char *module
, struct nsinfo
*nsi
,
459 const char *path
= module
;
460 char reason
[STRERR_BUFSIZE
];
461 struct debuginfo
*ret
= NULL
;
462 struct dso
*dso
= NULL
;
466 if (!module
|| !strchr(module
, '/')) {
467 err
= kernel_get_module_dso(module
, &dso
);
469 if (!dso
|| dso
->load_errno
== 0) {
470 if (!str_error_r(-err
, reason
, STRERR_BUFSIZE
))
471 strcpy(reason
, "(unknown)");
473 dso__strerror_load(dso
, reason
, STRERR_BUFSIZE
);
476 pr_err("Module %s is not loaded, please specify its full path name.\n", module
);
478 pr_err("Failed to find the path for the kernel: %s\n", reason
);
482 path
= dso
->long_name
;
484 nsinfo__mountns_enter(nsi
, &nsc
);
485 ret
= debuginfo__new(path
);
486 if (!ret
&& !silent
) {
487 pr_warning("The %s file has no debug information.\n", path
);
488 if (!module
|| !strtailcmp(path
, ".ko"))
489 pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, ");
491 pr_warning("Rebuild with -g, ");
492 pr_warning("or install an appropriate debuginfo package.\n");
494 nsinfo__mountns_exit(&nsc
);
498 /* For caching the last debuginfo */
499 static struct debuginfo
*debuginfo_cache
;
500 static char *debuginfo_cache_path
;
502 static struct debuginfo
*debuginfo_cache__open(const char *module
, bool silent
)
504 const char *path
= module
;
506 /* If the module is NULL, it should be the kernel. */
510 if (debuginfo_cache_path
&& !strcmp(debuginfo_cache_path
, path
))
513 /* Copy module path */
514 free(debuginfo_cache_path
);
515 debuginfo_cache_path
= strdup(path
);
516 if (!debuginfo_cache_path
) {
517 debuginfo__delete(debuginfo_cache
);
518 debuginfo_cache
= NULL
;
522 debuginfo_cache
= open_debuginfo(module
, NULL
, silent
);
523 if (!debuginfo_cache
)
524 zfree(&debuginfo_cache_path
);
526 return debuginfo_cache
;
529 static void debuginfo_cache__exit(void)
531 debuginfo__delete(debuginfo_cache
);
532 debuginfo_cache
= NULL
;
533 zfree(&debuginfo_cache_path
);
537 static int get_text_start_address(const char *exec
, unsigned long *address
,
543 int fd
, ret
= -ENOENT
;
546 nsinfo__mountns_enter(nsi
, &nsc
);
547 fd
= open(exec
, O_RDONLY
);
548 nsinfo__mountns_exit(&nsc
);
552 elf
= elf_begin(fd
, PERF_ELF_C_READ_MMAP
, NULL
);
558 if (gelf_getehdr(elf
, &ehdr
) == NULL
)
561 if (!elf_section_by_name(elf
, &ehdr
, &shdr
, ".text", NULL
))
564 *address
= shdr
.sh_addr
- shdr
.sh_offset
;
575 * Convert trace point to probe point with debuginfo
577 static int find_perf_probe_point_from_dwarf(struct probe_trace_point
*tp
,
578 struct perf_probe_point
*pp
,
581 struct debuginfo
*dinfo
= NULL
;
582 unsigned long stext
= 0;
583 u64 addr
= tp
->address
;
586 /* convert the address to dwarf address */
592 ret
= get_text_start_address(tp
->module
, &stext
, NULL
);
596 } else if (tp
->symbol
) {
597 /* If the module is given, this returns relative address */
598 ret
= kernel_get_symbol_address_by_name(tp
->symbol
, &addr
,
599 false, !!tp
->module
);
605 pr_debug("try to find information at %" PRIx64
" in %s\n", addr
,
606 tp
->module
? : "kernel");
608 dinfo
= debuginfo_cache__open(tp
->module
, verbose
<= 0);
610 ret
= debuginfo__find_probe_point(dinfo
,
611 (unsigned long)addr
, pp
);
616 pp
->retprobe
= tp
->retprobe
;
620 pr_debug("Failed to find corresponding probes from debuginfo.\n");
621 return ret
? : -ENOENT
;
624 /* Adjust symbol name and address */
625 static int post_process_probe_trace_point(struct probe_trace_point
*tp
,
626 struct map
*map
, unsigned long offs
)
629 u64 addr
= tp
->address
- offs
;
631 sym
= map__find_symbol(map
, addr
);
635 if (strcmp(sym
->name
, tp
->symbol
)) {
636 /* If we have no realname, use symbol for it */
638 tp
->realname
= tp
->symbol
;
641 tp
->symbol
= strdup(sym
->name
);
645 tp
->offset
= addr
- sym
->start
;
652 * Rename DWARF symbols to ELF symbols -- gcc sometimes optimizes functions
653 * and generate new symbols with suffixes such as .constprop.N or .isra.N
654 * etc. Since those symbols are not recorded in DWARF, we have to find
655 * correct generated symbols from offline ELF binary.
656 * For online kernel or uprobes we don't need this because those are
657 * rebased on _text, or already a section relative address.
660 post_process_offline_probe_trace_events(struct probe_trace_event
*tevs
,
661 int ntevs
, const char *pathname
)
664 unsigned long stext
= 0;
667 /* Prepare a map for offline binary */
668 map
= dso__new_map(pathname
);
669 if (!map
|| get_text_start_address(pathname
, &stext
, NULL
) < 0) {
670 pr_warning("Failed to get ELF symbols for %s\n", pathname
);
674 for (i
= 0; i
< ntevs
; i
++) {
675 ret
= post_process_probe_trace_point(&tevs
[i
].point
,
685 static int add_exec_to_probe_trace_events(struct probe_trace_event
*tevs
,
686 int ntevs
, const char *exec
,
690 unsigned long stext
= 0;
695 ret
= get_text_start_address(exec
, &stext
, nsi
);
699 for (i
= 0; i
< ntevs
&& ret
>= 0; i
++) {
700 /* point.address is the address of point.symbol + point.offset */
701 tevs
[i
].point
.address
-= stext
;
702 tevs
[i
].point
.module
= strdup(exec
);
703 if (!tevs
[i
].point
.module
) {
707 tevs
[i
].uprobes
= true;
714 post_process_module_probe_trace_events(struct probe_trace_event
*tevs
,
715 int ntevs
, const char *module
,
716 struct debuginfo
*dinfo
)
718 Dwarf_Addr text_offs
= 0;
720 char *mod_name
= NULL
;
726 map
= get_target_map(module
, NULL
, false);
727 if (!map
|| debuginfo__get_text_offset(dinfo
, &text_offs
, true) < 0) {
728 pr_warning("Failed to get ELF symbols for %s\n", module
);
732 mod_name
= find_module_name(module
);
733 for (i
= 0; i
< ntevs
; i
++) {
734 ret
= post_process_probe_trace_point(&tevs
[i
].point
,
735 map
, (unsigned long)text_offs
);
738 tevs
[i
].point
.module
=
739 strdup(mod_name
? mod_name
: module
);
740 if (!tevs
[i
].point
.module
) {
753 post_process_kernel_probe_trace_events(struct probe_trace_event
*tevs
,
756 struct ref_reloc_sym
*reloc_sym
;
761 /* Skip post process if the target is an offline kernel */
762 if (symbol_conf
.ignore_vmlinux_buildid
)
763 return post_process_offline_probe_trace_events(tevs
, ntevs
,
764 symbol_conf
.vmlinux_name
);
766 reloc_sym
= kernel_get_ref_reloc_sym(&map
);
768 pr_warning("Relocated base symbol is not found!\n");
772 for (i
= 0; i
< ntevs
; i
++) {
773 if (!tevs
[i
].point
.address
)
775 if (tevs
[i
].point
.retprobe
&& !kretprobe_offset_is_supported())
778 * If we found a wrong one, mark it by NULL symbol.
779 * Since addresses in debuginfo is same as objdump, we need
780 * to convert it to addresses on memory.
782 if (kprobe_warn_out_range(tevs
[i
].point
.symbol
,
783 map__objdump_2mem(map
, tevs
[i
].point
.address
))) {
787 tmp
= strdup(reloc_sym
->name
);
791 /* If we have no realname, use symbol for it */
792 if (!tevs
[i
].point
.realname
)
793 tevs
[i
].point
.realname
= tevs
[i
].point
.symbol
;
795 free(tevs
[i
].point
.symbol
);
796 tevs
[i
].point
.symbol
= tmp
;
797 tevs
[i
].point
.offset
= tevs
[i
].point
.address
-
798 reloc_sym
->unrelocated_addr
;
804 arch__post_process_probe_trace_events(struct perf_probe_event
*pev __maybe_unused
,
805 int ntevs __maybe_unused
)
809 /* Post processing the probe events */
810 static int post_process_probe_trace_events(struct perf_probe_event
*pev
,
811 struct probe_trace_event
*tevs
,
812 int ntevs
, const char *module
,
813 bool uprobe
, struct debuginfo
*dinfo
)
818 ret
= add_exec_to_probe_trace_events(tevs
, ntevs
, module
,
821 /* Currently ref_reloc_sym based probe is not for drivers */
822 ret
= post_process_module_probe_trace_events(tevs
, ntevs
,
825 ret
= post_process_kernel_probe_trace_events(tevs
, ntevs
);
828 arch__post_process_probe_trace_events(pev
, ntevs
);
833 /* Try to find perf_probe_event with debuginfo */
834 static int try_to_find_probe_trace_events(struct perf_probe_event
*pev
,
835 struct probe_trace_event
**tevs
)
837 bool need_dwarf
= perf_probe_event_need_dwarf(pev
);
838 struct perf_probe_point tmp
;
839 struct debuginfo
*dinfo
;
842 dinfo
= open_debuginfo(pev
->target
, pev
->nsi
, !need_dwarf
);
846 pr_debug("Could not open debuginfo. Try to use symbols.\n");
850 pr_debug("Try to find probe point from debuginfo.\n");
851 /* Searching trace events corresponding to a probe event */
852 ntevs
= debuginfo__find_trace_events(dinfo
, pev
, tevs
);
854 if (ntevs
== 0) { /* Not found, retry with an alternative */
855 ret
= get_alternative_probe_event(dinfo
, pev
, &tmp
);
857 ntevs
= debuginfo__find_trace_events(dinfo
, pev
, tevs
);
859 * Write back to the original probe_event for
860 * setting appropriate (user given) event name
862 clear_perf_probe_point(&pev
->point
);
863 memcpy(&pev
->point
, &tmp
, sizeof(tmp
));
867 if (ntevs
> 0) { /* Succeeded to find trace events */
868 pr_debug("Found %d probe_trace_events.\n", ntevs
);
869 ret
= post_process_probe_trace_events(pev
, *tevs
, ntevs
,
870 pev
->target
, pev
->uprobes
, dinfo
);
871 if (ret
< 0 || ret
== ntevs
) {
872 pr_debug("Post processing failed or all events are skipped. (%d)\n", ret
);
873 clear_probe_trace_events(*tevs
, ntevs
);
879 debuginfo__delete(dinfo
);
881 if (ntevs
== 0) { /* No error but failed to find probe point. */
882 pr_warning("Probe point '%s' not found.\n",
883 synthesize_perf_probe_point(&pev
->point
));
885 } else if (ntevs
< 0) {
886 /* Error path : ntevs < 0 */
887 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs
);
889 pr_warning("Warning: No dwarf info found in the vmlinux - "
890 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
892 pr_debug("Trying to use symbols.\n");
899 #define LINEBUF_SIZE 256
900 #define NR_ADDITIONAL_LINES 2
902 static int __show_one_line(FILE *fp
, int l
, bool skip
, bool show_num
)
904 char buf
[LINEBUF_SIZE
], sbuf
[STRERR_BUFSIZE
];
905 const char *color
= show_num
? "" : PERF_COLOR_BLUE
;
906 const char *prefix
= NULL
;
909 if (fgets(buf
, LINEBUF_SIZE
, fp
) == NULL
)
914 prefix
= show_num
? "%7d " : " ";
915 color_fprintf(stdout
, color
, prefix
, l
);
917 color_fprintf(stdout
, color
, "%s", buf
);
919 } while (strchr(buf
, '\n') == NULL
);
924 pr_warning("File read error: %s\n",
925 str_error_r(errno
, sbuf
, sizeof(sbuf
)));
931 static int _show_one_line(FILE *fp
, int l
, bool skip
, bool show_num
)
933 int rv
= __show_one_line(fp
, l
, skip
, show_num
);
935 pr_warning("Source file is shorter than expected.\n");
941 #define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
942 #define show_one_line(f,l) _show_one_line(f,l,false,false)
943 #define skip_one_line(f,l) _show_one_line(f,l,true,false)
944 #define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
947 * Show line-range always requires debuginfo to find source file and
950 static int __show_line_range(struct line_range
*lr
, const char *module
,
955 struct debuginfo
*dinfo
;
959 char sbuf
[STRERR_BUFSIZE
];
961 /* Search a line range */
962 dinfo
= open_debuginfo(module
, NULL
, false);
966 ret
= debuginfo__find_line_range(dinfo
, lr
);
967 if (!ret
) { /* Not found, retry with an alternative */
968 ret
= get_alternative_line_range(dinfo
, lr
, module
, user
);
970 ret
= debuginfo__find_line_range(dinfo
, lr
);
972 debuginfo__delete(dinfo
);
973 if (ret
== 0 || ret
== -ENOENT
) {
974 pr_warning("Specified source line is not found.\n");
976 } else if (ret
< 0) {
977 pr_warning("Debuginfo analysis failed.\n");
981 /* Convert source file path */
983 ret
= get_real_path(tmp
, lr
->comp_dir
, &lr
->path
);
985 /* Free old path when new path is assigned */
990 pr_warning("Failed to find source file path.\n");
997 fprintf(stdout
, "<%s@%s:%d>\n", lr
->function
, lr
->path
,
998 lr
->start
- lr
->offset
);
1000 fprintf(stdout
, "<%s:%d>\n", lr
->path
, lr
->start
);
1002 fp
= fopen(lr
->path
, "r");
1004 pr_warning("Failed to open %s: %s\n", lr
->path
,
1005 str_error_r(errno
, sbuf
, sizeof(sbuf
)));
1008 /* Skip to starting line number */
1009 while (l
< lr
->start
) {
1010 ret
= skip_one_line(fp
, l
++);
1015 intlist__for_each_entry(ln
, lr
->line_list
) {
1016 for (; ln
->i
> l
; l
++) {
1017 ret
= show_one_line(fp
, l
- lr
->offset
);
1021 ret
= show_one_line_with_num(fp
, l
++ - lr
->offset
);
1026 if (lr
->end
== INT_MAX
)
1027 lr
->end
= l
+ NR_ADDITIONAL_LINES
;
1028 while (l
<= lr
->end
) {
1029 ret
= show_one_line_or_eof(fp
, l
++ - lr
->offset
);
1038 int show_line_range(struct line_range
*lr
, const char *module
,
1039 struct nsinfo
*nsi
, bool user
)
1042 struct nscookie nsc
;
1044 ret
= init_probe_symbol_maps(user
);
1047 nsinfo__mountns_enter(nsi
, &nsc
);
1048 ret
= __show_line_range(lr
, module
, user
);
1049 nsinfo__mountns_exit(&nsc
);
1050 exit_probe_symbol_maps();
1055 static int show_available_vars_at(struct debuginfo
*dinfo
,
1056 struct perf_probe_event
*pev
,
1057 struct strfilter
*_filter
)
1061 struct str_node
*node
;
1062 struct variable_list
*vls
= NULL
, *vl
;
1063 struct perf_probe_point tmp
;
1066 buf
= synthesize_perf_probe_point(&pev
->point
);
1069 pr_debug("Searching variables at %s\n", buf
);
1071 ret
= debuginfo__find_available_vars_at(dinfo
, pev
, &vls
);
1072 if (!ret
) { /* Not found, retry with an alternative */
1073 ret
= get_alternative_probe_event(dinfo
, pev
, &tmp
);
1075 ret
= debuginfo__find_available_vars_at(dinfo
, pev
,
1077 /* Release the old probe_point */
1078 clear_perf_probe_point(&tmp
);
1082 if (ret
== 0 || ret
== -ENOENT
) {
1083 pr_err("Failed to find the address of %s\n", buf
);
1086 pr_warning("Debuginfo analysis failed.\n");
1090 /* Some variables are found */
1091 fprintf(stdout
, "Available variables at %s\n", buf
);
1092 for (i
= 0; i
< ret
; i
++) {
1095 * A probe point might be converted to
1096 * several trace points.
1098 fprintf(stdout
, "\t@<%s+%lu>\n", vl
->point
.symbol
,
1100 zfree(&vl
->point
.symbol
);
1103 strlist__for_each_entry(node
, vl
->vars
) {
1104 var
= strchr(node
->s
, '\t') + 1;
1105 if (strfilter__compare(_filter
, var
)) {
1106 fprintf(stdout
, "\t\t%s\n", node
->s
);
1110 strlist__delete(vl
->vars
);
1113 fprintf(stdout
, "\t\t(No matched variables)\n");
1121 /* Show available variables on given probe point */
1122 int show_available_vars(struct perf_probe_event
*pevs
, int npevs
,
1123 struct strfilter
*_filter
)
1126 struct debuginfo
*dinfo
;
1128 ret
= init_probe_symbol_maps(pevs
->uprobes
);
1132 dinfo
= open_debuginfo(pevs
->target
, pevs
->nsi
, false);
1140 for (i
= 0; i
< npevs
&& ret
>= 0; i
++)
1141 ret
= show_available_vars_at(dinfo
, &pevs
[i
], _filter
);
1143 debuginfo__delete(dinfo
);
1145 exit_probe_symbol_maps();
1149 #else /* !HAVE_DWARF_SUPPORT */
1151 static void debuginfo_cache__exit(void)
1156 find_perf_probe_point_from_dwarf(struct probe_trace_point
*tp __maybe_unused
,
1157 struct perf_probe_point
*pp __maybe_unused
,
1158 bool is_kprobe __maybe_unused
)
1163 static int try_to_find_probe_trace_events(struct perf_probe_event
*pev
,
1164 struct probe_trace_event
**tevs __maybe_unused
)
1166 if (perf_probe_event_need_dwarf(pev
)) {
1167 pr_warning("Debuginfo-analysis is not supported.\n");
1174 int show_line_range(struct line_range
*lr __maybe_unused
,
1175 const char *module __maybe_unused
,
1176 struct nsinfo
*nsi __maybe_unused
,
1177 bool user __maybe_unused
)
1179 pr_warning("Debuginfo-analysis is not supported.\n");
1183 int show_available_vars(struct perf_probe_event
*pevs __maybe_unused
,
1184 int npevs __maybe_unused
,
1185 struct strfilter
*filter __maybe_unused
)
1187 pr_warning("Debuginfo-analysis is not supported.\n");
1192 void line_range__clear(struct line_range
*lr
)
1194 zfree(&lr
->function
);
1197 zfree(&lr
->comp_dir
);
1198 intlist__delete(lr
->line_list
);
1201 int line_range__init(struct line_range
*lr
)
1203 memset(lr
, 0, sizeof(*lr
));
1204 lr
->line_list
= intlist__new(NULL
);
1211 static int parse_line_num(char **ptr
, int *val
, const char *what
)
1213 const char *start
= *ptr
;
1216 *val
= strtol(*ptr
, ptr
, 0);
1217 if (errno
|| *ptr
== start
) {
1218 semantic_error("'%s' is not a valid number.\n", what
);
1224 /* Check the name is good for event, group or function */
1225 static bool is_c_func_name(const char *name
)
1227 if (!isalpha(*name
) && *name
!= '_')
1229 while (*++name
!= '\0') {
1230 if (!isalpha(*name
) && !isdigit(*name
) && *name
!= '_')
1237 * Stuff 'lr' according to the line range described by 'arg'.
1238 * The line range syntax is described by:
1240 * SRC[:SLN[+NUM|-ELN]]
1241 * FNC[@SRC][:SLN[+NUM|-ELN]]
1243 int parse_line_range_desc(const char *arg
, struct line_range
*lr
)
1245 char *range
, *file
, *name
= strdup(arg
);
1254 range
= strchr(name
, ':');
1258 err
= parse_line_num(&range
, &lr
->start
, "start line");
1262 if (*range
== '+' || *range
== '-') {
1263 const char c
= *range
++;
1265 err
= parse_line_num(&range
, &lr
->end
, "end line");
1270 lr
->end
+= lr
->start
;
1272 * Adjust the number of lines here.
1273 * If the number of lines == 1, the
1274 * the end of line should be equal to
1275 * the start of line.
1281 pr_debug("Line range is %d to %d\n", lr
->start
, lr
->end
);
1284 if (lr
->start
> lr
->end
) {
1285 semantic_error("Start line must be smaller"
1286 " than end line.\n");
1289 if (*range
!= '\0') {
1290 semantic_error("Tailing with invalid str '%s'.\n", range
);
1295 file
= strchr(name
, '@');
1298 lr
->file
= strdup(++file
);
1299 if (lr
->file
== NULL
) {
1303 lr
->function
= name
;
1304 } else if (strchr(name
, '/') || strchr(name
, '.'))
1306 else if (is_c_func_name(name
))/* We reuse it for checking funcname */
1307 lr
->function
= name
;
1308 else { /* Invalid name */
1309 semantic_error("'%s' is not a valid function name.\n", name
);
1320 static int parse_perf_probe_event_name(char **arg
, struct perf_probe_event
*pev
)
1324 ptr
= strpbrk_esc(*arg
, ":");
1327 if (!pev
->sdt
&& !is_c_func_name(*arg
))
1329 pev
->group
= strdup_esc(*arg
);
1336 pev
->event
= strdup_esc(*arg
);
1337 if (pev
->event
== NULL
)
1340 if (!pev
->sdt
&& !is_c_func_name(pev
->event
)) {
1344 semantic_error("%s is bad for event name -it must "
1345 "follow C symbol-naming rule.\n", *arg
);
1351 /* Parse probepoint definition. */
1352 static int parse_perf_probe_point(char *arg
, struct perf_probe_event
*pev
)
1354 struct perf_probe_point
*pp
= &pev
->point
;
1357 bool file_spec
= false;
1362 * perf probe [GRP:][EVENT=]SRC[:LN|;PTN]
1363 * perf probe [GRP:][EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
1364 * perf probe %[GRP:]SDT_EVENT
1369 if (is_sdt_event(arg
)) {
1375 ptr
= strpbrk_esc(arg
, ";=@+%");
1379 semantic_error("%s must be an SDT name.\n",
1383 /* This must be a target file name or build id */
1384 tmp
= build_id_cache__complement(ptr
+ 1);
1386 pev
->target
= build_id_cache__origname(tmp
);
1389 pev
->target
= strdup_esc(ptr
+ 1);
1394 ret
= parse_perf_probe_event_name(&arg
, pev
);
1396 if (asprintf(&pev
->point
.function
, "%%%s", pev
->event
) < 0)
1402 if (ptr
&& *ptr
== '=') { /* Event name */
1405 ret
= parse_perf_probe_event_name(&arg
, pev
);
1413 * Check arg is function or file name and copy it.
1415 * We consider arg to be a file spec if and only if it satisfies
1416 * all of the below criteria::
1417 * - it does not include any of "+@%",
1418 * - it includes one of ":;", and
1419 * - it has a period '.' in the name.
1421 * Otherwise, we consider arg to be a function specification.
1423 if (!strpbrk_esc(arg
, "+@%")) {
1424 ptr
= strpbrk_esc(arg
, ";:");
1425 /* This is a file spec if it includes a '.' before ; or : */
1426 if (ptr
&& memchr(arg
, '.', ptr
- arg
))
1430 ptr
= strpbrk_esc(arg
, ";:+@%");
1439 tmp
= strdup_esc(arg
);
1450 * Keep pp->function even if this is absolute address,
1451 * so it can mark whether abs_address is valid.
1452 * Which make 'perf probe lib.bin 0x0' possible.
1454 * Note that checking length of tmp is not needed
1455 * because when we access tmp[1] we know tmp[0] is '0',
1456 * so tmp[1] should always valid (but could be '\0').
1458 if (tmp
&& !strncmp(tmp
, "0x", 2)) {
1459 pp
->abs_address
= strtoul(pp
->function
, &tmp
, 0);
1461 semantic_error("Invalid absolute address.\n");
1467 /* Parse other options */
1471 if (c
== ';') { /* Lazy pattern must be the last part */
1472 pp
->lazy_line
= strdup(arg
); /* let leave escapes */
1473 if (pp
->lazy_line
== NULL
)
1477 ptr
= strpbrk_esc(arg
, ";:+@%");
1483 case ':': /* Line number */
1484 pp
->line
= strtoul(arg
, &tmp
, 0);
1486 semantic_error("There is non-digit char"
1487 " in line number.\n");
1491 case '+': /* Byte offset from a symbol */
1492 pp
->offset
= strtoul(arg
, &tmp
, 0);
1494 semantic_error("There is non-digit character"
1499 case '@': /* File name */
1501 semantic_error("SRC@SRC is not allowed.\n");
1504 pp
->file
= strdup_esc(arg
);
1505 if (pp
->file
== NULL
)
1508 case '%': /* Probe places */
1509 if (strcmp(arg
, "return") == 0) {
1511 } else { /* Others not supported yet */
1512 semantic_error("%%%s is not supported.\n", arg
);
1516 default: /* Buggy case */
1517 pr_err("This program has a bug at %s:%d.\n",
1518 __FILE__
, __LINE__
);
1524 /* Exclusion check */
1525 if (pp
->lazy_line
&& pp
->line
) {
1526 semantic_error("Lazy pattern can't be used with"
1531 if (pp
->lazy_line
&& pp
->offset
) {
1532 semantic_error("Lazy pattern can't be used with offset.\n");
1536 if (pp
->line
&& pp
->offset
) {
1537 semantic_error("Offset can't be used with line number.\n");
1541 if (!pp
->line
&& !pp
->lazy_line
&& pp
->file
&& !pp
->function
) {
1542 semantic_error("File always requires line number or "
1547 if (pp
->offset
&& !pp
->function
) {
1548 semantic_error("Offset requires an entry function.\n");
1552 if ((pp
->offset
|| pp
->line
|| pp
->lazy_line
) && pp
->retprobe
) {
1553 semantic_error("Offset/Line/Lazy pattern can't be used with "
1558 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
1559 pp
->function
, pp
->file
, pp
->line
, pp
->offset
, pp
->retprobe
,
1564 /* Parse perf-probe event argument */
1565 static int parse_perf_probe_arg(char *str
, struct perf_probe_arg
*arg
)
1567 char *tmp
, *goodname
;
1568 struct perf_probe_arg_field
**fieldp
;
1570 pr_debug("parsing arg: %s into ", str
);
1572 tmp
= strchr(str
, '=');
1574 arg
->name
= strndup(str
, tmp
- str
);
1575 if (arg
->name
== NULL
)
1577 pr_debug("name:%s ", arg
->name
);
1581 tmp
= strchr(str
, '@');
1582 if (tmp
&& tmp
!= str
&& !strcmp(tmp
+ 1, "user")) { /* user attr */
1583 if (!user_access_is_supported()) {
1584 semantic_error("ftrace does not support user access\n");
1588 arg
->user_access
= true;
1589 pr_debug("user_access ");
1592 tmp
= strchr(str
, ':');
1593 if (tmp
) { /* Type setting */
1595 arg
->type
= strdup(tmp
+ 1);
1596 if (arg
->type
== NULL
)
1598 pr_debug("type:%s ", arg
->type
);
1601 tmp
= strpbrk(str
, "-.[");
1602 if (!is_c_varname(str
) || !tmp
) {
1603 /* A variable, register, symbol or special value */
1604 arg
->var
= strdup(str
);
1605 if (arg
->var
== NULL
)
1607 pr_debug("%s\n", arg
->var
);
1611 /* Structure fields or array element */
1612 arg
->var
= strndup(str
, tmp
- str
);
1613 if (arg
->var
== NULL
)
1615 goodname
= arg
->var
;
1616 pr_debug("%s, ", arg
->var
);
1617 fieldp
= &arg
->field
;
1620 *fieldp
= zalloc(sizeof(struct perf_probe_arg_field
));
1621 if (*fieldp
== NULL
)
1623 if (*tmp
== '[') { /* Array */
1625 (*fieldp
)->index
= strtol(str
+ 1, &tmp
, 0);
1626 (*fieldp
)->ref
= true;
1627 if (*tmp
!= ']' || tmp
== str
+ 1) {
1628 semantic_error("Array index must be a"
1635 } else { /* Structure */
1638 (*fieldp
)->ref
= false;
1639 } else if (tmp
[1] == '>') {
1641 (*fieldp
)->ref
= true;
1643 semantic_error("Argument parse error: %s\n",
1647 tmp
= strpbrk(str
, "-.[");
1650 (*fieldp
)->name
= strndup(str
, tmp
- str
);
1651 if ((*fieldp
)->name
== NULL
)
1654 goodname
= (*fieldp
)->name
;
1655 pr_debug("%s(%d), ", (*fieldp
)->name
, (*fieldp
)->ref
);
1656 fieldp
= &(*fieldp
)->next
;
1659 (*fieldp
)->name
= strdup(str
);
1660 if ((*fieldp
)->name
== NULL
)
1663 goodname
= (*fieldp
)->name
;
1664 pr_debug("%s(%d)\n", (*fieldp
)->name
, (*fieldp
)->ref
);
1666 /* If no name is specified, set the last field name (not array index)*/
1668 arg
->name
= strdup(goodname
);
1669 if (arg
->name
== NULL
)
1675 /* Parse perf-probe event command */
1676 int parse_perf_probe_command(const char *cmd
, struct perf_probe_event
*pev
)
1679 int argc
, i
, ret
= 0;
1681 argv
= argv_split(cmd
, &argc
);
1683 pr_debug("Failed to split arguments.\n");
1686 if (argc
- 1 > MAX_PROBE_ARGS
) {
1687 semantic_error("Too many probe arguments (%d).\n", argc
- 1);
1691 /* Parse probe point */
1692 ret
= parse_perf_probe_point(argv
[0], pev
);
1696 /* Generate event name if needed */
1697 if (!pev
->event
&& pev
->point
.function
&& pev
->point
.line
1698 && !pev
->point
.lazy_line
&& !pev
->point
.offset
) {
1699 if (asprintf(&pev
->event
, "%s_L%d", pev
->point
.function
,
1700 pev
->point
.line
) < 0)
1704 /* Copy arguments and ensure return probe has no C argument */
1705 pev
->nargs
= argc
- 1;
1706 pev
->args
= zalloc(sizeof(struct perf_probe_arg
) * pev
->nargs
);
1707 if (pev
->args
== NULL
) {
1711 for (i
= 0; i
< pev
->nargs
&& ret
>= 0; i
++) {
1712 ret
= parse_perf_probe_arg(argv
[i
+ 1], &pev
->args
[i
]);
1714 is_c_varname(pev
->args
[i
].var
) && pev
->point
.retprobe
) {
1715 semantic_error("You can't specify local variable for"
1726 /* Returns true if *any* ARG is either C variable, $params or $vars. */
1727 bool perf_probe_with_var(struct perf_probe_event
*pev
)
1731 for (i
= 0; i
< pev
->nargs
; i
++)
1732 if (is_c_varname(pev
->args
[i
].var
) ||
1733 !strcmp(pev
->args
[i
].var
, PROBE_ARG_PARAMS
) ||
1734 !strcmp(pev
->args
[i
].var
, PROBE_ARG_VARS
))
1739 /* Return true if this perf_probe_event requires debuginfo */
1740 bool perf_probe_event_need_dwarf(struct perf_probe_event
*pev
)
1742 if (pev
->point
.file
|| pev
->point
.line
|| pev
->point
.lazy_line
)
1745 if (perf_probe_with_var(pev
))
1751 /* Parse probe_events event into struct probe_point */
1752 int parse_probe_trace_command(const char *cmd
, struct probe_trace_event
*tev
)
1754 struct probe_trace_point
*tp
= &tev
->point
;
1757 char *argv0_str
= NULL
, *fmt
, *fmt1_str
, *fmt2_str
, *fmt3_str
;
1761 pr_debug("Parsing probe_events: %s\n", cmd
);
1762 argv
= argv_split(cmd
, &argc
);
1764 pr_debug("Failed to split arguments.\n");
1768 semantic_error("Too few probe arguments.\n");
1773 /* Scan event and group name. */
1774 argv0_str
= strdup(argv
[0]);
1775 if (argv0_str
== NULL
) {
1779 fmt1_str
= strtok_r(argv0_str
, ":", &fmt
);
1780 fmt2_str
= strtok_r(NULL
, "/", &fmt
);
1781 fmt3_str
= strtok_r(NULL
, " \t", &fmt
);
1782 if (fmt1_str
== NULL
|| fmt2_str
== NULL
|| fmt3_str
== NULL
) {
1783 semantic_error("Failed to parse event name: %s\n", argv
[0]);
1788 tev
->group
= strdup(fmt2_str
);
1789 tev
->event
= strdup(fmt3_str
);
1790 if (tev
->group
== NULL
|| tev
->event
== NULL
) {
1794 pr_debug("Group:%s Event:%s probe:%c\n", tev
->group
, tev
->event
, pr
);
1796 tp
->retprobe
= (pr
== 'r');
1798 /* Scan module name(if there), function name and offset */
1799 p
= strchr(argv
[1], ':');
1801 tp
->module
= strndup(argv
[1], p
- argv
[1]);
1806 tev
->uprobes
= (tp
->module
[0] == '/');
1810 fmt1_str
= strtok_r(p
, "+", &fmt
);
1811 /* only the address started with 0x */
1812 if (fmt1_str
[0] == '0') {
1814 * Fix a special case:
1815 * if address == 0, kernel reports something like:
1816 * p:probe_libc/abs_0 /lib/libc-2.18.so:0x (null) arg1=%ax
1817 * Newer kernel may fix that, but we want to
1818 * support old kernel also.
1820 if (strcmp(fmt1_str
, "0x") == 0) {
1821 if (!argv
[2] || strcmp(argv
[2], "(null)")) {
1828 for (i
= 2; argv
[i
+ 1] != NULL
; i
++)
1829 argv
[i
] = argv
[i
+ 1];
1834 tp
->address
= strtoul(fmt1_str
, NULL
, 0);
1836 /* Only the symbol-based probe has offset */
1837 tp
->symbol
= strdup(fmt1_str
);
1838 if (tp
->symbol
== NULL
) {
1842 fmt2_str
= strtok_r(NULL
, "", &fmt
);
1843 if (fmt2_str
== NULL
)
1846 tp
->offset
= strtoul(fmt2_str
, NULL
, 10);
1850 fmt2_str
= strchr(p
, '(');
1852 tp
->ref_ctr_offset
= strtoul(fmt2_str
+ 1, NULL
, 0);
1855 tev
->nargs
= argc
- 2;
1856 tev
->args
= zalloc(sizeof(struct probe_trace_arg
) * tev
->nargs
);
1857 if (tev
->args
== NULL
) {
1861 for (i
= 0; i
< tev
->nargs
; i
++) {
1862 p
= strchr(argv
[i
+ 2], '=');
1863 if (p
) /* We don't need which register is assigned. */
1867 tev
->args
[i
].name
= strdup(argv
[i
+ 2]);
1868 /* TODO: parse regs and offset */
1869 tev
->args
[i
].value
= strdup(p
);
1870 if (tev
->args
[i
].name
== NULL
|| tev
->args
[i
].value
== NULL
) {
1882 /* Compose only probe arg */
1883 char *synthesize_perf_probe_arg(struct perf_probe_arg
*pa
)
1885 struct perf_probe_arg_field
*field
= pa
->field
;
1890 if (strbuf_init(&buf
, 64) < 0)
1893 if (pa
->name
&& pa
->var
)
1894 err
= strbuf_addf(&buf
, "%s=%s", pa
->name
, pa
->var
);
1896 err
= strbuf_addstr(&buf
, pa
->name
?: pa
->var
);
1901 if (field
->name
[0] == '[')
1902 err
= strbuf_addstr(&buf
, field
->name
);
1904 err
= strbuf_addf(&buf
, "%s%s", field
->ref
? "->" : ".",
1906 field
= field
->next
;
1912 if (strbuf_addf(&buf
, ":%s", pa
->type
) < 0)
1915 ret
= strbuf_detach(&buf
, NULL
);
1917 strbuf_release(&buf
);
1921 /* Compose only probe point (not argument) */
1922 char *synthesize_perf_probe_point(struct perf_probe_point
*pp
)
1925 char *tmp
, *ret
= NULL
;
1928 if (strbuf_init(&buf
, 64) < 0)
1932 if (strbuf_addstr(&buf
, pp
->function
) < 0)
1935 err
= strbuf_addf(&buf
, "+%lu", pp
->offset
);
1937 err
= strbuf_addf(&buf
, ":%d", pp
->line
);
1938 else if (pp
->retprobe
)
1939 err
= strbuf_addstr(&buf
, "%return");
1947 tmp
= strchr(pp
->file
+ len
- 30, '/');
1948 tmp
= tmp
? tmp
+ 1 : pp
->file
+ len
- 30;
1950 err
= strbuf_addf(&buf
, "@%s", tmp
);
1951 if (!err
&& !pp
->function
&& pp
->line
)
1952 err
= strbuf_addf(&buf
, ":%d", pp
->line
);
1955 ret
= strbuf_detach(&buf
, NULL
);
1957 strbuf_release(&buf
);
1961 char *synthesize_perf_probe_command(struct perf_probe_event
*pev
)
1964 char *tmp
, *ret
= NULL
;
1967 if (strbuf_init(&buf
, 64))
1970 if (strbuf_addf(&buf
, "%s:%s=", pev
->group
?: PERFPROBE_GROUP
,
1974 tmp
= synthesize_perf_probe_point(&pev
->point
);
1975 if (!tmp
|| strbuf_addstr(&buf
, tmp
) < 0)
1979 for (i
= 0; i
< pev
->nargs
; i
++) {
1980 tmp
= synthesize_perf_probe_arg(pev
->args
+ i
);
1981 if (!tmp
|| strbuf_addf(&buf
, " %s", tmp
) < 0)
1986 ret
= strbuf_detach(&buf
, NULL
);
1988 strbuf_release(&buf
);
1992 static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref
*ref
,
1993 struct strbuf
*buf
, int depth
)
1997 depth
= __synthesize_probe_trace_arg_ref(ref
->next
, buf
,
2002 if (ref
->user_access
)
2003 err
= strbuf_addf(buf
, "%s%ld(", "+u", ref
->offset
);
2005 err
= strbuf_addf(buf
, "%+ld(", ref
->offset
);
2006 return (err
< 0) ? err
: depth
;
2009 static int synthesize_probe_trace_arg(struct probe_trace_arg
*arg
,
2012 struct probe_trace_arg_ref
*ref
= arg
->ref
;
2015 /* Argument name or separator */
2017 err
= strbuf_addf(buf
, " %s=", arg
->name
);
2019 err
= strbuf_addch(buf
, ' ');
2023 /* Special case: @XXX */
2024 if (arg
->value
[0] == '@' && arg
->ref
)
2027 /* Dereferencing arguments */
2029 depth
= __synthesize_probe_trace_arg_ref(ref
, buf
, 1);
2034 /* Print argument value */
2035 if (arg
->value
[0] == '@' && arg
->ref
)
2036 err
= strbuf_addf(buf
, "%s%+ld", arg
->value
, arg
->ref
->offset
);
2038 err
= strbuf_addstr(buf
, arg
->value
);
2041 while (!err
&& depth
--)
2042 err
= strbuf_addch(buf
, ')');
2044 /* Print argument type */
2045 if (!err
&& arg
->type
)
2046 err
= strbuf_addf(buf
, ":%s", arg
->type
);
2052 synthesize_uprobe_trace_def(struct probe_trace_event
*tev
, struct strbuf
*buf
)
2054 struct probe_trace_point
*tp
= &tev
->point
;
2057 err
= strbuf_addf(buf
, "%s:0x%lx", tp
->module
, tp
->address
);
2059 if (err
>= 0 && tp
->ref_ctr_offset
) {
2060 if (!uprobe_ref_ctr_is_supported())
2062 err
= strbuf_addf(buf
, "(0x%lx)", tp
->ref_ctr_offset
);
2064 return err
>= 0 ? 0 : -1;
2067 char *synthesize_probe_trace_command(struct probe_trace_event
*tev
)
2069 struct probe_trace_point
*tp
= &tev
->point
;
2074 /* Uprobes must have tp->module */
2075 if (tev
->uprobes
&& !tp
->module
)
2078 if (strbuf_init(&buf
, 32) < 0)
2081 if (strbuf_addf(&buf
, "%c:%s/%s ", tp
->retprobe
? 'r' : 'p',
2082 tev
->group
, tev
->event
) < 0)
2085 * If tp->address == 0, then this point must be a
2086 * absolute address uprobe.
2087 * try_to_find_absolute_address() should have made
2088 * tp->symbol to "0x0".
2090 if (tev
->uprobes
&& !tp
->address
) {
2091 if (!tp
->symbol
|| strcmp(tp
->symbol
, "0x0"))
2095 /* Use the tp->address for uprobes */
2097 err
= synthesize_uprobe_trace_def(tev
, &buf
);
2098 } else if (!strncmp(tp
->symbol
, "0x", 2)) {
2099 /* Absolute address. See try_to_find_absolute_address() */
2100 err
= strbuf_addf(&buf
, "%s%s0x%lx", tp
->module
?: "",
2101 tp
->module
? ":" : "", tp
->address
);
2103 err
= strbuf_addf(&buf
, "%s%s%s+%lu", tp
->module
?: "",
2104 tp
->module
? ":" : "", tp
->symbol
, tp
->offset
);
2110 for (i
= 0; i
< tev
->nargs
; i
++)
2111 if (synthesize_probe_trace_arg(&tev
->args
[i
], &buf
) < 0)
2114 ret
= strbuf_detach(&buf
, NULL
);
2116 strbuf_release(&buf
);
2120 static int find_perf_probe_point_from_map(struct probe_trace_point
*tp
,
2121 struct perf_probe_point
*pp
,
2124 struct symbol
*sym
= NULL
;
2125 struct map
*map
= NULL
;
2126 u64 addr
= tp
->address
;
2130 map
= dso__new_map(tp
->module
);
2133 sym
= map__find_symbol(map
, addr
);
2135 if (tp
->symbol
&& !addr
) {
2136 if (kernel_get_symbol_address_by_name(tp
->symbol
,
2137 &addr
, true, false) < 0)
2142 sym
= machine__find_kernel_symbol(host_machine
, addr
, &map
);
2149 pp
->retprobe
= tp
->retprobe
;
2150 pp
->offset
= addr
- map
->unmap_ip(map
, sym
->start
);
2151 pp
->function
= strdup(sym
->name
);
2152 ret
= pp
->function
? 0 : -ENOMEM
;
2155 if (map
&& !is_kprobe
) {
2162 static int convert_to_perf_probe_point(struct probe_trace_point
*tp
,
2163 struct perf_probe_point
*pp
,
2169 ret
= find_perf_probe_point_from_dwarf(tp
, pp
, is_kprobe
);
2172 ret
= find_perf_probe_point_from_map(tp
, pp
, is_kprobe
);
2176 pr_debug("Failed to find probe point from both of dwarf and map.\n");
2179 pp
->function
= strdup(tp
->symbol
);
2180 pp
->offset
= tp
->offset
;
2182 ret
= e_snprintf(buf
, 128, "0x%" PRIx64
, (u64
)tp
->address
);
2185 pp
->function
= strdup(buf
);
2188 if (pp
->function
== NULL
)
2191 pp
->retprobe
= tp
->retprobe
;
2196 static int convert_to_perf_probe_event(struct probe_trace_event
*tev
,
2197 struct perf_probe_event
*pev
, bool is_kprobe
)
2199 struct strbuf buf
= STRBUF_INIT
;
2202 /* Convert event/group name */
2203 pev
->event
= strdup(tev
->event
);
2204 pev
->group
= strdup(tev
->group
);
2205 if (pev
->event
== NULL
|| pev
->group
== NULL
)
2208 /* Convert trace_point to probe_point */
2209 ret
= convert_to_perf_probe_point(&tev
->point
, &pev
->point
, is_kprobe
);
2213 /* Convert trace_arg to probe_arg */
2214 pev
->nargs
= tev
->nargs
;
2215 pev
->args
= zalloc(sizeof(struct perf_probe_arg
) * pev
->nargs
);
2216 if (pev
->args
== NULL
)
2218 for (i
= 0; i
< tev
->nargs
&& ret
>= 0; i
++) {
2219 if (tev
->args
[i
].name
)
2220 pev
->args
[i
].name
= strdup(tev
->args
[i
].name
);
2222 if ((ret
= strbuf_init(&buf
, 32)) < 0)
2224 ret
= synthesize_probe_trace_arg(&tev
->args
[i
], &buf
);
2225 pev
->args
[i
].name
= strbuf_detach(&buf
, NULL
);
2227 if (pev
->args
[i
].name
== NULL
&& ret
>= 0)
2232 clear_perf_probe_event(pev
);
2237 void clear_perf_probe_event(struct perf_probe_event
*pev
)
2239 struct perf_probe_arg_field
*field
, *next
;
2244 zfree(&pev
->target
);
2245 clear_perf_probe_point(&pev
->point
);
2247 for (i
= 0; i
< pev
->nargs
; i
++) {
2248 zfree(&pev
->args
[i
].name
);
2249 zfree(&pev
->args
[i
].var
);
2250 zfree(&pev
->args
[i
].type
);
2251 field
= pev
->args
[i
].field
;
2254 zfree(&field
->name
);
2263 #define strdup_or_goto(str, label) \
2264 ({ char *__p = NULL; if (str && !(__p = strdup(str))) goto label; __p; })
2266 static int perf_probe_point__copy(struct perf_probe_point
*dst
,
2267 struct perf_probe_point
*src
)
2269 dst
->file
= strdup_or_goto(src
->file
, out_err
);
2270 dst
->function
= strdup_or_goto(src
->function
, out_err
);
2271 dst
->lazy_line
= strdup_or_goto(src
->lazy_line
, out_err
);
2272 dst
->line
= src
->line
;
2273 dst
->retprobe
= src
->retprobe
;
2274 dst
->offset
= src
->offset
;
2278 clear_perf_probe_point(dst
);
2282 static int perf_probe_arg__copy(struct perf_probe_arg
*dst
,
2283 struct perf_probe_arg
*src
)
2285 struct perf_probe_arg_field
*field
, **ppfield
;
2287 dst
->name
= strdup_or_goto(src
->name
, out_err
);
2288 dst
->var
= strdup_or_goto(src
->var
, out_err
);
2289 dst
->type
= strdup_or_goto(src
->type
, out_err
);
2292 ppfield
= &(dst
->field
);
2294 *ppfield
= zalloc(sizeof(*field
));
2297 (*ppfield
)->name
= strdup_or_goto(field
->name
, out_err
);
2298 (*ppfield
)->index
= field
->index
;
2299 (*ppfield
)->ref
= field
->ref
;
2300 field
= field
->next
;
2301 ppfield
= &((*ppfield
)->next
);
2308 int perf_probe_event__copy(struct perf_probe_event
*dst
,
2309 struct perf_probe_event
*src
)
2313 dst
->event
= strdup_or_goto(src
->event
, out_err
);
2314 dst
->group
= strdup_or_goto(src
->group
, out_err
);
2315 dst
->target
= strdup_or_goto(src
->target
, out_err
);
2316 dst
->uprobes
= src
->uprobes
;
2318 if (perf_probe_point__copy(&dst
->point
, &src
->point
) < 0)
2321 dst
->args
= zalloc(sizeof(struct perf_probe_arg
) * src
->nargs
);
2324 dst
->nargs
= src
->nargs
;
2326 for (i
= 0; i
< src
->nargs
; i
++)
2327 if (perf_probe_arg__copy(&dst
->args
[i
], &src
->args
[i
]) < 0)
2332 clear_perf_probe_event(dst
);
2336 void clear_probe_trace_event(struct probe_trace_event
*tev
)
2338 struct probe_trace_arg_ref
*ref
, *next
;
2343 zfree(&tev
->point
.symbol
);
2344 zfree(&tev
->point
.realname
);
2345 zfree(&tev
->point
.module
);
2346 for (i
= 0; i
< tev
->nargs
; i
++) {
2347 zfree(&tev
->args
[i
].name
);
2348 zfree(&tev
->args
[i
].value
);
2349 zfree(&tev
->args
[i
].type
);
2350 ref
= tev
->args
[i
].ref
;
2361 struct kprobe_blacklist_node
{
2362 struct list_head list
;
2363 unsigned long start
;
2368 static void kprobe_blacklist__delete(struct list_head
*blacklist
)
2370 struct kprobe_blacklist_node
*node
;
2372 while (!list_empty(blacklist
)) {
2373 node
= list_first_entry(blacklist
,
2374 struct kprobe_blacklist_node
, list
);
2375 list_del_init(&node
->list
);
2376 zfree(&node
->symbol
);
2381 static int kprobe_blacklist__load(struct list_head
*blacklist
)
2383 struct kprobe_blacklist_node
*node
;
2384 const char *__debugfs
= debugfs__mountpoint();
2385 char buf
[PATH_MAX
], *p
;
2389 if (__debugfs
== NULL
)
2392 ret
= e_snprintf(buf
, PATH_MAX
, "%s/kprobes/blacklist", __debugfs
);
2396 fp
= fopen(buf
, "r");
2401 while (fgets(buf
, PATH_MAX
, fp
)) {
2402 node
= zalloc(sizeof(*node
));
2407 INIT_LIST_HEAD(&node
->list
);
2408 list_add_tail(&node
->list
, blacklist
);
2409 if (sscanf(buf
, "0x%lx-0x%lx", &node
->start
, &node
->end
) != 2) {
2413 p
= strchr(buf
, '\t');
2416 if (p
[strlen(p
) - 1] == '\n')
2417 p
[strlen(p
) - 1] = '\0';
2419 p
= (char *)"unknown";
2420 node
->symbol
= strdup(p
);
2421 if (!node
->symbol
) {
2425 pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
2426 node
->start
, node
->end
, node
->symbol
);
2430 kprobe_blacklist__delete(blacklist
);
2436 static struct kprobe_blacklist_node
*
2437 kprobe_blacklist__find_by_address(struct list_head
*blacklist
,
2438 unsigned long address
)
2440 struct kprobe_blacklist_node
*node
;
2442 list_for_each_entry(node
, blacklist
, list
) {
2443 if (node
->start
<= address
&& address
< node
->end
)
2450 static LIST_HEAD(kprobe_blacklist
);
2452 static void kprobe_blacklist__init(void)
2454 if (!list_empty(&kprobe_blacklist
))
2457 if (kprobe_blacklist__load(&kprobe_blacklist
) < 0)
2458 pr_debug("No kprobe blacklist support, ignored\n");
2461 static void kprobe_blacklist__release(void)
2463 kprobe_blacklist__delete(&kprobe_blacklist
);
2466 static bool kprobe_blacklist__listed(unsigned long address
)
2468 return !!kprobe_blacklist__find_by_address(&kprobe_blacklist
, address
);
2471 static int perf_probe_event__sprintf(const char *group
, const char *event
,
2472 struct perf_probe_event
*pev
,
2474 struct strbuf
*result
)
2479 if (asprintf(&buf
, "%s:%s", group
, event
) < 0)
2481 ret
= strbuf_addf(result
, " %-20s (on ", buf
);
2486 /* Synthesize only event probe point */
2487 buf
= synthesize_perf_probe_point(&pev
->point
);
2490 ret
= strbuf_addstr(result
, buf
);
2494 ret
= strbuf_addf(result
, " in %s", module
);
2496 if (!ret
&& pev
->nargs
> 0) {
2497 ret
= strbuf_add(result
, " with", 5);
2498 for (i
= 0; !ret
&& i
< pev
->nargs
; i
++) {
2499 buf
= synthesize_perf_probe_arg(&pev
->args
[i
]);
2502 ret
= strbuf_addf(result
, " %s", buf
);
2507 ret
= strbuf_addch(result
, ')');
2513 int show_perf_probe_event(const char *group
, const char *event
,
2514 struct perf_probe_event
*pev
,
2515 const char *module
, bool use_stdout
)
2517 struct strbuf buf
= STRBUF_INIT
;
2520 ret
= perf_probe_event__sprintf(group
, event
, pev
, module
, &buf
);
2523 printf("%s\n", buf
.buf
);
2525 pr_info("%s\n", buf
.buf
);
2527 strbuf_release(&buf
);
2532 static bool filter_probe_trace_event(struct probe_trace_event
*tev
,
2533 struct strfilter
*filter
)
2537 /* At first, check the event name itself */
2538 if (strfilter__compare(filter
, tev
->event
))
2541 /* Next, check the combination of name and group */
2542 if (e_snprintf(tmp
, 128, "%s:%s", tev
->group
, tev
->event
) < 0)
2544 return strfilter__compare(filter
, tmp
);
2547 static int __show_perf_probe_events(int fd
, bool is_kprobe
,
2548 struct strfilter
*filter
)
2551 struct probe_trace_event tev
;
2552 struct perf_probe_event pev
;
2553 struct strlist
*rawlist
;
2554 struct str_node
*ent
;
2556 memset(&tev
, 0, sizeof(tev
));
2557 memset(&pev
, 0, sizeof(pev
));
2559 rawlist
= probe_file__get_rawlist(fd
);
2563 strlist__for_each_entry(ent
, rawlist
) {
2564 ret
= parse_probe_trace_command(ent
->s
, &tev
);
2566 if (!filter_probe_trace_event(&tev
, filter
))
2568 ret
= convert_to_perf_probe_event(&tev
, &pev
,
2572 ret
= show_perf_probe_event(pev
.group
, pev
.event
,
2573 &pev
, tev
.point
.module
,
2577 clear_perf_probe_event(&pev
);
2578 clear_probe_trace_event(&tev
);
2582 strlist__delete(rawlist
);
2583 /* Cleanup cached debuginfo if needed */
2584 debuginfo_cache__exit();
2589 /* List up current perf-probe events */
2590 int show_perf_probe_events(struct strfilter
*filter
)
2592 int kp_fd
, up_fd
, ret
;
2596 if (probe_conf
.cache
)
2597 return probe_cache__show_all_caches(filter
);
2599 ret
= init_probe_symbol_maps(false);
2603 ret
= probe_file__open_both(&kp_fd
, &up_fd
, 0);
2608 ret
= __show_perf_probe_events(kp_fd
, true, filter
);
2609 if (up_fd
>= 0 && ret
>= 0)
2610 ret
= __show_perf_probe_events(up_fd
, false, filter
);
2615 exit_probe_symbol_maps();
2620 static int get_new_event_name(char *buf
, size_t len
, const char *base
,
2621 struct strlist
*namelist
, bool ret_event
,
2629 nbase
= strdup(base
);
2633 /* Cut off the dot suffixes (e.g. .const, .isra) and version suffixes */
2634 p
= strpbrk(nbase
, ".@");
2635 if (p
&& p
!= nbase
)
2638 /* Try no suffix number */
2639 ret
= e_snprintf(buf
, len
, "%s%s", nbase
, ret_event
? "__return" : "");
2641 pr_debug("snprintf() failed: %d\n", ret
);
2644 if (!strlist__has_entry(namelist
, buf
))
2647 if (!allow_suffix
) {
2648 pr_warning("Error: event \"%s\" already exists.\n"
2649 " Hint: Remove existing event by 'perf probe -d'\n"
2650 " or force duplicates by 'perf probe -f'\n"
2651 " or set 'force=yes' in BPF source.\n",
2657 /* Try to add suffix */
2658 for (i
= 1; i
< MAX_EVENT_INDEX
; i
++) {
2659 ret
= e_snprintf(buf
, len
, "%s_%d", nbase
, i
);
2661 pr_debug("snprintf() failed: %d\n", ret
);
2664 if (!strlist__has_entry(namelist
, buf
))
2667 if (i
== MAX_EVENT_INDEX
) {
2668 pr_warning("Too many events are on the same function.\n");
2675 /* Final validation */
2676 if (ret
>= 0 && !is_c_func_name(buf
)) {
2677 pr_warning("Internal error: \"%s\" is an invalid event name.\n",
2685 /* Warn if the current kernel's uprobe implementation is old */
2686 static void warn_uprobe_event_compat(struct probe_trace_event
*tev
)
2689 char *buf
= synthesize_probe_trace_command(tev
);
2690 struct probe_trace_point
*tp
= &tev
->point
;
2692 if (tp
->ref_ctr_offset
&& !uprobe_ref_ctr_is_supported()) {
2693 pr_warning("A semaphore is associated with %s:%s and "
2694 "seems your kernel doesn't support it.\n",
2695 tev
->group
, tev
->event
);
2698 /* Old uprobe event doesn't support memory dereference */
2699 if (!tev
->uprobes
|| tev
->nargs
== 0 || !buf
)
2702 for (i
= 0; i
< tev
->nargs
; i
++)
2703 if (strglobmatch(tev
->args
[i
].value
, "[$@+-]*")) {
2704 pr_warning("Please upgrade your kernel to at least "
2705 "3.14 to have access to feature %s\n",
2706 tev
->args
[i
].value
);
2713 /* Set new name from original perf_probe_event and namelist */
2714 static int probe_trace_event__set_name(struct probe_trace_event
*tev
,
2715 struct perf_probe_event
*pev
,
2716 struct strlist
*namelist
,
2719 const char *event
, *group
;
2723 /* If probe_event or trace_event already have the name, reuse it */
2724 if (pev
->event
&& !pev
->sdt
)
2726 else if (tev
->event
)
2729 /* Or generate new one from probe point */
2730 if (pev
->point
.function
&&
2731 (strncmp(pev
->point
.function
, "0x", 2) != 0) &&
2732 !strisglob(pev
->point
.function
))
2733 event
= pev
->point
.function
;
2735 event
= tev
->point
.realname
;
2737 if (pev
->group
&& !pev
->sdt
)
2739 else if (tev
->group
)
2742 group
= PERFPROBE_GROUP
;
2744 /* Get an unused new event name */
2745 ret
= get_new_event_name(buf
, 64, event
, namelist
,
2746 tev
->point
.retprobe
, allow_suffix
);
2752 tev
->event
= strdup(event
);
2753 tev
->group
= strdup(group
);
2754 if (tev
->event
== NULL
|| tev
->group
== NULL
)
2758 * Add new event name to namelist if multiprobe event is NOT
2759 * supported, since we have to use new event name for following
2760 * probes in that case.
2762 if (!multiprobe_event_is_supported())
2763 strlist__add(namelist
, event
);
2767 static int __open_probe_file_and_namelist(bool uprobe
,
2768 struct strlist
**namelist
)
2772 fd
= probe_file__open(PF_FL_RW
| (uprobe
? PF_FL_UPROBE
: 0));
2776 /* Get current event names */
2777 *namelist
= probe_file__get_namelist(fd
);
2779 pr_debug("Failed to get current event list.\n");
2786 static int __add_probe_trace_events(struct perf_probe_event
*pev
,
2787 struct probe_trace_event
*tevs
,
2788 int ntevs
, bool allow_suffix
)
2790 int i
, fd
[2] = {-1, -1}, up
, ret
;
2791 struct probe_trace_event
*tev
= NULL
;
2792 struct probe_cache
*cache
= NULL
;
2793 struct strlist
*namelist
[2] = {NULL
, NULL
};
2794 struct nscookie nsc
;
2796 up
= pev
->uprobes
? 1 : 0;
2797 fd
[up
] = __open_probe_file_and_namelist(up
, &namelist
[up
]);
2802 for (i
= 0; i
< ntevs
; i
++) {
2804 up
= tev
->uprobes
? 1 : 0;
2805 if (fd
[up
] == -1) { /* Open the kprobe/uprobe_events */
2806 fd
[up
] = __open_probe_file_and_namelist(up
,
2811 /* Skip if the symbol is out of .text or blacklisted */
2812 if (!tev
->point
.symbol
&& !pev
->uprobes
)
2815 /* Set new name for tev (and update namelist) */
2816 ret
= probe_trace_event__set_name(tev
, pev
, namelist
[up
],
2821 nsinfo__mountns_enter(pev
->nsi
, &nsc
);
2822 ret
= probe_file__add_event(fd
[up
], tev
);
2823 nsinfo__mountns_exit(&nsc
);
2828 * Probes after the first probe which comes from same
2829 * user input are always allowed to add suffix, because
2830 * there might be several addresses corresponding to
2833 allow_suffix
= true;
2835 if (ret
== -EINVAL
&& pev
->uprobes
)
2836 warn_uprobe_event_compat(tev
);
2837 if (ret
== 0 && probe_conf
.cache
) {
2838 cache
= probe_cache__new(pev
->target
, pev
->nsi
);
2840 probe_cache__add_entry(cache
, pev
, tevs
, ntevs
) < 0 ||
2841 probe_cache__commit(cache
) < 0)
2842 pr_warning("Failed to add event to probe cache\n");
2843 probe_cache__delete(cache
);
2847 for (up
= 0; up
< 2; up
++) {
2848 strlist__delete(namelist
[up
]);
2855 static int find_probe_functions(struct map
*map
, char *name
,
2856 struct symbol
**syms
)
2860 struct rb_node
*tmp
;
2861 const char *norm
, *ver
;
2863 bool cut_version
= true;
2865 if (map__load(map
) < 0)
2868 /* If user gives a version, don't cut off the version from symbols */
2869 if (strchr(name
, '@'))
2870 cut_version
= false;
2872 map__for_each_symbol(map
, sym
, tmp
) {
2873 norm
= arch__normalize_symbol_name(sym
->name
);
2878 /* We don't care about default symbol or not */
2879 ver
= strchr(norm
, '@');
2881 buf
= strndup(norm
, ver
- norm
);
2888 if (strglobmatch(norm
, name
)) {
2890 if (syms
&& found
< probe_conf
.max_probes
)
2891 syms
[found
- 1] = sym
;
2900 void __weak
arch__fix_tev_from_maps(struct perf_probe_event
*pev __maybe_unused
,
2901 struct probe_trace_event
*tev __maybe_unused
,
2902 struct map
*map __maybe_unused
,
2903 struct symbol
*sym __maybe_unused
) { }
2906 * Find probe function addresses from map.
2907 * Return an error or the number of found probe_trace_event
2909 static int find_probe_trace_events_from_map(struct perf_probe_event
*pev
,
2910 struct probe_trace_event
**tevs
)
2912 struct map
*map
= NULL
;
2913 struct ref_reloc_sym
*reloc_sym
= NULL
;
2915 struct symbol
**syms
= NULL
;
2916 struct probe_trace_event
*tev
;
2917 struct perf_probe_point
*pp
= &pev
->point
;
2918 struct probe_trace_point
*tp
;
2919 int num_matched_functions
;
2920 int ret
, i
, j
, skipped
= 0;
2923 map
= get_target_map(pev
->target
, pev
->nsi
, pev
->uprobes
);
2929 syms
= malloc(sizeof(struct symbol
*) * probe_conf
.max_probes
);
2936 * Load matched symbols: Since the different local symbols may have
2937 * same name but different addresses, this lists all the symbols.
2939 num_matched_functions
= find_probe_functions(map
, pp
->function
, syms
);
2940 if (num_matched_functions
<= 0) {
2941 pr_err("Failed to find symbol %s in %s\n", pp
->function
,
2942 pev
->target
? : "kernel");
2945 } else if (num_matched_functions
> probe_conf
.max_probes
) {
2946 pr_err("Too many functions matched in %s\n",
2947 pev
->target
? : "kernel");
2952 /* Note that the symbols in the kmodule are not relocated */
2953 if (!pev
->uprobes
&& !pev
->target
&&
2954 (!pp
->retprobe
|| kretprobe_offset_is_supported())) {
2955 reloc_sym
= kernel_get_ref_reloc_sym(NULL
);
2957 pr_warning("Relocated base symbol is not found!\n");
2963 /* Setup result trace-probe-events */
2964 *tevs
= zalloc(sizeof(*tev
) * num_matched_functions
);
2972 for (j
= 0; j
< num_matched_functions
; j
++) {
2975 /* There can be duplicated symbols in the map */
2976 for (i
= 0; i
< j
; i
++)
2977 if (sym
->start
== syms
[i
]->start
) {
2978 pr_debug("Found duplicated symbol %s @ %" PRIx64
"\n",
2979 sym
->name
, sym
->start
);
2985 tev
= (*tevs
) + ret
;
2987 if (ret
== num_matched_functions
) {
2988 pr_warning("Too many symbols are listed. Skip it.\n");
2993 if (pp
->offset
> sym
->end
- sym
->start
) {
2994 pr_warning("Offset %ld is bigger than the size of %s\n",
2995 pp
->offset
, sym
->name
);
2999 /* Add one probe point */
3000 tp
->address
= map
->unmap_ip(map
, sym
->start
) + pp
->offset
;
3002 /* Check the kprobe (not in module) is within .text */
3003 if (!pev
->uprobes
&& !pev
->target
&&
3004 kprobe_warn_out_range(sym
->name
, tp
->address
)) {
3005 tp
->symbol
= NULL
; /* Skip it */
3007 } else if (reloc_sym
) {
3008 tp
->symbol
= strdup_or_goto(reloc_sym
->name
, nomem_out
);
3009 tp
->offset
= tp
->address
- reloc_sym
->addr
;
3011 tp
->symbol
= strdup_or_goto(sym
->name
, nomem_out
);
3012 tp
->offset
= pp
->offset
;
3014 tp
->realname
= strdup_or_goto(sym
->name
, nomem_out
);
3016 tp
->retprobe
= pp
->retprobe
;
3019 tev
->point
.module
= strdup_or_goto(pev
->target
,
3022 mod_name
= find_module_name(pev
->target
);
3024 strdup(mod_name
? mod_name
: pev
->target
);
3026 if (!tev
->point
.module
)
3030 tev
->uprobes
= pev
->uprobes
;
3031 tev
->nargs
= pev
->nargs
;
3033 tev
->args
= zalloc(sizeof(struct probe_trace_arg
) *
3035 if (tev
->args
== NULL
)
3038 for (i
= 0; i
< tev
->nargs
; i
++) {
3039 if (pev
->args
[i
].name
)
3041 strdup_or_goto(pev
->args
[i
].name
,
3044 tev
->args
[i
].value
= strdup_or_goto(pev
->args
[i
].var
,
3046 if (pev
->args
[i
].type
)
3048 strdup_or_goto(pev
->args
[i
].type
,
3051 arch__fix_tev_from_maps(pev
, tev
, map
, sym
);
3053 if (ret
== skipped
) {
3066 clear_probe_trace_events(*tevs
, num_matched_functions
);
3071 static int try_to_find_absolute_address(struct perf_probe_event
*pev
,
3072 struct probe_trace_event
**tevs
)
3074 struct perf_probe_point
*pp
= &pev
->point
;
3075 struct probe_trace_event
*tev
;
3076 struct probe_trace_point
*tp
;
3079 if (!(pev
->point
.function
&& !strncmp(pev
->point
.function
, "0x", 2)))
3081 if (perf_probe_event_need_dwarf(pev
))
3085 * This is 'perf probe /lib/libc.so 0xabcd'. Try to probe at
3088 * Only one tev can be generated by this.
3090 *tevs
= zalloc(sizeof(*tev
));
3098 * Don't use tp->offset, use address directly, because
3099 * in synthesize_probe_trace_command() address cannot be
3102 tp
->address
= pev
->point
.abs_address
;
3103 tp
->retprobe
= pp
->retprobe
;
3104 tev
->uprobes
= pev
->uprobes
;
3108 * Give it a '0x' leading symbol name.
3109 * In __add_probe_trace_events, a NULL symbol is interpreted as
3112 if (asprintf(&tp
->symbol
, "0x%lx", tp
->address
) < 0)
3115 /* For kprobe, check range */
3116 if ((!tev
->uprobes
) &&
3117 (kprobe_warn_out_range(tev
->point
.symbol
,
3118 tev
->point
.address
))) {
3123 if (asprintf(&tp
->realname
, "abs_%lx", tp
->address
) < 0)
3127 tp
->module
= strdup(pev
->target
);
3133 tev
->group
= strdup(pev
->group
);
3139 tev
->event
= strdup(pev
->event
);
3144 tev
->nargs
= pev
->nargs
;
3145 tev
->args
= zalloc(sizeof(struct probe_trace_arg
) * tev
->nargs
);
3149 for (i
= 0; i
< tev
->nargs
; i
++)
3150 copy_to_probe_trace_arg(&tev
->args
[i
], &pev
->args
[i
]);
3155 clear_probe_trace_events(*tevs
, 1);
3160 /* Concatinate two arrays */
3161 static void *memcat(void *a
, size_t sz_a
, void *b
, size_t sz_b
)
3165 ret
= malloc(sz_a
+ sz_b
);
3167 memcpy(ret
, a
, sz_a
);
3168 memcpy(ret
+ sz_a
, b
, sz_b
);
3174 concat_probe_trace_events(struct probe_trace_event
**tevs
, int *ntevs
,
3175 struct probe_trace_event
**tevs2
, int ntevs2
)
3177 struct probe_trace_event
*new_tevs
;
3187 if (*ntevs
+ ntevs2
> probe_conf
.max_probes
)
3190 /* Concatinate the array of probe_trace_event */
3191 new_tevs
= memcat(*tevs
, (*ntevs
) * sizeof(**tevs
),
3192 *tevs2
, ntevs2
* sizeof(**tevs2
));
3202 clear_probe_trace_events(*tevs2
, ntevs2
);
3209 * Try to find probe_trace_event from given probe caches. Return the number
3210 * of cached events found, if an error occurs return the error.
3212 static int find_cached_events(struct perf_probe_event
*pev
,
3213 struct probe_trace_event
**tevs
,
3216 struct probe_cache
*cache
;
3217 struct probe_cache_entry
*entry
;
3218 struct probe_trace_event
*tmp_tevs
= NULL
;
3222 cache
= probe_cache__new(target
, pev
->nsi
);
3223 /* Return 0 ("not found") if the target has no probe cache. */
3227 for_each_probe_cache_entry(entry
, cache
) {
3228 /* Skip the cache entry which has no name */
3229 if (!entry
->pev
.event
|| !entry
->pev
.group
)
3231 if ((!pev
->group
|| strglobmatch(entry
->pev
.group
, pev
->group
)) &&
3232 strglobmatch(entry
->pev
.event
, pev
->event
)) {
3233 ret
= probe_cache_entry__get_event(entry
, &tmp_tevs
);
3235 ret
= concat_probe_trace_events(tevs
, &ntevs
,
3241 probe_cache__delete(cache
);
3243 clear_probe_trace_events(*tevs
, ntevs
);
3247 if (ntevs
> 0 && target
&& target
[0] == '/')
3248 pev
->uprobes
= true;
3254 /* Try to find probe_trace_event from all probe caches */
3255 static int find_cached_events_all(struct perf_probe_event
*pev
,
3256 struct probe_trace_event
**tevs
)
3258 struct probe_trace_event
*tmp_tevs
= NULL
;
3259 struct strlist
*bidlist
;
3260 struct str_node
*nd
;
3265 /* Get the buildid list of all valid caches */
3266 bidlist
= build_id_cache__list_all(true);
3269 pr_debug("Failed to get buildids: %d\n", ret
);
3274 strlist__for_each_entry(nd
, bidlist
) {
3275 pathname
= build_id_cache__origname(nd
->s
);
3276 ret
= find_cached_events(pev
, &tmp_tevs
, pathname
);
3277 /* In the case of cnt == 0, we just skip it */
3279 ret
= concat_probe_trace_events(tevs
, &ntevs
,
3285 strlist__delete(bidlist
);
3288 clear_probe_trace_events(*tevs
, ntevs
);
3296 static int find_probe_trace_events_from_cache(struct perf_probe_event
*pev
,
3297 struct probe_trace_event
**tevs
)
3299 struct probe_cache
*cache
;
3300 struct probe_cache_entry
*entry
;
3301 struct probe_trace_event
*tev
;
3302 struct str_node
*node
;
3306 /* For SDT/cached events, we use special search functions */
3308 return find_cached_events_all(pev
, tevs
);
3310 return find_cached_events(pev
, tevs
, pev
->target
);
3312 cache
= probe_cache__new(pev
->target
, pev
->nsi
);
3316 entry
= probe_cache__find(cache
, pev
);
3318 /* SDT must be in the cache */
3319 ret
= pev
->sdt
? -ENOENT
: 0;
3323 ret
= strlist__nr_entries(entry
->tevlist
);
3324 if (ret
> probe_conf
.max_probes
) {
3325 pr_debug("Too many entries matched in the cache of %s\n",
3326 pev
->target
? : "kernel");
3331 *tevs
= zalloc(ret
* sizeof(*tev
));
3338 strlist__for_each_entry(node
, entry
->tevlist
) {
3339 tev
= &(*tevs
)[i
++];
3340 ret
= parse_probe_trace_command(node
->s
, tev
);
3343 /* Set the uprobes attribute as same as original */
3344 tev
->uprobes
= pev
->uprobes
;
3349 probe_cache__delete(cache
);
3353 static int convert_to_probe_trace_events(struct perf_probe_event
*pev
,
3354 struct probe_trace_event
**tevs
)
3358 if (!pev
->group
&& !pev
->sdt
) {
3359 /* Set group name if not given */
3360 if (!pev
->uprobes
) {
3361 pev
->group
= strdup(PERFPROBE_GROUP
);
3362 ret
= pev
->group
? 0 : -ENOMEM
;
3364 ret
= convert_exec_to_group(pev
->target
, &pev
->group
);
3366 pr_warning("Failed to make a group name.\n");
3371 ret
= try_to_find_absolute_address(pev
, tevs
);
3375 /* At first, we need to lookup cache entry */
3376 ret
= find_probe_trace_events_from_cache(pev
, tevs
);
3377 if (ret
> 0 || pev
->sdt
) /* SDT can be found only in the cache */
3378 return ret
== 0 ? -ENOENT
: ret
; /* Found in probe cache */
3380 /* Convert perf_probe_event with debuginfo */
3381 ret
= try_to_find_probe_trace_events(pev
, tevs
);
3383 return ret
; /* Found in debuginfo or got an error */
3385 return find_probe_trace_events_from_map(pev
, tevs
);
3388 int convert_perf_probe_events(struct perf_probe_event
*pevs
, int npevs
)
3392 /* Loop 1: convert all events */
3393 for (i
= 0; i
< npevs
; i
++) {
3394 /* Init kprobe blacklist if needed */
3395 if (!pevs
[i
].uprobes
)
3396 kprobe_blacklist__init();
3397 /* Convert with or without debuginfo */
3398 ret
= convert_to_probe_trace_events(&pevs
[i
], &pevs
[i
].tevs
);
3401 pevs
[i
].ntevs
= ret
;
3403 /* This just release blacklist only if allocated */
3404 kprobe_blacklist__release();
3409 static int show_probe_trace_event(struct probe_trace_event
*tev
)
3411 char *buf
= synthesize_probe_trace_command(tev
);
3414 pr_debug("Failed to synthesize probe trace event.\n");
3418 /* Showing definition always go stdout */
3419 printf("%s\n", buf
);
3425 int show_probe_trace_events(struct perf_probe_event
*pevs
, int npevs
)
3427 struct strlist
*namelist
= strlist__new(NULL
, NULL
);
3428 struct probe_trace_event
*tev
;
3429 struct perf_probe_event
*pev
;
3435 for (j
= 0; j
< npevs
&& !ret
; j
++) {
3437 for (i
= 0; i
< pev
->ntevs
&& !ret
; i
++) {
3438 tev
= &pev
->tevs
[i
];
3439 /* Skip if the symbol is out of .text or blacklisted */
3440 if (!tev
->point
.symbol
&& !pev
->uprobes
)
3443 /* Set new name for tev (and update namelist) */
3444 ret
= probe_trace_event__set_name(tev
, pev
,
3447 ret
= show_probe_trace_event(tev
);
3450 strlist__delete(namelist
);
3455 int apply_perf_probe_events(struct perf_probe_event
*pevs
, int npevs
)
3459 /* Loop 2: add all events */
3460 for (i
= 0; i
< npevs
; i
++) {
3461 ret
= __add_probe_trace_events(&pevs
[i
], pevs
[i
].tevs
,
3463 probe_conf
.force_add
);
3470 void cleanup_perf_probe_events(struct perf_probe_event
*pevs
, int npevs
)
3473 struct perf_probe_event
*pev
;
3475 /* Loop 3: cleanup and free trace events */
3476 for (i
= 0; i
< npevs
; i
++) {
3478 for (j
= 0; j
< pevs
[i
].ntevs
; j
++)
3479 clear_probe_trace_event(&pevs
[i
].tevs
[j
]);
3480 zfree(&pevs
[i
].tevs
);
3482 nsinfo__zput(pev
->nsi
);
3483 clear_perf_probe_event(&pevs
[i
]);
3487 int add_perf_probe_events(struct perf_probe_event
*pevs
, int npevs
)
3491 ret
= init_probe_symbol_maps(pevs
->uprobes
);
3495 ret
= convert_perf_probe_events(pevs
, npevs
);
3497 ret
= apply_perf_probe_events(pevs
, npevs
);
3499 cleanup_perf_probe_events(pevs
, npevs
);
3501 exit_probe_symbol_maps();
3505 int del_perf_probe_events(struct strfilter
*filter
)
3507 int ret
, ret2
, ufd
= -1, kfd
= -1;
3508 char *str
= strfilter__string(filter
);
3513 /* Get current event names */
3514 ret
= probe_file__open_both(&kfd
, &ufd
, PF_FL_RW
);
3518 ret
= probe_file__del_events(kfd
, filter
);
3519 if (ret
< 0 && ret
!= -ENOENT
)
3522 ret2
= probe_file__del_events(ufd
, filter
);
3523 if (ret2
< 0 && ret2
!= -ENOENT
) {
3540 int show_available_funcs(const char *target
, struct nsinfo
*nsi
,
3541 struct strfilter
*_filter
, bool user
)
3547 ret
= init_probe_symbol_maps(user
);
3551 /* Get a symbol map */
3552 map
= get_target_map(target
, nsi
, user
);
3554 pr_err("Failed to get a map for %s\n", (target
) ? : "kernel");
3558 ret
= map__load(map
);
3561 char *str
= strfilter__string(_filter
);
3562 pr_err("Failed to find symbols matched to \"%s\"\n",
3566 pr_err("Failed to load symbols in %s\n",
3567 (target
) ? : "kernel");
3570 if (!dso__sorted_by_name(map
->dso
))
3571 dso__sort_by_name(map
->dso
);
3573 /* Show all (filtered) symbols */
3576 for (nd
= rb_first_cached(&map
->dso
->symbol_names
); nd
;
3578 struct symbol_name_rb_node
*pos
= rb_entry(nd
, struct symbol_name_rb_node
, rb_node
);
3580 if (strfilter__compare(_filter
, pos
->sym
.name
))
3581 printf("%s\n", pos
->sym
.name
);
3585 exit_probe_symbol_maps();
3590 int copy_to_probe_trace_arg(struct probe_trace_arg
*tvar
,
3591 struct perf_probe_arg
*pvar
)
3593 tvar
->value
= strdup(pvar
->var
);
3594 if (tvar
->value
== NULL
)
3597 tvar
->type
= strdup(pvar
->type
);
3598 if (tvar
->type
== NULL
)
3602 tvar
->name
= strdup(pvar
->name
);
3603 if (tvar
->name
== NULL
)