]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - tools/perf/util/annotate.c
perf tools: Including missing inttypes.h header
[mirror_ubuntu-focal-kernel.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
fd20e811 10#include <inttypes.h>
78f7defe 11#include "util.h"
48c65bda
NK
12#include "ui/ui.h"
13#include "sort.h"
78f7defe
ACM
14#include "build-id.h"
15#include "color.h"
16#include "cache.h"
17#include "symbol.h"
18#include "debug.h"
19#include "annotate.h"
db8fd07a 20#include "evsel.h"
70fbe057 21#include "block-range.h"
786c1b51 22#include "arch/common.h"
e592488c 23#include <regex.h>
ce6f4fab 24#include <pthread.h>
4383db88 25#include <linux/bitops.h>
877a7a11 26#include <linux/kernel.h>
786c1b51 27#include <sys/utsname.h>
78f7defe 28
f69b64f7 29const char *disassembler_style;
7a4ec938 30const char *objdump_path;
e592488c 31static regex_t file_lineno;
f69b64f7 32
75b49202 33static struct ins_ops *ins__find(struct arch *arch, const char *name);
2a1ff812 34static void ins__sort(struct arch *arch);
75b49202 35static int disasm_line__parse(char *line, const char **namep, char **rawp);
7a997fe4 36
786c1b51
ACM
37struct arch {
38 const char *name;
763d8960
ACM
39 struct ins *instructions;
40 size_t nr_instructions;
2a1ff812
ACM
41 size_t nr_instructions_allocated;
42 struct ins_ops *(*associate_instruction_ops)(struct arch *arch, const char *name);
763d8960 43 bool sorted_instructions;
0781ea92
ACM
44 bool initialized;
45 void *priv;
46 int (*init)(struct arch *arch);
786c1b51
ACM
47 struct {
48 char comment_char;
9c2fb451 49 char skip_functions_char;
786c1b51
ACM
50 } objdump;
51};
52
763d8960
ACM
53static struct ins_ops call_ops;
54static struct ins_ops dec_ops;
55static struct ins_ops jump_ops;
56static struct ins_ops mov_ops;
57static struct ins_ops nop_ops;
58static struct ins_ops lock_ops;
59static struct ins_ops ret_ops;
60
2a1ff812
ACM
61static int arch__grow_instructions(struct arch *arch)
62{
63 struct ins *new_instructions;
64 size_t new_nr_allocated;
65
66 if (arch->nr_instructions_allocated == 0 && arch->instructions)
67 goto grow_from_non_allocated_table;
68
69 new_nr_allocated = arch->nr_instructions_allocated + 128;
70 new_instructions = realloc(arch->instructions, new_nr_allocated * sizeof(struct ins));
71 if (new_instructions == NULL)
72 return -1;
73
74out_update_instructions:
75 arch->instructions = new_instructions;
76 arch->nr_instructions_allocated = new_nr_allocated;
77 return 0;
78
79grow_from_non_allocated_table:
80 new_nr_allocated = arch->nr_instructions + 128;
81 new_instructions = calloc(new_nr_allocated, sizeof(struct ins));
82 if (new_instructions == NULL)
83 return -1;
84
85 memcpy(new_instructions, arch->instructions, arch->nr_instructions);
86 goto out_update_instructions;
87}
88
acc9bfb5 89static int arch__associate_ins_ops(struct arch* arch, const char *name, struct ins_ops *ops)
2a1ff812
ACM
90{
91 struct ins *ins;
92
93 if (arch->nr_instructions == arch->nr_instructions_allocated &&
94 arch__grow_instructions(arch))
95 return -1;
96
97 ins = &arch->instructions[arch->nr_instructions];
98 ins->name = strdup(name);
99 if (!ins->name)
100 return -1;
101
102 ins->ops = ops;
103 arch->nr_instructions++;
104
105 ins__sort(arch);
106 return 0;
107}
108
763d8960 109#include "arch/arm/annotate/instructions.c"
0fcb1da4 110#include "arch/arm64/annotate/instructions.c"
763d8960 111#include "arch/x86/annotate/instructions.c"
dbdebdc5 112#include "arch/powerpc/annotate/instructions.c"
d9f8dfa9 113#include "arch/s390/annotate/instructions.c"
763d8960 114
786c1b51
ACM
115static struct arch architectures[] = {
116 {
117 .name = "arm",
acc9bfb5 118 .init = arm__annotate_init,
786c1b51 119 },
0fcb1da4
KP
120 {
121 .name = "arm64",
122 .init = arm64__annotate_init,
123 },
786c1b51
ACM
124 {
125 .name = "x86",
763d8960
ACM
126 .instructions = x86__instructions,
127 .nr_instructions = ARRAY_SIZE(x86__instructions),
786c1b51
ACM
128 .objdump = {
129 .comment_char = '#',
130 },
131 },
dbdebdc5
RB
132 {
133 .name = "powerpc",
134 .init = powerpc__annotate_init,
135 },
e77852b3
CB
136 {
137 .name = "s390",
d9f8dfa9 138 .init = s390__annotate_init,
e77852b3
CB
139 .objdump = {
140 .comment_char = '#',
141 },
142 },
786c1b51
ACM
143};
144
c46219ac
ACM
145static void ins__delete(struct ins_operands *ops)
146{
3995614d
ACM
147 if (ops == NULL)
148 return;
74cf249d
ACM
149 zfree(&ops->source.raw);
150 zfree(&ops->source.name);
151 zfree(&ops->target.raw);
152 zfree(&ops->target.name);
c46219ac
ACM
153}
154
5417072b
ACM
155static int ins__raw_scnprintf(struct ins *ins, char *bf, size_t size,
156 struct ins_operands *ops)
157{
158 return scnprintf(bf, size, "%-6.6s %s", ins->name, ops->raw);
159}
160
161int ins__scnprintf(struct ins *ins, char *bf, size_t size,
162 struct ins_operands *ops)
163{
164 if (ins->ops->scnprintf)
165 return ins->ops->scnprintf(ins, bf, size, ops);
166
167 return ins__raw_scnprintf(ins, bf, size, ops);
168}
169
9c2fb451 170static int call__parse(struct arch *arch, struct ins_operands *ops, struct map *map)
d86b0597 171{
d2232885
ACM
172 char *endptr, *tok, *name;
173
44d1a3ed 174 ops->target.addr = strtoull(ops->raw, &endptr, 16);
d2232885
ACM
175
176 name = strchr(endptr, '<');
177 if (name == NULL)
178 goto indirect_call;
179
180 name++;
181
9c2fb451
ACM
182 if (arch->objdump.skip_functions_char &&
183 strchr(name, arch->objdump.skip_functions_char))
cfef25b8 184 return -1;
cfef25b8 185
d2232885
ACM
186 tok = strchr(name, '>');
187 if (tok == NULL)
188 return -1;
189
190 *tok = '\0';
44d1a3ed 191 ops->target.name = strdup(name);
d2232885
ACM
192 *tok = '>';
193
44d1a3ed 194 return ops->target.name == NULL ? -1 : 0;
d2232885
ACM
195
196indirect_call:
88a7fcf9
RB
197 tok = strchr(endptr, '*');
198 if (tok == NULL) {
5f62d4fd
ACM
199 struct symbol *sym = map__find_symbol(map, map->map_ip(map, ops->target.addr));
200 if (sym != NULL)
201 ops->target.name = strdup(sym->name);
202 else
203 ops->target.addr = 0;
e8ea1561
ACM
204 return 0;
205 }
206
44d1a3ed 207 ops->target.addr = strtoull(tok + 1, NULL, 16);
d86b0597
ACM
208 return 0;
209}
210
d2232885 211static int call__scnprintf(struct ins *ins, char *bf, size_t size,
5417072b 212 struct ins_operands *ops)
d2232885 213{
44d1a3ed
ACM
214 if (ops->target.name)
215 return scnprintf(bf, size, "%-6.6s %s", ins->name, ops->target.name);
d2232885 216
e8ea1561
ACM
217 if (ops->target.addr == 0)
218 return ins__raw_scnprintf(ins, bf, size, ops);
219
44d1a3ed 220 return scnprintf(bf, size, "%-6.6s *%" PRIx64, ins->name, ops->target.addr);
d2232885
ACM
221}
222
d86b0597 223static struct ins_ops call_ops = {
d2232885
ACM
224 .parse = call__parse,
225 .scnprintf = call__scnprintf,
d86b0597
ACM
226};
227
228bool ins__is_call(const struct ins *ins)
229{
230 return ins->ops == &call_ops;
231}
232
786c1b51 233static int jump__parse(struct arch *arch __maybe_unused, struct ins_operands *ops, struct map *map __maybe_unused)
4f9d0325 234{
c7e6ead7 235 const char *s = strchr(ops->raw, '+');
3ee2eb6d 236 const char *c = strchr(ops->raw, ',');
4f9d0325 237
3ee2eb6d
RB
238 if (c++ != NULL)
239 ops->target.addr = strtoull(c, NULL, 16);
240 else
241 ops->target.addr = strtoull(ops->raw, NULL, 16);
fb29fa58 242
e216874c 243 if (s++ != NULL) {
bbb7f846 244 ops->target.offset = strtoull(s, NULL, 16);
e216874c
RB
245 ops->target.offset_avail = true;
246 } else {
247 ops->target.offset_avail = false;
248 }
4f9d0325 249
4f9d0325
ACM
250 return 0;
251}
252
c7e6ead7 253static int jump__scnprintf(struct ins *ins, char *bf, size_t size,
5417072b 254 struct ins_operands *ops)
28548d78 255{
e216874c 256 if (!ops->target.addr || ops->target.offset < 0)
bec60e50
RB
257 return ins__raw_scnprintf(ins, bf, size, ops);
258
44d1a3ed 259 return scnprintf(bf, size, "%-6.6s %" PRIx64, ins->name, ops->target.offset);
28548d78
ACM
260}
261
4f9d0325 262static struct ins_ops jump_ops = {
c7e6ead7
ACM
263 .parse = jump__parse,
264 .scnprintf = jump__scnprintf,
4f9d0325
ACM
265};
266
267bool ins__is_jump(const struct ins *ins)
268{
269 return ins->ops == &jump_ops;
270}
271
6de783b6
ACM
272static int comment__symbol(char *raw, char *comment, u64 *addrp, char **namep)
273{
274 char *endptr, *name, *t;
275
276 if (strstr(raw, "(%rip)") == NULL)
277 return 0;
278
279 *addrp = strtoull(comment, &endptr, 16);
280 name = strchr(endptr, '<');
281 if (name == NULL)
282 return -1;
283
284 name++;
285
286 t = strchr(name, '>');
287 if (t == NULL)
288 return 0;
289
290 *t = '\0';
291 *namep = strdup(name);
292 *t = '>';
293
294 return 0;
295}
296
786c1b51 297static int lock__parse(struct arch *arch, struct ins_operands *ops, struct map *map)
7a997fe4 298{
7a997fe4
ACM
299 ops->locked.ops = zalloc(sizeof(*ops->locked.ops));
300 if (ops->locked.ops == NULL)
301 return 0;
302
75b49202 303 if (disasm_line__parse(ops->raw, &ops->locked.ins.name, &ops->locked.ops->raw) < 0)
7a997fe4
ACM
304 goto out_free_ops;
305
75b49202 306 ops->locked.ins.ops = ins__find(arch, ops->locked.ins.name);
0fb9f2aa 307
75b49202 308 if (ops->locked.ins.ops == NULL)
2ba34aaa 309 goto out_free_ops;
7a997fe4 310
75b49202
ACM
311 if (ops->locked.ins.ops->parse &&
312 ops->locked.ins.ops->parse(arch, ops->locked.ops, map) < 0)
be81908c 313 goto out_free_ops;
7a997fe4
ACM
314
315 return 0;
316
317out_free_ops:
04662523 318 zfree(&ops->locked.ops);
7a997fe4
ACM
319 return 0;
320}
321
322static int lock__scnprintf(struct ins *ins, char *bf, size_t size,
323 struct ins_operands *ops)
324{
325 int printed;
326
75b49202 327 if (ops->locked.ins.ops == NULL)
7a997fe4
ACM
328 return ins__raw_scnprintf(ins, bf, size, ops);
329
330 printed = scnprintf(bf, size, "%-6.6s ", ins->name);
75b49202 331 return printed + ins__scnprintf(&ops->locked.ins, bf + printed,
7a997fe4
ACM
332 size - printed, ops->locked.ops);
333}
334
c46219ac
ACM
335static void lock__delete(struct ins_operands *ops)
336{
75b49202 337 struct ins *ins = &ops->locked.ins;
0fb9f2aa 338
75b49202 339 if (ins->ops && ins->ops->free)
0fb9f2aa
RV
340 ins->ops->free(ops->locked.ops);
341 else
342 ins__delete(ops->locked.ops);
343
74cf249d
ACM
344 zfree(&ops->locked.ops);
345 zfree(&ops->target.raw);
346 zfree(&ops->target.name);
c46219ac
ACM
347}
348
7a997fe4 349static struct ins_ops lock_ops = {
c46219ac 350 .free = lock__delete,
7a997fe4
ACM
351 .parse = lock__parse,
352 .scnprintf = lock__scnprintf,
353};
354
786c1b51 355static int mov__parse(struct arch *arch, struct ins_operands *ops, struct map *map __maybe_unused)
6de783b6
ACM
356{
357 char *s = strchr(ops->raw, ','), *target, *comment, prev;
358
359 if (s == NULL)
360 return -1;
361
362 *s = '\0';
363 ops->source.raw = strdup(ops->raw);
364 *s = ',';
48000a1a 365
6de783b6
ACM
366 if (ops->source.raw == NULL)
367 return -1;
368
369 target = ++s;
786c1b51 370 comment = strchr(s, arch->objdump.comment_char);
1e2bb043
AC
371
372 if (comment != NULL)
373 s = comment - 1;
374 else
375 s = strchr(s, '\0') - 1;
6de783b6 376
1e2bb043
AC
377 while (s > target && isspace(s[0]))
378 --s;
379 s++;
6de783b6
ACM
380 prev = *s;
381 *s = '\0';
382
383 ops->target.raw = strdup(target);
384 *s = prev;
385
386 if (ops->target.raw == NULL)
387 goto out_free_source;
388
6de783b6
ACM
389 if (comment == NULL)
390 return 0;
391
4597cf06 392 comment = ltrim(comment);
6de783b6
ACM
393 comment__symbol(ops->source.raw, comment, &ops->source.addr, &ops->source.name);
394 comment__symbol(ops->target.raw, comment, &ops->target.addr, &ops->target.name);
395
396 return 0;
397
398out_free_source:
04662523 399 zfree(&ops->source.raw);
6de783b6
ACM
400 return -1;
401}
402
403static int mov__scnprintf(struct ins *ins, char *bf, size_t size,
404 struct ins_operands *ops)
405{
406 return scnprintf(bf, size, "%-6.6s %s,%s", ins->name,
407 ops->source.name ?: ops->source.raw,
408 ops->target.name ?: ops->target.raw);
409}
410
411static struct ins_ops mov_ops = {
412 .parse = mov__parse,
413 .scnprintf = mov__scnprintf,
414};
415
786c1b51 416static int dec__parse(struct arch *arch __maybe_unused, struct ins_operands *ops, struct map *map __maybe_unused)
a43712c4
ACM
417{
418 char *target, *comment, *s, prev;
419
420 target = s = ops->raw;
421
422 while (s[0] != '\0' && !isspace(s[0]))
423 ++s;
424 prev = *s;
425 *s = '\0';
426
427 ops->target.raw = strdup(target);
428 *s = prev;
429
430 if (ops->target.raw == NULL)
431 return -1;
432
859afa6c 433 comment = strchr(s, arch->objdump.comment_char);
a43712c4
ACM
434 if (comment == NULL)
435 return 0;
436
4597cf06 437 comment = ltrim(comment);
a43712c4
ACM
438 comment__symbol(ops->target.raw, comment, &ops->target.addr, &ops->target.name);
439
440 return 0;
441}
442
443static int dec__scnprintf(struct ins *ins, char *bf, size_t size,
444 struct ins_operands *ops)
445{
446 return scnprintf(bf, size, "%-6.6s %s", ins->name,
447 ops->target.name ?: ops->target.raw);
448}
449
450static struct ins_ops dec_ops = {
451 .parse = dec__parse,
452 .scnprintf = dec__scnprintf,
453};
454
1d037ca1
IT
455static int nop__scnprintf(struct ins *ins __maybe_unused, char *bf, size_t size,
456 struct ins_operands *ops __maybe_unused)
b9818e93
ACM
457{
458 return scnprintf(bf, size, "%-6.6s", "nop");
459}
460
461static struct ins_ops nop_ops = {
462 .scnprintf = nop__scnprintf,
463};
464
6ef94929
NR
465static struct ins_ops ret_ops = {
466 .scnprintf = ins__raw_scnprintf,
467};
468
469bool ins__is_ret(const struct ins *ins)
470{
471 return ins->ops == &ret_ops;
472}
473
7e4c1498 474static int ins__key_cmp(const void *name, const void *insp)
4f9d0325
ACM
475{
476 const struct ins *ins = insp;
477
478 return strcmp(name, ins->name);
479}
480
7e4c1498
CR
481static int ins__cmp(const void *a, const void *b)
482{
483 const struct ins *ia = a;
484 const struct ins *ib = b;
485
486 return strcmp(ia->name, ib->name);
487}
488
763d8960 489static void ins__sort(struct arch *arch)
7e4c1498 490{
763d8960 491 const int nmemb = arch->nr_instructions;
7e4c1498 492
763d8960 493 qsort(arch->instructions, nmemb, sizeof(struct ins), ins__cmp);
7e4c1498
CR
494}
495
2a1ff812 496static struct ins_ops *__ins__find(struct arch *arch, const char *name)
4f9d0325 497{
75b49202 498 struct ins *ins;
763d8960 499 const int nmemb = arch->nr_instructions;
7e4c1498 500
763d8960
ACM
501 if (!arch->sorted_instructions) {
502 ins__sort(arch);
503 arch->sorted_instructions = true;
7e4c1498 504 }
4f9d0325 505
75b49202
ACM
506 ins = bsearch(name, arch->instructions, nmemb, sizeof(struct ins), ins__key_cmp);
507 return ins ? ins->ops : NULL;
4f9d0325
ACM
508}
509
2a1ff812
ACM
510static struct ins_ops *ins__find(struct arch *arch, const char *name)
511{
512 struct ins_ops *ops = __ins__find(arch, name);
513
514 if (!ops && arch->associate_instruction_ops)
515 ops = arch->associate_instruction_ops(arch, name);
516
517 return ops;
518}
519
786c1b51
ACM
520static int arch__key_cmp(const void *name, const void *archp)
521{
522 const struct arch *arch = archp;
523
524 return strcmp(name, arch->name);
525}
526
527static int arch__cmp(const void *a, const void *b)
528{
529 const struct arch *aa = a;
530 const struct arch *ab = b;
531
532 return strcmp(aa->name, ab->name);
533}
534
535static void arch__sort(void)
536{
537 const int nmemb = ARRAY_SIZE(architectures);
538
539 qsort(architectures, nmemb, sizeof(struct arch), arch__cmp);
540}
541
542static struct arch *arch__find(const char *name)
543{
544 const int nmemb = ARRAY_SIZE(architectures);
545 static bool sorted;
546
547 if (!sorted) {
548 arch__sort();
549 sorted = true;
550 }
551
552 return bsearch(name, architectures, nmemb, sizeof(struct arch), arch__key_cmp);
553}
554
d04b35f8 555int symbol__alloc_hist(struct symbol *sym)
ce6f4fab
ACM
556{
557 struct annotation *notes = symbol__annotation(sym);
1b2e2df4 558 const size_t size = symbol__size(sym);
8696329b
CS
559 size_t sizeof_sym_hist;
560
561 /* Check for overflow when calculating sizeof_sym_hist */
562 if (size > (SIZE_MAX - sizeof(struct sym_hist)) / sizeof(u64))
563 return -1;
564
565 sizeof_sym_hist = (sizeof(struct sym_hist) + size * sizeof(u64));
566
567 /* Check for overflow in zalloc argument */
568 if (sizeof_sym_hist > (SIZE_MAX - sizeof(*notes->src))
569 / symbol_conf.nr_events)
570 return -1;
ce6f4fab 571
d04b35f8 572 notes->src = zalloc(sizeof(*notes->src) + symbol_conf.nr_events * sizeof_sym_hist);
ce6f4fab
ACM
573 if (notes->src == NULL)
574 return -1;
575 notes->src->sizeof_sym_hist = sizeof_sym_hist;
d04b35f8 576 notes->src->nr_histograms = symbol_conf.nr_events;
ce6f4fab
ACM
577 INIT_LIST_HEAD(&notes->src->source);
578 return 0;
78f7defe
ACM
579}
580
d4957633
AK
581/* The cycles histogram is lazily allocated. */
582static int symbol__alloc_hist_cycles(struct symbol *sym)
583{
584 struct annotation *notes = symbol__annotation(sym);
585 const size_t size = symbol__size(sym);
586
587 notes->src->cycles_hist = calloc(size, sizeof(struct cyc_hist));
588 if (notes->src->cycles_hist == NULL)
589 return -1;
590 return 0;
591}
592
36532461
ACM
593void symbol__annotate_zero_histograms(struct symbol *sym)
594{
595 struct annotation *notes = symbol__annotation(sym);
596
ce6f4fab 597 pthread_mutex_lock(&notes->lock);
d4957633 598 if (notes->src != NULL) {
ce6f4fab
ACM
599 memset(notes->src->histograms, 0,
600 notes->src->nr_histograms * notes->src->sizeof_sym_hist);
d4957633
AK
601 if (notes->src->cycles_hist)
602 memset(notes->src->cycles_hist, 0,
603 symbol__size(sym) * sizeof(struct cyc_hist));
604 }
ce6f4fab 605 pthread_mutex_unlock(&notes->lock);
36532461
ACM
606}
607
d4957633
AK
608static int __symbol__account_cycles(struct annotation *notes,
609 u64 start,
610 unsigned offset, unsigned cycles,
611 unsigned have_start)
612{
613 struct cyc_hist *ch;
614
615 ch = notes->src->cycles_hist;
616 /*
617 * For now we can only account one basic block per
618 * final jump. But multiple could be overlapping.
619 * Always account the longest one. So when
620 * a shorter one has been already seen throw it away.
621 *
622 * We separately always account the full cycles.
623 */
624 ch[offset].num_aggr++;
625 ch[offset].cycles_aggr += cycles;
626
627 if (!have_start && ch[offset].have_start)
628 return 0;
629 if (ch[offset].num) {
630 if (have_start && (!ch[offset].have_start ||
631 ch[offset].start > start)) {
632 ch[offset].have_start = 0;
633 ch[offset].cycles = 0;
634 ch[offset].num = 0;
635 if (ch[offset].reset < 0xffff)
636 ch[offset].reset++;
637 } else if (have_start &&
638 ch[offset].start < start)
639 return 0;
640 }
641 ch[offset].have_start = have_start;
642 ch[offset].start = start;
643 ch[offset].cycles += cycles;
644 ch[offset].num++;
645 return 0;
646}
647
b66d8c0c
ACM
648static int __symbol__inc_addr_samples(struct symbol *sym, struct map *map,
649 struct annotation *notes, int evidx, u64 addr)
78f7defe 650{
2f525d01 651 unsigned offset;
78f7defe
ACM
652 struct sym_hist *h;
653
78f7defe
ACM
654 pr_debug3("%s: addr=%#" PRIx64 "\n", __func__, map->unmap_ip(map, addr));
655
edee44be
RB
656 if ((addr < sym->start || addr >= sym->end) &&
657 (addr != sym->end || sym->start != sym->end)) {
e3d006ce
ACM
658 pr_debug("%s(%d): ERANGE! sym->name=%s, start=%#" PRIx64 ", addr=%#" PRIx64 ", end=%#" PRIx64 "\n",
659 __func__, __LINE__, sym->name, sym->start, addr, sym->end);
31d68e7b 660 return -ERANGE;
e3d006ce 661 }
78f7defe 662
2f525d01
ACM
663 offset = addr - sym->start;
664 h = annotation__histogram(notes, evidx);
78f7defe
ACM
665 h->sum++;
666 h->addr[offset]++;
667
668 pr_debug3("%#" PRIx64 " %s: period++ [addr: %#" PRIx64 ", %#" PRIx64
2f525d01
ACM
669 ", evidx=%d] => %" PRIu64 "\n", sym->start, sym->name,
670 addr, addr - sym->start, evidx, h->addr[offset]);
78f7defe
ACM
671 return 0;
672}
673
d4957633 674static struct annotation *symbol__get_annotation(struct symbol *sym, bool cycles)
83be34a7
AK
675{
676 struct annotation *notes = symbol__annotation(sym);
677
678 if (notes->src == NULL) {
679 if (symbol__alloc_hist(sym) < 0)
680 return NULL;
681 }
d4957633
AK
682 if (!notes->src->cycles_hist && cycles) {
683 if (symbol__alloc_hist_cycles(sym) < 0)
684 return NULL;
685 }
83be34a7
AK
686 return notes;
687}
688
44e83039
ACM
689static int symbol__inc_addr_samples(struct symbol *sym, struct map *map,
690 int evidx, u64 addr)
b66d8c0c
ACM
691{
692 struct annotation *notes;
693
48c65bda 694 if (sym == NULL)
b66d8c0c 695 return 0;
d4957633 696 notes = symbol__get_annotation(sym, false);
83be34a7
AK
697 if (notes == NULL)
698 return -ENOMEM;
b66d8c0c
ACM
699 return __symbol__inc_addr_samples(sym, map, notes, evidx, addr);
700}
701
d4957633
AK
702static int symbol__account_cycles(u64 addr, u64 start,
703 struct symbol *sym, unsigned cycles)
704{
705 struct annotation *notes;
706 unsigned offset;
707
708 if (sym == NULL)
709 return 0;
710 notes = symbol__get_annotation(sym, true);
711 if (notes == NULL)
712 return -ENOMEM;
713 if (addr < sym->start || addr >= sym->end)
714 return -ERANGE;
715
716 if (start) {
717 if (start < sym->start || start >= sym->end)
718 return -ERANGE;
719 if (start >= addr)
720 start = 0;
721 }
722 offset = addr - sym->start;
723 return __symbol__account_cycles(notes,
724 start ? start - sym->start : 0,
725 offset, cycles,
726 !!start);
727}
728
729int addr_map_symbol__account_cycles(struct addr_map_symbol *ams,
730 struct addr_map_symbol *start,
731 unsigned cycles)
732{
3d7245b0 733 u64 saddr = 0;
d4957633
AK
734 int err;
735
736 if (!cycles)
737 return 0;
738
739 /*
740 * Only set start when IPC can be computed. We can only
741 * compute it when the basic block is completely in a single
742 * function.
743 * Special case the case when the jump is elsewhere, but
744 * it starts on the function start.
745 */
746 if (start &&
747 (start->sym == ams->sym ||
748 (ams->sym &&
749 start->addr == ams->sym->start + ams->map->start)))
750 saddr = start->al_addr;
751 if (saddr == 0)
3d7245b0 752 pr_debug2("BB with bad start: addr %"PRIx64" start %"PRIx64" sym %"PRIx64" saddr %"PRIx64"\n",
d4957633
AK
753 ams->addr,
754 start ? start->addr : 0,
755 ams->sym ? ams->sym->start + ams->map->start : 0,
756 saddr);
757 err = symbol__account_cycles(ams->al_addr, saddr, ams->sym, cycles);
758 if (err)
759 pr_debug2("account_cycles failed %d\n", err);
760 return err;
761}
762
0f4e7a24
ACM
763int addr_map_symbol__inc_samples(struct addr_map_symbol *ams, int evidx)
764{
765 return symbol__inc_addr_samples(ams->sym, ams->map, evidx, ams->al_addr);
766}
767
f626adff
ACM
768int hist_entry__inc_addr_samples(struct hist_entry *he, int evidx, u64 ip)
769{
770 return symbol__inc_addr_samples(he->ms.sym, he->ms.map, evidx, ip);
771}
772
786c1b51 773static void disasm_line__init_ins(struct disasm_line *dl, struct arch *arch, struct map *map)
4f9d0325 774{
75b49202 775 dl->ins.ops = ins__find(arch, dl->ins.name);
4f9d0325 776
75b49202 777 if (!dl->ins.ops)
4f9d0325
ACM
778 return;
779
75b49202
ACM
780 if (dl->ins.ops->parse && dl->ins.ops->parse(arch, &dl->ops, map) < 0)
781 dl->ins.ops = NULL;
4f9d0325
ACM
782}
783
75b49202 784static int disasm_line__parse(char *line, const char **namep, char **rawp)
7a997fe4 785{
4597cf06 786 char tmp, *name = ltrim(line);
7a997fe4
ACM
787
788 if (name[0] == '\0')
789 return -1;
790
791 *rawp = name + 1;
792
793 while ((*rawp)[0] != '\0' && !isspace((*rawp)[0]))
794 ++*rawp;
795
796 tmp = (*rawp)[0];
797 (*rawp)[0] = '\0';
798 *namep = strdup(name);
799
800 if (*namep == NULL)
801 goto out_free_name;
802
803 (*rawp)[0] = tmp;
4597cf06 804 *rawp = ltrim(*rawp);
7a997fe4
ACM
805
806 return 0;
807
808out_free_name:
75b49202
ACM
809 free((void *)namep);
810 *namep = NULL;
7a997fe4
ACM
811 return -1;
812}
813
e592488c 814static struct disasm_line *disasm_line__new(s64 offset, char *line,
bff5c306 815 size_t privsize, int line_nr,
786c1b51 816 struct arch *arch,
bff5c306 817 struct map *map)
78f7defe 818{
5145418b 819 struct disasm_line *dl = zalloc(sizeof(*dl) + privsize);
78f7defe 820
29ed6e76
ACM
821 if (dl != NULL) {
822 dl->offset = offset;
823 dl->line = strdup(line);
e592488c 824 dl->line_nr = line_nr;
29ed6e76 825 if (dl->line == NULL)
058b4cc9 826 goto out_delete;
5145418b
ACM
827
828 if (offset != -1) {
75b49202 829 if (disasm_line__parse(dl->line, &dl->ins.name, &dl->ops.raw) < 0)
5145418b
ACM
830 goto out_free_line;
831
786c1b51 832 disasm_line__init_ins(dl, arch, map);
5145418b 833 }
78f7defe
ACM
834 }
835
29ed6e76 836 return dl;
5145418b
ACM
837
838out_free_line:
74cf249d 839 zfree(&dl->line);
058b4cc9 840out_delete:
29ed6e76 841 free(dl);
058b4cc9 842 return NULL;
78f7defe
ACM
843}
844
29ed6e76 845void disasm_line__free(struct disasm_line *dl)
78f7defe 846{
74cf249d 847 zfree(&dl->line);
75b49202
ACM
848 if (dl->ins.ops && dl->ins.ops->free)
849 dl->ins.ops->free(&dl->ops);
c46219ac
ACM
850 else
851 ins__delete(&dl->ops);
75b49202
ACM
852 free((void *)dl->ins.name);
853 dl->ins.name = NULL;
29ed6e76 854 free(dl);
78f7defe
ACM
855}
856
5417072b
ACM
857int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size, bool raw)
858{
75b49202
ACM
859 if (raw || !dl->ins.ops)
860 return scnprintf(bf, size, "%-6.6s %s", dl->ins.name, dl->ops.raw);
5417072b 861
75b49202 862 return ins__scnprintf(&dl->ins, bf, size, &dl->ops);
5417072b
ACM
863}
864
29ed6e76 865static void disasm__add(struct list_head *head, struct disasm_line *line)
78f7defe
ACM
866{
867 list_add_tail(&line->node, head);
868}
869
29ed6e76 870struct disasm_line *disasm__get_next_ip_line(struct list_head *head, struct disasm_line *pos)
78f7defe
ACM
871{
872 list_for_each_entry_continue(pos, head, node)
873 if (pos->offset >= 0)
874 return pos;
875
876 return NULL;
877}
878
e64aa75b 879double disasm__calc_percent(struct annotation *notes, int evidx, s64 offset,
0c4a5bce 880 s64 end, const char **path, u64 *nr_samples)
e5ccf9f4
NK
881{
882 struct source_line *src_line = notes->src->lines;
e5ccf9f4 883 double percent = 0.0;
0c4a5bce 884 *nr_samples = 0;
e5ccf9f4 885
bd64fcb8 886 if (src_line) {
1491c22a 887 size_t sizeof_src_line = sizeof(*src_line) +
276af92f 888 sizeof(src_line->samples) * (src_line->nr_pcnt - 1);
1491c22a 889
bd64fcb8 890 while (offset < end) {
1491c22a
NK
891 src_line = (void *)notes->src->lines +
892 (sizeof_src_line * offset);
893
e5ccf9f4 894 if (*path == NULL)
1491c22a 895 *path = src_line->path;
e5ccf9f4 896
276af92f
ACM
897 percent += src_line->samples[evidx].percent;
898 *nr_samples += src_line->samples[evidx].nr;
1491c22a 899 offset++;
bd64fcb8
NK
900 }
901 } else {
1491c22a
NK
902 struct sym_hist *h = annotation__histogram(notes, evidx);
903 unsigned int hits = 0;
904
bd64fcb8
NK
905 while (offset < end)
906 hits += h->addr[offset++];
e5ccf9f4 907
0c4a5bce
ML
908 if (h->sum) {
909 *nr_samples = hits;
bd64fcb8 910 percent = 100.0 * hits / h->sum;
0c4a5bce 911 }
bd64fcb8 912 }
e5ccf9f4
NK
913
914 return percent;
915}
916
70fbe057
PZ
917static const char *annotate__address_color(struct block_range *br)
918{
919 double cov = block_range__coverage(br);
920
921 if (cov >= 0) {
922 /* mark red for >75% coverage */
923 if (cov > 0.75)
924 return PERF_COLOR_RED;
925
926 /* mark dull for <1% coverage */
927 if (cov < 0.01)
928 return PERF_COLOR_NORMAL;
929 }
930
931 return PERF_COLOR_MAGENTA;
932}
933
934static const char *annotate__asm_color(struct block_range *br)
935{
936 double cov = block_range__coverage(br);
937
938 if (cov >= 0) {
939 /* mark dull for <1% coverage */
940 if (cov < 0.01)
941 return PERF_COLOR_NORMAL;
942 }
943
944 return PERF_COLOR_BLUE;
945}
946
947static void annotate__branch_printf(struct block_range *br, u64 addr)
948{
949 bool emit_comment = true;
950
951 if (!br)
952 return;
953
954#if 1
955 if (br->is_target && br->start == addr) {
956 struct block_range *branch = br;
957 double p;
958
959 /*
960 * Find matching branch to our target.
961 */
962 while (!branch->is_branch)
963 branch = block_range__next(branch);
964
965 p = 100 *(double)br->entry / branch->coverage;
966
967 if (p > 0.1) {
968 if (emit_comment) {
969 emit_comment = false;
970 printf("\t#");
971 }
972
973 /*
974 * The percentage of coverage joined at this target in relation
975 * to the next branch.
976 */
977 printf(" +%.2f%%", p);
978 }
979 }
980#endif
981 if (br->is_branch && br->end == addr) {
982 double p = 100*(double)br->taken / br->coverage;
983
984 if (p > 0.1) {
985 if (emit_comment) {
986 emit_comment = false;
987 printf("\t#");
988 }
989
990 /*
991 * The percentage of coverage leaving at this branch, and
992 * its prediction ratio.
993 */
994 printf(" -%.2f%% (p:%.2f%%)", p, 100*(double)br->pred / br->taken);
995 }
996 }
997}
998
999
29ed6e76 1000static int disasm_line__print(struct disasm_line *dl, struct symbol *sym, u64 start,
db8fd07a 1001 struct perf_evsel *evsel, u64 len, int min_pcnt, int printed,
29ed6e76 1002 int max_lines, struct disasm_line *queue)
78f7defe
ACM
1003{
1004 static const char *prev_line;
1005 static const char *prev_color;
1006
29ed6e76 1007 if (dl->offset != -1) {
78f7defe 1008 const char *path = NULL;
0c4a5bce 1009 u64 nr_samples;
b1dd4432
NK
1010 double percent, max_percent = 0.0;
1011 double *ppercents = &percent;
0c4a5bce 1012 u64 *psamples = &nr_samples;
b1dd4432 1013 int i, nr_percent = 1;
78f7defe
ACM
1014 const char *color;
1015 struct annotation *notes = symbol__annotation(sym);
29ed6e76 1016 s64 offset = dl->offset;
058b4cc9 1017 const u64 addr = start + offset;
29ed6e76 1018 struct disasm_line *next;
70fbe057 1019 struct block_range *br;
ce6f4fab 1020
29ed6e76 1021 next = disasm__get_next_ip_line(&notes->src->source, dl);
78f7defe 1022
759ff497 1023 if (perf_evsel__is_group_event(evsel)) {
b1dd4432
NK
1024 nr_percent = evsel->nr_members;
1025 ppercents = calloc(nr_percent, sizeof(double));
0c4a5bce
ML
1026 psamples = calloc(nr_percent, sizeof(u64));
1027 if (ppercents == NULL || psamples == NULL) {
b1dd4432 1028 return -1;
0c4a5bce 1029 }
b1dd4432
NK
1030 }
1031
1032 for (i = 0; i < nr_percent; i++) {
1033 percent = disasm__calc_percent(notes,
1491c22a
NK
1034 notes->src->lines ? i : evsel->idx + i,
1035 offset,
1036 next ? next->offset : (s64) len,
0c4a5bce 1037 &path, &nr_samples);
b1dd4432
NK
1038
1039 ppercents[i] = percent;
0c4a5bce 1040 psamples[i] = nr_samples;
b1dd4432
NK
1041 if (percent > max_percent)
1042 max_percent = percent;
1043 }
1044
1045 if (max_percent < min_pcnt)
36532461
ACM
1046 return -1;
1047
e3087b80 1048 if (max_lines && printed >= max_lines)
36532461 1049 return 1;
d040bd36 1050
d5e3d747
ACM
1051 if (queue != NULL) {
1052 list_for_each_entry_from(queue, &notes->src->source, node) {
29ed6e76 1053 if (queue == dl)
d5e3d747 1054 break;
db8fd07a 1055 disasm_line__print(queue, sym, start, evsel, len,
d5e3d747
ACM
1056 0, 0, 1, NULL);
1057 }
1058 }
1059
b1dd4432 1060 color = get_percent_color(max_percent);
78f7defe
ACM
1061
1062 /*
1063 * Also color the filename and line if needed, with
1064 * the same color than the percentage. Don't print it
1065 * twice for close colored addr with the same filename:line
1066 */
1067 if (path) {
1068 if (!prev_line || strcmp(prev_line, path)
1069 || color != prev_color) {
1070 color_fprintf(stdout, color, " %s", path);
1071 prev_line = path;
1072 prev_color = color;
1073 }
1074 }
1075
b1dd4432
NK
1076 for (i = 0; i < nr_percent; i++) {
1077 percent = ppercents[i];
0c4a5bce 1078 nr_samples = psamples[i];
b1dd4432 1079 color = get_percent_color(percent);
0c4a5bce
ML
1080
1081 if (symbol_conf.show_total_period)
1082 color_fprintf(stdout, color, " %7" PRIu64,
1083 nr_samples);
1084 else
1085 color_fprintf(stdout, color, " %7.2f", percent);
b1dd4432
NK
1086 }
1087
78f7defe 1088 printf(" : ");
70fbe057
PZ
1089
1090 br = block_range__find(addr);
1091 color_fprintf(stdout, annotate__address_color(br), " %" PRIx64 ":", addr);
1092 color_fprintf(stdout, annotate__asm_color(br), "%s", dl->line);
1093 annotate__branch_printf(br, addr);
1094 printf("\n");
b1dd4432
NK
1095
1096 if (ppercents != &percent)
1097 free(ppercents);
1098
0c4a5bce
ML
1099 if (psamples != &nr_samples)
1100 free(psamples);
1101
e3087b80 1102 } else if (max_lines && printed >= max_lines)
36532461
ACM
1103 return 1;
1104 else {
b1dd4432
NK
1105 int width = 8;
1106
d5e3d747
ACM
1107 if (queue)
1108 return -1;
1109
759ff497 1110 if (perf_evsel__is_group_event(evsel))
b1dd4432
NK
1111 width *= evsel->nr_members;
1112
29ed6e76 1113 if (!*dl->line)
b1dd4432 1114 printf(" %*s:\n", width, " ");
78f7defe 1115 else
b1dd4432 1116 printf(" %*s: %s\n", width, " ", dl->line);
78f7defe 1117 }
36532461
ACM
1118
1119 return 0;
78f7defe
ACM
1120}
1121
3aec150a
NK
1122/*
1123 * symbol__parse_objdump_line() parses objdump output (with -d --no-show-raw)
1124 * which looks like following
1125 *
1126 * 0000000000415500 <_init>:
1127 * 415500: sub $0x8,%rsp
1128 * 415504: mov 0x2f5ad5(%rip),%rax # 70afe0 <_DYNAMIC+0x2f8>
1129 * 41550b: test %rax,%rax
1130 * 41550e: je 415515 <_init+0x15>
1131 * 415510: callq 416e70 <__gmon_start__@plt>
1132 * 415515: add $0x8,%rsp
1133 * 415519: retq
1134 *
1135 * it will be parsed and saved into struct disasm_line as
1136 * <offset> <name> <ops.raw>
1137 *
1138 * The offset will be a relative offset from the start of the symbol and -1
1139 * means that it's not a disassembly line so should be treated differently.
1140 * The ops.raw part will be parsed further according to type of the instruction.
1141 */
ce6f4fab 1142static int symbol__parse_objdump_line(struct symbol *sym, struct map *map,
786c1b51 1143 struct arch *arch,
e592488c
AK
1144 FILE *file, size_t privsize,
1145 int *line_nr)
78f7defe 1146{
ce6f4fab 1147 struct annotation *notes = symbol__annotation(sym);
29ed6e76 1148 struct disasm_line *dl;
4597cf06 1149 char *line = NULL, *parsed_line, *tmp, *tmp2;
78f7defe
ACM
1150 size_t line_len;
1151 s64 line_ip, offset = -1;
e592488c 1152 regmatch_t match[2];
78f7defe
ACM
1153
1154 if (getline(&line, &line_len, file) < 0)
1155 return -1;
1156
1157 if (!line)
1158 return -1;
1159
78f7defe 1160 line_ip = -1;
4597cf06 1161 parsed_line = rtrim(line);
78f7defe 1162
e592488c 1163 /* /filename:linenr ? Save line number and ignore. */
986a5bc0
TS
1164 if (regexec(&file_lineno, parsed_line, 2, match, 0) == 0) {
1165 *line_nr = atoi(parsed_line + match[1].rm_so);
e592488c
AK
1166 return 0;
1167 }
1168
4597cf06 1169 tmp = ltrim(parsed_line);
78f7defe
ACM
1170 if (*tmp) {
1171 /*
1172 * Parse hexa addresses followed by ':'
1173 */
1174 line_ip = strtoull(tmp, &tmp2, 16);
1175 if (*tmp2 != ':' || tmp == tmp2 || tmp2[1] == '\0')
1176 line_ip = -1;
1177 }
1178
1179 if (line_ip != -1) {
1180 u64 start = map__rip_2objdump(map, sym->start),
1181 end = map__rip_2objdump(map, sym->end);
1182
1183 offset = line_ip - start;
2c241bd3 1184 if ((u64)line_ip < start || (u64)line_ip >= end)
78f7defe 1185 offset = -1;
058b4cc9
ACM
1186 else
1187 parsed_line = tmp2 + 1;
a31b7cc0 1188 }
78f7defe 1189
786c1b51 1190 dl = disasm_line__new(offset, parsed_line, privsize, *line_nr, arch, map);
058b4cc9 1191 free(line);
e592488c 1192 (*line_nr)++;
058b4cc9 1193
29ed6e76 1194 if (dl == NULL)
78f7defe 1195 return -1;
058b4cc9 1196
e216874c 1197 if (!disasm_line__has_offset(dl)) {
bbb7f846
AH
1198 dl->ops.target.offset = dl->ops.target.addr -
1199 map__rip_2objdump(map, sym->start);
e216874c
RB
1200 dl->ops.target.offset_avail = true;
1201 }
bbb7f846 1202
6e427ab0 1203 /* kcore has no symbols, so add the call target name */
75b49202 1204 if (dl->ins.ops && ins__is_call(&dl->ins) && !dl->ops.target.name) {
6e427ab0
AH
1205 struct addr_map_symbol target = {
1206 .map = map,
1207 .addr = dl->ops.target.addr,
1208 };
1209
be39db9f 1210 if (!map_groups__find_ams(&target) &&
6e427ab0
AH
1211 target.sym->start == target.al_addr)
1212 dl->ops.target.name = strdup(target.sym->name);
b178170a
AH
1213 }
1214
29ed6e76 1215 disasm__add(&notes->src->source, dl);
78f7defe
ACM
1216
1217 return 0;
1218}
1219
e592488c
AK
1220static __attribute__((constructor)) void symbol__init_regexpr(void)
1221{
1222 regcomp(&file_lineno, "^/[^:]+:([0-9]+)", REG_EXTENDED);
1223}
1224
484a5e74
AH
1225static void delete_last_nop(struct symbol *sym)
1226{
1227 struct annotation *notes = symbol__annotation(sym);
1228 struct list_head *list = &notes->src->source;
1229 struct disasm_line *dl;
1230
1231 while (!list_empty(list)) {
1232 dl = list_entry(list->prev, struct disasm_line, node);
1233
75b49202
ACM
1234 if (dl->ins.ops) {
1235 if (dl->ins.ops != &nop_ops)
484a5e74
AH
1236 return;
1237 } else {
1238 if (!strstr(dl->line, " nop ") &&
1239 !strstr(dl->line, " nopl ") &&
1240 !strstr(dl->line, " nopw "))
1241 return;
1242 }
1243
1244 list_del(&dl->node);
1245 disasm_line__free(dl);
1246 }
1247}
1248
ee51d851
ACM
1249int symbol__strerror_disassemble(struct symbol *sym __maybe_unused, struct map *map,
1250 int errnum, char *buf, size_t buflen)
1251{
1252 struct dso *dso = map->dso;
1253
1254 BUG_ON(buflen == 0);
1255
1256 if (errnum >= 0) {
1257 str_error_r(errnum, buf, buflen);
1258 return 0;
1259 }
1260
1261 switch (errnum) {
1262 case SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX: {
1263 char bf[SBUILD_ID_SIZE + 15] = " with build id ";
1264 char *build_id_msg = NULL;
1265
1266 if (dso->has_build_id) {
1267 build_id__sprintf(dso->build_id,
1268 sizeof(dso->build_id), bf + 15);
1269 build_id_msg = bf;
1270 }
1271 scnprintf(buf, buflen,
1272 "No vmlinux file%s\nwas found in the path.\n\n"
1273 "Note that annotation using /proc/kcore requires CAP_SYS_RAWIO capability.\n\n"
1274 "Please use:\n\n"
1275 " perf buildid-cache -vu vmlinux\n\n"
1276 "or:\n\n"
1277 " --vmlinux vmlinux\n", build_id_msg ?: "");
1278 }
1279 break;
1280 default:
1281 scnprintf(buf, buflen, "Internal error: Invalid %d error code\n", errnum);
1282 break;
1283 }
1284
1285 return 0;
1286}
1287
05ed3ac9 1288static int dso__disassemble_filename(struct dso *dso, char *filename, size_t filename_size)
78f7defe 1289{
05ed3ac9
ACM
1290 char linkname[PATH_MAX];
1291 char *build_id_filename;
6ebd2547 1292 char *build_id_path = NULL;
78f7defe 1293
c12944f7
ACM
1294 if (dso->symtab_type == DSO_BINARY_TYPE__KALLSYMS &&
1295 !dso__is_kcore(dso))
05ed3ac9 1296 return SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX;
c12944f7 1297
05ed3ac9
ACM
1298 build_id_filename = dso__build_id_filename(dso, NULL, 0);
1299 if (build_id_filename) {
1300 __symbol__join_symfs(filename, filename_size, build_id_filename);
1301 free(build_id_filename);
3caee094 1302 } else {
ee51d851
ACM
1303 if (dso->has_build_id)
1304 return ENOMEM;
78f7defe 1305 goto fallback;
3caee094
ACM
1306 }
1307
6ebd2547
TS
1308 build_id_path = strdup(filename);
1309 if (!build_id_path)
1310 return -1;
1311
1312 dirname(build_id_path);
1313
3caee094 1314 if (dso__is_kcore(dso) ||
6ebd2547 1315 readlink(build_id_path, linkname, sizeof(linkname)) < 0 ||
05ed3ac9
ACM
1316 strstr(linkname, DSO__NAME_KALLSYMS) ||
1317 access(filename, R_OK)) {
78f7defe
ACM
1318fallback:
1319 /*
1320 * If we don't have build-ids or the build-id file isn't in the
1321 * cache, or is just a kallsyms file, well, lets hope that this
1322 * DSO is the same as when 'perf record' ran.
1323 */
05ed3ac9 1324 __symbol__join_symfs(filename, filename_size, dso->long_name);
78f7defe
ACM
1325 }
1326
6ebd2547 1327 free(build_id_path);
05ed3ac9
ACM
1328 return 0;
1329}
1330
786c1b51
ACM
1331static const char *annotate__norm_arch(const char *arch_name)
1332{
1333 struct utsname uts;
1334
1335 if (!arch_name) { /* Assume we are annotating locally. */
1336 if (uname(&uts) < 0)
1337 return NULL;
1338 arch_name = uts.machine;
1339 }
1340 return normalize_arch((char *)arch_name);
1341}
1342
1343int symbol__disassemble(struct symbol *sym, struct map *map, const char *arch_name, size_t privsize)
05ed3ac9
ACM
1344{
1345 struct dso *dso = map->dso;
1346 char command[PATH_MAX * 2];
786c1b51 1347 struct arch *arch = NULL;
05ed3ac9
ACM
1348 FILE *file;
1349 char symfs_filename[PATH_MAX];
1350 struct kcore_extract kce;
1351 bool delete_extract = false;
1352 int stdout_fd[2];
1353 int lineno = 0;
1354 int nline;
1355 pid_t pid;
1356 int err = dso__disassemble_filename(dso, symfs_filename, sizeof(symfs_filename));
1357
1358 if (err)
1359 return err;
1360
786c1b51
ACM
1361 arch_name = annotate__norm_arch(arch_name);
1362 if (!arch_name)
1363 return -1;
1364
1365 arch = arch__find(arch_name);
1366 if (arch == NULL)
1367 return -ENOTSUP;
1368
0781ea92
ACM
1369 if (arch->init) {
1370 err = arch->init(arch);
1371 if (err) {
1372 pr_err("%s: failed to initialize %s arch priv area\n", __func__, arch->name);
1373 return err;
1374 }
1375 }
1376
78f7defe 1377 pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__,
3caee094 1378 symfs_filename, sym->name, map->unmap_ip(map, sym->start),
78f7defe
ACM
1379 map->unmap_ip(map, sym->end));
1380
78f7defe
ACM
1381 pr_debug("annotating [%p] %30s : [%p] %30s\n",
1382 dso, dso->long_name, sym, sym->name);
1383
afba19d9
AH
1384 if (dso__is_kcore(dso)) {
1385 kce.kcore_filename = symfs_filename;
1386 kce.addr = map__rip_2objdump(map, sym->start);
1387 kce.offs = sym->start;
2c241bd3 1388 kce.len = sym->end - sym->start;
afba19d9
AH
1389 if (!kcore_extract__create(&kce)) {
1390 delete_extract = true;
1391 strlcpy(symfs_filename, kce.extract_filename,
1392 sizeof(symfs_filename));
afba19d9 1393 }
2c7da8c5
JO
1394 } else if (dso__needs_decompress(dso)) {
1395 char tmp[PATH_MAX];
1396 struct kmod_path m;
1397 int fd;
1398 bool ret;
1399
1400 if (kmod_path__parse_ext(&m, symfs_filename))
3caee094 1401 goto out;
2c7da8c5
JO
1402
1403 snprintf(tmp, PATH_MAX, "/tmp/perf-kmod-XXXXXX");
1404
1405 fd = mkstemp(tmp);
1406 if (fd < 0) {
1407 free(m.ext);
3caee094 1408 goto out;
2c7da8c5
JO
1409 }
1410
1411 ret = decompress_to_file(m.ext, symfs_filename, fd);
1412
62ec9b3f
AK
1413 if (ret)
1414 pr_err("Cannot decompress %s %s\n", m.ext, symfs_filename);
1415
2c7da8c5
JO
1416 free(m.ext);
1417 close(fd);
1418
1419 if (!ret)
3caee094 1420 goto out;
2c7da8c5
JO
1421
1422 strcpy(symfs_filename, tmp);
afba19d9
AH
1423 }
1424
78f7defe 1425 snprintf(command, sizeof(command),
7a4ec938 1426 "%s %s%s --start-address=0x%016" PRIx64
3e6a2a7f 1427 " --stop-address=0x%016" PRIx64
e7cb9de2 1428 " -l -d %s %s -C %s 2>/dev/null|grep -v %s:|expand",
7a4ec938 1429 objdump_path ? objdump_path : "objdump",
f69b64f7
AK
1430 disassembler_style ? "-M " : "",
1431 disassembler_style ? disassembler_style : "",
78f7defe 1432 map__rip_2objdump(map, sym->start),
2c241bd3 1433 map__rip_2objdump(map, sym->end),
3e6a2a7f
SE
1434 symbol_conf.annotate_asm_raw ? "" : "--no-show-raw",
1435 symbol_conf.annotate_src ? "-S" : "",
3caee094 1436 symfs_filename, symfs_filename);
78f7defe
ACM
1437
1438 pr_debug("Executing: %s\n", command);
1439
9955d0be
ACM
1440 err = -1;
1441 if (pipe(stdout_fd) < 0) {
1442 pr_err("Failure creating the pipe to run %s\n", command);
1443 goto out_remove_tmp;
1444 }
1445
1446 pid = fork();
1447 if (pid < 0) {
1448 pr_err("Failure forking to run %s\n", command);
1449 goto out_close_stdout;
1450 }
1451
1452 if (pid == 0) {
1453 close(stdout_fd[0]);
1454 dup2(stdout_fd[1], 1);
1455 close(stdout_fd[1]);
1456 execl("/bin/sh", "sh", "-c", command, NULL);
1457 perror(command);
1458 exit(-1);
1459 }
1460
1461 close(stdout_fd[1]);
1462
1463 file = fdopen(stdout_fd[0], "r");
62ec9b3f 1464 if (!file) {
9955d0be 1465 pr_err("Failure creating FILE stream for %s\n", command);
62ec9b3f
AK
1466 /*
1467 * If we were using debug info should retry with
1468 * original binary.
1469 */
2c7da8c5 1470 goto out_remove_tmp;
62ec9b3f 1471 }
78f7defe 1472
62ec9b3f
AK
1473 nline = 0;
1474 while (!feof(file)) {
ed7b339f
ACM
1475 /*
1476 * The source code line number (lineno) needs to be kept in
1477 * accross calls to symbol__parse_objdump_line(), so that it
1478 * can associate it with the instructions till the next one.
1479 * See disasm_line__new() and struct disasm_line::line_nr.
1480 */
786c1b51 1481 if (symbol__parse_objdump_line(sym, map, arch, file, privsize,
e592488c 1482 &lineno) < 0)
78f7defe 1483 break;
62ec9b3f
AK
1484 nline++;
1485 }
1486
1487 if (nline == 0)
1488 pr_err("No output from %s\n", command);
78f7defe 1489
484a5e74
AH
1490 /*
1491 * kallsyms does not have symbol sizes so there may a nop at the end.
1492 * Remove it.
1493 */
1494 if (dso__is_kcore(dso))
1495 delete_last_nop(sym);
1496
9955d0be
ACM
1497 fclose(file);
1498 err = 0;
2c7da8c5 1499out_remove_tmp:
9955d0be
ACM
1500 close(stdout_fd[0]);
1501
2c7da8c5
JO
1502 if (dso__needs_decompress(dso))
1503 unlink(symfs_filename);
3caee094 1504
afba19d9
AH
1505 if (delete_extract)
1506 kcore_extract__delete(&kce);
c12944f7 1507out:
78f7defe 1508 return err;
9955d0be
ACM
1509
1510out_close_stdout:
1511 close(stdout_fd[1]);
1512 goto out_remove_tmp;
78f7defe
ACM
1513}
1514
1515static void insert_source_line(struct rb_root *root, struct source_line *src_line)
1516{
1517 struct source_line *iter;
1518 struct rb_node **p = &root->rb_node;
1519 struct rb_node *parent = NULL;
1491c22a 1520 int i, ret;
78f7defe
ACM
1521
1522 while (*p != NULL) {
1523 parent = *p;
1524 iter = rb_entry(parent, struct source_line, node);
1525
41127965
NK
1526 ret = strcmp(iter->path, src_line->path);
1527 if (ret == 0) {
1491c22a 1528 for (i = 0; i < src_line->nr_pcnt; i++)
276af92f 1529 iter->samples[i].percent_sum += src_line->samples[i].percent;
41127965
NK
1530 return;
1531 }
1532
1533 if (ret < 0)
1534 p = &(*p)->rb_left;
1535 else
1536 p = &(*p)->rb_right;
1537 }
1538
1491c22a 1539 for (i = 0; i < src_line->nr_pcnt; i++)
276af92f 1540 src_line->samples[i].percent_sum = src_line->samples[i].percent;
41127965
NK
1541
1542 rb_link_node(&src_line->node, parent, p);
1543 rb_insert_color(&src_line->node, root);
1544}
1545
1491c22a
NK
1546static int cmp_source_line(struct source_line *a, struct source_line *b)
1547{
1548 int i;
1549
1550 for (i = 0; i < a->nr_pcnt; i++) {
276af92f 1551 if (a->samples[i].percent_sum == b->samples[i].percent_sum)
1491c22a 1552 continue;
276af92f 1553 return a->samples[i].percent_sum > b->samples[i].percent_sum;
1491c22a
NK
1554 }
1555
1556 return 0;
1557}
1558
41127965
NK
1559static void __resort_source_line(struct rb_root *root, struct source_line *src_line)
1560{
1561 struct source_line *iter;
1562 struct rb_node **p = &root->rb_node;
1563 struct rb_node *parent = NULL;
1564
1565 while (*p != NULL) {
1566 parent = *p;
1567 iter = rb_entry(parent, struct source_line, node);
1568
1491c22a 1569 if (cmp_source_line(src_line, iter))
78f7defe
ACM
1570 p = &(*p)->rb_left;
1571 else
1572 p = &(*p)->rb_right;
1573 }
1574
1575 rb_link_node(&src_line->node, parent, p);
1576 rb_insert_color(&src_line->node, root);
1577}
1578
41127965
NK
1579static void resort_source_line(struct rb_root *dest_root, struct rb_root *src_root)
1580{
1581 struct source_line *src_line;
1582 struct rb_node *node;
1583
1584 node = rb_first(src_root);
1585 while (node) {
1586 struct rb_node *next;
1587
1588 src_line = rb_entry(node, struct source_line, node);
1589 next = rb_next(node);
1590 rb_erase(node, src_root);
1591
1592 __resort_source_line(dest_root, src_line);
1593 node = next;
1594 }
1595}
1596
78f7defe
ACM
1597static void symbol__free_source_line(struct symbol *sym, int len)
1598{
1599 struct annotation *notes = symbol__annotation(sym);
ce6f4fab 1600 struct source_line *src_line = notes->src->lines;
1491c22a 1601 size_t sizeof_src_line;
78f7defe
ACM
1602 int i;
1603
1491c22a 1604 sizeof_src_line = sizeof(*src_line) +
276af92f 1605 (sizeof(src_line->samples) * (src_line->nr_pcnt - 1));
78f7defe 1606
1491c22a 1607 for (i = 0; i < len; i++) {
f048d548 1608 free_srcline(src_line->path);
1491c22a
NK
1609 src_line = (void *)src_line + sizeof_src_line;
1610 }
1611
04662523 1612 zfree(&notes->src->lines);
78f7defe
ACM
1613}
1614
1615/* Get the filename:line for the colored entries */
1616static int symbol__get_source_line(struct symbol *sym, struct map *map,
db8fd07a 1617 struct perf_evsel *evsel,
86c98cab 1618 struct rb_root *root, int len)
78f7defe
ACM
1619{
1620 u64 start;
1491c22a
NK
1621 int i, k;
1622 int evidx = evsel->idx;
78f7defe
ACM
1623 struct source_line *src_line;
1624 struct annotation *notes = symbol__annotation(sym);
1491c22a 1625 struct sym_hist *h = annotation__histogram(notes, evidx);
41127965 1626 struct rb_root tmp_root = RB_ROOT;
1491c22a
NK
1627 int nr_pcnt = 1;
1628 u64 h_sum = h->sum;
1629 size_t sizeof_src_line = sizeof(struct source_line);
1630
1631 if (perf_evsel__is_group_event(evsel)) {
1632 for (i = 1; i < evsel->nr_members; i++) {
1633 h = annotation__histogram(notes, evidx + i);
1634 h_sum += h->sum;
1635 }
1636 nr_pcnt = evsel->nr_members;
276af92f 1637 sizeof_src_line += (nr_pcnt - 1) * sizeof(src_line->samples);
1491c22a 1638 }
78f7defe 1639
1491c22a 1640 if (!h_sum)
78f7defe
ACM
1641 return 0;
1642
1491c22a 1643 src_line = notes->src->lines = calloc(len, sizeof_src_line);
ce6f4fab 1644 if (!notes->src->lines)
78f7defe
ACM
1645 return -1;
1646
f40a0633 1647 start = map__rip_2objdump(map, sym->start);
78f7defe
ACM
1648
1649 for (i = 0; i < len; i++) {
99094a5e 1650 u64 offset, nr_samples;
1491c22a 1651 double percent_max = 0.0;
78f7defe 1652
1491c22a
NK
1653 src_line->nr_pcnt = nr_pcnt;
1654
1655 for (k = 0; k < nr_pcnt; k++) {
2e933b12
TS
1656 double percent = 0.0;
1657
1491c22a 1658 h = annotation__histogram(notes, evidx + k);
99094a5e 1659 nr_samples = h->addr[i];
2e933b12 1660 if (h->sum)
99094a5e 1661 percent = 100.0 * nr_samples / h->sum;
1491c22a 1662
2e933b12
TS
1663 if (percent > percent_max)
1664 percent_max = percent;
1665 src_line->samples[k].percent = percent;
99094a5e 1666 src_line->samples[k].nr = nr_samples;
1491c22a
NK
1667 }
1668
1669 if (percent_max <= 0.5)
1670 goto next;
78f7defe
ACM
1671
1672 offset = start + i;
5dfa210e
MW
1673 src_line->path = get_srcline(map->dso, offset, NULL,
1674 false, true);
1491c22a 1675 insert_source_line(&tmp_root, src_line);
78f7defe 1676
1491c22a
NK
1677 next:
1678 src_line = (void *)src_line + sizeof_src_line;
78f7defe
ACM
1679 }
1680
41127965 1681 resort_source_line(root, &tmp_root);
78f7defe
ACM
1682 return 0;
1683}
1684
1685static void print_summary(struct rb_root *root, const char *filename)
1686{
1687 struct source_line *src_line;
1688 struct rb_node *node;
1689
1690 printf("\nSorted summary for file %s\n", filename);
1691 printf("----------------------------------------------\n\n");
1692
1693 if (RB_EMPTY_ROOT(root)) {
1694 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
1695 return;
1696 }
1697
1698 node = rb_first(root);
1699 while (node) {
1491c22a 1700 double percent, percent_max = 0.0;
78f7defe
ACM
1701 const char *color;
1702 char *path;
1491c22a 1703 int i;
78f7defe
ACM
1704
1705 src_line = rb_entry(node, struct source_line, node);
1491c22a 1706 for (i = 0; i < src_line->nr_pcnt; i++) {
276af92f 1707 percent = src_line->samples[i].percent_sum;
1491c22a
NK
1708 color = get_percent_color(percent);
1709 color_fprintf(stdout, color, " %7.2f", percent);
1710
1711 if (percent > percent_max)
1712 percent_max = percent;
1713 }
1714
78f7defe 1715 path = src_line->path;
1491c22a 1716 color = get_percent_color(percent_max);
f048d548 1717 color_fprintf(stdout, color, " %s\n", path);
78f7defe 1718
78f7defe
ACM
1719 node = rb_next(node);
1720 }
1721}
1722
db8fd07a 1723static void symbol__annotate_hits(struct symbol *sym, struct perf_evsel *evsel)
78f7defe
ACM
1724{
1725 struct annotation *notes = symbol__annotation(sym);
db8fd07a 1726 struct sym_hist *h = annotation__histogram(notes, evsel->idx);
1b2e2df4 1727 u64 len = symbol__size(sym), offset;
78f7defe
ACM
1728
1729 for (offset = 0; offset < len; ++offset)
1730 if (h->addr[offset] != 0)
1731 printf("%*" PRIx64 ": %" PRIu64 "\n", BITS_PER_LONG / 2,
1732 sym->start + offset, h->addr[offset]);
1733 printf("%*s: %" PRIu64 "\n", BITS_PER_LONG / 2, "h->sum", h->sum);
1734}
1735
db8fd07a
NK
1736int symbol__annotate_printf(struct symbol *sym, struct map *map,
1737 struct perf_evsel *evsel, bool full_paths,
1738 int min_pcnt, int max_lines, int context)
78f7defe
ACM
1739{
1740 struct dso *dso = map->dso;
bfd14b9a
DA
1741 char *filename;
1742 const char *d_filename;
9cdbadce 1743 const char *evsel_name = perf_evsel__name(evsel);
ce6f4fab 1744 struct annotation *notes = symbol__annotation(sym);
135cce1b 1745 struct sym_hist *h = annotation__histogram(notes, evsel->idx);
29ed6e76 1746 struct disasm_line *pos, *queue = NULL;
058b4cc9 1747 u64 start = map__rip_2objdump(map, sym->start);
d5e3d747 1748 int printed = 2, queue_len = 0;
36532461 1749 int more = 0;
78f7defe 1750 u64 len;
b1dd4432 1751 int width = 8;
53dd9b5f 1752 int graph_dotted_len;
78f7defe 1753
bfd14b9a
DA
1754 filename = strdup(dso->long_name);
1755 if (!filename)
1756 return -ENOMEM;
1757
78f7defe
ACM
1758 if (full_paths)
1759 d_filename = filename;
1760 else
1761 d_filename = basename(filename);
1762
1b2e2df4 1763 len = symbol__size(sym);
b1dd4432 1764
759ff497 1765 if (perf_evsel__is_group_event(evsel))
b1dd4432 1766 width *= evsel->nr_members;
78f7defe 1767
135cce1b
PZI
1768 graph_dotted_len = printf(" %-*.*s| Source code & Disassembly of %s for %s (%" PRIu64 " samples)\n",
1769 width, width, "Percent", d_filename, evsel_name, h->sum);
9cdbadce 1770
53dd9b5f 1771 printf("%-*.*s----\n",
9cdbadce 1772 graph_dotted_len, graph_dotted_len, graph_dotted_line);
78f7defe 1773
bb963e16 1774 if (verbose > 0)
db8fd07a 1775 symbol__annotate_hits(sym, evsel);
78f7defe 1776
ce6f4fab 1777 list_for_each_entry(pos, &notes->src->source, node) {
d5e3d747
ACM
1778 if (context && queue == NULL) {
1779 queue = pos;
1780 queue_len = 0;
1781 }
1782
db8fd07a 1783 switch (disasm_line__print(pos, sym, start, evsel, len,
058b4cc9
ACM
1784 min_pcnt, printed, max_lines,
1785 queue)) {
36532461
ACM
1786 case 0:
1787 ++printed;
d5e3d747
ACM
1788 if (context) {
1789 printed += queue_len;
1790 queue = NULL;
1791 queue_len = 0;
1792 }
36532461
ACM
1793 break;
1794 case 1:
1795 /* filtered by max_lines */
1796 ++more;
d040bd36 1797 break;
36532461
ACM
1798 case -1:
1799 default:
d5e3d747
ACM
1800 /*
1801 * Filtered by min_pcnt or non IP lines when
1802 * context != 0
1803 */
1804 if (!context)
1805 break;
1806 if (queue_len == context)
1807 queue = list_entry(queue->node.next, typeof(*queue), node);
1808 else
1809 ++queue_len;
36532461
ACM
1810 break;
1811 }
1812 }
1813
bfd14b9a
DA
1814 free(filename);
1815
36532461
ACM
1816 return more;
1817}
f1e2701d 1818
36532461
ACM
1819void symbol__annotate_zero_histogram(struct symbol *sym, int evidx)
1820{
1821 struct annotation *notes = symbol__annotation(sym);
1822 struct sym_hist *h = annotation__histogram(notes, evidx);
1823
ce6f4fab 1824 memset(h, 0, notes->src->sizeof_sym_hist);
36532461
ACM
1825}
1826
ce6f4fab 1827void symbol__annotate_decay_histogram(struct symbol *sym, int evidx)
36532461
ACM
1828{
1829 struct annotation *notes = symbol__annotation(sym);
1830 struct sym_hist *h = annotation__histogram(notes, evidx);
1b2e2df4 1831 int len = symbol__size(sym), offset;
36532461
ACM
1832
1833 h->sum = 0;
8b84a568
ACM
1834 for (offset = 0; offset < len; ++offset) {
1835 h->addr[offset] = h->addr[offset] * 7 / 8;
1836 h->sum += h->addr[offset];
f1e2701d
ACM
1837 }
1838}
1839
29ed6e76 1840void disasm__purge(struct list_head *head)
f1e2701d 1841{
29ed6e76 1842 struct disasm_line *pos, *n;
f1e2701d
ACM
1843
1844 list_for_each_entry_safe(pos, n, head, node) {
1845 list_del(&pos->node);
29ed6e76 1846 disasm_line__free(pos);
f1e2701d
ACM
1847 }
1848}
1849
5145418b
ACM
1850static size_t disasm_line__fprintf(struct disasm_line *dl, FILE *fp)
1851{
1852 size_t printed;
1853
1854 if (dl->offset == -1)
1855 return fprintf(fp, "%s\n", dl->line);
1856
75b49202 1857 printed = fprintf(fp, "%#" PRIx64 " %s", dl->offset, dl->ins.name);
5145418b 1858
c7e6ead7 1859 if (dl->ops.raw[0] != '\0') {
5145418b 1860 printed += fprintf(fp, "%.*s %s\n", 6 - (int)printed, " ",
c7e6ead7 1861 dl->ops.raw);
5145418b
ACM
1862 }
1863
1864 return printed + fprintf(fp, "\n");
1865}
1866
1867size_t disasm__fprintf(struct list_head *head, FILE *fp)
1868{
1869 struct disasm_line *pos;
1870 size_t printed = 0;
1871
1872 list_for_each_entry(pos, head, node)
1873 printed += disasm_line__fprintf(pos, fp);
1874
1875 return printed;
1876}
1877
db8fd07a
NK
1878int symbol__tty_annotate(struct symbol *sym, struct map *map,
1879 struct perf_evsel *evsel, bool print_lines,
1880 bool full_paths, int min_pcnt, int max_lines)
f1e2701d
ACM
1881{
1882 struct dso *dso = map->dso;
f1e2701d 1883 struct rb_root source_line = RB_ROOT;
f1e2701d
ACM
1884 u64 len;
1885
786c1b51 1886 if (symbol__disassemble(sym, map, perf_evsel__env_arch(evsel), 0) < 0)
f1e2701d
ACM
1887 return -1;
1888
1b2e2df4 1889 len = symbol__size(sym);
f1e2701d
ACM
1890
1891 if (print_lines) {
4a4c03c1 1892 srcline_full_filename = full_paths;
86c98cab
NK
1893 symbol__get_source_line(sym, map, evsel, &source_line, len);
1894 print_summary(&source_line, dso->long_name);
78f7defe
ACM
1895 }
1896
db8fd07a 1897 symbol__annotate_printf(sym, map, evsel, full_paths,
d5e3d747 1898 min_pcnt, max_lines, 0);
78f7defe
ACM
1899 if (print_lines)
1900 symbol__free_source_line(sym, len);
1901
29ed6e76 1902 disasm__purge(&symbol__annotation(sym)->src->source);
f1e2701d 1903
78f7defe
ACM
1904 return 0;
1905}
f626adff 1906
48c65bda
NK
1907bool ui__has_annotation(void)
1908{
2e0453af 1909 return use_browser == 1 && perf_hpp_list.sym;
48c65bda 1910}