Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com> |
| 3 | * |
| 4 | * Parts came from builtin-annotate.c, see those files for further |
| 5 | * copyright notes. |
| 6 | * |
| 7 | * Released under the GPL v2. (and only v2, not any later version) |
| 8 | */ |
| 9 | |
| 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" |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 17 | #include <pthread.h> |
| 18 | |
Andi Kleen | f69b64f | 2011-09-15 14:31:41 -0700 | [diff] [blame] | 19 | const char *disassembler_style; |
| 20 | |
Arnaldo Carvalho de Melo | c7e6ead | 2012-04-20 14:38:46 -0300 | [diff] [blame^] | 21 | static int call__parse(struct ins_operands *ops) |
Arnaldo Carvalho de Melo | d86b059 | 2012-04-18 16:07:38 -0300 | [diff] [blame] | 22 | { |
Arnaldo Carvalho de Melo | c7e6ead | 2012-04-20 14:38:46 -0300 | [diff] [blame^] | 23 | ops->target = strtoull(ops->raw, NULL, 16); |
Arnaldo Carvalho de Melo | d86b059 | 2012-04-18 16:07:38 -0300 | [diff] [blame] | 24 | return 0; |
| 25 | } |
| 26 | |
| 27 | static struct ins_ops call_ops = { |
Arnaldo Carvalho de Melo | c7e6ead | 2012-04-20 14:38:46 -0300 | [diff] [blame^] | 28 | .parse = call__parse, |
Arnaldo Carvalho de Melo | d86b059 | 2012-04-18 16:07:38 -0300 | [diff] [blame] | 29 | }; |
| 30 | |
| 31 | bool ins__is_call(const struct ins *ins) |
| 32 | { |
| 33 | return ins->ops == &call_ops; |
| 34 | } |
| 35 | |
Arnaldo Carvalho de Melo | c7e6ead | 2012-04-20 14:38:46 -0300 | [diff] [blame^] | 36 | static int jump__parse(struct ins_operands *ops) |
Arnaldo Carvalho de Melo | 4f9d032 | 2012-04-18 13:58:34 -0300 | [diff] [blame] | 37 | { |
Arnaldo Carvalho de Melo | c7e6ead | 2012-04-20 14:38:46 -0300 | [diff] [blame^] | 38 | const char *s = strchr(ops->raw, '+'); |
Arnaldo Carvalho de Melo | 4f9d032 | 2012-04-18 13:58:34 -0300 | [diff] [blame] | 39 | |
| 40 | if (s++ == NULL) |
| 41 | return -1; |
| 42 | |
Arnaldo Carvalho de Melo | c7e6ead | 2012-04-20 14:38:46 -0300 | [diff] [blame^] | 43 | ops->target = strtoll(s, NULL, 16); |
Arnaldo Carvalho de Melo | 4f9d032 | 2012-04-18 13:58:34 -0300 | [diff] [blame] | 44 | return 0; |
| 45 | } |
| 46 | |
Arnaldo Carvalho de Melo | c7e6ead | 2012-04-20 14:38:46 -0300 | [diff] [blame^] | 47 | static int jump__scnprintf(struct ins *ins, char *bf, size_t size, |
| 48 | struct ins_operands *ops, bool addrs) |
Arnaldo Carvalho de Melo | 28548d7 | 2012-04-19 10:16:27 -0300 | [diff] [blame] | 49 | { |
Arnaldo Carvalho de Melo | c7e6ead | 2012-04-20 14:38:46 -0300 | [diff] [blame^] | 50 | if (addrs) |
| 51 | return scnprintf(bf, size, "%-6.6s %s", ins->name, ops->raw); |
Arnaldo Carvalho de Melo | 28548d7 | 2012-04-19 10:16:27 -0300 | [diff] [blame] | 52 | |
Arnaldo Carvalho de Melo | c7e6ead | 2012-04-20 14:38:46 -0300 | [diff] [blame^] | 53 | return scnprintf(bf, size, "%-6.6s %" PRIx64, ins->name, ops->target); |
Arnaldo Carvalho de Melo | 28548d7 | 2012-04-19 10:16:27 -0300 | [diff] [blame] | 54 | } |
| 55 | |
Arnaldo Carvalho de Melo | 4f9d032 | 2012-04-18 13:58:34 -0300 | [diff] [blame] | 56 | static struct ins_ops jump_ops = { |
Arnaldo Carvalho de Melo | c7e6ead | 2012-04-20 14:38:46 -0300 | [diff] [blame^] | 57 | .parse = jump__parse, |
| 58 | .scnprintf = jump__scnprintf, |
Arnaldo Carvalho de Melo | 4f9d032 | 2012-04-18 13:58:34 -0300 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | bool ins__is_jump(const struct ins *ins) |
| 62 | { |
| 63 | return ins->ops == &jump_ops; |
| 64 | } |
| 65 | |
Arnaldo Carvalho de Melo | 4f9d032 | 2012-04-18 13:58:34 -0300 | [diff] [blame] | 66 | /* |
| 67 | * Must be sorted by name! |
| 68 | */ |
| 69 | static struct ins instructions[] = { |
Arnaldo Carvalho de Melo | d86b059 | 2012-04-18 16:07:38 -0300 | [diff] [blame] | 70 | { .name = "call", .ops = &call_ops, }, |
| 71 | { .name = "callq", .ops = &call_ops, }, |
Arnaldo Carvalho de Melo | 4f9d032 | 2012-04-18 13:58:34 -0300 | [diff] [blame] | 72 | { .name = "ja", .ops = &jump_ops, }, |
Arnaldo Carvalho de Melo | 3f862fd | 2012-04-19 17:10:12 -0300 | [diff] [blame] | 73 | { .name = "jae", .ops = &jump_ops, }, |
| 74 | { .name = "jb", .ops = &jump_ops, }, |
| 75 | { .name = "jbe", .ops = &jump_ops, }, |
| 76 | { .name = "jc", .ops = &jump_ops, }, |
| 77 | { .name = "jcxz", .ops = &jump_ops, }, |
Arnaldo Carvalho de Melo | 4f9d032 | 2012-04-18 13:58:34 -0300 | [diff] [blame] | 78 | { .name = "je", .ops = &jump_ops, }, |
Arnaldo Carvalho de Melo | 3f862fd | 2012-04-19 17:10:12 -0300 | [diff] [blame] | 79 | { .name = "jecxz", .ops = &jump_ops, }, |
| 80 | { .name = "jg", .ops = &jump_ops, }, |
| 81 | { .name = "jge", .ops = &jump_ops, }, |
| 82 | { .name = "jl", .ops = &jump_ops, }, |
| 83 | { .name = "jle", .ops = &jump_ops, }, |
Arnaldo Carvalho de Melo | 4f9d032 | 2012-04-18 13:58:34 -0300 | [diff] [blame] | 84 | { .name = "jmp", .ops = &jump_ops, }, |
| 85 | { .name = "jmpq", .ops = &jump_ops, }, |
Arnaldo Carvalho de Melo | 3f862fd | 2012-04-19 17:10:12 -0300 | [diff] [blame] | 86 | { .name = "jna", .ops = &jump_ops, }, |
| 87 | { .name = "jnae", .ops = &jump_ops, }, |
| 88 | { .name = "jnb", .ops = &jump_ops, }, |
| 89 | { .name = "jnbe", .ops = &jump_ops, }, |
| 90 | { .name = "jnc", .ops = &jump_ops, }, |
Arnaldo Carvalho de Melo | 4f9d032 | 2012-04-18 13:58:34 -0300 | [diff] [blame] | 91 | { .name = "jne", .ops = &jump_ops, }, |
Arnaldo Carvalho de Melo | 3f862fd | 2012-04-19 17:10:12 -0300 | [diff] [blame] | 92 | { .name = "jng", .ops = &jump_ops, }, |
| 93 | { .name = "jnge", .ops = &jump_ops, }, |
| 94 | { .name = "jnl", .ops = &jump_ops, }, |
| 95 | { .name = "jnle", .ops = &jump_ops, }, |
| 96 | { .name = "jno", .ops = &jump_ops, }, |
| 97 | { .name = "jnp", .ops = &jump_ops, }, |
| 98 | { .name = "jns", .ops = &jump_ops, }, |
| 99 | { .name = "jnz", .ops = &jump_ops, }, |
| 100 | { .name = "jo", .ops = &jump_ops, }, |
| 101 | { .name = "jp", .ops = &jump_ops, }, |
| 102 | { .name = "jpe", .ops = &jump_ops, }, |
| 103 | { .name = "jpo", .ops = &jump_ops, }, |
| 104 | { .name = "jrcxz", .ops = &jump_ops, }, |
Arnaldo Carvalho de Melo | 4f9d032 | 2012-04-18 13:58:34 -0300 | [diff] [blame] | 105 | { .name = "js", .ops = &jump_ops, }, |
Arnaldo Carvalho de Melo | 3f862fd | 2012-04-19 17:10:12 -0300 | [diff] [blame] | 106 | { .name = "jz", .ops = &jump_ops, }, |
Arnaldo Carvalho de Melo | 4f9d032 | 2012-04-18 13:58:34 -0300 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | static int ins__cmp(const void *name, const void *insp) |
| 110 | { |
| 111 | const struct ins *ins = insp; |
| 112 | |
| 113 | return strcmp(name, ins->name); |
| 114 | } |
| 115 | |
| 116 | static struct ins *ins__find(const char *name) |
| 117 | { |
| 118 | const int nmemb = ARRAY_SIZE(instructions); |
| 119 | |
| 120 | return bsearch(name, instructions, nmemb, sizeof(struct ins), ins__cmp); |
| 121 | } |
| 122 | |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 123 | int symbol__annotate_init(struct map *map __used, struct symbol *sym) |
| 124 | { |
| 125 | struct annotation *notes = symbol__annotation(sym); |
| 126 | pthread_mutex_init(¬es->lock, NULL); |
| 127 | return 0; |
| 128 | } |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 129 | |
Arnaldo Carvalho de Melo | d04b35f | 2011-11-11 22:17:32 -0200 | [diff] [blame] | 130 | int symbol__alloc_hist(struct symbol *sym) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 131 | { |
| 132 | struct annotation *notes = symbol__annotation(sym); |
Arnaldo Carvalho de Melo | 1b2e2df | 2012-04-19 10:57:06 -0300 | [diff] [blame] | 133 | const size_t size = symbol__size(sym); |
Arnaldo Carvalho de Melo | 64c17be | 2012-03-28 12:49:35 -0300 | [diff] [blame] | 134 | size_t sizeof_sym_hist = (sizeof(struct sym_hist) + size * sizeof(u64)); |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 135 | |
Arnaldo Carvalho de Melo | d04b35f | 2011-11-11 22:17:32 -0200 | [diff] [blame] | 136 | notes->src = zalloc(sizeof(*notes->src) + symbol_conf.nr_events * sizeof_sym_hist); |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 137 | if (notes->src == NULL) |
| 138 | return -1; |
| 139 | notes->src->sizeof_sym_hist = sizeof_sym_hist; |
Arnaldo Carvalho de Melo | d04b35f | 2011-11-11 22:17:32 -0200 | [diff] [blame] | 140 | notes->src->nr_histograms = symbol_conf.nr_events; |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 141 | INIT_LIST_HEAD(¬es->src->source); |
| 142 | return 0; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 143 | } |
| 144 | |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 145 | void symbol__annotate_zero_histograms(struct symbol *sym) |
| 146 | { |
| 147 | struct annotation *notes = symbol__annotation(sym); |
| 148 | |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 149 | pthread_mutex_lock(¬es->lock); |
| 150 | if (notes->src != NULL) |
| 151 | memset(notes->src->histograms, 0, |
| 152 | notes->src->nr_histograms * notes->src->sizeof_sym_hist); |
| 153 | pthread_mutex_unlock(¬es->lock); |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 154 | } |
| 155 | |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame] | 156 | int symbol__inc_addr_samples(struct symbol *sym, struct map *map, |
| 157 | int evidx, u64 addr) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 158 | { |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame] | 159 | unsigned offset; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 160 | struct annotation *notes; |
| 161 | struct sym_hist *h; |
| 162 | |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 163 | notes = symbol__annotation(sym); |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 164 | if (notes->src == NULL) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 165 | return -ENOMEM; |
| 166 | |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 167 | pr_debug3("%s: addr=%#" PRIx64 "\n", __func__, map->unmap_ip(map, addr)); |
| 168 | |
Arnaldo Carvalho de Melo | 31d68e7 | 2012-03-27 12:55:57 -0300 | [diff] [blame] | 169 | if (addr < sym->start || addr > sym->end) |
| 170 | return -ERANGE; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 171 | |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame] | 172 | offset = addr - sym->start; |
| 173 | h = annotation__histogram(notes, evidx); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 174 | h->sum++; |
| 175 | h->addr[offset]++; |
| 176 | |
| 177 | pr_debug3("%#" PRIx64 " %s: period++ [addr: %#" PRIx64 ", %#" PRIx64 |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame] | 178 | ", evidx=%d] => %" PRIu64 "\n", sym->start, sym->name, |
| 179 | addr, addr - sym->start, evidx, h->addr[offset]); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 180 | return 0; |
| 181 | } |
| 182 | |
Arnaldo Carvalho de Melo | 4f9d032 | 2012-04-18 13:58:34 -0300 | [diff] [blame] | 183 | static void disasm_line__init_ins(struct disasm_line *dl) |
| 184 | { |
| 185 | dl->ins = ins__find(dl->name); |
| 186 | |
| 187 | if (dl->ins == NULL) |
| 188 | return; |
| 189 | |
| 190 | if (!dl->ins->ops) |
| 191 | return; |
| 192 | |
Arnaldo Carvalho de Melo | c7e6ead | 2012-04-20 14:38:46 -0300 | [diff] [blame^] | 193 | if (dl->ins->ops->parse) |
| 194 | dl->ins->ops->parse(&dl->ops); |
Arnaldo Carvalho de Melo | 4f9d032 | 2012-04-18 13:58:34 -0300 | [diff] [blame] | 195 | } |
| 196 | |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 197 | static struct disasm_line *disasm_line__new(s64 offset, char *line, size_t privsize) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 198 | { |
Arnaldo Carvalho de Melo | 5145418 | 2012-04-15 15:52:18 -0300 | [diff] [blame] | 199 | struct disasm_line *dl = zalloc(sizeof(*dl) + privsize); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 200 | |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 201 | if (dl != NULL) { |
| 202 | dl->offset = offset; |
| 203 | dl->line = strdup(line); |
| 204 | if (dl->line == NULL) |
Arnaldo Carvalho de Melo | 058b4cc | 2012-04-02 12:59:01 -0300 | [diff] [blame] | 205 | goto out_delete; |
Arnaldo Carvalho de Melo | 5145418 | 2012-04-15 15:52:18 -0300 | [diff] [blame] | 206 | |
| 207 | if (offset != -1) { |
| 208 | char *name = dl->line, tmp; |
| 209 | |
| 210 | while (isspace(name[0])) |
| 211 | ++name; |
| 212 | |
| 213 | if (name[0] == '\0') |
| 214 | goto out_delete; |
| 215 | |
Arnaldo Carvalho de Melo | c7e6ead | 2012-04-20 14:38:46 -0300 | [diff] [blame^] | 216 | dl->ops.raw = name + 1; |
Arnaldo Carvalho de Melo | 5145418 | 2012-04-15 15:52:18 -0300 | [diff] [blame] | 217 | |
Arnaldo Carvalho de Melo | c7e6ead | 2012-04-20 14:38:46 -0300 | [diff] [blame^] | 218 | while (dl->ops.raw[0] != '\0' && |
| 219 | !isspace(dl->ops.raw[0])) |
| 220 | ++dl->ops.raw; |
Arnaldo Carvalho de Melo | 5145418 | 2012-04-15 15:52:18 -0300 | [diff] [blame] | 221 | |
Arnaldo Carvalho de Melo | c7e6ead | 2012-04-20 14:38:46 -0300 | [diff] [blame^] | 222 | tmp = dl->ops.raw[0]; |
| 223 | dl->ops.raw[0] = '\0'; |
Arnaldo Carvalho de Melo | 5145418 | 2012-04-15 15:52:18 -0300 | [diff] [blame] | 224 | dl->name = strdup(name); |
| 225 | |
| 226 | if (dl->name == NULL) |
| 227 | goto out_free_line; |
| 228 | |
Arnaldo Carvalho de Melo | c7e6ead | 2012-04-20 14:38:46 -0300 | [diff] [blame^] | 229 | dl->ops.raw[0] = tmp; |
Arnaldo Carvalho de Melo | 5145418 | 2012-04-15 15:52:18 -0300 | [diff] [blame] | 230 | |
Arnaldo Carvalho de Melo | c7e6ead | 2012-04-20 14:38:46 -0300 | [diff] [blame^] | 231 | if (dl->ops.raw[0] != '\0') { |
| 232 | dl->ops.raw++; |
| 233 | while (isspace(dl->ops.raw[0])) |
| 234 | ++dl->ops.raw; |
Arnaldo Carvalho de Melo | 5145418 | 2012-04-15 15:52:18 -0300 | [diff] [blame] | 235 | } |
Arnaldo Carvalho de Melo | 4f9d032 | 2012-04-18 13:58:34 -0300 | [diff] [blame] | 236 | |
| 237 | disasm_line__init_ins(dl); |
Arnaldo Carvalho de Melo | 5145418 | 2012-04-15 15:52:18 -0300 | [diff] [blame] | 238 | } |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 239 | } |
| 240 | |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 241 | return dl; |
Arnaldo Carvalho de Melo | 5145418 | 2012-04-15 15:52:18 -0300 | [diff] [blame] | 242 | |
| 243 | out_free_line: |
| 244 | free(dl->line); |
Arnaldo Carvalho de Melo | 058b4cc | 2012-04-02 12:59:01 -0300 | [diff] [blame] | 245 | out_delete: |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 246 | free(dl); |
Arnaldo Carvalho de Melo | 058b4cc | 2012-04-02 12:59:01 -0300 | [diff] [blame] | 247 | return NULL; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 248 | } |
| 249 | |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 250 | void disasm_line__free(struct disasm_line *dl) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 251 | { |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 252 | free(dl->line); |
Arnaldo Carvalho de Melo | 5145418 | 2012-04-15 15:52:18 -0300 | [diff] [blame] | 253 | free(dl->name); |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 254 | free(dl); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 255 | } |
| 256 | |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 257 | static void disasm__add(struct list_head *head, struct disasm_line *line) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 258 | { |
| 259 | list_add_tail(&line->node, head); |
| 260 | } |
| 261 | |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 262 | struct disasm_line *disasm__get_next_ip_line(struct list_head *head, struct disasm_line *pos) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 263 | { |
| 264 | list_for_each_entry_continue(pos, head, node) |
| 265 | if (pos->offset >= 0) |
| 266 | return pos; |
| 267 | |
| 268 | return NULL; |
| 269 | } |
| 270 | |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 271 | static int disasm_line__print(struct disasm_line *dl, struct symbol *sym, u64 start, |
| 272 | int evidx, u64 len, int min_pcnt, int printed, |
| 273 | int max_lines, struct disasm_line *queue) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 274 | { |
| 275 | static const char *prev_line; |
| 276 | static const char *prev_color; |
| 277 | |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 278 | if (dl->offset != -1) { |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 279 | const char *path = NULL; |
| 280 | unsigned int hits = 0; |
| 281 | double percent = 0.0; |
| 282 | const char *color; |
| 283 | struct annotation *notes = symbol__annotation(sym); |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 284 | struct source_line *src_line = notes->src->lines; |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame] | 285 | struct sym_hist *h = annotation__histogram(notes, evidx); |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 286 | s64 offset = dl->offset; |
Arnaldo Carvalho de Melo | 058b4cc | 2012-04-02 12:59:01 -0300 | [diff] [blame] | 287 | const u64 addr = start + offset; |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 288 | struct disasm_line *next; |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 289 | |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 290 | next = disasm__get_next_ip_line(¬es->src->source, dl); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 291 | |
| 292 | while (offset < (s64)len && |
| 293 | (next == NULL || offset < next->offset)) { |
| 294 | if (src_line) { |
| 295 | if (path == NULL) |
| 296 | path = src_line[offset].path; |
| 297 | percent += src_line[offset].percent; |
| 298 | } else |
| 299 | hits += h->addr[offset]; |
| 300 | |
| 301 | ++offset; |
| 302 | } |
| 303 | |
| 304 | if (src_line == NULL && h->sum) |
| 305 | percent = 100.0 * hits / h->sum; |
| 306 | |
Arnaldo Carvalho de Melo | d040bd3 | 2011-02-05 15:37:31 -0200 | [diff] [blame] | 307 | if (percent < min_pcnt) |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 308 | return -1; |
| 309 | |
Arnaldo Carvalho de Melo | e3087b8 | 2011-02-08 15:01:39 -0200 | [diff] [blame] | 310 | if (max_lines && printed >= max_lines) |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 311 | return 1; |
Arnaldo Carvalho de Melo | d040bd3 | 2011-02-05 15:37:31 -0200 | [diff] [blame] | 312 | |
Arnaldo Carvalho de Melo | d5e3d74 | 2011-02-08 15:29:25 -0200 | [diff] [blame] | 313 | if (queue != NULL) { |
| 314 | list_for_each_entry_from(queue, ¬es->src->source, node) { |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 315 | if (queue == dl) |
Arnaldo Carvalho de Melo | d5e3d74 | 2011-02-08 15:29:25 -0200 | [diff] [blame] | 316 | break; |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 317 | disasm_line__print(queue, sym, start, evidx, len, |
Arnaldo Carvalho de Melo | d5e3d74 | 2011-02-08 15:29:25 -0200 | [diff] [blame] | 318 | 0, 0, 1, NULL); |
| 319 | } |
| 320 | } |
| 321 | |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 322 | color = get_percent_color(percent); |
| 323 | |
| 324 | /* |
| 325 | * Also color the filename and line if needed, with |
| 326 | * the same color than the percentage. Don't print it |
| 327 | * twice for close colored addr with the same filename:line |
| 328 | */ |
| 329 | if (path) { |
| 330 | if (!prev_line || strcmp(prev_line, path) |
| 331 | || color != prev_color) { |
| 332 | color_fprintf(stdout, color, " %s", path); |
| 333 | prev_line = path; |
| 334 | prev_color = color; |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | color_fprintf(stdout, color, " %7.2f", percent); |
| 339 | printf(" : "); |
Arnaldo Carvalho de Melo | 058b4cc | 2012-04-02 12:59:01 -0300 | [diff] [blame] | 340 | color_fprintf(stdout, PERF_COLOR_MAGENTA, " %" PRIx64 ":", addr); |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 341 | color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", dl->line); |
Arnaldo Carvalho de Melo | e3087b8 | 2011-02-08 15:01:39 -0200 | [diff] [blame] | 342 | } else if (max_lines && printed >= max_lines) |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 343 | return 1; |
| 344 | else { |
Arnaldo Carvalho de Melo | d5e3d74 | 2011-02-08 15:29:25 -0200 | [diff] [blame] | 345 | if (queue) |
| 346 | return -1; |
| 347 | |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 348 | if (!*dl->line) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 349 | printf(" :\n"); |
| 350 | else |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 351 | printf(" : %s\n", dl->line); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 352 | } |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 353 | |
| 354 | return 0; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 355 | } |
| 356 | |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 357 | static int symbol__parse_objdump_line(struct symbol *sym, struct map *map, |
| 358 | FILE *file, size_t privsize) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 359 | { |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 360 | struct annotation *notes = symbol__annotation(sym); |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 361 | struct disasm_line *dl; |
Arnaldo Carvalho de Melo | 058b4cc | 2012-04-02 12:59:01 -0300 | [diff] [blame] | 362 | char *line = NULL, *parsed_line, *tmp, *tmp2, *c; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 363 | size_t line_len; |
| 364 | s64 line_ip, offset = -1; |
| 365 | |
| 366 | if (getline(&line, &line_len, file) < 0) |
| 367 | return -1; |
| 368 | |
| 369 | if (!line) |
| 370 | return -1; |
| 371 | |
| 372 | while (line_len != 0 && isspace(line[line_len - 1])) |
| 373 | line[--line_len] = '\0'; |
| 374 | |
| 375 | c = strchr(line, '\n'); |
| 376 | if (c) |
| 377 | *c = 0; |
| 378 | |
| 379 | line_ip = -1; |
Namhyung Kim | a31b7cc | 2012-04-11 17:04:59 -0300 | [diff] [blame] | 380 | parsed_line = line; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 381 | |
| 382 | /* |
| 383 | * Strip leading spaces: |
| 384 | */ |
| 385 | tmp = line; |
| 386 | while (*tmp) { |
| 387 | if (*tmp != ' ') |
| 388 | break; |
| 389 | tmp++; |
| 390 | } |
| 391 | |
| 392 | if (*tmp) { |
| 393 | /* |
| 394 | * Parse hexa addresses followed by ':' |
| 395 | */ |
| 396 | line_ip = strtoull(tmp, &tmp2, 16); |
| 397 | if (*tmp2 != ':' || tmp == tmp2 || tmp2[1] == '\0') |
| 398 | line_ip = -1; |
| 399 | } |
| 400 | |
| 401 | if (line_ip != -1) { |
| 402 | u64 start = map__rip_2objdump(map, sym->start), |
| 403 | end = map__rip_2objdump(map, sym->end); |
| 404 | |
| 405 | offset = line_ip - start; |
| 406 | if (offset < 0 || (u64)line_ip > end) |
| 407 | offset = -1; |
Arnaldo Carvalho de Melo | 058b4cc | 2012-04-02 12:59:01 -0300 | [diff] [blame] | 408 | else |
| 409 | parsed_line = tmp2 + 1; |
Namhyung Kim | a31b7cc | 2012-04-11 17:04:59 -0300 | [diff] [blame] | 410 | } |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 411 | |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 412 | dl = disasm_line__new(offset, parsed_line, privsize); |
Arnaldo Carvalho de Melo | 058b4cc | 2012-04-02 12:59:01 -0300 | [diff] [blame] | 413 | free(line); |
| 414 | |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 415 | if (dl == NULL) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 416 | return -1; |
Arnaldo Carvalho de Melo | 058b4cc | 2012-04-02 12:59:01 -0300 | [diff] [blame] | 417 | |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 418 | disasm__add(¬es->src->source, dl); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 419 | |
| 420 | return 0; |
| 421 | } |
| 422 | |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 423 | int symbol__annotate(struct symbol *sym, struct map *map, size_t privsize) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 424 | { |
| 425 | struct dso *dso = map->dso; |
| 426 | char *filename = dso__build_id_filename(dso, NULL, 0); |
| 427 | bool free_filename = true; |
| 428 | char command[PATH_MAX * 2]; |
| 429 | FILE *file; |
| 430 | int err = 0; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 431 | char symfs_filename[PATH_MAX]; |
| 432 | |
| 433 | if (filename) { |
| 434 | snprintf(symfs_filename, sizeof(symfs_filename), "%s%s", |
| 435 | symbol_conf.symfs, filename); |
| 436 | } |
| 437 | |
| 438 | if (filename == NULL) { |
| 439 | if (dso->has_build_id) { |
| 440 | pr_err("Can't annotate %s: not enough memory\n", |
| 441 | sym->name); |
| 442 | return -ENOMEM; |
| 443 | } |
| 444 | goto fallback; |
| 445 | } else if (readlink(symfs_filename, command, sizeof(command)) < 0 || |
| 446 | strstr(command, "[kernel.kallsyms]") || |
| 447 | access(symfs_filename, R_OK)) { |
| 448 | free(filename); |
| 449 | fallback: |
| 450 | /* |
| 451 | * If we don't have build-ids or the build-id file isn't in the |
| 452 | * cache, or is just a kallsyms file, well, lets hope that this |
| 453 | * DSO is the same as when 'perf record' ran. |
| 454 | */ |
| 455 | filename = dso->long_name; |
| 456 | snprintf(symfs_filename, sizeof(symfs_filename), "%s%s", |
| 457 | symbol_conf.symfs, filename); |
| 458 | free_filename = false; |
| 459 | } |
| 460 | |
Arnaldo Carvalho de Melo | 878b439 | 2011-03-11 13:13:36 -0300 | [diff] [blame] | 461 | if (dso->symtab_type == SYMTAB__KALLSYMS) { |
Arnaldo Carvalho de Melo | 170ae6b | 2011-02-23 11:08:59 -0300 | [diff] [blame] | 462 | char bf[BUILD_ID_SIZE * 2 + 16] = " with build id "; |
| 463 | char *build_id_msg = NULL; |
| 464 | |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 465 | if (dso->annotate_warned) |
| 466 | goto out_free_filename; |
Arnaldo Carvalho de Melo | 170ae6b | 2011-02-23 11:08:59 -0300 | [diff] [blame] | 467 | |
| 468 | if (dso->has_build_id) { |
| 469 | build_id__sprintf(dso->build_id, |
| 470 | sizeof(dso->build_id), bf + 15); |
| 471 | build_id_msg = bf; |
| 472 | } |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 473 | err = -ENOENT; |
| 474 | dso->annotate_warned = 1; |
Arnaldo Carvalho de Melo | ae55795 | 2011-10-26 08:00:55 -0200 | [diff] [blame] | 475 | pr_err("Can't annotate %s:\n\n" |
| 476 | "No vmlinux file%s\nwas found in the path.\n\n" |
| 477 | "Please use:\n\n" |
| 478 | " perf buildid-cache -av vmlinux\n\n" |
| 479 | "or:\n\n" |
Namhyung Kim | ff2a661 | 2012-02-23 17:46:24 +0900 | [diff] [blame] | 480 | " --vmlinux vmlinux\n", |
Arnaldo Carvalho de Melo | 170ae6b | 2011-02-23 11:08:59 -0300 | [diff] [blame] | 481 | sym->name, build_id_msg ?: ""); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 482 | goto out_free_filename; |
| 483 | } |
| 484 | |
| 485 | pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__, |
| 486 | filename, sym->name, map->unmap_ip(map, sym->start), |
| 487 | map->unmap_ip(map, sym->end)); |
| 488 | |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 489 | pr_debug("annotating [%p] %30s : [%p] %30s\n", |
| 490 | dso, dso->long_name, sym, sym->name); |
| 491 | |
| 492 | snprintf(command, sizeof(command), |
Andi Kleen | f69b64f | 2011-09-15 14:31:41 -0700 | [diff] [blame] | 493 | "objdump %s%s --start-address=0x%016" PRIx64 |
Stephane Eranian | 3e6a2a7 | 2011-05-17 17:32:07 +0200 | [diff] [blame] | 494 | " --stop-address=0x%016" PRIx64 |
| 495 | " -d %s %s -C %s|grep -v %s|expand", |
Andi Kleen | f69b64f | 2011-09-15 14:31:41 -0700 | [diff] [blame] | 496 | disassembler_style ? "-M " : "", |
| 497 | disassembler_style ? disassembler_style : "", |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 498 | map__rip_2objdump(map, sym->start), |
Ingo Molnar | f41612f | 2011-12-23 14:08:04 +0100 | [diff] [blame] | 499 | map__rip_2objdump(map, sym->end+1), |
Stephane Eranian | 3e6a2a7 | 2011-05-17 17:32:07 +0200 | [diff] [blame] | 500 | symbol_conf.annotate_asm_raw ? "" : "--no-show-raw", |
| 501 | symbol_conf.annotate_src ? "-S" : "", |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 502 | symfs_filename, filename); |
| 503 | |
| 504 | pr_debug("Executing: %s\n", command); |
| 505 | |
| 506 | file = popen(command, "r"); |
| 507 | if (!file) |
| 508 | goto out_free_filename; |
| 509 | |
| 510 | while (!feof(file)) |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 511 | if (symbol__parse_objdump_line(sym, map, file, privsize) < 0) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 512 | break; |
| 513 | |
| 514 | pclose(file); |
| 515 | out_free_filename: |
| 516 | if (free_filename) |
| 517 | free(filename); |
| 518 | return err; |
| 519 | } |
| 520 | |
| 521 | static void insert_source_line(struct rb_root *root, struct source_line *src_line) |
| 522 | { |
| 523 | struct source_line *iter; |
| 524 | struct rb_node **p = &root->rb_node; |
| 525 | struct rb_node *parent = NULL; |
| 526 | |
| 527 | while (*p != NULL) { |
| 528 | parent = *p; |
| 529 | iter = rb_entry(parent, struct source_line, node); |
| 530 | |
| 531 | if (src_line->percent > iter->percent) |
| 532 | p = &(*p)->rb_left; |
| 533 | else |
| 534 | p = &(*p)->rb_right; |
| 535 | } |
| 536 | |
| 537 | rb_link_node(&src_line->node, parent, p); |
| 538 | rb_insert_color(&src_line->node, root); |
| 539 | } |
| 540 | |
| 541 | static void symbol__free_source_line(struct symbol *sym, int len) |
| 542 | { |
| 543 | struct annotation *notes = symbol__annotation(sym); |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 544 | struct source_line *src_line = notes->src->lines; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 545 | int i; |
| 546 | |
| 547 | for (i = 0; i < len; i++) |
| 548 | free(src_line[i].path); |
| 549 | |
| 550 | free(src_line); |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 551 | notes->src->lines = NULL; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | /* Get the filename:line for the colored entries */ |
| 555 | static int symbol__get_source_line(struct symbol *sym, struct map *map, |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame] | 556 | int evidx, struct rb_root *root, int len, |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 557 | const char *filename) |
| 558 | { |
| 559 | u64 start; |
| 560 | int i; |
| 561 | char cmd[PATH_MAX * 2]; |
| 562 | struct source_line *src_line; |
| 563 | struct annotation *notes = symbol__annotation(sym); |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame] | 564 | struct sym_hist *h = annotation__histogram(notes, evidx); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 565 | |
| 566 | if (!h->sum) |
| 567 | return 0; |
| 568 | |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 569 | src_line = notes->src->lines = calloc(len, sizeof(struct source_line)); |
| 570 | if (!notes->src->lines) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 571 | return -1; |
| 572 | |
David Miller | f40a063 | 2012-03-25 16:28:12 -0400 | [diff] [blame] | 573 | start = map__rip_2objdump(map, sym->start); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 574 | |
| 575 | for (i = 0; i < len; i++) { |
| 576 | char *path = NULL; |
| 577 | size_t line_len; |
| 578 | u64 offset; |
| 579 | FILE *fp; |
| 580 | |
| 581 | src_line[i].percent = 100.0 * h->addr[i] / h->sum; |
| 582 | if (src_line[i].percent <= 0.5) |
| 583 | continue; |
| 584 | |
| 585 | offset = start + i; |
| 586 | sprintf(cmd, "addr2line -e %s %016" PRIx64, filename, offset); |
| 587 | fp = popen(cmd, "r"); |
| 588 | if (!fp) |
| 589 | continue; |
| 590 | |
| 591 | if (getline(&path, &line_len, fp) < 0 || !line_len) |
| 592 | goto next; |
| 593 | |
| 594 | src_line[i].path = malloc(sizeof(char) * line_len + 1); |
| 595 | if (!src_line[i].path) |
| 596 | goto next; |
| 597 | |
| 598 | strcpy(src_line[i].path, path); |
| 599 | insert_source_line(root, &src_line[i]); |
| 600 | |
| 601 | next: |
| 602 | pclose(fp); |
| 603 | } |
| 604 | |
| 605 | return 0; |
| 606 | } |
| 607 | |
| 608 | static void print_summary(struct rb_root *root, const char *filename) |
| 609 | { |
| 610 | struct source_line *src_line; |
| 611 | struct rb_node *node; |
| 612 | |
| 613 | printf("\nSorted summary for file %s\n", filename); |
| 614 | printf("----------------------------------------------\n\n"); |
| 615 | |
| 616 | if (RB_EMPTY_ROOT(root)) { |
| 617 | printf(" Nothing higher than %1.1f%%\n", MIN_GREEN); |
| 618 | return; |
| 619 | } |
| 620 | |
| 621 | node = rb_first(root); |
| 622 | while (node) { |
| 623 | double percent; |
| 624 | const char *color; |
| 625 | char *path; |
| 626 | |
| 627 | src_line = rb_entry(node, struct source_line, node); |
| 628 | percent = src_line->percent; |
| 629 | color = get_percent_color(percent); |
| 630 | path = src_line->path; |
| 631 | |
| 632 | color_fprintf(stdout, color, " %7.2f %s", percent, path); |
| 633 | node = rb_next(node); |
| 634 | } |
| 635 | } |
| 636 | |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame] | 637 | static void symbol__annotate_hits(struct symbol *sym, int evidx) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 638 | { |
| 639 | struct annotation *notes = symbol__annotation(sym); |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame] | 640 | struct sym_hist *h = annotation__histogram(notes, evidx); |
Arnaldo Carvalho de Melo | 1b2e2df | 2012-04-19 10:57:06 -0300 | [diff] [blame] | 641 | u64 len = symbol__size(sym), offset; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 642 | |
| 643 | for (offset = 0; offset < len; ++offset) |
| 644 | if (h->addr[offset] != 0) |
| 645 | printf("%*" PRIx64 ": %" PRIu64 "\n", BITS_PER_LONG / 2, |
| 646 | sym->start + offset, h->addr[offset]); |
| 647 | printf("%*s: %" PRIu64 "\n", BITS_PER_LONG / 2, "h->sum", h->sum); |
| 648 | } |
| 649 | |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 650 | int symbol__annotate_printf(struct symbol *sym, struct map *map, int evidx, |
Arnaldo Carvalho de Melo | d5e3d74 | 2011-02-08 15:29:25 -0200 | [diff] [blame] | 651 | bool full_paths, int min_pcnt, int max_lines, |
| 652 | int context) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 653 | { |
| 654 | struct dso *dso = map->dso; |
| 655 | const char *filename = dso->long_name, *d_filename; |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 656 | struct annotation *notes = symbol__annotation(sym); |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 657 | struct disasm_line *pos, *queue = NULL; |
Arnaldo Carvalho de Melo | 058b4cc | 2012-04-02 12:59:01 -0300 | [diff] [blame] | 658 | u64 start = map__rip_2objdump(map, sym->start); |
Arnaldo Carvalho de Melo | d5e3d74 | 2011-02-08 15:29:25 -0200 | [diff] [blame] | 659 | int printed = 2, queue_len = 0; |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 660 | int more = 0; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 661 | u64 len; |
| 662 | |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 663 | if (full_paths) |
| 664 | d_filename = filename; |
| 665 | else |
| 666 | d_filename = basename(filename); |
| 667 | |
Arnaldo Carvalho de Melo | 1b2e2df | 2012-04-19 10:57:06 -0300 | [diff] [blame] | 668 | len = symbol__size(sym); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 669 | |
Arnaldo Carvalho de Melo | f1e2701 | 2011-02-05 18:51:38 -0200 | [diff] [blame] | 670 | printf(" Percent | Source code & Disassembly of %s\n", d_filename); |
| 671 | printf("------------------------------------------------\n"); |
| 672 | |
| 673 | if (verbose) |
| 674 | symbol__annotate_hits(sym, evidx); |
| 675 | |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 676 | list_for_each_entry(pos, ¬es->src->source, node) { |
Arnaldo Carvalho de Melo | d5e3d74 | 2011-02-08 15:29:25 -0200 | [diff] [blame] | 677 | if (context && queue == NULL) { |
| 678 | queue = pos; |
| 679 | queue_len = 0; |
| 680 | } |
| 681 | |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 682 | switch (disasm_line__print(pos, sym, start, evidx, len, |
Arnaldo Carvalho de Melo | 058b4cc | 2012-04-02 12:59:01 -0300 | [diff] [blame] | 683 | min_pcnt, printed, max_lines, |
| 684 | queue)) { |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 685 | case 0: |
| 686 | ++printed; |
Arnaldo Carvalho de Melo | d5e3d74 | 2011-02-08 15:29:25 -0200 | [diff] [blame] | 687 | if (context) { |
| 688 | printed += queue_len; |
| 689 | queue = NULL; |
| 690 | queue_len = 0; |
| 691 | } |
Arnaldo Carvalho de Melo | f1e2701 | 2011-02-05 18:51:38 -0200 | [diff] [blame] | 692 | break; |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 693 | case 1: |
| 694 | /* filtered by max_lines */ |
| 695 | ++more; |
| 696 | break; |
| 697 | case -1: |
| 698 | default: |
Arnaldo Carvalho de Melo | d5e3d74 | 2011-02-08 15:29:25 -0200 | [diff] [blame] | 699 | /* |
| 700 | * Filtered by min_pcnt or non IP lines when |
| 701 | * context != 0 |
| 702 | */ |
| 703 | if (!context) |
| 704 | break; |
| 705 | if (queue_len == context) |
| 706 | queue = list_entry(queue->node.next, typeof(*queue), node); |
| 707 | else |
| 708 | ++queue_len; |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 709 | break; |
| 710 | } |
| 711 | } |
Arnaldo Carvalho de Melo | f1e2701 | 2011-02-05 18:51:38 -0200 | [diff] [blame] | 712 | |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 713 | return more; |
| 714 | } |
| 715 | |
| 716 | void symbol__annotate_zero_histogram(struct symbol *sym, int evidx) |
| 717 | { |
| 718 | struct annotation *notes = symbol__annotation(sym); |
| 719 | struct sym_hist *h = annotation__histogram(notes, evidx); |
| 720 | |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 721 | memset(h, 0, notes->src->sizeof_sym_hist); |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 722 | } |
| 723 | |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 724 | void symbol__annotate_decay_histogram(struct symbol *sym, int evidx) |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 725 | { |
| 726 | struct annotation *notes = symbol__annotation(sym); |
| 727 | struct sym_hist *h = annotation__histogram(notes, evidx); |
Arnaldo Carvalho de Melo | 1b2e2df | 2012-04-19 10:57:06 -0300 | [diff] [blame] | 728 | int len = symbol__size(sym), offset; |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 729 | |
| 730 | h->sum = 0; |
Arnaldo Carvalho de Melo | 8b84a56 | 2012-04-05 16:15:59 -0300 | [diff] [blame] | 731 | for (offset = 0; offset < len; ++offset) { |
| 732 | h->addr[offset] = h->addr[offset] * 7 / 8; |
| 733 | h->sum += h->addr[offset]; |
Arnaldo Carvalho de Melo | f1e2701 | 2011-02-05 18:51:38 -0200 | [diff] [blame] | 734 | } |
| 735 | } |
| 736 | |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 737 | void disasm__purge(struct list_head *head) |
Arnaldo Carvalho de Melo | f1e2701 | 2011-02-05 18:51:38 -0200 | [diff] [blame] | 738 | { |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 739 | struct disasm_line *pos, *n; |
Arnaldo Carvalho de Melo | f1e2701 | 2011-02-05 18:51:38 -0200 | [diff] [blame] | 740 | |
| 741 | list_for_each_entry_safe(pos, n, head, node) { |
| 742 | list_del(&pos->node); |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 743 | disasm_line__free(pos); |
Arnaldo Carvalho de Melo | f1e2701 | 2011-02-05 18:51:38 -0200 | [diff] [blame] | 744 | } |
| 745 | } |
| 746 | |
Arnaldo Carvalho de Melo | 5145418 | 2012-04-15 15:52:18 -0300 | [diff] [blame] | 747 | static size_t disasm_line__fprintf(struct disasm_line *dl, FILE *fp) |
| 748 | { |
| 749 | size_t printed; |
| 750 | |
| 751 | if (dl->offset == -1) |
| 752 | return fprintf(fp, "%s\n", dl->line); |
| 753 | |
| 754 | printed = fprintf(fp, "%#" PRIx64 " %s", dl->offset, dl->name); |
| 755 | |
Arnaldo Carvalho de Melo | c7e6ead | 2012-04-20 14:38:46 -0300 | [diff] [blame^] | 756 | if (dl->ops.raw[0] != '\0') { |
Arnaldo Carvalho de Melo | 5145418 | 2012-04-15 15:52:18 -0300 | [diff] [blame] | 757 | printed += fprintf(fp, "%.*s %s\n", 6 - (int)printed, " ", |
Arnaldo Carvalho de Melo | c7e6ead | 2012-04-20 14:38:46 -0300 | [diff] [blame^] | 758 | dl->ops.raw); |
Arnaldo Carvalho de Melo | 5145418 | 2012-04-15 15:52:18 -0300 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | return printed + fprintf(fp, "\n"); |
| 762 | } |
| 763 | |
| 764 | size_t disasm__fprintf(struct list_head *head, FILE *fp) |
| 765 | { |
| 766 | struct disasm_line *pos; |
| 767 | size_t printed = 0; |
| 768 | |
| 769 | list_for_each_entry(pos, head, node) |
| 770 | printed += disasm_line__fprintf(pos, fp); |
| 771 | |
| 772 | return printed; |
| 773 | } |
| 774 | |
Arnaldo Carvalho de Melo | f1e2701 | 2011-02-05 18:51:38 -0200 | [diff] [blame] | 775 | int symbol__tty_annotate(struct symbol *sym, struct map *map, int evidx, |
| 776 | bool print_lines, bool full_paths, int min_pcnt, |
| 777 | int max_lines) |
| 778 | { |
| 779 | struct dso *dso = map->dso; |
| 780 | const char *filename = dso->long_name; |
| 781 | struct rb_root source_line = RB_ROOT; |
Arnaldo Carvalho de Melo | f1e2701 | 2011-02-05 18:51:38 -0200 | [diff] [blame] | 782 | u64 len; |
| 783 | |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 784 | if (symbol__annotate(sym, map, 0) < 0) |
Arnaldo Carvalho de Melo | f1e2701 | 2011-02-05 18:51:38 -0200 | [diff] [blame] | 785 | return -1; |
| 786 | |
Arnaldo Carvalho de Melo | 1b2e2df | 2012-04-19 10:57:06 -0300 | [diff] [blame] | 787 | len = symbol__size(sym); |
Arnaldo Carvalho de Melo | f1e2701 | 2011-02-05 18:51:38 -0200 | [diff] [blame] | 788 | |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 789 | if (print_lines) { |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame] | 790 | symbol__get_source_line(sym, map, evidx, &source_line, |
| 791 | len, filename); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 792 | print_summary(&source_line, filename); |
| 793 | } |
| 794 | |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 795 | symbol__annotate_printf(sym, map, evidx, full_paths, |
Arnaldo Carvalho de Melo | d5e3d74 | 2011-02-08 15:29:25 -0200 | [diff] [blame] | 796 | min_pcnt, max_lines, 0); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 797 | if (print_lines) |
| 798 | symbol__free_source_line(sym, len); |
| 799 | |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 800 | disasm__purge(&symbol__annotation(sym)->src->source); |
Arnaldo Carvalho de Melo | f1e2701 | 2011-02-05 18:51:38 -0200 | [diff] [blame] | 801 | |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 802 | return 0; |
| 803 | } |