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 ref_reloc_sym
*kernel_get_ref_reloc_sym(void)
116 /* kmap->ref_reloc_sym should be set if host_machine is initialized */
118 struct map
*map
= machine__kernel_map(host_machine
);
120 if (map__load(map
) < 0)
123 kmap
= map__kmap(map
);
126 return kmap
->ref_reloc_sym
;
129 static int kernel_get_symbol_address_by_name(const char *name
, u64
*addr
,
130 bool reloc
, bool reladdr
)
132 struct ref_reloc_sym
*reloc_sym
;
136 /* ref_reloc_sym is just a label. Need a special fix*/
137 reloc_sym
= kernel_get_ref_reloc_sym();
138 if (reloc_sym
&& strcmp(name
, reloc_sym
->name
) == 0)
139 *addr
= (reloc
) ? reloc_sym
->addr
: reloc_sym
->unrelocated_addr
;
141 sym
= machine__find_kernel_symbol_by_name(host_machine
, name
, &map
);
144 *addr
= map
->unmap_ip(map
, sym
->start
) -
145 ((reloc
) ? 0 : map
->reloc
) -
146 ((reladdr
) ? map
->start
: 0);
151 static struct map
*kernel_get_module_map(const char *module
)
153 struct maps
*maps
= machine__kernel_maps(host_machine
);
156 /* A file path -- this is an offline module */
157 if (module
&& strchr(module
, '/'))
158 return dso__new_map(module
);
161 pos
= machine__kernel_map(host_machine
);
162 return map__get(pos
);
165 for (pos
= maps__first(maps
); pos
; pos
= map__next(pos
)) {
166 /* short_name is "[module]" */
167 if (strncmp(pos
->dso
->short_name
+ 1, module
,
168 pos
->dso
->short_name_len
- 2) == 0 &&
169 module
[pos
->dso
->short_name_len
- 2] == '\0') {
170 return map__get(pos
);
176 struct map
*get_target_map(const char *target
, struct nsinfo
*nsi
, bool user
)
178 /* Init maps of given executable or kernel */
182 map
= dso__new_map(target
);
184 map
->dso
->nsinfo
= nsinfo__get(nsi
);
187 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
, 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
, struct nsinfo
*nsi
,
367 struct map
*map
= NULL
;
372 /* This can work only for function-name based one */
373 if (!pp
->function
|| pp
->file
)
376 map
= get_target_map(target
, nsi
, uprobes
);
380 /* Find the address of given function */
381 map__for_each_symbol_by_name(map
, pp
->function
, sym
) {
383 address
= sym
->start
;
385 address
= map
->unmap_ip(map
, sym
->start
) - map
->reloc
;
392 pr_debug("Symbol %s address found : %" PRIx64
"\n",
393 pp
->function
, address
);
395 ret
= debuginfo__find_probe_point(dinfo
, (unsigned long)address
,
398 ret
= (!ret
) ? -ENOENT
: ret
;
400 result
->offset
+= pp
->offset
;
401 result
->line
+= pp
->line
;
402 result
->retprobe
= pp
->retprobe
;
412 static int get_alternative_probe_event(struct debuginfo
*dinfo
,
413 struct perf_probe_event
*pev
,
414 struct perf_probe_point
*tmp
)
418 memcpy(tmp
, &pev
->point
, sizeof(*tmp
));
419 memset(&pev
->point
, 0, sizeof(pev
->point
));
420 ret
= find_alternative_probe_point(dinfo
, tmp
, &pev
->point
, pev
->target
,
421 pev
->nsi
, pev
->uprobes
);
423 memcpy(&pev
->point
, tmp
, sizeof(*tmp
));
428 static int get_alternative_line_range(struct debuginfo
*dinfo
,
429 struct line_range
*lr
,
430 const char *target
, bool user
)
432 struct perf_probe_point pp
= { .function
= lr
->function
,
435 struct perf_probe_point result
;
438 memset(&result
, 0, sizeof(result
));
440 if (lr
->end
!= INT_MAX
)
441 len
= lr
->end
- lr
->start
;
442 ret
= find_alternative_probe_point(dinfo
, &pp
, &result
,
445 lr
->function
= result
.function
;
446 lr
->file
= result
.file
;
447 lr
->start
= result
.line
;
448 if (lr
->end
!= INT_MAX
)
449 lr
->end
= lr
->start
+ len
;
450 clear_perf_probe_point(&pp
);
455 /* Open new debuginfo of given module */
456 static struct debuginfo
*open_debuginfo(const char *module
, struct nsinfo
*nsi
,
459 const char *path
= module
;
460 char reason
[STRERR_BUFSIZE
];
461 struct debuginfo
*ret
= NULL
;
462 struct dso
*dso
= NULL
;
466 if (!module
|| !strchr(module
, '/')) {
467 err
= kernel_get_module_dso(module
, &dso
);
469 if (!dso
|| dso
->load_errno
== 0) {
470 if (!str_error_r(-err
, reason
, STRERR_BUFSIZE
))
471 strcpy(reason
, "(unknown)");
473 dso__strerror_load(dso
, reason
, STRERR_BUFSIZE
);
475 pr_err("Failed to find the path for %s: %s\n",
476 module
?: "kernel", reason
);
479 path
= dso
->long_name
;
481 nsinfo__mountns_enter(nsi
, &nsc
);
482 ret
= debuginfo__new(path
);
483 if (!ret
&& !silent
) {
484 pr_warning("The %s file has no debug information.\n", path
);
485 if (!module
|| !strtailcmp(path
, ".ko"))
486 pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, ");
488 pr_warning("Rebuild with -g, ");
489 pr_warning("or install an appropriate debuginfo package.\n");
491 nsinfo__mountns_exit(&nsc
);
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
, NULL
, 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
,
540 int fd
, ret
= -ENOENT
;
543 nsinfo__mountns_enter(nsi
, &nsc
);
544 fd
= open(exec
, O_RDONLY
);
545 nsinfo__mountns_exit(&nsc
);
549 elf
= elf_begin(fd
, PERF_ELF_C_READ_MMAP
, NULL
);
555 if (gelf_getehdr(elf
, &ehdr
) == NULL
)
558 if (!elf_section_by_name(elf
, &ehdr
, &shdr
, ".text", NULL
))
561 *address
= shdr
.sh_addr
- shdr
.sh_offset
;
572 * Convert trace point to probe point with debuginfo
574 static int find_perf_probe_point_from_dwarf(struct probe_trace_point
*tp
,
575 struct perf_probe_point
*pp
,
578 struct debuginfo
*dinfo
= NULL
;
579 unsigned long stext
= 0;
580 u64 addr
= tp
->address
;
583 /* convert the address to dwarf address */
589 ret
= get_text_start_address(tp
->module
, &stext
, NULL
);
593 } else if (tp
->symbol
) {
594 /* If the module is given, this returns relative address */
595 ret
= kernel_get_symbol_address_by_name(tp
->symbol
, &addr
,
596 false, !!tp
->module
);
602 pr_debug("try to find information at %" PRIx64
" in %s\n", addr
,
603 tp
->module
? : "kernel");
605 dinfo
= debuginfo_cache__open(tp
->module
, verbose
<= 0);
607 ret
= debuginfo__find_probe_point(dinfo
,
608 (unsigned long)addr
, pp
);
613 pp
->retprobe
= tp
->retprobe
;
617 pr_debug("Failed to find corresponding probes from debuginfo.\n");
618 return ret
? : -ENOENT
;
621 /* Adjust symbol name and address */
622 static int post_process_probe_trace_point(struct probe_trace_point
*tp
,
623 struct map
*map
, unsigned long offs
)
626 u64 addr
= tp
->address
- offs
;
628 sym
= map__find_symbol(map
, addr
);
632 if (strcmp(sym
->name
, tp
->symbol
)) {
633 /* If we have no realname, use symbol for it */
635 tp
->realname
= tp
->symbol
;
638 tp
->symbol
= strdup(sym
->name
);
642 tp
->offset
= addr
- sym
->start
;
649 * Rename DWARF symbols to ELF symbols -- gcc sometimes optimizes functions
650 * and generate new symbols with suffixes such as .constprop.N or .isra.N
651 * etc. Since those symbols are not recorded in DWARF, we have to find
652 * correct generated symbols from offline ELF binary.
653 * For online kernel or uprobes we don't need this because those are
654 * rebased on _text, or already a section relative address.
657 post_process_offline_probe_trace_events(struct probe_trace_event
*tevs
,
658 int ntevs
, const char *pathname
)
661 unsigned long stext
= 0;
664 /* Prepare a map for offline binary */
665 map
= dso__new_map(pathname
);
666 if (!map
|| get_text_start_address(pathname
, &stext
, NULL
) < 0) {
667 pr_warning("Failed to get ELF symbols for %s\n", pathname
);
671 for (i
= 0; i
< ntevs
; i
++) {
672 ret
= post_process_probe_trace_point(&tevs
[i
].point
,
682 static int add_exec_to_probe_trace_events(struct probe_trace_event
*tevs
,
683 int ntevs
, const char *exec
,
687 unsigned long stext
= 0;
692 ret
= get_text_start_address(exec
, &stext
, nsi
);
696 for (i
= 0; i
< ntevs
&& ret
>= 0; i
++) {
697 /* point.address is the address of point.symbol + point.offset */
698 tevs
[i
].point
.address
-= stext
;
699 tevs
[i
].point
.module
= strdup(exec
);
700 if (!tevs
[i
].point
.module
) {
704 tevs
[i
].uprobes
= true;
711 post_process_module_probe_trace_events(struct probe_trace_event
*tevs
,
712 int ntevs
, const char *module
,
713 struct debuginfo
*dinfo
)
715 Dwarf_Addr text_offs
= 0;
717 char *mod_name
= NULL
;
723 map
= get_target_map(module
, NULL
, false);
724 if (!map
|| debuginfo__get_text_offset(dinfo
, &text_offs
, true) < 0) {
725 pr_warning("Failed to get ELF symbols for %s\n", module
);
729 mod_name
= find_module_name(module
);
730 for (i
= 0; i
< ntevs
; i
++) {
731 ret
= post_process_probe_trace_point(&tevs
[i
].point
,
732 map
, (unsigned long)text_offs
);
735 tevs
[i
].point
.module
=
736 strdup(mod_name
? mod_name
: module
);
737 if (!tevs
[i
].point
.module
) {
750 post_process_kernel_probe_trace_events(struct probe_trace_event
*tevs
,
753 struct ref_reloc_sym
*reloc_sym
;
757 /* Skip post process if the target is an offline kernel */
758 if (symbol_conf
.ignore_vmlinux_buildid
)
759 return post_process_offline_probe_trace_events(tevs
, ntevs
,
760 symbol_conf
.vmlinux_name
);
762 reloc_sym
= kernel_get_ref_reloc_sym();
764 pr_warning("Relocated base symbol is not found!\n");
768 for (i
= 0; i
< ntevs
; i
++) {
769 if (!tevs
[i
].point
.address
)
771 if (tevs
[i
].point
.retprobe
&& !kretprobe_offset_is_supported())
773 /* If we found a wrong one, mark it by NULL symbol */
774 if (kprobe_warn_out_range(tevs
[i
].point
.symbol
,
775 tevs
[i
].point
.address
)) {
779 tmp
= strdup(reloc_sym
->name
);
783 /* If we have no realname, use symbol for it */
784 if (!tevs
[i
].point
.realname
)
785 tevs
[i
].point
.realname
= tevs
[i
].point
.symbol
;
787 free(tevs
[i
].point
.symbol
);
788 tevs
[i
].point
.symbol
= tmp
;
789 tevs
[i
].point
.offset
= tevs
[i
].point
.address
-
790 reloc_sym
->unrelocated_addr
;
796 arch__post_process_probe_trace_events(struct perf_probe_event
*pev __maybe_unused
,
797 int ntevs __maybe_unused
)
801 /* Post processing the probe events */
802 static int post_process_probe_trace_events(struct perf_probe_event
*pev
,
803 struct probe_trace_event
*tevs
,
804 int ntevs
, const char *module
,
805 bool uprobe
, struct debuginfo
*dinfo
)
810 ret
= add_exec_to_probe_trace_events(tevs
, ntevs
, module
,
813 /* Currently ref_reloc_sym based probe is not for drivers */
814 ret
= post_process_module_probe_trace_events(tevs
, ntevs
,
817 ret
= post_process_kernel_probe_trace_events(tevs
, ntevs
);
820 arch__post_process_probe_trace_events(pev
, ntevs
);
825 /* Try to find perf_probe_event with debuginfo */
826 static int try_to_find_probe_trace_events(struct perf_probe_event
*pev
,
827 struct probe_trace_event
**tevs
)
829 bool need_dwarf
= perf_probe_event_need_dwarf(pev
);
830 struct perf_probe_point tmp
;
831 struct debuginfo
*dinfo
;
834 dinfo
= open_debuginfo(pev
->target
, pev
->nsi
, !need_dwarf
);
838 pr_debug("Could not open debuginfo. Try to use symbols.\n");
842 pr_debug("Try to find probe point from debuginfo.\n");
843 /* Searching trace events corresponding to a probe event */
844 ntevs
= debuginfo__find_trace_events(dinfo
, pev
, tevs
);
846 if (ntevs
== 0) { /* Not found, retry with an alternative */
847 ret
= get_alternative_probe_event(dinfo
, pev
, &tmp
);
849 ntevs
= debuginfo__find_trace_events(dinfo
, pev
, tevs
);
851 * Write back to the original probe_event for
852 * setting appropriate (user given) event name
854 clear_perf_probe_point(&pev
->point
);
855 memcpy(&pev
->point
, &tmp
, sizeof(tmp
));
859 if (ntevs
> 0) { /* Succeeded to find trace events */
860 pr_debug("Found %d probe_trace_events.\n", ntevs
);
861 ret
= post_process_probe_trace_events(pev
, *tevs
, ntevs
,
862 pev
->target
, pev
->uprobes
, dinfo
);
863 if (ret
< 0 || ret
== ntevs
) {
864 pr_debug("Post processing failed or all events are skipped. (%d)\n", ret
);
865 clear_probe_trace_events(*tevs
, ntevs
);
871 debuginfo__delete(dinfo
);
873 if (ntevs
== 0) { /* No error but failed to find probe point. */
874 pr_warning("Probe point '%s' not found.\n",
875 synthesize_perf_probe_point(&pev
->point
));
877 } else if (ntevs
< 0) {
878 /* Error path : ntevs < 0 */
879 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs
);
881 pr_warning("Warning: No dwarf info found in the vmlinux - "
882 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
884 pr_debug("Trying to use symbols.\n");
891 #define LINEBUF_SIZE 256
892 #define NR_ADDITIONAL_LINES 2
894 static int __show_one_line(FILE *fp
, int l
, bool skip
, bool show_num
)
896 char buf
[LINEBUF_SIZE
], sbuf
[STRERR_BUFSIZE
];
897 const char *color
= show_num
? "" : PERF_COLOR_BLUE
;
898 const char *prefix
= NULL
;
901 if (fgets(buf
, LINEBUF_SIZE
, fp
) == NULL
)
906 prefix
= show_num
? "%7d " : " ";
907 color_fprintf(stdout
, color
, prefix
, l
);
909 color_fprintf(stdout
, color
, "%s", buf
);
911 } while (strchr(buf
, '\n') == NULL
);
916 pr_warning("File read error: %s\n",
917 str_error_r(errno
, sbuf
, sizeof(sbuf
)));
923 static int _show_one_line(FILE *fp
, int l
, bool skip
, bool show_num
)
925 int rv
= __show_one_line(fp
, l
, skip
, show_num
);
927 pr_warning("Source file is shorter than expected.\n");
933 #define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
934 #define show_one_line(f,l) _show_one_line(f,l,false,false)
935 #define skip_one_line(f,l) _show_one_line(f,l,true,false)
936 #define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
939 * Show line-range always requires debuginfo to find source file and
942 static int __show_line_range(struct line_range
*lr
, const char *module
,
947 struct debuginfo
*dinfo
;
951 char sbuf
[STRERR_BUFSIZE
];
953 /* Search a line range */
954 dinfo
= open_debuginfo(module
, NULL
, false);
958 ret
= debuginfo__find_line_range(dinfo
, lr
);
959 if (!ret
) { /* Not found, retry with an alternative */
960 ret
= get_alternative_line_range(dinfo
, lr
, module
, user
);
962 ret
= debuginfo__find_line_range(dinfo
, lr
);
964 debuginfo__delete(dinfo
);
965 if (ret
== 0 || ret
== -ENOENT
) {
966 pr_warning("Specified source line is not found.\n");
968 } else if (ret
< 0) {
969 pr_warning("Debuginfo analysis failed.\n");
973 /* Convert source file path */
975 ret
= get_real_path(tmp
, lr
->comp_dir
, &lr
->path
);
977 /* Free old path when new path is assigned */
982 pr_warning("Failed to find source file path.\n");
989 fprintf(stdout
, "<%s@%s:%d>\n", lr
->function
, lr
->path
,
990 lr
->start
- lr
->offset
);
992 fprintf(stdout
, "<%s:%d>\n", lr
->path
, lr
->start
);
994 fp
= fopen(lr
->path
, "r");
996 pr_warning("Failed to open %s: %s\n", lr
->path
,
997 str_error_r(errno
, sbuf
, sizeof(sbuf
)));
1000 /* Skip to starting line number */
1001 while (l
< lr
->start
) {
1002 ret
= skip_one_line(fp
, l
++);
1007 intlist__for_each_entry(ln
, lr
->line_list
) {
1008 for (; ln
->i
> l
; l
++) {
1009 ret
= show_one_line(fp
, l
- lr
->offset
);
1013 ret
= show_one_line_with_num(fp
, l
++ - lr
->offset
);
1018 if (lr
->end
== INT_MAX
)
1019 lr
->end
= l
+ NR_ADDITIONAL_LINES
;
1020 while (l
<= lr
->end
) {
1021 ret
= show_one_line_or_eof(fp
, l
++ - lr
->offset
);
1030 int show_line_range(struct line_range
*lr
, const char *module
,
1031 struct nsinfo
*nsi
, bool user
)
1034 struct nscookie nsc
;
1036 ret
= init_probe_symbol_maps(user
);
1039 nsinfo__mountns_enter(nsi
, &nsc
);
1040 ret
= __show_line_range(lr
, module
, user
);
1041 nsinfo__mountns_exit(&nsc
);
1042 exit_probe_symbol_maps();
1047 static int show_available_vars_at(struct debuginfo
*dinfo
,
1048 struct perf_probe_event
*pev
,
1049 struct strfilter
*_filter
)
1053 struct str_node
*node
;
1054 struct variable_list
*vls
= NULL
, *vl
;
1055 struct perf_probe_point tmp
;
1058 buf
= synthesize_perf_probe_point(&pev
->point
);
1061 pr_debug("Searching variables at %s\n", buf
);
1063 ret
= debuginfo__find_available_vars_at(dinfo
, pev
, &vls
);
1064 if (!ret
) { /* Not found, retry with an alternative */
1065 ret
= get_alternative_probe_event(dinfo
, pev
, &tmp
);
1067 ret
= debuginfo__find_available_vars_at(dinfo
, pev
,
1069 /* Release the old probe_point */
1070 clear_perf_probe_point(&tmp
);
1074 if (ret
== 0 || ret
== -ENOENT
) {
1075 pr_err("Failed to find the address of %s\n", buf
);
1078 pr_warning("Debuginfo analysis failed.\n");
1082 /* Some variables are found */
1083 fprintf(stdout
, "Available variables at %s\n", buf
);
1084 for (i
= 0; i
< ret
; i
++) {
1087 * A probe point might be converted to
1088 * several trace points.
1090 fprintf(stdout
, "\t@<%s+%lu>\n", vl
->point
.symbol
,
1092 zfree(&vl
->point
.symbol
);
1095 strlist__for_each_entry(node
, vl
->vars
) {
1096 var
= strchr(node
->s
, '\t') + 1;
1097 if (strfilter__compare(_filter
, var
)) {
1098 fprintf(stdout
, "\t\t%s\n", node
->s
);
1102 strlist__delete(vl
->vars
);
1105 fprintf(stdout
, "\t\t(No matched variables)\n");
1113 /* Show available variables on given probe point */
1114 int show_available_vars(struct perf_probe_event
*pevs
, int npevs
,
1115 struct strfilter
*_filter
)
1118 struct debuginfo
*dinfo
;
1120 ret
= init_probe_symbol_maps(pevs
->uprobes
);
1124 dinfo
= open_debuginfo(pevs
->target
, pevs
->nsi
, false);
1132 for (i
= 0; i
< npevs
&& ret
>= 0; i
++)
1133 ret
= show_available_vars_at(dinfo
, &pevs
[i
], _filter
);
1135 debuginfo__delete(dinfo
);
1137 exit_probe_symbol_maps();
1141 #else /* !HAVE_DWARF_SUPPORT */
1143 static void debuginfo_cache__exit(void)
1148 find_perf_probe_point_from_dwarf(struct probe_trace_point
*tp __maybe_unused
,
1149 struct perf_probe_point
*pp __maybe_unused
,
1150 bool is_kprobe __maybe_unused
)
1155 static int try_to_find_probe_trace_events(struct perf_probe_event
*pev
,
1156 struct probe_trace_event
**tevs __maybe_unused
)
1158 if (perf_probe_event_need_dwarf(pev
)) {
1159 pr_warning("Debuginfo-analysis is not supported.\n");
1166 int show_line_range(struct line_range
*lr __maybe_unused
,
1167 const char *module __maybe_unused
,
1168 struct nsinfo
*nsi __maybe_unused
,
1169 bool user __maybe_unused
)
1171 pr_warning("Debuginfo-analysis is not supported.\n");
1175 int show_available_vars(struct perf_probe_event
*pevs __maybe_unused
,
1176 int npevs __maybe_unused
,
1177 struct strfilter
*filter __maybe_unused
)
1179 pr_warning("Debuginfo-analysis is not supported.\n");
1184 void line_range__clear(struct line_range
*lr
)
1190 intlist__delete(lr
->line_list
);
1191 memset(lr
, 0, sizeof(*lr
));
1194 int line_range__init(struct line_range
*lr
)
1196 memset(lr
, 0, sizeof(*lr
));
1197 lr
->line_list
= intlist__new(NULL
);
1204 static int parse_line_num(char **ptr
, int *val
, const char *what
)
1206 const char *start
= *ptr
;
1209 *val
= strtol(*ptr
, ptr
, 0);
1210 if (errno
|| *ptr
== start
) {
1211 semantic_error("'%s' is not a valid number.\n", what
);
1217 /* Check the name is good for event, group or function */
1218 static bool is_c_func_name(const char *name
)
1220 if (!isalpha(*name
) && *name
!= '_')
1222 while (*++name
!= '\0') {
1223 if (!isalpha(*name
) && !isdigit(*name
) && *name
!= '_')
1230 * Stuff 'lr' according to the line range described by 'arg'.
1231 * The line range syntax is described by:
1233 * SRC[:SLN[+NUM|-ELN]]
1234 * FNC[@SRC][:SLN[+NUM|-ELN]]
1236 int parse_line_range_desc(const char *arg
, struct line_range
*lr
)
1238 char *range
, *file
, *name
= strdup(arg
);
1247 range
= strchr(name
, ':');
1251 err
= parse_line_num(&range
, &lr
->start
, "start line");
1255 if (*range
== '+' || *range
== '-') {
1256 const char c
= *range
++;
1258 err
= parse_line_num(&range
, &lr
->end
, "end line");
1263 lr
->end
+= lr
->start
;
1265 * Adjust the number of lines here.
1266 * If the number of lines == 1, the
1267 * the end of line should be equal to
1268 * the start of line.
1274 pr_debug("Line range is %d to %d\n", lr
->start
, lr
->end
);
1277 if (lr
->start
> lr
->end
) {
1278 semantic_error("Start line must be smaller"
1279 " than end line.\n");
1282 if (*range
!= '\0') {
1283 semantic_error("Tailing with invalid str '%s'.\n", range
);
1288 file
= strchr(name
, '@');
1291 lr
->file
= strdup(++file
);
1292 if (lr
->file
== NULL
) {
1296 lr
->function
= name
;
1297 } else if (strchr(name
, '/') || strchr(name
, '.'))
1299 else if (is_c_func_name(name
))/* We reuse it for checking funcname */
1300 lr
->function
= name
;
1301 else { /* Invalid name */
1302 semantic_error("'%s' is not a valid function name.\n", name
);
1313 static int parse_perf_probe_event_name(char **arg
, struct perf_probe_event
*pev
)
1317 ptr
= strpbrk_esc(*arg
, ":");
1320 if (!pev
->sdt
&& !is_c_func_name(*arg
))
1322 pev
->group
= strdup_esc(*arg
);
1329 pev
->event
= strdup_esc(*arg
);
1330 if (pev
->event
== NULL
)
1333 if (!pev
->sdt
&& !is_c_func_name(pev
->event
)) {
1337 semantic_error("%s is bad for event name -it must "
1338 "follow C symbol-naming rule.\n", *arg
);
1344 /* Parse probepoint definition. */
1345 static int parse_perf_probe_point(char *arg
, struct perf_probe_event
*pev
)
1347 struct perf_probe_point
*pp
= &pev
->point
;
1350 bool file_spec
= false;
1355 * perf probe [GRP:][EVENT=]SRC[:LN|;PTN]
1356 * perf probe [GRP:][EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
1357 * perf probe %[GRP:]SDT_EVENT
1362 if (is_sdt_event(arg
)) {
1368 ptr
= strpbrk_esc(arg
, ";=@+%");
1372 semantic_error("%s must be an SDT name.\n",
1376 /* This must be a target file name or build id */
1377 tmp
= build_id_cache__complement(ptr
+ 1);
1379 pev
->target
= build_id_cache__origname(tmp
);
1382 pev
->target
= strdup_esc(ptr
+ 1);
1387 ret
= parse_perf_probe_event_name(&arg
, pev
);
1389 if (asprintf(&pev
->point
.function
, "%%%s", pev
->event
) < 0)
1395 if (ptr
&& *ptr
== '=') { /* Event name */
1398 ret
= parse_perf_probe_event_name(&arg
, pev
);
1406 * Check arg is function or file name and copy it.
1408 * We consider arg to be a file spec if and only if it satisfies
1409 * all of the below criteria::
1410 * - it does not include any of "+@%",
1411 * - it includes one of ":;", and
1412 * - it has a period '.' in the name.
1414 * Otherwise, we consider arg to be a function specification.
1416 if (!strpbrk_esc(arg
, "+@%")) {
1417 ptr
= strpbrk_esc(arg
, ";:");
1418 /* This is a file spec if it includes a '.' before ; or : */
1419 if (ptr
&& memchr(arg
, '.', ptr
- arg
))
1423 ptr
= strpbrk_esc(arg
, ";:+@%");
1432 tmp
= strdup_esc(arg
);
1443 * Keep pp->function even if this is absolute address,
1444 * so it can mark whether abs_address is valid.
1445 * Which make 'perf probe lib.bin 0x0' possible.
1447 * Note that checking length of tmp is not needed
1448 * because when we access tmp[1] we know tmp[0] is '0',
1449 * so tmp[1] should always valid (but could be '\0').
1451 if (tmp
&& !strncmp(tmp
, "0x", 2)) {
1452 pp
->abs_address
= strtoul(pp
->function
, &tmp
, 0);
1454 semantic_error("Invalid absolute address.\n");
1460 /* Parse other options */
1464 if (c
== ';') { /* Lazy pattern must be the last part */
1465 pp
->lazy_line
= strdup(arg
); /* let leave escapes */
1466 if (pp
->lazy_line
== NULL
)
1470 ptr
= strpbrk_esc(arg
, ";:+@%");
1476 case ':': /* Line number */
1477 pp
->line
= strtoul(arg
, &tmp
, 0);
1479 semantic_error("There is non-digit char"
1480 " in line number.\n");
1484 case '+': /* Byte offset from a symbol */
1485 pp
->offset
= strtoul(arg
, &tmp
, 0);
1487 semantic_error("There is non-digit character"
1492 case '@': /* File name */
1494 semantic_error("SRC@SRC is not allowed.\n");
1497 pp
->file
= strdup_esc(arg
);
1498 if (pp
->file
== NULL
)
1501 case '%': /* Probe places */
1502 if (strcmp(arg
, "return") == 0) {
1504 } else { /* Others not supported yet */
1505 semantic_error("%%%s is not supported.\n", arg
);
1509 default: /* Buggy case */
1510 pr_err("This program has a bug at %s:%d.\n",
1511 __FILE__
, __LINE__
);
1517 /* Exclusion check */
1518 if (pp
->lazy_line
&& pp
->line
) {
1519 semantic_error("Lazy pattern can't be used with"
1524 if (pp
->lazy_line
&& pp
->offset
) {
1525 semantic_error("Lazy pattern can't be used with offset.\n");
1529 if (pp
->line
&& pp
->offset
) {
1530 semantic_error("Offset can't be used with line number.\n");
1534 if (!pp
->line
&& !pp
->lazy_line
&& pp
->file
&& !pp
->function
) {
1535 semantic_error("File always requires line number or "
1540 if (pp
->offset
&& !pp
->function
) {
1541 semantic_error("Offset requires an entry function.\n");
1545 if ((pp
->offset
|| pp
->line
|| pp
->lazy_line
) && pp
->retprobe
) {
1546 semantic_error("Offset/Line/Lazy pattern can't be used with "
1551 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
1552 pp
->function
, pp
->file
, pp
->line
, pp
->offset
, pp
->retprobe
,
1557 /* Parse perf-probe event argument */
1558 static int parse_perf_probe_arg(char *str
, struct perf_probe_arg
*arg
)
1560 char *tmp
, *goodname
;
1561 struct perf_probe_arg_field
**fieldp
;
1563 pr_debug("parsing arg: %s into ", str
);
1565 tmp
= strchr(str
, '=');
1567 arg
->name
= strndup(str
, tmp
- str
);
1568 if (arg
->name
== NULL
)
1570 pr_debug("name:%s ", arg
->name
);
1574 tmp
= strchr(str
, ':');
1575 if (tmp
) { /* Type setting */
1577 arg
->type
= strdup(tmp
+ 1);
1578 if (arg
->type
== NULL
)
1580 pr_debug("type:%s ", arg
->type
);
1583 tmp
= strpbrk(str
, "-.[");
1584 if (!is_c_varname(str
) || !tmp
) {
1585 /* A variable, register, symbol or special value */
1586 arg
->var
= strdup(str
);
1587 if (arg
->var
== NULL
)
1589 pr_debug("%s\n", arg
->var
);
1593 /* Structure fields or array element */
1594 arg
->var
= strndup(str
, tmp
- str
);
1595 if (arg
->var
== NULL
)
1597 goodname
= arg
->var
;
1598 pr_debug("%s, ", arg
->var
);
1599 fieldp
= &arg
->field
;
1602 *fieldp
= zalloc(sizeof(struct perf_probe_arg_field
));
1603 if (*fieldp
== NULL
)
1605 if (*tmp
== '[') { /* Array */
1607 (*fieldp
)->index
= strtol(str
+ 1, &tmp
, 0);
1608 (*fieldp
)->ref
= true;
1609 if (*tmp
!= ']' || tmp
== str
+ 1) {
1610 semantic_error("Array index must be a"
1617 } else { /* Structure */
1620 (*fieldp
)->ref
= false;
1621 } else if (tmp
[1] == '>') {
1623 (*fieldp
)->ref
= true;
1625 semantic_error("Argument parse error: %s\n",
1629 tmp
= strpbrk(str
, "-.[");
1632 (*fieldp
)->name
= strndup(str
, tmp
- str
);
1633 if ((*fieldp
)->name
== NULL
)
1636 goodname
= (*fieldp
)->name
;
1637 pr_debug("%s(%d), ", (*fieldp
)->name
, (*fieldp
)->ref
);
1638 fieldp
= &(*fieldp
)->next
;
1641 (*fieldp
)->name
= strdup(str
);
1642 if ((*fieldp
)->name
== NULL
)
1645 goodname
= (*fieldp
)->name
;
1646 pr_debug("%s(%d)\n", (*fieldp
)->name
, (*fieldp
)->ref
);
1648 /* If no name is specified, set the last field name (not array index)*/
1650 arg
->name
= strdup(goodname
);
1651 if (arg
->name
== NULL
)
1657 /* Parse perf-probe event command */
1658 int parse_perf_probe_command(const char *cmd
, struct perf_probe_event
*pev
)
1661 int argc
, i
, ret
= 0;
1663 argv
= argv_split(cmd
, &argc
);
1665 pr_debug("Failed to split arguments.\n");
1668 if (argc
- 1 > MAX_PROBE_ARGS
) {
1669 semantic_error("Too many probe arguments (%d).\n", argc
- 1);
1673 /* Parse probe point */
1674 ret
= parse_perf_probe_point(argv
[0], pev
);
1678 /* Copy arguments and ensure return probe has no C argument */
1679 pev
->nargs
= argc
- 1;
1680 pev
->args
= zalloc(sizeof(struct perf_probe_arg
) * pev
->nargs
);
1681 if (pev
->args
== NULL
) {
1685 for (i
= 0; i
< pev
->nargs
&& ret
>= 0; i
++) {
1686 ret
= parse_perf_probe_arg(argv
[i
+ 1], &pev
->args
[i
]);
1688 is_c_varname(pev
->args
[i
].var
) && pev
->point
.retprobe
) {
1689 semantic_error("You can't specify local variable for"
1700 /* Returns true if *any* ARG is either C variable, $params or $vars. */
1701 bool perf_probe_with_var(struct perf_probe_event
*pev
)
1705 for (i
= 0; i
< pev
->nargs
; i
++)
1706 if (is_c_varname(pev
->args
[i
].var
) ||
1707 !strcmp(pev
->args
[i
].var
, PROBE_ARG_PARAMS
) ||
1708 !strcmp(pev
->args
[i
].var
, PROBE_ARG_VARS
))
1713 /* Return true if this perf_probe_event requires debuginfo */
1714 bool perf_probe_event_need_dwarf(struct perf_probe_event
*pev
)
1716 if (pev
->point
.file
|| pev
->point
.line
|| pev
->point
.lazy_line
)
1719 if (perf_probe_with_var(pev
))
1725 /* Parse probe_events event into struct probe_point */
1726 int parse_probe_trace_command(const char *cmd
, struct probe_trace_event
*tev
)
1728 struct probe_trace_point
*tp
= &tev
->point
;
1731 char *argv0_str
= NULL
, *fmt
, *fmt1_str
, *fmt2_str
, *fmt3_str
;
1735 pr_debug("Parsing probe_events: %s\n", cmd
);
1736 argv
= argv_split(cmd
, &argc
);
1738 pr_debug("Failed to split arguments.\n");
1742 semantic_error("Too few probe arguments.\n");
1747 /* Scan event and group name. */
1748 argv0_str
= strdup(argv
[0]);
1749 if (argv0_str
== NULL
) {
1753 fmt1_str
= strtok_r(argv0_str
, ":", &fmt
);
1754 fmt2_str
= strtok_r(NULL
, "/", &fmt
);
1755 fmt3_str
= strtok_r(NULL
, " \t", &fmt
);
1756 if (fmt1_str
== NULL
|| strlen(fmt1_str
) != 1 || fmt2_str
== NULL
1757 || fmt3_str
== NULL
) {
1758 semantic_error("Failed to parse event name: %s\n", argv
[0]);
1763 tev
->group
= strdup(fmt2_str
);
1764 tev
->event
= strdup(fmt3_str
);
1765 if (tev
->group
== NULL
|| tev
->event
== NULL
) {
1769 pr_debug("Group:%s Event:%s probe:%c\n", tev
->group
, tev
->event
, pr
);
1771 tp
->retprobe
= (pr
== 'r');
1773 /* Scan module name(if there), function name and offset */
1774 p
= strchr(argv
[1], ':');
1776 tp
->module
= strndup(argv
[1], p
- argv
[1]);
1781 tev
->uprobes
= (tp
->module
[0] == '/');
1785 fmt1_str
= strtok_r(p
, "+", &fmt
);
1786 /* only the address started with 0x */
1787 if (fmt1_str
[0] == '0') {
1789 * Fix a special case:
1790 * if address == 0, kernel reports something like:
1791 * p:probe_libc/abs_0 /lib/libc-2.18.so:0x (null) arg1=%ax
1792 * Newer kernel may fix that, but we want to
1793 * support old kernel also.
1795 if (strcmp(fmt1_str
, "0x") == 0) {
1796 if (!argv
[2] || strcmp(argv
[2], "(null)")) {
1803 for (i
= 2; argv
[i
+ 1] != NULL
; i
++)
1804 argv
[i
] = argv
[i
+ 1];
1809 tp
->address
= strtoul(fmt1_str
, NULL
, 0);
1811 /* Only the symbol-based probe has offset */
1812 tp
->symbol
= strdup(fmt1_str
);
1813 if (tp
->symbol
== NULL
) {
1817 fmt2_str
= strtok_r(NULL
, "", &fmt
);
1818 if (fmt2_str
== NULL
)
1821 tp
->offset
= strtoul(fmt2_str
, NULL
, 10);
1825 fmt2_str
= strchr(p
, '(');
1827 tp
->ref_ctr_offset
= strtoul(fmt2_str
+ 1, NULL
, 0);
1830 tev
->nargs
= argc
- 2;
1831 tev
->args
= zalloc(sizeof(struct probe_trace_arg
) * tev
->nargs
);
1832 if (tev
->args
== NULL
) {
1836 for (i
= 0; i
< tev
->nargs
; i
++) {
1837 p
= strchr(argv
[i
+ 2], '=');
1838 if (p
) /* We don't need which register is assigned. */
1842 tev
->args
[i
].name
= strdup(argv
[i
+ 2]);
1843 /* TODO: parse regs and offset */
1844 tev
->args
[i
].value
= strdup(p
);
1845 if (tev
->args
[i
].name
== NULL
|| tev
->args
[i
].value
== NULL
) {
1857 /* Compose only probe arg */
1858 char *synthesize_perf_probe_arg(struct perf_probe_arg
*pa
)
1860 struct perf_probe_arg_field
*field
= pa
->field
;
1865 if (strbuf_init(&buf
, 64) < 0)
1868 if (pa
->name
&& pa
->var
)
1869 err
= strbuf_addf(&buf
, "%s=%s", pa
->name
, pa
->var
);
1871 err
= strbuf_addstr(&buf
, pa
->name
?: pa
->var
);
1876 if (field
->name
[0] == '[')
1877 err
= strbuf_addstr(&buf
, field
->name
);
1879 err
= strbuf_addf(&buf
, "%s%s", field
->ref
? "->" : ".",
1881 field
= field
->next
;
1887 if (strbuf_addf(&buf
, ":%s", pa
->type
) < 0)
1890 ret
= strbuf_detach(&buf
, NULL
);
1892 strbuf_release(&buf
);
1896 /* Compose only probe point (not argument) */
1897 char *synthesize_perf_probe_point(struct perf_probe_point
*pp
)
1900 char *tmp
, *ret
= NULL
;
1903 if (strbuf_init(&buf
, 64) < 0)
1907 if (strbuf_addstr(&buf
, pp
->function
) < 0)
1910 err
= strbuf_addf(&buf
, "+%lu", pp
->offset
);
1912 err
= strbuf_addf(&buf
, ":%d", pp
->line
);
1913 else if (pp
->retprobe
)
1914 err
= strbuf_addstr(&buf
, "%return");
1922 tmp
= strchr(pp
->file
+ len
- 30, '/');
1923 tmp
= tmp
? tmp
+ 1 : pp
->file
+ len
- 30;
1925 err
= strbuf_addf(&buf
, "@%s", tmp
);
1926 if (!err
&& !pp
->function
&& pp
->line
)
1927 err
= strbuf_addf(&buf
, ":%d", pp
->line
);
1930 ret
= strbuf_detach(&buf
, NULL
);
1932 strbuf_release(&buf
);
1936 char *synthesize_perf_probe_command(struct perf_probe_event
*pev
)
1939 char *tmp
, *ret
= NULL
;
1942 if (strbuf_init(&buf
, 64))
1945 if (strbuf_addf(&buf
, "%s:%s=", pev
->group
?: PERFPROBE_GROUP
,
1949 tmp
= synthesize_perf_probe_point(&pev
->point
);
1950 if (!tmp
|| strbuf_addstr(&buf
, tmp
) < 0)
1954 for (i
= 0; i
< pev
->nargs
; i
++) {
1955 tmp
= synthesize_perf_probe_arg(pev
->args
+ i
);
1956 if (!tmp
|| strbuf_addf(&buf
, " %s", tmp
) < 0)
1961 ret
= strbuf_detach(&buf
, NULL
);
1963 strbuf_release(&buf
);
1967 static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref
*ref
,
1968 struct strbuf
*buf
, int depth
)
1972 depth
= __synthesize_probe_trace_arg_ref(ref
->next
, buf
,
1977 err
= strbuf_addf(buf
, "%+ld(", ref
->offset
);
1978 return (err
< 0) ? err
: depth
;
1981 static int synthesize_probe_trace_arg(struct probe_trace_arg
*arg
,
1984 struct probe_trace_arg_ref
*ref
= arg
->ref
;
1987 /* Argument name or separator */
1989 err
= strbuf_addf(buf
, " %s=", arg
->name
);
1991 err
= strbuf_addch(buf
, ' ');
1995 /* Special case: @XXX */
1996 if (arg
->value
[0] == '@' && arg
->ref
)
1999 /* Dereferencing arguments */
2001 depth
= __synthesize_probe_trace_arg_ref(ref
, buf
, 1);
2006 /* Print argument value */
2007 if (arg
->value
[0] == '@' && arg
->ref
)
2008 err
= strbuf_addf(buf
, "%s%+ld", arg
->value
, arg
->ref
->offset
);
2010 err
= strbuf_addstr(buf
, arg
->value
);
2013 while (!err
&& depth
--)
2014 err
= strbuf_addch(buf
, ')');
2016 /* Print argument type */
2017 if (!err
&& arg
->type
)
2018 err
= strbuf_addf(buf
, ":%s", arg
->type
);
2024 synthesize_uprobe_trace_def(struct probe_trace_event
*tev
, struct strbuf
*buf
)
2026 struct probe_trace_point
*tp
= &tev
->point
;
2029 err
= strbuf_addf(buf
, "%s:0x%lx", tp
->module
, tp
->address
);
2031 if (err
>= 0 && tp
->ref_ctr_offset
) {
2032 if (!uprobe_ref_ctr_is_supported())
2034 err
= strbuf_addf(buf
, "(0x%lx)", tp
->ref_ctr_offset
);
2036 return err
>= 0 ? 0 : -1;
2039 char *synthesize_probe_trace_command(struct probe_trace_event
*tev
)
2041 struct probe_trace_point
*tp
= &tev
->point
;
2046 /* Uprobes must have tp->module */
2047 if (tev
->uprobes
&& !tp
->module
)
2050 if (strbuf_init(&buf
, 32) < 0)
2053 if (strbuf_addf(&buf
, "%c:%s/%s ", tp
->retprobe
? 'r' : 'p',
2054 tev
->group
, tev
->event
) < 0)
2057 * If tp->address == 0, then this point must be a
2058 * absolute address uprobe.
2059 * try_to_find_absolute_address() should have made
2060 * tp->symbol to "0x0".
2062 if (tev
->uprobes
&& !tp
->address
) {
2063 if (!tp
->symbol
|| strcmp(tp
->symbol
, "0x0"))
2067 /* Use the tp->address for uprobes */
2069 err
= synthesize_uprobe_trace_def(tev
, &buf
);
2070 } else if (!strncmp(tp
->symbol
, "0x", 2)) {
2071 /* Absolute address. See try_to_find_absolute_address() */
2072 err
= strbuf_addf(&buf
, "%s%s0x%lx", tp
->module
?: "",
2073 tp
->module
? ":" : "", tp
->address
);
2075 err
= strbuf_addf(&buf
, "%s%s%s+%lu", tp
->module
?: "",
2076 tp
->module
? ":" : "", tp
->symbol
, tp
->offset
);
2082 for (i
= 0; i
< tev
->nargs
; i
++)
2083 if (synthesize_probe_trace_arg(&tev
->args
[i
], &buf
) < 0)
2086 ret
= strbuf_detach(&buf
, NULL
);
2088 strbuf_release(&buf
);
2092 static int find_perf_probe_point_from_map(struct probe_trace_point
*tp
,
2093 struct perf_probe_point
*pp
,
2096 struct symbol
*sym
= NULL
;
2097 struct map
*map
= NULL
;
2098 u64 addr
= tp
->address
;
2102 map
= dso__new_map(tp
->module
);
2105 sym
= map__find_symbol(map
, addr
);
2107 if (tp
->symbol
&& !addr
) {
2108 if (kernel_get_symbol_address_by_name(tp
->symbol
,
2109 &addr
, true, false) < 0)
2114 sym
= machine__find_kernel_symbol(host_machine
, addr
, &map
);
2121 pp
->retprobe
= tp
->retprobe
;
2122 pp
->offset
= addr
- map
->unmap_ip(map
, sym
->start
);
2123 pp
->function
= strdup(sym
->name
);
2124 ret
= pp
->function
? 0 : -ENOMEM
;
2127 if (map
&& !is_kprobe
) {
2134 static int convert_to_perf_probe_point(struct probe_trace_point
*tp
,
2135 struct perf_probe_point
*pp
,
2141 ret
= find_perf_probe_point_from_dwarf(tp
, pp
, is_kprobe
);
2144 ret
= find_perf_probe_point_from_map(tp
, pp
, is_kprobe
);
2148 pr_debug("Failed to find probe point from both of dwarf and map.\n");
2151 pp
->function
= strdup(tp
->symbol
);
2152 pp
->offset
= tp
->offset
;
2154 ret
= e_snprintf(buf
, 128, "0x%" PRIx64
, (u64
)tp
->address
);
2157 pp
->function
= strdup(buf
);
2160 if (pp
->function
== NULL
)
2163 pp
->retprobe
= tp
->retprobe
;
2168 static int convert_to_perf_probe_event(struct probe_trace_event
*tev
,
2169 struct perf_probe_event
*pev
, bool is_kprobe
)
2171 struct strbuf buf
= STRBUF_INIT
;
2174 /* Convert event/group name */
2175 pev
->event
= strdup(tev
->event
);
2176 pev
->group
= strdup(tev
->group
);
2177 if (pev
->event
== NULL
|| pev
->group
== NULL
)
2180 /* Convert trace_point to probe_point */
2181 ret
= convert_to_perf_probe_point(&tev
->point
, &pev
->point
, is_kprobe
);
2185 /* Convert trace_arg to probe_arg */
2186 pev
->nargs
= tev
->nargs
;
2187 pev
->args
= zalloc(sizeof(struct perf_probe_arg
) * pev
->nargs
);
2188 if (pev
->args
== NULL
)
2190 for (i
= 0; i
< tev
->nargs
&& ret
>= 0; i
++) {
2191 if (tev
->args
[i
].name
)
2192 pev
->args
[i
].name
= strdup(tev
->args
[i
].name
);
2194 if ((ret
= strbuf_init(&buf
, 32)) < 0)
2196 ret
= synthesize_probe_trace_arg(&tev
->args
[i
], &buf
);
2197 pev
->args
[i
].name
= strbuf_detach(&buf
, NULL
);
2199 if (pev
->args
[i
].name
== NULL
&& ret
>= 0)
2204 clear_perf_probe_event(pev
);
2209 void clear_perf_probe_event(struct perf_probe_event
*pev
)
2211 struct perf_probe_arg_field
*field
, *next
;
2217 clear_perf_probe_point(&pev
->point
);
2219 for (i
= 0; i
< pev
->nargs
; i
++) {
2220 free(pev
->args
[i
].name
);
2221 free(pev
->args
[i
].var
);
2222 free(pev
->args
[i
].type
);
2223 field
= pev
->args
[i
].field
;
2226 zfree(&field
->name
);
2232 memset(pev
, 0, sizeof(*pev
));
2235 #define strdup_or_goto(str, label) \
2236 ({ char *__p = NULL; if (str && !(__p = strdup(str))) goto label; __p; })
2238 static int perf_probe_point__copy(struct perf_probe_point
*dst
,
2239 struct perf_probe_point
*src
)
2241 dst
->file
= strdup_or_goto(src
->file
, out_err
);
2242 dst
->function
= strdup_or_goto(src
->function
, out_err
);
2243 dst
->lazy_line
= strdup_or_goto(src
->lazy_line
, out_err
);
2244 dst
->line
= src
->line
;
2245 dst
->retprobe
= src
->retprobe
;
2246 dst
->offset
= src
->offset
;
2250 clear_perf_probe_point(dst
);
2254 static int perf_probe_arg__copy(struct perf_probe_arg
*dst
,
2255 struct perf_probe_arg
*src
)
2257 struct perf_probe_arg_field
*field
, **ppfield
;
2259 dst
->name
= strdup_or_goto(src
->name
, out_err
);
2260 dst
->var
= strdup_or_goto(src
->var
, out_err
);
2261 dst
->type
= strdup_or_goto(src
->type
, out_err
);
2264 ppfield
= &(dst
->field
);
2266 *ppfield
= zalloc(sizeof(*field
));
2269 (*ppfield
)->name
= strdup_or_goto(field
->name
, out_err
);
2270 (*ppfield
)->index
= field
->index
;
2271 (*ppfield
)->ref
= field
->ref
;
2272 field
= field
->next
;
2273 ppfield
= &((*ppfield
)->next
);
2280 int perf_probe_event__copy(struct perf_probe_event
*dst
,
2281 struct perf_probe_event
*src
)
2285 dst
->event
= strdup_or_goto(src
->event
, out_err
);
2286 dst
->group
= strdup_or_goto(src
->group
, out_err
);
2287 dst
->target
= strdup_or_goto(src
->target
, out_err
);
2288 dst
->uprobes
= src
->uprobes
;
2290 if (perf_probe_point__copy(&dst
->point
, &src
->point
) < 0)
2293 dst
->args
= zalloc(sizeof(struct perf_probe_arg
) * src
->nargs
);
2296 dst
->nargs
= src
->nargs
;
2298 for (i
= 0; i
< src
->nargs
; i
++)
2299 if (perf_probe_arg__copy(&dst
->args
[i
], &src
->args
[i
]) < 0)
2304 clear_perf_probe_event(dst
);
2308 void clear_probe_trace_event(struct probe_trace_event
*tev
)
2310 struct probe_trace_arg_ref
*ref
, *next
;
2315 free(tev
->point
.symbol
);
2316 free(tev
->point
.realname
);
2317 free(tev
->point
.module
);
2318 for (i
= 0; i
< tev
->nargs
; i
++) {
2319 free(tev
->args
[i
].name
);
2320 free(tev
->args
[i
].value
);
2321 free(tev
->args
[i
].type
);
2322 ref
= tev
->args
[i
].ref
;
2330 memset(tev
, 0, sizeof(*tev
));
2333 struct kprobe_blacklist_node
{
2334 struct list_head list
;
2335 unsigned long start
;
2340 static void kprobe_blacklist__delete(struct list_head
*blacklist
)
2342 struct kprobe_blacklist_node
*node
;
2344 while (!list_empty(blacklist
)) {
2345 node
= list_first_entry(blacklist
,
2346 struct kprobe_blacklist_node
, list
);
2347 list_del(&node
->list
);
2353 static int kprobe_blacklist__load(struct list_head
*blacklist
)
2355 struct kprobe_blacklist_node
*node
;
2356 const char *__debugfs
= debugfs__mountpoint();
2357 char buf
[PATH_MAX
], *p
;
2361 if (__debugfs
== NULL
)
2364 ret
= e_snprintf(buf
, PATH_MAX
, "%s/kprobes/blacklist", __debugfs
);
2368 fp
= fopen(buf
, "r");
2373 while (fgets(buf
, PATH_MAX
, fp
)) {
2374 node
= zalloc(sizeof(*node
));
2379 INIT_LIST_HEAD(&node
->list
);
2380 list_add_tail(&node
->list
, blacklist
);
2381 if (sscanf(buf
, "0x%lx-0x%lx", &node
->start
, &node
->end
) != 2) {
2385 p
= strchr(buf
, '\t');
2388 if (p
[strlen(p
) - 1] == '\n')
2389 p
[strlen(p
) - 1] = '\0';
2391 p
= (char *)"unknown";
2392 node
->symbol
= strdup(p
);
2393 if (!node
->symbol
) {
2397 pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
2398 node
->start
, node
->end
, node
->symbol
);
2402 kprobe_blacklist__delete(blacklist
);
2408 static struct kprobe_blacklist_node
*
2409 kprobe_blacklist__find_by_address(struct list_head
*blacklist
,
2410 unsigned long address
)
2412 struct kprobe_blacklist_node
*node
;
2414 list_for_each_entry(node
, blacklist
, list
) {
2415 if (node
->start
<= address
&& address
< node
->end
)
2422 static LIST_HEAD(kprobe_blacklist
);
2424 static void kprobe_blacklist__init(void)
2426 if (!list_empty(&kprobe_blacklist
))
2429 if (kprobe_blacklist__load(&kprobe_blacklist
) < 0)
2430 pr_debug("No kprobe blacklist support, ignored\n");
2433 static void kprobe_blacklist__release(void)
2435 kprobe_blacklist__delete(&kprobe_blacklist
);
2438 static bool kprobe_blacklist__listed(unsigned long address
)
2440 return !!kprobe_blacklist__find_by_address(&kprobe_blacklist
, address
);
2443 static int perf_probe_event__sprintf(const char *group
, const char *event
,
2444 struct perf_probe_event
*pev
,
2446 struct strbuf
*result
)
2451 if (asprintf(&buf
, "%s:%s", group
, event
) < 0)
2453 ret
= strbuf_addf(result
, " %-20s (on ", buf
);
2458 /* Synthesize only event probe point */
2459 buf
= synthesize_perf_probe_point(&pev
->point
);
2462 ret
= strbuf_addstr(result
, buf
);
2466 ret
= strbuf_addf(result
, " in %s", module
);
2468 if (!ret
&& pev
->nargs
> 0) {
2469 ret
= strbuf_add(result
, " with", 5);
2470 for (i
= 0; !ret
&& i
< pev
->nargs
; i
++) {
2471 buf
= synthesize_perf_probe_arg(&pev
->args
[i
]);
2474 ret
= strbuf_addf(result
, " %s", buf
);
2479 ret
= strbuf_addch(result
, ')');
2485 int show_perf_probe_event(const char *group
, const char *event
,
2486 struct perf_probe_event
*pev
,
2487 const char *module
, bool use_stdout
)
2489 struct strbuf buf
= STRBUF_INIT
;
2492 ret
= perf_probe_event__sprintf(group
, event
, pev
, module
, &buf
);
2495 printf("%s\n", buf
.buf
);
2497 pr_info("%s\n", buf
.buf
);
2499 strbuf_release(&buf
);
2504 static bool filter_probe_trace_event(struct probe_trace_event
*tev
,
2505 struct strfilter
*filter
)
2509 /* At first, check the event name itself */
2510 if (strfilter__compare(filter
, tev
->event
))
2513 /* Next, check the combination of name and group */
2514 if (e_snprintf(tmp
, 128, "%s:%s", tev
->group
, tev
->event
) < 0)
2516 return strfilter__compare(filter
, tmp
);
2519 static int __show_perf_probe_events(int fd
, bool is_kprobe
,
2520 struct strfilter
*filter
)
2523 struct probe_trace_event tev
;
2524 struct perf_probe_event pev
;
2525 struct strlist
*rawlist
;
2526 struct str_node
*ent
;
2528 memset(&tev
, 0, sizeof(tev
));
2529 memset(&pev
, 0, sizeof(pev
));
2531 rawlist
= probe_file__get_rawlist(fd
);
2535 strlist__for_each_entry(ent
, rawlist
) {
2536 ret
= parse_probe_trace_command(ent
->s
, &tev
);
2538 if (!filter_probe_trace_event(&tev
, filter
))
2540 ret
= convert_to_perf_probe_event(&tev
, &pev
,
2544 ret
= show_perf_probe_event(pev
.group
, pev
.event
,
2545 &pev
, tev
.point
.module
,
2549 clear_perf_probe_event(&pev
);
2550 clear_probe_trace_event(&tev
);
2554 strlist__delete(rawlist
);
2555 /* Cleanup cached debuginfo if needed */
2556 debuginfo_cache__exit();
2561 /* List up current perf-probe events */
2562 int show_perf_probe_events(struct strfilter
*filter
)
2564 int kp_fd
, up_fd
, ret
;
2568 if (probe_conf
.cache
)
2569 return probe_cache__show_all_caches(filter
);
2571 ret
= init_probe_symbol_maps(false);
2575 ret
= probe_file__open_both(&kp_fd
, &up_fd
, 0);
2580 ret
= __show_perf_probe_events(kp_fd
, true, filter
);
2581 if (up_fd
>= 0 && ret
>= 0)
2582 ret
= __show_perf_probe_events(up_fd
, false, filter
);
2587 exit_probe_symbol_maps();
2592 static int get_new_event_name(char *buf
, size_t len
, const char *base
,
2593 struct strlist
*namelist
, bool ret_event
,
2601 nbase
= strdup(base
);
2605 /* Cut off the dot suffixes (e.g. .const, .isra) and version suffixes */
2606 p
= strpbrk(nbase
, ".@");
2607 if (p
&& p
!= nbase
)
2610 /* Try no suffix number */
2611 ret
= e_snprintf(buf
, len
, "%s%s", nbase
, ret_event
? "__return" : "");
2613 pr_debug("snprintf() failed: %d\n", ret
);
2616 if (!strlist__has_entry(namelist
, buf
))
2619 if (!allow_suffix
) {
2620 pr_warning("Error: event \"%s\" already exists.\n"
2621 " Hint: Remove existing event by 'perf probe -d'\n"
2622 " or force duplicates by 'perf probe -f'\n"
2623 " or set 'force=yes' in BPF source.\n",
2629 /* Try to add suffix */
2630 for (i
= 1; i
< MAX_EVENT_INDEX
; i
++) {
2631 ret
= e_snprintf(buf
, len
, "%s_%d", nbase
, i
);
2633 pr_debug("snprintf() failed: %d\n", ret
);
2636 if (!strlist__has_entry(namelist
, buf
))
2639 if (i
== MAX_EVENT_INDEX
) {
2640 pr_warning("Too many events are on the same function.\n");
2647 /* Final validation */
2648 if (ret
>= 0 && !is_c_func_name(buf
)) {
2649 pr_warning("Internal error: \"%s\" is an invalid event name.\n",
2657 /* Warn if the current kernel's uprobe implementation is old */
2658 static void warn_uprobe_event_compat(struct probe_trace_event
*tev
)
2661 char *buf
= synthesize_probe_trace_command(tev
);
2662 struct probe_trace_point
*tp
= &tev
->point
;
2664 if (tp
->ref_ctr_offset
&& !uprobe_ref_ctr_is_supported()) {
2665 pr_warning("A semaphore is associated with %s:%s and "
2666 "seems your kernel doesn't support it.\n",
2667 tev
->group
, tev
->event
);
2670 /* Old uprobe event doesn't support memory dereference */
2671 if (!tev
->uprobes
|| tev
->nargs
== 0 || !buf
)
2674 for (i
= 0; i
< tev
->nargs
; i
++)
2675 if (strglobmatch(tev
->args
[i
].value
, "[$@+-]*")) {
2676 pr_warning("Please upgrade your kernel to at least "
2677 "3.14 to have access to feature %s\n",
2678 tev
->args
[i
].value
);
2685 /* Set new name from original perf_probe_event and namelist */
2686 static int probe_trace_event__set_name(struct probe_trace_event
*tev
,
2687 struct perf_probe_event
*pev
,
2688 struct strlist
*namelist
,
2691 const char *event
, *group
;
2695 /* If probe_event or trace_event already have the name, reuse it */
2696 if (pev
->event
&& !pev
->sdt
)
2698 else if (tev
->event
)
2701 /* Or generate new one from probe point */
2702 if (pev
->point
.function
&&
2703 (strncmp(pev
->point
.function
, "0x", 2) != 0) &&
2704 !strisglob(pev
->point
.function
))
2705 event
= pev
->point
.function
;
2707 event
= tev
->point
.realname
;
2709 if (pev
->group
&& !pev
->sdt
)
2711 else if (tev
->group
)
2714 group
= PERFPROBE_GROUP
;
2716 /* Get an unused new event name */
2717 ret
= get_new_event_name(buf
, 64, event
, namelist
,
2718 tev
->point
.retprobe
, allow_suffix
);
2724 tev
->event
= strdup(event
);
2725 tev
->group
= strdup(group
);
2726 if (tev
->event
== NULL
|| tev
->group
== NULL
)
2729 /* Add added event name to namelist */
2730 strlist__add(namelist
, event
);
2734 static int __open_probe_file_and_namelist(bool uprobe
,
2735 struct strlist
**namelist
)
2739 fd
= probe_file__open(PF_FL_RW
| (uprobe
? PF_FL_UPROBE
: 0));
2743 /* Get current event names */
2744 *namelist
= probe_file__get_namelist(fd
);
2746 pr_debug("Failed to get current event list.\n");
2753 static int __add_probe_trace_events(struct perf_probe_event
*pev
,
2754 struct probe_trace_event
*tevs
,
2755 int ntevs
, bool allow_suffix
)
2757 int i
, fd
[2] = {-1, -1}, up
, ret
;
2758 struct probe_trace_event
*tev
= NULL
;
2759 struct probe_cache
*cache
= NULL
;
2760 struct strlist
*namelist
[2] = {NULL
, NULL
};
2761 struct nscookie nsc
;
2763 up
= pev
->uprobes
? 1 : 0;
2764 fd
[up
] = __open_probe_file_and_namelist(up
, &namelist
[up
]);
2769 for (i
= 0; i
< ntevs
; i
++) {
2771 up
= tev
->uprobes
? 1 : 0;
2772 if (fd
[up
] == -1) { /* Open the kprobe/uprobe_events */
2773 fd
[up
] = __open_probe_file_and_namelist(up
,
2778 /* Skip if the symbol is out of .text or blacklisted */
2779 if (!tev
->point
.symbol
&& !pev
->uprobes
)
2782 /* Set new name for tev (and update namelist) */
2783 ret
= probe_trace_event__set_name(tev
, pev
, namelist
[up
],
2788 nsinfo__mountns_enter(pev
->nsi
, &nsc
);
2789 ret
= probe_file__add_event(fd
[up
], tev
);
2790 nsinfo__mountns_exit(&nsc
);
2795 * Probes after the first probe which comes from same
2796 * user input are always allowed to add suffix, because
2797 * there might be several addresses corresponding to
2800 allow_suffix
= true;
2802 if (ret
== -EINVAL
&& pev
->uprobes
)
2803 warn_uprobe_event_compat(tev
);
2804 if (ret
== 0 && probe_conf
.cache
) {
2805 cache
= probe_cache__new(pev
->target
, pev
->nsi
);
2807 probe_cache__add_entry(cache
, pev
, tevs
, ntevs
) < 0 ||
2808 probe_cache__commit(cache
) < 0)
2809 pr_warning("Failed to add event to probe cache\n");
2810 probe_cache__delete(cache
);
2814 for (up
= 0; up
< 2; up
++) {
2815 strlist__delete(namelist
[up
]);
2822 static int find_probe_functions(struct map
*map
, char *name
,
2823 struct symbol
**syms
)
2827 struct rb_node
*tmp
;
2828 const char *norm
, *ver
;
2830 bool cut_version
= true;
2832 if (map__load(map
) < 0)
2835 /* If user gives a version, don't cut off the version from symbols */
2836 if (strchr(name
, '@'))
2837 cut_version
= false;
2839 map__for_each_symbol(map
, sym
, tmp
) {
2840 norm
= arch__normalize_symbol_name(sym
->name
);
2845 /* We don't care about default symbol or not */
2846 ver
= strchr(norm
, '@');
2848 buf
= strndup(norm
, ver
- norm
);
2855 if (strglobmatch(norm
, name
)) {
2857 if (syms
&& found
< probe_conf
.max_probes
)
2858 syms
[found
- 1] = sym
;
2867 void __weak
arch__fix_tev_from_maps(struct perf_probe_event
*pev __maybe_unused
,
2868 struct probe_trace_event
*tev __maybe_unused
,
2869 struct map
*map __maybe_unused
,
2870 struct symbol
*sym __maybe_unused
) { }
2873 * Find probe function addresses from map.
2874 * Return an error or the number of found probe_trace_event
2876 static int find_probe_trace_events_from_map(struct perf_probe_event
*pev
,
2877 struct probe_trace_event
**tevs
)
2879 struct map
*map
= NULL
;
2880 struct ref_reloc_sym
*reloc_sym
= NULL
;
2882 struct symbol
**syms
= NULL
;
2883 struct probe_trace_event
*tev
;
2884 struct perf_probe_point
*pp
= &pev
->point
;
2885 struct probe_trace_point
*tp
;
2886 int num_matched_functions
;
2887 int ret
, i
, j
, skipped
= 0;
2890 map
= get_target_map(pev
->target
, pev
->nsi
, pev
->uprobes
);
2896 syms
= malloc(sizeof(struct symbol
*) * probe_conf
.max_probes
);
2903 * Load matched symbols: Since the different local symbols may have
2904 * same name but different addresses, this lists all the symbols.
2906 num_matched_functions
= find_probe_functions(map
, pp
->function
, syms
);
2907 if (num_matched_functions
<= 0) {
2908 pr_err("Failed to find symbol %s in %s\n", pp
->function
,
2909 pev
->target
? : "kernel");
2912 } else if (num_matched_functions
> probe_conf
.max_probes
) {
2913 pr_err("Too many functions matched in %s\n",
2914 pev
->target
? : "kernel");
2919 /* Note that the symbols in the kmodule are not relocated */
2920 if (!pev
->uprobes
&& !pev
->target
&&
2921 (!pp
->retprobe
|| kretprobe_offset_is_supported())) {
2922 reloc_sym
= kernel_get_ref_reloc_sym();
2924 pr_warning("Relocated base symbol is not found!\n");
2930 /* Setup result trace-probe-events */
2931 *tevs
= zalloc(sizeof(*tev
) * num_matched_functions
);
2939 for (j
= 0; j
< num_matched_functions
; j
++) {
2942 tev
= (*tevs
) + ret
;
2944 if (ret
== num_matched_functions
) {
2945 pr_warning("Too many symbols are listed. Skip it.\n");
2950 if (pp
->offset
> sym
->end
- sym
->start
) {
2951 pr_warning("Offset %ld is bigger than the size of %s\n",
2952 pp
->offset
, sym
->name
);
2956 /* Add one probe point */
2957 tp
->address
= map
->unmap_ip(map
, sym
->start
) + pp
->offset
;
2959 /* Check the kprobe (not in module) is within .text */
2960 if (!pev
->uprobes
&& !pev
->target
&&
2961 kprobe_warn_out_range(sym
->name
, tp
->address
)) {
2962 tp
->symbol
= NULL
; /* Skip it */
2964 } else if (reloc_sym
) {
2965 tp
->symbol
= strdup_or_goto(reloc_sym
->name
, nomem_out
);
2966 tp
->offset
= tp
->address
- reloc_sym
->addr
;
2968 tp
->symbol
= strdup_or_goto(sym
->name
, nomem_out
);
2969 tp
->offset
= pp
->offset
;
2971 tp
->realname
= strdup_or_goto(sym
->name
, nomem_out
);
2973 tp
->retprobe
= pp
->retprobe
;
2976 tev
->point
.module
= strdup_or_goto(pev
->target
,
2979 mod_name
= find_module_name(pev
->target
);
2981 strdup(mod_name
? mod_name
: pev
->target
);
2983 if (!tev
->point
.module
)
2987 tev
->uprobes
= pev
->uprobes
;
2988 tev
->nargs
= pev
->nargs
;
2990 tev
->args
= zalloc(sizeof(struct probe_trace_arg
) *
2992 if (tev
->args
== NULL
)
2995 for (i
= 0; i
< tev
->nargs
; i
++) {
2996 if (pev
->args
[i
].name
)
2998 strdup_or_goto(pev
->args
[i
].name
,
3001 tev
->args
[i
].value
= strdup_or_goto(pev
->args
[i
].var
,
3003 if (pev
->args
[i
].type
)
3005 strdup_or_goto(pev
->args
[i
].type
,
3008 arch__fix_tev_from_maps(pev
, tev
, map
, sym
);
3010 if (ret
== skipped
) {
3023 clear_probe_trace_events(*tevs
, num_matched_functions
);
3028 static int try_to_find_absolute_address(struct perf_probe_event
*pev
,
3029 struct probe_trace_event
**tevs
)
3031 struct perf_probe_point
*pp
= &pev
->point
;
3032 struct probe_trace_event
*tev
;
3033 struct probe_trace_point
*tp
;
3036 if (!(pev
->point
.function
&& !strncmp(pev
->point
.function
, "0x", 2)))
3038 if (perf_probe_event_need_dwarf(pev
))
3042 * This is 'perf probe /lib/libc.so 0xabcd'. Try to probe at
3045 * Only one tev can be generated by this.
3047 *tevs
= zalloc(sizeof(*tev
));
3055 * Don't use tp->offset, use address directly, because
3056 * in synthesize_probe_trace_command() address cannot be
3059 tp
->address
= pev
->point
.abs_address
;
3060 tp
->retprobe
= pp
->retprobe
;
3061 tev
->uprobes
= pev
->uprobes
;
3065 * Give it a '0x' leading symbol name.
3066 * In __add_probe_trace_events, a NULL symbol is interpreted as
3069 if (asprintf(&tp
->symbol
, "0x%lx", tp
->address
) < 0)
3072 /* For kprobe, check range */
3073 if ((!tev
->uprobes
) &&
3074 (kprobe_warn_out_range(tev
->point
.symbol
,
3075 tev
->point
.address
))) {
3080 if (asprintf(&tp
->realname
, "abs_%lx", tp
->address
) < 0)
3084 tp
->module
= strdup(pev
->target
);
3090 tev
->group
= strdup(pev
->group
);
3096 tev
->event
= strdup(pev
->event
);
3101 tev
->nargs
= pev
->nargs
;
3102 tev
->args
= zalloc(sizeof(struct probe_trace_arg
) * tev
->nargs
);
3106 for (i
= 0; i
< tev
->nargs
; i
++)
3107 copy_to_probe_trace_arg(&tev
->args
[i
], &pev
->args
[i
]);
3112 clear_probe_trace_events(*tevs
, 1);
3117 /* Concatinate two arrays */
3118 static void *memcat(void *a
, size_t sz_a
, void *b
, size_t sz_b
)
3122 ret
= malloc(sz_a
+ sz_b
);
3124 memcpy(ret
, a
, sz_a
);
3125 memcpy(ret
+ sz_a
, b
, sz_b
);
3131 concat_probe_trace_events(struct probe_trace_event
**tevs
, int *ntevs
,
3132 struct probe_trace_event
**tevs2
, int ntevs2
)
3134 struct probe_trace_event
*new_tevs
;
3144 if (*ntevs
+ ntevs2
> probe_conf
.max_probes
)
3147 /* Concatinate the array of probe_trace_event */
3148 new_tevs
= memcat(*tevs
, (*ntevs
) * sizeof(**tevs
),
3149 *tevs2
, ntevs2
* sizeof(**tevs2
));
3159 clear_probe_trace_events(*tevs2
, ntevs2
);
3166 * Try to find probe_trace_event from given probe caches. Return the number
3167 * of cached events found, if an error occurs return the error.
3169 static int find_cached_events(struct perf_probe_event
*pev
,
3170 struct probe_trace_event
**tevs
,
3173 struct probe_cache
*cache
;
3174 struct probe_cache_entry
*entry
;
3175 struct probe_trace_event
*tmp_tevs
= NULL
;
3179 cache
= probe_cache__new(target
, pev
->nsi
);
3180 /* Return 0 ("not found") if the target has no probe cache. */
3184 for_each_probe_cache_entry(entry
, cache
) {
3185 /* Skip the cache entry which has no name */
3186 if (!entry
->pev
.event
|| !entry
->pev
.group
)
3188 if ((!pev
->group
|| strglobmatch(entry
->pev
.group
, pev
->group
)) &&
3189 strglobmatch(entry
->pev
.event
, pev
->event
)) {
3190 ret
= probe_cache_entry__get_event(entry
, &tmp_tevs
);
3192 ret
= concat_probe_trace_events(tevs
, &ntevs
,
3198 probe_cache__delete(cache
);
3200 clear_probe_trace_events(*tevs
, ntevs
);
3204 if (ntevs
> 0 && target
&& target
[0] == '/')
3205 pev
->uprobes
= true;
3211 /* Try to find probe_trace_event from all probe caches */
3212 static int find_cached_events_all(struct perf_probe_event
*pev
,
3213 struct probe_trace_event
**tevs
)
3215 struct probe_trace_event
*tmp_tevs
= NULL
;
3216 struct strlist
*bidlist
;
3217 struct str_node
*nd
;
3222 /* Get the buildid list of all valid caches */
3223 bidlist
= build_id_cache__list_all(true);
3226 pr_debug("Failed to get buildids: %d\n", ret
);
3231 strlist__for_each_entry(nd
, bidlist
) {
3232 pathname
= build_id_cache__origname(nd
->s
);
3233 ret
= find_cached_events(pev
, &tmp_tevs
, pathname
);
3234 /* In the case of cnt == 0, we just skip it */
3236 ret
= concat_probe_trace_events(tevs
, &ntevs
,
3242 strlist__delete(bidlist
);
3245 clear_probe_trace_events(*tevs
, ntevs
);
3253 static int find_probe_trace_events_from_cache(struct perf_probe_event
*pev
,
3254 struct probe_trace_event
**tevs
)
3256 struct probe_cache
*cache
;
3257 struct probe_cache_entry
*entry
;
3258 struct probe_trace_event
*tev
;
3259 struct str_node
*node
;
3263 /* For SDT/cached events, we use special search functions */
3265 return find_cached_events_all(pev
, tevs
);
3267 return find_cached_events(pev
, tevs
, pev
->target
);
3269 cache
= probe_cache__new(pev
->target
, pev
->nsi
);
3273 entry
= probe_cache__find(cache
, pev
);
3275 /* SDT must be in the cache */
3276 ret
= pev
->sdt
? -ENOENT
: 0;
3280 ret
= strlist__nr_entries(entry
->tevlist
);
3281 if (ret
> probe_conf
.max_probes
) {
3282 pr_debug("Too many entries matched in the cache of %s\n",
3283 pev
->target
? : "kernel");
3288 *tevs
= zalloc(ret
* sizeof(*tev
));
3295 strlist__for_each_entry(node
, entry
->tevlist
) {
3296 tev
= &(*tevs
)[i
++];
3297 ret
= parse_probe_trace_command(node
->s
, tev
);
3300 /* Set the uprobes attribute as same as original */
3301 tev
->uprobes
= pev
->uprobes
;
3306 probe_cache__delete(cache
);
3310 static int convert_to_probe_trace_events(struct perf_probe_event
*pev
,
3311 struct probe_trace_event
**tevs
)
3315 if (!pev
->group
&& !pev
->sdt
) {
3316 /* Set group name if not given */
3317 if (!pev
->uprobes
) {
3318 pev
->group
= strdup(PERFPROBE_GROUP
);
3319 ret
= pev
->group
? 0 : -ENOMEM
;
3321 ret
= convert_exec_to_group(pev
->target
, &pev
->group
);
3323 pr_warning("Failed to make a group name.\n");
3328 ret
= try_to_find_absolute_address(pev
, tevs
);
3332 /* At first, we need to lookup cache entry */
3333 ret
= find_probe_trace_events_from_cache(pev
, tevs
);
3334 if (ret
> 0 || pev
->sdt
) /* SDT can be found only in the cache */
3335 return ret
== 0 ? -ENOENT
: ret
; /* Found in probe cache */
3337 /* Convert perf_probe_event with debuginfo */
3338 ret
= try_to_find_probe_trace_events(pev
, tevs
);
3340 return ret
; /* Found in debuginfo or got an error */
3342 return find_probe_trace_events_from_map(pev
, tevs
);
3345 int convert_perf_probe_events(struct perf_probe_event
*pevs
, int npevs
)
3349 /* Loop 1: convert all events */
3350 for (i
= 0; i
< npevs
; i
++) {
3351 /* Init kprobe blacklist if needed */
3352 if (!pevs
[i
].uprobes
)
3353 kprobe_blacklist__init();
3354 /* Convert with or without debuginfo */
3355 ret
= convert_to_probe_trace_events(&pevs
[i
], &pevs
[i
].tevs
);
3358 pevs
[i
].ntevs
= ret
;
3360 /* This just release blacklist only if allocated */
3361 kprobe_blacklist__release();
3366 static int show_probe_trace_event(struct probe_trace_event
*tev
)
3368 char *buf
= synthesize_probe_trace_command(tev
);
3371 pr_debug("Failed to synthesize probe trace event.\n");
3375 /* Showing definition always go stdout */
3376 printf("%s\n", buf
);
3382 int show_probe_trace_events(struct perf_probe_event
*pevs
, int npevs
)
3384 struct strlist
*namelist
= strlist__new(NULL
, NULL
);
3385 struct probe_trace_event
*tev
;
3386 struct perf_probe_event
*pev
;
3392 for (j
= 0; j
< npevs
&& !ret
; j
++) {
3394 for (i
= 0; i
< pev
->ntevs
&& !ret
; i
++) {
3395 tev
= &pev
->tevs
[i
];
3396 /* Skip if the symbol is out of .text or blacklisted */
3397 if (!tev
->point
.symbol
&& !pev
->uprobes
)
3400 /* Set new name for tev (and update namelist) */
3401 ret
= probe_trace_event__set_name(tev
, pev
,
3404 ret
= show_probe_trace_event(tev
);
3407 strlist__delete(namelist
);
3412 int apply_perf_probe_events(struct perf_probe_event
*pevs
, int npevs
)
3416 /* Loop 2: add all events */
3417 for (i
= 0; i
< npevs
; i
++) {
3418 ret
= __add_probe_trace_events(&pevs
[i
], pevs
[i
].tevs
,
3420 probe_conf
.force_add
);
3427 void cleanup_perf_probe_events(struct perf_probe_event
*pevs
, int npevs
)
3430 struct perf_probe_event
*pev
;
3432 /* Loop 3: cleanup and free trace events */
3433 for (i
= 0; i
< npevs
; i
++) {
3435 for (j
= 0; j
< pevs
[i
].ntevs
; j
++)
3436 clear_probe_trace_event(&pevs
[i
].tevs
[j
]);
3437 zfree(&pevs
[i
].tevs
);
3439 nsinfo__zput(pev
->nsi
);
3440 clear_perf_probe_event(&pevs
[i
]);
3444 int add_perf_probe_events(struct perf_probe_event
*pevs
, int npevs
)
3448 ret
= init_probe_symbol_maps(pevs
->uprobes
);
3452 ret
= convert_perf_probe_events(pevs
, npevs
);
3454 ret
= apply_perf_probe_events(pevs
, npevs
);
3456 cleanup_perf_probe_events(pevs
, npevs
);
3458 exit_probe_symbol_maps();
3462 int del_perf_probe_events(struct strfilter
*filter
)
3464 int ret
, ret2
, ufd
= -1, kfd
= -1;
3465 char *str
= strfilter__string(filter
);
3470 /* Get current event names */
3471 ret
= probe_file__open_both(&kfd
, &ufd
, PF_FL_RW
);
3475 ret
= probe_file__del_events(kfd
, filter
);
3476 if (ret
< 0 && ret
!= -ENOENT
)
3479 ret2
= probe_file__del_events(ufd
, filter
);
3480 if (ret2
< 0 && ret2
!= -ENOENT
) {
3497 int show_available_funcs(const char *target
, struct nsinfo
*nsi
,
3498 struct strfilter
*_filter
, bool user
)
3504 ret
= init_probe_symbol_maps(user
);
3508 /* Get a symbol map */
3509 map
= get_target_map(target
, nsi
, user
);
3511 pr_err("Failed to get a map for %s\n", (target
) ? : "kernel");
3515 ret
= map__load(map
);
3518 char *str
= strfilter__string(_filter
);
3519 pr_err("Failed to find symbols matched to \"%s\"\n",
3523 pr_err("Failed to load symbols in %s\n",
3524 (target
) ? : "kernel");
3527 if (!dso__sorted_by_name(map
->dso
))
3528 dso__sort_by_name(map
->dso
);
3530 /* Show all (filtered) symbols */
3533 for (nd
= rb_first(&map
->dso
->symbol_names
); nd
; nd
= rb_next(nd
)) {
3534 struct symbol_name_rb_node
*pos
= rb_entry(nd
, struct symbol_name_rb_node
, rb_node
);
3536 if (strfilter__compare(_filter
, pos
->sym
.name
))
3537 printf("%s\n", pos
->sym
.name
);
3541 exit_probe_symbol_maps();
3546 int copy_to_probe_trace_arg(struct probe_trace_arg
*tvar
,
3547 struct perf_probe_arg
*pvar
)
3549 tvar
->value
= strdup(pvar
->var
);
3550 if (tvar
->value
== NULL
)
3553 tvar
->type
= strdup(pvar
->type
);
3554 if (tvar
->type
== NULL
)
3558 tvar
->name
= strdup(pvar
->name
);
3559 if (tvar
->name
== NULL
)