blob: f0a91037137731c3407d0165037c7a9acce45fc9 [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"
11#include "build-id.h"
12#include "color.h"
13#include "cache.h"
14#include "symbol.h"
15#include "debug.h"
16#include "annotate.h"
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -020017#include <pthread.h>
18
Andi Kleenf69b64f2011-09-15 14:31:41 -070019const char *disassembler_style;
Maciek Borzecki7a4ec932012-09-04 12:32:30 +020020const char *objdump_path;
Andi Kleenf69b64f2011-09-15 14:31:41 -070021
Arnaldo Carvalho de Melo7a997fe2012-05-12 13:15:34 -030022static struct ins *ins__find(const char *name);
23static int disasm_line__parse(char *line, char **namep, char **rawp);
24
Arnaldo Carvalho de Meloc46219ac2012-05-12 13:26:20 -030025static void ins__delete(struct ins_operands *ops)
26{
27 free(ops->source.raw);
28 free(ops->source.name);
29 free(ops->target.raw);
30 free(ops->target.name);
31}
32
Arnaldo Carvalho de Melo54170722012-05-07 18:54:16 -030033static int ins__raw_scnprintf(struct ins *ins, char *bf, size_t size,
34 struct ins_operands *ops)
35{
36 return scnprintf(bf, size, "%-6.6s %s", ins->name, ops->raw);
37}
38
39int ins__scnprintf(struct ins *ins, char *bf, size_t size,
40 struct ins_operands *ops)
41{
42 if (ins->ops->scnprintf)
43 return ins->ops->scnprintf(ins, bf, size, ops);
44
45 return ins__raw_scnprintf(ins, bf, size, ops);
46}
47
Arnaldo Carvalho de Meloc7e6ead2012-04-20 14:38:46 -030048static int call__parse(struct ins_operands *ops)
Arnaldo Carvalho de Melod86b0592012-04-18 16:07:38 -030049{
Arnaldo Carvalho de Melod2232882012-04-20 15:26:47 -030050 char *endptr, *tok, *name;
51
Arnaldo Carvalho de Melo44d1a3e2012-04-25 08:00:23 -030052 ops->target.addr = strtoull(ops->raw, &endptr, 16);
Arnaldo Carvalho de Melod2232882012-04-20 15:26:47 -030053
54 name = strchr(endptr, '<');
55 if (name == NULL)
56 goto indirect_call;
57
58 name++;
59
60 tok = strchr(name, '>');
61 if (tok == NULL)
62 return -1;
63
64 *tok = '\0';
Arnaldo Carvalho de Melo44d1a3e2012-04-25 08:00:23 -030065 ops->target.name = strdup(name);
Arnaldo Carvalho de Melod2232882012-04-20 15:26:47 -030066 *tok = '>';
67
Arnaldo Carvalho de Melo44d1a3e2012-04-25 08:00:23 -030068 return ops->target.name == NULL ? -1 : 0;
Arnaldo Carvalho de Melod2232882012-04-20 15:26:47 -030069
70indirect_call:
Arnaldo Carvalho de Meloe8ea1562012-05-11 12:28:55 -030071 tok = strchr(endptr, '(');
72 if (tok != NULL) {
73 ops->target.addr = 0;
74 return 0;
75 }
76
Arnaldo Carvalho de Melod2232882012-04-20 15:26:47 -030077 tok = strchr(endptr, '*');
78 if (tok == NULL)
79 return -1;
80
Arnaldo Carvalho de Melo44d1a3e2012-04-25 08:00:23 -030081 ops->target.addr = strtoull(tok + 1, NULL, 16);
Arnaldo Carvalho de Melod86b0592012-04-18 16:07:38 -030082 return 0;
83}
84
Arnaldo Carvalho de Melod2232882012-04-20 15:26:47 -030085static int call__scnprintf(struct ins *ins, char *bf, size_t size,
Arnaldo Carvalho de Melo54170722012-05-07 18:54:16 -030086 struct ins_operands *ops)
Arnaldo Carvalho de Melod2232882012-04-20 15:26:47 -030087{
Arnaldo Carvalho de Melo44d1a3e2012-04-25 08:00:23 -030088 if (ops->target.name)
89 return scnprintf(bf, size, "%-6.6s %s", ins->name, ops->target.name);
Arnaldo Carvalho de Melod2232882012-04-20 15:26:47 -030090
Arnaldo Carvalho de Meloe8ea1562012-05-11 12:28:55 -030091 if (ops->target.addr == 0)
92 return ins__raw_scnprintf(ins, bf, size, ops);
93
Arnaldo Carvalho de Melo44d1a3e2012-04-25 08:00:23 -030094 return scnprintf(bf, size, "%-6.6s *%" PRIx64, ins->name, ops->target.addr);
Arnaldo Carvalho de Melod2232882012-04-20 15:26:47 -030095}
96
Arnaldo Carvalho de Melod86b0592012-04-18 16:07:38 -030097static struct ins_ops call_ops = {
Arnaldo Carvalho de Melod2232882012-04-20 15:26:47 -030098 .parse = call__parse,
99 .scnprintf = call__scnprintf,
Arnaldo Carvalho de Melod86b0592012-04-18 16:07:38 -0300100};
101
102bool ins__is_call(const struct ins *ins)
103{
104 return ins->ops == &call_ops;
105}
106
Arnaldo Carvalho de Meloc7e6ead2012-04-20 14:38:46 -0300107static int jump__parse(struct ins_operands *ops)
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -0300108{
Arnaldo Carvalho de Meloc7e6ead2012-04-20 14:38:46 -0300109 const char *s = strchr(ops->raw, '+');
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -0300110
Arnaldo Carvalho de Melofb29fa52012-04-25 14:16:03 -0300111 ops->target.addr = strtoll(ops->raw, NULL, 16);
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -0300112
Arnaldo Carvalho de Melofb29fa52012-04-25 14:16:03 -0300113 if (s++ != NULL)
114 ops->target.offset = strtoll(s, NULL, 16);
115 else
116 ops->target.offset = UINT64_MAX;
117
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -0300118 return 0;
119}
120
Arnaldo Carvalho de Meloc7e6ead2012-04-20 14:38:46 -0300121static int jump__scnprintf(struct ins *ins, char *bf, size_t size,
Arnaldo Carvalho de Melo54170722012-05-07 18:54:16 -0300122 struct ins_operands *ops)
Arnaldo Carvalho de Melo28548d72012-04-19 10:16:27 -0300123{
Arnaldo Carvalho de Melo44d1a3e2012-04-25 08:00:23 -0300124 return scnprintf(bf, size, "%-6.6s %" PRIx64, ins->name, ops->target.offset);
Arnaldo Carvalho de Melo28548d72012-04-19 10:16:27 -0300125}
126
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -0300127static struct ins_ops jump_ops = {
Arnaldo Carvalho de Meloc7e6ead2012-04-20 14:38:46 -0300128 .parse = jump__parse,
129 .scnprintf = jump__scnprintf,
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -0300130};
131
132bool ins__is_jump(const struct ins *ins)
133{
134 return ins->ops == &jump_ops;
135}
136
Arnaldo Carvalho de Melo6de783b2012-05-11 16:48:49 -0300137static int comment__symbol(char *raw, char *comment, u64 *addrp, char **namep)
138{
139 char *endptr, *name, *t;
140
141 if (strstr(raw, "(%rip)") == NULL)
142 return 0;
143
144 *addrp = strtoull(comment, &endptr, 16);
145 name = strchr(endptr, '<');
146 if (name == NULL)
147 return -1;
148
149 name++;
150
151 t = strchr(name, '>');
152 if (t == NULL)
153 return 0;
154
155 *t = '\0';
156 *namep = strdup(name);
157 *t = '>';
158
159 return 0;
160}
161
Arnaldo Carvalho de Melo7a997fe2012-05-12 13:15:34 -0300162static int lock__parse(struct ins_operands *ops)
163{
164 char *name;
165
166 ops->locked.ops = zalloc(sizeof(*ops->locked.ops));
167 if (ops->locked.ops == NULL)
168 return 0;
169
170 if (disasm_line__parse(ops->raw, &name, &ops->locked.ops->raw) < 0)
171 goto out_free_ops;
172
173 ops->locked.ins = ins__find(name);
174 if (ops->locked.ins == NULL)
175 goto out_free_ops;
176
177 if (!ops->locked.ins->ops)
178 return 0;
179
180 if (ops->locked.ins->ops->parse)
181 ops->locked.ins->ops->parse(ops->locked.ops);
182
183 return 0;
184
185out_free_ops:
186 free(ops->locked.ops);
187 ops->locked.ops = NULL;
188 return 0;
189}
190
191static int lock__scnprintf(struct ins *ins, char *bf, size_t size,
192 struct ins_operands *ops)
193{
194 int printed;
195
196 if (ops->locked.ins == NULL)
197 return ins__raw_scnprintf(ins, bf, size, ops);
198
199 printed = scnprintf(bf, size, "%-6.6s ", ins->name);
200 return printed + ins__scnprintf(ops->locked.ins, bf + printed,
201 size - printed, ops->locked.ops);
202}
203
Arnaldo Carvalho de Meloc46219ac2012-05-12 13:26:20 -0300204static void lock__delete(struct ins_operands *ops)
205{
206 free(ops->locked.ops);
207 free(ops->target.raw);
208 free(ops->target.name);
209}
210
Arnaldo Carvalho de Melo7a997fe2012-05-12 13:15:34 -0300211static struct ins_ops lock_ops = {
Arnaldo Carvalho de Meloc46219ac2012-05-12 13:26:20 -0300212 .free = lock__delete,
Arnaldo Carvalho de Melo7a997fe2012-05-12 13:15:34 -0300213 .parse = lock__parse,
214 .scnprintf = lock__scnprintf,
215};
216
Arnaldo Carvalho de Melo6de783b2012-05-11 16:48:49 -0300217static int mov__parse(struct ins_operands *ops)
218{
219 char *s = strchr(ops->raw, ','), *target, *comment, prev;
220
221 if (s == NULL)
222 return -1;
223
224 *s = '\0';
225 ops->source.raw = strdup(ops->raw);
226 *s = ',';
227
228 if (ops->source.raw == NULL)
229 return -1;
230
231 target = ++s;
232
233 while (s[0] != '\0' && !isspace(s[0]))
234 ++s;
235 prev = *s;
236 *s = '\0';
237
238 ops->target.raw = strdup(target);
239 *s = prev;
240
241 if (ops->target.raw == NULL)
242 goto out_free_source;
243
244 comment = strchr(s, '#');
245 if (comment == NULL)
246 return 0;
247
248 while (comment[0] != '\0' && isspace(comment[0]))
249 ++comment;
250
251 comment__symbol(ops->source.raw, comment, &ops->source.addr, &ops->source.name);
252 comment__symbol(ops->target.raw, comment, &ops->target.addr, &ops->target.name);
253
254 return 0;
255
256out_free_source:
257 free(ops->source.raw);
258 ops->source.raw = NULL;
259 return -1;
260}
261
262static int mov__scnprintf(struct ins *ins, char *bf, size_t size,
263 struct ins_operands *ops)
264{
265 return scnprintf(bf, size, "%-6.6s %s,%s", ins->name,
266 ops->source.name ?: ops->source.raw,
267 ops->target.name ?: ops->target.raw);
268}
269
270static struct ins_ops mov_ops = {
271 .parse = mov__parse,
272 .scnprintf = mov__scnprintf,
273};
274
Arnaldo Carvalho de Meloa43712c2012-05-11 17:21:09 -0300275static int dec__parse(struct ins_operands *ops)
276{
277 char *target, *comment, *s, prev;
278
279 target = s = ops->raw;
280
281 while (s[0] != '\0' && !isspace(s[0]))
282 ++s;
283 prev = *s;
284 *s = '\0';
285
286 ops->target.raw = strdup(target);
287 *s = prev;
288
289 if (ops->target.raw == NULL)
290 return -1;
291
292 comment = strchr(s, '#');
293 if (comment == NULL)
294 return 0;
295
296 while (comment[0] != '\0' && isspace(comment[0]))
297 ++comment;
298
299 comment__symbol(ops->target.raw, comment, &ops->target.addr, &ops->target.name);
300
301 return 0;
302}
303
304static int dec__scnprintf(struct ins *ins, char *bf, size_t size,
305 struct ins_operands *ops)
306{
307 return scnprintf(bf, size, "%-6.6s %s", ins->name,
308 ops->target.name ?: ops->target.raw);
309}
310
311static struct ins_ops dec_ops = {
312 .parse = dec__parse,
313 .scnprintf = dec__scnprintf,
314};
315
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300316static int nop__scnprintf(struct ins *ins __maybe_unused, char *bf, size_t size,
317 struct ins_operands *ops __maybe_unused)
Arnaldo Carvalho de Melob9818e92012-05-07 18:57:02 -0300318{
319 return scnprintf(bf, size, "%-6.6s", "nop");
320}
321
322static struct ins_ops nop_ops = {
323 .scnprintf = nop__scnprintf,
324};
325
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -0300326/*
327 * Must be sorted by name!
328 */
329static struct ins instructions[] = {
Arnaldo Carvalho de Melo6de783b2012-05-11 16:48:49 -0300330 { .name = "add", .ops = &mov_ops, },
331 { .name = "addl", .ops = &mov_ops, },
332 { .name = "addq", .ops = &mov_ops, },
333 { .name = "addw", .ops = &mov_ops, },
334 { .name = "and", .ops = &mov_ops, },
Arnaldo Carvalho de Melo7a997fe2012-05-12 13:15:34 -0300335 { .name = "bts", .ops = &mov_ops, },
Arnaldo Carvalho de Melod86b0592012-04-18 16:07:38 -0300336 { .name = "call", .ops = &call_ops, },
337 { .name = "callq", .ops = &call_ops, },
Arnaldo Carvalho de Melo6de783b2012-05-11 16:48:49 -0300338 { .name = "cmp", .ops = &mov_ops, },
339 { .name = "cmpb", .ops = &mov_ops, },
340 { .name = "cmpl", .ops = &mov_ops, },
341 { .name = "cmpq", .ops = &mov_ops, },
342 { .name = "cmpw", .ops = &mov_ops, },
343 { .name = "cmpxch", .ops = &mov_ops, },
Arnaldo Carvalho de Meloa43712c2012-05-11 17:21:09 -0300344 { .name = "dec", .ops = &dec_ops, },
345 { .name = "decl", .ops = &dec_ops, },
Arnaldo Carvalho de Melo6de783b2012-05-11 16:48:49 -0300346 { .name = "imul", .ops = &mov_ops, },
Arnaldo Carvalho de Meloa43712c2012-05-11 17:21:09 -0300347 { .name = "inc", .ops = &dec_ops, },
348 { .name = "incl", .ops = &dec_ops, },
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -0300349 { .name = "ja", .ops = &jump_ops, },
Arnaldo Carvalho de Melo3f862fd2012-04-19 17:10:12 -0300350 { .name = "jae", .ops = &jump_ops, },
351 { .name = "jb", .ops = &jump_ops, },
352 { .name = "jbe", .ops = &jump_ops, },
353 { .name = "jc", .ops = &jump_ops, },
354 { .name = "jcxz", .ops = &jump_ops, },
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -0300355 { .name = "je", .ops = &jump_ops, },
Arnaldo Carvalho de Melo3f862fd2012-04-19 17:10:12 -0300356 { .name = "jecxz", .ops = &jump_ops, },
357 { .name = "jg", .ops = &jump_ops, },
358 { .name = "jge", .ops = &jump_ops, },
359 { .name = "jl", .ops = &jump_ops, },
360 { .name = "jle", .ops = &jump_ops, },
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -0300361 { .name = "jmp", .ops = &jump_ops, },
362 { .name = "jmpq", .ops = &jump_ops, },
Arnaldo Carvalho de Melo3f862fd2012-04-19 17:10:12 -0300363 { .name = "jna", .ops = &jump_ops, },
364 { .name = "jnae", .ops = &jump_ops, },
365 { .name = "jnb", .ops = &jump_ops, },
366 { .name = "jnbe", .ops = &jump_ops, },
367 { .name = "jnc", .ops = &jump_ops, },
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -0300368 { .name = "jne", .ops = &jump_ops, },
Arnaldo Carvalho de Melo3f862fd2012-04-19 17:10:12 -0300369 { .name = "jng", .ops = &jump_ops, },
370 { .name = "jnge", .ops = &jump_ops, },
371 { .name = "jnl", .ops = &jump_ops, },
372 { .name = "jnle", .ops = &jump_ops, },
373 { .name = "jno", .ops = &jump_ops, },
374 { .name = "jnp", .ops = &jump_ops, },
375 { .name = "jns", .ops = &jump_ops, },
376 { .name = "jnz", .ops = &jump_ops, },
377 { .name = "jo", .ops = &jump_ops, },
378 { .name = "jp", .ops = &jump_ops, },
379 { .name = "jpe", .ops = &jump_ops, },
380 { .name = "jpo", .ops = &jump_ops, },
381 { .name = "jrcxz", .ops = &jump_ops, },
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -0300382 { .name = "js", .ops = &jump_ops, },
Arnaldo Carvalho de Melo3f862fd2012-04-19 17:10:12 -0300383 { .name = "jz", .ops = &jump_ops, },
Arnaldo Carvalho de Melo6de783b2012-05-11 16:48:49 -0300384 { .name = "lea", .ops = &mov_ops, },
Arnaldo Carvalho de Melo7a997fe2012-05-12 13:15:34 -0300385 { .name = "lock", .ops = &lock_ops, },
Arnaldo Carvalho de Melo6de783b2012-05-11 16:48:49 -0300386 { .name = "mov", .ops = &mov_ops, },
387 { .name = "movb", .ops = &mov_ops, },
388 { .name = "movdqa",.ops = &mov_ops, },
389 { .name = "movl", .ops = &mov_ops, },
390 { .name = "movq", .ops = &mov_ops, },
391 { .name = "movslq", .ops = &mov_ops, },
392 { .name = "movzbl", .ops = &mov_ops, },
393 { .name = "movzwl", .ops = &mov_ops, },
Arnaldo Carvalho de Melob9818e92012-05-07 18:57:02 -0300394 { .name = "nop", .ops = &nop_ops, },
395 { .name = "nopl", .ops = &nop_ops, },
396 { .name = "nopw", .ops = &nop_ops, },
Arnaldo Carvalho de Melo6de783b2012-05-11 16:48:49 -0300397 { .name = "or", .ops = &mov_ops, },
398 { .name = "orl", .ops = &mov_ops, },
399 { .name = "test", .ops = &mov_ops, },
400 { .name = "testb", .ops = &mov_ops, },
401 { .name = "testl", .ops = &mov_ops, },
Arnaldo Carvalho de Melo7a997fe2012-05-12 13:15:34 -0300402 { .name = "xadd", .ops = &mov_ops, },
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -0300403};
404
405static int ins__cmp(const void *name, const void *insp)
406{
407 const struct ins *ins = insp;
408
409 return strcmp(name, ins->name);
410}
411
412static struct ins *ins__find(const char *name)
413{
414 const int nmemb = ARRAY_SIZE(instructions);
415
416 return bsearch(name, instructions, nmemb, sizeof(struct ins), ins__cmp);
417}
418
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300419int symbol__annotate_init(struct map *map __maybe_unused, struct symbol *sym)
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200420{
421 struct annotation *notes = symbol__annotation(sym);
422 pthread_mutex_init(&notes->lock, NULL);
423 return 0;
424}
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200425
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -0200426int symbol__alloc_hist(struct symbol *sym)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200427{
428 struct annotation *notes = symbol__annotation(sym);
Arnaldo Carvalho de Melo1b2e2df2012-04-19 10:57:06 -0300429 const size_t size = symbol__size(sym);
Cody Schafer86963292012-07-19 20:05:25 -0700430 size_t sizeof_sym_hist;
431
432 /* Check for overflow when calculating sizeof_sym_hist */
433 if (size > (SIZE_MAX - sizeof(struct sym_hist)) / sizeof(u64))
434 return -1;
435
436 sizeof_sym_hist = (sizeof(struct sym_hist) + size * sizeof(u64));
437
438 /* Check for overflow in zalloc argument */
439 if (sizeof_sym_hist > (SIZE_MAX - sizeof(*notes->src))
440 / symbol_conf.nr_events)
441 return -1;
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200442
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -0200443 notes->src = zalloc(sizeof(*notes->src) + symbol_conf.nr_events * sizeof_sym_hist);
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200444 if (notes->src == NULL)
445 return -1;
446 notes->src->sizeof_sym_hist = sizeof_sym_hist;
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -0200447 notes->src->nr_histograms = symbol_conf.nr_events;
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200448 INIT_LIST_HEAD(&notes->src->source);
449 return 0;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200450}
451
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200452void symbol__annotate_zero_histograms(struct symbol *sym)
453{
454 struct annotation *notes = symbol__annotation(sym);
455
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200456 pthread_mutex_lock(&notes->lock);
457 if (notes->src != NULL)
458 memset(notes->src->histograms, 0,
459 notes->src->nr_histograms * notes->src->sizeof_sym_hist);
460 pthread_mutex_unlock(&notes->lock);
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200461}
462
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200463int symbol__inc_addr_samples(struct symbol *sym, struct map *map,
464 int evidx, u64 addr)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200465{
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200466 unsigned offset;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200467 struct annotation *notes;
468 struct sym_hist *h;
469
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200470 notes = symbol__annotation(sym);
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200471 if (notes->src == NULL)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200472 return -ENOMEM;
473
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200474 pr_debug3("%s: addr=%#" PRIx64 "\n", __func__, map->unmap_ip(map, addr));
475
Arnaldo Carvalho de Melo31d68e72012-03-27 12:55:57 -0300476 if (addr < sym->start || addr > sym->end)
477 return -ERANGE;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200478
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200479 offset = addr - sym->start;
480 h = annotation__histogram(notes, evidx);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200481 h->sum++;
482 h->addr[offset]++;
483
484 pr_debug3("%#" PRIx64 " %s: period++ [addr: %#" PRIx64 ", %#" PRIx64
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200485 ", evidx=%d] => %" PRIu64 "\n", sym->start, sym->name,
486 addr, addr - sym->start, evidx, h->addr[offset]);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200487 return 0;
488}
489
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -0300490static void disasm_line__init_ins(struct disasm_line *dl)
491{
492 dl->ins = ins__find(dl->name);
493
494 if (dl->ins == NULL)
495 return;
496
497 if (!dl->ins->ops)
498 return;
499
Arnaldo Carvalho de Meloc7e6ead2012-04-20 14:38:46 -0300500 if (dl->ins->ops->parse)
501 dl->ins->ops->parse(&dl->ops);
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -0300502}
503
Arnaldo Carvalho de Melo7a997fe2012-05-12 13:15:34 -0300504static int disasm_line__parse(char *line, char **namep, char **rawp)
505{
506 char *name = line, tmp;
507
508 while (isspace(name[0]))
509 ++name;
510
511 if (name[0] == '\0')
512 return -1;
513
514 *rawp = name + 1;
515
516 while ((*rawp)[0] != '\0' && !isspace((*rawp)[0]))
517 ++*rawp;
518
519 tmp = (*rawp)[0];
520 (*rawp)[0] = '\0';
521 *namep = strdup(name);
522
523 if (*namep == NULL)
524 goto out_free_name;
525
526 (*rawp)[0] = tmp;
527
528 if ((*rawp)[0] != '\0') {
529 (*rawp)++;
530 while (isspace((*rawp)[0]))
531 ++(*rawp);
532 }
533
534 return 0;
535
536out_free_name:
537 free(*namep);
538 *namep = NULL;
539 return -1;
540}
541
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300542static struct disasm_line *disasm_line__new(s64 offset, char *line, size_t privsize)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200543{
Arnaldo Carvalho de Melo51454182012-04-15 15:52:18 -0300544 struct disasm_line *dl = zalloc(sizeof(*dl) + privsize);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200545
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300546 if (dl != NULL) {
547 dl->offset = offset;
548 dl->line = strdup(line);
549 if (dl->line == NULL)
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -0300550 goto out_delete;
Arnaldo Carvalho de Melo51454182012-04-15 15:52:18 -0300551
552 if (offset != -1) {
Arnaldo Carvalho de Melo7a997fe2012-05-12 13:15:34 -0300553 if (disasm_line__parse(dl->line, &dl->name, &dl->ops.raw) < 0)
Arnaldo Carvalho de Melo51454182012-04-15 15:52:18 -0300554 goto out_free_line;
555
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -0300556 disasm_line__init_ins(dl);
Arnaldo Carvalho de Melo51454182012-04-15 15:52:18 -0300557 }
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200558 }
559
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300560 return dl;
Arnaldo Carvalho de Melo51454182012-04-15 15:52:18 -0300561
562out_free_line:
563 free(dl->line);
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -0300564out_delete:
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300565 free(dl);
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -0300566 return NULL;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200567}
568
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300569void disasm_line__free(struct disasm_line *dl)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200570{
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300571 free(dl->line);
Arnaldo Carvalho de Melo51454182012-04-15 15:52:18 -0300572 free(dl->name);
Arnaldo Carvalho de Meloc46219ac2012-05-12 13:26:20 -0300573 if (dl->ins && dl->ins->ops->free)
574 dl->ins->ops->free(&dl->ops);
575 else
576 ins__delete(&dl->ops);
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300577 free(dl);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200578}
579
Arnaldo Carvalho de Melo54170722012-05-07 18:54:16 -0300580int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size, bool raw)
581{
582 if (raw || !dl->ins)
583 return scnprintf(bf, size, "%-6.6s %s", dl->name, dl->ops.raw);
584
585 return ins__scnprintf(dl->ins, bf, size, &dl->ops);
586}
587
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300588static void disasm__add(struct list_head *head, struct disasm_line *line)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200589{
590 list_add_tail(&line->node, head);
591}
592
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300593struct disasm_line *disasm__get_next_ip_line(struct list_head *head, struct disasm_line *pos)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200594{
595 list_for_each_entry_continue(pos, head, node)
596 if (pos->offset >= 0)
597 return pos;
598
599 return NULL;
600}
601
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300602static int disasm_line__print(struct disasm_line *dl, struct symbol *sym, u64 start,
603 int evidx, u64 len, int min_pcnt, int printed,
604 int max_lines, struct disasm_line *queue)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200605{
606 static const char *prev_line;
607 static const char *prev_color;
608
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300609 if (dl->offset != -1) {
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200610 const char *path = NULL;
611 unsigned int hits = 0;
612 double percent = 0.0;
613 const char *color;
614 struct annotation *notes = symbol__annotation(sym);
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200615 struct source_line *src_line = notes->src->lines;
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200616 struct sym_hist *h = annotation__histogram(notes, evidx);
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300617 s64 offset = dl->offset;
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -0300618 const u64 addr = start + offset;
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300619 struct disasm_line *next;
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200620
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300621 next = disasm__get_next_ip_line(&notes->src->source, dl);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200622
623 while (offset < (s64)len &&
624 (next == NULL || offset < next->offset)) {
625 if (src_line) {
626 if (path == NULL)
627 path = src_line[offset].path;
628 percent += src_line[offset].percent;
629 } else
630 hits += h->addr[offset];
631
632 ++offset;
633 }
634
635 if (src_line == NULL && h->sum)
636 percent = 100.0 * hits / h->sum;
637
Arnaldo Carvalho de Melod040bd32011-02-05 15:37:31 -0200638 if (percent < min_pcnt)
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200639 return -1;
640
Arnaldo Carvalho de Meloe3087b82011-02-08 15:01:39 -0200641 if (max_lines && printed >= max_lines)
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200642 return 1;
Arnaldo Carvalho de Melod040bd32011-02-05 15:37:31 -0200643
Arnaldo Carvalho de Melod5e3d742011-02-08 15:29:25 -0200644 if (queue != NULL) {
645 list_for_each_entry_from(queue, &notes->src->source, node) {
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300646 if (queue == dl)
Arnaldo Carvalho de Melod5e3d742011-02-08 15:29:25 -0200647 break;
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300648 disasm_line__print(queue, sym, start, evidx, len,
Arnaldo Carvalho de Melod5e3d742011-02-08 15:29:25 -0200649 0, 0, 1, NULL);
650 }
651 }
652
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200653 color = get_percent_color(percent);
654
655 /*
656 * Also color the filename and line if needed, with
657 * the same color than the percentage. Don't print it
658 * twice for close colored addr with the same filename:line
659 */
660 if (path) {
661 if (!prev_line || strcmp(prev_line, path)
662 || color != prev_color) {
663 color_fprintf(stdout, color, " %s", path);
664 prev_line = path;
665 prev_color = color;
666 }
667 }
668
669 color_fprintf(stdout, color, " %7.2f", percent);
670 printf(" : ");
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -0300671 color_fprintf(stdout, PERF_COLOR_MAGENTA, " %" PRIx64 ":", addr);
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300672 color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", dl->line);
Arnaldo Carvalho de Meloe3087b82011-02-08 15:01:39 -0200673 } else if (max_lines && printed >= max_lines)
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200674 return 1;
675 else {
Arnaldo Carvalho de Melod5e3d742011-02-08 15:29:25 -0200676 if (queue)
677 return -1;
678
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300679 if (!*dl->line)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200680 printf(" :\n");
681 else
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300682 printf(" : %s\n", dl->line);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200683 }
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200684
685 return 0;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200686}
687
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200688static int symbol__parse_objdump_line(struct symbol *sym, struct map *map,
689 FILE *file, size_t privsize)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200690{
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200691 struct annotation *notes = symbol__annotation(sym);
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300692 struct disasm_line *dl;
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -0300693 char *line = NULL, *parsed_line, *tmp, *tmp2, *c;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200694 size_t line_len;
695 s64 line_ip, offset = -1;
696
697 if (getline(&line, &line_len, file) < 0)
698 return -1;
699
700 if (!line)
701 return -1;
702
703 while (line_len != 0 && isspace(line[line_len - 1]))
704 line[--line_len] = '\0';
705
706 c = strchr(line, '\n');
707 if (c)
708 *c = 0;
709
710 line_ip = -1;
Namhyung Kima31b7cc2012-04-11 17:04:59 -0300711 parsed_line = line;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200712
713 /*
714 * Strip leading spaces:
715 */
716 tmp = line;
717 while (*tmp) {
718 if (*tmp != ' ')
719 break;
720 tmp++;
721 }
722
723 if (*tmp) {
724 /*
725 * Parse hexa addresses followed by ':'
726 */
727 line_ip = strtoull(tmp, &tmp2, 16);
728 if (*tmp2 != ':' || tmp == tmp2 || tmp2[1] == '\0')
729 line_ip = -1;
730 }
731
732 if (line_ip != -1) {
733 u64 start = map__rip_2objdump(map, sym->start),
734 end = map__rip_2objdump(map, sym->end);
735
736 offset = line_ip - start;
737 if (offset < 0 || (u64)line_ip > end)
738 offset = -1;
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -0300739 else
740 parsed_line = tmp2 + 1;
Namhyung Kima31b7cc2012-04-11 17:04:59 -0300741 }
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200742
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300743 dl = disasm_line__new(offset, parsed_line, privsize);
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -0300744 free(line);
745
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300746 if (dl == NULL)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200747 return -1;
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -0300748
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300749 disasm__add(&notes->src->source, dl);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200750
751 return 0;
752}
753
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200754int symbol__annotate(struct symbol *sym, struct map *map, size_t privsize)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200755{
756 struct dso *dso = map->dso;
757 char *filename = dso__build_id_filename(dso, NULL, 0);
758 bool free_filename = true;
759 char command[PATH_MAX * 2];
760 FILE *file;
761 int err = 0;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200762 char symfs_filename[PATH_MAX];
763
764 if (filename) {
765 snprintf(symfs_filename, sizeof(symfs_filename), "%s%s",
766 symbol_conf.symfs, filename);
767 }
768
769 if (filename == NULL) {
770 if (dso->has_build_id) {
771 pr_err("Can't annotate %s: not enough memory\n",
772 sym->name);
773 return -ENOMEM;
774 }
775 goto fallback;
776 } else if (readlink(symfs_filename, command, sizeof(command)) < 0 ||
777 strstr(command, "[kernel.kallsyms]") ||
778 access(symfs_filename, R_OK)) {
779 free(filename);
780fallback:
781 /*
782 * If we don't have build-ids or the build-id file isn't in the
783 * cache, or is just a kallsyms file, well, lets hope that this
784 * DSO is the same as when 'perf record' ran.
785 */
786 filename = dso->long_name;
787 snprintf(symfs_filename, sizeof(symfs_filename), "%s%s",
788 symbol_conf.symfs, filename);
789 free_filename = false;
790 }
791
Jiri Olsa44f24cb2012-07-22 14:14:32 +0200792 if (dso->symtab_type == DSO_BINARY_TYPE__KALLSYMS) {
Arnaldo Carvalho de Melo170ae6b2011-02-23 11:08:59 -0300793 char bf[BUILD_ID_SIZE * 2 + 16] = " with build id ";
794 char *build_id_msg = NULL;
795
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200796 if (dso->annotate_warned)
797 goto out_free_filename;
Arnaldo Carvalho de Melo170ae6b2011-02-23 11:08:59 -0300798
799 if (dso->has_build_id) {
800 build_id__sprintf(dso->build_id,
801 sizeof(dso->build_id), bf + 15);
802 build_id_msg = bf;
803 }
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200804 err = -ENOENT;
805 dso->annotate_warned = 1;
Arnaldo Carvalho de Meloae557952011-10-26 08:00:55 -0200806 pr_err("Can't annotate %s:\n\n"
807 "No vmlinux file%s\nwas found in the path.\n\n"
808 "Please use:\n\n"
809 " perf buildid-cache -av vmlinux\n\n"
810 "or:\n\n"
Namhyung Kimff2a6612012-02-23 17:46:24 +0900811 " --vmlinux vmlinux\n",
Arnaldo Carvalho de Melo170ae6b2011-02-23 11:08:59 -0300812 sym->name, build_id_msg ?: "");
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200813 goto out_free_filename;
814 }
815
816 pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__,
817 filename, sym->name, map->unmap_ip(map, sym->start),
818 map->unmap_ip(map, sym->end));
819
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200820 pr_debug("annotating [%p] %30s : [%p] %30s\n",
821 dso, dso->long_name, sym, sym->name);
822
823 snprintf(command, sizeof(command),
Maciek Borzecki7a4ec932012-09-04 12:32:30 +0200824 "%s %s%s --start-address=0x%016" PRIx64
Stephane Eranian3e6a2a72011-05-17 17:32:07 +0200825 " --stop-address=0x%016" PRIx64
826 " -d %s %s -C %s|grep -v %s|expand",
Maciek Borzecki7a4ec932012-09-04 12:32:30 +0200827 objdump_path ? objdump_path : "objdump",
Andi Kleenf69b64f2011-09-15 14:31:41 -0700828 disassembler_style ? "-M " : "",
829 disassembler_style ? disassembler_style : "",
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200830 map__rip_2objdump(map, sym->start),
Ingo Molnarf41612f2011-12-23 14:08:04 +0100831 map__rip_2objdump(map, sym->end+1),
Stephane Eranian3e6a2a72011-05-17 17:32:07 +0200832 symbol_conf.annotate_asm_raw ? "" : "--no-show-raw",
833 symbol_conf.annotate_src ? "-S" : "",
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200834 symfs_filename, filename);
835
836 pr_debug("Executing: %s\n", command);
837
838 file = popen(command, "r");
839 if (!file)
840 goto out_free_filename;
841
842 while (!feof(file))
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200843 if (symbol__parse_objdump_line(sym, map, file, privsize) < 0)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200844 break;
845
846 pclose(file);
847out_free_filename:
848 if (free_filename)
849 free(filename);
850 return err;
851}
852
853static void insert_source_line(struct rb_root *root, struct source_line *src_line)
854{
855 struct source_line *iter;
856 struct rb_node **p = &root->rb_node;
857 struct rb_node *parent = NULL;
858
859 while (*p != NULL) {
860 parent = *p;
861 iter = rb_entry(parent, struct source_line, node);
862
863 if (src_line->percent > iter->percent)
864 p = &(*p)->rb_left;
865 else
866 p = &(*p)->rb_right;
867 }
868
869 rb_link_node(&src_line->node, parent, p);
870 rb_insert_color(&src_line->node, root);
871}
872
873static void symbol__free_source_line(struct symbol *sym, int len)
874{
875 struct annotation *notes = symbol__annotation(sym);
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200876 struct source_line *src_line = notes->src->lines;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200877 int i;
878
879 for (i = 0; i < len; i++)
880 free(src_line[i].path);
881
882 free(src_line);
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200883 notes->src->lines = NULL;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200884}
885
886/* Get the filename:line for the colored entries */
887static int symbol__get_source_line(struct symbol *sym, struct map *map,
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200888 int evidx, struct rb_root *root, int len,
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200889 const char *filename)
890{
891 u64 start;
892 int i;
893 char cmd[PATH_MAX * 2];
894 struct source_line *src_line;
895 struct annotation *notes = symbol__annotation(sym);
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200896 struct sym_hist *h = annotation__histogram(notes, evidx);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200897
898 if (!h->sum)
899 return 0;
900
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200901 src_line = notes->src->lines = calloc(len, sizeof(struct source_line));
902 if (!notes->src->lines)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200903 return -1;
904
David Millerf40a0632012-03-25 16:28:12 -0400905 start = map__rip_2objdump(map, sym->start);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200906
907 for (i = 0; i < len; i++) {
908 char *path = NULL;
909 size_t line_len;
910 u64 offset;
911 FILE *fp;
912
913 src_line[i].percent = 100.0 * h->addr[i] / h->sum;
914 if (src_line[i].percent <= 0.5)
915 continue;
916
917 offset = start + i;
918 sprintf(cmd, "addr2line -e %s %016" PRIx64, filename, offset);
919 fp = popen(cmd, "r");
920 if (!fp)
921 continue;
922
923 if (getline(&path, &line_len, fp) < 0 || !line_len)
924 goto next;
925
926 src_line[i].path = malloc(sizeof(char) * line_len + 1);
927 if (!src_line[i].path)
928 goto next;
929
930 strcpy(src_line[i].path, path);
931 insert_source_line(root, &src_line[i]);
932
933 next:
934 pclose(fp);
935 }
936
937 return 0;
938}
939
940static void print_summary(struct rb_root *root, const char *filename)
941{
942 struct source_line *src_line;
943 struct rb_node *node;
944
945 printf("\nSorted summary for file %s\n", filename);
946 printf("----------------------------------------------\n\n");
947
948 if (RB_EMPTY_ROOT(root)) {
949 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
950 return;
951 }
952
953 node = rb_first(root);
954 while (node) {
955 double percent;
956 const char *color;
957 char *path;
958
959 src_line = rb_entry(node, struct source_line, node);
960 percent = src_line->percent;
961 color = get_percent_color(percent);
962 path = src_line->path;
963
964 color_fprintf(stdout, color, " %7.2f %s", percent, path);
965 node = rb_next(node);
966 }
967}
968
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200969static void symbol__annotate_hits(struct symbol *sym, int evidx)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200970{
971 struct annotation *notes = symbol__annotation(sym);
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200972 struct sym_hist *h = annotation__histogram(notes, evidx);
Arnaldo Carvalho de Melo1b2e2df2012-04-19 10:57:06 -0300973 u64 len = symbol__size(sym), offset;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200974
975 for (offset = 0; offset < len; ++offset)
976 if (h->addr[offset] != 0)
977 printf("%*" PRIx64 ": %" PRIu64 "\n", BITS_PER_LONG / 2,
978 sym->start + offset, h->addr[offset]);
979 printf("%*s: %" PRIu64 "\n", BITS_PER_LONG / 2, "h->sum", h->sum);
980}
981
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200982int symbol__annotate_printf(struct symbol *sym, struct map *map, int evidx,
Arnaldo Carvalho de Melod5e3d742011-02-08 15:29:25 -0200983 bool full_paths, int min_pcnt, int max_lines,
984 int context)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200985{
986 struct dso *dso = map->dso;
David Ahernbfd14b92012-09-08 09:06:50 -0600987 char *filename;
988 const char *d_filename;
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200989 struct annotation *notes = symbol__annotation(sym);
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300990 struct disasm_line *pos, *queue = NULL;
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -0300991 u64 start = map__rip_2objdump(map, sym->start);
Arnaldo Carvalho de Melod5e3d742011-02-08 15:29:25 -0200992 int printed = 2, queue_len = 0;
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200993 int more = 0;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200994 u64 len;
995
David Ahernbfd14b92012-09-08 09:06:50 -0600996 filename = strdup(dso->long_name);
997 if (!filename)
998 return -ENOMEM;
999
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001000 if (full_paths)
1001 d_filename = filename;
1002 else
1003 d_filename = basename(filename);
1004
Arnaldo Carvalho de Melo1b2e2df2012-04-19 10:57:06 -03001005 len = symbol__size(sym);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001006
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -02001007 printf(" Percent | Source code & Disassembly of %s\n", d_filename);
1008 printf("------------------------------------------------\n");
1009
1010 if (verbose)
1011 symbol__annotate_hits(sym, evidx);
1012
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -02001013 list_for_each_entry(pos, &notes->src->source, node) {
Arnaldo Carvalho de Melod5e3d742011-02-08 15:29:25 -02001014 if (context && queue == NULL) {
1015 queue = pos;
1016 queue_len = 0;
1017 }
1018
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -03001019 switch (disasm_line__print(pos, sym, start, evidx, len,
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -03001020 min_pcnt, printed, max_lines,
1021 queue)) {
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -02001022 case 0:
1023 ++printed;
Arnaldo Carvalho de Melod5e3d742011-02-08 15:29:25 -02001024 if (context) {
1025 printed += queue_len;
1026 queue = NULL;
1027 queue_len = 0;
1028 }
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -02001029 break;
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -02001030 case 1:
1031 /* filtered by max_lines */
1032 ++more;
1033 break;
1034 case -1:
1035 default:
Arnaldo Carvalho de Melod5e3d742011-02-08 15:29:25 -02001036 /*
1037 * Filtered by min_pcnt or non IP lines when
1038 * context != 0
1039 */
1040 if (!context)
1041 break;
1042 if (queue_len == context)
1043 queue = list_entry(queue->node.next, typeof(*queue), node);
1044 else
1045 ++queue_len;
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -02001046 break;
1047 }
1048 }
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -02001049
David Ahernbfd14b92012-09-08 09:06:50 -06001050 free(filename);
1051
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -02001052 return more;
1053}
1054
1055void symbol__annotate_zero_histogram(struct symbol *sym, int evidx)
1056{
1057 struct annotation *notes = symbol__annotation(sym);
1058 struct sym_hist *h = annotation__histogram(notes, evidx);
1059
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -02001060 memset(h, 0, notes->src->sizeof_sym_hist);
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -02001061}
1062
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -02001063void symbol__annotate_decay_histogram(struct symbol *sym, int evidx)
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -02001064{
1065 struct annotation *notes = symbol__annotation(sym);
1066 struct sym_hist *h = annotation__histogram(notes, evidx);
Arnaldo Carvalho de Melo1b2e2df2012-04-19 10:57:06 -03001067 int len = symbol__size(sym), offset;
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -02001068
1069 h->sum = 0;
Arnaldo Carvalho de Melo8b84a562012-04-05 16:15:59 -03001070 for (offset = 0; offset < len; ++offset) {
1071 h->addr[offset] = h->addr[offset] * 7 / 8;
1072 h->sum += h->addr[offset];
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -02001073 }
1074}
1075
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -03001076void disasm__purge(struct list_head *head)
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -02001077{
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -03001078 struct disasm_line *pos, *n;
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -02001079
1080 list_for_each_entry_safe(pos, n, head, node) {
1081 list_del(&pos->node);
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -03001082 disasm_line__free(pos);
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -02001083 }
1084}
1085
Arnaldo Carvalho de Melo51454182012-04-15 15:52:18 -03001086static size_t disasm_line__fprintf(struct disasm_line *dl, FILE *fp)
1087{
1088 size_t printed;
1089
1090 if (dl->offset == -1)
1091 return fprintf(fp, "%s\n", dl->line);
1092
1093 printed = fprintf(fp, "%#" PRIx64 " %s", dl->offset, dl->name);
1094
Arnaldo Carvalho de Meloc7e6ead2012-04-20 14:38:46 -03001095 if (dl->ops.raw[0] != '\0') {
Arnaldo Carvalho de Melo51454182012-04-15 15:52:18 -03001096 printed += fprintf(fp, "%.*s %s\n", 6 - (int)printed, " ",
Arnaldo Carvalho de Meloc7e6ead2012-04-20 14:38:46 -03001097 dl->ops.raw);
Arnaldo Carvalho de Melo51454182012-04-15 15:52:18 -03001098 }
1099
1100 return printed + fprintf(fp, "\n");
1101}
1102
1103size_t disasm__fprintf(struct list_head *head, FILE *fp)
1104{
1105 struct disasm_line *pos;
1106 size_t printed = 0;
1107
1108 list_for_each_entry(pos, head, node)
1109 printed += disasm_line__fprintf(pos, fp);
1110
1111 return printed;
1112}
1113
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -02001114int symbol__tty_annotate(struct symbol *sym, struct map *map, int evidx,
1115 bool print_lines, bool full_paths, int min_pcnt,
1116 int max_lines)
1117{
1118 struct dso *dso = map->dso;
1119 const char *filename = dso->long_name;
1120 struct rb_root source_line = RB_ROOT;
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -02001121 u64 len;
1122
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -02001123 if (symbol__annotate(sym, map, 0) < 0)
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -02001124 return -1;
1125
Arnaldo Carvalho de Melo1b2e2df2012-04-19 10:57:06 -03001126 len = symbol__size(sym);
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -02001127
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001128 if (print_lines) {
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -02001129 symbol__get_source_line(sym, map, evidx, &source_line,
1130 len, filename);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001131 print_summary(&source_line, filename);
1132 }
1133
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -02001134 symbol__annotate_printf(sym, map, evidx, full_paths,
Arnaldo Carvalho de Melod5e3d742011-02-08 15:29:25 -02001135 min_pcnt, max_lines, 0);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001136 if (print_lines)
1137 symbol__free_source_line(sym, len);
1138
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -03001139 disasm__purge(&symbol__annotation(sym)->src->source);
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -02001140
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001141 return 0;
1142}