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