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