]>
git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - tools/perf/util/probe-event.c
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>
39 #include "strfilter.h"
45 #include <api/fs/fs.h>
46 #include "trace-event.h" /* For __maybe_unused */
47 #include "probe-event.h"
48 #include "probe-finder.h"
49 #include "probe-file.h"
53 #include "sane_ctype.h"
55 #define PERFPROBE_GROUP "probe"
57 bool probe_event_dry_run
; /* Dry run flag */
58 struct probe_conf probe_conf
;
60 #define semantic_error(msg ...) pr_err("Semantic error :" msg)
62 int e_snprintf(char *str
, size_t size
, const char *format
, ...)
67 ret
= vsnprintf(str
, size
, format
, ap
);
74 static struct machine
*host_machine
;
76 /* Initialize symbol maps and path of vmlinux/modules */
77 int init_probe_symbol_maps(bool user_only
)
81 symbol_conf
.sort_by_name
= true;
82 symbol_conf
.allow_aliases
= true;
83 ret
= symbol__init(NULL
);
85 pr_debug("Failed to init symbol map.\n");
89 if (host_machine
|| user_only
) /* already initialized */
92 if (symbol_conf
.vmlinux_name
)
93 pr_debug("Use vmlinux: %s\n", symbol_conf
.vmlinux_name
);
95 host_machine
= machine__new_host();
97 pr_debug("machine__new_host() failed.\n");
103 pr_warning("Failed to init vmlinux path.\n");
107 void exit_probe_symbol_maps(void)
109 machine__delete(host_machine
);
114 static struct symbol
*__find_kernel_function_by_name(const char *name
,
117 return machine__find_kernel_function_by_name(host_machine
, name
, mapp
);
120 static struct symbol
*__find_kernel_function(u64 addr
, struct map
**mapp
)
122 return machine__find_kernel_function(host_machine
, addr
, mapp
);
125 static struct ref_reloc_sym
*kernel_get_ref_reloc_sym(void)
127 /* kmap->ref_reloc_sym should be set if host_machine is initialized */
129 struct map
*map
= machine__kernel_map(host_machine
);
131 if (map__load(map
) < 0)
134 kmap
= map__kmap(map
);
137 return kmap
->ref_reloc_sym
;
140 static int kernel_get_symbol_address_by_name(const char *name
, u64
*addr
,
141 bool reloc
, bool reladdr
)
143 struct ref_reloc_sym
*reloc_sym
;
147 /* ref_reloc_sym is just a label. Need a special fix*/
148 reloc_sym
= kernel_get_ref_reloc_sym();
149 if (reloc_sym
&& strcmp(name
, reloc_sym
->name
) == 0)
150 *addr
= (reloc
) ? reloc_sym
->addr
: reloc_sym
->unrelocated_addr
;
152 sym
= __find_kernel_function_by_name(name
, &map
);
155 *addr
= map
->unmap_ip(map
, sym
->start
) -
156 ((reloc
) ? 0 : map
->reloc
) -
157 ((reladdr
) ? map
->start
: 0);
162 static struct map
*kernel_get_module_map(const char *module
)
164 struct map_groups
*grp
= &host_machine
->kmaps
;
165 struct maps
*maps
= &grp
->maps
[MAP__FUNCTION
];
168 /* A file path -- this is an offline module */
169 if (module
&& strchr(module
, '/'))
170 return dso__new_map(module
);
173 pos
= machine__kernel_map(host_machine
);
174 return map__get(pos
);
177 for (pos
= maps__first(maps
); pos
; pos
= map__next(pos
)) {
178 /* short_name is "[module]" */
179 if (strncmp(pos
->dso
->short_name
+ 1, module
,
180 pos
->dso
->short_name_len
- 2) == 0 &&
181 module
[pos
->dso
->short_name_len
- 2] == '\0') {
189 struct map
*get_target_map(const char *target
, struct nsinfo
*nsi
, bool user
)
191 /* Init maps of given executable or kernel */
195 map
= dso__new_map(target
);
197 map
->dso
->nsinfo
= nsinfo__get(nsi
);
200 return kernel_get_module_map(target
);
204 static int convert_exec_to_group(const char *exec
, char **result
)
206 char *ptr1
, *ptr2
, *exec_copy
;
210 exec_copy
= strdup(exec
);
214 ptr1
= basename(exec_copy
);
220 for (ptr2
= ptr1
; *ptr2
!= '\0'; ptr2
++) {
221 if (!isalnum(*ptr2
) && *ptr2
!= '_') {
227 ret
= e_snprintf(buf
, 64, "%s_%s", PERFPROBE_GROUP
, ptr1
);
231 *result
= strdup(buf
);
232 ret
= *result
? 0 : -ENOMEM
;
239 static void clear_perf_probe_point(struct perf_probe_point
*pp
)
246 static void clear_probe_trace_events(struct probe_trace_event
*tevs
, int ntevs
)
250 for (i
= 0; i
< ntevs
; i
++)
251 clear_probe_trace_event(tevs
+ i
);
254 static bool kprobe_blacklist__listed(unsigned long address
);
255 static bool kprobe_warn_out_range(const char *symbol
, unsigned long address
)
260 /* Get the address of _etext for checking non-probable text symbol */
261 ret
= kernel_get_symbol_address_by_name("_etext", &etext_addr
,
264 if (ret
== 0 && etext_addr
< address
)
265 pr_warning("%s is out of .text, skip it.\n", symbol
);
266 else if (kprobe_blacklist__listed(address
))
267 pr_warning("%s is blacklisted function, skip it.\n", symbol
);
275 * @module can be module name of module file path. In case of path,
276 * inspect elf and find out what is actual module name.
277 * Caller has to free mod_name after using it.
279 static char *find_module_name(const char *module
)
287 char *mod_name
= NULL
;
290 fd
= open(module
, O_RDONLY
);
294 elf
= elf_begin(fd
, PERF_ELF_C_READ_MMAP
, NULL
);
298 if (gelf_getehdr(elf
, &ehdr
) == NULL
)
301 sec
= elf_section_by_name(elf
, &ehdr
, &shdr
,
302 ".gnu.linkonce.this_module", NULL
);
306 data
= elf_getdata(sec
, NULL
);
307 if (!data
|| !data
->d_buf
)
312 * '.gnu.linkonce.this_module' section of kernel module elf directly
313 * maps to 'struct module' from linux/module.h. This section contains
314 * actual module name which will be used by kernel after loading it.
315 * But, we cannot use 'struct module' here since linux/module.h is not
316 * exposed to user-space. Offset of 'name' has remained same from long
317 * time, so hardcoding it here.
319 if (ehdr
.e_ident
[EI_CLASS
] == ELFCLASS32
)
321 else /* expect ELFCLASS64 by default */
324 mod_name
= strdup((char *)data
->d_buf
+ name_offset
);
333 #ifdef HAVE_DWARF_SUPPORT
335 static int kernel_get_module_dso(const char *module
, struct dso
**pdso
)
339 const char *vmlinux_name
;
343 char module_name
[128];
345 snprintf(module_name
, sizeof(module_name
), "[%s]", module
);
346 map
= map_groups__find_by_name(&host_machine
->kmaps
, MAP__FUNCTION
, module_name
);
351 pr_debug("Failed to find module %s.\n", module
);
355 map
= machine__kernel_map(host_machine
);
358 vmlinux_name
= symbol_conf
.vmlinux_name
;
361 ret
= dso__load_vmlinux(dso
, map
, vmlinux_name
, false);
363 ret
= dso__load_vmlinux_path(dso
, map
);
370 * Some binaries like glibc have special symbols which are on the symbol
371 * table, but not in the debuginfo. If we can find the address of the
372 * symbol from map, we can translate the address back to the probe point.
374 static int find_alternative_probe_point(struct debuginfo
*dinfo
,
375 struct perf_probe_point
*pp
,
376 struct perf_probe_point
*result
,
377 const char *target
, struct nsinfo
*nsi
,
380 struct map
*map
= NULL
;
385 /* This can work only for function-name based one */
386 if (!pp
->function
|| pp
->file
)
389 map
= get_target_map(target
, nsi
, uprobes
);
393 /* Find the address of given function */
394 map__for_each_symbol_by_name(map
, pp
->function
, sym
) {
396 address
= sym
->start
;
398 address
= map
->unmap_ip(map
, sym
->start
) - map
->reloc
;
405 pr_debug("Symbol %s address found : %" PRIx64
"\n",
406 pp
->function
, address
);
408 ret
= debuginfo__find_probe_point(dinfo
, (unsigned long)address
,
411 ret
= (!ret
) ? -ENOENT
: ret
;
413 result
->offset
+= pp
->offset
;
414 result
->line
+= pp
->line
;
415 result
->retprobe
= pp
->retprobe
;
425 static int get_alternative_probe_event(struct debuginfo
*dinfo
,
426 struct perf_probe_event
*pev
,
427 struct perf_probe_point
*tmp
)
431 memcpy(tmp
, &pev
->point
, sizeof(*tmp
));
432 memset(&pev
->point
, 0, sizeof(pev
->point
));
433 ret
= find_alternative_probe_point(dinfo
, tmp
, &pev
->point
, pev
->target
,
434 pev
->nsi
, pev
->uprobes
);
436 memcpy(&pev
->point
, tmp
, sizeof(*tmp
));
441 static int get_alternative_line_range(struct debuginfo
*dinfo
,
442 struct line_range
*lr
,
443 const char *target
, bool user
)
445 struct perf_probe_point pp
= { .function
= lr
->function
,
448 struct perf_probe_point result
;
451 memset(&result
, 0, sizeof(result
));
453 if (lr
->end
!= INT_MAX
)
454 len
= lr
->end
- lr
->start
;
455 ret
= find_alternative_probe_point(dinfo
, &pp
, &result
,
458 lr
->function
= result
.function
;
459 lr
->file
= result
.file
;
460 lr
->start
= result
.line
;
461 if (lr
->end
!= INT_MAX
)
462 lr
->end
= lr
->start
+ len
;
463 clear_perf_probe_point(&pp
);
468 /* Open new debuginfo of given module */
469 static struct debuginfo
*open_debuginfo(const char *module
, struct nsinfo
*nsi
,
472 const char *path
= module
;
473 char reason
[STRERR_BUFSIZE
];
474 struct debuginfo
*ret
= NULL
;
475 struct dso
*dso
= NULL
;
479 if (!module
|| !strchr(module
, '/')) {
480 err
= kernel_get_module_dso(module
, &dso
);
482 if (!dso
|| dso
->load_errno
== 0) {
483 if (!str_error_r(-err
, reason
, STRERR_BUFSIZE
))
484 strcpy(reason
, "(unknown)");
486 dso__strerror_load(dso
, reason
, STRERR_BUFSIZE
);
488 pr_err("Failed to find the path for %s: %s\n",
489 module
?: "kernel", reason
);
492 path
= dso
->long_name
;
494 nsinfo__mountns_enter(nsi
, &nsc
);
495 ret
= debuginfo__new(path
);
496 if (!ret
&& !silent
) {
497 pr_warning("The %s file has no debug information.\n", path
);
498 if (!module
|| !strtailcmp(path
, ".ko"))
499 pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, ");
501 pr_warning("Rebuild with -g, ");
502 pr_warning("or install an appropriate debuginfo package.\n");
504 nsinfo__mountns_exit(&nsc
);
508 /* For caching the last debuginfo */
509 static struct debuginfo
*debuginfo_cache
;
510 static char *debuginfo_cache_path
;
512 static struct debuginfo
*debuginfo_cache__open(const char *module
, bool silent
)
514 const char *path
= module
;
516 /* If the module is NULL, it should be the kernel. */
520 if (debuginfo_cache_path
&& !strcmp(debuginfo_cache_path
, path
))
523 /* Copy module path */
524 free(debuginfo_cache_path
);
525 debuginfo_cache_path
= strdup(path
);
526 if (!debuginfo_cache_path
) {
527 debuginfo__delete(debuginfo_cache
);
528 debuginfo_cache
= NULL
;
532 debuginfo_cache
= open_debuginfo(module
, NULL
, silent
);
533 if (!debuginfo_cache
)
534 zfree(&debuginfo_cache_path
);
536 return debuginfo_cache
;
539 static void debuginfo_cache__exit(void)
541 debuginfo__delete(debuginfo_cache
);
542 debuginfo_cache
= NULL
;
543 zfree(&debuginfo_cache_path
);
547 static int get_text_start_address(const char *exec
, unsigned long *address
,
553 int fd
, ret
= -ENOENT
;
556 nsinfo__mountns_enter(nsi
, &nsc
);
557 fd
= open(exec
, O_RDONLY
);
558 nsinfo__mountns_exit(&nsc
);
562 elf
= elf_begin(fd
, PERF_ELF_C_READ_MMAP
, NULL
);
568 if (gelf_getehdr(elf
, &ehdr
) == NULL
)
571 if (!elf_section_by_name(elf
, &ehdr
, &shdr
, ".text", NULL
))
574 *address
= shdr
.sh_addr
- shdr
.sh_offset
;
585 * Convert trace point to probe point with debuginfo
587 static int find_perf_probe_point_from_dwarf(struct probe_trace_point
*tp
,
588 struct perf_probe_point
*pp
,
591 struct debuginfo
*dinfo
= NULL
;
592 unsigned long stext
= 0;
593 u64 addr
= tp
->address
;
596 /* convert the address to dwarf address */
602 ret
= get_text_start_address(tp
->module
, &stext
, NULL
);
606 } else if (tp
->symbol
) {
607 /* If the module is given, this returns relative address */
608 ret
= kernel_get_symbol_address_by_name(tp
->symbol
, &addr
,
609 false, !!tp
->module
);
615 pr_debug("try to find information at %" PRIx64
" in %s\n", addr
,
616 tp
->module
? : "kernel");
618 dinfo
= debuginfo_cache__open(tp
->module
, verbose
<= 0);
620 ret
= debuginfo__find_probe_point(dinfo
,
621 (unsigned long)addr
, pp
);
626 pp
->retprobe
= tp
->retprobe
;
630 pr_debug("Failed to find corresponding probes from debuginfo.\n");
631 return ret
? : -ENOENT
;
634 /* Adjust symbol name and address */
635 static int post_process_probe_trace_point(struct probe_trace_point
*tp
,
636 struct map
*map
, unsigned long offs
)
639 u64 addr
= tp
->address
- offs
;
641 sym
= map__find_symbol(map
, addr
);
645 if (strcmp(sym
->name
, tp
->symbol
)) {
646 /* If we have no realname, use symbol for it */
648 tp
->realname
= tp
->symbol
;
651 tp
->symbol
= strdup(sym
->name
);
655 tp
->offset
= addr
- sym
->start
;
662 * Rename DWARF symbols to ELF symbols -- gcc sometimes optimizes functions
663 * and generate new symbols with suffixes such as .constprop.N or .isra.N
664 * etc. Since those symbols are not recorded in DWARF, we have to find
665 * correct generated symbols from offline ELF binary.
666 * For online kernel or uprobes we don't need this because those are
667 * rebased on _text, or already a section relative address.
670 post_process_offline_probe_trace_events(struct probe_trace_event
*tevs
,
671 int ntevs
, const char *pathname
)
674 unsigned long stext
= 0;
677 /* Prepare a map for offline binary */
678 map
= dso__new_map(pathname
);
679 if (!map
|| get_text_start_address(pathname
, &stext
, NULL
) < 0) {
680 pr_warning("Failed to get ELF symbols for %s\n", pathname
);
684 for (i
= 0; i
< ntevs
; i
++) {
685 ret
= post_process_probe_trace_point(&tevs
[i
].point
,
695 static int add_exec_to_probe_trace_events(struct probe_trace_event
*tevs
,
696 int ntevs
, const char *exec
,
700 unsigned long stext
= 0;
705 ret
= get_text_start_address(exec
, &stext
, nsi
);
709 for (i
= 0; i
< ntevs
&& ret
>= 0; i
++) {
710 /* point.address is the addres of point.symbol + point.offset */
711 tevs
[i
].point
.address
-= stext
;
712 tevs
[i
].point
.module
= strdup(exec
);
713 if (!tevs
[i
].point
.module
) {
717 tevs
[i
].uprobes
= true;
724 post_process_module_probe_trace_events(struct probe_trace_event
*tevs
,
725 int ntevs
, const char *module
,
726 struct debuginfo
*dinfo
)
728 Dwarf_Addr text_offs
= 0;
730 char *mod_name
= NULL
;
736 map
= get_target_map(module
, NULL
, false);
737 if (!map
|| debuginfo__get_text_offset(dinfo
, &text_offs
, true) < 0) {
738 pr_warning("Failed to get ELF symbols for %s\n", module
);
742 mod_name
= find_module_name(module
);
743 for (i
= 0; i
< ntevs
; i
++) {
744 ret
= post_process_probe_trace_point(&tevs
[i
].point
,
745 map
, (unsigned long)text_offs
);
748 tevs
[i
].point
.module
=
749 strdup(mod_name
? mod_name
: module
);
750 if (!tevs
[i
].point
.module
) {
763 post_process_kernel_probe_trace_events(struct probe_trace_event
*tevs
,
766 struct ref_reloc_sym
*reloc_sym
;
770 /* Skip post process if the target is an offline kernel */
771 if (symbol_conf
.ignore_vmlinux_buildid
)
772 return post_process_offline_probe_trace_events(tevs
, ntevs
,
773 symbol_conf
.vmlinux_name
);
775 reloc_sym
= kernel_get_ref_reloc_sym();
777 pr_warning("Relocated base symbol is not found!\n");
781 for (i
= 0; i
< ntevs
; i
++) {
782 if (!tevs
[i
].point
.address
)
784 if (tevs
[i
].point
.retprobe
&& !kretprobe_offset_is_supported())
786 /* If we found a wrong one, mark it by NULL symbol */
787 if (kprobe_warn_out_range(tevs
[i
].point
.symbol
,
788 tevs
[i
].point
.address
)) {
792 tmp
= strdup(reloc_sym
->name
);
796 /* If we have no realname, use symbol for it */
797 if (!tevs
[i
].point
.realname
)
798 tevs
[i
].point
.realname
= tevs
[i
].point
.symbol
;
800 free(tevs
[i
].point
.symbol
);
801 tevs
[i
].point
.symbol
= tmp
;
802 tevs
[i
].point
.offset
= tevs
[i
].point
.address
-
803 reloc_sym
->unrelocated_addr
;
809 arch__post_process_probe_trace_events(struct perf_probe_event
*pev __maybe_unused
,
810 int ntevs __maybe_unused
)
814 /* Post processing the probe events */
815 static int post_process_probe_trace_events(struct perf_probe_event
*pev
,
816 struct probe_trace_event
*tevs
,
817 int ntevs
, const char *module
,
818 bool uprobe
, struct debuginfo
*dinfo
)
823 ret
= add_exec_to_probe_trace_events(tevs
, ntevs
, module
,
826 /* Currently ref_reloc_sym based probe is not for drivers */
827 ret
= post_process_module_probe_trace_events(tevs
, ntevs
,
830 ret
= post_process_kernel_probe_trace_events(tevs
, ntevs
);
833 arch__post_process_probe_trace_events(pev
, ntevs
);
838 /* Try to find perf_probe_event with debuginfo */
839 static int try_to_find_probe_trace_events(struct perf_probe_event
*pev
,
840 struct probe_trace_event
**tevs
)
842 bool need_dwarf
= perf_probe_event_need_dwarf(pev
);
843 struct perf_probe_point tmp
;
844 struct debuginfo
*dinfo
;
847 dinfo
= open_debuginfo(pev
->target
, pev
->nsi
, !need_dwarf
);
851 pr_debug("Could not open debuginfo. Try to use symbols.\n");
855 pr_debug("Try to find probe point from debuginfo.\n");
856 /* Searching trace events corresponding to a probe event */
857 ntevs
= debuginfo__find_trace_events(dinfo
, pev
, tevs
);
859 if (ntevs
== 0) { /* Not found, retry with an alternative */
860 ret
= get_alternative_probe_event(dinfo
, pev
, &tmp
);
862 ntevs
= debuginfo__find_trace_events(dinfo
, pev
, tevs
);
864 * Write back to the original probe_event for
865 * setting appropriate (user given) event name
867 clear_perf_probe_point(&pev
->point
);
868 memcpy(&pev
->point
, &tmp
, sizeof(tmp
));
872 if (ntevs
> 0) { /* Succeeded to find trace events */
873 pr_debug("Found %d probe_trace_events.\n", ntevs
);
874 ret
= post_process_probe_trace_events(pev
, *tevs
, ntevs
,
875 pev
->target
, pev
->uprobes
, dinfo
);
876 if (ret
< 0 || ret
== ntevs
) {
877 pr_debug("Post processing failed or all events are skipped. (%d)\n", ret
);
878 clear_probe_trace_events(*tevs
, ntevs
);
884 debuginfo__delete(dinfo
);
886 if (ntevs
== 0) { /* No error but failed to find probe point. */
887 pr_warning("Probe point '%s' not found.\n",
888 synthesize_perf_probe_point(&pev
->point
));
890 } else if (ntevs
< 0) {
891 /* Error path : ntevs < 0 */
892 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs
);
894 pr_warning("Warning: No dwarf info found in the vmlinux - "
895 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
897 pr_debug("Trying to use symbols.\n");
904 #define LINEBUF_SIZE 256
905 #define NR_ADDITIONAL_LINES 2
907 static int __show_one_line(FILE *fp
, int l
, bool skip
, bool show_num
)
909 char buf
[LINEBUF_SIZE
], sbuf
[STRERR_BUFSIZE
];
910 const char *color
= show_num
? "" : PERF_COLOR_BLUE
;
911 const char *prefix
= NULL
;
914 if (fgets(buf
, LINEBUF_SIZE
, fp
) == NULL
)
919 prefix
= show_num
? "%7d " : " ";
920 color_fprintf(stdout
, color
, prefix
, l
);
922 color_fprintf(stdout
, color
, "%s", buf
);
924 } while (strchr(buf
, '\n') == NULL
);
929 pr_warning("File read error: %s\n",
930 str_error_r(errno
, sbuf
, sizeof(sbuf
)));
936 static int _show_one_line(FILE *fp
, int l
, bool skip
, bool show_num
)
938 int rv
= __show_one_line(fp
, l
, skip
, show_num
);
940 pr_warning("Source file is shorter than expected.\n");
946 #define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
947 #define show_one_line(f,l) _show_one_line(f,l,false,false)
948 #define skip_one_line(f,l) _show_one_line(f,l,true,false)
949 #define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
952 * Show line-range always requires debuginfo to find source file and
955 static int __show_line_range(struct line_range
*lr
, const char *module
,
960 struct debuginfo
*dinfo
;
964 char sbuf
[STRERR_BUFSIZE
];
966 /* Search a line range */
967 dinfo
= open_debuginfo(module
, NULL
, false);
971 ret
= debuginfo__find_line_range(dinfo
, lr
);
972 if (!ret
) { /* Not found, retry with an alternative */
973 ret
= get_alternative_line_range(dinfo
, lr
, module
, user
);
975 ret
= debuginfo__find_line_range(dinfo
, lr
);
977 debuginfo__delete(dinfo
);
978 if (ret
== 0 || ret
== -ENOENT
) {
979 pr_warning("Specified source line is not found.\n");
981 } else if (ret
< 0) {
982 pr_warning("Debuginfo analysis failed.\n");
986 /* Convert source file path */
988 ret
= get_real_path(tmp
, lr
->comp_dir
, &lr
->path
);
990 /* Free old path when new path is assigned */
995 pr_warning("Failed to find source file path.\n");
1002 fprintf(stdout
, "<%s@%s:%d>\n", lr
->function
, lr
->path
,
1003 lr
->start
- lr
->offset
);
1005 fprintf(stdout
, "<%s:%d>\n", lr
->path
, lr
->start
);
1007 fp
= fopen(lr
->path
, "r");
1009 pr_warning("Failed to open %s: %s\n", lr
->path
,
1010 str_error_r(errno
, sbuf
, sizeof(sbuf
)));
1013 /* Skip to starting line number */
1014 while (l
< lr
->start
) {
1015 ret
= skip_one_line(fp
, l
++);
1020 intlist__for_each_entry(ln
, lr
->line_list
) {
1021 for (; ln
->i
> l
; l
++) {
1022 ret
= show_one_line(fp
, l
- lr
->offset
);
1026 ret
= show_one_line_with_num(fp
, l
++ - lr
->offset
);
1031 if (lr
->end
== INT_MAX
)
1032 lr
->end
= l
+ NR_ADDITIONAL_LINES
;
1033 while (l
<= lr
->end
) {
1034 ret
= show_one_line_or_eof(fp
, l
++ - lr
->offset
);
1043 int show_line_range(struct line_range
*lr
, const char *module
,
1044 struct nsinfo
*nsi
, bool user
)
1047 struct nscookie nsc
;
1049 ret
= init_probe_symbol_maps(user
);
1052 nsinfo__mountns_enter(nsi
, &nsc
);
1053 ret
= __show_line_range(lr
, module
, user
);
1054 nsinfo__mountns_exit(&nsc
);
1055 exit_probe_symbol_maps();
1060 static int show_available_vars_at(struct debuginfo
*dinfo
,
1061 struct perf_probe_event
*pev
,
1062 struct strfilter
*_filter
)
1066 struct str_node
*node
;
1067 struct variable_list
*vls
= NULL
, *vl
;
1068 struct perf_probe_point tmp
;
1071 buf
= synthesize_perf_probe_point(&pev
->point
);
1074 pr_debug("Searching variables at %s\n", buf
);
1076 ret
= debuginfo__find_available_vars_at(dinfo
, pev
, &vls
);
1077 if (!ret
) { /* Not found, retry with an alternative */
1078 ret
= get_alternative_probe_event(dinfo
, pev
, &tmp
);
1080 ret
= debuginfo__find_available_vars_at(dinfo
, pev
,
1082 /* Release the old probe_point */
1083 clear_perf_probe_point(&tmp
);
1087 if (ret
== 0 || ret
== -ENOENT
) {
1088 pr_err("Failed to find the address of %s\n", buf
);
1091 pr_warning("Debuginfo analysis failed.\n");
1095 /* Some variables are found */
1096 fprintf(stdout
, "Available variables at %s\n", buf
);
1097 for (i
= 0; i
< ret
; i
++) {
1100 * A probe point might be converted to
1101 * several trace points.
1103 fprintf(stdout
, "\t@<%s+%lu>\n", vl
->point
.symbol
,
1105 zfree(&vl
->point
.symbol
);
1108 strlist__for_each_entry(node
, vl
->vars
) {
1109 var
= strchr(node
->s
, '\t') + 1;
1110 if (strfilter__compare(_filter
, var
)) {
1111 fprintf(stdout
, "\t\t%s\n", node
->s
);
1115 strlist__delete(vl
->vars
);
1118 fprintf(stdout
, "\t\t(No matched variables)\n");
1126 /* Show available variables on given probe point */
1127 int show_available_vars(struct perf_probe_event
*pevs
, int npevs
,
1128 struct strfilter
*_filter
)
1131 struct debuginfo
*dinfo
;
1133 ret
= init_probe_symbol_maps(pevs
->uprobes
);
1137 dinfo
= open_debuginfo(pevs
->target
, pevs
->nsi
, false);
1145 for (i
= 0; i
< npevs
&& ret
>= 0; i
++)
1146 ret
= show_available_vars_at(dinfo
, &pevs
[i
], _filter
);
1148 debuginfo__delete(dinfo
);
1150 exit_probe_symbol_maps();
1154 #else /* !HAVE_DWARF_SUPPORT */
1156 static void debuginfo_cache__exit(void)
1161 find_perf_probe_point_from_dwarf(struct probe_trace_point
*tp __maybe_unused
,
1162 struct perf_probe_point
*pp __maybe_unused
,
1163 bool is_kprobe __maybe_unused
)
1168 static int try_to_find_probe_trace_events(struct perf_probe_event
*pev
,
1169 struct probe_trace_event
**tevs __maybe_unused
)
1171 if (perf_probe_event_need_dwarf(pev
)) {
1172 pr_warning("Debuginfo-analysis is not supported.\n");
1179 int show_line_range(struct line_range
*lr __maybe_unused
,
1180 const char *module __maybe_unused
,
1181 struct nsinfo
*nsi __maybe_unused
,
1182 bool user __maybe_unused
)
1184 pr_warning("Debuginfo-analysis is not supported.\n");
1188 int show_available_vars(struct perf_probe_event
*pevs __maybe_unused
,
1189 int npevs __maybe_unused
,
1190 struct strfilter
*filter __maybe_unused
)
1192 pr_warning("Debuginfo-analysis is not supported.\n");
1197 void line_range__clear(struct line_range
*lr
)
1203 intlist__delete(lr
->line_list
);
1204 memset(lr
, 0, sizeof(*lr
));
1207 int line_range__init(struct line_range
*lr
)
1209 memset(lr
, 0, sizeof(*lr
));
1210 lr
->line_list
= intlist__new(NULL
);
1217 static int parse_line_num(char **ptr
, int *val
, const char *what
)
1219 const char *start
= *ptr
;
1222 *val
= strtol(*ptr
, ptr
, 0);
1223 if (errno
|| *ptr
== start
) {
1224 semantic_error("'%s' is not a valid number.\n", what
);
1230 /* Check the name is good for event, group or function */
1231 static bool is_c_func_name(const char *name
)
1233 if (!isalpha(*name
) && *name
!= '_')
1235 while (*++name
!= '\0') {
1236 if (!isalpha(*name
) && !isdigit(*name
) && *name
!= '_')
1243 * Stuff 'lr' according to the line range described by 'arg'.
1244 * The line range syntax is described by:
1246 * SRC[:SLN[+NUM|-ELN]]
1247 * FNC[@SRC][:SLN[+NUM|-ELN]]
1249 int parse_line_range_desc(const char *arg
, struct line_range
*lr
)
1251 char *range
, *file
, *name
= strdup(arg
);
1260 range
= strchr(name
, ':');
1264 err
= parse_line_num(&range
, &lr
->start
, "start line");
1268 if (*range
== '+' || *range
== '-') {
1269 const char c
= *range
++;
1271 err
= parse_line_num(&range
, &lr
->end
, "end line");
1276 lr
->end
+= lr
->start
;
1278 * Adjust the number of lines here.
1279 * If the number of lines == 1, the
1280 * the end of line should be equal to
1281 * the start of line.
1287 pr_debug("Line range is %d to %d\n", lr
->start
, lr
->end
);
1290 if (lr
->start
> lr
->end
) {
1291 semantic_error("Start line must be smaller"
1292 " than end line.\n");
1295 if (*range
!= '\0') {
1296 semantic_error("Tailing with invalid str '%s'.\n", range
);
1301 file
= strchr(name
, '@');
1304 lr
->file
= strdup(++file
);
1305 if (lr
->file
== NULL
) {
1309 lr
->function
= name
;
1310 } else if (strchr(name
, '/') || strchr(name
, '.'))
1312 else if (is_c_func_name(name
))/* We reuse it for checking funcname */
1313 lr
->function
= name
;
1314 else { /* Invalid name */
1315 semantic_error("'%s' is not a valid function name.\n", name
);
1326 static int parse_perf_probe_event_name(char **arg
, struct perf_probe_event
*pev
)
1330 ptr
= strchr(*arg
, ':');
1333 if (!pev
->sdt
&& !is_c_func_name(*arg
))
1335 pev
->group
= strdup(*arg
);
1341 if (!pev
->sdt
&& !is_c_func_name(*arg
)) {
1343 semantic_error("%s is bad for event name -it must "
1344 "follow C symbol-naming rule.\n", *arg
);
1347 pev
->event
= strdup(*arg
);
1348 if (pev
->event
== NULL
)
1354 /* Parse probepoint definition. */
1355 static int parse_perf_probe_point(char *arg
, struct perf_probe_event
*pev
)
1357 struct perf_probe_point
*pp
= &pev
->point
;
1360 bool file_spec
= false;
1365 * perf probe [GRP:][EVENT=]SRC[:LN|;PTN]
1366 * perf probe [GRP:][EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
1367 * perf probe %[GRP:]SDT_EVENT
1372 if (is_sdt_event(arg
)) {
1378 ptr
= strpbrk(arg
, ";=@+%");
1382 semantic_error("%s must be an SDT name.\n",
1386 /* This must be a target file name or build id */
1387 tmp
= build_id_cache__complement(ptr
+ 1);
1389 pev
->target
= build_id_cache__origname(tmp
);
1392 pev
->target
= strdup(ptr
+ 1);
1397 ret
= parse_perf_probe_event_name(&arg
, pev
);
1399 if (asprintf(&pev
->point
.function
, "%%%s", pev
->event
) < 0)
1405 if (ptr
&& *ptr
== '=') { /* Event name */
1408 ret
= parse_perf_probe_event_name(&arg
, pev
);
1416 * Check arg is function or file name and copy it.
1418 * We consider arg to be a file spec if and only if it satisfies
1419 * all of the below criteria::
1420 * - it does not include any of "+@%",
1421 * - it includes one of ":;", and
1422 * - it has a period '.' in the name.
1424 * Otherwise, we consider arg to be a function specification.
1426 if (!strpbrk(arg
, "+@%") && (ptr
= strpbrk(arg
, ";:")) != NULL
) {
1427 /* This is a file spec if it includes a '.' before ; or : */
1428 if (memchr(arg
, '.', ptr
- arg
))
1432 ptr
= strpbrk(arg
, ";:+@%");
1452 * Keep pp->function even if this is absolute address,
1453 * so it can mark whether abs_address is valid.
1454 * Which make 'perf probe lib.bin 0x0' possible.
1456 * Note that checking length of tmp is not needed
1457 * because when we access tmp[1] we know tmp[0] is '0',
1458 * so tmp[1] should always valid (but could be '\0').
1460 if (tmp
&& !strncmp(tmp
, "0x", 2)) {
1461 pp
->abs_address
= strtoul(pp
->function
, &tmp
, 0);
1463 semantic_error("Invalid absolute address.\n");
1469 /* Parse other options */
1473 if (c
== ';') { /* Lazy pattern must be the last part */
1474 pp
->lazy_line
= strdup(arg
);
1475 if (pp
->lazy_line
== NULL
)
1479 ptr
= strpbrk(arg
, ";:+@%");
1485 case ':': /* Line number */
1486 pp
->line
= strtoul(arg
, &tmp
, 0);
1488 semantic_error("There is non-digit char"
1489 " in line number.\n");
1493 case '+': /* Byte offset from a symbol */
1494 pp
->offset
= strtoul(arg
, &tmp
, 0);
1496 semantic_error("There is non-digit character"
1501 case '@': /* File name */
1503 semantic_error("SRC@SRC is not allowed.\n");
1506 pp
->file
= strdup(arg
);
1507 if (pp
->file
== NULL
)
1510 case '%': /* Probe places */
1511 if (strcmp(arg
, "return") == 0) {
1513 } else { /* Others not supported yet */
1514 semantic_error("%%%s is not supported.\n", arg
);
1518 default: /* Buggy case */
1519 pr_err("This program has a bug at %s:%d.\n",
1520 __FILE__
, __LINE__
);
1526 /* Exclusion check */
1527 if (pp
->lazy_line
&& pp
->line
) {
1528 semantic_error("Lazy pattern can't be used with"
1533 if (pp
->lazy_line
&& pp
->offset
) {
1534 semantic_error("Lazy pattern can't be used with offset.\n");
1538 if (pp
->line
&& pp
->offset
) {
1539 semantic_error("Offset can't be used with line number.\n");
1543 if (!pp
->line
&& !pp
->lazy_line
&& pp
->file
&& !pp
->function
) {
1544 semantic_error("File always requires line number or "
1549 if (pp
->offset
&& !pp
->function
) {
1550 semantic_error("Offset requires an entry function.\n");
1554 if ((pp
->offset
|| pp
->line
|| pp
->lazy_line
) && pp
->retprobe
) {
1555 semantic_error("Offset/Line/Lazy pattern can't be used with "
1560 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
1561 pp
->function
, pp
->file
, pp
->line
, pp
->offset
, pp
->retprobe
,
1566 /* Parse perf-probe event argument */
1567 static int parse_perf_probe_arg(char *str
, struct perf_probe_arg
*arg
)
1569 char *tmp
, *goodname
;
1570 struct perf_probe_arg_field
**fieldp
;
1572 pr_debug("parsing arg: %s into ", str
);
1574 tmp
= strchr(str
, '=');
1576 arg
->name
= strndup(str
, tmp
- str
);
1577 if (arg
->name
== NULL
)
1579 pr_debug("name:%s ", arg
->name
);
1583 tmp
= strchr(str
, ':');
1584 if (tmp
) { /* Type setting */
1586 arg
->type
= strdup(tmp
+ 1);
1587 if (arg
->type
== NULL
)
1589 pr_debug("type:%s ", arg
->type
);
1592 tmp
= strpbrk(str
, "-.[");
1593 if (!is_c_varname(str
) || !tmp
) {
1594 /* A variable, register, symbol or special value */
1595 arg
->var
= strdup(str
);
1596 if (arg
->var
== NULL
)
1598 pr_debug("%s\n", arg
->var
);
1602 /* Structure fields or array element */
1603 arg
->var
= strndup(str
, tmp
- str
);
1604 if (arg
->var
== NULL
)
1606 goodname
= arg
->var
;
1607 pr_debug("%s, ", arg
->var
);
1608 fieldp
= &arg
->field
;
1611 *fieldp
= zalloc(sizeof(struct perf_probe_arg_field
));
1612 if (*fieldp
== NULL
)
1614 if (*tmp
== '[') { /* Array */
1616 (*fieldp
)->index
= strtol(str
+ 1, &tmp
, 0);
1617 (*fieldp
)->ref
= true;
1618 if (*tmp
!= ']' || tmp
== str
+ 1) {
1619 semantic_error("Array index must be a"
1626 } else { /* Structure */
1629 (*fieldp
)->ref
= false;
1630 } else if (tmp
[1] == '>') {
1632 (*fieldp
)->ref
= true;
1634 semantic_error("Argument parse error: %s\n",
1638 tmp
= strpbrk(str
, "-.[");
1641 (*fieldp
)->name
= strndup(str
, tmp
- str
);
1642 if ((*fieldp
)->name
== NULL
)
1645 goodname
= (*fieldp
)->name
;
1646 pr_debug("%s(%d), ", (*fieldp
)->name
, (*fieldp
)->ref
);
1647 fieldp
= &(*fieldp
)->next
;
1650 (*fieldp
)->name
= strdup(str
);
1651 if ((*fieldp
)->name
== NULL
)
1654 goodname
= (*fieldp
)->name
;
1655 pr_debug("%s(%d)\n", (*fieldp
)->name
, (*fieldp
)->ref
);
1657 /* If no name is specified, set the last field name (not array index)*/
1659 arg
->name
= strdup(goodname
);
1660 if (arg
->name
== NULL
)
1666 /* Parse perf-probe event command */
1667 int parse_perf_probe_command(const char *cmd
, struct perf_probe_event
*pev
)
1670 int argc
, i
, ret
= 0;
1672 argv
= argv_split(cmd
, &argc
);
1674 pr_debug("Failed to split arguments.\n");
1677 if (argc
- 1 > MAX_PROBE_ARGS
) {
1678 semantic_error("Too many probe arguments (%d).\n", argc
- 1);
1682 /* Parse probe point */
1683 ret
= parse_perf_probe_point(argv
[0], pev
);
1687 /* Copy arguments and ensure return probe has no C argument */
1688 pev
->nargs
= argc
- 1;
1689 pev
->args
= zalloc(sizeof(struct perf_probe_arg
) * pev
->nargs
);
1690 if (pev
->args
== NULL
) {
1694 for (i
= 0; i
< pev
->nargs
&& ret
>= 0; i
++) {
1695 ret
= parse_perf_probe_arg(argv
[i
+ 1], &pev
->args
[i
]);
1697 is_c_varname(pev
->args
[i
].var
) && pev
->point
.retprobe
) {
1698 semantic_error("You can't specify local variable for"
1709 /* Returns true if *any* ARG is either C variable, $params or $vars. */
1710 bool perf_probe_with_var(struct perf_probe_event
*pev
)
1714 for (i
= 0; i
< pev
->nargs
; i
++)
1715 if (is_c_varname(pev
->args
[i
].var
) ||
1716 !strcmp(pev
->args
[i
].var
, PROBE_ARG_PARAMS
) ||
1717 !strcmp(pev
->args
[i
].var
, PROBE_ARG_VARS
))
1722 /* Return true if this perf_probe_event requires debuginfo */
1723 bool perf_probe_event_need_dwarf(struct perf_probe_event
*pev
)
1725 if (pev
->point
.file
|| pev
->point
.line
|| pev
->point
.lazy_line
)
1728 if (perf_probe_with_var(pev
))
1734 /* Parse probe_events event into struct probe_point */
1735 int parse_probe_trace_command(const char *cmd
, struct probe_trace_event
*tev
)
1737 struct probe_trace_point
*tp
= &tev
->point
;
1740 char *argv0_str
= NULL
, *fmt
, *fmt1_str
, *fmt2_str
, *fmt3_str
;
1744 pr_debug("Parsing probe_events: %s\n", cmd
);
1745 argv
= argv_split(cmd
, &argc
);
1747 pr_debug("Failed to split arguments.\n");
1751 semantic_error("Too few probe arguments.\n");
1756 /* Scan event and group name. */
1757 argv0_str
= strdup(argv
[0]);
1758 if (argv0_str
== NULL
) {
1762 fmt1_str
= strtok_r(argv0_str
, ":", &fmt
);
1763 fmt2_str
= strtok_r(NULL
, "/", &fmt
);
1764 fmt3_str
= strtok_r(NULL
, " \t", &fmt
);
1765 if (fmt1_str
== NULL
|| strlen(fmt1_str
) != 1 || fmt2_str
== NULL
1766 || fmt3_str
== NULL
) {
1767 semantic_error("Failed to parse event name: %s\n", argv
[0]);
1772 tev
->group
= strdup(fmt2_str
);
1773 tev
->event
= strdup(fmt3_str
);
1774 if (tev
->group
== NULL
|| tev
->event
== NULL
) {
1778 pr_debug("Group:%s Event:%s probe:%c\n", tev
->group
, tev
->event
, pr
);
1780 tp
->retprobe
= (pr
== 'r');
1782 /* Scan module name(if there), function name and offset */
1783 p
= strchr(argv
[1], ':');
1785 tp
->module
= strndup(argv
[1], p
- argv
[1]);
1790 tev
->uprobes
= (tp
->module
[0] == '/');
1794 fmt1_str
= strtok_r(p
, "+", &fmt
);
1795 /* only the address started with 0x */
1796 if (fmt1_str
[0] == '0') {
1798 * Fix a special case:
1799 * if address == 0, kernel reports something like:
1800 * p:probe_libc/abs_0 /lib/libc-2.18.so:0x (null) arg1=%ax
1801 * Newer kernel may fix that, but we want to
1802 * support old kernel also.
1804 if (strcmp(fmt1_str
, "0x") == 0) {
1805 if (!argv
[2] || strcmp(argv
[2], "(null)")) {
1812 for (i
= 2; argv
[i
+ 1] != NULL
; i
++)
1813 argv
[i
] = argv
[i
+ 1];
1818 tp
->address
= strtoul(fmt1_str
, NULL
, 0);
1820 /* Only the symbol-based probe has offset */
1821 tp
->symbol
= strdup(fmt1_str
);
1822 if (tp
->symbol
== NULL
) {
1826 fmt2_str
= strtok_r(NULL
, "", &fmt
);
1827 if (fmt2_str
== NULL
)
1830 tp
->offset
= strtoul(fmt2_str
, NULL
, 10);
1833 tev
->nargs
= argc
- 2;
1834 tev
->args
= zalloc(sizeof(struct probe_trace_arg
) * tev
->nargs
);
1835 if (tev
->args
== NULL
) {
1839 for (i
= 0; i
< tev
->nargs
; i
++) {
1840 p
= strchr(argv
[i
+ 2], '=');
1841 if (p
) /* We don't need which register is assigned. */
1845 tev
->args
[i
].name
= strdup(argv
[i
+ 2]);
1846 /* TODO: parse regs and offset */
1847 tev
->args
[i
].value
= strdup(p
);
1848 if (tev
->args
[i
].name
== NULL
|| tev
->args
[i
].value
== NULL
) {
1860 /* Compose only probe arg */
1861 char *synthesize_perf_probe_arg(struct perf_probe_arg
*pa
)
1863 struct perf_probe_arg_field
*field
= pa
->field
;
1868 if (strbuf_init(&buf
, 64) < 0)
1871 if (pa
->name
&& pa
->var
)
1872 err
= strbuf_addf(&buf
, "%s=%s", pa
->name
, pa
->var
);
1874 err
= strbuf_addstr(&buf
, pa
->name
?: pa
->var
);
1879 if (field
->name
[0] == '[')
1880 err
= strbuf_addstr(&buf
, field
->name
);
1882 err
= strbuf_addf(&buf
, "%s%s", field
->ref
? "->" : ".",
1884 field
= field
->next
;
1890 if (strbuf_addf(&buf
, ":%s", pa
->type
) < 0)
1893 ret
= strbuf_detach(&buf
, NULL
);
1895 strbuf_release(&buf
);
1899 /* Compose only probe point (not argument) */
1900 char *synthesize_perf_probe_point(struct perf_probe_point
*pp
)
1903 char *tmp
, *ret
= NULL
;
1906 if (strbuf_init(&buf
, 64) < 0)
1910 if (strbuf_addstr(&buf
, pp
->function
) < 0)
1913 err
= strbuf_addf(&buf
, "+%lu", pp
->offset
);
1915 err
= strbuf_addf(&buf
, ":%d", pp
->line
);
1916 else if (pp
->retprobe
)
1917 err
= strbuf_addstr(&buf
, "%return");
1925 tmp
= strchr(pp
->file
+ len
- 30, '/');
1926 tmp
= tmp
? tmp
+ 1 : pp
->file
+ len
- 30;
1928 err
= strbuf_addf(&buf
, "@%s", tmp
);
1929 if (!err
&& !pp
->function
&& pp
->line
)
1930 err
= strbuf_addf(&buf
, ":%d", pp
->line
);
1933 ret
= strbuf_detach(&buf
, NULL
);
1935 strbuf_release(&buf
);
1939 char *synthesize_perf_probe_command(struct perf_probe_event
*pev
)
1942 char *tmp
, *ret
= NULL
;
1945 if (strbuf_init(&buf
, 64))
1948 if (strbuf_addf(&buf
, "%s:%s=", pev
->group
?: PERFPROBE_GROUP
,
1952 tmp
= synthesize_perf_probe_point(&pev
->point
);
1953 if (!tmp
|| strbuf_addstr(&buf
, tmp
) < 0)
1957 for (i
= 0; i
< pev
->nargs
; i
++) {
1958 tmp
= synthesize_perf_probe_arg(pev
->args
+ i
);
1959 if (!tmp
|| strbuf_addf(&buf
, " %s", tmp
) < 0)
1964 ret
= strbuf_detach(&buf
, NULL
);
1966 strbuf_release(&buf
);
1970 static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref
*ref
,
1971 struct strbuf
*buf
, int depth
)
1975 depth
= __synthesize_probe_trace_arg_ref(ref
->next
, buf
,
1980 err
= strbuf_addf(buf
, "%+ld(", ref
->offset
);
1981 return (err
< 0) ? err
: depth
;
1984 static int synthesize_probe_trace_arg(struct probe_trace_arg
*arg
,
1987 struct probe_trace_arg_ref
*ref
= arg
->ref
;
1990 /* Argument name or separator */
1992 err
= strbuf_addf(buf
, " %s=", arg
->name
);
1994 err
= strbuf_addch(buf
, ' ');
1998 /* Special case: @XXX */
1999 if (arg
->value
[0] == '@' && arg
->ref
)
2002 /* Dereferencing arguments */
2004 depth
= __synthesize_probe_trace_arg_ref(ref
, buf
, 1);
2009 /* Print argument value */
2010 if (arg
->value
[0] == '@' && arg
->ref
)
2011 err
= strbuf_addf(buf
, "%s%+ld", arg
->value
, arg
->ref
->offset
);
2013 err
= strbuf_addstr(buf
, arg
->value
);
2016 while (!err
&& depth
--)
2017 err
= strbuf_addch(buf
, ')');
2019 /* Print argument type */
2020 if (!err
&& arg
->type
)
2021 err
= strbuf_addf(buf
, ":%s", arg
->type
);
2026 char *synthesize_probe_trace_command(struct probe_trace_event
*tev
)
2028 struct probe_trace_point
*tp
= &tev
->point
;
2033 /* Uprobes must have tp->module */
2034 if (tev
->uprobes
&& !tp
->module
)
2037 if (strbuf_init(&buf
, 32) < 0)
2040 if (strbuf_addf(&buf
, "%c:%s/%s ", tp
->retprobe
? 'r' : 'p',
2041 tev
->group
, tev
->event
) < 0)
2044 * If tp->address == 0, then this point must be a
2045 * absolute address uprobe.
2046 * try_to_find_absolute_address() should have made
2047 * tp->symbol to "0x0".
2049 if (tev
->uprobes
&& !tp
->address
) {
2050 if (!tp
->symbol
|| strcmp(tp
->symbol
, "0x0"))
2054 /* Use the tp->address for uprobes */
2056 err
= strbuf_addf(&buf
, "%s:0x%lx", tp
->module
, tp
->address
);
2057 else if (!strncmp(tp
->symbol
, "0x", 2))
2058 /* Absolute address. See try_to_find_absolute_address() */
2059 err
= strbuf_addf(&buf
, "%s%s0x%lx", tp
->module
?: "",
2060 tp
->module
? ":" : "", tp
->address
);
2062 err
= strbuf_addf(&buf
, "%s%s%s+%lu", tp
->module
?: "",
2063 tp
->module
? ":" : "", tp
->symbol
, tp
->offset
);
2067 for (i
= 0; i
< tev
->nargs
; i
++)
2068 if (synthesize_probe_trace_arg(&tev
->args
[i
], &buf
) < 0)
2071 ret
= strbuf_detach(&buf
, NULL
);
2073 strbuf_release(&buf
);
2077 static int find_perf_probe_point_from_map(struct probe_trace_point
*tp
,
2078 struct perf_probe_point
*pp
,
2081 struct symbol
*sym
= NULL
;
2082 struct map
*map
= NULL
;
2083 u64 addr
= tp
->address
;
2087 map
= dso__new_map(tp
->module
);
2090 sym
= map__find_symbol(map
, addr
);
2092 if (tp
->symbol
&& !addr
) {
2093 if (kernel_get_symbol_address_by_name(tp
->symbol
,
2094 &addr
, true, false) < 0)
2099 sym
= __find_kernel_function(addr
, &map
);
2106 pp
->retprobe
= tp
->retprobe
;
2107 pp
->offset
= addr
- map
->unmap_ip(map
, sym
->start
);
2108 pp
->function
= strdup(sym
->name
);
2109 ret
= pp
->function
? 0 : -ENOMEM
;
2112 if (map
&& !is_kprobe
) {
2119 static int convert_to_perf_probe_point(struct probe_trace_point
*tp
,
2120 struct perf_probe_point
*pp
,
2126 ret
= find_perf_probe_point_from_dwarf(tp
, pp
, is_kprobe
);
2129 ret
= find_perf_probe_point_from_map(tp
, pp
, is_kprobe
);
2133 pr_debug("Failed to find probe point from both of dwarf and map.\n");
2136 pp
->function
= strdup(tp
->symbol
);
2137 pp
->offset
= tp
->offset
;
2139 ret
= e_snprintf(buf
, 128, "0x%" PRIx64
, (u64
)tp
->address
);
2142 pp
->function
= strdup(buf
);
2145 if (pp
->function
== NULL
)
2148 pp
->retprobe
= tp
->retprobe
;
2153 static int convert_to_perf_probe_event(struct probe_trace_event
*tev
,
2154 struct perf_probe_event
*pev
, bool is_kprobe
)
2156 struct strbuf buf
= STRBUF_INIT
;
2159 /* Convert event/group name */
2160 pev
->event
= strdup(tev
->event
);
2161 pev
->group
= strdup(tev
->group
);
2162 if (pev
->event
== NULL
|| pev
->group
== NULL
)
2165 /* Convert trace_point to probe_point */
2166 ret
= convert_to_perf_probe_point(&tev
->point
, &pev
->point
, is_kprobe
);
2170 /* Convert trace_arg to probe_arg */
2171 pev
->nargs
= tev
->nargs
;
2172 pev
->args
= zalloc(sizeof(struct perf_probe_arg
) * pev
->nargs
);
2173 if (pev
->args
== NULL
)
2175 for (i
= 0; i
< tev
->nargs
&& ret
>= 0; i
++) {
2176 if (tev
->args
[i
].name
)
2177 pev
->args
[i
].name
= strdup(tev
->args
[i
].name
);
2179 if ((ret
= strbuf_init(&buf
, 32)) < 0)
2181 ret
= synthesize_probe_trace_arg(&tev
->args
[i
], &buf
);
2182 pev
->args
[i
].name
= strbuf_detach(&buf
, NULL
);
2184 if (pev
->args
[i
].name
== NULL
&& ret
>= 0)
2189 clear_perf_probe_event(pev
);
2194 void clear_perf_probe_event(struct perf_probe_event
*pev
)
2196 struct perf_probe_arg_field
*field
, *next
;
2202 clear_perf_probe_point(&pev
->point
);
2204 for (i
= 0; i
< pev
->nargs
; i
++) {
2205 free(pev
->args
[i
].name
);
2206 free(pev
->args
[i
].var
);
2207 free(pev
->args
[i
].type
);
2208 field
= pev
->args
[i
].field
;
2211 zfree(&field
->name
);
2217 memset(pev
, 0, sizeof(*pev
));
2220 #define strdup_or_goto(str, label) \
2221 ({ char *__p = NULL; if (str && !(__p = strdup(str))) goto label; __p; })
2223 static int perf_probe_point__copy(struct perf_probe_point
*dst
,
2224 struct perf_probe_point
*src
)
2226 dst
->file
= strdup_or_goto(src
->file
, out_err
);
2227 dst
->function
= strdup_or_goto(src
->function
, out_err
);
2228 dst
->lazy_line
= strdup_or_goto(src
->lazy_line
, out_err
);
2229 dst
->line
= src
->line
;
2230 dst
->retprobe
= src
->retprobe
;
2231 dst
->offset
= src
->offset
;
2235 clear_perf_probe_point(dst
);
2239 static int perf_probe_arg__copy(struct perf_probe_arg
*dst
,
2240 struct perf_probe_arg
*src
)
2242 struct perf_probe_arg_field
*field
, **ppfield
;
2244 dst
->name
= strdup_or_goto(src
->name
, out_err
);
2245 dst
->var
= strdup_or_goto(src
->var
, out_err
);
2246 dst
->type
= strdup_or_goto(src
->type
, out_err
);
2249 ppfield
= &(dst
->field
);
2251 *ppfield
= zalloc(sizeof(*field
));
2254 (*ppfield
)->name
= strdup_or_goto(field
->name
, out_err
);
2255 (*ppfield
)->index
= field
->index
;
2256 (*ppfield
)->ref
= field
->ref
;
2257 field
= field
->next
;
2258 ppfield
= &((*ppfield
)->next
);
2265 int perf_probe_event__copy(struct perf_probe_event
*dst
,
2266 struct perf_probe_event
*src
)
2270 dst
->event
= strdup_or_goto(src
->event
, out_err
);
2271 dst
->group
= strdup_or_goto(src
->group
, out_err
);
2272 dst
->target
= strdup_or_goto(src
->target
, out_err
);
2273 dst
->uprobes
= src
->uprobes
;
2275 if (perf_probe_point__copy(&dst
->point
, &src
->point
) < 0)
2278 dst
->args
= zalloc(sizeof(struct perf_probe_arg
) * src
->nargs
);
2281 dst
->nargs
= src
->nargs
;
2283 for (i
= 0; i
< src
->nargs
; i
++)
2284 if (perf_probe_arg__copy(&dst
->args
[i
], &src
->args
[i
]) < 0)
2289 clear_perf_probe_event(dst
);
2293 void clear_probe_trace_event(struct probe_trace_event
*tev
)
2295 struct probe_trace_arg_ref
*ref
, *next
;
2300 free(tev
->point
.symbol
);
2301 free(tev
->point
.realname
);
2302 free(tev
->point
.module
);
2303 for (i
= 0; i
< tev
->nargs
; i
++) {
2304 free(tev
->args
[i
].name
);
2305 free(tev
->args
[i
].value
);
2306 free(tev
->args
[i
].type
);
2307 ref
= tev
->args
[i
].ref
;
2315 memset(tev
, 0, sizeof(*tev
));
2318 struct kprobe_blacklist_node
{
2319 struct list_head list
;
2320 unsigned long start
;
2325 static void kprobe_blacklist__delete(struct list_head
*blacklist
)
2327 struct kprobe_blacklist_node
*node
;
2329 while (!list_empty(blacklist
)) {
2330 node
= list_first_entry(blacklist
,
2331 struct kprobe_blacklist_node
, list
);
2332 list_del(&node
->list
);
2338 static int kprobe_blacklist__load(struct list_head
*blacklist
)
2340 struct kprobe_blacklist_node
*node
;
2341 const char *__debugfs
= debugfs__mountpoint();
2342 char buf
[PATH_MAX
], *p
;
2346 if (__debugfs
== NULL
)
2349 ret
= e_snprintf(buf
, PATH_MAX
, "%s/kprobes/blacklist", __debugfs
);
2353 fp
= fopen(buf
, "r");
2358 while (fgets(buf
, PATH_MAX
, fp
)) {
2359 node
= zalloc(sizeof(*node
));
2364 INIT_LIST_HEAD(&node
->list
);
2365 list_add_tail(&node
->list
, blacklist
);
2366 if (sscanf(buf
, "0x%lx-0x%lx", &node
->start
, &node
->end
) != 2) {
2370 p
= strchr(buf
, '\t');
2373 if (p
[strlen(p
) - 1] == '\n')
2374 p
[strlen(p
) - 1] = '\0';
2376 p
= (char *)"unknown";
2377 node
->symbol
= strdup(p
);
2378 if (!node
->symbol
) {
2382 pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
2383 node
->start
, node
->end
, node
->symbol
);
2387 kprobe_blacklist__delete(blacklist
);
2393 static struct kprobe_blacklist_node
*
2394 kprobe_blacklist__find_by_address(struct list_head
*blacklist
,
2395 unsigned long address
)
2397 struct kprobe_blacklist_node
*node
;
2399 list_for_each_entry(node
, blacklist
, list
) {
2400 if (node
->start
<= address
&& address
< node
->end
)
2407 static LIST_HEAD(kprobe_blacklist
);
2409 static void kprobe_blacklist__init(void)
2411 if (!list_empty(&kprobe_blacklist
))
2414 if (kprobe_blacklist__load(&kprobe_blacklist
) < 0)
2415 pr_debug("No kprobe blacklist support, ignored\n");
2418 static void kprobe_blacklist__release(void)
2420 kprobe_blacklist__delete(&kprobe_blacklist
);
2423 static bool kprobe_blacklist__listed(unsigned long address
)
2425 return !!kprobe_blacklist__find_by_address(&kprobe_blacklist
, address
);
2428 static int perf_probe_event__sprintf(const char *group
, const char *event
,
2429 struct perf_probe_event
*pev
,
2431 struct strbuf
*result
)
2436 if (asprintf(&buf
, "%s:%s", group
, event
) < 0)
2438 ret
= strbuf_addf(result
, " %-20s (on ", buf
);
2443 /* Synthesize only event probe point */
2444 buf
= synthesize_perf_probe_point(&pev
->point
);
2447 ret
= strbuf_addstr(result
, buf
);
2451 ret
= strbuf_addf(result
, " in %s", module
);
2453 if (!ret
&& pev
->nargs
> 0) {
2454 ret
= strbuf_add(result
, " with", 5);
2455 for (i
= 0; !ret
&& i
< pev
->nargs
; i
++) {
2456 buf
= synthesize_perf_probe_arg(&pev
->args
[i
]);
2459 ret
= strbuf_addf(result
, " %s", buf
);
2464 ret
= strbuf_addch(result
, ')');
2470 int show_perf_probe_event(const char *group
, const char *event
,
2471 struct perf_probe_event
*pev
,
2472 const char *module
, bool use_stdout
)
2474 struct strbuf buf
= STRBUF_INIT
;
2477 ret
= perf_probe_event__sprintf(group
, event
, pev
, module
, &buf
);
2480 printf("%s\n", buf
.buf
);
2482 pr_info("%s\n", buf
.buf
);
2484 strbuf_release(&buf
);
2489 static bool filter_probe_trace_event(struct probe_trace_event
*tev
,
2490 struct strfilter
*filter
)
2494 /* At first, check the event name itself */
2495 if (strfilter__compare(filter
, tev
->event
))
2498 /* Next, check the combination of name and group */
2499 if (e_snprintf(tmp
, 128, "%s:%s", tev
->group
, tev
->event
) < 0)
2501 return strfilter__compare(filter
, tmp
);
2504 static int __show_perf_probe_events(int fd
, bool is_kprobe
,
2505 struct strfilter
*filter
)
2508 struct probe_trace_event tev
;
2509 struct perf_probe_event pev
;
2510 struct strlist
*rawlist
;
2511 struct str_node
*ent
;
2513 memset(&tev
, 0, sizeof(tev
));
2514 memset(&pev
, 0, sizeof(pev
));
2516 rawlist
= probe_file__get_rawlist(fd
);
2520 strlist__for_each_entry(ent
, rawlist
) {
2521 ret
= parse_probe_trace_command(ent
->s
, &tev
);
2523 if (!filter_probe_trace_event(&tev
, filter
))
2525 ret
= convert_to_perf_probe_event(&tev
, &pev
,
2529 ret
= show_perf_probe_event(pev
.group
, pev
.event
,
2530 &pev
, tev
.point
.module
,
2534 clear_perf_probe_event(&pev
);
2535 clear_probe_trace_event(&tev
);
2539 strlist__delete(rawlist
);
2540 /* Cleanup cached debuginfo if needed */
2541 debuginfo_cache__exit();
2546 /* List up current perf-probe events */
2547 int show_perf_probe_events(struct strfilter
*filter
)
2549 int kp_fd
, up_fd
, ret
;
2553 if (probe_conf
.cache
)
2554 return probe_cache__show_all_caches(filter
);
2556 ret
= init_probe_symbol_maps(false);
2560 ret
= probe_file__open_both(&kp_fd
, &up_fd
, 0);
2565 ret
= __show_perf_probe_events(kp_fd
, true, filter
);
2566 if (up_fd
>= 0 && ret
>= 0)
2567 ret
= __show_perf_probe_events(up_fd
, false, filter
);
2572 exit_probe_symbol_maps();
2577 static int get_new_event_name(char *buf
, size_t len
, const char *base
,
2578 struct strlist
*namelist
, bool allow_suffix
)
2585 nbase
= strdup(base
);
2589 /* Cut off the dot suffixes (e.g. .const, .isra)*/
2590 p
= strchr(nbase
, '.');
2591 if (p
&& p
!= nbase
)
2594 /* Try no suffix number */
2595 ret
= e_snprintf(buf
, len
, "%s", nbase
);
2597 pr_debug("snprintf() failed: %d\n", ret
);
2600 if (!strlist__has_entry(namelist
, buf
))
2603 if (!allow_suffix
) {
2604 pr_warning("Error: event \"%s\" already exists.\n"
2605 " Hint: Remove existing event by 'perf probe -d'\n"
2606 " or force duplicates by 'perf probe -f'\n"
2607 " or set 'force=yes' in BPF source.\n",
2613 /* Try to add suffix */
2614 for (i
= 1; i
< MAX_EVENT_INDEX
; i
++) {
2615 ret
= e_snprintf(buf
, len
, "%s_%d", nbase
, i
);
2617 pr_debug("snprintf() failed: %d\n", ret
);
2620 if (!strlist__has_entry(namelist
, buf
))
2623 if (i
== MAX_EVENT_INDEX
) {
2624 pr_warning("Too many events are on the same function.\n");
2631 /* Final validation */
2632 if (ret
>= 0 && !is_c_func_name(buf
)) {
2633 pr_warning("Internal error: \"%s\" is an invalid event name.\n",
2641 /* Warn if the current kernel's uprobe implementation is old */
2642 static void warn_uprobe_event_compat(struct probe_trace_event
*tev
)
2645 char *buf
= synthesize_probe_trace_command(tev
);
2647 /* Old uprobe event doesn't support memory dereference */
2648 if (!tev
->uprobes
|| tev
->nargs
== 0 || !buf
)
2651 for (i
= 0; i
< tev
->nargs
; i
++)
2652 if (strglobmatch(tev
->args
[i
].value
, "[$@+-]*")) {
2653 pr_warning("Please upgrade your kernel to at least "
2654 "3.14 to have access to feature %s\n",
2655 tev
->args
[i
].value
);
2662 /* Set new name from original perf_probe_event and namelist */
2663 static int probe_trace_event__set_name(struct probe_trace_event
*tev
,
2664 struct perf_probe_event
*pev
,
2665 struct strlist
*namelist
,
2668 const char *event
, *group
;
2672 /* If probe_event or trace_event already have the name, reuse it */
2673 if (pev
->event
&& !pev
->sdt
)
2675 else if (tev
->event
)
2678 /* Or generate new one from probe point */
2679 if (pev
->point
.function
&&
2680 (strncmp(pev
->point
.function
, "0x", 2) != 0) &&
2681 !strisglob(pev
->point
.function
))
2682 event
= pev
->point
.function
;
2684 event
= tev
->point
.realname
;
2686 if (pev
->group
&& !pev
->sdt
)
2688 else if (tev
->group
)
2691 group
= PERFPROBE_GROUP
;
2693 /* Get an unused new event name */
2694 ret
= get_new_event_name(buf
, 64, event
,
2695 namelist
, allow_suffix
);
2701 tev
->event
= strdup(event
);
2702 tev
->group
= strdup(group
);
2703 if (tev
->event
== NULL
|| tev
->group
== NULL
)
2706 /* Add added event name to namelist */
2707 strlist__add(namelist
, event
);
2711 static int __open_probe_file_and_namelist(bool uprobe
,
2712 struct strlist
**namelist
)
2716 fd
= probe_file__open(PF_FL_RW
| (uprobe
? PF_FL_UPROBE
: 0));
2720 /* Get current event names */
2721 *namelist
= probe_file__get_namelist(fd
);
2723 pr_debug("Failed to get current event list.\n");
2730 static int __add_probe_trace_events(struct perf_probe_event
*pev
,
2731 struct probe_trace_event
*tevs
,
2732 int ntevs
, bool allow_suffix
)
2734 int i
, fd
[2] = {-1, -1}, up
, ret
;
2735 struct probe_trace_event
*tev
= NULL
;
2736 struct probe_cache
*cache
= NULL
;
2737 struct strlist
*namelist
[2] = {NULL
, NULL
};
2738 struct nscookie nsc
;
2740 up
= pev
->uprobes
? 1 : 0;
2741 fd
[up
] = __open_probe_file_and_namelist(up
, &namelist
[up
]);
2746 for (i
= 0; i
< ntevs
; i
++) {
2748 up
= tev
->uprobes
? 1 : 0;
2749 if (fd
[up
] == -1) { /* Open the kprobe/uprobe_events */
2750 fd
[up
] = __open_probe_file_and_namelist(up
,
2755 /* Skip if the symbol is out of .text or blacklisted */
2756 if (!tev
->point
.symbol
&& !pev
->uprobes
)
2759 /* Set new name for tev (and update namelist) */
2760 ret
= probe_trace_event__set_name(tev
, pev
, namelist
[up
],
2765 nsinfo__mountns_enter(pev
->nsi
, &nsc
);
2766 ret
= probe_file__add_event(fd
[up
], tev
);
2767 nsinfo__mountns_exit(&nsc
);
2772 * Probes after the first probe which comes from same
2773 * user input are always allowed to add suffix, because
2774 * there might be several addresses corresponding to
2777 allow_suffix
= true;
2779 if (ret
== -EINVAL
&& pev
->uprobes
)
2780 warn_uprobe_event_compat(tev
);
2781 if (ret
== 0 && probe_conf
.cache
) {
2782 cache
= probe_cache__new(pev
->target
, pev
->nsi
);
2784 probe_cache__add_entry(cache
, pev
, tevs
, ntevs
) < 0 ||
2785 probe_cache__commit(cache
) < 0)
2786 pr_warning("Failed to add event to probe cache\n");
2787 probe_cache__delete(cache
);
2791 for (up
= 0; up
< 2; up
++) {
2792 strlist__delete(namelist
[up
]);
2799 static int find_probe_functions(struct map
*map
, char *name
,
2800 struct symbol
**syms
)
2804 struct rb_node
*tmp
;
2805 const char *norm
, *ver
;
2808 if (map__load(map
) < 0)
2811 map__for_each_symbol(map
, sym
, tmp
) {
2812 norm
= arch__normalize_symbol_name(sym
->name
);
2816 /* We don't care about default symbol or not */
2817 ver
= strchr(norm
, '@');
2819 buf
= strndup(norm
, ver
- norm
);
2824 if (strglobmatch(norm
, name
)) {
2826 if (syms
&& found
< probe_conf
.max_probes
)
2827 syms
[found
- 1] = sym
;
2836 void __weak
arch__fix_tev_from_maps(struct perf_probe_event
*pev __maybe_unused
,
2837 struct probe_trace_event
*tev __maybe_unused
,
2838 struct map
*map __maybe_unused
,
2839 struct symbol
*sym __maybe_unused
) { }
2842 * Find probe function addresses from map.
2843 * Return an error or the number of found probe_trace_event
2845 static int find_probe_trace_events_from_map(struct perf_probe_event
*pev
,
2846 struct probe_trace_event
**tevs
)
2848 struct map
*map
= NULL
;
2849 struct ref_reloc_sym
*reloc_sym
= NULL
;
2851 struct symbol
**syms
= NULL
;
2852 struct probe_trace_event
*tev
;
2853 struct perf_probe_point
*pp
= &pev
->point
;
2854 struct probe_trace_point
*tp
;
2855 int num_matched_functions
;
2856 int ret
, i
, j
, skipped
= 0;
2859 map
= get_target_map(pev
->target
, pev
->nsi
, pev
->uprobes
);
2865 syms
= malloc(sizeof(struct symbol
*) * probe_conf
.max_probes
);
2872 * Load matched symbols: Since the different local symbols may have
2873 * same name but different addresses, this lists all the symbols.
2875 num_matched_functions
= find_probe_functions(map
, pp
->function
, syms
);
2876 if (num_matched_functions
<= 0) {
2877 pr_err("Failed to find symbol %s in %s\n", pp
->function
,
2878 pev
->target
? : "kernel");
2881 } else if (num_matched_functions
> probe_conf
.max_probes
) {
2882 pr_err("Too many functions matched in %s\n",
2883 pev
->target
? : "kernel");
2888 /* Note that the symbols in the kmodule are not relocated */
2889 if (!pev
->uprobes
&& !pev
->target
&&
2890 (!pp
->retprobe
|| kretprobe_offset_is_supported())) {
2891 reloc_sym
= kernel_get_ref_reloc_sym();
2893 pr_warning("Relocated base symbol is not found!\n");
2899 /* Setup result trace-probe-events */
2900 *tevs
= zalloc(sizeof(*tev
) * num_matched_functions
);
2908 for (j
= 0; j
< num_matched_functions
; j
++) {
2911 tev
= (*tevs
) + ret
;
2913 if (ret
== num_matched_functions
) {
2914 pr_warning("Too many symbols are listed. Skip it.\n");
2919 if (pp
->offset
> sym
->end
- sym
->start
) {
2920 pr_warning("Offset %ld is bigger than the size of %s\n",
2921 pp
->offset
, sym
->name
);
2925 /* Add one probe point */
2926 tp
->address
= map
->unmap_ip(map
, sym
->start
) + pp
->offset
;
2928 /* Check the kprobe (not in module) is within .text */
2929 if (!pev
->uprobes
&& !pev
->target
&&
2930 kprobe_warn_out_range(sym
->name
, tp
->address
)) {
2931 tp
->symbol
= NULL
; /* Skip it */
2933 } else if (reloc_sym
) {
2934 tp
->symbol
= strdup_or_goto(reloc_sym
->name
, nomem_out
);
2935 tp
->offset
= tp
->address
- reloc_sym
->addr
;
2937 tp
->symbol
= strdup_or_goto(sym
->name
, nomem_out
);
2938 tp
->offset
= pp
->offset
;
2940 tp
->realname
= strdup_or_goto(sym
->name
, nomem_out
);
2942 tp
->retprobe
= pp
->retprobe
;
2945 tev
->point
.module
= strdup_or_goto(pev
->target
,
2948 mod_name
= find_module_name(pev
->target
);
2950 strdup(mod_name
? mod_name
: pev
->target
);
2952 if (!tev
->point
.module
)
2956 tev
->uprobes
= pev
->uprobes
;
2957 tev
->nargs
= pev
->nargs
;
2959 tev
->args
= zalloc(sizeof(struct probe_trace_arg
) *
2961 if (tev
->args
== NULL
)
2964 for (i
= 0; i
< tev
->nargs
; i
++) {
2965 if (pev
->args
[i
].name
)
2967 strdup_or_goto(pev
->args
[i
].name
,
2970 tev
->args
[i
].value
= strdup_or_goto(pev
->args
[i
].var
,
2972 if (pev
->args
[i
].type
)
2974 strdup_or_goto(pev
->args
[i
].type
,
2977 arch__fix_tev_from_maps(pev
, tev
, map
, sym
);
2979 if (ret
== skipped
) {
2992 clear_probe_trace_events(*tevs
, num_matched_functions
);
2997 static int try_to_find_absolute_address(struct perf_probe_event
*pev
,
2998 struct probe_trace_event
**tevs
)
3000 struct perf_probe_point
*pp
= &pev
->point
;
3001 struct probe_trace_event
*tev
;
3002 struct probe_trace_point
*tp
;
3005 if (!(pev
->point
.function
&& !strncmp(pev
->point
.function
, "0x", 2)))
3007 if (perf_probe_event_need_dwarf(pev
))
3011 * This is 'perf probe /lib/libc.so 0xabcd'. Try to probe at
3014 * Only one tev can be generated by this.
3016 *tevs
= zalloc(sizeof(*tev
));
3024 * Don't use tp->offset, use address directly, because
3025 * in synthesize_probe_trace_command() address cannot be
3028 tp
->address
= pev
->point
.abs_address
;
3029 tp
->retprobe
= pp
->retprobe
;
3030 tev
->uprobes
= pev
->uprobes
;
3034 * Give it a '0x' leading symbol name.
3035 * In __add_probe_trace_events, a NULL symbol is interpreted as
3038 if (asprintf(&tp
->symbol
, "0x%lx", tp
->address
) < 0)
3041 /* For kprobe, check range */
3042 if ((!tev
->uprobes
) &&
3043 (kprobe_warn_out_range(tev
->point
.symbol
,
3044 tev
->point
.address
))) {
3049 if (asprintf(&tp
->realname
, "abs_%lx", tp
->address
) < 0)
3053 tp
->module
= strdup(pev
->target
);
3059 tev
->group
= strdup(pev
->group
);
3065 tev
->event
= strdup(pev
->event
);
3070 tev
->nargs
= pev
->nargs
;
3071 tev
->args
= zalloc(sizeof(struct probe_trace_arg
) * tev
->nargs
);
3075 for (i
= 0; i
< tev
->nargs
; i
++)
3076 copy_to_probe_trace_arg(&tev
->args
[i
], &pev
->args
[i
]);
3081 clear_probe_trace_events(*tevs
, 1);
3086 /* Concatinate two arrays */
3087 static void *memcat(void *a
, size_t sz_a
, void *b
, size_t sz_b
)
3091 ret
= malloc(sz_a
+ sz_b
);
3093 memcpy(ret
, a
, sz_a
);
3094 memcpy(ret
+ sz_a
, b
, sz_b
);
3100 concat_probe_trace_events(struct probe_trace_event
**tevs
, int *ntevs
,
3101 struct probe_trace_event
**tevs2
, int ntevs2
)
3103 struct probe_trace_event
*new_tevs
;
3113 if (*ntevs
+ ntevs2
> probe_conf
.max_probes
)
3116 /* Concatinate the array of probe_trace_event */
3117 new_tevs
= memcat(*tevs
, (*ntevs
) * sizeof(**tevs
),
3118 *tevs2
, ntevs2
* sizeof(**tevs2
));
3128 clear_probe_trace_events(*tevs2
, ntevs2
);
3135 * Try to find probe_trace_event from given probe caches. Return the number
3136 * of cached events found, if an error occurs return the error.
3138 static int find_cached_events(struct perf_probe_event
*pev
,
3139 struct probe_trace_event
**tevs
,
3142 struct probe_cache
*cache
;
3143 struct probe_cache_entry
*entry
;
3144 struct probe_trace_event
*tmp_tevs
= NULL
;
3148 cache
= probe_cache__new(target
, pev
->nsi
);
3149 /* Return 0 ("not found") if the target has no probe cache. */
3153 for_each_probe_cache_entry(entry
, cache
) {
3154 /* Skip the cache entry which has no name */
3155 if (!entry
->pev
.event
|| !entry
->pev
.group
)
3157 if ((!pev
->group
|| strglobmatch(entry
->pev
.group
, pev
->group
)) &&
3158 strglobmatch(entry
->pev
.event
, pev
->event
)) {
3159 ret
= probe_cache_entry__get_event(entry
, &tmp_tevs
);
3161 ret
= concat_probe_trace_events(tevs
, &ntevs
,
3167 probe_cache__delete(cache
);
3169 clear_probe_trace_events(*tevs
, ntevs
);
3173 if (ntevs
> 0 && target
&& target
[0] == '/')
3174 pev
->uprobes
= true;
3180 /* Try to find probe_trace_event from all probe caches */
3181 static int find_cached_events_all(struct perf_probe_event
*pev
,
3182 struct probe_trace_event
**tevs
)
3184 struct probe_trace_event
*tmp_tevs
= NULL
;
3185 struct strlist
*bidlist
;
3186 struct str_node
*nd
;
3191 /* Get the buildid list of all valid caches */
3192 bidlist
= build_id_cache__list_all(true);
3195 pr_debug("Failed to get buildids: %d\n", ret
);
3200 strlist__for_each_entry(nd
, bidlist
) {
3201 pathname
= build_id_cache__origname(nd
->s
);
3202 ret
= find_cached_events(pev
, &tmp_tevs
, pathname
);
3203 /* In the case of cnt == 0, we just skip it */
3205 ret
= concat_probe_trace_events(tevs
, &ntevs
,
3211 strlist__delete(bidlist
);
3214 clear_probe_trace_events(*tevs
, ntevs
);
3222 static int find_probe_trace_events_from_cache(struct perf_probe_event
*pev
,
3223 struct probe_trace_event
**tevs
)
3225 struct probe_cache
*cache
;
3226 struct probe_cache_entry
*entry
;
3227 struct probe_trace_event
*tev
;
3228 struct str_node
*node
;
3232 /* For SDT/cached events, we use special search functions */
3234 return find_cached_events_all(pev
, tevs
);
3236 return find_cached_events(pev
, tevs
, pev
->target
);
3238 cache
= probe_cache__new(pev
->target
, pev
->nsi
);
3242 entry
= probe_cache__find(cache
, pev
);
3244 /* SDT must be in the cache */
3245 ret
= pev
->sdt
? -ENOENT
: 0;
3249 ret
= strlist__nr_entries(entry
->tevlist
);
3250 if (ret
> probe_conf
.max_probes
) {
3251 pr_debug("Too many entries matched in the cache of %s\n",
3252 pev
->target
? : "kernel");
3257 *tevs
= zalloc(ret
* sizeof(*tev
));
3264 strlist__for_each_entry(node
, entry
->tevlist
) {
3265 tev
= &(*tevs
)[i
++];
3266 ret
= parse_probe_trace_command(node
->s
, tev
);
3269 /* Set the uprobes attribute as same as original */
3270 tev
->uprobes
= pev
->uprobes
;
3275 probe_cache__delete(cache
);
3279 static int convert_to_probe_trace_events(struct perf_probe_event
*pev
,
3280 struct probe_trace_event
**tevs
)
3284 if (!pev
->group
&& !pev
->sdt
) {
3285 /* Set group name if not given */
3286 if (!pev
->uprobes
) {
3287 pev
->group
= strdup(PERFPROBE_GROUP
);
3288 ret
= pev
->group
? 0 : -ENOMEM
;
3290 ret
= convert_exec_to_group(pev
->target
, &pev
->group
);
3292 pr_warning("Failed to make a group name.\n");
3297 ret
= try_to_find_absolute_address(pev
, tevs
);
3301 /* At first, we need to lookup cache entry */
3302 ret
= find_probe_trace_events_from_cache(pev
, tevs
);
3303 if (ret
> 0 || pev
->sdt
) /* SDT can be found only in the cache */
3304 return ret
== 0 ? -ENOENT
: ret
; /* Found in probe cache */
3306 /* Convert perf_probe_event with debuginfo */
3307 ret
= try_to_find_probe_trace_events(pev
, tevs
);
3309 return ret
; /* Found in debuginfo or got an error */
3311 return find_probe_trace_events_from_map(pev
, tevs
);
3314 int convert_perf_probe_events(struct perf_probe_event
*pevs
, int npevs
)
3318 /* Loop 1: convert all events */
3319 for (i
= 0; i
< npevs
; i
++) {
3320 /* Init kprobe blacklist if needed */
3321 if (!pevs
[i
].uprobes
)
3322 kprobe_blacklist__init();
3323 /* Convert with or without debuginfo */
3324 ret
= convert_to_probe_trace_events(&pevs
[i
], &pevs
[i
].tevs
);
3327 pevs
[i
].ntevs
= ret
;
3329 /* This just release blacklist only if allocated */
3330 kprobe_blacklist__release();
3335 static int show_probe_trace_event(struct probe_trace_event
*tev
)
3337 char *buf
= synthesize_probe_trace_command(tev
);
3340 pr_debug("Failed to synthesize probe trace event.\n");
3344 /* Showing definition always go stdout */
3345 printf("%s\n", buf
);
3351 int show_probe_trace_events(struct perf_probe_event
*pevs
, int npevs
)
3353 struct strlist
*namelist
= strlist__new(NULL
, NULL
);
3354 struct probe_trace_event
*tev
;
3355 struct perf_probe_event
*pev
;
3361 for (j
= 0; j
< npevs
&& !ret
; j
++) {
3363 for (i
= 0; i
< pev
->ntevs
&& !ret
; i
++) {
3364 tev
= &pev
->tevs
[i
];
3365 /* Skip if the symbol is out of .text or blacklisted */
3366 if (!tev
->point
.symbol
&& !pev
->uprobes
)
3369 /* Set new name for tev (and update namelist) */
3370 ret
= probe_trace_event__set_name(tev
, pev
,
3373 ret
= show_probe_trace_event(tev
);
3376 strlist__delete(namelist
);
3381 int apply_perf_probe_events(struct perf_probe_event
*pevs
, int npevs
)
3385 /* Loop 2: add all events */
3386 for (i
= 0; i
< npevs
; i
++) {
3387 ret
= __add_probe_trace_events(&pevs
[i
], pevs
[i
].tevs
,
3389 probe_conf
.force_add
);
3396 void cleanup_perf_probe_events(struct perf_probe_event
*pevs
, int npevs
)
3399 struct perf_probe_event
*pev
;
3401 /* Loop 3: cleanup and free trace events */
3402 for (i
= 0; i
< npevs
; i
++) {
3404 for (j
= 0; j
< pevs
[i
].ntevs
; j
++)
3405 clear_probe_trace_event(&pevs
[i
].tevs
[j
]);
3406 zfree(&pevs
[i
].tevs
);
3408 nsinfo__zput(pev
->nsi
);
3409 clear_perf_probe_event(&pevs
[i
]);
3413 int add_perf_probe_events(struct perf_probe_event
*pevs
, int npevs
)
3417 ret
= init_probe_symbol_maps(pevs
->uprobes
);
3421 ret
= convert_perf_probe_events(pevs
, npevs
);
3423 ret
= apply_perf_probe_events(pevs
, npevs
);
3425 cleanup_perf_probe_events(pevs
, npevs
);
3427 exit_probe_symbol_maps();
3431 int del_perf_probe_events(struct strfilter
*filter
)
3433 int ret
, ret2
, ufd
= -1, kfd
= -1;
3434 char *str
= strfilter__string(filter
);
3439 /* Get current event names */
3440 ret
= probe_file__open_both(&kfd
, &ufd
, PF_FL_RW
);
3444 ret
= probe_file__del_events(kfd
, filter
);
3445 if (ret
< 0 && ret
!= -ENOENT
)
3448 ret2
= probe_file__del_events(ufd
, filter
);
3449 if (ret2
< 0 && ret2
!= -ENOENT
) {
3466 int show_available_funcs(const char *target
, struct nsinfo
*nsi
,
3467 struct strfilter
*_filter
, bool user
)
3473 ret
= init_probe_symbol_maps(user
);
3477 /* Get a symbol map */
3478 map
= get_target_map(target
, nsi
, user
);
3480 pr_err("Failed to get a map for %s\n", (target
) ? : "kernel");
3484 ret
= map__load(map
);
3487 char *str
= strfilter__string(_filter
);
3488 pr_err("Failed to find symbols matched to \"%s\"\n",
3492 pr_err("Failed to load symbols in %s\n",
3493 (target
) ? : "kernel");
3496 if (!dso__sorted_by_name(map
->dso
, map
->type
))
3497 dso__sort_by_name(map
->dso
, map
->type
);
3499 /* Show all (filtered) symbols */
3502 for (nd
= rb_first(&map
->dso
->symbol_names
[map
->type
]); nd
; nd
= rb_next(nd
)) {
3503 struct symbol_name_rb_node
*pos
= rb_entry(nd
, struct symbol_name_rb_node
, rb_node
);
3505 if (strfilter__compare(_filter
, pos
->sym
.name
))
3506 printf("%s\n", pos
->sym
.name
);
3511 exit_probe_symbol_maps();
3516 int copy_to_probe_trace_arg(struct probe_trace_arg
*tvar
,
3517 struct perf_probe_arg
*pvar
)
3519 tvar
->value
= strdup(pvar
->var
);
3520 if (tvar
->value
== NULL
)
3523 tvar
->type
= strdup(pvar
->type
);
3524 if (tvar
->type
== NULL
)
3528 tvar
->name
= strdup(pvar
->name
);
3529 if (tvar
->name
== NULL
)