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 "trace-event.h" /* For __maybe_unused */
45 #include "probe-event.h"
46 #include "probe-finder.h"
49 #define MAX_CMDLEN 256
50 #define PERFPROBE_GROUP "probe"
52 bool probe_event_dry_run
; /* Dry run flag */
54 #define semantic_error(msg ...) pr_err("Semantic error :" msg)
56 /* If there is no space to write, returns -E2BIG. */
57 static int e_snprintf(char *str
, size_t size
, const char *format
, ...)
58 __attribute__((format(printf
, 3, 4)));
60 static int e_snprintf(char *str
, size_t size
, const char *format
, ...)
65 ret
= vsnprintf(str
, size
, format
, ap
);
72 static char *synthesize_perf_probe_point(struct perf_probe_point
*pp
);
73 static int convert_name_to_addr(struct perf_probe_event
*pev
,
75 static void clear_probe_trace_event(struct probe_trace_event
*tev
);
76 static struct machine machine
;
78 /* Initialize symbol maps and path of vmlinux/modules */
79 static int init_vmlinux(void)
83 symbol_conf
.sort_by_name
= true;
84 if (symbol_conf
.vmlinux_name
== NULL
)
85 symbol_conf
.try_vmlinux_path
= true;
87 pr_debug("Use vmlinux: %s\n", symbol_conf
.vmlinux_name
);
90 pr_debug("Failed to init symbol map.\n");
94 ret
= machine__init(&machine
, "", HOST_KERNEL_ID
);
98 if (machine__create_kernel_maps(&machine
) < 0) {
99 pr_debug("machine__create_kernel_maps() failed.\n");
104 pr_warning("Failed to init vmlinux path.\n");
108 static struct symbol
*__find_kernel_function_by_name(const char *name
,
111 return machine__find_kernel_function_by_name(&machine
, name
, mapp
,
115 static struct map
*kernel_get_module_map(const char *module
)
118 struct map_groups
*grp
= &machine
.kmaps
;
120 /* A file path -- this is an offline module */
121 if (module
&& strchr(module
, '/'))
122 return machine__new_module(&machine
, 0, module
);
127 for (nd
= rb_first(&grp
->maps
[MAP__FUNCTION
]); nd
; nd
= rb_next(nd
)) {
128 struct map
*pos
= rb_entry(nd
, struct map
, rb_node
);
129 if (strncmp(pos
->dso
->short_name
+ 1, module
,
130 pos
->dso
->short_name_len
- 2) == 0) {
137 static struct dso
*kernel_get_module_dso(const char *module
)
141 const char *vmlinux_name
;
144 list_for_each_entry(dso
, &machine
.kernel_dsos
, node
) {
145 if (strncmp(dso
->short_name
+ 1, module
,
146 dso
->short_name_len
- 2) == 0)
149 pr_debug("Failed to find module %s.\n", module
);
153 map
= machine
.vmlinux_maps
[MAP__FUNCTION
];
156 vmlinux_name
= symbol_conf
.vmlinux_name
;
158 if (dso__load_vmlinux(dso
, map
, vmlinux_name
, false, NULL
) <= 0)
161 if (dso__load_vmlinux_path(dso
, map
, NULL
) <= 0) {
162 pr_debug("Failed to load kernel map.\n");
170 const char *kernel_get_module_path(const char *module
)
172 struct dso
*dso
= kernel_get_module_dso(module
);
173 return (dso
) ? dso
->long_name
: NULL
;
176 static int init_user_exec(void)
180 symbol_conf
.try_vmlinux_path
= false;
181 symbol_conf
.sort_by_name
= true;
182 ret
= symbol__init();
185 pr_debug("Failed to init symbol map.\n");
190 static int convert_exec_to_group(const char *exec
, char **result
)
192 char *ptr1
, *ptr2
, *exec_copy
;
196 exec_copy
= strdup(exec
);
200 ptr1
= basename(exec_copy
);
206 ptr2
= strpbrk(ptr1
, "-._");
209 ret
= e_snprintf(buf
, 64, "%s_%s", PERFPROBE_GROUP
, ptr1
);
213 *result
= strdup(buf
);
214 ret
= *result
? 0 : -ENOMEM
;
221 static int convert_to_perf_probe_point(struct probe_trace_point
*tp
,
222 struct perf_probe_point
*pp
)
224 pp
->function
= strdup(tp
->symbol
);
226 if (pp
->function
== NULL
)
229 pp
->offset
= tp
->offset
;
230 pp
->retprobe
= tp
->retprobe
;
235 #ifdef HAVE_DWARF_SUPPORT
236 /* Open new debuginfo of given module */
237 static struct debuginfo
*open_debuginfo(const char *module
)
241 /* A file path -- this is an offline module */
242 if (module
&& strchr(module
, '/'))
245 path
= kernel_get_module_path(module
);
248 pr_err("Failed to find path of %s module.\n",
253 return debuginfo__new(path
);
257 * Convert trace point to probe point with debuginfo
258 * Currently only handles kprobes.
260 static int kprobe_convert_to_perf_probe(struct probe_trace_point
*tp
,
261 struct perf_probe_point
*pp
)
267 struct debuginfo
*dinfo
;
269 sym
= __find_kernel_function_by_name(tp
->symbol
, &map
);
271 addr
= map
->unmap_ip(map
, sym
->start
+ tp
->offset
);
272 pr_debug("try to find %s+%ld@%" PRIx64
"\n", tp
->symbol
,
275 dinfo
= debuginfo__new_online_kernel(addr
);
277 ret
= debuginfo__find_probe_point(dinfo
,
278 (unsigned long)addr
, pp
);
279 debuginfo__delete(dinfo
);
281 pr_debug("Failed to open debuginfo at 0x%" PRIx64
"\n",
287 pr_debug("Failed to find corresponding probes from "
288 "debuginfo. Use kprobe event information.\n");
289 return convert_to_perf_probe_point(tp
, pp
);
291 pp
->retprobe
= tp
->retprobe
;
296 static int get_text_start_address(const char *exec
, unsigned long *address
)
301 int fd
, ret
= -ENOENT
;
303 fd
= open(exec
, O_RDONLY
);
307 elf
= elf_begin(fd
, PERF_ELF_C_READ_MMAP
, NULL
);
311 if (gelf_getehdr(elf
, &ehdr
) == NULL
)
314 if (!elf_section_by_name(elf
, &ehdr
, &shdr
, ".text", NULL
))
317 *address
= shdr
.sh_addr
- shdr
.sh_offset
;
324 static int add_exec_to_probe_trace_events(struct probe_trace_event
*tevs
,
325 int ntevs
, const char *exec
)
328 unsigned long offset
, stext
= 0;
334 ret
= get_text_start_address(exec
, &stext
);
338 for (i
= 0; i
< ntevs
&& ret
>= 0; i
++) {
339 /* point.address is the addres of point.symbol + point.offset */
340 offset
= tevs
[i
].point
.address
- stext
;
341 tevs
[i
].point
.offset
= 0;
342 zfree(&tevs
[i
].point
.symbol
);
343 ret
= e_snprintf(buf
, 32, "0x%lx", offset
);
346 tevs
[i
].point
.module
= strdup(exec
);
347 tevs
[i
].point
.symbol
= strdup(buf
);
348 if (!tevs
[i
].point
.symbol
|| !tevs
[i
].point
.module
) {
352 tevs
[i
].uprobes
= true;
358 static int add_module_to_probe_trace_events(struct probe_trace_event
*tevs
,
359 int ntevs
, const char *module
)
367 tmp
= strrchr(module
, '/');
369 /* This is a module path -- get the module name */
370 module
= strdup(tmp
+ 1);
373 tmp
= strchr(module
, '.');
376 tmp
= (char *)module
; /* For free() */
379 for (i
= 0; i
< ntevs
; i
++) {
380 tevs
[i
].point
.module
= strdup(module
);
381 if (!tevs
[i
].point
.module
) {
391 static void clear_probe_trace_events(struct probe_trace_event
*tevs
, int ntevs
)
395 for (i
= 0; i
< ntevs
; i
++)
396 clear_probe_trace_event(tevs
+ i
);
399 /* Try to find perf_probe_event with debuginfo */
400 static int try_to_find_probe_trace_events(struct perf_probe_event
*pev
,
401 struct probe_trace_event
**tevs
,
402 int max_tevs
, const char *target
)
404 bool need_dwarf
= perf_probe_event_need_dwarf(pev
);
405 struct debuginfo
*dinfo
;
408 dinfo
= open_debuginfo(target
);
412 pr_warning("Failed to open debuginfo file.\n");
415 pr_debug("Could not open debuginfo. Try to use symbols.\n");
419 /* Searching trace events corresponding to a probe event */
420 ntevs
= debuginfo__find_trace_events(dinfo
, pev
, tevs
, max_tevs
);
422 debuginfo__delete(dinfo
);
424 if (ntevs
> 0) { /* Succeeded to find trace events */
425 pr_debug("find %d probe_trace_events.\n", ntevs
);
428 ret
= add_exec_to_probe_trace_events(*tevs
,
431 ret
= add_module_to_probe_trace_events(*tevs
,
435 clear_probe_trace_events(*tevs
, ntevs
);
438 return ret
< 0 ? ret
: ntevs
;
441 if (ntevs
== 0) { /* No error but failed to find probe point. */
442 pr_warning("Probe point '%s' not found.\n",
443 synthesize_perf_probe_point(&pev
->point
));
446 /* Error path : ntevs < 0 */
447 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs
);
448 if (ntevs
== -EBADF
) {
449 pr_warning("Warning: No dwarf info found in the vmlinux - "
450 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
452 pr_debug("Trying to use symbols.\n");
460 * Find a src file from a DWARF tag path. Prepend optional source path prefix
461 * and chop off leading directories that do not exist. Result is passed back as
462 * a newly allocated path on success.
463 * Return 0 if file was found and readable, -errno otherwise.
465 static int get_real_path(const char *raw_path
, const char *comp_dir
,
468 const char *prefix
= symbol_conf
.source_prefix
;
471 if (raw_path
[0] != '/' && comp_dir
)
472 /* If not an absolute path, try to use comp_dir */
475 if (access(raw_path
, R_OK
) == 0) {
476 *new_path
= strdup(raw_path
);
483 *new_path
= malloc((strlen(prefix
) + strlen(raw_path
) + 2));
488 sprintf(*new_path
, "%s/%s", prefix
, raw_path
);
490 if (access(*new_path
, R_OK
) == 0)
493 if (!symbol_conf
.source_prefix
)
494 /* In case of searching comp_dir, don't retry */
502 raw_path
= strchr(++raw_path
, '/');
516 #define LINEBUF_SIZE 256
517 #define NR_ADDITIONAL_LINES 2
519 static int __show_one_line(FILE *fp
, int l
, bool skip
, bool show_num
)
521 char buf
[LINEBUF_SIZE
];
522 const char *color
= show_num
? "" : PERF_COLOR_BLUE
;
523 const char *prefix
= NULL
;
526 if (fgets(buf
, LINEBUF_SIZE
, fp
) == NULL
)
531 prefix
= show_num
? "%7d " : " ";
532 color_fprintf(stdout
, color
, prefix
, l
);
534 color_fprintf(stdout
, color
, "%s", buf
);
536 } while (strchr(buf
, '\n') == NULL
);
541 pr_warning("File read error: %s\n", strerror(errno
));
547 static int _show_one_line(FILE *fp
, int l
, bool skip
, bool show_num
)
549 int rv
= __show_one_line(fp
, l
, skip
, show_num
);
551 pr_warning("Source file is shorter than expected.\n");
557 #define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
558 #define show_one_line(f,l) _show_one_line(f,l,false,false)
559 #define skip_one_line(f,l) _show_one_line(f,l,true,false)
560 #define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
563 * Show line-range always requires debuginfo to find source file and
566 int show_line_range(struct line_range
*lr
, const char *module
)
569 struct line_node
*ln
;
570 struct debuginfo
*dinfo
;
575 /* Search a line range */
576 ret
= init_vmlinux();
580 dinfo
= open_debuginfo(module
);
582 pr_warning("Failed to open debuginfo file.\n");
586 ret
= debuginfo__find_line_range(dinfo
, lr
);
587 debuginfo__delete(dinfo
);
589 pr_warning("Specified source line is not found.\n");
591 } else if (ret
< 0) {
592 pr_warning("Debuginfo analysis failed. (%d)\n", ret
);
596 /* Convert source file path */
598 ret
= get_real_path(tmp
, lr
->comp_dir
, &lr
->path
);
599 free(tmp
); /* Free old path */
601 pr_warning("Failed to find source file. (%d)\n", ret
);
608 fprintf(stdout
, "<%s@%s:%d>\n", lr
->function
, lr
->path
,
609 lr
->start
- lr
->offset
);
611 fprintf(stdout
, "<%s:%d>\n", lr
->path
, lr
->start
);
613 fp
= fopen(lr
->path
, "r");
615 pr_warning("Failed to open %s: %s\n", lr
->path
,
619 /* Skip to starting line number */
620 while (l
< lr
->start
) {
621 ret
= skip_one_line(fp
, l
++);
626 list_for_each_entry(ln
, &lr
->line_list
, list
) {
627 for (; ln
->line
> l
; l
++) {
628 ret
= show_one_line(fp
, l
- lr
->offset
);
632 ret
= show_one_line_with_num(fp
, l
++ - lr
->offset
);
637 if (lr
->end
== INT_MAX
)
638 lr
->end
= l
+ NR_ADDITIONAL_LINES
;
639 while (l
<= lr
->end
) {
640 ret
= show_one_line_or_eof(fp
, l
++ - lr
->offset
);
649 static int show_available_vars_at(struct debuginfo
*dinfo
,
650 struct perf_probe_event
*pev
,
651 int max_vls
, struct strfilter
*_filter
,
656 struct str_node
*node
;
657 struct variable_list
*vls
= NULL
, *vl
;
660 buf
= synthesize_perf_probe_point(&pev
->point
);
663 pr_debug("Searching variables at %s\n", buf
);
665 ret
= debuginfo__find_available_vars_at(dinfo
, pev
, &vls
,
668 pr_err("Failed to find variables at %s (%d)\n", buf
, ret
);
671 /* Some variables are found */
672 fprintf(stdout
, "Available variables at %s\n", buf
);
673 for (i
= 0; i
< ret
; i
++) {
676 * A probe point might be converted to
677 * several trace points.
679 fprintf(stdout
, "\t@<%s+%lu>\n", vl
->point
.symbol
,
681 zfree(&vl
->point
.symbol
);
684 strlist__for_each(node
, vl
->vars
) {
685 var
= strchr(node
->s
, '\t') + 1;
686 if (strfilter__compare(_filter
, var
)) {
687 fprintf(stdout
, "\t\t%s\n", node
->s
);
691 strlist__delete(vl
->vars
);
694 fprintf(stdout
, "\t\t(No matched variables)\n");
702 /* Show available variables on given probe point */
703 int show_available_vars(struct perf_probe_event
*pevs
, int npevs
,
704 int max_vls
, const char *module
,
705 struct strfilter
*_filter
, bool externs
)
708 struct debuginfo
*dinfo
;
710 ret
= init_vmlinux();
714 dinfo
= open_debuginfo(module
);
716 pr_warning("Failed to open debuginfo file.\n");
722 for (i
= 0; i
< npevs
&& ret
>= 0; i
++)
723 ret
= show_available_vars_at(dinfo
, &pevs
[i
], max_vls
, _filter
,
726 debuginfo__delete(dinfo
);
730 #else /* !HAVE_DWARF_SUPPORT */
732 static int kprobe_convert_to_perf_probe(struct probe_trace_point
*tp
,
733 struct perf_probe_point
*pp
)
737 sym
= __find_kernel_function_by_name(tp
->symbol
, NULL
);
739 pr_err("Failed to find symbol %s in kernel.\n", tp
->symbol
);
743 return convert_to_perf_probe_point(tp
, pp
);
746 static int try_to_find_probe_trace_events(struct perf_probe_event
*pev
,
747 struct probe_trace_event
**tevs __maybe_unused
,
748 int max_tevs __maybe_unused
,
749 const char *target __maybe_unused
)
751 if (perf_probe_event_need_dwarf(pev
)) {
752 pr_warning("Debuginfo-analysis is not supported.\n");
759 int show_line_range(struct line_range
*lr __maybe_unused
,
760 const char *module __maybe_unused
)
762 pr_warning("Debuginfo-analysis is not supported.\n");
766 int show_available_vars(struct perf_probe_event
*pevs __maybe_unused
,
767 int npevs __maybe_unused
, int max_vls __maybe_unused
,
768 const char *module __maybe_unused
,
769 struct strfilter
*filter __maybe_unused
,
770 bool externs __maybe_unused
)
772 pr_warning("Debuginfo-analysis is not supported.\n");
777 void line_range__clear(struct line_range
*lr
)
779 struct line_node
*ln
;
785 while (!list_empty(&lr
->line_list
)) {
786 ln
= list_first_entry(&lr
->line_list
, struct line_node
, list
);
790 memset(lr
, 0, sizeof(*lr
));
793 void line_range__init(struct line_range
*lr
)
795 memset(lr
, 0, sizeof(*lr
));
796 INIT_LIST_HEAD(&lr
->line_list
);
799 static int parse_line_num(char **ptr
, int *val
, const char *what
)
801 const char *start
= *ptr
;
804 *val
= strtol(*ptr
, ptr
, 0);
805 if (errno
|| *ptr
== start
) {
806 semantic_error("'%s' is not a valid number.\n", what
);
813 * Stuff 'lr' according to the line range described by 'arg'.
814 * The line range syntax is described by:
816 * SRC[:SLN[+NUM|-ELN]]
817 * FNC[@SRC][:SLN[+NUM|-ELN]]
819 int parse_line_range_desc(const char *arg
, struct line_range
*lr
)
821 char *range
, *file
, *name
= strdup(arg
);
830 range
= strchr(name
, ':');
834 err
= parse_line_num(&range
, &lr
->start
, "start line");
838 if (*range
== '+' || *range
== '-') {
839 const char c
= *range
++;
841 err
= parse_line_num(&range
, &lr
->end
, "end line");
846 lr
->end
+= lr
->start
;
848 * Adjust the number of lines here.
849 * If the number of lines == 1, the
850 * the end of line should be equal to
857 pr_debug("Line range is %d to %d\n", lr
->start
, lr
->end
);
860 if (lr
->start
> lr
->end
) {
861 semantic_error("Start line must be smaller"
862 " than end line.\n");
865 if (*range
!= '\0') {
866 semantic_error("Tailing with invalid str '%s'.\n", range
);
871 file
= strchr(name
, '@');
874 lr
->file
= strdup(++file
);
875 if (lr
->file
== NULL
) {
880 } else if (strchr(name
, '.'))
891 /* Check the name is good for event/group */
892 static bool check_event_name(const char *name
)
894 if (!isalpha(*name
) && *name
!= '_')
896 while (*++name
!= '\0') {
897 if (!isalpha(*name
) && !isdigit(*name
) && *name
!= '_')
903 /* Parse probepoint definition. */
904 static int parse_perf_probe_point(char *arg
, struct perf_probe_event
*pev
)
906 struct perf_probe_point
*pp
= &pev
->point
;
911 * perf probe [EVENT=]SRC[:LN|;PTN]
912 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
914 * TODO:Group name support
917 ptr
= strpbrk(arg
, ";=@+%");
918 if (ptr
&& *ptr
== '=') { /* Event name */
921 if (strchr(arg
, ':')) {
922 semantic_error("Group name is not supported yet.\n");
925 if (!check_event_name(arg
)) {
926 semantic_error("%s is bad for event name -it must "
927 "follow C symbol-naming rule.\n", arg
);
930 pev
->event
= strdup(arg
);
931 if (pev
->event
== NULL
)
937 ptr
= strpbrk(arg
, ";:+@%");
947 /* Check arg is function or file and copy it */
948 if (strchr(tmp
, '.')) /* File */
953 /* Parse other options */
957 if (c
== ';') { /* Lazy pattern must be the last part */
958 pp
->lazy_line
= strdup(arg
);
959 if (pp
->lazy_line
== NULL
)
963 ptr
= strpbrk(arg
, ";:+@%");
969 case ':': /* Line number */
970 pp
->line
= strtoul(arg
, &tmp
, 0);
972 semantic_error("There is non-digit char"
973 " in line number.\n");
977 case '+': /* Byte offset from a symbol */
978 pp
->offset
= strtoul(arg
, &tmp
, 0);
980 semantic_error("There is non-digit character"
985 case '@': /* File name */
987 semantic_error("SRC@SRC is not allowed.\n");
990 pp
->file
= strdup(arg
);
991 if (pp
->file
== NULL
)
994 case '%': /* Probe places */
995 if (strcmp(arg
, "return") == 0) {
997 } else { /* Others not supported yet */
998 semantic_error("%%%s is not supported.\n", arg
);
1002 default: /* Buggy case */
1003 pr_err("This program has a bug at %s:%d.\n",
1004 __FILE__
, __LINE__
);
1010 /* Exclusion check */
1011 if (pp
->lazy_line
&& pp
->line
) {
1012 semantic_error("Lazy pattern can't be used with"
1017 if (pp
->lazy_line
&& pp
->offset
) {
1018 semantic_error("Lazy pattern can't be used with offset.\n");
1022 if (pp
->line
&& pp
->offset
) {
1023 semantic_error("Offset can't be used with line number.\n");
1027 if (!pp
->line
&& !pp
->lazy_line
&& pp
->file
&& !pp
->function
) {
1028 semantic_error("File always requires line number or "
1033 if (pp
->offset
&& !pp
->function
) {
1034 semantic_error("Offset requires an entry function.\n");
1038 if (pp
->retprobe
&& !pp
->function
) {
1039 semantic_error("Return probe requires an entry function.\n");
1043 if ((pp
->offset
|| pp
->line
|| pp
->lazy_line
) && pp
->retprobe
) {
1044 semantic_error("Offset/Line/Lazy pattern can't be used with "
1049 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
1050 pp
->function
, pp
->file
, pp
->line
, pp
->offset
, pp
->retprobe
,
1055 /* Parse perf-probe event argument */
1056 static int parse_perf_probe_arg(char *str
, struct perf_probe_arg
*arg
)
1058 char *tmp
, *goodname
;
1059 struct perf_probe_arg_field
**fieldp
;
1061 pr_debug("parsing arg: %s into ", str
);
1063 tmp
= strchr(str
, '=');
1065 arg
->name
= strndup(str
, tmp
- str
);
1066 if (arg
->name
== NULL
)
1068 pr_debug("name:%s ", arg
->name
);
1072 tmp
= strchr(str
, ':');
1073 if (tmp
) { /* Type setting */
1075 arg
->type
= strdup(tmp
+ 1);
1076 if (arg
->type
== NULL
)
1078 pr_debug("type:%s ", arg
->type
);
1081 tmp
= strpbrk(str
, "-.[");
1082 if (!is_c_varname(str
) || !tmp
) {
1083 /* A variable, register, symbol or special value */
1084 arg
->var
= strdup(str
);
1085 if (arg
->var
== NULL
)
1087 pr_debug("%s\n", arg
->var
);
1091 /* Structure fields or array element */
1092 arg
->var
= strndup(str
, tmp
- str
);
1093 if (arg
->var
== NULL
)
1095 goodname
= arg
->var
;
1096 pr_debug("%s, ", arg
->var
);
1097 fieldp
= &arg
->field
;
1100 *fieldp
= zalloc(sizeof(struct perf_probe_arg_field
));
1101 if (*fieldp
== NULL
)
1103 if (*tmp
== '[') { /* Array */
1105 (*fieldp
)->index
= strtol(str
+ 1, &tmp
, 0);
1106 (*fieldp
)->ref
= true;
1107 if (*tmp
!= ']' || tmp
== str
+ 1) {
1108 semantic_error("Array index must be a"
1115 } else { /* Structure */
1118 (*fieldp
)->ref
= false;
1119 } else if (tmp
[1] == '>') {
1121 (*fieldp
)->ref
= true;
1123 semantic_error("Argument parse error: %s\n",
1127 tmp
= strpbrk(str
, "-.[");
1130 (*fieldp
)->name
= strndup(str
, tmp
- str
);
1131 if ((*fieldp
)->name
== NULL
)
1134 goodname
= (*fieldp
)->name
;
1135 pr_debug("%s(%d), ", (*fieldp
)->name
, (*fieldp
)->ref
);
1136 fieldp
= &(*fieldp
)->next
;
1139 (*fieldp
)->name
= strdup(str
);
1140 if ((*fieldp
)->name
== NULL
)
1143 goodname
= (*fieldp
)->name
;
1144 pr_debug("%s(%d)\n", (*fieldp
)->name
, (*fieldp
)->ref
);
1146 /* If no name is specified, set the last field name (not array index)*/
1148 arg
->name
= strdup(goodname
);
1149 if (arg
->name
== NULL
)
1155 /* Parse perf-probe event command */
1156 int parse_perf_probe_command(const char *cmd
, struct perf_probe_event
*pev
)
1159 int argc
, i
, ret
= 0;
1161 argv
= argv_split(cmd
, &argc
);
1163 pr_debug("Failed to split arguments.\n");
1166 if (argc
- 1 > MAX_PROBE_ARGS
) {
1167 semantic_error("Too many probe arguments (%d).\n", argc
- 1);
1171 /* Parse probe point */
1172 ret
= parse_perf_probe_point(argv
[0], pev
);
1176 /* Copy arguments and ensure return probe has no C argument */
1177 pev
->nargs
= argc
- 1;
1178 pev
->args
= zalloc(sizeof(struct perf_probe_arg
) * pev
->nargs
);
1179 if (pev
->args
== NULL
) {
1183 for (i
= 0; i
< pev
->nargs
&& ret
>= 0; i
++) {
1184 ret
= parse_perf_probe_arg(argv
[i
+ 1], &pev
->args
[i
]);
1186 is_c_varname(pev
->args
[i
].var
) && pev
->point
.retprobe
) {
1187 semantic_error("You can't specify local variable for"
1198 /* Return true if this perf_probe_event requires debuginfo */
1199 bool perf_probe_event_need_dwarf(struct perf_probe_event
*pev
)
1203 if (pev
->point
.file
|| pev
->point
.line
|| pev
->point
.lazy_line
)
1206 for (i
= 0; i
< pev
->nargs
; i
++)
1207 if (is_c_varname(pev
->args
[i
].var
))
1213 /* Parse probe_events event into struct probe_point */
1214 static int parse_probe_trace_command(const char *cmd
,
1215 struct probe_trace_event
*tev
)
1217 struct probe_trace_point
*tp
= &tev
->point
;
1220 char *argv0_str
= NULL
, *fmt
, *fmt1_str
, *fmt2_str
, *fmt3_str
;
1224 pr_debug("Parsing probe_events: %s\n", cmd
);
1225 argv
= argv_split(cmd
, &argc
);
1227 pr_debug("Failed to split arguments.\n");
1231 semantic_error("Too few probe arguments.\n");
1236 /* Scan event and group name. */
1237 argv0_str
= strdup(argv
[0]);
1238 if (argv0_str
== NULL
) {
1242 fmt1_str
= strtok_r(argv0_str
, ":", &fmt
);
1243 fmt2_str
= strtok_r(NULL
, "/", &fmt
);
1244 fmt3_str
= strtok_r(NULL
, " \t", &fmt
);
1245 if (fmt1_str
== NULL
|| strlen(fmt1_str
) != 1 || fmt2_str
== NULL
1246 || fmt3_str
== NULL
) {
1247 semantic_error("Failed to parse event name: %s\n", argv
[0]);
1252 tev
->group
= strdup(fmt2_str
);
1253 tev
->event
= strdup(fmt3_str
);
1254 if (tev
->group
== NULL
|| tev
->event
== NULL
) {
1258 pr_debug("Group:%s Event:%s probe:%c\n", tev
->group
, tev
->event
, pr
);
1260 tp
->retprobe
= (pr
== 'r');
1262 /* Scan module name(if there), function name and offset */
1263 p
= strchr(argv
[1], ':');
1265 tp
->module
= strndup(argv
[1], p
- argv
[1]);
1269 fmt1_str
= strtok_r(p
, "+", &fmt
);
1270 tp
->symbol
= strdup(fmt1_str
);
1271 if (tp
->symbol
== NULL
) {
1275 fmt2_str
= strtok_r(NULL
, "", &fmt
);
1276 if (fmt2_str
== NULL
)
1279 tp
->offset
= strtoul(fmt2_str
, NULL
, 10);
1281 tev
->nargs
= argc
- 2;
1282 tev
->args
= zalloc(sizeof(struct probe_trace_arg
) * tev
->nargs
);
1283 if (tev
->args
== NULL
) {
1287 for (i
= 0; i
< tev
->nargs
; i
++) {
1288 p
= strchr(argv
[i
+ 2], '=');
1289 if (p
) /* We don't need which register is assigned. */
1293 tev
->args
[i
].name
= strdup(argv
[i
+ 2]);
1294 /* TODO: parse regs and offset */
1295 tev
->args
[i
].value
= strdup(p
);
1296 if (tev
->args
[i
].name
== NULL
|| tev
->args
[i
].value
== NULL
) {
1308 /* Compose only probe arg */
1309 int synthesize_perf_probe_arg(struct perf_probe_arg
*pa
, char *buf
, size_t len
)
1311 struct perf_probe_arg_field
*field
= pa
->field
;
1315 if (pa
->name
&& pa
->var
)
1316 ret
= e_snprintf(tmp
, len
, "%s=%s", pa
->name
, pa
->var
);
1318 ret
= e_snprintf(tmp
, len
, "%s", pa
->name
? pa
->name
: pa
->var
);
1325 if (field
->name
[0] == '[')
1326 ret
= e_snprintf(tmp
, len
, "%s", field
->name
);
1328 ret
= e_snprintf(tmp
, len
, "%s%s",
1329 field
->ref
? "->" : ".", field
->name
);
1334 field
= field
->next
;
1338 ret
= e_snprintf(tmp
, len
, ":%s", pa
->type
);
1347 pr_debug("Failed to synthesize perf probe argument: %s\n",
1352 /* Compose only probe point (not argument) */
1353 static char *synthesize_perf_probe_point(struct perf_probe_point
*pp
)
1356 char offs
[32] = "", line
[32] = "", file
[32] = "";
1359 buf
= zalloc(MAX_CMDLEN
);
1365 ret
= e_snprintf(offs
, 32, "+%lu", pp
->offset
);
1370 ret
= e_snprintf(line
, 32, ":%d", pp
->line
);
1378 tmp
= strchr(pp
->file
+ len
- 30, '/');
1379 tmp
= tmp
? tmp
+ 1 : pp
->file
+ len
- 30;
1381 ret
= e_snprintf(file
, 32, "@%s", tmp
);
1387 ret
= e_snprintf(buf
, MAX_CMDLEN
, "%s%s%s%s%s", pp
->function
,
1388 offs
, pp
->retprobe
? "%return" : "", line
,
1391 ret
= e_snprintf(buf
, MAX_CMDLEN
, "%s%s", file
, line
);
1397 pr_debug("Failed to synthesize perf probe point: %s\n",
1404 char *synthesize_perf_probe_command(struct perf_probe_event
*pev
)
1409 buf
= synthesize_perf_probe_point(&pev
->point
);
1414 for (i
= 0; i
< pev
->nargs
; i
++) {
1415 ret
= e_snprintf(&buf
[len
], MAX_CMDLEN
- len
, " %s",
1428 static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref
*ref
,
1429 char **buf
, size_t *buflen
,
1434 depth
= __synthesize_probe_trace_arg_ref(ref
->next
, buf
,
1440 ret
= e_snprintf(*buf
, *buflen
, "%+ld(", ref
->offset
);
1452 static int synthesize_probe_trace_arg(struct probe_trace_arg
*arg
,
1453 char *buf
, size_t buflen
)
1455 struct probe_trace_arg_ref
*ref
= arg
->ref
;
1459 /* Argument name or separator */
1461 ret
= e_snprintf(buf
, buflen
, " %s=", arg
->name
);
1463 ret
= e_snprintf(buf
, buflen
, " ");
1469 /* Special case: @XXX */
1470 if (arg
->value
[0] == '@' && arg
->ref
)
1473 /* Dereferencing arguments */
1475 depth
= __synthesize_probe_trace_arg_ref(ref
, &buf
,
1481 /* Print argument value */
1482 if (arg
->value
[0] == '@' && arg
->ref
)
1483 ret
= e_snprintf(buf
, buflen
, "%s%+ld", arg
->value
,
1486 ret
= e_snprintf(buf
, buflen
, "%s", arg
->value
);
1494 ret
= e_snprintf(buf
, buflen
, ")");
1500 /* Print argument type */
1502 ret
= e_snprintf(buf
, buflen
, ":%s", arg
->type
);
1511 char *synthesize_probe_trace_command(struct probe_trace_event
*tev
)
1513 struct probe_trace_point
*tp
= &tev
->point
;
1517 buf
= zalloc(MAX_CMDLEN
);
1522 len
= e_snprintf(buf
, MAX_CMDLEN
, "%c:%s/%s %s:%s",
1523 tp
->retprobe
? 'r' : 'p',
1524 tev
->group
, tev
->event
,
1525 tp
->module
, tp
->symbol
);
1527 len
= e_snprintf(buf
, MAX_CMDLEN
, "%c:%s/%s %s%s%s+%lu",
1528 tp
->retprobe
? 'r' : 'p',
1529 tev
->group
, tev
->event
,
1530 tp
->module
?: "", tp
->module
? ":" : "",
1531 tp
->symbol
, tp
->offset
);
1536 for (i
= 0; i
< tev
->nargs
; i
++) {
1537 ret
= synthesize_probe_trace_arg(&tev
->args
[i
], buf
+ len
,
1550 static int convert_to_perf_probe_event(struct probe_trace_event
*tev
,
1551 struct perf_probe_event
*pev
, bool is_kprobe
)
1556 /* Convert event/group name */
1557 pev
->event
= strdup(tev
->event
);
1558 pev
->group
= strdup(tev
->group
);
1559 if (pev
->event
== NULL
|| pev
->group
== NULL
)
1562 /* Convert trace_point to probe_point */
1564 ret
= kprobe_convert_to_perf_probe(&tev
->point
, &pev
->point
);
1566 ret
= convert_to_perf_probe_point(&tev
->point
, &pev
->point
);
1571 /* Convert trace_arg to probe_arg */
1572 pev
->nargs
= tev
->nargs
;
1573 pev
->args
= zalloc(sizeof(struct perf_probe_arg
) * pev
->nargs
);
1574 if (pev
->args
== NULL
)
1576 for (i
= 0; i
< tev
->nargs
&& ret
>= 0; i
++) {
1577 if (tev
->args
[i
].name
)
1578 pev
->args
[i
].name
= strdup(tev
->args
[i
].name
);
1580 ret
= synthesize_probe_trace_arg(&tev
->args
[i
],
1582 pev
->args
[i
].name
= strdup(buf
);
1584 if (pev
->args
[i
].name
== NULL
&& ret
>= 0)
1589 clear_perf_probe_event(pev
);
1594 void clear_perf_probe_event(struct perf_probe_event
*pev
)
1596 struct perf_probe_point
*pp
= &pev
->point
;
1597 struct perf_probe_arg_field
*field
, *next
;
1604 free(pp
->lazy_line
);
1606 for (i
= 0; i
< pev
->nargs
; i
++) {
1607 free(pev
->args
[i
].name
);
1608 free(pev
->args
[i
].var
);
1609 free(pev
->args
[i
].type
);
1610 field
= pev
->args
[i
].field
;
1613 zfree(&field
->name
);
1619 memset(pev
, 0, sizeof(*pev
));
1622 static void clear_probe_trace_event(struct probe_trace_event
*tev
)
1624 struct probe_trace_arg_ref
*ref
, *next
;
1629 free(tev
->point
.symbol
);
1630 free(tev
->point
.module
);
1631 for (i
= 0; i
< tev
->nargs
; i
++) {
1632 free(tev
->args
[i
].name
);
1633 free(tev
->args
[i
].value
);
1634 free(tev
->args
[i
].type
);
1635 ref
= tev
->args
[i
].ref
;
1643 memset(tev
, 0, sizeof(*tev
));
1646 static void print_warn_msg(const char *file
, bool is_kprobe
)
1649 if (errno
== ENOENT
) {
1653 config
= "CONFIG_UPROBE_EVENTS";
1655 config
= "CONFIG_KPROBE_EVENTS";
1657 pr_warning("%s file does not exist - please rebuild kernel"
1658 " with %s.\n", file
, config
);
1660 pr_warning("Failed to open %s file: %s\n", file
,
1664 static int open_probe_events(const char *trace_file
, bool readwrite
,
1668 const char *__debugfs
;
1671 __debugfs
= debugfs_find_mountpoint();
1672 if (__debugfs
== NULL
) {
1673 pr_warning("Debugfs is not mounted.\n");
1677 ret
= e_snprintf(buf
, PATH_MAX
, "%s/%s", __debugfs
, trace_file
);
1679 pr_debug("Opening %s write=%d\n", buf
, readwrite
);
1680 if (readwrite
&& !probe_event_dry_run
)
1681 ret
= open(buf
, O_RDWR
, O_APPEND
);
1683 ret
= open(buf
, O_RDONLY
, 0);
1686 print_warn_msg(buf
, is_kprobe
);
1691 static int open_kprobe_events(bool readwrite
)
1693 return open_probe_events("tracing/kprobe_events", readwrite
, true);
1696 static int open_uprobe_events(bool readwrite
)
1698 return open_probe_events("tracing/uprobe_events", readwrite
, false);
1701 /* Get raw string list of current kprobe_events or uprobe_events */
1702 static struct strlist
*get_probe_trace_command_rawlist(int fd
)
1706 char buf
[MAX_CMDLEN
];
1710 sl
= strlist__new(true, NULL
);
1712 fp
= fdopen(dup(fd
), "r");
1714 p
= fgets(buf
, MAX_CMDLEN
, fp
);
1718 idx
= strlen(p
) - 1;
1721 ret
= strlist__add(sl
, buf
);
1723 pr_debug("strlist__add failed: %s\n", strerror(-ret
));
1724 strlist__delete(sl
);
1734 static int show_perf_probe_event(struct perf_probe_event
*pev
)
1740 /* Synthesize only event probe point */
1741 place
= synthesize_perf_probe_point(&pev
->point
);
1745 ret
= e_snprintf(buf
, 128, "%s:%s", pev
->group
, pev
->event
);
1749 printf(" %-20s (on %s", buf
, place
);
1751 if (pev
->nargs
> 0) {
1753 for (i
= 0; i
< pev
->nargs
; i
++) {
1754 ret
= synthesize_perf_probe_arg(&pev
->args
[i
],
1766 static int __show_perf_probe_events(int fd
, bool is_kprobe
)
1769 struct probe_trace_event tev
;
1770 struct perf_probe_event pev
;
1771 struct strlist
*rawlist
;
1772 struct str_node
*ent
;
1774 memset(&tev
, 0, sizeof(tev
));
1775 memset(&pev
, 0, sizeof(pev
));
1777 rawlist
= get_probe_trace_command_rawlist(fd
);
1781 strlist__for_each(ent
, rawlist
) {
1782 ret
= parse_probe_trace_command(ent
->s
, &tev
);
1784 ret
= convert_to_perf_probe_event(&tev
, &pev
,
1787 ret
= show_perf_probe_event(&pev
);
1789 clear_perf_probe_event(&pev
);
1790 clear_probe_trace_event(&tev
);
1794 strlist__delete(rawlist
);
1799 /* List up current perf-probe events */
1800 int show_perf_probe_events(void)
1805 fd
= open_kprobe_events(false);
1810 ret
= init_vmlinux();
1814 ret
= __show_perf_probe_events(fd
, true);
1817 fd
= open_uprobe_events(false);
1819 ret
= __show_perf_probe_events(fd
, false);
1826 /* Get current perf-probe event names */
1827 static struct strlist
*get_probe_trace_event_names(int fd
, bool include_group
)
1830 struct strlist
*sl
, *rawlist
;
1831 struct str_node
*ent
;
1832 struct probe_trace_event tev
;
1835 memset(&tev
, 0, sizeof(tev
));
1836 rawlist
= get_probe_trace_command_rawlist(fd
);
1837 sl
= strlist__new(true, NULL
);
1838 strlist__for_each(ent
, rawlist
) {
1839 ret
= parse_probe_trace_command(ent
->s
, &tev
);
1842 if (include_group
) {
1843 ret
= e_snprintf(buf
, 128, "%s:%s", tev
.group
,
1846 ret
= strlist__add(sl
, buf
);
1848 ret
= strlist__add(sl
, tev
.event
);
1849 clear_probe_trace_event(&tev
);
1853 strlist__delete(rawlist
);
1856 strlist__delete(sl
);
1862 static int write_probe_trace_event(int fd
, struct probe_trace_event
*tev
)
1865 char *buf
= synthesize_probe_trace_command(tev
);
1868 pr_debug("Failed to synthesize probe trace event.\n");
1872 pr_debug("Writing event: %s\n", buf
);
1873 if (!probe_event_dry_run
) {
1874 ret
= write(fd
, buf
, strlen(buf
));
1876 pr_warning("Failed to write event: %s\n",
1883 static int get_new_event_name(char *buf
, size_t len
, const char *base
,
1884 struct strlist
*namelist
, bool allow_suffix
)
1889 ret
= e_snprintf(buf
, len
, "%s", base
);
1891 pr_debug("snprintf() failed: %s\n", strerror(-ret
));
1894 if (!strlist__has_entry(namelist
, buf
))
1897 if (!allow_suffix
) {
1898 pr_warning("Error: event \"%s\" already exists. "
1899 "(Use -f to force duplicates.)\n", base
);
1903 /* Try to add suffix */
1904 for (i
= 1; i
< MAX_EVENT_INDEX
; i
++) {
1905 ret
= e_snprintf(buf
, len
, "%s_%d", base
, i
);
1907 pr_debug("snprintf() failed: %s\n", strerror(-ret
));
1910 if (!strlist__has_entry(namelist
, buf
))
1913 if (i
== MAX_EVENT_INDEX
) {
1914 pr_warning("Too many events are on the same function.\n");
1921 static int __add_probe_trace_events(struct perf_probe_event
*pev
,
1922 struct probe_trace_event
*tevs
,
1923 int ntevs
, bool allow_suffix
)
1926 struct probe_trace_event
*tev
= NULL
;
1928 const char *event
, *group
;
1929 struct strlist
*namelist
;
1932 fd
= open_uprobe_events(true);
1934 fd
= open_kprobe_events(true);
1938 /* Get current event names */
1939 namelist
= get_probe_trace_event_names(fd
, false);
1941 pr_debug("Failed to get current event list.\n");
1946 printf("Added new event%s\n", (ntevs
> 1) ? "s:" : ":");
1947 for (i
= 0; i
< ntevs
; i
++) {
1952 if (pev
->point
.function
)
1953 event
= pev
->point
.function
;
1955 event
= tev
->point
.symbol
;
1959 group
= PERFPROBE_GROUP
;
1961 /* Get an unused new event name */
1962 ret
= get_new_event_name(buf
, 64, event
,
1963 namelist
, allow_suffix
);
1968 tev
->event
= strdup(event
);
1969 tev
->group
= strdup(group
);
1970 if (tev
->event
== NULL
|| tev
->group
== NULL
) {
1974 ret
= write_probe_trace_event(fd
, tev
);
1977 /* Add added event name to namelist */
1978 strlist__add(namelist
, event
);
1980 /* Trick here - save current event/group */
1983 pev
->event
= tev
->event
;
1984 pev
->group
= tev
->group
;
1985 show_perf_probe_event(pev
);
1986 /* Trick here - restore current event/group */
1987 pev
->event
= (char *)event
;
1988 pev
->group
= (char *)group
;
1991 * Probes after the first probe which comes from same
1992 * user input are always allowed to add suffix, because
1993 * there might be several addresses corresponding to
1996 allow_suffix
= true;
2000 /* Show how to use the event. */
2001 printf("\nYou can now use it in all perf tools, such as:\n\n");
2002 printf("\tperf record -e %s:%s -aR sleep 1\n\n", tev
->group
,
2006 strlist__delete(namelist
);
2011 static int convert_to_probe_trace_events(struct perf_probe_event
*pev
,
2012 struct probe_trace_event
**tevs
,
2013 int max_tevs
, const char *target
)
2017 struct probe_trace_event
*tev
;
2019 if (pev
->uprobes
&& !pev
->group
) {
2020 /* Replace group name if not given */
2021 ret
= convert_exec_to_group(target
, &pev
->group
);
2023 pr_warning("Failed to make a group name.\n");
2028 /* Convert perf_probe_event with debuginfo */
2029 ret
= try_to_find_probe_trace_events(pev
, tevs
, max_tevs
, target
);
2031 return ret
; /* Found in debuginfo or got an error */
2034 ret
= convert_name_to_addr(pev
, target
);
2039 /* Allocate trace event buffer */
2040 tev
= *tevs
= zalloc(sizeof(struct probe_trace_event
));
2044 /* Copy parameters */
2045 tev
->point
.symbol
= strdup(pev
->point
.function
);
2046 if (tev
->point
.symbol
== NULL
) {
2052 tev
->point
.module
= strdup(target
);
2053 if (tev
->point
.module
== NULL
) {
2059 tev
->point
.offset
= pev
->point
.offset
;
2060 tev
->point
.retprobe
= pev
->point
.retprobe
;
2061 tev
->nargs
= pev
->nargs
;
2062 tev
->uprobes
= pev
->uprobes
;
2065 tev
->args
= zalloc(sizeof(struct probe_trace_arg
)
2067 if (tev
->args
== NULL
) {
2071 for (i
= 0; i
< tev
->nargs
; i
++) {
2072 if (pev
->args
[i
].name
) {
2073 tev
->args
[i
].name
= strdup(pev
->args
[i
].name
);
2074 if (tev
->args
[i
].name
== NULL
) {
2079 tev
->args
[i
].value
= strdup(pev
->args
[i
].var
);
2080 if (tev
->args
[i
].value
== NULL
) {
2084 if (pev
->args
[i
].type
) {
2085 tev
->args
[i
].type
= strdup(pev
->args
[i
].type
);
2086 if (tev
->args
[i
].type
== NULL
) {
2097 /* Currently just checking function name from symbol map */
2098 sym
= __find_kernel_function_by_name(tev
->point
.symbol
, NULL
);
2100 pr_warning("Kernel symbol \'%s\' not found.\n",
2104 } else if (tev
->point
.offset
> sym
->end
- sym
->start
) {
2105 pr_warning("Offset specified is greater than size of %s\n",
2114 clear_probe_trace_event(tev
);
2120 struct __event_package
{
2121 struct perf_probe_event
*pev
;
2122 struct probe_trace_event
*tevs
;
2126 int add_perf_probe_events(struct perf_probe_event
*pevs
, int npevs
,
2127 int max_tevs
, const char *target
, bool force_add
)
2130 struct __event_package
*pkgs
;
2133 pkgs
= zalloc(sizeof(struct __event_package
) * npevs
);
2139 /* Init vmlinux path */
2140 ret
= init_vmlinux();
2142 ret
= init_user_exec();
2149 /* Loop 1: convert all events */
2150 for (i
= 0; i
< npevs
; i
++) {
2151 pkgs
[i
].pev
= &pevs
[i
];
2152 /* Convert with or without debuginfo */
2153 ret
= convert_to_probe_trace_events(pkgs
[i
].pev
,
2159 pkgs
[i
].ntevs
= ret
;
2162 /* Loop 2: add all events */
2163 for (i
= 0; i
< npevs
; i
++) {
2164 ret
= __add_probe_trace_events(pkgs
[i
].pev
, pkgs
[i
].tevs
,
2165 pkgs
[i
].ntevs
, force_add
);
2170 /* Loop 3: cleanup and free trace events */
2171 for (i
= 0; i
< npevs
; i
++) {
2172 for (j
= 0; j
< pkgs
[i
].ntevs
; j
++)
2173 clear_probe_trace_event(&pkgs
[i
].tevs
[j
]);
2174 zfree(&pkgs
[i
].tevs
);
2181 static int __del_trace_probe_event(int fd
, struct str_node
*ent
)
2187 /* Convert from perf-probe event to trace-probe event */
2188 ret
= e_snprintf(buf
, 128, "-:%s", ent
->s
);
2192 p
= strchr(buf
+ 2, ':');
2194 pr_debug("Internal error: %s should have ':' but not.\n",
2201 pr_debug("Writing event: %s\n", buf
);
2202 ret
= write(fd
, buf
, strlen(buf
));
2208 printf("Removed event: %s\n", ent
->s
);
2211 pr_warning("Failed to delete event: %s\n", strerror(-ret
));
2215 static int del_trace_probe_event(int fd
, const char *buf
,
2216 struct strlist
*namelist
)
2218 struct str_node
*ent
, *n
;
2221 if (strpbrk(buf
, "*?")) { /* Glob-exp */
2222 strlist__for_each_safe(ent
, n
, namelist
)
2223 if (strglobmatch(ent
->s
, buf
)) {
2224 ret
= __del_trace_probe_event(fd
, ent
);
2227 strlist__remove(namelist
, ent
);
2230 ent
= strlist__find(namelist
, buf
);
2232 ret
= __del_trace_probe_event(fd
, ent
);
2234 strlist__remove(namelist
, ent
);
2241 int del_perf_probe_events(struct strlist
*dellist
)
2243 int ret
= -1, ufd
= -1, kfd
= -1;
2245 const char *group
, *event
;
2247 struct str_node
*ent
;
2248 struct strlist
*namelist
= NULL
, *unamelist
= NULL
;
2250 /* Get current event names */
2251 kfd
= open_kprobe_events(true);
2255 namelist
= get_probe_trace_event_names(kfd
, true);
2256 ufd
= open_uprobe_events(true);
2259 unamelist
= get_probe_trace_event_names(ufd
, true);
2261 if (namelist
== NULL
&& unamelist
== NULL
)
2264 strlist__for_each(ent
, dellist
) {
2265 str
= strdup(ent
->s
);
2270 pr_debug("Parsing: %s\n", str
);
2271 p
= strchr(str
, ':');
2281 ret
= e_snprintf(buf
, 128, "%s:%s", group
, event
);
2283 pr_err("Failed to copy event.");
2288 pr_debug("Group: %s, Event: %s\n", group
, event
);
2291 ret
= del_trace_probe_event(kfd
, buf
, namelist
);
2293 if (unamelist
&& ret
!= 0)
2294 ret
= del_trace_probe_event(ufd
, buf
, unamelist
);
2297 pr_info("Info: Event \"%s\" does not exist.\n", buf
);
2304 strlist__delete(namelist
);
2309 strlist__delete(unamelist
);
2316 /* TODO: don't use a global variable for filter ... */
2317 static struct strfilter
*available_func_filter
;
2320 * If a symbol corresponds to a function with global binding and
2321 * matches filter return 0. For all others return 1.
2323 static int filter_available_functions(struct map
*map __maybe_unused
,
2326 if (sym
->binding
== STB_GLOBAL
&&
2327 strfilter__compare(available_func_filter
, sym
->name
))
2332 static int __show_available_funcs(struct map
*map
)
2334 if (map__load(map
, filter_available_functions
)) {
2335 pr_err("Failed to load map.\n");
2338 if (!dso__sorted_by_name(map
->dso
, map
->type
))
2339 dso__sort_by_name(map
->dso
, map
->type
);
2341 dso__fprintf_symbols_by_name(map
->dso
, map
->type
, stdout
);
2345 static int available_kernel_funcs(const char *module
)
2350 ret
= init_vmlinux();
2354 map
= kernel_get_module_map(module
);
2356 pr_err("Failed to find %s map.\n", (module
) ? : "kernel");
2359 return __show_available_funcs(map
);
2362 static int available_user_funcs(const char *target
)
2367 ret
= init_user_exec();
2371 map
= dso__new_map(target
);
2372 ret
= __show_available_funcs(map
);
2373 dso__delete(map
->dso
);
2378 int show_available_funcs(const char *target
, struct strfilter
*_filter
,
2382 available_func_filter
= _filter
;
2385 return available_kernel_funcs(target
);
2387 return available_user_funcs(target
);
2391 * uprobe_events only accepts address:
2392 * Convert function and any offset to address
2394 static int convert_name_to_addr(struct perf_probe_event
*pev
, const char *exec
)
2396 struct perf_probe_point
*pp
= &pev
->point
;
2398 struct map
*map
= NULL
;
2399 char *function
= NULL
;
2401 unsigned long long vaddr
= 0;
2403 if (!pp
->function
) {
2404 pr_warning("No function specified for uprobes");
2408 function
= strdup(pp
->function
);
2410 pr_warning("Failed to allocate memory by strdup.\n");
2415 map
= dso__new_map(exec
);
2417 pr_warning("Cannot find appropriate DSO for %s.\n", exec
);
2420 available_func_filter
= strfilter__new(function
, NULL
);
2421 if (map__load(map
, filter_available_functions
)) {
2422 pr_err("Failed to load map.\n");
2426 sym
= map__find_symbol_by_name(map
, function
, NULL
);
2428 pr_warning("Cannot find %s in DSO %s\n", function
, exec
);
2432 if (map
->start
> sym
->start
)
2434 vaddr
+= sym
->start
+ pp
->offset
+ map
->pgoff
;
2438 pev
->event
= function
;
2442 char *ptr1
, *ptr2
, *exec_copy
;
2444 pev
->group
= zalloc(sizeof(char *) * 64);
2445 exec_copy
= strdup(exec
);
2448 pr_warning("Failed to copy exec string.\n");
2452 ptr1
= strdup(basename(exec_copy
));
2454 ptr2
= strpbrk(ptr1
, "-._");
2457 e_snprintf(pev
->group
, 64, "%s_%s", PERFPROBE_GROUP
,
2464 pp
->function
= zalloc(sizeof(char *) * MAX_PROBE_ARGS
);
2465 if (!pp
->function
) {
2467 pr_warning("Failed to allocate memory by zalloc.\n");
2470 e_snprintf(pp
->function
, MAX_PROBE_ARGS
, "0x%llx", vaddr
);
2475 dso__delete(map
->dso
);