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 | |
ce6f4fab | 19 | int symbol__annotate_init(struct map *map __used, struct symbol *sym) |
78f7defe ACM |
20 | { |
21 | struct annotation *notes = symbol__annotation(sym); | |
ce6f4fab ACM |
22 | pthread_mutex_init(¬es->lock, NULL); |
23 | return 0; | |
24 | } | |
78f7defe | 25 | |
ce6f4fab ACM |
26 | int symbol__alloc_hist(struct symbol *sym, int nevents) |
27 | { | |
28 | struct annotation *notes = symbol__annotation(sym); | |
29 | size_t sizeof_sym_hist = (sizeof(struct sym_hist) + | |
2f525d01 | 30 | (sym->end - sym->start) * sizeof(u64)); |
ce6f4fab ACM |
31 | |
32 | notes->src = zalloc(sizeof(*notes->src) + nevents * sizeof_sym_hist); | |
33 | if (notes->src == NULL) | |
34 | return -1; | |
35 | notes->src->sizeof_sym_hist = sizeof_sym_hist; | |
36 | notes->src->nr_histograms = nevents; | |
37 | INIT_LIST_HEAD(¬es->src->source); | |
38 | return 0; | |
78f7defe ACM |
39 | } |
40 | ||
36532461 ACM |
41 | void symbol__annotate_zero_histograms(struct symbol *sym) |
42 | { | |
43 | struct annotation *notes = symbol__annotation(sym); | |
44 | ||
ce6f4fab ACM |
45 | pthread_mutex_lock(¬es->lock); |
46 | if (notes->src != NULL) | |
47 | memset(notes->src->histograms, 0, | |
48 | notes->src->nr_histograms * notes->src->sizeof_sym_hist); | |
49 | pthread_mutex_unlock(¬es->lock); | |
36532461 ACM |
50 | } |
51 | ||
2f525d01 ACM |
52 | int symbol__inc_addr_samples(struct symbol *sym, struct map *map, |
53 | int evidx, u64 addr) | |
78f7defe | 54 | { |
2f525d01 | 55 | unsigned offset; |
78f7defe ACM |
56 | struct annotation *notes; |
57 | struct sym_hist *h; | |
58 | ||
78f7defe | 59 | notes = symbol__annotation(sym); |
ce6f4fab | 60 | if (notes->src == NULL) |
78f7defe ACM |
61 | return -ENOMEM; |
62 | ||
78f7defe ACM |
63 | pr_debug3("%s: addr=%#" PRIx64 "\n", __func__, map->unmap_ip(map, addr)); |
64 | ||
2f525d01 | 65 | if (addr >= sym->end) |
78f7defe ACM |
66 | return 0; |
67 | ||
2f525d01 ACM |
68 | offset = addr - sym->start; |
69 | h = annotation__histogram(notes, evidx); | |
78f7defe ACM |
70 | h->sum++; |
71 | h->addr[offset]++; | |
72 | ||
73 | pr_debug3("%#" PRIx64 " %s: period++ [addr: %#" PRIx64 ", %#" PRIx64 | |
2f525d01 ACM |
74 | ", evidx=%d] => %" PRIu64 "\n", sym->start, sym->name, |
75 | addr, addr - sym->start, evidx, h->addr[offset]); | |
78f7defe ACM |
76 | return 0; |
77 | } | |
78 | ||
79 | static struct objdump_line *objdump_line__new(s64 offset, char *line, size_t privsize) | |
80 | { | |
81 | struct objdump_line *self = malloc(sizeof(*self) + privsize); | |
82 | ||
83 | if (self != NULL) { | |
84 | self->offset = offset; | |
85 | self->line = line; | |
86 | } | |
87 | ||
88 | return self; | |
89 | } | |
90 | ||
91 | void objdump_line__free(struct objdump_line *self) | |
92 | { | |
93 | free(self->line); | |
94 | free(self); | |
95 | } | |
96 | ||
97 | static void objdump__add_line(struct list_head *head, struct objdump_line *line) | |
98 | { | |
99 | list_add_tail(&line->node, head); | |
100 | } | |
101 | ||
102 | struct objdump_line *objdump__get_next_ip_line(struct list_head *head, | |
103 | struct objdump_line *pos) | |
104 | { | |
105 | list_for_each_entry_continue(pos, head, node) | |
106 | if (pos->offset >= 0) | |
107 | return pos; | |
108 | ||
109 | return NULL; | |
110 | } | |
111 | ||
ce6f4fab | 112 | static int objdump_line__print(struct objdump_line *oline, struct symbol *sym, |
36532461 ACM |
113 | int evidx, u64 len, int min_pcnt, |
114 | int printed, int max_lines) | |
78f7defe ACM |
115 | { |
116 | static const char *prev_line; | |
117 | static const char *prev_color; | |
118 | ||
119 | if (oline->offset != -1) { | |
120 | const char *path = NULL; | |
121 | unsigned int hits = 0; | |
122 | double percent = 0.0; | |
123 | const char *color; | |
124 | struct annotation *notes = symbol__annotation(sym); | |
ce6f4fab | 125 | struct source_line *src_line = notes->src->lines; |
2f525d01 | 126 | struct sym_hist *h = annotation__histogram(notes, evidx); |
78f7defe | 127 | s64 offset = oline->offset; |
ce6f4fab ACM |
128 | struct objdump_line *next; |
129 | ||
130 | next = objdump__get_next_ip_line(¬es->src->source, oline); | |
78f7defe ACM |
131 | |
132 | while (offset < (s64)len && | |
133 | (next == NULL || offset < next->offset)) { | |
134 | if (src_line) { | |
135 | if (path == NULL) | |
136 | path = src_line[offset].path; | |
137 | percent += src_line[offset].percent; | |
138 | } else | |
139 | hits += h->addr[offset]; | |
140 | ||
141 | ++offset; | |
142 | } | |
143 | ||
144 | if (src_line == NULL && h->sum) | |
145 | percent = 100.0 * hits / h->sum; | |
146 | ||
d040bd36 | 147 | if (percent < min_pcnt) |
36532461 ACM |
148 | return -1; |
149 | ||
e3087b80 | 150 | if (max_lines && printed >= max_lines) |
36532461 | 151 | return 1; |
d040bd36 | 152 | |
78f7defe ACM |
153 | color = get_percent_color(percent); |
154 | ||
155 | /* | |
156 | * Also color the filename and line if needed, with | |
157 | * the same color than the percentage. Don't print it | |
158 | * twice for close colored addr with the same filename:line | |
159 | */ | |
160 | if (path) { | |
161 | if (!prev_line || strcmp(prev_line, path) | |
162 | || color != prev_color) { | |
163 | color_fprintf(stdout, color, " %s", path); | |
164 | prev_line = path; | |
165 | prev_color = color; | |
166 | } | |
167 | } | |
168 | ||
169 | color_fprintf(stdout, color, " %7.2f", percent); | |
170 | printf(" : "); | |
171 | color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", oline->line); | |
e3087b80 | 172 | } else if (max_lines && printed >= max_lines) |
36532461 ACM |
173 | return 1; |
174 | else { | |
78f7defe ACM |
175 | if (!*oline->line) |
176 | printf(" :\n"); | |
177 | else | |
178 | printf(" : %s\n", oline->line); | |
179 | } | |
36532461 ACM |
180 | |
181 | return 0; | |
78f7defe ACM |
182 | } |
183 | ||
ce6f4fab ACM |
184 | static int symbol__parse_objdump_line(struct symbol *sym, struct map *map, |
185 | FILE *file, size_t privsize) | |
78f7defe | 186 | { |
ce6f4fab | 187 | struct annotation *notes = symbol__annotation(sym); |
78f7defe ACM |
188 | struct objdump_line *objdump_line; |
189 | char *line = NULL, *tmp, *tmp2, *c; | |
190 | size_t line_len; | |
191 | s64 line_ip, offset = -1; | |
192 | ||
193 | if (getline(&line, &line_len, file) < 0) | |
194 | return -1; | |
195 | ||
196 | if (!line) | |
197 | return -1; | |
198 | ||
199 | while (line_len != 0 && isspace(line[line_len - 1])) | |
200 | line[--line_len] = '\0'; | |
201 | ||
202 | c = strchr(line, '\n'); | |
203 | if (c) | |
204 | *c = 0; | |
205 | ||
206 | line_ip = -1; | |
207 | ||
208 | /* | |
209 | * Strip leading spaces: | |
210 | */ | |
211 | tmp = line; | |
212 | while (*tmp) { | |
213 | if (*tmp != ' ') | |
214 | break; | |
215 | tmp++; | |
216 | } | |
217 | ||
218 | if (*tmp) { | |
219 | /* | |
220 | * Parse hexa addresses followed by ':' | |
221 | */ | |
222 | line_ip = strtoull(tmp, &tmp2, 16); | |
223 | if (*tmp2 != ':' || tmp == tmp2 || tmp2[1] == '\0') | |
224 | line_ip = -1; | |
225 | } | |
226 | ||
227 | if (line_ip != -1) { | |
228 | u64 start = map__rip_2objdump(map, sym->start), | |
229 | end = map__rip_2objdump(map, sym->end); | |
230 | ||
231 | offset = line_ip - start; | |
232 | if (offset < 0 || (u64)line_ip > end) | |
233 | offset = -1; | |
234 | } | |
235 | ||
236 | objdump_line = objdump_line__new(offset, line, privsize); | |
237 | if (objdump_line == NULL) { | |
238 | free(line); | |
239 | return -1; | |
240 | } | |
ce6f4fab | 241 | objdump__add_line(¬es->src->source, objdump_line); |
78f7defe ACM |
242 | |
243 | return 0; | |
244 | } | |
245 | ||
ce6f4fab | 246 | int symbol__annotate(struct symbol *sym, struct map *map, size_t privsize) |
78f7defe ACM |
247 | { |
248 | struct dso *dso = map->dso; | |
249 | char *filename = dso__build_id_filename(dso, NULL, 0); | |
250 | bool free_filename = true; | |
251 | char command[PATH_MAX * 2]; | |
252 | FILE *file; | |
253 | int err = 0; | |
78f7defe ACM |
254 | char symfs_filename[PATH_MAX]; |
255 | ||
256 | if (filename) { | |
257 | snprintf(symfs_filename, sizeof(symfs_filename), "%s%s", | |
258 | symbol_conf.symfs, filename); | |
259 | } | |
260 | ||
261 | if (filename == NULL) { | |
262 | if (dso->has_build_id) { | |
263 | pr_err("Can't annotate %s: not enough memory\n", | |
264 | sym->name); | |
265 | return -ENOMEM; | |
266 | } | |
267 | goto fallback; | |
268 | } else if (readlink(symfs_filename, command, sizeof(command)) < 0 || | |
269 | strstr(command, "[kernel.kallsyms]") || | |
270 | access(symfs_filename, R_OK)) { | |
271 | free(filename); | |
272 | fallback: | |
273 | /* | |
274 | * If we don't have build-ids or the build-id file isn't in the | |
275 | * cache, or is just a kallsyms file, well, lets hope that this | |
276 | * DSO is the same as when 'perf record' ran. | |
277 | */ | |
278 | filename = dso->long_name; | |
279 | snprintf(symfs_filename, sizeof(symfs_filename), "%s%s", | |
280 | symbol_conf.symfs, filename); | |
281 | free_filename = false; | |
282 | } | |
283 | ||
284 | if (dso->origin == DSO__ORIG_KERNEL) { | |
285 | if (dso->annotate_warned) | |
286 | goto out_free_filename; | |
287 | err = -ENOENT; | |
288 | dso->annotate_warned = 1; | |
289 | pr_err("Can't annotate %s: No vmlinux file was found in the " | |
290 | "path\n", sym->name); | |
291 | goto out_free_filename; | |
292 | } | |
293 | ||
294 | pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__, | |
295 | filename, sym->name, map->unmap_ip(map, sym->start), | |
296 | map->unmap_ip(map, sym->end)); | |
297 | ||
78f7defe ACM |
298 | pr_debug("annotating [%p] %30s : [%p] %30s\n", |
299 | dso, dso->long_name, sym, sym->name); | |
300 | ||
301 | snprintf(command, sizeof(command), | |
302 | "objdump --start-address=0x%016" PRIx64 | |
303 | " --stop-address=0x%016" PRIx64 " -dS -C %s|grep -v %s|expand", | |
304 | map__rip_2objdump(map, sym->start), | |
305 | map__rip_2objdump(map, sym->end), | |
306 | symfs_filename, filename); | |
307 | ||
308 | pr_debug("Executing: %s\n", command); | |
309 | ||
310 | file = popen(command, "r"); | |
311 | if (!file) | |
312 | goto out_free_filename; | |
313 | ||
314 | while (!feof(file)) | |
ce6f4fab | 315 | if (symbol__parse_objdump_line(sym, map, file, privsize) < 0) |
78f7defe ACM |
316 | break; |
317 | ||
318 | pclose(file); | |
319 | out_free_filename: | |
320 | if (free_filename) | |
321 | free(filename); | |
322 | return err; | |
323 | } | |
324 | ||
325 | static void insert_source_line(struct rb_root *root, struct source_line *src_line) | |
326 | { | |
327 | struct source_line *iter; | |
328 | struct rb_node **p = &root->rb_node; | |
329 | struct rb_node *parent = NULL; | |
330 | ||
331 | while (*p != NULL) { | |
332 | parent = *p; | |
333 | iter = rb_entry(parent, struct source_line, node); | |
334 | ||
335 | if (src_line->percent > iter->percent) | |
336 | p = &(*p)->rb_left; | |
337 | else | |
338 | p = &(*p)->rb_right; | |
339 | } | |
340 | ||
341 | rb_link_node(&src_line->node, parent, p); | |
342 | rb_insert_color(&src_line->node, root); | |
343 | } | |
344 | ||
345 | static void symbol__free_source_line(struct symbol *sym, int len) | |
346 | { | |
347 | struct annotation *notes = symbol__annotation(sym); | |
ce6f4fab | 348 | struct source_line *src_line = notes->src->lines; |
78f7defe ACM |
349 | int i; |
350 | ||
351 | for (i = 0; i < len; i++) | |
352 | free(src_line[i].path); | |
353 | ||
354 | free(src_line); | |
ce6f4fab | 355 | notes->src->lines = NULL; |
78f7defe ACM |
356 | } |
357 | ||
358 | /* Get the filename:line for the colored entries */ | |
359 | static int symbol__get_source_line(struct symbol *sym, struct map *map, | |
2f525d01 | 360 | int evidx, struct rb_root *root, int len, |
78f7defe ACM |
361 | const char *filename) |
362 | { | |
363 | u64 start; | |
364 | int i; | |
365 | char cmd[PATH_MAX * 2]; | |
366 | struct source_line *src_line; | |
367 | struct annotation *notes = symbol__annotation(sym); | |
2f525d01 | 368 | struct sym_hist *h = annotation__histogram(notes, evidx); |
78f7defe ACM |
369 | |
370 | if (!h->sum) | |
371 | return 0; | |
372 | ||
ce6f4fab ACM |
373 | src_line = notes->src->lines = calloc(len, sizeof(struct source_line)); |
374 | if (!notes->src->lines) | |
78f7defe ACM |
375 | return -1; |
376 | ||
377 | start = map->unmap_ip(map, sym->start); | |
378 | ||
379 | for (i = 0; i < len; i++) { | |
380 | char *path = NULL; | |
381 | size_t line_len; | |
382 | u64 offset; | |
383 | FILE *fp; | |
384 | ||
385 | src_line[i].percent = 100.0 * h->addr[i] / h->sum; | |
386 | if (src_line[i].percent <= 0.5) | |
387 | continue; | |
388 | ||
389 | offset = start + i; | |
390 | sprintf(cmd, "addr2line -e %s %016" PRIx64, filename, offset); | |
391 | fp = popen(cmd, "r"); | |
392 | if (!fp) | |
393 | continue; | |
394 | ||
395 | if (getline(&path, &line_len, fp) < 0 || !line_len) | |
396 | goto next; | |
397 | ||
398 | src_line[i].path = malloc(sizeof(char) * line_len + 1); | |
399 | if (!src_line[i].path) | |
400 | goto next; | |
401 | ||
402 | strcpy(src_line[i].path, path); | |
403 | insert_source_line(root, &src_line[i]); | |
404 | ||
405 | next: | |
406 | pclose(fp); | |
407 | } | |
408 | ||
409 | return 0; | |
410 | } | |
411 | ||
412 | static void print_summary(struct rb_root *root, const char *filename) | |
413 | { | |
414 | struct source_line *src_line; | |
415 | struct rb_node *node; | |
416 | ||
417 | printf("\nSorted summary for file %s\n", filename); | |
418 | printf("----------------------------------------------\n\n"); | |
419 | ||
420 | if (RB_EMPTY_ROOT(root)) { | |
421 | printf(" Nothing higher than %1.1f%%\n", MIN_GREEN); | |
422 | return; | |
423 | } | |
424 | ||
425 | node = rb_first(root); | |
426 | while (node) { | |
427 | double percent; | |
428 | const char *color; | |
429 | char *path; | |
430 | ||
431 | src_line = rb_entry(node, struct source_line, node); | |
432 | percent = src_line->percent; | |
433 | color = get_percent_color(percent); | |
434 | path = src_line->path; | |
435 | ||
436 | color_fprintf(stdout, color, " %7.2f %s", percent, path); | |
437 | node = rb_next(node); | |
438 | } | |
439 | } | |
440 | ||
2f525d01 | 441 | static void symbol__annotate_hits(struct symbol *sym, int evidx) |
78f7defe ACM |
442 | { |
443 | struct annotation *notes = symbol__annotation(sym); | |
2f525d01 | 444 | struct sym_hist *h = annotation__histogram(notes, evidx); |
78f7defe ACM |
445 | u64 len = sym->end - sym->start, offset; |
446 | ||
447 | for (offset = 0; offset < len; ++offset) | |
448 | if (h->addr[offset] != 0) | |
449 | printf("%*" PRIx64 ": %" PRIu64 "\n", BITS_PER_LONG / 2, | |
450 | sym->start + offset, h->addr[offset]); | |
451 | printf("%*s: %" PRIu64 "\n", BITS_PER_LONG / 2, "h->sum", h->sum); | |
452 | } | |
453 | ||
ce6f4fab ACM |
454 | int symbol__annotate_printf(struct symbol *sym, struct map *map, int evidx, |
455 | bool full_paths, int min_pcnt, int max_lines) | |
78f7defe ACM |
456 | { |
457 | struct dso *dso = map->dso; | |
458 | const char *filename = dso->long_name, *d_filename; | |
ce6f4fab | 459 | struct annotation *notes = symbol__annotation(sym); |
f1e2701d | 460 | struct objdump_line *pos; |
d040bd36 | 461 | int printed = 2; |
36532461 | 462 | int more = 0; |
78f7defe ACM |
463 | u64 len; |
464 | ||
78f7defe ACM |
465 | if (full_paths) |
466 | d_filename = filename; | |
467 | else | |
468 | d_filename = basename(filename); | |
469 | ||
470 | len = sym->end - sym->start; | |
471 | ||
78f7defe ACM |
472 | printf(" Percent | Source code & Disassembly of %s\n", d_filename); |
473 | printf("------------------------------------------------\n"); | |
474 | ||
475 | if (verbose) | |
2f525d01 | 476 | symbol__annotate_hits(sym, evidx); |
78f7defe | 477 | |
ce6f4fab ACM |
478 | list_for_each_entry(pos, ¬es->src->source, node) { |
479 | switch (objdump_line__print(pos, sym, evidx, len, min_pcnt, | |
36532461 ACM |
480 | printed, max_lines)) { |
481 | case 0: | |
482 | ++printed; | |
483 | break; | |
484 | case 1: | |
485 | /* filtered by max_lines */ | |
486 | ++more; | |
d040bd36 | 487 | break; |
36532461 ACM |
488 | case -1: |
489 | default: | |
490 | /* filtered by min_pcnt */ | |
491 | break; | |
492 | } | |
493 | } | |
494 | ||
495 | return more; | |
496 | } | |
f1e2701d | 497 | |
36532461 ACM |
498 | void symbol__annotate_zero_histogram(struct symbol *sym, int evidx) |
499 | { | |
500 | struct annotation *notes = symbol__annotation(sym); | |
501 | struct sym_hist *h = annotation__histogram(notes, evidx); | |
502 | ||
ce6f4fab | 503 | memset(h, 0, notes->src->sizeof_sym_hist); |
36532461 ACM |
504 | } |
505 | ||
ce6f4fab | 506 | void symbol__annotate_decay_histogram(struct symbol *sym, int evidx) |
36532461 ACM |
507 | { |
508 | struct annotation *notes = symbol__annotation(sym); | |
509 | struct sym_hist *h = annotation__histogram(notes, evidx); | |
510 | struct objdump_line *pos; | |
511 | ||
512 | h->sum = 0; | |
513 | ||
ce6f4fab | 514 | list_for_each_entry(pos, ¬es->src->source, node) { |
36532461 ACM |
515 | if (pos->offset != -1) { |
516 | h->addr[pos->offset] = h->addr[pos->offset] * 7 / 8; | |
517 | h->sum += h->addr[pos->offset]; | |
518 | } | |
f1e2701d ACM |
519 | } |
520 | } | |
521 | ||
522 | void objdump_line_list__purge(struct list_head *head) | |
523 | { | |
524 | struct objdump_line *pos, *n; | |
525 | ||
526 | list_for_each_entry_safe(pos, n, head, node) { | |
527 | list_del(&pos->node); | |
528 | objdump_line__free(pos); | |
529 | } | |
530 | } | |
531 | ||
532 | int symbol__tty_annotate(struct symbol *sym, struct map *map, int evidx, | |
533 | bool print_lines, bool full_paths, int min_pcnt, | |
534 | int max_lines) | |
535 | { | |
536 | struct dso *dso = map->dso; | |
537 | const char *filename = dso->long_name; | |
538 | struct rb_root source_line = RB_ROOT; | |
f1e2701d ACM |
539 | u64 len; |
540 | ||
ce6f4fab | 541 | if (symbol__annotate(sym, map, 0) < 0) |
f1e2701d ACM |
542 | return -1; |
543 | ||
544 | len = sym->end - sym->start; | |
545 | ||
546 | if (print_lines) { | |
547 | symbol__get_source_line(sym, map, evidx, &source_line, | |
548 | len, filename); | |
549 | print_summary(&source_line, filename); | |
78f7defe ACM |
550 | } |
551 | ||
ce6f4fab | 552 | symbol__annotate_printf(sym, map, evidx, full_paths, |
f1e2701d | 553 | min_pcnt, max_lines); |
78f7defe ACM |
554 | if (print_lines) |
555 | symbol__free_source_line(sym, len); | |
556 | ||
ce6f4fab | 557 | objdump_line_list__purge(&symbol__annotation(sym)->src->source); |
f1e2701d | 558 | |
78f7defe ACM |
559 | return 0; |
560 | } |