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 map_groups kmap_groups
;
76 static struct map
*kmaps
[MAP__NR_TYPES
];
78 /* Initialize symbol maps and path of vmlinux */
79 static int init_vmlinux(void)
83 symbol_conf
.sort_by_name
= true;
84 if (symbol_conf
.vmlinux_name
== NULL
)
85 symbol_conf
.try_vmlinux_path
= true;
87 pr_debug("Use vmlinux: %s\n", symbol_conf
.vmlinux_name
);
90 pr_debug("Failed to init symbol map.\n");
94 map_groups__init(&kmap_groups
);
95 ret
= map_groups__create_kernel_maps(&kmap_groups
, kmaps
);
97 pr_debug("Failed to create kernel maps.\n");
101 pr_warning("Failed to init vmlinux path.\n");
106 static int open_vmlinux(void)
108 if (map__load(kmaps
[MAP__FUNCTION
], NULL
) < 0) {
109 pr_debug("Failed to load kernel map.\n");
112 pr_debug("Try to open %s\n", kmaps
[MAP__FUNCTION
]->dso
->long_name
);
113 return open(kmaps
[MAP__FUNCTION
]->dso
->long_name
, O_RDONLY
);
116 /* Convert trace point to probe point with debuginfo */
117 static int convert_to_perf_probe_point(struct kprobe_trace_point
*tp
,
118 struct perf_probe_point
*pp
)
121 int fd
, ret
= -ENOENT
;
123 sym
= map__find_symbol_by_name(kmaps
[MAP__FUNCTION
],
128 ret
= find_perf_probe_point(fd
,
129 sym
->start
+ tp
->offset
, pp
);
134 pr_debug("Failed to find corresponding probes from "
135 "debuginfo. Use kprobe event information.\n");
136 pp
->function
= strdup(tp
->symbol
);
137 if (pp
->function
== NULL
)
139 pp
->offset
= tp
->offset
;
141 pp
->retprobe
= tp
->retprobe
;
146 /* Try to find perf_probe_event with debuginfo */
147 static int try_to_find_kprobe_trace_events(struct perf_probe_event
*pev
,
148 struct kprobe_trace_event
**tevs
)
150 bool need_dwarf
= perf_probe_event_need_dwarf(pev
);
156 pr_warning("Failed to open debuginfo file.\n");
159 pr_debug("Could not open vmlinux. Try to use symbols.\n");
163 /* Searching trace events corresponding to probe event */
164 ntevs
= find_kprobe_trace_events(fd
, pev
, tevs
);
167 if (ntevs
> 0) { /* Succeeded to find trace events */
168 pr_debug("find %d kprobe_trace_events.\n", ntevs
);
172 if (ntevs
== 0) { /* No error but failed to find probe point. */
173 pr_warning("Probe point '%s' not found.\n",
174 synthesize_perf_probe_point(&pev
->point
));
177 /* Error path : ntevs < 0 */
180 pr_warning("No dwarf info found in the vmlinux - "
181 "please rebuild with CONFIG_DEBUG_INFO=y.\n");
184 pr_debug("An error occurred in debuginfo analysis."
185 " Try to use symbols.\n");
189 #define LINEBUF_SIZE 256
190 #define NR_ADDITIONAL_LINES 2
192 static int show_one_line(FILE *fp
, int l
, bool skip
, bool show_num
)
194 char buf
[LINEBUF_SIZE
];
195 const char *color
= PERF_COLOR_BLUE
;
197 if (fgets(buf
, LINEBUF_SIZE
, fp
) == NULL
)
201 fprintf(stdout
, "%7d %s", l
, buf
);
203 color_fprintf(stdout
, color
, " %s", buf
);
206 while (strlen(buf
) == LINEBUF_SIZE
- 1 &&
207 buf
[LINEBUF_SIZE
- 2] != '\n') {
208 if (fgets(buf
, LINEBUF_SIZE
, fp
) == NULL
)
212 fprintf(stdout
, "%s", buf
);
214 color_fprintf(stdout
, color
, "%s", buf
);
221 pr_warning("Source file is shorter than expected.\n");
223 pr_warning("File read error: %s\n", strerror(errno
));
229 * Show line-range always requires debuginfo to find source file and
232 int show_line_range(struct line_range
*lr
)
235 struct line_node
*ln
;
239 /* Search a line range */
240 ret
= init_vmlinux();
246 pr_warning("Failed to open debuginfo file.\n");
250 ret
= find_line_range(fd
, lr
);
253 pr_warning("Specified source line is not found.\n");
255 } else if (ret
< 0) {
256 pr_warning("Debuginfo analysis failed. (%d)\n", ret
);
263 fprintf(stdout
, "<%s:%d>\n", lr
->function
,
264 lr
->start
- lr
->offset
);
266 fprintf(stdout
, "<%s:%d>\n", lr
->file
, lr
->start
);
268 fp
= fopen(lr
->path
, "r");
270 pr_warning("Failed to open %s: %s\n", lr
->path
,
274 /* Skip to starting line number */
275 while (l
< lr
->start
&& ret
>= 0)
276 ret
= show_one_line(fp
, l
++, true, false);
280 list_for_each_entry(ln
, &lr
->line_list
, list
) {
281 while (ln
->line
> l
&& ret
>= 0)
282 ret
= show_one_line(fp
, (l
++) - lr
->offset
,
285 ret
= show_one_line(fp
, (l
++) - lr
->offset
,
291 if (lr
->end
== INT_MAX
)
292 lr
->end
= l
+ NR_ADDITIONAL_LINES
;
293 while (l
<= lr
->end
&& !feof(fp
) && ret
>= 0)
294 ret
= show_one_line(fp
, (l
++) - lr
->offset
, false, false);
300 #else /* !DWARF_SUPPORT */
302 static int convert_to_perf_probe_point(struct kprobe_trace_point
*tp
,
303 struct perf_probe_point
*pp
)
305 pp
->function
= strdup(tp
->symbol
);
306 if (pp
->function
== NULL
)
308 pp
->offset
= tp
->offset
;
309 pp
->retprobe
= tp
->retprobe
;
314 static int try_to_find_kprobe_trace_events(struct perf_probe_event
*pev
,
315 struct kprobe_trace_event
**tevs __unused
)
317 if (perf_probe_event_need_dwarf(pev
)) {
318 pr_warning("Debuginfo-analysis is not supported.\n");
324 int show_line_range(struct line_range
*lr __unused
)
326 pr_warning("Debuginfo-analysis is not supported.\n");
332 int parse_line_range_desc(const char *arg
, struct line_range
*lr
)
339 * FUNC[:SLN[+NUM|-ELN]]
341 ptr
= strchr(arg
, ':');
343 lr
->start
= (int)strtoul(ptr
+ 1, &tmp
, 0);
345 lr
->end
= lr
->start
+ (int)strtoul(tmp
+ 1, &tmp
, 0);
347 * Adjust the number of lines here.
348 * If the number of lines == 1, the
349 * the end of line should be equal to
352 } else if (*tmp
== '-')
353 lr
->end
= (int)strtoul(tmp
+ 1, &tmp
, 0);
356 pr_debug("Line range is %d to %d\n", lr
->start
, lr
->end
);
357 if (lr
->start
> lr
->end
) {
358 semantic_error("Start line must be smaller"
359 " than end line.\n");
363 semantic_error("Tailing with invalid character '%d'.\n",
367 tmp
= strndup(arg
, (ptr
- arg
));
376 if (strchr(tmp
, '.'))
384 /* Check the name is good for event/group */
385 static bool check_event_name(const char *name
)
387 if (!isalpha(*name
) && *name
!= '_')
389 while (*++name
!= '\0') {
390 if (!isalpha(*name
) && !isdigit(*name
) && *name
!= '_')
396 /* Parse probepoint definition. */
397 static int parse_perf_probe_point(char *arg
, struct perf_probe_event
*pev
)
399 struct perf_probe_point
*pp
= &pev
->point
;
404 * perf probe [EVENT=]SRC[:LN|;PTN]
405 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
407 * TODO:Group name support
410 ptr
= strpbrk(arg
, ";=@+%");
411 if (ptr
&& *ptr
== '=') { /* Event name */
414 if (strchr(arg
, ':')) {
415 semantic_error("Group name is not supported yet.\n");
418 if (!check_event_name(arg
)) {
419 semantic_error("%s is bad for event name -it must "
420 "follow C symbol-naming rule.\n", arg
);
423 pev
->event
= strdup(arg
);
424 if (pev
->event
== NULL
)
430 ptr
= strpbrk(arg
, ";:+@%");
440 /* Check arg is function or file and copy it */
441 if (strchr(tmp
, '.')) /* File */
446 /* Parse other options */
450 if (c
== ';') { /* Lazy pattern must be the last part */
451 pp
->lazy_line
= strdup(arg
);
452 if (pp
->lazy_line
== NULL
)
456 ptr
= strpbrk(arg
, ";:+@%");
462 case ':': /* Line number */
463 pp
->line
= strtoul(arg
, &tmp
, 0);
465 semantic_error("There is non-digit char"
466 " in line number.\n");
470 case '+': /* Byte offset from a symbol */
471 pp
->offset
= strtoul(arg
, &tmp
, 0);
473 semantic_error("There is non-digit character"
478 case '@': /* File name */
480 semantic_error("SRC@SRC is not allowed.\n");
483 pp
->file
= strdup(arg
);
484 if (pp
->file
== NULL
)
487 case '%': /* Probe places */
488 if (strcmp(arg
, "return") == 0) {
490 } else { /* Others not supported yet */
491 semantic_error("%%%s is not supported.\n", arg
);
495 default: /* Buggy case */
496 pr_err("This program has a bug at %s:%d.\n",
503 /* Exclusion check */
504 if (pp
->lazy_line
&& pp
->line
) {
505 semantic_error("Lazy pattern can't be used with line number.");
509 if (pp
->lazy_line
&& pp
->offset
) {
510 semantic_error("Lazy pattern can't be used with offset.");
514 if (pp
->line
&& pp
->offset
) {
515 semantic_error("Offset can't be used with line number.");
519 if (!pp
->line
&& !pp
->lazy_line
&& pp
->file
&& !pp
->function
) {
520 semantic_error("File always requires line number or "
525 if (pp
->offset
&& !pp
->function
) {
526 semantic_error("Offset requires an entry function.");
530 if (pp
->retprobe
&& !pp
->function
) {
531 semantic_error("Return probe requires an entry function.");
535 if ((pp
->offset
|| pp
->line
|| pp
->lazy_line
) && pp
->retprobe
) {
536 semantic_error("Offset/Line/Lazy pattern can't be used with "
541 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
542 pp
->function
, pp
->file
, pp
->line
, pp
->offset
, pp
->retprobe
,
547 /* Parse perf-probe event argument */
548 static int parse_perf_probe_arg(char *str
, struct perf_probe_arg
*arg
)
551 struct perf_probe_arg_field
**fieldp
;
553 pr_debug("parsing arg: %s into ", str
);
555 tmp
= strchr(str
, '=');
557 arg
->name
= strndup(str
, tmp
- str
);
558 if (arg
->name
== NULL
)
560 pr_debug("name:%s ", arg
->name
);
564 tmp
= strchr(str
, ':');
565 if (tmp
) { /* Type setting */
567 arg
->type
= strdup(tmp
+ 1);
568 if (arg
->type
== NULL
)
570 pr_debug("type:%s ", arg
->type
);
573 tmp
= strpbrk(str
, "-.");
574 if (!is_c_varname(str
) || !tmp
) {
575 /* A variable, register, symbol or special value */
576 arg
->var
= strdup(str
);
577 if (arg
->var
== NULL
)
579 pr_debug("%s\n", arg
->var
);
583 /* Structure fields */
584 arg
->var
= strndup(str
, tmp
- str
);
585 if (arg
->var
== NULL
)
587 pr_debug("%s, ", arg
->var
);
588 fieldp
= &arg
->field
;
591 *fieldp
= zalloc(sizeof(struct perf_probe_arg_field
));
596 (*fieldp
)->ref
= false;
597 } else if (tmp
[1] == '>') {
599 (*fieldp
)->ref
= true;
601 semantic_error("Argument parse error: %s\n", str
);
605 tmp
= strpbrk(str
, "-.");
607 (*fieldp
)->name
= strndup(str
, tmp
- str
);
608 if ((*fieldp
)->name
== NULL
)
610 pr_debug("%s(%d), ", (*fieldp
)->name
, (*fieldp
)->ref
);
611 fieldp
= &(*fieldp
)->next
;
614 (*fieldp
)->name
= strdup(str
);
615 if ((*fieldp
)->name
== NULL
)
617 pr_debug("%s(%d)\n", (*fieldp
)->name
, (*fieldp
)->ref
);
619 /* If no name is specified, set the last field name */
621 arg
->name
= strdup((*fieldp
)->name
);
622 if (arg
->name
== NULL
)
628 /* Parse perf-probe event command */
629 int parse_perf_probe_command(const char *cmd
, struct perf_probe_event
*pev
)
632 int argc
, i
, ret
= 0;
634 argv
= argv_split(cmd
, &argc
);
636 pr_debug("Failed to split arguments.\n");
639 if (argc
- 1 > MAX_PROBE_ARGS
) {
640 semantic_error("Too many probe arguments (%d).\n", argc
- 1);
644 /* Parse probe point */
645 ret
= parse_perf_probe_point(argv
[0], pev
);
649 /* Copy arguments and ensure return probe has no C argument */
650 pev
->nargs
= argc
- 1;
651 pev
->args
= zalloc(sizeof(struct perf_probe_arg
) * pev
->nargs
);
652 if (pev
->args
== NULL
) {
656 for (i
= 0; i
< pev
->nargs
&& ret
>= 0; i
++) {
657 ret
= parse_perf_probe_arg(argv
[i
+ 1], &pev
->args
[i
]);
659 is_c_varname(pev
->args
[i
].var
) && pev
->point
.retprobe
) {
660 semantic_error("You can't specify local variable for"
671 /* Return true if this perf_probe_event requires debuginfo */
672 bool perf_probe_event_need_dwarf(struct perf_probe_event
*pev
)
676 if (pev
->point
.file
|| pev
->point
.line
|| pev
->point
.lazy_line
)
679 for (i
= 0; i
< pev
->nargs
; i
++)
680 if (is_c_varname(pev
->args
[i
].var
))
686 /* Parse kprobe_events event into struct probe_point */
687 int parse_kprobe_trace_command(const char *cmd
, struct kprobe_trace_event
*tev
)
689 struct kprobe_trace_point
*tp
= &tev
->point
;
695 pr_debug("Parsing kprobe_events: %s\n", cmd
);
696 argv
= argv_split(cmd
, &argc
);
698 pr_debug("Failed to split arguments.\n");
702 semantic_error("Too few probe arguments.\n");
707 /* Scan event and group name. */
708 ret
= sscanf(argv
[0], "%c:%a[^/ \t]/%a[^ \t]",
709 &pr
, (float *)(void *)&tev
->group
,
710 (float *)(void *)&tev
->event
);
712 semantic_error("Failed to parse event name: %s\n", argv
[0]);
716 pr_debug("Group:%s Event:%s probe:%c\n", tev
->group
, tev
->event
, pr
);
718 tp
->retprobe
= (pr
== 'r');
720 /* Scan function name and offset */
721 ret
= sscanf(argv
[1], "%a[^+]+%lu", (float *)(void *)&tp
->symbol
,
726 tev
->nargs
= argc
- 2;
727 tev
->args
= zalloc(sizeof(struct kprobe_trace_arg
) * tev
->nargs
);
728 if (tev
->args
== NULL
) {
732 for (i
= 0; i
< tev
->nargs
; i
++) {
733 p
= strchr(argv
[i
+ 2], '=');
734 if (p
) /* We don't need which register is assigned. */
738 tev
->args
[i
].name
= strdup(argv
[i
+ 2]);
739 /* TODO: parse regs and offset */
740 tev
->args
[i
].value
= strdup(p
);
741 if (tev
->args
[i
].name
== NULL
|| tev
->args
[i
].value
== NULL
) {
752 /* Compose only probe arg */
753 int synthesize_perf_probe_arg(struct perf_probe_arg
*pa
, char *buf
, size_t len
)
755 struct perf_probe_arg_field
*field
= pa
->field
;
759 if (pa
->name
&& pa
->var
)
760 ret
= e_snprintf(tmp
, len
, "%s=%s", pa
->name
, pa
->var
);
762 ret
= e_snprintf(tmp
, len
, "%s", pa
->name
? pa
->name
: pa
->var
);
769 ret
= e_snprintf(tmp
, len
, "%s%s", field
->ref
? "->" : ".",
779 ret
= e_snprintf(tmp
, len
, ":%s", pa
->type
);
788 pr_debug("Failed to synthesize perf probe argument: %s",
793 /* Compose only probe point (not argument) */
794 static char *synthesize_perf_probe_point(struct perf_probe_point
*pp
)
797 char offs
[32] = "", line
[32] = "", file
[32] = "";
800 buf
= zalloc(MAX_CMDLEN
);
806 ret
= e_snprintf(offs
, 32, "+%lu", pp
->offset
);
811 ret
= e_snprintf(line
, 32, ":%d", pp
->line
);
816 len
= strlen(pp
->file
) - 31;
819 tmp
= strchr(pp
->file
+ len
, '/');
821 tmp
= pp
->file
+ len
;
822 ret
= e_snprintf(file
, 32, "@%s", tmp
+ 1);
828 ret
= e_snprintf(buf
, MAX_CMDLEN
, "%s%s%s%s%s", pp
->function
,
829 offs
, pp
->retprobe
? "%return" : "", line
,
832 ret
= e_snprintf(buf
, MAX_CMDLEN
, "%s%s", file
, line
);
838 pr_debug("Failed to synthesize perf probe point: %s",
846 char *synthesize_perf_probe_command(struct perf_probe_event
*pev
)
851 buf
= synthesize_perf_probe_point(&pev
->point
);
856 for (i
= 0; i
< pev
->nargs
; i
++) {
857 ret
= e_snprintf(&buf
[len
], MAX_CMDLEN
- len
, " %s",
870 static int __synthesize_kprobe_trace_arg_ref(struct kprobe_trace_arg_ref
*ref
,
871 char **buf
, size_t *buflen
,
876 depth
= __synthesize_kprobe_trace_arg_ref(ref
->next
, buf
,
882 ret
= e_snprintf(*buf
, *buflen
, "%+ld(", ref
->offset
);
894 static int synthesize_kprobe_trace_arg(struct kprobe_trace_arg
*arg
,
895 char *buf
, size_t buflen
)
900 /* Argument name or separator */
902 ret
= e_snprintf(buf
, buflen
, " %s=", arg
->name
);
904 ret
= e_snprintf(buf
, buflen
, " ");
910 /* Dereferencing arguments */
912 depth
= __synthesize_kprobe_trace_arg_ref(arg
->ref
, &buf
,
918 /* Print argument value */
919 ret
= e_snprintf(buf
, buflen
, "%s", arg
->value
);
927 ret
= e_snprintf(buf
, buflen
, ")");
933 /* Print argument type */
935 ret
= e_snprintf(buf
, buflen
, ":%s", arg
->type
);
944 char *synthesize_kprobe_trace_command(struct kprobe_trace_event
*tev
)
946 struct kprobe_trace_point
*tp
= &tev
->point
;
950 buf
= zalloc(MAX_CMDLEN
);
954 len
= e_snprintf(buf
, MAX_CMDLEN
, "%c:%s/%s %s+%lu",
955 tp
->retprobe
? 'r' : 'p',
956 tev
->group
, tev
->event
,
957 tp
->symbol
, tp
->offset
);
961 for (i
= 0; i
< tev
->nargs
; i
++) {
962 ret
= synthesize_kprobe_trace_arg(&tev
->args
[i
], buf
+ len
,
975 int convert_to_perf_probe_event(struct kprobe_trace_event
*tev
,
976 struct perf_probe_event
*pev
)
981 /* Convert event/group name */
982 pev
->event
= strdup(tev
->event
);
983 pev
->group
= strdup(tev
->group
);
984 if (pev
->event
== NULL
|| pev
->group
== NULL
)
987 /* Convert trace_point to probe_point */
988 ret
= convert_to_perf_probe_point(&tev
->point
, &pev
->point
);
992 /* Convert trace_arg to probe_arg */
993 pev
->nargs
= tev
->nargs
;
994 pev
->args
= zalloc(sizeof(struct perf_probe_arg
) * pev
->nargs
);
995 if (pev
->args
== NULL
)
997 for (i
= 0; i
< tev
->nargs
&& ret
>= 0; i
++) {
998 if (tev
->args
[i
].name
)
999 pev
->args
[i
].name
= strdup(tev
->args
[i
].name
);
1001 ret
= synthesize_kprobe_trace_arg(&tev
->args
[i
],
1003 pev
->args
[i
].name
= strdup(buf
);
1005 if (pev
->args
[i
].name
== NULL
&& ret
>= 0)
1010 clear_perf_probe_event(pev
);
1015 void clear_perf_probe_event(struct perf_probe_event
*pev
)
1017 struct perf_probe_point
*pp
= &pev
->point
;
1018 struct perf_probe_arg_field
*field
, *next
;
1030 free(pp
->lazy_line
);
1031 for (i
= 0; i
< pev
->nargs
; i
++) {
1032 if (pev
->args
[i
].name
)
1033 free(pev
->args
[i
].name
);
1034 if (pev
->args
[i
].var
)
1035 free(pev
->args
[i
].var
);
1036 if (pev
->args
[i
].type
)
1037 free(pev
->args
[i
].type
);
1038 field
= pev
->args
[i
].field
;
1049 memset(pev
, 0, sizeof(*pev
));
1052 void clear_kprobe_trace_event(struct kprobe_trace_event
*tev
)
1054 struct kprobe_trace_arg_ref
*ref
, *next
;
1061 if (tev
->point
.symbol
)
1062 free(tev
->point
.symbol
);
1063 for (i
= 0; i
< tev
->nargs
; i
++) {
1064 if (tev
->args
[i
].name
)
1065 free(tev
->args
[i
].name
);
1066 if (tev
->args
[i
].value
)
1067 free(tev
->args
[i
].value
);
1068 if (tev
->args
[i
].type
)
1069 free(tev
->args
[i
].type
);
1070 ref
= tev
->args
[i
].ref
;
1079 memset(tev
, 0, sizeof(*tev
));
1082 static int open_kprobe_events(bool readwrite
)
1085 const char *__debugfs
;
1088 __debugfs
= debugfs_find_mountpoint();
1089 if (__debugfs
== NULL
) {
1090 pr_warning("Debugfs is not mounted.\n");
1094 ret
= e_snprintf(buf
, PATH_MAX
, "%stracing/kprobe_events", __debugfs
);
1096 pr_debug("Opening %s write=%d\n", buf
, readwrite
);
1097 if (readwrite
&& !probe_event_dry_run
)
1098 ret
= open(buf
, O_RDWR
, O_APPEND
);
1100 ret
= open(buf
, O_RDONLY
, 0);
1104 if (errno
== ENOENT
)
1105 pr_warning("kprobe_events file does not exist - please"
1106 " rebuild kernel with CONFIG_KPROBE_EVENT.\n");
1108 pr_warning("Failed to open kprobe_events file: %s\n",
1114 /* Get raw string list of current kprobe_events */
1115 static struct strlist
*get_kprobe_trace_command_rawlist(int fd
)
1119 char buf
[MAX_CMDLEN
];
1123 sl
= strlist__new(true, NULL
);
1125 fp
= fdopen(dup(fd
), "r");
1127 p
= fgets(buf
, MAX_CMDLEN
, fp
);
1131 idx
= strlen(p
) - 1;
1134 ret
= strlist__add(sl
, buf
);
1136 pr_debug("strlist__add failed: %s\n", strerror(-ret
));
1137 strlist__delete(sl
);
1147 static int show_perf_probe_event(struct perf_probe_event
*pev
)
1153 /* Synthesize only event probe point */
1154 place
= synthesize_perf_probe_point(&pev
->point
);
1158 ret
= e_snprintf(buf
, 128, "%s:%s", pev
->group
, pev
->event
);
1162 printf(" %-20s (on %s", buf
, place
);
1164 if (pev
->nargs
> 0) {
1166 for (i
= 0; i
< pev
->nargs
; i
++) {
1167 ret
= synthesize_perf_probe_arg(&pev
->args
[i
],
1179 /* List up current perf-probe events */
1180 int show_perf_probe_events(void)
1183 struct kprobe_trace_event tev
;
1184 struct perf_probe_event pev
;
1185 struct strlist
*rawlist
;
1186 struct str_node
*ent
;
1189 ret
= init_vmlinux();
1193 memset(&tev
, 0, sizeof(tev
));
1194 memset(&pev
, 0, sizeof(pev
));
1196 fd
= open_kprobe_events(false);
1200 rawlist
= get_kprobe_trace_command_rawlist(fd
);
1205 strlist__for_each(ent
, rawlist
) {
1206 ret
= parse_kprobe_trace_command(ent
->s
, &tev
);
1208 ret
= convert_to_perf_probe_event(&tev
, &pev
);
1210 ret
= show_perf_probe_event(&pev
);
1212 clear_perf_probe_event(&pev
);
1213 clear_kprobe_trace_event(&tev
);
1217 strlist__delete(rawlist
);
1222 /* Get current perf-probe event names */
1223 static struct strlist
*get_kprobe_trace_event_names(int fd
, bool include_group
)
1226 struct strlist
*sl
, *rawlist
;
1227 struct str_node
*ent
;
1228 struct kprobe_trace_event tev
;
1231 memset(&tev
, 0, sizeof(tev
));
1233 rawlist
= get_kprobe_trace_command_rawlist(fd
);
1234 sl
= strlist__new(true, NULL
);
1235 strlist__for_each(ent
, rawlist
) {
1236 ret
= parse_kprobe_trace_command(ent
->s
, &tev
);
1239 if (include_group
) {
1240 ret
= e_snprintf(buf
, 128, "%s:%s", tev
.group
,
1243 ret
= strlist__add(sl
, buf
);
1245 ret
= strlist__add(sl
, tev
.event
);
1246 clear_kprobe_trace_event(&tev
);
1250 strlist__delete(rawlist
);
1253 strlist__delete(sl
);
1259 static int write_kprobe_trace_event(int fd
, struct kprobe_trace_event
*tev
)
1262 char *buf
= synthesize_kprobe_trace_command(tev
);
1265 pr_debug("Failed to synthesize kprobe trace event.\n");
1269 pr_debug("Writing event: %s\n", buf
);
1270 if (!probe_event_dry_run
) {
1271 ret
= write(fd
, buf
, strlen(buf
));
1273 pr_warning("Failed to write event: %s\n",
1280 static int get_new_event_name(char *buf
, size_t len
, const char *base
,
1281 struct strlist
*namelist
, bool allow_suffix
)
1286 ret
= e_snprintf(buf
, len
, "%s", base
);
1288 pr_debug("snprintf() failed: %s\n", strerror(-ret
));
1291 if (!strlist__has_entry(namelist
, buf
))
1294 if (!allow_suffix
) {
1295 pr_warning("Error: event \"%s\" already exists. "
1296 "(Use -f to force duplicates.)\n", base
);
1300 /* Try to add suffix */
1301 for (i
= 1; i
< MAX_EVENT_INDEX
; i
++) {
1302 ret
= e_snprintf(buf
, len
, "%s_%d", base
, i
);
1304 pr_debug("snprintf() failed: %s\n", strerror(-ret
));
1307 if (!strlist__has_entry(namelist
, buf
))
1310 if (i
== MAX_EVENT_INDEX
) {
1311 pr_warning("Too many events are on the same function.\n");
1318 static int __add_kprobe_trace_events(struct perf_probe_event
*pev
,
1319 struct kprobe_trace_event
*tevs
,
1320 int ntevs
, bool allow_suffix
)
1323 struct kprobe_trace_event
*tev
= NULL
;
1325 const char *event
, *group
;
1326 struct strlist
*namelist
;
1328 fd
= open_kprobe_events(true);
1331 /* Get current event names */
1332 namelist
= get_kprobe_trace_event_names(fd
, false);
1334 pr_debug("Failed to get current event list.\n");
1339 printf("Add new event%s\n", (ntevs
> 1) ? "s:" : ":");
1340 for (i
= 0; i
< ntevs
; i
++) {
1345 if (pev
->point
.function
)
1346 event
= pev
->point
.function
;
1348 event
= tev
->point
.symbol
;
1352 group
= PERFPROBE_GROUP
;
1354 /* Get an unused new event name */
1355 ret
= get_new_event_name(buf
, 64, event
,
1356 namelist
, allow_suffix
);
1361 tev
->event
= strdup(event
);
1362 tev
->group
= strdup(group
);
1363 if (tev
->event
== NULL
|| tev
->group
== NULL
) {
1367 ret
= write_kprobe_trace_event(fd
, tev
);
1370 /* Add added event name to namelist */
1371 strlist__add(namelist
, event
);
1373 /* Trick here - save current event/group */
1376 pev
->event
= tev
->event
;
1377 pev
->group
= tev
->group
;
1378 show_perf_probe_event(pev
);
1379 /* Trick here - restore current event/group */
1380 pev
->event
= (char *)event
;
1381 pev
->group
= (char *)group
;
1384 * Probes after the first probe which comes from same
1385 * user input are always allowed to add suffix, because
1386 * there might be several addresses corresponding to
1389 allow_suffix
= true;
1393 /* Show how to use the event. */
1394 printf("\nYou can now use it on all perf tools, such as:\n\n");
1395 printf("\tperf record -e %s:%s -aR sleep 1\n\n", tev
->group
,
1399 strlist__delete(namelist
);
1404 static int convert_to_kprobe_trace_events(struct perf_probe_event
*pev
,
1405 struct kprobe_trace_event
**tevs
)
1409 struct kprobe_trace_event
*tev
;
1411 /* Convert perf_probe_event with debuginfo */
1412 ret
= try_to_find_kprobe_trace_events(pev
, tevs
);
1416 /* Allocate trace event buffer */
1417 tev
= *tevs
= zalloc(sizeof(struct kprobe_trace_event
));
1421 /* Copy parameters */
1422 tev
->point
.symbol
= strdup(pev
->point
.function
);
1423 if (tev
->point
.symbol
== NULL
) {
1427 tev
->point
.offset
= pev
->point
.offset
;
1428 tev
->nargs
= pev
->nargs
;
1430 tev
->args
= zalloc(sizeof(struct kprobe_trace_arg
)
1432 if (tev
->args
== NULL
) {
1436 for (i
= 0; i
< tev
->nargs
; i
++) {
1437 if (pev
->args
[i
].name
) {
1438 tev
->args
[i
].name
= strdup(pev
->args
[i
].name
);
1439 if (tev
->args
[i
].name
== NULL
) {
1444 tev
->args
[i
].value
= strdup(pev
->args
[i
].var
);
1445 if (tev
->args
[i
].value
== NULL
) {
1449 if (pev
->args
[i
].type
) {
1450 tev
->args
[i
].type
= strdup(pev
->args
[i
].type
);
1451 if (tev
->args
[i
].type
== NULL
) {
1459 /* Currently just checking function name from symbol map */
1460 sym
= map__find_symbol_by_name(kmaps
[MAP__FUNCTION
],
1461 tev
->point
.symbol
, NULL
);
1463 pr_warning("Kernel symbol \'%s\' not found.\n",
1471 clear_kprobe_trace_event(tev
);
1477 struct __event_package
{
1478 struct perf_probe_event
*pev
;
1479 struct kprobe_trace_event
*tevs
;
1483 int add_perf_probe_events(struct perf_probe_event
*pevs
, int npevs
,
1487 struct __event_package
*pkgs
;
1489 pkgs
= zalloc(sizeof(struct __event_package
) * npevs
);
1493 /* Init vmlinux path */
1494 ret
= init_vmlinux();
1498 /* Loop 1: convert all events */
1499 for (i
= 0; i
< npevs
; i
++) {
1500 pkgs
[i
].pev
= &pevs
[i
];
1501 /* Convert with or without debuginfo */
1502 ret
= convert_to_kprobe_trace_events(pkgs
[i
].pev
,
1506 pkgs
[i
].ntevs
= ret
;
1509 /* Loop 2: add all events */
1510 for (i
= 0; i
< npevs
&& ret
>= 0; i
++)
1511 ret
= __add_kprobe_trace_events(pkgs
[i
].pev
, pkgs
[i
].tevs
,
1512 pkgs
[i
].ntevs
, force_add
);
1514 /* Loop 3: cleanup trace events */
1515 for (i
= 0; i
< npevs
; i
++)
1516 for (j
= 0; j
< pkgs
[i
].ntevs
; j
++)
1517 clear_kprobe_trace_event(&pkgs
[i
].tevs
[j
]);
1522 static int __del_trace_kprobe_event(int fd
, struct str_node
*ent
)
1528 /* Convert from perf-probe event to trace-kprobe event */
1529 ret
= e_snprintf(buf
, 128, "-:%s", ent
->s
);
1533 p
= strchr(buf
+ 2, ':');
1535 pr_debug("Internal error: %s should have ':' but not.\n",
1542 pr_debug("Writing event: %s\n", buf
);
1543 ret
= write(fd
, buf
, strlen(buf
));
1547 printf("Remove event: %s\n", ent
->s
);
1550 pr_warning("Failed to delete event: %s\n", strerror(-ret
));
1554 static int del_trace_kprobe_event(int fd
, const char *group
,
1555 const char *event
, struct strlist
*namelist
)
1558 struct str_node
*ent
, *n
;
1559 int found
= 0, ret
= 0;
1561 ret
= e_snprintf(buf
, 128, "%s:%s", group
, event
);
1563 pr_err("Failed to copy event.");
1567 if (strpbrk(buf
, "*?")) { /* Glob-exp */
1568 strlist__for_each_safe(ent
, n
, namelist
)
1569 if (strglobmatch(ent
->s
, buf
)) {
1571 ret
= __del_trace_kprobe_event(fd
, ent
);
1574 strlist__remove(namelist
, ent
);
1577 ent
= strlist__find(namelist
, buf
);
1580 ret
= __del_trace_kprobe_event(fd
, ent
);
1582 strlist__remove(namelist
, ent
);
1585 if (found
== 0 && ret
>= 0)
1586 pr_info("Info: Event \"%s\" does not exist.\n", buf
);
1591 int del_perf_probe_events(struct strlist
*dellist
)
1594 const char *group
, *event
;
1596 struct str_node
*ent
;
1597 struct strlist
*namelist
;
1599 fd
= open_kprobe_events(true);
1603 /* Get current event names */
1604 namelist
= get_kprobe_trace_event_names(fd
, true);
1605 if (namelist
== NULL
)
1608 strlist__for_each(ent
, dellist
) {
1609 str
= strdup(ent
->s
);
1614 pr_debug("Parsing: %s\n", str
);
1615 p
= strchr(str
, ':');
1624 pr_debug("Group: %s, Event: %s\n", group
, event
);
1625 ret
= del_trace_kprobe_event(fd
, group
, event
, namelist
);
1630 strlist__delete(namelist
);