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
7 * Released under the GPL v2. (and only v2, not any later version)
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, '<');
48 tok = strchr(name, '>');
53 ops->target.name = strdup(name);
56 return ops->target.name == NULL ? -1 : 0;
59 tok = strchr(endptr, '(');
65 tok = strchr(endptr, '*');
69 ops->target.addr = strtoull(tok + 1, NULL, 16);
73 static int call__scnprintf(struct ins *ins, char *bf, size_t size,
74 struct ins_operands *ops)
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 = {
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);
102 ops->target.offset = strtoll(s, NULL, 16);
104 ops->target.offset = UINT64_MAX;
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)
132 *addrp = strtoull(comment, &endptr, 16);
133 name = strchr(endptr, '<');
139 t = strchr(name, '>');
144 *namep = strdup(name);
150 static int mov__parse(struct ins_operands *ops)
152 char *s = strchr(ops->raw, ','), *target, *comment, prev;
158 ops->source.raw = strdup(ops->raw);
161 if (ops->source.raw == NULL)
166 while (s[0] != '\0' && !isspace(s[0]))
171 ops->target.raw = strdup(target);
174 if (ops->target.raw == NULL)
175 goto out_free_source;
177 comment = strchr(s, '#');
181 while (comment[0] != '\0' && isspace(comment[0]))
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);
190 free(ops->source.raw);
191 ops->source.raw = NULL;
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 = {
205 .scnprintf = mov__scnprintf,
208 static int nop__scnprintf(struct ins *ins __used, char *bf, size_t size,
209 struct ins_operands *ops __used)
211 return scnprintf(bf, size, "%-6.6s", "nop");
214 static struct ins_ops nop_ops = {
215 .scnprintf = nop__scnprintf,
219 * Must be sorted by name!
221 static struct ins instructions[] = {
222 { .name = "add", .ops = &mov_ops, },
223 { .name = "addl", .ops = &mov_ops, },
224 { .name = "addq", .ops = &mov_ops, },
225 { .name = "addw", .ops = &mov_ops, },
226 { .name = "and", .ops = &mov_ops, },
227 { .name = "call", .ops = &call_ops, },
228 { .name = "callq", .ops = &call_ops, },
229 { .name = "cmp", .ops = &mov_ops, },
230 { .name = "cmpb", .ops = &mov_ops, },
231 { .name = "cmpl", .ops = &mov_ops, },
232 { .name = "cmpq", .ops = &mov_ops, },
233 { .name = "cmpw", .ops = &mov_ops, },
234 { .name = "cmpxch", .ops = &mov_ops, },
235 { .name = "imul", .ops = &mov_ops, },
236 { .name = "ja", .ops = &jump_ops, },
237 { .name = "jae", .ops = &jump_ops, },
238 { .name = "jb", .ops = &jump_ops, },
239 { .name = "jbe", .ops = &jump_ops, },
240 { .name = "jc", .ops = &jump_ops, },
241 { .name = "jcxz", .ops = &jump_ops, },
242 { .name = "je", .ops = &jump_ops, },
243 { .name = "jecxz", .ops = &jump_ops, },
244 { .name = "jg", .ops = &jump_ops, },
245 { .name = "jge", .ops = &jump_ops, },
246 { .name = "jl", .ops = &jump_ops, },
247 { .name = "jle", .ops = &jump_ops, },
248 { .name = "jmp", .ops = &jump_ops, },
249 { .name = "jmpq", .ops = &jump_ops, },
250 { .name = "jna", .ops = &jump_ops, },
251 { .name = "jnae", .ops = &jump_ops, },
252 { .name = "jnb", .ops = &jump_ops, },
253 { .name = "jnbe", .ops = &jump_ops, },
254 { .name = "jnc", .ops = &jump_ops, },
255 { .name = "jne", .ops = &jump_ops, },
256 { .name = "jng", .ops = &jump_ops, },
257 { .name = "jnge", .ops = &jump_ops, },
258 { .name = "jnl", .ops = &jump_ops, },
259 { .name = "jnle", .ops = &jump_ops, },
260 { .name = "jno", .ops = &jump_ops, },
261 { .name = "jnp", .ops = &jump_ops, },
262 { .name = "jns", .ops = &jump_ops, },
263 { .name = "jnz", .ops = &jump_ops, },
264 { .name = "jo", .ops = &jump_ops, },
265 { .name = "jp", .ops = &jump_ops, },
266 { .name = "jpe", .ops = &jump_ops, },
267 { .name = "jpo", .ops = &jump_ops, },
268 { .name = "jrcxz", .ops = &jump_ops, },
269 { .name = "js", .ops = &jump_ops, },
270 { .name = "jz", .ops = &jump_ops, },
271 { .name = "lea", .ops = &mov_ops, },
272 { .name = "mov", .ops = &mov_ops, },
273 { .name = "movb", .ops = &mov_ops, },
274 { .name = "movdqa",.ops = &mov_ops, },
275 { .name = "movl", .ops = &mov_ops, },
276 { .name = "movq", .ops = &mov_ops, },
277 { .name = "movslq", .ops = &mov_ops, },
278 { .name = "movzbl", .ops = &mov_ops, },
279 { .name = "movzwl", .ops = &mov_ops, },
280 { .name = "nop", .ops = &nop_ops, },
281 { .name = "nopl", .ops = &nop_ops, },
282 { .name = "nopw", .ops = &nop_ops, },
283 { .name = "or", .ops = &mov_ops, },
284 { .name = "orl", .ops = &mov_ops, },
285 { .name = "test", .ops = &mov_ops, },
286 { .name = "testb", .ops = &mov_ops, },
287 { .name = "testl", .ops = &mov_ops, },
290 static int ins__cmp(const void *name, const void *insp)
292 const struct ins *ins = insp;
294 return strcmp(name, ins->name);
297 static struct ins *ins__find(const char *name)
299 const int nmemb = ARRAY_SIZE(instructions);
301 return bsearch(name, instructions, nmemb, sizeof(struct ins), ins__cmp);
304 int symbol__annotate_init(struct map *map __used, struct symbol *sym)
306 struct annotation *notes = symbol__annotation(sym);
307 pthread_mutex_init(¬es->lock, NULL);
311 int symbol__alloc_hist(struct symbol *sym)
313 struct annotation *notes = symbol__annotation(sym);
314 const size_t size = symbol__size(sym);
315 size_t sizeof_sym_hist = (sizeof(struct sym_hist) + size * sizeof(u64));
317 notes->src = zalloc(sizeof(*notes->src) + symbol_conf.nr_events * sizeof_sym_hist);
318 if (notes->src == NULL)
320 notes->src->sizeof_sym_hist = sizeof_sym_hist;
321 notes->src->nr_histograms = symbol_conf.nr_events;
322 INIT_LIST_HEAD(¬es->src->source);
326 void symbol__annotate_zero_histograms(struct symbol *sym)
328 struct annotation *notes = symbol__annotation(sym);
330 pthread_mutex_lock(¬es->lock);
331 if (notes->src != NULL)
332 memset(notes->src->histograms, 0,
333 notes->src->nr_histograms * notes->src->sizeof_sym_hist);
334 pthread_mutex_unlock(¬es->lock);
337 int symbol__inc_addr_samples(struct symbol *sym, struct map *map,
341 struct annotation *notes;
344 notes = symbol__annotation(sym);
345 if (notes->src == NULL)
348 pr_debug3("%s: addr=%#" PRIx64 "\n", __func__, map->unmap_ip(map, addr));
350 if (addr < sym->start || addr > sym->end)
353 offset = addr - sym->start;
354 h = annotation__histogram(notes, evidx);
358 pr_debug3("%#" PRIx64 " %s: period++ [addr: %#" PRIx64 ", %#" PRIx64
359 ", evidx=%d] => %" PRIu64 "\n", sym->start, sym->name,
360 addr, addr - sym->start, evidx, h->addr[offset]);
364 static void disasm_line__init_ins(struct disasm_line *dl)
366 dl->ins = ins__find(dl->name);
374 if (dl->ins->ops->parse)
375 dl->ins->ops->parse(&dl->ops);
378 static struct disasm_line *disasm_line__new(s64 offset, char *line, size_t privsize)
380 struct disasm_line *dl = zalloc(sizeof(*dl) + privsize);
384 dl->line = strdup(line);
385 if (dl->line == NULL)
389 char *name = dl->line, tmp;
391 while (isspace(name[0]))
397 dl->ops.raw = name + 1;
399 while (dl->ops.raw[0] != '\0' &&
400 !isspace(dl->ops.raw[0]))
403 tmp = dl->ops.raw[0];
404 dl->ops.raw[0] = '\0';
405 dl->name = strdup(name);
407 if (dl->name == NULL)
410 dl->ops.raw[0] = tmp;
412 if (dl->ops.raw[0] != '\0') {
414 while (isspace(dl->ops.raw[0]))
418 disasm_line__init_ins(dl);
431 void disasm_line__free(struct disasm_line *dl)
435 free(dl->ops.source.raw);
436 free(dl->ops.source.name);
437 free(dl->ops.target.raw);
438 free(dl->ops.target.name);
442 int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size, bool raw)
445 return scnprintf(bf, size, "%-6.6s %s", dl->name, dl->ops.raw);
447 return ins__scnprintf(dl->ins, bf, size, &dl->ops);
450 static void disasm__add(struct list_head *head, struct disasm_line *line)
452 list_add_tail(&line->node, head);
455 struct disasm_line *disasm__get_next_ip_line(struct list_head *head, struct disasm_line *pos)
457 list_for_each_entry_continue(pos, head, node)
458 if (pos->offset >= 0)
464 static int disasm_line__print(struct disasm_line *dl, struct symbol *sym, u64 start,
465 int evidx, u64 len, int min_pcnt, int printed,
466 int max_lines, struct disasm_line *queue)
468 static const char *prev_line;
469 static const char *prev_color;
471 if (dl->offset != -1) {
472 const char *path = NULL;
473 unsigned int hits = 0;
474 double percent = 0.0;
476 struct annotation *notes = symbol__annotation(sym);
477 struct source_line *src_line = notes->src->lines;
478 struct sym_hist *h = annotation__histogram(notes, evidx);
479 s64 offset = dl->offset;
480 const u64 addr = start + offset;
481 struct disasm_line *next;
483 next = disasm__get_next_ip_line(¬es->src->source, dl);
485 while (offset < (s64)len &&
486 (next == NULL || offset < next->offset)) {
489 path = src_line[offset].path;
490 percent += src_line[offset].percent;
492 hits += h->addr[offset];
497 if (src_line == NULL && h->sum)
498 percent = 100.0 * hits / h->sum;
500 if (percent < min_pcnt)
503 if (max_lines && printed >= max_lines)
507 list_for_each_entry_from(queue, ¬es->src->source, node) {
510 disasm_line__print(queue, sym, start, evidx, len,
515 color = get_percent_color(percent);
518 * Also color the filename and line if needed, with
519 * the same color than the percentage. Don't print it
520 * twice for close colored addr with the same filename:line
523 if (!prev_line || strcmp(prev_line, path)
524 || color != prev_color) {
525 color_fprintf(stdout, color, " %s", path);
531 color_fprintf(stdout, color, " %7.2f", percent);
533 color_fprintf(stdout, PERF_COLOR_MAGENTA, " %" PRIx64 ":", addr);
534 color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", dl->line);
535 } else if (max_lines && printed >= max_lines)
544 printf(" : %s\n", dl->line);
550 static int symbol__parse_objdump_line(struct symbol *sym, struct map *map,
551 FILE *file, size_t privsize)
553 struct annotation *notes = symbol__annotation(sym);
554 struct disasm_line *dl;
555 char *line = NULL, *parsed_line, *tmp, *tmp2, *c;
557 s64 line_ip, offset = -1;
559 if (getline(&line, &line_len, file) < 0)
565 while (line_len != 0 && isspace(line[line_len - 1]))
566 line[--line_len] = '\0';
568 c = strchr(line, '\n');
576 * Strip leading spaces:
587 * Parse hexa addresses followed by ':'
589 line_ip = strtoull(tmp, &tmp2, 16);
590 if (*tmp2 != ':' || tmp == tmp2 || tmp2[1] == '\0')
595 u64 start = map__rip_2objdump(map, sym->start),
596 end = map__rip_2objdump(map, sym->end);
598 offset = line_ip - start;
599 if (offset < 0 || (u64)line_ip > end)
602 parsed_line = tmp2 + 1;
605 dl = disasm_line__new(offset, parsed_line, privsize);
611 disasm__add(¬es->src->source, dl);
616 int symbol__annotate(struct symbol *sym, struct map *map, size_t privsize)
618 struct dso *dso = map->dso;
619 char *filename = dso__build_id_filename(dso, NULL, 0);
620 bool free_filename = true;
621 char command[PATH_MAX * 2];
624 char symfs_filename[PATH_MAX];
627 snprintf(symfs_filename, sizeof(symfs_filename), "%s%s",
628 symbol_conf.symfs, filename);
631 if (filename == NULL) {
632 if (dso->has_build_id) {
633 pr_err("Can't annotate %s: not enough memory\n",
638 } else if (readlink(symfs_filename, command, sizeof(command)) < 0 ||
639 strstr(command, "[kernel.kallsyms]") ||
640 access(symfs_filename, R_OK)) {
644 * If we don't have build-ids or the build-id file isn't in the
645 * cache, or is just a kallsyms file, well, lets hope that this
646 * DSO is the same as when 'perf record' ran.
648 filename = dso->long_name;
649 snprintf(symfs_filename, sizeof(symfs_filename), "%s%s",
650 symbol_conf.symfs, filename);
651 free_filename = false;
654 if (dso->symtab_type == SYMTAB__KALLSYMS) {
655 char bf[BUILD_ID_SIZE * 2 + 16] = " with build id ";
656 char *build_id_msg = NULL;
658 if (dso->annotate_warned)
659 goto out_free_filename;
661 if (dso->has_build_id) {
662 build_id__sprintf(dso->build_id,
663 sizeof(dso->build_id), bf + 15);
667 dso->annotate_warned = 1;
668 pr_err("Can't annotate %s:\n\n"
669 "No vmlinux file%s\nwas found in the path.\n\n"
671 " perf buildid-cache -av vmlinux\n\n"
673 " --vmlinux vmlinux\n",
674 sym->name, build_id_msg ?: "");
675 goto out_free_filename;
678 pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__,
679 filename, sym->name, map->unmap_ip(map, sym->start),
680 map->unmap_ip(map, sym->end));
682 pr_debug("annotating [%p] %30s : [%p] %30s\n",
683 dso, dso->long_name, sym, sym->name);
685 snprintf(command, sizeof(command),
686 "objdump %s%s --start-address=0x%016" PRIx64
687 " --stop-address=0x%016" PRIx64
688 " -d %s %s -C %s|grep -v %s|expand",
689 disassembler_style ? "-M " : "",
690 disassembler_style ? disassembler_style : "",
691 map__rip_2objdump(map, sym->start),
692 map__rip_2objdump(map, sym->end+1),
693 symbol_conf.annotate_asm_raw ? "" : "--no-show-raw",
694 symbol_conf.annotate_src ? "-S" : "",
695 symfs_filename, filename);
697 pr_debug("Executing: %s\n", command);
699 file = popen(command, "r");
701 goto out_free_filename;
704 if (symbol__parse_objdump_line(sym, map, file, privsize) < 0)
714 static void insert_source_line(struct rb_root *root, struct source_line *src_line)
716 struct source_line *iter;
717 struct rb_node **p = &root->rb_node;
718 struct rb_node *parent = NULL;
722 iter = rb_entry(parent, struct source_line, node);
724 if (src_line->percent > iter->percent)
730 rb_link_node(&src_line->node, parent, p);
731 rb_insert_color(&src_line->node, root);
734 static void symbol__free_source_line(struct symbol *sym, int len)
736 struct annotation *notes = symbol__annotation(sym);
737 struct source_line *src_line = notes->src->lines;
740 for (i = 0; i < len; i++)
741 free(src_line[i].path);
744 notes->src->lines = NULL;
747 /* Get the filename:line for the colored entries */
748 static int symbol__get_source_line(struct symbol *sym, struct map *map,
749 int evidx, struct rb_root *root, int len,
750 const char *filename)
754 char cmd[PATH_MAX * 2];
755 struct source_line *src_line;
756 struct annotation *notes = symbol__annotation(sym);
757 struct sym_hist *h = annotation__histogram(notes, evidx);
762 src_line = notes->src->lines = calloc(len, sizeof(struct source_line));
763 if (!notes->src->lines)
766 start = map__rip_2objdump(map, sym->start);
768 for (i = 0; i < len; i++) {
774 src_line[i].percent = 100.0 * h->addr[i] / h->sum;
775 if (src_line[i].percent <= 0.5)
779 sprintf(cmd, "addr2line -e %s %016" PRIx64, filename, offset);
780 fp = popen(cmd, "r");
784 if (getline(&path, &line_len, fp) < 0 || !line_len)
787 src_line[i].path = malloc(sizeof(char) * line_len + 1);
788 if (!src_line[i].path)
791 strcpy(src_line[i].path, path);
792 insert_source_line(root, &src_line[i]);
801 static void print_summary(struct rb_root *root, const char *filename)
803 struct source_line *src_line;
804 struct rb_node *node;
806 printf("\nSorted summary for file %s\n", filename);
807 printf("----------------------------------------------\n\n");
809 if (RB_EMPTY_ROOT(root)) {
810 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
814 node = rb_first(root);
820 src_line = rb_entry(node, struct source_line, node);
821 percent = src_line->percent;
822 color = get_percent_color(percent);
823 path = src_line->path;
825 color_fprintf(stdout, color, " %7.2f %s", percent, path);
826 node = rb_next(node);
830 static void symbol__annotate_hits(struct symbol *sym, int evidx)
832 struct annotation *notes = symbol__annotation(sym);
833 struct sym_hist *h = annotation__histogram(notes, evidx);
834 u64 len = symbol__size(sym), offset;
836 for (offset = 0; offset < len; ++offset)
837 if (h->addr[offset] != 0)
838 printf("%*" PRIx64 ": %" PRIu64 "\n", BITS_PER_LONG / 2,
839 sym->start + offset, h->addr[offset]);
840 printf("%*s: %" PRIu64 "\n", BITS_PER_LONG / 2, "h->sum", h->sum);
843 int symbol__annotate_printf(struct symbol *sym, struct map *map, int evidx,
844 bool full_paths, int min_pcnt, int max_lines,
847 struct dso *dso = map->dso;
848 const char *filename = dso->long_name, *d_filename;
849 struct annotation *notes = symbol__annotation(sym);
850 struct disasm_line *pos, *queue = NULL;
851 u64 start = map__rip_2objdump(map, sym->start);
852 int printed = 2, queue_len = 0;
857 d_filename = filename;
859 d_filename = basename(filename);
861 len = symbol__size(sym);
863 printf(" Percent | Source code & Disassembly of %s\n", d_filename);
864 printf("------------------------------------------------\n");
867 symbol__annotate_hits(sym, evidx);
869 list_for_each_entry(pos, ¬es->src->source, node) {
870 if (context && queue == NULL) {
875 switch (disasm_line__print(pos, sym, start, evidx, len,
876 min_pcnt, printed, max_lines,
881 printed += queue_len;
887 /* filtered by max_lines */
893 * Filtered by min_pcnt or non IP lines when
898 if (queue_len == context)
899 queue = list_entry(queue->node.next, typeof(*queue), node);
909 void symbol__annotate_zero_histogram(struct symbol *sym, int evidx)
911 struct annotation *notes = symbol__annotation(sym);
912 struct sym_hist *h = annotation__histogram(notes, evidx);
914 memset(h, 0, notes->src->sizeof_sym_hist);
917 void symbol__annotate_decay_histogram(struct symbol *sym, int evidx)
919 struct annotation *notes = symbol__annotation(sym);
920 struct sym_hist *h = annotation__histogram(notes, evidx);
921 int len = symbol__size(sym), offset;
924 for (offset = 0; offset < len; ++offset) {
925 h->addr[offset] = h->addr[offset] * 7 / 8;
926 h->sum += h->addr[offset];
930 void disasm__purge(struct list_head *head)
932 struct disasm_line *pos, *n;
934 list_for_each_entry_safe(pos, n, head, node) {
935 list_del(&pos->node);
936 disasm_line__free(pos);
940 static size_t disasm_line__fprintf(struct disasm_line *dl, FILE *fp)
944 if (dl->offset == -1)
945 return fprintf(fp, "%s\n", dl->line);
947 printed = fprintf(fp, "%#" PRIx64 " %s", dl->offset, dl->name);
949 if (dl->ops.raw[0] != '\0') {
950 printed += fprintf(fp, "%.*s %s\n", 6 - (int)printed, " ",
954 return printed + fprintf(fp, "\n");
957 size_t disasm__fprintf(struct list_head *head, FILE *fp)
959 struct disasm_line *pos;
962 list_for_each_entry(pos, head, node)
963 printed += disasm_line__fprintf(pos, fp);
968 int symbol__tty_annotate(struct symbol *sym, struct map *map, int evidx,
969 bool print_lines, bool full_paths, int min_pcnt,
972 struct dso *dso = map->dso;
973 const char *filename = dso->long_name;
974 struct rb_root source_line = RB_ROOT;
977 if (symbol__annotate(sym, map, 0) < 0)
980 len = symbol__size(sym);
983 symbol__get_source_line(sym, map, evidx, &source_line,
985 print_summary(&source_line, filename);
988 symbol__annotate_printf(sym, map, evidx, full_paths,
989 min_pcnt, max_lines, 0);
991 symbol__free_source_line(sym, len);
993 disasm__purge(&symbol__annotation(sym)->src->source);