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