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 */
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
);
124 * Convert trace point to probe point with debuginfo
125 * Currently only handles kprobes.
127 static int kprobe_convert_to_perf_probe(struct probe_trace_point
*tp
,
128 struct perf_probe_point
*pp
)
131 int fd
, ret
= -ENOENT
;
133 sym
= map__find_symbol_by_name(machine
.vmlinux_maps
[MAP__FUNCTION
],
138 ret
= find_perf_probe_point(fd
,
139 sym
->start
+ tp
->offset
, pp
);
144 pr_debug("Failed to find corresponding probes from "
145 "debuginfo. Use kprobe event information.\n");
146 pp
->function
= strdup(tp
->symbol
);
147 if (pp
->function
== NULL
)
149 pp
->offset
= tp
->offset
;
151 pp
->retprobe
= tp
->retprobe
;
156 /* Try to find perf_probe_event with debuginfo */
157 static int try_to_find_probe_trace_events(struct perf_probe_event
*pev
,
158 struct probe_trace_event
**tevs
,
161 bool need_dwarf
= perf_probe_event_need_dwarf(pev
);
167 pr_warning("Failed to open debuginfo file.\n");
170 pr_debug("Could not open vmlinux. Try to use symbols.\n");
174 /* Searching trace events corresponding to probe event */
175 ntevs
= find_probe_trace_events(fd
, pev
, tevs
, max_tevs
);
178 if (ntevs
> 0) { /* Succeeded to find trace events */
179 pr_debug("find %d probe_trace_events.\n", ntevs
);
183 if (ntevs
== 0) { /* No error but failed to find probe point. */
184 pr_warning("Probe point '%s' not found.\n",
185 synthesize_perf_probe_point(&pev
->point
));
188 /* Error path : ntevs < 0 */
189 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs
);
190 if (ntevs
== -EBADF
) {
191 pr_warning("Warning: No dwarf info found in the vmlinux - "
192 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
194 pr_debug("Trying to use symbols.\nn");
202 * Find a src file from a DWARF tag path. Prepend optional source path prefix
203 * and chop off leading directories that do not exist. Result is passed back as
204 * a newly allocated path on success.
205 * Return 0 if file was found and readable, -errno otherwise.
207 static int get_real_path(const char *raw_path
, const char *comp_dir
,
210 const char *prefix
= symbol_conf
.source_prefix
;
213 if (raw_path
[0] != '/' && comp_dir
)
214 /* If not an absolute path, try to use comp_dir */
217 if (access(raw_path
, R_OK
) == 0) {
218 *new_path
= strdup(raw_path
);
225 *new_path
= malloc((strlen(prefix
) + strlen(raw_path
) + 2));
230 sprintf(*new_path
, "%s/%s", prefix
, raw_path
);
232 if (access(*new_path
, R_OK
) == 0)
235 if (!symbol_conf
.source_prefix
)
236 /* In case of searching comp_dir, don't retry */
244 raw_path
= strchr(++raw_path
, '/');
260 #define LINEBUF_SIZE 256
261 #define NR_ADDITIONAL_LINES 2
263 static int show_one_line(FILE *fp
, int l
, bool skip
, bool show_num
)
265 char buf
[LINEBUF_SIZE
];
266 const char *color
= PERF_COLOR_BLUE
;
268 if (fgets(buf
, LINEBUF_SIZE
, fp
) == NULL
)
272 fprintf(stdout
, "%7d %s", l
, buf
);
274 color_fprintf(stdout
, color
, " %s", buf
);
277 while (strlen(buf
) == LINEBUF_SIZE
- 1 &&
278 buf
[LINEBUF_SIZE
- 2] != '\n') {
279 if (fgets(buf
, LINEBUF_SIZE
, fp
) == NULL
)
283 fprintf(stdout
, "%s", buf
);
285 color_fprintf(stdout
, color
, "%s", buf
);
292 pr_warning("Source file is shorter than expected.\n");
294 pr_warning("File read error: %s\n", strerror(errno
));
300 * Show line-range always requires debuginfo to find source file and
303 int show_line_range(struct line_range
*lr
)
306 struct line_node
*ln
;
311 /* Search a line range */
312 ret
= init_vmlinux();
318 pr_warning("Failed to open debuginfo file.\n");
322 ret
= find_line_range(fd
, lr
);
325 pr_warning("Specified source line is not found.\n");
327 } else if (ret
< 0) {
328 pr_warning("Debuginfo analysis failed. (%d)\n", ret
);
332 /* Convert source file path */
334 ret
= get_real_path(tmp
, lr
->comp_dir
, &lr
->path
);
335 free(tmp
); /* Free old path */
337 pr_warning("Failed to find source file. (%d)\n", ret
);
344 fprintf(stdout
, "<%s:%d>\n", lr
->function
,
345 lr
->start
- lr
->offset
);
347 fprintf(stdout
, "<%s:%d>\n", lr
->file
, lr
->start
);
349 fp
= fopen(lr
->path
, "r");
351 pr_warning("Failed to open %s: %s\n", lr
->path
,
355 /* Skip to starting line number */
356 while (l
< lr
->start
&& ret
>= 0)
357 ret
= show_one_line(fp
, l
++, true, false);
361 list_for_each_entry(ln
, &lr
->line_list
, list
) {
362 while (ln
->line
> l
&& ret
>= 0)
363 ret
= show_one_line(fp
, (l
++) - lr
->offset
,
366 ret
= show_one_line(fp
, (l
++) - lr
->offset
,
372 if (lr
->end
== INT_MAX
)
373 lr
->end
= l
+ NR_ADDITIONAL_LINES
;
374 while (l
<= lr
->end
&& !feof(fp
) && ret
>= 0)
375 ret
= show_one_line(fp
, (l
++) - lr
->offset
, false, false);
381 #else /* !DWARF_SUPPORT */
383 static int kprobe_convert_to_perf_probe(struct probe_trace_point
*tp
,
384 struct perf_probe_point
*pp
)
386 pp
->function
= strdup(tp
->symbol
);
387 if (pp
->function
== NULL
)
389 pp
->offset
= tp
->offset
;
390 pp
->retprobe
= tp
->retprobe
;
395 static int try_to_find_probe_trace_events(struct perf_probe_event
*pev
,
396 struct probe_trace_event
**tevs __unused
,
397 int max_tevs __unused
)
399 if (perf_probe_event_need_dwarf(pev
)) {
400 pr_warning("Debuginfo-analysis is not supported.\n");
406 int show_line_range(struct line_range
*lr __unused
)
408 pr_warning("Debuginfo-analysis is not supported.\n");
414 int parse_line_range_desc(const char *arg
, struct line_range
*lr
)
421 * FUNC[:SLN[+NUM|-ELN]]
423 ptr
= strchr(arg
, ':');
425 lr
->start
= (int)strtoul(ptr
+ 1, &tmp
, 0);
427 lr
->end
= lr
->start
+ (int)strtoul(tmp
+ 1, &tmp
, 0);
429 * Adjust the number of lines here.
430 * If the number of lines == 1, the
431 * the end of line should be equal to
434 } else if (*tmp
== '-')
435 lr
->end
= (int)strtoul(tmp
+ 1, &tmp
, 0);
438 pr_debug("Line range is %d to %d\n", lr
->start
, lr
->end
);
439 if (lr
->start
> lr
->end
) {
440 semantic_error("Start line must be smaller"
441 " than end line.\n");
445 semantic_error("Tailing with invalid character '%d'.\n",
449 tmp
= strndup(arg
, (ptr
- arg
));
458 if (strchr(tmp
, '.'))
466 /* Check the name is good for event/group */
467 static bool check_event_name(const char *name
)
469 if (!isalpha(*name
) && *name
!= '_')
471 while (*++name
!= '\0') {
472 if (!isalpha(*name
) && !isdigit(*name
) && *name
!= '_')
478 /* Parse probepoint definition. */
479 static int parse_perf_probe_point(char *arg
, struct perf_probe_event
*pev
)
481 struct perf_probe_point
*pp
= &pev
->point
;
486 * perf probe [EVENT=]SRC[:LN|;PTN]
487 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
489 * TODO:Group name support
492 ptr
= strpbrk(arg
, ";=@+%");
493 if (ptr
&& *ptr
== '=') { /* Event name */
496 if (strchr(arg
, ':')) {
497 semantic_error("Group name is not supported yet.\n");
500 if (!check_event_name(arg
)) {
501 semantic_error("%s is bad for event name -it must "
502 "follow C symbol-naming rule.\n", arg
);
505 pev
->event
= strdup(arg
);
506 if (pev
->event
== NULL
)
512 ptr
= strpbrk(arg
, ";:+@%");
522 /* Check arg is function or file and copy it */
523 if (strchr(tmp
, '.')) /* File */
528 /* Parse other options */
532 if (c
== ';') { /* Lazy pattern must be the last part */
533 pp
->lazy_line
= strdup(arg
);
534 if (pp
->lazy_line
== NULL
)
538 ptr
= strpbrk(arg
, ";:+@%");
544 case ':': /* Line number */
545 pp
->line
= strtoul(arg
, &tmp
, 0);
547 semantic_error("There is non-digit char"
548 " in line number.\n");
552 case '+': /* Byte offset from a symbol */
553 pp
->offset
= strtoul(arg
, &tmp
, 0);
555 semantic_error("There is non-digit character"
560 case '@': /* File name */
562 semantic_error("SRC@SRC is not allowed.\n");
565 pp
->file
= strdup(arg
);
566 if (pp
->file
== NULL
)
569 case '%': /* Probe places */
570 if (strcmp(arg
, "return") == 0) {
572 } else { /* Others not supported yet */
573 semantic_error("%%%s is not supported.\n", arg
);
577 default: /* Buggy case */
578 pr_err("This program has a bug at %s:%d.\n",
585 /* Exclusion check */
586 if (pp
->lazy_line
&& pp
->line
) {
587 semantic_error("Lazy pattern can't be used with line number.");
591 if (pp
->lazy_line
&& pp
->offset
) {
592 semantic_error("Lazy pattern can't be used with offset.");
596 if (pp
->line
&& pp
->offset
) {
597 semantic_error("Offset can't be used with line number.");
601 if (!pp
->line
&& !pp
->lazy_line
&& pp
->file
&& !pp
->function
) {
602 semantic_error("File always requires line number or "
607 if (pp
->offset
&& !pp
->function
) {
608 semantic_error("Offset requires an entry function.");
612 if (pp
->retprobe
&& !pp
->function
) {
613 semantic_error("Return probe requires an entry function.");
617 if ((pp
->offset
|| pp
->line
|| pp
->lazy_line
) && pp
->retprobe
) {
618 semantic_error("Offset/Line/Lazy pattern can't be used with "
623 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
624 pp
->function
, pp
->file
, pp
->line
, pp
->offset
, pp
->retprobe
,
629 /* Parse perf-probe event argument */
630 static int parse_perf_probe_arg(char *str
, struct perf_probe_arg
*arg
)
632 char *tmp
, *goodname
;
633 struct perf_probe_arg_field
**fieldp
;
635 pr_debug("parsing arg: %s into ", str
);
637 tmp
= strchr(str
, '=');
639 arg
->name
= strndup(str
, tmp
- str
);
640 if (arg
->name
== NULL
)
642 pr_debug("name:%s ", arg
->name
);
646 tmp
= strchr(str
, ':');
647 if (tmp
) { /* Type setting */
649 arg
->type
= strdup(tmp
+ 1);
650 if (arg
->type
== NULL
)
652 pr_debug("type:%s ", arg
->type
);
655 tmp
= strpbrk(str
, "-.[");
656 if (!is_c_varname(str
) || !tmp
) {
657 /* A variable, register, symbol or special value */
658 arg
->var
= strdup(str
);
659 if (arg
->var
== NULL
)
661 pr_debug("%s\n", arg
->var
);
665 /* Structure fields or array element */
666 arg
->var
= strndup(str
, tmp
- str
);
667 if (arg
->var
== NULL
)
670 pr_debug("%s, ", arg
->var
);
671 fieldp
= &arg
->field
;
674 *fieldp
= zalloc(sizeof(struct perf_probe_arg_field
));
677 if (*tmp
== '[') { /* Array */
679 (*fieldp
)->index
= strtol(str
+ 1, &tmp
, 0);
680 (*fieldp
)->ref
= true;
681 if (*tmp
!= ']' || tmp
== str
+ 1) {
682 semantic_error("Array index must be a"
689 } else { /* Structure */
692 (*fieldp
)->ref
= false;
693 } else if (tmp
[1] == '>') {
695 (*fieldp
)->ref
= true;
697 semantic_error("Argument parse error: %s\n",
701 tmp
= strpbrk(str
, "-.[");
704 (*fieldp
)->name
= strndup(str
, tmp
- str
);
705 if ((*fieldp
)->name
== NULL
)
708 goodname
= (*fieldp
)->name
;
709 pr_debug("%s(%d), ", (*fieldp
)->name
, (*fieldp
)->ref
);
710 fieldp
= &(*fieldp
)->next
;
713 (*fieldp
)->name
= strdup(str
);
714 if ((*fieldp
)->name
== NULL
)
717 goodname
= (*fieldp
)->name
;
718 pr_debug("%s(%d)\n", (*fieldp
)->name
, (*fieldp
)->ref
);
720 /* If no name is specified, set the last field name (not array index)*/
722 arg
->name
= strdup(goodname
);
723 if (arg
->name
== NULL
)
729 /* Parse perf-probe event command */
730 int parse_perf_probe_command(const char *cmd
, struct perf_probe_event
*pev
)
733 int argc
, i
, ret
= 0;
735 argv
= argv_split(cmd
, &argc
);
737 pr_debug("Failed to split arguments.\n");
740 if (argc
- 1 > MAX_PROBE_ARGS
) {
741 semantic_error("Too many probe arguments (%d).\n", argc
- 1);
745 /* Parse probe point */
746 ret
= parse_perf_probe_point(argv
[0], pev
);
750 /* Copy arguments and ensure return probe has no C argument */
751 pev
->nargs
= argc
- 1;
752 pev
->args
= zalloc(sizeof(struct perf_probe_arg
) * pev
->nargs
);
753 if (pev
->args
== NULL
) {
757 for (i
= 0; i
< pev
->nargs
&& ret
>= 0; i
++) {
758 ret
= parse_perf_probe_arg(argv
[i
+ 1], &pev
->args
[i
]);
760 is_c_varname(pev
->args
[i
].var
) && pev
->point
.retprobe
) {
761 semantic_error("You can't specify local variable for"
772 /* Return true if this perf_probe_event requires debuginfo */
773 bool perf_probe_event_need_dwarf(struct perf_probe_event
*pev
)
777 if (pev
->point
.file
|| pev
->point
.line
|| pev
->point
.lazy_line
)
780 for (i
= 0; i
< pev
->nargs
; i
++)
781 if (is_c_varname(pev
->args
[i
].var
))
787 /* Parse probe_events event into struct probe_point */
788 static int parse_probe_trace_command(const char *cmd
,
789 struct probe_trace_event
*tev
)
791 struct probe_trace_point
*tp
= &tev
->point
;
797 pr_debug("Parsing probe_events: %s\n", cmd
);
798 argv
= argv_split(cmd
, &argc
);
800 pr_debug("Failed to split arguments.\n");
804 semantic_error("Too few probe arguments.\n");
809 /* Scan event and group name. */
810 ret
= sscanf(argv
[0], "%c:%a[^/ \t]/%a[^ \t]",
811 &pr
, (float *)(void *)&tev
->group
,
812 (float *)(void *)&tev
->event
);
814 semantic_error("Failed to parse event name: %s\n", argv
[0]);
818 pr_debug("Group:%s Event:%s probe:%c\n", tev
->group
, tev
->event
, pr
);
820 tp
->retprobe
= (pr
== 'r');
822 /* Scan function name and offset */
823 ret
= sscanf(argv
[1], "%a[^+]+%lu", (float *)(void *)&tp
->symbol
,
828 tev
->nargs
= argc
- 2;
829 tev
->args
= zalloc(sizeof(struct probe_trace_arg
) * tev
->nargs
);
830 if (tev
->args
== NULL
) {
834 for (i
= 0; i
< tev
->nargs
; i
++) {
835 p
= strchr(argv
[i
+ 2], '=');
836 if (p
) /* We don't need which register is assigned. */
840 tev
->args
[i
].name
= strdup(argv
[i
+ 2]);
841 /* TODO: parse regs and offset */
842 tev
->args
[i
].value
= strdup(p
);
843 if (tev
->args
[i
].name
== NULL
|| tev
->args
[i
].value
== NULL
) {
854 /* Compose only probe arg */
855 int synthesize_perf_probe_arg(struct perf_probe_arg
*pa
, char *buf
, size_t len
)
857 struct perf_probe_arg_field
*field
= pa
->field
;
861 if (pa
->name
&& pa
->var
)
862 ret
= e_snprintf(tmp
, len
, "%s=%s", pa
->name
, pa
->var
);
864 ret
= e_snprintf(tmp
, len
, "%s", pa
->name
? pa
->name
: pa
->var
);
871 if (field
->name
[0] == '[')
872 ret
= e_snprintf(tmp
, len
, "%s", field
->name
);
874 ret
= e_snprintf(tmp
, len
, "%s%s",
875 field
->ref
? "->" : ".", field
->name
);
884 ret
= e_snprintf(tmp
, len
, ":%s", pa
->type
);
893 pr_debug("Failed to synthesize perf probe argument: %s",
898 /* Compose only probe point (not argument) */
899 static char *synthesize_perf_probe_point(struct perf_probe_point
*pp
)
902 char offs
[32] = "", line
[32] = "", file
[32] = "";
905 buf
= zalloc(MAX_CMDLEN
);
911 ret
= e_snprintf(offs
, 32, "+%lu", pp
->offset
);
916 ret
= e_snprintf(line
, 32, ":%d", pp
->line
);
921 len
= strlen(pp
->file
) - 31;
924 tmp
= strchr(pp
->file
+ len
, '/');
926 tmp
= pp
->file
+ len
;
927 ret
= e_snprintf(file
, 32, "@%s", tmp
+ 1);
933 ret
= e_snprintf(buf
, MAX_CMDLEN
, "%s%s%s%s%s", pp
->function
,
934 offs
, pp
->retprobe
? "%return" : "", line
,
937 ret
= e_snprintf(buf
, MAX_CMDLEN
, "%s%s", file
, line
);
943 pr_debug("Failed to synthesize perf probe point: %s",
951 static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref
*ref
,
952 char **buf
, size_t *buflen
,
957 depth
= __synthesize_probe_trace_arg_ref(ref
->next
, buf
,
963 ret
= e_snprintf(*buf
, *buflen
, "%+ld(", ref
->offset
);
975 static int synthesize_probe_trace_arg(struct probe_trace_arg
*arg
,
976 char *buf
, size_t buflen
)
978 struct probe_trace_arg_ref
*ref
= arg
->ref
;
982 /* Argument name or separator */
984 ret
= e_snprintf(buf
, buflen
, " %s=", arg
->name
);
986 ret
= e_snprintf(buf
, buflen
, " ");
992 if (arg
->value
[0] == '@' && arg
->ref
)
995 /* Dereferencing arguments */
997 depth
= __synthesize_probe_trace_arg_ref(ref
, &buf
,
1003 /* Print argument value */
1004 if (arg
->value
[0] == '@' && arg
->ref
)
1005 ret
= e_snprintf(buf
, buflen
, "%s%+ld", arg
->value
,
1008 ret
= e_snprintf(buf
, buflen
, "%s", arg
->value
);
1016 ret
= e_snprintf(buf
, buflen
, ")");
1022 /* Print argument type */
1024 ret
= e_snprintf(buf
, buflen
, ":%s", arg
->type
);
1033 char *synthesize_probe_trace_command(struct probe_trace_event
*tev
)
1035 struct probe_trace_point
*tp
= &tev
->point
;
1039 buf
= zalloc(MAX_CMDLEN
);
1043 len
= e_snprintf(buf
, MAX_CMDLEN
, "%c:%s/%s %s+%lu",
1044 tp
->retprobe
? 'r' : 'p',
1045 tev
->group
, tev
->event
,
1046 tp
->symbol
, tp
->offset
);
1050 for (i
= 0; i
< tev
->nargs
; i
++) {
1051 ret
= synthesize_probe_trace_arg(&tev
->args
[i
], buf
+ len
,
1064 static int convert_to_perf_probe_event(struct probe_trace_event
*tev
,
1065 struct perf_probe_event
*pev
)
1070 /* Convert event/group name */
1071 pev
->event
= strdup(tev
->event
);
1072 pev
->group
= strdup(tev
->group
);
1073 if (pev
->event
== NULL
|| pev
->group
== NULL
)
1076 /* Convert trace_point to probe_point */
1077 ret
= kprobe_convert_to_perf_probe(&tev
->point
, &pev
->point
);
1081 /* Convert trace_arg to probe_arg */
1082 pev
->nargs
= tev
->nargs
;
1083 pev
->args
= zalloc(sizeof(struct perf_probe_arg
) * pev
->nargs
);
1084 if (pev
->args
== NULL
)
1086 for (i
= 0; i
< tev
->nargs
&& ret
>= 0; i
++) {
1087 if (tev
->args
[i
].name
)
1088 pev
->args
[i
].name
= strdup(tev
->args
[i
].name
);
1090 ret
= synthesize_probe_trace_arg(&tev
->args
[i
],
1092 pev
->args
[i
].name
= strdup(buf
);
1094 if (pev
->args
[i
].name
== NULL
&& ret
>= 0)
1099 clear_perf_probe_event(pev
);
1104 void clear_perf_probe_event(struct perf_probe_event
*pev
)
1106 struct perf_probe_point
*pp
= &pev
->point
;
1107 struct perf_probe_arg_field
*field
, *next
;
1119 free(pp
->lazy_line
);
1120 for (i
= 0; i
< pev
->nargs
; i
++) {
1121 if (pev
->args
[i
].name
)
1122 free(pev
->args
[i
].name
);
1123 if (pev
->args
[i
].var
)
1124 free(pev
->args
[i
].var
);
1125 if (pev
->args
[i
].type
)
1126 free(pev
->args
[i
].type
);
1127 field
= pev
->args
[i
].field
;
1138 memset(pev
, 0, sizeof(*pev
));
1141 static void clear_probe_trace_event(struct probe_trace_event
*tev
)
1143 struct probe_trace_arg_ref
*ref
, *next
;
1150 if (tev
->point
.symbol
)
1151 free(tev
->point
.symbol
);
1152 for (i
= 0; i
< tev
->nargs
; i
++) {
1153 if (tev
->args
[i
].name
)
1154 free(tev
->args
[i
].name
);
1155 if (tev
->args
[i
].value
)
1156 free(tev
->args
[i
].value
);
1157 if (tev
->args
[i
].type
)
1158 free(tev
->args
[i
].type
);
1159 ref
= tev
->args
[i
].ref
;
1168 memset(tev
, 0, sizeof(*tev
));
1171 static int open_kprobe_events(bool readwrite
)
1174 const char *__debugfs
;
1177 __debugfs
= debugfs_find_mountpoint();
1178 if (__debugfs
== NULL
) {
1179 pr_warning("Debugfs is not mounted.\n");
1183 ret
= e_snprintf(buf
, PATH_MAX
, "%stracing/kprobe_events", __debugfs
);
1185 pr_debug("Opening %s write=%d\n", buf
, readwrite
);
1186 if (readwrite
&& !probe_event_dry_run
)
1187 ret
= open(buf
, O_RDWR
, O_APPEND
);
1189 ret
= open(buf
, O_RDONLY
, 0);
1193 if (errno
== ENOENT
)
1194 pr_warning("kprobe_events file does not exist - please"
1195 " rebuild kernel with CONFIG_KPROBE_EVENT.\n");
1197 pr_warning("Failed to open kprobe_events file: %s\n",
1203 /* Get raw string list of current kprobe_events */
1204 static struct strlist
*get_probe_trace_command_rawlist(int fd
)
1208 char buf
[MAX_CMDLEN
];
1212 sl
= strlist__new(true, NULL
);
1214 fp
= fdopen(dup(fd
), "r");
1216 p
= fgets(buf
, MAX_CMDLEN
, fp
);
1220 idx
= strlen(p
) - 1;
1223 ret
= strlist__add(sl
, buf
);
1225 pr_debug("strlist__add failed: %s\n", strerror(-ret
));
1226 strlist__delete(sl
);
1236 static int show_perf_probe_event(struct perf_probe_event
*pev
)
1242 /* Synthesize only event probe point */
1243 place
= synthesize_perf_probe_point(&pev
->point
);
1247 ret
= e_snprintf(buf
, 128, "%s:%s", pev
->group
, pev
->event
);
1251 printf(" %-20s (on %s", buf
, place
);
1253 if (pev
->nargs
> 0) {
1255 for (i
= 0; i
< pev
->nargs
; i
++) {
1256 ret
= synthesize_perf_probe_arg(&pev
->args
[i
],
1268 /* List up current perf-probe events */
1269 int show_perf_probe_events(void)
1272 struct probe_trace_event tev
;
1273 struct perf_probe_event pev
;
1274 struct strlist
*rawlist
;
1275 struct str_node
*ent
;
1278 ret
= init_vmlinux();
1282 memset(&tev
, 0, sizeof(tev
));
1283 memset(&pev
, 0, sizeof(pev
));
1285 fd
= open_kprobe_events(false);
1289 rawlist
= get_probe_trace_command_rawlist(fd
);
1294 strlist__for_each(ent
, rawlist
) {
1295 ret
= parse_probe_trace_command(ent
->s
, &tev
);
1297 ret
= convert_to_perf_probe_event(&tev
, &pev
);
1299 ret
= show_perf_probe_event(&pev
);
1301 clear_perf_probe_event(&pev
);
1302 clear_probe_trace_event(&tev
);
1306 strlist__delete(rawlist
);
1311 /* Get current perf-probe event names */
1312 static struct strlist
*get_probe_trace_event_names(int fd
, bool include_group
)
1315 struct strlist
*sl
, *rawlist
;
1316 struct str_node
*ent
;
1317 struct probe_trace_event tev
;
1320 memset(&tev
, 0, sizeof(tev
));
1321 rawlist
= get_probe_trace_command_rawlist(fd
);
1322 sl
= strlist__new(true, NULL
);
1323 strlist__for_each(ent
, rawlist
) {
1324 ret
= parse_probe_trace_command(ent
->s
, &tev
);
1327 if (include_group
) {
1328 ret
= e_snprintf(buf
, 128, "%s:%s", tev
.group
,
1331 ret
= strlist__add(sl
, buf
);
1333 ret
= strlist__add(sl
, tev
.event
);
1334 clear_probe_trace_event(&tev
);
1338 strlist__delete(rawlist
);
1341 strlist__delete(sl
);
1347 static int write_probe_trace_event(int fd
, struct probe_trace_event
*tev
)
1350 char *buf
= synthesize_probe_trace_command(tev
);
1353 pr_debug("Failed to synthesize probe trace event.\n");
1357 pr_debug("Writing event: %s\n", buf
);
1358 if (!probe_event_dry_run
) {
1359 ret
= write(fd
, buf
, strlen(buf
));
1361 pr_warning("Failed to write event: %s\n",
1368 static int get_new_event_name(char *buf
, size_t len
, const char *base
,
1369 struct strlist
*namelist
, bool allow_suffix
)
1374 ret
= e_snprintf(buf
, len
, "%s", base
);
1376 pr_debug("snprintf() failed: %s\n", strerror(-ret
));
1379 if (!strlist__has_entry(namelist
, buf
))
1382 if (!allow_suffix
) {
1383 pr_warning("Error: event \"%s\" already exists. "
1384 "(Use -f to force duplicates.)\n", base
);
1388 /* Try to add suffix */
1389 for (i
= 1; i
< MAX_EVENT_INDEX
; i
++) {
1390 ret
= e_snprintf(buf
, len
, "%s_%d", base
, i
);
1392 pr_debug("snprintf() failed: %s\n", strerror(-ret
));
1395 if (!strlist__has_entry(namelist
, buf
))
1398 if (i
== MAX_EVENT_INDEX
) {
1399 pr_warning("Too many events are on the same function.\n");
1406 static int __add_probe_trace_events(struct perf_probe_event
*pev
,
1407 struct probe_trace_event
*tevs
,
1408 int ntevs
, bool allow_suffix
)
1411 struct probe_trace_event
*tev
= NULL
;
1413 const char *event
, *group
;
1414 struct strlist
*namelist
;
1416 fd
= open_kprobe_events(true);
1419 /* Get current event names */
1420 namelist
= get_probe_trace_event_names(fd
, false);
1422 pr_debug("Failed to get current event list.\n");
1427 printf("Add new event%s\n", (ntevs
> 1) ? "s:" : ":");
1428 for (i
= 0; i
< ntevs
; i
++) {
1433 if (pev
->point
.function
)
1434 event
= pev
->point
.function
;
1436 event
= tev
->point
.symbol
;
1440 group
= PERFPROBE_GROUP
;
1442 /* Get an unused new event name */
1443 ret
= get_new_event_name(buf
, 64, event
,
1444 namelist
, allow_suffix
);
1449 tev
->event
= strdup(event
);
1450 tev
->group
= strdup(group
);
1451 if (tev
->event
== NULL
|| tev
->group
== NULL
) {
1455 ret
= write_probe_trace_event(fd
, tev
);
1458 /* Add added event name to namelist */
1459 strlist__add(namelist
, event
);
1461 /* Trick here - save current event/group */
1464 pev
->event
= tev
->event
;
1465 pev
->group
= tev
->group
;
1466 show_perf_probe_event(pev
);
1467 /* Trick here - restore current event/group */
1468 pev
->event
= (char *)event
;
1469 pev
->group
= (char *)group
;
1472 * Probes after the first probe which comes from same
1473 * user input are always allowed to add suffix, because
1474 * there might be several addresses corresponding to
1477 allow_suffix
= true;
1481 /* Show how to use the event. */
1482 printf("\nYou can now use it on all perf tools, such as:\n\n");
1483 printf("\tperf record -e %s:%s -aR sleep 1\n\n", tev
->group
,
1487 strlist__delete(namelist
);
1492 static int convert_to_probe_trace_events(struct perf_probe_event
*pev
,
1493 struct probe_trace_event
**tevs
,
1498 struct probe_trace_event
*tev
;
1500 /* Convert perf_probe_event with debuginfo */
1501 ret
= try_to_find_probe_trace_events(pev
, tevs
, max_tevs
);
1505 /* Allocate trace event buffer */
1506 tev
= *tevs
= zalloc(sizeof(struct probe_trace_event
));
1510 /* Copy parameters */
1511 tev
->point
.symbol
= strdup(pev
->point
.function
);
1512 if (tev
->point
.symbol
== NULL
) {
1516 tev
->point
.offset
= pev
->point
.offset
;
1517 tev
->point
.retprobe
= pev
->point
.retprobe
;
1518 tev
->nargs
= pev
->nargs
;
1520 tev
->args
= zalloc(sizeof(struct probe_trace_arg
)
1522 if (tev
->args
== NULL
) {
1526 for (i
= 0; i
< tev
->nargs
; i
++) {
1527 if (pev
->args
[i
].name
) {
1528 tev
->args
[i
].name
= strdup(pev
->args
[i
].name
);
1529 if (tev
->args
[i
].name
== NULL
) {
1534 tev
->args
[i
].value
= strdup(pev
->args
[i
].var
);
1535 if (tev
->args
[i
].value
== NULL
) {
1539 if (pev
->args
[i
].type
) {
1540 tev
->args
[i
].type
= strdup(pev
->args
[i
].type
);
1541 if (tev
->args
[i
].type
== NULL
) {
1549 /* Currently just checking function name from symbol map */
1550 sym
= map__find_symbol_by_name(machine
.vmlinux_maps
[MAP__FUNCTION
],
1551 tev
->point
.symbol
, NULL
);
1553 pr_warning("Kernel symbol \'%s\' not found.\n",
1561 clear_probe_trace_event(tev
);
1567 struct __event_package
{
1568 struct perf_probe_event
*pev
;
1569 struct probe_trace_event
*tevs
;
1573 int add_perf_probe_events(struct perf_probe_event
*pevs
, int npevs
,
1574 bool force_add
, int max_tevs
)
1577 struct __event_package
*pkgs
;
1579 pkgs
= zalloc(sizeof(struct __event_package
) * npevs
);
1583 /* Init vmlinux path */
1584 ret
= init_vmlinux();
1590 /* Loop 1: convert all events */
1591 for (i
= 0; i
< npevs
; i
++) {
1592 pkgs
[i
].pev
= &pevs
[i
];
1593 /* Convert with or without debuginfo */
1594 ret
= convert_to_probe_trace_events(pkgs
[i
].pev
,
1595 &pkgs
[i
].tevs
, max_tevs
);
1598 pkgs
[i
].ntevs
= ret
;
1601 /* Loop 2: add all events */
1602 for (i
= 0; i
< npevs
&& ret
>= 0; i
++)
1603 ret
= __add_probe_trace_events(pkgs
[i
].pev
, pkgs
[i
].tevs
,
1604 pkgs
[i
].ntevs
, force_add
);
1606 /* Loop 3: cleanup and free trace events */
1607 for (i
= 0; i
< npevs
; i
++) {
1608 for (j
= 0; j
< pkgs
[i
].ntevs
; j
++)
1609 clear_probe_trace_event(&pkgs
[i
].tevs
[j
]);
1617 static int __del_trace_probe_event(int fd
, struct str_node
*ent
)
1623 /* Convert from perf-probe event to trace-probe event */
1624 ret
= e_snprintf(buf
, 128, "-:%s", ent
->s
);
1628 p
= strchr(buf
+ 2, ':');
1630 pr_debug("Internal error: %s should have ':' but not.\n",
1637 pr_debug("Writing event: %s\n", buf
);
1638 ret
= write(fd
, buf
, strlen(buf
));
1642 printf("Remove event: %s\n", ent
->s
);
1645 pr_warning("Failed to delete event: %s\n", strerror(-ret
));
1649 static int del_trace_probe_event(int fd
, const char *group
,
1650 const char *event
, struct strlist
*namelist
)
1653 struct str_node
*ent
, *n
;
1654 int found
= 0, ret
= 0;
1656 ret
= e_snprintf(buf
, 128, "%s:%s", group
, event
);
1658 pr_err("Failed to copy event.");
1662 if (strpbrk(buf
, "*?")) { /* Glob-exp */
1663 strlist__for_each_safe(ent
, n
, namelist
)
1664 if (strglobmatch(ent
->s
, buf
)) {
1666 ret
= __del_trace_probe_event(fd
, ent
);
1669 strlist__remove(namelist
, ent
);
1672 ent
= strlist__find(namelist
, buf
);
1675 ret
= __del_trace_probe_event(fd
, ent
);
1677 strlist__remove(namelist
, ent
);
1680 if (found
== 0 && ret
>= 0)
1681 pr_info("Info: Event \"%s\" does not exist.\n", buf
);
1686 int del_perf_probe_events(struct strlist
*dellist
)
1689 const char *group
, *event
;
1691 struct str_node
*ent
;
1692 struct strlist
*namelist
;
1694 fd
= open_kprobe_events(true);
1698 /* Get current event names */
1699 namelist
= get_probe_trace_event_names(fd
, true);
1700 if (namelist
== NULL
)
1703 strlist__for_each(ent
, dellist
) {
1704 str
= strdup(ent
->s
);
1709 pr_debug("Parsing: %s\n", str
);
1710 p
= strchr(str
, ':');
1719 pr_debug("Group: %s, Event: %s\n", group
, event
);
1720 ret
= del_trace_probe_event(fd
, group
, event
, namelist
);
1725 strlist__delete(namelist
);