]>
git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - tools/perf/builtin-annotate.c
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;
53 struct sym_hist
*hist
;
57 static const char *sym_hist_filter
;
59 static int symbol_filter(struct map
*map __used
, struct symbol
*sym
)
61 if (sym_hist_filter
== NULL
||
62 strcmp(sym
->name
, sym_hist_filter
) == 0) {
63 struct sym_priv
*priv
= symbol__priv(sym
);
64 const int size
= (sizeof(*priv
->hist
) +
65 (sym
->end
- sym
->start
) * sizeof(u64
));
67 priv
->hist
= malloc(size
);
69 memset(priv
->hist
, 0, size
);
73 * FIXME: We should really filter it out, as we don't want to go thru symbols
74 * we're not interested, and if a DSO ends up with no symbols, delete it too,
75 * but right now the kernel loading routines in symbol.c bail out if no symbols
76 * are found, fix it later.
82 * collect histogram counts
84 static void hist_hit(struct hist_entry
*he
, u64 ip
)
86 unsigned int sym_size
, offset
;
87 struct symbol
*sym
= he
->sym
;
88 struct sym_priv
*priv
;
96 priv
= symbol__priv(sym
);
100 sym_size
= sym
->end
- sym
->start
;
101 offset
= ip
- sym
->start
;
104 fprintf(stderr
, "%s: ip=%Lx\n", __func__
,
105 he
->map
->unmap_ip(he
->map
, ip
));
107 if (offset
>= sym_size
)
115 printf("%p %s: count++ [ip: %p, %08Lx] => %Ld\n",
116 (void *)(unsigned long)he
->sym
->start
,
118 (void *)(unsigned long)ip
, ip
- he
->sym
->start
,
122 static int hist_entry__add(struct thread
*thread
, struct map
*map
,
123 struct symbol
*sym
, u64 ip
, u64 count
, char level
)
126 struct hist_entry
*he
= __hist_entry__add(thread
, map
, sym
, NULL
, ip
,
135 process_sample_event(event_t
*event
, unsigned long offset
, unsigned long head
)
138 u64 ip
= event
->ip
.ip
;
139 struct map
*map
= NULL
;
140 struct symbol
*sym
= NULL
;
141 struct thread
*thread
= threads__findnew(event
->ip
.pid
);
143 dump_printf("%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
144 (void *)(offset
+ head
),
145 (void *)(long)(event
->header
.size
),
150 if (thread
== NULL
) {
151 fprintf(stderr
, "problem processing %d event, skipping it.\n",
156 dump_printf(" ... thread: %s:%d\n", thread
->comm
, thread
->pid
);
158 if (event
->header
.misc
& PERF_RECORD_MISC_KERNEL
) {
160 sym
= kernel_maps__find_symbol(ip
, &map
);
161 dump_printf(" ...... dso: %s\n",
162 map
? map
->dso
->long_name
: "<not found>");
163 } else if (event
->header
.misc
& PERF_RECORD_MISC_USER
) {
165 map
= thread__find_map(thread
, ip
);
168 ip
= map
->map_ip(map
, ip
);
169 sym
= map__find_symbol(map
, ip
, symbol_filter
);
172 * If this is outside of all known maps,
173 * and is a negative address, try to look it
174 * up in the kernel dso, as it might be a
175 * vsyscall or vdso (which executes in user-mode).
177 * XXX This is nasty, we should have a symbol list in
178 * the "[vdso]" dso, but for now lets use the old
179 * trick of looking in the whole kernel symbol list.
181 if ((long long)ip
< 0) {
186 dump_printf(" ...... dso: %s\n",
187 map
? map
->dso
->long_name
: "<not found>");
190 dump_printf(" ...... dso: [hypervisor]\n");
193 if (hist_entry__add(thread
, map
, sym
, ip
, 1, level
)) {
194 fprintf(stderr
, "problem incrementing symbol count, "
204 process_mmap_event(event_t
*event
, unsigned long offset
, unsigned long head
)
206 struct map
*map
= map__new(&event
->mmap
, NULL
, 0);
207 struct thread
*thread
= threads__findnew(event
->mmap
.pid
);
209 dump_printf("%p [%p]: PERF_RECORD_MMAP %d: [%p(%p) @ %p]: %s\n",
210 (void *)(offset
+ head
),
211 (void *)(long)(event
->header
.size
),
213 (void *)(long)event
->mmap
.start
,
214 (void *)(long)event
->mmap
.len
,
215 (void *)(long)event
->mmap
.pgoff
,
216 event
->mmap
.filename
);
218 if (thread
== NULL
|| map
== NULL
) {
219 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
223 thread__insert_map(thread
, map
);
230 process_comm_event(event_t
*event
, unsigned long offset
, unsigned long head
)
232 struct thread
*thread
= threads__findnew(event
->comm
.pid
);
234 dump_printf("%p [%p]: PERF_RECORD_COMM: %s:%d\n",
235 (void *)(offset
+ head
),
236 (void *)(long)(event
->header
.size
),
237 event
->comm
.comm
, event
->comm
.pid
);
239 if (thread
== NULL
||
240 thread__set_comm(thread
, event
->comm
.comm
)) {
241 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
250 process_fork_event(event_t
*event
, unsigned long offset
, unsigned long head
)
252 struct thread
*thread
= threads__findnew(event
->fork
.pid
);
253 struct thread
*parent
= threads__findnew(event
->fork
.ppid
);
255 dump_printf("%p [%p]: PERF_RECORD_FORK: %d:%d\n",
256 (void *)(offset
+ head
),
257 (void *)(long)(event
->header
.size
),
258 event
->fork
.pid
, event
->fork
.ppid
);
261 * A thread clone will have the same PID for both
264 if (thread
== parent
)
267 if (!thread
|| !parent
|| thread__fork(thread
, parent
)) {
268 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
277 process_event(event_t
*event
, unsigned long offset
, unsigned long head
)
279 switch (event
->header
.type
) {
280 case PERF_RECORD_SAMPLE
:
281 return process_sample_event(event
, offset
, head
);
283 case PERF_RECORD_MMAP
:
284 return process_mmap_event(event
, offset
, head
);
286 case PERF_RECORD_COMM
:
287 return process_comm_event(event
, offset
, head
);
289 case PERF_RECORD_FORK
:
290 return process_fork_event(event
, offset
, head
);
292 * We dont process them right now but they are fine:
295 case PERF_RECORD_THROTTLE
:
296 case PERF_RECORD_UNTHROTTLE
:
306 static int parse_line(FILE *file
, struct hist_entry
*he
, u64 len
)
308 struct symbol
*sym
= he
->sym
;
309 char *line
= NULL
, *tmp
, *tmp2
;
310 static const char *prev_line
;
311 static const char *prev_color
;
319 if (getline(&line
, &line_len
, file
) < 0)
324 c
= strchr(line
, '\n');
333 * Strip leading spaces:
344 * Parse hexa addresses followed by ':'
346 line_ip
= strtoull(tmp
, &tmp2
, 16);
351 start
= he
->map
->unmap_ip(he
->map
, sym
->start
);
354 const char *path
= NULL
;
355 unsigned int hits
= 0;
356 double percent
= 0.0;
358 struct sym_priv
*priv
= symbol__priv(sym
);
359 struct sym_ext
*sym_ext
= priv
->ext
;
360 struct sym_hist
*h
= priv
->hist
;
362 offset
= line_ip
- start
;
364 hits
= h
->ip
[offset
];
366 if (offset
< len
&& sym_ext
) {
367 path
= sym_ext
[offset
].path
;
368 percent
= sym_ext
[offset
].percent
;
370 percent
= 100.0 * hits
/ h
->sum
;
372 color
= get_percent_color(percent
);
375 * Also color the filename and line if needed, with
376 * the same color than the percentage. Don't print it
377 * twice for close colored ip with the same filename:line
380 if (!prev_line
|| strcmp(prev_line
, path
)
381 || color
!= prev_color
) {
382 color_fprintf(stdout
, color
, " %s", path
);
388 color_fprintf(stdout
, color
, " %7.2f", percent
);
390 color_fprintf(stdout
, PERF_COLOR_BLUE
, "%s\n", line
);
395 printf(" : %s\n", line
);
401 static struct rb_root root_sym_ext
;
403 static void insert_source_line(struct sym_ext
*sym_ext
)
405 struct sym_ext
*iter
;
406 struct rb_node
**p
= &root_sym_ext
.rb_node
;
407 struct rb_node
*parent
= NULL
;
411 iter
= rb_entry(parent
, struct sym_ext
, node
);
413 if (sym_ext
->percent
> iter
->percent
)
419 rb_link_node(&sym_ext
->node
, parent
, p
);
420 rb_insert_color(&sym_ext
->node
, &root_sym_ext
);
423 static void free_source_line(struct hist_entry
*he
, int len
)
425 struct sym_priv
*priv
= symbol__priv(he
->sym
);
426 struct sym_ext
*sym_ext
= priv
->ext
;
432 for (i
= 0; i
< len
; i
++)
433 free(sym_ext
[i
].path
);
437 root_sym_ext
= RB_ROOT
;
440 /* Get the filename:line for the colored entries */
442 get_source_line(struct hist_entry
*he
, int len
, const char *filename
)
444 struct symbol
*sym
= he
->sym
;
447 char cmd
[PATH_MAX
* 2];
448 struct sym_ext
*sym_ext
;
449 struct sym_priv
*priv
= symbol__priv(sym
);
450 struct sym_hist
*h
= priv
->hist
;
455 sym_ext
= priv
->ext
= calloc(len
, sizeof(struct sym_ext
));
459 start
= he
->map
->unmap_ip(he
->map
, sym
->start
);
461 for (i
= 0; i
< len
; i
++) {
467 sym_ext
[i
].percent
= 100.0 * h
->ip
[i
] / h
->sum
;
468 if (sym_ext
[i
].percent
<= 0.5)
472 sprintf(cmd
, "addr2line -e %s %016llx", filename
, offset
);
473 fp
= popen(cmd
, "r");
477 if (getline(&path
, &line_len
, fp
) < 0 || !line_len
)
480 sym_ext
[i
].path
= malloc(sizeof(char) * line_len
+ 1);
481 if (!sym_ext
[i
].path
)
484 strcpy(sym_ext
[i
].path
, path
);
485 insert_source_line(&sym_ext
[i
]);
492 static void print_summary(const char *filename
)
494 struct sym_ext
*sym_ext
;
495 struct rb_node
*node
;
497 printf("\nSorted summary for file %s\n", filename
);
498 printf("----------------------------------------------\n\n");
500 if (RB_EMPTY_ROOT(&root_sym_ext
)) {
501 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN
);
505 node
= rb_first(&root_sym_ext
);
511 sym_ext
= rb_entry(node
, struct sym_ext
, node
);
512 percent
= sym_ext
->percent
;
513 color
= get_percent_color(percent
);
514 path
= sym_ext
->path
;
516 color_fprintf(stdout
, color
, " %7.2f %s", percent
, path
);
517 node
= rb_next(node
);
521 static void annotate_sym(struct hist_entry
*he
)
523 struct map
*map
= he
->map
;
524 struct dso
*dso
= map
->dso
;
525 struct symbol
*sym
= he
->sym
;
526 const char *filename
= dso
->long_name
, *d_filename
;
528 char command
[PATH_MAX
*2];
535 fprintf(stderr
, "%s: filename=%s, sym=%s, start=%Lx, end=%Lx\n",
536 __func__
, filename
, sym
->name
,
537 map
->unmap_ip(map
, sym
->start
),
538 map
->unmap_ip(map
, sym
->end
));
541 d_filename
= filename
;
543 d_filename
= basename(filename
);
545 len
= sym
->end
- sym
->start
;
548 get_source_line(he
, len
, filename
);
549 print_summary(filename
);
552 printf("\n\n------------------------------------------------\n");
553 printf(" Percent | Source code & Disassembly of %s\n", d_filename
);
554 printf("------------------------------------------------\n");
557 printf("annotating [%p] %30s : [%p] %30s\n",
558 dso
, dso
->long_name
, sym
, sym
->name
);
560 sprintf(command
, "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS %s|grep -v %s",
561 map
->unmap_ip(map
, sym
->start
), map
->unmap_ip(map
, sym
->end
),
565 printf("doing: %s\n", command
);
567 file
= popen(command
, "r");
571 while (!feof(file
)) {
572 if (parse_line(file
, he
, len
) < 0)
578 free_source_line(he
, len
);
581 static void find_annotations(void)
585 for (nd
= rb_first(&output_hists
); nd
; nd
= rb_next(nd
)) {
586 struct hist_entry
*he
= rb_entry(nd
, struct hist_entry
, rb_node
);
587 struct sym_priv
*priv
;
592 priv
= symbol__priv(he
->sym
);
593 if (priv
->hist
== NULL
)
598 * Since we have a hist_entry per IP for the same symbol, free
599 * he->sym->hist to signal we already processed this symbol.
606 static int __cmd_annotate(void)
608 int ret
, rc
= EXIT_FAILURE
;
609 unsigned long offset
= 0;
610 unsigned long head
= 0;
611 struct stat input_stat
;
616 register_idle_thread();
618 input
= open(input_name
, O_RDONLY
);
620 perror("failed to open file");
624 ret
= fstat(input
, &input_stat
);
626 perror("failed to stat file");
630 if (!force
&& input_stat
.st_uid
&& (input_stat
.st_uid
!= geteuid())) {
631 fprintf(stderr
, "file: %s not owned by current user or root\n", input_name
);
635 if (!input_stat
.st_size
) {
636 fprintf(stderr
, "zero-sized file, nothing to do!\n");
640 if (load_kernel(symbol_filter
, use_modules
) < 0) {
641 perror("failed to load kernel symbols");
646 buf
= (char *)mmap(NULL
, page_size
* mmap_window
, PROT_READ
,
647 MAP_SHARED
, input
, offset
);
648 if (buf
== MAP_FAILED
) {
649 perror("failed to mmap file");
654 event
= (event_t
*)(buf
+ head
);
656 size
= event
->header
.size
;
660 if (head
+ event
->header
.size
>= page_size
* mmap_window
) {
661 unsigned long shift
= page_size
* (head
/ page_size
);
664 munmap_ret
= munmap(buf
, page_size
* mmap_window
);
665 assert(munmap_ret
== 0);
672 size
= event
->header
.size
;
674 dump_printf("%p [%p]: event: %d\n",
675 (void *)(offset
+ head
),
676 (void *)(long)event
->header
.size
,
679 if (!size
|| process_event(event
, offset
, head
) < 0) {
681 dump_printf("%p [%p]: skipping unknown header type: %d\n",
682 (void *)(offset
+ head
),
683 (void *)(long)(event
->header
.size
),
689 * assume we lost track of the stream, check alignment, and
690 * increment a single u64 in the hope to catch on again 'soon'.
693 if (unlikely(head
& 7))
701 if (offset
+ head
< (unsigned long)input_stat
.st_size
)
707 dump_printf(" IP events: %10ld\n", total
);
708 dump_printf(" mmap events: %10ld\n", total_mmap
);
709 dump_printf(" comm events: %10ld\n", total_comm
);
710 dump_printf(" fork events: %10ld\n", total_fork
);
711 dump_printf(" unknown events: %10ld\n", total_unknown
);
717 threads__fprintf(stdout
);
720 dsos__fprintf(stdout
);
723 output__resort(total
);
730 static const char * const annotate_usage
[] = {
731 "perf annotate [<options>] <command>",
735 static const struct option options
[] = {
736 OPT_STRING('i', "input", &input_name
, "file",
738 OPT_STRING('s', "symbol", &sym_hist_filter
, "symbol",
739 "symbol to annotate"),
740 OPT_BOOLEAN('f', "force", &force
, "don't complain, do it"),
741 OPT_BOOLEAN('v', "verbose", &verbose
,
742 "be more verbose (show symbol address, etc)"),
743 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace
,
744 "dump raw trace in ASCII"),
745 OPT_STRING('k', "vmlinux", &vmlinux_name
, "file", "vmlinux pathname"),
746 OPT_BOOLEAN('m', "modules", &use_modules
,
747 "load module symbols - WARNING: use only with -k and LIVE kernel"),
748 OPT_BOOLEAN('l', "print-line", &print_line
,
749 "print matching source lines (may be slow)"),
750 OPT_BOOLEAN('P', "full-paths", &full_paths
,
751 "Don't shorten the displayed pathnames"),
755 static void setup_sorting(void)
757 char *tmp
, *tok
, *str
= strdup(sort_order
);
759 for (tok
= strtok_r(str
, ", ", &tmp
);
760 tok
; tok
= strtok_r(NULL
, ", ", &tmp
)) {
761 if (sort_dimension__add(tok
) < 0) {
762 error("Unknown --sort key: `%s'", tok
);
763 usage_with_options(annotate_usage
, options
);
770 int cmd_annotate(int argc
, const char **argv
, const char *prefix __used
)
772 symbol__init(sizeof(struct sym_priv
));
774 page_size
= getpagesize();
776 argc
= parse_options(argc
, argv
, options
, annotate_usage
, 0);
782 * Special case: if there's an argument left then assume tha
783 * it's a symbol filter:
786 usage_with_options(annotate_usage
, options
);
788 sym_hist_filter
= argv
[0];
793 if (field_sep
&& *field_sep
== '.') {
794 fputs("'.' is the only non valid --field-separator argument\n",
799 return __cmd_annotate();