]>
git.proxmox.com Git - mirror_ubuntu-artful-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
);
175 for (pos
= maps__first(maps
); pos
; pos
= map__next(pos
)) {
176 /* short_name is "[module]" */
177 if (strncmp(pos
->dso
->short_name
+ 1, module
,
178 pos
->dso
->short_name_len
- 2) == 0 &&
179 module
[pos
->dso
->short_name_len
- 2] == '\0') {
187 struct map
*get_target_map(const char *target
, bool user
)
189 /* Init maps of given executable or kernel */
191 return dso__new_map(target
);
193 return kernel_get_module_map(target
);
196 static int convert_exec_to_group(const char *exec
, char **result
)
198 char *ptr1
, *ptr2
, *exec_copy
;
202 exec_copy
= strdup(exec
);
206 ptr1
= basename(exec_copy
);
212 for (ptr2
= ptr1
; *ptr2
!= '\0'; ptr2
++) {
213 if (!isalnum(*ptr2
) && *ptr2
!= '_') {
219 ret
= e_snprintf(buf
, 64, "%s_%s", PERFPROBE_GROUP
, ptr1
);
223 *result
= strdup(buf
);
224 ret
= *result
? 0 : -ENOMEM
;
231 static void clear_perf_probe_point(struct perf_probe_point
*pp
)
238 static void clear_probe_trace_events(struct probe_trace_event
*tevs
, int ntevs
)
242 for (i
= 0; i
< ntevs
; i
++)
243 clear_probe_trace_event(tevs
+ i
);
246 static bool kprobe_blacklist__listed(unsigned long address
);
247 static bool kprobe_warn_out_range(const char *symbol
, unsigned long address
)
252 /* Get the address of _etext for checking non-probable text symbol */
253 ret
= kernel_get_symbol_address_by_name("_etext", &etext_addr
,
256 if (ret
== 0 && etext_addr
< address
)
257 pr_warning("%s is out of .text, skip it.\n", symbol
);
258 else if (kprobe_blacklist__listed(address
))
259 pr_warning("%s is blacklisted function, skip it.\n", symbol
);
267 * @module can be module name of module file path. In case of path,
268 * inspect elf and find out what is actual module name.
269 * Caller has to free mod_name after using it.
271 static char *find_module_name(const char *module
)
279 char *mod_name
= NULL
;
282 fd
= open(module
, O_RDONLY
);
286 elf
= elf_begin(fd
, PERF_ELF_C_READ_MMAP
, NULL
);
290 if (gelf_getehdr(elf
, &ehdr
) == NULL
)
293 sec
= elf_section_by_name(elf
, &ehdr
, &shdr
,
294 ".gnu.linkonce.this_module", NULL
);
298 data
= elf_getdata(sec
, NULL
);
299 if (!data
|| !data
->d_buf
)
304 * '.gnu.linkonce.this_module' section of kernel module elf directly
305 * maps to 'struct module' from linux/module.h. This section contains
306 * actual module name which will be used by kernel after loading it.
307 * But, we cannot use 'struct module' here since linux/module.h is not
308 * exposed to user-space. Offset of 'name' has remained same from long
309 * time, so hardcoding it here.
311 if (ehdr
.e_ident
[EI_CLASS
] == ELFCLASS32
)
313 else /* expect ELFCLASS64 by default */
316 mod_name
= strdup((char *)data
->d_buf
+ name_offset
);
325 #ifdef HAVE_DWARF_SUPPORT
327 static int kernel_get_module_dso(const char *module
, struct dso
**pdso
)
331 const char *vmlinux_name
;
335 char module_name
[128];
337 snprintf(module_name
, sizeof(module_name
), "[%s]", module
);
338 map
= map_groups__find_by_name(&host_machine
->kmaps
, MAP__FUNCTION
, module_name
);
343 pr_debug("Failed to find module %s.\n", module
);
347 map
= machine__kernel_map(host_machine
);
350 vmlinux_name
= symbol_conf
.vmlinux_name
;
353 ret
= dso__load_vmlinux(dso
, map
, vmlinux_name
, false);
355 ret
= dso__load_vmlinux_path(dso
, map
);
362 * Some binaries like glibc have special symbols which are on the symbol
363 * table, but not in the debuginfo. If we can find the address of the
364 * symbol from map, we can translate the address back to the probe point.
366 static int find_alternative_probe_point(struct debuginfo
*dinfo
,
367 struct perf_probe_point
*pp
,
368 struct perf_probe_point
*result
,
369 const char *target
, bool uprobes
)
371 struct map
*map
= NULL
;
376 /* This can work only for function-name based one */
377 if (!pp
->function
|| pp
->file
)
380 map
= get_target_map(target
, uprobes
);
384 /* Find the address of given function */
385 map__for_each_symbol_by_name(map
, pp
->function
, sym
) {
387 address
= sym
->start
;
389 address
= map
->unmap_ip(map
, sym
->start
) - map
->reloc
;
396 pr_debug("Symbol %s address found : %" PRIx64
"\n",
397 pp
->function
, address
);
399 ret
= debuginfo__find_probe_point(dinfo
, (unsigned long)address
,
402 ret
= (!ret
) ? -ENOENT
: ret
;
404 result
->offset
+= pp
->offset
;
405 result
->line
+= pp
->line
;
406 result
->retprobe
= pp
->retprobe
;
416 static int get_alternative_probe_event(struct debuginfo
*dinfo
,
417 struct perf_probe_event
*pev
,
418 struct perf_probe_point
*tmp
)
422 memcpy(tmp
, &pev
->point
, sizeof(*tmp
));
423 memset(&pev
->point
, 0, sizeof(pev
->point
));
424 ret
= find_alternative_probe_point(dinfo
, tmp
, &pev
->point
,
425 pev
->target
, pev
->uprobes
);
427 memcpy(&pev
->point
, tmp
, sizeof(*tmp
));
432 static int get_alternative_line_range(struct debuginfo
*dinfo
,
433 struct line_range
*lr
,
434 const char *target
, bool user
)
436 struct perf_probe_point pp
= { .function
= lr
->function
,
439 struct perf_probe_point result
;
442 memset(&result
, 0, sizeof(result
));
444 if (lr
->end
!= INT_MAX
)
445 len
= lr
->end
- lr
->start
;
446 ret
= find_alternative_probe_point(dinfo
, &pp
, &result
,
449 lr
->function
= result
.function
;
450 lr
->file
= result
.file
;
451 lr
->start
= result
.line
;
452 if (lr
->end
!= INT_MAX
)
453 lr
->end
= lr
->start
+ len
;
454 clear_perf_probe_point(&pp
);
459 /* Open new debuginfo of given module */
460 static struct debuginfo
*open_debuginfo(const char *module
, bool silent
)
462 const char *path
= module
;
463 char reason
[STRERR_BUFSIZE
];
464 struct debuginfo
*ret
= NULL
;
465 struct dso
*dso
= NULL
;
468 if (!module
|| !strchr(module
, '/')) {
469 err
= kernel_get_module_dso(module
, &dso
);
471 if (!dso
|| dso
->load_errno
== 0) {
472 if (!str_error_r(-err
, reason
, STRERR_BUFSIZE
))
473 strcpy(reason
, "(unknown)");
475 dso__strerror_load(dso
, reason
, STRERR_BUFSIZE
);
477 pr_err("Failed to find the path for %s: %s\n",
478 module
?: "kernel", reason
);
481 path
= dso
->long_name
;
483 ret
= debuginfo__new(path
);
484 if (!ret
&& !silent
) {
485 pr_warning("The %s file has no debug information.\n", path
);
486 if (!module
|| !strtailcmp(path
, ".ko"))
487 pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, ");
489 pr_warning("Rebuild with -g, ");
490 pr_warning("or install an appropriate debuginfo package.\n");
495 /* For caching the last debuginfo */
496 static struct debuginfo
*debuginfo_cache
;
497 static char *debuginfo_cache_path
;
499 static struct debuginfo
*debuginfo_cache__open(const char *module
, bool silent
)
501 const char *path
= module
;
503 /* If the module is NULL, it should be the kernel. */
507 if (debuginfo_cache_path
&& !strcmp(debuginfo_cache_path
, path
))
510 /* Copy module path */
511 free(debuginfo_cache_path
);
512 debuginfo_cache_path
= strdup(path
);
513 if (!debuginfo_cache_path
) {
514 debuginfo__delete(debuginfo_cache
);
515 debuginfo_cache
= NULL
;
519 debuginfo_cache
= open_debuginfo(module
, silent
);
520 if (!debuginfo_cache
)
521 zfree(&debuginfo_cache_path
);
523 return debuginfo_cache
;
526 static void debuginfo_cache__exit(void)
528 debuginfo__delete(debuginfo_cache
);
529 debuginfo_cache
= NULL
;
530 zfree(&debuginfo_cache_path
);
534 static int get_text_start_address(const char *exec
, unsigned long *address
)
539 int fd
, ret
= -ENOENT
;
541 fd
= open(exec
, O_RDONLY
);
545 elf
= elf_begin(fd
, PERF_ELF_C_READ_MMAP
, NULL
);
551 if (gelf_getehdr(elf
, &ehdr
) == NULL
)
554 if (!elf_section_by_name(elf
, &ehdr
, &shdr
, ".text", NULL
))
557 *address
= shdr
.sh_addr
- shdr
.sh_offset
;
568 * Convert trace point to probe point with debuginfo
570 static int find_perf_probe_point_from_dwarf(struct probe_trace_point
*tp
,
571 struct perf_probe_point
*pp
,
574 struct debuginfo
*dinfo
= NULL
;
575 unsigned long stext
= 0;
576 u64 addr
= tp
->address
;
579 /* convert the address to dwarf address */
585 ret
= get_text_start_address(tp
->module
, &stext
);
589 } else if (tp
->symbol
) {
590 /* If the module is given, this returns relative address */
591 ret
= kernel_get_symbol_address_by_name(tp
->symbol
, &addr
,
592 false, !!tp
->module
);
598 pr_debug("try to find information at %" PRIx64
" in %s\n", addr
,
599 tp
->module
? : "kernel");
601 dinfo
= debuginfo_cache__open(tp
->module
, verbose
<= 0);
603 ret
= debuginfo__find_probe_point(dinfo
,
604 (unsigned long)addr
, pp
);
609 pp
->retprobe
= tp
->retprobe
;
613 pr_debug("Failed to find corresponding probes from debuginfo.\n");
614 return ret
? : -ENOENT
;
617 /* Adjust symbol name and address */
618 static int post_process_probe_trace_point(struct probe_trace_point
*tp
,
619 struct map
*map
, unsigned long offs
)
622 u64 addr
= tp
->address
- offs
;
624 sym
= map__find_symbol(map
, addr
);
628 if (strcmp(sym
->name
, tp
->symbol
)) {
629 /* If we have no realname, use symbol for it */
631 tp
->realname
= tp
->symbol
;
634 tp
->symbol
= strdup(sym
->name
);
638 tp
->offset
= addr
- sym
->start
;
645 * Rename DWARF symbols to ELF symbols -- gcc sometimes optimizes functions
646 * and generate new symbols with suffixes such as .constprop.N or .isra.N
647 * etc. Since those symbols are not recorded in DWARF, we have to find
648 * correct generated symbols from offline ELF binary.
649 * For online kernel or uprobes we don't need this because those are
650 * rebased on _text, or already a section relative address.
653 post_process_offline_probe_trace_events(struct probe_trace_event
*tevs
,
654 int ntevs
, const char *pathname
)
657 unsigned long stext
= 0;
660 /* Prepare a map for offline binary */
661 map
= dso__new_map(pathname
);
662 if (!map
|| get_text_start_address(pathname
, &stext
) < 0) {
663 pr_warning("Failed to get ELF symbols for %s\n", pathname
);
667 for (i
= 0; i
< ntevs
; i
++) {
668 ret
= post_process_probe_trace_point(&tevs
[i
].point
,
678 static int add_exec_to_probe_trace_events(struct probe_trace_event
*tevs
,
679 int ntevs
, const char *exec
)
682 unsigned long stext
= 0;
687 ret
= get_text_start_address(exec
, &stext
);
691 for (i
= 0; i
< ntevs
&& ret
>= 0; i
++) {
692 /* point.address is the addres of point.symbol + point.offset */
693 tevs
[i
].point
.address
-= stext
;
694 tevs
[i
].point
.module
= strdup(exec
);
695 if (!tevs
[i
].point
.module
) {
699 tevs
[i
].uprobes
= true;
706 post_process_module_probe_trace_events(struct probe_trace_event
*tevs
,
707 int ntevs
, const char *module
,
708 struct debuginfo
*dinfo
)
710 Dwarf_Addr text_offs
= 0;
712 char *mod_name
= NULL
;
718 map
= get_target_map(module
, false);
719 if (!map
|| debuginfo__get_text_offset(dinfo
, &text_offs
, true) < 0) {
720 pr_warning("Failed to get ELF symbols for %s\n", module
);
724 mod_name
= find_module_name(module
);
725 for (i
= 0; i
< ntevs
; i
++) {
726 ret
= post_process_probe_trace_point(&tevs
[i
].point
,
727 map
, (unsigned long)text_offs
);
730 tevs
[i
].point
.module
=
731 strdup(mod_name
? mod_name
: module
);
732 if (!tevs
[i
].point
.module
) {
745 post_process_kernel_probe_trace_events(struct probe_trace_event
*tevs
,
748 struct ref_reloc_sym
*reloc_sym
;
752 /* Skip post process if the target is an offline kernel */
753 if (symbol_conf
.ignore_vmlinux_buildid
)
754 return post_process_offline_probe_trace_events(tevs
, ntevs
,
755 symbol_conf
.vmlinux_name
);
757 reloc_sym
= kernel_get_ref_reloc_sym();
759 pr_warning("Relocated base symbol is not found!\n");
763 for (i
= 0; i
< ntevs
; i
++) {
764 if (!tevs
[i
].point
.address
)
766 if (tevs
[i
].point
.retprobe
&& !kretprobe_offset_is_supported())
768 /* If we found a wrong one, mark it by NULL symbol */
769 if (kprobe_warn_out_range(tevs
[i
].point
.symbol
,
770 tevs
[i
].point
.address
)) {
774 tmp
= strdup(reloc_sym
->name
);
778 /* If we have no realname, use symbol for it */
779 if (!tevs
[i
].point
.realname
)
780 tevs
[i
].point
.realname
= tevs
[i
].point
.symbol
;
782 free(tevs
[i
].point
.symbol
);
783 tevs
[i
].point
.symbol
= tmp
;
784 tevs
[i
].point
.offset
= tevs
[i
].point
.address
-
785 reloc_sym
->unrelocated_addr
;
791 arch__post_process_probe_trace_events(struct perf_probe_event
*pev __maybe_unused
,
792 int ntevs __maybe_unused
)
796 /* Post processing the probe events */
797 static int post_process_probe_trace_events(struct perf_probe_event
*pev
,
798 struct probe_trace_event
*tevs
,
799 int ntevs
, const char *module
,
800 bool uprobe
, struct debuginfo
*dinfo
)
805 ret
= add_exec_to_probe_trace_events(tevs
, ntevs
, module
);
807 /* Currently ref_reloc_sym based probe is not for drivers */
808 ret
= post_process_module_probe_trace_events(tevs
, ntevs
,
811 ret
= post_process_kernel_probe_trace_events(tevs
, ntevs
);
814 arch__post_process_probe_trace_events(pev
, ntevs
);
819 /* Try to find perf_probe_event with debuginfo */
820 static int try_to_find_probe_trace_events(struct perf_probe_event
*pev
,
821 struct probe_trace_event
**tevs
)
823 bool need_dwarf
= perf_probe_event_need_dwarf(pev
);
824 struct perf_probe_point tmp
;
825 struct debuginfo
*dinfo
;
828 dinfo
= open_debuginfo(pev
->target
, !need_dwarf
);
832 pr_debug("Could not open debuginfo. Try to use symbols.\n");
836 pr_debug("Try to find probe point from debuginfo.\n");
837 /* Searching trace events corresponding to a probe event */
838 ntevs
= debuginfo__find_trace_events(dinfo
, pev
, tevs
);
840 if (ntevs
== 0) { /* Not found, retry with an alternative */
841 ret
= get_alternative_probe_event(dinfo
, pev
, &tmp
);
843 ntevs
= debuginfo__find_trace_events(dinfo
, pev
, tevs
);
845 * Write back to the original probe_event for
846 * setting appropriate (user given) event name
848 clear_perf_probe_point(&pev
->point
);
849 memcpy(&pev
->point
, &tmp
, sizeof(tmp
));
853 if (ntevs
> 0) { /* Succeeded to find trace events */
854 pr_debug("Found %d probe_trace_events.\n", ntevs
);
855 ret
= post_process_probe_trace_events(pev
, *tevs
, ntevs
,
856 pev
->target
, pev
->uprobes
, dinfo
);
857 if (ret
< 0 || ret
== ntevs
) {
858 pr_debug("Post processing failed or all events are skipped. (%d)\n", ret
);
859 clear_probe_trace_events(*tevs
, ntevs
);
865 debuginfo__delete(dinfo
);
867 if (ntevs
== 0) { /* No error but failed to find probe point. */
868 pr_warning("Probe point '%s' not found.\n",
869 synthesize_perf_probe_point(&pev
->point
));
871 } else if (ntevs
< 0) {
872 /* Error path : ntevs < 0 */
873 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs
);
875 pr_warning("Warning: No dwarf info found in the vmlinux - "
876 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
878 pr_debug("Trying to use symbols.\n");
885 #define LINEBUF_SIZE 256
886 #define NR_ADDITIONAL_LINES 2
888 static int __show_one_line(FILE *fp
, int l
, bool skip
, bool show_num
)
890 char buf
[LINEBUF_SIZE
], sbuf
[STRERR_BUFSIZE
];
891 const char *color
= show_num
? "" : PERF_COLOR_BLUE
;
892 const char *prefix
= NULL
;
895 if (fgets(buf
, LINEBUF_SIZE
, fp
) == NULL
)
900 prefix
= show_num
? "%7d " : " ";
901 color_fprintf(stdout
, color
, prefix
, l
);
903 color_fprintf(stdout
, color
, "%s", buf
);
905 } while (strchr(buf
, '\n') == NULL
);
910 pr_warning("File read error: %s\n",
911 str_error_r(errno
, sbuf
, sizeof(sbuf
)));
917 static int _show_one_line(FILE *fp
, int l
, bool skip
, bool show_num
)
919 int rv
= __show_one_line(fp
, l
, skip
, show_num
);
921 pr_warning("Source file is shorter than expected.\n");
927 #define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
928 #define show_one_line(f,l) _show_one_line(f,l,false,false)
929 #define skip_one_line(f,l) _show_one_line(f,l,true,false)
930 #define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
933 * Show line-range always requires debuginfo to find source file and
936 static int __show_line_range(struct line_range
*lr
, const char *module
,
941 struct debuginfo
*dinfo
;
945 char sbuf
[STRERR_BUFSIZE
];
947 /* Search a line range */
948 dinfo
= open_debuginfo(module
, false);
952 ret
= debuginfo__find_line_range(dinfo
, lr
);
953 if (!ret
) { /* Not found, retry with an alternative */
954 ret
= get_alternative_line_range(dinfo
, lr
, module
, user
);
956 ret
= debuginfo__find_line_range(dinfo
, lr
);
958 debuginfo__delete(dinfo
);
959 if (ret
== 0 || ret
== -ENOENT
) {
960 pr_warning("Specified source line is not found.\n");
962 } else if (ret
< 0) {
963 pr_warning("Debuginfo analysis failed.\n");
967 /* Convert source file path */
969 ret
= get_real_path(tmp
, lr
->comp_dir
, &lr
->path
);
971 /* Free old path when new path is assigned */
976 pr_warning("Failed to find source file path.\n");
983 fprintf(stdout
, "<%s@%s:%d>\n", lr
->function
, lr
->path
,
984 lr
->start
- lr
->offset
);
986 fprintf(stdout
, "<%s:%d>\n", lr
->path
, lr
->start
);
988 fp
= fopen(lr
->path
, "r");
990 pr_warning("Failed to open %s: %s\n", lr
->path
,
991 str_error_r(errno
, sbuf
, sizeof(sbuf
)));
994 /* Skip to starting line number */
995 while (l
< lr
->start
) {
996 ret
= skip_one_line(fp
, l
++);
1001 intlist__for_each_entry(ln
, lr
->line_list
) {
1002 for (; ln
->i
> l
; l
++) {
1003 ret
= show_one_line(fp
, l
- lr
->offset
);
1007 ret
= show_one_line_with_num(fp
, l
++ - lr
->offset
);
1012 if (lr
->end
== INT_MAX
)
1013 lr
->end
= l
+ NR_ADDITIONAL_LINES
;
1014 while (l
<= lr
->end
) {
1015 ret
= show_one_line_or_eof(fp
, l
++ - lr
->offset
);
1024 int show_line_range(struct line_range
*lr
, const char *module
, bool user
)
1028 ret
= init_probe_symbol_maps(user
);
1031 ret
= __show_line_range(lr
, module
, user
);
1032 exit_probe_symbol_maps();
1037 static int show_available_vars_at(struct debuginfo
*dinfo
,
1038 struct perf_probe_event
*pev
,
1039 struct strfilter
*_filter
)
1043 struct str_node
*node
;
1044 struct variable_list
*vls
= NULL
, *vl
;
1045 struct perf_probe_point tmp
;
1048 buf
= synthesize_perf_probe_point(&pev
->point
);
1051 pr_debug("Searching variables at %s\n", buf
);
1053 ret
= debuginfo__find_available_vars_at(dinfo
, pev
, &vls
);
1054 if (!ret
) { /* Not found, retry with an alternative */
1055 ret
= get_alternative_probe_event(dinfo
, pev
, &tmp
);
1057 ret
= debuginfo__find_available_vars_at(dinfo
, pev
,
1059 /* Release the old probe_point */
1060 clear_perf_probe_point(&tmp
);
1064 if (ret
== 0 || ret
== -ENOENT
) {
1065 pr_err("Failed to find the address of %s\n", buf
);
1068 pr_warning("Debuginfo analysis failed.\n");
1072 /* Some variables are found */
1073 fprintf(stdout
, "Available variables at %s\n", buf
);
1074 for (i
= 0; i
< ret
; i
++) {
1077 * A probe point might be converted to
1078 * several trace points.
1080 fprintf(stdout
, "\t@<%s+%lu>\n", vl
->point
.symbol
,
1082 zfree(&vl
->point
.symbol
);
1085 strlist__for_each_entry(node
, vl
->vars
) {
1086 var
= strchr(node
->s
, '\t') + 1;
1087 if (strfilter__compare(_filter
, var
)) {
1088 fprintf(stdout
, "\t\t%s\n", node
->s
);
1092 strlist__delete(vl
->vars
);
1095 fprintf(stdout
, "\t\t(No matched variables)\n");
1103 /* Show available variables on given probe point */
1104 int show_available_vars(struct perf_probe_event
*pevs
, int npevs
,
1105 struct strfilter
*_filter
)
1108 struct debuginfo
*dinfo
;
1110 ret
= init_probe_symbol_maps(pevs
->uprobes
);
1114 dinfo
= open_debuginfo(pevs
->target
, false);
1122 for (i
= 0; i
< npevs
&& ret
>= 0; i
++)
1123 ret
= show_available_vars_at(dinfo
, &pevs
[i
], _filter
);
1125 debuginfo__delete(dinfo
);
1127 exit_probe_symbol_maps();
1131 #else /* !HAVE_DWARF_SUPPORT */
1133 static void debuginfo_cache__exit(void)
1138 find_perf_probe_point_from_dwarf(struct probe_trace_point
*tp __maybe_unused
,
1139 struct perf_probe_point
*pp __maybe_unused
,
1140 bool is_kprobe __maybe_unused
)
1145 static int try_to_find_probe_trace_events(struct perf_probe_event
*pev
,
1146 struct probe_trace_event
**tevs __maybe_unused
)
1148 if (perf_probe_event_need_dwarf(pev
)) {
1149 pr_warning("Debuginfo-analysis is not supported.\n");
1156 int show_line_range(struct line_range
*lr __maybe_unused
,
1157 const char *module __maybe_unused
,
1158 bool user __maybe_unused
)
1160 pr_warning("Debuginfo-analysis is not supported.\n");
1164 int show_available_vars(struct perf_probe_event
*pevs __maybe_unused
,
1165 int npevs __maybe_unused
,
1166 struct strfilter
*filter __maybe_unused
)
1168 pr_warning("Debuginfo-analysis is not supported.\n");
1173 void line_range__clear(struct line_range
*lr
)
1179 intlist__delete(lr
->line_list
);
1180 memset(lr
, 0, sizeof(*lr
));
1183 int line_range__init(struct line_range
*lr
)
1185 memset(lr
, 0, sizeof(*lr
));
1186 lr
->line_list
= intlist__new(NULL
);
1193 static int parse_line_num(char **ptr
, int *val
, const char *what
)
1195 const char *start
= *ptr
;
1198 *val
= strtol(*ptr
, ptr
, 0);
1199 if (errno
|| *ptr
== start
) {
1200 semantic_error("'%s' is not a valid number.\n", what
);
1206 /* Check the name is good for event, group or function */
1207 static bool is_c_func_name(const char *name
)
1209 if (!isalpha(*name
) && *name
!= '_')
1211 while (*++name
!= '\0') {
1212 if (!isalpha(*name
) && !isdigit(*name
) && *name
!= '_')
1219 * Stuff 'lr' according to the line range described by 'arg'.
1220 * The line range syntax is described by:
1222 * SRC[:SLN[+NUM|-ELN]]
1223 * FNC[@SRC][:SLN[+NUM|-ELN]]
1225 int parse_line_range_desc(const char *arg
, struct line_range
*lr
)
1227 char *range
, *file
, *name
= strdup(arg
);
1236 range
= strchr(name
, ':');
1240 err
= parse_line_num(&range
, &lr
->start
, "start line");
1244 if (*range
== '+' || *range
== '-') {
1245 const char c
= *range
++;
1247 err
= parse_line_num(&range
, &lr
->end
, "end line");
1252 lr
->end
+= lr
->start
;
1254 * Adjust the number of lines here.
1255 * If the number of lines == 1, the
1256 * the end of line should be equal to
1257 * the start of line.
1263 pr_debug("Line range is %d to %d\n", lr
->start
, lr
->end
);
1266 if (lr
->start
> lr
->end
) {
1267 semantic_error("Start line must be smaller"
1268 " than end line.\n");
1271 if (*range
!= '\0') {
1272 semantic_error("Tailing with invalid str '%s'.\n", range
);
1277 file
= strchr(name
, '@');
1280 lr
->file
= strdup(++file
);
1281 if (lr
->file
== NULL
) {
1285 lr
->function
= name
;
1286 } else if (strchr(name
, '/') || strchr(name
, '.'))
1288 else if (is_c_func_name(name
))/* We reuse it for checking funcname */
1289 lr
->function
= name
;
1290 else { /* Invalid name */
1291 semantic_error("'%s' is not a valid function name.\n", name
);
1302 static int parse_perf_probe_event_name(char **arg
, struct perf_probe_event
*pev
)
1306 ptr
= strchr(*arg
, ':');
1309 if (!pev
->sdt
&& !is_c_func_name(*arg
))
1311 pev
->group
= strdup(*arg
);
1317 if (!pev
->sdt
&& !is_c_func_name(*arg
)) {
1319 semantic_error("%s is bad for event name -it must "
1320 "follow C symbol-naming rule.\n", *arg
);
1323 pev
->event
= strdup(*arg
);
1324 if (pev
->event
== NULL
)
1330 /* Parse probepoint definition. */
1331 static int parse_perf_probe_point(char *arg
, struct perf_probe_event
*pev
)
1333 struct perf_probe_point
*pp
= &pev
->point
;
1336 bool file_spec
= false;
1341 * perf probe [GRP:][EVENT=]SRC[:LN|;PTN]
1342 * perf probe [GRP:][EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
1343 * perf probe %[GRP:]SDT_EVENT
1348 if (is_sdt_event(arg
)) {
1354 ptr
= strpbrk(arg
, ";=@+%");
1358 semantic_error("%s must be an SDT name.\n",
1362 /* This must be a target file name or build id */
1363 tmp
= build_id_cache__complement(ptr
+ 1);
1365 pev
->target
= build_id_cache__origname(tmp
);
1368 pev
->target
= strdup(ptr
+ 1);
1373 ret
= parse_perf_probe_event_name(&arg
, pev
);
1375 if (asprintf(&pev
->point
.function
, "%%%s", pev
->event
) < 0)
1381 if (ptr
&& *ptr
== '=') { /* Event name */
1384 ret
= parse_perf_probe_event_name(&arg
, pev
);
1392 * Check arg is function or file name and copy it.
1394 * We consider arg to be a file spec if and only if it satisfies
1395 * all of the below criteria::
1396 * - it does not include any of "+@%",
1397 * - it includes one of ":;", and
1398 * - it has a period '.' in the name.
1400 * Otherwise, we consider arg to be a function specification.
1402 if (!strpbrk(arg
, "+@%") && (ptr
= strpbrk(arg
, ";:")) != NULL
) {
1403 /* This is a file spec if it includes a '.' before ; or : */
1404 if (memchr(arg
, '.', ptr
- arg
))
1408 ptr
= strpbrk(arg
, ";:+@%");
1428 * Keep pp->function even if this is absolute address,
1429 * so it can mark whether abs_address is valid.
1430 * Which make 'perf probe lib.bin 0x0' possible.
1432 * Note that checking length of tmp is not needed
1433 * because when we access tmp[1] we know tmp[0] is '0',
1434 * so tmp[1] should always valid (but could be '\0').
1436 if (tmp
&& !strncmp(tmp
, "0x", 2)) {
1437 pp
->abs_address
= strtoul(pp
->function
, &tmp
, 0);
1439 semantic_error("Invalid absolute address.\n");
1445 /* Parse other options */
1449 if (c
== ';') { /* Lazy pattern must be the last part */
1450 pp
->lazy_line
= strdup(arg
);
1451 if (pp
->lazy_line
== NULL
)
1455 ptr
= strpbrk(arg
, ";:+@%");
1461 case ':': /* Line number */
1462 pp
->line
= strtoul(arg
, &tmp
, 0);
1464 semantic_error("There is non-digit char"
1465 " in line number.\n");
1469 case '+': /* Byte offset from a symbol */
1470 pp
->offset
= strtoul(arg
, &tmp
, 0);
1472 semantic_error("There is non-digit character"
1477 case '@': /* File name */
1479 semantic_error("SRC@SRC is not allowed.\n");
1482 pp
->file
= strdup(arg
);
1483 if (pp
->file
== NULL
)
1486 case '%': /* Probe places */
1487 if (strcmp(arg
, "return") == 0) {
1489 } else { /* Others not supported yet */
1490 semantic_error("%%%s is not supported.\n", arg
);
1494 default: /* Buggy case */
1495 pr_err("This program has a bug at %s:%d.\n",
1496 __FILE__
, __LINE__
);
1502 /* Exclusion check */
1503 if (pp
->lazy_line
&& pp
->line
) {
1504 semantic_error("Lazy pattern can't be used with"
1509 if (pp
->lazy_line
&& pp
->offset
) {
1510 semantic_error("Lazy pattern can't be used with offset.\n");
1514 if (pp
->line
&& pp
->offset
) {
1515 semantic_error("Offset can't be used with line number.\n");
1519 if (!pp
->line
&& !pp
->lazy_line
&& pp
->file
&& !pp
->function
) {
1520 semantic_error("File always requires line number or "
1525 if (pp
->offset
&& !pp
->function
) {
1526 semantic_error("Offset requires an entry function.\n");
1530 if ((pp
->offset
|| pp
->line
|| pp
->lazy_line
) && pp
->retprobe
) {
1531 semantic_error("Offset/Line/Lazy pattern can't be used with "
1536 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
1537 pp
->function
, pp
->file
, pp
->line
, pp
->offset
, pp
->retprobe
,
1542 /* Parse perf-probe event argument */
1543 static int parse_perf_probe_arg(char *str
, struct perf_probe_arg
*arg
)
1545 char *tmp
, *goodname
;
1546 struct perf_probe_arg_field
**fieldp
;
1548 pr_debug("parsing arg: %s into ", str
);
1550 tmp
= strchr(str
, '=');
1552 arg
->name
= strndup(str
, tmp
- str
);
1553 if (arg
->name
== NULL
)
1555 pr_debug("name:%s ", arg
->name
);
1559 tmp
= strchr(str
, ':');
1560 if (tmp
) { /* Type setting */
1562 arg
->type
= strdup(tmp
+ 1);
1563 if (arg
->type
== NULL
)
1565 pr_debug("type:%s ", arg
->type
);
1568 tmp
= strpbrk(str
, "-.[");
1569 if (!is_c_varname(str
) || !tmp
) {
1570 /* A variable, register, symbol or special value */
1571 arg
->var
= strdup(str
);
1572 if (arg
->var
== NULL
)
1574 pr_debug("%s\n", arg
->var
);
1578 /* Structure fields or array element */
1579 arg
->var
= strndup(str
, tmp
- str
);
1580 if (arg
->var
== NULL
)
1582 goodname
= arg
->var
;
1583 pr_debug("%s, ", arg
->var
);
1584 fieldp
= &arg
->field
;
1587 *fieldp
= zalloc(sizeof(struct perf_probe_arg_field
));
1588 if (*fieldp
== NULL
)
1590 if (*tmp
== '[') { /* Array */
1592 (*fieldp
)->index
= strtol(str
+ 1, &tmp
, 0);
1593 (*fieldp
)->ref
= true;
1594 if (*tmp
!= ']' || tmp
== str
+ 1) {
1595 semantic_error("Array index must be a"
1602 } else { /* Structure */
1605 (*fieldp
)->ref
= false;
1606 } else if (tmp
[1] == '>') {
1608 (*fieldp
)->ref
= true;
1610 semantic_error("Argument parse error: %s\n",
1614 tmp
= strpbrk(str
, "-.[");
1617 (*fieldp
)->name
= strndup(str
, tmp
- str
);
1618 if ((*fieldp
)->name
== NULL
)
1621 goodname
= (*fieldp
)->name
;
1622 pr_debug("%s(%d), ", (*fieldp
)->name
, (*fieldp
)->ref
);
1623 fieldp
= &(*fieldp
)->next
;
1626 (*fieldp
)->name
= strdup(str
);
1627 if ((*fieldp
)->name
== NULL
)
1630 goodname
= (*fieldp
)->name
;
1631 pr_debug("%s(%d)\n", (*fieldp
)->name
, (*fieldp
)->ref
);
1633 /* If no name is specified, set the last field name (not array index)*/
1635 arg
->name
= strdup(goodname
);
1636 if (arg
->name
== NULL
)
1642 /* Parse perf-probe event command */
1643 int parse_perf_probe_command(const char *cmd
, struct perf_probe_event
*pev
)
1646 int argc
, i
, ret
= 0;
1648 argv
= argv_split(cmd
, &argc
);
1650 pr_debug("Failed to split arguments.\n");
1653 if (argc
- 1 > MAX_PROBE_ARGS
) {
1654 semantic_error("Too many probe arguments (%d).\n", argc
- 1);
1658 /* Parse probe point */
1659 ret
= parse_perf_probe_point(argv
[0], pev
);
1663 /* Copy arguments and ensure return probe has no C argument */
1664 pev
->nargs
= argc
- 1;
1665 pev
->args
= zalloc(sizeof(struct perf_probe_arg
) * pev
->nargs
);
1666 if (pev
->args
== NULL
) {
1670 for (i
= 0; i
< pev
->nargs
&& ret
>= 0; i
++) {
1671 ret
= parse_perf_probe_arg(argv
[i
+ 1], &pev
->args
[i
]);
1673 is_c_varname(pev
->args
[i
].var
) && pev
->point
.retprobe
) {
1674 semantic_error("You can't specify local variable for"
1685 /* Returns true if *any* ARG is either C variable, $params or $vars. */
1686 bool perf_probe_with_var(struct perf_probe_event
*pev
)
1690 for (i
= 0; i
< pev
->nargs
; i
++)
1691 if (is_c_varname(pev
->args
[i
].var
) ||
1692 !strcmp(pev
->args
[i
].var
, PROBE_ARG_PARAMS
) ||
1693 !strcmp(pev
->args
[i
].var
, PROBE_ARG_VARS
))
1698 /* Return true if this perf_probe_event requires debuginfo */
1699 bool perf_probe_event_need_dwarf(struct perf_probe_event
*pev
)
1701 if (pev
->point
.file
|| pev
->point
.line
|| pev
->point
.lazy_line
)
1704 if (perf_probe_with_var(pev
))
1710 /* Parse probe_events event into struct probe_point */
1711 int parse_probe_trace_command(const char *cmd
, struct probe_trace_event
*tev
)
1713 struct probe_trace_point
*tp
= &tev
->point
;
1716 char *argv0_str
= NULL
, *fmt
, *fmt1_str
, *fmt2_str
, *fmt3_str
;
1720 pr_debug("Parsing probe_events: %s\n", cmd
);
1721 argv
= argv_split(cmd
, &argc
);
1723 pr_debug("Failed to split arguments.\n");
1727 semantic_error("Too few probe arguments.\n");
1732 /* Scan event and group name. */
1733 argv0_str
= strdup(argv
[0]);
1734 if (argv0_str
== NULL
) {
1738 fmt1_str
= strtok_r(argv0_str
, ":", &fmt
);
1739 fmt2_str
= strtok_r(NULL
, "/", &fmt
);
1740 fmt3_str
= strtok_r(NULL
, " \t", &fmt
);
1741 if (fmt1_str
== NULL
|| strlen(fmt1_str
) != 1 || fmt2_str
== NULL
1742 || fmt3_str
== NULL
) {
1743 semantic_error("Failed to parse event name: %s\n", argv
[0]);
1748 tev
->group
= strdup(fmt2_str
);
1749 tev
->event
= strdup(fmt3_str
);
1750 if (tev
->group
== NULL
|| tev
->event
== NULL
) {
1754 pr_debug("Group:%s Event:%s probe:%c\n", tev
->group
, tev
->event
, pr
);
1756 tp
->retprobe
= (pr
== 'r');
1758 /* Scan module name(if there), function name and offset */
1759 p
= strchr(argv
[1], ':');
1761 tp
->module
= strndup(argv
[1], p
- argv
[1]);
1766 tev
->uprobes
= (tp
->module
[0] == '/');
1770 fmt1_str
= strtok_r(p
, "+", &fmt
);
1771 /* only the address started with 0x */
1772 if (fmt1_str
[0] == '0') {
1774 * Fix a special case:
1775 * if address == 0, kernel reports something like:
1776 * p:probe_libc/abs_0 /lib/libc-2.18.so:0x (null) arg1=%ax
1777 * Newer kernel may fix that, but we want to
1778 * support old kernel also.
1780 if (strcmp(fmt1_str
, "0x") == 0) {
1781 if (!argv
[2] || strcmp(argv
[2], "(null)")) {
1788 for (i
= 2; argv
[i
+ 1] != NULL
; i
++)
1789 argv
[i
] = argv
[i
+ 1];
1794 tp
->address
= strtoul(fmt1_str
, NULL
, 0);
1796 /* Only the symbol-based probe has offset */
1797 tp
->symbol
= strdup(fmt1_str
);
1798 if (tp
->symbol
== NULL
) {
1802 fmt2_str
= strtok_r(NULL
, "", &fmt
);
1803 if (fmt2_str
== NULL
)
1806 tp
->offset
= strtoul(fmt2_str
, NULL
, 10);
1809 tev
->nargs
= argc
- 2;
1810 tev
->args
= zalloc(sizeof(struct probe_trace_arg
) * tev
->nargs
);
1811 if (tev
->args
== NULL
) {
1815 for (i
= 0; i
< tev
->nargs
; i
++) {
1816 p
= strchr(argv
[i
+ 2], '=');
1817 if (p
) /* We don't need which register is assigned. */
1821 tev
->args
[i
].name
= strdup(argv
[i
+ 2]);
1822 /* TODO: parse regs and offset */
1823 tev
->args
[i
].value
= strdup(p
);
1824 if (tev
->args
[i
].name
== NULL
|| tev
->args
[i
].value
== NULL
) {
1836 /* Compose only probe arg */
1837 char *synthesize_perf_probe_arg(struct perf_probe_arg
*pa
)
1839 struct perf_probe_arg_field
*field
= pa
->field
;
1844 if (strbuf_init(&buf
, 64) < 0)
1847 if (pa
->name
&& pa
->var
)
1848 err
= strbuf_addf(&buf
, "%s=%s", pa
->name
, pa
->var
);
1850 err
= strbuf_addstr(&buf
, pa
->name
?: pa
->var
);
1855 if (field
->name
[0] == '[')
1856 err
= strbuf_addstr(&buf
, field
->name
);
1858 err
= strbuf_addf(&buf
, "%s%s", field
->ref
? "->" : ".",
1860 field
= field
->next
;
1866 if (strbuf_addf(&buf
, ":%s", pa
->type
) < 0)
1869 ret
= strbuf_detach(&buf
, NULL
);
1871 strbuf_release(&buf
);
1875 /* Compose only probe point (not argument) */
1876 char *synthesize_perf_probe_point(struct perf_probe_point
*pp
)
1879 char *tmp
, *ret
= NULL
;
1882 if (strbuf_init(&buf
, 64) < 0)
1886 if (strbuf_addstr(&buf
, pp
->function
) < 0)
1889 err
= strbuf_addf(&buf
, "+%lu", pp
->offset
);
1891 err
= strbuf_addf(&buf
, ":%d", pp
->line
);
1892 else if (pp
->retprobe
)
1893 err
= strbuf_addstr(&buf
, "%return");
1901 tmp
= strchr(pp
->file
+ len
- 30, '/');
1902 tmp
= tmp
? tmp
+ 1 : pp
->file
+ len
- 30;
1904 err
= strbuf_addf(&buf
, "@%s", tmp
);
1905 if (!err
&& !pp
->function
&& pp
->line
)
1906 err
= strbuf_addf(&buf
, ":%d", pp
->line
);
1909 ret
= strbuf_detach(&buf
, NULL
);
1911 strbuf_release(&buf
);
1915 char *synthesize_perf_probe_command(struct perf_probe_event
*pev
)
1918 char *tmp
, *ret
= NULL
;
1921 if (strbuf_init(&buf
, 64))
1924 if (strbuf_addf(&buf
, "%s:%s=", pev
->group
?: PERFPROBE_GROUP
,
1928 tmp
= synthesize_perf_probe_point(&pev
->point
);
1929 if (!tmp
|| strbuf_addstr(&buf
, tmp
) < 0)
1933 for (i
= 0; i
< pev
->nargs
; i
++) {
1934 tmp
= synthesize_perf_probe_arg(pev
->args
+ i
);
1935 if (!tmp
|| strbuf_addf(&buf
, " %s", tmp
) < 0)
1940 ret
= strbuf_detach(&buf
, NULL
);
1942 strbuf_release(&buf
);
1946 static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref
*ref
,
1947 struct strbuf
*buf
, int depth
)
1951 depth
= __synthesize_probe_trace_arg_ref(ref
->next
, buf
,
1956 err
= strbuf_addf(buf
, "%+ld(", ref
->offset
);
1957 return (err
< 0) ? err
: depth
;
1960 static int synthesize_probe_trace_arg(struct probe_trace_arg
*arg
,
1963 struct probe_trace_arg_ref
*ref
= arg
->ref
;
1966 /* Argument name or separator */
1968 err
= strbuf_addf(buf
, " %s=", arg
->name
);
1970 err
= strbuf_addch(buf
, ' ');
1974 /* Special case: @XXX */
1975 if (arg
->value
[0] == '@' && arg
->ref
)
1978 /* Dereferencing arguments */
1980 depth
= __synthesize_probe_trace_arg_ref(ref
, buf
, 1);
1985 /* Print argument value */
1986 if (arg
->value
[0] == '@' && arg
->ref
)
1987 err
= strbuf_addf(buf
, "%s%+ld", arg
->value
, arg
->ref
->offset
);
1989 err
= strbuf_addstr(buf
, arg
->value
);
1992 while (!err
&& depth
--)
1993 err
= strbuf_addch(buf
, ')');
1995 /* Print argument type */
1996 if (!err
&& arg
->type
)
1997 err
= strbuf_addf(buf
, ":%s", arg
->type
);
2002 char *synthesize_probe_trace_command(struct probe_trace_event
*tev
)
2004 struct probe_trace_point
*tp
= &tev
->point
;
2009 /* Uprobes must have tp->module */
2010 if (tev
->uprobes
&& !tp
->module
)
2013 if (strbuf_init(&buf
, 32) < 0)
2016 if (strbuf_addf(&buf
, "%c:%s/%s ", tp
->retprobe
? 'r' : 'p',
2017 tev
->group
, tev
->event
) < 0)
2020 * If tp->address == 0, then this point must be a
2021 * absolute address uprobe.
2022 * try_to_find_absolute_address() should have made
2023 * tp->symbol to "0x0".
2025 if (tev
->uprobes
&& !tp
->address
) {
2026 if (!tp
->symbol
|| strcmp(tp
->symbol
, "0x0"))
2030 /* Use the tp->address for uprobes */
2032 err
= strbuf_addf(&buf
, "%s:0x%lx", tp
->module
, tp
->address
);
2033 else if (!strncmp(tp
->symbol
, "0x", 2))
2034 /* Absolute address. See try_to_find_absolute_address() */
2035 err
= strbuf_addf(&buf
, "%s%s0x%lx", tp
->module
?: "",
2036 tp
->module
? ":" : "", tp
->address
);
2038 err
= strbuf_addf(&buf
, "%s%s%s+%lu", tp
->module
?: "",
2039 tp
->module
? ":" : "", tp
->symbol
, tp
->offset
);
2043 for (i
= 0; i
< tev
->nargs
; i
++)
2044 if (synthesize_probe_trace_arg(&tev
->args
[i
], &buf
) < 0)
2047 ret
= strbuf_detach(&buf
, NULL
);
2049 strbuf_release(&buf
);
2053 static int find_perf_probe_point_from_map(struct probe_trace_point
*tp
,
2054 struct perf_probe_point
*pp
,
2057 struct symbol
*sym
= NULL
;
2058 struct map
*map
= NULL
;
2059 u64 addr
= tp
->address
;
2063 map
= dso__new_map(tp
->module
);
2066 sym
= map__find_symbol(map
, addr
);
2068 if (tp
->symbol
&& !addr
) {
2069 if (kernel_get_symbol_address_by_name(tp
->symbol
,
2070 &addr
, true, false) < 0)
2075 sym
= __find_kernel_function(addr
, &map
);
2082 pp
->retprobe
= tp
->retprobe
;
2083 pp
->offset
= addr
- map
->unmap_ip(map
, sym
->start
);
2084 pp
->function
= strdup(sym
->name
);
2085 ret
= pp
->function
? 0 : -ENOMEM
;
2088 if (map
&& !is_kprobe
) {
2095 static int convert_to_perf_probe_point(struct probe_trace_point
*tp
,
2096 struct perf_probe_point
*pp
,
2102 ret
= find_perf_probe_point_from_dwarf(tp
, pp
, is_kprobe
);
2105 ret
= find_perf_probe_point_from_map(tp
, pp
, is_kprobe
);
2109 pr_debug("Failed to find probe point from both of dwarf and map.\n");
2112 pp
->function
= strdup(tp
->symbol
);
2113 pp
->offset
= tp
->offset
;
2115 ret
= e_snprintf(buf
, 128, "0x%" PRIx64
, (u64
)tp
->address
);
2118 pp
->function
= strdup(buf
);
2121 if (pp
->function
== NULL
)
2124 pp
->retprobe
= tp
->retprobe
;
2129 static int convert_to_perf_probe_event(struct probe_trace_event
*tev
,
2130 struct perf_probe_event
*pev
, bool is_kprobe
)
2132 struct strbuf buf
= STRBUF_INIT
;
2135 /* Convert event/group name */
2136 pev
->event
= strdup(tev
->event
);
2137 pev
->group
= strdup(tev
->group
);
2138 if (pev
->event
== NULL
|| pev
->group
== NULL
)
2141 /* Convert trace_point to probe_point */
2142 ret
= convert_to_perf_probe_point(&tev
->point
, &pev
->point
, is_kprobe
);
2146 /* Convert trace_arg to probe_arg */
2147 pev
->nargs
= tev
->nargs
;
2148 pev
->args
= zalloc(sizeof(struct perf_probe_arg
) * pev
->nargs
);
2149 if (pev
->args
== NULL
)
2151 for (i
= 0; i
< tev
->nargs
&& ret
>= 0; i
++) {
2152 if (tev
->args
[i
].name
)
2153 pev
->args
[i
].name
= strdup(tev
->args
[i
].name
);
2155 if ((ret
= strbuf_init(&buf
, 32)) < 0)
2157 ret
= synthesize_probe_trace_arg(&tev
->args
[i
], &buf
);
2158 pev
->args
[i
].name
= strbuf_detach(&buf
, NULL
);
2160 if (pev
->args
[i
].name
== NULL
&& ret
>= 0)
2165 clear_perf_probe_event(pev
);
2170 void clear_perf_probe_event(struct perf_probe_event
*pev
)
2172 struct perf_probe_arg_field
*field
, *next
;
2178 clear_perf_probe_point(&pev
->point
);
2180 for (i
= 0; i
< pev
->nargs
; i
++) {
2181 free(pev
->args
[i
].name
);
2182 free(pev
->args
[i
].var
);
2183 free(pev
->args
[i
].type
);
2184 field
= pev
->args
[i
].field
;
2187 zfree(&field
->name
);
2193 memset(pev
, 0, sizeof(*pev
));
2196 #define strdup_or_goto(str, label) \
2197 ({ char *__p = NULL; if (str && !(__p = strdup(str))) goto label; __p; })
2199 static int perf_probe_point__copy(struct perf_probe_point
*dst
,
2200 struct perf_probe_point
*src
)
2202 dst
->file
= strdup_or_goto(src
->file
, out_err
);
2203 dst
->function
= strdup_or_goto(src
->function
, out_err
);
2204 dst
->lazy_line
= strdup_or_goto(src
->lazy_line
, out_err
);
2205 dst
->line
= src
->line
;
2206 dst
->retprobe
= src
->retprobe
;
2207 dst
->offset
= src
->offset
;
2211 clear_perf_probe_point(dst
);
2215 static int perf_probe_arg__copy(struct perf_probe_arg
*dst
,
2216 struct perf_probe_arg
*src
)
2218 struct perf_probe_arg_field
*field
, **ppfield
;
2220 dst
->name
= strdup_or_goto(src
->name
, out_err
);
2221 dst
->var
= strdup_or_goto(src
->var
, out_err
);
2222 dst
->type
= strdup_or_goto(src
->type
, out_err
);
2225 ppfield
= &(dst
->field
);
2227 *ppfield
= zalloc(sizeof(*field
));
2230 (*ppfield
)->name
= strdup_or_goto(field
->name
, out_err
);
2231 (*ppfield
)->index
= field
->index
;
2232 (*ppfield
)->ref
= field
->ref
;
2233 field
= field
->next
;
2234 ppfield
= &((*ppfield
)->next
);
2241 int perf_probe_event__copy(struct perf_probe_event
*dst
,
2242 struct perf_probe_event
*src
)
2246 dst
->event
= strdup_or_goto(src
->event
, out_err
);
2247 dst
->group
= strdup_or_goto(src
->group
, out_err
);
2248 dst
->target
= strdup_or_goto(src
->target
, out_err
);
2249 dst
->uprobes
= src
->uprobes
;
2251 if (perf_probe_point__copy(&dst
->point
, &src
->point
) < 0)
2254 dst
->args
= zalloc(sizeof(struct perf_probe_arg
) * src
->nargs
);
2257 dst
->nargs
= src
->nargs
;
2259 for (i
= 0; i
< src
->nargs
; i
++)
2260 if (perf_probe_arg__copy(&dst
->args
[i
], &src
->args
[i
]) < 0)
2265 clear_perf_probe_event(dst
);
2269 void clear_probe_trace_event(struct probe_trace_event
*tev
)
2271 struct probe_trace_arg_ref
*ref
, *next
;
2276 free(tev
->point
.symbol
);
2277 free(tev
->point
.realname
);
2278 free(tev
->point
.module
);
2279 for (i
= 0; i
< tev
->nargs
; i
++) {
2280 free(tev
->args
[i
].name
);
2281 free(tev
->args
[i
].value
);
2282 free(tev
->args
[i
].type
);
2283 ref
= tev
->args
[i
].ref
;
2291 memset(tev
, 0, sizeof(*tev
));
2294 struct kprobe_blacklist_node
{
2295 struct list_head list
;
2296 unsigned long start
;
2301 static void kprobe_blacklist__delete(struct list_head
*blacklist
)
2303 struct kprobe_blacklist_node
*node
;
2305 while (!list_empty(blacklist
)) {
2306 node
= list_first_entry(blacklist
,
2307 struct kprobe_blacklist_node
, list
);
2308 list_del(&node
->list
);
2314 static int kprobe_blacklist__load(struct list_head
*blacklist
)
2316 struct kprobe_blacklist_node
*node
;
2317 const char *__debugfs
= debugfs__mountpoint();
2318 char buf
[PATH_MAX
], *p
;
2322 if (__debugfs
== NULL
)
2325 ret
= e_snprintf(buf
, PATH_MAX
, "%s/kprobes/blacklist", __debugfs
);
2329 fp
= fopen(buf
, "r");
2334 while (fgets(buf
, PATH_MAX
, fp
)) {
2335 node
= zalloc(sizeof(*node
));
2340 INIT_LIST_HEAD(&node
->list
);
2341 list_add_tail(&node
->list
, blacklist
);
2342 if (sscanf(buf
, "0x%lx-0x%lx", &node
->start
, &node
->end
) != 2) {
2346 p
= strchr(buf
, '\t');
2349 if (p
[strlen(p
) - 1] == '\n')
2350 p
[strlen(p
) - 1] = '\0';
2352 p
= (char *)"unknown";
2353 node
->symbol
= strdup(p
);
2354 if (!node
->symbol
) {
2358 pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
2359 node
->start
, node
->end
, node
->symbol
);
2363 kprobe_blacklist__delete(blacklist
);
2369 static struct kprobe_blacklist_node
*
2370 kprobe_blacklist__find_by_address(struct list_head
*blacklist
,
2371 unsigned long address
)
2373 struct kprobe_blacklist_node
*node
;
2375 list_for_each_entry(node
, blacklist
, list
) {
2376 if (node
->start
<= address
&& address
<= node
->end
)
2383 static LIST_HEAD(kprobe_blacklist
);
2385 static void kprobe_blacklist__init(void)
2387 if (!list_empty(&kprobe_blacklist
))
2390 if (kprobe_blacklist__load(&kprobe_blacklist
) < 0)
2391 pr_debug("No kprobe blacklist support, ignored\n");
2394 static void kprobe_blacklist__release(void)
2396 kprobe_blacklist__delete(&kprobe_blacklist
);
2399 static bool kprobe_blacklist__listed(unsigned long address
)
2401 return !!kprobe_blacklist__find_by_address(&kprobe_blacklist
, address
);
2404 static int perf_probe_event__sprintf(const char *group
, const char *event
,
2405 struct perf_probe_event
*pev
,
2407 struct strbuf
*result
)
2412 if (asprintf(&buf
, "%s:%s", group
, event
) < 0)
2414 ret
= strbuf_addf(result
, " %-20s (on ", buf
);
2419 /* Synthesize only event probe point */
2420 buf
= synthesize_perf_probe_point(&pev
->point
);
2423 ret
= strbuf_addstr(result
, buf
);
2427 ret
= strbuf_addf(result
, " in %s", module
);
2429 if (!ret
&& pev
->nargs
> 0) {
2430 ret
= strbuf_add(result
, " with", 5);
2431 for (i
= 0; !ret
&& i
< pev
->nargs
; i
++) {
2432 buf
= synthesize_perf_probe_arg(&pev
->args
[i
]);
2435 ret
= strbuf_addf(result
, " %s", buf
);
2440 ret
= strbuf_addch(result
, ')');
2446 int show_perf_probe_event(const char *group
, const char *event
,
2447 struct perf_probe_event
*pev
,
2448 const char *module
, bool use_stdout
)
2450 struct strbuf buf
= STRBUF_INIT
;
2453 ret
= perf_probe_event__sprintf(group
, event
, pev
, module
, &buf
);
2456 printf("%s\n", buf
.buf
);
2458 pr_info("%s\n", buf
.buf
);
2460 strbuf_release(&buf
);
2465 static bool filter_probe_trace_event(struct probe_trace_event
*tev
,
2466 struct strfilter
*filter
)
2470 /* At first, check the event name itself */
2471 if (strfilter__compare(filter
, tev
->event
))
2474 /* Next, check the combination of name and group */
2475 if (e_snprintf(tmp
, 128, "%s:%s", tev
->group
, tev
->event
) < 0)
2477 return strfilter__compare(filter
, tmp
);
2480 static int __show_perf_probe_events(int fd
, bool is_kprobe
,
2481 struct strfilter
*filter
)
2484 struct probe_trace_event tev
;
2485 struct perf_probe_event pev
;
2486 struct strlist
*rawlist
;
2487 struct str_node
*ent
;
2489 memset(&tev
, 0, sizeof(tev
));
2490 memset(&pev
, 0, sizeof(pev
));
2492 rawlist
= probe_file__get_rawlist(fd
);
2496 strlist__for_each_entry(ent
, rawlist
) {
2497 ret
= parse_probe_trace_command(ent
->s
, &tev
);
2499 if (!filter_probe_trace_event(&tev
, filter
))
2501 ret
= convert_to_perf_probe_event(&tev
, &pev
,
2505 ret
= show_perf_probe_event(pev
.group
, pev
.event
,
2506 &pev
, tev
.point
.module
,
2510 clear_perf_probe_event(&pev
);
2511 clear_probe_trace_event(&tev
);
2515 strlist__delete(rawlist
);
2516 /* Cleanup cached debuginfo if needed */
2517 debuginfo_cache__exit();
2522 /* List up current perf-probe events */
2523 int show_perf_probe_events(struct strfilter
*filter
)
2525 int kp_fd
, up_fd
, ret
;
2529 if (probe_conf
.cache
)
2530 return probe_cache__show_all_caches(filter
);
2532 ret
= init_probe_symbol_maps(false);
2536 ret
= probe_file__open_both(&kp_fd
, &up_fd
, 0);
2541 ret
= __show_perf_probe_events(kp_fd
, true, filter
);
2542 if (up_fd
>= 0 && ret
>= 0)
2543 ret
= __show_perf_probe_events(up_fd
, false, filter
);
2548 exit_probe_symbol_maps();
2553 static int get_new_event_name(char *buf
, size_t len
, const char *base
,
2554 struct strlist
*namelist
, bool allow_suffix
)
2561 nbase
= strdup(base
);
2565 /* Cut off the dot suffixes (e.g. .const, .isra)*/
2566 p
= strchr(nbase
, '.');
2567 if (p
&& p
!= nbase
)
2570 /* Try no suffix number */
2571 ret
= e_snprintf(buf
, len
, "%s", nbase
);
2573 pr_debug("snprintf() failed: %d\n", ret
);
2576 if (!strlist__has_entry(namelist
, buf
))
2579 if (!allow_suffix
) {
2580 pr_warning("Error: event \"%s\" already exists.\n"
2581 " Hint: Remove existing event by 'perf probe -d'\n"
2582 " or force duplicates by 'perf probe -f'\n"
2583 " or set 'force=yes' in BPF source.\n",
2589 /* Try to add suffix */
2590 for (i
= 1; i
< MAX_EVENT_INDEX
; i
++) {
2591 ret
= e_snprintf(buf
, len
, "%s_%d", nbase
, i
);
2593 pr_debug("snprintf() failed: %d\n", ret
);
2596 if (!strlist__has_entry(namelist
, buf
))
2599 if (i
== MAX_EVENT_INDEX
) {
2600 pr_warning("Too many events are on the same function.\n");
2609 /* Warn if the current kernel's uprobe implementation is old */
2610 static void warn_uprobe_event_compat(struct probe_trace_event
*tev
)
2613 char *buf
= synthesize_probe_trace_command(tev
);
2615 /* Old uprobe event doesn't support memory dereference */
2616 if (!tev
->uprobes
|| tev
->nargs
== 0 || !buf
)
2619 for (i
= 0; i
< tev
->nargs
; i
++)
2620 if (strglobmatch(tev
->args
[i
].value
, "[$@+-]*")) {
2621 pr_warning("Please upgrade your kernel to at least "
2622 "3.14 to have access to feature %s\n",
2623 tev
->args
[i
].value
);
2630 /* Set new name from original perf_probe_event and namelist */
2631 static int probe_trace_event__set_name(struct probe_trace_event
*tev
,
2632 struct perf_probe_event
*pev
,
2633 struct strlist
*namelist
,
2636 const char *event
, *group
;
2640 /* If probe_event or trace_event already have the name, reuse it */
2641 if (pev
->event
&& !pev
->sdt
)
2643 else if (tev
->event
)
2646 /* Or generate new one from probe point */
2647 if (pev
->point
.function
&&
2648 (strncmp(pev
->point
.function
, "0x", 2) != 0) &&
2649 !strisglob(pev
->point
.function
))
2650 event
= pev
->point
.function
;
2652 event
= tev
->point
.realname
;
2654 if (pev
->group
&& !pev
->sdt
)
2656 else if (tev
->group
)
2659 group
= PERFPROBE_GROUP
;
2661 /* Get an unused new event name */
2662 ret
= get_new_event_name(buf
, 64, event
,
2663 namelist
, allow_suffix
);
2669 tev
->event
= strdup(event
);
2670 tev
->group
= strdup(group
);
2671 if (tev
->event
== NULL
|| tev
->group
== NULL
)
2674 /* Add added event name to namelist */
2675 strlist__add(namelist
, event
);
2679 static int __open_probe_file_and_namelist(bool uprobe
,
2680 struct strlist
**namelist
)
2684 fd
= probe_file__open(PF_FL_RW
| (uprobe
? PF_FL_UPROBE
: 0));
2688 /* Get current event names */
2689 *namelist
= probe_file__get_namelist(fd
);
2691 pr_debug("Failed to get current event list.\n");
2698 static int __add_probe_trace_events(struct perf_probe_event
*pev
,
2699 struct probe_trace_event
*tevs
,
2700 int ntevs
, bool allow_suffix
)
2702 int i
, fd
[2] = {-1, -1}, up
, ret
;
2703 struct probe_trace_event
*tev
= NULL
;
2704 struct probe_cache
*cache
= NULL
;
2705 struct strlist
*namelist
[2] = {NULL
, NULL
};
2707 up
= pev
->uprobes
? 1 : 0;
2708 fd
[up
] = __open_probe_file_and_namelist(up
, &namelist
[up
]);
2713 for (i
= 0; i
< ntevs
; i
++) {
2715 up
= tev
->uprobes
? 1 : 0;
2716 if (fd
[up
] == -1) { /* Open the kprobe/uprobe_events */
2717 fd
[up
] = __open_probe_file_and_namelist(up
,
2722 /* Skip if the symbol is out of .text or blacklisted */
2723 if (!tev
->point
.symbol
&& !pev
->uprobes
)
2726 /* Set new name for tev (and update namelist) */
2727 ret
= probe_trace_event__set_name(tev
, pev
, namelist
[up
],
2732 ret
= probe_file__add_event(fd
[up
], tev
);
2737 * Probes after the first probe which comes from same
2738 * user input are always allowed to add suffix, because
2739 * there might be several addresses corresponding to
2742 allow_suffix
= true;
2744 if (ret
== -EINVAL
&& pev
->uprobes
)
2745 warn_uprobe_event_compat(tev
);
2746 if (ret
== 0 && probe_conf
.cache
) {
2747 cache
= probe_cache__new(pev
->target
);
2749 probe_cache__add_entry(cache
, pev
, tevs
, ntevs
) < 0 ||
2750 probe_cache__commit(cache
) < 0)
2751 pr_warning("Failed to add event to probe cache\n");
2752 probe_cache__delete(cache
);
2756 for (up
= 0; up
< 2; up
++) {
2757 strlist__delete(namelist
[up
]);
2764 static int find_probe_functions(struct map
*map
, char *name
,
2765 struct symbol
**syms
)
2769 struct rb_node
*tmp
;
2771 if (map__load(map
) < 0)
2774 map__for_each_symbol(map
, sym
, tmp
) {
2775 if (strglobmatch(sym
->name
, name
)) {
2777 if (syms
&& found
< probe_conf
.max_probes
)
2778 syms
[found
- 1] = sym
;
2785 void __weak
arch__fix_tev_from_maps(struct perf_probe_event
*pev __maybe_unused
,
2786 struct probe_trace_event
*tev __maybe_unused
,
2787 struct map
*map __maybe_unused
,
2788 struct symbol
*sym __maybe_unused
) { }
2791 * Find probe function addresses from map.
2792 * Return an error or the number of found probe_trace_event
2794 static int find_probe_trace_events_from_map(struct perf_probe_event
*pev
,
2795 struct probe_trace_event
**tevs
)
2797 struct map
*map
= NULL
;
2798 struct ref_reloc_sym
*reloc_sym
= NULL
;
2800 struct symbol
**syms
= NULL
;
2801 struct probe_trace_event
*tev
;
2802 struct perf_probe_point
*pp
= &pev
->point
;
2803 struct probe_trace_point
*tp
;
2804 int num_matched_functions
;
2805 int ret
, i
, j
, skipped
= 0;
2808 map
= get_target_map(pev
->target
, pev
->uprobes
);
2814 syms
= malloc(sizeof(struct symbol
*) * probe_conf
.max_probes
);
2821 * Load matched symbols: Since the different local symbols may have
2822 * same name but different addresses, this lists all the symbols.
2824 num_matched_functions
= find_probe_functions(map
, pp
->function
, syms
);
2825 if (num_matched_functions
== 0) {
2826 pr_err("Failed to find symbol %s in %s\n", pp
->function
,
2827 pev
->target
? : "kernel");
2830 } else if (num_matched_functions
> probe_conf
.max_probes
) {
2831 pr_err("Too many functions matched in %s\n",
2832 pev
->target
? : "kernel");
2837 /* Note that the symbols in the kmodule are not relocated */
2838 if (!pev
->uprobes
&& !pev
->target
&&
2839 (!pp
->retprobe
|| kretprobe_offset_is_supported())) {
2840 reloc_sym
= kernel_get_ref_reloc_sym();
2842 pr_warning("Relocated base symbol is not found!\n");
2848 /* Setup result trace-probe-events */
2849 *tevs
= zalloc(sizeof(*tev
) * num_matched_functions
);
2857 for (j
= 0; j
< num_matched_functions
; j
++) {
2860 tev
= (*tevs
) + ret
;
2862 if (ret
== num_matched_functions
) {
2863 pr_warning("Too many symbols are listed. Skip it.\n");
2868 if (pp
->offset
> sym
->end
- sym
->start
) {
2869 pr_warning("Offset %ld is bigger than the size of %s\n",
2870 pp
->offset
, sym
->name
);
2874 /* Add one probe point */
2875 tp
->address
= map
->unmap_ip(map
, sym
->start
) + pp
->offset
;
2877 /* Check the kprobe (not in module) is within .text */
2878 if (!pev
->uprobes
&& !pev
->target
&&
2879 kprobe_warn_out_range(sym
->name
, tp
->address
)) {
2880 tp
->symbol
= NULL
; /* Skip it */
2882 } else if (reloc_sym
) {
2883 tp
->symbol
= strdup_or_goto(reloc_sym
->name
, nomem_out
);
2884 tp
->offset
= tp
->address
- reloc_sym
->addr
;
2886 tp
->symbol
= strdup_or_goto(sym
->name
, nomem_out
);
2887 tp
->offset
= pp
->offset
;
2889 tp
->realname
= strdup_or_goto(sym
->name
, nomem_out
);
2891 tp
->retprobe
= pp
->retprobe
;
2894 tev
->point
.module
= strdup_or_goto(pev
->target
,
2897 mod_name
= find_module_name(pev
->target
);
2899 strdup(mod_name
? mod_name
: pev
->target
);
2901 if (!tev
->point
.module
)
2905 tev
->uprobes
= pev
->uprobes
;
2906 tev
->nargs
= pev
->nargs
;
2908 tev
->args
= zalloc(sizeof(struct probe_trace_arg
) *
2910 if (tev
->args
== NULL
)
2913 for (i
= 0; i
< tev
->nargs
; i
++) {
2914 if (pev
->args
[i
].name
)
2916 strdup_or_goto(pev
->args
[i
].name
,
2919 tev
->args
[i
].value
= strdup_or_goto(pev
->args
[i
].var
,
2921 if (pev
->args
[i
].type
)
2923 strdup_or_goto(pev
->args
[i
].type
,
2926 arch__fix_tev_from_maps(pev
, tev
, map
, sym
);
2928 if (ret
== skipped
) {
2941 clear_probe_trace_events(*tevs
, num_matched_functions
);
2946 static int try_to_find_absolute_address(struct perf_probe_event
*pev
,
2947 struct probe_trace_event
**tevs
)
2949 struct perf_probe_point
*pp
= &pev
->point
;
2950 struct probe_trace_event
*tev
;
2951 struct probe_trace_point
*tp
;
2954 if (!(pev
->point
.function
&& !strncmp(pev
->point
.function
, "0x", 2)))
2956 if (perf_probe_event_need_dwarf(pev
))
2960 * This is 'perf probe /lib/libc.so 0xabcd'. Try to probe at
2963 * Only one tev can be generated by this.
2965 *tevs
= zalloc(sizeof(*tev
));
2973 * Don't use tp->offset, use address directly, because
2974 * in synthesize_probe_trace_command() address cannot be
2977 tp
->address
= pev
->point
.abs_address
;
2978 tp
->retprobe
= pp
->retprobe
;
2979 tev
->uprobes
= pev
->uprobes
;
2983 * Give it a '0x' leading symbol name.
2984 * In __add_probe_trace_events, a NULL symbol is interpreted as
2987 if (asprintf(&tp
->symbol
, "0x%lx", tp
->address
) < 0)
2990 /* For kprobe, check range */
2991 if ((!tev
->uprobes
) &&
2992 (kprobe_warn_out_range(tev
->point
.symbol
,
2993 tev
->point
.address
))) {
2998 if (asprintf(&tp
->realname
, "abs_%lx", tp
->address
) < 0)
3002 tp
->module
= strdup(pev
->target
);
3008 tev
->group
= strdup(pev
->group
);
3014 tev
->event
= strdup(pev
->event
);
3019 tev
->nargs
= pev
->nargs
;
3020 tev
->args
= zalloc(sizeof(struct probe_trace_arg
) * tev
->nargs
);
3024 for (i
= 0; i
< tev
->nargs
; i
++)
3025 copy_to_probe_trace_arg(&tev
->args
[i
], &pev
->args
[i
]);
3030 clear_probe_trace_events(*tevs
, 1);
3035 /* Concatinate two arrays */
3036 static void *memcat(void *a
, size_t sz_a
, void *b
, size_t sz_b
)
3040 ret
= malloc(sz_a
+ sz_b
);
3042 memcpy(ret
, a
, sz_a
);
3043 memcpy(ret
+ sz_a
, b
, sz_b
);
3049 concat_probe_trace_events(struct probe_trace_event
**tevs
, int *ntevs
,
3050 struct probe_trace_event
**tevs2
, int ntevs2
)
3052 struct probe_trace_event
*new_tevs
;
3062 if (*ntevs
+ ntevs2
> probe_conf
.max_probes
)
3065 /* Concatinate the array of probe_trace_event */
3066 new_tevs
= memcat(*tevs
, (*ntevs
) * sizeof(**tevs
),
3067 *tevs2
, ntevs2
* sizeof(**tevs2
));
3077 clear_probe_trace_events(*tevs2
, ntevs2
);
3084 * Try to find probe_trace_event from given probe caches. Return the number
3085 * of cached events found, if an error occurs return the error.
3087 static int find_cached_events(struct perf_probe_event
*pev
,
3088 struct probe_trace_event
**tevs
,
3091 struct probe_cache
*cache
;
3092 struct probe_cache_entry
*entry
;
3093 struct probe_trace_event
*tmp_tevs
= NULL
;
3097 cache
= probe_cache__new(target
);
3098 /* Return 0 ("not found") if the target has no probe cache. */
3102 for_each_probe_cache_entry(entry
, cache
) {
3103 /* Skip the cache entry which has no name */
3104 if (!entry
->pev
.event
|| !entry
->pev
.group
)
3106 if ((!pev
->group
|| strglobmatch(entry
->pev
.group
, pev
->group
)) &&
3107 strglobmatch(entry
->pev
.event
, pev
->event
)) {
3108 ret
= probe_cache_entry__get_event(entry
, &tmp_tevs
);
3110 ret
= concat_probe_trace_events(tevs
, &ntevs
,
3116 probe_cache__delete(cache
);
3118 clear_probe_trace_events(*tevs
, ntevs
);
3122 if (ntevs
> 0 && target
&& target
[0] == '/')
3123 pev
->uprobes
= true;
3129 /* Try to find probe_trace_event from all probe caches */
3130 static int find_cached_events_all(struct perf_probe_event
*pev
,
3131 struct probe_trace_event
**tevs
)
3133 struct probe_trace_event
*tmp_tevs
= NULL
;
3134 struct strlist
*bidlist
;
3135 struct str_node
*nd
;
3140 /* Get the buildid list of all valid caches */
3141 bidlist
= build_id_cache__list_all(true);
3144 pr_debug("Failed to get buildids: %d\n", ret
);
3149 strlist__for_each_entry(nd
, bidlist
) {
3150 pathname
= build_id_cache__origname(nd
->s
);
3151 ret
= find_cached_events(pev
, &tmp_tevs
, pathname
);
3152 /* In the case of cnt == 0, we just skip it */
3154 ret
= concat_probe_trace_events(tevs
, &ntevs
,
3160 strlist__delete(bidlist
);
3163 clear_probe_trace_events(*tevs
, ntevs
);
3171 static int find_probe_trace_events_from_cache(struct perf_probe_event
*pev
,
3172 struct probe_trace_event
**tevs
)
3174 struct probe_cache
*cache
;
3175 struct probe_cache_entry
*entry
;
3176 struct probe_trace_event
*tev
;
3177 struct str_node
*node
;
3181 /* For SDT/cached events, we use special search functions */
3183 return find_cached_events_all(pev
, tevs
);
3185 return find_cached_events(pev
, tevs
, pev
->target
);
3187 cache
= probe_cache__new(pev
->target
);
3191 entry
= probe_cache__find(cache
, pev
);
3193 /* SDT must be in the cache */
3194 ret
= pev
->sdt
? -ENOENT
: 0;
3198 ret
= strlist__nr_entries(entry
->tevlist
);
3199 if (ret
> probe_conf
.max_probes
) {
3200 pr_debug("Too many entries matched in the cache of %s\n",
3201 pev
->target
? : "kernel");
3206 *tevs
= zalloc(ret
* sizeof(*tev
));
3213 strlist__for_each_entry(node
, entry
->tevlist
) {
3214 tev
= &(*tevs
)[i
++];
3215 ret
= parse_probe_trace_command(node
->s
, tev
);
3218 /* Set the uprobes attribute as same as original */
3219 tev
->uprobes
= pev
->uprobes
;
3224 probe_cache__delete(cache
);
3228 static int convert_to_probe_trace_events(struct perf_probe_event
*pev
,
3229 struct probe_trace_event
**tevs
)
3233 if (!pev
->group
&& !pev
->sdt
) {
3234 /* Set group name if not given */
3235 if (!pev
->uprobes
) {
3236 pev
->group
= strdup(PERFPROBE_GROUP
);
3237 ret
= pev
->group
? 0 : -ENOMEM
;
3239 ret
= convert_exec_to_group(pev
->target
, &pev
->group
);
3241 pr_warning("Failed to make a group name.\n");
3246 ret
= try_to_find_absolute_address(pev
, tevs
);
3250 /* At first, we need to lookup cache entry */
3251 ret
= find_probe_trace_events_from_cache(pev
, tevs
);
3252 if (ret
> 0 || pev
->sdt
) /* SDT can be found only in the cache */
3253 return ret
== 0 ? -ENOENT
: ret
; /* Found in probe cache */
3255 /* Convert perf_probe_event with debuginfo */
3256 ret
= try_to_find_probe_trace_events(pev
, tevs
);
3258 return ret
; /* Found in debuginfo or got an error */
3260 return find_probe_trace_events_from_map(pev
, tevs
);
3263 int convert_perf_probe_events(struct perf_probe_event
*pevs
, int npevs
)
3267 /* Loop 1: convert all events */
3268 for (i
= 0; i
< npevs
; i
++) {
3269 /* Init kprobe blacklist if needed */
3270 if (!pevs
[i
].uprobes
)
3271 kprobe_blacklist__init();
3272 /* Convert with or without debuginfo */
3273 ret
= convert_to_probe_trace_events(&pevs
[i
], &pevs
[i
].tevs
);
3276 pevs
[i
].ntevs
= ret
;
3278 /* This just release blacklist only if allocated */
3279 kprobe_blacklist__release();
3284 static int show_probe_trace_event(struct probe_trace_event
*tev
)
3286 char *buf
= synthesize_probe_trace_command(tev
);
3289 pr_debug("Failed to synthesize probe trace event.\n");
3293 /* Showing definition always go stdout */
3294 printf("%s\n", buf
);
3300 int show_probe_trace_events(struct perf_probe_event
*pevs
, int npevs
)
3302 struct strlist
*namelist
= strlist__new(NULL
, NULL
);
3303 struct probe_trace_event
*tev
;
3304 struct perf_probe_event
*pev
;
3310 for (j
= 0; j
< npevs
&& !ret
; j
++) {
3312 for (i
= 0; i
< pev
->ntevs
&& !ret
; i
++) {
3313 tev
= &pev
->tevs
[i
];
3314 /* Skip if the symbol is out of .text or blacklisted */
3315 if (!tev
->point
.symbol
&& !pev
->uprobes
)
3318 /* Set new name for tev (and update namelist) */
3319 ret
= probe_trace_event__set_name(tev
, pev
,
3322 ret
= show_probe_trace_event(tev
);
3325 strlist__delete(namelist
);
3330 int apply_perf_probe_events(struct perf_probe_event
*pevs
, int npevs
)
3334 /* Loop 2: add all events */
3335 for (i
= 0; i
< npevs
; i
++) {
3336 ret
= __add_probe_trace_events(&pevs
[i
], pevs
[i
].tevs
,
3338 probe_conf
.force_add
);
3345 void cleanup_perf_probe_events(struct perf_probe_event
*pevs
, int npevs
)
3349 /* Loop 3: cleanup and free trace events */
3350 for (i
= 0; i
< npevs
; i
++) {
3351 for (j
= 0; j
< pevs
[i
].ntevs
; j
++)
3352 clear_probe_trace_event(&pevs
[i
].tevs
[j
]);
3353 zfree(&pevs
[i
].tevs
);
3355 clear_perf_probe_event(&pevs
[i
]);
3359 int add_perf_probe_events(struct perf_probe_event
*pevs
, int npevs
)
3363 ret
= init_probe_symbol_maps(pevs
->uprobes
);
3367 ret
= convert_perf_probe_events(pevs
, npevs
);
3369 ret
= apply_perf_probe_events(pevs
, npevs
);
3371 cleanup_perf_probe_events(pevs
, npevs
);
3373 exit_probe_symbol_maps();
3377 int del_perf_probe_events(struct strfilter
*filter
)
3379 int ret
, ret2
, ufd
= -1, kfd
= -1;
3380 char *str
= strfilter__string(filter
);
3385 /* Get current event names */
3386 ret
= probe_file__open_both(&kfd
, &ufd
, PF_FL_RW
);
3390 ret
= probe_file__del_events(kfd
, filter
);
3391 if (ret
< 0 && ret
!= -ENOENT
)
3394 ret2
= probe_file__del_events(ufd
, filter
);
3395 if (ret2
< 0 && ret2
!= -ENOENT
) {
3412 int show_available_funcs(const char *target
, struct strfilter
*_filter
,
3419 ret
= init_probe_symbol_maps(user
);
3423 /* Get a symbol map */
3424 map
= get_target_map(target
, user
);
3426 pr_err("Failed to get a map for %s\n", (target
) ? : "kernel");
3430 ret
= map__load(map
);
3433 char *str
= strfilter__string(_filter
);
3434 pr_err("Failed to find symbols matched to \"%s\"\n",
3438 pr_err("Failed to load symbols in %s\n",
3439 (target
) ? : "kernel");
3442 if (!dso__sorted_by_name(map
->dso
, map
->type
))
3443 dso__sort_by_name(map
->dso
, map
->type
);
3445 /* Show all (filtered) symbols */
3448 for (nd
= rb_first(&map
->dso
->symbol_names
[map
->type
]); nd
; nd
= rb_next(nd
)) {
3449 struct symbol_name_rb_node
*pos
= rb_entry(nd
, struct symbol_name_rb_node
, rb_node
);
3451 if (strfilter__compare(_filter
, pos
->sym
.name
))
3452 printf("%s\n", pos
->sym
.name
);
3457 exit_probe_symbol_maps();
3462 int copy_to_probe_trace_arg(struct probe_trace_arg
*tvar
,
3463 struct perf_probe_arg
*pvar
)
3465 tvar
->value
= strdup(pvar
->var
);
3466 if (tvar
->value
== NULL
)
3469 tvar
->type
= strdup(pvar
->type
);
3470 if (tvar
->type
== NULL
)
3474 tvar
->name
= strdup(pvar
->name
);
3475 if (tvar
->name
== NULL
)