perf tools: Move build_id__sprintf into build-id object
[deliverable/linux.git] / tools / perf / util / annotate.c
CommitLineData
78f7defe
ACM
1/*
2 * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
3 *
4 * Parts came from builtin-annotate.c, see those files for further
5 * copyright notes.
6 *
7 * Released under the GPL v2. (and only v2, not any later version)
8 */
9
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"
ce6f4fab 17#include <pthread.h>
78f7defe 18
f69b64f7 19const char *disassembler_style;
7a4ec938 20const char *objdump_path;
f69b64f7 21
7a997fe4
ACM
22static struct ins *ins__find(const char *name);
23static int disasm_line__parse(char *line, char **namep, char **rawp);
24
c46219ac
ACM
25static 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
5417072b
ACM
33static 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
c7e6ead7 48static int call__parse(struct ins_operands *ops)
d86b0597 49{
d2232885
ACM
50 char *endptr, *tok, *name;
51
44d1a3ed 52 ops->target.addr = strtoull(ops->raw, &endptr, 16);
d2232885
ACM
53
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';
44d1a3ed 65 ops->target.name = strdup(name);
d2232885
ACM
66 *tok = '>';
67
44d1a3ed 68 return ops->target.name == NULL ? -1 : 0;
d2232885
ACM
69
70indirect_call:
e8ea1561
ACM
71 tok = strchr(endptr, '(');
72 if (tok != NULL) {
73 ops->target.addr = 0;
74 return 0;
75 }
76
d2232885
ACM
77 tok = strchr(endptr, '*');
78 if (tok == NULL)
79 return -1;
80
44d1a3ed 81 ops->target.addr = strtoull(tok + 1, NULL, 16);
d86b0597
ACM
82 return 0;
83}
84
d2232885 85static int call__scnprintf(struct ins *ins, char *bf, size_t size,
5417072b 86 struct ins_operands *ops)
d2232885 87{
44d1a3ed
ACM
88 if (ops->target.name)
89 return scnprintf(bf, size, "%-6.6s %s", ins->name, ops->target.name);
d2232885 90
e8ea1561
ACM
91 if (ops->target.addr == 0)
92 return ins__raw_scnprintf(ins, bf, size, ops);
93
44d1a3ed 94 return scnprintf(bf, size, "%-6.6s *%" PRIx64, ins->name, ops->target.addr);
d2232885
ACM
95}
96
d86b0597 97static struct ins_ops call_ops = {
d2232885
ACM
98 .parse = call__parse,
99 .scnprintf = call__scnprintf,
d86b0597
ACM
100};
101
102bool ins__is_call(const struct ins *ins)
103{
104 return ins->ops == &call_ops;
105}
106
c7e6ead7 107static int jump__parse(struct ins_operands *ops)
4f9d0325 108{
c7e6ead7 109 const char *s = strchr(ops->raw, '+');
4f9d0325 110
fb29fa58
ACM
111 ops->target.addr = strtoll(ops->raw, NULL, 16);
112
113 if (s++ != NULL)
114 ops->target.offset = strtoll(s, NULL, 16);
115 else
116 ops->target.offset = UINT64_MAX;
4f9d0325 117
4f9d0325
ACM
118 return 0;
119}
120
c7e6ead7 121static int jump__scnprintf(struct ins *ins, char *bf, size_t size,
5417072b 122 struct ins_operands *ops)
28548d78 123{
44d1a3ed 124 return scnprintf(bf, size, "%-6.6s %" PRIx64, ins->name, ops->target.offset);
28548d78
ACM
125}
126
4f9d0325 127static struct ins_ops jump_ops = {
c7e6ead7
ACM
128 .parse = jump__parse,
129 .scnprintf = jump__scnprintf,
4f9d0325
ACM
130};
131
132bool ins__is_jump(const struct ins *ins)
133{
134 return ins->ops == &jump_ops;
135}
136
6de783b6
ACM
137static 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
7a997fe4
ACM
162static 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
c46219ac
ACM
204static 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
7a997fe4 211static struct ins_ops lock_ops = {
c46219ac 212 .free = lock__delete,
7a997fe4
ACM
213 .parse = lock__parse,
214 .scnprintf = lock__scnprintf,
215};
216
6de783b6
ACM
217static 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
a43712c4
ACM
275static 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
1d037ca1
IT
316static int nop__scnprintf(struct ins *ins __maybe_unused, char *bf, size_t size,
317 struct ins_operands *ops __maybe_unused)
b9818e93
ACM
318{
319 return scnprintf(bf, size, "%-6.6s", "nop");
320}
321
322static struct ins_ops nop_ops = {
323 .scnprintf = nop__scnprintf,
324};
325
4f9d0325
ACM
326/*
327 * Must be sorted by name!
328 */
329static struct ins instructions[] = {
6de783b6
ACM
330 { .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, },
7a997fe4 335 { .name = "bts", .ops = &mov_ops, },
d86b0597
ACM
336 { .name = "call", .ops = &call_ops, },
337 { .name = "callq", .ops = &call_ops, },
6de783b6
ACM
338 { .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, },
a43712c4
ACM
344 { .name = "dec", .ops = &dec_ops, },
345 { .name = "decl", .ops = &dec_ops, },
6de783b6 346 { .name = "imul", .ops = &mov_ops, },
a43712c4
ACM
347 { .name = "inc", .ops = &dec_ops, },
348 { .name = "incl", .ops = &dec_ops, },
4f9d0325 349 { .name = "ja", .ops = &jump_ops, },
3f862fd0
ACM
350 { .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, },
4f9d0325 355 { .name = "je", .ops = &jump_ops, },
3f862fd0
ACM
356 { .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, },
4f9d0325
ACM
361 { .name = "jmp", .ops = &jump_ops, },
362 { .name = "jmpq", .ops = &jump_ops, },
3f862fd0
ACM
363 { .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, },
4f9d0325 368 { .name = "jne", .ops = &jump_ops, },
3f862fd0
ACM
369 { .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, },
4f9d0325 382 { .name = "js", .ops = &jump_ops, },
3f862fd0 383 { .name = "jz", .ops = &jump_ops, },
6de783b6 384 { .name = "lea", .ops = &mov_ops, },
7a997fe4 385 { .name = "lock", .ops = &lock_ops, },
6de783b6
ACM
386 { .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, },
b9818e93
ACM
394 { .name = "nop", .ops = &nop_ops, },
395 { .name = "nopl", .ops = &nop_ops, },
396 { .name = "nopw", .ops = &nop_ops, },
6de783b6
ACM
397 { .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, },
7a997fe4 402 { .name = "xadd", .ops = &mov_ops, },
4f9d0325
ACM
403};
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
1d037ca1 419int symbol__annotate_init(struct map *map __maybe_unused, struct symbol *sym)
78f7defe
ACM
420{
421 struct annotation *notes = symbol__annotation(sym);
ce6f4fab
ACM
422 pthread_mutex_init(&notes->lock, NULL);
423 return 0;
424}
78f7defe 425
d04b35f8 426int symbol__alloc_hist(struct symbol *sym)
ce6f4fab
ACM
427{
428 struct annotation *notes = symbol__annotation(sym);
1b2e2df4 429 const size_t size = symbol__size(sym);
8696329b
CS
430 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;
ce6f4fab 442
d04b35f8 443 notes->src = zalloc(sizeof(*notes->src) + symbol_conf.nr_events * sizeof_sym_hist);
ce6f4fab
ACM
444 if (notes->src == NULL)
445 return -1;
446 notes->src->sizeof_sym_hist = sizeof_sym_hist;
d04b35f8 447 notes->src->nr_histograms = symbol_conf.nr_events;
ce6f4fab
ACM
448 INIT_LIST_HEAD(&notes->src->source);
449 return 0;
78f7defe
ACM
450}
451
36532461
ACM
452void symbol__annotate_zero_histograms(struct symbol *sym)
453{
454 struct annotation *notes = symbol__annotation(sym);
455
ce6f4fab
ACM
456 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);
36532461
ACM
461}
462
2f525d01
ACM
463int symbol__inc_addr_samples(struct symbol *sym, struct map *map,
464 int evidx, u64 addr)
78f7defe 465{
2f525d01 466 unsigned offset;
78f7defe
ACM
467 struct annotation *notes;
468 struct sym_hist *h;
469
78f7defe 470 notes = symbol__annotation(sym);
ce6f4fab 471 if (notes->src == NULL)
78f7defe
ACM
472 return -ENOMEM;
473
78f7defe
ACM
474 pr_debug3("%s: addr=%#" PRIx64 "\n", __func__, map->unmap_ip(map, addr));
475
31d68e7b
ACM
476 if (addr < sym->start || addr > sym->end)
477 return -ERANGE;
78f7defe 478
2f525d01
ACM
479 offset = addr - sym->start;
480 h = annotation__histogram(notes, evidx);
78f7defe
ACM
481 h->sum++;
482 h->addr[offset]++;
483
484 pr_debug3("%#" PRIx64 " %s: period++ [addr: %#" PRIx64 ", %#" PRIx64
2f525d01
ACM
485 ", evidx=%d] => %" PRIu64 "\n", sym->start, sym->name,
486 addr, addr - sym->start, evidx, h->addr[offset]);
78f7defe
ACM
487 return 0;
488}
489
4f9d0325
ACM
490static 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
c7e6ead7
ACM
500 if (dl->ins->ops->parse)
501 dl->ins->ops->parse(&dl->ops);
4f9d0325
ACM
502}
503
7a997fe4
ACM
504static 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
29ed6e76 542static struct disasm_line *disasm_line__new(s64 offset, char *line, size_t privsize)
78f7defe 543{
5145418b 544 struct disasm_line *dl = zalloc(sizeof(*dl) + privsize);
78f7defe 545
29ed6e76
ACM
546 if (dl != NULL) {
547 dl->offset = offset;
548 dl->line = strdup(line);
549 if (dl->line == NULL)
058b4cc9 550 goto out_delete;
5145418b
ACM
551
552 if (offset != -1) {
7a997fe4 553 if (disasm_line__parse(dl->line, &dl->name, &dl->ops.raw) < 0)
5145418b
ACM
554 goto out_free_line;
555
4f9d0325 556 disasm_line__init_ins(dl);
5145418b 557 }
78f7defe
ACM
558 }
559
29ed6e76 560 return dl;
5145418b
ACM
561
562out_free_line:
563 free(dl->line);
058b4cc9 564out_delete:
29ed6e76 565 free(dl);
058b4cc9 566 return NULL;
78f7defe
ACM
567}
568
29ed6e76 569void disasm_line__free(struct disasm_line *dl)
78f7defe 570{
29ed6e76 571 free(dl->line);
5145418b 572 free(dl->name);
c46219ac
ACM
573 if (dl->ins && dl->ins->ops->free)
574 dl->ins->ops->free(&dl->ops);
575 else
576 ins__delete(&dl->ops);
29ed6e76 577 free(dl);
78f7defe
ACM
578}
579
5417072b
ACM
580int 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
29ed6e76 588static void disasm__add(struct list_head *head, struct disasm_line *line)
78f7defe
ACM
589{
590 list_add_tail(&line->node, head);
591}
592
29ed6e76 593struct disasm_line *disasm__get_next_ip_line(struct list_head *head, struct disasm_line *pos)
78f7defe
ACM
594{
595 list_for_each_entry_continue(pos, head, node)
596 if (pos->offset >= 0)
597 return pos;
598
599 return NULL;
600}
601
29ed6e76
ACM
602static 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)
78f7defe
ACM
605{
606 static const char *prev_line;
607 static const char *prev_color;
608
29ed6e76 609 if (dl->offset != -1) {
78f7defe
ACM
610 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);
ce6f4fab 615 struct source_line *src_line = notes->src->lines;
2f525d01 616 struct sym_hist *h = annotation__histogram(notes, evidx);
29ed6e76 617 s64 offset = dl->offset;
058b4cc9 618 const u64 addr = start + offset;
29ed6e76 619 struct disasm_line *next;
ce6f4fab 620
29ed6e76 621 next = disasm__get_next_ip_line(&notes->src->source, dl);
78f7defe
ACM
622
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
d040bd36 638 if (percent < min_pcnt)
36532461
ACM
639 return -1;
640
e3087b80 641 if (max_lines && printed >= max_lines)
36532461 642 return 1;
d040bd36 643
d5e3d747
ACM
644 if (queue != NULL) {
645 list_for_each_entry_from(queue, &notes->src->source, node) {
29ed6e76 646 if (queue == dl)
d5e3d747 647 break;
29ed6e76 648 disasm_line__print(queue, sym, start, evidx, len,
d5e3d747
ACM
649 0, 0, 1, NULL);
650 }
651 }
652
78f7defe
ACM
653 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(" : ");
058b4cc9 671 color_fprintf(stdout, PERF_COLOR_MAGENTA, " %" PRIx64 ":", addr);
29ed6e76 672 color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", dl->line);
e3087b80 673 } else if (max_lines && printed >= max_lines)
36532461
ACM
674 return 1;
675 else {
d5e3d747
ACM
676 if (queue)
677 return -1;
678
29ed6e76 679 if (!*dl->line)
78f7defe
ACM
680 printf(" :\n");
681 else
29ed6e76 682 printf(" : %s\n", dl->line);
78f7defe 683 }
36532461
ACM
684
685 return 0;
78f7defe
ACM
686}
687
ce6f4fab
ACM
688static int symbol__parse_objdump_line(struct symbol *sym, struct map *map,
689 FILE *file, size_t privsize)
78f7defe 690{
ce6f4fab 691 struct annotation *notes = symbol__annotation(sym);
29ed6e76 692 struct disasm_line *dl;
058b4cc9 693 char *line = NULL, *parsed_line, *tmp, *tmp2, *c;
78f7defe
ACM
694 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;
a31b7cc0 711 parsed_line = line;
78f7defe
ACM
712
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;
058b4cc9
ACM
739 else
740 parsed_line = tmp2 + 1;
a31b7cc0 741 }
78f7defe 742
29ed6e76 743 dl = disasm_line__new(offset, parsed_line, privsize);
058b4cc9
ACM
744 free(line);
745
29ed6e76 746 if (dl == NULL)
78f7defe 747 return -1;
058b4cc9 748
29ed6e76 749 disasm__add(&notes->src->source, dl);
78f7defe
ACM
750
751 return 0;
752}
753
ce6f4fab 754int symbol__annotate(struct symbol *sym, struct map *map, size_t privsize)
78f7defe
ACM
755{
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;
78f7defe
ACM
762 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
44f24cb3 792 if (dso->symtab_type == DSO_BINARY_TYPE__KALLSYMS) {
170ae6bc
ACM
793 char bf[BUILD_ID_SIZE * 2 + 16] = " with build id ";
794 char *build_id_msg = NULL;
795
78f7defe
ACM
796 if (dso->annotate_warned)
797 goto out_free_filename;
170ae6bc
ACM
798
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 }
78f7defe
ACM
804 err = -ENOENT;
805 dso->annotate_warned = 1;
ae55795e
ACM
806 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"
ff2a6617 811 " --vmlinux vmlinux\n",
170ae6bc 812 sym->name, build_id_msg ?: "");
78f7defe
ACM
813 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
78f7defe
ACM
820 pr_debug("annotating [%p] %30s : [%p] %30s\n",
821 dso, dso->long_name, sym, sym->name);
822
823 snprintf(command, sizeof(command),
7a4ec938 824 "%s %s%s --start-address=0x%016" PRIx64
3e6a2a7f
SE
825 " --stop-address=0x%016" PRIx64
826 " -d %s %s -C %s|grep -v %s|expand",
7a4ec938 827 objdump_path ? objdump_path : "objdump",
f69b64f7
AK
828 disassembler_style ? "-M " : "",
829 disassembler_style ? disassembler_style : "",
78f7defe 830 map__rip_2objdump(map, sym->start),
f41612f4 831 map__rip_2objdump(map, sym->end+1),
3e6a2a7f
SE
832 symbol_conf.annotate_asm_raw ? "" : "--no-show-raw",
833 symbol_conf.annotate_src ? "-S" : "",
78f7defe
ACM
834 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))
ce6f4fab 843 if (symbol__parse_objdump_line(sym, map, file, privsize) < 0)
78f7defe
ACM
844 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);
ce6f4fab 876 struct source_line *src_line = notes->src->lines;
78f7defe
ACM
877 int i;
878
879 for (i = 0; i < len; i++)
880 free(src_line[i].path);
881
882 free(src_line);
ce6f4fab 883 notes->src->lines = NULL;
78f7defe
ACM
884}
885
886/* Get the filename:line for the colored entries */
887static int symbol__get_source_line(struct symbol *sym, struct map *map,
2f525d01 888 int evidx, struct rb_root *root, int len,
78f7defe
ACM
889 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);
2f525d01 896 struct sym_hist *h = annotation__histogram(notes, evidx);
78f7defe
ACM
897
898 if (!h->sum)
899 return 0;
900
ce6f4fab
ACM
901 src_line = notes->src->lines = calloc(len, sizeof(struct source_line));
902 if (!notes->src->lines)
78f7defe
ACM
903 return -1;
904
f40a0633 905 start = map__rip_2objdump(map, sym->start);
78f7defe
ACM
906
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
2f525d01 969static void symbol__annotate_hits(struct symbol *sym, int evidx)
78f7defe
ACM
970{
971 struct annotation *notes = symbol__annotation(sym);
2f525d01 972 struct sym_hist *h = annotation__histogram(notes, evidx);
1b2e2df4 973 u64 len = symbol__size(sym), offset;
78f7defe
ACM
974
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
ce6f4fab 982int symbol__annotate_printf(struct symbol *sym, struct map *map, int evidx,
d5e3d747
ACM
983 bool full_paths, int min_pcnt, int max_lines,
984 int context)
78f7defe
ACM
985{
986 struct dso *dso = map->dso;
bfd14b9a
DA
987 char *filename;
988 const char *d_filename;
ce6f4fab 989 struct annotation *notes = symbol__annotation(sym);
29ed6e76 990 struct disasm_line *pos, *queue = NULL;
058b4cc9 991 u64 start = map__rip_2objdump(map, sym->start);
d5e3d747 992 int printed = 2, queue_len = 0;
36532461 993 int more = 0;
78f7defe
ACM
994 u64 len;
995
bfd14b9a
DA
996 filename = strdup(dso->long_name);
997 if (!filename)
998 return -ENOMEM;
999
78f7defe
ACM
1000 if (full_paths)
1001 d_filename = filename;
1002 else
1003 d_filename = basename(filename);
1004
1b2e2df4 1005 len = symbol__size(sym);
78f7defe 1006
78f7defe
ACM
1007 printf(" Percent | Source code & Disassembly of %s\n", d_filename);
1008 printf("------------------------------------------------\n");
1009
1010 if (verbose)
2f525d01 1011 symbol__annotate_hits(sym, evidx);
78f7defe 1012
ce6f4fab 1013 list_for_each_entry(pos, &notes->src->source, node) {
d5e3d747
ACM
1014 if (context && queue == NULL) {
1015 queue = pos;
1016 queue_len = 0;
1017 }
1018
29ed6e76 1019 switch (disasm_line__print(pos, sym, start, evidx, len,
058b4cc9
ACM
1020 min_pcnt, printed, max_lines,
1021 queue)) {
36532461
ACM
1022 case 0:
1023 ++printed;
d5e3d747
ACM
1024 if (context) {
1025 printed += queue_len;
1026 queue = NULL;
1027 queue_len = 0;
1028 }
36532461
ACM
1029 break;
1030 case 1:
1031 /* filtered by max_lines */
1032 ++more;
d040bd36 1033 break;
36532461
ACM
1034 case -1:
1035 default:
d5e3d747
ACM
1036 /*
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;
36532461
ACM
1046 break;
1047 }
1048 }
1049
bfd14b9a
DA
1050 free(filename);
1051
36532461
ACM
1052 return more;
1053}
f1e2701d 1054
36532461
ACM
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
ce6f4fab 1060 memset(h, 0, notes->src->sizeof_sym_hist);
36532461
ACM
1061}
1062
ce6f4fab 1063void symbol__annotate_decay_histogram(struct symbol *sym, int evidx)
36532461
ACM
1064{
1065 struct annotation *notes = symbol__annotation(sym);
1066 struct sym_hist *h = annotation__histogram(notes, evidx);
1b2e2df4 1067 int len = symbol__size(sym), offset;
36532461
ACM
1068
1069 h->sum = 0;
8b84a568
ACM
1070 for (offset = 0; offset < len; ++offset) {
1071 h->addr[offset] = h->addr[offset] * 7 / 8;
1072 h->sum += h->addr[offset];
f1e2701d
ACM
1073 }
1074}
1075
29ed6e76 1076void disasm__purge(struct list_head *head)
f1e2701d 1077{
29ed6e76 1078 struct disasm_line *pos, *n;
f1e2701d
ACM
1079
1080 list_for_each_entry_safe(pos, n, head, node) {
1081 list_del(&pos->node);
29ed6e76 1082 disasm_line__free(pos);
f1e2701d
ACM
1083 }
1084}
1085
5145418b
ACM
1086static 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
c7e6ead7 1095 if (dl->ops.raw[0] != '\0') {
5145418b 1096 printed += fprintf(fp, "%.*s %s\n", 6 - (int)printed, " ",
c7e6ead7 1097 dl->ops.raw);
5145418b
ACM
1098 }
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
f1e2701d
ACM
1114int 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;
f1e2701d
ACM
1121 u64 len;
1122
ce6f4fab 1123 if (symbol__annotate(sym, map, 0) < 0)
f1e2701d
ACM
1124 return -1;
1125
1b2e2df4 1126 len = symbol__size(sym);
f1e2701d
ACM
1127
1128 if (print_lines) {
1129 symbol__get_source_line(sym, map, evidx, &source_line,
1130 len, filename);
1131 print_summary(&source_line, filename);
78f7defe
ACM
1132 }
1133
ce6f4fab 1134 symbol__annotate_printf(sym, map, evidx, full_paths,
d5e3d747 1135 min_pcnt, max_lines, 0);
78f7defe
ACM
1136 if (print_lines)
1137 symbol__free_source_line(sym, len);
1138
29ed6e76 1139 disasm__purge(&symbol__annotation(sym)->src->source);
f1e2701d 1140
78f7defe
ACM
1141 return 0;
1142}
This page took 0.138471 seconds and 5 git commands to generate.