perf annotate: Resolve symbols using objdump comment for single op ins
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / tools / perf / util / annotate.c
bloba6109dc3a81eaac12fe27dded8e7a0e9046d41ac
1 /*
2 * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
4 * Parts came from builtin-annotate.c, see those files for further
5 * copyright notes.
7 * Released under the GPL v2. (and only v2, not any later version)
8 */
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>
19 const char *disassembler_style;
21 static int ins__raw_scnprintf(struct ins *ins, char *bf, size_t size,
22 struct ins_operands *ops)
24 return scnprintf(bf, size, "%-6.6s %s", ins->name, ops->raw);
27 int ins__scnprintf(struct ins *ins, char *bf, size_t size,
28 struct ins_operands *ops)
30 if (ins->ops->scnprintf)
31 return ins->ops->scnprintf(ins, bf, size, ops);
33 return ins__raw_scnprintf(ins, bf, size, ops);
36 static int call__parse(struct ins_operands *ops)
38 char *endptr, *tok, *name;
40 ops->target.addr = strtoull(ops->raw, &endptr, 16);
42 name = strchr(endptr, '<');
43 if (name == NULL)
44 goto indirect_call;
46 name++;
48 tok = strchr(name, '>');
49 if (tok == NULL)
50 return -1;
52 *tok = '\0';
53 ops->target.name = strdup(name);
54 *tok = '>';
56 return ops->target.name == NULL ? -1 : 0;
58 indirect_call:
59 tok = strchr(endptr, '(');
60 if (tok != NULL) {
61 ops->target.addr = 0;
62 return 0;
65 tok = strchr(endptr, '*');
66 if (tok == NULL)
67 return -1;
69 ops->target.addr = strtoull(tok + 1, NULL, 16);
70 return 0;
73 static int call__scnprintf(struct ins *ins, char *bf, size_t size,
74 struct ins_operands *ops)
76 if (ops->target.name)
77 return scnprintf(bf, size, "%-6.6s %s", ins->name, ops->target.name);
79 if (ops->target.addr == 0)
80 return ins__raw_scnprintf(ins, bf, size, ops);
82 return scnprintf(bf, size, "%-6.6s *%" PRIx64, ins->name, ops->target.addr);
85 static struct ins_ops call_ops = {
86 .parse = call__parse,
87 .scnprintf = call__scnprintf,
90 bool ins__is_call(const struct ins *ins)
92 return ins->ops == &call_ops;
95 static int jump__parse(struct ins_operands *ops)
97 const char *s = strchr(ops->raw, '+');
99 ops->target.addr = strtoll(ops->raw, NULL, 16);
101 if (s++ != NULL)
102 ops->target.offset = strtoll(s, NULL, 16);
103 else
104 ops->target.offset = UINT64_MAX;
106 return 0;
109 static int jump__scnprintf(struct ins *ins, char *bf, size_t size,
110 struct ins_operands *ops)
112 return scnprintf(bf, size, "%-6.6s %" PRIx64, ins->name, ops->target.offset);
115 static struct ins_ops jump_ops = {
116 .parse = jump__parse,
117 .scnprintf = jump__scnprintf,
120 bool ins__is_jump(const struct ins *ins)
122 return ins->ops == &jump_ops;
125 static int comment__symbol(char *raw, char *comment, u64 *addrp, char **namep)
127 char *endptr, *name, *t;
129 if (strstr(raw, "(%rip)") == NULL)
130 return 0;
132 *addrp = strtoull(comment, &endptr, 16);
133 name = strchr(endptr, '<');
134 if (name == NULL)
135 return -1;
137 name++;
139 t = strchr(name, '>');
140 if (t == NULL)
141 return 0;
143 *t = '\0';
144 *namep = strdup(name);
145 *t = '>';
147 return 0;
150 static int mov__parse(struct ins_operands *ops)
152 char *s = strchr(ops->raw, ','), *target, *comment, prev;
154 if (s == NULL)
155 return -1;
157 *s = '\0';
158 ops->source.raw = strdup(ops->raw);
159 *s = ',';
161 if (ops->source.raw == NULL)
162 return -1;
164 target = ++s;
166 while (s[0] != '\0' && !isspace(s[0]))
167 ++s;
168 prev = *s;
169 *s = '\0';
171 ops->target.raw = strdup(target);
172 *s = prev;
174 if (ops->target.raw == NULL)
175 goto out_free_source;
177 comment = strchr(s, '#');
178 if (comment == NULL)
179 return 0;
181 while (comment[0] != '\0' && isspace(comment[0]))
182 ++comment;
184 comment__symbol(ops->source.raw, comment, &ops->source.addr, &ops->source.name);
185 comment__symbol(ops->target.raw, comment, &ops->target.addr, &ops->target.name);
187 return 0;
189 out_free_source:
190 free(ops->source.raw);
191 ops->source.raw = NULL;
192 return -1;
195 static int mov__scnprintf(struct ins *ins, char *bf, size_t size,
196 struct ins_operands *ops)
198 return scnprintf(bf, size, "%-6.6s %s,%s", ins->name,
199 ops->source.name ?: ops->source.raw,
200 ops->target.name ?: ops->target.raw);
203 static struct ins_ops mov_ops = {
204 .parse = mov__parse,
205 .scnprintf = mov__scnprintf,
208 static int dec__parse(struct ins_operands *ops)
210 char *target, *comment, *s, prev;
212 target = s = ops->raw;
214 while (s[0] != '\0' && !isspace(s[0]))
215 ++s;
216 prev = *s;
217 *s = '\0';
219 ops->target.raw = strdup(target);
220 *s = prev;
222 if (ops->target.raw == NULL)
223 return -1;
225 comment = strchr(s, '#');
226 if (comment == NULL)
227 return 0;
229 while (comment[0] != '\0' && isspace(comment[0]))
230 ++comment;
232 comment__symbol(ops->target.raw, comment, &ops->target.addr, &ops->target.name);
234 return 0;
237 static int dec__scnprintf(struct ins *ins, char *bf, size_t size,
238 struct ins_operands *ops)
240 return scnprintf(bf, size, "%-6.6s %s", ins->name,
241 ops->target.name ?: ops->target.raw);
244 static struct ins_ops dec_ops = {
245 .parse = dec__parse,
246 .scnprintf = dec__scnprintf,
249 static int nop__scnprintf(struct ins *ins __used, char *bf, size_t size,
250 struct ins_operands *ops __used)
252 return scnprintf(bf, size, "%-6.6s", "nop");
255 static struct ins_ops nop_ops = {
256 .scnprintf = nop__scnprintf,
260 * Must be sorted by name!
262 static struct ins instructions[] = {
263 { .name = "add", .ops = &mov_ops, },
264 { .name = "addl", .ops = &mov_ops, },
265 { .name = "addq", .ops = &mov_ops, },
266 { .name = "addw", .ops = &mov_ops, },
267 { .name = "and", .ops = &mov_ops, },
268 { .name = "call", .ops = &call_ops, },
269 { .name = "callq", .ops = &call_ops, },
270 { .name = "cmp", .ops = &mov_ops, },
271 { .name = "cmpb", .ops = &mov_ops, },
272 { .name = "cmpl", .ops = &mov_ops, },
273 { .name = "cmpq", .ops = &mov_ops, },
274 { .name = "cmpw", .ops = &mov_ops, },
275 { .name = "cmpxch", .ops = &mov_ops, },
276 { .name = "dec", .ops = &dec_ops, },
277 { .name = "decl", .ops = &dec_ops, },
278 { .name = "imul", .ops = &mov_ops, },
279 { .name = "inc", .ops = &dec_ops, },
280 { .name = "incl", .ops = &dec_ops, },
281 { .name = "ja", .ops = &jump_ops, },
282 { .name = "jae", .ops = &jump_ops, },
283 { .name = "jb", .ops = &jump_ops, },
284 { .name = "jbe", .ops = &jump_ops, },
285 { .name = "jc", .ops = &jump_ops, },
286 { .name = "jcxz", .ops = &jump_ops, },
287 { .name = "je", .ops = &jump_ops, },
288 { .name = "jecxz", .ops = &jump_ops, },
289 { .name = "jg", .ops = &jump_ops, },
290 { .name = "jge", .ops = &jump_ops, },
291 { .name = "jl", .ops = &jump_ops, },
292 { .name = "jle", .ops = &jump_ops, },
293 { .name = "jmp", .ops = &jump_ops, },
294 { .name = "jmpq", .ops = &jump_ops, },
295 { .name = "jna", .ops = &jump_ops, },
296 { .name = "jnae", .ops = &jump_ops, },
297 { .name = "jnb", .ops = &jump_ops, },
298 { .name = "jnbe", .ops = &jump_ops, },
299 { .name = "jnc", .ops = &jump_ops, },
300 { .name = "jne", .ops = &jump_ops, },
301 { .name = "jng", .ops = &jump_ops, },
302 { .name = "jnge", .ops = &jump_ops, },
303 { .name = "jnl", .ops = &jump_ops, },
304 { .name = "jnle", .ops = &jump_ops, },
305 { .name = "jno", .ops = &jump_ops, },
306 { .name = "jnp", .ops = &jump_ops, },
307 { .name = "jns", .ops = &jump_ops, },
308 { .name = "jnz", .ops = &jump_ops, },
309 { .name = "jo", .ops = &jump_ops, },
310 { .name = "jp", .ops = &jump_ops, },
311 { .name = "jpe", .ops = &jump_ops, },
312 { .name = "jpo", .ops = &jump_ops, },
313 { .name = "jrcxz", .ops = &jump_ops, },
314 { .name = "js", .ops = &jump_ops, },
315 { .name = "jz", .ops = &jump_ops, },
316 { .name = "lea", .ops = &mov_ops, },
317 { .name = "mov", .ops = &mov_ops, },
318 { .name = "movb", .ops = &mov_ops, },
319 { .name = "movdqa",.ops = &mov_ops, },
320 { .name = "movl", .ops = &mov_ops, },
321 { .name = "movq", .ops = &mov_ops, },
322 { .name = "movslq", .ops = &mov_ops, },
323 { .name = "movzbl", .ops = &mov_ops, },
324 { .name = "movzwl", .ops = &mov_ops, },
325 { .name = "nop", .ops = &nop_ops, },
326 { .name = "nopl", .ops = &nop_ops, },
327 { .name = "nopw", .ops = &nop_ops, },
328 { .name = "or", .ops = &mov_ops, },
329 { .name = "orl", .ops = &mov_ops, },
330 { .name = "test", .ops = &mov_ops, },
331 { .name = "testb", .ops = &mov_ops, },
332 { .name = "testl", .ops = &mov_ops, },
335 static int ins__cmp(const void *name, const void *insp)
337 const struct ins *ins = insp;
339 return strcmp(name, ins->name);
342 static struct ins *ins__find(const char *name)
344 const int nmemb = ARRAY_SIZE(instructions);
346 return bsearch(name, instructions, nmemb, sizeof(struct ins), ins__cmp);
349 int symbol__annotate_init(struct map *map __used, struct symbol *sym)
351 struct annotation *notes = symbol__annotation(sym);
352 pthread_mutex_init(&notes->lock, NULL);
353 return 0;
356 int symbol__alloc_hist(struct symbol *sym)
358 struct annotation *notes = symbol__annotation(sym);
359 const size_t size = symbol__size(sym);
360 size_t sizeof_sym_hist = (sizeof(struct sym_hist) + size * sizeof(u64));
362 notes->src = zalloc(sizeof(*notes->src) + symbol_conf.nr_events * sizeof_sym_hist);
363 if (notes->src == NULL)
364 return -1;
365 notes->src->sizeof_sym_hist = sizeof_sym_hist;
366 notes->src->nr_histograms = symbol_conf.nr_events;
367 INIT_LIST_HEAD(&notes->src->source);
368 return 0;
371 void symbol__annotate_zero_histograms(struct symbol *sym)
373 struct annotation *notes = symbol__annotation(sym);
375 pthread_mutex_lock(&notes->lock);
376 if (notes->src != NULL)
377 memset(notes->src->histograms, 0,
378 notes->src->nr_histograms * notes->src->sizeof_sym_hist);
379 pthread_mutex_unlock(&notes->lock);
382 int symbol__inc_addr_samples(struct symbol *sym, struct map *map,
383 int evidx, u64 addr)
385 unsigned offset;
386 struct annotation *notes;
387 struct sym_hist *h;
389 notes = symbol__annotation(sym);
390 if (notes->src == NULL)
391 return -ENOMEM;
393 pr_debug3("%s: addr=%#" PRIx64 "\n", __func__, map->unmap_ip(map, addr));
395 if (addr < sym->start || addr > sym->end)
396 return -ERANGE;
398 offset = addr - sym->start;
399 h = annotation__histogram(notes, evidx);
400 h->sum++;
401 h->addr[offset]++;
403 pr_debug3("%#" PRIx64 " %s: period++ [addr: %#" PRIx64 ", %#" PRIx64
404 ", evidx=%d] => %" PRIu64 "\n", sym->start, sym->name,
405 addr, addr - sym->start, evidx, h->addr[offset]);
406 return 0;
409 static void disasm_line__init_ins(struct disasm_line *dl)
411 dl->ins = ins__find(dl->name);
413 if (dl->ins == NULL)
414 return;
416 if (!dl->ins->ops)
417 return;
419 if (dl->ins->ops->parse)
420 dl->ins->ops->parse(&dl->ops);
423 static struct disasm_line *disasm_line__new(s64 offset, char *line, size_t privsize)
425 struct disasm_line *dl = zalloc(sizeof(*dl) + privsize);
427 if (dl != NULL) {
428 dl->offset = offset;
429 dl->line = strdup(line);
430 if (dl->line == NULL)
431 goto out_delete;
433 if (offset != -1) {
434 char *name = dl->line, tmp;
436 while (isspace(name[0]))
437 ++name;
439 if (name[0] == '\0')
440 goto out_delete;
442 dl->ops.raw = name + 1;
444 while (dl->ops.raw[0] != '\0' &&
445 !isspace(dl->ops.raw[0]))
446 ++dl->ops.raw;
448 tmp = dl->ops.raw[0];
449 dl->ops.raw[0] = '\0';
450 dl->name = strdup(name);
452 if (dl->name == NULL)
453 goto out_free_line;
455 dl->ops.raw[0] = tmp;
457 if (dl->ops.raw[0] != '\0') {
458 dl->ops.raw++;
459 while (isspace(dl->ops.raw[0]))
460 ++dl->ops.raw;
463 disasm_line__init_ins(dl);
467 return dl;
469 out_free_line:
470 free(dl->line);
471 out_delete:
472 free(dl);
473 return NULL;
476 void disasm_line__free(struct disasm_line *dl)
478 free(dl->line);
479 free(dl->name);
480 free(dl->ops.source.raw);
481 free(dl->ops.source.name);
482 free(dl->ops.target.raw);
483 free(dl->ops.target.name);
484 free(dl);
487 int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size, bool raw)
489 if (raw || !dl->ins)
490 return scnprintf(bf, size, "%-6.6s %s", dl->name, dl->ops.raw);
492 return ins__scnprintf(dl->ins, bf, size, &dl->ops);
495 static void disasm__add(struct list_head *head, struct disasm_line *line)
497 list_add_tail(&line->node, head);
500 struct disasm_line *disasm__get_next_ip_line(struct list_head *head, struct disasm_line *pos)
502 list_for_each_entry_continue(pos, head, node)
503 if (pos->offset >= 0)
504 return pos;
506 return NULL;
509 static int disasm_line__print(struct disasm_line *dl, struct symbol *sym, u64 start,
510 int evidx, u64 len, int min_pcnt, int printed,
511 int max_lines, struct disasm_line *queue)
513 static const char *prev_line;
514 static const char *prev_color;
516 if (dl->offset != -1) {
517 const char *path = NULL;
518 unsigned int hits = 0;
519 double percent = 0.0;
520 const char *color;
521 struct annotation *notes = symbol__annotation(sym);
522 struct source_line *src_line = notes->src->lines;
523 struct sym_hist *h = annotation__histogram(notes, evidx);
524 s64 offset = dl->offset;
525 const u64 addr = start + offset;
526 struct disasm_line *next;
528 next = disasm__get_next_ip_line(&notes->src->source, dl);
530 while (offset < (s64)len &&
531 (next == NULL || offset < next->offset)) {
532 if (src_line) {
533 if (path == NULL)
534 path = src_line[offset].path;
535 percent += src_line[offset].percent;
536 } else
537 hits += h->addr[offset];
539 ++offset;
542 if (src_line == NULL && h->sum)
543 percent = 100.0 * hits / h->sum;
545 if (percent < min_pcnt)
546 return -1;
548 if (max_lines && printed >= max_lines)
549 return 1;
551 if (queue != NULL) {
552 list_for_each_entry_from(queue, &notes->src->source, node) {
553 if (queue == dl)
554 break;
555 disasm_line__print(queue, sym, start, evidx, len,
556 0, 0, 1, NULL);
560 color = get_percent_color(percent);
563 * Also color the filename and line if needed, with
564 * the same color than the percentage. Don't print it
565 * twice for close colored addr with the same filename:line
567 if (path) {
568 if (!prev_line || strcmp(prev_line, path)
569 || color != prev_color) {
570 color_fprintf(stdout, color, " %s", path);
571 prev_line = path;
572 prev_color = color;
576 color_fprintf(stdout, color, " %7.2f", percent);
577 printf(" : ");
578 color_fprintf(stdout, PERF_COLOR_MAGENTA, " %" PRIx64 ":", addr);
579 color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", dl->line);
580 } else if (max_lines && printed >= max_lines)
581 return 1;
582 else {
583 if (queue)
584 return -1;
586 if (!*dl->line)
587 printf(" :\n");
588 else
589 printf(" : %s\n", dl->line);
592 return 0;
595 static int symbol__parse_objdump_line(struct symbol *sym, struct map *map,
596 FILE *file, size_t privsize)
598 struct annotation *notes = symbol__annotation(sym);
599 struct disasm_line *dl;
600 char *line = NULL, *parsed_line, *tmp, *tmp2, *c;
601 size_t line_len;
602 s64 line_ip, offset = -1;
604 if (getline(&line, &line_len, file) < 0)
605 return -1;
607 if (!line)
608 return -1;
610 while (line_len != 0 && isspace(line[line_len - 1]))
611 line[--line_len] = '\0';
613 c = strchr(line, '\n');
614 if (c)
615 *c = 0;
617 line_ip = -1;
618 parsed_line = line;
621 * Strip leading spaces:
623 tmp = line;
624 while (*tmp) {
625 if (*tmp != ' ')
626 break;
627 tmp++;
630 if (*tmp) {
632 * Parse hexa addresses followed by ':'
634 line_ip = strtoull(tmp, &tmp2, 16);
635 if (*tmp2 != ':' || tmp == tmp2 || tmp2[1] == '\0')
636 line_ip = -1;
639 if (line_ip != -1) {
640 u64 start = map__rip_2objdump(map, sym->start),
641 end = map__rip_2objdump(map, sym->end);
643 offset = line_ip - start;
644 if (offset < 0 || (u64)line_ip > end)
645 offset = -1;
646 else
647 parsed_line = tmp2 + 1;
650 dl = disasm_line__new(offset, parsed_line, privsize);
651 free(line);
653 if (dl == NULL)
654 return -1;
656 disasm__add(&notes->src->source, dl);
658 return 0;
661 int symbol__annotate(struct symbol *sym, struct map *map, size_t privsize)
663 struct dso *dso = map->dso;
664 char *filename = dso__build_id_filename(dso, NULL, 0);
665 bool free_filename = true;
666 char command[PATH_MAX * 2];
667 FILE *file;
668 int err = 0;
669 char symfs_filename[PATH_MAX];
671 if (filename) {
672 snprintf(symfs_filename, sizeof(symfs_filename), "%s%s",
673 symbol_conf.symfs, filename);
676 if (filename == NULL) {
677 if (dso->has_build_id) {
678 pr_err("Can't annotate %s: not enough memory\n",
679 sym->name);
680 return -ENOMEM;
682 goto fallback;
683 } else if (readlink(symfs_filename, command, sizeof(command)) < 0 ||
684 strstr(command, "[kernel.kallsyms]") ||
685 access(symfs_filename, R_OK)) {
686 free(filename);
687 fallback:
689 * If we don't have build-ids or the build-id file isn't in the
690 * cache, or is just a kallsyms file, well, lets hope that this
691 * DSO is the same as when 'perf record' ran.
693 filename = dso->long_name;
694 snprintf(symfs_filename, sizeof(symfs_filename), "%s%s",
695 symbol_conf.symfs, filename);
696 free_filename = false;
699 if (dso->symtab_type == SYMTAB__KALLSYMS) {
700 char bf[BUILD_ID_SIZE * 2 + 16] = " with build id ";
701 char *build_id_msg = NULL;
703 if (dso->annotate_warned)
704 goto out_free_filename;
706 if (dso->has_build_id) {
707 build_id__sprintf(dso->build_id,
708 sizeof(dso->build_id), bf + 15);
709 build_id_msg = bf;
711 err = -ENOENT;
712 dso->annotate_warned = 1;
713 pr_err("Can't annotate %s:\n\n"
714 "No vmlinux file%s\nwas found in the path.\n\n"
715 "Please use:\n\n"
716 " perf buildid-cache -av vmlinux\n\n"
717 "or:\n\n"
718 " --vmlinux vmlinux\n",
719 sym->name, build_id_msg ?: "");
720 goto out_free_filename;
723 pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__,
724 filename, sym->name, map->unmap_ip(map, sym->start),
725 map->unmap_ip(map, sym->end));
727 pr_debug("annotating [%p] %30s : [%p] %30s\n",
728 dso, dso->long_name, sym, sym->name);
730 snprintf(command, sizeof(command),
731 "objdump %s%s --start-address=0x%016" PRIx64
732 " --stop-address=0x%016" PRIx64
733 " -d %s %s -C %s|grep -v %s|expand",
734 disassembler_style ? "-M " : "",
735 disassembler_style ? disassembler_style : "",
736 map__rip_2objdump(map, sym->start),
737 map__rip_2objdump(map, sym->end+1),
738 symbol_conf.annotate_asm_raw ? "" : "--no-show-raw",
739 symbol_conf.annotate_src ? "-S" : "",
740 symfs_filename, filename);
742 pr_debug("Executing: %s\n", command);
744 file = popen(command, "r");
745 if (!file)
746 goto out_free_filename;
748 while (!feof(file))
749 if (symbol__parse_objdump_line(sym, map, file, privsize) < 0)
750 break;
752 pclose(file);
753 out_free_filename:
754 if (free_filename)
755 free(filename);
756 return err;
759 static void insert_source_line(struct rb_root *root, struct source_line *src_line)
761 struct source_line *iter;
762 struct rb_node **p = &root->rb_node;
763 struct rb_node *parent = NULL;
765 while (*p != NULL) {
766 parent = *p;
767 iter = rb_entry(parent, struct source_line, node);
769 if (src_line->percent > iter->percent)
770 p = &(*p)->rb_left;
771 else
772 p = &(*p)->rb_right;
775 rb_link_node(&src_line->node, parent, p);
776 rb_insert_color(&src_line->node, root);
779 static void symbol__free_source_line(struct symbol *sym, int len)
781 struct annotation *notes = symbol__annotation(sym);
782 struct source_line *src_line = notes->src->lines;
783 int i;
785 for (i = 0; i < len; i++)
786 free(src_line[i].path);
788 free(src_line);
789 notes->src->lines = NULL;
792 /* Get the filename:line for the colored entries */
793 static int symbol__get_source_line(struct symbol *sym, struct map *map,
794 int evidx, struct rb_root *root, int len,
795 const char *filename)
797 u64 start;
798 int i;
799 char cmd[PATH_MAX * 2];
800 struct source_line *src_line;
801 struct annotation *notes = symbol__annotation(sym);
802 struct sym_hist *h = annotation__histogram(notes, evidx);
804 if (!h->sum)
805 return 0;
807 src_line = notes->src->lines = calloc(len, sizeof(struct source_line));
808 if (!notes->src->lines)
809 return -1;
811 start = map__rip_2objdump(map, sym->start);
813 for (i = 0; i < len; i++) {
814 char *path = NULL;
815 size_t line_len;
816 u64 offset;
817 FILE *fp;
819 src_line[i].percent = 100.0 * h->addr[i] / h->sum;
820 if (src_line[i].percent <= 0.5)
821 continue;
823 offset = start + i;
824 sprintf(cmd, "addr2line -e %s %016" PRIx64, filename, offset);
825 fp = popen(cmd, "r");
826 if (!fp)
827 continue;
829 if (getline(&path, &line_len, fp) < 0 || !line_len)
830 goto next;
832 src_line[i].path = malloc(sizeof(char) * line_len + 1);
833 if (!src_line[i].path)
834 goto next;
836 strcpy(src_line[i].path, path);
837 insert_source_line(root, &src_line[i]);
839 next:
840 pclose(fp);
843 return 0;
846 static void print_summary(struct rb_root *root, const char *filename)
848 struct source_line *src_line;
849 struct rb_node *node;
851 printf("\nSorted summary for file %s\n", filename);
852 printf("----------------------------------------------\n\n");
854 if (RB_EMPTY_ROOT(root)) {
855 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
856 return;
859 node = rb_first(root);
860 while (node) {
861 double percent;
862 const char *color;
863 char *path;
865 src_line = rb_entry(node, struct source_line, node);
866 percent = src_line->percent;
867 color = get_percent_color(percent);
868 path = src_line->path;
870 color_fprintf(stdout, color, " %7.2f %s", percent, path);
871 node = rb_next(node);
875 static void symbol__annotate_hits(struct symbol *sym, int evidx)
877 struct annotation *notes = symbol__annotation(sym);
878 struct sym_hist *h = annotation__histogram(notes, evidx);
879 u64 len = symbol__size(sym), offset;
881 for (offset = 0; offset < len; ++offset)
882 if (h->addr[offset] != 0)
883 printf("%*" PRIx64 ": %" PRIu64 "\n", BITS_PER_LONG / 2,
884 sym->start + offset, h->addr[offset]);
885 printf("%*s: %" PRIu64 "\n", BITS_PER_LONG / 2, "h->sum", h->sum);
888 int symbol__annotate_printf(struct symbol *sym, struct map *map, int evidx,
889 bool full_paths, int min_pcnt, int max_lines,
890 int context)
892 struct dso *dso = map->dso;
893 const char *filename = dso->long_name, *d_filename;
894 struct annotation *notes = symbol__annotation(sym);
895 struct disasm_line *pos, *queue = NULL;
896 u64 start = map__rip_2objdump(map, sym->start);
897 int printed = 2, queue_len = 0;
898 int more = 0;
899 u64 len;
901 if (full_paths)
902 d_filename = filename;
903 else
904 d_filename = basename(filename);
906 len = symbol__size(sym);
908 printf(" Percent | Source code & Disassembly of %s\n", d_filename);
909 printf("------------------------------------------------\n");
911 if (verbose)
912 symbol__annotate_hits(sym, evidx);
914 list_for_each_entry(pos, &notes->src->source, node) {
915 if (context && queue == NULL) {
916 queue = pos;
917 queue_len = 0;
920 switch (disasm_line__print(pos, sym, start, evidx, len,
921 min_pcnt, printed, max_lines,
922 queue)) {
923 case 0:
924 ++printed;
925 if (context) {
926 printed += queue_len;
927 queue = NULL;
928 queue_len = 0;
930 break;
931 case 1:
932 /* filtered by max_lines */
933 ++more;
934 break;
935 case -1:
936 default:
938 * Filtered by min_pcnt or non IP lines when
939 * context != 0
941 if (!context)
942 break;
943 if (queue_len == context)
944 queue = list_entry(queue->node.next, typeof(*queue), node);
945 else
946 ++queue_len;
947 break;
951 return more;
954 void symbol__annotate_zero_histogram(struct symbol *sym, int evidx)
956 struct annotation *notes = symbol__annotation(sym);
957 struct sym_hist *h = annotation__histogram(notes, evidx);
959 memset(h, 0, notes->src->sizeof_sym_hist);
962 void symbol__annotate_decay_histogram(struct symbol *sym, int evidx)
964 struct annotation *notes = symbol__annotation(sym);
965 struct sym_hist *h = annotation__histogram(notes, evidx);
966 int len = symbol__size(sym), offset;
968 h->sum = 0;
969 for (offset = 0; offset < len; ++offset) {
970 h->addr[offset] = h->addr[offset] * 7 / 8;
971 h->sum += h->addr[offset];
975 void disasm__purge(struct list_head *head)
977 struct disasm_line *pos, *n;
979 list_for_each_entry_safe(pos, n, head, node) {
980 list_del(&pos->node);
981 disasm_line__free(pos);
985 static size_t disasm_line__fprintf(struct disasm_line *dl, FILE *fp)
987 size_t printed;
989 if (dl->offset == -1)
990 return fprintf(fp, "%s\n", dl->line);
992 printed = fprintf(fp, "%#" PRIx64 " %s", dl->offset, dl->name);
994 if (dl->ops.raw[0] != '\0') {
995 printed += fprintf(fp, "%.*s %s\n", 6 - (int)printed, " ",
996 dl->ops.raw);
999 return printed + fprintf(fp, "\n");
1002 size_t disasm__fprintf(struct list_head *head, FILE *fp)
1004 struct disasm_line *pos;
1005 size_t printed = 0;
1007 list_for_each_entry(pos, head, node)
1008 printed += disasm_line__fprintf(pos, fp);
1010 return printed;
1013 int symbol__tty_annotate(struct symbol *sym, struct map *map, int evidx,
1014 bool print_lines, bool full_paths, int min_pcnt,
1015 int max_lines)
1017 struct dso *dso = map->dso;
1018 const char *filename = dso->long_name;
1019 struct rb_root source_line = RB_ROOT;
1020 u64 len;
1022 if (symbol__annotate(sym, map, 0) < 0)
1023 return -1;
1025 len = symbol__size(sym);
1027 if (print_lines) {
1028 symbol__get_source_line(sym, map, evidx, &source_line,
1029 len, filename);
1030 print_summary(&source_line, filename);
1033 symbol__annotate_printf(sym, map, evidx, full_paths,
1034 min_pcnt, max_lines, 0);
1035 if (print_lines)
1036 symbol__free_source_line(sym, len);
1038 disasm__purge(&symbol__annotation(sym)->src->source);
1040 return 0;