2 * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
4 * Parts came from builtin-annotate.c, see those files for further
7 * Released under the GPL v2. (and only v2, not any later version)
19 const char *disassembler_style;
21 int symbol__annotate_init(struct map *map __used, struct symbol *sym)
23 struct annotation *notes = symbol__annotation(sym);
24 pthread_mutex_init(¬es->lock, NULL);
28 int symbol__alloc_hist(struct symbol *sym)
30 struct annotation *notes = symbol__annotation(sym);
31 const size_t size = sym->end - sym->start + 1;
32 size_t sizeof_sym_hist = (sizeof(struct sym_hist) + size * sizeof(u64));
34 notes->src = zalloc(sizeof(*notes->src) + symbol_conf.nr_events * sizeof_sym_hist);
35 if (notes->src == NULL)
37 notes->src->sizeof_sym_hist = sizeof_sym_hist;
38 notes->src->nr_histograms = symbol_conf.nr_events;
39 INIT_LIST_HEAD(¬es->src->source);
43 void symbol__annotate_zero_histograms(struct symbol *sym)
45 struct annotation *notes = symbol__annotation(sym);
47 pthread_mutex_lock(¬es->lock);
48 if (notes->src != NULL)
49 memset(notes->src->histograms, 0,
50 notes->src->nr_histograms * notes->src->sizeof_sym_hist);
51 pthread_mutex_unlock(¬es->lock);
54 int symbol__inc_addr_samples(struct symbol *sym, struct map *map,
58 struct annotation *notes;
61 notes = symbol__annotation(sym);
62 if (notes->src == NULL)
65 pr_debug3("%s: addr=%#" PRIx64 "\n", __func__, map->unmap_ip(map, addr));
67 if (addr < sym->start || addr > sym->end)
70 offset = addr - sym->start;
71 h = annotation__histogram(notes, evidx);
75 pr_debug3("%#" PRIx64 " %s: period++ [addr: %#" PRIx64 ", %#" PRIx64
76 ", evidx=%d] => %" PRIu64 "\n", sym->start, sym->name,
77 addr, addr - sym->start, evidx, h->addr[offset]);
81 static struct objdump_line *objdump_line__new(s64 offset, char *line, size_t privsize)
83 struct objdump_line *self = malloc(sizeof(*self) + privsize);
86 self->offset = offset;
87 self->line = strdup(line);
88 if (self->line == NULL)
98 void objdump_line__free(struct objdump_line *self)
104 static void objdump__add_line(struct list_head *head, struct objdump_line *line)
106 list_add_tail(&line->node, head);
109 struct objdump_line *objdump__get_next_ip_line(struct list_head *head,
110 struct objdump_line *pos)
112 list_for_each_entry_continue(pos, head, node)
113 if (pos->offset >= 0)
119 static int objdump_line__print(struct objdump_line *oline, struct symbol *sym,
120 u64 start, int evidx, u64 len, int min_pcnt,
121 int printed, int max_lines,
122 struct objdump_line *queue)
124 static const char *prev_line;
125 static const char *prev_color;
127 if (oline->offset != -1) {
128 const char *path = NULL;
129 unsigned int hits = 0;
130 double percent = 0.0;
132 struct annotation *notes = symbol__annotation(sym);
133 struct source_line *src_line = notes->src->lines;
134 struct sym_hist *h = annotation__histogram(notes, evidx);
135 s64 offset = oline->offset;
136 const u64 addr = start + offset;
137 struct objdump_line *next;
139 next = objdump__get_next_ip_line(¬es->src->source, oline);
141 while (offset < (s64)len &&
142 (next == NULL || offset < next->offset)) {
145 path = src_line[offset].path;
146 percent += src_line[offset].percent;
148 hits += h->addr[offset];
153 if (src_line == NULL && h->sum)
154 percent = 100.0 * hits / h->sum;
156 if (percent < min_pcnt)
159 if (max_lines && printed >= max_lines)
163 list_for_each_entry_from(queue, ¬es->src->source, node) {
166 objdump_line__print(queue, sym, start, evidx, len,
171 color = get_percent_color(percent);
174 * Also color the filename and line if needed, with
175 * the same color than the percentage. Don't print it
176 * twice for close colored addr with the same filename:line
179 if (!prev_line || strcmp(prev_line, path)
180 || color != prev_color) {
181 color_fprintf(stdout, color, " %s", path);
187 color_fprintf(stdout, color, " %7.2f", percent);
189 color_fprintf(stdout, PERF_COLOR_MAGENTA, " %" PRIx64 ":", addr);
190 color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", oline->line);
191 } else if (max_lines && printed >= max_lines)
200 printf(" : %s\n", oline->line);
206 static int symbol__parse_objdump_line(struct symbol *sym, struct map *map,
207 FILE *file, size_t privsize)
209 struct annotation *notes = symbol__annotation(sym);
210 struct objdump_line *objdump_line;
211 char *line = NULL, *parsed_line, *tmp, *tmp2, *c;
213 s64 line_ip, offset = -1;
215 if (getline(&line, &line_len, file) < 0)
221 while (line_len != 0 && isspace(line[line_len - 1]))
222 line[--line_len] = '\0';
224 c = strchr(line, '\n');
232 * Strip leading spaces:
243 * Parse hexa addresses followed by ':'
245 line_ip = strtoull(tmp, &tmp2, 16);
246 if (*tmp2 != ':' || tmp == tmp2 || tmp2[1] == '\0')
251 u64 start = map__rip_2objdump(map, sym->start),
252 end = map__rip_2objdump(map, sym->end);
254 offset = line_ip - start;
255 if (offset < 0 || (u64)line_ip > end)
258 parsed_line = tmp2 + 1;
261 objdump_line = objdump_line__new(offset, parsed_line, privsize);
264 if (objdump_line == NULL)
267 objdump__add_line(¬es->src->source, objdump_line);
272 int symbol__annotate(struct symbol *sym, struct map *map, size_t privsize)
274 struct dso *dso = map->dso;
275 char *filename = dso__build_id_filename(dso, NULL, 0);
276 bool free_filename = true;
277 char command[PATH_MAX * 2];
280 char symfs_filename[PATH_MAX];
283 snprintf(symfs_filename, sizeof(symfs_filename), "%s%s",
284 symbol_conf.symfs, filename);
287 if (filename == NULL) {
288 if (dso->has_build_id) {
289 pr_err("Can't annotate %s: not enough memory\n",
294 } else if (readlink(symfs_filename, command, sizeof(command)) < 0 ||
295 strstr(command, "[kernel.kallsyms]") ||
296 access(symfs_filename, R_OK)) {
300 * If we don't have build-ids or the build-id file isn't in the
301 * cache, or is just a kallsyms file, well, lets hope that this
302 * DSO is the same as when 'perf record' ran.
304 filename = dso->long_name;
305 snprintf(symfs_filename, sizeof(symfs_filename), "%s%s",
306 symbol_conf.symfs, filename);
307 free_filename = false;
310 if (dso->symtab_type == SYMTAB__KALLSYMS) {
311 char bf[BUILD_ID_SIZE * 2 + 16] = " with build id ";
312 char *build_id_msg = NULL;
314 if (dso->annotate_warned)
315 goto out_free_filename;
317 if (dso->has_build_id) {
318 build_id__sprintf(dso->build_id,
319 sizeof(dso->build_id), bf + 15);
323 dso->annotate_warned = 1;
324 pr_err("Can't annotate %s:\n\n"
325 "No vmlinux file%s\nwas found in the path.\n\n"
327 " perf buildid-cache -av vmlinux\n\n"
329 " --vmlinux vmlinux\n",
330 sym->name, build_id_msg ?: "");
331 goto out_free_filename;
334 pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__,
335 filename, sym->name, map->unmap_ip(map, sym->start),
336 map->unmap_ip(map, sym->end));
338 pr_debug("annotating [%p] %30s : [%p] %30s\n",
339 dso, dso->long_name, sym, sym->name);
341 snprintf(command, sizeof(command),
342 "objdump %s%s --start-address=0x%016" PRIx64
343 " --stop-address=0x%016" PRIx64
344 " -d %s %s -C %s|grep -v %s|expand",
345 disassembler_style ? "-M " : "",
346 disassembler_style ? disassembler_style : "",
347 map__rip_2objdump(map, sym->start),
348 map__rip_2objdump(map, sym->end+1),
349 symbol_conf.annotate_asm_raw ? "" : "--no-show-raw",
350 symbol_conf.annotate_src ? "-S" : "",
351 symfs_filename, filename);
353 pr_debug("Executing: %s\n", command);
355 file = popen(command, "r");
357 goto out_free_filename;
360 if (symbol__parse_objdump_line(sym, map, file, privsize) < 0)
370 static void insert_source_line(struct rb_root *root, struct source_line *src_line)
372 struct source_line *iter;
373 struct rb_node **p = &root->rb_node;
374 struct rb_node *parent = NULL;
378 iter = rb_entry(parent, struct source_line, node);
380 if (src_line->percent > iter->percent)
386 rb_link_node(&src_line->node, parent, p);
387 rb_insert_color(&src_line->node, root);
390 static void symbol__free_source_line(struct symbol *sym, int len)
392 struct annotation *notes = symbol__annotation(sym);
393 struct source_line *src_line = notes->src->lines;
396 for (i = 0; i < len; i++)
397 free(src_line[i].path);
400 notes->src->lines = NULL;
403 /* Get the filename:line for the colored entries */
404 static int symbol__get_source_line(struct symbol *sym, struct map *map,
405 int evidx, struct rb_root *root, int len,
406 const char *filename)
410 char cmd[PATH_MAX * 2];
411 struct source_line *src_line;
412 struct annotation *notes = symbol__annotation(sym);
413 struct sym_hist *h = annotation__histogram(notes, evidx);
418 src_line = notes->src->lines = calloc(len, sizeof(struct source_line));
419 if (!notes->src->lines)
422 start = map__rip_2objdump(map, sym->start);
424 for (i = 0; i < len; i++) {
430 src_line[i].percent = 100.0 * h->addr[i] / h->sum;
431 if (src_line[i].percent <= 0.5)
435 sprintf(cmd, "addr2line -e %s %016" PRIx64, filename, offset);
436 fp = popen(cmd, "r");
440 if (getline(&path, &line_len, fp) < 0 || !line_len)
443 src_line[i].path = malloc(sizeof(char) * line_len + 1);
444 if (!src_line[i].path)
447 strcpy(src_line[i].path, path);
448 insert_source_line(root, &src_line[i]);
457 static void print_summary(struct rb_root *root, const char *filename)
459 struct source_line *src_line;
460 struct rb_node *node;
462 printf("\nSorted summary for file %s\n", filename);
463 printf("----------------------------------------------\n\n");
465 if (RB_EMPTY_ROOT(root)) {
466 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
470 node = rb_first(root);
476 src_line = rb_entry(node, struct source_line, node);
477 percent = src_line->percent;
478 color = get_percent_color(percent);
479 path = src_line->path;
481 color_fprintf(stdout, color, " %7.2f %s", percent, path);
482 node = rb_next(node);
486 static void symbol__annotate_hits(struct symbol *sym, int evidx)
488 struct annotation *notes = symbol__annotation(sym);
489 struct sym_hist *h = annotation__histogram(notes, evidx);
490 u64 len = sym->end - sym->start, offset;
492 for (offset = 0; offset < len; ++offset)
493 if (h->addr[offset] != 0)
494 printf("%*" PRIx64 ": %" PRIu64 "\n", BITS_PER_LONG / 2,
495 sym->start + offset, h->addr[offset]);
496 printf("%*s: %" PRIu64 "\n", BITS_PER_LONG / 2, "h->sum", h->sum);
499 int symbol__annotate_printf(struct symbol *sym, struct map *map, int evidx,
500 bool full_paths, int min_pcnt, int max_lines,
503 struct dso *dso = map->dso;
504 const char *filename = dso->long_name, *d_filename;
505 struct annotation *notes = symbol__annotation(sym);
506 struct objdump_line *pos, *queue = NULL;
507 u64 start = map__rip_2objdump(map, sym->start);
508 int printed = 2, queue_len = 0;
513 d_filename = filename;
515 d_filename = basename(filename);
517 len = sym->end - sym->start;
519 printf(" Percent | Source code & Disassembly of %s\n", d_filename);
520 printf("------------------------------------------------\n");
523 symbol__annotate_hits(sym, evidx);
525 list_for_each_entry(pos, ¬es->src->source, node) {
526 if (context && queue == NULL) {
531 switch (objdump_line__print(pos, sym, start, evidx, len,
532 min_pcnt, printed, max_lines,
537 printed += queue_len;
543 /* filtered by max_lines */
549 * Filtered by min_pcnt or non IP lines when
554 if (queue_len == context)
555 queue = list_entry(queue->node.next, typeof(*queue), node);
565 void symbol__annotate_zero_histogram(struct symbol *sym, int evidx)
567 struct annotation *notes = symbol__annotation(sym);
568 struct sym_hist *h = annotation__histogram(notes, evidx);
570 memset(h, 0, notes->src->sizeof_sym_hist);
573 void symbol__annotate_decay_histogram(struct symbol *sym, int evidx)
575 struct annotation *notes = symbol__annotation(sym);
576 struct sym_hist *h = annotation__histogram(notes, evidx);
577 int len = sym->end - sym->start, offset;
580 for (offset = 0; offset < len; ++offset) {
581 h->addr[offset] = h->addr[offset] * 7 / 8;
582 h->sum += h->addr[offset];
586 void objdump_line_list__purge(struct list_head *head)
588 struct objdump_line *pos, *n;
590 list_for_each_entry_safe(pos, n, head, node) {
591 list_del(&pos->node);
592 objdump_line__free(pos);
596 int symbol__tty_annotate(struct symbol *sym, struct map *map, int evidx,
597 bool print_lines, bool full_paths, int min_pcnt,
600 struct dso *dso = map->dso;
601 const char *filename = dso->long_name;
602 struct rb_root source_line = RB_ROOT;
605 if (symbol__annotate(sym, map, 0) < 0)
608 len = sym->end - sym->start;
611 symbol__get_source_line(sym, map, evidx, &source_line,
613 print_summary(&source_line, filename);
616 symbol__annotate_printf(sym, map, evidx, full_paths,
617 min_pcnt, max_lines, 0);
619 symbol__free_source_line(sym, len);
621 objdump_line_list__purge(&symbol__annotation(sym)->src->source);