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" |
Namhyung Kim | 48c65bd | 2014-02-20 10:32:53 +0900 | [diff] [blame] | 11 | #include "ui/ui.h" |
| 12 | #include "sort.h" |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 13 | #include "build-id.h" |
| 14 | #include "color.h" |
| 15 | #include "cache.h" |
| 16 | #include "symbol.h" |
| 17 | #include "debug.h" |
| 18 | #include "annotate.h" |
Namhyung Kim | db8fd07 | 2013-03-05 14:53:21 +0900 | [diff] [blame] | 19 | #include "evsel.h" |
Andi Kleen | e592488 | 2014-11-12 18:05:26 -0800 | [diff] [blame] | 20 | #include <regex.h> |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 21 | #include <pthread.h> |
Jiri Olsa | 4383db8 | 2012-10-27 23:18:29 +0200 | [diff] [blame] | 22 | #include <linux/bitops.h> |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 23 | |
Andi Kleen | f69b64f | 2011-09-15 14:31:41 -0700 | [diff] [blame] | 24 | const char *disassembler_style; |
Maciek Borzecki | 7a4ec93 | 2012-09-04 12:32:30 +0200 | [diff] [blame] | 25 | const char *objdump_path; |
Andi Kleen | e592488 | 2014-11-12 18:05:26 -0800 | [diff] [blame] | 26 | static regex_t file_lineno; |
Andi Kleen | f69b64f | 2011-09-15 14:31:41 -0700 | [diff] [blame] | 27 | |
Arnaldo Carvalho de Melo | 7a997fe | 2012-05-12 13:15:34 -0300 | [diff] [blame] | 28 | static struct ins *ins__find(const char *name); |
| 29 | static int disasm_line__parse(char *line, char **namep, char **rawp); |
| 30 | |
Arnaldo Carvalho de Melo | c46219ac | 2012-05-12 13:26:20 -0300 | [diff] [blame] | 31 | static void ins__delete(struct ins_operands *ops) |
| 32 | { |
Arnaldo Carvalho de Melo | 3995614 | 2015-03-05 15:27:28 -0300 | [diff] [blame] | 33 | if (ops == NULL) |
| 34 | return; |
Arnaldo Carvalho de Melo | 74cf249 | 2013-12-27 16:55:14 -0300 | [diff] [blame] | 35 | zfree(&ops->source.raw); |
| 36 | zfree(&ops->source.name); |
| 37 | zfree(&ops->target.raw); |
| 38 | zfree(&ops->target.name); |
Arnaldo Carvalho de Melo | c46219ac | 2012-05-12 13:26:20 -0300 | [diff] [blame] | 39 | } |
| 40 | |
Arnaldo Carvalho de Melo | 5417072 | 2012-05-07 18:54:16 -0300 | [diff] [blame] | 41 | static int ins__raw_scnprintf(struct ins *ins, char *bf, size_t size, |
| 42 | struct ins_operands *ops) |
| 43 | { |
| 44 | return scnprintf(bf, size, "%-6.6s %s", ins->name, ops->raw); |
| 45 | } |
| 46 | |
| 47 | int ins__scnprintf(struct ins *ins, char *bf, size_t size, |
| 48 | struct ins_operands *ops) |
| 49 | { |
| 50 | if (ins->ops->scnprintf) |
| 51 | return ins->ops->scnprintf(ins, bf, size, ops); |
| 52 | |
| 53 | return ins__raw_scnprintf(ins, bf, size, ops); |
| 54 | } |
| 55 | |
Arnaldo Carvalho de Melo | c7e6ead | 2012-04-20 14:38:46 -0300 | [diff] [blame] | 56 | static int call__parse(struct ins_operands *ops) |
Arnaldo Carvalho de Melo | d86b059 | 2012-04-18 16:07:38 -0300 | [diff] [blame] | 57 | { |
Arnaldo Carvalho de Melo | d223288 | 2012-04-20 15:26:47 -0300 | [diff] [blame] | 58 | char *endptr, *tok, *name; |
| 59 | |
Arnaldo Carvalho de Melo | 44d1a3e | 2012-04-25 08:00:23 -0300 | [diff] [blame] | 60 | ops->target.addr = strtoull(ops->raw, &endptr, 16); |
Arnaldo Carvalho de Melo | d223288 | 2012-04-20 15:26:47 -0300 | [diff] [blame] | 61 | |
| 62 | name = strchr(endptr, '<'); |
| 63 | if (name == NULL) |
| 64 | goto indirect_call; |
| 65 | |
| 66 | name++; |
| 67 | |
| 68 | tok = strchr(name, '>'); |
| 69 | if (tok == NULL) |
| 70 | return -1; |
| 71 | |
| 72 | *tok = '\0'; |
Arnaldo Carvalho de Melo | 44d1a3e | 2012-04-25 08:00:23 -0300 | [diff] [blame] | 73 | ops->target.name = strdup(name); |
Arnaldo Carvalho de Melo | d223288 | 2012-04-20 15:26:47 -0300 | [diff] [blame] | 74 | *tok = '>'; |
| 75 | |
Arnaldo Carvalho de Melo | 44d1a3e | 2012-04-25 08:00:23 -0300 | [diff] [blame] | 76 | return ops->target.name == NULL ? -1 : 0; |
Arnaldo Carvalho de Melo | d223288 | 2012-04-20 15:26:47 -0300 | [diff] [blame] | 77 | |
| 78 | indirect_call: |
Arnaldo Carvalho de Melo | e8ea156 | 2012-05-11 12:28:55 -0300 | [diff] [blame] | 79 | tok = strchr(endptr, '('); |
| 80 | if (tok != NULL) { |
| 81 | ops->target.addr = 0; |
| 82 | return 0; |
| 83 | } |
| 84 | |
Arnaldo Carvalho de Melo | d223288 | 2012-04-20 15:26:47 -0300 | [diff] [blame] | 85 | tok = strchr(endptr, '*'); |
| 86 | if (tok == NULL) |
| 87 | return -1; |
| 88 | |
Arnaldo Carvalho de Melo | 44d1a3e | 2012-04-25 08:00:23 -0300 | [diff] [blame] | 89 | ops->target.addr = strtoull(tok + 1, NULL, 16); |
Arnaldo Carvalho de Melo | d86b059 | 2012-04-18 16:07:38 -0300 | [diff] [blame] | 90 | return 0; |
| 91 | } |
| 92 | |
Arnaldo Carvalho de Melo | d223288 | 2012-04-20 15:26:47 -0300 | [diff] [blame] | 93 | 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] | 94 | struct ins_operands *ops) |
Arnaldo Carvalho de Melo | d223288 | 2012-04-20 15:26:47 -0300 | [diff] [blame] | 95 | { |
Arnaldo Carvalho de Melo | 44d1a3e | 2012-04-25 08:00:23 -0300 | [diff] [blame] | 96 | if (ops->target.name) |
| 97 | 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] | 98 | |
Arnaldo Carvalho de Melo | e8ea156 | 2012-05-11 12:28:55 -0300 | [diff] [blame] | 99 | if (ops->target.addr == 0) |
| 100 | return ins__raw_scnprintf(ins, bf, size, ops); |
| 101 | |
Arnaldo Carvalho de Melo | 44d1a3e | 2012-04-25 08:00:23 -0300 | [diff] [blame] | 102 | 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] | 103 | } |
| 104 | |
Arnaldo Carvalho de Melo | d86b059 | 2012-04-18 16:07:38 -0300 | [diff] [blame] | 105 | static struct ins_ops call_ops = { |
Arnaldo Carvalho de Melo | d223288 | 2012-04-20 15:26:47 -0300 | [diff] [blame] | 106 | .parse = call__parse, |
| 107 | .scnprintf = call__scnprintf, |
Arnaldo Carvalho de Melo | d86b059 | 2012-04-18 16:07:38 -0300 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | bool ins__is_call(const struct ins *ins) |
| 111 | { |
| 112 | return ins->ops == &call_ops; |
| 113 | } |
| 114 | |
Arnaldo Carvalho de Melo | c7e6ead | 2012-04-20 14:38:46 -0300 | [diff] [blame] | 115 | static int jump__parse(struct ins_operands *ops) |
Arnaldo Carvalho de Melo | 4f9d032 | 2012-04-18 13:58:34 -0300 | [diff] [blame] | 116 | { |
Arnaldo Carvalho de Melo | c7e6ead | 2012-04-20 14:38:46 -0300 | [diff] [blame] | 117 | const char *s = strchr(ops->raw, '+'); |
Arnaldo Carvalho de Melo | 4f9d032 | 2012-04-18 13:58:34 -0300 | [diff] [blame] | 118 | |
Adrian Hunter | bbb7f84 | 2013-08-07 14:38:54 +0300 | [diff] [blame] | 119 | ops->target.addr = strtoull(ops->raw, NULL, 16); |
Arnaldo Carvalho de Melo | 4f9d032 | 2012-04-18 13:58:34 -0300 | [diff] [blame] | 120 | |
Arnaldo Carvalho de Melo | fb29fa58 | 2012-04-25 14:16:03 -0300 | [diff] [blame] | 121 | if (s++ != NULL) |
Adrian Hunter | bbb7f84 | 2013-08-07 14:38:54 +0300 | [diff] [blame] | 122 | ops->target.offset = strtoull(s, NULL, 16); |
Arnaldo Carvalho de Melo | fb29fa58 | 2012-04-25 14:16:03 -0300 | [diff] [blame] | 123 | else |
| 124 | ops->target.offset = UINT64_MAX; |
| 125 | |
Arnaldo Carvalho de Melo | 4f9d032 | 2012-04-18 13:58:34 -0300 | [diff] [blame] | 126 | return 0; |
| 127 | } |
| 128 | |
Arnaldo Carvalho de Melo | c7e6ead | 2012-04-20 14:38:46 -0300 | [diff] [blame] | 129 | 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] | 130 | struct ins_operands *ops) |
Arnaldo Carvalho de Melo | 28548d7 | 2012-04-19 10:16:27 -0300 | [diff] [blame] | 131 | { |
Arnaldo Carvalho de Melo | 44d1a3e | 2012-04-25 08:00:23 -0300 | [diff] [blame] | 132 | 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] | 133 | } |
| 134 | |
Arnaldo Carvalho de Melo | 4f9d032 | 2012-04-18 13:58:34 -0300 | [diff] [blame] | 135 | static struct ins_ops jump_ops = { |
Arnaldo Carvalho de Melo | c7e6ead | 2012-04-20 14:38:46 -0300 | [diff] [blame] | 136 | .parse = jump__parse, |
| 137 | .scnprintf = jump__scnprintf, |
Arnaldo Carvalho de Melo | 4f9d032 | 2012-04-18 13:58:34 -0300 | [diff] [blame] | 138 | }; |
| 139 | |
| 140 | bool ins__is_jump(const struct ins *ins) |
| 141 | { |
| 142 | return ins->ops == &jump_ops; |
| 143 | } |
| 144 | |
Arnaldo Carvalho de Melo | 6de783b | 2012-05-11 16:48:49 -0300 | [diff] [blame] | 145 | static int comment__symbol(char *raw, char *comment, u64 *addrp, char **namep) |
| 146 | { |
| 147 | char *endptr, *name, *t; |
| 148 | |
| 149 | if (strstr(raw, "(%rip)") == NULL) |
| 150 | return 0; |
| 151 | |
| 152 | *addrp = strtoull(comment, &endptr, 16); |
| 153 | name = strchr(endptr, '<'); |
| 154 | if (name == NULL) |
| 155 | return -1; |
| 156 | |
| 157 | name++; |
| 158 | |
| 159 | t = strchr(name, '>'); |
| 160 | if (t == NULL) |
| 161 | return 0; |
| 162 | |
| 163 | *t = '\0'; |
| 164 | *namep = strdup(name); |
| 165 | *t = '>'; |
| 166 | |
| 167 | return 0; |
| 168 | } |
| 169 | |
Arnaldo Carvalho de Melo | 7a997fe | 2012-05-12 13:15:34 -0300 | [diff] [blame] | 170 | static int lock__parse(struct ins_operands *ops) |
| 171 | { |
| 172 | char *name; |
| 173 | |
| 174 | ops->locked.ops = zalloc(sizeof(*ops->locked.ops)); |
| 175 | if (ops->locked.ops == NULL) |
| 176 | return 0; |
| 177 | |
| 178 | if (disasm_line__parse(ops->raw, &name, &ops->locked.ops->raw) < 0) |
| 179 | goto out_free_ops; |
| 180 | |
Namhyung Kim | 2ba34aa | 2012-11-10 02:27:13 +0900 | [diff] [blame] | 181 | ops->locked.ins = ins__find(name); |
Rabin Vincent | 0fb9f2a | 2015-01-18 20:00:21 +0100 | [diff] [blame] | 182 | free(name); |
| 183 | |
Namhyung Kim | 2ba34aa | 2012-11-10 02:27:13 +0900 | [diff] [blame] | 184 | if (ops->locked.ins == NULL) |
| 185 | goto out_free_ops; |
Arnaldo Carvalho de Melo | 7a997fe | 2012-05-12 13:15:34 -0300 | [diff] [blame] | 186 | |
Namhyung Kim | 2ba34aa | 2012-11-10 02:27:13 +0900 | [diff] [blame] | 187 | if (!ops->locked.ins->ops) |
| 188 | return 0; |
Arnaldo Carvalho de Melo | 7a997fe | 2012-05-12 13:15:34 -0300 | [diff] [blame] | 189 | |
Rabin Vincent | be81908 | 2015-01-18 20:00:20 +0100 | [diff] [blame] | 190 | if (ops->locked.ins->ops->parse && |
| 191 | ops->locked.ins->ops->parse(ops->locked.ops) < 0) |
| 192 | goto out_free_ops; |
Arnaldo Carvalho de Melo | 7a997fe | 2012-05-12 13:15:34 -0300 | [diff] [blame] | 193 | |
| 194 | return 0; |
| 195 | |
| 196 | out_free_ops: |
Arnaldo Carvalho de Melo | 0466252 | 2013-12-26 17:41:15 -0300 | [diff] [blame] | 197 | zfree(&ops->locked.ops); |
Arnaldo Carvalho de Melo | 7a997fe | 2012-05-12 13:15:34 -0300 | [diff] [blame] | 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | static int lock__scnprintf(struct ins *ins, char *bf, size_t size, |
| 202 | struct ins_operands *ops) |
| 203 | { |
| 204 | int printed; |
| 205 | |
| 206 | if (ops->locked.ins == NULL) |
| 207 | return ins__raw_scnprintf(ins, bf, size, ops); |
| 208 | |
| 209 | printed = scnprintf(bf, size, "%-6.6s ", ins->name); |
| 210 | return printed + ins__scnprintf(ops->locked.ins, bf + printed, |
| 211 | size - printed, ops->locked.ops); |
| 212 | } |
| 213 | |
Arnaldo Carvalho de Melo | c46219ac | 2012-05-12 13:26:20 -0300 | [diff] [blame] | 214 | static void lock__delete(struct ins_operands *ops) |
| 215 | { |
Rabin Vincent | 0fb9f2a | 2015-01-18 20:00:21 +0100 | [diff] [blame] | 216 | struct ins *ins = ops->locked.ins; |
| 217 | |
| 218 | if (ins && ins->ops->free) |
| 219 | ins->ops->free(ops->locked.ops); |
| 220 | else |
| 221 | ins__delete(ops->locked.ops); |
| 222 | |
Arnaldo Carvalho de Melo | 74cf249 | 2013-12-27 16:55:14 -0300 | [diff] [blame] | 223 | zfree(&ops->locked.ops); |
| 224 | zfree(&ops->target.raw); |
| 225 | zfree(&ops->target.name); |
Arnaldo Carvalho de Melo | c46219ac | 2012-05-12 13:26:20 -0300 | [diff] [blame] | 226 | } |
| 227 | |
Arnaldo Carvalho de Melo | 7a997fe | 2012-05-12 13:15:34 -0300 | [diff] [blame] | 228 | static struct ins_ops lock_ops = { |
Arnaldo Carvalho de Melo | c46219ac | 2012-05-12 13:26:20 -0300 | [diff] [blame] | 229 | .free = lock__delete, |
Arnaldo Carvalho de Melo | 7a997fe | 2012-05-12 13:15:34 -0300 | [diff] [blame] | 230 | .parse = lock__parse, |
| 231 | .scnprintf = lock__scnprintf, |
| 232 | }; |
| 233 | |
Arnaldo Carvalho de Melo | 6de783b | 2012-05-11 16:48:49 -0300 | [diff] [blame] | 234 | static int mov__parse(struct ins_operands *ops) |
| 235 | { |
| 236 | char *s = strchr(ops->raw, ','), *target, *comment, prev; |
| 237 | |
| 238 | if (s == NULL) |
| 239 | return -1; |
| 240 | |
| 241 | *s = '\0'; |
| 242 | ops->source.raw = strdup(ops->raw); |
| 243 | *s = ','; |
Arnaldo Carvalho de Melo | 48000a1 | 2014-12-17 17:24:45 -0300 | [diff] [blame] | 244 | |
Arnaldo Carvalho de Melo | 6de783b | 2012-05-11 16:48:49 -0300 | [diff] [blame] | 245 | if (ops->source.raw == NULL) |
| 246 | return -1; |
| 247 | |
| 248 | target = ++s; |
Alex Converse | 1e2bb04 | 2014-08-14 14:03:00 -0700 | [diff] [blame] | 249 | comment = strchr(s, '#'); |
Arnaldo Carvalho de Melo | 6de783b | 2012-05-11 16:48:49 -0300 | [diff] [blame] | 250 | |
Alex Converse | 1e2bb04 | 2014-08-14 14:03:00 -0700 | [diff] [blame] | 251 | if (comment != NULL) |
| 252 | s = comment - 1; |
| 253 | else |
| 254 | s = strchr(s, '\0') - 1; |
| 255 | |
| 256 | while (s > target && isspace(s[0])) |
| 257 | --s; |
| 258 | s++; |
Arnaldo Carvalho de Melo | 6de783b | 2012-05-11 16:48:49 -0300 | [diff] [blame] | 259 | prev = *s; |
| 260 | *s = '\0'; |
| 261 | |
| 262 | ops->target.raw = strdup(target); |
| 263 | *s = prev; |
| 264 | |
| 265 | if (ops->target.raw == NULL) |
| 266 | goto out_free_source; |
| 267 | |
Arnaldo Carvalho de Melo | 6de783b | 2012-05-11 16:48:49 -0300 | [diff] [blame] | 268 | if (comment == NULL) |
| 269 | return 0; |
| 270 | |
| 271 | while (comment[0] != '\0' && isspace(comment[0])) |
| 272 | ++comment; |
| 273 | |
| 274 | comment__symbol(ops->source.raw, comment, &ops->source.addr, &ops->source.name); |
| 275 | comment__symbol(ops->target.raw, comment, &ops->target.addr, &ops->target.name); |
| 276 | |
| 277 | return 0; |
| 278 | |
| 279 | out_free_source: |
Arnaldo Carvalho de Melo | 0466252 | 2013-12-26 17:41:15 -0300 | [diff] [blame] | 280 | zfree(&ops->source.raw); |
Arnaldo Carvalho de Melo | 6de783b | 2012-05-11 16:48:49 -0300 | [diff] [blame] | 281 | return -1; |
| 282 | } |
| 283 | |
| 284 | static int mov__scnprintf(struct ins *ins, char *bf, size_t size, |
| 285 | struct ins_operands *ops) |
| 286 | { |
| 287 | return scnprintf(bf, size, "%-6.6s %s,%s", ins->name, |
| 288 | ops->source.name ?: ops->source.raw, |
| 289 | ops->target.name ?: ops->target.raw); |
| 290 | } |
| 291 | |
| 292 | static struct ins_ops mov_ops = { |
| 293 | .parse = mov__parse, |
| 294 | .scnprintf = mov__scnprintf, |
| 295 | }; |
| 296 | |
Arnaldo Carvalho de Melo | a43712c | 2012-05-11 17:21:09 -0300 | [diff] [blame] | 297 | static int dec__parse(struct ins_operands *ops) |
| 298 | { |
| 299 | char *target, *comment, *s, prev; |
| 300 | |
| 301 | target = s = ops->raw; |
| 302 | |
| 303 | while (s[0] != '\0' && !isspace(s[0])) |
| 304 | ++s; |
| 305 | prev = *s; |
| 306 | *s = '\0'; |
| 307 | |
| 308 | ops->target.raw = strdup(target); |
| 309 | *s = prev; |
| 310 | |
| 311 | if (ops->target.raw == NULL) |
| 312 | return -1; |
| 313 | |
| 314 | comment = strchr(s, '#'); |
| 315 | if (comment == NULL) |
| 316 | return 0; |
| 317 | |
| 318 | while (comment[0] != '\0' && isspace(comment[0])) |
| 319 | ++comment; |
| 320 | |
| 321 | comment__symbol(ops->target.raw, comment, &ops->target.addr, &ops->target.name); |
| 322 | |
| 323 | return 0; |
| 324 | } |
| 325 | |
| 326 | static int dec__scnprintf(struct ins *ins, char *bf, size_t size, |
| 327 | struct ins_operands *ops) |
| 328 | { |
| 329 | return scnprintf(bf, size, "%-6.6s %s", ins->name, |
| 330 | ops->target.name ?: ops->target.raw); |
| 331 | } |
| 332 | |
| 333 | static struct ins_ops dec_ops = { |
| 334 | .parse = dec__parse, |
| 335 | .scnprintf = dec__scnprintf, |
| 336 | }; |
| 337 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 338 | static int nop__scnprintf(struct ins *ins __maybe_unused, char *bf, size_t size, |
| 339 | struct ins_operands *ops __maybe_unused) |
Arnaldo Carvalho de Melo | b9818e9 | 2012-05-07 18:57:02 -0300 | [diff] [blame] | 340 | { |
| 341 | return scnprintf(bf, size, "%-6.6s", "nop"); |
| 342 | } |
| 343 | |
| 344 | static struct ins_ops nop_ops = { |
| 345 | .scnprintf = nop__scnprintf, |
| 346 | }; |
| 347 | |
Arnaldo Carvalho de Melo | 4f9d032 | 2012-04-18 13:58:34 -0300 | [diff] [blame] | 348 | /* |
| 349 | * Must be sorted by name! |
| 350 | */ |
| 351 | static struct ins instructions[] = { |
Arnaldo Carvalho de Melo | 6de783b | 2012-05-11 16:48:49 -0300 | [diff] [blame] | 352 | { .name = "add", .ops = &mov_ops, }, |
| 353 | { .name = "addl", .ops = &mov_ops, }, |
| 354 | { .name = "addq", .ops = &mov_ops, }, |
| 355 | { .name = "addw", .ops = &mov_ops, }, |
| 356 | { .name = "and", .ops = &mov_ops, }, |
Arnaldo Carvalho de Melo | 7a997fe | 2012-05-12 13:15:34 -0300 | [diff] [blame] | 357 | { .name = "bts", .ops = &mov_ops, }, |
Arnaldo Carvalho de Melo | d86b059 | 2012-04-18 16:07:38 -0300 | [diff] [blame] | 358 | { .name = "call", .ops = &call_ops, }, |
| 359 | { .name = "callq", .ops = &call_ops, }, |
Arnaldo Carvalho de Melo | 6de783b | 2012-05-11 16:48:49 -0300 | [diff] [blame] | 360 | { .name = "cmp", .ops = &mov_ops, }, |
| 361 | { .name = "cmpb", .ops = &mov_ops, }, |
| 362 | { .name = "cmpl", .ops = &mov_ops, }, |
| 363 | { .name = "cmpq", .ops = &mov_ops, }, |
| 364 | { .name = "cmpw", .ops = &mov_ops, }, |
| 365 | { .name = "cmpxch", .ops = &mov_ops, }, |
Arnaldo Carvalho de Melo | a43712c | 2012-05-11 17:21:09 -0300 | [diff] [blame] | 366 | { .name = "dec", .ops = &dec_ops, }, |
| 367 | { .name = "decl", .ops = &dec_ops, }, |
Arnaldo Carvalho de Melo | 6de783b | 2012-05-11 16:48:49 -0300 | [diff] [blame] | 368 | { .name = "imul", .ops = &mov_ops, }, |
Arnaldo Carvalho de Melo | a43712c | 2012-05-11 17:21:09 -0300 | [diff] [blame] | 369 | { .name = "inc", .ops = &dec_ops, }, |
| 370 | { .name = "incl", .ops = &dec_ops, }, |
Arnaldo Carvalho de Melo | 4f9d032 | 2012-04-18 13:58:34 -0300 | [diff] [blame] | 371 | { .name = "ja", .ops = &jump_ops, }, |
Arnaldo Carvalho de Melo | 3f862fd | 2012-04-19 17:10:12 -0300 | [diff] [blame] | 372 | { .name = "jae", .ops = &jump_ops, }, |
| 373 | { .name = "jb", .ops = &jump_ops, }, |
| 374 | { .name = "jbe", .ops = &jump_ops, }, |
| 375 | { .name = "jc", .ops = &jump_ops, }, |
| 376 | { .name = "jcxz", .ops = &jump_ops, }, |
Arnaldo Carvalho de Melo | 4f9d032 | 2012-04-18 13:58:34 -0300 | [diff] [blame] | 377 | { .name = "je", .ops = &jump_ops, }, |
Arnaldo Carvalho de Melo | 3f862fd | 2012-04-19 17:10:12 -0300 | [diff] [blame] | 378 | { .name = "jecxz", .ops = &jump_ops, }, |
| 379 | { .name = "jg", .ops = &jump_ops, }, |
| 380 | { .name = "jge", .ops = &jump_ops, }, |
| 381 | { .name = "jl", .ops = &jump_ops, }, |
| 382 | { .name = "jle", .ops = &jump_ops, }, |
Arnaldo Carvalho de Melo | 4f9d032 | 2012-04-18 13:58:34 -0300 | [diff] [blame] | 383 | { .name = "jmp", .ops = &jump_ops, }, |
| 384 | { .name = "jmpq", .ops = &jump_ops, }, |
Arnaldo Carvalho de Melo | 3f862fd | 2012-04-19 17:10:12 -0300 | [diff] [blame] | 385 | { .name = "jna", .ops = &jump_ops, }, |
| 386 | { .name = "jnae", .ops = &jump_ops, }, |
| 387 | { .name = "jnb", .ops = &jump_ops, }, |
| 388 | { .name = "jnbe", .ops = &jump_ops, }, |
| 389 | { .name = "jnc", .ops = &jump_ops, }, |
Arnaldo Carvalho de Melo | 4f9d032 | 2012-04-18 13:58:34 -0300 | [diff] [blame] | 390 | { .name = "jne", .ops = &jump_ops, }, |
Arnaldo Carvalho de Melo | 3f862fd | 2012-04-19 17:10:12 -0300 | [diff] [blame] | 391 | { .name = "jng", .ops = &jump_ops, }, |
| 392 | { .name = "jnge", .ops = &jump_ops, }, |
| 393 | { .name = "jnl", .ops = &jump_ops, }, |
| 394 | { .name = "jnle", .ops = &jump_ops, }, |
| 395 | { .name = "jno", .ops = &jump_ops, }, |
| 396 | { .name = "jnp", .ops = &jump_ops, }, |
| 397 | { .name = "jns", .ops = &jump_ops, }, |
| 398 | { .name = "jnz", .ops = &jump_ops, }, |
| 399 | { .name = "jo", .ops = &jump_ops, }, |
| 400 | { .name = "jp", .ops = &jump_ops, }, |
| 401 | { .name = "jpe", .ops = &jump_ops, }, |
| 402 | { .name = "jpo", .ops = &jump_ops, }, |
| 403 | { .name = "jrcxz", .ops = &jump_ops, }, |
Arnaldo Carvalho de Melo | 4f9d032 | 2012-04-18 13:58:34 -0300 | [diff] [blame] | 404 | { .name = "js", .ops = &jump_ops, }, |
Arnaldo Carvalho de Melo | 3f862fd | 2012-04-19 17:10:12 -0300 | [diff] [blame] | 405 | { .name = "jz", .ops = &jump_ops, }, |
Arnaldo Carvalho de Melo | 6de783b | 2012-05-11 16:48:49 -0300 | [diff] [blame] | 406 | { .name = "lea", .ops = &mov_ops, }, |
Arnaldo Carvalho de Melo | 7a997fe | 2012-05-12 13:15:34 -0300 | [diff] [blame] | 407 | { .name = "lock", .ops = &lock_ops, }, |
Arnaldo Carvalho de Melo | 6de783b | 2012-05-11 16:48:49 -0300 | [diff] [blame] | 408 | { .name = "mov", .ops = &mov_ops, }, |
| 409 | { .name = "movb", .ops = &mov_ops, }, |
| 410 | { .name = "movdqa",.ops = &mov_ops, }, |
| 411 | { .name = "movl", .ops = &mov_ops, }, |
| 412 | { .name = "movq", .ops = &mov_ops, }, |
| 413 | { .name = "movslq", .ops = &mov_ops, }, |
| 414 | { .name = "movzbl", .ops = &mov_ops, }, |
| 415 | { .name = "movzwl", .ops = &mov_ops, }, |
Arnaldo Carvalho de Melo | b9818e9 | 2012-05-07 18:57:02 -0300 | [diff] [blame] | 416 | { .name = "nop", .ops = &nop_ops, }, |
| 417 | { .name = "nopl", .ops = &nop_ops, }, |
| 418 | { .name = "nopw", .ops = &nop_ops, }, |
Arnaldo Carvalho de Melo | 6de783b | 2012-05-11 16:48:49 -0300 | [diff] [blame] | 419 | { .name = "or", .ops = &mov_ops, }, |
| 420 | { .name = "orl", .ops = &mov_ops, }, |
| 421 | { .name = "test", .ops = &mov_ops, }, |
| 422 | { .name = "testb", .ops = &mov_ops, }, |
| 423 | { .name = "testl", .ops = &mov_ops, }, |
Arnaldo Carvalho de Melo | 7a997fe | 2012-05-12 13:15:34 -0300 | [diff] [blame] | 424 | { .name = "xadd", .ops = &mov_ops, }, |
Andi Kleen | ffadcf0 | 2012-10-30 17:34:08 -0700 | [diff] [blame] | 425 | { .name = "xbeginl", .ops = &jump_ops, }, |
| 426 | { .name = "xbeginq", .ops = &jump_ops, }, |
Arnaldo Carvalho de Melo | 4f9d032 | 2012-04-18 13:58:34 -0300 | [diff] [blame] | 427 | }; |
| 428 | |
| 429 | static int ins__cmp(const void *name, const void *insp) |
| 430 | { |
| 431 | const struct ins *ins = insp; |
| 432 | |
| 433 | return strcmp(name, ins->name); |
| 434 | } |
| 435 | |
| 436 | static struct ins *ins__find(const char *name) |
| 437 | { |
| 438 | const int nmemb = ARRAY_SIZE(instructions); |
| 439 | |
| 440 | return bsearch(name, instructions, nmemb, sizeof(struct ins), ins__cmp); |
| 441 | } |
| 442 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 443 | 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] | 444 | { |
| 445 | struct annotation *notes = symbol__annotation(sym); |
| 446 | pthread_mutex_init(¬es->lock, NULL); |
| 447 | return 0; |
| 448 | } |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 449 | |
Arnaldo Carvalho de Melo | d04b35f | 2011-11-11 22:17:32 -0200 | [diff] [blame] | 450 | int symbol__alloc_hist(struct symbol *sym) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 451 | { |
| 452 | struct annotation *notes = symbol__annotation(sym); |
Arnaldo Carvalho de Melo | 1b2e2df | 2012-04-19 10:57:06 -0300 | [diff] [blame] | 453 | const size_t size = symbol__size(sym); |
Cody Schafer | 8696329 | 2012-07-19 20:05:25 -0700 | [diff] [blame] | 454 | size_t sizeof_sym_hist; |
| 455 | |
| 456 | /* Check for overflow when calculating sizeof_sym_hist */ |
| 457 | if (size > (SIZE_MAX - sizeof(struct sym_hist)) / sizeof(u64)) |
| 458 | return -1; |
| 459 | |
| 460 | sizeof_sym_hist = (sizeof(struct sym_hist) + size * sizeof(u64)); |
| 461 | |
| 462 | /* Check for overflow in zalloc argument */ |
| 463 | if (sizeof_sym_hist > (SIZE_MAX - sizeof(*notes->src)) |
| 464 | / symbol_conf.nr_events) |
| 465 | return -1; |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 466 | |
Arnaldo Carvalho de Melo | d04b35f | 2011-11-11 22:17:32 -0200 | [diff] [blame] | 467 | 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] | 468 | if (notes->src == NULL) |
| 469 | return -1; |
| 470 | notes->src->sizeof_sym_hist = sizeof_sym_hist; |
Arnaldo Carvalho de Melo | d04b35f | 2011-11-11 22:17:32 -0200 | [diff] [blame] | 471 | notes->src->nr_histograms = symbol_conf.nr_events; |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 472 | INIT_LIST_HEAD(¬es->src->source); |
| 473 | return 0; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 474 | } |
| 475 | |
Andi Kleen | d495763 | 2015-07-18 08:24:48 -0700 | [diff] [blame^] | 476 | /* The cycles histogram is lazily allocated. */ |
| 477 | static int symbol__alloc_hist_cycles(struct symbol *sym) |
| 478 | { |
| 479 | struct annotation *notes = symbol__annotation(sym); |
| 480 | const size_t size = symbol__size(sym); |
| 481 | |
| 482 | notes->src->cycles_hist = calloc(size, sizeof(struct cyc_hist)); |
| 483 | if (notes->src->cycles_hist == NULL) |
| 484 | return -1; |
| 485 | return 0; |
| 486 | } |
| 487 | |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 488 | void symbol__annotate_zero_histograms(struct symbol *sym) |
| 489 | { |
| 490 | struct annotation *notes = symbol__annotation(sym); |
| 491 | |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 492 | pthread_mutex_lock(¬es->lock); |
Andi Kleen | d495763 | 2015-07-18 08:24:48 -0700 | [diff] [blame^] | 493 | if (notes->src != NULL) { |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 494 | memset(notes->src->histograms, 0, |
| 495 | notes->src->nr_histograms * notes->src->sizeof_sym_hist); |
Andi Kleen | d495763 | 2015-07-18 08:24:48 -0700 | [diff] [blame^] | 496 | if (notes->src->cycles_hist) |
| 497 | memset(notes->src->cycles_hist, 0, |
| 498 | symbol__size(sym) * sizeof(struct cyc_hist)); |
| 499 | } |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 500 | pthread_mutex_unlock(¬es->lock); |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 501 | } |
| 502 | |
Andi Kleen | d495763 | 2015-07-18 08:24:48 -0700 | [diff] [blame^] | 503 | static int __symbol__account_cycles(struct annotation *notes, |
| 504 | u64 start, |
| 505 | unsigned offset, unsigned cycles, |
| 506 | unsigned have_start) |
| 507 | { |
| 508 | struct cyc_hist *ch; |
| 509 | |
| 510 | ch = notes->src->cycles_hist; |
| 511 | /* |
| 512 | * For now we can only account one basic block per |
| 513 | * final jump. But multiple could be overlapping. |
| 514 | * Always account the longest one. So when |
| 515 | * a shorter one has been already seen throw it away. |
| 516 | * |
| 517 | * We separately always account the full cycles. |
| 518 | */ |
| 519 | ch[offset].num_aggr++; |
| 520 | ch[offset].cycles_aggr += cycles; |
| 521 | |
| 522 | if (!have_start && ch[offset].have_start) |
| 523 | return 0; |
| 524 | if (ch[offset].num) { |
| 525 | if (have_start && (!ch[offset].have_start || |
| 526 | ch[offset].start > start)) { |
| 527 | ch[offset].have_start = 0; |
| 528 | ch[offset].cycles = 0; |
| 529 | ch[offset].num = 0; |
| 530 | if (ch[offset].reset < 0xffff) |
| 531 | ch[offset].reset++; |
| 532 | } else if (have_start && |
| 533 | ch[offset].start < start) |
| 534 | return 0; |
| 535 | } |
| 536 | ch[offset].have_start = have_start; |
| 537 | ch[offset].start = start; |
| 538 | ch[offset].cycles += cycles; |
| 539 | ch[offset].num++; |
| 540 | return 0; |
| 541 | } |
| 542 | |
Arnaldo Carvalho de Melo | b66d8c0 | 2013-12-18 15:37:41 -0300 | [diff] [blame] | 543 | static int __symbol__inc_addr_samples(struct symbol *sym, struct map *map, |
| 544 | struct annotation *notes, int evidx, u64 addr) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 545 | { |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame] | 546 | unsigned offset; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 547 | struct sym_hist *h; |
| 548 | |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 549 | pr_debug3("%s: addr=%#" PRIx64 "\n", __func__, map->unmap_ip(map, addr)); |
| 550 | |
Arnaldo Carvalho de Melo | 2c241bd | 2014-10-14 17:19:44 -0300 | [diff] [blame] | 551 | if (addr < sym->start || addr >= sym->end) |
Arnaldo Carvalho de Melo | 31d68e7 | 2012-03-27 12:55:57 -0300 | [diff] [blame] | 552 | return -ERANGE; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 553 | |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame] | 554 | offset = addr - sym->start; |
| 555 | h = annotation__histogram(notes, evidx); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 556 | h->sum++; |
| 557 | h->addr[offset]++; |
| 558 | |
| 559 | pr_debug3("%#" PRIx64 " %s: period++ [addr: %#" PRIx64 ", %#" PRIx64 |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame] | 560 | ", evidx=%d] => %" PRIu64 "\n", sym->start, sym->name, |
| 561 | addr, addr - sym->start, evidx, h->addr[offset]); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 562 | return 0; |
| 563 | } |
| 564 | |
Andi Kleen | d495763 | 2015-07-18 08:24:48 -0700 | [diff] [blame^] | 565 | static struct annotation *symbol__get_annotation(struct symbol *sym, bool cycles) |
Andi Kleen | 83be34a | 2015-05-27 10:51:46 -0700 | [diff] [blame] | 566 | { |
| 567 | struct annotation *notes = symbol__annotation(sym); |
| 568 | |
| 569 | if (notes->src == NULL) { |
| 570 | if (symbol__alloc_hist(sym) < 0) |
| 571 | return NULL; |
| 572 | } |
Andi Kleen | d495763 | 2015-07-18 08:24:48 -0700 | [diff] [blame^] | 573 | if (!notes->src->cycles_hist && cycles) { |
| 574 | if (symbol__alloc_hist_cycles(sym) < 0) |
| 575 | return NULL; |
| 576 | } |
Andi Kleen | 83be34a | 2015-05-27 10:51:46 -0700 | [diff] [blame] | 577 | return notes; |
| 578 | } |
| 579 | |
Arnaldo Carvalho de Melo | 44e8303 | 2013-12-18 17:12:24 -0300 | [diff] [blame] | 580 | static int symbol__inc_addr_samples(struct symbol *sym, struct map *map, |
| 581 | int evidx, u64 addr) |
Arnaldo Carvalho de Melo | b66d8c0 | 2013-12-18 15:37:41 -0300 | [diff] [blame] | 582 | { |
| 583 | struct annotation *notes; |
| 584 | |
Namhyung Kim | 48c65bd | 2014-02-20 10:32:53 +0900 | [diff] [blame] | 585 | if (sym == NULL) |
Arnaldo Carvalho de Melo | b66d8c0 | 2013-12-18 15:37:41 -0300 | [diff] [blame] | 586 | return 0; |
Andi Kleen | d495763 | 2015-07-18 08:24:48 -0700 | [diff] [blame^] | 587 | notes = symbol__get_annotation(sym, false); |
Andi Kleen | 83be34a | 2015-05-27 10:51:46 -0700 | [diff] [blame] | 588 | if (notes == NULL) |
| 589 | return -ENOMEM; |
Arnaldo Carvalho de Melo | b66d8c0 | 2013-12-18 15:37:41 -0300 | [diff] [blame] | 590 | return __symbol__inc_addr_samples(sym, map, notes, evidx, addr); |
| 591 | } |
| 592 | |
Andi Kleen | d495763 | 2015-07-18 08:24:48 -0700 | [diff] [blame^] | 593 | static int symbol__account_cycles(u64 addr, u64 start, |
| 594 | struct symbol *sym, unsigned cycles) |
| 595 | { |
| 596 | struct annotation *notes; |
| 597 | unsigned offset; |
| 598 | |
| 599 | if (sym == NULL) |
| 600 | return 0; |
| 601 | notes = symbol__get_annotation(sym, true); |
| 602 | if (notes == NULL) |
| 603 | return -ENOMEM; |
| 604 | if (addr < sym->start || addr >= sym->end) |
| 605 | return -ERANGE; |
| 606 | |
| 607 | if (start) { |
| 608 | if (start < sym->start || start >= sym->end) |
| 609 | return -ERANGE; |
| 610 | if (start >= addr) |
| 611 | start = 0; |
| 612 | } |
| 613 | offset = addr - sym->start; |
| 614 | return __symbol__account_cycles(notes, |
| 615 | start ? start - sym->start : 0, |
| 616 | offset, cycles, |
| 617 | !!start); |
| 618 | } |
| 619 | |
| 620 | int addr_map_symbol__account_cycles(struct addr_map_symbol *ams, |
| 621 | struct addr_map_symbol *start, |
| 622 | unsigned cycles) |
| 623 | { |
| 624 | unsigned long saddr = 0; |
| 625 | int err; |
| 626 | |
| 627 | if (!cycles) |
| 628 | return 0; |
| 629 | |
| 630 | /* |
| 631 | * Only set start when IPC can be computed. We can only |
| 632 | * compute it when the basic block is completely in a single |
| 633 | * function. |
| 634 | * Special case the case when the jump is elsewhere, but |
| 635 | * it starts on the function start. |
| 636 | */ |
| 637 | if (start && |
| 638 | (start->sym == ams->sym || |
| 639 | (ams->sym && |
| 640 | start->addr == ams->sym->start + ams->map->start))) |
| 641 | saddr = start->al_addr; |
| 642 | if (saddr == 0) |
| 643 | pr_debug2("BB with bad start: addr %lx start %lx sym %lx saddr %lx\n", |
| 644 | ams->addr, |
| 645 | start ? start->addr : 0, |
| 646 | ams->sym ? ams->sym->start + ams->map->start : 0, |
| 647 | saddr); |
| 648 | err = symbol__account_cycles(ams->al_addr, saddr, ams->sym, cycles); |
| 649 | if (err) |
| 650 | pr_debug2("account_cycles failed %d\n", err); |
| 651 | return err; |
| 652 | } |
| 653 | |
Arnaldo Carvalho de Melo | 0f4e7a2 | 2013-12-18 16:48:29 -0300 | [diff] [blame] | 654 | int addr_map_symbol__inc_samples(struct addr_map_symbol *ams, int evidx) |
| 655 | { |
| 656 | return symbol__inc_addr_samples(ams->sym, ams->map, evidx, ams->al_addr); |
| 657 | } |
| 658 | |
Arnaldo Carvalho de Melo | f626adf | 2013-12-18 17:10:15 -0300 | [diff] [blame] | 659 | int hist_entry__inc_addr_samples(struct hist_entry *he, int evidx, u64 ip) |
| 660 | { |
| 661 | return symbol__inc_addr_samples(he->ms.sym, he->ms.map, evidx, ip); |
| 662 | } |
| 663 | |
Arnaldo Carvalho de Melo | 4f9d032 | 2012-04-18 13:58:34 -0300 | [diff] [blame] | 664 | static void disasm_line__init_ins(struct disasm_line *dl) |
| 665 | { |
| 666 | dl->ins = ins__find(dl->name); |
| 667 | |
| 668 | if (dl->ins == NULL) |
| 669 | return; |
| 670 | |
| 671 | if (!dl->ins->ops) |
| 672 | return; |
| 673 | |
Rabin Vincent | be81908 | 2015-01-18 20:00:20 +0100 | [diff] [blame] | 674 | if (dl->ins->ops->parse && dl->ins->ops->parse(&dl->ops) < 0) |
| 675 | dl->ins = NULL; |
Arnaldo Carvalho de Melo | 4f9d032 | 2012-04-18 13:58:34 -0300 | [diff] [blame] | 676 | } |
| 677 | |
Arnaldo Carvalho de Melo | 7a997fe | 2012-05-12 13:15:34 -0300 | [diff] [blame] | 678 | static int disasm_line__parse(char *line, char **namep, char **rawp) |
| 679 | { |
| 680 | char *name = line, tmp; |
| 681 | |
| 682 | while (isspace(name[0])) |
| 683 | ++name; |
| 684 | |
| 685 | if (name[0] == '\0') |
| 686 | return -1; |
| 687 | |
| 688 | *rawp = name + 1; |
| 689 | |
| 690 | while ((*rawp)[0] != '\0' && !isspace((*rawp)[0])) |
| 691 | ++*rawp; |
| 692 | |
| 693 | tmp = (*rawp)[0]; |
| 694 | (*rawp)[0] = '\0'; |
| 695 | *namep = strdup(name); |
| 696 | |
| 697 | if (*namep == NULL) |
| 698 | goto out_free_name; |
| 699 | |
| 700 | (*rawp)[0] = tmp; |
| 701 | |
| 702 | if ((*rawp)[0] != '\0') { |
| 703 | (*rawp)++; |
| 704 | while (isspace((*rawp)[0])) |
| 705 | ++(*rawp); |
| 706 | } |
| 707 | |
| 708 | return 0; |
| 709 | |
| 710 | out_free_name: |
Arnaldo Carvalho de Melo | 0466252 | 2013-12-26 17:41:15 -0300 | [diff] [blame] | 711 | zfree(namep); |
Arnaldo Carvalho de Melo | 7a997fe | 2012-05-12 13:15:34 -0300 | [diff] [blame] | 712 | return -1; |
| 713 | } |
| 714 | |
Andi Kleen | e592488 | 2014-11-12 18:05:26 -0800 | [diff] [blame] | 715 | static struct disasm_line *disasm_line__new(s64 offset, char *line, |
| 716 | size_t privsize, int line_nr) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 717 | { |
Arnaldo Carvalho de Melo | 5145418 | 2012-04-15 15:52:18 -0300 | [diff] [blame] | 718 | struct disasm_line *dl = zalloc(sizeof(*dl) + privsize); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 719 | |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 720 | if (dl != NULL) { |
| 721 | dl->offset = offset; |
| 722 | dl->line = strdup(line); |
Andi Kleen | e592488 | 2014-11-12 18:05:26 -0800 | [diff] [blame] | 723 | dl->line_nr = line_nr; |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 724 | if (dl->line == NULL) |
Arnaldo Carvalho de Melo | 058b4cc | 2012-04-02 12:59:01 -0300 | [diff] [blame] | 725 | goto out_delete; |
Arnaldo Carvalho de Melo | 5145418 | 2012-04-15 15:52:18 -0300 | [diff] [blame] | 726 | |
| 727 | if (offset != -1) { |
Arnaldo Carvalho de Melo | 7a997fe | 2012-05-12 13:15:34 -0300 | [diff] [blame] | 728 | 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] | 729 | goto out_free_line; |
| 730 | |
Arnaldo Carvalho de Melo | 4f9d032 | 2012-04-18 13:58:34 -0300 | [diff] [blame] | 731 | disasm_line__init_ins(dl); |
Arnaldo Carvalho de Melo | 5145418 | 2012-04-15 15:52:18 -0300 | [diff] [blame] | 732 | } |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 733 | } |
| 734 | |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 735 | return dl; |
Arnaldo Carvalho de Melo | 5145418 | 2012-04-15 15:52:18 -0300 | [diff] [blame] | 736 | |
| 737 | out_free_line: |
Arnaldo Carvalho de Melo | 74cf249 | 2013-12-27 16:55:14 -0300 | [diff] [blame] | 738 | zfree(&dl->line); |
Arnaldo Carvalho de Melo | 058b4cc | 2012-04-02 12:59:01 -0300 | [diff] [blame] | 739 | out_delete: |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 740 | free(dl); |
Arnaldo Carvalho de Melo | 058b4cc | 2012-04-02 12:59:01 -0300 | [diff] [blame] | 741 | return NULL; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 742 | } |
| 743 | |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 744 | void disasm_line__free(struct disasm_line *dl) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 745 | { |
Arnaldo Carvalho de Melo | 74cf249 | 2013-12-27 16:55:14 -0300 | [diff] [blame] | 746 | zfree(&dl->line); |
| 747 | zfree(&dl->name); |
Arnaldo Carvalho de Melo | c46219ac | 2012-05-12 13:26:20 -0300 | [diff] [blame] | 748 | if (dl->ins && dl->ins->ops->free) |
| 749 | dl->ins->ops->free(&dl->ops); |
| 750 | else |
| 751 | ins__delete(&dl->ops); |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 752 | free(dl); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 753 | } |
| 754 | |
Arnaldo Carvalho de Melo | 5417072 | 2012-05-07 18:54:16 -0300 | [diff] [blame] | 755 | int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size, bool raw) |
| 756 | { |
| 757 | if (raw || !dl->ins) |
| 758 | return scnprintf(bf, size, "%-6.6s %s", dl->name, dl->ops.raw); |
| 759 | |
| 760 | return ins__scnprintf(dl->ins, bf, size, &dl->ops); |
| 761 | } |
| 762 | |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 763 | 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] | 764 | { |
| 765 | list_add_tail(&line->node, head); |
| 766 | } |
| 767 | |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 768 | 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] | 769 | { |
| 770 | list_for_each_entry_continue(pos, head, node) |
| 771 | if (pos->offset >= 0) |
| 772 | return pos; |
| 773 | |
| 774 | return NULL; |
| 775 | } |
| 776 | |
Namhyung Kim | e64aa75 | 2013-03-05 14:53:30 +0900 | [diff] [blame] | 777 | double disasm__calc_percent(struct annotation *notes, int evidx, s64 offset, |
Martin Liška | 0c4a5bc | 2015-06-19 16:10:43 -0300 | [diff] [blame] | 778 | s64 end, const char **path, u64 *nr_samples) |
Namhyung Kim | e5ccf9f | 2013-03-05 14:53:23 +0900 | [diff] [blame] | 779 | { |
| 780 | struct source_line *src_line = notes->src->lines; |
Namhyung Kim | e5ccf9f | 2013-03-05 14:53:23 +0900 | [diff] [blame] | 781 | double percent = 0.0; |
Martin Liška | 0c4a5bc | 2015-06-19 16:10:43 -0300 | [diff] [blame] | 782 | *nr_samples = 0; |
Namhyung Kim | e5ccf9f | 2013-03-05 14:53:23 +0900 | [diff] [blame] | 783 | |
Namhyung Kim | bd64fcb | 2013-03-05 14:53:24 +0900 | [diff] [blame] | 784 | if (src_line) { |
Namhyung Kim | 1491c22 | 2013-03-05 14:53:28 +0900 | [diff] [blame] | 785 | size_t sizeof_src_line = sizeof(*src_line) + |
Arnaldo Carvalho de Melo | 276af92f | 2015-06-19 16:36:12 -0300 | [diff] [blame] | 786 | sizeof(src_line->samples) * (src_line->nr_pcnt - 1); |
Namhyung Kim | e5ccf9f | 2013-03-05 14:53:23 +0900 | [diff] [blame] | 787 | |
Namhyung Kim | 1491c22 | 2013-03-05 14:53:28 +0900 | [diff] [blame] | 788 | while (offset < end) { |
| 789 | src_line = (void *)notes->src->lines + |
| 790 | (sizeof_src_line * offset); |
| 791 | |
| 792 | if (*path == NULL) |
| 793 | *path = src_line->path; |
| 794 | |
Arnaldo Carvalho de Melo | 276af92f | 2015-06-19 16:36:12 -0300 | [diff] [blame] | 795 | percent += src_line->samples[evidx].percent; |
| 796 | *nr_samples += src_line->samples[evidx].nr; |
Namhyung Kim | 1491c22 | 2013-03-05 14:53:28 +0900 | [diff] [blame] | 797 | offset++; |
Namhyung Kim | bd64fcb | 2013-03-05 14:53:24 +0900 | [diff] [blame] | 798 | } |
| 799 | } else { |
Namhyung Kim | 1491c22 | 2013-03-05 14:53:28 +0900 | [diff] [blame] | 800 | struct sym_hist *h = annotation__histogram(notes, evidx); |
| 801 | unsigned int hits = 0; |
| 802 | |
Namhyung Kim | bd64fcb | 2013-03-05 14:53:24 +0900 | [diff] [blame] | 803 | while (offset < end) |
| 804 | hits += h->addr[offset++]; |
| 805 | |
Martin Liška | 0c4a5bc | 2015-06-19 16:10:43 -0300 | [diff] [blame] | 806 | if (h->sum) { |
| 807 | *nr_samples = hits; |
Namhyung Kim | bd64fcb | 2013-03-05 14:53:24 +0900 | [diff] [blame] | 808 | percent = 100.0 * hits / h->sum; |
Martin Liška | 0c4a5bc | 2015-06-19 16:10:43 -0300 | [diff] [blame] | 809 | } |
Namhyung Kim | e5ccf9f | 2013-03-05 14:53:23 +0900 | [diff] [blame] | 810 | } |
| 811 | |
Namhyung Kim | e5ccf9f | 2013-03-05 14:53:23 +0900 | [diff] [blame] | 812 | return percent; |
| 813 | } |
| 814 | |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 815 | static int disasm_line__print(struct disasm_line *dl, struct symbol *sym, u64 start, |
Namhyung Kim | db8fd07 | 2013-03-05 14:53:21 +0900 | [diff] [blame] | 816 | struct perf_evsel *evsel, u64 len, int min_pcnt, int printed, |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 817 | int max_lines, struct disasm_line *queue) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 818 | { |
| 819 | static const char *prev_line; |
| 820 | static const char *prev_color; |
| 821 | |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 822 | if (dl->offset != -1) { |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 823 | const char *path = NULL; |
Martin Liška | 0c4a5bc | 2015-06-19 16:10:43 -0300 | [diff] [blame] | 824 | u64 nr_samples; |
Namhyung Kim | b1dd443 | 2013-03-05 14:53:25 +0900 | [diff] [blame] | 825 | double percent, max_percent = 0.0; |
| 826 | double *ppercents = &percent; |
Martin Liška | 0c4a5bc | 2015-06-19 16:10:43 -0300 | [diff] [blame] | 827 | u64 *psamples = &nr_samples; |
Namhyung Kim | b1dd443 | 2013-03-05 14:53:25 +0900 | [diff] [blame] | 828 | int i, nr_percent = 1; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 829 | const char *color; |
| 830 | struct annotation *notes = symbol__annotation(sym); |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 831 | s64 offset = dl->offset; |
Arnaldo Carvalho de Melo | 058b4cc | 2012-04-02 12:59:01 -0300 | [diff] [blame] | 832 | const u64 addr = start + offset; |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 833 | struct disasm_line *next; |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 834 | |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 835 | next = disasm__get_next_ip_line(¬es->src->source, dl); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 836 | |
Namhyung Kim | 759ff49 | 2013-03-05 14:53:26 +0900 | [diff] [blame] | 837 | if (perf_evsel__is_group_event(evsel)) { |
Namhyung Kim | b1dd443 | 2013-03-05 14:53:25 +0900 | [diff] [blame] | 838 | nr_percent = evsel->nr_members; |
| 839 | ppercents = calloc(nr_percent, sizeof(double)); |
Martin Liška | 0c4a5bc | 2015-06-19 16:10:43 -0300 | [diff] [blame] | 840 | psamples = calloc(nr_percent, sizeof(u64)); |
| 841 | if (ppercents == NULL || psamples == NULL) { |
Namhyung Kim | b1dd443 | 2013-03-05 14:53:25 +0900 | [diff] [blame] | 842 | return -1; |
Martin Liška | 0c4a5bc | 2015-06-19 16:10:43 -0300 | [diff] [blame] | 843 | } |
Namhyung Kim | b1dd443 | 2013-03-05 14:53:25 +0900 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | for (i = 0; i < nr_percent; i++) { |
| 847 | percent = disasm__calc_percent(notes, |
Namhyung Kim | 1491c22 | 2013-03-05 14:53:28 +0900 | [diff] [blame] | 848 | notes->src->lines ? i : evsel->idx + i, |
| 849 | offset, |
| 850 | next ? next->offset : (s64) len, |
Martin Liška | 0c4a5bc | 2015-06-19 16:10:43 -0300 | [diff] [blame] | 851 | &path, &nr_samples); |
Namhyung Kim | b1dd443 | 2013-03-05 14:53:25 +0900 | [diff] [blame] | 852 | |
| 853 | ppercents[i] = percent; |
Martin Liška | 0c4a5bc | 2015-06-19 16:10:43 -0300 | [diff] [blame] | 854 | psamples[i] = nr_samples; |
Namhyung Kim | b1dd443 | 2013-03-05 14:53:25 +0900 | [diff] [blame] | 855 | if (percent > max_percent) |
| 856 | max_percent = percent; |
| 857 | } |
| 858 | |
| 859 | if (max_percent < min_pcnt) |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 860 | return -1; |
| 861 | |
Arnaldo Carvalho de Melo | e3087b8 | 2011-02-08 15:01:39 -0200 | [diff] [blame] | 862 | if (max_lines && printed >= max_lines) |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 863 | return 1; |
Arnaldo Carvalho de Melo | d040bd3 | 2011-02-05 15:37:31 -0200 | [diff] [blame] | 864 | |
Arnaldo Carvalho de Melo | d5e3d74 | 2011-02-08 15:29:25 -0200 | [diff] [blame] | 865 | if (queue != NULL) { |
| 866 | list_for_each_entry_from(queue, ¬es->src->source, node) { |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 867 | if (queue == dl) |
Arnaldo Carvalho de Melo | d5e3d74 | 2011-02-08 15:29:25 -0200 | [diff] [blame] | 868 | break; |
Namhyung Kim | db8fd07 | 2013-03-05 14:53:21 +0900 | [diff] [blame] | 869 | disasm_line__print(queue, sym, start, evsel, len, |
Arnaldo Carvalho de Melo | d5e3d74 | 2011-02-08 15:29:25 -0200 | [diff] [blame] | 870 | 0, 0, 1, NULL); |
| 871 | } |
| 872 | } |
| 873 | |
Namhyung Kim | b1dd443 | 2013-03-05 14:53:25 +0900 | [diff] [blame] | 874 | color = get_percent_color(max_percent); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 875 | |
| 876 | /* |
| 877 | * Also color the filename and line if needed, with |
| 878 | * the same color than the percentage. Don't print it |
| 879 | * twice for close colored addr with the same filename:line |
| 880 | */ |
| 881 | if (path) { |
| 882 | if (!prev_line || strcmp(prev_line, path) |
| 883 | || color != prev_color) { |
| 884 | color_fprintf(stdout, color, " %s", path); |
| 885 | prev_line = path; |
| 886 | prev_color = color; |
| 887 | } |
| 888 | } |
| 889 | |
Namhyung Kim | b1dd443 | 2013-03-05 14:53:25 +0900 | [diff] [blame] | 890 | for (i = 0; i < nr_percent; i++) { |
| 891 | percent = ppercents[i]; |
Martin Liška | 0c4a5bc | 2015-06-19 16:10:43 -0300 | [diff] [blame] | 892 | nr_samples = psamples[i]; |
Namhyung Kim | b1dd443 | 2013-03-05 14:53:25 +0900 | [diff] [blame] | 893 | color = get_percent_color(percent); |
Martin Liška | 0c4a5bc | 2015-06-19 16:10:43 -0300 | [diff] [blame] | 894 | |
| 895 | if (symbol_conf.show_total_period) |
| 896 | color_fprintf(stdout, color, " %7" PRIu64, |
| 897 | nr_samples); |
| 898 | else |
| 899 | color_fprintf(stdout, color, " %7.2f", percent); |
Namhyung Kim | b1dd443 | 2013-03-05 14:53:25 +0900 | [diff] [blame] | 900 | } |
| 901 | |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 902 | printf(" : "); |
Arnaldo Carvalho de Melo | 058b4cc | 2012-04-02 12:59:01 -0300 | [diff] [blame] | 903 | color_fprintf(stdout, PERF_COLOR_MAGENTA, " %" PRIx64 ":", addr); |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 904 | color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", dl->line); |
Namhyung Kim | b1dd443 | 2013-03-05 14:53:25 +0900 | [diff] [blame] | 905 | |
| 906 | if (ppercents != &percent) |
| 907 | free(ppercents); |
| 908 | |
Martin Liška | 0c4a5bc | 2015-06-19 16:10:43 -0300 | [diff] [blame] | 909 | if (psamples != &nr_samples) |
| 910 | free(psamples); |
| 911 | |
Arnaldo Carvalho de Melo | e3087b8 | 2011-02-08 15:01:39 -0200 | [diff] [blame] | 912 | } else if (max_lines && printed >= max_lines) |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 913 | return 1; |
| 914 | else { |
Namhyung Kim | b1dd443 | 2013-03-05 14:53:25 +0900 | [diff] [blame] | 915 | int width = 8; |
| 916 | |
Arnaldo Carvalho de Melo | d5e3d74 | 2011-02-08 15:29:25 -0200 | [diff] [blame] | 917 | if (queue) |
| 918 | return -1; |
| 919 | |
Namhyung Kim | 759ff49 | 2013-03-05 14:53:26 +0900 | [diff] [blame] | 920 | if (perf_evsel__is_group_event(evsel)) |
Namhyung Kim | b1dd443 | 2013-03-05 14:53:25 +0900 | [diff] [blame] | 921 | width *= evsel->nr_members; |
| 922 | |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 923 | if (!*dl->line) |
Namhyung Kim | b1dd443 | 2013-03-05 14:53:25 +0900 | [diff] [blame] | 924 | printf(" %*s:\n", width, " "); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 925 | else |
Namhyung Kim | b1dd443 | 2013-03-05 14:53:25 +0900 | [diff] [blame] | 926 | printf(" %*s: %s\n", width, " ", dl->line); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 927 | } |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 928 | |
| 929 | return 0; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 930 | } |
| 931 | |
Namhyung Kim | 3aec150 | 2013-03-05 14:53:22 +0900 | [diff] [blame] | 932 | /* |
| 933 | * symbol__parse_objdump_line() parses objdump output (with -d --no-show-raw) |
| 934 | * which looks like following |
| 935 | * |
| 936 | * 0000000000415500 <_init>: |
| 937 | * 415500: sub $0x8,%rsp |
| 938 | * 415504: mov 0x2f5ad5(%rip),%rax # 70afe0 <_DYNAMIC+0x2f8> |
| 939 | * 41550b: test %rax,%rax |
| 940 | * 41550e: je 415515 <_init+0x15> |
| 941 | * 415510: callq 416e70 <__gmon_start__@plt> |
| 942 | * 415515: add $0x8,%rsp |
| 943 | * 415519: retq |
| 944 | * |
| 945 | * it will be parsed and saved into struct disasm_line as |
| 946 | * <offset> <name> <ops.raw> |
| 947 | * |
| 948 | * The offset will be a relative offset from the start of the symbol and -1 |
| 949 | * means that it's not a disassembly line so should be treated differently. |
| 950 | * The ops.raw part will be parsed further according to type of the instruction. |
| 951 | */ |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 952 | static int symbol__parse_objdump_line(struct symbol *sym, struct map *map, |
Andi Kleen | e592488 | 2014-11-12 18:05:26 -0800 | [diff] [blame] | 953 | FILE *file, size_t privsize, |
| 954 | int *line_nr) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 955 | { |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 956 | struct annotation *notes = symbol__annotation(sym); |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 957 | struct disasm_line *dl; |
Arnaldo Carvalho de Melo | 058b4cc | 2012-04-02 12:59:01 -0300 | [diff] [blame] | 958 | char *line = NULL, *parsed_line, *tmp, *tmp2, *c; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 959 | size_t line_len; |
| 960 | s64 line_ip, offset = -1; |
Andi Kleen | e592488 | 2014-11-12 18:05:26 -0800 | [diff] [blame] | 961 | regmatch_t match[2]; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 962 | |
| 963 | if (getline(&line, &line_len, file) < 0) |
| 964 | return -1; |
| 965 | |
| 966 | if (!line) |
| 967 | return -1; |
| 968 | |
| 969 | while (line_len != 0 && isspace(line[line_len - 1])) |
| 970 | line[--line_len] = '\0'; |
| 971 | |
| 972 | c = strchr(line, '\n'); |
| 973 | if (c) |
| 974 | *c = 0; |
| 975 | |
| 976 | line_ip = -1; |
Namhyung Kim | a31b7cc | 2012-04-11 17:04:59 -0300 | [diff] [blame] | 977 | parsed_line = line; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 978 | |
Andi Kleen | e592488 | 2014-11-12 18:05:26 -0800 | [diff] [blame] | 979 | /* /filename:linenr ? Save line number and ignore. */ |
| 980 | if (regexec(&file_lineno, line, 2, match, 0) == 0) { |
| 981 | *line_nr = atoi(line + match[1].rm_so); |
| 982 | return 0; |
| 983 | } |
| 984 | |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 985 | /* |
| 986 | * Strip leading spaces: |
| 987 | */ |
| 988 | tmp = line; |
| 989 | while (*tmp) { |
| 990 | if (*tmp != ' ') |
| 991 | break; |
| 992 | tmp++; |
| 993 | } |
| 994 | |
| 995 | if (*tmp) { |
| 996 | /* |
| 997 | * Parse hexa addresses followed by ':' |
| 998 | */ |
| 999 | line_ip = strtoull(tmp, &tmp2, 16); |
| 1000 | if (*tmp2 != ':' || tmp == tmp2 || tmp2[1] == '\0') |
| 1001 | line_ip = -1; |
| 1002 | } |
| 1003 | |
| 1004 | if (line_ip != -1) { |
| 1005 | u64 start = map__rip_2objdump(map, sym->start), |
| 1006 | end = map__rip_2objdump(map, sym->end); |
| 1007 | |
| 1008 | offset = line_ip - start; |
Arnaldo Carvalho de Melo | 2c241bd | 2014-10-14 17:19:44 -0300 | [diff] [blame] | 1009 | if ((u64)line_ip < start || (u64)line_ip >= end) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1010 | offset = -1; |
Arnaldo Carvalho de Melo | 058b4cc | 2012-04-02 12:59:01 -0300 | [diff] [blame] | 1011 | else |
| 1012 | parsed_line = tmp2 + 1; |
Namhyung Kim | a31b7cc | 2012-04-11 17:04:59 -0300 | [diff] [blame] | 1013 | } |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1014 | |
Andi Kleen | e592488 | 2014-11-12 18:05:26 -0800 | [diff] [blame] | 1015 | dl = disasm_line__new(offset, parsed_line, privsize, *line_nr); |
Arnaldo Carvalho de Melo | 058b4cc | 2012-04-02 12:59:01 -0300 | [diff] [blame] | 1016 | free(line); |
Andi Kleen | e592488 | 2014-11-12 18:05:26 -0800 | [diff] [blame] | 1017 | (*line_nr)++; |
Arnaldo Carvalho de Melo | 058b4cc | 2012-04-02 12:59:01 -0300 | [diff] [blame] | 1018 | |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 1019 | if (dl == NULL) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1020 | return -1; |
Arnaldo Carvalho de Melo | 058b4cc | 2012-04-02 12:59:01 -0300 | [diff] [blame] | 1021 | |
Adrian Hunter | bbb7f84 | 2013-08-07 14:38:54 +0300 | [diff] [blame] | 1022 | if (dl->ops.target.offset == UINT64_MAX) |
| 1023 | dl->ops.target.offset = dl->ops.target.addr - |
| 1024 | map__rip_2objdump(map, sym->start); |
| 1025 | |
Adrian Hunter | 6e427ab | 2013-10-14 13:43:40 +0300 | [diff] [blame] | 1026 | /* kcore has no symbols, so add the call target name */ |
Adrian Hunter | b178170 | 2013-08-07 14:38:57 +0300 | [diff] [blame] | 1027 | if (dl->ins && ins__is_call(dl->ins) && !dl->ops.target.name) { |
Adrian Hunter | 6e427ab | 2013-10-14 13:43:40 +0300 | [diff] [blame] | 1028 | struct addr_map_symbol target = { |
| 1029 | .map = map, |
| 1030 | .addr = dl->ops.target.addr, |
| 1031 | }; |
Adrian Hunter | b178170 | 2013-08-07 14:38:57 +0300 | [diff] [blame] | 1032 | |
Adrian Hunter | 6e427ab | 2013-10-14 13:43:40 +0300 | [diff] [blame] | 1033 | if (!map_groups__find_ams(&target, NULL) && |
| 1034 | target.sym->start == target.al_addr) |
| 1035 | dl->ops.target.name = strdup(target.sym->name); |
Adrian Hunter | b178170 | 2013-08-07 14:38:57 +0300 | [diff] [blame] | 1036 | } |
| 1037 | |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 1038 | disasm__add(¬es->src->source, dl); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1039 | |
| 1040 | return 0; |
| 1041 | } |
| 1042 | |
Andi Kleen | e592488 | 2014-11-12 18:05:26 -0800 | [diff] [blame] | 1043 | static __attribute__((constructor)) void symbol__init_regexpr(void) |
| 1044 | { |
| 1045 | regcomp(&file_lineno, "^/[^:]+:([0-9]+)", REG_EXTENDED); |
| 1046 | } |
| 1047 | |
Adrian Hunter | 484a5e7 | 2013-08-07 14:38:56 +0300 | [diff] [blame] | 1048 | static void delete_last_nop(struct symbol *sym) |
| 1049 | { |
| 1050 | struct annotation *notes = symbol__annotation(sym); |
| 1051 | struct list_head *list = ¬es->src->source; |
| 1052 | struct disasm_line *dl; |
| 1053 | |
| 1054 | while (!list_empty(list)) { |
| 1055 | dl = list_entry(list->prev, struct disasm_line, node); |
| 1056 | |
| 1057 | if (dl->ins && dl->ins->ops) { |
| 1058 | if (dl->ins->ops != &nop_ops) |
| 1059 | return; |
| 1060 | } else { |
| 1061 | if (!strstr(dl->line, " nop ") && |
| 1062 | !strstr(dl->line, " nopl ") && |
| 1063 | !strstr(dl->line, " nopw ")) |
| 1064 | return; |
| 1065 | } |
| 1066 | |
| 1067 | list_del(&dl->node); |
| 1068 | disasm_line__free(dl); |
| 1069 | } |
| 1070 | } |
| 1071 | |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 1072 | 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] | 1073 | { |
| 1074 | struct dso *dso = map->dso; |
| 1075 | char *filename = dso__build_id_filename(dso, NULL, 0); |
| 1076 | bool free_filename = true; |
| 1077 | char command[PATH_MAX * 2]; |
| 1078 | FILE *file; |
| 1079 | int err = 0; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1080 | char symfs_filename[PATH_MAX]; |
Adrian Hunter | afba19d | 2013-10-09 15:01:12 +0300 | [diff] [blame] | 1081 | struct kcore_extract kce; |
| 1082 | bool delete_extract = false; |
Andi Kleen | e592488 | 2014-11-12 18:05:26 -0800 | [diff] [blame] | 1083 | int lineno = 0; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1084 | |
Arnaldo Carvalho de Melo | 972f393 | 2014-07-29 10:21:58 -0300 | [diff] [blame] | 1085 | if (filename) |
| 1086 | symbol__join_symfs(symfs_filename, filename); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1087 | |
| 1088 | if (filename == NULL) { |
| 1089 | if (dso->has_build_id) { |
| 1090 | pr_err("Can't annotate %s: not enough memory\n", |
| 1091 | sym->name); |
| 1092 | return -ENOMEM; |
| 1093 | } |
| 1094 | goto fallback; |
Adrian Hunter | ee20550 | 2014-11-11 12:04:54 +0200 | [diff] [blame] | 1095 | } else if (dso__is_kcore(dso)) { |
| 1096 | goto fallback; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1097 | } else if (readlink(symfs_filename, command, sizeof(command)) < 0 || |
| 1098 | strstr(command, "[kernel.kallsyms]") || |
| 1099 | access(symfs_filename, R_OK)) { |
| 1100 | free(filename); |
| 1101 | fallback: |
| 1102 | /* |
| 1103 | * If we don't have build-ids or the build-id file isn't in the |
| 1104 | * cache, or is just a kallsyms file, well, lets hope that this |
| 1105 | * DSO is the same as when 'perf record' ran. |
| 1106 | */ |
Arnaldo Carvalho de Melo | bf4414a | 2013-12-10 15:19:23 -0300 | [diff] [blame] | 1107 | filename = (char *)dso->long_name; |
Arnaldo Carvalho de Melo | 972f393 | 2014-07-29 10:21:58 -0300 | [diff] [blame] | 1108 | symbol__join_symfs(symfs_filename, filename); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1109 | free_filename = false; |
| 1110 | } |
| 1111 | |
Adrian Hunter | bbb7f84 | 2013-08-07 14:38:54 +0300 | [diff] [blame] | 1112 | if (dso->symtab_type == DSO_BINARY_TYPE__KALLSYMS && |
| 1113 | !dso__is_kcore(dso)) { |
Arnaldo Carvalho de Melo | 170ae6b | 2011-02-23 11:08:59 -0300 | [diff] [blame] | 1114 | char bf[BUILD_ID_SIZE * 2 + 16] = " with build id "; |
| 1115 | char *build_id_msg = NULL; |
| 1116 | |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1117 | if (dso->annotate_warned) |
| 1118 | goto out_free_filename; |
Arnaldo Carvalho de Melo | 170ae6b | 2011-02-23 11:08:59 -0300 | [diff] [blame] | 1119 | |
| 1120 | if (dso->has_build_id) { |
| 1121 | build_id__sprintf(dso->build_id, |
| 1122 | sizeof(dso->build_id), bf + 15); |
| 1123 | build_id_msg = bf; |
| 1124 | } |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1125 | err = -ENOENT; |
| 1126 | dso->annotate_warned = 1; |
Arnaldo Carvalho de Melo | ae55795 | 2011-10-26 08:00:55 -0200 | [diff] [blame] | 1127 | pr_err("Can't annotate %s:\n\n" |
| 1128 | "No vmlinux file%s\nwas found in the path.\n\n" |
| 1129 | "Please use:\n\n" |
Namhyung Kim | e3a3402 | 2013-02-07 18:02:12 +0900 | [diff] [blame] | 1130 | " perf buildid-cache -vu vmlinux\n\n" |
Arnaldo Carvalho de Melo | ae55795 | 2011-10-26 08:00:55 -0200 | [diff] [blame] | 1131 | "or:\n\n" |
Namhyung Kim | ff2a661 | 2012-02-23 17:46:24 +0900 | [diff] [blame] | 1132 | " --vmlinux vmlinux\n", |
Arnaldo Carvalho de Melo | 170ae6b | 2011-02-23 11:08:59 -0300 | [diff] [blame] | 1133 | sym->name, build_id_msg ?: ""); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1134 | goto out_free_filename; |
| 1135 | } |
| 1136 | |
| 1137 | pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__, |
| 1138 | filename, sym->name, map->unmap_ip(map, sym->start), |
| 1139 | map->unmap_ip(map, sym->end)); |
| 1140 | |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1141 | pr_debug("annotating [%p] %30s : [%p] %30s\n", |
| 1142 | dso, dso->long_name, sym, sym->name); |
| 1143 | |
Adrian Hunter | afba19d | 2013-10-09 15:01:12 +0300 | [diff] [blame] | 1144 | if (dso__is_kcore(dso)) { |
| 1145 | kce.kcore_filename = symfs_filename; |
| 1146 | kce.addr = map__rip_2objdump(map, sym->start); |
| 1147 | kce.offs = sym->start; |
Arnaldo Carvalho de Melo | 2c241bd | 2014-10-14 17:19:44 -0300 | [diff] [blame] | 1148 | kce.len = sym->end - sym->start; |
Adrian Hunter | afba19d | 2013-10-09 15:01:12 +0300 | [diff] [blame] | 1149 | if (!kcore_extract__create(&kce)) { |
| 1150 | delete_extract = true; |
| 1151 | strlcpy(symfs_filename, kce.extract_filename, |
| 1152 | sizeof(symfs_filename)); |
| 1153 | if (free_filename) { |
| 1154 | free(filename); |
| 1155 | free_filename = false; |
| 1156 | } |
| 1157 | filename = symfs_filename; |
| 1158 | } |
Jiri Olsa | 2c7da8c | 2015-03-02 12:56:12 -0500 | [diff] [blame] | 1159 | } else if (dso__needs_decompress(dso)) { |
| 1160 | char tmp[PATH_MAX]; |
| 1161 | struct kmod_path m; |
| 1162 | int fd; |
| 1163 | bool ret; |
| 1164 | |
| 1165 | if (kmod_path__parse_ext(&m, symfs_filename)) |
| 1166 | goto out_free_filename; |
| 1167 | |
| 1168 | snprintf(tmp, PATH_MAX, "/tmp/perf-kmod-XXXXXX"); |
| 1169 | |
| 1170 | fd = mkstemp(tmp); |
| 1171 | if (fd < 0) { |
| 1172 | free(m.ext); |
| 1173 | goto out_free_filename; |
| 1174 | } |
| 1175 | |
| 1176 | ret = decompress_to_file(m.ext, symfs_filename, fd); |
| 1177 | |
| 1178 | free(m.ext); |
| 1179 | close(fd); |
| 1180 | |
| 1181 | if (!ret) |
| 1182 | goto out_free_filename; |
| 1183 | |
| 1184 | strcpy(symfs_filename, tmp); |
Adrian Hunter | afba19d | 2013-10-09 15:01:12 +0300 | [diff] [blame] | 1185 | } |
| 1186 | |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1187 | snprintf(command, sizeof(command), |
Maciek Borzecki | 7a4ec93 | 2012-09-04 12:32:30 +0200 | [diff] [blame] | 1188 | "%s %s%s --start-address=0x%016" PRIx64 |
Stephane Eranian | 3e6a2a7 | 2011-05-17 17:32:07 +0200 | [diff] [blame] | 1189 | " --stop-address=0x%016" PRIx64 |
Andi Kleen | e592488 | 2014-11-12 18:05:26 -0800 | [diff] [blame] | 1190 | " -l -d %s %s -C %s 2>/dev/null|grep -v %s|expand", |
Maciek Borzecki | 7a4ec93 | 2012-09-04 12:32:30 +0200 | [diff] [blame] | 1191 | objdump_path ? objdump_path : "objdump", |
Andi Kleen | f69b64f | 2011-09-15 14:31:41 -0700 | [diff] [blame] | 1192 | disassembler_style ? "-M " : "", |
| 1193 | disassembler_style ? disassembler_style : "", |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1194 | map__rip_2objdump(map, sym->start), |
Arnaldo Carvalho de Melo | 2c241bd | 2014-10-14 17:19:44 -0300 | [diff] [blame] | 1195 | map__rip_2objdump(map, sym->end), |
Stephane Eranian | 3e6a2a7 | 2011-05-17 17:32:07 +0200 | [diff] [blame] | 1196 | symbol_conf.annotate_asm_raw ? "" : "--no-show-raw", |
| 1197 | symbol_conf.annotate_src ? "-S" : "", |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1198 | symfs_filename, filename); |
| 1199 | |
| 1200 | pr_debug("Executing: %s\n", command); |
| 1201 | |
| 1202 | file = popen(command, "r"); |
| 1203 | if (!file) |
Jiri Olsa | 2c7da8c | 2015-03-02 12:56:12 -0500 | [diff] [blame] | 1204 | goto out_remove_tmp; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1205 | |
| 1206 | while (!feof(file)) |
Andi Kleen | e592488 | 2014-11-12 18:05:26 -0800 | [diff] [blame] | 1207 | if (symbol__parse_objdump_line(sym, map, file, privsize, |
| 1208 | &lineno) < 0) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1209 | break; |
| 1210 | |
Adrian Hunter | 484a5e7 | 2013-08-07 14:38:56 +0300 | [diff] [blame] | 1211 | /* |
| 1212 | * kallsyms does not have symbol sizes so there may a nop at the end. |
| 1213 | * Remove it. |
| 1214 | */ |
| 1215 | if (dso__is_kcore(dso)) |
| 1216 | delete_last_nop(sym); |
| 1217 | |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1218 | pclose(file); |
Jiri Olsa | 2c7da8c | 2015-03-02 12:56:12 -0500 | [diff] [blame] | 1219 | |
| 1220 | out_remove_tmp: |
| 1221 | if (dso__needs_decompress(dso)) |
| 1222 | unlink(symfs_filename); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1223 | out_free_filename: |
Adrian Hunter | afba19d | 2013-10-09 15:01:12 +0300 | [diff] [blame] | 1224 | if (delete_extract) |
| 1225 | kcore_extract__delete(&kce); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1226 | if (free_filename) |
| 1227 | free(filename); |
| 1228 | return err; |
| 1229 | } |
| 1230 | |
| 1231 | static void insert_source_line(struct rb_root *root, struct source_line *src_line) |
| 1232 | { |
| 1233 | struct source_line *iter; |
| 1234 | struct rb_node **p = &root->rb_node; |
| 1235 | struct rb_node *parent = NULL; |
Namhyung Kim | 1491c22 | 2013-03-05 14:53:28 +0900 | [diff] [blame] | 1236 | int i, ret; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1237 | |
| 1238 | while (*p != NULL) { |
| 1239 | parent = *p; |
| 1240 | iter = rb_entry(parent, struct source_line, node); |
| 1241 | |
Namhyung Kim | 4112796 | 2012-11-09 14:58:49 +0900 | [diff] [blame] | 1242 | ret = strcmp(iter->path, src_line->path); |
| 1243 | if (ret == 0) { |
Namhyung Kim | 1491c22 | 2013-03-05 14:53:28 +0900 | [diff] [blame] | 1244 | for (i = 0; i < src_line->nr_pcnt; i++) |
Arnaldo Carvalho de Melo | 276af92f | 2015-06-19 16:36:12 -0300 | [diff] [blame] | 1245 | iter->samples[i].percent_sum += src_line->samples[i].percent; |
Namhyung Kim | 4112796 | 2012-11-09 14:58:49 +0900 | [diff] [blame] | 1246 | return; |
| 1247 | } |
| 1248 | |
| 1249 | if (ret < 0) |
| 1250 | p = &(*p)->rb_left; |
| 1251 | else |
| 1252 | p = &(*p)->rb_right; |
| 1253 | } |
| 1254 | |
Namhyung Kim | 1491c22 | 2013-03-05 14:53:28 +0900 | [diff] [blame] | 1255 | for (i = 0; i < src_line->nr_pcnt; i++) |
Arnaldo Carvalho de Melo | 276af92f | 2015-06-19 16:36:12 -0300 | [diff] [blame] | 1256 | src_line->samples[i].percent_sum = src_line->samples[i].percent; |
Namhyung Kim | 4112796 | 2012-11-09 14:58:49 +0900 | [diff] [blame] | 1257 | |
| 1258 | rb_link_node(&src_line->node, parent, p); |
| 1259 | rb_insert_color(&src_line->node, root); |
| 1260 | } |
| 1261 | |
Namhyung Kim | 1491c22 | 2013-03-05 14:53:28 +0900 | [diff] [blame] | 1262 | static int cmp_source_line(struct source_line *a, struct source_line *b) |
| 1263 | { |
| 1264 | int i; |
| 1265 | |
| 1266 | for (i = 0; i < a->nr_pcnt; i++) { |
Arnaldo Carvalho de Melo | 276af92f | 2015-06-19 16:36:12 -0300 | [diff] [blame] | 1267 | if (a->samples[i].percent_sum == b->samples[i].percent_sum) |
Namhyung Kim | 1491c22 | 2013-03-05 14:53:28 +0900 | [diff] [blame] | 1268 | continue; |
Arnaldo Carvalho de Melo | 276af92f | 2015-06-19 16:36:12 -0300 | [diff] [blame] | 1269 | return a->samples[i].percent_sum > b->samples[i].percent_sum; |
Namhyung Kim | 1491c22 | 2013-03-05 14:53:28 +0900 | [diff] [blame] | 1270 | } |
| 1271 | |
| 1272 | return 0; |
| 1273 | } |
| 1274 | |
Namhyung Kim | 4112796 | 2012-11-09 14:58:49 +0900 | [diff] [blame] | 1275 | static void __resort_source_line(struct rb_root *root, struct source_line *src_line) |
| 1276 | { |
| 1277 | struct source_line *iter; |
| 1278 | struct rb_node **p = &root->rb_node; |
| 1279 | struct rb_node *parent = NULL; |
| 1280 | |
| 1281 | while (*p != NULL) { |
| 1282 | parent = *p; |
| 1283 | iter = rb_entry(parent, struct source_line, node); |
| 1284 | |
Namhyung Kim | 1491c22 | 2013-03-05 14:53:28 +0900 | [diff] [blame] | 1285 | if (cmp_source_line(src_line, iter)) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1286 | p = &(*p)->rb_left; |
| 1287 | else |
| 1288 | p = &(*p)->rb_right; |
| 1289 | } |
| 1290 | |
| 1291 | rb_link_node(&src_line->node, parent, p); |
| 1292 | rb_insert_color(&src_line->node, root); |
| 1293 | } |
| 1294 | |
Namhyung Kim | 4112796 | 2012-11-09 14:58:49 +0900 | [diff] [blame] | 1295 | static void resort_source_line(struct rb_root *dest_root, struct rb_root *src_root) |
| 1296 | { |
| 1297 | struct source_line *src_line; |
| 1298 | struct rb_node *node; |
| 1299 | |
| 1300 | node = rb_first(src_root); |
| 1301 | while (node) { |
| 1302 | struct rb_node *next; |
| 1303 | |
| 1304 | src_line = rb_entry(node, struct source_line, node); |
| 1305 | next = rb_next(node); |
| 1306 | rb_erase(node, src_root); |
| 1307 | |
| 1308 | __resort_source_line(dest_root, src_line); |
| 1309 | node = next; |
| 1310 | } |
| 1311 | } |
| 1312 | |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1313 | static void symbol__free_source_line(struct symbol *sym, int len) |
| 1314 | { |
| 1315 | struct annotation *notes = symbol__annotation(sym); |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 1316 | struct source_line *src_line = notes->src->lines; |
Namhyung Kim | 1491c22 | 2013-03-05 14:53:28 +0900 | [diff] [blame] | 1317 | size_t sizeof_src_line; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1318 | int i; |
| 1319 | |
Namhyung Kim | 1491c22 | 2013-03-05 14:53:28 +0900 | [diff] [blame] | 1320 | sizeof_src_line = sizeof(*src_line) + |
Arnaldo Carvalho de Melo | 276af92f | 2015-06-19 16:36:12 -0300 | [diff] [blame] | 1321 | (sizeof(src_line->samples) * (src_line->nr_pcnt - 1)); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1322 | |
Namhyung Kim | 1491c22 | 2013-03-05 14:53:28 +0900 | [diff] [blame] | 1323 | for (i = 0; i < len; i++) { |
Namhyung Kim | f048d54 | 2013-09-11 14:09:28 +0900 | [diff] [blame] | 1324 | free_srcline(src_line->path); |
Namhyung Kim | 1491c22 | 2013-03-05 14:53:28 +0900 | [diff] [blame] | 1325 | src_line = (void *)src_line + sizeof_src_line; |
| 1326 | } |
| 1327 | |
Arnaldo Carvalho de Melo | 0466252 | 2013-12-26 17:41:15 -0300 | [diff] [blame] | 1328 | zfree(¬es->src->lines); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1329 | } |
| 1330 | |
| 1331 | /* Get the filename:line for the colored entries */ |
| 1332 | static int symbol__get_source_line(struct symbol *sym, struct map *map, |
Namhyung Kim | db8fd07 | 2013-03-05 14:53:21 +0900 | [diff] [blame] | 1333 | struct perf_evsel *evsel, |
Namhyung Kim | 86c98ca | 2013-09-11 14:09:30 +0900 | [diff] [blame] | 1334 | struct rb_root *root, int len) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1335 | { |
| 1336 | u64 start; |
Namhyung Kim | 1491c22 | 2013-03-05 14:53:28 +0900 | [diff] [blame] | 1337 | int i, k; |
| 1338 | int evidx = evsel->idx; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1339 | struct source_line *src_line; |
| 1340 | struct annotation *notes = symbol__annotation(sym); |
Namhyung Kim | 1491c22 | 2013-03-05 14:53:28 +0900 | [diff] [blame] | 1341 | struct sym_hist *h = annotation__histogram(notes, evidx); |
Namhyung Kim | 4112796 | 2012-11-09 14:58:49 +0900 | [diff] [blame] | 1342 | struct rb_root tmp_root = RB_ROOT; |
Namhyung Kim | 1491c22 | 2013-03-05 14:53:28 +0900 | [diff] [blame] | 1343 | int nr_pcnt = 1; |
| 1344 | u64 h_sum = h->sum; |
| 1345 | size_t sizeof_src_line = sizeof(struct source_line); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1346 | |
Namhyung Kim | 1491c22 | 2013-03-05 14:53:28 +0900 | [diff] [blame] | 1347 | if (perf_evsel__is_group_event(evsel)) { |
| 1348 | for (i = 1; i < evsel->nr_members; i++) { |
| 1349 | h = annotation__histogram(notes, evidx + i); |
| 1350 | h_sum += h->sum; |
| 1351 | } |
| 1352 | nr_pcnt = evsel->nr_members; |
Arnaldo Carvalho de Melo | 276af92f | 2015-06-19 16:36:12 -0300 | [diff] [blame] | 1353 | sizeof_src_line += (nr_pcnt - 1) * sizeof(src_line->samples); |
Namhyung Kim | 1491c22 | 2013-03-05 14:53:28 +0900 | [diff] [blame] | 1354 | } |
| 1355 | |
| 1356 | if (!h_sum) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1357 | return 0; |
| 1358 | |
Namhyung Kim | 1491c22 | 2013-03-05 14:53:28 +0900 | [diff] [blame] | 1359 | src_line = notes->src->lines = calloc(len, sizeof_src_line); |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 1360 | if (!notes->src->lines) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1361 | return -1; |
| 1362 | |
David Miller | f40a063 | 2012-03-25 16:28:12 -0400 | [diff] [blame] | 1363 | start = map__rip_2objdump(map, sym->start); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1364 | |
| 1365 | for (i = 0; i < len; i++) { |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1366 | u64 offset; |
Namhyung Kim | 1491c22 | 2013-03-05 14:53:28 +0900 | [diff] [blame] | 1367 | double percent_max = 0.0; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1368 | |
Namhyung Kim | 1491c22 | 2013-03-05 14:53:28 +0900 | [diff] [blame] | 1369 | src_line->nr_pcnt = nr_pcnt; |
| 1370 | |
| 1371 | for (k = 0; k < nr_pcnt; k++) { |
| 1372 | h = annotation__histogram(notes, evidx + k); |
Arnaldo Carvalho de Melo | 276af92f | 2015-06-19 16:36:12 -0300 | [diff] [blame] | 1373 | src_line->samples[k].percent = 100.0 * h->addr[i] / h->sum; |
Namhyung Kim | 1491c22 | 2013-03-05 14:53:28 +0900 | [diff] [blame] | 1374 | |
Arnaldo Carvalho de Melo | 276af92f | 2015-06-19 16:36:12 -0300 | [diff] [blame] | 1375 | if (src_line->samples[k].percent > percent_max) |
| 1376 | percent_max = src_line->samples[k].percent; |
Namhyung Kim | 1491c22 | 2013-03-05 14:53:28 +0900 | [diff] [blame] | 1377 | } |
| 1378 | |
| 1379 | if (percent_max <= 0.5) |
| 1380 | goto next; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1381 | |
| 1382 | offset = start + i; |
Andi Kleen | 85c116a | 2014-11-12 18:05:27 -0800 | [diff] [blame] | 1383 | src_line->path = get_srcline(map->dso, offset, NULL, false); |
Namhyung Kim | 1491c22 | 2013-03-05 14:53:28 +0900 | [diff] [blame] | 1384 | insert_source_line(&tmp_root, src_line); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1385 | |
Namhyung Kim | 1491c22 | 2013-03-05 14:53:28 +0900 | [diff] [blame] | 1386 | next: |
| 1387 | src_line = (void *)src_line + sizeof_src_line; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1388 | } |
| 1389 | |
Namhyung Kim | 4112796 | 2012-11-09 14:58:49 +0900 | [diff] [blame] | 1390 | resort_source_line(root, &tmp_root); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1391 | return 0; |
| 1392 | } |
| 1393 | |
| 1394 | static void print_summary(struct rb_root *root, const char *filename) |
| 1395 | { |
| 1396 | struct source_line *src_line; |
| 1397 | struct rb_node *node; |
| 1398 | |
| 1399 | printf("\nSorted summary for file %s\n", filename); |
| 1400 | printf("----------------------------------------------\n\n"); |
| 1401 | |
| 1402 | if (RB_EMPTY_ROOT(root)) { |
| 1403 | printf(" Nothing higher than %1.1f%%\n", MIN_GREEN); |
| 1404 | return; |
| 1405 | } |
| 1406 | |
| 1407 | node = rb_first(root); |
| 1408 | while (node) { |
Namhyung Kim | 1491c22 | 2013-03-05 14:53:28 +0900 | [diff] [blame] | 1409 | double percent, percent_max = 0.0; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1410 | const char *color; |
| 1411 | char *path; |
Namhyung Kim | 1491c22 | 2013-03-05 14:53:28 +0900 | [diff] [blame] | 1412 | int i; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1413 | |
| 1414 | src_line = rb_entry(node, struct source_line, node); |
Namhyung Kim | 1491c22 | 2013-03-05 14:53:28 +0900 | [diff] [blame] | 1415 | for (i = 0; i < src_line->nr_pcnt; i++) { |
Arnaldo Carvalho de Melo | 276af92f | 2015-06-19 16:36:12 -0300 | [diff] [blame] | 1416 | percent = src_line->samples[i].percent_sum; |
Namhyung Kim | 1491c22 | 2013-03-05 14:53:28 +0900 | [diff] [blame] | 1417 | color = get_percent_color(percent); |
| 1418 | color_fprintf(stdout, color, " %7.2f", percent); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1419 | |
Namhyung Kim | 1491c22 | 2013-03-05 14:53:28 +0900 | [diff] [blame] | 1420 | if (percent > percent_max) |
| 1421 | percent_max = percent; |
| 1422 | } |
| 1423 | |
| 1424 | path = src_line->path; |
| 1425 | color = get_percent_color(percent_max); |
Namhyung Kim | f048d54 | 2013-09-11 14:09:28 +0900 | [diff] [blame] | 1426 | color_fprintf(stdout, color, " %s\n", path); |
Namhyung Kim | 1491c22 | 2013-03-05 14:53:28 +0900 | [diff] [blame] | 1427 | |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1428 | node = rb_next(node); |
| 1429 | } |
| 1430 | } |
| 1431 | |
Namhyung Kim | db8fd07 | 2013-03-05 14:53:21 +0900 | [diff] [blame] | 1432 | static void symbol__annotate_hits(struct symbol *sym, struct perf_evsel *evsel) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1433 | { |
| 1434 | struct annotation *notes = symbol__annotation(sym); |
Namhyung Kim | db8fd07 | 2013-03-05 14:53:21 +0900 | [diff] [blame] | 1435 | struct sym_hist *h = annotation__histogram(notes, evsel->idx); |
Arnaldo Carvalho de Melo | 1b2e2df | 2012-04-19 10:57:06 -0300 | [diff] [blame] | 1436 | u64 len = symbol__size(sym), offset; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1437 | |
| 1438 | for (offset = 0; offset < len; ++offset) |
| 1439 | if (h->addr[offset] != 0) |
| 1440 | printf("%*" PRIx64 ": %" PRIu64 "\n", BITS_PER_LONG / 2, |
| 1441 | sym->start + offset, h->addr[offset]); |
| 1442 | printf("%*s: %" PRIu64 "\n", BITS_PER_LONG / 2, "h->sum", h->sum); |
| 1443 | } |
| 1444 | |
Namhyung Kim | db8fd07 | 2013-03-05 14:53:21 +0900 | [diff] [blame] | 1445 | int symbol__annotate_printf(struct symbol *sym, struct map *map, |
| 1446 | struct perf_evsel *evsel, bool full_paths, |
| 1447 | int min_pcnt, int max_lines, int context) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1448 | { |
| 1449 | struct dso *dso = map->dso; |
David Ahern | bfd14b9 | 2012-09-08 09:06:50 -0600 | [diff] [blame] | 1450 | char *filename; |
| 1451 | const char *d_filename; |
Arnaldo Carvalho de Melo | 9cdbadce | 2014-03-18 11:50:21 -0300 | [diff] [blame] | 1452 | const char *evsel_name = perf_evsel__name(evsel); |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 1453 | struct annotation *notes = symbol__annotation(sym); |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 1454 | struct disasm_line *pos, *queue = NULL; |
Arnaldo Carvalho de Melo | 058b4cc | 2012-04-02 12:59:01 -0300 | [diff] [blame] | 1455 | u64 start = map__rip_2objdump(map, sym->start); |
Arnaldo Carvalho de Melo | d5e3d74 | 2011-02-08 15:29:25 -0200 | [diff] [blame] | 1456 | int printed = 2, queue_len = 0; |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 1457 | int more = 0; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1458 | u64 len; |
Namhyung Kim | b1dd443 | 2013-03-05 14:53:25 +0900 | [diff] [blame] | 1459 | int width = 8; |
Arnaldo Carvalho de Melo | 9cdbadce | 2014-03-18 11:50:21 -0300 | [diff] [blame] | 1460 | int namelen, evsel_name_len, graph_dotted_len; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1461 | |
David Ahern | bfd14b9 | 2012-09-08 09:06:50 -0600 | [diff] [blame] | 1462 | filename = strdup(dso->long_name); |
| 1463 | if (!filename) |
| 1464 | return -ENOMEM; |
| 1465 | |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1466 | if (full_paths) |
| 1467 | d_filename = filename; |
| 1468 | else |
| 1469 | d_filename = basename(filename); |
| 1470 | |
Arnaldo Carvalho de Melo | 1b2e2df | 2012-04-19 10:57:06 -0300 | [diff] [blame] | 1471 | len = symbol__size(sym); |
Namhyung Kim | b1dd443 | 2013-03-05 14:53:25 +0900 | [diff] [blame] | 1472 | namelen = strlen(d_filename); |
Arnaldo Carvalho de Melo | 9cdbadce | 2014-03-18 11:50:21 -0300 | [diff] [blame] | 1473 | evsel_name_len = strlen(evsel_name); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1474 | |
Namhyung Kim | 759ff49 | 2013-03-05 14:53:26 +0900 | [diff] [blame] | 1475 | if (perf_evsel__is_group_event(evsel)) |
Namhyung Kim | b1dd443 | 2013-03-05 14:53:25 +0900 | [diff] [blame] | 1476 | width *= evsel->nr_members; |
| 1477 | |
Arnaldo Carvalho de Melo | 9cdbadce | 2014-03-18 11:50:21 -0300 | [diff] [blame] | 1478 | printf(" %-*.*s| Source code & Disassembly of %s for %s\n", |
| 1479 | width, width, "Percent", d_filename, evsel_name); |
| 1480 | |
| 1481 | graph_dotted_len = width + namelen + evsel_name_len; |
| 1482 | printf("-%-*.*s-----------------------------------------\n", |
| 1483 | graph_dotted_len, graph_dotted_len, graph_dotted_line); |
Arnaldo Carvalho de Melo | f1e2701 | 2011-02-05 18:51:38 -0200 | [diff] [blame] | 1484 | |
| 1485 | if (verbose) |
Namhyung Kim | db8fd07 | 2013-03-05 14:53:21 +0900 | [diff] [blame] | 1486 | symbol__annotate_hits(sym, evsel); |
Arnaldo Carvalho de Melo | f1e2701 | 2011-02-05 18:51:38 -0200 | [diff] [blame] | 1487 | |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 1488 | list_for_each_entry(pos, ¬es->src->source, node) { |
Arnaldo Carvalho de Melo | d5e3d74 | 2011-02-08 15:29:25 -0200 | [diff] [blame] | 1489 | if (context && queue == NULL) { |
| 1490 | queue = pos; |
| 1491 | queue_len = 0; |
| 1492 | } |
| 1493 | |
Namhyung Kim | db8fd07 | 2013-03-05 14:53:21 +0900 | [diff] [blame] | 1494 | switch (disasm_line__print(pos, sym, start, evsel, len, |
Arnaldo Carvalho de Melo | 058b4cc | 2012-04-02 12:59:01 -0300 | [diff] [blame] | 1495 | min_pcnt, printed, max_lines, |
| 1496 | queue)) { |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 1497 | case 0: |
| 1498 | ++printed; |
Arnaldo Carvalho de Melo | d5e3d74 | 2011-02-08 15:29:25 -0200 | [diff] [blame] | 1499 | if (context) { |
| 1500 | printed += queue_len; |
| 1501 | queue = NULL; |
| 1502 | queue_len = 0; |
| 1503 | } |
Arnaldo Carvalho de Melo | f1e2701 | 2011-02-05 18:51:38 -0200 | [diff] [blame] | 1504 | break; |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 1505 | case 1: |
| 1506 | /* filtered by max_lines */ |
| 1507 | ++more; |
| 1508 | break; |
| 1509 | case -1: |
| 1510 | default: |
Arnaldo Carvalho de Melo | d5e3d74 | 2011-02-08 15:29:25 -0200 | [diff] [blame] | 1511 | /* |
| 1512 | * Filtered by min_pcnt or non IP lines when |
| 1513 | * context != 0 |
| 1514 | */ |
| 1515 | if (!context) |
| 1516 | break; |
| 1517 | if (queue_len == context) |
| 1518 | queue = list_entry(queue->node.next, typeof(*queue), node); |
| 1519 | else |
| 1520 | ++queue_len; |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 1521 | break; |
| 1522 | } |
| 1523 | } |
Arnaldo Carvalho de Melo | f1e2701 | 2011-02-05 18:51:38 -0200 | [diff] [blame] | 1524 | |
David Ahern | bfd14b9 | 2012-09-08 09:06:50 -0600 | [diff] [blame] | 1525 | free(filename); |
| 1526 | |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 1527 | return more; |
| 1528 | } |
| 1529 | |
| 1530 | void symbol__annotate_zero_histogram(struct symbol *sym, int evidx) |
| 1531 | { |
| 1532 | struct annotation *notes = symbol__annotation(sym); |
| 1533 | struct sym_hist *h = annotation__histogram(notes, evidx); |
| 1534 | |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 1535 | memset(h, 0, notes->src->sizeof_sym_hist); |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 1536 | } |
| 1537 | |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 1538 | void symbol__annotate_decay_histogram(struct symbol *sym, int evidx) |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 1539 | { |
| 1540 | struct annotation *notes = symbol__annotation(sym); |
| 1541 | struct sym_hist *h = annotation__histogram(notes, evidx); |
Arnaldo Carvalho de Melo | 1b2e2df | 2012-04-19 10:57:06 -0300 | [diff] [blame] | 1542 | int len = symbol__size(sym), offset; |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 1543 | |
| 1544 | h->sum = 0; |
Arnaldo Carvalho de Melo | 8b84a56 | 2012-04-05 16:15:59 -0300 | [diff] [blame] | 1545 | for (offset = 0; offset < len; ++offset) { |
| 1546 | h->addr[offset] = h->addr[offset] * 7 / 8; |
| 1547 | h->sum += h->addr[offset]; |
Arnaldo Carvalho de Melo | f1e2701 | 2011-02-05 18:51:38 -0200 | [diff] [blame] | 1548 | } |
| 1549 | } |
| 1550 | |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 1551 | void disasm__purge(struct list_head *head) |
Arnaldo Carvalho de Melo | f1e2701 | 2011-02-05 18:51:38 -0200 | [diff] [blame] | 1552 | { |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 1553 | struct disasm_line *pos, *n; |
Arnaldo Carvalho de Melo | f1e2701 | 2011-02-05 18:51:38 -0200 | [diff] [blame] | 1554 | |
| 1555 | list_for_each_entry_safe(pos, n, head, node) { |
| 1556 | list_del(&pos->node); |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 1557 | disasm_line__free(pos); |
Arnaldo Carvalho de Melo | f1e2701 | 2011-02-05 18:51:38 -0200 | [diff] [blame] | 1558 | } |
| 1559 | } |
| 1560 | |
Arnaldo Carvalho de Melo | 5145418 | 2012-04-15 15:52:18 -0300 | [diff] [blame] | 1561 | static size_t disasm_line__fprintf(struct disasm_line *dl, FILE *fp) |
| 1562 | { |
| 1563 | size_t printed; |
| 1564 | |
| 1565 | if (dl->offset == -1) |
| 1566 | return fprintf(fp, "%s\n", dl->line); |
| 1567 | |
| 1568 | printed = fprintf(fp, "%#" PRIx64 " %s", dl->offset, dl->name); |
| 1569 | |
Arnaldo Carvalho de Melo | c7e6ead | 2012-04-20 14:38:46 -0300 | [diff] [blame] | 1570 | if (dl->ops.raw[0] != '\0') { |
Arnaldo Carvalho de Melo | 5145418 | 2012-04-15 15:52:18 -0300 | [diff] [blame] | 1571 | printed += fprintf(fp, "%.*s %s\n", 6 - (int)printed, " ", |
Arnaldo Carvalho de Melo | c7e6ead | 2012-04-20 14:38:46 -0300 | [diff] [blame] | 1572 | dl->ops.raw); |
Arnaldo Carvalho de Melo | 5145418 | 2012-04-15 15:52:18 -0300 | [diff] [blame] | 1573 | } |
| 1574 | |
| 1575 | return printed + fprintf(fp, "\n"); |
| 1576 | } |
| 1577 | |
| 1578 | size_t disasm__fprintf(struct list_head *head, FILE *fp) |
| 1579 | { |
| 1580 | struct disasm_line *pos; |
| 1581 | size_t printed = 0; |
| 1582 | |
| 1583 | list_for_each_entry(pos, head, node) |
| 1584 | printed += disasm_line__fprintf(pos, fp); |
| 1585 | |
| 1586 | return printed; |
| 1587 | } |
| 1588 | |
Namhyung Kim | db8fd07 | 2013-03-05 14:53:21 +0900 | [diff] [blame] | 1589 | int symbol__tty_annotate(struct symbol *sym, struct map *map, |
| 1590 | struct perf_evsel *evsel, bool print_lines, |
| 1591 | bool full_paths, int min_pcnt, int max_lines) |
Arnaldo Carvalho de Melo | f1e2701 | 2011-02-05 18:51:38 -0200 | [diff] [blame] | 1592 | { |
| 1593 | struct dso *dso = map->dso; |
Arnaldo Carvalho de Melo | f1e2701 | 2011-02-05 18:51:38 -0200 | [diff] [blame] | 1594 | struct rb_root source_line = RB_ROOT; |
Arnaldo Carvalho de Melo | f1e2701 | 2011-02-05 18:51:38 -0200 | [diff] [blame] | 1595 | u64 len; |
| 1596 | |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 1597 | if (symbol__annotate(sym, map, 0) < 0) |
Arnaldo Carvalho de Melo | f1e2701 | 2011-02-05 18:51:38 -0200 | [diff] [blame] | 1598 | return -1; |
| 1599 | |
Arnaldo Carvalho de Melo | 1b2e2df | 2012-04-19 10:57:06 -0300 | [diff] [blame] | 1600 | len = symbol__size(sym); |
Arnaldo Carvalho de Melo | f1e2701 | 2011-02-05 18:51:38 -0200 | [diff] [blame] | 1601 | |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1602 | if (print_lines) { |
Namhyung Kim | 86c98ca | 2013-09-11 14:09:30 +0900 | [diff] [blame] | 1603 | symbol__get_source_line(sym, map, evsel, &source_line, len); |
| 1604 | print_summary(&source_line, dso->long_name); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1605 | } |
| 1606 | |
Namhyung Kim | db8fd07 | 2013-03-05 14:53:21 +0900 | [diff] [blame] | 1607 | symbol__annotate_printf(sym, map, evsel, full_paths, |
Arnaldo Carvalho de Melo | d5e3d74 | 2011-02-08 15:29:25 -0200 | [diff] [blame] | 1608 | min_pcnt, max_lines, 0); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1609 | if (print_lines) |
| 1610 | symbol__free_source_line(sym, len); |
| 1611 | |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 1612 | disasm__purge(&symbol__annotation(sym)->src->source); |
Arnaldo Carvalho de Melo | f1e2701 | 2011-02-05 18:51:38 -0200 | [diff] [blame] | 1613 | |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1614 | return 0; |
| 1615 | } |
Arnaldo Carvalho de Melo | f626adf | 2013-12-18 17:10:15 -0300 | [diff] [blame] | 1616 | |
| 1617 | int hist_entry__annotate(struct hist_entry *he, size_t privsize) |
| 1618 | { |
| 1619 | return symbol__annotate(he->ms.sym, he->ms.map, privsize); |
| 1620 | } |
Namhyung Kim | 48c65bd | 2014-02-20 10:32:53 +0900 | [diff] [blame] | 1621 | |
| 1622 | bool ui__has_annotation(void) |
| 1623 | { |
| 1624 | return use_browser == 1 && sort__has_sym; |
| 1625 | } |