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.
23 #include <sys/utsname.h>
24 #include <sys/types.h>
46 #include "trace-event.h" /* For __unused */
47 #include "probe-event.h"
48 #include "probe-finder.h"
50 #define MAX_CMDLEN 256
51 #define MAX_PROBE_ARGS 128
52 #define PERFPROBE_GROUP "probe"
54 bool probe_event_dry_run
; /* Dry run flag */
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 struct machine machine
;
77 /* Initialize symbol maps and path of vmlinux/modules */
78 static int init_vmlinux(void)
82 symbol_conf
.sort_by_name
= true;
83 if (symbol_conf
.vmlinux_name
== NULL
)
84 symbol_conf
.try_vmlinux_path
= true;
86 pr_debug("Use vmlinux: %s\n", symbol_conf
.vmlinux_name
);
89 pr_debug("Failed to init symbol map.\n");
93 ret
= machine__init(&machine
, "", HOST_KERNEL_ID
);
97 if (machine__create_kernel_maps(&machine
) < 0) {
98 pr_debug("machine__create_kernel_maps() failed.\n");
103 pr_warning("Failed to init vmlinux path.\n");
107 static struct symbol
*__find_kernel_function_by_name(const char *name
,
110 return machine__find_kernel_function_by_name(&machine
, name
, mapp
,
114 const char *kernel_get_module_path(const char *module
)
118 const char *vmlinux_name
;
121 list_for_each_entry(dso
, &machine
.kernel_dsos
, node
) {
122 if (strncmp(dso
->short_name
+ 1, module
,
123 dso
->short_name_len
- 2) == 0)
126 pr_debug("Failed to find module %s.\n", module
);
130 map
= machine
.vmlinux_maps
[MAP__FUNCTION
];
133 vmlinux_name
= symbol_conf
.vmlinux_name
;
135 if (dso__load_vmlinux(dso
, map
, vmlinux_name
, NULL
) <= 0)
138 if (dso__load_vmlinux_path(dso
, map
, NULL
) <= 0) {
139 pr_debug("Failed to load kernel map.\n");
144 return dso
->long_name
;
148 static int open_vmlinux(const char *module
)
150 const char *path
= kernel_get_module_path(module
);
152 pr_err("Failed to find path of %s module.\n",
156 pr_debug("Try to open %s\n", path
);
157 return open(path
, O_RDONLY
);
161 * Convert trace point to probe point with debuginfo
162 * Currently only handles kprobes.
164 static int kprobe_convert_to_perf_probe(struct probe_trace_point
*tp
,
165 struct perf_probe_point
*pp
)
172 sym
= __find_kernel_function_by_name(tp
->symbol
, &map
);
174 addr
= map
->unmap_ip(map
, sym
->start
+ tp
->offset
);
175 pr_debug("try to find %s+%ld@%llx\n", tp
->symbol
,
177 ret
= find_perf_probe_point((unsigned long)addr
, pp
);
180 pr_debug("Failed to find corresponding probes from "
181 "debuginfo. Use kprobe event information.\n");
182 pp
->function
= strdup(tp
->symbol
);
183 if (pp
->function
== NULL
)
185 pp
->offset
= tp
->offset
;
187 pp
->retprobe
= tp
->retprobe
;
192 /* Try to find perf_probe_event with debuginfo */
193 static int try_to_find_probe_trace_events(struct perf_probe_event
*pev
,
194 struct probe_trace_event
**tevs
,
195 int max_tevs
, const char *module
)
197 bool need_dwarf
= perf_probe_event_need_dwarf(pev
);
200 fd
= open_vmlinux(module
);
203 pr_warning("Failed to open debuginfo file.\n");
206 pr_debug("Could not open vmlinux. Try to use symbols.\n");
210 /* Searching trace events corresponding to probe event */
211 ntevs
= find_probe_trace_events(fd
, pev
, tevs
, max_tevs
);
214 if (ntevs
> 0) { /* Succeeded to find trace events */
215 pr_debug("find %d probe_trace_events.\n", ntevs
);
219 if (ntevs
== 0) { /* No error but failed to find probe point. */
220 pr_warning("Probe point '%s' not found.\n",
221 synthesize_perf_probe_point(&pev
->point
));
224 /* Error path : ntevs < 0 */
225 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs
);
226 if (ntevs
== -EBADF
) {
227 pr_warning("Warning: No dwarf info found in the vmlinux - "
228 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
230 pr_debug("Trying to use symbols.\n");
238 * Find a src file from a DWARF tag path. Prepend optional source path prefix
239 * and chop off leading directories that do not exist. Result is passed back as
240 * a newly allocated path on success.
241 * Return 0 if file was found and readable, -errno otherwise.
243 static int get_real_path(const char *raw_path
, const char *comp_dir
,
246 const char *prefix
= symbol_conf
.source_prefix
;
249 if (raw_path
[0] != '/' && comp_dir
)
250 /* If not an absolute path, try to use comp_dir */
253 if (access(raw_path
, R_OK
) == 0) {
254 *new_path
= strdup(raw_path
);
261 *new_path
= malloc((strlen(prefix
) + strlen(raw_path
) + 2));
266 sprintf(*new_path
, "%s/%s", prefix
, raw_path
);
268 if (access(*new_path
, R_OK
) == 0)
271 if (!symbol_conf
.source_prefix
)
272 /* In case of searching comp_dir, don't retry */
280 raw_path
= strchr(++raw_path
, '/');
296 #define LINEBUF_SIZE 256
297 #define NR_ADDITIONAL_LINES 2
299 static int __show_one_line(FILE *fp
, int l
, bool skip
, bool show_num
)
301 char buf
[LINEBUF_SIZE
];
302 const char *color
= show_num
? "" : PERF_COLOR_BLUE
;
303 const char *prefix
= NULL
;
306 if (fgets(buf
, LINEBUF_SIZE
, fp
) == NULL
)
311 prefix
= show_num
? "%7d " : " ";
312 color_fprintf(stdout
, color
, prefix
, l
);
314 color_fprintf(stdout
, color
, "%s", buf
);
316 } while (strchr(buf
, '\n') == NULL
);
321 pr_warning("File read error: %s\n", strerror(errno
));
327 static int _show_one_line(FILE *fp
, int l
, bool skip
, bool show_num
)
329 int rv
= __show_one_line(fp
, l
, skip
, show_num
);
331 pr_warning("Source file is shorter than expected.\n");
337 #define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
338 #define show_one_line(f,l) _show_one_line(f,l,false,false)
339 #define skip_one_line(f,l) _show_one_line(f,l,true,false)
340 #define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
343 * Show line-range always requires debuginfo to find source file and
346 int show_line_range(struct line_range
*lr
, const char *module
)
349 struct line_node
*ln
;
354 /* Search a line range */
355 ret
= init_vmlinux();
359 fd
= open_vmlinux(module
);
361 pr_warning("Failed to open debuginfo file.\n");
365 ret
= find_line_range(fd
, lr
);
368 pr_warning("Specified source line is not found.\n");
370 } else if (ret
< 0) {
371 pr_warning("Debuginfo analysis failed. (%d)\n", ret
);
375 /* Convert source file path */
377 ret
= get_real_path(tmp
, lr
->comp_dir
, &lr
->path
);
378 free(tmp
); /* Free old path */
380 pr_warning("Failed to find source file. (%d)\n", ret
);
387 fprintf(stdout
, "<%s:%d>\n", lr
->function
,
388 lr
->start
- lr
->offset
);
390 fprintf(stdout
, "<%s:%d>\n", lr
->path
, lr
->start
);
392 fp
= fopen(lr
->path
, "r");
394 pr_warning("Failed to open %s: %s\n", lr
->path
,
398 /* Skip to starting line number */
399 while (l
< lr
->start
) {
400 ret
= skip_one_line(fp
, l
++);
405 list_for_each_entry(ln
, &lr
->line_list
, list
) {
406 for (; ln
->line
> l
; l
++) {
407 ret
= show_one_line(fp
, l
- lr
->offset
);
411 ret
= show_one_line_with_num(fp
, l
++ - lr
->offset
);
416 if (lr
->end
== INT_MAX
)
417 lr
->end
= l
+ NR_ADDITIONAL_LINES
;
418 while (l
<= lr
->end
) {
419 ret
= show_one_line_or_eof(fp
, l
++ - lr
->offset
);
428 static int show_available_vars_at(int fd
, struct perf_probe_event
*pev
,
429 int max_vls
, bool externs
)
433 struct str_node
*node
;
434 struct variable_list
*vls
= NULL
, *vl
;
436 buf
= synthesize_perf_probe_point(&pev
->point
);
439 pr_debug("Searching variables at %s\n", buf
);
441 ret
= find_available_vars_at(fd
, pev
, &vls
, max_vls
, externs
);
443 /* Some variables were found */
444 fprintf(stdout
, "Available variables at %s\n", buf
);
445 for (i
= 0; i
< ret
; i
++) {
448 * A probe point might be converted to
449 * several trace points.
451 fprintf(stdout
, "\t@<%s+%lu>\n", vl
->point
.symbol
,
453 free(vl
->point
.symbol
);
455 strlist__for_each(node
, vl
->vars
)
456 fprintf(stdout
, "\t\t%s\n", node
->s
);
457 strlist__delete(vl
->vars
);
459 fprintf(stdout
, "(No variables)\n");
463 pr_err("Failed to find variables at %s (%d)\n", buf
, ret
);
469 /* Show available variables on given probe point */
470 int show_available_vars(struct perf_probe_event
*pevs
, int npevs
,
471 int max_vls
, const char *module
, bool externs
)
475 ret
= init_vmlinux();
479 fd
= open_vmlinux(module
);
481 pr_warning("Failed to open debug information file.\n");
487 for (i
= 0; i
< npevs
&& ret
>= 0; i
++)
488 ret
= show_available_vars_at(fd
, &pevs
[i
], max_vls
, externs
);
494 #else /* !DWARF_SUPPORT */
496 static int kprobe_convert_to_perf_probe(struct probe_trace_point
*tp
,
497 struct perf_probe_point
*pp
)
501 sym
= __find_kernel_function_by_name(tp
->symbol
, NULL
);
503 pr_err("Failed to find symbol %s in kernel.\n", tp
->symbol
);
506 pp
->function
= strdup(tp
->symbol
);
507 if (pp
->function
== NULL
)
509 pp
->offset
= tp
->offset
;
510 pp
->retprobe
= tp
->retprobe
;
515 static int try_to_find_probe_trace_events(struct perf_probe_event
*pev
,
516 struct probe_trace_event
**tevs __unused
,
517 int max_tevs __unused
, const char *mod __unused
)
519 if (perf_probe_event_need_dwarf(pev
)) {
520 pr_warning("Debuginfo-analysis is not supported.\n");
526 int show_line_range(struct line_range
*lr __unused
, const char *module __unused
)
528 pr_warning("Debuginfo-analysis is not supported.\n");
532 int show_available_vars(struct perf_probe_event
*pevs __unused
,
533 int npevs __unused
, int max_vls __unused
,
534 const char *module __unused
, bool externs __unused
)
536 pr_warning("Debuginfo-analysis is not supported.\n");
541 static int parse_line_num(char **ptr
, int *val
, const char *what
)
543 const char *start
= *ptr
;
546 *val
= strtol(*ptr
, ptr
, 0);
547 if (errno
|| *ptr
== start
) {
548 semantic_error("'%s' is not a valid number.\n", what
);
555 * Stuff 'lr' according to the line range described by 'arg'.
556 * The line range syntax is described by:
558 * SRC[:SLN[+NUM|-ELN]]
559 * FNC[:SLN[+NUM|-ELN]]
561 int parse_line_range_desc(const char *arg
, struct line_range
*lr
)
563 char *range
, *name
= strdup(arg
);
572 range
= strchr(name
, ':');
576 err
= parse_line_num(&range
, &lr
->start
, "start line");
580 if (*range
== '+' || *range
== '-') {
581 const char c
= *range
++;
583 err
= parse_line_num(&range
, &lr
->end
, "end line");
588 lr
->end
+= lr
->start
;
590 * Adjust the number of lines here.
591 * If the number of lines == 1, the
592 * the end of line should be equal to
599 pr_debug("Line range is %d to %d\n", lr
->start
, lr
->end
);
602 if (lr
->start
> lr
->end
) {
603 semantic_error("Start line must be smaller"
604 " than end line.\n");
607 if (*range
!= '\0') {
608 semantic_error("Tailing with invalid str '%s'.\n", range
);
613 if (strchr(name
, '.'))
624 /* Check the name is good for event/group */
625 static bool check_event_name(const char *name
)
627 if (!isalpha(*name
) && *name
!= '_')
629 while (*++name
!= '\0') {
630 if (!isalpha(*name
) && !isdigit(*name
) && *name
!= '_')
636 /* Parse probepoint definition. */
637 static int parse_perf_probe_point(char *arg
, struct perf_probe_event
*pev
)
639 struct perf_probe_point
*pp
= &pev
->point
;
644 * perf probe [EVENT=]SRC[:LN|;PTN]
645 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
647 * TODO:Group name support
650 ptr
= strpbrk(arg
, ";=@+%");
651 if (ptr
&& *ptr
== '=') { /* Event name */
654 if (strchr(arg
, ':')) {
655 semantic_error("Group name is not supported yet.\n");
658 if (!check_event_name(arg
)) {
659 semantic_error("%s is bad for event name -it must "
660 "follow C symbol-naming rule.\n", arg
);
663 pev
->event
= strdup(arg
);
664 if (pev
->event
== NULL
)
670 ptr
= strpbrk(arg
, ";:+@%");
680 /* Check arg is function or file and copy it */
681 if (strchr(tmp
, '.')) /* File */
686 /* Parse other options */
690 if (c
== ';') { /* Lazy pattern must be the last part */
691 pp
->lazy_line
= strdup(arg
);
692 if (pp
->lazy_line
== NULL
)
696 ptr
= strpbrk(arg
, ";:+@%");
702 case ':': /* Line number */
703 pp
->line
= strtoul(arg
, &tmp
, 0);
705 semantic_error("There is non-digit char"
706 " in line number.\n");
710 case '+': /* Byte offset from a symbol */
711 pp
->offset
= strtoul(arg
, &tmp
, 0);
713 semantic_error("There is non-digit character"
718 case '@': /* File name */
720 semantic_error("SRC@SRC is not allowed.\n");
723 pp
->file
= strdup(arg
);
724 if (pp
->file
== NULL
)
727 case '%': /* Probe places */
728 if (strcmp(arg
, "return") == 0) {
730 } else { /* Others not supported yet */
731 semantic_error("%%%s is not supported.\n", arg
);
735 default: /* Buggy case */
736 pr_err("This program has a bug at %s:%d.\n",
743 /* Exclusion check */
744 if (pp
->lazy_line
&& pp
->line
) {
745 semantic_error("Lazy pattern can't be used with"
750 if (pp
->lazy_line
&& pp
->offset
) {
751 semantic_error("Lazy pattern can't be used with offset.\n");
755 if (pp
->line
&& pp
->offset
) {
756 semantic_error("Offset can't be used with line number.\n");
760 if (!pp
->line
&& !pp
->lazy_line
&& pp
->file
&& !pp
->function
) {
761 semantic_error("File always requires line number or "
766 if (pp
->offset
&& !pp
->function
) {
767 semantic_error("Offset requires an entry function.\n");
771 if (pp
->retprobe
&& !pp
->function
) {
772 semantic_error("Return probe requires an entry function.\n");
776 if ((pp
->offset
|| pp
->line
|| pp
->lazy_line
) && pp
->retprobe
) {
777 semantic_error("Offset/Line/Lazy pattern can't be used with "
782 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
783 pp
->function
, pp
->file
, pp
->line
, pp
->offset
, pp
->retprobe
,
788 /* Parse perf-probe event argument */
789 static int parse_perf_probe_arg(char *str
, struct perf_probe_arg
*arg
)
791 char *tmp
, *goodname
;
792 struct perf_probe_arg_field
**fieldp
;
794 pr_debug("parsing arg: %s into ", str
);
796 tmp
= strchr(str
, '=');
798 arg
->name
= strndup(str
, tmp
- str
);
799 if (arg
->name
== NULL
)
801 pr_debug("name:%s ", arg
->name
);
805 tmp
= strchr(str
, ':');
806 if (tmp
) { /* Type setting */
808 arg
->type
= strdup(tmp
+ 1);
809 if (arg
->type
== NULL
)
811 pr_debug("type:%s ", arg
->type
);
814 tmp
= strpbrk(str
, "-.[");
815 if (!is_c_varname(str
) || !tmp
) {
816 /* A variable, register, symbol or special value */
817 arg
->var
= strdup(str
);
818 if (arg
->var
== NULL
)
820 pr_debug("%s\n", arg
->var
);
824 /* Structure fields or array element */
825 arg
->var
= strndup(str
, tmp
- str
);
826 if (arg
->var
== NULL
)
829 pr_debug("%s, ", arg
->var
);
830 fieldp
= &arg
->field
;
833 *fieldp
= zalloc(sizeof(struct perf_probe_arg_field
));
836 if (*tmp
== '[') { /* Array */
838 (*fieldp
)->index
= strtol(str
+ 1, &tmp
, 0);
839 (*fieldp
)->ref
= true;
840 if (*tmp
!= ']' || tmp
== str
+ 1) {
841 semantic_error("Array index must be a"
848 } else { /* Structure */
851 (*fieldp
)->ref
= false;
852 } else if (tmp
[1] == '>') {
854 (*fieldp
)->ref
= true;
856 semantic_error("Argument parse error: %s\n",
860 tmp
= strpbrk(str
, "-.[");
863 (*fieldp
)->name
= strndup(str
, tmp
- str
);
864 if ((*fieldp
)->name
== NULL
)
867 goodname
= (*fieldp
)->name
;
868 pr_debug("%s(%d), ", (*fieldp
)->name
, (*fieldp
)->ref
);
869 fieldp
= &(*fieldp
)->next
;
872 (*fieldp
)->name
= strdup(str
);
873 if ((*fieldp
)->name
== NULL
)
876 goodname
= (*fieldp
)->name
;
877 pr_debug("%s(%d)\n", (*fieldp
)->name
, (*fieldp
)->ref
);
879 /* If no name is specified, set the last field name (not array index)*/
881 arg
->name
= strdup(goodname
);
882 if (arg
->name
== NULL
)
888 /* Parse perf-probe event command */
889 int parse_perf_probe_command(const char *cmd
, struct perf_probe_event
*pev
)
892 int argc
, i
, ret
= 0;
894 argv
= argv_split(cmd
, &argc
);
896 pr_debug("Failed to split arguments.\n");
899 if (argc
- 1 > MAX_PROBE_ARGS
) {
900 semantic_error("Too many probe arguments (%d).\n", argc
- 1);
904 /* Parse probe point */
905 ret
= parse_perf_probe_point(argv
[0], pev
);
909 /* Copy arguments and ensure return probe has no C argument */
910 pev
->nargs
= argc
- 1;
911 pev
->args
= zalloc(sizeof(struct perf_probe_arg
) * pev
->nargs
);
912 if (pev
->args
== NULL
) {
916 for (i
= 0; i
< pev
->nargs
&& ret
>= 0; i
++) {
917 ret
= parse_perf_probe_arg(argv
[i
+ 1], &pev
->args
[i
]);
919 is_c_varname(pev
->args
[i
].var
) && pev
->point
.retprobe
) {
920 semantic_error("You can't specify local variable for"
931 /* Return true if this perf_probe_event requires debuginfo */
932 bool perf_probe_event_need_dwarf(struct perf_probe_event
*pev
)
936 if (pev
->point
.file
|| pev
->point
.line
|| pev
->point
.lazy_line
)
939 for (i
= 0; i
< pev
->nargs
; i
++)
940 if (is_c_varname(pev
->args
[i
].var
))
946 /* Parse probe_events event into struct probe_point */
947 static int parse_probe_trace_command(const char *cmd
,
948 struct probe_trace_event
*tev
)
950 struct probe_trace_point
*tp
= &tev
->point
;
956 pr_debug("Parsing probe_events: %s\n", cmd
);
957 argv
= argv_split(cmd
, &argc
);
959 pr_debug("Failed to split arguments.\n");
963 semantic_error("Too few probe arguments.\n");
968 /* Scan event and group name. */
969 ret
= sscanf(argv
[0], "%c:%a[^/ \t]/%a[^ \t]",
970 &pr
, (float *)(void *)&tev
->group
,
971 (float *)(void *)&tev
->event
);
973 semantic_error("Failed to parse event name: %s\n", argv
[0]);
977 pr_debug("Group:%s Event:%s probe:%c\n", tev
->group
, tev
->event
, pr
);
979 tp
->retprobe
= (pr
== 'r');
981 /* Scan function name and offset */
982 ret
= sscanf(argv
[1], "%a[^+]+%lu", (float *)(void *)&tp
->symbol
,
987 tev
->nargs
= argc
- 2;
988 tev
->args
= zalloc(sizeof(struct probe_trace_arg
) * tev
->nargs
);
989 if (tev
->args
== NULL
) {
993 for (i
= 0; i
< tev
->nargs
; i
++) {
994 p
= strchr(argv
[i
+ 2], '=');
995 if (p
) /* We don't need which register is assigned. */
999 tev
->args
[i
].name
= strdup(argv
[i
+ 2]);
1000 /* TODO: parse regs and offset */
1001 tev
->args
[i
].value
= strdup(p
);
1002 if (tev
->args
[i
].name
== NULL
|| tev
->args
[i
].value
== NULL
) {
1013 /* Compose only probe arg */
1014 int synthesize_perf_probe_arg(struct perf_probe_arg
*pa
, char *buf
, size_t len
)
1016 struct perf_probe_arg_field
*field
= pa
->field
;
1020 if (pa
->name
&& pa
->var
)
1021 ret
= e_snprintf(tmp
, len
, "%s=%s", pa
->name
, pa
->var
);
1023 ret
= e_snprintf(tmp
, len
, "%s", pa
->name
? pa
->name
: pa
->var
);
1030 if (field
->name
[0] == '[')
1031 ret
= e_snprintf(tmp
, len
, "%s", field
->name
);
1033 ret
= e_snprintf(tmp
, len
, "%s%s",
1034 field
->ref
? "->" : ".", field
->name
);
1039 field
= field
->next
;
1043 ret
= e_snprintf(tmp
, len
, ":%s", pa
->type
);
1052 pr_debug("Failed to synthesize perf probe argument: %s\n",
1057 /* Compose only probe point (not argument) */
1058 static char *synthesize_perf_probe_point(struct perf_probe_point
*pp
)
1061 char offs
[32] = "", line
[32] = "", file
[32] = "";
1064 buf
= zalloc(MAX_CMDLEN
);
1070 ret
= e_snprintf(offs
, 32, "+%lu", pp
->offset
);
1075 ret
= e_snprintf(line
, 32, ":%d", pp
->line
);
1083 tmp
= strchr(pp
->file
+ len
- 30, '/');
1084 tmp
= tmp
? tmp
+ 1 : pp
->file
+ len
- 30;
1086 ret
= e_snprintf(file
, 32, "@%s", tmp
);
1092 ret
= e_snprintf(buf
, MAX_CMDLEN
, "%s%s%s%s%s", pp
->function
,
1093 offs
, pp
->retprobe
? "%return" : "", line
,
1096 ret
= e_snprintf(buf
, MAX_CMDLEN
, "%s%s", file
, line
);
1102 pr_debug("Failed to synthesize perf probe point: %s\n",
1110 char *synthesize_perf_probe_command(struct perf_probe_event
*pev
)
1115 buf
= synthesize_perf_probe_point(&pev
->point
);
1120 for (i
= 0; i
< pev
->nargs
; i
++) {
1121 ret
= e_snprintf(&buf
[len
], MAX_CMDLEN
- len
, " %s",
1134 static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref
*ref
,
1135 char **buf
, size_t *buflen
,
1140 depth
= __synthesize_probe_trace_arg_ref(ref
->next
, buf
,
1146 ret
= e_snprintf(*buf
, *buflen
, "%+ld(", ref
->offset
);
1158 static int synthesize_probe_trace_arg(struct probe_trace_arg
*arg
,
1159 char *buf
, size_t buflen
)
1161 struct probe_trace_arg_ref
*ref
= arg
->ref
;
1165 /* Argument name or separator */
1167 ret
= e_snprintf(buf
, buflen
, " %s=", arg
->name
);
1169 ret
= e_snprintf(buf
, buflen
, " ");
1175 /* Special case: @XXX */
1176 if (arg
->value
[0] == '@' && arg
->ref
)
1179 /* Dereferencing arguments */
1181 depth
= __synthesize_probe_trace_arg_ref(ref
, &buf
,
1187 /* Print argument value */
1188 if (arg
->value
[0] == '@' && arg
->ref
)
1189 ret
= e_snprintf(buf
, buflen
, "%s%+ld", arg
->value
,
1192 ret
= e_snprintf(buf
, buflen
, "%s", arg
->value
);
1200 ret
= e_snprintf(buf
, buflen
, ")");
1206 /* Print argument type */
1208 ret
= e_snprintf(buf
, buflen
, ":%s", arg
->type
);
1217 char *synthesize_probe_trace_command(struct probe_trace_event
*tev
)
1219 struct probe_trace_point
*tp
= &tev
->point
;
1223 buf
= zalloc(MAX_CMDLEN
);
1227 len
= e_snprintf(buf
, MAX_CMDLEN
, "%c:%s/%s %s+%lu",
1228 tp
->retprobe
? 'r' : 'p',
1229 tev
->group
, tev
->event
,
1230 tp
->symbol
, tp
->offset
);
1234 for (i
= 0; i
< tev
->nargs
; i
++) {
1235 ret
= synthesize_probe_trace_arg(&tev
->args
[i
], buf
+ len
,
1248 static int convert_to_perf_probe_event(struct probe_trace_event
*tev
,
1249 struct perf_probe_event
*pev
)
1254 /* Convert event/group name */
1255 pev
->event
= strdup(tev
->event
);
1256 pev
->group
= strdup(tev
->group
);
1257 if (pev
->event
== NULL
|| pev
->group
== NULL
)
1260 /* Convert trace_point to probe_point */
1261 ret
= kprobe_convert_to_perf_probe(&tev
->point
, &pev
->point
);
1265 /* Convert trace_arg to probe_arg */
1266 pev
->nargs
= tev
->nargs
;
1267 pev
->args
= zalloc(sizeof(struct perf_probe_arg
) * pev
->nargs
);
1268 if (pev
->args
== NULL
)
1270 for (i
= 0; i
< tev
->nargs
&& ret
>= 0; i
++) {
1271 if (tev
->args
[i
].name
)
1272 pev
->args
[i
].name
= strdup(tev
->args
[i
].name
);
1274 ret
= synthesize_probe_trace_arg(&tev
->args
[i
],
1276 pev
->args
[i
].name
= strdup(buf
);
1278 if (pev
->args
[i
].name
== NULL
&& ret
>= 0)
1283 clear_perf_probe_event(pev
);
1288 void clear_perf_probe_event(struct perf_probe_event
*pev
)
1290 struct perf_probe_point
*pp
= &pev
->point
;
1291 struct perf_probe_arg_field
*field
, *next
;
1303 free(pp
->lazy_line
);
1304 for (i
= 0; i
< pev
->nargs
; i
++) {
1305 if (pev
->args
[i
].name
)
1306 free(pev
->args
[i
].name
);
1307 if (pev
->args
[i
].var
)
1308 free(pev
->args
[i
].var
);
1309 if (pev
->args
[i
].type
)
1310 free(pev
->args
[i
].type
);
1311 field
= pev
->args
[i
].field
;
1322 memset(pev
, 0, sizeof(*pev
));
1325 static void clear_probe_trace_event(struct probe_trace_event
*tev
)
1327 struct probe_trace_arg_ref
*ref
, *next
;
1334 if (tev
->point
.symbol
)
1335 free(tev
->point
.symbol
);
1336 for (i
= 0; i
< tev
->nargs
; i
++) {
1337 if (tev
->args
[i
].name
)
1338 free(tev
->args
[i
].name
);
1339 if (tev
->args
[i
].value
)
1340 free(tev
->args
[i
].value
);
1341 if (tev
->args
[i
].type
)
1342 free(tev
->args
[i
].type
);
1343 ref
= tev
->args
[i
].ref
;
1352 memset(tev
, 0, sizeof(*tev
));
1355 static int open_kprobe_events(bool readwrite
)
1358 const char *__debugfs
;
1361 __debugfs
= debugfs_find_mountpoint();
1362 if (__debugfs
== NULL
) {
1363 pr_warning("Debugfs is not mounted.\n");
1367 ret
= e_snprintf(buf
, PATH_MAX
, "%stracing/kprobe_events", __debugfs
);
1369 pr_debug("Opening %s write=%d\n", buf
, readwrite
);
1370 if (readwrite
&& !probe_event_dry_run
)
1371 ret
= open(buf
, O_RDWR
, O_APPEND
);
1373 ret
= open(buf
, O_RDONLY
, 0);
1377 if (errno
== ENOENT
)
1378 pr_warning("kprobe_events file does not exist - please"
1379 " rebuild kernel with CONFIG_KPROBE_EVENT.\n");
1381 pr_warning("Failed to open kprobe_events file: %s\n",
1387 /* Get raw string list of current kprobe_events */
1388 static struct strlist
*get_probe_trace_command_rawlist(int fd
)
1392 char buf
[MAX_CMDLEN
];
1396 sl
= strlist__new(true, NULL
);
1398 fp
= fdopen(dup(fd
), "r");
1400 p
= fgets(buf
, MAX_CMDLEN
, fp
);
1404 idx
= strlen(p
) - 1;
1407 ret
= strlist__add(sl
, buf
);
1409 pr_debug("strlist__add failed: %s\n", strerror(-ret
));
1410 strlist__delete(sl
);
1420 static int show_perf_probe_event(struct perf_probe_event
*pev
)
1426 /* Synthesize only event probe point */
1427 place
= synthesize_perf_probe_point(&pev
->point
);
1431 ret
= e_snprintf(buf
, 128, "%s:%s", pev
->group
, pev
->event
);
1435 printf(" %-20s (on %s", buf
, place
);
1437 if (pev
->nargs
> 0) {
1439 for (i
= 0; i
< pev
->nargs
; i
++) {
1440 ret
= synthesize_perf_probe_arg(&pev
->args
[i
],
1452 /* List up current perf-probe events */
1453 int show_perf_probe_events(void)
1456 struct probe_trace_event tev
;
1457 struct perf_probe_event pev
;
1458 struct strlist
*rawlist
;
1459 struct str_node
*ent
;
1462 ret
= init_vmlinux();
1466 memset(&tev
, 0, sizeof(tev
));
1467 memset(&pev
, 0, sizeof(pev
));
1469 fd
= open_kprobe_events(false);
1473 rawlist
= get_probe_trace_command_rawlist(fd
);
1478 strlist__for_each(ent
, rawlist
) {
1479 ret
= parse_probe_trace_command(ent
->s
, &tev
);
1481 ret
= convert_to_perf_probe_event(&tev
, &pev
);
1483 ret
= show_perf_probe_event(&pev
);
1485 clear_perf_probe_event(&pev
);
1486 clear_probe_trace_event(&tev
);
1490 strlist__delete(rawlist
);
1495 /* Get current perf-probe event names */
1496 static struct strlist
*get_probe_trace_event_names(int fd
, bool include_group
)
1499 struct strlist
*sl
, *rawlist
;
1500 struct str_node
*ent
;
1501 struct probe_trace_event tev
;
1504 memset(&tev
, 0, sizeof(tev
));
1505 rawlist
= get_probe_trace_command_rawlist(fd
);
1506 sl
= strlist__new(true, NULL
);
1507 strlist__for_each(ent
, rawlist
) {
1508 ret
= parse_probe_trace_command(ent
->s
, &tev
);
1511 if (include_group
) {
1512 ret
= e_snprintf(buf
, 128, "%s:%s", tev
.group
,
1515 ret
= strlist__add(sl
, buf
);
1517 ret
= strlist__add(sl
, tev
.event
);
1518 clear_probe_trace_event(&tev
);
1522 strlist__delete(rawlist
);
1525 strlist__delete(sl
);
1531 static int write_probe_trace_event(int fd
, struct probe_trace_event
*tev
)
1534 char *buf
= synthesize_probe_trace_command(tev
);
1537 pr_debug("Failed to synthesize probe trace event.\n");
1541 pr_debug("Writing event: %s\n", buf
);
1542 if (!probe_event_dry_run
) {
1543 ret
= write(fd
, buf
, strlen(buf
));
1545 pr_warning("Failed to write event: %s\n",
1552 static int get_new_event_name(char *buf
, size_t len
, const char *base
,
1553 struct strlist
*namelist
, bool allow_suffix
)
1558 ret
= e_snprintf(buf
, len
, "%s", base
);
1560 pr_debug("snprintf() failed: %s\n", strerror(-ret
));
1563 if (!strlist__has_entry(namelist
, buf
))
1566 if (!allow_suffix
) {
1567 pr_warning("Error: event \"%s\" already exists. "
1568 "(Use -f to force duplicates.)\n", base
);
1572 /* Try to add suffix */
1573 for (i
= 1; i
< MAX_EVENT_INDEX
; i
++) {
1574 ret
= e_snprintf(buf
, len
, "%s_%d", base
, i
);
1576 pr_debug("snprintf() failed: %s\n", strerror(-ret
));
1579 if (!strlist__has_entry(namelist
, buf
))
1582 if (i
== MAX_EVENT_INDEX
) {
1583 pr_warning("Too many events are on the same function.\n");
1590 static int __add_probe_trace_events(struct perf_probe_event
*pev
,
1591 struct probe_trace_event
*tevs
,
1592 int ntevs
, bool allow_suffix
)
1595 struct probe_trace_event
*tev
= NULL
;
1597 const char *event
, *group
;
1598 struct strlist
*namelist
;
1600 fd
= open_kprobe_events(true);
1603 /* Get current event names */
1604 namelist
= get_probe_trace_event_names(fd
, false);
1606 pr_debug("Failed to get current event list.\n");
1611 printf("Add new event%s\n", (ntevs
> 1) ? "s:" : ":");
1612 for (i
= 0; i
< ntevs
; i
++) {
1617 if (pev
->point
.function
)
1618 event
= pev
->point
.function
;
1620 event
= tev
->point
.symbol
;
1624 group
= PERFPROBE_GROUP
;
1626 /* Get an unused new event name */
1627 ret
= get_new_event_name(buf
, 64, event
,
1628 namelist
, allow_suffix
);
1633 tev
->event
= strdup(event
);
1634 tev
->group
= strdup(group
);
1635 if (tev
->event
== NULL
|| tev
->group
== NULL
) {
1639 ret
= write_probe_trace_event(fd
, tev
);
1642 /* Add added event name to namelist */
1643 strlist__add(namelist
, event
);
1645 /* Trick here - save current event/group */
1648 pev
->event
= tev
->event
;
1649 pev
->group
= tev
->group
;
1650 show_perf_probe_event(pev
);
1651 /* Trick here - restore current event/group */
1652 pev
->event
= (char *)event
;
1653 pev
->group
= (char *)group
;
1656 * Probes after the first probe which comes from same
1657 * user input are always allowed to add suffix, because
1658 * there might be several addresses corresponding to
1661 allow_suffix
= true;
1665 /* Show how to use the event. */
1666 printf("\nYou can now use it on all perf tools, such as:\n\n");
1667 printf("\tperf record -e %s:%s -aR sleep 1\n\n", tev
->group
,
1671 strlist__delete(namelist
);
1676 static int convert_to_probe_trace_events(struct perf_probe_event
*pev
,
1677 struct probe_trace_event
**tevs
,
1678 int max_tevs
, const char *module
)
1682 struct probe_trace_event
*tev
;
1684 /* Convert perf_probe_event with debuginfo */
1685 ret
= try_to_find_probe_trace_events(pev
, tevs
, max_tevs
, module
);
1689 /* Allocate trace event buffer */
1690 tev
= *tevs
= zalloc(sizeof(struct probe_trace_event
));
1694 /* Copy parameters */
1695 tev
->point
.symbol
= strdup(pev
->point
.function
);
1696 if (tev
->point
.symbol
== NULL
) {
1700 tev
->point
.offset
= pev
->point
.offset
;
1701 tev
->point
.retprobe
= pev
->point
.retprobe
;
1702 tev
->nargs
= pev
->nargs
;
1704 tev
->args
= zalloc(sizeof(struct probe_trace_arg
)
1706 if (tev
->args
== NULL
) {
1710 for (i
= 0; i
< tev
->nargs
; i
++) {
1711 if (pev
->args
[i
].name
) {
1712 tev
->args
[i
].name
= strdup(pev
->args
[i
].name
);
1713 if (tev
->args
[i
].name
== NULL
) {
1718 tev
->args
[i
].value
= strdup(pev
->args
[i
].var
);
1719 if (tev
->args
[i
].value
== NULL
) {
1723 if (pev
->args
[i
].type
) {
1724 tev
->args
[i
].type
= strdup(pev
->args
[i
].type
);
1725 if (tev
->args
[i
].type
== NULL
) {
1733 /* Currently just checking function name from symbol map */
1734 sym
= __find_kernel_function_by_name(tev
->point
.symbol
, NULL
);
1736 pr_warning("Kernel symbol \'%s\' not found.\n",
1744 clear_probe_trace_event(tev
);
1750 struct __event_package
{
1751 struct perf_probe_event
*pev
;
1752 struct probe_trace_event
*tevs
;
1756 int add_perf_probe_events(struct perf_probe_event
*pevs
, int npevs
,
1757 int max_tevs
, const char *module
, bool force_add
)
1760 struct __event_package
*pkgs
;
1762 pkgs
= zalloc(sizeof(struct __event_package
) * npevs
);
1766 /* Init vmlinux path */
1767 ret
= init_vmlinux();
1773 /* Loop 1: convert all events */
1774 for (i
= 0; i
< npevs
; i
++) {
1775 pkgs
[i
].pev
= &pevs
[i
];
1776 /* Convert with or without debuginfo */
1777 ret
= convert_to_probe_trace_events(pkgs
[i
].pev
,
1783 pkgs
[i
].ntevs
= ret
;
1786 /* Loop 2: add all events */
1787 for (i
= 0; i
< npevs
&& ret
>= 0; i
++)
1788 ret
= __add_probe_trace_events(pkgs
[i
].pev
, pkgs
[i
].tevs
,
1789 pkgs
[i
].ntevs
, force_add
);
1791 /* Loop 3: cleanup and free trace events */
1792 for (i
= 0; i
< npevs
; i
++) {
1793 for (j
= 0; j
< pkgs
[i
].ntevs
; j
++)
1794 clear_probe_trace_event(&pkgs
[i
].tevs
[j
]);
1802 static int __del_trace_probe_event(int fd
, struct str_node
*ent
)
1808 /* Convert from perf-probe event to trace-probe event */
1809 ret
= e_snprintf(buf
, 128, "-:%s", ent
->s
);
1813 p
= strchr(buf
+ 2, ':');
1815 pr_debug("Internal error: %s should have ':' but not.\n",
1822 pr_debug("Writing event: %s\n", buf
);
1823 ret
= write(fd
, buf
, strlen(buf
));
1827 printf("Remove event: %s\n", ent
->s
);
1830 pr_warning("Failed to delete event: %s\n", strerror(-ret
));
1834 static int del_trace_probe_event(int fd
, const char *group
,
1835 const char *event
, struct strlist
*namelist
)
1838 struct str_node
*ent
, *n
;
1839 int found
= 0, ret
= 0;
1841 ret
= e_snprintf(buf
, 128, "%s:%s", group
, event
);
1843 pr_err("Failed to copy event.\n");
1847 if (strpbrk(buf
, "*?")) { /* Glob-exp */
1848 strlist__for_each_safe(ent
, n
, namelist
)
1849 if (strglobmatch(ent
->s
, buf
)) {
1851 ret
= __del_trace_probe_event(fd
, ent
);
1854 strlist__remove(namelist
, ent
);
1857 ent
= strlist__find(namelist
, buf
);
1860 ret
= __del_trace_probe_event(fd
, ent
);
1862 strlist__remove(namelist
, ent
);
1865 if (found
== 0 && ret
>= 0)
1866 pr_info("Info: Event \"%s\" does not exist.\n", buf
);
1871 int del_perf_probe_events(struct strlist
*dellist
)
1874 const char *group
, *event
;
1876 struct str_node
*ent
;
1877 struct strlist
*namelist
;
1879 fd
= open_kprobe_events(true);
1883 /* Get current event names */
1884 namelist
= get_probe_trace_event_names(fd
, true);
1885 if (namelist
== NULL
)
1888 strlist__for_each(ent
, dellist
) {
1889 str
= strdup(ent
->s
);
1894 pr_debug("Parsing: %s\n", str
);
1895 p
= strchr(str
, ':');
1904 pr_debug("Group: %s, Event: %s\n", group
, event
);
1905 ret
= del_trace_probe_event(fd
, group
, event
, namelist
);
1910 strlist__delete(namelist
);