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