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, '*');
63 ops->target.addr = strtoull(tok + 1, NULL, 16);
67 static int call__scnprintf(struct ins *ins, char *bf, size_t size,
68 struct ins_operands *ops)
71 return scnprintf(bf, size, "%-6.6s %s", ins->name, ops->target.name);
73 return scnprintf(bf, size, "%-6.6s *%" PRIx64, ins->name, ops->target.addr);
76 static struct ins_ops call_ops = {
78 .scnprintf = call__scnprintf,
81 bool ins__is_call(const struct ins *ins)
83 return ins->ops == &call_ops;
86 static int jump__parse(struct ins_operands *ops)
88 const char *s = strchr(ops->raw, '+');
90 ops->target.addr = strtoll(ops->raw, NULL, 16);
93 ops->target.offset = strtoll(s, NULL, 16);
95 ops->target.offset = UINT64_MAX;
100 static int jump__scnprintf(struct ins *ins, char *bf, size_t size,
101 struct ins_operands *ops)
103 return scnprintf(bf, size, "%-6.6s %" PRIx64, ins->name, ops->target.offset);
106 static struct ins_ops jump_ops = {
107 .parse = jump__parse,
108 .scnprintf = jump__scnprintf,
111 bool ins__is_jump(const struct ins *ins)
113 return ins->ops == &jump_ops;
117 * Must be sorted by name!
119 static struct ins instructions[] = {
120 { .name = "call", .ops = &call_ops, },
121 { .name = "callq", .ops = &call_ops, },
122 { .name = "ja", .ops = &jump_ops, },
123 { .name = "jae", .ops = &jump_ops, },
124 { .name = "jb", .ops = &jump_ops, },
125 { .name = "jbe", .ops = &jump_ops, },
126 { .name = "jc", .ops = &jump_ops, },
127 { .name = "jcxz", .ops = &jump_ops, },
128 { .name = "je", .ops = &jump_ops, },
129 { .name = "jecxz", .ops = &jump_ops, },
130 { .name = "jg", .ops = &jump_ops, },
131 { .name = "jge", .ops = &jump_ops, },
132 { .name = "jl", .ops = &jump_ops, },
133 { .name = "jle", .ops = &jump_ops, },
134 { .name = "jmp", .ops = &jump_ops, },
135 { .name = "jmpq", .ops = &jump_ops, },
136 { .name = "jna", .ops = &jump_ops, },
137 { .name = "jnae", .ops = &jump_ops, },
138 { .name = "jnb", .ops = &jump_ops, },
139 { .name = "jnbe", .ops = &jump_ops, },
140 { .name = "jnc", .ops = &jump_ops, },
141 { .name = "jne", .ops = &jump_ops, },
142 { .name = "jng", .ops = &jump_ops, },
143 { .name = "jnge", .ops = &jump_ops, },
144 { .name = "jnl", .ops = &jump_ops, },
145 { .name = "jnle", .ops = &jump_ops, },
146 { .name = "jno", .ops = &jump_ops, },
147 { .name = "jnp", .ops = &jump_ops, },
148 { .name = "jns", .ops = &jump_ops, },
149 { .name = "jnz", .ops = &jump_ops, },
150 { .name = "jo", .ops = &jump_ops, },
151 { .name = "jp", .ops = &jump_ops, },
152 { .name = "jpe", .ops = &jump_ops, },
153 { .name = "jpo", .ops = &jump_ops, },
154 { .name = "jrcxz", .ops = &jump_ops, },
155 { .name = "js", .ops = &jump_ops, },
156 { .name = "jz", .ops = &jump_ops, },
159 static int ins__cmp(const void *name, const void *insp)
161 const struct ins *ins = insp;
163 return strcmp(name, ins->name);
166 static struct ins *ins__find(const char *name)
168 const int nmemb = ARRAY_SIZE(instructions);
170 return bsearch(name, instructions, nmemb, sizeof(struct ins), ins__cmp);
173 int symbol__annotate_init(struct map *map __used, struct symbol *sym)
175 struct annotation *notes = symbol__annotation(sym);
176 pthread_mutex_init(¬es->lock, NULL);
180 int symbol__alloc_hist(struct symbol *sym)
182 struct annotation *notes = symbol__annotation(sym);
183 const size_t size = symbol__size(sym);
184 size_t sizeof_sym_hist = (sizeof(struct sym_hist) + size * sizeof(u64));
186 notes->src = zalloc(sizeof(*notes->src) + symbol_conf.nr_events * sizeof_sym_hist);
187 if (notes->src == NULL)
189 notes->src->sizeof_sym_hist = sizeof_sym_hist;
190 notes->src->nr_histograms = symbol_conf.nr_events;
191 INIT_LIST_HEAD(¬es->src->source);
195 void symbol__annotate_zero_histograms(struct symbol *sym)
197 struct annotation *notes = symbol__annotation(sym);
199 pthread_mutex_lock(¬es->lock);
200 if (notes->src != NULL)
201 memset(notes->src->histograms, 0,
202 notes->src->nr_histograms * notes->src->sizeof_sym_hist);
203 pthread_mutex_unlock(¬es->lock);
206 int symbol__inc_addr_samples(struct symbol *sym, struct map *map,
210 struct annotation *notes;
213 notes = symbol__annotation(sym);
214 if (notes->src == NULL)
217 pr_debug3("%s: addr=%#" PRIx64 "\n", __func__, map->unmap_ip(map, addr));
219 if (addr < sym->start || addr > sym->end)
222 offset = addr - sym->start;
223 h = annotation__histogram(notes, evidx);
227 pr_debug3("%#" PRIx64 " %s: period++ [addr: %#" PRIx64 ", %#" PRIx64
228 ", evidx=%d] => %" PRIu64 "\n", sym->start, sym->name,
229 addr, addr - sym->start, evidx, h->addr[offset]);
233 static void disasm_line__init_ins(struct disasm_line *dl)
235 dl->ins = ins__find(dl->name);
243 if (dl->ins->ops->parse)
244 dl->ins->ops->parse(&dl->ops);
247 static struct disasm_line *disasm_line__new(s64 offset, char *line, size_t privsize)
249 struct disasm_line *dl = zalloc(sizeof(*dl) + privsize);
253 dl->line = strdup(line);
254 if (dl->line == NULL)
258 char *name = dl->line, tmp;
260 while (isspace(name[0]))
266 dl->ops.raw = name + 1;
268 while (dl->ops.raw[0] != '\0' &&
269 !isspace(dl->ops.raw[0]))
272 tmp = dl->ops.raw[0];
273 dl->ops.raw[0] = '\0';
274 dl->name = strdup(name);
276 if (dl->name == NULL)
279 dl->ops.raw[0] = tmp;
281 if (dl->ops.raw[0] != '\0') {
283 while (isspace(dl->ops.raw[0]))
287 disasm_line__init_ins(dl);
300 void disasm_line__free(struct disasm_line *dl)
304 free(dl->ops.target.name);
308 int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size, bool raw)
311 return scnprintf(bf, size, "%-6.6s %s", dl->name, dl->ops.raw);
313 return ins__scnprintf(dl->ins, bf, size, &dl->ops);
316 static void disasm__add(struct list_head *head, struct disasm_line *line)
318 list_add_tail(&line->node, head);
321 struct disasm_line *disasm__get_next_ip_line(struct list_head *head, struct disasm_line *pos)
323 list_for_each_entry_continue(pos, head, node)
324 if (pos->offset >= 0)
330 static int disasm_line__print(struct disasm_line *dl, struct symbol *sym, u64 start,
331 int evidx, u64 len, int min_pcnt, int printed,
332 int max_lines, struct disasm_line *queue)
334 static const char *prev_line;
335 static const char *prev_color;
337 if (dl->offset != -1) {
338 const char *path = NULL;
339 unsigned int hits = 0;
340 double percent = 0.0;
342 struct annotation *notes = symbol__annotation(sym);
343 struct source_line *src_line = notes->src->lines;
344 struct sym_hist *h = annotation__histogram(notes, evidx);
345 s64 offset = dl->offset;
346 const u64 addr = start + offset;
347 struct disasm_line *next;
349 next = disasm__get_next_ip_line(¬es->src->source, dl);
351 while (offset < (s64)len &&
352 (next == NULL || offset < next->offset)) {
355 path = src_line[offset].path;
356 percent += src_line[offset].percent;
358 hits += h->addr[offset];
363 if (src_line == NULL && h->sum)
364 percent = 100.0 * hits / h->sum;
366 if (percent < min_pcnt)
369 if (max_lines && printed >= max_lines)
373 list_for_each_entry_from(queue, ¬es->src->source, node) {
376 disasm_line__print(queue, sym, start, evidx, len,
381 color = get_percent_color(percent);
384 * Also color the filename and line if needed, with
385 * the same color than the percentage. Don't print it
386 * twice for close colored addr with the same filename:line
389 if (!prev_line || strcmp(prev_line, path)
390 || color != prev_color) {
391 color_fprintf(stdout, color, " %s", path);
397 color_fprintf(stdout, color, " %7.2f", percent);
399 color_fprintf(stdout, PERF_COLOR_MAGENTA, " %" PRIx64 ":", addr);
400 color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", dl->line);
401 } else if (max_lines && printed >= max_lines)
410 printf(" : %s\n", dl->line);
416 static int symbol__parse_objdump_line(struct symbol *sym, struct map *map,
417 FILE *file, size_t privsize)
419 struct annotation *notes = symbol__annotation(sym);
420 struct disasm_line *dl;
421 char *line = NULL, *parsed_line, *tmp, *tmp2, *c;
423 s64 line_ip, offset = -1;
425 if (getline(&line, &line_len, file) < 0)
431 while (line_len != 0 && isspace(line[line_len - 1]))
432 line[--line_len] = '\0';
434 c = strchr(line, '\n');
442 * Strip leading spaces:
453 * Parse hexa addresses followed by ':'
455 line_ip = strtoull(tmp, &tmp2, 16);
456 if (*tmp2 != ':' || tmp == tmp2 || tmp2[1] == '\0')
461 u64 start = map__rip_2objdump(map, sym->start),
462 end = map__rip_2objdump(map, sym->end);
464 offset = line_ip - start;
465 if (offset < 0 || (u64)line_ip > end)
468 parsed_line = tmp2 + 1;
471 dl = disasm_line__new(offset, parsed_line, privsize);
477 disasm__add(¬es->src->source, dl);
482 int symbol__annotate(struct symbol *sym, struct map *map, size_t privsize)
484 struct dso *dso = map->dso;
485 char *filename = dso__build_id_filename(dso, NULL, 0);
486 bool free_filename = true;
487 char command[PATH_MAX * 2];
490 char symfs_filename[PATH_MAX];
493 snprintf(symfs_filename, sizeof(symfs_filename), "%s%s",
494 symbol_conf.symfs, filename);
497 if (filename == NULL) {
498 if (dso->has_build_id) {
499 pr_err("Can't annotate %s: not enough memory\n",
504 } else if (readlink(symfs_filename, command, sizeof(command)) < 0 ||
505 strstr(command, "[kernel.kallsyms]") ||
506 access(symfs_filename, R_OK)) {
510 * If we don't have build-ids or the build-id file isn't in the
511 * cache, or is just a kallsyms file, well, lets hope that this
512 * DSO is the same as when 'perf record' ran.
514 filename = dso->long_name;
515 snprintf(symfs_filename, sizeof(symfs_filename), "%s%s",
516 symbol_conf.symfs, filename);
517 free_filename = false;
520 if (dso->symtab_type == SYMTAB__KALLSYMS) {
521 char bf[BUILD_ID_SIZE * 2 + 16] = " with build id ";
522 char *build_id_msg = NULL;
524 if (dso->annotate_warned)
525 goto out_free_filename;
527 if (dso->has_build_id) {
528 build_id__sprintf(dso->build_id,
529 sizeof(dso->build_id), bf + 15);
533 dso->annotate_warned = 1;
534 pr_err("Can't annotate %s:\n\n"
535 "No vmlinux file%s\nwas found in the path.\n\n"
537 " perf buildid-cache -av vmlinux\n\n"
539 " --vmlinux vmlinux\n",
540 sym->name, build_id_msg ?: "");
541 goto out_free_filename;
544 pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__,
545 filename, sym->name, map->unmap_ip(map, sym->start),
546 map->unmap_ip(map, sym->end));
548 pr_debug("annotating [%p] %30s : [%p] %30s\n",
549 dso, dso->long_name, sym, sym->name);
551 snprintf(command, sizeof(command),
552 "objdump %s%s --start-address=0x%016" PRIx64
553 " --stop-address=0x%016" PRIx64
554 " -d %s %s -C %s|grep -v %s|expand",
555 disassembler_style ? "-M " : "",
556 disassembler_style ? disassembler_style : "",
557 map__rip_2objdump(map, sym->start),
558 map__rip_2objdump(map, sym->end+1),
559 symbol_conf.annotate_asm_raw ? "" : "--no-show-raw",
560 symbol_conf.annotate_src ? "-S" : "",
561 symfs_filename, filename);
563 pr_debug("Executing: %s\n", command);
565 file = popen(command, "r");
567 goto out_free_filename;
570 if (symbol__parse_objdump_line(sym, map, file, privsize) < 0)
580 static void insert_source_line(struct rb_root *root, struct source_line *src_line)
582 struct source_line *iter;
583 struct rb_node **p = &root->rb_node;
584 struct rb_node *parent = NULL;
588 iter = rb_entry(parent, struct source_line, node);
590 if (src_line->percent > iter->percent)
596 rb_link_node(&src_line->node, parent, p);
597 rb_insert_color(&src_line->node, root);
600 static void symbol__free_source_line(struct symbol *sym, int len)
602 struct annotation *notes = symbol__annotation(sym);
603 struct source_line *src_line = notes->src->lines;
606 for (i = 0; i < len; i++)
607 free(src_line[i].path);
610 notes->src->lines = NULL;
613 /* Get the filename:line for the colored entries */
614 static int symbol__get_source_line(struct symbol *sym, struct map *map,
615 int evidx, struct rb_root *root, int len,
616 const char *filename)
620 char cmd[PATH_MAX * 2];
621 struct source_line *src_line;
622 struct annotation *notes = symbol__annotation(sym);
623 struct sym_hist *h = annotation__histogram(notes, evidx);
628 src_line = notes->src->lines = calloc(len, sizeof(struct source_line));
629 if (!notes->src->lines)
632 start = map__rip_2objdump(map, sym->start);
634 for (i = 0; i < len; i++) {
640 src_line[i].percent = 100.0 * h->addr[i] / h->sum;
641 if (src_line[i].percent <= 0.5)
645 sprintf(cmd, "addr2line -e %s %016" PRIx64, filename, offset);
646 fp = popen(cmd, "r");
650 if (getline(&path, &line_len, fp) < 0 || !line_len)
653 src_line[i].path = malloc(sizeof(char) * line_len + 1);
654 if (!src_line[i].path)
657 strcpy(src_line[i].path, path);
658 insert_source_line(root, &src_line[i]);
667 static void print_summary(struct rb_root *root, const char *filename)
669 struct source_line *src_line;
670 struct rb_node *node;
672 printf("\nSorted summary for file %s\n", filename);
673 printf("----------------------------------------------\n\n");
675 if (RB_EMPTY_ROOT(root)) {
676 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
680 node = rb_first(root);
686 src_line = rb_entry(node, struct source_line, node);
687 percent = src_line->percent;
688 color = get_percent_color(percent);
689 path = src_line->path;
691 color_fprintf(stdout, color, " %7.2f %s", percent, path);
692 node = rb_next(node);
696 static void symbol__annotate_hits(struct symbol *sym, int evidx)
698 struct annotation *notes = symbol__annotation(sym);
699 struct sym_hist *h = annotation__histogram(notes, evidx);
700 u64 len = symbol__size(sym), offset;
702 for (offset = 0; offset < len; ++offset)
703 if (h->addr[offset] != 0)
704 printf("%*" PRIx64 ": %" PRIu64 "\n", BITS_PER_LONG / 2,
705 sym->start + offset, h->addr[offset]);
706 printf("%*s: %" PRIu64 "\n", BITS_PER_LONG / 2, "h->sum", h->sum);
709 int symbol__annotate_printf(struct symbol *sym, struct map *map, int evidx,
710 bool full_paths, int min_pcnt, int max_lines,
713 struct dso *dso = map->dso;
714 const char *filename = dso->long_name, *d_filename;
715 struct annotation *notes = symbol__annotation(sym);
716 struct disasm_line *pos, *queue = NULL;
717 u64 start = map__rip_2objdump(map, sym->start);
718 int printed = 2, queue_len = 0;
723 d_filename = filename;
725 d_filename = basename(filename);
727 len = symbol__size(sym);
729 printf(" Percent | Source code & Disassembly of %s\n", d_filename);
730 printf("------------------------------------------------\n");
733 symbol__annotate_hits(sym, evidx);
735 list_for_each_entry(pos, ¬es->src->source, node) {
736 if (context && queue == NULL) {
741 switch (disasm_line__print(pos, sym, start, evidx, len,
742 min_pcnt, printed, max_lines,
747 printed += queue_len;
753 /* filtered by max_lines */
759 * Filtered by min_pcnt or non IP lines when
764 if (queue_len == context)
765 queue = list_entry(queue->node.next, typeof(*queue), node);
775 void symbol__annotate_zero_histogram(struct symbol *sym, int evidx)
777 struct annotation *notes = symbol__annotation(sym);
778 struct sym_hist *h = annotation__histogram(notes, evidx);
780 memset(h, 0, notes->src->sizeof_sym_hist);
783 void symbol__annotate_decay_histogram(struct symbol *sym, int evidx)
785 struct annotation *notes = symbol__annotation(sym);
786 struct sym_hist *h = annotation__histogram(notes, evidx);
787 int len = symbol__size(sym), offset;
790 for (offset = 0; offset < len; ++offset) {
791 h->addr[offset] = h->addr[offset] * 7 / 8;
792 h->sum += h->addr[offset];
796 void disasm__purge(struct list_head *head)
798 struct disasm_line *pos, *n;
800 list_for_each_entry_safe(pos, n, head, node) {
801 list_del(&pos->node);
802 disasm_line__free(pos);
806 static size_t disasm_line__fprintf(struct disasm_line *dl, FILE *fp)
810 if (dl->offset == -1)
811 return fprintf(fp, "%s\n", dl->line);
813 printed = fprintf(fp, "%#" PRIx64 " %s", dl->offset, dl->name);
815 if (dl->ops.raw[0] != '\0') {
816 printed += fprintf(fp, "%.*s %s\n", 6 - (int)printed, " ",
820 return printed + fprintf(fp, "\n");
823 size_t disasm__fprintf(struct list_head *head, FILE *fp)
825 struct disasm_line *pos;
828 list_for_each_entry(pos, head, node)
829 printed += disasm_line__fprintf(pos, fp);
834 int symbol__tty_annotate(struct symbol *sym, struct map *map, int evidx,
835 bool print_lines, bool full_paths, int min_pcnt,
838 struct dso *dso = map->dso;
839 const char *filename = dso->long_name;
840 struct rb_root source_line = RB_ROOT;
843 if (symbol__annotate(sym, map, 0) < 0)
846 len = symbol__size(sym);
849 symbol__get_source_line(sym, map, evidx, &source_line,
851 print_summary(&source_line, filename);
854 symbol__annotate_printf(sym, map, evidx, full_paths,
855 min_pcnt, max_lines, 0);
857 symbol__free_source_line(sym, len);
859 disasm__purge(&symbol__annotation(sym)->src->source);