6b13a1ecf1e774bc6f8d613a017bf696edbdb760
4 * Builtin annotate command: Analyze the perf.data input file,
5 * look up and read DSOs and symbol information and display
6 * a histogram of results, along various sorting keys.
10 #include "util/util.h"
12 #include "util/color.h"
13 #include <linux/list.h>
14 #include "util/cache.h"
15 #include <linux/rbtree.h>
16 #include "util/symbol.h"
17 #include "util/string.h"
20 #include "util/debug.h"
22 #include "util/parse-options.h"
23 #include "util/parse-events.h"
24 #include "util/thread.h"
25 #include "util/sort.h"
26 #include "util/hist.h"
28 static char const *input_name
= "perf.data";
33 static int full_paths
;
35 static int print_line
;
36 static bool use_modules
;
38 static unsigned long page_size
;
39 static unsigned long mmap_window
= 32;
40 const char *vmlinux_name
;
54 struct sym_hist
*hist
;
58 static const char *sym_hist_filter
;
60 static int symbol_filter(struct map
*map __used
, struct symbol
*sym
)
62 if (sym_hist_filter
== NULL
||
63 strcmp(sym
->name
, sym_hist_filter
) == 0) {
64 struct sym_priv
*priv
= symbol__priv(sym
);
65 const int size
= (sizeof(*priv
->hist
) +
66 (sym
->end
- sym
->start
) * sizeof(u64
));
68 priv
->hist
= malloc(size
);
70 memset(priv
->hist
, 0, size
);
74 * FIXME: We should really filter it out, as we don't want to go thru symbols
75 * we're not interested, and if a DSO ends up with no symbols, delete it too,
76 * but right now the kernel loading routines in symbol.c bail out if no symbols
77 * are found, fix it later.
83 * collect histogram counts
85 static void hist_hit(struct hist_entry
*he
, u64 ip
)
87 unsigned int sym_size
, offset
;
88 struct symbol
*sym
= he
->sym
;
89 struct sym_priv
*priv
;
97 priv
= symbol__priv(sym
);
101 sym_size
= sym
->end
- sym
->start
;
102 offset
= ip
- sym
->start
;
105 fprintf(stderr
, "%s: ip=%Lx\n", __func__
,
106 he
->map
->unmap_ip(he
->map
, ip
));
108 if (offset
>= sym_size
)
116 printf("%p %s: count++ [ip: %p, %08Lx] => %Ld\n",
117 (void *)(unsigned long)he
->sym
->start
,
119 (void *)(unsigned long)ip
, ip
- he
->sym
->start
,
123 static int hist_entry__add(struct thread
*thread
, struct map
*map
,
124 struct symbol
*sym
, u64 ip
, u64 count
, char level
)
127 struct hist_entry
*he
= __hist_entry__add(thread
, map
, sym
, NULL
, ip
,
136 process_sample_event(event_t
*event
, unsigned long offset
, unsigned long head
)
139 u64 ip
= event
->ip
.ip
;
140 struct map
*map
= NULL
;
141 struct symbol
*sym
= NULL
;
142 struct thread
*thread
= threads__findnew(event
->ip
.pid
);
144 dump_printf("%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
145 (void *)(offset
+ head
),
146 (void *)(long)(event
->header
.size
),
151 if (thread
== NULL
) {
152 fprintf(stderr
, "problem processing %d event, skipping it.\n",
157 dump_printf(" ... thread: %s:%d\n", thread
->comm
, thread
->pid
);
159 if (event
->header
.misc
& PERF_RECORD_MISC_KERNEL
) {
161 sym
= kernel_maps__find_symbol(ip
, &map
, symbol_filter
);
162 dump_printf(" ...... dso: %s\n",
163 map
? map
->dso
->long_name
: "<not found>");
164 } else if (event
->header
.misc
& PERF_RECORD_MISC_USER
) {
166 map
= thread__find_map(thread
, ip
);
169 ip
= map
->map_ip(map
, ip
);
170 sym
= map__find_symbol(map
, ip
, symbol_filter
);
173 * If this is outside of all known maps,
174 * and is a negative address, try to look it
175 * up in the kernel dso, as it might be a
176 * vsyscall or vdso (which executes in user-mode).
178 * XXX This is nasty, we should have a symbol list in
179 * the "[vdso]" dso, but for now lets use the old
180 * trick of looking in the whole kernel symbol list.
182 if ((long long)ip
< 0) {
187 dump_printf(" ...... dso: %s\n",
188 map
? map
->dso
->long_name
: "<not found>");
191 dump_printf(" ...... dso: [hypervisor]\n");
194 if (hist_entry__add(thread
, map
, sym
, ip
, 1, level
)) {
195 fprintf(stderr
, "problem incrementing symbol count, "
205 process_mmap_event(event_t
*event
, unsigned long offset
, unsigned long head
)
207 struct map
*map
= map__new(&event
->mmap
, NULL
, 0);
208 struct thread
*thread
= threads__findnew(event
->mmap
.pid
);
210 dump_printf("%p [%p]: PERF_RECORD_MMAP %d: [%p(%p) @ %p]: %s\n",
211 (void *)(offset
+ head
),
212 (void *)(long)(event
->header
.size
),
214 (void *)(long)event
->mmap
.start
,
215 (void *)(long)event
->mmap
.len
,
216 (void *)(long)event
->mmap
.pgoff
,
217 event
->mmap
.filename
);
219 if (thread
== NULL
|| map
== NULL
) {
220 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
224 thread__insert_map(thread
, map
);
231 process_comm_event(event_t
*event
, unsigned long offset
, unsigned long head
)
233 struct thread
*thread
= threads__findnew(event
->comm
.pid
);
235 dump_printf("%p [%p]: PERF_RECORD_COMM: %s:%d\n",
236 (void *)(offset
+ head
),
237 (void *)(long)(event
->header
.size
),
238 event
->comm
.comm
, event
->comm
.pid
);
240 if (thread
== NULL
||
241 thread__set_comm(thread
, event
->comm
.comm
)) {
242 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
251 process_fork_event(event_t
*event
, unsigned long offset
, unsigned long head
)
253 struct thread
*thread
= threads__findnew(event
->fork
.pid
);
254 struct thread
*parent
= threads__findnew(event
->fork
.ppid
);
256 dump_printf("%p [%p]: PERF_RECORD_FORK: %d:%d\n",
257 (void *)(offset
+ head
),
258 (void *)(long)(event
->header
.size
),
259 event
->fork
.pid
, event
->fork
.ppid
);
262 * A thread clone will have the same PID for both
265 if (thread
== parent
)
268 if (!thread
|| !parent
|| thread__fork(thread
, parent
)) {
269 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
278 process_event(event_t
*event
, unsigned long offset
, unsigned long head
)
280 switch (event
->header
.type
) {
281 case PERF_RECORD_SAMPLE
:
282 return process_sample_event(event
, offset
, head
);
284 case PERF_RECORD_MMAP
:
285 return process_mmap_event(event
, offset
, head
);
287 case PERF_RECORD_COMM
:
288 return process_comm_event(event
, offset
, head
);
290 case PERF_RECORD_FORK
:
291 return process_fork_event(event
, offset
, head
);
293 * We dont process them right now but they are fine:
296 case PERF_RECORD_THROTTLE
:
297 case PERF_RECORD_UNTHROTTLE
:
307 static int parse_line(FILE *file
, struct hist_entry
*he
, u64 len
)
309 struct symbol
*sym
= he
->sym
;
310 char *line
= NULL
, *tmp
, *tmp2
;
311 static const char *prev_line
;
312 static const char *prev_color
;
320 if (getline(&line
, &line_len
, file
) < 0)
325 c
= strchr(line
, '\n');
334 * Strip leading spaces:
345 * Parse hexa addresses followed by ':'
347 line_ip
= strtoull(tmp
, &tmp2
, 16);
352 start
= he
->map
->unmap_ip(he
->map
, sym
->start
);
355 const char *path
= NULL
;
356 unsigned int hits
= 0;
357 double percent
= 0.0;
359 struct sym_priv
*priv
= symbol__priv(sym
);
360 struct sym_ext
*sym_ext
= priv
->ext
;
361 struct sym_hist
*h
= priv
->hist
;
363 offset
= line_ip
- start
;
365 hits
= h
->ip
[offset
];
367 if (offset
< len
&& sym_ext
) {
368 path
= sym_ext
[offset
].path
;
369 percent
= sym_ext
[offset
].percent
;
371 percent
= 100.0 * hits
/ h
->sum
;
373 color
= get_percent_color(percent
);
376 * Also color the filename and line if needed, with
377 * the same color than the percentage. Don't print it
378 * twice for close colored ip with the same filename:line
381 if (!prev_line
|| strcmp(prev_line
, path
)
382 || color
!= prev_color
) {
383 color_fprintf(stdout
, color
, " %s", path
);
389 color_fprintf(stdout
, color
, " %7.2f", percent
);
391 color_fprintf(stdout
, PERF_COLOR_BLUE
, "%s\n", line
);
396 printf(" : %s\n", line
);
402 static struct rb_root root_sym_ext
;
404 static void insert_source_line(struct sym_ext
*sym_ext
)
406 struct sym_ext
*iter
;
407 struct rb_node
**p
= &root_sym_ext
.rb_node
;
408 struct rb_node
*parent
= NULL
;
412 iter
= rb_entry(parent
, struct sym_ext
, node
);
414 if (sym_ext
->percent
> iter
->percent
)
420 rb_link_node(&sym_ext
->node
, parent
, p
);
421 rb_insert_color(&sym_ext
->node
, &root_sym_ext
);
424 static void free_source_line(struct hist_entry
*he
, int len
)
426 struct sym_priv
*priv
= symbol__priv(he
->sym
);
427 struct sym_ext
*sym_ext
= priv
->ext
;
433 for (i
= 0; i
< len
; i
++)
434 free(sym_ext
[i
].path
);
438 root_sym_ext
= RB_ROOT
;
441 /* Get the filename:line for the colored entries */
443 get_source_line(struct hist_entry
*he
, int len
, const char *filename
)
445 struct symbol
*sym
= he
->sym
;
448 char cmd
[PATH_MAX
* 2];
449 struct sym_ext
*sym_ext
;
450 struct sym_priv
*priv
= symbol__priv(sym
);
451 struct sym_hist
*h
= priv
->hist
;
456 sym_ext
= priv
->ext
= calloc(len
, sizeof(struct sym_ext
));
460 start
= he
->map
->unmap_ip(he
->map
, sym
->start
);
462 for (i
= 0; i
< len
; i
++) {
468 sym_ext
[i
].percent
= 100.0 * h
->ip
[i
] / h
->sum
;
469 if (sym_ext
[i
].percent
<= 0.5)
473 sprintf(cmd
, "addr2line -e %s %016llx", filename
, offset
);
474 fp
= popen(cmd
, "r");
478 if (getline(&path
, &line_len
, fp
) < 0 || !line_len
)
481 sym_ext
[i
].path
= malloc(sizeof(char) * line_len
+ 1);
482 if (!sym_ext
[i
].path
)
485 strcpy(sym_ext
[i
].path
, path
);
486 insert_source_line(&sym_ext
[i
]);
493 static void print_summary(const char *filename
)
495 struct sym_ext
*sym_ext
;
496 struct rb_node
*node
;
498 printf("\nSorted summary for file %s\n", filename
);
499 printf("----------------------------------------------\n\n");
501 if (RB_EMPTY_ROOT(&root_sym_ext
)) {
502 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN
);
506 node
= rb_first(&root_sym_ext
);
512 sym_ext
= rb_entry(node
, struct sym_ext
, node
);
513 percent
= sym_ext
->percent
;
514 color
= get_percent_color(percent
);
515 path
= sym_ext
->path
;
517 color_fprintf(stdout
, color
, " %7.2f %s", percent
, path
);
518 node
= rb_next(node
);
522 static void annotate_sym(struct hist_entry
*he
)
524 struct map
*map
= he
->map
;
525 struct dso
*dso
= map
->dso
;
526 struct symbol
*sym
= he
->sym
;
527 const char *filename
= dso
->long_name
, *d_filename
;
529 char command
[PATH_MAX
*2];
536 fprintf(stderr
, "%s: filename=%s, sym=%s, start=%Lx, end=%Lx\n",
537 __func__
, filename
, sym
->name
,
538 map
->unmap_ip(map
, sym
->start
),
539 map
->unmap_ip(map
, sym
->end
));
542 d_filename
= filename
;
544 d_filename
= basename(filename
);
546 len
= sym
->end
- sym
->start
;
549 get_source_line(he
, len
, filename
);
550 print_summary(filename
);
553 printf("\n\n------------------------------------------------\n");
554 printf(" Percent | Source code & Disassembly of %s\n", d_filename
);
555 printf("------------------------------------------------\n");
558 printf("annotating [%p] %30s : [%p] %30s\n",
559 dso
, dso
->long_name
, sym
, sym
->name
);
561 sprintf(command
, "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS %s|grep -v %s",
562 map
->unmap_ip(map
, sym
->start
), map
->unmap_ip(map
, sym
->end
),
566 printf("doing: %s\n", command
);
568 file
= popen(command
, "r");
572 while (!feof(file
)) {
573 if (parse_line(file
, he
, len
) < 0)
579 free_source_line(he
, len
);
582 static void find_annotations(void)
586 for (nd
= rb_first(&output_hists
); nd
; nd
= rb_next(nd
)) {
587 struct hist_entry
*he
= rb_entry(nd
, struct hist_entry
, rb_node
);
588 struct sym_priv
*priv
;
593 priv
= symbol__priv(he
->sym
);
594 if (priv
->hist
== NULL
)
599 * Since we have a hist_entry per IP for the same symbol, free
600 * he->sym->hist to signal we already processed this symbol.
607 static int __cmd_annotate(void)
609 int ret
, rc
= EXIT_FAILURE
;
610 unsigned long offset
= 0;
611 unsigned long head
= 0;
612 struct stat input_stat
;
617 register_idle_thread();
619 input
= open(input_name
, O_RDONLY
);
621 perror("failed to open file");
625 ret
= fstat(input
, &input_stat
);
627 perror("failed to stat file");
631 if (!force
&& input_stat
.st_uid
&& (input_stat
.st_uid
!= geteuid())) {
632 fprintf(stderr
, "file: %s not owned by current user or root\n", input_name
);
636 if (!input_stat
.st_size
) {
637 fprintf(stderr
, "zero-sized file, nothing to do!\n");
641 if (kernel_maps__init(vmlinux_name
, true, use_modules
) < 0) {
642 pr_err("failed to create kernel maps for symbol resolution\b");
647 buf
= (char *)mmap(NULL
, page_size
* mmap_window
, PROT_READ
,
648 MAP_SHARED
, input
, offset
);
649 if (buf
== MAP_FAILED
) {
650 perror("failed to mmap file");
655 event
= (event_t
*)(buf
+ head
);
657 size
= event
->header
.size
;
661 if (head
+ event
->header
.size
>= page_size
* mmap_window
) {
662 unsigned long shift
= page_size
* (head
/ page_size
);
665 munmap_ret
= munmap(buf
, page_size
* mmap_window
);
666 assert(munmap_ret
== 0);
673 size
= event
->header
.size
;
675 dump_printf("%p [%p]: event: %d\n",
676 (void *)(offset
+ head
),
677 (void *)(long)event
->header
.size
,
680 if (!size
|| process_event(event
, offset
, head
) < 0) {
682 dump_printf("%p [%p]: skipping unknown header type: %d\n",
683 (void *)(offset
+ head
),
684 (void *)(long)(event
->header
.size
),
690 * assume we lost track of the stream, check alignment, and
691 * increment a single u64 in the hope to catch on again 'soon'.
694 if (unlikely(head
& 7))
702 if (offset
+ head
< (unsigned long)input_stat
.st_size
)
708 dump_printf(" IP events: %10ld\n", total
);
709 dump_printf(" mmap events: %10ld\n", total_mmap
);
710 dump_printf(" comm events: %10ld\n", total_comm
);
711 dump_printf(" fork events: %10ld\n", total_fork
);
712 dump_printf(" unknown events: %10ld\n", total_unknown
);
718 threads__fprintf(stdout
);
721 dsos__fprintf(stdout
);
724 output__resort(total
);
731 static const char * const annotate_usage
[] = {
732 "perf annotate [<options>] <command>",
736 static const struct option options
[] = {
737 OPT_STRING('i', "input", &input_name
, "file",
739 OPT_STRING('s', "symbol", &sym_hist_filter
, "symbol",
740 "symbol to annotate"),
741 OPT_BOOLEAN('f', "force", &force
, "don't complain, do it"),
742 OPT_BOOLEAN('v', "verbose", &verbose
,
743 "be more verbose (show symbol address, etc)"),
744 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace
,
745 "dump raw trace in ASCII"),
746 OPT_STRING('k', "vmlinux", &vmlinux_name
, "file", "vmlinux pathname"),
747 OPT_BOOLEAN('m', "modules", &use_modules
,
748 "load module symbols - WARNING: use only with -k and LIVE kernel"),
749 OPT_BOOLEAN('l', "print-line", &print_line
,
750 "print matching source lines (may be slow)"),
751 OPT_BOOLEAN('P', "full-paths", &full_paths
,
752 "Don't shorten the displayed pathnames"),
756 static void setup_sorting(void)
758 char *tmp
, *tok
, *str
= strdup(sort_order
);
760 for (tok
= strtok_r(str
, ", ", &tmp
);
761 tok
; tok
= strtok_r(NULL
, ", ", &tmp
)) {
762 if (sort_dimension__add(tok
) < 0) {
763 error("Unknown --sort key: `%s'", tok
);
764 usage_with_options(annotate_usage
, options
);
771 int cmd_annotate(int argc
, const char **argv
, const char *prefix __used
)
773 symbol__init(sizeof(struct sym_priv
));
775 page_size
= getpagesize();
777 argc
= parse_options(argc
, argv
, options
, annotate_usage
, 0);
783 * Special case: if there's an argument left then assume tha
784 * it's a symbol filter:
787 usage_with_options(annotate_usage
, options
);
789 sym_hist_filter
= argv
[0];
794 if (field_sep
&& *field_sep
== '.') {
795 fputs("'.' is the only non valid --field-separator argument\n",
800 return __cmd_annotate();
This page took 0.045815 seconds and 4 git commands to generate.