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>
45 #include "trace-event.h" /* For __unused */
46 #include "probe-event.h"
47 #include "probe-finder.h"
49 #define MAX_CMDLEN 256
50 #define MAX_PROBE_ARGS 128
51 #define PERFPROBE_GROUP "probe"
53 bool probe_event_dry_run
; /* Dry run flag */
55 #define semantic_error(msg ...) pr_err("Semantic error :" msg)
57 /* If there is no space to write, returns -E2BIG. */
58 static int e_snprintf(char *str
, size_t size
, const char *format
, ...)
59 __attribute__((format(printf
, 3, 4)));
61 static int e_snprintf(char *str
, size_t size
, const char *format
, ...)
66 ret
= vsnprintf(str
, size
, format
, ap
);
73 static char *synthesize_perf_probe_point(struct perf_probe_point
*pp
);
74 static struct machine machine
;
76 /* Initialize symbol maps and path of vmlinux/modules */
77 static int init_vmlinux(void)
81 symbol_conf
.sort_by_name
= true;
82 if (symbol_conf
.vmlinux_name
== NULL
)
83 symbol_conf
.try_vmlinux_path
= true;
85 pr_debug("Use vmlinux: %s\n", symbol_conf
.vmlinux_name
);
88 pr_debug("Failed to init symbol map.\n");
92 ret
= machine__init(&machine
, "", HOST_KERNEL_ID
);
96 if (machine__create_kernel_maps(&machine
) < 0) {
97 pr_debug("machine__create_kernel_maps() failed.\n");
102 pr_warning("Failed to init vmlinux path.\n");
106 static struct symbol
*__find_kernel_function_by_name(const char *name
,
109 return machine__find_kernel_function_by_name(&machine
, name
, mapp
,
113 static struct map
*kernel_get_module_map(const char *module
)
116 struct map_groups
*grp
= &machine
.kmaps
;
118 /* A file path -- this is an offline module */
119 if (module
&& strchr(module
, '/'))
120 return machine__new_module(&machine
, 0, module
);
125 for (nd
= rb_first(&grp
->maps
[MAP__FUNCTION
]); nd
; nd
= rb_next(nd
)) {
126 struct map
*pos
= rb_entry(nd
, struct map
, rb_node
);
127 if (strncmp(pos
->dso
->short_name
+ 1, module
,
128 pos
->dso
->short_name_len
- 2) == 0) {
135 static struct dso
*kernel_get_module_dso(const char *module
)
139 const char *vmlinux_name
;
142 list_for_each_entry(dso
, &machine
.kernel_dsos
, node
) {
143 if (strncmp(dso
->short_name
+ 1, module
,
144 dso
->short_name_len
- 2) == 0)
147 pr_debug("Failed to find module %s.\n", module
);
151 map
= machine
.vmlinux_maps
[MAP__FUNCTION
];
154 vmlinux_name
= symbol_conf
.vmlinux_name
;
156 if (dso__load_vmlinux(dso
, map
, vmlinux_name
, NULL
) <= 0)
159 if (dso__load_vmlinux_path(dso
, map
, NULL
) <= 0) {
160 pr_debug("Failed to load kernel map.\n");
168 const char *kernel_get_module_path(const char *module
)
170 struct dso
*dso
= kernel_get_module_dso(module
);
171 return (dso
) ? dso
->long_name
: NULL
;
175 /* Open new debuginfo of given module */
176 static struct debuginfo
*open_debuginfo(const char *module
)
180 /* A file path -- this is an offline module */
181 if (module
&& strchr(module
, '/'))
184 path
= kernel_get_module_path(module
);
187 pr_err("Failed to find path of %s module.\n",
192 return debuginfo__new(path
);
196 * Convert trace point to probe point with debuginfo
197 * Currently only handles kprobes.
199 static int kprobe_convert_to_perf_probe(struct probe_trace_point
*tp
,
200 struct perf_probe_point
*pp
)
206 struct debuginfo
*dinfo
;
208 sym
= __find_kernel_function_by_name(tp
->symbol
, &map
);
210 addr
= map
->unmap_ip(map
, sym
->start
+ tp
->offset
);
211 pr_debug("try to find %s+%ld@%" PRIx64
"\n", tp
->symbol
,
214 dinfo
= debuginfo__new_online_kernel(addr
);
216 ret
= debuginfo__find_probe_point(dinfo
,
217 (unsigned long)addr
, pp
);
218 debuginfo__delete(dinfo
);
220 pr_debug("Failed to open debuginfo at 0x%" PRIx64
"\n",
226 pr_debug("Failed to find corresponding probes from "
227 "debuginfo. Use kprobe event information.\n");
228 pp
->function
= strdup(tp
->symbol
);
229 if (pp
->function
== NULL
)
231 pp
->offset
= tp
->offset
;
233 pp
->retprobe
= tp
->retprobe
;
238 static int add_module_to_probe_trace_events(struct probe_trace_event
*tevs
,
239 int ntevs
, const char *module
)
247 tmp
= strrchr(module
, '/');
249 /* This is a module path -- get the module name */
250 module
= strdup(tmp
+ 1);
253 tmp
= strchr(module
, '.');
256 tmp
= (char *)module
; /* For free() */
259 for (i
= 0; i
< ntevs
; i
++) {
260 tevs
[i
].point
.module
= strdup(module
);
261 if (!tevs
[i
].point
.module
) {
273 /* Try to find perf_probe_event with debuginfo */
274 static int try_to_find_probe_trace_events(struct perf_probe_event
*pev
,
275 struct probe_trace_event
**tevs
,
276 int max_tevs
, const char *module
)
278 bool need_dwarf
= perf_probe_event_need_dwarf(pev
);
279 struct debuginfo
*dinfo
= open_debuginfo(module
);
284 pr_warning("Failed to open debuginfo file.\n");
287 pr_debug("Could not open debuginfo. Try to use symbols.\n");
291 /* Searching trace events corresponding to a probe event */
292 ntevs
= debuginfo__find_trace_events(dinfo
, pev
, tevs
, max_tevs
);
294 debuginfo__delete(dinfo
);
296 if (ntevs
> 0) { /* Succeeded to find trace events */
297 pr_debug("find %d probe_trace_events.\n", ntevs
);
299 ret
= add_module_to_probe_trace_events(*tevs
, ntevs
,
301 return ret
< 0 ? ret
: ntevs
;
304 if (ntevs
== 0) { /* No error but failed to find probe point. */
305 pr_warning("Probe point '%s' not found.\n",
306 synthesize_perf_probe_point(&pev
->point
));
309 /* Error path : ntevs < 0 */
310 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs
);
311 if (ntevs
== -EBADF
) {
312 pr_warning("Warning: No dwarf info found in the vmlinux - "
313 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
315 pr_debug("Trying to use symbols.\n");
323 * Find a src file from a DWARF tag path. Prepend optional source path prefix
324 * and chop off leading directories that do not exist. Result is passed back as
325 * a newly allocated path on success.
326 * Return 0 if file was found and readable, -errno otherwise.
328 static int get_real_path(const char *raw_path
, const char *comp_dir
,
331 const char *prefix
= symbol_conf
.source_prefix
;
334 if (raw_path
[0] != '/' && comp_dir
)
335 /* If not an absolute path, try to use comp_dir */
338 if (access(raw_path
, R_OK
) == 0) {
339 *new_path
= strdup(raw_path
);
346 *new_path
= malloc((strlen(prefix
) + strlen(raw_path
) + 2));
351 sprintf(*new_path
, "%s/%s", prefix
, raw_path
);
353 if (access(*new_path
, R_OK
) == 0)
356 if (!symbol_conf
.source_prefix
)
357 /* In case of searching comp_dir, don't retry */
365 raw_path
= strchr(++raw_path
, '/');
381 #define LINEBUF_SIZE 256
382 #define NR_ADDITIONAL_LINES 2
384 static int __show_one_line(FILE *fp
, int l
, bool skip
, bool show_num
)
386 char buf
[LINEBUF_SIZE
];
387 const char *color
= show_num
? "" : PERF_COLOR_BLUE
;
388 const char *prefix
= NULL
;
391 if (fgets(buf
, LINEBUF_SIZE
, fp
) == NULL
)
396 prefix
= show_num
? "%7d " : " ";
397 color_fprintf(stdout
, color
, prefix
, l
);
399 color_fprintf(stdout
, color
, "%s", buf
);
401 } while (strchr(buf
, '\n') == NULL
);
406 pr_warning("File read error: %s\n", strerror(errno
));
412 static int _show_one_line(FILE *fp
, int l
, bool skip
, bool show_num
)
414 int rv
= __show_one_line(fp
, l
, skip
, show_num
);
416 pr_warning("Source file is shorter than expected.\n");
422 #define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
423 #define show_one_line(f,l) _show_one_line(f,l,false,false)
424 #define skip_one_line(f,l) _show_one_line(f,l,true,false)
425 #define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
428 * Show line-range always requires debuginfo to find source file and
431 int show_line_range(struct line_range
*lr
, const char *module
)
434 struct line_node
*ln
;
435 struct debuginfo
*dinfo
;
440 /* Search a line range */
441 ret
= init_vmlinux();
445 dinfo
= open_debuginfo(module
);
447 pr_warning("Failed to open debuginfo file.\n");
451 ret
= debuginfo__find_line_range(dinfo
, lr
);
452 debuginfo__delete(dinfo
);
454 pr_warning("Specified source line is not found.\n");
456 } else if (ret
< 0) {
457 pr_warning("Debuginfo analysis failed. (%d)\n", ret
);
461 /* Convert source file path */
463 ret
= get_real_path(tmp
, lr
->comp_dir
, &lr
->path
);
464 free(tmp
); /* Free old path */
466 pr_warning("Failed to find source file. (%d)\n", ret
);
473 fprintf(stdout
, "<%s@%s:%d>\n", lr
->function
, lr
->path
,
474 lr
->start
- lr
->offset
);
476 fprintf(stdout
, "<%s:%d>\n", lr
->path
, lr
->start
);
478 fp
= fopen(lr
->path
, "r");
480 pr_warning("Failed to open %s: %s\n", lr
->path
,
484 /* Skip to starting line number */
485 while (l
< lr
->start
) {
486 ret
= skip_one_line(fp
, l
++);
491 list_for_each_entry(ln
, &lr
->line_list
, list
) {
492 for (; ln
->line
> l
; l
++) {
493 ret
= show_one_line(fp
, l
- lr
->offset
);
497 ret
= show_one_line_with_num(fp
, l
++ - lr
->offset
);
502 if (lr
->end
== INT_MAX
)
503 lr
->end
= l
+ NR_ADDITIONAL_LINES
;
504 while (l
<= lr
->end
) {
505 ret
= show_one_line_or_eof(fp
, l
++ - lr
->offset
);
514 static int show_available_vars_at(struct debuginfo
*dinfo
,
515 struct perf_probe_event
*pev
,
516 int max_vls
, struct strfilter
*_filter
,
521 struct str_node
*node
;
522 struct variable_list
*vls
= NULL
, *vl
;
525 buf
= synthesize_perf_probe_point(&pev
->point
);
528 pr_debug("Searching variables at %s\n", buf
);
530 ret
= debuginfo__find_available_vars_at(dinfo
, pev
, &vls
,
533 pr_err("Failed to find variables at %s (%d)\n", buf
, ret
);
536 /* Some variables are found */
537 fprintf(stdout
, "Available variables at %s\n", buf
);
538 for (i
= 0; i
< ret
; i
++) {
541 * A probe point might be converted to
542 * several trace points.
544 fprintf(stdout
, "\t@<%s+%lu>\n", vl
->point
.symbol
,
546 free(vl
->point
.symbol
);
549 strlist__for_each(node
, vl
->vars
) {
550 var
= strchr(node
->s
, '\t') + 1;
551 if (strfilter__compare(_filter
, var
)) {
552 fprintf(stdout
, "\t\t%s\n", node
->s
);
556 strlist__delete(vl
->vars
);
559 fprintf(stdout
, "\t\t(No matched variables)\n");
567 /* Show available variables on given probe point */
568 int show_available_vars(struct perf_probe_event
*pevs
, int npevs
,
569 int max_vls
, const char *module
,
570 struct strfilter
*_filter
, bool externs
)
573 struct debuginfo
*dinfo
;
575 ret
= init_vmlinux();
579 dinfo
= open_debuginfo(module
);
581 pr_warning("Failed to open debuginfo file.\n");
587 for (i
= 0; i
< npevs
&& ret
>= 0; i
++)
588 ret
= show_available_vars_at(dinfo
, &pevs
[i
], max_vls
, _filter
,
591 debuginfo__delete(dinfo
);
595 #else /* !DWARF_SUPPORT */
597 static int kprobe_convert_to_perf_probe(struct probe_trace_point
*tp
,
598 struct perf_probe_point
*pp
)
602 sym
= __find_kernel_function_by_name(tp
->symbol
, NULL
);
604 pr_err("Failed to find symbol %s in kernel.\n", tp
->symbol
);
607 pp
->function
= strdup(tp
->symbol
);
608 if (pp
->function
== NULL
)
610 pp
->offset
= tp
->offset
;
611 pp
->retprobe
= tp
->retprobe
;
616 static int try_to_find_probe_trace_events(struct perf_probe_event
*pev
,
617 struct probe_trace_event
**tevs __unused
,
618 int max_tevs __unused
, const char *mod __unused
)
620 if (perf_probe_event_need_dwarf(pev
)) {
621 pr_warning("Debuginfo-analysis is not supported.\n");
627 int show_line_range(struct line_range
*lr __unused
, const char *module __unused
)
629 pr_warning("Debuginfo-analysis is not supported.\n");
633 int show_available_vars(struct perf_probe_event
*pevs __unused
,
634 int npevs __unused
, int max_vls __unused
,
635 const char *module __unused
,
636 struct strfilter
*filter __unused
,
637 bool externs __unused
)
639 pr_warning("Debuginfo-analysis is not supported.\n");
644 static int parse_line_num(char **ptr
, int *val
, const char *what
)
646 const char *start
= *ptr
;
649 *val
= strtol(*ptr
, ptr
, 0);
650 if (errno
|| *ptr
== start
) {
651 semantic_error("'%s' is not a valid number.\n", what
);
658 * Stuff 'lr' according to the line range described by 'arg'.
659 * The line range syntax is described by:
661 * SRC[:SLN[+NUM|-ELN]]
662 * FNC[@SRC][:SLN[+NUM|-ELN]]
664 int parse_line_range_desc(const char *arg
, struct line_range
*lr
)
666 char *range
, *file
, *name
= strdup(arg
);
675 range
= strchr(name
, ':');
679 err
= parse_line_num(&range
, &lr
->start
, "start line");
683 if (*range
== '+' || *range
== '-') {
684 const char c
= *range
++;
686 err
= parse_line_num(&range
, &lr
->end
, "end line");
691 lr
->end
+= lr
->start
;
693 * Adjust the number of lines here.
694 * If the number of lines == 1, the
695 * the end of line should be equal to
702 pr_debug("Line range is %d to %d\n", lr
->start
, lr
->end
);
705 if (lr
->start
> lr
->end
) {
706 semantic_error("Start line must be smaller"
707 " than end line.\n");
710 if (*range
!= '\0') {
711 semantic_error("Tailing with invalid str '%s'.\n", range
);
716 file
= strchr(name
, '@');
719 lr
->file
= strdup(++file
);
720 if (lr
->file
== NULL
) {
725 } else if (strchr(name
, '.'))
736 /* Check the name is good for event/group */
737 static bool check_event_name(const char *name
)
739 if (!isalpha(*name
) && *name
!= '_')
741 while (*++name
!= '\0') {
742 if (!isalpha(*name
) && !isdigit(*name
) && *name
!= '_')
748 /* Parse probepoint definition. */
749 static int parse_perf_probe_point(char *arg
, struct perf_probe_event
*pev
)
751 struct perf_probe_point
*pp
= &pev
->point
;
756 * perf probe [EVENT=]SRC[:LN|;PTN]
757 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
759 * TODO:Group name support
762 ptr
= strpbrk(arg
, ";=@+%");
763 if (ptr
&& *ptr
== '=') { /* Event name */
766 if (strchr(arg
, ':')) {
767 semantic_error("Group name is not supported yet.\n");
770 if (!check_event_name(arg
)) {
771 semantic_error("%s is bad for event name -it must "
772 "follow C symbol-naming rule.\n", arg
);
775 pev
->event
= strdup(arg
);
776 if (pev
->event
== NULL
)
782 ptr
= strpbrk(arg
, ";:+@%");
792 /* Check arg is function or file and copy it */
793 if (strchr(tmp
, '.')) /* File */
798 /* Parse other options */
802 if (c
== ';') { /* Lazy pattern must be the last part */
803 pp
->lazy_line
= strdup(arg
);
804 if (pp
->lazy_line
== NULL
)
808 ptr
= strpbrk(arg
, ";:+@%");
814 case ':': /* Line number */
815 pp
->line
= strtoul(arg
, &tmp
, 0);
817 semantic_error("There is non-digit char"
818 " in line number.\n");
822 case '+': /* Byte offset from a symbol */
823 pp
->offset
= strtoul(arg
, &tmp
, 0);
825 semantic_error("There is non-digit character"
830 case '@': /* File name */
832 semantic_error("SRC@SRC is not allowed.\n");
835 pp
->file
= strdup(arg
);
836 if (pp
->file
== NULL
)
839 case '%': /* Probe places */
840 if (strcmp(arg
, "return") == 0) {
842 } else { /* Others not supported yet */
843 semantic_error("%%%s is not supported.\n", arg
);
847 default: /* Buggy case */
848 pr_err("This program has a bug at %s:%d.\n",
855 /* Exclusion check */
856 if (pp
->lazy_line
&& pp
->line
) {
857 semantic_error("Lazy pattern can't be used with"
862 if (pp
->lazy_line
&& pp
->offset
) {
863 semantic_error("Lazy pattern can't be used with offset.\n");
867 if (pp
->line
&& pp
->offset
) {
868 semantic_error("Offset can't be used with line number.\n");
872 if (!pp
->line
&& !pp
->lazy_line
&& pp
->file
&& !pp
->function
) {
873 semantic_error("File always requires line number or "
878 if (pp
->offset
&& !pp
->function
) {
879 semantic_error("Offset requires an entry function.\n");
883 if (pp
->retprobe
&& !pp
->function
) {
884 semantic_error("Return probe requires an entry function.\n");
888 if ((pp
->offset
|| pp
->line
|| pp
->lazy_line
) && pp
->retprobe
) {
889 semantic_error("Offset/Line/Lazy pattern can't be used with "
894 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
895 pp
->function
, pp
->file
, pp
->line
, pp
->offset
, pp
->retprobe
,
900 /* Parse perf-probe event argument */
901 static int parse_perf_probe_arg(char *str
, struct perf_probe_arg
*arg
)
903 char *tmp
, *goodname
;
904 struct perf_probe_arg_field
**fieldp
;
906 pr_debug("parsing arg: %s into ", str
);
908 tmp
= strchr(str
, '=');
910 arg
->name
= strndup(str
, tmp
- str
);
911 if (arg
->name
== NULL
)
913 pr_debug("name:%s ", arg
->name
);
917 tmp
= strchr(str
, ':');
918 if (tmp
) { /* Type setting */
920 arg
->type
= strdup(tmp
+ 1);
921 if (arg
->type
== NULL
)
923 pr_debug("type:%s ", arg
->type
);
926 tmp
= strpbrk(str
, "-.[");
927 if (!is_c_varname(str
) || !tmp
) {
928 /* A variable, register, symbol or special value */
929 arg
->var
= strdup(str
);
930 if (arg
->var
== NULL
)
932 pr_debug("%s\n", arg
->var
);
936 /* Structure fields or array element */
937 arg
->var
= strndup(str
, tmp
- str
);
938 if (arg
->var
== NULL
)
941 pr_debug("%s, ", arg
->var
);
942 fieldp
= &arg
->field
;
945 *fieldp
= zalloc(sizeof(struct perf_probe_arg_field
));
948 if (*tmp
== '[') { /* Array */
950 (*fieldp
)->index
= strtol(str
+ 1, &tmp
, 0);
951 (*fieldp
)->ref
= true;
952 if (*tmp
!= ']' || tmp
== str
+ 1) {
953 semantic_error("Array index must be a"
960 } else { /* Structure */
963 (*fieldp
)->ref
= false;
964 } else if (tmp
[1] == '>') {
966 (*fieldp
)->ref
= true;
968 semantic_error("Argument parse error: %s\n",
972 tmp
= strpbrk(str
, "-.[");
975 (*fieldp
)->name
= strndup(str
, tmp
- str
);
976 if ((*fieldp
)->name
== NULL
)
979 goodname
= (*fieldp
)->name
;
980 pr_debug("%s(%d), ", (*fieldp
)->name
, (*fieldp
)->ref
);
981 fieldp
= &(*fieldp
)->next
;
984 (*fieldp
)->name
= strdup(str
);
985 if ((*fieldp
)->name
== NULL
)
988 goodname
= (*fieldp
)->name
;
989 pr_debug("%s(%d)\n", (*fieldp
)->name
, (*fieldp
)->ref
);
991 /* If no name is specified, set the last field name (not array index)*/
993 arg
->name
= strdup(goodname
);
994 if (arg
->name
== NULL
)
1000 /* Parse perf-probe event command */
1001 int parse_perf_probe_command(const char *cmd
, struct perf_probe_event
*pev
)
1004 int argc
, i
, ret
= 0;
1006 argv
= argv_split(cmd
, &argc
);
1008 pr_debug("Failed to split arguments.\n");
1011 if (argc
- 1 > MAX_PROBE_ARGS
) {
1012 semantic_error("Too many probe arguments (%d).\n", argc
- 1);
1016 /* Parse probe point */
1017 ret
= parse_perf_probe_point(argv
[0], pev
);
1021 /* Copy arguments and ensure return probe has no C argument */
1022 pev
->nargs
= argc
- 1;
1023 pev
->args
= zalloc(sizeof(struct perf_probe_arg
) * pev
->nargs
);
1024 if (pev
->args
== NULL
) {
1028 for (i
= 0; i
< pev
->nargs
&& ret
>= 0; i
++) {
1029 ret
= parse_perf_probe_arg(argv
[i
+ 1], &pev
->args
[i
]);
1031 is_c_varname(pev
->args
[i
].var
) && pev
->point
.retprobe
) {
1032 semantic_error("You can't specify local variable for"
1043 /* Return true if this perf_probe_event requires debuginfo */
1044 bool perf_probe_event_need_dwarf(struct perf_probe_event
*pev
)
1048 if (pev
->point
.file
|| pev
->point
.line
|| pev
->point
.lazy_line
)
1051 for (i
= 0; i
< pev
->nargs
; i
++)
1052 if (is_c_varname(pev
->args
[i
].var
))
1058 /* Parse probe_events event into struct probe_point */
1059 static int parse_probe_trace_command(const char *cmd
,
1060 struct probe_trace_event
*tev
)
1062 struct probe_trace_point
*tp
= &tev
->point
;
1068 pr_debug("Parsing probe_events: %s\n", cmd
);
1069 argv
= argv_split(cmd
, &argc
);
1071 pr_debug("Failed to split arguments.\n");
1075 semantic_error("Too few probe arguments.\n");
1080 /* Scan event and group name. */
1081 ret
= sscanf(argv
[0], "%c:%a[^/ \t]/%a[^ \t]",
1082 &pr
, (float *)(void *)&tev
->group
,
1083 (float *)(void *)&tev
->event
);
1085 semantic_error("Failed to parse event name: %s\n", argv
[0]);
1089 pr_debug("Group:%s Event:%s probe:%c\n", tev
->group
, tev
->event
, pr
);
1091 tp
->retprobe
= (pr
== 'r');
1093 /* Scan module name(if there), function name and offset */
1094 p
= strchr(argv
[1], ':');
1096 tp
->module
= strndup(argv
[1], p
- argv
[1]);
1100 ret
= sscanf(p
, "%a[^+]+%lu", (float *)(void *)&tp
->symbol
,
1105 tev
->nargs
= argc
- 2;
1106 tev
->args
= zalloc(sizeof(struct probe_trace_arg
) * tev
->nargs
);
1107 if (tev
->args
== NULL
) {
1111 for (i
= 0; i
< tev
->nargs
; i
++) {
1112 p
= strchr(argv
[i
+ 2], '=');
1113 if (p
) /* We don't need which register is assigned. */
1117 tev
->args
[i
].name
= strdup(argv
[i
+ 2]);
1118 /* TODO: parse regs and offset */
1119 tev
->args
[i
].value
= strdup(p
);
1120 if (tev
->args
[i
].name
== NULL
|| tev
->args
[i
].value
== NULL
) {
1131 /* Compose only probe arg */
1132 int synthesize_perf_probe_arg(struct perf_probe_arg
*pa
, char *buf
, size_t len
)
1134 struct perf_probe_arg_field
*field
= pa
->field
;
1138 if (pa
->name
&& pa
->var
)
1139 ret
= e_snprintf(tmp
, len
, "%s=%s", pa
->name
, pa
->var
);
1141 ret
= e_snprintf(tmp
, len
, "%s", pa
->name
? pa
->name
: pa
->var
);
1148 if (field
->name
[0] == '[')
1149 ret
= e_snprintf(tmp
, len
, "%s", field
->name
);
1151 ret
= e_snprintf(tmp
, len
, "%s%s",
1152 field
->ref
? "->" : ".", field
->name
);
1157 field
= field
->next
;
1161 ret
= e_snprintf(tmp
, len
, ":%s", pa
->type
);
1170 pr_debug("Failed to synthesize perf probe argument: %s\n",
1175 /* Compose only probe point (not argument) */
1176 static char *synthesize_perf_probe_point(struct perf_probe_point
*pp
)
1179 char offs
[32] = "", line
[32] = "", file
[32] = "";
1182 buf
= zalloc(MAX_CMDLEN
);
1188 ret
= e_snprintf(offs
, 32, "+%lu", pp
->offset
);
1193 ret
= e_snprintf(line
, 32, ":%d", pp
->line
);
1201 tmp
= strchr(pp
->file
+ len
- 30, '/');
1202 tmp
= tmp
? tmp
+ 1 : pp
->file
+ len
- 30;
1204 ret
= e_snprintf(file
, 32, "@%s", tmp
);
1210 ret
= e_snprintf(buf
, MAX_CMDLEN
, "%s%s%s%s%s", pp
->function
,
1211 offs
, pp
->retprobe
? "%return" : "", line
,
1214 ret
= e_snprintf(buf
, MAX_CMDLEN
, "%s%s", file
, line
);
1220 pr_debug("Failed to synthesize perf probe point: %s\n",
1228 char *synthesize_perf_probe_command(struct perf_probe_event
*pev
)
1233 buf
= synthesize_perf_probe_point(&pev
->point
);
1238 for (i
= 0; i
< pev
->nargs
; i
++) {
1239 ret
= e_snprintf(&buf
[len
], MAX_CMDLEN
- len
, " %s",
1252 static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref
*ref
,
1253 char **buf
, size_t *buflen
,
1258 depth
= __synthesize_probe_trace_arg_ref(ref
->next
, buf
,
1264 ret
= e_snprintf(*buf
, *buflen
, "%+ld(", ref
->offset
);
1276 static int synthesize_probe_trace_arg(struct probe_trace_arg
*arg
,
1277 char *buf
, size_t buflen
)
1279 struct probe_trace_arg_ref
*ref
= arg
->ref
;
1283 /* Argument name or separator */
1285 ret
= e_snprintf(buf
, buflen
, " %s=", arg
->name
);
1287 ret
= e_snprintf(buf
, buflen
, " ");
1293 /* Special case: @XXX */
1294 if (arg
->value
[0] == '@' && arg
->ref
)
1297 /* Dereferencing arguments */
1299 depth
= __synthesize_probe_trace_arg_ref(ref
, &buf
,
1305 /* Print argument value */
1306 if (arg
->value
[0] == '@' && arg
->ref
)
1307 ret
= e_snprintf(buf
, buflen
, "%s%+ld", arg
->value
,
1310 ret
= e_snprintf(buf
, buflen
, "%s", arg
->value
);
1318 ret
= e_snprintf(buf
, buflen
, ")");
1324 /* Print argument type */
1326 ret
= e_snprintf(buf
, buflen
, ":%s", arg
->type
);
1335 char *synthesize_probe_trace_command(struct probe_trace_event
*tev
)
1337 struct probe_trace_point
*tp
= &tev
->point
;
1341 buf
= zalloc(MAX_CMDLEN
);
1345 len
= e_snprintf(buf
, MAX_CMDLEN
, "%c:%s/%s %s%s%s+%lu",
1346 tp
->retprobe
? 'r' : 'p',
1347 tev
->group
, tev
->event
,
1348 tp
->module
?: "", tp
->module
? ":" : "",
1349 tp
->symbol
, tp
->offset
);
1353 for (i
= 0; i
< tev
->nargs
; i
++) {
1354 ret
= synthesize_probe_trace_arg(&tev
->args
[i
], buf
+ len
,
1367 static int convert_to_perf_probe_event(struct probe_trace_event
*tev
,
1368 struct perf_probe_event
*pev
)
1373 /* Convert event/group name */
1374 pev
->event
= strdup(tev
->event
);
1375 pev
->group
= strdup(tev
->group
);
1376 if (pev
->event
== NULL
|| pev
->group
== NULL
)
1379 /* Convert trace_point to probe_point */
1380 ret
= kprobe_convert_to_perf_probe(&tev
->point
, &pev
->point
);
1384 /* Convert trace_arg to probe_arg */
1385 pev
->nargs
= tev
->nargs
;
1386 pev
->args
= zalloc(sizeof(struct perf_probe_arg
) * pev
->nargs
);
1387 if (pev
->args
== NULL
)
1389 for (i
= 0; i
< tev
->nargs
&& ret
>= 0; i
++) {
1390 if (tev
->args
[i
].name
)
1391 pev
->args
[i
].name
= strdup(tev
->args
[i
].name
);
1393 ret
= synthesize_probe_trace_arg(&tev
->args
[i
],
1395 pev
->args
[i
].name
= strdup(buf
);
1397 if (pev
->args
[i
].name
== NULL
&& ret
>= 0)
1402 clear_perf_probe_event(pev
);
1407 void clear_perf_probe_event(struct perf_probe_event
*pev
)
1409 struct perf_probe_point
*pp
= &pev
->point
;
1410 struct perf_probe_arg_field
*field
, *next
;
1422 free(pp
->lazy_line
);
1423 for (i
= 0; i
< pev
->nargs
; i
++) {
1424 if (pev
->args
[i
].name
)
1425 free(pev
->args
[i
].name
);
1426 if (pev
->args
[i
].var
)
1427 free(pev
->args
[i
].var
);
1428 if (pev
->args
[i
].type
)
1429 free(pev
->args
[i
].type
);
1430 field
= pev
->args
[i
].field
;
1441 memset(pev
, 0, sizeof(*pev
));
1444 static void clear_probe_trace_event(struct probe_trace_event
*tev
)
1446 struct probe_trace_arg_ref
*ref
, *next
;
1453 if (tev
->point
.symbol
)
1454 free(tev
->point
.symbol
);
1455 if (tev
->point
.module
)
1456 free(tev
->point
.module
);
1457 for (i
= 0; i
< tev
->nargs
; i
++) {
1458 if (tev
->args
[i
].name
)
1459 free(tev
->args
[i
].name
);
1460 if (tev
->args
[i
].value
)
1461 free(tev
->args
[i
].value
);
1462 if (tev
->args
[i
].type
)
1463 free(tev
->args
[i
].type
);
1464 ref
= tev
->args
[i
].ref
;
1473 memset(tev
, 0, sizeof(*tev
));
1476 static int open_kprobe_events(bool readwrite
)
1479 const char *__debugfs
;
1482 __debugfs
= debugfs_find_mountpoint();
1483 if (__debugfs
== NULL
) {
1484 pr_warning("Debugfs is not mounted.\n");
1488 ret
= e_snprintf(buf
, PATH_MAX
, "%stracing/kprobe_events", __debugfs
);
1490 pr_debug("Opening %s write=%d\n", buf
, readwrite
);
1491 if (readwrite
&& !probe_event_dry_run
)
1492 ret
= open(buf
, O_RDWR
, O_APPEND
);
1494 ret
= open(buf
, O_RDONLY
, 0);
1498 if (errno
== ENOENT
)
1499 pr_warning("kprobe_events file does not exist - please"
1500 " rebuild kernel with CONFIG_KPROBE_EVENT.\n");
1502 pr_warning("Failed to open kprobe_events file: %s\n",
1508 /* Get raw string list of current kprobe_events */
1509 static struct strlist
*get_probe_trace_command_rawlist(int fd
)
1513 char buf
[MAX_CMDLEN
];
1517 sl
= strlist__new(true, NULL
);
1519 fp
= fdopen(dup(fd
), "r");
1521 p
= fgets(buf
, MAX_CMDLEN
, fp
);
1525 idx
= strlen(p
) - 1;
1528 ret
= strlist__add(sl
, buf
);
1530 pr_debug("strlist__add failed: %s\n", strerror(-ret
));
1531 strlist__delete(sl
);
1541 static int show_perf_probe_event(struct perf_probe_event
*pev
)
1547 /* Synthesize only event probe point */
1548 place
= synthesize_perf_probe_point(&pev
->point
);
1552 ret
= e_snprintf(buf
, 128, "%s:%s", pev
->group
, pev
->event
);
1556 printf(" %-20s (on %s", buf
, place
);
1558 if (pev
->nargs
> 0) {
1560 for (i
= 0; i
< pev
->nargs
; i
++) {
1561 ret
= synthesize_perf_probe_arg(&pev
->args
[i
],
1573 /* List up current perf-probe events */
1574 int show_perf_probe_events(void)
1577 struct probe_trace_event tev
;
1578 struct perf_probe_event pev
;
1579 struct strlist
*rawlist
;
1580 struct str_node
*ent
;
1583 ret
= init_vmlinux();
1587 memset(&tev
, 0, sizeof(tev
));
1588 memset(&pev
, 0, sizeof(pev
));
1590 fd
= open_kprobe_events(false);
1594 rawlist
= get_probe_trace_command_rawlist(fd
);
1599 strlist__for_each(ent
, rawlist
) {
1600 ret
= parse_probe_trace_command(ent
->s
, &tev
);
1602 ret
= convert_to_perf_probe_event(&tev
, &pev
);
1604 ret
= show_perf_probe_event(&pev
);
1606 clear_perf_probe_event(&pev
);
1607 clear_probe_trace_event(&tev
);
1611 strlist__delete(rawlist
);
1616 /* Get current perf-probe event names */
1617 static struct strlist
*get_probe_trace_event_names(int fd
, bool include_group
)
1620 struct strlist
*sl
, *rawlist
;
1621 struct str_node
*ent
;
1622 struct probe_trace_event tev
;
1625 memset(&tev
, 0, sizeof(tev
));
1626 rawlist
= get_probe_trace_command_rawlist(fd
);
1627 sl
= strlist__new(true, NULL
);
1628 strlist__for_each(ent
, rawlist
) {
1629 ret
= parse_probe_trace_command(ent
->s
, &tev
);
1632 if (include_group
) {
1633 ret
= e_snprintf(buf
, 128, "%s:%s", tev
.group
,
1636 ret
= strlist__add(sl
, buf
);
1638 ret
= strlist__add(sl
, tev
.event
);
1639 clear_probe_trace_event(&tev
);
1643 strlist__delete(rawlist
);
1646 strlist__delete(sl
);
1652 static int write_probe_trace_event(int fd
, struct probe_trace_event
*tev
)
1655 char *buf
= synthesize_probe_trace_command(tev
);
1658 pr_debug("Failed to synthesize probe trace event.\n");
1662 pr_debug("Writing event: %s\n", buf
);
1663 if (!probe_event_dry_run
) {
1664 ret
= write(fd
, buf
, strlen(buf
));
1666 pr_warning("Failed to write event: %s\n",
1673 static int get_new_event_name(char *buf
, size_t len
, const char *base
,
1674 struct strlist
*namelist
, bool allow_suffix
)
1679 ret
= e_snprintf(buf
, len
, "%s", base
);
1681 pr_debug("snprintf() failed: %s\n", strerror(-ret
));
1684 if (!strlist__has_entry(namelist
, buf
))
1687 if (!allow_suffix
) {
1688 pr_warning("Error: event \"%s\" already exists. "
1689 "(Use -f to force duplicates.)\n", base
);
1693 /* Try to add suffix */
1694 for (i
= 1; i
< MAX_EVENT_INDEX
; i
++) {
1695 ret
= e_snprintf(buf
, len
, "%s_%d", base
, i
);
1697 pr_debug("snprintf() failed: %s\n", strerror(-ret
));
1700 if (!strlist__has_entry(namelist
, buf
))
1703 if (i
== MAX_EVENT_INDEX
) {
1704 pr_warning("Too many events are on the same function.\n");
1711 static int __add_probe_trace_events(struct perf_probe_event
*pev
,
1712 struct probe_trace_event
*tevs
,
1713 int ntevs
, bool allow_suffix
)
1716 struct probe_trace_event
*tev
= NULL
;
1718 const char *event
, *group
;
1719 struct strlist
*namelist
;
1721 fd
= open_kprobe_events(true);
1724 /* Get current event names */
1725 namelist
= get_probe_trace_event_names(fd
, false);
1727 pr_debug("Failed to get current event list.\n");
1732 printf("Add new event%s\n", (ntevs
> 1) ? "s:" : ":");
1733 for (i
= 0; i
< ntevs
; i
++) {
1738 if (pev
->point
.function
)
1739 event
= pev
->point
.function
;
1741 event
= tev
->point
.symbol
;
1745 group
= PERFPROBE_GROUP
;
1747 /* Get an unused new event name */
1748 ret
= get_new_event_name(buf
, 64, event
,
1749 namelist
, allow_suffix
);
1754 tev
->event
= strdup(event
);
1755 tev
->group
= strdup(group
);
1756 if (tev
->event
== NULL
|| tev
->group
== NULL
) {
1760 ret
= write_probe_trace_event(fd
, tev
);
1763 /* Add added event name to namelist */
1764 strlist__add(namelist
, event
);
1766 /* Trick here - save current event/group */
1769 pev
->event
= tev
->event
;
1770 pev
->group
= tev
->group
;
1771 show_perf_probe_event(pev
);
1772 /* Trick here - restore current event/group */
1773 pev
->event
= (char *)event
;
1774 pev
->group
= (char *)group
;
1777 * Probes after the first probe which comes from same
1778 * user input are always allowed to add suffix, because
1779 * there might be several addresses corresponding to
1782 allow_suffix
= true;
1786 /* Show how to use the event. */
1787 printf("\nYou can now use it on all perf tools, such as:\n\n");
1788 printf("\tperf record -e %s:%s -aR sleep 1\n\n", tev
->group
,
1792 strlist__delete(namelist
);
1797 static int convert_to_probe_trace_events(struct perf_probe_event
*pev
,
1798 struct probe_trace_event
**tevs
,
1799 int max_tevs
, const char *module
)
1803 struct probe_trace_event
*tev
;
1805 /* Convert perf_probe_event with debuginfo */
1806 ret
= try_to_find_probe_trace_events(pev
, tevs
, max_tevs
, module
);
1808 return ret
; /* Found in debuginfo or got an error */
1810 /* Allocate trace event buffer */
1811 tev
= *tevs
= zalloc(sizeof(struct probe_trace_event
));
1815 /* Copy parameters */
1816 tev
->point
.symbol
= strdup(pev
->point
.function
);
1817 if (tev
->point
.symbol
== NULL
) {
1823 tev
->point
.module
= strdup(module
);
1824 if (tev
->point
.module
== NULL
) {
1830 tev
->point
.offset
= pev
->point
.offset
;
1831 tev
->point
.retprobe
= pev
->point
.retprobe
;
1832 tev
->nargs
= pev
->nargs
;
1834 tev
->args
= zalloc(sizeof(struct probe_trace_arg
)
1836 if (tev
->args
== NULL
) {
1840 for (i
= 0; i
< tev
->nargs
; i
++) {
1841 if (pev
->args
[i
].name
) {
1842 tev
->args
[i
].name
= strdup(pev
->args
[i
].name
);
1843 if (tev
->args
[i
].name
== NULL
) {
1848 tev
->args
[i
].value
= strdup(pev
->args
[i
].var
);
1849 if (tev
->args
[i
].value
== NULL
) {
1853 if (pev
->args
[i
].type
) {
1854 tev
->args
[i
].type
= strdup(pev
->args
[i
].type
);
1855 if (tev
->args
[i
].type
== NULL
) {
1863 /* Currently just checking function name from symbol map */
1864 sym
= __find_kernel_function_by_name(tev
->point
.symbol
, NULL
);
1866 pr_warning("Kernel symbol \'%s\' not found.\n",
1870 } else if (tev
->point
.offset
> sym
->end
- sym
->start
) {
1871 pr_warning("Offset specified is greater than size of %s\n",
1880 clear_probe_trace_event(tev
);
1886 struct __event_package
{
1887 struct perf_probe_event
*pev
;
1888 struct probe_trace_event
*tevs
;
1892 int add_perf_probe_events(struct perf_probe_event
*pevs
, int npevs
,
1893 int max_tevs
, const char *module
, bool force_add
)
1896 struct __event_package
*pkgs
;
1898 pkgs
= zalloc(sizeof(struct __event_package
) * npevs
);
1902 /* Init vmlinux path */
1903 ret
= init_vmlinux();
1909 /* Loop 1: convert all events */
1910 for (i
= 0; i
< npevs
; i
++) {
1911 pkgs
[i
].pev
= &pevs
[i
];
1912 /* Convert with or without debuginfo */
1913 ret
= convert_to_probe_trace_events(pkgs
[i
].pev
,
1919 pkgs
[i
].ntevs
= ret
;
1922 /* Loop 2: add all events */
1923 for (i
= 0; i
< npevs
; i
++) {
1924 ret
= __add_probe_trace_events(pkgs
[i
].pev
, pkgs
[i
].tevs
,
1925 pkgs
[i
].ntevs
, force_add
);
1930 /* Loop 3: cleanup and free trace events */
1931 for (i
= 0; i
< npevs
; i
++) {
1932 for (j
= 0; j
< pkgs
[i
].ntevs
; j
++)
1933 clear_probe_trace_event(&pkgs
[i
].tevs
[j
]);
1941 static int __del_trace_probe_event(int fd
, struct str_node
*ent
)
1947 /* Convert from perf-probe event to trace-probe event */
1948 ret
= e_snprintf(buf
, 128, "-:%s", ent
->s
);
1952 p
= strchr(buf
+ 2, ':');
1954 pr_debug("Internal error: %s should have ':' but not.\n",
1961 pr_debug("Writing event: %s\n", buf
);
1962 ret
= write(fd
, buf
, strlen(buf
));
1968 printf("Remove event: %s\n", ent
->s
);
1971 pr_warning("Failed to delete event: %s\n", strerror(-ret
));
1975 static int del_trace_probe_event(int fd
, const char *group
,
1976 const char *event
, struct strlist
*namelist
)
1979 struct str_node
*ent
, *n
;
1980 int found
= 0, ret
= 0;
1982 ret
= e_snprintf(buf
, 128, "%s:%s", group
, event
);
1984 pr_err("Failed to copy event.\n");
1988 if (strpbrk(buf
, "*?")) { /* Glob-exp */
1989 strlist__for_each_safe(ent
, n
, namelist
)
1990 if (strglobmatch(ent
->s
, buf
)) {
1992 ret
= __del_trace_probe_event(fd
, ent
);
1995 strlist__remove(namelist
, ent
);
1998 ent
= strlist__find(namelist
, buf
);
2001 ret
= __del_trace_probe_event(fd
, ent
);
2003 strlist__remove(namelist
, ent
);
2006 if (found
== 0 && ret
>= 0)
2007 pr_info("Info: Event \"%s\" does not exist.\n", buf
);
2012 int del_perf_probe_events(struct strlist
*dellist
)
2015 const char *group
, *event
;
2017 struct str_node
*ent
;
2018 struct strlist
*namelist
;
2020 fd
= open_kprobe_events(true);
2024 /* Get current event names */
2025 namelist
= get_probe_trace_event_names(fd
, true);
2026 if (namelist
== NULL
)
2029 strlist__for_each(ent
, dellist
) {
2030 str
= strdup(ent
->s
);
2035 pr_debug("Parsing: %s\n", str
);
2036 p
= strchr(str
, ':');
2045 pr_debug("Group: %s, Event: %s\n", group
, event
);
2046 ret
= del_trace_probe_event(fd
, group
, event
, namelist
);
2051 strlist__delete(namelist
);
2056 /* TODO: don't use a global variable for filter ... */
2057 static struct strfilter
*available_func_filter
;
2060 * If a symbol corresponds to a function with global binding and
2061 * matches filter return 0. For all others return 1.
2063 static int filter_available_functions(struct map
*map __unused
,
2066 if (sym
->binding
== STB_GLOBAL
&&
2067 strfilter__compare(available_func_filter
, sym
->name
))
2072 int show_available_funcs(const char *module
, struct strfilter
*_filter
)
2079 ret
= init_vmlinux();
2083 map
= kernel_get_module_map(module
);
2085 pr_err("Failed to find %s map.\n", (module
) ? : "kernel");
2088 available_func_filter
= _filter
;
2089 if (map__load(map
, filter_available_functions
)) {
2090 pr_err("Failed to load map.\n");
2093 if (!dso__sorted_by_name(map
->dso
, map
->type
))
2094 dso__sort_by_name(map
->dso
, map
->type
);
2096 dso__fprintf_symbols_by_name(map
->dso
, map
->type
, stdout
);