]>
git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - tools/perf/util/annotate.c
2 * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
4 * Parts came from builtin-annotate.c, see those files for further
7 * Released under the GPL v2. (and only v2, not any later version)
19 const char *disassembler_style
;
21 static int call__parse(struct ins_operands
*ops
)
23 ops
->target
= strtoull(ops
->raw
, NULL
, 16);
27 static struct ins_ops call_ops
= {
31 bool ins__is_call(const struct ins
*ins
)
33 return ins
->ops
== &call_ops
;
36 static int jump__parse(struct ins_operands
*ops
)
38 const char *s
= strchr(ops
->raw
, '+');
43 ops
->target
= strtoll(s
, NULL
, 16);
47 static int jump__scnprintf(struct ins
*ins
, char *bf
, size_t size
,
48 struct ins_operands
*ops
, bool addrs
)
51 return scnprintf(bf
, size
, "%-6.6s %s", ins
->name
, ops
->raw
);
53 return scnprintf(bf
, size
, "%-6.6s %" PRIx64
, ins
->name
, ops
->target
);
56 static struct ins_ops jump_ops
= {
58 .scnprintf
= jump__scnprintf
,
61 bool ins__is_jump(const struct ins
*ins
)
63 return ins
->ops
== &jump_ops
;
67 * Must be sorted by name!
69 static struct ins instructions
[] = {
70 { .name
= "call", .ops
= &call_ops
, },
71 { .name
= "callq", .ops
= &call_ops
, },
72 { .name
= "ja", .ops
= &jump_ops
, },
73 { .name
= "jae", .ops
= &jump_ops
, },
74 { .name
= "jb", .ops
= &jump_ops
, },
75 { .name
= "jbe", .ops
= &jump_ops
, },
76 { .name
= "jc", .ops
= &jump_ops
, },
77 { .name
= "jcxz", .ops
= &jump_ops
, },
78 { .name
= "je", .ops
= &jump_ops
, },
79 { .name
= "jecxz", .ops
= &jump_ops
, },
80 { .name
= "jg", .ops
= &jump_ops
, },
81 { .name
= "jge", .ops
= &jump_ops
, },
82 { .name
= "jl", .ops
= &jump_ops
, },
83 { .name
= "jle", .ops
= &jump_ops
, },
84 { .name
= "jmp", .ops
= &jump_ops
, },
85 { .name
= "jmpq", .ops
= &jump_ops
, },
86 { .name
= "jna", .ops
= &jump_ops
, },
87 { .name
= "jnae", .ops
= &jump_ops
, },
88 { .name
= "jnb", .ops
= &jump_ops
, },
89 { .name
= "jnbe", .ops
= &jump_ops
, },
90 { .name
= "jnc", .ops
= &jump_ops
, },
91 { .name
= "jne", .ops
= &jump_ops
, },
92 { .name
= "jng", .ops
= &jump_ops
, },
93 { .name
= "jnge", .ops
= &jump_ops
, },
94 { .name
= "jnl", .ops
= &jump_ops
, },
95 { .name
= "jnle", .ops
= &jump_ops
, },
96 { .name
= "jno", .ops
= &jump_ops
, },
97 { .name
= "jnp", .ops
= &jump_ops
, },
98 { .name
= "jns", .ops
= &jump_ops
, },
99 { .name
= "jnz", .ops
= &jump_ops
, },
100 { .name
= "jo", .ops
= &jump_ops
, },
101 { .name
= "jp", .ops
= &jump_ops
, },
102 { .name
= "jpe", .ops
= &jump_ops
, },
103 { .name
= "jpo", .ops
= &jump_ops
, },
104 { .name
= "jrcxz", .ops
= &jump_ops
, },
105 { .name
= "js", .ops
= &jump_ops
, },
106 { .name
= "jz", .ops
= &jump_ops
, },
109 static int ins__cmp(const void *name
, const void *insp
)
111 const struct ins
*ins
= insp
;
113 return strcmp(name
, ins
->name
);
116 static struct ins
*ins__find(const char *name
)
118 const int nmemb
= ARRAY_SIZE(instructions
);
120 return bsearch(name
, instructions
, nmemb
, sizeof(struct ins
), ins__cmp
);
123 int symbol__annotate_init(struct map
*map __used
, struct symbol
*sym
)
125 struct annotation
*notes
= symbol__annotation(sym
);
126 pthread_mutex_init(¬es
->lock
, NULL
);
130 int symbol__alloc_hist(struct symbol
*sym
)
132 struct annotation
*notes
= symbol__annotation(sym
);
133 const size_t size
= symbol__size(sym
);
134 size_t sizeof_sym_hist
= (sizeof(struct sym_hist
) + size
* sizeof(u64
));
136 notes
->src
= zalloc(sizeof(*notes
->src
) + symbol_conf
.nr_events
* sizeof_sym_hist
);
137 if (notes
->src
== NULL
)
139 notes
->src
->sizeof_sym_hist
= sizeof_sym_hist
;
140 notes
->src
->nr_histograms
= symbol_conf
.nr_events
;
141 INIT_LIST_HEAD(¬es
->src
->source
);
145 void symbol__annotate_zero_histograms(struct symbol
*sym
)
147 struct annotation
*notes
= symbol__annotation(sym
);
149 pthread_mutex_lock(¬es
->lock
);
150 if (notes
->src
!= NULL
)
151 memset(notes
->src
->histograms
, 0,
152 notes
->src
->nr_histograms
* notes
->src
->sizeof_sym_hist
);
153 pthread_mutex_unlock(¬es
->lock
);
156 int symbol__inc_addr_samples(struct symbol
*sym
, struct map
*map
,
160 struct annotation
*notes
;
163 notes
= symbol__annotation(sym
);
164 if (notes
->src
== NULL
)
167 pr_debug3("%s: addr=%#" PRIx64
"\n", __func__
, map
->unmap_ip(map
, addr
));
169 if (addr
< sym
->start
|| addr
> sym
->end
)
172 offset
= addr
- sym
->start
;
173 h
= annotation__histogram(notes
, evidx
);
177 pr_debug3("%#" PRIx64
" %s: period++ [addr: %#" PRIx64
", %#" PRIx64
178 ", evidx=%d] => %" PRIu64
"\n", sym
->start
, sym
->name
,
179 addr
, addr
- sym
->start
, evidx
, h
->addr
[offset
]);
183 static void disasm_line__init_ins(struct disasm_line
*dl
)
185 dl
->ins
= ins__find(dl
->name
);
193 if (dl
->ins
->ops
->parse
)
194 dl
->ins
->ops
->parse(&dl
->ops
);
197 static struct disasm_line
*disasm_line__new(s64 offset
, char *line
, size_t privsize
)
199 struct disasm_line
*dl
= zalloc(sizeof(*dl
) + privsize
);
203 dl
->line
= strdup(line
);
204 if (dl
->line
== NULL
)
208 char *name
= dl
->line
, tmp
;
210 while (isspace(name
[0]))
216 dl
->ops
.raw
= name
+ 1;
218 while (dl
->ops
.raw
[0] != '\0' &&
219 !isspace(dl
->ops
.raw
[0]))
222 tmp
= dl
->ops
.raw
[0];
223 dl
->ops
.raw
[0] = '\0';
224 dl
->name
= strdup(name
);
226 if (dl
->name
== NULL
)
229 dl
->ops
.raw
[0] = tmp
;
231 if (dl
->ops
.raw
[0] != '\0') {
233 while (isspace(dl
->ops
.raw
[0]))
237 disasm_line__init_ins(dl
);
250 void disasm_line__free(struct disasm_line
*dl
)
257 static void disasm__add(struct list_head
*head
, struct disasm_line
*line
)
259 list_add_tail(&line
->node
, head
);
262 struct disasm_line
*disasm__get_next_ip_line(struct list_head
*head
, struct disasm_line
*pos
)
264 list_for_each_entry_continue(pos
, head
, node
)
265 if (pos
->offset
>= 0)
271 static int disasm_line__print(struct disasm_line
*dl
, struct symbol
*sym
, u64 start
,
272 int evidx
, u64 len
, int min_pcnt
, int printed
,
273 int max_lines
, struct disasm_line
*queue
)
275 static const char *prev_line
;
276 static const char *prev_color
;
278 if (dl
->offset
!= -1) {
279 const char *path
= NULL
;
280 unsigned int hits
= 0;
281 double percent
= 0.0;
283 struct annotation
*notes
= symbol__annotation(sym
);
284 struct source_line
*src_line
= notes
->src
->lines
;
285 struct sym_hist
*h
= annotation__histogram(notes
, evidx
);
286 s64 offset
= dl
->offset
;
287 const u64 addr
= start
+ offset
;
288 struct disasm_line
*next
;
290 next
= disasm__get_next_ip_line(¬es
->src
->source
, dl
);
292 while (offset
< (s64
)len
&&
293 (next
== NULL
|| offset
< next
->offset
)) {
296 path
= src_line
[offset
].path
;
297 percent
+= src_line
[offset
].percent
;
299 hits
+= h
->addr
[offset
];
304 if (src_line
== NULL
&& h
->sum
)
305 percent
= 100.0 * hits
/ h
->sum
;
307 if (percent
< min_pcnt
)
310 if (max_lines
&& printed
>= max_lines
)
314 list_for_each_entry_from(queue
, ¬es
->src
->source
, node
) {
317 disasm_line__print(queue
, sym
, start
, evidx
, len
,
322 color
= get_percent_color(percent
);
325 * Also color the filename and line if needed, with
326 * the same color than the percentage. Don't print it
327 * twice for close colored addr with the same filename:line
330 if (!prev_line
|| strcmp(prev_line
, path
)
331 || color
!= prev_color
) {
332 color_fprintf(stdout
, color
, " %s", path
);
338 color_fprintf(stdout
, color
, " %7.2f", percent
);
340 color_fprintf(stdout
, PERF_COLOR_MAGENTA
, " %" PRIx64
":", addr
);
341 color_fprintf(stdout
, PERF_COLOR_BLUE
, "%s\n", dl
->line
);
342 } else if (max_lines
&& printed
>= max_lines
)
351 printf(" : %s\n", dl
->line
);
357 static int symbol__parse_objdump_line(struct symbol
*sym
, struct map
*map
,
358 FILE *file
, size_t privsize
)
360 struct annotation
*notes
= symbol__annotation(sym
);
361 struct disasm_line
*dl
;
362 char *line
= NULL
, *parsed_line
, *tmp
, *tmp2
, *c
;
364 s64 line_ip
, offset
= -1;
366 if (getline(&line
, &line_len
, file
) < 0)
372 while (line_len
!= 0 && isspace(line
[line_len
- 1]))
373 line
[--line_len
] = '\0';
375 c
= strchr(line
, '\n');
383 * Strip leading spaces:
394 * Parse hexa addresses followed by ':'
396 line_ip
= strtoull(tmp
, &tmp2
, 16);
397 if (*tmp2
!= ':' || tmp
== tmp2
|| tmp2
[1] == '\0')
402 u64 start
= map__rip_2objdump(map
, sym
->start
),
403 end
= map__rip_2objdump(map
, sym
->end
);
405 offset
= line_ip
- start
;
406 if (offset
< 0 || (u64
)line_ip
> end
)
409 parsed_line
= tmp2
+ 1;
412 dl
= disasm_line__new(offset
, parsed_line
, privsize
);
418 disasm__add(¬es
->src
->source
, dl
);
423 int symbol__annotate(struct symbol
*sym
, struct map
*map
, size_t privsize
)
425 struct dso
*dso
= map
->dso
;
426 char *filename
= dso__build_id_filename(dso
, NULL
, 0);
427 bool free_filename
= true;
428 char command
[PATH_MAX
* 2];
431 char symfs_filename
[PATH_MAX
];
434 snprintf(symfs_filename
, sizeof(symfs_filename
), "%s%s",
435 symbol_conf
.symfs
, filename
);
438 if (filename
== NULL
) {
439 if (dso
->has_build_id
) {
440 pr_err("Can't annotate %s: not enough memory\n",
445 } else if (readlink(symfs_filename
, command
, sizeof(command
)) < 0 ||
446 strstr(command
, "[kernel.kallsyms]") ||
447 access(symfs_filename
, R_OK
)) {
451 * If we don't have build-ids or the build-id file isn't in the
452 * cache, or is just a kallsyms file, well, lets hope that this
453 * DSO is the same as when 'perf record' ran.
455 filename
= dso
->long_name
;
456 snprintf(symfs_filename
, sizeof(symfs_filename
), "%s%s",
457 symbol_conf
.symfs
, filename
);
458 free_filename
= false;
461 if (dso
->symtab_type
== SYMTAB__KALLSYMS
) {
462 char bf
[BUILD_ID_SIZE
* 2 + 16] = " with build id ";
463 char *build_id_msg
= NULL
;
465 if (dso
->annotate_warned
)
466 goto out_free_filename
;
468 if (dso
->has_build_id
) {
469 build_id__sprintf(dso
->build_id
,
470 sizeof(dso
->build_id
), bf
+ 15);
474 dso
->annotate_warned
= 1;
475 pr_err("Can't annotate %s:\n\n"
476 "No vmlinux file%s\nwas found in the path.\n\n"
478 " perf buildid-cache -av vmlinux\n\n"
480 " --vmlinux vmlinux\n",
481 sym
->name
, build_id_msg
?: "");
482 goto out_free_filename
;
485 pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64
", end=%#" PRIx64
"\n", __func__
,
486 filename
, sym
->name
, map
->unmap_ip(map
, sym
->start
),
487 map
->unmap_ip(map
, sym
->end
));
489 pr_debug("annotating [%p] %30s : [%p] %30s\n",
490 dso
, dso
->long_name
, sym
, sym
->name
);
492 snprintf(command
, sizeof(command
),
493 "objdump %s%s --start-address=0x%016" PRIx64
494 " --stop-address=0x%016" PRIx64
495 " -d %s %s -C %s|grep -v %s|expand",
496 disassembler_style
? "-M " : "",
497 disassembler_style
? disassembler_style
: "",
498 map__rip_2objdump(map
, sym
->start
),
499 map__rip_2objdump(map
, sym
->end
+1),
500 symbol_conf
.annotate_asm_raw
? "" : "--no-show-raw",
501 symbol_conf
.annotate_src
? "-S" : "",
502 symfs_filename
, filename
);
504 pr_debug("Executing: %s\n", command
);
506 file
= popen(command
, "r");
508 goto out_free_filename
;
511 if (symbol__parse_objdump_line(sym
, map
, file
, privsize
) < 0)
521 static void insert_source_line(struct rb_root
*root
, struct source_line
*src_line
)
523 struct source_line
*iter
;
524 struct rb_node
**p
= &root
->rb_node
;
525 struct rb_node
*parent
= NULL
;
529 iter
= rb_entry(parent
, struct source_line
, node
);
531 if (src_line
->percent
> iter
->percent
)
537 rb_link_node(&src_line
->node
, parent
, p
);
538 rb_insert_color(&src_line
->node
, root
);
541 static void symbol__free_source_line(struct symbol
*sym
, int len
)
543 struct annotation
*notes
= symbol__annotation(sym
);
544 struct source_line
*src_line
= notes
->src
->lines
;
547 for (i
= 0; i
< len
; i
++)
548 free(src_line
[i
].path
);
551 notes
->src
->lines
= NULL
;
554 /* Get the filename:line for the colored entries */
555 static int symbol__get_source_line(struct symbol
*sym
, struct map
*map
,
556 int evidx
, struct rb_root
*root
, int len
,
557 const char *filename
)
561 char cmd
[PATH_MAX
* 2];
562 struct source_line
*src_line
;
563 struct annotation
*notes
= symbol__annotation(sym
);
564 struct sym_hist
*h
= annotation__histogram(notes
, evidx
);
569 src_line
= notes
->src
->lines
= calloc(len
, sizeof(struct source_line
));
570 if (!notes
->src
->lines
)
573 start
= map__rip_2objdump(map
, sym
->start
);
575 for (i
= 0; i
< len
; i
++) {
581 src_line
[i
].percent
= 100.0 * h
->addr
[i
] / h
->sum
;
582 if (src_line
[i
].percent
<= 0.5)
586 sprintf(cmd
, "addr2line -e %s %016" PRIx64
, filename
, offset
);
587 fp
= popen(cmd
, "r");
591 if (getline(&path
, &line_len
, fp
) < 0 || !line_len
)
594 src_line
[i
].path
= malloc(sizeof(char) * line_len
+ 1);
595 if (!src_line
[i
].path
)
598 strcpy(src_line
[i
].path
, path
);
599 insert_source_line(root
, &src_line
[i
]);
608 static void print_summary(struct rb_root
*root
, const char *filename
)
610 struct source_line
*src_line
;
611 struct rb_node
*node
;
613 printf("\nSorted summary for file %s\n", filename
);
614 printf("----------------------------------------------\n\n");
616 if (RB_EMPTY_ROOT(root
)) {
617 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN
);
621 node
= rb_first(root
);
627 src_line
= rb_entry(node
, struct source_line
, node
);
628 percent
= src_line
->percent
;
629 color
= get_percent_color(percent
);
630 path
= src_line
->path
;
632 color_fprintf(stdout
, color
, " %7.2f %s", percent
, path
);
633 node
= rb_next(node
);
637 static void symbol__annotate_hits(struct symbol
*sym
, int evidx
)
639 struct annotation
*notes
= symbol__annotation(sym
);
640 struct sym_hist
*h
= annotation__histogram(notes
, evidx
);
641 u64 len
= symbol__size(sym
), offset
;
643 for (offset
= 0; offset
< len
; ++offset
)
644 if (h
->addr
[offset
] != 0)
645 printf("%*" PRIx64
": %" PRIu64
"\n", BITS_PER_LONG
/ 2,
646 sym
->start
+ offset
, h
->addr
[offset
]);
647 printf("%*s: %" PRIu64
"\n", BITS_PER_LONG
/ 2, "h->sum", h
->sum
);
650 int symbol__annotate_printf(struct symbol
*sym
, struct map
*map
, int evidx
,
651 bool full_paths
, int min_pcnt
, int max_lines
,
654 struct dso
*dso
= map
->dso
;
655 const char *filename
= dso
->long_name
, *d_filename
;
656 struct annotation
*notes
= symbol__annotation(sym
);
657 struct disasm_line
*pos
, *queue
= NULL
;
658 u64 start
= map__rip_2objdump(map
, sym
->start
);
659 int printed
= 2, queue_len
= 0;
664 d_filename
= filename
;
666 d_filename
= basename(filename
);
668 len
= symbol__size(sym
);
670 printf(" Percent | Source code & Disassembly of %s\n", d_filename
);
671 printf("------------------------------------------------\n");
674 symbol__annotate_hits(sym
, evidx
);
676 list_for_each_entry(pos
, ¬es
->src
->source
, node
) {
677 if (context
&& queue
== NULL
) {
682 switch (disasm_line__print(pos
, sym
, start
, evidx
, len
,
683 min_pcnt
, printed
, max_lines
,
688 printed
+= queue_len
;
694 /* filtered by max_lines */
700 * Filtered by min_pcnt or non IP lines when
705 if (queue_len
== context
)
706 queue
= list_entry(queue
->node
.next
, typeof(*queue
), node
);
716 void symbol__annotate_zero_histogram(struct symbol
*sym
, int evidx
)
718 struct annotation
*notes
= symbol__annotation(sym
);
719 struct sym_hist
*h
= annotation__histogram(notes
, evidx
);
721 memset(h
, 0, notes
->src
->sizeof_sym_hist
);
724 void symbol__annotate_decay_histogram(struct symbol
*sym
, int evidx
)
726 struct annotation
*notes
= symbol__annotation(sym
);
727 struct sym_hist
*h
= annotation__histogram(notes
, evidx
);
728 int len
= symbol__size(sym
), offset
;
731 for (offset
= 0; offset
< len
; ++offset
) {
732 h
->addr
[offset
] = h
->addr
[offset
] * 7 / 8;
733 h
->sum
+= h
->addr
[offset
];
737 void disasm__purge(struct list_head
*head
)
739 struct disasm_line
*pos
, *n
;
741 list_for_each_entry_safe(pos
, n
, head
, node
) {
742 list_del(&pos
->node
);
743 disasm_line__free(pos
);
747 static size_t disasm_line__fprintf(struct disasm_line
*dl
, FILE *fp
)
751 if (dl
->offset
== -1)
752 return fprintf(fp
, "%s\n", dl
->line
);
754 printed
= fprintf(fp
, "%#" PRIx64
" %s", dl
->offset
, dl
->name
);
756 if (dl
->ops
.raw
[0] != '\0') {
757 printed
+= fprintf(fp
, "%.*s %s\n", 6 - (int)printed
, " ",
761 return printed
+ fprintf(fp
, "\n");
764 size_t disasm__fprintf(struct list_head
*head
, FILE *fp
)
766 struct disasm_line
*pos
;
769 list_for_each_entry(pos
, head
, node
)
770 printed
+= disasm_line__fprintf(pos
, fp
);
775 int symbol__tty_annotate(struct symbol
*sym
, struct map
*map
, int evidx
,
776 bool print_lines
, bool full_paths
, int min_pcnt
,
779 struct dso
*dso
= map
->dso
;
780 const char *filename
= dso
->long_name
;
781 struct rb_root source_line
= RB_ROOT
;
784 if (symbol__annotate(sym
, map
, 0) < 0)
787 len
= symbol__size(sym
);
790 symbol__get_source_line(sym
, map
, evidx
, &source_line
,
792 print_summary(&source_line
, filename
);
795 symbol__annotate_printf(sym
, map
, evidx
, full_paths
,
796 min_pcnt
, max_lines
, 0);
798 symbol__free_source_line(sym
, len
);
800 disasm__purge(&symbol__annotation(sym
)->src
->source
);