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