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 struct ins
*ins__find(const char *name
);
22 static int disasm_line__parse(char *line
, char **namep
, char **rawp
);
24 static void ins__delete(struct ins_operands
*ops
)
26 free(ops
->source
.raw
);
27 free(ops
->source
.name
);
28 free(ops
->target
.raw
);
29 free(ops
->target
.name
);
32 static int ins__raw_scnprintf(struct ins
*ins
, char *bf
, size_t size
,
33 struct ins_operands
*ops
)
35 return scnprintf(bf
, size
, "%-6.6s %s", ins
->name
, ops
->raw
);
38 int ins__scnprintf(struct ins
*ins
, char *bf
, size_t size
,
39 struct ins_operands
*ops
)
41 if (ins
->ops
->scnprintf
)
42 return ins
->ops
->scnprintf(ins
, bf
, size
, ops
);
44 return ins__raw_scnprintf(ins
, bf
, size
, ops
);
47 static int call__parse(struct ins_operands
*ops
)
49 char *endptr
, *tok
, *name
;
51 ops
->target
.addr
= strtoull(ops
->raw
, &endptr
, 16);
53 name
= strchr(endptr
, '<');
59 tok
= strchr(name
, '>');
64 ops
->target
.name
= strdup(name
);
67 return ops
->target
.name
== NULL
? -1 : 0;
70 tok
= strchr(endptr
, '(');
76 tok
= strchr(endptr
, '*');
80 ops
->target
.addr
= strtoull(tok
+ 1, NULL
, 16);
84 static int call__scnprintf(struct ins
*ins
, char *bf
, size_t size
,
85 struct ins_operands
*ops
)
88 return scnprintf(bf
, size
, "%-6.6s %s", ins
->name
, ops
->target
.name
);
90 if (ops
->target
.addr
== 0)
91 return ins__raw_scnprintf(ins
, bf
, size
, ops
);
93 return scnprintf(bf
, size
, "%-6.6s *%" PRIx64
, ins
->name
, ops
->target
.addr
);
96 static struct ins_ops call_ops
= {
98 .scnprintf
= call__scnprintf
,
101 bool ins__is_call(const struct ins
*ins
)
103 return ins
->ops
== &call_ops
;
106 static int jump__parse(struct ins_operands
*ops
)
108 const char *s
= strchr(ops
->raw
, '+');
110 ops
->target
.addr
= strtoll(ops
->raw
, NULL
, 16);
113 ops
->target
.offset
= strtoll(s
, NULL
, 16);
115 ops
->target
.offset
= UINT64_MAX
;
120 static int jump__scnprintf(struct ins
*ins
, char *bf
, size_t size
,
121 struct ins_operands
*ops
)
123 return scnprintf(bf
, size
, "%-6.6s %" PRIx64
, ins
->name
, ops
->target
.offset
);
126 static struct ins_ops jump_ops
= {
127 .parse
= jump__parse
,
128 .scnprintf
= jump__scnprintf
,
131 bool ins__is_jump(const struct ins
*ins
)
133 return ins
->ops
== &jump_ops
;
136 static int comment__symbol(char *raw
, char *comment
, u64
*addrp
, char **namep
)
138 char *endptr
, *name
, *t
;
140 if (strstr(raw
, "(%rip)") == NULL
)
143 *addrp
= strtoull(comment
, &endptr
, 16);
144 name
= strchr(endptr
, '<');
150 t
= strchr(name
, '>');
155 *namep
= strdup(name
);
161 static int lock__parse(struct ins_operands
*ops
)
165 ops
->locked
.ops
= zalloc(sizeof(*ops
->locked
.ops
));
166 if (ops
->locked
.ops
== NULL
)
169 if (disasm_line__parse(ops
->raw
, &name
, &ops
->locked
.ops
->raw
) < 0)
172 ops
->locked
.ins
= ins__find(name
);
173 if (ops
->locked
.ins
== NULL
)
176 if (!ops
->locked
.ins
->ops
)
179 if (ops
->locked
.ins
->ops
->parse
)
180 ops
->locked
.ins
->ops
->parse(ops
->locked
.ops
);
185 free(ops
->locked
.ops
);
186 ops
->locked
.ops
= NULL
;
190 static int lock__scnprintf(struct ins
*ins
, char *bf
, size_t size
,
191 struct ins_operands
*ops
)
195 if (ops
->locked
.ins
== NULL
)
196 return ins__raw_scnprintf(ins
, bf
, size
, ops
);
198 printed
= scnprintf(bf
, size
, "%-6.6s ", ins
->name
);
199 return printed
+ ins__scnprintf(ops
->locked
.ins
, bf
+ printed
,
200 size
- printed
, ops
->locked
.ops
);
203 static void lock__delete(struct ins_operands
*ops
)
205 free(ops
->locked
.ops
);
206 free(ops
->target
.raw
);
207 free(ops
->target
.name
);
210 static struct ins_ops lock_ops
= {
211 .free
= lock__delete
,
212 .parse
= lock__parse
,
213 .scnprintf
= lock__scnprintf
,
216 static int mov__parse(struct ins_operands
*ops
)
218 char *s
= strchr(ops
->raw
, ','), *target
, *comment
, prev
;
224 ops
->source
.raw
= strdup(ops
->raw
);
227 if (ops
->source
.raw
== NULL
)
232 while (s
[0] != '\0' && !isspace(s
[0]))
237 ops
->target
.raw
= strdup(target
);
240 if (ops
->target
.raw
== NULL
)
241 goto out_free_source
;
243 comment
= strchr(s
, '#');
247 while (comment
[0] != '\0' && isspace(comment
[0]))
250 comment__symbol(ops
->source
.raw
, comment
, &ops
->source
.addr
, &ops
->source
.name
);
251 comment__symbol(ops
->target
.raw
, comment
, &ops
->target
.addr
, &ops
->target
.name
);
256 free(ops
->source
.raw
);
257 ops
->source
.raw
= NULL
;
261 static int mov__scnprintf(struct ins
*ins
, char *bf
, size_t size
,
262 struct ins_operands
*ops
)
264 return scnprintf(bf
, size
, "%-6.6s %s,%s", ins
->name
,
265 ops
->source
.name
?: ops
->source
.raw
,
266 ops
->target
.name
?: ops
->target
.raw
);
269 static struct ins_ops mov_ops
= {
271 .scnprintf
= mov__scnprintf
,
274 static int dec__parse(struct ins_operands
*ops
)
276 char *target
, *comment
, *s
, prev
;
278 target
= s
= ops
->raw
;
280 while (s
[0] != '\0' && !isspace(s
[0]))
285 ops
->target
.raw
= strdup(target
);
288 if (ops
->target
.raw
== NULL
)
291 comment
= strchr(s
, '#');
295 while (comment
[0] != '\0' && isspace(comment
[0]))
298 comment__symbol(ops
->target
.raw
, comment
, &ops
->target
.addr
, &ops
->target
.name
);
303 static int dec__scnprintf(struct ins
*ins
, char *bf
, size_t size
,
304 struct ins_operands
*ops
)
306 return scnprintf(bf
, size
, "%-6.6s %s", ins
->name
,
307 ops
->target
.name
?: ops
->target
.raw
);
310 static struct ins_ops dec_ops
= {
312 .scnprintf
= dec__scnprintf
,
315 static int nop__scnprintf(struct ins
*ins __used
, char *bf
, size_t size
,
316 struct ins_operands
*ops __used
)
318 return scnprintf(bf
, size
, "%-6.6s", "nop");
321 static struct ins_ops nop_ops
= {
322 .scnprintf
= nop__scnprintf
,
326 * Must be sorted by name!
328 static struct ins instructions
[] = {
329 { .name
= "add", .ops
= &mov_ops
, },
330 { .name
= "addl", .ops
= &mov_ops
, },
331 { .name
= "addq", .ops
= &mov_ops
, },
332 { .name
= "addw", .ops
= &mov_ops
, },
333 { .name
= "and", .ops
= &mov_ops
, },
334 { .name
= "bts", .ops
= &mov_ops
, },
335 { .name
= "call", .ops
= &call_ops
, },
336 { .name
= "callq", .ops
= &call_ops
, },
337 { .name
= "cmp", .ops
= &mov_ops
, },
338 { .name
= "cmpb", .ops
= &mov_ops
, },
339 { .name
= "cmpl", .ops
= &mov_ops
, },
340 { .name
= "cmpq", .ops
= &mov_ops
, },
341 { .name
= "cmpw", .ops
= &mov_ops
, },
342 { .name
= "cmpxch", .ops
= &mov_ops
, },
343 { .name
= "dec", .ops
= &dec_ops
, },
344 { .name
= "decl", .ops
= &dec_ops
, },
345 { .name
= "imul", .ops
= &mov_ops
, },
346 { .name
= "inc", .ops
= &dec_ops
, },
347 { .name
= "incl", .ops
= &dec_ops
, },
348 { .name
= "ja", .ops
= &jump_ops
, },
349 { .name
= "jae", .ops
= &jump_ops
, },
350 { .name
= "jb", .ops
= &jump_ops
, },
351 { .name
= "jbe", .ops
= &jump_ops
, },
352 { .name
= "jc", .ops
= &jump_ops
, },
353 { .name
= "jcxz", .ops
= &jump_ops
, },
354 { .name
= "je", .ops
= &jump_ops
, },
355 { .name
= "jecxz", .ops
= &jump_ops
, },
356 { .name
= "jg", .ops
= &jump_ops
, },
357 { .name
= "jge", .ops
= &jump_ops
, },
358 { .name
= "jl", .ops
= &jump_ops
, },
359 { .name
= "jle", .ops
= &jump_ops
, },
360 { .name
= "jmp", .ops
= &jump_ops
, },
361 { .name
= "jmpq", .ops
= &jump_ops
, },
362 { .name
= "jna", .ops
= &jump_ops
, },
363 { .name
= "jnae", .ops
= &jump_ops
, },
364 { .name
= "jnb", .ops
= &jump_ops
, },
365 { .name
= "jnbe", .ops
= &jump_ops
, },
366 { .name
= "jnc", .ops
= &jump_ops
, },
367 { .name
= "jne", .ops
= &jump_ops
, },
368 { .name
= "jng", .ops
= &jump_ops
, },
369 { .name
= "jnge", .ops
= &jump_ops
, },
370 { .name
= "jnl", .ops
= &jump_ops
, },
371 { .name
= "jnle", .ops
= &jump_ops
, },
372 { .name
= "jno", .ops
= &jump_ops
, },
373 { .name
= "jnp", .ops
= &jump_ops
, },
374 { .name
= "jns", .ops
= &jump_ops
, },
375 { .name
= "jnz", .ops
= &jump_ops
, },
376 { .name
= "jo", .ops
= &jump_ops
, },
377 { .name
= "jp", .ops
= &jump_ops
, },
378 { .name
= "jpe", .ops
= &jump_ops
, },
379 { .name
= "jpo", .ops
= &jump_ops
, },
380 { .name
= "jrcxz", .ops
= &jump_ops
, },
381 { .name
= "js", .ops
= &jump_ops
, },
382 { .name
= "jz", .ops
= &jump_ops
, },
383 { .name
= "lea", .ops
= &mov_ops
, },
384 { .name
= "lock", .ops
= &lock_ops
, },
385 { .name
= "mov", .ops
= &mov_ops
, },
386 { .name
= "movb", .ops
= &mov_ops
, },
387 { .name
= "movdqa",.ops
= &mov_ops
, },
388 { .name
= "movl", .ops
= &mov_ops
, },
389 { .name
= "movq", .ops
= &mov_ops
, },
390 { .name
= "movslq", .ops
= &mov_ops
, },
391 { .name
= "movzbl", .ops
= &mov_ops
, },
392 { .name
= "movzwl", .ops
= &mov_ops
, },
393 { .name
= "nop", .ops
= &nop_ops
, },
394 { .name
= "nopl", .ops
= &nop_ops
, },
395 { .name
= "nopw", .ops
= &nop_ops
, },
396 { .name
= "or", .ops
= &mov_ops
, },
397 { .name
= "orl", .ops
= &mov_ops
, },
398 { .name
= "test", .ops
= &mov_ops
, },
399 { .name
= "testb", .ops
= &mov_ops
, },
400 { .name
= "testl", .ops
= &mov_ops
, },
401 { .name
= "xadd", .ops
= &mov_ops
, },
404 static int ins__cmp(const void *name
, const void *insp
)
406 const struct ins
*ins
= insp
;
408 return strcmp(name
, ins
->name
);
411 static struct ins
*ins__find(const char *name
)
413 const int nmemb
= ARRAY_SIZE(instructions
);
415 return bsearch(name
, instructions
, nmemb
, sizeof(struct ins
), ins__cmp
);
418 int symbol__annotate_init(struct map
*map __used
, struct symbol
*sym
)
420 struct annotation
*notes
= symbol__annotation(sym
);
421 pthread_mutex_init(¬es
->lock
, NULL
);
425 int symbol__alloc_hist(struct symbol
*sym
)
427 struct annotation
*notes
= symbol__annotation(sym
);
428 const size_t size
= symbol__size(sym
);
429 size_t sizeof_sym_hist
;
431 /* Check for overflow when calculating sizeof_sym_hist */
432 if (size
> (SIZE_MAX
- sizeof(struct sym_hist
)) / sizeof(u64
))
435 sizeof_sym_hist
= (sizeof(struct sym_hist
) + size
* sizeof(u64
));
437 /* Check for overflow in zalloc argument */
438 if (sizeof_sym_hist
> (SIZE_MAX
- sizeof(*notes
->src
))
439 / symbol_conf
.nr_events
)
442 notes
->src
= zalloc(sizeof(*notes
->src
) + symbol_conf
.nr_events
* sizeof_sym_hist
);
443 if (notes
->src
== NULL
)
445 notes
->src
->sizeof_sym_hist
= sizeof_sym_hist
;
446 notes
->src
->nr_histograms
= symbol_conf
.nr_events
;
447 INIT_LIST_HEAD(¬es
->src
->source
);
451 void symbol__annotate_zero_histograms(struct symbol
*sym
)
453 struct annotation
*notes
= symbol__annotation(sym
);
455 pthread_mutex_lock(¬es
->lock
);
456 if (notes
->src
!= NULL
)
457 memset(notes
->src
->histograms
, 0,
458 notes
->src
->nr_histograms
* notes
->src
->sizeof_sym_hist
);
459 pthread_mutex_unlock(¬es
->lock
);
462 int symbol__inc_addr_samples(struct symbol
*sym
, struct map
*map
,
466 struct annotation
*notes
;
469 notes
= symbol__annotation(sym
);
470 if (notes
->src
== NULL
)
473 pr_debug3("%s: addr=%#" PRIx64
"\n", __func__
, map
->unmap_ip(map
, addr
));
475 if (addr
< sym
->start
|| addr
> sym
->end
)
478 offset
= addr
- sym
->start
;
479 h
= annotation__histogram(notes
, evidx
);
483 pr_debug3("%#" PRIx64
" %s: period++ [addr: %#" PRIx64
", %#" PRIx64
484 ", evidx=%d] => %" PRIu64
"\n", sym
->start
, sym
->name
,
485 addr
, addr
- sym
->start
, evidx
, h
->addr
[offset
]);
489 static void disasm_line__init_ins(struct disasm_line
*dl
)
491 dl
->ins
= ins__find(dl
->name
);
499 if (dl
->ins
->ops
->parse
)
500 dl
->ins
->ops
->parse(&dl
->ops
);
503 static int disasm_line__parse(char *line
, char **namep
, char **rawp
)
505 char *name
= line
, tmp
;
507 while (isspace(name
[0]))
515 while ((*rawp
)[0] != '\0' && !isspace((*rawp
)[0]))
520 *namep
= strdup(name
);
527 if ((*rawp
)[0] != '\0') {
529 while (isspace((*rawp
)[0]))
541 static struct disasm_line
*disasm_line__new(s64 offset
, char *line
, size_t privsize
)
543 struct disasm_line
*dl
= zalloc(sizeof(*dl
) + privsize
);
547 dl
->line
= strdup(line
);
548 if (dl
->line
== NULL
)
552 if (disasm_line__parse(dl
->line
, &dl
->name
, &dl
->ops
.raw
) < 0)
555 disasm_line__init_ins(dl
);
568 void disasm_line__free(struct disasm_line
*dl
)
572 if (dl
->ins
&& dl
->ins
->ops
->free
)
573 dl
->ins
->ops
->free(&dl
->ops
);
575 ins__delete(&dl
->ops
);
579 int disasm_line__scnprintf(struct disasm_line
*dl
, char *bf
, size_t size
, bool raw
)
582 return scnprintf(bf
, size
, "%-6.6s %s", dl
->name
, dl
->ops
.raw
);
584 return ins__scnprintf(dl
->ins
, bf
, size
, &dl
->ops
);
587 static void disasm__add(struct list_head
*head
, struct disasm_line
*line
)
589 list_add_tail(&line
->node
, head
);
592 struct disasm_line
*disasm__get_next_ip_line(struct list_head
*head
, struct disasm_line
*pos
)
594 list_for_each_entry_continue(pos
, head
, node
)
595 if (pos
->offset
>= 0)
601 static int disasm_line__print(struct disasm_line
*dl
, struct symbol
*sym
, u64 start
,
602 int evidx
, u64 len
, int min_pcnt
, int printed
,
603 int max_lines
, struct disasm_line
*queue
)
605 static const char *prev_line
;
606 static const char *prev_color
;
608 if (dl
->offset
!= -1) {
609 const char *path
= NULL
;
610 unsigned int hits
= 0;
611 double percent
= 0.0;
613 struct annotation
*notes
= symbol__annotation(sym
);
614 struct source_line
*src_line
= notes
->src
->lines
;
615 struct sym_hist
*h
= annotation__histogram(notes
, evidx
);
616 s64 offset
= dl
->offset
;
617 const u64 addr
= start
+ offset
;
618 struct disasm_line
*next
;
620 next
= disasm__get_next_ip_line(¬es
->src
->source
, dl
);
622 while (offset
< (s64
)len
&&
623 (next
== NULL
|| offset
< next
->offset
)) {
626 path
= src_line
[offset
].path
;
627 percent
+= src_line
[offset
].percent
;
629 hits
+= h
->addr
[offset
];
634 if (src_line
== NULL
&& h
->sum
)
635 percent
= 100.0 * hits
/ h
->sum
;
637 if (percent
< min_pcnt
)
640 if (max_lines
&& printed
>= max_lines
)
644 list_for_each_entry_from(queue
, ¬es
->src
->source
, node
) {
647 disasm_line__print(queue
, sym
, start
, evidx
, len
,
652 color
= get_percent_color(percent
);
655 * Also color the filename and line if needed, with
656 * the same color than the percentage. Don't print it
657 * twice for close colored addr with the same filename:line
660 if (!prev_line
|| strcmp(prev_line
, path
)
661 || color
!= prev_color
) {
662 color_fprintf(stdout
, color
, " %s", path
);
668 color_fprintf(stdout
, color
, " %7.2f", percent
);
670 color_fprintf(stdout
, PERF_COLOR_MAGENTA
, " %" PRIx64
":", addr
);
671 color_fprintf(stdout
, PERF_COLOR_BLUE
, "%s\n", dl
->line
);
672 } else if (max_lines
&& printed
>= max_lines
)
681 printf(" : %s\n", dl
->line
);
687 static int symbol__parse_objdump_line(struct symbol
*sym
, struct map
*map
,
688 FILE *file
, size_t privsize
)
690 struct annotation
*notes
= symbol__annotation(sym
);
691 struct disasm_line
*dl
;
692 char *line
= NULL
, *parsed_line
, *tmp
, *tmp2
, *c
;
694 s64 line_ip
, offset
= -1;
696 if (getline(&line
, &line_len
, file
) < 0)
702 while (line_len
!= 0 && isspace(line
[line_len
- 1]))
703 line
[--line_len
] = '\0';
705 c
= strchr(line
, '\n');
713 * Strip leading spaces:
724 * Parse hexa addresses followed by ':'
726 line_ip
= strtoull(tmp
, &tmp2
, 16);
727 if (*tmp2
!= ':' || tmp
== tmp2
|| tmp2
[1] == '\0')
732 u64 start
= map__rip_2objdump(map
, sym
->start
),
733 end
= map__rip_2objdump(map
, sym
->end
);
735 offset
= line_ip
- start
;
736 if (offset
< 0 || (u64
)line_ip
> end
)
739 parsed_line
= tmp2
+ 1;
742 dl
= disasm_line__new(offset
, parsed_line
, privsize
);
748 disasm__add(¬es
->src
->source
, dl
);
753 int symbol__annotate(struct symbol
*sym
, struct map
*map
, size_t privsize
)
755 struct dso
*dso
= map
->dso
;
756 char *filename
= dso__build_id_filename(dso
, NULL
, 0);
757 bool free_filename
= true;
758 char command
[PATH_MAX
* 2];
761 char symfs_filename
[PATH_MAX
];
764 snprintf(symfs_filename
, sizeof(symfs_filename
), "%s%s",
765 symbol_conf
.symfs
, filename
);
768 if (filename
== NULL
) {
769 if (dso
->has_build_id
) {
770 pr_err("Can't annotate %s: not enough memory\n",
775 } else if (readlink(symfs_filename
, command
, sizeof(command
)) < 0 ||
776 strstr(command
, "[kernel.kallsyms]") ||
777 access(symfs_filename
, R_OK
)) {
781 * If we don't have build-ids or the build-id file isn't in the
782 * cache, or is just a kallsyms file, well, lets hope that this
783 * DSO is the same as when 'perf record' ran.
785 filename
= dso
->long_name
;
786 snprintf(symfs_filename
, sizeof(symfs_filename
), "%s%s",
787 symbol_conf
.symfs
, filename
);
788 free_filename
= false;
791 if (dso
->symtab_type
== DSO_BINARY_TYPE__KALLSYMS
) {
792 char bf
[BUILD_ID_SIZE
* 2 + 16] = " with build id ";
793 char *build_id_msg
= NULL
;
795 if (dso
->annotate_warned
)
796 goto out_free_filename
;
798 if (dso
->has_build_id
) {
799 build_id__sprintf(dso
->build_id
,
800 sizeof(dso
->build_id
), bf
+ 15);
804 dso
->annotate_warned
= 1;
805 pr_err("Can't annotate %s:\n\n"
806 "No vmlinux file%s\nwas found in the path.\n\n"
808 " perf buildid-cache -av vmlinux\n\n"
810 " --vmlinux vmlinux\n",
811 sym
->name
, build_id_msg
?: "");
812 goto out_free_filename
;
815 pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64
", end=%#" PRIx64
"\n", __func__
,
816 filename
, sym
->name
, map
->unmap_ip(map
, sym
->start
),
817 map
->unmap_ip(map
, sym
->end
));
819 pr_debug("annotating [%p] %30s : [%p] %30s\n",
820 dso
, dso
->long_name
, sym
, sym
->name
);
822 snprintf(command
, sizeof(command
),
823 "objdump %s%s --start-address=0x%016" PRIx64
824 " --stop-address=0x%016" PRIx64
825 " -d %s %s -C %s|grep -v %s|expand",
826 disassembler_style
? "-M " : "",
827 disassembler_style
? disassembler_style
: "",
828 map__rip_2objdump(map
, sym
->start
),
829 map__rip_2objdump(map
, sym
->end
+1),
830 symbol_conf
.annotate_asm_raw
? "" : "--no-show-raw",
831 symbol_conf
.annotate_src
? "-S" : "",
832 symfs_filename
, filename
);
834 pr_debug("Executing: %s\n", command
);
836 file
= popen(command
, "r");
838 goto out_free_filename
;
841 if (symbol__parse_objdump_line(sym
, map
, file
, privsize
) < 0)
851 static void insert_source_line(struct rb_root
*root
, struct source_line
*src_line
)
853 struct source_line
*iter
;
854 struct rb_node
**p
= &root
->rb_node
;
855 struct rb_node
*parent
= NULL
;
859 iter
= rb_entry(parent
, struct source_line
, node
);
861 if (src_line
->percent
> iter
->percent
)
867 rb_link_node(&src_line
->node
, parent
, p
);
868 rb_insert_color(&src_line
->node
, root
);
871 static void symbol__free_source_line(struct symbol
*sym
, int len
)
873 struct annotation
*notes
= symbol__annotation(sym
);
874 struct source_line
*src_line
= notes
->src
->lines
;
877 for (i
= 0; i
< len
; i
++)
878 free(src_line
[i
].path
);
881 notes
->src
->lines
= NULL
;
884 /* Get the filename:line for the colored entries */
885 static int symbol__get_source_line(struct symbol
*sym
, struct map
*map
,
886 int evidx
, struct rb_root
*root
, int len
,
887 const char *filename
)
891 char cmd
[PATH_MAX
* 2];
892 struct source_line
*src_line
;
893 struct annotation
*notes
= symbol__annotation(sym
);
894 struct sym_hist
*h
= annotation__histogram(notes
, evidx
);
899 src_line
= notes
->src
->lines
= calloc(len
, sizeof(struct source_line
));
900 if (!notes
->src
->lines
)
903 start
= map__rip_2objdump(map
, sym
->start
);
905 for (i
= 0; i
< len
; i
++) {
911 src_line
[i
].percent
= 100.0 * h
->addr
[i
] / h
->sum
;
912 if (src_line
[i
].percent
<= 0.5)
916 sprintf(cmd
, "addr2line -e %s %016" PRIx64
, filename
, offset
);
917 fp
= popen(cmd
, "r");
921 if (getline(&path
, &line_len
, fp
) < 0 || !line_len
)
924 src_line
[i
].path
= malloc(sizeof(char) * line_len
+ 1);
925 if (!src_line
[i
].path
)
928 strcpy(src_line
[i
].path
, path
);
929 insert_source_line(root
, &src_line
[i
]);
938 static void print_summary(struct rb_root
*root
, const char *filename
)
940 struct source_line
*src_line
;
941 struct rb_node
*node
;
943 printf("\nSorted summary for file %s\n", filename
);
944 printf("----------------------------------------------\n\n");
946 if (RB_EMPTY_ROOT(root
)) {
947 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN
);
951 node
= rb_first(root
);
957 src_line
= rb_entry(node
, struct source_line
, node
);
958 percent
= src_line
->percent
;
959 color
= get_percent_color(percent
);
960 path
= src_line
->path
;
962 color_fprintf(stdout
, color
, " %7.2f %s", percent
, path
);
963 node
= rb_next(node
);
967 static void symbol__annotate_hits(struct symbol
*sym
, int evidx
)
969 struct annotation
*notes
= symbol__annotation(sym
);
970 struct sym_hist
*h
= annotation__histogram(notes
, evidx
);
971 u64 len
= symbol__size(sym
), offset
;
973 for (offset
= 0; offset
< len
; ++offset
)
974 if (h
->addr
[offset
] != 0)
975 printf("%*" PRIx64
": %" PRIu64
"\n", BITS_PER_LONG
/ 2,
976 sym
->start
+ offset
, h
->addr
[offset
]);
977 printf("%*s: %" PRIu64
"\n", BITS_PER_LONG
/ 2, "h->sum", h
->sum
);
980 int symbol__annotate_printf(struct symbol
*sym
, struct map
*map
, int evidx
,
981 bool full_paths
, int min_pcnt
, int max_lines
,
984 struct dso
*dso
= map
->dso
;
985 const char *filename
= dso
->long_name
, *d_filename
;
986 struct annotation
*notes
= symbol__annotation(sym
);
987 struct disasm_line
*pos
, *queue
= NULL
;
988 u64 start
= map__rip_2objdump(map
, sym
->start
);
989 int printed
= 2, queue_len
= 0;
994 d_filename
= filename
;
996 d_filename
= basename(filename
);
998 len
= symbol__size(sym
);
1000 printf(" Percent | Source code & Disassembly of %s\n", d_filename
);
1001 printf("------------------------------------------------\n");
1004 symbol__annotate_hits(sym
, evidx
);
1006 list_for_each_entry(pos
, ¬es
->src
->source
, node
) {
1007 if (context
&& queue
== NULL
) {
1012 switch (disasm_line__print(pos
, sym
, start
, evidx
, len
,
1013 min_pcnt
, printed
, max_lines
,
1018 printed
+= queue_len
;
1024 /* filtered by max_lines */
1030 * Filtered by min_pcnt or non IP lines when
1035 if (queue_len
== context
)
1036 queue
= list_entry(queue
->node
.next
, typeof(*queue
), node
);
1046 void symbol__annotate_zero_histogram(struct symbol
*sym
, int evidx
)
1048 struct annotation
*notes
= symbol__annotation(sym
);
1049 struct sym_hist
*h
= annotation__histogram(notes
, evidx
);
1051 memset(h
, 0, notes
->src
->sizeof_sym_hist
);
1054 void symbol__annotate_decay_histogram(struct symbol
*sym
, int evidx
)
1056 struct annotation
*notes
= symbol__annotation(sym
);
1057 struct sym_hist
*h
= annotation__histogram(notes
, evidx
);
1058 int len
= symbol__size(sym
), offset
;
1061 for (offset
= 0; offset
< len
; ++offset
) {
1062 h
->addr
[offset
] = h
->addr
[offset
] * 7 / 8;
1063 h
->sum
+= h
->addr
[offset
];
1067 void disasm__purge(struct list_head
*head
)
1069 struct disasm_line
*pos
, *n
;
1071 list_for_each_entry_safe(pos
, n
, head
, node
) {
1072 list_del(&pos
->node
);
1073 disasm_line__free(pos
);
1077 static size_t disasm_line__fprintf(struct disasm_line
*dl
, FILE *fp
)
1081 if (dl
->offset
== -1)
1082 return fprintf(fp
, "%s\n", dl
->line
);
1084 printed
= fprintf(fp
, "%#" PRIx64
" %s", dl
->offset
, dl
->name
);
1086 if (dl
->ops
.raw
[0] != '\0') {
1087 printed
+= fprintf(fp
, "%.*s %s\n", 6 - (int)printed
, " ",
1091 return printed
+ fprintf(fp
, "\n");
1094 size_t disasm__fprintf(struct list_head
*head
, FILE *fp
)
1096 struct disasm_line
*pos
;
1099 list_for_each_entry(pos
, head
, node
)
1100 printed
+= disasm_line__fprintf(pos
, fp
);
1105 int symbol__tty_annotate(struct symbol
*sym
, struct map
*map
, int evidx
,
1106 bool print_lines
, bool full_paths
, int min_pcnt
,
1109 struct dso
*dso
= map
->dso
;
1110 const char *filename
= dso
->long_name
;
1111 struct rb_root source_line
= RB_ROOT
;
1114 if (symbol__annotate(sym
, map
, 0) < 0)
1117 len
= symbol__size(sym
);
1120 symbol__get_source_line(sym
, map
, evidx
, &source_line
,
1122 print_summary(&source_line
, filename
);
1125 symbol__annotate_printf(sym
, map
, evidx
, full_paths
,
1126 min_pcnt
, max_lines
, 0);
1128 symbol__free_source_line(sym
, len
);
1130 disasm__purge(&symbol__annotation(sym
)->src
->source
);