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)
22 #include "block-range.h"
24 #include "arch/common.h"
27 #include <linux/bitops.h>
28 #include <linux/kernel.h>
30 #include "sane_ctype.h"
32 const char *disassembler_style;
33 const char *objdump_path;
34 static regex_t file_lineno;
36 static struct ins_ops *ins__find(struct arch *arch, const char *name);
37 static void ins__sort(struct arch *arch);
38 static int disasm_line__parse(char *line, const char **namep, char **rawp);
42 struct ins *instructions;
43 size_t nr_instructions;
44 size_t nr_instructions_allocated;
45 struct ins_ops *(*associate_instruction_ops)(struct arch *arch, const char *name);
46 bool sorted_instructions;
51 int (*init)(struct arch *arch, char *cpuid);
52 bool (*ins_is_fused)(struct arch *arch, const char *ins1,
56 char skip_functions_char;
60 static struct ins_ops call_ops;
61 static struct ins_ops dec_ops;
62 static struct ins_ops jump_ops;
63 static struct ins_ops mov_ops;
64 static struct ins_ops nop_ops;
65 static struct ins_ops lock_ops;
66 static struct ins_ops ret_ops;
68 static int arch__grow_instructions(struct arch *arch)
70 struct ins *new_instructions;
71 size_t new_nr_allocated;
73 if (arch->nr_instructions_allocated == 0 && arch->instructions)
74 goto grow_from_non_allocated_table;
76 new_nr_allocated = arch->nr_instructions_allocated + 128;
77 new_instructions = realloc(arch->instructions, new_nr_allocated * sizeof(struct ins));
78 if (new_instructions == NULL)
81 out_update_instructions:
82 arch->instructions = new_instructions;
83 arch->nr_instructions_allocated = new_nr_allocated;
86 grow_from_non_allocated_table:
87 new_nr_allocated = arch->nr_instructions + 128;
88 new_instructions = calloc(new_nr_allocated, sizeof(struct ins));
89 if (new_instructions == NULL)
92 memcpy(new_instructions, arch->instructions, arch->nr_instructions);
93 goto out_update_instructions;
96 static int arch__associate_ins_ops(struct arch* arch, const char *name, struct ins_ops *ops)
100 if (arch->nr_instructions == arch->nr_instructions_allocated &&
101 arch__grow_instructions(arch))
104 ins = &arch->instructions[arch->nr_instructions];
105 ins->name = strdup(name);
110 arch->nr_instructions++;
116 #include "arch/arm/annotate/instructions.c"
117 #include "arch/arm64/annotate/instructions.c"
118 #include "arch/x86/annotate/instructions.c"
119 #include "arch/powerpc/annotate/instructions.c"
120 #include "arch/s390/annotate/instructions.c"
122 static struct arch architectures[] = {
125 .init = arm__annotate_init,
129 .init = arm64__annotate_init,
133 .init = x86__annotate_init,
134 .instructions = x86__instructions,
135 .nr_instructions = ARRAY_SIZE(x86__instructions),
136 .ins_is_fused = x86__ins_is_fused,
143 .init = powerpc__annotate_init,
147 .init = s390__annotate_init,
154 static void ins__delete(struct ins_operands *ops)
158 zfree(&ops->source.raw);
159 zfree(&ops->source.name);
160 zfree(&ops->target.raw);
161 zfree(&ops->target.name);
164 static int ins__raw_scnprintf(struct ins *ins, char *bf, size_t size,
165 struct ins_operands *ops)
167 return scnprintf(bf, size, "%-6s %s", ins->name, ops->raw);
170 int ins__scnprintf(struct ins *ins, char *bf, size_t size,
171 struct ins_operands *ops)
173 if (ins->ops->scnprintf)
174 return ins->ops->scnprintf(ins, bf, size, ops);
176 return ins__raw_scnprintf(ins, bf, size, ops);
179 bool ins__is_fused(struct arch *arch, const char *ins1, const char *ins2)
181 if (!arch || !arch->ins_is_fused)
184 return arch->ins_is_fused(arch, ins1, ins2);
187 static int call__parse(struct arch *arch, struct ins_operands *ops, struct map *map)
189 char *endptr, *tok, *name;
190 struct addr_map_symbol target = {
194 ops->target.addr = strtoull(ops->raw, &endptr, 16);
196 name = strchr(endptr, '<');
202 if (arch->objdump.skip_functions_char &&
203 strchr(name, arch->objdump.skip_functions_char))
206 tok = strchr(name, '>');
211 ops->target.name = strdup(name);
214 if (ops->target.name == NULL)
217 target.addr = map__objdump_2mem(map, ops->target.addr);
219 if (map_groups__find_ams(&target) == 0 &&
220 map__rip_2objdump(target.map, map->map_ip(target.map, target.addr)) == ops->target.addr)
221 ops->target.sym = target.sym;
226 tok = strchr(endptr, '*');
228 ops->target.addr = strtoull(tok + 1, NULL, 16);
232 static int call__scnprintf(struct ins *ins, char *bf, size_t size,
233 struct ins_operands *ops)
236 return scnprintf(bf, size, "%-6s %s", ins->name, ops->target.sym->name);
238 if (ops->target.addr == 0)
239 return ins__raw_scnprintf(ins, bf, size, ops);
241 return scnprintf(bf, size, "%-6s *%" PRIx64, ins->name, ops->target.addr);
244 static struct ins_ops call_ops = {
245 .parse = call__parse,
246 .scnprintf = call__scnprintf,
249 bool ins__is_call(const struct ins *ins)
251 return ins->ops == &call_ops;
254 static int jump__parse(struct arch *arch __maybe_unused, struct ins_operands *ops, struct map *map __maybe_unused)
256 const char *s = strchr(ops->raw, '+');
257 const char *c = strchr(ops->raw, ',');
260 * skip over possible up to 2 operands to get to address, e.g.:
261 * tbnz w0, #26, ffff0000083cd190 <security_file_permission+0xd0>
264 ops->target.addr = strtoull(c, NULL, 16);
265 if (!ops->target.addr) {
268 ops->target.addr = strtoull(c, NULL, 16);
271 ops->target.addr = strtoull(ops->raw, NULL, 16);
275 ops->target.offset = strtoull(s, NULL, 16);
276 ops->target.offset_avail = true;
278 ops->target.offset_avail = false;
284 static int jump__scnprintf(struct ins *ins, char *bf, size_t size,
285 struct ins_operands *ops)
287 const char *c = strchr(ops->raw, ',');
289 if (!ops->target.addr || ops->target.offset < 0)
290 return ins__raw_scnprintf(ins, bf, size, ops);
293 const char *c2 = strchr(c + 1, ',');
295 /* check for 3-op insn */
300 /* mirror arch objdump's space-after-comma style */
305 return scnprintf(bf, size, "%-6s %.*s%" PRIx64,
306 ins->name, c ? c - ops->raw : 0, ops->raw,
310 static struct ins_ops jump_ops = {
311 .parse = jump__parse,
312 .scnprintf = jump__scnprintf,
315 bool ins__is_jump(const struct ins *ins)
317 return ins->ops == &jump_ops;
320 static int comment__symbol(char *raw, char *comment, u64 *addrp, char **namep)
322 char *endptr, *name, *t;
324 if (strstr(raw, "(%rip)") == NULL)
327 *addrp = strtoull(comment, &endptr, 16);
328 if (endptr == comment)
330 name = strchr(endptr, '<');
336 t = strchr(name, '>');
341 *namep = strdup(name);
347 static int lock__parse(struct arch *arch, struct ins_operands *ops, struct map *map)
349 ops->locked.ops = zalloc(sizeof(*ops->locked.ops));
350 if (ops->locked.ops == NULL)
353 if (disasm_line__parse(ops->raw, &ops->locked.ins.name, &ops->locked.ops->raw) < 0)
356 ops->locked.ins.ops = ins__find(arch, ops->locked.ins.name);
358 if (ops->locked.ins.ops == NULL)
361 if (ops->locked.ins.ops->parse &&
362 ops->locked.ins.ops->parse(arch, ops->locked.ops, map) < 0)
368 zfree(&ops->locked.ops);
372 static int lock__scnprintf(struct ins *ins, char *bf, size_t size,
373 struct ins_operands *ops)
377 if (ops->locked.ins.ops == NULL)
378 return ins__raw_scnprintf(ins, bf, size, ops);
380 printed = scnprintf(bf, size, "%-6s ", ins->name);
381 return printed + ins__scnprintf(&ops->locked.ins, bf + printed,
382 size - printed, ops->locked.ops);
385 static void lock__delete(struct ins_operands *ops)
387 struct ins *ins = &ops->locked.ins;
389 if (ins->ops && ins->ops->free)
390 ins->ops->free(ops->locked.ops);
392 ins__delete(ops->locked.ops);
394 zfree(&ops->locked.ops);
395 zfree(&ops->target.raw);
396 zfree(&ops->target.name);
399 static struct ins_ops lock_ops = {
400 .free = lock__delete,
401 .parse = lock__parse,
402 .scnprintf = lock__scnprintf,
405 static int mov__parse(struct arch *arch, struct ins_operands *ops, struct map *map __maybe_unused)
407 char *s = strchr(ops->raw, ','), *target, *comment, prev;
413 ops->source.raw = strdup(ops->raw);
416 if (ops->source.raw == NULL)
420 comment = strchr(s, arch->objdump.comment_char);
425 s = strchr(s, '\0') - 1;
427 while (s > target && isspace(s[0]))
433 ops->target.raw = strdup(target);
436 if (ops->target.raw == NULL)
437 goto out_free_source;
442 comment = ltrim(comment);
443 comment__symbol(ops->source.raw, comment + 1, &ops->source.addr, &ops->source.name);
444 comment__symbol(ops->target.raw, comment + 1, &ops->target.addr, &ops->target.name);
449 zfree(&ops->source.raw);
453 static int mov__scnprintf(struct ins *ins, char *bf, size_t size,
454 struct ins_operands *ops)
456 return scnprintf(bf, size, "%-6s %s,%s", ins->name,
457 ops->source.name ?: ops->source.raw,
458 ops->target.name ?: ops->target.raw);
461 static struct ins_ops mov_ops = {
463 .scnprintf = mov__scnprintf,
466 static int dec__parse(struct arch *arch __maybe_unused, struct ins_operands *ops, struct map *map __maybe_unused)
468 char *target, *comment, *s, prev;
470 target = s = ops->raw;
472 while (s[0] != '\0' && !isspace(s[0]))
477 ops->target.raw = strdup(target);
480 if (ops->target.raw == NULL)
483 comment = strchr(s, arch->objdump.comment_char);
487 comment = ltrim(comment);
488 comment__symbol(ops->target.raw, comment + 1, &ops->target.addr, &ops->target.name);
493 static int dec__scnprintf(struct ins *ins, char *bf, size_t size,
494 struct ins_operands *ops)
496 return scnprintf(bf, size, "%-6s %s", ins->name,
497 ops->target.name ?: ops->target.raw);
500 static struct ins_ops dec_ops = {
502 .scnprintf = dec__scnprintf,
505 static int nop__scnprintf(struct ins *ins __maybe_unused, char *bf, size_t size,
506 struct ins_operands *ops __maybe_unused)
508 return scnprintf(bf, size, "%-6s", "nop");
511 static struct ins_ops nop_ops = {
512 .scnprintf = nop__scnprintf,
515 static struct ins_ops ret_ops = {
516 .scnprintf = ins__raw_scnprintf,
519 bool ins__is_ret(const struct ins *ins)
521 return ins->ops == &ret_ops;
524 bool ins__is_lock(const struct ins *ins)
526 return ins->ops == &lock_ops;
529 static int ins__key_cmp(const void *name, const void *insp)
531 const struct ins *ins = insp;
533 return strcmp(name, ins->name);
536 static int ins__cmp(const void *a, const void *b)
538 const struct ins *ia = a;
539 const struct ins *ib = b;
541 return strcmp(ia->name, ib->name);
544 static void ins__sort(struct arch *arch)
546 const int nmemb = arch->nr_instructions;
548 qsort(arch->instructions, nmemb, sizeof(struct ins), ins__cmp);
551 static struct ins_ops *__ins__find(struct arch *arch, const char *name)
554 const int nmemb = arch->nr_instructions;
556 if (!arch->sorted_instructions) {
558 arch->sorted_instructions = true;
561 ins = bsearch(name, arch->instructions, nmemb, sizeof(struct ins), ins__key_cmp);
562 return ins ? ins->ops : NULL;
565 static struct ins_ops *ins__find(struct arch *arch, const char *name)
567 struct ins_ops *ops = __ins__find(arch, name);
569 if (!ops && arch->associate_instruction_ops)
570 ops = arch->associate_instruction_ops(arch, name);
575 static int arch__key_cmp(const void *name, const void *archp)
577 const struct arch *arch = archp;
579 return strcmp(name, arch->name);
582 static int arch__cmp(const void *a, const void *b)
584 const struct arch *aa = a;
585 const struct arch *ab = b;
587 return strcmp(aa->name, ab->name);
590 static void arch__sort(void)
592 const int nmemb = ARRAY_SIZE(architectures);
594 qsort(architectures, nmemb, sizeof(struct arch), arch__cmp);
597 static struct arch *arch__find(const char *name)
599 const int nmemb = ARRAY_SIZE(architectures);
607 return bsearch(name, architectures, nmemb, sizeof(struct arch), arch__key_cmp);
610 int symbol__alloc_hist(struct symbol *sym)
612 struct annotation *notes = symbol__annotation(sym);
613 size_t size = symbol__size(sym);
614 size_t sizeof_sym_hist;
617 * Add buffer of one element for zero length symbol.
618 * When sample is taken from first instruction of
619 * zero length symbol, perf still resolves it and
620 * shows symbol name in perf report and allows to
626 /* Check for overflow when calculating sizeof_sym_hist */
627 if (size > (SIZE_MAX - sizeof(struct sym_hist)) / sizeof(struct sym_hist_entry))
630 sizeof_sym_hist = (sizeof(struct sym_hist) + size * sizeof(struct sym_hist_entry));
632 /* Check for overflow in zalloc argument */
633 if (sizeof_sym_hist > (SIZE_MAX - sizeof(*notes->src))
634 / symbol_conf.nr_events)
637 notes->src = zalloc(sizeof(*notes->src) + symbol_conf.nr_events * sizeof_sym_hist);
638 if (notes->src == NULL)
640 notes->src->sizeof_sym_hist = sizeof_sym_hist;
641 notes->src->nr_histograms = symbol_conf.nr_events;
642 INIT_LIST_HEAD(¬es->src->source);
646 /* The cycles histogram is lazily allocated. */
647 static int symbol__alloc_hist_cycles(struct symbol *sym)
649 struct annotation *notes = symbol__annotation(sym);
650 const size_t size = symbol__size(sym);
652 notes->src->cycles_hist = calloc(size, sizeof(struct cyc_hist));
653 if (notes->src->cycles_hist == NULL)
658 void symbol__annotate_zero_histograms(struct symbol *sym)
660 struct annotation *notes = symbol__annotation(sym);
662 pthread_mutex_lock(¬es->lock);
663 if (notes->src != NULL) {
664 memset(notes->src->histograms, 0,
665 notes->src->nr_histograms * notes->src->sizeof_sym_hist);
666 if (notes->src->cycles_hist)
667 memset(notes->src->cycles_hist, 0,
668 symbol__size(sym) * sizeof(struct cyc_hist));
670 pthread_mutex_unlock(¬es->lock);
673 static int __symbol__account_cycles(struct annotation *notes,
675 unsigned offset, unsigned cycles,
680 ch = notes->src->cycles_hist;
682 * For now we can only account one basic block per
683 * final jump. But multiple could be overlapping.
684 * Always account the longest one. So when
685 * a shorter one has been already seen throw it away.
687 * We separately always account the full cycles.
689 ch[offset].num_aggr++;
690 ch[offset].cycles_aggr += cycles;
692 if (!have_start && ch[offset].have_start)
694 if (ch[offset].num) {
695 if (have_start && (!ch[offset].have_start ||
696 ch[offset].start > start)) {
697 ch[offset].have_start = 0;
698 ch[offset].cycles = 0;
700 if (ch[offset].reset < 0xffff)
702 } else if (have_start &&
703 ch[offset].start < start)
706 ch[offset].have_start = have_start;
707 ch[offset].start = start;
708 ch[offset].cycles += cycles;
713 static int __symbol__inc_addr_samples(struct symbol *sym, struct map *map,
714 struct annotation *notes, int evidx, u64 addr,
715 struct perf_sample *sample)
720 pr_debug3("%s: addr=%#" PRIx64 "\n", __func__, map->unmap_ip(map, addr));
722 if ((addr < sym->start || addr >= sym->end) &&
723 (addr != sym->end || sym->start != sym->end)) {
724 pr_debug("%s(%d): ERANGE! sym->name=%s, start=%#" PRIx64 ", addr=%#" PRIx64 ", end=%#" PRIx64 "\n",
725 __func__, __LINE__, sym->name, sym->start, addr, sym->end);
729 offset = addr - sym->start;
730 h = annotation__histogram(notes, evidx);
732 h->addr[offset].nr_samples++;
733 h->period += sample->period;
734 h->addr[offset].period += sample->period;
736 pr_debug3("%#" PRIx64 " %s: period++ [addr: %#" PRIx64 ", %#" PRIx64
737 ", evidx=%d] => nr_samples: %" PRIu64 ", period: %" PRIu64 "\n",
738 sym->start, sym->name, addr, addr - sym->start, evidx,
739 h->addr[offset].nr_samples, h->addr[offset].period);
743 static struct annotation *symbol__get_annotation(struct symbol *sym, bool cycles)
745 struct annotation *notes = symbol__annotation(sym);
747 if (notes->src == NULL) {
748 if (symbol__alloc_hist(sym) < 0)
751 if (!notes->src->cycles_hist && cycles) {
752 if (symbol__alloc_hist_cycles(sym) < 0)
758 static int symbol__inc_addr_samples(struct symbol *sym, struct map *map,
760 struct perf_sample *sample)
762 struct annotation *notes;
766 notes = symbol__get_annotation(sym, false);
769 return __symbol__inc_addr_samples(sym, map, notes, evidx, addr, sample);
772 static int symbol__account_cycles(u64 addr, u64 start,
773 struct symbol *sym, unsigned cycles)
775 struct annotation *notes;
780 notes = symbol__get_annotation(sym, true);
783 if (addr < sym->start || addr >= sym->end)
787 if (start < sym->start || start >= sym->end)
792 offset = addr - sym->start;
793 return __symbol__account_cycles(notes,
794 start ? start - sym->start : 0,
799 int addr_map_symbol__account_cycles(struct addr_map_symbol *ams,
800 struct addr_map_symbol *start,
810 * Only set start when IPC can be computed. We can only
811 * compute it when the basic block is completely in a single
813 * Special case the case when the jump is elsewhere, but
814 * it starts on the function start.
817 (start->sym == ams->sym ||
819 start->addr == ams->sym->start + ams->map->start)))
820 saddr = start->al_addr;
822 pr_debug2("BB with bad start: addr %"PRIx64" start %"PRIx64" sym %"PRIx64" saddr %"PRIx64"\n",
824 start ? start->addr : 0,
825 ams->sym ? ams->sym->start + ams->map->start : 0,
827 err = symbol__account_cycles(ams->al_addr, saddr, ams->sym, cycles);
829 pr_debug2("account_cycles failed %d\n", err);
833 int addr_map_symbol__inc_samples(struct addr_map_symbol *ams, struct perf_sample *sample,
836 return symbol__inc_addr_samples(ams->sym, ams->map, evidx, ams->al_addr, sample);
839 int hist_entry__inc_addr_samples(struct hist_entry *he, struct perf_sample *sample,
842 return symbol__inc_addr_samples(he->ms.sym, he->ms.map, evidx, ip, sample);
845 static void disasm_line__init_ins(struct disasm_line *dl, struct arch *arch, struct map *map)
847 dl->ins.ops = ins__find(arch, dl->ins.name);
852 if (dl->ins.ops->parse && dl->ins.ops->parse(arch, &dl->ops, map) < 0)
856 static int disasm_line__parse(char *line, const char **namep, char **rawp)
858 char tmp, *name = ltrim(line);
865 while ((*rawp)[0] != '\0' && !isspace((*rawp)[0]))
870 *namep = strdup(name);
876 *rawp = ltrim(*rawp);
886 struct annotate_args {
890 struct perf_evsel *evsel;
896 static void annotation_line__delete(struct annotation_line *al)
898 void *ptr = (void *) al - al->privsize;
900 free_srcline(al->path);
906 * Allocating the annotation line data with following
909 * --------------------------------------
910 * private space | struct annotation_line
911 * --------------------------------------
913 * Size of the private space is stored in 'struct annotation_line'.
916 static struct annotation_line *
917 annotation_line__new(struct annotate_args *args, size_t privsize)
919 struct annotation_line *al;
920 struct perf_evsel *evsel = args->evsel;
921 size_t size = privsize + sizeof(*al);
924 if (perf_evsel__is_group_event(evsel))
925 nr = evsel->nr_members;
927 size += sizeof(al->samples[0]) * nr;
931 al = (void *) al + privsize;
932 al->privsize = privsize;
933 al->offset = args->offset;
934 al->line = strdup(args->line);
935 al->line_nr = args->line_nr;
943 * Allocating the disasm annotation line data with
944 * following structure:
946 * ------------------------------------------------------------
947 * privsize space | struct disasm_line | struct annotation_line
948 * ------------------------------------------------------------
950 * We have 'struct annotation_line' member as last member
951 * of 'struct disasm_line' to have an easy access.
954 static struct disasm_line *disasm_line__new(struct annotate_args *args)
956 struct disasm_line *dl = NULL;
957 struct annotation_line *al;
958 size_t privsize = args->privsize + offsetof(struct disasm_line, al);
960 al = annotation_line__new(args, privsize);
962 dl = disasm_line(al);
964 if (dl->al.line == NULL)
967 if (args->offset != -1) {
968 if (disasm_line__parse(dl->al.line, &dl->ins.name, &dl->ops.raw) < 0)
971 disasm_line__init_ins(dl, args->arch, args->map);
984 void disasm_line__free(struct disasm_line *dl)
986 if (dl->ins.ops && dl->ins.ops->free)
987 dl->ins.ops->free(&dl->ops);
989 ins__delete(&dl->ops);
990 free((void *)dl->ins.name);
992 annotation_line__delete(&dl->al);
995 int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size, bool raw)
997 if (raw || !dl->ins.ops)
998 return scnprintf(bf, size, "%-6s %s", dl->ins.name, dl->ops.raw);
1000 return ins__scnprintf(&dl->ins, bf, size, &dl->ops);
1003 static void annotation_line__add(struct annotation_line *al, struct list_head *head)
1005 list_add_tail(&al->node, head);
1008 struct annotation_line *
1009 annotation_line__next(struct annotation_line *pos, struct list_head *head)
1011 list_for_each_entry_continue(pos, head, node)
1012 if (pos->offset >= 0)
1018 static const char *annotate__address_color(struct block_range *br)
1020 double cov = block_range__coverage(br);
1023 /* mark red for >75% coverage */
1025 return PERF_COLOR_RED;
1027 /* mark dull for <1% coverage */
1029 return PERF_COLOR_NORMAL;
1032 return PERF_COLOR_MAGENTA;
1035 static const char *annotate__asm_color(struct block_range *br)
1037 double cov = block_range__coverage(br);
1040 /* mark dull for <1% coverage */
1042 return PERF_COLOR_NORMAL;
1045 return PERF_COLOR_BLUE;
1048 static void annotate__branch_printf(struct block_range *br, u64 addr)
1050 bool emit_comment = true;
1056 if (br->is_target && br->start == addr) {
1057 struct block_range *branch = br;
1061 * Find matching branch to our target.
1063 while (!branch->is_branch)
1064 branch = block_range__next(branch);
1066 p = 100 *(double)br->entry / branch->coverage;
1070 emit_comment = false;
1075 * The percentage of coverage joined at this target in relation
1076 * to the next branch.
1078 printf(" +%.2f%%", p);
1082 if (br->is_branch && br->end == addr) {
1083 double p = 100*(double)br->taken / br->coverage;
1087 emit_comment = false;
1092 * The percentage of coverage leaving at this branch, and
1093 * its prediction ratio.
1095 printf(" -%.2f%% (p:%.2f%%)", p, 100*(double)br->pred / br->taken);
1100 static int disasm_line__print(struct disasm_line *dl, u64 start, int addr_fmt_width)
1102 s64 offset = dl->al.offset;
1103 const u64 addr = start + offset;
1104 struct block_range *br;
1106 br = block_range__find(addr);
1107 color_fprintf(stdout, annotate__address_color(br), " %*" PRIx64 ":", addr_fmt_width, addr);
1108 color_fprintf(stdout, annotate__asm_color(br), "%s", dl->al.line);
1109 annotate__branch_printf(br, addr);
1114 annotation_line__print(struct annotation_line *al, struct symbol *sym, u64 start,
1115 struct perf_evsel *evsel, u64 len, int min_pcnt, int printed,
1116 int max_lines, struct annotation_line *queue, int addr_fmt_width)
1118 struct disasm_line *dl = container_of(al, struct disasm_line, al);
1119 static const char *prev_line;
1120 static const char *prev_color;
1122 if (al->offset != -1) {
1123 double max_percent = 0.0;
1124 int i, nr_percent = 1;
1126 struct annotation *notes = symbol__annotation(sym);
1128 for (i = 0; i < al->samples_nr; i++) {
1129 struct annotation_data *sample = &al->samples[i];
1131 if (sample->percent > max_percent)
1132 max_percent = sample->percent;
1135 if (max_percent < min_pcnt)
1138 if (max_lines && printed >= max_lines)
1141 if (queue != NULL) {
1142 list_for_each_entry_from(queue, ¬es->src->source, node) {
1145 annotation_line__print(queue, sym, start, evsel, len,
1146 0, 0, 1, NULL, addr_fmt_width);
1150 color = get_percent_color(max_percent);
1153 * Also color the filename and line if needed, with
1154 * the same color than the percentage. Don't print it
1155 * twice for close colored addr with the same filename:line
1158 if (!prev_line || strcmp(prev_line, al->path)
1159 || color != prev_color) {
1160 color_fprintf(stdout, color, " %s", al->path);
1161 prev_line = al->path;
1166 for (i = 0; i < nr_percent; i++) {
1167 struct annotation_data *sample = &al->samples[i];
1169 color = get_percent_color(sample->percent);
1171 if (symbol_conf.show_total_period)
1172 color_fprintf(stdout, color, " %11" PRIu64,
1174 else if (symbol_conf.show_nr_samples)
1175 color_fprintf(stdout, color, " %7" PRIu64,
1176 sample->he.nr_samples);
1178 color_fprintf(stdout, color, " %7.2f", sample->percent);
1183 disasm_line__print(dl, start, addr_fmt_width);
1185 } else if (max_lines && printed >= max_lines)
1188 int width = symbol_conf.show_total_period ? 12 : 8;
1193 if (perf_evsel__is_group_event(evsel))
1194 width *= evsel->nr_members;
1197 printf(" %*s:\n", width, " ");
1199 printf(" %*s: %*s %s\n", width, " ", addr_fmt_width, " ", al->line);
1206 * symbol__parse_objdump_line() parses objdump output (with -d --no-show-raw)
1207 * which looks like following
1209 * 0000000000415500 <_init>:
1210 * 415500: sub $0x8,%rsp
1211 * 415504: mov 0x2f5ad5(%rip),%rax # 70afe0 <_DYNAMIC+0x2f8>
1212 * 41550b: test %rax,%rax
1213 * 41550e: je 415515 <_init+0x15>
1214 * 415510: callq 416e70 <__gmon_start__@plt>
1215 * 415515: add $0x8,%rsp
1218 * it will be parsed and saved into struct disasm_line as
1219 * <offset> <name> <ops.raw>
1221 * The offset will be a relative offset from the start of the symbol and -1
1222 * means that it's not a disassembly line so should be treated differently.
1223 * The ops.raw part will be parsed further according to type of the instruction.
1225 static int symbol__parse_objdump_line(struct symbol *sym, FILE *file,
1226 struct annotate_args *args,
1229 struct map *map = args->map;
1230 struct annotation *notes = symbol__annotation(sym);
1231 struct disasm_line *dl;
1232 char *line = NULL, *parsed_line, *tmp, *tmp2;
1234 s64 line_ip, offset = -1;
1235 regmatch_t match[2];
1237 if (getline(&line, &line_len, file) < 0)
1244 parsed_line = rtrim(line);
1246 /* /filename:linenr ? Save line number and ignore. */
1247 if (regexec(&file_lineno, parsed_line, 2, match, 0) == 0) {
1248 *line_nr = atoi(parsed_line + match[1].rm_so);
1252 tmp = ltrim(parsed_line);
1255 * Parse hexa addresses followed by ':'
1257 line_ip = strtoull(tmp, &tmp2, 16);
1258 if (*tmp2 != ':' || tmp == tmp2 || tmp2[1] == '\0')
1262 if (line_ip != -1) {
1263 u64 start = map__rip_2objdump(map, sym->start),
1264 end = map__rip_2objdump(map, sym->end);
1266 offset = line_ip - start;
1267 if ((u64)line_ip < start || (u64)line_ip >= end)
1270 parsed_line = tmp2 + 1;
1273 args->offset = offset;
1274 args->line = parsed_line;
1275 args->line_nr = *line_nr;
1277 dl = disasm_line__new(args);
1284 if (!disasm_line__has_offset(dl)) {
1285 dl->ops.target.offset = dl->ops.target.addr -
1286 map__rip_2objdump(map, sym->start);
1287 dl->ops.target.offset_avail = true;
1290 /* kcore has no symbols, so add the call target symbol */
1291 if (dl->ins.ops && ins__is_call(&dl->ins) && !dl->ops.target.sym) {
1292 struct addr_map_symbol target = {
1294 .addr = dl->ops.target.addr,
1297 if (!map_groups__find_ams(&target) &&
1298 target.sym->start == target.al_addr)
1299 dl->ops.target.sym = target.sym;
1302 annotation_line__add(&dl->al, ¬es->src->source);
1307 static __attribute__((constructor)) void symbol__init_regexpr(void)
1309 regcomp(&file_lineno, "^/[^:]+:([0-9]+)", REG_EXTENDED);
1312 static void delete_last_nop(struct symbol *sym)
1314 struct annotation *notes = symbol__annotation(sym);
1315 struct list_head *list = ¬es->src->source;
1316 struct disasm_line *dl;
1318 while (!list_empty(list)) {
1319 dl = list_entry(list->prev, struct disasm_line, al.node);
1322 if (dl->ins.ops != &nop_ops)
1325 if (!strstr(dl->al.line, " nop ") &&
1326 !strstr(dl->al.line, " nopl ") &&
1327 !strstr(dl->al.line, " nopw "))
1331 list_del(&dl->al.node);
1332 disasm_line__free(dl);
1336 int symbol__strerror_disassemble(struct symbol *sym __maybe_unused, struct map *map,
1337 int errnum, char *buf, size_t buflen)
1339 struct dso *dso = map->dso;
1341 BUG_ON(buflen == 0);
1344 str_error_r(errnum, buf, buflen);
1349 case SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX: {
1350 char bf[SBUILD_ID_SIZE + 15] = " with build id ";
1351 char *build_id_msg = NULL;
1353 if (dso->has_build_id) {
1354 build_id__sprintf(dso->build_id,
1355 sizeof(dso->build_id), bf + 15);
1358 scnprintf(buf, buflen,
1359 "No vmlinux file%s\nwas found in the path.\n\n"
1360 "Note that annotation using /proc/kcore requires CAP_SYS_RAWIO capability.\n\n"
1362 " perf buildid-cache -vu vmlinux\n\n"
1364 " --vmlinux vmlinux\n", build_id_msg ?: "");
1368 scnprintf(buf, buflen, "Internal error: Invalid %d error code\n", errnum);
1375 static int dso__disassemble_filename(struct dso *dso, char *filename, size_t filename_size)
1377 char linkname[PATH_MAX];
1378 char *build_id_filename;
1379 char *build_id_path = NULL;
1382 if (dso->symtab_type == DSO_BINARY_TYPE__KALLSYMS &&
1383 !dso__is_kcore(dso))
1384 return SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX;
1386 build_id_filename = dso__build_id_filename(dso, NULL, 0, false);
1387 if (build_id_filename) {
1388 __symbol__join_symfs(filename, filename_size, build_id_filename);
1389 free(build_id_filename);
1391 if (dso->has_build_id)
1396 build_id_path = strdup(filename);
1401 * old style build-id cache has name of XX/XXXXXXX.. while
1402 * new style has XX/XXXXXXX../{elf,kallsyms,vdso}.
1403 * extract the build-id part of dirname in the new style only.
1405 pos = strrchr(build_id_path, '/');
1406 if (pos && strlen(pos) < SBUILD_ID_SIZE - 2)
1407 dirname(build_id_path);
1409 if (dso__is_kcore(dso) ||
1410 readlink(build_id_path, linkname, sizeof(linkname)) < 0 ||
1411 strstr(linkname, DSO__NAME_KALLSYMS) ||
1412 access(filename, R_OK)) {
1415 * If we don't have build-ids or the build-id file isn't in the
1416 * cache, or is just a kallsyms file, well, lets hope that this
1417 * DSO is the same as when 'perf record' ran.
1419 __symbol__join_symfs(filename, filename_size, dso->long_name);
1422 free(build_id_path);
1426 static int symbol__disassemble(struct symbol *sym, struct annotate_args *args)
1428 struct map *map = args->map;
1429 struct dso *dso = map->dso;
1430 char command[PATH_MAX * 2];
1432 char symfs_filename[PATH_MAX];
1433 struct kcore_extract kce;
1434 bool delete_extract = false;
1439 int err = dso__disassemble_filename(dso, symfs_filename, sizeof(symfs_filename));
1444 pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__,
1445 symfs_filename, sym->name, map->unmap_ip(map, sym->start),
1446 map->unmap_ip(map, sym->end));
1448 pr_debug("annotating [%p] %30s : [%p] %30s\n",
1449 dso, dso->long_name, sym, sym->name);
1451 if (dso__is_kcore(dso)) {
1452 kce.kcore_filename = symfs_filename;
1453 kce.addr = map__rip_2objdump(map, sym->start);
1454 kce.offs = sym->start;
1455 kce.len = sym->end - sym->start;
1456 if (!kcore_extract__create(&kce)) {
1457 delete_extract = true;
1458 strlcpy(symfs_filename, kce.extract_filename,
1459 sizeof(symfs_filename));
1461 } else if (dso__needs_decompress(dso)) {
1462 char tmp[KMOD_DECOMP_LEN];
1464 if (dso__decompress_kmodule_path(dso, symfs_filename,
1465 tmp, sizeof(tmp)) < 0)
1468 strcpy(symfs_filename, tmp);
1471 snprintf(command, sizeof(command),
1472 "%s %s%s --start-address=0x%016" PRIx64
1473 " --stop-address=0x%016" PRIx64
1474 " -l -d %s %s -C \"%s\" 2>/dev/null|grep -v \"%s:\"|expand",
1475 objdump_path ? objdump_path : "objdump",
1476 disassembler_style ? "-M " : "",
1477 disassembler_style ? disassembler_style : "",
1478 map__rip_2objdump(map, sym->start),
1479 map__rip_2objdump(map, sym->end),
1480 symbol_conf.annotate_asm_raw ? "" : "--no-show-raw",
1481 symbol_conf.annotate_src ? "-S" : "",
1482 symfs_filename, symfs_filename);
1484 pr_debug("Executing: %s\n", command);
1487 if (pipe(stdout_fd) < 0) {
1488 pr_err("Failure creating the pipe to run %s\n", command);
1489 goto out_remove_tmp;
1494 pr_err("Failure forking to run %s\n", command);
1495 goto out_close_stdout;
1499 close(stdout_fd[0]);
1500 dup2(stdout_fd[1], 1);
1501 close(stdout_fd[1]);
1502 execl("/bin/sh", "sh", "-c", command, NULL);
1507 close(stdout_fd[1]);
1509 file = fdopen(stdout_fd[0], "r");
1511 pr_err("Failure creating FILE stream for %s\n", command);
1513 * If we were using debug info should retry with
1516 goto out_remove_tmp;
1520 while (!feof(file)) {
1522 * The source code line number (lineno) needs to be kept in
1523 * accross calls to symbol__parse_objdump_line(), so that it
1524 * can associate it with the instructions till the next one.
1525 * See disasm_line__new() and struct disasm_line::line_nr.
1527 if (symbol__parse_objdump_line(sym, file, args, &lineno) < 0)
1533 pr_err("No output from %s\n", command);
1536 * kallsyms does not have symbol sizes so there may a nop at the end.
1539 if (dso__is_kcore(dso))
1540 delete_last_nop(sym);
1545 close(stdout_fd[0]);
1547 if (dso__needs_decompress(dso))
1548 unlink(symfs_filename);
1551 kcore_extract__delete(&kce);
1556 close(stdout_fd[1]);
1557 goto out_remove_tmp;
1560 static void calc_percent(struct sym_hist *hist,
1561 struct annotation_data *sample,
1562 s64 offset, s64 end)
1564 unsigned int hits = 0;
1567 while (offset < end) {
1568 hits += hist->addr[offset].nr_samples;
1569 period += hist->addr[offset].period;
1573 if (hist->nr_samples) {
1574 sample->he.period = period;
1575 sample->he.nr_samples = hits;
1576 sample->percent = 100.0 * hits / hist->nr_samples;
1580 static void annotation__calc_percent(struct annotation *notes,
1581 struct perf_evsel *evsel, s64 len)
1583 struct annotation_line *al, *next;
1585 list_for_each_entry(al, ¬es->src->source, node) {
1589 if (al->offset == -1)
1592 next = annotation_line__next(al, ¬es->src->source);
1593 end = next ? next->offset : len;
1595 for (i = 0; i < al->samples_nr; i++) {
1596 struct annotation_data *sample;
1597 struct sym_hist *hist;
1599 hist = annotation__histogram(notes, evsel->idx + i);
1600 sample = &al->samples[i];
1602 calc_percent(hist, sample, al->offset, end);
1607 void symbol__calc_percent(struct symbol *sym, struct perf_evsel *evsel)
1609 struct annotation *notes = symbol__annotation(sym);
1611 annotation__calc_percent(notes, evsel, symbol__size(sym));
1614 int symbol__annotate(struct symbol *sym, struct map *map,
1615 struct perf_evsel *evsel, size_t privsize,
1616 struct arch **parch)
1618 struct annotate_args args = {
1619 .privsize = privsize,
1623 struct perf_env *env = perf_evsel__env(evsel);
1624 const char *arch_name = perf_env__arch(env);
1631 args.arch = arch = arch__find(arch_name);
1639 err = arch->init(arch, env ? env->cpuid : NULL);
1641 pr_err("%s: failed to initialize %s arch priv area\n", __func__, arch->name);
1646 return symbol__disassemble(sym, &args);
1649 static void insert_source_line(struct rb_root *root, struct annotation_line *al)
1651 struct annotation_line *iter;
1652 struct rb_node **p = &root->rb_node;
1653 struct rb_node *parent = NULL;
1656 while (*p != NULL) {
1658 iter = rb_entry(parent, struct annotation_line, rb_node);
1660 ret = strcmp(iter->path, al->path);
1662 for (i = 0; i < al->samples_nr; i++)
1663 iter->samples[i].percent_sum += al->samples[i].percent;
1670 p = &(*p)->rb_right;
1673 for (i = 0; i < al->samples_nr; i++)
1674 al->samples[i].percent_sum = al->samples[i].percent;
1676 rb_link_node(&al->rb_node, parent, p);
1677 rb_insert_color(&al->rb_node, root);
1680 static int cmp_source_line(struct annotation_line *a, struct annotation_line *b)
1684 for (i = 0; i < a->samples_nr; i++) {
1685 if (a->samples[i].percent_sum == b->samples[i].percent_sum)
1687 return a->samples[i].percent_sum > b->samples[i].percent_sum;
1693 static void __resort_source_line(struct rb_root *root, struct annotation_line *al)
1695 struct annotation_line *iter;
1696 struct rb_node **p = &root->rb_node;
1697 struct rb_node *parent = NULL;
1699 while (*p != NULL) {
1701 iter = rb_entry(parent, struct annotation_line, rb_node);
1703 if (cmp_source_line(al, iter))
1706 p = &(*p)->rb_right;
1709 rb_link_node(&al->rb_node, parent, p);
1710 rb_insert_color(&al->rb_node, root);
1713 static void resort_source_line(struct rb_root *dest_root, struct rb_root *src_root)
1715 struct annotation_line *al;
1716 struct rb_node *node;
1718 node = rb_first(src_root);
1720 struct rb_node *next;
1722 al = rb_entry(node, struct annotation_line, rb_node);
1723 next = rb_next(node);
1724 rb_erase(node, src_root);
1726 __resort_source_line(dest_root, al);
1731 static void print_summary(struct rb_root *root, const char *filename)
1733 struct annotation_line *al;
1734 struct rb_node *node;
1736 printf("\nSorted summary for file %s\n", filename);
1737 printf("----------------------------------------------\n\n");
1739 if (RB_EMPTY_ROOT(root)) {
1740 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
1744 node = rb_first(root);
1746 double percent, percent_max = 0.0;
1751 al = rb_entry(node, struct annotation_line, rb_node);
1752 for (i = 0; i < al->samples_nr; i++) {
1753 percent = al->samples[i].percent_sum;
1754 color = get_percent_color(percent);
1755 color_fprintf(stdout, color, " %7.2f", percent);
1757 if (percent > percent_max)
1758 percent_max = percent;
1762 color = get_percent_color(percent_max);
1763 color_fprintf(stdout, color, " %s\n", path);
1765 node = rb_next(node);
1769 static void symbol__annotate_hits(struct symbol *sym, struct perf_evsel *evsel)
1771 struct annotation *notes = symbol__annotation(sym);
1772 struct sym_hist *h = annotation__histogram(notes, evsel->idx);
1773 u64 len = symbol__size(sym), offset;
1775 for (offset = 0; offset < len; ++offset)
1776 if (h->addr[offset].nr_samples != 0)
1777 printf("%*" PRIx64 ": %" PRIu64 "\n", BITS_PER_LONG / 2,
1778 sym->start + offset, h->addr[offset].nr_samples);
1779 printf("%*s: %" PRIu64 "\n", BITS_PER_LONG / 2, "h->nr_samples", h->nr_samples);
1782 static int annotated_source__addr_fmt_width(struct list_head *lines, u64 start)
1785 struct annotation_line *line;
1787 list_for_each_entry_reverse(line, lines, node) {
1788 if (line->offset != -1)
1789 return scnprintf(bf, sizeof(bf), "%" PRIx64, start + line->offset);
1795 int symbol__annotate_printf(struct symbol *sym, struct map *map,
1796 struct perf_evsel *evsel, bool full_paths,
1797 int min_pcnt, int max_lines, int context)
1799 struct dso *dso = map->dso;
1801 const char *d_filename;
1802 const char *evsel_name = perf_evsel__name(evsel);
1803 struct annotation *notes = symbol__annotation(sym);
1804 struct sym_hist *h = annotation__histogram(notes, evsel->idx);
1805 struct annotation_line *pos, *queue = NULL;
1806 u64 start = map__rip_2objdump(map, sym->start);
1807 int printed = 2, queue_len = 0, addr_fmt_width;
1810 int width = symbol_conf.show_total_period ? 12 : 8;
1811 int graph_dotted_len;
1813 filename = strdup(dso->long_name);
1818 d_filename = filename;
1820 d_filename = basename(filename);
1822 len = symbol__size(sym);
1824 if (perf_evsel__is_group_event(evsel))
1825 width *= evsel->nr_members;
1827 graph_dotted_len = printf(" %-*.*s| Source code & Disassembly of %s for %s (%" PRIu64 " samples)\n",
1828 width, width, symbol_conf.show_total_period ? "Period" :
1829 symbol_conf.show_nr_samples ? "Samples" : "Percent",
1830 d_filename, evsel_name, h->nr_samples);
1832 printf("%-*.*s----\n",
1833 graph_dotted_len, graph_dotted_len, graph_dotted_line);
1836 symbol__annotate_hits(sym, evsel);
1838 addr_fmt_width = annotated_source__addr_fmt_width(¬es->src->source, start);
1840 list_for_each_entry(pos, ¬es->src->source, node) {
1843 if (context && queue == NULL) {
1848 err = annotation_line__print(pos, sym, start, evsel, len,
1849 min_pcnt, printed, max_lines,
1850 queue, addr_fmt_width);
1856 printed += queue_len;
1862 /* filtered by max_lines */
1868 * Filtered by min_pcnt or non IP lines when
1873 if (queue_len == context)
1874 queue = list_entry(queue->node.next, typeof(*queue), node);
1886 void symbol__annotate_zero_histogram(struct symbol *sym, int evidx)
1888 struct annotation *notes = symbol__annotation(sym);
1889 struct sym_hist *h = annotation__histogram(notes, evidx);
1891 memset(h, 0, notes->src->sizeof_sym_hist);
1894 void symbol__annotate_decay_histogram(struct symbol *sym, int evidx)
1896 struct annotation *notes = symbol__annotation(sym);
1897 struct sym_hist *h = annotation__histogram(notes, evidx);
1898 int len = symbol__size(sym), offset;
1901 for (offset = 0; offset < len; ++offset) {
1902 h->addr[offset].nr_samples = h->addr[offset].nr_samples * 7 / 8;
1903 h->nr_samples += h->addr[offset].nr_samples;
1907 void annotated_source__purge(struct annotated_source *as)
1909 struct annotation_line *al, *n;
1911 list_for_each_entry_safe(al, n, &as->source, node) {
1912 list_del(&al->node);
1913 disasm_line__free(disasm_line(al));
1917 static size_t disasm_line__fprintf(struct disasm_line *dl, FILE *fp)
1921 if (dl->al.offset == -1)
1922 return fprintf(fp, "%s\n", dl->al.line);
1924 printed = fprintf(fp, "%#" PRIx64 " %s", dl->al.offset, dl->ins.name);
1926 if (dl->ops.raw[0] != '\0') {
1927 printed += fprintf(fp, "%.*s %s\n", 6 - (int)printed, " ",
1931 return printed + fprintf(fp, "\n");
1934 size_t disasm__fprintf(struct list_head *head, FILE *fp)
1936 struct disasm_line *pos;
1939 list_for_each_entry(pos, head, al.node)
1940 printed += disasm_line__fprintf(pos, fp);
1945 static void annotation__calc_lines(struct annotation *notes, struct map *map,
1946 struct rb_root *root, u64 start)
1948 struct annotation_line *al;
1949 struct rb_root tmp_root = RB_ROOT;
1951 list_for_each_entry(al, ¬es->src->source, node) {
1952 double percent_max = 0.0;
1955 for (i = 0; i < al->samples_nr; i++) {
1956 struct annotation_data *sample;
1958 sample = &al->samples[i];
1960 if (sample->percent > percent_max)
1961 percent_max = sample->percent;
1964 if (percent_max <= 0.5)
1967 al->path = get_srcline(map->dso, start + al->offset, NULL,
1968 false, true, start + al->offset);
1969 insert_source_line(&tmp_root, al);
1972 resort_source_line(root, &tmp_root);
1975 static void symbol__calc_lines(struct symbol *sym, struct map *map,
1976 struct rb_root *root)
1978 struct annotation *notes = symbol__annotation(sym);
1979 u64 start = map__rip_2objdump(map, sym->start);
1981 annotation__calc_lines(notes, map, root, start);
1984 int symbol__tty_annotate(struct symbol *sym, struct map *map,
1985 struct perf_evsel *evsel, bool print_lines,
1986 bool full_paths, int min_pcnt, int max_lines)
1988 struct dso *dso = map->dso;
1989 struct rb_root source_line = RB_ROOT;
1991 if (symbol__annotate(sym, map, evsel, 0, NULL) < 0)
1994 symbol__calc_percent(sym, evsel);
1997 srcline_full_filename = full_paths;
1998 symbol__calc_lines(sym, map, &source_line);
1999 print_summary(&source_line, dso->long_name);
2002 symbol__annotate_printf(sym, map, evsel, full_paths,
2003 min_pcnt, max_lines, 0);
2005 annotated_source__purge(symbol__annotation(sym)->src);
2010 bool ui__has_annotation(void)
2012 return use_browser == 1 && perf_hpp_list.sym;