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