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)
15 #include <bpf/libbpf.h>
16 #include <linux/btf.h>
31 #include "bpf-event.h"
32 #include "block-range.h"
34 #include "arch/common.h"
37 #include <linux/bitops.h>
38 #include <linux/kernel.h>
39 #include <bpf/libbpf.h>
41 /* FIXME: For the HE_COLORSET */
42 #include "ui/browser.h"
45 * FIXME: Using the same values as slang.h,
46 * but that header may not be available everywhere
48 #define LARROW_CHAR ((unsigned char)',')
49 #define RARROW_CHAR ((unsigned char)'+')
50 #define DARROW_CHAR ((unsigned char)'.')
51 #define UARROW_CHAR ((unsigned char)'-')
53 #include "sane_ctype.h"
55 struct annotation_options annotation__default_options
= {
59 .offset_level
= ANNOTATION__OFFSET_JUMP_TARGETS
,
60 .percent_type
= PERCENT_PERIOD_LOCAL
,
63 static regex_t file_lineno
;
65 static struct ins_ops
*ins__find(struct arch
*arch
, const char *name
);
66 static void ins__sort(struct arch
*arch
);
67 static int disasm_line__parse(char *line
, const char **namep
, char **rawp
);
71 struct ins
*instructions
;
72 size_t nr_instructions
;
73 size_t nr_instructions_allocated
;
74 struct ins_ops
*(*associate_instruction_ops
)(struct arch
*arch
, const char *name
);
75 bool sorted_instructions
;
80 int (*init
)(struct arch
*arch
, char *cpuid
);
81 bool (*ins_is_fused
)(struct arch
*arch
, const char *ins1
,
85 char skip_functions_char
;
89 static struct ins_ops call_ops
;
90 static struct ins_ops dec_ops
;
91 static struct ins_ops jump_ops
;
92 static struct ins_ops mov_ops
;
93 static struct ins_ops nop_ops
;
94 static struct ins_ops lock_ops
;
95 static struct ins_ops ret_ops
;
97 static int arch__grow_instructions(struct arch
*arch
)
99 struct ins
*new_instructions
;
100 size_t new_nr_allocated
;
102 if (arch
->nr_instructions_allocated
== 0 && arch
->instructions
)
103 goto grow_from_non_allocated_table
;
105 new_nr_allocated
= arch
->nr_instructions_allocated
+ 128;
106 new_instructions
= realloc(arch
->instructions
, new_nr_allocated
* sizeof(struct ins
));
107 if (new_instructions
== NULL
)
110 out_update_instructions
:
111 arch
->instructions
= new_instructions
;
112 arch
->nr_instructions_allocated
= new_nr_allocated
;
115 grow_from_non_allocated_table
:
116 new_nr_allocated
= arch
->nr_instructions
+ 128;
117 new_instructions
= calloc(new_nr_allocated
, sizeof(struct ins
));
118 if (new_instructions
== NULL
)
121 memcpy(new_instructions
, arch
->instructions
, arch
->nr_instructions
);
122 goto out_update_instructions
;
125 static int arch__associate_ins_ops(struct arch
* arch
, const char *name
, struct ins_ops
*ops
)
129 if (arch
->nr_instructions
== arch
->nr_instructions_allocated
&&
130 arch__grow_instructions(arch
))
133 ins
= &arch
->instructions
[arch
->nr_instructions
];
134 ins
->name
= strdup(name
);
139 arch
->nr_instructions
++;
145 #include "arch/arc/annotate/instructions.c"
146 #include "arch/arm/annotate/instructions.c"
147 #include "arch/arm64/annotate/instructions.c"
148 #include "arch/x86/annotate/instructions.c"
149 #include "arch/powerpc/annotate/instructions.c"
150 #include "arch/s390/annotate/instructions.c"
151 #include "arch/sparc/annotate/instructions.c"
153 static struct arch architectures
[] = {
156 .init
= arc__annotate_init
,
160 .init
= arm__annotate_init
,
164 .init
= arm64__annotate_init
,
168 .init
= x86__annotate_init
,
169 .instructions
= x86__instructions
,
170 .nr_instructions
= ARRAY_SIZE(x86__instructions
),
171 .ins_is_fused
= x86__ins_is_fused
,
178 .init
= powerpc__annotate_init
,
182 .init
= s390__annotate_init
,
189 .init
= sparc__annotate_init
,
196 static void ins__delete(struct ins_operands
*ops
)
200 zfree(&ops
->source
.raw
);
201 zfree(&ops
->source
.name
);
202 zfree(&ops
->target
.raw
);
203 zfree(&ops
->target
.name
);
206 static int ins__raw_scnprintf(struct ins
*ins
, char *bf
, size_t size
,
207 struct ins_operands
*ops
, int max_ins_name
)
209 return scnprintf(bf
, size
, "%-*s %s", max_ins_name
, ins
->name
, ops
->raw
);
212 int ins__scnprintf(struct ins
*ins
, char *bf
, size_t size
,
213 struct ins_operands
*ops
, int max_ins_name
)
215 if (ins
->ops
->scnprintf
)
216 return ins
->ops
->scnprintf(ins
, bf
, size
, ops
, max_ins_name
);
218 return ins__raw_scnprintf(ins
, bf
, size
, ops
, max_ins_name
);
221 bool ins__is_fused(struct arch
*arch
, const char *ins1
, const char *ins2
)
223 if (!arch
|| !arch
->ins_is_fused
)
226 return arch
->ins_is_fused(arch
, ins1
, ins2
);
229 static int call__parse(struct arch
*arch
, struct ins_operands
*ops
, struct map_symbol
*ms
)
231 char *endptr
, *tok
, *name
;
232 struct map
*map
= ms
->map
;
233 struct addr_map_symbol target
= {
237 ops
->target
.addr
= strtoull(ops
->raw
, &endptr
, 16);
239 name
= strchr(endptr
, '<');
245 if (arch
->objdump
.skip_functions_char
&&
246 strchr(name
, arch
->objdump
.skip_functions_char
))
249 tok
= strchr(name
, '>');
254 ops
->target
.name
= strdup(name
);
257 if (ops
->target
.name
== NULL
)
260 target
.addr
= map__objdump_2mem(map
, ops
->target
.addr
);
262 if (map_groups__find_ams(&target
) == 0 &&
263 map__rip_2objdump(target
.map
, map
->map_ip(target
.map
, target
.addr
)) == ops
->target
.addr
)
264 ops
->target
.sym
= target
.sym
;
269 tok
= strchr(endptr
, '*');
273 /* Indirect call can use a non-rip register and offset: callq *0x8(%rbx).
274 * Do not parse such instruction. */
275 if (strstr(endptr
, "(%r") == NULL
)
276 ops
->target
.addr
= strtoull(endptr
, NULL
, 16);
281 static int call__scnprintf(struct ins
*ins
, char *bf
, size_t size
,
282 struct ins_operands
*ops
, int max_ins_name
)
285 return scnprintf(bf
, size
, "%-*s %s", max_ins_name
, ins
->name
, ops
->target
.sym
->name
);
287 if (ops
->target
.addr
== 0)
288 return ins__raw_scnprintf(ins
, bf
, size
, ops
, max_ins_name
);
290 if (ops
->target
.name
)
291 return scnprintf(bf
, size
, "%-*s %s", max_ins_name
, ins
->name
, ops
->target
.name
);
293 return scnprintf(bf
, size
, "%-*s *%" PRIx64
, max_ins_name
, ins
->name
, ops
->target
.addr
);
296 static struct ins_ops call_ops
= {
297 .parse
= call__parse
,
298 .scnprintf
= call__scnprintf
,
301 bool ins__is_call(const struct ins
*ins
)
303 return ins
->ops
== &call_ops
|| ins
->ops
== &s390_call_ops
;
307 * Prevents from matching commas in the comment section, e.g.:
308 * ffff200008446e70: b.cs ffff2000084470f4 <generic_exec_single+0x314> // b.hs, b.nlast
310 static inline const char *validate_comma(const char *c
, struct ins_operands
*ops
)
312 if (ops
->raw_comment
&& c
> ops
->raw_comment
)
318 static int jump__parse(struct arch
*arch
, struct ins_operands
*ops
, struct map_symbol
*ms
)
320 struct map
*map
= ms
->map
;
321 struct symbol
*sym
= ms
->sym
;
322 struct addr_map_symbol target
= {
325 const char *c
= strchr(ops
->raw
, ',');
328 ops
->raw_comment
= strchr(ops
->raw
, arch
->objdump
.comment_char
);
329 c
= validate_comma(c
, ops
);
332 * Examples of lines to parse for the _cpp_lex_token@@Base
335 * 1159e6c: jne 115aa32 <_cpp_lex_token@@Base+0xf92>
336 * 1159e8b: jne c469be <cpp_named_operator2name@@Base+0xa72>
338 * The first is a jump to an offset inside the same function,
339 * the second is to another function, i.e. that 0xa72 is an
340 * offset in the cpp_named_operator2name@@base function.
343 * skip over possible up to 2 operands to get to address, e.g.:
344 * tbnz w0, #26, ffff0000083cd190 <security_file_permission+0xd0>
347 ops
->target
.addr
= strtoull(c
, NULL
, 16);
348 if (!ops
->target
.addr
) {
350 c
= validate_comma(c
, ops
);
352 ops
->target
.addr
= strtoull(c
, NULL
, 16);
355 ops
->target
.addr
= strtoull(ops
->raw
, NULL
, 16);
358 target
.addr
= map__objdump_2mem(map
, ops
->target
.addr
);
359 start
= map
->unmap_ip(map
, sym
->start
),
360 end
= map
->unmap_ip(map
, sym
->end
);
362 ops
->target
.outside
= target
.addr
< start
|| target
.addr
> end
;
365 * FIXME: things like this in _cpp_lex_token (gcc's cc1 program):
367 cpp_named_operator2name@@Base+0xa72
369 * Point to a place that is after the cpp_named_operator2name
370 * boundaries, i.e. in the ELF symbol table for cc1
371 * cpp_named_operator2name is marked as being 32-bytes long, but it in
372 * fact is much larger than that, so we seem to need a symbols__find()
373 * routine that looks for >= current->start and < next_symbol->start,
374 * possibly just for C++ objects?
376 * For now lets just make some progress by marking jumps to outside the
377 * current function as call like.
379 * Actual navigation will come next, with further understanding of how
380 * the symbol searching and disassembly should be done.
382 if (map_groups__find_ams(&target
) == 0 &&
383 map__rip_2objdump(target
.map
, map
->map_ip(target
.map
, target
.addr
)) == ops
->target
.addr
)
384 ops
->target
.sym
= target
.sym
;
386 if (!ops
->target
.outside
) {
387 ops
->target
.offset
= target
.addr
- start
;
388 ops
->target
.offset_avail
= true;
390 ops
->target
.offset_avail
= false;
396 static int jump__scnprintf(struct ins
*ins
, char *bf
, size_t size
,
397 struct ins_operands
*ops
, int max_ins_name
)
401 if (!ops
->target
.addr
|| ops
->target
.offset
< 0)
402 return ins__raw_scnprintf(ins
, bf
, size
, ops
, max_ins_name
);
404 if (ops
->target
.outside
&& ops
->target
.sym
!= NULL
)
405 return scnprintf(bf
, size
, "%-*s %s", max_ins_name
, ins
->name
, ops
->target
.sym
->name
);
407 c
= strchr(ops
->raw
, ',');
408 c
= validate_comma(c
, ops
);
411 const char *c2
= strchr(c
+ 1, ',');
413 c2
= validate_comma(c2
, ops
);
414 /* check for 3-op insn */
419 /* mirror arch objdump's space-after-comma style */
424 return scnprintf(bf
, size
, "%-*s %.*s%" PRIx64
, max_ins_name
,
425 ins
->name
, c
? c
- ops
->raw
: 0, ops
->raw
,
429 static struct ins_ops jump_ops
= {
430 .parse
= jump__parse
,
431 .scnprintf
= jump__scnprintf
,
434 bool ins__is_jump(const struct ins
*ins
)
436 return ins
->ops
== &jump_ops
;
439 static int comment__symbol(char *raw
, char *comment
, u64
*addrp
, char **namep
)
441 char *endptr
, *name
, *t
;
443 if (strstr(raw
, "(%rip)") == NULL
)
446 *addrp
= strtoull(comment
, &endptr
, 16);
447 if (endptr
== comment
)
449 name
= strchr(endptr
, '<');
455 t
= strchr(name
, '>');
460 *namep
= strdup(name
);
466 static int lock__parse(struct arch
*arch
, struct ins_operands
*ops
, struct map_symbol
*ms
)
468 ops
->locked
.ops
= zalloc(sizeof(*ops
->locked
.ops
));
469 if (ops
->locked
.ops
== NULL
)
472 if (disasm_line__parse(ops
->raw
, &ops
->locked
.ins
.name
, &ops
->locked
.ops
->raw
) < 0)
475 ops
->locked
.ins
.ops
= ins__find(arch
, ops
->locked
.ins
.name
);
477 if (ops
->locked
.ins
.ops
== NULL
)
480 if (ops
->locked
.ins
.ops
->parse
&&
481 ops
->locked
.ins
.ops
->parse(arch
, ops
->locked
.ops
, ms
) < 0)
487 zfree(&ops
->locked
.ops
);
491 static int lock__scnprintf(struct ins
*ins
, char *bf
, size_t size
,
492 struct ins_operands
*ops
, int max_ins_name
)
496 if (ops
->locked
.ins
.ops
== NULL
)
497 return ins__raw_scnprintf(ins
, bf
, size
, ops
, max_ins_name
);
499 printed
= scnprintf(bf
, size
, "%-*s ", max_ins_name
, ins
->name
);
500 return printed
+ ins__scnprintf(&ops
->locked
.ins
, bf
+ printed
,
501 size
- printed
, ops
->locked
.ops
, max_ins_name
);
504 static void lock__delete(struct ins_operands
*ops
)
506 struct ins
*ins
= &ops
->locked
.ins
;
508 if (ins
->ops
&& ins
->ops
->free
)
509 ins
->ops
->free(ops
->locked
.ops
);
511 ins__delete(ops
->locked
.ops
);
513 zfree(&ops
->locked
.ops
);
514 zfree(&ops
->target
.raw
);
515 zfree(&ops
->target
.name
);
518 static struct ins_ops lock_ops
= {
519 .free
= lock__delete
,
520 .parse
= lock__parse
,
521 .scnprintf
= lock__scnprintf
,
524 static int mov__parse(struct arch
*arch
, struct ins_operands
*ops
, struct map_symbol
*ms __maybe_unused
)
526 char *s
= strchr(ops
->raw
, ','), *target
, *comment
, prev
;
532 ops
->source
.raw
= strdup(ops
->raw
);
535 if (ops
->source
.raw
== NULL
)
539 comment
= strchr(s
, arch
->objdump
.comment_char
);
544 s
= strchr(s
, '\0') - 1;
546 while (s
> target
&& isspace(s
[0]))
552 ops
->target
.raw
= strdup(target
);
555 if (ops
->target
.raw
== NULL
)
556 goto out_free_source
;
561 comment
= ltrim(comment
);
562 comment__symbol(ops
->source
.raw
, comment
+ 1, &ops
->source
.addr
, &ops
->source
.name
);
563 comment__symbol(ops
->target
.raw
, comment
+ 1, &ops
->target
.addr
, &ops
->target
.name
);
568 zfree(&ops
->source
.raw
);
572 static int mov__scnprintf(struct ins
*ins
, char *bf
, size_t size
,
573 struct ins_operands
*ops
, int max_ins_name
)
575 return scnprintf(bf
, size
, "%-*s %s,%s", max_ins_name
, ins
->name
,
576 ops
->source
.name
?: ops
->source
.raw
,
577 ops
->target
.name
?: ops
->target
.raw
);
580 static struct ins_ops mov_ops
= {
582 .scnprintf
= mov__scnprintf
,
585 static int dec__parse(struct arch
*arch __maybe_unused
, struct ins_operands
*ops
, struct map_symbol
*ms __maybe_unused
)
587 char *target
, *comment
, *s
, prev
;
589 target
= s
= ops
->raw
;
591 while (s
[0] != '\0' && !isspace(s
[0]))
596 ops
->target
.raw
= strdup(target
);
599 if (ops
->target
.raw
== NULL
)
602 comment
= strchr(s
, arch
->objdump
.comment_char
);
606 comment
= ltrim(comment
);
607 comment__symbol(ops
->target
.raw
, comment
+ 1, &ops
->target
.addr
, &ops
->target
.name
);
612 static int dec__scnprintf(struct ins
*ins
, char *bf
, size_t size
,
613 struct ins_operands
*ops
, int max_ins_name
)
615 return scnprintf(bf
, size
, "%-*s %s", max_ins_name
, ins
->name
,
616 ops
->target
.name
?: ops
->target
.raw
);
619 static struct ins_ops dec_ops
= {
621 .scnprintf
= dec__scnprintf
,
624 static int nop__scnprintf(struct ins
*ins __maybe_unused
, char *bf
, size_t size
,
625 struct ins_operands
*ops __maybe_unused
, int max_ins_name
)
627 return scnprintf(bf
, size
, "%-*s", max_ins_name
, "nop");
630 static struct ins_ops nop_ops
= {
631 .scnprintf
= nop__scnprintf
,
634 static struct ins_ops ret_ops
= {
635 .scnprintf
= ins__raw_scnprintf
,
638 bool ins__is_ret(const struct ins
*ins
)
640 return ins
->ops
== &ret_ops
;
643 bool ins__is_lock(const struct ins
*ins
)
645 return ins
->ops
== &lock_ops
;
648 static int ins__key_cmp(const void *name
, const void *insp
)
650 const struct ins
*ins
= insp
;
652 return strcmp(name
, ins
->name
);
655 static int ins__cmp(const void *a
, const void *b
)
657 const struct ins
*ia
= a
;
658 const struct ins
*ib
= b
;
660 return strcmp(ia
->name
, ib
->name
);
663 static void ins__sort(struct arch
*arch
)
665 const int nmemb
= arch
->nr_instructions
;
667 qsort(arch
->instructions
, nmemb
, sizeof(struct ins
), ins__cmp
);
670 static struct ins_ops
*__ins__find(struct arch
*arch
, const char *name
)
673 const int nmemb
= arch
->nr_instructions
;
675 if (!arch
->sorted_instructions
) {
677 arch
->sorted_instructions
= true;
680 ins
= bsearch(name
, arch
->instructions
, nmemb
, sizeof(struct ins
), ins__key_cmp
);
681 return ins
? ins
->ops
: NULL
;
684 static struct ins_ops
*ins__find(struct arch
*arch
, const char *name
)
686 struct ins_ops
*ops
= __ins__find(arch
, name
);
688 if (!ops
&& arch
->associate_instruction_ops
)
689 ops
= arch
->associate_instruction_ops(arch
, name
);
694 static int arch__key_cmp(const void *name
, const void *archp
)
696 const struct arch
*arch
= archp
;
698 return strcmp(name
, arch
->name
);
701 static int arch__cmp(const void *a
, const void *b
)
703 const struct arch
*aa
= a
;
704 const struct arch
*ab
= b
;
706 return strcmp(aa
->name
, ab
->name
);
709 static void arch__sort(void)
711 const int nmemb
= ARRAY_SIZE(architectures
);
713 qsort(architectures
, nmemb
, sizeof(struct arch
), arch__cmp
);
716 static struct arch
*arch__find(const char *name
)
718 const int nmemb
= ARRAY_SIZE(architectures
);
726 return bsearch(name
, architectures
, nmemb
, sizeof(struct arch
), arch__key_cmp
);
729 static struct annotated_source
*annotated_source__new(void)
731 struct annotated_source
*src
= zalloc(sizeof(*src
));
734 INIT_LIST_HEAD(&src
->source
);
739 static __maybe_unused
void annotated_source__delete(struct annotated_source
*src
)
743 zfree(&src
->histograms
);
744 zfree(&src
->cycles_hist
);
748 static int annotated_source__alloc_histograms(struct annotated_source
*src
,
749 size_t size
, int nr_hists
)
751 size_t sizeof_sym_hist
;
754 * Add buffer of one element for zero length symbol.
755 * When sample is taken from first instruction of
756 * zero length symbol, perf still resolves it and
757 * shows symbol name in perf report and allows to
763 /* Check for overflow when calculating sizeof_sym_hist */
764 if (size
> (SIZE_MAX
- sizeof(struct sym_hist
)) / sizeof(struct sym_hist_entry
))
767 sizeof_sym_hist
= (sizeof(struct sym_hist
) + size
* sizeof(struct sym_hist_entry
));
769 /* Check for overflow in zalloc argument */
770 if (sizeof_sym_hist
> SIZE_MAX
/ nr_hists
)
773 src
->sizeof_sym_hist
= sizeof_sym_hist
;
774 src
->nr_histograms
= nr_hists
;
775 src
->histograms
= calloc(nr_hists
, sizeof_sym_hist
) ;
776 return src
->histograms
? 0 : -1;
779 /* The cycles histogram is lazily allocated. */
780 static int symbol__alloc_hist_cycles(struct symbol
*sym
)
782 struct annotation
*notes
= symbol__annotation(sym
);
783 const size_t size
= symbol__size(sym
);
785 notes
->src
->cycles_hist
= calloc(size
, sizeof(struct cyc_hist
));
786 if (notes
->src
->cycles_hist
== NULL
)
791 void symbol__annotate_zero_histograms(struct symbol
*sym
)
793 struct annotation
*notes
= symbol__annotation(sym
);
795 pthread_mutex_lock(¬es
->lock
);
796 if (notes
->src
!= NULL
) {
797 memset(notes
->src
->histograms
, 0,
798 notes
->src
->nr_histograms
* notes
->src
->sizeof_sym_hist
);
799 if (notes
->src
->cycles_hist
)
800 memset(notes
->src
->cycles_hist
, 0,
801 symbol__size(sym
) * sizeof(struct cyc_hist
));
803 pthread_mutex_unlock(¬es
->lock
);
806 static int __symbol__account_cycles(struct cyc_hist
*ch
,
808 unsigned offset
, unsigned cycles
,
812 * For now we can only account one basic block per
813 * final jump. But multiple could be overlapping.
814 * Always account the longest one. So when
815 * a shorter one has been already seen throw it away.
817 * We separately always account the full cycles.
819 ch
[offset
].num_aggr
++;
820 ch
[offset
].cycles_aggr
+= cycles
;
822 if (cycles
> ch
[offset
].cycles_max
)
823 ch
[offset
].cycles_max
= cycles
;
825 if (ch
[offset
].cycles_min
) {
826 if (cycles
&& cycles
< ch
[offset
].cycles_min
)
827 ch
[offset
].cycles_min
= cycles
;
829 ch
[offset
].cycles_min
= cycles
;
831 if (!have_start
&& ch
[offset
].have_start
)
833 if (ch
[offset
].num
) {
834 if (have_start
&& (!ch
[offset
].have_start
||
835 ch
[offset
].start
> start
)) {
836 ch
[offset
].have_start
= 0;
837 ch
[offset
].cycles
= 0;
839 if (ch
[offset
].reset
< 0xffff)
841 } else if (have_start
&&
842 ch
[offset
].start
< start
)
845 ch
[offset
].have_start
= have_start
;
846 ch
[offset
].start
= start
;
847 ch
[offset
].cycles
+= cycles
;
852 static int __symbol__inc_addr_samples(struct symbol
*sym
, struct map
*map
,
853 struct annotated_source
*src
, int evidx
, u64 addr
,
854 struct perf_sample
*sample
)
859 pr_debug3("%s: addr=%#" PRIx64
"\n", __func__
, map
->unmap_ip(map
, addr
));
861 if ((addr
< sym
->start
|| addr
>= sym
->end
) &&
862 (addr
!= sym
->end
|| sym
->start
!= sym
->end
)) {
863 pr_debug("%s(%d): ERANGE! sym->name=%s, start=%#" PRIx64
", addr=%#" PRIx64
", end=%#" PRIx64
"\n",
864 __func__
, __LINE__
, sym
->name
, sym
->start
, addr
, sym
->end
);
868 offset
= addr
- sym
->start
;
869 h
= annotated_source__histogram(src
, evidx
);
871 pr_debug("%s(%d): ENOMEM! sym->name=%s, start=%#" PRIx64
", addr=%#" PRIx64
", end=%#" PRIx64
", func: %d\n",
872 __func__
, __LINE__
, sym
->name
, sym
->start
, addr
, sym
->end
, sym
->type
== STT_FUNC
);
876 h
->addr
[offset
].nr_samples
++;
877 h
->period
+= sample
->period
;
878 h
->addr
[offset
].period
+= sample
->period
;
880 pr_debug3("%#" PRIx64
" %s: period++ [addr: %#" PRIx64
", %#" PRIx64
881 ", evidx=%d] => nr_samples: %" PRIu64
", period: %" PRIu64
"\n",
882 sym
->start
, sym
->name
, addr
, addr
- sym
->start
, evidx
,
883 h
->addr
[offset
].nr_samples
, h
->addr
[offset
].period
);
887 static struct cyc_hist
*symbol__cycles_hist(struct symbol
*sym
)
889 struct annotation
*notes
= symbol__annotation(sym
);
891 if (notes
->src
== NULL
) {
892 notes
->src
= annotated_source__new();
893 if (notes
->src
== NULL
)
895 goto alloc_cycles_hist
;
898 if (!notes
->src
->cycles_hist
) {
900 symbol__alloc_hist_cycles(sym
);
903 return notes
->src
->cycles_hist
;
906 struct annotated_source
*symbol__hists(struct symbol
*sym
, int nr_hists
)
908 struct annotation
*notes
= symbol__annotation(sym
);
910 if (notes
->src
== NULL
) {
911 notes
->src
= annotated_source__new();
912 if (notes
->src
== NULL
)
914 goto alloc_histograms
;
917 if (notes
->src
->histograms
== NULL
) {
919 annotated_source__alloc_histograms(notes
->src
, symbol__size(sym
),
926 static int symbol__inc_addr_samples(struct symbol
*sym
, struct map
*map
,
927 struct perf_evsel
*evsel
, u64 addr
,
928 struct perf_sample
*sample
)
930 struct annotated_source
*src
;
934 src
= symbol__hists(sym
, evsel
->evlist
->nr_entries
);
937 return __symbol__inc_addr_samples(sym
, map
, src
, evsel
->idx
, addr
, sample
);
940 static int symbol__account_cycles(u64 addr
, u64 start
,
941 struct symbol
*sym
, unsigned cycles
)
943 struct cyc_hist
*cycles_hist
;
948 cycles_hist
= symbol__cycles_hist(sym
);
949 if (cycles_hist
== NULL
)
951 if (addr
< sym
->start
|| addr
>= sym
->end
)
955 if (start
< sym
->start
|| start
>= sym
->end
)
960 offset
= addr
- sym
->start
;
961 return __symbol__account_cycles(cycles_hist
,
962 start
? start
- sym
->start
: 0,
967 int addr_map_symbol__account_cycles(struct addr_map_symbol
*ams
,
968 struct addr_map_symbol
*start
,
978 * Only set start when IPC can be computed. We can only
979 * compute it when the basic block is completely in a single
981 * Special case the case when the jump is elsewhere, but
982 * it starts on the function start.
985 (start
->sym
== ams
->sym
||
987 start
->addr
== ams
->sym
->start
+ ams
->map
->start
)))
988 saddr
= start
->al_addr
;
990 pr_debug2("BB with bad start: addr %"PRIx64
" start %"PRIx64
" sym %"PRIx64
" saddr %"PRIx64
"\n",
992 start
? start
->addr
: 0,
993 ams
->sym
? ams
->sym
->start
+ ams
->map
->start
: 0,
995 err
= symbol__account_cycles(ams
->al_addr
, saddr
, ams
->sym
, cycles
);
997 pr_debug2("account_cycles failed %d\n", err
);
1001 static unsigned annotation__count_insn(struct annotation
*notes
, u64 start
, u64 end
)
1003 unsigned n_insn
= 0;
1006 for (offset
= start
; offset
<= end
; offset
++) {
1007 if (notes
->offsets
[offset
])
1013 static void annotation__count_and_fill(struct annotation
*notes
, u64 start
, u64 end
, struct cyc_hist
*ch
)
1016 unsigned int cover_insn
= 0;
1019 n_insn
= annotation__count_insn(notes
, start
, end
);
1020 if (n_insn
&& ch
->num
&& ch
->cycles
) {
1021 float ipc
= n_insn
/ ((double)ch
->cycles
/ (double)ch
->num
);
1023 /* Hide data when there are too many overlaps. */
1024 if (ch
->reset
>= 0x7fff)
1027 for (offset
= start
; offset
<= end
; offset
++) {
1028 struct annotation_line
*al
= notes
->offsets
[offset
];
1030 if (al
&& al
->ipc
== 0.0) {
1037 notes
->hit_cycles
+= ch
->cycles
;
1038 notes
->hit_insn
+= n_insn
* ch
->num
;
1039 notes
->cover_insn
+= cover_insn
;
1044 void annotation__compute_ipc(struct annotation
*notes
, size_t size
)
1048 if (!notes
->src
|| !notes
->src
->cycles_hist
)
1051 notes
->total_insn
= annotation__count_insn(notes
, 0, size
- 1);
1052 notes
->hit_cycles
= 0;
1053 notes
->hit_insn
= 0;
1054 notes
->cover_insn
= 0;
1056 pthread_mutex_lock(¬es
->lock
);
1057 for (offset
= size
- 1; offset
>= 0; --offset
) {
1058 struct cyc_hist
*ch
;
1060 ch
= ¬es
->src
->cycles_hist
[offset
];
1061 if (ch
&& ch
->cycles
) {
1062 struct annotation_line
*al
;
1065 annotation__count_and_fill(notes
, ch
->start
, offset
, ch
);
1066 al
= notes
->offsets
[offset
];
1067 if (al
&& ch
->num_aggr
) {
1068 al
->cycles
= ch
->cycles_aggr
/ ch
->num_aggr
;
1069 al
->cycles_max
= ch
->cycles_max
;
1070 al
->cycles_min
= ch
->cycles_min
;
1072 notes
->have_cycles
= true;
1075 pthread_mutex_unlock(¬es
->lock
);
1078 int addr_map_symbol__inc_samples(struct addr_map_symbol
*ams
, struct perf_sample
*sample
,
1079 struct perf_evsel
*evsel
)
1081 return symbol__inc_addr_samples(ams
->sym
, ams
->map
, evsel
, ams
->al_addr
, sample
);
1084 int hist_entry__inc_addr_samples(struct hist_entry
*he
, struct perf_sample
*sample
,
1085 struct perf_evsel
*evsel
, u64 ip
)
1087 return symbol__inc_addr_samples(he
->ms
.sym
, he
->ms
.map
, evsel
, ip
, sample
);
1090 static void disasm_line__init_ins(struct disasm_line
*dl
, struct arch
*arch
, struct map_symbol
*ms
)
1092 dl
->ins
.ops
= ins__find(arch
, dl
->ins
.name
);
1097 if (dl
->ins
.ops
->parse
&& dl
->ins
.ops
->parse(arch
, &dl
->ops
, ms
) < 0)
1101 static int disasm_line__parse(char *line
, const char **namep
, char **rawp
)
1103 char tmp
, *name
= ltrim(line
);
1105 if (name
[0] == '\0')
1110 while ((*rawp
)[0] != '\0' && !isspace((*rawp
)[0]))
1115 *namep
= strdup(name
);
1121 *rawp
= ltrim(*rawp
);
1126 free((void *)namep
);
1131 struct annotate_args
{
1134 struct map_symbol ms
;
1135 struct perf_evsel
*evsel
;
1136 struct annotation_options
*options
;
1142 static void annotation_line__delete(struct annotation_line
*al
)
1144 void *ptr
= (void *) al
- al
->privsize
;
1146 free_srcline(al
->path
);
1152 * Allocating the annotation line data with following
1155 * --------------------------------------
1156 * private space | struct annotation_line
1157 * --------------------------------------
1159 * Size of the private space is stored in 'struct annotation_line'.
1162 static struct annotation_line
*
1163 annotation_line__new(struct annotate_args
*args
, size_t privsize
)
1165 struct annotation_line
*al
;
1166 struct perf_evsel
*evsel
= args
->evsel
;
1167 size_t size
= privsize
+ sizeof(*al
);
1170 if (perf_evsel__is_group_event(evsel
))
1171 nr
= evsel
->nr_members
;
1173 size
+= sizeof(al
->data
[0]) * nr
;
1177 al
= (void *) al
+ privsize
;
1178 al
->privsize
= privsize
;
1179 al
->offset
= args
->offset
;
1180 al
->line
= strdup(args
->line
);
1181 al
->line_nr
= args
->line_nr
;
1189 * Allocating the disasm annotation line data with
1190 * following structure:
1192 * ------------------------------------------------------------
1193 * privsize space | struct disasm_line | struct annotation_line
1194 * ------------------------------------------------------------
1196 * We have 'struct annotation_line' member as last member
1197 * of 'struct disasm_line' to have an easy access.
1200 static struct disasm_line
*disasm_line__new(struct annotate_args
*args
)
1202 struct disasm_line
*dl
= NULL
;
1203 struct annotation_line
*al
;
1204 size_t privsize
= args
->privsize
+ offsetof(struct disasm_line
, al
);
1206 al
= annotation_line__new(args
, privsize
);
1208 dl
= disasm_line(al
);
1210 if (dl
->al
.line
== NULL
)
1213 if (args
->offset
!= -1) {
1214 if (disasm_line__parse(dl
->al
.line
, &dl
->ins
.name
, &dl
->ops
.raw
) < 0)
1217 disasm_line__init_ins(dl
, args
->arch
, &args
->ms
);
1224 zfree(&dl
->al
.line
);
1230 void disasm_line__free(struct disasm_line
*dl
)
1232 if (dl
->ins
.ops
&& dl
->ins
.ops
->free
)
1233 dl
->ins
.ops
->free(&dl
->ops
);
1235 ins__delete(&dl
->ops
);
1236 free((void *)dl
->ins
.name
);
1237 dl
->ins
.name
= NULL
;
1238 annotation_line__delete(&dl
->al
);
1241 int disasm_line__scnprintf(struct disasm_line
*dl
, char *bf
, size_t size
, bool raw
, int max_ins_name
)
1243 if (raw
|| !dl
->ins
.ops
)
1244 return scnprintf(bf
, size
, "%-*s %s", max_ins_name
, dl
->ins
.name
, dl
->ops
.raw
);
1246 return ins__scnprintf(&dl
->ins
, bf
, size
, &dl
->ops
, max_ins_name
);
1249 static void annotation_line__add(struct annotation_line
*al
, struct list_head
*head
)
1251 list_add_tail(&al
->node
, head
);
1254 struct annotation_line
*
1255 annotation_line__next(struct annotation_line
*pos
, struct list_head
*head
)
1257 list_for_each_entry_continue(pos
, head
, node
)
1258 if (pos
->offset
>= 0)
1264 static const char *annotate__address_color(struct block_range
*br
)
1266 double cov
= block_range__coverage(br
);
1269 /* mark red for >75% coverage */
1271 return PERF_COLOR_RED
;
1273 /* mark dull for <1% coverage */
1275 return PERF_COLOR_NORMAL
;
1278 return PERF_COLOR_MAGENTA
;
1281 static const char *annotate__asm_color(struct block_range
*br
)
1283 double cov
= block_range__coverage(br
);
1286 /* mark dull for <1% coverage */
1288 return PERF_COLOR_NORMAL
;
1291 return PERF_COLOR_BLUE
;
1294 static void annotate__branch_printf(struct block_range
*br
, u64 addr
)
1296 bool emit_comment
= true;
1302 if (br
->is_target
&& br
->start
== addr
) {
1303 struct block_range
*branch
= br
;
1307 * Find matching branch to our target.
1309 while (!branch
->is_branch
)
1310 branch
= block_range__next(branch
);
1312 p
= 100 *(double)br
->entry
/ branch
->coverage
;
1316 emit_comment
= false;
1321 * The percentage of coverage joined at this target in relation
1322 * to the next branch.
1324 printf(" +%.2f%%", p
);
1328 if (br
->is_branch
&& br
->end
== addr
) {
1329 double p
= 100*(double)br
->taken
/ br
->coverage
;
1333 emit_comment
= false;
1338 * The percentage of coverage leaving at this branch, and
1339 * its prediction ratio.
1341 printf(" -%.2f%% (p:%.2f%%)", p
, 100*(double)br
->pred
/ br
->taken
);
1346 static int disasm_line__print(struct disasm_line
*dl
, u64 start
, int addr_fmt_width
)
1348 s64 offset
= dl
->al
.offset
;
1349 const u64 addr
= start
+ offset
;
1350 struct block_range
*br
;
1352 br
= block_range__find(addr
);
1353 color_fprintf(stdout
, annotate__address_color(br
), " %*" PRIx64
":", addr_fmt_width
, addr
);
1354 color_fprintf(stdout
, annotate__asm_color(br
), "%s", dl
->al
.line
);
1355 annotate__branch_printf(br
, addr
);
1360 annotation_line__print(struct annotation_line
*al
, struct symbol
*sym
, u64 start
,
1361 struct perf_evsel
*evsel
, u64 len
, int min_pcnt
, int printed
,
1362 int max_lines
, struct annotation_line
*queue
, int addr_fmt_width
,
1365 struct disasm_line
*dl
= container_of(al
, struct disasm_line
, al
);
1366 static const char *prev_line
;
1367 static const char *prev_color
;
1369 if (al
->offset
!= -1) {
1370 double max_percent
= 0.0;
1371 int i
, nr_percent
= 1;
1373 struct annotation
*notes
= symbol__annotation(sym
);
1375 for (i
= 0; i
< al
->data_nr
; i
++) {
1378 percent
= annotation_data__percent(&al
->data
[i
],
1381 if (percent
> max_percent
)
1382 max_percent
= percent
;
1385 if (al
->data_nr
> nr_percent
)
1386 nr_percent
= al
->data_nr
;
1388 if (max_percent
< min_pcnt
)
1391 if (max_lines
&& printed
>= max_lines
)
1394 if (queue
!= NULL
) {
1395 list_for_each_entry_from(queue
, ¬es
->src
->source
, node
) {
1398 annotation_line__print(queue
, sym
, start
, evsel
, len
,
1399 0, 0, 1, NULL
, addr_fmt_width
,
1404 color
= get_percent_color(max_percent
);
1407 * Also color the filename and line if needed, with
1408 * the same color than the percentage. Don't print it
1409 * twice for close colored addr with the same filename:line
1412 if (!prev_line
|| strcmp(prev_line
, al
->path
)
1413 || color
!= prev_color
) {
1414 color_fprintf(stdout
, color
, " %s", al
->path
);
1415 prev_line
= al
->path
;
1420 for (i
= 0; i
< nr_percent
; i
++) {
1421 struct annotation_data
*data
= &al
->data
[i
];
1424 percent
= annotation_data__percent(data
, percent_type
);
1425 color
= get_percent_color(percent
);
1427 if (symbol_conf
.show_total_period
)
1428 color_fprintf(stdout
, color
, " %11" PRIu64
,
1430 else if (symbol_conf
.show_nr_samples
)
1431 color_fprintf(stdout
, color
, " %7" PRIu64
,
1432 data
->he
.nr_samples
);
1434 color_fprintf(stdout
, color
, " %7.2f", percent
);
1439 disasm_line__print(dl
, start
, addr_fmt_width
);
1441 } else if (max_lines
&& printed
>= max_lines
)
1444 int width
= symbol_conf
.show_total_period
? 12 : 8;
1449 if (perf_evsel__is_group_event(evsel
))
1450 width
*= evsel
->nr_members
;
1453 printf(" %*s:\n", width
, " ");
1455 printf(" %*s: %*s %s\n", width
, " ", addr_fmt_width
, " ", al
->line
);
1462 * symbol__parse_objdump_line() parses objdump output (with -d --no-show-raw)
1463 * which looks like following
1465 * 0000000000415500 <_init>:
1466 * 415500: sub $0x8,%rsp
1467 * 415504: mov 0x2f5ad5(%rip),%rax # 70afe0 <_DYNAMIC+0x2f8>
1468 * 41550b: test %rax,%rax
1469 * 41550e: je 415515 <_init+0x15>
1470 * 415510: callq 416e70 <__gmon_start__@plt>
1471 * 415515: add $0x8,%rsp
1474 * it will be parsed and saved into struct disasm_line as
1475 * <offset> <name> <ops.raw>
1477 * The offset will be a relative offset from the start of the symbol and -1
1478 * means that it's not a disassembly line so should be treated differently.
1479 * The ops.raw part will be parsed further according to type of the instruction.
1481 static int symbol__parse_objdump_line(struct symbol
*sym
, FILE *file
,
1482 struct annotate_args
*args
,
1485 struct map
*map
= args
->ms
.map
;
1486 struct annotation
*notes
= symbol__annotation(sym
);
1487 struct disasm_line
*dl
;
1488 char *line
= NULL
, *parsed_line
, *tmp
, *tmp2
;
1490 s64 line_ip
, offset
= -1;
1491 regmatch_t match
[2];
1493 if (getline(&line
, &line_len
, file
) < 0)
1500 parsed_line
= rtrim(line
);
1502 /* /filename:linenr ? Save line number and ignore. */
1503 if (regexec(&file_lineno
, parsed_line
, 2, match
, 0) == 0) {
1504 *line_nr
= atoi(parsed_line
+ match
[1].rm_so
);
1508 tmp
= ltrim(parsed_line
);
1511 * Parse hexa addresses followed by ':'
1513 line_ip
= strtoull(tmp
, &tmp2
, 16);
1514 if (*tmp2
!= ':' || tmp
== tmp2
|| tmp2
[1] == '\0')
1518 if (line_ip
!= -1) {
1519 u64 start
= map__rip_2objdump(map
, sym
->start
),
1520 end
= map__rip_2objdump(map
, sym
->end
);
1522 offset
= line_ip
- start
;
1523 if ((u64
)line_ip
< start
|| (u64
)line_ip
>= end
)
1526 parsed_line
= tmp2
+ 1;
1529 args
->offset
= offset
;
1530 args
->line
= parsed_line
;
1531 args
->line_nr
= *line_nr
;
1534 dl
= disasm_line__new(args
);
1541 if (!disasm_line__has_local_offset(dl
)) {
1542 dl
->ops
.target
.offset
= dl
->ops
.target
.addr
-
1543 map__rip_2objdump(map
, sym
->start
);
1544 dl
->ops
.target
.offset_avail
= true;
1547 /* kcore has no symbols, so add the call target symbol */
1548 if (dl
->ins
.ops
&& ins__is_call(&dl
->ins
) && !dl
->ops
.target
.sym
) {
1549 struct addr_map_symbol target
= {
1551 .addr
= dl
->ops
.target
.addr
,
1554 if (!map_groups__find_ams(&target
) &&
1555 target
.sym
->start
== target
.al_addr
)
1556 dl
->ops
.target
.sym
= target
.sym
;
1559 annotation_line__add(&dl
->al
, ¬es
->src
->source
);
1564 static __attribute__((constructor
)) void symbol__init_regexpr(void)
1566 regcomp(&file_lineno
, "^/[^:]+:([0-9]+)", REG_EXTENDED
);
1569 static void delete_last_nop(struct symbol
*sym
)
1571 struct annotation
*notes
= symbol__annotation(sym
);
1572 struct list_head
*list
= ¬es
->src
->source
;
1573 struct disasm_line
*dl
;
1575 while (!list_empty(list
)) {
1576 dl
= list_entry(list
->prev
, struct disasm_line
, al
.node
);
1579 if (dl
->ins
.ops
!= &nop_ops
)
1582 if (!strstr(dl
->al
.line
, " nop ") &&
1583 !strstr(dl
->al
.line
, " nopl ") &&
1584 !strstr(dl
->al
.line
, " nopw "))
1588 list_del(&dl
->al
.node
);
1589 disasm_line__free(dl
);
1593 int symbol__strerror_disassemble(struct symbol
*sym __maybe_unused
, struct map
*map
,
1594 int errnum
, char *buf
, size_t buflen
)
1596 struct dso
*dso
= map
->dso
;
1598 BUG_ON(buflen
== 0);
1601 str_error_r(errnum
, buf
, buflen
);
1606 case SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX
: {
1607 char bf
[SBUILD_ID_SIZE
+ 15] = " with build id ";
1608 char *build_id_msg
= NULL
;
1610 if (dso
->has_build_id
) {
1611 build_id__sprintf(dso
->build_id
,
1612 sizeof(dso
->build_id
), bf
+ 15);
1615 scnprintf(buf
, buflen
,
1616 "No vmlinux file%s\nwas found in the path.\n\n"
1617 "Note that annotation using /proc/kcore requires CAP_SYS_RAWIO capability.\n\n"
1619 " perf buildid-cache -vu vmlinux\n\n"
1621 " --vmlinux vmlinux\n", build_id_msg
?: "");
1624 case SYMBOL_ANNOTATE_ERRNO__NO_LIBOPCODES_FOR_BPF
:
1625 scnprintf(buf
, buflen
, "Please link with binutils's libopcode to enable BPF annotation");
1628 scnprintf(buf
, buflen
, "Internal error: Invalid %d error code\n", errnum
);
1635 static int dso__disassemble_filename(struct dso
*dso
, char *filename
, size_t filename_size
)
1637 char linkname
[PATH_MAX
];
1638 char *build_id_filename
;
1639 char *build_id_path
= NULL
;
1642 if (dso
->symtab_type
== DSO_BINARY_TYPE__KALLSYMS
&&
1643 !dso__is_kcore(dso
))
1644 return SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX
;
1646 build_id_filename
= dso__build_id_filename(dso
, NULL
, 0, false);
1647 if (build_id_filename
) {
1648 __symbol__join_symfs(filename
, filename_size
, build_id_filename
);
1649 free(build_id_filename
);
1651 if (dso
->has_build_id
)
1656 build_id_path
= strdup(filename
);
1661 * old style build-id cache has name of XX/XXXXXXX.. while
1662 * new style has XX/XXXXXXX../{elf,kallsyms,vdso}.
1663 * extract the build-id part of dirname in the new style only.
1665 pos
= strrchr(build_id_path
, '/');
1666 if (pos
&& strlen(pos
) < SBUILD_ID_SIZE
- 2)
1667 dirname(build_id_path
);
1669 if (dso__is_kcore(dso
) ||
1670 readlink(build_id_path
, linkname
, sizeof(linkname
)) < 0 ||
1671 strstr(linkname
, DSO__NAME_KALLSYMS
) ||
1672 access(filename
, R_OK
)) {
1675 * If we don't have build-ids or the build-id file isn't in the
1676 * cache, or is just a kallsyms file, well, lets hope that this
1677 * DSO is the same as when 'perf record' ran.
1679 __symbol__join_symfs(filename
, filename_size
, dso
->long_name
);
1682 free(build_id_path
);
1686 #if defined(HAVE_LIBBFD_SUPPORT) && defined(HAVE_LIBBPF_SUPPORT)
1687 #define PACKAGE "perf"
1689 #include <dis-asm.h>
1691 static int symbol__disassemble_bpf(struct symbol
*sym
,
1692 struct annotate_args
*args
)
1694 struct annotation
*notes
= symbol__annotation(sym
);
1695 struct annotation_options
*opts
= args
->options
;
1696 struct bpf_prog_info_linear
*info_linear
;
1697 struct bpf_prog_linfo
*prog_linfo
= NULL
;
1698 struct bpf_prog_info_node
*info_node
;
1699 int len
= sym
->end
- sym
->start
;
1700 disassembler_ftype disassemble
;
1701 struct map
*map
= args
->ms
.map
;
1702 struct disassemble_info info
;
1703 struct dso
*dso
= map
->dso
;
1704 int pc
= 0, count
, sub_id
;
1705 struct btf
*btf
= NULL
;
1706 char tpath
[PATH_MAX
];
1714 if (dso
->binary_type
!= DSO_BINARY_TYPE__BPF_PROG_INFO
)
1717 pr_debug("%s: handling sym %s addr %" PRIx64
" len %" PRIx64
"\n", __func__
,
1718 sym
->name
, sym
->start
, sym
->end
- sym
->start
);
1720 memset(tpath
, 0, sizeof(tpath
));
1721 perf_exe(tpath
, sizeof(tpath
));
1723 bfdf
= bfd_openr(tpath
, NULL
);
1725 assert(bfd_check_format(bfdf
, bfd_object
));
1727 s
= open_memstream(&buf
, &buf_size
);
1730 init_disassemble_info(&info
, s
,
1731 (fprintf_ftype
) fprintf
);
1733 info
.arch
= bfd_get_arch(bfdf
);
1734 info
.mach
= bfd_get_mach(bfdf
);
1736 info_node
= perf_env__find_bpf_prog_info(dso
->bpf_prog
.env
,
1740 info_linear
= info_node
->info_linear
;
1741 sub_id
= dso
->bpf_prog
.sub_id
;
1743 info
.buffer
= (void *)(uintptr_t)(info_linear
->info
.jited_prog_insns
);
1744 info
.buffer_length
= info_linear
->info
.jited_prog_len
;
1746 if (info_linear
->info
.nr_line_info
)
1747 prog_linfo
= bpf_prog_linfo__new(&info_linear
->info
);
1749 if (info_linear
->info
.btf_id
) {
1750 struct btf_node
*node
;
1752 node
= perf_env__find_btf(dso
->bpf_prog
.env
,
1753 info_linear
->info
.btf_id
);
1755 btf
= btf__new((__u8
*)(node
->data
),
1759 disassemble_init_for_target(&info
);
1761 #ifdef DISASM_FOUR_ARGS_SIGNATURE
1762 disassemble
= disassembler(info
.arch
,
1763 bfd_big_endian(bfdf
),
1767 disassemble
= disassembler(bfdf
);
1769 assert(disassemble
);
1773 const struct bpf_line_info
*linfo
= NULL
;
1774 struct disasm_line
*dl
;
1775 size_t prev_buf_size
;
1776 const char *srcline
;
1779 addr
= pc
+ ((u64
*)(uintptr_t)(info_linear
->info
.jited_ksyms
))[sub_id
];
1780 count
= disassemble(pc
, &info
);
1783 linfo
= bpf_prog_linfo__lfind_addr_func(prog_linfo
,
1788 srcline
= btf__name_by_offset(btf
, linfo
->line_off
);
1794 prev_buf_size
= buf_size
;
1797 if (!opts
->hide_src_code
&& srcline
) {
1799 args
->line
= strdup(srcline
);
1802 dl
= disasm_line__new(args
);
1804 annotation_line__add(&dl
->al
,
1805 ¬es
->src
->source
);
1810 args
->line
= buf
+ prev_buf_size
;
1813 dl
= disasm_line__new(args
);
1815 annotation_line__add(&dl
->al
, ¬es
->src
->source
);
1818 } while (count
> 0 && pc
< len
);
1828 #else // defined(HAVE_LIBBFD_SUPPORT) && defined(HAVE_LIBBPF_SUPPORT)
1829 static int symbol__disassemble_bpf(struct symbol
*sym __maybe_unused
,
1830 struct annotate_args
*args __maybe_unused
)
1832 return SYMBOL_ANNOTATE_ERRNO__NO_LIBOPCODES_FOR_BPF
;
1834 #endif // defined(HAVE_LIBBFD_SUPPORT) && defined(HAVE_LIBBPF_SUPPORT)
1836 static int symbol__disassemble(struct symbol
*sym
, struct annotate_args
*args
)
1838 struct annotation_options
*opts
= args
->options
;
1839 struct map
*map
= args
->ms
.map
;
1840 struct dso
*dso
= map
->dso
;
1843 char symfs_filename
[PATH_MAX
];
1844 struct kcore_extract kce
;
1845 bool delete_extract
= false;
1846 bool decomp
= false;
1851 int err
= dso__disassemble_filename(dso
, symfs_filename
, sizeof(symfs_filename
));
1856 pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64
", end=%#" PRIx64
"\n", __func__
,
1857 symfs_filename
, sym
->name
, map
->unmap_ip(map
, sym
->start
),
1858 map
->unmap_ip(map
, sym
->end
));
1860 pr_debug("annotating [%p] %30s : [%p] %30s\n",
1861 dso
, dso
->long_name
, sym
, sym
->name
);
1863 if (dso
->binary_type
== DSO_BINARY_TYPE__BPF_PROG_INFO
) {
1864 return symbol__disassemble_bpf(sym
, args
);
1865 } else if (dso__is_kcore(dso
)) {
1866 kce
.kcore_filename
= symfs_filename
;
1867 kce
.addr
= map__rip_2objdump(map
, sym
->start
);
1868 kce
.offs
= sym
->start
;
1869 kce
.len
= sym
->end
- sym
->start
;
1870 if (!kcore_extract__create(&kce
)) {
1871 delete_extract
= true;
1872 strlcpy(symfs_filename
, kce
.extract_filename
,
1873 sizeof(symfs_filename
));
1875 } else if (dso__needs_decompress(dso
)) {
1876 char tmp
[KMOD_DECOMP_LEN
];
1878 if (dso__decompress_kmodule_path(dso
, symfs_filename
,
1879 tmp
, sizeof(tmp
)) < 0)
1883 strcpy(symfs_filename
, tmp
);
1886 err
= asprintf(&command
,
1887 "%s %s%s --start-address=0x%016" PRIx64
1888 " --stop-address=0x%016" PRIx64
1889 " -l -d %s %s -C \"$1\" 2>/dev/null|grep -v \"$1:\"|expand",
1890 opts
->objdump_path
?: "objdump",
1891 opts
->disassembler_style
? "-M " : "",
1892 opts
->disassembler_style
?: "",
1893 map__rip_2objdump(map
, sym
->start
),
1894 map__rip_2objdump(map
, sym
->end
),
1895 opts
->show_asm_raw
? "" : "--no-show-raw",
1896 opts
->annotate_src
? "-S" : "");
1899 pr_err("Failure allocating memory for the command to run\n");
1900 goto out_remove_tmp
;
1903 pr_debug("Executing: %s\n", command
);
1906 if (pipe(stdout_fd
) < 0) {
1907 pr_err("Failure creating the pipe to run %s\n", command
);
1908 goto out_free_command
;
1913 pr_err("Failure forking to run %s\n", command
);
1914 goto out_close_stdout
;
1918 close(stdout_fd
[0]);
1919 dup2(stdout_fd
[1], 1);
1920 close(stdout_fd
[1]);
1921 execl("/bin/sh", "sh", "-c", command
, "--", symfs_filename
,
1927 close(stdout_fd
[1]);
1929 file
= fdopen(stdout_fd
[0], "r");
1931 pr_err("Failure creating FILE stream for %s\n", command
);
1933 * If we were using debug info should retry with
1936 goto out_free_command
;
1940 while (!feof(file
)) {
1942 * The source code line number (lineno) needs to be kept in
1943 * across calls to symbol__parse_objdump_line(), so that it
1944 * can associate it with the instructions till the next one.
1945 * See disasm_line__new() and struct disasm_line::line_nr.
1947 if (symbol__parse_objdump_line(sym
, file
, args
, &lineno
) < 0)
1953 pr_err("No output from %s\n", command
);
1956 * kallsyms does not have symbol sizes so there may a nop at the end.
1959 if (dso__is_kcore(dso
))
1960 delete_last_nop(sym
);
1967 close(stdout_fd
[0]);
1970 unlink(symfs_filename
);
1973 kcore_extract__delete(&kce
);
1978 close(stdout_fd
[1]);
1979 goto out_free_command
;
1982 static void calc_percent(struct sym_hist
*sym_hist
,
1983 struct hists
*hists
,
1984 struct annotation_data
*data
,
1985 s64 offset
, s64 end
)
1987 unsigned int hits
= 0;
1990 while (offset
< end
) {
1991 hits
+= sym_hist
->addr
[offset
].nr_samples
;
1992 period
+= sym_hist
->addr
[offset
].period
;
1996 if (sym_hist
->nr_samples
) {
1997 data
->he
.period
= period
;
1998 data
->he
.nr_samples
= hits
;
1999 data
->percent
[PERCENT_HITS_LOCAL
] = 100.0 * hits
/ sym_hist
->nr_samples
;
2002 if (hists
->stats
.nr_non_filtered_samples
)
2003 data
->percent
[PERCENT_HITS_GLOBAL
] = 100.0 * hits
/ hists
->stats
.nr_non_filtered_samples
;
2005 if (sym_hist
->period
)
2006 data
->percent
[PERCENT_PERIOD_LOCAL
] = 100.0 * period
/ sym_hist
->period
;
2008 if (hists
->stats
.total_period
)
2009 data
->percent
[PERCENT_PERIOD_GLOBAL
] = 100.0 * period
/ hists
->stats
.total_period
;
2012 static void annotation__calc_percent(struct annotation
*notes
,
2013 struct perf_evsel
*leader
, s64 len
)
2015 struct annotation_line
*al
, *next
;
2016 struct perf_evsel
*evsel
;
2018 list_for_each_entry(al
, ¬es
->src
->source
, node
) {
2022 if (al
->offset
== -1)
2025 next
= annotation_line__next(al
, ¬es
->src
->source
);
2026 end
= next
? next
->offset
: len
;
2028 for_each_group_evsel(evsel
, leader
) {
2029 struct hists
*hists
= evsel__hists(evsel
);
2030 struct annotation_data
*data
;
2031 struct sym_hist
*sym_hist
;
2033 BUG_ON(i
>= al
->data_nr
);
2035 sym_hist
= annotation__histogram(notes
, evsel
->idx
);
2036 data
= &al
->data
[i
++];
2038 calc_percent(sym_hist
, hists
, data
, al
->offset
, end
);
2043 void symbol__calc_percent(struct symbol
*sym
, struct perf_evsel
*evsel
)
2045 struct annotation
*notes
= symbol__annotation(sym
);
2047 annotation__calc_percent(notes
, evsel
, symbol__size(sym
));
2050 int symbol__annotate(struct symbol
*sym
, struct map
*map
,
2051 struct perf_evsel
*evsel
, size_t privsize
,
2052 struct annotation_options
*options
,
2053 struct arch
**parch
)
2055 struct annotation
*notes
= symbol__annotation(sym
);
2056 struct annotate_args args
= {
2057 .privsize
= privsize
,
2061 struct perf_env
*env
= perf_evsel__env(evsel
);
2062 const char *arch_name
= perf_env__arch(env
);
2069 args
.arch
= arch
= arch__find(arch_name
);
2077 err
= arch
->init(arch
, env
? env
->cpuid
: NULL
);
2079 pr_err("%s: failed to initialize %s arch priv area\n", __func__
, arch
->name
);
2086 notes
->start
= map__rip_2objdump(map
, sym
->start
);
2088 return symbol__disassemble(sym
, &args
);
2091 static void insert_source_line(struct rb_root
*root
, struct annotation_line
*al
,
2092 struct annotation_options
*opts
)
2094 struct annotation_line
*iter
;
2095 struct rb_node
**p
= &root
->rb_node
;
2096 struct rb_node
*parent
= NULL
;
2099 while (*p
!= NULL
) {
2101 iter
= rb_entry(parent
, struct annotation_line
, rb_node
);
2103 ret
= strcmp(iter
->path
, al
->path
);
2105 for (i
= 0; i
< al
->data_nr
; i
++) {
2106 iter
->data
[i
].percent_sum
+= annotation_data__percent(&al
->data
[i
],
2107 opts
->percent_type
);
2115 p
= &(*p
)->rb_right
;
2118 for (i
= 0; i
< al
->data_nr
; i
++) {
2119 al
->data
[i
].percent_sum
= annotation_data__percent(&al
->data
[i
],
2120 opts
->percent_type
);
2123 rb_link_node(&al
->rb_node
, parent
, p
);
2124 rb_insert_color(&al
->rb_node
, root
);
2127 static int cmp_source_line(struct annotation_line
*a
, struct annotation_line
*b
)
2131 for (i
= 0; i
< a
->data_nr
; i
++) {
2132 if (a
->data
[i
].percent_sum
== b
->data
[i
].percent_sum
)
2134 return a
->data
[i
].percent_sum
> b
->data
[i
].percent_sum
;
2140 static void __resort_source_line(struct rb_root
*root
, struct annotation_line
*al
)
2142 struct annotation_line
*iter
;
2143 struct rb_node
**p
= &root
->rb_node
;
2144 struct rb_node
*parent
= NULL
;
2146 while (*p
!= NULL
) {
2148 iter
= rb_entry(parent
, struct annotation_line
, rb_node
);
2150 if (cmp_source_line(al
, iter
))
2153 p
= &(*p
)->rb_right
;
2156 rb_link_node(&al
->rb_node
, parent
, p
);
2157 rb_insert_color(&al
->rb_node
, root
);
2160 static void resort_source_line(struct rb_root
*dest_root
, struct rb_root
*src_root
)
2162 struct annotation_line
*al
;
2163 struct rb_node
*node
;
2165 node
= rb_first(src_root
);
2167 struct rb_node
*next
;
2169 al
= rb_entry(node
, struct annotation_line
, rb_node
);
2170 next
= rb_next(node
);
2171 rb_erase(node
, src_root
);
2173 __resort_source_line(dest_root
, al
);
2178 static void print_summary(struct rb_root
*root
, const char *filename
)
2180 struct annotation_line
*al
;
2181 struct rb_node
*node
;
2183 printf("\nSorted summary for file %s\n", filename
);
2184 printf("----------------------------------------------\n\n");
2186 if (RB_EMPTY_ROOT(root
)) {
2187 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN
);
2191 node
= rb_first(root
);
2193 double percent
, percent_max
= 0.0;
2198 al
= rb_entry(node
, struct annotation_line
, rb_node
);
2199 for (i
= 0; i
< al
->data_nr
; i
++) {
2200 percent
= al
->data
[i
].percent_sum
;
2201 color
= get_percent_color(percent
);
2202 color_fprintf(stdout
, color
, " %7.2f", percent
);
2204 if (percent
> percent_max
)
2205 percent_max
= percent
;
2209 color
= get_percent_color(percent_max
);
2210 color_fprintf(stdout
, color
, " %s\n", path
);
2212 node
= rb_next(node
);
2216 static void symbol__annotate_hits(struct symbol
*sym
, struct perf_evsel
*evsel
)
2218 struct annotation
*notes
= symbol__annotation(sym
);
2219 struct sym_hist
*h
= annotation__histogram(notes
, evsel
->idx
);
2220 u64 len
= symbol__size(sym
), offset
;
2222 for (offset
= 0; offset
< len
; ++offset
)
2223 if (h
->addr
[offset
].nr_samples
!= 0)
2224 printf("%*" PRIx64
": %" PRIu64
"\n", BITS_PER_LONG
/ 2,
2225 sym
->start
+ offset
, h
->addr
[offset
].nr_samples
);
2226 printf("%*s: %" PRIu64
"\n", BITS_PER_LONG
/ 2, "h->nr_samples", h
->nr_samples
);
2229 static int annotated_source__addr_fmt_width(struct list_head
*lines
, u64 start
)
2232 struct annotation_line
*line
;
2234 list_for_each_entry_reverse(line
, lines
, node
) {
2235 if (line
->offset
!= -1)
2236 return scnprintf(bf
, sizeof(bf
), "%" PRIx64
, start
+ line
->offset
);
2242 int symbol__annotate_printf(struct symbol
*sym
, struct map
*map
,
2243 struct perf_evsel
*evsel
,
2244 struct annotation_options
*opts
)
2246 struct dso
*dso
= map
->dso
;
2248 const char *d_filename
;
2249 const char *evsel_name
= perf_evsel__name(evsel
);
2250 struct annotation
*notes
= symbol__annotation(sym
);
2251 struct sym_hist
*h
= annotation__histogram(notes
, evsel
->idx
);
2252 struct annotation_line
*pos
, *queue
= NULL
;
2253 u64 start
= map__rip_2objdump(map
, sym
->start
);
2254 int printed
= 2, queue_len
= 0, addr_fmt_width
;
2256 bool context
= opts
->context
;
2258 int width
= symbol_conf
.show_total_period
? 12 : 8;
2259 int graph_dotted_len
;
2262 filename
= strdup(dso
->long_name
);
2266 if (opts
->full_path
)
2267 d_filename
= filename
;
2269 d_filename
= basename(filename
);
2271 len
= symbol__size(sym
);
2273 if (perf_evsel__is_group_event(evsel
)) {
2274 width
*= evsel
->nr_members
;
2275 perf_evsel__group_desc(evsel
, buf
, sizeof(buf
));
2279 graph_dotted_len
= printf(" %-*.*s| Source code & Disassembly of %s for %s (%" PRIu64
" samples, "
2281 width
, width
, symbol_conf
.show_total_period
? "Period" :
2282 symbol_conf
.show_nr_samples
? "Samples" : "Percent",
2283 d_filename
, evsel_name
, h
->nr_samples
,
2284 percent_type_str(opts
->percent_type
));
2286 printf("%-*.*s----\n",
2287 graph_dotted_len
, graph_dotted_len
, graph_dotted_line
);
2290 symbol__annotate_hits(sym
, evsel
);
2292 addr_fmt_width
= annotated_source__addr_fmt_width(¬es
->src
->source
, start
);
2294 list_for_each_entry(pos
, ¬es
->src
->source
, node
) {
2297 if (context
&& queue
== NULL
) {
2302 err
= annotation_line__print(pos
, sym
, start
, evsel
, len
,
2303 opts
->min_pcnt
, printed
, opts
->max_lines
,
2304 queue
, addr_fmt_width
, opts
->percent_type
);
2310 printed
+= queue_len
;
2316 /* filtered by max_lines */
2322 * Filtered by min_pcnt or non IP lines when
2327 if (queue_len
== context
)
2328 queue
= list_entry(queue
->node
.next
, typeof(*queue
), node
);
2340 static void FILE__set_percent_color(void *fp __maybe_unused
,
2341 double percent __maybe_unused
,
2342 bool current __maybe_unused
)
2346 static int FILE__set_jumps_percent_color(void *fp __maybe_unused
,
2347 int nr __maybe_unused
, bool current __maybe_unused
)
2352 static int FILE__set_color(void *fp __maybe_unused
, int color __maybe_unused
)
2357 static void FILE__printf(void *fp
, const char *fmt
, ...)
2361 va_start(args
, fmt
);
2362 vfprintf(fp
, fmt
, args
);
2366 static void FILE__write_graph(void *fp
, int graph
)
2371 case DARROW_CHAR
: s
= "↓"; break;
2372 case UARROW_CHAR
: s
= "↑"; break;
2373 case LARROW_CHAR
: s
= "←"; break;
2374 case RARROW_CHAR
: s
= "→"; break;
2375 default: s
= "?"; break;
2381 static int symbol__annotate_fprintf2(struct symbol
*sym
, FILE *fp
,
2382 struct annotation_options
*opts
)
2384 struct annotation
*notes
= symbol__annotation(sym
);
2385 struct annotation_write_ops wops
= {
2388 .set_color
= FILE__set_color
,
2389 .set_percent_color
= FILE__set_percent_color
,
2390 .set_jumps_percent_color
= FILE__set_jumps_percent_color
,
2391 .printf
= FILE__printf
,
2392 .write_graph
= FILE__write_graph
,
2394 struct annotation_line
*al
;
2396 list_for_each_entry(al
, ¬es
->src
->source
, node
) {
2397 if (annotation_line__filter(al
, notes
))
2399 annotation_line__write(al
, notes
, &wops
, opts
);
2401 wops
.first_line
= false;
2407 int map_symbol__annotation_dump(struct map_symbol
*ms
, struct perf_evsel
*evsel
,
2408 struct annotation_options
*opts
)
2410 const char *ev_name
= perf_evsel__name(evsel
);
2416 if (asprintf(&filename
, "%s.annotation", ms
->sym
->name
) < 0)
2419 fp
= fopen(filename
, "w");
2421 goto out_free_filename
;
2423 if (perf_evsel__is_group_event(evsel
)) {
2424 perf_evsel__group_desc(evsel
, buf
, sizeof(buf
));
2428 fprintf(fp
, "%s() %s\nEvent: %s\n\n",
2429 ms
->sym
->name
, ms
->map
->dso
->long_name
, ev_name
);
2430 symbol__annotate_fprintf2(ms
->sym
, fp
, opts
);
2439 void symbol__annotate_zero_histogram(struct symbol
*sym
, int evidx
)
2441 struct annotation
*notes
= symbol__annotation(sym
);
2442 struct sym_hist
*h
= annotation__histogram(notes
, evidx
);
2444 memset(h
, 0, notes
->src
->sizeof_sym_hist
);
2447 void symbol__annotate_decay_histogram(struct symbol
*sym
, int evidx
)
2449 struct annotation
*notes
= symbol__annotation(sym
);
2450 struct sym_hist
*h
= annotation__histogram(notes
, evidx
);
2451 int len
= symbol__size(sym
), offset
;
2454 for (offset
= 0; offset
< len
; ++offset
) {
2455 h
->addr
[offset
].nr_samples
= h
->addr
[offset
].nr_samples
* 7 / 8;
2456 h
->nr_samples
+= h
->addr
[offset
].nr_samples
;
2460 void annotated_source__purge(struct annotated_source
*as
)
2462 struct annotation_line
*al
, *n
;
2464 list_for_each_entry_safe(al
, n
, &as
->source
, node
) {
2465 list_del(&al
->node
);
2466 disasm_line__free(disasm_line(al
));
2470 static size_t disasm_line__fprintf(struct disasm_line
*dl
, FILE *fp
)
2474 if (dl
->al
.offset
== -1)
2475 return fprintf(fp
, "%s\n", dl
->al
.line
);
2477 printed
= fprintf(fp
, "%#" PRIx64
" %s", dl
->al
.offset
, dl
->ins
.name
);
2479 if (dl
->ops
.raw
[0] != '\0') {
2480 printed
+= fprintf(fp
, "%.*s %s\n", 6 - (int)printed
, " ",
2484 return printed
+ fprintf(fp
, "\n");
2487 size_t disasm__fprintf(struct list_head
*head
, FILE *fp
)
2489 struct disasm_line
*pos
;
2492 list_for_each_entry(pos
, head
, al
.node
)
2493 printed
+= disasm_line__fprintf(pos
, fp
);
2498 bool disasm_line__is_valid_local_jump(struct disasm_line
*dl
, struct symbol
*sym
)
2500 if (!dl
|| !dl
->ins
.ops
|| !ins__is_jump(&dl
->ins
) ||
2501 !disasm_line__has_local_offset(dl
) || dl
->ops
.target
.offset
< 0 ||
2502 dl
->ops
.target
.offset
>= (s64
)symbol__size(sym
))
2508 void annotation__mark_jump_targets(struct annotation
*notes
, struct symbol
*sym
)
2510 u64 offset
, size
= symbol__size(sym
);
2512 /* PLT symbols contain external offsets */
2513 if (strstr(sym
->name
, "@plt"))
2516 for (offset
= 0; offset
< size
; ++offset
) {
2517 struct annotation_line
*al
= notes
->offsets
[offset
];
2518 struct disasm_line
*dl
;
2520 dl
= disasm_line(al
);
2522 if (!disasm_line__is_valid_local_jump(dl
, sym
))
2525 al
= notes
->offsets
[dl
->ops
.target
.offset
];
2528 * FIXME: Oops, no jump target? Buggy disassembler? Or do we
2529 * have to adjust to the previous offset?
2534 if (++al
->jump_sources
> notes
->max_jump_sources
)
2535 notes
->max_jump_sources
= al
->jump_sources
;
2541 void annotation__set_offsets(struct annotation
*notes
, s64 size
)
2543 struct annotation_line
*al
;
2545 notes
->max_line_len
= 0;
2547 list_for_each_entry(al
, ¬es
->src
->source
, node
) {
2548 size_t line_len
= strlen(al
->line
);
2550 if (notes
->max_line_len
< line_len
)
2551 notes
->max_line_len
= line_len
;
2552 al
->idx
= notes
->nr_entries
++;
2553 if (al
->offset
!= -1) {
2554 al
->idx_asm
= notes
->nr_asm_entries
++;
2556 * FIXME: short term bandaid to cope with assembly
2557 * routines that comes with labels in the same column
2558 * as the address in objdump, sigh.
2560 * E.g. copy_user_generic_unrolled
2562 if (al
->offset
< size
)
2563 notes
->offsets
[al
->offset
] = al
;
2569 static inline int width_jumps(int n
)
2578 static int annotation__max_ins_name(struct annotation
*notes
)
2580 int max_name
= 0, len
;
2581 struct annotation_line
*al
;
2583 list_for_each_entry(al
, ¬es
->src
->source
, node
) {
2584 if (al
->offset
== -1)
2587 len
= strlen(disasm_line(al
)->ins
.name
);
2595 void annotation__init_column_widths(struct annotation
*notes
, struct symbol
*sym
)
2597 notes
->widths
.addr
= notes
->widths
.target
=
2598 notes
->widths
.min_addr
= hex_width(symbol__size(sym
));
2599 notes
->widths
.max_addr
= hex_width(sym
->end
);
2600 notes
->widths
.jumps
= width_jumps(notes
->max_jump_sources
);
2601 notes
->widths
.max_ins_name
= annotation__max_ins_name(notes
);
2604 void annotation__update_column_widths(struct annotation
*notes
)
2606 if (notes
->options
->use_offset
)
2607 notes
->widths
.target
= notes
->widths
.min_addr
;
2609 notes
->widths
.target
= notes
->widths
.max_addr
;
2611 notes
->widths
.addr
= notes
->widths
.target
;
2613 if (notes
->options
->show_nr_jumps
)
2614 notes
->widths
.addr
+= notes
->widths
.jumps
+ 1;
2617 static void annotation__calc_lines(struct annotation
*notes
, struct map
*map
,
2618 struct rb_root
*root
,
2619 struct annotation_options
*opts
)
2621 struct annotation_line
*al
;
2622 struct rb_root tmp_root
= RB_ROOT
;
2624 list_for_each_entry(al
, ¬es
->src
->source
, node
) {
2625 double percent_max
= 0.0;
2628 for (i
= 0; i
< al
->data_nr
; i
++) {
2631 percent
= annotation_data__percent(&al
->data
[i
],
2632 opts
->percent_type
);
2634 if (percent
> percent_max
)
2635 percent_max
= percent
;
2638 if (percent_max
<= 0.5)
2641 al
->path
= get_srcline(map
->dso
, notes
->start
+ al
->offset
, NULL
,
2642 false, true, notes
->start
+ al
->offset
);
2643 insert_source_line(&tmp_root
, al
, opts
);
2646 resort_source_line(root
, &tmp_root
);
2649 static void symbol__calc_lines(struct symbol
*sym
, struct map
*map
,
2650 struct rb_root
*root
,
2651 struct annotation_options
*opts
)
2653 struct annotation
*notes
= symbol__annotation(sym
);
2655 annotation__calc_lines(notes
, map
, root
, opts
);
2658 int symbol__tty_annotate2(struct symbol
*sym
, struct map
*map
,
2659 struct perf_evsel
*evsel
,
2660 struct annotation_options
*opts
)
2662 struct dso
*dso
= map
->dso
;
2663 struct rb_root source_line
= RB_ROOT
;
2664 struct hists
*hists
= evsel__hists(evsel
);
2667 if (symbol__annotate2(sym
, map
, evsel
, opts
, NULL
) < 0)
2670 if (opts
->print_lines
) {
2671 srcline_full_filename
= opts
->full_path
;
2672 symbol__calc_lines(sym
, map
, &source_line
, opts
);
2673 print_summary(&source_line
, dso
->long_name
);
2676 hists__scnprintf_title(hists
, buf
, sizeof(buf
));
2677 fprintf(stdout
, "%s, [percent: %s]\n%s() %s\n",
2678 buf
, percent_type_str(opts
->percent_type
), sym
->name
, dso
->long_name
);
2679 symbol__annotate_fprintf2(sym
, stdout
, opts
);
2681 annotated_source__purge(symbol__annotation(sym
)->src
);
2686 int symbol__tty_annotate(struct symbol
*sym
, struct map
*map
,
2687 struct perf_evsel
*evsel
,
2688 struct annotation_options
*opts
)
2690 struct dso
*dso
= map
->dso
;
2691 struct rb_root source_line
= RB_ROOT
;
2693 if (symbol__annotate(sym
, map
, evsel
, 0, opts
, NULL
) < 0)
2696 symbol__calc_percent(sym
, evsel
);
2698 if (opts
->print_lines
) {
2699 srcline_full_filename
= opts
->full_path
;
2700 symbol__calc_lines(sym
, map
, &source_line
, opts
);
2701 print_summary(&source_line
, dso
->long_name
);
2704 symbol__annotate_printf(sym
, map
, evsel
, opts
);
2706 annotated_source__purge(symbol__annotation(sym
)->src
);
2711 bool ui__has_annotation(void)
2713 return use_browser
== 1 && perf_hpp_list
.sym
;
2717 static double annotation_line__max_percent(struct annotation_line
*al
,
2718 struct annotation
*notes
,
2719 unsigned int percent_type
)
2721 double percent_max
= 0.0;
2724 for (i
= 0; i
< notes
->nr_events
; i
++) {
2727 percent
= annotation_data__percent(&al
->data
[i
],
2730 if (percent
> percent_max
)
2731 percent_max
= percent
;
2737 static void disasm_line__write(struct disasm_line
*dl
, struct annotation
*notes
,
2738 void *obj
, char *bf
, size_t size
,
2739 void (*obj__printf
)(void *obj
, const char *fmt
, ...),
2740 void (*obj__write_graph
)(void *obj
, int graph
))
2742 if (dl
->ins
.ops
&& dl
->ins
.ops
->scnprintf
) {
2743 if (ins__is_jump(&dl
->ins
)) {
2746 if (dl
->ops
.target
.outside
)
2748 fwd
= dl
->ops
.target
.offset
> dl
->al
.offset
;
2749 obj__write_graph(obj
, fwd
? DARROW_CHAR
: UARROW_CHAR
);
2750 obj__printf(obj
, " ");
2751 } else if (ins__is_call(&dl
->ins
)) {
2753 obj__write_graph(obj
, RARROW_CHAR
);
2754 obj__printf(obj
, " ");
2755 } else if (ins__is_ret(&dl
->ins
)) {
2756 obj__write_graph(obj
, LARROW_CHAR
);
2757 obj__printf(obj
, " ");
2759 obj__printf(obj
, " ");
2762 obj__printf(obj
, " ");
2765 disasm_line__scnprintf(dl
, bf
, size
, !notes
->options
->use_offset
, notes
->widths
.max_ins_name
);
2768 static void ipc_coverage_string(char *bf
, int size
, struct annotation
*notes
)
2770 double ipc
= 0.0, coverage
= 0.0;
2772 if (notes
->hit_cycles
)
2773 ipc
= notes
->hit_insn
/ ((double)notes
->hit_cycles
);
2775 if (notes
->total_insn
) {
2776 coverage
= notes
->cover_insn
* 100.0 /
2777 ((double)notes
->total_insn
);
2780 scnprintf(bf
, size
, "(Average IPC: %.2f, IPC Coverage: %.1f%%)",
2784 static void __annotation_line__write(struct annotation_line
*al
, struct annotation
*notes
,
2785 bool first_line
, bool current_entry
, bool change_color
, int width
,
2786 void *obj
, unsigned int percent_type
,
2787 int (*obj__set_color
)(void *obj
, int color
),
2788 void (*obj__set_percent_color
)(void *obj
, double percent
, bool current
),
2789 int (*obj__set_jumps_percent_color
)(void *obj
, int nr
, bool current
),
2790 void (*obj__printf
)(void *obj
, const char *fmt
, ...),
2791 void (*obj__write_graph
)(void *obj
, int graph
))
2794 double percent_max
= annotation_line__max_percent(al
, notes
, percent_type
);
2795 int pcnt_width
= annotation__pcnt_width(notes
),
2796 cycles_width
= annotation__cycles_width(notes
);
2797 bool show_title
= false;
2801 if (first_line
&& (al
->offset
== -1 || percent_max
== 0.0)) {
2802 if (notes
->have_cycles
) {
2803 if (al
->ipc
== 0.0 && al
->cycles
== 0)
2809 if (al
->offset
!= -1 && percent_max
!= 0.0) {
2812 for (i
= 0; i
< notes
->nr_events
; i
++) {
2815 percent
= annotation_data__percent(&al
->data
[i
], percent_type
);
2817 obj__set_percent_color(obj
, percent
, current_entry
);
2818 if (notes
->options
->show_total_period
) {
2819 obj__printf(obj
, "%11" PRIu64
" ", al
->data
[i
].he
.period
);
2820 } else if (notes
->options
->show_nr_samples
) {
2821 obj__printf(obj
, "%6" PRIu64
" ",
2822 al
->data
[i
].he
.nr_samples
);
2824 obj__printf(obj
, "%6.2f ", percent
);
2828 obj__set_percent_color(obj
, 0, current_entry
);
2831 obj__printf(obj
, "%-*s", pcnt_width
, " ");
2833 obj__printf(obj
, "%-*s", pcnt_width
,
2834 notes
->options
->show_total_period
? "Period" :
2835 notes
->options
->show_nr_samples
? "Samples" : "Percent");
2839 if (notes
->have_cycles
) {
2841 obj__printf(obj
, "%*.2f ", ANNOTATION__IPC_WIDTH
- 1, al
->ipc
);
2842 else if (!show_title
)
2843 obj__printf(obj
, "%*s", ANNOTATION__IPC_WIDTH
, " ");
2845 obj__printf(obj
, "%*s ", ANNOTATION__IPC_WIDTH
- 1, "IPC");
2847 if (!notes
->options
->show_minmax_cycle
) {
2849 obj__printf(obj
, "%*" PRIu64
" ",
2850 ANNOTATION__CYCLES_WIDTH
- 1, al
->cycles
);
2851 else if (!show_title
)
2852 obj__printf(obj
, "%*s",
2853 ANNOTATION__CYCLES_WIDTH
, " ");
2855 obj__printf(obj
, "%*s ",
2856 ANNOTATION__CYCLES_WIDTH
- 1,
2862 scnprintf(str
, sizeof(str
),
2863 "%" PRIu64
"(%" PRIu64
"/%" PRIu64
")",
2864 al
->cycles
, al
->cycles_min
,
2867 obj__printf(obj
, "%*s ",
2868 ANNOTATION__MINMAX_CYCLES_WIDTH
- 1,
2870 } else if (!show_title
)
2871 obj__printf(obj
, "%*s",
2872 ANNOTATION__MINMAX_CYCLES_WIDTH
,
2875 obj__printf(obj
, "%*s ",
2876 ANNOTATION__MINMAX_CYCLES_WIDTH
- 1,
2880 if (show_title
&& !*al
->line
) {
2881 ipc_coverage_string(bf
, sizeof(bf
), notes
);
2882 obj__printf(obj
, "%*s", ANNOTATION__AVG_IPC_WIDTH
, bf
);
2886 obj__printf(obj
, " ");
2889 obj__printf(obj
, "%-*s", width
- pcnt_width
- cycles_width
, " ");
2890 else if (al
->offset
== -1) {
2891 if (al
->line_nr
&& notes
->options
->show_linenr
)
2892 printed
= scnprintf(bf
, sizeof(bf
), "%-*d ", notes
->widths
.addr
+ 1, al
->line_nr
);
2894 printed
= scnprintf(bf
, sizeof(bf
), "%-*s ", notes
->widths
.addr
, " ");
2895 obj__printf(obj
, bf
);
2896 obj__printf(obj
, "%-*s", width
- printed
- pcnt_width
- cycles_width
+ 1, al
->line
);
2898 u64 addr
= al
->offset
;
2901 if (!notes
->options
->use_offset
)
2902 addr
+= notes
->start
;
2904 if (!notes
->options
->use_offset
) {
2905 printed
= scnprintf(bf
, sizeof(bf
), "%" PRIx64
": ", addr
);
2907 if (al
->jump_sources
&&
2908 notes
->options
->offset_level
>= ANNOTATION__OFFSET_JUMP_TARGETS
) {
2909 if (notes
->options
->show_nr_jumps
) {
2911 printed
= scnprintf(bf
, sizeof(bf
), "%*d ",
2912 notes
->widths
.jumps
,
2914 prev
= obj__set_jumps_percent_color(obj
, al
->jump_sources
,
2916 obj__printf(obj
, bf
);
2917 obj__set_color(obj
, prev
);
2920 printed
= scnprintf(bf
, sizeof(bf
), "%*" PRIx64
": ",
2921 notes
->widths
.target
, addr
);
2922 } else if (ins__is_call(&disasm_line(al
)->ins
) &&
2923 notes
->options
->offset_level
>= ANNOTATION__OFFSET_CALL
) {
2925 } else if (notes
->options
->offset_level
== ANNOTATION__MAX_OFFSET_LEVEL
) {
2928 printed
= scnprintf(bf
, sizeof(bf
), "%-*s ",
2929 notes
->widths
.addr
, " ");
2934 color
= obj__set_color(obj
, HE_COLORSET_ADDR
);
2935 obj__printf(obj
, bf
);
2937 obj__set_color(obj
, color
);
2939 disasm_line__write(disasm_line(al
), notes
, obj
, bf
, sizeof(bf
), obj__printf
, obj__write_graph
);
2941 obj__printf(obj
, "%-*s", width
- pcnt_width
- cycles_width
- 3 - printed
, bf
);
2946 void annotation_line__write(struct annotation_line
*al
, struct annotation
*notes
,
2947 struct annotation_write_ops
*wops
,
2948 struct annotation_options
*opts
)
2950 __annotation_line__write(al
, notes
, wops
->first_line
, wops
->current_entry
,
2951 wops
->change_color
, wops
->width
, wops
->obj
,
2953 wops
->set_color
, wops
->set_percent_color
,
2954 wops
->set_jumps_percent_color
, wops
->printf
,
2958 int symbol__annotate2(struct symbol
*sym
, struct map
*map
, struct perf_evsel
*evsel
,
2959 struct annotation_options
*options
, struct arch
**parch
)
2961 struct annotation
*notes
= symbol__annotation(sym
);
2962 size_t size
= symbol__size(sym
);
2963 int nr_pcnt
= 1, err
;
2965 notes
->offsets
= zalloc(size
* sizeof(struct annotation_line
*));
2966 if (notes
->offsets
== NULL
)
2969 if (perf_evsel__is_group_event(evsel
))
2970 nr_pcnt
= evsel
->nr_members
;
2972 err
= symbol__annotate(sym
, map
, evsel
, 0, options
, parch
);
2974 goto out_free_offsets
;
2976 notes
->options
= options
;
2978 symbol__calc_percent(sym
, evsel
);
2980 annotation__set_offsets(notes
, size
);
2981 annotation__mark_jump_targets(notes
, sym
);
2982 annotation__compute_ipc(notes
, size
);
2983 annotation__init_column_widths(notes
, sym
);
2984 notes
->nr_events
= nr_pcnt
;
2986 annotation__update_column_widths(notes
);
2987 sym
->annotate2
= true;
2992 zfree(¬es
->offsets
);
2996 #define ANNOTATION__CFG(n) \
2997 { .name = #n, .value = &annotation__default_options.n, }
3000 * Keep the entries sorted, they are bsearch'ed
3002 static struct annotation_config
{
3005 } annotation__configs
[] = {
3006 ANNOTATION__CFG(hide_src_code
),
3007 ANNOTATION__CFG(jump_arrows
),
3008 ANNOTATION__CFG(offset_level
),
3009 ANNOTATION__CFG(show_linenr
),
3010 ANNOTATION__CFG(show_nr_jumps
),
3011 ANNOTATION__CFG(show_nr_samples
),
3012 ANNOTATION__CFG(show_total_period
),
3013 ANNOTATION__CFG(use_offset
),
3016 #undef ANNOTATION__CFG
3018 static int annotation_config__cmp(const void *name
, const void *cfgp
)
3020 const struct annotation_config
*cfg
= cfgp
;
3022 return strcmp(name
, cfg
->name
);
3025 static int annotation__config(const char *var
, const char *value
,
3026 void *data __maybe_unused
)
3028 struct annotation_config
*cfg
;
3031 if (!strstarts(var
, "annotate."))
3035 cfg
= bsearch(name
, annotation__configs
, ARRAY_SIZE(annotation__configs
),
3036 sizeof(struct annotation_config
), annotation_config__cmp
);
3039 pr_debug("%s variable unknown, ignoring...", var
);
3040 else if (strcmp(var
, "annotate.offset_level") == 0) {
3041 perf_config_int(cfg
->value
, name
, value
);
3043 if (*(int *)cfg
->value
> ANNOTATION__MAX_OFFSET_LEVEL
)
3044 *(int *)cfg
->value
= ANNOTATION__MAX_OFFSET_LEVEL
;
3045 else if (*(int *)cfg
->value
< ANNOTATION__MIN_OFFSET_LEVEL
)
3046 *(int *)cfg
->value
= ANNOTATION__MIN_OFFSET_LEVEL
;
3048 *(bool *)cfg
->value
= perf_config_bool(name
, value
);
3053 void annotation_config__init(void)
3055 perf_config(annotation__config
, NULL
);
3057 annotation__default_options
.show_total_period
= symbol_conf
.show_total_period
;
3058 annotation__default_options
.show_nr_samples
= symbol_conf
.show_nr_samples
;
3061 static unsigned int parse_percent_type(char *str1
, char *str2
)
3063 unsigned int type
= (unsigned int) -1;
3065 if (!strcmp("period", str1
)) {
3066 if (!strcmp("local", str2
))
3067 type
= PERCENT_PERIOD_LOCAL
;
3068 else if (!strcmp("global", str2
))
3069 type
= PERCENT_PERIOD_GLOBAL
;
3072 if (!strcmp("hits", str1
)) {
3073 if (!strcmp("local", str2
))
3074 type
= PERCENT_HITS_LOCAL
;
3075 else if (!strcmp("global", str2
))
3076 type
= PERCENT_HITS_GLOBAL
;
3082 int annotate_parse_percent_type(const struct option
*opt
, const char *_str
,
3083 int unset __maybe_unused
)
3085 struct annotation_options
*opts
= opt
->value
;
3090 str1
= strdup(_str
);
3094 str2
= strchr(str1
, '-');
3100 type
= parse_percent_type(str1
, str2
);
3101 if (type
== (unsigned int) -1)
3102 type
= parse_percent_type(str2
, str1
);
3103 if (type
!= (unsigned int) -1) {
3104 opts
->percent_type
= type
;