perf annotate: Add missing jump variants
[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 <pthread.h>
18
19 const char      *disassembler_style;
20
21 static int call_ops__parse_target(const char *operands, u64 *target)
22 {
23         *target = strtoull(operands, NULL, 16);
24         return 0;
25 }
26
27 static struct ins_ops call_ops = {
28         .parse_target = call_ops__parse_target,
29 };
30
31 bool ins__is_call(const struct ins *ins)
32 {
33         return ins->ops == &call_ops;
34 }
35
36 static int jump_ops__parse_target(const char *operands, u64 *target)
37 {
38         const char *s = strchr(operands, '+');
39
40         if (s++ == NULL)
41                 return -1;
42
43         *target = strtoll(s, NULL, 16);
44         return 0;
45 }
46
47 static int jump_ops__scnprintf(struct ins *ins, char *bf, size_t size,
48                                const char *operands, u64 target)
49 {
50         if (operands)
51                 return scnprintf(bf, size, "%-6.6s %s", ins->name, operands);
52
53         return scnprintf(bf, size, "%-6.6s %" PRIx64, ins->name, target);
54 }
55
56 static struct ins_ops jump_ops = {
57         .parse_target = jump_ops__parse_target,
58         .scnprintf = jump_ops__scnprintf,
59 };
60
61 bool ins__is_jump(const struct ins *ins)
62 {
63         return ins->ops == &jump_ops;
64 }
65
66 /*
67  * Must be sorted by name!
68  */
69 static struct ins instructions[] = {
70         { .name = "call",  .ops  = &call_ops, },
71         { .name = "callq", .ops  = &call_ops, },
72         { .name = "ja",    .ops  = &jump_ops, },
73         { .name = "jae",   .ops  = &jump_ops, },
74         { .name = "jb",    .ops  = &jump_ops, },
75         { .name = "jbe",   .ops  = &jump_ops, },
76         { .name = "jc",    .ops  = &jump_ops, },
77         { .name = "jcxz",  .ops  = &jump_ops, },
78         { .name = "je",    .ops  = &jump_ops, },
79         { .name = "jecxz", .ops  = &jump_ops, },
80         { .name = "jg",    .ops  = &jump_ops, },
81         { .name = "jge",   .ops  = &jump_ops, },
82         { .name = "jl",    .ops  = &jump_ops, },
83         { .name = "jle",   .ops  = &jump_ops, },
84         { .name = "jmp",   .ops  = &jump_ops, },
85         { .name = "jmpq",  .ops  = &jump_ops, },
86         { .name = "jna",   .ops  = &jump_ops, },
87         { .name = "jnae",  .ops  = &jump_ops, },
88         { .name = "jnb",   .ops  = &jump_ops, },
89         { .name = "jnbe",  .ops  = &jump_ops, },
90         { .name = "jnc",   .ops  = &jump_ops, },
91         { .name = "jne",   .ops  = &jump_ops, },
92         { .name = "jng",   .ops  = &jump_ops, },
93         { .name = "jnge",  .ops  = &jump_ops, },
94         { .name = "jnl",   .ops  = &jump_ops, },
95         { .name = "jnle",  .ops  = &jump_ops, },
96         { .name = "jno",   .ops  = &jump_ops, },
97         { .name = "jnp",   .ops  = &jump_ops, },
98         { .name = "jns",   .ops  = &jump_ops, },
99         { .name = "jnz",   .ops  = &jump_ops, },
100         { .name = "jo",    .ops  = &jump_ops, },
101         { .name = "jp",    .ops  = &jump_ops, },
102         { .name = "jpe",   .ops  = &jump_ops, },
103         { .name = "jpo",   .ops  = &jump_ops, },
104         { .name = "jrcxz", .ops  = &jump_ops, },
105         { .name = "js",    .ops  = &jump_ops, },
106         { .name = "jz",    .ops  = &jump_ops, },
107 };
108
109 static int ins__cmp(const void *name, const void *insp)
110 {
111         const struct ins *ins = insp;
112
113         return strcmp(name, ins->name);
114 }
115
116 static struct ins *ins__find(const char *name)
117 {
118         const int nmemb = ARRAY_SIZE(instructions);
119
120         return bsearch(name, instructions, nmemb, sizeof(struct ins), ins__cmp);
121 }
122
123 int symbol__annotate_init(struct map *map __used, struct symbol *sym)
124 {
125         struct annotation *notes = symbol__annotation(sym);
126         pthread_mutex_init(&notes->lock, NULL);
127         return 0;
128 }
129
130 int symbol__alloc_hist(struct symbol *sym)
131 {
132         struct annotation *notes = symbol__annotation(sym);
133         const size_t size = symbol__size(sym);
134         size_t sizeof_sym_hist = (sizeof(struct sym_hist) + size * sizeof(u64));
135
136         notes->src = zalloc(sizeof(*notes->src) + symbol_conf.nr_events * sizeof_sym_hist);
137         if (notes->src == NULL)
138                 return -1;
139         notes->src->sizeof_sym_hist = sizeof_sym_hist;
140         notes->src->nr_histograms   = symbol_conf.nr_events;
141         INIT_LIST_HEAD(&notes->src->source);
142         return 0;
143 }
144
145 void symbol__annotate_zero_histograms(struct symbol *sym)
146 {
147         struct annotation *notes = symbol__annotation(sym);
148
149         pthread_mutex_lock(&notes->lock);
150         if (notes->src != NULL)
151                 memset(notes->src->histograms, 0,
152                        notes->src->nr_histograms * notes->src->sizeof_sym_hist);
153         pthread_mutex_unlock(&notes->lock);
154 }
155
156 int symbol__inc_addr_samples(struct symbol *sym, struct map *map,
157                              int evidx, u64 addr)
158 {
159         unsigned offset;
160         struct annotation *notes;
161         struct sym_hist *h;
162
163         notes = symbol__annotation(sym);
164         if (notes->src == NULL)
165                 return -ENOMEM;
166
167         pr_debug3("%s: addr=%#" PRIx64 "\n", __func__, map->unmap_ip(map, addr));
168
169         if (addr < sym->start || addr > sym->end)
170                 return -ERANGE;
171
172         offset = addr - sym->start;
173         h = annotation__histogram(notes, evidx);
174         h->sum++;
175         h->addr[offset]++;
176
177         pr_debug3("%#" PRIx64 " %s: period++ [addr: %#" PRIx64 ", %#" PRIx64
178                   ", evidx=%d] => %" PRIu64 "\n", sym->start, sym->name,
179                   addr, addr - sym->start, evidx, h->addr[offset]);
180         return 0;
181 }
182
183 static void disasm_line__init_ins(struct disasm_line *dl)
184 {
185         dl->ins = ins__find(dl->name);
186
187         if (dl->ins == NULL)
188                 return;
189
190         if (!dl->ins->ops)
191                 return;
192
193         if (dl->ins->ops->parse_target)
194                 dl->ins->ops->parse_target(dl->operands, &dl->target);
195 }
196
197 static struct disasm_line *disasm_line__new(s64 offset, char *line, size_t privsize)
198 {
199         struct disasm_line *dl = zalloc(sizeof(*dl) + privsize);
200
201         if (dl != NULL) {
202                 dl->offset = offset;
203                 dl->line = strdup(line);
204                 if (dl->line == NULL)
205                         goto out_delete;
206
207                 if (offset != -1) {
208                         char *name = dl->line, tmp;
209
210                         while (isspace(name[0]))
211                                 ++name;
212
213                         if (name[0] == '\0')
214                                 goto out_delete;
215
216                         dl->operands = name + 1;
217
218                         while (dl->operands[0] != '\0' &&
219                                !isspace(dl->operands[0]))
220                                 ++dl->operands;
221
222                         tmp = dl->operands[0];
223                         dl->operands[0] = '\0';
224                         dl->name = strdup(name);
225
226                         if (dl->name == NULL)
227                                 goto out_free_line;
228
229                         dl->operands[0] = tmp;
230
231                         if (dl->operands[0] != '\0') {
232                                 dl->operands++;
233                                 while (isspace(dl->operands[0]))
234                                         ++dl->operands;
235                         }
236
237                         disasm_line__init_ins(dl);
238                 }
239         }
240
241         return dl;
242
243 out_free_line:
244         free(dl->line);
245 out_delete:
246         free(dl);
247         return NULL;
248 }
249
250 void disasm_line__free(struct disasm_line *dl)
251 {
252         free(dl->line);
253         free(dl->name);
254         free(dl);
255 }
256
257 static void disasm__add(struct list_head *head, struct disasm_line *line)
258 {
259         list_add_tail(&line->node, head);
260 }
261
262 struct disasm_line *disasm__get_next_ip_line(struct list_head *head, struct disasm_line *pos)
263 {
264         list_for_each_entry_continue(pos, head, node)
265                 if (pos->offset >= 0)
266                         return pos;
267
268         return NULL;
269 }
270
271 static int disasm_line__print(struct disasm_line *dl, struct symbol *sym, u64 start,
272                       int evidx, u64 len, int min_pcnt, int printed,
273                       int max_lines, struct disasm_line *queue)
274 {
275         static const char *prev_line;
276         static const char *prev_color;
277
278         if (dl->offset != -1) {
279                 const char *path = NULL;
280                 unsigned int hits = 0;
281                 double percent = 0.0;
282                 const char *color;
283                 struct annotation *notes = symbol__annotation(sym);
284                 struct source_line *src_line = notes->src->lines;
285                 struct sym_hist *h = annotation__histogram(notes, evidx);
286                 s64 offset = dl->offset;
287                 const u64 addr = start + offset;
288                 struct disasm_line *next;
289
290                 next = disasm__get_next_ip_line(&notes->src->source, dl);
291
292                 while (offset < (s64)len &&
293                        (next == NULL || offset < next->offset)) {
294                         if (src_line) {
295                                 if (path == NULL)
296                                         path = src_line[offset].path;
297                                 percent += src_line[offset].percent;
298                         } else
299                                 hits += h->addr[offset];
300
301                         ++offset;
302                 }
303
304                 if (src_line == NULL && h->sum)
305                         percent = 100.0 * hits / h->sum;
306
307                 if (percent < min_pcnt)
308                         return -1;
309
310                 if (max_lines && printed >= max_lines)
311                         return 1;
312
313                 if (queue != NULL) {
314                         list_for_each_entry_from(queue, &notes->src->source, node) {
315                                 if (queue == dl)
316                                         break;
317                                 disasm_line__print(queue, sym, start, evidx, len,
318                                                     0, 0, 1, NULL);
319                         }
320                 }
321
322                 color = get_percent_color(percent);
323
324                 /*
325                  * Also color the filename and line if needed, with
326                  * the same color than the percentage. Don't print it
327                  * twice for close colored addr with the same filename:line
328                  */
329                 if (path) {
330                         if (!prev_line || strcmp(prev_line, path)
331                                        || color != prev_color) {
332                                 color_fprintf(stdout, color, " %s", path);
333                                 prev_line = path;
334                                 prev_color = color;
335                         }
336                 }
337
338                 color_fprintf(stdout, color, " %7.2f", percent);
339                 printf(" :      ");
340                 color_fprintf(stdout, PERF_COLOR_MAGENTA, "  %" PRIx64 ":", addr);
341                 color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", dl->line);
342         } else if (max_lines && printed >= max_lines)
343                 return 1;
344         else {
345                 if (queue)
346                         return -1;
347
348                 if (!*dl->line)
349                         printf("         :\n");
350                 else
351                         printf("         :      %s\n", dl->line);
352         }
353
354         return 0;
355 }
356
357 static int symbol__parse_objdump_line(struct symbol *sym, struct map *map,
358                                       FILE *file, size_t privsize)
359 {
360         struct annotation *notes = symbol__annotation(sym);
361         struct disasm_line *dl;
362         char *line = NULL, *parsed_line, *tmp, *tmp2, *c;
363         size_t line_len;
364         s64 line_ip, offset = -1;
365
366         if (getline(&line, &line_len, file) < 0)
367                 return -1;
368
369         if (!line)
370                 return -1;
371
372         while (line_len != 0 && isspace(line[line_len - 1]))
373                 line[--line_len] = '\0';
374
375         c = strchr(line, '\n');
376         if (c)
377                 *c = 0;
378
379         line_ip = -1;
380         parsed_line = line;
381
382         /*
383          * Strip leading spaces:
384          */
385         tmp = line;
386         while (*tmp) {
387                 if (*tmp != ' ')
388                         break;
389                 tmp++;
390         }
391
392         if (*tmp) {
393                 /*
394                  * Parse hexa addresses followed by ':'
395                  */
396                 line_ip = strtoull(tmp, &tmp2, 16);
397                 if (*tmp2 != ':' || tmp == tmp2 || tmp2[1] == '\0')
398                         line_ip = -1;
399         }
400
401         if (line_ip != -1) {
402                 u64 start = map__rip_2objdump(map, sym->start),
403                     end = map__rip_2objdump(map, sym->end);
404
405                 offset = line_ip - start;
406                 if (offset < 0 || (u64)line_ip > end)
407                         offset = -1;
408                 else
409                         parsed_line = tmp2 + 1;
410         }
411
412         dl = disasm_line__new(offset, parsed_line, privsize);
413         free(line);
414
415         if (dl == NULL)
416                 return -1;
417
418         disasm__add(&notes->src->source, dl);
419
420         return 0;
421 }
422
423 int symbol__annotate(struct symbol *sym, struct map *map, size_t privsize)
424 {
425         struct dso *dso = map->dso;
426         char *filename = dso__build_id_filename(dso, NULL, 0);
427         bool free_filename = true;
428         char command[PATH_MAX * 2];
429         FILE *file;
430         int err = 0;
431         char symfs_filename[PATH_MAX];
432
433         if (filename) {
434                 snprintf(symfs_filename, sizeof(symfs_filename), "%s%s",
435                          symbol_conf.symfs, filename);
436         }
437
438         if (filename == NULL) {
439                 if (dso->has_build_id) {
440                         pr_err("Can't annotate %s: not enough memory\n",
441                                sym->name);
442                         return -ENOMEM;
443                 }
444                 goto fallback;
445         } else if (readlink(symfs_filename, command, sizeof(command)) < 0 ||
446                    strstr(command, "[kernel.kallsyms]") ||
447                    access(symfs_filename, R_OK)) {
448                 free(filename);
449 fallback:
450                 /*
451                  * If we don't have build-ids or the build-id file isn't in the
452                  * cache, or is just a kallsyms file, well, lets hope that this
453                  * DSO is the same as when 'perf record' ran.
454                  */
455                 filename = dso->long_name;
456                 snprintf(symfs_filename, sizeof(symfs_filename), "%s%s",
457                          symbol_conf.symfs, filename);
458                 free_filename = false;
459         }
460
461         if (dso->symtab_type == SYMTAB__KALLSYMS) {
462                 char bf[BUILD_ID_SIZE * 2 + 16] = " with build id ";
463                 char *build_id_msg = NULL;
464
465                 if (dso->annotate_warned)
466                         goto out_free_filename;
467
468                 if (dso->has_build_id) {
469                         build_id__sprintf(dso->build_id,
470                                           sizeof(dso->build_id), bf + 15);
471                         build_id_msg = bf;
472                 }
473                 err = -ENOENT;
474                 dso->annotate_warned = 1;
475                 pr_err("Can't annotate %s:\n\n"
476                        "No vmlinux file%s\nwas found in the path.\n\n"
477                        "Please use:\n\n"
478                        "  perf buildid-cache -av vmlinux\n\n"
479                        "or:\n\n"
480                        "  --vmlinux vmlinux\n",
481                        sym->name, build_id_msg ?: "");
482                 goto out_free_filename;
483         }
484
485         pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__,
486                  filename, sym->name, map->unmap_ip(map, sym->start),
487                  map->unmap_ip(map, sym->end));
488
489         pr_debug("annotating [%p] %30s : [%p] %30s\n",
490                  dso, dso->long_name, sym, sym->name);
491
492         snprintf(command, sizeof(command),
493                  "objdump %s%s --start-address=0x%016" PRIx64
494                  " --stop-address=0x%016" PRIx64
495                  " -d %s %s -C %s|grep -v %s|expand",
496                  disassembler_style ? "-M " : "",
497                  disassembler_style ? disassembler_style : "",
498                  map__rip_2objdump(map, sym->start),
499                  map__rip_2objdump(map, sym->end+1),
500                  symbol_conf.annotate_asm_raw ? "" : "--no-show-raw",
501                  symbol_conf.annotate_src ? "-S" : "",
502                  symfs_filename, filename);
503
504         pr_debug("Executing: %s\n", command);
505
506         file = popen(command, "r");
507         if (!file)
508                 goto out_free_filename;
509
510         while (!feof(file))
511                 if (symbol__parse_objdump_line(sym, map, file, privsize) < 0)
512                         break;
513
514         pclose(file);
515 out_free_filename:
516         if (free_filename)
517                 free(filename);
518         return err;
519 }
520
521 static void insert_source_line(struct rb_root *root, struct source_line *src_line)
522 {
523         struct source_line *iter;
524         struct rb_node **p = &root->rb_node;
525         struct rb_node *parent = NULL;
526
527         while (*p != NULL) {
528                 parent = *p;
529                 iter = rb_entry(parent, struct source_line, node);
530
531                 if (src_line->percent > iter->percent)
532                         p = &(*p)->rb_left;
533                 else
534                         p = &(*p)->rb_right;
535         }
536
537         rb_link_node(&src_line->node, parent, p);
538         rb_insert_color(&src_line->node, root);
539 }
540
541 static void symbol__free_source_line(struct symbol *sym, int len)
542 {
543         struct annotation *notes = symbol__annotation(sym);
544         struct source_line *src_line = notes->src->lines;
545         int i;
546
547         for (i = 0; i < len; i++)
548                 free(src_line[i].path);
549
550         free(src_line);
551         notes->src->lines = NULL;
552 }
553
554 /* Get the filename:line for the colored entries */
555 static int symbol__get_source_line(struct symbol *sym, struct map *map,
556                                    int evidx, struct rb_root *root, int len,
557                                    const char *filename)
558 {
559         u64 start;
560         int i;
561         char cmd[PATH_MAX * 2];
562         struct source_line *src_line;
563         struct annotation *notes = symbol__annotation(sym);
564         struct sym_hist *h = annotation__histogram(notes, evidx);
565
566         if (!h->sum)
567                 return 0;
568
569         src_line = notes->src->lines = calloc(len, sizeof(struct source_line));
570         if (!notes->src->lines)
571                 return -1;
572
573         start = map__rip_2objdump(map, sym->start);
574
575         for (i = 0; i < len; i++) {
576                 char *path = NULL;
577                 size_t line_len;
578                 u64 offset;
579                 FILE *fp;
580
581                 src_line[i].percent = 100.0 * h->addr[i] / h->sum;
582                 if (src_line[i].percent <= 0.5)
583                         continue;
584
585                 offset = start + i;
586                 sprintf(cmd, "addr2line -e %s %016" PRIx64, filename, offset);
587                 fp = popen(cmd, "r");
588                 if (!fp)
589                         continue;
590
591                 if (getline(&path, &line_len, fp) < 0 || !line_len)
592                         goto next;
593
594                 src_line[i].path = malloc(sizeof(char) * line_len + 1);
595                 if (!src_line[i].path)
596                         goto next;
597
598                 strcpy(src_line[i].path, path);
599                 insert_source_line(root, &src_line[i]);
600
601         next:
602                 pclose(fp);
603         }
604
605         return 0;
606 }
607
608 static void print_summary(struct rb_root *root, const char *filename)
609 {
610         struct source_line *src_line;
611         struct rb_node *node;
612
613         printf("\nSorted summary for file %s\n", filename);
614         printf("----------------------------------------------\n\n");
615
616         if (RB_EMPTY_ROOT(root)) {
617                 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
618                 return;
619         }
620
621         node = rb_first(root);
622         while (node) {
623                 double percent;
624                 const char *color;
625                 char *path;
626
627                 src_line = rb_entry(node, struct source_line, node);
628                 percent = src_line->percent;
629                 color = get_percent_color(percent);
630                 path = src_line->path;
631
632                 color_fprintf(stdout, color, " %7.2f %s", percent, path);
633                 node = rb_next(node);
634         }
635 }
636
637 static void symbol__annotate_hits(struct symbol *sym, int evidx)
638 {
639         struct annotation *notes = symbol__annotation(sym);
640         struct sym_hist *h = annotation__histogram(notes, evidx);
641         u64 len = symbol__size(sym), offset;
642
643         for (offset = 0; offset < len; ++offset)
644                 if (h->addr[offset] != 0)
645                         printf("%*" PRIx64 ": %" PRIu64 "\n", BITS_PER_LONG / 2,
646                                sym->start + offset, h->addr[offset]);
647         printf("%*s: %" PRIu64 "\n", BITS_PER_LONG / 2, "h->sum", h->sum);
648 }
649
650 int symbol__annotate_printf(struct symbol *sym, struct map *map, int evidx,
651                             bool full_paths, int min_pcnt, int max_lines,
652                             int context)
653 {
654         struct dso *dso = map->dso;
655         const char *filename = dso->long_name, *d_filename;
656         struct annotation *notes = symbol__annotation(sym);
657         struct disasm_line *pos, *queue = NULL;
658         u64 start = map__rip_2objdump(map, sym->start);
659         int printed = 2, queue_len = 0;
660         int more = 0;
661         u64 len;
662
663         if (full_paths)
664                 d_filename = filename;
665         else
666                 d_filename = basename(filename);
667
668         len = symbol__size(sym);
669
670         printf(" Percent |      Source code & Disassembly of %s\n", d_filename);
671         printf("------------------------------------------------\n");
672
673         if (verbose)
674                 symbol__annotate_hits(sym, evidx);
675
676         list_for_each_entry(pos, &notes->src->source, node) {
677                 if (context && queue == NULL) {
678                         queue = pos;
679                         queue_len = 0;
680                 }
681
682                 switch (disasm_line__print(pos, sym, start, evidx, len,
683                                             min_pcnt, printed, max_lines,
684                                             queue)) {
685                 case 0:
686                         ++printed;
687                         if (context) {
688                                 printed += queue_len;
689                                 queue = NULL;
690                                 queue_len = 0;
691                         }
692                         break;
693                 case 1:
694                         /* filtered by max_lines */
695                         ++more;
696                         break;
697                 case -1:
698                 default:
699                         /*
700                          * Filtered by min_pcnt or non IP lines when
701                          * context != 0
702                          */
703                         if (!context)
704                                 break;
705                         if (queue_len == context)
706                                 queue = list_entry(queue->node.next, typeof(*queue), node);
707                         else
708                                 ++queue_len;
709                         break;
710                 }
711         }
712
713         return more;
714 }
715
716 void symbol__annotate_zero_histogram(struct symbol *sym, int evidx)
717 {
718         struct annotation *notes = symbol__annotation(sym);
719         struct sym_hist *h = annotation__histogram(notes, evidx);
720
721         memset(h, 0, notes->src->sizeof_sym_hist);
722 }
723
724 void symbol__annotate_decay_histogram(struct symbol *sym, int evidx)
725 {
726         struct annotation *notes = symbol__annotation(sym);
727         struct sym_hist *h = annotation__histogram(notes, evidx);
728         int len = symbol__size(sym), offset;
729
730         h->sum = 0;
731         for (offset = 0; offset < len; ++offset) {
732                 h->addr[offset] = h->addr[offset] * 7 / 8;
733                 h->sum += h->addr[offset];
734         }
735 }
736
737 void disasm__purge(struct list_head *head)
738 {
739         struct disasm_line *pos, *n;
740
741         list_for_each_entry_safe(pos, n, head, node) {
742                 list_del(&pos->node);
743                 disasm_line__free(pos);
744         }
745 }
746
747 static size_t disasm_line__fprintf(struct disasm_line *dl, FILE *fp)
748 {
749         size_t printed;
750
751         if (dl->offset == -1)
752                 return fprintf(fp, "%s\n", dl->line);
753
754         printed = fprintf(fp, "%#" PRIx64 " %s", dl->offset, dl->name);
755
756         if (dl->operands[0] != '\0') {
757                 printed += fprintf(fp, "%.*s %s\n", 6 - (int)printed, " ",
758                                    dl->operands);
759         }
760
761         return printed + fprintf(fp, "\n");
762 }
763
764 size_t disasm__fprintf(struct list_head *head, FILE *fp)
765 {
766         struct disasm_line *pos;
767         size_t printed = 0;
768
769         list_for_each_entry(pos, head, node)
770                 printed += disasm_line__fprintf(pos, fp);
771
772         return printed;
773 }
774
775 int symbol__tty_annotate(struct symbol *sym, struct map *map, int evidx,
776                          bool print_lines, bool full_paths, int min_pcnt,
777                          int max_lines)
778 {
779         struct dso *dso = map->dso;
780         const char *filename = dso->long_name;
781         struct rb_root source_line = RB_ROOT;
782         u64 len;
783
784         if (symbol__annotate(sym, map, 0) < 0)
785                 return -1;
786
787         len = symbol__size(sym);
788
789         if (print_lines) {
790                 symbol__get_source_line(sym, map, evidx, &source_line,
791                                         len, filename);
792                 print_summary(&source_line, filename);
793         }
794
795         symbol__annotate_printf(sym, map, evidx, full_paths,
796                                 min_pcnt, max_lines, 0);
797         if (print_lines)
798                 symbol__free_source_line(sym, len);
799
800         disasm__purge(&symbol__annotation(sym)->src->source);
801
802         return 0;
803 }