perf annotate: Fix 32-bit compilation error in util/annotate.c
[linux-2.6-block.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"
e592488c 20#include <regex.h>
ce6f4fab 21#include <pthread.h>
4383db88 22#include <linux/bitops.h>
78f7defe 23
f69b64f7 24const char *disassembler_style;
7a4ec938 25const char *objdump_path;
e592488c 26static regex_t file_lineno;
f69b64f7 27
7a997fe4
ACM
28static struct ins *ins__find(const char *name);
29static int disasm_line__parse(char *line, char **namep, char **rawp);
30
c46219ac
ACM
31static void ins__delete(struct ins_operands *ops)
32{
3995614d
ACM
33 if (ops == NULL)
34 return;
74cf249d
ACM
35 zfree(&ops->source.raw);
36 zfree(&ops->source.name);
37 zfree(&ops->target.raw);
38 zfree(&ops->target.name);
c46219ac
ACM
39}
40
5417072b
ACM
41static int ins__raw_scnprintf(struct ins *ins, char *bf, size_t size,
42 struct ins_operands *ops)
43{
44 return scnprintf(bf, size, "%-6.6s %s", ins->name, ops->raw);
45}
46
47int ins__scnprintf(struct ins *ins, char *bf, size_t size,
48 struct ins_operands *ops)
49{
50 if (ins->ops->scnprintf)
51 return ins->ops->scnprintf(ins, bf, size, ops);
52
53 return ins__raw_scnprintf(ins, bf, size, ops);
54}
55
c7e6ead7 56static int call__parse(struct ins_operands *ops)
d86b0597 57{
d2232885
ACM
58 char *endptr, *tok, *name;
59
44d1a3ed 60 ops->target.addr = strtoull(ops->raw, &endptr, 16);
d2232885
ACM
61
62 name = strchr(endptr, '<');
63 if (name == NULL)
64 goto indirect_call;
65
66 name++;
67
68 tok = strchr(name, '>');
69 if (tok == NULL)
70 return -1;
71
72 *tok = '\0';
44d1a3ed 73 ops->target.name = strdup(name);
d2232885
ACM
74 *tok = '>';
75
44d1a3ed 76 return ops->target.name == NULL ? -1 : 0;
d2232885
ACM
77
78indirect_call:
e8ea1561
ACM
79 tok = strchr(endptr, '(');
80 if (tok != NULL) {
81 ops->target.addr = 0;
82 return 0;
83 }
84
d2232885
ACM
85 tok = strchr(endptr, '*');
86 if (tok == NULL)
87 return -1;
88
44d1a3ed 89 ops->target.addr = strtoull(tok + 1, NULL, 16);
d86b0597
ACM
90 return 0;
91}
92
d2232885 93static int call__scnprintf(struct ins *ins, char *bf, size_t size,
5417072b 94 struct ins_operands *ops)
d2232885 95{
44d1a3ed
ACM
96 if (ops->target.name)
97 return scnprintf(bf, size, "%-6.6s %s", ins->name, ops->target.name);
d2232885 98
e8ea1561
ACM
99 if (ops->target.addr == 0)
100 return ins__raw_scnprintf(ins, bf, size, ops);
101
44d1a3ed 102 return scnprintf(bf, size, "%-6.6s *%" PRIx64, ins->name, ops->target.addr);
d2232885
ACM
103}
104
d86b0597 105static struct ins_ops call_ops = {
d2232885
ACM
106 .parse = call__parse,
107 .scnprintf = call__scnprintf,
d86b0597
ACM
108};
109
110bool ins__is_call(const struct ins *ins)
111{
112 return ins->ops == &call_ops;
113}
114
c7e6ead7 115static int jump__parse(struct ins_operands *ops)
4f9d0325 116{
c7e6ead7 117 const char *s = strchr(ops->raw, '+');
4f9d0325 118
bbb7f846 119 ops->target.addr = strtoull(ops->raw, NULL, 16);
fb29fa58
ACM
120
121 if (s++ != NULL)
bbb7f846 122 ops->target.offset = strtoull(s, NULL, 16);
fb29fa58
ACM
123 else
124 ops->target.offset = UINT64_MAX;
4f9d0325 125
4f9d0325
ACM
126 return 0;
127}
128
c7e6ead7 129static int jump__scnprintf(struct ins *ins, char *bf, size_t size,
5417072b 130 struct ins_operands *ops)
28548d78 131{
44d1a3ed 132 return scnprintf(bf, size, "%-6.6s %" PRIx64, ins->name, ops->target.offset);
28548d78
ACM
133}
134
4f9d0325 135static struct ins_ops jump_ops = {
c7e6ead7
ACM
136 .parse = jump__parse,
137 .scnprintf = jump__scnprintf,
4f9d0325
ACM
138};
139
140bool ins__is_jump(const struct ins *ins)
141{
142 return ins->ops == &jump_ops;
143}
144
6de783b6
ACM
145static int comment__symbol(char *raw, char *comment, u64 *addrp, char **namep)
146{
147 char *endptr, *name, *t;
148
149 if (strstr(raw, "(%rip)") == NULL)
150 return 0;
151
152 *addrp = strtoull(comment, &endptr, 16);
153 name = strchr(endptr, '<');
154 if (name == NULL)
155 return -1;
156
157 name++;
158
159 t = strchr(name, '>');
160 if (t == NULL)
161 return 0;
162
163 *t = '\0';
164 *namep = strdup(name);
165 *t = '>';
166
167 return 0;
168}
169
7a997fe4
ACM
170static int lock__parse(struct ins_operands *ops)
171{
172 char *name;
173
174 ops->locked.ops = zalloc(sizeof(*ops->locked.ops));
175 if (ops->locked.ops == NULL)
176 return 0;
177
178 if (disasm_line__parse(ops->raw, &name, &ops->locked.ops->raw) < 0)
179 goto out_free_ops;
180
2ba34aaa 181 ops->locked.ins = ins__find(name);
0fb9f2aa
RV
182 free(name);
183
2ba34aaa
NK
184 if (ops->locked.ins == NULL)
185 goto out_free_ops;
7a997fe4 186
2ba34aaa
NK
187 if (!ops->locked.ins->ops)
188 return 0;
7a997fe4 189
be81908c
RV
190 if (ops->locked.ins->ops->parse &&
191 ops->locked.ins->ops->parse(ops->locked.ops) < 0)
192 goto out_free_ops;
7a997fe4
ACM
193
194 return 0;
195
196out_free_ops:
04662523 197 zfree(&ops->locked.ops);
7a997fe4
ACM
198 return 0;
199}
200
201static int lock__scnprintf(struct ins *ins, char *bf, size_t size,
202 struct ins_operands *ops)
203{
204 int printed;
205
206 if (ops->locked.ins == NULL)
207 return ins__raw_scnprintf(ins, bf, size, ops);
208
209 printed = scnprintf(bf, size, "%-6.6s ", ins->name);
210 return printed + ins__scnprintf(ops->locked.ins, bf + printed,
211 size - printed, ops->locked.ops);
212}
213
c46219ac
ACM
214static void lock__delete(struct ins_operands *ops)
215{
0fb9f2aa
RV
216 struct ins *ins = ops->locked.ins;
217
218 if (ins && ins->ops->free)
219 ins->ops->free(ops->locked.ops);
220 else
221 ins__delete(ops->locked.ops);
222
74cf249d
ACM
223 zfree(&ops->locked.ops);
224 zfree(&ops->target.raw);
225 zfree(&ops->target.name);
c46219ac
ACM
226}
227
7a997fe4 228static struct ins_ops lock_ops = {
c46219ac 229 .free = lock__delete,
7a997fe4
ACM
230 .parse = lock__parse,
231 .scnprintf = lock__scnprintf,
232};
233
6de783b6
ACM
234static int mov__parse(struct ins_operands *ops)
235{
236 char *s = strchr(ops->raw, ','), *target, *comment, prev;
237
238 if (s == NULL)
239 return -1;
240
241 *s = '\0';
242 ops->source.raw = strdup(ops->raw);
243 *s = ',';
48000a1a 244
6de783b6
ACM
245 if (ops->source.raw == NULL)
246 return -1;
247
248 target = ++s;
1e2bb043
AC
249 comment = strchr(s, '#');
250
251 if (comment != NULL)
252 s = comment - 1;
253 else
254 s = strchr(s, '\0') - 1;
6de783b6 255
1e2bb043
AC
256 while (s > target && isspace(s[0]))
257 --s;
258 s++;
6de783b6
ACM
259 prev = *s;
260 *s = '\0';
261
262 ops->target.raw = strdup(target);
263 *s = prev;
264
265 if (ops->target.raw == NULL)
266 goto out_free_source;
267
6de783b6
ACM
268 if (comment == NULL)
269 return 0;
270
271 while (comment[0] != '\0' && isspace(comment[0]))
272 ++comment;
273
274 comment__symbol(ops->source.raw, comment, &ops->source.addr, &ops->source.name);
275 comment__symbol(ops->target.raw, comment, &ops->target.addr, &ops->target.name);
276
277 return 0;
278
279out_free_source:
04662523 280 zfree(&ops->source.raw);
6de783b6
ACM
281 return -1;
282}
283
284static int mov__scnprintf(struct ins *ins, char *bf, size_t size,
285 struct ins_operands *ops)
286{
287 return scnprintf(bf, size, "%-6.6s %s,%s", ins->name,
288 ops->source.name ?: ops->source.raw,
289 ops->target.name ?: ops->target.raw);
290}
291
292static struct ins_ops mov_ops = {
293 .parse = mov__parse,
294 .scnprintf = mov__scnprintf,
295};
296
a43712c4
ACM
297static int dec__parse(struct ins_operands *ops)
298{
299 char *target, *comment, *s, prev;
300
301 target = s = ops->raw;
302
303 while (s[0] != '\0' && !isspace(s[0]))
304 ++s;
305 prev = *s;
306 *s = '\0';
307
308 ops->target.raw = strdup(target);
309 *s = prev;
310
311 if (ops->target.raw == NULL)
312 return -1;
313
314 comment = strchr(s, '#');
315 if (comment == NULL)
316 return 0;
317
318 while (comment[0] != '\0' && isspace(comment[0]))
319 ++comment;
320
321 comment__symbol(ops->target.raw, comment, &ops->target.addr, &ops->target.name);
322
323 return 0;
324}
325
326static int dec__scnprintf(struct ins *ins, char *bf, size_t size,
327 struct ins_operands *ops)
328{
329 return scnprintf(bf, size, "%-6.6s %s", ins->name,
330 ops->target.name ?: ops->target.raw);
331}
332
333static struct ins_ops dec_ops = {
334 .parse = dec__parse,
335 .scnprintf = dec__scnprintf,
336};
337
1d037ca1
IT
338static int nop__scnprintf(struct ins *ins __maybe_unused, char *bf, size_t size,
339 struct ins_operands *ops __maybe_unused)
b9818e93
ACM
340{
341 return scnprintf(bf, size, "%-6.6s", "nop");
342}
343
344static struct ins_ops nop_ops = {
345 .scnprintf = nop__scnprintf,
346};
347
4f9d0325
ACM
348/*
349 * Must be sorted by name!
350 */
351static struct ins instructions[] = {
6de783b6
ACM
352 { .name = "add", .ops = &mov_ops, },
353 { .name = "addl", .ops = &mov_ops, },
354 { .name = "addq", .ops = &mov_ops, },
355 { .name = "addw", .ops = &mov_ops, },
356 { .name = "and", .ops = &mov_ops, },
7a997fe4 357 { .name = "bts", .ops = &mov_ops, },
d86b0597
ACM
358 { .name = "call", .ops = &call_ops, },
359 { .name = "callq", .ops = &call_ops, },
6de783b6
ACM
360 { .name = "cmp", .ops = &mov_ops, },
361 { .name = "cmpb", .ops = &mov_ops, },
362 { .name = "cmpl", .ops = &mov_ops, },
363 { .name = "cmpq", .ops = &mov_ops, },
364 { .name = "cmpw", .ops = &mov_ops, },
365 { .name = "cmpxch", .ops = &mov_ops, },
a43712c4
ACM
366 { .name = "dec", .ops = &dec_ops, },
367 { .name = "decl", .ops = &dec_ops, },
6de783b6 368 { .name = "imul", .ops = &mov_ops, },
a43712c4
ACM
369 { .name = "inc", .ops = &dec_ops, },
370 { .name = "incl", .ops = &dec_ops, },
4f9d0325 371 { .name = "ja", .ops = &jump_ops, },
3f862fd0
ACM
372 { .name = "jae", .ops = &jump_ops, },
373 { .name = "jb", .ops = &jump_ops, },
374 { .name = "jbe", .ops = &jump_ops, },
375 { .name = "jc", .ops = &jump_ops, },
376 { .name = "jcxz", .ops = &jump_ops, },
4f9d0325 377 { .name = "je", .ops = &jump_ops, },
3f862fd0
ACM
378 { .name = "jecxz", .ops = &jump_ops, },
379 { .name = "jg", .ops = &jump_ops, },
380 { .name = "jge", .ops = &jump_ops, },
381 { .name = "jl", .ops = &jump_ops, },
382 { .name = "jle", .ops = &jump_ops, },
4f9d0325
ACM
383 { .name = "jmp", .ops = &jump_ops, },
384 { .name = "jmpq", .ops = &jump_ops, },
3f862fd0
ACM
385 { .name = "jna", .ops = &jump_ops, },
386 { .name = "jnae", .ops = &jump_ops, },
387 { .name = "jnb", .ops = &jump_ops, },
388 { .name = "jnbe", .ops = &jump_ops, },
389 { .name = "jnc", .ops = &jump_ops, },
4f9d0325 390 { .name = "jne", .ops = &jump_ops, },
3f862fd0
ACM
391 { .name = "jng", .ops = &jump_ops, },
392 { .name = "jnge", .ops = &jump_ops, },
393 { .name = "jnl", .ops = &jump_ops, },
394 { .name = "jnle", .ops = &jump_ops, },
395 { .name = "jno", .ops = &jump_ops, },
396 { .name = "jnp", .ops = &jump_ops, },
397 { .name = "jns", .ops = &jump_ops, },
398 { .name = "jnz", .ops = &jump_ops, },
399 { .name = "jo", .ops = &jump_ops, },
400 { .name = "jp", .ops = &jump_ops, },
401 { .name = "jpe", .ops = &jump_ops, },
402 { .name = "jpo", .ops = &jump_ops, },
403 { .name = "jrcxz", .ops = &jump_ops, },
4f9d0325 404 { .name = "js", .ops = &jump_ops, },
3f862fd0 405 { .name = "jz", .ops = &jump_ops, },
6de783b6 406 { .name = "lea", .ops = &mov_ops, },
7a997fe4 407 { .name = "lock", .ops = &lock_ops, },
6de783b6
ACM
408 { .name = "mov", .ops = &mov_ops, },
409 { .name = "movb", .ops = &mov_ops, },
410 { .name = "movdqa",.ops = &mov_ops, },
411 { .name = "movl", .ops = &mov_ops, },
412 { .name = "movq", .ops = &mov_ops, },
413 { .name = "movslq", .ops = &mov_ops, },
414 { .name = "movzbl", .ops = &mov_ops, },
415 { .name = "movzwl", .ops = &mov_ops, },
b9818e93
ACM
416 { .name = "nop", .ops = &nop_ops, },
417 { .name = "nopl", .ops = &nop_ops, },
418 { .name = "nopw", .ops = &nop_ops, },
6de783b6
ACM
419 { .name = "or", .ops = &mov_ops, },
420 { .name = "orl", .ops = &mov_ops, },
421 { .name = "test", .ops = &mov_ops, },
422 { .name = "testb", .ops = &mov_ops, },
423 { .name = "testl", .ops = &mov_ops, },
7a997fe4 424 { .name = "xadd", .ops = &mov_ops, },
ffadcf09
AK
425 { .name = "xbeginl", .ops = &jump_ops, },
426 { .name = "xbeginq", .ops = &jump_ops, },
4f9d0325
ACM
427};
428
429static int ins__cmp(const void *name, const void *insp)
430{
431 const struct ins *ins = insp;
432
433 return strcmp(name, ins->name);
434}
435
436static struct ins *ins__find(const char *name)
437{
438 const int nmemb = ARRAY_SIZE(instructions);
439
440 return bsearch(name, instructions, nmemb, sizeof(struct ins), ins__cmp);
441}
442
1d037ca1 443int symbol__annotate_init(struct map *map __maybe_unused, struct symbol *sym)
78f7defe
ACM
444{
445 struct annotation *notes = symbol__annotation(sym);
ce6f4fab
ACM
446 pthread_mutex_init(&notes->lock, NULL);
447 return 0;
448}
78f7defe 449
d04b35f8 450int symbol__alloc_hist(struct symbol *sym)
ce6f4fab
ACM
451{
452 struct annotation *notes = symbol__annotation(sym);
1b2e2df4 453 const size_t size = symbol__size(sym);
8696329b
CS
454 size_t sizeof_sym_hist;
455
456 /* Check for overflow when calculating sizeof_sym_hist */
457 if (size > (SIZE_MAX - sizeof(struct sym_hist)) / sizeof(u64))
458 return -1;
459
460 sizeof_sym_hist = (sizeof(struct sym_hist) + size * sizeof(u64));
461
462 /* Check for overflow in zalloc argument */
463 if (sizeof_sym_hist > (SIZE_MAX - sizeof(*notes->src))
464 / symbol_conf.nr_events)
465 return -1;
ce6f4fab 466
d04b35f8 467 notes->src = zalloc(sizeof(*notes->src) + symbol_conf.nr_events * sizeof_sym_hist);
ce6f4fab
ACM
468 if (notes->src == NULL)
469 return -1;
470 notes->src->sizeof_sym_hist = sizeof_sym_hist;
d04b35f8 471 notes->src->nr_histograms = symbol_conf.nr_events;
ce6f4fab
ACM
472 INIT_LIST_HEAD(&notes->src->source);
473 return 0;
78f7defe
ACM
474}
475
d4957633
AK
476/* The cycles histogram is lazily allocated. */
477static int symbol__alloc_hist_cycles(struct symbol *sym)
478{
479 struct annotation *notes = symbol__annotation(sym);
480 const size_t size = symbol__size(sym);
481
482 notes->src->cycles_hist = calloc(size, sizeof(struct cyc_hist));
483 if (notes->src->cycles_hist == NULL)
484 return -1;
485 return 0;
486}
487
36532461
ACM
488void symbol__annotate_zero_histograms(struct symbol *sym)
489{
490 struct annotation *notes = symbol__annotation(sym);
491
ce6f4fab 492 pthread_mutex_lock(&notes->lock);
d4957633 493 if (notes->src != NULL) {
ce6f4fab
ACM
494 memset(notes->src->histograms, 0,
495 notes->src->nr_histograms * notes->src->sizeof_sym_hist);
d4957633
AK
496 if (notes->src->cycles_hist)
497 memset(notes->src->cycles_hist, 0,
498 symbol__size(sym) * sizeof(struct cyc_hist));
499 }
ce6f4fab 500 pthread_mutex_unlock(&notes->lock);
36532461
ACM
501}
502
d4957633
AK
503static int __symbol__account_cycles(struct annotation *notes,
504 u64 start,
505 unsigned offset, unsigned cycles,
506 unsigned have_start)
507{
508 struct cyc_hist *ch;
509
510 ch = notes->src->cycles_hist;
511 /*
512 * For now we can only account one basic block per
513 * final jump. But multiple could be overlapping.
514 * Always account the longest one. So when
515 * a shorter one has been already seen throw it away.
516 *
517 * We separately always account the full cycles.
518 */
519 ch[offset].num_aggr++;
520 ch[offset].cycles_aggr += cycles;
521
522 if (!have_start && ch[offset].have_start)
523 return 0;
524 if (ch[offset].num) {
525 if (have_start && (!ch[offset].have_start ||
526 ch[offset].start > start)) {
527 ch[offset].have_start = 0;
528 ch[offset].cycles = 0;
529 ch[offset].num = 0;
530 if (ch[offset].reset < 0xffff)
531 ch[offset].reset++;
532 } else if (have_start &&
533 ch[offset].start < start)
534 return 0;
535 }
536 ch[offset].have_start = have_start;
537 ch[offset].start = start;
538 ch[offset].cycles += cycles;
539 ch[offset].num++;
540 return 0;
541}
542
b66d8c0c
ACM
543static int __symbol__inc_addr_samples(struct symbol *sym, struct map *map,
544 struct annotation *notes, int evidx, u64 addr)
78f7defe 545{
2f525d01 546 unsigned offset;
78f7defe
ACM
547 struct sym_hist *h;
548
78f7defe
ACM
549 pr_debug3("%s: addr=%#" PRIx64 "\n", __func__, map->unmap_ip(map, addr));
550
2c241bd3 551 if (addr < sym->start || addr >= sym->end)
31d68e7b 552 return -ERANGE;
78f7defe 553
2f525d01
ACM
554 offset = addr - sym->start;
555 h = annotation__histogram(notes, evidx);
78f7defe
ACM
556 h->sum++;
557 h->addr[offset]++;
558
559 pr_debug3("%#" PRIx64 " %s: period++ [addr: %#" PRIx64 ", %#" PRIx64
2f525d01
ACM
560 ", evidx=%d] => %" PRIu64 "\n", sym->start, sym->name,
561 addr, addr - sym->start, evidx, h->addr[offset]);
78f7defe
ACM
562 return 0;
563}
564
d4957633 565static struct annotation *symbol__get_annotation(struct symbol *sym, bool cycles)
83be34a7
AK
566{
567 struct annotation *notes = symbol__annotation(sym);
568
569 if (notes->src == NULL) {
570 if (symbol__alloc_hist(sym) < 0)
571 return NULL;
572 }
d4957633
AK
573 if (!notes->src->cycles_hist && cycles) {
574 if (symbol__alloc_hist_cycles(sym) < 0)
575 return NULL;
576 }
83be34a7
AK
577 return notes;
578}
579
44e83039
ACM
580static int symbol__inc_addr_samples(struct symbol *sym, struct map *map,
581 int evidx, u64 addr)
b66d8c0c
ACM
582{
583 struct annotation *notes;
584
48c65bda 585 if (sym == NULL)
b66d8c0c 586 return 0;
d4957633 587 notes = symbol__get_annotation(sym, false);
83be34a7
AK
588 if (notes == NULL)
589 return -ENOMEM;
b66d8c0c
ACM
590 return __symbol__inc_addr_samples(sym, map, notes, evidx, addr);
591}
592
d4957633
AK
593static int symbol__account_cycles(u64 addr, u64 start,
594 struct symbol *sym, unsigned cycles)
595{
596 struct annotation *notes;
597 unsigned offset;
598
599 if (sym == NULL)
600 return 0;
601 notes = symbol__get_annotation(sym, true);
602 if (notes == NULL)
603 return -ENOMEM;
604 if (addr < sym->start || addr >= sym->end)
605 return -ERANGE;
606
607 if (start) {
608 if (start < sym->start || start >= sym->end)
609 return -ERANGE;
610 if (start >= addr)
611 start = 0;
612 }
613 offset = addr - sym->start;
614 return __symbol__account_cycles(notes,
615 start ? start - sym->start : 0,
616 offset, cycles,
617 !!start);
618}
619
620int addr_map_symbol__account_cycles(struct addr_map_symbol *ams,
621 struct addr_map_symbol *start,
622 unsigned cycles)
623{
3d7245b0 624 u64 saddr = 0;
d4957633
AK
625 int err;
626
627 if (!cycles)
628 return 0;
629
630 /*
631 * Only set start when IPC can be computed. We can only
632 * compute it when the basic block is completely in a single
633 * function.
634 * Special case the case when the jump is elsewhere, but
635 * it starts on the function start.
636 */
637 if (start &&
638 (start->sym == ams->sym ||
639 (ams->sym &&
640 start->addr == ams->sym->start + ams->map->start)))
641 saddr = start->al_addr;
642 if (saddr == 0)
3d7245b0 643 pr_debug2("BB with bad start: addr %"PRIx64" start %"PRIx64" sym %"PRIx64" saddr %"PRIx64"\n",
d4957633
AK
644 ams->addr,
645 start ? start->addr : 0,
646 ams->sym ? ams->sym->start + ams->map->start : 0,
647 saddr);
648 err = symbol__account_cycles(ams->al_addr, saddr, ams->sym, cycles);
649 if (err)
650 pr_debug2("account_cycles failed %d\n", err);
651 return err;
652}
653
0f4e7a24
ACM
654int addr_map_symbol__inc_samples(struct addr_map_symbol *ams, int evidx)
655{
656 return symbol__inc_addr_samples(ams->sym, ams->map, evidx, ams->al_addr);
657}
658
f626adff
ACM
659int hist_entry__inc_addr_samples(struct hist_entry *he, int evidx, u64 ip)
660{
661 return symbol__inc_addr_samples(he->ms.sym, he->ms.map, evidx, ip);
662}
663
4f9d0325
ACM
664static void disasm_line__init_ins(struct disasm_line *dl)
665{
666 dl->ins = ins__find(dl->name);
667
668 if (dl->ins == NULL)
669 return;
670
671 if (!dl->ins->ops)
672 return;
673
be81908c
RV
674 if (dl->ins->ops->parse && dl->ins->ops->parse(&dl->ops) < 0)
675 dl->ins = NULL;
4f9d0325
ACM
676}
677
7a997fe4
ACM
678static int disasm_line__parse(char *line, char **namep, char **rawp)
679{
680 char *name = line, tmp;
681
682 while (isspace(name[0]))
683 ++name;
684
685 if (name[0] == '\0')
686 return -1;
687
688 *rawp = name + 1;
689
690 while ((*rawp)[0] != '\0' && !isspace((*rawp)[0]))
691 ++*rawp;
692
693 tmp = (*rawp)[0];
694 (*rawp)[0] = '\0';
695 *namep = strdup(name);
696
697 if (*namep == NULL)
698 goto out_free_name;
699
700 (*rawp)[0] = tmp;
701
702 if ((*rawp)[0] != '\0') {
703 (*rawp)++;
704 while (isspace((*rawp)[0]))
705 ++(*rawp);
706 }
707
708 return 0;
709
710out_free_name:
04662523 711 zfree(namep);
7a997fe4
ACM
712 return -1;
713}
714
e592488c
AK
715static struct disasm_line *disasm_line__new(s64 offset, char *line,
716 size_t privsize, int line_nr)
78f7defe 717{
5145418b 718 struct disasm_line *dl = zalloc(sizeof(*dl) + privsize);
78f7defe 719
29ed6e76
ACM
720 if (dl != NULL) {
721 dl->offset = offset;
722 dl->line = strdup(line);
e592488c 723 dl->line_nr = line_nr;
29ed6e76 724 if (dl->line == NULL)
058b4cc9 725 goto out_delete;
5145418b
ACM
726
727 if (offset != -1) {
7a997fe4 728 if (disasm_line__parse(dl->line, &dl->name, &dl->ops.raw) < 0)
5145418b
ACM
729 goto out_free_line;
730
4f9d0325 731 disasm_line__init_ins(dl);
5145418b 732 }
78f7defe
ACM
733 }
734
29ed6e76 735 return dl;
5145418b
ACM
736
737out_free_line:
74cf249d 738 zfree(&dl->line);
058b4cc9 739out_delete:
29ed6e76 740 free(dl);
058b4cc9 741 return NULL;
78f7defe
ACM
742}
743
29ed6e76 744void disasm_line__free(struct disasm_line *dl)
78f7defe 745{
74cf249d
ACM
746 zfree(&dl->line);
747 zfree(&dl->name);
c46219ac
ACM
748 if (dl->ins && dl->ins->ops->free)
749 dl->ins->ops->free(&dl->ops);
750 else
751 ins__delete(&dl->ops);
29ed6e76 752 free(dl);
78f7defe
ACM
753}
754
5417072b
ACM
755int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size, bool raw)
756{
757 if (raw || !dl->ins)
758 return scnprintf(bf, size, "%-6.6s %s", dl->name, dl->ops.raw);
759
760 return ins__scnprintf(dl->ins, bf, size, &dl->ops);
761}
762
29ed6e76 763static void disasm__add(struct list_head *head, struct disasm_line *line)
78f7defe
ACM
764{
765 list_add_tail(&line->node, head);
766}
767
29ed6e76 768struct disasm_line *disasm__get_next_ip_line(struct list_head *head, struct disasm_line *pos)
78f7defe
ACM
769{
770 list_for_each_entry_continue(pos, head, node)
771 if (pos->offset >= 0)
772 return pos;
773
774 return NULL;
775}
776
e64aa75b 777double disasm__calc_percent(struct annotation *notes, int evidx, s64 offset,
0c4a5bce 778 s64 end, const char **path, u64 *nr_samples)
e5ccf9f4
NK
779{
780 struct source_line *src_line = notes->src->lines;
e5ccf9f4 781 double percent = 0.0;
0c4a5bce 782 *nr_samples = 0;
e5ccf9f4 783
bd64fcb8 784 if (src_line) {
1491c22a 785 size_t sizeof_src_line = sizeof(*src_line) +
276af92f 786 sizeof(src_line->samples) * (src_line->nr_pcnt - 1);
1491c22a 787
bd64fcb8 788 while (offset < end) {
1491c22a
NK
789 src_line = (void *)notes->src->lines +
790 (sizeof_src_line * offset);
791
e5ccf9f4 792 if (*path == NULL)
1491c22a 793 *path = src_line->path;
e5ccf9f4 794
276af92f
ACM
795 percent += src_line->samples[evidx].percent;
796 *nr_samples += src_line->samples[evidx].nr;
1491c22a 797 offset++;
bd64fcb8
NK
798 }
799 } else {
1491c22a
NK
800 struct sym_hist *h = annotation__histogram(notes, evidx);
801 unsigned int hits = 0;
802
bd64fcb8
NK
803 while (offset < end)
804 hits += h->addr[offset++];
e5ccf9f4 805
0c4a5bce
ML
806 if (h->sum) {
807 *nr_samples = hits;
bd64fcb8 808 percent = 100.0 * hits / h->sum;
0c4a5bce 809 }
bd64fcb8 810 }
e5ccf9f4
NK
811
812 return percent;
813}
814
29ed6e76 815static int disasm_line__print(struct disasm_line *dl, struct symbol *sym, u64 start,
db8fd07a 816 struct perf_evsel *evsel, u64 len, int min_pcnt, int printed,
29ed6e76 817 int max_lines, struct disasm_line *queue)
78f7defe
ACM
818{
819 static const char *prev_line;
820 static const char *prev_color;
821
29ed6e76 822 if (dl->offset != -1) {
78f7defe 823 const char *path = NULL;
0c4a5bce 824 u64 nr_samples;
b1dd4432
NK
825 double percent, max_percent = 0.0;
826 double *ppercents = &percent;
0c4a5bce 827 u64 *psamples = &nr_samples;
b1dd4432 828 int i, nr_percent = 1;
78f7defe
ACM
829 const char *color;
830 struct annotation *notes = symbol__annotation(sym);
29ed6e76 831 s64 offset = dl->offset;
058b4cc9 832 const u64 addr = start + offset;
29ed6e76 833 struct disasm_line *next;
ce6f4fab 834
29ed6e76 835 next = disasm__get_next_ip_line(&notes->src->source, dl);
78f7defe 836
759ff497 837 if (perf_evsel__is_group_event(evsel)) {
b1dd4432
NK
838 nr_percent = evsel->nr_members;
839 ppercents = calloc(nr_percent, sizeof(double));
0c4a5bce
ML
840 psamples = calloc(nr_percent, sizeof(u64));
841 if (ppercents == NULL || psamples == NULL) {
b1dd4432 842 return -1;
0c4a5bce 843 }
b1dd4432
NK
844 }
845
846 for (i = 0; i < nr_percent; i++) {
847 percent = disasm__calc_percent(notes,
1491c22a
NK
848 notes->src->lines ? i : evsel->idx + i,
849 offset,
850 next ? next->offset : (s64) len,
0c4a5bce 851 &path, &nr_samples);
b1dd4432
NK
852
853 ppercents[i] = percent;
0c4a5bce 854 psamples[i] = nr_samples;
b1dd4432
NK
855 if (percent > max_percent)
856 max_percent = percent;
857 }
858
859 if (max_percent < min_pcnt)
36532461
ACM
860 return -1;
861
e3087b80 862 if (max_lines && printed >= max_lines)
36532461 863 return 1;
d040bd36 864
d5e3d747
ACM
865 if (queue != NULL) {
866 list_for_each_entry_from(queue, &notes->src->source, node) {
29ed6e76 867 if (queue == dl)
d5e3d747 868 break;
db8fd07a 869 disasm_line__print(queue, sym, start, evsel, len,
d5e3d747
ACM
870 0, 0, 1, NULL);
871 }
872 }
873
b1dd4432 874 color = get_percent_color(max_percent);
78f7defe
ACM
875
876 /*
877 * Also color the filename and line if needed, with
878 * the same color than the percentage. Don't print it
879 * twice for close colored addr with the same filename:line
880 */
881 if (path) {
882 if (!prev_line || strcmp(prev_line, path)
883 || color != prev_color) {
884 color_fprintf(stdout, color, " %s", path);
885 prev_line = path;
886 prev_color = color;
887 }
888 }
889
b1dd4432
NK
890 for (i = 0; i < nr_percent; i++) {
891 percent = ppercents[i];
0c4a5bce 892 nr_samples = psamples[i];
b1dd4432 893 color = get_percent_color(percent);
0c4a5bce
ML
894
895 if (symbol_conf.show_total_period)
896 color_fprintf(stdout, color, " %7" PRIu64,
897 nr_samples);
898 else
899 color_fprintf(stdout, color, " %7.2f", percent);
b1dd4432
NK
900 }
901
78f7defe 902 printf(" : ");
058b4cc9 903 color_fprintf(stdout, PERF_COLOR_MAGENTA, " %" PRIx64 ":", addr);
29ed6e76 904 color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", dl->line);
b1dd4432
NK
905
906 if (ppercents != &percent)
907 free(ppercents);
908
0c4a5bce
ML
909 if (psamples != &nr_samples)
910 free(psamples);
911
e3087b80 912 } else if (max_lines && printed >= max_lines)
36532461
ACM
913 return 1;
914 else {
b1dd4432
NK
915 int width = 8;
916
d5e3d747
ACM
917 if (queue)
918 return -1;
919
759ff497 920 if (perf_evsel__is_group_event(evsel))
b1dd4432
NK
921 width *= evsel->nr_members;
922
29ed6e76 923 if (!*dl->line)
b1dd4432 924 printf(" %*s:\n", width, " ");
78f7defe 925 else
b1dd4432 926 printf(" %*s: %s\n", width, " ", dl->line);
78f7defe 927 }
36532461
ACM
928
929 return 0;
78f7defe
ACM
930}
931
3aec150a
NK
932/*
933 * symbol__parse_objdump_line() parses objdump output (with -d --no-show-raw)
934 * which looks like following
935 *
936 * 0000000000415500 <_init>:
937 * 415500: sub $0x8,%rsp
938 * 415504: mov 0x2f5ad5(%rip),%rax # 70afe0 <_DYNAMIC+0x2f8>
939 * 41550b: test %rax,%rax
940 * 41550e: je 415515 <_init+0x15>
941 * 415510: callq 416e70 <__gmon_start__@plt>
942 * 415515: add $0x8,%rsp
943 * 415519: retq
944 *
945 * it will be parsed and saved into struct disasm_line as
946 * <offset> <name> <ops.raw>
947 *
948 * The offset will be a relative offset from the start of the symbol and -1
949 * means that it's not a disassembly line so should be treated differently.
950 * The ops.raw part will be parsed further according to type of the instruction.
951 */
ce6f4fab 952static int symbol__parse_objdump_line(struct symbol *sym, struct map *map,
e592488c
AK
953 FILE *file, size_t privsize,
954 int *line_nr)
78f7defe 955{
ce6f4fab 956 struct annotation *notes = symbol__annotation(sym);
29ed6e76 957 struct disasm_line *dl;
058b4cc9 958 char *line = NULL, *parsed_line, *tmp, *tmp2, *c;
78f7defe
ACM
959 size_t line_len;
960 s64 line_ip, offset = -1;
e592488c 961 regmatch_t match[2];
78f7defe
ACM
962
963 if (getline(&line, &line_len, file) < 0)
964 return -1;
965
966 if (!line)
967 return -1;
968
969 while (line_len != 0 && isspace(line[line_len - 1]))
970 line[--line_len] = '\0';
971
972 c = strchr(line, '\n');
973 if (c)
974 *c = 0;
975
976 line_ip = -1;
a31b7cc0 977 parsed_line = line;
78f7defe 978
e592488c
AK
979 /* /filename:linenr ? Save line number and ignore. */
980 if (regexec(&file_lineno, line, 2, match, 0) == 0) {
981 *line_nr = atoi(line + match[1].rm_so);
982 return 0;
983 }
984
78f7defe
ACM
985 /*
986 * Strip leading spaces:
987 */
988 tmp = line;
989 while (*tmp) {
990 if (*tmp != ' ')
991 break;
992 tmp++;
993 }
994
995 if (*tmp) {
996 /*
997 * Parse hexa addresses followed by ':'
998 */
999 line_ip = strtoull(tmp, &tmp2, 16);
1000 if (*tmp2 != ':' || tmp == tmp2 || tmp2[1] == '\0')
1001 line_ip = -1;
1002 }
1003
1004 if (line_ip != -1) {
1005 u64 start = map__rip_2objdump(map, sym->start),
1006 end = map__rip_2objdump(map, sym->end);
1007
1008 offset = line_ip - start;
2c241bd3 1009 if ((u64)line_ip < start || (u64)line_ip >= end)
78f7defe 1010 offset = -1;
058b4cc9
ACM
1011 else
1012 parsed_line = tmp2 + 1;
a31b7cc0 1013 }
78f7defe 1014
e592488c 1015 dl = disasm_line__new(offset, parsed_line, privsize, *line_nr);
058b4cc9 1016 free(line);
e592488c 1017 (*line_nr)++;
058b4cc9 1018
29ed6e76 1019 if (dl == NULL)
78f7defe 1020 return -1;
058b4cc9 1021
bbb7f846
AH
1022 if (dl->ops.target.offset == UINT64_MAX)
1023 dl->ops.target.offset = dl->ops.target.addr -
1024 map__rip_2objdump(map, sym->start);
1025
6e427ab0 1026 /* kcore has no symbols, so add the call target name */
b178170a 1027 if (dl->ins && ins__is_call(dl->ins) && !dl->ops.target.name) {
6e427ab0
AH
1028 struct addr_map_symbol target = {
1029 .map = map,
1030 .addr = dl->ops.target.addr,
1031 };
1032
1033 if (!map_groups__find_ams(&target, NULL) &&
1034 target.sym->start == target.al_addr)
1035 dl->ops.target.name = strdup(target.sym->name);
b178170a
AH
1036 }
1037
29ed6e76 1038 disasm__add(&notes->src->source, dl);
78f7defe
ACM
1039
1040 return 0;
1041}
1042
e592488c
AK
1043static __attribute__((constructor)) void symbol__init_regexpr(void)
1044{
1045 regcomp(&file_lineno, "^/[^:]+:([0-9]+)", REG_EXTENDED);
1046}
1047
484a5e74
AH
1048static void delete_last_nop(struct symbol *sym)
1049{
1050 struct annotation *notes = symbol__annotation(sym);
1051 struct list_head *list = &notes->src->source;
1052 struct disasm_line *dl;
1053
1054 while (!list_empty(list)) {
1055 dl = list_entry(list->prev, struct disasm_line, node);
1056
1057 if (dl->ins && dl->ins->ops) {
1058 if (dl->ins->ops != &nop_ops)
1059 return;
1060 } else {
1061 if (!strstr(dl->line, " nop ") &&
1062 !strstr(dl->line, " nopl ") &&
1063 !strstr(dl->line, " nopw "))
1064 return;
1065 }
1066
1067 list_del(&dl->node);
1068 disasm_line__free(dl);
1069 }
1070}
1071
ce6f4fab 1072int symbol__annotate(struct symbol *sym, struct map *map, size_t privsize)
78f7defe
ACM
1073{
1074 struct dso *dso = map->dso;
1075 char *filename = dso__build_id_filename(dso, NULL, 0);
1076 bool free_filename = true;
1077 char command[PATH_MAX * 2];
1078 FILE *file;
1079 int err = 0;
78f7defe 1080 char symfs_filename[PATH_MAX];
afba19d9
AH
1081 struct kcore_extract kce;
1082 bool delete_extract = false;
e592488c 1083 int lineno = 0;
78f7defe 1084
972f393b
ACM
1085 if (filename)
1086 symbol__join_symfs(symfs_filename, filename);
78f7defe
ACM
1087
1088 if (filename == NULL) {
1089 if (dso->has_build_id) {
1090 pr_err("Can't annotate %s: not enough memory\n",
1091 sym->name);
1092 return -ENOMEM;
1093 }
1094 goto fallback;
ee205503
AH
1095 } else if (dso__is_kcore(dso)) {
1096 goto fallback;
78f7defe
ACM
1097 } else if (readlink(symfs_filename, command, sizeof(command)) < 0 ||
1098 strstr(command, "[kernel.kallsyms]") ||
1099 access(symfs_filename, R_OK)) {
1100 free(filename);
1101fallback:
1102 /*
1103 * If we don't have build-ids or the build-id file isn't in the
1104 * cache, or is just a kallsyms file, well, lets hope that this
1105 * DSO is the same as when 'perf record' ran.
1106 */
bf4414ae 1107 filename = (char *)dso->long_name;
972f393b 1108 symbol__join_symfs(symfs_filename, filename);
78f7defe
ACM
1109 free_filename = false;
1110 }
1111
bbb7f846
AH
1112 if (dso->symtab_type == DSO_BINARY_TYPE__KALLSYMS &&
1113 !dso__is_kcore(dso)) {
170ae6bc
ACM
1114 char bf[BUILD_ID_SIZE * 2 + 16] = " with build id ";
1115 char *build_id_msg = NULL;
1116
78f7defe
ACM
1117 if (dso->annotate_warned)
1118 goto out_free_filename;
170ae6bc
ACM
1119
1120 if (dso->has_build_id) {
1121 build_id__sprintf(dso->build_id,
1122 sizeof(dso->build_id), bf + 15);
1123 build_id_msg = bf;
1124 }
78f7defe
ACM
1125 err = -ENOENT;
1126 dso->annotate_warned = 1;
ae55795e
ACM
1127 pr_err("Can't annotate %s:\n\n"
1128 "No vmlinux file%s\nwas found in the path.\n\n"
1129 "Please use:\n\n"
e3a34029 1130 " perf buildid-cache -vu vmlinux\n\n"
ae55795e 1131 "or:\n\n"
ff2a6617 1132 " --vmlinux vmlinux\n",
170ae6bc 1133 sym->name, build_id_msg ?: "");
78f7defe
ACM
1134 goto out_free_filename;
1135 }
1136
1137 pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__,
1138 filename, sym->name, map->unmap_ip(map, sym->start),
1139 map->unmap_ip(map, sym->end));
1140
78f7defe
ACM
1141 pr_debug("annotating [%p] %30s : [%p] %30s\n",
1142 dso, dso->long_name, sym, sym->name);
1143
afba19d9
AH
1144 if (dso__is_kcore(dso)) {
1145 kce.kcore_filename = symfs_filename;
1146 kce.addr = map__rip_2objdump(map, sym->start);
1147 kce.offs = sym->start;
2c241bd3 1148 kce.len = sym->end - sym->start;
afba19d9
AH
1149 if (!kcore_extract__create(&kce)) {
1150 delete_extract = true;
1151 strlcpy(symfs_filename, kce.extract_filename,
1152 sizeof(symfs_filename));
1153 if (free_filename) {
1154 free(filename);
1155 free_filename = false;
1156 }
1157 filename = symfs_filename;
1158 }
2c7da8c5
JO
1159 } else if (dso__needs_decompress(dso)) {
1160 char tmp[PATH_MAX];
1161 struct kmod_path m;
1162 int fd;
1163 bool ret;
1164
1165 if (kmod_path__parse_ext(&m, symfs_filename))
1166 goto out_free_filename;
1167
1168 snprintf(tmp, PATH_MAX, "/tmp/perf-kmod-XXXXXX");
1169
1170 fd = mkstemp(tmp);
1171 if (fd < 0) {
1172 free(m.ext);
1173 goto out_free_filename;
1174 }
1175
1176 ret = decompress_to_file(m.ext, symfs_filename, fd);
1177
1178 free(m.ext);
1179 close(fd);
1180
1181 if (!ret)
1182 goto out_free_filename;
1183
1184 strcpy(symfs_filename, tmp);
afba19d9
AH
1185 }
1186
78f7defe 1187 snprintf(command, sizeof(command),
7a4ec938 1188 "%s %s%s --start-address=0x%016" PRIx64
3e6a2a7f 1189 " --stop-address=0x%016" PRIx64
e592488c 1190 " -l -d %s %s -C %s 2>/dev/null|grep -v %s|expand",
7a4ec938 1191 objdump_path ? objdump_path : "objdump",
f69b64f7
AK
1192 disassembler_style ? "-M " : "",
1193 disassembler_style ? disassembler_style : "",
78f7defe 1194 map__rip_2objdump(map, sym->start),
2c241bd3 1195 map__rip_2objdump(map, sym->end),
3e6a2a7f
SE
1196 symbol_conf.annotate_asm_raw ? "" : "--no-show-raw",
1197 symbol_conf.annotate_src ? "-S" : "",
78f7defe
ACM
1198 symfs_filename, filename);
1199
1200 pr_debug("Executing: %s\n", command);
1201
1202 file = popen(command, "r");
1203 if (!file)
2c7da8c5 1204 goto out_remove_tmp;
78f7defe
ACM
1205
1206 while (!feof(file))
e592488c
AK
1207 if (symbol__parse_objdump_line(sym, map, file, privsize,
1208 &lineno) < 0)
78f7defe
ACM
1209 break;
1210
484a5e74
AH
1211 /*
1212 * kallsyms does not have symbol sizes so there may a nop at the end.
1213 * Remove it.
1214 */
1215 if (dso__is_kcore(dso))
1216 delete_last_nop(sym);
1217
78f7defe 1218 pclose(file);
2c7da8c5
JO
1219
1220out_remove_tmp:
1221 if (dso__needs_decompress(dso))
1222 unlink(symfs_filename);
78f7defe 1223out_free_filename:
afba19d9
AH
1224 if (delete_extract)
1225 kcore_extract__delete(&kce);
78f7defe
ACM
1226 if (free_filename)
1227 free(filename);
1228 return err;
1229}
1230
1231static void insert_source_line(struct rb_root *root, struct source_line *src_line)
1232{
1233 struct source_line *iter;
1234 struct rb_node **p = &root->rb_node;
1235 struct rb_node *parent = NULL;
1491c22a 1236 int i, ret;
78f7defe
ACM
1237
1238 while (*p != NULL) {
1239 parent = *p;
1240 iter = rb_entry(parent, struct source_line, node);
1241
41127965
NK
1242 ret = strcmp(iter->path, src_line->path);
1243 if (ret == 0) {
1491c22a 1244 for (i = 0; i < src_line->nr_pcnt; i++)
276af92f 1245 iter->samples[i].percent_sum += src_line->samples[i].percent;
41127965
NK
1246 return;
1247 }
1248
1249 if (ret < 0)
1250 p = &(*p)->rb_left;
1251 else
1252 p = &(*p)->rb_right;
1253 }
1254
1491c22a 1255 for (i = 0; i < src_line->nr_pcnt; i++)
276af92f 1256 src_line->samples[i].percent_sum = src_line->samples[i].percent;
41127965
NK
1257
1258 rb_link_node(&src_line->node, parent, p);
1259 rb_insert_color(&src_line->node, root);
1260}
1261
1491c22a
NK
1262static int cmp_source_line(struct source_line *a, struct source_line *b)
1263{
1264 int i;
1265
1266 for (i = 0; i < a->nr_pcnt; i++) {
276af92f 1267 if (a->samples[i].percent_sum == b->samples[i].percent_sum)
1491c22a 1268 continue;
276af92f 1269 return a->samples[i].percent_sum > b->samples[i].percent_sum;
1491c22a
NK
1270 }
1271
1272 return 0;
1273}
1274
41127965
NK
1275static void __resort_source_line(struct rb_root *root, struct source_line *src_line)
1276{
1277 struct source_line *iter;
1278 struct rb_node **p = &root->rb_node;
1279 struct rb_node *parent = NULL;
1280
1281 while (*p != NULL) {
1282 parent = *p;
1283 iter = rb_entry(parent, struct source_line, node);
1284
1491c22a 1285 if (cmp_source_line(src_line, iter))
78f7defe
ACM
1286 p = &(*p)->rb_left;
1287 else
1288 p = &(*p)->rb_right;
1289 }
1290
1291 rb_link_node(&src_line->node, parent, p);
1292 rb_insert_color(&src_line->node, root);
1293}
1294
41127965
NK
1295static void resort_source_line(struct rb_root *dest_root, struct rb_root *src_root)
1296{
1297 struct source_line *src_line;
1298 struct rb_node *node;
1299
1300 node = rb_first(src_root);
1301 while (node) {
1302 struct rb_node *next;
1303
1304 src_line = rb_entry(node, struct source_line, node);
1305 next = rb_next(node);
1306 rb_erase(node, src_root);
1307
1308 __resort_source_line(dest_root, src_line);
1309 node = next;
1310 }
1311}
1312
78f7defe
ACM
1313static void symbol__free_source_line(struct symbol *sym, int len)
1314{
1315 struct annotation *notes = symbol__annotation(sym);
ce6f4fab 1316 struct source_line *src_line = notes->src->lines;
1491c22a 1317 size_t sizeof_src_line;
78f7defe
ACM
1318 int i;
1319
1491c22a 1320 sizeof_src_line = sizeof(*src_line) +
276af92f 1321 (sizeof(src_line->samples) * (src_line->nr_pcnt - 1));
78f7defe 1322
1491c22a 1323 for (i = 0; i < len; i++) {
f048d548 1324 free_srcline(src_line->path);
1491c22a
NK
1325 src_line = (void *)src_line + sizeof_src_line;
1326 }
1327
04662523 1328 zfree(&notes->src->lines);
78f7defe
ACM
1329}
1330
1331/* Get the filename:line for the colored entries */
1332static int symbol__get_source_line(struct symbol *sym, struct map *map,
db8fd07a 1333 struct perf_evsel *evsel,
86c98cab 1334 struct rb_root *root, int len)
78f7defe
ACM
1335{
1336 u64 start;
1491c22a
NK
1337 int i, k;
1338 int evidx = evsel->idx;
78f7defe
ACM
1339 struct source_line *src_line;
1340 struct annotation *notes = symbol__annotation(sym);
1491c22a 1341 struct sym_hist *h = annotation__histogram(notes, evidx);
41127965 1342 struct rb_root tmp_root = RB_ROOT;
1491c22a
NK
1343 int nr_pcnt = 1;
1344 u64 h_sum = h->sum;
1345 size_t sizeof_src_line = sizeof(struct source_line);
1346
1347 if (perf_evsel__is_group_event(evsel)) {
1348 for (i = 1; i < evsel->nr_members; i++) {
1349 h = annotation__histogram(notes, evidx + i);
1350 h_sum += h->sum;
1351 }
1352 nr_pcnt = evsel->nr_members;
276af92f 1353 sizeof_src_line += (nr_pcnt - 1) * sizeof(src_line->samples);
1491c22a 1354 }
78f7defe 1355
1491c22a 1356 if (!h_sum)
78f7defe
ACM
1357 return 0;
1358
1491c22a 1359 src_line = notes->src->lines = calloc(len, sizeof_src_line);
ce6f4fab 1360 if (!notes->src->lines)
78f7defe
ACM
1361 return -1;
1362
f40a0633 1363 start = map__rip_2objdump(map, sym->start);
78f7defe
ACM
1364
1365 for (i = 0; i < len; i++) {
78f7defe 1366 u64 offset;
1491c22a 1367 double percent_max = 0.0;
78f7defe 1368
1491c22a
NK
1369 src_line->nr_pcnt = nr_pcnt;
1370
1371 for (k = 0; k < nr_pcnt; k++) {
1372 h = annotation__histogram(notes, evidx + k);
276af92f 1373 src_line->samples[k].percent = 100.0 * h->addr[i] / h->sum;
1491c22a 1374
276af92f
ACM
1375 if (src_line->samples[k].percent > percent_max)
1376 percent_max = src_line->samples[k].percent;
1491c22a
NK
1377 }
1378
1379 if (percent_max <= 0.5)
1380 goto next;
78f7defe
ACM
1381
1382 offset = start + i;
85c116a6 1383 src_line->path = get_srcline(map->dso, offset, NULL, false);
1491c22a 1384 insert_source_line(&tmp_root, src_line);
78f7defe 1385
1491c22a
NK
1386 next:
1387 src_line = (void *)src_line + sizeof_src_line;
78f7defe
ACM
1388 }
1389
41127965 1390 resort_source_line(root, &tmp_root);
78f7defe
ACM
1391 return 0;
1392}
1393
1394static void print_summary(struct rb_root *root, const char *filename)
1395{
1396 struct source_line *src_line;
1397 struct rb_node *node;
1398
1399 printf("\nSorted summary for file %s\n", filename);
1400 printf("----------------------------------------------\n\n");
1401
1402 if (RB_EMPTY_ROOT(root)) {
1403 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
1404 return;
1405 }
1406
1407 node = rb_first(root);
1408 while (node) {
1491c22a 1409 double percent, percent_max = 0.0;
78f7defe
ACM
1410 const char *color;
1411 char *path;
1491c22a 1412 int i;
78f7defe
ACM
1413
1414 src_line = rb_entry(node, struct source_line, node);
1491c22a 1415 for (i = 0; i < src_line->nr_pcnt; i++) {
276af92f 1416 percent = src_line->samples[i].percent_sum;
1491c22a
NK
1417 color = get_percent_color(percent);
1418 color_fprintf(stdout, color, " %7.2f", percent);
1419
1420 if (percent > percent_max)
1421 percent_max = percent;
1422 }
1423
78f7defe 1424 path = src_line->path;
1491c22a 1425 color = get_percent_color(percent_max);
f048d548 1426 color_fprintf(stdout, color, " %s\n", path);
78f7defe 1427
78f7defe
ACM
1428 node = rb_next(node);
1429 }
1430}
1431
db8fd07a 1432static void symbol__annotate_hits(struct symbol *sym, struct perf_evsel *evsel)
78f7defe
ACM
1433{
1434 struct annotation *notes = symbol__annotation(sym);
db8fd07a 1435 struct sym_hist *h = annotation__histogram(notes, evsel->idx);
1b2e2df4 1436 u64 len = symbol__size(sym), offset;
78f7defe
ACM
1437
1438 for (offset = 0; offset < len; ++offset)
1439 if (h->addr[offset] != 0)
1440 printf("%*" PRIx64 ": %" PRIu64 "\n", BITS_PER_LONG / 2,
1441 sym->start + offset, h->addr[offset]);
1442 printf("%*s: %" PRIu64 "\n", BITS_PER_LONG / 2, "h->sum", h->sum);
1443}
1444
db8fd07a
NK
1445int symbol__annotate_printf(struct symbol *sym, struct map *map,
1446 struct perf_evsel *evsel, bool full_paths,
1447 int min_pcnt, int max_lines, int context)
78f7defe
ACM
1448{
1449 struct dso *dso = map->dso;
bfd14b9a
DA
1450 char *filename;
1451 const char *d_filename;
9cdbadce 1452 const char *evsel_name = perf_evsel__name(evsel);
ce6f4fab 1453 struct annotation *notes = symbol__annotation(sym);
29ed6e76 1454 struct disasm_line *pos, *queue = NULL;
058b4cc9 1455 u64 start = map__rip_2objdump(map, sym->start);
d5e3d747 1456 int printed = 2, queue_len = 0;
36532461 1457 int more = 0;
78f7defe 1458 u64 len;
b1dd4432 1459 int width = 8;
9cdbadce 1460 int namelen, evsel_name_len, graph_dotted_len;
78f7defe 1461
bfd14b9a
DA
1462 filename = strdup(dso->long_name);
1463 if (!filename)
1464 return -ENOMEM;
1465
78f7defe
ACM
1466 if (full_paths)
1467 d_filename = filename;
1468 else
1469 d_filename = basename(filename);
1470
1b2e2df4 1471 len = symbol__size(sym);
b1dd4432 1472 namelen = strlen(d_filename);
9cdbadce 1473 evsel_name_len = strlen(evsel_name);
b1dd4432 1474
759ff497 1475 if (perf_evsel__is_group_event(evsel))
b1dd4432 1476 width *= evsel->nr_members;
78f7defe 1477
9cdbadce
ACM
1478 printf(" %-*.*s| Source code & Disassembly of %s for %s\n",
1479 width, width, "Percent", d_filename, evsel_name);
1480
1481 graph_dotted_len = width + namelen + evsel_name_len;
1482 printf("-%-*.*s-----------------------------------------\n",
1483 graph_dotted_len, graph_dotted_len, graph_dotted_line);
78f7defe
ACM
1484
1485 if (verbose)
db8fd07a 1486 symbol__annotate_hits(sym, evsel);
78f7defe 1487
ce6f4fab 1488 list_for_each_entry(pos, &notes->src->source, node) {
d5e3d747
ACM
1489 if (context && queue == NULL) {
1490 queue = pos;
1491 queue_len = 0;
1492 }
1493
db8fd07a 1494 switch (disasm_line__print(pos, sym, start, evsel, len,
058b4cc9
ACM
1495 min_pcnt, printed, max_lines,
1496 queue)) {
36532461
ACM
1497 case 0:
1498 ++printed;
d5e3d747
ACM
1499 if (context) {
1500 printed += queue_len;
1501 queue = NULL;
1502 queue_len = 0;
1503 }
36532461
ACM
1504 break;
1505 case 1:
1506 /* filtered by max_lines */
1507 ++more;
d040bd36 1508 break;
36532461
ACM
1509 case -1:
1510 default:
d5e3d747
ACM
1511 /*
1512 * Filtered by min_pcnt or non IP lines when
1513 * context != 0
1514 */
1515 if (!context)
1516 break;
1517 if (queue_len == context)
1518 queue = list_entry(queue->node.next, typeof(*queue), node);
1519 else
1520 ++queue_len;
36532461
ACM
1521 break;
1522 }
1523 }
1524
bfd14b9a
DA
1525 free(filename);
1526
36532461
ACM
1527 return more;
1528}
f1e2701d 1529
36532461
ACM
1530void symbol__annotate_zero_histogram(struct symbol *sym, int evidx)
1531{
1532 struct annotation *notes = symbol__annotation(sym);
1533 struct sym_hist *h = annotation__histogram(notes, evidx);
1534
ce6f4fab 1535 memset(h, 0, notes->src->sizeof_sym_hist);
36532461
ACM
1536}
1537
ce6f4fab 1538void symbol__annotate_decay_histogram(struct symbol *sym, int evidx)
36532461
ACM
1539{
1540 struct annotation *notes = symbol__annotation(sym);
1541 struct sym_hist *h = annotation__histogram(notes, evidx);
1b2e2df4 1542 int len = symbol__size(sym), offset;
36532461
ACM
1543
1544 h->sum = 0;
8b84a568
ACM
1545 for (offset = 0; offset < len; ++offset) {
1546 h->addr[offset] = h->addr[offset] * 7 / 8;
1547 h->sum += h->addr[offset];
f1e2701d
ACM
1548 }
1549}
1550
29ed6e76 1551void disasm__purge(struct list_head *head)
f1e2701d 1552{
29ed6e76 1553 struct disasm_line *pos, *n;
f1e2701d
ACM
1554
1555 list_for_each_entry_safe(pos, n, head, node) {
1556 list_del(&pos->node);
29ed6e76 1557 disasm_line__free(pos);
f1e2701d
ACM
1558 }
1559}
1560
5145418b
ACM
1561static size_t disasm_line__fprintf(struct disasm_line *dl, FILE *fp)
1562{
1563 size_t printed;
1564
1565 if (dl->offset == -1)
1566 return fprintf(fp, "%s\n", dl->line);
1567
1568 printed = fprintf(fp, "%#" PRIx64 " %s", dl->offset, dl->name);
1569
c7e6ead7 1570 if (dl->ops.raw[0] != '\0') {
5145418b 1571 printed += fprintf(fp, "%.*s %s\n", 6 - (int)printed, " ",
c7e6ead7 1572 dl->ops.raw);
5145418b
ACM
1573 }
1574
1575 return printed + fprintf(fp, "\n");
1576}
1577
1578size_t disasm__fprintf(struct list_head *head, FILE *fp)
1579{
1580 struct disasm_line *pos;
1581 size_t printed = 0;
1582
1583 list_for_each_entry(pos, head, node)
1584 printed += disasm_line__fprintf(pos, fp);
1585
1586 return printed;
1587}
1588
db8fd07a
NK
1589int symbol__tty_annotate(struct symbol *sym, struct map *map,
1590 struct perf_evsel *evsel, bool print_lines,
1591 bool full_paths, int min_pcnt, int max_lines)
f1e2701d
ACM
1592{
1593 struct dso *dso = map->dso;
f1e2701d 1594 struct rb_root source_line = RB_ROOT;
f1e2701d
ACM
1595 u64 len;
1596
ce6f4fab 1597 if (symbol__annotate(sym, map, 0) < 0)
f1e2701d
ACM
1598 return -1;
1599
1b2e2df4 1600 len = symbol__size(sym);
f1e2701d
ACM
1601
1602 if (print_lines) {
86c98cab
NK
1603 symbol__get_source_line(sym, map, evsel, &source_line, len);
1604 print_summary(&source_line, dso->long_name);
78f7defe
ACM
1605 }
1606
db8fd07a 1607 symbol__annotate_printf(sym, map, evsel, full_paths,
d5e3d747 1608 min_pcnt, max_lines, 0);
78f7defe
ACM
1609 if (print_lines)
1610 symbol__free_source_line(sym, len);
1611
29ed6e76 1612 disasm__purge(&symbol__annotation(sym)->src->source);
f1e2701d 1613
78f7defe
ACM
1614 return 0;
1615}
f626adff
ACM
1616
1617int hist_entry__annotate(struct hist_entry *he, size_t privsize)
1618{
1619 return symbol__annotate(he->ms.sym, he->ms.map, privsize);
1620}
48c65bda
NK
1621
1622bool ui__has_annotation(void)
1623{
1624 return use_browser == 1 && sort__has_sym;
1625}