2 * probe-event.c : perf-probe definition to probe_events format converter
4 * Written by Masami Hiramatsu <mhiramat@redhat.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #include <sys/utsname.h>
24 #include <sys/types.h>
38 #include "namespaces.h"
40 #include "strfilter.h"
45 #include "map_groups.h"
48 #include <api/fs/fs.h>
49 #include "trace-event.h" /* For __maybe_unused */
50 #include "probe-event.h"
51 #include "probe-finder.h"
52 #include "probe-file.h"
56 #include "sane_ctype.h"
58 #define PERFPROBE_GROUP "probe"
60 bool probe_event_dry_run
; /* Dry run flag */
61 struct probe_conf probe_conf
;
63 #define semantic_error(msg ...) pr_err("Semantic error :" msg)
65 int e_snprintf(char *str
, size_t size
, const char *format
, ...)
70 ret
= vsnprintf(str
, size
, format
, ap
);
77 static struct machine
*host_machine
;
79 /* Initialize symbol maps and path of vmlinux/modules */
80 int init_probe_symbol_maps(bool user_only
)
84 symbol_conf
.sort_by_name
= true;
85 symbol_conf
.allow_aliases
= true;
86 ret
= symbol__init(NULL
);
88 pr_debug("Failed to init symbol map.\n");
92 if (host_machine
|| user_only
) /* already initialized */
95 if (symbol_conf
.vmlinux_name
)
96 pr_debug("Use vmlinux: %s\n", symbol_conf
.vmlinux_name
);
98 host_machine
= machine__new_host();
100 pr_debug("machine__new_host() failed.\n");
106 pr_warning("Failed to init vmlinux path.\n");
110 void exit_probe_symbol_maps(void)
112 machine__delete(host_machine
);
117 static struct ref_reloc_sym
*kernel_get_ref_reloc_sym(void)
119 /* kmap->ref_reloc_sym should be set if host_machine is initialized */
121 struct map
*map
= machine__kernel_map(host_machine
);
123 if (map__load(map
) < 0)
126 kmap
= map__kmap(map
);
129 return kmap
->ref_reloc_sym
;
132 static int kernel_get_symbol_address_by_name(const char *name
, u64
*addr
,
133 bool reloc
, bool reladdr
)
135 struct ref_reloc_sym
*reloc_sym
;
139 /* ref_reloc_sym is just a label. Need a special fix*/
140 reloc_sym
= kernel_get_ref_reloc_sym();
141 if (reloc_sym
&& strcmp(name
, reloc_sym
->name
) == 0)
142 *addr
= (reloc
) ? reloc_sym
->addr
: reloc_sym
->unrelocated_addr
;
144 sym
= machine__find_kernel_symbol_by_name(host_machine
, name
, &map
);
147 *addr
= map
->unmap_ip(map
, sym
->start
) -
148 ((reloc
) ? 0 : map
->reloc
) -
149 ((reladdr
) ? map
->start
: 0);
154 static struct map
*kernel_get_module_map(const char *module
)
156 struct maps
*maps
= machine__kernel_maps(host_machine
);
159 /* A file path -- this is an offline module */
160 if (module
&& strchr(module
, '/'))
161 return dso__new_map(module
);
164 pos
= machine__kernel_map(host_machine
);
165 return map__get(pos
);
168 for (pos
= maps__first(maps
); pos
; pos
= map__next(pos
)) {
169 /* short_name is "[module]" */
170 if (strncmp(pos
->dso
->short_name
+ 1, module
,
171 pos
->dso
->short_name_len
- 2) == 0 &&
172 module
[pos
->dso
->short_name_len
- 2] == '\0') {
173 return map__get(pos
);
179 struct map
*get_target_map(const char *target
, struct nsinfo
*nsi
, bool user
)
181 /* Init maps of given executable or kernel */
185 map
= dso__new_map(target
);
187 map
->dso
->nsinfo
= nsinfo__get(nsi
);
190 return kernel_get_module_map(target
);
194 static int convert_exec_to_group(const char *exec
, char **result
)
196 char *ptr1
, *ptr2
, *exec_copy
;
200 exec_copy
= strdup(exec
);
204 ptr1
= basename(exec_copy
);
210 for (ptr2
= ptr1
; *ptr2
!= '\0'; ptr2
++) {
211 if (!isalnum(*ptr2
) && *ptr2
!= '_') {
217 ret
= e_snprintf(buf
, 64, "%s_%s", PERFPROBE_GROUP
, ptr1
);
221 *result
= strdup(buf
);
222 ret
= *result
? 0 : -ENOMEM
;
229 static void clear_perf_probe_point(struct perf_probe_point
*pp
)
236 static void clear_probe_trace_events(struct probe_trace_event
*tevs
, int ntevs
)
240 for (i
= 0; i
< ntevs
; i
++)
241 clear_probe_trace_event(tevs
+ i
);
244 static bool kprobe_blacklist__listed(unsigned long address
);
245 static bool kprobe_warn_out_range(const char *symbol
, unsigned long address
)
250 /* Get the address of _etext for checking non-probable text symbol */
251 ret
= kernel_get_symbol_address_by_name("_etext", &etext_addr
,
254 if (ret
== 0 && etext_addr
< address
)
255 pr_warning("%s is out of .text, skip it.\n", symbol
);
256 else if (kprobe_blacklist__listed(address
))
257 pr_warning("%s is blacklisted function, skip it.\n", symbol
);
265 * @module can be module name of module file path. In case of path,
266 * inspect elf and find out what is actual module name.
267 * Caller has to free mod_name after using it.
269 static char *find_module_name(const char *module
)
277 char *mod_name
= NULL
;
280 fd
= open(module
, O_RDONLY
);
284 elf
= elf_begin(fd
, PERF_ELF_C_READ_MMAP
, NULL
);
288 if (gelf_getehdr(elf
, &ehdr
) == NULL
)
291 sec
= elf_section_by_name(elf
, &ehdr
, &shdr
,
292 ".gnu.linkonce.this_module", NULL
);
296 data
= elf_getdata(sec
, NULL
);
297 if (!data
|| !data
->d_buf
)
302 * '.gnu.linkonce.this_module' section of kernel module elf directly
303 * maps to 'struct module' from linux/module.h. This section contains
304 * actual module name which will be used by kernel after loading it.
305 * But, we cannot use 'struct module' here since linux/module.h is not
306 * exposed to user-space. Offset of 'name' has remained same from long
307 * time, so hardcoding it here.
309 if (ehdr
.e_ident
[EI_CLASS
] == ELFCLASS32
)
311 else /* expect ELFCLASS64 by default */
314 mod_name
= strdup((char *)data
->d_buf
+ name_offset
);
323 #ifdef HAVE_DWARF_SUPPORT
325 static int kernel_get_module_dso(const char *module
, struct dso
**pdso
)
329 const char *vmlinux_name
;
333 char module_name
[128];
335 snprintf(module_name
, sizeof(module_name
), "[%s]", module
);
336 map
= map_groups__find_by_name(&host_machine
->kmaps
, module_name
);
341 pr_debug("Failed to find module %s.\n", module
);
345 map
= machine__kernel_map(host_machine
);
348 vmlinux_name
= symbol_conf
.vmlinux_name
;
351 ret
= dso__load_vmlinux(dso
, map
, vmlinux_name
, false);
353 ret
= dso__load_vmlinux_path(dso
, map
);
360 * Some binaries like glibc have special symbols which are on the symbol
361 * table, but not in the debuginfo. If we can find the address of the
362 * symbol from map, we can translate the address back to the probe point.
364 static int find_alternative_probe_point(struct debuginfo
*dinfo
,
365 struct perf_probe_point
*pp
,
366 struct perf_probe_point
*result
,
367 const char *target
, struct nsinfo
*nsi
,
370 struct map
*map
= NULL
;
375 /* This can work only for function-name based one */
376 if (!pp
->function
|| pp
->file
)
379 map
= get_target_map(target
, nsi
, uprobes
);
383 /* Find the address of given function */
384 map__for_each_symbol_by_name(map
, pp
->function
, sym
) {
386 address
= sym
->start
;
388 address
= map
->unmap_ip(map
, sym
->start
) - map
->reloc
;
395 pr_debug("Symbol %s address found : %" PRIx64
"\n",
396 pp
->function
, address
);
398 ret
= debuginfo__find_probe_point(dinfo
, (unsigned long)address
,
401 ret
= (!ret
) ? -ENOENT
: ret
;
403 result
->offset
+= pp
->offset
;
404 result
->line
+= pp
->line
;
405 result
->retprobe
= pp
->retprobe
;
415 static int get_alternative_probe_event(struct debuginfo
*dinfo
,
416 struct perf_probe_event
*pev
,
417 struct perf_probe_point
*tmp
)
421 memcpy(tmp
, &pev
->point
, sizeof(*tmp
));
422 memset(&pev
->point
, 0, sizeof(pev
->point
));
423 ret
= find_alternative_probe_point(dinfo
, tmp
, &pev
->point
, pev
->target
,
424 pev
->nsi
, pev
->uprobes
);
426 memcpy(&pev
->point
, tmp
, sizeof(*tmp
));
431 static int get_alternative_line_range(struct debuginfo
*dinfo
,
432 struct line_range
*lr
,
433 const char *target
, bool user
)
435 struct perf_probe_point pp
= { .function
= lr
->function
,
438 struct perf_probe_point result
;
441 memset(&result
, 0, sizeof(result
));
443 if (lr
->end
!= INT_MAX
)
444 len
= lr
->end
- lr
->start
;
445 ret
= find_alternative_probe_point(dinfo
, &pp
, &result
,
448 lr
->function
= result
.function
;
449 lr
->file
= result
.file
;
450 lr
->start
= result
.line
;
451 if (lr
->end
!= INT_MAX
)
452 lr
->end
= lr
->start
+ len
;
453 clear_perf_probe_point(&pp
);
458 /* Open new debuginfo of given module */
459 static struct debuginfo
*open_debuginfo(const char *module
, struct nsinfo
*nsi
,
462 const char *path
= module
;
463 char reason
[STRERR_BUFSIZE
];
464 struct debuginfo
*ret
= NULL
;
465 struct dso
*dso
= NULL
;
469 if (!module
|| !strchr(module
, '/')) {
470 err
= kernel_get_module_dso(module
, &dso
);
472 if (!dso
|| dso
->load_errno
== 0) {
473 if (!str_error_r(-err
, reason
, STRERR_BUFSIZE
))
474 strcpy(reason
, "(unknown)");
476 dso__strerror_load(dso
, reason
, STRERR_BUFSIZE
);
479 pr_err("Module %s is not loaded, please specify its full path name.\n", module
);
481 pr_err("Failed to find the path for the kernel: %s\n", reason
);
485 path
= dso
->long_name
;
487 nsinfo__mountns_enter(nsi
, &nsc
);
488 ret
= debuginfo__new(path
);
489 if (!ret
&& !silent
) {
490 pr_warning("The %s file has no debug information.\n", path
);
491 if (!module
|| !strtailcmp(path
, ".ko"))
492 pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, ");
494 pr_warning("Rebuild with -g, ");
495 pr_warning("or install an appropriate debuginfo package.\n");
497 nsinfo__mountns_exit(&nsc
);
501 /* For caching the last debuginfo */
502 static struct debuginfo
*debuginfo_cache
;
503 static char *debuginfo_cache_path
;
505 static struct debuginfo
*debuginfo_cache__open(const char *module
, bool silent
)
507 const char *path
= module
;
509 /* If the module is NULL, it should be the kernel. */
513 if (debuginfo_cache_path
&& !strcmp(debuginfo_cache_path
, path
))
516 /* Copy module path */
517 free(debuginfo_cache_path
);
518 debuginfo_cache_path
= strdup(path
);
519 if (!debuginfo_cache_path
) {
520 debuginfo__delete(debuginfo_cache
);
521 debuginfo_cache
= NULL
;
525 debuginfo_cache
= open_debuginfo(module
, NULL
, silent
);
526 if (!debuginfo_cache
)
527 zfree(&debuginfo_cache_path
);
529 return debuginfo_cache
;
532 static void debuginfo_cache__exit(void)
534 debuginfo__delete(debuginfo_cache
);
535 debuginfo_cache
= NULL
;
536 zfree(&debuginfo_cache_path
);
540 static int get_text_start_address(const char *exec
, unsigned long *address
,
546 int fd
, ret
= -ENOENT
;
549 nsinfo__mountns_enter(nsi
, &nsc
);
550 fd
= open(exec
, O_RDONLY
);
551 nsinfo__mountns_exit(&nsc
);
555 elf
= elf_begin(fd
, PERF_ELF_C_READ_MMAP
, NULL
);
561 if (gelf_getehdr(elf
, &ehdr
) == NULL
)
564 if (!elf_section_by_name(elf
, &ehdr
, &shdr
, ".text", NULL
))
567 *address
= shdr
.sh_addr
- shdr
.sh_offset
;
578 * Convert trace point to probe point with debuginfo
580 static int find_perf_probe_point_from_dwarf(struct probe_trace_point
*tp
,
581 struct perf_probe_point
*pp
,
584 struct debuginfo
*dinfo
= NULL
;
585 unsigned long stext
= 0;
586 u64 addr
= tp
->address
;
589 /* convert the address to dwarf address */
595 ret
= get_text_start_address(tp
->module
, &stext
, NULL
);
599 } else if (tp
->symbol
) {
600 /* If the module is given, this returns relative address */
601 ret
= kernel_get_symbol_address_by_name(tp
->symbol
, &addr
,
602 false, !!tp
->module
);
608 pr_debug("try to find information at %" PRIx64
" in %s\n", addr
,
609 tp
->module
? : "kernel");
611 dinfo
= debuginfo_cache__open(tp
->module
, verbose
<= 0);
613 ret
= debuginfo__find_probe_point(dinfo
,
614 (unsigned long)addr
, pp
);
619 pp
->retprobe
= tp
->retprobe
;
623 pr_debug("Failed to find corresponding probes from debuginfo.\n");
624 return ret
? : -ENOENT
;
627 /* Adjust symbol name and address */
628 static int post_process_probe_trace_point(struct probe_trace_point
*tp
,
629 struct map
*map
, unsigned long offs
)
632 u64 addr
= tp
->address
- offs
;
634 sym
= map__find_symbol(map
, addr
);
638 if (strcmp(sym
->name
, tp
->symbol
)) {
639 /* If we have no realname, use symbol for it */
641 tp
->realname
= tp
->symbol
;
644 tp
->symbol
= strdup(sym
->name
);
648 tp
->offset
= addr
- sym
->start
;
655 * Rename DWARF symbols to ELF symbols -- gcc sometimes optimizes functions
656 * and generate new symbols with suffixes such as .constprop.N or .isra.N
657 * etc. Since those symbols are not recorded in DWARF, we have to find
658 * correct generated symbols from offline ELF binary.
659 * For online kernel or uprobes we don't need this because those are
660 * rebased on _text, or already a section relative address.
663 post_process_offline_probe_trace_events(struct probe_trace_event
*tevs
,
664 int ntevs
, const char *pathname
)
667 unsigned long stext
= 0;
670 /* Prepare a map for offline binary */
671 map
= dso__new_map(pathname
);
672 if (!map
|| get_text_start_address(pathname
, &stext
, NULL
) < 0) {
673 pr_warning("Failed to get ELF symbols for %s\n", pathname
);
677 for (i
= 0; i
< ntevs
; i
++) {
678 ret
= post_process_probe_trace_point(&tevs
[i
].point
,
688 static int add_exec_to_probe_trace_events(struct probe_trace_event
*tevs
,
689 int ntevs
, const char *exec
,
693 unsigned long stext
= 0;
698 ret
= get_text_start_address(exec
, &stext
, nsi
);
702 for (i
= 0; i
< ntevs
&& ret
>= 0; i
++) {
703 /* point.address is the address of point.symbol + point.offset */
704 tevs
[i
].point
.address
-= stext
;
705 tevs
[i
].point
.module
= strdup(exec
);
706 if (!tevs
[i
].point
.module
) {
710 tevs
[i
].uprobes
= true;
717 post_process_module_probe_trace_events(struct probe_trace_event
*tevs
,
718 int ntevs
, const char *module
,
719 struct debuginfo
*dinfo
)
721 Dwarf_Addr text_offs
= 0;
723 char *mod_name
= NULL
;
729 map
= get_target_map(module
, NULL
, false);
730 if (!map
|| debuginfo__get_text_offset(dinfo
, &text_offs
, true) < 0) {
731 pr_warning("Failed to get ELF symbols for %s\n", module
);
735 mod_name
= find_module_name(module
);
736 for (i
= 0; i
< ntevs
; i
++) {
737 ret
= post_process_probe_trace_point(&tevs
[i
].point
,
738 map
, (unsigned long)text_offs
);
741 tevs
[i
].point
.module
=
742 strdup(mod_name
? mod_name
: module
);
743 if (!tevs
[i
].point
.module
) {
756 post_process_kernel_probe_trace_events(struct probe_trace_event
*tevs
,
759 struct ref_reloc_sym
*reloc_sym
;
763 /* Skip post process if the target is an offline kernel */
764 if (symbol_conf
.ignore_vmlinux_buildid
)
765 return post_process_offline_probe_trace_events(tevs
, ntevs
,
766 symbol_conf
.vmlinux_name
);
768 reloc_sym
= kernel_get_ref_reloc_sym();
770 pr_warning("Relocated base symbol is not found!\n");
774 for (i
= 0; i
< ntevs
; i
++) {
775 if (!tevs
[i
].point
.address
)
777 if (tevs
[i
].point
.retprobe
&& !kretprobe_offset_is_supported())
779 /* If we found a wrong one, mark it by NULL symbol */
780 if (kprobe_warn_out_range(tevs
[i
].point
.symbol
,
781 tevs
[i
].point
.address
)) {
785 tmp
= strdup(reloc_sym
->name
);
789 /* If we have no realname, use symbol for it */
790 if (!tevs
[i
].point
.realname
)
791 tevs
[i
].point
.realname
= tevs
[i
].point
.symbol
;
793 free(tevs
[i
].point
.symbol
);
794 tevs
[i
].point
.symbol
= tmp
;
795 tevs
[i
].point
.offset
= tevs
[i
].point
.address
-
796 reloc_sym
->unrelocated_addr
;
802 arch__post_process_probe_trace_events(struct perf_probe_event
*pev __maybe_unused
,
803 int ntevs __maybe_unused
)
807 /* Post processing the probe events */
808 static int post_process_probe_trace_events(struct perf_probe_event
*pev
,
809 struct probe_trace_event
*tevs
,
810 int ntevs
, const char *module
,
811 bool uprobe
, struct debuginfo
*dinfo
)
816 ret
= add_exec_to_probe_trace_events(tevs
, ntevs
, module
,
819 /* Currently ref_reloc_sym based probe is not for drivers */
820 ret
= post_process_module_probe_trace_events(tevs
, ntevs
,
823 ret
= post_process_kernel_probe_trace_events(tevs
, ntevs
);
826 arch__post_process_probe_trace_events(pev
, ntevs
);
831 /* Try to find perf_probe_event with debuginfo */
832 static int try_to_find_probe_trace_events(struct perf_probe_event
*pev
,
833 struct probe_trace_event
**tevs
)
835 bool need_dwarf
= perf_probe_event_need_dwarf(pev
);
836 struct perf_probe_point tmp
;
837 struct debuginfo
*dinfo
;
840 dinfo
= open_debuginfo(pev
->target
, pev
->nsi
, !need_dwarf
);
844 pr_debug("Could not open debuginfo. Try to use symbols.\n");
848 pr_debug("Try to find probe point from debuginfo.\n");
849 /* Searching trace events corresponding to a probe event */
850 ntevs
= debuginfo__find_trace_events(dinfo
, pev
, tevs
);
852 if (ntevs
== 0) { /* Not found, retry with an alternative */
853 ret
= get_alternative_probe_event(dinfo
, pev
, &tmp
);
855 ntevs
= debuginfo__find_trace_events(dinfo
, pev
, tevs
);
857 * Write back to the original probe_event for
858 * setting appropriate (user given) event name
860 clear_perf_probe_point(&pev
->point
);
861 memcpy(&pev
->point
, &tmp
, sizeof(tmp
));
865 if (ntevs
> 0) { /* Succeeded to find trace events */
866 pr_debug("Found %d probe_trace_events.\n", ntevs
);
867 ret
= post_process_probe_trace_events(pev
, *tevs
, ntevs
,
868 pev
->target
, pev
->uprobes
, dinfo
);
869 if (ret
< 0 || ret
== ntevs
) {
870 pr_debug("Post processing failed or all events are skipped. (%d)\n", ret
);
871 clear_probe_trace_events(*tevs
, ntevs
);
877 debuginfo__delete(dinfo
);
879 if (ntevs
== 0) { /* No error but failed to find probe point. */
880 pr_warning("Probe point '%s' not found.\n",
881 synthesize_perf_probe_point(&pev
->point
));
883 } else if (ntevs
< 0) {
884 /* Error path : ntevs < 0 */
885 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs
);
887 pr_warning("Warning: No dwarf info found in the vmlinux - "
888 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
890 pr_debug("Trying to use symbols.\n");
897 #define LINEBUF_SIZE 256
898 #define NR_ADDITIONAL_LINES 2
900 static int __show_one_line(FILE *fp
, int l
, bool skip
, bool show_num
)
902 char buf
[LINEBUF_SIZE
], sbuf
[STRERR_BUFSIZE
];
903 const char *color
= show_num
? "" : PERF_COLOR_BLUE
;
904 const char *prefix
= NULL
;
907 if (fgets(buf
, LINEBUF_SIZE
, fp
) == NULL
)
912 prefix
= show_num
? "%7d " : " ";
913 color_fprintf(stdout
, color
, prefix
, l
);
915 color_fprintf(stdout
, color
, "%s", buf
);
917 } while (strchr(buf
, '\n') == NULL
);
922 pr_warning("File read error: %s\n",
923 str_error_r(errno
, sbuf
, sizeof(sbuf
)));
929 static int _show_one_line(FILE *fp
, int l
, bool skip
, bool show_num
)
931 int rv
= __show_one_line(fp
, l
, skip
, show_num
);
933 pr_warning("Source file is shorter than expected.\n");
939 #define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
940 #define show_one_line(f,l) _show_one_line(f,l,false,false)
941 #define skip_one_line(f,l) _show_one_line(f,l,true,false)
942 #define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
945 * Show line-range always requires debuginfo to find source file and
948 static int __show_line_range(struct line_range
*lr
, const char *module
,
953 struct debuginfo
*dinfo
;
957 char sbuf
[STRERR_BUFSIZE
];
959 /* Search a line range */
960 dinfo
= open_debuginfo(module
, NULL
, false);
964 ret
= debuginfo__find_line_range(dinfo
, lr
);
965 if (!ret
) { /* Not found, retry with an alternative */
966 ret
= get_alternative_line_range(dinfo
, lr
, module
, user
);
968 ret
= debuginfo__find_line_range(dinfo
, lr
);
970 debuginfo__delete(dinfo
);
971 if (ret
== 0 || ret
== -ENOENT
) {
972 pr_warning("Specified source line is not found.\n");
974 } else if (ret
< 0) {
975 pr_warning("Debuginfo analysis failed.\n");
979 /* Convert source file path */
981 ret
= get_real_path(tmp
, lr
->comp_dir
, &lr
->path
);
983 /* Free old path when new path is assigned */
988 pr_warning("Failed to find source file path.\n");
995 fprintf(stdout
, "<%s@%s:%d>\n", lr
->function
, lr
->path
,
996 lr
->start
- lr
->offset
);
998 fprintf(stdout
, "<%s:%d>\n", lr
->path
, lr
->start
);
1000 fp
= fopen(lr
->path
, "r");
1002 pr_warning("Failed to open %s: %s\n", lr
->path
,
1003 str_error_r(errno
, sbuf
, sizeof(sbuf
)));
1006 /* Skip to starting line number */
1007 while (l
< lr
->start
) {
1008 ret
= skip_one_line(fp
, l
++);
1013 intlist__for_each_entry(ln
, lr
->line_list
) {
1014 for (; ln
->i
> l
; l
++) {
1015 ret
= show_one_line(fp
, l
- lr
->offset
);
1019 ret
= show_one_line_with_num(fp
, l
++ - lr
->offset
);
1024 if (lr
->end
== INT_MAX
)
1025 lr
->end
= l
+ NR_ADDITIONAL_LINES
;
1026 while (l
<= lr
->end
) {
1027 ret
= show_one_line_or_eof(fp
, l
++ - lr
->offset
);
1036 int show_line_range(struct line_range
*lr
, const char *module
,
1037 struct nsinfo
*nsi
, bool user
)
1040 struct nscookie nsc
;
1042 ret
= init_probe_symbol_maps(user
);
1045 nsinfo__mountns_enter(nsi
, &nsc
);
1046 ret
= __show_line_range(lr
, module
, user
);
1047 nsinfo__mountns_exit(&nsc
);
1048 exit_probe_symbol_maps();
1053 static int show_available_vars_at(struct debuginfo
*dinfo
,
1054 struct perf_probe_event
*pev
,
1055 struct strfilter
*_filter
)
1059 struct str_node
*node
;
1060 struct variable_list
*vls
= NULL
, *vl
;
1061 struct perf_probe_point tmp
;
1064 buf
= synthesize_perf_probe_point(&pev
->point
);
1067 pr_debug("Searching variables at %s\n", buf
);
1069 ret
= debuginfo__find_available_vars_at(dinfo
, pev
, &vls
);
1070 if (!ret
) { /* Not found, retry with an alternative */
1071 ret
= get_alternative_probe_event(dinfo
, pev
, &tmp
);
1073 ret
= debuginfo__find_available_vars_at(dinfo
, pev
,
1075 /* Release the old probe_point */
1076 clear_perf_probe_point(&tmp
);
1080 if (ret
== 0 || ret
== -ENOENT
) {
1081 pr_err("Failed to find the address of %s\n", buf
);
1084 pr_warning("Debuginfo analysis failed.\n");
1088 /* Some variables are found */
1089 fprintf(stdout
, "Available variables at %s\n", buf
);
1090 for (i
= 0; i
< ret
; i
++) {
1093 * A probe point might be converted to
1094 * several trace points.
1096 fprintf(stdout
, "\t@<%s+%lu>\n", vl
->point
.symbol
,
1098 zfree(&vl
->point
.symbol
);
1101 strlist__for_each_entry(node
, vl
->vars
) {
1102 var
= strchr(node
->s
, '\t') + 1;
1103 if (strfilter__compare(_filter
, var
)) {
1104 fprintf(stdout
, "\t\t%s\n", node
->s
);
1108 strlist__delete(vl
->vars
);
1111 fprintf(stdout
, "\t\t(No matched variables)\n");
1119 /* Show available variables on given probe point */
1120 int show_available_vars(struct perf_probe_event
*pevs
, int npevs
,
1121 struct strfilter
*_filter
)
1124 struct debuginfo
*dinfo
;
1126 ret
= init_probe_symbol_maps(pevs
->uprobes
);
1130 dinfo
= open_debuginfo(pevs
->target
, pevs
->nsi
, false);
1138 for (i
= 0; i
< npevs
&& ret
>= 0; i
++)
1139 ret
= show_available_vars_at(dinfo
, &pevs
[i
], _filter
);
1141 debuginfo__delete(dinfo
);
1143 exit_probe_symbol_maps();
1147 #else /* !HAVE_DWARF_SUPPORT */
1149 static void debuginfo_cache__exit(void)
1154 find_perf_probe_point_from_dwarf(struct probe_trace_point
*tp __maybe_unused
,
1155 struct perf_probe_point
*pp __maybe_unused
,
1156 bool is_kprobe __maybe_unused
)
1161 static int try_to_find_probe_trace_events(struct perf_probe_event
*pev
,
1162 struct probe_trace_event
**tevs __maybe_unused
)
1164 if (perf_probe_event_need_dwarf(pev
)) {
1165 pr_warning("Debuginfo-analysis is not supported.\n");
1172 int show_line_range(struct line_range
*lr __maybe_unused
,
1173 const char *module __maybe_unused
,
1174 struct nsinfo
*nsi __maybe_unused
,
1175 bool user __maybe_unused
)
1177 pr_warning("Debuginfo-analysis is not supported.\n");
1181 int show_available_vars(struct perf_probe_event
*pevs __maybe_unused
,
1182 int npevs __maybe_unused
,
1183 struct strfilter
*filter __maybe_unused
)
1185 pr_warning("Debuginfo-analysis is not supported.\n");
1190 void line_range__clear(struct line_range
*lr
)
1196 intlist__delete(lr
->line_list
);
1197 memset(lr
, 0, sizeof(*lr
));
1200 int line_range__init(struct line_range
*lr
)
1202 memset(lr
, 0, sizeof(*lr
));
1203 lr
->line_list
= intlist__new(NULL
);
1210 static int parse_line_num(char **ptr
, int *val
, const char *what
)
1212 const char *start
= *ptr
;
1215 *val
= strtol(*ptr
, ptr
, 0);
1216 if (errno
|| *ptr
== start
) {
1217 semantic_error("'%s' is not a valid number.\n", what
);
1223 /* Check the name is good for event, group or function */
1224 static bool is_c_func_name(const char *name
)
1226 if (!isalpha(*name
) && *name
!= '_')
1228 while (*++name
!= '\0') {
1229 if (!isalpha(*name
) && !isdigit(*name
) && *name
!= '_')
1236 * Stuff 'lr' according to the line range described by 'arg'.
1237 * The line range syntax is described by:
1239 * SRC[:SLN[+NUM|-ELN]]
1240 * FNC[@SRC][:SLN[+NUM|-ELN]]
1242 int parse_line_range_desc(const char *arg
, struct line_range
*lr
)
1244 char *range
, *file
, *name
= strdup(arg
);
1253 range
= strchr(name
, ':');
1257 err
= parse_line_num(&range
, &lr
->start
, "start line");
1261 if (*range
== '+' || *range
== '-') {
1262 const char c
= *range
++;
1264 err
= parse_line_num(&range
, &lr
->end
, "end line");
1269 lr
->end
+= lr
->start
;
1271 * Adjust the number of lines here.
1272 * If the number of lines == 1, the
1273 * the end of line should be equal to
1274 * the start of line.
1280 pr_debug("Line range is %d to %d\n", lr
->start
, lr
->end
);
1283 if (lr
->start
> lr
->end
) {
1284 semantic_error("Start line must be smaller"
1285 " than end line.\n");
1288 if (*range
!= '\0') {
1289 semantic_error("Tailing with invalid str '%s'.\n", range
);
1294 file
= strchr(name
, '@');
1297 lr
->file
= strdup(++file
);
1298 if (lr
->file
== NULL
) {
1302 lr
->function
= name
;
1303 } else if (strchr(name
, '/') || strchr(name
, '.'))
1305 else if (is_c_func_name(name
))/* We reuse it for checking funcname */
1306 lr
->function
= name
;
1307 else { /* Invalid name */
1308 semantic_error("'%s' is not a valid function name.\n", name
);
1319 static int parse_perf_probe_event_name(char **arg
, struct perf_probe_event
*pev
)
1323 ptr
= strpbrk_esc(*arg
, ":");
1326 if (!pev
->sdt
&& !is_c_func_name(*arg
))
1328 pev
->group
= strdup_esc(*arg
);
1335 pev
->event
= strdup_esc(*arg
);
1336 if (pev
->event
== NULL
)
1339 if (!pev
->sdt
&& !is_c_func_name(pev
->event
)) {
1343 semantic_error("%s is bad for event name -it must "
1344 "follow C symbol-naming rule.\n", *arg
);
1350 /* Parse probepoint definition. */
1351 static int parse_perf_probe_point(char *arg
, struct perf_probe_event
*pev
)
1353 struct perf_probe_point
*pp
= &pev
->point
;
1356 bool file_spec
= false;
1361 * perf probe [GRP:][EVENT=]SRC[:LN|;PTN]
1362 * perf probe [GRP:][EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
1363 * perf probe %[GRP:]SDT_EVENT
1368 if (is_sdt_event(arg
)) {
1374 ptr
= strpbrk_esc(arg
, ";=@+%");
1378 semantic_error("%s must be an SDT name.\n",
1382 /* This must be a target file name or build id */
1383 tmp
= build_id_cache__complement(ptr
+ 1);
1385 pev
->target
= build_id_cache__origname(tmp
);
1388 pev
->target
= strdup_esc(ptr
+ 1);
1393 ret
= parse_perf_probe_event_name(&arg
, pev
);
1395 if (asprintf(&pev
->point
.function
, "%%%s", pev
->event
) < 0)
1401 if (ptr
&& *ptr
== '=') { /* Event name */
1404 ret
= parse_perf_probe_event_name(&arg
, pev
);
1412 * Check arg is function or file name and copy it.
1414 * We consider arg to be a file spec if and only if it satisfies
1415 * all of the below criteria::
1416 * - it does not include any of "+@%",
1417 * - it includes one of ":;", and
1418 * - it has a period '.' in the name.
1420 * Otherwise, we consider arg to be a function specification.
1422 if (!strpbrk_esc(arg
, "+@%")) {
1423 ptr
= strpbrk_esc(arg
, ";:");
1424 /* This is a file spec if it includes a '.' before ; or : */
1425 if (ptr
&& memchr(arg
, '.', ptr
- arg
))
1429 ptr
= strpbrk_esc(arg
, ";:+@%");
1438 tmp
= strdup_esc(arg
);
1449 * Keep pp->function even if this is absolute address,
1450 * so it can mark whether abs_address is valid.
1451 * Which make 'perf probe lib.bin 0x0' possible.
1453 * Note that checking length of tmp is not needed
1454 * because when we access tmp[1] we know tmp[0] is '0',
1455 * so tmp[1] should always valid (but could be '\0').
1457 if (tmp
&& !strncmp(tmp
, "0x", 2)) {
1458 pp
->abs_address
= strtoul(pp
->function
, &tmp
, 0);
1460 semantic_error("Invalid absolute address.\n");
1466 /* Parse other options */
1470 if (c
== ';') { /* Lazy pattern must be the last part */
1471 pp
->lazy_line
= strdup(arg
); /* let leave escapes */
1472 if (pp
->lazy_line
== NULL
)
1476 ptr
= strpbrk_esc(arg
, ";:+@%");
1482 case ':': /* Line number */
1483 pp
->line
= strtoul(arg
, &tmp
, 0);
1485 semantic_error("There is non-digit char"
1486 " in line number.\n");
1490 case '+': /* Byte offset from a symbol */
1491 pp
->offset
= strtoul(arg
, &tmp
, 0);
1493 semantic_error("There is non-digit character"
1498 case '@': /* File name */
1500 semantic_error("SRC@SRC is not allowed.\n");
1503 pp
->file
= strdup_esc(arg
);
1504 if (pp
->file
== NULL
)
1507 case '%': /* Probe places */
1508 if (strcmp(arg
, "return") == 0) {
1510 } else { /* Others not supported yet */
1511 semantic_error("%%%s is not supported.\n", arg
);
1515 default: /* Buggy case */
1516 pr_err("This program has a bug at %s:%d.\n",
1517 __FILE__
, __LINE__
);
1523 /* Exclusion check */
1524 if (pp
->lazy_line
&& pp
->line
) {
1525 semantic_error("Lazy pattern can't be used with"
1530 if (pp
->lazy_line
&& pp
->offset
) {
1531 semantic_error("Lazy pattern can't be used with offset.\n");
1535 if (pp
->line
&& pp
->offset
) {
1536 semantic_error("Offset can't be used with line number.\n");
1540 if (!pp
->line
&& !pp
->lazy_line
&& pp
->file
&& !pp
->function
) {
1541 semantic_error("File always requires line number or "
1546 if (pp
->offset
&& !pp
->function
) {
1547 semantic_error("Offset requires an entry function.\n");
1551 if ((pp
->offset
|| pp
->line
|| pp
->lazy_line
) && pp
->retprobe
) {
1552 semantic_error("Offset/Line/Lazy pattern can't be used with "
1557 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
1558 pp
->function
, pp
->file
, pp
->line
, pp
->offset
, pp
->retprobe
,
1563 /* Parse perf-probe event argument */
1564 static int parse_perf_probe_arg(char *str
, struct perf_probe_arg
*arg
)
1566 char *tmp
, *goodname
;
1567 struct perf_probe_arg_field
**fieldp
;
1569 pr_debug("parsing arg: %s into ", str
);
1571 tmp
= strchr(str
, '=');
1573 arg
->name
= strndup(str
, tmp
- str
);
1574 if (arg
->name
== NULL
)
1576 pr_debug("name:%s ", arg
->name
);
1580 tmp
= strchr(str
, ':');
1581 if (tmp
) { /* Type setting */
1583 arg
->type
= strdup(tmp
+ 1);
1584 if (arg
->type
== NULL
)
1586 pr_debug("type:%s ", arg
->type
);
1589 tmp
= strpbrk(str
, "-.[");
1590 if (!is_c_varname(str
) || !tmp
) {
1591 /* A variable, register, symbol or special value */
1592 arg
->var
= strdup(str
);
1593 if (arg
->var
== NULL
)
1595 pr_debug("%s\n", arg
->var
);
1599 /* Structure fields or array element */
1600 arg
->var
= strndup(str
, tmp
- str
);
1601 if (arg
->var
== NULL
)
1603 goodname
= arg
->var
;
1604 pr_debug("%s, ", arg
->var
);
1605 fieldp
= &arg
->field
;
1608 *fieldp
= zalloc(sizeof(struct perf_probe_arg_field
));
1609 if (*fieldp
== NULL
)
1611 if (*tmp
== '[') { /* Array */
1613 (*fieldp
)->index
= strtol(str
+ 1, &tmp
, 0);
1614 (*fieldp
)->ref
= true;
1615 if (*tmp
!= ']' || tmp
== str
+ 1) {
1616 semantic_error("Array index must be a"
1623 } else { /* Structure */
1626 (*fieldp
)->ref
= false;
1627 } else if (tmp
[1] == '>') {
1629 (*fieldp
)->ref
= true;
1631 semantic_error("Argument parse error: %s\n",
1635 tmp
= strpbrk(str
, "-.[");
1638 (*fieldp
)->name
= strndup(str
, tmp
- str
);
1639 if ((*fieldp
)->name
== NULL
)
1642 goodname
= (*fieldp
)->name
;
1643 pr_debug("%s(%d), ", (*fieldp
)->name
, (*fieldp
)->ref
);
1644 fieldp
= &(*fieldp
)->next
;
1647 (*fieldp
)->name
= strdup(str
);
1648 if ((*fieldp
)->name
== NULL
)
1651 goodname
= (*fieldp
)->name
;
1652 pr_debug("%s(%d)\n", (*fieldp
)->name
, (*fieldp
)->ref
);
1654 /* If no name is specified, set the last field name (not array index)*/
1656 arg
->name
= strdup(goodname
);
1657 if (arg
->name
== NULL
)
1663 /* Parse perf-probe event command */
1664 int parse_perf_probe_command(const char *cmd
, struct perf_probe_event
*pev
)
1667 int argc
, i
, ret
= 0;
1669 argv
= argv_split(cmd
, &argc
);
1671 pr_debug("Failed to split arguments.\n");
1674 if (argc
- 1 > MAX_PROBE_ARGS
) {
1675 semantic_error("Too many probe arguments (%d).\n", argc
- 1);
1679 /* Parse probe point */
1680 ret
= parse_perf_probe_point(argv
[0], pev
);
1684 /* Copy arguments and ensure return probe has no C argument */
1685 pev
->nargs
= argc
- 1;
1686 pev
->args
= zalloc(sizeof(struct perf_probe_arg
) * pev
->nargs
);
1687 if (pev
->args
== NULL
) {
1691 for (i
= 0; i
< pev
->nargs
&& ret
>= 0; i
++) {
1692 ret
= parse_perf_probe_arg(argv
[i
+ 1], &pev
->args
[i
]);
1694 is_c_varname(pev
->args
[i
].var
) && pev
->point
.retprobe
) {
1695 semantic_error("You can't specify local variable for"
1706 /* Returns true if *any* ARG is either C variable, $params or $vars. */
1707 bool perf_probe_with_var(struct perf_probe_event
*pev
)
1711 for (i
= 0; i
< pev
->nargs
; i
++)
1712 if (is_c_varname(pev
->args
[i
].var
) ||
1713 !strcmp(pev
->args
[i
].var
, PROBE_ARG_PARAMS
) ||
1714 !strcmp(pev
->args
[i
].var
, PROBE_ARG_VARS
))
1719 /* Return true if this perf_probe_event requires debuginfo */
1720 bool perf_probe_event_need_dwarf(struct perf_probe_event
*pev
)
1722 if (pev
->point
.file
|| pev
->point
.line
|| pev
->point
.lazy_line
)
1725 if (perf_probe_with_var(pev
))
1731 /* Parse probe_events event into struct probe_point */
1732 int parse_probe_trace_command(const char *cmd
, struct probe_trace_event
*tev
)
1734 struct probe_trace_point
*tp
= &tev
->point
;
1737 char *argv0_str
= NULL
, *fmt
, *fmt1_str
, *fmt2_str
, *fmt3_str
;
1741 pr_debug("Parsing probe_events: %s\n", cmd
);
1742 argv
= argv_split(cmd
, &argc
);
1744 pr_debug("Failed to split arguments.\n");
1748 semantic_error("Too few probe arguments.\n");
1753 /* Scan event and group name. */
1754 argv0_str
= strdup(argv
[0]);
1755 if (argv0_str
== NULL
) {
1759 fmt1_str
= strtok_r(argv0_str
, ":", &fmt
);
1760 fmt2_str
= strtok_r(NULL
, "/", &fmt
);
1761 fmt3_str
= strtok_r(NULL
, " \t", &fmt
);
1762 if (fmt1_str
== NULL
|| strlen(fmt1_str
) != 1 || fmt2_str
== NULL
1763 || fmt3_str
== NULL
) {
1764 semantic_error("Failed to parse event name: %s\n", argv
[0]);
1769 tev
->group
= strdup(fmt2_str
);
1770 tev
->event
= strdup(fmt3_str
);
1771 if (tev
->group
== NULL
|| tev
->event
== NULL
) {
1775 pr_debug("Group:%s Event:%s probe:%c\n", tev
->group
, tev
->event
, pr
);
1777 tp
->retprobe
= (pr
== 'r');
1779 /* Scan module name(if there), function name and offset */
1780 p
= strchr(argv
[1], ':');
1782 tp
->module
= strndup(argv
[1], p
- argv
[1]);
1787 tev
->uprobes
= (tp
->module
[0] == '/');
1791 fmt1_str
= strtok_r(p
, "+", &fmt
);
1792 /* only the address started with 0x */
1793 if (fmt1_str
[0] == '0') {
1795 * Fix a special case:
1796 * if address == 0, kernel reports something like:
1797 * p:probe_libc/abs_0 /lib/libc-2.18.so:0x (null) arg1=%ax
1798 * Newer kernel may fix that, but we want to
1799 * support old kernel also.
1801 if (strcmp(fmt1_str
, "0x") == 0) {
1802 if (!argv
[2] || strcmp(argv
[2], "(null)")) {
1809 for (i
= 2; argv
[i
+ 1] != NULL
; i
++)
1810 argv
[i
] = argv
[i
+ 1];
1815 tp
->address
= strtoul(fmt1_str
, NULL
, 0);
1817 /* Only the symbol-based probe has offset */
1818 tp
->symbol
= strdup(fmt1_str
);
1819 if (tp
->symbol
== NULL
) {
1823 fmt2_str
= strtok_r(NULL
, "", &fmt
);
1824 if (fmt2_str
== NULL
)
1827 tp
->offset
= strtoul(fmt2_str
, NULL
, 10);
1831 fmt2_str
= strchr(p
, '(');
1833 tp
->ref_ctr_offset
= strtoul(fmt2_str
+ 1, NULL
, 0);
1836 tev
->nargs
= argc
- 2;
1837 tev
->args
= zalloc(sizeof(struct probe_trace_arg
) * tev
->nargs
);
1838 if (tev
->args
== NULL
) {
1842 for (i
= 0; i
< tev
->nargs
; i
++) {
1843 p
= strchr(argv
[i
+ 2], '=');
1844 if (p
) /* We don't need which register is assigned. */
1848 tev
->args
[i
].name
= strdup(argv
[i
+ 2]);
1849 /* TODO: parse regs and offset */
1850 tev
->args
[i
].value
= strdup(p
);
1851 if (tev
->args
[i
].name
== NULL
|| tev
->args
[i
].value
== NULL
) {
1863 /* Compose only probe arg */
1864 char *synthesize_perf_probe_arg(struct perf_probe_arg
*pa
)
1866 struct perf_probe_arg_field
*field
= pa
->field
;
1871 if (strbuf_init(&buf
, 64) < 0)
1874 if (pa
->name
&& pa
->var
)
1875 err
= strbuf_addf(&buf
, "%s=%s", pa
->name
, pa
->var
);
1877 err
= strbuf_addstr(&buf
, pa
->name
?: pa
->var
);
1882 if (field
->name
[0] == '[')
1883 err
= strbuf_addstr(&buf
, field
->name
);
1885 err
= strbuf_addf(&buf
, "%s%s", field
->ref
? "->" : ".",
1887 field
= field
->next
;
1893 if (strbuf_addf(&buf
, ":%s", pa
->type
) < 0)
1896 ret
= strbuf_detach(&buf
, NULL
);
1898 strbuf_release(&buf
);
1902 /* Compose only probe point (not argument) */
1903 char *synthesize_perf_probe_point(struct perf_probe_point
*pp
)
1906 char *tmp
, *ret
= NULL
;
1909 if (strbuf_init(&buf
, 64) < 0)
1913 if (strbuf_addstr(&buf
, pp
->function
) < 0)
1916 err
= strbuf_addf(&buf
, "+%lu", pp
->offset
);
1918 err
= strbuf_addf(&buf
, ":%d", pp
->line
);
1919 else if (pp
->retprobe
)
1920 err
= strbuf_addstr(&buf
, "%return");
1928 tmp
= strchr(pp
->file
+ len
- 30, '/');
1929 tmp
= tmp
? tmp
+ 1 : pp
->file
+ len
- 30;
1931 err
= strbuf_addf(&buf
, "@%s", tmp
);
1932 if (!err
&& !pp
->function
&& pp
->line
)
1933 err
= strbuf_addf(&buf
, ":%d", pp
->line
);
1936 ret
= strbuf_detach(&buf
, NULL
);
1938 strbuf_release(&buf
);
1942 char *synthesize_perf_probe_command(struct perf_probe_event
*pev
)
1945 char *tmp
, *ret
= NULL
;
1948 if (strbuf_init(&buf
, 64))
1951 if (strbuf_addf(&buf
, "%s:%s=", pev
->group
?: PERFPROBE_GROUP
,
1955 tmp
= synthesize_perf_probe_point(&pev
->point
);
1956 if (!tmp
|| strbuf_addstr(&buf
, tmp
) < 0)
1960 for (i
= 0; i
< pev
->nargs
; i
++) {
1961 tmp
= synthesize_perf_probe_arg(pev
->args
+ i
);
1962 if (!tmp
|| strbuf_addf(&buf
, " %s", tmp
) < 0)
1967 ret
= strbuf_detach(&buf
, NULL
);
1969 strbuf_release(&buf
);
1973 static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref
*ref
,
1974 struct strbuf
*buf
, int depth
)
1978 depth
= __synthesize_probe_trace_arg_ref(ref
->next
, buf
,
1983 err
= strbuf_addf(buf
, "%+ld(", ref
->offset
);
1984 return (err
< 0) ? err
: depth
;
1987 static int synthesize_probe_trace_arg(struct probe_trace_arg
*arg
,
1990 struct probe_trace_arg_ref
*ref
= arg
->ref
;
1993 /* Argument name or separator */
1995 err
= strbuf_addf(buf
, " %s=", arg
->name
);
1997 err
= strbuf_addch(buf
, ' ');
2001 /* Special case: @XXX */
2002 if (arg
->value
[0] == '@' && arg
->ref
)
2005 /* Dereferencing arguments */
2007 depth
= __synthesize_probe_trace_arg_ref(ref
, buf
, 1);
2012 /* Print argument value */
2013 if (arg
->value
[0] == '@' && arg
->ref
)
2014 err
= strbuf_addf(buf
, "%s%+ld", arg
->value
, arg
->ref
->offset
);
2016 err
= strbuf_addstr(buf
, arg
->value
);
2019 while (!err
&& depth
--)
2020 err
= strbuf_addch(buf
, ')');
2022 /* Print argument type */
2023 if (!err
&& arg
->type
)
2024 err
= strbuf_addf(buf
, ":%s", arg
->type
);
2030 synthesize_uprobe_trace_def(struct probe_trace_event
*tev
, struct strbuf
*buf
)
2032 struct probe_trace_point
*tp
= &tev
->point
;
2035 err
= strbuf_addf(buf
, "%s:0x%lx", tp
->module
, tp
->address
);
2037 if (err
>= 0 && tp
->ref_ctr_offset
) {
2038 if (!uprobe_ref_ctr_is_supported())
2040 err
= strbuf_addf(buf
, "(0x%lx)", tp
->ref_ctr_offset
);
2042 return err
>= 0 ? 0 : -1;
2045 char *synthesize_probe_trace_command(struct probe_trace_event
*tev
)
2047 struct probe_trace_point
*tp
= &tev
->point
;
2052 /* Uprobes must have tp->module */
2053 if (tev
->uprobes
&& !tp
->module
)
2056 if (strbuf_init(&buf
, 32) < 0)
2059 if (strbuf_addf(&buf
, "%c:%s/%s ", tp
->retprobe
? 'r' : 'p',
2060 tev
->group
, tev
->event
) < 0)
2063 * If tp->address == 0, then this point must be a
2064 * absolute address uprobe.
2065 * try_to_find_absolute_address() should have made
2066 * tp->symbol to "0x0".
2068 if (tev
->uprobes
&& !tp
->address
) {
2069 if (!tp
->symbol
|| strcmp(tp
->symbol
, "0x0"))
2073 /* Use the tp->address for uprobes */
2075 err
= synthesize_uprobe_trace_def(tev
, &buf
);
2076 } else if (!strncmp(tp
->symbol
, "0x", 2)) {
2077 /* Absolute address. See try_to_find_absolute_address() */
2078 err
= strbuf_addf(&buf
, "%s%s0x%lx", tp
->module
?: "",
2079 tp
->module
? ":" : "", tp
->address
);
2081 err
= strbuf_addf(&buf
, "%s%s%s+%lu", tp
->module
?: "",
2082 tp
->module
? ":" : "", tp
->symbol
, tp
->offset
);
2088 for (i
= 0; i
< tev
->nargs
; i
++)
2089 if (synthesize_probe_trace_arg(&tev
->args
[i
], &buf
) < 0)
2092 ret
= strbuf_detach(&buf
, NULL
);
2094 strbuf_release(&buf
);
2098 static int find_perf_probe_point_from_map(struct probe_trace_point
*tp
,
2099 struct perf_probe_point
*pp
,
2102 struct symbol
*sym
= NULL
;
2103 struct map
*map
= NULL
;
2104 u64 addr
= tp
->address
;
2108 map
= dso__new_map(tp
->module
);
2111 sym
= map__find_symbol(map
, addr
);
2113 if (tp
->symbol
&& !addr
) {
2114 if (kernel_get_symbol_address_by_name(tp
->symbol
,
2115 &addr
, true, false) < 0)
2120 sym
= machine__find_kernel_symbol(host_machine
, addr
, &map
);
2127 pp
->retprobe
= tp
->retprobe
;
2128 pp
->offset
= addr
- map
->unmap_ip(map
, sym
->start
);
2129 pp
->function
= strdup(sym
->name
);
2130 ret
= pp
->function
? 0 : -ENOMEM
;
2133 if (map
&& !is_kprobe
) {
2140 static int convert_to_perf_probe_point(struct probe_trace_point
*tp
,
2141 struct perf_probe_point
*pp
,
2147 ret
= find_perf_probe_point_from_dwarf(tp
, pp
, is_kprobe
);
2150 ret
= find_perf_probe_point_from_map(tp
, pp
, is_kprobe
);
2154 pr_debug("Failed to find probe point from both of dwarf and map.\n");
2157 pp
->function
= strdup(tp
->symbol
);
2158 pp
->offset
= tp
->offset
;
2160 ret
= e_snprintf(buf
, 128, "0x%" PRIx64
, (u64
)tp
->address
);
2163 pp
->function
= strdup(buf
);
2166 if (pp
->function
== NULL
)
2169 pp
->retprobe
= tp
->retprobe
;
2174 static int convert_to_perf_probe_event(struct probe_trace_event
*tev
,
2175 struct perf_probe_event
*pev
, bool is_kprobe
)
2177 struct strbuf buf
= STRBUF_INIT
;
2180 /* Convert event/group name */
2181 pev
->event
= strdup(tev
->event
);
2182 pev
->group
= strdup(tev
->group
);
2183 if (pev
->event
== NULL
|| pev
->group
== NULL
)
2186 /* Convert trace_point to probe_point */
2187 ret
= convert_to_perf_probe_point(&tev
->point
, &pev
->point
, is_kprobe
);
2191 /* Convert trace_arg to probe_arg */
2192 pev
->nargs
= tev
->nargs
;
2193 pev
->args
= zalloc(sizeof(struct perf_probe_arg
) * pev
->nargs
);
2194 if (pev
->args
== NULL
)
2196 for (i
= 0; i
< tev
->nargs
&& ret
>= 0; i
++) {
2197 if (tev
->args
[i
].name
)
2198 pev
->args
[i
].name
= strdup(tev
->args
[i
].name
);
2200 if ((ret
= strbuf_init(&buf
, 32)) < 0)
2202 ret
= synthesize_probe_trace_arg(&tev
->args
[i
], &buf
);
2203 pev
->args
[i
].name
= strbuf_detach(&buf
, NULL
);
2205 if (pev
->args
[i
].name
== NULL
&& ret
>= 0)
2210 clear_perf_probe_event(pev
);
2215 void clear_perf_probe_event(struct perf_probe_event
*pev
)
2217 struct perf_probe_arg_field
*field
, *next
;
2223 clear_perf_probe_point(&pev
->point
);
2225 for (i
= 0; i
< pev
->nargs
; i
++) {
2226 free(pev
->args
[i
].name
);
2227 free(pev
->args
[i
].var
);
2228 free(pev
->args
[i
].type
);
2229 field
= pev
->args
[i
].field
;
2232 zfree(&field
->name
);
2238 memset(pev
, 0, sizeof(*pev
));
2241 #define strdup_or_goto(str, label) \
2242 ({ char *__p = NULL; if (str && !(__p = strdup(str))) goto label; __p; })
2244 static int perf_probe_point__copy(struct perf_probe_point
*dst
,
2245 struct perf_probe_point
*src
)
2247 dst
->file
= strdup_or_goto(src
->file
, out_err
);
2248 dst
->function
= strdup_or_goto(src
->function
, out_err
);
2249 dst
->lazy_line
= strdup_or_goto(src
->lazy_line
, out_err
);
2250 dst
->line
= src
->line
;
2251 dst
->retprobe
= src
->retprobe
;
2252 dst
->offset
= src
->offset
;
2256 clear_perf_probe_point(dst
);
2260 static int perf_probe_arg__copy(struct perf_probe_arg
*dst
,
2261 struct perf_probe_arg
*src
)
2263 struct perf_probe_arg_field
*field
, **ppfield
;
2265 dst
->name
= strdup_or_goto(src
->name
, out_err
);
2266 dst
->var
= strdup_or_goto(src
->var
, out_err
);
2267 dst
->type
= strdup_or_goto(src
->type
, out_err
);
2270 ppfield
= &(dst
->field
);
2272 *ppfield
= zalloc(sizeof(*field
));
2275 (*ppfield
)->name
= strdup_or_goto(field
->name
, out_err
);
2276 (*ppfield
)->index
= field
->index
;
2277 (*ppfield
)->ref
= field
->ref
;
2278 field
= field
->next
;
2279 ppfield
= &((*ppfield
)->next
);
2286 int perf_probe_event__copy(struct perf_probe_event
*dst
,
2287 struct perf_probe_event
*src
)
2291 dst
->event
= strdup_or_goto(src
->event
, out_err
);
2292 dst
->group
= strdup_or_goto(src
->group
, out_err
);
2293 dst
->target
= strdup_or_goto(src
->target
, out_err
);
2294 dst
->uprobes
= src
->uprobes
;
2296 if (perf_probe_point__copy(&dst
->point
, &src
->point
) < 0)
2299 dst
->args
= zalloc(sizeof(struct perf_probe_arg
) * src
->nargs
);
2302 dst
->nargs
= src
->nargs
;
2304 for (i
= 0; i
< src
->nargs
; i
++)
2305 if (perf_probe_arg__copy(&dst
->args
[i
], &src
->args
[i
]) < 0)
2310 clear_perf_probe_event(dst
);
2314 void clear_probe_trace_event(struct probe_trace_event
*tev
)
2316 struct probe_trace_arg_ref
*ref
, *next
;
2321 free(tev
->point
.symbol
);
2322 free(tev
->point
.realname
);
2323 free(tev
->point
.module
);
2324 for (i
= 0; i
< tev
->nargs
; i
++) {
2325 free(tev
->args
[i
].name
);
2326 free(tev
->args
[i
].value
);
2327 free(tev
->args
[i
].type
);
2328 ref
= tev
->args
[i
].ref
;
2336 memset(tev
, 0, sizeof(*tev
));
2339 struct kprobe_blacklist_node
{
2340 struct list_head list
;
2341 unsigned long start
;
2346 static void kprobe_blacklist__delete(struct list_head
*blacklist
)
2348 struct kprobe_blacklist_node
*node
;
2350 while (!list_empty(blacklist
)) {
2351 node
= list_first_entry(blacklist
,
2352 struct kprobe_blacklist_node
, list
);
2353 list_del(&node
->list
);
2359 static int kprobe_blacklist__load(struct list_head
*blacklist
)
2361 struct kprobe_blacklist_node
*node
;
2362 const char *__debugfs
= debugfs__mountpoint();
2363 char buf
[PATH_MAX
], *p
;
2367 if (__debugfs
== NULL
)
2370 ret
= e_snprintf(buf
, PATH_MAX
, "%s/kprobes/blacklist", __debugfs
);
2374 fp
= fopen(buf
, "r");
2379 while (fgets(buf
, PATH_MAX
, fp
)) {
2380 node
= zalloc(sizeof(*node
));
2385 INIT_LIST_HEAD(&node
->list
);
2386 list_add_tail(&node
->list
, blacklist
);
2387 if (sscanf(buf
, "0x%lx-0x%lx", &node
->start
, &node
->end
) != 2) {
2391 p
= strchr(buf
, '\t');
2394 if (p
[strlen(p
) - 1] == '\n')
2395 p
[strlen(p
) - 1] = '\0';
2397 p
= (char *)"unknown";
2398 node
->symbol
= strdup(p
);
2399 if (!node
->symbol
) {
2403 pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
2404 node
->start
, node
->end
, node
->symbol
);
2408 kprobe_blacklist__delete(blacklist
);
2414 static struct kprobe_blacklist_node
*
2415 kprobe_blacklist__find_by_address(struct list_head
*blacklist
,
2416 unsigned long address
)
2418 struct kprobe_blacklist_node
*node
;
2420 list_for_each_entry(node
, blacklist
, list
) {
2421 if (node
->start
<= address
&& address
< node
->end
)
2428 static LIST_HEAD(kprobe_blacklist
);
2430 static void kprobe_blacklist__init(void)
2432 if (!list_empty(&kprobe_blacklist
))
2435 if (kprobe_blacklist__load(&kprobe_blacklist
) < 0)
2436 pr_debug("No kprobe blacklist support, ignored\n");
2439 static void kprobe_blacklist__release(void)
2441 kprobe_blacklist__delete(&kprobe_blacklist
);
2444 static bool kprobe_blacklist__listed(unsigned long address
)
2446 return !!kprobe_blacklist__find_by_address(&kprobe_blacklist
, address
);
2449 static int perf_probe_event__sprintf(const char *group
, const char *event
,
2450 struct perf_probe_event
*pev
,
2452 struct strbuf
*result
)
2457 if (asprintf(&buf
, "%s:%s", group
, event
) < 0)
2459 ret
= strbuf_addf(result
, " %-20s (on ", buf
);
2464 /* Synthesize only event probe point */
2465 buf
= synthesize_perf_probe_point(&pev
->point
);
2468 ret
= strbuf_addstr(result
, buf
);
2472 ret
= strbuf_addf(result
, " in %s", module
);
2474 if (!ret
&& pev
->nargs
> 0) {
2475 ret
= strbuf_add(result
, " with", 5);
2476 for (i
= 0; !ret
&& i
< pev
->nargs
; i
++) {
2477 buf
= synthesize_perf_probe_arg(&pev
->args
[i
]);
2480 ret
= strbuf_addf(result
, " %s", buf
);
2485 ret
= strbuf_addch(result
, ')');
2491 int show_perf_probe_event(const char *group
, const char *event
,
2492 struct perf_probe_event
*pev
,
2493 const char *module
, bool use_stdout
)
2495 struct strbuf buf
= STRBUF_INIT
;
2498 ret
= perf_probe_event__sprintf(group
, event
, pev
, module
, &buf
);
2501 printf("%s\n", buf
.buf
);
2503 pr_info("%s\n", buf
.buf
);
2505 strbuf_release(&buf
);
2510 static bool filter_probe_trace_event(struct probe_trace_event
*tev
,
2511 struct strfilter
*filter
)
2515 /* At first, check the event name itself */
2516 if (strfilter__compare(filter
, tev
->event
))
2519 /* Next, check the combination of name and group */
2520 if (e_snprintf(tmp
, 128, "%s:%s", tev
->group
, tev
->event
) < 0)
2522 return strfilter__compare(filter
, tmp
);
2525 static int __show_perf_probe_events(int fd
, bool is_kprobe
,
2526 struct strfilter
*filter
)
2529 struct probe_trace_event tev
;
2530 struct perf_probe_event pev
;
2531 struct strlist
*rawlist
;
2532 struct str_node
*ent
;
2534 memset(&tev
, 0, sizeof(tev
));
2535 memset(&pev
, 0, sizeof(pev
));
2537 rawlist
= probe_file__get_rawlist(fd
);
2541 strlist__for_each_entry(ent
, rawlist
) {
2542 ret
= parse_probe_trace_command(ent
->s
, &tev
);
2544 if (!filter_probe_trace_event(&tev
, filter
))
2546 ret
= convert_to_perf_probe_event(&tev
, &pev
,
2550 ret
= show_perf_probe_event(pev
.group
, pev
.event
,
2551 &pev
, tev
.point
.module
,
2555 clear_perf_probe_event(&pev
);
2556 clear_probe_trace_event(&tev
);
2560 strlist__delete(rawlist
);
2561 /* Cleanup cached debuginfo if needed */
2562 debuginfo_cache__exit();
2567 /* List up current perf-probe events */
2568 int show_perf_probe_events(struct strfilter
*filter
)
2570 int kp_fd
, up_fd
, ret
;
2574 if (probe_conf
.cache
)
2575 return probe_cache__show_all_caches(filter
);
2577 ret
= init_probe_symbol_maps(false);
2581 ret
= probe_file__open_both(&kp_fd
, &up_fd
, 0);
2586 ret
= __show_perf_probe_events(kp_fd
, true, filter
);
2587 if (up_fd
>= 0 && ret
>= 0)
2588 ret
= __show_perf_probe_events(up_fd
, false, filter
);
2593 exit_probe_symbol_maps();
2598 static int get_new_event_name(char *buf
, size_t len
, const char *base
,
2599 struct strlist
*namelist
, bool ret_event
,
2607 nbase
= strdup(base
);
2611 /* Cut off the dot suffixes (e.g. .const, .isra) and version suffixes */
2612 p
= strpbrk(nbase
, ".@");
2613 if (p
&& p
!= nbase
)
2616 /* Try no suffix number */
2617 ret
= e_snprintf(buf
, len
, "%s%s", nbase
, ret_event
? "__return" : "");
2619 pr_debug("snprintf() failed: %d\n", ret
);
2622 if (!strlist__has_entry(namelist
, buf
))
2625 if (!allow_suffix
) {
2626 pr_warning("Error: event \"%s\" already exists.\n"
2627 " Hint: Remove existing event by 'perf probe -d'\n"
2628 " or force duplicates by 'perf probe -f'\n"
2629 " or set 'force=yes' in BPF source.\n",
2635 /* Try to add suffix */
2636 for (i
= 1; i
< MAX_EVENT_INDEX
; i
++) {
2637 ret
= e_snprintf(buf
, len
, "%s_%d", nbase
, i
);
2639 pr_debug("snprintf() failed: %d\n", ret
);
2642 if (!strlist__has_entry(namelist
, buf
))
2645 if (i
== MAX_EVENT_INDEX
) {
2646 pr_warning("Too many events are on the same function.\n");
2653 /* Final validation */
2654 if (ret
>= 0 && !is_c_func_name(buf
)) {
2655 pr_warning("Internal error: \"%s\" is an invalid event name.\n",
2663 /* Warn if the current kernel's uprobe implementation is old */
2664 static void warn_uprobe_event_compat(struct probe_trace_event
*tev
)
2667 char *buf
= synthesize_probe_trace_command(tev
);
2668 struct probe_trace_point
*tp
= &tev
->point
;
2670 if (tp
->ref_ctr_offset
&& !uprobe_ref_ctr_is_supported()) {
2671 pr_warning("A semaphore is associated with %s:%s and "
2672 "seems your kernel doesn't support it.\n",
2673 tev
->group
, tev
->event
);
2676 /* Old uprobe event doesn't support memory dereference */
2677 if (!tev
->uprobes
|| tev
->nargs
== 0 || !buf
)
2680 for (i
= 0; i
< tev
->nargs
; i
++)
2681 if (strglobmatch(tev
->args
[i
].value
, "[$@+-]*")) {
2682 pr_warning("Please upgrade your kernel to at least "
2683 "3.14 to have access to feature %s\n",
2684 tev
->args
[i
].value
);
2691 /* Set new name from original perf_probe_event and namelist */
2692 static int probe_trace_event__set_name(struct probe_trace_event
*tev
,
2693 struct perf_probe_event
*pev
,
2694 struct strlist
*namelist
,
2697 const char *event
, *group
;
2701 /* If probe_event or trace_event already have the name, reuse it */
2702 if (pev
->event
&& !pev
->sdt
)
2704 else if (tev
->event
)
2707 /* Or generate new one from probe point */
2708 if (pev
->point
.function
&&
2709 (strncmp(pev
->point
.function
, "0x", 2) != 0) &&
2710 !strisglob(pev
->point
.function
))
2711 event
= pev
->point
.function
;
2713 event
= tev
->point
.realname
;
2715 if (pev
->group
&& !pev
->sdt
)
2717 else if (tev
->group
)
2720 group
= PERFPROBE_GROUP
;
2722 /* Get an unused new event name */
2723 ret
= get_new_event_name(buf
, 64, event
, namelist
,
2724 tev
->point
.retprobe
, allow_suffix
);
2730 tev
->event
= strdup(event
);
2731 tev
->group
= strdup(group
);
2732 if (tev
->event
== NULL
|| tev
->group
== NULL
)
2735 /* Add added event name to namelist */
2736 strlist__add(namelist
, event
);
2740 static int __open_probe_file_and_namelist(bool uprobe
,
2741 struct strlist
**namelist
)
2745 fd
= probe_file__open(PF_FL_RW
| (uprobe
? PF_FL_UPROBE
: 0));
2749 /* Get current event names */
2750 *namelist
= probe_file__get_namelist(fd
);
2752 pr_debug("Failed to get current event list.\n");
2759 static int __add_probe_trace_events(struct perf_probe_event
*pev
,
2760 struct probe_trace_event
*tevs
,
2761 int ntevs
, bool allow_suffix
)
2763 int i
, fd
[2] = {-1, -1}, up
, ret
;
2764 struct probe_trace_event
*tev
= NULL
;
2765 struct probe_cache
*cache
= NULL
;
2766 struct strlist
*namelist
[2] = {NULL
, NULL
};
2767 struct nscookie nsc
;
2769 up
= pev
->uprobes
? 1 : 0;
2770 fd
[up
] = __open_probe_file_and_namelist(up
, &namelist
[up
]);
2775 for (i
= 0; i
< ntevs
; i
++) {
2777 up
= tev
->uprobes
? 1 : 0;
2778 if (fd
[up
] == -1) { /* Open the kprobe/uprobe_events */
2779 fd
[up
] = __open_probe_file_and_namelist(up
,
2784 /* Skip if the symbol is out of .text or blacklisted */
2785 if (!tev
->point
.symbol
&& !pev
->uprobes
)
2788 /* Set new name for tev (and update namelist) */
2789 ret
= probe_trace_event__set_name(tev
, pev
, namelist
[up
],
2794 nsinfo__mountns_enter(pev
->nsi
, &nsc
);
2795 ret
= probe_file__add_event(fd
[up
], tev
);
2796 nsinfo__mountns_exit(&nsc
);
2801 * Probes after the first probe which comes from same
2802 * user input are always allowed to add suffix, because
2803 * there might be several addresses corresponding to
2806 allow_suffix
= true;
2808 if (ret
== -EINVAL
&& pev
->uprobes
)
2809 warn_uprobe_event_compat(tev
);
2810 if (ret
== 0 && probe_conf
.cache
) {
2811 cache
= probe_cache__new(pev
->target
, pev
->nsi
);
2813 probe_cache__add_entry(cache
, pev
, tevs
, ntevs
) < 0 ||
2814 probe_cache__commit(cache
) < 0)
2815 pr_warning("Failed to add event to probe cache\n");
2816 probe_cache__delete(cache
);
2820 for (up
= 0; up
< 2; up
++) {
2821 strlist__delete(namelist
[up
]);
2828 static int find_probe_functions(struct map
*map
, char *name
,
2829 struct symbol
**syms
)
2833 struct rb_node
*tmp
;
2834 const char *norm
, *ver
;
2836 bool cut_version
= true;
2838 if (map__load(map
) < 0)
2841 /* If user gives a version, don't cut off the version from symbols */
2842 if (strchr(name
, '@'))
2843 cut_version
= false;
2845 map__for_each_symbol(map
, sym
, tmp
) {
2846 norm
= arch__normalize_symbol_name(sym
->name
);
2851 /* We don't care about default symbol or not */
2852 ver
= strchr(norm
, '@');
2854 buf
= strndup(norm
, ver
- norm
);
2861 if (strglobmatch(norm
, name
)) {
2863 if (syms
&& found
< probe_conf
.max_probes
)
2864 syms
[found
- 1] = sym
;
2873 void __weak
arch__fix_tev_from_maps(struct perf_probe_event
*pev __maybe_unused
,
2874 struct probe_trace_event
*tev __maybe_unused
,
2875 struct map
*map __maybe_unused
,
2876 struct symbol
*sym __maybe_unused
) { }
2879 * Find probe function addresses from map.
2880 * Return an error or the number of found probe_trace_event
2882 static int find_probe_trace_events_from_map(struct perf_probe_event
*pev
,
2883 struct probe_trace_event
**tevs
)
2885 struct map
*map
= NULL
;
2886 struct ref_reloc_sym
*reloc_sym
= NULL
;
2888 struct symbol
**syms
= NULL
;
2889 struct probe_trace_event
*tev
;
2890 struct perf_probe_point
*pp
= &pev
->point
;
2891 struct probe_trace_point
*tp
;
2892 int num_matched_functions
;
2893 int ret
, i
, j
, skipped
= 0;
2896 map
= get_target_map(pev
->target
, pev
->nsi
, pev
->uprobes
);
2902 syms
= malloc(sizeof(struct symbol
*) * probe_conf
.max_probes
);
2909 * Load matched symbols: Since the different local symbols may have
2910 * same name but different addresses, this lists all the symbols.
2912 num_matched_functions
= find_probe_functions(map
, pp
->function
, syms
);
2913 if (num_matched_functions
<= 0) {
2914 pr_err("Failed to find symbol %s in %s\n", pp
->function
,
2915 pev
->target
? : "kernel");
2918 } else if (num_matched_functions
> probe_conf
.max_probes
) {
2919 pr_err("Too many functions matched in %s\n",
2920 pev
->target
? : "kernel");
2925 /* Note that the symbols in the kmodule are not relocated */
2926 if (!pev
->uprobes
&& !pev
->target
&&
2927 (!pp
->retprobe
|| kretprobe_offset_is_supported())) {
2928 reloc_sym
= kernel_get_ref_reloc_sym();
2930 pr_warning("Relocated base symbol is not found!\n");
2936 /* Setup result trace-probe-events */
2937 *tevs
= zalloc(sizeof(*tev
) * num_matched_functions
);
2945 for (j
= 0; j
< num_matched_functions
; j
++) {
2948 tev
= (*tevs
) + ret
;
2950 if (ret
== num_matched_functions
) {
2951 pr_warning("Too many symbols are listed. Skip it.\n");
2956 if (pp
->offset
> sym
->end
- sym
->start
) {
2957 pr_warning("Offset %ld is bigger than the size of %s\n",
2958 pp
->offset
, sym
->name
);
2962 /* Add one probe point */
2963 tp
->address
= map
->unmap_ip(map
, sym
->start
) + pp
->offset
;
2965 /* Check the kprobe (not in module) is within .text */
2966 if (!pev
->uprobes
&& !pev
->target
&&
2967 kprobe_warn_out_range(sym
->name
, tp
->address
)) {
2968 tp
->symbol
= NULL
; /* Skip it */
2970 } else if (reloc_sym
) {
2971 tp
->symbol
= strdup_or_goto(reloc_sym
->name
, nomem_out
);
2972 tp
->offset
= tp
->address
- reloc_sym
->addr
;
2974 tp
->symbol
= strdup_or_goto(sym
->name
, nomem_out
);
2975 tp
->offset
= pp
->offset
;
2977 tp
->realname
= strdup_or_goto(sym
->name
, nomem_out
);
2979 tp
->retprobe
= pp
->retprobe
;
2982 tev
->point
.module
= strdup_or_goto(pev
->target
,
2985 mod_name
= find_module_name(pev
->target
);
2987 strdup(mod_name
? mod_name
: pev
->target
);
2989 if (!tev
->point
.module
)
2993 tev
->uprobes
= pev
->uprobes
;
2994 tev
->nargs
= pev
->nargs
;
2996 tev
->args
= zalloc(sizeof(struct probe_trace_arg
) *
2998 if (tev
->args
== NULL
)
3001 for (i
= 0; i
< tev
->nargs
; i
++) {
3002 if (pev
->args
[i
].name
)
3004 strdup_or_goto(pev
->args
[i
].name
,
3007 tev
->args
[i
].value
= strdup_or_goto(pev
->args
[i
].var
,
3009 if (pev
->args
[i
].type
)
3011 strdup_or_goto(pev
->args
[i
].type
,
3014 arch__fix_tev_from_maps(pev
, tev
, map
, sym
);
3016 if (ret
== skipped
) {
3029 clear_probe_trace_events(*tevs
, num_matched_functions
);
3034 static int try_to_find_absolute_address(struct perf_probe_event
*pev
,
3035 struct probe_trace_event
**tevs
)
3037 struct perf_probe_point
*pp
= &pev
->point
;
3038 struct probe_trace_event
*tev
;
3039 struct probe_trace_point
*tp
;
3042 if (!(pev
->point
.function
&& !strncmp(pev
->point
.function
, "0x", 2)))
3044 if (perf_probe_event_need_dwarf(pev
))
3048 * This is 'perf probe /lib/libc.so 0xabcd'. Try to probe at
3051 * Only one tev can be generated by this.
3053 *tevs
= zalloc(sizeof(*tev
));
3061 * Don't use tp->offset, use address directly, because
3062 * in synthesize_probe_trace_command() address cannot be
3065 tp
->address
= pev
->point
.abs_address
;
3066 tp
->retprobe
= pp
->retprobe
;
3067 tev
->uprobes
= pev
->uprobes
;
3071 * Give it a '0x' leading symbol name.
3072 * In __add_probe_trace_events, a NULL symbol is interpreted as
3075 if (asprintf(&tp
->symbol
, "0x%lx", tp
->address
) < 0)
3078 /* For kprobe, check range */
3079 if ((!tev
->uprobes
) &&
3080 (kprobe_warn_out_range(tev
->point
.symbol
,
3081 tev
->point
.address
))) {
3086 if (asprintf(&tp
->realname
, "abs_%lx", tp
->address
) < 0)
3090 tp
->module
= strdup(pev
->target
);
3096 tev
->group
= strdup(pev
->group
);
3102 tev
->event
= strdup(pev
->event
);
3107 tev
->nargs
= pev
->nargs
;
3108 tev
->args
= zalloc(sizeof(struct probe_trace_arg
) * tev
->nargs
);
3112 for (i
= 0; i
< tev
->nargs
; i
++)
3113 copy_to_probe_trace_arg(&tev
->args
[i
], &pev
->args
[i
]);
3118 clear_probe_trace_events(*tevs
, 1);
3123 /* Concatinate two arrays */
3124 static void *memcat(void *a
, size_t sz_a
, void *b
, size_t sz_b
)
3128 ret
= malloc(sz_a
+ sz_b
);
3130 memcpy(ret
, a
, sz_a
);
3131 memcpy(ret
+ sz_a
, b
, sz_b
);
3137 concat_probe_trace_events(struct probe_trace_event
**tevs
, int *ntevs
,
3138 struct probe_trace_event
**tevs2
, int ntevs2
)
3140 struct probe_trace_event
*new_tevs
;
3150 if (*ntevs
+ ntevs2
> probe_conf
.max_probes
)
3153 /* Concatinate the array of probe_trace_event */
3154 new_tevs
= memcat(*tevs
, (*ntevs
) * sizeof(**tevs
),
3155 *tevs2
, ntevs2
* sizeof(**tevs2
));
3165 clear_probe_trace_events(*tevs2
, ntevs2
);
3172 * Try to find probe_trace_event from given probe caches. Return the number
3173 * of cached events found, if an error occurs return the error.
3175 static int find_cached_events(struct perf_probe_event
*pev
,
3176 struct probe_trace_event
**tevs
,
3179 struct probe_cache
*cache
;
3180 struct probe_cache_entry
*entry
;
3181 struct probe_trace_event
*tmp_tevs
= NULL
;
3185 cache
= probe_cache__new(target
, pev
->nsi
);
3186 /* Return 0 ("not found") if the target has no probe cache. */
3190 for_each_probe_cache_entry(entry
, cache
) {
3191 /* Skip the cache entry which has no name */
3192 if (!entry
->pev
.event
|| !entry
->pev
.group
)
3194 if ((!pev
->group
|| strglobmatch(entry
->pev
.group
, pev
->group
)) &&
3195 strglobmatch(entry
->pev
.event
, pev
->event
)) {
3196 ret
= probe_cache_entry__get_event(entry
, &tmp_tevs
);
3198 ret
= concat_probe_trace_events(tevs
, &ntevs
,
3204 probe_cache__delete(cache
);
3206 clear_probe_trace_events(*tevs
, ntevs
);
3210 if (ntevs
> 0 && target
&& target
[0] == '/')
3211 pev
->uprobes
= true;
3217 /* Try to find probe_trace_event from all probe caches */
3218 static int find_cached_events_all(struct perf_probe_event
*pev
,
3219 struct probe_trace_event
**tevs
)
3221 struct probe_trace_event
*tmp_tevs
= NULL
;
3222 struct strlist
*bidlist
;
3223 struct str_node
*nd
;
3228 /* Get the buildid list of all valid caches */
3229 bidlist
= build_id_cache__list_all(true);
3232 pr_debug("Failed to get buildids: %d\n", ret
);
3237 strlist__for_each_entry(nd
, bidlist
) {
3238 pathname
= build_id_cache__origname(nd
->s
);
3239 ret
= find_cached_events(pev
, &tmp_tevs
, pathname
);
3240 /* In the case of cnt == 0, we just skip it */
3242 ret
= concat_probe_trace_events(tevs
, &ntevs
,
3248 strlist__delete(bidlist
);
3251 clear_probe_trace_events(*tevs
, ntevs
);
3259 static int find_probe_trace_events_from_cache(struct perf_probe_event
*pev
,
3260 struct probe_trace_event
**tevs
)
3262 struct probe_cache
*cache
;
3263 struct probe_cache_entry
*entry
;
3264 struct probe_trace_event
*tev
;
3265 struct str_node
*node
;
3269 /* For SDT/cached events, we use special search functions */
3271 return find_cached_events_all(pev
, tevs
);
3273 return find_cached_events(pev
, tevs
, pev
->target
);
3275 cache
= probe_cache__new(pev
->target
, pev
->nsi
);
3279 entry
= probe_cache__find(cache
, pev
);
3281 /* SDT must be in the cache */
3282 ret
= pev
->sdt
? -ENOENT
: 0;
3286 ret
= strlist__nr_entries(entry
->tevlist
);
3287 if (ret
> probe_conf
.max_probes
) {
3288 pr_debug("Too many entries matched in the cache of %s\n",
3289 pev
->target
? : "kernel");
3294 *tevs
= zalloc(ret
* sizeof(*tev
));
3301 strlist__for_each_entry(node
, entry
->tevlist
) {
3302 tev
= &(*tevs
)[i
++];
3303 ret
= parse_probe_trace_command(node
->s
, tev
);
3306 /* Set the uprobes attribute as same as original */
3307 tev
->uprobes
= pev
->uprobes
;
3312 probe_cache__delete(cache
);
3316 static int convert_to_probe_trace_events(struct perf_probe_event
*pev
,
3317 struct probe_trace_event
**tevs
)
3321 if (!pev
->group
&& !pev
->sdt
) {
3322 /* Set group name if not given */
3323 if (!pev
->uprobes
) {
3324 pev
->group
= strdup(PERFPROBE_GROUP
);
3325 ret
= pev
->group
? 0 : -ENOMEM
;
3327 ret
= convert_exec_to_group(pev
->target
, &pev
->group
);
3329 pr_warning("Failed to make a group name.\n");
3334 ret
= try_to_find_absolute_address(pev
, tevs
);
3338 /* At first, we need to lookup cache entry */
3339 ret
= find_probe_trace_events_from_cache(pev
, tevs
);
3340 if (ret
> 0 || pev
->sdt
) /* SDT can be found only in the cache */
3341 return ret
== 0 ? -ENOENT
: ret
; /* Found in probe cache */
3343 /* Convert perf_probe_event with debuginfo */
3344 ret
= try_to_find_probe_trace_events(pev
, tevs
);
3346 return ret
; /* Found in debuginfo or got an error */
3348 return find_probe_trace_events_from_map(pev
, tevs
);
3351 int convert_perf_probe_events(struct perf_probe_event
*pevs
, int npevs
)
3355 /* Loop 1: convert all events */
3356 for (i
= 0; i
< npevs
; i
++) {
3357 /* Init kprobe blacklist if needed */
3358 if (!pevs
[i
].uprobes
)
3359 kprobe_blacklist__init();
3360 /* Convert with or without debuginfo */
3361 ret
= convert_to_probe_trace_events(&pevs
[i
], &pevs
[i
].tevs
);
3364 pevs
[i
].ntevs
= ret
;
3366 /* This just release blacklist only if allocated */
3367 kprobe_blacklist__release();
3372 static int show_probe_trace_event(struct probe_trace_event
*tev
)
3374 char *buf
= synthesize_probe_trace_command(tev
);
3377 pr_debug("Failed to synthesize probe trace event.\n");
3381 /* Showing definition always go stdout */
3382 printf("%s\n", buf
);
3388 int show_probe_trace_events(struct perf_probe_event
*pevs
, int npevs
)
3390 struct strlist
*namelist
= strlist__new(NULL
, NULL
);
3391 struct probe_trace_event
*tev
;
3392 struct perf_probe_event
*pev
;
3398 for (j
= 0; j
< npevs
&& !ret
; j
++) {
3400 for (i
= 0; i
< pev
->ntevs
&& !ret
; i
++) {
3401 tev
= &pev
->tevs
[i
];
3402 /* Skip if the symbol is out of .text or blacklisted */
3403 if (!tev
->point
.symbol
&& !pev
->uprobes
)
3406 /* Set new name for tev (and update namelist) */
3407 ret
= probe_trace_event__set_name(tev
, pev
,
3410 ret
= show_probe_trace_event(tev
);
3413 strlist__delete(namelist
);
3418 int apply_perf_probe_events(struct perf_probe_event
*pevs
, int npevs
)
3422 /* Loop 2: add all events */
3423 for (i
= 0; i
< npevs
; i
++) {
3424 ret
= __add_probe_trace_events(&pevs
[i
], pevs
[i
].tevs
,
3426 probe_conf
.force_add
);
3433 void cleanup_perf_probe_events(struct perf_probe_event
*pevs
, int npevs
)
3436 struct perf_probe_event
*pev
;
3438 /* Loop 3: cleanup and free trace events */
3439 for (i
= 0; i
< npevs
; i
++) {
3441 for (j
= 0; j
< pevs
[i
].ntevs
; j
++)
3442 clear_probe_trace_event(&pevs
[i
].tevs
[j
]);
3443 zfree(&pevs
[i
].tevs
);
3445 nsinfo__zput(pev
->nsi
);
3446 clear_perf_probe_event(&pevs
[i
]);
3450 int add_perf_probe_events(struct perf_probe_event
*pevs
, int npevs
)
3454 ret
= init_probe_symbol_maps(pevs
->uprobes
);
3458 ret
= convert_perf_probe_events(pevs
, npevs
);
3460 ret
= apply_perf_probe_events(pevs
, npevs
);
3462 cleanup_perf_probe_events(pevs
, npevs
);
3464 exit_probe_symbol_maps();
3468 int del_perf_probe_events(struct strfilter
*filter
)
3470 int ret
, ret2
, ufd
= -1, kfd
= -1;
3471 char *str
= strfilter__string(filter
);
3476 /* Get current event names */
3477 ret
= probe_file__open_both(&kfd
, &ufd
, PF_FL_RW
);
3481 ret
= probe_file__del_events(kfd
, filter
);
3482 if (ret
< 0 && ret
!= -ENOENT
)
3485 ret2
= probe_file__del_events(ufd
, filter
);
3486 if (ret2
< 0 && ret2
!= -ENOENT
) {
3503 int show_available_funcs(const char *target
, struct nsinfo
*nsi
,
3504 struct strfilter
*_filter
, bool user
)
3510 ret
= init_probe_symbol_maps(user
);
3514 /* Get a symbol map */
3515 map
= get_target_map(target
, nsi
, user
);
3517 pr_err("Failed to get a map for %s\n", (target
) ? : "kernel");
3521 ret
= map__load(map
);
3524 char *str
= strfilter__string(_filter
);
3525 pr_err("Failed to find symbols matched to \"%s\"\n",
3529 pr_err("Failed to load symbols in %s\n",
3530 (target
) ? : "kernel");
3533 if (!dso__sorted_by_name(map
->dso
))
3534 dso__sort_by_name(map
->dso
);
3536 /* Show all (filtered) symbols */
3539 for (nd
= rb_first_cached(&map
->dso
->symbol_names
); nd
;
3541 struct symbol_name_rb_node
*pos
= rb_entry(nd
, struct symbol_name_rb_node
, rb_node
);
3543 if (strfilter__compare(_filter
, pos
->sym
.name
))
3544 printf("%s\n", pos
->sym
.name
);
3548 exit_probe_symbol_maps();
3553 int copy_to_probe_trace_arg(struct probe_trace_arg
*tvar
,
3554 struct perf_probe_arg
*pvar
)
3556 tvar
->value
= strdup(pvar
->var
);
3557 if (tvar
->value
== NULL
)
3560 tvar
->type
= strdup(pvar
->type
);
3561 if (tvar
->type
== NULL
)
3565 tvar
->name
= strdup(pvar
->name
);
3566 if (tvar
->name
== NULL
)