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>
45 #include "parse-events.h" /* For debugfs_path */
46 #include "probe-event.h"
47 #include "probe-finder.h"
49 #define MAX_CMDLEN 256
50 #define MAX_PROBE_ARGS 128
51 #define PERFPROBE_GROUP "probe"
53 bool probe_event_dry_run
; /* Dry run flag */
55 #define semantic_error(msg ...) die("Semantic error :" msg)
57 /* If there is no space to write, returns -E2BIG. */
58 static int e_snprintf(char *str
, size_t size
, const char *format
, ...)
59 __attribute__((format(printf
, 3, 4)));
61 static int e_snprintf(char *str
, size_t size
, const char *format
, ...)
66 ret
= vsnprintf(str
, size
, format
, ap
);
73 static struct map_groups kmap_groups
;
74 static struct map
*kmaps
[MAP__NR_TYPES
];
76 /* Initialize symbol maps for vmlinux */
77 static void init_vmlinux(void)
79 symbol_conf
.sort_by_name
= true;
80 if (symbol_conf
.vmlinux_name
== NULL
)
81 symbol_conf
.try_vmlinux_path
= true;
83 pr_debug("Use vmlinux: %s\n", symbol_conf
.vmlinux_name
);
84 if (symbol__init() < 0)
85 die("Failed to init symbol map.");
87 map_groups__init(&kmap_groups
);
88 if (map_groups__create_kernel_maps(&kmap_groups
, kmaps
) < 0)
89 die("Failed to create kernel maps.");
92 #ifndef NO_DWARF_SUPPORT
93 static int open_vmlinux(void)
95 if (map__load(kmaps
[MAP__FUNCTION
], NULL
) < 0) {
96 pr_debug("Failed to load kernel map.\n");
99 pr_debug("Try to open %s\n", kmaps
[MAP__FUNCTION
]->dso
->long_name
);
100 return open(kmaps
[MAP__FUNCTION
]->dso
->long_name
, O_RDONLY
);
104 void parse_line_range_desc(const char *arg
, struct line_range
*lr
)
111 * FUNC[:SLN[+NUM|-ELN]]
113 ptr
= strchr(arg
, ':');
115 lr
->start
= (unsigned int)strtoul(ptr
+ 1, &tmp
, 0);
117 lr
->end
= lr
->start
+ (unsigned int)strtoul(tmp
+ 1,
119 else if (*tmp
== '-')
120 lr
->end
= (unsigned int)strtoul(tmp
+ 1, &tmp
, 0);
123 pr_debug("Line range is %u to %u\n", lr
->start
, lr
->end
);
124 if (lr
->end
&& lr
->start
> lr
->end
)
125 semantic_error("Start line must be smaller"
128 semantic_error("Tailing with invalid character '%d'.",
130 tmp
= xstrndup(arg
, (ptr
- arg
));
134 if (strchr(tmp
, '.'))
140 /* Check the name is good for event/group */
141 static bool check_event_name(const char *name
)
143 if (!isalpha(*name
) && *name
!= '_')
145 while (*++name
!= '\0') {
146 if (!isalpha(*name
) && !isdigit(*name
) && *name
!= '_')
152 /* Parse probepoint definition. */
153 static void parse_perf_probe_point(char *arg
, struct perf_probe_event
*pev
)
155 struct perf_probe_point
*pp
= &pev
->point
;
160 * perf probe [EVENT=]SRC[:LN|;PTN]
161 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
163 * TODO:Group name support
166 ptr
= strpbrk(arg
, ";=@+%");
167 if (ptr
&& *ptr
== '=') { /* Event name */
170 ptr
= strchr(arg
, ':');
171 if (ptr
) /* Group name is not supported yet. */
172 semantic_error("Group name is not supported yet.");
173 if (!check_event_name(arg
))
174 semantic_error("%s is bad for event name -it must "
175 "follow C symbol-naming rule.", arg
);
176 pev
->event
= xstrdup(arg
);
181 ptr
= strpbrk(arg
, ";:+@%");
187 /* Check arg is function or file and copy it */
188 if (strchr(arg
, '.')) /* File */
189 pp
->file
= xstrdup(arg
);
191 pp
->function
= xstrdup(arg
);
193 /* Parse other options */
197 if (c
== ';') { /* Lazy pattern must be the last part */
198 pp
->lazy_line
= xstrdup(arg
);
201 ptr
= strpbrk(arg
, ";:+@%");
207 case ':': /* Line number */
208 pp
->line
= strtoul(arg
, &tmp
, 0);
210 semantic_error("There is non-digit char"
213 case '+': /* Byte offset from a symbol */
214 pp
->offset
= strtoul(arg
, &tmp
, 0);
216 semantic_error("There is non-digit character"
219 case '@': /* File name */
221 semantic_error("SRC@SRC is not allowed.");
222 pp
->file
= xstrdup(arg
);
224 case '%': /* Probe places */
225 if (strcmp(arg
, "return") == 0) {
227 } else /* Others not supported yet */
228 semantic_error("%%%s is not supported.", arg
);
231 DIE_IF("Program has a bug.");
236 /* Exclusion check */
237 if (pp
->lazy_line
&& pp
->line
)
238 semantic_error("Lazy pattern can't be used with line number.");
240 if (pp
->lazy_line
&& pp
->offset
)
241 semantic_error("Lazy pattern can't be used with offset.");
243 if (pp
->line
&& pp
->offset
)
244 semantic_error("Offset can't be used with line number.");
246 if (!pp
->line
&& !pp
->lazy_line
&& pp
->file
&& !pp
->function
)
247 semantic_error("File always requires line number or "
250 if (pp
->offset
&& !pp
->function
)
251 semantic_error("Offset requires an entry function.");
253 if (pp
->retprobe
&& !pp
->function
)
254 semantic_error("Return probe requires an entry function.");
256 if ((pp
->offset
|| pp
->line
|| pp
->lazy_line
) && pp
->retprobe
)
257 semantic_error("Offset/Line/Lazy pattern can't be used with "
260 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
261 pp
->function
, pp
->file
, pp
->line
, pp
->offset
, pp
->retprobe
,
265 /* Parse perf-probe event argument */
266 static void parse_perf_probe_arg(const char *str
, struct perf_probe_arg
*arg
)
269 struct perf_probe_arg_field
**fieldp
;
271 pr_debug("parsing arg: %s into ", str
);
273 tmp
= strpbrk(str
, "-.");
274 if (!is_c_varname(str
) || !tmp
) {
275 /* A variable, register, symbol or special value */
276 arg
->name
= xstrdup(str
);
277 pr_debug("%s\n", arg
->name
);
281 /* Structure fields */
282 arg
->name
= xstrndup(str
, tmp
- str
);
283 pr_debug("%s, ", arg
->name
);
284 fieldp
= &arg
->field
;
287 *fieldp
= xzalloc(sizeof(struct perf_probe_arg_field
));
290 (*fieldp
)->ref
= false;
291 } else if (tmp
[1] == '>') {
293 (*fieldp
)->ref
= true;
295 semantic_error("Argument parse error: %s", str
);
297 tmp
= strpbrk(str
, "-.");
299 (*fieldp
)->name
= xstrndup(str
, tmp
- str
);
300 pr_debug("%s(%d), ", (*fieldp
)->name
, (*fieldp
)->ref
);
301 fieldp
= &(*fieldp
)->next
;
304 (*fieldp
)->name
= xstrdup(str
);
305 pr_debug("%s(%d)\n", (*fieldp
)->name
, (*fieldp
)->ref
);
308 /* Parse perf-probe event command */
309 void parse_perf_probe_command(const char *cmd
, struct perf_probe_event
*pev
)
314 argv
= argv_split(cmd
, &argc
);
316 die("argv_split failed.");
317 if (argc
> MAX_PROBE_ARGS
+ 1)
318 semantic_error("Too many arguments");
320 /* Parse probe point */
321 parse_perf_probe_point(argv
[0], pev
);
323 /* Copy arguments and ensure return probe has no C argument */
324 pev
->nargs
= argc
- 1;
325 pev
->args
= xzalloc(sizeof(struct perf_probe_arg
) * pev
->nargs
);
326 for (i
= 0; i
< pev
->nargs
; i
++) {
327 parse_perf_probe_arg(argv
[i
+ 1], &pev
->args
[i
]);
328 if (is_c_varname(pev
->args
[i
].name
) && pev
->point
.retprobe
)
329 semantic_error("You can't specify local variable for"
336 /* Return true if this perf_probe_event requires debuginfo */
337 bool perf_probe_event_need_dwarf(struct perf_probe_event
*pev
)
341 if (pev
->point
.file
|| pev
->point
.line
|| pev
->point
.lazy_line
)
344 for (i
= 0; i
< pev
->nargs
; i
++)
345 if (is_c_varname(pev
->args
[i
].name
))
351 /* Parse kprobe_events event into struct probe_point */
352 void parse_kprobe_trace_command(const char *cmd
, struct kprobe_trace_event
*tev
)
354 struct kprobe_trace_point
*tp
= &tev
->point
;
360 pr_debug("Parsing kprobe_events: %s\n", cmd
);
361 argv
= argv_split(cmd
, &argc
);
363 die("argv_split failed.");
365 semantic_error("Too less arguments.");
367 /* Scan event and group name. */
368 ret
= sscanf(argv
[0], "%c:%a[^/ \t]/%a[^ \t]",
369 &pr
, (float *)(void *)&tev
->group
,
370 (float *)(void *)&tev
->event
);
372 semantic_error("Failed to parse event name: %s", argv
[0]);
373 pr_debug("Group:%s Event:%s probe:%c\n", tev
->group
, tev
->event
, pr
);
375 tp
->retprobe
= (pr
== 'r');
377 /* Scan function name and offset */
378 ret
= sscanf(argv
[1], "%a[^+]+%lu", (float *)(void *)&tp
->symbol
,
383 tev
->nargs
= argc
- 2;
384 tev
->args
= xzalloc(sizeof(struct kprobe_trace_arg
) * tev
->nargs
);
385 for (i
= 0; i
< tev
->nargs
; i
++) {
386 p
= strchr(argv
[i
+ 2], '=');
387 if (p
) /* We don't need which register is assigned. */
391 tev
->args
[i
].name
= xstrdup(argv
[i
+ 2]);
392 /* TODO: parse regs and offset */
393 tev
->args
[i
].value
= xstrdup(p
);
399 /* Compose only probe arg */
400 int synthesize_perf_probe_arg(struct perf_probe_arg
*pa
, char *buf
, size_t len
)
402 struct perf_probe_arg_field
*field
= pa
->field
;
406 ret
= e_snprintf(tmp
, len
, "%s", pa
->name
);
413 ret
= e_snprintf(tmp
, len
, "%s%s", field
->ref
? "->" : ".",
423 die("Failed to synthesize perf probe argument: %s", strerror(-ret
));
426 /* Compose only probe point (not argument) */
427 static char *synthesize_perf_probe_point(struct perf_probe_point
*pp
)
430 char offs
[32] = "", line
[32] = "", file
[32] = "";
433 buf
= xzalloc(MAX_CMDLEN
);
435 ret
= e_snprintf(offs
, 32, "+%lu", pp
->offset
);
440 ret
= e_snprintf(line
, 32, ":%d", pp
->line
);
445 len
= strlen(pp
->file
) - 32;
448 tmp
= strchr(pp
->file
+ len
, '/');
450 tmp
= pp
->file
+ len
- 1;
451 ret
= e_snprintf(file
, 32, "@%s", tmp
+ 1);
457 ret
= e_snprintf(buf
, MAX_CMDLEN
, "%s%s%s%s%s", pp
->function
,
458 offs
, pp
->retprobe
? "%return" : "", line
,
461 ret
= e_snprintf(buf
, MAX_CMDLEN
, "%s%s", file
, line
);
467 die("Failed to synthesize perf probe point: %s", strerror(-ret
));
471 char *synthesize_perf_probe_command(struct perf_probe_event
*pev
)
476 buf
= synthesize_perf_probe_point(&pev
->point
);
481 for (i
= 0; i
< pev
->nargs
; i
++) {
482 ret
= e_snprintf(&buf
[len
], MAX_CMDLEN
- len
, " %s",
495 static int __synthesize_kprobe_trace_arg_ref(struct kprobe_trace_arg_ref
*ref
,
496 char **buf
, size_t *buflen
,
501 depth
= __synthesize_kprobe_trace_arg_ref(ref
->next
, buf
,
507 ret
= e_snprintf(*buf
, *buflen
, "%+ld(", ref
->offset
);
519 static int synthesize_kprobe_trace_arg(struct kprobe_trace_arg
*arg
,
520 char *buf
, size_t buflen
)
525 /* Argument name or separator */
527 ret
= e_snprintf(buf
, buflen
, " %s=", arg
->name
);
529 ret
= e_snprintf(buf
, buflen
, " ");
535 /* Dereferencing arguments */
537 depth
= __synthesize_kprobe_trace_arg_ref(arg
->ref
, &buf
,
543 /* Print argument value */
544 ret
= e_snprintf(buf
, buflen
, "%s", arg
->value
);
552 ret
= e_snprintf(buf
, buflen
, ")");
562 char *synthesize_kprobe_trace_command(struct kprobe_trace_event
*tev
)
564 struct kprobe_trace_point
*tp
= &tev
->point
;
568 buf
= xzalloc(MAX_CMDLEN
);
569 len
= e_snprintf(buf
, MAX_CMDLEN
, "%c:%s/%s %s+%lu",
570 tp
->retprobe
? 'r' : 'p',
571 tev
->group
, tev
->event
,
572 tp
->symbol
, tp
->offset
);
576 for (i
= 0; i
< tev
->nargs
; i
++) {
577 ret
= synthesize_kprobe_trace_arg(&tev
->args
[i
], buf
+ len
,
590 void convert_to_perf_probe_event(struct kprobe_trace_event
*tev
,
591 struct perf_probe_event
*pev
)
595 #ifndef NO_DWARF_SUPPORT
599 sym
= map__find_symbol_by_name(kmaps
[MAP__FUNCTION
],
600 tev
->point
.symbol
, NULL
);
603 ret
= find_perf_probe_point(fd
, sym
->start
+ tev
->point
.offset
,
608 pev
->point
.function
= xstrdup(tev
->point
.symbol
);
609 pev
->point
.offset
= tev
->point
.offset
;
612 /* Convert trace_point to probe_point */
613 pev
->point
.function
= xstrdup(tev
->point
.symbol
);
614 pev
->point
.offset
= tev
->point
.offset
;
616 pev
->point
.retprobe
= tev
->point
.retprobe
;
618 pev
->event
= xstrdup(tev
->event
);
619 pev
->group
= xstrdup(tev
->group
);
621 /* Convert trace_arg to probe_arg */
622 pev
->nargs
= tev
->nargs
;
623 pev
->args
= xzalloc(sizeof(struct perf_probe_arg
) * pev
->nargs
);
624 for (i
= 0; i
< tev
->nargs
; i
++)
625 if (tev
->args
[i
].name
)
626 pev
->args
[i
].name
= xstrdup(tev
->args
[i
].name
);
628 synthesize_kprobe_trace_arg(&tev
->args
[i
], buf
, 64);
629 pev
->args
[i
].name
= xstrdup(buf
);
633 void clear_perf_probe_event(struct perf_probe_event
*pev
)
635 struct perf_probe_point
*pp
= &pev
->point
;
636 struct perf_probe_arg_field
*field
, *next
;
649 for (i
= 0; i
< pev
->nargs
; i
++) {
650 if (pev
->args
[i
].name
)
651 free(pev
->args
[i
].name
);
652 field
= pev
->args
[i
].field
;
663 memset(pev
, 0, sizeof(*pev
));
666 void clear_kprobe_trace_event(struct kprobe_trace_event
*tev
)
668 struct kprobe_trace_arg_ref
*ref
, *next
;
675 if (tev
->point
.symbol
)
676 free(tev
->point
.symbol
);
677 for (i
= 0; i
< tev
->nargs
; i
++) {
678 if (tev
->args
[i
].name
)
679 free(tev
->args
[i
].name
);
680 if (tev
->args
[i
].value
)
681 free(tev
->args
[i
].value
);
682 ref
= tev
->args
[i
].ref
;
691 memset(tev
, 0, sizeof(*tev
));
694 static int open_kprobe_events(bool readwrite
)
699 ret
= e_snprintf(buf
, PATH_MAX
, "%s/../kprobe_events", debugfs_path
);
701 die("Failed to make kprobe_events path.");
703 if (readwrite
&& !probe_event_dry_run
)
704 ret
= open(buf
, O_RDWR
, O_APPEND
);
706 ret
= open(buf
, O_RDONLY
, 0);
710 die("kprobe_events file does not exist -"
711 " please rebuild with CONFIG_KPROBE_EVENT.");
713 die("Could not open kprobe_events file: %s",
719 /* Get raw string list of current kprobe_events */
720 static struct strlist
*get_kprobe_trace_command_rawlist(int fd
)
724 char buf
[MAX_CMDLEN
];
728 sl
= strlist__new(true, NULL
);
730 fp
= fdopen(dup(fd
), "r");
732 p
= fgets(buf
, MAX_CMDLEN
, fp
);
739 ret
= strlist__add(sl
, buf
);
741 die("strlist__add failed: %s", strerror(-ret
));
749 static void show_perf_probe_event(struct perf_probe_event
*pev
)
755 /* Synthesize only event probe point */
756 place
= synthesize_perf_probe_point(&pev
->point
);
758 ret
= e_snprintf(buf
, 128, "%s:%s", pev
->group
, pev
->event
);
760 die("Failed to copy event: %s", strerror(-ret
));
761 printf(" %-20s (on %s", buf
, place
);
763 if (pev
->nargs
> 0) {
765 for (i
= 0; i
< pev
->nargs
; i
++) {
766 synthesize_perf_probe_arg(&pev
->args
[i
], buf
, 128);
774 /* List up current perf-probe events */
775 void show_perf_probe_events(void)
778 struct kprobe_trace_event tev
;
779 struct perf_probe_event pev
;
780 struct strlist
*rawlist
;
781 struct str_node
*ent
;
786 memset(&tev
, 0, sizeof(tev
));
787 memset(&pev
, 0, sizeof(pev
));
789 fd
= open_kprobe_events(false);
790 rawlist
= get_kprobe_trace_command_rawlist(fd
);
793 strlist__for_each(ent
, rawlist
) {
794 parse_kprobe_trace_command(ent
->s
, &tev
);
795 convert_to_perf_probe_event(&tev
, &pev
);
797 show_perf_probe_event(&pev
);
798 clear_perf_probe_event(&pev
);
799 clear_kprobe_trace_event(&tev
);
802 strlist__delete(rawlist
);
805 /* Get current perf-probe event names */
806 static struct strlist
*get_kprobe_trace_event_names(int fd
, bool include_group
)
809 struct strlist
*sl
, *rawlist
;
810 struct str_node
*ent
;
811 struct kprobe_trace_event tev
;
813 memset(&tev
, 0, sizeof(tev
));
815 rawlist
= get_kprobe_trace_command_rawlist(fd
);
816 sl
= strlist__new(true, NULL
);
817 strlist__for_each(ent
, rawlist
) {
818 parse_kprobe_trace_command(ent
->s
, &tev
);
820 if (e_snprintf(buf
, 128, "%s:%s", tev
.group
,
822 die("Failed to copy group:event name.");
823 strlist__add(sl
, buf
);
825 strlist__add(sl
, tev
.event
);
826 clear_kprobe_trace_event(&tev
);
829 strlist__delete(rawlist
);
834 static void write_kprobe_trace_event(int fd
, struct kprobe_trace_event
*tev
)
837 char *buf
= synthesize_kprobe_trace_command(tev
);
839 pr_debug("Writing event: %s\n", buf
);
840 if (!probe_event_dry_run
) {
841 ret
= write(fd
, buf
, strlen(buf
));
843 die("Failed to write event: %s", strerror(errno
));
848 static void get_new_event_name(char *buf
, size_t len
, const char *base
,
849 struct strlist
*namelist
, bool allow_suffix
)
854 ret
= e_snprintf(buf
, len
, "%s", base
);
856 die("snprintf() failed: %s", strerror(-ret
));
857 if (!strlist__has_entry(namelist
, buf
))
861 pr_warning("Error: event \"%s\" already exists. "
862 "(Use -f to force duplicates.)\n", base
);
863 die("Can't add new event.");
866 /* Try to add suffix */
867 for (i
= 1; i
< MAX_EVENT_INDEX
; i
++) {
868 ret
= e_snprintf(buf
, len
, "%s_%d", base
, i
);
870 die("snprintf() failed: %s", strerror(-ret
));
871 if (!strlist__has_entry(namelist
, buf
))
874 if (i
== MAX_EVENT_INDEX
)
875 die("Too many events are on the same function.");
878 static void __add_kprobe_trace_events(struct perf_probe_event
*pev
,
879 struct kprobe_trace_event
*tevs
,
880 int ntevs
, bool allow_suffix
)
883 struct kprobe_trace_event
*tev
= NULL
;
885 const char *event
, *group
;
886 struct strlist
*namelist
;
888 fd
= open_kprobe_events(true);
889 /* Get current event names */
890 namelist
= get_kprobe_trace_event_names(fd
, false);
892 printf("Add new event%s\n", (ntevs
> 1) ? "s:" : ":");
893 for (i
= 0; i
< ntevs
; i
++) {
898 if (pev
->point
.function
)
899 event
= pev
->point
.function
;
901 event
= tev
->point
.symbol
;
905 group
= PERFPROBE_GROUP
;
907 /* Get an unused new event name */
908 get_new_event_name(buf
, 64, event
, namelist
, allow_suffix
);
911 tev
->event
= xstrdup(event
);
912 tev
->group
= xstrdup(group
);
913 write_kprobe_trace_event(fd
, tev
);
914 /* Add added event name to namelist */
915 strlist__add(namelist
, event
);
917 /* Trick here - save current event/group */
920 pev
->event
= tev
->event
;
921 pev
->group
= tev
->group
;
922 show_perf_probe_event(pev
);
923 /* Trick here - restore current event/group */
924 pev
->event
= (char *)event
;
925 pev
->group
= (char *)group
;
928 * Probes after the first probe which comes from same
929 * user input are always allowed to add suffix, because
930 * there might be several addresses corresponding to
935 /* Show how to use the event. */
936 printf("\nYou can now use it on all perf tools, such as:\n\n");
937 printf("\tperf record -e %s:%s -a sleep 1\n\n", tev
->group
, tev
->event
);
939 strlist__delete(namelist
);
943 static int convert_to_kprobe_trace_events(struct perf_probe_event
*pev
,
944 struct kprobe_trace_event
**tevs
)
948 #ifndef NO_DWARF_SUPPORT
952 struct kprobe_trace_event
*tev
;
954 need_dwarf
= perf_probe_event_need_dwarf(pev
);
957 #ifdef NO_DWARF_SUPPORT
958 die("Debuginfo-analysis is not supported");
959 #else /* !NO_DWARF_SUPPORT */
960 pr_debug("Some probes require debuginfo.\n");
965 die("Could not open debuginfo file.");
967 pr_debug("Could not open vmlinux/module file."
968 " Try to use symbols.\n");
972 /* Searching probe points */
973 ntevs
= find_kprobe_trace_events(fd
, pev
, tevs
);
975 if (ntevs
> 0) /* Found */
978 if (ntevs
== 0) /* No error but failed to find probe point. */
979 die("Probe point '%s' not found. - probe not added.",
980 synthesize_perf_probe_point(&pev
->point
));
984 if (ntevs
== -ENOENT
)
985 pr_warning("No dwarf info found in the vmlinux - please rebuild with CONFIG_DEBUG_INFO=y.\n");
986 die("Could not analyze debuginfo.");
988 pr_debug("An error occurred in debuginfo analysis."
989 " Try to use symbols.\n");
992 #endif /* !NO_DWARF_SUPPORT */
994 /* Allocate trace event buffer */
996 tev
= *tevs
= xzalloc(sizeof(struct kprobe_trace_event
));
998 /* Copy parameters */
999 tev
->point
.symbol
= xstrdup(pev
->point
.function
);
1000 tev
->point
.offset
= pev
->point
.offset
;
1001 tev
->nargs
= pev
->nargs
;
1003 tev
->args
= xzalloc(sizeof(struct kprobe_trace_arg
)
1005 for (i
= 0; i
< tev
->nargs
; i
++)
1006 tev
->args
[i
].value
= xstrdup(pev
->args
[i
].name
);
1009 /* Currently just checking function name from symbol map */
1010 sym
= map__find_symbol_by_name(kmaps
[MAP__FUNCTION
],
1011 tev
->point
.symbol
, NULL
);
1013 die("Kernel symbol \'%s\' not found - probe not added.",
1015 #ifndef NO_DWARF_SUPPORT
1022 struct __event_package
{
1023 struct perf_probe_event
*pev
;
1024 struct kprobe_trace_event
*tevs
;
1028 void add_perf_probe_events(struct perf_probe_event
*pevs
, int npevs
,
1032 struct __event_package
*pkgs
;
1034 pkgs
= xzalloc(sizeof(struct __event_package
) * npevs
);
1036 /* Init vmlinux path */
1039 /* Loop 1: convert all events */
1040 for (i
= 0; i
< npevs
; i
++) {
1041 pkgs
[i
].pev
= &pevs
[i
];
1042 /* Convert with or without debuginfo */
1043 pkgs
[i
].ntevs
= convert_to_kprobe_trace_events(pkgs
[i
].pev
,
1047 /* Loop 2: add all events */
1048 for (i
= 0; i
< npevs
; i
++)
1049 __add_kprobe_trace_events(pkgs
[i
].pev
, pkgs
[i
].tevs
,
1050 pkgs
[i
].ntevs
, force_add
);
1051 /* TODO: cleanup all trace events? */
1054 static void __del_trace_kprobe_event(int fd
, struct str_node
*ent
)
1060 /* Convert from perf-probe event to trace-kprobe event */
1061 if (e_snprintf(buf
, 128, "-:%s", ent
->s
) < 0)
1062 die("Failed to copy event.");
1063 p
= strchr(buf
+ 2, ':');
1065 die("Internal error: %s should have ':' but not.", ent
->s
);
1068 pr_debug("Writing event: %s\n", buf
);
1069 ret
= write(fd
, buf
, strlen(buf
));
1071 die("Failed to write event: %s", strerror(errno
));
1072 printf("Remove event: %s\n", ent
->s
);
1075 static void del_trace_kprobe_event(int fd
, const char *group
,
1076 const char *event
, struct strlist
*namelist
)
1079 struct str_node
*ent
, *n
;
1082 if (e_snprintf(buf
, 128, "%s:%s", group
, event
) < 0)
1083 die("Failed to copy event.");
1085 if (strpbrk(buf
, "*?")) { /* Glob-exp */
1086 strlist__for_each_safe(ent
, n
, namelist
)
1087 if (strglobmatch(ent
->s
, buf
)) {
1089 __del_trace_kprobe_event(fd
, ent
);
1090 strlist__remove(namelist
, ent
);
1093 ent
= strlist__find(namelist
, buf
);
1096 __del_trace_kprobe_event(fd
, ent
);
1097 strlist__remove(namelist
, ent
);
1101 pr_info("Info: event \"%s\" does not exist, could not remove it.\n", buf
);
1104 void del_perf_probe_events(struct strlist
*dellist
)
1107 const char *group
, *event
;
1109 struct str_node
*ent
;
1110 struct strlist
*namelist
;
1112 fd
= open_kprobe_events(true);
1113 /* Get current event names */
1114 namelist
= get_kprobe_trace_event_names(fd
, true);
1116 strlist__for_each(ent
, dellist
) {
1117 str
= xstrdup(ent
->s
);
1118 pr_debug("Parsing: %s\n", str
);
1119 p
= strchr(str
, ':');
1128 pr_debug("Group: %s, Event: %s\n", group
, event
);
1129 del_trace_kprobe_event(fd
, group
, event
, namelist
);
1132 strlist__delete(namelist
);
1136 #define LINEBUF_SIZE 256
1137 #define NR_ADDITIONAL_LINES 2
1139 static void show_one_line(FILE *fp
, unsigned int l
, bool skip
, bool show_num
)
1141 char buf
[LINEBUF_SIZE
];
1142 const char *color
= PERF_COLOR_BLUE
;
1144 if (fgets(buf
, LINEBUF_SIZE
, fp
) == NULL
)
1148 fprintf(stdout
, "%7u %s", l
, buf
);
1150 color_fprintf(stdout
, color
, " %s", buf
);
1153 while (strlen(buf
) == LINEBUF_SIZE
- 1 &&
1154 buf
[LINEBUF_SIZE
- 2] != '\n') {
1155 if (fgets(buf
, LINEBUF_SIZE
, fp
) == NULL
)
1159 fprintf(stdout
, "%s", buf
);
1161 color_fprintf(stdout
, color
, "%s", buf
);
1167 die("Source file is shorter than expected.");
1169 die("File read error: %s", strerror(errno
));
1172 void show_line_range(struct line_range
*lr
)
1175 struct line_node
*ln
;
1177 #ifndef NO_DWARF_SUPPORT
1181 /* Search a line range */
1183 #ifndef NO_DWARF_SUPPORT
1184 fd
= open_vmlinux();
1186 die("Could not open debuginfo file.");
1187 ret
= find_line_range(fd
, lr
);
1189 die("Source line is not found.\n");
1196 fprintf(stdout
, "<%s:%d>\n", lr
->function
,
1197 lr
->start
- lr
->offset
);
1199 fprintf(stdout
, "<%s:%d>\n", lr
->file
, lr
->start
);
1201 fp
= fopen(lr
->path
, "r");
1203 die("Failed to open %s: %s", lr
->path
, strerror(errno
));
1204 /* Skip to starting line number */
1205 while (l
< lr
->start
)
1206 show_one_line(fp
, l
++, true, false);
1208 list_for_each_entry(ln
, &lr
->line_list
, list
) {
1209 while (ln
->line
> l
)
1210 show_one_line(fp
, (l
++) - lr
->offset
, false, false);
1211 show_one_line(fp
, (l
++) - lr
->offset
, false, true);
1214 if (lr
->end
== INT_MAX
)
1215 lr
->end
= l
+ NR_ADDITIONAL_LINES
;
1216 while (l
< lr
->end
&& !feof(fp
))
1217 show_one_line(fp
, (l
++) - lr
->offset
, false, false);
This page took 0.057399 seconds and 5 git commands to generate.