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;
50 int (*init)(struct arch *arch);
53 char skip_functions_char;
57 static struct ins_ops call_ops;
58 static struct ins_ops dec_ops;
59 static struct ins_ops jump_ops;
60 static struct ins_ops mov_ops;
61 static struct ins_ops nop_ops;
62 static struct ins_ops lock_ops;
63 static struct ins_ops ret_ops;
65 static int arch__grow_instructions(struct arch *arch)
67 struct ins *new_instructions;
68 size_t new_nr_allocated;
70 if (arch->nr_instructions_allocated == 0 && arch->instructions)
71 goto grow_from_non_allocated_table;
73 new_nr_allocated = arch->nr_instructions_allocated + 128;
74 new_instructions = realloc(arch->instructions, new_nr_allocated * sizeof(struct ins));
75 if (new_instructions == NULL)
78 out_update_instructions:
79 arch->instructions = new_instructions;
80 arch->nr_instructions_allocated = new_nr_allocated;
83 grow_from_non_allocated_table:
84 new_nr_allocated = arch->nr_instructions + 128;
85 new_instructions = calloc(new_nr_allocated, sizeof(struct ins));
86 if (new_instructions == NULL)
89 memcpy(new_instructions, arch->instructions, arch->nr_instructions);
90 goto out_update_instructions;
93 static int arch__associate_ins_ops(struct arch* arch, const char *name, struct ins_ops *ops)
97 if (arch->nr_instructions == arch->nr_instructions_allocated &&
98 arch__grow_instructions(arch))
101 ins = &arch->instructions[arch->nr_instructions];
102 ins->name = strdup(name);
107 arch->nr_instructions++;
113 #include "arch/arm/annotate/instructions.c"
114 #include "arch/arm64/annotate/instructions.c"
115 #include "arch/x86/annotate/instructions.c"
116 #include "arch/powerpc/annotate/instructions.c"
117 #include "arch/s390/annotate/instructions.c"
119 static struct arch architectures[] = {
122 .init = arm__annotate_init,
126 .init = arm64__annotate_init,
130 .instructions = x86__instructions,
131 .nr_instructions = ARRAY_SIZE(x86__instructions),
138 .init = powerpc__annotate_init,
142 .init = s390__annotate_init,
149 static void ins__delete(struct ins_operands *ops)
153 zfree(&ops->source.raw);
154 zfree(&ops->source.name);
155 zfree(&ops->target.raw);
156 zfree(&ops->target.name);
159 static int ins__raw_scnprintf(struct ins *ins, char *bf, size_t size,
160 struct ins_operands *ops)
162 return scnprintf(bf, size, "%-6.6s %s", ins->name, ops->raw);
165 int ins__scnprintf(struct ins *ins, char *bf, size_t size,
166 struct ins_operands *ops)
168 if (ins->ops->scnprintf)
169 return ins->ops->scnprintf(ins, bf, size, ops);
171 return ins__raw_scnprintf(ins, bf, size, ops);
174 static int call__parse(struct arch *arch, struct ins_operands *ops, struct map *map)
176 char *endptr, *tok, *name;
178 ops->target.addr = strtoull(ops->raw, &endptr, 16);
180 name = strchr(endptr, '<');
186 if (arch->objdump.skip_functions_char &&
187 strchr(name, arch->objdump.skip_functions_char))
190 tok = strchr(name, '>');
195 ops->target.name = strdup(name);
198 return ops->target.name == NULL ? -1 : 0;
201 tok = strchr(endptr, '*');
203 struct symbol *sym = map__find_symbol(map, map->map_ip(map, ops->target.addr));
205 ops->target.name = strdup(sym->name);
207 ops->target.addr = 0;
211 ops->target.addr = strtoull(tok + 1, NULL, 16);
215 static int call__scnprintf(struct ins *ins, char *bf, size_t size,
216 struct ins_operands *ops)
218 if (ops->target.name)
219 return scnprintf(bf, size, "%-6.6s %s", ins->name, ops->target.name);
221 if (ops->target.addr == 0)
222 return ins__raw_scnprintf(ins, bf, size, ops);
224 return scnprintf(bf, size, "%-6.6s *%" PRIx64, ins->name, ops->target.addr);
227 static struct ins_ops call_ops = {
228 .parse = call__parse,
229 .scnprintf = call__scnprintf,
232 bool ins__is_call(const struct ins *ins)
234 return ins->ops == &call_ops;
237 static int jump__parse(struct arch *arch __maybe_unused, struct ins_operands *ops, struct map *map __maybe_unused)
239 const char *s = strchr(ops->raw, '+');
240 const char *c = strchr(ops->raw, ',');
243 ops->target.addr = strtoull(c, NULL, 16);
245 ops->target.addr = strtoull(ops->raw, NULL, 16);
248 ops->target.offset = strtoull(s, NULL, 16);
249 ops->target.offset_avail = true;
251 ops->target.offset_avail = false;
257 static int jump__scnprintf(struct ins *ins, char *bf, size_t size,
258 struct ins_operands *ops)
260 if (!ops->target.addr || ops->target.offset < 0)
261 return ins__raw_scnprintf(ins, bf, size, ops);
263 return scnprintf(bf, size, "%-6.6s %" PRIx64, ins->name, ops->target.offset);
266 static struct ins_ops jump_ops = {
267 .parse = jump__parse,
268 .scnprintf = jump__scnprintf,
271 bool ins__is_jump(const struct ins *ins)
273 return ins->ops == &jump_ops;
276 static int comment__symbol(char *raw, char *comment, u64 *addrp, char **namep)
278 char *endptr, *name, *t;
280 if (strstr(raw, "(%rip)") == NULL)
283 *addrp = strtoull(comment, &endptr, 16);
284 name = strchr(endptr, '<');
290 t = strchr(name, '>');
295 *namep = strdup(name);
301 static int lock__parse(struct arch *arch, struct ins_operands *ops, struct map *map)
303 ops->locked.ops = zalloc(sizeof(*ops->locked.ops));
304 if (ops->locked.ops == NULL)
307 if (disasm_line__parse(ops->raw, &ops->locked.ins.name, &ops->locked.ops->raw) < 0)
310 ops->locked.ins.ops = ins__find(arch, ops->locked.ins.name);
312 if (ops->locked.ins.ops == NULL)
315 if (ops->locked.ins.ops->parse &&
316 ops->locked.ins.ops->parse(arch, ops->locked.ops, map) < 0)
322 zfree(&ops->locked.ops);
326 static int lock__scnprintf(struct ins *ins, char *bf, size_t size,
327 struct ins_operands *ops)
331 if (ops->locked.ins.ops == NULL)
332 return ins__raw_scnprintf(ins, bf, size, ops);
334 printed = scnprintf(bf, size, "%-6.6s ", ins->name);
335 return printed + ins__scnprintf(&ops->locked.ins, bf + printed,
336 size - printed, ops->locked.ops);
339 static void lock__delete(struct ins_operands *ops)
341 struct ins *ins = &ops->locked.ins;
343 if (ins->ops && ins->ops->free)
344 ins->ops->free(ops->locked.ops);
346 ins__delete(ops->locked.ops);
348 zfree(&ops->locked.ops);
349 zfree(&ops->target.raw);
350 zfree(&ops->target.name);
353 static struct ins_ops lock_ops = {
354 .free = lock__delete,
355 .parse = lock__parse,
356 .scnprintf = lock__scnprintf,
359 static int mov__parse(struct arch *arch, struct ins_operands *ops, struct map *map __maybe_unused)
361 char *s = strchr(ops->raw, ','), *target, *comment, prev;
367 ops->source.raw = strdup(ops->raw);
370 if (ops->source.raw == NULL)
374 comment = strchr(s, arch->objdump.comment_char);
379 s = strchr(s, '\0') - 1;
381 while (s > target && isspace(s[0]))
387 ops->target.raw = strdup(target);
390 if (ops->target.raw == NULL)
391 goto out_free_source;
396 comment = ltrim(comment);
397 comment__symbol(ops->source.raw, comment, &ops->source.addr, &ops->source.name);
398 comment__symbol(ops->target.raw, comment, &ops->target.addr, &ops->target.name);
403 zfree(&ops->source.raw);
407 static int mov__scnprintf(struct ins *ins, char *bf, size_t size,
408 struct ins_operands *ops)
410 return scnprintf(bf, size, "%-6.6s %s,%s", ins->name,
411 ops->source.name ?: ops->source.raw,
412 ops->target.name ?: ops->target.raw);
415 static struct ins_ops mov_ops = {
417 .scnprintf = mov__scnprintf,
420 static int dec__parse(struct arch *arch __maybe_unused, struct ins_operands *ops, struct map *map __maybe_unused)
422 char *target, *comment, *s, prev;
424 target = s = ops->raw;
426 while (s[0] != '\0' && !isspace(s[0]))
431 ops->target.raw = strdup(target);
434 if (ops->target.raw == NULL)
437 comment = strchr(s, arch->objdump.comment_char);
441 comment = ltrim(comment);
442 comment__symbol(ops->target.raw, comment, &ops->target.addr, &ops->target.name);
447 static int dec__scnprintf(struct ins *ins, char *bf, size_t size,
448 struct ins_operands *ops)
450 return scnprintf(bf, size, "%-6.6s %s", ins->name,
451 ops->target.name ?: ops->target.raw);
454 static struct ins_ops dec_ops = {
456 .scnprintf = dec__scnprintf,
459 static int nop__scnprintf(struct ins *ins __maybe_unused, char *bf, size_t size,
460 struct ins_operands *ops __maybe_unused)
462 return scnprintf(bf, size, "%-6.6s", "nop");
465 static struct ins_ops nop_ops = {
466 .scnprintf = nop__scnprintf,
469 static struct ins_ops ret_ops = {
470 .scnprintf = ins__raw_scnprintf,
473 bool ins__is_ret(const struct ins *ins)
475 return ins->ops == &ret_ops;
478 static int ins__key_cmp(const void *name, const void *insp)
480 const struct ins *ins = insp;
482 return strcmp(name, ins->name);
485 static int ins__cmp(const void *a, const void *b)
487 const struct ins *ia = a;
488 const struct ins *ib = b;
490 return strcmp(ia->name, ib->name);
493 static void ins__sort(struct arch *arch)
495 const int nmemb = arch->nr_instructions;
497 qsort(arch->instructions, nmemb, sizeof(struct ins), ins__cmp);
500 static struct ins_ops *__ins__find(struct arch *arch, const char *name)
503 const int nmemb = arch->nr_instructions;
505 if (!arch->sorted_instructions) {
507 arch->sorted_instructions = true;
510 ins = bsearch(name, arch->instructions, nmemb, sizeof(struct ins), ins__key_cmp);
511 return ins ? ins->ops : NULL;
514 static struct ins_ops *ins__find(struct arch *arch, const char *name)
516 struct ins_ops *ops = __ins__find(arch, name);
518 if (!ops && arch->associate_instruction_ops)
519 ops = arch->associate_instruction_ops(arch, name);
524 static int arch__key_cmp(const void *name, const void *archp)
526 const struct arch *arch = archp;
528 return strcmp(name, arch->name);
531 static int arch__cmp(const void *a, const void *b)
533 const struct arch *aa = a;
534 const struct arch *ab = b;
536 return strcmp(aa->name, ab->name);
539 static void arch__sort(void)
541 const int nmemb = ARRAY_SIZE(architectures);
543 qsort(architectures, nmemb, sizeof(struct arch), arch__cmp);
546 static struct arch *arch__find(const char *name)
548 const int nmemb = ARRAY_SIZE(architectures);
556 return bsearch(name, architectures, nmemb, sizeof(struct arch), arch__key_cmp);
559 int symbol__alloc_hist(struct symbol *sym)
561 struct annotation *notes = symbol__annotation(sym);
562 const size_t size = symbol__size(sym);
563 size_t sizeof_sym_hist;
565 /* Check for overflow when calculating sizeof_sym_hist */
566 if (size > (SIZE_MAX - sizeof(struct sym_hist)) / sizeof(u64))
569 sizeof_sym_hist = (sizeof(struct sym_hist) + size * sizeof(u64));
571 /* Check for overflow in zalloc argument */
572 if (sizeof_sym_hist > (SIZE_MAX - sizeof(*notes->src))
573 / symbol_conf.nr_events)
576 notes->src = zalloc(sizeof(*notes->src) + symbol_conf.nr_events * sizeof_sym_hist);
577 if (notes->src == NULL)
579 notes->src->sizeof_sym_hist = sizeof_sym_hist;
580 notes->src->nr_histograms = symbol_conf.nr_events;
581 INIT_LIST_HEAD(¬es->src->source);
585 /* The cycles histogram is lazily allocated. */
586 static int symbol__alloc_hist_cycles(struct symbol *sym)
588 struct annotation *notes = symbol__annotation(sym);
589 const size_t size = symbol__size(sym);
591 notes->src->cycles_hist = calloc(size, sizeof(struct cyc_hist));
592 if (notes->src->cycles_hist == NULL)
597 void symbol__annotate_zero_histograms(struct symbol *sym)
599 struct annotation *notes = symbol__annotation(sym);
601 pthread_mutex_lock(¬es->lock);
602 if (notes->src != NULL) {
603 memset(notes->src->histograms, 0,
604 notes->src->nr_histograms * notes->src->sizeof_sym_hist);
605 if (notes->src->cycles_hist)
606 memset(notes->src->cycles_hist, 0,
607 symbol__size(sym) * sizeof(struct cyc_hist));
609 pthread_mutex_unlock(¬es->lock);
612 static int __symbol__account_cycles(struct annotation *notes,
614 unsigned offset, unsigned cycles,
619 ch = notes->src->cycles_hist;
621 * For now we can only account one basic block per
622 * final jump. But multiple could be overlapping.
623 * Always account the longest one. So when
624 * a shorter one has been already seen throw it away.
626 * We separately always account the full cycles.
628 ch[offset].num_aggr++;
629 ch[offset].cycles_aggr += cycles;
631 if (!have_start && ch[offset].have_start)
633 if (ch[offset].num) {
634 if (have_start && (!ch[offset].have_start ||
635 ch[offset].start > start)) {
636 ch[offset].have_start = 0;
637 ch[offset].cycles = 0;
639 if (ch[offset].reset < 0xffff)
641 } else if (have_start &&
642 ch[offset].start < start)
645 ch[offset].have_start = have_start;
646 ch[offset].start = start;
647 ch[offset].cycles += cycles;
652 static int __symbol__inc_addr_samples(struct symbol *sym, struct map *map,
653 struct annotation *notes, int evidx, u64 addr)
658 pr_debug3("%s: addr=%#" PRIx64 "\n", __func__, map->unmap_ip(map, addr));
660 if ((addr < sym->start || addr >= sym->end) &&
661 (addr != sym->end || sym->start != sym->end)) {
662 pr_debug("%s(%d): ERANGE! sym->name=%s, start=%#" PRIx64 ", addr=%#" PRIx64 ", end=%#" PRIx64 "\n",
663 __func__, __LINE__, sym->name, sym->start, addr, sym->end);
667 offset = addr - sym->start;
668 h = annotation__histogram(notes, evidx);
672 pr_debug3("%#" PRIx64 " %s: period++ [addr: %#" PRIx64 ", %#" PRIx64
673 ", evidx=%d] => %" PRIu64 "\n", sym->start, sym->name,
674 addr, addr - sym->start, evidx, h->addr[offset]);
678 static struct annotation *symbol__get_annotation(struct symbol *sym, bool cycles)
680 struct annotation *notes = symbol__annotation(sym);
682 if (notes->src == NULL) {
683 if (symbol__alloc_hist(sym) < 0)
686 if (!notes->src->cycles_hist && cycles) {
687 if (symbol__alloc_hist_cycles(sym) < 0)
693 static int symbol__inc_addr_samples(struct symbol *sym, struct map *map,
696 struct annotation *notes;
700 notes = symbol__get_annotation(sym, false);
703 return __symbol__inc_addr_samples(sym, map, notes, evidx, addr);
706 static int symbol__account_cycles(u64 addr, u64 start,
707 struct symbol *sym, unsigned cycles)
709 struct annotation *notes;
714 notes = symbol__get_annotation(sym, true);
717 if (addr < sym->start || addr >= sym->end)
721 if (start < sym->start || start >= sym->end)
726 offset = addr - sym->start;
727 return __symbol__account_cycles(notes,
728 start ? start - sym->start : 0,
733 int addr_map_symbol__account_cycles(struct addr_map_symbol *ams,
734 struct addr_map_symbol *start,
744 * Only set start when IPC can be computed. We can only
745 * compute it when the basic block is completely in a single
747 * Special case the case when the jump is elsewhere, but
748 * it starts on the function start.
751 (start->sym == ams->sym ||
753 start->addr == ams->sym->start + ams->map->start)))
754 saddr = start->al_addr;
756 pr_debug2("BB with bad start: addr %"PRIx64" start %"PRIx64" sym %"PRIx64" saddr %"PRIx64"\n",
758 start ? start->addr : 0,
759 ams->sym ? ams->sym->start + ams->map->start : 0,
761 err = symbol__account_cycles(ams->al_addr, saddr, ams->sym, cycles);
763 pr_debug2("account_cycles failed %d\n", err);
767 int addr_map_symbol__inc_samples(struct addr_map_symbol *ams, int evidx)
769 return symbol__inc_addr_samples(ams->sym, ams->map, evidx, ams->al_addr);
772 int hist_entry__inc_addr_samples(struct hist_entry *he, int evidx, u64 ip)
774 return symbol__inc_addr_samples(he->ms.sym, he->ms.map, evidx, ip);
777 static void disasm_line__init_ins(struct disasm_line *dl, struct arch *arch, struct map *map)
779 dl->ins.ops = ins__find(arch, dl->ins.name);
784 if (dl->ins.ops->parse && dl->ins.ops->parse(arch, &dl->ops, map) < 0)
788 static int disasm_line__parse(char *line, const char **namep, char **rawp)
790 char tmp, *name = ltrim(line);
797 while ((*rawp)[0] != '\0' && !isspace((*rawp)[0]))
802 *namep = strdup(name);
808 *rawp = ltrim(*rawp);
818 static struct disasm_line *disasm_line__new(s64 offset, char *line,
819 size_t privsize, int line_nr,
823 struct disasm_line *dl = zalloc(sizeof(*dl) + privsize);
827 dl->line = strdup(line);
828 dl->line_nr = line_nr;
829 if (dl->line == NULL)
833 if (disasm_line__parse(dl->line, &dl->ins.name, &dl->ops.raw) < 0)
836 disasm_line__init_ins(dl, arch, map);
849 void disasm_line__free(struct disasm_line *dl)
852 if (dl->ins.ops && dl->ins.ops->free)
853 dl->ins.ops->free(&dl->ops);
855 ins__delete(&dl->ops);
856 free((void *)dl->ins.name);
861 int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size, bool raw)
863 if (raw || !dl->ins.ops)
864 return scnprintf(bf, size, "%-6.6s %s", dl->ins.name, dl->ops.raw);
866 return ins__scnprintf(&dl->ins, bf, size, &dl->ops);
869 static void disasm__add(struct list_head *head, struct disasm_line *line)
871 list_add_tail(&line->node, head);
874 struct disasm_line *disasm__get_next_ip_line(struct list_head *head, struct disasm_line *pos)
876 list_for_each_entry_continue(pos, head, node)
877 if (pos->offset >= 0)
883 double disasm__calc_percent(struct annotation *notes, int evidx, s64 offset,
884 s64 end, const char **path, u64 *nr_samples)
886 struct source_line *src_line = notes->src->lines;
887 double percent = 0.0;
891 size_t sizeof_src_line = sizeof(*src_line) +
892 sizeof(src_line->samples) * (src_line->nr_pcnt - 1);
894 while (offset < end) {
895 src_line = (void *)notes->src->lines +
896 (sizeof_src_line * offset);
899 *path = src_line->path;
901 percent += src_line->samples[evidx].percent;
902 *nr_samples += src_line->samples[evidx].nr;
906 struct sym_hist *h = annotation__histogram(notes, evidx);
907 unsigned int hits = 0;
910 hits += h->addr[offset++];
914 percent = 100.0 * hits / h->sum;
921 static const char *annotate__address_color(struct block_range *br)
923 double cov = block_range__coverage(br);
926 /* mark red for >75% coverage */
928 return PERF_COLOR_RED;
930 /* mark dull for <1% coverage */
932 return PERF_COLOR_NORMAL;
935 return PERF_COLOR_MAGENTA;
938 static const char *annotate__asm_color(struct block_range *br)
940 double cov = block_range__coverage(br);
943 /* mark dull for <1% coverage */
945 return PERF_COLOR_NORMAL;
948 return PERF_COLOR_BLUE;
951 static void annotate__branch_printf(struct block_range *br, u64 addr)
953 bool emit_comment = true;
959 if (br->is_target && br->start == addr) {
960 struct block_range *branch = br;
964 * Find matching branch to our target.
966 while (!branch->is_branch)
967 branch = block_range__next(branch);
969 p = 100 *(double)br->entry / branch->coverage;
973 emit_comment = false;
978 * The percentage of coverage joined at this target in relation
979 * to the next branch.
981 printf(" +%.2f%%", p);
985 if (br->is_branch && br->end == addr) {
986 double p = 100*(double)br->taken / br->coverage;
990 emit_comment = false;
995 * The percentage of coverage leaving at this branch, and
996 * its prediction ratio.
998 printf(" -%.2f%% (p:%.2f%%)", p, 100*(double)br->pred / br->taken);
1004 static int disasm_line__print(struct disasm_line *dl, struct symbol *sym, u64 start,
1005 struct perf_evsel *evsel, u64 len, int min_pcnt, int printed,
1006 int max_lines, struct disasm_line *queue)
1008 static const char *prev_line;
1009 static const char *prev_color;
1011 if (dl->offset != -1) {
1012 const char *path = NULL;
1014 double percent, max_percent = 0.0;
1015 double *ppercents = &percent;
1016 u64 *psamples = &nr_samples;
1017 int i, nr_percent = 1;
1019 struct annotation *notes = symbol__annotation(sym);
1020 s64 offset = dl->offset;
1021 const u64 addr = start + offset;
1022 struct disasm_line *next;
1023 struct block_range *br;
1025 next = disasm__get_next_ip_line(¬es->src->source, dl);
1027 if (perf_evsel__is_group_event(evsel)) {
1028 nr_percent = evsel->nr_members;
1029 ppercents = calloc(nr_percent, sizeof(double));
1030 psamples = calloc(nr_percent, sizeof(u64));
1031 if (ppercents == NULL || psamples == NULL) {
1036 for (i = 0; i < nr_percent; i++) {
1037 percent = disasm__calc_percent(notes,
1038 notes->src->lines ? i : evsel->idx + i,
1040 next ? next->offset : (s64) len,
1041 &path, &nr_samples);
1043 ppercents[i] = percent;
1044 psamples[i] = nr_samples;
1045 if (percent > max_percent)
1046 max_percent = percent;
1049 if (max_percent < min_pcnt)
1052 if (max_lines && printed >= max_lines)
1055 if (queue != NULL) {
1056 list_for_each_entry_from(queue, ¬es->src->source, node) {
1059 disasm_line__print(queue, sym, start, evsel, len,
1064 color = get_percent_color(max_percent);
1067 * Also color the filename and line if needed, with
1068 * the same color than the percentage. Don't print it
1069 * twice for close colored addr with the same filename:line
1072 if (!prev_line || strcmp(prev_line, path)
1073 || color != prev_color) {
1074 color_fprintf(stdout, color, " %s", path);
1080 for (i = 0; i < nr_percent; i++) {
1081 percent = ppercents[i];
1082 nr_samples = psamples[i];
1083 color = get_percent_color(percent);
1085 if (symbol_conf.show_total_period)
1086 color_fprintf(stdout, color, " %7" PRIu64,
1089 color_fprintf(stdout, color, " %7.2f", percent);
1094 br = block_range__find(addr);
1095 color_fprintf(stdout, annotate__address_color(br), " %" PRIx64 ":", addr);
1096 color_fprintf(stdout, annotate__asm_color(br), "%s", dl->line);
1097 annotate__branch_printf(br, addr);
1100 if (ppercents != &percent)
1103 if (psamples != &nr_samples)
1106 } else if (max_lines && printed >= max_lines)
1114 if (perf_evsel__is_group_event(evsel))
1115 width *= evsel->nr_members;
1118 printf(" %*s:\n", width, " ");
1120 printf(" %*s: %s\n", width, " ", dl->line);
1127 * symbol__parse_objdump_line() parses objdump output (with -d --no-show-raw)
1128 * which looks like following
1130 * 0000000000415500 <_init>:
1131 * 415500: sub $0x8,%rsp
1132 * 415504: mov 0x2f5ad5(%rip),%rax # 70afe0 <_DYNAMIC+0x2f8>
1133 * 41550b: test %rax,%rax
1134 * 41550e: je 415515 <_init+0x15>
1135 * 415510: callq 416e70 <__gmon_start__@plt>
1136 * 415515: add $0x8,%rsp
1139 * it will be parsed and saved into struct disasm_line as
1140 * <offset> <name> <ops.raw>
1142 * The offset will be a relative offset from the start of the symbol and -1
1143 * means that it's not a disassembly line so should be treated differently.
1144 * The ops.raw part will be parsed further according to type of the instruction.
1146 static int symbol__parse_objdump_line(struct symbol *sym, struct map *map,
1148 FILE *file, size_t privsize,
1151 struct annotation *notes = symbol__annotation(sym);
1152 struct disasm_line *dl;
1153 char *line = NULL, *parsed_line, *tmp, *tmp2;
1155 s64 line_ip, offset = -1;
1156 regmatch_t match[2];
1158 if (getline(&line, &line_len, file) < 0)
1165 parsed_line = rtrim(line);
1167 /* /filename:linenr ? Save line number and ignore. */
1168 if (regexec(&file_lineno, parsed_line, 2, match, 0) == 0) {
1169 *line_nr = atoi(parsed_line + match[1].rm_so);
1173 tmp = ltrim(parsed_line);
1176 * Parse hexa addresses followed by ':'
1178 line_ip = strtoull(tmp, &tmp2, 16);
1179 if (*tmp2 != ':' || tmp == tmp2 || tmp2[1] == '\0')
1183 if (line_ip != -1) {
1184 u64 start = map__rip_2objdump(map, sym->start),
1185 end = map__rip_2objdump(map, sym->end);
1187 offset = line_ip - start;
1188 if ((u64)line_ip < start || (u64)line_ip >= end)
1191 parsed_line = tmp2 + 1;
1194 dl = disasm_line__new(offset, parsed_line, privsize, *line_nr, arch, map);
1201 if (!disasm_line__has_offset(dl)) {
1202 dl->ops.target.offset = dl->ops.target.addr -
1203 map__rip_2objdump(map, sym->start);
1204 dl->ops.target.offset_avail = true;
1207 /* kcore has no symbols, so add the call target name */
1208 if (dl->ins.ops && ins__is_call(&dl->ins) && !dl->ops.target.name) {
1209 struct addr_map_symbol target = {
1211 .addr = dl->ops.target.addr,
1214 if (!map_groups__find_ams(&target) &&
1215 target.sym->start == target.al_addr)
1216 dl->ops.target.name = strdup(target.sym->name);
1219 disasm__add(¬es->src->source, dl);
1224 static __attribute__((constructor)) void symbol__init_regexpr(void)
1226 regcomp(&file_lineno, "^/[^:]+:([0-9]+)", REG_EXTENDED);
1229 static void delete_last_nop(struct symbol *sym)
1231 struct annotation *notes = symbol__annotation(sym);
1232 struct list_head *list = ¬es->src->source;
1233 struct disasm_line *dl;
1235 while (!list_empty(list)) {
1236 dl = list_entry(list->prev, struct disasm_line, node);
1239 if (dl->ins.ops != &nop_ops)
1242 if (!strstr(dl->line, " nop ") &&
1243 !strstr(dl->line, " nopl ") &&
1244 !strstr(dl->line, " nopw "))
1248 list_del(&dl->node);
1249 disasm_line__free(dl);
1253 int symbol__strerror_disassemble(struct symbol *sym __maybe_unused, struct map *map,
1254 int errnum, char *buf, size_t buflen)
1256 struct dso *dso = map->dso;
1258 BUG_ON(buflen == 0);
1261 str_error_r(errnum, buf, buflen);
1266 case SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX: {
1267 char bf[SBUILD_ID_SIZE + 15] = " with build id ";
1268 char *build_id_msg = NULL;
1270 if (dso->has_build_id) {
1271 build_id__sprintf(dso->build_id,
1272 sizeof(dso->build_id), bf + 15);
1275 scnprintf(buf, buflen,
1276 "No vmlinux file%s\nwas found in the path.\n\n"
1277 "Note that annotation using /proc/kcore requires CAP_SYS_RAWIO capability.\n\n"
1279 " perf buildid-cache -vu vmlinux\n\n"
1281 " --vmlinux vmlinux\n", build_id_msg ?: "");
1285 scnprintf(buf, buflen, "Internal error: Invalid %d error code\n", errnum);
1292 static int dso__disassemble_filename(struct dso *dso, char *filename, size_t filename_size)
1294 char linkname[PATH_MAX];
1295 char *build_id_filename;
1296 char *build_id_path = NULL;
1298 if (dso->symtab_type == DSO_BINARY_TYPE__KALLSYMS &&
1299 !dso__is_kcore(dso))
1300 return SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX;
1302 build_id_filename = dso__build_id_filename(dso, NULL, 0);
1303 if (build_id_filename) {
1304 __symbol__join_symfs(filename, filename_size, build_id_filename);
1305 free(build_id_filename);
1307 if (dso->has_build_id)
1312 build_id_path = strdup(filename);
1316 dirname(build_id_path);
1318 if (dso__is_kcore(dso) ||
1319 readlink(build_id_path, linkname, sizeof(linkname)) < 0 ||
1320 strstr(linkname, DSO__NAME_KALLSYMS) ||
1321 access(filename, R_OK)) {
1324 * If we don't have build-ids or the build-id file isn't in the
1325 * cache, or is just a kallsyms file, well, lets hope that this
1326 * DSO is the same as when 'perf record' ran.
1328 __symbol__join_symfs(filename, filename_size, dso->long_name);
1331 free(build_id_path);
1335 static const char *annotate__norm_arch(const char *arch_name)
1339 if (!arch_name) { /* Assume we are annotating locally. */
1340 if (uname(&uts) < 0)
1342 arch_name = uts.machine;
1344 return normalize_arch((char *)arch_name);
1347 int symbol__disassemble(struct symbol *sym, struct map *map, const char *arch_name, size_t privsize)
1349 struct dso *dso = map->dso;
1350 char command[PATH_MAX * 2];
1351 struct arch *arch = NULL;
1353 char symfs_filename[PATH_MAX];
1354 struct kcore_extract kce;
1355 bool delete_extract = false;
1360 int err = dso__disassemble_filename(dso, symfs_filename, sizeof(symfs_filename));
1365 arch_name = annotate__norm_arch(arch_name);
1369 arch = arch__find(arch_name);
1374 err = arch->init(arch);
1376 pr_err("%s: failed to initialize %s arch priv area\n", __func__, arch->name);
1381 pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__,
1382 symfs_filename, sym->name, map->unmap_ip(map, sym->start),
1383 map->unmap_ip(map, sym->end));
1385 pr_debug("annotating [%p] %30s : [%p] %30s\n",
1386 dso, dso->long_name, sym, sym->name);
1388 if (dso__is_kcore(dso)) {
1389 kce.kcore_filename = symfs_filename;
1390 kce.addr = map__rip_2objdump(map, sym->start);
1391 kce.offs = sym->start;
1392 kce.len = sym->end - sym->start;
1393 if (!kcore_extract__create(&kce)) {
1394 delete_extract = true;
1395 strlcpy(symfs_filename, kce.extract_filename,
1396 sizeof(symfs_filename));
1398 } else if (dso__needs_decompress(dso)) {
1404 if (kmod_path__parse_ext(&m, symfs_filename))
1407 snprintf(tmp, PATH_MAX, "/tmp/perf-kmod-XXXXXX");
1415 ret = decompress_to_file(m.ext, symfs_filename, fd);
1418 pr_err("Cannot decompress %s %s\n", m.ext, symfs_filename);
1426 strcpy(symfs_filename, tmp);
1429 snprintf(command, sizeof(command),
1430 "%s %s%s --start-address=0x%016" PRIx64
1431 " --stop-address=0x%016" PRIx64
1432 " -l -d %s %s -C %s 2>/dev/null|grep -v %s:|expand",
1433 objdump_path ? objdump_path : "objdump",
1434 disassembler_style ? "-M " : "",
1435 disassembler_style ? disassembler_style : "",
1436 map__rip_2objdump(map, sym->start),
1437 map__rip_2objdump(map, sym->end),
1438 symbol_conf.annotate_asm_raw ? "" : "--no-show-raw",
1439 symbol_conf.annotate_src ? "-S" : "",
1440 symfs_filename, symfs_filename);
1442 pr_debug("Executing: %s\n", command);
1445 if (pipe(stdout_fd) < 0) {
1446 pr_err("Failure creating the pipe to run %s\n", command);
1447 goto out_remove_tmp;
1452 pr_err("Failure forking to run %s\n", command);
1453 goto out_close_stdout;
1457 close(stdout_fd[0]);
1458 dup2(stdout_fd[1], 1);
1459 close(stdout_fd[1]);
1460 execl("/bin/sh", "sh", "-c", command, NULL);
1465 close(stdout_fd[1]);
1467 file = fdopen(stdout_fd[0], "r");
1469 pr_err("Failure creating FILE stream for %s\n", command);
1471 * If we were using debug info should retry with
1474 goto out_remove_tmp;
1478 while (!feof(file)) {
1480 * The source code line number (lineno) needs to be kept in
1481 * accross calls to symbol__parse_objdump_line(), so that it
1482 * can associate it with the instructions till the next one.
1483 * See disasm_line__new() and struct disasm_line::line_nr.
1485 if (symbol__parse_objdump_line(sym, map, arch, file, privsize,
1492 pr_err("No output from %s\n", command);
1495 * kallsyms does not have symbol sizes so there may a nop at the end.
1498 if (dso__is_kcore(dso))
1499 delete_last_nop(sym);
1504 close(stdout_fd[0]);
1506 if (dso__needs_decompress(dso))
1507 unlink(symfs_filename);
1510 kcore_extract__delete(&kce);
1515 close(stdout_fd[1]);
1516 goto out_remove_tmp;
1519 static void insert_source_line(struct rb_root *root, struct source_line *src_line)
1521 struct source_line *iter;
1522 struct rb_node **p = &root->rb_node;
1523 struct rb_node *parent = NULL;
1526 while (*p != NULL) {
1528 iter = rb_entry(parent, struct source_line, node);
1530 ret = strcmp(iter->path, src_line->path);
1532 for (i = 0; i < src_line->nr_pcnt; i++)
1533 iter->samples[i].percent_sum += src_line->samples[i].percent;
1540 p = &(*p)->rb_right;
1543 for (i = 0; i < src_line->nr_pcnt; i++)
1544 src_line->samples[i].percent_sum = src_line->samples[i].percent;
1546 rb_link_node(&src_line->node, parent, p);
1547 rb_insert_color(&src_line->node, root);
1550 static int cmp_source_line(struct source_line *a, struct source_line *b)
1554 for (i = 0; i < a->nr_pcnt; i++) {
1555 if (a->samples[i].percent_sum == b->samples[i].percent_sum)
1557 return a->samples[i].percent_sum > b->samples[i].percent_sum;
1563 static void __resort_source_line(struct rb_root *root, struct source_line *src_line)
1565 struct source_line *iter;
1566 struct rb_node **p = &root->rb_node;
1567 struct rb_node *parent = NULL;
1569 while (*p != NULL) {
1571 iter = rb_entry(parent, struct source_line, node);
1573 if (cmp_source_line(src_line, iter))
1576 p = &(*p)->rb_right;
1579 rb_link_node(&src_line->node, parent, p);
1580 rb_insert_color(&src_line->node, root);
1583 static void resort_source_line(struct rb_root *dest_root, struct rb_root *src_root)
1585 struct source_line *src_line;
1586 struct rb_node *node;
1588 node = rb_first(src_root);
1590 struct rb_node *next;
1592 src_line = rb_entry(node, struct source_line, node);
1593 next = rb_next(node);
1594 rb_erase(node, src_root);
1596 __resort_source_line(dest_root, src_line);
1601 static void symbol__free_source_line(struct symbol *sym, int len)
1603 struct annotation *notes = symbol__annotation(sym);
1604 struct source_line *src_line = notes->src->lines;
1605 size_t sizeof_src_line;
1608 sizeof_src_line = sizeof(*src_line) +
1609 (sizeof(src_line->samples) * (src_line->nr_pcnt - 1));
1611 for (i = 0; i < len; i++) {
1612 free_srcline(src_line->path);
1613 src_line = (void *)src_line + sizeof_src_line;
1616 zfree(¬es->src->lines);
1619 /* Get the filename:line for the colored entries */
1620 static int symbol__get_source_line(struct symbol *sym, struct map *map,
1621 struct perf_evsel *evsel,
1622 struct rb_root *root, int len)
1626 int evidx = evsel->idx;
1627 struct source_line *src_line;
1628 struct annotation *notes = symbol__annotation(sym);
1629 struct sym_hist *h = annotation__histogram(notes, evidx);
1630 struct rb_root tmp_root = RB_ROOT;
1633 size_t sizeof_src_line = sizeof(struct source_line);
1635 if (perf_evsel__is_group_event(evsel)) {
1636 for (i = 1; i < evsel->nr_members; i++) {
1637 h = annotation__histogram(notes, evidx + i);
1640 nr_pcnt = evsel->nr_members;
1641 sizeof_src_line += (nr_pcnt - 1) * sizeof(src_line->samples);
1647 src_line = notes->src->lines = calloc(len, sizeof_src_line);
1648 if (!notes->src->lines)
1651 start = map__rip_2objdump(map, sym->start);
1653 for (i = 0; i < len; i++) {
1654 u64 offset, nr_samples;
1655 double percent_max = 0.0;
1657 src_line->nr_pcnt = nr_pcnt;
1659 for (k = 0; k < nr_pcnt; k++) {
1660 double percent = 0.0;
1662 h = annotation__histogram(notes, evidx + k);
1663 nr_samples = h->addr[i];
1665 percent = 100.0 * nr_samples / h->sum;
1667 if (percent > percent_max)
1668 percent_max = percent;
1669 src_line->samples[k].percent = percent;
1670 src_line->samples[k].nr = nr_samples;
1673 if (percent_max <= 0.5)
1677 src_line->path = get_srcline(map->dso, offset, NULL,
1679 insert_source_line(&tmp_root, src_line);
1682 src_line = (void *)src_line + sizeof_src_line;
1685 resort_source_line(root, &tmp_root);
1689 static void print_summary(struct rb_root *root, const char *filename)
1691 struct source_line *src_line;
1692 struct rb_node *node;
1694 printf("\nSorted summary for file %s\n", filename);
1695 printf("----------------------------------------------\n\n");
1697 if (RB_EMPTY_ROOT(root)) {
1698 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
1702 node = rb_first(root);
1704 double percent, percent_max = 0.0;
1709 src_line = rb_entry(node, struct source_line, node);
1710 for (i = 0; i < src_line->nr_pcnt; i++) {
1711 percent = src_line->samples[i].percent_sum;
1712 color = get_percent_color(percent);
1713 color_fprintf(stdout, color, " %7.2f", percent);
1715 if (percent > percent_max)
1716 percent_max = percent;
1719 path = src_line->path;
1720 color = get_percent_color(percent_max);
1721 color_fprintf(stdout, color, " %s\n", path);
1723 node = rb_next(node);
1727 static void symbol__annotate_hits(struct symbol *sym, struct perf_evsel *evsel)
1729 struct annotation *notes = symbol__annotation(sym);
1730 struct sym_hist *h = annotation__histogram(notes, evsel->idx);
1731 u64 len = symbol__size(sym), offset;
1733 for (offset = 0; offset < len; ++offset)
1734 if (h->addr[offset] != 0)
1735 printf("%*" PRIx64 ": %" PRIu64 "\n", BITS_PER_LONG / 2,
1736 sym->start + offset, h->addr[offset]);
1737 printf("%*s: %" PRIu64 "\n", BITS_PER_LONG / 2, "h->sum", h->sum);
1740 int symbol__annotate_printf(struct symbol *sym, struct map *map,
1741 struct perf_evsel *evsel, bool full_paths,
1742 int min_pcnt, int max_lines, int context)
1744 struct dso *dso = map->dso;
1746 const char *d_filename;
1747 const char *evsel_name = perf_evsel__name(evsel);
1748 struct annotation *notes = symbol__annotation(sym);
1749 struct sym_hist *h = annotation__histogram(notes, evsel->idx);
1750 struct disasm_line *pos, *queue = NULL;
1751 u64 start = map__rip_2objdump(map, sym->start);
1752 int printed = 2, queue_len = 0;
1756 int graph_dotted_len;
1758 filename = strdup(dso->long_name);
1763 d_filename = filename;
1765 d_filename = basename(filename);
1767 len = symbol__size(sym);
1769 if (perf_evsel__is_group_event(evsel))
1770 width *= evsel->nr_members;
1772 graph_dotted_len = printf(" %-*.*s| Source code & Disassembly of %s for %s (%" PRIu64 " samples)\n",
1773 width, width, "Percent", d_filename, evsel_name, h->sum);
1775 printf("%-*.*s----\n",
1776 graph_dotted_len, graph_dotted_len, graph_dotted_line);
1779 symbol__annotate_hits(sym, evsel);
1781 list_for_each_entry(pos, ¬es->src->source, node) {
1782 if (context && queue == NULL) {
1787 switch (disasm_line__print(pos, sym, start, evsel, len,
1788 min_pcnt, printed, max_lines,
1793 printed += queue_len;
1799 /* filtered by max_lines */
1805 * Filtered by min_pcnt or non IP lines when
1810 if (queue_len == context)
1811 queue = list_entry(queue->node.next, typeof(*queue), node);
1823 void symbol__annotate_zero_histogram(struct symbol *sym, int evidx)
1825 struct annotation *notes = symbol__annotation(sym);
1826 struct sym_hist *h = annotation__histogram(notes, evidx);
1828 memset(h, 0, notes->src->sizeof_sym_hist);
1831 void symbol__annotate_decay_histogram(struct symbol *sym, int evidx)
1833 struct annotation *notes = symbol__annotation(sym);
1834 struct sym_hist *h = annotation__histogram(notes, evidx);
1835 int len = symbol__size(sym), offset;
1838 for (offset = 0; offset < len; ++offset) {
1839 h->addr[offset] = h->addr[offset] * 7 / 8;
1840 h->sum += h->addr[offset];
1844 void disasm__purge(struct list_head *head)
1846 struct disasm_line *pos, *n;
1848 list_for_each_entry_safe(pos, n, head, node) {
1849 list_del(&pos->node);
1850 disasm_line__free(pos);
1854 static size_t disasm_line__fprintf(struct disasm_line *dl, FILE *fp)
1858 if (dl->offset == -1)
1859 return fprintf(fp, "%s\n", dl->line);
1861 printed = fprintf(fp, "%#" PRIx64 " %s", dl->offset, dl->ins.name);
1863 if (dl->ops.raw[0] != '\0') {
1864 printed += fprintf(fp, "%.*s %s\n", 6 - (int)printed, " ",
1868 return printed + fprintf(fp, "\n");
1871 size_t disasm__fprintf(struct list_head *head, FILE *fp)
1873 struct disasm_line *pos;
1876 list_for_each_entry(pos, head, node)
1877 printed += disasm_line__fprintf(pos, fp);
1882 int symbol__tty_annotate(struct symbol *sym, struct map *map,
1883 struct perf_evsel *evsel, bool print_lines,
1884 bool full_paths, int min_pcnt, int max_lines)
1886 struct dso *dso = map->dso;
1887 struct rb_root source_line = RB_ROOT;
1890 if (symbol__disassemble(sym, map, perf_evsel__env_arch(evsel), 0) < 0)
1893 len = symbol__size(sym);
1896 srcline_full_filename = full_paths;
1897 symbol__get_source_line(sym, map, evsel, &source_line, len);
1898 print_summary(&source_line, dso->long_name);
1901 symbol__annotate_printf(sym, map, evsel, full_paths,
1902 min_pcnt, max_lines, 0);
1904 symbol__free_source_line(sym, len);
1906 disasm__purge(&symbol__annotation(sym)->src->source);
1911 bool ui__has_annotation(void)
1913 return use_browser == 1 && perf_hpp_list.sym;