2 * probe-event.c : perf-probe definition to kprobe_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 */
78 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
, "/", 0);
98 kernel
= dso__new_kernel(symbol_conf
.vmlinux_name
);
100 die("Failed to create kernel dso.");
102 ret
= __machine__create_kernel_maps(&machine
, kernel
);
104 pr_debug("Failed to create kernel maps.\n");
108 pr_warning("Failed to init vmlinux path.\n");
113 static int open_vmlinux(void)
115 if (map__load(machine
.vmlinux_maps
[MAP__FUNCTION
], NULL
) < 0) {
116 pr_debug("Failed to load kernel map.\n");
119 pr_debug("Try to open %s\n", machine
.vmlinux_maps
[MAP__FUNCTION
]->dso
->long_name
);
120 return open(machine
.vmlinux_maps
[MAP__FUNCTION
]->dso
->long_name
, O_RDONLY
);
123 /* Convert trace point to probe point with debuginfo */
124 static int convert_to_perf_probe_point(struct kprobe_trace_point
*tp
,
125 struct perf_probe_point
*pp
)
128 int fd
, ret
= -ENOENT
;
130 sym
= map__find_symbol_by_name(machine
.vmlinux_maps
[MAP__FUNCTION
],
135 ret
= find_perf_probe_point(fd
,
136 sym
->start
+ tp
->offset
, pp
);
141 pr_debug("Failed to find corresponding probes from "
142 "debuginfo. Use kprobe event information.\n");
143 pp
->function
= strdup(tp
->symbol
);
144 if (pp
->function
== NULL
)
146 pp
->offset
= tp
->offset
;
148 pp
->retprobe
= tp
->retprobe
;
153 /* Try to find perf_probe_event with debuginfo */
154 static int try_to_find_kprobe_trace_events(struct perf_probe_event
*pev
,
155 struct kprobe_trace_event
**tevs
,
158 bool need_dwarf
= perf_probe_event_need_dwarf(pev
);
164 pr_warning("Failed to open debuginfo file.\n");
167 pr_debug("Could not open vmlinux. Try to use symbols.\n");
171 /* Searching trace events corresponding to probe event */
172 ntevs
= find_kprobe_trace_events(fd
, pev
, tevs
, max_tevs
);
175 if (ntevs
> 0) { /* Succeeded to find trace events */
176 pr_debug("find %d kprobe_trace_events.\n", ntevs
);
180 if (ntevs
== 0) { /* No error but failed to find probe point. */
181 pr_warning("Probe point '%s' not found.\n",
182 synthesize_perf_probe_point(&pev
->point
));
185 /* Error path : ntevs < 0 */
186 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs
);
187 if (ntevs
== -EBADF
) {
188 pr_warning("Warning: No dwarf info found in the vmlinux - "
189 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
191 pr_debug("Trying to use symbols.\nn");
198 #define LINEBUF_SIZE 256
199 #define NR_ADDITIONAL_LINES 2
201 static int show_one_line(FILE *fp
, int l
, bool skip
, bool show_num
)
203 char buf
[LINEBUF_SIZE
];
204 const char *color
= PERF_COLOR_BLUE
;
206 if (fgets(buf
, LINEBUF_SIZE
, fp
) == NULL
)
210 fprintf(stdout
, "%7d %s", l
, buf
);
212 color_fprintf(stdout
, color
, " %s", buf
);
215 while (strlen(buf
) == LINEBUF_SIZE
- 1 &&
216 buf
[LINEBUF_SIZE
- 2] != '\n') {
217 if (fgets(buf
, LINEBUF_SIZE
, fp
) == NULL
)
221 fprintf(stdout
, "%s", buf
);
223 color_fprintf(stdout
, color
, "%s", buf
);
230 pr_warning("Source file is shorter than expected.\n");
232 pr_warning("File read error: %s\n", strerror(errno
));
238 * Show line-range always requires debuginfo to find source file and
241 int show_line_range(struct line_range
*lr
)
244 struct line_node
*ln
;
248 /* Search a line range */
249 ret
= init_vmlinux();
255 pr_warning("Failed to open debuginfo file.\n");
259 ret
= find_line_range(fd
, lr
);
262 pr_warning("Specified source line is not found.\n");
264 } else if (ret
< 0) {
265 pr_warning("Debuginfo analysis failed. (%d)\n", ret
);
272 fprintf(stdout
, "<%s:%d>\n", lr
->function
,
273 lr
->start
- lr
->offset
);
275 fprintf(stdout
, "<%s:%d>\n", lr
->file
, lr
->start
);
277 fp
= fopen(lr
->path
, "r");
279 pr_warning("Failed to open %s: %s\n", lr
->path
,
283 /* Skip to starting line number */
284 while (l
< lr
->start
&& ret
>= 0)
285 ret
= show_one_line(fp
, l
++, true, false);
289 list_for_each_entry(ln
, &lr
->line_list
, list
) {
290 while (ln
->line
> l
&& ret
>= 0)
291 ret
= show_one_line(fp
, (l
++) - lr
->offset
,
294 ret
= show_one_line(fp
, (l
++) - lr
->offset
,
300 if (lr
->end
== INT_MAX
)
301 lr
->end
= l
+ NR_ADDITIONAL_LINES
;
302 while (l
<= lr
->end
&& !feof(fp
) && ret
>= 0)
303 ret
= show_one_line(fp
, (l
++) - lr
->offset
, false, false);
309 #else /* !DWARF_SUPPORT */
311 static int convert_to_perf_probe_point(struct kprobe_trace_point
*tp
,
312 struct perf_probe_point
*pp
)
314 pp
->function
= strdup(tp
->symbol
);
315 if (pp
->function
== NULL
)
317 pp
->offset
= tp
->offset
;
318 pp
->retprobe
= tp
->retprobe
;
323 static int try_to_find_kprobe_trace_events(struct perf_probe_event
*pev
,
324 struct kprobe_trace_event
**tevs __unused
,
325 int max_tevs __unused
)
327 if (perf_probe_event_need_dwarf(pev
)) {
328 pr_warning("Debuginfo-analysis is not supported.\n");
334 int show_line_range(struct line_range
*lr __unused
)
336 pr_warning("Debuginfo-analysis is not supported.\n");
342 int parse_line_range_desc(const char *arg
, struct line_range
*lr
)
349 * FUNC[:SLN[+NUM|-ELN]]
351 ptr
= strchr(arg
, ':');
353 lr
->start
= (int)strtoul(ptr
+ 1, &tmp
, 0);
355 lr
->end
= lr
->start
+ (int)strtoul(tmp
+ 1, &tmp
, 0);
357 * Adjust the number of lines here.
358 * If the number of lines == 1, the
359 * the end of line should be equal to
362 } else if (*tmp
== '-')
363 lr
->end
= (int)strtoul(tmp
+ 1, &tmp
, 0);
366 pr_debug("Line range is %d to %d\n", lr
->start
, lr
->end
);
367 if (lr
->start
> lr
->end
) {
368 semantic_error("Start line must be smaller"
369 " than end line.\n");
373 semantic_error("Tailing with invalid character '%d'.\n",
377 tmp
= strndup(arg
, (ptr
- arg
));
386 if (strchr(tmp
, '.'))
394 /* Check the name is good for event/group */
395 static bool check_event_name(const char *name
)
397 if (!isalpha(*name
) && *name
!= '_')
399 while (*++name
!= '\0') {
400 if (!isalpha(*name
) && !isdigit(*name
) && *name
!= '_')
406 /* Parse probepoint definition. */
407 static int parse_perf_probe_point(char *arg
, struct perf_probe_event
*pev
)
409 struct perf_probe_point
*pp
= &pev
->point
;
414 * perf probe [EVENT=]SRC[:LN|;PTN]
415 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
417 * TODO:Group name support
420 ptr
= strpbrk(arg
, ";=@+%");
421 if (ptr
&& *ptr
== '=') { /* Event name */
424 if (strchr(arg
, ':')) {
425 semantic_error("Group name is not supported yet.\n");
428 if (!check_event_name(arg
)) {
429 semantic_error("%s is bad for event name -it must "
430 "follow C symbol-naming rule.\n", arg
);
433 pev
->event
= strdup(arg
);
434 if (pev
->event
== NULL
)
440 ptr
= strpbrk(arg
, ";:+@%");
450 /* Check arg is function or file and copy it */
451 if (strchr(tmp
, '.')) /* File */
456 /* Parse other options */
460 if (c
== ';') { /* Lazy pattern must be the last part */
461 pp
->lazy_line
= strdup(arg
);
462 if (pp
->lazy_line
== NULL
)
466 ptr
= strpbrk(arg
, ";:+@%");
472 case ':': /* Line number */
473 pp
->line
= strtoul(arg
, &tmp
, 0);
475 semantic_error("There is non-digit char"
476 " in line number.\n");
480 case '+': /* Byte offset from a symbol */
481 pp
->offset
= strtoul(arg
, &tmp
, 0);
483 semantic_error("There is non-digit character"
488 case '@': /* File name */
490 semantic_error("SRC@SRC is not allowed.\n");
493 pp
->file
= strdup(arg
);
494 if (pp
->file
== NULL
)
497 case '%': /* Probe places */
498 if (strcmp(arg
, "return") == 0) {
500 } else { /* Others not supported yet */
501 semantic_error("%%%s is not supported.\n", arg
);
505 default: /* Buggy case */
506 pr_err("This program has a bug at %s:%d.\n",
513 /* Exclusion check */
514 if (pp
->lazy_line
&& pp
->line
) {
515 semantic_error("Lazy pattern can't be used with line number.");
519 if (pp
->lazy_line
&& pp
->offset
) {
520 semantic_error("Lazy pattern can't be used with offset.");
524 if (pp
->line
&& pp
->offset
) {
525 semantic_error("Offset can't be used with line number.");
529 if (!pp
->line
&& !pp
->lazy_line
&& pp
->file
&& !pp
->function
) {
530 semantic_error("File always requires line number or "
535 if (pp
->offset
&& !pp
->function
) {
536 semantic_error("Offset requires an entry function.");
540 if (pp
->retprobe
&& !pp
->function
) {
541 semantic_error("Return probe requires an entry function.");
545 if ((pp
->offset
|| pp
->line
|| pp
->lazy_line
) && pp
->retprobe
) {
546 semantic_error("Offset/Line/Lazy pattern can't be used with "
551 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
552 pp
->function
, pp
->file
, pp
->line
, pp
->offset
, pp
->retprobe
,
557 /* Parse perf-probe event argument */
558 static int parse_perf_probe_arg(char *str
, struct perf_probe_arg
*arg
)
561 struct perf_probe_arg_field
**fieldp
;
563 pr_debug("parsing arg: %s into ", str
);
565 tmp
= strchr(str
, '=');
567 arg
->name
= strndup(str
, tmp
- str
);
568 if (arg
->name
== NULL
)
570 pr_debug("name:%s ", arg
->name
);
574 tmp
= strchr(str
, ':');
575 if (tmp
) { /* Type setting */
577 arg
->type
= strdup(tmp
+ 1);
578 if (arg
->type
== NULL
)
580 pr_debug("type:%s ", arg
->type
);
583 tmp
= strpbrk(str
, "-.");
584 if (!is_c_varname(str
) || !tmp
) {
585 /* A variable, register, symbol or special value */
586 arg
->var
= strdup(str
);
587 if (arg
->var
== NULL
)
589 pr_debug("%s\n", arg
->var
);
593 /* Structure fields */
594 arg
->var
= strndup(str
, tmp
- str
);
595 if (arg
->var
== NULL
)
597 pr_debug("%s, ", arg
->var
);
598 fieldp
= &arg
->field
;
601 *fieldp
= zalloc(sizeof(struct perf_probe_arg_field
));
606 (*fieldp
)->ref
= false;
607 } else if (tmp
[1] == '>') {
609 (*fieldp
)->ref
= true;
611 semantic_error("Argument parse error: %s\n", str
);
615 tmp
= strpbrk(str
, "-.");
617 (*fieldp
)->name
= strndup(str
, tmp
- str
);
618 if ((*fieldp
)->name
== NULL
)
620 pr_debug("%s(%d), ", (*fieldp
)->name
, (*fieldp
)->ref
);
621 fieldp
= &(*fieldp
)->next
;
624 (*fieldp
)->name
= strdup(str
);
625 if ((*fieldp
)->name
== NULL
)
627 pr_debug("%s(%d)\n", (*fieldp
)->name
, (*fieldp
)->ref
);
629 /* If no name is specified, set the last field name */
631 arg
->name
= strdup((*fieldp
)->name
);
632 if (arg
->name
== NULL
)
638 /* Parse perf-probe event command */
639 int parse_perf_probe_command(const char *cmd
, struct perf_probe_event
*pev
)
642 int argc
, i
, ret
= 0;
644 argv
= argv_split(cmd
, &argc
);
646 pr_debug("Failed to split arguments.\n");
649 if (argc
- 1 > MAX_PROBE_ARGS
) {
650 semantic_error("Too many probe arguments (%d).\n", argc
- 1);
654 /* Parse probe point */
655 ret
= parse_perf_probe_point(argv
[0], pev
);
659 /* Copy arguments and ensure return probe has no C argument */
660 pev
->nargs
= argc
- 1;
661 pev
->args
= zalloc(sizeof(struct perf_probe_arg
) * pev
->nargs
);
662 if (pev
->args
== NULL
) {
666 for (i
= 0; i
< pev
->nargs
&& ret
>= 0; i
++) {
667 ret
= parse_perf_probe_arg(argv
[i
+ 1], &pev
->args
[i
]);
669 is_c_varname(pev
->args
[i
].var
) && pev
->point
.retprobe
) {
670 semantic_error("You can't specify local variable for"
681 /* Return true if this perf_probe_event requires debuginfo */
682 bool perf_probe_event_need_dwarf(struct perf_probe_event
*pev
)
686 if (pev
->point
.file
|| pev
->point
.line
|| pev
->point
.lazy_line
)
689 for (i
= 0; i
< pev
->nargs
; i
++)
690 if (is_c_varname(pev
->args
[i
].var
))
696 /* Parse kprobe_events event into struct probe_point */
697 int parse_kprobe_trace_command(const char *cmd
, struct kprobe_trace_event
*tev
)
699 struct kprobe_trace_point
*tp
= &tev
->point
;
705 pr_debug("Parsing kprobe_events: %s\n", cmd
);
706 argv
= argv_split(cmd
, &argc
);
708 pr_debug("Failed to split arguments.\n");
712 semantic_error("Too few probe arguments.\n");
717 /* Scan event and group name. */
718 ret
= sscanf(argv
[0], "%c:%a[^/ \t]/%a[^ \t]",
719 &pr
, (float *)(void *)&tev
->group
,
720 (float *)(void *)&tev
->event
);
722 semantic_error("Failed to parse event name: %s\n", argv
[0]);
726 pr_debug("Group:%s Event:%s probe:%c\n", tev
->group
, tev
->event
, pr
);
728 tp
->retprobe
= (pr
== 'r');
730 /* Scan function name and offset */
731 ret
= sscanf(argv
[1], "%a[^+]+%lu", (float *)(void *)&tp
->symbol
,
736 tev
->nargs
= argc
- 2;
737 tev
->args
= zalloc(sizeof(struct kprobe_trace_arg
) * tev
->nargs
);
738 if (tev
->args
== NULL
) {
742 for (i
= 0; i
< tev
->nargs
; i
++) {
743 p
= strchr(argv
[i
+ 2], '=');
744 if (p
) /* We don't need which register is assigned. */
748 tev
->args
[i
].name
= strdup(argv
[i
+ 2]);
749 /* TODO: parse regs and offset */
750 tev
->args
[i
].value
= strdup(p
);
751 if (tev
->args
[i
].name
== NULL
|| tev
->args
[i
].value
== NULL
) {
762 /* Compose only probe arg */
763 int synthesize_perf_probe_arg(struct perf_probe_arg
*pa
, char *buf
, size_t len
)
765 struct perf_probe_arg_field
*field
= pa
->field
;
769 if (pa
->name
&& pa
->var
)
770 ret
= e_snprintf(tmp
, len
, "%s=%s", pa
->name
, pa
->var
);
772 ret
= e_snprintf(tmp
, len
, "%s", pa
->name
? pa
->name
: pa
->var
);
779 ret
= e_snprintf(tmp
, len
, "%s%s", field
->ref
? "->" : ".",
789 ret
= e_snprintf(tmp
, len
, ":%s", pa
->type
);
798 pr_debug("Failed to synthesize perf probe argument: %s",
803 /* Compose only probe point (not argument) */
804 static char *synthesize_perf_probe_point(struct perf_probe_point
*pp
)
807 char offs
[32] = "", line
[32] = "", file
[32] = "";
810 buf
= zalloc(MAX_CMDLEN
);
816 ret
= e_snprintf(offs
, 32, "+%lu", pp
->offset
);
821 ret
= e_snprintf(line
, 32, ":%d", pp
->line
);
826 len
= strlen(pp
->file
) - 31;
829 tmp
= strchr(pp
->file
+ len
, '/');
831 tmp
= pp
->file
+ len
;
832 ret
= e_snprintf(file
, 32, "@%s", tmp
+ 1);
838 ret
= e_snprintf(buf
, MAX_CMDLEN
, "%s%s%s%s%s", pp
->function
,
839 offs
, pp
->retprobe
? "%return" : "", line
,
842 ret
= e_snprintf(buf
, MAX_CMDLEN
, "%s%s", file
, line
);
848 pr_debug("Failed to synthesize perf probe point: %s",
856 char *synthesize_perf_probe_command(struct perf_probe_event
*pev
)
861 buf
= synthesize_perf_probe_point(&pev
->point
);
866 for (i
= 0; i
< pev
->nargs
; i
++) {
867 ret
= e_snprintf(&buf
[len
], MAX_CMDLEN
- len
, " %s",
880 static int __synthesize_kprobe_trace_arg_ref(struct kprobe_trace_arg_ref
*ref
,
881 char **buf
, size_t *buflen
,
886 depth
= __synthesize_kprobe_trace_arg_ref(ref
->next
, buf
,
892 ret
= e_snprintf(*buf
, *buflen
, "%+ld(", ref
->offset
);
904 static int synthesize_kprobe_trace_arg(struct kprobe_trace_arg
*arg
,
905 char *buf
, size_t buflen
)
910 /* Argument name or separator */
912 ret
= e_snprintf(buf
, buflen
, " %s=", arg
->name
);
914 ret
= e_snprintf(buf
, buflen
, " ");
920 /* Dereferencing arguments */
922 depth
= __synthesize_kprobe_trace_arg_ref(arg
->ref
, &buf
,
928 /* Print argument value */
929 ret
= e_snprintf(buf
, buflen
, "%s", arg
->value
);
937 ret
= e_snprintf(buf
, buflen
, ")");
943 /* Print argument type */
945 ret
= e_snprintf(buf
, buflen
, ":%s", arg
->type
);
954 char *synthesize_kprobe_trace_command(struct kprobe_trace_event
*tev
)
956 struct kprobe_trace_point
*tp
= &tev
->point
;
960 buf
= zalloc(MAX_CMDLEN
);
964 len
= e_snprintf(buf
, MAX_CMDLEN
, "%c:%s/%s %s+%lu",
965 tp
->retprobe
? 'r' : 'p',
966 tev
->group
, tev
->event
,
967 tp
->symbol
, tp
->offset
);
971 for (i
= 0; i
< tev
->nargs
; i
++) {
972 ret
= synthesize_kprobe_trace_arg(&tev
->args
[i
], buf
+ len
,
985 int convert_to_perf_probe_event(struct kprobe_trace_event
*tev
,
986 struct perf_probe_event
*pev
)
991 /* Convert event/group name */
992 pev
->event
= strdup(tev
->event
);
993 pev
->group
= strdup(tev
->group
);
994 if (pev
->event
== NULL
|| pev
->group
== NULL
)
997 /* Convert trace_point to probe_point */
998 ret
= convert_to_perf_probe_point(&tev
->point
, &pev
->point
);
1002 /* Convert trace_arg to probe_arg */
1003 pev
->nargs
= tev
->nargs
;
1004 pev
->args
= zalloc(sizeof(struct perf_probe_arg
) * pev
->nargs
);
1005 if (pev
->args
== NULL
)
1007 for (i
= 0; i
< tev
->nargs
&& ret
>= 0; i
++) {
1008 if (tev
->args
[i
].name
)
1009 pev
->args
[i
].name
= strdup(tev
->args
[i
].name
);
1011 ret
= synthesize_kprobe_trace_arg(&tev
->args
[i
],
1013 pev
->args
[i
].name
= strdup(buf
);
1015 if (pev
->args
[i
].name
== NULL
&& ret
>= 0)
1020 clear_perf_probe_event(pev
);
1025 void clear_perf_probe_event(struct perf_probe_event
*pev
)
1027 struct perf_probe_point
*pp
= &pev
->point
;
1028 struct perf_probe_arg_field
*field
, *next
;
1040 free(pp
->lazy_line
);
1041 for (i
= 0; i
< pev
->nargs
; i
++) {
1042 if (pev
->args
[i
].name
)
1043 free(pev
->args
[i
].name
);
1044 if (pev
->args
[i
].var
)
1045 free(pev
->args
[i
].var
);
1046 if (pev
->args
[i
].type
)
1047 free(pev
->args
[i
].type
);
1048 field
= pev
->args
[i
].field
;
1059 memset(pev
, 0, sizeof(*pev
));
1062 void clear_kprobe_trace_event(struct kprobe_trace_event
*tev
)
1064 struct kprobe_trace_arg_ref
*ref
, *next
;
1071 if (tev
->point
.symbol
)
1072 free(tev
->point
.symbol
);
1073 for (i
= 0; i
< tev
->nargs
; i
++) {
1074 if (tev
->args
[i
].name
)
1075 free(tev
->args
[i
].name
);
1076 if (tev
->args
[i
].value
)
1077 free(tev
->args
[i
].value
);
1078 if (tev
->args
[i
].type
)
1079 free(tev
->args
[i
].type
);
1080 ref
= tev
->args
[i
].ref
;
1089 memset(tev
, 0, sizeof(*tev
));
1092 static int open_kprobe_events(bool readwrite
)
1095 const char *__debugfs
;
1098 __debugfs
= debugfs_find_mountpoint();
1099 if (__debugfs
== NULL
) {
1100 pr_warning("Debugfs is not mounted.\n");
1104 ret
= e_snprintf(buf
, PATH_MAX
, "%stracing/kprobe_events", __debugfs
);
1106 pr_debug("Opening %s write=%d\n", buf
, readwrite
);
1107 if (readwrite
&& !probe_event_dry_run
)
1108 ret
= open(buf
, O_RDWR
, O_APPEND
);
1110 ret
= open(buf
, O_RDONLY
, 0);
1114 if (errno
== ENOENT
)
1115 pr_warning("kprobe_events file does not exist - please"
1116 " rebuild kernel with CONFIG_KPROBE_EVENT.\n");
1118 pr_warning("Failed to open kprobe_events file: %s\n",
1124 /* Get raw string list of current kprobe_events */
1125 static struct strlist
*get_kprobe_trace_command_rawlist(int fd
)
1129 char buf
[MAX_CMDLEN
];
1133 sl
= strlist__new(true, NULL
);
1135 fp
= fdopen(dup(fd
), "r");
1137 p
= fgets(buf
, MAX_CMDLEN
, fp
);
1141 idx
= strlen(p
) - 1;
1144 ret
= strlist__add(sl
, buf
);
1146 pr_debug("strlist__add failed: %s\n", strerror(-ret
));
1147 strlist__delete(sl
);
1157 static int show_perf_probe_event(struct perf_probe_event
*pev
)
1163 /* Synthesize only event probe point */
1164 place
= synthesize_perf_probe_point(&pev
->point
);
1168 ret
= e_snprintf(buf
, 128, "%s:%s", pev
->group
, pev
->event
);
1172 printf(" %-20s (on %s", buf
, place
);
1174 if (pev
->nargs
> 0) {
1176 for (i
= 0; i
< pev
->nargs
; i
++) {
1177 ret
= synthesize_perf_probe_arg(&pev
->args
[i
],
1189 /* List up current perf-probe events */
1190 int show_perf_probe_events(void)
1193 struct kprobe_trace_event tev
;
1194 struct perf_probe_event pev
;
1195 struct strlist
*rawlist
;
1196 struct str_node
*ent
;
1199 ret
= init_vmlinux();
1203 memset(&tev
, 0, sizeof(tev
));
1204 memset(&pev
, 0, sizeof(pev
));
1206 fd
= open_kprobe_events(false);
1210 rawlist
= get_kprobe_trace_command_rawlist(fd
);
1215 strlist__for_each(ent
, rawlist
) {
1216 ret
= parse_kprobe_trace_command(ent
->s
, &tev
);
1218 ret
= convert_to_perf_probe_event(&tev
, &pev
);
1220 ret
= show_perf_probe_event(&pev
);
1222 clear_perf_probe_event(&pev
);
1223 clear_kprobe_trace_event(&tev
);
1227 strlist__delete(rawlist
);
1232 /* Get current perf-probe event names */
1233 static struct strlist
*get_kprobe_trace_event_names(int fd
, bool include_group
)
1236 struct strlist
*sl
, *rawlist
;
1237 struct str_node
*ent
;
1238 struct kprobe_trace_event tev
;
1241 memset(&tev
, 0, sizeof(tev
));
1243 rawlist
= get_kprobe_trace_command_rawlist(fd
);
1244 sl
= strlist__new(true, NULL
);
1245 strlist__for_each(ent
, rawlist
) {
1246 ret
= parse_kprobe_trace_command(ent
->s
, &tev
);
1249 if (include_group
) {
1250 ret
= e_snprintf(buf
, 128, "%s:%s", tev
.group
,
1253 ret
= strlist__add(sl
, buf
);
1255 ret
= strlist__add(sl
, tev
.event
);
1256 clear_kprobe_trace_event(&tev
);
1260 strlist__delete(rawlist
);
1263 strlist__delete(sl
);
1269 static int write_kprobe_trace_event(int fd
, struct kprobe_trace_event
*tev
)
1272 char *buf
= synthesize_kprobe_trace_command(tev
);
1275 pr_debug("Failed to synthesize kprobe trace event.\n");
1279 pr_debug("Writing event: %s\n", buf
);
1280 if (!probe_event_dry_run
) {
1281 ret
= write(fd
, buf
, strlen(buf
));
1283 pr_warning("Failed to write event: %s\n",
1290 static int get_new_event_name(char *buf
, size_t len
, const char *base
,
1291 struct strlist
*namelist
, bool allow_suffix
)
1296 ret
= e_snprintf(buf
, len
, "%s", base
);
1298 pr_debug("snprintf() failed: %s\n", strerror(-ret
));
1301 if (!strlist__has_entry(namelist
, buf
))
1304 if (!allow_suffix
) {
1305 pr_warning("Error: event \"%s\" already exists. "
1306 "(Use -f to force duplicates.)\n", base
);
1310 /* Try to add suffix */
1311 for (i
= 1; i
< MAX_EVENT_INDEX
; i
++) {
1312 ret
= e_snprintf(buf
, len
, "%s_%d", base
, i
);
1314 pr_debug("snprintf() failed: %s\n", strerror(-ret
));
1317 if (!strlist__has_entry(namelist
, buf
))
1320 if (i
== MAX_EVENT_INDEX
) {
1321 pr_warning("Too many events are on the same function.\n");
1328 static int __add_kprobe_trace_events(struct perf_probe_event
*pev
,
1329 struct kprobe_trace_event
*tevs
,
1330 int ntevs
, bool allow_suffix
)
1333 struct kprobe_trace_event
*tev
= NULL
;
1335 const char *event
, *group
;
1336 struct strlist
*namelist
;
1338 fd
= open_kprobe_events(true);
1341 /* Get current event names */
1342 namelist
= get_kprobe_trace_event_names(fd
, false);
1344 pr_debug("Failed to get current event list.\n");
1349 printf("Add new event%s\n", (ntevs
> 1) ? "s:" : ":");
1350 for (i
= 0; i
< ntevs
; i
++) {
1355 if (pev
->point
.function
)
1356 event
= pev
->point
.function
;
1358 event
= tev
->point
.symbol
;
1362 group
= PERFPROBE_GROUP
;
1364 /* Get an unused new event name */
1365 ret
= get_new_event_name(buf
, 64, event
,
1366 namelist
, allow_suffix
);
1371 tev
->event
= strdup(event
);
1372 tev
->group
= strdup(group
);
1373 if (tev
->event
== NULL
|| tev
->group
== NULL
) {
1377 ret
= write_kprobe_trace_event(fd
, tev
);
1380 /* Add added event name to namelist */
1381 strlist__add(namelist
, event
);
1383 /* Trick here - save current event/group */
1386 pev
->event
= tev
->event
;
1387 pev
->group
= tev
->group
;
1388 show_perf_probe_event(pev
);
1389 /* Trick here - restore current event/group */
1390 pev
->event
= (char *)event
;
1391 pev
->group
= (char *)group
;
1394 * Probes after the first probe which comes from same
1395 * user input are always allowed to add suffix, because
1396 * there might be several addresses corresponding to
1399 allow_suffix
= true;
1403 /* Show how to use the event. */
1404 printf("\nYou can now use it on all perf tools, such as:\n\n");
1405 printf("\tperf record -e %s:%s -aR sleep 1\n\n", tev
->group
,
1409 strlist__delete(namelist
);
1414 static int convert_to_kprobe_trace_events(struct perf_probe_event
*pev
,
1415 struct kprobe_trace_event
**tevs
,
1420 struct kprobe_trace_event
*tev
;
1422 /* Convert perf_probe_event with debuginfo */
1423 ret
= try_to_find_kprobe_trace_events(pev
, tevs
, max_tevs
);
1427 /* Allocate trace event buffer */
1428 tev
= *tevs
= zalloc(sizeof(struct kprobe_trace_event
));
1432 /* Copy parameters */
1433 tev
->point
.symbol
= strdup(pev
->point
.function
);
1434 if (tev
->point
.symbol
== NULL
) {
1438 tev
->point
.offset
= pev
->point
.offset
;
1439 tev
->nargs
= pev
->nargs
;
1441 tev
->args
= zalloc(sizeof(struct kprobe_trace_arg
)
1443 if (tev
->args
== NULL
) {
1447 for (i
= 0; i
< tev
->nargs
; i
++) {
1448 if (pev
->args
[i
].name
) {
1449 tev
->args
[i
].name
= strdup(pev
->args
[i
].name
);
1450 if (tev
->args
[i
].name
== NULL
) {
1455 tev
->args
[i
].value
= strdup(pev
->args
[i
].var
);
1456 if (tev
->args
[i
].value
== NULL
) {
1460 if (pev
->args
[i
].type
) {
1461 tev
->args
[i
].type
= strdup(pev
->args
[i
].type
);
1462 if (tev
->args
[i
].type
== NULL
) {
1470 /* Currently just checking function name from symbol map */
1471 sym
= map__find_symbol_by_name(machine
.vmlinux_maps
[MAP__FUNCTION
],
1472 tev
->point
.symbol
, NULL
);
1474 pr_warning("Kernel symbol \'%s\' not found.\n",
1482 clear_kprobe_trace_event(tev
);
1488 struct __event_package
{
1489 struct perf_probe_event
*pev
;
1490 struct kprobe_trace_event
*tevs
;
1494 int add_perf_probe_events(struct perf_probe_event
*pevs
, int npevs
,
1495 bool force_add
, int max_tevs
)
1498 struct __event_package
*pkgs
;
1500 pkgs
= zalloc(sizeof(struct __event_package
) * npevs
);
1504 /* Init vmlinux path */
1505 ret
= init_vmlinux();
1509 /* Loop 1: convert all events */
1510 for (i
= 0; i
< npevs
; i
++) {
1511 pkgs
[i
].pev
= &pevs
[i
];
1512 /* Convert with or without debuginfo */
1513 ret
= convert_to_kprobe_trace_events(pkgs
[i
].pev
,
1514 &pkgs
[i
].tevs
, max_tevs
);
1517 pkgs
[i
].ntevs
= ret
;
1520 /* Loop 2: add all events */
1521 for (i
= 0; i
< npevs
&& ret
>= 0; i
++)
1522 ret
= __add_kprobe_trace_events(pkgs
[i
].pev
, pkgs
[i
].tevs
,
1523 pkgs
[i
].ntevs
, force_add
);
1525 /* Loop 3: cleanup trace events */
1526 for (i
= 0; i
< npevs
; i
++)
1527 for (j
= 0; j
< pkgs
[i
].ntevs
; j
++)
1528 clear_kprobe_trace_event(&pkgs
[i
].tevs
[j
]);
1533 static int __del_trace_kprobe_event(int fd
, struct str_node
*ent
)
1539 /* Convert from perf-probe event to trace-kprobe event */
1540 ret
= e_snprintf(buf
, 128, "-:%s", ent
->s
);
1544 p
= strchr(buf
+ 2, ':');
1546 pr_debug("Internal error: %s should have ':' but not.\n",
1553 pr_debug("Writing event: %s\n", buf
);
1554 ret
= write(fd
, buf
, strlen(buf
));
1558 printf("Remove event: %s\n", ent
->s
);
1561 pr_warning("Failed to delete event: %s\n", strerror(-ret
));
1565 static int del_trace_kprobe_event(int fd
, const char *group
,
1566 const char *event
, struct strlist
*namelist
)
1569 struct str_node
*ent
, *n
;
1570 int found
= 0, ret
= 0;
1572 ret
= e_snprintf(buf
, 128, "%s:%s", group
, event
);
1574 pr_err("Failed to copy event.");
1578 if (strpbrk(buf
, "*?")) { /* Glob-exp */
1579 strlist__for_each_safe(ent
, n
, namelist
)
1580 if (strglobmatch(ent
->s
, buf
)) {
1582 ret
= __del_trace_kprobe_event(fd
, ent
);
1585 strlist__remove(namelist
, ent
);
1588 ent
= strlist__find(namelist
, buf
);
1591 ret
= __del_trace_kprobe_event(fd
, ent
);
1593 strlist__remove(namelist
, ent
);
1596 if (found
== 0 && ret
>= 0)
1597 pr_info("Info: Event \"%s\" does not exist.\n", buf
);
1602 int del_perf_probe_events(struct strlist
*dellist
)
1605 const char *group
, *event
;
1607 struct str_node
*ent
;
1608 struct strlist
*namelist
;
1610 fd
= open_kprobe_events(true);
1614 /* Get current event names */
1615 namelist
= get_kprobe_trace_event_names(fd
, true);
1616 if (namelist
== NULL
)
1619 strlist__for_each(ent
, dellist
) {
1620 str
= strdup(ent
->s
);
1625 pr_debug("Parsing: %s\n", str
);
1626 p
= strchr(str
, ':');
1635 pr_debug("Group: %s, Event: %s\n", group
, event
);
1636 ret
= del_trace_kprobe_event(fd
, group
, event
, namelist
);
1641 strlist__delete(namelist
);