]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - tools/perf/util/annotate.c
perf annotate: Use raw form for register indirect call instructions
[mirror_ubuntu-artful-kernel.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
AK
19const char *disassembler_style;
20
5417072b
ACM
21static int ins__raw_scnprintf(struct ins *ins, char *bf, size_t size,
22 struct ins_operands *ops)
23{
24 return scnprintf(bf, size, "%-6.6s %s", ins->name, ops->raw);
25}
26
27int ins__scnprintf(struct ins *ins, char *bf, size_t size,
28 struct ins_operands *ops)
29{
30 if (ins->ops->scnprintf)
31 return ins->ops->scnprintf(ins, bf, size, ops);
32
33 return ins__raw_scnprintf(ins, bf, size, ops);
34}
35
c7e6ead7 36static int call__parse(struct ins_operands *ops)
d86b0597 37{
d2232885
ACM
38 char *endptr, *tok, *name;
39
44d1a3ed 40 ops->target.addr = strtoull(ops->raw, &endptr, 16);
d2232885
ACM
41
42 name = strchr(endptr, '<');
43 if (name == NULL)
44 goto indirect_call;
45
46 name++;
47
48 tok = strchr(name, '>');
49 if (tok == NULL)
50 return -1;
51
52 *tok = '\0';
44d1a3ed 53 ops->target.name = strdup(name);
d2232885
ACM
54 *tok = '>';
55
44d1a3ed 56 return ops->target.name == NULL ? -1 : 0;
d2232885
ACM
57
58indirect_call:
e8ea1561
ACM
59 tok = strchr(endptr, '(');
60 if (tok != NULL) {
61 ops->target.addr = 0;
62 return 0;
63 }
64
d2232885
ACM
65 tok = strchr(endptr, '*');
66 if (tok == NULL)
67 return -1;
68
44d1a3ed 69 ops->target.addr = strtoull(tok + 1, NULL, 16);
d86b0597
ACM
70 return 0;
71}
72
d2232885 73static int call__scnprintf(struct ins *ins, char *bf, size_t size,
5417072b 74 struct ins_operands *ops)
d2232885 75{
44d1a3ed
ACM
76 if (ops->target.name)
77 return scnprintf(bf, size, "%-6.6s %s", ins->name, ops->target.name);
d2232885 78
e8ea1561
ACM
79 if (ops->target.addr == 0)
80 return ins__raw_scnprintf(ins, bf, size, ops);
81
44d1a3ed 82 return scnprintf(bf, size, "%-6.6s *%" PRIx64, ins->name, ops->target.addr);
d2232885
ACM
83}
84
d86b0597 85static struct ins_ops call_ops = {
d2232885
ACM
86 .parse = call__parse,
87 .scnprintf = call__scnprintf,
d86b0597
ACM
88};
89
90bool ins__is_call(const struct ins *ins)
91{
92 return ins->ops == &call_ops;
93}
94
c7e6ead7 95static int jump__parse(struct ins_operands *ops)
4f9d0325 96{
c7e6ead7 97 const char *s = strchr(ops->raw, '+');
4f9d0325 98
fb29fa58
ACM
99 ops->target.addr = strtoll(ops->raw, NULL, 16);
100
101 if (s++ != NULL)
102 ops->target.offset = strtoll(s, NULL, 16);
103 else
104 ops->target.offset = UINT64_MAX;
4f9d0325 105
4f9d0325
ACM
106 return 0;
107}
108
c7e6ead7 109static int jump__scnprintf(struct ins *ins, char *bf, size_t size,
5417072b 110 struct ins_operands *ops)
28548d78 111{
44d1a3ed 112 return scnprintf(bf, size, "%-6.6s %" PRIx64, ins->name, ops->target.offset);
28548d78
ACM
113}
114
4f9d0325 115static struct ins_ops jump_ops = {
c7e6ead7
ACM
116 .parse = jump__parse,
117 .scnprintf = jump__scnprintf,
4f9d0325
ACM
118};
119
120bool ins__is_jump(const struct ins *ins)
121{
122 return ins->ops == &jump_ops;
123}
124
b9818e93
ACM
125static int nop__scnprintf(struct ins *ins __used, char *bf, size_t size,
126 struct ins_operands *ops __used)
127{
128 return scnprintf(bf, size, "%-6.6s", "nop");
129}
130
131static struct ins_ops nop_ops = {
132 .scnprintf = nop__scnprintf,
133};
134
4f9d0325
ACM
135/*
136 * Must be sorted by name!
137 */
138static struct ins instructions[] = {
d86b0597
ACM
139 { .name = "call", .ops = &call_ops, },
140 { .name = "callq", .ops = &call_ops, },
4f9d0325 141 { .name = "ja", .ops = &jump_ops, },
3f862fd0
ACM
142 { .name = "jae", .ops = &jump_ops, },
143 { .name = "jb", .ops = &jump_ops, },
144 { .name = "jbe", .ops = &jump_ops, },
145 { .name = "jc", .ops = &jump_ops, },
146 { .name = "jcxz", .ops = &jump_ops, },
4f9d0325 147 { .name = "je", .ops = &jump_ops, },
3f862fd0
ACM
148 { .name = "jecxz", .ops = &jump_ops, },
149 { .name = "jg", .ops = &jump_ops, },
150 { .name = "jge", .ops = &jump_ops, },
151 { .name = "jl", .ops = &jump_ops, },
152 { .name = "jle", .ops = &jump_ops, },
4f9d0325
ACM
153 { .name = "jmp", .ops = &jump_ops, },
154 { .name = "jmpq", .ops = &jump_ops, },
3f862fd0
ACM
155 { .name = "jna", .ops = &jump_ops, },
156 { .name = "jnae", .ops = &jump_ops, },
157 { .name = "jnb", .ops = &jump_ops, },
158 { .name = "jnbe", .ops = &jump_ops, },
159 { .name = "jnc", .ops = &jump_ops, },
4f9d0325 160 { .name = "jne", .ops = &jump_ops, },
3f862fd0
ACM
161 { .name = "jng", .ops = &jump_ops, },
162 { .name = "jnge", .ops = &jump_ops, },
163 { .name = "jnl", .ops = &jump_ops, },
164 { .name = "jnle", .ops = &jump_ops, },
165 { .name = "jno", .ops = &jump_ops, },
166 { .name = "jnp", .ops = &jump_ops, },
167 { .name = "jns", .ops = &jump_ops, },
168 { .name = "jnz", .ops = &jump_ops, },
169 { .name = "jo", .ops = &jump_ops, },
170 { .name = "jp", .ops = &jump_ops, },
171 { .name = "jpe", .ops = &jump_ops, },
172 { .name = "jpo", .ops = &jump_ops, },
173 { .name = "jrcxz", .ops = &jump_ops, },
4f9d0325 174 { .name = "js", .ops = &jump_ops, },
3f862fd0 175 { .name = "jz", .ops = &jump_ops, },
b9818e93
ACM
176 { .name = "nop", .ops = &nop_ops, },
177 { .name = "nopl", .ops = &nop_ops, },
178 { .name = "nopw", .ops = &nop_ops, },
4f9d0325
ACM
179};
180
181static int ins__cmp(const void *name, const void *insp)
182{
183 const struct ins *ins = insp;
184
185 return strcmp(name, ins->name);
186}
187
188static struct ins *ins__find(const char *name)
189{
190 const int nmemb = ARRAY_SIZE(instructions);
191
192 return bsearch(name, instructions, nmemb, sizeof(struct ins), ins__cmp);
193}
194
ce6f4fab 195int symbol__annotate_init(struct map *map __used, struct symbol *sym)
78f7defe
ACM
196{
197 struct annotation *notes = symbol__annotation(sym);
ce6f4fab
ACM
198 pthread_mutex_init(&notes->lock, NULL);
199 return 0;
200}
78f7defe 201
d04b35f8 202int symbol__alloc_hist(struct symbol *sym)
ce6f4fab
ACM
203{
204 struct annotation *notes = symbol__annotation(sym);
1b2e2df4 205 const size_t size = symbol__size(sym);
64c17be4 206 size_t sizeof_sym_hist = (sizeof(struct sym_hist) + size * sizeof(u64));
ce6f4fab 207
d04b35f8 208 notes->src = zalloc(sizeof(*notes->src) + symbol_conf.nr_events * sizeof_sym_hist);
ce6f4fab
ACM
209 if (notes->src == NULL)
210 return -1;
211 notes->src->sizeof_sym_hist = sizeof_sym_hist;
d04b35f8 212 notes->src->nr_histograms = symbol_conf.nr_events;
ce6f4fab
ACM
213 INIT_LIST_HEAD(&notes->src->source);
214 return 0;
78f7defe
ACM
215}
216
36532461
ACM
217void symbol__annotate_zero_histograms(struct symbol *sym)
218{
219 struct annotation *notes = symbol__annotation(sym);
220
ce6f4fab
ACM
221 pthread_mutex_lock(&notes->lock);
222 if (notes->src != NULL)
223 memset(notes->src->histograms, 0,
224 notes->src->nr_histograms * notes->src->sizeof_sym_hist);
225 pthread_mutex_unlock(&notes->lock);
36532461
ACM
226}
227
2f525d01
ACM
228int symbol__inc_addr_samples(struct symbol *sym, struct map *map,
229 int evidx, u64 addr)
78f7defe 230{
2f525d01 231 unsigned offset;
78f7defe
ACM
232 struct annotation *notes;
233 struct sym_hist *h;
234
78f7defe 235 notes = symbol__annotation(sym);
ce6f4fab 236 if (notes->src == NULL)
78f7defe
ACM
237 return -ENOMEM;
238
78f7defe
ACM
239 pr_debug3("%s: addr=%#" PRIx64 "\n", __func__, map->unmap_ip(map, addr));
240
31d68e7b
ACM
241 if (addr < sym->start || addr > sym->end)
242 return -ERANGE;
78f7defe 243
2f525d01
ACM
244 offset = addr - sym->start;
245 h = annotation__histogram(notes, evidx);
78f7defe
ACM
246 h->sum++;
247 h->addr[offset]++;
248
249 pr_debug3("%#" PRIx64 " %s: period++ [addr: %#" PRIx64 ", %#" PRIx64
2f525d01
ACM
250 ", evidx=%d] => %" PRIu64 "\n", sym->start, sym->name,
251 addr, addr - sym->start, evidx, h->addr[offset]);
78f7defe
ACM
252 return 0;
253}
254
4f9d0325
ACM
255static void disasm_line__init_ins(struct disasm_line *dl)
256{
257 dl->ins = ins__find(dl->name);
258
259 if (dl->ins == NULL)
260 return;
261
262 if (!dl->ins->ops)
263 return;
264
c7e6ead7
ACM
265 if (dl->ins->ops->parse)
266 dl->ins->ops->parse(&dl->ops);
4f9d0325
ACM
267}
268
29ed6e76 269static struct disasm_line *disasm_line__new(s64 offset, char *line, size_t privsize)
78f7defe 270{
5145418b 271 struct disasm_line *dl = zalloc(sizeof(*dl) + privsize);
78f7defe 272
29ed6e76
ACM
273 if (dl != NULL) {
274 dl->offset = offset;
275 dl->line = strdup(line);
276 if (dl->line == NULL)
058b4cc9 277 goto out_delete;
5145418b
ACM
278
279 if (offset != -1) {
280 char *name = dl->line, tmp;
281
282 while (isspace(name[0]))
283 ++name;
284
285 if (name[0] == '\0')
286 goto out_delete;
287
c7e6ead7 288 dl->ops.raw = name + 1;
5145418b 289
c7e6ead7
ACM
290 while (dl->ops.raw[0] != '\0' &&
291 !isspace(dl->ops.raw[0]))
292 ++dl->ops.raw;
5145418b 293
c7e6ead7
ACM
294 tmp = dl->ops.raw[0];
295 dl->ops.raw[0] = '\0';
5145418b
ACM
296 dl->name = strdup(name);
297
298 if (dl->name == NULL)
299 goto out_free_line;
300
c7e6ead7 301 dl->ops.raw[0] = tmp;
5145418b 302
c7e6ead7
ACM
303 if (dl->ops.raw[0] != '\0') {
304 dl->ops.raw++;
305 while (isspace(dl->ops.raw[0]))
306 ++dl->ops.raw;
5145418b 307 }
4f9d0325
ACM
308
309 disasm_line__init_ins(dl);
5145418b 310 }
78f7defe
ACM
311 }
312
29ed6e76 313 return dl;
5145418b
ACM
314
315out_free_line:
316 free(dl->line);
058b4cc9 317out_delete:
29ed6e76 318 free(dl);
058b4cc9 319 return NULL;
78f7defe
ACM
320}
321
29ed6e76 322void disasm_line__free(struct disasm_line *dl)
78f7defe 323{
29ed6e76 324 free(dl->line);
5145418b 325 free(dl->name);
44d1a3ed 326 free(dl->ops.target.name);
29ed6e76 327 free(dl);
78f7defe
ACM
328}
329
5417072b
ACM
330int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size, bool raw)
331{
332 if (raw || !dl->ins)
333 return scnprintf(bf, size, "%-6.6s %s", dl->name, dl->ops.raw);
334
335 return ins__scnprintf(dl->ins, bf, size, &dl->ops);
336}
337
29ed6e76 338static void disasm__add(struct list_head *head, struct disasm_line *line)
78f7defe
ACM
339{
340 list_add_tail(&line->node, head);
341}
342
29ed6e76 343struct disasm_line *disasm__get_next_ip_line(struct list_head *head, struct disasm_line *pos)
78f7defe
ACM
344{
345 list_for_each_entry_continue(pos, head, node)
346 if (pos->offset >= 0)
347 return pos;
348
349 return NULL;
350}
351
29ed6e76
ACM
352static int disasm_line__print(struct disasm_line *dl, struct symbol *sym, u64 start,
353 int evidx, u64 len, int min_pcnt, int printed,
354 int max_lines, struct disasm_line *queue)
78f7defe
ACM
355{
356 static const char *prev_line;
357 static const char *prev_color;
358
29ed6e76 359 if (dl->offset != -1) {
78f7defe
ACM
360 const char *path = NULL;
361 unsigned int hits = 0;
362 double percent = 0.0;
363 const char *color;
364 struct annotation *notes = symbol__annotation(sym);
ce6f4fab 365 struct source_line *src_line = notes->src->lines;
2f525d01 366 struct sym_hist *h = annotation__histogram(notes, evidx);
29ed6e76 367 s64 offset = dl->offset;
058b4cc9 368 const u64 addr = start + offset;
29ed6e76 369 struct disasm_line *next;
ce6f4fab 370
29ed6e76 371 next = disasm__get_next_ip_line(&notes->src->source, dl);
78f7defe
ACM
372
373 while (offset < (s64)len &&
374 (next == NULL || offset < next->offset)) {
375 if (src_line) {
376 if (path == NULL)
377 path = src_line[offset].path;
378 percent += src_line[offset].percent;
379 } else
380 hits += h->addr[offset];
381
382 ++offset;
383 }
384
385 if (src_line == NULL && h->sum)
386 percent = 100.0 * hits / h->sum;
387
d040bd36 388 if (percent < min_pcnt)
36532461
ACM
389 return -1;
390
e3087b80 391 if (max_lines && printed >= max_lines)
36532461 392 return 1;
d040bd36 393
d5e3d747
ACM
394 if (queue != NULL) {
395 list_for_each_entry_from(queue, &notes->src->source, node) {
29ed6e76 396 if (queue == dl)
d5e3d747 397 break;
29ed6e76 398 disasm_line__print(queue, sym, start, evidx, len,
d5e3d747
ACM
399 0, 0, 1, NULL);
400 }
401 }
402
78f7defe
ACM
403 color = get_percent_color(percent);
404
405 /*
406 * Also color the filename and line if needed, with
407 * the same color than the percentage. Don't print it
408 * twice for close colored addr with the same filename:line
409 */
410 if (path) {
411 if (!prev_line || strcmp(prev_line, path)
412 || color != prev_color) {
413 color_fprintf(stdout, color, " %s", path);
414 prev_line = path;
415 prev_color = color;
416 }
417 }
418
419 color_fprintf(stdout, color, " %7.2f", percent);
420 printf(" : ");
058b4cc9 421 color_fprintf(stdout, PERF_COLOR_MAGENTA, " %" PRIx64 ":", addr);
29ed6e76 422 color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", dl->line);
e3087b80 423 } else if (max_lines && printed >= max_lines)
36532461
ACM
424 return 1;
425 else {
d5e3d747
ACM
426 if (queue)
427 return -1;
428
29ed6e76 429 if (!*dl->line)
78f7defe
ACM
430 printf(" :\n");
431 else
29ed6e76 432 printf(" : %s\n", dl->line);
78f7defe 433 }
36532461
ACM
434
435 return 0;
78f7defe
ACM
436}
437
ce6f4fab
ACM
438static int symbol__parse_objdump_line(struct symbol *sym, struct map *map,
439 FILE *file, size_t privsize)
78f7defe 440{
ce6f4fab 441 struct annotation *notes = symbol__annotation(sym);
29ed6e76 442 struct disasm_line *dl;
058b4cc9 443 char *line = NULL, *parsed_line, *tmp, *tmp2, *c;
78f7defe
ACM
444 size_t line_len;
445 s64 line_ip, offset = -1;
446
447 if (getline(&line, &line_len, file) < 0)
448 return -1;
449
450 if (!line)
451 return -1;
452
453 while (line_len != 0 && isspace(line[line_len - 1]))
454 line[--line_len] = '\0';
455
456 c = strchr(line, '\n');
457 if (c)
458 *c = 0;
459
460 line_ip = -1;
a31b7cc0 461 parsed_line = line;
78f7defe
ACM
462
463 /*
464 * Strip leading spaces:
465 */
466 tmp = line;
467 while (*tmp) {
468 if (*tmp != ' ')
469 break;
470 tmp++;
471 }
472
473 if (*tmp) {
474 /*
475 * Parse hexa addresses followed by ':'
476 */
477 line_ip = strtoull(tmp, &tmp2, 16);
478 if (*tmp2 != ':' || tmp == tmp2 || tmp2[1] == '\0')
479 line_ip = -1;
480 }
481
482 if (line_ip != -1) {
483 u64 start = map__rip_2objdump(map, sym->start),
484 end = map__rip_2objdump(map, sym->end);
485
486 offset = line_ip - start;
487 if (offset < 0 || (u64)line_ip > end)
488 offset = -1;
058b4cc9
ACM
489 else
490 parsed_line = tmp2 + 1;
a31b7cc0 491 }
78f7defe 492
29ed6e76 493 dl = disasm_line__new(offset, parsed_line, privsize);
058b4cc9
ACM
494 free(line);
495
29ed6e76 496 if (dl == NULL)
78f7defe 497 return -1;
058b4cc9 498
29ed6e76 499 disasm__add(&notes->src->source, dl);
78f7defe
ACM
500
501 return 0;
502}
503
ce6f4fab 504int symbol__annotate(struct symbol *sym, struct map *map, size_t privsize)
78f7defe
ACM
505{
506 struct dso *dso = map->dso;
507 char *filename = dso__build_id_filename(dso, NULL, 0);
508 bool free_filename = true;
509 char command[PATH_MAX * 2];
510 FILE *file;
511 int err = 0;
78f7defe
ACM
512 char symfs_filename[PATH_MAX];
513
514 if (filename) {
515 snprintf(symfs_filename, sizeof(symfs_filename), "%s%s",
516 symbol_conf.symfs, filename);
517 }
518
519 if (filename == NULL) {
520 if (dso->has_build_id) {
521 pr_err("Can't annotate %s: not enough memory\n",
522 sym->name);
523 return -ENOMEM;
524 }
525 goto fallback;
526 } else if (readlink(symfs_filename, command, sizeof(command)) < 0 ||
527 strstr(command, "[kernel.kallsyms]") ||
528 access(symfs_filename, R_OK)) {
529 free(filename);
530fallback:
531 /*
532 * If we don't have build-ids or the build-id file isn't in the
533 * cache, or is just a kallsyms file, well, lets hope that this
534 * DSO is the same as when 'perf record' ran.
535 */
536 filename = dso->long_name;
537 snprintf(symfs_filename, sizeof(symfs_filename), "%s%s",
538 symbol_conf.symfs, filename);
539 free_filename = false;
540 }
541
878b439d 542 if (dso->symtab_type == SYMTAB__KALLSYMS) {
170ae6bc
ACM
543 char bf[BUILD_ID_SIZE * 2 + 16] = " with build id ";
544 char *build_id_msg = NULL;
545
78f7defe
ACM
546 if (dso->annotate_warned)
547 goto out_free_filename;
170ae6bc
ACM
548
549 if (dso->has_build_id) {
550 build_id__sprintf(dso->build_id,
551 sizeof(dso->build_id), bf + 15);
552 build_id_msg = bf;
553 }
78f7defe
ACM
554 err = -ENOENT;
555 dso->annotate_warned = 1;
ae55795e
ACM
556 pr_err("Can't annotate %s:\n\n"
557 "No vmlinux file%s\nwas found in the path.\n\n"
558 "Please use:\n\n"
559 " perf buildid-cache -av vmlinux\n\n"
560 "or:\n\n"
ff2a6617 561 " --vmlinux vmlinux\n",
170ae6bc 562 sym->name, build_id_msg ?: "");
78f7defe
ACM
563 goto out_free_filename;
564 }
565
566 pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__,
567 filename, sym->name, map->unmap_ip(map, sym->start),
568 map->unmap_ip(map, sym->end));
569
78f7defe
ACM
570 pr_debug("annotating [%p] %30s : [%p] %30s\n",
571 dso, dso->long_name, sym, sym->name);
572
573 snprintf(command, sizeof(command),
f69b64f7 574 "objdump %s%s --start-address=0x%016" PRIx64
3e6a2a7f
SE
575 " --stop-address=0x%016" PRIx64
576 " -d %s %s -C %s|grep -v %s|expand",
f69b64f7
AK
577 disassembler_style ? "-M " : "",
578 disassembler_style ? disassembler_style : "",
78f7defe 579 map__rip_2objdump(map, sym->start),
f41612f4 580 map__rip_2objdump(map, sym->end+1),
3e6a2a7f
SE
581 symbol_conf.annotate_asm_raw ? "" : "--no-show-raw",
582 symbol_conf.annotate_src ? "-S" : "",
78f7defe
ACM
583 symfs_filename, filename);
584
585 pr_debug("Executing: %s\n", command);
586
587 file = popen(command, "r");
588 if (!file)
589 goto out_free_filename;
590
591 while (!feof(file))
ce6f4fab 592 if (symbol__parse_objdump_line(sym, map, file, privsize) < 0)
78f7defe
ACM
593 break;
594
595 pclose(file);
596out_free_filename:
597 if (free_filename)
598 free(filename);
599 return err;
600}
601
602static void insert_source_line(struct rb_root *root, struct source_line *src_line)
603{
604 struct source_line *iter;
605 struct rb_node **p = &root->rb_node;
606 struct rb_node *parent = NULL;
607
608 while (*p != NULL) {
609 parent = *p;
610 iter = rb_entry(parent, struct source_line, node);
611
612 if (src_line->percent > iter->percent)
613 p = &(*p)->rb_left;
614 else
615 p = &(*p)->rb_right;
616 }
617
618 rb_link_node(&src_line->node, parent, p);
619 rb_insert_color(&src_line->node, root);
620}
621
622static void symbol__free_source_line(struct symbol *sym, int len)
623{
624 struct annotation *notes = symbol__annotation(sym);
ce6f4fab 625 struct source_line *src_line = notes->src->lines;
78f7defe
ACM
626 int i;
627
628 for (i = 0; i < len; i++)
629 free(src_line[i].path);
630
631 free(src_line);
ce6f4fab 632 notes->src->lines = NULL;
78f7defe
ACM
633}
634
635/* Get the filename:line for the colored entries */
636static int symbol__get_source_line(struct symbol *sym, struct map *map,
2f525d01 637 int evidx, struct rb_root *root, int len,
78f7defe
ACM
638 const char *filename)
639{
640 u64 start;
641 int i;
642 char cmd[PATH_MAX * 2];
643 struct source_line *src_line;
644 struct annotation *notes = symbol__annotation(sym);
2f525d01 645 struct sym_hist *h = annotation__histogram(notes, evidx);
78f7defe
ACM
646
647 if (!h->sum)
648 return 0;
649
ce6f4fab
ACM
650 src_line = notes->src->lines = calloc(len, sizeof(struct source_line));
651 if (!notes->src->lines)
78f7defe
ACM
652 return -1;
653
f40a0633 654 start = map__rip_2objdump(map, sym->start);
78f7defe
ACM
655
656 for (i = 0; i < len; i++) {
657 char *path = NULL;
658 size_t line_len;
659 u64 offset;
660 FILE *fp;
661
662 src_line[i].percent = 100.0 * h->addr[i] / h->sum;
663 if (src_line[i].percent <= 0.5)
664 continue;
665
666 offset = start + i;
667 sprintf(cmd, "addr2line -e %s %016" PRIx64, filename, offset);
668 fp = popen(cmd, "r");
669 if (!fp)
670 continue;
671
672 if (getline(&path, &line_len, fp) < 0 || !line_len)
673 goto next;
674
675 src_line[i].path = malloc(sizeof(char) * line_len + 1);
676 if (!src_line[i].path)
677 goto next;
678
679 strcpy(src_line[i].path, path);
680 insert_source_line(root, &src_line[i]);
681
682 next:
683 pclose(fp);
684 }
685
686 return 0;
687}
688
689static void print_summary(struct rb_root *root, const char *filename)
690{
691 struct source_line *src_line;
692 struct rb_node *node;
693
694 printf("\nSorted summary for file %s\n", filename);
695 printf("----------------------------------------------\n\n");
696
697 if (RB_EMPTY_ROOT(root)) {
698 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
699 return;
700 }
701
702 node = rb_first(root);
703 while (node) {
704 double percent;
705 const char *color;
706 char *path;
707
708 src_line = rb_entry(node, struct source_line, node);
709 percent = src_line->percent;
710 color = get_percent_color(percent);
711 path = src_line->path;
712
713 color_fprintf(stdout, color, " %7.2f %s", percent, path);
714 node = rb_next(node);
715 }
716}
717
2f525d01 718static void symbol__annotate_hits(struct symbol *sym, int evidx)
78f7defe
ACM
719{
720 struct annotation *notes = symbol__annotation(sym);
2f525d01 721 struct sym_hist *h = annotation__histogram(notes, evidx);
1b2e2df4 722 u64 len = symbol__size(sym), offset;
78f7defe
ACM
723
724 for (offset = 0; offset < len; ++offset)
725 if (h->addr[offset] != 0)
726 printf("%*" PRIx64 ": %" PRIu64 "\n", BITS_PER_LONG / 2,
727 sym->start + offset, h->addr[offset]);
728 printf("%*s: %" PRIu64 "\n", BITS_PER_LONG / 2, "h->sum", h->sum);
729}
730
ce6f4fab 731int symbol__annotate_printf(struct symbol *sym, struct map *map, int evidx,
d5e3d747
ACM
732 bool full_paths, int min_pcnt, int max_lines,
733 int context)
78f7defe
ACM
734{
735 struct dso *dso = map->dso;
736 const char *filename = dso->long_name, *d_filename;
ce6f4fab 737 struct annotation *notes = symbol__annotation(sym);
29ed6e76 738 struct disasm_line *pos, *queue = NULL;
058b4cc9 739 u64 start = map__rip_2objdump(map, sym->start);
d5e3d747 740 int printed = 2, queue_len = 0;
36532461 741 int more = 0;
78f7defe
ACM
742 u64 len;
743
78f7defe
ACM
744 if (full_paths)
745 d_filename = filename;
746 else
747 d_filename = basename(filename);
748
1b2e2df4 749 len = symbol__size(sym);
78f7defe 750
78f7defe
ACM
751 printf(" Percent | Source code & Disassembly of %s\n", d_filename);
752 printf("------------------------------------------------\n");
753
754 if (verbose)
2f525d01 755 symbol__annotate_hits(sym, evidx);
78f7defe 756
ce6f4fab 757 list_for_each_entry(pos, &notes->src->source, node) {
d5e3d747
ACM
758 if (context && queue == NULL) {
759 queue = pos;
760 queue_len = 0;
761 }
762
29ed6e76 763 switch (disasm_line__print(pos, sym, start, evidx, len,
058b4cc9
ACM
764 min_pcnt, printed, max_lines,
765 queue)) {
36532461
ACM
766 case 0:
767 ++printed;
d5e3d747
ACM
768 if (context) {
769 printed += queue_len;
770 queue = NULL;
771 queue_len = 0;
772 }
36532461
ACM
773 break;
774 case 1:
775 /* filtered by max_lines */
776 ++more;
d040bd36 777 break;
36532461
ACM
778 case -1:
779 default:
d5e3d747
ACM
780 /*
781 * Filtered by min_pcnt or non IP lines when
782 * context != 0
783 */
784 if (!context)
785 break;
786 if (queue_len == context)
787 queue = list_entry(queue->node.next, typeof(*queue), node);
788 else
789 ++queue_len;
36532461
ACM
790 break;
791 }
792 }
793
794 return more;
795}
f1e2701d 796
36532461
ACM
797void symbol__annotate_zero_histogram(struct symbol *sym, int evidx)
798{
799 struct annotation *notes = symbol__annotation(sym);
800 struct sym_hist *h = annotation__histogram(notes, evidx);
801
ce6f4fab 802 memset(h, 0, notes->src->sizeof_sym_hist);
36532461
ACM
803}
804
ce6f4fab 805void symbol__annotate_decay_histogram(struct symbol *sym, int evidx)
36532461
ACM
806{
807 struct annotation *notes = symbol__annotation(sym);
808 struct sym_hist *h = annotation__histogram(notes, evidx);
1b2e2df4 809 int len = symbol__size(sym), offset;
36532461
ACM
810
811 h->sum = 0;
8b84a568
ACM
812 for (offset = 0; offset < len; ++offset) {
813 h->addr[offset] = h->addr[offset] * 7 / 8;
814 h->sum += h->addr[offset];
f1e2701d
ACM
815 }
816}
817
29ed6e76 818void disasm__purge(struct list_head *head)
f1e2701d 819{
29ed6e76 820 struct disasm_line *pos, *n;
f1e2701d
ACM
821
822 list_for_each_entry_safe(pos, n, head, node) {
823 list_del(&pos->node);
29ed6e76 824 disasm_line__free(pos);
f1e2701d
ACM
825 }
826}
827
5145418b
ACM
828static size_t disasm_line__fprintf(struct disasm_line *dl, FILE *fp)
829{
830 size_t printed;
831
832 if (dl->offset == -1)
833 return fprintf(fp, "%s\n", dl->line);
834
835 printed = fprintf(fp, "%#" PRIx64 " %s", dl->offset, dl->name);
836
c7e6ead7 837 if (dl->ops.raw[0] != '\0') {
5145418b 838 printed += fprintf(fp, "%.*s %s\n", 6 - (int)printed, " ",
c7e6ead7 839 dl->ops.raw);
5145418b
ACM
840 }
841
842 return printed + fprintf(fp, "\n");
843}
844
845size_t disasm__fprintf(struct list_head *head, FILE *fp)
846{
847 struct disasm_line *pos;
848 size_t printed = 0;
849
850 list_for_each_entry(pos, head, node)
851 printed += disasm_line__fprintf(pos, fp);
852
853 return printed;
854}
855
f1e2701d
ACM
856int symbol__tty_annotate(struct symbol *sym, struct map *map, int evidx,
857 bool print_lines, bool full_paths, int min_pcnt,
858 int max_lines)
859{
860 struct dso *dso = map->dso;
861 const char *filename = dso->long_name;
862 struct rb_root source_line = RB_ROOT;
f1e2701d
ACM
863 u64 len;
864
ce6f4fab 865 if (symbol__annotate(sym, map, 0) < 0)
f1e2701d
ACM
866 return -1;
867
1b2e2df4 868 len = symbol__size(sym);
f1e2701d
ACM
869
870 if (print_lines) {
871 symbol__get_source_line(sym, map, evidx, &source_line,
872 len, filename);
873 print_summary(&source_line, filename);
78f7defe
ACM
874 }
875
ce6f4fab 876 symbol__annotate_printf(sym, map, evidx, full_paths,
d5e3d747 877 min_pcnt, max_lines, 0);
78f7defe
ACM
878 if (print_lines)
879 symbol__free_source_line(sym, len);
880
29ed6e76 881 disasm__purge(&symbol__annotation(sym)->src->source);
f1e2701d 882
78f7defe
ACM
883 return 0;
884}