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 */
54 struct probe_conf probe_conf
;
56 #define semantic_error(msg ...) pr_err("Semantic error :" msg)
58 /* If there is no space to write, returns -E2BIG. */
59 static int e_snprintf(char *str
, size_t size
, const char *format
, ...)
60 __attribute__((format(printf
, 3, 4)));
62 static int e_snprintf(char *str
, size_t size
, const char *format
, ...)
67 ret
= vsnprintf(str
, size
, format
, ap
);
74 static char *synthesize_perf_probe_point(struct perf_probe_point
*pp
);
75 static void clear_probe_trace_event(struct probe_trace_event
*tev
);
76 static struct machine
*host_machine
;
78 /* Initialize symbol maps and path of vmlinux/modules */
79 static int init_symbol_maps(bool user_only
)
83 symbol_conf
.sort_by_name
= true;
84 symbol_conf
.allow_aliases
= true;
85 ret
= symbol__init(NULL
);
87 pr_debug("Failed to init symbol map.\n");
91 if (host_machine
|| user_only
) /* already initialized */
94 if (symbol_conf
.vmlinux_name
)
95 pr_debug("Use vmlinux: %s\n", symbol_conf
.vmlinux_name
);
97 host_machine
= machine__new_host();
99 pr_debug("machine__new_host() failed.\n");
105 pr_warning("Failed to init vmlinux path.\n");
109 static void exit_symbol_maps(void)
112 machine__delete(host_machine
);
118 static struct symbol
*__find_kernel_function_by_name(const char *name
,
121 return machine__find_kernel_function_by_name(host_machine
, name
, mapp
,
125 static struct symbol
*__find_kernel_function(u64 addr
, struct map
**mapp
)
127 return machine__find_kernel_function(host_machine
, addr
, mapp
, NULL
);
130 static struct ref_reloc_sym
*kernel_get_ref_reloc_sym(void)
132 /* kmap->ref_reloc_sym should be set if host_machine is initialized */
135 if (map__load(host_machine
->vmlinux_maps
[MAP__FUNCTION
], NULL
) < 0)
138 kmap
= map__kmap(host_machine
->vmlinux_maps
[MAP__FUNCTION
]);
141 return kmap
->ref_reloc_sym
;
144 static u64
kernel_get_symbol_address_by_name(const char *name
, bool reloc
)
146 struct ref_reloc_sym
*reloc_sym
;
150 /* ref_reloc_sym is just a label. Need a special fix*/
151 reloc_sym
= kernel_get_ref_reloc_sym();
152 if (reloc_sym
&& strcmp(name
, reloc_sym
->name
) == 0)
153 return (reloc
) ? reloc_sym
->addr
: reloc_sym
->unrelocated_addr
;
155 sym
= __find_kernel_function_by_name(name
, &map
);
157 return map
->unmap_ip(map
, sym
->start
) -
158 ((reloc
) ? 0 : map
->reloc
);
163 static struct map
*kernel_get_module_map(const char *module
)
165 struct map_groups
*grp
= &host_machine
->kmaps
;
166 struct maps
*maps
= &grp
->maps
[MAP__FUNCTION
];
169 /* A file path -- this is an offline module */
170 if (module
&& strchr(module
, '/'))
171 return machine__findnew_module_map(host_machine
, 0, module
);
176 for (pos
= maps__first(maps
); pos
; pos
= map__next(pos
)) {
177 if (strncmp(pos
->dso
->short_name
+ 1, module
,
178 pos
->dso
->short_name_len
- 2) == 0) {
185 static struct map
*get_target_map(const char *target
, bool user
)
187 /* Init maps of given executable or kernel */
189 return dso__new_map(target
);
191 return kernel_get_module_map(target
);
194 static void put_target_map(struct map
*map
, bool user
)
197 /* Only the user map needs to be released */
203 static int convert_exec_to_group(const char *exec
, char **result
)
205 char *ptr1
, *ptr2
, *exec_copy
;
209 exec_copy
= strdup(exec
);
213 ptr1
= basename(exec_copy
);
219 ptr2
= strpbrk(ptr1
, "-._");
222 ret
= e_snprintf(buf
, 64, "%s_%s", PERFPROBE_GROUP
, ptr1
);
226 *result
= strdup(buf
);
227 ret
= *result
? 0 : -ENOMEM
;
234 static void clear_perf_probe_point(struct perf_probe_point
*pp
)
241 static void clear_probe_trace_events(struct probe_trace_event
*tevs
, int ntevs
)
245 for (i
= 0; i
< ntevs
; i
++)
246 clear_probe_trace_event(tevs
+ i
);
249 static bool kprobe_blacklist__listed(unsigned long address
);
250 static bool kprobe_warn_out_range(const char *symbol
, unsigned long address
)
252 /* Get the address of _etext for checking non-probable text symbol */
253 if (kernel_get_symbol_address_by_name("_etext", false) < 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 static int parse_probe_trace_command(const char *cmd
,
1467 struct probe_trace_event
*tev
)
1469 struct probe_trace_point
*tp
= &tev
->point
;
1472 char *argv0_str
= NULL
, *fmt
, *fmt1_str
, *fmt2_str
, *fmt3_str
;
1476 pr_debug("Parsing probe_events: %s\n", cmd
);
1477 argv
= argv_split(cmd
, &argc
);
1479 pr_debug("Failed to split arguments.\n");
1483 semantic_error("Too few probe arguments.\n");
1488 /* Scan event and group name. */
1489 argv0_str
= strdup(argv
[0]);
1490 if (argv0_str
== NULL
) {
1494 fmt1_str
= strtok_r(argv0_str
, ":", &fmt
);
1495 fmt2_str
= strtok_r(NULL
, "/", &fmt
);
1496 fmt3_str
= strtok_r(NULL
, " \t", &fmt
);
1497 if (fmt1_str
== NULL
|| strlen(fmt1_str
) != 1 || fmt2_str
== NULL
1498 || fmt3_str
== NULL
) {
1499 semantic_error("Failed to parse event name: %s\n", argv
[0]);
1504 tev
->group
= strdup(fmt2_str
);
1505 tev
->event
= strdup(fmt3_str
);
1506 if (tev
->group
== NULL
|| tev
->event
== NULL
) {
1510 pr_debug("Group:%s Event:%s probe:%c\n", tev
->group
, tev
->event
, pr
);
1512 tp
->retprobe
= (pr
== 'r');
1514 /* Scan module name(if there), function name and offset */
1515 p
= strchr(argv
[1], ':');
1517 tp
->module
= strndup(argv
[1], p
- argv
[1]);
1521 fmt1_str
= strtok_r(p
, "+", &fmt
);
1522 if (fmt1_str
[0] == '0') /* only the address started with 0x */
1523 tp
->address
= strtoul(fmt1_str
, NULL
, 0);
1525 /* Only the symbol-based probe has offset */
1526 tp
->symbol
= strdup(fmt1_str
);
1527 if (tp
->symbol
== NULL
) {
1531 fmt2_str
= strtok_r(NULL
, "", &fmt
);
1532 if (fmt2_str
== NULL
)
1535 tp
->offset
= strtoul(fmt2_str
, NULL
, 10);
1538 tev
->nargs
= argc
- 2;
1539 tev
->args
= zalloc(sizeof(struct probe_trace_arg
) * tev
->nargs
);
1540 if (tev
->args
== NULL
) {
1544 for (i
= 0; i
< tev
->nargs
; i
++) {
1545 p
= strchr(argv
[i
+ 2], '=');
1546 if (p
) /* We don't need which register is assigned. */
1550 tev
->args
[i
].name
= strdup(argv
[i
+ 2]);
1551 /* TODO: parse regs and offset */
1552 tev
->args
[i
].value
= strdup(p
);
1553 if (tev
->args
[i
].name
== NULL
|| tev
->args
[i
].value
== NULL
) {
1565 /* Compose only probe arg */
1566 int synthesize_perf_probe_arg(struct perf_probe_arg
*pa
, char *buf
, size_t len
)
1568 struct perf_probe_arg_field
*field
= pa
->field
;
1572 if (pa
->name
&& pa
->var
)
1573 ret
= e_snprintf(tmp
, len
, "%s=%s", pa
->name
, pa
->var
);
1575 ret
= e_snprintf(tmp
, len
, "%s", pa
->name
? pa
->name
: pa
->var
);
1582 if (field
->name
[0] == '[')
1583 ret
= e_snprintf(tmp
, len
, "%s", field
->name
);
1585 ret
= e_snprintf(tmp
, len
, "%s%s",
1586 field
->ref
? "->" : ".", field
->name
);
1591 field
= field
->next
;
1595 ret
= e_snprintf(tmp
, len
, ":%s", pa
->type
);
1604 pr_debug("Failed to synthesize perf probe argument: %d\n", ret
);
1608 /* Compose only probe point (not argument) */
1609 static char *synthesize_perf_probe_point(struct perf_probe_point
*pp
)
1612 char offs
[32] = "", line
[32] = "", file
[32] = "";
1615 buf
= zalloc(MAX_CMDLEN
);
1621 ret
= e_snprintf(offs
, 32, "+%lu", pp
->offset
);
1626 ret
= e_snprintf(line
, 32, ":%d", pp
->line
);
1634 tmp
= strchr(pp
->file
+ len
- 30, '/');
1635 tmp
= tmp
? tmp
+ 1 : pp
->file
+ len
- 30;
1637 ret
= e_snprintf(file
, 32, "@%s", tmp
);
1643 ret
= e_snprintf(buf
, MAX_CMDLEN
, "%s%s%s%s%s", pp
->function
,
1644 offs
, pp
->retprobe
? "%return" : "", line
,
1647 ret
= e_snprintf(buf
, MAX_CMDLEN
, "%s%s", file
, line
);
1653 pr_debug("Failed to synthesize perf probe point: %d\n", ret
);
1659 char *synthesize_perf_probe_command(struct perf_probe_event
*pev
)
1664 buf
= synthesize_perf_probe_point(&pev
->point
);
1669 for (i
= 0; i
< pev
->nargs
; i
++) {
1670 ret
= e_snprintf(&buf
[len
], MAX_CMDLEN
- len
, " %s",
1683 static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref
*ref
,
1684 char **buf
, size_t *buflen
,
1689 depth
= __synthesize_probe_trace_arg_ref(ref
->next
, buf
,
1695 ret
= e_snprintf(*buf
, *buflen
, "%+ld(", ref
->offset
);
1707 static int synthesize_probe_trace_arg(struct probe_trace_arg
*arg
,
1708 char *buf
, size_t buflen
)
1710 struct probe_trace_arg_ref
*ref
= arg
->ref
;
1714 /* Argument name or separator */
1716 ret
= e_snprintf(buf
, buflen
, " %s=", arg
->name
);
1718 ret
= e_snprintf(buf
, buflen
, " ");
1724 /* Special case: @XXX */
1725 if (arg
->value
[0] == '@' && arg
->ref
)
1728 /* Dereferencing arguments */
1730 depth
= __synthesize_probe_trace_arg_ref(ref
, &buf
,
1736 /* Print argument value */
1737 if (arg
->value
[0] == '@' && arg
->ref
)
1738 ret
= e_snprintf(buf
, buflen
, "%s%+ld", arg
->value
,
1741 ret
= e_snprintf(buf
, buflen
, "%s", arg
->value
);
1749 ret
= e_snprintf(buf
, buflen
, ")");
1755 /* Print argument type */
1757 ret
= e_snprintf(buf
, buflen
, ":%s", arg
->type
);
1766 char *synthesize_probe_trace_command(struct probe_trace_event
*tev
)
1768 struct probe_trace_point
*tp
= &tev
->point
;
1772 buf
= zalloc(MAX_CMDLEN
);
1776 len
= e_snprintf(buf
, MAX_CMDLEN
, "%c:%s/%s ", tp
->retprobe
? 'r' : 'p',
1777 tev
->group
, tev
->event
);
1781 /* Uprobes must have tp->address and tp->module */
1782 if (tev
->uprobes
&& (!tp
->address
|| !tp
->module
))
1785 /* Use the tp->address for uprobes */
1787 ret
= e_snprintf(buf
+ len
, MAX_CMDLEN
- len
, "%s:0x%lx",
1788 tp
->module
, tp
->address
);
1790 ret
= e_snprintf(buf
+ len
, MAX_CMDLEN
- len
, "%s%s%s+%lu",
1791 tp
->module
?: "", tp
->module
? ":" : "",
1792 tp
->symbol
, tp
->offset
);
1798 for (i
= 0; i
< tev
->nargs
; i
++) {
1799 ret
= synthesize_probe_trace_arg(&tev
->args
[i
], buf
+ len
,
1812 static int find_perf_probe_point_from_map(struct probe_trace_point
*tp
,
1813 struct perf_probe_point
*pp
,
1816 struct symbol
*sym
= NULL
;
1822 map
= dso__new_map(tp
->module
);
1826 sym
= map__find_symbol(map
, addr
, NULL
);
1828 addr
= kernel_get_symbol_address_by_name(tp
->symbol
, true);
1831 sym
= __find_kernel_function(addr
, &map
);
1837 pp
->retprobe
= tp
->retprobe
;
1838 pp
->offset
= addr
- map
->unmap_ip(map
, sym
->start
);
1839 pp
->function
= strdup(sym
->name
);
1840 ret
= pp
->function
? 0 : -ENOMEM
;
1843 if (map
&& !is_kprobe
) {
1850 static int convert_to_perf_probe_point(struct probe_trace_point
*tp
,
1851 struct perf_probe_point
*pp
,
1857 ret
= find_perf_probe_point_from_dwarf(tp
, pp
, is_kprobe
);
1860 ret
= find_perf_probe_point_from_map(tp
, pp
, is_kprobe
);
1864 pr_debug("Failed to find probe point from both of dwarf and map.\n");
1867 pp
->function
= strdup(tp
->symbol
);
1868 pp
->offset
= tp
->offset
;
1869 } else if (!tp
->module
&& !is_kprobe
) {
1870 ret
= e_snprintf(buf
, 128, "0x%" PRIx64
, (u64
)tp
->address
);
1873 pp
->function
= strdup(buf
);
1876 if (pp
->function
== NULL
)
1879 pp
->retprobe
= tp
->retprobe
;
1884 static int convert_to_perf_probe_event(struct probe_trace_event
*tev
,
1885 struct perf_probe_event
*pev
, bool is_kprobe
)
1890 /* Convert event/group name */
1891 pev
->event
= strdup(tev
->event
);
1892 pev
->group
= strdup(tev
->group
);
1893 if (pev
->event
== NULL
|| pev
->group
== NULL
)
1896 /* Convert trace_point to probe_point */
1897 ret
= convert_to_perf_probe_point(&tev
->point
, &pev
->point
, is_kprobe
);
1901 /* Convert trace_arg to probe_arg */
1902 pev
->nargs
= tev
->nargs
;
1903 pev
->args
= zalloc(sizeof(struct perf_probe_arg
) * pev
->nargs
);
1904 if (pev
->args
== NULL
)
1906 for (i
= 0; i
< tev
->nargs
&& ret
>= 0; i
++) {
1907 if (tev
->args
[i
].name
)
1908 pev
->args
[i
].name
= strdup(tev
->args
[i
].name
);
1910 ret
= synthesize_probe_trace_arg(&tev
->args
[i
],
1912 pev
->args
[i
].name
= strdup(buf
);
1914 if (pev
->args
[i
].name
== NULL
&& ret
>= 0)
1919 clear_perf_probe_event(pev
);
1924 void clear_perf_probe_event(struct perf_probe_event
*pev
)
1926 struct perf_probe_arg_field
*field
, *next
;
1932 clear_perf_probe_point(&pev
->point
);
1934 for (i
= 0; i
< pev
->nargs
; i
++) {
1935 free(pev
->args
[i
].name
);
1936 free(pev
->args
[i
].var
);
1937 free(pev
->args
[i
].type
);
1938 field
= pev
->args
[i
].field
;
1941 zfree(&field
->name
);
1947 memset(pev
, 0, sizeof(*pev
));
1950 static void clear_probe_trace_event(struct probe_trace_event
*tev
)
1952 struct probe_trace_arg_ref
*ref
, *next
;
1957 free(tev
->point
.symbol
);
1958 free(tev
->point
.realname
);
1959 free(tev
->point
.module
);
1960 for (i
= 0; i
< tev
->nargs
; i
++) {
1961 free(tev
->args
[i
].name
);
1962 free(tev
->args
[i
].value
);
1963 free(tev
->args
[i
].type
);
1964 ref
= tev
->args
[i
].ref
;
1972 memset(tev
, 0, sizeof(*tev
));
1975 static void print_open_warning(int err
, bool is_kprobe
)
1977 char sbuf
[STRERR_BUFSIZE
];
1979 if (err
== -ENOENT
) {
1983 config
= "CONFIG_UPROBE_EVENTS";
1985 config
= "CONFIG_KPROBE_EVENTS";
1987 pr_warning("%cprobe_events file does not exist"
1988 " - please rebuild kernel with %s.\n",
1989 is_kprobe
? 'k' : 'u', config
);
1990 } else if (err
== -ENOTSUP
)
1991 pr_warning("Tracefs or debugfs is not mounted.\n");
1993 pr_warning("Failed to open %cprobe_events: %s\n",
1994 is_kprobe
? 'k' : 'u',
1995 strerror_r(-err
, sbuf
, sizeof(sbuf
)));
1998 static void print_both_open_warning(int kerr
, int uerr
)
2000 /* Both kprobes and uprobes are disabled, warn it. */
2001 if (kerr
== -ENOTSUP
&& uerr
== -ENOTSUP
)
2002 pr_warning("Tracefs or debugfs is not mounted.\n");
2003 else if (kerr
== -ENOENT
&& uerr
== -ENOENT
)
2004 pr_warning("Please rebuild kernel with CONFIG_KPROBE_EVENTS "
2005 "or/and CONFIG_UPROBE_EVENTS.\n");
2007 char sbuf
[STRERR_BUFSIZE
];
2008 pr_warning("Failed to open kprobe events: %s.\n",
2009 strerror_r(-kerr
, sbuf
, sizeof(sbuf
)));
2010 pr_warning("Failed to open uprobe events: %s.\n",
2011 strerror_r(-uerr
, sbuf
, sizeof(sbuf
)));
2015 static int open_probe_events(const char *trace_file
, bool readwrite
)
2018 const char *__debugfs
;
2019 const char *tracing_dir
= "";
2022 __debugfs
= tracefs_find_mountpoint();
2023 if (__debugfs
== NULL
) {
2024 tracing_dir
= "tracing/";
2026 __debugfs
= debugfs_find_mountpoint();
2027 if (__debugfs
== NULL
)
2031 ret
= e_snprintf(buf
, PATH_MAX
, "%s/%s%s",
2032 __debugfs
, tracing_dir
, trace_file
);
2034 pr_debug("Opening %s write=%d\n", buf
, readwrite
);
2035 if (readwrite
&& !probe_event_dry_run
)
2036 ret
= open(buf
, O_RDWR
| O_APPEND
, 0);
2038 ret
= open(buf
, O_RDONLY
, 0);
2046 static int open_kprobe_events(bool readwrite
)
2048 return open_probe_events("kprobe_events", readwrite
);
2051 static int open_uprobe_events(bool readwrite
)
2053 return open_probe_events("uprobe_events", readwrite
);
2056 /* Get raw string list of current kprobe_events or uprobe_events */
2057 static struct strlist
*get_probe_trace_command_rawlist(int fd
)
2061 char buf
[MAX_CMDLEN
];
2065 sl
= strlist__new(true, NULL
);
2067 fp
= fdopen(dup(fd
), "r");
2069 p
= fgets(buf
, MAX_CMDLEN
, fp
);
2073 idx
= strlen(p
) - 1;
2076 ret
= strlist__add(sl
, buf
);
2078 pr_debug("strlist__add failed (%d)\n", ret
);
2079 strlist__delete(sl
);
2088 struct kprobe_blacklist_node
{
2089 struct list_head list
;
2090 unsigned long start
;
2095 static void kprobe_blacklist__delete(struct list_head
*blacklist
)
2097 struct kprobe_blacklist_node
*node
;
2099 while (!list_empty(blacklist
)) {
2100 node
= list_first_entry(blacklist
,
2101 struct kprobe_blacklist_node
, list
);
2102 list_del(&node
->list
);
2108 static int kprobe_blacklist__load(struct list_head
*blacklist
)
2110 struct kprobe_blacklist_node
*node
;
2111 const char *__debugfs
= debugfs_find_mountpoint();
2112 char buf
[PATH_MAX
], *p
;
2116 if (__debugfs
== NULL
)
2119 ret
= e_snprintf(buf
, PATH_MAX
, "%s/kprobes/blacklist", __debugfs
);
2123 fp
= fopen(buf
, "r");
2128 while (fgets(buf
, PATH_MAX
, fp
)) {
2129 node
= zalloc(sizeof(*node
));
2134 INIT_LIST_HEAD(&node
->list
);
2135 list_add_tail(&node
->list
, blacklist
);
2136 if (sscanf(buf
, "0x%lx-0x%lx", &node
->start
, &node
->end
) != 2) {
2140 p
= strchr(buf
, '\t');
2143 if (p
[strlen(p
) - 1] == '\n')
2144 p
[strlen(p
) - 1] = '\0';
2146 p
= (char *)"unknown";
2147 node
->symbol
= strdup(p
);
2148 if (!node
->symbol
) {
2152 pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
2153 node
->start
, node
->end
, node
->symbol
);
2157 kprobe_blacklist__delete(blacklist
);
2163 static struct kprobe_blacklist_node
*
2164 kprobe_blacklist__find_by_address(struct list_head
*blacklist
,
2165 unsigned long address
)
2167 struct kprobe_blacklist_node
*node
;
2169 list_for_each_entry(node
, blacklist
, list
) {
2170 if (node
->start
<= address
&& address
<= node
->end
)
2177 static LIST_HEAD(kprobe_blacklist
);
2179 static void kprobe_blacklist__init(void)
2181 if (!list_empty(&kprobe_blacklist
))
2184 if (kprobe_blacklist__load(&kprobe_blacklist
) < 0)
2185 pr_debug("No kprobe blacklist support, ignored\n");
2188 static void kprobe_blacklist__release(void)
2190 kprobe_blacklist__delete(&kprobe_blacklist
);
2193 static bool kprobe_blacklist__listed(unsigned long address
)
2195 return !!kprobe_blacklist__find_by_address(&kprobe_blacklist
, address
);
2198 static int perf_probe_event__sprintf(const char *group
, const char *event
,
2199 struct perf_probe_event
*pev
,
2201 struct strbuf
*result
)
2207 /* Synthesize only event probe point */
2208 place
= synthesize_perf_probe_point(&pev
->point
);
2212 ret
= e_snprintf(buf
, 128, "%s:%s", group
, event
);
2216 strbuf_addf(result
, " %-20s (on %s", buf
, place
);
2218 strbuf_addf(result
, " in %s", module
);
2220 if (pev
->nargs
> 0) {
2221 strbuf_addstr(result
, " with");
2222 for (i
= 0; i
< pev
->nargs
; i
++) {
2223 ret
= synthesize_perf_probe_arg(&pev
->args
[i
],
2227 strbuf_addf(result
, " %s", buf
);
2230 strbuf_addch(result
, ')');
2237 static int show_perf_probe_event(const char *group
, const char *event
,
2238 struct perf_probe_event
*pev
,
2239 const char *module
, bool use_stdout
)
2241 struct strbuf buf
= STRBUF_INIT
;
2244 ret
= perf_probe_event__sprintf(group
, event
, pev
, module
, &buf
);
2247 printf("%s\n", buf
.buf
);
2249 pr_info("%s\n", buf
.buf
);
2251 strbuf_release(&buf
);
2256 static bool filter_probe_trace_event(struct probe_trace_event
*tev
,
2257 struct strfilter
*filter
)
2261 /* At first, check the event name itself */
2262 if (strfilter__compare(filter
, tev
->event
))
2265 /* Next, check the combination of name and group */
2266 if (e_snprintf(tmp
, 128, "%s:%s", tev
->group
, tev
->event
) < 0)
2268 return strfilter__compare(filter
, tmp
);
2271 static int __show_perf_probe_events(int fd
, bool is_kprobe
,
2272 struct strfilter
*filter
)
2275 struct probe_trace_event tev
;
2276 struct perf_probe_event pev
;
2277 struct strlist
*rawlist
;
2278 struct str_node
*ent
;
2280 memset(&tev
, 0, sizeof(tev
));
2281 memset(&pev
, 0, sizeof(pev
));
2283 rawlist
= get_probe_trace_command_rawlist(fd
);
2287 strlist__for_each(ent
, rawlist
) {
2288 ret
= parse_probe_trace_command(ent
->s
, &tev
);
2290 if (!filter_probe_trace_event(&tev
, filter
))
2292 ret
= convert_to_perf_probe_event(&tev
, &pev
,
2296 ret
= show_perf_probe_event(pev
.group
, pev
.event
,
2297 &pev
, tev
.point
.module
,
2301 clear_perf_probe_event(&pev
);
2302 clear_probe_trace_event(&tev
);
2306 strlist__delete(rawlist
);
2307 /* Cleanup cached debuginfo if needed */
2308 debuginfo_cache__exit();
2313 /* List up current perf-probe events */
2314 int show_perf_probe_events(struct strfilter
*filter
)
2316 int kp_fd
, up_fd
, ret
;
2320 ret
= init_symbol_maps(false);
2324 kp_fd
= open_kprobe_events(false);
2326 ret
= __show_perf_probe_events(kp_fd
, true, filter
);
2332 up_fd
= open_uprobe_events(false);
2333 if (kp_fd
< 0 && up_fd
< 0) {
2334 print_both_open_warning(kp_fd
, up_fd
);
2340 ret
= __show_perf_probe_events(up_fd
, false, filter
);
2348 /* Get current perf-probe event names */
2349 static struct strlist
*get_probe_trace_event_names(int fd
, bool include_group
)
2352 struct strlist
*sl
, *rawlist
;
2353 struct str_node
*ent
;
2354 struct probe_trace_event tev
;
2357 memset(&tev
, 0, sizeof(tev
));
2358 rawlist
= get_probe_trace_command_rawlist(fd
);
2361 sl
= strlist__new(true, NULL
);
2362 strlist__for_each(ent
, rawlist
) {
2363 ret
= parse_probe_trace_command(ent
->s
, &tev
);
2366 if (include_group
) {
2367 ret
= e_snprintf(buf
, 128, "%s:%s", tev
.group
,
2370 ret
= strlist__add(sl
, buf
);
2372 ret
= strlist__add(sl
, tev
.event
);
2373 clear_probe_trace_event(&tev
);
2377 strlist__delete(rawlist
);
2380 strlist__delete(sl
);
2386 static int write_probe_trace_event(int fd
, struct probe_trace_event
*tev
)
2389 char *buf
= synthesize_probe_trace_command(tev
);
2390 char sbuf
[STRERR_BUFSIZE
];
2393 pr_debug("Failed to synthesize probe trace event.\n");
2397 pr_debug("Writing event: %s\n", buf
);
2398 if (!probe_event_dry_run
) {
2399 ret
= write(fd
, buf
, strlen(buf
));
2402 pr_warning("Failed to write event: %s\n",
2403 strerror_r(errno
, sbuf
, sizeof(sbuf
)));
2410 static int get_new_event_name(char *buf
, size_t len
, const char *base
,
2411 struct strlist
*namelist
, bool allow_suffix
)
2420 ret
= e_snprintf(buf
, len
, "%s", base
);
2422 pr_debug("snprintf() failed: %d\n", ret
);
2425 /* Cut off the postfixes (e.g. .const, .isra)*/
2426 p
= strchr(buf
, '.');
2429 if (!strlist__has_entry(namelist
, buf
))
2432 if (!allow_suffix
) {
2433 pr_warning("Error: event \"%s\" already exists. "
2434 "(Use -f to force duplicates.)\n", base
);
2438 /* Try to add suffix */
2439 for (i
= 1; i
< MAX_EVENT_INDEX
; i
++) {
2440 ret
= e_snprintf(buf
, len
, "%s_%d", base
, i
);
2442 pr_debug("snprintf() failed: %d\n", ret
);
2445 if (!strlist__has_entry(namelist
, buf
))
2448 if (i
== MAX_EVENT_INDEX
) {
2449 pr_warning("Too many events are on the same function.\n");
2456 /* Warn if the current kernel's uprobe implementation is old */
2457 static void warn_uprobe_event_compat(struct probe_trace_event
*tev
)
2460 char *buf
= synthesize_probe_trace_command(tev
);
2462 /* Old uprobe event doesn't support memory dereference */
2463 if (!tev
->uprobes
|| tev
->nargs
== 0 || !buf
)
2466 for (i
= 0; i
< tev
->nargs
; i
++)
2467 if (strglobmatch(tev
->args
[i
].value
, "[$@+-]*")) {
2468 pr_warning("Please upgrade your kernel to at least "
2469 "3.14 to have access to feature %s\n",
2470 tev
->args
[i
].value
);
2477 static int __add_probe_trace_events(struct perf_probe_event
*pev
,
2478 struct probe_trace_event
*tevs
,
2479 int ntevs
, bool allow_suffix
)
2482 struct probe_trace_event
*tev
= NULL
;
2484 const char *event
= NULL
, *group
= NULL
;
2485 struct strlist
*namelist
;
2489 fd
= open_uprobe_events(true);
2491 fd
= open_kprobe_events(true);
2494 print_open_warning(fd
, !pev
->uprobes
);
2498 /* Get current event names */
2499 namelist
= get_probe_trace_event_names(fd
, false);
2501 pr_debug("Failed to get current event list.\n");
2506 safename
= (pev
->point
.function
&& !strisglob(pev
->point
.function
));
2508 pr_info("Added new event%s\n", (ntevs
> 1) ? "s:" : ":");
2509 for (i
= 0; i
< ntevs
; i
++) {
2511 /* Skip if the symbol is out of .text or blacklisted */
2512 if (!tev
->point
.symbol
)
2519 event
= pev
->point
.function
;
2521 event
= tev
->point
.realname
;
2525 group
= PERFPROBE_GROUP
;
2527 /* Get an unused new event name */
2528 ret
= get_new_event_name(buf
, 64, event
,
2529 namelist
, allow_suffix
);
2534 tev
->event
= strdup(event
);
2535 tev
->group
= strdup(group
);
2536 if (tev
->event
== NULL
|| tev
->group
== NULL
) {
2540 ret
= write_probe_trace_event(fd
, tev
);
2543 /* Add added event name to namelist */
2544 strlist__add(namelist
, event
);
2546 /* We use tev's name for showing new events */
2547 show_perf_probe_event(tev
->group
, tev
->event
, pev
,
2548 tev
->point
.module
, false);
2549 /* Save the last valid name */
2554 * Probes after the first probe which comes from same
2555 * user input are always allowed to add suffix, because
2556 * there might be several addresses corresponding to
2559 allow_suffix
= true;
2561 if (ret
== -EINVAL
&& pev
->uprobes
)
2562 warn_uprobe_event_compat(tev
);
2564 /* Note that it is possible to skip all events because of blacklist */
2565 if (ret
>= 0 && event
) {
2566 /* Show how to use the event. */
2567 pr_info("\nYou can now use it in all perf tools, such as:\n\n");
2568 pr_info("\tperf record -e %s:%s -aR sleep 1\n\n", group
, event
);
2571 strlist__delete(namelist
);
2577 static int find_probe_functions(struct map
*map
, char *name
,
2578 struct symbol
**syms
)
2582 struct rb_node
*tmp
;
2584 if (map__load(map
, NULL
) < 0)
2587 map__for_each_symbol(map
, sym
, tmp
) {
2588 if (strglobmatch(sym
->name
, name
)) {
2590 if (syms
&& found
< probe_conf
.max_probes
)
2591 syms
[found
- 1] = sym
;
2598 #define strdup_or_goto(str, label) \
2599 ({ char *__p = strdup(str); if (!__p) goto label; __p; })
2601 void __weak
arch__fix_tev_from_maps(struct perf_probe_event
*pev __maybe_unused
,
2602 struct probe_trace_event
*tev __maybe_unused
,
2603 struct map
*map __maybe_unused
) { }
2606 * Find probe function addresses from map.
2607 * Return an error or the number of found probe_trace_event
2609 static int find_probe_trace_events_from_map(struct perf_probe_event
*pev
,
2610 struct probe_trace_event
**tevs
)
2612 struct map
*map
= NULL
;
2613 struct ref_reloc_sym
*reloc_sym
= NULL
;
2615 struct symbol
**syms
= NULL
;
2616 struct probe_trace_event
*tev
;
2617 struct perf_probe_point
*pp
= &pev
->point
;
2618 struct probe_trace_point
*tp
;
2619 int num_matched_functions
;
2620 int ret
, i
, j
, skipped
= 0;
2622 map
= get_target_map(pev
->target
, pev
->uprobes
);
2628 syms
= malloc(sizeof(struct symbol
*) * probe_conf
.max_probes
);
2635 * Load matched symbols: Since the different local symbols may have
2636 * same name but different addresses, this lists all the symbols.
2638 num_matched_functions
= find_probe_functions(map
, pp
->function
, syms
);
2639 if (num_matched_functions
== 0) {
2640 pr_err("Failed to find symbol %s in %s\n", pp
->function
,
2641 pev
->target
? : "kernel");
2644 } else if (num_matched_functions
> probe_conf
.max_probes
) {
2645 pr_err("Too many functions matched in %s\n",
2646 pev
->target
? : "kernel");
2651 if (!pev
->uprobes
&& !pp
->retprobe
) {
2652 reloc_sym
= kernel_get_ref_reloc_sym();
2654 pr_warning("Relocated base symbol is not found!\n");
2660 /* Setup result trace-probe-events */
2661 *tevs
= zalloc(sizeof(*tev
) * num_matched_functions
);
2669 for (j
= 0; j
< num_matched_functions
; j
++) {
2672 tev
= (*tevs
) + ret
;
2674 if (ret
== num_matched_functions
) {
2675 pr_warning("Too many symbols are listed. Skip it.\n");
2680 if (pp
->offset
> sym
->end
- sym
->start
) {
2681 pr_warning("Offset %ld is bigger than the size of %s\n",
2682 pp
->offset
, sym
->name
);
2686 /* Add one probe point */
2687 tp
->address
= map
->unmap_ip(map
, sym
->start
) + pp
->offset
;
2688 /* If we found a wrong one, mark it by NULL symbol */
2689 if (!pev
->uprobes
&&
2690 kprobe_warn_out_range(sym
->name
, tp
->address
)) {
2691 tp
->symbol
= NULL
; /* Skip it */
2693 } else if (reloc_sym
) {
2694 tp
->symbol
= strdup_or_goto(reloc_sym
->name
, nomem_out
);
2695 tp
->offset
= tp
->address
- reloc_sym
->addr
;
2697 tp
->symbol
= strdup_or_goto(sym
->name
, nomem_out
);
2698 tp
->offset
= pp
->offset
;
2700 tp
->realname
= strdup_or_goto(sym
->name
, nomem_out
);
2702 tp
->retprobe
= pp
->retprobe
;
2704 tev
->point
.module
= strdup_or_goto(pev
->target
,
2706 tev
->uprobes
= pev
->uprobes
;
2707 tev
->nargs
= pev
->nargs
;
2709 tev
->args
= zalloc(sizeof(struct probe_trace_arg
) *
2711 if (tev
->args
== NULL
)
2714 for (i
= 0; i
< tev
->nargs
; i
++) {
2715 if (pev
->args
[i
].name
)
2717 strdup_or_goto(pev
->args
[i
].name
,
2720 tev
->args
[i
].value
= strdup_or_goto(pev
->args
[i
].var
,
2722 if (pev
->args
[i
].type
)
2724 strdup_or_goto(pev
->args
[i
].type
,
2727 arch__fix_tev_from_maps(pev
, tev
, map
);
2729 if (ret
== skipped
) {
2735 put_target_map(map
, pev
->uprobes
);
2742 clear_probe_trace_events(*tevs
, num_matched_functions
);
2747 bool __weak
arch__prefers_symtab(void) { return false; }
2749 static int convert_to_probe_trace_events(struct perf_probe_event
*pev
,
2750 struct probe_trace_event
**tevs
)
2754 if (pev
->uprobes
&& !pev
->group
) {
2755 /* Replace group name if not given */
2756 ret
= convert_exec_to_group(pev
->target
, &pev
->group
);
2758 pr_warning("Failed to make a group name.\n");
2763 if (arch__prefers_symtab() && !perf_probe_event_need_dwarf(pev
)) {
2764 ret
= find_probe_trace_events_from_map(pev
, tevs
);
2766 return ret
; /* Found in symbol table */
2769 /* Convert perf_probe_event with debuginfo */
2770 ret
= try_to_find_probe_trace_events(pev
, tevs
);
2772 return ret
; /* Found in debuginfo or got an error */
2774 return find_probe_trace_events_from_map(pev
, tevs
);
2777 struct __event_package
{
2778 struct perf_probe_event
*pev
;
2779 struct probe_trace_event
*tevs
;
2783 int add_perf_probe_events(struct perf_probe_event
*pevs
, int npevs
)
2786 struct __event_package
*pkgs
;
2789 pkgs
= zalloc(sizeof(struct __event_package
) * npevs
);
2794 ret
= init_symbol_maps(pevs
->uprobes
);
2800 /* Loop 1: convert all events */
2801 for (i
= 0; i
< npevs
; i
++) {
2802 pkgs
[i
].pev
= &pevs
[i
];
2803 /* Init kprobe blacklist if needed */
2804 if (!pkgs
[i
].pev
->uprobes
)
2805 kprobe_blacklist__init();
2806 /* Convert with or without debuginfo */
2807 ret
= convert_to_probe_trace_events(pkgs
[i
].pev
,
2811 pkgs
[i
].ntevs
= ret
;
2813 /* This just release blacklist only if allocated */
2814 kprobe_blacklist__release();
2816 /* Loop 2: add all events */
2817 for (i
= 0; i
< npevs
; i
++) {
2818 ret
= __add_probe_trace_events(pkgs
[i
].pev
, pkgs
[i
].tevs
,
2820 probe_conf
.force_add
);
2825 /* Loop 3: cleanup and free trace events */
2826 for (i
= 0; i
< npevs
; i
++) {
2827 for (j
= 0; j
< pkgs
[i
].ntevs
; j
++)
2828 clear_probe_trace_event(&pkgs
[i
].tevs
[j
]);
2829 zfree(&pkgs
[i
].tevs
);
2837 static int __del_trace_probe_event(int fd
, struct str_node
*ent
)
2843 /* Convert from perf-probe event to trace-probe event */
2844 ret
= e_snprintf(buf
, 128, "-:%s", ent
->s
);
2848 p
= strchr(buf
+ 2, ':');
2850 pr_debug("Internal error: %s should have ':' but not.\n",
2857 pr_debug("Writing event: %s\n", buf
);
2858 ret
= write(fd
, buf
, strlen(buf
));
2864 pr_info("Removed event: %s\n", ent
->s
);
2867 pr_warning("Failed to delete event: %s\n",
2868 strerror_r(-ret
, buf
, sizeof(buf
)));
2872 static int del_trace_probe_events(int fd
, struct strfilter
*filter
,
2873 struct strlist
*namelist
)
2875 struct str_node
*ent
;
2882 strlist__for_each(ent
, namelist
) {
2883 p
= strchr(ent
->s
, ':');
2884 if ((p
&& strfilter__compare(filter
, p
+ 1)) ||
2885 strfilter__compare(filter
, ent
->s
)) {
2886 ret
= __del_trace_probe_event(fd
, ent
);
2895 int del_perf_probe_events(struct strfilter
*filter
)
2897 int ret
, ret2
, ufd
= -1, kfd
= -1;
2898 struct strlist
*namelist
= NULL
, *unamelist
= NULL
;
2899 char *str
= strfilter__string(filter
);
2904 pr_debug("Delete filter: \'%s\'\n", str
);
2906 /* Get current event names */
2907 kfd
= open_kprobe_events(true);
2909 namelist
= get_probe_trace_event_names(kfd
, true);
2911 ufd
= open_uprobe_events(true);
2913 unamelist
= get_probe_trace_event_names(ufd
, true);
2915 if (kfd
< 0 && ufd
< 0) {
2916 print_both_open_warning(kfd
, ufd
);
2921 ret
= del_trace_probe_events(kfd
, filter
, namelist
);
2922 if (ret
< 0 && ret
!= -ENOENT
)
2925 ret2
= del_trace_probe_events(ufd
, filter
, unamelist
);
2926 if (ret2
< 0 && ret2
!= -ENOENT
) {
2930 if (ret
== -ENOENT
&& ret2
== -ENOENT
)
2931 pr_debug("\"%s\" does not hit any event.\n", str
);
2932 /* Note that this is silently ignored */
2937 strlist__delete(namelist
);
2942 strlist__delete(unamelist
);
2950 /* TODO: don't use a global variable for filter ... */
2951 static struct strfilter
*available_func_filter
;
2954 * If a symbol corresponds to a function with global binding and
2955 * matches filter return 0. For all others return 1.
2957 static int filter_available_functions(struct map
*map __maybe_unused
,
2960 if (strfilter__compare(available_func_filter
, sym
->name
))
2965 int show_available_funcs(const char *target
, struct strfilter
*_filter
,
2971 ret
= init_symbol_maps(user
);
2975 /* Get a symbol map */
2977 map
= dso__new_map(target
);
2979 map
= kernel_get_module_map(target
);
2981 pr_err("Failed to get a map for %s\n", (target
) ? : "kernel");
2985 /* Load symbols with given filter */
2986 available_func_filter
= _filter
;
2987 if (map__load(map
, filter_available_functions
)) {
2988 pr_err("Failed to load symbols in %s\n", (target
) ? : "kernel");
2991 if (!dso__sorted_by_name(map
->dso
, map
->type
))
2992 dso__sort_by_name(map
->dso
, map
->type
);
2994 /* Show all (filtered) symbols */
2996 dso__fprintf_symbols_by_name(map
->dso
, map
->type
, stdout
);