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 ins__raw_scnprintf(struct ins
*ins
, char *bf
, size_t size
,
22 struct ins_operands
*ops
)
24 return scnprintf(bf
, size
, "%-6.6s %s", ins
->name
, ops
->raw
);
27 int ins__scnprintf(struct ins
*ins
, char *bf
, size_t size
,
28 struct ins_operands
*ops
)
30 if (ins
->ops
->scnprintf
)
31 return ins
->ops
->scnprintf(ins
, bf
, size
, ops
);
33 return ins__raw_scnprintf(ins
, bf
, size
, ops
);
36 static int call__parse(struct ins_operands
*ops
)
38 char *endptr
, *tok
, *name
;
40 ops
->target
.addr
= strtoull(ops
->raw
, &endptr
, 16);
42 name
= strchr(endptr
, '<');
48 tok
= strchr(name
, '>');
53 ops
->target
.name
= strdup(name
);
56 return ops
->target
.name
== NULL
? -1 : 0;
59 tok
= strchr(endptr
, '(');
65 tok
= strchr(endptr
, '*');
69 ops
->target
.addr
= strtoull(tok
+ 1, NULL
, 16);
73 static int call__scnprintf(struct ins
*ins
, char *bf
, size_t size
,
74 struct ins_operands
*ops
)
77 return scnprintf(bf
, size
, "%-6.6s %s", ins
->name
, ops
->target
.name
);
79 if (ops
->target
.addr
== 0)
80 return ins__raw_scnprintf(ins
, bf
, size
, ops
);
82 return scnprintf(bf
, size
, "%-6.6s *%" PRIx64
, ins
->name
, ops
->target
.addr
);
85 static struct ins_ops call_ops
= {
87 .scnprintf
= call__scnprintf
,
90 bool ins__is_call(const struct ins
*ins
)
92 return ins
->ops
== &call_ops
;
95 static int jump__parse(struct ins_operands
*ops
)
97 const char *s
= strchr(ops
->raw
, '+');
99 ops
->target
.addr
= strtoll(ops
->raw
, NULL
, 16);
102 ops
->target
.offset
= strtoll(s
, NULL
, 16);
104 ops
->target
.offset
= UINT64_MAX
;
109 static int jump__scnprintf(struct ins
*ins
, char *bf
, size_t size
,
110 struct ins_operands
*ops
)
112 return scnprintf(bf
, size
, "%-6.6s %" PRIx64
, ins
->name
, ops
->target
.offset
);
115 static struct ins_ops jump_ops
= {
116 .parse
= jump__parse
,
117 .scnprintf
= jump__scnprintf
,
120 bool ins__is_jump(const struct ins
*ins
)
122 return ins
->ops
== &jump_ops
;
125 static int comment__symbol(char *raw
, char *comment
, u64
*addrp
, char **namep
)
127 char *endptr
, *name
, *t
;
129 if (strstr(raw
, "(%rip)") == NULL
)
132 *addrp
= strtoull(comment
, &endptr
, 16);
133 name
= strchr(endptr
, '<');
139 t
= strchr(name
, '>');
144 *namep
= strdup(name
);
150 static int mov__parse(struct ins_operands
*ops
)
152 char *s
= strchr(ops
->raw
, ','), *target
, *comment
, prev
;
158 ops
->source
.raw
= strdup(ops
->raw
);
161 if (ops
->source
.raw
== NULL
)
166 while (s
[0] != '\0' && !isspace(s
[0]))
171 ops
->target
.raw
= strdup(target
);
174 if (ops
->target
.raw
== NULL
)
175 goto out_free_source
;
177 comment
= strchr(s
, '#');
181 while (comment
[0] != '\0' && isspace(comment
[0]))
184 comment__symbol(ops
->source
.raw
, comment
, &ops
->source
.addr
, &ops
->source
.name
);
185 comment__symbol(ops
->target
.raw
, comment
, &ops
->target
.addr
, &ops
->target
.name
);
190 free(ops
->source
.raw
);
191 ops
->source
.raw
= NULL
;
195 static int mov__scnprintf(struct ins
*ins
, char *bf
, size_t size
,
196 struct ins_operands
*ops
)
198 return scnprintf(bf
, size
, "%-6.6s %s,%s", ins
->name
,
199 ops
->source
.name
?: ops
->source
.raw
,
200 ops
->target
.name
?: ops
->target
.raw
);
203 static struct ins_ops mov_ops
= {
205 .scnprintf
= mov__scnprintf
,
208 static int dec__parse(struct ins_operands
*ops
)
210 char *target
, *comment
, *s
, prev
;
212 target
= s
= ops
->raw
;
214 while (s
[0] != '\0' && !isspace(s
[0]))
219 ops
->target
.raw
= strdup(target
);
222 if (ops
->target
.raw
== NULL
)
225 comment
= strchr(s
, '#');
229 while (comment
[0] != '\0' && isspace(comment
[0]))
232 comment__symbol(ops
->target
.raw
, comment
, &ops
->target
.addr
, &ops
->target
.name
);
237 static int dec__scnprintf(struct ins
*ins
, char *bf
, size_t size
,
238 struct ins_operands
*ops
)
240 return scnprintf(bf
, size
, "%-6.6s %s", ins
->name
,
241 ops
->target
.name
?: ops
->target
.raw
);
244 static struct ins_ops dec_ops
= {
246 .scnprintf
= dec__scnprintf
,
249 static int nop__scnprintf(struct ins
*ins __used
, char *bf
, size_t size
,
250 struct ins_operands
*ops __used
)
252 return scnprintf(bf
, size
, "%-6.6s", "nop");
255 static struct ins_ops nop_ops
= {
256 .scnprintf
= nop__scnprintf
,
260 * Must be sorted by name!
262 static struct ins instructions
[] = {
263 { .name
= "add", .ops
= &mov_ops
, },
264 { .name
= "addl", .ops
= &mov_ops
, },
265 { .name
= "addq", .ops
= &mov_ops
, },
266 { .name
= "addw", .ops
= &mov_ops
, },
267 { .name
= "and", .ops
= &mov_ops
, },
268 { .name
= "call", .ops
= &call_ops
, },
269 { .name
= "callq", .ops
= &call_ops
, },
270 { .name
= "cmp", .ops
= &mov_ops
, },
271 { .name
= "cmpb", .ops
= &mov_ops
, },
272 { .name
= "cmpl", .ops
= &mov_ops
, },
273 { .name
= "cmpq", .ops
= &mov_ops
, },
274 { .name
= "cmpw", .ops
= &mov_ops
, },
275 { .name
= "cmpxch", .ops
= &mov_ops
, },
276 { .name
= "dec", .ops
= &dec_ops
, },
277 { .name
= "decl", .ops
= &dec_ops
, },
278 { .name
= "imul", .ops
= &mov_ops
, },
279 { .name
= "inc", .ops
= &dec_ops
, },
280 { .name
= "incl", .ops
= &dec_ops
, },
281 { .name
= "ja", .ops
= &jump_ops
, },
282 { .name
= "jae", .ops
= &jump_ops
, },
283 { .name
= "jb", .ops
= &jump_ops
, },
284 { .name
= "jbe", .ops
= &jump_ops
, },
285 { .name
= "jc", .ops
= &jump_ops
, },
286 { .name
= "jcxz", .ops
= &jump_ops
, },
287 { .name
= "je", .ops
= &jump_ops
, },
288 { .name
= "jecxz", .ops
= &jump_ops
, },
289 { .name
= "jg", .ops
= &jump_ops
, },
290 { .name
= "jge", .ops
= &jump_ops
, },
291 { .name
= "jl", .ops
= &jump_ops
, },
292 { .name
= "jle", .ops
= &jump_ops
, },
293 { .name
= "jmp", .ops
= &jump_ops
, },
294 { .name
= "jmpq", .ops
= &jump_ops
, },
295 { .name
= "jna", .ops
= &jump_ops
, },
296 { .name
= "jnae", .ops
= &jump_ops
, },
297 { .name
= "jnb", .ops
= &jump_ops
, },
298 { .name
= "jnbe", .ops
= &jump_ops
, },
299 { .name
= "jnc", .ops
= &jump_ops
, },
300 { .name
= "jne", .ops
= &jump_ops
, },
301 { .name
= "jng", .ops
= &jump_ops
, },
302 { .name
= "jnge", .ops
= &jump_ops
, },
303 { .name
= "jnl", .ops
= &jump_ops
, },
304 { .name
= "jnle", .ops
= &jump_ops
, },
305 { .name
= "jno", .ops
= &jump_ops
, },
306 { .name
= "jnp", .ops
= &jump_ops
, },
307 { .name
= "jns", .ops
= &jump_ops
, },
308 { .name
= "jnz", .ops
= &jump_ops
, },
309 { .name
= "jo", .ops
= &jump_ops
, },
310 { .name
= "jp", .ops
= &jump_ops
, },
311 { .name
= "jpe", .ops
= &jump_ops
, },
312 { .name
= "jpo", .ops
= &jump_ops
, },
313 { .name
= "jrcxz", .ops
= &jump_ops
, },
314 { .name
= "js", .ops
= &jump_ops
, },
315 { .name
= "jz", .ops
= &jump_ops
, },
316 { .name
= "lea", .ops
= &mov_ops
, },
317 { .name
= "mov", .ops
= &mov_ops
, },
318 { .name
= "movb", .ops
= &mov_ops
, },
319 { .name
= "movdqa",.ops
= &mov_ops
, },
320 { .name
= "movl", .ops
= &mov_ops
, },
321 { .name
= "movq", .ops
= &mov_ops
, },
322 { .name
= "movslq", .ops
= &mov_ops
, },
323 { .name
= "movzbl", .ops
= &mov_ops
, },
324 { .name
= "movzwl", .ops
= &mov_ops
, },
325 { .name
= "nop", .ops
= &nop_ops
, },
326 { .name
= "nopl", .ops
= &nop_ops
, },
327 { .name
= "nopw", .ops
= &nop_ops
, },
328 { .name
= "or", .ops
= &mov_ops
, },
329 { .name
= "orl", .ops
= &mov_ops
, },
330 { .name
= "test", .ops
= &mov_ops
, },
331 { .name
= "testb", .ops
= &mov_ops
, },
332 { .name
= "testl", .ops
= &mov_ops
, },
335 static int ins__cmp(const void *name
, const void *insp
)
337 const struct ins
*ins
= insp
;
339 return strcmp(name
, ins
->name
);
342 static struct ins
*ins__find(const char *name
)
344 const int nmemb
= ARRAY_SIZE(instructions
);
346 return bsearch(name
, instructions
, nmemb
, sizeof(struct ins
), ins__cmp
);
349 int symbol__annotate_init(struct map
*map __used
, struct symbol
*sym
)
351 struct annotation
*notes
= symbol__annotation(sym
);
352 pthread_mutex_init(¬es
->lock
, NULL
);
356 int symbol__alloc_hist(struct symbol
*sym
)
358 struct annotation
*notes
= symbol__annotation(sym
);
359 const size_t size
= symbol__size(sym
);
360 size_t sizeof_sym_hist
= (sizeof(struct sym_hist
) + size
* sizeof(u64
));
362 notes
->src
= zalloc(sizeof(*notes
->src
) + symbol_conf
.nr_events
* sizeof_sym_hist
);
363 if (notes
->src
== NULL
)
365 notes
->src
->sizeof_sym_hist
= sizeof_sym_hist
;
366 notes
->src
->nr_histograms
= symbol_conf
.nr_events
;
367 INIT_LIST_HEAD(¬es
->src
->source
);
371 void symbol__annotate_zero_histograms(struct symbol
*sym
)
373 struct annotation
*notes
= symbol__annotation(sym
);
375 pthread_mutex_lock(¬es
->lock
);
376 if (notes
->src
!= NULL
)
377 memset(notes
->src
->histograms
, 0,
378 notes
->src
->nr_histograms
* notes
->src
->sizeof_sym_hist
);
379 pthread_mutex_unlock(¬es
->lock
);
382 int symbol__inc_addr_samples(struct symbol
*sym
, struct map
*map
,
386 struct annotation
*notes
;
389 notes
= symbol__annotation(sym
);
390 if (notes
->src
== NULL
)
393 pr_debug3("%s: addr=%#" PRIx64
"\n", __func__
, map
->unmap_ip(map
, addr
));
395 if (addr
< sym
->start
|| addr
> sym
->end
)
398 offset
= addr
- sym
->start
;
399 h
= annotation__histogram(notes
, evidx
);
403 pr_debug3("%#" PRIx64
" %s: period++ [addr: %#" PRIx64
", %#" PRIx64
404 ", evidx=%d] => %" PRIu64
"\n", sym
->start
, sym
->name
,
405 addr
, addr
- sym
->start
, evidx
, h
->addr
[offset
]);
409 static void disasm_line__init_ins(struct disasm_line
*dl
)
411 dl
->ins
= ins__find(dl
->name
);
419 if (dl
->ins
->ops
->parse
)
420 dl
->ins
->ops
->parse(&dl
->ops
);
423 static struct disasm_line
*disasm_line__new(s64 offset
, char *line
, size_t privsize
)
425 struct disasm_line
*dl
= zalloc(sizeof(*dl
) + privsize
);
429 dl
->line
= strdup(line
);
430 if (dl
->line
== NULL
)
434 char *name
= dl
->line
, tmp
;
436 while (isspace(name
[0]))
442 dl
->ops
.raw
= name
+ 1;
444 while (dl
->ops
.raw
[0] != '\0' &&
445 !isspace(dl
->ops
.raw
[0]))
448 tmp
= dl
->ops
.raw
[0];
449 dl
->ops
.raw
[0] = '\0';
450 dl
->name
= strdup(name
);
452 if (dl
->name
== NULL
)
455 dl
->ops
.raw
[0] = tmp
;
457 if (dl
->ops
.raw
[0] != '\0') {
459 while (isspace(dl
->ops
.raw
[0]))
463 disasm_line__init_ins(dl
);
476 void disasm_line__free(struct disasm_line
*dl
)
480 free(dl
->ops
.source
.raw
);
481 free(dl
->ops
.source
.name
);
482 free(dl
->ops
.target
.raw
);
483 free(dl
->ops
.target
.name
);
487 int disasm_line__scnprintf(struct disasm_line
*dl
, char *bf
, size_t size
, bool raw
)
490 return scnprintf(bf
, size
, "%-6.6s %s", dl
->name
, dl
->ops
.raw
);
492 return ins__scnprintf(dl
->ins
, bf
, size
, &dl
->ops
);
495 static void disasm__add(struct list_head
*head
, struct disasm_line
*line
)
497 list_add_tail(&line
->node
, head
);
500 struct disasm_line
*disasm__get_next_ip_line(struct list_head
*head
, struct disasm_line
*pos
)
502 list_for_each_entry_continue(pos
, head
, node
)
503 if (pos
->offset
>= 0)
509 static int disasm_line__print(struct disasm_line
*dl
, struct symbol
*sym
, u64 start
,
510 int evidx
, u64 len
, int min_pcnt
, int printed
,
511 int max_lines
, struct disasm_line
*queue
)
513 static const char *prev_line
;
514 static const char *prev_color
;
516 if (dl
->offset
!= -1) {
517 const char *path
= NULL
;
518 unsigned int hits
= 0;
519 double percent
= 0.0;
521 struct annotation
*notes
= symbol__annotation(sym
);
522 struct source_line
*src_line
= notes
->src
->lines
;
523 struct sym_hist
*h
= annotation__histogram(notes
, evidx
);
524 s64 offset
= dl
->offset
;
525 const u64 addr
= start
+ offset
;
526 struct disasm_line
*next
;
528 next
= disasm__get_next_ip_line(¬es
->src
->source
, dl
);
530 while (offset
< (s64
)len
&&
531 (next
== NULL
|| offset
< next
->offset
)) {
534 path
= src_line
[offset
].path
;
535 percent
+= src_line
[offset
].percent
;
537 hits
+= h
->addr
[offset
];
542 if (src_line
== NULL
&& h
->sum
)
543 percent
= 100.0 * hits
/ h
->sum
;
545 if (percent
< min_pcnt
)
548 if (max_lines
&& printed
>= max_lines
)
552 list_for_each_entry_from(queue
, ¬es
->src
->source
, node
) {
555 disasm_line__print(queue
, sym
, start
, evidx
, len
,
560 color
= get_percent_color(percent
);
563 * Also color the filename and line if needed, with
564 * the same color than the percentage. Don't print it
565 * twice for close colored addr with the same filename:line
568 if (!prev_line
|| strcmp(prev_line
, path
)
569 || color
!= prev_color
) {
570 color_fprintf(stdout
, color
, " %s", path
);
576 color_fprintf(stdout
, color
, " %7.2f", percent
);
578 color_fprintf(stdout
, PERF_COLOR_MAGENTA
, " %" PRIx64
":", addr
);
579 color_fprintf(stdout
, PERF_COLOR_BLUE
, "%s\n", dl
->line
);
580 } else if (max_lines
&& printed
>= max_lines
)
589 printf(" : %s\n", dl
->line
);
595 static int symbol__parse_objdump_line(struct symbol
*sym
, struct map
*map
,
596 FILE *file
, size_t privsize
)
598 struct annotation
*notes
= symbol__annotation(sym
);
599 struct disasm_line
*dl
;
600 char *line
= NULL
, *parsed_line
, *tmp
, *tmp2
, *c
;
602 s64 line_ip
, offset
= -1;
604 if (getline(&line
, &line_len
, file
) < 0)
610 while (line_len
!= 0 && isspace(line
[line_len
- 1]))
611 line
[--line_len
] = '\0';
613 c
= strchr(line
, '\n');
621 * Strip leading spaces:
632 * Parse hexa addresses followed by ':'
634 line_ip
= strtoull(tmp
, &tmp2
, 16);
635 if (*tmp2
!= ':' || tmp
== tmp2
|| tmp2
[1] == '\0')
640 u64 start
= map__rip_2objdump(map
, sym
->start
),
641 end
= map__rip_2objdump(map
, sym
->end
);
643 offset
= line_ip
- start
;
644 if (offset
< 0 || (u64
)line_ip
> end
)
647 parsed_line
= tmp2
+ 1;
650 dl
= disasm_line__new(offset
, parsed_line
, privsize
);
656 disasm__add(¬es
->src
->source
, dl
);
661 int symbol__annotate(struct symbol
*sym
, struct map
*map
, size_t privsize
)
663 struct dso
*dso
= map
->dso
;
664 char *filename
= dso__build_id_filename(dso
, NULL
, 0);
665 bool free_filename
= true;
666 char command
[PATH_MAX
* 2];
669 char symfs_filename
[PATH_MAX
];
672 snprintf(symfs_filename
, sizeof(symfs_filename
), "%s%s",
673 symbol_conf
.symfs
, filename
);
676 if (filename
== NULL
) {
677 if (dso
->has_build_id
) {
678 pr_err("Can't annotate %s: not enough memory\n",
683 } else if (readlink(symfs_filename
, command
, sizeof(command
)) < 0 ||
684 strstr(command
, "[kernel.kallsyms]") ||
685 access(symfs_filename
, R_OK
)) {
689 * If we don't have build-ids or the build-id file isn't in the
690 * cache, or is just a kallsyms file, well, lets hope that this
691 * DSO is the same as when 'perf record' ran.
693 filename
= dso
->long_name
;
694 snprintf(symfs_filename
, sizeof(symfs_filename
), "%s%s",
695 symbol_conf
.symfs
, filename
);
696 free_filename
= false;
699 if (dso
->symtab_type
== SYMTAB__KALLSYMS
) {
700 char bf
[BUILD_ID_SIZE
* 2 + 16] = " with build id ";
701 char *build_id_msg
= NULL
;
703 if (dso
->annotate_warned
)
704 goto out_free_filename
;
706 if (dso
->has_build_id
) {
707 build_id__sprintf(dso
->build_id
,
708 sizeof(dso
->build_id
), bf
+ 15);
712 dso
->annotate_warned
= 1;
713 pr_err("Can't annotate %s:\n\n"
714 "No vmlinux file%s\nwas found in the path.\n\n"
716 " perf buildid-cache -av vmlinux\n\n"
718 " --vmlinux vmlinux\n",
719 sym
->name
, build_id_msg
?: "");
720 goto out_free_filename
;
723 pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64
", end=%#" PRIx64
"\n", __func__
,
724 filename
, sym
->name
, map
->unmap_ip(map
, sym
->start
),
725 map
->unmap_ip(map
, sym
->end
));
727 pr_debug("annotating [%p] %30s : [%p] %30s\n",
728 dso
, dso
->long_name
, sym
, sym
->name
);
730 snprintf(command
, sizeof(command
),
731 "objdump %s%s --start-address=0x%016" PRIx64
732 " --stop-address=0x%016" PRIx64
733 " -d %s %s -C %s|grep -v %s|expand",
734 disassembler_style
? "-M " : "",
735 disassembler_style
? disassembler_style
: "",
736 map__rip_2objdump(map
, sym
->start
),
737 map__rip_2objdump(map
, sym
->end
+1),
738 symbol_conf
.annotate_asm_raw
? "" : "--no-show-raw",
739 symbol_conf
.annotate_src
? "-S" : "",
740 symfs_filename
, filename
);
742 pr_debug("Executing: %s\n", command
);
744 file
= popen(command
, "r");
746 goto out_free_filename
;
749 if (symbol__parse_objdump_line(sym
, map
, file
, privsize
) < 0)
759 static void insert_source_line(struct rb_root
*root
, struct source_line
*src_line
)
761 struct source_line
*iter
;
762 struct rb_node
**p
= &root
->rb_node
;
763 struct rb_node
*parent
= NULL
;
767 iter
= rb_entry(parent
, struct source_line
, node
);
769 if (src_line
->percent
> iter
->percent
)
775 rb_link_node(&src_line
->node
, parent
, p
);
776 rb_insert_color(&src_line
->node
, root
);
779 static void symbol__free_source_line(struct symbol
*sym
, int len
)
781 struct annotation
*notes
= symbol__annotation(sym
);
782 struct source_line
*src_line
= notes
->src
->lines
;
785 for (i
= 0; i
< len
; i
++)
786 free(src_line
[i
].path
);
789 notes
->src
->lines
= NULL
;
792 /* Get the filename:line for the colored entries */
793 static int symbol__get_source_line(struct symbol
*sym
, struct map
*map
,
794 int evidx
, struct rb_root
*root
, int len
,
795 const char *filename
)
799 char cmd
[PATH_MAX
* 2];
800 struct source_line
*src_line
;
801 struct annotation
*notes
= symbol__annotation(sym
);
802 struct sym_hist
*h
= annotation__histogram(notes
, evidx
);
807 src_line
= notes
->src
->lines
= calloc(len
, sizeof(struct source_line
));
808 if (!notes
->src
->lines
)
811 start
= map__rip_2objdump(map
, sym
->start
);
813 for (i
= 0; i
< len
; i
++) {
819 src_line
[i
].percent
= 100.0 * h
->addr
[i
] / h
->sum
;
820 if (src_line
[i
].percent
<= 0.5)
824 sprintf(cmd
, "addr2line -e %s %016" PRIx64
, filename
, offset
);
825 fp
= popen(cmd
, "r");
829 if (getline(&path
, &line_len
, fp
) < 0 || !line_len
)
832 src_line
[i
].path
= malloc(sizeof(char) * line_len
+ 1);
833 if (!src_line
[i
].path
)
836 strcpy(src_line
[i
].path
, path
);
837 insert_source_line(root
, &src_line
[i
]);
846 static void print_summary(struct rb_root
*root
, const char *filename
)
848 struct source_line
*src_line
;
849 struct rb_node
*node
;
851 printf("\nSorted summary for file %s\n", filename
);
852 printf("----------------------------------------------\n\n");
854 if (RB_EMPTY_ROOT(root
)) {
855 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN
);
859 node
= rb_first(root
);
865 src_line
= rb_entry(node
, struct source_line
, node
);
866 percent
= src_line
->percent
;
867 color
= get_percent_color(percent
);
868 path
= src_line
->path
;
870 color_fprintf(stdout
, color
, " %7.2f %s", percent
, path
);
871 node
= rb_next(node
);
875 static void symbol__annotate_hits(struct symbol
*sym
, int evidx
)
877 struct annotation
*notes
= symbol__annotation(sym
);
878 struct sym_hist
*h
= annotation__histogram(notes
, evidx
);
879 u64 len
= symbol__size(sym
), offset
;
881 for (offset
= 0; offset
< len
; ++offset
)
882 if (h
->addr
[offset
] != 0)
883 printf("%*" PRIx64
": %" PRIu64
"\n", BITS_PER_LONG
/ 2,
884 sym
->start
+ offset
, h
->addr
[offset
]);
885 printf("%*s: %" PRIu64
"\n", BITS_PER_LONG
/ 2, "h->sum", h
->sum
);
888 int symbol__annotate_printf(struct symbol
*sym
, struct map
*map
, int evidx
,
889 bool full_paths
, int min_pcnt
, int max_lines
,
892 struct dso
*dso
= map
->dso
;
893 const char *filename
= dso
->long_name
, *d_filename
;
894 struct annotation
*notes
= symbol__annotation(sym
);
895 struct disasm_line
*pos
, *queue
= NULL
;
896 u64 start
= map__rip_2objdump(map
, sym
->start
);
897 int printed
= 2, queue_len
= 0;
902 d_filename
= filename
;
904 d_filename
= basename(filename
);
906 len
= symbol__size(sym
);
908 printf(" Percent | Source code & Disassembly of %s\n", d_filename
);
909 printf("------------------------------------------------\n");
912 symbol__annotate_hits(sym
, evidx
);
914 list_for_each_entry(pos
, ¬es
->src
->source
, node
) {
915 if (context
&& queue
== NULL
) {
920 switch (disasm_line__print(pos
, sym
, start
, evidx
, len
,
921 min_pcnt
, printed
, max_lines
,
926 printed
+= queue_len
;
932 /* filtered by max_lines */
938 * Filtered by min_pcnt or non IP lines when
943 if (queue_len
== context
)
944 queue
= list_entry(queue
->node
.next
, typeof(*queue
), node
);
954 void symbol__annotate_zero_histogram(struct symbol
*sym
, int evidx
)
956 struct annotation
*notes
= symbol__annotation(sym
);
957 struct sym_hist
*h
= annotation__histogram(notes
, evidx
);
959 memset(h
, 0, notes
->src
->sizeof_sym_hist
);
962 void symbol__annotate_decay_histogram(struct symbol
*sym
, int evidx
)
964 struct annotation
*notes
= symbol__annotation(sym
);
965 struct sym_hist
*h
= annotation__histogram(notes
, evidx
);
966 int len
= symbol__size(sym
), offset
;
969 for (offset
= 0; offset
< len
; ++offset
) {
970 h
->addr
[offset
] = h
->addr
[offset
] * 7 / 8;
971 h
->sum
+= h
->addr
[offset
];
975 void disasm__purge(struct list_head
*head
)
977 struct disasm_line
*pos
, *n
;
979 list_for_each_entry_safe(pos
, n
, head
, node
) {
980 list_del(&pos
->node
);
981 disasm_line__free(pos
);
985 static size_t disasm_line__fprintf(struct disasm_line
*dl
, FILE *fp
)
989 if (dl
->offset
== -1)
990 return fprintf(fp
, "%s\n", dl
->line
);
992 printed
= fprintf(fp
, "%#" PRIx64
" %s", dl
->offset
, dl
->name
);
994 if (dl
->ops
.raw
[0] != '\0') {
995 printed
+= fprintf(fp
, "%.*s %s\n", 6 - (int)printed
, " ",
999 return printed
+ fprintf(fp
, "\n");
1002 size_t disasm__fprintf(struct list_head
*head
, FILE *fp
)
1004 struct disasm_line
*pos
;
1007 list_for_each_entry(pos
, head
, node
)
1008 printed
+= disasm_line__fprintf(pos
, fp
);
1013 int symbol__tty_annotate(struct symbol
*sym
, struct map
*map
, int evidx
,
1014 bool print_lines
, bool full_paths
, int min_pcnt
,
1017 struct dso
*dso
= map
->dso
;
1018 const char *filename
= dso
->long_name
;
1019 struct rb_root source_line
= RB_ROOT
;
1022 if (symbol__annotate(sym
, map
, 0) < 0)
1025 len
= symbol__size(sym
);
1028 symbol__get_source_line(sym
, map
, evidx
, &source_line
,
1030 print_summary(&source_line
, filename
);
1033 symbol__annotate_printf(sym
, map
, evidx
, full_paths
,
1034 min_pcnt
, max_lines
, 0);
1036 symbol__free_source_line(sym
, len
);
1038 disasm__purge(&symbol__annotation(sym
)->src
->source
);