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_ops__parse_target(const char *operands
, u64
*target
)
23 *target
= strtoull(operands
, NULL
, 16);
27 static struct ins_ops call_ops
= {
28 .parse_target
= call_ops__parse_target
,
31 bool ins__is_call(const struct ins
*ins
)
33 return ins
->ops
== &call_ops
;
36 static int jump_ops__parse_target(const char *operands
, u64
*target
)
38 const char *s
= strchr(operands
, '+');
43 *target
= strtoll(s
, NULL
, 16);
47 static int jump_ops__scnprintf(struct ins
*ins
, char *bf
, size_t size
,
48 const char *operands
, u64 target
)
51 return scnprintf(bf
, size
, "%-6.6s %s", ins
->name
, operands
);
53 return scnprintf(bf
, size
, "%-6.6s %" PRIx64
, ins
->name
, target
);
56 static struct ins_ops jump_ops
= {
57 .parse_target
= jump_ops__parse_target
,
58 .scnprintf
= jump_ops__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
= "je", .ops
= &jump_ops
, },
74 { .name
= "jmp", .ops
= &jump_ops
, },
75 { .name
= "jmpq", .ops
= &jump_ops
, },
76 { .name
= "jne", .ops
= &jump_ops
, },
77 { .name
= "js", .ops
= &jump_ops
, },
80 static int ins__cmp(const void *name
, const void *insp
)
82 const struct ins
*ins
= insp
;
84 return strcmp(name
, ins
->name
);
87 static struct ins
*ins__find(const char *name
)
89 const int nmemb
= ARRAY_SIZE(instructions
);
91 return bsearch(name
, instructions
, nmemb
, sizeof(struct ins
), ins__cmp
);
94 int symbol__annotate_init(struct map
*map __used
, struct symbol
*sym
)
96 struct annotation
*notes
= symbol__annotation(sym
);
97 pthread_mutex_init(¬es
->lock
, NULL
);
101 int symbol__alloc_hist(struct symbol
*sym
)
103 struct annotation
*notes
= symbol__annotation(sym
);
104 const size_t size
= symbol__size(sym
);
105 size_t sizeof_sym_hist
= (sizeof(struct sym_hist
) + size
* sizeof(u64
));
107 notes
->src
= zalloc(sizeof(*notes
->src
) + symbol_conf
.nr_events
* sizeof_sym_hist
);
108 if (notes
->src
== NULL
)
110 notes
->src
->sizeof_sym_hist
= sizeof_sym_hist
;
111 notes
->src
->nr_histograms
= symbol_conf
.nr_events
;
112 INIT_LIST_HEAD(¬es
->src
->source
);
116 void symbol__annotate_zero_histograms(struct symbol
*sym
)
118 struct annotation
*notes
= symbol__annotation(sym
);
120 pthread_mutex_lock(¬es
->lock
);
121 if (notes
->src
!= NULL
)
122 memset(notes
->src
->histograms
, 0,
123 notes
->src
->nr_histograms
* notes
->src
->sizeof_sym_hist
);
124 pthread_mutex_unlock(¬es
->lock
);
127 int symbol__inc_addr_samples(struct symbol
*sym
, struct map
*map
,
131 struct annotation
*notes
;
134 notes
= symbol__annotation(sym
);
135 if (notes
->src
== NULL
)
138 pr_debug3("%s: addr=%#" PRIx64
"\n", __func__
, map
->unmap_ip(map
, addr
));
140 if (addr
< sym
->start
|| addr
> sym
->end
)
143 offset
= addr
- sym
->start
;
144 h
= annotation__histogram(notes
, evidx
);
148 pr_debug3("%#" PRIx64
" %s: period++ [addr: %#" PRIx64
", %#" PRIx64
149 ", evidx=%d] => %" PRIu64
"\n", sym
->start
, sym
->name
,
150 addr
, addr
- sym
->start
, evidx
, h
->addr
[offset
]);
154 static void disasm_line__init_ins(struct disasm_line
*dl
)
156 dl
->ins
= ins__find(dl
->name
);
164 if (dl
->ins
->ops
->parse_target
)
165 dl
->ins
->ops
->parse_target(dl
->operands
, &dl
->target
);
168 static struct disasm_line
*disasm_line__new(s64 offset
, char *line
, size_t privsize
)
170 struct disasm_line
*dl
= zalloc(sizeof(*dl
) + privsize
);
174 dl
->line
= strdup(line
);
175 if (dl
->line
== NULL
)
179 char *name
= dl
->line
, tmp
;
181 while (isspace(name
[0]))
187 dl
->operands
= name
+ 1;
189 while (dl
->operands
[0] != '\0' &&
190 !isspace(dl
->operands
[0]))
193 tmp
= dl
->operands
[0];
194 dl
->operands
[0] = '\0';
195 dl
->name
= strdup(name
);
197 if (dl
->name
== NULL
)
200 dl
->operands
[0] = tmp
;
202 if (dl
->operands
[0] != '\0') {
204 while (isspace(dl
->operands
[0]))
208 disasm_line__init_ins(dl
);
221 void disasm_line__free(struct disasm_line
*dl
)
228 static void disasm__add(struct list_head
*head
, struct disasm_line
*line
)
230 list_add_tail(&line
->node
, head
);
233 struct disasm_line
*disasm__get_next_ip_line(struct list_head
*head
, struct disasm_line
*pos
)
235 list_for_each_entry_continue(pos
, head
, node
)
236 if (pos
->offset
>= 0)
242 static int disasm_line__print(struct disasm_line
*dl
, struct symbol
*sym
, u64 start
,
243 int evidx
, u64 len
, int min_pcnt
, int printed
,
244 int max_lines
, struct disasm_line
*queue
)
246 static const char *prev_line
;
247 static const char *prev_color
;
249 if (dl
->offset
!= -1) {
250 const char *path
= NULL
;
251 unsigned int hits
= 0;
252 double percent
= 0.0;
254 struct annotation
*notes
= symbol__annotation(sym
);
255 struct source_line
*src_line
= notes
->src
->lines
;
256 struct sym_hist
*h
= annotation__histogram(notes
, evidx
);
257 s64 offset
= dl
->offset
;
258 const u64 addr
= start
+ offset
;
259 struct disasm_line
*next
;
261 next
= disasm__get_next_ip_line(¬es
->src
->source
, dl
);
263 while (offset
< (s64
)len
&&
264 (next
== NULL
|| offset
< next
->offset
)) {
267 path
= src_line
[offset
].path
;
268 percent
+= src_line
[offset
].percent
;
270 hits
+= h
->addr
[offset
];
275 if (src_line
== NULL
&& h
->sum
)
276 percent
= 100.0 * hits
/ h
->sum
;
278 if (percent
< min_pcnt
)
281 if (max_lines
&& printed
>= max_lines
)
285 list_for_each_entry_from(queue
, ¬es
->src
->source
, node
) {
288 disasm_line__print(queue
, sym
, start
, evidx
, len
,
293 color
= get_percent_color(percent
);
296 * Also color the filename and line if needed, with
297 * the same color than the percentage. Don't print it
298 * twice for close colored addr with the same filename:line
301 if (!prev_line
|| strcmp(prev_line
, path
)
302 || color
!= prev_color
) {
303 color_fprintf(stdout
, color
, " %s", path
);
309 color_fprintf(stdout
, color
, " %7.2f", percent
);
311 color_fprintf(stdout
, PERF_COLOR_MAGENTA
, " %" PRIx64
":", addr
);
312 color_fprintf(stdout
, PERF_COLOR_BLUE
, "%s\n", dl
->line
);
313 } else if (max_lines
&& printed
>= max_lines
)
322 printf(" : %s\n", dl
->line
);
328 static int symbol__parse_objdump_line(struct symbol
*sym
, struct map
*map
,
329 FILE *file
, size_t privsize
)
331 struct annotation
*notes
= symbol__annotation(sym
);
332 struct disasm_line
*dl
;
333 char *line
= NULL
, *parsed_line
, *tmp
, *tmp2
, *c
;
335 s64 line_ip
, offset
= -1;
337 if (getline(&line
, &line_len
, file
) < 0)
343 while (line_len
!= 0 && isspace(line
[line_len
- 1]))
344 line
[--line_len
] = '\0';
346 c
= strchr(line
, '\n');
354 * Strip leading spaces:
365 * Parse hexa addresses followed by ':'
367 line_ip
= strtoull(tmp
, &tmp2
, 16);
368 if (*tmp2
!= ':' || tmp
== tmp2
|| tmp2
[1] == '\0')
373 u64 start
= map__rip_2objdump(map
, sym
->start
),
374 end
= map__rip_2objdump(map
, sym
->end
);
376 offset
= line_ip
- start
;
377 if (offset
< 0 || (u64
)line_ip
> end
)
380 parsed_line
= tmp2
+ 1;
383 dl
= disasm_line__new(offset
, parsed_line
, privsize
);
389 disasm__add(¬es
->src
->source
, dl
);
394 int symbol__annotate(struct symbol
*sym
, struct map
*map
, size_t privsize
)
396 struct dso
*dso
= map
->dso
;
397 char *filename
= dso__build_id_filename(dso
, NULL
, 0);
398 bool free_filename
= true;
399 char command
[PATH_MAX
* 2];
402 char symfs_filename
[PATH_MAX
];
405 snprintf(symfs_filename
, sizeof(symfs_filename
), "%s%s",
406 symbol_conf
.symfs
, filename
);
409 if (filename
== NULL
) {
410 if (dso
->has_build_id
) {
411 pr_err("Can't annotate %s: not enough memory\n",
416 } else if (readlink(symfs_filename
, command
, sizeof(command
)) < 0 ||
417 strstr(command
, "[kernel.kallsyms]") ||
418 access(symfs_filename
, R_OK
)) {
422 * If we don't have build-ids or the build-id file isn't in the
423 * cache, or is just a kallsyms file, well, lets hope that this
424 * DSO is the same as when 'perf record' ran.
426 filename
= dso
->long_name
;
427 snprintf(symfs_filename
, sizeof(symfs_filename
), "%s%s",
428 symbol_conf
.symfs
, filename
);
429 free_filename
= false;
432 if (dso
->symtab_type
== SYMTAB__KALLSYMS
) {
433 char bf
[BUILD_ID_SIZE
* 2 + 16] = " with build id ";
434 char *build_id_msg
= NULL
;
436 if (dso
->annotate_warned
)
437 goto out_free_filename
;
439 if (dso
->has_build_id
) {
440 build_id__sprintf(dso
->build_id
,
441 sizeof(dso
->build_id
), bf
+ 15);
445 dso
->annotate_warned
= 1;
446 pr_err("Can't annotate %s:\n\n"
447 "No vmlinux file%s\nwas found in the path.\n\n"
449 " perf buildid-cache -av vmlinux\n\n"
451 " --vmlinux vmlinux\n",
452 sym
->name
, build_id_msg
?: "");
453 goto out_free_filename
;
456 pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64
", end=%#" PRIx64
"\n", __func__
,
457 filename
, sym
->name
, map
->unmap_ip(map
, sym
->start
),
458 map
->unmap_ip(map
, sym
->end
));
460 pr_debug("annotating [%p] %30s : [%p] %30s\n",
461 dso
, dso
->long_name
, sym
, sym
->name
);
463 snprintf(command
, sizeof(command
),
464 "objdump %s%s --start-address=0x%016" PRIx64
465 " --stop-address=0x%016" PRIx64
466 " -d %s %s -C %s|grep -v %s|expand",
467 disassembler_style
? "-M " : "",
468 disassembler_style
? disassembler_style
: "",
469 map__rip_2objdump(map
, sym
->start
),
470 map__rip_2objdump(map
, sym
->end
+1),
471 symbol_conf
.annotate_asm_raw
? "" : "--no-show-raw",
472 symbol_conf
.annotate_src
? "-S" : "",
473 symfs_filename
, filename
);
475 pr_debug("Executing: %s\n", command
);
477 file
= popen(command
, "r");
479 goto out_free_filename
;
482 if (symbol__parse_objdump_line(sym
, map
, file
, privsize
) < 0)
492 static void insert_source_line(struct rb_root
*root
, struct source_line
*src_line
)
494 struct source_line
*iter
;
495 struct rb_node
**p
= &root
->rb_node
;
496 struct rb_node
*parent
= NULL
;
500 iter
= rb_entry(parent
, struct source_line
, node
);
502 if (src_line
->percent
> iter
->percent
)
508 rb_link_node(&src_line
->node
, parent
, p
);
509 rb_insert_color(&src_line
->node
, root
);
512 static void symbol__free_source_line(struct symbol
*sym
, int len
)
514 struct annotation
*notes
= symbol__annotation(sym
);
515 struct source_line
*src_line
= notes
->src
->lines
;
518 for (i
= 0; i
< len
; i
++)
519 free(src_line
[i
].path
);
522 notes
->src
->lines
= NULL
;
525 /* Get the filename:line for the colored entries */
526 static int symbol__get_source_line(struct symbol
*sym
, struct map
*map
,
527 int evidx
, struct rb_root
*root
, int len
,
528 const char *filename
)
532 char cmd
[PATH_MAX
* 2];
533 struct source_line
*src_line
;
534 struct annotation
*notes
= symbol__annotation(sym
);
535 struct sym_hist
*h
= annotation__histogram(notes
, evidx
);
540 src_line
= notes
->src
->lines
= calloc(len
, sizeof(struct source_line
));
541 if (!notes
->src
->lines
)
544 start
= map__rip_2objdump(map
, sym
->start
);
546 for (i
= 0; i
< len
; i
++) {
552 src_line
[i
].percent
= 100.0 * h
->addr
[i
] / h
->sum
;
553 if (src_line
[i
].percent
<= 0.5)
557 sprintf(cmd
, "addr2line -e %s %016" PRIx64
, filename
, offset
);
558 fp
= popen(cmd
, "r");
562 if (getline(&path
, &line_len
, fp
) < 0 || !line_len
)
565 src_line
[i
].path
= malloc(sizeof(char) * line_len
+ 1);
566 if (!src_line
[i
].path
)
569 strcpy(src_line
[i
].path
, path
);
570 insert_source_line(root
, &src_line
[i
]);
579 static void print_summary(struct rb_root
*root
, const char *filename
)
581 struct source_line
*src_line
;
582 struct rb_node
*node
;
584 printf("\nSorted summary for file %s\n", filename
);
585 printf("----------------------------------------------\n\n");
587 if (RB_EMPTY_ROOT(root
)) {
588 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN
);
592 node
= rb_first(root
);
598 src_line
= rb_entry(node
, struct source_line
, node
);
599 percent
= src_line
->percent
;
600 color
= get_percent_color(percent
);
601 path
= src_line
->path
;
603 color_fprintf(stdout
, color
, " %7.2f %s", percent
, path
);
604 node
= rb_next(node
);
608 static void symbol__annotate_hits(struct symbol
*sym
, int evidx
)
610 struct annotation
*notes
= symbol__annotation(sym
);
611 struct sym_hist
*h
= annotation__histogram(notes
, evidx
);
612 u64 len
= symbol__size(sym
), offset
;
614 for (offset
= 0; offset
< len
; ++offset
)
615 if (h
->addr
[offset
] != 0)
616 printf("%*" PRIx64
": %" PRIu64
"\n", BITS_PER_LONG
/ 2,
617 sym
->start
+ offset
, h
->addr
[offset
]);
618 printf("%*s: %" PRIu64
"\n", BITS_PER_LONG
/ 2, "h->sum", h
->sum
);
621 int symbol__annotate_printf(struct symbol
*sym
, struct map
*map
, int evidx
,
622 bool full_paths
, int min_pcnt
, int max_lines
,
625 struct dso
*dso
= map
->dso
;
626 const char *filename
= dso
->long_name
, *d_filename
;
627 struct annotation
*notes
= symbol__annotation(sym
);
628 struct disasm_line
*pos
, *queue
= NULL
;
629 u64 start
= map__rip_2objdump(map
, sym
->start
);
630 int printed
= 2, queue_len
= 0;
635 d_filename
= filename
;
637 d_filename
= basename(filename
);
639 len
= symbol__size(sym
);
641 printf(" Percent | Source code & Disassembly of %s\n", d_filename
);
642 printf("------------------------------------------------\n");
645 symbol__annotate_hits(sym
, evidx
);
647 list_for_each_entry(pos
, ¬es
->src
->source
, node
) {
648 if (context
&& queue
== NULL
) {
653 switch (disasm_line__print(pos
, sym
, start
, evidx
, len
,
654 min_pcnt
, printed
, max_lines
,
659 printed
+= queue_len
;
665 /* filtered by max_lines */
671 * Filtered by min_pcnt or non IP lines when
676 if (queue_len
== context
)
677 queue
= list_entry(queue
->node
.next
, typeof(*queue
), node
);
687 void symbol__annotate_zero_histogram(struct symbol
*sym
, int evidx
)
689 struct annotation
*notes
= symbol__annotation(sym
);
690 struct sym_hist
*h
= annotation__histogram(notes
, evidx
);
692 memset(h
, 0, notes
->src
->sizeof_sym_hist
);
695 void symbol__annotate_decay_histogram(struct symbol
*sym
, int evidx
)
697 struct annotation
*notes
= symbol__annotation(sym
);
698 struct sym_hist
*h
= annotation__histogram(notes
, evidx
);
699 int len
= symbol__size(sym
), offset
;
702 for (offset
= 0; offset
< len
; ++offset
) {
703 h
->addr
[offset
] = h
->addr
[offset
] * 7 / 8;
704 h
->sum
+= h
->addr
[offset
];
708 void disasm__purge(struct list_head
*head
)
710 struct disasm_line
*pos
, *n
;
712 list_for_each_entry_safe(pos
, n
, head
, node
) {
713 list_del(&pos
->node
);
714 disasm_line__free(pos
);
718 static size_t disasm_line__fprintf(struct disasm_line
*dl
, FILE *fp
)
722 if (dl
->offset
== -1)
723 return fprintf(fp
, "%s\n", dl
->line
);
725 printed
= fprintf(fp
, "%#" PRIx64
" %s", dl
->offset
, dl
->name
);
727 if (dl
->operands
[0] != '\0') {
728 printed
+= fprintf(fp
, "%.*s %s\n", 6 - (int)printed
, " ",
732 return printed
+ fprintf(fp
, "\n");
735 size_t disasm__fprintf(struct list_head
*head
, FILE *fp
)
737 struct disasm_line
*pos
;
740 list_for_each_entry(pos
, head
, node
)
741 printed
+= disasm_line__fprintf(pos
, fp
);
746 int symbol__tty_annotate(struct symbol
*sym
, struct map
*map
, int evidx
,
747 bool print_lines
, bool full_paths
, int min_pcnt
,
750 struct dso
*dso
= map
->dso
;
751 const char *filename
= dso
->long_name
;
752 struct rb_root source_line
= RB_ROOT
;
755 if (symbol__annotate(sym
, map
, 0) < 0)
758 len
= symbol__size(sym
);
761 symbol__get_source_line(sym
, map
, evidx
, &source_line
,
763 print_summary(&source_line
, filename
);
766 symbol__annotate_printf(sym
, map
, evidx
, full_paths
,
767 min_pcnt
, max_lines
, 0);
769 symbol__free_source_line(sym
, len
);
771 disasm__purge(&symbol__annotation(sym
)->src
->source
);