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"
50 #define MAX_CMDLEN 256
51 #define PERFPROBE_GROUP "probe"
53 bool probe_event_dry_run
; /* Dry run flag */
55 #define semantic_error(msg ...) pr_err("Semantic error :" msg)
57 /* If there is no space to write, returns -E2BIG. */
58 static int e_snprintf(char *str
, size_t size
, const char *format
, ...)
59 __attribute__((format(printf
, 3, 4)));
61 static int e_snprintf(char *str
, size_t size
, const char *format
, ...)
66 ret
= vsnprintf(str
, size
, format
, ap
);
73 static char *synthesize_perf_probe_point(struct perf_probe_point
*pp
);
74 static void clear_probe_trace_event(struct probe_trace_event
*tev
);
75 static struct machine
*host_machine
;
77 /* Initialize symbol maps and path of vmlinux/modules */
78 static int init_symbol_maps(bool user_only
)
82 symbol_conf
.sort_by_name
= true;
83 symbol_conf
.allow_aliases
= true;
84 ret
= symbol__init(NULL
);
86 pr_debug("Failed to init symbol map.\n");
90 if (host_machine
|| user_only
) /* already initialized */
93 if (symbol_conf
.vmlinux_name
)
94 pr_debug("Use vmlinux: %s\n", symbol_conf
.vmlinux_name
);
96 host_machine
= machine__new_host();
98 pr_debug("machine__new_host() failed.\n");
104 pr_warning("Failed to init vmlinux path.\n");
108 static void exit_symbol_maps(void)
111 machine__delete(host_machine
);
117 static struct symbol
*__find_kernel_function_by_name(const char *name
,
120 return machine__find_kernel_function_by_name(host_machine
, name
, mapp
,
124 static struct symbol
*__find_kernel_function(u64 addr
, struct map
**mapp
)
126 return machine__find_kernel_function(host_machine
, addr
, mapp
, NULL
);
129 static struct ref_reloc_sym
*kernel_get_ref_reloc_sym(void)
131 /* kmap->ref_reloc_sym should be set if host_machine is initialized */
134 if (map__load(host_machine
->vmlinux_maps
[MAP__FUNCTION
], NULL
) < 0)
137 kmap
= map__kmap(host_machine
->vmlinux_maps
[MAP__FUNCTION
]);
140 return kmap
->ref_reloc_sym
;
143 static u64
kernel_get_symbol_address_by_name(const char *name
, bool reloc
)
145 struct ref_reloc_sym
*reloc_sym
;
149 /* ref_reloc_sym is just a label. Need a special fix*/
150 reloc_sym
= kernel_get_ref_reloc_sym();
151 if (reloc_sym
&& strcmp(name
, reloc_sym
->name
) == 0)
152 return (reloc
) ? reloc_sym
->addr
: reloc_sym
->unrelocated_addr
;
154 sym
= __find_kernel_function_by_name(name
, &map
);
156 return map
->unmap_ip(map
, sym
->start
) -
157 ((reloc
) ? 0 : map
->reloc
);
162 static struct map
*kernel_get_module_map(const char *module
)
165 struct map_groups
*grp
= &host_machine
->kmaps
;
167 /* A file path -- this is an offline module */
168 if (module
&& strchr(module
, '/'))
169 return machine__new_module(host_machine
, 0, module
);
174 for (nd
= rb_first(&grp
->maps
[MAP__FUNCTION
]); nd
; nd
= rb_next(nd
)) {
175 struct map
*pos
= rb_entry(nd
, struct map
, rb_node
);
176 if (strncmp(pos
->dso
->short_name
+ 1, module
,
177 pos
->dso
->short_name_len
- 2) == 0) {
184 static struct map
*get_target_map(const char *target
, bool user
)
186 /* Init maps of given executable or kernel */
188 return dso__new_map(target
);
190 return kernel_get_module_map(target
);
193 static void put_target_map(struct map
*map
, bool user
)
196 /* Only the user map needs to be released */
197 dso__delete(map
->dso
);
203 static struct dso
*kernel_get_module_dso(const char *module
)
207 const char *vmlinux_name
;
210 list_for_each_entry(dso
, &host_machine
->kernel_dsos
.head
,
212 if (strncmp(dso
->short_name
+ 1, module
,
213 dso
->short_name_len
- 2) == 0)
216 pr_debug("Failed to find module %s.\n", module
);
220 map
= host_machine
->vmlinux_maps
[MAP__FUNCTION
];
223 vmlinux_name
= symbol_conf
.vmlinux_name
;
225 if (dso__load_vmlinux(dso
, map
, vmlinux_name
, false, NULL
) <= 0)
228 if (dso__load_vmlinux_path(dso
, map
, NULL
) <= 0) {
229 pr_debug("Failed to load kernel map.\n");
237 const char *kernel_get_module_path(const char *module
)
239 struct dso
*dso
= kernel_get_module_dso(module
);
240 return (dso
) ? dso
->long_name
: NULL
;
243 static int convert_exec_to_group(const char *exec
, char **result
)
245 char *ptr1
, *ptr2
, *exec_copy
;
249 exec_copy
= strdup(exec
);
253 ptr1
= basename(exec_copy
);
259 ptr2
= strpbrk(ptr1
, "-._");
262 ret
= e_snprintf(buf
, 64, "%s_%s", PERFPROBE_GROUP
, ptr1
);
266 *result
= strdup(buf
);
267 ret
= *result
? 0 : -ENOMEM
;
274 static void clear_perf_probe_point(struct perf_probe_point
*pp
)
281 static void clear_probe_trace_events(struct probe_trace_event
*tevs
, int ntevs
)
285 for (i
= 0; i
< ntevs
; i
++)
286 clear_probe_trace_event(tevs
+ i
);
289 #ifdef HAVE_DWARF_SUPPORT
291 * Some binaries like glibc have special symbols which are on the symbol
292 * table, but not in the debuginfo. If we can find the address of the
293 * symbol from map, we can translate the address back to the probe point.
295 static int find_alternative_probe_point(struct debuginfo
*dinfo
,
296 struct perf_probe_point
*pp
,
297 struct perf_probe_point
*result
,
298 const char *target
, bool uprobes
)
300 struct map
*map
= NULL
;
305 /* This can work only for function-name based one */
306 if (!pp
->function
|| pp
->file
)
309 map
= get_target_map(target
, uprobes
);
313 /* Find the address of given function */
314 map__for_each_symbol_by_name(map
, pp
->function
, sym
) {
316 address
= sym
->start
;
318 address
= map
->unmap_ip(map
, sym
->start
);
325 pr_debug("Symbol %s address found : %" PRIx64
"\n",
326 pp
->function
, address
);
328 ret
= debuginfo__find_probe_point(dinfo
, (unsigned long)address
,
331 ret
= (!ret
) ? -ENOENT
: ret
;
333 result
->offset
+= pp
->offset
;
334 result
->line
+= pp
->line
;
335 result
->retprobe
= pp
->retprobe
;
340 put_target_map(map
, uprobes
);
345 static int get_alternative_probe_event(struct debuginfo
*dinfo
,
346 struct perf_probe_event
*pev
,
347 struct perf_probe_point
*tmp
,
352 memcpy(tmp
, &pev
->point
, sizeof(*tmp
));
353 memset(&pev
->point
, 0, sizeof(pev
->point
));
354 ret
= find_alternative_probe_point(dinfo
, tmp
, &pev
->point
,
355 target
, pev
->uprobes
);
357 memcpy(&pev
->point
, tmp
, sizeof(*tmp
));
362 static int get_alternative_line_range(struct debuginfo
*dinfo
,
363 struct line_range
*lr
,
364 const char *target
, bool user
)
366 struct perf_probe_point pp
= { .function
= lr
->function
,
369 struct perf_probe_point result
;
372 memset(&result
, 0, sizeof(result
));
374 if (lr
->end
!= INT_MAX
)
375 len
= lr
->end
- lr
->start
;
376 ret
= find_alternative_probe_point(dinfo
, &pp
, &result
,
379 lr
->function
= result
.function
;
380 lr
->file
= result
.file
;
381 lr
->start
= result
.line
;
382 if (lr
->end
!= INT_MAX
)
383 lr
->end
= lr
->start
+ len
;
384 clear_perf_probe_point(&pp
);
389 /* Open new debuginfo of given module */
390 static struct debuginfo
*open_debuginfo(const char *module
, bool silent
)
392 const char *path
= module
;
393 struct debuginfo
*ret
;
395 if (!module
|| !strchr(module
, '/')) {
396 path
= kernel_get_module_path(module
);
399 pr_err("Failed to find path of %s module.\n",
404 ret
= debuginfo__new(path
);
405 if (!ret
&& !silent
) {
406 pr_warning("The %s file has no debug information.\n", path
);
407 if (!module
|| !strtailcmp(path
, ".ko"))
408 pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, ");
410 pr_warning("Rebuild with -g, ");
411 pr_warning("or install an appropriate debuginfo package.\n");
417 static int get_text_start_address(const char *exec
, unsigned long *address
)
422 int fd
, ret
= -ENOENT
;
424 fd
= open(exec
, O_RDONLY
);
428 elf
= elf_begin(fd
, PERF_ELF_C_READ_MMAP
, NULL
);
432 if (gelf_getehdr(elf
, &ehdr
) == NULL
)
435 if (!elf_section_by_name(elf
, &ehdr
, &shdr
, ".text", NULL
))
438 *address
= shdr
.sh_addr
- shdr
.sh_offset
;
446 * Convert trace point to probe point with debuginfo
448 static int find_perf_probe_point_from_dwarf(struct probe_trace_point
*tp
,
449 struct perf_probe_point
*pp
,
452 struct debuginfo
*dinfo
= NULL
;
453 unsigned long stext
= 0;
454 u64 addr
= tp
->address
;
457 /* convert the address to dwarf address */
463 ret
= get_text_start_address(tp
->module
, &stext
);
468 addr
= kernel_get_symbol_address_by_name(tp
->symbol
, false);
474 pr_debug("try to find information at %" PRIx64
" in %s\n", addr
,
475 tp
->module
? : "kernel");
477 dinfo
= open_debuginfo(tp
->module
, verbose
== 0);
479 ret
= debuginfo__find_probe_point(dinfo
,
480 (unsigned long)addr
, pp
);
481 debuginfo__delete(dinfo
);
486 pp
->retprobe
= tp
->retprobe
;
490 pr_debug("Failed to find corresponding probes from debuginfo.\n");
491 return ret
? : -ENOENT
;
494 static int add_exec_to_probe_trace_events(struct probe_trace_event
*tevs
,
495 int ntevs
, const char *exec
)
498 unsigned long stext
= 0;
503 ret
= get_text_start_address(exec
, &stext
);
507 for (i
= 0; i
< ntevs
&& ret
>= 0; i
++) {
508 /* point.address is the addres of point.symbol + point.offset */
509 tevs
[i
].point
.address
-= stext
;
510 tevs
[i
].point
.module
= strdup(exec
);
511 if (!tevs
[i
].point
.module
) {
515 tevs
[i
].uprobes
= true;
521 static int add_module_to_probe_trace_events(struct probe_trace_event
*tevs
,
522 int ntevs
, const char *module
)
530 tmp
= strrchr(module
, '/');
532 /* This is a module path -- get the module name */
533 module
= strdup(tmp
+ 1);
536 tmp
= strchr(module
, '.');
539 tmp
= (char *)module
; /* For free() */
542 for (i
= 0; i
< ntevs
; i
++) {
543 tevs
[i
].point
.module
= strdup(module
);
544 if (!tevs
[i
].point
.module
) {
554 /* Post processing the probe events */
555 static int post_process_probe_trace_events(struct probe_trace_event
*tevs
,
556 int ntevs
, const char *module
,
559 struct ref_reloc_sym
*reloc_sym
;
564 return add_exec_to_probe_trace_events(tevs
, ntevs
, module
);
566 /* Note that currently ref_reloc_sym based probe is not for drivers */
568 return add_module_to_probe_trace_events(tevs
, ntevs
, module
);
570 reloc_sym
= kernel_get_ref_reloc_sym();
572 pr_warning("Relocated base symbol is not found!\n");
576 for (i
= 0; i
< ntevs
; i
++) {
577 if (tevs
[i
].point
.address
&& !tevs
[i
].point
.retprobe
) {
578 tmp
= strdup(reloc_sym
->name
);
581 free(tevs
[i
].point
.symbol
);
582 tevs
[i
].point
.symbol
= tmp
;
583 tevs
[i
].point
.offset
= tevs
[i
].point
.address
-
584 reloc_sym
->unrelocated_addr
;
590 /* Try to find perf_probe_event with debuginfo */
591 static int try_to_find_probe_trace_events(struct perf_probe_event
*pev
,
592 struct probe_trace_event
**tevs
,
593 int max_tevs
, const char *target
)
595 bool need_dwarf
= perf_probe_event_need_dwarf(pev
);
596 struct perf_probe_point tmp
;
597 struct debuginfo
*dinfo
;
600 dinfo
= open_debuginfo(target
, !need_dwarf
);
605 pr_debug("Could not open debuginfo. Try to use symbols.\n");
609 pr_debug("Try to find probe point from debuginfo.\n");
610 /* Searching trace events corresponding to a probe event */
611 ntevs
= debuginfo__find_trace_events(dinfo
, pev
, tevs
, max_tevs
);
613 if (ntevs
== 0) { /* Not found, retry with an alternative */
614 ret
= get_alternative_probe_event(dinfo
, pev
, &tmp
, target
);
616 ntevs
= debuginfo__find_trace_events(dinfo
, pev
,
619 * Write back to the original probe_event for
620 * setting appropriate (user given) event name
622 clear_perf_probe_point(&pev
->point
);
623 memcpy(&pev
->point
, &tmp
, sizeof(tmp
));
627 debuginfo__delete(dinfo
);
629 if (ntevs
> 0) { /* Succeeded to find trace events */
630 pr_debug("Found %d probe_trace_events.\n", ntevs
);
631 ret
= post_process_probe_trace_events(*tevs
, ntevs
,
632 target
, pev
->uprobes
);
634 clear_probe_trace_events(*tevs
, ntevs
);
637 return ret
< 0 ? ret
: ntevs
;
640 if (ntevs
== 0) { /* No error but failed to find probe point. */
641 pr_warning("Probe point '%s' not found.\n",
642 synthesize_perf_probe_point(&pev
->point
));
645 /* Error path : ntevs < 0 */
646 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs
);
647 if (ntevs
== -EBADF
) {
648 pr_warning("Warning: No dwarf info found in the vmlinux - "
649 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
651 pr_debug("Trying to use symbols.\n");
658 #define LINEBUF_SIZE 256
659 #define NR_ADDITIONAL_LINES 2
661 static int __show_one_line(FILE *fp
, int l
, bool skip
, bool show_num
)
663 char buf
[LINEBUF_SIZE
], sbuf
[STRERR_BUFSIZE
];
664 const char *color
= show_num
? "" : PERF_COLOR_BLUE
;
665 const char *prefix
= NULL
;
668 if (fgets(buf
, LINEBUF_SIZE
, fp
) == NULL
)
673 prefix
= show_num
? "%7d " : " ";
674 color_fprintf(stdout
, color
, prefix
, l
);
676 color_fprintf(stdout
, color
, "%s", buf
);
678 } while (strchr(buf
, '\n') == NULL
);
683 pr_warning("File read error: %s\n",
684 strerror_r(errno
, sbuf
, sizeof(sbuf
)));
690 static int _show_one_line(FILE *fp
, int l
, bool skip
, bool show_num
)
692 int rv
= __show_one_line(fp
, l
, skip
, show_num
);
694 pr_warning("Source file is shorter than expected.\n");
700 #define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
701 #define show_one_line(f,l) _show_one_line(f,l,false,false)
702 #define skip_one_line(f,l) _show_one_line(f,l,true,false)
703 #define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
706 * Show line-range always requires debuginfo to find source file and
709 static int __show_line_range(struct line_range
*lr
, const char *module
,
714 struct debuginfo
*dinfo
;
718 char sbuf
[STRERR_BUFSIZE
];
720 /* Search a line range */
721 dinfo
= open_debuginfo(module
, false);
725 ret
= debuginfo__find_line_range(dinfo
, lr
);
726 if (!ret
) { /* Not found, retry with an alternative */
727 ret
= get_alternative_line_range(dinfo
, lr
, module
, user
);
729 ret
= debuginfo__find_line_range(dinfo
, lr
);
731 debuginfo__delete(dinfo
);
732 if (ret
== 0 || ret
== -ENOENT
) {
733 pr_warning("Specified source line is not found.\n");
735 } else if (ret
< 0) {
736 pr_warning("Debuginfo analysis failed.\n");
740 /* Convert source file path */
742 ret
= get_real_path(tmp
, lr
->comp_dir
, &lr
->path
);
744 /* Free old path when new path is assigned */
749 pr_warning("Failed to find source file path.\n");
756 fprintf(stdout
, "<%s@%s:%d>\n", lr
->function
, lr
->path
,
757 lr
->start
- lr
->offset
);
759 fprintf(stdout
, "<%s:%d>\n", lr
->path
, lr
->start
);
761 fp
= fopen(lr
->path
, "r");
763 pr_warning("Failed to open %s: %s\n", lr
->path
,
764 strerror_r(errno
, sbuf
, sizeof(sbuf
)));
767 /* Skip to starting line number */
768 while (l
< lr
->start
) {
769 ret
= skip_one_line(fp
, l
++);
774 intlist__for_each(ln
, lr
->line_list
) {
775 for (; ln
->i
> l
; l
++) {
776 ret
= show_one_line(fp
, l
- lr
->offset
);
780 ret
= show_one_line_with_num(fp
, l
++ - lr
->offset
);
785 if (lr
->end
== INT_MAX
)
786 lr
->end
= l
+ NR_ADDITIONAL_LINES
;
787 while (l
<= lr
->end
) {
788 ret
= show_one_line_or_eof(fp
, l
++ - lr
->offset
);
797 int show_line_range(struct line_range
*lr
, const char *module
, bool user
)
801 ret
= init_symbol_maps(user
);
804 ret
= __show_line_range(lr
, module
, user
);
810 static int show_available_vars_at(struct debuginfo
*dinfo
,
811 struct perf_probe_event
*pev
,
812 int max_vls
, struct strfilter
*_filter
,
813 bool externs
, const char *target
)
817 struct str_node
*node
;
818 struct variable_list
*vls
= NULL
, *vl
;
819 struct perf_probe_point tmp
;
822 buf
= synthesize_perf_probe_point(&pev
->point
);
825 pr_debug("Searching variables at %s\n", buf
);
827 ret
= debuginfo__find_available_vars_at(dinfo
, pev
, &vls
,
829 if (!ret
) { /* Not found, retry with an alternative */
830 ret
= get_alternative_probe_event(dinfo
, pev
, &tmp
, target
);
832 ret
= debuginfo__find_available_vars_at(dinfo
, pev
,
833 &vls
, max_vls
, externs
);
834 /* Release the old probe_point */
835 clear_perf_probe_point(&tmp
);
839 if (ret
== 0 || ret
== -ENOENT
) {
840 pr_err("Failed to find the address of %s\n", buf
);
843 pr_warning("Debuginfo analysis failed.\n");
847 /* Some variables are found */
848 fprintf(stdout
, "Available variables at %s\n", buf
);
849 for (i
= 0; i
< ret
; i
++) {
852 * A probe point might be converted to
853 * several trace points.
855 fprintf(stdout
, "\t@<%s+%lu>\n", vl
->point
.symbol
,
857 zfree(&vl
->point
.symbol
);
860 strlist__for_each(node
, vl
->vars
) {
861 var
= strchr(node
->s
, '\t') + 1;
862 if (strfilter__compare(_filter
, var
)) {
863 fprintf(stdout
, "\t\t%s\n", node
->s
);
867 strlist__delete(vl
->vars
);
870 fprintf(stdout
, "\t\t(No matched variables)\n");
878 /* Show available variables on given probe point */
879 int show_available_vars(struct perf_probe_event
*pevs
, int npevs
,
880 int max_vls
, const char *module
,
881 struct strfilter
*_filter
, bool externs
)
884 struct debuginfo
*dinfo
;
886 ret
= init_symbol_maps(pevs
->uprobes
);
890 dinfo
= open_debuginfo(module
, false);
898 for (i
= 0; i
< npevs
&& ret
>= 0; i
++)
899 ret
= show_available_vars_at(dinfo
, &pevs
[i
], max_vls
, _filter
,
902 debuginfo__delete(dinfo
);
908 #else /* !HAVE_DWARF_SUPPORT */
911 find_perf_probe_point_from_dwarf(struct probe_trace_point
*tp __maybe_unused
,
912 struct perf_probe_point
*pp __maybe_unused
,
913 bool is_kprobe __maybe_unused
)
918 static int try_to_find_probe_trace_events(struct perf_probe_event
*pev
,
919 struct probe_trace_event
**tevs __maybe_unused
,
920 int max_tevs __maybe_unused
,
921 const char *target __maybe_unused
)
923 if (perf_probe_event_need_dwarf(pev
)) {
924 pr_warning("Debuginfo-analysis is not supported.\n");
931 int show_line_range(struct line_range
*lr __maybe_unused
,
932 const char *module __maybe_unused
,
933 bool user __maybe_unused
)
935 pr_warning("Debuginfo-analysis is not supported.\n");
939 int show_available_vars(struct perf_probe_event
*pevs __maybe_unused
,
940 int npevs __maybe_unused
, int max_vls __maybe_unused
,
941 const char *module __maybe_unused
,
942 struct strfilter
*filter __maybe_unused
,
943 bool externs __maybe_unused
)
945 pr_warning("Debuginfo-analysis is not supported.\n");
950 void line_range__clear(struct line_range
*lr
)
956 intlist__delete(lr
->line_list
);
957 memset(lr
, 0, sizeof(*lr
));
960 int line_range__init(struct line_range
*lr
)
962 memset(lr
, 0, sizeof(*lr
));
963 lr
->line_list
= intlist__new(NULL
);
970 static int parse_line_num(char **ptr
, int *val
, const char *what
)
972 const char *start
= *ptr
;
975 *val
= strtol(*ptr
, ptr
, 0);
976 if (errno
|| *ptr
== start
) {
977 semantic_error("'%s' is not a valid number.\n", what
);
984 * Stuff 'lr' according to the line range described by 'arg'.
985 * The line range syntax is described by:
987 * SRC[:SLN[+NUM|-ELN]]
988 * FNC[@SRC][:SLN[+NUM|-ELN]]
990 int parse_line_range_desc(const char *arg
, struct line_range
*lr
)
992 char *range
, *file
, *name
= strdup(arg
);
1001 range
= strchr(name
, ':');
1005 err
= parse_line_num(&range
, &lr
->start
, "start line");
1009 if (*range
== '+' || *range
== '-') {
1010 const char c
= *range
++;
1012 err
= parse_line_num(&range
, &lr
->end
, "end line");
1017 lr
->end
+= lr
->start
;
1019 * Adjust the number of lines here.
1020 * If the number of lines == 1, the
1021 * the end of line should be equal to
1022 * the start of line.
1028 pr_debug("Line range is %d to %d\n", lr
->start
, lr
->end
);
1031 if (lr
->start
> lr
->end
) {
1032 semantic_error("Start line must be smaller"
1033 " than end line.\n");
1036 if (*range
!= '\0') {
1037 semantic_error("Tailing with invalid str '%s'.\n", range
);
1042 file
= strchr(name
, '@');
1045 lr
->file
= strdup(++file
);
1046 if (lr
->file
== NULL
) {
1050 lr
->function
= name
;
1051 } else if (strchr(name
, '.'))
1054 lr
->function
= name
;
1062 /* Check the name is good for event/group */
1063 static bool check_event_name(const char *name
)
1065 if (!isalpha(*name
) && *name
!= '_')
1067 while (*++name
!= '\0') {
1068 if (!isalpha(*name
) && !isdigit(*name
) && *name
!= '_')
1074 /* Parse probepoint definition. */
1075 static int parse_perf_probe_point(char *arg
, struct perf_probe_event
*pev
)
1077 struct perf_probe_point
*pp
= &pev
->point
;
1082 * perf probe [EVENT=]SRC[:LN|;PTN]
1083 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
1085 * TODO:Group name support
1090 ptr
= strpbrk(arg
, ";=@+%");
1091 if (ptr
&& *ptr
== '=') { /* Event name */
1094 if (strchr(arg
, ':')) {
1095 semantic_error("Group name is not supported yet.\n");
1098 if (!check_event_name(arg
)) {
1099 semantic_error("%s is bad for event name -it must "
1100 "follow C symbol-naming rule.\n", arg
);
1103 pev
->event
= strdup(arg
);
1104 if (pev
->event
== NULL
)
1110 ptr
= strpbrk(arg
, ";:+@%");
1120 /* Check arg is function or file and copy it */
1121 if (strchr(tmp
, '.')) /* File */
1126 /* Parse other options */
1130 if (c
== ';') { /* Lazy pattern must be the last part */
1131 pp
->lazy_line
= strdup(arg
);
1132 if (pp
->lazy_line
== NULL
)
1136 ptr
= strpbrk(arg
, ";:+@%");
1142 case ':': /* Line number */
1143 pp
->line
= strtoul(arg
, &tmp
, 0);
1145 semantic_error("There is non-digit char"
1146 " in line number.\n");
1150 case '+': /* Byte offset from a symbol */
1151 pp
->offset
= strtoul(arg
, &tmp
, 0);
1153 semantic_error("There is non-digit character"
1158 case '@': /* File name */
1160 semantic_error("SRC@SRC is not allowed.\n");
1163 pp
->file
= strdup(arg
);
1164 if (pp
->file
== NULL
)
1167 case '%': /* Probe places */
1168 if (strcmp(arg
, "return") == 0) {
1170 } else { /* Others not supported yet */
1171 semantic_error("%%%s is not supported.\n", arg
);
1175 default: /* Buggy case */
1176 pr_err("This program has a bug at %s:%d.\n",
1177 __FILE__
, __LINE__
);
1183 /* Exclusion check */
1184 if (pp
->lazy_line
&& pp
->line
) {
1185 semantic_error("Lazy pattern can't be used with"
1190 if (pp
->lazy_line
&& pp
->offset
) {
1191 semantic_error("Lazy pattern can't be used with offset.\n");
1195 if (pp
->line
&& pp
->offset
) {
1196 semantic_error("Offset can't be used with line number.\n");
1200 if (!pp
->line
&& !pp
->lazy_line
&& pp
->file
&& !pp
->function
) {
1201 semantic_error("File always requires line number or "
1206 if (pp
->offset
&& !pp
->function
) {
1207 semantic_error("Offset requires an entry function.\n");
1211 if (pp
->retprobe
&& !pp
->function
) {
1212 semantic_error("Return probe requires an entry function.\n");
1216 if ((pp
->offset
|| pp
->line
|| pp
->lazy_line
) && pp
->retprobe
) {
1217 semantic_error("Offset/Line/Lazy pattern can't be used with "
1222 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
1223 pp
->function
, pp
->file
, pp
->line
, pp
->offset
, pp
->retprobe
,
1228 /* Parse perf-probe event argument */
1229 static int parse_perf_probe_arg(char *str
, struct perf_probe_arg
*arg
)
1231 char *tmp
, *goodname
;
1232 struct perf_probe_arg_field
**fieldp
;
1234 pr_debug("parsing arg: %s into ", str
);
1236 tmp
= strchr(str
, '=');
1238 arg
->name
= strndup(str
, tmp
- str
);
1239 if (arg
->name
== NULL
)
1241 pr_debug("name:%s ", arg
->name
);
1245 tmp
= strchr(str
, ':');
1246 if (tmp
) { /* Type setting */
1248 arg
->type
= strdup(tmp
+ 1);
1249 if (arg
->type
== NULL
)
1251 pr_debug("type:%s ", arg
->type
);
1254 tmp
= strpbrk(str
, "-.[");
1255 if (!is_c_varname(str
) || !tmp
) {
1256 /* A variable, register, symbol or special value */
1257 arg
->var
= strdup(str
);
1258 if (arg
->var
== NULL
)
1260 pr_debug("%s\n", arg
->var
);
1264 /* Structure fields or array element */
1265 arg
->var
= strndup(str
, tmp
- str
);
1266 if (arg
->var
== NULL
)
1268 goodname
= arg
->var
;
1269 pr_debug("%s, ", arg
->var
);
1270 fieldp
= &arg
->field
;
1273 *fieldp
= zalloc(sizeof(struct perf_probe_arg_field
));
1274 if (*fieldp
== NULL
)
1276 if (*tmp
== '[') { /* Array */
1278 (*fieldp
)->index
= strtol(str
+ 1, &tmp
, 0);
1279 (*fieldp
)->ref
= true;
1280 if (*tmp
!= ']' || tmp
== str
+ 1) {
1281 semantic_error("Array index must be a"
1288 } else { /* Structure */
1291 (*fieldp
)->ref
= false;
1292 } else if (tmp
[1] == '>') {
1294 (*fieldp
)->ref
= true;
1296 semantic_error("Argument parse error: %s\n",
1300 tmp
= strpbrk(str
, "-.[");
1303 (*fieldp
)->name
= strndup(str
, tmp
- str
);
1304 if ((*fieldp
)->name
== NULL
)
1307 goodname
= (*fieldp
)->name
;
1308 pr_debug("%s(%d), ", (*fieldp
)->name
, (*fieldp
)->ref
);
1309 fieldp
= &(*fieldp
)->next
;
1312 (*fieldp
)->name
= strdup(str
);
1313 if ((*fieldp
)->name
== NULL
)
1316 goodname
= (*fieldp
)->name
;
1317 pr_debug("%s(%d)\n", (*fieldp
)->name
, (*fieldp
)->ref
);
1319 /* If no name is specified, set the last field name (not array index)*/
1321 arg
->name
= strdup(goodname
);
1322 if (arg
->name
== NULL
)
1328 /* Parse perf-probe event command */
1329 int parse_perf_probe_command(const char *cmd
, struct perf_probe_event
*pev
)
1332 int argc
, i
, ret
= 0;
1334 argv
= argv_split(cmd
, &argc
);
1336 pr_debug("Failed to split arguments.\n");
1339 if (argc
- 1 > MAX_PROBE_ARGS
) {
1340 semantic_error("Too many probe arguments (%d).\n", argc
- 1);
1344 /* Parse probe point */
1345 ret
= parse_perf_probe_point(argv
[0], pev
);
1349 /* Copy arguments and ensure return probe has no C argument */
1350 pev
->nargs
= argc
- 1;
1351 pev
->args
= zalloc(sizeof(struct perf_probe_arg
) * pev
->nargs
);
1352 if (pev
->args
== NULL
) {
1356 for (i
= 0; i
< pev
->nargs
&& ret
>= 0; i
++) {
1357 ret
= parse_perf_probe_arg(argv
[i
+ 1], &pev
->args
[i
]);
1359 is_c_varname(pev
->args
[i
].var
) && pev
->point
.retprobe
) {
1360 semantic_error("You can't specify local variable for"
1371 /* Return true if this perf_probe_event requires debuginfo */
1372 bool perf_probe_event_need_dwarf(struct perf_probe_event
*pev
)
1376 if (pev
->point
.file
|| pev
->point
.line
|| pev
->point
.lazy_line
)
1379 for (i
= 0; i
< pev
->nargs
; i
++)
1380 if (is_c_varname(pev
->args
[i
].var
))
1386 /* Parse probe_events event into struct probe_point */
1387 static int parse_probe_trace_command(const char *cmd
,
1388 struct probe_trace_event
*tev
)
1390 struct probe_trace_point
*tp
= &tev
->point
;
1393 char *argv0_str
= NULL
, *fmt
, *fmt1_str
, *fmt2_str
, *fmt3_str
;
1397 pr_debug("Parsing probe_events: %s\n", cmd
);
1398 argv
= argv_split(cmd
, &argc
);
1400 pr_debug("Failed to split arguments.\n");
1404 semantic_error("Too few probe arguments.\n");
1409 /* Scan event and group name. */
1410 argv0_str
= strdup(argv
[0]);
1411 if (argv0_str
== NULL
) {
1415 fmt1_str
= strtok_r(argv0_str
, ":", &fmt
);
1416 fmt2_str
= strtok_r(NULL
, "/", &fmt
);
1417 fmt3_str
= strtok_r(NULL
, " \t", &fmt
);
1418 if (fmt1_str
== NULL
|| strlen(fmt1_str
) != 1 || fmt2_str
== NULL
1419 || fmt3_str
== NULL
) {
1420 semantic_error("Failed to parse event name: %s\n", argv
[0]);
1425 tev
->group
= strdup(fmt2_str
);
1426 tev
->event
= strdup(fmt3_str
);
1427 if (tev
->group
== NULL
|| tev
->event
== NULL
) {
1431 pr_debug("Group:%s Event:%s probe:%c\n", tev
->group
, tev
->event
, pr
);
1433 tp
->retprobe
= (pr
== 'r');
1435 /* Scan module name(if there), function name and offset */
1436 p
= strchr(argv
[1], ':');
1438 tp
->module
= strndup(argv
[1], p
- argv
[1]);
1442 fmt1_str
= strtok_r(p
, "+", &fmt
);
1443 if (fmt1_str
[0] == '0') /* only the address started with 0x */
1444 tp
->address
= strtoul(fmt1_str
, NULL
, 0);
1446 /* Only the symbol-based probe has offset */
1447 tp
->symbol
= strdup(fmt1_str
);
1448 if (tp
->symbol
== NULL
) {
1452 fmt2_str
= strtok_r(NULL
, "", &fmt
);
1453 if (fmt2_str
== NULL
)
1456 tp
->offset
= strtoul(fmt2_str
, NULL
, 10);
1459 tev
->nargs
= argc
- 2;
1460 tev
->args
= zalloc(sizeof(struct probe_trace_arg
) * tev
->nargs
);
1461 if (tev
->args
== NULL
) {
1465 for (i
= 0; i
< tev
->nargs
; i
++) {
1466 p
= strchr(argv
[i
+ 2], '=');
1467 if (p
) /* We don't need which register is assigned. */
1471 tev
->args
[i
].name
= strdup(argv
[i
+ 2]);
1472 /* TODO: parse regs and offset */
1473 tev
->args
[i
].value
= strdup(p
);
1474 if (tev
->args
[i
].name
== NULL
|| tev
->args
[i
].value
== NULL
) {
1486 /* Compose only probe arg */
1487 int synthesize_perf_probe_arg(struct perf_probe_arg
*pa
, char *buf
, size_t len
)
1489 struct perf_probe_arg_field
*field
= pa
->field
;
1493 if (pa
->name
&& pa
->var
)
1494 ret
= e_snprintf(tmp
, len
, "%s=%s", pa
->name
, pa
->var
);
1496 ret
= e_snprintf(tmp
, len
, "%s", pa
->name
? pa
->name
: pa
->var
);
1503 if (field
->name
[0] == '[')
1504 ret
= e_snprintf(tmp
, len
, "%s", field
->name
);
1506 ret
= e_snprintf(tmp
, len
, "%s%s",
1507 field
->ref
? "->" : ".", field
->name
);
1512 field
= field
->next
;
1516 ret
= e_snprintf(tmp
, len
, ":%s", pa
->type
);
1525 pr_debug("Failed to synthesize perf probe argument: %d\n", ret
);
1529 /* Compose only probe point (not argument) */
1530 static char *synthesize_perf_probe_point(struct perf_probe_point
*pp
)
1533 char offs
[32] = "", line
[32] = "", file
[32] = "";
1536 buf
= zalloc(MAX_CMDLEN
);
1542 ret
= e_snprintf(offs
, 32, "+%lu", pp
->offset
);
1547 ret
= e_snprintf(line
, 32, ":%d", pp
->line
);
1555 tmp
= strchr(pp
->file
+ len
- 30, '/');
1556 tmp
= tmp
? tmp
+ 1 : pp
->file
+ len
- 30;
1558 ret
= e_snprintf(file
, 32, "@%s", tmp
);
1564 ret
= e_snprintf(buf
, MAX_CMDLEN
, "%s%s%s%s%s", pp
->function
,
1565 offs
, pp
->retprobe
? "%return" : "", line
,
1568 ret
= e_snprintf(buf
, MAX_CMDLEN
, "%s%s", file
, line
);
1574 pr_debug("Failed to synthesize perf probe point: %d\n", ret
);
1580 char *synthesize_perf_probe_command(struct perf_probe_event
*pev
)
1585 buf
= synthesize_perf_probe_point(&pev
->point
);
1590 for (i
= 0; i
< pev
->nargs
; i
++) {
1591 ret
= e_snprintf(&buf
[len
], MAX_CMDLEN
- len
, " %s",
1604 static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref
*ref
,
1605 char **buf
, size_t *buflen
,
1610 depth
= __synthesize_probe_trace_arg_ref(ref
->next
, buf
,
1616 ret
= e_snprintf(*buf
, *buflen
, "%+ld(", ref
->offset
);
1628 static int synthesize_probe_trace_arg(struct probe_trace_arg
*arg
,
1629 char *buf
, size_t buflen
)
1631 struct probe_trace_arg_ref
*ref
= arg
->ref
;
1635 /* Argument name or separator */
1637 ret
= e_snprintf(buf
, buflen
, " %s=", arg
->name
);
1639 ret
= e_snprintf(buf
, buflen
, " ");
1645 /* Special case: @XXX */
1646 if (arg
->value
[0] == '@' && arg
->ref
)
1649 /* Dereferencing arguments */
1651 depth
= __synthesize_probe_trace_arg_ref(ref
, &buf
,
1657 /* Print argument value */
1658 if (arg
->value
[0] == '@' && arg
->ref
)
1659 ret
= e_snprintf(buf
, buflen
, "%s%+ld", arg
->value
,
1662 ret
= e_snprintf(buf
, buflen
, "%s", arg
->value
);
1670 ret
= e_snprintf(buf
, buflen
, ")");
1676 /* Print argument type */
1678 ret
= e_snprintf(buf
, buflen
, ":%s", arg
->type
);
1687 char *synthesize_probe_trace_command(struct probe_trace_event
*tev
)
1689 struct probe_trace_point
*tp
= &tev
->point
;
1693 buf
= zalloc(MAX_CMDLEN
);
1697 len
= e_snprintf(buf
, MAX_CMDLEN
, "%c:%s/%s ", tp
->retprobe
? 'r' : 'p',
1698 tev
->group
, tev
->event
);
1702 /* Uprobes must have tp->address and tp->module */
1703 if (tev
->uprobes
&& (!tp
->address
|| !tp
->module
))
1706 /* Use the tp->address for uprobes */
1708 ret
= e_snprintf(buf
+ len
, MAX_CMDLEN
- len
, "%s:0x%lx",
1709 tp
->module
, tp
->address
);
1711 ret
= e_snprintf(buf
+ len
, MAX_CMDLEN
- len
, "%s%s%s+%lu",
1712 tp
->module
?: "", tp
->module
? ":" : "",
1713 tp
->symbol
, tp
->offset
);
1719 for (i
= 0; i
< tev
->nargs
; i
++) {
1720 ret
= synthesize_probe_trace_arg(&tev
->args
[i
], buf
+ len
,
1733 static int find_perf_probe_point_from_map(struct probe_trace_point
*tp
,
1734 struct perf_probe_point
*pp
,
1737 struct symbol
*sym
= NULL
;
1743 map
= dso__new_map(tp
->module
);
1747 sym
= map__find_symbol(map
, addr
, NULL
);
1749 addr
= kernel_get_symbol_address_by_name(tp
->symbol
, true);
1752 sym
= __find_kernel_function(addr
, &map
);
1758 pp
->retprobe
= tp
->retprobe
;
1759 pp
->offset
= addr
- map
->unmap_ip(map
, sym
->start
);
1760 pp
->function
= strdup(sym
->name
);
1761 ret
= pp
->function
? 0 : -ENOMEM
;
1764 if (map
&& !is_kprobe
) {
1765 dso__delete(map
->dso
);
1772 static int convert_to_perf_probe_point(struct probe_trace_point
*tp
,
1773 struct perf_probe_point
*pp
,
1779 ret
= find_perf_probe_point_from_dwarf(tp
, pp
, is_kprobe
);
1782 ret
= find_perf_probe_point_from_map(tp
, pp
, is_kprobe
);
1786 pr_debug("Failed to find probe point from both of dwarf and map.\n");
1789 pp
->function
= strdup(tp
->symbol
);
1790 pp
->offset
= tp
->offset
;
1791 } else if (!tp
->module
&& !is_kprobe
) {
1792 ret
= e_snprintf(buf
, 128, "0x%" PRIx64
, (u64
)tp
->address
);
1795 pp
->function
= strdup(buf
);
1798 if (pp
->function
== NULL
)
1801 pp
->retprobe
= tp
->retprobe
;
1806 static int convert_to_perf_probe_event(struct probe_trace_event
*tev
,
1807 struct perf_probe_event
*pev
, bool is_kprobe
)
1812 /* Convert event/group name */
1813 pev
->event
= strdup(tev
->event
);
1814 pev
->group
= strdup(tev
->group
);
1815 if (pev
->event
== NULL
|| pev
->group
== NULL
)
1818 /* Convert trace_point to probe_point */
1819 ret
= convert_to_perf_probe_point(&tev
->point
, &pev
->point
, is_kprobe
);
1823 /* Convert trace_arg to probe_arg */
1824 pev
->nargs
= tev
->nargs
;
1825 pev
->args
= zalloc(sizeof(struct perf_probe_arg
) * pev
->nargs
);
1826 if (pev
->args
== NULL
)
1828 for (i
= 0; i
< tev
->nargs
&& ret
>= 0; i
++) {
1829 if (tev
->args
[i
].name
)
1830 pev
->args
[i
].name
= strdup(tev
->args
[i
].name
);
1832 ret
= synthesize_probe_trace_arg(&tev
->args
[i
],
1834 pev
->args
[i
].name
= strdup(buf
);
1836 if (pev
->args
[i
].name
== NULL
&& ret
>= 0)
1841 clear_perf_probe_event(pev
);
1846 void clear_perf_probe_event(struct perf_probe_event
*pev
)
1848 struct perf_probe_arg_field
*field
, *next
;
1854 clear_perf_probe_point(&pev
->point
);
1856 for (i
= 0; i
< pev
->nargs
; i
++) {
1857 free(pev
->args
[i
].name
);
1858 free(pev
->args
[i
].var
);
1859 free(pev
->args
[i
].type
);
1860 field
= pev
->args
[i
].field
;
1863 zfree(&field
->name
);
1869 memset(pev
, 0, sizeof(*pev
));
1872 static void clear_probe_trace_event(struct probe_trace_event
*tev
)
1874 struct probe_trace_arg_ref
*ref
, *next
;
1879 free(tev
->point
.symbol
);
1880 free(tev
->point
.module
);
1881 for (i
= 0; i
< tev
->nargs
; i
++) {
1882 free(tev
->args
[i
].name
);
1883 free(tev
->args
[i
].value
);
1884 free(tev
->args
[i
].type
);
1885 ref
= tev
->args
[i
].ref
;
1893 memset(tev
, 0, sizeof(*tev
));
1896 static void print_open_warning(int err
, bool is_kprobe
)
1898 char sbuf
[STRERR_BUFSIZE
];
1900 if (err
== -ENOENT
) {
1904 config
= "CONFIG_UPROBE_EVENTS";
1906 config
= "CONFIG_KPROBE_EVENTS";
1908 pr_warning("%cprobe_events file does not exist"
1909 " - please rebuild kernel with %s.\n",
1910 is_kprobe
? 'k' : 'u', config
);
1911 } else if (err
== -ENOTSUP
)
1912 pr_warning("Tracefs or debugfs is not mounted.\n");
1914 pr_warning("Failed to open %cprobe_events: %s\n",
1915 is_kprobe
? 'k' : 'u',
1916 strerror_r(-err
, sbuf
, sizeof(sbuf
)));
1919 static void print_both_open_warning(int kerr
, int uerr
)
1921 /* Both kprobes and uprobes are disabled, warn it. */
1922 if (kerr
== -ENOTSUP
&& uerr
== -ENOTSUP
)
1923 pr_warning("Tracefs or debugfs is not mounted.\n");
1924 else if (kerr
== -ENOENT
&& uerr
== -ENOENT
)
1925 pr_warning("Please rebuild kernel with CONFIG_KPROBE_EVENTS "
1926 "or/and CONFIG_UPROBE_EVENTS.\n");
1928 char sbuf
[STRERR_BUFSIZE
];
1929 pr_warning("Failed to open kprobe events: %s.\n",
1930 strerror_r(-kerr
, sbuf
, sizeof(sbuf
)));
1931 pr_warning("Failed to open uprobe events: %s.\n",
1932 strerror_r(-uerr
, sbuf
, sizeof(sbuf
)));
1936 static int open_probe_events(const char *trace_file
, bool readwrite
)
1939 const char *__debugfs
;
1940 const char *tracing_dir
= "";
1943 __debugfs
= tracefs_find_mountpoint();
1944 if (__debugfs
== NULL
) {
1945 tracing_dir
= "tracing/";
1947 __debugfs
= debugfs_find_mountpoint();
1948 if (__debugfs
== NULL
)
1952 ret
= e_snprintf(buf
, PATH_MAX
, "%s/%s%s",
1953 __debugfs
, tracing_dir
, trace_file
);
1955 pr_debug("Opening %s write=%d\n", buf
, readwrite
);
1956 if (readwrite
&& !probe_event_dry_run
)
1957 ret
= open(buf
, O_RDWR
, O_APPEND
);
1959 ret
= open(buf
, O_RDONLY
, 0);
1967 static int open_kprobe_events(bool readwrite
)
1969 return open_probe_events("kprobe_events", readwrite
);
1972 static int open_uprobe_events(bool readwrite
)
1974 return open_probe_events("uprobe_events", readwrite
);
1977 /* Get raw string list of current kprobe_events or uprobe_events */
1978 static struct strlist
*get_probe_trace_command_rawlist(int fd
)
1982 char buf
[MAX_CMDLEN
];
1986 sl
= strlist__new(true, NULL
);
1988 fp
= fdopen(dup(fd
), "r");
1990 p
= fgets(buf
, MAX_CMDLEN
, fp
);
1994 idx
= strlen(p
) - 1;
1997 ret
= strlist__add(sl
, buf
);
1999 pr_debug("strlist__add failed (%d)\n", ret
);
2000 strlist__delete(sl
);
2009 struct kprobe_blacklist_node
{
2010 struct list_head list
;
2011 unsigned long start
;
2016 static void kprobe_blacklist__delete(struct list_head
*blacklist
)
2018 struct kprobe_blacklist_node
*node
;
2020 while (!list_empty(blacklist
)) {
2021 node
= list_first_entry(blacklist
,
2022 struct kprobe_blacklist_node
, list
);
2023 list_del(&node
->list
);
2029 static int kprobe_blacklist__load(struct list_head
*blacklist
)
2031 struct kprobe_blacklist_node
*node
;
2032 const char *__debugfs
= debugfs_find_mountpoint();
2033 char buf
[PATH_MAX
], *p
;
2037 if (__debugfs
== NULL
)
2040 ret
= e_snprintf(buf
, PATH_MAX
, "%s/kprobes/blacklist", __debugfs
);
2044 fp
= fopen(buf
, "r");
2049 while (fgets(buf
, PATH_MAX
, fp
)) {
2050 node
= zalloc(sizeof(*node
));
2055 INIT_LIST_HEAD(&node
->list
);
2056 list_add_tail(&node
->list
, blacklist
);
2057 if (sscanf(buf
, "0x%lx-0x%lx", &node
->start
, &node
->end
) != 2) {
2061 p
= strchr(buf
, '\t');
2064 if (p
[strlen(p
) - 1] == '\n')
2065 p
[strlen(p
) - 1] = '\0';
2067 p
= (char *)"unknown";
2068 node
->symbol
= strdup(p
);
2069 if (!node
->symbol
) {
2073 pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
2074 node
->start
, node
->end
, node
->symbol
);
2078 kprobe_blacklist__delete(blacklist
);
2084 static struct kprobe_blacklist_node
*
2085 kprobe_blacklist__find_by_address(struct list_head
*blacklist
,
2086 unsigned long address
)
2088 struct kprobe_blacklist_node
*node
;
2090 list_for_each_entry(node
, blacklist
, list
) {
2091 if (node
->start
<= address
&& address
<= node
->end
)
2099 static int show_perf_probe_event(struct perf_probe_event
*pev
,
2106 /* Synthesize only event probe point */
2107 place
= synthesize_perf_probe_point(&pev
->point
);
2111 ret
= e_snprintf(buf
, 128, "%s:%s", pev
->group
, pev
->event
);
2115 pr_info(" %-20s (on %s", buf
, place
);
2117 pr_info(" in %s", module
);
2119 if (pev
->nargs
> 0) {
2121 for (i
= 0; i
< pev
->nargs
; i
++) {
2122 ret
= synthesize_perf_probe_arg(&pev
->args
[i
],
2126 pr_info(" %s", buf
);
2134 static int __show_perf_probe_events(int fd
, bool is_kprobe
)
2137 struct probe_trace_event tev
;
2138 struct perf_probe_event pev
;
2139 struct strlist
*rawlist
;
2140 struct str_node
*ent
;
2142 memset(&tev
, 0, sizeof(tev
));
2143 memset(&pev
, 0, sizeof(pev
));
2145 rawlist
= get_probe_trace_command_rawlist(fd
);
2149 strlist__for_each(ent
, rawlist
) {
2150 ret
= parse_probe_trace_command(ent
->s
, &tev
);
2152 ret
= convert_to_perf_probe_event(&tev
, &pev
,
2155 ret
= show_perf_probe_event(&pev
,
2158 clear_perf_probe_event(&pev
);
2159 clear_probe_trace_event(&tev
);
2163 strlist__delete(rawlist
);
2168 /* List up current perf-probe events */
2169 int show_perf_probe_events(void)
2171 int kp_fd
, up_fd
, ret
;
2175 ret
= init_symbol_maps(false);
2179 kp_fd
= open_kprobe_events(false);
2181 ret
= __show_perf_probe_events(kp_fd
, true);
2187 up_fd
= open_uprobe_events(false);
2188 if (kp_fd
< 0 && up_fd
< 0) {
2189 print_both_open_warning(kp_fd
, up_fd
);
2195 ret
= __show_perf_probe_events(up_fd
, false);
2203 /* Get current perf-probe event names */
2204 static struct strlist
*get_probe_trace_event_names(int fd
, bool include_group
)
2207 struct strlist
*sl
, *rawlist
;
2208 struct str_node
*ent
;
2209 struct probe_trace_event tev
;
2212 memset(&tev
, 0, sizeof(tev
));
2213 rawlist
= get_probe_trace_command_rawlist(fd
);
2216 sl
= strlist__new(true, NULL
);
2217 strlist__for_each(ent
, rawlist
) {
2218 ret
= parse_probe_trace_command(ent
->s
, &tev
);
2221 if (include_group
) {
2222 ret
= e_snprintf(buf
, 128, "%s:%s", tev
.group
,
2225 ret
= strlist__add(sl
, buf
);
2227 ret
= strlist__add(sl
, tev
.event
);
2228 clear_probe_trace_event(&tev
);
2232 strlist__delete(rawlist
);
2235 strlist__delete(sl
);
2241 static int write_probe_trace_event(int fd
, struct probe_trace_event
*tev
)
2244 char *buf
= synthesize_probe_trace_command(tev
);
2245 char sbuf
[STRERR_BUFSIZE
];
2248 pr_debug("Failed to synthesize probe trace event.\n");
2252 pr_debug("Writing event: %s\n", buf
);
2253 if (!probe_event_dry_run
) {
2254 ret
= write(fd
, buf
, strlen(buf
));
2257 pr_warning("Failed to write event: %s\n",
2258 strerror_r(errno
, sbuf
, sizeof(sbuf
)));
2265 static int get_new_event_name(char *buf
, size_t len
, const char *base
,
2266 struct strlist
*namelist
, bool allow_suffix
)
2271 ret
= e_snprintf(buf
, len
, "%s", base
);
2273 pr_debug("snprintf() failed: %d\n", ret
);
2276 if (!strlist__has_entry(namelist
, buf
))
2279 if (!allow_suffix
) {
2280 pr_warning("Error: event \"%s\" already exists. "
2281 "(Use -f to force duplicates.)\n", base
);
2285 /* Try to add suffix */
2286 for (i
= 1; i
< MAX_EVENT_INDEX
; i
++) {
2287 ret
= e_snprintf(buf
, len
, "%s_%d", base
, i
);
2289 pr_debug("snprintf() failed: %d\n", ret
);
2292 if (!strlist__has_entry(namelist
, buf
))
2295 if (i
== MAX_EVENT_INDEX
) {
2296 pr_warning("Too many events are on the same function.\n");
2303 /* Warn if the current kernel's uprobe implementation is old */
2304 static void warn_uprobe_event_compat(struct probe_trace_event
*tev
)
2307 char *buf
= synthesize_probe_trace_command(tev
);
2309 /* Old uprobe event doesn't support memory dereference */
2310 if (!tev
->uprobes
|| tev
->nargs
== 0 || !buf
)
2313 for (i
= 0; i
< tev
->nargs
; i
++)
2314 if (strglobmatch(tev
->args
[i
].value
, "[$@+-]*")) {
2315 pr_warning("Please upgrade your kernel to at least "
2316 "3.14 to have access to feature %s\n",
2317 tev
->args
[i
].value
);
2324 static int __add_probe_trace_events(struct perf_probe_event
*pev
,
2325 struct probe_trace_event
*tevs
,
2326 int ntevs
, bool allow_suffix
)
2329 struct probe_trace_event
*tev
= NULL
;
2331 const char *event
, *group
;
2332 struct strlist
*namelist
;
2333 LIST_HEAD(blacklist
);
2334 struct kprobe_blacklist_node
*node
;
2337 fd
= open_uprobe_events(true);
2339 fd
= open_kprobe_events(true);
2342 print_open_warning(fd
, !pev
->uprobes
);
2346 /* Get current event names */
2347 namelist
= get_probe_trace_event_names(fd
, false);
2349 pr_debug("Failed to get current event list.\n");
2352 /* Get kprobe blacklist if exists */
2353 if (!pev
->uprobes
) {
2354 ret
= kprobe_blacklist__load(&blacklist
);
2356 pr_debug("No kprobe blacklist support, ignored\n");
2360 pr_info("Added new event%s\n", (ntevs
> 1) ? "s:" : ":");
2361 for (i
= 0; i
< ntevs
; i
++) {
2363 /* Ensure that the address is NOT blacklisted */
2364 node
= kprobe_blacklist__find_by_address(&blacklist
,
2365 tev
->point
.address
);
2367 pr_warning("Warning: Skipped probing on blacklisted function: %s\n", node
->symbol
);
2374 if (pev
->point
.function
)
2375 event
= pev
->point
.function
;
2377 event
= tev
->point
.symbol
;
2381 group
= PERFPROBE_GROUP
;
2383 /* Get an unused new event name */
2384 ret
= get_new_event_name(buf
, 64, event
,
2385 namelist
, allow_suffix
);
2390 tev
->event
= strdup(event
);
2391 tev
->group
= strdup(group
);
2392 if (tev
->event
== NULL
|| tev
->group
== NULL
) {
2396 ret
= write_probe_trace_event(fd
, tev
);
2399 /* Add added event name to namelist */
2400 strlist__add(namelist
, event
);
2402 /* Trick here - save current event/group */
2405 pev
->event
= tev
->event
;
2406 pev
->group
= tev
->group
;
2407 show_perf_probe_event(pev
, tev
->point
.module
);
2408 /* Trick here - restore current event/group */
2409 pev
->event
= (char *)event
;
2410 pev
->group
= (char *)group
;
2413 * Probes after the first probe which comes from same
2414 * user input are always allowed to add suffix, because
2415 * there might be several addresses corresponding to
2418 allow_suffix
= true;
2420 if (ret
== -EINVAL
&& pev
->uprobes
)
2421 warn_uprobe_event_compat(tev
);
2423 /* Note that it is possible to skip all events because of blacklist */
2424 if (ret
>= 0 && tev
->event
) {
2425 /* Show how to use the event. */
2426 pr_info("\nYou can now use it in all perf tools, such as:\n\n");
2427 pr_info("\tperf record -e %s:%s -aR sleep 1\n\n", tev
->group
,
2431 kprobe_blacklist__delete(&blacklist
);
2432 strlist__delete(namelist
);
2437 static int find_probe_functions(struct map
*map
, char *name
)
2442 map__for_each_symbol_by_name(map
, name
, sym
) {
2449 #define strdup_or_goto(str, label) \
2450 ({ char *__p = strdup(str); if (!__p) goto label; __p; })
2453 * Find probe function addresses from map.
2454 * Return an error or the number of found probe_trace_event
2456 static int find_probe_trace_events_from_map(struct perf_probe_event
*pev
,
2457 struct probe_trace_event
**tevs
,
2458 int max_tevs
, const char *target
)
2460 struct map
*map
= NULL
;
2461 struct ref_reloc_sym
*reloc_sym
= NULL
;
2463 struct probe_trace_event
*tev
;
2464 struct perf_probe_point
*pp
= &pev
->point
;
2465 struct probe_trace_point
*tp
;
2466 int num_matched_functions
;
2469 map
= get_target_map(target
, pev
->uprobes
);
2476 * Load matched symbols: Since the different local symbols may have
2477 * same name but different addresses, this lists all the symbols.
2479 num_matched_functions
= find_probe_functions(map
, pp
->function
);
2480 if (num_matched_functions
== 0) {
2481 pr_err("Failed to find symbol %s in %s\n", pp
->function
,
2482 target
? : "kernel");
2485 } else if (num_matched_functions
> max_tevs
) {
2486 pr_err("Too many functions matched in %s\n",
2487 target
? : "kernel");
2492 if (!pev
->uprobes
&& !pp
->retprobe
) {
2493 reloc_sym
= kernel_get_ref_reloc_sym();
2495 pr_warning("Relocated base symbol is not found!\n");
2501 /* Setup result trace-probe-events */
2502 *tevs
= zalloc(sizeof(*tev
) * num_matched_functions
);
2510 map__for_each_symbol_by_name(map
, pp
->function
, sym
) {
2511 tev
= (*tevs
) + ret
;
2513 if (ret
== num_matched_functions
) {
2514 pr_warning("Too many symbols are listed. Skip it.\n");
2519 if (pp
->offset
> sym
->end
- sym
->start
) {
2520 pr_warning("Offset %ld is bigger than the size of %s\n",
2521 pp
->offset
, sym
->name
);
2525 /* Add one probe point */
2526 tp
->address
= map
->unmap_ip(map
, sym
->start
) + pp
->offset
;
2528 tp
->symbol
= strdup_or_goto(reloc_sym
->name
, nomem_out
);
2529 tp
->offset
= tp
->address
- reloc_sym
->addr
;
2531 tp
->symbol
= strdup_or_goto(sym
->name
, nomem_out
);
2532 tp
->offset
= pp
->offset
;
2534 tp
->retprobe
= pp
->retprobe
;
2536 tev
->point
.module
= strdup_or_goto(target
, nomem_out
);
2537 tev
->uprobes
= pev
->uprobes
;
2538 tev
->nargs
= pev
->nargs
;
2540 tev
->args
= zalloc(sizeof(struct probe_trace_arg
) *
2542 if (tev
->args
== NULL
)
2545 for (i
= 0; i
< tev
->nargs
; i
++) {
2546 if (pev
->args
[i
].name
)
2548 strdup_or_goto(pev
->args
[i
].name
,
2551 tev
->args
[i
].value
= strdup_or_goto(pev
->args
[i
].var
,
2553 if (pev
->args
[i
].type
)
2555 strdup_or_goto(pev
->args
[i
].type
,
2561 put_target_map(map
, pev
->uprobes
);
2567 clear_probe_trace_events(*tevs
, num_matched_functions
);
2572 static int convert_to_probe_trace_events(struct perf_probe_event
*pev
,
2573 struct probe_trace_event
**tevs
,
2574 int max_tevs
, const char *target
)
2578 if (pev
->uprobes
&& !pev
->group
) {
2579 /* Replace group name if not given */
2580 ret
= convert_exec_to_group(target
, &pev
->group
);
2582 pr_warning("Failed to make a group name.\n");
2587 /* Convert perf_probe_event with debuginfo */
2588 ret
= try_to_find_probe_trace_events(pev
, tevs
, max_tevs
, target
);
2590 return ret
; /* Found in debuginfo or got an error */
2592 return find_probe_trace_events_from_map(pev
, tevs
, max_tevs
, target
);
2595 struct __event_package
{
2596 struct perf_probe_event
*pev
;
2597 struct probe_trace_event
*tevs
;
2601 int add_perf_probe_events(struct perf_probe_event
*pevs
, int npevs
,
2602 int max_tevs
, bool force_add
)
2605 struct __event_package
*pkgs
;
2608 pkgs
= zalloc(sizeof(struct __event_package
) * npevs
);
2613 ret
= init_symbol_maps(pevs
->uprobes
);
2619 /* Loop 1: convert all events */
2620 for (i
= 0; i
< npevs
; i
++) {
2621 pkgs
[i
].pev
= &pevs
[i
];
2622 /* Convert with or without debuginfo */
2623 ret
= convert_to_probe_trace_events(pkgs
[i
].pev
,
2626 pkgs
[i
].pev
->target
);
2629 pkgs
[i
].ntevs
= ret
;
2632 /* Loop 2: add all events */
2633 for (i
= 0; i
< npevs
; i
++) {
2634 ret
= __add_probe_trace_events(pkgs
[i
].pev
, pkgs
[i
].tevs
,
2635 pkgs
[i
].ntevs
, force_add
);
2640 /* Loop 3: cleanup and free trace events */
2641 for (i
= 0; i
< npevs
; i
++) {
2642 for (j
= 0; j
< pkgs
[i
].ntevs
; j
++)
2643 clear_probe_trace_event(&pkgs
[i
].tevs
[j
]);
2644 zfree(&pkgs
[i
].tevs
);
2652 static int __del_trace_probe_event(int fd
, struct str_node
*ent
)
2658 /* Convert from perf-probe event to trace-probe event */
2659 ret
= e_snprintf(buf
, 128, "-:%s", ent
->s
);
2663 p
= strchr(buf
+ 2, ':');
2665 pr_debug("Internal error: %s should have ':' but not.\n",
2672 pr_debug("Writing event: %s\n", buf
);
2673 ret
= write(fd
, buf
, strlen(buf
));
2679 pr_info("Removed event: %s\n", ent
->s
);
2682 pr_warning("Failed to delete event: %s\n",
2683 strerror_r(-ret
, buf
, sizeof(buf
)));
2687 static int del_trace_probe_event(int fd
, const char *buf
,
2688 struct strlist
*namelist
)
2690 struct str_node
*ent
, *n
;
2693 if (strpbrk(buf
, "*?")) { /* Glob-exp */
2694 strlist__for_each_safe(ent
, n
, namelist
)
2695 if (strglobmatch(ent
->s
, buf
)) {
2696 ret
= __del_trace_probe_event(fd
, ent
);
2699 strlist__remove(namelist
, ent
);
2702 ent
= strlist__find(namelist
, buf
);
2704 ret
= __del_trace_probe_event(fd
, ent
);
2706 strlist__remove(namelist
, ent
);
2713 int del_perf_probe_events(struct strlist
*dellist
)
2715 int ret
= -1, ufd
= -1, kfd
= -1;
2717 const char *group
, *event
;
2719 struct str_node
*ent
;
2720 struct strlist
*namelist
= NULL
, *unamelist
= NULL
;
2722 /* Get current event names */
2723 kfd
= open_kprobe_events(true);
2725 namelist
= get_probe_trace_event_names(kfd
, true);
2727 ufd
= open_uprobe_events(true);
2729 unamelist
= get_probe_trace_event_names(ufd
, true);
2731 if (kfd
< 0 && ufd
< 0) {
2732 print_both_open_warning(kfd
, ufd
);
2736 if (namelist
== NULL
&& unamelist
== NULL
)
2739 strlist__for_each(ent
, dellist
) {
2740 str
= strdup(ent
->s
);
2745 pr_debug("Parsing: %s\n", str
);
2746 p
= strchr(str
, ':');
2756 ret
= e_snprintf(buf
, 128, "%s:%s", group
, event
);
2758 pr_err("Failed to copy event.");
2763 pr_debug("Group: %s, Event: %s\n", group
, event
);
2766 ret
= del_trace_probe_event(kfd
, buf
, namelist
);
2768 if (unamelist
&& ret
!= 0)
2769 ret
= del_trace_probe_event(ufd
, buf
, unamelist
);
2772 pr_info("Info: Event \"%s\" does not exist.\n", buf
);
2779 strlist__delete(namelist
);
2784 strlist__delete(unamelist
);
2791 /* TODO: don't use a global variable for filter ... */
2792 static struct strfilter
*available_func_filter
;
2795 * If a symbol corresponds to a function with global binding and
2796 * matches filter return 0. For all others return 1.
2798 static int filter_available_functions(struct map
*map __maybe_unused
,
2801 if (strfilter__compare(available_func_filter
, sym
->name
))
2806 int show_available_funcs(const char *target
, struct strfilter
*_filter
,
2812 ret
= init_symbol_maps(user
);
2816 /* Get a symbol map */
2818 map
= dso__new_map(target
);
2820 map
= kernel_get_module_map(target
);
2822 pr_err("Failed to get a map for %s\n", (target
) ? : "kernel");
2826 /* Load symbols with given filter */
2827 available_func_filter
= _filter
;
2828 if (map__load(map
, filter_available_functions
)) {
2829 pr_err("Failed to load symbols in %s\n", (target
) ? : "kernel");
2832 if (!dso__sorted_by_name(map
->dso
, map
->type
))
2833 dso__sort_by_name(map
->dso
, map
->type
);
2835 /* Show all (filtered) symbols */
2837 dso__fprintf_symbols_by_name(map
->dso
, map
->type
, stdout
);
2840 dso__delete(map
->dso
);