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