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