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" |
Arnaldo Carvalho de Melo | 7f0b6fd | 2018-03-16 14:33:38 -0300 | [diff] [blame] | 17 | #include "config.h" |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 18 | #include "cache.h" |
| 19 | #include "symbol.h" |
Arnaldo Carvalho de Melo | b213eac | 2018-04-03 15:19:47 -0300 | [diff] [blame] | 20 | #include "units.h" |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 21 | #include "debug.h" |
| 22 | #include "annotate.h" |
Namhyung Kim | db8fd07 | 2013-03-05 14:53:21 +0900 | [diff] [blame] | 23 | #include "evsel.h" |
Peter Zijlstra | 70fbe05 | 2016-09-05 16:08:12 -0300 | [diff] [blame] | 24 | #include "block-range.h" |
Arnaldo Carvalho de Melo | a067558 | 2017-04-17 16:51:59 -0300 | [diff] [blame] | 25 | #include "string2.h" |
Arnaldo Carvalho de Melo | 786c1b5 | 2016-11-16 15:39:50 -0300 | [diff] [blame] | 26 | #include "arch/common.h" |
Andi Kleen | e592488 | 2014-11-12 18:05:26 -0800 | [diff] [blame] | 27 | #include <regex.h> |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 28 | #include <pthread.h> |
Jiri Olsa | 4383db8 | 2012-10-27 23:18:29 +0200 | [diff] [blame] | 29 | #include <linux/bitops.h> |
Arnaldo Carvalho de Melo | 877a7a1 | 2017-04-17 11:39:06 -0300 | [diff] [blame] | 30 | #include <linux/kernel.h> |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 31 | |
Arnaldo Carvalho de Melo | a1e9b74 | 2018-03-15 19:12:39 -0300 | [diff] [blame] | 32 | /* FIXME: For the HE_COLORSET */ |
| 33 | #include "ui/browser.h" |
| 34 | |
| 35 | /* |
| 36 | * FIXME: Using the same values as slang.h, |
| 37 | * but that header may not be available everywhere |
| 38 | */ |
Arnaldo Carvalho de Melo | c298304 | 2018-03-15 23:14:51 -0300 | [diff] [blame] | 39 | #define LARROW_CHAR ((unsigned char)',') |
| 40 | #define RARROW_CHAR ((unsigned char)'+') |
| 41 | #define DARROW_CHAR ((unsigned char)'.') |
| 42 | #define UARROW_CHAR ((unsigned char)'-') |
Arnaldo Carvalho de Melo | a1e9b74 | 2018-03-15 19:12:39 -0300 | [diff] [blame] | 43 | |
Arnaldo Carvalho de Melo | 3d689ed | 2017-04-17 16:10:49 -0300 | [diff] [blame] | 44 | #include "sane_ctype.h" |
| 45 | |
Arnaldo Carvalho de Melo | 7f0b6fd | 2018-03-16 14:33:38 -0300 | [diff] [blame] | 46 | struct annotation_options annotation__default_options = { |
| 47 | .use_offset = true, |
| 48 | .jump_arrows = true, |
Arnaldo Carvalho de Melo | 592c10e | 2018-04-11 10:30:03 -0300 | [diff] [blame] | 49 | .offset_level = ANNOTATION__OFFSET_JUMP_TARGETS, |
Arnaldo Carvalho de Melo | 7f0b6fd | 2018-03-16 14:33:38 -0300 | [diff] [blame] | 50 | }; |
| 51 | |
Andi Kleen | f69b64f | 2011-09-15 14:31:41 -0700 | [diff] [blame] | 52 | const char *disassembler_style; |
Maciek Borzecki | 7a4ec93 | 2012-09-04 12:32:30 +0200 | [diff] [blame] | 53 | const char *objdump_path; |
Andi Kleen | e592488 | 2014-11-12 18:05:26 -0800 | [diff] [blame] | 54 | static regex_t file_lineno; |
Andi Kleen | f69b64f | 2011-09-15 14:31:41 -0700 | [diff] [blame] | 55 | |
Arnaldo Carvalho de Melo | 75b4920 | 2016-11-24 11:16:06 -0300 | [diff] [blame] | 56 | 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] | 57 | static void ins__sort(struct arch *arch); |
Arnaldo Carvalho de Melo | 75b4920 | 2016-11-24 11:16:06 -0300 | [diff] [blame] | 58 | 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] | 59 | |
Arnaldo Carvalho de Melo | 786c1b5 | 2016-11-16 15:39:50 -0300 | [diff] [blame] | 60 | struct arch { |
| 61 | const char *name; |
Arnaldo Carvalho de Melo | 763d896 | 2016-11-17 12:31:51 -0300 | [diff] [blame] | 62 | struct ins *instructions; |
| 63 | size_t nr_instructions; |
Arnaldo Carvalho de Melo | 2a1ff81 | 2016-11-24 11:37:08 -0300 | [diff] [blame] | 64 | size_t nr_instructions_allocated; |
| 65 | 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] | 66 | bool sorted_instructions; |
Arnaldo Carvalho de Melo | 0781ea9 | 2016-11-18 12:34:26 -0300 | [diff] [blame] | 67 | bool initialized; |
| 68 | void *priv; |
Jin Yao | 69fb09f | 2017-07-07 13:06:34 +0800 | [diff] [blame] | 69 | unsigned int model; |
| 70 | unsigned int family; |
Jiri Olsa | 696e2457 | 2017-10-11 17:01:24 +0200 | [diff] [blame] | 71 | int (*init)(struct arch *arch, char *cpuid); |
Jin Yao | 69fb09f | 2017-07-07 13:06:34 +0800 | [diff] [blame] | 72 | bool (*ins_is_fused)(struct arch *arch, const char *ins1, |
| 73 | const char *ins2); |
Arnaldo Carvalho de Melo | 786c1b5 | 2016-11-16 15:39:50 -0300 | [diff] [blame] | 74 | struct { |
| 75 | char comment_char; |
Arnaldo Carvalho de Melo | 9c2fb45 | 2016-11-16 15:50:38 -0300 | [diff] [blame] | 76 | char skip_functions_char; |
Arnaldo Carvalho de Melo | 786c1b5 | 2016-11-16 15:39:50 -0300 | [diff] [blame] | 77 | } objdump; |
| 78 | }; |
| 79 | |
Arnaldo Carvalho de Melo | 763d896 | 2016-11-17 12:31:51 -0300 | [diff] [blame] | 80 | static struct ins_ops call_ops; |
| 81 | static struct ins_ops dec_ops; |
| 82 | static struct ins_ops jump_ops; |
| 83 | static struct ins_ops mov_ops; |
| 84 | static struct ins_ops nop_ops; |
| 85 | static struct ins_ops lock_ops; |
| 86 | static struct ins_ops ret_ops; |
| 87 | |
Arnaldo Carvalho de Melo | 2a1ff81 | 2016-11-24 11:37:08 -0300 | [diff] [blame] | 88 | static int arch__grow_instructions(struct arch *arch) |
| 89 | { |
| 90 | struct ins *new_instructions; |
| 91 | size_t new_nr_allocated; |
| 92 | |
| 93 | if (arch->nr_instructions_allocated == 0 && arch->instructions) |
| 94 | goto grow_from_non_allocated_table; |
| 95 | |
| 96 | new_nr_allocated = arch->nr_instructions_allocated + 128; |
| 97 | new_instructions = realloc(arch->instructions, new_nr_allocated * sizeof(struct ins)); |
| 98 | if (new_instructions == NULL) |
| 99 | return -1; |
| 100 | |
| 101 | out_update_instructions: |
| 102 | arch->instructions = new_instructions; |
| 103 | arch->nr_instructions_allocated = new_nr_allocated; |
| 104 | return 0; |
| 105 | |
| 106 | grow_from_non_allocated_table: |
| 107 | new_nr_allocated = arch->nr_instructions + 128; |
| 108 | new_instructions = calloc(new_nr_allocated, sizeof(struct ins)); |
| 109 | if (new_instructions == NULL) |
| 110 | return -1; |
| 111 | |
| 112 | memcpy(new_instructions, arch->instructions, arch->nr_instructions); |
| 113 | goto out_update_instructions; |
| 114 | } |
| 115 | |
Arnaldo Carvalho de Melo | acc9bfb | 2016-11-18 16:54:10 -0300 | [diff] [blame] | 116 | 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] | 117 | { |
| 118 | struct ins *ins; |
| 119 | |
| 120 | if (arch->nr_instructions == arch->nr_instructions_allocated && |
| 121 | arch__grow_instructions(arch)) |
| 122 | return -1; |
| 123 | |
| 124 | ins = &arch->instructions[arch->nr_instructions]; |
| 125 | ins->name = strdup(name); |
| 126 | if (!ins->name) |
| 127 | return -1; |
| 128 | |
| 129 | ins->ops = ops; |
| 130 | arch->nr_instructions++; |
| 131 | |
| 132 | ins__sort(arch); |
| 133 | return 0; |
| 134 | } |
| 135 | |
Arnaldo Carvalho de Melo | 763d896 | 2016-11-17 12:31:51 -0300 | [diff] [blame] | 136 | #include "arch/arm/annotate/instructions.c" |
Kim Phillips | 0fcb1da | 2016-11-30 09:23:44 -0600 | [diff] [blame] | 137 | #include "arch/arm64/annotate/instructions.c" |
Arnaldo Carvalho de Melo | 763d896 | 2016-11-17 12:31:51 -0300 | [diff] [blame] | 138 | #include "arch/x86/annotate/instructions.c" |
Ravi Bangoria | dbdebdc | 2016-11-23 21:33:46 +0530 | [diff] [blame] | 139 | #include "arch/powerpc/annotate/instructions.c" |
Christian Borntraeger | d9f8dfa | 2017-04-06 09:51:52 +0200 | [diff] [blame] | 140 | #include "arch/s390/annotate/instructions.c" |
Arnaldo Carvalho de Melo | 763d896 | 2016-11-17 12:31:51 -0300 | [diff] [blame] | 141 | |
Arnaldo Carvalho de Melo | 786c1b5 | 2016-11-16 15:39:50 -0300 | [diff] [blame] | 142 | static struct arch architectures[] = { |
| 143 | { |
| 144 | .name = "arm", |
Arnaldo Carvalho de Melo | acc9bfb | 2016-11-18 16:54:10 -0300 | [diff] [blame] | 145 | .init = arm__annotate_init, |
Arnaldo Carvalho de Melo | 786c1b5 | 2016-11-16 15:39:50 -0300 | [diff] [blame] | 146 | }, |
| 147 | { |
Kim Phillips | 0fcb1da | 2016-11-30 09:23:44 -0600 | [diff] [blame] | 148 | .name = "arm64", |
| 149 | .init = arm64__annotate_init, |
| 150 | }, |
| 151 | { |
Arnaldo Carvalho de Melo | 786c1b5 | 2016-11-16 15:39:50 -0300 | [diff] [blame] | 152 | .name = "x86", |
Jiri Olsa | 696e2457 | 2017-10-11 17:01:24 +0200 | [diff] [blame] | 153 | .init = x86__annotate_init, |
Arnaldo Carvalho de Melo | 763d896 | 2016-11-17 12:31:51 -0300 | [diff] [blame] | 154 | .instructions = x86__instructions, |
| 155 | .nr_instructions = ARRAY_SIZE(x86__instructions), |
Jin Yao | 69fb09f | 2017-07-07 13:06:34 +0800 | [diff] [blame] | 156 | .ins_is_fused = x86__ins_is_fused, |
Arnaldo Carvalho de Melo | 786c1b5 | 2016-11-16 15:39:50 -0300 | [diff] [blame] | 157 | .objdump = { |
| 158 | .comment_char = '#', |
| 159 | }, |
| 160 | }, |
Ravi Bangoria | dbdebdc | 2016-11-23 21:33:46 +0530 | [diff] [blame] | 161 | { |
| 162 | .name = "powerpc", |
| 163 | .init = powerpc__annotate_init, |
| 164 | }, |
Christian Borntraeger | e77852b | 2017-04-06 09:51:51 +0200 | [diff] [blame] | 165 | { |
| 166 | .name = "s390", |
Christian Borntraeger | d9f8dfa | 2017-04-06 09:51:52 +0200 | [diff] [blame] | 167 | .init = s390__annotate_init, |
Christian Borntraeger | e77852b | 2017-04-06 09:51:51 +0200 | [diff] [blame] | 168 | .objdump = { |
| 169 | .comment_char = '#', |
| 170 | }, |
| 171 | }, |
Arnaldo Carvalho de Melo | 786c1b5 | 2016-11-16 15:39:50 -0300 | [diff] [blame] | 172 | }; |
| 173 | |
Arnaldo Carvalho de Melo | c46219ac | 2012-05-12 13:26:20 -0300 | [diff] [blame] | 174 | static void ins__delete(struct ins_operands *ops) |
| 175 | { |
Arnaldo Carvalho de Melo | 3995614 | 2015-03-05 15:27:28 -0300 | [diff] [blame] | 176 | if (ops == NULL) |
| 177 | return; |
Arnaldo Carvalho de Melo | 74cf249 | 2013-12-27 16:55:14 -0300 | [diff] [blame] | 178 | zfree(&ops->source.raw); |
| 179 | zfree(&ops->source.name); |
| 180 | zfree(&ops->target.raw); |
| 181 | zfree(&ops->target.name); |
Arnaldo Carvalho de Melo | c46219ac | 2012-05-12 13:26:20 -0300 | [diff] [blame] | 182 | } |
| 183 | |
Arnaldo Carvalho de Melo | 5417072 | 2012-05-07 18:54:16 -0300 | [diff] [blame] | 184 | static int ins__raw_scnprintf(struct ins *ins, char *bf, size_t size, |
| 185 | struct ins_operands *ops) |
| 186 | { |
Ravi Bangoria | 648388a | 2017-11-14 08:55:40 +0530 | [diff] [blame] | 187 | return scnprintf(bf, size, "%-6s %s", ins->name, ops->raw); |
Arnaldo Carvalho de Melo | 5417072 | 2012-05-07 18:54:16 -0300 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | int ins__scnprintf(struct ins *ins, char *bf, size_t size, |
| 191 | struct ins_operands *ops) |
| 192 | { |
| 193 | if (ins->ops->scnprintf) |
| 194 | return ins->ops->scnprintf(ins, bf, size, ops); |
| 195 | |
| 196 | return ins__raw_scnprintf(ins, bf, size, ops); |
| 197 | } |
| 198 | |
Jin Yao | 69fb09f | 2017-07-07 13:06:34 +0800 | [diff] [blame] | 199 | bool ins__is_fused(struct arch *arch, const char *ins1, const char *ins2) |
| 200 | { |
| 201 | if (!arch || !arch->ins_is_fused) |
| 202 | return false; |
| 203 | |
| 204 | return arch->ins_is_fused(arch, ins1, ins2); |
| 205 | } |
| 206 | |
Arnaldo Carvalho de Melo | 85a84e4 | 2018-03-20 16:19:08 -0300 | [diff] [blame] | 207 | static int call__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms) |
Arnaldo Carvalho de Melo | d86b059 | 2012-04-18 16:07:38 -0300 | [diff] [blame] | 208 | { |
Arnaldo Carvalho de Melo | d223288 | 2012-04-20 15:26:47 -0300 | [diff] [blame] | 209 | char *endptr, *tok, *name; |
Arnaldo Carvalho de Melo | 85a84e4 | 2018-03-20 16:19:08 -0300 | [diff] [blame] | 210 | struct map *map = ms->map; |
Arnaldo Carvalho de Melo | 696703a | 2018-03-02 11:59:36 -0300 | [diff] [blame] | 211 | struct addr_map_symbol target = { |
| 212 | .map = map, |
| 213 | }; |
Arnaldo Carvalho de Melo | d223288 | 2012-04-20 15:26:47 -0300 | [diff] [blame] | 214 | |
Arnaldo Carvalho de Melo | 44d1a3e | 2012-04-25 08:00:23 -0300 | [diff] [blame] | 215 | ops->target.addr = strtoull(ops->raw, &endptr, 16); |
Arnaldo Carvalho de Melo | d223288 | 2012-04-20 15:26:47 -0300 | [diff] [blame] | 216 | |
| 217 | name = strchr(endptr, '<'); |
| 218 | if (name == NULL) |
| 219 | goto indirect_call; |
| 220 | |
| 221 | name++; |
| 222 | |
Arnaldo Carvalho de Melo | 9c2fb45 | 2016-11-16 15:50:38 -0300 | [diff] [blame] | 223 | if (arch->objdump.skip_functions_char && |
| 224 | strchr(name, arch->objdump.skip_functions_char)) |
Russell King | cfef25b | 2015-12-06 23:07:13 +0000 | [diff] [blame] | 225 | return -1; |
Russell King | cfef25b | 2015-12-06 23:07:13 +0000 | [diff] [blame] | 226 | |
Arnaldo Carvalho de Melo | d223288 | 2012-04-20 15:26:47 -0300 | [diff] [blame] | 227 | tok = strchr(name, '>'); |
| 228 | if (tok == NULL) |
| 229 | return -1; |
| 230 | |
| 231 | *tok = '\0'; |
Arnaldo Carvalho de Melo | 44d1a3e | 2012-04-25 08:00:23 -0300 | [diff] [blame] | 232 | ops->target.name = strdup(name); |
Arnaldo Carvalho de Melo | d223288 | 2012-04-20 15:26:47 -0300 | [diff] [blame] | 233 | *tok = '>'; |
| 234 | |
Arnaldo Carvalho de Melo | 696703a | 2018-03-02 11:59:36 -0300 | [diff] [blame] | 235 | if (ops->target.name == NULL) |
| 236 | return -1; |
| 237 | find_target: |
| 238 | target.addr = map__objdump_2mem(map, ops->target.addr); |
| 239 | |
| 240 | if (map_groups__find_ams(&target) == 0 && |
| 241 | map__rip_2objdump(target.map, map->map_ip(target.map, target.addr)) == ops->target.addr) |
| 242 | ops->target.sym = target.sym; |
| 243 | |
| 244 | return 0; |
Arnaldo Carvalho de Melo | d223288 | 2012-04-20 15:26:47 -0300 | [diff] [blame] | 245 | |
| 246 | indirect_call: |
Ravi Bangoria | 88a7fcf | 2016-08-19 18:29:35 +0530 | [diff] [blame] | 247 | tok = strchr(endptr, '*'); |
Arnaldo Carvalho de Melo | 696703a | 2018-03-02 11:59:36 -0300 | [diff] [blame] | 248 | if (tok != NULL) |
| 249 | ops->target.addr = strtoull(tok + 1, NULL, 16); |
| 250 | goto find_target; |
Arnaldo Carvalho de Melo | d86b059 | 2012-04-18 16:07:38 -0300 | [diff] [blame] | 251 | } |
| 252 | |
Arnaldo Carvalho de Melo | d223288 | 2012-04-20 15:26:47 -0300 | [diff] [blame] | 253 | 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] | 254 | struct ins_operands *ops) |
Arnaldo Carvalho de Melo | d223288 | 2012-04-20 15:26:47 -0300 | [diff] [blame] | 255 | { |
Arnaldo Carvalho de Melo | 696703a | 2018-03-02 11:59:36 -0300 | [diff] [blame] | 256 | if (ops->target.sym) |
| 257 | 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] | 258 | |
Arnaldo Carvalho de Melo | e8ea156 | 2012-05-11 12:28:55 -0300 | [diff] [blame] | 259 | if (ops->target.addr == 0) |
| 260 | return ins__raw_scnprintf(ins, bf, size, ops); |
| 261 | |
Arnaldo Carvalho de Melo | 4c9cb2c | 2018-03-16 13:28:09 -0300 | [diff] [blame] | 262 | if (ops->target.name) |
| 263 | return scnprintf(bf, size, "%-6s %s", ins->name, ops->target.name); |
| 264 | |
Ravi Bangoria | 648388a | 2017-11-14 08:55:40 +0530 | [diff] [blame] | 265 | 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] | 266 | } |
| 267 | |
Arnaldo Carvalho de Melo | d86b059 | 2012-04-18 16:07:38 -0300 | [diff] [blame] | 268 | static struct ins_ops call_ops = { |
Arnaldo Carvalho de Melo | d223288 | 2012-04-20 15:26:47 -0300 | [diff] [blame] | 269 | .parse = call__parse, |
| 270 | .scnprintf = call__scnprintf, |
Arnaldo Carvalho de Melo | d86b059 | 2012-04-18 16:07:38 -0300 | [diff] [blame] | 271 | }; |
| 272 | |
| 273 | bool ins__is_call(const struct ins *ins) |
| 274 | { |
Thomas Richter | 0b58a77 | 2018-03-07 14:43:25 +0100 | [diff] [blame] | 275 | return ins->ops == &call_ops || ins->ops == &s390_call_ops; |
Arnaldo Carvalho de Melo | d86b059 | 2012-04-18 16:07:38 -0300 | [diff] [blame] | 276 | } |
| 277 | |
Arnaldo Carvalho de Melo | 751b178 | 2018-03-20 17:20:43 -0300 | [diff] [blame] | 278 | static int jump__parse(struct arch *arch __maybe_unused, struct ins_operands *ops, struct map_symbol *ms) |
Arnaldo Carvalho de Melo | 4f9d032 | 2012-04-18 13:58:34 -0300 | [diff] [blame] | 279 | { |
Arnaldo Carvalho de Melo | 751b178 | 2018-03-20 17:20:43 -0300 | [diff] [blame] | 280 | struct map *map = ms->map; |
| 281 | struct symbol *sym = ms->sym; |
| 282 | struct addr_map_symbol target = { |
| 283 | .map = map, |
| 284 | }; |
Ravi Bangoria | 3ee2eb6 | 2016-12-05 21:26:46 +0530 | [diff] [blame] | 285 | const char *c = strchr(ops->raw, ','); |
Arnaldo Carvalho de Melo | 751b178 | 2018-03-20 17:20:43 -0300 | [diff] [blame] | 286 | u64 start, end; |
| 287 | /* |
| 288 | * Examples of lines to parse for the _cpp_lex_token@@Base |
| 289 | * function: |
| 290 | * |
| 291 | * 1159e6c: jne 115aa32 <_cpp_lex_token@@Base+0xf92> |
| 292 | * 1159e8b: jne c469be <cpp_named_operator2name@@Base+0xa72> |
| 293 | * |
| 294 | * The first is a jump to an offset inside the same function, |
| 295 | * the second is to another function, i.e. that 0xa72 is an |
| 296 | * offset in the cpp_named_operator2name@@base function. |
| 297 | */ |
Kim Phillips | b13bbee | 2017-06-01 09:29:59 -0500 | [diff] [blame] | 298 | /* |
| 299 | * skip over possible up to 2 operands to get to address, e.g.: |
| 300 | * tbnz w0, #26, ffff0000083cd190 <security_file_permission+0xd0> |
| 301 | */ |
| 302 | if (c++ != NULL) { |
Ravi Bangoria | 3ee2eb6 | 2016-12-05 21:26:46 +0530 | [diff] [blame] | 303 | ops->target.addr = strtoull(c, NULL, 16); |
Kim Phillips | b13bbee | 2017-06-01 09:29:59 -0500 | [diff] [blame] | 304 | if (!ops->target.addr) { |
| 305 | c = strchr(c, ','); |
| 306 | if (c++ != NULL) |
| 307 | ops->target.addr = strtoull(c, NULL, 16); |
| 308 | } |
| 309 | } else { |
Ravi Bangoria | 3ee2eb6 | 2016-12-05 21:26:46 +0530 | [diff] [blame] | 310 | ops->target.addr = strtoull(ops->raw, NULL, 16); |
Kim Phillips | b13bbee | 2017-06-01 09:29:59 -0500 | [diff] [blame] | 311 | } |
Arnaldo Carvalho de Melo | 4f9d032 | 2012-04-18 13:58:34 -0300 | [diff] [blame] | 312 | |
Arnaldo Carvalho de Melo | 751b178 | 2018-03-20 17:20:43 -0300 | [diff] [blame] | 313 | target.addr = map__objdump_2mem(map, ops->target.addr); |
| 314 | start = map->unmap_ip(map, sym->start), |
| 315 | end = map->unmap_ip(map, sym->end); |
| 316 | |
| 317 | ops->target.outside = target.addr < start || target.addr > end; |
| 318 | |
| 319 | /* |
| 320 | * FIXME: things like this in _cpp_lex_token (gcc's cc1 program): |
| 321 | |
| 322 | cpp_named_operator2name@@Base+0xa72 |
| 323 | |
| 324 | * Point to a place that is after the cpp_named_operator2name |
| 325 | * boundaries, i.e. in the ELF symbol table for cc1 |
| 326 | * cpp_named_operator2name is marked as being 32-bytes long, but it in |
| 327 | * fact is much larger than that, so we seem to need a symbols__find() |
| 328 | * routine that looks for >= current->start and < next_symbol->start, |
| 329 | * possibly just for C++ objects? |
| 330 | * |
| 331 | * For now lets just make some progress by marking jumps to outside the |
| 332 | * current function as call like. |
| 333 | * |
| 334 | * Actual navigation will come next, with further understanding of how |
| 335 | * the symbol searching and disassembly should be done. |
Arnaldo Carvalho de Melo | e4cc91b | 2018-03-23 10:50:35 -0300 | [diff] [blame] | 336 | */ |
Arnaldo Carvalho de Melo | 751b178 | 2018-03-20 17:20:43 -0300 | [diff] [blame] | 337 | if (map_groups__find_ams(&target) == 0 && |
| 338 | map__rip_2objdump(target.map, map->map_ip(target.map, target.addr)) == ops->target.addr) |
| 339 | ops->target.sym = target.sym; |
Arnaldo Carvalho de Melo | 751b178 | 2018-03-20 17:20:43 -0300 | [diff] [blame] | 340 | |
Arnaldo Carvalho de Melo | 980b68e | 2018-03-23 12:26:39 -0300 | [diff] [blame] | 341 | if (!ops->target.outside) { |
| 342 | ops->target.offset = target.addr - start; |
Ravi Bangoria | e216874 | 2016-12-05 21:26:47 +0530 | [diff] [blame] | 343 | ops->target.offset_avail = true; |
| 344 | } else { |
| 345 | ops->target.offset_avail = false; |
| 346 | } |
Arnaldo Carvalho de Melo | fb29fa58 | 2012-04-25 14:16:03 -0300 | [diff] [blame] | 347 | |
Arnaldo Carvalho de Melo | 4f9d032 | 2012-04-18 13:58:34 -0300 | [diff] [blame] | 348 | return 0; |
| 349 | } |
| 350 | |
Arnaldo Carvalho de Melo | c7e6ead | 2012-04-20 14:38:46 -0300 | [diff] [blame] | 351 | 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] | 352 | struct ins_operands *ops) |
Arnaldo Carvalho de Melo | 28548d7 | 2012-04-19 10:16:27 -0300 | [diff] [blame] | 353 | { |
Arnaldo Carvalho de Melo | c448234 | 2018-03-23 10:57:08 -0300 | [diff] [blame] | 354 | const char *c; |
Kim Phillips | b13bbee | 2017-06-01 09:29:59 -0500 | [diff] [blame] | 355 | |
Ravi Bangoria | e216874 | 2016-12-05 21:26:47 +0530 | [diff] [blame] | 356 | if (!ops->target.addr || ops->target.offset < 0) |
Ravi Bangoria | bec60e5 | 2016-12-05 21:26:45 +0530 | [diff] [blame] | 357 | return ins__raw_scnprintf(ins, bf, size, ops); |
| 358 | |
Arnaldo Carvalho de Melo | e4cc91b | 2018-03-23 10:50:35 -0300 | [diff] [blame] | 359 | if (ops->target.outside && ops->target.sym != NULL) |
| 360 | return scnprintf(bf, size, "%-6s %s", ins->name, ops->target.sym->name); |
| 361 | |
Arnaldo Carvalho de Melo | c448234 | 2018-03-23 10:57:08 -0300 | [diff] [blame] | 362 | c = strchr(ops->raw, ','); |
Kim Phillips | b13bbee | 2017-06-01 09:29:59 -0500 | [diff] [blame] | 363 | if (c != NULL) { |
| 364 | const char *c2 = strchr(c + 1, ','); |
| 365 | |
| 366 | /* check for 3-op insn */ |
| 367 | if (c2 != NULL) |
| 368 | c = c2; |
| 369 | c++; |
| 370 | |
| 371 | /* mirror arch objdump's space-after-comma style */ |
| 372 | if (*c == ' ') |
| 373 | c++; |
| 374 | } |
| 375 | |
Ravi Bangoria | 648388a | 2017-11-14 08:55:40 +0530 | [diff] [blame] | 376 | return scnprintf(bf, size, "%-6s %.*s%" PRIx64, |
Kim Phillips | b13bbee | 2017-06-01 09:29:59 -0500 | [diff] [blame] | 377 | ins->name, c ? c - ops->raw : 0, ops->raw, |
| 378 | ops->target.offset); |
Arnaldo Carvalho de Melo | 28548d7 | 2012-04-19 10:16:27 -0300 | [diff] [blame] | 379 | } |
| 380 | |
Arnaldo Carvalho de Melo | 4f9d032 | 2012-04-18 13:58:34 -0300 | [diff] [blame] | 381 | static struct ins_ops jump_ops = { |
Arnaldo Carvalho de Melo | c7e6ead | 2012-04-20 14:38:46 -0300 | [diff] [blame] | 382 | .parse = jump__parse, |
| 383 | .scnprintf = jump__scnprintf, |
Arnaldo Carvalho de Melo | 4f9d032 | 2012-04-18 13:58:34 -0300 | [diff] [blame] | 384 | }; |
| 385 | |
| 386 | bool ins__is_jump(const struct ins *ins) |
| 387 | { |
| 388 | return ins->ops == &jump_ops; |
| 389 | } |
| 390 | |
Arnaldo Carvalho de Melo | 6de783b | 2012-05-11 16:48:49 -0300 | [diff] [blame] | 391 | static int comment__symbol(char *raw, char *comment, u64 *addrp, char **namep) |
| 392 | { |
| 393 | char *endptr, *name, *t; |
| 394 | |
| 395 | if (strstr(raw, "(%rip)") == NULL) |
| 396 | return 0; |
| 397 | |
| 398 | *addrp = strtoull(comment, &endptr, 16); |
Thomas Richter | 35a8a14 | 2017-11-28 08:56:32 +0100 | [diff] [blame] | 399 | if (endptr == comment) |
| 400 | return 0; |
Arnaldo Carvalho de Melo | 6de783b | 2012-05-11 16:48:49 -0300 | [diff] [blame] | 401 | name = strchr(endptr, '<'); |
| 402 | if (name == NULL) |
| 403 | return -1; |
| 404 | |
| 405 | name++; |
| 406 | |
| 407 | t = strchr(name, '>'); |
| 408 | if (t == NULL) |
| 409 | return 0; |
| 410 | |
| 411 | *t = '\0'; |
| 412 | *namep = strdup(name); |
| 413 | *t = '>'; |
| 414 | |
| 415 | return 0; |
| 416 | } |
| 417 | |
Arnaldo Carvalho de Melo | 85a84e4 | 2018-03-20 16:19:08 -0300 | [diff] [blame] | 418 | static int lock__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms) |
Arnaldo Carvalho de Melo | 7a997fe | 2012-05-12 13:15:34 -0300 | [diff] [blame] | 419 | { |
Arnaldo Carvalho de Melo | 7a997fe | 2012-05-12 13:15:34 -0300 | [diff] [blame] | 420 | ops->locked.ops = zalloc(sizeof(*ops->locked.ops)); |
| 421 | if (ops->locked.ops == NULL) |
| 422 | return 0; |
| 423 | |
Arnaldo Carvalho de Melo | 75b4920 | 2016-11-24 11:16:06 -0300 | [diff] [blame] | 424 | 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] | 425 | goto out_free_ops; |
| 426 | |
Arnaldo Carvalho de Melo | 75b4920 | 2016-11-24 11:16:06 -0300 | [diff] [blame] | 427 | ops->locked.ins.ops = ins__find(arch, ops->locked.ins.name); |
Rabin Vincent | 0fb9f2a | 2015-01-18 20:00:21 +0100 | [diff] [blame] | 428 | |
Arnaldo Carvalho de Melo | 75b4920 | 2016-11-24 11:16:06 -0300 | [diff] [blame] | 429 | if (ops->locked.ins.ops == NULL) |
Namhyung Kim | 2ba34aa | 2012-11-10 02:27:13 +0900 | [diff] [blame] | 430 | goto out_free_ops; |
Arnaldo Carvalho de Melo | 7a997fe | 2012-05-12 13:15:34 -0300 | [diff] [blame] | 431 | |
Arnaldo Carvalho de Melo | 75b4920 | 2016-11-24 11:16:06 -0300 | [diff] [blame] | 432 | if (ops->locked.ins.ops->parse && |
Arnaldo Carvalho de Melo | 85a84e4 | 2018-03-20 16:19:08 -0300 | [diff] [blame] | 433 | ops->locked.ins.ops->parse(arch, ops->locked.ops, ms) < 0) |
Rabin Vincent | be81908 | 2015-01-18 20:00:20 +0100 | [diff] [blame] | 434 | goto out_free_ops; |
Arnaldo Carvalho de Melo | 7a997fe | 2012-05-12 13:15:34 -0300 | [diff] [blame] | 435 | |
| 436 | return 0; |
| 437 | |
| 438 | out_free_ops: |
Arnaldo Carvalho de Melo | 0466252 | 2013-12-26 17:41:15 -0300 | [diff] [blame] | 439 | zfree(&ops->locked.ops); |
Arnaldo Carvalho de Melo | 7a997fe | 2012-05-12 13:15:34 -0300 | [diff] [blame] | 440 | return 0; |
| 441 | } |
| 442 | |
| 443 | static int lock__scnprintf(struct ins *ins, char *bf, size_t size, |
| 444 | struct ins_operands *ops) |
| 445 | { |
| 446 | int printed; |
| 447 | |
Arnaldo Carvalho de Melo | 75b4920 | 2016-11-24 11:16:06 -0300 | [diff] [blame] | 448 | if (ops->locked.ins.ops == NULL) |
Arnaldo Carvalho de Melo | 7a997fe | 2012-05-12 13:15:34 -0300 | [diff] [blame] | 449 | return ins__raw_scnprintf(ins, bf, size, ops); |
| 450 | |
Ravi Bangoria | 648388a | 2017-11-14 08:55:40 +0530 | [diff] [blame] | 451 | printed = scnprintf(bf, size, "%-6s ", ins->name); |
Arnaldo Carvalho de Melo | 75b4920 | 2016-11-24 11:16:06 -0300 | [diff] [blame] | 452 | return printed + ins__scnprintf(&ops->locked.ins, bf + printed, |
Arnaldo Carvalho de Melo | 7a997fe | 2012-05-12 13:15:34 -0300 | [diff] [blame] | 453 | size - printed, ops->locked.ops); |
| 454 | } |
| 455 | |
Arnaldo Carvalho de Melo | c46219ac | 2012-05-12 13:26:20 -0300 | [diff] [blame] | 456 | static void lock__delete(struct ins_operands *ops) |
| 457 | { |
Arnaldo Carvalho de Melo | 75b4920 | 2016-11-24 11:16:06 -0300 | [diff] [blame] | 458 | struct ins *ins = &ops->locked.ins; |
Rabin Vincent | 0fb9f2a | 2015-01-18 20:00:21 +0100 | [diff] [blame] | 459 | |
Arnaldo Carvalho de Melo | 75b4920 | 2016-11-24 11:16:06 -0300 | [diff] [blame] | 460 | if (ins->ops && ins->ops->free) |
Rabin Vincent | 0fb9f2a | 2015-01-18 20:00:21 +0100 | [diff] [blame] | 461 | ins->ops->free(ops->locked.ops); |
| 462 | else |
| 463 | ins__delete(ops->locked.ops); |
| 464 | |
Arnaldo Carvalho de Melo | 74cf249 | 2013-12-27 16:55:14 -0300 | [diff] [blame] | 465 | zfree(&ops->locked.ops); |
| 466 | zfree(&ops->target.raw); |
| 467 | zfree(&ops->target.name); |
Arnaldo Carvalho de Melo | c46219ac | 2012-05-12 13:26:20 -0300 | [diff] [blame] | 468 | } |
| 469 | |
Arnaldo Carvalho de Melo | 7a997fe | 2012-05-12 13:15:34 -0300 | [diff] [blame] | 470 | static struct ins_ops lock_ops = { |
Arnaldo Carvalho de Melo | c46219ac | 2012-05-12 13:26:20 -0300 | [diff] [blame] | 471 | .free = lock__delete, |
Arnaldo Carvalho de Melo | 7a997fe | 2012-05-12 13:15:34 -0300 | [diff] [blame] | 472 | .parse = lock__parse, |
| 473 | .scnprintf = lock__scnprintf, |
| 474 | }; |
| 475 | |
Arnaldo Carvalho de Melo | 85a84e4 | 2018-03-20 16:19:08 -0300 | [diff] [blame] | 476 | static int mov__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms __maybe_unused) |
Arnaldo Carvalho de Melo | 6de783b | 2012-05-11 16:48:49 -0300 | [diff] [blame] | 477 | { |
| 478 | char *s = strchr(ops->raw, ','), *target, *comment, prev; |
| 479 | |
| 480 | if (s == NULL) |
| 481 | return -1; |
| 482 | |
| 483 | *s = '\0'; |
| 484 | ops->source.raw = strdup(ops->raw); |
| 485 | *s = ','; |
Arnaldo Carvalho de Melo | 48000a1 | 2014-12-17 17:24:45 -0300 | [diff] [blame] | 486 | |
Arnaldo Carvalho de Melo | 6de783b | 2012-05-11 16:48:49 -0300 | [diff] [blame] | 487 | if (ops->source.raw == NULL) |
| 488 | return -1; |
| 489 | |
| 490 | target = ++s; |
Arnaldo Carvalho de Melo | 786c1b5 | 2016-11-16 15:39:50 -0300 | [diff] [blame] | 491 | comment = strchr(s, arch->objdump.comment_char); |
Arnaldo Carvalho de Melo | 6de783b | 2012-05-11 16:48:49 -0300 | [diff] [blame] | 492 | |
Alex Converse | 1e2bb04 | 2014-08-14 14:03:00 -0700 | [diff] [blame] | 493 | if (comment != NULL) |
| 494 | s = comment - 1; |
| 495 | else |
| 496 | s = strchr(s, '\0') - 1; |
| 497 | |
| 498 | while (s > target && isspace(s[0])) |
| 499 | --s; |
| 500 | s++; |
Arnaldo Carvalho de Melo | 6de783b | 2012-05-11 16:48:49 -0300 | [diff] [blame] | 501 | prev = *s; |
| 502 | *s = '\0'; |
| 503 | |
| 504 | ops->target.raw = strdup(target); |
| 505 | *s = prev; |
| 506 | |
| 507 | if (ops->target.raw == NULL) |
| 508 | goto out_free_source; |
| 509 | |
Arnaldo Carvalho de Melo | 6de783b | 2012-05-11 16:48:49 -0300 | [diff] [blame] | 510 | if (comment == NULL) |
| 511 | return 0; |
| 512 | |
Taeung Song | 4597cf0 | 2017-04-08 09:52:24 +0900 | [diff] [blame] | 513 | comment = ltrim(comment); |
Thomas Richter | 35a8a14 | 2017-11-28 08:56:32 +0100 | [diff] [blame] | 514 | comment__symbol(ops->source.raw, comment + 1, &ops->source.addr, &ops->source.name); |
| 515 | 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] | 516 | |
| 517 | return 0; |
| 518 | |
| 519 | out_free_source: |
Arnaldo Carvalho de Melo | 0466252 | 2013-12-26 17:41:15 -0300 | [diff] [blame] | 520 | zfree(&ops->source.raw); |
Arnaldo Carvalho de Melo | 6de783b | 2012-05-11 16:48:49 -0300 | [diff] [blame] | 521 | return -1; |
| 522 | } |
| 523 | |
| 524 | static int mov__scnprintf(struct ins *ins, char *bf, size_t size, |
| 525 | struct ins_operands *ops) |
| 526 | { |
Ravi Bangoria | 648388a | 2017-11-14 08:55:40 +0530 | [diff] [blame] | 527 | return scnprintf(bf, size, "%-6s %s,%s", ins->name, |
Arnaldo Carvalho de Melo | 6de783b | 2012-05-11 16:48:49 -0300 | [diff] [blame] | 528 | ops->source.name ?: ops->source.raw, |
| 529 | ops->target.name ?: ops->target.raw); |
| 530 | } |
| 531 | |
| 532 | static struct ins_ops mov_ops = { |
| 533 | .parse = mov__parse, |
| 534 | .scnprintf = mov__scnprintf, |
| 535 | }; |
| 536 | |
Arnaldo Carvalho de Melo | 85a84e4 | 2018-03-20 16:19:08 -0300 | [diff] [blame] | 537 | static int dec__parse(struct arch *arch __maybe_unused, struct ins_operands *ops, struct map_symbol *ms __maybe_unused) |
Arnaldo Carvalho de Melo | a43712c | 2012-05-11 17:21:09 -0300 | [diff] [blame] | 538 | { |
| 539 | char *target, *comment, *s, prev; |
| 540 | |
| 541 | target = s = ops->raw; |
| 542 | |
| 543 | while (s[0] != '\0' && !isspace(s[0])) |
| 544 | ++s; |
| 545 | prev = *s; |
| 546 | *s = '\0'; |
| 547 | |
| 548 | ops->target.raw = strdup(target); |
| 549 | *s = prev; |
| 550 | |
| 551 | if (ops->target.raw == NULL) |
| 552 | return -1; |
| 553 | |
Kim Phillips | 859afa6 | 2016-11-30 09:23:33 -0600 | [diff] [blame] | 554 | comment = strchr(s, arch->objdump.comment_char); |
Arnaldo Carvalho de Melo | a43712c | 2012-05-11 17:21:09 -0300 | [diff] [blame] | 555 | if (comment == NULL) |
| 556 | return 0; |
| 557 | |
Taeung Song | 4597cf0 | 2017-04-08 09:52:24 +0900 | [diff] [blame] | 558 | comment = ltrim(comment); |
Thomas Richter | 35a8a14 | 2017-11-28 08:56:32 +0100 | [diff] [blame] | 559 | 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] | 560 | |
| 561 | return 0; |
| 562 | } |
| 563 | |
| 564 | static int dec__scnprintf(struct ins *ins, char *bf, size_t size, |
| 565 | struct ins_operands *ops) |
| 566 | { |
Ravi Bangoria | 648388a | 2017-11-14 08:55:40 +0530 | [diff] [blame] | 567 | return scnprintf(bf, size, "%-6s %s", ins->name, |
Arnaldo Carvalho de Melo | a43712c | 2012-05-11 17:21:09 -0300 | [diff] [blame] | 568 | ops->target.name ?: ops->target.raw); |
| 569 | } |
| 570 | |
| 571 | static struct ins_ops dec_ops = { |
| 572 | .parse = dec__parse, |
| 573 | .scnprintf = dec__scnprintf, |
| 574 | }; |
| 575 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 576 | static int nop__scnprintf(struct ins *ins __maybe_unused, char *bf, size_t size, |
| 577 | struct ins_operands *ops __maybe_unused) |
Arnaldo Carvalho de Melo | b9818e9 | 2012-05-07 18:57:02 -0300 | [diff] [blame] | 578 | { |
Ravi Bangoria | 648388a | 2017-11-14 08:55:40 +0530 | [diff] [blame] | 579 | return scnprintf(bf, size, "%-6s", "nop"); |
Arnaldo Carvalho de Melo | b9818e9 | 2012-05-07 18:57:02 -0300 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | static struct ins_ops nop_ops = { |
| 583 | .scnprintf = nop__scnprintf, |
| 584 | }; |
| 585 | |
Naveen N. Rao | 6ef9492 | 2016-06-24 17:23:58 +0530 | [diff] [blame] | 586 | static struct ins_ops ret_ops = { |
| 587 | .scnprintf = ins__raw_scnprintf, |
| 588 | }; |
| 589 | |
| 590 | bool ins__is_ret(const struct ins *ins) |
| 591 | { |
| 592 | return ins->ops == &ret_ops; |
| 593 | } |
| 594 | |
Jin Yao | 7e63a13 | 2017-07-07 13:06:35 +0800 | [diff] [blame] | 595 | bool ins__is_lock(const struct ins *ins) |
| 596 | { |
| 597 | return ins->ops == &lock_ops; |
| 598 | } |
| 599 | |
Chris Ryder | 7e4c149 | 2016-05-19 17:59:46 +0100 | [diff] [blame] | 600 | 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] | 601 | { |
| 602 | const struct ins *ins = insp; |
| 603 | |
| 604 | return strcmp(name, ins->name); |
| 605 | } |
| 606 | |
Chris Ryder | 7e4c149 | 2016-05-19 17:59:46 +0100 | [diff] [blame] | 607 | static int ins__cmp(const void *a, const void *b) |
| 608 | { |
| 609 | const struct ins *ia = a; |
| 610 | const struct ins *ib = b; |
| 611 | |
| 612 | return strcmp(ia->name, ib->name); |
| 613 | } |
| 614 | |
Arnaldo Carvalho de Melo | 763d896 | 2016-11-17 12:31:51 -0300 | [diff] [blame] | 615 | static void ins__sort(struct arch *arch) |
Arnaldo Carvalho de Melo | 4f9d032 | 2012-04-18 13:58:34 -0300 | [diff] [blame] | 616 | { |
Arnaldo Carvalho de Melo | 763d896 | 2016-11-17 12:31:51 -0300 | [diff] [blame] | 617 | const int nmemb = arch->nr_instructions; |
Arnaldo Carvalho de Melo | 4f9d032 | 2012-04-18 13:58:34 -0300 | [diff] [blame] | 618 | |
Arnaldo Carvalho de Melo | 763d896 | 2016-11-17 12:31:51 -0300 | [diff] [blame] | 619 | qsort(arch->instructions, nmemb, sizeof(struct ins), ins__cmp); |
Chris Ryder | 7e4c149 | 2016-05-19 17:59:46 +0100 | [diff] [blame] | 620 | } |
| 621 | |
Arnaldo Carvalho de Melo | 2a1ff81 | 2016-11-24 11:37:08 -0300 | [diff] [blame] | 622 | static struct ins_ops *__ins__find(struct arch *arch, const char *name) |
Chris Ryder | 7e4c149 | 2016-05-19 17:59:46 +0100 | [diff] [blame] | 623 | { |
Arnaldo Carvalho de Melo | 75b4920 | 2016-11-24 11:16:06 -0300 | [diff] [blame] | 624 | struct ins *ins; |
Arnaldo Carvalho de Melo | 763d896 | 2016-11-17 12:31:51 -0300 | [diff] [blame] | 625 | const int nmemb = arch->nr_instructions; |
Chris Ryder | 7e4c149 | 2016-05-19 17:59:46 +0100 | [diff] [blame] | 626 | |
Arnaldo Carvalho de Melo | 763d896 | 2016-11-17 12:31:51 -0300 | [diff] [blame] | 627 | if (!arch->sorted_instructions) { |
| 628 | ins__sort(arch); |
| 629 | arch->sorted_instructions = true; |
Chris Ryder | 7e4c149 | 2016-05-19 17:59:46 +0100 | [diff] [blame] | 630 | } |
| 631 | |
Arnaldo Carvalho de Melo | 75b4920 | 2016-11-24 11:16:06 -0300 | [diff] [blame] | 632 | ins = bsearch(name, arch->instructions, nmemb, sizeof(struct ins), ins__key_cmp); |
| 633 | return ins ? ins->ops : NULL; |
Arnaldo Carvalho de Melo | 4f9d032 | 2012-04-18 13:58:34 -0300 | [diff] [blame] | 634 | } |
| 635 | |
Arnaldo Carvalho de Melo | 2a1ff81 | 2016-11-24 11:37:08 -0300 | [diff] [blame] | 636 | static struct ins_ops *ins__find(struct arch *arch, const char *name) |
| 637 | { |
| 638 | struct ins_ops *ops = __ins__find(arch, name); |
| 639 | |
| 640 | if (!ops && arch->associate_instruction_ops) |
| 641 | ops = arch->associate_instruction_ops(arch, name); |
| 642 | |
| 643 | return ops; |
| 644 | } |
| 645 | |
Arnaldo Carvalho de Melo | 786c1b5 | 2016-11-16 15:39:50 -0300 | [diff] [blame] | 646 | static int arch__key_cmp(const void *name, const void *archp) |
| 647 | { |
| 648 | const struct arch *arch = archp; |
| 649 | |
| 650 | return strcmp(name, arch->name); |
| 651 | } |
| 652 | |
| 653 | static int arch__cmp(const void *a, const void *b) |
| 654 | { |
| 655 | const struct arch *aa = a; |
| 656 | const struct arch *ab = b; |
| 657 | |
| 658 | return strcmp(aa->name, ab->name); |
| 659 | } |
| 660 | |
| 661 | static void arch__sort(void) |
| 662 | { |
| 663 | const int nmemb = ARRAY_SIZE(architectures); |
| 664 | |
| 665 | qsort(architectures, nmemb, sizeof(struct arch), arch__cmp); |
| 666 | } |
| 667 | |
| 668 | static struct arch *arch__find(const char *name) |
| 669 | { |
| 670 | const int nmemb = ARRAY_SIZE(architectures); |
| 671 | static bool sorted; |
| 672 | |
| 673 | if (!sorted) { |
| 674 | arch__sort(); |
| 675 | sorted = true; |
| 676 | } |
| 677 | |
| 678 | return bsearch(name, architectures, nmemb, sizeof(struct arch), arch__key_cmp); |
| 679 | } |
| 680 | |
Arnaldo Carvalho de Melo | ca39650 | 2018-05-24 16:01:31 -0300 | [diff] [blame] | 681 | static struct annotated_source *annotated_source__new(void) |
| 682 | { |
| 683 | struct annotated_source *src = zalloc(sizeof(*src)); |
| 684 | |
| 685 | if (src != NULL) |
| 686 | INIT_LIST_HEAD(&src->source); |
| 687 | |
| 688 | return src; |
| 689 | } |
| 690 | |
| 691 | static void annotated_source__delete(struct annotated_source *src) |
| 692 | { |
| 693 | if (src == NULL) |
| 694 | return; |
| 695 | zfree(&src->histograms); |
| 696 | zfree(&src->cycles_hist); |
| 697 | free(src); |
| 698 | } |
| 699 | |
Arnaldo Carvalho de Melo | be3e26d | 2018-05-24 16:23:08 -0300 | [diff] [blame] | 700 | static int annotated_source__alloc_histograms(struct annotated_source *src, |
| 701 | size_t size, int nr_hists) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 702 | { |
Cody Schafer | 8696329 | 2012-07-19 20:05:25 -0700 | [diff] [blame] | 703 | size_t sizeof_sym_hist; |
| 704 | |
Ravi Bangoria | 331c7cb | 2017-10-24 19:50:06 +0530 | [diff] [blame] | 705 | /* |
| 706 | * Add buffer of one element for zero length symbol. |
| 707 | * When sample is taken from first instruction of |
| 708 | * zero length symbol, perf still resolves it and |
| 709 | * shows symbol name in perf report and allows to |
| 710 | * annotate it. |
| 711 | */ |
| 712 | if (size == 0) |
| 713 | size = 1; |
| 714 | |
Cody Schafer | 8696329 | 2012-07-19 20:05:25 -0700 | [diff] [blame] | 715 | /* Check for overflow when calculating sizeof_sym_hist */ |
Taeung Song | 896bccd | 2017-07-20 06:36:45 +0900 | [diff] [blame] | 716 | 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] | 717 | return -1; |
| 718 | |
Taeung Song | 896bccd | 2017-07-20 06:36:45 +0900 | [diff] [blame] | 719 | 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] | 720 | |
| 721 | /* Check for overflow in zalloc argument */ |
Arnaldo Carvalho de Melo | be3e26d | 2018-05-24 16:23:08 -0300 | [diff] [blame] | 722 | if (sizeof_sym_hist > SIZE_MAX / nr_hists) |
Cody Schafer | 8696329 | 2012-07-19 20:05:25 -0700 | [diff] [blame] | 723 | return -1; |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 724 | |
Arnaldo Carvalho de Melo | be3e26d | 2018-05-24 16:23:08 -0300 | [diff] [blame] | 725 | src->sizeof_sym_hist = sizeof_sym_hist; |
| 726 | src->nr_histograms = nr_hists; |
| 727 | src->histograms = calloc(nr_hists, sizeof_sym_hist) ; |
| 728 | return src->histograms ? 0 : -1; |
| 729 | } |
| 730 | |
| 731 | int symbol__alloc_hist(struct symbol *sym) |
| 732 | { |
| 733 | size_t size = symbol__size(sym); |
| 734 | struct annotation *notes = symbol__annotation(sym); |
| 735 | |
Arnaldo Carvalho de Melo | ca39650 | 2018-05-24 16:01:31 -0300 | [diff] [blame] | 736 | notes->src = annotated_source__new(); |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 737 | if (notes->src == NULL) |
| 738 | return -1; |
Arnaldo Carvalho de Melo | be3e26d | 2018-05-24 16:23:08 -0300 | [diff] [blame] | 739 | |
| 740 | if (annotated_source__alloc_histograms(notes->src, size, symbol_conf.nr_events) < 0) { |
Arnaldo Carvalho de Melo | ca39650 | 2018-05-24 16:01:31 -0300 | [diff] [blame] | 741 | annotated_source__delete(notes->src); |
| 742 | notes->src = NULL; |
Arnaldo Carvalho de Melo | 116c626 | 2018-05-24 15:31:40 -0300 | [diff] [blame] | 743 | return -1; |
| 744 | } |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 745 | return 0; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 746 | } |
| 747 | |
Andi Kleen | d495763 | 2015-07-18 08:24:48 -0700 | [diff] [blame] | 748 | /* The cycles histogram is lazily allocated. */ |
| 749 | static int symbol__alloc_hist_cycles(struct symbol *sym) |
| 750 | { |
| 751 | struct annotation *notes = symbol__annotation(sym); |
| 752 | const size_t size = symbol__size(sym); |
| 753 | |
| 754 | notes->src->cycles_hist = calloc(size, sizeof(struct cyc_hist)); |
| 755 | if (notes->src->cycles_hist == NULL) |
| 756 | return -1; |
| 757 | return 0; |
| 758 | } |
| 759 | |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 760 | void symbol__annotate_zero_histograms(struct symbol *sym) |
| 761 | { |
| 762 | struct annotation *notes = symbol__annotation(sym); |
| 763 | |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 764 | pthread_mutex_lock(¬es->lock); |
Andi Kleen | d495763 | 2015-07-18 08:24:48 -0700 | [diff] [blame] | 765 | if (notes->src != NULL) { |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 766 | memset(notes->src->histograms, 0, |
| 767 | notes->src->nr_histograms * notes->src->sizeof_sym_hist); |
Andi Kleen | d495763 | 2015-07-18 08:24:48 -0700 | [diff] [blame] | 768 | if (notes->src->cycles_hist) |
| 769 | memset(notes->src->cycles_hist, 0, |
| 770 | symbol__size(sym) * sizeof(struct cyc_hist)); |
| 771 | } |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 772 | pthread_mutex_unlock(¬es->lock); |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 773 | } |
| 774 | |
Arnaldo Carvalho de Melo | f40dd6d | 2018-05-24 15:20:18 -0300 | [diff] [blame] | 775 | static int __symbol__account_cycles(struct cyc_hist *ch, |
Andi Kleen | d495763 | 2015-07-18 08:24:48 -0700 | [diff] [blame] | 776 | u64 start, |
| 777 | unsigned offset, unsigned cycles, |
| 778 | unsigned have_start) |
| 779 | { |
Andi Kleen | d495763 | 2015-07-18 08:24:48 -0700 | [diff] [blame] | 780 | /* |
| 781 | * For now we can only account one basic block per |
| 782 | * final jump. But multiple could be overlapping. |
| 783 | * Always account the longest one. So when |
| 784 | * a shorter one has been already seen throw it away. |
| 785 | * |
| 786 | * We separately always account the full cycles. |
| 787 | */ |
| 788 | ch[offset].num_aggr++; |
| 789 | ch[offset].cycles_aggr += cycles; |
| 790 | |
Jin Yao | 48659eb | 2018-05-17 22:58:37 +0800 | [diff] [blame] | 791 | if (cycles > ch[offset].cycles_max) |
| 792 | ch[offset].cycles_max = cycles; |
| 793 | |
| 794 | if (ch[offset].cycles_min) { |
| 795 | if (cycles && cycles < ch[offset].cycles_min) |
| 796 | ch[offset].cycles_min = cycles; |
| 797 | } else |
| 798 | ch[offset].cycles_min = cycles; |
| 799 | |
Andi Kleen | d495763 | 2015-07-18 08:24:48 -0700 | [diff] [blame] | 800 | if (!have_start && ch[offset].have_start) |
| 801 | return 0; |
| 802 | if (ch[offset].num) { |
| 803 | if (have_start && (!ch[offset].have_start || |
| 804 | ch[offset].start > start)) { |
| 805 | ch[offset].have_start = 0; |
| 806 | ch[offset].cycles = 0; |
| 807 | ch[offset].num = 0; |
| 808 | if (ch[offset].reset < 0xffff) |
| 809 | ch[offset].reset++; |
| 810 | } else if (have_start && |
| 811 | ch[offset].start < start) |
| 812 | return 0; |
| 813 | } |
| 814 | ch[offset].have_start = have_start; |
| 815 | ch[offset].start = start; |
| 816 | ch[offset].cycles += cycles; |
| 817 | ch[offset].num++; |
| 818 | return 0; |
| 819 | } |
| 820 | |
Arnaldo Carvalho de Melo | b66d8c0 | 2013-12-18 15:37:41 -0300 | [diff] [blame] | 821 | static int __symbol__inc_addr_samples(struct symbol *sym, struct map *map, |
Arnaldo Carvalho de Melo | e1a91a8 | 2018-05-24 16:28:29 -0300 | [diff] [blame^] | 822 | struct annotated_source *src, int evidx, u64 addr, |
Taeung Song | 461c17f | 2017-07-20 17:18:05 -0300 | [diff] [blame] | 823 | struct perf_sample *sample) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 824 | { |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame] | 825 | unsigned offset; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 826 | struct sym_hist *h; |
| 827 | |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 828 | pr_debug3("%s: addr=%#" PRIx64 "\n", __func__, map->unmap_ip(map, addr)); |
| 829 | |
Ravi Bangoria | edee44b | 2016-11-22 14:10:50 +0530 | [diff] [blame] | 830 | if ((addr < sym->start || addr >= sym->end) && |
| 831 | (addr != sym->end || sym->start != sym->end)) { |
Arnaldo Carvalho de Melo | e3d006c | 2015-10-21 15:45:13 -0300 | [diff] [blame] | 832 | pr_debug("%s(%d): ERANGE! sym->name=%s, start=%#" PRIx64 ", addr=%#" PRIx64 ", end=%#" PRIx64 "\n", |
| 833 | __func__, __LINE__, sym->name, sym->start, addr, sym->end); |
Arnaldo Carvalho de Melo | 31d68e7 | 2012-03-27 12:55:57 -0300 | [diff] [blame] | 834 | return -ERANGE; |
Arnaldo Carvalho de Melo | e3d006c | 2015-10-21 15:45:13 -0300 | [diff] [blame] | 835 | } |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 836 | |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame] | 837 | offset = addr - sym->start; |
Arnaldo Carvalho de Melo | e1a91a8 | 2018-05-24 16:28:29 -0300 | [diff] [blame^] | 838 | h = annotated_source__histogram(src, evidx); |
Taeung Song | 8158683d | 2017-07-20 06:36:51 +0900 | [diff] [blame] | 839 | h->nr_samples++; |
Taeung Song | 896bccd | 2017-07-20 06:36:45 +0900 | [diff] [blame] | 840 | h->addr[offset].nr_samples++; |
Taeung Song | 461c17f | 2017-07-20 17:18:05 -0300 | [diff] [blame] | 841 | h->period += sample->period; |
| 842 | h->addr[offset].period += sample->period; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 843 | |
| 844 | pr_debug3("%#" PRIx64 " %s: period++ [addr: %#" PRIx64 ", %#" PRIx64 |
Taeung Song | 461c17f | 2017-07-20 17:18:05 -0300 | [diff] [blame] | 845 | ", evidx=%d] => nr_samples: %" PRIu64 ", period: %" PRIu64 "\n", |
| 846 | sym->start, sym->name, addr, addr - sym->start, evidx, |
| 847 | h->addr[offset].nr_samples, h->addr[offset].period); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 848 | return 0; |
| 849 | } |
| 850 | |
Andi Kleen | d495763 | 2015-07-18 08:24:48 -0700 | [diff] [blame] | 851 | static struct annotation *symbol__get_annotation(struct symbol *sym, bool cycles) |
Andi Kleen | 83be34a | 2015-05-27 10:51:46 -0700 | [diff] [blame] | 852 | { |
| 853 | struct annotation *notes = symbol__annotation(sym); |
| 854 | |
| 855 | if (notes->src == NULL) { |
| 856 | if (symbol__alloc_hist(sym) < 0) |
| 857 | return NULL; |
| 858 | } |
Andi Kleen | d495763 | 2015-07-18 08:24:48 -0700 | [diff] [blame] | 859 | if (!notes->src->cycles_hist && cycles) { |
| 860 | if (symbol__alloc_hist_cycles(sym) < 0) |
| 861 | return NULL; |
| 862 | } |
Andi Kleen | 83be34a | 2015-05-27 10:51:46 -0700 | [diff] [blame] | 863 | return notes; |
| 864 | } |
| 865 | |
Arnaldo Carvalho de Melo | 44e8303 | 2013-12-18 17:12:24 -0300 | [diff] [blame] | 866 | static int symbol__inc_addr_samples(struct symbol *sym, struct map *map, |
Arnaldo Carvalho de Melo | e345f3b | 2018-05-24 12:05:39 -0300 | [diff] [blame] | 867 | struct perf_evsel *evsel, u64 addr, |
Taeung Song | bab89f6 | 2017-07-20 16:28:53 -0300 | [diff] [blame] | 868 | struct perf_sample *sample) |
Arnaldo Carvalho de Melo | b66d8c0 | 2013-12-18 15:37:41 -0300 | [diff] [blame] | 869 | { |
| 870 | struct annotation *notes; |
| 871 | |
Namhyung Kim | 48c65bd | 2014-02-20 10:32:53 +0900 | [diff] [blame] | 872 | if (sym == NULL) |
Arnaldo Carvalho de Melo | b66d8c0 | 2013-12-18 15:37:41 -0300 | [diff] [blame] | 873 | return 0; |
Andi Kleen | d495763 | 2015-07-18 08:24:48 -0700 | [diff] [blame] | 874 | notes = symbol__get_annotation(sym, false); |
Andi Kleen | 83be34a | 2015-05-27 10:51:46 -0700 | [diff] [blame] | 875 | if (notes == NULL) |
| 876 | return -ENOMEM; |
Arnaldo Carvalho de Melo | e1a91a8 | 2018-05-24 16:28:29 -0300 | [diff] [blame^] | 877 | return __symbol__inc_addr_samples(sym, map, notes->src, evsel->idx, addr, sample); |
Arnaldo Carvalho de Melo | b66d8c0 | 2013-12-18 15:37:41 -0300 | [diff] [blame] | 878 | } |
| 879 | |
Andi Kleen | d495763 | 2015-07-18 08:24:48 -0700 | [diff] [blame] | 880 | static int symbol__account_cycles(u64 addr, u64 start, |
| 881 | struct symbol *sym, unsigned cycles) |
| 882 | { |
| 883 | struct annotation *notes; |
| 884 | unsigned offset; |
| 885 | |
| 886 | if (sym == NULL) |
| 887 | return 0; |
| 888 | notes = symbol__get_annotation(sym, true); |
| 889 | if (notes == NULL) |
| 890 | return -ENOMEM; |
| 891 | if (addr < sym->start || addr >= sym->end) |
| 892 | return -ERANGE; |
| 893 | |
| 894 | if (start) { |
| 895 | if (start < sym->start || start >= sym->end) |
| 896 | return -ERANGE; |
| 897 | if (start >= addr) |
| 898 | start = 0; |
| 899 | } |
| 900 | offset = addr - sym->start; |
Arnaldo Carvalho de Melo | f40dd6d | 2018-05-24 15:20:18 -0300 | [diff] [blame] | 901 | return __symbol__account_cycles(notes->src->cycles_hist, |
Andi Kleen | d495763 | 2015-07-18 08:24:48 -0700 | [diff] [blame] | 902 | start ? start - sym->start : 0, |
| 903 | offset, cycles, |
| 904 | !!start); |
| 905 | } |
| 906 | |
| 907 | int addr_map_symbol__account_cycles(struct addr_map_symbol *ams, |
| 908 | struct addr_map_symbol *start, |
| 909 | unsigned cycles) |
| 910 | { |
Adrian Hunter | 3d7245b | 2015-08-14 10:11:34 +0300 | [diff] [blame] | 911 | u64 saddr = 0; |
Andi Kleen | d495763 | 2015-07-18 08:24:48 -0700 | [diff] [blame] | 912 | int err; |
| 913 | |
| 914 | if (!cycles) |
| 915 | return 0; |
| 916 | |
| 917 | /* |
| 918 | * Only set start when IPC can be computed. We can only |
| 919 | * compute it when the basic block is completely in a single |
| 920 | * function. |
| 921 | * Special case the case when the jump is elsewhere, but |
| 922 | * it starts on the function start. |
| 923 | */ |
| 924 | if (start && |
| 925 | (start->sym == ams->sym || |
| 926 | (ams->sym && |
| 927 | start->addr == ams->sym->start + ams->map->start))) |
| 928 | saddr = start->al_addr; |
| 929 | if (saddr == 0) |
Adrian Hunter | 3d7245b | 2015-08-14 10:11:34 +0300 | [diff] [blame] | 930 | 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] | 931 | ams->addr, |
| 932 | start ? start->addr : 0, |
| 933 | ams->sym ? ams->sym->start + ams->map->start : 0, |
| 934 | saddr); |
| 935 | err = symbol__account_cycles(ams->al_addr, saddr, ams->sym, cycles); |
| 936 | if (err) |
| 937 | pr_debug2("account_cycles failed %d\n", err); |
| 938 | return err; |
| 939 | } |
| 940 | |
Arnaldo Carvalho de Melo | f56c083 | 2018-03-15 11:46:23 -0300 | [diff] [blame] | 941 | static unsigned annotation__count_insn(struct annotation *notes, u64 start, u64 end) |
| 942 | { |
| 943 | unsigned n_insn = 0; |
| 944 | u64 offset; |
| 945 | |
| 946 | for (offset = start; offset <= end; offset++) { |
| 947 | if (notes->offsets[offset]) |
| 948 | n_insn++; |
| 949 | } |
| 950 | return n_insn; |
| 951 | } |
| 952 | |
| 953 | static void annotation__count_and_fill(struct annotation *notes, u64 start, u64 end, struct cyc_hist *ch) |
| 954 | { |
| 955 | unsigned n_insn; |
| 956 | u64 offset; |
| 957 | |
| 958 | n_insn = annotation__count_insn(notes, start, end); |
| 959 | if (n_insn && ch->num && ch->cycles) { |
| 960 | float ipc = n_insn / ((double)ch->cycles / (double)ch->num); |
| 961 | |
| 962 | /* Hide data when there are too many overlaps. */ |
| 963 | if (ch->reset >= 0x7fff || ch->reset >= ch->num / 2) |
| 964 | return; |
| 965 | |
| 966 | for (offset = start; offset <= end; offset++) { |
| 967 | struct annotation_line *al = notes->offsets[offset]; |
| 968 | |
| 969 | if (al) |
| 970 | al->ipc = ipc; |
| 971 | } |
| 972 | } |
| 973 | } |
| 974 | |
| 975 | void annotation__compute_ipc(struct annotation *notes, size_t size) |
| 976 | { |
| 977 | u64 offset; |
| 978 | |
| 979 | if (!notes->src || !notes->src->cycles_hist) |
| 980 | return; |
| 981 | |
| 982 | pthread_mutex_lock(¬es->lock); |
| 983 | for (offset = 0; offset < size; ++offset) { |
| 984 | struct cyc_hist *ch; |
| 985 | |
| 986 | ch = ¬es->src->cycles_hist[offset]; |
| 987 | if (ch && ch->cycles) { |
| 988 | struct annotation_line *al; |
| 989 | |
| 990 | if (ch->have_start) |
| 991 | annotation__count_and_fill(notes, ch->start, offset, ch); |
| 992 | al = notes->offsets[offset]; |
Jin Yao | 48659eb | 2018-05-17 22:58:37 +0800 | [diff] [blame] | 993 | if (al && ch->num_aggr) { |
Arnaldo Carvalho de Melo | f56c083 | 2018-03-15 11:46:23 -0300 | [diff] [blame] | 994 | al->cycles = ch->cycles_aggr / ch->num_aggr; |
Jin Yao | 48659eb | 2018-05-17 22:58:37 +0800 | [diff] [blame] | 995 | al->cycles_max = ch->cycles_max; |
| 996 | al->cycles_min = ch->cycles_min; |
| 997 | } |
Arnaldo Carvalho de Melo | f56c083 | 2018-03-15 11:46:23 -0300 | [diff] [blame] | 998 | notes->have_cycles = true; |
| 999 | } |
| 1000 | } |
| 1001 | pthread_mutex_unlock(¬es->lock); |
| 1002 | } |
| 1003 | |
Taeung Song | bab89f6 | 2017-07-20 16:28:53 -0300 | [diff] [blame] | 1004 | int addr_map_symbol__inc_samples(struct addr_map_symbol *ams, struct perf_sample *sample, |
Arnaldo Carvalho de Melo | e345f3b | 2018-05-24 12:05:39 -0300 | [diff] [blame] | 1005 | struct perf_evsel *evsel) |
Arnaldo Carvalho de Melo | 0f4e7a2 | 2013-12-18 16:48:29 -0300 | [diff] [blame] | 1006 | { |
Arnaldo Carvalho de Melo | e345f3b | 2018-05-24 12:05:39 -0300 | [diff] [blame] | 1007 | return symbol__inc_addr_samples(ams->sym, ams->map, evsel, ams->al_addr, sample); |
Arnaldo Carvalho de Melo | 0f4e7a2 | 2013-12-18 16:48:29 -0300 | [diff] [blame] | 1008 | } |
| 1009 | |
Taeung Song | bab89f6 | 2017-07-20 16:28:53 -0300 | [diff] [blame] | 1010 | int hist_entry__inc_addr_samples(struct hist_entry *he, struct perf_sample *sample, |
Arnaldo Carvalho de Melo | e345f3b | 2018-05-24 12:05:39 -0300 | [diff] [blame] | 1011 | struct perf_evsel *evsel, u64 ip) |
Arnaldo Carvalho de Melo | f626adf | 2013-12-18 17:10:15 -0300 | [diff] [blame] | 1012 | { |
Arnaldo Carvalho de Melo | e345f3b | 2018-05-24 12:05:39 -0300 | [diff] [blame] | 1013 | return symbol__inc_addr_samples(he->ms.sym, he->ms.map, evsel, ip, sample); |
Arnaldo Carvalho de Melo | f626adf | 2013-12-18 17:10:15 -0300 | [diff] [blame] | 1014 | } |
| 1015 | |
Arnaldo Carvalho de Melo | 85a84e4 | 2018-03-20 16:19:08 -0300 | [diff] [blame] | 1016 | static void disasm_line__init_ins(struct disasm_line *dl, struct arch *arch, struct map_symbol *ms) |
Arnaldo Carvalho de Melo | 4f9d032 | 2012-04-18 13:58:34 -0300 | [diff] [blame] | 1017 | { |
Arnaldo Carvalho de Melo | 75b4920 | 2016-11-24 11:16:06 -0300 | [diff] [blame] | 1018 | dl->ins.ops = ins__find(arch, dl->ins.name); |
Arnaldo Carvalho de Melo | 4f9d032 | 2012-04-18 13:58:34 -0300 | [diff] [blame] | 1019 | |
Arnaldo Carvalho de Melo | 75b4920 | 2016-11-24 11:16:06 -0300 | [diff] [blame] | 1020 | if (!dl->ins.ops) |
Arnaldo Carvalho de Melo | 4f9d032 | 2012-04-18 13:58:34 -0300 | [diff] [blame] | 1021 | return; |
| 1022 | |
Arnaldo Carvalho de Melo | 85a84e4 | 2018-03-20 16:19:08 -0300 | [diff] [blame] | 1023 | if (dl->ins.ops->parse && dl->ins.ops->parse(arch, &dl->ops, ms) < 0) |
Arnaldo Carvalho de Melo | 75b4920 | 2016-11-24 11:16:06 -0300 | [diff] [blame] | 1024 | dl->ins.ops = NULL; |
Arnaldo Carvalho de Melo | 4f9d032 | 2012-04-18 13:58:34 -0300 | [diff] [blame] | 1025 | } |
| 1026 | |
Arnaldo Carvalho de Melo | 75b4920 | 2016-11-24 11:16:06 -0300 | [diff] [blame] | 1027 | 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] | 1028 | { |
Taeung Song | 4597cf0 | 2017-04-08 09:52:24 +0900 | [diff] [blame] | 1029 | char tmp, *name = ltrim(line); |
Arnaldo Carvalho de Melo | 7a997fe | 2012-05-12 13:15:34 -0300 | [diff] [blame] | 1030 | |
| 1031 | if (name[0] == '\0') |
| 1032 | return -1; |
| 1033 | |
| 1034 | *rawp = name + 1; |
| 1035 | |
| 1036 | while ((*rawp)[0] != '\0' && !isspace((*rawp)[0])) |
| 1037 | ++*rawp; |
| 1038 | |
| 1039 | tmp = (*rawp)[0]; |
| 1040 | (*rawp)[0] = '\0'; |
| 1041 | *namep = strdup(name); |
| 1042 | |
| 1043 | if (*namep == NULL) |
| 1044 | goto out_free_name; |
| 1045 | |
| 1046 | (*rawp)[0] = tmp; |
Taeung Song | 4597cf0 | 2017-04-08 09:52:24 +0900 | [diff] [blame] | 1047 | *rawp = ltrim(*rawp); |
Arnaldo Carvalho de Melo | 7a997fe | 2012-05-12 13:15:34 -0300 | [diff] [blame] | 1048 | |
| 1049 | return 0; |
| 1050 | |
| 1051 | out_free_name: |
Arnaldo Carvalho de Melo | 75b4920 | 2016-11-24 11:16:06 -0300 | [diff] [blame] | 1052 | free((void *)namep); |
| 1053 | *namep = NULL; |
Arnaldo Carvalho de Melo | 7a997fe | 2012-05-12 13:15:34 -0300 | [diff] [blame] | 1054 | return -1; |
| 1055 | } |
| 1056 | |
Jiri Olsa | ea07c5a | 2017-10-11 17:01:29 +0200 | [diff] [blame] | 1057 | struct annotate_args { |
| 1058 | size_t privsize; |
Jiri Olsa | 24fe7b8 | 2017-10-11 17:01:30 +0200 | [diff] [blame] | 1059 | struct arch *arch; |
Arnaldo Carvalho de Melo | 85a84e4 | 2018-03-20 16:19:08 -0300 | [diff] [blame] | 1060 | struct map_symbol ms; |
Jiri Olsa | d03a686 | 2017-10-11 17:01:33 +0200 | [diff] [blame] | 1061 | struct perf_evsel *evsel; |
Jiri Olsa | 4748834 | 2017-10-11 17:01:32 +0200 | [diff] [blame] | 1062 | s64 offset; |
| 1063 | char *line; |
| 1064 | int line_nr; |
Jiri Olsa | ea07c5a | 2017-10-11 17:01:29 +0200 | [diff] [blame] | 1065 | }; |
| 1066 | |
Jiri Olsa | c835e19 | 2017-10-11 17:01:37 +0200 | [diff] [blame] | 1067 | static void annotation_line__delete(struct annotation_line *al) |
| 1068 | { |
| 1069 | void *ptr = (void *) al - al->privsize; |
| 1070 | |
Jiri Olsa | 8b4c74d | 2017-10-11 17:01:41 +0200 | [diff] [blame] | 1071 | free_srcline(al->path); |
Jiri Olsa | c835e19 | 2017-10-11 17:01:37 +0200 | [diff] [blame] | 1072 | zfree(&al->line); |
| 1073 | free(ptr); |
| 1074 | } |
| 1075 | |
| 1076 | /* |
| 1077 | * Allocating the annotation line data with following |
| 1078 | * structure: |
| 1079 | * |
| 1080 | * -------------------------------------- |
| 1081 | * private space | struct annotation_line |
| 1082 | * -------------------------------------- |
| 1083 | * |
| 1084 | * Size of the private space is stored in 'struct annotation_line'. |
| 1085 | * |
| 1086 | */ |
| 1087 | static struct annotation_line * |
| 1088 | annotation_line__new(struct annotate_args *args, size_t privsize) |
| 1089 | { |
| 1090 | struct annotation_line *al; |
Jiri Olsa | 7e30455 | 2017-10-11 17:01:39 +0200 | [diff] [blame] | 1091 | struct perf_evsel *evsel = args->evsel; |
Jiri Olsa | c835e19 | 2017-10-11 17:01:37 +0200 | [diff] [blame] | 1092 | size_t size = privsize + sizeof(*al); |
Jiri Olsa | 7e30455 | 2017-10-11 17:01:39 +0200 | [diff] [blame] | 1093 | int nr = 1; |
| 1094 | |
| 1095 | if (perf_evsel__is_group_event(evsel)) |
| 1096 | nr = evsel->nr_members; |
| 1097 | |
| 1098 | size += sizeof(al->samples[0]) * nr; |
Jiri Olsa | c835e19 | 2017-10-11 17:01:37 +0200 | [diff] [blame] | 1099 | |
| 1100 | al = zalloc(size); |
| 1101 | if (al) { |
| 1102 | al = (void *) al + privsize; |
| 1103 | al->privsize = privsize; |
| 1104 | al->offset = args->offset; |
| 1105 | al->line = strdup(args->line); |
| 1106 | al->line_nr = args->line_nr; |
Jiri Olsa | 7e30455 | 2017-10-11 17:01:39 +0200 | [diff] [blame] | 1107 | al->samples_nr = nr; |
Jiri Olsa | c835e19 | 2017-10-11 17:01:37 +0200 | [diff] [blame] | 1108 | } |
| 1109 | |
| 1110 | return al; |
| 1111 | } |
| 1112 | |
| 1113 | /* |
| 1114 | * Allocating the disasm annotation line data with |
| 1115 | * following structure: |
| 1116 | * |
| 1117 | * ------------------------------------------------------------ |
| 1118 | * privsize space | struct disasm_line | struct annotation_line |
| 1119 | * ------------------------------------------------------------ |
| 1120 | * |
| 1121 | * We have 'struct annotation_line' member as last member |
| 1122 | * of 'struct disasm_line' to have an easy access. |
| 1123 | * |
| 1124 | */ |
Jiri Olsa | 4748834 | 2017-10-11 17:01:32 +0200 | [diff] [blame] | 1125 | 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] | 1126 | { |
Jiri Olsa | c835e19 | 2017-10-11 17:01:37 +0200 | [diff] [blame] | 1127 | struct disasm_line *dl = NULL; |
| 1128 | struct annotation_line *al; |
| 1129 | size_t privsize = args->privsize + offsetof(struct disasm_line, al); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1130 | |
Jiri Olsa | c835e19 | 2017-10-11 17:01:37 +0200 | [diff] [blame] | 1131 | al = annotation_line__new(args, privsize); |
| 1132 | if (al != NULL) { |
| 1133 | dl = disasm_line(al); |
Jiri Olsa | d5490b9 | 2017-10-11 17:01:26 +0200 | [diff] [blame] | 1134 | |
| 1135 | if (dl->al.line == NULL) |
Arnaldo Carvalho de Melo | 058b4cc | 2012-04-02 12:59:01 -0300 | [diff] [blame] | 1136 | goto out_delete; |
Arnaldo Carvalho de Melo | 5145418 | 2012-04-15 15:52:18 -0300 | [diff] [blame] | 1137 | |
Jiri Olsa | 4748834 | 2017-10-11 17:01:32 +0200 | [diff] [blame] | 1138 | if (args->offset != -1) { |
Jiri Olsa | d5490b9 | 2017-10-11 17:01:26 +0200 | [diff] [blame] | 1139 | 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] | 1140 | goto out_free_line; |
| 1141 | |
Arnaldo Carvalho de Melo | 85a84e4 | 2018-03-20 16:19:08 -0300 | [diff] [blame] | 1142 | disasm_line__init_ins(dl, args->arch, &args->ms); |
Arnaldo Carvalho de Melo | 5145418 | 2012-04-15 15:52:18 -0300 | [diff] [blame] | 1143 | } |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1144 | } |
| 1145 | |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 1146 | return dl; |
Arnaldo Carvalho de Melo | 5145418 | 2012-04-15 15:52:18 -0300 | [diff] [blame] | 1147 | |
| 1148 | out_free_line: |
Jiri Olsa | d5490b9 | 2017-10-11 17:01:26 +0200 | [diff] [blame] | 1149 | zfree(&dl->al.line); |
Arnaldo Carvalho de Melo | 058b4cc | 2012-04-02 12:59:01 -0300 | [diff] [blame] | 1150 | out_delete: |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 1151 | free(dl); |
Arnaldo Carvalho de Melo | 058b4cc | 2012-04-02 12:59:01 -0300 | [diff] [blame] | 1152 | return NULL; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1153 | } |
| 1154 | |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 1155 | void disasm_line__free(struct disasm_line *dl) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1156 | { |
Arnaldo Carvalho de Melo | 75b4920 | 2016-11-24 11:16:06 -0300 | [diff] [blame] | 1157 | if (dl->ins.ops && dl->ins.ops->free) |
| 1158 | dl->ins.ops->free(&dl->ops); |
Arnaldo Carvalho de Melo | c46219ac | 2012-05-12 13:26:20 -0300 | [diff] [blame] | 1159 | else |
| 1160 | ins__delete(&dl->ops); |
Arnaldo Carvalho de Melo | 75b4920 | 2016-11-24 11:16:06 -0300 | [diff] [blame] | 1161 | free((void *)dl->ins.name); |
| 1162 | dl->ins.name = NULL; |
Jiri Olsa | c835e19 | 2017-10-11 17:01:37 +0200 | [diff] [blame] | 1163 | annotation_line__delete(&dl->al); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1164 | } |
| 1165 | |
Arnaldo Carvalho de Melo | 5417072 | 2012-05-07 18:54:16 -0300 | [diff] [blame] | 1166 | int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size, bool raw) |
| 1167 | { |
Arnaldo Carvalho de Melo | 75b4920 | 2016-11-24 11:16:06 -0300 | [diff] [blame] | 1168 | if (raw || !dl->ins.ops) |
Ravi Bangoria | 648388a | 2017-11-14 08:55:40 +0530 | [diff] [blame] | 1169 | 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] | 1170 | |
Arnaldo Carvalho de Melo | 75b4920 | 2016-11-24 11:16:06 -0300 | [diff] [blame] | 1171 | return ins__scnprintf(&dl->ins, bf, size, &dl->ops); |
Arnaldo Carvalho de Melo | 5417072 | 2012-05-07 18:54:16 -0300 | [diff] [blame] | 1172 | } |
| 1173 | |
Jiri Olsa | 82b9d7f | 2017-10-11 17:01:35 +0200 | [diff] [blame] | 1174 | 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] | 1175 | { |
Jiri Olsa | 82b9d7f | 2017-10-11 17:01:35 +0200 | [diff] [blame] | 1176 | list_add_tail(&al->node, head); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1177 | } |
| 1178 | |
Jiri Olsa | c4c7243 | 2017-10-11 17:01:34 +0200 | [diff] [blame] | 1179 | struct annotation_line * |
| 1180 | 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] | 1181 | { |
Jiri Olsa | c4c7243 | 2017-10-11 17:01:34 +0200 | [diff] [blame] | 1182 | list_for_each_entry_continue(pos, head, node) |
| 1183 | if (pos->offset >= 0) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1184 | return pos; |
| 1185 | |
| 1186 | return NULL; |
| 1187 | } |
| 1188 | |
Peter Zijlstra | 70fbe05 | 2016-09-05 16:08:12 -0300 | [diff] [blame] | 1189 | static const char *annotate__address_color(struct block_range *br) |
| 1190 | { |
| 1191 | double cov = block_range__coverage(br); |
| 1192 | |
| 1193 | if (cov >= 0) { |
| 1194 | /* mark red for >75% coverage */ |
| 1195 | if (cov > 0.75) |
| 1196 | return PERF_COLOR_RED; |
| 1197 | |
| 1198 | /* mark dull for <1% coverage */ |
| 1199 | if (cov < 0.01) |
| 1200 | return PERF_COLOR_NORMAL; |
| 1201 | } |
| 1202 | |
| 1203 | return PERF_COLOR_MAGENTA; |
| 1204 | } |
| 1205 | |
| 1206 | static const char *annotate__asm_color(struct block_range *br) |
| 1207 | { |
| 1208 | double cov = block_range__coverage(br); |
| 1209 | |
| 1210 | if (cov >= 0) { |
| 1211 | /* mark dull for <1% coverage */ |
| 1212 | if (cov < 0.01) |
| 1213 | return PERF_COLOR_NORMAL; |
| 1214 | } |
| 1215 | |
| 1216 | return PERF_COLOR_BLUE; |
| 1217 | } |
| 1218 | |
| 1219 | static void annotate__branch_printf(struct block_range *br, u64 addr) |
| 1220 | { |
| 1221 | bool emit_comment = true; |
| 1222 | |
| 1223 | if (!br) |
| 1224 | return; |
| 1225 | |
| 1226 | #if 1 |
| 1227 | if (br->is_target && br->start == addr) { |
| 1228 | struct block_range *branch = br; |
| 1229 | double p; |
| 1230 | |
| 1231 | /* |
| 1232 | * Find matching branch to our target. |
| 1233 | */ |
| 1234 | while (!branch->is_branch) |
| 1235 | branch = block_range__next(branch); |
| 1236 | |
| 1237 | p = 100 *(double)br->entry / branch->coverage; |
| 1238 | |
| 1239 | if (p > 0.1) { |
| 1240 | if (emit_comment) { |
| 1241 | emit_comment = false; |
| 1242 | printf("\t#"); |
| 1243 | } |
| 1244 | |
| 1245 | /* |
| 1246 | * The percentage of coverage joined at this target in relation |
| 1247 | * to the next branch. |
| 1248 | */ |
| 1249 | printf(" +%.2f%%", p); |
| 1250 | } |
| 1251 | } |
| 1252 | #endif |
| 1253 | if (br->is_branch && br->end == addr) { |
| 1254 | double p = 100*(double)br->taken / br->coverage; |
| 1255 | |
| 1256 | if (p > 0.1) { |
| 1257 | if (emit_comment) { |
| 1258 | emit_comment = false; |
| 1259 | printf("\t#"); |
| 1260 | } |
| 1261 | |
| 1262 | /* |
| 1263 | * The percentage of coverage leaving at this branch, and |
| 1264 | * its prediction ratio. |
| 1265 | */ |
| 1266 | printf(" -%.2f%% (p:%.2f%%)", p, 100*(double)br->pred / br->taken); |
| 1267 | } |
| 1268 | } |
| 1269 | } |
| 1270 | |
Jiri Olsa | f48e7c4 | 2017-10-11 17:01:58 +0200 | [diff] [blame] | 1271 | 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] | 1272 | { |
Jiri Olsa | 29971f9 | 2017-10-11 17:01:47 +0200 | [diff] [blame] | 1273 | s64 offset = dl->al.offset; |
| 1274 | const u64 addr = start + offset; |
| 1275 | struct block_range *br; |
| 1276 | |
| 1277 | br = block_range__find(addr); |
Jiri Olsa | f48e7c4 | 2017-10-11 17:01:58 +0200 | [diff] [blame] | 1278 | color_fprintf(stdout, annotate__address_color(br), " %*" PRIx64 ":", addr_fmt_width, addr); |
Jiri Olsa | 29971f9 | 2017-10-11 17:01:47 +0200 | [diff] [blame] | 1279 | color_fprintf(stdout, annotate__asm_color(br), "%s", dl->al.line); |
| 1280 | annotate__branch_printf(br, addr); |
| 1281 | return 0; |
| 1282 | } |
| 1283 | |
| 1284 | static int |
| 1285 | annotation_line__print(struct annotation_line *al, struct symbol *sym, u64 start, |
| 1286 | struct perf_evsel *evsel, u64 len, int min_pcnt, int printed, |
Jiri Olsa | f48e7c4 | 2017-10-11 17:01:58 +0200 | [diff] [blame] | 1287 | int max_lines, struct annotation_line *queue, int addr_fmt_width) |
Jiri Olsa | 29971f9 | 2017-10-11 17:01:47 +0200 | [diff] [blame] | 1288 | { |
| 1289 | 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] | 1290 | static const char *prev_line; |
| 1291 | static const char *prev_color; |
| 1292 | |
Jiri Olsa | 29971f9 | 2017-10-11 17:01:47 +0200 | [diff] [blame] | 1293 | if (al->offset != -1) { |
Jiri Olsa | f681d59 | 2017-10-11 17:01:42 +0200 | [diff] [blame] | 1294 | double max_percent = 0.0; |
Namhyung Kim | b1dd443 | 2013-03-05 14:53:25 +0900 | [diff] [blame] | 1295 | int i, nr_percent = 1; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1296 | const char *color; |
| 1297 | struct annotation *notes = symbol__annotation(sym); |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 1298 | |
Jiri Olsa | 29971f9 | 2017-10-11 17:01:47 +0200 | [diff] [blame] | 1299 | for (i = 0; i < al->samples_nr; i++) { |
| 1300 | struct annotation_data *sample = &al->samples[i]; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1301 | |
Jiri Olsa | f681d59 | 2017-10-11 17:01:42 +0200 | [diff] [blame] | 1302 | if (sample->percent > max_percent) |
| 1303 | max_percent = sample->percent; |
Namhyung Kim | b1dd443 | 2013-03-05 14:53:25 +0900 | [diff] [blame] | 1304 | } |
| 1305 | |
Jin Yao | 04d2600 | 2018-05-09 23:57:15 +0800 | [diff] [blame] | 1306 | if (al->samples_nr > nr_percent) |
| 1307 | nr_percent = al->samples_nr; |
| 1308 | |
Namhyung Kim | b1dd443 | 2013-03-05 14:53:25 +0900 | [diff] [blame] | 1309 | if (max_percent < min_pcnt) |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 1310 | return -1; |
| 1311 | |
Arnaldo Carvalho de Melo | e3087b8 | 2011-02-08 15:01:39 -0200 | [diff] [blame] | 1312 | if (max_lines && printed >= max_lines) |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 1313 | return 1; |
Arnaldo Carvalho de Melo | d040bd3 | 2011-02-05 15:37:31 -0200 | [diff] [blame] | 1314 | |
Arnaldo Carvalho de Melo | d5e3d74 | 2011-02-08 15:29:25 -0200 | [diff] [blame] | 1315 | if (queue != NULL) { |
Jiri Olsa | 29971f9 | 2017-10-11 17:01:47 +0200 | [diff] [blame] | 1316 | list_for_each_entry_from(queue, ¬es->src->source, node) { |
| 1317 | if (queue == al) |
Arnaldo Carvalho de Melo | d5e3d74 | 2011-02-08 15:29:25 -0200 | [diff] [blame] | 1318 | break; |
Jiri Olsa | 29971f9 | 2017-10-11 17:01:47 +0200 | [diff] [blame] | 1319 | annotation_line__print(queue, sym, start, evsel, len, |
Jiri Olsa | f48e7c4 | 2017-10-11 17:01:58 +0200 | [diff] [blame] | 1320 | 0, 0, 1, NULL, addr_fmt_width); |
Arnaldo Carvalho de Melo | d5e3d74 | 2011-02-08 15:29:25 -0200 | [diff] [blame] | 1321 | } |
| 1322 | } |
| 1323 | |
Namhyung Kim | b1dd443 | 2013-03-05 14:53:25 +0900 | [diff] [blame] | 1324 | color = get_percent_color(max_percent); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1325 | |
| 1326 | /* |
| 1327 | * Also color the filename and line if needed, with |
| 1328 | * the same color than the percentage. Don't print it |
| 1329 | * twice for close colored addr with the same filename:line |
| 1330 | */ |
Jiri Olsa | 29971f9 | 2017-10-11 17:01:47 +0200 | [diff] [blame] | 1331 | if (al->path) { |
| 1332 | if (!prev_line || strcmp(prev_line, al->path) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1333 | || color != prev_color) { |
Jiri Olsa | 29971f9 | 2017-10-11 17:01:47 +0200 | [diff] [blame] | 1334 | color_fprintf(stdout, color, " %s", al->path); |
| 1335 | prev_line = al->path; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1336 | prev_color = color; |
| 1337 | } |
| 1338 | } |
| 1339 | |
Namhyung Kim | b1dd443 | 2013-03-05 14:53:25 +0900 | [diff] [blame] | 1340 | for (i = 0; i < nr_percent; i++) { |
Jiri Olsa | 29971f9 | 2017-10-11 17:01:47 +0200 | [diff] [blame] | 1341 | struct annotation_data *sample = &al->samples[i]; |
Jiri Olsa | f681d59 | 2017-10-11 17:01:42 +0200 | [diff] [blame] | 1342 | |
| 1343 | color = get_percent_color(sample->percent); |
Martin Liška | 0c4a5bc | 2015-06-19 16:10:43 -0300 | [diff] [blame] | 1344 | |
| 1345 | if (symbol_conf.show_total_period) |
Arnaldo Carvalho de Melo | ce9ee4a | 2017-07-26 17:16:46 -0300 | [diff] [blame] | 1346 | color_fprintf(stdout, color, " %11" PRIu64, |
Jiri Olsa | f681d59 | 2017-10-11 17:01:42 +0200 | [diff] [blame] | 1347 | sample->he.period); |
Taeung Song | 1ac3937 | 2017-08-18 17:46:48 +0900 | [diff] [blame] | 1348 | else if (symbol_conf.show_nr_samples) |
| 1349 | color_fprintf(stdout, color, " %7" PRIu64, |
Jiri Olsa | f681d59 | 2017-10-11 17:01:42 +0200 | [diff] [blame] | 1350 | sample->he.nr_samples); |
Martin Liška | 0c4a5bc | 2015-06-19 16:10:43 -0300 | [diff] [blame] | 1351 | else |
Jiri Olsa | f681d59 | 2017-10-11 17:01:42 +0200 | [diff] [blame] | 1352 | color_fprintf(stdout, color, " %7.2f", sample->percent); |
Namhyung Kim | b1dd443 | 2013-03-05 14:53:25 +0900 | [diff] [blame] | 1353 | } |
| 1354 | |
Jiri Olsa | f48e7c4 | 2017-10-11 17:01:58 +0200 | [diff] [blame] | 1355 | printf(" : "); |
Peter Zijlstra | 70fbe05 | 2016-09-05 16:08:12 -0300 | [diff] [blame] | 1356 | |
Jiri Olsa | f48e7c4 | 2017-10-11 17:01:58 +0200 | [diff] [blame] | 1357 | disasm_line__print(dl, start, addr_fmt_width); |
Peter Zijlstra | 70fbe05 | 2016-09-05 16:08:12 -0300 | [diff] [blame] | 1358 | printf("\n"); |
Arnaldo Carvalho de Melo | e3087b8 | 2011-02-08 15:01:39 -0200 | [diff] [blame] | 1359 | } else if (max_lines && printed >= max_lines) |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 1360 | return 1; |
| 1361 | else { |
Arnaldo Carvalho de Melo | ce9ee4a | 2017-07-26 17:16:46 -0300 | [diff] [blame] | 1362 | int width = symbol_conf.show_total_period ? 12 : 8; |
Namhyung Kim | b1dd443 | 2013-03-05 14:53:25 +0900 | [diff] [blame] | 1363 | |
Arnaldo Carvalho de Melo | d5e3d74 | 2011-02-08 15:29:25 -0200 | [diff] [blame] | 1364 | if (queue) |
| 1365 | return -1; |
| 1366 | |
Namhyung Kim | 759ff49 | 2013-03-05 14:53:26 +0900 | [diff] [blame] | 1367 | if (perf_evsel__is_group_event(evsel)) |
Namhyung Kim | b1dd443 | 2013-03-05 14:53:25 +0900 | [diff] [blame] | 1368 | width *= evsel->nr_members; |
| 1369 | |
Jiri Olsa | 29971f9 | 2017-10-11 17:01:47 +0200 | [diff] [blame] | 1370 | if (!*al->line) |
Namhyung Kim | b1dd443 | 2013-03-05 14:53:25 +0900 | [diff] [blame] | 1371 | printf(" %*s:\n", width, " "); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1372 | else |
Jiri Olsa | f48e7c4 | 2017-10-11 17:01:58 +0200 | [diff] [blame] | 1373 | 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] | 1374 | } |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 1375 | |
| 1376 | return 0; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1377 | } |
| 1378 | |
Namhyung Kim | 3aec150 | 2013-03-05 14:53:22 +0900 | [diff] [blame] | 1379 | /* |
| 1380 | * symbol__parse_objdump_line() parses objdump output (with -d --no-show-raw) |
| 1381 | * which looks like following |
| 1382 | * |
| 1383 | * 0000000000415500 <_init>: |
| 1384 | * 415500: sub $0x8,%rsp |
| 1385 | * 415504: mov 0x2f5ad5(%rip),%rax # 70afe0 <_DYNAMIC+0x2f8> |
| 1386 | * 41550b: test %rax,%rax |
| 1387 | * 41550e: je 415515 <_init+0x15> |
| 1388 | * 415510: callq 416e70 <__gmon_start__@plt> |
| 1389 | * 415515: add $0x8,%rsp |
| 1390 | * 415519: retq |
| 1391 | * |
| 1392 | * it will be parsed and saved into struct disasm_line as |
| 1393 | * <offset> <name> <ops.raw> |
| 1394 | * |
| 1395 | * The offset will be a relative offset from the start of the symbol and -1 |
| 1396 | * means that it's not a disassembly line so should be treated differently. |
| 1397 | * The ops.raw part will be parsed further according to type of the instruction. |
| 1398 | */ |
Jiri Olsa | 1a04db7 | 2017-10-11 17:01:31 +0200 | [diff] [blame] | 1399 | static int symbol__parse_objdump_line(struct symbol *sym, FILE *file, |
Jiri Olsa | ea07c5a | 2017-10-11 17:01:29 +0200 | [diff] [blame] | 1400 | struct annotate_args *args, |
Andi Kleen | e592488 | 2014-11-12 18:05:26 -0800 | [diff] [blame] | 1401 | int *line_nr) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1402 | { |
Arnaldo Carvalho de Melo | 85a84e4 | 2018-03-20 16:19:08 -0300 | [diff] [blame] | 1403 | struct map *map = args->ms.map; |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 1404 | struct annotation *notes = symbol__annotation(sym); |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 1405 | struct disasm_line *dl; |
Taeung Song | 4597cf0 | 2017-04-08 09:52:24 +0900 | [diff] [blame] | 1406 | char *line = NULL, *parsed_line, *tmp, *tmp2; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1407 | size_t line_len; |
| 1408 | s64 line_ip, offset = -1; |
Andi Kleen | e592488 | 2014-11-12 18:05:26 -0800 | [diff] [blame] | 1409 | regmatch_t match[2]; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1410 | |
| 1411 | if (getline(&line, &line_len, file) < 0) |
| 1412 | return -1; |
| 1413 | |
| 1414 | if (!line) |
| 1415 | return -1; |
| 1416 | |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1417 | line_ip = -1; |
Taeung Song | 4597cf0 | 2017-04-08 09:52:24 +0900 | [diff] [blame] | 1418 | parsed_line = rtrim(line); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1419 | |
Andi Kleen | e592488 | 2014-11-12 18:05:26 -0800 | [diff] [blame] | 1420 | /* /filename:linenr ? Save line number and ignore. */ |
Taeung Song | 986a5bc | 2017-04-08 09:52:25 +0900 | [diff] [blame] | 1421 | if (regexec(&file_lineno, parsed_line, 2, match, 0) == 0) { |
| 1422 | *line_nr = atoi(parsed_line + match[1].rm_so); |
Andi Kleen | e592488 | 2014-11-12 18:05:26 -0800 | [diff] [blame] | 1423 | return 0; |
| 1424 | } |
| 1425 | |
Taeung Song | 4597cf0 | 2017-04-08 09:52:24 +0900 | [diff] [blame] | 1426 | tmp = ltrim(parsed_line); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1427 | if (*tmp) { |
| 1428 | /* |
| 1429 | * Parse hexa addresses followed by ':' |
| 1430 | */ |
| 1431 | line_ip = strtoull(tmp, &tmp2, 16); |
| 1432 | if (*tmp2 != ':' || tmp == tmp2 || tmp2[1] == '\0') |
| 1433 | line_ip = -1; |
| 1434 | } |
| 1435 | |
| 1436 | if (line_ip != -1) { |
| 1437 | u64 start = map__rip_2objdump(map, sym->start), |
| 1438 | end = map__rip_2objdump(map, sym->end); |
| 1439 | |
| 1440 | offset = line_ip - start; |
Arnaldo Carvalho de Melo | 2c241bd | 2014-10-14 17:19:44 -0300 | [diff] [blame] | 1441 | if ((u64)line_ip < start || (u64)line_ip >= end) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1442 | offset = -1; |
Arnaldo Carvalho de Melo | 058b4cc | 2012-04-02 12:59:01 -0300 | [diff] [blame] | 1443 | else |
| 1444 | parsed_line = tmp2 + 1; |
Namhyung Kim | a31b7cc | 2012-04-11 17:04:59 -0300 | [diff] [blame] | 1445 | } |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1446 | |
Jiri Olsa | 4748834 | 2017-10-11 17:01:32 +0200 | [diff] [blame] | 1447 | args->offset = offset; |
| 1448 | args->line = parsed_line; |
| 1449 | args->line_nr = *line_nr; |
Arnaldo Carvalho de Melo | 85a84e4 | 2018-03-20 16:19:08 -0300 | [diff] [blame] | 1450 | args->ms.sym = sym; |
Jiri Olsa | 4748834 | 2017-10-11 17:01:32 +0200 | [diff] [blame] | 1451 | |
| 1452 | dl = disasm_line__new(args); |
Arnaldo Carvalho de Melo | 058b4cc | 2012-04-02 12:59:01 -0300 | [diff] [blame] | 1453 | free(line); |
Andi Kleen | e592488 | 2014-11-12 18:05:26 -0800 | [diff] [blame] | 1454 | (*line_nr)++; |
Arnaldo Carvalho de Melo | 058b4cc | 2012-04-02 12:59:01 -0300 | [diff] [blame] | 1455 | |
Arnaldo Carvalho de Melo | 29ed6e7 | 2012-04-15 15:24:39 -0300 | [diff] [blame] | 1456 | if (dl == NULL) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1457 | return -1; |
Arnaldo Carvalho de Melo | 058b4cc | 2012-04-02 12:59:01 -0300 | [diff] [blame] | 1458 | |
Arnaldo Carvalho de Melo | 2eff061 | 2018-03-23 10:12:33 -0300 | [diff] [blame] | 1459 | if (!disasm_line__has_local_offset(dl)) { |
Adrian Hunter | bbb7f84 | 2013-08-07 14:38:54 +0300 | [diff] [blame] | 1460 | dl->ops.target.offset = dl->ops.target.addr - |
| 1461 | map__rip_2objdump(map, sym->start); |
Ravi Bangoria | e216874 | 2016-12-05 21:26:47 +0530 | [diff] [blame] | 1462 | dl->ops.target.offset_avail = true; |
| 1463 | } |
Adrian Hunter | bbb7f84 | 2013-08-07 14:38:54 +0300 | [diff] [blame] | 1464 | |
Arnaldo Carvalho de Melo | 696703a | 2018-03-02 11:59:36 -0300 | [diff] [blame] | 1465 | /* kcore has no symbols, so add the call target symbol */ |
| 1466 | 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] | 1467 | struct addr_map_symbol target = { |
| 1468 | .map = map, |
| 1469 | .addr = dl->ops.target.addr, |
| 1470 | }; |
Adrian Hunter | b178170 | 2013-08-07 14:38:57 +0300 | [diff] [blame] | 1471 | |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame] | 1472 | if (!map_groups__find_ams(&target) && |
Adrian Hunter | 6e427ab | 2013-10-14 13:43:40 +0300 | [diff] [blame] | 1473 | target.sym->start == target.al_addr) |
Arnaldo Carvalho de Melo | 696703a | 2018-03-02 11:59:36 -0300 | [diff] [blame] | 1474 | dl->ops.target.sym = target.sym; |
Adrian Hunter | b178170 | 2013-08-07 14:38:57 +0300 | [diff] [blame] | 1475 | } |
| 1476 | |
Jiri Olsa | 82b9d7f | 2017-10-11 17:01:35 +0200 | [diff] [blame] | 1477 | annotation_line__add(&dl->al, ¬es->src->source); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1478 | |
| 1479 | return 0; |
| 1480 | } |
| 1481 | |
Andi Kleen | e592488 | 2014-11-12 18:05:26 -0800 | [diff] [blame] | 1482 | static __attribute__((constructor)) void symbol__init_regexpr(void) |
| 1483 | { |
| 1484 | regcomp(&file_lineno, "^/[^:]+:([0-9]+)", REG_EXTENDED); |
| 1485 | } |
| 1486 | |
Adrian Hunter | 484a5e7 | 2013-08-07 14:38:56 +0300 | [diff] [blame] | 1487 | static void delete_last_nop(struct symbol *sym) |
| 1488 | { |
| 1489 | struct annotation *notes = symbol__annotation(sym); |
| 1490 | struct list_head *list = ¬es->src->source; |
| 1491 | struct disasm_line *dl; |
| 1492 | |
| 1493 | while (!list_empty(list)) { |
Jiri Olsa | a17c4ca | 2017-10-11 17:01:25 +0200 | [diff] [blame] | 1494 | dl = list_entry(list->prev, struct disasm_line, al.node); |
Adrian Hunter | 484a5e7 | 2013-08-07 14:38:56 +0300 | [diff] [blame] | 1495 | |
Arnaldo Carvalho de Melo | 75b4920 | 2016-11-24 11:16:06 -0300 | [diff] [blame] | 1496 | if (dl->ins.ops) { |
| 1497 | if (dl->ins.ops != &nop_ops) |
Adrian Hunter | 484a5e7 | 2013-08-07 14:38:56 +0300 | [diff] [blame] | 1498 | return; |
| 1499 | } else { |
Jiri Olsa | d5490b9 | 2017-10-11 17:01:26 +0200 | [diff] [blame] | 1500 | if (!strstr(dl->al.line, " nop ") && |
| 1501 | !strstr(dl->al.line, " nopl ") && |
| 1502 | !strstr(dl->al.line, " nopw ")) |
Adrian Hunter | 484a5e7 | 2013-08-07 14:38:56 +0300 | [diff] [blame] | 1503 | return; |
| 1504 | } |
| 1505 | |
Jiri Olsa | a17c4ca | 2017-10-11 17:01:25 +0200 | [diff] [blame] | 1506 | list_del(&dl->al.node); |
Adrian Hunter | 484a5e7 | 2013-08-07 14:38:56 +0300 | [diff] [blame] | 1507 | disasm_line__free(dl); |
| 1508 | } |
| 1509 | } |
| 1510 | |
Arnaldo Carvalho de Melo | ee51d85 | 2016-07-29 16:27:18 -0300 | [diff] [blame] | 1511 | int symbol__strerror_disassemble(struct symbol *sym __maybe_unused, struct map *map, |
| 1512 | int errnum, char *buf, size_t buflen) |
| 1513 | { |
| 1514 | struct dso *dso = map->dso; |
| 1515 | |
| 1516 | BUG_ON(buflen == 0); |
| 1517 | |
| 1518 | if (errnum >= 0) { |
| 1519 | str_error_r(errnum, buf, buflen); |
| 1520 | return 0; |
| 1521 | } |
| 1522 | |
| 1523 | switch (errnum) { |
| 1524 | case SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX: { |
| 1525 | char bf[SBUILD_ID_SIZE + 15] = " with build id "; |
| 1526 | char *build_id_msg = NULL; |
| 1527 | |
| 1528 | if (dso->has_build_id) { |
| 1529 | build_id__sprintf(dso->build_id, |
| 1530 | sizeof(dso->build_id), bf + 15); |
| 1531 | build_id_msg = bf; |
| 1532 | } |
| 1533 | scnprintf(buf, buflen, |
| 1534 | "No vmlinux file%s\nwas found in the path.\n\n" |
| 1535 | "Note that annotation using /proc/kcore requires CAP_SYS_RAWIO capability.\n\n" |
| 1536 | "Please use:\n\n" |
| 1537 | " perf buildid-cache -vu vmlinux\n\n" |
| 1538 | "or:\n\n" |
| 1539 | " --vmlinux vmlinux\n", build_id_msg ?: ""); |
| 1540 | } |
| 1541 | break; |
| 1542 | default: |
| 1543 | scnprintf(buf, buflen, "Internal error: Invalid %d error code\n", errnum); |
| 1544 | break; |
| 1545 | } |
| 1546 | |
| 1547 | return 0; |
| 1548 | } |
| 1549 | |
Arnaldo Carvalho de Melo | 05ed3ac | 2016-08-09 15:32:53 -0300 | [diff] [blame] | 1550 | static int dso__disassemble_filename(struct dso *dso, char *filename, size_t filename_size) |
| 1551 | { |
| 1552 | char linkname[PATH_MAX]; |
| 1553 | char *build_id_filename; |
Taeung Song | 6ebd254 | 2017-03-27 16:10:36 +0900 | [diff] [blame] | 1554 | char *build_id_path = NULL; |
Namhyung Kim | 3619ef7 | 2017-06-08 16:31:01 +0900 | [diff] [blame] | 1555 | char *pos; |
Arnaldo Carvalho de Melo | 05ed3ac | 2016-08-09 15:32:53 -0300 | [diff] [blame] | 1556 | |
| 1557 | if (dso->symtab_type == DSO_BINARY_TYPE__KALLSYMS && |
| 1558 | !dso__is_kcore(dso)) |
| 1559 | return SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX; |
| 1560 | |
Krister Johansen | d239699 | 2017-07-05 18:48:13 -0700 | [diff] [blame] | 1561 | 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] | 1562 | if (build_id_filename) { |
| 1563 | __symbol__join_symfs(filename, filename_size, build_id_filename); |
| 1564 | free(build_id_filename); |
| 1565 | } else { |
| 1566 | if (dso->has_build_id) |
| 1567 | return ENOMEM; |
| 1568 | goto fallback; |
| 1569 | } |
| 1570 | |
Taeung Song | 6ebd254 | 2017-03-27 16:10:36 +0900 | [diff] [blame] | 1571 | build_id_path = strdup(filename); |
| 1572 | if (!build_id_path) |
| 1573 | return -1; |
| 1574 | |
Namhyung Kim | 3619ef7 | 2017-06-08 16:31:01 +0900 | [diff] [blame] | 1575 | /* |
| 1576 | * old style build-id cache has name of XX/XXXXXXX.. while |
| 1577 | * new style has XX/XXXXXXX../{elf,kallsyms,vdso}. |
| 1578 | * extract the build-id part of dirname in the new style only. |
| 1579 | */ |
| 1580 | pos = strrchr(build_id_path, '/'); |
| 1581 | if (pos && strlen(pos) < SBUILD_ID_SIZE - 2) |
| 1582 | dirname(build_id_path); |
Taeung Song | 6ebd254 | 2017-03-27 16:10:36 +0900 | [diff] [blame] | 1583 | |
Arnaldo Carvalho de Melo | 05ed3ac | 2016-08-09 15:32:53 -0300 | [diff] [blame] | 1584 | if (dso__is_kcore(dso) || |
Taeung Song | 6ebd254 | 2017-03-27 16:10:36 +0900 | [diff] [blame] | 1585 | readlink(build_id_path, linkname, sizeof(linkname)) < 0 || |
Arnaldo Carvalho de Melo | 05ed3ac | 2016-08-09 15:32:53 -0300 | [diff] [blame] | 1586 | strstr(linkname, DSO__NAME_KALLSYMS) || |
| 1587 | access(filename, R_OK)) { |
| 1588 | fallback: |
| 1589 | /* |
| 1590 | * If we don't have build-ids or the build-id file isn't in the |
| 1591 | * cache, or is just a kallsyms file, well, lets hope that this |
| 1592 | * DSO is the same as when 'perf record' ran. |
| 1593 | */ |
| 1594 | __symbol__join_symfs(filename, filename_size, dso->long_name); |
| 1595 | } |
| 1596 | |
Taeung Song | 6ebd254 | 2017-03-27 16:10:36 +0900 | [diff] [blame] | 1597 | free(build_id_path); |
Arnaldo Carvalho de Melo | 05ed3ac | 2016-08-09 15:32:53 -0300 | [diff] [blame] | 1598 | return 0; |
| 1599 | } |
| 1600 | |
Jiri Olsa | 1a04db7 | 2017-10-11 17:01:31 +0200 | [diff] [blame] | 1601 | 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] | 1602 | { |
Arnaldo Carvalho de Melo | 85a84e4 | 2018-03-20 16:19:08 -0300 | [diff] [blame] | 1603 | struct map *map = args->ms.map; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1604 | struct dso *dso = map->dso; |
Arnaldo Carvalho de Melo | 6810158 | 2018-03-14 10:34:11 -0300 | [diff] [blame] | 1605 | char *command; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1606 | FILE *file; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1607 | char symfs_filename[PATH_MAX]; |
Adrian Hunter | afba19d | 2013-10-09 15:01:12 +0300 | [diff] [blame] | 1608 | struct kcore_extract kce; |
| 1609 | bool delete_extract = false; |
Arnaldo Carvalho de Melo | 9955d0b | 2016-06-15 15:48:08 -0300 | [diff] [blame] | 1610 | int stdout_fd[2]; |
Andi Kleen | e592488 | 2014-11-12 18:05:26 -0800 | [diff] [blame] | 1611 | int lineno = 0; |
Andi Kleen | 62ec9b3 | 2015-11-05 19:06:07 -0800 | [diff] [blame] | 1612 | int nline; |
Arnaldo Carvalho de Melo | 9955d0b | 2016-06-15 15:48:08 -0300 | [diff] [blame] | 1613 | pid_t pid; |
Arnaldo Carvalho de Melo | 05ed3ac | 2016-08-09 15:32:53 -0300 | [diff] [blame] | 1614 | 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] | 1615 | |
Arnaldo Carvalho de Melo | 05ed3ac | 2016-08-09 15:32:53 -0300 | [diff] [blame] | 1616 | if (err) |
| 1617 | return err; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1618 | |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1619 | 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] | 1620 | symfs_filename, sym->name, map->unmap_ip(map, sym->start), |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1621 | map->unmap_ip(map, sym->end)); |
| 1622 | |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1623 | pr_debug("annotating [%p] %30s : [%p] %30s\n", |
| 1624 | dso, dso->long_name, sym, sym->name); |
| 1625 | |
Adrian Hunter | afba19d | 2013-10-09 15:01:12 +0300 | [diff] [blame] | 1626 | if (dso__is_kcore(dso)) { |
| 1627 | kce.kcore_filename = symfs_filename; |
| 1628 | kce.addr = map__rip_2objdump(map, sym->start); |
| 1629 | kce.offs = sym->start; |
Arnaldo Carvalho de Melo | 2c241bd | 2014-10-14 17:19:44 -0300 | [diff] [blame] | 1630 | kce.len = sym->end - sym->start; |
Adrian Hunter | afba19d | 2013-10-09 15:01:12 +0300 | [diff] [blame] | 1631 | if (!kcore_extract__create(&kce)) { |
| 1632 | delete_extract = true; |
| 1633 | strlcpy(symfs_filename, kce.extract_filename, |
| 1634 | sizeof(symfs_filename)); |
Adrian Hunter | afba19d | 2013-10-09 15:01:12 +0300 | [diff] [blame] | 1635 | } |
Jiri Olsa | 2c7da8c | 2015-03-02 12:56:12 -0500 | [diff] [blame] | 1636 | } else if (dso__needs_decompress(dso)) { |
Namhyung Kim | 3c84fd5 | 2017-06-08 16:31:04 +0900 | [diff] [blame] | 1637 | char tmp[KMOD_DECOMP_LEN]; |
Jiri Olsa | 2c7da8c | 2015-03-02 12:56:12 -0500 | [diff] [blame] | 1638 | |
Namhyung Kim | 3c84fd5 | 2017-06-08 16:31:04 +0900 | [diff] [blame] | 1639 | if (dso__decompress_kmodule_path(dso, symfs_filename, |
| 1640 | tmp, sizeof(tmp)) < 0) |
Arnaldo Carvalho de Melo | 3caee09 | 2016-08-09 15:16:37 -0300 | [diff] [blame] | 1641 | goto out; |
Jiri Olsa | 2c7da8c | 2015-03-02 12:56:12 -0500 | [diff] [blame] | 1642 | |
| 1643 | strcpy(symfs_filename, tmp); |
Adrian Hunter | afba19d | 2013-10-09 15:01:12 +0300 | [diff] [blame] | 1644 | } |
| 1645 | |
Arnaldo Carvalho de Melo | 6810158 | 2018-03-14 10:34:11 -0300 | [diff] [blame] | 1646 | err = asprintf(&command, |
Maciek Borzecki | 7a4ec93 | 2012-09-04 12:32:30 +0200 | [diff] [blame] | 1647 | "%s %s%s --start-address=0x%016" PRIx64 |
Stephane Eranian | 3e6a2a7 | 2011-05-17 17:32:07 +0200 | [diff] [blame] | 1648 | " --stop-address=0x%016" PRIx64 |
Ravi Bangoria | 7b4500b | 2017-05-05 15:44:17 +0530 | [diff] [blame] | 1649 | " -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] | 1650 | objdump_path ? objdump_path : "objdump", |
Andi Kleen | f69b64f | 2011-09-15 14:31:41 -0700 | [diff] [blame] | 1651 | disassembler_style ? "-M " : "", |
| 1652 | disassembler_style ? disassembler_style : "", |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1653 | map__rip_2objdump(map, sym->start), |
Arnaldo Carvalho de Melo | 2c241bd | 2014-10-14 17:19:44 -0300 | [diff] [blame] | 1654 | map__rip_2objdump(map, sym->end), |
Stephane Eranian | 3e6a2a7 | 2011-05-17 17:32:07 +0200 | [diff] [blame] | 1655 | symbol_conf.annotate_asm_raw ? "" : "--no-show-raw", |
| 1656 | symbol_conf.annotate_src ? "-S" : "", |
Arnaldo Carvalho de Melo | 3caee09 | 2016-08-09 15:16:37 -0300 | [diff] [blame] | 1657 | symfs_filename, symfs_filename); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1658 | |
Arnaldo Carvalho de Melo | 6810158 | 2018-03-14 10:34:11 -0300 | [diff] [blame] | 1659 | if (err < 0) { |
| 1660 | pr_err("Failure allocating memory for the command to run\n"); |
| 1661 | goto out_remove_tmp; |
| 1662 | } |
| 1663 | |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1664 | pr_debug("Executing: %s\n", command); |
| 1665 | |
Arnaldo Carvalho de Melo | 9955d0b | 2016-06-15 15:48:08 -0300 | [diff] [blame] | 1666 | err = -1; |
| 1667 | if (pipe(stdout_fd) < 0) { |
| 1668 | pr_err("Failure creating the pipe to run %s\n", command); |
Arnaldo Carvalho de Melo | 6810158 | 2018-03-14 10:34:11 -0300 | [diff] [blame] | 1669 | goto out_free_command; |
Arnaldo Carvalho de Melo | 9955d0b | 2016-06-15 15:48:08 -0300 | [diff] [blame] | 1670 | } |
| 1671 | |
| 1672 | pid = fork(); |
| 1673 | if (pid < 0) { |
| 1674 | pr_err("Failure forking to run %s\n", command); |
| 1675 | goto out_close_stdout; |
| 1676 | } |
| 1677 | |
| 1678 | if (pid == 0) { |
| 1679 | close(stdout_fd[0]); |
| 1680 | dup2(stdout_fd[1], 1); |
| 1681 | close(stdout_fd[1]); |
| 1682 | execl("/bin/sh", "sh", "-c", command, NULL); |
| 1683 | perror(command); |
| 1684 | exit(-1); |
| 1685 | } |
| 1686 | |
| 1687 | close(stdout_fd[1]); |
| 1688 | |
| 1689 | file = fdopen(stdout_fd[0], "r"); |
Andi Kleen | 62ec9b3 | 2015-11-05 19:06:07 -0800 | [diff] [blame] | 1690 | if (!file) { |
Arnaldo Carvalho de Melo | 9955d0b | 2016-06-15 15:48:08 -0300 | [diff] [blame] | 1691 | pr_err("Failure creating FILE stream for %s\n", command); |
Andi Kleen | 62ec9b3 | 2015-11-05 19:06:07 -0800 | [diff] [blame] | 1692 | /* |
| 1693 | * If we were using debug info should retry with |
| 1694 | * original binary. |
| 1695 | */ |
Arnaldo Carvalho de Melo | 6810158 | 2018-03-14 10:34:11 -0300 | [diff] [blame] | 1696 | goto out_free_command; |
Andi Kleen | 62ec9b3 | 2015-11-05 19:06:07 -0800 | [diff] [blame] | 1697 | } |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1698 | |
Andi Kleen | 62ec9b3 | 2015-11-05 19:06:07 -0800 | [diff] [blame] | 1699 | nline = 0; |
| 1700 | while (!feof(file)) { |
Arnaldo Carvalho de Melo | ed7b339 | 2017-03-21 16:00:50 -0300 | [diff] [blame] | 1701 | /* |
| 1702 | * The source code line number (lineno) needs to be kept in |
| 1703 | * accross calls to symbol__parse_objdump_line(), so that it |
| 1704 | * can associate it with the instructions till the next one. |
| 1705 | * See disasm_line__new() and struct disasm_line::line_nr. |
| 1706 | */ |
Jiri Olsa | 1a04db7 | 2017-10-11 17:01:31 +0200 | [diff] [blame] | 1707 | if (symbol__parse_objdump_line(sym, file, args, &lineno) < 0) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1708 | break; |
Andi Kleen | 62ec9b3 | 2015-11-05 19:06:07 -0800 | [diff] [blame] | 1709 | nline++; |
| 1710 | } |
| 1711 | |
| 1712 | if (nline == 0) |
| 1713 | pr_err("No output from %s\n", command); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1714 | |
Adrian Hunter | 484a5e7 | 2013-08-07 14:38:56 +0300 | [diff] [blame] | 1715 | /* |
| 1716 | * kallsyms does not have symbol sizes so there may a nop at the end. |
| 1717 | * Remove it. |
| 1718 | */ |
| 1719 | if (dso__is_kcore(dso)) |
| 1720 | delete_last_nop(sym); |
| 1721 | |
Arnaldo Carvalho de Melo | 9955d0b | 2016-06-15 15:48:08 -0300 | [diff] [blame] | 1722 | fclose(file); |
| 1723 | err = 0; |
Arnaldo Carvalho de Melo | 6810158 | 2018-03-14 10:34:11 -0300 | [diff] [blame] | 1724 | out_free_command: |
| 1725 | free(command); |
Jiri Olsa | 2c7da8c | 2015-03-02 12:56:12 -0500 | [diff] [blame] | 1726 | out_remove_tmp: |
Arnaldo Carvalho de Melo | 9955d0b | 2016-06-15 15:48:08 -0300 | [diff] [blame] | 1727 | close(stdout_fd[0]); |
| 1728 | |
Jiri Olsa | 2c7da8c | 2015-03-02 12:56:12 -0500 | [diff] [blame] | 1729 | if (dso__needs_decompress(dso)) |
| 1730 | unlink(symfs_filename); |
Arnaldo Carvalho de Melo | 3caee09 | 2016-08-09 15:16:37 -0300 | [diff] [blame] | 1731 | |
Adrian Hunter | afba19d | 2013-10-09 15:01:12 +0300 | [diff] [blame] | 1732 | if (delete_extract) |
| 1733 | kcore_extract__delete(&kce); |
Arnaldo Carvalho de Melo | c12944f | 2016-08-09 14:56:13 -0300 | [diff] [blame] | 1734 | out: |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1735 | return err; |
Arnaldo Carvalho de Melo | 9955d0b | 2016-06-15 15:48:08 -0300 | [diff] [blame] | 1736 | |
| 1737 | out_close_stdout: |
| 1738 | close(stdout_fd[1]); |
Arnaldo Carvalho de Melo | 6810158 | 2018-03-14 10:34:11 -0300 | [diff] [blame] | 1739 | goto out_free_command; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1740 | } |
| 1741 | |
Jiri Olsa | 073ae60 | 2017-10-11 17:01:40 +0200 | [diff] [blame] | 1742 | static void calc_percent(struct sym_hist *hist, |
| 1743 | struct annotation_data *sample, |
| 1744 | s64 offset, s64 end) |
| 1745 | { |
| 1746 | unsigned int hits = 0; |
| 1747 | u64 period = 0; |
| 1748 | |
| 1749 | while (offset < end) { |
| 1750 | hits += hist->addr[offset].nr_samples; |
| 1751 | period += hist->addr[offset].period; |
| 1752 | ++offset; |
| 1753 | } |
| 1754 | |
| 1755 | if (hist->nr_samples) { |
| 1756 | sample->he.period = period; |
| 1757 | sample->he.nr_samples = hits; |
| 1758 | sample->percent = 100.0 * hits / hist->nr_samples; |
| 1759 | } |
| 1760 | } |
| 1761 | |
Jiri Olsa | 9e4e0a9 | 2017-11-15 12:05:59 +0100 | [diff] [blame] | 1762 | static void annotation__calc_percent(struct annotation *notes, |
| 1763 | struct perf_evsel *evsel, s64 len) |
Jiri Olsa | 073ae60 | 2017-10-11 17:01:40 +0200 | [diff] [blame] | 1764 | { |
| 1765 | struct annotation_line *al, *next; |
| 1766 | |
| 1767 | list_for_each_entry(al, ¬es->src->source, node) { |
| 1768 | s64 end; |
| 1769 | int i; |
| 1770 | |
| 1771 | if (al->offset == -1) |
| 1772 | continue; |
| 1773 | |
| 1774 | next = annotation_line__next(al, ¬es->src->source); |
| 1775 | end = next ? next->offset : len; |
| 1776 | |
| 1777 | for (i = 0; i < al->samples_nr; i++) { |
| 1778 | struct annotation_data *sample; |
| 1779 | struct sym_hist *hist; |
| 1780 | |
| 1781 | hist = annotation__histogram(notes, evsel->idx + i); |
| 1782 | sample = &al->samples[i]; |
| 1783 | |
| 1784 | calc_percent(hist, sample, al->offset, end); |
| 1785 | } |
| 1786 | } |
Jiri Olsa | 073ae60 | 2017-10-11 17:01:40 +0200 | [diff] [blame] | 1787 | } |
| 1788 | |
Jiri Olsa | 9e4e0a9 | 2017-11-15 12:05:59 +0100 | [diff] [blame] | 1789 | void symbol__calc_percent(struct symbol *sym, struct perf_evsel *evsel) |
Jiri Olsa | 073ae60 | 2017-10-11 17:01:40 +0200 | [diff] [blame] | 1790 | { |
| 1791 | struct annotation *notes = symbol__annotation(sym); |
| 1792 | |
Jiri Olsa | 9e4e0a9 | 2017-11-15 12:05:59 +0100 | [diff] [blame] | 1793 | annotation__calc_percent(notes, evsel, symbol__size(sym)); |
Jiri Olsa | 073ae60 | 2017-10-11 17:01:40 +0200 | [diff] [blame] | 1794 | } |
| 1795 | |
Jiri Olsa | c34df25 | 2017-10-11 17:01:28 +0200 | [diff] [blame] | 1796 | int symbol__annotate(struct symbol *sym, struct map *map, |
Jiri Olsa | d03a686 | 2017-10-11 17:01:33 +0200 | [diff] [blame] | 1797 | struct perf_evsel *evsel, size_t privsize, |
Arnaldo Carvalho de Melo | 5449f13 | 2017-12-11 12:46:11 -0300 | [diff] [blame] | 1798 | struct arch **parch) |
Jiri Olsa | c34df25 | 2017-10-11 17:01:28 +0200 | [diff] [blame] | 1799 | { |
Jiri Olsa | ea07c5a | 2017-10-11 17:01:29 +0200 | [diff] [blame] | 1800 | struct annotate_args args = { |
| 1801 | .privsize = privsize, |
Jiri Olsa | d03a686 | 2017-10-11 17:01:33 +0200 | [diff] [blame] | 1802 | .evsel = evsel, |
Jiri Olsa | ea07c5a | 2017-10-11 17:01:29 +0200 | [diff] [blame] | 1803 | }; |
Arnaldo Carvalho de Melo | 5449f13 | 2017-12-11 12:46:11 -0300 | [diff] [blame] | 1804 | struct perf_env *env = perf_evsel__env(evsel); |
Arnaldo Carvalho de Melo | 3285deb | 2017-12-11 12:52:17 -0300 | [diff] [blame] | 1805 | const char *arch_name = perf_env__arch(env); |
Jiri Olsa | c34df25 | 2017-10-11 17:01:28 +0200 | [diff] [blame] | 1806 | struct arch *arch; |
| 1807 | int err; |
| 1808 | |
Jiri Olsa | c34df25 | 2017-10-11 17:01:28 +0200 | [diff] [blame] | 1809 | if (!arch_name) |
| 1810 | return -1; |
| 1811 | |
Jiri Olsa | 24fe7b8 | 2017-10-11 17:01:30 +0200 | [diff] [blame] | 1812 | args.arch = arch = arch__find(arch_name); |
Jiri Olsa | c34df25 | 2017-10-11 17:01:28 +0200 | [diff] [blame] | 1813 | if (arch == NULL) |
| 1814 | return -ENOTSUP; |
| 1815 | |
| 1816 | if (parch) |
| 1817 | *parch = arch; |
| 1818 | |
| 1819 | if (arch->init) { |
Arnaldo Carvalho de Melo | 5449f13 | 2017-12-11 12:46:11 -0300 | [diff] [blame] | 1820 | err = arch->init(arch, env ? env->cpuid : NULL); |
Jiri Olsa | c34df25 | 2017-10-11 17:01:28 +0200 | [diff] [blame] | 1821 | if (err) { |
| 1822 | pr_err("%s: failed to initialize %s arch priv area\n", __func__, arch->name); |
| 1823 | return err; |
| 1824 | } |
| 1825 | } |
| 1826 | |
Arnaldo Carvalho de Melo | 85a84e4 | 2018-03-20 16:19:08 -0300 | [diff] [blame] | 1827 | args.ms.map = map; |
| 1828 | args.ms.sym = sym; |
| 1829 | |
Jiri Olsa | 05d3f1a | 2017-11-15 12:20:08 +0100 | [diff] [blame] | 1830 | return symbol__disassemble(sym, &args); |
Jiri Olsa | c34df25 | 2017-10-11 17:01:28 +0200 | [diff] [blame] | 1831 | } |
| 1832 | |
Jiri Olsa | 8b4c74d | 2017-10-11 17:01:41 +0200 | [diff] [blame] | 1833 | 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] | 1834 | { |
Jiri Olsa | 8b4c74d | 2017-10-11 17:01:41 +0200 | [diff] [blame] | 1835 | struct annotation_line *iter; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1836 | struct rb_node **p = &root->rb_node; |
| 1837 | struct rb_node *parent = NULL; |
Namhyung Kim | 1491c22 | 2013-03-05 14:53:28 +0900 | [diff] [blame] | 1838 | int i, ret; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1839 | |
| 1840 | while (*p != NULL) { |
| 1841 | parent = *p; |
Jiri Olsa | 8b4c74d | 2017-10-11 17:01:41 +0200 | [diff] [blame] | 1842 | iter = rb_entry(parent, struct annotation_line, rb_node); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1843 | |
Jiri Olsa | 8b4c74d | 2017-10-11 17:01:41 +0200 | [diff] [blame] | 1844 | ret = strcmp(iter->path, al->path); |
Namhyung Kim | 4112796 | 2012-11-09 14:58:49 +0900 | [diff] [blame] | 1845 | if (ret == 0) { |
Jiri Olsa | 8b4c74d | 2017-10-11 17:01:41 +0200 | [diff] [blame] | 1846 | for (i = 0; i < al->samples_nr; i++) |
| 1847 | iter->samples[i].percent_sum += al->samples[i].percent; |
Namhyung Kim | 4112796 | 2012-11-09 14:58:49 +0900 | [diff] [blame] | 1848 | return; |
| 1849 | } |
| 1850 | |
| 1851 | if (ret < 0) |
| 1852 | p = &(*p)->rb_left; |
| 1853 | else |
| 1854 | p = &(*p)->rb_right; |
| 1855 | } |
| 1856 | |
Jiri Olsa | 8b4c74d | 2017-10-11 17:01:41 +0200 | [diff] [blame] | 1857 | for (i = 0; i < al->samples_nr; i++) |
| 1858 | al->samples[i].percent_sum = al->samples[i].percent; |
Namhyung Kim | 4112796 | 2012-11-09 14:58:49 +0900 | [diff] [blame] | 1859 | |
Jiri Olsa | 8b4c74d | 2017-10-11 17:01:41 +0200 | [diff] [blame] | 1860 | rb_link_node(&al->rb_node, parent, p); |
| 1861 | rb_insert_color(&al->rb_node, root); |
Namhyung Kim | 4112796 | 2012-11-09 14:58:49 +0900 | [diff] [blame] | 1862 | } |
| 1863 | |
Jiri Olsa | 8b4c74d | 2017-10-11 17:01:41 +0200 | [diff] [blame] | 1864 | 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] | 1865 | { |
| 1866 | int i; |
| 1867 | |
Jiri Olsa | 8b4c74d | 2017-10-11 17:01:41 +0200 | [diff] [blame] | 1868 | for (i = 0; i < a->samples_nr; i++) { |
Arnaldo Carvalho de Melo | 276af92f | 2015-06-19 16:36:12 -0300 | [diff] [blame] | 1869 | if (a->samples[i].percent_sum == b->samples[i].percent_sum) |
Namhyung Kim | 1491c22 | 2013-03-05 14:53:28 +0900 | [diff] [blame] | 1870 | continue; |
Arnaldo Carvalho de Melo | 276af92f | 2015-06-19 16:36:12 -0300 | [diff] [blame] | 1871 | return a->samples[i].percent_sum > b->samples[i].percent_sum; |
Namhyung Kim | 1491c22 | 2013-03-05 14:53:28 +0900 | [diff] [blame] | 1872 | } |
| 1873 | |
| 1874 | return 0; |
| 1875 | } |
| 1876 | |
Jiri Olsa | 8b4c74d | 2017-10-11 17:01:41 +0200 | [diff] [blame] | 1877 | 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] | 1878 | { |
Jiri Olsa | 8b4c74d | 2017-10-11 17:01:41 +0200 | [diff] [blame] | 1879 | struct annotation_line *iter; |
Namhyung Kim | 4112796 | 2012-11-09 14:58:49 +0900 | [diff] [blame] | 1880 | struct rb_node **p = &root->rb_node; |
| 1881 | struct rb_node *parent = NULL; |
| 1882 | |
| 1883 | while (*p != NULL) { |
| 1884 | parent = *p; |
Jiri Olsa | 8b4c74d | 2017-10-11 17:01:41 +0200 | [diff] [blame] | 1885 | iter = rb_entry(parent, struct annotation_line, rb_node); |
Namhyung Kim | 4112796 | 2012-11-09 14:58:49 +0900 | [diff] [blame] | 1886 | |
Jiri Olsa | 8b4c74d | 2017-10-11 17:01:41 +0200 | [diff] [blame] | 1887 | if (cmp_source_line(al, iter)) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1888 | p = &(*p)->rb_left; |
| 1889 | else |
| 1890 | p = &(*p)->rb_right; |
| 1891 | } |
| 1892 | |
Jiri Olsa | 8b4c74d | 2017-10-11 17:01:41 +0200 | [diff] [blame] | 1893 | rb_link_node(&al->rb_node, parent, p); |
| 1894 | rb_insert_color(&al->rb_node, root); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1895 | } |
| 1896 | |
Namhyung Kim | 4112796 | 2012-11-09 14:58:49 +0900 | [diff] [blame] | 1897 | static void resort_source_line(struct rb_root *dest_root, struct rb_root *src_root) |
| 1898 | { |
Jiri Olsa | 8b4c74d | 2017-10-11 17:01:41 +0200 | [diff] [blame] | 1899 | struct annotation_line *al; |
Namhyung Kim | 4112796 | 2012-11-09 14:58:49 +0900 | [diff] [blame] | 1900 | struct rb_node *node; |
| 1901 | |
| 1902 | node = rb_first(src_root); |
| 1903 | while (node) { |
| 1904 | struct rb_node *next; |
| 1905 | |
Jiri Olsa | 8b4c74d | 2017-10-11 17:01:41 +0200 | [diff] [blame] | 1906 | al = rb_entry(node, struct annotation_line, rb_node); |
Namhyung Kim | 4112796 | 2012-11-09 14:58:49 +0900 | [diff] [blame] | 1907 | next = rb_next(node); |
| 1908 | rb_erase(node, src_root); |
| 1909 | |
Jiri Olsa | 8b4c74d | 2017-10-11 17:01:41 +0200 | [diff] [blame] | 1910 | __resort_source_line(dest_root, al); |
Namhyung Kim | 4112796 | 2012-11-09 14:58:49 +0900 | [diff] [blame] | 1911 | node = next; |
| 1912 | } |
| 1913 | } |
| 1914 | |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1915 | static void print_summary(struct rb_root *root, const char *filename) |
| 1916 | { |
Jiri Olsa | 8b4c74d | 2017-10-11 17:01:41 +0200 | [diff] [blame] | 1917 | struct annotation_line *al; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1918 | struct rb_node *node; |
| 1919 | |
| 1920 | printf("\nSorted summary for file %s\n", filename); |
| 1921 | printf("----------------------------------------------\n\n"); |
| 1922 | |
| 1923 | if (RB_EMPTY_ROOT(root)) { |
| 1924 | printf(" Nothing higher than %1.1f%%\n", MIN_GREEN); |
| 1925 | return; |
| 1926 | } |
| 1927 | |
| 1928 | node = rb_first(root); |
| 1929 | while (node) { |
Namhyung Kim | 1491c22 | 2013-03-05 14:53:28 +0900 | [diff] [blame] | 1930 | double percent, percent_max = 0.0; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1931 | const char *color; |
| 1932 | char *path; |
Namhyung Kim | 1491c22 | 2013-03-05 14:53:28 +0900 | [diff] [blame] | 1933 | int i; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1934 | |
Jiri Olsa | 8b4c74d | 2017-10-11 17:01:41 +0200 | [diff] [blame] | 1935 | al = rb_entry(node, struct annotation_line, rb_node); |
| 1936 | for (i = 0; i < al->samples_nr; i++) { |
| 1937 | percent = al->samples[i].percent_sum; |
Namhyung Kim | 1491c22 | 2013-03-05 14:53:28 +0900 | [diff] [blame] | 1938 | color = get_percent_color(percent); |
| 1939 | color_fprintf(stdout, color, " %7.2f", percent); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1940 | |
Namhyung Kim | 1491c22 | 2013-03-05 14:53:28 +0900 | [diff] [blame] | 1941 | if (percent > percent_max) |
| 1942 | percent_max = percent; |
| 1943 | } |
| 1944 | |
Jiri Olsa | 8b4c74d | 2017-10-11 17:01:41 +0200 | [diff] [blame] | 1945 | path = al->path; |
Namhyung Kim | 1491c22 | 2013-03-05 14:53:28 +0900 | [diff] [blame] | 1946 | color = get_percent_color(percent_max); |
Namhyung Kim | f048d54 | 2013-09-11 14:09:28 +0900 | [diff] [blame] | 1947 | color_fprintf(stdout, color, " %s\n", path); |
Namhyung Kim | 1491c22 | 2013-03-05 14:53:28 +0900 | [diff] [blame] | 1948 | |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1949 | node = rb_next(node); |
| 1950 | } |
| 1951 | } |
| 1952 | |
Namhyung Kim | db8fd07 | 2013-03-05 14:53:21 +0900 | [diff] [blame] | 1953 | 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] | 1954 | { |
| 1955 | struct annotation *notes = symbol__annotation(sym); |
Namhyung Kim | db8fd07 | 2013-03-05 14:53:21 +0900 | [diff] [blame] | 1956 | struct sym_hist *h = annotation__histogram(notes, evsel->idx); |
Arnaldo Carvalho de Melo | 1b2e2df | 2012-04-19 10:57:06 -0300 | [diff] [blame] | 1957 | u64 len = symbol__size(sym), offset; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1958 | |
| 1959 | for (offset = 0; offset < len; ++offset) |
Taeung Song | 896bccd | 2017-07-20 06:36:45 +0900 | [diff] [blame] | 1960 | if (h->addr[offset].nr_samples != 0) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1961 | printf("%*" PRIx64 ": %" PRIu64 "\n", BITS_PER_LONG / 2, |
Taeung Song | 896bccd | 2017-07-20 06:36:45 +0900 | [diff] [blame] | 1962 | sym->start + offset, h->addr[offset].nr_samples); |
Taeung Song | 8158683d | 2017-07-20 06:36:51 +0900 | [diff] [blame] | 1963 | 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] | 1964 | } |
| 1965 | |
Jiri Olsa | f48e7c4 | 2017-10-11 17:01:58 +0200 | [diff] [blame] | 1966 | static int annotated_source__addr_fmt_width(struct list_head *lines, u64 start) |
| 1967 | { |
| 1968 | char bf[32]; |
| 1969 | struct annotation_line *line; |
| 1970 | |
| 1971 | list_for_each_entry_reverse(line, lines, node) { |
| 1972 | if (line->offset != -1) |
| 1973 | return scnprintf(bf, sizeof(bf), "%" PRIx64, start + line->offset); |
| 1974 | } |
| 1975 | |
| 1976 | return 0; |
| 1977 | } |
| 1978 | |
Namhyung Kim | db8fd07 | 2013-03-05 14:53:21 +0900 | [diff] [blame] | 1979 | int symbol__annotate_printf(struct symbol *sym, struct map *map, |
| 1980 | struct perf_evsel *evsel, bool full_paths, |
| 1981 | int min_pcnt, int max_lines, int context) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1982 | { |
| 1983 | struct dso *dso = map->dso; |
David Ahern | bfd14b9 | 2012-09-08 09:06:50 -0600 | [diff] [blame] | 1984 | char *filename; |
| 1985 | const char *d_filename; |
Arnaldo Carvalho de Melo | 9cdbadce | 2014-03-18 11:50:21 -0300 | [diff] [blame] | 1986 | const char *evsel_name = perf_evsel__name(evsel); |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 1987 | struct annotation *notes = symbol__annotation(sym); |
Peter Zijlstra (Intel) | 135cce1 | 2016-06-30 10:29:55 +0200 | [diff] [blame] | 1988 | struct sym_hist *h = annotation__histogram(notes, evsel->idx); |
Jiri Olsa | 8f25b81 | 2017-10-11 17:01:46 +0200 | [diff] [blame] | 1989 | struct annotation_line *pos, *queue = NULL; |
Arnaldo Carvalho de Melo | 058b4cc | 2012-04-02 12:59:01 -0300 | [diff] [blame] | 1990 | u64 start = map__rip_2objdump(map, sym->start); |
Jiri Olsa | f48e7c4 | 2017-10-11 17:01:58 +0200 | [diff] [blame] | 1991 | int printed = 2, queue_len = 0, addr_fmt_width; |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 1992 | int more = 0; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1993 | u64 len; |
Arnaldo Carvalho de Melo | ce9ee4a | 2017-07-26 17:16:46 -0300 | [diff] [blame] | 1994 | int width = symbol_conf.show_total_period ? 12 : 8; |
Peter Zijlstra (Intel) | 53dd9b5 | 2016-06-30 09:17:26 -0300 | [diff] [blame] | 1995 | int graph_dotted_len; |
Jin Yao | 787e4da | 2018-05-22 19:38:35 +0800 | [diff] [blame] | 1996 | char buf[512]; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1997 | |
David Ahern | bfd14b9 | 2012-09-08 09:06:50 -0600 | [diff] [blame] | 1998 | filename = strdup(dso->long_name); |
| 1999 | if (!filename) |
| 2000 | return -ENOMEM; |
| 2001 | |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 2002 | if (full_paths) |
| 2003 | d_filename = filename; |
| 2004 | else |
| 2005 | d_filename = basename(filename); |
| 2006 | |
Arnaldo Carvalho de Melo | 1b2e2df | 2012-04-19 10:57:06 -0300 | [diff] [blame] | 2007 | len = symbol__size(sym); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 2008 | |
Jin Yao | 787e4da | 2018-05-22 19:38:35 +0800 | [diff] [blame] | 2009 | if (perf_evsel__is_group_event(evsel)) { |
Namhyung Kim | b1dd443 | 2013-03-05 14:53:25 +0900 | [diff] [blame] | 2010 | width *= evsel->nr_members; |
Jin Yao | 787e4da | 2018-05-22 19:38:35 +0800 | [diff] [blame] | 2011 | perf_evsel__group_desc(evsel, buf, sizeof(buf)); |
| 2012 | evsel_name = buf; |
| 2013 | } |
Namhyung Kim | b1dd443 | 2013-03-05 14:53:25 +0900 | [diff] [blame] | 2014 | |
Peter Zijlstra (Intel) | 135cce1 | 2016-06-30 10:29:55 +0200 | [diff] [blame] | 2015 | 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] | 2016 | width, width, symbol_conf.show_total_period ? "Period" : |
| 2017 | symbol_conf.show_nr_samples ? "Samples" : "Percent", |
Taeung Song | 38d2dcd | 2017-07-25 06:28:42 +0900 | [diff] [blame] | 2018 | d_filename, evsel_name, h->nr_samples); |
Arnaldo Carvalho de Melo | 9cdbadce | 2014-03-18 11:50:21 -0300 | [diff] [blame] | 2019 | |
Peter Zijlstra (Intel) | 53dd9b5 | 2016-06-30 09:17:26 -0300 | [diff] [blame] | 2020 | printf("%-*.*s----\n", |
Arnaldo Carvalho de Melo | 9cdbadce | 2014-03-18 11:50:21 -0300 | [diff] [blame] | 2021 | graph_dotted_len, graph_dotted_len, graph_dotted_line); |
Arnaldo Carvalho de Melo | f1e2701 | 2011-02-05 18:51:38 -0200 | [diff] [blame] | 2022 | |
Namhyung Kim | bb963e1 | 2017-02-17 17:17:38 +0900 | [diff] [blame] | 2023 | if (verbose > 0) |
Namhyung Kim | db8fd07 | 2013-03-05 14:53:21 +0900 | [diff] [blame] | 2024 | symbol__annotate_hits(sym, evsel); |
Arnaldo Carvalho de Melo | f1e2701 | 2011-02-05 18:51:38 -0200 | [diff] [blame] | 2025 | |
Jiri Olsa | f48e7c4 | 2017-10-11 17:01:58 +0200 | [diff] [blame] | 2026 | addr_fmt_width = annotated_source__addr_fmt_width(¬es->src->source, start); |
| 2027 | |
Jiri Olsa | 8f25b81 | 2017-10-11 17:01:46 +0200 | [diff] [blame] | 2028 | list_for_each_entry(pos, ¬es->src->source, node) { |
| 2029 | int err; |
| 2030 | |
Arnaldo Carvalho de Melo | d5e3d74 | 2011-02-08 15:29:25 -0200 | [diff] [blame] | 2031 | if (context && queue == NULL) { |
| 2032 | queue = pos; |
| 2033 | queue_len = 0; |
| 2034 | } |
| 2035 | |
Jiri Olsa | 8f25b81 | 2017-10-11 17:01:46 +0200 | [diff] [blame] | 2036 | err = annotation_line__print(pos, sym, start, evsel, len, |
| 2037 | min_pcnt, printed, max_lines, |
Jiri Olsa | f48e7c4 | 2017-10-11 17:01:58 +0200 | [diff] [blame] | 2038 | queue, addr_fmt_width); |
Jiri Olsa | 8f25b81 | 2017-10-11 17:01:46 +0200 | [diff] [blame] | 2039 | |
| 2040 | switch (err) { |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 2041 | case 0: |
| 2042 | ++printed; |
Arnaldo Carvalho de Melo | d5e3d74 | 2011-02-08 15:29:25 -0200 | [diff] [blame] | 2043 | if (context) { |
| 2044 | printed += queue_len; |
| 2045 | queue = NULL; |
| 2046 | queue_len = 0; |
| 2047 | } |
Arnaldo Carvalho de Melo | f1e2701 | 2011-02-05 18:51:38 -0200 | [diff] [blame] | 2048 | break; |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 2049 | case 1: |
| 2050 | /* filtered by max_lines */ |
| 2051 | ++more; |
| 2052 | break; |
| 2053 | case -1: |
| 2054 | default: |
Arnaldo Carvalho de Melo | d5e3d74 | 2011-02-08 15:29:25 -0200 | [diff] [blame] | 2055 | /* |
| 2056 | * Filtered by min_pcnt or non IP lines when |
| 2057 | * context != 0 |
| 2058 | */ |
| 2059 | if (!context) |
| 2060 | break; |
| 2061 | if (queue_len == context) |
Jiri Olsa | 8f25b81 | 2017-10-11 17:01:46 +0200 | [diff] [blame] | 2062 | queue = list_entry(queue->node.next, typeof(*queue), node); |
Arnaldo Carvalho de Melo | d5e3d74 | 2011-02-08 15:29:25 -0200 | [diff] [blame] | 2063 | else |
| 2064 | ++queue_len; |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 2065 | break; |
| 2066 | } |
| 2067 | } |
Arnaldo Carvalho de Melo | f1e2701 | 2011-02-05 18:51:38 -0200 | [diff] [blame] | 2068 | |
David Ahern | bfd14b9 | 2012-09-08 09:06:50 -0600 | [diff] [blame] | 2069 | free(filename); |
| 2070 | |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 2071 | return more; |
| 2072 | } |
| 2073 | |
Arnaldo Carvalho de Melo | befd2a3 | 2018-03-15 23:44:34 -0300 | [diff] [blame] | 2074 | static void FILE__set_percent_color(void *fp __maybe_unused, |
| 2075 | double percent __maybe_unused, |
| 2076 | bool current __maybe_unused) |
| 2077 | { |
| 2078 | } |
| 2079 | |
| 2080 | static int FILE__set_jumps_percent_color(void *fp __maybe_unused, |
| 2081 | int nr __maybe_unused, bool current __maybe_unused) |
| 2082 | { |
| 2083 | return 0; |
| 2084 | } |
| 2085 | |
| 2086 | static int FILE__set_color(void *fp __maybe_unused, int color __maybe_unused) |
| 2087 | { |
| 2088 | return 0; |
| 2089 | } |
| 2090 | |
| 2091 | static void FILE__printf(void *fp, const char *fmt, ...) |
| 2092 | { |
| 2093 | va_list args; |
| 2094 | |
| 2095 | va_start(args, fmt); |
| 2096 | vfprintf(fp, fmt, args); |
| 2097 | va_end(args); |
| 2098 | } |
| 2099 | |
| 2100 | static void FILE__write_graph(void *fp, int graph) |
| 2101 | { |
| 2102 | const char *s; |
| 2103 | switch (graph) { |
| 2104 | |
| 2105 | case DARROW_CHAR: s = "↓"; break; |
| 2106 | case UARROW_CHAR: s = "↑"; break; |
| 2107 | case LARROW_CHAR: s = "←"; break; |
| 2108 | case RARROW_CHAR: s = "→"; break; |
| 2109 | default: s = "?"; break; |
| 2110 | } |
| 2111 | |
| 2112 | fputs(s, fp); |
| 2113 | } |
| 2114 | |
| 2115 | int symbol__annotate_fprintf2(struct symbol *sym, FILE *fp) |
| 2116 | { |
| 2117 | struct annotation *notes = symbol__annotation(sym); |
| 2118 | struct annotation_write_ops ops = { |
| 2119 | .first_line = true, |
| 2120 | .obj = fp, |
| 2121 | .set_color = FILE__set_color, |
| 2122 | .set_percent_color = FILE__set_percent_color, |
| 2123 | .set_jumps_percent_color = FILE__set_jumps_percent_color, |
| 2124 | .printf = FILE__printf, |
| 2125 | .write_graph = FILE__write_graph, |
| 2126 | }; |
| 2127 | struct annotation_line *al; |
| 2128 | |
| 2129 | list_for_each_entry(al, ¬es->src->source, node) { |
| 2130 | if (annotation_line__filter(al, notes)) |
| 2131 | continue; |
| 2132 | annotation_line__write(al, notes, &ops); |
| 2133 | fputc('\n', fp); |
| 2134 | ops.first_line = false; |
| 2135 | } |
| 2136 | |
| 2137 | return 0; |
| 2138 | } |
| 2139 | |
Arnaldo Carvalho de Melo | d9bd766 | 2018-03-16 16:57:47 -0300 | [diff] [blame] | 2140 | int map_symbol__annotation_dump(struct map_symbol *ms, struct perf_evsel *evsel) |
| 2141 | { |
| 2142 | const char *ev_name = perf_evsel__name(evsel); |
| 2143 | char buf[1024]; |
| 2144 | char *filename; |
| 2145 | int err = -1; |
| 2146 | FILE *fp; |
| 2147 | |
| 2148 | if (asprintf(&filename, "%s.annotation", ms->sym->name) < 0) |
| 2149 | return -1; |
| 2150 | |
| 2151 | fp = fopen(filename, "w"); |
| 2152 | if (fp == NULL) |
| 2153 | goto out_free_filename; |
| 2154 | |
| 2155 | if (perf_evsel__is_group_event(evsel)) { |
| 2156 | perf_evsel__group_desc(evsel, buf, sizeof(buf)); |
| 2157 | ev_name = buf; |
| 2158 | } |
| 2159 | |
| 2160 | fprintf(fp, "%s() %s\nEvent: %s\n\n", |
| 2161 | ms->sym->name, ms->map->dso->long_name, ev_name); |
| 2162 | symbol__annotate_fprintf2(ms->sym, fp); |
| 2163 | |
| 2164 | fclose(fp); |
| 2165 | err = 0; |
| 2166 | out_free_filename: |
| 2167 | free(filename); |
| 2168 | return err; |
| 2169 | } |
| 2170 | |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 2171 | void symbol__annotate_zero_histogram(struct symbol *sym, int evidx) |
| 2172 | { |
| 2173 | struct annotation *notes = symbol__annotation(sym); |
| 2174 | struct sym_hist *h = annotation__histogram(notes, evidx); |
| 2175 | |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 2176 | memset(h, 0, notes->src->sizeof_sym_hist); |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 2177 | } |
| 2178 | |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 2179 | void symbol__annotate_decay_histogram(struct symbol *sym, int evidx) |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 2180 | { |
| 2181 | struct annotation *notes = symbol__annotation(sym); |
| 2182 | struct sym_hist *h = annotation__histogram(notes, evidx); |
Arnaldo Carvalho de Melo | 1b2e2df | 2012-04-19 10:57:06 -0300 | [diff] [blame] | 2183 | int len = symbol__size(sym), offset; |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 2184 | |
Taeung Song | 8158683d | 2017-07-20 06:36:51 +0900 | [diff] [blame] | 2185 | h->nr_samples = 0; |
Arnaldo Carvalho de Melo | 8b84a56 | 2012-04-05 16:15:59 -0300 | [diff] [blame] | 2186 | for (offset = 0; offset < len; ++offset) { |
Taeung Song | 896bccd | 2017-07-20 06:36:45 +0900 | [diff] [blame] | 2187 | h->addr[offset].nr_samples = h->addr[offset].nr_samples * 7 / 8; |
Taeung Song | 8158683d | 2017-07-20 06:36:51 +0900 | [diff] [blame] | 2188 | h->nr_samples += h->addr[offset].nr_samples; |
Arnaldo Carvalho de Melo | f1e2701 | 2011-02-05 18:51:38 -0200 | [diff] [blame] | 2189 | } |
| 2190 | } |
| 2191 | |
Jiri Olsa | f8eb37b | 2017-10-11 17:01:38 +0200 | [diff] [blame] | 2192 | void annotated_source__purge(struct annotated_source *as) |
Arnaldo Carvalho de Melo | f1e2701 | 2011-02-05 18:51:38 -0200 | [diff] [blame] | 2193 | { |
Jiri Olsa | f8eb37b | 2017-10-11 17:01:38 +0200 | [diff] [blame] | 2194 | struct annotation_line *al, *n; |
Arnaldo Carvalho de Melo | f1e2701 | 2011-02-05 18:51:38 -0200 | [diff] [blame] | 2195 | |
Jiri Olsa | f8eb37b | 2017-10-11 17:01:38 +0200 | [diff] [blame] | 2196 | list_for_each_entry_safe(al, n, &as->source, node) { |
| 2197 | list_del(&al->node); |
| 2198 | disasm_line__free(disasm_line(al)); |
Arnaldo Carvalho de Melo | f1e2701 | 2011-02-05 18:51:38 -0200 | [diff] [blame] | 2199 | } |
| 2200 | } |
| 2201 | |
Arnaldo Carvalho de Melo | 5145418 | 2012-04-15 15:52:18 -0300 | [diff] [blame] | 2202 | static size_t disasm_line__fprintf(struct disasm_line *dl, FILE *fp) |
| 2203 | { |
| 2204 | size_t printed; |
| 2205 | |
Jiri Olsa | d5490b9 | 2017-10-11 17:01:26 +0200 | [diff] [blame] | 2206 | if (dl->al.offset == -1) |
| 2207 | return fprintf(fp, "%s\n", dl->al.line); |
Arnaldo Carvalho de Melo | 5145418 | 2012-04-15 15:52:18 -0300 | [diff] [blame] | 2208 | |
Jiri Olsa | d5490b9 | 2017-10-11 17:01:26 +0200 | [diff] [blame] | 2209 | 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] | 2210 | |
Arnaldo Carvalho de Melo | c7e6ead | 2012-04-20 14:38:46 -0300 | [diff] [blame] | 2211 | if (dl->ops.raw[0] != '\0') { |
Arnaldo Carvalho de Melo | 5145418 | 2012-04-15 15:52:18 -0300 | [diff] [blame] | 2212 | printed += fprintf(fp, "%.*s %s\n", 6 - (int)printed, " ", |
Arnaldo Carvalho de Melo | c7e6ead | 2012-04-20 14:38:46 -0300 | [diff] [blame] | 2213 | dl->ops.raw); |
Arnaldo Carvalho de Melo | 5145418 | 2012-04-15 15:52:18 -0300 | [diff] [blame] | 2214 | } |
| 2215 | |
| 2216 | return printed + fprintf(fp, "\n"); |
| 2217 | } |
| 2218 | |
| 2219 | size_t disasm__fprintf(struct list_head *head, FILE *fp) |
| 2220 | { |
| 2221 | struct disasm_line *pos; |
| 2222 | size_t printed = 0; |
| 2223 | |
Jiri Olsa | a17c4ca | 2017-10-11 17:01:25 +0200 | [diff] [blame] | 2224 | list_for_each_entry(pos, head, al.node) |
Arnaldo Carvalho de Melo | 5145418 | 2012-04-15 15:52:18 -0300 | [diff] [blame] | 2225 | printed += disasm_line__fprintf(pos, fp); |
| 2226 | |
| 2227 | return printed; |
| 2228 | } |
| 2229 | |
Arnaldo Carvalho de Melo | 2eff061 | 2018-03-23 10:12:33 -0300 | [diff] [blame] | 2230 | bool disasm_line__is_valid_local_jump(struct disasm_line *dl, struct symbol *sym) |
Arnaldo Carvalho de Melo | 0db45bc | 2018-03-15 15:31:56 -0300 | [diff] [blame] | 2231 | { |
| 2232 | if (!dl || !dl->ins.ops || !ins__is_jump(&dl->ins) || |
Arnaldo Carvalho de Melo | 2eff061 | 2018-03-23 10:12:33 -0300 | [diff] [blame] | 2233 | !disasm_line__has_local_offset(dl) || dl->ops.target.offset < 0 || |
Arnaldo Carvalho de Melo | 0db45bc | 2018-03-15 15:31:56 -0300 | [diff] [blame] | 2234 | dl->ops.target.offset >= (s64)symbol__size(sym)) |
| 2235 | return false; |
| 2236 | |
| 2237 | return true; |
| 2238 | } |
| 2239 | |
| 2240 | void annotation__mark_jump_targets(struct annotation *notes, struct symbol *sym) |
| 2241 | { |
| 2242 | u64 offset, size = symbol__size(sym); |
| 2243 | |
| 2244 | /* PLT symbols contain external offsets */ |
| 2245 | if (strstr(sym->name, "@plt")) |
| 2246 | return; |
| 2247 | |
| 2248 | for (offset = 0; offset < size; ++offset) { |
| 2249 | struct annotation_line *al = notes->offsets[offset]; |
| 2250 | struct disasm_line *dl; |
| 2251 | |
| 2252 | dl = disasm_line(al); |
| 2253 | |
Arnaldo Carvalho de Melo | 2eff061 | 2018-03-23 10:12:33 -0300 | [diff] [blame] | 2254 | if (!disasm_line__is_valid_local_jump(dl, sym)) |
Arnaldo Carvalho de Melo | 0db45bc | 2018-03-15 15:31:56 -0300 | [diff] [blame] | 2255 | continue; |
| 2256 | |
| 2257 | al = notes->offsets[dl->ops.target.offset]; |
| 2258 | |
| 2259 | /* |
| 2260 | * FIXME: Oops, no jump target? Buggy disassembler? Or do we |
| 2261 | * have to adjust to the previous offset? |
| 2262 | */ |
| 2263 | if (al == NULL) |
| 2264 | continue; |
| 2265 | |
| 2266 | if (++al->jump_sources > notes->max_jump_sources) |
| 2267 | notes->max_jump_sources = al->jump_sources; |
| 2268 | |
| 2269 | ++notes->nr_jumps; |
| 2270 | } |
| 2271 | } |
| 2272 | |
Arnaldo Carvalho de Melo | 5bc49f6 | 2018-03-15 15:59:01 -0300 | [diff] [blame] | 2273 | void annotation__set_offsets(struct annotation *notes, s64 size) |
| 2274 | { |
| 2275 | struct annotation_line *al; |
| 2276 | |
| 2277 | notes->max_line_len = 0; |
| 2278 | |
| 2279 | list_for_each_entry(al, ¬es->src->source, node) { |
| 2280 | size_t line_len = strlen(al->line); |
| 2281 | |
| 2282 | if (notes->max_line_len < line_len) |
| 2283 | notes->max_line_len = line_len; |
| 2284 | al->idx = notes->nr_entries++; |
| 2285 | if (al->offset != -1) { |
| 2286 | al->idx_asm = notes->nr_asm_entries++; |
| 2287 | /* |
| 2288 | * FIXME: short term bandaid to cope with assembly |
| 2289 | * routines that comes with labels in the same column |
| 2290 | * as the address in objdump, sigh. |
| 2291 | * |
| 2292 | * E.g. copy_user_generic_unrolled |
| 2293 | */ |
| 2294 | if (al->offset < size) |
| 2295 | notes->offsets[al->offset] = al; |
| 2296 | } else |
| 2297 | al->idx_asm = -1; |
| 2298 | } |
| 2299 | } |
| 2300 | |
Arnaldo Carvalho de Melo | b8b0d81 | 2018-03-15 16:26:29 -0300 | [diff] [blame] | 2301 | static inline int width_jumps(int n) |
| 2302 | { |
| 2303 | if (n >= 100) |
| 2304 | return 5; |
| 2305 | if (n / 10) |
| 2306 | return 2; |
| 2307 | return 1; |
| 2308 | } |
| 2309 | |
| 2310 | void annotation__init_column_widths(struct annotation *notes, struct symbol *sym) |
| 2311 | { |
| 2312 | notes->widths.addr = notes->widths.target = |
| 2313 | notes->widths.min_addr = hex_width(symbol__size(sym)); |
| 2314 | notes->widths.max_addr = hex_width(sym->end); |
| 2315 | notes->widths.jumps = width_jumps(notes->max_jump_sources); |
| 2316 | } |
| 2317 | |
Arnaldo Carvalho de Melo | 7232bf7 | 2018-03-15 16:19:59 -0300 | [diff] [blame] | 2318 | void annotation__update_column_widths(struct annotation *notes) |
| 2319 | { |
| 2320 | if (notes->options->use_offset) |
| 2321 | notes->widths.target = notes->widths.min_addr; |
| 2322 | else |
| 2323 | notes->widths.target = notes->widths.max_addr; |
| 2324 | |
| 2325 | notes->widths.addr = notes->widths.target; |
| 2326 | |
| 2327 | if (notes->options->show_nr_jumps) |
| 2328 | notes->widths.addr += notes->widths.jumps + 1; |
| 2329 | } |
| 2330 | |
Jiri Olsa | 8b4c74d | 2017-10-11 17:01:41 +0200 | [diff] [blame] | 2331 | static void annotation__calc_lines(struct annotation *notes, struct map *map, |
Arnaldo Carvalho de Melo | 425859f | 2018-03-20 11:03:30 -0300 | [diff] [blame] | 2332 | struct rb_root *root) |
Jiri Olsa | 8b4c74d | 2017-10-11 17:01:41 +0200 | [diff] [blame] | 2333 | { |
| 2334 | struct annotation_line *al; |
| 2335 | struct rb_root tmp_root = RB_ROOT; |
| 2336 | |
| 2337 | list_for_each_entry(al, ¬es->src->source, node) { |
| 2338 | double percent_max = 0.0; |
| 2339 | int i; |
| 2340 | |
| 2341 | for (i = 0; i < al->samples_nr; i++) { |
| 2342 | struct annotation_data *sample; |
| 2343 | |
| 2344 | sample = &al->samples[i]; |
| 2345 | |
| 2346 | if (sample->percent > percent_max) |
| 2347 | percent_max = sample->percent; |
| 2348 | } |
| 2349 | |
| 2350 | if (percent_max <= 0.5) |
| 2351 | continue; |
| 2352 | |
Arnaldo Carvalho de Melo | 425859f | 2018-03-20 11:03:30 -0300 | [diff] [blame] | 2353 | al->path = get_srcline(map->dso, notes->start + al->offset, NULL, |
| 2354 | false, true, notes->start + al->offset); |
Jiri Olsa | 8b4c74d | 2017-10-11 17:01:41 +0200 | [diff] [blame] | 2355 | insert_source_line(&tmp_root, al); |
| 2356 | } |
| 2357 | |
| 2358 | resort_source_line(root, &tmp_root); |
| 2359 | } |
| 2360 | |
| 2361 | static void symbol__calc_lines(struct symbol *sym, struct map *map, |
| 2362 | struct rb_root *root) |
| 2363 | { |
| 2364 | struct annotation *notes = symbol__annotation(sym); |
Jiri Olsa | 8b4c74d | 2017-10-11 17:01:41 +0200 | [diff] [blame] | 2365 | |
Arnaldo Carvalho de Melo | 425859f | 2018-03-20 11:03:30 -0300 | [diff] [blame] | 2366 | annotation__calc_lines(notes, map, root); |
Jiri Olsa | 8b4c74d | 2017-10-11 17:01:41 +0200 | [diff] [blame] | 2367 | } |
| 2368 | |
Arnaldo Carvalho de Melo | befd2a3 | 2018-03-15 23:44:34 -0300 | [diff] [blame] | 2369 | int symbol__tty_annotate2(struct symbol *sym, struct map *map, |
| 2370 | struct perf_evsel *evsel, bool print_lines, |
| 2371 | bool full_paths) |
| 2372 | { |
| 2373 | struct dso *dso = map->dso; |
| 2374 | struct rb_root source_line = RB_ROOT; |
Arnaldo Carvalho de Melo | 3563289 | 2018-03-16 14:37:33 -0300 | [diff] [blame] | 2375 | struct annotation_options opts = annotation__default_options; |
Arnaldo Carvalho de Melo | 520d3f0 | 2018-04-02 16:42:01 -0300 | [diff] [blame] | 2376 | struct annotation *notes = symbol__annotation(sym); |
Arnaldo Carvalho de Melo | 864298f2 | 2018-03-16 15:17:23 -0300 | [diff] [blame] | 2377 | char buf[1024]; |
Arnaldo Carvalho de Melo | befd2a3 | 2018-03-15 23:44:34 -0300 | [diff] [blame] | 2378 | |
| 2379 | if (symbol__annotate2(sym, map, evsel, &opts, NULL) < 0) |
| 2380 | return -1; |
| 2381 | |
| 2382 | if (print_lines) { |
| 2383 | srcline_full_filename = full_paths; |
| 2384 | symbol__calc_lines(sym, map, &source_line); |
| 2385 | print_summary(&source_line, dso->long_name); |
| 2386 | } |
| 2387 | |
Arnaldo Carvalho de Melo | 520d3f0 | 2018-04-02 16:42:01 -0300 | [diff] [blame] | 2388 | annotation__scnprintf_samples_period(notes, buf, sizeof(buf), evsel); |
| 2389 | fprintf(stdout, "%s\n%s() %s\n", buf, sym->name, dso->long_name); |
Arnaldo Carvalho de Melo | befd2a3 | 2018-03-15 23:44:34 -0300 | [diff] [blame] | 2390 | symbol__annotate_fprintf2(sym, stdout); |
| 2391 | |
| 2392 | annotated_source__purge(symbol__annotation(sym)->src); |
| 2393 | |
| 2394 | return 0; |
| 2395 | } |
| 2396 | |
Namhyung Kim | db8fd07 | 2013-03-05 14:53:21 +0900 | [diff] [blame] | 2397 | int symbol__tty_annotate(struct symbol *sym, struct map *map, |
| 2398 | struct perf_evsel *evsel, bool print_lines, |
| 2399 | bool full_paths, int min_pcnt, int max_lines) |
Arnaldo Carvalho de Melo | f1e2701 | 2011-02-05 18:51:38 -0200 | [diff] [blame] | 2400 | { |
| 2401 | struct dso *dso = map->dso; |
Arnaldo Carvalho de Melo | f1e2701 | 2011-02-05 18:51:38 -0200 | [diff] [blame] | 2402 | struct rb_root source_line = RB_ROOT; |
Arnaldo Carvalho de Melo | f1e2701 | 2011-02-05 18:51:38 -0200 | [diff] [blame] | 2403 | |
Arnaldo Carvalho de Melo | 5449f13 | 2017-12-11 12:46:11 -0300 | [diff] [blame] | 2404 | if (symbol__annotate(sym, map, evsel, 0, NULL) < 0) |
Arnaldo Carvalho de Melo | f1e2701 | 2011-02-05 18:51:38 -0200 | [diff] [blame] | 2405 | return -1; |
| 2406 | |
Jiri Olsa | 05d3f1a | 2017-11-15 12:20:08 +0100 | [diff] [blame] | 2407 | symbol__calc_percent(sym, evsel); |
| 2408 | |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 2409 | if (print_lines) { |
Michael Petlan | 4a4c03c | 2015-11-09 16:33:31 +0100 | [diff] [blame] | 2410 | srcline_full_filename = full_paths; |
Jiri Olsa | 8b4c74d | 2017-10-11 17:01:41 +0200 | [diff] [blame] | 2411 | symbol__calc_lines(sym, map, &source_line); |
Namhyung Kim | 86c98ca | 2013-09-11 14:09:30 +0900 | [diff] [blame] | 2412 | print_summary(&source_line, dso->long_name); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 2413 | } |
| 2414 | |
Namhyung Kim | db8fd07 | 2013-03-05 14:53:21 +0900 | [diff] [blame] | 2415 | symbol__annotate_printf(sym, map, evsel, full_paths, |
Arnaldo Carvalho de Melo | d5e3d74 | 2011-02-08 15:29:25 -0200 | [diff] [blame] | 2416 | min_pcnt, max_lines, 0); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 2417 | |
Jiri Olsa | f8eb37b | 2017-10-11 17:01:38 +0200 | [diff] [blame] | 2418 | annotated_source__purge(symbol__annotation(sym)->src); |
Arnaldo Carvalho de Melo | f1e2701 | 2011-02-05 18:51:38 -0200 | [diff] [blame] | 2419 | |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 2420 | return 0; |
| 2421 | } |
Arnaldo Carvalho de Melo | f626adf | 2013-12-18 17:10:15 -0300 | [diff] [blame] | 2422 | |
Namhyung Kim | 48c65bd | 2014-02-20 10:32:53 +0900 | [diff] [blame] | 2423 | bool ui__has_annotation(void) |
| 2424 | { |
Jiri Olsa | 2e0453a | 2016-05-03 13:54:44 +0200 | [diff] [blame] | 2425 | return use_browser == 1 && perf_hpp_list.sym; |
Namhyung Kim | 48c65bd | 2014-02-20 10:32:53 +0900 | [diff] [blame] | 2426 | } |
Arnaldo Carvalho de Melo | ecda45b | 2018-03-15 16:54:11 -0300 | [diff] [blame] | 2427 | |
Arnaldo Carvalho de Melo | 2f025ea | 2018-03-15 17:04:53 -0300 | [diff] [blame] | 2428 | |
| 2429 | double annotation_line__max_percent(struct annotation_line *al, struct annotation *notes) |
| 2430 | { |
| 2431 | double percent_max = 0.0; |
| 2432 | int i; |
| 2433 | |
| 2434 | for (i = 0; i < notes->nr_events; i++) { |
| 2435 | if (al->samples[i].percent > percent_max) |
| 2436 | percent_max = al->samples[i].percent; |
| 2437 | } |
| 2438 | |
| 2439 | return percent_max; |
| 2440 | } |
| 2441 | |
Arnaldo Carvalho de Melo | a1e9b74 | 2018-03-15 19:12:39 -0300 | [diff] [blame] | 2442 | static void disasm_line__write(struct disasm_line *dl, struct annotation *notes, |
| 2443 | void *obj, char *bf, size_t size, |
| 2444 | void (*obj__printf)(void *obj, const char *fmt, ...), |
| 2445 | void (*obj__write_graph)(void *obj, int graph)) |
| 2446 | { |
| 2447 | if (dl->ins.ops && dl->ins.ops->scnprintf) { |
| 2448 | if (ins__is_jump(&dl->ins)) { |
Arnaldo Carvalho de Melo | 751b178 | 2018-03-20 17:20:43 -0300 | [diff] [blame] | 2449 | bool fwd; |
Arnaldo Carvalho de Melo | a1e9b74 | 2018-03-15 19:12:39 -0300 | [diff] [blame] | 2450 | |
Arnaldo Carvalho de Melo | 751b178 | 2018-03-20 17:20:43 -0300 | [diff] [blame] | 2451 | if (dl->ops.target.outside) |
| 2452 | goto call_like; |
| 2453 | fwd = dl->ops.target.offset > dl->al.offset; |
Arnaldo Carvalho de Melo | a1e9b74 | 2018-03-15 19:12:39 -0300 | [diff] [blame] | 2454 | obj__write_graph(obj, fwd ? DARROW_CHAR : UARROW_CHAR); |
| 2455 | obj__printf(obj, " "); |
| 2456 | } else if (ins__is_call(&dl->ins)) { |
Arnaldo Carvalho de Melo | 751b178 | 2018-03-20 17:20:43 -0300 | [diff] [blame] | 2457 | call_like: |
Arnaldo Carvalho de Melo | a1e9b74 | 2018-03-15 19:12:39 -0300 | [diff] [blame] | 2458 | obj__write_graph(obj, RARROW_CHAR); |
| 2459 | obj__printf(obj, " "); |
| 2460 | } else if (ins__is_ret(&dl->ins)) { |
| 2461 | obj__write_graph(obj, LARROW_CHAR); |
| 2462 | obj__printf(obj, " "); |
| 2463 | } else { |
| 2464 | obj__printf(obj, " "); |
| 2465 | } |
| 2466 | } else { |
| 2467 | obj__printf(obj, " "); |
| 2468 | } |
| 2469 | |
| 2470 | disasm_line__scnprintf(dl, bf, size, !notes->options->use_offset); |
| 2471 | } |
| 2472 | |
Arnaldo Carvalho de Melo | c298304 | 2018-03-15 23:14:51 -0300 | [diff] [blame] | 2473 | static void __annotation_line__write(struct annotation_line *al, struct annotation *notes, |
| 2474 | bool first_line, bool current_entry, bool change_color, int width, |
| 2475 | void *obj, |
| 2476 | int (*obj__set_color)(void *obj, int color), |
| 2477 | void (*obj__set_percent_color)(void *obj, double percent, bool current), |
| 2478 | int (*obj__set_jumps_percent_color)(void *obj, int nr, bool current), |
| 2479 | void (*obj__printf)(void *obj, const char *fmt, ...), |
| 2480 | void (*obj__write_graph)(void *obj, int graph)) |
| 2481 | |
Arnaldo Carvalho de Melo | 2ba5eca | 2018-03-15 17:54:36 -0300 | [diff] [blame] | 2482 | { |
| 2483 | double percent_max = annotation_line__max_percent(al, notes); |
Arnaldo Carvalho de Melo | a1e9b74 | 2018-03-15 19:12:39 -0300 | [diff] [blame] | 2484 | int pcnt_width = annotation__pcnt_width(notes), |
| 2485 | cycles_width = annotation__cycles_width(notes); |
Arnaldo Carvalho de Melo | 2ba5eca | 2018-03-15 17:54:36 -0300 | [diff] [blame] | 2486 | bool show_title = false; |
Arnaldo Carvalho de Melo | a1e9b74 | 2018-03-15 19:12:39 -0300 | [diff] [blame] | 2487 | char bf[256]; |
| 2488 | int printed; |
Arnaldo Carvalho de Melo | 2ba5eca | 2018-03-15 17:54:36 -0300 | [diff] [blame] | 2489 | |
| 2490 | if (first_line && (al->offset == -1 || percent_max == 0.0)) { |
| 2491 | if (notes->have_cycles) { |
| 2492 | if (al->ipc == 0.0 && al->cycles == 0) |
| 2493 | show_title = true; |
| 2494 | } else |
| 2495 | show_title = true; |
| 2496 | } |
| 2497 | |
Arnaldo Carvalho de Melo | 2ba5eca | 2018-03-15 17:54:36 -0300 | [diff] [blame] | 2498 | if (al->offset != -1 && percent_max != 0.0) { |
| 2499 | int i; |
| 2500 | |
| 2501 | for (i = 0; i < notes->nr_events; i++) { |
| 2502 | obj__set_percent_color(obj, al->samples[i].percent, current_entry); |
| 2503 | if (notes->options->show_total_period) { |
| 2504 | obj__printf(obj, "%11" PRIu64 " ", al->samples[i].he.period); |
| 2505 | } else if (notes->options->show_nr_samples) { |
| 2506 | obj__printf(obj, "%6" PRIu64 " ", |
| 2507 | al->samples[i].he.nr_samples); |
| 2508 | } else { |
| 2509 | obj__printf(obj, "%6.2f ", |
| 2510 | al->samples[i].percent); |
| 2511 | } |
| 2512 | } |
| 2513 | } else { |
Arnaldo Carvalho de Melo | 2ba5eca | 2018-03-15 17:54:36 -0300 | [diff] [blame] | 2514 | obj__set_percent_color(obj, 0, current_entry); |
| 2515 | |
| 2516 | if (!show_title) |
Arnaldo Carvalho de Melo | a1e9b74 | 2018-03-15 19:12:39 -0300 | [diff] [blame] | 2517 | obj__printf(obj, "%-*s", pcnt_width, " "); |
Arnaldo Carvalho de Melo | 2ba5eca | 2018-03-15 17:54:36 -0300 | [diff] [blame] | 2518 | else { |
Arnaldo Carvalho de Melo | a1e9b74 | 2018-03-15 19:12:39 -0300 | [diff] [blame] | 2519 | obj__printf(obj, "%-*s", pcnt_width, |
Arnaldo Carvalho de Melo | 2ba5eca | 2018-03-15 17:54:36 -0300 | [diff] [blame] | 2520 | notes->options->show_total_period ? "Period" : |
| 2521 | notes->options->show_nr_samples ? "Samples" : "Percent"); |
| 2522 | } |
| 2523 | } |
| 2524 | |
| 2525 | if (notes->have_cycles) { |
| 2526 | if (al->ipc) |
| 2527 | obj__printf(obj, "%*.2f ", ANNOTATION__IPC_WIDTH - 1, al->ipc); |
| 2528 | else if (!show_title) |
| 2529 | obj__printf(obj, "%*s", ANNOTATION__IPC_WIDTH, " "); |
| 2530 | else |
| 2531 | obj__printf(obj, "%*s ", ANNOTATION__IPC_WIDTH - 1, "IPC"); |
| 2532 | |
Jin Yao | 3e71fc0 | 2018-05-17 22:58:38 +0800 | [diff] [blame] | 2533 | if (!notes->options->show_minmax_cycle) { |
| 2534 | if (al->cycles) |
| 2535 | obj__printf(obj, "%*" PRIu64 " ", |
Arnaldo Carvalho de Melo | 2ba5eca | 2018-03-15 17:54:36 -0300 | [diff] [blame] | 2536 | ANNOTATION__CYCLES_WIDTH - 1, al->cycles); |
Jin Yao | 3e71fc0 | 2018-05-17 22:58:38 +0800 | [diff] [blame] | 2537 | else if (!show_title) |
| 2538 | obj__printf(obj, "%*s", |
| 2539 | ANNOTATION__CYCLES_WIDTH, " "); |
| 2540 | else |
| 2541 | obj__printf(obj, "%*s ", |
| 2542 | ANNOTATION__CYCLES_WIDTH - 1, |
| 2543 | "Cycle"); |
| 2544 | } else { |
| 2545 | if (al->cycles) { |
| 2546 | char str[32]; |
| 2547 | |
| 2548 | scnprintf(str, sizeof(str), |
| 2549 | "%" PRIu64 "(%" PRIu64 "/%" PRIu64 ")", |
| 2550 | al->cycles, al->cycles_min, |
| 2551 | al->cycles_max); |
| 2552 | |
| 2553 | obj__printf(obj, "%*s ", |
| 2554 | ANNOTATION__MINMAX_CYCLES_WIDTH - 1, |
| 2555 | str); |
| 2556 | } else if (!show_title) |
| 2557 | obj__printf(obj, "%*s", |
| 2558 | ANNOTATION__MINMAX_CYCLES_WIDTH, |
| 2559 | " "); |
| 2560 | else |
| 2561 | obj__printf(obj, "%*s ", |
| 2562 | ANNOTATION__MINMAX_CYCLES_WIDTH - 1, |
| 2563 | "Cycle(min/max)"); |
| 2564 | } |
Arnaldo Carvalho de Melo | 2ba5eca | 2018-03-15 17:54:36 -0300 | [diff] [blame] | 2565 | } |
| 2566 | |
| 2567 | obj__printf(obj, " "); |
Arnaldo Carvalho de Melo | a1e9b74 | 2018-03-15 19:12:39 -0300 | [diff] [blame] | 2568 | |
| 2569 | if (!*al->line) |
| 2570 | obj__printf(obj, "%-*s", width - pcnt_width - cycles_width, " "); |
| 2571 | else if (al->offset == -1) { |
| 2572 | if (al->line_nr && notes->options->show_linenr) |
| 2573 | printed = scnprintf(bf, sizeof(bf), "%-*d ", notes->widths.addr + 1, al->line_nr); |
| 2574 | else |
| 2575 | printed = scnprintf(bf, sizeof(bf), "%-*s ", notes->widths.addr, " "); |
| 2576 | obj__printf(obj, bf); |
| 2577 | obj__printf(obj, "%-*s", width - printed - pcnt_width - cycles_width + 1, al->line); |
| 2578 | } else { |
| 2579 | u64 addr = al->offset; |
| 2580 | int color = -1; |
| 2581 | |
| 2582 | if (!notes->options->use_offset) |
| 2583 | addr += notes->start; |
| 2584 | |
| 2585 | if (!notes->options->use_offset) { |
| 2586 | printed = scnprintf(bf, sizeof(bf), "%" PRIx64 ": ", addr); |
| 2587 | } else { |
Arnaldo Carvalho de Melo | 592c10e | 2018-04-11 10:30:03 -0300 | [diff] [blame] | 2588 | if (al->jump_sources && |
| 2589 | notes->options->offset_level >= ANNOTATION__OFFSET_JUMP_TARGETS) { |
Arnaldo Carvalho de Melo | a1e9b74 | 2018-03-15 19:12:39 -0300 | [diff] [blame] | 2590 | if (notes->options->show_nr_jumps) { |
| 2591 | int prev; |
| 2592 | printed = scnprintf(bf, sizeof(bf), "%*d ", |
| 2593 | notes->widths.jumps, |
| 2594 | al->jump_sources); |
| 2595 | prev = obj__set_jumps_percent_color(obj, al->jump_sources, |
| 2596 | current_entry); |
| 2597 | obj__printf(obj, bf); |
| 2598 | obj__set_color(obj, prev); |
| 2599 | } |
Arnaldo Carvalho de Melo | 592c10e | 2018-04-11 10:30:03 -0300 | [diff] [blame] | 2600 | print_addr: |
Arnaldo Carvalho de Melo | a1e9b74 | 2018-03-15 19:12:39 -0300 | [diff] [blame] | 2601 | printed = scnprintf(bf, sizeof(bf), "%*" PRIx64 ": ", |
| 2602 | notes->widths.target, addr); |
Arnaldo Carvalho de Melo | 592c10e | 2018-04-11 10:30:03 -0300 | [diff] [blame] | 2603 | } else if (ins__is_call(&disasm_line(al)->ins) && |
| 2604 | notes->options->offset_level >= ANNOTATION__OFFSET_CALL) { |
| 2605 | goto print_addr; |
| 2606 | } else if (notes->options->offset_level == ANNOTATION__MAX_OFFSET_LEVEL) { |
| 2607 | goto print_addr; |
Arnaldo Carvalho de Melo | a1e9b74 | 2018-03-15 19:12:39 -0300 | [diff] [blame] | 2608 | } else { |
| 2609 | printed = scnprintf(bf, sizeof(bf), "%-*s ", |
| 2610 | notes->widths.addr, " "); |
| 2611 | } |
| 2612 | } |
| 2613 | |
| 2614 | if (change_color) |
| 2615 | color = obj__set_color(obj, HE_COLORSET_ADDR); |
| 2616 | obj__printf(obj, bf); |
| 2617 | if (change_color) |
| 2618 | obj__set_color(obj, color); |
| 2619 | |
| 2620 | disasm_line__write(disasm_line(al), notes, obj, bf, sizeof(bf), obj__printf, obj__write_graph); |
| 2621 | |
| 2622 | obj__printf(obj, "%-*s", width - pcnt_width - cycles_width - 3 - printed, bf); |
| 2623 | } |
| 2624 | |
Arnaldo Carvalho de Melo | 2ba5eca | 2018-03-15 17:54:36 -0300 | [diff] [blame] | 2625 | } |
| 2626 | |
Arnaldo Carvalho de Melo | c298304 | 2018-03-15 23:14:51 -0300 | [diff] [blame] | 2627 | void annotation_line__write(struct annotation_line *al, struct annotation *notes, |
| 2628 | struct annotation_write_ops *ops) |
| 2629 | { |
| 2630 | __annotation_line__write(al, notes, ops->first_line, ops->current_entry, |
| 2631 | ops->change_color, ops->width, ops->obj, |
| 2632 | ops->set_color, ops->set_percent_color, |
| 2633 | ops->set_jumps_percent_color, ops->printf, |
| 2634 | ops->write_graph); |
| 2635 | } |
| 2636 | |
Arnaldo Carvalho de Melo | ecda45b | 2018-03-15 16:54:11 -0300 | [diff] [blame] | 2637 | int symbol__annotate2(struct symbol *sym, struct map *map, struct perf_evsel *evsel, |
| 2638 | struct annotation_options *options, struct arch **parch) |
| 2639 | { |
| 2640 | struct annotation *notes = symbol__annotation(sym); |
| 2641 | size_t size = symbol__size(sym); |
| 2642 | int nr_pcnt = 1, err; |
| 2643 | |
| 2644 | notes->offsets = zalloc(size * sizeof(struct annotation_line *)); |
| 2645 | if (notes->offsets == NULL) |
| 2646 | return -1; |
| 2647 | |
| 2648 | if (perf_evsel__is_group_event(evsel)) |
| 2649 | nr_pcnt = evsel->nr_members; |
| 2650 | |
| 2651 | err = symbol__annotate(sym, map, evsel, 0, parch); |
| 2652 | if (err) |
| 2653 | goto out_free_offsets; |
| 2654 | |
| 2655 | notes->options = options; |
| 2656 | |
| 2657 | symbol__calc_percent(sym, evsel); |
| 2658 | |
| 2659 | notes->start = map__rip_2objdump(map, sym->start); |
| 2660 | |
| 2661 | annotation__set_offsets(notes, size); |
| 2662 | annotation__mark_jump_targets(notes, sym); |
| 2663 | annotation__compute_ipc(notes, size); |
| 2664 | annotation__init_column_widths(notes, sym); |
| 2665 | notes->nr_events = nr_pcnt; |
| 2666 | |
| 2667 | annotation__update_column_widths(notes); |
| 2668 | |
| 2669 | return 0; |
| 2670 | |
| 2671 | out_free_offsets: |
| 2672 | zfree(¬es->offsets); |
| 2673 | return -1; |
| 2674 | } |
Arnaldo Carvalho de Melo | 7f0b6fd | 2018-03-16 14:33:38 -0300 | [diff] [blame] | 2675 | |
Arnaldo Carvalho de Melo | b213eac | 2018-04-03 15:19:47 -0300 | [diff] [blame] | 2676 | int __annotation__scnprintf_samples_period(struct annotation *notes, |
| 2677 | char *bf, size_t size, |
| 2678 | struct perf_evsel *evsel, |
| 2679 | bool show_freq) |
| 2680 | { |
| 2681 | const char *ev_name = perf_evsel__name(evsel); |
Arnaldo Carvalho de Melo | c0459a0 | 2018-04-05 11:13:24 -0300 | [diff] [blame] | 2682 | char buf[1024], ref[30] = " show reference callgraph, "; |
Arnaldo Carvalho de Melo | b213eac | 2018-04-03 15:19:47 -0300 | [diff] [blame] | 2683 | char sample_freq_str[64] = ""; |
| 2684 | unsigned long nr_samples = 0; |
| 2685 | int nr_members = 1; |
| 2686 | bool enable_ref = false; |
| 2687 | u64 nr_events = 0; |
| 2688 | char unit; |
| 2689 | int i; |
| 2690 | |
Arnaldo Carvalho de Melo | c0459a0 | 2018-04-05 11:13:24 -0300 | [diff] [blame] | 2691 | if (perf_evsel__is_group_event(evsel)) { |
| 2692 | perf_evsel__group_desc(evsel, buf, sizeof(buf)); |
| 2693 | ev_name = buf; |
Arnaldo Carvalho de Melo | b213eac | 2018-04-03 15:19:47 -0300 | [diff] [blame] | 2694 | nr_members = evsel->nr_members; |
Arnaldo Carvalho de Melo | c0459a0 | 2018-04-05 11:13:24 -0300 | [diff] [blame] | 2695 | } |
Arnaldo Carvalho de Melo | b213eac | 2018-04-03 15:19:47 -0300 | [diff] [blame] | 2696 | |
| 2697 | for (i = 0; i < nr_members; i++) { |
| 2698 | struct sym_hist *ah = annotation__histogram(notes, evsel->idx + i); |
| 2699 | |
| 2700 | nr_samples += ah->nr_samples; |
| 2701 | nr_events += ah->period; |
| 2702 | } |
| 2703 | |
| 2704 | if (symbol_conf.show_ref_callgraph && strstr(ev_name, "call-graph=no")) |
| 2705 | enable_ref = true; |
| 2706 | |
| 2707 | if (show_freq) |
| 2708 | scnprintf(sample_freq_str, sizeof(sample_freq_str), " %d Hz,", evsel->attr.sample_freq); |
| 2709 | |
| 2710 | nr_samples = convert_unit(nr_samples, &unit); |
| 2711 | return scnprintf(bf, size, "Samples: %lu%c of event%s '%s',%s%sEvent count (approx.): %" PRIu64, |
| 2712 | nr_samples, unit, evsel->nr_members > 1 ? "s" : "", |
| 2713 | ev_name, sample_freq_str, enable_ref ? ref : " ", nr_events); |
| 2714 | } |
| 2715 | |
Arnaldo Carvalho de Melo | 7f0b6fd | 2018-03-16 14:33:38 -0300 | [diff] [blame] | 2716 | #define ANNOTATION__CFG(n) \ |
| 2717 | { .name = #n, .value = &annotation__default_options.n, } |
| 2718 | |
| 2719 | /* |
| 2720 | * Keep the entries sorted, they are bsearch'ed |
| 2721 | */ |
| 2722 | static struct annotation_config { |
| 2723 | const char *name; |
Arnaldo Carvalho de Melo | 43c4023 | 2018-04-12 15:23:02 -0300 | [diff] [blame] | 2724 | void *value; |
Arnaldo Carvalho de Melo | 7f0b6fd | 2018-03-16 14:33:38 -0300 | [diff] [blame] | 2725 | } annotation__configs[] = { |
| 2726 | ANNOTATION__CFG(hide_src_code), |
| 2727 | ANNOTATION__CFG(jump_arrows), |
Arnaldo Carvalho de Melo | 43c4023 | 2018-04-12 15:23:02 -0300 | [diff] [blame] | 2728 | ANNOTATION__CFG(offset_level), |
Arnaldo Carvalho de Melo | 7f0b6fd | 2018-03-16 14:33:38 -0300 | [diff] [blame] | 2729 | ANNOTATION__CFG(show_linenr), |
| 2730 | ANNOTATION__CFG(show_nr_jumps), |
| 2731 | ANNOTATION__CFG(show_nr_samples), |
| 2732 | ANNOTATION__CFG(show_total_period), |
| 2733 | ANNOTATION__CFG(use_offset), |
| 2734 | }; |
| 2735 | |
| 2736 | #undef ANNOTATION__CFG |
| 2737 | |
| 2738 | static int annotation_config__cmp(const void *name, const void *cfgp) |
| 2739 | { |
| 2740 | const struct annotation_config *cfg = cfgp; |
| 2741 | |
| 2742 | return strcmp(name, cfg->name); |
| 2743 | } |
| 2744 | |
| 2745 | static int annotation__config(const char *var, const char *value, |
| 2746 | void *data __maybe_unused) |
| 2747 | { |
| 2748 | struct annotation_config *cfg; |
| 2749 | const char *name; |
| 2750 | |
| 2751 | if (!strstarts(var, "annotate.")) |
| 2752 | return 0; |
| 2753 | |
| 2754 | name = var + 9; |
| 2755 | cfg = bsearch(name, annotation__configs, ARRAY_SIZE(annotation__configs), |
| 2756 | sizeof(struct annotation_config), annotation_config__cmp); |
| 2757 | |
| 2758 | if (cfg == NULL) |
| 2759 | pr_debug("%s variable unknown, ignoring...", var); |
Arnaldo Carvalho de Melo | 43c4023 | 2018-04-12 15:23:02 -0300 | [diff] [blame] | 2760 | else if (strcmp(var, "annotate.offset_level") == 0) { |
| 2761 | perf_config_int(cfg->value, name, value); |
| 2762 | |
| 2763 | if (*(int *)cfg->value > ANNOTATION__MAX_OFFSET_LEVEL) |
| 2764 | *(int *)cfg->value = ANNOTATION__MAX_OFFSET_LEVEL; |
| 2765 | else if (*(int *)cfg->value < ANNOTATION__MIN_OFFSET_LEVEL) |
| 2766 | *(int *)cfg->value = ANNOTATION__MIN_OFFSET_LEVEL; |
| 2767 | } else { |
| 2768 | *(bool *)cfg->value = perf_config_bool(name, value); |
| 2769 | } |
Arnaldo Carvalho de Melo | 7f0b6fd | 2018-03-16 14:33:38 -0300 | [diff] [blame] | 2770 | return 0; |
| 2771 | } |
| 2772 | |
| 2773 | void annotation_config__init(void) |
| 2774 | { |
| 2775 | perf_config(annotation__config, NULL); |
| 2776 | |
| 2777 | annotation__default_options.show_total_period = symbol_conf.show_total_period; |
| 2778 | annotation__default_options.show_nr_samples = symbol_conf.show_nr_samples; |
| 2779 | } |