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/debugfs.h>
44 #include <api/fs/tracefs.h>
45 #include "trace-event.h" /* For __maybe_unused */
46 #include "probe-event.h"
47 #include "probe-finder.h"
48 #include "probe-file.h"
51 #define MAX_CMDLEN 256
52 #define PERFPROBE_GROUP "probe"
54 bool probe_event_dry_run
; /* Dry run flag */
55 struct probe_conf probe_conf
;
57 #define semantic_error(msg ...) pr_err("Semantic error :" msg)
59 int e_snprintf(char *str
, size_t size
, const char *format
, ...)
64 ret
= vsnprintf(str
, size
, format
, ap
);
71 static char *synthesize_perf_probe_point(struct perf_probe_point
*pp
);
72 static struct machine
*host_machine
;
74 /* Initialize symbol maps and path of vmlinux/modules */
75 static int init_symbol_maps(bool user_only
)
79 symbol_conf
.sort_by_name
= true;
80 symbol_conf
.allow_aliases
= true;
81 ret
= symbol__init(NULL
);
83 pr_debug("Failed to init symbol map.\n");
87 if (host_machine
|| user_only
) /* already initialized */
90 if (symbol_conf
.vmlinux_name
)
91 pr_debug("Use vmlinux: %s\n", symbol_conf
.vmlinux_name
);
93 host_machine
= machine__new_host();
95 pr_debug("machine__new_host() failed.\n");
101 pr_warning("Failed to init vmlinux path.\n");
105 static void exit_symbol_maps(void)
108 machine__delete(host_machine
);
114 static struct symbol
*__find_kernel_function_by_name(const char *name
,
117 return machine__find_kernel_function_by_name(host_machine
, name
, mapp
,
121 static struct symbol
*__find_kernel_function(u64 addr
, struct map
**mapp
)
123 return machine__find_kernel_function(host_machine
, addr
, mapp
, NULL
);
126 static struct ref_reloc_sym
*kernel_get_ref_reloc_sym(void)
128 /* kmap->ref_reloc_sym should be set if host_machine is initialized */
131 if (map__load(host_machine
->vmlinux_maps
[MAP__FUNCTION
], NULL
) < 0)
134 kmap
= map__kmap(host_machine
->vmlinux_maps
[MAP__FUNCTION
]);
137 return kmap
->ref_reloc_sym
;
140 static u64
kernel_get_symbol_address_by_name(const char *name
, bool reloc
)
142 struct ref_reloc_sym
*reloc_sym
;
146 /* ref_reloc_sym is just a label. Need a special fix*/
147 reloc_sym
= kernel_get_ref_reloc_sym();
148 if (reloc_sym
&& strcmp(name
, reloc_sym
->name
) == 0)
149 return (reloc
) ? reloc_sym
->addr
: reloc_sym
->unrelocated_addr
;
151 sym
= __find_kernel_function_by_name(name
, &map
);
153 return map
->unmap_ip(map
, sym
->start
) -
154 ((reloc
) ? 0 : map
->reloc
);
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
)
250 /* Get the address of _etext for checking non-probable text symbol */
251 etext_addr
= kernel_get_symbol_address_by_name("_etext", false);
253 if (etext_addr
!= 0 && etext_addr
< address
)
254 pr_warning("%s is out of .text, skip it.\n", symbol
);
255 else if (kprobe_blacklist__listed(address
))
256 pr_warning("%s is blacklisted function, skip it.\n", symbol
);
263 #ifdef HAVE_DWARF_SUPPORT
265 static int kernel_get_module_dso(const char *module
, struct dso
**pdso
)
269 const char *vmlinux_name
;
273 list_for_each_entry(dso
, &host_machine
->dsos
.head
, node
) {
276 if (strncmp(dso
->short_name
+ 1, module
,
277 dso
->short_name_len
- 2) == 0)
280 pr_debug("Failed to find module %s.\n", module
);
284 map
= host_machine
->vmlinux_maps
[MAP__FUNCTION
];
287 vmlinux_name
= symbol_conf
.vmlinux_name
;
290 ret
= dso__load_vmlinux(dso
, map
, vmlinux_name
, false, NULL
);
292 ret
= dso__load_vmlinux_path(dso
, map
, NULL
);
299 * Some binaries like glibc have special symbols which are on the symbol
300 * table, but not in the debuginfo. If we can find the address of the
301 * symbol from map, we can translate the address back to the probe point.
303 static int find_alternative_probe_point(struct debuginfo
*dinfo
,
304 struct perf_probe_point
*pp
,
305 struct perf_probe_point
*result
,
306 const char *target
, bool uprobes
)
308 struct map
*map
= NULL
;
313 /* This can work only for function-name based one */
314 if (!pp
->function
|| pp
->file
)
317 map
= get_target_map(target
, uprobes
);
321 /* Find the address of given function */
322 map__for_each_symbol_by_name(map
, pp
->function
, sym
) {
324 address
= sym
->start
;
326 address
= map
->unmap_ip(map
, sym
->start
);
333 pr_debug("Symbol %s address found : %" PRIx64
"\n",
334 pp
->function
, address
);
336 ret
= debuginfo__find_probe_point(dinfo
, (unsigned long)address
,
339 ret
= (!ret
) ? -ENOENT
: ret
;
341 result
->offset
+= pp
->offset
;
342 result
->line
+= pp
->line
;
343 result
->retprobe
= pp
->retprobe
;
348 put_target_map(map
, uprobes
);
353 static int get_alternative_probe_event(struct debuginfo
*dinfo
,
354 struct perf_probe_event
*pev
,
355 struct perf_probe_point
*tmp
)
359 memcpy(tmp
, &pev
->point
, sizeof(*tmp
));
360 memset(&pev
->point
, 0, sizeof(pev
->point
));
361 ret
= find_alternative_probe_point(dinfo
, tmp
, &pev
->point
,
362 pev
->target
, pev
->uprobes
);
364 memcpy(&pev
->point
, tmp
, sizeof(*tmp
));
369 static int get_alternative_line_range(struct debuginfo
*dinfo
,
370 struct line_range
*lr
,
371 const char *target
, bool user
)
373 struct perf_probe_point pp
= { .function
= lr
->function
,
376 struct perf_probe_point result
;
379 memset(&result
, 0, sizeof(result
));
381 if (lr
->end
!= INT_MAX
)
382 len
= lr
->end
- lr
->start
;
383 ret
= find_alternative_probe_point(dinfo
, &pp
, &result
,
386 lr
->function
= result
.function
;
387 lr
->file
= result
.file
;
388 lr
->start
= result
.line
;
389 if (lr
->end
!= INT_MAX
)
390 lr
->end
= lr
->start
+ len
;
391 clear_perf_probe_point(&pp
);
396 /* Open new debuginfo of given module */
397 static struct debuginfo
*open_debuginfo(const char *module
, bool silent
)
399 const char *path
= module
;
400 char reason
[STRERR_BUFSIZE
];
401 struct debuginfo
*ret
= NULL
;
402 struct dso
*dso
= NULL
;
405 if (!module
|| !strchr(module
, '/')) {
406 err
= kernel_get_module_dso(module
, &dso
);
408 if (!dso
|| dso
->load_errno
== 0) {
409 if (!strerror_r(-err
, reason
, STRERR_BUFSIZE
))
410 strcpy(reason
, "(unknown)");
412 dso__strerror_load(dso
, reason
, STRERR_BUFSIZE
);
414 pr_err("Failed to find the path for %s: %s\n",
415 module
?: "kernel", reason
);
418 path
= dso
->long_name
;
420 ret
= debuginfo__new(path
);
421 if (!ret
&& !silent
) {
422 pr_warning("The %s file has no debug information.\n", path
);
423 if (!module
|| !strtailcmp(path
, ".ko"))
424 pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, ");
426 pr_warning("Rebuild with -g, ");
427 pr_warning("or install an appropriate debuginfo package.\n");
432 /* For caching the last debuginfo */
433 static struct debuginfo
*debuginfo_cache
;
434 static char *debuginfo_cache_path
;
436 static struct debuginfo
*debuginfo_cache__open(const char *module
, bool silent
)
438 if ((debuginfo_cache_path
&& !strcmp(debuginfo_cache_path
, module
)) ||
439 (!debuginfo_cache_path
&& !module
&& debuginfo_cache
))
442 /* Copy module path */
443 free(debuginfo_cache_path
);
445 debuginfo_cache_path
= strdup(module
);
446 if (!debuginfo_cache_path
) {
447 debuginfo__delete(debuginfo_cache
);
448 debuginfo_cache
= NULL
;
453 debuginfo_cache
= open_debuginfo(module
, silent
);
454 if (!debuginfo_cache
)
455 zfree(&debuginfo_cache_path
);
457 return debuginfo_cache
;
460 static void debuginfo_cache__exit(void)
462 debuginfo__delete(debuginfo_cache
);
463 debuginfo_cache
= NULL
;
464 zfree(&debuginfo_cache_path
);
468 static int get_text_start_address(const char *exec
, unsigned long *address
)
473 int fd
, ret
= -ENOENT
;
475 fd
= open(exec
, O_RDONLY
);
479 elf
= elf_begin(fd
, PERF_ELF_C_READ_MMAP
, NULL
);
483 if (gelf_getehdr(elf
, &ehdr
) == NULL
)
486 if (!elf_section_by_name(elf
, &ehdr
, &shdr
, ".text", NULL
))
489 *address
= shdr
.sh_addr
- shdr
.sh_offset
;
497 * Convert trace point to probe point with debuginfo
499 static int find_perf_probe_point_from_dwarf(struct probe_trace_point
*tp
,
500 struct perf_probe_point
*pp
,
503 struct debuginfo
*dinfo
= NULL
;
504 unsigned long stext
= 0;
505 u64 addr
= tp
->address
;
508 /* convert the address to dwarf address */
514 ret
= get_text_start_address(tp
->module
, &stext
);
519 addr
= kernel_get_symbol_address_by_name(tp
->symbol
, false);
525 pr_debug("try to find information at %" PRIx64
" in %s\n", addr
,
526 tp
->module
? : "kernel");
528 dinfo
= debuginfo_cache__open(tp
->module
, verbose
== 0);
530 ret
= debuginfo__find_probe_point(dinfo
,
531 (unsigned long)addr
, pp
);
536 pp
->retprobe
= tp
->retprobe
;
540 pr_debug("Failed to find corresponding probes from debuginfo.\n");
541 return ret
? : -ENOENT
;
544 static int add_exec_to_probe_trace_events(struct probe_trace_event
*tevs
,
545 int ntevs
, const char *exec
)
548 unsigned long stext
= 0;
553 ret
= get_text_start_address(exec
, &stext
);
557 for (i
= 0; i
< ntevs
&& ret
>= 0; i
++) {
558 /* point.address is the addres of point.symbol + point.offset */
559 tevs
[i
].point
.address
-= stext
;
560 tevs
[i
].point
.module
= strdup(exec
);
561 if (!tevs
[i
].point
.module
) {
565 tevs
[i
].uprobes
= true;
571 static int add_module_to_probe_trace_events(struct probe_trace_event
*tevs
,
572 int ntevs
, const char *module
)
580 tmp
= strrchr(module
, '/');
582 /* This is a module path -- get the module name */
583 module
= strdup(tmp
+ 1);
586 tmp
= strchr(module
, '.');
589 tmp
= (char *)module
; /* For free() */
592 for (i
= 0; i
< ntevs
; i
++) {
593 tevs
[i
].point
.module
= strdup(module
);
594 if (!tevs
[i
].point
.module
) {
604 /* Post processing the probe events */
605 static int post_process_probe_trace_events(struct probe_trace_event
*tevs
,
606 int ntevs
, const char *module
,
609 struct ref_reloc_sym
*reloc_sym
;
614 return add_exec_to_probe_trace_events(tevs
, ntevs
, module
);
616 /* Note that currently ref_reloc_sym based probe is not for drivers */
618 return add_module_to_probe_trace_events(tevs
, ntevs
, module
);
620 reloc_sym
= kernel_get_ref_reloc_sym();
622 pr_warning("Relocated base symbol is not found!\n");
626 for (i
= 0; i
< ntevs
; i
++) {
627 if (!tevs
[i
].point
.address
|| tevs
[i
].point
.retprobe
)
629 /* If we found a wrong one, mark it by NULL symbol */
630 if (kprobe_warn_out_range(tevs
[i
].point
.symbol
,
631 tevs
[i
].point
.address
)) {
635 tmp
= strdup(reloc_sym
->name
);
639 /* If we have no realname, use symbol for it */
640 if (!tevs
[i
].point
.realname
)
641 tevs
[i
].point
.realname
= tevs
[i
].point
.symbol
;
643 free(tevs
[i
].point
.symbol
);
644 tevs
[i
].point
.symbol
= tmp
;
645 tevs
[i
].point
.offset
= tevs
[i
].point
.address
-
646 reloc_sym
->unrelocated_addr
;
651 /* Try to find perf_probe_event with debuginfo */
652 static int try_to_find_probe_trace_events(struct perf_probe_event
*pev
,
653 struct probe_trace_event
**tevs
)
655 bool need_dwarf
= perf_probe_event_need_dwarf(pev
);
656 struct perf_probe_point tmp
;
657 struct debuginfo
*dinfo
;
660 dinfo
= open_debuginfo(pev
->target
, !need_dwarf
);
664 pr_debug("Could not open debuginfo. Try to use symbols.\n");
668 pr_debug("Try to find probe point from debuginfo.\n");
669 /* Searching trace events corresponding to a probe event */
670 ntevs
= debuginfo__find_trace_events(dinfo
, pev
, tevs
);
672 if (ntevs
== 0) { /* Not found, retry with an alternative */
673 ret
= get_alternative_probe_event(dinfo
, pev
, &tmp
);
675 ntevs
= debuginfo__find_trace_events(dinfo
, pev
, tevs
);
677 * Write back to the original probe_event for
678 * setting appropriate (user given) event name
680 clear_perf_probe_point(&pev
->point
);
681 memcpy(&pev
->point
, &tmp
, sizeof(tmp
));
685 debuginfo__delete(dinfo
);
687 if (ntevs
> 0) { /* Succeeded to find trace events */
688 pr_debug("Found %d probe_trace_events.\n", ntevs
);
689 ret
= post_process_probe_trace_events(*tevs
, ntevs
,
690 pev
->target
, pev
->uprobes
);
691 if (ret
< 0 || ret
== ntevs
) {
692 clear_probe_trace_events(*tevs
, ntevs
);
696 return ret
< 0 ? ret
: ntevs
;
701 if (ntevs
== 0) { /* No error but failed to find probe point. */
702 pr_warning("Probe point '%s' not found.\n",
703 synthesize_perf_probe_point(&pev
->point
));
706 /* Error path : ntevs < 0 */
707 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs
);
708 if (ntevs
== -EBADF
) {
709 pr_warning("Warning: No dwarf info found in the vmlinux - "
710 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
712 pr_debug("Trying to use symbols.\n");
719 #define LINEBUF_SIZE 256
720 #define NR_ADDITIONAL_LINES 2
722 static int __show_one_line(FILE *fp
, int l
, bool skip
, bool show_num
)
724 char buf
[LINEBUF_SIZE
], sbuf
[STRERR_BUFSIZE
];
725 const char *color
= show_num
? "" : PERF_COLOR_BLUE
;
726 const char *prefix
= NULL
;
729 if (fgets(buf
, LINEBUF_SIZE
, fp
) == NULL
)
734 prefix
= show_num
? "%7d " : " ";
735 color_fprintf(stdout
, color
, prefix
, l
);
737 color_fprintf(stdout
, color
, "%s", buf
);
739 } while (strchr(buf
, '\n') == NULL
);
744 pr_warning("File read error: %s\n",
745 strerror_r(errno
, sbuf
, sizeof(sbuf
)));
751 static int _show_one_line(FILE *fp
, int l
, bool skip
, bool show_num
)
753 int rv
= __show_one_line(fp
, l
, skip
, show_num
);
755 pr_warning("Source file is shorter than expected.\n");
761 #define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
762 #define show_one_line(f,l) _show_one_line(f,l,false,false)
763 #define skip_one_line(f,l) _show_one_line(f,l,true,false)
764 #define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
767 * Show line-range always requires debuginfo to find source file and
770 static int __show_line_range(struct line_range
*lr
, const char *module
,
775 struct debuginfo
*dinfo
;
779 char sbuf
[STRERR_BUFSIZE
];
781 /* Search a line range */
782 dinfo
= open_debuginfo(module
, false);
786 ret
= debuginfo__find_line_range(dinfo
, lr
);
787 if (!ret
) { /* Not found, retry with an alternative */
788 ret
= get_alternative_line_range(dinfo
, lr
, module
, user
);
790 ret
= debuginfo__find_line_range(dinfo
, lr
);
792 debuginfo__delete(dinfo
);
793 if (ret
== 0 || ret
== -ENOENT
) {
794 pr_warning("Specified source line is not found.\n");
796 } else if (ret
< 0) {
797 pr_warning("Debuginfo analysis failed.\n");
801 /* Convert source file path */
803 ret
= get_real_path(tmp
, lr
->comp_dir
, &lr
->path
);
805 /* Free old path when new path is assigned */
810 pr_warning("Failed to find source file path.\n");
817 fprintf(stdout
, "<%s@%s:%d>\n", lr
->function
, lr
->path
,
818 lr
->start
- lr
->offset
);
820 fprintf(stdout
, "<%s:%d>\n", lr
->path
, lr
->start
);
822 fp
= fopen(lr
->path
, "r");
824 pr_warning("Failed to open %s: %s\n", lr
->path
,
825 strerror_r(errno
, sbuf
, sizeof(sbuf
)));
828 /* Skip to starting line number */
829 while (l
< lr
->start
) {
830 ret
= skip_one_line(fp
, l
++);
835 intlist__for_each(ln
, lr
->line_list
) {
836 for (; ln
->i
> l
; l
++) {
837 ret
= show_one_line(fp
, l
- lr
->offset
);
841 ret
= show_one_line_with_num(fp
, l
++ - lr
->offset
);
846 if (lr
->end
== INT_MAX
)
847 lr
->end
= l
+ NR_ADDITIONAL_LINES
;
848 while (l
<= lr
->end
) {
849 ret
= show_one_line_or_eof(fp
, l
++ - lr
->offset
);
858 int show_line_range(struct line_range
*lr
, const char *module
, bool user
)
862 ret
= init_symbol_maps(user
);
865 ret
= __show_line_range(lr
, module
, user
);
871 static int show_available_vars_at(struct debuginfo
*dinfo
,
872 struct perf_probe_event
*pev
,
873 struct strfilter
*_filter
)
877 struct str_node
*node
;
878 struct variable_list
*vls
= NULL
, *vl
;
879 struct perf_probe_point tmp
;
882 buf
= synthesize_perf_probe_point(&pev
->point
);
885 pr_debug("Searching variables at %s\n", buf
);
887 ret
= debuginfo__find_available_vars_at(dinfo
, pev
, &vls
);
888 if (!ret
) { /* Not found, retry with an alternative */
889 ret
= get_alternative_probe_event(dinfo
, pev
, &tmp
);
891 ret
= debuginfo__find_available_vars_at(dinfo
, pev
,
893 /* Release the old probe_point */
894 clear_perf_probe_point(&tmp
);
898 if (ret
== 0 || ret
== -ENOENT
) {
899 pr_err("Failed to find the address of %s\n", buf
);
902 pr_warning("Debuginfo analysis failed.\n");
906 /* Some variables are found */
907 fprintf(stdout
, "Available variables at %s\n", buf
);
908 for (i
= 0; i
< ret
; i
++) {
911 * A probe point might be converted to
912 * several trace points.
914 fprintf(stdout
, "\t@<%s+%lu>\n", vl
->point
.symbol
,
916 zfree(&vl
->point
.symbol
);
919 strlist__for_each(node
, vl
->vars
) {
920 var
= strchr(node
->s
, '\t') + 1;
921 if (strfilter__compare(_filter
, var
)) {
922 fprintf(stdout
, "\t\t%s\n", node
->s
);
926 strlist__delete(vl
->vars
);
929 fprintf(stdout
, "\t\t(No matched variables)\n");
937 /* Show available variables on given probe point */
938 int show_available_vars(struct perf_probe_event
*pevs
, int npevs
,
939 struct strfilter
*_filter
)
942 struct debuginfo
*dinfo
;
944 ret
= init_symbol_maps(pevs
->uprobes
);
948 dinfo
= open_debuginfo(pevs
->target
, false);
956 for (i
= 0; i
< npevs
&& ret
>= 0; i
++)
957 ret
= show_available_vars_at(dinfo
, &pevs
[i
], _filter
);
959 debuginfo__delete(dinfo
);
965 #else /* !HAVE_DWARF_SUPPORT */
967 static void debuginfo_cache__exit(void)
972 find_perf_probe_point_from_dwarf(struct probe_trace_point
*tp __maybe_unused
,
973 struct perf_probe_point
*pp __maybe_unused
,
974 bool is_kprobe __maybe_unused
)
979 static int try_to_find_probe_trace_events(struct perf_probe_event
*pev
,
980 struct probe_trace_event
**tevs __maybe_unused
)
982 if (perf_probe_event_need_dwarf(pev
)) {
983 pr_warning("Debuginfo-analysis is not supported.\n");
990 int show_line_range(struct line_range
*lr __maybe_unused
,
991 const char *module __maybe_unused
,
992 bool user __maybe_unused
)
994 pr_warning("Debuginfo-analysis is not supported.\n");
998 int show_available_vars(struct perf_probe_event
*pevs __maybe_unused
,
999 int npevs __maybe_unused
,
1000 struct strfilter
*filter __maybe_unused
)
1002 pr_warning("Debuginfo-analysis is not supported.\n");
1007 void line_range__clear(struct line_range
*lr
)
1013 intlist__delete(lr
->line_list
);
1014 memset(lr
, 0, sizeof(*lr
));
1017 int line_range__init(struct line_range
*lr
)
1019 memset(lr
, 0, sizeof(*lr
));
1020 lr
->line_list
= intlist__new(NULL
);
1027 static int parse_line_num(char **ptr
, int *val
, const char *what
)
1029 const char *start
= *ptr
;
1032 *val
= strtol(*ptr
, ptr
, 0);
1033 if (errno
|| *ptr
== start
) {
1034 semantic_error("'%s' is not a valid number.\n", what
);
1040 /* Check the name is good for event, group or function */
1041 static bool is_c_func_name(const char *name
)
1043 if (!isalpha(*name
) && *name
!= '_')
1045 while (*++name
!= '\0') {
1046 if (!isalpha(*name
) && !isdigit(*name
) && *name
!= '_')
1053 * Stuff 'lr' according to the line range described by 'arg'.
1054 * The line range syntax is described by:
1056 * SRC[:SLN[+NUM|-ELN]]
1057 * FNC[@SRC][:SLN[+NUM|-ELN]]
1059 int parse_line_range_desc(const char *arg
, struct line_range
*lr
)
1061 char *range
, *file
, *name
= strdup(arg
);
1070 range
= strchr(name
, ':');
1074 err
= parse_line_num(&range
, &lr
->start
, "start line");
1078 if (*range
== '+' || *range
== '-') {
1079 const char c
= *range
++;
1081 err
= parse_line_num(&range
, &lr
->end
, "end line");
1086 lr
->end
+= lr
->start
;
1088 * Adjust the number of lines here.
1089 * If the number of lines == 1, the
1090 * the end of line should be equal to
1091 * the start of line.
1097 pr_debug("Line range is %d to %d\n", lr
->start
, lr
->end
);
1100 if (lr
->start
> lr
->end
) {
1101 semantic_error("Start line must be smaller"
1102 " than end line.\n");
1105 if (*range
!= '\0') {
1106 semantic_error("Tailing with invalid str '%s'.\n", range
);
1111 file
= strchr(name
, '@');
1114 lr
->file
= strdup(++file
);
1115 if (lr
->file
== NULL
) {
1119 lr
->function
= name
;
1120 } else if (strchr(name
, '/') || strchr(name
, '.'))
1122 else if (is_c_func_name(name
))/* We reuse it for checking funcname */
1123 lr
->function
= name
;
1124 else { /* Invalid name */
1125 semantic_error("'%s' is not a valid function name.\n", name
);
1136 /* Parse probepoint definition. */
1137 static int parse_perf_probe_point(char *arg
, struct perf_probe_event
*pev
)
1139 struct perf_probe_point
*pp
= &pev
->point
;
1142 bool file_spec
= false;
1145 * perf probe [EVENT=]SRC[:LN|;PTN]
1146 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
1148 * TODO:Group name support
1153 ptr
= strpbrk(arg
, ";=@+%");
1154 if (ptr
&& *ptr
== '=') { /* Event name */
1157 if (strchr(arg
, ':')) {
1158 semantic_error("Group name is not supported yet.\n");
1161 if (!is_c_func_name(arg
)) {
1162 semantic_error("%s is bad for event name -it must "
1163 "follow C symbol-naming rule.\n", arg
);
1166 pev
->event
= strdup(arg
);
1167 if (pev
->event
== NULL
)
1174 * Check arg is function or file name and copy it.
1176 * We consider arg to be a file spec if and only if it satisfies
1177 * all of the below criteria::
1178 * - it does not include any of "+@%",
1179 * - it includes one of ":;", and
1180 * - it has a period '.' in the name.
1182 * Otherwise, we consider arg to be a function specification.
1184 if (!strpbrk(arg
, "+@%") && (ptr
= strpbrk(arg
, ";:")) != NULL
) {
1185 /* This is a file spec if it includes a '.' before ; or : */
1186 if (memchr(arg
, '.', ptr
- arg
))
1190 ptr
= strpbrk(arg
, ";:+@%");
1205 /* Parse other options */
1209 if (c
== ';') { /* Lazy pattern must be the last part */
1210 pp
->lazy_line
= strdup(arg
);
1211 if (pp
->lazy_line
== NULL
)
1215 ptr
= strpbrk(arg
, ";:+@%");
1221 case ':': /* Line number */
1222 pp
->line
= strtoul(arg
, &tmp
, 0);
1224 semantic_error("There is non-digit char"
1225 " in line number.\n");
1229 case '+': /* Byte offset from a symbol */
1230 pp
->offset
= strtoul(arg
, &tmp
, 0);
1232 semantic_error("There is non-digit character"
1237 case '@': /* File name */
1239 semantic_error("SRC@SRC is not allowed.\n");
1242 pp
->file
= strdup(arg
);
1243 if (pp
->file
== NULL
)
1246 case '%': /* Probe places */
1247 if (strcmp(arg
, "return") == 0) {
1249 } else { /* Others not supported yet */
1250 semantic_error("%%%s is not supported.\n", arg
);
1254 default: /* Buggy case */
1255 pr_err("This program has a bug at %s:%d.\n",
1256 __FILE__
, __LINE__
);
1262 /* Exclusion check */
1263 if (pp
->lazy_line
&& pp
->line
) {
1264 semantic_error("Lazy pattern can't be used with"
1269 if (pp
->lazy_line
&& pp
->offset
) {
1270 semantic_error("Lazy pattern can't be used with offset.\n");
1274 if (pp
->line
&& pp
->offset
) {
1275 semantic_error("Offset can't be used with line number.\n");
1279 if (!pp
->line
&& !pp
->lazy_line
&& pp
->file
&& !pp
->function
) {
1280 semantic_error("File always requires line number or "
1285 if (pp
->offset
&& !pp
->function
) {
1286 semantic_error("Offset requires an entry function.\n");
1290 if (pp
->retprobe
&& !pp
->function
) {
1291 semantic_error("Return probe requires an entry function.\n");
1295 if ((pp
->offset
|| pp
->line
|| pp
->lazy_line
) && pp
->retprobe
) {
1296 semantic_error("Offset/Line/Lazy pattern can't be used with "
1301 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
1302 pp
->function
, pp
->file
, pp
->line
, pp
->offset
, pp
->retprobe
,
1307 /* Parse perf-probe event argument */
1308 static int parse_perf_probe_arg(char *str
, struct perf_probe_arg
*arg
)
1310 char *tmp
, *goodname
;
1311 struct perf_probe_arg_field
**fieldp
;
1313 pr_debug("parsing arg: %s into ", str
);
1315 tmp
= strchr(str
, '=');
1317 arg
->name
= strndup(str
, tmp
- str
);
1318 if (arg
->name
== NULL
)
1320 pr_debug("name:%s ", arg
->name
);
1324 tmp
= strchr(str
, ':');
1325 if (tmp
) { /* Type setting */
1327 arg
->type
= strdup(tmp
+ 1);
1328 if (arg
->type
== NULL
)
1330 pr_debug("type:%s ", arg
->type
);
1333 tmp
= strpbrk(str
, "-.[");
1334 if (!is_c_varname(str
) || !tmp
) {
1335 /* A variable, register, symbol or special value */
1336 arg
->var
= strdup(str
);
1337 if (arg
->var
== NULL
)
1339 pr_debug("%s\n", arg
->var
);
1343 /* Structure fields or array element */
1344 arg
->var
= strndup(str
, tmp
- str
);
1345 if (arg
->var
== NULL
)
1347 goodname
= arg
->var
;
1348 pr_debug("%s, ", arg
->var
);
1349 fieldp
= &arg
->field
;
1352 *fieldp
= zalloc(sizeof(struct perf_probe_arg_field
));
1353 if (*fieldp
== NULL
)
1355 if (*tmp
== '[') { /* Array */
1357 (*fieldp
)->index
= strtol(str
+ 1, &tmp
, 0);
1358 (*fieldp
)->ref
= true;
1359 if (*tmp
!= ']' || tmp
== str
+ 1) {
1360 semantic_error("Array index must be a"
1367 } else { /* Structure */
1370 (*fieldp
)->ref
= false;
1371 } else if (tmp
[1] == '>') {
1373 (*fieldp
)->ref
= true;
1375 semantic_error("Argument parse error: %s\n",
1379 tmp
= strpbrk(str
, "-.[");
1382 (*fieldp
)->name
= strndup(str
, tmp
- str
);
1383 if ((*fieldp
)->name
== NULL
)
1386 goodname
= (*fieldp
)->name
;
1387 pr_debug("%s(%d), ", (*fieldp
)->name
, (*fieldp
)->ref
);
1388 fieldp
= &(*fieldp
)->next
;
1391 (*fieldp
)->name
= strdup(str
);
1392 if ((*fieldp
)->name
== NULL
)
1395 goodname
= (*fieldp
)->name
;
1396 pr_debug("%s(%d)\n", (*fieldp
)->name
, (*fieldp
)->ref
);
1398 /* If no name is specified, set the last field name (not array index)*/
1400 arg
->name
= strdup(goodname
);
1401 if (arg
->name
== NULL
)
1407 /* Parse perf-probe event command */
1408 int parse_perf_probe_command(const char *cmd
, struct perf_probe_event
*pev
)
1411 int argc
, i
, ret
= 0;
1413 argv
= argv_split(cmd
, &argc
);
1415 pr_debug("Failed to split arguments.\n");
1418 if (argc
- 1 > MAX_PROBE_ARGS
) {
1419 semantic_error("Too many probe arguments (%d).\n", argc
- 1);
1423 /* Parse probe point */
1424 ret
= parse_perf_probe_point(argv
[0], pev
);
1428 /* Copy arguments and ensure return probe has no C argument */
1429 pev
->nargs
= argc
- 1;
1430 pev
->args
= zalloc(sizeof(struct perf_probe_arg
) * pev
->nargs
);
1431 if (pev
->args
== NULL
) {
1435 for (i
= 0; i
< pev
->nargs
&& ret
>= 0; i
++) {
1436 ret
= parse_perf_probe_arg(argv
[i
+ 1], &pev
->args
[i
]);
1438 is_c_varname(pev
->args
[i
].var
) && pev
->point
.retprobe
) {
1439 semantic_error("You can't specify local variable for"
1450 /* Return true if this perf_probe_event requires debuginfo */
1451 bool perf_probe_event_need_dwarf(struct perf_probe_event
*pev
)
1455 if (pev
->point
.file
|| pev
->point
.line
|| pev
->point
.lazy_line
)
1458 for (i
= 0; i
< pev
->nargs
; i
++)
1459 if (is_c_varname(pev
->args
[i
].var
))
1465 /* Parse probe_events event into struct probe_point */
1466 int parse_probe_trace_command(const char *cmd
, struct probe_trace_event
*tev
)
1468 struct probe_trace_point
*tp
= &tev
->point
;
1471 char *argv0_str
= NULL
, *fmt
, *fmt1_str
, *fmt2_str
, *fmt3_str
;
1475 pr_debug("Parsing probe_events: %s\n", cmd
);
1476 argv
= argv_split(cmd
, &argc
);
1478 pr_debug("Failed to split arguments.\n");
1482 semantic_error("Too few probe arguments.\n");
1487 /* Scan event and group name. */
1488 argv0_str
= strdup(argv
[0]);
1489 if (argv0_str
== NULL
) {
1493 fmt1_str
= strtok_r(argv0_str
, ":", &fmt
);
1494 fmt2_str
= strtok_r(NULL
, "/", &fmt
);
1495 fmt3_str
= strtok_r(NULL
, " \t", &fmt
);
1496 if (fmt1_str
== NULL
|| strlen(fmt1_str
) != 1 || fmt2_str
== NULL
1497 || fmt3_str
== NULL
) {
1498 semantic_error("Failed to parse event name: %s\n", argv
[0]);
1503 tev
->group
= strdup(fmt2_str
);
1504 tev
->event
= strdup(fmt3_str
);
1505 if (tev
->group
== NULL
|| tev
->event
== NULL
) {
1509 pr_debug("Group:%s Event:%s probe:%c\n", tev
->group
, tev
->event
, pr
);
1511 tp
->retprobe
= (pr
== 'r');
1513 /* Scan module name(if there), function name and offset */
1514 p
= strchr(argv
[1], ':');
1516 tp
->module
= strndup(argv
[1], p
- argv
[1]);
1520 fmt1_str
= strtok_r(p
, "+", &fmt
);
1521 if (fmt1_str
[0] == '0') /* only the address started with 0x */
1522 tp
->address
= strtoul(fmt1_str
, NULL
, 0);
1524 /* Only the symbol-based probe has offset */
1525 tp
->symbol
= strdup(fmt1_str
);
1526 if (tp
->symbol
== NULL
) {
1530 fmt2_str
= strtok_r(NULL
, "", &fmt
);
1531 if (fmt2_str
== NULL
)
1534 tp
->offset
= strtoul(fmt2_str
, NULL
, 10);
1537 tev
->nargs
= argc
- 2;
1538 tev
->args
= zalloc(sizeof(struct probe_trace_arg
) * tev
->nargs
);
1539 if (tev
->args
== NULL
) {
1543 for (i
= 0; i
< tev
->nargs
; i
++) {
1544 p
= strchr(argv
[i
+ 2], '=');
1545 if (p
) /* We don't need which register is assigned. */
1549 tev
->args
[i
].name
= strdup(argv
[i
+ 2]);
1550 /* TODO: parse regs and offset */
1551 tev
->args
[i
].value
= strdup(p
);
1552 if (tev
->args
[i
].name
== NULL
|| tev
->args
[i
].value
== NULL
) {
1564 /* Compose only probe arg */
1565 int synthesize_perf_probe_arg(struct perf_probe_arg
*pa
, char *buf
, size_t len
)
1567 struct perf_probe_arg_field
*field
= pa
->field
;
1571 if (pa
->name
&& pa
->var
)
1572 ret
= e_snprintf(tmp
, len
, "%s=%s", pa
->name
, pa
->var
);
1574 ret
= e_snprintf(tmp
, len
, "%s", pa
->name
? pa
->name
: pa
->var
);
1581 if (field
->name
[0] == '[')
1582 ret
= e_snprintf(tmp
, len
, "%s", field
->name
);
1584 ret
= e_snprintf(tmp
, len
, "%s%s",
1585 field
->ref
? "->" : ".", field
->name
);
1590 field
= field
->next
;
1594 ret
= e_snprintf(tmp
, len
, ":%s", pa
->type
);
1603 pr_debug("Failed to synthesize perf probe argument: %d\n", ret
);
1607 /* Compose only probe point (not argument) */
1608 static char *synthesize_perf_probe_point(struct perf_probe_point
*pp
)
1611 char offs
[32] = "", line
[32] = "", file
[32] = "";
1614 buf
= zalloc(MAX_CMDLEN
);
1620 ret
= e_snprintf(offs
, 32, "+%lu", pp
->offset
);
1625 ret
= e_snprintf(line
, 32, ":%d", pp
->line
);
1633 tmp
= strchr(pp
->file
+ len
- 30, '/');
1634 tmp
= tmp
? tmp
+ 1 : pp
->file
+ len
- 30;
1636 ret
= e_snprintf(file
, 32, "@%s", tmp
);
1642 ret
= e_snprintf(buf
, MAX_CMDLEN
, "%s%s%s%s%s", pp
->function
,
1643 offs
, pp
->retprobe
? "%return" : "", line
,
1646 ret
= e_snprintf(buf
, MAX_CMDLEN
, "%s%s", file
, line
);
1652 pr_debug("Failed to synthesize perf probe point: %d\n", ret
);
1658 char *synthesize_perf_probe_command(struct perf_probe_event
*pev
)
1663 buf
= synthesize_perf_probe_point(&pev
->point
);
1668 for (i
= 0; i
< pev
->nargs
; i
++) {
1669 ret
= e_snprintf(&buf
[len
], MAX_CMDLEN
- len
, " %s",
1682 static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref
*ref
,
1683 char **buf
, size_t *buflen
,
1688 depth
= __synthesize_probe_trace_arg_ref(ref
->next
, buf
,
1694 ret
= e_snprintf(*buf
, *buflen
, "%+ld(", ref
->offset
);
1706 static int synthesize_probe_trace_arg(struct probe_trace_arg
*arg
,
1707 char *buf
, size_t buflen
)
1709 struct probe_trace_arg_ref
*ref
= arg
->ref
;
1713 /* Argument name or separator */
1715 ret
= e_snprintf(buf
, buflen
, " %s=", arg
->name
);
1717 ret
= e_snprintf(buf
, buflen
, " ");
1723 /* Special case: @XXX */
1724 if (arg
->value
[0] == '@' && arg
->ref
)
1727 /* Dereferencing arguments */
1729 depth
= __synthesize_probe_trace_arg_ref(ref
, &buf
,
1735 /* Print argument value */
1736 if (arg
->value
[0] == '@' && arg
->ref
)
1737 ret
= e_snprintf(buf
, buflen
, "%s%+ld", arg
->value
,
1740 ret
= e_snprintf(buf
, buflen
, "%s", arg
->value
);
1748 ret
= e_snprintf(buf
, buflen
, ")");
1754 /* Print argument type */
1756 ret
= e_snprintf(buf
, buflen
, ":%s", arg
->type
);
1765 char *synthesize_probe_trace_command(struct probe_trace_event
*tev
)
1767 struct probe_trace_point
*tp
= &tev
->point
;
1771 buf
= zalloc(MAX_CMDLEN
);
1775 len
= e_snprintf(buf
, MAX_CMDLEN
, "%c:%s/%s ", tp
->retprobe
? 'r' : 'p',
1776 tev
->group
, tev
->event
);
1780 /* Uprobes must have tp->address and tp->module */
1781 if (tev
->uprobes
&& (!tp
->address
|| !tp
->module
))
1784 /* Use the tp->address for uprobes */
1786 ret
= e_snprintf(buf
+ len
, MAX_CMDLEN
- len
, "%s:0x%lx",
1787 tp
->module
, tp
->address
);
1789 ret
= e_snprintf(buf
+ len
, MAX_CMDLEN
- len
, "%s%s%s+%lu",
1790 tp
->module
?: "", tp
->module
? ":" : "",
1791 tp
->symbol
, tp
->offset
);
1797 for (i
= 0; i
< tev
->nargs
; i
++) {
1798 ret
= synthesize_probe_trace_arg(&tev
->args
[i
], buf
+ len
,
1811 static int find_perf_probe_point_from_map(struct probe_trace_point
*tp
,
1812 struct perf_probe_point
*pp
,
1815 struct symbol
*sym
= NULL
;
1821 map
= dso__new_map(tp
->module
);
1825 sym
= map__find_symbol(map
, addr
, NULL
);
1827 addr
= kernel_get_symbol_address_by_name(tp
->symbol
, true);
1830 sym
= __find_kernel_function(addr
, &map
);
1836 pp
->retprobe
= tp
->retprobe
;
1837 pp
->offset
= addr
- map
->unmap_ip(map
, sym
->start
);
1838 pp
->function
= strdup(sym
->name
);
1839 ret
= pp
->function
? 0 : -ENOMEM
;
1842 if (map
&& !is_kprobe
) {
1849 static int convert_to_perf_probe_point(struct probe_trace_point
*tp
,
1850 struct perf_probe_point
*pp
,
1856 ret
= find_perf_probe_point_from_dwarf(tp
, pp
, is_kprobe
);
1859 ret
= find_perf_probe_point_from_map(tp
, pp
, is_kprobe
);
1863 pr_debug("Failed to find probe point from both of dwarf and map.\n");
1866 pp
->function
= strdup(tp
->symbol
);
1867 pp
->offset
= tp
->offset
;
1868 } else if (!tp
->module
&& !is_kprobe
) {
1869 ret
= e_snprintf(buf
, 128, "0x%" PRIx64
, (u64
)tp
->address
);
1872 pp
->function
= strdup(buf
);
1875 if (pp
->function
== NULL
)
1878 pp
->retprobe
= tp
->retprobe
;
1883 static int convert_to_perf_probe_event(struct probe_trace_event
*tev
,
1884 struct perf_probe_event
*pev
, bool is_kprobe
)
1889 /* Convert event/group name */
1890 pev
->event
= strdup(tev
->event
);
1891 pev
->group
= strdup(tev
->group
);
1892 if (pev
->event
== NULL
|| pev
->group
== NULL
)
1895 /* Convert trace_point to probe_point */
1896 ret
= convert_to_perf_probe_point(&tev
->point
, &pev
->point
, is_kprobe
);
1900 /* Convert trace_arg to probe_arg */
1901 pev
->nargs
= tev
->nargs
;
1902 pev
->args
= zalloc(sizeof(struct perf_probe_arg
) * pev
->nargs
);
1903 if (pev
->args
== NULL
)
1905 for (i
= 0; i
< tev
->nargs
&& ret
>= 0; i
++) {
1906 if (tev
->args
[i
].name
)
1907 pev
->args
[i
].name
= strdup(tev
->args
[i
].name
);
1909 ret
= synthesize_probe_trace_arg(&tev
->args
[i
],
1911 pev
->args
[i
].name
= strdup(buf
);
1913 if (pev
->args
[i
].name
== NULL
&& ret
>= 0)
1918 clear_perf_probe_event(pev
);
1923 void clear_perf_probe_event(struct perf_probe_event
*pev
)
1925 struct perf_probe_arg_field
*field
, *next
;
1931 clear_perf_probe_point(&pev
->point
);
1933 for (i
= 0; i
< pev
->nargs
; i
++) {
1934 free(pev
->args
[i
].name
);
1935 free(pev
->args
[i
].var
);
1936 free(pev
->args
[i
].type
);
1937 field
= pev
->args
[i
].field
;
1940 zfree(&field
->name
);
1946 memset(pev
, 0, sizeof(*pev
));
1949 void clear_probe_trace_event(struct probe_trace_event
*tev
)
1951 struct probe_trace_arg_ref
*ref
, *next
;
1956 free(tev
->point
.symbol
);
1957 free(tev
->point
.realname
);
1958 free(tev
->point
.module
);
1959 for (i
= 0; i
< tev
->nargs
; i
++) {
1960 free(tev
->args
[i
].name
);
1961 free(tev
->args
[i
].value
);
1962 free(tev
->args
[i
].type
);
1963 ref
= tev
->args
[i
].ref
;
1971 memset(tev
, 0, sizeof(*tev
));
1974 struct kprobe_blacklist_node
{
1975 struct list_head list
;
1976 unsigned long start
;
1981 static void kprobe_blacklist__delete(struct list_head
*blacklist
)
1983 struct kprobe_blacklist_node
*node
;
1985 while (!list_empty(blacklist
)) {
1986 node
= list_first_entry(blacklist
,
1987 struct kprobe_blacklist_node
, list
);
1988 list_del(&node
->list
);
1994 static int kprobe_blacklist__load(struct list_head
*blacklist
)
1996 struct kprobe_blacklist_node
*node
;
1997 const char *__debugfs
= debugfs_find_mountpoint();
1998 char buf
[PATH_MAX
], *p
;
2002 if (__debugfs
== NULL
)
2005 ret
= e_snprintf(buf
, PATH_MAX
, "%s/kprobes/blacklist", __debugfs
);
2009 fp
= fopen(buf
, "r");
2014 while (fgets(buf
, PATH_MAX
, fp
)) {
2015 node
= zalloc(sizeof(*node
));
2020 INIT_LIST_HEAD(&node
->list
);
2021 list_add_tail(&node
->list
, blacklist
);
2022 if (sscanf(buf
, "0x%lx-0x%lx", &node
->start
, &node
->end
) != 2) {
2026 p
= strchr(buf
, '\t');
2029 if (p
[strlen(p
) - 1] == '\n')
2030 p
[strlen(p
) - 1] = '\0';
2032 p
= (char *)"unknown";
2033 node
->symbol
= strdup(p
);
2034 if (!node
->symbol
) {
2038 pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
2039 node
->start
, node
->end
, node
->symbol
);
2043 kprobe_blacklist__delete(blacklist
);
2049 static struct kprobe_blacklist_node
*
2050 kprobe_blacklist__find_by_address(struct list_head
*blacklist
,
2051 unsigned long address
)
2053 struct kprobe_blacklist_node
*node
;
2055 list_for_each_entry(node
, blacklist
, list
) {
2056 if (node
->start
<= address
&& address
<= node
->end
)
2063 static LIST_HEAD(kprobe_blacklist
);
2065 static void kprobe_blacklist__init(void)
2067 if (!list_empty(&kprobe_blacklist
))
2070 if (kprobe_blacklist__load(&kprobe_blacklist
) < 0)
2071 pr_debug("No kprobe blacklist support, ignored\n");
2074 static void kprobe_blacklist__release(void)
2076 kprobe_blacklist__delete(&kprobe_blacklist
);
2079 static bool kprobe_blacklist__listed(unsigned long address
)
2081 return !!kprobe_blacklist__find_by_address(&kprobe_blacklist
, address
);
2084 static int perf_probe_event__sprintf(const char *group
, const char *event
,
2085 struct perf_probe_event
*pev
,
2087 struct strbuf
*result
)
2093 /* Synthesize only event probe point */
2094 place
= synthesize_perf_probe_point(&pev
->point
);
2098 ret
= e_snprintf(buf
, 128, "%s:%s", group
, event
);
2102 strbuf_addf(result
, " %-20s (on %s", buf
, place
);
2104 strbuf_addf(result
, " in %s", module
);
2106 if (pev
->nargs
> 0) {
2107 strbuf_addstr(result
, " with");
2108 for (i
= 0; i
< pev
->nargs
; i
++) {
2109 ret
= synthesize_perf_probe_arg(&pev
->args
[i
],
2113 strbuf_addf(result
, " %s", buf
);
2116 strbuf_addch(result
, ')');
2123 static int show_perf_probe_event(const char *group
, const char *event
,
2124 struct perf_probe_event
*pev
,
2125 const char *module
, bool use_stdout
)
2127 struct strbuf buf
= STRBUF_INIT
;
2130 ret
= perf_probe_event__sprintf(group
, event
, pev
, module
, &buf
);
2133 printf("%s\n", buf
.buf
);
2135 pr_info("%s\n", buf
.buf
);
2137 strbuf_release(&buf
);
2142 static bool filter_probe_trace_event(struct probe_trace_event
*tev
,
2143 struct strfilter
*filter
)
2147 /* At first, check the event name itself */
2148 if (strfilter__compare(filter
, tev
->event
))
2151 /* Next, check the combination of name and group */
2152 if (e_snprintf(tmp
, 128, "%s:%s", tev
->group
, tev
->event
) < 0)
2154 return strfilter__compare(filter
, tmp
);
2157 static int __show_perf_probe_events(int fd
, bool is_kprobe
,
2158 struct strfilter
*filter
)
2161 struct probe_trace_event tev
;
2162 struct perf_probe_event pev
;
2163 struct strlist
*rawlist
;
2164 struct str_node
*ent
;
2166 memset(&tev
, 0, sizeof(tev
));
2167 memset(&pev
, 0, sizeof(pev
));
2169 rawlist
= probe_file__get_rawlist(fd
);
2173 strlist__for_each(ent
, rawlist
) {
2174 ret
= parse_probe_trace_command(ent
->s
, &tev
);
2176 if (!filter_probe_trace_event(&tev
, filter
))
2178 ret
= convert_to_perf_probe_event(&tev
, &pev
,
2182 ret
= show_perf_probe_event(pev
.group
, pev
.event
,
2183 &pev
, tev
.point
.module
,
2187 clear_perf_probe_event(&pev
);
2188 clear_probe_trace_event(&tev
);
2192 strlist__delete(rawlist
);
2193 /* Cleanup cached debuginfo if needed */
2194 debuginfo_cache__exit();
2199 /* List up current perf-probe events */
2200 int show_perf_probe_events(struct strfilter
*filter
)
2202 int kp_fd
, up_fd
, ret
;
2206 ret
= init_symbol_maps(false);
2210 ret
= probe_file__open_both(&kp_fd
, &up_fd
, 0);
2215 ret
= __show_perf_probe_events(kp_fd
, true, filter
);
2216 if (up_fd
>= 0 && ret
>= 0)
2217 ret
= __show_perf_probe_events(up_fd
, false, filter
);
2227 static int get_new_event_name(char *buf
, size_t len
, const char *base
,
2228 struct strlist
*namelist
, bool allow_suffix
)
2237 ret
= e_snprintf(buf
, len
, "%s", base
);
2239 pr_debug("snprintf() failed: %d\n", ret
);
2242 /* Cut off the postfixes (e.g. .const, .isra)*/
2243 p
= strchr(buf
, '.');
2246 if (!strlist__has_entry(namelist
, buf
))
2249 if (!allow_suffix
) {
2250 pr_warning("Error: event \"%s\" already exists. "
2251 "(Use -f to force duplicates.)\n", base
);
2255 /* Try to add suffix */
2256 for (i
= 1; i
< MAX_EVENT_INDEX
; i
++) {
2257 ret
= e_snprintf(buf
, len
, "%s_%d", base
, i
);
2259 pr_debug("snprintf() failed: %d\n", ret
);
2262 if (!strlist__has_entry(namelist
, buf
))
2265 if (i
== MAX_EVENT_INDEX
) {
2266 pr_warning("Too many events are on the same function.\n");
2273 /* Warn if the current kernel's uprobe implementation is old */
2274 static void warn_uprobe_event_compat(struct probe_trace_event
*tev
)
2277 char *buf
= synthesize_probe_trace_command(tev
);
2279 /* Old uprobe event doesn't support memory dereference */
2280 if (!tev
->uprobes
|| tev
->nargs
== 0 || !buf
)
2283 for (i
= 0; i
< tev
->nargs
; i
++)
2284 if (strglobmatch(tev
->args
[i
].value
, "[$@+-]*")) {
2285 pr_warning("Please upgrade your kernel to at least "
2286 "3.14 to have access to feature %s\n",
2287 tev
->args
[i
].value
);
2294 /* Set new name from original perf_probe_event and namelist */
2295 static int probe_trace_event__set_name(struct probe_trace_event
*tev
,
2296 struct perf_probe_event
*pev
,
2297 struct strlist
*namelist
,
2300 const char *event
, *group
;
2307 if (pev
->point
.function
&& !strisglob(pev
->point
.function
))
2308 event
= pev
->point
.function
;
2310 event
= tev
->point
.realname
;
2314 group
= PERFPROBE_GROUP
;
2316 /* Get an unused new event name */
2317 ret
= get_new_event_name(buf
, 64, event
,
2318 namelist
, allow_suffix
);
2324 tev
->event
= strdup(event
);
2325 tev
->group
= strdup(group
);
2326 if (tev
->event
== NULL
|| tev
->group
== NULL
)
2329 /* Add added event name to namelist */
2330 strlist__add(namelist
, event
);
2334 static int __add_probe_trace_events(struct perf_probe_event
*pev
,
2335 struct probe_trace_event
*tevs
,
2336 int ntevs
, bool allow_suffix
)
2339 struct probe_trace_event
*tev
= NULL
;
2340 const char *event
= NULL
, *group
= NULL
;
2341 struct strlist
*namelist
;
2343 fd
= probe_file__open(PF_FL_RW
| (pev
->uprobes
? PF_FL_UPROBE
: 0));
2347 /* Get current event names */
2348 namelist
= probe_file__get_namelist(fd
);
2350 pr_debug("Failed to get current event list.\n");
2356 pr_info("Added new event%s\n", (ntevs
> 1) ? "s:" : ":");
2357 for (i
= 0; i
< ntevs
; i
++) {
2359 /* Skip if the symbol is out of .text or blacklisted */
2360 if (!tev
->point
.symbol
)
2363 /* Set new name for tev (and update namelist) */
2364 ret
= probe_trace_event__set_name(tev
, pev
, namelist
,
2369 ret
= probe_file__add_event(fd
, tev
);
2373 /* We use tev's name for showing new events */
2374 show_perf_probe_event(tev
->group
, tev
->event
, pev
,
2375 tev
->point
.module
, false);
2376 /* Save the last valid name */
2381 * Probes after the first probe which comes from same
2382 * user input are always allowed to add suffix, because
2383 * there might be several addresses corresponding to
2386 allow_suffix
= true;
2388 if (ret
== -EINVAL
&& pev
->uprobes
)
2389 warn_uprobe_event_compat(tev
);
2391 /* Note that it is possible to skip all events because of blacklist */
2392 if (ret
>= 0 && event
) {
2393 /* Show how to use the event. */
2394 pr_info("\nYou can now use it in all perf tools, such as:\n\n");
2395 pr_info("\tperf record -e %s:%s -aR sleep 1\n\n", group
, event
);
2398 strlist__delete(namelist
);
2404 static int find_probe_functions(struct map
*map
, char *name
,
2405 struct symbol
**syms
)
2409 struct rb_node
*tmp
;
2411 if (map__load(map
, NULL
) < 0)
2414 map__for_each_symbol(map
, sym
, tmp
) {
2415 if (strglobmatch(sym
->name
, name
)) {
2417 if (syms
&& found
< probe_conf
.max_probes
)
2418 syms
[found
- 1] = sym
;
2425 #define strdup_or_goto(str, label) \
2426 ({ char *__p = strdup(str); if (!__p) goto label; __p; })
2428 void __weak
arch__fix_tev_from_maps(struct perf_probe_event
*pev __maybe_unused
,
2429 struct probe_trace_event
*tev __maybe_unused
,
2430 struct map
*map __maybe_unused
) { }
2433 * Find probe function addresses from map.
2434 * Return an error or the number of found probe_trace_event
2436 static int find_probe_trace_events_from_map(struct perf_probe_event
*pev
,
2437 struct probe_trace_event
**tevs
)
2439 struct map
*map
= NULL
;
2440 struct ref_reloc_sym
*reloc_sym
= NULL
;
2442 struct symbol
**syms
= NULL
;
2443 struct probe_trace_event
*tev
;
2444 struct perf_probe_point
*pp
= &pev
->point
;
2445 struct probe_trace_point
*tp
;
2446 int num_matched_functions
;
2447 int ret
, i
, j
, skipped
= 0;
2449 map
= get_target_map(pev
->target
, pev
->uprobes
);
2455 syms
= malloc(sizeof(struct symbol
*) * probe_conf
.max_probes
);
2462 * Load matched symbols: Since the different local symbols may have
2463 * same name but different addresses, this lists all the symbols.
2465 num_matched_functions
= find_probe_functions(map
, pp
->function
, syms
);
2466 if (num_matched_functions
== 0) {
2467 pr_err("Failed to find symbol %s in %s\n", pp
->function
,
2468 pev
->target
? : "kernel");
2471 } else if (num_matched_functions
> probe_conf
.max_probes
) {
2472 pr_err("Too many functions matched in %s\n",
2473 pev
->target
? : "kernel");
2478 if (!pev
->uprobes
&& !pp
->retprobe
) {
2479 reloc_sym
= kernel_get_ref_reloc_sym();
2481 pr_warning("Relocated base symbol is not found!\n");
2487 /* Setup result trace-probe-events */
2488 *tevs
= zalloc(sizeof(*tev
) * num_matched_functions
);
2496 for (j
= 0; j
< num_matched_functions
; j
++) {
2499 tev
= (*tevs
) + ret
;
2501 if (ret
== num_matched_functions
) {
2502 pr_warning("Too many symbols are listed. Skip it.\n");
2507 if (pp
->offset
> sym
->end
- sym
->start
) {
2508 pr_warning("Offset %ld is bigger than the size of %s\n",
2509 pp
->offset
, sym
->name
);
2513 /* Add one probe point */
2514 tp
->address
= map
->unmap_ip(map
, sym
->start
) + pp
->offset
;
2515 /* If we found a wrong one, mark it by NULL symbol */
2516 if (!pev
->uprobes
&&
2517 kprobe_warn_out_range(sym
->name
, tp
->address
)) {
2518 tp
->symbol
= NULL
; /* Skip it */
2520 } else if (reloc_sym
) {
2521 tp
->symbol
= strdup_or_goto(reloc_sym
->name
, nomem_out
);
2522 tp
->offset
= tp
->address
- reloc_sym
->addr
;
2524 tp
->symbol
= strdup_or_goto(sym
->name
, nomem_out
);
2525 tp
->offset
= pp
->offset
;
2527 tp
->realname
= strdup_or_goto(sym
->name
, nomem_out
);
2529 tp
->retprobe
= pp
->retprobe
;
2531 tev
->point
.module
= strdup_or_goto(pev
->target
,
2533 tev
->uprobes
= pev
->uprobes
;
2534 tev
->nargs
= pev
->nargs
;
2536 tev
->args
= zalloc(sizeof(struct probe_trace_arg
) *
2538 if (tev
->args
== NULL
)
2541 for (i
= 0; i
< tev
->nargs
; i
++) {
2542 if (pev
->args
[i
].name
)
2544 strdup_or_goto(pev
->args
[i
].name
,
2547 tev
->args
[i
].value
= strdup_or_goto(pev
->args
[i
].var
,
2549 if (pev
->args
[i
].type
)
2551 strdup_or_goto(pev
->args
[i
].type
,
2554 arch__fix_tev_from_maps(pev
, tev
, map
);
2556 if (ret
== skipped
) {
2562 put_target_map(map
, pev
->uprobes
);
2569 clear_probe_trace_events(*tevs
, num_matched_functions
);
2574 bool __weak
arch__prefers_symtab(void) { return false; }
2576 static int convert_to_probe_trace_events(struct perf_probe_event
*pev
,
2577 struct probe_trace_event
**tevs
)
2581 if (pev
->uprobes
&& !pev
->group
) {
2582 /* Replace group name if not given */
2583 ret
= convert_exec_to_group(pev
->target
, &pev
->group
);
2585 pr_warning("Failed to make a group name.\n");
2590 if (arch__prefers_symtab() && !perf_probe_event_need_dwarf(pev
)) {
2591 ret
= find_probe_trace_events_from_map(pev
, tevs
);
2593 return ret
; /* Found in symbol table */
2596 /* Convert perf_probe_event with debuginfo */
2597 ret
= try_to_find_probe_trace_events(pev
, tevs
);
2599 return ret
; /* Found in debuginfo or got an error */
2601 return find_probe_trace_events_from_map(pev
, tevs
);
2604 struct __event_package
{
2605 struct perf_probe_event
*pev
;
2606 struct probe_trace_event
*tevs
;
2610 int add_perf_probe_events(struct perf_probe_event
*pevs
, int npevs
)
2613 struct __event_package
*pkgs
;
2616 pkgs
= zalloc(sizeof(struct __event_package
) * npevs
);
2621 ret
= init_symbol_maps(pevs
->uprobes
);
2627 /* Loop 1: convert all events */
2628 for (i
= 0; i
< npevs
; i
++) {
2629 pkgs
[i
].pev
= &pevs
[i
];
2630 /* Init kprobe blacklist if needed */
2631 if (!pkgs
[i
].pev
->uprobes
)
2632 kprobe_blacklist__init();
2633 /* Convert with or without debuginfo */
2634 ret
= convert_to_probe_trace_events(pkgs
[i
].pev
,
2638 pkgs
[i
].ntevs
= ret
;
2640 /* This just release blacklist only if allocated */
2641 kprobe_blacklist__release();
2643 /* Loop 2: add all events */
2644 for (i
= 0; i
< npevs
; i
++) {
2645 ret
= __add_probe_trace_events(pkgs
[i
].pev
, pkgs
[i
].tevs
,
2647 probe_conf
.force_add
);
2652 /* Loop 3: cleanup and free trace events */
2653 for (i
= 0; i
< npevs
; i
++) {
2654 for (j
= 0; j
< pkgs
[i
].ntevs
; j
++)
2655 clear_probe_trace_event(&pkgs
[i
].tevs
[j
]);
2656 zfree(&pkgs
[i
].tevs
);
2664 int del_perf_probe_events(struct strfilter
*filter
)
2666 int ret
, ret2
, ufd
= -1, kfd
= -1;
2667 char *str
= strfilter__string(filter
);
2672 pr_debug("Delete filter: \'%s\'\n", str
);
2674 /* Get current event names */
2675 ret
= probe_file__open_both(&kfd
, &ufd
, PF_FL_RW
);
2679 ret
= probe_file__del_events(kfd
, filter
);
2680 if (ret
< 0 && ret
!= -ENOENT
)
2683 ret2
= probe_file__del_events(ufd
, filter
);
2684 if (ret2
< 0 && ret2
!= -ENOENT
) {
2688 if (ret
== -ENOENT
&& ret2
== -ENOENT
)
2689 pr_debug("\"%s\" does not hit any event.\n", str
);
2690 /* Note that this is silently ignored */
2704 /* TODO: don't use a global variable for filter ... */
2705 static struct strfilter
*available_func_filter
;
2708 * If a symbol corresponds to a function with global binding and
2709 * matches filter return 0. For all others return 1.
2711 static int filter_available_functions(struct map
*map __maybe_unused
,
2714 if (strfilter__compare(available_func_filter
, sym
->name
))
2719 int show_available_funcs(const char *target
, struct strfilter
*_filter
,
2725 ret
= init_symbol_maps(user
);
2729 /* Get a symbol map */
2731 map
= dso__new_map(target
);
2733 map
= kernel_get_module_map(target
);
2735 pr_err("Failed to get a map for %s\n", (target
) ? : "kernel");
2739 /* Load symbols with given filter */
2740 available_func_filter
= _filter
;
2741 if (map__load(map
, filter_available_functions
)) {
2742 pr_err("Failed to load symbols in %s\n", (target
) ? : "kernel");
2745 if (!dso__sorted_by_name(map
->dso
, map
->type
))
2746 dso__sort_by_name(map
->dso
, map
->type
);
2748 /* Show all (filtered) symbols */
2750 dso__fprintf_symbols_by_name(map
->dso
, map
->type
, stdout
);