]>
git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - tools/perf/util/probe-event.c
2 * probe-event.c : perf-probe definition to probe_events format converter
4 * Written by Masami Hiramatsu <mhiramat@redhat.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #include <sys/utsname.h>
24 #include <sys/types.h>
46 #include "trace-event.h" /* For __unused */
47 #include "probe-event.h"
48 #include "probe-finder.h"
50 #define MAX_CMDLEN 256
51 #define MAX_PROBE_ARGS 128
52 #define PERFPROBE_GROUP "probe"
54 bool probe_event_dry_run
; /* Dry run flag */
56 #define semantic_error(msg ...) pr_err("Semantic error :" msg)
58 /* If there is no space to write, returns -E2BIG. */
59 static int e_snprintf(char *str
, size_t size
, const char *format
, ...)
60 __attribute__((format(printf
, 3, 4)));
62 static int e_snprintf(char *str
, size_t size
, const char *format
, ...)
67 ret
= vsnprintf(str
, size
, format
, ap
);
74 static char *synthesize_perf_probe_point(struct perf_probe_point
*pp
);
75 static struct machine machine
;
77 /* Initialize symbol maps and path of vmlinux/modules */
78 static int init_vmlinux(void)
82 symbol_conf
.sort_by_name
= true;
83 if (symbol_conf
.vmlinux_name
== NULL
)
84 symbol_conf
.try_vmlinux_path
= true;
86 pr_debug("Use vmlinux: %s\n", symbol_conf
.vmlinux_name
);
89 pr_debug("Failed to init symbol map.\n");
93 ret
= machine__init(&machine
, "", HOST_KERNEL_ID
);
97 if (machine__create_kernel_maps(&machine
) < 0) {
98 pr_debug("machine__create_kernel_maps ");
103 pr_warning("Failed to init vmlinux path.\n");
107 static struct symbol
*__find_kernel_function_by_name(const char *name
,
110 return machine__find_kernel_function_by_name(&machine
, name
, mapp
,
114 const char *kernel_get_module_path(const char *module
)
118 const char *vmlinux_name
;
121 list_for_each_entry(dso
, &machine
.kernel_dsos
, node
) {
122 if (strncmp(dso
->short_name
+ 1, module
,
123 dso
->short_name_len
- 2) == 0)
126 pr_debug("Failed to find module %s.\n", module
);
130 map
= machine
.vmlinux_maps
[MAP__FUNCTION
];
133 vmlinux_name
= symbol_conf
.vmlinux_name
;
135 if (dso__load_vmlinux(dso
, map
, vmlinux_name
, NULL
) <= 0)
138 if (dso__load_vmlinux_path(dso
, map
, NULL
) <= 0) {
139 pr_debug("Failed to load kernel map.\n");
144 return dso
->long_name
;
148 static int open_vmlinux(const char *module
)
150 const char *path
= kernel_get_module_path(module
);
152 pr_err("Failed to find path of %s module", module
?: "kernel");
155 pr_debug("Try to open %s\n", path
);
156 return open(path
, O_RDONLY
);
160 * Convert trace point to probe point with debuginfo
161 * Currently only handles kprobes.
163 static int kprobe_convert_to_perf_probe(struct probe_trace_point
*tp
,
164 struct perf_probe_point
*pp
)
171 sym
= __find_kernel_function_by_name(tp
->symbol
, &map
);
173 addr
= map
->unmap_ip(map
, sym
->start
+ tp
->offset
);
174 pr_debug("try to find %s+%ld@%llx\n", tp
->symbol
,
176 ret
= find_perf_probe_point((unsigned long)addr
, pp
);
179 pr_debug("Failed to find corresponding probes from "
180 "debuginfo. Use kprobe event information.\n");
181 pp
->function
= strdup(tp
->symbol
);
182 if (pp
->function
== NULL
)
184 pp
->offset
= tp
->offset
;
186 pp
->retprobe
= tp
->retprobe
;
191 /* Try to find perf_probe_event with debuginfo */
192 static int try_to_find_probe_trace_events(struct perf_probe_event
*pev
,
193 struct probe_trace_event
**tevs
,
194 int max_tevs
, const char *module
)
196 bool need_dwarf
= perf_probe_event_need_dwarf(pev
);
199 fd
= open_vmlinux(module
);
202 pr_warning("Failed to open debuginfo file.\n");
205 pr_debug("Could not open vmlinux. Try to use symbols.\n");
209 /* Searching trace events corresponding to probe event */
210 ntevs
= find_probe_trace_events(fd
, pev
, tevs
, max_tevs
);
213 if (ntevs
> 0) { /* Succeeded to find trace events */
214 pr_debug("find %d probe_trace_events.\n", ntevs
);
218 if (ntevs
== 0) { /* No error but failed to find probe point. */
219 pr_warning("Probe point '%s' not found.\n",
220 synthesize_perf_probe_point(&pev
->point
));
223 /* Error path : ntevs < 0 */
224 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs
);
225 if (ntevs
== -EBADF
) {
226 pr_warning("Warning: No dwarf info found in the vmlinux - "
227 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
229 pr_debug("Trying to use symbols.\nn");
237 * Find a src file from a DWARF tag path. Prepend optional source path prefix
238 * and chop off leading directories that do not exist. Result is passed back as
239 * a newly allocated path on success.
240 * Return 0 if file was found and readable, -errno otherwise.
242 static int get_real_path(const char *raw_path
, const char *comp_dir
,
245 const char *prefix
= symbol_conf
.source_prefix
;
248 if (raw_path
[0] != '/' && comp_dir
)
249 /* If not an absolute path, try to use comp_dir */
252 if (access(raw_path
, R_OK
) == 0) {
253 *new_path
= strdup(raw_path
);
260 *new_path
= malloc((strlen(prefix
) + strlen(raw_path
) + 2));
265 sprintf(*new_path
, "%s/%s", prefix
, raw_path
);
267 if (access(*new_path
, R_OK
) == 0)
270 if (!symbol_conf
.source_prefix
)
271 /* In case of searching comp_dir, don't retry */
279 raw_path
= strchr(++raw_path
, '/');
295 #define LINEBUF_SIZE 256
296 #define NR_ADDITIONAL_LINES 2
298 static int show_one_line(FILE *fp
, int l
, bool skip
, bool show_num
)
300 char buf
[LINEBUF_SIZE
];
301 const char *color
= PERF_COLOR_BLUE
;
303 if (fgets(buf
, LINEBUF_SIZE
, fp
) == NULL
)
307 fprintf(stdout
, "%7d %s", l
, buf
);
309 color_fprintf(stdout
, color
, " %s", buf
);
312 while (strlen(buf
) == LINEBUF_SIZE
- 1 &&
313 buf
[LINEBUF_SIZE
- 2] != '\n') {
314 if (fgets(buf
, LINEBUF_SIZE
, fp
) == NULL
)
318 fprintf(stdout
, "%s", buf
);
320 color_fprintf(stdout
, color
, "%s", buf
);
327 pr_warning("Source file is shorter than expected.\n");
329 pr_warning("File read error: %s\n", strerror(errno
));
335 * Show line-range always requires debuginfo to find source file and
338 int show_line_range(struct line_range
*lr
, const char *module
)
341 struct line_node
*ln
;
346 /* Search a line range */
347 ret
= init_vmlinux();
351 fd
= open_vmlinux(module
);
353 pr_warning("Failed to open debuginfo file.\n");
357 ret
= find_line_range(fd
, lr
);
360 pr_warning("Specified source line is not found.\n");
362 } else if (ret
< 0) {
363 pr_warning("Debuginfo analysis failed. (%d)\n", ret
);
367 /* Convert source file path */
369 ret
= get_real_path(tmp
, lr
->comp_dir
, &lr
->path
);
370 free(tmp
); /* Free old path */
372 pr_warning("Failed to find source file. (%d)\n", ret
);
379 fprintf(stdout
, "<%s:%d>\n", lr
->function
,
380 lr
->start
- lr
->offset
);
382 fprintf(stdout
, "<%s:%d>\n", lr
->file
, lr
->start
);
384 fp
= fopen(lr
->path
, "r");
386 pr_warning("Failed to open %s: %s\n", lr
->path
,
390 /* Skip to starting line number */
391 while (l
< lr
->start
&& ret
>= 0)
392 ret
= show_one_line(fp
, l
++, true, false);
396 list_for_each_entry(ln
, &lr
->line_list
, list
) {
397 while (ln
->line
> l
&& ret
>= 0)
398 ret
= show_one_line(fp
, (l
++) - lr
->offset
,
401 ret
= show_one_line(fp
, (l
++) - lr
->offset
,
407 if (lr
->end
== INT_MAX
)
408 lr
->end
= l
+ NR_ADDITIONAL_LINES
;
409 while (l
<= lr
->end
&& !feof(fp
) && ret
>= 0)
410 ret
= show_one_line(fp
, (l
++) - lr
->offset
, false, false);
416 static int show_available_vars_at(int fd
, struct perf_probe_event
*pev
,
417 int max_vls
, bool externs
)
421 struct str_node
*node
;
422 struct variable_list
*vls
= NULL
, *vl
;
424 buf
= synthesize_perf_probe_point(&pev
->point
);
427 pr_debug("Searching variables at %s\n", buf
);
429 ret
= find_available_vars_at(fd
, pev
, &vls
, max_vls
, externs
);
431 /* Some variables were found */
432 fprintf(stdout
, "Available variables at %s\n", buf
);
433 for (i
= 0; i
< ret
; i
++) {
436 * A probe point might be converted to
437 * several trace points.
439 fprintf(stdout
, "\t@<%s+%lu>\n", vl
->point
.symbol
,
441 free(vl
->point
.symbol
);
443 strlist__for_each(node
, vl
->vars
)
444 fprintf(stdout
, "\t\t%s\n", node
->s
);
445 strlist__delete(vl
->vars
);
447 fprintf(stdout
, "(No variables)\n");
451 pr_err("Failed to find variables at %s (%d)\n", buf
, ret
);
457 /* Show available variables on given probe point */
458 int show_available_vars(struct perf_probe_event
*pevs
, int npevs
,
459 int max_vls
, const char *module
, bool externs
)
463 ret
= init_vmlinux();
467 fd
= open_vmlinux(module
);
469 pr_warning("Failed to open debuginfo file.\n");
475 for (i
= 0; i
< npevs
&& ret
>= 0; i
++)
476 ret
= show_available_vars_at(fd
, &pevs
[i
], max_vls
, externs
);
482 #else /* !DWARF_SUPPORT */
484 static int kprobe_convert_to_perf_probe(struct probe_trace_point
*tp
,
485 struct perf_probe_point
*pp
)
489 sym
= __find_kernel_function_by_name(tp
->symbol
, NULL
);
491 pr_err("Failed to find symbol %s in kernel.\n", tp
->symbol
);
494 pp
->function
= strdup(tp
->symbol
);
495 if (pp
->function
== NULL
)
497 pp
->offset
= tp
->offset
;
498 pp
->retprobe
= tp
->retprobe
;
503 static int try_to_find_probe_trace_events(struct perf_probe_event
*pev
,
504 struct probe_trace_event
**tevs __unused
,
505 int max_tevs __unused
, const char *mod __unused
)
507 if (perf_probe_event_need_dwarf(pev
)) {
508 pr_warning("Debuginfo-analysis is not supported.\n");
514 int show_line_range(struct line_range
*lr __unused
, const char *module __unused
)
516 pr_warning("Debuginfo-analysis is not supported.\n");
520 int show_available_vars(struct perf_probe_event
*pevs __unused
,
521 int npevs __unused
, int max_vls __unused
,
522 const char *module __unused
, bool externs __unused
)
524 pr_warning("Debuginfo-analysis is not supported.\n");
529 int parse_line_range_desc(const char *arg
, struct line_range
*lr
)
536 * FUNC[:SLN[+NUM|-ELN]]
538 ptr
= strchr(arg
, ':');
540 lr
->start
= (int)strtoul(ptr
+ 1, &tmp
, 0);
542 lr
->end
= lr
->start
+ (int)strtoul(tmp
+ 1, &tmp
, 0);
544 * Adjust the number of lines here.
545 * If the number of lines == 1, the
546 * the end of line should be equal to
549 } else if (*tmp
== '-')
550 lr
->end
= (int)strtoul(tmp
+ 1, &tmp
, 0);
553 pr_debug("Line range is %d to %d\n", lr
->start
, lr
->end
);
554 if (lr
->start
> lr
->end
) {
555 semantic_error("Start line must be smaller"
556 " than end line.\n");
560 semantic_error("Tailing with invalid character '%d'.\n",
564 tmp
= strndup(arg
, (ptr
- arg
));
573 if (strchr(tmp
, '.'))
581 /* Check the name is good for event/group */
582 static bool check_event_name(const char *name
)
584 if (!isalpha(*name
) && *name
!= '_')
586 while (*++name
!= '\0') {
587 if (!isalpha(*name
) && !isdigit(*name
) && *name
!= '_')
593 /* Parse probepoint definition. */
594 static int parse_perf_probe_point(char *arg
, struct perf_probe_event
*pev
)
596 struct perf_probe_point
*pp
= &pev
->point
;
601 * perf probe [EVENT=]SRC[:LN|;PTN]
602 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
604 * TODO:Group name support
607 ptr
= strpbrk(arg
, ";=@+%");
608 if (ptr
&& *ptr
== '=') { /* Event name */
611 if (strchr(arg
, ':')) {
612 semantic_error("Group name is not supported yet.\n");
615 if (!check_event_name(arg
)) {
616 semantic_error("%s is bad for event name -it must "
617 "follow C symbol-naming rule.\n", arg
);
620 pev
->event
= strdup(arg
);
621 if (pev
->event
== NULL
)
627 ptr
= strpbrk(arg
, ";:+@%");
637 /* Check arg is function or file and copy it */
638 if (strchr(tmp
, '.')) /* File */
643 /* Parse other options */
647 if (c
== ';') { /* Lazy pattern must be the last part */
648 pp
->lazy_line
= strdup(arg
);
649 if (pp
->lazy_line
== NULL
)
653 ptr
= strpbrk(arg
, ";:+@%");
659 case ':': /* Line number */
660 pp
->line
= strtoul(arg
, &tmp
, 0);
662 semantic_error("There is non-digit char"
663 " in line number.\n");
667 case '+': /* Byte offset from a symbol */
668 pp
->offset
= strtoul(arg
, &tmp
, 0);
670 semantic_error("There is non-digit character"
675 case '@': /* File name */
677 semantic_error("SRC@SRC is not allowed.\n");
680 pp
->file
= strdup(arg
);
681 if (pp
->file
== NULL
)
684 case '%': /* Probe places */
685 if (strcmp(arg
, "return") == 0) {
687 } else { /* Others not supported yet */
688 semantic_error("%%%s is not supported.\n", arg
);
692 default: /* Buggy case */
693 pr_err("This program has a bug at %s:%d.\n",
700 /* Exclusion check */
701 if (pp
->lazy_line
&& pp
->line
) {
702 semantic_error("Lazy pattern can't be used with line number.");
706 if (pp
->lazy_line
&& pp
->offset
) {
707 semantic_error("Lazy pattern can't be used with offset.");
711 if (pp
->line
&& pp
->offset
) {
712 semantic_error("Offset can't be used with line number.");
716 if (!pp
->line
&& !pp
->lazy_line
&& pp
->file
&& !pp
->function
) {
717 semantic_error("File always requires line number or "
722 if (pp
->offset
&& !pp
->function
) {
723 semantic_error("Offset requires an entry function.");
727 if (pp
->retprobe
&& !pp
->function
) {
728 semantic_error("Return probe requires an entry function.");
732 if ((pp
->offset
|| pp
->line
|| pp
->lazy_line
) && pp
->retprobe
) {
733 semantic_error("Offset/Line/Lazy pattern can't be used with "
738 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
739 pp
->function
, pp
->file
, pp
->line
, pp
->offset
, pp
->retprobe
,
744 /* Parse perf-probe event argument */
745 static int parse_perf_probe_arg(char *str
, struct perf_probe_arg
*arg
)
747 char *tmp
, *goodname
;
748 struct perf_probe_arg_field
**fieldp
;
750 pr_debug("parsing arg: %s into ", str
);
752 tmp
= strchr(str
, '=');
754 arg
->name
= strndup(str
, tmp
- str
);
755 if (arg
->name
== NULL
)
757 pr_debug("name:%s ", arg
->name
);
761 tmp
= strchr(str
, ':');
762 if (tmp
) { /* Type setting */
764 arg
->type
= strdup(tmp
+ 1);
765 if (arg
->type
== NULL
)
767 pr_debug("type:%s ", arg
->type
);
770 tmp
= strpbrk(str
, "-.[");
771 if (!is_c_varname(str
) || !tmp
) {
772 /* A variable, register, symbol or special value */
773 arg
->var
= strdup(str
);
774 if (arg
->var
== NULL
)
776 pr_debug("%s\n", arg
->var
);
780 /* Structure fields or array element */
781 arg
->var
= strndup(str
, tmp
- str
);
782 if (arg
->var
== NULL
)
785 pr_debug("%s, ", arg
->var
);
786 fieldp
= &arg
->field
;
789 *fieldp
= zalloc(sizeof(struct perf_probe_arg_field
));
792 if (*tmp
== '[') { /* Array */
794 (*fieldp
)->index
= strtol(str
+ 1, &tmp
, 0);
795 (*fieldp
)->ref
= true;
796 if (*tmp
!= ']' || tmp
== str
+ 1) {
797 semantic_error("Array index must be a"
804 } else { /* Structure */
807 (*fieldp
)->ref
= false;
808 } else if (tmp
[1] == '>') {
810 (*fieldp
)->ref
= true;
812 semantic_error("Argument parse error: %s\n",
816 tmp
= strpbrk(str
, "-.[");
819 (*fieldp
)->name
= strndup(str
, tmp
- str
);
820 if ((*fieldp
)->name
== NULL
)
823 goodname
= (*fieldp
)->name
;
824 pr_debug("%s(%d), ", (*fieldp
)->name
, (*fieldp
)->ref
);
825 fieldp
= &(*fieldp
)->next
;
828 (*fieldp
)->name
= strdup(str
);
829 if ((*fieldp
)->name
== NULL
)
832 goodname
= (*fieldp
)->name
;
833 pr_debug("%s(%d)\n", (*fieldp
)->name
, (*fieldp
)->ref
);
835 /* If no name is specified, set the last field name (not array index)*/
837 arg
->name
= strdup(goodname
);
838 if (arg
->name
== NULL
)
844 /* Parse perf-probe event command */
845 int parse_perf_probe_command(const char *cmd
, struct perf_probe_event
*pev
)
848 int argc
, i
, ret
= 0;
850 argv
= argv_split(cmd
, &argc
);
852 pr_debug("Failed to split arguments.\n");
855 if (argc
- 1 > MAX_PROBE_ARGS
) {
856 semantic_error("Too many probe arguments (%d).\n", argc
- 1);
860 /* Parse probe point */
861 ret
= parse_perf_probe_point(argv
[0], pev
);
865 /* Copy arguments and ensure return probe has no C argument */
866 pev
->nargs
= argc
- 1;
867 pev
->args
= zalloc(sizeof(struct perf_probe_arg
) * pev
->nargs
);
868 if (pev
->args
== NULL
) {
872 for (i
= 0; i
< pev
->nargs
&& ret
>= 0; i
++) {
873 ret
= parse_perf_probe_arg(argv
[i
+ 1], &pev
->args
[i
]);
875 is_c_varname(pev
->args
[i
].var
) && pev
->point
.retprobe
) {
876 semantic_error("You can't specify local variable for"
887 /* Return true if this perf_probe_event requires debuginfo */
888 bool perf_probe_event_need_dwarf(struct perf_probe_event
*pev
)
892 if (pev
->point
.file
|| pev
->point
.line
|| pev
->point
.lazy_line
)
895 for (i
= 0; i
< pev
->nargs
; i
++)
896 if (is_c_varname(pev
->args
[i
].var
))
902 /* Parse probe_events event into struct probe_point */
903 static int parse_probe_trace_command(const char *cmd
,
904 struct probe_trace_event
*tev
)
906 struct probe_trace_point
*tp
= &tev
->point
;
912 pr_debug("Parsing probe_events: %s\n", cmd
);
913 argv
= argv_split(cmd
, &argc
);
915 pr_debug("Failed to split arguments.\n");
919 semantic_error("Too few probe arguments.\n");
924 /* Scan event and group name. */
925 ret
= sscanf(argv
[0], "%c:%a[^/ \t]/%a[^ \t]",
926 &pr
, (float *)(void *)&tev
->group
,
927 (float *)(void *)&tev
->event
);
929 semantic_error("Failed to parse event name: %s\n", argv
[0]);
933 pr_debug("Group:%s Event:%s probe:%c\n", tev
->group
, tev
->event
, pr
);
935 tp
->retprobe
= (pr
== 'r');
937 /* Scan function name and offset */
938 ret
= sscanf(argv
[1], "%a[^+]+%lu", (float *)(void *)&tp
->symbol
,
943 tev
->nargs
= argc
- 2;
944 tev
->args
= zalloc(sizeof(struct probe_trace_arg
) * tev
->nargs
);
945 if (tev
->args
== NULL
) {
949 for (i
= 0; i
< tev
->nargs
; i
++) {
950 p
= strchr(argv
[i
+ 2], '=');
951 if (p
) /* We don't need which register is assigned. */
955 tev
->args
[i
].name
= strdup(argv
[i
+ 2]);
956 /* TODO: parse regs and offset */
957 tev
->args
[i
].value
= strdup(p
);
958 if (tev
->args
[i
].name
== NULL
|| tev
->args
[i
].value
== NULL
) {
969 /* Compose only probe arg */
970 int synthesize_perf_probe_arg(struct perf_probe_arg
*pa
, char *buf
, size_t len
)
972 struct perf_probe_arg_field
*field
= pa
->field
;
976 if (pa
->name
&& pa
->var
)
977 ret
= e_snprintf(tmp
, len
, "%s=%s", pa
->name
, pa
->var
);
979 ret
= e_snprintf(tmp
, len
, "%s", pa
->name
? pa
->name
: pa
->var
);
986 if (field
->name
[0] == '[')
987 ret
= e_snprintf(tmp
, len
, "%s", field
->name
);
989 ret
= e_snprintf(tmp
, len
, "%s%s",
990 field
->ref
? "->" : ".", field
->name
);
999 ret
= e_snprintf(tmp
, len
, ":%s", pa
->type
);
1008 pr_debug("Failed to synthesize perf probe argument: %s",
1013 /* Compose only probe point (not argument) */
1014 static char *synthesize_perf_probe_point(struct perf_probe_point
*pp
)
1017 char offs
[32] = "", line
[32] = "", file
[32] = "";
1020 buf
= zalloc(MAX_CMDLEN
);
1026 ret
= e_snprintf(offs
, 32, "+%lu", pp
->offset
);
1031 ret
= e_snprintf(line
, 32, ":%d", pp
->line
);
1036 len
= strlen(pp
->file
) - 31;
1039 tmp
= strchr(pp
->file
+ len
, '/');
1041 tmp
= pp
->file
+ len
;
1042 ret
= e_snprintf(file
, 32, "@%s", tmp
+ 1);
1048 ret
= e_snprintf(buf
, MAX_CMDLEN
, "%s%s%s%s%s", pp
->function
,
1049 offs
, pp
->retprobe
? "%return" : "", line
,
1052 ret
= e_snprintf(buf
, MAX_CMDLEN
, "%s%s", file
, line
);
1058 pr_debug("Failed to synthesize perf probe point: %s",
1066 char *synthesize_perf_probe_command(struct perf_probe_event
*pev
)
1071 buf
= synthesize_perf_probe_point(&pev
->point
);
1076 for (i
= 0; i
< pev
->nargs
; i
++) {
1077 ret
= e_snprintf(&buf
[len
], MAX_CMDLEN
- len
, " %s",
1090 static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref
*ref
,
1091 char **buf
, size_t *buflen
,
1096 depth
= __synthesize_probe_trace_arg_ref(ref
->next
, buf
,
1102 ret
= e_snprintf(*buf
, *buflen
, "%+ld(", ref
->offset
);
1114 static int synthesize_probe_trace_arg(struct probe_trace_arg
*arg
,
1115 char *buf
, size_t buflen
)
1117 struct probe_trace_arg_ref
*ref
= arg
->ref
;
1121 /* Argument name or separator */
1123 ret
= e_snprintf(buf
, buflen
, " %s=", arg
->name
);
1125 ret
= e_snprintf(buf
, buflen
, " ");
1131 /* Special case: @XXX */
1132 if (arg
->value
[0] == '@' && arg
->ref
)
1135 /* Dereferencing arguments */
1137 depth
= __synthesize_probe_trace_arg_ref(ref
, &buf
,
1143 /* Print argument value */
1144 if (arg
->value
[0] == '@' && arg
->ref
)
1145 ret
= e_snprintf(buf
, buflen
, "%s%+ld", arg
->value
,
1148 ret
= e_snprintf(buf
, buflen
, "%s", arg
->value
);
1156 ret
= e_snprintf(buf
, buflen
, ")");
1162 /* Print argument type */
1164 ret
= e_snprintf(buf
, buflen
, ":%s", arg
->type
);
1173 char *synthesize_probe_trace_command(struct probe_trace_event
*tev
)
1175 struct probe_trace_point
*tp
= &tev
->point
;
1179 buf
= zalloc(MAX_CMDLEN
);
1183 len
= e_snprintf(buf
, MAX_CMDLEN
, "%c:%s/%s %s+%lu",
1184 tp
->retprobe
? 'r' : 'p',
1185 tev
->group
, tev
->event
,
1186 tp
->symbol
, tp
->offset
);
1190 for (i
= 0; i
< tev
->nargs
; i
++) {
1191 ret
= synthesize_probe_trace_arg(&tev
->args
[i
], buf
+ len
,
1204 static int convert_to_perf_probe_event(struct probe_trace_event
*tev
,
1205 struct perf_probe_event
*pev
)
1210 /* Convert event/group name */
1211 pev
->event
= strdup(tev
->event
);
1212 pev
->group
= strdup(tev
->group
);
1213 if (pev
->event
== NULL
|| pev
->group
== NULL
)
1216 /* Convert trace_point to probe_point */
1217 ret
= kprobe_convert_to_perf_probe(&tev
->point
, &pev
->point
);
1221 /* Convert trace_arg to probe_arg */
1222 pev
->nargs
= tev
->nargs
;
1223 pev
->args
= zalloc(sizeof(struct perf_probe_arg
) * pev
->nargs
);
1224 if (pev
->args
== NULL
)
1226 for (i
= 0; i
< tev
->nargs
&& ret
>= 0; i
++) {
1227 if (tev
->args
[i
].name
)
1228 pev
->args
[i
].name
= strdup(tev
->args
[i
].name
);
1230 ret
= synthesize_probe_trace_arg(&tev
->args
[i
],
1232 pev
->args
[i
].name
= strdup(buf
);
1234 if (pev
->args
[i
].name
== NULL
&& ret
>= 0)
1239 clear_perf_probe_event(pev
);
1244 void clear_perf_probe_event(struct perf_probe_event
*pev
)
1246 struct perf_probe_point
*pp
= &pev
->point
;
1247 struct perf_probe_arg_field
*field
, *next
;
1259 free(pp
->lazy_line
);
1260 for (i
= 0; i
< pev
->nargs
; i
++) {
1261 if (pev
->args
[i
].name
)
1262 free(pev
->args
[i
].name
);
1263 if (pev
->args
[i
].var
)
1264 free(pev
->args
[i
].var
);
1265 if (pev
->args
[i
].type
)
1266 free(pev
->args
[i
].type
);
1267 field
= pev
->args
[i
].field
;
1278 memset(pev
, 0, sizeof(*pev
));
1281 static void clear_probe_trace_event(struct probe_trace_event
*tev
)
1283 struct probe_trace_arg_ref
*ref
, *next
;
1290 if (tev
->point
.symbol
)
1291 free(tev
->point
.symbol
);
1292 for (i
= 0; i
< tev
->nargs
; i
++) {
1293 if (tev
->args
[i
].name
)
1294 free(tev
->args
[i
].name
);
1295 if (tev
->args
[i
].value
)
1296 free(tev
->args
[i
].value
);
1297 if (tev
->args
[i
].type
)
1298 free(tev
->args
[i
].type
);
1299 ref
= tev
->args
[i
].ref
;
1308 memset(tev
, 0, sizeof(*tev
));
1311 static int open_kprobe_events(bool readwrite
)
1314 const char *__debugfs
;
1317 __debugfs
= debugfs_find_mountpoint();
1318 if (__debugfs
== NULL
) {
1319 pr_warning("Debugfs is not mounted.\n");
1323 ret
= e_snprintf(buf
, PATH_MAX
, "%stracing/kprobe_events", __debugfs
);
1325 pr_debug("Opening %s write=%d\n", buf
, readwrite
);
1326 if (readwrite
&& !probe_event_dry_run
)
1327 ret
= open(buf
, O_RDWR
, O_APPEND
);
1329 ret
= open(buf
, O_RDONLY
, 0);
1333 if (errno
== ENOENT
)
1334 pr_warning("kprobe_events file does not exist - please"
1335 " rebuild kernel with CONFIG_KPROBE_EVENT.\n");
1337 pr_warning("Failed to open kprobe_events file: %s\n",
1343 /* Get raw string list of current kprobe_events */
1344 static struct strlist
*get_probe_trace_command_rawlist(int fd
)
1348 char buf
[MAX_CMDLEN
];
1352 sl
= strlist__new(true, NULL
);
1354 fp
= fdopen(dup(fd
), "r");
1356 p
= fgets(buf
, MAX_CMDLEN
, fp
);
1360 idx
= strlen(p
) - 1;
1363 ret
= strlist__add(sl
, buf
);
1365 pr_debug("strlist__add failed: %s\n", strerror(-ret
));
1366 strlist__delete(sl
);
1376 static int show_perf_probe_event(struct perf_probe_event
*pev
)
1382 /* Synthesize only event probe point */
1383 place
= synthesize_perf_probe_point(&pev
->point
);
1387 ret
= e_snprintf(buf
, 128, "%s:%s", pev
->group
, pev
->event
);
1391 printf(" %-20s (on %s", buf
, place
);
1393 if (pev
->nargs
> 0) {
1395 for (i
= 0; i
< pev
->nargs
; i
++) {
1396 ret
= synthesize_perf_probe_arg(&pev
->args
[i
],
1408 /* List up current perf-probe events */
1409 int show_perf_probe_events(void)
1412 struct probe_trace_event tev
;
1413 struct perf_probe_event pev
;
1414 struct strlist
*rawlist
;
1415 struct str_node
*ent
;
1418 ret
= init_vmlinux();
1422 memset(&tev
, 0, sizeof(tev
));
1423 memset(&pev
, 0, sizeof(pev
));
1425 fd
= open_kprobe_events(false);
1429 rawlist
= get_probe_trace_command_rawlist(fd
);
1434 strlist__for_each(ent
, rawlist
) {
1435 ret
= parse_probe_trace_command(ent
->s
, &tev
);
1437 ret
= convert_to_perf_probe_event(&tev
, &pev
);
1439 ret
= show_perf_probe_event(&pev
);
1441 clear_perf_probe_event(&pev
);
1442 clear_probe_trace_event(&tev
);
1446 strlist__delete(rawlist
);
1451 /* Get current perf-probe event names */
1452 static struct strlist
*get_probe_trace_event_names(int fd
, bool include_group
)
1455 struct strlist
*sl
, *rawlist
;
1456 struct str_node
*ent
;
1457 struct probe_trace_event tev
;
1460 memset(&tev
, 0, sizeof(tev
));
1461 rawlist
= get_probe_trace_command_rawlist(fd
);
1462 sl
= strlist__new(true, NULL
);
1463 strlist__for_each(ent
, rawlist
) {
1464 ret
= parse_probe_trace_command(ent
->s
, &tev
);
1467 if (include_group
) {
1468 ret
= e_snprintf(buf
, 128, "%s:%s", tev
.group
,
1471 ret
= strlist__add(sl
, buf
);
1473 ret
= strlist__add(sl
, tev
.event
);
1474 clear_probe_trace_event(&tev
);
1478 strlist__delete(rawlist
);
1481 strlist__delete(sl
);
1487 static int write_probe_trace_event(int fd
, struct probe_trace_event
*tev
)
1490 char *buf
= synthesize_probe_trace_command(tev
);
1493 pr_debug("Failed to synthesize probe trace event.\n");
1497 pr_debug("Writing event: %s\n", buf
);
1498 if (!probe_event_dry_run
) {
1499 ret
= write(fd
, buf
, strlen(buf
));
1501 pr_warning("Failed to write event: %s\n",
1508 static int get_new_event_name(char *buf
, size_t len
, const char *base
,
1509 struct strlist
*namelist
, bool allow_suffix
)
1514 ret
= e_snprintf(buf
, len
, "%s", base
);
1516 pr_debug("snprintf() failed: %s\n", strerror(-ret
));
1519 if (!strlist__has_entry(namelist
, buf
))
1522 if (!allow_suffix
) {
1523 pr_warning("Error: event \"%s\" already exists. "
1524 "(Use -f to force duplicates.)\n", base
);
1528 /* Try to add suffix */
1529 for (i
= 1; i
< MAX_EVENT_INDEX
; i
++) {
1530 ret
= e_snprintf(buf
, len
, "%s_%d", base
, i
);
1532 pr_debug("snprintf() failed: %s\n", strerror(-ret
));
1535 if (!strlist__has_entry(namelist
, buf
))
1538 if (i
== MAX_EVENT_INDEX
) {
1539 pr_warning("Too many events are on the same function.\n");
1546 static int __add_probe_trace_events(struct perf_probe_event
*pev
,
1547 struct probe_trace_event
*tevs
,
1548 int ntevs
, bool allow_suffix
)
1551 struct probe_trace_event
*tev
= NULL
;
1553 const char *event
, *group
;
1554 struct strlist
*namelist
;
1556 fd
= open_kprobe_events(true);
1559 /* Get current event names */
1560 namelist
= get_probe_trace_event_names(fd
, false);
1562 pr_debug("Failed to get current event list.\n");
1567 printf("Add new event%s\n", (ntevs
> 1) ? "s:" : ":");
1568 for (i
= 0; i
< ntevs
; i
++) {
1573 if (pev
->point
.function
)
1574 event
= pev
->point
.function
;
1576 event
= tev
->point
.symbol
;
1580 group
= PERFPROBE_GROUP
;
1582 /* Get an unused new event name */
1583 ret
= get_new_event_name(buf
, 64, event
,
1584 namelist
, allow_suffix
);
1589 tev
->event
= strdup(event
);
1590 tev
->group
= strdup(group
);
1591 if (tev
->event
== NULL
|| tev
->group
== NULL
) {
1595 ret
= write_probe_trace_event(fd
, tev
);
1598 /* Add added event name to namelist */
1599 strlist__add(namelist
, event
);
1601 /* Trick here - save current event/group */
1604 pev
->event
= tev
->event
;
1605 pev
->group
= tev
->group
;
1606 show_perf_probe_event(pev
);
1607 /* Trick here - restore current event/group */
1608 pev
->event
= (char *)event
;
1609 pev
->group
= (char *)group
;
1612 * Probes after the first probe which comes from same
1613 * user input are always allowed to add suffix, because
1614 * there might be several addresses corresponding to
1617 allow_suffix
= true;
1621 /* Show how to use the event. */
1622 printf("\nYou can now use it on all perf tools, such as:\n\n");
1623 printf("\tperf record -e %s:%s -aR sleep 1\n\n", tev
->group
,
1627 strlist__delete(namelist
);
1632 static int convert_to_probe_trace_events(struct perf_probe_event
*pev
,
1633 struct probe_trace_event
**tevs
,
1634 int max_tevs
, const char *module
)
1638 struct probe_trace_event
*tev
;
1640 /* Convert perf_probe_event with debuginfo */
1641 ret
= try_to_find_probe_trace_events(pev
, tevs
, max_tevs
, module
);
1645 /* Allocate trace event buffer */
1646 tev
= *tevs
= zalloc(sizeof(struct probe_trace_event
));
1650 /* Copy parameters */
1651 tev
->point
.symbol
= strdup(pev
->point
.function
);
1652 if (tev
->point
.symbol
== NULL
) {
1656 tev
->point
.offset
= pev
->point
.offset
;
1657 tev
->point
.retprobe
= pev
->point
.retprobe
;
1658 tev
->nargs
= pev
->nargs
;
1660 tev
->args
= zalloc(sizeof(struct probe_trace_arg
)
1662 if (tev
->args
== NULL
) {
1666 for (i
= 0; i
< tev
->nargs
; i
++) {
1667 if (pev
->args
[i
].name
) {
1668 tev
->args
[i
].name
= strdup(pev
->args
[i
].name
);
1669 if (tev
->args
[i
].name
== NULL
) {
1674 tev
->args
[i
].value
= strdup(pev
->args
[i
].var
);
1675 if (tev
->args
[i
].value
== NULL
) {
1679 if (pev
->args
[i
].type
) {
1680 tev
->args
[i
].type
= strdup(pev
->args
[i
].type
);
1681 if (tev
->args
[i
].type
== NULL
) {
1689 /* Currently just checking function name from symbol map */
1690 sym
= __find_kernel_function_by_name(tev
->point
.symbol
, NULL
);
1692 pr_warning("Kernel symbol \'%s\' not found.\n",
1700 clear_probe_trace_event(tev
);
1706 struct __event_package
{
1707 struct perf_probe_event
*pev
;
1708 struct probe_trace_event
*tevs
;
1712 int add_perf_probe_events(struct perf_probe_event
*pevs
, int npevs
,
1713 int max_tevs
, const char *module
, bool force_add
)
1716 struct __event_package
*pkgs
;
1718 pkgs
= zalloc(sizeof(struct __event_package
) * npevs
);
1722 /* Init vmlinux path */
1723 ret
= init_vmlinux();
1729 /* Loop 1: convert all events */
1730 for (i
= 0; i
< npevs
; i
++) {
1731 pkgs
[i
].pev
= &pevs
[i
];
1732 /* Convert with or without debuginfo */
1733 ret
= convert_to_probe_trace_events(pkgs
[i
].pev
,
1739 pkgs
[i
].ntevs
= ret
;
1742 /* Loop 2: add all events */
1743 for (i
= 0; i
< npevs
&& ret
>= 0; i
++)
1744 ret
= __add_probe_trace_events(pkgs
[i
].pev
, pkgs
[i
].tevs
,
1745 pkgs
[i
].ntevs
, force_add
);
1747 /* Loop 3: cleanup and free trace events */
1748 for (i
= 0; i
< npevs
; i
++) {
1749 for (j
= 0; j
< pkgs
[i
].ntevs
; j
++)
1750 clear_probe_trace_event(&pkgs
[i
].tevs
[j
]);
1758 static int __del_trace_probe_event(int fd
, struct str_node
*ent
)
1764 /* Convert from perf-probe event to trace-probe event */
1765 ret
= e_snprintf(buf
, 128, "-:%s", ent
->s
);
1769 p
= strchr(buf
+ 2, ':');
1771 pr_debug("Internal error: %s should have ':' but not.\n",
1778 pr_debug("Writing event: %s\n", buf
);
1779 ret
= write(fd
, buf
, strlen(buf
));
1783 printf("Remove event: %s\n", ent
->s
);
1786 pr_warning("Failed to delete event: %s\n", strerror(-ret
));
1790 static int del_trace_probe_event(int fd
, const char *group
,
1791 const char *event
, struct strlist
*namelist
)
1794 struct str_node
*ent
, *n
;
1795 int found
= 0, ret
= 0;
1797 ret
= e_snprintf(buf
, 128, "%s:%s", group
, event
);
1799 pr_err("Failed to copy event.");
1803 if (strpbrk(buf
, "*?")) { /* Glob-exp */
1804 strlist__for_each_safe(ent
, n
, namelist
)
1805 if (strglobmatch(ent
->s
, buf
)) {
1807 ret
= __del_trace_probe_event(fd
, ent
);
1810 strlist__remove(namelist
, ent
);
1813 ent
= strlist__find(namelist
, buf
);
1816 ret
= __del_trace_probe_event(fd
, ent
);
1818 strlist__remove(namelist
, ent
);
1821 if (found
== 0 && ret
>= 0)
1822 pr_info("Info: Event \"%s\" does not exist.\n", buf
);
1827 int del_perf_probe_events(struct strlist
*dellist
)
1830 const char *group
, *event
;
1832 struct str_node
*ent
;
1833 struct strlist
*namelist
;
1835 fd
= open_kprobe_events(true);
1839 /* Get current event names */
1840 namelist
= get_probe_trace_event_names(fd
, true);
1841 if (namelist
== NULL
)
1844 strlist__for_each(ent
, dellist
) {
1845 str
= strdup(ent
->s
);
1850 pr_debug("Parsing: %s\n", str
);
1851 p
= strchr(str
, ':');
1860 pr_debug("Group: %s, Event: %s\n", group
, event
);
1861 ret
= del_trace_probe_event(fd
, group
, event
, namelist
);
1866 strlist__delete(namelist
);