]>
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.
22 #include <sys/utsname.h>
23 #include <sys/types.h>
43 #include <api/fs/fs.h>
44 #include "trace-event.h" /* For __maybe_unused */
45 #include "probe-event.h"
46 #include "probe-finder.h"
47 #include "probe-file.h"
50 #define PERFPROBE_GROUP "probe"
52 bool probe_event_dry_run
; /* Dry run flag */
53 struct probe_conf probe_conf
;
55 #define semantic_error(msg ...) pr_err("Semantic error :" msg)
57 int e_snprintf(char *str
, size_t size
, const char *format
, ...)
62 ret
= vsnprintf(str
, size
, format
, ap
);
69 static struct machine
*host_machine
;
71 /* Initialize symbol maps and path of vmlinux/modules */
72 int init_probe_symbol_maps(bool user_only
)
76 symbol_conf
.sort_by_name
= true;
77 symbol_conf
.allow_aliases
= true;
78 ret
= symbol__init(NULL
);
80 pr_debug("Failed to init symbol map.\n");
84 if (host_machine
|| user_only
) /* already initialized */
87 if (symbol_conf
.vmlinux_name
)
88 pr_debug("Use vmlinux: %s\n", symbol_conf
.vmlinux_name
);
90 host_machine
= machine__new_host();
92 pr_debug("machine__new_host() failed.\n");
98 pr_warning("Failed to init vmlinux path.\n");
102 void exit_probe_symbol_maps(void)
104 machine__delete(host_machine
);
109 static struct symbol
*__find_kernel_function_by_name(const char *name
,
112 return machine__find_kernel_function_by_name(host_machine
, name
, mapp
);
115 static struct symbol
*__find_kernel_function(u64 addr
, struct map
**mapp
)
117 return machine__find_kernel_function(host_machine
, addr
, mapp
);
120 static struct ref_reloc_sym
*kernel_get_ref_reloc_sym(void)
122 /* kmap->ref_reloc_sym should be set if host_machine is initialized */
124 struct map
*map
= machine__kernel_map(host_machine
);
126 if (map__load(map
) < 0)
129 kmap
= map__kmap(map
);
132 return kmap
->ref_reloc_sym
;
135 static int kernel_get_symbol_address_by_name(const char *name
, u64
*addr
,
136 bool reloc
, bool reladdr
)
138 struct ref_reloc_sym
*reloc_sym
;
142 /* ref_reloc_sym is just a label. Need a special fix*/
143 reloc_sym
= kernel_get_ref_reloc_sym();
144 if (reloc_sym
&& strcmp(name
, reloc_sym
->name
) == 0)
145 *addr
= (reloc
) ? reloc_sym
->addr
: reloc_sym
->unrelocated_addr
;
147 sym
= __find_kernel_function_by_name(name
, &map
);
150 *addr
= map
->unmap_ip(map
, sym
->start
) -
151 ((reloc
) ? 0 : map
->reloc
) -
152 ((reladdr
) ? map
->start
: 0);
157 static struct map
*kernel_get_module_map(const char *module
)
159 struct map_groups
*grp
= &host_machine
->kmaps
;
160 struct maps
*maps
= &grp
->maps
[MAP__FUNCTION
];
163 /* A file path -- this is an offline module */
164 if (module
&& strchr(module
, '/'))
165 return dso__new_map(module
);
170 for (pos
= maps__first(maps
); pos
; pos
= map__next(pos
)) {
171 /* short_name is "[module]" */
172 if (strncmp(pos
->dso
->short_name
+ 1, module
,
173 pos
->dso
->short_name_len
- 2) == 0 &&
174 module
[pos
->dso
->short_name_len
- 2] == '\0') {
182 struct map
*get_target_map(const char *target
, bool user
)
184 /* Init maps of given executable or kernel */
186 return dso__new_map(target
);
188 return kernel_get_module_map(target
);
191 static int convert_exec_to_group(const char *exec
, char **result
)
193 char *ptr1
, *ptr2
, *exec_copy
;
197 exec_copy
= strdup(exec
);
201 ptr1
= basename(exec_copy
);
207 for (ptr2
= ptr1
; *ptr2
!= '\0'; ptr2
++) {
208 if (!isalnum(*ptr2
) && *ptr2
!= '_') {
214 ret
= e_snprintf(buf
, 64, "%s_%s", PERFPROBE_GROUP
, ptr1
);
218 *result
= strdup(buf
);
219 ret
= *result
? 0 : -ENOMEM
;
226 static void clear_perf_probe_point(struct perf_probe_point
*pp
)
233 static void clear_probe_trace_events(struct probe_trace_event
*tevs
, int ntevs
)
237 for (i
= 0; i
< ntevs
; i
++)
238 clear_probe_trace_event(tevs
+ i
);
241 static bool kprobe_blacklist__listed(unsigned long address
);
242 static bool kprobe_warn_out_range(const char *symbol
, unsigned long address
)
247 /* Get the address of _etext for checking non-probable text symbol */
248 ret
= kernel_get_symbol_address_by_name("_etext", &etext_addr
,
251 if (ret
== 0 && etext_addr
< address
)
252 pr_warning("%s is out of .text, skip it.\n", symbol
);
253 else if (kprobe_blacklist__listed(address
))
254 pr_warning("%s is blacklisted function, skip it.\n", symbol
);
262 * @module can be module name of module file path. In case of path,
263 * inspect elf and find out what is actual module name.
264 * Caller has to free mod_name after using it.
266 static char *find_module_name(const char *module
)
274 char *mod_name
= NULL
;
277 fd
= open(module
, O_RDONLY
);
281 elf
= elf_begin(fd
, PERF_ELF_C_READ_MMAP
, NULL
);
285 if (gelf_getehdr(elf
, &ehdr
) == NULL
)
288 sec
= elf_section_by_name(elf
, &ehdr
, &shdr
,
289 ".gnu.linkonce.this_module", NULL
);
293 data
= elf_getdata(sec
, NULL
);
294 if (!data
|| !data
->d_buf
)
299 * '.gnu.linkonce.this_module' section of kernel module elf directly
300 * maps to 'struct module' from linux/module.h. This section contains
301 * actual module name which will be used by kernel after loading it.
302 * But, we cannot use 'struct module' here since linux/module.h is not
303 * exposed to user-space. Offset of 'name' has remained same from long
304 * time, so hardcoding it here.
306 if (ehdr
.e_ident
[EI_CLASS
] == ELFCLASS32
)
308 else /* expect ELFCLASS64 by default */
311 mod_name
= strdup((char *)data
->d_buf
+ name_offset
);
320 #ifdef HAVE_DWARF_SUPPORT
322 static int kernel_get_module_dso(const char *module
, struct dso
**pdso
)
326 const char *vmlinux_name
;
330 char module_name
[128];
332 snprintf(module_name
, sizeof(module_name
), "[%s]", module
);
333 map
= map_groups__find_by_name(&host_machine
->kmaps
, MAP__FUNCTION
, module_name
);
338 pr_debug("Failed to find module %s.\n", module
);
342 map
= machine__kernel_map(host_machine
);
345 vmlinux_name
= symbol_conf
.vmlinux_name
;
348 ret
= dso__load_vmlinux(dso
, map
, vmlinux_name
, false);
350 ret
= dso__load_vmlinux_path(dso
, map
);
357 * Some binaries like glibc have special symbols which are on the symbol
358 * table, but not in the debuginfo. If we can find the address of the
359 * symbol from map, we can translate the address back to the probe point.
361 static int find_alternative_probe_point(struct debuginfo
*dinfo
,
362 struct perf_probe_point
*pp
,
363 struct perf_probe_point
*result
,
364 const char *target
, bool uprobes
)
366 struct map
*map
= NULL
;
371 /* This can work only for function-name based one */
372 if (!pp
->function
|| pp
->file
)
375 map
= get_target_map(target
, uprobes
);
379 /* Find the address of given function */
380 map__for_each_symbol_by_name(map
, pp
->function
, sym
) {
382 address
= sym
->start
;
384 address
= map
->unmap_ip(map
, sym
->start
) - map
->reloc
;
391 pr_debug("Symbol %s address found : %" PRIx64
"\n",
392 pp
->function
, address
);
394 ret
= debuginfo__find_probe_point(dinfo
, (unsigned long)address
,
397 ret
= (!ret
) ? -ENOENT
: ret
;
399 result
->offset
+= pp
->offset
;
400 result
->line
+= pp
->line
;
401 result
->retprobe
= pp
->retprobe
;
411 static int get_alternative_probe_event(struct debuginfo
*dinfo
,
412 struct perf_probe_event
*pev
,
413 struct perf_probe_point
*tmp
)
417 memcpy(tmp
, &pev
->point
, sizeof(*tmp
));
418 memset(&pev
->point
, 0, sizeof(pev
->point
));
419 ret
= find_alternative_probe_point(dinfo
, tmp
, &pev
->point
,
420 pev
->target
, pev
->uprobes
);
422 memcpy(&pev
->point
, tmp
, sizeof(*tmp
));
427 static int get_alternative_line_range(struct debuginfo
*dinfo
,
428 struct line_range
*lr
,
429 const char *target
, bool user
)
431 struct perf_probe_point pp
= { .function
= lr
->function
,
434 struct perf_probe_point result
;
437 memset(&result
, 0, sizeof(result
));
439 if (lr
->end
!= INT_MAX
)
440 len
= lr
->end
- lr
->start
;
441 ret
= find_alternative_probe_point(dinfo
, &pp
, &result
,
444 lr
->function
= result
.function
;
445 lr
->file
= result
.file
;
446 lr
->start
= result
.line
;
447 if (lr
->end
!= INT_MAX
)
448 lr
->end
= lr
->start
+ len
;
449 clear_perf_probe_point(&pp
);
454 /* Open new debuginfo of given module */
455 static struct debuginfo
*open_debuginfo(const char *module
, bool silent
)
457 const char *path
= module
;
458 char reason
[STRERR_BUFSIZE
];
459 struct debuginfo
*ret
= NULL
;
460 struct dso
*dso
= NULL
;
463 if (!module
|| !strchr(module
, '/')) {
464 err
= kernel_get_module_dso(module
, &dso
);
466 if (!dso
|| dso
->load_errno
== 0) {
467 if (!str_error_r(-err
, reason
, STRERR_BUFSIZE
))
468 strcpy(reason
, "(unknown)");
470 dso__strerror_load(dso
, reason
, STRERR_BUFSIZE
);
472 pr_err("Failed to find the path for %s: %s\n",
473 module
?: "kernel", reason
);
476 path
= dso
->long_name
;
478 ret
= debuginfo__new(path
);
479 if (!ret
&& !silent
) {
480 pr_warning("The %s file has no debug information.\n", path
);
481 if (!module
|| !strtailcmp(path
, ".ko"))
482 pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, ");
484 pr_warning("Rebuild with -g, ");
485 pr_warning("or install an appropriate debuginfo package.\n");
490 /* For caching the last debuginfo */
491 static struct debuginfo
*debuginfo_cache
;
492 static char *debuginfo_cache_path
;
494 static struct debuginfo
*debuginfo_cache__open(const char *module
, bool silent
)
496 const char *path
= module
;
498 /* If the module is NULL, it should be the kernel. */
502 if (debuginfo_cache_path
&& !strcmp(debuginfo_cache_path
, path
))
505 /* Copy module path */
506 free(debuginfo_cache_path
);
507 debuginfo_cache_path
= strdup(path
);
508 if (!debuginfo_cache_path
) {
509 debuginfo__delete(debuginfo_cache
);
510 debuginfo_cache
= NULL
;
514 debuginfo_cache
= open_debuginfo(module
, silent
);
515 if (!debuginfo_cache
)
516 zfree(&debuginfo_cache_path
);
518 return debuginfo_cache
;
521 static void debuginfo_cache__exit(void)
523 debuginfo__delete(debuginfo_cache
);
524 debuginfo_cache
= NULL
;
525 zfree(&debuginfo_cache_path
);
529 static int get_text_start_address(const char *exec
, unsigned long *address
)
534 int fd
, ret
= -ENOENT
;
536 fd
= open(exec
, O_RDONLY
);
540 elf
= elf_begin(fd
, PERF_ELF_C_READ_MMAP
, NULL
);
546 if (gelf_getehdr(elf
, &ehdr
) == NULL
)
549 if (!elf_section_by_name(elf
, &ehdr
, &shdr
, ".text", NULL
))
552 *address
= shdr
.sh_addr
- shdr
.sh_offset
;
563 * Convert trace point to probe point with debuginfo
565 static int find_perf_probe_point_from_dwarf(struct probe_trace_point
*tp
,
566 struct perf_probe_point
*pp
,
569 struct debuginfo
*dinfo
= NULL
;
570 unsigned long stext
= 0;
571 u64 addr
= tp
->address
;
574 /* convert the address to dwarf address */
580 ret
= get_text_start_address(tp
->module
, &stext
);
584 } else if (tp
->symbol
) {
585 /* If the module is given, this returns relative address */
586 ret
= kernel_get_symbol_address_by_name(tp
->symbol
, &addr
,
587 false, !!tp
->module
);
593 pr_debug("try to find information at %" PRIx64
" in %s\n", addr
,
594 tp
->module
? : "kernel");
596 dinfo
= debuginfo_cache__open(tp
->module
, verbose
<= 0);
598 ret
= debuginfo__find_probe_point(dinfo
,
599 (unsigned long)addr
, pp
);
604 pp
->retprobe
= tp
->retprobe
;
608 pr_debug("Failed to find corresponding probes from debuginfo.\n");
609 return ret
? : -ENOENT
;
612 /* Adjust symbol name and address */
613 static int post_process_probe_trace_point(struct probe_trace_point
*tp
,
614 struct map
*map
, unsigned long offs
)
617 u64 addr
= tp
->address
+ tp
->offset
- offs
;
619 sym
= map__find_symbol(map
, addr
);
623 if (strcmp(sym
->name
, tp
->symbol
)) {
624 /* If we have no realname, use symbol for it */
626 tp
->realname
= tp
->symbol
;
629 tp
->symbol
= strdup(sym
->name
);
633 tp
->offset
= addr
- sym
->start
;
640 * Rename DWARF symbols to ELF symbols -- gcc sometimes optimizes functions
641 * and generate new symbols with suffixes such as .constprop.N or .isra.N
642 * etc. Since those symbols are not recorded in DWARF, we have to find
643 * correct generated symbols from offline ELF binary.
644 * For online kernel or uprobes we don't need this because those are
645 * rebased on _text, or already a section relative address.
648 post_process_offline_probe_trace_events(struct probe_trace_event
*tevs
,
649 int ntevs
, const char *pathname
)
652 unsigned long stext
= 0;
655 /* Prepare a map for offline binary */
656 map
= dso__new_map(pathname
);
657 if (!map
|| get_text_start_address(pathname
, &stext
) < 0) {
658 pr_warning("Failed to get ELF symbols for %s\n", pathname
);
662 for (i
= 0; i
< ntevs
; i
++) {
663 ret
= post_process_probe_trace_point(&tevs
[i
].point
,
673 static int add_exec_to_probe_trace_events(struct probe_trace_event
*tevs
,
674 int ntevs
, const char *exec
)
677 unsigned long stext
= 0;
682 ret
= get_text_start_address(exec
, &stext
);
686 for (i
= 0; i
< ntevs
&& ret
>= 0; i
++) {
687 /* point.address is the addres of point.symbol + point.offset */
688 tevs
[i
].point
.address
-= stext
;
689 tevs
[i
].point
.module
= strdup(exec
);
690 if (!tevs
[i
].point
.module
) {
694 tevs
[i
].uprobes
= true;
701 post_process_module_probe_trace_events(struct probe_trace_event
*tevs
,
702 int ntevs
, const char *module
,
703 struct debuginfo
*dinfo
)
705 Dwarf_Addr text_offs
= 0;
707 char *mod_name
= NULL
;
713 map
= get_target_map(module
, false);
714 if (!map
|| debuginfo__get_text_offset(dinfo
, &text_offs
, true) < 0) {
715 pr_warning("Failed to get ELF symbols for %s\n", module
);
719 mod_name
= find_module_name(module
);
720 for (i
= 0; i
< ntevs
; i
++) {
721 ret
= post_process_probe_trace_point(&tevs
[i
].point
,
722 map
, (unsigned long)text_offs
);
725 tevs
[i
].point
.module
=
726 strdup(mod_name
? mod_name
: module
);
727 if (!tevs
[i
].point
.module
) {
740 post_process_kernel_probe_trace_events(struct probe_trace_event
*tevs
,
743 struct ref_reloc_sym
*reloc_sym
;
747 /* Skip post process if the target is an offline kernel */
748 if (symbol_conf
.ignore_vmlinux_buildid
)
749 return post_process_offline_probe_trace_events(tevs
, ntevs
,
750 symbol_conf
.vmlinux_name
);
752 reloc_sym
= kernel_get_ref_reloc_sym();
754 pr_warning("Relocated base symbol is not found!\n");
758 for (i
= 0; i
< ntevs
; i
++) {
759 if (!tevs
[i
].point
.address
)
761 if (tevs
[i
].point
.retprobe
&& !kretprobe_offset_is_supported())
763 /* If we found a wrong one, mark it by NULL symbol */
764 if (kprobe_warn_out_range(tevs
[i
].point
.symbol
,
765 tevs
[i
].point
.address
)) {
769 tmp
= strdup(reloc_sym
->name
);
773 /* If we have no realname, use symbol for it */
774 if (!tevs
[i
].point
.realname
)
775 tevs
[i
].point
.realname
= tevs
[i
].point
.symbol
;
777 free(tevs
[i
].point
.symbol
);
778 tevs
[i
].point
.symbol
= tmp
;
779 tevs
[i
].point
.offset
= tevs
[i
].point
.address
-
780 reloc_sym
->unrelocated_addr
;
786 arch__post_process_probe_trace_events(struct perf_probe_event
*pev __maybe_unused
,
787 int ntevs __maybe_unused
)
791 /* Post processing the probe events */
792 static int post_process_probe_trace_events(struct perf_probe_event
*pev
,
793 struct probe_trace_event
*tevs
,
794 int ntevs
, const char *module
,
795 bool uprobe
, struct debuginfo
*dinfo
)
800 ret
= add_exec_to_probe_trace_events(tevs
, ntevs
, module
);
802 /* Currently ref_reloc_sym based probe is not for drivers */
803 ret
= post_process_module_probe_trace_events(tevs
, ntevs
,
806 ret
= post_process_kernel_probe_trace_events(tevs
, ntevs
);
809 arch__post_process_probe_trace_events(pev
, ntevs
);
814 /* Try to find perf_probe_event with debuginfo */
815 static int try_to_find_probe_trace_events(struct perf_probe_event
*pev
,
816 struct probe_trace_event
**tevs
)
818 bool need_dwarf
= perf_probe_event_need_dwarf(pev
);
819 struct perf_probe_point tmp
;
820 struct debuginfo
*dinfo
;
823 dinfo
= open_debuginfo(pev
->target
, !need_dwarf
);
827 pr_debug("Could not open debuginfo. Try to use symbols.\n");
831 pr_debug("Try to find probe point from debuginfo.\n");
832 /* Searching trace events corresponding to a probe event */
833 ntevs
= debuginfo__find_trace_events(dinfo
, pev
, tevs
);
835 if (ntevs
== 0) { /* Not found, retry with an alternative */
836 ret
= get_alternative_probe_event(dinfo
, pev
, &tmp
);
838 ntevs
= debuginfo__find_trace_events(dinfo
, pev
, tevs
);
840 * Write back to the original probe_event for
841 * setting appropriate (user given) event name
843 clear_perf_probe_point(&pev
->point
);
844 memcpy(&pev
->point
, &tmp
, sizeof(tmp
));
848 if (ntevs
> 0) { /* Succeeded to find trace events */
849 pr_debug("Found %d probe_trace_events.\n", ntevs
);
850 ret
= post_process_probe_trace_events(pev
, *tevs
, ntevs
,
851 pev
->target
, pev
->uprobes
, dinfo
);
852 if (ret
< 0 || ret
== ntevs
) {
853 pr_debug("Post processing failed or all events are skipped. (%d)\n", ret
);
854 clear_probe_trace_events(*tevs
, ntevs
);
860 debuginfo__delete(dinfo
);
862 if (ntevs
== 0) { /* No error but failed to find probe point. */
863 pr_warning("Probe point '%s' not found.\n",
864 synthesize_perf_probe_point(&pev
->point
));
866 } else if (ntevs
< 0) {
867 /* Error path : ntevs < 0 */
868 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs
);
870 pr_warning("Warning: No dwarf info found in the vmlinux - "
871 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
873 pr_debug("Trying to use symbols.\n");
880 #define LINEBUF_SIZE 256
881 #define NR_ADDITIONAL_LINES 2
883 static int __show_one_line(FILE *fp
, int l
, bool skip
, bool show_num
)
885 char buf
[LINEBUF_SIZE
], sbuf
[STRERR_BUFSIZE
];
886 const char *color
= show_num
? "" : PERF_COLOR_BLUE
;
887 const char *prefix
= NULL
;
890 if (fgets(buf
, LINEBUF_SIZE
, fp
) == NULL
)
895 prefix
= show_num
? "%7d " : " ";
896 color_fprintf(stdout
, color
, prefix
, l
);
898 color_fprintf(stdout
, color
, "%s", buf
);
900 } while (strchr(buf
, '\n') == NULL
);
905 pr_warning("File read error: %s\n",
906 str_error_r(errno
, sbuf
, sizeof(sbuf
)));
912 static int _show_one_line(FILE *fp
, int l
, bool skip
, bool show_num
)
914 int rv
= __show_one_line(fp
, l
, skip
, show_num
);
916 pr_warning("Source file is shorter than expected.\n");
922 #define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
923 #define show_one_line(f,l) _show_one_line(f,l,false,false)
924 #define skip_one_line(f,l) _show_one_line(f,l,true,false)
925 #define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
928 * Show line-range always requires debuginfo to find source file and
931 static int __show_line_range(struct line_range
*lr
, const char *module
,
936 struct debuginfo
*dinfo
;
940 char sbuf
[STRERR_BUFSIZE
];
942 /* Search a line range */
943 dinfo
= open_debuginfo(module
, false);
947 ret
= debuginfo__find_line_range(dinfo
, lr
);
948 if (!ret
) { /* Not found, retry with an alternative */
949 ret
= get_alternative_line_range(dinfo
, lr
, module
, user
);
951 ret
= debuginfo__find_line_range(dinfo
, lr
);
953 debuginfo__delete(dinfo
);
954 if (ret
== 0 || ret
== -ENOENT
) {
955 pr_warning("Specified source line is not found.\n");
957 } else if (ret
< 0) {
958 pr_warning("Debuginfo analysis failed.\n");
962 /* Convert source file path */
964 ret
= get_real_path(tmp
, lr
->comp_dir
, &lr
->path
);
966 /* Free old path when new path is assigned */
971 pr_warning("Failed to find source file path.\n");
978 fprintf(stdout
, "<%s@%s:%d>\n", lr
->function
, lr
->path
,
979 lr
->start
- lr
->offset
);
981 fprintf(stdout
, "<%s:%d>\n", lr
->path
, lr
->start
);
983 fp
= fopen(lr
->path
, "r");
985 pr_warning("Failed to open %s: %s\n", lr
->path
,
986 str_error_r(errno
, sbuf
, sizeof(sbuf
)));
989 /* Skip to starting line number */
990 while (l
< lr
->start
) {
991 ret
= skip_one_line(fp
, l
++);
996 intlist__for_each_entry(ln
, lr
->line_list
) {
997 for (; ln
->i
> l
; l
++) {
998 ret
= show_one_line(fp
, l
- lr
->offset
);
1002 ret
= show_one_line_with_num(fp
, l
++ - lr
->offset
);
1007 if (lr
->end
== INT_MAX
)
1008 lr
->end
= l
+ NR_ADDITIONAL_LINES
;
1009 while (l
<= lr
->end
) {
1010 ret
= show_one_line_or_eof(fp
, l
++ - lr
->offset
);
1019 int show_line_range(struct line_range
*lr
, const char *module
, bool user
)
1023 ret
= init_probe_symbol_maps(user
);
1026 ret
= __show_line_range(lr
, module
, user
);
1027 exit_probe_symbol_maps();
1032 static int show_available_vars_at(struct debuginfo
*dinfo
,
1033 struct perf_probe_event
*pev
,
1034 struct strfilter
*_filter
)
1038 struct str_node
*node
;
1039 struct variable_list
*vls
= NULL
, *vl
;
1040 struct perf_probe_point tmp
;
1043 buf
= synthesize_perf_probe_point(&pev
->point
);
1046 pr_debug("Searching variables at %s\n", buf
);
1048 ret
= debuginfo__find_available_vars_at(dinfo
, pev
, &vls
);
1049 if (!ret
) { /* Not found, retry with an alternative */
1050 ret
= get_alternative_probe_event(dinfo
, pev
, &tmp
);
1052 ret
= debuginfo__find_available_vars_at(dinfo
, pev
,
1054 /* Release the old probe_point */
1055 clear_perf_probe_point(&tmp
);
1059 if (ret
== 0 || ret
== -ENOENT
) {
1060 pr_err("Failed to find the address of %s\n", buf
);
1063 pr_warning("Debuginfo analysis failed.\n");
1067 /* Some variables are found */
1068 fprintf(stdout
, "Available variables at %s\n", buf
);
1069 for (i
= 0; i
< ret
; i
++) {
1072 * A probe point might be converted to
1073 * several trace points.
1075 fprintf(stdout
, "\t@<%s+%lu>\n", vl
->point
.symbol
,
1077 zfree(&vl
->point
.symbol
);
1080 strlist__for_each_entry(node
, vl
->vars
) {
1081 var
= strchr(node
->s
, '\t') + 1;
1082 if (strfilter__compare(_filter
, var
)) {
1083 fprintf(stdout
, "\t\t%s\n", node
->s
);
1087 strlist__delete(vl
->vars
);
1090 fprintf(stdout
, "\t\t(No matched variables)\n");
1098 /* Show available variables on given probe point */
1099 int show_available_vars(struct perf_probe_event
*pevs
, int npevs
,
1100 struct strfilter
*_filter
)
1103 struct debuginfo
*dinfo
;
1105 ret
= init_probe_symbol_maps(pevs
->uprobes
);
1109 dinfo
= open_debuginfo(pevs
->target
, false);
1117 for (i
= 0; i
< npevs
&& ret
>= 0; i
++)
1118 ret
= show_available_vars_at(dinfo
, &pevs
[i
], _filter
);
1120 debuginfo__delete(dinfo
);
1122 exit_probe_symbol_maps();
1126 #else /* !HAVE_DWARF_SUPPORT */
1128 static void debuginfo_cache__exit(void)
1133 find_perf_probe_point_from_dwarf(struct probe_trace_point
*tp __maybe_unused
,
1134 struct perf_probe_point
*pp __maybe_unused
,
1135 bool is_kprobe __maybe_unused
)
1140 static int try_to_find_probe_trace_events(struct perf_probe_event
*pev
,
1141 struct probe_trace_event
**tevs __maybe_unused
)
1143 if (perf_probe_event_need_dwarf(pev
)) {
1144 pr_warning("Debuginfo-analysis is not supported.\n");
1151 int show_line_range(struct line_range
*lr __maybe_unused
,
1152 const char *module __maybe_unused
,
1153 bool user __maybe_unused
)
1155 pr_warning("Debuginfo-analysis is not supported.\n");
1159 int show_available_vars(struct perf_probe_event
*pevs __maybe_unused
,
1160 int npevs __maybe_unused
,
1161 struct strfilter
*filter __maybe_unused
)
1163 pr_warning("Debuginfo-analysis is not supported.\n");
1168 void line_range__clear(struct line_range
*lr
)
1174 intlist__delete(lr
->line_list
);
1175 memset(lr
, 0, sizeof(*lr
));
1178 int line_range__init(struct line_range
*lr
)
1180 memset(lr
, 0, sizeof(*lr
));
1181 lr
->line_list
= intlist__new(NULL
);
1188 static int parse_line_num(char **ptr
, int *val
, const char *what
)
1190 const char *start
= *ptr
;
1193 *val
= strtol(*ptr
, ptr
, 0);
1194 if (errno
|| *ptr
== start
) {
1195 semantic_error("'%s' is not a valid number.\n", what
);
1201 /* Check the name is good for event, group or function */
1202 static bool is_c_func_name(const char *name
)
1204 if (!isalpha(*name
) && *name
!= '_')
1206 while (*++name
!= '\0') {
1207 if (!isalpha(*name
) && !isdigit(*name
) && *name
!= '_')
1214 * Stuff 'lr' according to the line range described by 'arg'.
1215 * The line range syntax is described by:
1217 * SRC[:SLN[+NUM|-ELN]]
1218 * FNC[@SRC][:SLN[+NUM|-ELN]]
1220 int parse_line_range_desc(const char *arg
, struct line_range
*lr
)
1222 char *range
, *file
, *name
= strdup(arg
);
1231 range
= strchr(name
, ':');
1235 err
= parse_line_num(&range
, &lr
->start
, "start line");
1239 if (*range
== '+' || *range
== '-') {
1240 const char c
= *range
++;
1242 err
= parse_line_num(&range
, &lr
->end
, "end line");
1247 lr
->end
+= lr
->start
;
1249 * Adjust the number of lines here.
1250 * If the number of lines == 1, the
1251 * the end of line should be equal to
1252 * the start of line.
1258 pr_debug("Line range is %d to %d\n", lr
->start
, lr
->end
);
1261 if (lr
->start
> lr
->end
) {
1262 semantic_error("Start line must be smaller"
1263 " than end line.\n");
1266 if (*range
!= '\0') {
1267 semantic_error("Tailing with invalid str '%s'.\n", range
);
1272 file
= strchr(name
, '@');
1275 lr
->file
= strdup(++file
);
1276 if (lr
->file
== NULL
) {
1280 lr
->function
= name
;
1281 } else if (strchr(name
, '/') || strchr(name
, '.'))
1283 else if (is_c_func_name(name
))/* We reuse it for checking funcname */
1284 lr
->function
= name
;
1285 else { /* Invalid name */
1286 semantic_error("'%s' is not a valid function name.\n", name
);
1297 static int parse_perf_probe_event_name(char **arg
, struct perf_probe_event
*pev
)
1301 ptr
= strchr(*arg
, ':');
1304 if (!pev
->sdt
&& !is_c_func_name(*arg
))
1306 pev
->group
= strdup(*arg
);
1312 if (!pev
->sdt
&& !is_c_func_name(*arg
)) {
1314 semantic_error("%s is bad for event name -it must "
1315 "follow C symbol-naming rule.\n", *arg
);
1318 pev
->event
= strdup(*arg
);
1319 if (pev
->event
== NULL
)
1325 /* Parse probepoint definition. */
1326 static int parse_perf_probe_point(char *arg
, struct perf_probe_event
*pev
)
1328 struct perf_probe_point
*pp
= &pev
->point
;
1331 bool file_spec
= false;
1336 * perf probe [GRP:][EVENT=]SRC[:LN|;PTN]
1337 * perf probe [GRP:][EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
1338 * perf probe %[GRP:]SDT_EVENT
1343 if (is_sdt_event(arg
)) {
1349 ptr
= strpbrk(arg
, ";=@+%");
1353 semantic_error("%s must be an SDT name.\n",
1357 /* This must be a target file name or build id */
1358 tmp
= build_id_cache__complement(ptr
+ 1);
1360 pev
->target
= build_id_cache__origname(tmp
);
1363 pev
->target
= strdup(ptr
+ 1);
1368 ret
= parse_perf_probe_event_name(&arg
, pev
);
1370 if (asprintf(&pev
->point
.function
, "%%%s", pev
->event
) < 0)
1376 if (ptr
&& *ptr
== '=') { /* Event name */
1379 ret
= parse_perf_probe_event_name(&arg
, pev
);
1387 * Check arg is function or file name and copy it.
1389 * We consider arg to be a file spec if and only if it satisfies
1390 * all of the below criteria::
1391 * - it does not include any of "+@%",
1392 * - it includes one of ":;", and
1393 * - it has a period '.' in the name.
1395 * Otherwise, we consider arg to be a function specification.
1397 if (!strpbrk(arg
, "+@%") && (ptr
= strpbrk(arg
, ";:")) != NULL
) {
1398 /* This is a file spec if it includes a '.' before ; or : */
1399 if (memchr(arg
, '.', ptr
- arg
))
1403 ptr
= strpbrk(arg
, ";:+@%");
1423 * Keep pp->function even if this is absolute address,
1424 * so it can mark whether abs_address is valid.
1425 * Which make 'perf probe lib.bin 0x0' possible.
1427 * Note that checking length of tmp is not needed
1428 * because when we access tmp[1] we know tmp[0] is '0',
1429 * so tmp[1] should always valid (but could be '\0').
1431 if (tmp
&& !strncmp(tmp
, "0x", 2)) {
1432 pp
->abs_address
= strtoul(pp
->function
, &tmp
, 0);
1434 semantic_error("Invalid absolute address.\n");
1440 /* Parse other options */
1444 if (c
== ';') { /* Lazy pattern must be the last part */
1445 pp
->lazy_line
= strdup(arg
);
1446 if (pp
->lazy_line
== NULL
)
1450 ptr
= strpbrk(arg
, ";:+@%");
1456 case ':': /* Line number */
1457 pp
->line
= strtoul(arg
, &tmp
, 0);
1459 semantic_error("There is non-digit char"
1460 " in line number.\n");
1464 case '+': /* Byte offset from a symbol */
1465 pp
->offset
= strtoul(arg
, &tmp
, 0);
1467 semantic_error("There is non-digit character"
1472 case '@': /* File name */
1474 semantic_error("SRC@SRC is not allowed.\n");
1477 pp
->file
= strdup(arg
);
1478 if (pp
->file
== NULL
)
1481 case '%': /* Probe places */
1482 if (strcmp(arg
, "return") == 0) {
1484 } else { /* Others not supported yet */
1485 semantic_error("%%%s is not supported.\n", arg
);
1489 default: /* Buggy case */
1490 pr_err("This program has a bug at %s:%d.\n",
1491 __FILE__
, __LINE__
);
1497 /* Exclusion check */
1498 if (pp
->lazy_line
&& pp
->line
) {
1499 semantic_error("Lazy pattern can't be used with"
1504 if (pp
->lazy_line
&& pp
->offset
) {
1505 semantic_error("Lazy pattern can't be used with offset.\n");
1509 if (pp
->line
&& pp
->offset
) {
1510 semantic_error("Offset can't be used with line number.\n");
1514 if (!pp
->line
&& !pp
->lazy_line
&& pp
->file
&& !pp
->function
) {
1515 semantic_error("File always requires line number or "
1520 if (pp
->offset
&& !pp
->function
) {
1521 semantic_error("Offset requires an entry function.\n");
1525 if ((pp
->offset
|| pp
->line
|| pp
->lazy_line
) && pp
->retprobe
) {
1526 semantic_error("Offset/Line/Lazy pattern can't be used with "
1531 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
1532 pp
->function
, pp
->file
, pp
->line
, pp
->offset
, pp
->retprobe
,
1537 /* Parse perf-probe event argument */
1538 static int parse_perf_probe_arg(char *str
, struct perf_probe_arg
*arg
)
1540 char *tmp
, *goodname
;
1541 struct perf_probe_arg_field
**fieldp
;
1543 pr_debug("parsing arg: %s into ", str
);
1545 tmp
= strchr(str
, '=');
1547 arg
->name
= strndup(str
, tmp
- str
);
1548 if (arg
->name
== NULL
)
1550 pr_debug("name:%s ", arg
->name
);
1554 tmp
= strchr(str
, ':');
1555 if (tmp
) { /* Type setting */
1557 arg
->type
= strdup(tmp
+ 1);
1558 if (arg
->type
== NULL
)
1560 pr_debug("type:%s ", arg
->type
);
1563 tmp
= strpbrk(str
, "-.[");
1564 if (!is_c_varname(str
) || !tmp
) {
1565 /* A variable, register, symbol or special value */
1566 arg
->var
= strdup(str
);
1567 if (arg
->var
== NULL
)
1569 pr_debug("%s\n", arg
->var
);
1573 /* Structure fields or array element */
1574 arg
->var
= strndup(str
, tmp
- str
);
1575 if (arg
->var
== NULL
)
1577 goodname
= arg
->var
;
1578 pr_debug("%s, ", arg
->var
);
1579 fieldp
= &arg
->field
;
1582 *fieldp
= zalloc(sizeof(struct perf_probe_arg_field
));
1583 if (*fieldp
== NULL
)
1585 if (*tmp
== '[') { /* Array */
1587 (*fieldp
)->index
= strtol(str
+ 1, &tmp
, 0);
1588 (*fieldp
)->ref
= true;
1589 if (*tmp
!= ']' || tmp
== str
+ 1) {
1590 semantic_error("Array index must be a"
1597 } else { /* Structure */
1600 (*fieldp
)->ref
= false;
1601 } else if (tmp
[1] == '>') {
1603 (*fieldp
)->ref
= true;
1605 semantic_error("Argument parse error: %s\n",
1609 tmp
= strpbrk(str
, "-.[");
1612 (*fieldp
)->name
= strndup(str
, tmp
- str
);
1613 if ((*fieldp
)->name
== NULL
)
1616 goodname
= (*fieldp
)->name
;
1617 pr_debug("%s(%d), ", (*fieldp
)->name
, (*fieldp
)->ref
);
1618 fieldp
= &(*fieldp
)->next
;
1621 (*fieldp
)->name
= strdup(str
);
1622 if ((*fieldp
)->name
== NULL
)
1625 goodname
= (*fieldp
)->name
;
1626 pr_debug("%s(%d)\n", (*fieldp
)->name
, (*fieldp
)->ref
);
1628 /* If no name is specified, set the last field name (not array index)*/
1630 arg
->name
= strdup(goodname
);
1631 if (arg
->name
== NULL
)
1637 /* Parse perf-probe event command */
1638 int parse_perf_probe_command(const char *cmd
, struct perf_probe_event
*pev
)
1641 int argc
, i
, ret
= 0;
1643 argv
= argv_split(cmd
, &argc
);
1645 pr_debug("Failed to split arguments.\n");
1648 if (argc
- 1 > MAX_PROBE_ARGS
) {
1649 semantic_error("Too many probe arguments (%d).\n", argc
- 1);
1653 /* Parse probe point */
1654 ret
= parse_perf_probe_point(argv
[0], pev
);
1658 /* Copy arguments and ensure return probe has no C argument */
1659 pev
->nargs
= argc
- 1;
1660 pev
->args
= zalloc(sizeof(struct perf_probe_arg
) * pev
->nargs
);
1661 if (pev
->args
== NULL
) {
1665 for (i
= 0; i
< pev
->nargs
&& ret
>= 0; i
++) {
1666 ret
= parse_perf_probe_arg(argv
[i
+ 1], &pev
->args
[i
]);
1668 is_c_varname(pev
->args
[i
].var
) && pev
->point
.retprobe
) {
1669 semantic_error("You can't specify local variable for"
1680 /* Returns true if *any* ARG is either C variable, $params or $vars. */
1681 bool perf_probe_with_var(struct perf_probe_event
*pev
)
1685 for (i
= 0; i
< pev
->nargs
; i
++)
1686 if (is_c_varname(pev
->args
[i
].var
) ||
1687 !strcmp(pev
->args
[i
].var
, PROBE_ARG_PARAMS
) ||
1688 !strcmp(pev
->args
[i
].var
, PROBE_ARG_VARS
))
1693 /* Return true if this perf_probe_event requires debuginfo */
1694 bool perf_probe_event_need_dwarf(struct perf_probe_event
*pev
)
1696 if (pev
->point
.file
|| pev
->point
.line
|| pev
->point
.lazy_line
)
1699 if (perf_probe_with_var(pev
))
1705 /* Parse probe_events event into struct probe_point */
1706 int parse_probe_trace_command(const char *cmd
, struct probe_trace_event
*tev
)
1708 struct probe_trace_point
*tp
= &tev
->point
;
1711 char *argv0_str
= NULL
, *fmt
, *fmt1_str
, *fmt2_str
, *fmt3_str
;
1715 pr_debug("Parsing probe_events: %s\n", cmd
);
1716 argv
= argv_split(cmd
, &argc
);
1718 pr_debug("Failed to split arguments.\n");
1722 semantic_error("Too few probe arguments.\n");
1727 /* Scan event and group name. */
1728 argv0_str
= strdup(argv
[0]);
1729 if (argv0_str
== NULL
) {
1733 fmt1_str
= strtok_r(argv0_str
, ":", &fmt
);
1734 fmt2_str
= strtok_r(NULL
, "/", &fmt
);
1735 fmt3_str
= strtok_r(NULL
, " \t", &fmt
);
1736 if (fmt1_str
== NULL
|| strlen(fmt1_str
) != 1 || fmt2_str
== NULL
1737 || fmt3_str
== NULL
) {
1738 semantic_error("Failed to parse event name: %s\n", argv
[0]);
1743 tev
->group
= strdup(fmt2_str
);
1744 tev
->event
= strdup(fmt3_str
);
1745 if (tev
->group
== NULL
|| tev
->event
== NULL
) {
1749 pr_debug("Group:%s Event:%s probe:%c\n", tev
->group
, tev
->event
, pr
);
1751 tp
->retprobe
= (pr
== 'r');
1753 /* Scan module name(if there), function name and offset */
1754 p
= strchr(argv
[1], ':');
1756 tp
->module
= strndup(argv
[1], p
- argv
[1]);
1761 tev
->uprobes
= (tp
->module
[0] == '/');
1765 fmt1_str
= strtok_r(p
, "+", &fmt
);
1766 /* only the address started with 0x */
1767 if (fmt1_str
[0] == '0') {
1769 * Fix a special case:
1770 * if address == 0, kernel reports something like:
1771 * p:probe_libc/abs_0 /lib/libc-2.18.so:0x (null) arg1=%ax
1772 * Newer kernel may fix that, but we want to
1773 * support old kernel also.
1775 if (strcmp(fmt1_str
, "0x") == 0) {
1776 if (!argv
[2] || strcmp(argv
[2], "(null)")) {
1783 for (i
= 2; argv
[i
+ 1] != NULL
; i
++)
1784 argv
[i
] = argv
[i
+ 1];
1789 tp
->address
= strtoul(fmt1_str
, NULL
, 0);
1791 /* Only the symbol-based probe has offset */
1792 tp
->symbol
= strdup(fmt1_str
);
1793 if (tp
->symbol
== NULL
) {
1797 fmt2_str
= strtok_r(NULL
, "", &fmt
);
1798 if (fmt2_str
== NULL
)
1801 tp
->offset
= strtoul(fmt2_str
, NULL
, 10);
1804 tev
->nargs
= argc
- 2;
1805 tev
->args
= zalloc(sizeof(struct probe_trace_arg
) * tev
->nargs
);
1806 if (tev
->args
== NULL
) {
1810 for (i
= 0; i
< tev
->nargs
; i
++) {
1811 p
= strchr(argv
[i
+ 2], '=');
1812 if (p
) /* We don't need which register is assigned. */
1816 tev
->args
[i
].name
= strdup(argv
[i
+ 2]);
1817 /* TODO: parse regs and offset */
1818 tev
->args
[i
].value
= strdup(p
);
1819 if (tev
->args
[i
].name
== NULL
|| tev
->args
[i
].value
== NULL
) {
1831 /* Compose only probe arg */
1832 char *synthesize_perf_probe_arg(struct perf_probe_arg
*pa
)
1834 struct perf_probe_arg_field
*field
= pa
->field
;
1839 if (strbuf_init(&buf
, 64) < 0)
1842 if (pa
->name
&& pa
->var
)
1843 err
= strbuf_addf(&buf
, "%s=%s", pa
->name
, pa
->var
);
1845 err
= strbuf_addstr(&buf
, pa
->name
?: pa
->var
);
1850 if (field
->name
[0] == '[')
1851 err
= strbuf_addstr(&buf
, field
->name
);
1853 err
= strbuf_addf(&buf
, "%s%s", field
->ref
? "->" : ".",
1855 field
= field
->next
;
1861 if (strbuf_addf(&buf
, ":%s", pa
->type
) < 0)
1864 ret
= strbuf_detach(&buf
, NULL
);
1866 strbuf_release(&buf
);
1870 /* Compose only probe point (not argument) */
1871 char *synthesize_perf_probe_point(struct perf_probe_point
*pp
)
1874 char *tmp
, *ret
= NULL
;
1877 if (strbuf_init(&buf
, 64) < 0)
1881 if (strbuf_addstr(&buf
, pp
->function
) < 0)
1884 err
= strbuf_addf(&buf
, "+%lu", pp
->offset
);
1886 err
= strbuf_addf(&buf
, ":%d", pp
->line
);
1887 else if (pp
->retprobe
)
1888 err
= strbuf_addstr(&buf
, "%return");
1896 tmp
= strchr(pp
->file
+ len
- 30, '/');
1897 tmp
= tmp
? tmp
+ 1 : pp
->file
+ len
- 30;
1899 err
= strbuf_addf(&buf
, "@%s", tmp
);
1900 if (!err
&& !pp
->function
&& pp
->line
)
1901 err
= strbuf_addf(&buf
, ":%d", pp
->line
);
1904 ret
= strbuf_detach(&buf
, NULL
);
1906 strbuf_release(&buf
);
1910 char *synthesize_perf_probe_command(struct perf_probe_event
*pev
)
1913 char *tmp
, *ret
= NULL
;
1916 if (strbuf_init(&buf
, 64))
1919 if (strbuf_addf(&buf
, "%s:%s=", pev
->group
?: PERFPROBE_GROUP
,
1923 tmp
= synthesize_perf_probe_point(&pev
->point
);
1924 if (!tmp
|| strbuf_addstr(&buf
, tmp
) < 0)
1928 for (i
= 0; i
< pev
->nargs
; i
++) {
1929 tmp
= synthesize_perf_probe_arg(pev
->args
+ i
);
1930 if (!tmp
|| strbuf_addf(&buf
, " %s", tmp
) < 0)
1935 ret
= strbuf_detach(&buf
, NULL
);
1937 strbuf_release(&buf
);
1941 static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref
*ref
,
1942 struct strbuf
*buf
, int depth
)
1946 depth
= __synthesize_probe_trace_arg_ref(ref
->next
, buf
,
1951 err
= strbuf_addf(buf
, "%+ld(", ref
->offset
);
1952 return (err
< 0) ? err
: depth
;
1955 static int synthesize_probe_trace_arg(struct probe_trace_arg
*arg
,
1958 struct probe_trace_arg_ref
*ref
= arg
->ref
;
1961 /* Argument name or separator */
1963 err
= strbuf_addf(buf
, " %s=", arg
->name
);
1965 err
= strbuf_addch(buf
, ' ');
1969 /* Special case: @XXX */
1970 if (arg
->value
[0] == '@' && arg
->ref
)
1973 /* Dereferencing arguments */
1975 depth
= __synthesize_probe_trace_arg_ref(ref
, buf
, 1);
1980 /* Print argument value */
1981 if (arg
->value
[0] == '@' && arg
->ref
)
1982 err
= strbuf_addf(buf
, "%s%+ld", arg
->value
, arg
->ref
->offset
);
1984 err
= strbuf_addstr(buf
, arg
->value
);
1987 while (!err
&& depth
--)
1988 err
= strbuf_addch(buf
, ')');
1990 /* Print argument type */
1991 if (!err
&& arg
->type
)
1992 err
= strbuf_addf(buf
, ":%s", arg
->type
);
1997 char *synthesize_probe_trace_command(struct probe_trace_event
*tev
)
1999 struct probe_trace_point
*tp
= &tev
->point
;
2004 /* Uprobes must have tp->module */
2005 if (tev
->uprobes
&& !tp
->module
)
2008 if (strbuf_init(&buf
, 32) < 0)
2011 if (strbuf_addf(&buf
, "%c:%s/%s ", tp
->retprobe
? 'r' : 'p',
2012 tev
->group
, tev
->event
) < 0)
2015 * If tp->address == 0, then this point must be a
2016 * absolute address uprobe.
2017 * try_to_find_absolute_address() should have made
2018 * tp->symbol to "0x0".
2020 if (tev
->uprobes
&& !tp
->address
) {
2021 if (!tp
->symbol
|| strcmp(tp
->symbol
, "0x0"))
2025 /* Use the tp->address for uprobes */
2027 err
= strbuf_addf(&buf
, "%s:0x%lx", tp
->module
, tp
->address
);
2028 else if (!strncmp(tp
->symbol
, "0x", 2))
2029 /* Absolute address. See try_to_find_absolute_address() */
2030 err
= strbuf_addf(&buf
, "%s%s0x%lx", tp
->module
?: "",
2031 tp
->module
? ":" : "", tp
->address
);
2033 err
= strbuf_addf(&buf
, "%s%s%s+%lu", tp
->module
?: "",
2034 tp
->module
? ":" : "", tp
->symbol
, tp
->offset
);
2038 for (i
= 0; i
< tev
->nargs
; i
++)
2039 if (synthesize_probe_trace_arg(&tev
->args
[i
], &buf
) < 0)
2042 ret
= strbuf_detach(&buf
, NULL
);
2044 strbuf_release(&buf
);
2048 static int find_perf_probe_point_from_map(struct probe_trace_point
*tp
,
2049 struct perf_probe_point
*pp
,
2052 struct symbol
*sym
= NULL
;
2053 struct map
*map
= NULL
;
2054 u64 addr
= tp
->address
;
2058 map
= dso__new_map(tp
->module
);
2061 sym
= map__find_symbol(map
, addr
);
2063 if (tp
->symbol
&& !addr
) {
2064 if (kernel_get_symbol_address_by_name(tp
->symbol
,
2065 &addr
, true, false) < 0)
2070 sym
= __find_kernel_function(addr
, &map
);
2077 pp
->retprobe
= tp
->retprobe
;
2078 pp
->offset
= addr
- map
->unmap_ip(map
, sym
->start
);
2079 pp
->function
= strdup(sym
->name
);
2080 ret
= pp
->function
? 0 : -ENOMEM
;
2083 if (map
&& !is_kprobe
) {
2090 static int convert_to_perf_probe_point(struct probe_trace_point
*tp
,
2091 struct perf_probe_point
*pp
,
2097 ret
= find_perf_probe_point_from_dwarf(tp
, pp
, is_kprobe
);
2100 ret
= find_perf_probe_point_from_map(tp
, pp
, is_kprobe
);
2104 pr_debug("Failed to find probe point from both of dwarf and map.\n");
2107 pp
->function
= strdup(tp
->symbol
);
2108 pp
->offset
= tp
->offset
;
2110 ret
= e_snprintf(buf
, 128, "0x%" PRIx64
, (u64
)tp
->address
);
2113 pp
->function
= strdup(buf
);
2116 if (pp
->function
== NULL
)
2119 pp
->retprobe
= tp
->retprobe
;
2124 static int convert_to_perf_probe_event(struct probe_trace_event
*tev
,
2125 struct perf_probe_event
*pev
, bool is_kprobe
)
2127 struct strbuf buf
= STRBUF_INIT
;
2130 /* Convert event/group name */
2131 pev
->event
= strdup(tev
->event
);
2132 pev
->group
= strdup(tev
->group
);
2133 if (pev
->event
== NULL
|| pev
->group
== NULL
)
2136 /* Convert trace_point to probe_point */
2137 ret
= convert_to_perf_probe_point(&tev
->point
, &pev
->point
, is_kprobe
);
2141 /* Convert trace_arg to probe_arg */
2142 pev
->nargs
= tev
->nargs
;
2143 pev
->args
= zalloc(sizeof(struct perf_probe_arg
) * pev
->nargs
);
2144 if (pev
->args
== NULL
)
2146 for (i
= 0; i
< tev
->nargs
&& ret
>= 0; i
++) {
2147 if (tev
->args
[i
].name
)
2148 pev
->args
[i
].name
= strdup(tev
->args
[i
].name
);
2150 if ((ret
= strbuf_init(&buf
, 32)) < 0)
2152 ret
= synthesize_probe_trace_arg(&tev
->args
[i
], &buf
);
2153 pev
->args
[i
].name
= strbuf_detach(&buf
, NULL
);
2155 if (pev
->args
[i
].name
== NULL
&& ret
>= 0)
2160 clear_perf_probe_event(pev
);
2165 void clear_perf_probe_event(struct perf_probe_event
*pev
)
2167 struct perf_probe_arg_field
*field
, *next
;
2173 clear_perf_probe_point(&pev
->point
);
2175 for (i
= 0; i
< pev
->nargs
; i
++) {
2176 free(pev
->args
[i
].name
);
2177 free(pev
->args
[i
].var
);
2178 free(pev
->args
[i
].type
);
2179 field
= pev
->args
[i
].field
;
2182 zfree(&field
->name
);
2188 memset(pev
, 0, sizeof(*pev
));
2191 #define strdup_or_goto(str, label) \
2192 ({ char *__p = NULL; if (str && !(__p = strdup(str))) goto label; __p; })
2194 static int perf_probe_point__copy(struct perf_probe_point
*dst
,
2195 struct perf_probe_point
*src
)
2197 dst
->file
= strdup_or_goto(src
->file
, out_err
);
2198 dst
->function
= strdup_or_goto(src
->function
, out_err
);
2199 dst
->lazy_line
= strdup_or_goto(src
->lazy_line
, out_err
);
2200 dst
->line
= src
->line
;
2201 dst
->retprobe
= src
->retprobe
;
2202 dst
->offset
= src
->offset
;
2206 clear_perf_probe_point(dst
);
2210 static int perf_probe_arg__copy(struct perf_probe_arg
*dst
,
2211 struct perf_probe_arg
*src
)
2213 struct perf_probe_arg_field
*field
, **ppfield
;
2215 dst
->name
= strdup_or_goto(src
->name
, out_err
);
2216 dst
->var
= strdup_or_goto(src
->var
, out_err
);
2217 dst
->type
= strdup_or_goto(src
->type
, out_err
);
2220 ppfield
= &(dst
->field
);
2222 *ppfield
= zalloc(sizeof(*field
));
2225 (*ppfield
)->name
= strdup_or_goto(field
->name
, out_err
);
2226 (*ppfield
)->index
= field
->index
;
2227 (*ppfield
)->ref
= field
->ref
;
2228 field
= field
->next
;
2229 ppfield
= &((*ppfield
)->next
);
2236 int perf_probe_event__copy(struct perf_probe_event
*dst
,
2237 struct perf_probe_event
*src
)
2241 dst
->event
= strdup_or_goto(src
->event
, out_err
);
2242 dst
->group
= strdup_or_goto(src
->group
, out_err
);
2243 dst
->target
= strdup_or_goto(src
->target
, out_err
);
2244 dst
->uprobes
= src
->uprobes
;
2246 if (perf_probe_point__copy(&dst
->point
, &src
->point
) < 0)
2249 dst
->args
= zalloc(sizeof(struct perf_probe_arg
) * src
->nargs
);
2252 dst
->nargs
= src
->nargs
;
2254 for (i
= 0; i
< src
->nargs
; i
++)
2255 if (perf_probe_arg__copy(&dst
->args
[i
], &src
->args
[i
]) < 0)
2260 clear_perf_probe_event(dst
);
2264 void clear_probe_trace_event(struct probe_trace_event
*tev
)
2266 struct probe_trace_arg_ref
*ref
, *next
;
2271 free(tev
->point
.symbol
);
2272 free(tev
->point
.realname
);
2273 free(tev
->point
.module
);
2274 for (i
= 0; i
< tev
->nargs
; i
++) {
2275 free(tev
->args
[i
].name
);
2276 free(tev
->args
[i
].value
);
2277 free(tev
->args
[i
].type
);
2278 ref
= tev
->args
[i
].ref
;
2286 memset(tev
, 0, sizeof(*tev
));
2289 struct kprobe_blacklist_node
{
2290 struct list_head list
;
2291 unsigned long start
;
2296 static void kprobe_blacklist__delete(struct list_head
*blacklist
)
2298 struct kprobe_blacklist_node
*node
;
2300 while (!list_empty(blacklist
)) {
2301 node
= list_first_entry(blacklist
,
2302 struct kprobe_blacklist_node
, list
);
2303 list_del(&node
->list
);
2309 static int kprobe_blacklist__load(struct list_head
*blacklist
)
2311 struct kprobe_blacklist_node
*node
;
2312 const char *__debugfs
= debugfs__mountpoint();
2313 char buf
[PATH_MAX
], *p
;
2317 if (__debugfs
== NULL
)
2320 ret
= e_snprintf(buf
, PATH_MAX
, "%s/kprobes/blacklist", __debugfs
);
2324 fp
= fopen(buf
, "r");
2329 while (fgets(buf
, PATH_MAX
, fp
)) {
2330 node
= zalloc(sizeof(*node
));
2335 INIT_LIST_HEAD(&node
->list
);
2336 list_add_tail(&node
->list
, blacklist
);
2337 if (sscanf(buf
, "0x%lx-0x%lx", &node
->start
, &node
->end
) != 2) {
2341 p
= strchr(buf
, '\t');
2344 if (p
[strlen(p
) - 1] == '\n')
2345 p
[strlen(p
) - 1] = '\0';
2347 p
= (char *)"unknown";
2348 node
->symbol
= strdup(p
);
2349 if (!node
->symbol
) {
2353 pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
2354 node
->start
, node
->end
, node
->symbol
);
2358 kprobe_blacklist__delete(blacklist
);
2364 static struct kprobe_blacklist_node
*
2365 kprobe_blacklist__find_by_address(struct list_head
*blacklist
,
2366 unsigned long address
)
2368 struct kprobe_blacklist_node
*node
;
2370 list_for_each_entry(node
, blacklist
, list
) {
2371 if (node
->start
<= address
&& address
<= node
->end
)
2378 static LIST_HEAD(kprobe_blacklist
);
2380 static void kprobe_blacklist__init(void)
2382 if (!list_empty(&kprobe_blacklist
))
2385 if (kprobe_blacklist__load(&kprobe_blacklist
) < 0)
2386 pr_debug("No kprobe blacklist support, ignored\n");
2389 static void kprobe_blacklist__release(void)
2391 kprobe_blacklist__delete(&kprobe_blacklist
);
2394 static bool kprobe_blacklist__listed(unsigned long address
)
2396 return !!kprobe_blacklist__find_by_address(&kprobe_blacklist
, address
);
2399 static int perf_probe_event__sprintf(const char *group
, const char *event
,
2400 struct perf_probe_event
*pev
,
2402 struct strbuf
*result
)
2407 if (asprintf(&buf
, "%s:%s", group
, event
) < 0)
2409 ret
= strbuf_addf(result
, " %-20s (on ", buf
);
2414 /* Synthesize only event probe point */
2415 buf
= synthesize_perf_probe_point(&pev
->point
);
2418 ret
= strbuf_addstr(result
, buf
);
2422 ret
= strbuf_addf(result
, " in %s", module
);
2424 if (!ret
&& pev
->nargs
> 0) {
2425 ret
= strbuf_add(result
, " with", 5);
2426 for (i
= 0; !ret
&& i
< pev
->nargs
; i
++) {
2427 buf
= synthesize_perf_probe_arg(&pev
->args
[i
]);
2430 ret
= strbuf_addf(result
, " %s", buf
);
2435 ret
= strbuf_addch(result
, ')');
2441 int show_perf_probe_event(const char *group
, const char *event
,
2442 struct perf_probe_event
*pev
,
2443 const char *module
, bool use_stdout
)
2445 struct strbuf buf
= STRBUF_INIT
;
2448 ret
= perf_probe_event__sprintf(group
, event
, pev
, module
, &buf
);
2451 printf("%s\n", buf
.buf
);
2453 pr_info("%s\n", buf
.buf
);
2455 strbuf_release(&buf
);
2460 static bool filter_probe_trace_event(struct probe_trace_event
*tev
,
2461 struct strfilter
*filter
)
2465 /* At first, check the event name itself */
2466 if (strfilter__compare(filter
, tev
->event
))
2469 /* Next, check the combination of name and group */
2470 if (e_snprintf(tmp
, 128, "%s:%s", tev
->group
, tev
->event
) < 0)
2472 return strfilter__compare(filter
, tmp
);
2475 static int __show_perf_probe_events(int fd
, bool is_kprobe
,
2476 struct strfilter
*filter
)
2479 struct probe_trace_event tev
;
2480 struct perf_probe_event pev
;
2481 struct strlist
*rawlist
;
2482 struct str_node
*ent
;
2484 memset(&tev
, 0, sizeof(tev
));
2485 memset(&pev
, 0, sizeof(pev
));
2487 rawlist
= probe_file__get_rawlist(fd
);
2491 strlist__for_each_entry(ent
, rawlist
) {
2492 ret
= parse_probe_trace_command(ent
->s
, &tev
);
2494 if (!filter_probe_trace_event(&tev
, filter
))
2496 ret
= convert_to_perf_probe_event(&tev
, &pev
,
2500 ret
= show_perf_probe_event(pev
.group
, pev
.event
,
2501 &pev
, tev
.point
.module
,
2505 clear_perf_probe_event(&pev
);
2506 clear_probe_trace_event(&tev
);
2510 strlist__delete(rawlist
);
2511 /* Cleanup cached debuginfo if needed */
2512 debuginfo_cache__exit();
2517 /* List up current perf-probe events */
2518 int show_perf_probe_events(struct strfilter
*filter
)
2520 int kp_fd
, up_fd
, ret
;
2524 if (probe_conf
.cache
)
2525 return probe_cache__show_all_caches(filter
);
2527 ret
= init_probe_symbol_maps(false);
2531 ret
= probe_file__open_both(&kp_fd
, &up_fd
, 0);
2536 ret
= __show_perf_probe_events(kp_fd
, true, filter
);
2537 if (up_fd
>= 0 && ret
>= 0)
2538 ret
= __show_perf_probe_events(up_fd
, false, filter
);
2543 exit_probe_symbol_maps();
2548 static int get_new_event_name(char *buf
, size_t len
, const char *base
,
2549 struct strlist
*namelist
, bool allow_suffix
)
2556 nbase
= strdup(base
);
2560 /* Cut off the dot suffixes (e.g. .const, .isra)*/
2561 p
= strchr(nbase
, '.');
2562 if (p
&& p
!= nbase
)
2565 /* Try no suffix number */
2566 ret
= e_snprintf(buf
, len
, "%s", nbase
);
2568 pr_debug("snprintf() failed: %d\n", ret
);
2571 if (!strlist__has_entry(namelist
, buf
))
2574 if (!allow_suffix
) {
2575 pr_warning("Error: event \"%s\" already exists.\n"
2576 " Hint: Remove existing event by 'perf probe -d'\n"
2577 " or force duplicates by 'perf probe -f'\n"
2578 " or set 'force=yes' in BPF source.\n",
2584 /* Try to add suffix */
2585 for (i
= 1; i
< MAX_EVENT_INDEX
; i
++) {
2586 ret
= e_snprintf(buf
, len
, "%s_%d", nbase
, i
);
2588 pr_debug("snprintf() failed: %d\n", ret
);
2591 if (!strlist__has_entry(namelist
, buf
))
2594 if (i
== MAX_EVENT_INDEX
) {
2595 pr_warning("Too many events are on the same function.\n");
2604 /* Warn if the current kernel's uprobe implementation is old */
2605 static void warn_uprobe_event_compat(struct probe_trace_event
*tev
)
2608 char *buf
= synthesize_probe_trace_command(tev
);
2610 /* Old uprobe event doesn't support memory dereference */
2611 if (!tev
->uprobes
|| tev
->nargs
== 0 || !buf
)
2614 for (i
= 0; i
< tev
->nargs
; i
++)
2615 if (strglobmatch(tev
->args
[i
].value
, "[$@+-]*")) {
2616 pr_warning("Please upgrade your kernel to at least "
2617 "3.14 to have access to feature %s\n",
2618 tev
->args
[i
].value
);
2625 /* Set new name from original perf_probe_event and namelist */
2626 static int probe_trace_event__set_name(struct probe_trace_event
*tev
,
2627 struct perf_probe_event
*pev
,
2628 struct strlist
*namelist
,
2631 const char *event
, *group
;
2635 /* If probe_event or trace_event already have the name, reuse it */
2636 if (pev
->event
&& !pev
->sdt
)
2638 else if (tev
->event
)
2641 /* Or generate new one from probe point */
2642 if (pev
->point
.function
&&
2643 (strncmp(pev
->point
.function
, "0x", 2) != 0) &&
2644 !strisglob(pev
->point
.function
))
2645 event
= pev
->point
.function
;
2647 event
= tev
->point
.realname
;
2649 if (pev
->group
&& !pev
->sdt
)
2651 else if (tev
->group
)
2654 group
= PERFPROBE_GROUP
;
2656 /* Get an unused new event name */
2657 ret
= get_new_event_name(buf
, 64, event
,
2658 namelist
, allow_suffix
);
2664 tev
->event
= strdup(event
);
2665 tev
->group
= strdup(group
);
2666 if (tev
->event
== NULL
|| tev
->group
== NULL
)
2669 /* Add added event name to namelist */
2670 strlist__add(namelist
, event
);
2674 static int __open_probe_file_and_namelist(bool uprobe
,
2675 struct strlist
**namelist
)
2679 fd
= probe_file__open(PF_FL_RW
| (uprobe
? PF_FL_UPROBE
: 0));
2683 /* Get current event names */
2684 *namelist
= probe_file__get_namelist(fd
);
2686 pr_debug("Failed to get current event list.\n");
2693 static int __add_probe_trace_events(struct perf_probe_event
*pev
,
2694 struct probe_trace_event
*tevs
,
2695 int ntevs
, bool allow_suffix
)
2697 int i
, fd
[2] = {-1, -1}, up
, ret
;
2698 struct probe_trace_event
*tev
= NULL
;
2699 struct probe_cache
*cache
= NULL
;
2700 struct strlist
*namelist
[2] = {NULL
, NULL
};
2702 up
= pev
->uprobes
? 1 : 0;
2703 fd
[up
] = __open_probe_file_and_namelist(up
, &namelist
[up
]);
2708 for (i
= 0; i
< ntevs
; i
++) {
2710 up
= tev
->uprobes
? 1 : 0;
2711 if (fd
[up
] == -1) { /* Open the kprobe/uprobe_events */
2712 fd
[up
] = __open_probe_file_and_namelist(up
,
2717 /* Skip if the symbol is out of .text or blacklisted */
2718 if (!tev
->point
.symbol
&& !pev
->uprobes
)
2721 /* Set new name for tev (and update namelist) */
2722 ret
= probe_trace_event__set_name(tev
, pev
, namelist
[up
],
2727 ret
= probe_file__add_event(fd
[up
], tev
);
2732 * Probes after the first probe which comes from same
2733 * user input are always allowed to add suffix, because
2734 * there might be several addresses corresponding to
2737 allow_suffix
= true;
2739 if (ret
== -EINVAL
&& pev
->uprobes
)
2740 warn_uprobe_event_compat(tev
);
2741 if (ret
== 0 && probe_conf
.cache
) {
2742 cache
= probe_cache__new(pev
->target
);
2744 probe_cache__add_entry(cache
, pev
, tevs
, ntevs
) < 0 ||
2745 probe_cache__commit(cache
) < 0)
2746 pr_warning("Failed to add event to probe cache\n");
2747 probe_cache__delete(cache
);
2751 for (up
= 0; up
< 2; up
++) {
2752 strlist__delete(namelist
[up
]);
2759 static int find_probe_functions(struct map
*map
, char *name
,
2760 struct symbol
**syms
)
2764 struct rb_node
*tmp
;
2766 if (map__load(map
) < 0)
2769 map__for_each_symbol(map
, sym
, tmp
) {
2770 if (strglobmatch(sym
->name
, name
)) {
2772 if (syms
&& found
< probe_conf
.max_probes
)
2773 syms
[found
- 1] = sym
;
2780 void __weak
arch__fix_tev_from_maps(struct perf_probe_event
*pev __maybe_unused
,
2781 struct probe_trace_event
*tev __maybe_unused
,
2782 struct map
*map __maybe_unused
,
2783 struct symbol
*sym __maybe_unused
) { }
2786 * Find probe function addresses from map.
2787 * Return an error or the number of found probe_trace_event
2789 static int find_probe_trace_events_from_map(struct perf_probe_event
*pev
,
2790 struct probe_trace_event
**tevs
)
2792 struct map
*map
= NULL
;
2793 struct ref_reloc_sym
*reloc_sym
= NULL
;
2795 struct symbol
**syms
= NULL
;
2796 struct probe_trace_event
*tev
;
2797 struct perf_probe_point
*pp
= &pev
->point
;
2798 struct probe_trace_point
*tp
;
2799 int num_matched_functions
;
2800 int ret
, i
, j
, skipped
= 0;
2803 map
= get_target_map(pev
->target
, pev
->uprobes
);
2809 syms
= malloc(sizeof(struct symbol
*) * probe_conf
.max_probes
);
2816 * Load matched symbols: Since the different local symbols may have
2817 * same name but different addresses, this lists all the symbols.
2819 num_matched_functions
= find_probe_functions(map
, pp
->function
, syms
);
2820 if (num_matched_functions
== 0) {
2821 pr_err("Failed to find symbol %s in %s\n", pp
->function
,
2822 pev
->target
? : "kernel");
2825 } else if (num_matched_functions
> probe_conf
.max_probes
) {
2826 pr_err("Too many functions matched in %s\n",
2827 pev
->target
? : "kernel");
2832 /* Note that the symbols in the kmodule are not relocated */
2833 if (!pev
->uprobes
&& !pev
->target
&&
2834 (!pp
->retprobe
|| kretprobe_offset_is_supported())) {
2835 reloc_sym
= kernel_get_ref_reloc_sym();
2837 pr_warning("Relocated base symbol is not found!\n");
2843 /* Setup result trace-probe-events */
2844 *tevs
= zalloc(sizeof(*tev
) * num_matched_functions
);
2852 for (j
= 0; j
< num_matched_functions
; j
++) {
2855 tev
= (*tevs
) + ret
;
2857 if (ret
== num_matched_functions
) {
2858 pr_warning("Too many symbols are listed. Skip it.\n");
2863 if (pp
->offset
> sym
->end
- sym
->start
) {
2864 pr_warning("Offset %ld is bigger than the size of %s\n",
2865 pp
->offset
, sym
->name
);
2869 /* Add one probe point */
2870 tp
->address
= map
->unmap_ip(map
, sym
->start
) + pp
->offset
;
2872 /* Check the kprobe (not in module) is within .text */
2873 if (!pev
->uprobes
&& !pev
->target
&&
2874 kprobe_warn_out_range(sym
->name
, tp
->address
)) {
2875 tp
->symbol
= NULL
; /* Skip it */
2877 } else if (reloc_sym
) {
2878 tp
->symbol
= strdup_or_goto(reloc_sym
->name
, nomem_out
);
2879 tp
->offset
= tp
->address
- reloc_sym
->addr
;
2881 tp
->symbol
= strdup_or_goto(sym
->name
, nomem_out
);
2882 tp
->offset
= pp
->offset
;
2884 tp
->realname
= strdup_or_goto(sym
->name
, nomem_out
);
2886 tp
->retprobe
= pp
->retprobe
;
2889 tev
->point
.module
= strdup_or_goto(pev
->target
,
2892 mod_name
= find_module_name(pev
->target
);
2894 strdup(mod_name
? mod_name
: pev
->target
);
2896 if (!tev
->point
.module
)
2900 tev
->uprobes
= pev
->uprobes
;
2901 tev
->nargs
= pev
->nargs
;
2903 tev
->args
= zalloc(sizeof(struct probe_trace_arg
) *
2905 if (tev
->args
== NULL
)
2908 for (i
= 0; i
< tev
->nargs
; i
++) {
2909 if (pev
->args
[i
].name
)
2911 strdup_or_goto(pev
->args
[i
].name
,
2914 tev
->args
[i
].value
= strdup_or_goto(pev
->args
[i
].var
,
2916 if (pev
->args
[i
].type
)
2918 strdup_or_goto(pev
->args
[i
].type
,
2921 arch__fix_tev_from_maps(pev
, tev
, map
, sym
);
2923 if (ret
== skipped
) {
2936 clear_probe_trace_events(*tevs
, num_matched_functions
);
2941 static int try_to_find_absolute_address(struct perf_probe_event
*pev
,
2942 struct probe_trace_event
**tevs
)
2944 struct perf_probe_point
*pp
= &pev
->point
;
2945 struct probe_trace_event
*tev
;
2946 struct probe_trace_point
*tp
;
2949 if (!(pev
->point
.function
&& !strncmp(pev
->point
.function
, "0x", 2)))
2951 if (perf_probe_event_need_dwarf(pev
))
2955 * This is 'perf probe /lib/libc.so 0xabcd'. Try to probe at
2958 * Only one tev can be generated by this.
2960 *tevs
= zalloc(sizeof(*tev
));
2968 * Don't use tp->offset, use address directly, because
2969 * in synthesize_probe_trace_command() address cannot be
2972 tp
->address
= pev
->point
.abs_address
;
2973 tp
->retprobe
= pp
->retprobe
;
2974 tev
->uprobes
= pev
->uprobes
;
2978 * Give it a '0x' leading symbol name.
2979 * In __add_probe_trace_events, a NULL symbol is interpreted as
2982 if (asprintf(&tp
->symbol
, "0x%lx", tp
->address
) < 0)
2985 /* For kprobe, check range */
2986 if ((!tev
->uprobes
) &&
2987 (kprobe_warn_out_range(tev
->point
.symbol
,
2988 tev
->point
.address
))) {
2993 if (asprintf(&tp
->realname
, "abs_%lx", tp
->address
) < 0)
2997 tp
->module
= strdup(pev
->target
);
3003 tev
->group
= strdup(pev
->group
);
3009 tev
->event
= strdup(pev
->event
);
3014 tev
->nargs
= pev
->nargs
;
3015 tev
->args
= zalloc(sizeof(struct probe_trace_arg
) * tev
->nargs
);
3019 for (i
= 0; i
< tev
->nargs
; i
++)
3020 copy_to_probe_trace_arg(&tev
->args
[i
], &pev
->args
[i
]);
3025 clear_probe_trace_events(*tevs
, 1);
3030 /* Concatinate two arrays */
3031 static void *memcat(void *a
, size_t sz_a
, void *b
, size_t sz_b
)
3035 ret
= malloc(sz_a
+ sz_b
);
3037 memcpy(ret
, a
, sz_a
);
3038 memcpy(ret
+ sz_a
, b
, sz_b
);
3044 concat_probe_trace_events(struct probe_trace_event
**tevs
, int *ntevs
,
3045 struct probe_trace_event
**tevs2
, int ntevs2
)
3047 struct probe_trace_event
*new_tevs
;
3057 if (*ntevs
+ ntevs2
> probe_conf
.max_probes
)
3060 /* Concatinate the array of probe_trace_event */
3061 new_tevs
= memcat(*tevs
, (*ntevs
) * sizeof(**tevs
),
3062 *tevs2
, ntevs2
* sizeof(**tevs2
));
3072 clear_probe_trace_events(*tevs2
, ntevs2
);
3079 * Try to find probe_trace_event from given probe caches. Return the number
3080 * of cached events found, if an error occurs return the error.
3082 static int find_cached_events(struct perf_probe_event
*pev
,
3083 struct probe_trace_event
**tevs
,
3086 struct probe_cache
*cache
;
3087 struct probe_cache_entry
*entry
;
3088 struct probe_trace_event
*tmp_tevs
= NULL
;
3092 cache
= probe_cache__new(target
);
3093 /* Return 0 ("not found") if the target has no probe cache. */
3097 for_each_probe_cache_entry(entry
, cache
) {
3098 /* Skip the cache entry which has no name */
3099 if (!entry
->pev
.event
|| !entry
->pev
.group
)
3101 if ((!pev
->group
|| strglobmatch(entry
->pev
.group
, pev
->group
)) &&
3102 strglobmatch(entry
->pev
.event
, pev
->event
)) {
3103 ret
= probe_cache_entry__get_event(entry
, &tmp_tevs
);
3105 ret
= concat_probe_trace_events(tevs
, &ntevs
,
3111 probe_cache__delete(cache
);
3113 clear_probe_trace_events(*tevs
, ntevs
);
3117 if (ntevs
> 0 && target
&& target
[0] == '/')
3118 pev
->uprobes
= true;
3124 /* Try to find probe_trace_event from all probe caches */
3125 static int find_cached_events_all(struct perf_probe_event
*pev
,
3126 struct probe_trace_event
**tevs
)
3128 struct probe_trace_event
*tmp_tevs
= NULL
;
3129 struct strlist
*bidlist
;
3130 struct str_node
*nd
;
3135 /* Get the buildid list of all valid caches */
3136 bidlist
= build_id_cache__list_all(true);
3139 pr_debug("Failed to get buildids: %d\n", ret
);
3144 strlist__for_each_entry(nd
, bidlist
) {
3145 pathname
= build_id_cache__origname(nd
->s
);
3146 ret
= find_cached_events(pev
, &tmp_tevs
, pathname
);
3147 /* In the case of cnt == 0, we just skip it */
3149 ret
= concat_probe_trace_events(tevs
, &ntevs
,
3155 strlist__delete(bidlist
);
3158 clear_probe_trace_events(*tevs
, ntevs
);
3166 static int find_probe_trace_events_from_cache(struct perf_probe_event
*pev
,
3167 struct probe_trace_event
**tevs
)
3169 struct probe_cache
*cache
;
3170 struct probe_cache_entry
*entry
;
3171 struct probe_trace_event
*tev
;
3172 struct str_node
*node
;
3176 /* For SDT/cached events, we use special search functions */
3178 return find_cached_events_all(pev
, tevs
);
3180 return find_cached_events(pev
, tevs
, pev
->target
);
3182 cache
= probe_cache__new(pev
->target
);
3186 entry
= probe_cache__find(cache
, pev
);
3188 /* SDT must be in the cache */
3189 ret
= pev
->sdt
? -ENOENT
: 0;
3193 ret
= strlist__nr_entries(entry
->tevlist
);
3194 if (ret
> probe_conf
.max_probes
) {
3195 pr_debug("Too many entries matched in the cache of %s\n",
3196 pev
->target
? : "kernel");
3201 *tevs
= zalloc(ret
* sizeof(*tev
));
3208 strlist__for_each_entry(node
, entry
->tevlist
) {
3209 tev
= &(*tevs
)[i
++];
3210 ret
= parse_probe_trace_command(node
->s
, tev
);
3213 /* Set the uprobes attribute as same as original */
3214 tev
->uprobes
= pev
->uprobes
;
3219 probe_cache__delete(cache
);
3223 static int convert_to_probe_trace_events(struct perf_probe_event
*pev
,
3224 struct probe_trace_event
**tevs
)
3228 if (!pev
->group
&& !pev
->sdt
) {
3229 /* Set group name if not given */
3230 if (!pev
->uprobes
) {
3231 pev
->group
= strdup(PERFPROBE_GROUP
);
3232 ret
= pev
->group
? 0 : -ENOMEM
;
3234 ret
= convert_exec_to_group(pev
->target
, &pev
->group
);
3236 pr_warning("Failed to make a group name.\n");
3241 ret
= try_to_find_absolute_address(pev
, tevs
);
3245 /* At first, we need to lookup cache entry */
3246 ret
= find_probe_trace_events_from_cache(pev
, tevs
);
3247 if (ret
> 0 || pev
->sdt
) /* SDT can be found only in the cache */
3248 return ret
== 0 ? -ENOENT
: ret
; /* Found in probe cache */
3250 /* Convert perf_probe_event with debuginfo */
3251 ret
= try_to_find_probe_trace_events(pev
, tevs
);
3253 return ret
; /* Found in debuginfo or got an error */
3255 return find_probe_trace_events_from_map(pev
, tevs
);
3258 int convert_perf_probe_events(struct perf_probe_event
*pevs
, int npevs
)
3262 /* Loop 1: convert all events */
3263 for (i
= 0; i
< npevs
; i
++) {
3264 /* Init kprobe blacklist if needed */
3265 if (!pevs
[i
].uprobes
)
3266 kprobe_blacklist__init();
3267 /* Convert with or without debuginfo */
3268 ret
= convert_to_probe_trace_events(&pevs
[i
], &pevs
[i
].tevs
);
3271 pevs
[i
].ntevs
= ret
;
3273 /* This just release blacklist only if allocated */
3274 kprobe_blacklist__release();
3279 static int show_probe_trace_event(struct probe_trace_event
*tev
)
3281 char *buf
= synthesize_probe_trace_command(tev
);
3284 pr_debug("Failed to synthesize probe trace event.\n");
3288 /* Showing definition always go stdout */
3289 printf("%s\n", buf
);
3295 int show_probe_trace_events(struct perf_probe_event
*pevs
, int npevs
)
3297 struct strlist
*namelist
= strlist__new(NULL
, NULL
);
3298 struct probe_trace_event
*tev
;
3299 struct perf_probe_event
*pev
;
3305 for (j
= 0; j
< npevs
&& !ret
; j
++) {
3307 for (i
= 0; i
< pev
->ntevs
&& !ret
; i
++) {
3308 tev
= &pev
->tevs
[i
];
3309 /* Skip if the symbol is out of .text or blacklisted */
3310 if (!tev
->point
.symbol
&& !pev
->uprobes
)
3313 /* Set new name for tev (and update namelist) */
3314 ret
= probe_trace_event__set_name(tev
, pev
,
3317 ret
= show_probe_trace_event(tev
);
3320 strlist__delete(namelist
);
3325 int apply_perf_probe_events(struct perf_probe_event
*pevs
, int npevs
)
3329 /* Loop 2: add all events */
3330 for (i
= 0; i
< npevs
; i
++) {
3331 ret
= __add_probe_trace_events(&pevs
[i
], pevs
[i
].tevs
,
3333 probe_conf
.force_add
);
3340 void cleanup_perf_probe_events(struct perf_probe_event
*pevs
, int npevs
)
3344 /* Loop 3: cleanup and free trace events */
3345 for (i
= 0; i
< npevs
; i
++) {
3346 for (j
= 0; j
< pevs
[i
].ntevs
; j
++)
3347 clear_probe_trace_event(&pevs
[i
].tevs
[j
]);
3348 zfree(&pevs
[i
].tevs
);
3350 clear_perf_probe_event(&pevs
[i
]);
3354 int add_perf_probe_events(struct perf_probe_event
*pevs
, int npevs
)
3358 ret
= init_probe_symbol_maps(pevs
->uprobes
);
3362 ret
= convert_perf_probe_events(pevs
, npevs
);
3364 ret
= apply_perf_probe_events(pevs
, npevs
);
3366 cleanup_perf_probe_events(pevs
, npevs
);
3368 exit_probe_symbol_maps();
3372 int del_perf_probe_events(struct strfilter
*filter
)
3374 int ret
, ret2
, ufd
= -1, kfd
= -1;
3375 char *str
= strfilter__string(filter
);
3380 /* Get current event names */
3381 ret
= probe_file__open_both(&kfd
, &ufd
, PF_FL_RW
);
3385 ret
= probe_file__del_events(kfd
, filter
);
3386 if (ret
< 0 && ret
!= -ENOENT
)
3389 ret2
= probe_file__del_events(ufd
, filter
);
3390 if (ret2
< 0 && ret2
!= -ENOENT
) {
3407 int show_available_funcs(const char *target
, struct strfilter
*_filter
,
3414 ret
= init_probe_symbol_maps(user
);
3418 /* Get a symbol map */
3419 map
= get_target_map(target
, user
);
3421 pr_err("Failed to get a map for %s\n", (target
) ? : "kernel");
3425 ret
= map__load(map
);
3428 char *str
= strfilter__string(_filter
);
3429 pr_err("Failed to find symbols matched to \"%s\"\n",
3433 pr_err("Failed to load symbols in %s\n",
3434 (target
) ? : "kernel");
3437 if (!dso__sorted_by_name(map
->dso
, map
->type
))
3438 dso__sort_by_name(map
->dso
, map
->type
);
3440 /* Show all (filtered) symbols */
3443 for (nd
= rb_first(&map
->dso
->symbol_names
[map
->type
]); nd
; nd
= rb_next(nd
)) {
3444 struct symbol_name_rb_node
*pos
= rb_entry(nd
, struct symbol_name_rb_node
, rb_node
);
3446 if (strfilter__compare(_filter
, pos
->sym
.name
))
3447 printf("%s\n", pos
->sym
.name
);
3452 exit_probe_symbol_maps();
3457 int copy_to_probe_trace_arg(struct probe_trace_arg
*tvar
,
3458 struct perf_probe_arg
*pvar
)
3460 tvar
->value
= strdup(pvar
->var
);
3461 if (tvar
->value
== NULL
)
3464 tvar
->type
= strdup(pvar
->type
);
3465 if (tvar
->type
== NULL
)
3469 tvar
->name
= strdup(pvar
->name
);
3470 if (tvar
->name
== NULL
)