blob: 72769762ece90f017af1a6d23e0430277d2b5937 [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 Melo7a997fe2012-05-12 13:15:34 -030031static struct ins *ins__find(const char *name);
32static int disasm_line__parse(char *line, char **namep, char **rawp);
33
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -030034struct arch {
35 const char *name;
36 struct {
37 char comment_char;
Arnaldo Carvalho de Melo9c2fb452016-11-16 15:50:38 -030038 char skip_functions_char;
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -030039 } objdump;
40};
41
42static struct arch architectures[] = {
43 {
44 .name = "arm",
45 .objdump = {
46 .comment_char = ';',
Arnaldo Carvalho de Melo9c2fb452016-11-16 15:50:38 -030047 .skip_functions_char = '+',
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -030048 },
49 },
50 {
51 .name = "x86",
52 .objdump = {
53 .comment_char = '#',
54 },
55 },
56};
57
Arnaldo Carvalho de Meloc46219ac2012-05-12 13:26:20 -030058static void ins__delete(struct ins_operands *ops)
59{
Arnaldo Carvalho de Melo39956142015-03-05 15:27:28 -030060 if (ops == NULL)
61 return;
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -030062 zfree(&ops->source.raw);
63 zfree(&ops->source.name);
64 zfree(&ops->target.raw);
65 zfree(&ops->target.name);
Arnaldo Carvalho de Meloc46219ac2012-05-12 13:26:20 -030066}
67
Arnaldo Carvalho de Melo54170722012-05-07 18:54:16 -030068static int ins__raw_scnprintf(struct ins *ins, char *bf, size_t size,
69 struct ins_operands *ops)
70{
71 return scnprintf(bf, size, "%-6.6s %s", ins->name, ops->raw);
72}
73
74int ins__scnprintf(struct ins *ins, char *bf, size_t size,
75 struct ins_operands *ops)
76{
77 if (ins->ops->scnprintf)
78 return ins->ops->scnprintf(ins, bf, size, ops);
79
80 return ins__raw_scnprintf(ins, bf, size, ops);
81}
82
Arnaldo Carvalho de Melo9c2fb452016-11-16 15:50:38 -030083static int call__parse(struct arch *arch, struct ins_operands *ops, struct map *map)
Arnaldo Carvalho de Melod86b0592012-04-18 16:07:38 -030084{
Arnaldo Carvalho de Melod2232882012-04-20 15:26:47 -030085 char *endptr, *tok, *name;
86
Arnaldo Carvalho de Melo44d1a3e2012-04-25 08:00:23 -030087 ops->target.addr = strtoull(ops->raw, &endptr, 16);
Arnaldo Carvalho de Melod2232882012-04-20 15:26:47 -030088
89 name = strchr(endptr, '<');
90 if (name == NULL)
91 goto indirect_call;
92
93 name++;
94
Arnaldo Carvalho de Melo9c2fb452016-11-16 15:50:38 -030095 if (arch->objdump.skip_functions_char &&
96 strchr(name, arch->objdump.skip_functions_char))
Russell Kingcfef25b2015-12-06 23:07:13 +000097 return -1;
Russell Kingcfef25b2015-12-06 23:07:13 +000098
Arnaldo Carvalho de Melod2232882012-04-20 15:26:47 -030099 tok = strchr(name, '>');
100 if (tok == NULL)
101 return -1;
102
103 *tok = '\0';
Arnaldo Carvalho de Melo44d1a3e2012-04-25 08:00:23 -0300104 ops->target.name = strdup(name);
Arnaldo Carvalho de Melod2232882012-04-20 15:26:47 -0300105 *tok = '>';
106
Arnaldo Carvalho de Melo44d1a3e2012-04-25 08:00:23 -0300107 return ops->target.name == NULL ? -1 : 0;
Arnaldo Carvalho de Melod2232882012-04-20 15:26:47 -0300108
109indirect_call:
Ravi Bangoria88a7fcf2016-08-19 18:29:35 +0530110 tok = strchr(endptr, '*');
111 if (tok == NULL) {
Arnaldo Carvalho de Melo5f62d4f2016-09-19 17:26:11 -0300112 struct symbol *sym = map__find_symbol(map, map->map_ip(map, ops->target.addr));
113 if (sym != NULL)
114 ops->target.name = strdup(sym->name);
115 else
116 ops->target.addr = 0;
Arnaldo Carvalho de Meloe8ea1562012-05-11 12:28:55 -0300117 return 0;
118 }
119
Arnaldo Carvalho de Melo44d1a3e2012-04-25 08:00:23 -0300120 ops->target.addr = strtoull(tok + 1, NULL, 16);
Arnaldo Carvalho de Melod86b0592012-04-18 16:07:38 -0300121 return 0;
122}
123
Arnaldo Carvalho de Melod2232882012-04-20 15:26:47 -0300124static int call__scnprintf(struct ins *ins, char *bf, size_t size,
Arnaldo Carvalho de Melo54170722012-05-07 18:54:16 -0300125 struct ins_operands *ops)
Arnaldo Carvalho de Melod2232882012-04-20 15:26:47 -0300126{
Arnaldo Carvalho de Melo44d1a3e2012-04-25 08:00:23 -0300127 if (ops->target.name)
128 return scnprintf(bf, size, "%-6.6s %s", ins->name, ops->target.name);
Arnaldo Carvalho de Melod2232882012-04-20 15:26:47 -0300129
Arnaldo Carvalho de Meloe8ea1562012-05-11 12:28:55 -0300130 if (ops->target.addr == 0)
131 return ins__raw_scnprintf(ins, bf, size, ops);
132
Arnaldo Carvalho de Melo44d1a3e2012-04-25 08:00:23 -0300133 return scnprintf(bf, size, "%-6.6s *%" PRIx64, ins->name, ops->target.addr);
Arnaldo Carvalho de Melod2232882012-04-20 15:26:47 -0300134}
135
Arnaldo Carvalho de Melod86b0592012-04-18 16:07:38 -0300136static struct ins_ops call_ops = {
Arnaldo Carvalho de Melod2232882012-04-20 15:26:47 -0300137 .parse = call__parse,
138 .scnprintf = call__scnprintf,
Arnaldo Carvalho de Melod86b0592012-04-18 16:07:38 -0300139};
140
141bool ins__is_call(const struct ins *ins)
142{
143 return ins->ops == &call_ops;
144}
145
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -0300146static 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 -0300147{
Arnaldo Carvalho de Meloc7e6ead2012-04-20 14:38:46 -0300148 const char *s = strchr(ops->raw, '+');
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -0300149
Adrian Hunterbbb7f842013-08-07 14:38:54 +0300150 ops->target.addr = strtoull(ops->raw, NULL, 16);
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -0300151
Arnaldo Carvalho de Melofb29fa582012-04-25 14:16:03 -0300152 if (s++ != NULL)
Adrian Hunterbbb7f842013-08-07 14:38:54 +0300153 ops->target.offset = strtoull(s, NULL, 16);
Arnaldo Carvalho de Melofb29fa582012-04-25 14:16:03 -0300154 else
155 ops->target.offset = UINT64_MAX;
156
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -0300157 return 0;
158}
159
Arnaldo Carvalho de Meloc7e6ead2012-04-20 14:38:46 -0300160static int jump__scnprintf(struct ins *ins, char *bf, size_t size,
Arnaldo Carvalho de Melo54170722012-05-07 18:54:16 -0300161 struct ins_operands *ops)
Arnaldo Carvalho de Melo28548d72012-04-19 10:16:27 -0300162{
Arnaldo Carvalho de Melo44d1a3e2012-04-25 08:00:23 -0300163 return scnprintf(bf, size, "%-6.6s %" PRIx64, ins->name, ops->target.offset);
Arnaldo Carvalho de Melo28548d72012-04-19 10:16:27 -0300164}
165
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -0300166static struct ins_ops jump_ops = {
Arnaldo Carvalho de Meloc7e6ead2012-04-20 14:38:46 -0300167 .parse = jump__parse,
168 .scnprintf = jump__scnprintf,
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -0300169};
170
171bool ins__is_jump(const struct ins *ins)
172{
173 return ins->ops == &jump_ops;
174}
175
Arnaldo Carvalho de Melo6de783b2012-05-11 16:48:49 -0300176static int comment__symbol(char *raw, char *comment, u64 *addrp, char **namep)
177{
178 char *endptr, *name, *t;
179
180 if (strstr(raw, "(%rip)") == NULL)
181 return 0;
182
183 *addrp = strtoull(comment, &endptr, 16);
184 name = strchr(endptr, '<');
185 if (name == NULL)
186 return -1;
187
188 name++;
189
190 t = strchr(name, '>');
191 if (t == NULL)
192 return 0;
193
194 *t = '\0';
195 *namep = strdup(name);
196 *t = '>';
197
198 return 0;
199}
200
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -0300201static int lock__parse(struct arch *arch, struct ins_operands *ops, struct map *map)
Arnaldo Carvalho de Melo7a997fe2012-05-12 13:15:34 -0300202{
203 char *name;
204
205 ops->locked.ops = zalloc(sizeof(*ops->locked.ops));
206 if (ops->locked.ops == NULL)
207 return 0;
208
209 if (disasm_line__parse(ops->raw, &name, &ops->locked.ops->raw) < 0)
210 goto out_free_ops;
211
Namhyung Kim2ba34aa2012-11-10 02:27:13 +0900212 ops->locked.ins = ins__find(name);
Rabin Vincent0fb9f2a2015-01-18 20:00:21 +0100213 free(name);
214
Namhyung Kim2ba34aa2012-11-10 02:27:13 +0900215 if (ops->locked.ins == NULL)
216 goto out_free_ops;
Arnaldo Carvalho de Melo7a997fe2012-05-12 13:15:34 -0300217
Namhyung Kim2ba34aa2012-11-10 02:27:13 +0900218 if (!ops->locked.ins->ops)
219 return 0;
Arnaldo Carvalho de Melo7a997fe2012-05-12 13:15:34 -0300220
Rabin Vincentbe819082015-01-18 20:00:20 +0100221 if (ops->locked.ins->ops->parse &&
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -0300222 ops->locked.ins->ops->parse(arch, ops->locked.ops, map) < 0)
Rabin Vincentbe819082015-01-18 20:00:20 +0100223 goto out_free_ops;
Arnaldo Carvalho de Melo7a997fe2012-05-12 13:15:34 -0300224
225 return 0;
226
227out_free_ops:
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300228 zfree(&ops->locked.ops);
Arnaldo Carvalho de Melo7a997fe2012-05-12 13:15:34 -0300229 return 0;
230}
231
232static int lock__scnprintf(struct ins *ins, char *bf, size_t size,
233 struct ins_operands *ops)
234{
235 int printed;
236
237 if (ops->locked.ins == NULL)
238 return ins__raw_scnprintf(ins, bf, size, ops);
239
240 printed = scnprintf(bf, size, "%-6.6s ", ins->name);
241 return printed + ins__scnprintf(ops->locked.ins, bf + printed,
242 size - printed, ops->locked.ops);
243}
244
Arnaldo Carvalho de Meloc46219ac2012-05-12 13:26:20 -0300245static void lock__delete(struct ins_operands *ops)
246{
Rabin Vincent0fb9f2a2015-01-18 20:00:21 +0100247 struct ins *ins = ops->locked.ins;
248
249 if (ins && ins->ops->free)
250 ins->ops->free(ops->locked.ops);
251 else
252 ins__delete(ops->locked.ops);
253
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300254 zfree(&ops->locked.ops);
255 zfree(&ops->target.raw);
256 zfree(&ops->target.name);
Arnaldo Carvalho de Meloc46219ac2012-05-12 13:26:20 -0300257}
258
Arnaldo Carvalho de Melo7a997fe2012-05-12 13:15:34 -0300259static struct ins_ops lock_ops = {
Arnaldo Carvalho de Meloc46219ac2012-05-12 13:26:20 -0300260 .free = lock__delete,
Arnaldo Carvalho de Melo7a997fe2012-05-12 13:15:34 -0300261 .parse = lock__parse,
262 .scnprintf = lock__scnprintf,
263};
264
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -0300265static int mov__parse(struct arch *arch, struct ins_operands *ops, struct map *map __maybe_unused)
Arnaldo Carvalho de Melo6de783b2012-05-11 16:48:49 -0300266{
267 char *s = strchr(ops->raw, ','), *target, *comment, prev;
268
269 if (s == NULL)
270 return -1;
271
272 *s = '\0';
273 ops->source.raw = strdup(ops->raw);
274 *s = ',';
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -0300275
Arnaldo Carvalho de Melo6de783b2012-05-11 16:48:49 -0300276 if (ops->source.raw == NULL)
277 return -1;
278
279 target = ++s;
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -0300280 comment = strchr(s, arch->objdump.comment_char);
Arnaldo Carvalho de Melo6de783b2012-05-11 16:48:49 -0300281
Alex Converse1e2bb042014-08-14 14:03:00 -0700282 if (comment != NULL)
283 s = comment - 1;
284 else
285 s = strchr(s, '\0') - 1;
286
287 while (s > target && isspace(s[0]))
288 --s;
289 s++;
Arnaldo Carvalho de Melo6de783b2012-05-11 16:48:49 -0300290 prev = *s;
291 *s = '\0';
292
293 ops->target.raw = strdup(target);
294 *s = prev;
295
296 if (ops->target.raw == NULL)
297 goto out_free_source;
298
Arnaldo Carvalho de Melo6de783b2012-05-11 16:48:49 -0300299 if (comment == NULL)
300 return 0;
301
302 while (comment[0] != '\0' && isspace(comment[0]))
303 ++comment;
304
305 comment__symbol(ops->source.raw, comment, &ops->source.addr, &ops->source.name);
306 comment__symbol(ops->target.raw, comment, &ops->target.addr, &ops->target.name);
307
308 return 0;
309
310out_free_source:
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300311 zfree(&ops->source.raw);
Arnaldo Carvalho de Melo6de783b2012-05-11 16:48:49 -0300312 return -1;
313}
314
315static int mov__scnprintf(struct ins *ins, char *bf, size_t size,
316 struct ins_operands *ops)
317{
318 return scnprintf(bf, size, "%-6.6s %s,%s", ins->name,
319 ops->source.name ?: ops->source.raw,
320 ops->target.name ?: ops->target.raw);
321}
322
323static struct ins_ops mov_ops = {
324 .parse = mov__parse,
325 .scnprintf = mov__scnprintf,
326};
327
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -0300328static 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 -0300329{
330 char *target, *comment, *s, prev;
331
332 target = s = ops->raw;
333
334 while (s[0] != '\0' && !isspace(s[0]))
335 ++s;
336 prev = *s;
337 *s = '\0';
338
339 ops->target.raw = strdup(target);
340 *s = prev;
341
342 if (ops->target.raw == NULL)
343 return -1;
344
345 comment = strchr(s, '#');
346 if (comment == NULL)
347 return 0;
348
349 while (comment[0] != '\0' && isspace(comment[0]))
350 ++comment;
351
352 comment__symbol(ops->target.raw, comment, &ops->target.addr, &ops->target.name);
353
354 return 0;
355}
356
357static int dec__scnprintf(struct ins *ins, char *bf, size_t size,
358 struct ins_operands *ops)
359{
360 return scnprintf(bf, size, "%-6.6s %s", ins->name,
361 ops->target.name ?: ops->target.raw);
362}
363
364static struct ins_ops dec_ops = {
365 .parse = dec__parse,
366 .scnprintf = dec__scnprintf,
367};
368
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300369static int nop__scnprintf(struct ins *ins __maybe_unused, char *bf, size_t size,
370 struct ins_operands *ops __maybe_unused)
Arnaldo Carvalho de Melob9818e92012-05-07 18:57:02 -0300371{
372 return scnprintf(bf, size, "%-6.6s", "nop");
373}
374
375static struct ins_ops nop_ops = {
376 .scnprintf = nop__scnprintf,
377};
378
Naveen N. Rao6ef94922016-06-24 17:23:58 +0530379static struct ins_ops ret_ops = {
380 .scnprintf = ins__raw_scnprintf,
381};
382
383bool ins__is_ret(const struct ins *ins)
384{
385 return ins->ops == &ret_ops;
386}
387
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -0300388static struct ins instructions[] = {
Arnaldo Carvalho de Melo6de783b2012-05-11 16:48:49 -0300389 { .name = "add", .ops = &mov_ops, },
390 { .name = "addl", .ops = &mov_ops, },
391 { .name = "addq", .ops = &mov_ops, },
392 { .name = "addw", .ops = &mov_ops, },
393 { .name = "and", .ops = &mov_ops, },
Russell Kingcfef25b2015-12-06 23:07:13 +0000394#ifdef __arm__
395 { .name = "b", .ops = &jump_ops, }, // might also be a call
396 { .name = "bcc", .ops = &jump_ops, },
397 { .name = "bcs", .ops = &jump_ops, },
398 { .name = "beq", .ops = &jump_ops, },
399 { .name = "bge", .ops = &jump_ops, },
400 { .name = "bgt", .ops = &jump_ops, },
401 { .name = "bhi", .ops = &jump_ops, },
402 { .name = "bl", .ops = &call_ops, },
Russell Kingcfef25b2015-12-06 23:07:13 +0000403 { .name = "bls", .ops = &jump_ops, },
Chris Ryder58c04002016-05-19 17:59:45 +0100404 { .name = "blt", .ops = &jump_ops, },
Russell Kingcfef25b2015-12-06 23:07:13 +0000405 { .name = "blx", .ops = &call_ops, },
406 { .name = "bne", .ops = &jump_ops, },
407#endif
Arnaldo Carvalho de Melo7a997fe2012-05-12 13:15:34 -0300408 { .name = "bts", .ops = &mov_ops, },
Arnaldo Carvalho de Melod86b0592012-04-18 16:07:38 -0300409 { .name = "call", .ops = &call_ops, },
410 { .name = "callq", .ops = &call_ops, },
Arnaldo Carvalho de Melo6de783b2012-05-11 16:48:49 -0300411 { .name = "cmp", .ops = &mov_ops, },
412 { .name = "cmpb", .ops = &mov_ops, },
413 { .name = "cmpl", .ops = &mov_ops, },
414 { .name = "cmpq", .ops = &mov_ops, },
415 { .name = "cmpw", .ops = &mov_ops, },
416 { .name = "cmpxch", .ops = &mov_ops, },
Arnaldo Carvalho de Meloa43712c2012-05-11 17:21:09 -0300417 { .name = "dec", .ops = &dec_ops, },
418 { .name = "decl", .ops = &dec_ops, },
Arnaldo Carvalho de Melo6de783b2012-05-11 16:48:49 -0300419 { .name = "imul", .ops = &mov_ops, },
Arnaldo Carvalho de Meloa43712c2012-05-11 17:21:09 -0300420 { .name = "inc", .ops = &dec_ops, },
421 { .name = "incl", .ops = &dec_ops, },
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -0300422 { .name = "ja", .ops = &jump_ops, },
Arnaldo Carvalho de Melo3f862fd2012-04-19 17:10:12 -0300423 { .name = "jae", .ops = &jump_ops, },
424 { .name = "jb", .ops = &jump_ops, },
425 { .name = "jbe", .ops = &jump_ops, },
426 { .name = "jc", .ops = &jump_ops, },
427 { .name = "jcxz", .ops = &jump_ops, },
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -0300428 { .name = "je", .ops = &jump_ops, },
Arnaldo Carvalho de Melo3f862fd2012-04-19 17:10:12 -0300429 { .name = "jecxz", .ops = &jump_ops, },
430 { .name = "jg", .ops = &jump_ops, },
431 { .name = "jge", .ops = &jump_ops, },
432 { .name = "jl", .ops = &jump_ops, },
433 { .name = "jle", .ops = &jump_ops, },
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -0300434 { .name = "jmp", .ops = &jump_ops, },
435 { .name = "jmpq", .ops = &jump_ops, },
Arnaldo Carvalho de Melo3f862fd2012-04-19 17:10:12 -0300436 { .name = "jna", .ops = &jump_ops, },
437 { .name = "jnae", .ops = &jump_ops, },
438 { .name = "jnb", .ops = &jump_ops, },
439 { .name = "jnbe", .ops = &jump_ops, },
440 { .name = "jnc", .ops = &jump_ops, },
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -0300441 { .name = "jne", .ops = &jump_ops, },
Arnaldo Carvalho de Melo3f862fd2012-04-19 17:10:12 -0300442 { .name = "jng", .ops = &jump_ops, },
443 { .name = "jnge", .ops = &jump_ops, },
444 { .name = "jnl", .ops = &jump_ops, },
445 { .name = "jnle", .ops = &jump_ops, },
446 { .name = "jno", .ops = &jump_ops, },
447 { .name = "jnp", .ops = &jump_ops, },
448 { .name = "jns", .ops = &jump_ops, },
449 { .name = "jnz", .ops = &jump_ops, },
450 { .name = "jo", .ops = &jump_ops, },
451 { .name = "jp", .ops = &jump_ops, },
452 { .name = "jpe", .ops = &jump_ops, },
453 { .name = "jpo", .ops = &jump_ops, },
454 { .name = "jrcxz", .ops = &jump_ops, },
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -0300455 { .name = "js", .ops = &jump_ops, },
Arnaldo Carvalho de Melo3f862fd2012-04-19 17:10:12 -0300456 { .name = "jz", .ops = &jump_ops, },
Arnaldo Carvalho de Melo6de783b2012-05-11 16:48:49 -0300457 { .name = "lea", .ops = &mov_ops, },
Arnaldo Carvalho de Melo7a997fe2012-05-12 13:15:34 -0300458 { .name = "lock", .ops = &lock_ops, },
Arnaldo Carvalho de Melo6de783b2012-05-11 16:48:49 -0300459 { .name = "mov", .ops = &mov_ops, },
460 { .name = "movb", .ops = &mov_ops, },
461 { .name = "movdqa",.ops = &mov_ops, },
462 { .name = "movl", .ops = &mov_ops, },
463 { .name = "movq", .ops = &mov_ops, },
464 { .name = "movslq", .ops = &mov_ops, },
465 { .name = "movzbl", .ops = &mov_ops, },
466 { .name = "movzwl", .ops = &mov_ops, },
Arnaldo Carvalho de Melob9818e92012-05-07 18:57:02 -0300467 { .name = "nop", .ops = &nop_ops, },
468 { .name = "nopl", .ops = &nop_ops, },
469 { .name = "nopw", .ops = &nop_ops, },
Arnaldo Carvalho de Melo6de783b2012-05-11 16:48:49 -0300470 { .name = "or", .ops = &mov_ops, },
471 { .name = "orl", .ops = &mov_ops, },
472 { .name = "test", .ops = &mov_ops, },
473 { .name = "testb", .ops = &mov_ops, },
474 { .name = "testl", .ops = &mov_ops, },
Arnaldo Carvalho de Melo7a997fe2012-05-12 13:15:34 -0300475 { .name = "xadd", .ops = &mov_ops, },
Andi Kleenffadcf02012-10-30 17:34:08 -0700476 { .name = "xbeginl", .ops = &jump_ops, },
477 { .name = "xbeginq", .ops = &jump_ops, },
Naveen N. Rao6ef94922016-06-24 17:23:58 +0530478 { .name = "retq", .ops = &ret_ops, },
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -0300479};
480
Chris Ryder7e4c1492016-05-19 17:59:46 +0100481static int ins__key_cmp(const void *name, const void *insp)
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -0300482{
483 const struct ins *ins = insp;
484
485 return strcmp(name, ins->name);
486}
487
Chris Ryder7e4c1492016-05-19 17:59:46 +0100488static int ins__cmp(const void *a, const void *b)
489{
490 const struct ins *ia = a;
491 const struct ins *ib = b;
492
493 return strcmp(ia->name, ib->name);
494}
495
496static void ins__sort(void)
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -0300497{
498 const int nmemb = ARRAY_SIZE(instructions);
499
Chris Ryder7e4c1492016-05-19 17:59:46 +0100500 qsort(instructions, nmemb, sizeof(struct ins), ins__cmp);
501}
502
503static struct ins *ins__find(const char *name)
504{
505 const int nmemb = ARRAY_SIZE(instructions);
506 static bool sorted;
507
508 if (!sorted) {
509 ins__sort();
510 sorted = true;
511 }
512
513 return bsearch(name, instructions, nmemb, sizeof(struct ins), ins__key_cmp);
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -0300514}
515
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -0300516static int arch__key_cmp(const void *name, const void *archp)
517{
518 const struct arch *arch = archp;
519
520 return strcmp(name, arch->name);
521}
522
523static int arch__cmp(const void *a, const void *b)
524{
525 const struct arch *aa = a;
526 const struct arch *ab = b;
527
528 return strcmp(aa->name, ab->name);
529}
530
531static void arch__sort(void)
532{
533 const int nmemb = ARRAY_SIZE(architectures);
534
535 qsort(architectures, nmemb, sizeof(struct arch), arch__cmp);
536}
537
538static struct arch *arch__find(const char *name)
539{
540 const int nmemb = ARRAY_SIZE(architectures);
541 static bool sorted;
542
543 if (!sorted) {
544 arch__sort();
545 sorted = true;
546 }
547
548 return bsearch(name, architectures, nmemb, sizeof(struct arch), arch__key_cmp);
549}
550
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -0200551int symbol__alloc_hist(struct symbol *sym)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200552{
553 struct annotation *notes = symbol__annotation(sym);
Arnaldo Carvalho de Melo1b2e2df2012-04-19 10:57:06 -0300554 const size_t size = symbol__size(sym);
Cody Schafer86963292012-07-19 20:05:25 -0700555 size_t sizeof_sym_hist;
556
557 /* Check for overflow when calculating sizeof_sym_hist */
558 if (size > (SIZE_MAX - sizeof(struct sym_hist)) / sizeof(u64))
559 return -1;
560
561 sizeof_sym_hist = (sizeof(struct sym_hist) + size * sizeof(u64));
562
563 /* Check for overflow in zalloc argument */
564 if (sizeof_sym_hist > (SIZE_MAX - sizeof(*notes->src))
565 / symbol_conf.nr_events)
566 return -1;
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200567
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -0200568 notes->src = zalloc(sizeof(*notes->src) + symbol_conf.nr_events * sizeof_sym_hist);
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200569 if (notes->src == NULL)
570 return -1;
571 notes->src->sizeof_sym_hist = sizeof_sym_hist;
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -0200572 notes->src->nr_histograms = symbol_conf.nr_events;
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200573 INIT_LIST_HEAD(&notes->src->source);
574 return 0;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200575}
576
Andi Kleend4957632015-07-18 08:24:48 -0700577/* The cycles histogram is lazily allocated. */
578static int symbol__alloc_hist_cycles(struct symbol *sym)
579{
580 struct annotation *notes = symbol__annotation(sym);
581 const size_t size = symbol__size(sym);
582
583 notes->src->cycles_hist = calloc(size, sizeof(struct cyc_hist));
584 if (notes->src->cycles_hist == NULL)
585 return -1;
586 return 0;
587}
588
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200589void symbol__annotate_zero_histograms(struct symbol *sym)
590{
591 struct annotation *notes = symbol__annotation(sym);
592
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200593 pthread_mutex_lock(&notes->lock);
Andi Kleend4957632015-07-18 08:24:48 -0700594 if (notes->src != NULL) {
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200595 memset(notes->src->histograms, 0,
596 notes->src->nr_histograms * notes->src->sizeof_sym_hist);
Andi Kleend4957632015-07-18 08:24:48 -0700597 if (notes->src->cycles_hist)
598 memset(notes->src->cycles_hist, 0,
599 symbol__size(sym) * sizeof(struct cyc_hist));
600 }
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200601 pthread_mutex_unlock(&notes->lock);
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200602}
603
Andi Kleend4957632015-07-18 08:24:48 -0700604static int __symbol__account_cycles(struct annotation *notes,
605 u64 start,
606 unsigned offset, unsigned cycles,
607 unsigned have_start)
608{
609 struct cyc_hist *ch;
610
611 ch = notes->src->cycles_hist;
612 /*
613 * For now we can only account one basic block per
614 * final jump. But multiple could be overlapping.
615 * Always account the longest one. So when
616 * a shorter one has been already seen throw it away.
617 *
618 * We separately always account the full cycles.
619 */
620 ch[offset].num_aggr++;
621 ch[offset].cycles_aggr += cycles;
622
623 if (!have_start && ch[offset].have_start)
624 return 0;
625 if (ch[offset].num) {
626 if (have_start && (!ch[offset].have_start ||
627 ch[offset].start > start)) {
628 ch[offset].have_start = 0;
629 ch[offset].cycles = 0;
630 ch[offset].num = 0;
631 if (ch[offset].reset < 0xffff)
632 ch[offset].reset++;
633 } else if (have_start &&
634 ch[offset].start < start)
635 return 0;
636 }
637 ch[offset].have_start = have_start;
638 ch[offset].start = start;
639 ch[offset].cycles += cycles;
640 ch[offset].num++;
641 return 0;
642}
643
Arnaldo Carvalho de Melob66d8c02013-12-18 15:37:41 -0300644static int __symbol__inc_addr_samples(struct symbol *sym, struct map *map,
645 struct annotation *notes, int evidx, u64 addr)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200646{
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200647 unsigned offset;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200648 struct sym_hist *h;
649
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200650 pr_debug3("%s: addr=%#" PRIx64 "\n", __func__, map->unmap_ip(map, addr));
651
Arnaldo Carvalho de Meloe3d006c2015-10-21 15:45:13 -0300652 if (addr < sym->start || addr >= sym->end) {
653 pr_debug("%s(%d): ERANGE! sym->name=%s, start=%#" PRIx64 ", addr=%#" PRIx64 ", end=%#" PRIx64 "\n",
654 __func__, __LINE__, sym->name, sym->start, addr, sym->end);
Arnaldo Carvalho de Melo31d68e72012-03-27 12:55:57 -0300655 return -ERANGE;
Arnaldo Carvalho de Meloe3d006c2015-10-21 15:45:13 -0300656 }
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200657
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200658 offset = addr - sym->start;
659 h = annotation__histogram(notes, evidx);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200660 h->sum++;
661 h->addr[offset]++;
662
663 pr_debug3("%#" PRIx64 " %s: period++ [addr: %#" PRIx64 ", %#" PRIx64
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200664 ", evidx=%d] => %" PRIu64 "\n", sym->start, sym->name,
665 addr, addr - sym->start, evidx, h->addr[offset]);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200666 return 0;
667}
668
Andi Kleend4957632015-07-18 08:24:48 -0700669static struct annotation *symbol__get_annotation(struct symbol *sym, bool cycles)
Andi Kleen83be34a2015-05-27 10:51:46 -0700670{
671 struct annotation *notes = symbol__annotation(sym);
672
673 if (notes->src == NULL) {
674 if (symbol__alloc_hist(sym) < 0)
675 return NULL;
676 }
Andi Kleend4957632015-07-18 08:24:48 -0700677 if (!notes->src->cycles_hist && cycles) {
678 if (symbol__alloc_hist_cycles(sym) < 0)
679 return NULL;
680 }
Andi Kleen83be34a2015-05-27 10:51:46 -0700681 return notes;
682}
683
Arnaldo Carvalho de Melo44e83032013-12-18 17:12:24 -0300684static int symbol__inc_addr_samples(struct symbol *sym, struct map *map,
685 int evidx, u64 addr)
Arnaldo Carvalho de Melob66d8c02013-12-18 15:37:41 -0300686{
687 struct annotation *notes;
688
Namhyung Kim48c65bd2014-02-20 10:32:53 +0900689 if (sym == NULL)
Arnaldo Carvalho de Melob66d8c02013-12-18 15:37:41 -0300690 return 0;
Andi Kleend4957632015-07-18 08:24:48 -0700691 notes = symbol__get_annotation(sym, false);
Andi Kleen83be34a2015-05-27 10:51:46 -0700692 if (notes == NULL)
693 return -ENOMEM;
Arnaldo Carvalho de Melob66d8c02013-12-18 15:37:41 -0300694 return __symbol__inc_addr_samples(sym, map, notes, evidx, addr);
695}
696
Andi Kleend4957632015-07-18 08:24:48 -0700697static int symbol__account_cycles(u64 addr, u64 start,
698 struct symbol *sym, unsigned cycles)
699{
700 struct annotation *notes;
701 unsigned offset;
702
703 if (sym == NULL)
704 return 0;
705 notes = symbol__get_annotation(sym, true);
706 if (notes == NULL)
707 return -ENOMEM;
708 if (addr < sym->start || addr >= sym->end)
709 return -ERANGE;
710
711 if (start) {
712 if (start < sym->start || start >= sym->end)
713 return -ERANGE;
714 if (start >= addr)
715 start = 0;
716 }
717 offset = addr - sym->start;
718 return __symbol__account_cycles(notes,
719 start ? start - sym->start : 0,
720 offset, cycles,
721 !!start);
722}
723
724int addr_map_symbol__account_cycles(struct addr_map_symbol *ams,
725 struct addr_map_symbol *start,
726 unsigned cycles)
727{
Adrian Hunter3d7245b2015-08-14 10:11:34 +0300728 u64 saddr = 0;
Andi Kleend4957632015-07-18 08:24:48 -0700729 int err;
730
731 if (!cycles)
732 return 0;
733
734 /*
735 * Only set start when IPC can be computed. We can only
736 * compute it when the basic block is completely in a single
737 * function.
738 * Special case the case when the jump is elsewhere, but
739 * it starts on the function start.
740 */
741 if (start &&
742 (start->sym == ams->sym ||
743 (ams->sym &&
744 start->addr == ams->sym->start + ams->map->start)))
745 saddr = start->al_addr;
746 if (saddr == 0)
Adrian Hunter3d7245b2015-08-14 10:11:34 +0300747 pr_debug2("BB with bad start: addr %"PRIx64" start %"PRIx64" sym %"PRIx64" saddr %"PRIx64"\n",
Andi Kleend4957632015-07-18 08:24:48 -0700748 ams->addr,
749 start ? start->addr : 0,
750 ams->sym ? ams->sym->start + ams->map->start : 0,
751 saddr);
752 err = symbol__account_cycles(ams->al_addr, saddr, ams->sym, cycles);
753 if (err)
754 pr_debug2("account_cycles failed %d\n", err);
755 return err;
756}
757
Arnaldo Carvalho de Melo0f4e7a22013-12-18 16:48:29 -0300758int addr_map_symbol__inc_samples(struct addr_map_symbol *ams, int evidx)
759{
760 return symbol__inc_addr_samples(ams->sym, ams->map, evidx, ams->al_addr);
761}
762
Arnaldo Carvalho de Melof626adf2013-12-18 17:10:15 -0300763int hist_entry__inc_addr_samples(struct hist_entry *he, int evidx, u64 ip)
764{
765 return symbol__inc_addr_samples(he->ms.sym, he->ms.map, evidx, ip);
766}
767
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -0300768static void disasm_line__init_ins(struct disasm_line *dl, struct arch *arch, struct map *map)
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -0300769{
770 dl->ins = ins__find(dl->name);
771
772 if (dl->ins == NULL)
773 return;
774
775 if (!dl->ins->ops)
776 return;
777
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -0300778 if (dl->ins->ops->parse && dl->ins->ops->parse(arch, &dl->ops, map) < 0)
Rabin Vincentbe819082015-01-18 20:00:20 +0100779 dl->ins = NULL;
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -0300780}
781
Arnaldo Carvalho de Melo7a997fe2012-05-12 13:15:34 -0300782static int disasm_line__parse(char *line, char **namep, char **rawp)
783{
784 char *name = line, tmp;
785
786 while (isspace(name[0]))
787 ++name;
788
789 if (name[0] == '\0')
790 return -1;
791
792 *rawp = name + 1;
793
794 while ((*rawp)[0] != '\0' && !isspace((*rawp)[0]))
795 ++*rawp;
796
797 tmp = (*rawp)[0];
798 (*rawp)[0] = '\0';
799 *namep = strdup(name);
800
801 if (*namep == NULL)
802 goto out_free_name;
803
804 (*rawp)[0] = tmp;
805
806 if ((*rawp)[0] != '\0') {
807 (*rawp)++;
808 while (isspace((*rawp)[0]))
809 ++(*rawp);
810 }
811
812 return 0;
813
814out_free_name:
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300815 zfree(namep);
Arnaldo Carvalho de Melo7a997fe2012-05-12 13:15:34 -0300816 return -1;
817}
818
Andi Kleene5924882014-11-12 18:05:26 -0800819static struct disasm_line *disasm_line__new(s64 offset, char *line,
Arnaldo Carvalho de Melobff5c302016-09-19 17:18:16 -0300820 size_t privsize, int line_nr,
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -0300821 struct arch *arch,
Arnaldo Carvalho de Melobff5c302016-09-19 17:18:16 -0300822 struct map *map)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200823{
Arnaldo Carvalho de Melo51454182012-04-15 15:52:18 -0300824 struct disasm_line *dl = zalloc(sizeof(*dl) + privsize);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200825
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300826 if (dl != NULL) {
827 dl->offset = offset;
828 dl->line = strdup(line);
Andi Kleene5924882014-11-12 18:05:26 -0800829 dl->line_nr = line_nr;
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300830 if (dl->line == NULL)
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -0300831 goto out_delete;
Arnaldo Carvalho de Melo51454182012-04-15 15:52:18 -0300832
833 if (offset != -1) {
Arnaldo Carvalho de Melo7a997fe2012-05-12 13:15:34 -0300834 if (disasm_line__parse(dl->line, &dl->name, &dl->ops.raw) < 0)
Arnaldo Carvalho de Melo51454182012-04-15 15:52:18 -0300835 goto out_free_line;
836
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -0300837 disasm_line__init_ins(dl, arch, map);
Arnaldo Carvalho de Melo51454182012-04-15 15:52:18 -0300838 }
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200839 }
840
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300841 return dl;
Arnaldo Carvalho de Melo51454182012-04-15 15:52:18 -0300842
843out_free_line:
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300844 zfree(&dl->line);
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -0300845out_delete:
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300846 free(dl);
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -0300847 return NULL;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200848}
849
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300850void disasm_line__free(struct disasm_line *dl)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200851{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300852 zfree(&dl->line);
853 zfree(&dl->name);
Arnaldo Carvalho de Meloc46219ac2012-05-12 13:26:20 -0300854 if (dl->ins && dl->ins->ops->free)
855 dl->ins->ops->free(&dl->ops);
856 else
857 ins__delete(&dl->ops);
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300858 free(dl);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200859}
860
Arnaldo Carvalho de Melo54170722012-05-07 18:54:16 -0300861int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size, bool raw)
862{
863 if (raw || !dl->ins)
864 return scnprintf(bf, size, "%-6.6s %s", dl->name, dl->ops.raw);
865
866 return ins__scnprintf(dl->ins, bf, size, &dl->ops);
867}
868
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300869static void disasm__add(struct list_head *head, struct disasm_line *line)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200870{
871 list_add_tail(&line->node, head);
872}
873
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300874struct disasm_line *disasm__get_next_ip_line(struct list_head *head, struct disasm_line *pos)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200875{
876 list_for_each_entry_continue(pos, head, node)
877 if (pos->offset >= 0)
878 return pos;
879
880 return NULL;
881}
882
Namhyung Kime64aa752013-03-05 14:53:30 +0900883double disasm__calc_percent(struct annotation *notes, int evidx, s64 offset,
Martin Liška0c4a5bc2015-06-19 16:10:43 -0300884 s64 end, const char **path, u64 *nr_samples)
Namhyung Kime5ccf9f2013-03-05 14:53:23 +0900885{
886 struct source_line *src_line = notes->src->lines;
Namhyung Kime5ccf9f2013-03-05 14:53:23 +0900887 double percent = 0.0;
Martin Liška0c4a5bc2015-06-19 16:10:43 -0300888 *nr_samples = 0;
Namhyung Kime5ccf9f2013-03-05 14:53:23 +0900889
Namhyung Kimbd64fcb2013-03-05 14:53:24 +0900890 if (src_line) {
Namhyung Kim1491c222013-03-05 14:53:28 +0900891 size_t sizeof_src_line = sizeof(*src_line) +
Arnaldo Carvalho de Melo276af92f2015-06-19 16:36:12 -0300892 sizeof(src_line->samples) * (src_line->nr_pcnt - 1);
Namhyung Kime5ccf9f2013-03-05 14:53:23 +0900893
Namhyung Kim1491c222013-03-05 14:53:28 +0900894 while (offset < end) {
895 src_line = (void *)notes->src->lines +
896 (sizeof_src_line * offset);
897
898 if (*path == NULL)
899 *path = src_line->path;
900
Arnaldo Carvalho de Melo276af92f2015-06-19 16:36:12 -0300901 percent += src_line->samples[evidx].percent;
902 *nr_samples += src_line->samples[evidx].nr;
Namhyung Kim1491c222013-03-05 14:53:28 +0900903 offset++;
Namhyung Kimbd64fcb2013-03-05 14:53:24 +0900904 }
905 } else {
Namhyung Kim1491c222013-03-05 14:53:28 +0900906 struct sym_hist *h = annotation__histogram(notes, evidx);
907 unsigned int hits = 0;
908
Namhyung Kimbd64fcb2013-03-05 14:53:24 +0900909 while (offset < end)
910 hits += h->addr[offset++];
911
Martin Liška0c4a5bc2015-06-19 16:10:43 -0300912 if (h->sum) {
913 *nr_samples = hits;
Namhyung Kimbd64fcb2013-03-05 14:53:24 +0900914 percent = 100.0 * hits / h->sum;
Martin Liška0c4a5bc2015-06-19 16:10:43 -0300915 }
Namhyung Kime5ccf9f2013-03-05 14:53:23 +0900916 }
917
Namhyung Kime5ccf9f2013-03-05 14:53:23 +0900918 return percent;
919}
920
Peter Zijlstra70fbe052016-09-05 16:08:12 -0300921static const char *annotate__address_color(struct block_range *br)
922{
923 double cov = block_range__coverage(br);
924
925 if (cov >= 0) {
926 /* mark red for >75% coverage */
927 if (cov > 0.75)
928 return PERF_COLOR_RED;
929
930 /* mark dull for <1% coverage */
931 if (cov < 0.01)
932 return PERF_COLOR_NORMAL;
933 }
934
935 return PERF_COLOR_MAGENTA;
936}
937
938static const char *annotate__asm_color(struct block_range *br)
939{
940 double cov = block_range__coverage(br);
941
942 if (cov >= 0) {
943 /* mark dull for <1% coverage */
944 if (cov < 0.01)
945 return PERF_COLOR_NORMAL;
946 }
947
948 return PERF_COLOR_BLUE;
949}
950
951static void annotate__branch_printf(struct block_range *br, u64 addr)
952{
953 bool emit_comment = true;
954
955 if (!br)
956 return;
957
958#if 1
959 if (br->is_target && br->start == addr) {
960 struct block_range *branch = br;
961 double p;
962
963 /*
964 * Find matching branch to our target.
965 */
966 while (!branch->is_branch)
967 branch = block_range__next(branch);
968
969 p = 100 *(double)br->entry / branch->coverage;
970
971 if (p > 0.1) {
972 if (emit_comment) {
973 emit_comment = false;
974 printf("\t#");
975 }
976
977 /*
978 * The percentage of coverage joined at this target in relation
979 * to the next branch.
980 */
981 printf(" +%.2f%%", p);
982 }
983 }
984#endif
985 if (br->is_branch && br->end == addr) {
986 double p = 100*(double)br->taken / br->coverage;
987
988 if (p > 0.1) {
989 if (emit_comment) {
990 emit_comment = false;
991 printf("\t#");
992 }
993
994 /*
995 * The percentage of coverage leaving at this branch, and
996 * its prediction ratio.
997 */
998 printf(" -%.2f%% (p:%.2f%%)", p, 100*(double)br->pred / br->taken);
999 }
1000 }
1001}
1002
1003
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -03001004static int disasm_line__print(struct disasm_line *dl, struct symbol *sym, u64 start,
Namhyung Kimdb8fd072013-03-05 14:53:21 +09001005 struct perf_evsel *evsel, u64 len, int min_pcnt, int printed,
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -03001006 int max_lines, struct disasm_line *queue)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001007{
1008 static const char *prev_line;
1009 static const char *prev_color;
1010
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -03001011 if (dl->offset != -1) {
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001012 const char *path = NULL;
Martin Liška0c4a5bc2015-06-19 16:10:43 -03001013 u64 nr_samples;
Namhyung Kimb1dd4432013-03-05 14:53:25 +09001014 double percent, max_percent = 0.0;
1015 double *ppercents = &percent;
Martin Liška0c4a5bc2015-06-19 16:10:43 -03001016 u64 *psamples = &nr_samples;
Namhyung Kimb1dd4432013-03-05 14:53:25 +09001017 int i, nr_percent = 1;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001018 const char *color;
1019 struct annotation *notes = symbol__annotation(sym);
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -03001020 s64 offset = dl->offset;
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -03001021 const u64 addr = start + offset;
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -03001022 struct disasm_line *next;
Peter Zijlstra70fbe052016-09-05 16:08:12 -03001023 struct block_range *br;
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -02001024
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -03001025 next = disasm__get_next_ip_line(&notes->src->source, dl);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001026
Namhyung Kim759ff492013-03-05 14:53:26 +09001027 if (perf_evsel__is_group_event(evsel)) {
Namhyung Kimb1dd4432013-03-05 14:53:25 +09001028 nr_percent = evsel->nr_members;
1029 ppercents = calloc(nr_percent, sizeof(double));
Martin Liška0c4a5bc2015-06-19 16:10:43 -03001030 psamples = calloc(nr_percent, sizeof(u64));
1031 if (ppercents == NULL || psamples == NULL) {
Namhyung Kimb1dd4432013-03-05 14:53:25 +09001032 return -1;
Martin Liška0c4a5bc2015-06-19 16:10:43 -03001033 }
Namhyung Kimb1dd4432013-03-05 14:53:25 +09001034 }
1035
1036 for (i = 0; i < nr_percent; i++) {
1037 percent = disasm__calc_percent(notes,
Namhyung Kim1491c222013-03-05 14:53:28 +09001038 notes->src->lines ? i : evsel->idx + i,
1039 offset,
1040 next ? next->offset : (s64) len,
Martin Liška0c4a5bc2015-06-19 16:10:43 -03001041 &path, &nr_samples);
Namhyung Kimb1dd4432013-03-05 14:53:25 +09001042
1043 ppercents[i] = percent;
Martin Liška0c4a5bc2015-06-19 16:10:43 -03001044 psamples[i] = nr_samples;
Namhyung Kimb1dd4432013-03-05 14:53:25 +09001045 if (percent > max_percent)
1046 max_percent = percent;
1047 }
1048
1049 if (max_percent < min_pcnt)
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -02001050 return -1;
1051
Arnaldo Carvalho de Meloe3087b82011-02-08 15:01:39 -02001052 if (max_lines && printed >= max_lines)
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -02001053 return 1;
Arnaldo Carvalho de Melod040bd32011-02-05 15:37:31 -02001054
Arnaldo Carvalho de Melod5e3d742011-02-08 15:29:25 -02001055 if (queue != NULL) {
1056 list_for_each_entry_from(queue, &notes->src->source, node) {
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -03001057 if (queue == dl)
Arnaldo Carvalho de Melod5e3d742011-02-08 15:29:25 -02001058 break;
Namhyung Kimdb8fd072013-03-05 14:53:21 +09001059 disasm_line__print(queue, sym, start, evsel, len,
Arnaldo Carvalho de Melod5e3d742011-02-08 15:29:25 -02001060 0, 0, 1, NULL);
1061 }
1062 }
1063
Namhyung Kimb1dd4432013-03-05 14:53:25 +09001064 color = get_percent_color(max_percent);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001065
1066 /*
1067 * Also color the filename and line if needed, with
1068 * the same color than the percentage. Don't print it
1069 * twice for close colored addr with the same filename:line
1070 */
1071 if (path) {
1072 if (!prev_line || strcmp(prev_line, path)
1073 || color != prev_color) {
1074 color_fprintf(stdout, color, " %s", path);
1075 prev_line = path;
1076 prev_color = color;
1077 }
1078 }
1079
Namhyung Kimb1dd4432013-03-05 14:53:25 +09001080 for (i = 0; i < nr_percent; i++) {
1081 percent = ppercents[i];
Martin Liška0c4a5bc2015-06-19 16:10:43 -03001082 nr_samples = psamples[i];
Namhyung Kimb1dd4432013-03-05 14:53:25 +09001083 color = get_percent_color(percent);
Martin Liška0c4a5bc2015-06-19 16:10:43 -03001084
1085 if (symbol_conf.show_total_period)
1086 color_fprintf(stdout, color, " %7" PRIu64,
1087 nr_samples);
1088 else
1089 color_fprintf(stdout, color, " %7.2f", percent);
Namhyung Kimb1dd4432013-03-05 14:53:25 +09001090 }
1091
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001092 printf(" : ");
Peter Zijlstra70fbe052016-09-05 16:08:12 -03001093
1094 br = block_range__find(addr);
1095 color_fprintf(stdout, annotate__address_color(br), " %" PRIx64 ":", addr);
1096 color_fprintf(stdout, annotate__asm_color(br), "%s", dl->line);
1097 annotate__branch_printf(br, addr);
1098 printf("\n");
Namhyung Kimb1dd4432013-03-05 14:53:25 +09001099
1100 if (ppercents != &percent)
1101 free(ppercents);
1102
Martin Liška0c4a5bc2015-06-19 16:10:43 -03001103 if (psamples != &nr_samples)
1104 free(psamples);
1105
Arnaldo Carvalho de Meloe3087b82011-02-08 15:01:39 -02001106 } else if (max_lines && printed >= max_lines)
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -02001107 return 1;
1108 else {
Namhyung Kimb1dd4432013-03-05 14:53:25 +09001109 int width = 8;
1110
Arnaldo Carvalho de Melod5e3d742011-02-08 15:29:25 -02001111 if (queue)
1112 return -1;
1113
Namhyung Kim759ff492013-03-05 14:53:26 +09001114 if (perf_evsel__is_group_event(evsel))
Namhyung Kimb1dd4432013-03-05 14:53:25 +09001115 width *= evsel->nr_members;
1116
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -03001117 if (!*dl->line)
Namhyung Kimb1dd4432013-03-05 14:53:25 +09001118 printf(" %*s:\n", width, " ");
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001119 else
Namhyung Kimb1dd4432013-03-05 14:53:25 +09001120 printf(" %*s: %s\n", width, " ", dl->line);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001121 }
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -02001122
1123 return 0;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001124}
1125
Namhyung Kim3aec1502013-03-05 14:53:22 +09001126/*
1127 * symbol__parse_objdump_line() parses objdump output (with -d --no-show-raw)
1128 * which looks like following
1129 *
1130 * 0000000000415500 <_init>:
1131 * 415500: sub $0x8,%rsp
1132 * 415504: mov 0x2f5ad5(%rip),%rax # 70afe0 <_DYNAMIC+0x2f8>
1133 * 41550b: test %rax,%rax
1134 * 41550e: je 415515 <_init+0x15>
1135 * 415510: callq 416e70 <__gmon_start__@plt>
1136 * 415515: add $0x8,%rsp
1137 * 415519: retq
1138 *
1139 * it will be parsed and saved into struct disasm_line as
1140 * <offset> <name> <ops.raw>
1141 *
1142 * The offset will be a relative offset from the start of the symbol and -1
1143 * means that it's not a disassembly line so should be treated differently.
1144 * The ops.raw part will be parsed further according to type of the instruction.
1145 */
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -02001146static int symbol__parse_objdump_line(struct symbol *sym, struct map *map,
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -03001147 struct arch *arch,
Andi Kleene5924882014-11-12 18:05:26 -08001148 FILE *file, size_t privsize,
1149 int *line_nr)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001150{
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -02001151 struct annotation *notes = symbol__annotation(sym);
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -03001152 struct disasm_line *dl;
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -03001153 char *line = NULL, *parsed_line, *tmp, *tmp2, *c;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001154 size_t line_len;
1155 s64 line_ip, offset = -1;
Andi Kleene5924882014-11-12 18:05:26 -08001156 regmatch_t match[2];
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001157
1158 if (getline(&line, &line_len, file) < 0)
1159 return -1;
1160
1161 if (!line)
1162 return -1;
1163
1164 while (line_len != 0 && isspace(line[line_len - 1]))
1165 line[--line_len] = '\0';
1166
1167 c = strchr(line, '\n');
1168 if (c)
1169 *c = 0;
1170
1171 line_ip = -1;
Namhyung Kima31b7cc2012-04-11 17:04:59 -03001172 parsed_line = line;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001173
Andi Kleene5924882014-11-12 18:05:26 -08001174 /* /filename:linenr ? Save line number and ignore. */
1175 if (regexec(&file_lineno, line, 2, match, 0) == 0) {
1176 *line_nr = atoi(line + match[1].rm_so);
1177 return 0;
1178 }
1179
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001180 /*
1181 * Strip leading spaces:
1182 */
1183 tmp = line;
1184 while (*tmp) {
1185 if (*tmp != ' ')
1186 break;
1187 tmp++;
1188 }
1189
1190 if (*tmp) {
1191 /*
1192 * Parse hexa addresses followed by ':'
1193 */
1194 line_ip = strtoull(tmp, &tmp2, 16);
1195 if (*tmp2 != ':' || tmp == tmp2 || tmp2[1] == '\0')
1196 line_ip = -1;
1197 }
1198
1199 if (line_ip != -1) {
1200 u64 start = map__rip_2objdump(map, sym->start),
1201 end = map__rip_2objdump(map, sym->end);
1202
1203 offset = line_ip - start;
Arnaldo Carvalho de Melo2c241bd2014-10-14 17:19:44 -03001204 if ((u64)line_ip < start || (u64)line_ip >= end)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001205 offset = -1;
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -03001206 else
1207 parsed_line = tmp2 + 1;
Namhyung Kima31b7cc2012-04-11 17:04:59 -03001208 }
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001209
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -03001210 dl = disasm_line__new(offset, parsed_line, privsize, *line_nr, arch, map);
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -03001211 free(line);
Andi Kleene5924882014-11-12 18:05:26 -08001212 (*line_nr)++;
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -03001213
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -03001214 if (dl == NULL)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001215 return -1;
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -03001216
Adrian Hunterbbb7f842013-08-07 14:38:54 +03001217 if (dl->ops.target.offset == UINT64_MAX)
1218 dl->ops.target.offset = dl->ops.target.addr -
1219 map__rip_2objdump(map, sym->start);
1220
Adrian Hunter6e427ab2013-10-14 13:43:40 +03001221 /* kcore has no symbols, so add the call target name */
Adrian Hunterb1781702013-08-07 14:38:57 +03001222 if (dl->ins && ins__is_call(dl->ins) && !dl->ops.target.name) {
Adrian Hunter6e427ab2013-10-14 13:43:40 +03001223 struct addr_map_symbol target = {
1224 .map = map,
1225 .addr = dl->ops.target.addr,
1226 };
Adrian Hunterb1781702013-08-07 14:38:57 +03001227
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001228 if (!map_groups__find_ams(&target) &&
Adrian Hunter6e427ab2013-10-14 13:43:40 +03001229 target.sym->start == target.al_addr)
1230 dl->ops.target.name = strdup(target.sym->name);
Adrian Hunterb1781702013-08-07 14:38:57 +03001231 }
1232
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -03001233 disasm__add(&notes->src->source, dl);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001234
1235 return 0;
1236}
1237
Andi Kleene5924882014-11-12 18:05:26 -08001238static __attribute__((constructor)) void symbol__init_regexpr(void)
1239{
1240 regcomp(&file_lineno, "^/[^:]+:([0-9]+)", REG_EXTENDED);
1241}
1242
Adrian Hunter484a5e72013-08-07 14:38:56 +03001243static void delete_last_nop(struct symbol *sym)
1244{
1245 struct annotation *notes = symbol__annotation(sym);
1246 struct list_head *list = &notes->src->source;
1247 struct disasm_line *dl;
1248
1249 while (!list_empty(list)) {
1250 dl = list_entry(list->prev, struct disasm_line, node);
1251
1252 if (dl->ins && dl->ins->ops) {
1253 if (dl->ins->ops != &nop_ops)
1254 return;
1255 } else {
1256 if (!strstr(dl->line, " nop ") &&
1257 !strstr(dl->line, " nopl ") &&
1258 !strstr(dl->line, " nopw "))
1259 return;
1260 }
1261
1262 list_del(&dl->node);
1263 disasm_line__free(dl);
1264 }
1265}
1266
Arnaldo Carvalho de Meloee51d852016-07-29 16:27:18 -03001267int symbol__strerror_disassemble(struct symbol *sym __maybe_unused, struct map *map,
1268 int errnum, char *buf, size_t buflen)
1269{
1270 struct dso *dso = map->dso;
1271
1272 BUG_ON(buflen == 0);
1273
1274 if (errnum >= 0) {
1275 str_error_r(errnum, buf, buflen);
1276 return 0;
1277 }
1278
1279 switch (errnum) {
1280 case SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX: {
1281 char bf[SBUILD_ID_SIZE + 15] = " with build id ";
1282 char *build_id_msg = NULL;
1283
1284 if (dso->has_build_id) {
1285 build_id__sprintf(dso->build_id,
1286 sizeof(dso->build_id), bf + 15);
1287 build_id_msg = bf;
1288 }
1289 scnprintf(buf, buflen,
1290 "No vmlinux file%s\nwas found in the path.\n\n"
1291 "Note that annotation using /proc/kcore requires CAP_SYS_RAWIO capability.\n\n"
1292 "Please use:\n\n"
1293 " perf buildid-cache -vu vmlinux\n\n"
1294 "or:\n\n"
1295 " --vmlinux vmlinux\n", build_id_msg ?: "");
1296 }
1297 break;
1298 default:
1299 scnprintf(buf, buflen, "Internal error: Invalid %d error code\n", errnum);
1300 break;
1301 }
1302
1303 return 0;
1304}
1305
Arnaldo Carvalho de Melo05ed3ac2016-08-09 15:32:53 -03001306static int dso__disassemble_filename(struct dso *dso, char *filename, size_t filename_size)
1307{
1308 char linkname[PATH_MAX];
1309 char *build_id_filename;
1310
1311 if (dso->symtab_type == DSO_BINARY_TYPE__KALLSYMS &&
1312 !dso__is_kcore(dso))
1313 return SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX;
1314
1315 build_id_filename = dso__build_id_filename(dso, NULL, 0);
1316 if (build_id_filename) {
1317 __symbol__join_symfs(filename, filename_size, build_id_filename);
1318 free(build_id_filename);
1319 } else {
1320 if (dso->has_build_id)
1321 return ENOMEM;
1322 goto fallback;
1323 }
1324
1325 if (dso__is_kcore(dso) ||
1326 readlink(filename, linkname, sizeof(linkname)) < 0 ||
1327 strstr(linkname, DSO__NAME_KALLSYMS) ||
1328 access(filename, R_OK)) {
1329fallback:
1330 /*
1331 * If we don't have build-ids or the build-id file isn't in the
1332 * cache, or is just a kallsyms file, well, lets hope that this
1333 * DSO is the same as when 'perf record' ran.
1334 */
1335 __symbol__join_symfs(filename, filename_size, dso->long_name);
1336 }
1337
1338 return 0;
1339}
1340
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -03001341static const char *annotate__norm_arch(const char *arch_name)
1342{
1343 struct utsname uts;
1344
1345 if (!arch_name) { /* Assume we are annotating locally. */
1346 if (uname(&uts) < 0)
1347 return NULL;
1348 arch_name = uts.machine;
1349 }
1350 return normalize_arch((char *)arch_name);
1351}
1352
1353int symbol__disassemble(struct symbol *sym, struct map *map, const char *arch_name, size_t privsize)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001354{
1355 struct dso *dso = map->dso;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001356 char command[PATH_MAX * 2];
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -03001357 struct arch *arch = NULL;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001358 FILE *file;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001359 char symfs_filename[PATH_MAX];
Adrian Hunterafba19d2013-10-09 15:01:12 +03001360 struct kcore_extract kce;
1361 bool delete_extract = false;
Arnaldo Carvalho de Melo9955d0b2016-06-15 15:48:08 -03001362 int stdout_fd[2];
Andi Kleene5924882014-11-12 18:05:26 -08001363 int lineno = 0;
Andi Kleen62ec9b32015-11-05 19:06:07 -08001364 int nline;
Arnaldo Carvalho de Melo9955d0b2016-06-15 15:48:08 -03001365 pid_t pid;
Arnaldo Carvalho de Melo05ed3ac2016-08-09 15:32:53 -03001366 int err = dso__disassemble_filename(dso, symfs_filename, sizeof(symfs_filename));
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001367
Arnaldo Carvalho de Melo05ed3ac2016-08-09 15:32:53 -03001368 if (err)
1369 return err;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001370
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -03001371 arch_name = annotate__norm_arch(arch_name);
1372 if (!arch_name)
1373 return -1;
1374
1375 arch = arch__find(arch_name);
1376 if (arch == NULL)
1377 return -ENOTSUP;
1378
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001379 pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__,
Arnaldo Carvalho de Melo3caee092016-08-09 15:16:37 -03001380 symfs_filename, sym->name, map->unmap_ip(map, sym->start),
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001381 map->unmap_ip(map, sym->end));
1382
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001383 pr_debug("annotating [%p] %30s : [%p] %30s\n",
1384 dso, dso->long_name, sym, sym->name);
1385
Adrian Hunterafba19d2013-10-09 15:01:12 +03001386 if (dso__is_kcore(dso)) {
1387 kce.kcore_filename = symfs_filename;
1388 kce.addr = map__rip_2objdump(map, sym->start);
1389 kce.offs = sym->start;
Arnaldo Carvalho de Melo2c241bd2014-10-14 17:19:44 -03001390 kce.len = sym->end - sym->start;
Adrian Hunterafba19d2013-10-09 15:01:12 +03001391 if (!kcore_extract__create(&kce)) {
1392 delete_extract = true;
1393 strlcpy(symfs_filename, kce.extract_filename,
1394 sizeof(symfs_filename));
Adrian Hunterafba19d2013-10-09 15:01:12 +03001395 }
Jiri Olsa2c7da8c2015-03-02 12:56:12 -05001396 } else if (dso__needs_decompress(dso)) {
1397 char tmp[PATH_MAX];
1398 struct kmod_path m;
1399 int fd;
1400 bool ret;
1401
1402 if (kmod_path__parse_ext(&m, symfs_filename))
Arnaldo Carvalho de Melo3caee092016-08-09 15:16:37 -03001403 goto out;
Jiri Olsa2c7da8c2015-03-02 12:56:12 -05001404
1405 snprintf(tmp, PATH_MAX, "/tmp/perf-kmod-XXXXXX");
1406
1407 fd = mkstemp(tmp);
1408 if (fd < 0) {
1409 free(m.ext);
Arnaldo Carvalho de Melo3caee092016-08-09 15:16:37 -03001410 goto out;
Jiri Olsa2c7da8c2015-03-02 12:56:12 -05001411 }
1412
1413 ret = decompress_to_file(m.ext, symfs_filename, fd);
1414
Andi Kleen62ec9b32015-11-05 19:06:07 -08001415 if (ret)
1416 pr_err("Cannot decompress %s %s\n", m.ext, symfs_filename);
1417
Jiri Olsa2c7da8c2015-03-02 12:56:12 -05001418 free(m.ext);
1419 close(fd);
1420
1421 if (!ret)
Arnaldo Carvalho de Melo3caee092016-08-09 15:16:37 -03001422 goto out;
Jiri Olsa2c7da8c2015-03-02 12:56:12 -05001423
1424 strcpy(symfs_filename, tmp);
Adrian Hunterafba19d2013-10-09 15:01:12 +03001425 }
1426
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001427 snprintf(command, sizeof(command),
Maciek Borzecki7a4ec932012-09-04 12:32:30 +02001428 "%s %s%s --start-address=0x%016" PRIx64
Stephane Eranian3e6a2a72011-05-17 17:32:07 +02001429 " --stop-address=0x%016" PRIx64
Andi Kleene5924882014-11-12 18:05:26 -08001430 " -l -d %s %s -C %s 2>/dev/null|grep -v %s|expand",
Maciek Borzecki7a4ec932012-09-04 12:32:30 +02001431 objdump_path ? objdump_path : "objdump",
Andi Kleenf69b64f2011-09-15 14:31:41 -07001432 disassembler_style ? "-M " : "",
1433 disassembler_style ? disassembler_style : "",
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001434 map__rip_2objdump(map, sym->start),
Arnaldo Carvalho de Melo2c241bd2014-10-14 17:19:44 -03001435 map__rip_2objdump(map, sym->end),
Stephane Eranian3e6a2a72011-05-17 17:32:07 +02001436 symbol_conf.annotate_asm_raw ? "" : "--no-show-raw",
1437 symbol_conf.annotate_src ? "-S" : "",
Arnaldo Carvalho de Melo3caee092016-08-09 15:16:37 -03001438 symfs_filename, symfs_filename);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001439
1440 pr_debug("Executing: %s\n", command);
1441
Arnaldo Carvalho de Melo9955d0b2016-06-15 15:48:08 -03001442 err = -1;
1443 if (pipe(stdout_fd) < 0) {
1444 pr_err("Failure creating the pipe to run %s\n", command);
1445 goto out_remove_tmp;
1446 }
1447
1448 pid = fork();
1449 if (pid < 0) {
1450 pr_err("Failure forking to run %s\n", command);
1451 goto out_close_stdout;
1452 }
1453
1454 if (pid == 0) {
1455 close(stdout_fd[0]);
1456 dup2(stdout_fd[1], 1);
1457 close(stdout_fd[1]);
1458 execl("/bin/sh", "sh", "-c", command, NULL);
1459 perror(command);
1460 exit(-1);
1461 }
1462
1463 close(stdout_fd[1]);
1464
1465 file = fdopen(stdout_fd[0], "r");
Andi Kleen62ec9b32015-11-05 19:06:07 -08001466 if (!file) {
Arnaldo Carvalho de Melo9955d0b2016-06-15 15:48:08 -03001467 pr_err("Failure creating FILE stream for %s\n", command);
Andi Kleen62ec9b32015-11-05 19:06:07 -08001468 /*
1469 * If we were using debug info should retry with
1470 * original binary.
1471 */
Jiri Olsa2c7da8c2015-03-02 12:56:12 -05001472 goto out_remove_tmp;
Andi Kleen62ec9b32015-11-05 19:06:07 -08001473 }
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001474
Andi Kleen62ec9b32015-11-05 19:06:07 -08001475 nline = 0;
1476 while (!feof(file)) {
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -03001477 if (symbol__parse_objdump_line(sym, map, arch, file, privsize,
Andi Kleene5924882014-11-12 18:05:26 -08001478 &lineno) < 0)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001479 break;
Andi Kleen62ec9b32015-11-05 19:06:07 -08001480 nline++;
1481 }
1482
1483 if (nline == 0)
1484 pr_err("No output from %s\n", command);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001485
Adrian Hunter484a5e72013-08-07 14:38:56 +03001486 /*
1487 * kallsyms does not have symbol sizes so there may a nop at the end.
1488 * Remove it.
1489 */
1490 if (dso__is_kcore(dso))
1491 delete_last_nop(sym);
1492
Arnaldo Carvalho de Melo9955d0b2016-06-15 15:48:08 -03001493 fclose(file);
1494 err = 0;
Jiri Olsa2c7da8c2015-03-02 12:56:12 -05001495out_remove_tmp:
Arnaldo Carvalho de Melo9955d0b2016-06-15 15:48:08 -03001496 close(stdout_fd[0]);
1497
Jiri Olsa2c7da8c2015-03-02 12:56:12 -05001498 if (dso__needs_decompress(dso))
1499 unlink(symfs_filename);
Arnaldo Carvalho de Melo3caee092016-08-09 15:16:37 -03001500
Adrian Hunterafba19d2013-10-09 15:01:12 +03001501 if (delete_extract)
1502 kcore_extract__delete(&kce);
Arnaldo Carvalho de Meloc12944f2016-08-09 14:56:13 -03001503out:
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001504 return err;
Arnaldo Carvalho de Melo9955d0b2016-06-15 15:48:08 -03001505
1506out_close_stdout:
1507 close(stdout_fd[1]);
1508 goto out_remove_tmp;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001509}
1510
1511static void insert_source_line(struct rb_root *root, struct source_line *src_line)
1512{
1513 struct source_line *iter;
1514 struct rb_node **p = &root->rb_node;
1515 struct rb_node *parent = NULL;
Namhyung Kim1491c222013-03-05 14:53:28 +09001516 int i, ret;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001517
1518 while (*p != NULL) {
1519 parent = *p;
1520 iter = rb_entry(parent, struct source_line, node);
1521
Namhyung Kim41127962012-11-09 14:58:49 +09001522 ret = strcmp(iter->path, src_line->path);
1523 if (ret == 0) {
Namhyung Kim1491c222013-03-05 14:53:28 +09001524 for (i = 0; i < src_line->nr_pcnt; i++)
Arnaldo Carvalho de Melo276af92f2015-06-19 16:36:12 -03001525 iter->samples[i].percent_sum += src_line->samples[i].percent;
Namhyung Kim41127962012-11-09 14:58:49 +09001526 return;
1527 }
1528
1529 if (ret < 0)
1530 p = &(*p)->rb_left;
1531 else
1532 p = &(*p)->rb_right;
1533 }
1534
Namhyung Kim1491c222013-03-05 14:53:28 +09001535 for (i = 0; i < src_line->nr_pcnt; i++)
Arnaldo Carvalho de Melo276af92f2015-06-19 16:36:12 -03001536 src_line->samples[i].percent_sum = src_line->samples[i].percent;
Namhyung Kim41127962012-11-09 14:58:49 +09001537
1538 rb_link_node(&src_line->node, parent, p);
1539 rb_insert_color(&src_line->node, root);
1540}
1541
Namhyung Kim1491c222013-03-05 14:53:28 +09001542static int cmp_source_line(struct source_line *a, struct source_line *b)
1543{
1544 int i;
1545
1546 for (i = 0; i < a->nr_pcnt; i++) {
Arnaldo Carvalho de Melo276af92f2015-06-19 16:36:12 -03001547 if (a->samples[i].percent_sum == b->samples[i].percent_sum)
Namhyung Kim1491c222013-03-05 14:53:28 +09001548 continue;
Arnaldo Carvalho de Melo276af92f2015-06-19 16:36:12 -03001549 return a->samples[i].percent_sum > b->samples[i].percent_sum;
Namhyung Kim1491c222013-03-05 14:53:28 +09001550 }
1551
1552 return 0;
1553}
1554
Namhyung Kim41127962012-11-09 14:58:49 +09001555static void __resort_source_line(struct rb_root *root, struct source_line *src_line)
1556{
1557 struct source_line *iter;
1558 struct rb_node **p = &root->rb_node;
1559 struct rb_node *parent = NULL;
1560
1561 while (*p != NULL) {
1562 parent = *p;
1563 iter = rb_entry(parent, struct source_line, node);
1564
Namhyung Kim1491c222013-03-05 14:53:28 +09001565 if (cmp_source_line(src_line, iter))
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001566 p = &(*p)->rb_left;
1567 else
1568 p = &(*p)->rb_right;
1569 }
1570
1571 rb_link_node(&src_line->node, parent, p);
1572 rb_insert_color(&src_line->node, root);
1573}
1574
Namhyung Kim41127962012-11-09 14:58:49 +09001575static void resort_source_line(struct rb_root *dest_root, struct rb_root *src_root)
1576{
1577 struct source_line *src_line;
1578 struct rb_node *node;
1579
1580 node = rb_first(src_root);
1581 while (node) {
1582 struct rb_node *next;
1583
1584 src_line = rb_entry(node, struct source_line, node);
1585 next = rb_next(node);
1586 rb_erase(node, src_root);
1587
1588 __resort_source_line(dest_root, src_line);
1589 node = next;
1590 }
1591}
1592
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001593static void symbol__free_source_line(struct symbol *sym, int len)
1594{
1595 struct annotation *notes = symbol__annotation(sym);
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -02001596 struct source_line *src_line = notes->src->lines;
Namhyung Kim1491c222013-03-05 14:53:28 +09001597 size_t sizeof_src_line;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001598 int i;
1599
Namhyung Kim1491c222013-03-05 14:53:28 +09001600 sizeof_src_line = sizeof(*src_line) +
Arnaldo Carvalho de Melo276af92f2015-06-19 16:36:12 -03001601 (sizeof(src_line->samples) * (src_line->nr_pcnt - 1));
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001602
Namhyung Kim1491c222013-03-05 14:53:28 +09001603 for (i = 0; i < len; i++) {
Namhyung Kimf048d542013-09-11 14:09:28 +09001604 free_srcline(src_line->path);
Namhyung Kim1491c222013-03-05 14:53:28 +09001605 src_line = (void *)src_line + sizeof_src_line;
1606 }
1607
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001608 zfree(&notes->src->lines);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001609}
1610
1611/* Get the filename:line for the colored entries */
1612static int symbol__get_source_line(struct symbol *sym, struct map *map,
Namhyung Kimdb8fd072013-03-05 14:53:21 +09001613 struct perf_evsel *evsel,
Namhyung Kim86c98ca2013-09-11 14:09:30 +09001614 struct rb_root *root, int len)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001615{
1616 u64 start;
Namhyung Kim1491c222013-03-05 14:53:28 +09001617 int i, k;
1618 int evidx = evsel->idx;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001619 struct source_line *src_line;
1620 struct annotation *notes = symbol__annotation(sym);
Namhyung Kim1491c222013-03-05 14:53:28 +09001621 struct sym_hist *h = annotation__histogram(notes, evidx);
Namhyung Kim41127962012-11-09 14:58:49 +09001622 struct rb_root tmp_root = RB_ROOT;
Namhyung Kim1491c222013-03-05 14:53:28 +09001623 int nr_pcnt = 1;
1624 u64 h_sum = h->sum;
1625 size_t sizeof_src_line = sizeof(struct source_line);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001626
Namhyung Kim1491c222013-03-05 14:53:28 +09001627 if (perf_evsel__is_group_event(evsel)) {
1628 for (i = 1; i < evsel->nr_members; i++) {
1629 h = annotation__histogram(notes, evidx + i);
1630 h_sum += h->sum;
1631 }
1632 nr_pcnt = evsel->nr_members;
Arnaldo Carvalho de Melo276af92f2015-06-19 16:36:12 -03001633 sizeof_src_line += (nr_pcnt - 1) * sizeof(src_line->samples);
Namhyung Kim1491c222013-03-05 14:53:28 +09001634 }
1635
1636 if (!h_sum)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001637 return 0;
1638
Namhyung Kim1491c222013-03-05 14:53:28 +09001639 src_line = notes->src->lines = calloc(len, sizeof_src_line);
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -02001640 if (!notes->src->lines)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001641 return -1;
1642
David Millerf40a0632012-03-25 16:28:12 -04001643 start = map__rip_2objdump(map, sym->start);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001644
1645 for (i = 0; i < len; i++) {
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001646 u64 offset;
Namhyung Kim1491c222013-03-05 14:53:28 +09001647 double percent_max = 0.0;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001648
Namhyung Kim1491c222013-03-05 14:53:28 +09001649 src_line->nr_pcnt = nr_pcnt;
1650
1651 for (k = 0; k < nr_pcnt; k++) {
1652 h = annotation__histogram(notes, evidx + k);
Arnaldo Carvalho de Melo276af92f2015-06-19 16:36:12 -03001653 src_line->samples[k].percent = 100.0 * h->addr[i] / h->sum;
Namhyung Kim1491c222013-03-05 14:53:28 +09001654
Arnaldo Carvalho de Melo276af92f2015-06-19 16:36:12 -03001655 if (src_line->samples[k].percent > percent_max)
1656 percent_max = src_line->samples[k].percent;
Namhyung Kim1491c222013-03-05 14:53:28 +09001657 }
1658
1659 if (percent_max <= 0.5)
1660 goto next;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001661
1662 offset = start + i;
Andi Kleen85c116a2014-11-12 18:05:27 -08001663 src_line->path = get_srcline(map->dso, offset, NULL, false);
Namhyung Kim1491c222013-03-05 14:53:28 +09001664 insert_source_line(&tmp_root, src_line);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001665
Namhyung Kim1491c222013-03-05 14:53:28 +09001666 next:
1667 src_line = (void *)src_line + sizeof_src_line;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001668 }
1669
Namhyung Kim41127962012-11-09 14:58:49 +09001670 resort_source_line(root, &tmp_root);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001671 return 0;
1672}
1673
1674static void print_summary(struct rb_root *root, const char *filename)
1675{
1676 struct source_line *src_line;
1677 struct rb_node *node;
1678
1679 printf("\nSorted summary for file %s\n", filename);
1680 printf("----------------------------------------------\n\n");
1681
1682 if (RB_EMPTY_ROOT(root)) {
1683 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
1684 return;
1685 }
1686
1687 node = rb_first(root);
1688 while (node) {
Namhyung Kim1491c222013-03-05 14:53:28 +09001689 double percent, percent_max = 0.0;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001690 const char *color;
1691 char *path;
Namhyung Kim1491c222013-03-05 14:53:28 +09001692 int i;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001693
1694 src_line = rb_entry(node, struct source_line, node);
Namhyung Kim1491c222013-03-05 14:53:28 +09001695 for (i = 0; i < src_line->nr_pcnt; i++) {
Arnaldo Carvalho de Melo276af92f2015-06-19 16:36:12 -03001696 percent = src_line->samples[i].percent_sum;
Namhyung Kim1491c222013-03-05 14:53:28 +09001697 color = get_percent_color(percent);
1698 color_fprintf(stdout, color, " %7.2f", percent);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001699
Namhyung Kim1491c222013-03-05 14:53:28 +09001700 if (percent > percent_max)
1701 percent_max = percent;
1702 }
1703
1704 path = src_line->path;
1705 color = get_percent_color(percent_max);
Namhyung Kimf048d542013-09-11 14:09:28 +09001706 color_fprintf(stdout, color, " %s\n", path);
Namhyung Kim1491c222013-03-05 14:53:28 +09001707
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001708 node = rb_next(node);
1709 }
1710}
1711
Namhyung Kimdb8fd072013-03-05 14:53:21 +09001712static void symbol__annotate_hits(struct symbol *sym, struct perf_evsel *evsel)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001713{
1714 struct annotation *notes = symbol__annotation(sym);
Namhyung Kimdb8fd072013-03-05 14:53:21 +09001715 struct sym_hist *h = annotation__histogram(notes, evsel->idx);
Arnaldo Carvalho de Melo1b2e2df2012-04-19 10:57:06 -03001716 u64 len = symbol__size(sym), offset;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001717
1718 for (offset = 0; offset < len; ++offset)
1719 if (h->addr[offset] != 0)
1720 printf("%*" PRIx64 ": %" PRIu64 "\n", BITS_PER_LONG / 2,
1721 sym->start + offset, h->addr[offset]);
1722 printf("%*s: %" PRIu64 "\n", BITS_PER_LONG / 2, "h->sum", h->sum);
1723}
1724
Namhyung Kimdb8fd072013-03-05 14:53:21 +09001725int symbol__annotate_printf(struct symbol *sym, struct map *map,
1726 struct perf_evsel *evsel, bool full_paths,
1727 int min_pcnt, int max_lines, int context)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001728{
1729 struct dso *dso = map->dso;
David Ahernbfd14b92012-09-08 09:06:50 -06001730 char *filename;
1731 const char *d_filename;
Arnaldo Carvalho de Melo9cdbadce2014-03-18 11:50:21 -03001732 const char *evsel_name = perf_evsel__name(evsel);
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -02001733 struct annotation *notes = symbol__annotation(sym);
Peter Zijlstra (Intel)135cce12016-06-30 10:29:55 +02001734 struct sym_hist *h = annotation__histogram(notes, evsel->idx);
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -03001735 struct disasm_line *pos, *queue = NULL;
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -03001736 u64 start = map__rip_2objdump(map, sym->start);
Arnaldo Carvalho de Melod5e3d742011-02-08 15:29:25 -02001737 int printed = 2, queue_len = 0;
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -02001738 int more = 0;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001739 u64 len;
Namhyung Kimb1dd4432013-03-05 14:53:25 +09001740 int width = 8;
Peter Zijlstra (Intel)53dd9b52016-06-30 09:17:26 -03001741 int graph_dotted_len;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001742
David Ahernbfd14b92012-09-08 09:06:50 -06001743 filename = strdup(dso->long_name);
1744 if (!filename)
1745 return -ENOMEM;
1746
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001747 if (full_paths)
1748 d_filename = filename;
1749 else
1750 d_filename = basename(filename);
1751
Arnaldo Carvalho de Melo1b2e2df2012-04-19 10:57:06 -03001752 len = symbol__size(sym);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001753
Namhyung Kim759ff492013-03-05 14:53:26 +09001754 if (perf_evsel__is_group_event(evsel))
Namhyung Kimb1dd4432013-03-05 14:53:25 +09001755 width *= evsel->nr_members;
1756
Peter Zijlstra (Intel)135cce12016-06-30 10:29:55 +02001757 graph_dotted_len = printf(" %-*.*s| Source code & Disassembly of %s for %s (%" PRIu64 " samples)\n",
1758 width, width, "Percent", d_filename, evsel_name, h->sum);
Arnaldo Carvalho de Melo9cdbadce2014-03-18 11:50:21 -03001759
Peter Zijlstra (Intel)53dd9b52016-06-30 09:17:26 -03001760 printf("%-*.*s----\n",
Arnaldo Carvalho de Melo9cdbadce2014-03-18 11:50:21 -03001761 graph_dotted_len, graph_dotted_len, graph_dotted_line);
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -02001762
1763 if (verbose)
Namhyung Kimdb8fd072013-03-05 14:53:21 +09001764 symbol__annotate_hits(sym, evsel);
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -02001765
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -02001766 list_for_each_entry(pos, &notes->src->source, node) {
Arnaldo Carvalho de Melod5e3d742011-02-08 15:29:25 -02001767 if (context && queue == NULL) {
1768 queue = pos;
1769 queue_len = 0;
1770 }
1771
Namhyung Kimdb8fd072013-03-05 14:53:21 +09001772 switch (disasm_line__print(pos, sym, start, evsel, len,
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -03001773 min_pcnt, printed, max_lines,
1774 queue)) {
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -02001775 case 0:
1776 ++printed;
Arnaldo Carvalho de Melod5e3d742011-02-08 15:29:25 -02001777 if (context) {
1778 printed += queue_len;
1779 queue = NULL;
1780 queue_len = 0;
1781 }
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -02001782 break;
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -02001783 case 1:
1784 /* filtered by max_lines */
1785 ++more;
1786 break;
1787 case -1:
1788 default:
Arnaldo Carvalho de Melod5e3d742011-02-08 15:29:25 -02001789 /*
1790 * Filtered by min_pcnt or non IP lines when
1791 * context != 0
1792 */
1793 if (!context)
1794 break;
1795 if (queue_len == context)
1796 queue = list_entry(queue->node.next, typeof(*queue), node);
1797 else
1798 ++queue_len;
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -02001799 break;
1800 }
1801 }
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -02001802
David Ahernbfd14b92012-09-08 09:06:50 -06001803 free(filename);
1804
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -02001805 return more;
1806}
1807
1808void symbol__annotate_zero_histogram(struct symbol *sym, int evidx)
1809{
1810 struct annotation *notes = symbol__annotation(sym);
1811 struct sym_hist *h = annotation__histogram(notes, evidx);
1812
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -02001813 memset(h, 0, notes->src->sizeof_sym_hist);
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -02001814}
1815
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -02001816void symbol__annotate_decay_histogram(struct symbol *sym, int evidx)
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -02001817{
1818 struct annotation *notes = symbol__annotation(sym);
1819 struct sym_hist *h = annotation__histogram(notes, evidx);
Arnaldo Carvalho de Melo1b2e2df2012-04-19 10:57:06 -03001820 int len = symbol__size(sym), offset;
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -02001821
1822 h->sum = 0;
Arnaldo Carvalho de Melo8b84a562012-04-05 16:15:59 -03001823 for (offset = 0; offset < len; ++offset) {
1824 h->addr[offset] = h->addr[offset] * 7 / 8;
1825 h->sum += h->addr[offset];
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -02001826 }
1827}
1828
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -03001829void disasm__purge(struct list_head *head)
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -02001830{
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -03001831 struct disasm_line *pos, *n;
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -02001832
1833 list_for_each_entry_safe(pos, n, head, node) {
1834 list_del(&pos->node);
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -03001835 disasm_line__free(pos);
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -02001836 }
1837}
1838
Arnaldo Carvalho de Melo51454182012-04-15 15:52:18 -03001839static size_t disasm_line__fprintf(struct disasm_line *dl, FILE *fp)
1840{
1841 size_t printed;
1842
1843 if (dl->offset == -1)
1844 return fprintf(fp, "%s\n", dl->line);
1845
1846 printed = fprintf(fp, "%#" PRIx64 " %s", dl->offset, dl->name);
1847
Arnaldo Carvalho de Meloc7e6ead2012-04-20 14:38:46 -03001848 if (dl->ops.raw[0] != '\0') {
Arnaldo Carvalho de Melo51454182012-04-15 15:52:18 -03001849 printed += fprintf(fp, "%.*s %s\n", 6 - (int)printed, " ",
Arnaldo Carvalho de Meloc7e6ead2012-04-20 14:38:46 -03001850 dl->ops.raw);
Arnaldo Carvalho de Melo51454182012-04-15 15:52:18 -03001851 }
1852
1853 return printed + fprintf(fp, "\n");
1854}
1855
1856size_t disasm__fprintf(struct list_head *head, FILE *fp)
1857{
1858 struct disasm_line *pos;
1859 size_t printed = 0;
1860
1861 list_for_each_entry(pos, head, node)
1862 printed += disasm_line__fprintf(pos, fp);
1863
1864 return printed;
1865}
1866
Namhyung Kimdb8fd072013-03-05 14:53:21 +09001867int symbol__tty_annotate(struct symbol *sym, struct map *map,
1868 struct perf_evsel *evsel, bool print_lines,
1869 bool full_paths, int min_pcnt, int max_lines)
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -02001870{
1871 struct dso *dso = map->dso;
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -02001872 struct rb_root source_line = RB_ROOT;
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -02001873 u64 len;
1874
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -03001875 if (symbol__disassemble(sym, map, perf_evsel__env_arch(evsel), 0) < 0)
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -02001876 return -1;
1877
Arnaldo Carvalho de Melo1b2e2df2012-04-19 10:57:06 -03001878 len = symbol__size(sym);
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -02001879
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001880 if (print_lines) {
Michael Petlan4a4c03c2015-11-09 16:33:31 +01001881 srcline_full_filename = full_paths;
Namhyung Kim86c98ca2013-09-11 14:09:30 +09001882 symbol__get_source_line(sym, map, evsel, &source_line, len);
1883 print_summary(&source_line, dso->long_name);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001884 }
1885
Namhyung Kimdb8fd072013-03-05 14:53:21 +09001886 symbol__annotate_printf(sym, map, evsel, full_paths,
Arnaldo Carvalho de Melod5e3d742011-02-08 15:29:25 -02001887 min_pcnt, max_lines, 0);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001888 if (print_lines)
1889 symbol__free_source_line(sym, len);
1890
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -03001891 disasm__purge(&symbol__annotation(sym)->src->source);
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -02001892
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001893 return 0;
1894}
Arnaldo Carvalho de Melof626adf2013-12-18 17:10:15 -03001895
Namhyung Kim48c65bd2014-02-20 10:32:53 +09001896bool ui__has_annotation(void)
1897{
Jiri Olsa2e0453a2016-05-03 13:54:44 +02001898 return use_browser == 1 && perf_hpp_list.sym;
Namhyung Kim48c65bd2014-02-20 10:32:53 +09001899}