]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - tools/perf/util/annotate.c
sched: Don't scan all-offline ->cpus_allowed twice if !CONFIG_CPUSETS
[karo-tx-linux.git] / tools / perf / util / annotate.c
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"
11 #include "ui/ui.h"
12 #include "sort.h"
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"
19 #include "evsel.h"
20 #include <regex.h>
21 #include <pthread.h>
22 #include <linux/bitops.h>
23
24 const char      *disassembler_style;
25 const char      *objdump_path;
26 static regex_t   file_lineno;
27
28 static struct ins *ins__find(const char *name);
29 static int disasm_line__parse(char *line, char **namep, char **rawp);
30
31 static void ins__delete(struct ins_operands *ops)
32 {
33         if (ops == NULL)
34                 return;
35         zfree(&ops->source.raw);
36         zfree(&ops->source.name);
37         zfree(&ops->target.raw);
38         zfree(&ops->target.name);
39 }
40
41 static 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
47 int 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
56 static int call__parse(struct ins_operands *ops)
57 {
58         char *endptr, *tok, *name;
59
60         ops->target.addr = strtoull(ops->raw, &endptr, 16);
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';
73         ops->target.name = strdup(name);
74         *tok = '>';
75
76         return ops->target.name == NULL ? -1 : 0;
77
78 indirect_call:
79         tok = strchr(endptr, '(');
80         if (tok != NULL) {
81                 ops->target.addr = 0;
82                 return 0;
83         }
84
85         tok = strchr(endptr, '*');
86         if (tok == NULL)
87                 return -1;
88
89         ops->target.addr = strtoull(tok + 1, NULL, 16);
90         return 0;
91 }
92
93 static int call__scnprintf(struct ins *ins, char *bf, size_t size,
94                            struct ins_operands *ops)
95 {
96         if (ops->target.name)
97                 return scnprintf(bf, size, "%-6.6s %s", ins->name, ops->target.name);
98
99         if (ops->target.addr == 0)
100                 return ins__raw_scnprintf(ins, bf, size, ops);
101
102         return scnprintf(bf, size, "%-6.6s *%" PRIx64, ins->name, ops->target.addr);
103 }
104
105 static struct ins_ops call_ops = {
106         .parse     = call__parse,
107         .scnprintf = call__scnprintf,
108 };
109
110 bool ins__is_call(const struct ins *ins)
111 {
112         return ins->ops == &call_ops;
113 }
114
115 static int jump__parse(struct ins_operands *ops)
116 {
117         const char *s = strchr(ops->raw, '+');
118
119         ops->target.addr = strtoull(ops->raw, NULL, 16);
120
121         if (s++ != NULL)
122                 ops->target.offset = strtoull(s, NULL, 16);
123         else
124                 ops->target.offset = UINT64_MAX;
125
126         return 0;
127 }
128
129 static int jump__scnprintf(struct ins *ins, char *bf, size_t size,
130                            struct ins_operands *ops)
131 {
132         return scnprintf(bf, size, "%-6.6s %" PRIx64, ins->name, ops->target.offset);
133 }
134
135 static struct ins_ops jump_ops = {
136         .parse     = jump__parse,
137         .scnprintf = jump__scnprintf,
138 };
139
140 bool ins__is_jump(const struct ins *ins)
141 {
142         return ins->ops == &jump_ops;
143 }
144
145 static 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
170 static 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
181         ops->locked.ins = ins__find(name);
182         free(name);
183
184         if (ops->locked.ins == NULL)
185                 goto out_free_ops;
186
187         if (!ops->locked.ins->ops)
188                 return 0;
189
190         if (ops->locked.ins->ops->parse &&
191             ops->locked.ins->ops->parse(ops->locked.ops) < 0)
192                 goto out_free_ops;
193
194         return 0;
195
196 out_free_ops:
197         zfree(&ops->locked.ops);
198         return 0;
199 }
200
201 static 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
214 static void lock__delete(struct ins_operands *ops)
215 {
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
223         zfree(&ops->locked.ops);
224         zfree(&ops->target.raw);
225         zfree(&ops->target.name);
226 }
227
228 static struct ins_ops lock_ops = {
229         .free      = lock__delete,
230         .parse     = lock__parse,
231         .scnprintf = lock__scnprintf,
232 };
233
234 static 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 = ',';
244
245         if (ops->source.raw == NULL)
246                 return -1;
247
248         target = ++s;
249         comment = strchr(s, '#');
250
251         if (comment != NULL)
252                 s = comment - 1;
253         else
254                 s = strchr(s, '\0') - 1;
255
256         while (s > target && isspace(s[0]))
257                 --s;
258         s++;
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
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
279 out_free_source:
280         zfree(&ops->source.raw);
281         return -1;
282 }
283
284 static 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
292 static struct ins_ops mov_ops = {
293         .parse     = mov__parse,
294         .scnprintf = mov__scnprintf,
295 };
296
297 static 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
326 static 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
333 static struct ins_ops dec_ops = {
334         .parse     = dec__parse,
335         .scnprintf = dec__scnprintf,
336 };
337
338 static int nop__scnprintf(struct ins *ins __maybe_unused, char *bf, size_t size,
339                           struct ins_operands *ops __maybe_unused)
340 {
341         return scnprintf(bf, size, "%-6.6s", "nop");
342 }
343
344 static struct ins_ops nop_ops = {
345         .scnprintf = nop__scnprintf,
346 };
347
348 /*
349  * Must be sorted by name!
350  */
351 static struct ins instructions[] = {
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, },
357         { .name = "bts",   .ops  = &mov_ops, },
358         { .name = "call",  .ops  = &call_ops, },
359         { .name = "callq", .ops  = &call_ops, },
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, },
366         { .name = "dec",   .ops  = &dec_ops, },
367         { .name = "decl",  .ops  = &dec_ops, },
368         { .name = "imul",  .ops  = &mov_ops, },
369         { .name = "inc",   .ops  = &dec_ops, },
370         { .name = "incl",  .ops  = &dec_ops, },
371         { .name = "ja",    .ops  = &jump_ops, },
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, },
377         { .name = "je",    .ops  = &jump_ops, },
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, },
383         { .name = "jmp",   .ops  = &jump_ops, },
384         { .name = "jmpq",  .ops  = &jump_ops, },
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, },
390         { .name = "jne",   .ops  = &jump_ops, },
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, },
404         { .name = "js",    .ops  = &jump_ops, },
405         { .name = "jz",    .ops  = &jump_ops, },
406         { .name = "lea",   .ops  = &mov_ops, },
407         { .name = "lock",  .ops  = &lock_ops, },
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, },
416         { .name = "nop",   .ops  = &nop_ops, },
417         { .name = "nopl",  .ops  = &nop_ops, },
418         { .name = "nopw",  .ops  = &nop_ops, },
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, },
424         { .name = "xadd",  .ops  = &mov_ops, },
425         { .name = "xbeginl", .ops  = &jump_ops, },
426         { .name = "xbeginq", .ops  = &jump_ops, },
427 };
428
429 static 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
436 static 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
443 int symbol__annotate_init(struct map *map __maybe_unused, struct symbol *sym)
444 {
445         struct annotation *notes = symbol__annotation(sym);
446         pthread_mutex_init(&notes->lock, NULL);
447         return 0;
448 }
449
450 int symbol__alloc_hist(struct symbol *sym)
451 {
452         struct annotation *notes = symbol__annotation(sym);
453         const size_t size = symbol__size(sym);
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;
466
467         notes->src = zalloc(sizeof(*notes->src) + symbol_conf.nr_events * sizeof_sym_hist);
468         if (notes->src == NULL)
469                 return -1;
470         notes->src->sizeof_sym_hist = sizeof_sym_hist;
471         notes->src->nr_histograms   = symbol_conf.nr_events;
472         INIT_LIST_HEAD(&notes->src->source);
473         return 0;
474 }
475
476 /* The cycles histogram is lazily allocated. */
477 static 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
488 void symbol__annotate_zero_histograms(struct symbol *sym)
489 {
490         struct annotation *notes = symbol__annotation(sym);
491
492         pthread_mutex_lock(&notes->lock);
493         if (notes->src != NULL) {
494                 memset(notes->src->histograms, 0,
495                        notes->src->nr_histograms * notes->src->sizeof_sym_hist);
496                 if (notes->src->cycles_hist)
497                         memset(notes->src->cycles_hist, 0,
498                                 symbol__size(sym) * sizeof(struct cyc_hist));
499         }
500         pthread_mutex_unlock(&notes->lock);
501 }
502
503 static 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
543 static int __symbol__inc_addr_samples(struct symbol *sym, struct map *map,
544                                       struct annotation *notes, int evidx, u64 addr)
545 {
546         unsigned offset;
547         struct sym_hist *h;
548
549         pr_debug3("%s: addr=%#" PRIx64 "\n", __func__, map->unmap_ip(map, addr));
550
551         if (addr < sym->start || addr >= sym->end)
552                 return -ERANGE;
553
554         offset = addr - sym->start;
555         h = annotation__histogram(notes, evidx);
556         h->sum++;
557         h->addr[offset]++;
558
559         pr_debug3("%#" PRIx64 " %s: period++ [addr: %#" PRIx64 ", %#" PRIx64
560                   ", evidx=%d] => %" PRIu64 "\n", sym->start, sym->name,
561                   addr, addr - sym->start, evidx, h->addr[offset]);
562         return 0;
563 }
564
565 static struct annotation *symbol__get_annotation(struct symbol *sym, bool cycles)
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         }
573         if (!notes->src->cycles_hist && cycles) {
574                 if (symbol__alloc_hist_cycles(sym) < 0)
575                         return NULL;
576         }
577         return notes;
578 }
579
580 static int symbol__inc_addr_samples(struct symbol *sym, struct map *map,
581                                     int evidx, u64 addr)
582 {
583         struct annotation *notes;
584
585         if (sym == NULL)
586                 return 0;
587         notes = symbol__get_annotation(sym, false);
588         if (notes == NULL)
589                 return -ENOMEM;
590         return __symbol__inc_addr_samples(sym, map, notes, evidx, addr);
591 }
592
593 static 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
620 int addr_map_symbol__account_cycles(struct addr_map_symbol *ams,
621                                     struct addr_map_symbol *start,
622                                     unsigned cycles)
623 {
624         u64 saddr = 0;
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)
643                 pr_debug2("BB with bad start: addr %"PRIx64" start %"PRIx64" sym %"PRIx64" saddr %"PRIx64"\n",
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
654 int 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
659 int 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
664 static 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
674         if (dl->ins->ops->parse && dl->ins->ops->parse(&dl->ops) < 0)
675                 dl->ins = NULL;
676 }
677
678 static 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
710 out_free_name:
711         zfree(namep);
712         return -1;
713 }
714
715 static struct disasm_line *disasm_line__new(s64 offset, char *line,
716                                         size_t privsize, int line_nr)
717 {
718         struct disasm_line *dl = zalloc(sizeof(*dl) + privsize);
719
720         if (dl != NULL) {
721                 dl->offset = offset;
722                 dl->line = strdup(line);
723                 dl->line_nr = line_nr;
724                 if (dl->line == NULL)
725                         goto out_delete;
726
727                 if (offset != -1) {
728                         if (disasm_line__parse(dl->line, &dl->name, &dl->ops.raw) < 0)
729                                 goto out_free_line;
730
731                         disasm_line__init_ins(dl);
732                 }
733         }
734
735         return dl;
736
737 out_free_line:
738         zfree(&dl->line);
739 out_delete:
740         free(dl);
741         return NULL;
742 }
743
744 void disasm_line__free(struct disasm_line *dl)
745 {
746         zfree(&dl->line);
747         zfree(&dl->name);
748         if (dl->ins && dl->ins->ops->free)
749                 dl->ins->ops->free(&dl->ops);
750         else
751                 ins__delete(&dl->ops);
752         free(dl);
753 }
754
755 int 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
763 static void disasm__add(struct list_head *head, struct disasm_line *line)
764 {
765         list_add_tail(&line->node, head);
766 }
767
768 struct disasm_line *disasm__get_next_ip_line(struct list_head *head, struct disasm_line *pos)
769 {
770         list_for_each_entry_continue(pos, head, node)
771                 if (pos->offset >= 0)
772                         return pos;
773
774         return NULL;
775 }
776
777 double disasm__calc_percent(struct annotation *notes, int evidx, s64 offset,
778                             s64 end, const char **path, u64 *nr_samples)
779 {
780         struct source_line *src_line = notes->src->lines;
781         double percent = 0.0;
782         *nr_samples = 0;
783
784         if (src_line) {
785                 size_t sizeof_src_line = sizeof(*src_line) +
786                                 sizeof(src_line->samples) * (src_line->nr_pcnt - 1);
787
788                 while (offset < end) {
789                         src_line = (void *)notes->src->lines +
790                                         (sizeof_src_line * offset);
791
792                         if (*path == NULL)
793                                 *path = src_line->path;
794
795                         percent += src_line->samples[evidx].percent;
796                         *nr_samples += src_line->samples[evidx].nr;
797                         offset++;
798                 }
799         } else {
800                 struct sym_hist *h = annotation__histogram(notes, evidx);
801                 unsigned int hits = 0;
802
803                 while (offset < end)
804                         hits += h->addr[offset++];
805
806                 if (h->sum) {
807                         *nr_samples = hits;
808                         percent = 100.0 * hits / h->sum;
809                 }
810         }
811
812         return percent;
813 }
814
815 static int disasm_line__print(struct disasm_line *dl, struct symbol *sym, u64 start,
816                       struct perf_evsel *evsel, u64 len, int min_pcnt, int printed,
817                       int max_lines, struct disasm_line *queue)
818 {
819         static const char *prev_line;
820         static const char *prev_color;
821
822         if (dl->offset != -1) {
823                 const char *path = NULL;
824                 u64 nr_samples;
825                 double percent, max_percent = 0.0;
826                 double *ppercents = &percent;
827                 u64 *psamples = &nr_samples;
828                 int i, nr_percent = 1;
829                 const char *color;
830                 struct annotation *notes = symbol__annotation(sym);
831                 s64 offset = dl->offset;
832                 const u64 addr = start + offset;
833                 struct disasm_line *next;
834
835                 next = disasm__get_next_ip_line(&notes->src->source, dl);
836
837                 if (perf_evsel__is_group_event(evsel)) {
838                         nr_percent = evsel->nr_members;
839                         ppercents = calloc(nr_percent, sizeof(double));
840                         psamples = calloc(nr_percent, sizeof(u64));
841                         if (ppercents == NULL || psamples == NULL) {
842                                 return -1;
843                         }
844                 }
845
846                 for (i = 0; i < nr_percent; i++) {
847                         percent = disasm__calc_percent(notes,
848                                         notes->src->lines ? i : evsel->idx + i,
849                                         offset,
850                                         next ? next->offset : (s64) len,
851                                         &path, &nr_samples);
852
853                         ppercents[i] = percent;
854                         psamples[i] = nr_samples;
855                         if (percent > max_percent)
856                                 max_percent = percent;
857                 }
858
859                 if (max_percent < min_pcnt)
860                         return -1;
861
862                 if (max_lines && printed >= max_lines)
863                         return 1;
864
865                 if (queue != NULL) {
866                         list_for_each_entry_from(queue, &notes->src->source, node) {
867                                 if (queue == dl)
868                                         break;
869                                 disasm_line__print(queue, sym, start, evsel, len,
870                                                     0, 0, 1, NULL);
871                         }
872                 }
873
874                 color = get_percent_color(max_percent);
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
890                 for (i = 0; i < nr_percent; i++) {
891                         percent = ppercents[i];
892                         nr_samples = psamples[i];
893                         color = get_percent_color(percent);
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);
900                 }
901
902                 printf(" :      ");
903                 color_fprintf(stdout, PERF_COLOR_MAGENTA, "  %" PRIx64 ":", addr);
904                 color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", dl->line);
905
906                 if (ppercents != &percent)
907                         free(ppercents);
908
909                 if (psamples != &nr_samples)
910                         free(psamples);
911
912         } else if (max_lines && printed >= max_lines)
913                 return 1;
914         else {
915                 int width = 8;
916
917                 if (queue)
918                         return -1;
919
920                 if (perf_evsel__is_group_event(evsel))
921                         width *= evsel->nr_members;
922
923                 if (!*dl->line)
924                         printf(" %*s:\n", width, " ");
925                 else
926                         printf(" %*s:   %s\n", width, " ", dl->line);
927         }
928
929         return 0;
930 }
931
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  */
952 static int symbol__parse_objdump_line(struct symbol *sym, struct map *map,
953                                       FILE *file, size_t privsize,
954                                       int *line_nr)
955 {
956         struct annotation *notes = symbol__annotation(sym);
957         struct disasm_line *dl;
958         char *line = NULL, *parsed_line, *tmp, *tmp2, *c;
959         size_t line_len;
960         s64 line_ip, offset = -1;
961         regmatch_t match[2];
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;
977         parsed_line = line;
978
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
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;
1009                 if ((u64)line_ip < start || (u64)line_ip >= end)
1010                         offset = -1;
1011                 else
1012                         parsed_line = tmp2 + 1;
1013         }
1014
1015         dl = disasm_line__new(offset, parsed_line, privsize, *line_nr);
1016         free(line);
1017         (*line_nr)++;
1018
1019         if (dl == NULL)
1020                 return -1;
1021
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
1026         /* kcore has no symbols, so add the call target name */
1027         if (dl->ins && ins__is_call(dl->ins) && !dl->ops.target.name) {
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);
1036         }
1037
1038         disasm__add(&notes->src->source, dl);
1039
1040         return 0;
1041 }
1042
1043 static __attribute__((constructor)) void symbol__init_regexpr(void)
1044 {
1045         regcomp(&file_lineno, "^/[^:]+:([0-9]+)", REG_EXTENDED);
1046 }
1047
1048 static 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
1072 int symbol__annotate(struct symbol *sym, struct map *map, size_t privsize)
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;
1080         char symfs_filename[PATH_MAX];
1081         struct kcore_extract kce;
1082         bool delete_extract = false;
1083         int lineno = 0;
1084
1085         if (filename)
1086                 symbol__join_symfs(symfs_filename, filename);
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;
1095         } else if (dso__is_kcore(dso)) {
1096                 goto fallback;
1097         } else if (readlink(symfs_filename, command, sizeof(command)) < 0 ||
1098                    strstr(command, "[kernel.kallsyms]") ||
1099                    access(symfs_filename, R_OK)) {
1100                 free(filename);
1101 fallback:
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                  */
1107                 filename = (char *)dso->long_name;
1108                 symbol__join_symfs(symfs_filename, filename);
1109                 free_filename = false;
1110         }
1111
1112         if (dso->symtab_type == DSO_BINARY_TYPE__KALLSYMS &&
1113             !dso__is_kcore(dso)) {
1114                 char bf[BUILD_ID_SIZE * 2 + 16] = " with build id ";
1115                 char *build_id_msg = NULL;
1116
1117                 if (dso->annotate_warned)
1118                         goto out_free_filename;
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                 }
1125                 err = -ENOENT;
1126                 dso->annotate_warned = 1;
1127                 pr_err("Can't annotate %s:\n\n"
1128                        "No vmlinux file%s\nwas found in the path.\n\n"
1129                        "Note that annotation using /proc/kcore requires CAP_SYS_RAWIO capability.\n\n"
1130                        "Please use:\n\n"
1131                        "  perf buildid-cache -vu vmlinux\n\n"
1132                        "or:\n\n"
1133                        "  --vmlinux vmlinux\n",
1134                        sym->name, build_id_msg ?: "");
1135                 goto out_free_filename;
1136         }
1137
1138         pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__,
1139                  filename, sym->name, map->unmap_ip(map, sym->start),
1140                  map->unmap_ip(map, sym->end));
1141
1142         pr_debug("annotating [%p] %30s : [%p] %30s\n",
1143                  dso, dso->long_name, sym, sym->name);
1144
1145         if (dso__is_kcore(dso)) {
1146                 kce.kcore_filename = symfs_filename;
1147                 kce.addr = map__rip_2objdump(map, sym->start);
1148                 kce.offs = sym->start;
1149                 kce.len = sym->end - sym->start;
1150                 if (!kcore_extract__create(&kce)) {
1151                         delete_extract = true;
1152                         strlcpy(symfs_filename, kce.extract_filename,
1153                                 sizeof(symfs_filename));
1154                         if (free_filename) {
1155                                 free(filename);
1156                                 free_filename = false;
1157                         }
1158                         filename = symfs_filename;
1159                 }
1160         } else if (dso__needs_decompress(dso)) {
1161                 char tmp[PATH_MAX];
1162                 struct kmod_path m;
1163                 int fd;
1164                 bool ret;
1165
1166                 if (kmod_path__parse_ext(&m, symfs_filename))
1167                         goto out_free_filename;
1168
1169                 snprintf(tmp, PATH_MAX, "/tmp/perf-kmod-XXXXXX");
1170
1171                 fd = mkstemp(tmp);
1172                 if (fd < 0) {
1173                         free(m.ext);
1174                         goto out_free_filename;
1175                 }
1176
1177                 ret = decompress_to_file(m.ext, symfs_filename, fd);
1178
1179                 free(m.ext);
1180                 close(fd);
1181
1182                 if (!ret)
1183                         goto out_free_filename;
1184
1185                 strcpy(symfs_filename, tmp);
1186         }
1187
1188         snprintf(command, sizeof(command),
1189                  "%s %s%s --start-address=0x%016" PRIx64
1190                  " --stop-address=0x%016" PRIx64
1191                  " -l -d %s %s -C %s 2>/dev/null|grep -v %s|expand",
1192                  objdump_path ? objdump_path : "objdump",
1193                  disassembler_style ? "-M " : "",
1194                  disassembler_style ? disassembler_style : "",
1195                  map__rip_2objdump(map, sym->start),
1196                  map__rip_2objdump(map, sym->end),
1197                  symbol_conf.annotate_asm_raw ? "" : "--no-show-raw",
1198                  symbol_conf.annotate_src ? "-S" : "",
1199                  symfs_filename, filename);
1200
1201         pr_debug("Executing: %s\n", command);
1202
1203         file = popen(command, "r");
1204         if (!file)
1205                 goto out_remove_tmp;
1206
1207         while (!feof(file))
1208                 if (symbol__parse_objdump_line(sym, map, file, privsize,
1209                             &lineno) < 0)
1210                         break;
1211
1212         /*
1213          * kallsyms does not have symbol sizes so there may a nop at the end.
1214          * Remove it.
1215          */
1216         if (dso__is_kcore(dso))
1217                 delete_last_nop(sym);
1218
1219         pclose(file);
1220
1221 out_remove_tmp:
1222         if (dso__needs_decompress(dso))
1223                 unlink(symfs_filename);
1224 out_free_filename:
1225         if (delete_extract)
1226                 kcore_extract__delete(&kce);
1227         if (free_filename)
1228                 free(filename);
1229         return err;
1230 }
1231
1232 static void insert_source_line(struct rb_root *root, struct source_line *src_line)
1233 {
1234         struct source_line *iter;
1235         struct rb_node **p = &root->rb_node;
1236         struct rb_node *parent = NULL;
1237         int i, ret;
1238
1239         while (*p != NULL) {
1240                 parent = *p;
1241                 iter = rb_entry(parent, struct source_line, node);
1242
1243                 ret = strcmp(iter->path, src_line->path);
1244                 if (ret == 0) {
1245                         for (i = 0; i < src_line->nr_pcnt; i++)
1246                                 iter->samples[i].percent_sum += src_line->samples[i].percent;
1247                         return;
1248                 }
1249
1250                 if (ret < 0)
1251                         p = &(*p)->rb_left;
1252                 else
1253                         p = &(*p)->rb_right;
1254         }
1255
1256         for (i = 0; i < src_line->nr_pcnt; i++)
1257                 src_line->samples[i].percent_sum = src_line->samples[i].percent;
1258
1259         rb_link_node(&src_line->node, parent, p);
1260         rb_insert_color(&src_line->node, root);
1261 }
1262
1263 static int cmp_source_line(struct source_line *a, struct source_line *b)
1264 {
1265         int i;
1266
1267         for (i = 0; i < a->nr_pcnt; i++) {
1268                 if (a->samples[i].percent_sum == b->samples[i].percent_sum)
1269                         continue;
1270                 return a->samples[i].percent_sum > b->samples[i].percent_sum;
1271         }
1272
1273         return 0;
1274 }
1275
1276 static void __resort_source_line(struct rb_root *root, struct source_line *src_line)
1277 {
1278         struct source_line *iter;
1279         struct rb_node **p = &root->rb_node;
1280         struct rb_node *parent = NULL;
1281
1282         while (*p != NULL) {
1283                 parent = *p;
1284                 iter = rb_entry(parent, struct source_line, node);
1285
1286                 if (cmp_source_line(src_line, iter))
1287                         p = &(*p)->rb_left;
1288                 else
1289                         p = &(*p)->rb_right;
1290         }
1291
1292         rb_link_node(&src_line->node, parent, p);
1293         rb_insert_color(&src_line->node, root);
1294 }
1295
1296 static void resort_source_line(struct rb_root *dest_root, struct rb_root *src_root)
1297 {
1298         struct source_line *src_line;
1299         struct rb_node *node;
1300
1301         node = rb_first(src_root);
1302         while (node) {
1303                 struct rb_node *next;
1304
1305                 src_line = rb_entry(node, struct source_line, node);
1306                 next = rb_next(node);
1307                 rb_erase(node, src_root);
1308
1309                 __resort_source_line(dest_root, src_line);
1310                 node = next;
1311         }
1312 }
1313
1314 static void symbol__free_source_line(struct symbol *sym, int len)
1315 {
1316         struct annotation *notes = symbol__annotation(sym);
1317         struct source_line *src_line = notes->src->lines;
1318         size_t sizeof_src_line;
1319         int i;
1320
1321         sizeof_src_line = sizeof(*src_line) +
1322                           (sizeof(src_line->samples) * (src_line->nr_pcnt - 1));
1323
1324         for (i = 0; i < len; i++) {
1325                 free_srcline(src_line->path);
1326                 src_line = (void *)src_line + sizeof_src_line;
1327         }
1328
1329         zfree(&notes->src->lines);
1330 }
1331
1332 /* Get the filename:line for the colored entries */
1333 static int symbol__get_source_line(struct symbol *sym, struct map *map,
1334                                    struct perf_evsel *evsel,
1335                                    struct rb_root *root, int len)
1336 {
1337         u64 start;
1338         int i, k;
1339         int evidx = evsel->idx;
1340         struct source_line *src_line;
1341         struct annotation *notes = symbol__annotation(sym);
1342         struct sym_hist *h = annotation__histogram(notes, evidx);
1343         struct rb_root tmp_root = RB_ROOT;
1344         int nr_pcnt = 1;
1345         u64 h_sum = h->sum;
1346         size_t sizeof_src_line = sizeof(struct source_line);
1347
1348         if (perf_evsel__is_group_event(evsel)) {
1349                 for (i = 1; i < evsel->nr_members; i++) {
1350                         h = annotation__histogram(notes, evidx + i);
1351                         h_sum += h->sum;
1352                 }
1353                 nr_pcnt = evsel->nr_members;
1354                 sizeof_src_line += (nr_pcnt - 1) * sizeof(src_line->samples);
1355         }
1356
1357         if (!h_sum)
1358                 return 0;
1359
1360         src_line = notes->src->lines = calloc(len, sizeof_src_line);
1361         if (!notes->src->lines)
1362                 return -1;
1363
1364         start = map__rip_2objdump(map, sym->start);
1365
1366         for (i = 0; i < len; i++) {
1367                 u64 offset;
1368                 double percent_max = 0.0;
1369
1370                 src_line->nr_pcnt = nr_pcnt;
1371
1372                 for (k = 0; k < nr_pcnt; k++) {
1373                         h = annotation__histogram(notes, evidx + k);
1374                         src_line->samples[k].percent = 100.0 * h->addr[i] / h->sum;
1375
1376                         if (src_line->samples[k].percent > percent_max)
1377                                 percent_max = src_line->samples[k].percent;
1378                 }
1379
1380                 if (percent_max <= 0.5)
1381                         goto next;
1382
1383                 offset = start + i;
1384                 src_line->path = get_srcline(map->dso, offset, NULL, false);
1385                 insert_source_line(&tmp_root, src_line);
1386
1387         next:
1388                 src_line = (void *)src_line + sizeof_src_line;
1389         }
1390
1391         resort_source_line(root, &tmp_root);
1392         return 0;
1393 }
1394
1395 static void print_summary(struct rb_root *root, const char *filename)
1396 {
1397         struct source_line *src_line;
1398         struct rb_node *node;
1399
1400         printf("\nSorted summary for file %s\n", filename);
1401         printf("----------------------------------------------\n\n");
1402
1403         if (RB_EMPTY_ROOT(root)) {
1404                 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
1405                 return;
1406         }
1407
1408         node = rb_first(root);
1409         while (node) {
1410                 double percent, percent_max = 0.0;
1411                 const char *color;
1412                 char *path;
1413                 int i;
1414
1415                 src_line = rb_entry(node, struct source_line, node);
1416                 for (i = 0; i < src_line->nr_pcnt; i++) {
1417                         percent = src_line->samples[i].percent_sum;
1418                         color = get_percent_color(percent);
1419                         color_fprintf(stdout, color, " %7.2f", percent);
1420
1421                         if (percent > percent_max)
1422                                 percent_max = percent;
1423                 }
1424
1425                 path = src_line->path;
1426                 color = get_percent_color(percent_max);
1427                 color_fprintf(stdout, color, " %s\n", path);
1428
1429                 node = rb_next(node);
1430         }
1431 }
1432
1433 static void symbol__annotate_hits(struct symbol *sym, struct perf_evsel *evsel)
1434 {
1435         struct annotation *notes = symbol__annotation(sym);
1436         struct sym_hist *h = annotation__histogram(notes, evsel->idx);
1437         u64 len = symbol__size(sym), offset;
1438
1439         for (offset = 0; offset < len; ++offset)
1440                 if (h->addr[offset] != 0)
1441                         printf("%*" PRIx64 ": %" PRIu64 "\n", BITS_PER_LONG / 2,
1442                                sym->start + offset, h->addr[offset]);
1443         printf("%*s: %" PRIu64 "\n", BITS_PER_LONG / 2, "h->sum", h->sum);
1444 }
1445
1446 int symbol__annotate_printf(struct symbol *sym, struct map *map,
1447                             struct perf_evsel *evsel, bool full_paths,
1448                             int min_pcnt, int max_lines, int context)
1449 {
1450         struct dso *dso = map->dso;
1451         char *filename;
1452         const char *d_filename;
1453         const char *evsel_name = perf_evsel__name(evsel);
1454         struct annotation *notes = symbol__annotation(sym);
1455         struct disasm_line *pos, *queue = NULL;
1456         u64 start = map__rip_2objdump(map, sym->start);
1457         int printed = 2, queue_len = 0;
1458         int more = 0;
1459         u64 len;
1460         int width = 8;
1461         int namelen, evsel_name_len, graph_dotted_len;
1462
1463         filename = strdup(dso->long_name);
1464         if (!filename)
1465                 return -ENOMEM;
1466
1467         if (full_paths)
1468                 d_filename = filename;
1469         else
1470                 d_filename = basename(filename);
1471
1472         len = symbol__size(sym);
1473         namelen = strlen(d_filename);
1474         evsel_name_len = strlen(evsel_name);
1475
1476         if (perf_evsel__is_group_event(evsel))
1477                 width *= evsel->nr_members;
1478
1479         printf(" %-*.*s|        Source code & Disassembly of %s for %s\n",
1480                width, width, "Percent", d_filename, evsel_name);
1481
1482         graph_dotted_len = width + namelen + evsel_name_len;
1483         printf("-%-*.*s-----------------------------------------\n",
1484                graph_dotted_len, graph_dotted_len, graph_dotted_line);
1485
1486         if (verbose)
1487                 symbol__annotate_hits(sym, evsel);
1488
1489         list_for_each_entry(pos, &notes->src->source, node) {
1490                 if (context && queue == NULL) {
1491                         queue = pos;
1492                         queue_len = 0;
1493                 }
1494
1495                 switch (disasm_line__print(pos, sym, start, evsel, len,
1496                                             min_pcnt, printed, max_lines,
1497                                             queue)) {
1498                 case 0:
1499                         ++printed;
1500                         if (context) {
1501                                 printed += queue_len;
1502                                 queue = NULL;
1503                                 queue_len = 0;
1504                         }
1505                         break;
1506                 case 1:
1507                         /* filtered by max_lines */
1508                         ++more;
1509                         break;
1510                 case -1:
1511                 default:
1512                         /*
1513                          * Filtered by min_pcnt or non IP lines when
1514                          * context != 0
1515                          */
1516                         if (!context)
1517                                 break;
1518                         if (queue_len == context)
1519                                 queue = list_entry(queue->node.next, typeof(*queue), node);
1520                         else
1521                                 ++queue_len;
1522                         break;
1523                 }
1524         }
1525
1526         free(filename);
1527
1528         return more;
1529 }
1530
1531 void symbol__annotate_zero_histogram(struct symbol *sym, int evidx)
1532 {
1533         struct annotation *notes = symbol__annotation(sym);
1534         struct sym_hist *h = annotation__histogram(notes, evidx);
1535
1536         memset(h, 0, notes->src->sizeof_sym_hist);
1537 }
1538
1539 void symbol__annotate_decay_histogram(struct symbol *sym, int evidx)
1540 {
1541         struct annotation *notes = symbol__annotation(sym);
1542         struct sym_hist *h = annotation__histogram(notes, evidx);
1543         int len = symbol__size(sym), offset;
1544
1545         h->sum = 0;
1546         for (offset = 0; offset < len; ++offset) {
1547                 h->addr[offset] = h->addr[offset] * 7 / 8;
1548                 h->sum += h->addr[offset];
1549         }
1550 }
1551
1552 void disasm__purge(struct list_head *head)
1553 {
1554         struct disasm_line *pos, *n;
1555
1556         list_for_each_entry_safe(pos, n, head, node) {
1557                 list_del(&pos->node);
1558                 disasm_line__free(pos);
1559         }
1560 }
1561
1562 static size_t disasm_line__fprintf(struct disasm_line *dl, FILE *fp)
1563 {
1564         size_t printed;
1565
1566         if (dl->offset == -1)
1567                 return fprintf(fp, "%s\n", dl->line);
1568
1569         printed = fprintf(fp, "%#" PRIx64 " %s", dl->offset, dl->name);
1570
1571         if (dl->ops.raw[0] != '\0') {
1572                 printed += fprintf(fp, "%.*s %s\n", 6 - (int)printed, " ",
1573                                    dl->ops.raw);
1574         }
1575
1576         return printed + fprintf(fp, "\n");
1577 }
1578
1579 size_t disasm__fprintf(struct list_head *head, FILE *fp)
1580 {
1581         struct disasm_line *pos;
1582         size_t printed = 0;
1583
1584         list_for_each_entry(pos, head, node)
1585                 printed += disasm_line__fprintf(pos, fp);
1586
1587         return printed;
1588 }
1589
1590 int symbol__tty_annotate(struct symbol *sym, struct map *map,
1591                          struct perf_evsel *evsel, bool print_lines,
1592                          bool full_paths, int min_pcnt, int max_lines)
1593 {
1594         struct dso *dso = map->dso;
1595         struct rb_root source_line = RB_ROOT;
1596         u64 len;
1597
1598         if (symbol__annotate(sym, map, 0) < 0)
1599                 return -1;
1600
1601         len = symbol__size(sym);
1602
1603         if (print_lines) {
1604                 symbol__get_source_line(sym, map, evsel, &source_line, len);
1605                 print_summary(&source_line, dso->long_name);
1606         }
1607
1608         symbol__annotate_printf(sym, map, evsel, full_paths,
1609                                 min_pcnt, max_lines, 0);
1610         if (print_lines)
1611                 symbol__free_source_line(sym, len);
1612
1613         disasm__purge(&symbol__annotation(sym)->src->source);
1614
1615         return 0;
1616 }
1617
1618 int hist_entry__annotate(struct hist_entry *he, size_t privsize)
1619 {
1620         return symbol__annotate(he->ms.sym, he->ms.map, privsize);
1621 }
1622
1623 bool ui__has_annotation(void)
1624 {
1625         return use_browser == 1 && sort__has_sym;
1626 }