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