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
;
20 const char *objdump_path
;
22 static struct ins
*ins__find(const char *name
);
23 static int disasm_line__parse(char *line
, char **namep
, char **rawp
);
25 static void ins__delete(struct ins_operands
*ops
)
27 free(ops
->source
.raw
);
28 free(ops
->source
.name
);
29 free(ops
->target
.raw
);
30 free(ops
->target
.name
);
33 static int ins__raw_scnprintf(struct ins
*ins
, char *bf
, size_t size
,
34 struct ins_operands
*ops
)
36 return scnprintf(bf
, size
, "%-6.6s %s", ins
->name
, ops
->raw
);
39 int ins__scnprintf(struct ins
*ins
, char *bf
, size_t size
,
40 struct ins_operands
*ops
)
42 if (ins
->ops
->scnprintf
)
43 return ins
->ops
->scnprintf(ins
, bf
, size
, ops
);
45 return ins__raw_scnprintf(ins
, bf
, size
, ops
);
48 static int call__parse(struct ins_operands
*ops
)
50 char *endptr
, *tok
, *name
;
52 ops
->target
.addr
= strtoull(ops
->raw
, &endptr
, 16);
54 name
= strchr(endptr
, '<');
60 tok
= strchr(name
, '>');
65 ops
->target
.name
= strdup(name
);
68 return ops
->target
.name
== NULL
? -1 : 0;
71 tok
= strchr(endptr
, '(');
77 tok
= strchr(endptr
, '*');
81 ops
->target
.addr
= strtoull(tok
+ 1, NULL
, 16);
85 static int call__scnprintf(struct ins
*ins
, char *bf
, size_t size
,
86 struct ins_operands
*ops
)
89 return scnprintf(bf
, size
, "%-6.6s %s", ins
->name
, ops
->target
.name
);
91 if (ops
->target
.addr
== 0)
92 return ins__raw_scnprintf(ins
, bf
, size
, ops
);
94 return scnprintf(bf
, size
, "%-6.6s *%" PRIx64
, ins
->name
, ops
->target
.addr
);
97 static struct ins_ops call_ops
= {
99 .scnprintf
= call__scnprintf
,
102 bool ins__is_call(const struct ins
*ins
)
104 return ins
->ops
== &call_ops
;
107 static int jump__parse(struct ins_operands
*ops
)
109 const char *s
= strchr(ops
->raw
, '+');
111 ops
->target
.addr
= strtoll(ops
->raw
, NULL
, 16);
114 ops
->target
.offset
= strtoll(s
, NULL
, 16);
116 ops
->target
.offset
= UINT64_MAX
;
121 static int jump__scnprintf(struct ins
*ins
, char *bf
, size_t size
,
122 struct ins_operands
*ops
)
124 return scnprintf(bf
, size
, "%-6.6s %" PRIx64
, ins
->name
, ops
->target
.offset
);
127 static struct ins_ops jump_ops
= {
128 .parse
= jump__parse
,
129 .scnprintf
= jump__scnprintf
,
132 bool ins__is_jump(const struct ins
*ins
)
134 return ins
->ops
== &jump_ops
;
137 static int comment__symbol(char *raw
, char *comment
, u64
*addrp
, char **namep
)
139 char *endptr
, *name
, *t
;
141 if (strstr(raw
, "(%rip)") == NULL
)
144 *addrp
= strtoull(comment
, &endptr
, 16);
145 name
= strchr(endptr
, '<');
151 t
= strchr(name
, '>');
156 *namep
= strdup(name
);
162 static int lock__parse(struct ins_operands
*ops
)
166 ops
->locked
.ops
= zalloc(sizeof(*ops
->locked
.ops
));
167 if (ops
->locked
.ops
== NULL
)
170 if (disasm_line__parse(ops
->raw
, &name
, &ops
->locked
.ops
->raw
) < 0)
173 ops
->locked
.ins
= ins__find(name
);
174 if (ops
->locked
.ins
== NULL
)
177 if (!ops
->locked
.ins
->ops
)
180 if (ops
->locked
.ins
->ops
->parse
)
181 ops
->locked
.ins
->ops
->parse(ops
->locked
.ops
);
186 free(ops
->locked
.ops
);
187 ops
->locked
.ops
= NULL
;
191 static int lock__scnprintf(struct ins
*ins
, char *bf
, size_t size
,
192 struct ins_operands
*ops
)
196 if (ops
->locked
.ins
== NULL
)
197 return ins__raw_scnprintf(ins
, bf
, size
, ops
);
199 printed
= scnprintf(bf
, size
, "%-6.6s ", ins
->name
);
200 return printed
+ ins__scnprintf(ops
->locked
.ins
, bf
+ printed
,
201 size
- printed
, ops
->locked
.ops
);
204 static void lock__delete(struct ins_operands
*ops
)
206 free(ops
->locked
.ops
);
207 free(ops
->target
.raw
);
208 free(ops
->target
.name
);
211 static struct ins_ops lock_ops
= {
212 .free
= lock__delete
,
213 .parse
= lock__parse
,
214 .scnprintf
= lock__scnprintf
,
217 static int mov__parse(struct ins_operands
*ops
)
219 char *s
= strchr(ops
->raw
, ','), *target
, *comment
, prev
;
225 ops
->source
.raw
= strdup(ops
->raw
);
228 if (ops
->source
.raw
== NULL
)
233 while (s
[0] != '\0' && !isspace(s
[0]))
238 ops
->target
.raw
= strdup(target
);
241 if (ops
->target
.raw
== NULL
)
242 goto out_free_source
;
244 comment
= strchr(s
, '#');
248 while (comment
[0] != '\0' && isspace(comment
[0]))
251 comment__symbol(ops
->source
.raw
, comment
, &ops
->source
.addr
, &ops
->source
.name
);
252 comment__symbol(ops
->target
.raw
, comment
, &ops
->target
.addr
, &ops
->target
.name
);
257 free(ops
->source
.raw
);
258 ops
->source
.raw
= NULL
;
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
, },
405 static int ins__cmp(const void *name
, const void *insp
)
407 const struct ins
*ins
= insp
;
409 return strcmp(name
, ins
->name
);
412 static struct ins
*ins__find(const char *name
)
414 const int nmemb
= ARRAY_SIZE(instructions
);
416 return bsearch(name
, instructions
, nmemb
, sizeof(struct ins
), ins__cmp
);
419 int symbol__annotate_init(struct map
*map __maybe_unused
, struct symbol
*sym
)
421 struct annotation
*notes
= symbol__annotation(sym
);
422 pthread_mutex_init(¬es
->lock
, NULL
);
426 int symbol__alloc_hist(struct symbol
*sym
)
428 struct annotation
*notes
= symbol__annotation(sym
);
429 const size_t size
= symbol__size(sym
);
430 size_t sizeof_sym_hist
;
432 /* Check for overflow when calculating sizeof_sym_hist */
433 if (size
> (SIZE_MAX
- sizeof(struct sym_hist
)) / sizeof(u64
))
436 sizeof_sym_hist
= (sizeof(struct sym_hist
) + size
* sizeof(u64
));
438 /* Check for overflow in zalloc argument */
439 if (sizeof_sym_hist
> (SIZE_MAX
- sizeof(*notes
->src
))
440 / symbol_conf
.nr_events
)
443 notes
->src
= zalloc(sizeof(*notes
->src
) + symbol_conf
.nr_events
* sizeof_sym_hist
);
444 if (notes
->src
== NULL
)
446 notes
->src
->sizeof_sym_hist
= sizeof_sym_hist
;
447 notes
->src
->nr_histograms
= symbol_conf
.nr_events
;
448 INIT_LIST_HEAD(¬es
->src
->source
);
452 void symbol__annotate_zero_histograms(struct symbol
*sym
)
454 struct annotation
*notes
= symbol__annotation(sym
);
456 pthread_mutex_lock(¬es
->lock
);
457 if (notes
->src
!= NULL
)
458 memset(notes
->src
->histograms
, 0,
459 notes
->src
->nr_histograms
* notes
->src
->sizeof_sym_hist
);
460 pthread_mutex_unlock(¬es
->lock
);
463 int symbol__inc_addr_samples(struct symbol
*sym
, struct map
*map
,
467 struct annotation
*notes
;
470 notes
= symbol__annotation(sym
);
471 if (notes
->src
== NULL
)
474 pr_debug3("%s: addr=%#" PRIx64
"\n", __func__
, map
->unmap_ip(map
, addr
));
476 if (addr
< sym
->start
|| addr
> sym
->end
)
479 offset
= addr
- sym
->start
;
480 h
= annotation__histogram(notes
, evidx
);
484 pr_debug3("%#" PRIx64
" %s: period++ [addr: %#" PRIx64
", %#" PRIx64
485 ", evidx=%d] => %" PRIu64
"\n", sym
->start
, sym
->name
,
486 addr
, addr
- sym
->start
, evidx
, h
->addr
[offset
]);
490 static void disasm_line__init_ins(struct disasm_line
*dl
)
492 dl
->ins
= ins__find(dl
->name
);
500 if (dl
->ins
->ops
->parse
)
501 dl
->ins
->ops
->parse(&dl
->ops
);
504 static int disasm_line__parse(char *line
, char **namep
, char **rawp
)
506 char *name
= line
, tmp
;
508 while (isspace(name
[0]))
516 while ((*rawp
)[0] != '\0' && !isspace((*rawp
)[0]))
521 *namep
= strdup(name
);
528 if ((*rawp
)[0] != '\0') {
530 while (isspace((*rawp
)[0]))
542 static struct disasm_line
*disasm_line__new(s64 offset
, char *line
, size_t privsize
)
544 struct disasm_line
*dl
= zalloc(sizeof(*dl
) + privsize
);
548 dl
->line
= strdup(line
);
549 if (dl
->line
== NULL
)
553 if (disasm_line__parse(dl
->line
, &dl
->name
, &dl
->ops
.raw
) < 0)
556 disasm_line__init_ins(dl
);
569 void disasm_line__free(struct disasm_line
*dl
)
573 if (dl
->ins
&& dl
->ins
->ops
->free
)
574 dl
->ins
->ops
->free(&dl
->ops
);
576 ins__delete(&dl
->ops
);
580 int disasm_line__scnprintf(struct disasm_line
*dl
, char *bf
, size_t size
, bool raw
)
583 return scnprintf(bf
, size
, "%-6.6s %s", dl
->name
, dl
->ops
.raw
);
585 return ins__scnprintf(dl
->ins
, bf
, size
, &dl
->ops
);
588 static void disasm__add(struct list_head
*head
, struct disasm_line
*line
)
590 list_add_tail(&line
->node
, head
);
593 struct disasm_line
*disasm__get_next_ip_line(struct list_head
*head
, struct disasm_line
*pos
)
595 list_for_each_entry_continue(pos
, head
, node
)
596 if (pos
->offset
>= 0)
602 static int disasm_line__print(struct disasm_line
*dl
, struct symbol
*sym
, u64 start
,
603 int evidx
, u64 len
, int min_pcnt
, int printed
,
604 int max_lines
, struct disasm_line
*queue
)
606 static const char *prev_line
;
607 static const char *prev_color
;
609 if (dl
->offset
!= -1) {
610 const char *path
= NULL
;
611 unsigned int hits
= 0;
612 double percent
= 0.0;
614 struct annotation
*notes
= symbol__annotation(sym
);
615 struct source_line
*src_line
= notes
->src
->lines
;
616 struct sym_hist
*h
= annotation__histogram(notes
, evidx
);
617 s64 offset
= dl
->offset
;
618 const u64 addr
= start
+ offset
;
619 struct disasm_line
*next
;
621 next
= disasm__get_next_ip_line(¬es
->src
->source
, dl
);
623 while (offset
< (s64
)len
&&
624 (next
== NULL
|| offset
< next
->offset
)) {
627 path
= src_line
[offset
].path
;
628 percent
+= src_line
[offset
].percent
;
630 hits
+= h
->addr
[offset
];
635 if (src_line
== NULL
&& h
->sum
)
636 percent
= 100.0 * hits
/ h
->sum
;
638 if (percent
< min_pcnt
)
641 if (max_lines
&& printed
>= max_lines
)
645 list_for_each_entry_from(queue
, ¬es
->src
->source
, node
) {
648 disasm_line__print(queue
, sym
, start
, evidx
, len
,
653 color
= get_percent_color(percent
);
656 * Also color the filename and line if needed, with
657 * the same color than the percentage. Don't print it
658 * twice for close colored addr with the same filename:line
661 if (!prev_line
|| strcmp(prev_line
, path
)
662 || color
!= prev_color
) {
663 color_fprintf(stdout
, color
, " %s", path
);
669 color_fprintf(stdout
, color
, " %7.2f", percent
);
671 color_fprintf(stdout
, PERF_COLOR_MAGENTA
, " %" PRIx64
":", addr
);
672 color_fprintf(stdout
, PERF_COLOR_BLUE
, "%s\n", dl
->line
);
673 } else if (max_lines
&& printed
>= max_lines
)
682 printf(" : %s\n", dl
->line
);
688 static int symbol__parse_objdump_line(struct symbol
*sym
, struct map
*map
,
689 FILE *file
, size_t privsize
)
691 struct annotation
*notes
= symbol__annotation(sym
);
692 struct disasm_line
*dl
;
693 char *line
= NULL
, *parsed_line
, *tmp
, *tmp2
, *c
;
695 s64 line_ip
, offset
= -1;
697 if (getline(&line
, &line_len
, file
) < 0)
703 while (line_len
!= 0 && isspace(line
[line_len
- 1]))
704 line
[--line_len
] = '\0';
706 c
= strchr(line
, '\n');
714 * Strip leading spaces:
725 * Parse hexa addresses followed by ':'
727 line_ip
= strtoull(tmp
, &tmp2
, 16);
728 if (*tmp2
!= ':' || tmp
== tmp2
|| tmp2
[1] == '\0')
733 u64 start
= map__rip_2objdump(map
, sym
->start
),
734 end
= map__rip_2objdump(map
, sym
->end
);
736 offset
= line_ip
- start
;
737 if (offset
< 0 || (u64
)line_ip
> end
)
740 parsed_line
= tmp2
+ 1;
743 dl
= disasm_line__new(offset
, parsed_line
, privsize
);
749 disasm__add(¬es
->src
->source
, dl
);
754 int symbol__annotate(struct symbol
*sym
, struct map
*map
, size_t privsize
)
756 struct dso
*dso
= map
->dso
;
757 char *filename
= dso__build_id_filename(dso
, NULL
, 0);
758 bool free_filename
= true;
759 char command
[PATH_MAX
* 2];
762 char symfs_filename
[PATH_MAX
];
765 snprintf(symfs_filename
, sizeof(symfs_filename
), "%s%s",
766 symbol_conf
.symfs
, filename
);
769 if (filename
== NULL
) {
770 if (dso
->has_build_id
) {
771 pr_err("Can't annotate %s: not enough memory\n",
776 } else if (readlink(symfs_filename
, command
, sizeof(command
)) < 0 ||
777 strstr(command
, "[kernel.kallsyms]") ||
778 access(symfs_filename
, R_OK
)) {
782 * If we don't have build-ids or the build-id file isn't in the
783 * cache, or is just a kallsyms file, well, lets hope that this
784 * DSO is the same as when 'perf record' ran.
786 filename
= dso
->long_name
;
787 snprintf(symfs_filename
, sizeof(symfs_filename
), "%s%s",
788 symbol_conf
.symfs
, filename
);
789 free_filename
= false;
792 if (dso
->symtab_type
== DSO_BINARY_TYPE__KALLSYMS
) {
793 char bf
[BUILD_ID_SIZE
* 2 + 16] = " with build id ";
794 char *build_id_msg
= NULL
;
796 if (dso
->annotate_warned
)
797 goto out_free_filename
;
799 if (dso
->has_build_id
) {
800 build_id__sprintf(dso
->build_id
,
801 sizeof(dso
->build_id
), bf
+ 15);
805 dso
->annotate_warned
= 1;
806 pr_err("Can't annotate %s:\n\n"
807 "No vmlinux file%s\nwas found in the path.\n\n"
809 " perf buildid-cache -av vmlinux\n\n"
811 " --vmlinux vmlinux\n",
812 sym
->name
, build_id_msg
?: "");
813 goto out_free_filename
;
816 pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64
", end=%#" PRIx64
"\n", __func__
,
817 filename
, sym
->name
, map
->unmap_ip(map
, sym
->start
),
818 map
->unmap_ip(map
, sym
->end
));
820 pr_debug("annotating [%p] %30s : [%p] %30s\n",
821 dso
, dso
->long_name
, sym
, sym
->name
);
823 snprintf(command
, sizeof(command
),
824 "%s %s%s --start-address=0x%016" PRIx64
825 " --stop-address=0x%016" PRIx64
826 " -d %s %s -C %s|grep -v %s|expand",
827 objdump_path
? objdump_path
: "objdump",
828 disassembler_style
? "-M " : "",
829 disassembler_style
? disassembler_style
: "",
830 map__rip_2objdump(map
, sym
->start
),
831 map__rip_2objdump(map
, sym
->end
+1),
832 symbol_conf
.annotate_asm_raw
? "" : "--no-show-raw",
833 symbol_conf
.annotate_src
? "-S" : "",
834 symfs_filename
, filename
);
836 pr_debug("Executing: %s\n", command
);
838 file
= popen(command
, "r");
840 goto out_free_filename
;
843 if (symbol__parse_objdump_line(sym
, map
, file
, privsize
) < 0)
853 static void insert_source_line(struct rb_root
*root
, struct source_line
*src_line
)
855 struct source_line
*iter
;
856 struct rb_node
**p
= &root
->rb_node
;
857 struct rb_node
*parent
= NULL
;
861 iter
= rb_entry(parent
, struct source_line
, node
);
863 if (src_line
->percent
> iter
->percent
)
869 rb_link_node(&src_line
->node
, parent
, p
);
870 rb_insert_color(&src_line
->node
, root
);
873 static void symbol__free_source_line(struct symbol
*sym
, int len
)
875 struct annotation
*notes
= symbol__annotation(sym
);
876 struct source_line
*src_line
= notes
->src
->lines
;
879 for (i
= 0; i
< len
; i
++)
880 free(src_line
[i
].path
);
883 notes
->src
->lines
= NULL
;
886 /* Get the filename:line for the colored entries */
887 static int symbol__get_source_line(struct symbol
*sym
, struct map
*map
,
888 int evidx
, struct rb_root
*root
, int len
,
889 const char *filename
)
893 char cmd
[PATH_MAX
* 2];
894 struct source_line
*src_line
;
895 struct annotation
*notes
= symbol__annotation(sym
);
896 struct sym_hist
*h
= annotation__histogram(notes
, evidx
);
901 src_line
= notes
->src
->lines
= calloc(len
, sizeof(struct source_line
));
902 if (!notes
->src
->lines
)
905 start
= map__rip_2objdump(map
, sym
->start
);
907 for (i
= 0; i
< len
; i
++) {
913 src_line
[i
].percent
= 100.0 * h
->addr
[i
] / h
->sum
;
914 if (src_line
[i
].percent
<= 0.5)
918 sprintf(cmd
, "addr2line -e %s %016" PRIx64
, filename
, offset
);
919 fp
= popen(cmd
, "r");
923 if (getline(&path
, &line_len
, fp
) < 0 || !line_len
)
926 src_line
[i
].path
= malloc(sizeof(char) * line_len
+ 1);
927 if (!src_line
[i
].path
)
930 strcpy(src_line
[i
].path
, path
);
931 insert_source_line(root
, &src_line
[i
]);
940 static void print_summary(struct rb_root
*root
, const char *filename
)
942 struct source_line
*src_line
;
943 struct rb_node
*node
;
945 printf("\nSorted summary for file %s\n", filename
);
946 printf("----------------------------------------------\n\n");
948 if (RB_EMPTY_ROOT(root
)) {
949 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN
);
953 node
= rb_first(root
);
959 src_line
= rb_entry(node
, struct source_line
, node
);
960 percent
= src_line
->percent
;
961 color
= get_percent_color(percent
);
962 path
= src_line
->path
;
964 color_fprintf(stdout
, color
, " %7.2f %s", percent
, path
);
965 node
= rb_next(node
);
969 static void symbol__annotate_hits(struct symbol
*sym
, int evidx
)
971 struct annotation
*notes
= symbol__annotation(sym
);
972 struct sym_hist
*h
= annotation__histogram(notes
, evidx
);
973 u64 len
= symbol__size(sym
), offset
;
975 for (offset
= 0; offset
< len
; ++offset
)
976 if (h
->addr
[offset
] != 0)
977 printf("%*" PRIx64
": %" PRIu64
"\n", BITS_PER_LONG
/ 2,
978 sym
->start
+ offset
, h
->addr
[offset
]);
979 printf("%*s: %" PRIu64
"\n", BITS_PER_LONG
/ 2, "h->sum", h
->sum
);
982 int symbol__annotate_printf(struct symbol
*sym
, struct map
*map
, int evidx
,
983 bool full_paths
, int min_pcnt
, int max_lines
,
986 struct dso
*dso
= map
->dso
;
988 const char *d_filename
;
989 struct annotation
*notes
= symbol__annotation(sym
);
990 struct disasm_line
*pos
, *queue
= NULL
;
991 u64 start
= map__rip_2objdump(map
, sym
->start
);
992 int printed
= 2, queue_len
= 0;
996 filename
= strdup(dso
->long_name
);
1001 d_filename
= filename
;
1003 d_filename
= basename(filename
);
1005 len
= symbol__size(sym
);
1007 printf(" Percent | Source code & Disassembly of %s\n", d_filename
);
1008 printf("------------------------------------------------\n");
1011 symbol__annotate_hits(sym
, evidx
);
1013 list_for_each_entry(pos
, ¬es
->src
->source
, node
) {
1014 if (context
&& queue
== NULL
) {
1019 switch (disasm_line__print(pos
, sym
, start
, evidx
, len
,
1020 min_pcnt
, printed
, max_lines
,
1025 printed
+= queue_len
;
1031 /* filtered by max_lines */
1037 * Filtered by min_pcnt or non IP lines when
1042 if (queue_len
== context
)
1043 queue
= list_entry(queue
->node
.next
, typeof(*queue
), node
);
1055 void symbol__annotate_zero_histogram(struct symbol
*sym
, int evidx
)
1057 struct annotation
*notes
= symbol__annotation(sym
);
1058 struct sym_hist
*h
= annotation__histogram(notes
, evidx
);
1060 memset(h
, 0, notes
->src
->sizeof_sym_hist
);
1063 void symbol__annotate_decay_histogram(struct symbol
*sym
, int evidx
)
1065 struct annotation
*notes
= symbol__annotation(sym
);
1066 struct sym_hist
*h
= annotation__histogram(notes
, evidx
);
1067 int len
= symbol__size(sym
), offset
;
1070 for (offset
= 0; offset
< len
; ++offset
) {
1071 h
->addr
[offset
] = h
->addr
[offset
] * 7 / 8;
1072 h
->sum
+= h
->addr
[offset
];
1076 void disasm__purge(struct list_head
*head
)
1078 struct disasm_line
*pos
, *n
;
1080 list_for_each_entry_safe(pos
, n
, head
, node
) {
1081 list_del(&pos
->node
);
1082 disasm_line__free(pos
);
1086 static size_t disasm_line__fprintf(struct disasm_line
*dl
, FILE *fp
)
1090 if (dl
->offset
== -1)
1091 return fprintf(fp
, "%s\n", dl
->line
);
1093 printed
= fprintf(fp
, "%#" PRIx64
" %s", dl
->offset
, dl
->name
);
1095 if (dl
->ops
.raw
[0] != '\0') {
1096 printed
+= fprintf(fp
, "%.*s %s\n", 6 - (int)printed
, " ",
1100 return printed
+ fprintf(fp
, "\n");
1103 size_t disasm__fprintf(struct list_head
*head
, FILE *fp
)
1105 struct disasm_line
*pos
;
1108 list_for_each_entry(pos
, head
, node
)
1109 printed
+= disasm_line__fprintf(pos
, fp
);
1114 int symbol__tty_annotate(struct symbol
*sym
, struct map
*map
, int evidx
,
1115 bool print_lines
, bool full_paths
, int min_pcnt
,
1118 struct dso
*dso
= map
->dso
;
1119 const char *filename
= dso
->long_name
;
1120 struct rb_root source_line
= RB_ROOT
;
1123 if (symbol__annotate(sym
, map
, 0) < 0)
1126 len
= symbol__size(sym
);
1129 symbol__get_source_line(sym
, map
, evidx
, &source_line
,
1131 print_summary(&source_line
, filename
);
1134 symbol__annotate_printf(sym
, map
, evidx
, full_paths
,
1135 min_pcnt
, max_lines
, 0);
1137 symbol__free_source_line(sym
, len
);
1139 disasm__purge(&symbol__annotation(sym
)->src
->source
);