]> git.ipfire.org Git - thirdparty/linux.git/blame - tools/perf/util/annotate.c
perf annotate: Add "_local" to jump/offset validation routines
[thirdparty/linux.git] / tools / perf / util / annotate.c
CommitLineData
78f7defe
ACM
1/*
2 * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
3 *
4 * Parts came from builtin-annotate.c, see those files for further
5 * copyright notes.
6 *
7 * Released under the GPL v2. (and only v2, not any later version)
8 */
9
a43783ae 10#include <errno.h>
fd20e811 11#include <inttypes.h>
78f7defe 12#include "util.h"
48c65bda
NK
13#include "ui/ui.h"
14#include "sort.h"
78f7defe
ACM
15#include "build-id.h"
16#include "color.h"
7f0b6fde 17#include "config.h"
78f7defe
ACM
18#include "cache.h"
19#include "symbol.h"
20#include "debug.h"
21#include "annotate.h"
db8fd07a 22#include "evsel.h"
70fbe057 23#include "block-range.h"
a067558e 24#include "string2.h"
786c1b51 25#include "arch/common.h"
e592488c 26#include <regex.h>
ce6f4fab 27#include <pthread.h>
4383db88 28#include <linux/bitops.h>
877a7a11 29#include <linux/kernel.h>
78f7defe 30
a1e9b74c
ACM
31/* FIXME: For the HE_COLORSET */
32#include "ui/browser.h"
33
34/*
35 * FIXME: Using the same values as slang.h,
36 * but that header may not be available everywhere
37 */
c298304b
ACM
38#define LARROW_CHAR ((unsigned char)',')
39#define RARROW_CHAR ((unsigned char)'+')
40#define DARROW_CHAR ((unsigned char)'.')
41#define UARROW_CHAR ((unsigned char)'-')
a1e9b74c 42
3d689ed6
ACM
43#include "sane_ctype.h"
44
7f0b6fde
ACM
45struct annotation_options annotation__default_options = {
46 .use_offset = true,
47 .jump_arrows = true,
48};
49
f69b64f7 50const char *disassembler_style;
7a4ec938 51const char *objdump_path;
e592488c 52static regex_t file_lineno;
f69b64f7 53
75b49202 54static struct ins_ops *ins__find(struct arch *arch, const char *name);
2a1ff812 55static void ins__sort(struct arch *arch);
75b49202 56static int disasm_line__parse(char *line, const char **namep, char **rawp);
7a997fe4 57
786c1b51
ACM
58struct arch {
59 const char *name;
763d8960
ACM
60 struct ins *instructions;
61 size_t nr_instructions;
2a1ff812
ACM
62 size_t nr_instructions_allocated;
63 struct ins_ops *(*associate_instruction_ops)(struct arch *arch, const char *name);
763d8960 64 bool sorted_instructions;
0781ea92
ACM
65 bool initialized;
66 void *priv;
69fb09f6
JY
67 unsigned int model;
68 unsigned int family;
696e2457 69 int (*init)(struct arch *arch, char *cpuid);
69fb09f6
JY
70 bool (*ins_is_fused)(struct arch *arch, const char *ins1,
71 const char *ins2);
786c1b51
ACM
72 struct {
73 char comment_char;
9c2fb451 74 char skip_functions_char;
786c1b51
ACM
75 } objdump;
76};
77
763d8960
ACM
78static struct ins_ops call_ops;
79static struct ins_ops dec_ops;
80static struct ins_ops jump_ops;
81static struct ins_ops mov_ops;
82static struct ins_ops nop_ops;
83static struct ins_ops lock_ops;
84static struct ins_ops ret_ops;
85
2a1ff812
ACM
86static int arch__grow_instructions(struct arch *arch)
87{
88 struct ins *new_instructions;
89 size_t new_nr_allocated;
90
91 if (arch->nr_instructions_allocated == 0 && arch->instructions)
92 goto grow_from_non_allocated_table;
93
94 new_nr_allocated = arch->nr_instructions_allocated + 128;
95 new_instructions = realloc(arch->instructions, new_nr_allocated * sizeof(struct ins));
96 if (new_instructions == NULL)
97 return -1;
98
99out_update_instructions:
100 arch->instructions = new_instructions;
101 arch->nr_instructions_allocated = new_nr_allocated;
102 return 0;
103
104grow_from_non_allocated_table:
105 new_nr_allocated = arch->nr_instructions + 128;
106 new_instructions = calloc(new_nr_allocated, sizeof(struct ins));
107 if (new_instructions == NULL)
108 return -1;
109
110 memcpy(new_instructions, arch->instructions, arch->nr_instructions);
111 goto out_update_instructions;
112}
113
acc9bfb5 114static int arch__associate_ins_ops(struct arch* arch, const char *name, struct ins_ops *ops)
2a1ff812
ACM
115{
116 struct ins *ins;
117
118 if (arch->nr_instructions == arch->nr_instructions_allocated &&
119 arch__grow_instructions(arch))
120 return -1;
121
122 ins = &arch->instructions[arch->nr_instructions];
123 ins->name = strdup(name);
124 if (!ins->name)
125 return -1;
126
127 ins->ops = ops;
128 arch->nr_instructions++;
129
130 ins__sort(arch);
131 return 0;
132}
133
763d8960 134#include "arch/arm/annotate/instructions.c"
0fcb1da4 135#include "arch/arm64/annotate/instructions.c"
763d8960 136#include "arch/x86/annotate/instructions.c"
dbdebdc5 137#include "arch/powerpc/annotate/instructions.c"
d9f8dfa9 138#include "arch/s390/annotate/instructions.c"
763d8960 139
786c1b51
ACM
140static struct arch architectures[] = {
141 {
142 .name = "arm",
acc9bfb5 143 .init = arm__annotate_init,
786c1b51 144 },
0fcb1da4
KP
145 {
146 .name = "arm64",
147 .init = arm64__annotate_init,
148 },
786c1b51
ACM
149 {
150 .name = "x86",
696e2457 151 .init = x86__annotate_init,
763d8960
ACM
152 .instructions = x86__instructions,
153 .nr_instructions = ARRAY_SIZE(x86__instructions),
69fb09f6 154 .ins_is_fused = x86__ins_is_fused,
786c1b51
ACM
155 .objdump = {
156 .comment_char = '#',
157 },
158 },
dbdebdc5
RB
159 {
160 .name = "powerpc",
161 .init = powerpc__annotate_init,
162 },
e77852b3
CB
163 {
164 .name = "s390",
d9f8dfa9 165 .init = s390__annotate_init,
e77852b3
CB
166 .objdump = {
167 .comment_char = '#',
168 },
169 },
786c1b51
ACM
170};
171
c46219ac
ACM
172static void ins__delete(struct ins_operands *ops)
173{
3995614d
ACM
174 if (ops == NULL)
175 return;
74cf249d
ACM
176 zfree(&ops->source.raw);
177 zfree(&ops->source.name);
178 zfree(&ops->target.raw);
179 zfree(&ops->target.name);
c46219ac
ACM
180}
181
5417072b
ACM
182static int ins__raw_scnprintf(struct ins *ins, char *bf, size_t size,
183 struct ins_operands *ops)
184{
648388ae 185 return scnprintf(bf, size, "%-6s %s", ins->name, ops->raw);
5417072b
ACM
186}
187
188int ins__scnprintf(struct ins *ins, char *bf, size_t size,
189 struct ins_operands *ops)
190{
191 if (ins->ops->scnprintf)
192 return ins->ops->scnprintf(ins, bf, size, ops);
193
194 return ins__raw_scnprintf(ins, bf, size, ops);
195}
196
69fb09f6
JY
197bool ins__is_fused(struct arch *arch, const char *ins1, const char *ins2)
198{
199 if (!arch || !arch->ins_is_fused)
200 return false;
201
202 return arch->ins_is_fused(arch, ins1, ins2);
203}
204
85a84e4f 205static int call__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms)
d86b0597 206{
d2232885 207 char *endptr, *tok, *name;
85a84e4f 208 struct map *map = ms->map;
696703af
ACM
209 struct addr_map_symbol target = {
210 .map = map,
211 };
d2232885 212
44d1a3ed 213 ops->target.addr = strtoull(ops->raw, &endptr, 16);
d2232885
ACM
214
215 name = strchr(endptr, '<');
216 if (name == NULL)
217 goto indirect_call;
218
219 name++;
220
9c2fb451
ACM
221 if (arch->objdump.skip_functions_char &&
222 strchr(name, arch->objdump.skip_functions_char))
cfef25b8 223 return -1;
cfef25b8 224
d2232885
ACM
225 tok = strchr(name, '>');
226 if (tok == NULL)
227 return -1;
228
229 *tok = '\0';
44d1a3ed 230 ops->target.name = strdup(name);
d2232885
ACM
231 *tok = '>';
232
696703af
ACM
233 if (ops->target.name == NULL)
234 return -1;
235find_target:
236 target.addr = map__objdump_2mem(map, ops->target.addr);
d2232885 237
696703af
ACM
238 if (map_groups__find_ams(&target) == 0 &&
239 map__rip_2objdump(target.map, map->map_ip(target.map, target.addr)) == ops->target.addr)
240 ops->target.sym = target.sym;
e8ea1561 241
d86b0597 242 return 0;
696703af
ACM
243
244indirect_call:
245 tok = strchr(endptr, '*');
246 if (tok != NULL)
247 ops->target.addr = strtoull(tok + 1, NULL, 16);
248 goto find_target;
d86b0597
ACM
249}
250
d2232885 251static int call__scnprintf(struct ins *ins, char *bf, size_t size,
5417072b 252 struct ins_operands *ops)
d2232885 253{
696703af
ACM
254 if (ops->target.sym)
255 return scnprintf(bf, size, "%-6s %s", ins->name, ops->target.sym->name);
d2232885 256
e8ea1561
ACM
257 if (ops->target.addr == 0)
258 return ins__raw_scnprintf(ins, bf, size, ops);
259
4c9cb2c2
ACM
260 if (ops->target.name)
261 return scnprintf(bf, size, "%-6s %s", ins->name, ops->target.name);
262
648388ae 263 return scnprintf(bf, size, "%-6s *%" PRIx64, ins->name, ops->target.addr);
d2232885
ACM
264}
265
d86b0597 266static struct ins_ops call_ops = {
d2232885
ACM
267 .parse = call__parse,
268 .scnprintf = call__scnprintf,
d86b0597
ACM
269};
270
271bool ins__is_call(const struct ins *ins)
272{
0b58a77c 273 return ins->ops == &call_ops || ins->ops == &s390_call_ops;
d86b0597
ACM
274}
275
751b1783 276static int jump__parse(struct arch *arch __maybe_unused, struct ins_operands *ops, struct map_symbol *ms)
4f9d0325 277{
751b1783
ACM
278 struct map *map = ms->map;
279 struct symbol *sym = ms->sym;
280 struct addr_map_symbol target = {
281 .map = map,
282 };
c7e6ead7 283 const char *s = strchr(ops->raw, '+');
3ee2eb6d 284 const char *c = strchr(ops->raw, ',');
751b1783
ACM
285 u64 start, end;
286 /*
287 * Examples of lines to parse for the _cpp_lex_token@@Base
288 * function:
289 *
290 * 1159e6c: jne 115aa32 <_cpp_lex_token@@Base+0xf92>
291 * 1159e8b: jne c469be <cpp_named_operator2name@@Base+0xa72>
292 *
293 * The first is a jump to an offset inside the same function,
294 * the second is to another function, i.e. that 0xa72 is an
295 * offset in the cpp_named_operator2name@@base function.
296 */
b13bbeee
KP
297 /*
298 * skip over possible up to 2 operands to get to address, e.g.:
299 * tbnz w0, #26, ffff0000083cd190 <security_file_permission+0xd0>
300 */
301 if (c++ != NULL) {
3ee2eb6d 302 ops->target.addr = strtoull(c, NULL, 16);
b13bbeee
KP
303 if (!ops->target.addr) {
304 c = strchr(c, ',');
305 if (c++ != NULL)
306 ops->target.addr = strtoull(c, NULL, 16);
307 }
308 } else {
3ee2eb6d 309 ops->target.addr = strtoull(ops->raw, NULL, 16);
b13bbeee 310 }
fb29fa58 311
751b1783
ACM
312 target.addr = map__objdump_2mem(map, ops->target.addr);
313 start = map->unmap_ip(map, sym->start),
314 end = map->unmap_ip(map, sym->end);
315
316 ops->target.outside = target.addr < start || target.addr > end;
317
318 /*
319 * FIXME: things like this in _cpp_lex_token (gcc's cc1 program):
320
321 cpp_named_operator2name@@Base+0xa72
322
323 * Point to a place that is after the cpp_named_operator2name
324 * boundaries, i.e. in the ELF symbol table for cc1
325 * cpp_named_operator2name is marked as being 32-bytes long, but it in
326 * fact is much larger than that, so we seem to need a symbols__find()
327 * routine that looks for >= current->start and < next_symbol->start,
328 * possibly just for C++ objects?
329 *
330 * For now lets just make some progress by marking jumps to outside the
331 * current function as call like.
332 *
333 * Actual navigation will come next, with further understanding of how
334 * the symbol searching and disassembly should be done.
335
336 if (map_groups__find_ams(&target) == 0 &&
337 map__rip_2objdump(target.map, map->map_ip(target.map, target.addr)) == ops->target.addr)
338 ops->target.sym = target.sym;
339 */
340
e216874c 341 if (s++ != NULL) {
bbb7f846 342 ops->target.offset = strtoull(s, NULL, 16);
e216874c
RB
343 ops->target.offset_avail = true;
344 } else {
345 ops->target.offset_avail = false;
346 }
4f9d0325 347
4f9d0325
ACM
348 return 0;
349}
350
c7e6ead7 351static int jump__scnprintf(struct ins *ins, char *bf, size_t size,
5417072b 352 struct ins_operands *ops)
28548d78 353{
b13bbeee
KP
354 const char *c = strchr(ops->raw, ',');
355
e216874c 356 if (!ops->target.addr || ops->target.offset < 0)
bec60e50
RB
357 return ins__raw_scnprintf(ins, bf, size, ops);
358
b13bbeee
KP
359 if (c != NULL) {
360 const char *c2 = strchr(c + 1, ',');
361
362 /* check for 3-op insn */
363 if (c2 != NULL)
364 c = c2;
365 c++;
366
367 /* mirror arch objdump's space-after-comma style */
368 if (*c == ' ')
369 c++;
370 }
371
648388ae 372 return scnprintf(bf, size, "%-6s %.*s%" PRIx64,
b13bbeee
KP
373 ins->name, c ? c - ops->raw : 0, ops->raw,
374 ops->target.offset);
28548d78
ACM
375}
376
4f9d0325 377static struct ins_ops jump_ops = {
c7e6ead7
ACM
378 .parse = jump__parse,
379 .scnprintf = jump__scnprintf,
4f9d0325
ACM
380};
381
382bool ins__is_jump(const struct ins *ins)
383{
384 return ins->ops == &jump_ops;
385}
386
6de783b6
ACM
387static int comment__symbol(char *raw, char *comment, u64 *addrp, char **namep)
388{
389 char *endptr, *name, *t;
390
391 if (strstr(raw, "(%rip)") == NULL)
392 return 0;
393
394 *addrp = strtoull(comment, &endptr, 16);
35a8a148
TR
395 if (endptr == comment)
396 return 0;
6de783b6
ACM
397 name = strchr(endptr, '<');
398 if (name == NULL)
399 return -1;
400
401 name++;
402
403 t = strchr(name, '>');
404 if (t == NULL)
405 return 0;
406
407 *t = '\0';
408 *namep = strdup(name);
409 *t = '>';
410
411 return 0;
412}
413
85a84e4f 414static int lock__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms)
7a997fe4 415{
7a997fe4
ACM
416 ops->locked.ops = zalloc(sizeof(*ops->locked.ops));
417 if (ops->locked.ops == NULL)
418 return 0;
419
75b49202 420 if (disasm_line__parse(ops->raw, &ops->locked.ins.name, &ops->locked.ops->raw) < 0)
7a997fe4
ACM
421 goto out_free_ops;
422
75b49202 423 ops->locked.ins.ops = ins__find(arch, ops->locked.ins.name);
0fb9f2aa 424
75b49202 425 if (ops->locked.ins.ops == NULL)
2ba34aaa 426 goto out_free_ops;
7a997fe4 427
75b49202 428 if (ops->locked.ins.ops->parse &&
85a84e4f 429 ops->locked.ins.ops->parse(arch, ops->locked.ops, ms) < 0)
be81908c 430 goto out_free_ops;
7a997fe4
ACM
431
432 return 0;
433
434out_free_ops:
04662523 435 zfree(&ops->locked.ops);
7a997fe4
ACM
436 return 0;
437}
438
439static int lock__scnprintf(struct ins *ins, char *bf, size_t size,
440 struct ins_operands *ops)
441{
442 int printed;
443
75b49202 444 if (ops->locked.ins.ops == NULL)
7a997fe4
ACM
445 return ins__raw_scnprintf(ins, bf, size, ops);
446
648388ae 447 printed = scnprintf(bf, size, "%-6s ", ins->name);
75b49202 448 return printed + ins__scnprintf(&ops->locked.ins, bf + printed,
7a997fe4
ACM
449 size - printed, ops->locked.ops);
450}
451
c46219ac
ACM
452static void lock__delete(struct ins_operands *ops)
453{
75b49202 454 struct ins *ins = &ops->locked.ins;
0fb9f2aa 455
75b49202 456 if (ins->ops && ins->ops->free)
0fb9f2aa
RV
457 ins->ops->free(ops->locked.ops);
458 else
459 ins__delete(ops->locked.ops);
460
74cf249d
ACM
461 zfree(&ops->locked.ops);
462 zfree(&ops->target.raw);
463 zfree(&ops->target.name);
c46219ac
ACM
464}
465
7a997fe4 466static struct ins_ops lock_ops = {
c46219ac 467 .free = lock__delete,
7a997fe4
ACM
468 .parse = lock__parse,
469 .scnprintf = lock__scnprintf,
470};
471
85a84e4f 472static int mov__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms __maybe_unused)
6de783b6
ACM
473{
474 char *s = strchr(ops->raw, ','), *target, *comment, prev;
475
476 if (s == NULL)
477 return -1;
478
479 *s = '\0';
480 ops->source.raw = strdup(ops->raw);
481 *s = ',';
48000a1a 482
6de783b6
ACM
483 if (ops->source.raw == NULL)
484 return -1;
485
486 target = ++s;
786c1b51 487 comment = strchr(s, arch->objdump.comment_char);
1e2bb043
AC
488
489 if (comment != NULL)
490 s = comment - 1;
491 else
492 s = strchr(s, '\0') - 1;
6de783b6 493
1e2bb043
AC
494 while (s > target && isspace(s[0]))
495 --s;
496 s++;
6de783b6
ACM
497 prev = *s;
498 *s = '\0';
499
500 ops->target.raw = strdup(target);
501 *s = prev;
502
503 if (ops->target.raw == NULL)
504 goto out_free_source;
505
6de783b6
ACM
506 if (comment == NULL)
507 return 0;
508
4597cf06 509 comment = ltrim(comment);
35a8a148
TR
510 comment__symbol(ops->source.raw, comment + 1, &ops->source.addr, &ops->source.name);
511 comment__symbol(ops->target.raw, comment + 1, &ops->target.addr, &ops->target.name);
6de783b6
ACM
512
513 return 0;
514
515out_free_source:
04662523 516 zfree(&ops->source.raw);
6de783b6
ACM
517 return -1;
518}
519
520static int mov__scnprintf(struct ins *ins, char *bf, size_t size,
521 struct ins_operands *ops)
522{
648388ae 523 return scnprintf(bf, size, "%-6s %s,%s", ins->name,
6de783b6
ACM
524 ops->source.name ?: ops->source.raw,
525 ops->target.name ?: ops->target.raw);
526}
527
528static struct ins_ops mov_ops = {
529 .parse = mov__parse,
530 .scnprintf = mov__scnprintf,
531};
532
85a84e4f 533static int dec__parse(struct arch *arch __maybe_unused, struct ins_operands *ops, struct map_symbol *ms __maybe_unused)
a43712c4
ACM
534{
535 char *target, *comment, *s, prev;
536
537 target = s = ops->raw;
538
539 while (s[0] != '\0' && !isspace(s[0]))
540 ++s;
541 prev = *s;
542 *s = '\0';
543
544 ops->target.raw = strdup(target);
545 *s = prev;
546
547 if (ops->target.raw == NULL)
548 return -1;
549
859afa6c 550 comment = strchr(s, arch->objdump.comment_char);
a43712c4
ACM
551 if (comment == NULL)
552 return 0;
553
4597cf06 554 comment = ltrim(comment);
35a8a148 555 comment__symbol(ops->target.raw, comment + 1, &ops->target.addr, &ops->target.name);
a43712c4
ACM
556
557 return 0;
558}
559
560static int dec__scnprintf(struct ins *ins, char *bf, size_t size,
561 struct ins_operands *ops)
562{
648388ae 563 return scnprintf(bf, size, "%-6s %s", ins->name,
a43712c4
ACM
564 ops->target.name ?: ops->target.raw);
565}
566
567static struct ins_ops dec_ops = {
568 .parse = dec__parse,
569 .scnprintf = dec__scnprintf,
570};
571
1d037ca1
IT
572static int nop__scnprintf(struct ins *ins __maybe_unused, char *bf, size_t size,
573 struct ins_operands *ops __maybe_unused)
b9818e93 574{
648388ae 575 return scnprintf(bf, size, "%-6s", "nop");
b9818e93
ACM
576}
577
578static struct ins_ops nop_ops = {
579 .scnprintf = nop__scnprintf,
580};
581
6ef94929
NR
582static struct ins_ops ret_ops = {
583 .scnprintf = ins__raw_scnprintf,
584};
585
586bool ins__is_ret(const struct ins *ins)
587{
588 return ins->ops == &ret_ops;
589}
590
7e63a13a
JY
591bool ins__is_lock(const struct ins *ins)
592{
593 return ins->ops == &lock_ops;
594}
595
7e4c1498 596static int ins__key_cmp(const void *name, const void *insp)
4f9d0325
ACM
597{
598 const struct ins *ins = insp;
599
600 return strcmp(name, ins->name);
601}
602
7e4c1498
CR
603static int ins__cmp(const void *a, const void *b)
604{
605 const struct ins *ia = a;
606 const struct ins *ib = b;
607
608 return strcmp(ia->name, ib->name);
609}
610
763d8960 611static void ins__sort(struct arch *arch)
7e4c1498 612{
763d8960 613 const int nmemb = arch->nr_instructions;
7e4c1498 614
763d8960 615 qsort(arch->instructions, nmemb, sizeof(struct ins), ins__cmp);
7e4c1498
CR
616}
617
2a1ff812 618static struct ins_ops *__ins__find(struct arch *arch, const char *name)
4f9d0325 619{
75b49202 620 struct ins *ins;
763d8960 621 const int nmemb = arch->nr_instructions;
7e4c1498 622
763d8960
ACM
623 if (!arch->sorted_instructions) {
624 ins__sort(arch);
625 arch->sorted_instructions = true;
7e4c1498 626 }
4f9d0325 627
75b49202
ACM
628 ins = bsearch(name, arch->instructions, nmemb, sizeof(struct ins), ins__key_cmp);
629 return ins ? ins->ops : NULL;
4f9d0325
ACM
630}
631
2a1ff812
ACM
632static struct ins_ops *ins__find(struct arch *arch, const char *name)
633{
634 struct ins_ops *ops = __ins__find(arch, name);
635
636 if (!ops && arch->associate_instruction_ops)
637 ops = arch->associate_instruction_ops(arch, name);
638
639 return ops;
640}
641
786c1b51
ACM
642static int arch__key_cmp(const void *name, const void *archp)
643{
644 const struct arch *arch = archp;
645
646 return strcmp(name, arch->name);
647}
648
649static int arch__cmp(const void *a, const void *b)
650{
651 const struct arch *aa = a;
652 const struct arch *ab = b;
653
654 return strcmp(aa->name, ab->name);
655}
656
657static void arch__sort(void)
658{
659 const int nmemb = ARRAY_SIZE(architectures);
660
661 qsort(architectures, nmemb, sizeof(struct arch), arch__cmp);
662}
663
664static struct arch *arch__find(const char *name)
665{
666 const int nmemb = ARRAY_SIZE(architectures);
667 static bool sorted;
668
669 if (!sorted) {
670 arch__sort();
671 sorted = true;
672 }
673
674 return bsearch(name, architectures, nmemb, sizeof(struct arch), arch__key_cmp);
675}
676
d04b35f8 677int symbol__alloc_hist(struct symbol *sym)
ce6f4fab
ACM
678{
679 struct annotation *notes = symbol__annotation(sym);
331c7cb3 680 size_t size = symbol__size(sym);
8696329b
CS
681 size_t sizeof_sym_hist;
682
331c7cb3
RB
683 /*
684 * Add buffer of one element for zero length symbol.
685 * When sample is taken from first instruction of
686 * zero length symbol, perf still resolves it and
687 * shows symbol name in perf report and allows to
688 * annotate it.
689 */
690 if (size == 0)
691 size = 1;
692
8696329b 693 /* Check for overflow when calculating sizeof_sym_hist */
896bccd3 694 if (size > (SIZE_MAX - sizeof(struct sym_hist)) / sizeof(struct sym_hist_entry))
8696329b
CS
695 return -1;
696
896bccd3 697 sizeof_sym_hist = (sizeof(struct sym_hist) + size * sizeof(struct sym_hist_entry));
8696329b
CS
698
699 /* Check for overflow in zalloc argument */
700 if (sizeof_sym_hist > (SIZE_MAX - sizeof(*notes->src))
701 / symbol_conf.nr_events)
702 return -1;
ce6f4fab 703
d04b35f8 704 notes->src = zalloc(sizeof(*notes->src) + symbol_conf.nr_events * sizeof_sym_hist);
ce6f4fab
ACM
705 if (notes->src == NULL)
706 return -1;
707 notes->src->sizeof_sym_hist = sizeof_sym_hist;
d04b35f8 708 notes->src->nr_histograms = symbol_conf.nr_events;
ce6f4fab
ACM
709 INIT_LIST_HEAD(&notes->src->source);
710 return 0;
78f7defe
ACM
711}
712
d4957633
AK
713/* The cycles histogram is lazily allocated. */
714static int symbol__alloc_hist_cycles(struct symbol *sym)
715{
716 struct annotation *notes = symbol__annotation(sym);
717 const size_t size = symbol__size(sym);
718
719 notes->src->cycles_hist = calloc(size, sizeof(struct cyc_hist));
720 if (notes->src->cycles_hist == NULL)
721 return -1;
722 return 0;
723}
724
36532461
ACM
725void symbol__annotate_zero_histograms(struct symbol *sym)
726{
727 struct annotation *notes = symbol__annotation(sym);
728
ce6f4fab 729 pthread_mutex_lock(&notes->lock);
d4957633 730 if (notes->src != NULL) {
ce6f4fab
ACM
731 memset(notes->src->histograms, 0,
732 notes->src->nr_histograms * notes->src->sizeof_sym_hist);
d4957633
AK
733 if (notes->src->cycles_hist)
734 memset(notes->src->cycles_hist, 0,
735 symbol__size(sym) * sizeof(struct cyc_hist));
736 }
ce6f4fab 737 pthread_mutex_unlock(&notes->lock);
36532461
ACM
738}
739
d4957633
AK
740static int __symbol__account_cycles(struct annotation *notes,
741 u64 start,
742 unsigned offset, unsigned cycles,
743 unsigned have_start)
744{
745 struct cyc_hist *ch;
746
747 ch = notes->src->cycles_hist;
748 /*
749 * For now we can only account one basic block per
750 * final jump. But multiple could be overlapping.
751 * Always account the longest one. So when
752 * a shorter one has been already seen throw it away.
753 *
754 * We separately always account the full cycles.
755 */
756 ch[offset].num_aggr++;
757 ch[offset].cycles_aggr += cycles;
758
759 if (!have_start && ch[offset].have_start)
760 return 0;
761 if (ch[offset].num) {
762 if (have_start && (!ch[offset].have_start ||
763 ch[offset].start > start)) {
764 ch[offset].have_start = 0;
765 ch[offset].cycles = 0;
766 ch[offset].num = 0;
767 if (ch[offset].reset < 0xffff)
768 ch[offset].reset++;
769 } else if (have_start &&
770 ch[offset].start < start)
771 return 0;
772 }
773 ch[offset].have_start = have_start;
774 ch[offset].start = start;
775 ch[offset].cycles += cycles;
776 ch[offset].num++;
777 return 0;
778}
779
b66d8c0c 780static int __symbol__inc_addr_samples(struct symbol *sym, struct map *map,
bab89f6a 781 struct annotation *notes, int evidx, u64 addr,
461c17f0 782 struct perf_sample *sample)
78f7defe 783{
2f525d01 784 unsigned offset;
78f7defe
ACM
785 struct sym_hist *h;
786
78f7defe
ACM
787 pr_debug3("%s: addr=%#" PRIx64 "\n", __func__, map->unmap_ip(map, addr));
788
edee44be
RB
789 if ((addr < sym->start || addr >= sym->end) &&
790 (addr != sym->end || sym->start != sym->end)) {
e3d006ce
ACM
791 pr_debug("%s(%d): ERANGE! sym->name=%s, start=%#" PRIx64 ", addr=%#" PRIx64 ", end=%#" PRIx64 "\n",
792 __func__, __LINE__, sym->name, sym->start, addr, sym->end);
31d68e7b 793 return -ERANGE;
e3d006ce 794 }
78f7defe 795
2f525d01
ACM
796 offset = addr - sym->start;
797 h = annotation__histogram(notes, evidx);
8158683d 798 h->nr_samples++;
896bccd3 799 h->addr[offset].nr_samples++;
461c17f0
TS
800 h->period += sample->period;
801 h->addr[offset].period += sample->period;
78f7defe
ACM
802
803 pr_debug3("%#" PRIx64 " %s: period++ [addr: %#" PRIx64 ", %#" PRIx64
461c17f0
TS
804 ", evidx=%d] => nr_samples: %" PRIu64 ", period: %" PRIu64 "\n",
805 sym->start, sym->name, addr, addr - sym->start, evidx,
806 h->addr[offset].nr_samples, h->addr[offset].period);
78f7defe
ACM
807 return 0;
808}
809
d4957633 810static struct annotation *symbol__get_annotation(struct symbol *sym, bool cycles)
83be34a7
AK
811{
812 struct annotation *notes = symbol__annotation(sym);
813
814 if (notes->src == NULL) {
815 if (symbol__alloc_hist(sym) < 0)
816 return NULL;
817 }
d4957633
AK
818 if (!notes->src->cycles_hist && cycles) {
819 if (symbol__alloc_hist_cycles(sym) < 0)
820 return NULL;
821 }
83be34a7
AK
822 return notes;
823}
824
44e83039 825static int symbol__inc_addr_samples(struct symbol *sym, struct map *map,
bab89f6a
TS
826 int evidx, u64 addr,
827 struct perf_sample *sample)
b66d8c0c
ACM
828{
829 struct annotation *notes;
830
48c65bda 831 if (sym == NULL)
b66d8c0c 832 return 0;
d4957633 833 notes = symbol__get_annotation(sym, false);
83be34a7
AK
834 if (notes == NULL)
835 return -ENOMEM;
bab89f6a 836 return __symbol__inc_addr_samples(sym, map, notes, evidx, addr, sample);
b66d8c0c
ACM
837}
838
d4957633
AK
839static int symbol__account_cycles(u64 addr, u64 start,
840 struct symbol *sym, unsigned cycles)
841{
842 struct annotation *notes;
843 unsigned offset;
844
845 if (sym == NULL)
846 return 0;
847 notes = symbol__get_annotation(sym, true);
848 if (notes == NULL)
849 return -ENOMEM;
850 if (addr < sym->start || addr >= sym->end)
851 return -ERANGE;
852
853 if (start) {
854 if (start < sym->start || start >= sym->end)
855 return -ERANGE;
856 if (start >= addr)
857 start = 0;
858 }
859 offset = addr - sym->start;
860 return __symbol__account_cycles(notes,
861 start ? start - sym->start : 0,
862 offset, cycles,
863 !!start);
864}
865
866int addr_map_symbol__account_cycles(struct addr_map_symbol *ams,
867 struct addr_map_symbol *start,
868 unsigned cycles)
869{
3d7245b0 870 u64 saddr = 0;
d4957633
AK
871 int err;
872
873 if (!cycles)
874 return 0;
875
876 /*
877 * Only set start when IPC can be computed. We can only
878 * compute it when the basic block is completely in a single
879 * function.
880 * Special case the case when the jump is elsewhere, but
881 * it starts on the function start.
882 */
883 if (start &&
884 (start->sym == ams->sym ||
885 (ams->sym &&
886 start->addr == ams->sym->start + ams->map->start)))
887 saddr = start->al_addr;
888 if (saddr == 0)
3d7245b0 889 pr_debug2("BB with bad start: addr %"PRIx64" start %"PRIx64" sym %"PRIx64" saddr %"PRIx64"\n",
d4957633
AK
890 ams->addr,
891 start ? start->addr : 0,
892 ams->sym ? ams->sym->start + ams->map->start : 0,
893 saddr);
894 err = symbol__account_cycles(ams->al_addr, saddr, ams->sym, cycles);
895 if (err)
896 pr_debug2("account_cycles failed %d\n", err);
897 return err;
898}
899
f56c083b
ACM
900static unsigned annotation__count_insn(struct annotation *notes, u64 start, u64 end)
901{
902 unsigned n_insn = 0;
903 u64 offset;
904
905 for (offset = start; offset <= end; offset++) {
906 if (notes->offsets[offset])
907 n_insn++;
908 }
909 return n_insn;
910}
911
912static void annotation__count_and_fill(struct annotation *notes, u64 start, u64 end, struct cyc_hist *ch)
913{
914 unsigned n_insn;
915 u64 offset;
916
917 n_insn = annotation__count_insn(notes, start, end);
918 if (n_insn && ch->num && ch->cycles) {
919 float ipc = n_insn / ((double)ch->cycles / (double)ch->num);
920
921 /* Hide data when there are too many overlaps. */
922 if (ch->reset >= 0x7fff || ch->reset >= ch->num / 2)
923 return;
924
925 for (offset = start; offset <= end; offset++) {
926 struct annotation_line *al = notes->offsets[offset];
927
928 if (al)
929 al->ipc = ipc;
930 }
931 }
932}
933
934void annotation__compute_ipc(struct annotation *notes, size_t size)
935{
936 u64 offset;
937
938 if (!notes->src || !notes->src->cycles_hist)
939 return;
940
941 pthread_mutex_lock(&notes->lock);
942 for (offset = 0; offset < size; ++offset) {
943 struct cyc_hist *ch;
944
945 ch = &notes->src->cycles_hist[offset];
946 if (ch && ch->cycles) {
947 struct annotation_line *al;
948
949 if (ch->have_start)
950 annotation__count_and_fill(notes, ch->start, offset, ch);
951 al = notes->offsets[offset];
952 if (al && ch->num_aggr)
953 al->cycles = ch->cycles_aggr / ch->num_aggr;
954 notes->have_cycles = true;
955 }
956 }
957 pthread_mutex_unlock(&notes->lock);
958}
959
bab89f6a
TS
960int addr_map_symbol__inc_samples(struct addr_map_symbol *ams, struct perf_sample *sample,
961 int evidx)
0f4e7a24 962{
bab89f6a 963 return symbol__inc_addr_samples(ams->sym, ams->map, evidx, ams->al_addr, sample);
0f4e7a24
ACM
964}
965
bab89f6a
TS
966int hist_entry__inc_addr_samples(struct hist_entry *he, struct perf_sample *sample,
967 int evidx, u64 ip)
f626adff 968{
bab89f6a 969 return symbol__inc_addr_samples(he->ms.sym, he->ms.map, evidx, ip, sample);
f626adff
ACM
970}
971
85a84e4f 972static void disasm_line__init_ins(struct disasm_line *dl, struct arch *arch, struct map_symbol *ms)
4f9d0325 973{
75b49202 974 dl->ins.ops = ins__find(arch, dl->ins.name);
4f9d0325 975
75b49202 976 if (!dl->ins.ops)
4f9d0325
ACM
977 return;
978
85a84e4f 979 if (dl->ins.ops->parse && dl->ins.ops->parse(arch, &dl->ops, ms) < 0)
75b49202 980 dl->ins.ops = NULL;
4f9d0325
ACM
981}
982
75b49202 983static int disasm_line__parse(char *line, const char **namep, char **rawp)
7a997fe4 984{
4597cf06 985 char tmp, *name = ltrim(line);
7a997fe4
ACM
986
987 if (name[0] == '\0')
988 return -1;
989
990 *rawp = name + 1;
991
992 while ((*rawp)[0] != '\0' && !isspace((*rawp)[0]))
993 ++*rawp;
994
995 tmp = (*rawp)[0];
996 (*rawp)[0] = '\0';
997 *namep = strdup(name);
998
999 if (*namep == NULL)
1000 goto out_free_name;
1001
1002 (*rawp)[0] = tmp;
4597cf06 1003 *rawp = ltrim(*rawp);
7a997fe4
ACM
1004
1005 return 0;
1006
1007out_free_name:
75b49202
ACM
1008 free((void *)namep);
1009 *namep = NULL;
7a997fe4
ACM
1010 return -1;
1011}
1012
ea07c5aa
JO
1013struct annotate_args {
1014 size_t privsize;
24fe7b88 1015 struct arch *arch;
85a84e4f 1016 struct map_symbol ms;
d03a686e 1017 struct perf_evsel *evsel;
4748834f
JO
1018 s64 offset;
1019 char *line;
1020 int line_nr;
ea07c5aa
JO
1021};
1022
c835e191
JO
1023static void annotation_line__delete(struct annotation_line *al)
1024{
1025 void *ptr = (void *) al - al->privsize;
1026
8b4c74dc 1027 free_srcline(al->path);
c835e191
JO
1028 zfree(&al->line);
1029 free(ptr);
1030}
1031
1032/*
1033 * Allocating the annotation line data with following
1034 * structure:
1035 *
1036 * --------------------------------------
1037 * private space | struct annotation_line
1038 * --------------------------------------
1039 *
1040 * Size of the private space is stored in 'struct annotation_line'.
1041 *
1042 */
1043static struct annotation_line *
1044annotation_line__new(struct annotate_args *args, size_t privsize)
1045{
1046 struct annotation_line *al;
7e304557 1047 struct perf_evsel *evsel = args->evsel;
c835e191 1048 size_t size = privsize + sizeof(*al);
7e304557
JO
1049 int nr = 1;
1050
1051 if (perf_evsel__is_group_event(evsel))
1052 nr = evsel->nr_members;
1053
1054 size += sizeof(al->samples[0]) * nr;
c835e191
JO
1055
1056 al = zalloc(size);
1057 if (al) {
1058 al = (void *) al + privsize;
1059 al->privsize = privsize;
1060 al->offset = args->offset;
1061 al->line = strdup(args->line);
1062 al->line_nr = args->line_nr;
7e304557 1063 al->samples_nr = nr;
c835e191
JO
1064 }
1065
1066 return al;
1067}
1068
1069/*
1070 * Allocating the disasm annotation line data with
1071 * following structure:
1072 *
1073 * ------------------------------------------------------------
1074 * privsize space | struct disasm_line | struct annotation_line
1075 * ------------------------------------------------------------
1076 *
1077 * We have 'struct annotation_line' member as last member
1078 * of 'struct disasm_line' to have an easy access.
1079 *
1080 */
4748834f 1081static struct disasm_line *disasm_line__new(struct annotate_args *args)
78f7defe 1082{
c835e191
JO
1083 struct disasm_line *dl = NULL;
1084 struct annotation_line *al;
1085 size_t privsize = args->privsize + offsetof(struct disasm_line, al);
78f7defe 1086
c835e191
JO
1087 al = annotation_line__new(args, privsize);
1088 if (al != NULL) {
1089 dl = disasm_line(al);
d5490b96
JO
1090
1091 if (dl->al.line == NULL)
058b4cc9 1092 goto out_delete;
5145418b 1093
4748834f 1094 if (args->offset != -1) {
d5490b96 1095 if (disasm_line__parse(dl->al.line, &dl->ins.name, &dl->ops.raw) < 0)
5145418b
ACM
1096 goto out_free_line;
1097
85a84e4f 1098 disasm_line__init_ins(dl, args->arch, &args->ms);
5145418b 1099 }
78f7defe
ACM
1100 }
1101
29ed6e76 1102 return dl;
5145418b
ACM
1103
1104out_free_line:
d5490b96 1105 zfree(&dl->al.line);
058b4cc9 1106out_delete:
29ed6e76 1107 free(dl);
058b4cc9 1108 return NULL;
78f7defe
ACM
1109}
1110
29ed6e76 1111void disasm_line__free(struct disasm_line *dl)
78f7defe 1112{
75b49202
ACM
1113 if (dl->ins.ops && dl->ins.ops->free)
1114 dl->ins.ops->free(&dl->ops);
c46219ac
ACM
1115 else
1116 ins__delete(&dl->ops);
75b49202
ACM
1117 free((void *)dl->ins.name);
1118 dl->ins.name = NULL;
c835e191 1119 annotation_line__delete(&dl->al);
78f7defe
ACM
1120}
1121
5417072b
ACM
1122int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size, bool raw)
1123{
75b49202 1124 if (raw || !dl->ins.ops)
648388ae 1125 return scnprintf(bf, size, "%-6s %s", dl->ins.name, dl->ops.raw);
5417072b 1126
75b49202 1127 return ins__scnprintf(&dl->ins, bf, size, &dl->ops);
5417072b
ACM
1128}
1129
82b9d7ff 1130static void annotation_line__add(struct annotation_line *al, struct list_head *head)
78f7defe 1131{
82b9d7ff 1132 list_add_tail(&al->node, head);
78f7defe
ACM
1133}
1134
c4c72436
JO
1135struct annotation_line *
1136annotation_line__next(struct annotation_line *pos, struct list_head *head)
78f7defe 1137{
c4c72436
JO
1138 list_for_each_entry_continue(pos, head, node)
1139 if (pos->offset >= 0)
78f7defe
ACM
1140 return pos;
1141
1142 return NULL;
1143}
1144
70fbe057
PZ
1145static const char *annotate__address_color(struct block_range *br)
1146{
1147 double cov = block_range__coverage(br);
1148
1149 if (cov >= 0) {
1150 /* mark red for >75% coverage */
1151 if (cov > 0.75)
1152 return PERF_COLOR_RED;
1153
1154 /* mark dull for <1% coverage */
1155 if (cov < 0.01)
1156 return PERF_COLOR_NORMAL;
1157 }
1158
1159 return PERF_COLOR_MAGENTA;
1160}
1161
1162static const char *annotate__asm_color(struct block_range *br)
1163{
1164 double cov = block_range__coverage(br);
1165
1166 if (cov >= 0) {
1167 /* mark dull for <1% coverage */
1168 if (cov < 0.01)
1169 return PERF_COLOR_NORMAL;
1170 }
1171
1172 return PERF_COLOR_BLUE;
1173}
1174
1175static void annotate__branch_printf(struct block_range *br, u64 addr)
1176{
1177 bool emit_comment = true;
1178
1179 if (!br)
1180 return;
1181
1182#if 1
1183 if (br->is_target && br->start == addr) {
1184 struct block_range *branch = br;
1185 double p;
1186
1187 /*
1188 * Find matching branch to our target.
1189 */
1190 while (!branch->is_branch)
1191 branch = block_range__next(branch);
1192
1193 p = 100 *(double)br->entry / branch->coverage;
1194
1195 if (p > 0.1) {
1196 if (emit_comment) {
1197 emit_comment = false;
1198 printf("\t#");
1199 }
1200
1201 /*
1202 * The percentage of coverage joined at this target in relation
1203 * to the next branch.
1204 */
1205 printf(" +%.2f%%", p);
1206 }
1207 }
1208#endif
1209 if (br->is_branch && br->end == addr) {
1210 double p = 100*(double)br->taken / br->coverage;
1211
1212 if (p > 0.1) {
1213 if (emit_comment) {
1214 emit_comment = false;
1215 printf("\t#");
1216 }
1217
1218 /*
1219 * The percentage of coverage leaving at this branch, and
1220 * its prediction ratio.
1221 */
1222 printf(" -%.2f%% (p:%.2f%%)", p, 100*(double)br->pred / br->taken);
1223 }
1224 }
1225}
1226
f48e7c40 1227static int disasm_line__print(struct disasm_line *dl, u64 start, int addr_fmt_width)
78f7defe 1228{
29971f9a
JO
1229 s64 offset = dl->al.offset;
1230 const u64 addr = start + offset;
1231 struct block_range *br;
1232
1233 br = block_range__find(addr);
f48e7c40 1234 color_fprintf(stdout, annotate__address_color(br), " %*" PRIx64 ":", addr_fmt_width, addr);
29971f9a
JO
1235 color_fprintf(stdout, annotate__asm_color(br), "%s", dl->al.line);
1236 annotate__branch_printf(br, addr);
1237 return 0;
1238}
1239
1240static int
1241annotation_line__print(struct annotation_line *al, struct symbol *sym, u64 start,
1242 struct perf_evsel *evsel, u64 len, int min_pcnt, int printed,
f48e7c40 1243 int max_lines, struct annotation_line *queue, int addr_fmt_width)
29971f9a
JO
1244{
1245 struct disasm_line *dl = container_of(al, struct disasm_line, al);
78f7defe
ACM
1246 static const char *prev_line;
1247 static const char *prev_color;
1248
29971f9a 1249 if (al->offset != -1) {
f681d593 1250 double max_percent = 0.0;
b1dd4432 1251 int i, nr_percent = 1;
78f7defe
ACM
1252 const char *color;
1253 struct annotation *notes = symbol__annotation(sym);
ce6f4fab 1254
29971f9a
JO
1255 for (i = 0; i < al->samples_nr; i++) {
1256 struct annotation_data *sample = &al->samples[i];
78f7defe 1257
f681d593
JO
1258 if (sample->percent > max_percent)
1259 max_percent = sample->percent;
b1dd4432
NK
1260 }
1261
1262 if (max_percent < min_pcnt)
36532461
ACM
1263 return -1;
1264
e3087b80 1265 if (max_lines && printed >= max_lines)
36532461 1266 return 1;
d040bd36 1267
d5e3d747 1268 if (queue != NULL) {
29971f9a
JO
1269 list_for_each_entry_from(queue, &notes->src->source, node) {
1270 if (queue == al)
d5e3d747 1271 break;
29971f9a 1272 annotation_line__print(queue, sym, start, evsel, len,
f48e7c40 1273 0, 0, 1, NULL, addr_fmt_width);
d5e3d747
ACM
1274 }
1275 }
1276
b1dd4432 1277 color = get_percent_color(max_percent);
78f7defe
ACM
1278
1279 /*
1280 * Also color the filename and line if needed, with
1281 * the same color than the percentage. Don't print it
1282 * twice for close colored addr with the same filename:line
1283 */
29971f9a
JO
1284 if (al->path) {
1285 if (!prev_line || strcmp(prev_line, al->path)
78f7defe 1286 || color != prev_color) {
29971f9a
JO
1287 color_fprintf(stdout, color, " %s", al->path);
1288 prev_line = al->path;
78f7defe
ACM
1289 prev_color = color;
1290 }
1291 }
1292
b1dd4432 1293 for (i = 0; i < nr_percent; i++) {
29971f9a 1294 struct annotation_data *sample = &al->samples[i];
f681d593
JO
1295
1296 color = get_percent_color(sample->percent);
0c4a5bce
ML
1297
1298 if (symbol_conf.show_total_period)
ce9ee4a2 1299 color_fprintf(stdout, color, " %11" PRIu64,
f681d593 1300 sample->he.period);
1ac39372
TS
1301 else if (symbol_conf.show_nr_samples)
1302 color_fprintf(stdout, color, " %7" PRIu64,
f681d593 1303 sample->he.nr_samples);
0c4a5bce 1304 else
f681d593 1305 color_fprintf(stdout, color, " %7.2f", sample->percent);
b1dd4432
NK
1306 }
1307
f48e7c40 1308 printf(" : ");
70fbe057 1309
f48e7c40 1310 disasm_line__print(dl, start, addr_fmt_width);
70fbe057 1311 printf("\n");
e3087b80 1312 } else if (max_lines && printed >= max_lines)
36532461
ACM
1313 return 1;
1314 else {
ce9ee4a2 1315 int width = symbol_conf.show_total_period ? 12 : 8;
b1dd4432 1316
d5e3d747
ACM
1317 if (queue)
1318 return -1;
1319
759ff497 1320 if (perf_evsel__is_group_event(evsel))
b1dd4432
NK
1321 width *= evsel->nr_members;
1322
29971f9a 1323 if (!*al->line)
b1dd4432 1324 printf(" %*s:\n", width, " ");
78f7defe 1325 else
f48e7c40 1326 printf(" %*s: %*s %s\n", width, " ", addr_fmt_width, " ", al->line);
78f7defe 1327 }
36532461
ACM
1328
1329 return 0;
78f7defe
ACM
1330}
1331
3aec150a
NK
1332/*
1333 * symbol__parse_objdump_line() parses objdump output (with -d --no-show-raw)
1334 * which looks like following
1335 *
1336 * 0000000000415500 <_init>:
1337 * 415500: sub $0x8,%rsp
1338 * 415504: mov 0x2f5ad5(%rip),%rax # 70afe0 <_DYNAMIC+0x2f8>
1339 * 41550b: test %rax,%rax
1340 * 41550e: je 415515 <_init+0x15>
1341 * 415510: callq 416e70 <__gmon_start__@plt>
1342 * 415515: add $0x8,%rsp
1343 * 415519: retq
1344 *
1345 * it will be parsed and saved into struct disasm_line as
1346 * <offset> <name> <ops.raw>
1347 *
1348 * The offset will be a relative offset from the start of the symbol and -1
1349 * means that it's not a disassembly line so should be treated differently.
1350 * The ops.raw part will be parsed further according to type of the instruction.
1351 */
1a04db70 1352static int symbol__parse_objdump_line(struct symbol *sym, FILE *file,
ea07c5aa 1353 struct annotate_args *args,
e592488c 1354 int *line_nr)
78f7defe 1355{
85a84e4f 1356 struct map *map = args->ms.map;
ce6f4fab 1357 struct annotation *notes = symbol__annotation(sym);
29ed6e76 1358 struct disasm_line *dl;
4597cf06 1359 char *line = NULL, *parsed_line, *tmp, *tmp2;
78f7defe
ACM
1360 size_t line_len;
1361 s64 line_ip, offset = -1;
e592488c 1362 regmatch_t match[2];
78f7defe
ACM
1363
1364 if (getline(&line, &line_len, file) < 0)
1365 return -1;
1366
1367 if (!line)
1368 return -1;
1369
78f7defe 1370 line_ip = -1;
4597cf06 1371 parsed_line = rtrim(line);
78f7defe 1372
e592488c 1373 /* /filename:linenr ? Save line number and ignore. */
986a5bc0
TS
1374 if (regexec(&file_lineno, parsed_line, 2, match, 0) == 0) {
1375 *line_nr = atoi(parsed_line + match[1].rm_so);
e592488c
AK
1376 return 0;
1377 }
1378
4597cf06 1379 tmp = ltrim(parsed_line);
78f7defe
ACM
1380 if (*tmp) {
1381 /*
1382 * Parse hexa addresses followed by ':'
1383 */
1384 line_ip = strtoull(tmp, &tmp2, 16);
1385 if (*tmp2 != ':' || tmp == tmp2 || tmp2[1] == '\0')
1386 line_ip = -1;
1387 }
1388
1389 if (line_ip != -1) {
1390 u64 start = map__rip_2objdump(map, sym->start),
1391 end = map__rip_2objdump(map, sym->end);
1392
1393 offset = line_ip - start;
2c241bd3 1394 if ((u64)line_ip < start || (u64)line_ip >= end)
78f7defe 1395 offset = -1;
058b4cc9
ACM
1396 else
1397 parsed_line = tmp2 + 1;
a31b7cc0 1398 }
78f7defe 1399
4748834f
JO
1400 args->offset = offset;
1401 args->line = parsed_line;
1402 args->line_nr = *line_nr;
85a84e4f 1403 args->ms.sym = sym;
4748834f
JO
1404
1405 dl = disasm_line__new(args);
058b4cc9 1406 free(line);
e592488c 1407 (*line_nr)++;
058b4cc9 1408
29ed6e76 1409 if (dl == NULL)
78f7defe 1410 return -1;
058b4cc9 1411
2eff0611 1412 if (!disasm_line__has_local_offset(dl)) {
bbb7f846
AH
1413 dl->ops.target.offset = dl->ops.target.addr -
1414 map__rip_2objdump(map, sym->start);
e216874c
RB
1415 dl->ops.target.offset_avail = true;
1416 }
bbb7f846 1417
696703af
ACM
1418 /* kcore has no symbols, so add the call target symbol */
1419 if (dl->ins.ops && ins__is_call(&dl->ins) && !dl->ops.target.sym) {
6e427ab0
AH
1420 struct addr_map_symbol target = {
1421 .map = map,
1422 .addr = dl->ops.target.addr,
1423 };
1424
be39db9f 1425 if (!map_groups__find_ams(&target) &&
6e427ab0 1426 target.sym->start == target.al_addr)
696703af 1427 dl->ops.target.sym = target.sym;
b178170a
AH
1428 }
1429
82b9d7ff 1430 annotation_line__add(&dl->al, &notes->src->source);
78f7defe
ACM
1431
1432 return 0;
1433}
1434
e592488c
AK
1435static __attribute__((constructor)) void symbol__init_regexpr(void)
1436{
1437 regcomp(&file_lineno, "^/[^:]+:([0-9]+)", REG_EXTENDED);
1438}
1439
484a5e74
AH
1440static void delete_last_nop(struct symbol *sym)
1441{
1442 struct annotation *notes = symbol__annotation(sym);
1443 struct list_head *list = &notes->src->source;
1444 struct disasm_line *dl;
1445
1446 while (!list_empty(list)) {
a17c4ca0 1447 dl = list_entry(list->prev, struct disasm_line, al.node);
484a5e74 1448
75b49202
ACM
1449 if (dl->ins.ops) {
1450 if (dl->ins.ops != &nop_ops)
484a5e74
AH
1451 return;
1452 } else {
d5490b96
JO
1453 if (!strstr(dl->al.line, " nop ") &&
1454 !strstr(dl->al.line, " nopl ") &&
1455 !strstr(dl->al.line, " nopw "))
484a5e74
AH
1456 return;
1457 }
1458
a17c4ca0 1459 list_del(&dl->al.node);
484a5e74
AH
1460 disasm_line__free(dl);
1461 }
1462}
1463
ee51d851
ACM
1464int symbol__strerror_disassemble(struct symbol *sym __maybe_unused, struct map *map,
1465 int errnum, char *buf, size_t buflen)
1466{
1467 struct dso *dso = map->dso;
1468
1469 BUG_ON(buflen == 0);
1470
1471 if (errnum >= 0) {
1472 str_error_r(errnum, buf, buflen);
1473 return 0;
1474 }
1475
1476 switch (errnum) {
1477 case SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX: {
1478 char bf[SBUILD_ID_SIZE + 15] = " with build id ";
1479 char *build_id_msg = NULL;
1480
1481 if (dso->has_build_id) {
1482 build_id__sprintf(dso->build_id,
1483 sizeof(dso->build_id), bf + 15);
1484 build_id_msg = bf;
1485 }
1486 scnprintf(buf, buflen,
1487 "No vmlinux file%s\nwas found in the path.\n\n"
1488 "Note that annotation using /proc/kcore requires CAP_SYS_RAWIO capability.\n\n"
1489 "Please use:\n\n"
1490 " perf buildid-cache -vu vmlinux\n\n"
1491 "or:\n\n"
1492 " --vmlinux vmlinux\n", build_id_msg ?: "");
1493 }
1494 break;
1495 default:
1496 scnprintf(buf, buflen, "Internal error: Invalid %d error code\n", errnum);
1497 break;
1498 }
1499
1500 return 0;
1501}
1502
05ed3ac9 1503static int dso__disassemble_filename(struct dso *dso, char *filename, size_t filename_size)
78f7defe 1504{
05ed3ac9
ACM
1505 char linkname[PATH_MAX];
1506 char *build_id_filename;
6ebd2547 1507 char *build_id_path = NULL;
3619ef76 1508 char *pos;
78f7defe 1509
c12944f7
ACM
1510 if (dso->symtab_type == DSO_BINARY_TYPE__KALLSYMS &&
1511 !dso__is_kcore(dso))
05ed3ac9 1512 return SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX;
c12944f7 1513
d2396999 1514 build_id_filename = dso__build_id_filename(dso, NULL, 0, false);
05ed3ac9
ACM
1515 if (build_id_filename) {
1516 __symbol__join_symfs(filename, filename_size, build_id_filename);
1517 free(build_id_filename);
3caee094 1518 } else {
ee51d851
ACM
1519 if (dso->has_build_id)
1520 return ENOMEM;
78f7defe 1521 goto fallback;
3caee094
ACM
1522 }
1523
6ebd2547
TS
1524 build_id_path = strdup(filename);
1525 if (!build_id_path)
1526 return -1;
1527
3619ef76
NK
1528 /*
1529 * old style build-id cache has name of XX/XXXXXXX.. while
1530 * new style has XX/XXXXXXX../{elf,kallsyms,vdso}.
1531 * extract the build-id part of dirname in the new style only.
1532 */
1533 pos = strrchr(build_id_path, '/');
1534 if (pos && strlen(pos) < SBUILD_ID_SIZE - 2)
1535 dirname(build_id_path);
6ebd2547 1536
3caee094 1537 if (dso__is_kcore(dso) ||
6ebd2547 1538 readlink(build_id_path, linkname, sizeof(linkname)) < 0 ||
05ed3ac9
ACM
1539 strstr(linkname, DSO__NAME_KALLSYMS) ||
1540 access(filename, R_OK)) {
78f7defe
ACM
1541fallback:
1542 /*
1543 * If we don't have build-ids or the build-id file isn't in the
1544 * cache, or is just a kallsyms file, well, lets hope that this
1545 * DSO is the same as when 'perf record' ran.
1546 */
05ed3ac9 1547 __symbol__join_symfs(filename, filename_size, dso->long_name);
78f7defe
ACM
1548 }
1549
6ebd2547 1550 free(build_id_path);
05ed3ac9
ACM
1551 return 0;
1552}
1553
1a04db70 1554static int symbol__disassemble(struct symbol *sym, struct annotate_args *args)
05ed3ac9 1555{
85a84e4f 1556 struct map *map = args->ms.map;
05ed3ac9 1557 struct dso *dso = map->dso;
6810158d 1558 char *command;
05ed3ac9
ACM
1559 FILE *file;
1560 char symfs_filename[PATH_MAX];
1561 struct kcore_extract kce;
1562 bool delete_extract = false;
1563 int stdout_fd[2];
1564 int lineno = 0;
1565 int nline;
1566 pid_t pid;
1567 int err = dso__disassemble_filename(dso, symfs_filename, sizeof(symfs_filename));
1568
1569 if (err)
1570 return err;
1571
78f7defe 1572 pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__,
3caee094 1573 symfs_filename, sym->name, map->unmap_ip(map, sym->start),
78f7defe
ACM
1574 map->unmap_ip(map, sym->end));
1575
78f7defe
ACM
1576 pr_debug("annotating [%p] %30s : [%p] %30s\n",
1577 dso, dso->long_name, sym, sym->name);
1578
afba19d9
AH
1579 if (dso__is_kcore(dso)) {
1580 kce.kcore_filename = symfs_filename;
1581 kce.addr = map__rip_2objdump(map, sym->start);
1582 kce.offs = sym->start;
2c241bd3 1583 kce.len = sym->end - sym->start;
afba19d9
AH
1584 if (!kcore_extract__create(&kce)) {
1585 delete_extract = true;
1586 strlcpy(symfs_filename, kce.extract_filename,
1587 sizeof(symfs_filename));
afba19d9 1588 }
2c7da8c5 1589 } else if (dso__needs_decompress(dso)) {
3c84fd53 1590 char tmp[KMOD_DECOMP_LEN];
2c7da8c5 1591
3c84fd53
NK
1592 if (dso__decompress_kmodule_path(dso, symfs_filename,
1593 tmp, sizeof(tmp)) < 0)
3caee094 1594 goto out;
2c7da8c5
JO
1595
1596 strcpy(symfs_filename, tmp);
afba19d9
AH
1597 }
1598
6810158d 1599 err = asprintf(&command,
7a4ec938 1600 "%s %s%s --start-address=0x%016" PRIx64
3e6a2a7f 1601 " --stop-address=0x%016" PRIx64
7b4500bc 1602 " -l -d %s %s -C \"%s\" 2>/dev/null|grep -v \"%s:\"|expand",
7a4ec938 1603 objdump_path ? objdump_path : "objdump",
f69b64f7
AK
1604 disassembler_style ? "-M " : "",
1605 disassembler_style ? disassembler_style : "",
78f7defe 1606 map__rip_2objdump(map, sym->start),
2c241bd3 1607 map__rip_2objdump(map, sym->end),
3e6a2a7f
SE
1608 symbol_conf.annotate_asm_raw ? "" : "--no-show-raw",
1609 symbol_conf.annotate_src ? "-S" : "",
3caee094 1610 symfs_filename, symfs_filename);
78f7defe 1611
6810158d
ACM
1612 if (err < 0) {
1613 pr_err("Failure allocating memory for the command to run\n");
1614 goto out_remove_tmp;
1615 }
1616
78f7defe
ACM
1617 pr_debug("Executing: %s\n", command);
1618
9955d0be
ACM
1619 err = -1;
1620 if (pipe(stdout_fd) < 0) {
1621 pr_err("Failure creating the pipe to run %s\n", command);
6810158d 1622 goto out_free_command;
9955d0be
ACM
1623 }
1624
1625 pid = fork();
1626 if (pid < 0) {
1627 pr_err("Failure forking to run %s\n", command);
1628 goto out_close_stdout;
1629 }
1630
1631 if (pid == 0) {
1632 close(stdout_fd[0]);
1633 dup2(stdout_fd[1], 1);
1634 close(stdout_fd[1]);
1635 execl("/bin/sh", "sh", "-c", command, NULL);
1636 perror(command);
1637 exit(-1);
1638 }
1639
1640 close(stdout_fd[1]);
1641
1642 file = fdopen(stdout_fd[0], "r");
62ec9b3f 1643 if (!file) {
9955d0be 1644 pr_err("Failure creating FILE stream for %s\n", command);
62ec9b3f
AK
1645 /*
1646 * If we were using debug info should retry with
1647 * original binary.
1648 */
6810158d 1649 goto out_free_command;
62ec9b3f 1650 }
78f7defe 1651
62ec9b3f
AK
1652 nline = 0;
1653 while (!feof(file)) {
ed7b339f
ACM
1654 /*
1655 * The source code line number (lineno) needs to be kept in
1656 * accross calls to symbol__parse_objdump_line(), so that it
1657 * can associate it with the instructions till the next one.
1658 * See disasm_line__new() and struct disasm_line::line_nr.
1659 */
1a04db70 1660 if (symbol__parse_objdump_line(sym, file, args, &lineno) < 0)
78f7defe 1661 break;
62ec9b3f
AK
1662 nline++;
1663 }
1664
1665 if (nline == 0)
1666 pr_err("No output from %s\n", command);
78f7defe 1667
484a5e74
AH
1668 /*
1669 * kallsyms does not have symbol sizes so there may a nop at the end.
1670 * Remove it.
1671 */
1672 if (dso__is_kcore(dso))
1673 delete_last_nop(sym);
1674
9955d0be
ACM
1675 fclose(file);
1676 err = 0;
6810158d
ACM
1677out_free_command:
1678 free(command);
2c7da8c5 1679out_remove_tmp:
9955d0be
ACM
1680 close(stdout_fd[0]);
1681
2c7da8c5
JO
1682 if (dso__needs_decompress(dso))
1683 unlink(symfs_filename);
3caee094 1684
afba19d9
AH
1685 if (delete_extract)
1686 kcore_extract__delete(&kce);
c12944f7 1687out:
78f7defe 1688 return err;
9955d0be
ACM
1689
1690out_close_stdout:
1691 close(stdout_fd[1]);
6810158d 1692 goto out_free_command;
78f7defe
ACM
1693}
1694
073ae601
JO
1695static void calc_percent(struct sym_hist *hist,
1696 struct annotation_data *sample,
1697 s64 offset, s64 end)
1698{
1699 unsigned int hits = 0;
1700 u64 period = 0;
1701
1702 while (offset < end) {
1703 hits += hist->addr[offset].nr_samples;
1704 period += hist->addr[offset].period;
1705 ++offset;
1706 }
1707
1708 if (hist->nr_samples) {
1709 sample->he.period = period;
1710 sample->he.nr_samples = hits;
1711 sample->percent = 100.0 * hits / hist->nr_samples;
1712 }
1713}
1714
9e4e0a9d
JO
1715static void annotation__calc_percent(struct annotation *notes,
1716 struct perf_evsel *evsel, s64 len)
073ae601
JO
1717{
1718 struct annotation_line *al, *next;
1719
1720 list_for_each_entry(al, &notes->src->source, node) {
1721 s64 end;
1722 int i;
1723
1724 if (al->offset == -1)
1725 continue;
1726
1727 next = annotation_line__next(al, &notes->src->source);
1728 end = next ? next->offset : len;
1729
1730 for (i = 0; i < al->samples_nr; i++) {
1731 struct annotation_data *sample;
1732 struct sym_hist *hist;
1733
1734 hist = annotation__histogram(notes, evsel->idx + i);
1735 sample = &al->samples[i];
1736
1737 calc_percent(hist, sample, al->offset, end);
1738 }
1739 }
073ae601
JO
1740}
1741
9e4e0a9d 1742void symbol__calc_percent(struct symbol *sym, struct perf_evsel *evsel)
073ae601
JO
1743{
1744 struct annotation *notes = symbol__annotation(sym);
1745
9e4e0a9d 1746 annotation__calc_percent(notes, evsel, symbol__size(sym));
073ae601
JO
1747}
1748
c34df25b 1749int symbol__annotate(struct symbol *sym, struct map *map,
d03a686e 1750 struct perf_evsel *evsel, size_t privsize,
5449f13c 1751 struct arch **parch)
c34df25b 1752{
ea07c5aa
JO
1753 struct annotate_args args = {
1754 .privsize = privsize,
d03a686e 1755 .evsel = evsel,
ea07c5aa 1756 };
5449f13c 1757 struct perf_env *env = perf_evsel__env(evsel);
3285deba 1758 const char *arch_name = perf_env__arch(env);
c34df25b
JO
1759 struct arch *arch;
1760 int err;
1761
c34df25b
JO
1762 if (!arch_name)
1763 return -1;
1764
24fe7b88 1765 args.arch = arch = arch__find(arch_name);
c34df25b
JO
1766 if (arch == NULL)
1767 return -ENOTSUP;
1768
1769 if (parch)
1770 *parch = arch;
1771
1772 if (arch->init) {
5449f13c 1773 err = arch->init(arch, env ? env->cpuid : NULL);
c34df25b
JO
1774 if (err) {
1775 pr_err("%s: failed to initialize %s arch priv area\n", __func__, arch->name);
1776 return err;
1777 }
1778 }
1779
85a84e4f
ACM
1780 args.ms.map = map;
1781 args.ms.sym = sym;
1782
05d3f1a1 1783 return symbol__disassemble(sym, &args);
c34df25b
JO
1784}
1785
8b4c74dc 1786static void insert_source_line(struct rb_root *root, struct annotation_line *al)
78f7defe 1787{
8b4c74dc 1788 struct annotation_line *iter;
78f7defe
ACM
1789 struct rb_node **p = &root->rb_node;
1790 struct rb_node *parent = NULL;
1491c22a 1791 int i, ret;
78f7defe
ACM
1792
1793 while (*p != NULL) {
1794 parent = *p;
8b4c74dc 1795 iter = rb_entry(parent, struct annotation_line, rb_node);
78f7defe 1796
8b4c74dc 1797 ret = strcmp(iter->path, al->path);
41127965 1798 if (ret == 0) {
8b4c74dc
JO
1799 for (i = 0; i < al->samples_nr; i++)
1800 iter->samples[i].percent_sum += al->samples[i].percent;
41127965
NK
1801 return;
1802 }
1803
1804 if (ret < 0)
1805 p = &(*p)->rb_left;
1806 else
1807 p = &(*p)->rb_right;
1808 }
1809
8b4c74dc
JO
1810 for (i = 0; i < al->samples_nr; i++)
1811 al->samples[i].percent_sum = al->samples[i].percent;
41127965 1812
8b4c74dc
JO
1813 rb_link_node(&al->rb_node, parent, p);
1814 rb_insert_color(&al->rb_node, root);
41127965
NK
1815}
1816
8b4c74dc 1817static int cmp_source_line(struct annotation_line *a, struct annotation_line *b)
1491c22a
NK
1818{
1819 int i;
1820
8b4c74dc 1821 for (i = 0; i < a->samples_nr; i++) {
276af92f 1822 if (a->samples[i].percent_sum == b->samples[i].percent_sum)
1491c22a 1823 continue;
276af92f 1824 return a->samples[i].percent_sum > b->samples[i].percent_sum;
1491c22a
NK
1825 }
1826
1827 return 0;
1828}
1829
8b4c74dc 1830static void __resort_source_line(struct rb_root *root, struct annotation_line *al)
41127965 1831{
8b4c74dc 1832 struct annotation_line *iter;
41127965
NK
1833 struct rb_node **p = &root->rb_node;
1834 struct rb_node *parent = NULL;
1835
1836 while (*p != NULL) {
1837 parent = *p;
8b4c74dc 1838 iter = rb_entry(parent, struct annotation_line, rb_node);
41127965 1839
8b4c74dc 1840 if (cmp_source_line(al, iter))
78f7defe
ACM
1841 p = &(*p)->rb_left;
1842 else
1843 p = &(*p)->rb_right;
1844 }
1845
8b4c74dc
JO
1846 rb_link_node(&al->rb_node, parent, p);
1847 rb_insert_color(&al->rb_node, root);
78f7defe
ACM
1848}
1849
41127965
NK
1850static void resort_source_line(struct rb_root *dest_root, struct rb_root *src_root)
1851{
8b4c74dc 1852 struct annotation_line *al;
41127965
NK
1853 struct rb_node *node;
1854
1855 node = rb_first(src_root);
1856 while (node) {
1857 struct rb_node *next;
1858
8b4c74dc 1859 al = rb_entry(node, struct annotation_line, rb_node);
41127965
NK
1860 next = rb_next(node);
1861 rb_erase(node, src_root);
1862
8b4c74dc 1863 __resort_source_line(dest_root, al);
41127965
NK
1864 node = next;
1865 }
1866}
1867
78f7defe
ACM
1868static void print_summary(struct rb_root *root, const char *filename)
1869{
8b4c74dc 1870 struct annotation_line *al;
78f7defe
ACM
1871 struct rb_node *node;
1872
1873 printf("\nSorted summary for file %s\n", filename);
1874 printf("----------------------------------------------\n\n");
1875
1876 if (RB_EMPTY_ROOT(root)) {
1877 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
1878 return;
1879 }
1880
1881 node = rb_first(root);
1882 while (node) {
1491c22a 1883 double percent, percent_max = 0.0;
78f7defe
ACM
1884 const char *color;
1885 char *path;
1491c22a 1886 int i;
78f7defe 1887
8b4c74dc
JO
1888 al = rb_entry(node, struct annotation_line, rb_node);
1889 for (i = 0; i < al->samples_nr; i++) {
1890 percent = al->samples[i].percent_sum;
1491c22a
NK
1891 color = get_percent_color(percent);
1892 color_fprintf(stdout, color, " %7.2f", percent);
1893
1894 if (percent > percent_max)
1895 percent_max = percent;
1896 }
1897
8b4c74dc 1898 path = al->path;
1491c22a 1899 color = get_percent_color(percent_max);
f048d548 1900 color_fprintf(stdout, color, " %s\n", path);
78f7defe 1901
78f7defe
ACM
1902 node = rb_next(node);
1903 }
1904}
1905
db8fd07a 1906static void symbol__annotate_hits(struct symbol *sym, struct perf_evsel *evsel)
78f7defe
ACM
1907{
1908 struct annotation *notes = symbol__annotation(sym);
db8fd07a 1909 struct sym_hist *h = annotation__histogram(notes, evsel->idx);
1b2e2df4 1910 u64 len = symbol__size(sym), offset;
78f7defe
ACM
1911
1912 for (offset = 0; offset < len; ++offset)
896bccd3 1913 if (h->addr[offset].nr_samples != 0)
78f7defe 1914 printf("%*" PRIx64 ": %" PRIu64 "\n", BITS_PER_LONG / 2,
896bccd3 1915 sym->start + offset, h->addr[offset].nr_samples);
8158683d 1916 printf("%*s: %" PRIu64 "\n", BITS_PER_LONG / 2, "h->nr_samples", h->nr_samples);
78f7defe
ACM
1917}
1918
f48e7c40
JO
1919static int annotated_source__addr_fmt_width(struct list_head *lines, u64 start)
1920{
1921 char bf[32];
1922 struct annotation_line *line;
1923
1924 list_for_each_entry_reverse(line, lines, node) {
1925 if (line->offset != -1)
1926 return scnprintf(bf, sizeof(bf), "%" PRIx64, start + line->offset);
1927 }
1928
1929 return 0;
1930}
1931
db8fd07a
NK
1932int symbol__annotate_printf(struct symbol *sym, struct map *map,
1933 struct perf_evsel *evsel, bool full_paths,
1934 int min_pcnt, int max_lines, int context)
78f7defe
ACM
1935{
1936 struct dso *dso = map->dso;
bfd14b9a
DA
1937 char *filename;
1938 const char *d_filename;
9cdbadce 1939 const char *evsel_name = perf_evsel__name(evsel);
ce6f4fab 1940 struct annotation *notes = symbol__annotation(sym);
135cce1b 1941 struct sym_hist *h = annotation__histogram(notes, evsel->idx);
8f25b819 1942 struct annotation_line *pos, *queue = NULL;
058b4cc9 1943 u64 start = map__rip_2objdump(map, sym->start);
f48e7c40 1944 int printed = 2, queue_len = 0, addr_fmt_width;
36532461 1945 int more = 0;
78f7defe 1946 u64 len;
ce9ee4a2 1947 int width = symbol_conf.show_total_period ? 12 : 8;
53dd9b5f 1948 int graph_dotted_len;
78f7defe 1949
bfd14b9a
DA
1950 filename = strdup(dso->long_name);
1951 if (!filename)
1952 return -ENOMEM;
1953
78f7defe
ACM
1954 if (full_paths)
1955 d_filename = filename;
1956 else
1957 d_filename = basename(filename);
1958
1b2e2df4 1959 len = symbol__size(sym);
b1dd4432 1960
759ff497 1961 if (perf_evsel__is_group_event(evsel))
b1dd4432 1962 width *= evsel->nr_members;
78f7defe 1963
135cce1b 1964 graph_dotted_len = printf(" %-*.*s| Source code & Disassembly of %s for %s (%" PRIu64 " samples)\n",
1ac39372
TS
1965 width, width, symbol_conf.show_total_period ? "Period" :
1966 symbol_conf.show_nr_samples ? "Samples" : "Percent",
38d2dcd0 1967 d_filename, evsel_name, h->nr_samples);
9cdbadce 1968
53dd9b5f 1969 printf("%-*.*s----\n",
9cdbadce 1970 graph_dotted_len, graph_dotted_len, graph_dotted_line);
78f7defe 1971
bb963e16 1972 if (verbose > 0)
db8fd07a 1973 symbol__annotate_hits(sym, evsel);
78f7defe 1974
f48e7c40
JO
1975 addr_fmt_width = annotated_source__addr_fmt_width(&notes->src->source, start);
1976
8f25b819
JO
1977 list_for_each_entry(pos, &notes->src->source, node) {
1978 int err;
1979
d5e3d747
ACM
1980 if (context && queue == NULL) {
1981 queue = pos;
1982 queue_len = 0;
1983 }
1984
8f25b819
JO
1985 err = annotation_line__print(pos, sym, start, evsel, len,
1986 min_pcnt, printed, max_lines,
f48e7c40 1987 queue, addr_fmt_width);
8f25b819
JO
1988
1989 switch (err) {
36532461
ACM
1990 case 0:
1991 ++printed;
d5e3d747
ACM
1992 if (context) {
1993 printed += queue_len;
1994 queue = NULL;
1995 queue_len = 0;
1996 }
36532461
ACM
1997 break;
1998 case 1:
1999 /* filtered by max_lines */
2000 ++more;
d040bd36 2001 break;
36532461
ACM
2002 case -1:
2003 default:
d5e3d747
ACM
2004 /*
2005 * Filtered by min_pcnt or non IP lines when
2006 * context != 0
2007 */
2008 if (!context)
2009 break;
2010 if (queue_len == context)
8f25b819 2011 queue = list_entry(queue->node.next, typeof(*queue), node);
d5e3d747
ACM
2012 else
2013 ++queue_len;
36532461
ACM
2014 break;
2015 }
2016 }
2017
bfd14b9a
DA
2018 free(filename);
2019
36532461
ACM
2020 return more;
2021}
f1e2701d 2022
befd2a38
ACM
2023static void FILE__set_percent_color(void *fp __maybe_unused,
2024 double percent __maybe_unused,
2025 bool current __maybe_unused)
2026{
2027}
2028
2029static int FILE__set_jumps_percent_color(void *fp __maybe_unused,
2030 int nr __maybe_unused, bool current __maybe_unused)
2031{
2032 return 0;
2033}
2034
2035static int FILE__set_color(void *fp __maybe_unused, int color __maybe_unused)
2036{
2037 return 0;
2038}
2039
2040static void FILE__printf(void *fp, const char *fmt, ...)
2041{
2042 va_list args;
2043
2044 va_start(args, fmt);
2045 vfprintf(fp, fmt, args);
2046 va_end(args);
2047}
2048
2049static void FILE__write_graph(void *fp, int graph)
2050{
2051 const char *s;
2052 switch (graph) {
2053
2054 case DARROW_CHAR: s = "↓"; break;
2055 case UARROW_CHAR: s = "↑"; break;
2056 case LARROW_CHAR: s = "←"; break;
2057 case RARROW_CHAR: s = "→"; break;
2058 default: s = "?"; break;
2059 }
2060
2061 fputs(s, fp);
2062}
2063
2064int symbol__annotate_fprintf2(struct symbol *sym, FILE *fp)
2065{
2066 struct annotation *notes = symbol__annotation(sym);
2067 struct annotation_write_ops ops = {
2068 .first_line = true,
2069 .obj = fp,
2070 .set_color = FILE__set_color,
2071 .set_percent_color = FILE__set_percent_color,
2072 .set_jumps_percent_color = FILE__set_jumps_percent_color,
2073 .printf = FILE__printf,
2074 .write_graph = FILE__write_graph,
2075 };
2076 struct annotation_line *al;
2077
2078 list_for_each_entry(al, &notes->src->source, node) {
2079 if (annotation_line__filter(al, notes))
2080 continue;
2081 annotation_line__write(al, notes, &ops);
2082 fputc('\n', fp);
2083 ops.first_line = false;
2084 }
2085
2086 return 0;
2087}
2088
d9bd7665
ACM
2089int map_symbol__annotation_dump(struct map_symbol *ms, struct perf_evsel *evsel)
2090{
2091 const char *ev_name = perf_evsel__name(evsel);
2092 char buf[1024];
2093 char *filename;
2094 int err = -1;
2095 FILE *fp;
2096
2097 if (asprintf(&filename, "%s.annotation", ms->sym->name) < 0)
2098 return -1;
2099
2100 fp = fopen(filename, "w");
2101 if (fp == NULL)
2102 goto out_free_filename;
2103
2104 if (perf_evsel__is_group_event(evsel)) {
2105 perf_evsel__group_desc(evsel, buf, sizeof(buf));
2106 ev_name = buf;
2107 }
2108
2109 fprintf(fp, "%s() %s\nEvent: %s\n\n",
2110 ms->sym->name, ms->map->dso->long_name, ev_name);
2111 symbol__annotate_fprintf2(ms->sym, fp);
2112
2113 fclose(fp);
2114 err = 0;
2115out_free_filename:
2116 free(filename);
2117 return err;
2118}
2119
36532461
ACM
2120void symbol__annotate_zero_histogram(struct symbol *sym, int evidx)
2121{
2122 struct annotation *notes = symbol__annotation(sym);
2123 struct sym_hist *h = annotation__histogram(notes, evidx);
2124
ce6f4fab 2125 memset(h, 0, notes->src->sizeof_sym_hist);
36532461
ACM
2126}
2127
ce6f4fab 2128void symbol__annotate_decay_histogram(struct symbol *sym, int evidx)
36532461
ACM
2129{
2130 struct annotation *notes = symbol__annotation(sym);
2131 struct sym_hist *h = annotation__histogram(notes, evidx);
1b2e2df4 2132 int len = symbol__size(sym), offset;
36532461 2133
8158683d 2134 h->nr_samples = 0;
8b84a568 2135 for (offset = 0; offset < len; ++offset) {
896bccd3 2136 h->addr[offset].nr_samples = h->addr[offset].nr_samples * 7 / 8;
8158683d 2137 h->nr_samples += h->addr[offset].nr_samples;
f1e2701d
ACM
2138 }
2139}
2140
f8eb37bd 2141void annotated_source__purge(struct annotated_source *as)
f1e2701d 2142{
f8eb37bd 2143 struct annotation_line *al, *n;
f1e2701d 2144
f8eb37bd
JO
2145 list_for_each_entry_safe(al, n, &as->source, node) {
2146 list_del(&al->node);
2147 disasm_line__free(disasm_line(al));
f1e2701d
ACM
2148 }
2149}
2150
5145418b
ACM
2151static size_t disasm_line__fprintf(struct disasm_line *dl, FILE *fp)
2152{
2153 size_t printed;
2154
d5490b96
JO
2155 if (dl->al.offset == -1)
2156 return fprintf(fp, "%s\n", dl->al.line);
5145418b 2157
d5490b96 2158 printed = fprintf(fp, "%#" PRIx64 " %s", dl->al.offset, dl->ins.name);
5145418b 2159
c7e6ead7 2160 if (dl->ops.raw[0] != '\0') {
5145418b 2161 printed += fprintf(fp, "%.*s %s\n", 6 - (int)printed, " ",
c7e6ead7 2162 dl->ops.raw);
5145418b
ACM
2163 }
2164
2165 return printed + fprintf(fp, "\n");
2166}
2167
2168size_t disasm__fprintf(struct list_head *head, FILE *fp)
2169{
2170 struct disasm_line *pos;
2171 size_t printed = 0;
2172
a17c4ca0 2173 list_for_each_entry(pos, head, al.node)
5145418b
ACM
2174 printed += disasm_line__fprintf(pos, fp);
2175
2176 return printed;
2177}
2178
2eff0611 2179bool disasm_line__is_valid_local_jump(struct disasm_line *dl, struct symbol *sym)
0db45bcf
ACM
2180{
2181 if (!dl || !dl->ins.ops || !ins__is_jump(&dl->ins) ||
2eff0611 2182 !disasm_line__has_local_offset(dl) || dl->ops.target.offset < 0 ||
0db45bcf
ACM
2183 dl->ops.target.offset >= (s64)symbol__size(sym))
2184 return false;
2185
2186 return true;
2187}
2188
2189void annotation__mark_jump_targets(struct annotation *notes, struct symbol *sym)
2190{
2191 u64 offset, size = symbol__size(sym);
2192
2193 /* PLT symbols contain external offsets */
2194 if (strstr(sym->name, "@plt"))
2195 return;
2196
2197 for (offset = 0; offset < size; ++offset) {
2198 struct annotation_line *al = notes->offsets[offset];
2199 struct disasm_line *dl;
2200
2201 dl = disasm_line(al);
2202
2eff0611 2203 if (!disasm_line__is_valid_local_jump(dl, sym))
0db45bcf
ACM
2204 continue;
2205
2206 al = notes->offsets[dl->ops.target.offset];
2207
2208 /*
2209 * FIXME: Oops, no jump target? Buggy disassembler? Or do we
2210 * have to adjust to the previous offset?
2211 */
2212 if (al == NULL)
2213 continue;
2214
2215 if (++al->jump_sources > notes->max_jump_sources)
2216 notes->max_jump_sources = al->jump_sources;
2217
2218 ++notes->nr_jumps;
2219 }
2220}
2221
5bc49f61
ACM
2222void annotation__set_offsets(struct annotation *notes, s64 size)
2223{
2224 struct annotation_line *al;
2225
2226 notes->max_line_len = 0;
2227
2228 list_for_each_entry(al, &notes->src->source, node) {
2229 size_t line_len = strlen(al->line);
2230
2231 if (notes->max_line_len < line_len)
2232 notes->max_line_len = line_len;
2233 al->idx = notes->nr_entries++;
2234 if (al->offset != -1) {
2235 al->idx_asm = notes->nr_asm_entries++;
2236 /*
2237 * FIXME: short term bandaid to cope with assembly
2238 * routines that comes with labels in the same column
2239 * as the address in objdump, sigh.
2240 *
2241 * E.g. copy_user_generic_unrolled
2242 */
2243 if (al->offset < size)
2244 notes->offsets[al->offset] = al;
2245 } else
2246 al->idx_asm = -1;
2247 }
2248}
2249
b8b0d819
ACM
2250static inline int width_jumps(int n)
2251{
2252 if (n >= 100)
2253 return 5;
2254 if (n / 10)
2255 return 2;
2256 return 1;
2257}
2258
2259void annotation__init_column_widths(struct annotation *notes, struct symbol *sym)
2260{
2261 notes->widths.addr = notes->widths.target =
2262 notes->widths.min_addr = hex_width(symbol__size(sym));
2263 notes->widths.max_addr = hex_width(sym->end);
2264 notes->widths.jumps = width_jumps(notes->max_jump_sources);
2265}
2266
7232bf7a
ACM
2267void annotation__update_column_widths(struct annotation *notes)
2268{
2269 if (notes->options->use_offset)
2270 notes->widths.target = notes->widths.min_addr;
2271 else
2272 notes->widths.target = notes->widths.max_addr;
2273
2274 notes->widths.addr = notes->widths.target;
2275
2276 if (notes->options->show_nr_jumps)
2277 notes->widths.addr += notes->widths.jumps + 1;
2278}
2279
8b4c74dc 2280static void annotation__calc_lines(struct annotation *notes, struct map *map,
425859ff 2281 struct rb_root *root)
8b4c74dc
JO
2282{
2283 struct annotation_line *al;
2284 struct rb_root tmp_root = RB_ROOT;
2285
2286 list_for_each_entry(al, &notes->src->source, node) {
2287 double percent_max = 0.0;
2288 int i;
2289
2290 for (i = 0; i < al->samples_nr; i++) {
2291 struct annotation_data *sample;
2292
2293 sample = &al->samples[i];
2294
2295 if (sample->percent > percent_max)
2296 percent_max = sample->percent;
2297 }
2298
2299 if (percent_max <= 0.5)
2300 continue;
2301
425859ff
ACM
2302 al->path = get_srcline(map->dso, notes->start + al->offset, NULL,
2303 false, true, notes->start + al->offset);
8b4c74dc
JO
2304 insert_source_line(&tmp_root, al);
2305 }
2306
2307 resort_source_line(root, &tmp_root);
2308}
2309
2310static void symbol__calc_lines(struct symbol *sym, struct map *map,
2311 struct rb_root *root)
2312{
2313 struct annotation *notes = symbol__annotation(sym);
8b4c74dc 2314
425859ff 2315 annotation__calc_lines(notes, map, root);
8b4c74dc
JO
2316}
2317
befd2a38
ACM
2318int symbol__tty_annotate2(struct symbol *sym, struct map *map,
2319 struct perf_evsel *evsel, bool print_lines,
2320 bool full_paths)
2321{
2322 struct dso *dso = map->dso;
2323 struct rb_root source_line = RB_ROOT;
35632892 2324 struct annotation_options opts = annotation__default_options;
864298f2
ACM
2325 const char *ev_name = perf_evsel__name(evsel);
2326 char buf[1024];
befd2a38
ACM
2327
2328 if (symbol__annotate2(sym, map, evsel, &opts, NULL) < 0)
2329 return -1;
2330
2331 if (print_lines) {
2332 srcline_full_filename = full_paths;
2333 symbol__calc_lines(sym, map, &source_line);
2334 print_summary(&source_line, dso->long_name);
2335 }
2336
864298f2
ACM
2337 if (perf_evsel__is_group_event(evsel)) {
2338 perf_evsel__group_desc(evsel, buf, sizeof(buf));
2339 ev_name = buf;
2340 }
2341
2342 fprintf(stdout, "%s() %s\nEvent: %s\n\n", sym->name, dso->long_name, ev_name);
befd2a38
ACM
2343 symbol__annotate_fprintf2(sym, stdout);
2344
2345 annotated_source__purge(symbol__annotation(sym)->src);
2346
2347 return 0;
2348}
2349
db8fd07a
NK
2350int symbol__tty_annotate(struct symbol *sym, struct map *map,
2351 struct perf_evsel *evsel, bool print_lines,
2352 bool full_paths, int min_pcnt, int max_lines)
f1e2701d
ACM
2353{
2354 struct dso *dso = map->dso;
f1e2701d 2355 struct rb_root source_line = RB_ROOT;
f1e2701d 2356
5449f13c 2357 if (symbol__annotate(sym, map, evsel, 0, NULL) < 0)
f1e2701d
ACM
2358 return -1;
2359
05d3f1a1
JO
2360 symbol__calc_percent(sym, evsel);
2361
f1e2701d 2362 if (print_lines) {
4a4c03c1 2363 srcline_full_filename = full_paths;
8b4c74dc 2364 symbol__calc_lines(sym, map, &source_line);
86c98cab 2365 print_summary(&source_line, dso->long_name);
78f7defe
ACM
2366 }
2367
db8fd07a 2368 symbol__annotate_printf(sym, map, evsel, full_paths,
d5e3d747 2369 min_pcnt, max_lines, 0);
78f7defe 2370
f8eb37bd 2371 annotated_source__purge(symbol__annotation(sym)->src);
f1e2701d 2372
78f7defe
ACM
2373 return 0;
2374}
f626adff 2375
48c65bda
NK
2376bool ui__has_annotation(void)
2377{
2e0453af 2378 return use_browser == 1 && perf_hpp_list.sym;
48c65bda 2379}
ecda45bd 2380
2f025ea0
ACM
2381
2382double annotation_line__max_percent(struct annotation_line *al, struct annotation *notes)
2383{
2384 double percent_max = 0.0;
2385 int i;
2386
2387 for (i = 0; i < notes->nr_events; i++) {
2388 if (al->samples[i].percent > percent_max)
2389 percent_max = al->samples[i].percent;
2390 }
2391
2392 return percent_max;
2393}
2394
a1e9b74c
ACM
2395static void disasm_line__write(struct disasm_line *dl, struct annotation *notes,
2396 void *obj, char *bf, size_t size,
2397 void (*obj__printf)(void *obj, const char *fmt, ...),
2398 void (*obj__write_graph)(void *obj, int graph))
2399{
2400 if (dl->ins.ops && dl->ins.ops->scnprintf) {
2401 if (ins__is_jump(&dl->ins)) {
751b1783 2402 bool fwd;
a1e9b74c 2403
751b1783
ACM
2404 if (dl->ops.target.outside)
2405 goto call_like;
2406 fwd = dl->ops.target.offset > dl->al.offset;
a1e9b74c
ACM
2407 obj__write_graph(obj, fwd ? DARROW_CHAR : UARROW_CHAR);
2408 obj__printf(obj, " ");
2409 } else if (ins__is_call(&dl->ins)) {
751b1783 2410call_like:
a1e9b74c
ACM
2411 obj__write_graph(obj, RARROW_CHAR);
2412 obj__printf(obj, " ");
2413 } else if (ins__is_ret(&dl->ins)) {
2414 obj__write_graph(obj, LARROW_CHAR);
2415 obj__printf(obj, " ");
2416 } else {
2417 obj__printf(obj, " ");
2418 }
2419 } else {
2420 obj__printf(obj, " ");
2421 }
2422
2423 disasm_line__scnprintf(dl, bf, size, !notes->options->use_offset);
2424}
2425
c298304b
ACM
2426static void __annotation_line__write(struct annotation_line *al, struct annotation *notes,
2427 bool first_line, bool current_entry, bool change_color, int width,
2428 void *obj,
2429 int (*obj__set_color)(void *obj, int color),
2430 void (*obj__set_percent_color)(void *obj, double percent, bool current),
2431 int (*obj__set_jumps_percent_color)(void *obj, int nr, bool current),
2432 void (*obj__printf)(void *obj, const char *fmt, ...),
2433 void (*obj__write_graph)(void *obj, int graph))
2434
2ba5eca1
ACM
2435{
2436 double percent_max = annotation_line__max_percent(al, notes);
a1e9b74c
ACM
2437 int pcnt_width = annotation__pcnt_width(notes),
2438 cycles_width = annotation__cycles_width(notes);
2ba5eca1 2439 bool show_title = false;
a1e9b74c
ACM
2440 char bf[256];
2441 int printed;
2ba5eca1
ACM
2442
2443 if (first_line && (al->offset == -1 || percent_max == 0.0)) {
2444 if (notes->have_cycles) {
2445 if (al->ipc == 0.0 && al->cycles == 0)
2446 show_title = true;
2447 } else
2448 show_title = true;
2449 }
2450
2ba5eca1
ACM
2451 if (al->offset != -1 && percent_max != 0.0) {
2452 int i;
2453
2454 for (i = 0; i < notes->nr_events; i++) {
2455 obj__set_percent_color(obj, al->samples[i].percent, current_entry);
2456 if (notes->options->show_total_period) {
2457 obj__printf(obj, "%11" PRIu64 " ", al->samples[i].he.period);
2458 } else if (notes->options->show_nr_samples) {
2459 obj__printf(obj, "%6" PRIu64 " ",
2460 al->samples[i].he.nr_samples);
2461 } else {
2462 obj__printf(obj, "%6.2f ",
2463 al->samples[i].percent);
2464 }
2465 }
2466 } else {
2ba5eca1
ACM
2467 obj__set_percent_color(obj, 0, current_entry);
2468
2469 if (!show_title)
a1e9b74c 2470 obj__printf(obj, "%-*s", pcnt_width, " ");
2ba5eca1 2471 else {
a1e9b74c 2472 obj__printf(obj, "%-*s", pcnt_width,
2ba5eca1
ACM
2473 notes->options->show_total_period ? "Period" :
2474 notes->options->show_nr_samples ? "Samples" : "Percent");
2475 }
2476 }
2477
2478 if (notes->have_cycles) {
2479 if (al->ipc)
2480 obj__printf(obj, "%*.2f ", ANNOTATION__IPC_WIDTH - 1, al->ipc);
2481 else if (!show_title)
2482 obj__printf(obj, "%*s", ANNOTATION__IPC_WIDTH, " ");
2483 else
2484 obj__printf(obj, "%*s ", ANNOTATION__IPC_WIDTH - 1, "IPC");
2485
2486 if (al->cycles)
2487 obj__printf(obj, "%*" PRIu64 " ",
2488 ANNOTATION__CYCLES_WIDTH - 1, al->cycles);
2489 else if (!show_title)
2490 obj__printf(obj, "%*s", ANNOTATION__CYCLES_WIDTH, " ");
2491 else
2492 obj__printf(obj, "%*s ", ANNOTATION__CYCLES_WIDTH - 1, "Cycle");
2493 }
2494
2495 obj__printf(obj, " ");
a1e9b74c
ACM
2496
2497 if (!*al->line)
2498 obj__printf(obj, "%-*s", width - pcnt_width - cycles_width, " ");
2499 else if (al->offset == -1) {
2500 if (al->line_nr && notes->options->show_linenr)
2501 printed = scnprintf(bf, sizeof(bf), "%-*d ", notes->widths.addr + 1, al->line_nr);
2502 else
2503 printed = scnprintf(bf, sizeof(bf), "%-*s ", notes->widths.addr, " ");
2504 obj__printf(obj, bf);
2505 obj__printf(obj, "%-*s", width - printed - pcnt_width - cycles_width + 1, al->line);
2506 } else {
2507 u64 addr = al->offset;
2508 int color = -1;
2509
2510 if (!notes->options->use_offset)
2511 addr += notes->start;
2512
2513 if (!notes->options->use_offset) {
2514 printed = scnprintf(bf, sizeof(bf), "%" PRIx64 ": ", addr);
2515 } else {
2516 if (al->jump_sources) {
2517 if (notes->options->show_nr_jumps) {
2518 int prev;
2519 printed = scnprintf(bf, sizeof(bf), "%*d ",
2520 notes->widths.jumps,
2521 al->jump_sources);
2522 prev = obj__set_jumps_percent_color(obj, al->jump_sources,
2523 current_entry);
2524 obj__printf(obj, bf);
2525 obj__set_color(obj, prev);
2526 }
2527
2528 printed = scnprintf(bf, sizeof(bf), "%*" PRIx64 ": ",
2529 notes->widths.target, addr);
2530 } else {
2531 printed = scnprintf(bf, sizeof(bf), "%-*s ",
2532 notes->widths.addr, " ");
2533 }
2534 }
2535
2536 if (change_color)
2537 color = obj__set_color(obj, HE_COLORSET_ADDR);
2538 obj__printf(obj, bf);
2539 if (change_color)
2540 obj__set_color(obj, color);
2541
2542 disasm_line__write(disasm_line(al), notes, obj, bf, sizeof(bf), obj__printf, obj__write_graph);
2543
2544 obj__printf(obj, "%-*s", width - pcnt_width - cycles_width - 3 - printed, bf);
2545 }
2546
2ba5eca1
ACM
2547}
2548
c298304b
ACM
2549void annotation_line__write(struct annotation_line *al, struct annotation *notes,
2550 struct annotation_write_ops *ops)
2551{
2552 __annotation_line__write(al, notes, ops->first_line, ops->current_entry,
2553 ops->change_color, ops->width, ops->obj,
2554 ops->set_color, ops->set_percent_color,
2555 ops->set_jumps_percent_color, ops->printf,
2556 ops->write_graph);
2557}
2558
ecda45bd
ACM
2559int symbol__annotate2(struct symbol *sym, struct map *map, struct perf_evsel *evsel,
2560 struct annotation_options *options, struct arch **parch)
2561{
2562 struct annotation *notes = symbol__annotation(sym);
2563 size_t size = symbol__size(sym);
2564 int nr_pcnt = 1, err;
2565
2566 notes->offsets = zalloc(size * sizeof(struct annotation_line *));
2567 if (notes->offsets == NULL)
2568 return -1;
2569
2570 if (perf_evsel__is_group_event(evsel))
2571 nr_pcnt = evsel->nr_members;
2572
2573 err = symbol__annotate(sym, map, evsel, 0, parch);
2574 if (err)
2575 goto out_free_offsets;
2576
2577 notes->options = options;
2578
2579 symbol__calc_percent(sym, evsel);
2580
2581 notes->start = map__rip_2objdump(map, sym->start);
2582
2583 annotation__set_offsets(notes, size);
2584 annotation__mark_jump_targets(notes, sym);
2585 annotation__compute_ipc(notes, size);
2586 annotation__init_column_widths(notes, sym);
2587 notes->nr_events = nr_pcnt;
2588
2589 annotation__update_column_widths(notes);
2590
2591 return 0;
2592
2593out_free_offsets:
2594 zfree(&notes->offsets);
2595 return -1;
2596}
7f0b6fde
ACM
2597
2598#define ANNOTATION__CFG(n) \
2599 { .name = #n, .value = &annotation__default_options.n, }
2600
2601/*
2602 * Keep the entries sorted, they are bsearch'ed
2603 */
2604static struct annotation_config {
2605 const char *name;
2606 bool *value;
2607} annotation__configs[] = {
2608 ANNOTATION__CFG(hide_src_code),
2609 ANNOTATION__CFG(jump_arrows),
2610 ANNOTATION__CFG(show_linenr),
2611 ANNOTATION__CFG(show_nr_jumps),
2612 ANNOTATION__CFG(show_nr_samples),
2613 ANNOTATION__CFG(show_total_period),
2614 ANNOTATION__CFG(use_offset),
2615};
2616
2617#undef ANNOTATION__CFG
2618
2619static int annotation_config__cmp(const void *name, const void *cfgp)
2620{
2621 const struct annotation_config *cfg = cfgp;
2622
2623 return strcmp(name, cfg->name);
2624}
2625
2626static int annotation__config(const char *var, const char *value,
2627 void *data __maybe_unused)
2628{
2629 struct annotation_config *cfg;
2630 const char *name;
2631
2632 if (!strstarts(var, "annotate."))
2633 return 0;
2634
2635 name = var + 9;
2636 cfg = bsearch(name, annotation__configs, ARRAY_SIZE(annotation__configs),
2637 sizeof(struct annotation_config), annotation_config__cmp);
2638
2639 if (cfg == NULL)
2640 pr_debug("%s variable unknown, ignoring...", var);
2641 else
2642 *cfg->value = perf_config_bool(name, value);
2643 return 0;
2644}
2645
2646void annotation_config__init(void)
2647{
2648 perf_config(annotation__config, NULL);
2649
2650 annotation__default_options.show_total_period = symbol_conf.show_total_period;
2651 annotation__default_options.show_nr_samples = symbol_conf.show_nr_samples;
2652}