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