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> |
Jiri Olsa | 4383db8 | 2012-10-27 23:18:29 +0200 | [diff] [blame] | 18 | #include <linux/bitops.h> |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 19 | |
Andi Kleen | f69b64f | 2011-09-15 14:31:41 -0700 | [diff] [blame] | 20 | const char *disassembler_style; |
Maciek Borzecki | 7a4ec93 | 2012-09-04 12:32:30 +0200 | [diff] [blame] | 21 | const char *objdump_path; |
Andi Kleen | f69b64f | 2011-09-15 14:31:41 -0700 | [diff] [blame] | 22 | |
Arnaldo Carvalho de Melo | 7a997fe | 2012-05-12 13:15:34 -0300 | [diff] [blame] | 23 | static struct ins *ins__find(const char *name); |
| 24 | static int disasm_line__parse(char *line, char **namep, char **rawp); |
| 25 | |
Arnaldo Carvalho de Melo | c46219ac | 2012-05-12 13:26:20 -0300 | [diff] [blame] | 26 | static void ins__delete(struct ins_operands *ops) |
| 27 | { |
| 28 | free(ops->source.raw); |
| 29 | free(ops->source.name); |
| 30 | free(ops->target.raw); |
| 31 | free(ops->target.name); |
| 32 | } |
| 33 | |
Arnaldo Carvalho de Melo | 5417072 | 2012-05-07 18:54:16 -0300 | [diff] [blame] | 34 | static int ins__raw_scnprintf(struct ins *ins, char *bf, size_t size, |
| 35 | struct ins_operands *ops) |
| 36 | { |
| 37 | return scnprintf(bf, size, "%-6.6s %s", ins->name, ops->raw); |
| 38 | } |
| 39 | |
| 40 | int ins__scnprintf(struct ins *ins, char *bf, size_t size, |
| 41 | struct ins_operands *ops) |
| 42 | { |
| 43 | if (ins->ops->scnprintf) |
| 44 | return ins->ops->scnprintf(ins, bf, size, ops); |
| 45 | |
| 46 | return ins__raw_scnprintf(ins, bf, size, ops); |
| 47 | } |
| 48 | |
Arnaldo Carvalho de Melo | c7e6ead | 2012-04-20 14:38:46 -0300 | [diff] [blame] | 49 | static int call__parse(struct ins_operands *ops) |
Arnaldo Carvalho de Melo | d86b059 | 2012-04-18 16:07:38 -0300 | [diff] [blame] | 50 | { |
Arnaldo Carvalho de Melo | d223288 | 2012-04-20 15:26:47 -0300 | [diff] [blame] | 51 | char *endptr, *tok, *name; |
| 52 | |
Arnaldo Carvalho de Melo | 44d1a3e | 2012-04-25 08:00:23 -0300 | [diff] [blame] | 53 | ops->target.addr = strtoull(ops->raw, &endptr, 16); |
Arnaldo Carvalho de Melo | d223288 | 2012-04-20 15:26:47 -0300 | [diff] [blame] | 54 | |
| 55 | name = strchr(endptr, '<'); |
| 56 | if (name == NULL) |
| 57 | goto indirect_call; |
| 58 | |
| 59 | name++; |
| 60 | |
| 61 | tok = strchr(name, '>'); |
| 62 | if (tok == NULL) |
| 63 | return -1; |
| 64 | |
| 65 | *tok = '\0'; |
Arnaldo Carvalho de Melo | 44d1a3e | 2012-04-25 08:00:23 -0300 | [diff] [blame] | 66 | ops->target.name = strdup(name); |
Arnaldo Carvalho de Melo | d223288 | 2012-04-20 15:26:47 -0300 | [diff] [blame] | 67 | *tok = '>'; |
| 68 | |
Arnaldo Carvalho de Melo | 44d1a3e | 2012-04-25 08:00:23 -0300 | [diff] [blame] | 69 | return ops->target.name == NULL ? -1 : 0; |
Arnaldo Carvalho de Melo | d223288 | 2012-04-20 15:26:47 -0300 | [diff] [blame] | 70 | |
| 71 | indirect_call: |
Arnaldo Carvalho de Melo | e8ea156 | 2012-05-11 12:28:55 -0300 | [diff] [blame] | 72 | tok = strchr(endptr, '('); |
| 73 | if (tok != NULL) { |
| 74 | ops->target.addr = 0; |
| 75 | return 0; |
| 76 | } |
| 77 | |
Arnaldo Carvalho de Melo | d223288 | 2012-04-20 15:26:47 -0300 | [diff] [blame] | 78 | tok = strchr(endptr, '*'); |
| 79 | if (tok == NULL) |
| 80 | return -1; |
| 81 | |
Arnaldo Carvalho de Melo | 44d1a3e | 2012-04-25 08:00:23 -0300 | [diff] [blame] | 82 | ops->target.addr = strtoull(tok + 1, NULL, 16); |
Arnaldo Carvalho de Melo | d86b059 | 2012-04-18 16:07:38 -0300 | [diff] [blame] | 83 | return 0; |
| 84 | } |
| 85 | |
Arnaldo Carvalho de Melo | d223288 | 2012-04-20 15:26:47 -0300 | [diff] [blame] | 86 | 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] | 87 | struct ins_operands *ops) |
Arnaldo Carvalho de Melo | d223288 | 2012-04-20 15:26:47 -0300 | [diff] [blame] | 88 | { |
Arnaldo Carvalho de Melo | 44d1a3e | 2012-04-25 08:00:23 -0300 | [diff] [blame] | 89 | if (ops->target.name) |
| 90 | 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] | 91 | |
Arnaldo Carvalho de Melo | e8ea156 | 2012-05-11 12:28:55 -0300 | [diff] [blame] | 92 | if (ops->target.addr == 0) |
| 93 | return ins__raw_scnprintf(ins, bf, size, ops); |
| 94 | |
Arnaldo Carvalho de Melo | 44d1a3e | 2012-04-25 08:00:23 -0300 | [diff] [blame] | 95 | 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] | 96 | } |
| 97 | |
Arnaldo Carvalho de Melo | d86b059 | 2012-04-18 16:07:38 -0300 | [diff] [blame] | 98 | static struct ins_ops call_ops = { |
Arnaldo Carvalho de Melo | d223288 | 2012-04-20 15:26:47 -0300 | [diff] [blame] | 99 | .parse = call__parse, |
| 100 | .scnprintf = call__scnprintf, |
Arnaldo Carvalho de Melo | d86b059 | 2012-04-18 16:07:38 -0300 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | bool ins__is_call(const struct ins *ins) |
| 104 | { |
| 105 | return ins->ops == &call_ops; |
| 106 | } |
| 107 | |
Arnaldo Carvalho de Melo | c7e6ead | 2012-04-20 14:38:46 -0300 | [diff] [blame] | 108 | static int jump__parse(struct ins_operands *ops) |
Arnaldo Carvalho de Melo | 4f9d032 | 2012-04-18 13:58:34 -0300 | [diff] [blame] | 109 | { |
Arnaldo Carvalho de Melo | c7e6ead | 2012-04-20 14:38:46 -0300 | [diff] [blame] | 110 | const char *s = strchr(ops->raw, '+'); |
Arnaldo Carvalho de Melo | 4f9d032 | 2012-04-18 13:58:34 -0300 | [diff] [blame] | 111 | |
Arnaldo Carvalho de Melo | fb29fa58 | 2012-04-25 14:16:03 -0300 | [diff] [blame] | 112 | ops->target.addr = strtoll(ops->raw, NULL, 16); |
Arnaldo Carvalho de Melo | 4f9d032 | 2012-04-18 13:58:34 -0300 | [diff] [blame] | 113 | |
Arnaldo Carvalho de Melo | fb29fa58 | 2012-04-25 14:16:03 -0300 | [diff] [blame] | 114 | if (s++ != NULL) |
| 115 | ops->target.offset = strtoll(s, NULL, 16); |
| 116 | else |
| 117 | ops->target.offset = UINT64_MAX; |
| 118 | |
Arnaldo Carvalho de Melo | 4f9d032 | 2012-04-18 13:58:34 -0300 | [diff] [blame] | 119 | return 0; |
| 120 | } |
| 121 | |
Arnaldo Carvalho de Melo | c7e6ead | 2012-04-20 14:38:46 -0300 | [diff] [blame] | 122 | 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] | 123 | struct ins_operands *ops) |
Arnaldo Carvalho de Melo | 28548d7 | 2012-04-19 10:16:27 -0300 | [diff] [blame] | 124 | { |
Arnaldo Carvalho de Melo | 44d1a3e | 2012-04-25 08:00:23 -0300 | [diff] [blame] | 125 | 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] | 126 | } |
| 127 | |
Arnaldo Carvalho de Melo | 4f9d032 | 2012-04-18 13:58:34 -0300 | [diff] [blame] | 128 | static struct ins_ops jump_ops = { |
Arnaldo Carvalho de Melo | c7e6ead | 2012-04-20 14:38:46 -0300 | [diff] [blame] | 129 | .parse = jump__parse, |
| 130 | .scnprintf = jump__scnprintf, |
Arnaldo Carvalho de Melo | 4f9d032 | 2012-04-18 13:58:34 -0300 | [diff] [blame] | 131 | }; |
| 132 | |
| 133 | bool ins__is_jump(const struct ins *ins) |
| 134 | { |
| 135 | return ins->ops == &jump_ops; |
| 136 | } |
| 137 | |
Arnaldo Carvalho de Melo | 6de783b | 2012-05-11 16:48:49 -0300 | [diff] [blame] | 138 | static int comment__symbol(char *raw, char *comment, u64 *addrp, char **namep) |
| 139 | { |
| 140 | char *endptr, *name, *t; |
| 141 | |
| 142 | if (strstr(raw, "(%rip)") == NULL) |
| 143 | return 0; |
| 144 | |
| 145 | *addrp = strtoull(comment, &endptr, 16); |
| 146 | name = strchr(endptr, '<'); |
| 147 | if (name == NULL) |
| 148 | return -1; |
| 149 | |
| 150 | name++; |
| 151 | |
| 152 | t = strchr(name, '>'); |
| 153 | if (t == NULL) |
| 154 | return 0; |
| 155 | |
| 156 | *t = '\0'; |
| 157 | *namep = strdup(name); |
| 158 | *t = '>'; |
| 159 | |
| 160 | return 0; |
| 161 | } |
| 162 | |
Arnaldo Carvalho de Melo | 7a997fe | 2012-05-12 13:15:34 -0300 | [diff] [blame] | 163 | static int lock__parse(struct ins_operands *ops) |
| 164 | { |
| 165 | char *name; |
| 166 | |
| 167 | ops->locked.ops = zalloc(sizeof(*ops->locked.ops)); |
| 168 | if (ops->locked.ops == NULL) |
| 169 | return 0; |
| 170 | |
| 171 | if (disasm_line__parse(ops->raw, &name, &ops->locked.ops->raw) < 0) |
| 172 | goto out_free_ops; |
| 173 | |
| 174 | ops->locked.ins = ins__find(name); |
| 175 | if (ops->locked.ins == NULL) |
| 176 | goto out_free_ops; |
| 177 | |
| 178 | if (!ops->locked.ins->ops) |
| 179 | return 0; |
| 180 | |
| 181 | if (ops->locked.ins->ops->parse) |
| 182 | ops->locked.ins->ops->parse(ops->locked.ops); |
| 183 | |
| 184 | return 0; |
| 185 | |
| 186 | out_free_ops: |
| 187 | free(ops->locked.ops); |
| 188 | ops->locked.ops = NULL; |
| 189 | return 0; |
| 190 | } |
| 191 | |
| 192 | static int lock__scnprintf(struct ins *ins, char *bf, size_t size, |
| 193 | struct ins_operands *ops) |
| 194 | { |
| 195 | int printed; |
| 196 | |
| 197 | if (ops->locked.ins == NULL) |
| 198 | return ins__raw_scnprintf(ins, bf, size, ops); |
| 199 | |
| 200 | printed = scnprintf(bf, size, "%-6.6s ", ins->name); |
| 201 | return printed + ins__scnprintf(ops->locked.ins, bf + printed, |
| 202 | size - printed, ops->locked.ops); |
| 203 | } |
| 204 | |
Arnaldo Carvalho de Melo | c46219ac | 2012-05-12 13:26:20 -0300 | [diff] [blame] | 205 | static void lock__delete(struct ins_operands *ops) |
| 206 | { |
| 207 | free(ops->locked.ops); |
| 208 | free(ops->target.raw); |
| 209 | free(ops->target.name); |
| 210 | } |
| 211 | |
Arnaldo Carvalho de Melo | 7a997fe | 2012-05-12 13:15:34 -0300 | [diff] [blame] | 212 | static struct ins_ops lock_ops = { |
Arnaldo Carvalho de Melo | c46219ac | 2012-05-12 13:26:20 -0300 | [diff] [blame] | 213 | .free = lock__delete, |
Arnaldo Carvalho de Melo | 7a997fe | 2012-05-12 13:15:34 -0300 | [diff] [blame] | 214 | .parse = lock__parse, |
| 215 | .scnprintf = lock__scnprintf, |
| 216 | }; |
| 217 | |
Arnaldo Carvalho de Melo | 6de783b | 2012-05-11 16:48:49 -0300 | [diff] [blame] | 218 | static int mov__parse(struct ins_operands *ops) |
| 219 | { |
| 220 | char *s = strchr(ops->raw, ','), *target, *comment, prev; |
| 221 | |
| 222 | if (s == NULL) |
| 223 | return -1; |
| 224 | |
| 225 | *s = '\0'; |
| 226 | ops->source.raw = strdup(ops->raw); |
| 227 | *s = ','; |
| 228 | |
| 229 | if (ops->source.raw == NULL) |
| 230 | return -1; |
| 231 | |
| 232 | target = ++s; |
| 233 | |
| 234 | while (s[0] != '\0' && !isspace(s[0])) |
| 235 | ++s; |
| 236 | prev = *s; |
| 237 | *s = '\0'; |
| 238 | |
| 239 | ops->target.raw = strdup(target); |
| 240 | *s = prev; |
| 241 | |
| 242 | if (ops->target.raw == NULL) |
| 243 | goto out_free_source; |
| 244 | |
| 245 | comment = strchr(s, '#'); |
| 246 | if (comment == NULL) |
| 247 | return 0; |
| 248 | |
| 249 | while (comment[0] != '\0' && isspace(comment[0])) |
| 250 | ++comment; |
| 251 | |
| 252 | comment__symbol(ops->source.raw, comment, &ops->source.addr, &ops->source.name); |
| 253 | comment__symbol(ops->target.raw, comment, &ops->target.addr, &ops->target.name); |
| 254 | |
| 255 | return 0; |
| 256 | |
| 257 | out_free_source: |
| 258 | free(ops->source.raw); |
| 259 | ops->source.raw = NULL; |
| 260 | return -1; |
| 261 | } |
| 262 | |
| 263 | static int mov__scnprintf(struct ins *ins, char *bf, size_t size, |
| 264 | struct ins_operands *ops) |
| 265 | { |
| 266 | return scnprintf(bf, size, "%-6.6s %s,%s", ins->name, |
| 267 | ops->source.name ?: ops->source.raw, |
| 268 | ops->target.name ?: ops->target.raw); |
| 269 | } |
| 270 | |
| 271 | static struct ins_ops mov_ops = { |
| 272 | .parse = mov__parse, |
| 273 | .scnprintf = mov__scnprintf, |
| 274 | }; |
| 275 | |
Arnaldo Carvalho de Melo | a43712c | 2012-05-11 17:21:09 -0300 | [diff] [blame] | 276 | static int dec__parse(struct ins_operands *ops) |
| 277 | { |
| 278 | char *target, *comment, *s, prev; |
| 279 | |
| 280 | target = s = ops->raw; |
| 281 | |
| 282 | while (s[0] != '\0' && !isspace(s[0])) |
| 283 | ++s; |
| 284 | prev = *s; |
| 285 | *s = '\0'; |
| 286 | |
| 287 | ops->target.raw = strdup(target); |
| 288 | *s = prev; |
| 289 | |
| 290 | if (ops->target.raw == NULL) |
| 291 | return -1; |
| 292 | |
| 293 | comment = strchr(s, '#'); |
| 294 | if (comment == NULL) |
| 295 | return 0; |
| 296 | |
| 297 | while (comment[0] != '\0' && isspace(comment[0])) |
| 298 | ++comment; |
| 299 | |
| 300 | comment__symbol(ops->target.raw, comment, &ops->target.addr, &ops->target.name); |
| 301 | |
| 302 | return 0; |
| 303 | } |
| 304 | |
| 305 | static int dec__scnprintf(struct ins *ins, char *bf, size_t size, |
| 306 | struct ins_operands *ops) |
| 307 | { |
| 308 | return scnprintf(bf, size, "%-6.6s %s", ins->name, |
| 309 | ops->target.name ?: ops->target.raw); |
| 310 | } |
| 311 | |
| 312 | static struct ins_ops dec_ops = { |
| 313 | .parse = dec__parse, |
| 314 | .scnprintf = dec__scnprintf, |
| 315 | }; |
| 316 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 317 | static int nop__scnprintf(struct ins *ins __maybe_unused, char *bf, size_t size, |
| 318 | struct ins_operands *ops __maybe_unused) |
Arnaldo Carvalho de Melo | b9818e9 | 2012-05-07 18:57:02 -0300 | [diff] [blame] | 319 | { |
| 320 | return scnprintf(bf, size, "%-6.6s", "nop"); |
| 321 | } |
| 322 | |
| 323 | static struct ins_ops nop_ops = { |
| 324 | .scnprintf = nop__scnprintf, |
| 325 | }; |
| 326 | |
Arnaldo Carvalho de Melo | 4f9d032 | 2012-04-18 13:58:34 -0300 | [diff] [blame] | 327 | /* |
| 328 | * Must be sorted by name! |
| 329 | */ |
| 330 | static struct ins instructions[] = { |
Arnaldo Carvalho de Melo | 6de783b | 2012-05-11 16:48:49 -0300 | [diff] [blame] | 331 | { .name = "add", .ops = &mov_ops, }, |
| 332 | { .name = "addl", .ops = &mov_ops, }, |
| 333 | { .name = "addq", .ops = &mov_ops, }, |
| 334 | { .name = "addw", .ops = &mov_ops, }, |
| 335 | { .name = "and", .ops = &mov_ops, }, |
Arnaldo Carvalho de Melo | 7a997fe | 2012-05-12 13:15:34 -0300 | [diff] [blame] | 336 | { .name = "bts", .ops = &mov_ops, }, |
Arnaldo Carvalho de Melo | d86b059 | 2012-04-18 16:07:38 -0300 | [diff] [blame] | 337 | { .name = "call", .ops = &call_ops, }, |
| 338 | { .name = "callq", .ops = &call_ops, }, |
Arnaldo Carvalho de Melo | 6de783b | 2012-05-11 16:48:49 -0300 | [diff] [blame] | 339 | { .name = "cmp", .ops = &mov_ops, }, |
| 340 | { .name = "cmpb", .ops = &mov_ops, }, |
| 341 | { .name = "cmpl", .ops = &mov_ops, }, |
| 342 | { .name = "cmpq", .ops = &mov_ops, }, |
| 343 | { .name = "cmpw", .ops = &mov_ops, }, |
| 344 | { .name = "cmpxch", .ops = &mov_ops, }, |
Arnaldo Carvalho de Melo | a43712c | 2012-05-11 17:21:09 -0300 | [diff] [blame] | 345 | { .name = "dec", .ops = &dec_ops, }, |
| 346 | { .name = "decl", .ops = &dec_ops, }, |
Arnaldo Carvalho de Melo | 6de783b | 2012-05-11 16:48:49 -0300 | [diff] [blame] | 347 | { .name = "imul", .ops = &mov_ops, }, |
Arnaldo Carvalho de Melo | a43712c | 2012-05-11 17:21:09 -0300 | [diff] [blame] | 348 | { .name = "inc", .ops = &dec_ops, }, |
| 349 | { .name = "incl", .ops = &dec_ops, }, |
Arnaldo Carvalho de Melo | 4f9d032 | 2012-04-18 13:58:34 -0300 | [diff] [blame] | 350 | { .name = "ja", .ops = &jump_ops, }, |
Arnaldo Carvalho de Melo | 3f862fd | 2012-04-19 17:10:12 -0300 | [diff] [blame] | 351 | { .name = "jae", .ops = &jump_ops, }, |
| 352 | { .name = "jb", .ops = &jump_ops, }, |
| 353 | { .name = "jbe", .ops = &jump_ops, }, |
| 354 | { .name = "jc", .ops = &jump_ops, }, |
| 355 | { .name = "jcxz", .ops = &jump_ops, }, |
Arnaldo Carvalho de Melo | 4f9d032 | 2012-04-18 13:58:34 -0300 | [diff] [blame] | 356 | { .name = "je", .ops = &jump_ops, }, |
Arnaldo Carvalho de Melo | 3f862fd | 2012-04-19 17:10:12 -0300 | [diff] [blame] | 357 | { .name = "jecxz", .ops = &jump_ops, }, |
| 358 | { .name = "jg", .ops = &jump_ops, }, |
| 359 | { .name = "jge", .ops = &jump_ops, }, |
| 360 | { .name = "jl", .ops = &jump_ops, }, |
| 361 | { .name = "jle", .ops = &jump_ops, }, |
Arnaldo Carvalho de Melo | 4f9d032 | 2012-04-18 13:58:34 -0300 | [diff] [blame] | 362 | { .name = "jmp", .ops = &jump_ops, }, |
| 363 | { .name = "jmpq", .ops = &jump_ops, }, |
Arnaldo Carvalho de Melo | 3f862fd | 2012-04-19 17:10:12 -0300 | [diff] [blame] | 364 | { .name = "jna", .ops = &jump_ops, }, |
| 365 | { .name = "jnae", .ops = &jump_ops, }, |
| 366 | { .name = "jnb", .ops = &jump_ops, }, |
| 367 | { .name = "jnbe", .ops = &jump_ops, }, |
| 368 | { .name = "jnc", .ops = &jump_ops, }, |
Arnaldo Carvalho de Melo | 4f9d032 | 2012-04-18 13:58:34 -0300 | [diff] [blame] | 369 | { .name = "jne", .ops = &jump_ops, }, |
Arnaldo Carvalho de Melo | 3f862fd | 2012-04-19 17:10:12 -0300 | [diff] [blame] | 370 | { .name = "jng", .ops = &jump_ops, }, |
| 371 | { .name = "jnge", .ops = &jump_ops, }, |
| 372 | { .name = "jnl", .ops = &jump_ops, }, |
| 373 | { .name = "jnle", .ops = &jump_ops, }, |
| 374 | { .name = "jno", .ops = &jump_ops, }, |
| 375 | { .name = "jnp", .ops = &jump_ops, }, |
| 376 | { .name = "jns", .ops = &jump_ops, }, |
| 377 | { .name = "jnz", .ops = &jump_ops, }, |
| 378 | { .name = "jo", .ops = &jump_ops, }, |
| 379 | { .name = "jp", .ops = &jump_ops, }, |
| 380 | { .name = "jpe", .ops = &jump_ops, }, |
| 381 | { .name = "jpo", .ops = &jump_ops, }, |
| 382 | { .name = "jrcxz", .ops = &jump_ops, }, |
Arnaldo Carvalho de Melo | 4f9d032 | 2012-04-18 13:58:34 -0300 | [diff] [blame] | 383 | { .name = "js", .ops = &jump_ops, }, |
Arnaldo Carvalho de Melo | 3f862fd | 2012-04-19 17:10:12 -0300 | [diff] [blame] | 384 | { .name = "jz", .ops = &jump_ops, }, |
Arnaldo Carvalho de Melo | 6de783b | 2012-05-11 16:48:49 -0300 | [diff] [blame] | 385 | { .name = "lea", .ops = &mov_ops, }, |
Arnaldo Carvalho de Melo | 7a997fe | 2012-05-12 13:15:34 -0300 | [diff] [blame] | 386 | { .name = "lock", .ops = &lock_ops, }, |
Arnaldo Carvalho de Melo | 6de783b | 2012-05-11 16:48:49 -0300 | [diff] [blame] | 387 | { .name = "mov", .ops = &mov_ops, }, |
| 388 | { .name = "movb", .ops = &mov_ops, }, |
| 389 | { .name = "movdqa",.ops = &mov_ops, }, |
| 390 | { .name = "movl", .ops = &mov_ops, }, |
| 391 | { .name = "movq", .ops = &mov_ops, }, |
| 392 | { .name = "movslq", .ops = &mov_ops, }, |
| 393 | { .name = "movzbl", .ops = &mov_ops, }, |
| 394 | { .name = "movzwl", .ops = &mov_ops, }, |
Arnaldo Carvalho de Melo | b9818e9 | 2012-05-07 18:57:02 -0300 | [diff] [blame] | 395 | { .name = "nop", .ops = &nop_ops, }, |
| 396 | { .name = "nopl", .ops = &nop_ops, }, |
| 397 | { .name = "nopw", .ops = &nop_ops, }, |
Arnaldo Carvalho de Melo | 6de783b | 2012-05-11 16:48:49 -0300 | [diff] [blame] | 398 | { .name = "or", .ops = &mov_ops, }, |
| 399 | { .name = "orl", .ops = &mov_ops, }, |
| 400 | { .name = "test", .ops = &mov_ops, }, |
| 401 | { .name = "testb", .ops = &mov_ops, }, |
| 402 | { .name = "testl", .ops = &mov_ops, }, |
Arnaldo Carvalho de Melo | 7a997fe | 2012-05-12 13:15:34 -0300 | [diff] [blame] | 403 | { .name = "xadd", .ops = &mov_ops, }, |
Arnaldo Carvalho de Melo | 4f9d032 | 2012-04-18 13:58:34 -0300 | [diff] [blame] | 404 | }; |
| 405 | |
| 406 | static int ins__cmp(const void *name, const void *insp) |
| 407 | { |
| 408 | const struct ins *ins = insp; |
| 409 | |
| 410 | return strcmp(name, ins->name); |
| 411 | } |
| 412 | |
| 413 | static struct ins *ins__find(const char *name) |
| 414 | { |
| 415 | const int nmemb = ARRAY_SIZE(instructions); |
| 416 | |
| 417 | return bsearch(name, instructions, nmemb, sizeof(struct ins), ins__cmp); |
| 418 | } |
| 419 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 420 | int symbol__annotate_init(struct map *map __maybe_unused, struct symbol *sym) |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 421 | { |
| 422 | struct annotation *notes = symbol__annotation(sym); |
| 423 | pthread_mutex_init(¬es->lock, NULL); |
| 424 | return 0; |
| 425 | } |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 426 | |
Arnaldo Carvalho de Melo | d04b35f | 2011-11-11 22:17:32 -0200 | [diff] [blame] | 427 | int symbol__alloc_hist(struct symbol *sym) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 428 | { |
| 429 | struct annotation *notes = symbol__annotation(sym); |
Arnaldo Carvalho de Melo | 1b2e2df | 2012-04-19 10:57:06 -0300 | [diff] [blame] | 430 | const size_t size = symbol__size(sym); |
Cody Schafer | 8696329 | 2012-07-19 20:05:25 -0700 | [diff] [blame] | 431 | size_t sizeof_sym_hist; |
| 432 | |
| 433 | /* Check for overflow when calculating sizeof_sym_hist */ |
| 434 | if (size > (SIZE_MAX - sizeof(struct sym_hist)) / sizeof(u64)) |
| 435 | return -1; |
| 436 | |
| 437 | sizeof_sym_hist = (sizeof(struct sym_hist) + size * sizeof(u64)); |
| 438 | |
| 439 | /* Check for overflow in zalloc argument */ |
| 440 | if (sizeof_sym_hist > (SIZE_MAX - sizeof(*notes->src)) |
| 441 | / symbol_conf.nr_events) |
| 442 | return -1; |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 443 | |
Arnaldo Carvalho de Melo | d04b35f | 2011-11-11 22:17:32 -0200 | [diff] [blame] | 444 | 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] | 445 | if (notes->src == NULL) |
| 446 | return -1; |
| 447 | notes->src->sizeof_sym_hist = sizeof_sym_hist; |
Arnaldo Carvalho de Melo | d04b35f | 2011-11-11 22:17:32 -0200 | [diff] [blame] | 448 | notes->src->nr_histograms = symbol_conf.nr_events; |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 449 | INIT_LIST_HEAD(¬es->src->source); |
| 450 | return 0; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 451 | } |
| 452 | |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 453 | void symbol__annotate_zero_histograms(struct symbol *sym) |
| 454 | { |
| 455 | struct annotation *notes = symbol__annotation(sym); |
| 456 | |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 457 | pthread_mutex_lock(¬es->lock); |
| 458 | if (notes->src != NULL) |
| 459 | memset(notes->src->histograms, 0, |
| 460 | notes->src->nr_histograms * notes->src->sizeof_sym_hist); |
| 461 | pthread_mutex_unlock(¬es->lock); |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 462 | } |
| 463 | |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame] | 464 | int symbol__inc_addr_samples(struct symbol *sym, struct map *map, |
| 465 | int evidx, u64 addr) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 466 | { |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame] | 467 | unsigned offset; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 468 | struct annotation *notes; |
| 469 | struct sym_hist *h; |
| 470 | |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 471 | notes = symbol__annotation(sym); |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 472 | if (notes->src == NULL) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 473 | return -ENOMEM; |
| 474 | |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 475 | pr_debug3("%s: addr=%#" PRIx64 "\n", __func__, map->unmap_ip(map, addr)); |
| 476 | |
Arnaldo Carvalho de Melo | 31d68e7 | 2012-03-27 12:55:57 -0300 | [diff] [blame] | 477 | if (addr < sym->start || addr > sym->end) |
| 478 | return -ERANGE; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 479 | |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame] | 480 | offset = addr - sym->start; |
| 481 | h = annotation__histogram(notes, evidx); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 482 | h->sum++; |
| 483 | h->addr[offset]++; |
| 484 | |
| 485 | pr_debug3("%#" PRIx64 " %s: period++ [addr: %#" PRIx64 ", %#" PRIx64 |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame] | 486 | ", evidx=%d] => %" PRIu64 "\n", sym->start, sym->name, |
| 487 | addr, addr - sym->start, evidx, h->addr[offset]); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 488 | return 0; |
| 489 | } |
| 490 | |
Arnaldo Carvalho de Melo | 4f9d032 | 2012-04-18 13:58:34 -0300 | [diff] [blame] | 491 | static void disasm_line__init_ins(struct disasm_line *dl) |
| 492 | { |
| 493 | dl->ins = ins__find(dl->name); |
| 494 | |
| 495 | if (dl->ins == NULL) |
| 496 | return; |
| 497 | |
| 498 | if (!dl->ins->ops) |
| 499 | return; |
| 500 | |
Arnaldo Carvalho de Melo | c7e6ead | 2012-04-20 14:38:46 -0300 | [diff] [blame] | 501 | if (dl->ins->ops->parse) |
| 502 | dl->ins->ops->parse(&dl->ops); |
Arnaldo Carvalho de Melo | 4f9d032 | 2012-04-18 13:58:34 -0300 | [diff] [blame] | 503 | } |
| 504 | |
Arnaldo Carvalho de Melo | 7a997fe | 2012-05-12 13:15:34 -0300 | [diff] [blame] | 505 | static int disasm_line__parse(char *line, char **namep, char **rawp) |
| 506 | { |
| 507 | char *name = line, tmp; |
| 508 | |
| 509 | while (isspace(name[0])) |
| 510 | ++name; |
| 511 | |
| 512 | if (name[0] == '\0') |
| 513 | return -1; |
| 514 | |
| 515 | *rawp = name + 1; |
| 516 | |
| 517 | while ((*rawp)[0] != '\0' && !isspace((*rawp)[0])) |
| 518 | ++*rawp; |
| 519 | |
| 520 | tmp = (*rawp)[0]; |
| 521 | (*rawp)[0] = '\0'; |
| 522 | *namep = strdup(name); |
| 523 | |
| 524 | if (*namep == NULL) |
| 525 | goto out_free_name; |
| 526 | |
| 527 | (*rawp)[0] = tmp; |
| 528 | |
| 529 | if ((*rawp)[0] != '\0') { |
| 530 | (*rawp)++; |
| 531 | while (isspace((*rawp)[0])) |
| 532 | ++(*rawp); |
| 533 | } |
| 534 | |
| 535 | return 0; |
| 536 | |
| 537 | out_free_name: |
| 538 | free(*namep); |
| 539 | *namep = NULL; |
| 540 | return -1; |
| 541 | } |
| 542 | |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 543 | 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] | 544 | { |
Arnaldo Carvalho de Melo | 5145418 | 2012-04-15 15:52:18 -0300 | [diff] [blame] | 545 | struct disasm_line *dl = zalloc(sizeof(*dl) + privsize); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 546 | |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 547 | if (dl != NULL) { |
| 548 | dl->offset = offset; |
| 549 | dl->line = strdup(line); |
| 550 | if (dl->line == NULL) |
Arnaldo Carvalho de Melo | 058b4cc | 2012-04-02 12:59:01 -0300 | [diff] [blame] | 551 | goto out_delete; |
Arnaldo Carvalho de Melo | 5145418 | 2012-04-15 15:52:18 -0300 | [diff] [blame] | 552 | |
| 553 | if (offset != -1) { |
Arnaldo Carvalho de Melo | 7a997fe | 2012-05-12 13:15:34 -0300 | [diff] [blame] | 554 | if (disasm_line__parse(dl->line, &dl->name, &dl->ops.raw) < 0) |
Arnaldo Carvalho de Melo | 5145418 | 2012-04-15 15:52:18 -0300 | [diff] [blame] | 555 | goto out_free_line; |
| 556 | |
Arnaldo Carvalho de Melo | 4f9d032 | 2012-04-18 13:58:34 -0300 | [diff] [blame] | 557 | disasm_line__init_ins(dl); |
Arnaldo Carvalho de Melo | 5145418 | 2012-04-15 15:52:18 -0300 | [diff] [blame] | 558 | } |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 559 | } |
| 560 | |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 561 | return dl; |
Arnaldo Carvalho de Melo | 5145418 | 2012-04-15 15:52:18 -0300 | [diff] [blame] | 562 | |
| 563 | out_free_line: |
| 564 | free(dl->line); |
Arnaldo Carvalho de Melo | 058b4cc | 2012-04-02 12:59:01 -0300 | [diff] [blame] | 565 | out_delete: |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 566 | free(dl); |
Arnaldo Carvalho de Melo | 058b4cc | 2012-04-02 12:59:01 -0300 | [diff] [blame] | 567 | return NULL; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 568 | } |
| 569 | |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 570 | void disasm_line__free(struct disasm_line *dl) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 571 | { |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 572 | free(dl->line); |
Arnaldo Carvalho de Melo | 5145418 | 2012-04-15 15:52:18 -0300 | [diff] [blame] | 573 | free(dl->name); |
Arnaldo Carvalho de Melo | c46219ac | 2012-05-12 13:26:20 -0300 | [diff] [blame] | 574 | if (dl->ins && dl->ins->ops->free) |
| 575 | dl->ins->ops->free(&dl->ops); |
| 576 | else |
| 577 | ins__delete(&dl->ops); |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 578 | free(dl); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 579 | } |
| 580 | |
Arnaldo Carvalho de Melo | 5417072 | 2012-05-07 18:54:16 -0300 | [diff] [blame] | 581 | int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size, bool raw) |
| 582 | { |
| 583 | if (raw || !dl->ins) |
| 584 | return scnprintf(bf, size, "%-6.6s %s", dl->name, dl->ops.raw); |
| 585 | |
| 586 | return ins__scnprintf(dl->ins, bf, size, &dl->ops); |
| 587 | } |
| 588 | |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 589 | 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] | 590 | { |
| 591 | list_add_tail(&line->node, head); |
| 592 | } |
| 593 | |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 594 | 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] | 595 | { |
| 596 | list_for_each_entry_continue(pos, head, node) |
| 597 | if (pos->offset >= 0) |
| 598 | return pos; |
| 599 | |
| 600 | return NULL; |
| 601 | } |
| 602 | |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 603 | static int disasm_line__print(struct disasm_line *dl, struct symbol *sym, u64 start, |
| 604 | int evidx, u64 len, int min_pcnt, int printed, |
| 605 | int max_lines, struct disasm_line *queue) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 606 | { |
| 607 | static const char *prev_line; |
| 608 | static const char *prev_color; |
| 609 | |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 610 | if (dl->offset != -1) { |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 611 | const char *path = NULL; |
| 612 | unsigned int hits = 0; |
| 613 | double percent = 0.0; |
| 614 | const char *color; |
| 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 | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame] | 617 | struct sym_hist *h = annotation__histogram(notes, evidx); |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 618 | s64 offset = dl->offset; |
Arnaldo Carvalho de Melo | 058b4cc | 2012-04-02 12:59:01 -0300 | [diff] [blame] | 619 | const u64 addr = start + offset; |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 620 | struct disasm_line *next; |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 621 | |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 622 | next = disasm__get_next_ip_line(¬es->src->source, dl); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 623 | |
| 624 | while (offset < (s64)len && |
| 625 | (next == NULL || offset < next->offset)) { |
| 626 | if (src_line) { |
| 627 | if (path == NULL) |
| 628 | path = src_line[offset].path; |
| 629 | percent += src_line[offset].percent; |
| 630 | } else |
| 631 | hits += h->addr[offset]; |
| 632 | |
| 633 | ++offset; |
| 634 | } |
| 635 | |
| 636 | if (src_line == NULL && h->sum) |
| 637 | percent = 100.0 * hits / h->sum; |
| 638 | |
Arnaldo Carvalho de Melo | d040bd3 | 2011-02-05 15:37:31 -0200 | [diff] [blame] | 639 | if (percent < min_pcnt) |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 640 | return -1; |
| 641 | |
Arnaldo Carvalho de Melo | e3087b8 | 2011-02-08 15:01:39 -0200 | [diff] [blame] | 642 | if (max_lines && printed >= max_lines) |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 643 | return 1; |
Arnaldo Carvalho de Melo | d040bd3 | 2011-02-05 15:37:31 -0200 | [diff] [blame] | 644 | |
Arnaldo Carvalho de Melo | d5e3d74 | 2011-02-08 15:29:25 -0200 | [diff] [blame] | 645 | if (queue != NULL) { |
| 646 | list_for_each_entry_from(queue, ¬es->src->source, node) { |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 647 | if (queue == dl) |
Arnaldo Carvalho de Melo | d5e3d74 | 2011-02-08 15:29:25 -0200 | [diff] [blame] | 648 | break; |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 649 | disasm_line__print(queue, sym, start, evidx, len, |
Arnaldo Carvalho de Melo | d5e3d74 | 2011-02-08 15:29:25 -0200 | [diff] [blame] | 650 | 0, 0, 1, NULL); |
| 651 | } |
| 652 | } |
| 653 | |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 654 | color = get_percent_color(percent); |
| 655 | |
| 656 | /* |
| 657 | * Also color the filename and line if needed, with |
| 658 | * the same color than the percentage. Don't print it |
| 659 | * twice for close colored addr with the same filename:line |
| 660 | */ |
| 661 | if (path) { |
| 662 | if (!prev_line || strcmp(prev_line, path) |
| 663 | || color != prev_color) { |
| 664 | color_fprintf(stdout, color, " %s", path); |
| 665 | prev_line = path; |
| 666 | prev_color = color; |
| 667 | } |
| 668 | } |
| 669 | |
| 670 | color_fprintf(stdout, color, " %7.2f", percent); |
| 671 | printf(" : "); |
Arnaldo Carvalho de Melo | 058b4cc | 2012-04-02 12:59:01 -0300 | [diff] [blame] | 672 | color_fprintf(stdout, PERF_COLOR_MAGENTA, " %" PRIx64 ":", addr); |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 673 | color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", dl->line); |
Arnaldo Carvalho de Melo | e3087b8 | 2011-02-08 15:01:39 -0200 | [diff] [blame] | 674 | } else if (max_lines && printed >= max_lines) |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 675 | return 1; |
| 676 | else { |
Arnaldo Carvalho de Melo | d5e3d74 | 2011-02-08 15:29:25 -0200 | [diff] [blame] | 677 | if (queue) |
| 678 | return -1; |
| 679 | |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 680 | if (!*dl->line) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 681 | printf(" :\n"); |
| 682 | else |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 683 | printf(" : %s\n", dl->line); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 684 | } |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 685 | |
| 686 | return 0; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 687 | } |
| 688 | |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 689 | static int symbol__parse_objdump_line(struct symbol *sym, struct map *map, |
| 690 | FILE *file, size_t privsize) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 691 | { |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 692 | struct annotation *notes = symbol__annotation(sym); |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 693 | struct disasm_line *dl; |
Arnaldo Carvalho de Melo | 058b4cc | 2012-04-02 12:59:01 -0300 | [diff] [blame] | 694 | char *line = NULL, *parsed_line, *tmp, *tmp2, *c; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 695 | size_t line_len; |
| 696 | s64 line_ip, offset = -1; |
| 697 | |
| 698 | if (getline(&line, &line_len, file) < 0) |
| 699 | return -1; |
| 700 | |
| 701 | if (!line) |
| 702 | return -1; |
| 703 | |
| 704 | while (line_len != 0 && isspace(line[line_len - 1])) |
| 705 | line[--line_len] = '\0'; |
| 706 | |
| 707 | c = strchr(line, '\n'); |
| 708 | if (c) |
| 709 | *c = 0; |
| 710 | |
| 711 | line_ip = -1; |
Namhyung Kim | a31b7cc | 2012-04-11 17:04:59 -0300 | [diff] [blame] | 712 | parsed_line = line; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 713 | |
| 714 | /* |
| 715 | * Strip leading spaces: |
| 716 | */ |
| 717 | tmp = line; |
| 718 | while (*tmp) { |
| 719 | if (*tmp != ' ') |
| 720 | break; |
| 721 | tmp++; |
| 722 | } |
| 723 | |
| 724 | if (*tmp) { |
| 725 | /* |
| 726 | * Parse hexa addresses followed by ':' |
| 727 | */ |
| 728 | line_ip = strtoull(tmp, &tmp2, 16); |
| 729 | if (*tmp2 != ':' || tmp == tmp2 || tmp2[1] == '\0') |
| 730 | line_ip = -1; |
| 731 | } |
| 732 | |
| 733 | if (line_ip != -1) { |
| 734 | u64 start = map__rip_2objdump(map, sym->start), |
| 735 | end = map__rip_2objdump(map, sym->end); |
| 736 | |
| 737 | offset = line_ip - start; |
| 738 | if (offset < 0 || (u64)line_ip > end) |
| 739 | offset = -1; |
Arnaldo Carvalho de Melo | 058b4cc | 2012-04-02 12:59:01 -0300 | [diff] [blame] | 740 | else |
| 741 | parsed_line = tmp2 + 1; |
Namhyung Kim | a31b7cc | 2012-04-11 17:04:59 -0300 | [diff] [blame] | 742 | } |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 743 | |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 744 | dl = disasm_line__new(offset, parsed_line, privsize); |
Arnaldo Carvalho de Melo | 058b4cc | 2012-04-02 12:59:01 -0300 | [diff] [blame] | 745 | free(line); |
| 746 | |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 747 | if (dl == NULL) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 748 | return -1; |
Arnaldo Carvalho de Melo | 058b4cc | 2012-04-02 12:59:01 -0300 | [diff] [blame] | 749 | |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 750 | disasm__add(¬es->src->source, dl); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 751 | |
| 752 | return 0; |
| 753 | } |
| 754 | |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 755 | 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] | 756 | { |
| 757 | struct dso *dso = map->dso; |
| 758 | char *filename = dso__build_id_filename(dso, NULL, 0); |
| 759 | bool free_filename = true; |
| 760 | char command[PATH_MAX * 2]; |
| 761 | FILE *file; |
| 762 | int err = 0; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 763 | char symfs_filename[PATH_MAX]; |
| 764 | |
| 765 | if (filename) { |
| 766 | snprintf(symfs_filename, sizeof(symfs_filename), "%s%s", |
| 767 | symbol_conf.symfs, filename); |
| 768 | } |
| 769 | |
| 770 | if (filename == NULL) { |
| 771 | if (dso->has_build_id) { |
| 772 | pr_err("Can't annotate %s: not enough memory\n", |
| 773 | sym->name); |
| 774 | return -ENOMEM; |
| 775 | } |
| 776 | goto fallback; |
| 777 | } else if (readlink(symfs_filename, command, sizeof(command)) < 0 || |
| 778 | strstr(command, "[kernel.kallsyms]") || |
| 779 | access(symfs_filename, R_OK)) { |
| 780 | free(filename); |
| 781 | fallback: |
| 782 | /* |
| 783 | * If we don't have build-ids or the build-id file isn't in the |
| 784 | * cache, or is just a kallsyms file, well, lets hope that this |
| 785 | * DSO is the same as when 'perf record' ran. |
| 786 | */ |
| 787 | filename = dso->long_name; |
| 788 | snprintf(symfs_filename, sizeof(symfs_filename), "%s%s", |
| 789 | symbol_conf.symfs, filename); |
| 790 | free_filename = false; |
| 791 | } |
| 792 | |
Jiri Olsa | 44f24cb | 2012-07-22 14:14:32 +0200 | [diff] [blame] | 793 | if (dso->symtab_type == DSO_BINARY_TYPE__KALLSYMS) { |
Arnaldo Carvalho de Melo | 170ae6b | 2011-02-23 11:08:59 -0300 | [diff] [blame] | 794 | char bf[BUILD_ID_SIZE * 2 + 16] = " with build id "; |
| 795 | char *build_id_msg = NULL; |
| 796 | |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 797 | if (dso->annotate_warned) |
| 798 | goto out_free_filename; |
Arnaldo Carvalho de Melo | 170ae6b | 2011-02-23 11:08:59 -0300 | [diff] [blame] | 799 | |
| 800 | if (dso->has_build_id) { |
| 801 | build_id__sprintf(dso->build_id, |
| 802 | sizeof(dso->build_id), bf + 15); |
| 803 | build_id_msg = bf; |
| 804 | } |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 805 | err = -ENOENT; |
| 806 | dso->annotate_warned = 1; |
Arnaldo Carvalho de Melo | ae55795 | 2011-10-26 08:00:55 -0200 | [diff] [blame] | 807 | pr_err("Can't annotate %s:\n\n" |
| 808 | "No vmlinux file%s\nwas found in the path.\n\n" |
| 809 | "Please use:\n\n" |
| 810 | " perf buildid-cache -av vmlinux\n\n" |
| 811 | "or:\n\n" |
Namhyung Kim | ff2a661 | 2012-02-23 17:46:24 +0900 | [diff] [blame] | 812 | " --vmlinux vmlinux\n", |
Arnaldo Carvalho de Melo | 170ae6b | 2011-02-23 11:08:59 -0300 | [diff] [blame] | 813 | sym->name, build_id_msg ?: ""); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 814 | goto out_free_filename; |
| 815 | } |
| 816 | |
| 817 | pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__, |
| 818 | filename, sym->name, map->unmap_ip(map, sym->start), |
| 819 | map->unmap_ip(map, sym->end)); |
| 820 | |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 821 | pr_debug("annotating [%p] %30s : [%p] %30s\n", |
| 822 | dso, dso->long_name, sym, sym->name); |
| 823 | |
| 824 | snprintf(command, sizeof(command), |
Maciek Borzecki | 7a4ec93 | 2012-09-04 12:32:30 +0200 | [diff] [blame] | 825 | "%s %s%s --start-address=0x%016" PRIx64 |
Stephane Eranian | 3e6a2a7 | 2011-05-17 17:32:07 +0200 | [diff] [blame] | 826 | " --stop-address=0x%016" PRIx64 |
| 827 | " -d %s %s -C %s|grep -v %s|expand", |
Maciek Borzecki | 7a4ec93 | 2012-09-04 12:32:30 +0200 | [diff] [blame] | 828 | objdump_path ? objdump_path : "objdump", |
Andi Kleen | f69b64f | 2011-09-15 14:31:41 -0700 | [diff] [blame] | 829 | disassembler_style ? "-M " : "", |
| 830 | disassembler_style ? disassembler_style : "", |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 831 | map__rip_2objdump(map, sym->start), |
Ingo Molnar | f41612f | 2011-12-23 14:08:04 +0100 | [diff] [blame] | 832 | map__rip_2objdump(map, sym->end+1), |
Stephane Eranian | 3e6a2a7 | 2011-05-17 17:32:07 +0200 | [diff] [blame] | 833 | symbol_conf.annotate_asm_raw ? "" : "--no-show-raw", |
| 834 | symbol_conf.annotate_src ? "-S" : "", |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 835 | symfs_filename, filename); |
| 836 | |
| 837 | pr_debug("Executing: %s\n", command); |
| 838 | |
| 839 | file = popen(command, "r"); |
| 840 | if (!file) |
| 841 | goto out_free_filename; |
| 842 | |
| 843 | while (!feof(file)) |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 844 | if (symbol__parse_objdump_line(sym, map, file, privsize) < 0) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 845 | break; |
| 846 | |
| 847 | pclose(file); |
| 848 | out_free_filename: |
| 849 | if (free_filename) |
| 850 | free(filename); |
| 851 | return err; |
| 852 | } |
| 853 | |
| 854 | static void insert_source_line(struct rb_root *root, struct source_line *src_line) |
| 855 | { |
| 856 | struct source_line *iter; |
| 857 | struct rb_node **p = &root->rb_node; |
| 858 | struct rb_node *parent = NULL; |
| 859 | |
| 860 | while (*p != NULL) { |
| 861 | parent = *p; |
| 862 | iter = rb_entry(parent, struct source_line, node); |
| 863 | |
| 864 | if (src_line->percent > iter->percent) |
| 865 | p = &(*p)->rb_left; |
| 866 | else |
| 867 | p = &(*p)->rb_right; |
| 868 | } |
| 869 | |
| 870 | rb_link_node(&src_line->node, parent, p); |
| 871 | rb_insert_color(&src_line->node, root); |
| 872 | } |
| 873 | |
| 874 | static void symbol__free_source_line(struct symbol *sym, int len) |
| 875 | { |
| 876 | struct annotation *notes = symbol__annotation(sym); |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 877 | struct source_line *src_line = notes->src->lines; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 878 | int i; |
| 879 | |
| 880 | for (i = 0; i < len; i++) |
| 881 | free(src_line[i].path); |
| 882 | |
| 883 | free(src_line); |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 884 | notes->src->lines = NULL; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | /* Get the filename:line for the colored entries */ |
| 888 | 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] | 889 | int evidx, struct rb_root *root, int len, |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 890 | const char *filename) |
| 891 | { |
| 892 | u64 start; |
| 893 | int i; |
| 894 | char cmd[PATH_MAX * 2]; |
| 895 | struct source_line *src_line; |
| 896 | struct annotation *notes = symbol__annotation(sym); |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame] | 897 | struct sym_hist *h = annotation__histogram(notes, evidx); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 898 | |
| 899 | if (!h->sum) |
| 900 | return 0; |
| 901 | |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 902 | src_line = notes->src->lines = calloc(len, sizeof(struct source_line)); |
| 903 | if (!notes->src->lines) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 904 | return -1; |
| 905 | |
David Miller | f40a063 | 2012-03-25 16:28:12 -0400 | [diff] [blame] | 906 | start = map__rip_2objdump(map, sym->start); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 907 | |
| 908 | for (i = 0; i < len; i++) { |
| 909 | char *path = NULL; |
| 910 | size_t line_len; |
| 911 | u64 offset; |
| 912 | FILE *fp; |
| 913 | |
| 914 | src_line[i].percent = 100.0 * h->addr[i] / h->sum; |
| 915 | if (src_line[i].percent <= 0.5) |
| 916 | continue; |
| 917 | |
| 918 | offset = start + i; |
| 919 | sprintf(cmd, "addr2line -e %s %016" PRIx64, filename, offset); |
| 920 | fp = popen(cmd, "r"); |
| 921 | if (!fp) |
| 922 | continue; |
| 923 | |
| 924 | if (getline(&path, &line_len, fp) < 0 || !line_len) |
| 925 | goto next; |
| 926 | |
| 927 | src_line[i].path = malloc(sizeof(char) * line_len + 1); |
| 928 | if (!src_line[i].path) |
| 929 | goto next; |
| 930 | |
| 931 | strcpy(src_line[i].path, path); |
| 932 | insert_source_line(root, &src_line[i]); |
| 933 | |
| 934 | next: |
| 935 | pclose(fp); |
| 936 | } |
| 937 | |
| 938 | return 0; |
| 939 | } |
| 940 | |
| 941 | static void print_summary(struct rb_root *root, const char *filename) |
| 942 | { |
| 943 | struct source_line *src_line; |
| 944 | struct rb_node *node; |
| 945 | |
| 946 | printf("\nSorted summary for file %s\n", filename); |
| 947 | printf("----------------------------------------------\n\n"); |
| 948 | |
| 949 | if (RB_EMPTY_ROOT(root)) { |
| 950 | printf(" Nothing higher than %1.1f%%\n", MIN_GREEN); |
| 951 | return; |
| 952 | } |
| 953 | |
| 954 | node = rb_first(root); |
| 955 | while (node) { |
| 956 | double percent; |
| 957 | const char *color; |
| 958 | char *path; |
| 959 | |
| 960 | src_line = rb_entry(node, struct source_line, node); |
| 961 | percent = src_line->percent; |
| 962 | color = get_percent_color(percent); |
| 963 | path = src_line->path; |
| 964 | |
| 965 | color_fprintf(stdout, color, " %7.2f %s", percent, path); |
| 966 | node = rb_next(node); |
| 967 | } |
| 968 | } |
| 969 | |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame] | 970 | static void symbol__annotate_hits(struct symbol *sym, int evidx) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 971 | { |
| 972 | struct annotation *notes = symbol__annotation(sym); |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame] | 973 | struct sym_hist *h = annotation__histogram(notes, evidx); |
Arnaldo Carvalho de Melo | 1b2e2df | 2012-04-19 10:57:06 -0300 | [diff] [blame] | 974 | u64 len = symbol__size(sym), offset; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 975 | |
| 976 | for (offset = 0; offset < len; ++offset) |
| 977 | if (h->addr[offset] != 0) |
| 978 | printf("%*" PRIx64 ": %" PRIu64 "\n", BITS_PER_LONG / 2, |
| 979 | sym->start + offset, h->addr[offset]); |
| 980 | printf("%*s: %" PRIu64 "\n", BITS_PER_LONG / 2, "h->sum", h->sum); |
| 981 | } |
| 982 | |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 983 | 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] | 984 | bool full_paths, int min_pcnt, int max_lines, |
| 985 | int context) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 986 | { |
| 987 | struct dso *dso = map->dso; |
David Ahern | bfd14b9 | 2012-09-08 09:06:50 -0600 | [diff] [blame] | 988 | char *filename; |
| 989 | const char *d_filename; |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 990 | struct annotation *notes = symbol__annotation(sym); |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 991 | struct disasm_line *pos, *queue = NULL; |
Arnaldo Carvalho de Melo | 058b4cc | 2012-04-02 12:59:01 -0300 | [diff] [blame] | 992 | u64 start = map__rip_2objdump(map, sym->start); |
Arnaldo Carvalho de Melo | d5e3d74 | 2011-02-08 15:29:25 -0200 | [diff] [blame] | 993 | int printed = 2, queue_len = 0; |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 994 | int more = 0; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 995 | u64 len; |
| 996 | |
David Ahern | bfd14b9 | 2012-09-08 09:06:50 -0600 | [diff] [blame] | 997 | filename = strdup(dso->long_name); |
| 998 | if (!filename) |
| 999 | return -ENOMEM; |
| 1000 | |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1001 | if (full_paths) |
| 1002 | d_filename = filename; |
| 1003 | else |
| 1004 | d_filename = basename(filename); |
| 1005 | |
Arnaldo Carvalho de Melo | 1b2e2df | 2012-04-19 10:57:06 -0300 | [diff] [blame] | 1006 | len = symbol__size(sym); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1007 | |
Arnaldo Carvalho de Melo | f1e2701 | 2011-02-05 18:51:38 -0200 | [diff] [blame] | 1008 | printf(" Percent | Source code & Disassembly of %s\n", d_filename); |
| 1009 | printf("------------------------------------------------\n"); |
| 1010 | |
| 1011 | if (verbose) |
| 1012 | symbol__annotate_hits(sym, evidx); |
| 1013 | |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 1014 | list_for_each_entry(pos, ¬es->src->source, node) { |
Arnaldo Carvalho de Melo | d5e3d74 | 2011-02-08 15:29:25 -0200 | [diff] [blame] | 1015 | if (context && queue == NULL) { |
| 1016 | queue = pos; |
| 1017 | queue_len = 0; |
| 1018 | } |
| 1019 | |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 1020 | switch (disasm_line__print(pos, sym, start, evidx, len, |
Arnaldo Carvalho de Melo | 058b4cc | 2012-04-02 12:59:01 -0300 | [diff] [blame] | 1021 | min_pcnt, printed, max_lines, |
| 1022 | queue)) { |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 1023 | case 0: |
| 1024 | ++printed; |
Arnaldo Carvalho de Melo | d5e3d74 | 2011-02-08 15:29:25 -0200 | [diff] [blame] | 1025 | if (context) { |
| 1026 | printed += queue_len; |
| 1027 | queue = NULL; |
| 1028 | queue_len = 0; |
| 1029 | } |
Arnaldo Carvalho de Melo | f1e2701 | 2011-02-05 18:51:38 -0200 | [diff] [blame] | 1030 | break; |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 1031 | case 1: |
| 1032 | /* filtered by max_lines */ |
| 1033 | ++more; |
| 1034 | break; |
| 1035 | case -1: |
| 1036 | default: |
Arnaldo Carvalho de Melo | d5e3d74 | 2011-02-08 15:29:25 -0200 | [diff] [blame] | 1037 | /* |
| 1038 | * Filtered by min_pcnt or non IP lines when |
| 1039 | * context != 0 |
| 1040 | */ |
| 1041 | if (!context) |
| 1042 | break; |
| 1043 | if (queue_len == context) |
| 1044 | queue = list_entry(queue->node.next, typeof(*queue), node); |
| 1045 | else |
| 1046 | ++queue_len; |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 1047 | break; |
| 1048 | } |
| 1049 | } |
Arnaldo Carvalho de Melo | f1e2701 | 2011-02-05 18:51:38 -0200 | [diff] [blame] | 1050 | |
David Ahern | bfd14b9 | 2012-09-08 09:06:50 -0600 | [diff] [blame] | 1051 | free(filename); |
| 1052 | |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 1053 | return more; |
| 1054 | } |
| 1055 | |
| 1056 | void symbol__annotate_zero_histogram(struct symbol *sym, int evidx) |
| 1057 | { |
| 1058 | struct annotation *notes = symbol__annotation(sym); |
| 1059 | struct sym_hist *h = annotation__histogram(notes, evidx); |
| 1060 | |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 1061 | memset(h, 0, notes->src->sizeof_sym_hist); |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 1062 | } |
| 1063 | |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 1064 | void symbol__annotate_decay_histogram(struct symbol *sym, int evidx) |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 1065 | { |
| 1066 | struct annotation *notes = symbol__annotation(sym); |
| 1067 | struct sym_hist *h = annotation__histogram(notes, evidx); |
Arnaldo Carvalho de Melo | 1b2e2df | 2012-04-19 10:57:06 -0300 | [diff] [blame] | 1068 | int len = symbol__size(sym), offset; |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 1069 | |
| 1070 | h->sum = 0; |
Arnaldo Carvalho de Melo | 8b84a56 | 2012-04-05 16:15:59 -0300 | [diff] [blame] | 1071 | for (offset = 0; offset < len; ++offset) { |
| 1072 | h->addr[offset] = h->addr[offset] * 7 / 8; |
| 1073 | h->sum += h->addr[offset]; |
Arnaldo Carvalho de Melo | f1e2701 | 2011-02-05 18:51:38 -0200 | [diff] [blame] | 1074 | } |
| 1075 | } |
| 1076 | |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 1077 | void disasm__purge(struct list_head *head) |
Arnaldo Carvalho de Melo | f1e2701 | 2011-02-05 18:51:38 -0200 | [diff] [blame] | 1078 | { |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 1079 | struct disasm_line *pos, *n; |
Arnaldo Carvalho de Melo | f1e2701 | 2011-02-05 18:51:38 -0200 | [diff] [blame] | 1080 | |
| 1081 | list_for_each_entry_safe(pos, n, head, node) { |
| 1082 | list_del(&pos->node); |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 1083 | disasm_line__free(pos); |
Arnaldo Carvalho de Melo | f1e2701 | 2011-02-05 18:51:38 -0200 | [diff] [blame] | 1084 | } |
| 1085 | } |
| 1086 | |
Arnaldo Carvalho de Melo | 5145418 | 2012-04-15 15:52:18 -0300 | [diff] [blame] | 1087 | static size_t disasm_line__fprintf(struct disasm_line *dl, FILE *fp) |
| 1088 | { |
| 1089 | size_t printed; |
| 1090 | |
| 1091 | if (dl->offset == -1) |
| 1092 | return fprintf(fp, "%s\n", dl->line); |
| 1093 | |
| 1094 | printed = fprintf(fp, "%#" PRIx64 " %s", dl->offset, dl->name); |
| 1095 | |
Arnaldo Carvalho de Melo | c7e6ead | 2012-04-20 14:38:46 -0300 | [diff] [blame] | 1096 | if (dl->ops.raw[0] != '\0') { |
Arnaldo Carvalho de Melo | 5145418 | 2012-04-15 15:52:18 -0300 | [diff] [blame] | 1097 | printed += fprintf(fp, "%.*s %s\n", 6 - (int)printed, " ", |
Arnaldo Carvalho de Melo | c7e6ead | 2012-04-20 14:38:46 -0300 | [diff] [blame] | 1098 | dl->ops.raw); |
Arnaldo Carvalho de Melo | 5145418 | 2012-04-15 15:52:18 -0300 | [diff] [blame] | 1099 | } |
| 1100 | |
| 1101 | return printed + fprintf(fp, "\n"); |
| 1102 | } |
| 1103 | |
| 1104 | size_t disasm__fprintf(struct list_head *head, FILE *fp) |
| 1105 | { |
| 1106 | struct disasm_line *pos; |
| 1107 | size_t printed = 0; |
| 1108 | |
| 1109 | list_for_each_entry(pos, head, node) |
| 1110 | printed += disasm_line__fprintf(pos, fp); |
| 1111 | |
| 1112 | return printed; |
| 1113 | } |
| 1114 | |
Arnaldo Carvalho de Melo | f1e2701 | 2011-02-05 18:51:38 -0200 | [diff] [blame] | 1115 | int symbol__tty_annotate(struct symbol *sym, struct map *map, int evidx, |
| 1116 | bool print_lines, bool full_paths, int min_pcnt, |
| 1117 | int max_lines) |
| 1118 | { |
| 1119 | struct dso *dso = map->dso; |
| 1120 | const char *filename = dso->long_name; |
| 1121 | struct rb_root source_line = RB_ROOT; |
Arnaldo Carvalho de Melo | f1e2701 | 2011-02-05 18:51:38 -0200 | [diff] [blame] | 1122 | u64 len; |
| 1123 | |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 1124 | if (symbol__annotate(sym, map, 0) < 0) |
Arnaldo Carvalho de Melo | f1e2701 | 2011-02-05 18:51:38 -0200 | [diff] [blame] | 1125 | return -1; |
| 1126 | |
Arnaldo Carvalho de Melo | 1b2e2df | 2012-04-19 10:57:06 -0300 | [diff] [blame] | 1127 | len = symbol__size(sym); |
Arnaldo Carvalho de Melo | f1e2701 | 2011-02-05 18:51:38 -0200 | [diff] [blame] | 1128 | |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1129 | if (print_lines) { |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame] | 1130 | symbol__get_source_line(sym, map, evidx, &source_line, |
| 1131 | len, filename); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1132 | print_summary(&source_line, filename); |
| 1133 | } |
| 1134 | |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 1135 | symbol__annotate_printf(sym, map, evidx, full_paths, |
Arnaldo Carvalho de Melo | d5e3d74 | 2011-02-08 15:29:25 -0200 | [diff] [blame] | 1136 | min_pcnt, max_lines, 0); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1137 | if (print_lines) |
| 1138 | symbol__free_source_line(sym, len); |
| 1139 | |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 1140 | disasm__purge(&symbol__annotation(sym)->src->source); |
Arnaldo Carvalho de Melo | f1e2701 | 2011-02-05 18:51:38 -0200 | [diff] [blame] | 1141 | |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1142 | return 0; |
| 1143 | } |