4445a1e7052f4e4b1b1ed3c883baea00a9a4f044
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");
199 * Find a src file from a DWARF tag path. Prepend optional source path prefix
200 * and chop off leading directories that do not exist. Result is passed back as
201 * a newly allocated path on success.
202 * Return 0 if file was found and readable, -errno otherwise.
204 static int get_real_path(const char *raw_path
, const char *comp_dir
,
207 const char *prefix
= symbol_conf
.source_prefix
;
210 if (raw_path
[0] != '/' && comp_dir
)
211 /* If not an absolute path, try to use comp_dir */
214 if (access(raw_path
, R_OK
) == 0) {
215 *new_path
= strdup(raw_path
);
222 *new_path
= malloc((strlen(prefix
) + strlen(raw_path
) + 2));
227 sprintf(*new_path
, "%s/%s", prefix
, raw_path
);
229 if (access(*new_path
, R_OK
) == 0)
232 if (!symbol_conf
.source_prefix
)
233 /* In case of searching comp_dir, don't retry */
241 raw_path
= strchr(++raw_path
, '/');
257 #define LINEBUF_SIZE 256
258 #define NR_ADDITIONAL_LINES 2
260 static int show_one_line(FILE *fp
, int l
, bool skip
, bool show_num
)
262 char buf
[LINEBUF_SIZE
];
263 const char *color
= PERF_COLOR_BLUE
;
265 if (fgets(buf
, LINEBUF_SIZE
, fp
) == NULL
)
269 fprintf(stdout
, "%7d %s", l
, buf
);
271 color_fprintf(stdout
, color
, " %s", buf
);
274 while (strlen(buf
) == LINEBUF_SIZE
- 1 &&
275 buf
[LINEBUF_SIZE
- 2] != '\n') {
276 if (fgets(buf
, LINEBUF_SIZE
, fp
) == NULL
)
280 fprintf(stdout
, "%s", buf
);
282 color_fprintf(stdout
, color
, "%s", buf
);
289 pr_warning("Source file is shorter than expected.\n");
291 pr_warning("File read error: %s\n", strerror(errno
));
297 * Show line-range always requires debuginfo to find source file and
300 int show_line_range(struct line_range
*lr
)
303 struct line_node
*ln
;
308 /* Search a line range */
309 ret
= init_vmlinux();
315 pr_warning("Failed to open debuginfo file.\n");
319 ret
= find_line_range(fd
, lr
);
322 pr_warning("Specified source line is not found.\n");
324 } else if (ret
< 0) {
325 pr_warning("Debuginfo analysis failed. (%d)\n", ret
);
329 /* Convert source file path */
331 ret
= get_real_path(tmp
, lr
->comp_dir
, &lr
->path
);
332 free(tmp
); /* Free old path */
334 pr_warning("Failed to find source file. (%d)\n", ret
);
341 fprintf(stdout
, "<%s:%d>\n", lr
->function
,
342 lr
->start
- lr
->offset
);
344 fprintf(stdout
, "<%s:%d>\n", lr
->file
, lr
->start
);
346 fp
= fopen(lr
->path
, "r");
348 pr_warning("Failed to open %s: %s\n", lr
->path
,
352 /* Skip to starting line number */
353 while (l
< lr
->start
&& ret
>= 0)
354 ret
= show_one_line(fp
, l
++, true, false);
358 list_for_each_entry(ln
, &lr
->line_list
, list
) {
359 while (ln
->line
> l
&& ret
>= 0)
360 ret
= show_one_line(fp
, (l
++) - lr
->offset
,
363 ret
= show_one_line(fp
, (l
++) - lr
->offset
,
369 if (lr
->end
== INT_MAX
)
370 lr
->end
= l
+ NR_ADDITIONAL_LINES
;
371 while (l
<= lr
->end
&& !feof(fp
) && ret
>= 0)
372 ret
= show_one_line(fp
, (l
++) - lr
->offset
, false, false);
378 #else /* !DWARF_SUPPORT */
380 static int convert_to_perf_probe_point(struct kprobe_trace_point
*tp
,
381 struct perf_probe_point
*pp
)
383 pp
->function
= strdup(tp
->symbol
);
384 if (pp
->function
== NULL
)
386 pp
->offset
= tp
->offset
;
387 pp
->retprobe
= tp
->retprobe
;
392 static int try_to_find_kprobe_trace_events(struct perf_probe_event
*pev
,
393 struct kprobe_trace_event
**tevs __unused
,
394 int max_tevs __unused
)
396 if (perf_probe_event_need_dwarf(pev
)) {
397 pr_warning("Debuginfo-analysis is not supported.\n");
403 int show_line_range(struct line_range
*lr __unused
)
405 pr_warning("Debuginfo-analysis is not supported.\n");
411 int parse_line_range_desc(const char *arg
, struct line_range
*lr
)
418 * FUNC[:SLN[+NUM|-ELN]]
420 ptr
= strchr(arg
, ':');
422 lr
->start
= (int)strtoul(ptr
+ 1, &tmp
, 0);
424 lr
->end
= lr
->start
+ (int)strtoul(tmp
+ 1, &tmp
, 0);
426 * Adjust the number of lines here.
427 * If the number of lines == 1, the
428 * the end of line should be equal to
431 } else if (*tmp
== '-')
432 lr
->end
= (int)strtoul(tmp
+ 1, &tmp
, 0);
435 pr_debug("Line range is %d to %d\n", lr
->start
, lr
->end
);
436 if (lr
->start
> lr
->end
) {
437 semantic_error("Start line must be smaller"
438 " than end line.\n");
442 semantic_error("Tailing with invalid character '%d'.\n",
446 tmp
= strndup(arg
, (ptr
- arg
));
455 if (strchr(tmp
, '.'))
463 /* Check the name is good for event/group */
464 static bool check_event_name(const char *name
)
466 if (!isalpha(*name
) && *name
!= '_')
468 while (*++name
!= '\0') {
469 if (!isalpha(*name
) && !isdigit(*name
) && *name
!= '_')
475 /* Parse probepoint definition. */
476 static int parse_perf_probe_point(char *arg
, struct perf_probe_event
*pev
)
478 struct perf_probe_point
*pp
= &pev
->point
;
483 * perf probe [EVENT=]SRC[:LN|;PTN]
484 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
486 * TODO:Group name support
489 ptr
= strpbrk(arg
, ";=@+%");
490 if (ptr
&& *ptr
== '=') { /* Event name */
493 if (strchr(arg
, ':')) {
494 semantic_error("Group name is not supported yet.\n");
497 if (!check_event_name(arg
)) {
498 semantic_error("%s is bad for event name -it must "
499 "follow C symbol-naming rule.\n", arg
);
502 pev
->event
= strdup(arg
);
503 if (pev
->event
== NULL
)
509 ptr
= strpbrk(arg
, ";:+@%");
519 /* Check arg is function or file and copy it */
520 if (strchr(tmp
, '.')) /* File */
525 /* Parse other options */
529 if (c
== ';') { /* Lazy pattern must be the last part */
530 pp
->lazy_line
= strdup(arg
);
531 if (pp
->lazy_line
== NULL
)
535 ptr
= strpbrk(arg
, ";:+@%");
541 case ':': /* Line number */
542 pp
->line
= strtoul(arg
, &tmp
, 0);
544 semantic_error("There is non-digit char"
545 " in line number.\n");
549 case '+': /* Byte offset from a symbol */
550 pp
->offset
= strtoul(arg
, &tmp
, 0);
552 semantic_error("There is non-digit character"
557 case '@': /* File name */
559 semantic_error("SRC@SRC is not allowed.\n");
562 pp
->file
= strdup(arg
);
563 if (pp
->file
== NULL
)
566 case '%': /* Probe places */
567 if (strcmp(arg
, "return") == 0) {
569 } else { /* Others not supported yet */
570 semantic_error("%%%s is not supported.\n", arg
);
574 default: /* Buggy case */
575 pr_err("This program has a bug at %s:%d.\n",
582 /* Exclusion check */
583 if (pp
->lazy_line
&& pp
->line
) {
584 semantic_error("Lazy pattern can't be used with line number.");
588 if (pp
->lazy_line
&& pp
->offset
) {
589 semantic_error("Lazy pattern can't be used with offset.");
593 if (pp
->line
&& pp
->offset
) {
594 semantic_error("Offset can't be used with line number.");
598 if (!pp
->line
&& !pp
->lazy_line
&& pp
->file
&& !pp
->function
) {
599 semantic_error("File always requires line number or "
604 if (pp
->offset
&& !pp
->function
) {
605 semantic_error("Offset requires an entry function.");
609 if (pp
->retprobe
&& !pp
->function
) {
610 semantic_error("Return probe requires an entry function.");
614 if ((pp
->offset
|| pp
->line
|| pp
->lazy_line
) && pp
->retprobe
) {
615 semantic_error("Offset/Line/Lazy pattern can't be used with "
620 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
621 pp
->function
, pp
->file
, pp
->line
, pp
->offset
, pp
->retprobe
,
626 /* Parse perf-probe event argument */
627 static int parse_perf_probe_arg(char *str
, struct perf_probe_arg
*arg
)
629 char *tmp
, *goodname
;
630 struct perf_probe_arg_field
**fieldp
;
632 pr_debug("parsing arg: %s into ", str
);
634 tmp
= strchr(str
, '=');
636 arg
->name
= strndup(str
, tmp
- str
);
637 if (arg
->name
== NULL
)
639 pr_debug("name:%s ", arg
->name
);
643 tmp
= strchr(str
, ':');
644 if (tmp
) { /* Type setting */
646 arg
->type
= strdup(tmp
+ 1);
647 if (arg
->type
== NULL
)
649 pr_debug("type:%s ", arg
->type
);
652 tmp
= strpbrk(str
, "-.[");
653 if (!is_c_varname(str
) || !tmp
) {
654 /* A variable, register, symbol or special value */
655 arg
->var
= strdup(str
);
656 if (arg
->var
== NULL
)
658 pr_debug("%s\n", arg
->var
);
662 /* Structure fields or array element */
663 arg
->var
= strndup(str
, tmp
- str
);
664 if (arg
->var
== NULL
)
667 pr_debug("%s, ", arg
->var
);
668 fieldp
= &arg
->field
;
671 *fieldp
= zalloc(sizeof(struct perf_probe_arg_field
));
674 if (*tmp
== '[') { /* Array */
676 (*fieldp
)->index
= strtol(str
+ 1, &tmp
, 0);
677 (*fieldp
)->ref
= true;
678 if (*tmp
!= ']' || tmp
== str
+ 1) {
679 semantic_error("Array index must be a"
686 } else { /* Structure */
689 (*fieldp
)->ref
= false;
690 } else if (tmp
[1] == '>') {
692 (*fieldp
)->ref
= true;
694 semantic_error("Argument parse error: %s\n",
698 tmp
= strpbrk(str
, "-.[");
701 (*fieldp
)->name
= strndup(str
, tmp
- str
);
702 if ((*fieldp
)->name
== NULL
)
705 goodname
= (*fieldp
)->name
;
706 pr_debug("%s(%d), ", (*fieldp
)->name
, (*fieldp
)->ref
);
707 fieldp
= &(*fieldp
)->next
;
710 (*fieldp
)->name
= strdup(str
);
711 if ((*fieldp
)->name
== NULL
)
714 goodname
= (*fieldp
)->name
;
715 pr_debug("%s(%d)\n", (*fieldp
)->name
, (*fieldp
)->ref
);
717 /* If no name is specified, set the last field name (not array index)*/
719 arg
->name
= strdup(goodname
);
720 if (arg
->name
== NULL
)
726 /* Parse perf-probe event command */
727 int parse_perf_probe_command(const char *cmd
, struct perf_probe_event
*pev
)
730 int argc
, i
, ret
= 0;
732 argv
= argv_split(cmd
, &argc
);
734 pr_debug("Failed to split arguments.\n");
737 if (argc
- 1 > MAX_PROBE_ARGS
) {
738 semantic_error("Too many probe arguments (%d).\n", argc
- 1);
742 /* Parse probe point */
743 ret
= parse_perf_probe_point(argv
[0], pev
);
747 /* Copy arguments and ensure return probe has no C argument */
748 pev
->nargs
= argc
- 1;
749 pev
->args
= zalloc(sizeof(struct perf_probe_arg
) * pev
->nargs
);
750 if (pev
->args
== NULL
) {
754 for (i
= 0; i
< pev
->nargs
&& ret
>= 0; i
++) {
755 ret
= parse_perf_probe_arg(argv
[i
+ 1], &pev
->args
[i
]);
757 is_c_varname(pev
->args
[i
].var
) && pev
->point
.retprobe
) {
758 semantic_error("You can't specify local variable for"
769 /* Return true if this perf_probe_event requires debuginfo */
770 bool perf_probe_event_need_dwarf(struct perf_probe_event
*pev
)
774 if (pev
->point
.file
|| pev
->point
.line
|| pev
->point
.lazy_line
)
777 for (i
= 0; i
< pev
->nargs
; i
++)
778 if (is_c_varname(pev
->args
[i
].var
))
784 /* Parse kprobe_events event into struct probe_point */
785 int parse_kprobe_trace_command(const char *cmd
, struct kprobe_trace_event
*tev
)
787 struct kprobe_trace_point
*tp
= &tev
->point
;
793 pr_debug("Parsing kprobe_events: %s\n", cmd
);
794 argv
= argv_split(cmd
, &argc
);
796 pr_debug("Failed to split arguments.\n");
800 semantic_error("Too few probe arguments.\n");
805 /* Scan event and group name. */
806 ret
= sscanf(argv
[0], "%c:%a[^/ \t]/%a[^ \t]",
807 &pr
, (float *)(void *)&tev
->group
,
808 (float *)(void *)&tev
->event
);
810 semantic_error("Failed to parse event name: %s\n", argv
[0]);
814 pr_debug("Group:%s Event:%s probe:%c\n", tev
->group
, tev
->event
, pr
);
816 tp
->retprobe
= (pr
== 'r');
818 /* Scan function name and offset */
819 ret
= sscanf(argv
[1], "%a[^+]+%lu", (float *)(void *)&tp
->symbol
,
824 tev
->nargs
= argc
- 2;
825 tev
->args
= zalloc(sizeof(struct kprobe_trace_arg
) * tev
->nargs
);
826 if (tev
->args
== NULL
) {
830 for (i
= 0; i
< tev
->nargs
; i
++) {
831 p
= strchr(argv
[i
+ 2], '=');
832 if (p
) /* We don't need which register is assigned. */
836 tev
->args
[i
].name
= strdup(argv
[i
+ 2]);
837 /* TODO: parse regs and offset */
838 tev
->args
[i
].value
= strdup(p
);
839 if (tev
->args
[i
].name
== NULL
|| tev
->args
[i
].value
== NULL
) {
850 /* Compose only probe arg */
851 int synthesize_perf_probe_arg(struct perf_probe_arg
*pa
, char *buf
, size_t len
)
853 struct perf_probe_arg_field
*field
= pa
->field
;
857 if (pa
->name
&& pa
->var
)
858 ret
= e_snprintf(tmp
, len
, "%s=%s", pa
->name
, pa
->var
);
860 ret
= e_snprintf(tmp
, len
, "%s", pa
->name
? pa
->name
: pa
->var
);
867 if (field
->name
[0] == '[')
868 ret
= e_snprintf(tmp
, len
, "%s", field
->name
);
870 ret
= e_snprintf(tmp
, len
, "%s%s",
871 field
->ref
? "->" : ".", field
->name
);
880 ret
= e_snprintf(tmp
, len
, ":%s", pa
->type
);
889 pr_debug("Failed to synthesize perf probe argument: %s",
894 /* Compose only probe point (not argument) */
895 static char *synthesize_perf_probe_point(struct perf_probe_point
*pp
)
898 char offs
[32] = "", line
[32] = "", file
[32] = "";
901 buf
= zalloc(MAX_CMDLEN
);
907 ret
= e_snprintf(offs
, 32, "+%lu", pp
->offset
);
912 ret
= e_snprintf(line
, 32, ":%d", pp
->line
);
917 len
= strlen(pp
->file
) - 31;
920 tmp
= strchr(pp
->file
+ len
, '/');
922 tmp
= pp
->file
+ len
;
923 ret
= e_snprintf(file
, 32, "@%s", tmp
+ 1);
929 ret
= e_snprintf(buf
, MAX_CMDLEN
, "%s%s%s%s%s", pp
->function
,
930 offs
, pp
->retprobe
? "%return" : "", line
,
933 ret
= e_snprintf(buf
, MAX_CMDLEN
, "%s%s", file
, line
);
939 pr_debug("Failed to synthesize perf probe point: %s",
947 char *synthesize_perf_probe_command(struct perf_probe_event
*pev
)
952 buf
= synthesize_perf_probe_point(&pev
->point
);
957 for (i
= 0; i
< pev
->nargs
; i
++) {
958 ret
= e_snprintf(&buf
[len
], MAX_CMDLEN
- len
, " %s",
971 static int __synthesize_kprobe_trace_arg_ref(struct kprobe_trace_arg_ref
*ref
,
972 char **buf
, size_t *buflen
,
977 depth
= __synthesize_kprobe_trace_arg_ref(ref
->next
, buf
,
983 ret
= e_snprintf(*buf
, *buflen
, "%+ld(", ref
->offset
);
995 static int synthesize_kprobe_trace_arg(struct kprobe_trace_arg
*arg
,
996 char *buf
, size_t buflen
)
998 struct kprobe_trace_arg_ref
*ref
= arg
->ref
;
1002 /* Argument name or separator */
1004 ret
= e_snprintf(buf
, buflen
, " %s=", arg
->name
);
1006 ret
= e_snprintf(buf
, buflen
, " ");
1012 /* Special case: @XXX */
1013 if (arg
->value
[0] == '@' && arg
->ref
)
1016 /* Dereferencing arguments */
1018 depth
= __synthesize_kprobe_trace_arg_ref(ref
, &buf
,
1024 /* Print argument value */
1025 if (arg
->value
[0] == '@' && arg
->ref
)
1026 ret
= e_snprintf(buf
, buflen
, "%s%+ld", arg
->value
,
1029 ret
= e_snprintf(buf
, buflen
, "%s", arg
->value
);
1037 ret
= e_snprintf(buf
, buflen
, ")");
1043 /* Print argument type */
1045 ret
= e_snprintf(buf
, buflen
, ":%s", arg
->type
);
1054 char *synthesize_kprobe_trace_command(struct kprobe_trace_event
*tev
)
1056 struct kprobe_trace_point
*tp
= &tev
->point
;
1060 buf
= zalloc(MAX_CMDLEN
);
1064 len
= e_snprintf(buf
, MAX_CMDLEN
, "%c:%s/%s %s+%lu",
1065 tp
->retprobe
? 'r' : 'p',
1066 tev
->group
, tev
->event
,
1067 tp
->symbol
, tp
->offset
);
1071 for (i
= 0; i
< tev
->nargs
; i
++) {
1072 ret
= synthesize_kprobe_trace_arg(&tev
->args
[i
], buf
+ len
,
1085 int convert_to_perf_probe_event(struct kprobe_trace_event
*tev
,
1086 struct perf_probe_event
*pev
)
1091 /* Convert event/group name */
1092 pev
->event
= strdup(tev
->event
);
1093 pev
->group
= strdup(tev
->group
);
1094 if (pev
->event
== NULL
|| pev
->group
== NULL
)
1097 /* Convert trace_point to probe_point */
1098 ret
= convert_to_perf_probe_point(&tev
->point
, &pev
->point
);
1102 /* Convert trace_arg to probe_arg */
1103 pev
->nargs
= tev
->nargs
;
1104 pev
->args
= zalloc(sizeof(struct perf_probe_arg
) * pev
->nargs
);
1105 if (pev
->args
== NULL
)
1107 for (i
= 0; i
< tev
->nargs
&& ret
>= 0; i
++) {
1108 if (tev
->args
[i
].name
)
1109 pev
->args
[i
].name
= strdup(tev
->args
[i
].name
);
1111 ret
= synthesize_kprobe_trace_arg(&tev
->args
[i
],
1113 pev
->args
[i
].name
= strdup(buf
);
1115 if (pev
->args
[i
].name
== NULL
&& ret
>= 0)
1120 clear_perf_probe_event(pev
);
1125 void clear_perf_probe_event(struct perf_probe_event
*pev
)
1127 struct perf_probe_point
*pp
= &pev
->point
;
1128 struct perf_probe_arg_field
*field
, *next
;
1140 free(pp
->lazy_line
);
1141 for (i
= 0; i
< pev
->nargs
; i
++) {
1142 if (pev
->args
[i
].name
)
1143 free(pev
->args
[i
].name
);
1144 if (pev
->args
[i
].var
)
1145 free(pev
->args
[i
].var
);
1146 if (pev
->args
[i
].type
)
1147 free(pev
->args
[i
].type
);
1148 field
= pev
->args
[i
].field
;
1159 memset(pev
, 0, sizeof(*pev
));
1162 void clear_kprobe_trace_event(struct kprobe_trace_event
*tev
)
1164 struct kprobe_trace_arg_ref
*ref
, *next
;
1171 if (tev
->point
.symbol
)
1172 free(tev
->point
.symbol
);
1173 for (i
= 0; i
< tev
->nargs
; i
++) {
1174 if (tev
->args
[i
].name
)
1175 free(tev
->args
[i
].name
);
1176 if (tev
->args
[i
].value
)
1177 free(tev
->args
[i
].value
);
1178 if (tev
->args
[i
].type
)
1179 free(tev
->args
[i
].type
);
1180 ref
= tev
->args
[i
].ref
;
1189 memset(tev
, 0, sizeof(*tev
));
1192 static int open_kprobe_events(bool readwrite
)
1195 const char *__debugfs
;
1198 __debugfs
= debugfs_find_mountpoint();
1199 if (__debugfs
== NULL
) {
1200 pr_warning("Debugfs is not mounted.\n");
1204 ret
= e_snprintf(buf
, PATH_MAX
, "%stracing/kprobe_events", __debugfs
);
1206 pr_debug("Opening %s write=%d\n", buf
, readwrite
);
1207 if (readwrite
&& !probe_event_dry_run
)
1208 ret
= open(buf
, O_RDWR
, O_APPEND
);
1210 ret
= open(buf
, O_RDONLY
, 0);
1214 if (errno
== ENOENT
)
1215 pr_warning("kprobe_events file does not exist - please"
1216 " rebuild kernel with CONFIG_KPROBE_EVENT.\n");
1218 pr_warning("Failed to open kprobe_events file: %s\n",
1224 /* Get raw string list of current kprobe_events */
1225 static struct strlist
*get_kprobe_trace_command_rawlist(int fd
)
1229 char buf
[MAX_CMDLEN
];
1233 sl
= strlist__new(true, NULL
);
1235 fp
= fdopen(dup(fd
), "r");
1237 p
= fgets(buf
, MAX_CMDLEN
, fp
);
1241 idx
= strlen(p
) - 1;
1244 ret
= strlist__add(sl
, buf
);
1246 pr_debug("strlist__add failed: %s\n", strerror(-ret
));
1247 strlist__delete(sl
);
1257 static int show_perf_probe_event(struct perf_probe_event
*pev
)
1263 /* Synthesize only event probe point */
1264 place
= synthesize_perf_probe_point(&pev
->point
);
1268 ret
= e_snprintf(buf
, 128, "%s:%s", pev
->group
, pev
->event
);
1272 printf(" %-20s (on %s", buf
, place
);
1274 if (pev
->nargs
> 0) {
1276 for (i
= 0; i
< pev
->nargs
; i
++) {
1277 ret
= synthesize_perf_probe_arg(&pev
->args
[i
],
1289 /* List up current perf-probe events */
1290 int show_perf_probe_events(void)
1293 struct kprobe_trace_event tev
;
1294 struct perf_probe_event pev
;
1295 struct strlist
*rawlist
;
1296 struct str_node
*ent
;
1299 ret
= init_vmlinux();
1303 memset(&tev
, 0, sizeof(tev
));
1304 memset(&pev
, 0, sizeof(pev
));
1306 fd
= open_kprobe_events(false);
1310 rawlist
= get_kprobe_trace_command_rawlist(fd
);
1315 strlist__for_each(ent
, rawlist
) {
1316 ret
= parse_kprobe_trace_command(ent
->s
, &tev
);
1318 ret
= convert_to_perf_probe_event(&tev
, &pev
);
1320 ret
= show_perf_probe_event(&pev
);
1322 clear_perf_probe_event(&pev
);
1323 clear_kprobe_trace_event(&tev
);
1327 strlist__delete(rawlist
);
1332 /* Get current perf-probe event names */
1333 static struct strlist
*get_kprobe_trace_event_names(int fd
, bool include_group
)
1336 struct strlist
*sl
, *rawlist
;
1337 struct str_node
*ent
;
1338 struct kprobe_trace_event tev
;
1341 memset(&tev
, 0, sizeof(tev
));
1343 rawlist
= get_kprobe_trace_command_rawlist(fd
);
1344 sl
= strlist__new(true, NULL
);
1345 strlist__for_each(ent
, rawlist
) {
1346 ret
= parse_kprobe_trace_command(ent
->s
, &tev
);
1349 if (include_group
) {
1350 ret
= e_snprintf(buf
, 128, "%s:%s", tev
.group
,
1353 ret
= strlist__add(sl
, buf
);
1355 ret
= strlist__add(sl
, tev
.event
);
1356 clear_kprobe_trace_event(&tev
);
1360 strlist__delete(rawlist
);
1363 strlist__delete(sl
);
1369 static int write_kprobe_trace_event(int fd
, struct kprobe_trace_event
*tev
)
1372 char *buf
= synthesize_kprobe_trace_command(tev
);
1375 pr_debug("Failed to synthesize kprobe trace event.\n");
1379 pr_debug("Writing event: %s\n", buf
);
1380 if (!probe_event_dry_run
) {
1381 ret
= write(fd
, buf
, strlen(buf
));
1383 pr_warning("Failed to write event: %s\n",
1390 static int get_new_event_name(char *buf
, size_t len
, const char *base
,
1391 struct strlist
*namelist
, bool allow_suffix
)
1396 ret
= e_snprintf(buf
, len
, "%s", base
);
1398 pr_debug("snprintf() failed: %s\n", strerror(-ret
));
1401 if (!strlist__has_entry(namelist
, buf
))
1404 if (!allow_suffix
) {
1405 pr_warning("Error: event \"%s\" already exists. "
1406 "(Use -f to force duplicates.)\n", base
);
1410 /* Try to add suffix */
1411 for (i
= 1; i
< MAX_EVENT_INDEX
; i
++) {
1412 ret
= e_snprintf(buf
, len
, "%s_%d", base
, i
);
1414 pr_debug("snprintf() failed: %s\n", strerror(-ret
));
1417 if (!strlist__has_entry(namelist
, buf
))
1420 if (i
== MAX_EVENT_INDEX
) {
1421 pr_warning("Too many events are on the same function.\n");
1428 static int __add_kprobe_trace_events(struct perf_probe_event
*pev
,
1429 struct kprobe_trace_event
*tevs
,
1430 int ntevs
, bool allow_suffix
)
1433 struct kprobe_trace_event
*tev
= NULL
;
1435 const char *event
, *group
;
1436 struct strlist
*namelist
;
1438 fd
= open_kprobe_events(true);
1441 /* Get current event names */
1442 namelist
= get_kprobe_trace_event_names(fd
, false);
1444 pr_debug("Failed to get current event list.\n");
1449 printf("Add new event%s\n", (ntevs
> 1) ? "s:" : ":");
1450 for (i
= 0; i
< ntevs
; i
++) {
1455 if (pev
->point
.function
)
1456 event
= pev
->point
.function
;
1458 event
= tev
->point
.symbol
;
1462 group
= PERFPROBE_GROUP
;
1464 /* Get an unused new event name */
1465 ret
= get_new_event_name(buf
, 64, event
,
1466 namelist
, allow_suffix
);
1471 tev
->event
= strdup(event
);
1472 tev
->group
= strdup(group
);
1473 if (tev
->event
== NULL
|| tev
->group
== NULL
) {
1477 ret
= write_kprobe_trace_event(fd
, tev
);
1480 /* Add added event name to namelist */
1481 strlist__add(namelist
, event
);
1483 /* Trick here - save current event/group */
1486 pev
->event
= tev
->event
;
1487 pev
->group
= tev
->group
;
1488 show_perf_probe_event(pev
);
1489 /* Trick here - restore current event/group */
1490 pev
->event
= (char *)event
;
1491 pev
->group
= (char *)group
;
1494 * Probes after the first probe which comes from same
1495 * user input are always allowed to add suffix, because
1496 * there might be several addresses corresponding to
1499 allow_suffix
= true;
1503 /* Show how to use the event. */
1504 printf("\nYou can now use it on all perf tools, such as:\n\n");
1505 printf("\tperf record -e %s:%s -aR sleep 1\n\n", tev
->group
,
1509 strlist__delete(namelist
);
1514 static int convert_to_kprobe_trace_events(struct perf_probe_event
*pev
,
1515 struct kprobe_trace_event
**tevs
,
1520 struct kprobe_trace_event
*tev
;
1522 /* Convert perf_probe_event with debuginfo */
1523 ret
= try_to_find_kprobe_trace_events(pev
, tevs
, max_tevs
);
1527 /* Allocate trace event buffer */
1528 tev
= *tevs
= zalloc(sizeof(struct kprobe_trace_event
));
1532 /* Copy parameters */
1533 tev
->point
.symbol
= strdup(pev
->point
.function
);
1534 if (tev
->point
.symbol
== NULL
) {
1538 tev
->point
.offset
= pev
->point
.offset
;
1539 tev
->nargs
= pev
->nargs
;
1541 tev
->args
= zalloc(sizeof(struct kprobe_trace_arg
)
1543 if (tev
->args
== NULL
) {
1547 for (i
= 0; i
< tev
->nargs
; i
++) {
1548 if (pev
->args
[i
].name
) {
1549 tev
->args
[i
].name
= strdup(pev
->args
[i
].name
);
1550 if (tev
->args
[i
].name
== NULL
) {
1555 tev
->args
[i
].value
= strdup(pev
->args
[i
].var
);
1556 if (tev
->args
[i
].value
== NULL
) {
1560 if (pev
->args
[i
].type
) {
1561 tev
->args
[i
].type
= strdup(pev
->args
[i
].type
);
1562 if (tev
->args
[i
].type
== NULL
) {
1570 /* Currently just checking function name from symbol map */
1571 sym
= map__find_symbol_by_name(machine
.vmlinux_maps
[MAP__FUNCTION
],
1572 tev
->point
.symbol
, NULL
);
1574 pr_warning("Kernel symbol \'%s\' not found.\n",
1582 clear_kprobe_trace_event(tev
);
1588 struct __event_package
{
1589 struct perf_probe_event
*pev
;
1590 struct kprobe_trace_event
*tevs
;
1594 int add_perf_probe_events(struct perf_probe_event
*pevs
, int npevs
,
1595 bool force_add
, int max_tevs
)
1598 struct __event_package
*pkgs
;
1600 pkgs
= zalloc(sizeof(struct __event_package
) * npevs
);
1604 /* Init vmlinux path */
1605 ret
= init_vmlinux();
1609 /* Loop 1: convert all events */
1610 for (i
= 0; i
< npevs
; i
++) {
1611 pkgs
[i
].pev
= &pevs
[i
];
1612 /* Convert with or without debuginfo */
1613 ret
= convert_to_kprobe_trace_events(pkgs
[i
].pev
,
1614 &pkgs
[i
].tevs
, max_tevs
);
1617 pkgs
[i
].ntevs
= ret
;
1620 /* Loop 2: add all events */
1621 for (i
= 0; i
< npevs
&& ret
>= 0; i
++)
1622 ret
= __add_kprobe_trace_events(pkgs
[i
].pev
, pkgs
[i
].tevs
,
1623 pkgs
[i
].ntevs
, force_add
);
1625 /* Loop 3: cleanup trace events */
1626 for (i
= 0; i
< npevs
; i
++)
1627 for (j
= 0; j
< pkgs
[i
].ntevs
; j
++)
1628 clear_kprobe_trace_event(&pkgs
[i
].tevs
[j
]);
1633 static int __del_trace_kprobe_event(int fd
, struct str_node
*ent
)
1639 /* Convert from perf-probe event to trace-kprobe event */
1640 ret
= e_snprintf(buf
, 128, "-:%s", ent
->s
);
1644 p
= strchr(buf
+ 2, ':');
1646 pr_debug("Internal error: %s should have ':' but not.\n",
1653 pr_debug("Writing event: %s\n", buf
);
1654 ret
= write(fd
, buf
, strlen(buf
));
1658 printf("Remove event: %s\n", ent
->s
);
1661 pr_warning("Failed to delete event: %s\n", strerror(-ret
));
1665 static int del_trace_kprobe_event(int fd
, const char *group
,
1666 const char *event
, struct strlist
*namelist
)
1669 struct str_node
*ent
, *n
;
1670 int found
= 0, ret
= 0;
1672 ret
= e_snprintf(buf
, 128, "%s:%s", group
, event
);
1674 pr_err("Failed to copy event.");
1678 if (strpbrk(buf
, "*?")) { /* Glob-exp */
1679 strlist__for_each_safe(ent
, n
, namelist
)
1680 if (strglobmatch(ent
->s
, buf
)) {
1682 ret
= __del_trace_kprobe_event(fd
, ent
);
1685 strlist__remove(namelist
, ent
);
1688 ent
= strlist__find(namelist
, buf
);
1691 ret
= __del_trace_kprobe_event(fd
, ent
);
1693 strlist__remove(namelist
, ent
);
1696 if (found
== 0 && ret
>= 0)
1697 pr_info("Info: Event \"%s\" does not exist.\n", buf
);
1702 int del_perf_probe_events(struct strlist
*dellist
)
1705 const char *group
, *event
;
1707 struct str_node
*ent
;
1708 struct strlist
*namelist
;
1710 fd
= open_kprobe_events(true);
1714 /* Get current event names */
1715 namelist
= get_kprobe_trace_event_names(fd
, true);
1716 if (namelist
== NULL
)
1719 strlist__for_each(ent
, dellist
) {
1720 str
= strdup(ent
->s
);
1725 pr_debug("Parsing: %s\n", str
);
1726 p
= strchr(str
, ':');
1735 pr_debug("Group: %s, Event: %s\n", group
, event
);
1736 ret
= del_trace_kprobe_event(fd
, group
, event
, namelist
);
1741 strlist__delete(namelist
);