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>
29 #include <sys/utsname.h>
31 #include "sane_ctype.h"
33 const char *disassembler_style;
34 const char *objdump_path;
35 static regex_t file_lineno;
37 static struct ins_ops *ins__find(struct arch *arch, const char *name);
38 static void ins__sort(struct arch *arch);
39 static int disasm_line__parse(char *line, const char **namep, char **rawp);
43 struct ins *instructions;
44 size_t nr_instructions;
45 size_t nr_instructions_allocated;
46 struct ins_ops *(*associate_instruction_ops)(struct arch *arch, const char *name);
47 bool sorted_instructions;
52 int (*init)(struct arch *arch);
53 bool (*ins_is_fused)(struct arch *arch, const char *ins1,
55 int (*cpuid_parse)(struct arch *arch, char *cpuid);
58 char skip_functions_char;
62 static struct ins_ops call_ops;
63 static struct ins_ops dec_ops;
64 static struct ins_ops jump_ops;
65 static struct ins_ops mov_ops;
66 static struct ins_ops nop_ops;
67 static struct ins_ops lock_ops;
68 static struct ins_ops ret_ops;
70 static int arch__grow_instructions(struct arch *arch)
72 struct ins *new_instructions;
73 size_t new_nr_allocated;
75 if (arch->nr_instructions_allocated == 0 && arch->instructions)
76 goto grow_from_non_allocated_table;
78 new_nr_allocated = arch->nr_instructions_allocated + 128;
79 new_instructions = realloc(arch->instructions, new_nr_allocated * sizeof(struct ins));
80 if (new_instructions == NULL)
83 out_update_instructions:
84 arch->instructions = new_instructions;
85 arch->nr_instructions_allocated = new_nr_allocated;
88 grow_from_non_allocated_table:
89 new_nr_allocated = arch->nr_instructions + 128;
90 new_instructions = calloc(new_nr_allocated, sizeof(struct ins));
91 if (new_instructions == NULL)
94 memcpy(new_instructions, arch->instructions, arch->nr_instructions);
95 goto out_update_instructions;
98 static int arch__associate_ins_ops(struct arch* arch, const char *name, struct ins_ops *ops)
102 if (arch->nr_instructions == arch->nr_instructions_allocated &&
103 arch__grow_instructions(arch))
106 ins = &arch->instructions[arch->nr_instructions];
107 ins->name = strdup(name);
112 arch->nr_instructions++;
118 #include "arch/arm/annotate/instructions.c"
119 #include "arch/arm64/annotate/instructions.c"
120 #include "arch/x86/annotate/instructions.c"
121 #include "arch/powerpc/annotate/instructions.c"
122 #include "arch/s390/annotate/instructions.c"
124 static struct arch architectures[] = {
127 .init = arm__annotate_init,
131 .init = arm64__annotate_init,
135 .instructions = x86__instructions,
136 .nr_instructions = ARRAY_SIZE(x86__instructions),
137 .ins_is_fused = x86__ins_is_fused,
138 .cpuid_parse = x86__cpuid_parse,
145 .init = powerpc__annotate_init,
149 .init = s390__annotate_init,
156 static void ins__delete(struct ins_operands *ops)
160 zfree(&ops->source.raw);
161 zfree(&ops->source.name);
162 zfree(&ops->target.raw);
163 zfree(&ops->target.name);
166 static int ins__raw_scnprintf(struct ins *ins, char *bf, size_t size,
167 struct ins_operands *ops)
169 return scnprintf(bf, size, "%-6.6s %s", ins->name, ops->raw);
172 int ins__scnprintf(struct ins *ins, char *bf, size_t size,
173 struct ins_operands *ops)
175 if (ins->ops->scnprintf)
176 return ins->ops->scnprintf(ins, bf, size, ops);
178 return ins__raw_scnprintf(ins, bf, size, ops);
181 bool ins__is_fused(struct arch *arch, const char *ins1, const char *ins2)
183 if (!arch || !arch->ins_is_fused)
186 return arch->ins_is_fused(arch, ins1, ins2);
189 static int call__parse(struct arch *arch, struct ins_operands *ops, struct map *map)
191 char *endptr, *tok, *name;
193 ops->target.addr = strtoull(ops->raw, &endptr, 16);
195 name = strchr(endptr, '<');
201 if (arch->objdump.skip_functions_char &&
202 strchr(name, arch->objdump.skip_functions_char))
205 tok = strchr(name, '>');
210 ops->target.name = strdup(name);
213 return ops->target.name == NULL ? -1 : 0;
216 tok = strchr(endptr, '*');
218 struct symbol *sym = map__find_symbol(map, map->map_ip(map, ops->target.addr));
220 ops->target.name = strdup(sym->name);
222 ops->target.addr = 0;
226 ops->target.addr = strtoull(tok + 1, NULL, 16);
230 static int call__scnprintf(struct ins *ins, char *bf, size_t size,
231 struct ins_operands *ops)
233 if (ops->target.name)
234 return scnprintf(bf, size, "%-6.6s %s", ins->name, ops->target.name);
236 if (ops->target.addr == 0)
237 return ins__raw_scnprintf(ins, bf, size, ops);
239 return scnprintf(bf, size, "%-6.6s *%" PRIx64, ins->name, ops->target.addr);
242 static struct ins_ops call_ops = {
243 .parse = call__parse,
244 .scnprintf = call__scnprintf,
247 bool ins__is_call(const struct ins *ins)
249 return ins->ops == &call_ops;
252 static int jump__parse(struct arch *arch __maybe_unused, struct ins_operands *ops, struct map *map __maybe_unused)
254 const char *s = strchr(ops->raw, '+');
255 const char *c = strchr(ops->raw, ',');
258 * skip over possible up to 2 operands to get to address, e.g.:
259 * tbnz w0, #26, ffff0000083cd190 <security_file_permission+0xd0>
262 ops->target.addr = strtoull(c, NULL, 16);
263 if (!ops->target.addr) {
266 ops->target.addr = strtoull(c, NULL, 16);
269 ops->target.addr = strtoull(ops->raw, NULL, 16);
273 ops->target.offset = strtoull(s, NULL, 16);
274 ops->target.offset_avail = true;
276 ops->target.offset_avail = false;
282 static int jump__scnprintf(struct ins *ins, char *bf, size_t size,
283 struct ins_operands *ops)
285 const char *c = strchr(ops->raw, ',');
287 if (!ops->target.addr || ops->target.offset < 0)
288 return ins__raw_scnprintf(ins, bf, size, ops);
291 const char *c2 = strchr(c + 1, ',');
293 /* check for 3-op insn */
298 /* mirror arch objdump's space-after-comma style */
303 return scnprintf(bf, size, "%-6.6s %.*s%" PRIx64,
304 ins->name, c ? c - ops->raw : 0, ops->raw,
308 static struct ins_ops jump_ops = {
309 .parse = jump__parse,
310 .scnprintf = jump__scnprintf,
313 bool ins__is_jump(const struct ins *ins)
315 return ins->ops == &jump_ops;
318 static int comment__symbol(char *raw, char *comment, u64 *addrp, char **namep)
320 char *endptr, *name, *t;
322 if (strstr(raw, "(%rip)") == NULL)
325 *addrp = strtoull(comment, &endptr, 16);
326 name = strchr(endptr, '<');
332 t = strchr(name, '>');
337 *namep = strdup(name);
343 static int lock__parse(struct arch *arch, struct ins_operands *ops, struct map *map)
345 ops->locked.ops = zalloc(sizeof(*ops->locked.ops));
346 if (ops->locked.ops == NULL)
349 if (disasm_line__parse(ops->raw, &ops->locked.ins.name, &ops->locked.ops->raw) < 0)
352 ops->locked.ins.ops = ins__find(arch, ops->locked.ins.name);
354 if (ops->locked.ins.ops == NULL)
357 if (ops->locked.ins.ops->parse &&
358 ops->locked.ins.ops->parse(arch, ops->locked.ops, map) < 0)
364 zfree(&ops->locked.ops);
368 static int lock__scnprintf(struct ins *ins, char *bf, size_t size,
369 struct ins_operands *ops)
373 if (ops->locked.ins.ops == NULL)
374 return ins__raw_scnprintf(ins, bf, size, ops);
376 printed = scnprintf(bf, size, "%-6.6s ", ins->name);
377 return printed + ins__scnprintf(&ops->locked.ins, bf + printed,
378 size - printed, ops->locked.ops);
381 static void lock__delete(struct ins_operands *ops)
383 struct ins *ins = &ops->locked.ins;
385 if (ins->ops && ins->ops->free)
386 ins->ops->free(ops->locked.ops);
388 ins__delete(ops->locked.ops);
390 zfree(&ops->locked.ops);
391 zfree(&ops->target.raw);
392 zfree(&ops->target.name);
395 static struct ins_ops lock_ops = {
396 .free = lock__delete,
397 .parse = lock__parse,
398 .scnprintf = lock__scnprintf,
401 static int mov__parse(struct arch *arch, struct ins_operands *ops, struct map *map __maybe_unused)
403 char *s = strchr(ops->raw, ','), *target, *comment, prev;
409 ops->source.raw = strdup(ops->raw);
412 if (ops->source.raw == NULL)
416 comment = strchr(s, arch->objdump.comment_char);
421 s = strchr(s, '\0') - 1;
423 while (s > target && isspace(s[0]))
429 ops->target.raw = strdup(target);
432 if (ops->target.raw == NULL)
433 goto out_free_source;
438 comment = ltrim(comment);
439 comment__symbol(ops->source.raw, comment, &ops->source.addr, &ops->source.name);
440 comment__symbol(ops->target.raw, comment, &ops->target.addr, &ops->target.name);
445 zfree(&ops->source.raw);
449 static int mov__scnprintf(struct ins *ins, char *bf, size_t size,
450 struct ins_operands *ops)
452 return scnprintf(bf, size, "%-6.6s %s,%s", ins->name,
453 ops->source.name ?: ops->source.raw,
454 ops->target.name ?: ops->target.raw);
457 static struct ins_ops mov_ops = {
459 .scnprintf = mov__scnprintf,
462 static int dec__parse(struct arch *arch __maybe_unused, struct ins_operands *ops, struct map *map __maybe_unused)
464 char *target, *comment, *s, prev;
466 target = s = ops->raw;
468 while (s[0] != '\0' && !isspace(s[0]))
473 ops->target.raw = strdup(target);
476 if (ops->target.raw == NULL)
479 comment = strchr(s, arch->objdump.comment_char);
483 comment = ltrim(comment);
484 comment__symbol(ops->target.raw, comment, &ops->target.addr, &ops->target.name);
489 static int dec__scnprintf(struct ins *ins, char *bf, size_t size,
490 struct ins_operands *ops)
492 return scnprintf(bf, size, "%-6.6s %s", ins->name,
493 ops->target.name ?: ops->target.raw);
496 static struct ins_ops dec_ops = {
498 .scnprintf = dec__scnprintf,
501 static int nop__scnprintf(struct ins *ins __maybe_unused, char *bf, size_t size,
502 struct ins_operands *ops __maybe_unused)
504 return scnprintf(bf, size, "%-6.6s", "nop");
507 static struct ins_ops nop_ops = {
508 .scnprintf = nop__scnprintf,
511 static struct ins_ops ret_ops = {
512 .scnprintf = ins__raw_scnprintf,
515 bool ins__is_ret(const struct ins *ins)
517 return ins->ops == &ret_ops;
520 bool ins__is_lock(const struct ins *ins)
522 return ins->ops == &lock_ops;
525 static int ins__key_cmp(const void *name, const void *insp)
527 const struct ins *ins = insp;
529 return strcmp(name, ins->name);
532 static int ins__cmp(const void *a, const void *b)
534 const struct ins *ia = a;
535 const struct ins *ib = b;
537 return strcmp(ia->name, ib->name);
540 static void ins__sort(struct arch *arch)
542 const int nmemb = arch->nr_instructions;
544 qsort(arch->instructions, nmemb, sizeof(struct ins), ins__cmp);
547 static struct ins_ops *__ins__find(struct arch *arch, const char *name)
550 const int nmemb = arch->nr_instructions;
552 if (!arch->sorted_instructions) {
554 arch->sorted_instructions = true;
557 ins = bsearch(name, arch->instructions, nmemb, sizeof(struct ins), ins__key_cmp);
558 return ins ? ins->ops : NULL;
561 static struct ins_ops *ins__find(struct arch *arch, const char *name)
563 struct ins_ops *ops = __ins__find(arch, name);
565 if (!ops && arch->associate_instruction_ops)
566 ops = arch->associate_instruction_ops(arch, name);
571 static int arch__key_cmp(const void *name, const void *archp)
573 const struct arch *arch = archp;
575 return strcmp(name, arch->name);
578 static int arch__cmp(const void *a, const void *b)
580 const struct arch *aa = a;
581 const struct arch *ab = b;
583 return strcmp(aa->name, ab->name);
586 static void arch__sort(void)
588 const int nmemb = ARRAY_SIZE(architectures);
590 qsort(architectures, nmemb, sizeof(struct arch), arch__cmp);
593 static struct arch *arch__find(const char *name)
595 const int nmemb = ARRAY_SIZE(architectures);
603 return bsearch(name, architectures, nmemb, sizeof(struct arch), arch__key_cmp);
606 int symbol__alloc_hist(struct symbol *sym)
608 struct annotation *notes = symbol__annotation(sym);
609 const size_t size = symbol__size(sym);
610 size_t sizeof_sym_hist;
612 /* Check for overflow when calculating sizeof_sym_hist */
613 if (size > (SIZE_MAX - sizeof(struct sym_hist)) / sizeof(struct sym_hist_entry))
616 sizeof_sym_hist = (sizeof(struct sym_hist) + size * sizeof(struct sym_hist_entry));
618 /* Check for overflow in zalloc argument */
619 if (sizeof_sym_hist > (SIZE_MAX - sizeof(*notes->src))
620 / symbol_conf.nr_events)
623 notes->src = zalloc(sizeof(*notes->src) + symbol_conf.nr_events * sizeof_sym_hist);
624 if (notes->src == NULL)
626 notes->src->sizeof_sym_hist = sizeof_sym_hist;
627 notes->src->nr_histograms = symbol_conf.nr_events;
628 INIT_LIST_HEAD(¬es->src->source);
632 /* The cycles histogram is lazily allocated. */
633 static int symbol__alloc_hist_cycles(struct symbol *sym)
635 struct annotation *notes = symbol__annotation(sym);
636 const size_t size = symbol__size(sym);
638 notes->src->cycles_hist = calloc(size, sizeof(struct cyc_hist));
639 if (notes->src->cycles_hist == NULL)
644 void symbol__annotate_zero_histograms(struct symbol *sym)
646 struct annotation *notes = symbol__annotation(sym);
648 pthread_mutex_lock(¬es->lock);
649 if (notes->src != NULL) {
650 memset(notes->src->histograms, 0,
651 notes->src->nr_histograms * notes->src->sizeof_sym_hist);
652 if (notes->src->cycles_hist)
653 memset(notes->src->cycles_hist, 0,
654 symbol__size(sym) * sizeof(struct cyc_hist));
656 pthread_mutex_unlock(¬es->lock);
659 static int __symbol__account_cycles(struct annotation *notes,
661 unsigned offset, unsigned cycles,
666 ch = notes->src->cycles_hist;
668 * For now we can only account one basic block per
669 * final jump. But multiple could be overlapping.
670 * Always account the longest one. So when
671 * a shorter one has been already seen throw it away.
673 * We separately always account the full cycles.
675 ch[offset].num_aggr++;
676 ch[offset].cycles_aggr += cycles;
678 if (!have_start && ch[offset].have_start)
680 if (ch[offset].num) {
681 if (have_start && (!ch[offset].have_start ||
682 ch[offset].start > start)) {
683 ch[offset].have_start = 0;
684 ch[offset].cycles = 0;
686 if (ch[offset].reset < 0xffff)
688 } else if (have_start &&
689 ch[offset].start < start)
692 ch[offset].have_start = have_start;
693 ch[offset].start = start;
694 ch[offset].cycles += cycles;
699 static int __symbol__inc_addr_samples(struct symbol *sym, struct map *map,
700 struct annotation *notes, int evidx, u64 addr,
701 struct perf_sample *sample)
706 pr_debug3("%s: addr=%#" PRIx64 "\n", __func__, map->unmap_ip(map, addr));
708 if ((addr < sym->start || addr >= sym->end) &&
709 (addr != sym->end || sym->start != sym->end)) {
710 pr_debug("%s(%d): ERANGE! sym->name=%s, start=%#" PRIx64 ", addr=%#" PRIx64 ", end=%#" PRIx64 "\n",
711 __func__, __LINE__, sym->name, sym->start, addr, sym->end);
715 offset = addr - sym->start;
716 h = annotation__histogram(notes, evidx);
718 h->addr[offset].nr_samples++;
719 h->period += sample->period;
720 h->addr[offset].period += sample->period;
722 pr_debug3("%#" PRIx64 " %s: period++ [addr: %#" PRIx64 ", %#" PRIx64
723 ", evidx=%d] => nr_samples: %" PRIu64 ", period: %" PRIu64 "\n",
724 sym->start, sym->name, addr, addr - sym->start, evidx,
725 h->addr[offset].nr_samples, h->addr[offset].period);
729 static struct annotation *symbol__get_annotation(struct symbol *sym, bool cycles)
731 struct annotation *notes = symbol__annotation(sym);
733 if (notes->src == NULL) {
734 if (symbol__alloc_hist(sym) < 0)
737 if (!notes->src->cycles_hist && cycles) {
738 if (symbol__alloc_hist_cycles(sym) < 0)
744 static int symbol__inc_addr_samples(struct symbol *sym, struct map *map,
746 struct perf_sample *sample)
748 struct annotation *notes;
752 notes = symbol__get_annotation(sym, false);
755 return __symbol__inc_addr_samples(sym, map, notes, evidx, addr, sample);
758 static int symbol__account_cycles(u64 addr, u64 start,
759 struct symbol *sym, unsigned cycles)
761 struct annotation *notes;
766 notes = symbol__get_annotation(sym, true);
769 if (addr < sym->start || addr >= sym->end)
773 if (start < sym->start || start >= sym->end)
778 offset = addr - sym->start;
779 return __symbol__account_cycles(notes,
780 start ? start - sym->start : 0,
785 int addr_map_symbol__account_cycles(struct addr_map_symbol *ams,
786 struct addr_map_symbol *start,
796 * Only set start when IPC can be computed. We can only
797 * compute it when the basic block is completely in a single
799 * Special case the case when the jump is elsewhere, but
800 * it starts on the function start.
803 (start->sym == ams->sym ||
805 start->addr == ams->sym->start + ams->map->start)))
806 saddr = start->al_addr;
808 pr_debug2("BB with bad start: addr %"PRIx64" start %"PRIx64" sym %"PRIx64" saddr %"PRIx64"\n",
810 start ? start->addr : 0,
811 ams->sym ? ams->sym->start + ams->map->start : 0,
813 err = symbol__account_cycles(ams->al_addr, saddr, ams->sym, cycles);
815 pr_debug2("account_cycles failed %d\n", err);
819 int addr_map_symbol__inc_samples(struct addr_map_symbol *ams, struct perf_sample *sample,
822 return symbol__inc_addr_samples(ams->sym, ams->map, evidx, ams->al_addr, sample);
825 int hist_entry__inc_addr_samples(struct hist_entry *he, struct perf_sample *sample,
828 return symbol__inc_addr_samples(he->ms.sym, he->ms.map, evidx, ip, sample);
831 static void disasm_line__init_ins(struct disasm_line *dl, struct arch *arch, struct map *map)
833 dl->ins.ops = ins__find(arch, dl->ins.name);
838 if (dl->ins.ops->parse && dl->ins.ops->parse(arch, &dl->ops, map) < 0)
842 static int disasm_line__parse(char *line, const char **namep, char **rawp)
844 char tmp, *name = ltrim(line);
851 while ((*rawp)[0] != '\0' && !isspace((*rawp)[0]))
856 *namep = strdup(name);
862 *rawp = ltrim(*rawp);
872 static struct disasm_line *disasm_line__new(s64 offset, char *line,
873 size_t privsize, int line_nr,
877 struct disasm_line *dl = zalloc(sizeof(*dl) + privsize);
881 dl->line = strdup(line);
882 dl->line_nr = line_nr;
883 if (dl->line == NULL)
887 if (disasm_line__parse(dl->line, &dl->ins.name, &dl->ops.raw) < 0)
890 disasm_line__init_ins(dl, arch, map);
903 void disasm_line__free(struct disasm_line *dl)
906 if (dl->ins.ops && dl->ins.ops->free)
907 dl->ins.ops->free(&dl->ops);
909 ins__delete(&dl->ops);
910 free((void *)dl->ins.name);
915 int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size, bool raw)
917 if (raw || !dl->ins.ops)
918 return scnprintf(bf, size, "%-6.6s %s", dl->ins.name, dl->ops.raw);
920 return ins__scnprintf(&dl->ins, bf, size, &dl->ops);
923 static void disasm__add(struct list_head *head, struct disasm_line *line)
925 list_add_tail(&line->node, head);
928 struct disasm_line *disasm__get_next_ip_line(struct list_head *head, struct disasm_line *pos)
930 list_for_each_entry_continue(pos, head, node)
931 if (pos->offset >= 0)
937 double disasm__calc_percent(struct annotation *notes, int evidx, s64 offset,
938 s64 end, const char **path, struct sym_hist_entry *sample)
940 struct source_line *src_line = notes->src->lines;
941 double percent = 0.0;
943 sample->nr_samples = sample->period = 0;
946 size_t sizeof_src_line = sizeof(*src_line) +
947 sizeof(src_line->samples) * (src_line->nr_pcnt - 1);
949 while (offset < end) {
950 src_line = (void *)notes->src->lines +
951 (sizeof_src_line * offset);
954 *path = src_line->path;
956 percent += src_line->samples[evidx].percent;
957 sample->nr_samples += src_line->samples[evidx].nr;
961 struct sym_hist *h = annotation__histogram(notes, evidx);
962 unsigned int hits = 0;
965 while (offset < end) {
966 hits += h->addr[offset].nr_samples;
967 period += h->addr[offset].period;
972 sample->period = period;
973 sample->nr_samples = hits;
974 percent = 100.0 * hits / h->nr_samples;
981 static const char *annotate__address_color(struct block_range *br)
983 double cov = block_range__coverage(br);
986 /* mark red for >75% coverage */
988 return PERF_COLOR_RED;
990 /* mark dull for <1% coverage */
992 return PERF_COLOR_NORMAL;
995 return PERF_COLOR_MAGENTA;
998 static const char *annotate__asm_color(struct block_range *br)
1000 double cov = block_range__coverage(br);
1003 /* mark dull for <1% coverage */
1005 return PERF_COLOR_NORMAL;
1008 return PERF_COLOR_BLUE;
1011 static void annotate__branch_printf(struct block_range *br, u64 addr)
1013 bool emit_comment = true;
1019 if (br->is_target && br->start == addr) {
1020 struct block_range *branch = br;
1024 * Find matching branch to our target.
1026 while (!branch->is_branch)
1027 branch = block_range__next(branch);
1029 p = 100 *(double)br->entry / branch->coverage;
1033 emit_comment = false;
1038 * The percentage of coverage joined at this target in relation
1039 * to the next branch.
1041 printf(" +%.2f%%", p);
1045 if (br->is_branch && br->end == addr) {
1046 double p = 100*(double)br->taken / br->coverage;
1050 emit_comment = false;
1055 * The percentage of coverage leaving at this branch, and
1056 * its prediction ratio.
1058 printf(" -%.2f%% (p:%.2f%%)", p, 100*(double)br->pred / br->taken);
1064 static int disasm_line__print(struct disasm_line *dl, struct symbol *sym, u64 start,
1065 struct perf_evsel *evsel, u64 len, int min_pcnt, int printed,
1066 int max_lines, struct disasm_line *queue)
1068 static const char *prev_line;
1069 static const char *prev_color;
1071 if (dl->offset != -1) {
1072 const char *path = NULL;
1073 double percent, max_percent = 0.0;
1074 double *ppercents = &percent;
1075 struct sym_hist_entry sample;
1076 struct sym_hist_entry *psamples = &sample;
1077 int i, nr_percent = 1;
1079 struct annotation *notes = symbol__annotation(sym);
1080 s64 offset = dl->offset;
1081 const u64 addr = start + offset;
1082 struct disasm_line *next;
1083 struct block_range *br;
1085 next = disasm__get_next_ip_line(¬es->src->source, dl);
1087 if (perf_evsel__is_group_event(evsel)) {
1088 nr_percent = evsel->nr_members;
1089 ppercents = calloc(nr_percent, sizeof(double));
1090 psamples = calloc(nr_percent, sizeof(struct sym_hist_entry));
1091 if (ppercents == NULL || psamples == NULL) {
1096 for (i = 0; i < nr_percent; i++) {
1097 percent = disasm__calc_percent(notes,
1098 notes->src->lines ? i : evsel->idx + i,
1100 next ? next->offset : (s64) len,
1103 ppercents[i] = percent;
1104 psamples[i] = sample;
1105 if (percent > max_percent)
1106 max_percent = percent;
1109 if (max_percent < min_pcnt)
1112 if (max_lines && printed >= max_lines)
1115 if (queue != NULL) {
1116 list_for_each_entry_from(queue, ¬es->src->source, node) {
1119 disasm_line__print(queue, sym, start, evsel, len,
1124 color = get_percent_color(max_percent);
1127 * Also color the filename and line if needed, with
1128 * the same color than the percentage. Don't print it
1129 * twice for close colored addr with the same filename:line
1132 if (!prev_line || strcmp(prev_line, path)
1133 || color != prev_color) {
1134 color_fprintf(stdout, color, " %s", path);
1140 for (i = 0; i < nr_percent; i++) {
1141 percent = ppercents[i];
1142 sample = psamples[i];
1143 color = get_percent_color(percent);
1145 if (symbol_conf.show_total_period)
1146 color_fprintf(stdout, color, " %11" PRIu64,
1148 else if (symbol_conf.show_nr_samples)
1149 color_fprintf(stdout, color, " %7" PRIu64,
1152 color_fprintf(stdout, color, " %7.2f", percent);
1157 br = block_range__find(addr);
1158 color_fprintf(stdout, annotate__address_color(br), " %" PRIx64 ":", addr);
1159 color_fprintf(stdout, annotate__asm_color(br), "%s", dl->line);
1160 annotate__branch_printf(br, addr);
1163 if (ppercents != &percent)
1166 if (psamples != &sample)
1169 } else if (max_lines && printed >= max_lines)
1172 int width = symbol_conf.show_total_period ? 12 : 8;
1177 if (perf_evsel__is_group_event(evsel))
1178 width *= evsel->nr_members;
1181 printf(" %*s:\n", width, " ");
1183 printf(" %*s: %s\n", width, " ", dl->line);
1190 * symbol__parse_objdump_line() parses objdump output (with -d --no-show-raw)
1191 * which looks like following
1193 * 0000000000415500 <_init>:
1194 * 415500: sub $0x8,%rsp
1195 * 415504: mov 0x2f5ad5(%rip),%rax # 70afe0 <_DYNAMIC+0x2f8>
1196 * 41550b: test %rax,%rax
1197 * 41550e: je 415515 <_init+0x15>
1198 * 415510: callq 416e70 <__gmon_start__@plt>
1199 * 415515: add $0x8,%rsp
1202 * it will be parsed and saved into struct disasm_line as
1203 * <offset> <name> <ops.raw>
1205 * The offset will be a relative offset from the start of the symbol and -1
1206 * means that it's not a disassembly line so should be treated differently.
1207 * The ops.raw part will be parsed further according to type of the instruction.
1209 static int symbol__parse_objdump_line(struct symbol *sym, struct map *map,
1211 FILE *file, size_t privsize,
1214 struct annotation *notes = symbol__annotation(sym);
1215 struct disasm_line *dl;
1216 char *line = NULL, *parsed_line, *tmp, *tmp2;
1218 s64 line_ip, offset = -1;
1219 regmatch_t match[2];
1221 if (getline(&line, &line_len, file) < 0)
1228 parsed_line = rtrim(line);
1230 /* /filename:linenr ? Save line number and ignore. */
1231 if (regexec(&file_lineno, parsed_line, 2, match, 0) == 0) {
1232 *line_nr = atoi(parsed_line + match[1].rm_so);
1236 tmp = ltrim(parsed_line);
1239 * Parse hexa addresses followed by ':'
1241 line_ip = strtoull(tmp, &tmp2, 16);
1242 if (*tmp2 != ':' || tmp == tmp2 || tmp2[1] == '\0')
1246 if (line_ip != -1) {
1247 u64 start = map__rip_2objdump(map, sym->start),
1248 end = map__rip_2objdump(map, sym->end);
1250 offset = line_ip - start;
1251 if ((u64)line_ip < start || (u64)line_ip >= end)
1254 parsed_line = tmp2 + 1;
1257 dl = disasm_line__new(offset, parsed_line, privsize, *line_nr, arch, map);
1264 if (!disasm_line__has_offset(dl)) {
1265 dl->ops.target.offset = dl->ops.target.addr -
1266 map__rip_2objdump(map, sym->start);
1267 dl->ops.target.offset_avail = true;
1270 /* kcore has no symbols, so add the call target name */
1271 if (dl->ins.ops && ins__is_call(&dl->ins) && !dl->ops.target.name) {
1272 struct addr_map_symbol target = {
1274 .addr = dl->ops.target.addr,
1277 if (!map_groups__find_ams(&target) &&
1278 target.sym->start == target.al_addr)
1279 dl->ops.target.name = strdup(target.sym->name);
1282 disasm__add(¬es->src->source, dl);
1287 static __attribute__((constructor)) void symbol__init_regexpr(void)
1289 regcomp(&file_lineno, "^/[^:]+:([0-9]+)", REG_EXTENDED);
1292 static void delete_last_nop(struct symbol *sym)
1294 struct annotation *notes = symbol__annotation(sym);
1295 struct list_head *list = ¬es->src->source;
1296 struct disasm_line *dl;
1298 while (!list_empty(list)) {
1299 dl = list_entry(list->prev, struct disasm_line, node);
1302 if (dl->ins.ops != &nop_ops)
1305 if (!strstr(dl->line, " nop ") &&
1306 !strstr(dl->line, " nopl ") &&
1307 !strstr(dl->line, " nopw "))
1311 list_del(&dl->node);
1312 disasm_line__free(dl);
1316 int symbol__strerror_disassemble(struct symbol *sym __maybe_unused, struct map *map,
1317 int errnum, char *buf, size_t buflen)
1319 struct dso *dso = map->dso;
1321 BUG_ON(buflen == 0);
1324 str_error_r(errnum, buf, buflen);
1329 case SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX: {
1330 char bf[SBUILD_ID_SIZE + 15] = " with build id ";
1331 char *build_id_msg = NULL;
1333 if (dso->has_build_id) {
1334 build_id__sprintf(dso->build_id,
1335 sizeof(dso->build_id), bf + 15);
1338 scnprintf(buf, buflen,
1339 "No vmlinux file%s\nwas found in the path.\n\n"
1340 "Note that annotation using /proc/kcore requires CAP_SYS_RAWIO capability.\n\n"
1342 " perf buildid-cache -vu vmlinux\n\n"
1344 " --vmlinux vmlinux\n", build_id_msg ?: "");
1348 scnprintf(buf, buflen, "Internal error: Invalid %d error code\n", errnum);
1355 static int dso__disassemble_filename(struct dso *dso, char *filename, size_t filename_size)
1357 char linkname[PATH_MAX];
1358 char *build_id_filename;
1359 char *build_id_path = NULL;
1362 if (dso->symtab_type == DSO_BINARY_TYPE__KALLSYMS &&
1363 !dso__is_kcore(dso))
1364 return SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX;
1366 build_id_filename = dso__build_id_filename(dso, NULL, 0, false);
1367 if (build_id_filename) {
1368 __symbol__join_symfs(filename, filename_size, build_id_filename);
1369 free(build_id_filename);
1371 if (dso->has_build_id)
1376 build_id_path = strdup(filename);
1381 * old style build-id cache has name of XX/XXXXXXX.. while
1382 * new style has XX/XXXXXXX../{elf,kallsyms,vdso}.
1383 * extract the build-id part of dirname in the new style only.
1385 pos = strrchr(build_id_path, '/');
1386 if (pos && strlen(pos) < SBUILD_ID_SIZE - 2)
1387 dirname(build_id_path);
1389 if (dso__is_kcore(dso) ||
1390 readlink(build_id_path, linkname, sizeof(linkname)) < 0 ||
1391 strstr(linkname, DSO__NAME_KALLSYMS) ||
1392 access(filename, R_OK)) {
1395 * If we don't have build-ids or the build-id file isn't in the
1396 * cache, or is just a kallsyms file, well, lets hope that this
1397 * DSO is the same as when 'perf record' ran.
1399 __symbol__join_symfs(filename, filename_size, dso->long_name);
1402 free(build_id_path);
1406 static const char *annotate__norm_arch(const char *arch_name)
1410 if (!arch_name) { /* Assume we are annotating locally. */
1411 if (uname(&uts) < 0)
1413 arch_name = uts.machine;
1415 return normalize_arch((char *)arch_name);
1418 int symbol__disassemble(struct symbol *sym, struct map *map,
1419 const char *arch_name, size_t privsize,
1420 struct arch **parch, char *cpuid)
1422 struct dso *dso = map->dso;
1423 char command[PATH_MAX * 2];
1424 struct arch *arch = NULL;
1426 char symfs_filename[PATH_MAX];
1427 struct kcore_extract kce;
1428 bool delete_extract = false;
1433 int err = dso__disassemble_filename(dso, symfs_filename, sizeof(symfs_filename));
1438 arch_name = annotate__norm_arch(arch_name);
1442 arch = arch__find(arch_name);
1450 err = arch->init(arch);
1452 pr_err("%s: failed to initialize %s arch priv area\n", __func__, arch->name);
1457 if (arch->cpuid_parse && cpuid)
1458 arch->cpuid_parse(arch, cpuid);
1460 pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__,
1461 symfs_filename, sym->name, map->unmap_ip(map, sym->start),
1462 map->unmap_ip(map, sym->end));
1464 pr_debug("annotating [%p] %30s : [%p] %30s\n",
1465 dso, dso->long_name, sym, sym->name);
1467 if (dso__is_kcore(dso)) {
1468 kce.kcore_filename = symfs_filename;
1469 kce.addr = map__rip_2objdump(map, sym->start);
1470 kce.offs = sym->start;
1471 kce.len = sym->end - sym->start;
1472 if (!kcore_extract__create(&kce)) {
1473 delete_extract = true;
1474 strlcpy(symfs_filename, kce.extract_filename,
1475 sizeof(symfs_filename));
1477 } else if (dso__needs_decompress(dso)) {
1478 char tmp[KMOD_DECOMP_LEN];
1480 if (dso__decompress_kmodule_path(dso, symfs_filename,
1481 tmp, sizeof(tmp)) < 0)
1484 strcpy(symfs_filename, tmp);
1487 snprintf(command, sizeof(command),
1488 "%s %s%s --start-address=0x%016" PRIx64
1489 " --stop-address=0x%016" PRIx64
1490 " -l -d %s %s -C \"%s\" 2>/dev/null|grep -v \"%s:\"|expand",
1491 objdump_path ? objdump_path : "objdump",
1492 disassembler_style ? "-M " : "",
1493 disassembler_style ? disassembler_style : "",
1494 map__rip_2objdump(map, sym->start),
1495 map__rip_2objdump(map, sym->end),
1496 symbol_conf.annotate_asm_raw ? "" : "--no-show-raw",
1497 symbol_conf.annotate_src ? "-S" : "",
1498 symfs_filename, symfs_filename);
1500 pr_debug("Executing: %s\n", command);
1503 if (pipe(stdout_fd) < 0) {
1504 pr_err("Failure creating the pipe to run %s\n", command);
1505 goto out_remove_tmp;
1510 pr_err("Failure forking to run %s\n", command);
1511 goto out_close_stdout;
1515 close(stdout_fd[0]);
1516 dup2(stdout_fd[1], 1);
1517 close(stdout_fd[1]);
1518 execl("/bin/sh", "sh", "-c", command, NULL);
1523 close(stdout_fd[1]);
1525 file = fdopen(stdout_fd[0], "r");
1527 pr_err("Failure creating FILE stream for %s\n", command);
1529 * If we were using debug info should retry with
1532 goto out_remove_tmp;
1536 while (!feof(file)) {
1538 * The source code line number (lineno) needs to be kept in
1539 * accross calls to symbol__parse_objdump_line(), so that it
1540 * can associate it with the instructions till the next one.
1541 * See disasm_line__new() and struct disasm_line::line_nr.
1543 if (symbol__parse_objdump_line(sym, map, arch, file, privsize,
1550 pr_err("No output from %s\n", command);
1553 * kallsyms does not have symbol sizes so there may a nop at the end.
1556 if (dso__is_kcore(dso))
1557 delete_last_nop(sym);
1562 close(stdout_fd[0]);
1564 if (dso__needs_decompress(dso))
1565 unlink(symfs_filename);
1568 kcore_extract__delete(&kce);
1573 close(stdout_fd[1]);
1574 goto out_remove_tmp;
1577 static void insert_source_line(struct rb_root *root, struct source_line *src_line)
1579 struct source_line *iter;
1580 struct rb_node **p = &root->rb_node;
1581 struct rb_node *parent = NULL;
1584 while (*p != NULL) {
1586 iter = rb_entry(parent, struct source_line, node);
1588 ret = strcmp(iter->path, src_line->path);
1590 for (i = 0; i < src_line->nr_pcnt; i++)
1591 iter->samples[i].percent_sum += src_line->samples[i].percent;
1598 p = &(*p)->rb_right;
1601 for (i = 0; i < src_line->nr_pcnt; i++)
1602 src_line->samples[i].percent_sum = src_line->samples[i].percent;
1604 rb_link_node(&src_line->node, parent, p);
1605 rb_insert_color(&src_line->node, root);
1608 static int cmp_source_line(struct source_line *a, struct source_line *b)
1612 for (i = 0; i < a->nr_pcnt; i++) {
1613 if (a->samples[i].percent_sum == b->samples[i].percent_sum)
1615 return a->samples[i].percent_sum > b->samples[i].percent_sum;
1621 static void __resort_source_line(struct rb_root *root, struct source_line *src_line)
1623 struct source_line *iter;
1624 struct rb_node **p = &root->rb_node;
1625 struct rb_node *parent = NULL;
1627 while (*p != NULL) {
1629 iter = rb_entry(parent, struct source_line, node);
1631 if (cmp_source_line(src_line, iter))
1634 p = &(*p)->rb_right;
1637 rb_link_node(&src_line->node, parent, p);
1638 rb_insert_color(&src_line->node, root);
1641 static void resort_source_line(struct rb_root *dest_root, struct rb_root *src_root)
1643 struct source_line *src_line;
1644 struct rb_node *node;
1646 node = rb_first(src_root);
1648 struct rb_node *next;
1650 src_line = rb_entry(node, struct source_line, node);
1651 next = rb_next(node);
1652 rb_erase(node, src_root);
1654 __resort_source_line(dest_root, src_line);
1659 static void symbol__free_source_line(struct symbol *sym, int len)
1661 struct annotation *notes = symbol__annotation(sym);
1662 struct source_line *src_line = notes->src->lines;
1663 size_t sizeof_src_line;
1666 sizeof_src_line = sizeof(*src_line) +
1667 (sizeof(src_line->samples) * (src_line->nr_pcnt - 1));
1669 for (i = 0; i < len; i++) {
1670 free_srcline(src_line->path);
1671 src_line = (void *)src_line + sizeof_src_line;
1674 zfree(¬es->src->lines);
1677 /* Get the filename:line for the colored entries */
1678 static int symbol__get_source_line(struct symbol *sym, struct map *map,
1679 struct perf_evsel *evsel,
1680 struct rb_root *root, int len)
1684 int evidx = evsel->idx;
1685 struct source_line *src_line;
1686 struct annotation *notes = symbol__annotation(sym);
1687 struct sym_hist *h = annotation__histogram(notes, evidx);
1688 struct rb_root tmp_root = RB_ROOT;
1690 u64 nr_samples = h->nr_samples;
1691 size_t sizeof_src_line = sizeof(struct source_line);
1693 if (perf_evsel__is_group_event(evsel)) {
1694 for (i = 1; i < evsel->nr_members; i++) {
1695 h = annotation__histogram(notes, evidx + i);
1696 nr_samples += h->nr_samples;
1698 nr_pcnt = evsel->nr_members;
1699 sizeof_src_line += (nr_pcnt - 1) * sizeof(src_line->samples);
1705 src_line = notes->src->lines = calloc(len, sizeof_src_line);
1706 if (!notes->src->lines)
1709 start = map__rip_2objdump(map, sym->start);
1711 for (i = 0; i < len; i++) {
1713 double percent_max = 0.0;
1715 src_line->nr_pcnt = nr_pcnt;
1717 for (k = 0; k < nr_pcnt; k++) {
1718 double percent = 0.0;
1720 h = annotation__histogram(notes, evidx + k);
1721 nr_samples = h->addr[i].nr_samples;
1723 percent = 100.0 * nr_samples / h->nr_samples;
1725 if (percent > percent_max)
1726 percent_max = percent;
1727 src_line->samples[k].percent = percent;
1728 src_line->samples[k].nr = nr_samples;
1731 if (percent_max <= 0.5)
1735 src_line->path = get_srcline(map->dso, offset, NULL,
1737 insert_source_line(&tmp_root, src_line);
1740 src_line = (void *)src_line + sizeof_src_line;
1743 resort_source_line(root, &tmp_root);
1747 static void print_summary(struct rb_root *root, const char *filename)
1749 struct source_line *src_line;
1750 struct rb_node *node;
1752 printf("\nSorted summary for file %s\n", filename);
1753 printf("----------------------------------------------\n\n");
1755 if (RB_EMPTY_ROOT(root)) {
1756 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
1760 node = rb_first(root);
1762 double percent, percent_max = 0.0;
1767 src_line = rb_entry(node, struct source_line, node);
1768 for (i = 0; i < src_line->nr_pcnt; i++) {
1769 percent = src_line->samples[i].percent_sum;
1770 color = get_percent_color(percent);
1771 color_fprintf(stdout, color, " %7.2f", percent);
1773 if (percent > percent_max)
1774 percent_max = percent;
1777 path = src_line->path;
1778 color = get_percent_color(percent_max);
1779 color_fprintf(stdout, color, " %s\n", path);
1781 node = rb_next(node);
1785 static void symbol__annotate_hits(struct symbol *sym, struct perf_evsel *evsel)
1787 struct annotation *notes = symbol__annotation(sym);
1788 struct sym_hist *h = annotation__histogram(notes, evsel->idx);
1789 u64 len = symbol__size(sym), offset;
1791 for (offset = 0; offset < len; ++offset)
1792 if (h->addr[offset].nr_samples != 0)
1793 printf("%*" PRIx64 ": %" PRIu64 "\n", BITS_PER_LONG / 2,
1794 sym->start + offset, h->addr[offset].nr_samples);
1795 printf("%*s: %" PRIu64 "\n", BITS_PER_LONG / 2, "h->nr_samples", h->nr_samples);
1798 int symbol__annotate_printf(struct symbol *sym, struct map *map,
1799 struct perf_evsel *evsel, bool full_paths,
1800 int min_pcnt, int max_lines, int context)
1802 struct dso *dso = map->dso;
1804 const char *d_filename;
1805 const char *evsel_name = perf_evsel__name(evsel);
1806 struct annotation *notes = symbol__annotation(sym);
1807 struct sym_hist *h = annotation__histogram(notes, evsel->idx);
1808 struct disasm_line *pos, *queue = NULL;
1809 u64 start = map__rip_2objdump(map, sym->start);
1810 int printed = 2, queue_len = 0;
1813 int width = symbol_conf.show_total_period ? 12 : 8;
1814 int graph_dotted_len;
1816 filename = strdup(dso->long_name);
1821 d_filename = filename;
1823 d_filename = basename(filename);
1825 len = symbol__size(sym);
1827 if (perf_evsel__is_group_event(evsel))
1828 width *= evsel->nr_members;
1830 graph_dotted_len = printf(" %-*.*s| Source code & Disassembly of %s for %s (%" PRIu64 " samples)\n",
1831 width, width, symbol_conf.show_total_period ? "Period" :
1832 symbol_conf.show_nr_samples ? "Samples" : "Percent",
1833 d_filename, evsel_name, h->nr_samples);
1835 printf("%-*.*s----\n",
1836 graph_dotted_len, graph_dotted_len, graph_dotted_line);
1839 symbol__annotate_hits(sym, evsel);
1841 list_for_each_entry(pos, ¬es->src->source, node) {
1842 if (context && queue == NULL) {
1847 switch (disasm_line__print(pos, sym, start, evsel, len,
1848 min_pcnt, printed, max_lines,
1853 printed += queue_len;
1859 /* filtered by max_lines */
1865 * Filtered by min_pcnt or non IP lines when
1870 if (queue_len == context)
1871 queue = list_entry(queue->node.next, typeof(*queue), node);
1883 void symbol__annotate_zero_histogram(struct symbol *sym, int evidx)
1885 struct annotation *notes = symbol__annotation(sym);
1886 struct sym_hist *h = annotation__histogram(notes, evidx);
1888 memset(h, 0, notes->src->sizeof_sym_hist);
1891 void symbol__annotate_decay_histogram(struct symbol *sym, int evidx)
1893 struct annotation *notes = symbol__annotation(sym);
1894 struct sym_hist *h = annotation__histogram(notes, evidx);
1895 int len = symbol__size(sym), offset;
1898 for (offset = 0; offset < len; ++offset) {
1899 h->addr[offset].nr_samples = h->addr[offset].nr_samples * 7 / 8;
1900 h->nr_samples += h->addr[offset].nr_samples;
1904 void disasm__purge(struct list_head *head)
1906 struct disasm_line *pos, *n;
1908 list_for_each_entry_safe(pos, n, head, node) {
1909 list_del(&pos->node);
1910 disasm_line__free(pos);
1914 static size_t disasm_line__fprintf(struct disasm_line *dl, FILE *fp)
1918 if (dl->offset == -1)
1919 return fprintf(fp, "%s\n", dl->line);
1921 printed = fprintf(fp, "%#" PRIx64 " %s", dl->offset, dl->ins.name);
1923 if (dl->ops.raw[0] != '\0') {
1924 printed += fprintf(fp, "%.*s %s\n", 6 - (int)printed, " ",
1928 return printed + fprintf(fp, "\n");
1931 size_t disasm__fprintf(struct list_head *head, FILE *fp)
1933 struct disasm_line *pos;
1936 list_for_each_entry(pos, head, node)
1937 printed += disasm_line__fprintf(pos, fp);
1942 int symbol__tty_annotate(struct symbol *sym, struct map *map,
1943 struct perf_evsel *evsel, bool print_lines,
1944 bool full_paths, int min_pcnt, int max_lines)
1946 struct dso *dso = map->dso;
1947 struct rb_root source_line = RB_ROOT;
1950 if (symbol__disassemble(sym, map, perf_evsel__env_arch(evsel),
1954 len = symbol__size(sym);
1957 srcline_full_filename = full_paths;
1958 symbol__get_source_line(sym, map, evsel, &source_line, len);
1959 print_summary(&source_line, dso->long_name);
1962 symbol__annotate_printf(sym, map, evsel, full_paths,
1963 min_pcnt, max_lines, 0);
1965 symbol__free_source_line(sym, len);
1967 disasm__purge(&symbol__annotation(sym)->src->source);
1972 bool ui__has_annotation(void)
1974 return use_browser == 1 && perf_hpp_list.sym;