1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
4 *
5 * Parts came from builtin-annotate.c, see those files for further
6 * copyright notes.
7 */
8
9 #include <errno.h>
10 #include <inttypes.h>
11 #include <libgen.h>
12 #include <stdlib.h>
13 #include "util.h" // hex_width()
14 #include "ui/ui.h"
15 #include "sort.h"
16 #include "build-id.h"
17 #include "color.h"
18 #include "config.h"
19 #include "dso.h"
20 #include "env.h"
21 #include "map.h"
22 #include "maps.h"
23 #include "symbol.h"
24 #include "srcline.h"
25 #include "units.h"
26 #include "debug.h"
27 #include "annotate.h"
28 #include "evsel.h"
29 #include "evlist.h"
30 #include "bpf-event.h"
31 #include "bpf-utils.h"
32 #include "block-range.h"
33 #include "string2.h"
34 #include "util/event.h"
35 #include "util/sharded_mutex.h"
36 #include "arch/common.h"
37 #include "namespaces.h"
38 #include <regex.h>
39 #include <linux/bitops.h>
40 #include <linux/kernel.h>
41 #include <linux/string.h>
42 #include <linux/zalloc.h>
43 #include <subcmd/parse-options.h>
44 #include <subcmd/run-command.h>
45
46 /* FIXME: For the HE_COLORSET */
47 #include "ui/browser.h"
48
49 /*
50 * FIXME: Using the same values as slang.h,
51 * but that header may not be available everywhere
52 */
53 #define LARROW_CHAR ((unsigned char)',')
54 #define RARROW_CHAR ((unsigned char)'+')
55 #define DARROW_CHAR ((unsigned char)'.')
56 #define UARROW_CHAR ((unsigned char)'-')
57
58 #include <linux/ctype.h>
59
60 /* global annotation options */
61 struct annotation_options annotate_opts;
62
63 static regex_t file_lineno;
64
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);
68 static int call__scnprintf(struct ins *ins, char *bf, size_t size,
69 struct ins_operands *ops, int max_ins_name);
70 static int jump__scnprintf(struct ins *ins, char *bf, size_t size,
71 struct ins_operands *ops, int max_ins_name);
72
73 struct arch {
74 const char *name;
75 struct ins *instructions;
76 size_t nr_instructions;
77 size_t nr_instructions_allocated;
78 struct ins_ops *(*associate_instruction_ops)(struct arch *arch, const char *name);
79 bool sorted_instructions;
80 bool initialized;
81 const char *insn_suffix;
82 void *priv;
83 unsigned int model;
84 unsigned int family;
85 int (*init)(struct arch *arch, char *cpuid);
86 bool (*ins_is_fused)(struct arch *arch, const char *ins1,
87 const char *ins2);
88 struct {
89 char comment_char;
90 char skip_functions_char;
91 } objdump;
92 };
93
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;
101
arch__grow_instructions(struct arch * arch)102 static int arch__grow_instructions(struct arch *arch)
103 {
104 struct ins *new_instructions;
105 size_t new_nr_allocated;
106
107 if (arch->nr_instructions_allocated == 0 && arch->instructions)
108 goto grow_from_non_allocated_table;
109
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)
113 return -1;
114
115 out_update_instructions:
116 arch->instructions = new_instructions;
117 arch->nr_instructions_allocated = new_nr_allocated;
118 return 0;
119
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)
124 return -1;
125
126 memcpy(new_instructions, arch->instructions, arch->nr_instructions);
127 goto out_update_instructions;
128 }
129
arch__associate_ins_ops(struct arch * arch,const char * name,struct ins_ops * ops)130 static int arch__associate_ins_ops(struct arch* arch, const char *name, struct ins_ops *ops)
131 {
132 struct ins *ins;
133
134 if (arch->nr_instructions == arch->nr_instructions_allocated &&
135 arch__grow_instructions(arch))
136 return -1;
137
138 ins = &arch->instructions[arch->nr_instructions];
139 ins->name = strdup(name);
140 if (!ins->name)
141 return -1;
142
143 ins->ops = ops;
144 arch->nr_instructions++;
145
146 ins__sort(arch);
147 return 0;
148 }
149
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/loongarch/annotate/instructions.c"
155 #include "arch/mips/annotate/instructions.c"
156 #include "arch/x86/annotate/instructions.c"
157 #include "arch/powerpc/annotate/instructions.c"
158 #include "arch/riscv64/annotate/instructions.c"
159 #include "arch/s390/annotate/instructions.c"
160 #include "arch/sparc/annotate/instructions.c"
161
162 static struct arch architectures[] = {
163 {
164 .name = "arc",
165 .init = arc__annotate_init,
166 },
167 {
168 .name = "arm",
169 .init = arm__annotate_init,
170 },
171 {
172 .name = "arm64",
173 .init = arm64__annotate_init,
174 },
175 {
176 .name = "csky",
177 .init = csky__annotate_init,
178 },
179 {
180 .name = "mips",
181 .init = mips__annotate_init,
182 .objdump = {
183 .comment_char = '#',
184 },
185 },
186 {
187 .name = "x86",
188 .init = x86__annotate_init,
189 .instructions = x86__instructions,
190 .nr_instructions = ARRAY_SIZE(x86__instructions),
191 .insn_suffix = "bwlq",
192 .objdump = {
193 .comment_char = '#',
194 },
195 },
196 {
197 .name = "powerpc",
198 .init = powerpc__annotate_init,
199 },
200 {
201 .name = "riscv64",
202 .init = riscv64__annotate_init,
203 },
204 {
205 .name = "s390",
206 .init = s390__annotate_init,
207 .objdump = {
208 .comment_char = '#',
209 },
210 },
211 {
212 .name = "sparc",
213 .init = sparc__annotate_init,
214 .objdump = {
215 .comment_char = '#',
216 },
217 },
218 {
219 .name = "loongarch",
220 .init = loongarch__annotate_init,
221 .objdump = {
222 .comment_char = '#',
223 },
224 },
225 };
226
ins__delete(struct ins_operands * ops)227 static void ins__delete(struct ins_operands *ops)
228 {
229 if (ops == NULL)
230 return;
231 zfree(&ops->source.raw);
232 zfree(&ops->source.name);
233 zfree(&ops->target.raw);
234 zfree(&ops->target.name);
235 }
236
ins__raw_scnprintf(struct ins * ins,char * bf,size_t size,struct ins_operands * ops,int max_ins_name)237 static int ins__raw_scnprintf(struct ins *ins, char *bf, size_t size,
238 struct ins_operands *ops, int max_ins_name)
239 {
240 return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name, ops->raw);
241 }
242
ins__scnprintf(struct ins * ins,char * bf,size_t size,struct ins_operands * ops,int max_ins_name)243 int ins__scnprintf(struct ins *ins, char *bf, size_t size,
244 struct ins_operands *ops, int max_ins_name)
245 {
246 if (ins->ops->scnprintf)
247 return ins->ops->scnprintf(ins, bf, size, ops, max_ins_name);
248
249 return ins__raw_scnprintf(ins, bf, size, ops, max_ins_name);
250 }
251
ins__is_fused(struct arch * arch,const char * ins1,const char * ins2)252 bool ins__is_fused(struct arch *arch, const char *ins1, const char *ins2)
253 {
254 if (!arch || !arch->ins_is_fused)
255 return false;
256
257 return arch->ins_is_fused(arch, ins1, ins2);
258 }
259
call__parse(struct arch * arch,struct ins_operands * ops,struct map_symbol * ms)260 static int call__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms)
261 {
262 char *endptr, *tok, *name;
263 struct map *map = ms->map;
264 struct addr_map_symbol target = {
265 .ms = { .map = map, },
266 };
267
268 ops->target.addr = strtoull(ops->raw, &endptr, 16);
269
270 name = strchr(endptr, '<');
271 if (name == NULL)
272 goto indirect_call;
273
274 name++;
275
276 if (arch->objdump.skip_functions_char &&
277 strchr(name, arch->objdump.skip_functions_char))
278 return -1;
279
280 tok = strchr(name, '>');
281 if (tok == NULL)
282 return -1;
283
284 *tok = '\0';
285 ops->target.name = strdup(name);
286 *tok = '>';
287
288 if (ops->target.name == NULL)
289 return -1;
290 find_target:
291 target.addr = map__objdump_2mem(map, ops->target.addr);
292
293 if (maps__find_ams(ms->maps, &target) == 0 &&
294 map__rip_2objdump(target.ms.map, map__map_ip(target.ms.map, target.addr)) == ops->target.addr)
295 ops->target.sym = target.ms.sym;
296
297 return 0;
298
299 indirect_call:
300 tok = strchr(endptr, '*');
301 if (tok != NULL) {
302 endptr++;
303
304 /* Indirect call can use a non-rip register and offset: callq *0x8(%rbx).
305 * Do not parse such instruction. */
306 if (strstr(endptr, "(%r") == NULL)
307 ops->target.addr = strtoull(endptr, NULL, 16);
308 }
309 goto find_target;
310 }
311
call__scnprintf(struct ins * ins,char * bf,size_t size,struct ins_operands * ops,int max_ins_name)312 static int call__scnprintf(struct ins *ins, char *bf, size_t size,
313 struct ins_operands *ops, int max_ins_name)
314 {
315 if (ops->target.sym)
316 return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name, ops->target.sym->name);
317
318 if (ops->target.addr == 0)
319 return ins__raw_scnprintf(ins, bf, size, ops, max_ins_name);
320
321 if (ops->target.name)
322 return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name, ops->target.name);
323
324 return scnprintf(bf, size, "%-*s *%" PRIx64, max_ins_name, ins->name, ops->target.addr);
325 }
326
327 static struct ins_ops call_ops = {
328 .parse = call__parse,
329 .scnprintf = call__scnprintf,
330 };
331
ins__is_call(const struct ins * ins)332 bool ins__is_call(const struct ins *ins)
333 {
334 return ins->ops == &call_ops || ins->ops == &s390_call_ops || ins->ops == &loongarch_call_ops;
335 }
336
337 /*
338 * Prevents from matching commas in the comment section, e.g.:
339 * ffff200008446e70: b.cs ffff2000084470f4 <generic_exec_single+0x314> // b.hs, b.nlast
340 *
341 * and skip comma as part of function arguments, e.g.:
342 * 1d8b4ac <linemap_lookup(line_maps const*, unsigned int)+0xcc>
343 */
validate_comma(const char * c,struct ins_operands * ops)344 static inline const char *validate_comma(const char *c, struct ins_operands *ops)
345 {
346 if (ops->raw_comment && c > ops->raw_comment)
347 return NULL;
348
349 if (ops->raw_func_start && c > ops->raw_func_start)
350 return NULL;
351
352 return c;
353 }
354
jump__parse(struct arch * arch,struct ins_operands * ops,struct map_symbol * ms)355 static int jump__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms)
356 {
357 struct map *map = ms->map;
358 struct symbol *sym = ms->sym;
359 struct addr_map_symbol target = {
360 .ms = { .map = map, },
361 };
362 const char *c = strchr(ops->raw, ',');
363 u64 start, end;
364
365 ops->raw_comment = strchr(ops->raw, arch->objdump.comment_char);
366 ops->raw_func_start = strchr(ops->raw, '<');
367
368 c = validate_comma(c, ops);
369
370 /*
371 * Examples of lines to parse for the _cpp_lex_token@@Base
372 * function:
373 *
374 * 1159e6c: jne 115aa32 <_cpp_lex_token@@Base+0xf92>
375 * 1159e8b: jne c469be <cpp_named_operator2name@@Base+0xa72>
376 *
377 * The first is a jump to an offset inside the same function,
378 * the second is to another function, i.e. that 0xa72 is an
379 * offset in the cpp_named_operator2name@@base function.
380 */
381 /*
382 * skip over possible up to 2 operands to get to address, e.g.:
383 * tbnz w0, #26, ffff0000083cd190 <security_file_permission+0xd0>
384 */
385 if (c++ != NULL) {
386 ops->target.addr = strtoull(c, NULL, 16);
387 if (!ops->target.addr) {
388 c = strchr(c, ',');
389 c = validate_comma(c, ops);
390 if (c++ != NULL)
391 ops->target.addr = strtoull(c, NULL, 16);
392 }
393 } else {
394 ops->target.addr = strtoull(ops->raw, NULL, 16);
395 }
396
397 target.addr = map__objdump_2mem(map, ops->target.addr);
398 start = map__unmap_ip(map, sym->start);
399 end = map__unmap_ip(map, sym->end);
400
401 ops->target.outside = target.addr < start || target.addr > end;
402
403 /*
404 * FIXME: things like this in _cpp_lex_token (gcc's cc1 program):
405
406 cpp_named_operator2name@@Base+0xa72
407
408 * Point to a place that is after the cpp_named_operator2name
409 * boundaries, i.e. in the ELF symbol table for cc1
410 * cpp_named_operator2name is marked as being 32-bytes long, but it in
411 * fact is much larger than that, so we seem to need a symbols__find()
412 * routine that looks for >= current->start and < next_symbol->start,
413 * possibly just for C++ objects?
414 *
415 * For now lets just make some progress by marking jumps to outside the
416 * current function as call like.
417 *
418 * Actual navigation will come next, with further understanding of how
419 * the symbol searching and disassembly should be done.
420 */
421 if (maps__find_ams(ms->maps, &target) == 0 &&
422 map__rip_2objdump(target.ms.map, map__map_ip(target.ms.map, target.addr)) == ops->target.addr)
423 ops->target.sym = target.ms.sym;
424
425 if (!ops->target.outside) {
426 ops->target.offset = target.addr - start;
427 ops->target.offset_avail = true;
428 } else {
429 ops->target.offset_avail = false;
430 }
431
432 return 0;
433 }
434
jump__scnprintf(struct ins * ins,char * bf,size_t size,struct ins_operands * ops,int max_ins_name)435 static int jump__scnprintf(struct ins *ins, char *bf, size_t size,
436 struct ins_operands *ops, int max_ins_name)
437 {
438 const char *c;
439
440 if (!ops->target.addr || ops->target.offset < 0)
441 return ins__raw_scnprintf(ins, bf, size, ops, max_ins_name);
442
443 if (ops->target.outside && ops->target.sym != NULL)
444 return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name, ops->target.sym->name);
445
446 c = strchr(ops->raw, ',');
447 c = validate_comma(c, ops);
448
449 if (c != NULL) {
450 const char *c2 = strchr(c + 1, ',');
451
452 c2 = validate_comma(c2, ops);
453 /* check for 3-op insn */
454 if (c2 != NULL)
455 c = c2;
456 c++;
457
458 /* mirror arch objdump's space-after-comma style */
459 if (*c == ' ')
460 c++;
461 }
462
463 return scnprintf(bf, size, "%-*s %.*s%" PRIx64, max_ins_name,
464 ins->name, c ? c - ops->raw : 0, ops->raw,
465 ops->target.offset);
466 }
467
468 static struct ins_ops jump_ops = {
469 .parse = jump__parse,
470 .scnprintf = jump__scnprintf,
471 };
472
ins__is_jump(const struct ins * ins)473 bool ins__is_jump(const struct ins *ins)
474 {
475 return ins->ops == &jump_ops || ins->ops == &loongarch_jump_ops;
476 }
477
comment__symbol(char * raw,char * comment,u64 * addrp,char ** namep)478 static int comment__symbol(char *raw, char *comment, u64 *addrp, char **namep)
479 {
480 char *endptr, *name, *t;
481
482 if (strstr(raw, "(%rip)") == NULL)
483 return 0;
484
485 *addrp = strtoull(comment, &endptr, 16);
486 if (endptr == comment)
487 return 0;
488 name = strchr(endptr, '<');
489 if (name == NULL)
490 return -1;
491
492 name++;
493
494 t = strchr(name, '>');
495 if (t == NULL)
496 return 0;
497
498 *t = '\0';
499 *namep = strdup(name);
500 *t = '>';
501
502 return 0;
503 }
504
lock__parse(struct arch * arch,struct ins_operands * ops,struct map_symbol * ms)505 static int lock__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms)
506 {
507 ops->locked.ops = zalloc(sizeof(*ops->locked.ops));
508 if (ops->locked.ops == NULL)
509 return 0;
510
511 if (disasm_line__parse(ops->raw, &ops->locked.ins.name, &ops->locked.ops->raw) < 0)
512 goto out_free_ops;
513
514 ops->locked.ins.ops = ins__find(arch, ops->locked.ins.name);
515
516 if (ops->locked.ins.ops == NULL)
517 goto out_free_ops;
518
519 if (ops->locked.ins.ops->parse &&
520 ops->locked.ins.ops->parse(arch, ops->locked.ops, ms) < 0)
521 goto out_free_ops;
522
523 return 0;
524
525 out_free_ops:
526 zfree(&ops->locked.ops);
527 return 0;
528 }
529
lock__scnprintf(struct ins * ins,char * bf,size_t size,struct ins_operands * ops,int max_ins_name)530 static int lock__scnprintf(struct ins *ins, char *bf, size_t size,
531 struct ins_operands *ops, int max_ins_name)
532 {
533 int printed;
534
535 if (ops->locked.ins.ops == NULL)
536 return ins__raw_scnprintf(ins, bf, size, ops, max_ins_name);
537
538 printed = scnprintf(bf, size, "%-*s ", max_ins_name, ins->name);
539 return printed + ins__scnprintf(&ops->locked.ins, bf + printed,
540 size - printed, ops->locked.ops, max_ins_name);
541 }
542
lock__delete(struct ins_operands * ops)543 static void lock__delete(struct ins_operands *ops)
544 {
545 struct ins *ins = &ops->locked.ins;
546
547 if (ins->ops && ins->ops->free)
548 ins->ops->free(ops->locked.ops);
549 else
550 ins__delete(ops->locked.ops);
551
552 zfree(&ops->locked.ops);
553 zfree(&ops->target.raw);
554 zfree(&ops->target.name);
555 }
556
557 static struct ins_ops lock_ops = {
558 .free = lock__delete,
559 .parse = lock__parse,
560 .scnprintf = lock__scnprintf,
561 };
562
mov__parse(struct arch * arch,struct ins_operands * ops,struct map_symbol * ms __maybe_unused)563 static int mov__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms __maybe_unused)
564 {
565 char *s = strchr(ops->raw, ','), *target, *comment, prev;
566
567 if (s == NULL)
568 return -1;
569
570 *s = '\0';
571
572 /*
573 * x86 SIB addressing has something like 0x8(%rax, %rcx, 1)
574 * then it needs to have the closing parenthesis.
575 */
576 if (strchr(ops->raw, '(')) {
577 *s = ',';
578 s = strchr(ops->raw, ')');
579 if (s == NULL || s[1] != ',')
580 return -1;
581 *++s = '\0';
582 }
583
584 ops->source.raw = strdup(ops->raw);
585 *s = ',';
586
587 if (ops->source.raw == NULL)
588 return -1;
589
590 target = skip_spaces(++s);
591 comment = strchr(s, arch->objdump.comment_char);
592
593 if (comment != NULL)
594 s = comment - 1;
595 else
596 s = strchr(s, '\0') - 1;
597
598 while (s > target && isspace(s[0]))
599 --s;
600 s++;
601 prev = *s;
602 *s = '\0';
603
604 ops->target.raw = strdup(target);
605 *s = prev;
606
607 if (ops->target.raw == NULL)
608 goto out_free_source;
609
610 if (comment == NULL)
611 return 0;
612
613 comment = skip_spaces(comment);
614 comment__symbol(ops->source.raw, comment + 1, &ops->source.addr, &ops->source.name);
615 comment__symbol(ops->target.raw, comment + 1, &ops->target.addr, &ops->target.name);
616
617 return 0;
618
619 out_free_source:
620 zfree(&ops->source.raw);
621 return -1;
622 }
623
mov__scnprintf(struct ins * ins,char * bf,size_t size,struct ins_operands * ops,int max_ins_name)624 static int mov__scnprintf(struct ins *ins, char *bf, size_t size,
625 struct ins_operands *ops, int max_ins_name)
626 {
627 return scnprintf(bf, size, "%-*s %s,%s", max_ins_name, ins->name,
628 ops->source.name ?: ops->source.raw,
629 ops->target.name ?: ops->target.raw);
630 }
631
632 static struct ins_ops mov_ops = {
633 .parse = mov__parse,
634 .scnprintf = mov__scnprintf,
635 };
636
dec__parse(struct arch * arch __maybe_unused,struct ins_operands * ops,struct map_symbol * ms __maybe_unused)637 static int dec__parse(struct arch *arch __maybe_unused, struct ins_operands *ops, struct map_symbol *ms __maybe_unused)
638 {
639 char *target, *comment, *s, prev;
640
641 target = s = ops->raw;
642
643 while (s[0] != '\0' && !isspace(s[0]))
644 ++s;
645 prev = *s;
646 *s = '\0';
647
648 ops->target.raw = strdup(target);
649 *s = prev;
650
651 if (ops->target.raw == NULL)
652 return -1;
653
654 comment = strchr(s, arch->objdump.comment_char);
655 if (comment == NULL)
656 return 0;
657
658 comment = skip_spaces(comment);
659 comment__symbol(ops->target.raw, comment + 1, &ops->target.addr, &ops->target.name);
660
661 return 0;
662 }
663
dec__scnprintf(struct ins * ins,char * bf,size_t size,struct ins_operands * ops,int max_ins_name)664 static int dec__scnprintf(struct ins *ins, char *bf, size_t size,
665 struct ins_operands *ops, int max_ins_name)
666 {
667 return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name,
668 ops->target.name ?: ops->target.raw);
669 }
670
671 static struct ins_ops dec_ops = {
672 .parse = dec__parse,
673 .scnprintf = dec__scnprintf,
674 };
675
nop__scnprintf(struct ins * ins __maybe_unused,char * bf,size_t size,struct ins_operands * ops __maybe_unused,int max_ins_name)676 static int nop__scnprintf(struct ins *ins __maybe_unused, char *bf, size_t size,
677 struct ins_operands *ops __maybe_unused, int max_ins_name)
678 {
679 return scnprintf(bf, size, "%-*s", max_ins_name, "nop");
680 }
681
682 static struct ins_ops nop_ops = {
683 .scnprintf = nop__scnprintf,
684 };
685
686 static struct ins_ops ret_ops = {
687 .scnprintf = ins__raw_scnprintf,
688 };
689
ins__is_ret(const struct ins * ins)690 bool ins__is_ret(const struct ins *ins)
691 {
692 return ins->ops == &ret_ops;
693 }
694
ins__is_lock(const struct ins * ins)695 bool ins__is_lock(const struct ins *ins)
696 {
697 return ins->ops == &lock_ops;
698 }
699
ins__key_cmp(const void * name,const void * insp)700 static int ins__key_cmp(const void *name, const void *insp)
701 {
702 const struct ins *ins = insp;
703
704 return strcmp(name, ins->name);
705 }
706
ins__cmp(const void * a,const void * b)707 static int ins__cmp(const void *a, const void *b)
708 {
709 const struct ins *ia = a;
710 const struct ins *ib = b;
711
712 return strcmp(ia->name, ib->name);
713 }
714
ins__sort(struct arch * arch)715 static void ins__sort(struct arch *arch)
716 {
717 const int nmemb = arch->nr_instructions;
718
719 qsort(arch->instructions, nmemb, sizeof(struct ins), ins__cmp);
720 }
721
__ins__find(struct arch * arch,const char * name)722 static struct ins_ops *__ins__find(struct arch *arch, const char *name)
723 {
724 struct ins *ins;
725 const int nmemb = arch->nr_instructions;
726
727 if (!arch->sorted_instructions) {
728 ins__sort(arch);
729 arch->sorted_instructions = true;
730 }
731
732 ins = bsearch(name, arch->instructions, nmemb, sizeof(struct ins), ins__key_cmp);
733 if (ins)
734 return ins->ops;
735
736 if (arch->insn_suffix) {
737 char tmp[32];
738 char suffix;
739 size_t len = strlen(name);
740
741 if (len == 0 || len >= sizeof(tmp))
742 return NULL;
743
744 suffix = name[len - 1];
745 if (strchr(arch->insn_suffix, suffix) == NULL)
746 return NULL;
747
748 strcpy(tmp, name);
749 tmp[len - 1] = '\0'; /* remove the suffix and check again */
750
751 ins = bsearch(tmp, arch->instructions, nmemb, sizeof(struct ins), ins__key_cmp);
752 }
753 return ins ? ins->ops : NULL;
754 }
755
ins__find(struct arch * arch,const char * name)756 static struct ins_ops *ins__find(struct arch *arch, const char *name)
757 {
758 struct ins_ops *ops = __ins__find(arch, name);
759
760 if (!ops && arch->associate_instruction_ops)
761 ops = arch->associate_instruction_ops(arch, name);
762
763 return ops;
764 }
765
arch__key_cmp(const void * name,const void * archp)766 static int arch__key_cmp(const void *name, const void *archp)
767 {
768 const struct arch *arch = archp;
769
770 return strcmp(name, arch->name);
771 }
772
arch__cmp(const void * a,const void * b)773 static int arch__cmp(const void *a, const void *b)
774 {
775 const struct arch *aa = a;
776 const struct arch *ab = b;
777
778 return strcmp(aa->name, ab->name);
779 }
780
arch__sort(void)781 static void arch__sort(void)
782 {
783 const int nmemb = ARRAY_SIZE(architectures);
784
785 qsort(architectures, nmemb, sizeof(struct arch), arch__cmp);
786 }
787
arch__find(const char * name)788 static struct arch *arch__find(const char *name)
789 {
790 const int nmemb = ARRAY_SIZE(architectures);
791 static bool sorted;
792
793 if (!sorted) {
794 arch__sort();
795 sorted = true;
796 }
797
798 return bsearch(name, architectures, nmemb, sizeof(struct arch), arch__key_cmp);
799 }
800
annotated_source__new(void)801 static struct annotated_source *annotated_source__new(void)
802 {
803 struct annotated_source *src = zalloc(sizeof(*src));
804
805 if (src != NULL)
806 INIT_LIST_HEAD(&src->source);
807
808 return src;
809 }
810
annotated_source__delete(struct annotated_source * src)811 static __maybe_unused void annotated_source__delete(struct annotated_source *src)
812 {
813 if (src == NULL)
814 return;
815 zfree(&src->histograms);
816 free(src);
817 }
818
annotated_source__alloc_histograms(struct annotated_source * src,size_t size,int nr_hists)819 static int annotated_source__alloc_histograms(struct annotated_source *src,
820 size_t size, int nr_hists)
821 {
822 size_t sizeof_sym_hist;
823
824 /*
825 * Add buffer of one element for zero length symbol.
826 * When sample is taken from first instruction of
827 * zero length symbol, perf still resolves it and
828 * shows symbol name in perf report and allows to
829 * annotate it.
830 */
831 if (size == 0)
832 size = 1;
833
834 /* Check for overflow when calculating sizeof_sym_hist */
835 if (size > (SIZE_MAX - sizeof(struct sym_hist)) / sizeof(struct sym_hist_entry))
836 return -1;
837
838 sizeof_sym_hist = (sizeof(struct sym_hist) + size * sizeof(struct sym_hist_entry));
839
840 /* Check for overflow in zalloc argument */
841 if (sizeof_sym_hist > SIZE_MAX / nr_hists)
842 return -1;
843
844 src->sizeof_sym_hist = sizeof_sym_hist;
845 src->nr_histograms = nr_hists;
846 src->histograms = calloc(nr_hists, sizeof_sym_hist) ;
847 return src->histograms ? 0 : -1;
848 }
849
symbol__annotate_zero_histograms(struct symbol * sym)850 void symbol__annotate_zero_histograms(struct symbol *sym)
851 {
852 struct annotation *notes = symbol__annotation(sym);
853
854 annotation__lock(notes);
855 if (notes->src != NULL) {
856 memset(notes->src->histograms, 0,
857 notes->src->nr_histograms * notes->src->sizeof_sym_hist);
858 }
859 if (notes->branch && notes->branch->cycles_hist) {
860 memset(notes->branch->cycles_hist, 0,
861 symbol__size(sym) * sizeof(struct cyc_hist));
862 }
863 annotation__unlock(notes);
864 }
865
__symbol__account_cycles(struct cyc_hist * ch,u64 start,unsigned offset,unsigned cycles,unsigned have_start)866 static int __symbol__account_cycles(struct cyc_hist *ch,
867 u64 start,
868 unsigned offset, unsigned cycles,
869 unsigned have_start)
870 {
871 /*
872 * For now we can only account one basic block per
873 * final jump. But multiple could be overlapping.
874 * Always account the longest one. So when
875 * a shorter one has been already seen throw it away.
876 *
877 * We separately always account the full cycles.
878 */
879 ch[offset].num_aggr++;
880 ch[offset].cycles_aggr += cycles;
881
882 if (cycles > ch[offset].cycles_max)
883 ch[offset].cycles_max = cycles;
884
885 if (ch[offset].cycles_min) {
886 if (cycles && cycles < ch[offset].cycles_min)
887 ch[offset].cycles_min = cycles;
888 } else
889 ch[offset].cycles_min = cycles;
890
891 if (!have_start && ch[offset].have_start)
892 return 0;
893 if (ch[offset].num) {
894 if (have_start && (!ch[offset].have_start ||
895 ch[offset].start > start)) {
896 ch[offset].have_start = 0;
897 ch[offset].cycles = 0;
898 ch[offset].num = 0;
899 if (ch[offset].reset < 0xffff)
900 ch[offset].reset++;
901 } else if (have_start &&
902 ch[offset].start < start)
903 return 0;
904 }
905
906 if (ch[offset].num < NUM_SPARKS)
907 ch[offset].cycles_spark[ch[offset].num] = cycles;
908
909 ch[offset].have_start = have_start;
910 ch[offset].start = start;
911 ch[offset].cycles += cycles;
912 ch[offset].num++;
913 return 0;
914 }
915
__symbol__inc_addr_samples(struct map_symbol * ms,struct annotated_source * src,int evidx,u64 addr,struct perf_sample * sample)916 static int __symbol__inc_addr_samples(struct map_symbol *ms,
917 struct annotated_source *src, int evidx, u64 addr,
918 struct perf_sample *sample)
919 {
920 struct symbol *sym = ms->sym;
921 unsigned offset;
922 struct sym_hist *h;
923
924 pr_debug3("%s: addr=%#" PRIx64 "\n", __func__, map__unmap_ip(ms->map, addr));
925
926 if ((addr < sym->start || addr >= sym->end) &&
927 (addr != sym->end || sym->start != sym->end)) {
928 pr_debug("%s(%d): ERANGE! sym->name=%s, start=%#" PRIx64 ", addr=%#" PRIx64 ", end=%#" PRIx64 "\n",
929 __func__, __LINE__, sym->name, sym->start, addr, sym->end);
930 return -ERANGE;
931 }
932
933 offset = addr - sym->start;
934 h = annotated_source__histogram(src, evidx);
935 if (h == NULL) {
936 pr_debug("%s(%d): ENOMEM! sym->name=%s, start=%#" PRIx64 ", addr=%#" PRIx64 ", end=%#" PRIx64 ", func: %d\n",
937 __func__, __LINE__, sym->name, sym->start, addr, sym->end, sym->type == STT_FUNC);
938 return -ENOMEM;
939 }
940 h->nr_samples++;
941 h->addr[offset].nr_samples++;
942 h->period += sample->period;
943 h->addr[offset].period += sample->period;
944
945 pr_debug3("%#" PRIx64 " %s: period++ [addr: %#" PRIx64 ", %#" PRIx64
946 ", evidx=%d] => nr_samples: %" PRIu64 ", period: %" PRIu64 "\n",
947 sym->start, sym->name, addr, addr - sym->start, evidx,
948 h->addr[offset].nr_samples, h->addr[offset].period);
949 return 0;
950 }
951
annotation__get_branch(struct annotation * notes)952 static struct annotated_branch *annotation__get_branch(struct annotation *notes)
953 {
954 if (notes == NULL)
955 return NULL;
956
957 if (notes->branch == NULL)
958 notes->branch = zalloc(sizeof(*notes->branch));
959
960 return notes->branch;
961 }
962
symbol__cycles_hist(struct symbol * sym)963 static struct cyc_hist *symbol__cycles_hist(struct symbol *sym)
964 {
965 struct annotation *notes = symbol__annotation(sym);
966 struct annotated_branch *branch;
967
968 branch = annotation__get_branch(notes);
969 if (branch == NULL)
970 return NULL;
971
972 if (branch->cycles_hist == NULL) {
973 const size_t size = symbol__size(sym);
974
975 branch->cycles_hist = calloc(size, sizeof(struct cyc_hist));
976 }
977
978 return branch->cycles_hist;
979 }
980
symbol__hists(struct symbol * sym,int nr_hists)981 struct annotated_source *symbol__hists(struct symbol *sym, int nr_hists)
982 {
983 struct annotation *notes = symbol__annotation(sym);
984
985 if (notes->src == NULL) {
986 notes->src = annotated_source__new();
987 if (notes->src == NULL)
988 return NULL;
989 goto alloc_histograms;
990 }
991
992 if (notes->src->histograms == NULL) {
993 alloc_histograms:
994 annotated_source__alloc_histograms(notes->src, symbol__size(sym),
995 nr_hists);
996 }
997
998 return notes->src;
999 }
1000
symbol__inc_addr_samples(struct map_symbol * ms,struct evsel * evsel,u64 addr,struct perf_sample * sample)1001 static int symbol__inc_addr_samples(struct map_symbol *ms,
1002 struct evsel *evsel, u64 addr,
1003 struct perf_sample *sample)
1004 {
1005 struct symbol *sym = ms->sym;
1006 struct annotated_source *src;
1007
1008 if (sym == NULL)
1009 return 0;
1010 src = symbol__hists(sym, evsel->evlist->core.nr_entries);
1011 return src ? __symbol__inc_addr_samples(ms, src, evsel->core.idx, addr, sample) : 0;
1012 }
1013
symbol__account_cycles(u64 addr,u64 start,struct symbol * sym,unsigned cycles)1014 static int symbol__account_cycles(u64 addr, u64 start,
1015 struct symbol *sym, unsigned cycles)
1016 {
1017 struct cyc_hist *cycles_hist;
1018 unsigned offset;
1019
1020 if (sym == NULL)
1021 return 0;
1022 cycles_hist = symbol__cycles_hist(sym);
1023 if (cycles_hist == NULL)
1024 return -ENOMEM;
1025 if (addr < sym->start || addr >= sym->end)
1026 return -ERANGE;
1027
1028 if (start) {
1029 if (start < sym->start || start >= sym->end)
1030 return -ERANGE;
1031 if (start >= addr)
1032 start = 0;
1033 }
1034 offset = addr - sym->start;
1035 return __symbol__account_cycles(cycles_hist,
1036 start ? start - sym->start : 0,
1037 offset, cycles,
1038 !!start);
1039 }
1040
addr_map_symbol__account_cycles(struct addr_map_symbol * ams,struct addr_map_symbol * start,unsigned cycles)1041 int addr_map_symbol__account_cycles(struct addr_map_symbol *ams,
1042 struct addr_map_symbol *start,
1043 unsigned cycles)
1044 {
1045 u64 saddr = 0;
1046 int err;
1047
1048 if (!cycles)
1049 return 0;
1050
1051 /*
1052 * Only set start when IPC can be computed. We can only
1053 * compute it when the basic block is completely in a single
1054 * function.
1055 * Special case the case when the jump is elsewhere, but
1056 * it starts on the function start.
1057 */
1058 if (start &&
1059 (start->ms.sym == ams->ms.sym ||
1060 (ams->ms.sym &&
1061 start->addr == ams->ms.sym->start + map__start(ams->ms.map))))
1062 saddr = start->al_addr;
1063 if (saddr == 0)
1064 pr_debug2("BB with bad start: addr %"PRIx64" start %"PRIx64" sym %"PRIx64" saddr %"PRIx64"\n",
1065 ams->addr,
1066 start ? start->addr : 0,
1067 ams->ms.sym ? ams->ms.sym->start + map__start(ams->ms.map) : 0,
1068 saddr);
1069 err = symbol__account_cycles(ams->al_addr, saddr, ams->ms.sym, cycles);
1070 if (err)
1071 pr_debug2("account_cycles failed %d\n", err);
1072 return err;
1073 }
1074
annotation__count_insn(struct annotation * notes,u64 start,u64 end)1075 static unsigned annotation__count_insn(struct annotation *notes, u64 start, u64 end)
1076 {
1077 unsigned n_insn = 0;
1078 u64 offset;
1079
1080 for (offset = start; offset <= end; offset++) {
1081 if (notes->offsets[offset])
1082 n_insn++;
1083 }
1084 return n_insn;
1085 }
1086
annotated_branch__delete(struct annotated_branch * branch)1087 static void annotated_branch__delete(struct annotated_branch *branch)
1088 {
1089 if (branch) {
1090 zfree(&branch->cycles_hist);
1091 free(branch);
1092 }
1093 }
1094
annotation__count_and_fill(struct annotation * notes,u64 start,u64 end,struct cyc_hist * ch)1095 static void annotation__count_and_fill(struct annotation *notes, u64 start, u64 end, struct cyc_hist *ch)
1096 {
1097 unsigned n_insn;
1098 unsigned int cover_insn = 0;
1099 u64 offset;
1100
1101 n_insn = annotation__count_insn(notes, start, end);
1102 if (n_insn && ch->num && ch->cycles) {
1103 struct annotated_branch *branch;
1104 float ipc = n_insn / ((double)ch->cycles / (double)ch->num);
1105
1106 /* Hide data when there are too many overlaps. */
1107 if (ch->reset >= 0x7fff)
1108 return;
1109
1110 for (offset = start; offset <= end; offset++) {
1111 struct annotation_line *al = notes->offsets[offset];
1112
1113 if (al && al->cycles && al->cycles->ipc == 0.0) {
1114 al->cycles->ipc = ipc;
1115 cover_insn++;
1116 }
1117 }
1118
1119 branch = annotation__get_branch(notes);
1120 if (cover_insn && branch) {
1121 branch->hit_cycles += ch->cycles;
1122 branch->hit_insn += n_insn * ch->num;
1123 branch->cover_insn += cover_insn;
1124 }
1125 }
1126 }
1127
annotation__compute_ipc(struct annotation * notes,size_t size)1128 static int annotation__compute_ipc(struct annotation *notes, size_t size)
1129 {
1130 int err = 0;
1131 s64 offset;
1132
1133 if (!notes->branch || !notes->branch->cycles_hist)
1134 return 0;
1135
1136 notes->branch->total_insn = annotation__count_insn(notes, 0, size - 1);
1137 notes->branch->hit_cycles = 0;
1138 notes->branch->hit_insn = 0;
1139 notes->branch->cover_insn = 0;
1140
1141 annotation__lock(notes);
1142 for (offset = size - 1; offset >= 0; --offset) {
1143 struct cyc_hist *ch;
1144
1145 ch = ¬es->branch->cycles_hist[offset];
1146 if (ch && ch->cycles) {
1147 struct annotation_line *al;
1148
1149 al = notes->offsets[offset];
1150 if (al && al->cycles == NULL) {
1151 al->cycles = zalloc(sizeof(*al->cycles));
1152 if (al->cycles == NULL) {
1153 err = ENOMEM;
1154 break;
1155 }
1156 }
1157 if (ch->have_start)
1158 annotation__count_and_fill(notes, ch->start, offset, ch);
1159 if (al && ch->num_aggr) {
1160 al->cycles->avg = ch->cycles_aggr / ch->num_aggr;
1161 al->cycles->max = ch->cycles_max;
1162 al->cycles->min = ch->cycles_min;
1163 }
1164 }
1165 }
1166
1167 if (err) {
1168 while (++offset < (s64)size) {
1169 struct cyc_hist *ch = ¬es->branch->cycles_hist[offset];
1170
1171 if (ch && ch->cycles) {
1172 struct annotation_line *al = notes->offsets[offset];
1173 if (al)
1174 zfree(&al->cycles);
1175 }
1176 }
1177 }
1178
1179 annotation__unlock(notes);
1180 return 0;
1181 }
1182
addr_map_symbol__inc_samples(struct addr_map_symbol * ams,struct perf_sample * sample,struct evsel * evsel)1183 int addr_map_symbol__inc_samples(struct addr_map_symbol *ams, struct perf_sample *sample,
1184 struct evsel *evsel)
1185 {
1186 return symbol__inc_addr_samples(&ams->ms, evsel, ams->al_addr, sample);
1187 }
1188
hist_entry__inc_addr_samples(struct hist_entry * he,struct perf_sample * sample,struct evsel * evsel,u64 ip)1189 int hist_entry__inc_addr_samples(struct hist_entry *he, struct perf_sample *sample,
1190 struct evsel *evsel, u64 ip)
1191 {
1192 return symbol__inc_addr_samples(&he->ms, evsel, ip, sample);
1193 }
1194
disasm_line__init_ins(struct disasm_line * dl,struct arch * arch,struct map_symbol * ms)1195 static void disasm_line__init_ins(struct disasm_line *dl, struct arch *arch, struct map_symbol *ms)
1196 {
1197 dl->ins.ops = ins__find(arch, dl->ins.name);
1198
1199 if (!dl->ins.ops)
1200 return;
1201
1202 if (dl->ins.ops->parse && dl->ins.ops->parse(arch, &dl->ops, ms) < 0)
1203 dl->ins.ops = NULL;
1204 }
1205
disasm_line__parse(char * line,const char ** namep,char ** rawp)1206 static int disasm_line__parse(char *line, const char **namep, char **rawp)
1207 {
1208 char tmp, *name = skip_spaces(line);
1209
1210 if (name[0] == '\0')
1211 return -1;
1212
1213 *rawp = name + 1;
1214
1215 while ((*rawp)[0] != '\0' && !isspace((*rawp)[0]))
1216 ++*rawp;
1217
1218 tmp = (*rawp)[0];
1219 (*rawp)[0] = '\0';
1220 *namep = strdup(name);
1221
1222 if (*namep == NULL)
1223 goto out;
1224
1225 (*rawp)[0] = tmp;
1226 *rawp = strim(*rawp);
1227
1228 return 0;
1229
1230 out:
1231 return -1;
1232 }
1233
1234 struct annotate_args {
1235 struct arch *arch;
1236 struct map_symbol ms;
1237 struct evsel *evsel;
1238 struct annotation_options *options;
1239 s64 offset;
1240 char *line;
1241 int line_nr;
1242 char *fileloc;
1243 };
1244
annotation_line__init(struct annotation_line * al,struct annotate_args * args,int nr)1245 static void annotation_line__init(struct annotation_line *al,
1246 struct annotate_args *args,
1247 int nr)
1248 {
1249 al->offset = args->offset;
1250 al->line = strdup(args->line);
1251 al->line_nr = args->line_nr;
1252 al->fileloc = args->fileloc;
1253 al->data_nr = nr;
1254 }
1255
annotation_line__exit(struct annotation_line * al)1256 static void annotation_line__exit(struct annotation_line *al)
1257 {
1258 zfree_srcline(&al->path);
1259 zfree(&al->line);
1260 zfree(&al->cycles);
1261 }
1262
disasm_line_size(int nr)1263 static size_t disasm_line_size(int nr)
1264 {
1265 struct annotation_line *al;
1266
1267 return (sizeof(struct disasm_line) + (sizeof(al->data[0]) * nr));
1268 }
1269
1270 /*
1271 * Allocating the disasm annotation line data with
1272 * following structure:
1273 *
1274 * -------------------------------------------
1275 * struct disasm_line | struct annotation_line
1276 * -------------------------------------------
1277 *
1278 * We have 'struct annotation_line' member as last member
1279 * of 'struct disasm_line' to have an easy access.
1280 */
disasm_line__new(struct annotate_args * args)1281 static struct disasm_line *disasm_line__new(struct annotate_args *args)
1282 {
1283 struct disasm_line *dl = NULL;
1284 int nr = 1;
1285
1286 if (evsel__is_group_event(args->evsel))
1287 nr = args->evsel->core.nr_members;
1288
1289 dl = zalloc(disasm_line_size(nr));
1290 if (!dl)
1291 return NULL;
1292
1293 annotation_line__init(&dl->al, args, nr);
1294 if (dl->al.line == NULL)
1295 goto out_delete;
1296
1297 if (args->offset != -1) {
1298 if (disasm_line__parse(dl->al.line, &dl->ins.name, &dl->ops.raw) < 0)
1299 goto out_free_line;
1300
1301 disasm_line__init_ins(dl, args->arch, &args->ms);
1302 }
1303
1304 return dl;
1305
1306 out_free_line:
1307 zfree(&dl->al.line);
1308 out_delete:
1309 free(dl);
1310 return NULL;
1311 }
1312
disasm_line__free(struct disasm_line * dl)1313 void disasm_line__free(struct disasm_line *dl)
1314 {
1315 if (dl->ins.ops && dl->ins.ops->free)
1316 dl->ins.ops->free(&dl->ops);
1317 else
1318 ins__delete(&dl->ops);
1319 zfree(&dl->ins.name);
1320 annotation_line__exit(&dl->al);
1321 free(dl);
1322 }
1323
disasm_line__scnprintf(struct disasm_line * dl,char * bf,size_t size,bool raw,int max_ins_name)1324 int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size, bool raw, int max_ins_name)
1325 {
1326 if (raw || !dl->ins.ops)
1327 return scnprintf(bf, size, "%-*s %s", max_ins_name, dl->ins.name, dl->ops.raw);
1328
1329 return ins__scnprintf(&dl->ins, bf, size, &dl->ops, max_ins_name);
1330 }
1331
annotation__exit(struct annotation * notes)1332 void annotation__exit(struct annotation *notes)
1333 {
1334 annotated_source__delete(notes->src);
1335 annotated_branch__delete(notes->branch);
1336 }
1337
1338 static struct sharded_mutex *sharded_mutex;
1339
annotation__init_sharded_mutex(void)1340 static void annotation__init_sharded_mutex(void)
1341 {
1342 /* As many mutexes as there are CPUs. */
1343 sharded_mutex = sharded_mutex__new(cpu__max_present_cpu().cpu);
1344 }
1345
annotation__hash(const struct annotation * notes)1346 static size_t annotation__hash(const struct annotation *notes)
1347 {
1348 return (size_t)notes;
1349 }
1350
annotation__get_mutex(const struct annotation * notes)1351 static struct mutex *annotation__get_mutex(const struct annotation *notes)
1352 {
1353 static pthread_once_t once = PTHREAD_ONCE_INIT;
1354
1355 pthread_once(&once, annotation__init_sharded_mutex);
1356 if (!sharded_mutex)
1357 return NULL;
1358
1359 return sharded_mutex__get_mutex(sharded_mutex, annotation__hash(notes));
1360 }
1361
annotation__lock(struct annotation * notes)1362 void annotation__lock(struct annotation *notes)
1363 NO_THREAD_SAFETY_ANALYSIS
1364 {
1365 struct mutex *mutex = annotation__get_mutex(notes);
1366
1367 if (mutex)
1368 mutex_lock(mutex);
1369 }
1370
annotation__unlock(struct annotation * notes)1371 void annotation__unlock(struct annotation *notes)
1372 NO_THREAD_SAFETY_ANALYSIS
1373 {
1374 struct mutex *mutex = annotation__get_mutex(notes);
1375
1376 if (mutex)
1377 mutex_unlock(mutex);
1378 }
1379
annotation__trylock(struct annotation * notes)1380 bool annotation__trylock(struct annotation *notes)
1381 {
1382 struct mutex *mutex = annotation__get_mutex(notes);
1383
1384 if (!mutex)
1385 return false;
1386
1387 return mutex_trylock(mutex);
1388 }
1389
1390
annotation_line__add(struct annotation_line * al,struct list_head * head)1391 static void annotation_line__add(struct annotation_line *al, struct list_head *head)
1392 {
1393 list_add_tail(&al->node, head);
1394 }
1395
1396 struct annotation_line *
annotation_line__next(struct annotation_line * pos,struct list_head * head)1397 annotation_line__next(struct annotation_line *pos, struct list_head *head)
1398 {
1399 list_for_each_entry_continue(pos, head, node)
1400 if (pos->offset >= 0)
1401 return pos;
1402
1403 return NULL;
1404 }
1405
annotate__address_color(struct block_range * br)1406 static const char *annotate__address_color(struct block_range *br)
1407 {
1408 double cov = block_range__coverage(br);
1409
1410 if (cov >= 0) {
1411 /* mark red for >75% coverage */
1412 if (cov > 0.75)
1413 return PERF_COLOR_RED;
1414
1415 /* mark dull for <1% coverage */
1416 if (cov < 0.01)
1417 return PERF_COLOR_NORMAL;
1418 }
1419
1420 return PERF_COLOR_MAGENTA;
1421 }
1422
annotate__asm_color(struct block_range * br)1423 static const char *annotate__asm_color(struct block_range *br)
1424 {
1425 double cov = block_range__coverage(br);
1426
1427 if (cov >= 0) {
1428 /* mark dull for <1% coverage */
1429 if (cov < 0.01)
1430 return PERF_COLOR_NORMAL;
1431 }
1432
1433 return PERF_COLOR_BLUE;
1434 }
1435
annotate__branch_printf(struct block_range * br,u64 addr)1436 static void annotate__branch_printf(struct block_range *br, u64 addr)
1437 {
1438 bool emit_comment = true;
1439
1440 if (!br)
1441 return;
1442
1443 #if 1
1444 if (br->is_target && br->start == addr) {
1445 struct block_range *branch = br;
1446 double p;
1447
1448 /*
1449 * Find matching branch to our target.
1450 */
1451 while (!branch->is_branch)
1452 branch = block_range__next(branch);
1453
1454 p = 100 *(double)br->entry / branch->coverage;
1455
1456 if (p > 0.1) {
1457 if (emit_comment) {
1458 emit_comment = false;
1459 printf("\t#");
1460 }
1461
1462 /*
1463 * The percentage of coverage joined at this target in relation
1464 * to the next branch.
1465 */
1466 printf(" +%.2f%%", p);
1467 }
1468 }
1469 #endif
1470 if (br->is_branch && br->end == addr) {
1471 double p = 100*(double)br->taken / br->coverage;
1472
1473 if (p > 0.1) {
1474 if (emit_comment) {
1475 emit_comment = false;
1476 printf("\t#");
1477 }
1478
1479 /*
1480 * The percentage of coverage leaving at this branch, and
1481 * its prediction ratio.
1482 */
1483 printf(" -%.2f%% (p:%.2f%%)", p, 100*(double)br->pred / br->taken);
1484 }
1485 }
1486 }
1487
disasm_line__print(struct disasm_line * dl,u64 start,int addr_fmt_width)1488 static int disasm_line__print(struct disasm_line *dl, u64 start, int addr_fmt_width)
1489 {
1490 s64 offset = dl->al.offset;
1491 const u64 addr = start + offset;
1492 struct block_range *br;
1493
1494 br = block_range__find(addr);
1495 color_fprintf(stdout, annotate__address_color(br), " %*" PRIx64 ":", addr_fmt_width, addr);
1496 color_fprintf(stdout, annotate__asm_color(br), "%s", dl->al.line);
1497 annotate__branch_printf(br, addr);
1498 return 0;
1499 }
1500
1501 static int
annotation_line__print(struct annotation_line * al,struct symbol * sym,u64 start,struct evsel * evsel,u64 len,int min_pcnt,int printed,int max_lines,struct annotation_line * queue,int addr_fmt_width,int percent_type)1502 annotation_line__print(struct annotation_line *al, struct symbol *sym, u64 start,
1503 struct evsel *evsel, u64 len, int min_pcnt, int printed,
1504 int max_lines, struct annotation_line *queue, int addr_fmt_width,
1505 int percent_type)
1506 {
1507 struct disasm_line *dl = container_of(al, struct disasm_line, al);
1508 static const char *prev_line;
1509
1510 if (al->offset != -1) {
1511 double max_percent = 0.0;
1512 int i, nr_percent = 1;
1513 const char *color;
1514 struct annotation *notes = symbol__annotation(sym);
1515
1516 for (i = 0; i < al->data_nr; i++) {
1517 double percent;
1518
1519 percent = annotation_data__percent(&al->data[i],
1520 percent_type);
1521
1522 if (percent > max_percent)
1523 max_percent = percent;
1524 }
1525
1526 if (al->data_nr > nr_percent)
1527 nr_percent = al->data_nr;
1528
1529 if (max_percent < min_pcnt)
1530 return -1;
1531
1532 if (max_lines && printed >= max_lines)
1533 return 1;
1534
1535 if (queue != NULL) {
1536 list_for_each_entry_from(queue, ¬es->src->source, node) {
1537 if (queue == al)
1538 break;
1539 annotation_line__print(queue, sym, start, evsel, len,
1540 0, 0, 1, NULL, addr_fmt_width,
1541 percent_type);
1542 }
1543 }
1544
1545 color = get_percent_color(max_percent);
1546
1547 for (i = 0; i < nr_percent; i++) {
1548 struct annotation_data *data = &al->data[i];
1549 double percent;
1550
1551 percent = annotation_data__percent(data, percent_type);
1552 color = get_percent_color(percent);
1553
1554 if (symbol_conf.show_total_period)
1555 color_fprintf(stdout, color, " %11" PRIu64,
1556 data->he.period);
1557 else if (symbol_conf.show_nr_samples)
1558 color_fprintf(stdout, color, " %7" PRIu64,
1559 data->he.nr_samples);
1560 else
1561 color_fprintf(stdout, color, " %7.2f", percent);
1562 }
1563
1564 printf(" : ");
1565
1566 disasm_line__print(dl, start, addr_fmt_width);
1567
1568 /*
1569 * Also color the filename and line if needed, with
1570 * the same color than the percentage. Don't print it
1571 * twice for close colored addr with the same filename:line
1572 */
1573 if (al->path) {
1574 if (!prev_line || strcmp(prev_line, al->path)) {
1575 color_fprintf(stdout, color, " // %s", al->path);
1576 prev_line = al->path;
1577 }
1578 }
1579
1580 printf("\n");
1581 } else if (max_lines && printed >= max_lines)
1582 return 1;
1583 else {
1584 int width = symbol_conf.show_total_period ? 12 : 8;
1585
1586 if (queue)
1587 return -1;
1588
1589 if (evsel__is_group_event(evsel))
1590 width *= evsel->core.nr_members;
1591
1592 if (!*al->line)
1593 printf(" %*s:\n", width, " ");
1594 else
1595 printf(" %*s: %-*d %s\n", width, " ", addr_fmt_width, al->line_nr, al->line);
1596 }
1597
1598 return 0;
1599 }
1600
1601 /*
1602 * symbol__parse_objdump_line() parses objdump output (with -d --no-show-raw)
1603 * which looks like following
1604 *
1605 * 0000000000415500 <_init>:
1606 * 415500: sub $0x8,%rsp
1607 * 415504: mov 0x2f5ad5(%rip),%rax # 70afe0 <_DYNAMIC+0x2f8>
1608 * 41550b: test %rax,%rax
1609 * 41550e: je 415515 <_init+0x15>
1610 * 415510: callq 416e70 <__gmon_start__@plt>
1611 * 415515: add $0x8,%rsp
1612 * 415519: retq
1613 *
1614 * it will be parsed and saved into struct disasm_line as
1615 * <offset> <name> <ops.raw>
1616 *
1617 * The offset will be a relative offset from the start of the symbol and -1
1618 * means that it's not a disassembly line so should be treated differently.
1619 * The ops.raw part will be parsed further according to type of the instruction.
1620 */
symbol__parse_objdump_line(struct symbol * sym,struct annotate_args * args,char * parsed_line,int * line_nr,char ** fileloc)1621 static int symbol__parse_objdump_line(struct symbol *sym,
1622 struct annotate_args *args,
1623 char *parsed_line, int *line_nr, char **fileloc)
1624 {
1625 struct map *map = args->ms.map;
1626 struct annotation *notes = symbol__annotation(sym);
1627 struct disasm_line *dl;
1628 char *tmp;
1629 s64 line_ip, offset = -1;
1630 regmatch_t match[2];
1631
1632 /* /filename:linenr ? Save line number and ignore. */
1633 if (regexec(&file_lineno, parsed_line, 2, match, 0) == 0) {
1634 *line_nr = atoi(parsed_line + match[1].rm_so);
1635 free(*fileloc);
1636 *fileloc = strdup(parsed_line);
1637 return 0;
1638 }
1639
1640 /* Process hex address followed by ':'. */
1641 line_ip = strtoull(parsed_line, &tmp, 16);
1642 if (parsed_line != tmp && tmp[0] == ':' && tmp[1] != '\0') {
1643 u64 start = map__rip_2objdump(map, sym->start),
1644 end = map__rip_2objdump(map, sym->end);
1645
1646 offset = line_ip - start;
1647 if ((u64)line_ip < start || (u64)line_ip >= end)
1648 offset = -1;
1649 else
1650 parsed_line = tmp + 1;
1651 }
1652
1653 args->offset = offset;
1654 args->line = parsed_line;
1655 args->line_nr = *line_nr;
1656 args->fileloc = *fileloc;
1657 args->ms.sym = sym;
1658
1659 dl = disasm_line__new(args);
1660 (*line_nr)++;
1661
1662 if (dl == NULL)
1663 return -1;
1664
1665 if (!disasm_line__has_local_offset(dl)) {
1666 dl->ops.target.offset = dl->ops.target.addr -
1667 map__rip_2objdump(map, sym->start);
1668 dl->ops.target.offset_avail = true;
1669 }
1670
1671 /* kcore has no symbols, so add the call target symbol */
1672 if (dl->ins.ops && ins__is_call(&dl->ins) && !dl->ops.target.sym) {
1673 struct addr_map_symbol target = {
1674 .addr = dl->ops.target.addr,
1675 .ms = { .map = map, },
1676 };
1677
1678 if (!maps__find_ams(args->ms.maps, &target) &&
1679 target.ms.sym->start == target.al_addr)
1680 dl->ops.target.sym = target.ms.sym;
1681 }
1682
1683 annotation_line__add(&dl->al, ¬es->src->source);
1684 return 0;
1685 }
1686
symbol__init_regexpr(void)1687 static __attribute__((constructor)) void symbol__init_regexpr(void)
1688 {
1689 regcomp(&file_lineno, "^/[^:]+:([0-9]+)", REG_EXTENDED);
1690 }
1691
delete_last_nop(struct symbol * sym)1692 static void delete_last_nop(struct symbol *sym)
1693 {
1694 struct annotation *notes = symbol__annotation(sym);
1695 struct list_head *list = ¬es->src->source;
1696 struct disasm_line *dl;
1697
1698 while (!list_empty(list)) {
1699 dl = list_entry(list->prev, struct disasm_line, al.node);
1700
1701 if (dl->ins.ops) {
1702 if (dl->ins.ops != &nop_ops)
1703 return;
1704 } else {
1705 if (!strstr(dl->al.line, " nop ") &&
1706 !strstr(dl->al.line, " nopl ") &&
1707 !strstr(dl->al.line, " nopw "))
1708 return;
1709 }
1710
1711 list_del_init(&dl->al.node);
1712 disasm_line__free(dl);
1713 }
1714 }
1715
symbol__strerror_disassemble(struct map_symbol * ms,int errnum,char * buf,size_t buflen)1716 int symbol__strerror_disassemble(struct map_symbol *ms, int errnum, char *buf, size_t buflen)
1717 {
1718 struct dso *dso = map__dso(ms->map);
1719
1720 BUG_ON(buflen == 0);
1721
1722 if (errnum >= 0) {
1723 str_error_r(errnum, buf, buflen);
1724 return 0;
1725 }
1726
1727 switch (errnum) {
1728 case SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX: {
1729 char bf[SBUILD_ID_SIZE + 15] = " with build id ";
1730 char *build_id_msg = NULL;
1731
1732 if (dso->has_build_id) {
1733 build_id__sprintf(&dso->bid, bf + 15);
1734 build_id_msg = bf;
1735 }
1736 scnprintf(buf, buflen,
1737 "No vmlinux file%s\nwas found in the path.\n\n"
1738 "Note that annotation using /proc/kcore requires CAP_SYS_RAWIO capability.\n\n"
1739 "Please use:\n\n"
1740 " perf buildid-cache -vu vmlinux\n\n"
1741 "or:\n\n"
1742 " --vmlinux vmlinux\n", build_id_msg ?: "");
1743 }
1744 break;
1745 case SYMBOL_ANNOTATE_ERRNO__NO_LIBOPCODES_FOR_BPF:
1746 scnprintf(buf, buflen, "Please link with binutils's libopcode to enable BPF annotation");
1747 break;
1748 case SYMBOL_ANNOTATE_ERRNO__ARCH_INIT_REGEXP:
1749 scnprintf(buf, buflen, "Problems with arch specific instruction name regular expressions.");
1750 break;
1751 case SYMBOL_ANNOTATE_ERRNO__ARCH_INIT_CPUID_PARSING:
1752 scnprintf(buf, buflen, "Problems while parsing the CPUID in the arch specific initialization.");
1753 break;
1754 case SYMBOL_ANNOTATE_ERRNO__BPF_INVALID_FILE:
1755 scnprintf(buf, buflen, "Invalid BPF file: %s.", dso->long_name);
1756 break;
1757 case SYMBOL_ANNOTATE_ERRNO__BPF_MISSING_BTF:
1758 scnprintf(buf, buflen, "The %s BPF file has no BTF section, compile with -g or use pahole -J.",
1759 dso->long_name);
1760 break;
1761 default:
1762 scnprintf(buf, buflen, "Internal error: Invalid %d error code\n", errnum);
1763 break;
1764 }
1765
1766 return 0;
1767 }
1768
dso__disassemble_filename(struct dso * dso,char * filename,size_t filename_size)1769 static int dso__disassemble_filename(struct dso *dso, char *filename, size_t filename_size)
1770 {
1771 char linkname[PATH_MAX];
1772 char *build_id_filename;
1773 char *build_id_path = NULL;
1774 char *pos;
1775 int len;
1776
1777 if (dso->symtab_type == DSO_BINARY_TYPE__KALLSYMS &&
1778 !dso__is_kcore(dso))
1779 return SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX;
1780
1781 build_id_filename = dso__build_id_filename(dso, NULL, 0, false);
1782 if (build_id_filename) {
1783 __symbol__join_symfs(filename, filename_size, build_id_filename);
1784 free(build_id_filename);
1785 } else {
1786 if (dso->has_build_id)
1787 return ENOMEM;
1788 goto fallback;
1789 }
1790
1791 build_id_path = strdup(filename);
1792 if (!build_id_path)
1793 return ENOMEM;
1794
1795 /*
1796 * old style build-id cache has name of XX/XXXXXXX.. while
1797 * new style has XX/XXXXXXX../{elf,kallsyms,vdso}.
1798 * extract the build-id part of dirname in the new style only.
1799 */
1800 pos = strrchr(build_id_path, '/');
1801 if (pos && strlen(pos) < SBUILD_ID_SIZE - 2)
1802 dirname(build_id_path);
1803
1804 if (dso__is_kcore(dso))
1805 goto fallback;
1806
1807 len = readlink(build_id_path, linkname, sizeof(linkname) - 1);
1808 if (len < 0)
1809 goto fallback;
1810
1811 linkname[len] = '\0';
1812 if (strstr(linkname, DSO__NAME_KALLSYMS) ||
1813 access(filename, R_OK)) {
1814 fallback:
1815 /*
1816 * If we don't have build-ids or the build-id file isn't in the
1817 * cache, or is just a kallsyms file, well, lets hope that this
1818 * DSO is the same as when 'perf record' ran.
1819 */
1820 if (dso->kernel && dso->long_name[0] == '/')
1821 snprintf(filename, filename_size, "%s", dso->long_name);
1822 else
1823 __symbol__join_symfs(filename, filename_size, dso->long_name);
1824
1825 mutex_lock(&dso->lock);
1826 if (access(filename, R_OK) && errno == ENOENT && dso->nsinfo) {
1827 char *new_name = dso__filename_with_chroot(dso, filename);
1828 if (new_name) {
1829 strlcpy(filename, new_name, filename_size);
1830 free(new_name);
1831 }
1832 }
1833 mutex_unlock(&dso->lock);
1834 }
1835
1836 free(build_id_path);
1837 return 0;
1838 }
1839
1840 #if defined(HAVE_LIBBFD_SUPPORT) && defined(HAVE_LIBBPF_SUPPORT)
1841 #define PACKAGE "perf"
1842 #include <bfd.h>
1843 #include <dis-asm.h>
1844 #include <bpf/bpf.h>
1845 #include <bpf/btf.h>
1846 #include <bpf/libbpf.h>
1847 #include <linux/btf.h>
1848 #include <tools/dis-asm-compat.h>
1849
symbol__disassemble_bpf(struct symbol * sym,struct annotate_args * args)1850 static int symbol__disassemble_bpf(struct symbol *sym,
1851 struct annotate_args *args)
1852 {
1853 struct annotation *notes = symbol__annotation(sym);
1854 struct bpf_prog_linfo *prog_linfo = NULL;
1855 struct bpf_prog_info_node *info_node;
1856 int len = sym->end - sym->start;
1857 disassembler_ftype disassemble;
1858 struct map *map = args->ms.map;
1859 struct perf_bpil *info_linear;
1860 struct disassemble_info info;
1861 struct dso *dso = map__dso(map);
1862 int pc = 0, count, sub_id;
1863 struct btf *btf = NULL;
1864 char tpath[PATH_MAX];
1865 size_t buf_size;
1866 int nr_skip = 0;
1867 char *buf;
1868 bfd *bfdf;
1869 int ret;
1870 FILE *s;
1871
1872 if (dso->binary_type != DSO_BINARY_TYPE__BPF_PROG_INFO)
1873 return SYMBOL_ANNOTATE_ERRNO__BPF_INVALID_FILE;
1874
1875 pr_debug("%s: handling sym %s addr %" PRIx64 " len %" PRIx64 "\n", __func__,
1876 sym->name, sym->start, sym->end - sym->start);
1877
1878 memset(tpath, 0, sizeof(tpath));
1879 perf_exe(tpath, sizeof(tpath));
1880
1881 bfdf = bfd_openr(tpath, NULL);
1882 if (bfdf == NULL)
1883 abort();
1884
1885 if (!bfd_check_format(bfdf, bfd_object))
1886 abort();
1887
1888 s = open_memstream(&buf, &buf_size);
1889 if (!s) {
1890 ret = errno;
1891 goto out;
1892 }
1893 init_disassemble_info_compat(&info, s,
1894 (fprintf_ftype) fprintf,
1895 fprintf_styled);
1896 info.arch = bfd_get_arch(bfdf);
1897 info.mach = bfd_get_mach(bfdf);
1898
1899 info_node = perf_env__find_bpf_prog_info(dso->bpf_prog.env,
1900 dso->bpf_prog.id);
1901 if (!info_node) {
1902 ret = SYMBOL_ANNOTATE_ERRNO__BPF_MISSING_BTF;
1903 goto out;
1904 }
1905 info_linear = info_node->info_linear;
1906 sub_id = dso->bpf_prog.sub_id;
1907
1908 info.buffer = (void *)(uintptr_t)(info_linear->info.jited_prog_insns);
1909 info.buffer_length = info_linear->info.jited_prog_len;
1910
1911 if (info_linear->info.nr_line_info)
1912 prog_linfo = bpf_prog_linfo__new(&info_linear->info);
1913
1914 if (info_linear->info.btf_id) {
1915 struct btf_node *node;
1916
1917 node = perf_env__find_btf(dso->bpf_prog.env,
1918 info_linear->info.btf_id);
1919 if (node)
1920 btf = btf__new((__u8 *)(node->data),
1921 node->data_size);
1922 }
1923
1924 disassemble_init_for_target(&info);
1925
1926 #ifdef DISASM_FOUR_ARGS_SIGNATURE
1927 disassemble = disassembler(info.arch,
1928 bfd_big_endian(bfdf),
1929 info.mach,
1930 bfdf);
1931 #else
1932 disassemble = disassembler(bfdf);
1933 #endif
1934 if (disassemble == NULL)
1935 abort();
1936
1937 fflush(s);
1938 do {
1939 const struct bpf_line_info *linfo = NULL;
1940 struct disasm_line *dl;
1941 size_t prev_buf_size;
1942 const char *srcline;
1943 u64 addr;
1944
1945 addr = pc + ((u64 *)(uintptr_t)(info_linear->info.jited_ksyms))[sub_id];
1946 count = disassemble(pc, &info);
1947
1948 if (prog_linfo)
1949 linfo = bpf_prog_linfo__lfind_addr_func(prog_linfo,
1950 addr, sub_id,
1951 nr_skip);
1952
1953 if (linfo && btf) {
1954 srcline = btf__name_by_offset(btf, linfo->line_off);
1955 nr_skip++;
1956 } else
1957 srcline = NULL;
1958
1959 fprintf(s, "\n");
1960 prev_buf_size = buf_size;
1961 fflush(s);
1962
1963 if (!annotate_opts.hide_src_code && srcline) {
1964 args->offset = -1;
1965 args->line = strdup(srcline);
1966 args->line_nr = 0;
1967 args->fileloc = NULL;
1968 args->ms.sym = sym;
1969 dl = disasm_line__new(args);
1970 if (dl) {
1971 annotation_line__add(&dl->al,
1972 ¬es->src->source);
1973 }
1974 }
1975
1976 args->offset = pc;
1977 args->line = buf + prev_buf_size;
1978 args->line_nr = 0;
1979 args->fileloc = NULL;
1980 args->ms.sym = sym;
1981 dl = disasm_line__new(args);
1982 if (dl)
1983 annotation_line__add(&dl->al, ¬es->src->source);
1984
1985 pc += count;
1986 } while (count > 0 && pc < len);
1987
1988 ret = 0;
1989 out:
1990 free(prog_linfo);
1991 btf__free(btf);
1992 fclose(s);
1993 bfd_close(bfdf);
1994 return ret;
1995 }
1996 #else // defined(HAVE_LIBBFD_SUPPORT) && defined(HAVE_LIBBPF_SUPPORT)
symbol__disassemble_bpf(struct symbol * sym __maybe_unused,struct annotate_args * args __maybe_unused)1997 static int symbol__disassemble_bpf(struct symbol *sym __maybe_unused,
1998 struct annotate_args *args __maybe_unused)
1999 {
2000 return SYMBOL_ANNOTATE_ERRNO__NO_LIBOPCODES_FOR_BPF;
2001 }
2002 #endif // defined(HAVE_LIBBFD_SUPPORT) && defined(HAVE_LIBBPF_SUPPORT)
2003
2004 static int
symbol__disassemble_bpf_image(struct symbol * sym,struct annotate_args * args)2005 symbol__disassemble_bpf_image(struct symbol *sym,
2006 struct annotate_args *args)
2007 {
2008 struct annotation *notes = symbol__annotation(sym);
2009 struct disasm_line *dl;
2010
2011 args->offset = -1;
2012 args->line = strdup("to be implemented");
2013 args->line_nr = 0;
2014 args->fileloc = NULL;
2015 dl = disasm_line__new(args);
2016 if (dl)
2017 annotation_line__add(&dl->al, ¬es->src->source);
2018
2019 zfree(&args->line);
2020 return 0;
2021 }
2022
2023 /*
2024 * Possibly create a new version of line with tabs expanded. Returns the
2025 * existing or new line, storage is updated if a new line is allocated. If
2026 * allocation fails then NULL is returned.
2027 */
expand_tabs(char * line,char ** storage,size_t * storage_len)2028 static char *expand_tabs(char *line, char **storage, size_t *storage_len)
2029 {
2030 size_t i, src, dst, len, new_storage_len, num_tabs;
2031 char *new_line;
2032 size_t line_len = strlen(line);
2033
2034 for (num_tabs = 0, i = 0; i < line_len; i++)
2035 if (line[i] == '\t')
2036 num_tabs++;
2037
2038 if (num_tabs == 0)
2039 return line;
2040
2041 /*
2042 * Space for the line and '\0', less the leading and trailing
2043 * spaces. Each tab may introduce 7 additional spaces.
2044 */
2045 new_storage_len = line_len + 1 + (num_tabs * 7);
2046
2047 new_line = malloc(new_storage_len);
2048 if (new_line == NULL) {
2049 pr_err("Failure allocating memory for tab expansion\n");
2050 return NULL;
2051 }
2052
2053 /*
2054 * Copy regions starting at src and expand tabs. If there are two
2055 * adjacent tabs then 'src == i', the memcpy is of size 0 and the spaces
2056 * are inserted.
2057 */
2058 for (i = 0, src = 0, dst = 0; i < line_len && num_tabs; i++) {
2059 if (line[i] == '\t') {
2060 len = i - src;
2061 memcpy(&new_line[dst], &line[src], len);
2062 dst += len;
2063 new_line[dst++] = ' ';
2064 while (dst % 8 != 0)
2065 new_line[dst++] = ' ';
2066 src = i + 1;
2067 num_tabs--;
2068 }
2069 }
2070
2071 /* Expand the last region. */
2072 len = line_len - src;
2073 memcpy(&new_line[dst], &line[src], len);
2074 dst += len;
2075 new_line[dst] = '\0';
2076
2077 free(*storage);
2078 *storage = new_line;
2079 *storage_len = new_storage_len;
2080 return new_line;
2081
2082 }
2083
symbol__disassemble(struct symbol * sym,struct annotate_args * args)2084 static int symbol__disassemble(struct symbol *sym, struct annotate_args *args)
2085 {
2086 struct annotation_options *opts = &annotate_opts;
2087 struct map *map = args->ms.map;
2088 struct dso *dso = map__dso(map);
2089 char *command;
2090 FILE *file;
2091 char symfs_filename[PATH_MAX];
2092 struct kcore_extract kce;
2093 bool delete_extract = false;
2094 bool decomp = false;
2095 int lineno = 0;
2096 char *fileloc = NULL;
2097 int nline;
2098 char *line;
2099 size_t line_len;
2100 const char *objdump_argv[] = {
2101 "/bin/sh",
2102 "-c",
2103 NULL, /* Will be the objdump command to run. */
2104 "--",
2105 NULL, /* Will be the symfs path. */
2106 NULL,
2107 };
2108 struct child_process objdump_process;
2109 int err = dso__disassemble_filename(dso, symfs_filename, sizeof(symfs_filename));
2110
2111 if (err)
2112 return err;
2113
2114 pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__,
2115 symfs_filename, sym->name, map__unmap_ip(map, sym->start),
2116 map__unmap_ip(map, sym->end));
2117
2118 pr_debug("annotating [%p] %30s : [%p] %30s\n",
2119 dso, dso->long_name, sym, sym->name);
2120
2121 if (dso->binary_type == DSO_BINARY_TYPE__BPF_PROG_INFO) {
2122 return symbol__disassemble_bpf(sym, args);
2123 } else if (dso->binary_type == DSO_BINARY_TYPE__BPF_IMAGE) {
2124 return symbol__disassemble_bpf_image(sym, args);
2125 } else if (dso__is_kcore(dso)) {
2126 kce.kcore_filename = symfs_filename;
2127 kce.addr = map__rip_2objdump(map, sym->start);
2128 kce.offs = sym->start;
2129 kce.len = sym->end - sym->start;
2130 if (!kcore_extract__create(&kce)) {
2131 delete_extract = true;
2132 strlcpy(symfs_filename, kce.extract_filename,
2133 sizeof(symfs_filename));
2134 }
2135 } else if (dso__needs_decompress(dso)) {
2136 char tmp[KMOD_DECOMP_LEN];
2137
2138 if (dso__decompress_kmodule_path(dso, symfs_filename,
2139 tmp, sizeof(tmp)) < 0)
2140 return -1;
2141
2142 decomp = true;
2143 strcpy(symfs_filename, tmp);
2144 }
2145
2146 err = asprintf(&command,
2147 "%s %s%s --start-address=0x%016" PRIx64
2148 " --stop-address=0x%016" PRIx64
2149 " -l -d %s %s %s %c%s%c %s%s -C \"$1\"",
2150 opts->objdump_path ?: "objdump",
2151 opts->disassembler_style ? "-M " : "",
2152 opts->disassembler_style ?: "",
2153 map__rip_2objdump(map, sym->start),
2154 map__rip_2objdump(map, sym->end),
2155 opts->show_asm_raw ? "" : "--no-show-raw-insn",
2156 opts->annotate_src ? "-S" : "",
2157 opts->prefix ? "--prefix " : "",
2158 opts->prefix ? '"' : ' ',
2159 opts->prefix ?: "",
2160 opts->prefix ? '"' : ' ',
2161 opts->prefix_strip ? "--prefix-strip=" : "",
2162 opts->prefix_strip ?: "");
2163
2164 if (err < 0) {
2165 pr_err("Failure allocating memory for the command to run\n");
2166 goto out_remove_tmp;
2167 }
2168
2169 pr_debug("Executing: %s\n", command);
2170
2171 objdump_argv[2] = command;
2172 objdump_argv[4] = symfs_filename;
2173
2174 /* Create a pipe to read from for stdout */
2175 memset(&objdump_process, 0, sizeof(objdump_process));
2176 objdump_process.argv = objdump_argv;
2177 objdump_process.out = -1;
2178 objdump_process.err = -1;
2179 objdump_process.no_stderr = 1;
2180 if (start_command(&objdump_process)) {
2181 pr_err("Failure starting to run %s\n", command);
2182 err = -1;
2183 goto out_free_command;
2184 }
2185
2186 file = fdopen(objdump_process.out, "r");
2187 if (!file) {
2188 pr_err("Failure creating FILE stream for %s\n", command);
2189 /*
2190 * If we were using debug info should retry with
2191 * original binary.
2192 */
2193 err = -1;
2194 goto out_close_stdout;
2195 }
2196
2197 /* Storage for getline. */
2198 line = NULL;
2199 line_len = 0;
2200
2201 nline = 0;
2202 while (!feof(file)) {
2203 const char *match;
2204 char *expanded_line;
2205
2206 if (getline(&line, &line_len, file) < 0 || !line)
2207 break;
2208
2209 /* Skip lines containing "filename:" */
2210 match = strstr(line, symfs_filename);
2211 if (match && match[strlen(symfs_filename)] == ':')
2212 continue;
2213
2214 expanded_line = strim(line);
2215 expanded_line = expand_tabs(expanded_line, &line, &line_len);
2216 if (!expanded_line)
2217 break;
2218
2219 /*
2220 * The source code line number (lineno) needs to be kept in
2221 * across calls to symbol__parse_objdump_line(), so that it
2222 * can associate it with the instructions till the next one.
2223 * See disasm_line__new() and struct disasm_line::line_nr.
2224 */
2225 if (symbol__parse_objdump_line(sym, args, expanded_line,
2226 &lineno, &fileloc) < 0)
2227 break;
2228 nline++;
2229 }
2230 free(line);
2231 free(fileloc);
2232
2233 err = finish_command(&objdump_process);
2234 if (err)
2235 pr_err("Error running %s\n", command);
2236
2237 if (nline == 0) {
2238 err = -1;
2239 pr_err("No output from %s\n", command);
2240 }
2241
2242 /*
2243 * kallsyms does not have symbol sizes so there may a nop at the end.
2244 * Remove it.
2245 */
2246 if (dso__is_kcore(dso))
2247 delete_last_nop(sym);
2248
2249 fclose(file);
2250
2251 out_close_stdout:
2252 close(objdump_process.out);
2253
2254 out_free_command:
2255 free(command);
2256
2257 out_remove_tmp:
2258 if (decomp)
2259 unlink(symfs_filename);
2260
2261 if (delete_extract)
2262 kcore_extract__delete(&kce);
2263
2264 return err;
2265 }
2266
calc_percent(struct sym_hist * sym_hist,struct hists * hists,struct annotation_data * data,s64 offset,s64 end)2267 static void calc_percent(struct sym_hist *sym_hist,
2268 struct hists *hists,
2269 struct annotation_data *data,
2270 s64 offset, s64 end)
2271 {
2272 unsigned int hits = 0;
2273 u64 period = 0;
2274
2275 while (offset < end) {
2276 hits += sym_hist->addr[offset].nr_samples;
2277 period += sym_hist->addr[offset].period;
2278 ++offset;
2279 }
2280
2281 if (sym_hist->nr_samples) {
2282 data->he.period = period;
2283 data->he.nr_samples = hits;
2284 data->percent[PERCENT_HITS_LOCAL] = 100.0 * hits / sym_hist->nr_samples;
2285 }
2286
2287 if (hists->stats.nr_non_filtered_samples)
2288 data->percent[PERCENT_HITS_GLOBAL] = 100.0 * hits / hists->stats.nr_non_filtered_samples;
2289
2290 if (sym_hist->period)
2291 data->percent[PERCENT_PERIOD_LOCAL] = 100.0 * period / sym_hist->period;
2292
2293 if (hists->stats.total_period)
2294 data->percent[PERCENT_PERIOD_GLOBAL] = 100.0 * period / hists->stats.total_period;
2295 }
2296
annotation__calc_percent(struct annotation * notes,struct evsel * leader,s64 len)2297 static void annotation__calc_percent(struct annotation *notes,
2298 struct evsel *leader, s64 len)
2299 {
2300 struct annotation_line *al, *next;
2301 struct evsel *evsel;
2302
2303 list_for_each_entry(al, ¬es->src->source, node) {
2304 s64 end;
2305 int i = 0;
2306
2307 if (al->offset == -1)
2308 continue;
2309
2310 next = annotation_line__next(al, ¬es->src->source);
2311 end = next ? next->offset : len;
2312
2313 for_each_group_evsel(evsel, leader) {
2314 struct hists *hists = evsel__hists(evsel);
2315 struct annotation_data *data;
2316 struct sym_hist *sym_hist;
2317
2318 BUG_ON(i >= al->data_nr);
2319
2320 sym_hist = annotation__histogram(notes, evsel->core.idx);
2321 data = &al->data[i++];
2322
2323 calc_percent(sym_hist, hists, data, al->offset, end);
2324 }
2325 }
2326 }
2327
symbol__calc_percent(struct symbol * sym,struct evsel * evsel)2328 void symbol__calc_percent(struct symbol *sym, struct evsel *evsel)
2329 {
2330 struct annotation *notes = symbol__annotation(sym);
2331
2332 annotation__calc_percent(notes, evsel, symbol__size(sym));
2333 }
2334
symbol__annotate(struct map_symbol * ms,struct evsel * evsel,struct arch ** parch)2335 int symbol__annotate(struct map_symbol *ms, struct evsel *evsel,
2336 struct arch **parch)
2337 {
2338 struct symbol *sym = ms->sym;
2339 struct annotation *notes = symbol__annotation(sym);
2340 struct annotate_args args = {
2341 .evsel = evsel,
2342 .options = &annotate_opts,
2343 };
2344 struct perf_env *env = evsel__env(evsel);
2345 const char *arch_name = perf_env__arch(env);
2346 struct arch *arch;
2347 int err;
2348
2349 if (!arch_name)
2350 return errno;
2351
2352 args.arch = arch = arch__find(arch_name);
2353 if (arch == NULL) {
2354 pr_err("%s: unsupported arch %s\n", __func__, arch_name);
2355 return ENOTSUP;
2356 }
2357
2358 if (parch)
2359 *parch = arch;
2360
2361 if (arch->init) {
2362 err = arch->init(arch, env ? env->cpuid : NULL);
2363 if (err) {
2364 pr_err("%s: failed to initialize %s arch priv area\n", __func__, arch->name);
2365 return err;
2366 }
2367 }
2368
2369 args.ms = *ms;
2370 if (annotate_opts.full_addr)
2371 notes->start = map__objdump_2mem(ms->map, ms->sym->start);
2372 else
2373 notes->start = map__rip_2objdump(ms->map, ms->sym->start);
2374
2375 return symbol__disassemble(sym, &args);
2376 }
2377
insert_source_line(struct rb_root * root,struct annotation_line * al)2378 static void insert_source_line(struct rb_root *root, struct annotation_line *al)
2379 {
2380 struct annotation_line *iter;
2381 struct rb_node **p = &root->rb_node;
2382 struct rb_node *parent = NULL;
2383 unsigned int percent_type = annotate_opts.percent_type;
2384 int i, ret;
2385
2386 while (*p != NULL) {
2387 parent = *p;
2388 iter = rb_entry(parent, struct annotation_line, rb_node);
2389
2390 ret = strcmp(iter->path, al->path);
2391 if (ret == 0) {
2392 for (i = 0; i < al->data_nr; i++) {
2393 iter->data[i].percent_sum += annotation_data__percent(&al->data[i],
2394 percent_type);
2395 }
2396 return;
2397 }
2398
2399 if (ret < 0)
2400 p = &(*p)->rb_left;
2401 else
2402 p = &(*p)->rb_right;
2403 }
2404
2405 for (i = 0; i < al->data_nr; i++) {
2406 al->data[i].percent_sum = annotation_data__percent(&al->data[i],
2407 percent_type);
2408 }
2409
2410 rb_link_node(&al->rb_node, parent, p);
2411 rb_insert_color(&al->rb_node, root);
2412 }
2413
cmp_source_line(struct annotation_line * a,struct annotation_line * b)2414 static int cmp_source_line(struct annotation_line *a, struct annotation_line *b)
2415 {
2416 int i;
2417
2418 for (i = 0; i < a->data_nr; i++) {
2419 if (a->data[i].percent_sum == b->data[i].percent_sum)
2420 continue;
2421 return a->data[i].percent_sum > b->data[i].percent_sum;
2422 }
2423
2424 return 0;
2425 }
2426
__resort_source_line(struct rb_root * root,struct annotation_line * al)2427 static void __resort_source_line(struct rb_root *root, struct annotation_line *al)
2428 {
2429 struct annotation_line *iter;
2430 struct rb_node **p = &root->rb_node;
2431 struct rb_node *parent = NULL;
2432
2433 while (*p != NULL) {
2434 parent = *p;
2435 iter = rb_entry(parent, struct annotation_line, rb_node);
2436
2437 if (cmp_source_line(al, iter))
2438 p = &(*p)->rb_left;
2439 else
2440 p = &(*p)->rb_right;
2441 }
2442
2443 rb_link_node(&al->rb_node, parent, p);
2444 rb_insert_color(&al->rb_node, root);
2445 }
2446
resort_source_line(struct rb_root * dest_root,struct rb_root * src_root)2447 static void resort_source_line(struct rb_root *dest_root, struct rb_root *src_root)
2448 {
2449 struct annotation_line *al;
2450 struct rb_node *node;
2451
2452 node = rb_first(src_root);
2453 while (node) {
2454 struct rb_node *next;
2455
2456 al = rb_entry(node, struct annotation_line, rb_node);
2457 next = rb_next(node);
2458 rb_erase(node, src_root);
2459
2460 __resort_source_line(dest_root, al);
2461 node = next;
2462 }
2463 }
2464
print_summary(struct rb_root * root,const char * filename)2465 static void print_summary(struct rb_root *root, const char *filename)
2466 {
2467 struct annotation_line *al;
2468 struct rb_node *node;
2469
2470 printf("\nSorted summary for file %s\n", filename);
2471 printf("----------------------------------------------\n\n");
2472
2473 if (RB_EMPTY_ROOT(root)) {
2474 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
2475 return;
2476 }
2477
2478 node = rb_first(root);
2479 while (node) {
2480 double percent, percent_max = 0.0;
2481 const char *color;
2482 char *path;
2483 int i;
2484
2485 al = rb_entry(node, struct annotation_line, rb_node);
2486 for (i = 0; i < al->data_nr; i++) {
2487 percent = al->data[i].percent_sum;
2488 color = get_percent_color(percent);
2489 color_fprintf(stdout, color, " %7.2f", percent);
2490
2491 if (percent > percent_max)
2492 percent_max = percent;
2493 }
2494
2495 path = al->path;
2496 color = get_percent_color(percent_max);
2497 color_fprintf(stdout, color, " %s\n", path);
2498
2499 node = rb_next(node);
2500 }
2501 }
2502
symbol__annotate_hits(struct symbol * sym,struct evsel * evsel)2503 static void symbol__annotate_hits(struct symbol *sym, struct evsel *evsel)
2504 {
2505 struct annotation *notes = symbol__annotation(sym);
2506 struct sym_hist *h = annotation__histogram(notes, evsel->core.idx);
2507 u64 len = symbol__size(sym), offset;
2508
2509 for (offset = 0; offset < len; ++offset)
2510 if (h->addr[offset].nr_samples != 0)
2511 printf("%*" PRIx64 ": %" PRIu64 "\n", BITS_PER_LONG / 2,
2512 sym->start + offset, h->addr[offset].nr_samples);
2513 printf("%*s: %" PRIu64 "\n", BITS_PER_LONG / 2, "h->nr_samples", h->nr_samples);
2514 }
2515
annotated_source__addr_fmt_width(struct list_head * lines,u64 start)2516 static int annotated_source__addr_fmt_width(struct list_head *lines, u64 start)
2517 {
2518 char bf[32];
2519 struct annotation_line *line;
2520
2521 list_for_each_entry_reverse(line, lines, node) {
2522 if (line->offset != -1)
2523 return scnprintf(bf, sizeof(bf), "%" PRIx64, start + line->offset);
2524 }
2525
2526 return 0;
2527 }
2528
symbol__annotate_printf(struct map_symbol * ms,struct evsel * evsel)2529 int symbol__annotate_printf(struct map_symbol *ms, struct evsel *evsel)
2530 {
2531 struct map *map = ms->map;
2532 struct symbol *sym = ms->sym;
2533 struct dso *dso = map__dso(map);
2534 char *filename;
2535 const char *d_filename;
2536 const char *evsel_name = evsel__name(evsel);
2537 struct annotation *notes = symbol__annotation(sym);
2538 struct sym_hist *h = annotation__histogram(notes, evsel->core.idx);
2539 struct annotation_line *pos, *queue = NULL;
2540 struct annotation_options *opts = &annotate_opts;
2541 u64 start = map__rip_2objdump(map, sym->start);
2542 int printed = 2, queue_len = 0, addr_fmt_width;
2543 int more = 0;
2544 bool context = opts->context;
2545 u64 len;
2546 int width = symbol_conf.show_total_period ? 12 : 8;
2547 int graph_dotted_len;
2548 char buf[512];
2549
2550 filename = strdup(dso->long_name);
2551 if (!filename)
2552 return -ENOMEM;
2553
2554 if (opts->full_path)
2555 d_filename = filename;
2556 else
2557 d_filename = basename(filename);
2558
2559 len = symbol__size(sym);
2560
2561 if (evsel__is_group_event(evsel)) {
2562 width *= evsel->core.nr_members;
2563 evsel__group_desc(evsel, buf, sizeof(buf));
2564 evsel_name = buf;
2565 }
2566
2567 graph_dotted_len = printf(" %-*.*s| Source code & Disassembly of %s for %s (%" PRIu64 " samples, "
2568 "percent: %s)\n",
2569 width, width, symbol_conf.show_total_period ? "Period" :
2570 symbol_conf.show_nr_samples ? "Samples" : "Percent",
2571 d_filename, evsel_name, h->nr_samples,
2572 percent_type_str(opts->percent_type));
2573
2574 printf("%-*.*s----\n",
2575 graph_dotted_len, graph_dotted_len, graph_dotted_line);
2576
2577 if (verbose > 0)
2578 symbol__annotate_hits(sym, evsel);
2579
2580 addr_fmt_width = annotated_source__addr_fmt_width(¬es->src->source, start);
2581
2582 list_for_each_entry(pos, ¬es->src->source, node) {
2583 int err;
2584
2585 if (context && queue == NULL) {
2586 queue = pos;
2587 queue_len = 0;
2588 }
2589
2590 err = annotation_line__print(pos, sym, start, evsel, len,
2591 opts->min_pcnt, printed, opts->max_lines,
2592 queue, addr_fmt_width, opts->percent_type);
2593
2594 switch (err) {
2595 case 0:
2596 ++printed;
2597 if (context) {
2598 printed += queue_len;
2599 queue = NULL;
2600 queue_len = 0;
2601 }
2602 break;
2603 case 1:
2604 /* filtered by max_lines */
2605 ++more;
2606 break;
2607 case -1:
2608 default:
2609 /*
2610 * Filtered by min_pcnt or non IP lines when
2611 * context != 0
2612 */
2613 if (!context)
2614 break;
2615 if (queue_len == context)
2616 queue = list_entry(queue->node.next, typeof(*queue), node);
2617 else
2618 ++queue_len;
2619 break;
2620 }
2621 }
2622
2623 free(filename);
2624
2625 return more;
2626 }
2627
FILE__set_percent_color(void * fp __maybe_unused,double percent __maybe_unused,bool current __maybe_unused)2628 static void FILE__set_percent_color(void *fp __maybe_unused,
2629 double percent __maybe_unused,
2630 bool current __maybe_unused)
2631 {
2632 }
2633
FILE__set_jumps_percent_color(void * fp __maybe_unused,int nr __maybe_unused,bool current __maybe_unused)2634 static int FILE__set_jumps_percent_color(void *fp __maybe_unused,
2635 int nr __maybe_unused, bool current __maybe_unused)
2636 {
2637 return 0;
2638 }
2639
FILE__set_color(void * fp __maybe_unused,int color __maybe_unused)2640 static int FILE__set_color(void *fp __maybe_unused, int color __maybe_unused)
2641 {
2642 return 0;
2643 }
2644
FILE__printf(void * fp,const char * fmt,...)2645 static void FILE__printf(void *fp, const char *fmt, ...)
2646 {
2647 va_list args;
2648
2649 va_start(args, fmt);
2650 vfprintf(fp, fmt, args);
2651 va_end(args);
2652 }
2653
FILE__write_graph(void * fp,int graph)2654 static void FILE__write_graph(void *fp, int graph)
2655 {
2656 const char *s;
2657 switch (graph) {
2658
2659 case DARROW_CHAR: s = "↓"; break;
2660 case UARROW_CHAR: s = "↑"; break;
2661 case LARROW_CHAR: s = "←"; break;
2662 case RARROW_CHAR: s = "→"; break;
2663 default: s = "?"; break;
2664 }
2665
2666 fputs(s, fp);
2667 }
2668
symbol__annotate_fprintf2(struct symbol * sym,FILE * fp)2669 static int symbol__annotate_fprintf2(struct symbol *sym, FILE *fp)
2670 {
2671 struct annotation *notes = symbol__annotation(sym);
2672 struct annotation_write_ops wops = {
2673 .first_line = true,
2674 .obj = fp,
2675 .set_color = FILE__set_color,
2676 .set_percent_color = FILE__set_percent_color,
2677 .set_jumps_percent_color = FILE__set_jumps_percent_color,
2678 .printf = FILE__printf,
2679 .write_graph = FILE__write_graph,
2680 };
2681 struct annotation_line *al;
2682
2683 list_for_each_entry(al, ¬es->src->source, node) {
2684 if (annotation_line__filter(al, notes))
2685 continue;
2686 annotation_line__write(al, notes, &wops);
2687 fputc('\n', fp);
2688 wops.first_line = false;
2689 }
2690
2691 return 0;
2692 }
2693
map_symbol__annotation_dump(struct map_symbol * ms,struct evsel * evsel)2694 int map_symbol__annotation_dump(struct map_symbol *ms, struct evsel *evsel)
2695 {
2696 const char *ev_name = evsel__name(evsel);
2697 char buf[1024];
2698 char *filename;
2699 int err = -1;
2700 FILE *fp;
2701
2702 if (asprintf(&filename, "%s.annotation", ms->sym->name) < 0)
2703 return -1;
2704
2705 fp = fopen(filename, "w");
2706 if (fp == NULL)
2707 goto out_free_filename;
2708
2709 if (evsel__is_group_event(evsel)) {
2710 evsel__group_desc(evsel, buf, sizeof(buf));
2711 ev_name = buf;
2712 }
2713
2714 fprintf(fp, "%s() %s\nEvent: %s\n\n",
2715 ms->sym->name, map__dso(ms->map)->long_name, ev_name);
2716 symbol__annotate_fprintf2(ms->sym, fp);
2717
2718 fclose(fp);
2719 err = 0;
2720 out_free_filename:
2721 free(filename);
2722 return err;
2723 }
2724
symbol__annotate_zero_histogram(struct symbol * sym,int evidx)2725 void symbol__annotate_zero_histogram(struct symbol *sym, int evidx)
2726 {
2727 struct annotation *notes = symbol__annotation(sym);
2728 struct sym_hist *h = annotation__histogram(notes, evidx);
2729
2730 memset(h, 0, notes->src->sizeof_sym_hist);
2731 }
2732
symbol__annotate_decay_histogram(struct symbol * sym,int evidx)2733 void symbol__annotate_decay_histogram(struct symbol *sym, int evidx)
2734 {
2735 struct annotation *notes = symbol__annotation(sym);
2736 struct sym_hist *h = annotation__histogram(notes, evidx);
2737 int len = symbol__size(sym), offset;
2738
2739 h->nr_samples = 0;
2740 for (offset = 0; offset < len; ++offset) {
2741 h->addr[offset].nr_samples = h->addr[offset].nr_samples * 7 / 8;
2742 h->nr_samples += h->addr[offset].nr_samples;
2743 }
2744 }
2745
annotated_source__purge(struct annotated_source * as)2746 void annotated_source__purge(struct annotated_source *as)
2747 {
2748 struct annotation_line *al, *n;
2749
2750 list_for_each_entry_safe(al, n, &as->source, node) {
2751 list_del_init(&al->node);
2752 disasm_line__free(disasm_line(al));
2753 }
2754 }
2755
disasm_line__fprintf(struct disasm_line * dl,FILE * fp)2756 static size_t disasm_line__fprintf(struct disasm_line *dl, FILE *fp)
2757 {
2758 size_t printed;
2759
2760 if (dl->al.offset == -1)
2761 return fprintf(fp, "%s\n", dl->al.line);
2762
2763 printed = fprintf(fp, "%#" PRIx64 " %s", dl->al.offset, dl->ins.name);
2764
2765 if (dl->ops.raw[0] != '\0') {
2766 printed += fprintf(fp, "%.*s %s\n", 6 - (int)printed, " ",
2767 dl->ops.raw);
2768 }
2769
2770 return printed + fprintf(fp, "\n");
2771 }
2772
disasm__fprintf(struct list_head * head,FILE * fp)2773 size_t disasm__fprintf(struct list_head *head, FILE *fp)
2774 {
2775 struct disasm_line *pos;
2776 size_t printed = 0;
2777
2778 list_for_each_entry(pos, head, al.node)
2779 printed += disasm_line__fprintf(pos, fp);
2780
2781 return printed;
2782 }
2783
disasm_line__is_valid_local_jump(struct disasm_line * dl,struct symbol * sym)2784 bool disasm_line__is_valid_local_jump(struct disasm_line *dl, struct symbol *sym)
2785 {
2786 if (!dl || !dl->ins.ops || !ins__is_jump(&dl->ins) ||
2787 !disasm_line__has_local_offset(dl) || dl->ops.target.offset < 0 ||
2788 dl->ops.target.offset >= (s64)symbol__size(sym))
2789 return false;
2790
2791 return true;
2792 }
2793
annotation__mark_jump_targets(struct annotation * notes,struct symbol * sym)2794 void annotation__mark_jump_targets(struct annotation *notes, struct symbol *sym)
2795 {
2796 u64 offset, size = symbol__size(sym);
2797
2798 /* PLT symbols contain external offsets */
2799 if (strstr(sym->name, "@plt"))
2800 return;
2801
2802 for (offset = 0; offset < size; ++offset) {
2803 struct annotation_line *al = notes->offsets[offset];
2804 struct disasm_line *dl;
2805
2806 dl = disasm_line(al);
2807
2808 if (!disasm_line__is_valid_local_jump(dl, sym))
2809 continue;
2810
2811 al = notes->offsets[dl->ops.target.offset];
2812
2813 /*
2814 * FIXME: Oops, no jump target? Buggy disassembler? Or do we
2815 * have to adjust to the previous offset?
2816 */
2817 if (al == NULL)
2818 continue;
2819
2820 if (++al->jump_sources > notes->max_jump_sources)
2821 notes->max_jump_sources = al->jump_sources;
2822 }
2823 }
2824
annotation__set_offsets(struct annotation * notes,s64 size)2825 void annotation__set_offsets(struct annotation *notes, s64 size)
2826 {
2827 struct annotation_line *al;
2828 struct annotated_source *src = notes->src;
2829
2830 src->max_line_len = 0;
2831 src->nr_entries = 0;
2832 src->nr_asm_entries = 0;
2833
2834 list_for_each_entry(al, &src->source, node) {
2835 size_t line_len = strlen(al->line);
2836
2837 if (src->max_line_len < line_len)
2838 src->max_line_len = line_len;
2839 al->idx = src->nr_entries++;
2840 if (al->offset != -1) {
2841 al->idx_asm = src->nr_asm_entries++;
2842 /*
2843 * FIXME: short term bandaid to cope with assembly
2844 * routines that comes with labels in the same column
2845 * as the address in objdump, sigh.
2846 *
2847 * E.g. copy_user_generic_unrolled
2848 */
2849 if (al->offset < size)
2850 notes->offsets[al->offset] = al;
2851 } else
2852 al->idx_asm = -1;
2853 }
2854 }
2855
width_jumps(int n)2856 static inline int width_jumps(int n)
2857 {
2858 if (n >= 100)
2859 return 5;
2860 if (n / 10)
2861 return 2;
2862 return 1;
2863 }
2864
annotation__max_ins_name(struct annotation * notes)2865 static int annotation__max_ins_name(struct annotation *notes)
2866 {
2867 int max_name = 0, len;
2868 struct annotation_line *al;
2869
2870 list_for_each_entry(al, ¬es->src->source, node) {
2871 if (al->offset == -1)
2872 continue;
2873
2874 len = strlen(disasm_line(al)->ins.name);
2875 if (max_name < len)
2876 max_name = len;
2877 }
2878
2879 return max_name;
2880 }
2881
annotation__init_column_widths(struct annotation * notes,struct symbol * sym)2882 void annotation__init_column_widths(struct annotation *notes, struct symbol *sym)
2883 {
2884 notes->widths.addr = notes->widths.target =
2885 notes->widths.min_addr = hex_width(symbol__size(sym));
2886 notes->widths.max_addr = hex_width(sym->end);
2887 notes->widths.jumps = width_jumps(notes->max_jump_sources);
2888 notes->widths.max_ins_name = annotation__max_ins_name(notes);
2889 }
2890
annotation__update_column_widths(struct annotation * notes)2891 void annotation__update_column_widths(struct annotation *notes)
2892 {
2893 if (annotate_opts.use_offset)
2894 notes->widths.target = notes->widths.min_addr;
2895 else if (annotate_opts.full_addr)
2896 notes->widths.target = BITS_PER_LONG / 4;
2897 else
2898 notes->widths.target = notes->widths.max_addr;
2899
2900 notes->widths.addr = notes->widths.target;
2901
2902 if (annotate_opts.show_nr_jumps)
2903 notes->widths.addr += notes->widths.jumps + 1;
2904 }
2905
annotation__toggle_full_addr(struct annotation * notes,struct map_symbol * ms)2906 void annotation__toggle_full_addr(struct annotation *notes, struct map_symbol *ms)
2907 {
2908 annotate_opts.full_addr = !annotate_opts.full_addr;
2909
2910 if (annotate_opts.full_addr)
2911 notes->start = map__objdump_2mem(ms->map, ms->sym->start);
2912 else
2913 notes->start = map__rip_2objdump(ms->map, ms->sym->start);
2914
2915 annotation__update_column_widths(notes);
2916 }
2917
annotation__calc_lines(struct annotation * notes,struct map_symbol * ms,struct rb_root * root)2918 static void annotation__calc_lines(struct annotation *notes, struct map_symbol *ms,
2919 struct rb_root *root)
2920 {
2921 struct annotation_line *al;
2922 struct rb_root tmp_root = RB_ROOT;
2923
2924 list_for_each_entry(al, ¬es->src->source, node) {
2925 double percent_max = 0.0;
2926 u64 addr;
2927 int i;
2928
2929 for (i = 0; i < al->data_nr; i++) {
2930 double percent;
2931
2932 percent = annotation_data__percent(&al->data[i],
2933 annotate_opts.percent_type);
2934
2935 if (percent > percent_max)
2936 percent_max = percent;
2937 }
2938
2939 if (percent_max <= 0.5)
2940 continue;
2941
2942 addr = map__rip_2objdump(ms->map, ms->sym->start);
2943 al->path = get_srcline(map__dso(ms->map), addr + al->offset, NULL,
2944 false, true, ms->sym->start + al->offset);
2945 insert_source_line(&tmp_root, al);
2946 }
2947
2948 resort_source_line(root, &tmp_root);
2949 }
2950
symbol__calc_lines(struct map_symbol * ms,struct rb_root * root)2951 static void symbol__calc_lines(struct map_symbol *ms, struct rb_root *root)
2952 {
2953 struct annotation *notes = symbol__annotation(ms->sym);
2954
2955 annotation__calc_lines(notes, ms, root);
2956 }
2957
symbol__tty_annotate2(struct map_symbol * ms,struct evsel * evsel)2958 int symbol__tty_annotate2(struct map_symbol *ms, struct evsel *evsel)
2959 {
2960 struct dso *dso = map__dso(ms->map);
2961 struct symbol *sym = ms->sym;
2962 struct rb_root source_line = RB_ROOT;
2963 struct hists *hists = evsel__hists(evsel);
2964 char buf[1024];
2965 int err;
2966
2967 err = symbol__annotate2(ms, evsel, NULL);
2968 if (err) {
2969 char msg[BUFSIZ];
2970
2971 dso->annotate_warned = true;
2972 symbol__strerror_disassemble(ms, err, msg, sizeof(msg));
2973 ui__error("Couldn't annotate %s:\n%s", sym->name, msg);
2974 return -1;
2975 }
2976
2977 if (annotate_opts.print_lines) {
2978 srcline_full_filename = annotate_opts.full_path;
2979 symbol__calc_lines(ms, &source_line);
2980 print_summary(&source_line, dso->long_name);
2981 }
2982
2983 hists__scnprintf_title(hists, buf, sizeof(buf));
2984 fprintf(stdout, "%s, [percent: %s]\n%s() %s\n",
2985 buf, percent_type_str(annotate_opts.percent_type), sym->name,
2986 dso->long_name);
2987 symbol__annotate_fprintf2(sym, stdout);
2988
2989 annotated_source__purge(symbol__annotation(sym)->src);
2990
2991 return 0;
2992 }
2993
symbol__tty_annotate(struct map_symbol * ms,struct evsel * evsel)2994 int symbol__tty_annotate(struct map_symbol *ms, struct evsel *evsel)
2995 {
2996 struct dso *dso = map__dso(ms->map);
2997 struct symbol *sym = ms->sym;
2998 struct rb_root source_line = RB_ROOT;
2999 int err;
3000
3001 err = symbol__annotate(ms, evsel, NULL);
3002 if (err) {
3003 char msg[BUFSIZ];
3004
3005 dso->annotate_warned = true;
3006 symbol__strerror_disassemble(ms, err, msg, sizeof(msg));
3007 ui__error("Couldn't annotate %s:\n%s", sym->name, msg);
3008 return -1;
3009 }
3010
3011 symbol__calc_percent(sym, evsel);
3012
3013 if (annotate_opts.print_lines) {
3014 srcline_full_filename = annotate_opts.full_path;
3015 symbol__calc_lines(ms, &source_line);
3016 print_summary(&source_line, dso->long_name);
3017 }
3018
3019 symbol__annotate_printf(ms, evsel);
3020
3021 annotated_source__purge(symbol__annotation(sym)->src);
3022
3023 return 0;
3024 }
3025
ui__has_annotation(void)3026 bool ui__has_annotation(void)
3027 {
3028 return use_browser == 1 && perf_hpp_list.sym;
3029 }
3030
3031
annotation_line__max_percent(struct annotation_line * al,struct annotation * notes,unsigned int percent_type)3032 static double annotation_line__max_percent(struct annotation_line *al,
3033 struct annotation *notes,
3034 unsigned int percent_type)
3035 {
3036 double percent_max = 0.0;
3037 int i;
3038
3039 for (i = 0; i < notes->nr_events; i++) {
3040 double percent;
3041
3042 percent = annotation_data__percent(&al->data[i],
3043 percent_type);
3044
3045 if (percent > percent_max)
3046 percent_max = percent;
3047 }
3048
3049 return percent_max;
3050 }
3051
disasm_line__write(struct disasm_line * dl,struct annotation * notes,void * obj,char * bf,size_t size,void (* obj__printf)(void * obj,const char * fmt,...),void (* obj__write_graph)(void * obj,int graph))3052 static void disasm_line__write(struct disasm_line *dl, struct annotation *notes,
3053 void *obj, char *bf, size_t size,
3054 void (*obj__printf)(void *obj, const char *fmt, ...),
3055 void (*obj__write_graph)(void *obj, int graph))
3056 {
3057 if (dl->ins.ops && dl->ins.ops->scnprintf) {
3058 if (ins__is_jump(&dl->ins)) {
3059 bool fwd;
3060
3061 if (dl->ops.target.outside)
3062 goto call_like;
3063 fwd = dl->ops.target.offset > dl->al.offset;
3064 obj__write_graph(obj, fwd ? DARROW_CHAR : UARROW_CHAR);
3065 obj__printf(obj, " ");
3066 } else if (ins__is_call(&dl->ins)) {
3067 call_like:
3068 obj__write_graph(obj, RARROW_CHAR);
3069 obj__printf(obj, " ");
3070 } else if (ins__is_ret(&dl->ins)) {
3071 obj__write_graph(obj, LARROW_CHAR);
3072 obj__printf(obj, " ");
3073 } else {
3074 obj__printf(obj, " ");
3075 }
3076 } else {
3077 obj__printf(obj, " ");
3078 }
3079
3080 disasm_line__scnprintf(dl, bf, size, !annotate_opts.use_offset, notes->widths.max_ins_name);
3081 }
3082
ipc_coverage_string(char * bf,int size,struct annotation * notes)3083 static void ipc_coverage_string(char *bf, int size, struct annotation *notes)
3084 {
3085 double ipc = 0.0, coverage = 0.0;
3086 struct annotated_branch *branch = annotation__get_branch(notes);
3087
3088 if (branch && branch->hit_cycles)
3089 ipc = branch->hit_insn / ((double)branch->hit_cycles);
3090
3091 if (branch && branch->total_insn) {
3092 coverage = branch->cover_insn * 100.0 /
3093 ((double)branch->total_insn);
3094 }
3095
3096 scnprintf(bf, size, "(Average IPC: %.2f, IPC Coverage: %.1f%%)",
3097 ipc, coverage);
3098 }
3099
__annotation_line__write(struct annotation_line * al,struct annotation * notes,bool first_line,bool current_entry,bool change_color,int width,void * obj,unsigned int percent_type,int (* obj__set_color)(void * obj,int color),void (* obj__set_percent_color)(void * obj,double percent,bool current),int (* obj__set_jumps_percent_color)(void * obj,int nr,bool current),void (* obj__printf)(void * obj,const char * fmt,...),void (* obj__write_graph)(void * obj,int graph))3100 static void __annotation_line__write(struct annotation_line *al, struct annotation *notes,
3101 bool first_line, bool current_entry, bool change_color, int width,
3102 void *obj, unsigned int percent_type,
3103 int (*obj__set_color)(void *obj, int color),
3104 void (*obj__set_percent_color)(void *obj, double percent, bool current),
3105 int (*obj__set_jumps_percent_color)(void *obj, int nr, bool current),
3106 void (*obj__printf)(void *obj, const char *fmt, ...),
3107 void (*obj__write_graph)(void *obj, int graph))
3108
3109 {
3110 double percent_max = annotation_line__max_percent(al, notes, percent_type);
3111 int pcnt_width = annotation__pcnt_width(notes),
3112 cycles_width = annotation__cycles_width(notes);
3113 bool show_title = false;
3114 char bf[256];
3115 int printed;
3116
3117 if (first_line && (al->offset == -1 || percent_max == 0.0)) {
3118 if (notes->branch && al->cycles) {
3119 if (al->cycles->ipc == 0.0 && al->cycles->avg == 0)
3120 show_title = true;
3121 } else
3122 show_title = true;
3123 }
3124
3125 if (al->offset != -1 && percent_max != 0.0) {
3126 int i;
3127
3128 for (i = 0; i < notes->nr_events; i++) {
3129 double percent;
3130
3131 percent = annotation_data__percent(&al->data[i], percent_type);
3132
3133 obj__set_percent_color(obj, percent, current_entry);
3134 if (symbol_conf.show_total_period) {
3135 obj__printf(obj, "%11" PRIu64 " ", al->data[i].he.period);
3136 } else if (symbol_conf.show_nr_samples) {
3137 obj__printf(obj, "%6" PRIu64 " ",
3138 al->data[i].he.nr_samples);
3139 } else {
3140 obj__printf(obj, "%6.2f ", percent);
3141 }
3142 }
3143 } else {
3144 obj__set_percent_color(obj, 0, current_entry);
3145
3146 if (!show_title)
3147 obj__printf(obj, "%-*s", pcnt_width, " ");
3148 else {
3149 obj__printf(obj, "%-*s", pcnt_width,
3150 symbol_conf.show_total_period ? "Period" :
3151 symbol_conf.show_nr_samples ? "Samples" : "Percent");
3152 }
3153 }
3154
3155 if (notes->branch) {
3156 if (al->cycles && al->cycles->ipc)
3157 obj__printf(obj, "%*.2f ", ANNOTATION__IPC_WIDTH - 1, al->cycles->ipc);
3158 else if (!show_title)
3159 obj__printf(obj, "%*s", ANNOTATION__IPC_WIDTH, " ");
3160 else
3161 obj__printf(obj, "%*s ", ANNOTATION__IPC_WIDTH - 1, "IPC");
3162
3163 if (!annotate_opts.show_minmax_cycle) {
3164 if (al->cycles && al->cycles->avg)
3165 obj__printf(obj, "%*" PRIu64 " ",
3166 ANNOTATION__CYCLES_WIDTH - 1, al->cycles->avg);
3167 else if (!show_title)
3168 obj__printf(obj, "%*s",
3169 ANNOTATION__CYCLES_WIDTH, " ");
3170 else
3171 obj__printf(obj, "%*s ",
3172 ANNOTATION__CYCLES_WIDTH - 1,
3173 "Cycle");
3174 } else {
3175 if (al->cycles) {
3176 char str[32];
3177
3178 scnprintf(str, sizeof(str),
3179 "%" PRIu64 "(%" PRIu64 "/%" PRIu64 ")",
3180 al->cycles->avg, al->cycles->min,
3181 al->cycles->max);
3182
3183 obj__printf(obj, "%*s ",
3184 ANNOTATION__MINMAX_CYCLES_WIDTH - 1,
3185 str);
3186 } else if (!show_title)
3187 obj__printf(obj, "%*s",
3188 ANNOTATION__MINMAX_CYCLES_WIDTH,
3189 " ");
3190 else
3191 obj__printf(obj, "%*s ",
3192 ANNOTATION__MINMAX_CYCLES_WIDTH - 1,
3193 "Cycle(min/max)");
3194 }
3195
3196 if (show_title && !*al->line) {
3197 ipc_coverage_string(bf, sizeof(bf), notes);
3198 obj__printf(obj, "%*s", ANNOTATION__AVG_IPC_WIDTH, bf);
3199 }
3200 }
3201
3202 obj__printf(obj, " ");
3203
3204 if (!*al->line)
3205 obj__printf(obj, "%-*s", width - pcnt_width - cycles_width, " ");
3206 else if (al->offset == -1) {
3207 if (al->line_nr && annotate_opts.show_linenr)
3208 printed = scnprintf(bf, sizeof(bf), "%-*d ", notes->widths.addr + 1, al->line_nr);
3209 else
3210 printed = scnprintf(bf, sizeof(bf), "%-*s ", notes->widths.addr, " ");
3211 obj__printf(obj, bf);
3212 obj__printf(obj, "%-*s", width - printed - pcnt_width - cycles_width + 1, al->line);
3213 } else {
3214 u64 addr = al->offset;
3215 int color = -1;
3216
3217 if (!annotate_opts.use_offset)
3218 addr += notes->start;
3219
3220 if (!annotate_opts.use_offset) {
3221 printed = scnprintf(bf, sizeof(bf), "%" PRIx64 ": ", addr);
3222 } else {
3223 if (al->jump_sources &&
3224 annotate_opts.offset_level >= ANNOTATION__OFFSET_JUMP_TARGETS) {
3225 if (annotate_opts.show_nr_jumps) {
3226 int prev;
3227 printed = scnprintf(bf, sizeof(bf), "%*d ",
3228 notes->widths.jumps,
3229 al->jump_sources);
3230 prev = obj__set_jumps_percent_color(obj, al->jump_sources,
3231 current_entry);
3232 obj__printf(obj, bf);
3233 obj__set_color(obj, prev);
3234 }
3235 print_addr:
3236 printed = scnprintf(bf, sizeof(bf), "%*" PRIx64 ": ",
3237 notes->widths.target, addr);
3238 } else if (ins__is_call(&disasm_line(al)->ins) &&
3239 annotate_opts.offset_level >= ANNOTATION__OFFSET_CALL) {
3240 goto print_addr;
3241 } else if (annotate_opts.offset_level == ANNOTATION__MAX_OFFSET_LEVEL) {
3242 goto print_addr;
3243 } else {
3244 printed = scnprintf(bf, sizeof(bf), "%-*s ",
3245 notes->widths.addr, " ");
3246 }
3247 }
3248
3249 if (change_color)
3250 color = obj__set_color(obj, HE_COLORSET_ADDR);
3251 obj__printf(obj, bf);
3252 if (change_color)
3253 obj__set_color(obj, color);
3254
3255 disasm_line__write(disasm_line(al), notes, obj, bf, sizeof(bf), obj__printf, obj__write_graph);
3256
3257 obj__printf(obj, "%-*s", width - pcnt_width - cycles_width - 3 - printed, bf);
3258 }
3259
3260 }
3261
annotation_line__write(struct annotation_line * al,struct annotation * notes,struct annotation_write_ops * wops)3262 void annotation_line__write(struct annotation_line *al, struct annotation *notes,
3263 struct annotation_write_ops *wops)
3264 {
3265 __annotation_line__write(al, notes, wops->first_line, wops->current_entry,
3266 wops->change_color, wops->width, wops->obj,
3267 annotate_opts.percent_type,
3268 wops->set_color, wops->set_percent_color,
3269 wops->set_jumps_percent_color, wops->printf,
3270 wops->write_graph);
3271 }
3272
symbol__annotate2(struct map_symbol * ms,struct evsel * evsel,struct arch ** parch)3273 int symbol__annotate2(struct map_symbol *ms, struct evsel *evsel,
3274 struct arch **parch)
3275 {
3276 struct symbol *sym = ms->sym;
3277 struct annotation *notes = symbol__annotation(sym);
3278 size_t size = symbol__size(sym);
3279 int nr_pcnt = 1, err;
3280
3281 notes->offsets = zalloc(size * sizeof(struct annotation_line *));
3282 if (notes->offsets == NULL)
3283 return ENOMEM;
3284
3285 if (evsel__is_group_event(evsel))
3286 nr_pcnt = evsel->core.nr_members;
3287
3288 err = symbol__annotate(ms, evsel, parch);
3289 if (err)
3290 goto out_free_offsets;
3291
3292 notes->options = &annotate_opts;
3293
3294 symbol__calc_percent(sym, evsel);
3295
3296 annotation__set_offsets(notes, size);
3297 annotation__mark_jump_targets(notes, sym);
3298
3299 err = annotation__compute_ipc(notes, size);
3300 if (err)
3301 goto out_free_offsets;
3302
3303 annotation__init_column_widths(notes, sym);
3304 notes->nr_events = nr_pcnt;
3305
3306 annotation__update_column_widths(notes);
3307 sym->annotate2 = 1;
3308
3309 return 0;
3310
3311 out_free_offsets:
3312 zfree(¬es->offsets);
3313 return err;
3314 }
3315
annotation__config(const char * var,const char * value,void * data)3316 static int annotation__config(const char *var, const char *value, void *data)
3317 {
3318 struct annotation_options *opt = data;
3319
3320 if (!strstarts(var, "annotate."))
3321 return 0;
3322
3323 if (!strcmp(var, "annotate.offset_level")) {
3324 perf_config_u8(&opt->offset_level, "offset_level", value);
3325
3326 if (opt->offset_level > ANNOTATION__MAX_OFFSET_LEVEL)
3327 opt->offset_level = ANNOTATION__MAX_OFFSET_LEVEL;
3328 else if (opt->offset_level < ANNOTATION__MIN_OFFSET_LEVEL)
3329 opt->offset_level = ANNOTATION__MIN_OFFSET_LEVEL;
3330 } else if (!strcmp(var, "annotate.hide_src_code")) {
3331 opt->hide_src_code = perf_config_bool("hide_src_code", value);
3332 } else if (!strcmp(var, "annotate.jump_arrows")) {
3333 opt->jump_arrows = perf_config_bool("jump_arrows", value);
3334 } else if (!strcmp(var, "annotate.show_linenr")) {
3335 opt->show_linenr = perf_config_bool("show_linenr", value);
3336 } else if (!strcmp(var, "annotate.show_nr_jumps")) {
3337 opt->show_nr_jumps = perf_config_bool("show_nr_jumps", value);
3338 } else if (!strcmp(var, "annotate.show_nr_samples")) {
3339 symbol_conf.show_nr_samples = perf_config_bool("show_nr_samples",
3340 value);
3341 } else if (!strcmp(var, "annotate.show_total_period")) {
3342 symbol_conf.show_total_period = perf_config_bool("show_total_period",
3343 value);
3344 } else if (!strcmp(var, "annotate.use_offset")) {
3345 opt->use_offset = perf_config_bool("use_offset", value);
3346 } else if (!strcmp(var, "annotate.disassembler_style")) {
3347 opt->disassembler_style = strdup(value);
3348 if (!opt->disassembler_style) {
3349 pr_err("Not enough memory for annotate.disassembler_style\n");
3350 return -1;
3351 }
3352 } else if (!strcmp(var, "annotate.objdump")) {
3353 opt->objdump_path = strdup(value);
3354 if (!opt->objdump_path) {
3355 pr_err("Not enough memory for annotate.objdump\n");
3356 return -1;
3357 }
3358 } else if (!strcmp(var, "annotate.addr2line")) {
3359 symbol_conf.addr2line_path = strdup(value);
3360 if (!symbol_conf.addr2line_path) {
3361 pr_err("Not enough memory for annotate.addr2line\n");
3362 return -1;
3363 }
3364 } else if (!strcmp(var, "annotate.demangle")) {
3365 symbol_conf.demangle = perf_config_bool("demangle", value);
3366 } else if (!strcmp(var, "annotate.demangle_kernel")) {
3367 symbol_conf.demangle_kernel = perf_config_bool("demangle_kernel", value);
3368 } else {
3369 pr_debug("%s variable unknown, ignoring...", var);
3370 }
3371
3372 return 0;
3373 }
3374
annotation_options__init(struct annotation_options * opt)3375 void annotation_options__init(struct annotation_options *opt)
3376 {
3377 memset(opt, 0, sizeof(*opt));
3378
3379 /* Default values. */
3380 opt->use_offset = true;
3381 opt->jump_arrows = true;
3382 opt->annotate_src = true;
3383 opt->offset_level = ANNOTATION__OFFSET_JUMP_TARGETS;
3384 opt->percent_type = PERCENT_PERIOD_LOCAL;
3385 }
3386
3387
annotation_options__exit(struct annotation_options * opt)3388 void annotation_options__exit(struct annotation_options *opt)
3389 {
3390 zfree(&opt->disassembler_style);
3391 zfree(&opt->objdump_path);
3392 }
3393
annotation_config__init(struct annotation_options * opt)3394 void annotation_config__init(struct annotation_options *opt)
3395 {
3396 perf_config(annotation__config, opt);
3397 }
3398
parse_percent_type(char * str1,char * str2)3399 static unsigned int parse_percent_type(char *str1, char *str2)
3400 {
3401 unsigned int type = (unsigned int) -1;
3402
3403 if (!strcmp("period", str1)) {
3404 if (!strcmp("local", str2))
3405 type = PERCENT_PERIOD_LOCAL;
3406 else if (!strcmp("global", str2))
3407 type = PERCENT_PERIOD_GLOBAL;
3408 }
3409
3410 if (!strcmp("hits", str1)) {
3411 if (!strcmp("local", str2))
3412 type = PERCENT_HITS_LOCAL;
3413 else if (!strcmp("global", str2))
3414 type = PERCENT_HITS_GLOBAL;
3415 }
3416
3417 return type;
3418 }
3419
annotate_parse_percent_type(const struct option * opt __maybe_unused,const char * _str,int unset __maybe_unused)3420 int annotate_parse_percent_type(const struct option *opt __maybe_unused, const char *_str,
3421 int unset __maybe_unused)
3422 {
3423 unsigned int type;
3424 char *str1, *str2;
3425 int err = -1;
3426
3427 str1 = strdup(_str);
3428 if (!str1)
3429 return -ENOMEM;
3430
3431 str2 = strchr(str1, '-');
3432 if (!str2)
3433 goto out;
3434
3435 *str2++ = 0;
3436
3437 type = parse_percent_type(str1, str2);
3438 if (type == (unsigned int) -1)
3439 type = parse_percent_type(str2, str1);
3440 if (type != (unsigned int) -1) {
3441 annotate_opts.percent_type = type;
3442 err = 0;
3443 }
3444
3445 out:
3446 free(str1);
3447 return err;
3448 }
3449
annotate_check_args(struct annotation_options * args)3450 int annotate_check_args(struct annotation_options *args)
3451 {
3452 if (args->prefix_strip && !args->prefix) {
3453 pr_err("--prefix-strip requires --prefix\n");
3454 return -1;
3455 }
3456 return 0;
3457 }
3458