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