2 * probe-event.c : perf-probe definition to probe_events format converter
4 * Written by Masami Hiramatsu <mhiramat@redhat.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 #include <sys/utsname.h>
23 #include <sys/types.h>
43 #include <api/fs/fs.h>
44 #include "trace-event.h" /* For __maybe_unused */
45 #include "probe-event.h"
46 #include "probe-finder.h"
47 #include "probe-file.h"
50 #define MAX_CMDLEN 256
51 #define PERFPROBE_GROUP "probe"
53 bool probe_event_dry_run
; /* Dry run flag */
54 struct probe_conf probe_conf
;
56 #define semantic_error(msg ...) pr_err("Semantic error :" msg)
58 int e_snprintf(char *str
, size_t size
, const char *format
, ...)
63 ret
= vsnprintf(str
, size
, format
, ap
);
70 static struct machine
*host_machine
;
72 /* Initialize symbol maps and path of vmlinux/modules */
73 int init_probe_symbol_maps(bool user_only
)
77 symbol_conf
.sort_by_name
= true;
78 symbol_conf
.allow_aliases
= true;
79 ret
= symbol__init(NULL
);
81 pr_debug("Failed to init symbol map.\n");
85 if (host_machine
|| user_only
) /* already initialized */
88 if (symbol_conf
.vmlinux_name
)
89 pr_debug("Use vmlinux: %s\n", symbol_conf
.vmlinux_name
);
91 host_machine
= machine__new_host();
93 pr_debug("machine__new_host() failed.\n");
99 pr_warning("Failed to init vmlinux path.\n");
103 void exit_probe_symbol_maps(void)
105 machine__delete(host_machine
);
110 static struct symbol
*__find_kernel_function_by_name(const char *name
,
113 return machine__find_kernel_function_by_name(host_machine
, name
, mapp
,
117 static struct symbol
*__find_kernel_function(u64 addr
, struct map
**mapp
)
119 return machine__find_kernel_function(host_machine
, addr
, mapp
, NULL
);
122 static struct ref_reloc_sym
*kernel_get_ref_reloc_sym(void)
124 /* kmap->ref_reloc_sym should be set if host_machine is initialized */
126 struct map
*map
= machine__kernel_map(host_machine
);
128 if (map__load(map
, NULL
) < 0)
131 kmap
= map__kmap(map
);
134 return kmap
->ref_reloc_sym
;
137 static int kernel_get_symbol_address_by_name(const char *name
, u64
*addr
,
138 bool reloc
, bool reladdr
)
140 struct ref_reloc_sym
*reloc_sym
;
144 /* ref_reloc_sym is just a label. Need a special fix*/
145 reloc_sym
= kernel_get_ref_reloc_sym();
146 if (reloc_sym
&& strcmp(name
, reloc_sym
->name
) == 0)
147 *addr
= (reloc
) ? reloc_sym
->addr
: reloc_sym
->unrelocated_addr
;
149 sym
= __find_kernel_function_by_name(name
, &map
);
152 *addr
= map
->unmap_ip(map
, sym
->start
) -
153 ((reloc
) ? 0 : map
->reloc
) -
154 ((reladdr
) ? map
->start
: 0);
159 static struct map
*kernel_get_module_map(const char *module
)
161 struct map_groups
*grp
= &host_machine
->kmaps
;
162 struct maps
*maps
= &grp
->maps
[MAP__FUNCTION
];
165 /* A file path -- this is an offline module */
166 if (module
&& strchr(module
, '/'))
167 return machine__findnew_module_map(host_machine
, 0, module
);
172 for (pos
= maps__first(maps
); pos
; pos
= map__next(pos
)) {
173 if (strncmp(pos
->dso
->short_name
+ 1, module
,
174 pos
->dso
->short_name_len
- 2) == 0) {
181 static struct map
*get_target_map(const char *target
, bool user
)
183 /* Init maps of given executable or kernel */
185 return dso__new_map(target
);
187 return kernel_get_module_map(target
);
190 static void put_target_map(struct map
*map
, bool user
)
193 /* Only the user map needs to be released */
199 static int convert_exec_to_group(const char *exec
, char **result
)
201 char *ptr1
, *ptr2
, *exec_copy
;
205 exec_copy
= strdup(exec
);
209 ptr1
= basename(exec_copy
);
215 ptr2
= strpbrk(ptr1
, "-._");
218 ret
= e_snprintf(buf
, 64, "%s_%s", PERFPROBE_GROUP
, ptr1
);
222 *result
= strdup(buf
);
223 ret
= *result
? 0 : -ENOMEM
;
230 static void clear_perf_probe_point(struct perf_probe_point
*pp
)
237 static void clear_probe_trace_events(struct probe_trace_event
*tevs
, int ntevs
)
241 for (i
= 0; i
< ntevs
; i
++)
242 clear_probe_trace_event(tevs
+ i
);
245 static bool kprobe_blacklist__listed(unsigned long address
);
246 static bool kprobe_warn_out_range(const char *symbol
, unsigned long address
)
251 /* Get the address of _etext for checking non-probable text symbol */
252 ret
= kernel_get_symbol_address_by_name("_etext", &etext_addr
,
255 if (ret
== 0 && etext_addr
< address
)
256 pr_warning("%s is out of .text, skip it.\n", symbol
);
257 else if (kprobe_blacklist__listed(address
))
258 pr_warning("%s is blacklisted function, skip it.\n", symbol
);
267 * '.gnu.linkonce.this_module' section of kernel module elf directly
268 * maps to 'struct module' from linux/module.h. This section contains
269 * actual module name which will be used by kernel after loading it.
270 * But, we cannot use 'struct module' here since linux/module.h is not
271 * exposed to user-space. Offset of 'name' has remained same from long
272 * time, so hardcoding it here.
275 #define MOD_NAME_OFFSET 24
277 #define MOD_NAME_OFFSET 12
281 * @module can be module name of module file path. In case of path,
282 * inspect elf and find out what is actual module name.
283 * Caller has to free mod_name after using it.
285 static char *find_module_name(const char *module
)
293 char *mod_name
= NULL
;
295 fd
= open(module
, O_RDONLY
);
299 elf
= elf_begin(fd
, PERF_ELF_C_READ_MMAP
, NULL
);
303 if (gelf_getehdr(elf
, &ehdr
) == NULL
)
306 sec
= elf_section_by_name(elf
, &ehdr
, &shdr
,
307 ".gnu.linkonce.this_module", NULL
);
311 data
= elf_getdata(sec
, NULL
);
312 if (!data
|| !data
->d_buf
)
315 mod_name
= strdup((char *)data
->d_buf
+ MOD_NAME_OFFSET
);
324 #ifdef HAVE_DWARF_SUPPORT
326 static int kernel_get_module_dso(const char *module
, struct dso
**pdso
)
330 const char *vmlinux_name
;
334 char module_name
[128];
336 snprintf(module_name
, sizeof(module_name
), "[%s]", module
);
337 map
= map_groups__find_by_name(&host_machine
->kmaps
, MAP__FUNCTION
, module_name
);
342 pr_debug("Failed to find module %s.\n", module
);
346 map
= machine__kernel_map(host_machine
);
349 vmlinux_name
= symbol_conf
.vmlinux_name
;
352 ret
= dso__load_vmlinux(dso
, map
, vmlinux_name
, false, NULL
);
354 ret
= dso__load_vmlinux_path(dso
, map
, NULL
);
361 * Some binaries like glibc have special symbols which are on the symbol
362 * table, but not in the debuginfo. If we can find the address of the
363 * symbol from map, we can translate the address back to the probe point.
365 static int find_alternative_probe_point(struct debuginfo
*dinfo
,
366 struct perf_probe_point
*pp
,
367 struct perf_probe_point
*result
,
368 const char *target
, bool uprobes
)
370 struct map
*map
= NULL
;
375 /* This can work only for function-name based one */
376 if (!pp
->function
|| pp
->file
)
379 map
= get_target_map(target
, uprobes
);
383 /* Find the address of given function */
384 map__for_each_symbol_by_name(map
, pp
->function
, sym
) {
386 address
= sym
->start
;
388 address
= map
->unmap_ip(map
, sym
->start
);
395 pr_debug("Symbol %s address found : %" PRIx64
"\n",
396 pp
->function
, address
);
398 ret
= debuginfo__find_probe_point(dinfo
, (unsigned long)address
,
401 ret
= (!ret
) ? -ENOENT
: ret
;
403 result
->offset
+= pp
->offset
;
404 result
->line
+= pp
->line
;
405 result
->retprobe
= pp
->retprobe
;
410 put_target_map(map
, uprobes
);
415 static int get_alternative_probe_event(struct debuginfo
*dinfo
,
416 struct perf_probe_event
*pev
,
417 struct perf_probe_point
*tmp
)
421 memcpy(tmp
, &pev
->point
, sizeof(*tmp
));
422 memset(&pev
->point
, 0, sizeof(pev
->point
));
423 ret
= find_alternative_probe_point(dinfo
, tmp
, &pev
->point
,
424 pev
->target
, pev
->uprobes
);
426 memcpy(&pev
->point
, tmp
, sizeof(*tmp
));
431 static int get_alternative_line_range(struct debuginfo
*dinfo
,
432 struct line_range
*lr
,
433 const char *target
, bool user
)
435 struct perf_probe_point pp
= { .function
= lr
->function
,
438 struct perf_probe_point result
;
441 memset(&result
, 0, sizeof(result
));
443 if (lr
->end
!= INT_MAX
)
444 len
= lr
->end
- lr
->start
;
445 ret
= find_alternative_probe_point(dinfo
, &pp
, &result
,
448 lr
->function
= result
.function
;
449 lr
->file
= result
.file
;
450 lr
->start
= result
.line
;
451 if (lr
->end
!= INT_MAX
)
452 lr
->end
= lr
->start
+ len
;
453 clear_perf_probe_point(&pp
);
458 /* Open new debuginfo of given module */
459 static struct debuginfo
*open_debuginfo(const char *module
, bool silent
)
461 const char *path
= module
;
462 char reason
[STRERR_BUFSIZE
];
463 struct debuginfo
*ret
= NULL
;
464 struct dso
*dso
= NULL
;
467 if (!module
|| !strchr(module
, '/')) {
468 err
= kernel_get_module_dso(module
, &dso
);
470 if (!dso
|| dso
->load_errno
== 0) {
471 if (!str_error_r(-err
, reason
, STRERR_BUFSIZE
))
472 strcpy(reason
, "(unknown)");
474 dso__strerror_load(dso
, reason
, STRERR_BUFSIZE
);
476 pr_err("Failed to find the path for %s: %s\n",
477 module
?: "kernel", reason
);
480 path
= dso
->long_name
;
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");
494 /* For caching the last debuginfo */
495 static struct debuginfo
*debuginfo_cache
;
496 static char *debuginfo_cache_path
;
498 static struct debuginfo
*debuginfo_cache__open(const char *module
, bool silent
)
500 const char *path
= module
;
502 /* If the module is NULL, it should be the kernel. */
506 if (debuginfo_cache_path
&& !strcmp(debuginfo_cache_path
, path
))
509 /* Copy module path */
510 free(debuginfo_cache_path
);
511 debuginfo_cache_path
= strdup(path
);
512 if (!debuginfo_cache_path
) {
513 debuginfo__delete(debuginfo_cache
);
514 debuginfo_cache
= NULL
;
518 debuginfo_cache
= open_debuginfo(module
, silent
);
519 if (!debuginfo_cache
)
520 zfree(&debuginfo_cache_path
);
522 return debuginfo_cache
;
525 static void debuginfo_cache__exit(void)
527 debuginfo__delete(debuginfo_cache
);
528 debuginfo_cache
= NULL
;
529 zfree(&debuginfo_cache_path
);
533 static int get_text_start_address(const char *exec
, unsigned long *address
)
538 int fd
, ret
= -ENOENT
;
540 fd
= open(exec
, O_RDONLY
);
544 elf
= elf_begin(fd
, PERF_ELF_C_READ_MMAP
, NULL
);
550 if (gelf_getehdr(elf
, &ehdr
) == NULL
)
553 if (!elf_section_by_name(elf
, &ehdr
, &shdr
, ".text", NULL
))
556 *address
= shdr
.sh_addr
- shdr
.sh_offset
;
567 * Convert trace point to probe point with debuginfo
569 static int find_perf_probe_point_from_dwarf(struct probe_trace_point
*tp
,
570 struct perf_probe_point
*pp
,
573 struct debuginfo
*dinfo
= NULL
;
574 unsigned long stext
= 0;
575 u64 addr
= tp
->address
;
578 /* convert the address to dwarf address */
584 ret
= get_text_start_address(tp
->module
, &stext
);
588 } else if (tp
->symbol
) {
589 /* If the module is given, this returns relative address */
590 ret
= kernel_get_symbol_address_by_name(tp
->symbol
, &addr
,
591 false, !!tp
->module
);
597 pr_debug("try to find information at %" PRIx64
" in %s\n", addr
,
598 tp
->module
? : "kernel");
600 dinfo
= debuginfo_cache__open(tp
->module
, verbose
== 0);
602 ret
= debuginfo__find_probe_point(dinfo
,
603 (unsigned long)addr
, pp
);
608 pp
->retprobe
= tp
->retprobe
;
612 pr_debug("Failed to find corresponding probes from debuginfo.\n");
613 return ret
? : -ENOENT
;
616 static int add_exec_to_probe_trace_events(struct probe_trace_event
*tevs
,
617 int ntevs
, const char *exec
)
620 unsigned long stext
= 0;
625 ret
= get_text_start_address(exec
, &stext
);
629 for (i
= 0; i
< ntevs
&& ret
>= 0; i
++) {
630 /* point.address is the addres of point.symbol + point.offset */
631 tevs
[i
].point
.address
-= stext
;
632 tevs
[i
].point
.module
= strdup(exec
);
633 if (!tevs
[i
].point
.module
) {
637 tevs
[i
].uprobes
= true;
643 static int add_module_to_probe_trace_events(struct probe_trace_event
*tevs
,
644 int ntevs
, const char *module
)
647 char *mod_name
= NULL
;
652 mod_name
= find_module_name(module
);
654 for (i
= 0; i
< ntevs
; i
++) {
655 tevs
[i
].point
.module
=
656 strdup(mod_name
? mod_name
: module
);
657 if (!tevs
[i
].point
.module
) {
667 /* Post processing the probe events */
668 static int post_process_probe_trace_events(struct probe_trace_event
*tevs
,
669 int ntevs
, const char *module
,
672 struct ref_reloc_sym
*reloc_sym
;
677 return add_exec_to_probe_trace_events(tevs
, ntevs
, module
);
679 /* Note that currently ref_reloc_sym based probe is not for drivers */
681 return add_module_to_probe_trace_events(tevs
, ntevs
, module
);
683 reloc_sym
= kernel_get_ref_reloc_sym();
685 pr_warning("Relocated base symbol is not found!\n");
689 for (i
= 0; i
< ntevs
; i
++) {
690 if (!tevs
[i
].point
.address
|| tevs
[i
].point
.retprobe
)
692 /* If we found a wrong one, mark it by NULL symbol */
693 if (kprobe_warn_out_range(tevs
[i
].point
.symbol
,
694 tevs
[i
].point
.address
)) {
698 tmp
= strdup(reloc_sym
->name
);
702 /* If we have no realname, use symbol for it */
703 if (!tevs
[i
].point
.realname
)
704 tevs
[i
].point
.realname
= tevs
[i
].point
.symbol
;
706 free(tevs
[i
].point
.symbol
);
707 tevs
[i
].point
.symbol
= tmp
;
708 tevs
[i
].point
.offset
= tevs
[i
].point
.address
-
709 reloc_sym
->unrelocated_addr
;
714 /* Try to find perf_probe_event with debuginfo */
715 static int try_to_find_probe_trace_events(struct perf_probe_event
*pev
,
716 struct probe_trace_event
**tevs
)
718 bool need_dwarf
= perf_probe_event_need_dwarf(pev
);
719 struct perf_probe_point tmp
;
720 struct debuginfo
*dinfo
;
723 dinfo
= open_debuginfo(pev
->target
, !need_dwarf
);
727 pr_debug("Could not open debuginfo. Try to use symbols.\n");
731 pr_debug("Try to find probe point from debuginfo.\n");
732 /* Searching trace events corresponding to a probe event */
733 ntevs
= debuginfo__find_trace_events(dinfo
, pev
, tevs
);
735 if (ntevs
== 0) { /* Not found, retry with an alternative */
736 ret
= get_alternative_probe_event(dinfo
, pev
, &tmp
);
738 ntevs
= debuginfo__find_trace_events(dinfo
, pev
, tevs
);
740 * Write back to the original probe_event for
741 * setting appropriate (user given) event name
743 clear_perf_probe_point(&pev
->point
);
744 memcpy(&pev
->point
, &tmp
, sizeof(tmp
));
748 debuginfo__delete(dinfo
);
750 if (ntevs
> 0) { /* Succeeded to find trace events */
751 pr_debug("Found %d probe_trace_events.\n", ntevs
);
752 ret
= post_process_probe_trace_events(*tevs
, ntevs
,
753 pev
->target
, pev
->uprobes
);
754 if (ret
< 0 || ret
== ntevs
) {
755 clear_probe_trace_events(*tevs
, ntevs
);
759 return ret
< 0 ? ret
: ntevs
;
764 if (ntevs
== 0) { /* No error but failed to find probe point. */
765 pr_warning("Probe point '%s' not found.\n",
766 synthesize_perf_probe_point(&pev
->point
));
769 /* Error path : ntevs < 0 */
770 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs
);
773 pr_warning("Warning: No dwarf info found in the vmlinux - "
774 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
776 pr_debug("Trying to use symbols.\n");
783 #define LINEBUF_SIZE 256
784 #define NR_ADDITIONAL_LINES 2
786 static int __show_one_line(FILE *fp
, int l
, bool skip
, bool show_num
)
788 char buf
[LINEBUF_SIZE
], sbuf
[STRERR_BUFSIZE
];
789 const char *color
= show_num
? "" : PERF_COLOR_BLUE
;
790 const char *prefix
= NULL
;
793 if (fgets(buf
, LINEBUF_SIZE
, fp
) == NULL
)
798 prefix
= show_num
? "%7d " : " ";
799 color_fprintf(stdout
, color
, prefix
, l
);
801 color_fprintf(stdout
, color
, "%s", buf
);
803 } while (strchr(buf
, '\n') == NULL
);
808 pr_warning("File read error: %s\n",
809 str_error_r(errno
, sbuf
, sizeof(sbuf
)));
815 static int _show_one_line(FILE *fp
, int l
, bool skip
, bool show_num
)
817 int rv
= __show_one_line(fp
, l
, skip
, show_num
);
819 pr_warning("Source file is shorter than expected.\n");
825 #define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
826 #define show_one_line(f,l) _show_one_line(f,l,false,false)
827 #define skip_one_line(f,l) _show_one_line(f,l,true,false)
828 #define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
831 * Show line-range always requires debuginfo to find source file and
834 static int __show_line_range(struct line_range
*lr
, const char *module
,
839 struct debuginfo
*dinfo
;
843 char sbuf
[STRERR_BUFSIZE
];
845 /* Search a line range */
846 dinfo
= open_debuginfo(module
, false);
850 ret
= debuginfo__find_line_range(dinfo
, lr
);
851 if (!ret
) { /* Not found, retry with an alternative */
852 ret
= get_alternative_line_range(dinfo
, lr
, module
, user
);
854 ret
= debuginfo__find_line_range(dinfo
, lr
);
856 debuginfo__delete(dinfo
);
857 if (ret
== 0 || ret
== -ENOENT
) {
858 pr_warning("Specified source line is not found.\n");
860 } else if (ret
< 0) {
861 pr_warning("Debuginfo analysis failed.\n");
865 /* Convert source file path */
867 ret
= get_real_path(tmp
, lr
->comp_dir
, &lr
->path
);
869 /* Free old path when new path is assigned */
874 pr_warning("Failed to find source file path.\n");
881 fprintf(stdout
, "<%s@%s:%d>\n", lr
->function
, lr
->path
,
882 lr
->start
- lr
->offset
);
884 fprintf(stdout
, "<%s:%d>\n", lr
->path
, lr
->start
);
886 fp
= fopen(lr
->path
, "r");
888 pr_warning("Failed to open %s: %s\n", lr
->path
,
889 str_error_r(errno
, sbuf
, sizeof(sbuf
)));
892 /* Skip to starting line number */
893 while (l
< lr
->start
) {
894 ret
= skip_one_line(fp
, l
++);
899 intlist__for_each_entry(ln
, lr
->line_list
) {
900 for (; ln
->i
> l
; l
++) {
901 ret
= show_one_line(fp
, l
- lr
->offset
);
905 ret
= show_one_line_with_num(fp
, l
++ - lr
->offset
);
910 if (lr
->end
== INT_MAX
)
911 lr
->end
= l
+ NR_ADDITIONAL_LINES
;
912 while (l
<= lr
->end
) {
913 ret
= show_one_line_or_eof(fp
, l
++ - lr
->offset
);
922 int show_line_range(struct line_range
*lr
, const char *module
, bool user
)
926 ret
= init_probe_symbol_maps(user
);
929 ret
= __show_line_range(lr
, module
, user
);
930 exit_probe_symbol_maps();
935 static int show_available_vars_at(struct debuginfo
*dinfo
,
936 struct perf_probe_event
*pev
,
937 struct strfilter
*_filter
)
941 struct str_node
*node
;
942 struct variable_list
*vls
= NULL
, *vl
;
943 struct perf_probe_point tmp
;
946 buf
= synthesize_perf_probe_point(&pev
->point
);
949 pr_debug("Searching variables at %s\n", buf
);
951 ret
= debuginfo__find_available_vars_at(dinfo
, pev
, &vls
);
952 if (!ret
) { /* Not found, retry with an alternative */
953 ret
= get_alternative_probe_event(dinfo
, pev
, &tmp
);
955 ret
= debuginfo__find_available_vars_at(dinfo
, pev
,
957 /* Release the old probe_point */
958 clear_perf_probe_point(&tmp
);
962 if (ret
== 0 || ret
== -ENOENT
) {
963 pr_err("Failed to find the address of %s\n", buf
);
966 pr_warning("Debuginfo analysis failed.\n");
970 /* Some variables are found */
971 fprintf(stdout
, "Available variables at %s\n", buf
);
972 for (i
= 0; i
< ret
; i
++) {
975 * A probe point might be converted to
976 * several trace points.
978 fprintf(stdout
, "\t@<%s+%lu>\n", vl
->point
.symbol
,
980 zfree(&vl
->point
.symbol
);
983 strlist__for_each_entry(node
, vl
->vars
) {
984 var
= strchr(node
->s
, '\t') + 1;
985 if (strfilter__compare(_filter
, var
)) {
986 fprintf(stdout
, "\t\t%s\n", node
->s
);
990 strlist__delete(vl
->vars
);
993 fprintf(stdout
, "\t\t(No matched variables)\n");
1001 /* Show available variables on given probe point */
1002 int show_available_vars(struct perf_probe_event
*pevs
, int npevs
,
1003 struct strfilter
*_filter
)
1006 struct debuginfo
*dinfo
;
1008 ret
= init_probe_symbol_maps(pevs
->uprobes
);
1012 dinfo
= open_debuginfo(pevs
->target
, false);
1020 for (i
= 0; i
< npevs
&& ret
>= 0; i
++)
1021 ret
= show_available_vars_at(dinfo
, &pevs
[i
], _filter
);
1023 debuginfo__delete(dinfo
);
1025 exit_probe_symbol_maps();
1029 #else /* !HAVE_DWARF_SUPPORT */
1031 static void debuginfo_cache__exit(void)
1036 find_perf_probe_point_from_dwarf(struct probe_trace_point
*tp __maybe_unused
,
1037 struct perf_probe_point
*pp __maybe_unused
,
1038 bool is_kprobe __maybe_unused
)
1043 static int try_to_find_probe_trace_events(struct perf_probe_event
*pev
,
1044 struct probe_trace_event
**tevs __maybe_unused
)
1046 if (perf_probe_event_need_dwarf(pev
)) {
1047 pr_warning("Debuginfo-analysis is not supported.\n");
1054 int show_line_range(struct line_range
*lr __maybe_unused
,
1055 const char *module __maybe_unused
,
1056 bool user __maybe_unused
)
1058 pr_warning("Debuginfo-analysis is not supported.\n");
1062 int show_available_vars(struct perf_probe_event
*pevs __maybe_unused
,
1063 int npevs __maybe_unused
,
1064 struct strfilter
*filter __maybe_unused
)
1066 pr_warning("Debuginfo-analysis is not supported.\n");
1071 void line_range__clear(struct line_range
*lr
)
1077 intlist__delete(lr
->line_list
);
1078 memset(lr
, 0, sizeof(*lr
));
1081 int line_range__init(struct line_range
*lr
)
1083 memset(lr
, 0, sizeof(*lr
));
1084 lr
->line_list
= intlist__new(NULL
);
1091 static int parse_line_num(char **ptr
, int *val
, const char *what
)
1093 const char *start
= *ptr
;
1096 *val
= strtol(*ptr
, ptr
, 0);
1097 if (errno
|| *ptr
== start
) {
1098 semantic_error("'%s' is not a valid number.\n", what
);
1104 /* Check the name is good for event, group or function */
1105 static bool is_c_func_name(const char *name
)
1107 if (!isalpha(*name
) && *name
!= '_')
1109 while (*++name
!= '\0') {
1110 if (!isalpha(*name
) && !isdigit(*name
) && *name
!= '_')
1117 * Stuff 'lr' according to the line range described by 'arg'.
1118 * The line range syntax is described by:
1120 * SRC[:SLN[+NUM|-ELN]]
1121 * FNC[@SRC][:SLN[+NUM|-ELN]]
1123 int parse_line_range_desc(const char *arg
, struct line_range
*lr
)
1125 char *range
, *file
, *name
= strdup(arg
);
1134 range
= strchr(name
, ':');
1138 err
= parse_line_num(&range
, &lr
->start
, "start line");
1142 if (*range
== '+' || *range
== '-') {
1143 const char c
= *range
++;
1145 err
= parse_line_num(&range
, &lr
->end
, "end line");
1150 lr
->end
+= lr
->start
;
1152 * Adjust the number of lines here.
1153 * If the number of lines == 1, the
1154 * the end of line should be equal to
1155 * the start of line.
1161 pr_debug("Line range is %d to %d\n", lr
->start
, lr
->end
);
1164 if (lr
->start
> lr
->end
) {
1165 semantic_error("Start line must be smaller"
1166 " than end line.\n");
1169 if (*range
!= '\0') {
1170 semantic_error("Tailing with invalid str '%s'.\n", range
);
1175 file
= strchr(name
, '@');
1178 lr
->file
= strdup(++file
);
1179 if (lr
->file
== NULL
) {
1183 lr
->function
= name
;
1184 } else if (strchr(name
, '/') || strchr(name
, '.'))
1186 else if (is_c_func_name(name
))/* We reuse it for checking funcname */
1187 lr
->function
= name
;
1188 else { /* Invalid name */
1189 semantic_error("'%s' is not a valid function name.\n", name
);
1200 static int parse_perf_probe_event_name(char **arg
, struct perf_probe_event
*pev
)
1204 ptr
= strchr(*arg
, ':');
1207 if (!pev
->sdt
&& !is_c_func_name(*arg
))
1209 pev
->group
= strdup(*arg
);
1215 if (!pev
->sdt
&& !is_c_func_name(*arg
)) {
1217 semantic_error("%s is bad for event name -it must "
1218 "follow C symbol-naming rule.\n", *arg
);
1221 pev
->event
= strdup(*arg
);
1222 if (pev
->event
== NULL
)
1228 /* Parse probepoint definition. */
1229 static int parse_perf_probe_point(char *arg
, struct perf_probe_event
*pev
)
1231 struct perf_probe_point
*pp
= &pev
->point
;
1234 bool file_spec
= false;
1239 * perf probe [GRP:][EVENT=]SRC[:LN|;PTN]
1240 * perf probe [GRP:][EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
1241 * perf probe %[GRP:]SDT_EVENT
1247 * If the probe point starts with '%',
1248 * or starts with "sdt_" and has a ':' but no '=',
1249 * then it should be a SDT/cached probe point.
1251 if (arg
[0] == '%' ||
1252 (!strncmp(arg
, "sdt_", 4) &&
1253 !!strchr(arg
, ':') && !strchr(arg
, '='))) {
1259 ptr
= strpbrk(arg
, ";=@+%");
1263 semantic_error("%s must be an SDT name.\n",
1267 /* This must be a target file name or build id */
1268 tmp
= build_id_cache__complement(ptr
+ 1);
1270 pev
->target
= build_id_cache__origname(tmp
);
1273 pev
->target
= strdup(ptr
+ 1);
1278 ret
= parse_perf_probe_event_name(&arg
, pev
);
1280 if (asprintf(&pev
->point
.function
, "%%%s", pev
->event
) < 0)
1286 if (ptr
&& *ptr
== '=') { /* Event name */
1289 ret
= parse_perf_probe_event_name(&arg
, pev
);
1297 * Check arg is function or file name and copy it.
1299 * We consider arg to be a file spec if and only if it satisfies
1300 * all of the below criteria::
1301 * - it does not include any of "+@%",
1302 * - it includes one of ":;", and
1303 * - it has a period '.' in the name.
1305 * Otherwise, we consider arg to be a function specification.
1307 if (!strpbrk(arg
, "+@%") && (ptr
= strpbrk(arg
, ";:")) != NULL
) {
1308 /* This is a file spec if it includes a '.' before ; or : */
1309 if (memchr(arg
, '.', ptr
- arg
))
1313 ptr
= strpbrk(arg
, ";:+@%");
1333 * Keep pp->function even if this is absolute address,
1334 * so it can mark whether abs_address is valid.
1335 * Which make 'perf probe lib.bin 0x0' possible.
1337 * Note that checking length of tmp is not needed
1338 * because when we access tmp[1] we know tmp[0] is '0',
1339 * so tmp[1] should always valid (but could be '\0').
1341 if (tmp
&& !strncmp(tmp
, "0x", 2)) {
1342 pp
->abs_address
= strtoul(pp
->function
, &tmp
, 0);
1344 semantic_error("Invalid absolute address.\n");
1350 /* Parse other options */
1354 if (c
== ';') { /* Lazy pattern must be the last part */
1355 pp
->lazy_line
= strdup(arg
);
1356 if (pp
->lazy_line
== NULL
)
1360 ptr
= strpbrk(arg
, ";:+@%");
1366 case ':': /* Line number */
1367 pp
->line
= strtoul(arg
, &tmp
, 0);
1369 semantic_error("There is non-digit char"
1370 " in line number.\n");
1374 case '+': /* Byte offset from a symbol */
1375 pp
->offset
= strtoul(arg
, &tmp
, 0);
1377 semantic_error("There is non-digit character"
1382 case '@': /* File name */
1384 semantic_error("SRC@SRC is not allowed.\n");
1387 pp
->file
= strdup(arg
);
1388 if (pp
->file
== NULL
)
1391 case '%': /* Probe places */
1392 if (strcmp(arg
, "return") == 0) {
1394 } else { /* Others not supported yet */
1395 semantic_error("%%%s is not supported.\n", arg
);
1399 default: /* Buggy case */
1400 pr_err("This program has a bug at %s:%d.\n",
1401 __FILE__
, __LINE__
);
1407 /* Exclusion check */
1408 if (pp
->lazy_line
&& pp
->line
) {
1409 semantic_error("Lazy pattern can't be used with"
1414 if (pp
->lazy_line
&& pp
->offset
) {
1415 semantic_error("Lazy pattern can't be used with offset.\n");
1419 if (pp
->line
&& pp
->offset
) {
1420 semantic_error("Offset can't be used with line number.\n");
1424 if (!pp
->line
&& !pp
->lazy_line
&& pp
->file
&& !pp
->function
) {
1425 semantic_error("File always requires line number or "
1430 if (pp
->offset
&& !pp
->function
) {
1431 semantic_error("Offset requires an entry function.\n");
1435 if (pp
->retprobe
&& !pp
->function
) {
1436 semantic_error("Return probe requires an entry function.\n");
1440 if ((pp
->offset
|| pp
->line
|| pp
->lazy_line
) && pp
->retprobe
) {
1441 semantic_error("Offset/Line/Lazy pattern can't be used with "
1446 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
1447 pp
->function
, pp
->file
, pp
->line
, pp
->offset
, pp
->retprobe
,
1452 /* Parse perf-probe event argument */
1453 static int parse_perf_probe_arg(char *str
, struct perf_probe_arg
*arg
)
1455 char *tmp
, *goodname
;
1456 struct perf_probe_arg_field
**fieldp
;
1458 pr_debug("parsing arg: %s into ", str
);
1460 tmp
= strchr(str
, '=');
1462 arg
->name
= strndup(str
, tmp
- str
);
1463 if (arg
->name
== NULL
)
1465 pr_debug("name:%s ", arg
->name
);
1469 tmp
= strchr(str
, ':');
1470 if (tmp
) { /* Type setting */
1472 arg
->type
= strdup(tmp
+ 1);
1473 if (arg
->type
== NULL
)
1475 pr_debug("type:%s ", arg
->type
);
1478 tmp
= strpbrk(str
, "-.[");
1479 if (!is_c_varname(str
) || !tmp
) {
1480 /* A variable, register, symbol or special value */
1481 arg
->var
= strdup(str
);
1482 if (arg
->var
== NULL
)
1484 pr_debug("%s\n", arg
->var
);
1488 /* Structure fields or array element */
1489 arg
->var
= strndup(str
, tmp
- str
);
1490 if (arg
->var
== NULL
)
1492 goodname
= arg
->var
;
1493 pr_debug("%s, ", arg
->var
);
1494 fieldp
= &arg
->field
;
1497 *fieldp
= zalloc(sizeof(struct perf_probe_arg_field
));
1498 if (*fieldp
== NULL
)
1500 if (*tmp
== '[') { /* Array */
1502 (*fieldp
)->index
= strtol(str
+ 1, &tmp
, 0);
1503 (*fieldp
)->ref
= true;
1504 if (*tmp
!= ']' || tmp
== str
+ 1) {
1505 semantic_error("Array index must be a"
1512 } else { /* Structure */
1515 (*fieldp
)->ref
= false;
1516 } else if (tmp
[1] == '>') {
1518 (*fieldp
)->ref
= true;
1520 semantic_error("Argument parse error: %s\n",
1524 tmp
= strpbrk(str
, "-.[");
1527 (*fieldp
)->name
= strndup(str
, tmp
- str
);
1528 if ((*fieldp
)->name
== NULL
)
1531 goodname
= (*fieldp
)->name
;
1532 pr_debug("%s(%d), ", (*fieldp
)->name
, (*fieldp
)->ref
);
1533 fieldp
= &(*fieldp
)->next
;
1536 (*fieldp
)->name
= strdup(str
);
1537 if ((*fieldp
)->name
== NULL
)
1540 goodname
= (*fieldp
)->name
;
1541 pr_debug("%s(%d)\n", (*fieldp
)->name
, (*fieldp
)->ref
);
1543 /* If no name is specified, set the last field name (not array index)*/
1545 arg
->name
= strdup(goodname
);
1546 if (arg
->name
== NULL
)
1552 /* Parse perf-probe event command */
1553 int parse_perf_probe_command(const char *cmd
, struct perf_probe_event
*pev
)
1556 int argc
, i
, ret
= 0;
1558 argv
= argv_split(cmd
, &argc
);
1560 pr_debug("Failed to split arguments.\n");
1563 if (argc
- 1 > MAX_PROBE_ARGS
) {
1564 semantic_error("Too many probe arguments (%d).\n", argc
- 1);
1568 /* Parse probe point */
1569 ret
= parse_perf_probe_point(argv
[0], pev
);
1573 /* Copy arguments and ensure return probe has no C argument */
1574 pev
->nargs
= argc
- 1;
1575 pev
->args
= zalloc(sizeof(struct perf_probe_arg
) * pev
->nargs
);
1576 if (pev
->args
== NULL
) {
1580 for (i
= 0; i
< pev
->nargs
&& ret
>= 0; i
++) {
1581 ret
= parse_perf_probe_arg(argv
[i
+ 1], &pev
->args
[i
]);
1583 is_c_varname(pev
->args
[i
].var
) && pev
->point
.retprobe
) {
1584 semantic_error("You can't specify local variable for"
1595 /* Return true if this perf_probe_event requires debuginfo */
1596 bool perf_probe_event_need_dwarf(struct perf_probe_event
*pev
)
1600 if (pev
->point
.file
|| pev
->point
.line
|| pev
->point
.lazy_line
)
1603 for (i
= 0; i
< pev
->nargs
; i
++)
1604 if (is_c_varname(pev
->args
[i
].var
) ||
1605 !strcmp(pev
->args
[i
].var
, "$params") ||
1606 !strcmp(pev
->args
[i
].var
, "$vars"))
1612 /* Parse probe_events event into struct probe_point */
1613 int parse_probe_trace_command(const char *cmd
, struct probe_trace_event
*tev
)
1615 struct probe_trace_point
*tp
= &tev
->point
;
1618 char *argv0_str
= NULL
, *fmt
, *fmt1_str
, *fmt2_str
, *fmt3_str
;
1622 pr_debug("Parsing probe_events: %s\n", cmd
);
1623 argv
= argv_split(cmd
, &argc
);
1625 pr_debug("Failed to split arguments.\n");
1629 semantic_error("Too few probe arguments.\n");
1634 /* Scan event and group name. */
1635 argv0_str
= strdup(argv
[0]);
1636 if (argv0_str
== NULL
) {
1640 fmt1_str
= strtok_r(argv0_str
, ":", &fmt
);
1641 fmt2_str
= strtok_r(NULL
, "/", &fmt
);
1642 fmt3_str
= strtok_r(NULL
, " \t", &fmt
);
1643 if (fmt1_str
== NULL
|| strlen(fmt1_str
) != 1 || fmt2_str
== NULL
1644 || fmt3_str
== NULL
) {
1645 semantic_error("Failed to parse event name: %s\n", argv
[0]);
1650 tev
->group
= strdup(fmt2_str
);
1651 tev
->event
= strdup(fmt3_str
);
1652 if (tev
->group
== NULL
|| tev
->event
== NULL
) {
1656 pr_debug("Group:%s Event:%s probe:%c\n", tev
->group
, tev
->event
, pr
);
1658 tp
->retprobe
= (pr
== 'r');
1660 /* Scan module name(if there), function name and offset */
1661 p
= strchr(argv
[1], ':');
1663 tp
->module
= strndup(argv
[1], p
- argv
[1]);
1668 tev
->uprobes
= (tp
->module
[0] == '/');
1672 fmt1_str
= strtok_r(p
, "+", &fmt
);
1673 /* only the address started with 0x */
1674 if (fmt1_str
[0] == '0') {
1676 * Fix a special case:
1677 * if address == 0, kernel reports something like:
1678 * p:probe_libc/abs_0 /lib/libc-2.18.so:0x (null) arg1=%ax
1679 * Newer kernel may fix that, but we want to
1680 * support old kernel also.
1682 if (strcmp(fmt1_str
, "0x") == 0) {
1683 if (!argv
[2] || strcmp(argv
[2], "(null)")) {
1690 for (i
= 2; argv
[i
+ 1] != NULL
; i
++)
1691 argv
[i
] = argv
[i
+ 1];
1696 tp
->address
= strtoul(fmt1_str
, NULL
, 0);
1698 /* Only the symbol-based probe has offset */
1699 tp
->symbol
= strdup(fmt1_str
);
1700 if (tp
->symbol
== NULL
) {
1704 fmt2_str
= strtok_r(NULL
, "", &fmt
);
1705 if (fmt2_str
== NULL
)
1708 tp
->offset
= strtoul(fmt2_str
, NULL
, 10);
1711 tev
->nargs
= argc
- 2;
1712 tev
->args
= zalloc(sizeof(struct probe_trace_arg
) * tev
->nargs
);
1713 if (tev
->args
== NULL
) {
1717 for (i
= 0; i
< tev
->nargs
; i
++) {
1718 p
= strchr(argv
[i
+ 2], '=');
1719 if (p
) /* We don't need which register is assigned. */
1723 tev
->args
[i
].name
= strdup(argv
[i
+ 2]);
1724 /* TODO: parse regs and offset */
1725 tev
->args
[i
].value
= strdup(p
);
1726 if (tev
->args
[i
].name
== NULL
|| tev
->args
[i
].value
== NULL
) {
1738 /* Compose only probe arg */
1739 char *synthesize_perf_probe_arg(struct perf_probe_arg
*pa
)
1741 struct perf_probe_arg_field
*field
= pa
->field
;
1746 if (strbuf_init(&buf
, 64) < 0)
1749 if (pa
->name
&& pa
->var
)
1750 err
= strbuf_addf(&buf
, "%s=%s", pa
->name
, pa
->var
);
1752 err
= strbuf_addstr(&buf
, pa
->name
?: pa
->var
);
1757 if (field
->name
[0] == '[')
1758 err
= strbuf_addstr(&buf
, field
->name
);
1760 err
= strbuf_addf(&buf
, "%s%s", field
->ref
? "->" : ".",
1762 field
= field
->next
;
1768 if (strbuf_addf(&buf
, ":%s", pa
->type
) < 0)
1771 ret
= strbuf_detach(&buf
, NULL
);
1773 strbuf_release(&buf
);
1777 /* Compose only probe point (not argument) */
1778 char *synthesize_perf_probe_point(struct perf_probe_point
*pp
)
1781 char *tmp
, *ret
= NULL
;
1784 if (strbuf_init(&buf
, 64) < 0)
1788 if (strbuf_addstr(&buf
, pp
->function
) < 0)
1791 err
= strbuf_addf(&buf
, "+%lu", pp
->offset
);
1793 err
= strbuf_addf(&buf
, ":%d", pp
->line
);
1794 else if (pp
->retprobe
)
1795 err
= strbuf_addstr(&buf
, "%return");
1803 tmp
= strchr(pp
->file
+ len
- 30, '/');
1804 tmp
= tmp
? tmp
+ 1 : pp
->file
+ len
- 30;
1806 err
= strbuf_addf(&buf
, "@%s", tmp
);
1807 if (!err
&& !pp
->function
&& pp
->line
)
1808 err
= strbuf_addf(&buf
, ":%d", pp
->line
);
1811 ret
= strbuf_detach(&buf
, NULL
);
1813 strbuf_release(&buf
);
1817 char *synthesize_perf_probe_command(struct perf_probe_event
*pev
)
1820 char *tmp
, *ret
= NULL
;
1823 if (strbuf_init(&buf
, 64))
1826 if (strbuf_addf(&buf
, "%s:%s=", pev
->group
?: PERFPROBE_GROUP
,
1830 tmp
= synthesize_perf_probe_point(&pev
->point
);
1831 if (!tmp
|| strbuf_addstr(&buf
, tmp
) < 0)
1835 for (i
= 0; i
< pev
->nargs
; i
++) {
1836 tmp
= synthesize_perf_probe_arg(pev
->args
+ i
);
1837 if (!tmp
|| strbuf_addf(&buf
, " %s", tmp
) < 0)
1842 ret
= strbuf_detach(&buf
, NULL
);
1844 strbuf_release(&buf
);
1848 static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref
*ref
,
1849 struct strbuf
*buf
, int depth
)
1853 depth
= __synthesize_probe_trace_arg_ref(ref
->next
, buf
,
1858 err
= strbuf_addf(buf
, "%+ld(", ref
->offset
);
1859 return (err
< 0) ? err
: depth
;
1862 static int synthesize_probe_trace_arg(struct probe_trace_arg
*arg
,
1865 struct probe_trace_arg_ref
*ref
= arg
->ref
;
1868 /* Argument name or separator */
1870 err
= strbuf_addf(buf
, " %s=", arg
->name
);
1872 err
= strbuf_addch(buf
, ' ');
1876 /* Special case: @XXX */
1877 if (arg
->value
[0] == '@' && arg
->ref
)
1880 /* Dereferencing arguments */
1882 depth
= __synthesize_probe_trace_arg_ref(ref
, buf
, 1);
1887 /* Print argument value */
1888 if (arg
->value
[0] == '@' && arg
->ref
)
1889 err
= strbuf_addf(buf
, "%s%+ld", arg
->value
, arg
->ref
->offset
);
1891 err
= strbuf_addstr(buf
, arg
->value
);
1894 while (!err
&& depth
--)
1895 err
= strbuf_addch(buf
, ')');
1897 /* Print argument type */
1898 if (!err
&& arg
->type
)
1899 err
= strbuf_addf(buf
, ":%s", arg
->type
);
1904 char *synthesize_probe_trace_command(struct probe_trace_event
*tev
)
1906 struct probe_trace_point
*tp
= &tev
->point
;
1911 /* Uprobes must have tp->module */
1912 if (tev
->uprobes
&& !tp
->module
)
1915 if (strbuf_init(&buf
, 32) < 0)
1918 if (strbuf_addf(&buf
, "%c:%s/%s ", tp
->retprobe
? 'r' : 'p',
1919 tev
->group
, tev
->event
) < 0)
1922 * If tp->address == 0, then this point must be a
1923 * absolute address uprobe.
1924 * try_to_find_absolute_address() should have made
1925 * tp->symbol to "0x0".
1927 if (tev
->uprobes
&& !tp
->address
) {
1928 if (!tp
->symbol
|| strcmp(tp
->symbol
, "0x0"))
1932 /* Use the tp->address for uprobes */
1934 err
= strbuf_addf(&buf
, "%s:0x%lx", tp
->module
, tp
->address
);
1935 else if (!strncmp(tp
->symbol
, "0x", 2))
1936 /* Absolute address. See try_to_find_absolute_address() */
1937 err
= strbuf_addf(&buf
, "%s%s0x%lx", tp
->module
?: "",
1938 tp
->module
? ":" : "", tp
->address
);
1940 err
= strbuf_addf(&buf
, "%s%s%s+%lu", tp
->module
?: "",
1941 tp
->module
? ":" : "", tp
->symbol
, tp
->offset
);
1945 for (i
= 0; i
< tev
->nargs
; i
++)
1946 if (synthesize_probe_trace_arg(&tev
->args
[i
], &buf
) < 0)
1949 ret
= strbuf_detach(&buf
, NULL
);
1951 strbuf_release(&buf
);
1955 static int find_perf_probe_point_from_map(struct probe_trace_point
*tp
,
1956 struct perf_probe_point
*pp
,
1959 struct symbol
*sym
= NULL
;
1961 u64 addr
= tp
->address
;
1965 map
= dso__new_map(tp
->module
);
1968 sym
= map__find_symbol(map
, addr
, NULL
);
1970 if (tp
->symbol
&& !addr
) {
1971 if (kernel_get_symbol_address_by_name(tp
->symbol
,
1972 &addr
, true, false) < 0)
1977 sym
= __find_kernel_function(addr
, &map
);
1984 pp
->retprobe
= tp
->retprobe
;
1985 pp
->offset
= addr
- map
->unmap_ip(map
, sym
->start
);
1986 pp
->function
= strdup(sym
->name
);
1987 ret
= pp
->function
? 0 : -ENOMEM
;
1990 if (map
&& !is_kprobe
) {
1997 static int convert_to_perf_probe_point(struct probe_trace_point
*tp
,
1998 struct perf_probe_point
*pp
,
2004 ret
= find_perf_probe_point_from_dwarf(tp
, pp
, is_kprobe
);
2007 ret
= find_perf_probe_point_from_map(tp
, pp
, is_kprobe
);
2011 pr_debug("Failed to find probe point from both of dwarf and map.\n");
2014 pp
->function
= strdup(tp
->symbol
);
2015 pp
->offset
= tp
->offset
;
2017 ret
= e_snprintf(buf
, 128, "0x%" PRIx64
, (u64
)tp
->address
);
2020 pp
->function
= strdup(buf
);
2023 if (pp
->function
== NULL
)
2026 pp
->retprobe
= tp
->retprobe
;
2031 static int convert_to_perf_probe_event(struct probe_trace_event
*tev
,
2032 struct perf_probe_event
*pev
, bool is_kprobe
)
2034 struct strbuf buf
= STRBUF_INIT
;
2037 /* Convert event/group name */
2038 pev
->event
= strdup(tev
->event
);
2039 pev
->group
= strdup(tev
->group
);
2040 if (pev
->event
== NULL
|| pev
->group
== NULL
)
2043 /* Convert trace_point to probe_point */
2044 ret
= convert_to_perf_probe_point(&tev
->point
, &pev
->point
, is_kprobe
);
2048 /* Convert trace_arg to probe_arg */
2049 pev
->nargs
= tev
->nargs
;
2050 pev
->args
= zalloc(sizeof(struct perf_probe_arg
) * pev
->nargs
);
2051 if (pev
->args
== NULL
)
2053 for (i
= 0; i
< tev
->nargs
&& ret
>= 0; i
++) {
2054 if (tev
->args
[i
].name
)
2055 pev
->args
[i
].name
= strdup(tev
->args
[i
].name
);
2057 if ((ret
= strbuf_init(&buf
, 32)) < 0)
2059 ret
= synthesize_probe_trace_arg(&tev
->args
[i
], &buf
);
2060 pev
->args
[i
].name
= strbuf_detach(&buf
, NULL
);
2062 if (pev
->args
[i
].name
== NULL
&& ret
>= 0)
2067 clear_perf_probe_event(pev
);
2072 void clear_perf_probe_event(struct perf_probe_event
*pev
)
2074 struct perf_probe_arg_field
*field
, *next
;
2080 clear_perf_probe_point(&pev
->point
);
2082 for (i
= 0; i
< pev
->nargs
; i
++) {
2083 free(pev
->args
[i
].name
);
2084 free(pev
->args
[i
].var
);
2085 free(pev
->args
[i
].type
);
2086 field
= pev
->args
[i
].field
;
2089 zfree(&field
->name
);
2095 memset(pev
, 0, sizeof(*pev
));
2098 #define strdup_or_goto(str, label) \
2099 ({ char *__p = NULL; if (str && !(__p = strdup(str))) goto label; __p; })
2101 static int perf_probe_point__copy(struct perf_probe_point
*dst
,
2102 struct perf_probe_point
*src
)
2104 dst
->file
= strdup_or_goto(src
->file
, out_err
);
2105 dst
->function
= strdup_or_goto(src
->function
, out_err
);
2106 dst
->lazy_line
= strdup_or_goto(src
->lazy_line
, out_err
);
2107 dst
->line
= src
->line
;
2108 dst
->retprobe
= src
->retprobe
;
2109 dst
->offset
= src
->offset
;
2113 clear_perf_probe_point(dst
);
2117 static int perf_probe_arg__copy(struct perf_probe_arg
*dst
,
2118 struct perf_probe_arg
*src
)
2120 struct perf_probe_arg_field
*field
, **ppfield
;
2122 dst
->name
= strdup_or_goto(src
->name
, out_err
);
2123 dst
->var
= strdup_or_goto(src
->var
, out_err
);
2124 dst
->type
= strdup_or_goto(src
->type
, out_err
);
2127 ppfield
= &(dst
->field
);
2129 *ppfield
= zalloc(sizeof(*field
));
2132 (*ppfield
)->name
= strdup_or_goto(field
->name
, out_err
);
2133 (*ppfield
)->index
= field
->index
;
2134 (*ppfield
)->ref
= field
->ref
;
2135 field
= field
->next
;
2136 ppfield
= &((*ppfield
)->next
);
2143 int perf_probe_event__copy(struct perf_probe_event
*dst
,
2144 struct perf_probe_event
*src
)
2148 dst
->event
= strdup_or_goto(src
->event
, out_err
);
2149 dst
->group
= strdup_or_goto(src
->group
, out_err
);
2150 dst
->target
= strdup_or_goto(src
->target
, out_err
);
2151 dst
->uprobes
= src
->uprobes
;
2153 if (perf_probe_point__copy(&dst
->point
, &src
->point
) < 0)
2156 dst
->args
= zalloc(sizeof(struct perf_probe_arg
) * src
->nargs
);
2159 dst
->nargs
= src
->nargs
;
2161 for (i
= 0; i
< src
->nargs
; i
++)
2162 if (perf_probe_arg__copy(&dst
->args
[i
], &src
->args
[i
]) < 0)
2167 clear_perf_probe_event(dst
);
2171 void clear_probe_trace_event(struct probe_trace_event
*tev
)
2173 struct probe_trace_arg_ref
*ref
, *next
;
2178 free(tev
->point
.symbol
);
2179 free(tev
->point
.realname
);
2180 free(tev
->point
.module
);
2181 for (i
= 0; i
< tev
->nargs
; i
++) {
2182 free(tev
->args
[i
].name
);
2183 free(tev
->args
[i
].value
);
2184 free(tev
->args
[i
].type
);
2185 ref
= tev
->args
[i
].ref
;
2193 memset(tev
, 0, sizeof(*tev
));
2196 struct kprobe_blacklist_node
{
2197 struct list_head list
;
2198 unsigned long start
;
2203 static void kprobe_blacklist__delete(struct list_head
*blacklist
)
2205 struct kprobe_blacklist_node
*node
;
2207 while (!list_empty(blacklist
)) {
2208 node
= list_first_entry(blacklist
,
2209 struct kprobe_blacklist_node
, list
);
2210 list_del(&node
->list
);
2216 static int kprobe_blacklist__load(struct list_head
*blacklist
)
2218 struct kprobe_blacklist_node
*node
;
2219 const char *__debugfs
= debugfs__mountpoint();
2220 char buf
[PATH_MAX
], *p
;
2224 if (__debugfs
== NULL
)
2227 ret
= e_snprintf(buf
, PATH_MAX
, "%s/kprobes/blacklist", __debugfs
);
2231 fp
= fopen(buf
, "r");
2236 while (fgets(buf
, PATH_MAX
, fp
)) {
2237 node
= zalloc(sizeof(*node
));
2242 INIT_LIST_HEAD(&node
->list
);
2243 list_add_tail(&node
->list
, blacklist
);
2244 if (sscanf(buf
, "0x%lx-0x%lx", &node
->start
, &node
->end
) != 2) {
2248 p
= strchr(buf
, '\t');
2251 if (p
[strlen(p
) - 1] == '\n')
2252 p
[strlen(p
) - 1] = '\0';
2254 p
= (char *)"unknown";
2255 node
->symbol
= strdup(p
);
2256 if (!node
->symbol
) {
2260 pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
2261 node
->start
, node
->end
, node
->symbol
);
2265 kprobe_blacklist__delete(blacklist
);
2271 static struct kprobe_blacklist_node
*
2272 kprobe_blacklist__find_by_address(struct list_head
*blacklist
,
2273 unsigned long address
)
2275 struct kprobe_blacklist_node
*node
;
2277 list_for_each_entry(node
, blacklist
, list
) {
2278 if (node
->start
<= address
&& address
<= node
->end
)
2285 static LIST_HEAD(kprobe_blacklist
);
2287 static void kprobe_blacklist__init(void)
2289 if (!list_empty(&kprobe_blacklist
))
2292 if (kprobe_blacklist__load(&kprobe_blacklist
) < 0)
2293 pr_debug("No kprobe blacklist support, ignored\n");
2296 static void kprobe_blacklist__release(void)
2298 kprobe_blacklist__delete(&kprobe_blacklist
);
2301 static bool kprobe_blacklist__listed(unsigned long address
)
2303 return !!kprobe_blacklist__find_by_address(&kprobe_blacklist
, address
);
2306 static int perf_probe_event__sprintf(const char *group
, const char *event
,
2307 struct perf_probe_event
*pev
,
2309 struct strbuf
*result
)
2314 if (asprintf(&buf
, "%s:%s", group
, event
) < 0)
2316 ret
= strbuf_addf(result
, " %-20s (on ", buf
);
2321 /* Synthesize only event probe point */
2322 buf
= synthesize_perf_probe_point(&pev
->point
);
2325 ret
= strbuf_addstr(result
, buf
);
2329 ret
= strbuf_addf(result
, " in %s", module
);
2331 if (!ret
&& pev
->nargs
> 0) {
2332 ret
= strbuf_add(result
, " with", 5);
2333 for (i
= 0; !ret
&& i
< pev
->nargs
; i
++) {
2334 buf
= synthesize_perf_probe_arg(&pev
->args
[i
]);
2337 ret
= strbuf_addf(result
, " %s", buf
);
2342 ret
= strbuf_addch(result
, ')');
2348 int show_perf_probe_event(const char *group
, const char *event
,
2349 struct perf_probe_event
*pev
,
2350 const char *module
, bool use_stdout
)
2352 struct strbuf buf
= STRBUF_INIT
;
2355 ret
= perf_probe_event__sprintf(group
, event
, pev
, module
, &buf
);
2358 printf("%s\n", buf
.buf
);
2360 pr_info("%s\n", buf
.buf
);
2362 strbuf_release(&buf
);
2367 static bool filter_probe_trace_event(struct probe_trace_event
*tev
,
2368 struct strfilter
*filter
)
2372 /* At first, check the event name itself */
2373 if (strfilter__compare(filter
, tev
->event
))
2376 /* Next, check the combination of name and group */
2377 if (e_snprintf(tmp
, 128, "%s:%s", tev
->group
, tev
->event
) < 0)
2379 return strfilter__compare(filter
, tmp
);
2382 static int __show_perf_probe_events(int fd
, bool is_kprobe
,
2383 struct strfilter
*filter
)
2386 struct probe_trace_event tev
;
2387 struct perf_probe_event pev
;
2388 struct strlist
*rawlist
;
2389 struct str_node
*ent
;
2391 memset(&tev
, 0, sizeof(tev
));
2392 memset(&pev
, 0, sizeof(pev
));
2394 rawlist
= probe_file__get_rawlist(fd
);
2398 strlist__for_each_entry(ent
, rawlist
) {
2399 ret
= parse_probe_trace_command(ent
->s
, &tev
);
2401 if (!filter_probe_trace_event(&tev
, filter
))
2403 ret
= convert_to_perf_probe_event(&tev
, &pev
,
2407 ret
= show_perf_probe_event(pev
.group
, pev
.event
,
2408 &pev
, tev
.point
.module
,
2412 clear_perf_probe_event(&pev
);
2413 clear_probe_trace_event(&tev
);
2417 strlist__delete(rawlist
);
2418 /* Cleanup cached debuginfo if needed */
2419 debuginfo_cache__exit();
2424 /* List up current perf-probe events */
2425 int show_perf_probe_events(struct strfilter
*filter
)
2427 int kp_fd
, up_fd
, ret
;
2431 if (probe_conf
.cache
)
2432 return probe_cache__show_all_caches(filter
);
2434 ret
= init_probe_symbol_maps(false);
2438 ret
= probe_file__open_both(&kp_fd
, &up_fd
, 0);
2443 ret
= __show_perf_probe_events(kp_fd
, true, filter
);
2444 if (up_fd
>= 0 && ret
>= 0)
2445 ret
= __show_perf_probe_events(up_fd
, false, filter
);
2450 exit_probe_symbol_maps();
2455 static int get_new_event_name(char *buf
, size_t len
, const char *base
,
2456 struct strlist
*namelist
, bool allow_suffix
)
2463 nbase
= strdup(base
);
2467 /* Cut off the dot suffixes (e.g. .const, .isra)*/
2468 p
= strchr(nbase
, '.');
2469 if (p
&& p
!= nbase
)
2472 /* Try no suffix number */
2473 ret
= e_snprintf(buf
, len
, "%s", nbase
);
2475 pr_debug("snprintf() failed: %d\n", ret
);
2478 if (!strlist__has_entry(namelist
, buf
))
2481 if (!allow_suffix
) {
2482 pr_warning("Error: event \"%s\" already exists.\n"
2483 " Hint: Remove existing event by 'perf probe -d'\n"
2484 " or force duplicates by 'perf probe -f'\n"
2485 " or set 'force=yes' in BPF source.\n",
2491 /* Try to add suffix */
2492 for (i
= 1; i
< MAX_EVENT_INDEX
; i
++) {
2493 ret
= e_snprintf(buf
, len
, "%s_%d", nbase
, i
);
2495 pr_debug("snprintf() failed: %d\n", ret
);
2498 if (!strlist__has_entry(namelist
, buf
))
2501 if (i
== MAX_EVENT_INDEX
) {
2502 pr_warning("Too many events are on the same function.\n");
2511 /* Warn if the current kernel's uprobe implementation is old */
2512 static void warn_uprobe_event_compat(struct probe_trace_event
*tev
)
2515 char *buf
= synthesize_probe_trace_command(tev
);
2517 /* Old uprobe event doesn't support memory dereference */
2518 if (!tev
->uprobes
|| tev
->nargs
== 0 || !buf
)
2521 for (i
= 0; i
< tev
->nargs
; i
++)
2522 if (strglobmatch(tev
->args
[i
].value
, "[$@+-]*")) {
2523 pr_warning("Please upgrade your kernel to at least "
2524 "3.14 to have access to feature %s\n",
2525 tev
->args
[i
].value
);
2532 /* Set new name from original perf_probe_event and namelist */
2533 static int probe_trace_event__set_name(struct probe_trace_event
*tev
,
2534 struct perf_probe_event
*pev
,
2535 struct strlist
*namelist
,
2538 const char *event
, *group
;
2542 /* If probe_event or trace_event already have the name, reuse it */
2543 if (pev
->event
&& !pev
->sdt
)
2545 else if (tev
->event
)
2548 /* Or generate new one from probe point */
2549 if (pev
->point
.function
&&
2550 (strncmp(pev
->point
.function
, "0x", 2) != 0) &&
2551 !strisglob(pev
->point
.function
))
2552 event
= pev
->point
.function
;
2554 event
= tev
->point
.realname
;
2556 if (pev
->group
&& !pev
->sdt
)
2558 else if (tev
->group
)
2561 group
= PERFPROBE_GROUP
;
2563 /* Get an unused new event name */
2564 ret
= get_new_event_name(buf
, 64, event
,
2565 namelist
, allow_suffix
);
2571 tev
->event
= strdup(event
);
2572 tev
->group
= strdup(group
);
2573 if (tev
->event
== NULL
|| tev
->group
== NULL
)
2576 /* Add added event name to namelist */
2577 strlist__add(namelist
, event
);
2581 static int __open_probe_file_and_namelist(bool uprobe
,
2582 struct strlist
**namelist
)
2586 fd
= probe_file__open(PF_FL_RW
| (uprobe
? PF_FL_UPROBE
: 0));
2590 /* Get current event names */
2591 *namelist
= probe_file__get_namelist(fd
);
2593 pr_debug("Failed to get current event list.\n");
2600 static int __add_probe_trace_events(struct perf_probe_event
*pev
,
2601 struct probe_trace_event
*tevs
,
2602 int ntevs
, bool allow_suffix
)
2604 int i
, fd
[2] = {-1, -1}, up
, ret
;
2605 struct probe_trace_event
*tev
= NULL
;
2606 struct probe_cache
*cache
= NULL
;
2607 struct strlist
*namelist
[2] = {NULL
, NULL
};
2609 up
= pev
->uprobes
? 1 : 0;
2610 fd
[up
] = __open_probe_file_and_namelist(up
, &namelist
[up
]);
2615 for (i
= 0; i
< ntevs
; i
++) {
2617 up
= tev
->uprobes
? 1 : 0;
2618 if (fd
[up
] == -1) { /* Open the kprobe/uprobe_events */
2619 fd
[up
] = __open_probe_file_and_namelist(up
,
2624 /* Skip if the symbol is out of .text or blacklisted */
2625 if (!tev
->point
.symbol
&& !pev
->uprobes
)
2628 /* Set new name for tev (and update namelist) */
2629 ret
= probe_trace_event__set_name(tev
, pev
, namelist
[up
],
2634 ret
= probe_file__add_event(fd
[up
], tev
);
2639 * Probes after the first probe which comes from same
2640 * user input are always allowed to add suffix, because
2641 * there might be several addresses corresponding to
2644 allow_suffix
= true;
2646 if (ret
== -EINVAL
&& pev
->uprobes
)
2647 warn_uprobe_event_compat(tev
);
2648 if (ret
== 0 && probe_conf
.cache
) {
2649 cache
= probe_cache__new(pev
->target
);
2651 probe_cache__add_entry(cache
, pev
, tevs
, ntevs
) < 0 ||
2652 probe_cache__commit(cache
) < 0)
2653 pr_warning("Failed to add event to probe cache\n");
2654 probe_cache__delete(cache
);
2658 for (up
= 0; up
< 2; up
++) {
2659 strlist__delete(namelist
[up
]);
2666 static int find_probe_functions(struct map
*map
, char *name
,
2667 struct symbol
**syms
)
2671 struct rb_node
*tmp
;
2673 if (map__load(map
, NULL
) < 0)
2676 map__for_each_symbol(map
, sym
, tmp
) {
2677 if (strglobmatch(sym
->name
, name
)) {
2679 if (syms
&& found
< probe_conf
.max_probes
)
2680 syms
[found
- 1] = sym
;
2687 void __weak
arch__fix_tev_from_maps(struct perf_probe_event
*pev __maybe_unused
,
2688 struct probe_trace_event
*tev __maybe_unused
,
2689 struct map
*map __maybe_unused
,
2690 struct symbol
*sym __maybe_unused
) { }
2693 * Find probe function addresses from map.
2694 * Return an error or the number of found probe_trace_event
2696 static int find_probe_trace_events_from_map(struct perf_probe_event
*pev
,
2697 struct probe_trace_event
**tevs
)
2699 struct map
*map
= NULL
;
2700 struct ref_reloc_sym
*reloc_sym
= NULL
;
2702 struct symbol
**syms
= NULL
;
2703 struct probe_trace_event
*tev
;
2704 struct perf_probe_point
*pp
= &pev
->point
;
2705 struct probe_trace_point
*tp
;
2706 int num_matched_functions
;
2707 int ret
, i
, j
, skipped
= 0;
2710 map
= get_target_map(pev
->target
, pev
->uprobes
);
2716 syms
= malloc(sizeof(struct symbol
*) * probe_conf
.max_probes
);
2723 * Load matched symbols: Since the different local symbols may have
2724 * same name but different addresses, this lists all the symbols.
2726 num_matched_functions
= find_probe_functions(map
, pp
->function
, syms
);
2727 if (num_matched_functions
== 0) {
2728 pr_err("Failed to find symbol %s in %s\n", pp
->function
,
2729 pev
->target
? : "kernel");
2732 } else if (num_matched_functions
> probe_conf
.max_probes
) {
2733 pr_err("Too many functions matched in %s\n",
2734 pev
->target
? : "kernel");
2739 /* Note that the symbols in the kmodule are not relocated */
2740 if (!pev
->uprobes
&& !pp
->retprobe
&& !pev
->target
) {
2741 reloc_sym
= kernel_get_ref_reloc_sym();
2743 pr_warning("Relocated base symbol is not found!\n");
2749 /* Setup result trace-probe-events */
2750 *tevs
= zalloc(sizeof(*tev
) * num_matched_functions
);
2758 for (j
= 0; j
< num_matched_functions
; j
++) {
2761 tev
= (*tevs
) + ret
;
2763 if (ret
== num_matched_functions
) {
2764 pr_warning("Too many symbols are listed. Skip it.\n");
2769 if (pp
->offset
> sym
->end
- sym
->start
) {
2770 pr_warning("Offset %ld is bigger than the size of %s\n",
2771 pp
->offset
, sym
->name
);
2775 /* Add one probe point */
2776 tp
->address
= map
->unmap_ip(map
, sym
->start
) + pp
->offset
;
2778 /* Check the kprobe (not in module) is within .text */
2779 if (!pev
->uprobes
&& !pev
->target
&&
2780 kprobe_warn_out_range(sym
->name
, tp
->address
)) {
2781 tp
->symbol
= NULL
; /* Skip it */
2783 } else if (reloc_sym
) {
2784 tp
->symbol
= strdup_or_goto(reloc_sym
->name
, nomem_out
);
2785 tp
->offset
= tp
->address
- reloc_sym
->addr
;
2787 tp
->symbol
= strdup_or_goto(sym
->name
, nomem_out
);
2788 tp
->offset
= pp
->offset
;
2790 tp
->realname
= strdup_or_goto(sym
->name
, nomem_out
);
2792 tp
->retprobe
= pp
->retprobe
;
2795 tev
->point
.module
= strdup_or_goto(pev
->target
,
2798 mod_name
= find_module_name(pev
->target
);
2800 strdup(mod_name
? mod_name
: pev
->target
);
2802 if (!tev
->point
.module
)
2806 tev
->uprobes
= pev
->uprobes
;
2807 tev
->nargs
= pev
->nargs
;
2809 tev
->args
= zalloc(sizeof(struct probe_trace_arg
) *
2811 if (tev
->args
== NULL
)
2814 for (i
= 0; i
< tev
->nargs
; i
++) {
2815 if (pev
->args
[i
].name
)
2817 strdup_or_goto(pev
->args
[i
].name
,
2820 tev
->args
[i
].value
= strdup_or_goto(pev
->args
[i
].var
,
2822 if (pev
->args
[i
].type
)
2824 strdup_or_goto(pev
->args
[i
].type
,
2827 arch__fix_tev_from_maps(pev
, tev
, map
, sym
);
2829 if (ret
== skipped
) {
2835 put_target_map(map
, pev
->uprobes
);
2842 clear_probe_trace_events(*tevs
, num_matched_functions
);
2847 static int try_to_find_absolute_address(struct perf_probe_event
*pev
,
2848 struct probe_trace_event
**tevs
)
2850 struct perf_probe_point
*pp
= &pev
->point
;
2851 struct probe_trace_event
*tev
;
2852 struct probe_trace_point
*tp
;
2855 if (!(pev
->point
.function
&& !strncmp(pev
->point
.function
, "0x", 2)))
2857 if (perf_probe_event_need_dwarf(pev
))
2861 * This is 'perf probe /lib/libc.so 0xabcd'. Try to probe at
2864 * Only one tev can be generated by this.
2866 *tevs
= zalloc(sizeof(*tev
));
2874 * Don't use tp->offset, use address directly, because
2875 * in synthesize_probe_trace_command() address cannot be
2878 tp
->address
= pev
->point
.abs_address
;
2879 tp
->retprobe
= pp
->retprobe
;
2880 tev
->uprobes
= pev
->uprobes
;
2884 * Give it a '0x' leading symbol name.
2885 * In __add_probe_trace_events, a NULL symbol is interpreted as
2888 if (asprintf(&tp
->symbol
, "0x%lx", tp
->address
) < 0)
2891 /* For kprobe, check range */
2892 if ((!tev
->uprobes
) &&
2893 (kprobe_warn_out_range(tev
->point
.symbol
,
2894 tev
->point
.address
))) {
2899 if (asprintf(&tp
->realname
, "abs_%lx", tp
->address
) < 0)
2903 tp
->module
= strdup(pev
->target
);
2909 tev
->group
= strdup(pev
->group
);
2915 tev
->event
= strdup(pev
->event
);
2920 tev
->nargs
= pev
->nargs
;
2921 tev
->args
= zalloc(sizeof(struct probe_trace_arg
) * tev
->nargs
);
2926 for (i
= 0; i
< tev
->nargs
; i
++)
2927 copy_to_probe_trace_arg(&tev
->args
[i
], &pev
->args
[i
]);
2933 clear_probe_trace_events(*tevs
, 1);
2939 bool __weak
arch__prefers_symtab(void) { return false; }
2941 /* Concatinate two arrays */
2942 static void *memcat(void *a
, size_t sz_a
, void *b
, size_t sz_b
)
2946 ret
= malloc(sz_a
+ sz_b
);
2948 memcpy(ret
, a
, sz_a
);
2949 memcpy(ret
+ sz_a
, b
, sz_b
);
2955 concat_probe_trace_events(struct probe_trace_event
**tevs
, int *ntevs
,
2956 struct probe_trace_event
**tevs2
, int ntevs2
)
2958 struct probe_trace_event
*new_tevs
;
2968 if (*ntevs
+ ntevs2
> probe_conf
.max_probes
)
2971 /* Concatinate the array of probe_trace_event */
2972 new_tevs
= memcat(*tevs
, (*ntevs
) * sizeof(**tevs
),
2973 *tevs2
, ntevs2
* sizeof(**tevs2
));
2983 clear_probe_trace_events(*tevs2
, ntevs2
);
2990 * Try to find probe_trace_event from given probe caches. Return the number
2991 * of cached events found, if an error occurs return the error.
2993 static int find_cached_events(struct perf_probe_event
*pev
,
2994 struct probe_trace_event
**tevs
,
2997 struct probe_cache
*cache
;
2998 struct probe_cache_entry
*entry
;
2999 struct probe_trace_event
*tmp_tevs
= NULL
;
3003 cache
= probe_cache__new(target
);
3004 /* Return 0 ("not found") if the target has no probe cache. */
3008 for_each_probe_cache_entry(entry
, cache
) {
3009 /* Skip the cache entry which has no name */
3010 if (!entry
->pev
.event
|| !entry
->pev
.group
)
3012 if ((!pev
->group
|| strglobmatch(entry
->pev
.group
, pev
->group
)) &&
3013 strglobmatch(entry
->pev
.event
, pev
->event
)) {
3014 ret
= probe_cache_entry__get_event(entry
, &tmp_tevs
);
3016 ret
= concat_probe_trace_events(tevs
, &ntevs
,
3022 probe_cache__delete(cache
);
3024 clear_probe_trace_events(*tevs
, ntevs
);
3028 if (ntevs
> 0 && target
&& target
[0] == '/')
3029 pev
->uprobes
= true;
3035 /* Try to find probe_trace_event from all probe caches */
3036 static int find_cached_events_all(struct perf_probe_event
*pev
,
3037 struct probe_trace_event
**tevs
)
3039 struct probe_trace_event
*tmp_tevs
= NULL
;
3040 struct strlist
*bidlist
;
3041 struct str_node
*nd
;
3046 /* Get the buildid list of all valid caches */
3047 bidlist
= build_id_cache__list_all(true);
3050 pr_debug("Failed to get buildids: %d\n", ret
);
3055 strlist__for_each_entry(nd
, bidlist
) {
3056 pathname
= build_id_cache__origname(nd
->s
);
3057 ret
= find_cached_events(pev
, &tmp_tevs
, pathname
);
3058 /* In the case of cnt == 0, we just skip it */
3060 ret
= concat_probe_trace_events(tevs
, &ntevs
,
3066 strlist__delete(bidlist
);
3069 clear_probe_trace_events(*tevs
, ntevs
);
3077 static int find_probe_trace_events_from_cache(struct perf_probe_event
*pev
,
3078 struct probe_trace_event
**tevs
)
3080 struct probe_cache
*cache
;
3081 struct probe_cache_entry
*entry
;
3082 struct probe_trace_event
*tev
;
3083 struct str_node
*node
;
3087 /* For SDT/cached events, we use special search functions */
3089 return find_cached_events_all(pev
, tevs
);
3091 return find_cached_events(pev
, tevs
, pev
->target
);
3093 cache
= probe_cache__new(pev
->target
);
3097 entry
= probe_cache__find(cache
, pev
);
3099 /* SDT must be in the cache */
3100 ret
= pev
->sdt
? -ENOENT
: 0;
3104 ret
= strlist__nr_entries(entry
->tevlist
);
3105 if (ret
> probe_conf
.max_probes
) {
3106 pr_debug("Too many entries matched in the cache of %s\n",
3107 pev
->target
? : "kernel");
3112 *tevs
= zalloc(ret
* sizeof(*tev
));
3119 strlist__for_each_entry(node
, entry
->tevlist
) {
3120 tev
= &(*tevs
)[i
++];
3121 ret
= parse_probe_trace_command(node
->s
, tev
);
3124 /* Set the uprobes attribute as same as original */
3125 tev
->uprobes
= pev
->uprobes
;
3130 probe_cache__delete(cache
);
3134 static int convert_to_probe_trace_events(struct perf_probe_event
*pev
,
3135 struct probe_trace_event
**tevs
)
3139 if (!pev
->group
&& !pev
->sdt
) {
3140 /* Set group name if not given */
3141 if (!pev
->uprobes
) {
3142 pev
->group
= strdup(PERFPROBE_GROUP
);
3143 ret
= pev
->group
? 0 : -ENOMEM
;
3145 ret
= convert_exec_to_group(pev
->target
, &pev
->group
);
3147 pr_warning("Failed to make a group name.\n");
3152 ret
= try_to_find_absolute_address(pev
, tevs
);
3156 /* At first, we need to lookup cache entry */
3157 ret
= find_probe_trace_events_from_cache(pev
, tevs
);
3158 if (ret
> 0 || pev
->sdt
) /* SDT can be found only in the cache */
3159 return ret
== 0 ? -ENOENT
: ret
; /* Found in probe cache */
3161 if (arch__prefers_symtab() && !perf_probe_event_need_dwarf(pev
)) {
3162 ret
= find_probe_trace_events_from_map(pev
, tevs
);
3164 return ret
; /* Found in symbol table */
3167 /* Convert perf_probe_event with debuginfo */
3168 ret
= try_to_find_probe_trace_events(pev
, tevs
);
3170 return ret
; /* Found in debuginfo or got an error */
3172 return find_probe_trace_events_from_map(pev
, tevs
);
3175 int convert_perf_probe_events(struct perf_probe_event
*pevs
, int npevs
)
3179 /* Loop 1: convert all events */
3180 for (i
= 0; i
< npevs
; i
++) {
3181 /* Init kprobe blacklist if needed */
3182 if (!pevs
[i
].uprobes
)
3183 kprobe_blacklist__init();
3184 /* Convert with or without debuginfo */
3185 ret
= convert_to_probe_trace_events(&pevs
[i
], &pevs
[i
].tevs
);
3188 pevs
[i
].ntevs
= ret
;
3190 /* This just release blacklist only if allocated */
3191 kprobe_blacklist__release();
3196 int apply_perf_probe_events(struct perf_probe_event
*pevs
, int npevs
)
3200 /* Loop 2: add all events */
3201 for (i
= 0; i
< npevs
; i
++) {
3202 ret
= __add_probe_trace_events(&pevs
[i
], pevs
[i
].tevs
,
3204 probe_conf
.force_add
);
3211 void cleanup_perf_probe_events(struct perf_probe_event
*pevs
, int npevs
)
3215 /* Loop 3: cleanup and free trace events */
3216 for (i
= 0; i
< npevs
; i
++) {
3217 for (j
= 0; j
< pevs
[i
].ntevs
; j
++)
3218 clear_probe_trace_event(&pevs
[i
].tevs
[j
]);
3219 zfree(&pevs
[i
].tevs
);
3221 clear_perf_probe_event(&pevs
[i
]);
3225 int add_perf_probe_events(struct perf_probe_event
*pevs
, int npevs
)
3229 ret
= init_probe_symbol_maps(pevs
->uprobes
);
3233 ret
= convert_perf_probe_events(pevs
, npevs
);
3235 ret
= apply_perf_probe_events(pevs
, npevs
);
3237 cleanup_perf_probe_events(pevs
, npevs
);
3239 exit_probe_symbol_maps();
3243 int del_perf_probe_events(struct strfilter
*filter
)
3245 int ret
, ret2
, ufd
= -1, kfd
= -1;
3246 char *str
= strfilter__string(filter
);
3251 /* Get current event names */
3252 ret
= probe_file__open_both(&kfd
, &ufd
, PF_FL_RW
);
3256 ret
= probe_file__del_events(kfd
, filter
);
3257 if (ret
< 0 && ret
!= -ENOENT
)
3260 ret2
= probe_file__del_events(ufd
, filter
);
3261 if (ret2
< 0 && ret2
!= -ENOENT
) {
3278 /* TODO: don't use a global variable for filter ... */
3279 static struct strfilter
*available_func_filter
;
3282 * If a symbol corresponds to a function with global binding and
3283 * matches filter return 0. For all others return 1.
3285 static int filter_available_functions(struct map
*map __maybe_unused
,
3288 if (strfilter__compare(available_func_filter
, sym
->name
))
3293 int show_available_funcs(const char *target
, struct strfilter
*_filter
,
3299 ret
= init_probe_symbol_maps(user
);
3303 /* Get a symbol map */
3305 map
= dso__new_map(target
);
3307 map
= kernel_get_module_map(target
);
3309 pr_err("Failed to get a map for %s\n", (target
) ? : "kernel");
3313 /* Load symbols with given filter */
3314 available_func_filter
= _filter
;
3315 ret
= map__load(map
, filter_available_functions
);
3318 char *str
= strfilter__string(_filter
);
3319 pr_err("Failed to find symbols matched to \"%s\"\n",
3323 pr_err("Failed to load symbols in %s\n",
3324 (target
) ? : "kernel");
3327 if (!dso__sorted_by_name(map
->dso
, map
->type
))
3328 dso__sort_by_name(map
->dso
, map
->type
);
3330 /* Show all (filtered) symbols */
3332 dso__fprintf_symbols_by_name(map
->dso
, map
->type
, stdout
);
3337 exit_probe_symbol_maps();
3342 int copy_to_probe_trace_arg(struct probe_trace_arg
*tvar
,
3343 struct perf_probe_arg
*pvar
)
3345 tvar
->value
= strdup(pvar
->var
);
3346 if (tvar
->value
== NULL
)
3349 tvar
->type
= strdup(pvar
->type
);
3350 if (tvar
->type
== NULL
)
3354 tvar
->name
= strdup(pvar
->name
);
3355 if (tvar
->name
== NULL
)