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