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)
18 #include <linux/bitops.h>
20 const char *disassembler_style;
21 const char *objdump_path;
23 static struct ins *ins__find(const char *name);
24 static int disasm_line__parse(char *line, char **namep, char **rawp);
26 static void ins__delete(struct ins_operands *ops)
28 free(ops->source.raw);
29 free(ops->source.name);
30 free(ops->target.raw);
31 free(ops->target.name);
34 static int ins__raw_scnprintf(struct ins *ins, char *bf, size_t size,
35 struct ins_operands *ops)
37 return scnprintf(bf, size, "%-6.6s %s", ins->name, ops->raw);
40 int ins__scnprintf(struct ins *ins, char *bf, size_t size,
41 struct ins_operands *ops)
43 if (ins->ops->scnprintf)
44 return ins->ops->scnprintf(ins, bf, size, ops);
46 return ins__raw_scnprintf(ins, bf, size, ops);
49 static int call__parse(struct ins_operands *ops)
51 char *endptr, *tok, *name;
53 ops->target.addr = strtoull(ops->raw, &endptr, 16);
55 name = strchr(endptr, '<');
61 tok = strchr(name, '>');
66 ops->target.name = strdup(name);
69 return ops->target.name == NULL ? -1 : 0;
72 tok = strchr(endptr, '(');
78 tok = strchr(endptr, '*');
82 ops->target.addr = strtoull(tok + 1, NULL, 16);
86 static int call__scnprintf(struct ins *ins, char *bf, size_t size,
87 struct ins_operands *ops)
90 return scnprintf(bf, size, "%-6.6s %s", ins->name, ops->target.name);
92 if (ops->target.addr == 0)
93 return ins__raw_scnprintf(ins, bf, size, ops);
95 return scnprintf(bf, size, "%-6.6s *%" PRIx64, ins->name, ops->target.addr);
98 static struct ins_ops call_ops = {
100 .scnprintf = call__scnprintf,
103 bool ins__is_call(const struct ins *ins)
105 return ins->ops == &call_ops;
108 static int jump__parse(struct ins_operands *ops)
110 const char *s = strchr(ops->raw, '+');
112 ops->target.addr = strtoll(ops->raw, NULL, 16);
115 ops->target.offset = strtoll(s, NULL, 16);
117 ops->target.offset = UINT64_MAX;
122 static int jump__scnprintf(struct ins *ins, char *bf, size_t size,
123 struct ins_operands *ops)
125 return scnprintf(bf, size, "%-6.6s %" PRIx64, ins->name, ops->target.offset);
128 static struct ins_ops jump_ops = {
129 .parse = jump__parse,
130 .scnprintf = jump__scnprintf,
133 bool ins__is_jump(const struct ins *ins)
135 return ins->ops == &jump_ops;
138 static int comment__symbol(char *raw, char *comment, u64 *addrp, char **namep)
140 char *endptr, *name, *t;
142 if (strstr(raw, "(%rip)") == NULL)
145 *addrp = strtoull(comment, &endptr, 16);
146 name = strchr(endptr, '<');
152 t = strchr(name, '>');
157 *namep = strdup(name);
163 static int lock__parse(struct ins_operands *ops)
167 ops->locked.ops = zalloc(sizeof(*ops->locked.ops));
168 if (ops->locked.ops == NULL)
171 if (disasm_line__parse(ops->raw, &name, &ops->locked.ops->raw) < 0)
174 ops->locked.ins = ins__find(name);
175 if (ops->locked.ins == NULL)
178 if (!ops->locked.ins->ops)
181 if (ops->locked.ins->ops->parse)
182 ops->locked.ins->ops->parse(ops->locked.ops);
187 free(ops->locked.ops);
188 ops->locked.ops = NULL;
192 static int lock__scnprintf(struct ins *ins, char *bf, size_t size,
193 struct ins_operands *ops)
197 if (ops->locked.ins == NULL)
198 return ins__raw_scnprintf(ins, bf, size, ops);
200 printed = scnprintf(bf, size, "%-6.6s ", ins->name);
201 return printed + ins__scnprintf(ops->locked.ins, bf + printed,
202 size - printed, ops->locked.ops);
205 static void lock__delete(struct ins_operands *ops)
207 free(ops->locked.ops);
208 free(ops->target.raw);
209 free(ops->target.name);
212 static struct ins_ops lock_ops = {
213 .free = lock__delete,
214 .parse = lock__parse,
215 .scnprintf = lock__scnprintf,
218 static int mov__parse(struct ins_operands *ops)
220 char *s = strchr(ops->raw, ','), *target, *comment, prev;
226 ops->source.raw = strdup(ops->raw);
229 if (ops->source.raw == NULL)
234 while (s[0] != '\0' && !isspace(s[0]))
239 ops->target.raw = strdup(target);
242 if (ops->target.raw == NULL)
243 goto out_free_source;
245 comment = strchr(s, '#');
249 while (comment[0] != '\0' && isspace(comment[0]))
252 comment__symbol(ops->source.raw, comment, &ops->source.addr, &ops->source.name);
253 comment__symbol(ops->target.raw, comment, &ops->target.addr, &ops->target.name);
258 free(ops->source.raw);
259 ops->source.raw = NULL;
263 static int mov__scnprintf(struct ins *ins, char *bf, size_t size,
264 struct ins_operands *ops)
266 return scnprintf(bf, size, "%-6.6s %s,%s", ins->name,
267 ops->source.name ?: ops->source.raw,
268 ops->target.name ?: ops->target.raw);
271 static struct ins_ops mov_ops = {
273 .scnprintf = mov__scnprintf,
276 static int dec__parse(struct ins_operands *ops)
278 char *target, *comment, *s, prev;
280 target = s = ops->raw;
282 while (s[0] != '\0' && !isspace(s[0]))
287 ops->target.raw = strdup(target);
290 if (ops->target.raw == NULL)
293 comment = strchr(s, '#');
297 while (comment[0] != '\0' && isspace(comment[0]))
300 comment__symbol(ops->target.raw, comment, &ops->target.addr, &ops->target.name);
305 static int dec__scnprintf(struct ins *ins, char *bf, size_t size,
306 struct ins_operands *ops)
308 return scnprintf(bf, size, "%-6.6s %s", ins->name,
309 ops->target.name ?: ops->target.raw);
312 static struct ins_ops dec_ops = {
314 .scnprintf = dec__scnprintf,
317 static int nop__scnprintf(struct ins *ins __maybe_unused, char *bf, size_t size,
318 struct ins_operands *ops __maybe_unused)
320 return scnprintf(bf, size, "%-6.6s", "nop");
323 static struct ins_ops nop_ops = {
324 .scnprintf = nop__scnprintf,
328 * Must be sorted by name!
330 static struct ins instructions[] = {
331 { .name = "add", .ops = &mov_ops, },
332 { .name = "addl", .ops = &mov_ops, },
333 { .name = "addq", .ops = &mov_ops, },
334 { .name = "addw", .ops = &mov_ops, },
335 { .name = "and", .ops = &mov_ops, },
336 { .name = "bts", .ops = &mov_ops, },
337 { .name = "call", .ops = &call_ops, },
338 { .name = "callq", .ops = &call_ops, },
339 { .name = "cmp", .ops = &mov_ops, },
340 { .name = "cmpb", .ops = &mov_ops, },
341 { .name = "cmpl", .ops = &mov_ops, },
342 { .name = "cmpq", .ops = &mov_ops, },
343 { .name = "cmpw", .ops = &mov_ops, },
344 { .name = "cmpxch", .ops = &mov_ops, },
345 { .name = "dec", .ops = &dec_ops, },
346 { .name = "decl", .ops = &dec_ops, },
347 { .name = "imul", .ops = &mov_ops, },
348 { .name = "inc", .ops = &dec_ops, },
349 { .name = "incl", .ops = &dec_ops, },
350 { .name = "ja", .ops = &jump_ops, },
351 { .name = "jae", .ops = &jump_ops, },
352 { .name = "jb", .ops = &jump_ops, },
353 { .name = "jbe", .ops = &jump_ops, },
354 { .name = "jc", .ops = &jump_ops, },
355 { .name = "jcxz", .ops = &jump_ops, },
356 { .name = "je", .ops = &jump_ops, },
357 { .name = "jecxz", .ops = &jump_ops, },
358 { .name = "jg", .ops = &jump_ops, },
359 { .name = "jge", .ops = &jump_ops, },
360 { .name = "jl", .ops = &jump_ops, },
361 { .name = "jle", .ops = &jump_ops, },
362 { .name = "jmp", .ops = &jump_ops, },
363 { .name = "jmpq", .ops = &jump_ops, },
364 { .name = "jna", .ops = &jump_ops, },
365 { .name = "jnae", .ops = &jump_ops, },
366 { .name = "jnb", .ops = &jump_ops, },
367 { .name = "jnbe", .ops = &jump_ops, },
368 { .name = "jnc", .ops = &jump_ops, },
369 { .name = "jne", .ops = &jump_ops, },
370 { .name = "jng", .ops = &jump_ops, },
371 { .name = "jnge", .ops = &jump_ops, },
372 { .name = "jnl", .ops = &jump_ops, },
373 { .name = "jnle", .ops = &jump_ops, },
374 { .name = "jno", .ops = &jump_ops, },
375 { .name = "jnp", .ops = &jump_ops, },
376 { .name = "jns", .ops = &jump_ops, },
377 { .name = "jnz", .ops = &jump_ops, },
378 { .name = "jo", .ops = &jump_ops, },
379 { .name = "jp", .ops = &jump_ops, },
380 { .name = "jpe", .ops = &jump_ops, },
381 { .name = "jpo", .ops = &jump_ops, },
382 { .name = "jrcxz", .ops = &jump_ops, },
383 { .name = "js", .ops = &jump_ops, },
384 { .name = "jz", .ops = &jump_ops, },
385 { .name = "lea", .ops = &mov_ops, },
386 { .name = "lock", .ops = &lock_ops, },
387 { .name = "mov", .ops = &mov_ops, },
388 { .name = "movb", .ops = &mov_ops, },
389 { .name = "movdqa",.ops = &mov_ops, },
390 { .name = "movl", .ops = &mov_ops, },
391 { .name = "movq", .ops = &mov_ops, },
392 { .name = "movslq", .ops = &mov_ops, },
393 { .name = "movzbl", .ops = &mov_ops, },
394 { .name = "movzwl", .ops = &mov_ops, },
395 { .name = "nop", .ops = &nop_ops, },
396 { .name = "nopl", .ops = &nop_ops, },
397 { .name = "nopw", .ops = &nop_ops, },
398 { .name = "or", .ops = &mov_ops, },
399 { .name = "orl", .ops = &mov_ops, },
400 { .name = "test", .ops = &mov_ops, },
401 { .name = "testb", .ops = &mov_ops, },
402 { .name = "testl", .ops = &mov_ops, },
403 { .name = "xadd", .ops = &mov_ops, },
404 { .name = "xbeginl", .ops = &jump_ops, },
405 { .name = "xbeginq", .ops = &jump_ops, },
408 static int ins__cmp(const void *name, const void *insp)
410 const struct ins *ins = insp;
412 return strcmp(name, ins->name);
415 static struct ins *ins__find(const char *name)
417 const int nmemb = ARRAY_SIZE(instructions);
419 return bsearch(name, instructions, nmemb, sizeof(struct ins), ins__cmp);
422 int symbol__annotate_init(struct map *map __maybe_unused, struct symbol *sym)
424 struct annotation *notes = symbol__annotation(sym);
425 pthread_mutex_init(¬es->lock, NULL);
429 int symbol__alloc_hist(struct symbol *sym)
431 struct annotation *notes = symbol__annotation(sym);
432 const size_t size = symbol__size(sym);
433 size_t sizeof_sym_hist;
435 /* Check for overflow when calculating sizeof_sym_hist */
436 if (size > (SIZE_MAX - sizeof(struct sym_hist)) / sizeof(u64))
439 sizeof_sym_hist = (sizeof(struct sym_hist) + size * sizeof(u64));
441 /* Check for overflow in zalloc argument */
442 if (sizeof_sym_hist > (SIZE_MAX - sizeof(*notes->src))
443 / symbol_conf.nr_events)
446 notes->src = zalloc(sizeof(*notes->src) + symbol_conf.nr_events * sizeof_sym_hist);
447 if (notes->src == NULL)
449 notes->src->sizeof_sym_hist = sizeof_sym_hist;
450 notes->src->nr_histograms = symbol_conf.nr_events;
451 INIT_LIST_HEAD(¬es->src->source);
455 void symbol__annotate_zero_histograms(struct symbol *sym)
457 struct annotation *notes = symbol__annotation(sym);
459 pthread_mutex_lock(¬es->lock);
460 if (notes->src != NULL)
461 memset(notes->src->histograms, 0,
462 notes->src->nr_histograms * notes->src->sizeof_sym_hist);
463 pthread_mutex_unlock(¬es->lock);
466 int symbol__inc_addr_samples(struct symbol *sym, struct map *map,
470 struct annotation *notes;
473 notes = symbol__annotation(sym);
474 if (notes->src == NULL)
477 pr_debug3("%s: addr=%#" PRIx64 "\n", __func__, map->unmap_ip(map, addr));
479 if (addr < sym->start || addr > sym->end)
482 offset = addr - sym->start;
483 h = annotation__histogram(notes, evidx);
487 pr_debug3("%#" PRIx64 " %s: period++ [addr: %#" PRIx64 ", %#" PRIx64
488 ", evidx=%d] => %" PRIu64 "\n", sym->start, sym->name,
489 addr, addr - sym->start, evidx, h->addr[offset]);
493 static void disasm_line__init_ins(struct disasm_line *dl)
495 dl->ins = ins__find(dl->name);
503 if (dl->ins->ops->parse)
504 dl->ins->ops->parse(&dl->ops);
507 static int disasm_line__parse(char *line, char **namep, char **rawp)
509 char *name = line, tmp;
511 while (isspace(name[0]))
519 while ((*rawp)[0] != '\0' && !isspace((*rawp)[0]))
524 *namep = strdup(name);
531 if ((*rawp)[0] != '\0') {
533 while (isspace((*rawp)[0]))
545 static struct disasm_line *disasm_line__new(s64 offset, char *line, size_t privsize)
547 struct disasm_line *dl = zalloc(sizeof(*dl) + privsize);
551 dl->line = strdup(line);
552 if (dl->line == NULL)
556 if (disasm_line__parse(dl->line, &dl->name, &dl->ops.raw) < 0)
559 disasm_line__init_ins(dl);
572 void disasm_line__free(struct disasm_line *dl)
576 if (dl->ins && dl->ins->ops->free)
577 dl->ins->ops->free(&dl->ops);
579 ins__delete(&dl->ops);
583 int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size, bool raw)
586 return scnprintf(bf, size, "%-6.6s %s", dl->name, dl->ops.raw);
588 return ins__scnprintf(dl->ins, bf, size, &dl->ops);
591 static void disasm__add(struct list_head *head, struct disasm_line *line)
593 list_add_tail(&line->node, head);
596 struct disasm_line *disasm__get_next_ip_line(struct list_head *head, struct disasm_line *pos)
598 list_for_each_entry_continue(pos, head, node)
599 if (pos->offset >= 0)
605 static int disasm_line__print(struct disasm_line *dl, struct symbol *sym, u64 start,
606 int evidx, u64 len, int min_pcnt, int printed,
607 int max_lines, struct disasm_line *queue)
609 static const char *prev_line;
610 static const char *prev_color;
612 if (dl->offset != -1) {
613 const char *path = NULL;
614 unsigned int hits = 0;
615 double percent = 0.0;
617 struct annotation *notes = symbol__annotation(sym);
618 struct source_line *src_line = notes->src->lines;
619 struct sym_hist *h = annotation__histogram(notes, evidx);
620 s64 offset = dl->offset;
621 const u64 addr = start + offset;
622 struct disasm_line *next;
624 next = disasm__get_next_ip_line(¬es->src->source, dl);
626 while (offset < (s64)len &&
627 (next == NULL || offset < next->offset)) {
630 path = src_line[offset].path;
631 percent += src_line[offset].percent;
633 hits += h->addr[offset];
638 if (src_line == NULL && h->sum)
639 percent = 100.0 * hits / h->sum;
641 if (percent < min_pcnt)
644 if (max_lines && printed >= max_lines)
648 list_for_each_entry_from(queue, ¬es->src->source, node) {
651 disasm_line__print(queue, sym, start, evidx, len,
656 color = get_percent_color(percent);
659 * Also color the filename and line if needed, with
660 * the same color than the percentage. Don't print it
661 * twice for close colored addr with the same filename:line
664 if (!prev_line || strcmp(prev_line, path)
665 || color != prev_color) {
666 color_fprintf(stdout, color, " %s", path);
672 color_fprintf(stdout, color, " %7.2f", percent);
674 color_fprintf(stdout, PERF_COLOR_MAGENTA, " %" PRIx64 ":", addr);
675 color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", dl->line);
676 } else if (max_lines && printed >= max_lines)
685 printf(" : %s\n", dl->line);
691 static int symbol__parse_objdump_line(struct symbol *sym, struct map *map,
692 FILE *file, size_t privsize)
694 struct annotation *notes = symbol__annotation(sym);
695 struct disasm_line *dl;
696 char *line = NULL, *parsed_line, *tmp, *tmp2, *c;
698 s64 line_ip, offset = -1;
700 if (getline(&line, &line_len, file) < 0)
706 while (line_len != 0 && isspace(line[line_len - 1]))
707 line[--line_len] = '\0';
709 c = strchr(line, '\n');
717 * Strip leading spaces:
728 * Parse hexa addresses followed by ':'
730 line_ip = strtoull(tmp, &tmp2, 16);
731 if (*tmp2 != ':' || tmp == tmp2 || tmp2[1] == '\0')
736 u64 start = map__rip_2objdump(map, sym->start),
737 end = map__rip_2objdump(map, sym->end);
739 offset = line_ip - start;
740 if (offset < 0 || (u64)line_ip > end)
743 parsed_line = tmp2 + 1;
746 dl = disasm_line__new(offset, parsed_line, privsize);
752 disasm__add(¬es->src->source, dl);
757 int symbol__annotate(struct symbol *sym, struct map *map, size_t privsize)
759 struct dso *dso = map->dso;
760 char *filename = dso__build_id_filename(dso, NULL, 0);
761 bool free_filename = true;
762 char command[PATH_MAX * 2];
765 char symfs_filename[PATH_MAX];
768 snprintf(symfs_filename, sizeof(symfs_filename), "%s%s",
769 symbol_conf.symfs, filename);
772 if (filename == NULL) {
773 if (dso->has_build_id) {
774 pr_err("Can't annotate %s: not enough memory\n",
779 } else if (readlink(symfs_filename, command, sizeof(command)) < 0 ||
780 strstr(command, "[kernel.kallsyms]") ||
781 access(symfs_filename, R_OK)) {
785 * If we don't have build-ids or the build-id file isn't in the
786 * cache, or is just a kallsyms file, well, lets hope that this
787 * DSO is the same as when 'perf record' ran.
789 filename = dso->long_name;
790 snprintf(symfs_filename, sizeof(symfs_filename), "%s%s",
791 symbol_conf.symfs, filename);
792 free_filename = false;
795 if (dso->symtab_type == DSO_BINARY_TYPE__KALLSYMS) {
796 char bf[BUILD_ID_SIZE * 2 + 16] = " with build id ";
797 char *build_id_msg = NULL;
799 if (dso->annotate_warned)
800 goto out_free_filename;
802 if (dso->has_build_id) {
803 build_id__sprintf(dso->build_id,
804 sizeof(dso->build_id), bf + 15);
808 dso->annotate_warned = 1;
809 pr_err("Can't annotate %s:\n\n"
810 "No vmlinux file%s\nwas found in the path.\n\n"
812 " perf buildid-cache -av vmlinux\n\n"
814 " --vmlinux vmlinux\n",
815 sym->name, build_id_msg ?: "");
816 goto out_free_filename;
819 pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__,
820 filename, sym->name, map->unmap_ip(map, sym->start),
821 map->unmap_ip(map, sym->end));
823 pr_debug("annotating [%p] %30s : [%p] %30s\n",
824 dso, dso->long_name, sym, sym->name);
826 snprintf(command, sizeof(command),
827 "%s %s%s --start-address=0x%016" PRIx64
828 " --stop-address=0x%016" PRIx64
829 " -d %s %s -C %s|grep -v %s|expand",
830 objdump_path ? objdump_path : "objdump",
831 disassembler_style ? "-M " : "",
832 disassembler_style ? disassembler_style : "",
833 map__rip_2objdump(map, sym->start),
834 map__rip_2objdump(map, sym->end+1),
835 symbol_conf.annotate_asm_raw ? "" : "--no-show-raw",
836 symbol_conf.annotate_src ? "-S" : "",
837 symfs_filename, filename);
839 pr_debug("Executing: %s\n", command);
841 file = popen(command, "r");
843 goto out_free_filename;
846 if (symbol__parse_objdump_line(sym, map, file, privsize) < 0)
856 static void insert_source_line(struct rb_root *root, struct source_line *src_line)
858 struct source_line *iter;
859 struct rb_node **p = &root->rb_node;
860 struct rb_node *parent = NULL;
865 iter = rb_entry(parent, struct source_line, node);
867 ret = strcmp(iter->path, src_line->path);
869 iter->percent_sum += src_line->percent;
879 src_line->percent_sum = src_line->percent;
881 rb_link_node(&src_line->node, parent, p);
882 rb_insert_color(&src_line->node, root);
885 static void __resort_source_line(struct rb_root *root, struct source_line *src_line)
887 struct source_line *iter;
888 struct rb_node **p = &root->rb_node;
889 struct rb_node *parent = NULL;
893 iter = rb_entry(parent, struct source_line, node);
895 if (src_line->percent_sum > iter->percent_sum)
901 rb_link_node(&src_line->node, parent, p);
902 rb_insert_color(&src_line->node, root);
905 static void resort_source_line(struct rb_root *dest_root, struct rb_root *src_root)
907 struct source_line *src_line;
908 struct rb_node *node;
910 node = rb_first(src_root);
912 struct rb_node *next;
914 src_line = rb_entry(node, struct source_line, node);
915 next = rb_next(node);
916 rb_erase(node, src_root);
918 __resort_source_line(dest_root, src_line);
923 static void symbol__free_source_line(struct symbol *sym, int len)
925 struct annotation *notes = symbol__annotation(sym);
926 struct source_line *src_line = notes->src->lines;
929 for (i = 0; i < len; i++)
930 free(src_line[i].path);
933 notes->src->lines = NULL;
936 /* Get the filename:line for the colored entries */
937 static int symbol__get_source_line(struct symbol *sym, struct map *map,
938 int evidx, struct rb_root *root, int len,
939 const char *filename)
943 char cmd[PATH_MAX * 2];
944 struct source_line *src_line;
945 struct annotation *notes = symbol__annotation(sym);
946 struct sym_hist *h = annotation__histogram(notes, evidx);
947 struct rb_root tmp_root = RB_ROOT;
952 src_line = notes->src->lines = calloc(len, sizeof(struct source_line));
953 if (!notes->src->lines)
956 start = map__rip_2objdump(map, sym->start);
958 for (i = 0; i < len; i++) {
964 src_line[i].percent = 100.0 * h->addr[i] / h->sum;
965 if (src_line[i].percent <= 0.5)
969 sprintf(cmd, "addr2line -e %s %016" PRIx64, filename, offset);
970 fp = popen(cmd, "r");
974 if (getline(&path, &line_len, fp) < 0 || !line_len)
977 src_line[i].path = malloc(sizeof(char) * line_len + 1);
978 if (!src_line[i].path)
981 strcpy(src_line[i].path, path);
982 insert_source_line(&tmp_root, &src_line[i]);
988 resort_source_line(root, &tmp_root);
992 static void print_summary(struct rb_root *root, const char *filename)
994 struct source_line *src_line;
995 struct rb_node *node;
997 printf("\nSorted summary for file %s\n", filename);
998 printf("----------------------------------------------\n\n");
1000 if (RB_EMPTY_ROOT(root)) {
1001 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
1005 node = rb_first(root);
1011 src_line = rb_entry(node, struct source_line, node);
1012 percent = src_line->percent_sum;
1013 color = get_percent_color(percent);
1014 path = src_line->path;
1016 color_fprintf(stdout, color, " %7.2f %s", percent, path);
1017 node = rb_next(node);
1021 static void symbol__annotate_hits(struct symbol *sym, int evidx)
1023 struct annotation *notes = symbol__annotation(sym);
1024 struct sym_hist *h = annotation__histogram(notes, evidx);
1025 u64 len = symbol__size(sym), offset;
1027 for (offset = 0; offset < len; ++offset)
1028 if (h->addr[offset] != 0)
1029 printf("%*" PRIx64 ": %" PRIu64 "\n", BITS_PER_LONG / 2,
1030 sym->start + offset, h->addr[offset]);
1031 printf("%*s: %" PRIu64 "\n", BITS_PER_LONG / 2, "h->sum", h->sum);
1034 int symbol__annotate_printf(struct symbol *sym, struct map *map, int evidx,
1035 bool full_paths, int min_pcnt, int max_lines,
1038 struct dso *dso = map->dso;
1040 const char *d_filename;
1041 struct annotation *notes = symbol__annotation(sym);
1042 struct disasm_line *pos, *queue = NULL;
1043 u64 start = map__rip_2objdump(map, sym->start);
1044 int printed = 2, queue_len = 0;
1048 filename = strdup(dso->long_name);
1053 d_filename = filename;
1055 d_filename = basename(filename);
1057 len = symbol__size(sym);
1059 printf(" Percent | Source code & Disassembly of %s\n", d_filename);
1060 printf("------------------------------------------------\n");
1063 symbol__annotate_hits(sym, evidx);
1065 list_for_each_entry(pos, ¬es->src->source, node) {
1066 if (context && queue == NULL) {
1071 switch (disasm_line__print(pos, sym, start, evidx, len,
1072 min_pcnt, printed, max_lines,
1077 printed += queue_len;
1083 /* filtered by max_lines */
1089 * Filtered by min_pcnt or non IP lines when
1094 if (queue_len == context)
1095 queue = list_entry(queue->node.next, typeof(*queue), node);
1107 void symbol__annotate_zero_histogram(struct symbol *sym, int evidx)
1109 struct annotation *notes = symbol__annotation(sym);
1110 struct sym_hist *h = annotation__histogram(notes, evidx);
1112 memset(h, 0, notes->src->sizeof_sym_hist);
1115 void symbol__annotate_decay_histogram(struct symbol *sym, int evidx)
1117 struct annotation *notes = symbol__annotation(sym);
1118 struct sym_hist *h = annotation__histogram(notes, evidx);
1119 int len = symbol__size(sym), offset;
1122 for (offset = 0; offset < len; ++offset) {
1123 h->addr[offset] = h->addr[offset] * 7 / 8;
1124 h->sum += h->addr[offset];
1128 void disasm__purge(struct list_head *head)
1130 struct disasm_line *pos, *n;
1132 list_for_each_entry_safe(pos, n, head, node) {
1133 list_del(&pos->node);
1134 disasm_line__free(pos);
1138 static size_t disasm_line__fprintf(struct disasm_line *dl, FILE *fp)
1142 if (dl->offset == -1)
1143 return fprintf(fp, "%s\n", dl->line);
1145 printed = fprintf(fp, "%#" PRIx64 " %s", dl->offset, dl->name);
1147 if (dl->ops.raw[0] != '\0') {
1148 printed += fprintf(fp, "%.*s %s\n", 6 - (int)printed, " ",
1152 return printed + fprintf(fp, "\n");
1155 size_t disasm__fprintf(struct list_head *head, FILE *fp)
1157 struct disasm_line *pos;
1160 list_for_each_entry(pos, head, node)
1161 printed += disasm_line__fprintf(pos, fp);
1166 int symbol__tty_annotate(struct symbol *sym, struct map *map, int evidx,
1167 bool print_lines, bool full_paths, int min_pcnt,
1170 struct dso *dso = map->dso;
1171 const char *filename = dso->long_name;
1172 struct rb_root source_line = RB_ROOT;
1175 if (symbol__annotate(sym, map, 0) < 0)
1178 len = symbol__size(sym);
1181 symbol__get_source_line(sym, map, evidx, &source_line,
1183 print_summary(&source_line, filename);
1186 symbol__annotate_printf(sym, map, evidx, full_paths,
1187 min_pcnt, max_lines, 0);
1189 symbol__free_source_line(sym, len);
1191 disasm__purge(&symbol__annotation(sym)->src->source);