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;
116 static int nop__scnprintf(struct ins *ins __used, char *bf, size_t size,
117 struct ins_operands *ops __used)
119 return scnprintf(bf, size, "%-6.6s", "nop");
122 static struct ins_ops nop_ops = {
123 .scnprintf = nop__scnprintf,
127 * Must be sorted by name!
129 static struct ins instructions[] = {
130 { .name = "call", .ops = &call_ops, },
131 { .name = "callq", .ops = &call_ops, },
132 { .name = "ja", .ops = &jump_ops, },
133 { .name = "jae", .ops = &jump_ops, },
134 { .name = "jb", .ops = &jump_ops, },
135 { .name = "jbe", .ops = &jump_ops, },
136 { .name = "jc", .ops = &jump_ops, },
137 { .name = "jcxz", .ops = &jump_ops, },
138 { .name = "je", .ops = &jump_ops, },
139 { .name = "jecxz", .ops = &jump_ops, },
140 { .name = "jg", .ops = &jump_ops, },
141 { .name = "jge", .ops = &jump_ops, },
142 { .name = "jl", .ops = &jump_ops, },
143 { .name = "jle", .ops = &jump_ops, },
144 { .name = "jmp", .ops = &jump_ops, },
145 { .name = "jmpq", .ops = &jump_ops, },
146 { .name = "jna", .ops = &jump_ops, },
147 { .name = "jnae", .ops = &jump_ops, },
148 { .name = "jnb", .ops = &jump_ops, },
149 { .name = "jnbe", .ops = &jump_ops, },
150 { .name = "jnc", .ops = &jump_ops, },
151 { .name = "jne", .ops = &jump_ops, },
152 { .name = "jng", .ops = &jump_ops, },
153 { .name = "jnge", .ops = &jump_ops, },
154 { .name = "jnl", .ops = &jump_ops, },
155 { .name = "jnle", .ops = &jump_ops, },
156 { .name = "jno", .ops = &jump_ops, },
157 { .name = "jnp", .ops = &jump_ops, },
158 { .name = "jns", .ops = &jump_ops, },
159 { .name = "jnz", .ops = &jump_ops, },
160 { .name = "jo", .ops = &jump_ops, },
161 { .name = "jp", .ops = &jump_ops, },
162 { .name = "jpe", .ops = &jump_ops, },
163 { .name = "jpo", .ops = &jump_ops, },
164 { .name = "jrcxz", .ops = &jump_ops, },
165 { .name = "js", .ops = &jump_ops, },
166 { .name = "jz", .ops = &jump_ops, },
167 { .name = "nop", .ops = &nop_ops, },
168 { .name = "nopl", .ops = &nop_ops, },
169 { .name = "nopw", .ops = &nop_ops, },
172 static int ins__cmp(const void *name, const void *insp)
174 const struct ins *ins = insp;
176 return strcmp(name, ins->name);
179 static struct ins *ins__find(const char *name)
181 const int nmemb = ARRAY_SIZE(instructions);
183 return bsearch(name, instructions, nmemb, sizeof(struct ins), ins__cmp);
186 int symbol__annotate_init(struct map *map __used, struct symbol *sym)
188 struct annotation *notes = symbol__annotation(sym);
189 pthread_mutex_init(¬es->lock, NULL);
193 int symbol__alloc_hist(struct symbol *sym)
195 struct annotation *notes = symbol__annotation(sym);
196 const size_t size = symbol__size(sym);
197 size_t sizeof_sym_hist = (sizeof(struct sym_hist) + size * sizeof(u64));
199 notes->src = zalloc(sizeof(*notes->src) + symbol_conf.nr_events * sizeof_sym_hist);
200 if (notes->src == NULL)
202 notes->src->sizeof_sym_hist = sizeof_sym_hist;
203 notes->src->nr_histograms = symbol_conf.nr_events;
204 INIT_LIST_HEAD(¬es->src->source);
208 void symbol__annotate_zero_histograms(struct symbol *sym)
210 struct annotation *notes = symbol__annotation(sym);
212 pthread_mutex_lock(¬es->lock);
213 if (notes->src != NULL)
214 memset(notes->src->histograms, 0,
215 notes->src->nr_histograms * notes->src->sizeof_sym_hist);
216 pthread_mutex_unlock(¬es->lock);
219 int symbol__inc_addr_samples(struct symbol *sym, struct map *map,
223 struct annotation *notes;
226 notes = symbol__annotation(sym);
227 if (notes->src == NULL)
230 pr_debug3("%s: addr=%#" PRIx64 "\n", __func__, map->unmap_ip(map, addr));
232 if (addr < sym->start || addr > sym->end)
235 offset = addr - sym->start;
236 h = annotation__histogram(notes, evidx);
240 pr_debug3("%#" PRIx64 " %s: period++ [addr: %#" PRIx64 ", %#" PRIx64
241 ", evidx=%d] => %" PRIu64 "\n", sym->start, sym->name,
242 addr, addr - sym->start, evidx, h->addr[offset]);
246 static void disasm_line__init_ins(struct disasm_line *dl)
248 dl->ins = ins__find(dl->name);
256 if (dl->ins->ops->parse)
257 dl->ins->ops->parse(&dl->ops);
260 static struct disasm_line *disasm_line__new(s64 offset, char *line, size_t privsize)
262 struct disasm_line *dl = zalloc(sizeof(*dl) + privsize);
266 dl->line = strdup(line);
267 if (dl->line == NULL)
271 char *name = dl->line, tmp;
273 while (isspace(name[0]))
279 dl->ops.raw = name + 1;
281 while (dl->ops.raw[0] != '\0' &&
282 !isspace(dl->ops.raw[0]))
285 tmp = dl->ops.raw[0];
286 dl->ops.raw[0] = '\0';
287 dl->name = strdup(name);
289 if (dl->name == NULL)
292 dl->ops.raw[0] = tmp;
294 if (dl->ops.raw[0] != '\0') {
296 while (isspace(dl->ops.raw[0]))
300 disasm_line__init_ins(dl);
313 void disasm_line__free(struct disasm_line *dl)
317 free(dl->ops.target.name);
321 int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size, bool raw)
324 return scnprintf(bf, size, "%-6.6s %s", dl->name, dl->ops.raw);
326 return ins__scnprintf(dl->ins, bf, size, &dl->ops);
329 static void disasm__add(struct list_head *head, struct disasm_line *line)
331 list_add_tail(&line->node, head);
334 struct disasm_line *disasm__get_next_ip_line(struct list_head *head, struct disasm_line *pos)
336 list_for_each_entry_continue(pos, head, node)
337 if (pos->offset >= 0)
343 static int disasm_line__print(struct disasm_line *dl, struct symbol *sym, u64 start,
344 int evidx, u64 len, int min_pcnt, int printed,
345 int max_lines, struct disasm_line *queue)
347 static const char *prev_line;
348 static const char *prev_color;
350 if (dl->offset != -1) {
351 const char *path = NULL;
352 unsigned int hits = 0;
353 double percent = 0.0;
355 struct annotation *notes = symbol__annotation(sym);
356 struct source_line *src_line = notes->src->lines;
357 struct sym_hist *h = annotation__histogram(notes, evidx);
358 s64 offset = dl->offset;
359 const u64 addr = start + offset;
360 struct disasm_line *next;
362 next = disasm__get_next_ip_line(¬es->src->source, dl);
364 while (offset < (s64)len &&
365 (next == NULL || offset < next->offset)) {
368 path = src_line[offset].path;
369 percent += src_line[offset].percent;
371 hits += h->addr[offset];
376 if (src_line == NULL && h->sum)
377 percent = 100.0 * hits / h->sum;
379 if (percent < min_pcnt)
382 if (max_lines && printed >= max_lines)
386 list_for_each_entry_from(queue, ¬es->src->source, node) {
389 disasm_line__print(queue, sym, start, evidx, len,
394 color = get_percent_color(percent);
397 * Also color the filename and line if needed, with
398 * the same color than the percentage. Don't print it
399 * twice for close colored addr with the same filename:line
402 if (!prev_line || strcmp(prev_line, path)
403 || color != prev_color) {
404 color_fprintf(stdout, color, " %s", path);
410 color_fprintf(stdout, color, " %7.2f", percent);
412 color_fprintf(stdout, PERF_COLOR_MAGENTA, " %" PRIx64 ":", addr);
413 color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", dl->line);
414 } else if (max_lines && printed >= max_lines)
423 printf(" : %s\n", dl->line);
429 static int symbol__parse_objdump_line(struct symbol *sym, struct map *map,
430 FILE *file, size_t privsize)
432 struct annotation *notes = symbol__annotation(sym);
433 struct disasm_line *dl;
434 char *line = NULL, *parsed_line, *tmp, *tmp2, *c;
436 s64 line_ip, offset = -1;
438 if (getline(&line, &line_len, file) < 0)
444 while (line_len != 0 && isspace(line[line_len - 1]))
445 line[--line_len] = '\0';
447 c = strchr(line, '\n');
455 * Strip leading spaces:
466 * Parse hexa addresses followed by ':'
468 line_ip = strtoull(tmp, &tmp2, 16);
469 if (*tmp2 != ':' || tmp == tmp2 || tmp2[1] == '\0')
474 u64 start = map__rip_2objdump(map, sym->start),
475 end = map__rip_2objdump(map, sym->end);
477 offset = line_ip - start;
478 if (offset < 0 || (u64)line_ip > end)
481 parsed_line = tmp2 + 1;
484 dl = disasm_line__new(offset, parsed_line, privsize);
490 disasm__add(¬es->src->source, dl);
495 int symbol__annotate(struct symbol *sym, struct map *map, size_t privsize)
497 struct dso *dso = map->dso;
498 char *filename = dso__build_id_filename(dso, NULL, 0);
499 bool free_filename = true;
500 char command[PATH_MAX * 2];
503 char symfs_filename[PATH_MAX];
506 snprintf(symfs_filename, sizeof(symfs_filename), "%s%s",
507 symbol_conf.symfs, filename);
510 if (filename == NULL) {
511 if (dso->has_build_id) {
512 pr_err("Can't annotate %s: not enough memory\n",
517 } else if (readlink(symfs_filename, command, sizeof(command)) < 0 ||
518 strstr(command, "[kernel.kallsyms]") ||
519 access(symfs_filename, R_OK)) {
523 * If we don't have build-ids or the build-id file isn't in the
524 * cache, or is just a kallsyms file, well, lets hope that this
525 * DSO is the same as when 'perf record' ran.
527 filename = dso->long_name;
528 snprintf(symfs_filename, sizeof(symfs_filename), "%s%s",
529 symbol_conf.symfs, filename);
530 free_filename = false;
533 if (dso->symtab_type == SYMTAB__KALLSYMS) {
534 char bf[BUILD_ID_SIZE * 2 + 16] = " with build id ";
535 char *build_id_msg = NULL;
537 if (dso->annotate_warned)
538 goto out_free_filename;
540 if (dso->has_build_id) {
541 build_id__sprintf(dso->build_id,
542 sizeof(dso->build_id), bf + 15);
546 dso->annotate_warned = 1;
547 pr_err("Can't annotate %s:\n\n"
548 "No vmlinux file%s\nwas found in the path.\n\n"
550 " perf buildid-cache -av vmlinux\n\n"
552 " --vmlinux vmlinux\n",
553 sym->name, build_id_msg ?: "");
554 goto out_free_filename;
557 pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__,
558 filename, sym->name, map->unmap_ip(map, sym->start),
559 map->unmap_ip(map, sym->end));
561 pr_debug("annotating [%p] %30s : [%p] %30s\n",
562 dso, dso->long_name, sym, sym->name);
564 snprintf(command, sizeof(command),
565 "objdump %s%s --start-address=0x%016" PRIx64
566 " --stop-address=0x%016" PRIx64
567 " -d %s %s -C %s|grep -v %s|expand",
568 disassembler_style ? "-M " : "",
569 disassembler_style ? disassembler_style : "",
570 map__rip_2objdump(map, sym->start),
571 map__rip_2objdump(map, sym->end+1),
572 symbol_conf.annotate_asm_raw ? "" : "--no-show-raw",
573 symbol_conf.annotate_src ? "-S" : "",
574 symfs_filename, filename);
576 pr_debug("Executing: %s\n", command);
578 file = popen(command, "r");
580 goto out_free_filename;
583 if (symbol__parse_objdump_line(sym, map, file, privsize) < 0)
593 static void insert_source_line(struct rb_root *root, struct source_line *src_line)
595 struct source_line *iter;
596 struct rb_node **p = &root->rb_node;
597 struct rb_node *parent = NULL;
601 iter = rb_entry(parent, struct source_line, node);
603 if (src_line->percent > iter->percent)
609 rb_link_node(&src_line->node, parent, p);
610 rb_insert_color(&src_line->node, root);
613 static void symbol__free_source_line(struct symbol *sym, int len)
615 struct annotation *notes = symbol__annotation(sym);
616 struct source_line *src_line = notes->src->lines;
619 for (i = 0; i < len; i++)
620 free(src_line[i].path);
623 notes->src->lines = NULL;
626 /* Get the filename:line for the colored entries */
627 static int symbol__get_source_line(struct symbol *sym, struct map *map,
628 int evidx, struct rb_root *root, int len,
629 const char *filename)
633 char cmd[PATH_MAX * 2];
634 struct source_line *src_line;
635 struct annotation *notes = symbol__annotation(sym);
636 struct sym_hist *h = annotation__histogram(notes, evidx);
641 src_line = notes->src->lines = calloc(len, sizeof(struct source_line));
642 if (!notes->src->lines)
645 start = map__rip_2objdump(map, sym->start);
647 for (i = 0; i < len; i++) {
653 src_line[i].percent = 100.0 * h->addr[i] / h->sum;
654 if (src_line[i].percent <= 0.5)
658 sprintf(cmd, "addr2line -e %s %016" PRIx64, filename, offset);
659 fp = popen(cmd, "r");
663 if (getline(&path, &line_len, fp) < 0 || !line_len)
666 src_line[i].path = malloc(sizeof(char) * line_len + 1);
667 if (!src_line[i].path)
670 strcpy(src_line[i].path, path);
671 insert_source_line(root, &src_line[i]);
680 static void print_summary(struct rb_root *root, const char *filename)
682 struct source_line *src_line;
683 struct rb_node *node;
685 printf("\nSorted summary for file %s\n", filename);
686 printf("----------------------------------------------\n\n");
688 if (RB_EMPTY_ROOT(root)) {
689 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
693 node = rb_first(root);
699 src_line = rb_entry(node, struct source_line, node);
700 percent = src_line->percent;
701 color = get_percent_color(percent);
702 path = src_line->path;
704 color_fprintf(stdout, color, " %7.2f %s", percent, path);
705 node = rb_next(node);
709 static void symbol__annotate_hits(struct symbol *sym, int evidx)
711 struct annotation *notes = symbol__annotation(sym);
712 struct sym_hist *h = annotation__histogram(notes, evidx);
713 u64 len = symbol__size(sym), offset;
715 for (offset = 0; offset < len; ++offset)
716 if (h->addr[offset] != 0)
717 printf("%*" PRIx64 ": %" PRIu64 "\n", BITS_PER_LONG / 2,
718 sym->start + offset, h->addr[offset]);
719 printf("%*s: %" PRIu64 "\n", BITS_PER_LONG / 2, "h->sum", h->sum);
722 int symbol__annotate_printf(struct symbol *sym, struct map *map, int evidx,
723 bool full_paths, int min_pcnt, int max_lines,
726 struct dso *dso = map->dso;
727 const char *filename = dso->long_name, *d_filename;
728 struct annotation *notes = symbol__annotation(sym);
729 struct disasm_line *pos, *queue = NULL;
730 u64 start = map__rip_2objdump(map, sym->start);
731 int printed = 2, queue_len = 0;
736 d_filename = filename;
738 d_filename = basename(filename);
740 len = symbol__size(sym);
742 printf(" Percent | Source code & Disassembly of %s\n", d_filename);
743 printf("------------------------------------------------\n");
746 symbol__annotate_hits(sym, evidx);
748 list_for_each_entry(pos, ¬es->src->source, node) {
749 if (context && queue == NULL) {
754 switch (disasm_line__print(pos, sym, start, evidx, len,
755 min_pcnt, printed, max_lines,
760 printed += queue_len;
766 /* filtered by max_lines */
772 * Filtered by min_pcnt or non IP lines when
777 if (queue_len == context)
778 queue = list_entry(queue->node.next, typeof(*queue), node);
788 void symbol__annotate_zero_histogram(struct symbol *sym, int evidx)
790 struct annotation *notes = symbol__annotation(sym);
791 struct sym_hist *h = annotation__histogram(notes, evidx);
793 memset(h, 0, notes->src->sizeof_sym_hist);
796 void symbol__annotate_decay_histogram(struct symbol *sym, int evidx)
798 struct annotation *notes = symbol__annotation(sym);
799 struct sym_hist *h = annotation__histogram(notes, evidx);
800 int len = symbol__size(sym), offset;
803 for (offset = 0; offset < len; ++offset) {
804 h->addr[offset] = h->addr[offset] * 7 / 8;
805 h->sum += h->addr[offset];
809 void disasm__purge(struct list_head *head)
811 struct disasm_line *pos, *n;
813 list_for_each_entry_safe(pos, n, head, node) {
814 list_del(&pos->node);
815 disasm_line__free(pos);
819 static size_t disasm_line__fprintf(struct disasm_line *dl, FILE *fp)
823 if (dl->offset == -1)
824 return fprintf(fp, "%s\n", dl->line);
826 printed = fprintf(fp, "%#" PRIx64 " %s", dl->offset, dl->name);
828 if (dl->ops.raw[0] != '\0') {
829 printed += fprintf(fp, "%.*s %s\n", 6 - (int)printed, " ",
833 return printed + fprintf(fp, "\n");
836 size_t disasm__fprintf(struct list_head *head, FILE *fp)
838 struct disasm_line *pos;
841 list_for_each_entry(pos, head, node)
842 printed += disasm_line__fprintf(pos, fp);
847 int symbol__tty_annotate(struct symbol *sym, struct map *map, int evidx,
848 bool print_lines, bool full_paths, int min_pcnt,
851 struct dso *dso = map->dso;
852 const char *filename = dso->long_name;
853 struct rb_root source_line = RB_ROOT;
856 if (symbol__annotate(sym, map, 0) < 0)
859 len = symbol__size(sym);
862 symbol__get_source_line(sym, map, evidx, &source_line,
864 print_summary(&source_line, filename);
867 symbol__annotate_printf(sym, map, evidx, full_paths,
868 min_pcnt, max_lines, 0);
870 symbol__free_source_line(sym, len);
872 disasm__purge(&symbol__annotation(sym)->src->source);