blob: be1caabb92906befc755d1b99e22cb6a0439dc34 [file] [log] [blame]
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001/*
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 Meloa43783a2017-04-18 10:46:11 -030010#include <errno.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -030011#include <inttypes.h>
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -020012#include "util.h"
Namhyung Kim48c65bd2014-02-20 10:32:53 +090013#include "ui/ui.h"
14#include "sort.h"
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -020015#include "build-id.h"
16#include "color.h"
17#include "cache.h"
18#include "symbol.h"
19#include "debug.h"
20#include "annotate.h"
Namhyung Kimdb8fd072013-03-05 14:53:21 +090021#include "evsel.h"
Peter Zijlstra70fbe052016-09-05 16:08:12 -030022#include "block-range.h"
Arnaldo Carvalho de Meloa0675582017-04-17 16:51:59 -030023#include "string2.h"
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -030024#include "arch/common.h"
Andi Kleene5924882014-11-12 18:05:26 -080025#include <regex.h>
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -020026#include <pthread.h>
Jiri Olsa4383db82012-10-27 23:18:29 +020027#include <linux/bitops.h>
Arnaldo Carvalho de Melo877a7a12017-04-17 11:39:06 -030028#include <linux/kernel.h>
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -030029#include <sys/utsname.h>
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -020030
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030031#include "sane_ctype.h"
32
Andi Kleenf69b64f2011-09-15 14:31:41 -070033const char *disassembler_style;
Maciek Borzecki7a4ec932012-09-04 12:32:30 +020034const char *objdump_path;
Andi Kleene5924882014-11-12 18:05:26 -080035static regex_t file_lineno;
Andi Kleenf69b64f2011-09-15 14:31:41 -070036
Arnaldo Carvalho de Melo75b49202016-11-24 11:16:06 -030037static struct ins_ops *ins__find(struct arch *arch, const char *name);
Arnaldo Carvalho de Melo2a1ff812016-11-24 11:37:08 -030038static void ins__sort(struct arch *arch);
Arnaldo Carvalho de Melo75b49202016-11-24 11:16:06 -030039static int disasm_line__parse(char *line, const char **namep, char **rawp);
Arnaldo Carvalho de Melo7a997fe2012-05-12 13:15:34 -030040
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -030041struct arch {
42 const char *name;
Arnaldo Carvalho de Melo763d8962016-11-17 12:31:51 -030043 struct ins *instructions;
44 size_t nr_instructions;
Arnaldo Carvalho de Melo2a1ff812016-11-24 11:37:08 -030045 size_t nr_instructions_allocated;
46 struct ins_ops *(*associate_instruction_ops)(struct arch *arch, const char *name);
Arnaldo Carvalho de Melo763d8962016-11-17 12:31:51 -030047 bool sorted_instructions;
Arnaldo Carvalho de Melo0781ea92016-11-18 12:34:26 -030048 bool initialized;
49 void *priv;
50 int (*init)(struct arch *arch);
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -030051 struct {
52 char comment_char;
Arnaldo Carvalho de Melo9c2fb452016-11-16 15:50:38 -030053 char skip_functions_char;
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -030054 } objdump;
55};
56
Arnaldo Carvalho de Melo763d8962016-11-17 12:31:51 -030057static struct ins_ops call_ops;
58static struct ins_ops dec_ops;
59static struct ins_ops jump_ops;
60static struct ins_ops mov_ops;
61static struct ins_ops nop_ops;
62static struct ins_ops lock_ops;
63static struct ins_ops ret_ops;
64
Arnaldo Carvalho de Melo2a1ff812016-11-24 11:37:08 -030065static int arch__grow_instructions(struct arch *arch)
66{
67 struct ins *new_instructions;
68 size_t new_nr_allocated;
69
70 if (arch->nr_instructions_allocated == 0 && arch->instructions)
71 goto grow_from_non_allocated_table;
72
73 new_nr_allocated = arch->nr_instructions_allocated + 128;
74 new_instructions = realloc(arch->instructions, new_nr_allocated * sizeof(struct ins));
75 if (new_instructions == NULL)
76 return -1;
77
78out_update_instructions:
79 arch->instructions = new_instructions;
80 arch->nr_instructions_allocated = new_nr_allocated;
81 return 0;
82
83grow_from_non_allocated_table:
84 new_nr_allocated = arch->nr_instructions + 128;
85 new_instructions = calloc(new_nr_allocated, sizeof(struct ins));
86 if (new_instructions == NULL)
87 return -1;
88
89 memcpy(new_instructions, arch->instructions, arch->nr_instructions);
90 goto out_update_instructions;
91}
92
Arnaldo Carvalho de Meloacc9bfb2016-11-18 16:54:10 -030093static int arch__associate_ins_ops(struct arch* arch, const char *name, struct ins_ops *ops)
Arnaldo Carvalho de Melo2a1ff812016-11-24 11:37:08 -030094{
95 struct ins *ins;
96
97 if (arch->nr_instructions == arch->nr_instructions_allocated &&
98 arch__grow_instructions(arch))
99 return -1;
100
101 ins = &arch->instructions[arch->nr_instructions];
102 ins->name = strdup(name);
103 if (!ins->name)
104 return -1;
105
106 ins->ops = ops;
107 arch->nr_instructions++;
108
109 ins__sort(arch);
110 return 0;
111}
112
Arnaldo Carvalho de Melo763d8962016-11-17 12:31:51 -0300113#include "arch/arm/annotate/instructions.c"
Kim Phillips0fcb1da2016-11-30 09:23:44 -0600114#include "arch/arm64/annotate/instructions.c"
Arnaldo Carvalho de Melo763d8962016-11-17 12:31:51 -0300115#include "arch/x86/annotate/instructions.c"
Ravi Bangoriadbdebdc2016-11-23 21:33:46 +0530116#include "arch/powerpc/annotate/instructions.c"
Christian Borntraegerd9f8dfa2017-04-06 09:51:52 +0200117#include "arch/s390/annotate/instructions.c"
Arnaldo Carvalho de Melo763d8962016-11-17 12:31:51 -0300118
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -0300119static struct arch architectures[] = {
120 {
121 .name = "arm",
Arnaldo Carvalho de Meloacc9bfb2016-11-18 16:54:10 -0300122 .init = arm__annotate_init,
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -0300123 },
124 {
Kim Phillips0fcb1da2016-11-30 09:23:44 -0600125 .name = "arm64",
126 .init = arm64__annotate_init,
127 },
128 {
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -0300129 .name = "x86",
Arnaldo Carvalho de Melo763d8962016-11-17 12:31:51 -0300130 .instructions = x86__instructions,
131 .nr_instructions = ARRAY_SIZE(x86__instructions),
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -0300132 .objdump = {
133 .comment_char = '#',
134 },
135 },
Ravi Bangoriadbdebdc2016-11-23 21:33:46 +0530136 {
137 .name = "powerpc",
138 .init = powerpc__annotate_init,
139 },
Christian Borntraegere77852b2017-04-06 09:51:51 +0200140 {
141 .name = "s390",
Christian Borntraegerd9f8dfa2017-04-06 09:51:52 +0200142 .init = s390__annotate_init,
Christian Borntraegere77852b2017-04-06 09:51:51 +0200143 .objdump = {
144 .comment_char = '#',
145 },
146 },
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -0300147};
148
Arnaldo Carvalho de Meloc46219ac2012-05-12 13:26:20 -0300149static void ins__delete(struct ins_operands *ops)
150{
Arnaldo Carvalho de Melo39956142015-03-05 15:27:28 -0300151 if (ops == NULL)
152 return;
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300153 zfree(&ops->source.raw);
154 zfree(&ops->source.name);
155 zfree(&ops->target.raw);
156 zfree(&ops->target.name);
Arnaldo Carvalho de Meloc46219ac2012-05-12 13:26:20 -0300157}
158
Arnaldo Carvalho de Melo54170722012-05-07 18:54:16 -0300159static int ins__raw_scnprintf(struct ins *ins, char *bf, size_t size,
160 struct ins_operands *ops)
161{
162 return scnprintf(bf, size, "%-6.6s %s", ins->name, ops->raw);
163}
164
165int ins__scnprintf(struct ins *ins, char *bf, size_t size,
166 struct ins_operands *ops)
167{
168 if (ins->ops->scnprintf)
169 return ins->ops->scnprintf(ins, bf, size, ops);
170
171 return ins__raw_scnprintf(ins, bf, size, ops);
172}
173
Arnaldo Carvalho de Melo9c2fb452016-11-16 15:50:38 -0300174static int call__parse(struct arch *arch, struct ins_operands *ops, struct map *map)
Arnaldo Carvalho de Melod86b0592012-04-18 16:07:38 -0300175{
Arnaldo Carvalho de Melod2232882012-04-20 15:26:47 -0300176 char *endptr, *tok, *name;
177
Arnaldo Carvalho de Melo44d1a3e2012-04-25 08:00:23 -0300178 ops->target.addr = strtoull(ops->raw, &endptr, 16);
Arnaldo Carvalho de Melod2232882012-04-20 15:26:47 -0300179
180 name = strchr(endptr, '<');
181 if (name == NULL)
182 goto indirect_call;
183
184 name++;
185
Arnaldo Carvalho de Melo9c2fb452016-11-16 15:50:38 -0300186 if (arch->objdump.skip_functions_char &&
187 strchr(name, arch->objdump.skip_functions_char))
Russell Kingcfef25b2015-12-06 23:07:13 +0000188 return -1;
Russell Kingcfef25b2015-12-06 23:07:13 +0000189
Arnaldo Carvalho de Melod2232882012-04-20 15:26:47 -0300190 tok = strchr(name, '>');
191 if (tok == NULL)
192 return -1;
193
194 *tok = '\0';
Arnaldo Carvalho de Melo44d1a3e2012-04-25 08:00:23 -0300195 ops->target.name = strdup(name);
Arnaldo Carvalho de Melod2232882012-04-20 15:26:47 -0300196 *tok = '>';
197
Arnaldo Carvalho de Melo44d1a3e2012-04-25 08:00:23 -0300198 return ops->target.name == NULL ? -1 : 0;
Arnaldo Carvalho de Melod2232882012-04-20 15:26:47 -0300199
200indirect_call:
Ravi Bangoria88a7fcf2016-08-19 18:29:35 +0530201 tok = strchr(endptr, '*');
202 if (tok == NULL) {
Arnaldo Carvalho de Melo5f62d4f2016-09-19 17:26:11 -0300203 struct symbol *sym = map__find_symbol(map, map->map_ip(map, ops->target.addr));
204 if (sym != NULL)
205 ops->target.name = strdup(sym->name);
206 else
207 ops->target.addr = 0;
Arnaldo Carvalho de Meloe8ea1562012-05-11 12:28:55 -0300208 return 0;
209 }
210
Arnaldo Carvalho de Melo44d1a3e2012-04-25 08:00:23 -0300211 ops->target.addr = strtoull(tok + 1, NULL, 16);
Arnaldo Carvalho de Melod86b0592012-04-18 16:07:38 -0300212 return 0;
213}
214
Arnaldo Carvalho de Melod2232882012-04-20 15:26:47 -0300215static int call__scnprintf(struct ins *ins, char *bf, size_t size,
Arnaldo Carvalho de Melo54170722012-05-07 18:54:16 -0300216 struct ins_operands *ops)
Arnaldo Carvalho de Melod2232882012-04-20 15:26:47 -0300217{
Arnaldo Carvalho de Melo44d1a3e2012-04-25 08:00:23 -0300218 if (ops->target.name)
219 return scnprintf(bf, size, "%-6.6s %s", ins->name, ops->target.name);
Arnaldo Carvalho de Melod2232882012-04-20 15:26:47 -0300220
Arnaldo Carvalho de Meloe8ea1562012-05-11 12:28:55 -0300221 if (ops->target.addr == 0)
222 return ins__raw_scnprintf(ins, bf, size, ops);
223
Arnaldo Carvalho de Melo44d1a3e2012-04-25 08:00:23 -0300224 return scnprintf(bf, size, "%-6.6s *%" PRIx64, ins->name, ops->target.addr);
Arnaldo Carvalho de Melod2232882012-04-20 15:26:47 -0300225}
226
Arnaldo Carvalho de Melod86b0592012-04-18 16:07:38 -0300227static struct ins_ops call_ops = {
Arnaldo Carvalho de Melod2232882012-04-20 15:26:47 -0300228 .parse = call__parse,
229 .scnprintf = call__scnprintf,
Arnaldo Carvalho de Melod86b0592012-04-18 16:07:38 -0300230};
231
232bool ins__is_call(const struct ins *ins)
233{
234 return ins->ops == &call_ops;
235}
236
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -0300237static int jump__parse(struct arch *arch __maybe_unused, struct ins_operands *ops, struct map *map __maybe_unused)
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -0300238{
Arnaldo Carvalho de Meloc7e6ead2012-04-20 14:38:46 -0300239 const char *s = strchr(ops->raw, '+');
Ravi Bangoria3ee2eb62016-12-05 21:26:46 +0530240 const char *c = strchr(ops->raw, ',');
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -0300241
Kim Phillipsb13bbee2017-06-01 09:29:59 -0500242 /*
243 * skip over possible up to 2 operands to get to address, e.g.:
244 * tbnz w0, #26, ffff0000083cd190 <security_file_permission+0xd0>
245 */
246 if (c++ != NULL) {
Ravi Bangoria3ee2eb62016-12-05 21:26:46 +0530247 ops->target.addr = strtoull(c, NULL, 16);
Kim Phillipsb13bbee2017-06-01 09:29:59 -0500248 if (!ops->target.addr) {
249 c = strchr(c, ',');
250 if (c++ != NULL)
251 ops->target.addr = strtoull(c, NULL, 16);
252 }
253 } else {
Ravi Bangoria3ee2eb62016-12-05 21:26:46 +0530254 ops->target.addr = strtoull(ops->raw, NULL, 16);
Kim Phillipsb13bbee2017-06-01 09:29:59 -0500255 }
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -0300256
Ravi Bangoriae2168742016-12-05 21:26:47 +0530257 if (s++ != NULL) {
Adrian Hunterbbb7f842013-08-07 14:38:54 +0300258 ops->target.offset = strtoull(s, NULL, 16);
Ravi Bangoriae2168742016-12-05 21:26:47 +0530259 ops->target.offset_avail = true;
260 } else {
261 ops->target.offset_avail = false;
262 }
Arnaldo Carvalho de Melofb29fa582012-04-25 14:16:03 -0300263
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -0300264 return 0;
265}
266
Arnaldo Carvalho de Meloc7e6ead2012-04-20 14:38:46 -0300267static int jump__scnprintf(struct ins *ins, char *bf, size_t size,
Arnaldo Carvalho de Melo54170722012-05-07 18:54:16 -0300268 struct ins_operands *ops)
Arnaldo Carvalho de Melo28548d72012-04-19 10:16:27 -0300269{
Kim Phillipsb13bbee2017-06-01 09:29:59 -0500270 const char *c = strchr(ops->raw, ',');
271
Ravi Bangoriae2168742016-12-05 21:26:47 +0530272 if (!ops->target.addr || ops->target.offset < 0)
Ravi Bangoriabec60e52016-12-05 21:26:45 +0530273 return ins__raw_scnprintf(ins, bf, size, ops);
274
Kim Phillipsb13bbee2017-06-01 09:29:59 -0500275 if (c != NULL) {
276 const char *c2 = strchr(c + 1, ',');
277
278 /* check for 3-op insn */
279 if (c2 != NULL)
280 c = c2;
281 c++;
282
283 /* mirror arch objdump's space-after-comma style */
284 if (*c == ' ')
285 c++;
286 }
287
288 return scnprintf(bf, size, "%-6.6s %.*s%" PRIx64,
289 ins->name, c ? c - ops->raw : 0, ops->raw,
290 ops->target.offset);
Arnaldo Carvalho de Melo28548d72012-04-19 10:16:27 -0300291}
292
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -0300293static struct ins_ops jump_ops = {
Arnaldo Carvalho de Meloc7e6ead2012-04-20 14:38:46 -0300294 .parse = jump__parse,
295 .scnprintf = jump__scnprintf,
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -0300296};
297
298bool ins__is_jump(const struct ins *ins)
299{
300 return ins->ops == &jump_ops;
301}
302
Arnaldo Carvalho de Melo6de783b2012-05-11 16:48:49 -0300303static int comment__symbol(char *raw, char *comment, u64 *addrp, char **namep)
304{
305 char *endptr, *name, *t;
306
307 if (strstr(raw, "(%rip)") == NULL)
308 return 0;
309
310 *addrp = strtoull(comment, &endptr, 16);
311 name = strchr(endptr, '<');
312 if (name == NULL)
313 return -1;
314
315 name++;
316
317 t = strchr(name, '>');
318 if (t == NULL)
319 return 0;
320
321 *t = '\0';
322 *namep = strdup(name);
323 *t = '>';
324
325 return 0;
326}
327
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -0300328static int lock__parse(struct arch *arch, struct ins_operands *ops, struct map *map)
Arnaldo Carvalho de Melo7a997fe2012-05-12 13:15:34 -0300329{
Arnaldo Carvalho de Melo7a997fe2012-05-12 13:15:34 -0300330 ops->locked.ops = zalloc(sizeof(*ops->locked.ops));
331 if (ops->locked.ops == NULL)
332 return 0;
333
Arnaldo Carvalho de Melo75b49202016-11-24 11:16:06 -0300334 if (disasm_line__parse(ops->raw, &ops->locked.ins.name, &ops->locked.ops->raw) < 0)
Arnaldo Carvalho de Melo7a997fe2012-05-12 13:15:34 -0300335 goto out_free_ops;
336
Arnaldo Carvalho de Melo75b49202016-11-24 11:16:06 -0300337 ops->locked.ins.ops = ins__find(arch, ops->locked.ins.name);
Rabin Vincent0fb9f2a2015-01-18 20:00:21 +0100338
Arnaldo Carvalho de Melo75b49202016-11-24 11:16:06 -0300339 if (ops->locked.ins.ops == NULL)
Namhyung Kim2ba34aa2012-11-10 02:27:13 +0900340 goto out_free_ops;
Arnaldo Carvalho de Melo7a997fe2012-05-12 13:15:34 -0300341
Arnaldo Carvalho de Melo75b49202016-11-24 11:16:06 -0300342 if (ops->locked.ins.ops->parse &&
343 ops->locked.ins.ops->parse(arch, ops->locked.ops, map) < 0)
Rabin Vincentbe819082015-01-18 20:00:20 +0100344 goto out_free_ops;
Arnaldo Carvalho de Melo7a997fe2012-05-12 13:15:34 -0300345
346 return 0;
347
348out_free_ops:
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300349 zfree(&ops->locked.ops);
Arnaldo Carvalho de Melo7a997fe2012-05-12 13:15:34 -0300350 return 0;
351}
352
353static int lock__scnprintf(struct ins *ins, char *bf, size_t size,
354 struct ins_operands *ops)
355{
356 int printed;
357
Arnaldo Carvalho de Melo75b49202016-11-24 11:16:06 -0300358 if (ops->locked.ins.ops == NULL)
Arnaldo Carvalho de Melo7a997fe2012-05-12 13:15:34 -0300359 return ins__raw_scnprintf(ins, bf, size, ops);
360
361 printed = scnprintf(bf, size, "%-6.6s ", ins->name);
Arnaldo Carvalho de Melo75b49202016-11-24 11:16:06 -0300362 return printed + ins__scnprintf(&ops->locked.ins, bf + printed,
Arnaldo Carvalho de Melo7a997fe2012-05-12 13:15:34 -0300363 size - printed, ops->locked.ops);
364}
365
Arnaldo Carvalho de Meloc46219ac2012-05-12 13:26:20 -0300366static void lock__delete(struct ins_operands *ops)
367{
Arnaldo Carvalho de Melo75b49202016-11-24 11:16:06 -0300368 struct ins *ins = &ops->locked.ins;
Rabin Vincent0fb9f2a2015-01-18 20:00:21 +0100369
Arnaldo Carvalho de Melo75b49202016-11-24 11:16:06 -0300370 if (ins->ops && ins->ops->free)
Rabin Vincent0fb9f2a2015-01-18 20:00:21 +0100371 ins->ops->free(ops->locked.ops);
372 else
373 ins__delete(ops->locked.ops);
374
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300375 zfree(&ops->locked.ops);
376 zfree(&ops->target.raw);
377 zfree(&ops->target.name);
Arnaldo Carvalho de Meloc46219ac2012-05-12 13:26:20 -0300378}
379
Arnaldo Carvalho de Melo7a997fe2012-05-12 13:15:34 -0300380static struct ins_ops lock_ops = {
Arnaldo Carvalho de Meloc46219ac2012-05-12 13:26:20 -0300381 .free = lock__delete,
Arnaldo Carvalho de Melo7a997fe2012-05-12 13:15:34 -0300382 .parse = lock__parse,
383 .scnprintf = lock__scnprintf,
384};
385
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -0300386static int mov__parse(struct arch *arch, struct ins_operands *ops, struct map *map __maybe_unused)
Arnaldo Carvalho de Melo6de783b2012-05-11 16:48:49 -0300387{
388 char *s = strchr(ops->raw, ','), *target, *comment, prev;
389
390 if (s == NULL)
391 return -1;
392
393 *s = '\0';
394 ops->source.raw = strdup(ops->raw);
395 *s = ',';
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -0300396
Arnaldo Carvalho de Melo6de783b2012-05-11 16:48:49 -0300397 if (ops->source.raw == NULL)
398 return -1;
399
400 target = ++s;
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -0300401 comment = strchr(s, arch->objdump.comment_char);
Arnaldo Carvalho de Melo6de783b2012-05-11 16:48:49 -0300402
Alex Converse1e2bb042014-08-14 14:03:00 -0700403 if (comment != NULL)
404 s = comment - 1;
405 else
406 s = strchr(s, '\0') - 1;
407
408 while (s > target && isspace(s[0]))
409 --s;
410 s++;
Arnaldo Carvalho de Melo6de783b2012-05-11 16:48:49 -0300411 prev = *s;
412 *s = '\0';
413
414 ops->target.raw = strdup(target);
415 *s = prev;
416
417 if (ops->target.raw == NULL)
418 goto out_free_source;
419
Arnaldo Carvalho de Melo6de783b2012-05-11 16:48:49 -0300420 if (comment == NULL)
421 return 0;
422
Taeung Song4597cf02017-04-08 09:52:24 +0900423 comment = ltrim(comment);
Arnaldo Carvalho de Melo6de783b2012-05-11 16:48:49 -0300424 comment__symbol(ops->source.raw, comment, &ops->source.addr, &ops->source.name);
425 comment__symbol(ops->target.raw, comment, &ops->target.addr, &ops->target.name);
426
427 return 0;
428
429out_free_source:
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300430 zfree(&ops->source.raw);
Arnaldo Carvalho de Melo6de783b2012-05-11 16:48:49 -0300431 return -1;
432}
433
434static int mov__scnprintf(struct ins *ins, char *bf, size_t size,
435 struct ins_operands *ops)
436{
437 return scnprintf(bf, size, "%-6.6s %s,%s", ins->name,
438 ops->source.name ?: ops->source.raw,
439 ops->target.name ?: ops->target.raw);
440}
441
442static struct ins_ops mov_ops = {
443 .parse = mov__parse,
444 .scnprintf = mov__scnprintf,
445};
446
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -0300447static int dec__parse(struct arch *arch __maybe_unused, struct ins_operands *ops, struct map *map __maybe_unused)
Arnaldo Carvalho de Meloa43712c2012-05-11 17:21:09 -0300448{
449 char *target, *comment, *s, prev;
450
451 target = s = ops->raw;
452
453 while (s[0] != '\0' && !isspace(s[0]))
454 ++s;
455 prev = *s;
456 *s = '\0';
457
458 ops->target.raw = strdup(target);
459 *s = prev;
460
461 if (ops->target.raw == NULL)
462 return -1;
463
Kim Phillips859afa62016-11-30 09:23:33 -0600464 comment = strchr(s, arch->objdump.comment_char);
Arnaldo Carvalho de Meloa43712c2012-05-11 17:21:09 -0300465 if (comment == NULL)
466 return 0;
467
Taeung Song4597cf02017-04-08 09:52:24 +0900468 comment = ltrim(comment);
Arnaldo Carvalho de Meloa43712c2012-05-11 17:21:09 -0300469 comment__symbol(ops->target.raw, comment, &ops->target.addr, &ops->target.name);
470
471 return 0;
472}
473
474static int dec__scnprintf(struct ins *ins, char *bf, size_t size,
475 struct ins_operands *ops)
476{
477 return scnprintf(bf, size, "%-6.6s %s", ins->name,
478 ops->target.name ?: ops->target.raw);
479}
480
481static struct ins_ops dec_ops = {
482 .parse = dec__parse,
483 .scnprintf = dec__scnprintf,
484};
485
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300486static int nop__scnprintf(struct ins *ins __maybe_unused, char *bf, size_t size,
487 struct ins_operands *ops __maybe_unused)
Arnaldo Carvalho de Melob9818e92012-05-07 18:57:02 -0300488{
489 return scnprintf(bf, size, "%-6.6s", "nop");
490}
491
492static struct ins_ops nop_ops = {
493 .scnprintf = nop__scnprintf,
494};
495
Naveen N. Rao6ef94922016-06-24 17:23:58 +0530496static struct ins_ops ret_ops = {
497 .scnprintf = ins__raw_scnprintf,
498};
499
500bool ins__is_ret(const struct ins *ins)
501{
502 return ins->ops == &ret_ops;
503}
504
Chris Ryder7e4c1492016-05-19 17:59:46 +0100505static int ins__key_cmp(const void *name, const void *insp)
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -0300506{
507 const struct ins *ins = insp;
508
509 return strcmp(name, ins->name);
510}
511
Chris Ryder7e4c1492016-05-19 17:59:46 +0100512static int ins__cmp(const void *a, const void *b)
513{
514 const struct ins *ia = a;
515 const struct ins *ib = b;
516
517 return strcmp(ia->name, ib->name);
518}
519
Arnaldo Carvalho de Melo763d8962016-11-17 12:31:51 -0300520static void ins__sort(struct arch *arch)
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -0300521{
Arnaldo Carvalho de Melo763d8962016-11-17 12:31:51 -0300522 const int nmemb = arch->nr_instructions;
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -0300523
Arnaldo Carvalho de Melo763d8962016-11-17 12:31:51 -0300524 qsort(arch->instructions, nmemb, sizeof(struct ins), ins__cmp);
Chris Ryder7e4c1492016-05-19 17:59:46 +0100525}
526
Arnaldo Carvalho de Melo2a1ff812016-11-24 11:37:08 -0300527static struct ins_ops *__ins__find(struct arch *arch, const char *name)
Chris Ryder7e4c1492016-05-19 17:59:46 +0100528{
Arnaldo Carvalho de Melo75b49202016-11-24 11:16:06 -0300529 struct ins *ins;
Arnaldo Carvalho de Melo763d8962016-11-17 12:31:51 -0300530 const int nmemb = arch->nr_instructions;
Chris Ryder7e4c1492016-05-19 17:59:46 +0100531
Arnaldo Carvalho de Melo763d8962016-11-17 12:31:51 -0300532 if (!arch->sorted_instructions) {
533 ins__sort(arch);
534 arch->sorted_instructions = true;
Chris Ryder7e4c1492016-05-19 17:59:46 +0100535 }
536
Arnaldo Carvalho de Melo75b49202016-11-24 11:16:06 -0300537 ins = bsearch(name, arch->instructions, nmemb, sizeof(struct ins), ins__key_cmp);
538 return ins ? ins->ops : NULL;
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -0300539}
540
Arnaldo Carvalho de Melo2a1ff812016-11-24 11:37:08 -0300541static struct ins_ops *ins__find(struct arch *arch, const char *name)
542{
543 struct ins_ops *ops = __ins__find(arch, name);
544
545 if (!ops && arch->associate_instruction_ops)
546 ops = arch->associate_instruction_ops(arch, name);
547
548 return ops;
549}
550
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -0300551static int arch__key_cmp(const void *name, const void *archp)
552{
553 const struct arch *arch = archp;
554
555 return strcmp(name, arch->name);
556}
557
558static int arch__cmp(const void *a, const void *b)
559{
560 const struct arch *aa = a;
561 const struct arch *ab = b;
562
563 return strcmp(aa->name, ab->name);
564}
565
566static void arch__sort(void)
567{
568 const int nmemb = ARRAY_SIZE(architectures);
569
570 qsort(architectures, nmemb, sizeof(struct arch), arch__cmp);
571}
572
573static struct arch *arch__find(const char *name)
574{
575 const int nmemb = ARRAY_SIZE(architectures);
576 static bool sorted;
577
578 if (!sorted) {
579 arch__sort();
580 sorted = true;
581 }
582
583 return bsearch(name, architectures, nmemb, sizeof(struct arch), arch__key_cmp);
584}
585
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -0200586int symbol__alloc_hist(struct symbol *sym)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200587{
588 struct annotation *notes = symbol__annotation(sym);
Arnaldo Carvalho de Melo1b2e2df2012-04-19 10:57:06 -0300589 const size_t size = symbol__size(sym);
Cody Schafer86963292012-07-19 20:05:25 -0700590 size_t sizeof_sym_hist;
591
592 /* Check for overflow when calculating sizeof_sym_hist */
593 if (size > (SIZE_MAX - sizeof(struct sym_hist)) / sizeof(u64))
594 return -1;
595
596 sizeof_sym_hist = (sizeof(struct sym_hist) + size * sizeof(u64));
597
598 /* Check for overflow in zalloc argument */
599 if (sizeof_sym_hist > (SIZE_MAX - sizeof(*notes->src))
600 / symbol_conf.nr_events)
601 return -1;
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200602
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -0200603 notes->src = zalloc(sizeof(*notes->src) + symbol_conf.nr_events * sizeof_sym_hist);
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200604 if (notes->src == NULL)
605 return -1;
606 notes->src->sizeof_sym_hist = sizeof_sym_hist;
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -0200607 notes->src->nr_histograms = symbol_conf.nr_events;
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200608 INIT_LIST_HEAD(&notes->src->source);
609 return 0;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200610}
611
Andi Kleend4957632015-07-18 08:24:48 -0700612/* The cycles histogram is lazily allocated. */
613static int symbol__alloc_hist_cycles(struct symbol *sym)
614{
615 struct annotation *notes = symbol__annotation(sym);
616 const size_t size = symbol__size(sym);
617
618 notes->src->cycles_hist = calloc(size, sizeof(struct cyc_hist));
619 if (notes->src->cycles_hist == NULL)
620 return -1;
621 return 0;
622}
623
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200624void symbol__annotate_zero_histograms(struct symbol *sym)
625{
626 struct annotation *notes = symbol__annotation(sym);
627
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200628 pthread_mutex_lock(&notes->lock);
Andi Kleend4957632015-07-18 08:24:48 -0700629 if (notes->src != NULL) {
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200630 memset(notes->src->histograms, 0,
631 notes->src->nr_histograms * notes->src->sizeof_sym_hist);
Andi Kleend4957632015-07-18 08:24:48 -0700632 if (notes->src->cycles_hist)
633 memset(notes->src->cycles_hist, 0,
634 symbol__size(sym) * sizeof(struct cyc_hist));
635 }
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200636 pthread_mutex_unlock(&notes->lock);
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200637}
638
Andi Kleend4957632015-07-18 08:24:48 -0700639static int __symbol__account_cycles(struct annotation *notes,
640 u64 start,
641 unsigned offset, unsigned cycles,
642 unsigned have_start)
643{
644 struct cyc_hist *ch;
645
646 ch = notes->src->cycles_hist;
647 /*
648 * For now we can only account one basic block per
649 * final jump. But multiple could be overlapping.
650 * Always account the longest one. So when
651 * a shorter one has been already seen throw it away.
652 *
653 * We separately always account the full cycles.
654 */
655 ch[offset].num_aggr++;
656 ch[offset].cycles_aggr += cycles;
657
658 if (!have_start && ch[offset].have_start)
659 return 0;
660 if (ch[offset].num) {
661 if (have_start && (!ch[offset].have_start ||
662 ch[offset].start > start)) {
663 ch[offset].have_start = 0;
664 ch[offset].cycles = 0;
665 ch[offset].num = 0;
666 if (ch[offset].reset < 0xffff)
667 ch[offset].reset++;
668 } else if (have_start &&
669 ch[offset].start < start)
670 return 0;
671 }
672 ch[offset].have_start = have_start;
673 ch[offset].start = start;
674 ch[offset].cycles += cycles;
675 ch[offset].num++;
676 return 0;
677}
678
Arnaldo Carvalho de Melob66d8c02013-12-18 15:37:41 -0300679static int __symbol__inc_addr_samples(struct symbol *sym, struct map *map,
680 struct annotation *notes, int evidx, u64 addr)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200681{
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200682 unsigned offset;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200683 struct sym_hist *h;
684
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200685 pr_debug3("%s: addr=%#" PRIx64 "\n", __func__, map->unmap_ip(map, addr));
686
Ravi Bangoriaedee44b2016-11-22 14:10:50 +0530687 if ((addr < sym->start || addr >= sym->end) &&
688 (addr != sym->end || sym->start != sym->end)) {
Arnaldo Carvalho de Meloe3d006c2015-10-21 15:45:13 -0300689 pr_debug("%s(%d): ERANGE! sym->name=%s, start=%#" PRIx64 ", addr=%#" PRIx64 ", end=%#" PRIx64 "\n",
690 __func__, __LINE__, sym->name, sym->start, addr, sym->end);
Arnaldo Carvalho de Melo31d68e72012-03-27 12:55:57 -0300691 return -ERANGE;
Arnaldo Carvalho de Meloe3d006c2015-10-21 15:45:13 -0300692 }
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200693
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200694 offset = addr - sym->start;
695 h = annotation__histogram(notes, evidx);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200696 h->sum++;
697 h->addr[offset]++;
698
699 pr_debug3("%#" PRIx64 " %s: period++ [addr: %#" PRIx64 ", %#" PRIx64
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200700 ", evidx=%d] => %" PRIu64 "\n", sym->start, sym->name,
701 addr, addr - sym->start, evidx, h->addr[offset]);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200702 return 0;
703}
704
Andi Kleend4957632015-07-18 08:24:48 -0700705static struct annotation *symbol__get_annotation(struct symbol *sym, bool cycles)
Andi Kleen83be34a2015-05-27 10:51:46 -0700706{
707 struct annotation *notes = symbol__annotation(sym);
708
709 if (notes->src == NULL) {
710 if (symbol__alloc_hist(sym) < 0)
711 return NULL;
712 }
Andi Kleend4957632015-07-18 08:24:48 -0700713 if (!notes->src->cycles_hist && cycles) {
714 if (symbol__alloc_hist_cycles(sym) < 0)
715 return NULL;
716 }
Andi Kleen83be34a2015-05-27 10:51:46 -0700717 return notes;
718}
719
Arnaldo Carvalho de Melo44e83032013-12-18 17:12:24 -0300720static int symbol__inc_addr_samples(struct symbol *sym, struct map *map,
721 int evidx, u64 addr)
Arnaldo Carvalho de Melob66d8c02013-12-18 15:37:41 -0300722{
723 struct annotation *notes;
724
Namhyung Kim48c65bd2014-02-20 10:32:53 +0900725 if (sym == NULL)
Arnaldo Carvalho de Melob66d8c02013-12-18 15:37:41 -0300726 return 0;
Andi Kleend4957632015-07-18 08:24:48 -0700727 notes = symbol__get_annotation(sym, false);
Andi Kleen83be34a2015-05-27 10:51:46 -0700728 if (notes == NULL)
729 return -ENOMEM;
Arnaldo Carvalho de Melob66d8c02013-12-18 15:37:41 -0300730 return __symbol__inc_addr_samples(sym, map, notes, evidx, addr);
731}
732
Andi Kleend4957632015-07-18 08:24:48 -0700733static int symbol__account_cycles(u64 addr, u64 start,
734 struct symbol *sym, unsigned cycles)
735{
736 struct annotation *notes;
737 unsigned offset;
738
739 if (sym == NULL)
740 return 0;
741 notes = symbol__get_annotation(sym, true);
742 if (notes == NULL)
743 return -ENOMEM;
744 if (addr < sym->start || addr >= sym->end)
745 return -ERANGE;
746
747 if (start) {
748 if (start < sym->start || start >= sym->end)
749 return -ERANGE;
750 if (start >= addr)
751 start = 0;
752 }
753 offset = addr - sym->start;
754 return __symbol__account_cycles(notes,
755 start ? start - sym->start : 0,
756 offset, cycles,
757 !!start);
758}
759
760int addr_map_symbol__account_cycles(struct addr_map_symbol *ams,
761 struct addr_map_symbol *start,
762 unsigned cycles)
763{
Adrian Hunter3d7245b2015-08-14 10:11:34 +0300764 u64 saddr = 0;
Andi Kleend4957632015-07-18 08:24:48 -0700765 int err;
766
767 if (!cycles)
768 return 0;
769
770 /*
771 * Only set start when IPC can be computed. We can only
772 * compute it when the basic block is completely in a single
773 * function.
774 * Special case the case when the jump is elsewhere, but
775 * it starts on the function start.
776 */
777 if (start &&
778 (start->sym == ams->sym ||
779 (ams->sym &&
780 start->addr == ams->sym->start + ams->map->start)))
781 saddr = start->al_addr;
782 if (saddr == 0)
Adrian Hunter3d7245b2015-08-14 10:11:34 +0300783 pr_debug2("BB with bad start: addr %"PRIx64" start %"PRIx64" sym %"PRIx64" saddr %"PRIx64"\n",
Andi Kleend4957632015-07-18 08:24:48 -0700784 ams->addr,
785 start ? start->addr : 0,
786 ams->sym ? ams->sym->start + ams->map->start : 0,
787 saddr);
788 err = symbol__account_cycles(ams->al_addr, saddr, ams->sym, cycles);
789 if (err)
790 pr_debug2("account_cycles failed %d\n", err);
791 return err;
792}
793
Arnaldo Carvalho de Melo0f4e7a22013-12-18 16:48:29 -0300794int addr_map_symbol__inc_samples(struct addr_map_symbol *ams, int evidx)
795{
796 return symbol__inc_addr_samples(ams->sym, ams->map, evidx, ams->al_addr);
797}
798
Arnaldo Carvalho de Melof626adf2013-12-18 17:10:15 -0300799int hist_entry__inc_addr_samples(struct hist_entry *he, int evidx, u64 ip)
800{
801 return symbol__inc_addr_samples(he->ms.sym, he->ms.map, evidx, ip);
802}
803
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -0300804static void disasm_line__init_ins(struct disasm_line *dl, struct arch *arch, struct map *map)
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -0300805{
Arnaldo Carvalho de Melo75b49202016-11-24 11:16:06 -0300806 dl->ins.ops = ins__find(arch, dl->ins.name);
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -0300807
Arnaldo Carvalho de Melo75b49202016-11-24 11:16:06 -0300808 if (!dl->ins.ops)
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -0300809 return;
810
Arnaldo Carvalho de Melo75b49202016-11-24 11:16:06 -0300811 if (dl->ins.ops->parse && dl->ins.ops->parse(arch, &dl->ops, map) < 0)
812 dl->ins.ops = NULL;
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -0300813}
814
Arnaldo Carvalho de Melo75b49202016-11-24 11:16:06 -0300815static int disasm_line__parse(char *line, const char **namep, char **rawp)
Arnaldo Carvalho de Melo7a997fe2012-05-12 13:15:34 -0300816{
Taeung Song4597cf02017-04-08 09:52:24 +0900817 char tmp, *name = ltrim(line);
Arnaldo Carvalho de Melo7a997fe2012-05-12 13:15:34 -0300818
819 if (name[0] == '\0')
820 return -1;
821
822 *rawp = name + 1;
823
824 while ((*rawp)[0] != '\0' && !isspace((*rawp)[0]))
825 ++*rawp;
826
827 tmp = (*rawp)[0];
828 (*rawp)[0] = '\0';
829 *namep = strdup(name);
830
831 if (*namep == NULL)
832 goto out_free_name;
833
834 (*rawp)[0] = tmp;
Taeung Song4597cf02017-04-08 09:52:24 +0900835 *rawp = ltrim(*rawp);
Arnaldo Carvalho de Melo7a997fe2012-05-12 13:15:34 -0300836
837 return 0;
838
839out_free_name:
Arnaldo Carvalho de Melo75b49202016-11-24 11:16:06 -0300840 free((void *)namep);
841 *namep = NULL;
Arnaldo Carvalho de Melo7a997fe2012-05-12 13:15:34 -0300842 return -1;
843}
844
Andi Kleene5924882014-11-12 18:05:26 -0800845static struct disasm_line *disasm_line__new(s64 offset, char *line,
Arnaldo Carvalho de Melobff5c302016-09-19 17:18:16 -0300846 size_t privsize, int line_nr,
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -0300847 struct arch *arch,
Arnaldo Carvalho de Melobff5c302016-09-19 17:18:16 -0300848 struct map *map)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200849{
Arnaldo Carvalho de Melo51454182012-04-15 15:52:18 -0300850 struct disasm_line *dl = zalloc(sizeof(*dl) + privsize);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200851
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300852 if (dl != NULL) {
853 dl->offset = offset;
854 dl->line = strdup(line);
Andi Kleene5924882014-11-12 18:05:26 -0800855 dl->line_nr = line_nr;
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300856 if (dl->line == NULL)
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -0300857 goto out_delete;
Arnaldo Carvalho de Melo51454182012-04-15 15:52:18 -0300858
859 if (offset != -1) {
Arnaldo Carvalho de Melo75b49202016-11-24 11:16:06 -0300860 if (disasm_line__parse(dl->line, &dl->ins.name, &dl->ops.raw) < 0)
Arnaldo Carvalho de Melo51454182012-04-15 15:52:18 -0300861 goto out_free_line;
862
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -0300863 disasm_line__init_ins(dl, arch, map);
Arnaldo Carvalho de Melo51454182012-04-15 15:52:18 -0300864 }
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200865 }
866
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300867 return dl;
Arnaldo Carvalho de Melo51454182012-04-15 15:52:18 -0300868
869out_free_line:
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300870 zfree(&dl->line);
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -0300871out_delete:
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300872 free(dl);
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -0300873 return NULL;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200874}
875
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300876void disasm_line__free(struct disasm_line *dl)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200877{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300878 zfree(&dl->line);
Arnaldo Carvalho de Melo75b49202016-11-24 11:16:06 -0300879 if (dl->ins.ops && dl->ins.ops->free)
880 dl->ins.ops->free(&dl->ops);
Arnaldo Carvalho de Meloc46219ac2012-05-12 13:26:20 -0300881 else
882 ins__delete(&dl->ops);
Arnaldo Carvalho de Melo75b49202016-11-24 11:16:06 -0300883 free((void *)dl->ins.name);
884 dl->ins.name = NULL;
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300885 free(dl);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200886}
887
Arnaldo Carvalho de Melo54170722012-05-07 18:54:16 -0300888int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size, bool raw)
889{
Arnaldo Carvalho de Melo75b49202016-11-24 11:16:06 -0300890 if (raw || !dl->ins.ops)
891 return scnprintf(bf, size, "%-6.6s %s", dl->ins.name, dl->ops.raw);
Arnaldo Carvalho de Melo54170722012-05-07 18:54:16 -0300892
Arnaldo Carvalho de Melo75b49202016-11-24 11:16:06 -0300893 return ins__scnprintf(&dl->ins, bf, size, &dl->ops);
Arnaldo Carvalho de Melo54170722012-05-07 18:54:16 -0300894}
895
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300896static void disasm__add(struct list_head *head, struct disasm_line *line)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200897{
898 list_add_tail(&line->node, head);
899}
900
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300901struct disasm_line *disasm__get_next_ip_line(struct list_head *head, struct disasm_line *pos)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200902{
903 list_for_each_entry_continue(pos, head, node)
904 if (pos->offset >= 0)
905 return pos;
906
907 return NULL;
908}
909
Namhyung Kime64aa752013-03-05 14:53:30 +0900910double disasm__calc_percent(struct annotation *notes, int evidx, s64 offset,
Martin Liška0c4a5bc2015-06-19 16:10:43 -0300911 s64 end, const char **path, u64 *nr_samples)
Namhyung Kime5ccf9f2013-03-05 14:53:23 +0900912{
913 struct source_line *src_line = notes->src->lines;
Namhyung Kime5ccf9f2013-03-05 14:53:23 +0900914 double percent = 0.0;
Martin Liška0c4a5bc2015-06-19 16:10:43 -0300915 *nr_samples = 0;
Namhyung Kime5ccf9f2013-03-05 14:53:23 +0900916
Namhyung Kimbd64fcb2013-03-05 14:53:24 +0900917 if (src_line) {
Namhyung Kim1491c222013-03-05 14:53:28 +0900918 size_t sizeof_src_line = sizeof(*src_line) +
Arnaldo Carvalho de Melo276af92f2015-06-19 16:36:12 -0300919 sizeof(src_line->samples) * (src_line->nr_pcnt - 1);
Namhyung Kime5ccf9f2013-03-05 14:53:23 +0900920
Namhyung Kim1491c222013-03-05 14:53:28 +0900921 while (offset < end) {
922 src_line = (void *)notes->src->lines +
923 (sizeof_src_line * offset);
924
925 if (*path == NULL)
926 *path = src_line->path;
927
Arnaldo Carvalho de Melo276af92f2015-06-19 16:36:12 -0300928 percent += src_line->samples[evidx].percent;
929 *nr_samples += src_line->samples[evidx].nr;
Namhyung Kim1491c222013-03-05 14:53:28 +0900930 offset++;
Namhyung Kimbd64fcb2013-03-05 14:53:24 +0900931 }
932 } else {
Namhyung Kim1491c222013-03-05 14:53:28 +0900933 struct sym_hist *h = annotation__histogram(notes, evidx);
934 unsigned int hits = 0;
935
Namhyung Kimbd64fcb2013-03-05 14:53:24 +0900936 while (offset < end)
937 hits += h->addr[offset++];
938
Martin Liška0c4a5bc2015-06-19 16:10:43 -0300939 if (h->sum) {
940 *nr_samples = hits;
Namhyung Kimbd64fcb2013-03-05 14:53:24 +0900941 percent = 100.0 * hits / h->sum;
Martin Liška0c4a5bc2015-06-19 16:10:43 -0300942 }
Namhyung Kime5ccf9f2013-03-05 14:53:23 +0900943 }
944
Namhyung Kime5ccf9f2013-03-05 14:53:23 +0900945 return percent;
946}
947
Peter Zijlstra70fbe052016-09-05 16:08:12 -0300948static const char *annotate__address_color(struct block_range *br)
949{
950 double cov = block_range__coverage(br);
951
952 if (cov >= 0) {
953 /* mark red for >75% coverage */
954 if (cov > 0.75)
955 return PERF_COLOR_RED;
956
957 /* mark dull for <1% coverage */
958 if (cov < 0.01)
959 return PERF_COLOR_NORMAL;
960 }
961
962 return PERF_COLOR_MAGENTA;
963}
964
965static const char *annotate__asm_color(struct block_range *br)
966{
967 double cov = block_range__coverage(br);
968
969 if (cov >= 0) {
970 /* mark dull for <1% coverage */
971 if (cov < 0.01)
972 return PERF_COLOR_NORMAL;
973 }
974
975 return PERF_COLOR_BLUE;
976}
977
978static void annotate__branch_printf(struct block_range *br, u64 addr)
979{
980 bool emit_comment = true;
981
982 if (!br)
983 return;
984
985#if 1
986 if (br->is_target && br->start == addr) {
987 struct block_range *branch = br;
988 double p;
989
990 /*
991 * Find matching branch to our target.
992 */
993 while (!branch->is_branch)
994 branch = block_range__next(branch);
995
996 p = 100 *(double)br->entry / branch->coverage;
997
998 if (p > 0.1) {
999 if (emit_comment) {
1000 emit_comment = false;
1001 printf("\t#");
1002 }
1003
1004 /*
1005 * The percentage of coverage joined at this target in relation
1006 * to the next branch.
1007 */
1008 printf(" +%.2f%%", p);
1009 }
1010 }
1011#endif
1012 if (br->is_branch && br->end == addr) {
1013 double p = 100*(double)br->taken / br->coverage;
1014
1015 if (p > 0.1) {
1016 if (emit_comment) {
1017 emit_comment = false;
1018 printf("\t#");
1019 }
1020
1021 /*
1022 * The percentage of coverage leaving at this branch, and
1023 * its prediction ratio.
1024 */
1025 printf(" -%.2f%% (p:%.2f%%)", p, 100*(double)br->pred / br->taken);
1026 }
1027 }
1028}
1029
1030
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -03001031static int disasm_line__print(struct disasm_line *dl, struct symbol *sym, u64 start,
Namhyung Kimdb8fd072013-03-05 14:53:21 +09001032 struct perf_evsel *evsel, u64 len, int min_pcnt, int printed,
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -03001033 int max_lines, struct disasm_line *queue)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001034{
1035 static const char *prev_line;
1036 static const char *prev_color;
1037
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -03001038 if (dl->offset != -1) {
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001039 const char *path = NULL;
Martin Liška0c4a5bc2015-06-19 16:10:43 -03001040 u64 nr_samples;
Namhyung Kimb1dd4432013-03-05 14:53:25 +09001041 double percent, max_percent = 0.0;
1042 double *ppercents = &percent;
Martin Liška0c4a5bc2015-06-19 16:10:43 -03001043 u64 *psamples = &nr_samples;
Namhyung Kimb1dd4432013-03-05 14:53:25 +09001044 int i, nr_percent = 1;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001045 const char *color;
1046 struct annotation *notes = symbol__annotation(sym);
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -03001047 s64 offset = dl->offset;
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -03001048 const u64 addr = start + offset;
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -03001049 struct disasm_line *next;
Peter Zijlstra70fbe052016-09-05 16:08:12 -03001050 struct block_range *br;
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -02001051
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -03001052 next = disasm__get_next_ip_line(&notes->src->source, dl);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001053
Namhyung Kim759ff492013-03-05 14:53:26 +09001054 if (perf_evsel__is_group_event(evsel)) {
Namhyung Kimb1dd4432013-03-05 14:53:25 +09001055 nr_percent = evsel->nr_members;
1056 ppercents = calloc(nr_percent, sizeof(double));
Martin Liška0c4a5bc2015-06-19 16:10:43 -03001057 psamples = calloc(nr_percent, sizeof(u64));
1058 if (ppercents == NULL || psamples == NULL) {
Namhyung Kimb1dd4432013-03-05 14:53:25 +09001059 return -1;
Martin Liška0c4a5bc2015-06-19 16:10:43 -03001060 }
Namhyung Kimb1dd4432013-03-05 14:53:25 +09001061 }
1062
1063 for (i = 0; i < nr_percent; i++) {
1064 percent = disasm__calc_percent(notes,
Namhyung Kim1491c222013-03-05 14:53:28 +09001065 notes->src->lines ? i : evsel->idx + i,
1066 offset,
1067 next ? next->offset : (s64) len,
Martin Liška0c4a5bc2015-06-19 16:10:43 -03001068 &path, &nr_samples);
Namhyung Kimb1dd4432013-03-05 14:53:25 +09001069
1070 ppercents[i] = percent;
Martin Liška0c4a5bc2015-06-19 16:10:43 -03001071 psamples[i] = nr_samples;
Namhyung Kimb1dd4432013-03-05 14:53:25 +09001072 if (percent > max_percent)
1073 max_percent = percent;
1074 }
1075
1076 if (max_percent < min_pcnt)
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -02001077 return -1;
1078
Arnaldo Carvalho de Meloe3087b82011-02-08 15:01:39 -02001079 if (max_lines && printed >= max_lines)
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -02001080 return 1;
Arnaldo Carvalho de Melod040bd32011-02-05 15:37:31 -02001081
Arnaldo Carvalho de Melod5e3d742011-02-08 15:29:25 -02001082 if (queue != NULL) {
1083 list_for_each_entry_from(queue, &notes->src->source, node) {
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -03001084 if (queue == dl)
Arnaldo Carvalho de Melod5e3d742011-02-08 15:29:25 -02001085 break;
Namhyung Kimdb8fd072013-03-05 14:53:21 +09001086 disasm_line__print(queue, sym, start, evsel, len,
Arnaldo Carvalho de Melod5e3d742011-02-08 15:29:25 -02001087 0, 0, 1, NULL);
1088 }
1089 }
1090
Namhyung Kimb1dd4432013-03-05 14:53:25 +09001091 color = get_percent_color(max_percent);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001092
1093 /*
1094 * Also color the filename and line if needed, with
1095 * the same color than the percentage. Don't print it
1096 * twice for close colored addr with the same filename:line
1097 */
1098 if (path) {
1099 if (!prev_line || strcmp(prev_line, path)
1100 || color != prev_color) {
1101 color_fprintf(stdout, color, " %s", path);
1102 prev_line = path;
1103 prev_color = color;
1104 }
1105 }
1106
Namhyung Kimb1dd4432013-03-05 14:53:25 +09001107 for (i = 0; i < nr_percent; i++) {
1108 percent = ppercents[i];
Martin Liška0c4a5bc2015-06-19 16:10:43 -03001109 nr_samples = psamples[i];
Namhyung Kimb1dd4432013-03-05 14:53:25 +09001110 color = get_percent_color(percent);
Martin Liška0c4a5bc2015-06-19 16:10:43 -03001111
1112 if (symbol_conf.show_total_period)
1113 color_fprintf(stdout, color, " %7" PRIu64,
1114 nr_samples);
1115 else
1116 color_fprintf(stdout, color, " %7.2f", percent);
Namhyung Kimb1dd4432013-03-05 14:53:25 +09001117 }
1118
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001119 printf(" : ");
Peter Zijlstra70fbe052016-09-05 16:08:12 -03001120
1121 br = block_range__find(addr);
1122 color_fprintf(stdout, annotate__address_color(br), " %" PRIx64 ":", addr);
1123 color_fprintf(stdout, annotate__asm_color(br), "%s", dl->line);
1124 annotate__branch_printf(br, addr);
1125 printf("\n");
Namhyung Kimb1dd4432013-03-05 14:53:25 +09001126
1127 if (ppercents != &percent)
1128 free(ppercents);
1129
Martin Liška0c4a5bc2015-06-19 16:10:43 -03001130 if (psamples != &nr_samples)
1131 free(psamples);
1132
Arnaldo Carvalho de Meloe3087b82011-02-08 15:01:39 -02001133 } else if (max_lines && printed >= max_lines)
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -02001134 return 1;
1135 else {
Namhyung Kimb1dd4432013-03-05 14:53:25 +09001136 int width = 8;
1137
Arnaldo Carvalho de Melod5e3d742011-02-08 15:29:25 -02001138 if (queue)
1139 return -1;
1140
Namhyung Kim759ff492013-03-05 14:53:26 +09001141 if (perf_evsel__is_group_event(evsel))
Namhyung Kimb1dd4432013-03-05 14:53:25 +09001142 width *= evsel->nr_members;
1143
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -03001144 if (!*dl->line)
Namhyung Kimb1dd4432013-03-05 14:53:25 +09001145 printf(" %*s:\n", width, " ");
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001146 else
Namhyung Kimb1dd4432013-03-05 14:53:25 +09001147 printf(" %*s: %s\n", width, " ", dl->line);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001148 }
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -02001149
1150 return 0;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001151}
1152
Namhyung Kim3aec1502013-03-05 14:53:22 +09001153/*
1154 * symbol__parse_objdump_line() parses objdump output (with -d --no-show-raw)
1155 * which looks like following
1156 *
1157 * 0000000000415500 <_init>:
1158 * 415500: sub $0x8,%rsp
1159 * 415504: mov 0x2f5ad5(%rip),%rax # 70afe0 <_DYNAMIC+0x2f8>
1160 * 41550b: test %rax,%rax
1161 * 41550e: je 415515 <_init+0x15>
1162 * 415510: callq 416e70 <__gmon_start__@plt>
1163 * 415515: add $0x8,%rsp
1164 * 415519: retq
1165 *
1166 * it will be parsed and saved into struct disasm_line as
1167 * <offset> <name> <ops.raw>
1168 *
1169 * The offset will be a relative offset from the start of the symbol and -1
1170 * means that it's not a disassembly line so should be treated differently.
1171 * The ops.raw part will be parsed further according to type of the instruction.
1172 */
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -02001173static int symbol__parse_objdump_line(struct symbol *sym, struct map *map,
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -03001174 struct arch *arch,
Andi Kleene5924882014-11-12 18:05:26 -08001175 FILE *file, size_t privsize,
1176 int *line_nr)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001177{
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -02001178 struct annotation *notes = symbol__annotation(sym);
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -03001179 struct disasm_line *dl;
Taeung Song4597cf02017-04-08 09:52:24 +09001180 char *line = NULL, *parsed_line, *tmp, *tmp2;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001181 size_t line_len;
1182 s64 line_ip, offset = -1;
Andi Kleene5924882014-11-12 18:05:26 -08001183 regmatch_t match[2];
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001184
1185 if (getline(&line, &line_len, file) < 0)
1186 return -1;
1187
1188 if (!line)
1189 return -1;
1190
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001191 line_ip = -1;
Taeung Song4597cf02017-04-08 09:52:24 +09001192 parsed_line = rtrim(line);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001193
Andi Kleene5924882014-11-12 18:05:26 -08001194 /* /filename:linenr ? Save line number and ignore. */
Taeung Song986a5bc2017-04-08 09:52:25 +09001195 if (regexec(&file_lineno, parsed_line, 2, match, 0) == 0) {
1196 *line_nr = atoi(parsed_line + match[1].rm_so);
Andi Kleene5924882014-11-12 18:05:26 -08001197 return 0;
1198 }
1199
Taeung Song4597cf02017-04-08 09:52:24 +09001200 tmp = ltrim(parsed_line);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001201 if (*tmp) {
1202 /*
1203 * Parse hexa addresses followed by ':'
1204 */
1205 line_ip = strtoull(tmp, &tmp2, 16);
1206 if (*tmp2 != ':' || tmp == tmp2 || tmp2[1] == '\0')
1207 line_ip = -1;
1208 }
1209
1210 if (line_ip != -1) {
1211 u64 start = map__rip_2objdump(map, sym->start),
1212 end = map__rip_2objdump(map, sym->end);
1213
1214 offset = line_ip - start;
Arnaldo Carvalho de Melo2c241bd2014-10-14 17:19:44 -03001215 if ((u64)line_ip < start || (u64)line_ip >= end)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001216 offset = -1;
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -03001217 else
1218 parsed_line = tmp2 + 1;
Namhyung Kima31b7cc2012-04-11 17:04:59 -03001219 }
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001220
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -03001221 dl = disasm_line__new(offset, parsed_line, privsize, *line_nr, arch, map);
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -03001222 free(line);
Andi Kleene5924882014-11-12 18:05:26 -08001223 (*line_nr)++;
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -03001224
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -03001225 if (dl == NULL)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001226 return -1;
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -03001227
Ravi Bangoriae2168742016-12-05 21:26:47 +05301228 if (!disasm_line__has_offset(dl)) {
Adrian Hunterbbb7f842013-08-07 14:38:54 +03001229 dl->ops.target.offset = dl->ops.target.addr -
1230 map__rip_2objdump(map, sym->start);
Ravi Bangoriae2168742016-12-05 21:26:47 +05301231 dl->ops.target.offset_avail = true;
1232 }
Adrian Hunterbbb7f842013-08-07 14:38:54 +03001233
Adrian Hunter6e427ab2013-10-14 13:43:40 +03001234 /* kcore has no symbols, so add the call target name */
Arnaldo Carvalho de Melo75b49202016-11-24 11:16:06 -03001235 if (dl->ins.ops && ins__is_call(&dl->ins) && !dl->ops.target.name) {
Adrian Hunter6e427ab2013-10-14 13:43:40 +03001236 struct addr_map_symbol target = {
1237 .map = map,
1238 .addr = dl->ops.target.addr,
1239 };
Adrian Hunterb1781702013-08-07 14:38:57 +03001240
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001241 if (!map_groups__find_ams(&target) &&
Adrian Hunter6e427ab2013-10-14 13:43:40 +03001242 target.sym->start == target.al_addr)
1243 dl->ops.target.name = strdup(target.sym->name);
Adrian Hunterb1781702013-08-07 14:38:57 +03001244 }
1245
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -03001246 disasm__add(&notes->src->source, dl);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001247
1248 return 0;
1249}
1250
Andi Kleene5924882014-11-12 18:05:26 -08001251static __attribute__((constructor)) void symbol__init_regexpr(void)
1252{
1253 regcomp(&file_lineno, "^/[^:]+:([0-9]+)", REG_EXTENDED);
1254}
1255
Adrian Hunter484a5e72013-08-07 14:38:56 +03001256static void delete_last_nop(struct symbol *sym)
1257{
1258 struct annotation *notes = symbol__annotation(sym);
1259 struct list_head *list = &notes->src->source;
1260 struct disasm_line *dl;
1261
1262 while (!list_empty(list)) {
1263 dl = list_entry(list->prev, struct disasm_line, node);
1264
Arnaldo Carvalho de Melo75b49202016-11-24 11:16:06 -03001265 if (dl->ins.ops) {
1266 if (dl->ins.ops != &nop_ops)
Adrian Hunter484a5e72013-08-07 14:38:56 +03001267 return;
1268 } else {
1269 if (!strstr(dl->line, " nop ") &&
1270 !strstr(dl->line, " nopl ") &&
1271 !strstr(dl->line, " nopw "))
1272 return;
1273 }
1274
1275 list_del(&dl->node);
1276 disasm_line__free(dl);
1277 }
1278}
1279
Arnaldo Carvalho de Meloee51d852016-07-29 16:27:18 -03001280int symbol__strerror_disassemble(struct symbol *sym __maybe_unused, struct map *map,
1281 int errnum, char *buf, size_t buflen)
1282{
1283 struct dso *dso = map->dso;
1284
1285 BUG_ON(buflen == 0);
1286
1287 if (errnum >= 0) {
1288 str_error_r(errnum, buf, buflen);
1289 return 0;
1290 }
1291
1292 switch (errnum) {
1293 case SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX: {
1294 char bf[SBUILD_ID_SIZE + 15] = " with build id ";
1295 char *build_id_msg = NULL;
1296
1297 if (dso->has_build_id) {
1298 build_id__sprintf(dso->build_id,
1299 sizeof(dso->build_id), bf + 15);
1300 build_id_msg = bf;
1301 }
1302 scnprintf(buf, buflen,
1303 "No vmlinux file%s\nwas found in the path.\n\n"
1304 "Note that annotation using /proc/kcore requires CAP_SYS_RAWIO capability.\n\n"
1305 "Please use:\n\n"
1306 " perf buildid-cache -vu vmlinux\n\n"
1307 "or:\n\n"
1308 " --vmlinux vmlinux\n", build_id_msg ?: "");
1309 }
1310 break;
1311 default:
1312 scnprintf(buf, buflen, "Internal error: Invalid %d error code\n", errnum);
1313 break;
1314 }
1315
1316 return 0;
1317}
1318
Arnaldo Carvalho de Melo05ed3ac2016-08-09 15:32:53 -03001319static int dso__disassemble_filename(struct dso *dso, char *filename, size_t filename_size)
1320{
1321 char linkname[PATH_MAX];
1322 char *build_id_filename;
Taeung Song6ebd2542017-03-27 16:10:36 +09001323 char *build_id_path = NULL;
Namhyung Kim3619ef72017-06-08 16:31:01 +09001324 char *pos;
Arnaldo Carvalho de Melo05ed3ac2016-08-09 15:32:53 -03001325
1326 if (dso->symtab_type == DSO_BINARY_TYPE__KALLSYMS &&
1327 !dso__is_kcore(dso))
1328 return SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX;
1329
1330 build_id_filename = dso__build_id_filename(dso, NULL, 0);
1331 if (build_id_filename) {
1332 __symbol__join_symfs(filename, filename_size, build_id_filename);
1333 free(build_id_filename);
1334 } else {
1335 if (dso->has_build_id)
1336 return ENOMEM;
1337 goto fallback;
1338 }
1339
Taeung Song6ebd2542017-03-27 16:10:36 +09001340 build_id_path = strdup(filename);
1341 if (!build_id_path)
1342 return -1;
1343
Namhyung Kim3619ef72017-06-08 16:31:01 +09001344 /*
1345 * old style build-id cache has name of XX/XXXXXXX.. while
1346 * new style has XX/XXXXXXX../{elf,kallsyms,vdso}.
1347 * extract the build-id part of dirname in the new style only.
1348 */
1349 pos = strrchr(build_id_path, '/');
1350 if (pos && strlen(pos) < SBUILD_ID_SIZE - 2)
1351 dirname(build_id_path);
Taeung Song6ebd2542017-03-27 16:10:36 +09001352
Arnaldo Carvalho de Melo05ed3ac2016-08-09 15:32:53 -03001353 if (dso__is_kcore(dso) ||
Taeung Song6ebd2542017-03-27 16:10:36 +09001354 readlink(build_id_path, linkname, sizeof(linkname)) < 0 ||
Arnaldo Carvalho de Melo05ed3ac2016-08-09 15:32:53 -03001355 strstr(linkname, DSO__NAME_KALLSYMS) ||
1356 access(filename, R_OK)) {
1357fallback:
1358 /*
1359 * If we don't have build-ids or the build-id file isn't in the
1360 * cache, or is just a kallsyms file, well, lets hope that this
1361 * DSO is the same as when 'perf record' ran.
1362 */
1363 __symbol__join_symfs(filename, filename_size, dso->long_name);
1364 }
1365
Taeung Song6ebd2542017-03-27 16:10:36 +09001366 free(build_id_path);
Arnaldo Carvalho de Melo05ed3ac2016-08-09 15:32:53 -03001367 return 0;
1368}
1369
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -03001370static const char *annotate__norm_arch(const char *arch_name)
1371{
1372 struct utsname uts;
1373
1374 if (!arch_name) { /* Assume we are annotating locally. */
1375 if (uname(&uts) < 0)
1376 return NULL;
1377 arch_name = uts.machine;
1378 }
1379 return normalize_arch((char *)arch_name);
1380}
1381
Jin Yaodcaa3942017-06-19 10:55:56 +08001382int symbol__disassemble(struct symbol *sym, struct map *map,
1383 const char *arch_name, size_t privsize,
1384 struct arch **parch)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001385{
1386 struct dso *dso = map->dso;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001387 char command[PATH_MAX * 2];
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -03001388 struct arch *arch = NULL;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001389 FILE *file;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001390 char symfs_filename[PATH_MAX];
Adrian Hunterafba19d2013-10-09 15:01:12 +03001391 struct kcore_extract kce;
1392 bool delete_extract = false;
Arnaldo Carvalho de Melo9955d0b2016-06-15 15:48:08 -03001393 int stdout_fd[2];
Andi Kleene5924882014-11-12 18:05:26 -08001394 int lineno = 0;
Andi Kleen62ec9b32015-11-05 19:06:07 -08001395 int nline;
Arnaldo Carvalho de Melo9955d0b2016-06-15 15:48:08 -03001396 pid_t pid;
Arnaldo Carvalho de Melo05ed3ac2016-08-09 15:32:53 -03001397 int err = dso__disassemble_filename(dso, symfs_filename, sizeof(symfs_filename));
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001398
Arnaldo Carvalho de Melo05ed3ac2016-08-09 15:32:53 -03001399 if (err)
1400 return err;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001401
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -03001402 arch_name = annotate__norm_arch(arch_name);
1403 if (!arch_name)
1404 return -1;
1405
1406 arch = arch__find(arch_name);
1407 if (arch == NULL)
1408 return -ENOTSUP;
1409
Jin Yaodcaa3942017-06-19 10:55:56 +08001410 if (parch)
1411 *parch = arch;
1412
Arnaldo Carvalho de Melo0781ea92016-11-18 12:34:26 -03001413 if (arch->init) {
1414 err = arch->init(arch);
1415 if (err) {
1416 pr_err("%s: failed to initialize %s arch priv area\n", __func__, arch->name);
1417 return err;
1418 }
1419 }
1420
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001421 pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__,
Arnaldo Carvalho de Melo3caee092016-08-09 15:16:37 -03001422 symfs_filename, sym->name, map->unmap_ip(map, sym->start),
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001423 map->unmap_ip(map, sym->end));
1424
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001425 pr_debug("annotating [%p] %30s : [%p] %30s\n",
1426 dso, dso->long_name, sym, sym->name);
1427
Adrian Hunterafba19d2013-10-09 15:01:12 +03001428 if (dso__is_kcore(dso)) {
1429 kce.kcore_filename = symfs_filename;
1430 kce.addr = map__rip_2objdump(map, sym->start);
1431 kce.offs = sym->start;
Arnaldo Carvalho de Melo2c241bd2014-10-14 17:19:44 -03001432 kce.len = sym->end - sym->start;
Adrian Hunterafba19d2013-10-09 15:01:12 +03001433 if (!kcore_extract__create(&kce)) {
1434 delete_extract = true;
1435 strlcpy(symfs_filename, kce.extract_filename,
1436 sizeof(symfs_filename));
Adrian Hunterafba19d2013-10-09 15:01:12 +03001437 }
Jiri Olsa2c7da8c2015-03-02 12:56:12 -05001438 } else if (dso__needs_decompress(dso)) {
Namhyung Kim3c84fd52017-06-08 16:31:04 +09001439 char tmp[KMOD_DECOMP_LEN];
Jiri Olsa2c7da8c2015-03-02 12:56:12 -05001440
Namhyung Kim3c84fd52017-06-08 16:31:04 +09001441 if (dso__decompress_kmodule_path(dso, symfs_filename,
1442 tmp, sizeof(tmp)) < 0)
Arnaldo Carvalho de Melo3caee092016-08-09 15:16:37 -03001443 goto out;
Jiri Olsa2c7da8c2015-03-02 12:56:12 -05001444
1445 strcpy(symfs_filename, tmp);
Adrian Hunterafba19d2013-10-09 15:01:12 +03001446 }
1447
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001448 snprintf(command, sizeof(command),
Maciek Borzecki7a4ec932012-09-04 12:32:30 +02001449 "%s %s%s --start-address=0x%016" PRIx64
Stephane Eranian3e6a2a72011-05-17 17:32:07 +02001450 " --stop-address=0x%016" PRIx64
Ravi Bangoria7b4500b2017-05-05 15:44:17 +05301451 " -l -d %s %s -C \"%s\" 2>/dev/null|grep -v \"%s:\"|expand",
Maciek Borzecki7a4ec932012-09-04 12:32:30 +02001452 objdump_path ? objdump_path : "objdump",
Andi Kleenf69b64f2011-09-15 14:31:41 -07001453 disassembler_style ? "-M " : "",
1454 disassembler_style ? disassembler_style : "",
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001455 map__rip_2objdump(map, sym->start),
Arnaldo Carvalho de Melo2c241bd2014-10-14 17:19:44 -03001456 map__rip_2objdump(map, sym->end),
Stephane Eranian3e6a2a72011-05-17 17:32:07 +02001457 symbol_conf.annotate_asm_raw ? "" : "--no-show-raw",
1458 symbol_conf.annotate_src ? "-S" : "",
Arnaldo Carvalho de Melo3caee092016-08-09 15:16:37 -03001459 symfs_filename, symfs_filename);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001460
1461 pr_debug("Executing: %s\n", command);
1462
Arnaldo Carvalho de Melo9955d0b2016-06-15 15:48:08 -03001463 err = -1;
1464 if (pipe(stdout_fd) < 0) {
1465 pr_err("Failure creating the pipe to run %s\n", command);
1466 goto out_remove_tmp;
1467 }
1468
1469 pid = fork();
1470 if (pid < 0) {
1471 pr_err("Failure forking to run %s\n", command);
1472 goto out_close_stdout;
1473 }
1474
1475 if (pid == 0) {
1476 close(stdout_fd[0]);
1477 dup2(stdout_fd[1], 1);
1478 close(stdout_fd[1]);
1479 execl("/bin/sh", "sh", "-c", command, NULL);
1480 perror(command);
1481 exit(-1);
1482 }
1483
1484 close(stdout_fd[1]);
1485
1486 file = fdopen(stdout_fd[0], "r");
Andi Kleen62ec9b32015-11-05 19:06:07 -08001487 if (!file) {
Arnaldo Carvalho de Melo9955d0b2016-06-15 15:48:08 -03001488 pr_err("Failure creating FILE stream for %s\n", command);
Andi Kleen62ec9b32015-11-05 19:06:07 -08001489 /*
1490 * If we were using debug info should retry with
1491 * original binary.
1492 */
Jiri Olsa2c7da8c2015-03-02 12:56:12 -05001493 goto out_remove_tmp;
Andi Kleen62ec9b32015-11-05 19:06:07 -08001494 }
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001495
Andi Kleen62ec9b32015-11-05 19:06:07 -08001496 nline = 0;
1497 while (!feof(file)) {
Arnaldo Carvalho de Meloed7b3392017-03-21 16:00:50 -03001498 /*
1499 * The source code line number (lineno) needs to be kept in
1500 * accross calls to symbol__parse_objdump_line(), so that it
1501 * can associate it with the instructions till the next one.
1502 * See disasm_line__new() and struct disasm_line::line_nr.
1503 */
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -03001504 if (symbol__parse_objdump_line(sym, map, arch, file, privsize,
Andi Kleene5924882014-11-12 18:05:26 -08001505 &lineno) < 0)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001506 break;
Andi Kleen62ec9b32015-11-05 19:06:07 -08001507 nline++;
1508 }
1509
1510 if (nline == 0)
1511 pr_err("No output from %s\n", command);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001512
Adrian Hunter484a5e72013-08-07 14:38:56 +03001513 /*
1514 * kallsyms does not have symbol sizes so there may a nop at the end.
1515 * Remove it.
1516 */
1517 if (dso__is_kcore(dso))
1518 delete_last_nop(sym);
1519
Arnaldo Carvalho de Melo9955d0b2016-06-15 15:48:08 -03001520 fclose(file);
1521 err = 0;
Jiri Olsa2c7da8c2015-03-02 12:56:12 -05001522out_remove_tmp:
Arnaldo Carvalho de Melo9955d0b2016-06-15 15:48:08 -03001523 close(stdout_fd[0]);
1524
Jiri Olsa2c7da8c2015-03-02 12:56:12 -05001525 if (dso__needs_decompress(dso))
1526 unlink(symfs_filename);
Arnaldo Carvalho de Melo3caee092016-08-09 15:16:37 -03001527
Adrian Hunterafba19d2013-10-09 15:01:12 +03001528 if (delete_extract)
1529 kcore_extract__delete(&kce);
Arnaldo Carvalho de Meloc12944f2016-08-09 14:56:13 -03001530out:
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001531 return err;
Arnaldo Carvalho de Melo9955d0b2016-06-15 15:48:08 -03001532
1533out_close_stdout:
1534 close(stdout_fd[1]);
1535 goto out_remove_tmp;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001536}
1537
1538static void insert_source_line(struct rb_root *root, struct source_line *src_line)
1539{
1540 struct source_line *iter;
1541 struct rb_node **p = &root->rb_node;
1542 struct rb_node *parent = NULL;
Namhyung Kim1491c222013-03-05 14:53:28 +09001543 int i, ret;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001544
1545 while (*p != NULL) {
1546 parent = *p;
1547 iter = rb_entry(parent, struct source_line, node);
1548
Namhyung Kim41127962012-11-09 14:58:49 +09001549 ret = strcmp(iter->path, src_line->path);
1550 if (ret == 0) {
Namhyung Kim1491c222013-03-05 14:53:28 +09001551 for (i = 0; i < src_line->nr_pcnt; i++)
Arnaldo Carvalho de Melo276af92f2015-06-19 16:36:12 -03001552 iter->samples[i].percent_sum += src_line->samples[i].percent;
Namhyung Kim41127962012-11-09 14:58:49 +09001553 return;
1554 }
1555
1556 if (ret < 0)
1557 p = &(*p)->rb_left;
1558 else
1559 p = &(*p)->rb_right;
1560 }
1561
Namhyung Kim1491c222013-03-05 14:53:28 +09001562 for (i = 0; i < src_line->nr_pcnt; i++)
Arnaldo Carvalho de Melo276af92f2015-06-19 16:36:12 -03001563 src_line->samples[i].percent_sum = src_line->samples[i].percent;
Namhyung Kim41127962012-11-09 14:58:49 +09001564
1565 rb_link_node(&src_line->node, parent, p);
1566 rb_insert_color(&src_line->node, root);
1567}
1568
Namhyung Kim1491c222013-03-05 14:53:28 +09001569static int cmp_source_line(struct source_line *a, struct source_line *b)
1570{
1571 int i;
1572
1573 for (i = 0; i < a->nr_pcnt; i++) {
Arnaldo Carvalho de Melo276af92f2015-06-19 16:36:12 -03001574 if (a->samples[i].percent_sum == b->samples[i].percent_sum)
Namhyung Kim1491c222013-03-05 14:53:28 +09001575 continue;
Arnaldo Carvalho de Melo276af92f2015-06-19 16:36:12 -03001576 return a->samples[i].percent_sum > b->samples[i].percent_sum;
Namhyung Kim1491c222013-03-05 14:53:28 +09001577 }
1578
1579 return 0;
1580}
1581
Namhyung Kim41127962012-11-09 14:58:49 +09001582static void __resort_source_line(struct rb_root *root, struct source_line *src_line)
1583{
1584 struct source_line *iter;
1585 struct rb_node **p = &root->rb_node;
1586 struct rb_node *parent = NULL;
1587
1588 while (*p != NULL) {
1589 parent = *p;
1590 iter = rb_entry(parent, struct source_line, node);
1591
Namhyung Kim1491c222013-03-05 14:53:28 +09001592 if (cmp_source_line(src_line, iter))
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001593 p = &(*p)->rb_left;
1594 else
1595 p = &(*p)->rb_right;
1596 }
1597
1598 rb_link_node(&src_line->node, parent, p);
1599 rb_insert_color(&src_line->node, root);
1600}
1601
Namhyung Kim41127962012-11-09 14:58:49 +09001602static void resort_source_line(struct rb_root *dest_root, struct rb_root *src_root)
1603{
1604 struct source_line *src_line;
1605 struct rb_node *node;
1606
1607 node = rb_first(src_root);
1608 while (node) {
1609 struct rb_node *next;
1610
1611 src_line = rb_entry(node, struct source_line, node);
1612 next = rb_next(node);
1613 rb_erase(node, src_root);
1614
1615 __resort_source_line(dest_root, src_line);
1616 node = next;
1617 }
1618}
1619
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001620static void symbol__free_source_line(struct symbol *sym, int len)
1621{
1622 struct annotation *notes = symbol__annotation(sym);
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -02001623 struct source_line *src_line = notes->src->lines;
Namhyung Kim1491c222013-03-05 14:53:28 +09001624 size_t sizeof_src_line;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001625 int i;
1626
Namhyung Kim1491c222013-03-05 14:53:28 +09001627 sizeof_src_line = sizeof(*src_line) +
Arnaldo Carvalho de Melo276af92f2015-06-19 16:36:12 -03001628 (sizeof(src_line->samples) * (src_line->nr_pcnt - 1));
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001629
Namhyung Kim1491c222013-03-05 14:53:28 +09001630 for (i = 0; i < len; i++) {
Namhyung Kimf048d542013-09-11 14:09:28 +09001631 free_srcline(src_line->path);
Namhyung Kim1491c222013-03-05 14:53:28 +09001632 src_line = (void *)src_line + sizeof_src_line;
1633 }
1634
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001635 zfree(&notes->src->lines);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001636}
1637
1638/* Get the filename:line for the colored entries */
1639static int symbol__get_source_line(struct symbol *sym, struct map *map,
Namhyung Kimdb8fd072013-03-05 14:53:21 +09001640 struct perf_evsel *evsel,
Namhyung Kim86c98ca2013-09-11 14:09:30 +09001641 struct rb_root *root, int len)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001642{
1643 u64 start;
Namhyung Kim1491c222013-03-05 14:53:28 +09001644 int i, k;
1645 int evidx = evsel->idx;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001646 struct source_line *src_line;
1647 struct annotation *notes = symbol__annotation(sym);
Namhyung Kim1491c222013-03-05 14:53:28 +09001648 struct sym_hist *h = annotation__histogram(notes, evidx);
Namhyung Kim41127962012-11-09 14:58:49 +09001649 struct rb_root tmp_root = RB_ROOT;
Namhyung Kim1491c222013-03-05 14:53:28 +09001650 int nr_pcnt = 1;
1651 u64 h_sum = h->sum;
1652 size_t sizeof_src_line = sizeof(struct source_line);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001653
Namhyung Kim1491c222013-03-05 14:53:28 +09001654 if (perf_evsel__is_group_event(evsel)) {
1655 for (i = 1; i < evsel->nr_members; i++) {
1656 h = annotation__histogram(notes, evidx + i);
1657 h_sum += h->sum;
1658 }
1659 nr_pcnt = evsel->nr_members;
Arnaldo Carvalho de Melo276af92f2015-06-19 16:36:12 -03001660 sizeof_src_line += (nr_pcnt - 1) * sizeof(src_line->samples);
Namhyung Kim1491c222013-03-05 14:53:28 +09001661 }
1662
1663 if (!h_sum)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001664 return 0;
1665
Namhyung Kim1491c222013-03-05 14:53:28 +09001666 src_line = notes->src->lines = calloc(len, sizeof_src_line);
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -02001667 if (!notes->src->lines)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001668 return -1;
1669
David Millerf40a0632012-03-25 16:28:12 -04001670 start = map__rip_2objdump(map, sym->start);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001671
1672 for (i = 0; i < len; i++) {
Taeung Song99094a5e2017-03-28 21:12:05 +09001673 u64 offset, nr_samples;
Namhyung Kim1491c222013-03-05 14:53:28 +09001674 double percent_max = 0.0;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001675
Namhyung Kim1491c222013-03-05 14:53:28 +09001676 src_line->nr_pcnt = nr_pcnt;
1677
1678 for (k = 0; k < nr_pcnt; k++) {
Taeung Song2e933b12017-03-27 16:10:37 +09001679 double percent = 0.0;
Namhyung Kim1491c222013-03-05 14:53:28 +09001680
Taeung Song2e933b12017-03-27 16:10:37 +09001681 h = annotation__histogram(notes, evidx + k);
Taeung Song99094a5e2017-03-28 21:12:05 +09001682 nr_samples = h->addr[i];
Taeung Song2e933b12017-03-27 16:10:37 +09001683 if (h->sum)
Taeung Song99094a5e2017-03-28 21:12:05 +09001684 percent = 100.0 * nr_samples / h->sum;
Taeung Song2e933b12017-03-27 16:10:37 +09001685
1686 if (percent > percent_max)
1687 percent_max = percent;
1688 src_line->samples[k].percent = percent;
Taeung Song99094a5e2017-03-28 21:12:05 +09001689 src_line->samples[k].nr = nr_samples;
Namhyung Kim1491c222013-03-05 14:53:28 +09001690 }
1691
1692 if (percent_max <= 0.5)
1693 goto next;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001694
1695 offset = start + i;
Milian Wolff5dfa2102017-03-18 22:49:28 +01001696 src_line->path = get_srcline(map->dso, offset, NULL,
1697 false, true);
Namhyung Kim1491c222013-03-05 14:53:28 +09001698 insert_source_line(&tmp_root, src_line);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001699
Namhyung Kim1491c222013-03-05 14:53:28 +09001700 next:
1701 src_line = (void *)src_line + sizeof_src_line;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001702 }
1703
Namhyung Kim41127962012-11-09 14:58:49 +09001704 resort_source_line(root, &tmp_root);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001705 return 0;
1706}
1707
1708static void print_summary(struct rb_root *root, const char *filename)
1709{
1710 struct source_line *src_line;
1711 struct rb_node *node;
1712
1713 printf("\nSorted summary for file %s\n", filename);
1714 printf("----------------------------------------------\n\n");
1715
1716 if (RB_EMPTY_ROOT(root)) {
1717 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
1718 return;
1719 }
1720
1721 node = rb_first(root);
1722 while (node) {
Namhyung Kim1491c222013-03-05 14:53:28 +09001723 double percent, percent_max = 0.0;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001724 const char *color;
1725 char *path;
Namhyung Kim1491c222013-03-05 14:53:28 +09001726 int i;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001727
1728 src_line = rb_entry(node, struct source_line, node);
Namhyung Kim1491c222013-03-05 14:53:28 +09001729 for (i = 0; i < src_line->nr_pcnt; i++) {
Arnaldo Carvalho de Melo276af92f2015-06-19 16:36:12 -03001730 percent = src_line->samples[i].percent_sum;
Namhyung Kim1491c222013-03-05 14:53:28 +09001731 color = get_percent_color(percent);
1732 color_fprintf(stdout, color, " %7.2f", percent);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001733
Namhyung Kim1491c222013-03-05 14:53:28 +09001734 if (percent > percent_max)
1735 percent_max = percent;
1736 }
1737
1738 path = src_line->path;
1739 color = get_percent_color(percent_max);
Namhyung Kimf048d542013-09-11 14:09:28 +09001740 color_fprintf(stdout, color, " %s\n", path);
Namhyung Kim1491c222013-03-05 14:53:28 +09001741
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001742 node = rb_next(node);
1743 }
1744}
1745
Namhyung Kimdb8fd072013-03-05 14:53:21 +09001746static void symbol__annotate_hits(struct symbol *sym, struct perf_evsel *evsel)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001747{
1748 struct annotation *notes = symbol__annotation(sym);
Namhyung Kimdb8fd072013-03-05 14:53:21 +09001749 struct sym_hist *h = annotation__histogram(notes, evsel->idx);
Arnaldo Carvalho de Melo1b2e2df2012-04-19 10:57:06 -03001750 u64 len = symbol__size(sym), offset;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001751
1752 for (offset = 0; offset < len; ++offset)
1753 if (h->addr[offset] != 0)
1754 printf("%*" PRIx64 ": %" PRIu64 "\n", BITS_PER_LONG / 2,
1755 sym->start + offset, h->addr[offset]);
1756 printf("%*s: %" PRIu64 "\n", BITS_PER_LONG / 2, "h->sum", h->sum);
1757}
1758
Namhyung Kimdb8fd072013-03-05 14:53:21 +09001759int symbol__annotate_printf(struct symbol *sym, struct map *map,
1760 struct perf_evsel *evsel, bool full_paths,
1761 int min_pcnt, int max_lines, int context)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001762{
1763 struct dso *dso = map->dso;
David Ahernbfd14b92012-09-08 09:06:50 -06001764 char *filename;
1765 const char *d_filename;
Arnaldo Carvalho de Melo9cdbadce2014-03-18 11:50:21 -03001766 const char *evsel_name = perf_evsel__name(evsel);
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -02001767 struct annotation *notes = symbol__annotation(sym);
Peter Zijlstra (Intel)135cce12016-06-30 10:29:55 +02001768 struct sym_hist *h = annotation__histogram(notes, evsel->idx);
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -03001769 struct disasm_line *pos, *queue = NULL;
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -03001770 u64 start = map__rip_2objdump(map, sym->start);
Arnaldo Carvalho de Melod5e3d742011-02-08 15:29:25 -02001771 int printed = 2, queue_len = 0;
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -02001772 int more = 0;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001773 u64 len;
Namhyung Kimb1dd4432013-03-05 14:53:25 +09001774 int width = 8;
Peter Zijlstra (Intel)53dd9b52016-06-30 09:17:26 -03001775 int graph_dotted_len;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001776
David Ahernbfd14b92012-09-08 09:06:50 -06001777 filename = strdup(dso->long_name);
1778 if (!filename)
1779 return -ENOMEM;
1780
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001781 if (full_paths)
1782 d_filename = filename;
1783 else
1784 d_filename = basename(filename);
1785
Arnaldo Carvalho de Melo1b2e2df2012-04-19 10:57:06 -03001786 len = symbol__size(sym);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001787
Namhyung Kim759ff492013-03-05 14:53:26 +09001788 if (perf_evsel__is_group_event(evsel))
Namhyung Kimb1dd4432013-03-05 14:53:25 +09001789 width *= evsel->nr_members;
1790
Peter Zijlstra (Intel)135cce12016-06-30 10:29:55 +02001791 graph_dotted_len = printf(" %-*.*s| Source code & Disassembly of %s for %s (%" PRIu64 " samples)\n",
1792 width, width, "Percent", d_filename, evsel_name, h->sum);
Arnaldo Carvalho de Melo9cdbadce2014-03-18 11:50:21 -03001793
Peter Zijlstra (Intel)53dd9b52016-06-30 09:17:26 -03001794 printf("%-*.*s----\n",
Arnaldo Carvalho de Melo9cdbadce2014-03-18 11:50:21 -03001795 graph_dotted_len, graph_dotted_len, graph_dotted_line);
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -02001796
Namhyung Kimbb963e12017-02-17 17:17:38 +09001797 if (verbose > 0)
Namhyung Kimdb8fd072013-03-05 14:53:21 +09001798 symbol__annotate_hits(sym, evsel);
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -02001799
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -02001800 list_for_each_entry(pos, &notes->src->source, node) {
Arnaldo Carvalho de Melod5e3d742011-02-08 15:29:25 -02001801 if (context && queue == NULL) {
1802 queue = pos;
1803 queue_len = 0;
1804 }
1805
Namhyung Kimdb8fd072013-03-05 14:53:21 +09001806 switch (disasm_line__print(pos, sym, start, evsel, len,
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -03001807 min_pcnt, printed, max_lines,
1808 queue)) {
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -02001809 case 0:
1810 ++printed;
Arnaldo Carvalho de Melod5e3d742011-02-08 15:29:25 -02001811 if (context) {
1812 printed += queue_len;
1813 queue = NULL;
1814 queue_len = 0;
1815 }
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -02001816 break;
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -02001817 case 1:
1818 /* filtered by max_lines */
1819 ++more;
1820 break;
1821 case -1:
1822 default:
Arnaldo Carvalho de Melod5e3d742011-02-08 15:29:25 -02001823 /*
1824 * Filtered by min_pcnt or non IP lines when
1825 * context != 0
1826 */
1827 if (!context)
1828 break;
1829 if (queue_len == context)
1830 queue = list_entry(queue->node.next, typeof(*queue), node);
1831 else
1832 ++queue_len;
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -02001833 break;
1834 }
1835 }
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -02001836
David Ahernbfd14b92012-09-08 09:06:50 -06001837 free(filename);
1838
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -02001839 return more;
1840}
1841
1842void symbol__annotate_zero_histogram(struct symbol *sym, int evidx)
1843{
1844 struct annotation *notes = symbol__annotation(sym);
1845 struct sym_hist *h = annotation__histogram(notes, evidx);
1846
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -02001847 memset(h, 0, notes->src->sizeof_sym_hist);
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -02001848}
1849
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -02001850void symbol__annotate_decay_histogram(struct symbol *sym, int evidx)
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -02001851{
1852 struct annotation *notes = symbol__annotation(sym);
1853 struct sym_hist *h = annotation__histogram(notes, evidx);
Arnaldo Carvalho de Melo1b2e2df2012-04-19 10:57:06 -03001854 int len = symbol__size(sym), offset;
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -02001855
1856 h->sum = 0;
Arnaldo Carvalho de Melo8b84a562012-04-05 16:15:59 -03001857 for (offset = 0; offset < len; ++offset) {
1858 h->addr[offset] = h->addr[offset] * 7 / 8;
1859 h->sum += h->addr[offset];
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -02001860 }
1861}
1862
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -03001863void disasm__purge(struct list_head *head)
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -02001864{
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -03001865 struct disasm_line *pos, *n;
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -02001866
1867 list_for_each_entry_safe(pos, n, head, node) {
1868 list_del(&pos->node);
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -03001869 disasm_line__free(pos);
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -02001870 }
1871}
1872
Arnaldo Carvalho de Melo51454182012-04-15 15:52:18 -03001873static size_t disasm_line__fprintf(struct disasm_line *dl, FILE *fp)
1874{
1875 size_t printed;
1876
1877 if (dl->offset == -1)
1878 return fprintf(fp, "%s\n", dl->line);
1879
Arnaldo Carvalho de Melo75b49202016-11-24 11:16:06 -03001880 printed = fprintf(fp, "%#" PRIx64 " %s", dl->offset, dl->ins.name);
Arnaldo Carvalho de Melo51454182012-04-15 15:52:18 -03001881
Arnaldo Carvalho de Meloc7e6ead2012-04-20 14:38:46 -03001882 if (dl->ops.raw[0] != '\0') {
Arnaldo Carvalho de Melo51454182012-04-15 15:52:18 -03001883 printed += fprintf(fp, "%.*s %s\n", 6 - (int)printed, " ",
Arnaldo Carvalho de Meloc7e6ead2012-04-20 14:38:46 -03001884 dl->ops.raw);
Arnaldo Carvalho de Melo51454182012-04-15 15:52:18 -03001885 }
1886
1887 return printed + fprintf(fp, "\n");
1888}
1889
1890size_t disasm__fprintf(struct list_head *head, FILE *fp)
1891{
1892 struct disasm_line *pos;
1893 size_t printed = 0;
1894
1895 list_for_each_entry(pos, head, node)
1896 printed += disasm_line__fprintf(pos, fp);
1897
1898 return printed;
1899}
1900
Namhyung Kimdb8fd072013-03-05 14:53:21 +09001901int symbol__tty_annotate(struct symbol *sym, struct map *map,
1902 struct perf_evsel *evsel, bool print_lines,
1903 bool full_paths, int min_pcnt, int max_lines)
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -02001904{
1905 struct dso *dso = map->dso;
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -02001906 struct rb_root source_line = RB_ROOT;
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -02001907 u64 len;
1908
Jin Yaodcaa3942017-06-19 10:55:56 +08001909 if (symbol__disassemble(sym, map, perf_evsel__env_arch(evsel),
1910 0, NULL) < 0)
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -02001911 return -1;
1912
Arnaldo Carvalho de Melo1b2e2df2012-04-19 10:57:06 -03001913 len = symbol__size(sym);
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -02001914
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001915 if (print_lines) {
Michael Petlan4a4c03c2015-11-09 16:33:31 +01001916 srcline_full_filename = full_paths;
Namhyung Kim86c98ca2013-09-11 14:09:30 +09001917 symbol__get_source_line(sym, map, evsel, &source_line, len);
1918 print_summary(&source_line, dso->long_name);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001919 }
1920
Namhyung Kimdb8fd072013-03-05 14:53:21 +09001921 symbol__annotate_printf(sym, map, evsel, full_paths,
Arnaldo Carvalho de Melod5e3d742011-02-08 15:29:25 -02001922 min_pcnt, max_lines, 0);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001923 if (print_lines)
1924 symbol__free_source_line(sym, len);
1925
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -03001926 disasm__purge(&symbol__annotation(sym)->src->source);
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -02001927
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001928 return 0;
1929}
Arnaldo Carvalho de Melof626adf2013-12-18 17:10:15 -03001930
Namhyung Kim48c65bd2014-02-20 10:32:53 +09001931bool ui__has_annotation(void)
1932{
Jiri Olsa2e0453a2016-05-03 13:54:44 +02001933 return use_browser == 1 && perf_hpp_list.sym;
Namhyung Kim48c65bd2014-02-20 10:32:53 +09001934}