1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
5 * Parts came from builtin-annotate.c, see those files for further
15 #include <bpf/libbpf.h>
16 #include <linux/btf.h>
26 #include "map_groups.h"
34 #include "bpf-event.h"
35 #include "block-range.h"
37 #include "arch/common.h"
40 #include <linux/bitops.h>
41 #include <linux/kernel.h>
42 #include <linux/string.h>
43 #include <bpf/libbpf.h>
44 #include <subcmd/parse-options.h>
46 /* FIXME: For the HE_COLORSET */
47 #include "ui/browser.h"
50 * FIXME: Using the same values as slang.h,
51 * but that header may not be available everywhere
53 #define LARROW_CHAR ((unsigned char)',')
54 #define RARROW_CHAR ((unsigned char)'+')
55 #define DARROW_CHAR ((unsigned char)'.')
56 #define UARROW_CHAR ((unsigned char)'-')
58 #include <linux/ctype.h>
60 struct annotation_options annotation__default_options = {
64 .offset_level = ANNOTATION__OFFSET_JUMP_TARGETS,
65 .percent_type = PERCENT_PERIOD_LOCAL,
68 static regex_t file_lineno;
70 static struct ins_ops *ins__find(struct arch *arch, const char *name);
71 static void ins__sort(struct arch *arch);
72 static int disasm_line__parse(char *line, const char **namep, char **rawp);
76 struct ins *instructions;
77 size_t nr_instructions;
78 size_t nr_instructions_allocated;
79 struct ins_ops *(*associate_instruction_ops)(struct arch *arch, const char *name);
80 bool sorted_instructions;
85 int (*init)(struct arch *arch, char *cpuid);
86 bool (*ins_is_fused)(struct arch *arch, const char *ins1,
90 char skip_functions_char;
94 static struct ins_ops call_ops;
95 static struct ins_ops dec_ops;
96 static struct ins_ops jump_ops;
97 static struct ins_ops mov_ops;
98 static struct ins_ops nop_ops;
99 static struct ins_ops lock_ops;
100 static struct ins_ops ret_ops;
102 static int arch__grow_instructions(struct arch *arch)
104 struct ins *new_instructions;
105 size_t new_nr_allocated;
107 if (arch->nr_instructions_allocated == 0 && arch->instructions)
108 goto grow_from_non_allocated_table;
110 new_nr_allocated = arch->nr_instructions_allocated + 128;
111 new_instructions = realloc(arch->instructions, new_nr_allocated * sizeof(struct ins));
112 if (new_instructions == NULL)
115 out_update_instructions:
116 arch->instructions = new_instructions;
117 arch->nr_instructions_allocated = new_nr_allocated;
120 grow_from_non_allocated_table:
121 new_nr_allocated = arch->nr_instructions + 128;
122 new_instructions = calloc(new_nr_allocated, sizeof(struct ins));
123 if (new_instructions == NULL)
126 memcpy(new_instructions, arch->instructions, arch->nr_instructions);
127 goto out_update_instructions;
130 static int arch__associate_ins_ops(struct arch* arch, const char *name, struct ins_ops *ops)
134 if (arch->nr_instructions == arch->nr_instructions_allocated &&
135 arch__grow_instructions(arch))
138 ins = &arch->instructions[arch->nr_instructions];
139 ins->name = strdup(name);
144 arch->nr_instructions++;
150 #include "arch/arc/annotate/instructions.c"
151 #include "arch/arm/annotate/instructions.c"
152 #include "arch/arm64/annotate/instructions.c"
153 #include "arch/csky/annotate/instructions.c"
154 #include "arch/x86/annotate/instructions.c"
155 #include "arch/powerpc/annotate/instructions.c"
156 #include "arch/s390/annotate/instructions.c"
157 #include "arch/sparc/annotate/instructions.c"
159 static struct arch architectures[] = {
162 .init = arc__annotate_init,
166 .init = arm__annotate_init,
170 .init = arm64__annotate_init,
174 .init = csky__annotate_init,
178 .init = x86__annotate_init,
179 .instructions = x86__instructions,
180 .nr_instructions = ARRAY_SIZE(x86__instructions),
181 .ins_is_fused = x86__ins_is_fused,
188 .init = powerpc__annotate_init,
192 .init = s390__annotate_init,
199 .init = sparc__annotate_init,
206 static void ins__delete(struct ins_operands *ops)
210 zfree(&ops->source.raw);
211 zfree(&ops->source.name);
212 zfree(&ops->target.raw);
213 zfree(&ops->target.name);
216 static int ins__raw_scnprintf(struct ins *ins, char *bf, size_t size,
217 struct ins_operands *ops, int max_ins_name)
219 return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name, ops->raw);
222 int ins__scnprintf(struct ins *ins, char *bf, size_t size,
223 struct ins_operands *ops, int max_ins_name)
225 if (ins->ops->scnprintf)
226 return ins->ops->scnprintf(ins, bf, size, ops, max_ins_name);
228 return ins__raw_scnprintf(ins, bf, size, ops, max_ins_name);
231 bool ins__is_fused(struct arch *arch, const char *ins1, const char *ins2)
233 if (!arch || !arch->ins_is_fused)
236 return arch->ins_is_fused(arch, ins1, ins2);
239 static int call__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms)
241 char *endptr, *tok, *name;
242 struct map *map = ms->map;
243 struct addr_map_symbol target = {
247 ops->target.addr = strtoull(ops->raw, &endptr, 16);
249 name = strchr(endptr, '<');
255 if (arch->objdump.skip_functions_char &&
256 strchr(name, arch->objdump.skip_functions_char))
259 tok = strchr(name, '>');
264 ops->target.name = strdup(name);
267 if (ops->target.name == NULL)
270 target.addr = map__objdump_2mem(map, ops->target.addr);
272 if (map_groups__find_ams(&target) == 0 &&
273 map__rip_2objdump(target.map, map->map_ip(target.map, target.addr)) == ops->target.addr)
274 ops->target.sym = target.sym;
279 tok = strchr(endptr, '*');
283 /* Indirect call can use a non-rip register and offset: callq *0x8(%rbx).
284 * Do not parse such instruction. */
285 if (strstr(endptr, "(%r") == NULL)
286 ops->target.addr = strtoull(endptr, NULL, 16);
291 static int call__scnprintf(struct ins *ins, char *bf, size_t size,
292 struct ins_operands *ops, int max_ins_name)
295 return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name, ops->target.sym->name);
297 if (ops->target.addr == 0)
298 return ins__raw_scnprintf(ins, bf, size, ops, max_ins_name);
300 if (ops->target.name)
301 return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name, ops->target.name);
303 return scnprintf(bf, size, "%-*s *%" PRIx64, max_ins_name, ins->name, ops->target.addr);
306 static struct ins_ops call_ops = {
307 .parse = call__parse,
308 .scnprintf = call__scnprintf,
311 bool ins__is_call(const struct ins *ins)
313 return ins->ops == &call_ops || ins->ops == &s390_call_ops;
317 * Prevents from matching commas in the comment section, e.g.:
318 * ffff200008446e70: b.cs ffff2000084470f4 <generic_exec_single+0x314> // b.hs, b.nlast
320 static inline const char *validate_comma(const char *c, struct ins_operands *ops)
322 if (ops->raw_comment && c > ops->raw_comment)
328 static int jump__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms)
330 struct map *map = ms->map;
331 struct symbol *sym = ms->sym;
332 struct addr_map_symbol target = {
335 const char *c = strchr(ops->raw, ',');
338 ops->raw_comment = strchr(ops->raw, arch->objdump.comment_char);
339 c = validate_comma(c, ops);
342 * Examples of lines to parse for the _cpp_lex_token@@Base
345 * 1159e6c: jne 115aa32 <_cpp_lex_token@@Base+0xf92>
346 * 1159e8b: jne c469be <cpp_named_operator2name@@Base+0xa72>
348 * The first is a jump to an offset inside the same function,
349 * the second is to another function, i.e. that 0xa72 is an
350 * offset in the cpp_named_operator2name@@base function.
353 * skip over possible up to 2 operands to get to address, e.g.:
354 * tbnz w0, #26, ffff0000083cd190 <security_file_permission+0xd0>
357 ops->target.addr = strtoull(c, NULL, 16);
358 if (!ops->target.addr) {
360 c = validate_comma(c, ops);
362 ops->target.addr = strtoull(c, NULL, 16);
365 ops->target.addr = strtoull(ops->raw, NULL, 16);
368 target.addr = map__objdump_2mem(map, ops->target.addr);
369 start = map->unmap_ip(map, sym->start),
370 end = map->unmap_ip(map, sym->end);
372 ops->target.outside = target.addr < start || target.addr > end;
375 * FIXME: things like this in _cpp_lex_token (gcc's cc1 program):
377 cpp_named_operator2name@@Base+0xa72
379 * Point to a place that is after the cpp_named_operator2name
380 * boundaries, i.e. in the ELF symbol table for cc1
381 * cpp_named_operator2name is marked as being 32-bytes long, but it in
382 * fact is much larger than that, so we seem to need a symbols__find()
383 * routine that looks for >= current->start and < next_symbol->start,
384 * possibly just for C++ objects?
386 * For now lets just make some progress by marking jumps to outside the
387 * current function as call like.
389 * Actual navigation will come next, with further understanding of how
390 * the symbol searching and disassembly should be done.
392 if (map_groups__find_ams(&target) == 0 &&
393 map__rip_2objdump(target.map, map->map_ip(target.map, target.addr)) == ops->target.addr)
394 ops->target.sym = target.sym;
396 if (!ops->target.outside) {
397 ops->target.offset = target.addr - start;
398 ops->target.offset_avail = true;
400 ops->target.offset_avail = false;
406 static int jump__scnprintf(struct ins *ins, char *bf, size_t size,
407 struct ins_operands *ops, int max_ins_name)
411 if (!ops->target.addr || ops->target.offset < 0)
412 return ins__raw_scnprintf(ins, bf, size, ops, max_ins_name);
414 if (ops->target.outside && ops->target.sym != NULL)
415 return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name, ops->target.sym->name);
417 c = strchr(ops->raw, ',');
418 c = validate_comma(c, ops);
421 const char *c2 = strchr(c + 1, ',');
423 c2 = validate_comma(c2, ops);
424 /* check for 3-op insn */
429 /* mirror arch objdump's space-after-comma style */
434 return scnprintf(bf, size, "%-*s %.*s%" PRIx64, max_ins_name,
435 ins->name, c ? c - ops->raw : 0, ops->raw,
439 static struct ins_ops jump_ops = {
440 .parse = jump__parse,
441 .scnprintf = jump__scnprintf,
444 bool ins__is_jump(const struct ins *ins)
446 return ins->ops == &jump_ops;
449 static int comment__symbol(char *raw, char *comment, u64 *addrp, char **namep)
451 char *endptr, *name, *t;
453 if (strstr(raw, "(%rip)") == NULL)
456 *addrp = strtoull(comment, &endptr, 16);
457 if (endptr == comment)
459 name = strchr(endptr, '<');
465 t = strchr(name, '>');
470 *namep = strdup(name);
476 static int lock__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms)
478 ops->locked.ops = zalloc(sizeof(*ops->locked.ops));
479 if (ops->locked.ops == NULL)
482 if (disasm_line__parse(ops->raw, &ops->locked.ins.name, &ops->locked.ops->raw) < 0)
485 ops->locked.ins.ops = ins__find(arch, ops->locked.ins.name);
487 if (ops->locked.ins.ops == NULL)
490 if (ops->locked.ins.ops->parse &&
491 ops->locked.ins.ops->parse(arch, ops->locked.ops, ms) < 0)
497 zfree(&ops->locked.ops);
501 static int lock__scnprintf(struct ins *ins, char *bf, size_t size,
502 struct ins_operands *ops, int max_ins_name)
506 if (ops->locked.ins.ops == NULL)
507 return ins__raw_scnprintf(ins, bf, size, ops, max_ins_name);
509 printed = scnprintf(bf, size, "%-*s ", max_ins_name, ins->name);
510 return printed + ins__scnprintf(&ops->locked.ins, bf + printed,
511 size - printed, ops->locked.ops, max_ins_name);
514 static void lock__delete(struct ins_operands *ops)
516 struct ins *ins = &ops->locked.ins;
518 if (ins->ops && ins->ops->free)
519 ins->ops->free(ops->locked.ops);
521 ins__delete(ops->locked.ops);
523 zfree(&ops->locked.ops);
524 zfree(&ops->target.raw);
525 zfree(&ops->target.name);
528 static struct ins_ops lock_ops = {
529 .free = lock__delete,
530 .parse = lock__parse,
531 .scnprintf = lock__scnprintf,
534 static int mov__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms __maybe_unused)
536 char *s = strchr(ops->raw, ','), *target, *comment, prev;
542 ops->source.raw = strdup(ops->raw);
545 if (ops->source.raw == NULL)
549 comment = strchr(s, arch->objdump.comment_char);
554 s = strchr(s, '\0') - 1;
556 while (s > target && isspace(s[0]))
562 ops->target.raw = strdup(target);
565 if (ops->target.raw == NULL)
566 goto out_free_source;
571 comment = skip_spaces(comment);
572 comment__symbol(ops->source.raw, comment + 1, &ops->source.addr, &ops->source.name);
573 comment__symbol(ops->target.raw, comment + 1, &ops->target.addr, &ops->target.name);
578 zfree(&ops->source.raw);
582 static int mov__scnprintf(struct ins *ins, char *bf, size_t size,
583 struct ins_operands *ops, int max_ins_name)
585 return scnprintf(bf, size, "%-*s %s,%s", max_ins_name, ins->name,
586 ops->source.name ?: ops->source.raw,
587 ops->target.name ?: ops->target.raw);
590 static struct ins_ops mov_ops = {
592 .scnprintf = mov__scnprintf,
595 static int dec__parse(struct arch *arch __maybe_unused, struct ins_operands *ops, struct map_symbol *ms __maybe_unused)
597 char *target, *comment, *s, prev;
599 target = s = ops->raw;
601 while (s[0] != '\0' && !isspace(s[0]))
606 ops->target.raw = strdup(target);
609 if (ops->target.raw == NULL)
612 comment = strchr(s, arch->objdump.comment_char);
616 comment = skip_spaces(comment);
617 comment__symbol(ops->target.raw, comment + 1, &ops->target.addr, &ops->target.name);
622 static int dec__scnprintf(struct ins *ins, char *bf, size_t size,
623 struct ins_operands *ops, int max_ins_name)
625 return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name,
626 ops->target.name ?: ops->target.raw);
629 static struct ins_ops dec_ops = {
631 .scnprintf = dec__scnprintf,
634 static int nop__scnprintf(struct ins *ins __maybe_unused, char *bf, size_t size,
635 struct ins_operands *ops __maybe_unused, int max_ins_name)
637 return scnprintf(bf, size, "%-*s", max_ins_name, "nop");
640 static struct ins_ops nop_ops = {
641 .scnprintf = nop__scnprintf,
644 static struct ins_ops ret_ops = {
645 .scnprintf = ins__raw_scnprintf,
648 bool ins__is_ret(const struct ins *ins)
650 return ins->ops == &ret_ops;
653 bool ins__is_lock(const struct ins *ins)
655 return ins->ops == &lock_ops;
658 static int ins__key_cmp(const void *name, const void *insp)
660 const struct ins *ins = insp;
662 return strcmp(name, ins->name);
665 static int ins__cmp(const void *a, const void *b)
667 const struct ins *ia = a;
668 const struct ins *ib = b;
670 return strcmp(ia->name, ib->name);
673 static void ins__sort(struct arch *arch)
675 const int nmemb = arch->nr_instructions;
677 qsort(arch->instructions, nmemb, sizeof(struct ins), ins__cmp);
680 static struct ins_ops *__ins__find(struct arch *arch, const char *name)
683 const int nmemb = arch->nr_instructions;
685 if (!arch->sorted_instructions) {
687 arch->sorted_instructions = true;
690 ins = bsearch(name, arch->instructions, nmemb, sizeof(struct ins), ins__key_cmp);
691 return ins ? ins->ops : NULL;
694 static struct ins_ops *ins__find(struct arch *arch, const char *name)
696 struct ins_ops *ops = __ins__find(arch, name);
698 if (!ops && arch->associate_instruction_ops)
699 ops = arch->associate_instruction_ops(arch, name);
704 static int arch__key_cmp(const void *name, const void *archp)
706 const struct arch *arch = archp;
708 return strcmp(name, arch->name);
711 static int arch__cmp(const void *a, const void *b)
713 const struct arch *aa = a;
714 const struct arch *ab = b;
716 return strcmp(aa->name, ab->name);
719 static void arch__sort(void)
721 const int nmemb = ARRAY_SIZE(architectures);
723 qsort(architectures, nmemb, sizeof(struct arch), arch__cmp);
726 static struct arch *arch__find(const char *name)
728 const int nmemb = ARRAY_SIZE(architectures);
736 return bsearch(name, architectures, nmemb, sizeof(struct arch), arch__key_cmp);
739 static struct annotated_source *annotated_source__new(void)
741 struct annotated_source *src = zalloc(sizeof(*src));
744 INIT_LIST_HEAD(&src->source);
749 static __maybe_unused void annotated_source__delete(struct annotated_source *src)
753 zfree(&src->histograms);
754 zfree(&src->cycles_hist);
758 static int annotated_source__alloc_histograms(struct annotated_source *src,
759 size_t size, int nr_hists)
761 size_t sizeof_sym_hist;
764 * Add buffer of one element for zero length symbol.
765 * When sample is taken from first instruction of
766 * zero length symbol, perf still resolves it and
767 * shows symbol name in perf report and allows to
773 /* Check for overflow when calculating sizeof_sym_hist */
774 if (size > (SIZE_MAX - sizeof(struct sym_hist)) / sizeof(struct sym_hist_entry))
777 sizeof_sym_hist = (sizeof(struct sym_hist) + size * sizeof(struct sym_hist_entry));
779 /* Check for overflow in zalloc argument */
780 if (sizeof_sym_hist > SIZE_MAX / nr_hists)
783 src->sizeof_sym_hist = sizeof_sym_hist;
784 src->nr_histograms = nr_hists;
785 src->histograms = calloc(nr_hists, sizeof_sym_hist) ;
786 return src->histograms ? 0 : -1;
789 /* The cycles histogram is lazily allocated. */
790 static int symbol__alloc_hist_cycles(struct symbol *sym)
792 struct annotation *notes = symbol__annotation(sym);
793 const size_t size = symbol__size(sym);
795 notes->src->cycles_hist = calloc(size, sizeof(struct cyc_hist));
796 if (notes->src->cycles_hist == NULL)
801 void symbol__annotate_zero_histograms(struct symbol *sym)
803 struct annotation *notes = symbol__annotation(sym);
805 pthread_mutex_lock(¬es->lock);
806 if (notes->src != NULL) {
807 memset(notes->src->histograms, 0,
808 notes->src->nr_histograms * notes->src->sizeof_sym_hist);
809 if (notes->src->cycles_hist)
810 memset(notes->src->cycles_hist, 0,
811 symbol__size(sym) * sizeof(struct cyc_hist));
813 pthread_mutex_unlock(¬es->lock);
816 static int __symbol__account_cycles(struct cyc_hist *ch,
818 unsigned offset, unsigned cycles,
822 * For now we can only account one basic block per
823 * final jump. But multiple could be overlapping.
824 * Always account the longest one. So when
825 * a shorter one has been already seen throw it away.
827 * We separately always account the full cycles.
829 ch[offset].num_aggr++;
830 ch[offset].cycles_aggr += cycles;
832 if (cycles > ch[offset].cycles_max)
833 ch[offset].cycles_max = cycles;
835 if (ch[offset].cycles_min) {
836 if (cycles && cycles < ch[offset].cycles_min)
837 ch[offset].cycles_min = cycles;
839 ch[offset].cycles_min = cycles;
841 if (!have_start && ch[offset].have_start)
843 if (ch[offset].num) {
844 if (have_start && (!ch[offset].have_start ||
845 ch[offset].start > start)) {
846 ch[offset].have_start = 0;
847 ch[offset].cycles = 0;
849 if (ch[offset].reset < 0xffff)
851 } else if (have_start &&
852 ch[offset].start < start)
855 ch[offset].have_start = have_start;
856 ch[offset].start = start;
857 ch[offset].cycles += cycles;
862 static int __symbol__inc_addr_samples(struct symbol *sym, struct map *map,
863 struct annotated_source *src, int evidx, u64 addr,
864 struct perf_sample *sample)
869 pr_debug3("%s: addr=%#" PRIx64 "\n", __func__, map->unmap_ip(map, addr));
871 if ((addr < sym->start || addr >= sym->end) &&
872 (addr != sym->end || sym->start != sym->end)) {
873 pr_debug("%s(%d): ERANGE! sym->name=%s, start=%#" PRIx64 ", addr=%#" PRIx64 ", end=%#" PRIx64 "\n",
874 __func__, __LINE__, sym->name, sym->start, addr, sym->end);
878 offset = addr - sym->start;
879 h = annotated_source__histogram(src, evidx);
881 pr_debug("%s(%d): ENOMEM! sym->name=%s, start=%#" PRIx64 ", addr=%#" PRIx64 ", end=%#" PRIx64 ", func: %d\n",
882 __func__, __LINE__, sym->name, sym->start, addr, sym->end, sym->type == STT_FUNC);
886 h->addr[offset].nr_samples++;
887 h->period += sample->period;
888 h->addr[offset].period += sample->period;
890 pr_debug3("%#" PRIx64 " %s: period++ [addr: %#" PRIx64 ", %#" PRIx64
891 ", evidx=%d] => nr_samples: %" PRIu64 ", period: %" PRIu64 "\n",
892 sym->start, sym->name, addr, addr - sym->start, evidx,
893 h->addr[offset].nr_samples, h->addr[offset].period);
897 static struct cyc_hist *symbol__cycles_hist(struct symbol *sym)
899 struct annotation *notes = symbol__annotation(sym);
901 if (notes->src == NULL) {
902 notes->src = annotated_source__new();
903 if (notes->src == NULL)
905 goto alloc_cycles_hist;
908 if (!notes->src->cycles_hist) {
910 symbol__alloc_hist_cycles(sym);
913 return notes->src->cycles_hist;
916 struct annotated_source *symbol__hists(struct symbol *sym, int nr_hists)
918 struct annotation *notes = symbol__annotation(sym);
920 if (notes->src == NULL) {
921 notes->src = annotated_source__new();
922 if (notes->src == NULL)
924 goto alloc_histograms;
927 if (notes->src->histograms == NULL) {
929 annotated_source__alloc_histograms(notes->src, symbol__size(sym),
936 static int symbol__inc_addr_samples(struct symbol *sym, struct map *map,
937 struct evsel *evsel, u64 addr,
938 struct perf_sample *sample)
940 struct annotated_source *src;
944 src = symbol__hists(sym, evsel->evlist->core.nr_entries);
945 return (src) ? __symbol__inc_addr_samples(sym, map, src, evsel->idx,
949 static int symbol__account_cycles(u64 addr, u64 start,
950 struct symbol *sym, unsigned cycles)
952 struct cyc_hist *cycles_hist;
957 cycles_hist = symbol__cycles_hist(sym);
958 if (cycles_hist == NULL)
960 if (addr < sym->start || addr >= sym->end)
964 if (start < sym->start || start >= sym->end)
969 offset = addr - sym->start;
970 return __symbol__account_cycles(cycles_hist,
971 start ? start - sym->start : 0,
976 int addr_map_symbol__account_cycles(struct addr_map_symbol *ams,
977 struct addr_map_symbol *start,
987 * Only set start when IPC can be computed. We can only
988 * compute it when the basic block is completely in a single
990 * Special case the case when the jump is elsewhere, but
991 * it starts on the function start.
994 (start->sym == ams->sym ||
996 start->addr == ams->sym->start + ams->map->start)))
997 saddr = start->al_addr;
999 pr_debug2("BB with bad start: addr %"PRIx64" start %"PRIx64" sym %"PRIx64" saddr %"PRIx64"\n",
1001 start ? start->addr : 0,
1002 ams->sym ? ams->sym->start + ams->map->start : 0,
1004 err = symbol__account_cycles(ams->al_addr, saddr, ams->sym, cycles);
1006 pr_debug2("account_cycles failed %d\n", err);
1010 static unsigned annotation__count_insn(struct annotation *notes, u64 start, u64 end)
1012 unsigned n_insn = 0;
1015 for (offset = start; offset <= end; offset++) {
1016 if (notes->offsets[offset])
1022 static void annotation__count_and_fill(struct annotation *notes, u64 start, u64 end, struct cyc_hist *ch)
1025 unsigned int cover_insn = 0;
1028 n_insn = annotation__count_insn(notes, start, end);
1029 if (n_insn && ch->num && ch->cycles) {
1030 float ipc = n_insn / ((double)ch->cycles / (double)ch->num);
1032 /* Hide data when there are too many overlaps. */
1033 if (ch->reset >= 0x7fff)
1036 for (offset = start; offset <= end; offset++) {
1037 struct annotation_line *al = notes->offsets[offset];
1039 if (al && al->ipc == 0.0) {
1046 notes->hit_cycles += ch->cycles;
1047 notes->hit_insn += n_insn * ch->num;
1048 notes->cover_insn += cover_insn;
1053 void annotation__compute_ipc(struct annotation *notes, size_t size)
1057 if (!notes->src || !notes->src->cycles_hist)
1060 notes->total_insn = annotation__count_insn(notes, 0, size - 1);
1061 notes->hit_cycles = 0;
1062 notes->hit_insn = 0;
1063 notes->cover_insn = 0;
1065 pthread_mutex_lock(¬es->lock);
1066 for (offset = size - 1; offset >= 0; --offset) {
1067 struct cyc_hist *ch;
1069 ch = ¬es->src->cycles_hist[offset];
1070 if (ch && ch->cycles) {
1071 struct annotation_line *al;
1074 annotation__count_and_fill(notes, ch->start, offset, ch);
1075 al = notes->offsets[offset];
1076 if (al && ch->num_aggr) {
1077 al->cycles = ch->cycles_aggr / ch->num_aggr;
1078 al->cycles_max = ch->cycles_max;
1079 al->cycles_min = ch->cycles_min;
1081 notes->have_cycles = true;
1084 pthread_mutex_unlock(¬es->lock);
1087 int addr_map_symbol__inc_samples(struct addr_map_symbol *ams, struct perf_sample *sample,
1088 struct evsel *evsel)
1090 return symbol__inc_addr_samples(ams->sym, ams->map, evsel, ams->al_addr, sample);
1093 int hist_entry__inc_addr_samples(struct hist_entry *he, struct perf_sample *sample,
1094 struct evsel *evsel, u64 ip)
1096 return symbol__inc_addr_samples(he->ms.sym, he->ms.map, evsel, ip, sample);
1099 static void disasm_line__init_ins(struct disasm_line *dl, struct arch *arch, struct map_symbol *ms)
1101 dl->ins.ops = ins__find(arch, dl->ins.name);
1106 if (dl->ins.ops->parse && dl->ins.ops->parse(arch, &dl->ops, ms) < 0)
1110 static int disasm_line__parse(char *line, const char **namep, char **rawp)
1112 char tmp, *name = skip_spaces(line);
1114 if (name[0] == '\0')
1119 while ((*rawp)[0] != '\0' && !isspace((*rawp)[0]))
1124 *namep = strdup(name);
1130 *rawp = strim(*rawp);
1138 struct annotate_args {
1141 struct map_symbol ms;
1142 struct evsel *evsel;
1143 struct annotation_options *options;
1149 static void annotation_line__delete(struct annotation_line *al)
1151 void *ptr = (void *) al - al->privsize;
1153 free_srcline(al->path);
1159 * Allocating the annotation line data with following
1162 * --------------------------------------
1163 * private space | struct annotation_line
1164 * --------------------------------------
1166 * Size of the private space is stored in 'struct annotation_line'.
1169 static struct annotation_line *
1170 annotation_line__new(struct annotate_args *args, size_t privsize)
1172 struct annotation_line *al;
1173 struct evsel *evsel = args->evsel;
1174 size_t size = privsize + sizeof(*al);
1177 if (perf_evsel__is_group_event(evsel))
1178 nr = evsel->core.nr_members;
1180 size += sizeof(al->data[0]) * nr;
1184 al = (void *) al + privsize;
1185 al->privsize = privsize;
1186 al->offset = args->offset;
1187 al->line = strdup(args->line);
1188 al->line_nr = args->line_nr;
1196 * Allocating the disasm annotation line data with
1197 * following structure:
1199 * ------------------------------------------------------------
1200 * privsize space | struct disasm_line | struct annotation_line
1201 * ------------------------------------------------------------
1203 * We have 'struct annotation_line' member as last member
1204 * of 'struct disasm_line' to have an easy access.
1207 static struct disasm_line *disasm_line__new(struct annotate_args *args)
1209 struct disasm_line *dl = NULL;
1210 struct annotation_line *al;
1211 size_t privsize = args->privsize + offsetof(struct disasm_line, al);
1213 al = annotation_line__new(args, privsize);
1215 dl = disasm_line(al);
1217 if (dl->al.line == NULL)
1220 if (args->offset != -1) {
1221 if (disasm_line__parse(dl->al.line, &dl->ins.name, &dl->ops.raw) < 0)
1224 disasm_line__init_ins(dl, args->arch, &args->ms);
1231 zfree(&dl->al.line);
1237 void disasm_line__free(struct disasm_line *dl)
1239 if (dl->ins.ops && dl->ins.ops->free)
1240 dl->ins.ops->free(&dl->ops);
1242 ins__delete(&dl->ops);
1243 zfree(&dl->ins.name);
1244 annotation_line__delete(&dl->al);
1247 int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size, bool raw, int max_ins_name)
1249 if (raw || !dl->ins.ops)
1250 return scnprintf(bf, size, "%-*s %s", max_ins_name, dl->ins.name, dl->ops.raw);
1252 return ins__scnprintf(&dl->ins, bf, size, &dl->ops, max_ins_name);
1255 static void annotation_line__add(struct annotation_line *al, struct list_head *head)
1257 list_add_tail(&al->node, head);
1260 struct annotation_line *
1261 annotation_line__next(struct annotation_line *pos, struct list_head *head)
1263 list_for_each_entry_continue(pos, head, node)
1264 if (pos->offset >= 0)
1270 static const char *annotate__address_color(struct block_range *br)
1272 double cov = block_range__coverage(br);
1275 /* mark red for >75% coverage */
1277 return PERF_COLOR_RED;
1279 /* mark dull for <1% coverage */
1281 return PERF_COLOR_NORMAL;
1284 return PERF_COLOR_MAGENTA;
1287 static const char *annotate__asm_color(struct block_range *br)
1289 double cov = block_range__coverage(br);
1292 /* mark dull for <1% coverage */
1294 return PERF_COLOR_NORMAL;
1297 return PERF_COLOR_BLUE;
1300 static void annotate__branch_printf(struct block_range *br, u64 addr)
1302 bool emit_comment = true;
1308 if (br->is_target && br->start == addr) {
1309 struct block_range *branch = br;
1313 * Find matching branch to our target.
1315 while (!branch->is_branch)
1316 branch = block_range__next(branch);
1318 p = 100 *(double)br->entry / branch->coverage;
1322 emit_comment = false;
1327 * The percentage of coverage joined at this target in relation
1328 * to the next branch.
1330 printf(" +%.2f%%", p);
1334 if (br->is_branch && br->end == addr) {
1335 double p = 100*(double)br->taken / br->coverage;
1339 emit_comment = false;
1344 * The percentage of coverage leaving at this branch, and
1345 * its prediction ratio.
1347 printf(" -%.2f%% (p:%.2f%%)", p, 100*(double)br->pred / br->taken);
1352 static int disasm_line__print(struct disasm_line *dl, u64 start, int addr_fmt_width)
1354 s64 offset = dl->al.offset;
1355 const u64 addr = start + offset;
1356 struct block_range *br;
1358 br = block_range__find(addr);
1359 color_fprintf(stdout, annotate__address_color(br), " %*" PRIx64 ":", addr_fmt_width, addr);
1360 color_fprintf(stdout, annotate__asm_color(br), "%s", dl->al.line);
1361 annotate__branch_printf(br, addr);
1366 annotation_line__print(struct annotation_line *al, struct symbol *sym, u64 start,
1367 struct evsel *evsel, u64 len, int min_pcnt, int printed,
1368 int max_lines, struct annotation_line *queue, int addr_fmt_width,
1371 struct disasm_line *dl = container_of(al, struct disasm_line, al);
1372 static const char *prev_line;
1373 static const char *prev_color;
1375 if (al->offset != -1) {
1376 double max_percent = 0.0;
1377 int i, nr_percent = 1;
1379 struct annotation *notes = symbol__annotation(sym);
1381 for (i = 0; i < al->data_nr; i++) {
1384 percent = annotation_data__percent(&al->data[i],
1387 if (percent > max_percent)
1388 max_percent = percent;
1391 if (al->data_nr > nr_percent)
1392 nr_percent = al->data_nr;
1394 if (max_percent < min_pcnt)
1397 if (max_lines && printed >= max_lines)
1400 if (queue != NULL) {
1401 list_for_each_entry_from(queue, ¬es->src->source, node) {
1404 annotation_line__print(queue, sym, start, evsel, len,
1405 0, 0, 1, NULL, addr_fmt_width,
1410 color = get_percent_color(max_percent);
1413 * Also color the filename and line if needed, with
1414 * the same color than the percentage. Don't print it
1415 * twice for close colored addr with the same filename:line
1418 if (!prev_line || strcmp(prev_line, al->path)
1419 || color != prev_color) {
1420 color_fprintf(stdout, color, " %s", al->path);
1421 prev_line = al->path;
1426 for (i = 0; i < nr_percent; i++) {
1427 struct annotation_data *data = &al->data[i];
1430 percent = annotation_data__percent(data, percent_type);
1431 color = get_percent_color(percent);
1433 if (symbol_conf.show_total_period)
1434 color_fprintf(stdout, color, " %11" PRIu64,
1436 else if (symbol_conf.show_nr_samples)
1437 color_fprintf(stdout, color, " %7" PRIu64,
1438 data->he.nr_samples);
1440 color_fprintf(stdout, color, " %7.2f", percent);
1445 disasm_line__print(dl, start, addr_fmt_width);
1447 } else if (max_lines && printed >= max_lines)
1450 int width = symbol_conf.show_total_period ? 12 : 8;
1455 if (perf_evsel__is_group_event(evsel))
1456 width *= evsel->core.nr_members;
1459 printf(" %*s:\n", width, " ");
1461 printf(" %*s: %*s %s\n", width, " ", addr_fmt_width, " ", al->line);
1468 * symbol__parse_objdump_line() parses objdump output (with -d --no-show-raw)
1469 * which looks like following
1471 * 0000000000415500 <_init>:
1472 * 415500: sub $0x8,%rsp
1473 * 415504: mov 0x2f5ad5(%rip),%rax # 70afe0 <_DYNAMIC+0x2f8>
1474 * 41550b: test %rax,%rax
1475 * 41550e: je 415515 <_init+0x15>
1476 * 415510: callq 416e70 <__gmon_start__@plt>
1477 * 415515: add $0x8,%rsp
1480 * it will be parsed and saved into struct disasm_line as
1481 * <offset> <name> <ops.raw>
1483 * The offset will be a relative offset from the start of the symbol and -1
1484 * means that it's not a disassembly line so should be treated differently.
1485 * The ops.raw part will be parsed further according to type of the instruction.
1487 static int symbol__parse_objdump_line(struct symbol *sym, FILE *file,
1488 struct annotate_args *args,
1491 struct map *map = args->ms.map;
1492 struct annotation *notes = symbol__annotation(sym);
1493 struct disasm_line *dl;
1494 char *line = NULL, *parsed_line, *tmp, *tmp2;
1496 s64 line_ip, offset = -1;
1497 regmatch_t match[2];
1499 if (getline(&line, &line_len, file) < 0)
1506 parsed_line = strim(line);
1508 /* /filename:linenr ? Save line number and ignore. */
1509 if (regexec(&file_lineno, parsed_line, 2, match, 0) == 0) {
1510 *line_nr = atoi(parsed_line + match[1].rm_so);
1514 tmp = skip_spaces(parsed_line);
1517 * Parse hexa addresses followed by ':'
1519 line_ip = strtoull(tmp, &tmp2, 16);
1520 if (*tmp2 != ':' || tmp == tmp2 || tmp2[1] == '\0')
1524 if (line_ip != -1) {
1525 u64 start = map__rip_2objdump(map, sym->start),
1526 end = map__rip_2objdump(map, sym->end);
1528 offset = line_ip - start;
1529 if ((u64)line_ip < start || (u64)line_ip >= end)
1532 parsed_line = tmp2 + 1;
1535 args->offset = offset;
1536 args->line = parsed_line;
1537 args->line_nr = *line_nr;
1540 dl = disasm_line__new(args);
1547 if (!disasm_line__has_local_offset(dl)) {
1548 dl->ops.target.offset = dl->ops.target.addr -
1549 map__rip_2objdump(map, sym->start);
1550 dl->ops.target.offset_avail = true;
1553 /* kcore has no symbols, so add the call target symbol */
1554 if (dl->ins.ops && ins__is_call(&dl->ins) && !dl->ops.target.sym) {
1555 struct addr_map_symbol target = {
1557 .addr = dl->ops.target.addr,
1560 if (!map_groups__find_ams(&target) &&
1561 target.sym->start == target.al_addr)
1562 dl->ops.target.sym = target.sym;
1565 annotation_line__add(&dl->al, ¬es->src->source);
1570 static __attribute__((constructor)) void symbol__init_regexpr(void)
1572 regcomp(&file_lineno, "^/[^:]+:([0-9]+)", REG_EXTENDED);
1575 static void delete_last_nop(struct symbol *sym)
1577 struct annotation *notes = symbol__annotation(sym);
1578 struct list_head *list = ¬es->src->source;
1579 struct disasm_line *dl;
1581 while (!list_empty(list)) {
1582 dl = list_entry(list->prev, struct disasm_line, al.node);
1585 if (dl->ins.ops != &nop_ops)
1588 if (!strstr(dl->al.line, " nop ") &&
1589 !strstr(dl->al.line, " nopl ") &&
1590 !strstr(dl->al.line, " nopw "))
1594 list_del_init(&dl->al.node);
1595 disasm_line__free(dl);
1599 int symbol__strerror_disassemble(struct symbol *sym __maybe_unused, struct map *map,
1600 int errnum, char *buf, size_t buflen)
1602 struct dso *dso = map->dso;
1604 BUG_ON(buflen == 0);
1607 str_error_r(errnum, buf, buflen);
1612 case SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX: {
1613 char bf[SBUILD_ID_SIZE + 15] = " with build id ";
1614 char *build_id_msg = NULL;
1616 if (dso->has_build_id) {
1617 build_id__sprintf(dso->build_id,
1618 sizeof(dso->build_id), bf + 15);
1621 scnprintf(buf, buflen,
1622 "No vmlinux file%s\nwas found in the path.\n\n"
1623 "Note that annotation using /proc/kcore requires CAP_SYS_RAWIO capability.\n\n"
1625 " perf buildid-cache -vu vmlinux\n\n"
1627 " --vmlinux vmlinux\n", build_id_msg ?: "");
1630 case SYMBOL_ANNOTATE_ERRNO__NO_LIBOPCODES_FOR_BPF:
1631 scnprintf(buf, buflen, "Please link with binutils's libopcode to enable BPF annotation");
1634 scnprintf(buf, buflen, "Internal error: Invalid %d error code\n", errnum);
1641 static int dso__disassemble_filename(struct dso *dso, char *filename, size_t filename_size)
1643 char linkname[PATH_MAX];
1644 char *build_id_filename;
1645 char *build_id_path = NULL;
1648 if (dso->symtab_type == DSO_BINARY_TYPE__KALLSYMS &&
1649 !dso__is_kcore(dso))
1650 return SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX;
1652 build_id_filename = dso__build_id_filename(dso, NULL, 0, false);
1653 if (build_id_filename) {
1654 __symbol__join_symfs(filename, filename_size, build_id_filename);
1655 free(build_id_filename);
1657 if (dso->has_build_id)
1662 build_id_path = strdup(filename);
1667 * old style build-id cache has name of XX/XXXXXXX.. while
1668 * new style has XX/XXXXXXX../{elf,kallsyms,vdso}.
1669 * extract the build-id part of dirname in the new style only.
1671 pos = strrchr(build_id_path, '/');
1672 if (pos && strlen(pos) < SBUILD_ID_SIZE - 2)
1673 dirname(build_id_path);
1675 if (dso__is_kcore(dso) ||
1676 readlink(build_id_path, linkname, sizeof(linkname)) < 0 ||
1677 strstr(linkname, DSO__NAME_KALLSYMS) ||
1678 access(filename, R_OK)) {
1681 * If we don't have build-ids or the build-id file isn't in the
1682 * cache, or is just a kallsyms file, well, lets hope that this
1683 * DSO is the same as when 'perf record' ran.
1685 __symbol__join_symfs(filename, filename_size, dso->long_name);
1688 free(build_id_path);
1692 #if defined(HAVE_LIBBFD_SUPPORT) && defined(HAVE_LIBBPF_SUPPORT)
1693 #define PACKAGE "perf"
1695 #include <dis-asm.h>
1697 static int symbol__disassemble_bpf(struct symbol *sym,
1698 struct annotate_args *args)
1700 struct annotation *notes = symbol__annotation(sym);
1701 struct annotation_options *opts = args->options;
1702 struct bpf_prog_info_linear *info_linear;
1703 struct bpf_prog_linfo *prog_linfo = NULL;
1704 struct bpf_prog_info_node *info_node;
1705 int len = sym->end - sym->start;
1706 disassembler_ftype disassemble;
1707 struct map *map = args->ms.map;
1708 struct disassemble_info info;
1709 struct dso *dso = map->dso;
1710 int pc = 0, count, sub_id;
1711 struct btf *btf = NULL;
1712 char tpath[PATH_MAX];
1720 if (dso->binary_type != DSO_BINARY_TYPE__BPF_PROG_INFO)
1723 pr_debug("%s: handling sym %s addr %" PRIx64 " len %" PRIx64 "\n", __func__,
1724 sym->name, sym->start, sym->end - sym->start);
1726 memset(tpath, 0, sizeof(tpath));
1727 perf_exe(tpath, sizeof(tpath));
1729 bfdf = bfd_openr(tpath, NULL);
1731 assert(bfd_check_format(bfdf, bfd_object));
1733 s = open_memstream(&buf, &buf_size);
1736 init_disassemble_info(&info, s,
1737 (fprintf_ftype) fprintf);
1739 info.arch = bfd_get_arch(bfdf);
1740 info.mach = bfd_get_mach(bfdf);
1742 info_node = perf_env__find_bpf_prog_info(dso->bpf_prog.env,
1746 info_linear = info_node->info_linear;
1747 sub_id = dso->bpf_prog.sub_id;
1749 info.buffer = (void *)(uintptr_t)(info_linear->info.jited_prog_insns);
1750 info.buffer_length = info_linear->info.jited_prog_len;
1752 if (info_linear->info.nr_line_info)
1753 prog_linfo = bpf_prog_linfo__new(&info_linear->info);
1755 if (info_linear->info.btf_id) {
1756 struct btf_node *node;
1758 node = perf_env__find_btf(dso->bpf_prog.env,
1759 info_linear->info.btf_id);
1761 btf = btf__new((__u8 *)(node->data),
1765 disassemble_init_for_target(&info);
1767 #ifdef DISASM_FOUR_ARGS_SIGNATURE
1768 disassemble = disassembler(info.arch,
1769 bfd_big_endian(bfdf),
1773 disassemble = disassembler(bfdf);
1775 assert(disassemble);
1779 const struct bpf_line_info *linfo = NULL;
1780 struct disasm_line *dl;
1781 size_t prev_buf_size;
1782 const char *srcline;
1785 addr = pc + ((u64 *)(uintptr_t)(info_linear->info.jited_ksyms))[sub_id];
1786 count = disassemble(pc, &info);
1789 linfo = bpf_prog_linfo__lfind_addr_func(prog_linfo,
1794 srcline = btf__name_by_offset(btf, linfo->line_off);
1800 prev_buf_size = buf_size;
1803 if (!opts->hide_src_code && srcline) {
1805 args->line = strdup(srcline);
1808 dl = disasm_line__new(args);
1810 annotation_line__add(&dl->al,
1811 ¬es->src->source);
1816 args->line = buf + prev_buf_size;
1819 dl = disasm_line__new(args);
1821 annotation_line__add(&dl->al, ¬es->src->source);
1824 } while (count > 0 && pc < len);
1834 #else // defined(HAVE_LIBBFD_SUPPORT) && defined(HAVE_LIBBPF_SUPPORT)
1835 static int symbol__disassemble_bpf(struct symbol *sym __maybe_unused,
1836 struct annotate_args *args __maybe_unused)
1838 return SYMBOL_ANNOTATE_ERRNO__NO_LIBOPCODES_FOR_BPF;
1840 #endif // defined(HAVE_LIBBFD_SUPPORT) && defined(HAVE_LIBBPF_SUPPORT)
1842 static int symbol__disassemble(struct symbol *sym, struct annotate_args *args)
1844 struct annotation_options *opts = args->options;
1845 struct map *map = args->ms.map;
1846 struct dso *dso = map->dso;
1849 char symfs_filename[PATH_MAX];
1850 struct kcore_extract kce;
1851 bool delete_extract = false;
1852 bool decomp = false;
1857 int err = dso__disassemble_filename(dso, symfs_filename, sizeof(symfs_filename));
1862 pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__,
1863 symfs_filename, sym->name, map->unmap_ip(map, sym->start),
1864 map->unmap_ip(map, sym->end));
1866 pr_debug("annotating [%p] %30s : [%p] %30s\n",
1867 dso, dso->long_name, sym, sym->name);
1869 if (dso->binary_type == DSO_BINARY_TYPE__BPF_PROG_INFO) {
1870 return symbol__disassemble_bpf(sym, args);
1871 } else if (dso__is_kcore(dso)) {
1872 kce.kcore_filename = symfs_filename;
1873 kce.addr = map__rip_2objdump(map, sym->start);
1874 kce.offs = sym->start;
1875 kce.len = sym->end - sym->start;
1876 if (!kcore_extract__create(&kce)) {
1877 delete_extract = true;
1878 strlcpy(symfs_filename, kce.extract_filename,
1879 sizeof(symfs_filename));
1881 } else if (dso__needs_decompress(dso)) {
1882 char tmp[KMOD_DECOMP_LEN];
1884 if (dso__decompress_kmodule_path(dso, symfs_filename,
1885 tmp, sizeof(tmp)) < 0)
1889 strcpy(symfs_filename, tmp);
1892 err = asprintf(&command,
1893 "%s %s%s --start-address=0x%016" PRIx64
1894 " --stop-address=0x%016" PRIx64
1895 " -l -d %s %s -C \"$1\" 2>/dev/null|grep -v \"$1:\"|expand",
1896 opts->objdump_path ?: "objdump",
1897 opts->disassembler_style ? "-M " : "",
1898 opts->disassembler_style ?: "",
1899 map__rip_2objdump(map, sym->start),
1900 map__rip_2objdump(map, sym->end),
1901 opts->show_asm_raw ? "" : "--no-show-raw",
1902 opts->annotate_src ? "-S" : "");
1905 pr_err("Failure allocating memory for the command to run\n");
1906 goto out_remove_tmp;
1909 pr_debug("Executing: %s\n", command);
1912 if (pipe(stdout_fd) < 0) {
1913 pr_err("Failure creating the pipe to run %s\n", command);
1914 goto out_free_command;
1919 pr_err("Failure forking to run %s\n", command);
1920 goto out_close_stdout;
1924 close(stdout_fd[0]);
1925 dup2(stdout_fd[1], 1);
1926 close(stdout_fd[1]);
1927 execl("/bin/sh", "sh", "-c", command, "--", symfs_filename,
1933 close(stdout_fd[1]);
1935 file = fdopen(stdout_fd[0], "r");
1937 pr_err("Failure creating FILE stream for %s\n", command);
1939 * If we were using debug info should retry with
1942 goto out_free_command;
1946 while (!feof(file)) {
1948 * The source code line number (lineno) needs to be kept in
1949 * across calls to symbol__parse_objdump_line(), so that it
1950 * can associate it with the instructions till the next one.
1951 * See disasm_line__new() and struct disasm_line::line_nr.
1953 if (symbol__parse_objdump_line(sym, file, args, &lineno) < 0)
1959 pr_err("No output from %s\n", command);
1962 * kallsyms does not have symbol sizes so there may a nop at the end.
1965 if (dso__is_kcore(dso))
1966 delete_last_nop(sym);
1973 close(stdout_fd[0]);
1976 unlink(symfs_filename);
1979 kcore_extract__delete(&kce);
1984 close(stdout_fd[1]);
1985 goto out_free_command;
1988 static void calc_percent(struct sym_hist *sym_hist,
1989 struct hists *hists,
1990 struct annotation_data *data,
1991 s64 offset, s64 end)
1993 unsigned int hits = 0;
1996 while (offset < end) {
1997 hits += sym_hist->addr[offset].nr_samples;
1998 period += sym_hist->addr[offset].period;
2002 if (sym_hist->nr_samples) {
2003 data->he.period = period;
2004 data->he.nr_samples = hits;
2005 data->percent[PERCENT_HITS_LOCAL] = 100.0 * hits / sym_hist->nr_samples;
2008 if (hists->stats.nr_non_filtered_samples)
2009 data->percent[PERCENT_HITS_GLOBAL] = 100.0 * hits / hists->stats.nr_non_filtered_samples;
2011 if (sym_hist->period)
2012 data->percent[PERCENT_PERIOD_LOCAL] = 100.0 * period / sym_hist->period;
2014 if (hists->stats.total_period)
2015 data->percent[PERCENT_PERIOD_GLOBAL] = 100.0 * period / hists->stats.total_period;
2018 static void annotation__calc_percent(struct annotation *notes,
2019 struct evsel *leader, s64 len)
2021 struct annotation_line *al, *next;
2022 struct evsel *evsel;
2024 list_for_each_entry(al, ¬es->src->source, node) {
2028 if (al->offset == -1)
2031 next = annotation_line__next(al, ¬es->src->source);
2032 end = next ? next->offset : len;
2034 for_each_group_evsel(evsel, leader) {
2035 struct hists *hists = evsel__hists(evsel);
2036 struct annotation_data *data;
2037 struct sym_hist *sym_hist;
2039 BUG_ON(i >= al->data_nr);
2041 sym_hist = annotation__histogram(notes, evsel->idx);
2042 data = &al->data[i++];
2044 calc_percent(sym_hist, hists, data, al->offset, end);
2049 void symbol__calc_percent(struct symbol *sym, struct evsel *evsel)
2051 struct annotation *notes = symbol__annotation(sym);
2053 annotation__calc_percent(notes, evsel, symbol__size(sym));
2056 int symbol__annotate(struct symbol *sym, struct map *map,
2057 struct evsel *evsel, size_t privsize,
2058 struct annotation_options *options,
2059 struct arch **parch)
2061 struct annotation *notes = symbol__annotation(sym);
2062 struct annotate_args args = {
2063 .privsize = privsize,
2067 struct perf_env *env = perf_evsel__env(evsel);
2068 const char *arch_name = perf_env__arch(env);
2075 args.arch = arch = arch__find(arch_name);
2083 err = arch->init(arch, env ? env->cpuid : NULL);
2085 pr_err("%s: failed to initialize %s arch priv area\n", __func__, arch->name);
2092 notes->start = map__rip_2objdump(map, sym->start);
2094 return symbol__disassemble(sym, &args);
2097 static void insert_source_line(struct rb_root *root, struct annotation_line *al,
2098 struct annotation_options *opts)
2100 struct annotation_line *iter;
2101 struct rb_node **p = &root->rb_node;
2102 struct rb_node *parent = NULL;
2105 while (*p != NULL) {
2107 iter = rb_entry(parent, struct annotation_line, rb_node);
2109 ret = strcmp(iter->path, al->path);
2111 for (i = 0; i < al->data_nr; i++) {
2112 iter->data[i].percent_sum += annotation_data__percent(&al->data[i],
2113 opts->percent_type);
2121 p = &(*p)->rb_right;
2124 for (i = 0; i < al->data_nr; i++) {
2125 al->data[i].percent_sum = annotation_data__percent(&al->data[i],
2126 opts->percent_type);
2129 rb_link_node(&al->rb_node, parent, p);
2130 rb_insert_color(&al->rb_node, root);
2133 static int cmp_source_line(struct annotation_line *a, struct annotation_line *b)
2137 for (i = 0; i < a->data_nr; i++) {
2138 if (a->data[i].percent_sum == b->data[i].percent_sum)
2140 return a->data[i].percent_sum > b->data[i].percent_sum;
2146 static void __resort_source_line(struct rb_root *root, struct annotation_line *al)
2148 struct annotation_line *iter;
2149 struct rb_node **p = &root->rb_node;
2150 struct rb_node *parent = NULL;
2152 while (*p != NULL) {
2154 iter = rb_entry(parent, struct annotation_line, rb_node);
2156 if (cmp_source_line(al, iter))
2159 p = &(*p)->rb_right;
2162 rb_link_node(&al->rb_node, parent, p);
2163 rb_insert_color(&al->rb_node, root);
2166 static void resort_source_line(struct rb_root *dest_root, struct rb_root *src_root)
2168 struct annotation_line *al;
2169 struct rb_node *node;
2171 node = rb_first(src_root);
2173 struct rb_node *next;
2175 al = rb_entry(node, struct annotation_line, rb_node);
2176 next = rb_next(node);
2177 rb_erase(node, src_root);
2179 __resort_source_line(dest_root, al);
2184 static void print_summary(struct rb_root *root, const char *filename)
2186 struct annotation_line *al;
2187 struct rb_node *node;
2189 printf("\nSorted summary for file %s\n", filename);
2190 printf("----------------------------------------------\n\n");
2192 if (RB_EMPTY_ROOT(root)) {
2193 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
2197 node = rb_first(root);
2199 double percent, percent_max = 0.0;
2204 al = rb_entry(node, struct annotation_line, rb_node);
2205 for (i = 0; i < al->data_nr; i++) {
2206 percent = al->data[i].percent_sum;
2207 color = get_percent_color(percent);
2208 color_fprintf(stdout, color, " %7.2f", percent);
2210 if (percent > percent_max)
2211 percent_max = percent;
2215 color = get_percent_color(percent_max);
2216 color_fprintf(stdout, color, " %s\n", path);
2218 node = rb_next(node);
2222 static void symbol__annotate_hits(struct symbol *sym, struct evsel *evsel)
2224 struct annotation *notes = symbol__annotation(sym);
2225 struct sym_hist *h = annotation__histogram(notes, evsel->idx);
2226 u64 len = symbol__size(sym), offset;
2228 for (offset = 0; offset < len; ++offset)
2229 if (h->addr[offset].nr_samples != 0)
2230 printf("%*" PRIx64 ": %" PRIu64 "\n", BITS_PER_LONG / 2,
2231 sym->start + offset, h->addr[offset].nr_samples);
2232 printf("%*s: %" PRIu64 "\n", BITS_PER_LONG / 2, "h->nr_samples", h->nr_samples);
2235 static int annotated_source__addr_fmt_width(struct list_head *lines, u64 start)
2238 struct annotation_line *line;
2240 list_for_each_entry_reverse(line, lines, node) {
2241 if (line->offset != -1)
2242 return scnprintf(bf, sizeof(bf), "%" PRIx64, start + line->offset);
2248 int symbol__annotate_printf(struct symbol *sym, struct map *map,
2249 struct evsel *evsel,
2250 struct annotation_options *opts)
2252 struct dso *dso = map->dso;
2254 const char *d_filename;
2255 const char *evsel_name = perf_evsel__name(evsel);
2256 struct annotation *notes = symbol__annotation(sym);
2257 struct sym_hist *h = annotation__histogram(notes, evsel->idx);
2258 struct annotation_line *pos, *queue = NULL;
2259 u64 start = map__rip_2objdump(map, sym->start);
2260 int printed = 2, queue_len = 0, addr_fmt_width;
2262 bool context = opts->context;
2264 int width = symbol_conf.show_total_period ? 12 : 8;
2265 int graph_dotted_len;
2268 filename = strdup(dso->long_name);
2272 if (opts->full_path)
2273 d_filename = filename;
2275 d_filename = basename(filename);
2277 len = symbol__size(sym);
2279 if (perf_evsel__is_group_event(evsel)) {
2280 width *= evsel->core.nr_members;
2281 perf_evsel__group_desc(evsel, buf, sizeof(buf));
2285 graph_dotted_len = printf(" %-*.*s| Source code & Disassembly of %s for %s (%" PRIu64 " samples, "
2287 width, width, symbol_conf.show_total_period ? "Period" :
2288 symbol_conf.show_nr_samples ? "Samples" : "Percent",
2289 d_filename, evsel_name, h->nr_samples,
2290 percent_type_str(opts->percent_type));
2292 printf("%-*.*s----\n",
2293 graph_dotted_len, graph_dotted_len, graph_dotted_line);
2296 symbol__annotate_hits(sym, evsel);
2298 addr_fmt_width = annotated_source__addr_fmt_width(¬es->src->source, start);
2300 list_for_each_entry(pos, ¬es->src->source, node) {
2303 if (context && queue == NULL) {
2308 err = annotation_line__print(pos, sym, start, evsel, len,
2309 opts->min_pcnt, printed, opts->max_lines,
2310 queue, addr_fmt_width, opts->percent_type);
2316 printed += queue_len;
2322 /* filtered by max_lines */
2328 * Filtered by min_pcnt or non IP lines when
2333 if (queue_len == context)
2334 queue = list_entry(queue->node.next, typeof(*queue), node);
2346 static void FILE__set_percent_color(void *fp __maybe_unused,
2347 double percent __maybe_unused,
2348 bool current __maybe_unused)
2352 static int FILE__set_jumps_percent_color(void *fp __maybe_unused,
2353 int nr __maybe_unused, bool current __maybe_unused)
2358 static int FILE__set_color(void *fp __maybe_unused, int color __maybe_unused)
2363 static void FILE__printf(void *fp, const char *fmt, ...)
2367 va_start(args, fmt);
2368 vfprintf(fp, fmt, args);
2372 static void FILE__write_graph(void *fp, int graph)
2377 case DARROW_CHAR: s = "↓"; break;
2378 case UARROW_CHAR: s = "↑"; break;
2379 case LARROW_CHAR: s = "←"; break;
2380 case RARROW_CHAR: s = "→"; break;
2381 default: s = "?"; break;
2387 static int symbol__annotate_fprintf2(struct symbol *sym, FILE *fp,
2388 struct annotation_options *opts)
2390 struct annotation *notes = symbol__annotation(sym);
2391 struct annotation_write_ops wops = {
2394 .set_color = FILE__set_color,
2395 .set_percent_color = FILE__set_percent_color,
2396 .set_jumps_percent_color = FILE__set_jumps_percent_color,
2397 .printf = FILE__printf,
2398 .write_graph = FILE__write_graph,
2400 struct annotation_line *al;
2402 list_for_each_entry(al, ¬es->src->source, node) {
2403 if (annotation_line__filter(al, notes))
2405 annotation_line__write(al, notes, &wops, opts);
2407 wops.first_line = false;
2413 int map_symbol__annotation_dump(struct map_symbol *ms, struct evsel *evsel,
2414 struct annotation_options *opts)
2416 const char *ev_name = perf_evsel__name(evsel);
2422 if (asprintf(&filename, "%s.annotation", ms->sym->name) < 0)
2425 fp = fopen(filename, "w");
2427 goto out_free_filename;
2429 if (perf_evsel__is_group_event(evsel)) {
2430 perf_evsel__group_desc(evsel, buf, sizeof(buf));
2434 fprintf(fp, "%s() %s\nEvent: %s\n\n",
2435 ms->sym->name, ms->map->dso->long_name, ev_name);
2436 symbol__annotate_fprintf2(ms->sym, fp, opts);
2445 void symbol__annotate_zero_histogram(struct symbol *sym, int evidx)
2447 struct annotation *notes = symbol__annotation(sym);
2448 struct sym_hist *h = annotation__histogram(notes, evidx);
2450 memset(h, 0, notes->src->sizeof_sym_hist);
2453 void symbol__annotate_decay_histogram(struct symbol *sym, int evidx)
2455 struct annotation *notes = symbol__annotation(sym);
2456 struct sym_hist *h = annotation__histogram(notes, evidx);
2457 int len = symbol__size(sym), offset;
2460 for (offset = 0; offset < len; ++offset) {
2461 h->addr[offset].nr_samples = h->addr[offset].nr_samples * 7 / 8;
2462 h->nr_samples += h->addr[offset].nr_samples;
2466 void annotated_source__purge(struct annotated_source *as)
2468 struct annotation_line *al, *n;
2470 list_for_each_entry_safe(al, n, &as->source, node) {
2471 list_del_init(&al->node);
2472 disasm_line__free(disasm_line(al));
2476 static size_t disasm_line__fprintf(struct disasm_line *dl, FILE *fp)
2480 if (dl->al.offset == -1)
2481 return fprintf(fp, "%s\n", dl->al.line);
2483 printed = fprintf(fp, "%#" PRIx64 " %s", dl->al.offset, dl->ins.name);
2485 if (dl->ops.raw[0] != '\0') {
2486 printed += fprintf(fp, "%.*s %s\n", 6 - (int)printed, " ",
2490 return printed + fprintf(fp, "\n");
2493 size_t disasm__fprintf(struct list_head *head, FILE *fp)
2495 struct disasm_line *pos;
2498 list_for_each_entry(pos, head, al.node)
2499 printed += disasm_line__fprintf(pos, fp);
2504 bool disasm_line__is_valid_local_jump(struct disasm_line *dl, struct symbol *sym)
2506 if (!dl || !dl->ins.ops || !ins__is_jump(&dl->ins) ||
2507 !disasm_line__has_local_offset(dl) || dl->ops.target.offset < 0 ||
2508 dl->ops.target.offset >= (s64)symbol__size(sym))
2514 void annotation__mark_jump_targets(struct annotation *notes, struct symbol *sym)
2516 u64 offset, size = symbol__size(sym);
2518 /* PLT symbols contain external offsets */
2519 if (strstr(sym->name, "@plt"))
2522 for (offset = 0; offset < size; ++offset) {
2523 struct annotation_line *al = notes->offsets[offset];
2524 struct disasm_line *dl;
2526 dl = disasm_line(al);
2528 if (!disasm_line__is_valid_local_jump(dl, sym))
2531 al = notes->offsets[dl->ops.target.offset];
2534 * FIXME: Oops, no jump target? Buggy disassembler? Or do we
2535 * have to adjust to the previous offset?
2540 if (++al->jump_sources > notes->max_jump_sources)
2541 notes->max_jump_sources = al->jump_sources;
2547 void annotation__set_offsets(struct annotation *notes, s64 size)
2549 struct annotation_line *al;
2551 notes->max_line_len = 0;
2553 list_for_each_entry(al, ¬es->src->source, node) {
2554 size_t line_len = strlen(al->line);
2556 if (notes->max_line_len < line_len)
2557 notes->max_line_len = line_len;
2558 al->idx = notes->nr_entries++;
2559 if (al->offset != -1) {
2560 al->idx_asm = notes->nr_asm_entries++;
2562 * FIXME: short term bandaid to cope with assembly
2563 * routines that comes with labels in the same column
2564 * as the address in objdump, sigh.
2566 * E.g. copy_user_generic_unrolled
2568 if (al->offset < size)
2569 notes->offsets[al->offset] = al;
2575 static inline int width_jumps(int n)
2584 static int annotation__max_ins_name(struct annotation *notes)
2586 int max_name = 0, len;
2587 struct annotation_line *al;
2589 list_for_each_entry(al, ¬es->src->source, node) {
2590 if (al->offset == -1)
2593 len = strlen(disasm_line(al)->ins.name);
2601 void annotation__init_column_widths(struct annotation *notes, struct symbol *sym)
2603 notes->widths.addr = notes->widths.target =
2604 notes->widths.min_addr = hex_width(symbol__size(sym));
2605 notes->widths.max_addr = hex_width(sym->end);
2606 notes->widths.jumps = width_jumps(notes->max_jump_sources);
2607 notes->widths.max_ins_name = annotation__max_ins_name(notes);
2610 void annotation__update_column_widths(struct annotation *notes)
2612 if (notes->options->use_offset)
2613 notes->widths.target = notes->widths.min_addr;
2615 notes->widths.target = notes->widths.max_addr;
2617 notes->widths.addr = notes->widths.target;
2619 if (notes->options->show_nr_jumps)
2620 notes->widths.addr += notes->widths.jumps + 1;
2623 static void annotation__calc_lines(struct annotation *notes, struct map *map,
2624 struct rb_root *root,
2625 struct annotation_options *opts)
2627 struct annotation_line *al;
2628 struct rb_root tmp_root = RB_ROOT;
2630 list_for_each_entry(al, ¬es->src->source, node) {
2631 double percent_max = 0.0;
2634 for (i = 0; i < al->data_nr; i++) {
2637 percent = annotation_data__percent(&al->data[i],
2638 opts->percent_type);
2640 if (percent > percent_max)
2641 percent_max = percent;
2644 if (percent_max <= 0.5)
2647 al->path = get_srcline(map->dso, notes->start + al->offset, NULL,
2648 false, true, notes->start + al->offset);
2649 insert_source_line(&tmp_root, al, opts);
2652 resort_source_line(root, &tmp_root);
2655 static void symbol__calc_lines(struct symbol *sym, struct map *map,
2656 struct rb_root *root,
2657 struct annotation_options *opts)
2659 struct annotation *notes = symbol__annotation(sym);
2661 annotation__calc_lines(notes, map, root, opts);
2664 int symbol__tty_annotate2(struct symbol *sym, struct map *map,
2665 struct evsel *evsel,
2666 struct annotation_options *opts)
2668 struct dso *dso = map->dso;
2669 struct rb_root source_line = RB_ROOT;
2670 struct hists *hists = evsel__hists(evsel);
2673 if (symbol__annotate2(sym, map, evsel, opts, NULL) < 0)
2676 if (opts->print_lines) {
2677 srcline_full_filename = opts->full_path;
2678 symbol__calc_lines(sym, map, &source_line, opts);
2679 print_summary(&source_line, dso->long_name);
2682 hists__scnprintf_title(hists, buf, sizeof(buf));
2683 fprintf(stdout, "%s, [percent: %s]\n%s() %s\n",
2684 buf, percent_type_str(opts->percent_type), sym->name, dso->long_name);
2685 symbol__annotate_fprintf2(sym, stdout, opts);
2687 annotated_source__purge(symbol__annotation(sym)->src);
2692 int symbol__tty_annotate(struct symbol *sym, struct map *map,
2693 struct evsel *evsel,
2694 struct annotation_options *opts)
2696 struct dso *dso = map->dso;
2697 struct rb_root source_line = RB_ROOT;
2699 if (symbol__annotate(sym, map, evsel, 0, opts, NULL) < 0)
2702 symbol__calc_percent(sym, evsel);
2704 if (opts->print_lines) {
2705 srcline_full_filename = opts->full_path;
2706 symbol__calc_lines(sym, map, &source_line, opts);
2707 print_summary(&source_line, dso->long_name);
2710 symbol__annotate_printf(sym, map, evsel, opts);
2712 annotated_source__purge(symbol__annotation(sym)->src);
2717 bool ui__has_annotation(void)
2719 return use_browser == 1 && perf_hpp_list.sym;
2723 static double annotation_line__max_percent(struct annotation_line *al,
2724 struct annotation *notes,
2725 unsigned int percent_type)
2727 double percent_max = 0.0;
2730 for (i = 0; i < notes->nr_events; i++) {
2733 percent = annotation_data__percent(&al->data[i],
2736 if (percent > percent_max)
2737 percent_max = percent;
2743 static void disasm_line__write(struct disasm_line *dl, struct annotation *notes,
2744 void *obj, char *bf, size_t size,
2745 void (*obj__printf)(void *obj, const char *fmt, ...),
2746 void (*obj__write_graph)(void *obj, int graph))
2748 if (dl->ins.ops && dl->ins.ops->scnprintf) {
2749 if (ins__is_jump(&dl->ins)) {
2752 if (dl->ops.target.outside)
2754 fwd = dl->ops.target.offset > dl->al.offset;
2755 obj__write_graph(obj, fwd ? DARROW_CHAR : UARROW_CHAR);
2756 obj__printf(obj, " ");
2757 } else if (ins__is_call(&dl->ins)) {
2759 obj__write_graph(obj, RARROW_CHAR);
2760 obj__printf(obj, " ");
2761 } else if (ins__is_ret(&dl->ins)) {
2762 obj__write_graph(obj, LARROW_CHAR);
2763 obj__printf(obj, " ");
2765 obj__printf(obj, " ");
2768 obj__printf(obj, " ");
2771 disasm_line__scnprintf(dl, bf, size, !notes->options->use_offset, notes->widths.max_ins_name);
2774 static void ipc_coverage_string(char *bf, int size, struct annotation *notes)
2776 double ipc = 0.0, coverage = 0.0;
2778 if (notes->hit_cycles)
2779 ipc = notes->hit_insn / ((double)notes->hit_cycles);
2781 if (notes->total_insn) {
2782 coverage = notes->cover_insn * 100.0 /
2783 ((double)notes->total_insn);
2786 scnprintf(bf, size, "(Average IPC: %.2f, IPC Coverage: %.1f%%)",
2790 static void __annotation_line__write(struct annotation_line *al, struct annotation *notes,
2791 bool first_line, bool current_entry, bool change_color, int width,
2792 void *obj, unsigned int percent_type,
2793 int (*obj__set_color)(void *obj, int color),
2794 void (*obj__set_percent_color)(void *obj, double percent, bool current),
2795 int (*obj__set_jumps_percent_color)(void *obj, int nr, bool current),
2796 void (*obj__printf)(void *obj, const char *fmt, ...),
2797 void (*obj__write_graph)(void *obj, int graph))
2800 double percent_max = annotation_line__max_percent(al, notes, percent_type);
2801 int pcnt_width = annotation__pcnt_width(notes),
2802 cycles_width = annotation__cycles_width(notes);
2803 bool show_title = false;
2807 if (first_line && (al->offset == -1 || percent_max == 0.0)) {
2808 if (notes->have_cycles) {
2809 if (al->ipc == 0.0 && al->cycles == 0)
2815 if (al->offset != -1 && percent_max != 0.0) {
2818 for (i = 0; i < notes->nr_events; i++) {
2821 percent = annotation_data__percent(&al->data[i], percent_type);
2823 obj__set_percent_color(obj, percent, current_entry);
2824 if (notes->options->show_total_period) {
2825 obj__printf(obj, "%11" PRIu64 " ", al->data[i].he.period);
2826 } else if (notes->options->show_nr_samples) {
2827 obj__printf(obj, "%6" PRIu64 " ",
2828 al->data[i].he.nr_samples);
2830 obj__printf(obj, "%6.2f ", percent);
2834 obj__set_percent_color(obj, 0, current_entry);
2837 obj__printf(obj, "%-*s", pcnt_width, " ");
2839 obj__printf(obj, "%-*s", pcnt_width,
2840 notes->options->show_total_period ? "Period" :
2841 notes->options->show_nr_samples ? "Samples" : "Percent");
2845 if (notes->have_cycles) {
2847 obj__printf(obj, "%*.2f ", ANNOTATION__IPC_WIDTH - 1, al->ipc);
2848 else if (!show_title)
2849 obj__printf(obj, "%*s", ANNOTATION__IPC_WIDTH, " ");
2851 obj__printf(obj, "%*s ", ANNOTATION__IPC_WIDTH - 1, "IPC");
2853 if (!notes->options->show_minmax_cycle) {
2855 obj__printf(obj, "%*" PRIu64 " ",
2856 ANNOTATION__CYCLES_WIDTH - 1, al->cycles);
2857 else if (!show_title)
2858 obj__printf(obj, "%*s",
2859 ANNOTATION__CYCLES_WIDTH, " ");
2861 obj__printf(obj, "%*s ",
2862 ANNOTATION__CYCLES_WIDTH - 1,
2868 scnprintf(str, sizeof(str),
2869 "%" PRIu64 "(%" PRIu64 "/%" PRIu64 ")",
2870 al->cycles, al->cycles_min,
2873 obj__printf(obj, "%*s ",
2874 ANNOTATION__MINMAX_CYCLES_WIDTH - 1,
2876 } else if (!show_title)
2877 obj__printf(obj, "%*s",
2878 ANNOTATION__MINMAX_CYCLES_WIDTH,
2881 obj__printf(obj, "%*s ",
2882 ANNOTATION__MINMAX_CYCLES_WIDTH - 1,
2886 if (show_title && !*al->line) {
2887 ipc_coverage_string(bf, sizeof(bf), notes);
2888 obj__printf(obj, "%*s", ANNOTATION__AVG_IPC_WIDTH, bf);
2892 obj__printf(obj, " ");
2895 obj__printf(obj, "%-*s", width - pcnt_width - cycles_width, " ");
2896 else if (al->offset == -1) {
2897 if (al->line_nr && notes->options->show_linenr)
2898 printed = scnprintf(bf, sizeof(bf), "%-*d ", notes->widths.addr + 1, al->line_nr);
2900 printed = scnprintf(bf, sizeof(bf), "%-*s ", notes->widths.addr, " ");
2901 obj__printf(obj, bf);
2902 obj__printf(obj, "%-*s", width - printed - pcnt_width - cycles_width + 1, al->line);
2904 u64 addr = al->offset;
2907 if (!notes->options->use_offset)
2908 addr += notes->start;
2910 if (!notes->options->use_offset) {
2911 printed = scnprintf(bf, sizeof(bf), "%" PRIx64 ": ", addr);
2913 if (al->jump_sources &&
2914 notes->options->offset_level >= ANNOTATION__OFFSET_JUMP_TARGETS) {
2915 if (notes->options->show_nr_jumps) {
2917 printed = scnprintf(bf, sizeof(bf), "%*d ",
2918 notes->widths.jumps,
2920 prev = obj__set_jumps_percent_color(obj, al->jump_sources,
2922 obj__printf(obj, bf);
2923 obj__set_color(obj, prev);
2926 printed = scnprintf(bf, sizeof(bf), "%*" PRIx64 ": ",
2927 notes->widths.target, addr);
2928 } else if (ins__is_call(&disasm_line(al)->ins) &&
2929 notes->options->offset_level >= ANNOTATION__OFFSET_CALL) {
2931 } else if (notes->options->offset_level == ANNOTATION__MAX_OFFSET_LEVEL) {
2934 printed = scnprintf(bf, sizeof(bf), "%-*s ",
2935 notes->widths.addr, " ");
2940 color = obj__set_color(obj, HE_COLORSET_ADDR);
2941 obj__printf(obj, bf);
2943 obj__set_color(obj, color);
2945 disasm_line__write(disasm_line(al), notes, obj, bf, sizeof(bf), obj__printf, obj__write_graph);
2947 obj__printf(obj, "%-*s", width - pcnt_width - cycles_width - 3 - printed, bf);
2952 void annotation_line__write(struct annotation_line *al, struct annotation *notes,
2953 struct annotation_write_ops *wops,
2954 struct annotation_options *opts)
2956 __annotation_line__write(al, notes, wops->first_line, wops->current_entry,
2957 wops->change_color, wops->width, wops->obj,
2959 wops->set_color, wops->set_percent_color,
2960 wops->set_jumps_percent_color, wops->printf,
2964 int symbol__annotate2(struct symbol *sym, struct map *map, struct evsel *evsel,
2965 struct annotation_options *options, struct arch **parch)
2967 struct annotation *notes = symbol__annotation(sym);
2968 size_t size = symbol__size(sym);
2969 int nr_pcnt = 1, err;
2971 notes->offsets = zalloc(size * sizeof(struct annotation_line *));
2972 if (notes->offsets == NULL)
2975 if (perf_evsel__is_group_event(evsel))
2976 nr_pcnt = evsel->core.nr_members;
2978 err = symbol__annotate(sym, map, evsel, 0, options, parch);
2980 goto out_free_offsets;
2982 notes->options = options;
2984 symbol__calc_percent(sym, evsel);
2986 annotation__set_offsets(notes, size);
2987 annotation__mark_jump_targets(notes, sym);
2988 annotation__compute_ipc(notes, size);
2989 annotation__init_column_widths(notes, sym);
2990 notes->nr_events = nr_pcnt;
2992 annotation__update_column_widths(notes);
2993 sym->annotate2 = true;
2998 zfree(¬es->offsets);
3002 #define ANNOTATION__CFG(n) \
3003 { .name = #n, .value = &annotation__default_options.n, }
3006 * Keep the entries sorted, they are bsearch'ed
3008 static struct annotation_config {
3011 } annotation__configs[] = {
3012 ANNOTATION__CFG(hide_src_code),
3013 ANNOTATION__CFG(jump_arrows),
3014 ANNOTATION__CFG(offset_level),
3015 ANNOTATION__CFG(show_linenr),
3016 ANNOTATION__CFG(show_nr_jumps),
3017 ANNOTATION__CFG(show_nr_samples),
3018 ANNOTATION__CFG(show_total_period),
3019 ANNOTATION__CFG(use_offset),
3022 #undef ANNOTATION__CFG
3024 static int annotation_config__cmp(const void *name, const void *cfgp)
3026 const struct annotation_config *cfg = cfgp;
3028 return strcmp(name, cfg->name);
3031 static int annotation__config(const char *var, const char *value,
3032 void *data __maybe_unused)
3034 struct annotation_config *cfg;
3037 if (!strstarts(var, "annotate."))
3041 cfg = bsearch(name, annotation__configs, ARRAY_SIZE(annotation__configs),
3042 sizeof(struct annotation_config), annotation_config__cmp);
3045 pr_debug("%s variable unknown, ignoring...", var);
3046 else if (strcmp(var, "annotate.offset_level") == 0) {
3047 perf_config_int(cfg->value, name, value);
3049 if (*(int *)cfg->value > ANNOTATION__MAX_OFFSET_LEVEL)
3050 *(int *)cfg->value = ANNOTATION__MAX_OFFSET_LEVEL;
3051 else if (*(int *)cfg->value < ANNOTATION__MIN_OFFSET_LEVEL)
3052 *(int *)cfg->value = ANNOTATION__MIN_OFFSET_LEVEL;
3054 *(bool *)cfg->value = perf_config_bool(name, value);
3059 void annotation_config__init(void)
3061 perf_config(annotation__config, NULL);
3063 annotation__default_options.show_total_period = symbol_conf.show_total_period;
3064 annotation__default_options.show_nr_samples = symbol_conf.show_nr_samples;
3067 static unsigned int parse_percent_type(char *str1, char *str2)
3069 unsigned int type = (unsigned int) -1;
3071 if (!strcmp("period", str1)) {
3072 if (!strcmp("local", str2))
3073 type = PERCENT_PERIOD_LOCAL;
3074 else if (!strcmp("global", str2))
3075 type = PERCENT_PERIOD_GLOBAL;
3078 if (!strcmp("hits", str1)) {
3079 if (!strcmp("local", str2))
3080 type = PERCENT_HITS_LOCAL;
3081 else if (!strcmp("global", str2))
3082 type = PERCENT_HITS_GLOBAL;
3088 int annotate_parse_percent_type(const struct option *opt, const char *_str,
3089 int unset __maybe_unused)
3091 struct annotation_options *opts = opt->value;
3096 str1 = strdup(_str);
3100 str2 = strchr(str1, '-');
3106 type = parse_percent_type(str1, str2);
3107 if (type == (unsigned int) -1)
3108 type = parse_percent_type(str2, str1);
3109 if (type != (unsigned int) -1) {
3110 opts->percent_type = type;