blob: c81a3950a7fea11fa5059c501f3e51794a0d48da [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
10#include "util.h"
Namhyung Kim48c65bd2014-02-20 10:32:53 +090011#include "ui/ui.h"
12#include "sort.h"
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -020013#include "build-id.h"
14#include "color.h"
15#include "cache.h"
16#include "symbol.h"
17#include "debug.h"
18#include "annotate.h"
Namhyung Kimdb8fd072013-03-05 14:53:21 +090019#include "evsel.h"
Peter Zijlstra70fbe052016-09-05 16:08:12 -030020#include "block-range.h"
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -030021#include "arch/common.h"
Andi Kleene5924882014-11-12 18:05:26 -080022#include <regex.h>
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -020023#include <pthread.h>
Jiri Olsa4383db82012-10-27 23:18:29 +020024#include <linux/bitops.h>
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -030025#include <sys/utsname.h>
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -020026
Andi Kleenf69b64f2011-09-15 14:31:41 -070027const char *disassembler_style;
Maciek Borzecki7a4ec932012-09-04 12:32:30 +020028const char *objdump_path;
Andi Kleene5924882014-11-12 18:05:26 -080029static regex_t file_lineno;
Andi Kleenf69b64f2011-09-15 14:31:41 -070030
Arnaldo Carvalho de Melo75b49202016-11-24 11:16:06 -030031static struct ins_ops *ins__find(struct arch *arch, const char *name);
Arnaldo Carvalho de Melo2a1ff812016-11-24 11:37:08 -030032static void ins__sort(struct arch *arch);
Arnaldo Carvalho de Melo75b49202016-11-24 11:16:06 -030033static int disasm_line__parse(char *line, const char **namep, char **rawp);
Arnaldo Carvalho de Melo7a997fe2012-05-12 13:15:34 -030034
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -030035struct arch {
36 const char *name;
Arnaldo Carvalho de Melo763d8962016-11-17 12:31:51 -030037 struct ins *instructions;
38 size_t nr_instructions;
Arnaldo Carvalho de Melo2a1ff812016-11-24 11:37:08 -030039 size_t nr_instructions_allocated;
40 struct ins_ops *(*associate_instruction_ops)(struct arch *arch, const char *name);
Arnaldo Carvalho de Melo763d8962016-11-17 12:31:51 -030041 bool sorted_instructions;
Arnaldo Carvalho de Melo0781ea92016-11-18 12:34:26 -030042 bool initialized;
43 void *priv;
44 int (*init)(struct arch *arch);
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -030045 struct {
46 char comment_char;
Arnaldo Carvalho de Melo9c2fb452016-11-16 15:50:38 -030047 char skip_functions_char;
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -030048 } objdump;
49};
50
Arnaldo Carvalho de Melo763d8962016-11-17 12:31:51 -030051static struct ins_ops call_ops;
52static struct ins_ops dec_ops;
53static struct ins_ops jump_ops;
54static struct ins_ops mov_ops;
55static struct ins_ops nop_ops;
56static struct ins_ops lock_ops;
57static struct ins_ops ret_ops;
58
Arnaldo Carvalho de Melo2a1ff812016-11-24 11:37:08 -030059static int arch__grow_instructions(struct arch *arch)
60{
61 struct ins *new_instructions;
62 size_t new_nr_allocated;
63
64 if (arch->nr_instructions_allocated == 0 && arch->instructions)
65 goto grow_from_non_allocated_table;
66
67 new_nr_allocated = arch->nr_instructions_allocated + 128;
68 new_instructions = realloc(arch->instructions, new_nr_allocated * sizeof(struct ins));
69 if (new_instructions == NULL)
70 return -1;
71
72out_update_instructions:
73 arch->instructions = new_instructions;
74 arch->nr_instructions_allocated = new_nr_allocated;
75 return 0;
76
77grow_from_non_allocated_table:
78 new_nr_allocated = arch->nr_instructions + 128;
79 new_instructions = calloc(new_nr_allocated, sizeof(struct ins));
80 if (new_instructions == NULL)
81 return -1;
82
83 memcpy(new_instructions, arch->instructions, arch->nr_instructions);
84 goto out_update_instructions;
85}
86
Arnaldo Carvalho de Meloacc9bfb2016-11-18 16:54:10 -030087static int arch__associate_ins_ops(struct arch* arch, const char *name, struct ins_ops *ops)
Arnaldo Carvalho de Melo2a1ff812016-11-24 11:37:08 -030088{
89 struct ins *ins;
90
91 if (arch->nr_instructions == arch->nr_instructions_allocated &&
92 arch__grow_instructions(arch))
93 return -1;
94
95 ins = &arch->instructions[arch->nr_instructions];
96 ins->name = strdup(name);
97 if (!ins->name)
98 return -1;
99
100 ins->ops = ops;
101 arch->nr_instructions++;
102
103 ins__sort(arch);
104 return 0;
105}
106
Arnaldo Carvalho de Melo763d8962016-11-17 12:31:51 -0300107#include "arch/arm/annotate/instructions.c"
Kim Phillips0fcb1da2016-11-30 09:23:44 -0600108#include "arch/arm64/annotate/instructions.c"
Arnaldo Carvalho de Melo763d8962016-11-17 12:31:51 -0300109#include "arch/x86/annotate/instructions.c"
Ravi Bangoriadbdebdc2016-11-23 21:33:46 +0530110#include "arch/powerpc/annotate/instructions.c"
Arnaldo Carvalho de Melo763d8962016-11-17 12:31:51 -0300111
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -0300112static struct arch architectures[] = {
113 {
114 .name = "arm",
Arnaldo Carvalho de Meloacc9bfb2016-11-18 16:54:10 -0300115 .init = arm__annotate_init,
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -0300116 },
117 {
Kim Phillips0fcb1da2016-11-30 09:23:44 -0600118 .name = "arm64",
119 .init = arm64__annotate_init,
120 },
121 {
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -0300122 .name = "x86",
Arnaldo Carvalho de Melo763d8962016-11-17 12:31:51 -0300123 .instructions = x86__instructions,
124 .nr_instructions = ARRAY_SIZE(x86__instructions),
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -0300125 .objdump = {
126 .comment_char = '#',
127 },
128 },
Ravi Bangoriadbdebdc2016-11-23 21:33:46 +0530129 {
130 .name = "powerpc",
131 .init = powerpc__annotate_init,
132 },
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -0300133};
134
Arnaldo Carvalho de Meloc46219ac2012-05-12 13:26:20 -0300135static void ins__delete(struct ins_operands *ops)
136{
Arnaldo Carvalho de Melo39956142015-03-05 15:27:28 -0300137 if (ops == NULL)
138 return;
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300139 zfree(&ops->source.raw);
140 zfree(&ops->source.name);
141 zfree(&ops->target.raw);
142 zfree(&ops->target.name);
Arnaldo Carvalho de Meloc46219ac2012-05-12 13:26:20 -0300143}
144
Arnaldo Carvalho de Melo54170722012-05-07 18:54:16 -0300145static int ins__raw_scnprintf(struct ins *ins, char *bf, size_t size,
146 struct ins_operands *ops)
147{
148 return scnprintf(bf, size, "%-6.6s %s", ins->name, ops->raw);
149}
150
151int ins__scnprintf(struct ins *ins, char *bf, size_t size,
152 struct ins_operands *ops)
153{
154 if (ins->ops->scnprintf)
155 return ins->ops->scnprintf(ins, bf, size, ops);
156
157 return ins__raw_scnprintf(ins, bf, size, ops);
158}
159
Arnaldo Carvalho de Melo9c2fb452016-11-16 15:50:38 -0300160static int call__parse(struct arch *arch, struct ins_operands *ops, struct map *map)
Arnaldo Carvalho de Melod86b0592012-04-18 16:07:38 -0300161{
Arnaldo Carvalho de Melod2232882012-04-20 15:26:47 -0300162 char *endptr, *tok, *name;
163
Arnaldo Carvalho de Melo44d1a3e2012-04-25 08:00:23 -0300164 ops->target.addr = strtoull(ops->raw, &endptr, 16);
Arnaldo Carvalho de Melod2232882012-04-20 15:26:47 -0300165
166 name = strchr(endptr, '<');
167 if (name == NULL)
168 goto indirect_call;
169
170 name++;
171
Arnaldo Carvalho de Melo9c2fb452016-11-16 15:50:38 -0300172 if (arch->objdump.skip_functions_char &&
173 strchr(name, arch->objdump.skip_functions_char))
Russell Kingcfef25b2015-12-06 23:07:13 +0000174 return -1;
Russell Kingcfef25b2015-12-06 23:07:13 +0000175
Arnaldo Carvalho de Melod2232882012-04-20 15:26:47 -0300176 tok = strchr(name, '>');
177 if (tok == NULL)
178 return -1;
179
180 *tok = '\0';
Arnaldo Carvalho de Melo44d1a3e2012-04-25 08:00:23 -0300181 ops->target.name = strdup(name);
Arnaldo Carvalho de Melod2232882012-04-20 15:26:47 -0300182 *tok = '>';
183
Arnaldo Carvalho de Melo44d1a3e2012-04-25 08:00:23 -0300184 return ops->target.name == NULL ? -1 : 0;
Arnaldo Carvalho de Melod2232882012-04-20 15:26:47 -0300185
186indirect_call:
Ravi Bangoria88a7fcf2016-08-19 18:29:35 +0530187 tok = strchr(endptr, '*');
188 if (tok == NULL) {
Arnaldo Carvalho de Melo5f62d4f2016-09-19 17:26:11 -0300189 struct symbol *sym = map__find_symbol(map, map->map_ip(map, ops->target.addr));
190 if (sym != NULL)
191 ops->target.name = strdup(sym->name);
192 else
193 ops->target.addr = 0;
Arnaldo Carvalho de Meloe8ea1562012-05-11 12:28:55 -0300194 return 0;
195 }
196
Arnaldo Carvalho de Melo44d1a3e2012-04-25 08:00:23 -0300197 ops->target.addr = strtoull(tok + 1, NULL, 16);
Arnaldo Carvalho de Melod86b0592012-04-18 16:07:38 -0300198 return 0;
199}
200
Arnaldo Carvalho de Melod2232882012-04-20 15:26:47 -0300201static int call__scnprintf(struct ins *ins, char *bf, size_t size,
Arnaldo Carvalho de Melo54170722012-05-07 18:54:16 -0300202 struct ins_operands *ops)
Arnaldo Carvalho de Melod2232882012-04-20 15:26:47 -0300203{
Arnaldo Carvalho de Melo44d1a3e2012-04-25 08:00:23 -0300204 if (ops->target.name)
205 return scnprintf(bf, size, "%-6.6s %s", ins->name, ops->target.name);
Arnaldo Carvalho de Melod2232882012-04-20 15:26:47 -0300206
Arnaldo Carvalho de Meloe8ea1562012-05-11 12:28:55 -0300207 if (ops->target.addr == 0)
208 return ins__raw_scnprintf(ins, bf, size, ops);
209
Arnaldo Carvalho de Melo44d1a3e2012-04-25 08:00:23 -0300210 return scnprintf(bf, size, "%-6.6s *%" PRIx64, ins->name, ops->target.addr);
Arnaldo Carvalho de Melod2232882012-04-20 15:26:47 -0300211}
212
Arnaldo Carvalho de Melod86b0592012-04-18 16:07:38 -0300213static struct ins_ops call_ops = {
Arnaldo Carvalho de Melod2232882012-04-20 15:26:47 -0300214 .parse = call__parse,
215 .scnprintf = call__scnprintf,
Arnaldo Carvalho de Melod86b0592012-04-18 16:07:38 -0300216};
217
218bool ins__is_call(const struct ins *ins)
219{
220 return ins->ops == &call_ops;
221}
222
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -0300223static 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 -0300224{
Arnaldo Carvalho de Meloc7e6ead2012-04-20 14:38:46 -0300225 const char *s = strchr(ops->raw, '+');
Ravi Bangoria3ee2eb62016-12-05 21:26:46 +0530226 const char *c = strchr(ops->raw, ',');
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -0300227
Ravi Bangoria3ee2eb62016-12-05 21:26:46 +0530228 if (c++ != NULL)
229 ops->target.addr = strtoull(c, NULL, 16);
230 else
231 ops->target.addr = strtoull(ops->raw, NULL, 16);
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -0300232
Ravi Bangoriae2168742016-12-05 21:26:47 +0530233 if (s++ != NULL) {
Adrian Hunterbbb7f842013-08-07 14:38:54 +0300234 ops->target.offset = strtoull(s, NULL, 16);
Ravi Bangoriae2168742016-12-05 21:26:47 +0530235 ops->target.offset_avail = true;
236 } else {
237 ops->target.offset_avail = false;
238 }
Arnaldo Carvalho de Melofb29fa582012-04-25 14:16:03 -0300239
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -0300240 return 0;
241}
242
Arnaldo Carvalho de Meloc7e6ead2012-04-20 14:38:46 -0300243static int jump__scnprintf(struct ins *ins, char *bf, size_t size,
Arnaldo Carvalho de Melo54170722012-05-07 18:54:16 -0300244 struct ins_operands *ops)
Arnaldo Carvalho de Melo28548d72012-04-19 10:16:27 -0300245{
Ravi Bangoriae2168742016-12-05 21:26:47 +0530246 if (!ops->target.addr || ops->target.offset < 0)
Ravi Bangoriabec60e52016-12-05 21:26:45 +0530247 return ins__raw_scnprintf(ins, bf, size, ops);
248
Arnaldo Carvalho de Melo44d1a3e2012-04-25 08:00:23 -0300249 return scnprintf(bf, size, "%-6.6s %" PRIx64, ins->name, ops->target.offset);
Arnaldo Carvalho de Melo28548d72012-04-19 10:16:27 -0300250}
251
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -0300252static struct ins_ops jump_ops = {
Arnaldo Carvalho de Meloc7e6ead2012-04-20 14:38:46 -0300253 .parse = jump__parse,
254 .scnprintf = jump__scnprintf,
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -0300255};
256
257bool ins__is_jump(const struct ins *ins)
258{
259 return ins->ops == &jump_ops;
260}
261
Arnaldo Carvalho de Melo6de783b2012-05-11 16:48:49 -0300262static int comment__symbol(char *raw, char *comment, u64 *addrp, char **namep)
263{
264 char *endptr, *name, *t;
265
266 if (strstr(raw, "(%rip)") == NULL)
267 return 0;
268
269 *addrp = strtoull(comment, &endptr, 16);
270 name = strchr(endptr, '<');
271 if (name == NULL)
272 return -1;
273
274 name++;
275
276 t = strchr(name, '>');
277 if (t == NULL)
278 return 0;
279
280 *t = '\0';
281 *namep = strdup(name);
282 *t = '>';
283
284 return 0;
285}
286
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -0300287static int lock__parse(struct arch *arch, struct ins_operands *ops, struct map *map)
Arnaldo Carvalho de Melo7a997fe2012-05-12 13:15:34 -0300288{
Arnaldo Carvalho de Melo7a997fe2012-05-12 13:15:34 -0300289 ops->locked.ops = zalloc(sizeof(*ops->locked.ops));
290 if (ops->locked.ops == NULL)
291 return 0;
292
Arnaldo Carvalho de Melo75b49202016-11-24 11:16:06 -0300293 if (disasm_line__parse(ops->raw, &ops->locked.ins.name, &ops->locked.ops->raw) < 0)
Arnaldo Carvalho de Melo7a997fe2012-05-12 13:15:34 -0300294 goto out_free_ops;
295
Arnaldo Carvalho de Melo75b49202016-11-24 11:16:06 -0300296 ops->locked.ins.ops = ins__find(arch, ops->locked.ins.name);
Rabin Vincent0fb9f2a2015-01-18 20:00:21 +0100297
Arnaldo Carvalho de Melo75b49202016-11-24 11:16:06 -0300298 if (ops->locked.ins.ops == NULL)
Namhyung Kim2ba34aa2012-11-10 02:27:13 +0900299 goto out_free_ops;
Arnaldo Carvalho de Melo7a997fe2012-05-12 13:15:34 -0300300
Arnaldo Carvalho de Melo75b49202016-11-24 11:16:06 -0300301 if (ops->locked.ins.ops->parse &&
302 ops->locked.ins.ops->parse(arch, ops->locked.ops, map) < 0)
Rabin Vincentbe819082015-01-18 20:00:20 +0100303 goto out_free_ops;
Arnaldo Carvalho de Melo7a997fe2012-05-12 13:15:34 -0300304
305 return 0;
306
307out_free_ops:
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300308 zfree(&ops->locked.ops);
Arnaldo Carvalho de Melo7a997fe2012-05-12 13:15:34 -0300309 return 0;
310}
311
312static int lock__scnprintf(struct ins *ins, char *bf, size_t size,
313 struct ins_operands *ops)
314{
315 int printed;
316
Arnaldo Carvalho de Melo75b49202016-11-24 11:16:06 -0300317 if (ops->locked.ins.ops == NULL)
Arnaldo Carvalho de Melo7a997fe2012-05-12 13:15:34 -0300318 return ins__raw_scnprintf(ins, bf, size, ops);
319
320 printed = scnprintf(bf, size, "%-6.6s ", ins->name);
Arnaldo Carvalho de Melo75b49202016-11-24 11:16:06 -0300321 return printed + ins__scnprintf(&ops->locked.ins, bf + printed,
Arnaldo Carvalho de Melo7a997fe2012-05-12 13:15:34 -0300322 size - printed, ops->locked.ops);
323}
324
Arnaldo Carvalho de Meloc46219ac2012-05-12 13:26:20 -0300325static void lock__delete(struct ins_operands *ops)
326{
Arnaldo Carvalho de Melo75b49202016-11-24 11:16:06 -0300327 struct ins *ins = &ops->locked.ins;
Rabin Vincent0fb9f2a2015-01-18 20:00:21 +0100328
Arnaldo Carvalho de Melo75b49202016-11-24 11:16:06 -0300329 if (ins->ops && ins->ops->free)
Rabin Vincent0fb9f2a2015-01-18 20:00:21 +0100330 ins->ops->free(ops->locked.ops);
331 else
332 ins__delete(ops->locked.ops);
333
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300334 zfree(&ops->locked.ops);
335 zfree(&ops->target.raw);
336 zfree(&ops->target.name);
Arnaldo Carvalho de Meloc46219ac2012-05-12 13:26:20 -0300337}
338
Arnaldo Carvalho de Melo7a997fe2012-05-12 13:15:34 -0300339static struct ins_ops lock_ops = {
Arnaldo Carvalho de Meloc46219ac2012-05-12 13:26:20 -0300340 .free = lock__delete,
Arnaldo Carvalho de Melo7a997fe2012-05-12 13:15:34 -0300341 .parse = lock__parse,
342 .scnprintf = lock__scnprintf,
343};
344
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -0300345static int mov__parse(struct arch *arch, struct ins_operands *ops, struct map *map __maybe_unused)
Arnaldo Carvalho de Melo6de783b2012-05-11 16:48:49 -0300346{
347 char *s = strchr(ops->raw, ','), *target, *comment, prev;
348
349 if (s == NULL)
350 return -1;
351
352 *s = '\0';
353 ops->source.raw = strdup(ops->raw);
354 *s = ',';
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -0300355
Arnaldo Carvalho de Melo6de783b2012-05-11 16:48:49 -0300356 if (ops->source.raw == NULL)
357 return -1;
358
359 target = ++s;
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -0300360 comment = strchr(s, arch->objdump.comment_char);
Arnaldo Carvalho de Melo6de783b2012-05-11 16:48:49 -0300361
Alex Converse1e2bb042014-08-14 14:03:00 -0700362 if (comment != NULL)
363 s = comment - 1;
364 else
365 s = strchr(s, '\0') - 1;
366
367 while (s > target && isspace(s[0]))
368 --s;
369 s++;
Arnaldo Carvalho de Melo6de783b2012-05-11 16:48:49 -0300370 prev = *s;
371 *s = '\0';
372
373 ops->target.raw = strdup(target);
374 *s = prev;
375
376 if (ops->target.raw == NULL)
377 goto out_free_source;
378
Arnaldo Carvalho de Melo6de783b2012-05-11 16:48:49 -0300379 if (comment == NULL)
380 return 0;
381
382 while (comment[0] != '\0' && isspace(comment[0]))
383 ++comment;
384
385 comment__symbol(ops->source.raw, comment, &ops->source.addr, &ops->source.name);
386 comment__symbol(ops->target.raw, comment, &ops->target.addr, &ops->target.name);
387
388 return 0;
389
390out_free_source:
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300391 zfree(&ops->source.raw);
Arnaldo Carvalho de Melo6de783b2012-05-11 16:48:49 -0300392 return -1;
393}
394
395static int mov__scnprintf(struct ins *ins, char *bf, size_t size,
396 struct ins_operands *ops)
397{
398 return scnprintf(bf, size, "%-6.6s %s,%s", ins->name,
399 ops->source.name ?: ops->source.raw,
400 ops->target.name ?: ops->target.raw);
401}
402
403static struct ins_ops mov_ops = {
404 .parse = mov__parse,
405 .scnprintf = mov__scnprintf,
406};
407
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -0300408static 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 -0300409{
410 char *target, *comment, *s, prev;
411
412 target = s = ops->raw;
413
414 while (s[0] != '\0' && !isspace(s[0]))
415 ++s;
416 prev = *s;
417 *s = '\0';
418
419 ops->target.raw = strdup(target);
420 *s = prev;
421
422 if (ops->target.raw == NULL)
423 return -1;
424
Kim Phillips859afa62016-11-30 09:23:33 -0600425 comment = strchr(s, arch->objdump.comment_char);
Arnaldo Carvalho de Meloa43712c2012-05-11 17:21:09 -0300426 if (comment == NULL)
427 return 0;
428
429 while (comment[0] != '\0' && isspace(comment[0]))
430 ++comment;
431
432 comment__symbol(ops->target.raw, comment, &ops->target.addr, &ops->target.name);
433
434 return 0;
435}
436
437static int dec__scnprintf(struct ins *ins, char *bf, size_t size,
438 struct ins_operands *ops)
439{
440 return scnprintf(bf, size, "%-6.6s %s", ins->name,
441 ops->target.name ?: ops->target.raw);
442}
443
444static struct ins_ops dec_ops = {
445 .parse = dec__parse,
446 .scnprintf = dec__scnprintf,
447};
448
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300449static int nop__scnprintf(struct ins *ins __maybe_unused, char *bf, size_t size,
450 struct ins_operands *ops __maybe_unused)
Arnaldo Carvalho de Melob9818e92012-05-07 18:57:02 -0300451{
452 return scnprintf(bf, size, "%-6.6s", "nop");
453}
454
455static struct ins_ops nop_ops = {
456 .scnprintf = nop__scnprintf,
457};
458
Naveen N. Rao6ef94922016-06-24 17:23:58 +0530459static struct ins_ops ret_ops = {
460 .scnprintf = ins__raw_scnprintf,
461};
462
463bool ins__is_ret(const struct ins *ins)
464{
465 return ins->ops == &ret_ops;
466}
467
Chris Ryder7e4c1492016-05-19 17:59:46 +0100468static int ins__key_cmp(const void *name, const void *insp)
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -0300469{
470 const struct ins *ins = insp;
471
472 return strcmp(name, ins->name);
473}
474
Chris Ryder7e4c1492016-05-19 17:59:46 +0100475static int ins__cmp(const void *a, const void *b)
476{
477 const struct ins *ia = a;
478 const struct ins *ib = b;
479
480 return strcmp(ia->name, ib->name);
481}
482
Arnaldo Carvalho de Melo763d8962016-11-17 12:31:51 -0300483static void ins__sort(struct arch *arch)
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -0300484{
Arnaldo Carvalho de Melo763d8962016-11-17 12:31:51 -0300485 const int nmemb = arch->nr_instructions;
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -0300486
Arnaldo Carvalho de Melo763d8962016-11-17 12:31:51 -0300487 qsort(arch->instructions, nmemb, sizeof(struct ins), ins__cmp);
Chris Ryder7e4c1492016-05-19 17:59:46 +0100488}
489
Arnaldo Carvalho de Melo2a1ff812016-11-24 11:37:08 -0300490static struct ins_ops *__ins__find(struct arch *arch, const char *name)
Chris Ryder7e4c1492016-05-19 17:59:46 +0100491{
Arnaldo Carvalho de Melo75b49202016-11-24 11:16:06 -0300492 struct ins *ins;
Arnaldo Carvalho de Melo763d8962016-11-17 12:31:51 -0300493 const int nmemb = arch->nr_instructions;
Chris Ryder7e4c1492016-05-19 17:59:46 +0100494
Arnaldo Carvalho de Melo763d8962016-11-17 12:31:51 -0300495 if (!arch->sorted_instructions) {
496 ins__sort(arch);
497 arch->sorted_instructions = true;
Chris Ryder7e4c1492016-05-19 17:59:46 +0100498 }
499
Arnaldo Carvalho de Melo75b49202016-11-24 11:16:06 -0300500 ins = bsearch(name, arch->instructions, nmemb, sizeof(struct ins), ins__key_cmp);
501 return ins ? ins->ops : NULL;
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -0300502}
503
Arnaldo Carvalho de Melo2a1ff812016-11-24 11:37:08 -0300504static struct ins_ops *ins__find(struct arch *arch, const char *name)
505{
506 struct ins_ops *ops = __ins__find(arch, name);
507
508 if (!ops && arch->associate_instruction_ops)
509 ops = arch->associate_instruction_ops(arch, name);
510
511 return ops;
512}
513
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -0300514static int arch__key_cmp(const void *name, const void *archp)
515{
516 const struct arch *arch = archp;
517
518 return strcmp(name, arch->name);
519}
520
521static int arch__cmp(const void *a, const void *b)
522{
523 const struct arch *aa = a;
524 const struct arch *ab = b;
525
526 return strcmp(aa->name, ab->name);
527}
528
529static void arch__sort(void)
530{
531 const int nmemb = ARRAY_SIZE(architectures);
532
533 qsort(architectures, nmemb, sizeof(struct arch), arch__cmp);
534}
535
536static struct arch *arch__find(const char *name)
537{
538 const int nmemb = ARRAY_SIZE(architectures);
539 static bool sorted;
540
541 if (!sorted) {
542 arch__sort();
543 sorted = true;
544 }
545
546 return bsearch(name, architectures, nmemb, sizeof(struct arch), arch__key_cmp);
547}
548
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -0200549int symbol__alloc_hist(struct symbol *sym)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200550{
551 struct annotation *notes = symbol__annotation(sym);
Arnaldo Carvalho de Melo1b2e2df2012-04-19 10:57:06 -0300552 const size_t size = symbol__size(sym);
Cody Schafer86963292012-07-19 20:05:25 -0700553 size_t sizeof_sym_hist;
554
555 /* Check for overflow when calculating sizeof_sym_hist */
556 if (size > (SIZE_MAX - sizeof(struct sym_hist)) / sizeof(u64))
557 return -1;
558
559 sizeof_sym_hist = (sizeof(struct sym_hist) + size * sizeof(u64));
560
561 /* Check for overflow in zalloc argument */
562 if (sizeof_sym_hist > (SIZE_MAX - sizeof(*notes->src))
563 / symbol_conf.nr_events)
564 return -1;
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200565
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -0200566 notes->src = zalloc(sizeof(*notes->src) + symbol_conf.nr_events * sizeof_sym_hist);
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200567 if (notes->src == NULL)
568 return -1;
569 notes->src->sizeof_sym_hist = sizeof_sym_hist;
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -0200570 notes->src->nr_histograms = symbol_conf.nr_events;
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200571 INIT_LIST_HEAD(&notes->src->source);
572 return 0;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200573}
574
Andi Kleend4957632015-07-18 08:24:48 -0700575/* The cycles histogram is lazily allocated. */
576static int symbol__alloc_hist_cycles(struct symbol *sym)
577{
578 struct annotation *notes = symbol__annotation(sym);
579 const size_t size = symbol__size(sym);
580
581 notes->src->cycles_hist = calloc(size, sizeof(struct cyc_hist));
582 if (notes->src->cycles_hist == NULL)
583 return -1;
584 return 0;
585}
586
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200587void symbol__annotate_zero_histograms(struct symbol *sym)
588{
589 struct annotation *notes = symbol__annotation(sym);
590
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200591 pthread_mutex_lock(&notes->lock);
Andi Kleend4957632015-07-18 08:24:48 -0700592 if (notes->src != NULL) {
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200593 memset(notes->src->histograms, 0,
594 notes->src->nr_histograms * notes->src->sizeof_sym_hist);
Andi Kleend4957632015-07-18 08:24:48 -0700595 if (notes->src->cycles_hist)
596 memset(notes->src->cycles_hist, 0,
597 symbol__size(sym) * sizeof(struct cyc_hist));
598 }
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200599 pthread_mutex_unlock(&notes->lock);
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200600}
601
Andi Kleend4957632015-07-18 08:24:48 -0700602static int __symbol__account_cycles(struct annotation *notes,
603 u64 start,
604 unsigned offset, unsigned cycles,
605 unsigned have_start)
606{
607 struct cyc_hist *ch;
608
609 ch = notes->src->cycles_hist;
610 /*
611 * For now we can only account one basic block per
612 * final jump. But multiple could be overlapping.
613 * Always account the longest one. So when
614 * a shorter one has been already seen throw it away.
615 *
616 * We separately always account the full cycles.
617 */
618 ch[offset].num_aggr++;
619 ch[offset].cycles_aggr += cycles;
620
621 if (!have_start && ch[offset].have_start)
622 return 0;
623 if (ch[offset].num) {
624 if (have_start && (!ch[offset].have_start ||
625 ch[offset].start > start)) {
626 ch[offset].have_start = 0;
627 ch[offset].cycles = 0;
628 ch[offset].num = 0;
629 if (ch[offset].reset < 0xffff)
630 ch[offset].reset++;
631 } else if (have_start &&
632 ch[offset].start < start)
633 return 0;
634 }
635 ch[offset].have_start = have_start;
636 ch[offset].start = start;
637 ch[offset].cycles += cycles;
638 ch[offset].num++;
639 return 0;
640}
641
Arnaldo Carvalho de Melob66d8c02013-12-18 15:37:41 -0300642static int __symbol__inc_addr_samples(struct symbol *sym, struct map *map,
643 struct annotation *notes, int evidx, u64 addr)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200644{
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200645 unsigned offset;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200646 struct sym_hist *h;
647
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200648 pr_debug3("%s: addr=%#" PRIx64 "\n", __func__, map->unmap_ip(map, addr));
649
Arnaldo Carvalho de Meloe3d006c2015-10-21 15:45:13 -0300650 if (addr < sym->start || addr >= sym->end) {
651 pr_debug("%s(%d): ERANGE! sym->name=%s, start=%#" PRIx64 ", addr=%#" PRIx64 ", end=%#" PRIx64 "\n",
652 __func__, __LINE__, sym->name, sym->start, addr, sym->end);
Arnaldo Carvalho de Melo31d68e72012-03-27 12:55:57 -0300653 return -ERANGE;
Arnaldo Carvalho de Meloe3d006c2015-10-21 15:45:13 -0300654 }
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200655
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200656 offset = addr - sym->start;
657 h = annotation__histogram(notes, evidx);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200658 h->sum++;
659 h->addr[offset]++;
660
661 pr_debug3("%#" PRIx64 " %s: period++ [addr: %#" PRIx64 ", %#" PRIx64
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200662 ", evidx=%d] => %" PRIu64 "\n", sym->start, sym->name,
663 addr, addr - sym->start, evidx, h->addr[offset]);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200664 return 0;
665}
666
Andi Kleend4957632015-07-18 08:24:48 -0700667static struct annotation *symbol__get_annotation(struct symbol *sym, bool cycles)
Andi Kleen83be34a2015-05-27 10:51:46 -0700668{
669 struct annotation *notes = symbol__annotation(sym);
670
671 if (notes->src == NULL) {
672 if (symbol__alloc_hist(sym) < 0)
673 return NULL;
674 }
Andi Kleend4957632015-07-18 08:24:48 -0700675 if (!notes->src->cycles_hist && cycles) {
676 if (symbol__alloc_hist_cycles(sym) < 0)
677 return NULL;
678 }
Andi Kleen83be34a2015-05-27 10:51:46 -0700679 return notes;
680}
681
Arnaldo Carvalho de Melo44e83032013-12-18 17:12:24 -0300682static int symbol__inc_addr_samples(struct symbol *sym, struct map *map,
683 int evidx, u64 addr)
Arnaldo Carvalho de Melob66d8c02013-12-18 15:37:41 -0300684{
685 struct annotation *notes;
686
Namhyung Kim48c65bd2014-02-20 10:32:53 +0900687 if (sym == NULL)
Arnaldo Carvalho de Melob66d8c02013-12-18 15:37:41 -0300688 return 0;
Andi Kleend4957632015-07-18 08:24:48 -0700689 notes = symbol__get_annotation(sym, false);
Andi Kleen83be34a2015-05-27 10:51:46 -0700690 if (notes == NULL)
691 return -ENOMEM;
Arnaldo Carvalho de Melob66d8c02013-12-18 15:37:41 -0300692 return __symbol__inc_addr_samples(sym, map, notes, evidx, addr);
693}
694
Andi Kleend4957632015-07-18 08:24:48 -0700695static int symbol__account_cycles(u64 addr, u64 start,
696 struct symbol *sym, unsigned cycles)
697{
698 struct annotation *notes;
699 unsigned offset;
700
701 if (sym == NULL)
702 return 0;
703 notes = symbol__get_annotation(sym, true);
704 if (notes == NULL)
705 return -ENOMEM;
706 if (addr < sym->start || addr >= sym->end)
707 return -ERANGE;
708
709 if (start) {
710 if (start < sym->start || start >= sym->end)
711 return -ERANGE;
712 if (start >= addr)
713 start = 0;
714 }
715 offset = addr - sym->start;
716 return __symbol__account_cycles(notes,
717 start ? start - sym->start : 0,
718 offset, cycles,
719 !!start);
720}
721
722int addr_map_symbol__account_cycles(struct addr_map_symbol *ams,
723 struct addr_map_symbol *start,
724 unsigned cycles)
725{
Adrian Hunter3d7245b2015-08-14 10:11:34 +0300726 u64 saddr = 0;
Andi Kleend4957632015-07-18 08:24:48 -0700727 int err;
728
729 if (!cycles)
730 return 0;
731
732 /*
733 * Only set start when IPC can be computed. We can only
734 * compute it when the basic block is completely in a single
735 * function.
736 * Special case the case when the jump is elsewhere, but
737 * it starts on the function start.
738 */
739 if (start &&
740 (start->sym == ams->sym ||
741 (ams->sym &&
742 start->addr == ams->sym->start + ams->map->start)))
743 saddr = start->al_addr;
744 if (saddr == 0)
Adrian Hunter3d7245b2015-08-14 10:11:34 +0300745 pr_debug2("BB with bad start: addr %"PRIx64" start %"PRIx64" sym %"PRIx64" saddr %"PRIx64"\n",
Andi Kleend4957632015-07-18 08:24:48 -0700746 ams->addr,
747 start ? start->addr : 0,
748 ams->sym ? ams->sym->start + ams->map->start : 0,
749 saddr);
750 err = symbol__account_cycles(ams->al_addr, saddr, ams->sym, cycles);
751 if (err)
752 pr_debug2("account_cycles failed %d\n", err);
753 return err;
754}
755
Arnaldo Carvalho de Melo0f4e7a22013-12-18 16:48:29 -0300756int addr_map_symbol__inc_samples(struct addr_map_symbol *ams, int evidx)
757{
758 return symbol__inc_addr_samples(ams->sym, ams->map, evidx, ams->al_addr);
759}
760
Arnaldo Carvalho de Melof626adf2013-12-18 17:10:15 -0300761int hist_entry__inc_addr_samples(struct hist_entry *he, int evidx, u64 ip)
762{
763 return symbol__inc_addr_samples(he->ms.sym, he->ms.map, evidx, ip);
764}
765
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -0300766static void disasm_line__init_ins(struct disasm_line *dl, struct arch *arch, struct map *map)
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -0300767{
Arnaldo Carvalho de Melo75b49202016-11-24 11:16:06 -0300768 dl->ins.ops = ins__find(arch, dl->ins.name);
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -0300769
Arnaldo Carvalho de Melo75b49202016-11-24 11:16:06 -0300770 if (!dl->ins.ops)
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -0300771 return;
772
Arnaldo Carvalho de Melo75b49202016-11-24 11:16:06 -0300773 if (dl->ins.ops->parse && dl->ins.ops->parse(arch, &dl->ops, map) < 0)
774 dl->ins.ops = NULL;
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -0300775}
776
Arnaldo Carvalho de Melo75b49202016-11-24 11:16:06 -0300777static int disasm_line__parse(char *line, const char **namep, char **rawp)
Arnaldo Carvalho de Melo7a997fe2012-05-12 13:15:34 -0300778{
779 char *name = line, tmp;
780
781 while (isspace(name[0]))
782 ++name;
783
784 if (name[0] == '\0')
785 return -1;
786
787 *rawp = name + 1;
788
789 while ((*rawp)[0] != '\0' && !isspace((*rawp)[0]))
790 ++*rawp;
791
792 tmp = (*rawp)[0];
793 (*rawp)[0] = '\0';
794 *namep = strdup(name);
795
796 if (*namep == NULL)
797 goto out_free_name;
798
799 (*rawp)[0] = tmp;
800
801 if ((*rawp)[0] != '\0') {
802 (*rawp)++;
803 while (isspace((*rawp)[0]))
804 ++(*rawp);
805 }
806
807 return 0;
808
809out_free_name:
Arnaldo Carvalho de Melo75b49202016-11-24 11:16:06 -0300810 free((void *)namep);
811 *namep = NULL;
Arnaldo Carvalho de Melo7a997fe2012-05-12 13:15:34 -0300812 return -1;
813}
814
Andi Kleene5924882014-11-12 18:05:26 -0800815static struct disasm_line *disasm_line__new(s64 offset, char *line,
Arnaldo Carvalho de Melobff5c302016-09-19 17:18:16 -0300816 size_t privsize, int line_nr,
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -0300817 struct arch *arch,
Arnaldo Carvalho de Melobff5c302016-09-19 17:18:16 -0300818 struct map *map)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200819{
Arnaldo Carvalho de Melo51454182012-04-15 15:52:18 -0300820 struct disasm_line *dl = zalloc(sizeof(*dl) + privsize);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200821
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300822 if (dl != NULL) {
823 dl->offset = offset;
824 dl->line = strdup(line);
Andi Kleene5924882014-11-12 18:05:26 -0800825 dl->line_nr = line_nr;
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300826 if (dl->line == NULL)
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -0300827 goto out_delete;
Arnaldo Carvalho de Melo51454182012-04-15 15:52:18 -0300828
829 if (offset != -1) {
Arnaldo Carvalho de Melo75b49202016-11-24 11:16:06 -0300830 if (disasm_line__parse(dl->line, &dl->ins.name, &dl->ops.raw) < 0)
Arnaldo Carvalho de Melo51454182012-04-15 15:52:18 -0300831 goto out_free_line;
832
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -0300833 disasm_line__init_ins(dl, arch, map);
Arnaldo Carvalho de Melo51454182012-04-15 15:52:18 -0300834 }
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200835 }
836
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300837 return dl;
Arnaldo Carvalho de Melo51454182012-04-15 15:52:18 -0300838
839out_free_line:
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300840 zfree(&dl->line);
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -0300841out_delete:
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300842 free(dl);
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -0300843 return NULL;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200844}
845
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300846void disasm_line__free(struct disasm_line *dl)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200847{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300848 zfree(&dl->line);
Arnaldo Carvalho de Melo75b49202016-11-24 11:16:06 -0300849 if (dl->ins.ops && dl->ins.ops->free)
850 dl->ins.ops->free(&dl->ops);
Arnaldo Carvalho de Meloc46219ac2012-05-12 13:26:20 -0300851 else
852 ins__delete(&dl->ops);
Arnaldo Carvalho de Melo75b49202016-11-24 11:16:06 -0300853 free((void *)dl->ins.name);
854 dl->ins.name = NULL;
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300855 free(dl);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200856}
857
Arnaldo Carvalho de Melo54170722012-05-07 18:54:16 -0300858int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size, bool raw)
859{
Arnaldo Carvalho de Melo75b49202016-11-24 11:16:06 -0300860 if (raw || !dl->ins.ops)
861 return scnprintf(bf, size, "%-6.6s %s", dl->ins.name, dl->ops.raw);
Arnaldo Carvalho de Melo54170722012-05-07 18:54:16 -0300862
Arnaldo Carvalho de Melo75b49202016-11-24 11:16:06 -0300863 return ins__scnprintf(&dl->ins, bf, size, &dl->ops);
Arnaldo Carvalho de Melo54170722012-05-07 18:54:16 -0300864}
865
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300866static void disasm__add(struct list_head *head, struct disasm_line *line)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200867{
868 list_add_tail(&line->node, head);
869}
870
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300871struct disasm_line *disasm__get_next_ip_line(struct list_head *head, struct disasm_line *pos)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200872{
873 list_for_each_entry_continue(pos, head, node)
874 if (pos->offset >= 0)
875 return pos;
876
877 return NULL;
878}
879
Namhyung Kime64aa752013-03-05 14:53:30 +0900880double disasm__calc_percent(struct annotation *notes, int evidx, s64 offset,
Martin Liška0c4a5bc2015-06-19 16:10:43 -0300881 s64 end, const char **path, u64 *nr_samples)
Namhyung Kime5ccf9f2013-03-05 14:53:23 +0900882{
883 struct source_line *src_line = notes->src->lines;
Namhyung Kime5ccf9f2013-03-05 14:53:23 +0900884 double percent = 0.0;
Martin Liška0c4a5bc2015-06-19 16:10:43 -0300885 *nr_samples = 0;
Namhyung Kime5ccf9f2013-03-05 14:53:23 +0900886
Namhyung Kimbd64fcb2013-03-05 14:53:24 +0900887 if (src_line) {
Namhyung Kim1491c222013-03-05 14:53:28 +0900888 size_t sizeof_src_line = sizeof(*src_line) +
Arnaldo Carvalho de Melo276af92f2015-06-19 16:36:12 -0300889 sizeof(src_line->samples) * (src_line->nr_pcnt - 1);
Namhyung Kime5ccf9f2013-03-05 14:53:23 +0900890
Namhyung Kim1491c222013-03-05 14:53:28 +0900891 while (offset < end) {
892 src_line = (void *)notes->src->lines +
893 (sizeof_src_line * offset);
894
895 if (*path == NULL)
896 *path = src_line->path;
897
Arnaldo Carvalho de Melo276af92f2015-06-19 16:36:12 -0300898 percent += src_line->samples[evidx].percent;
899 *nr_samples += src_line->samples[evidx].nr;
Namhyung Kim1491c222013-03-05 14:53:28 +0900900 offset++;
Namhyung Kimbd64fcb2013-03-05 14:53:24 +0900901 }
902 } else {
Namhyung Kim1491c222013-03-05 14:53:28 +0900903 struct sym_hist *h = annotation__histogram(notes, evidx);
904 unsigned int hits = 0;
905
Namhyung Kimbd64fcb2013-03-05 14:53:24 +0900906 while (offset < end)
907 hits += h->addr[offset++];
908
Martin Liška0c4a5bc2015-06-19 16:10:43 -0300909 if (h->sum) {
910 *nr_samples = hits;
Namhyung Kimbd64fcb2013-03-05 14:53:24 +0900911 percent = 100.0 * hits / h->sum;
Martin Liška0c4a5bc2015-06-19 16:10:43 -0300912 }
Namhyung Kime5ccf9f2013-03-05 14:53:23 +0900913 }
914
Namhyung Kime5ccf9f2013-03-05 14:53:23 +0900915 return percent;
916}
917
Peter Zijlstra70fbe052016-09-05 16:08:12 -0300918static const char *annotate__address_color(struct block_range *br)
919{
920 double cov = block_range__coverage(br);
921
922 if (cov >= 0) {
923 /* mark red for >75% coverage */
924 if (cov > 0.75)
925 return PERF_COLOR_RED;
926
927 /* mark dull for <1% coverage */
928 if (cov < 0.01)
929 return PERF_COLOR_NORMAL;
930 }
931
932 return PERF_COLOR_MAGENTA;
933}
934
935static const char *annotate__asm_color(struct block_range *br)
936{
937 double cov = block_range__coverage(br);
938
939 if (cov >= 0) {
940 /* mark dull for <1% coverage */
941 if (cov < 0.01)
942 return PERF_COLOR_NORMAL;
943 }
944
945 return PERF_COLOR_BLUE;
946}
947
948static void annotate__branch_printf(struct block_range *br, u64 addr)
949{
950 bool emit_comment = true;
951
952 if (!br)
953 return;
954
955#if 1
956 if (br->is_target && br->start == addr) {
957 struct block_range *branch = br;
958 double p;
959
960 /*
961 * Find matching branch to our target.
962 */
963 while (!branch->is_branch)
964 branch = block_range__next(branch);
965
966 p = 100 *(double)br->entry / branch->coverage;
967
968 if (p > 0.1) {
969 if (emit_comment) {
970 emit_comment = false;
971 printf("\t#");
972 }
973
974 /*
975 * The percentage of coverage joined at this target in relation
976 * to the next branch.
977 */
978 printf(" +%.2f%%", p);
979 }
980 }
981#endif
982 if (br->is_branch && br->end == addr) {
983 double p = 100*(double)br->taken / br->coverage;
984
985 if (p > 0.1) {
986 if (emit_comment) {
987 emit_comment = false;
988 printf("\t#");
989 }
990
991 /*
992 * The percentage of coverage leaving at this branch, and
993 * its prediction ratio.
994 */
995 printf(" -%.2f%% (p:%.2f%%)", p, 100*(double)br->pred / br->taken);
996 }
997 }
998}
999
1000
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -03001001static int disasm_line__print(struct disasm_line *dl, struct symbol *sym, u64 start,
Namhyung Kimdb8fd072013-03-05 14:53:21 +09001002 struct perf_evsel *evsel, u64 len, int min_pcnt, int printed,
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -03001003 int max_lines, struct disasm_line *queue)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001004{
1005 static const char *prev_line;
1006 static const char *prev_color;
1007
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -03001008 if (dl->offset != -1) {
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001009 const char *path = NULL;
Martin Liška0c4a5bc2015-06-19 16:10:43 -03001010 u64 nr_samples;
Namhyung Kimb1dd4432013-03-05 14:53:25 +09001011 double percent, max_percent = 0.0;
1012 double *ppercents = &percent;
Martin Liška0c4a5bc2015-06-19 16:10:43 -03001013 u64 *psamples = &nr_samples;
Namhyung Kimb1dd4432013-03-05 14:53:25 +09001014 int i, nr_percent = 1;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001015 const char *color;
1016 struct annotation *notes = symbol__annotation(sym);
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -03001017 s64 offset = dl->offset;
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -03001018 const u64 addr = start + offset;
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -03001019 struct disasm_line *next;
Peter Zijlstra70fbe052016-09-05 16:08:12 -03001020 struct block_range *br;
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -02001021
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -03001022 next = disasm__get_next_ip_line(&notes->src->source, dl);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001023
Namhyung Kim759ff492013-03-05 14:53:26 +09001024 if (perf_evsel__is_group_event(evsel)) {
Namhyung Kimb1dd4432013-03-05 14:53:25 +09001025 nr_percent = evsel->nr_members;
1026 ppercents = calloc(nr_percent, sizeof(double));
Martin Liška0c4a5bc2015-06-19 16:10:43 -03001027 psamples = calloc(nr_percent, sizeof(u64));
1028 if (ppercents == NULL || psamples == NULL) {
Namhyung Kimb1dd4432013-03-05 14:53:25 +09001029 return -1;
Martin Liška0c4a5bc2015-06-19 16:10:43 -03001030 }
Namhyung Kimb1dd4432013-03-05 14:53:25 +09001031 }
1032
1033 for (i = 0; i < nr_percent; i++) {
1034 percent = disasm__calc_percent(notes,
Namhyung Kim1491c222013-03-05 14:53:28 +09001035 notes->src->lines ? i : evsel->idx + i,
1036 offset,
1037 next ? next->offset : (s64) len,
Martin Liška0c4a5bc2015-06-19 16:10:43 -03001038 &path, &nr_samples);
Namhyung Kimb1dd4432013-03-05 14:53:25 +09001039
1040 ppercents[i] = percent;
Martin Liška0c4a5bc2015-06-19 16:10:43 -03001041 psamples[i] = nr_samples;
Namhyung Kimb1dd4432013-03-05 14:53:25 +09001042 if (percent > max_percent)
1043 max_percent = percent;
1044 }
1045
1046 if (max_percent < min_pcnt)
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -02001047 return -1;
1048
Arnaldo Carvalho de Meloe3087b82011-02-08 15:01:39 -02001049 if (max_lines && printed >= max_lines)
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -02001050 return 1;
Arnaldo Carvalho de Melod040bd32011-02-05 15:37:31 -02001051
Arnaldo Carvalho de Melod5e3d742011-02-08 15:29:25 -02001052 if (queue != NULL) {
1053 list_for_each_entry_from(queue, &notes->src->source, node) {
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -03001054 if (queue == dl)
Arnaldo Carvalho de Melod5e3d742011-02-08 15:29:25 -02001055 break;
Namhyung Kimdb8fd072013-03-05 14:53:21 +09001056 disasm_line__print(queue, sym, start, evsel, len,
Arnaldo Carvalho de Melod5e3d742011-02-08 15:29:25 -02001057 0, 0, 1, NULL);
1058 }
1059 }
1060
Namhyung Kimb1dd4432013-03-05 14:53:25 +09001061 color = get_percent_color(max_percent);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001062
1063 /*
1064 * Also color the filename and line if needed, with
1065 * the same color than the percentage. Don't print it
1066 * twice for close colored addr with the same filename:line
1067 */
1068 if (path) {
1069 if (!prev_line || strcmp(prev_line, path)
1070 || color != prev_color) {
1071 color_fprintf(stdout, color, " %s", path);
1072 prev_line = path;
1073 prev_color = color;
1074 }
1075 }
1076
Namhyung Kimb1dd4432013-03-05 14:53:25 +09001077 for (i = 0; i < nr_percent; i++) {
1078 percent = ppercents[i];
Martin Liška0c4a5bc2015-06-19 16:10:43 -03001079 nr_samples = psamples[i];
Namhyung Kimb1dd4432013-03-05 14:53:25 +09001080 color = get_percent_color(percent);
Martin Liška0c4a5bc2015-06-19 16:10:43 -03001081
1082 if (symbol_conf.show_total_period)
1083 color_fprintf(stdout, color, " %7" PRIu64,
1084 nr_samples);
1085 else
1086 color_fprintf(stdout, color, " %7.2f", percent);
Namhyung Kimb1dd4432013-03-05 14:53:25 +09001087 }
1088
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001089 printf(" : ");
Peter Zijlstra70fbe052016-09-05 16:08:12 -03001090
1091 br = block_range__find(addr);
1092 color_fprintf(stdout, annotate__address_color(br), " %" PRIx64 ":", addr);
1093 color_fprintf(stdout, annotate__asm_color(br), "%s", dl->line);
1094 annotate__branch_printf(br, addr);
1095 printf("\n");
Namhyung Kimb1dd4432013-03-05 14:53:25 +09001096
1097 if (ppercents != &percent)
1098 free(ppercents);
1099
Martin Liška0c4a5bc2015-06-19 16:10:43 -03001100 if (psamples != &nr_samples)
1101 free(psamples);
1102
Arnaldo Carvalho de Meloe3087b82011-02-08 15:01:39 -02001103 } else if (max_lines && printed >= max_lines)
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -02001104 return 1;
1105 else {
Namhyung Kimb1dd4432013-03-05 14:53:25 +09001106 int width = 8;
1107
Arnaldo Carvalho de Melod5e3d742011-02-08 15:29:25 -02001108 if (queue)
1109 return -1;
1110
Namhyung Kim759ff492013-03-05 14:53:26 +09001111 if (perf_evsel__is_group_event(evsel))
Namhyung Kimb1dd4432013-03-05 14:53:25 +09001112 width *= evsel->nr_members;
1113
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -03001114 if (!*dl->line)
Namhyung Kimb1dd4432013-03-05 14:53:25 +09001115 printf(" %*s:\n", width, " ");
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001116 else
Namhyung Kimb1dd4432013-03-05 14:53:25 +09001117 printf(" %*s: %s\n", width, " ", dl->line);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001118 }
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -02001119
1120 return 0;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001121}
1122
Namhyung Kim3aec1502013-03-05 14:53:22 +09001123/*
1124 * symbol__parse_objdump_line() parses objdump output (with -d --no-show-raw)
1125 * which looks like following
1126 *
1127 * 0000000000415500 <_init>:
1128 * 415500: sub $0x8,%rsp
1129 * 415504: mov 0x2f5ad5(%rip),%rax # 70afe0 <_DYNAMIC+0x2f8>
1130 * 41550b: test %rax,%rax
1131 * 41550e: je 415515 <_init+0x15>
1132 * 415510: callq 416e70 <__gmon_start__@plt>
1133 * 415515: add $0x8,%rsp
1134 * 415519: retq
1135 *
1136 * it will be parsed and saved into struct disasm_line as
1137 * <offset> <name> <ops.raw>
1138 *
1139 * The offset will be a relative offset from the start of the symbol and -1
1140 * means that it's not a disassembly line so should be treated differently.
1141 * The ops.raw part will be parsed further according to type of the instruction.
1142 */
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -02001143static int symbol__parse_objdump_line(struct symbol *sym, struct map *map,
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -03001144 struct arch *arch,
Andi Kleene5924882014-11-12 18:05:26 -08001145 FILE *file, size_t privsize,
1146 int *line_nr)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001147{
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -02001148 struct annotation *notes = symbol__annotation(sym);
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -03001149 struct disasm_line *dl;
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -03001150 char *line = NULL, *parsed_line, *tmp, *tmp2, *c;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001151 size_t line_len;
1152 s64 line_ip, offset = -1;
Andi Kleene5924882014-11-12 18:05:26 -08001153 regmatch_t match[2];
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001154
1155 if (getline(&line, &line_len, file) < 0)
1156 return -1;
1157
1158 if (!line)
1159 return -1;
1160
1161 while (line_len != 0 && isspace(line[line_len - 1]))
1162 line[--line_len] = '\0';
1163
1164 c = strchr(line, '\n');
1165 if (c)
1166 *c = 0;
1167
1168 line_ip = -1;
Namhyung Kima31b7cc2012-04-11 17:04:59 -03001169 parsed_line = line;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001170
Andi Kleene5924882014-11-12 18:05:26 -08001171 /* /filename:linenr ? Save line number and ignore. */
1172 if (regexec(&file_lineno, line, 2, match, 0) == 0) {
1173 *line_nr = atoi(line + match[1].rm_so);
1174 return 0;
1175 }
1176
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001177 /*
1178 * Strip leading spaces:
1179 */
1180 tmp = line;
1181 while (*tmp) {
1182 if (*tmp != ' ')
1183 break;
1184 tmp++;
1185 }
1186
1187 if (*tmp) {
1188 /*
1189 * Parse hexa addresses followed by ':'
1190 */
1191 line_ip = strtoull(tmp, &tmp2, 16);
1192 if (*tmp2 != ':' || tmp == tmp2 || tmp2[1] == '\0')
1193 line_ip = -1;
1194 }
1195
1196 if (line_ip != -1) {
1197 u64 start = map__rip_2objdump(map, sym->start),
1198 end = map__rip_2objdump(map, sym->end);
1199
1200 offset = line_ip - start;
Arnaldo Carvalho de Melo2c241bd2014-10-14 17:19:44 -03001201 if ((u64)line_ip < start || (u64)line_ip >= end)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001202 offset = -1;
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -03001203 else
1204 parsed_line = tmp2 + 1;
Namhyung Kima31b7cc2012-04-11 17:04:59 -03001205 }
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001206
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -03001207 dl = disasm_line__new(offset, parsed_line, privsize, *line_nr, arch, map);
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -03001208 free(line);
Andi Kleene5924882014-11-12 18:05:26 -08001209 (*line_nr)++;
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -03001210
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -03001211 if (dl == NULL)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001212 return -1;
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -03001213
Ravi Bangoriae2168742016-12-05 21:26:47 +05301214 if (!disasm_line__has_offset(dl)) {
Adrian Hunterbbb7f842013-08-07 14:38:54 +03001215 dl->ops.target.offset = dl->ops.target.addr -
1216 map__rip_2objdump(map, sym->start);
Ravi Bangoriae2168742016-12-05 21:26:47 +05301217 dl->ops.target.offset_avail = true;
1218 }
Adrian Hunterbbb7f842013-08-07 14:38:54 +03001219
Adrian Hunter6e427ab2013-10-14 13:43:40 +03001220 /* kcore has no symbols, so add the call target name */
Arnaldo Carvalho de Melo75b49202016-11-24 11:16:06 -03001221 if (dl->ins.ops && ins__is_call(&dl->ins) && !dl->ops.target.name) {
Adrian Hunter6e427ab2013-10-14 13:43:40 +03001222 struct addr_map_symbol target = {
1223 .map = map,
1224 .addr = dl->ops.target.addr,
1225 };
Adrian Hunterb1781702013-08-07 14:38:57 +03001226
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001227 if (!map_groups__find_ams(&target) &&
Adrian Hunter6e427ab2013-10-14 13:43:40 +03001228 target.sym->start == target.al_addr)
1229 dl->ops.target.name = strdup(target.sym->name);
Adrian Hunterb1781702013-08-07 14:38:57 +03001230 }
1231
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -03001232 disasm__add(&notes->src->source, dl);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001233
1234 return 0;
1235}
1236
Andi Kleene5924882014-11-12 18:05:26 -08001237static __attribute__((constructor)) void symbol__init_regexpr(void)
1238{
1239 regcomp(&file_lineno, "^/[^:]+:([0-9]+)", REG_EXTENDED);
1240}
1241
Adrian Hunter484a5e72013-08-07 14:38:56 +03001242static void delete_last_nop(struct symbol *sym)
1243{
1244 struct annotation *notes = symbol__annotation(sym);
1245 struct list_head *list = &notes->src->source;
1246 struct disasm_line *dl;
1247
1248 while (!list_empty(list)) {
1249 dl = list_entry(list->prev, struct disasm_line, node);
1250
Arnaldo Carvalho de Melo75b49202016-11-24 11:16:06 -03001251 if (dl->ins.ops) {
1252 if (dl->ins.ops != &nop_ops)
Adrian Hunter484a5e72013-08-07 14:38:56 +03001253 return;
1254 } else {
1255 if (!strstr(dl->line, " nop ") &&
1256 !strstr(dl->line, " nopl ") &&
1257 !strstr(dl->line, " nopw "))
1258 return;
1259 }
1260
1261 list_del(&dl->node);
1262 disasm_line__free(dl);
1263 }
1264}
1265
Arnaldo Carvalho de Meloee51d852016-07-29 16:27:18 -03001266int symbol__strerror_disassemble(struct symbol *sym __maybe_unused, struct map *map,
1267 int errnum, char *buf, size_t buflen)
1268{
1269 struct dso *dso = map->dso;
1270
1271 BUG_ON(buflen == 0);
1272
1273 if (errnum >= 0) {
1274 str_error_r(errnum, buf, buflen);
1275 return 0;
1276 }
1277
1278 switch (errnum) {
1279 case SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX: {
1280 char bf[SBUILD_ID_SIZE + 15] = " with build id ";
1281 char *build_id_msg = NULL;
1282
1283 if (dso->has_build_id) {
1284 build_id__sprintf(dso->build_id,
1285 sizeof(dso->build_id), bf + 15);
1286 build_id_msg = bf;
1287 }
1288 scnprintf(buf, buflen,
1289 "No vmlinux file%s\nwas found in the path.\n\n"
1290 "Note that annotation using /proc/kcore requires CAP_SYS_RAWIO capability.\n\n"
1291 "Please use:\n\n"
1292 " perf buildid-cache -vu vmlinux\n\n"
1293 "or:\n\n"
1294 " --vmlinux vmlinux\n", build_id_msg ?: "");
1295 }
1296 break;
1297 default:
1298 scnprintf(buf, buflen, "Internal error: Invalid %d error code\n", errnum);
1299 break;
1300 }
1301
1302 return 0;
1303}
1304
Arnaldo Carvalho de Melo05ed3ac2016-08-09 15:32:53 -03001305static int dso__disassemble_filename(struct dso *dso, char *filename, size_t filename_size)
1306{
1307 char linkname[PATH_MAX];
1308 char *build_id_filename;
1309
1310 if (dso->symtab_type == DSO_BINARY_TYPE__KALLSYMS &&
1311 !dso__is_kcore(dso))
1312 return SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX;
1313
1314 build_id_filename = dso__build_id_filename(dso, NULL, 0);
1315 if (build_id_filename) {
1316 __symbol__join_symfs(filename, filename_size, build_id_filename);
1317 free(build_id_filename);
1318 } else {
1319 if (dso->has_build_id)
1320 return ENOMEM;
1321 goto fallback;
1322 }
1323
1324 if (dso__is_kcore(dso) ||
1325 readlink(filename, linkname, sizeof(linkname)) < 0 ||
1326 strstr(linkname, DSO__NAME_KALLSYMS) ||
1327 access(filename, R_OK)) {
1328fallback:
1329 /*
1330 * If we don't have build-ids or the build-id file isn't in the
1331 * cache, or is just a kallsyms file, well, lets hope that this
1332 * DSO is the same as when 'perf record' ran.
1333 */
1334 __symbol__join_symfs(filename, filename_size, dso->long_name);
1335 }
1336
1337 return 0;
1338}
1339
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -03001340static const char *annotate__norm_arch(const char *arch_name)
1341{
1342 struct utsname uts;
1343
1344 if (!arch_name) { /* Assume we are annotating locally. */
1345 if (uname(&uts) < 0)
1346 return NULL;
1347 arch_name = uts.machine;
1348 }
1349 return normalize_arch((char *)arch_name);
1350}
1351
1352int symbol__disassemble(struct symbol *sym, struct map *map, const char *arch_name, size_t privsize)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001353{
1354 struct dso *dso = map->dso;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001355 char command[PATH_MAX * 2];
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -03001356 struct arch *arch = NULL;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001357 FILE *file;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001358 char symfs_filename[PATH_MAX];
Adrian Hunterafba19d2013-10-09 15:01:12 +03001359 struct kcore_extract kce;
1360 bool delete_extract = false;
Arnaldo Carvalho de Melo9955d0b2016-06-15 15:48:08 -03001361 int stdout_fd[2];
Andi Kleene5924882014-11-12 18:05:26 -08001362 int lineno = 0;
Andi Kleen62ec9b32015-11-05 19:06:07 -08001363 int nline;
Arnaldo Carvalho de Melo9955d0b2016-06-15 15:48:08 -03001364 pid_t pid;
Arnaldo Carvalho de Melo05ed3ac2016-08-09 15:32:53 -03001365 int err = dso__disassemble_filename(dso, symfs_filename, sizeof(symfs_filename));
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001366
Arnaldo Carvalho de Melo05ed3ac2016-08-09 15:32:53 -03001367 if (err)
1368 return err;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001369
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -03001370 arch_name = annotate__norm_arch(arch_name);
1371 if (!arch_name)
1372 return -1;
1373
1374 arch = arch__find(arch_name);
1375 if (arch == NULL)
1376 return -ENOTSUP;
1377
Arnaldo Carvalho de Melo0781ea92016-11-18 12:34:26 -03001378 if (arch->init) {
1379 err = arch->init(arch);
1380 if (err) {
1381 pr_err("%s: failed to initialize %s arch priv area\n", __func__, arch->name);
1382 return err;
1383 }
1384 }
1385
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001386 pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__,
Arnaldo Carvalho de Melo3caee092016-08-09 15:16:37 -03001387 symfs_filename, sym->name, map->unmap_ip(map, sym->start),
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001388 map->unmap_ip(map, sym->end));
1389
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001390 pr_debug("annotating [%p] %30s : [%p] %30s\n",
1391 dso, dso->long_name, sym, sym->name);
1392
Adrian Hunterafba19d2013-10-09 15:01:12 +03001393 if (dso__is_kcore(dso)) {
1394 kce.kcore_filename = symfs_filename;
1395 kce.addr = map__rip_2objdump(map, sym->start);
1396 kce.offs = sym->start;
Arnaldo Carvalho de Melo2c241bd2014-10-14 17:19:44 -03001397 kce.len = sym->end - sym->start;
Adrian Hunterafba19d2013-10-09 15:01:12 +03001398 if (!kcore_extract__create(&kce)) {
1399 delete_extract = true;
1400 strlcpy(symfs_filename, kce.extract_filename,
1401 sizeof(symfs_filename));
Adrian Hunterafba19d2013-10-09 15:01:12 +03001402 }
Jiri Olsa2c7da8c2015-03-02 12:56:12 -05001403 } else if (dso__needs_decompress(dso)) {
1404 char tmp[PATH_MAX];
1405 struct kmod_path m;
1406 int fd;
1407 bool ret;
1408
1409 if (kmod_path__parse_ext(&m, symfs_filename))
Arnaldo Carvalho de Melo3caee092016-08-09 15:16:37 -03001410 goto out;
Jiri Olsa2c7da8c2015-03-02 12:56:12 -05001411
1412 snprintf(tmp, PATH_MAX, "/tmp/perf-kmod-XXXXXX");
1413
1414 fd = mkstemp(tmp);
1415 if (fd < 0) {
1416 free(m.ext);
Arnaldo Carvalho de Melo3caee092016-08-09 15:16:37 -03001417 goto out;
Jiri Olsa2c7da8c2015-03-02 12:56:12 -05001418 }
1419
1420 ret = decompress_to_file(m.ext, symfs_filename, fd);
1421
Andi Kleen62ec9b32015-11-05 19:06:07 -08001422 if (ret)
1423 pr_err("Cannot decompress %s %s\n", m.ext, symfs_filename);
1424
Jiri Olsa2c7da8c2015-03-02 12:56:12 -05001425 free(m.ext);
1426 close(fd);
1427
1428 if (!ret)
Arnaldo Carvalho de Melo3caee092016-08-09 15:16:37 -03001429 goto out;
Jiri Olsa2c7da8c2015-03-02 12:56:12 -05001430
1431 strcpy(symfs_filename, tmp);
Adrian Hunterafba19d2013-10-09 15:01:12 +03001432 }
1433
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001434 snprintf(command, sizeof(command),
Maciek Borzecki7a4ec932012-09-04 12:32:30 +02001435 "%s %s%s --start-address=0x%016" PRIx64
Stephane Eranian3e6a2a72011-05-17 17:32:07 +02001436 " --stop-address=0x%016" PRIx64
Andi Kleene5924882014-11-12 18:05:26 -08001437 " -l -d %s %s -C %s 2>/dev/null|grep -v %s|expand",
Maciek Borzecki7a4ec932012-09-04 12:32:30 +02001438 objdump_path ? objdump_path : "objdump",
Andi Kleenf69b64f2011-09-15 14:31:41 -07001439 disassembler_style ? "-M " : "",
1440 disassembler_style ? disassembler_style : "",
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001441 map__rip_2objdump(map, sym->start),
Arnaldo Carvalho de Melo2c241bd2014-10-14 17:19:44 -03001442 map__rip_2objdump(map, sym->end),
Stephane Eranian3e6a2a72011-05-17 17:32:07 +02001443 symbol_conf.annotate_asm_raw ? "" : "--no-show-raw",
1444 symbol_conf.annotate_src ? "-S" : "",
Arnaldo Carvalho de Melo3caee092016-08-09 15:16:37 -03001445 symfs_filename, symfs_filename);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001446
1447 pr_debug("Executing: %s\n", command);
1448
Arnaldo Carvalho de Melo9955d0b2016-06-15 15:48:08 -03001449 err = -1;
1450 if (pipe(stdout_fd) < 0) {
1451 pr_err("Failure creating the pipe to run %s\n", command);
1452 goto out_remove_tmp;
1453 }
1454
1455 pid = fork();
1456 if (pid < 0) {
1457 pr_err("Failure forking to run %s\n", command);
1458 goto out_close_stdout;
1459 }
1460
1461 if (pid == 0) {
1462 close(stdout_fd[0]);
1463 dup2(stdout_fd[1], 1);
1464 close(stdout_fd[1]);
1465 execl("/bin/sh", "sh", "-c", command, NULL);
1466 perror(command);
1467 exit(-1);
1468 }
1469
1470 close(stdout_fd[1]);
1471
1472 file = fdopen(stdout_fd[0], "r");
Andi Kleen62ec9b32015-11-05 19:06:07 -08001473 if (!file) {
Arnaldo Carvalho de Melo9955d0b2016-06-15 15:48:08 -03001474 pr_err("Failure creating FILE stream for %s\n", command);
Andi Kleen62ec9b32015-11-05 19:06:07 -08001475 /*
1476 * If we were using debug info should retry with
1477 * original binary.
1478 */
Jiri Olsa2c7da8c2015-03-02 12:56:12 -05001479 goto out_remove_tmp;
Andi Kleen62ec9b32015-11-05 19:06:07 -08001480 }
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001481
Andi Kleen62ec9b32015-11-05 19:06:07 -08001482 nline = 0;
1483 while (!feof(file)) {
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -03001484 if (symbol__parse_objdump_line(sym, map, arch, file, privsize,
Andi Kleene5924882014-11-12 18:05:26 -08001485 &lineno) < 0)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001486 break;
Andi Kleen62ec9b32015-11-05 19:06:07 -08001487 nline++;
1488 }
1489
1490 if (nline == 0)
1491 pr_err("No output from %s\n", command);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001492
Adrian Hunter484a5e72013-08-07 14:38:56 +03001493 /*
1494 * kallsyms does not have symbol sizes so there may a nop at the end.
1495 * Remove it.
1496 */
1497 if (dso__is_kcore(dso))
1498 delete_last_nop(sym);
1499
Arnaldo Carvalho de Melo9955d0b2016-06-15 15:48:08 -03001500 fclose(file);
1501 err = 0;
Jiri Olsa2c7da8c2015-03-02 12:56:12 -05001502out_remove_tmp:
Arnaldo Carvalho de Melo9955d0b2016-06-15 15:48:08 -03001503 close(stdout_fd[0]);
1504
Jiri Olsa2c7da8c2015-03-02 12:56:12 -05001505 if (dso__needs_decompress(dso))
1506 unlink(symfs_filename);
Arnaldo Carvalho de Melo3caee092016-08-09 15:16:37 -03001507
Adrian Hunterafba19d2013-10-09 15:01:12 +03001508 if (delete_extract)
1509 kcore_extract__delete(&kce);
Arnaldo Carvalho de Meloc12944f2016-08-09 14:56:13 -03001510out:
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001511 return err;
Arnaldo Carvalho de Melo9955d0b2016-06-15 15:48:08 -03001512
1513out_close_stdout:
1514 close(stdout_fd[1]);
1515 goto out_remove_tmp;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001516}
1517
1518static void insert_source_line(struct rb_root *root, struct source_line *src_line)
1519{
1520 struct source_line *iter;
1521 struct rb_node **p = &root->rb_node;
1522 struct rb_node *parent = NULL;
Namhyung Kim1491c222013-03-05 14:53:28 +09001523 int i, ret;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001524
1525 while (*p != NULL) {
1526 parent = *p;
1527 iter = rb_entry(parent, struct source_line, node);
1528
Namhyung Kim41127962012-11-09 14:58:49 +09001529 ret = strcmp(iter->path, src_line->path);
1530 if (ret == 0) {
Namhyung Kim1491c222013-03-05 14:53:28 +09001531 for (i = 0; i < src_line->nr_pcnt; i++)
Arnaldo Carvalho de Melo276af92f2015-06-19 16:36:12 -03001532 iter->samples[i].percent_sum += src_line->samples[i].percent;
Namhyung Kim41127962012-11-09 14:58:49 +09001533 return;
1534 }
1535
1536 if (ret < 0)
1537 p = &(*p)->rb_left;
1538 else
1539 p = &(*p)->rb_right;
1540 }
1541
Namhyung Kim1491c222013-03-05 14:53:28 +09001542 for (i = 0; i < src_line->nr_pcnt; i++)
Arnaldo Carvalho de Melo276af92f2015-06-19 16:36:12 -03001543 src_line->samples[i].percent_sum = src_line->samples[i].percent;
Namhyung Kim41127962012-11-09 14:58:49 +09001544
1545 rb_link_node(&src_line->node, parent, p);
1546 rb_insert_color(&src_line->node, root);
1547}
1548
Namhyung Kim1491c222013-03-05 14:53:28 +09001549static int cmp_source_line(struct source_line *a, struct source_line *b)
1550{
1551 int i;
1552
1553 for (i = 0; i < a->nr_pcnt; i++) {
Arnaldo Carvalho de Melo276af92f2015-06-19 16:36:12 -03001554 if (a->samples[i].percent_sum == b->samples[i].percent_sum)
Namhyung Kim1491c222013-03-05 14:53:28 +09001555 continue;
Arnaldo Carvalho de Melo276af92f2015-06-19 16:36:12 -03001556 return a->samples[i].percent_sum > b->samples[i].percent_sum;
Namhyung Kim1491c222013-03-05 14:53:28 +09001557 }
1558
1559 return 0;
1560}
1561
Namhyung Kim41127962012-11-09 14:58:49 +09001562static void __resort_source_line(struct rb_root *root, struct source_line *src_line)
1563{
1564 struct source_line *iter;
1565 struct rb_node **p = &root->rb_node;
1566 struct rb_node *parent = NULL;
1567
1568 while (*p != NULL) {
1569 parent = *p;
1570 iter = rb_entry(parent, struct source_line, node);
1571
Namhyung Kim1491c222013-03-05 14:53:28 +09001572 if (cmp_source_line(src_line, iter))
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001573 p = &(*p)->rb_left;
1574 else
1575 p = &(*p)->rb_right;
1576 }
1577
1578 rb_link_node(&src_line->node, parent, p);
1579 rb_insert_color(&src_line->node, root);
1580}
1581
Namhyung Kim41127962012-11-09 14:58:49 +09001582static void resort_source_line(struct rb_root *dest_root, struct rb_root *src_root)
1583{
1584 struct source_line *src_line;
1585 struct rb_node *node;
1586
1587 node = rb_first(src_root);
1588 while (node) {
1589 struct rb_node *next;
1590
1591 src_line = rb_entry(node, struct source_line, node);
1592 next = rb_next(node);
1593 rb_erase(node, src_root);
1594
1595 __resort_source_line(dest_root, src_line);
1596 node = next;
1597 }
1598}
1599
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001600static void symbol__free_source_line(struct symbol *sym, int len)
1601{
1602 struct annotation *notes = symbol__annotation(sym);
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -02001603 struct source_line *src_line = notes->src->lines;
Namhyung Kim1491c222013-03-05 14:53:28 +09001604 size_t sizeof_src_line;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001605 int i;
1606
Namhyung Kim1491c222013-03-05 14:53:28 +09001607 sizeof_src_line = sizeof(*src_line) +
Arnaldo Carvalho de Melo276af92f2015-06-19 16:36:12 -03001608 (sizeof(src_line->samples) * (src_line->nr_pcnt - 1));
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001609
Namhyung Kim1491c222013-03-05 14:53:28 +09001610 for (i = 0; i < len; i++) {
Namhyung Kimf048d542013-09-11 14:09:28 +09001611 free_srcline(src_line->path);
Namhyung Kim1491c222013-03-05 14:53:28 +09001612 src_line = (void *)src_line + sizeof_src_line;
1613 }
1614
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001615 zfree(&notes->src->lines);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001616}
1617
1618/* Get the filename:line for the colored entries */
1619static int symbol__get_source_line(struct symbol *sym, struct map *map,
Namhyung Kimdb8fd072013-03-05 14:53:21 +09001620 struct perf_evsel *evsel,
Namhyung Kim86c98ca2013-09-11 14:09:30 +09001621 struct rb_root *root, int len)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001622{
1623 u64 start;
Namhyung Kim1491c222013-03-05 14:53:28 +09001624 int i, k;
1625 int evidx = evsel->idx;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001626 struct source_line *src_line;
1627 struct annotation *notes = symbol__annotation(sym);
Namhyung Kim1491c222013-03-05 14:53:28 +09001628 struct sym_hist *h = annotation__histogram(notes, evidx);
Namhyung Kim41127962012-11-09 14:58:49 +09001629 struct rb_root tmp_root = RB_ROOT;
Namhyung Kim1491c222013-03-05 14:53:28 +09001630 int nr_pcnt = 1;
1631 u64 h_sum = h->sum;
1632 size_t sizeof_src_line = sizeof(struct source_line);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001633
Namhyung Kim1491c222013-03-05 14:53:28 +09001634 if (perf_evsel__is_group_event(evsel)) {
1635 for (i = 1; i < evsel->nr_members; i++) {
1636 h = annotation__histogram(notes, evidx + i);
1637 h_sum += h->sum;
1638 }
1639 nr_pcnt = evsel->nr_members;
Arnaldo Carvalho de Melo276af92f2015-06-19 16:36:12 -03001640 sizeof_src_line += (nr_pcnt - 1) * sizeof(src_line->samples);
Namhyung Kim1491c222013-03-05 14:53:28 +09001641 }
1642
1643 if (!h_sum)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001644 return 0;
1645
Namhyung Kim1491c222013-03-05 14:53:28 +09001646 src_line = notes->src->lines = calloc(len, sizeof_src_line);
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -02001647 if (!notes->src->lines)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001648 return -1;
1649
David Millerf40a0632012-03-25 16:28:12 -04001650 start = map__rip_2objdump(map, sym->start);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001651
1652 for (i = 0; i < len; i++) {
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001653 u64 offset;
Namhyung Kim1491c222013-03-05 14:53:28 +09001654 double percent_max = 0.0;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001655
Namhyung Kim1491c222013-03-05 14:53:28 +09001656 src_line->nr_pcnt = nr_pcnt;
1657
1658 for (k = 0; k < nr_pcnt; k++) {
1659 h = annotation__histogram(notes, evidx + k);
Arnaldo Carvalho de Melo276af92f2015-06-19 16:36:12 -03001660 src_line->samples[k].percent = 100.0 * h->addr[i] / h->sum;
Namhyung Kim1491c222013-03-05 14:53:28 +09001661
Arnaldo Carvalho de Melo276af92f2015-06-19 16:36:12 -03001662 if (src_line->samples[k].percent > percent_max)
1663 percent_max = src_line->samples[k].percent;
Namhyung Kim1491c222013-03-05 14:53:28 +09001664 }
1665
1666 if (percent_max <= 0.5)
1667 goto next;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001668
1669 offset = start + i;
Andi Kleen85c116a2014-11-12 18:05:27 -08001670 src_line->path = get_srcline(map->dso, offset, NULL, false);
Namhyung Kim1491c222013-03-05 14:53:28 +09001671 insert_source_line(&tmp_root, src_line);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001672
Namhyung Kim1491c222013-03-05 14:53:28 +09001673 next:
1674 src_line = (void *)src_line + sizeof_src_line;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001675 }
1676
Namhyung Kim41127962012-11-09 14:58:49 +09001677 resort_source_line(root, &tmp_root);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001678 return 0;
1679}
1680
1681static void print_summary(struct rb_root *root, const char *filename)
1682{
1683 struct source_line *src_line;
1684 struct rb_node *node;
1685
1686 printf("\nSorted summary for file %s\n", filename);
1687 printf("----------------------------------------------\n\n");
1688
1689 if (RB_EMPTY_ROOT(root)) {
1690 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
1691 return;
1692 }
1693
1694 node = rb_first(root);
1695 while (node) {
Namhyung Kim1491c222013-03-05 14:53:28 +09001696 double percent, percent_max = 0.0;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001697 const char *color;
1698 char *path;
Namhyung Kim1491c222013-03-05 14:53:28 +09001699 int i;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001700
1701 src_line = rb_entry(node, struct source_line, node);
Namhyung Kim1491c222013-03-05 14:53:28 +09001702 for (i = 0; i < src_line->nr_pcnt; i++) {
Arnaldo Carvalho de Melo276af92f2015-06-19 16:36:12 -03001703 percent = src_line->samples[i].percent_sum;
Namhyung Kim1491c222013-03-05 14:53:28 +09001704 color = get_percent_color(percent);
1705 color_fprintf(stdout, color, " %7.2f", percent);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001706
Namhyung Kim1491c222013-03-05 14:53:28 +09001707 if (percent > percent_max)
1708 percent_max = percent;
1709 }
1710
1711 path = src_line->path;
1712 color = get_percent_color(percent_max);
Namhyung Kimf048d542013-09-11 14:09:28 +09001713 color_fprintf(stdout, color, " %s\n", path);
Namhyung Kim1491c222013-03-05 14:53:28 +09001714
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001715 node = rb_next(node);
1716 }
1717}
1718
Namhyung Kimdb8fd072013-03-05 14:53:21 +09001719static void symbol__annotate_hits(struct symbol *sym, struct perf_evsel *evsel)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001720{
1721 struct annotation *notes = symbol__annotation(sym);
Namhyung Kimdb8fd072013-03-05 14:53:21 +09001722 struct sym_hist *h = annotation__histogram(notes, evsel->idx);
Arnaldo Carvalho de Melo1b2e2df2012-04-19 10:57:06 -03001723 u64 len = symbol__size(sym), offset;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001724
1725 for (offset = 0; offset < len; ++offset)
1726 if (h->addr[offset] != 0)
1727 printf("%*" PRIx64 ": %" PRIu64 "\n", BITS_PER_LONG / 2,
1728 sym->start + offset, h->addr[offset]);
1729 printf("%*s: %" PRIu64 "\n", BITS_PER_LONG / 2, "h->sum", h->sum);
1730}
1731
Namhyung Kimdb8fd072013-03-05 14:53:21 +09001732int symbol__annotate_printf(struct symbol *sym, struct map *map,
1733 struct perf_evsel *evsel, bool full_paths,
1734 int min_pcnt, int max_lines, int context)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001735{
1736 struct dso *dso = map->dso;
David Ahernbfd14b92012-09-08 09:06:50 -06001737 char *filename;
1738 const char *d_filename;
Arnaldo Carvalho de Melo9cdbadce2014-03-18 11:50:21 -03001739 const char *evsel_name = perf_evsel__name(evsel);
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -02001740 struct annotation *notes = symbol__annotation(sym);
Peter Zijlstra (Intel)135cce12016-06-30 10:29:55 +02001741 struct sym_hist *h = annotation__histogram(notes, evsel->idx);
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -03001742 struct disasm_line *pos, *queue = NULL;
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -03001743 u64 start = map__rip_2objdump(map, sym->start);
Arnaldo Carvalho de Melod5e3d742011-02-08 15:29:25 -02001744 int printed = 2, queue_len = 0;
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -02001745 int more = 0;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001746 u64 len;
Namhyung Kimb1dd4432013-03-05 14:53:25 +09001747 int width = 8;
Peter Zijlstra (Intel)53dd9b52016-06-30 09:17:26 -03001748 int graph_dotted_len;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001749
David Ahernbfd14b92012-09-08 09:06:50 -06001750 filename = strdup(dso->long_name);
1751 if (!filename)
1752 return -ENOMEM;
1753
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001754 if (full_paths)
1755 d_filename = filename;
1756 else
1757 d_filename = basename(filename);
1758
Arnaldo Carvalho de Melo1b2e2df2012-04-19 10:57:06 -03001759 len = symbol__size(sym);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001760
Namhyung Kim759ff492013-03-05 14:53:26 +09001761 if (perf_evsel__is_group_event(evsel))
Namhyung Kimb1dd4432013-03-05 14:53:25 +09001762 width *= evsel->nr_members;
1763
Peter Zijlstra (Intel)135cce12016-06-30 10:29:55 +02001764 graph_dotted_len = printf(" %-*.*s| Source code & Disassembly of %s for %s (%" PRIu64 " samples)\n",
1765 width, width, "Percent", d_filename, evsel_name, h->sum);
Arnaldo Carvalho de Melo9cdbadce2014-03-18 11:50:21 -03001766
Peter Zijlstra (Intel)53dd9b52016-06-30 09:17:26 -03001767 printf("%-*.*s----\n",
Arnaldo Carvalho de Melo9cdbadce2014-03-18 11:50:21 -03001768 graph_dotted_len, graph_dotted_len, graph_dotted_line);
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -02001769
1770 if (verbose)
Namhyung Kimdb8fd072013-03-05 14:53:21 +09001771 symbol__annotate_hits(sym, evsel);
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -02001772
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -02001773 list_for_each_entry(pos, &notes->src->source, node) {
Arnaldo Carvalho de Melod5e3d742011-02-08 15:29:25 -02001774 if (context && queue == NULL) {
1775 queue = pos;
1776 queue_len = 0;
1777 }
1778
Namhyung Kimdb8fd072013-03-05 14:53:21 +09001779 switch (disasm_line__print(pos, sym, start, evsel, len,
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -03001780 min_pcnt, printed, max_lines,
1781 queue)) {
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -02001782 case 0:
1783 ++printed;
Arnaldo Carvalho de Melod5e3d742011-02-08 15:29:25 -02001784 if (context) {
1785 printed += queue_len;
1786 queue = NULL;
1787 queue_len = 0;
1788 }
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -02001789 break;
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -02001790 case 1:
1791 /* filtered by max_lines */
1792 ++more;
1793 break;
1794 case -1:
1795 default:
Arnaldo Carvalho de Melod5e3d742011-02-08 15:29:25 -02001796 /*
1797 * Filtered by min_pcnt or non IP lines when
1798 * context != 0
1799 */
1800 if (!context)
1801 break;
1802 if (queue_len == context)
1803 queue = list_entry(queue->node.next, typeof(*queue), node);
1804 else
1805 ++queue_len;
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -02001806 break;
1807 }
1808 }
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -02001809
David Ahernbfd14b92012-09-08 09:06:50 -06001810 free(filename);
1811
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -02001812 return more;
1813}
1814
1815void symbol__annotate_zero_histogram(struct symbol *sym, int evidx)
1816{
1817 struct annotation *notes = symbol__annotation(sym);
1818 struct sym_hist *h = annotation__histogram(notes, evidx);
1819
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -02001820 memset(h, 0, notes->src->sizeof_sym_hist);
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -02001821}
1822
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -02001823void symbol__annotate_decay_histogram(struct symbol *sym, int evidx)
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -02001824{
1825 struct annotation *notes = symbol__annotation(sym);
1826 struct sym_hist *h = annotation__histogram(notes, evidx);
Arnaldo Carvalho de Melo1b2e2df2012-04-19 10:57:06 -03001827 int len = symbol__size(sym), offset;
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -02001828
1829 h->sum = 0;
Arnaldo Carvalho de Melo8b84a562012-04-05 16:15:59 -03001830 for (offset = 0; offset < len; ++offset) {
1831 h->addr[offset] = h->addr[offset] * 7 / 8;
1832 h->sum += h->addr[offset];
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -02001833 }
1834}
1835
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -03001836void disasm__purge(struct list_head *head)
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -02001837{
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -03001838 struct disasm_line *pos, *n;
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -02001839
1840 list_for_each_entry_safe(pos, n, head, node) {
1841 list_del(&pos->node);
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -03001842 disasm_line__free(pos);
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -02001843 }
1844}
1845
Arnaldo Carvalho de Melo51454182012-04-15 15:52:18 -03001846static size_t disasm_line__fprintf(struct disasm_line *dl, FILE *fp)
1847{
1848 size_t printed;
1849
1850 if (dl->offset == -1)
1851 return fprintf(fp, "%s\n", dl->line);
1852
Arnaldo Carvalho de Melo75b49202016-11-24 11:16:06 -03001853 printed = fprintf(fp, "%#" PRIx64 " %s", dl->offset, dl->ins.name);
Arnaldo Carvalho de Melo51454182012-04-15 15:52:18 -03001854
Arnaldo Carvalho de Meloc7e6ead2012-04-20 14:38:46 -03001855 if (dl->ops.raw[0] != '\0') {
Arnaldo Carvalho de Melo51454182012-04-15 15:52:18 -03001856 printed += fprintf(fp, "%.*s %s\n", 6 - (int)printed, " ",
Arnaldo Carvalho de Meloc7e6ead2012-04-20 14:38:46 -03001857 dl->ops.raw);
Arnaldo Carvalho de Melo51454182012-04-15 15:52:18 -03001858 }
1859
1860 return printed + fprintf(fp, "\n");
1861}
1862
1863size_t disasm__fprintf(struct list_head *head, FILE *fp)
1864{
1865 struct disasm_line *pos;
1866 size_t printed = 0;
1867
1868 list_for_each_entry(pos, head, node)
1869 printed += disasm_line__fprintf(pos, fp);
1870
1871 return printed;
1872}
1873
Namhyung Kimdb8fd072013-03-05 14:53:21 +09001874int symbol__tty_annotate(struct symbol *sym, struct map *map,
1875 struct perf_evsel *evsel, bool print_lines,
1876 bool full_paths, int min_pcnt, int max_lines)
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -02001877{
1878 struct dso *dso = map->dso;
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -02001879 struct rb_root source_line = RB_ROOT;
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -02001880 u64 len;
1881
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -03001882 if (symbol__disassemble(sym, map, perf_evsel__env_arch(evsel), 0) < 0)
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -02001883 return -1;
1884
Arnaldo Carvalho de Melo1b2e2df2012-04-19 10:57:06 -03001885 len = symbol__size(sym);
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -02001886
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001887 if (print_lines) {
Michael Petlan4a4c03c2015-11-09 16:33:31 +01001888 srcline_full_filename = full_paths;
Namhyung Kim86c98ca2013-09-11 14:09:30 +09001889 symbol__get_source_line(sym, map, evsel, &source_line, len);
1890 print_summary(&source_line, dso->long_name);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001891 }
1892
Namhyung Kimdb8fd072013-03-05 14:53:21 +09001893 symbol__annotate_printf(sym, map, evsel, full_paths,
Arnaldo Carvalho de Melod5e3d742011-02-08 15:29:25 -02001894 min_pcnt, max_lines, 0);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001895 if (print_lines)
1896 symbol__free_source_line(sym, len);
1897
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -03001898 disasm__purge(&symbol__annotation(sym)->src->source);
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -02001899
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001900 return 0;
1901}
Arnaldo Carvalho de Melof626adf2013-12-18 17:10:15 -03001902
Namhyung Kim48c65bd2014-02-20 10:32:53 +09001903bool ui__has_annotation(void)
1904{
Jiri Olsa2e0453a2016-05-03 13:54:44 +02001905 return use_browser == 1 && perf_hpp_list.sym;
Namhyung Kim48c65bd2014-02-20 10:32:53 +09001906}