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 #include <linux/bitops.h>
21 const char *disassembler_style
;
22 const char *objdump_path
;
24 static struct ins
*ins__find(const char *name
);
25 static int disasm_line__parse(char *line
, char **namep
, char **rawp
);
27 static void ins__delete(struct ins_operands
*ops
)
29 zfree(&ops
->source
.raw
);
30 zfree(&ops
->source
.name
);
31 zfree(&ops
->target
.raw
);
32 zfree(&ops
->target
.name
);
35 static int ins__raw_scnprintf(struct ins
*ins
, char *bf
, size_t size
,
36 struct ins_operands
*ops
)
38 return scnprintf(bf
, size
, "%-6.6s %s", ins
->name
, ops
->raw
);
41 int ins__scnprintf(struct ins
*ins
, char *bf
, size_t size
,
42 struct ins_operands
*ops
)
44 if (ins
->ops
->scnprintf
)
45 return ins
->ops
->scnprintf(ins
, bf
, size
, ops
);
47 return ins__raw_scnprintf(ins
, bf
, size
, ops
);
50 static int call__parse(struct ins_operands
*ops
)
52 char *endptr
, *tok
, *name
;
54 ops
->target
.addr
= strtoull(ops
->raw
, &endptr
, 16);
56 name
= strchr(endptr
, '<');
62 tok
= strchr(name
, '>');
67 ops
->target
.name
= strdup(name
);
70 return ops
->target
.name
== NULL
? -1 : 0;
73 tok
= strchr(endptr
, '(');
79 tok
= strchr(endptr
, '*');
83 ops
->target
.addr
= strtoull(tok
+ 1, NULL
, 16);
87 static int call__scnprintf(struct ins
*ins
, char *bf
, size_t size
,
88 struct ins_operands
*ops
)
91 return scnprintf(bf
, size
, "%-6.6s %s", ins
->name
, ops
->target
.name
);
93 if (ops
->target
.addr
== 0)
94 return ins__raw_scnprintf(ins
, bf
, size
, ops
);
96 return scnprintf(bf
, size
, "%-6.6s *%" PRIx64
, ins
->name
, ops
->target
.addr
);
99 static struct ins_ops call_ops
= {
100 .parse
= call__parse
,
101 .scnprintf
= call__scnprintf
,
104 bool ins__is_call(const struct ins
*ins
)
106 return ins
->ops
== &call_ops
;
109 static int jump__parse(struct ins_operands
*ops
)
111 const char *s
= strchr(ops
->raw
, '+');
113 ops
->target
.addr
= strtoull(ops
->raw
, NULL
, 16);
116 ops
->target
.offset
= strtoull(s
, NULL
, 16);
118 ops
->target
.offset
= UINT64_MAX
;
123 static int jump__scnprintf(struct ins
*ins
, char *bf
, size_t size
,
124 struct ins_operands
*ops
)
126 return scnprintf(bf
, size
, "%-6.6s %" PRIx64
, ins
->name
, ops
->target
.offset
);
129 static struct ins_ops jump_ops
= {
130 .parse
= jump__parse
,
131 .scnprintf
= jump__scnprintf
,
134 bool ins__is_jump(const struct ins
*ins
)
136 return ins
->ops
== &jump_ops
;
139 static int comment__symbol(char *raw
, char *comment
, u64
*addrp
, char **namep
)
141 char *endptr
, *name
, *t
;
143 if (strstr(raw
, "(%rip)") == NULL
)
146 *addrp
= strtoull(comment
, &endptr
, 16);
147 name
= strchr(endptr
, '<');
153 t
= strchr(name
, '>');
158 *namep
= strdup(name
);
164 static int lock__parse(struct ins_operands
*ops
)
168 ops
->locked
.ops
= zalloc(sizeof(*ops
->locked
.ops
));
169 if (ops
->locked
.ops
== NULL
)
172 if (disasm_line__parse(ops
->raw
, &name
, &ops
->locked
.ops
->raw
) < 0)
175 ops
->locked
.ins
= ins__find(name
);
176 if (ops
->locked
.ins
== NULL
)
179 if (!ops
->locked
.ins
->ops
)
182 if (ops
->locked
.ins
->ops
->parse
)
183 ops
->locked
.ins
->ops
->parse(ops
->locked
.ops
);
188 zfree(&ops
->locked
.ops
);
192 static int lock__scnprintf(struct ins
*ins
, char *bf
, size_t size
,
193 struct ins_operands
*ops
)
197 if (ops
->locked
.ins
== NULL
)
198 return ins__raw_scnprintf(ins
, bf
, size
, ops
);
200 printed
= scnprintf(bf
, size
, "%-6.6s ", ins
->name
);
201 return printed
+ ins__scnprintf(ops
->locked
.ins
, bf
+ printed
,
202 size
- printed
, ops
->locked
.ops
);
205 static void lock__delete(struct ins_operands
*ops
)
207 zfree(&ops
->locked
.ops
);
208 zfree(&ops
->target
.raw
);
209 zfree(&ops
->target
.name
);
212 static struct ins_ops lock_ops
= {
213 .free
= lock__delete
,
214 .parse
= lock__parse
,
215 .scnprintf
= lock__scnprintf
,
218 static int mov__parse(struct ins_operands
*ops
)
220 char *s
= strchr(ops
->raw
, ','), *target
, *comment
, prev
;
226 ops
->source
.raw
= strdup(ops
->raw
);
229 if (ops
->source
.raw
== NULL
)
234 while (s
[0] != '\0' && !isspace(s
[0]))
239 ops
->target
.raw
= strdup(target
);
242 if (ops
->target
.raw
== NULL
)
243 goto out_free_source
;
245 comment
= strchr(s
, '#');
249 while (comment
[0] != '\0' && isspace(comment
[0]))
252 comment__symbol(ops
->source
.raw
, comment
, &ops
->source
.addr
, &ops
->source
.name
);
253 comment__symbol(ops
->target
.raw
, comment
, &ops
->target
.addr
, &ops
->target
.name
);
258 zfree(&ops
->source
.raw
);
262 static int mov__scnprintf(struct ins
*ins
, char *bf
, size_t size
,
263 struct ins_operands
*ops
)
265 return scnprintf(bf
, size
, "%-6.6s %s,%s", ins
->name
,
266 ops
->source
.name
?: ops
->source
.raw
,
267 ops
->target
.name
?: ops
->target
.raw
);
270 static struct ins_ops mov_ops
= {
272 .scnprintf
= mov__scnprintf
,
275 static int dec__parse(struct ins_operands
*ops
)
277 char *target
, *comment
, *s
, prev
;
279 target
= s
= ops
->raw
;
281 while (s
[0] != '\0' && !isspace(s
[0]))
286 ops
->target
.raw
= strdup(target
);
289 if (ops
->target
.raw
== NULL
)
292 comment
= strchr(s
, '#');
296 while (comment
[0] != '\0' && isspace(comment
[0]))
299 comment__symbol(ops
->target
.raw
, comment
, &ops
->target
.addr
, &ops
->target
.name
);
304 static int dec__scnprintf(struct ins
*ins
, char *bf
, size_t size
,
305 struct ins_operands
*ops
)
307 return scnprintf(bf
, size
, "%-6.6s %s", ins
->name
,
308 ops
->target
.name
?: ops
->target
.raw
);
311 static struct ins_ops dec_ops
= {
313 .scnprintf
= dec__scnprintf
,
316 static int nop__scnprintf(struct ins
*ins __maybe_unused
, char *bf
, size_t size
,
317 struct ins_operands
*ops __maybe_unused
)
319 return scnprintf(bf
, size
, "%-6.6s", "nop");
322 static struct ins_ops nop_ops
= {
323 .scnprintf
= nop__scnprintf
,
327 * Must be sorted by name!
329 static struct ins instructions
[] = {
330 { .name
= "add", .ops
= &mov_ops
, },
331 { .name
= "addl", .ops
= &mov_ops
, },
332 { .name
= "addq", .ops
= &mov_ops
, },
333 { .name
= "addw", .ops
= &mov_ops
, },
334 { .name
= "and", .ops
= &mov_ops
, },
335 { .name
= "bts", .ops
= &mov_ops
, },
336 { .name
= "call", .ops
= &call_ops
, },
337 { .name
= "callq", .ops
= &call_ops
, },
338 { .name
= "cmp", .ops
= &mov_ops
, },
339 { .name
= "cmpb", .ops
= &mov_ops
, },
340 { .name
= "cmpl", .ops
= &mov_ops
, },
341 { .name
= "cmpq", .ops
= &mov_ops
, },
342 { .name
= "cmpw", .ops
= &mov_ops
, },
343 { .name
= "cmpxch", .ops
= &mov_ops
, },
344 { .name
= "dec", .ops
= &dec_ops
, },
345 { .name
= "decl", .ops
= &dec_ops
, },
346 { .name
= "imul", .ops
= &mov_ops
, },
347 { .name
= "inc", .ops
= &dec_ops
, },
348 { .name
= "incl", .ops
= &dec_ops
, },
349 { .name
= "ja", .ops
= &jump_ops
, },
350 { .name
= "jae", .ops
= &jump_ops
, },
351 { .name
= "jb", .ops
= &jump_ops
, },
352 { .name
= "jbe", .ops
= &jump_ops
, },
353 { .name
= "jc", .ops
= &jump_ops
, },
354 { .name
= "jcxz", .ops
= &jump_ops
, },
355 { .name
= "je", .ops
= &jump_ops
, },
356 { .name
= "jecxz", .ops
= &jump_ops
, },
357 { .name
= "jg", .ops
= &jump_ops
, },
358 { .name
= "jge", .ops
= &jump_ops
, },
359 { .name
= "jl", .ops
= &jump_ops
, },
360 { .name
= "jle", .ops
= &jump_ops
, },
361 { .name
= "jmp", .ops
= &jump_ops
, },
362 { .name
= "jmpq", .ops
= &jump_ops
, },
363 { .name
= "jna", .ops
= &jump_ops
, },
364 { .name
= "jnae", .ops
= &jump_ops
, },
365 { .name
= "jnb", .ops
= &jump_ops
, },
366 { .name
= "jnbe", .ops
= &jump_ops
, },
367 { .name
= "jnc", .ops
= &jump_ops
, },
368 { .name
= "jne", .ops
= &jump_ops
, },
369 { .name
= "jng", .ops
= &jump_ops
, },
370 { .name
= "jnge", .ops
= &jump_ops
, },
371 { .name
= "jnl", .ops
= &jump_ops
, },
372 { .name
= "jnle", .ops
= &jump_ops
, },
373 { .name
= "jno", .ops
= &jump_ops
, },
374 { .name
= "jnp", .ops
= &jump_ops
, },
375 { .name
= "jns", .ops
= &jump_ops
, },
376 { .name
= "jnz", .ops
= &jump_ops
, },
377 { .name
= "jo", .ops
= &jump_ops
, },
378 { .name
= "jp", .ops
= &jump_ops
, },
379 { .name
= "jpe", .ops
= &jump_ops
, },
380 { .name
= "jpo", .ops
= &jump_ops
, },
381 { .name
= "jrcxz", .ops
= &jump_ops
, },
382 { .name
= "js", .ops
= &jump_ops
, },
383 { .name
= "jz", .ops
= &jump_ops
, },
384 { .name
= "lea", .ops
= &mov_ops
, },
385 { .name
= "lock", .ops
= &lock_ops
, },
386 { .name
= "mov", .ops
= &mov_ops
, },
387 { .name
= "movb", .ops
= &mov_ops
, },
388 { .name
= "movdqa",.ops
= &mov_ops
, },
389 { .name
= "movl", .ops
= &mov_ops
, },
390 { .name
= "movq", .ops
= &mov_ops
, },
391 { .name
= "movslq", .ops
= &mov_ops
, },
392 { .name
= "movzbl", .ops
= &mov_ops
, },
393 { .name
= "movzwl", .ops
= &mov_ops
, },
394 { .name
= "nop", .ops
= &nop_ops
, },
395 { .name
= "nopl", .ops
= &nop_ops
, },
396 { .name
= "nopw", .ops
= &nop_ops
, },
397 { .name
= "or", .ops
= &mov_ops
, },
398 { .name
= "orl", .ops
= &mov_ops
, },
399 { .name
= "test", .ops
= &mov_ops
, },
400 { .name
= "testb", .ops
= &mov_ops
, },
401 { .name
= "testl", .ops
= &mov_ops
, },
402 { .name
= "xadd", .ops
= &mov_ops
, },
403 { .name
= "xbeginl", .ops
= &jump_ops
, },
404 { .name
= "xbeginq", .ops
= &jump_ops
, },
407 static int ins__cmp(const void *name
, const void *insp
)
409 const struct ins
*ins
= insp
;
411 return strcmp(name
, ins
->name
);
414 static struct ins
*ins__find(const char *name
)
416 const int nmemb
= ARRAY_SIZE(instructions
);
418 return bsearch(name
, instructions
, nmemb
, sizeof(struct ins
), ins__cmp
);
421 int symbol__annotate_init(struct map
*map __maybe_unused
, struct symbol
*sym
)
423 struct annotation
*notes
= symbol__annotation(sym
);
424 pthread_mutex_init(¬es
->lock
, NULL
);
428 int symbol__alloc_hist(struct symbol
*sym
)
430 struct annotation
*notes
= symbol__annotation(sym
);
431 const size_t size
= symbol__size(sym
);
432 size_t sizeof_sym_hist
;
434 /* Check for overflow when calculating sizeof_sym_hist */
435 if (size
> (SIZE_MAX
- sizeof(struct sym_hist
)) / sizeof(u64
))
438 sizeof_sym_hist
= (sizeof(struct sym_hist
) + size
* sizeof(u64
));
440 /* Check for overflow in zalloc argument */
441 if (sizeof_sym_hist
> (SIZE_MAX
- sizeof(*notes
->src
))
442 / symbol_conf
.nr_events
)
445 notes
->src
= zalloc(sizeof(*notes
->src
) + symbol_conf
.nr_events
* sizeof_sym_hist
);
446 if (notes
->src
== NULL
)
448 notes
->src
->sizeof_sym_hist
= sizeof_sym_hist
;
449 notes
->src
->nr_histograms
= symbol_conf
.nr_events
;
450 INIT_LIST_HEAD(¬es
->src
->source
);
454 void symbol__annotate_zero_histograms(struct symbol
*sym
)
456 struct annotation
*notes
= symbol__annotation(sym
);
458 pthread_mutex_lock(¬es
->lock
);
459 if (notes
->src
!= NULL
)
460 memset(notes
->src
->histograms
, 0,
461 notes
->src
->nr_histograms
* notes
->src
->sizeof_sym_hist
);
462 pthread_mutex_unlock(¬es
->lock
);
465 static int __symbol__inc_addr_samples(struct symbol
*sym
, struct map
*map
,
466 struct annotation
*notes
, int evidx
, u64 addr
)
471 pr_debug3("%s: addr=%#" PRIx64
"\n", __func__
, map
->unmap_ip(map
, addr
));
473 if (addr
< sym
->start
|| addr
> sym
->end
)
476 offset
= addr
- sym
->start
;
477 h
= annotation__histogram(notes
, evidx
);
481 pr_debug3("%#" PRIx64
" %s: period++ [addr: %#" PRIx64
", %#" PRIx64
482 ", evidx=%d] => %" PRIu64
"\n", sym
->start
, sym
->name
,
483 addr
, addr
- sym
->start
, evidx
, h
->addr
[offset
]);
487 static int symbol__inc_addr_samples(struct symbol
*sym
, struct map
*map
,
490 struct annotation
*notes
;
492 if (sym
== NULL
|| use_browser
!= 1 || !sort__has_sym
)
495 notes
= symbol__annotation(sym
);
496 if (notes
->src
== NULL
) {
497 if (symbol__alloc_hist(sym
) < 0)
501 return __symbol__inc_addr_samples(sym
, map
, notes
, evidx
, addr
);
504 int addr_map_symbol__inc_samples(struct addr_map_symbol
*ams
, int evidx
)
506 return symbol__inc_addr_samples(ams
->sym
, ams
->map
, evidx
, ams
->al_addr
);
509 int hist_entry__inc_addr_samples(struct hist_entry
*he
, int evidx
, u64 ip
)
511 return symbol__inc_addr_samples(he
->ms
.sym
, he
->ms
.map
, evidx
, ip
);
514 static void disasm_line__init_ins(struct disasm_line
*dl
)
516 dl
->ins
= ins__find(dl
->name
);
524 if (dl
->ins
->ops
->parse
)
525 dl
->ins
->ops
->parse(&dl
->ops
);
528 static int disasm_line__parse(char *line
, char **namep
, char **rawp
)
530 char *name
= line
, tmp
;
532 while (isspace(name
[0]))
540 while ((*rawp
)[0] != '\0' && !isspace((*rawp
)[0]))
545 *namep
= strdup(name
);
552 if ((*rawp
)[0] != '\0') {
554 while (isspace((*rawp
)[0]))
565 static struct disasm_line
*disasm_line__new(s64 offset
, char *line
, size_t privsize
)
567 struct disasm_line
*dl
= zalloc(sizeof(*dl
) + privsize
);
571 dl
->line
= strdup(line
);
572 if (dl
->line
== NULL
)
576 if (disasm_line__parse(dl
->line
, &dl
->name
, &dl
->ops
.raw
) < 0)
579 disasm_line__init_ins(dl
);
592 void disasm_line__free(struct disasm_line
*dl
)
596 if (dl
->ins
&& dl
->ins
->ops
->free
)
597 dl
->ins
->ops
->free(&dl
->ops
);
599 ins__delete(&dl
->ops
);
603 int disasm_line__scnprintf(struct disasm_line
*dl
, char *bf
, size_t size
, bool raw
)
606 return scnprintf(bf
, size
, "%-6.6s %s", dl
->name
, dl
->ops
.raw
);
608 return ins__scnprintf(dl
->ins
, bf
, size
, &dl
->ops
);
611 static void disasm__add(struct list_head
*head
, struct disasm_line
*line
)
613 list_add_tail(&line
->node
, head
);
616 struct disasm_line
*disasm__get_next_ip_line(struct list_head
*head
, struct disasm_line
*pos
)
618 list_for_each_entry_continue(pos
, head
, node
)
619 if (pos
->offset
>= 0)
625 double disasm__calc_percent(struct annotation
*notes
, int evidx
, s64 offset
,
626 s64 end
, const char **path
)
628 struct source_line
*src_line
= notes
->src
->lines
;
629 double percent
= 0.0;
632 size_t sizeof_src_line
= sizeof(*src_line
) +
633 sizeof(src_line
->p
) * (src_line
->nr_pcnt
- 1);
635 while (offset
< end
) {
636 src_line
= (void *)notes
->src
->lines
+
637 (sizeof_src_line
* offset
);
640 *path
= src_line
->path
;
642 percent
+= src_line
->p
[evidx
].percent
;
646 struct sym_hist
*h
= annotation__histogram(notes
, evidx
);
647 unsigned int hits
= 0;
650 hits
+= h
->addr
[offset
++];
653 percent
= 100.0 * hits
/ h
->sum
;
659 static int disasm_line__print(struct disasm_line
*dl
, struct symbol
*sym
, u64 start
,
660 struct perf_evsel
*evsel
, u64 len
, int min_pcnt
, int printed
,
661 int max_lines
, struct disasm_line
*queue
)
663 static const char *prev_line
;
664 static const char *prev_color
;
666 if (dl
->offset
!= -1) {
667 const char *path
= NULL
;
668 double percent
, max_percent
= 0.0;
669 double *ppercents
= &percent
;
670 int i
, nr_percent
= 1;
672 struct annotation
*notes
= symbol__annotation(sym
);
673 s64 offset
= dl
->offset
;
674 const u64 addr
= start
+ offset
;
675 struct disasm_line
*next
;
677 next
= disasm__get_next_ip_line(¬es
->src
->source
, dl
);
679 if (perf_evsel__is_group_event(evsel
)) {
680 nr_percent
= evsel
->nr_members
;
681 ppercents
= calloc(nr_percent
, sizeof(double));
682 if (ppercents
== NULL
)
686 for (i
= 0; i
< nr_percent
; i
++) {
687 percent
= disasm__calc_percent(notes
,
688 notes
->src
->lines
? i
: evsel
->idx
+ i
,
690 next
? next
->offset
: (s64
) len
,
693 ppercents
[i
] = percent
;
694 if (percent
> max_percent
)
695 max_percent
= percent
;
698 if (max_percent
< min_pcnt
)
701 if (max_lines
&& printed
>= max_lines
)
705 list_for_each_entry_from(queue
, ¬es
->src
->source
, node
) {
708 disasm_line__print(queue
, sym
, start
, evsel
, len
,
713 color
= get_percent_color(max_percent
);
716 * Also color the filename and line if needed, with
717 * the same color than the percentage. Don't print it
718 * twice for close colored addr with the same filename:line
721 if (!prev_line
|| strcmp(prev_line
, path
)
722 || color
!= prev_color
) {
723 color_fprintf(stdout
, color
, " %s", path
);
729 for (i
= 0; i
< nr_percent
; i
++) {
730 percent
= ppercents
[i
];
731 color
= get_percent_color(percent
);
732 color_fprintf(stdout
, color
, " %7.2f", percent
);
736 color_fprintf(stdout
, PERF_COLOR_MAGENTA
, " %" PRIx64
":", addr
);
737 color_fprintf(stdout
, PERF_COLOR_BLUE
, "%s\n", dl
->line
);
739 if (ppercents
!= &percent
)
742 } else if (max_lines
&& printed
>= max_lines
)
750 if (perf_evsel__is_group_event(evsel
))
751 width
*= evsel
->nr_members
;
754 printf(" %*s:\n", width
, " ");
756 printf(" %*s: %s\n", width
, " ", dl
->line
);
763 * symbol__parse_objdump_line() parses objdump output (with -d --no-show-raw)
764 * which looks like following
766 * 0000000000415500 <_init>:
767 * 415500: sub $0x8,%rsp
768 * 415504: mov 0x2f5ad5(%rip),%rax # 70afe0 <_DYNAMIC+0x2f8>
769 * 41550b: test %rax,%rax
770 * 41550e: je 415515 <_init+0x15>
771 * 415510: callq 416e70 <__gmon_start__@plt>
772 * 415515: add $0x8,%rsp
775 * it will be parsed and saved into struct disasm_line as
776 * <offset> <name> <ops.raw>
778 * The offset will be a relative offset from the start of the symbol and -1
779 * means that it's not a disassembly line so should be treated differently.
780 * The ops.raw part will be parsed further according to type of the instruction.
782 static int symbol__parse_objdump_line(struct symbol
*sym
, struct map
*map
,
783 FILE *file
, size_t privsize
)
785 struct annotation
*notes
= symbol__annotation(sym
);
786 struct disasm_line
*dl
;
787 char *line
= NULL
, *parsed_line
, *tmp
, *tmp2
, *c
;
789 s64 line_ip
, offset
= -1;
791 if (getline(&line
, &line_len
, file
) < 0)
797 while (line_len
!= 0 && isspace(line
[line_len
- 1]))
798 line
[--line_len
] = '\0';
800 c
= strchr(line
, '\n');
808 * Strip leading spaces:
819 * Parse hexa addresses followed by ':'
821 line_ip
= strtoull(tmp
, &tmp2
, 16);
822 if (*tmp2
!= ':' || tmp
== tmp2
|| tmp2
[1] == '\0')
827 u64 start
= map__rip_2objdump(map
, sym
->start
),
828 end
= map__rip_2objdump(map
, sym
->end
);
830 offset
= line_ip
- start
;
831 if ((u64
)line_ip
< start
|| (u64
)line_ip
> end
)
834 parsed_line
= tmp2
+ 1;
837 dl
= disasm_line__new(offset
, parsed_line
, privsize
);
843 if (dl
->ops
.target
.offset
== UINT64_MAX
)
844 dl
->ops
.target
.offset
= dl
->ops
.target
.addr
-
845 map__rip_2objdump(map
, sym
->start
);
847 /* kcore has no symbols, so add the call target name */
848 if (dl
->ins
&& ins__is_call(dl
->ins
) && !dl
->ops
.target
.name
) {
849 struct addr_map_symbol target
= {
851 .addr
= dl
->ops
.target
.addr
,
854 if (!map_groups__find_ams(&target
, NULL
) &&
855 target
.sym
->start
== target
.al_addr
)
856 dl
->ops
.target
.name
= strdup(target
.sym
->name
);
859 disasm__add(¬es
->src
->source
, dl
);
864 static void delete_last_nop(struct symbol
*sym
)
866 struct annotation
*notes
= symbol__annotation(sym
);
867 struct list_head
*list
= ¬es
->src
->source
;
868 struct disasm_line
*dl
;
870 while (!list_empty(list
)) {
871 dl
= list_entry(list
->prev
, struct disasm_line
, node
);
873 if (dl
->ins
&& dl
->ins
->ops
) {
874 if (dl
->ins
->ops
!= &nop_ops
)
877 if (!strstr(dl
->line
, " nop ") &&
878 !strstr(dl
->line
, " nopl ") &&
879 !strstr(dl
->line
, " nopw "))
884 disasm_line__free(dl
);
888 int symbol__annotate(struct symbol
*sym
, struct map
*map
, size_t privsize
)
890 struct dso
*dso
= map
->dso
;
891 char *filename
= dso__build_id_filename(dso
, NULL
, 0);
892 bool free_filename
= true;
893 char command
[PATH_MAX
* 2];
896 char symfs_filename
[PATH_MAX
];
897 struct kcore_extract kce
;
898 bool delete_extract
= false;
901 snprintf(symfs_filename
, sizeof(symfs_filename
), "%s%s",
902 symbol_conf
.symfs
, filename
);
905 if (filename
== NULL
) {
906 if (dso
->has_build_id
) {
907 pr_err("Can't annotate %s: not enough memory\n",
912 } else if (readlink(symfs_filename
, command
, sizeof(command
)) < 0 ||
913 strstr(command
, "[kernel.kallsyms]") ||
914 access(symfs_filename
, R_OK
)) {
918 * If we don't have build-ids or the build-id file isn't in the
919 * cache, or is just a kallsyms file, well, lets hope that this
920 * DSO is the same as when 'perf record' ran.
922 filename
= (char *)dso
->long_name
;
923 snprintf(symfs_filename
, sizeof(symfs_filename
), "%s%s",
924 symbol_conf
.symfs
, filename
);
925 free_filename
= false;
928 if (dso
->symtab_type
== DSO_BINARY_TYPE__KALLSYMS
&&
929 !dso__is_kcore(dso
)) {
930 char bf
[BUILD_ID_SIZE
* 2 + 16] = " with build id ";
931 char *build_id_msg
= NULL
;
933 if (dso
->annotate_warned
)
934 goto out_free_filename
;
936 if (dso
->has_build_id
) {
937 build_id__sprintf(dso
->build_id
,
938 sizeof(dso
->build_id
), bf
+ 15);
942 dso
->annotate_warned
= 1;
943 pr_err("Can't annotate %s:\n\n"
944 "No vmlinux file%s\nwas found in the path.\n\n"
946 " perf buildid-cache -vu vmlinux\n\n"
948 " --vmlinux vmlinux\n",
949 sym
->name
, build_id_msg
?: "");
950 goto out_free_filename
;
953 pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64
", end=%#" PRIx64
"\n", __func__
,
954 filename
, sym
->name
, map
->unmap_ip(map
, sym
->start
),
955 map
->unmap_ip(map
, sym
->end
));
957 pr_debug("annotating [%p] %30s : [%p] %30s\n",
958 dso
, dso
->long_name
, sym
, sym
->name
);
960 if (dso__is_kcore(dso
)) {
961 kce
.kcore_filename
= symfs_filename
;
962 kce
.addr
= map__rip_2objdump(map
, sym
->start
);
963 kce
.offs
= sym
->start
;
964 kce
.len
= sym
->end
+ 1 - sym
->start
;
965 if (!kcore_extract__create(&kce
)) {
966 delete_extract
= true;
967 strlcpy(symfs_filename
, kce
.extract_filename
,
968 sizeof(symfs_filename
));
971 free_filename
= false;
973 filename
= symfs_filename
;
977 snprintf(command
, sizeof(command
),
978 "%s %s%s --start-address=0x%016" PRIx64
979 " --stop-address=0x%016" PRIx64
980 " -d %s %s -C %s 2>/dev/null|grep -v %s|expand",
981 objdump_path
? objdump_path
: "objdump",
982 disassembler_style
? "-M " : "",
983 disassembler_style
? disassembler_style
: "",
984 map__rip_2objdump(map
, sym
->start
),
985 map__rip_2objdump(map
, sym
->end
+1),
986 symbol_conf
.annotate_asm_raw
? "" : "--no-show-raw",
987 symbol_conf
.annotate_src
? "-S" : "",
988 symfs_filename
, filename
);
990 pr_debug("Executing: %s\n", command
);
992 file
= popen(command
, "r");
994 goto out_free_filename
;
997 if (symbol__parse_objdump_line(sym
, map
, file
, privsize
) < 0)
1001 * kallsyms does not have symbol sizes so there may a nop at the end.
1004 if (dso__is_kcore(dso
))
1005 delete_last_nop(sym
);
1010 kcore_extract__delete(&kce
);
1016 static void insert_source_line(struct rb_root
*root
, struct source_line
*src_line
)
1018 struct source_line
*iter
;
1019 struct rb_node
**p
= &root
->rb_node
;
1020 struct rb_node
*parent
= NULL
;
1023 while (*p
!= NULL
) {
1025 iter
= rb_entry(parent
, struct source_line
, node
);
1027 ret
= strcmp(iter
->path
, src_line
->path
);
1029 for (i
= 0; i
< src_line
->nr_pcnt
; i
++)
1030 iter
->p
[i
].percent_sum
+= src_line
->p
[i
].percent
;
1037 p
= &(*p
)->rb_right
;
1040 for (i
= 0; i
< src_line
->nr_pcnt
; i
++)
1041 src_line
->p
[i
].percent_sum
= src_line
->p
[i
].percent
;
1043 rb_link_node(&src_line
->node
, parent
, p
);
1044 rb_insert_color(&src_line
->node
, root
);
1047 static int cmp_source_line(struct source_line
*a
, struct source_line
*b
)
1051 for (i
= 0; i
< a
->nr_pcnt
; i
++) {
1052 if (a
->p
[i
].percent_sum
== b
->p
[i
].percent_sum
)
1054 return a
->p
[i
].percent_sum
> b
->p
[i
].percent_sum
;
1060 static void __resort_source_line(struct rb_root
*root
, struct source_line
*src_line
)
1062 struct source_line
*iter
;
1063 struct rb_node
**p
= &root
->rb_node
;
1064 struct rb_node
*parent
= NULL
;
1066 while (*p
!= NULL
) {
1068 iter
= rb_entry(parent
, struct source_line
, node
);
1070 if (cmp_source_line(src_line
, iter
))
1073 p
= &(*p
)->rb_right
;
1076 rb_link_node(&src_line
->node
, parent
, p
);
1077 rb_insert_color(&src_line
->node
, root
);
1080 static void resort_source_line(struct rb_root
*dest_root
, struct rb_root
*src_root
)
1082 struct source_line
*src_line
;
1083 struct rb_node
*node
;
1085 node
= rb_first(src_root
);
1087 struct rb_node
*next
;
1089 src_line
= rb_entry(node
, struct source_line
, node
);
1090 next
= rb_next(node
);
1091 rb_erase(node
, src_root
);
1093 __resort_source_line(dest_root
, src_line
);
1098 static void symbol__free_source_line(struct symbol
*sym
, int len
)
1100 struct annotation
*notes
= symbol__annotation(sym
);
1101 struct source_line
*src_line
= notes
->src
->lines
;
1102 size_t sizeof_src_line
;
1105 sizeof_src_line
= sizeof(*src_line
) +
1106 (sizeof(src_line
->p
) * (src_line
->nr_pcnt
- 1));
1108 for (i
= 0; i
< len
; i
++) {
1109 free_srcline(src_line
->path
);
1110 src_line
= (void *)src_line
+ sizeof_src_line
;
1113 zfree(¬es
->src
->lines
);
1116 /* Get the filename:line for the colored entries */
1117 static int symbol__get_source_line(struct symbol
*sym
, struct map
*map
,
1118 struct perf_evsel
*evsel
,
1119 struct rb_root
*root
, int len
)
1123 int evidx
= evsel
->idx
;
1124 struct source_line
*src_line
;
1125 struct annotation
*notes
= symbol__annotation(sym
);
1126 struct sym_hist
*h
= annotation__histogram(notes
, evidx
);
1127 struct rb_root tmp_root
= RB_ROOT
;
1130 size_t sizeof_src_line
= sizeof(struct source_line
);
1132 if (perf_evsel__is_group_event(evsel
)) {
1133 for (i
= 1; i
< evsel
->nr_members
; i
++) {
1134 h
= annotation__histogram(notes
, evidx
+ i
);
1137 nr_pcnt
= evsel
->nr_members
;
1138 sizeof_src_line
+= (nr_pcnt
- 1) * sizeof(src_line
->p
);
1144 src_line
= notes
->src
->lines
= calloc(len
, sizeof_src_line
);
1145 if (!notes
->src
->lines
)
1148 start
= map__rip_2objdump(map
, sym
->start
);
1150 for (i
= 0; i
< len
; i
++) {
1152 double percent_max
= 0.0;
1154 src_line
->nr_pcnt
= nr_pcnt
;
1156 for (k
= 0; k
< nr_pcnt
; k
++) {
1157 h
= annotation__histogram(notes
, evidx
+ k
);
1158 src_line
->p
[k
].percent
= 100.0 * h
->addr
[i
] / h
->sum
;
1160 if (src_line
->p
[k
].percent
> percent_max
)
1161 percent_max
= src_line
->p
[k
].percent
;
1164 if (percent_max
<= 0.5)
1168 src_line
->path
= get_srcline(map
->dso
, offset
);
1169 insert_source_line(&tmp_root
, src_line
);
1172 src_line
= (void *)src_line
+ sizeof_src_line
;
1175 resort_source_line(root
, &tmp_root
);
1179 static void print_summary(struct rb_root
*root
, const char *filename
)
1181 struct source_line
*src_line
;
1182 struct rb_node
*node
;
1184 printf("\nSorted summary for file %s\n", filename
);
1185 printf("----------------------------------------------\n\n");
1187 if (RB_EMPTY_ROOT(root
)) {
1188 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN
);
1192 node
= rb_first(root
);
1194 double percent
, percent_max
= 0.0;
1199 src_line
= rb_entry(node
, struct source_line
, node
);
1200 for (i
= 0; i
< src_line
->nr_pcnt
; i
++) {
1201 percent
= src_line
->p
[i
].percent_sum
;
1202 color
= get_percent_color(percent
);
1203 color_fprintf(stdout
, color
, " %7.2f", percent
);
1205 if (percent
> percent_max
)
1206 percent_max
= percent
;
1209 path
= src_line
->path
;
1210 color
= get_percent_color(percent_max
);
1211 color_fprintf(stdout
, color
, " %s\n", path
);
1213 node
= rb_next(node
);
1217 static void symbol__annotate_hits(struct symbol
*sym
, struct perf_evsel
*evsel
)
1219 struct annotation
*notes
= symbol__annotation(sym
);
1220 struct sym_hist
*h
= annotation__histogram(notes
, evsel
->idx
);
1221 u64 len
= symbol__size(sym
), offset
;
1223 for (offset
= 0; offset
< len
; ++offset
)
1224 if (h
->addr
[offset
] != 0)
1225 printf("%*" PRIx64
": %" PRIu64
"\n", BITS_PER_LONG
/ 2,
1226 sym
->start
+ offset
, h
->addr
[offset
]);
1227 printf("%*s: %" PRIu64
"\n", BITS_PER_LONG
/ 2, "h->sum", h
->sum
);
1230 int symbol__annotate_printf(struct symbol
*sym
, struct map
*map
,
1231 struct perf_evsel
*evsel
, bool full_paths
,
1232 int min_pcnt
, int max_lines
, int context
)
1234 struct dso
*dso
= map
->dso
;
1236 const char *d_filename
;
1237 struct annotation
*notes
= symbol__annotation(sym
);
1238 struct disasm_line
*pos
, *queue
= NULL
;
1239 u64 start
= map__rip_2objdump(map
, sym
->start
);
1240 int printed
= 2, queue_len
= 0;
1246 filename
= strdup(dso
->long_name
);
1251 d_filename
= filename
;
1253 d_filename
= basename(filename
);
1255 len
= symbol__size(sym
);
1256 namelen
= strlen(d_filename
);
1258 if (perf_evsel__is_group_event(evsel
))
1259 width
*= evsel
->nr_members
;
1261 printf(" %-*.*s| Source code & Disassembly of %s\n",
1262 width
, width
, "Percent", d_filename
);
1263 printf("-%-*.*s-------------------------------------\n",
1264 width
+namelen
, width
+namelen
, graph_dotted_line
);
1267 symbol__annotate_hits(sym
, evsel
);
1269 list_for_each_entry(pos
, ¬es
->src
->source
, node
) {
1270 if (context
&& queue
== NULL
) {
1275 switch (disasm_line__print(pos
, sym
, start
, evsel
, len
,
1276 min_pcnt
, printed
, max_lines
,
1281 printed
+= queue_len
;
1287 /* filtered by max_lines */
1293 * Filtered by min_pcnt or non IP lines when
1298 if (queue_len
== context
)
1299 queue
= list_entry(queue
->node
.next
, typeof(*queue
), node
);
1311 void symbol__annotate_zero_histogram(struct symbol
*sym
, int evidx
)
1313 struct annotation
*notes
= symbol__annotation(sym
);
1314 struct sym_hist
*h
= annotation__histogram(notes
, evidx
);
1316 memset(h
, 0, notes
->src
->sizeof_sym_hist
);
1319 void symbol__annotate_decay_histogram(struct symbol
*sym
, int evidx
)
1321 struct annotation
*notes
= symbol__annotation(sym
);
1322 struct sym_hist
*h
= annotation__histogram(notes
, evidx
);
1323 int len
= symbol__size(sym
), offset
;
1326 for (offset
= 0; offset
< len
; ++offset
) {
1327 h
->addr
[offset
] = h
->addr
[offset
] * 7 / 8;
1328 h
->sum
+= h
->addr
[offset
];
1332 void disasm__purge(struct list_head
*head
)
1334 struct disasm_line
*pos
, *n
;
1336 list_for_each_entry_safe(pos
, n
, head
, node
) {
1337 list_del(&pos
->node
);
1338 disasm_line__free(pos
);
1342 static size_t disasm_line__fprintf(struct disasm_line
*dl
, FILE *fp
)
1346 if (dl
->offset
== -1)
1347 return fprintf(fp
, "%s\n", dl
->line
);
1349 printed
= fprintf(fp
, "%#" PRIx64
" %s", dl
->offset
, dl
->name
);
1351 if (dl
->ops
.raw
[0] != '\0') {
1352 printed
+= fprintf(fp
, "%.*s %s\n", 6 - (int)printed
, " ",
1356 return printed
+ fprintf(fp
, "\n");
1359 size_t disasm__fprintf(struct list_head
*head
, FILE *fp
)
1361 struct disasm_line
*pos
;
1364 list_for_each_entry(pos
, head
, node
)
1365 printed
+= disasm_line__fprintf(pos
, fp
);
1370 int symbol__tty_annotate(struct symbol
*sym
, struct map
*map
,
1371 struct perf_evsel
*evsel
, bool print_lines
,
1372 bool full_paths
, int min_pcnt
, int max_lines
)
1374 struct dso
*dso
= map
->dso
;
1375 struct rb_root source_line
= RB_ROOT
;
1378 if (symbol__annotate(sym
, map
, 0) < 0)
1381 len
= symbol__size(sym
);
1384 symbol__get_source_line(sym
, map
, evsel
, &source_line
, len
);
1385 print_summary(&source_line
, dso
->long_name
);
1388 symbol__annotate_printf(sym
, map
, evsel
, full_paths
,
1389 min_pcnt
, max_lines
, 0);
1391 symbol__free_source_line(sym
, len
);
1393 disasm__purge(&symbol__annotation(sym
)->src
->source
);
1398 int hist_entry__annotate(struct hist_entry
*he
, size_t privsize
)
1400 return symbol__annotate(he
->ms
.sym
, he
->ms
.map
, privsize
);