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 int symbol__annotate_init(struct map *map __used, struct symbol *sym)
21 struct annotation *notes = symbol__annotation(sym);
22 pthread_mutex_init(¬es->lock, NULL);
26 int symbol__alloc_hist(struct symbol *sym, int nevents)
28 struct annotation *notes = symbol__annotation(sym);
29 size_t sizeof_sym_hist = (sizeof(struct sym_hist) +
30 (sym->end - sym->start) * sizeof(u64));
32 notes->src = zalloc(sizeof(*notes->src) + nevents * sizeof_sym_hist);
33 if (notes->src == NULL)
35 notes->src->sizeof_sym_hist = sizeof_sym_hist;
36 notes->src->nr_histograms = nevents;
37 INIT_LIST_HEAD(¬es->src->source);
41 void symbol__annotate_zero_histograms(struct symbol *sym)
43 struct annotation *notes = symbol__annotation(sym);
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);
52 int symbol__inc_addr_samples(struct symbol *sym, struct map *map,
56 struct annotation *notes;
59 notes = symbol__annotation(sym);
60 if (notes->src == NULL)
63 pr_debug3("%s: addr=%#" PRIx64 "\n", __func__, map->unmap_ip(map, addr));
68 offset = addr - sym->start;
69 h = annotation__histogram(notes, evidx);
73 pr_debug3("%#" PRIx64 " %s: period++ [addr: %#" PRIx64 ", %#" PRIx64
74 ", evidx=%d] => %" PRIu64 "\n", sym->start, sym->name,
75 addr, addr - sym->start, evidx, h->addr[offset]);
79 static struct objdump_line *objdump_line__new(s64 offset, char *line, size_t privsize)
81 struct objdump_line *self = malloc(sizeof(*self) + privsize);
84 self->offset = offset;
91 void objdump_line__free(struct objdump_line *self)
97 static void objdump__add_line(struct list_head *head, struct objdump_line *line)
99 list_add_tail(&line->node, head);
102 struct objdump_line *objdump__get_next_ip_line(struct list_head *head,
103 struct objdump_line *pos)
105 list_for_each_entry_continue(pos, head, node)
106 if (pos->offset >= 0)
112 static int objdump_line__print(struct objdump_line *oline, struct symbol *sym,
113 int evidx, u64 len, int min_pcnt,
114 int printed, int max_lines,
115 struct objdump_line *queue)
117 static const char *prev_line;
118 static const char *prev_color;
120 if (oline->offset != -1) {
121 const char *path = NULL;
122 unsigned int hits = 0;
123 double percent = 0.0;
125 struct annotation *notes = symbol__annotation(sym);
126 struct source_line *src_line = notes->src->lines;
127 struct sym_hist *h = annotation__histogram(notes, evidx);
128 s64 offset = oline->offset;
129 struct objdump_line *next;
131 next = objdump__get_next_ip_line(¬es->src->source, oline);
133 while (offset < (s64)len &&
134 (next == NULL || offset < next->offset)) {
137 path = src_line[offset].path;
138 percent += src_line[offset].percent;
140 hits += h->addr[offset];
145 if (src_line == NULL && h->sum)
146 percent = 100.0 * hits / h->sum;
148 if (percent < min_pcnt)
151 if (max_lines && printed >= max_lines)
155 list_for_each_entry_from(queue, ¬es->src->source, node) {
158 objdump_line__print(queue, sym, evidx, len,
163 color = get_percent_color(percent);
166 * Also color the filename and line if needed, with
167 * the same color than the percentage. Don't print it
168 * twice for close colored addr with the same filename:line
171 if (!prev_line || strcmp(prev_line, path)
172 || color != prev_color) {
173 color_fprintf(stdout, color, " %s", path);
179 color_fprintf(stdout, color, " %7.2f", percent);
181 color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", oline->line);
182 } else if (max_lines && printed >= max_lines)
191 printf(" : %s\n", oline->line);
197 static int symbol__parse_objdump_line(struct symbol *sym, struct map *map,
198 FILE *file, size_t privsize)
200 struct annotation *notes = symbol__annotation(sym);
201 struct objdump_line *objdump_line;
202 char *line = NULL, *tmp, *tmp2, *c;
204 s64 line_ip, offset = -1;
206 if (getline(&line, &line_len, file) < 0)
212 while (line_len != 0 && isspace(line[line_len - 1]))
213 line[--line_len] = '\0';
215 c = strchr(line, '\n');
222 * Strip leading spaces:
233 * Parse hexa addresses followed by ':'
235 line_ip = strtoull(tmp, &tmp2, 16);
236 if (*tmp2 != ':' || tmp == tmp2 || tmp2[1] == '\0')
241 u64 start = map__rip_2objdump(map, sym->start),
242 end = map__rip_2objdump(map, sym->end);
244 offset = line_ip - start;
245 if (offset < 0 || (u64)line_ip > end)
249 objdump_line = objdump_line__new(offset, line, privsize);
250 if (objdump_line == NULL) {
254 objdump__add_line(¬es->src->source, objdump_line);
259 int symbol__annotate(struct symbol *sym, struct map *map, size_t privsize)
261 struct dso *dso = map->dso;
262 char *filename = dso__build_id_filename(dso, NULL, 0);
263 bool free_filename = true;
264 char command[PATH_MAX * 2];
267 char symfs_filename[PATH_MAX];
270 snprintf(symfs_filename, sizeof(symfs_filename), "%s%s",
271 symbol_conf.symfs, filename);
274 if (filename == NULL) {
275 if (dso->has_build_id) {
276 pr_err("Can't annotate %s: not enough memory\n",
281 } else if (readlink(symfs_filename, command, sizeof(command)) < 0 ||
282 strstr(command, "[kernel.kallsyms]") ||
283 access(symfs_filename, R_OK)) {
287 * If we don't have build-ids or the build-id file isn't in the
288 * cache, or is just a kallsyms file, well, lets hope that this
289 * DSO is the same as when 'perf record' ran.
291 filename = dso->long_name;
292 snprintf(symfs_filename, sizeof(symfs_filename), "%s%s",
293 symbol_conf.symfs, filename);
294 free_filename = false;
297 if (dso->symtab_type == SYMTAB__KALLSYMS) {
298 char bf[BUILD_ID_SIZE * 2 + 16] = " with build id ";
299 char *build_id_msg = NULL;
301 if (dso->annotate_warned)
302 goto out_free_filename;
304 if (dso->has_build_id) {
305 build_id__sprintf(dso->build_id,
306 sizeof(dso->build_id), bf + 15);
310 dso->annotate_warned = 1;
311 pr_err("Can't annotate %s: No vmlinux file%s was found in the "
312 "path.\nPlease use 'perf buildid-cache -av vmlinux' or "
313 "--vmlinux vmlinux.\n",
314 sym->name, build_id_msg ?: "");
315 goto out_free_filename;
318 pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__,
319 filename, sym->name, map->unmap_ip(map, sym->start),
320 map->unmap_ip(map, sym->end));
322 pr_debug("annotating [%p] %30s : [%p] %30s\n",
323 dso, dso->long_name, sym, sym->name);
325 snprintf(command, sizeof(command),
326 "objdump --start-address=0x%016" PRIx64
327 " --stop-address=0x%016" PRIx64
328 " -d %s %s -C %s|grep -v %s|expand",
329 map__rip_2objdump(map, sym->start),
330 map__rip_2objdump(map, sym->end),
331 symbol_conf.annotate_asm_raw ? "" : "--no-show-raw",
332 symbol_conf.annotate_src ? "-S" : "",
333 symfs_filename, filename);
335 pr_debug("Executing: %s\n", command);
337 file = popen(command, "r");
339 goto out_free_filename;
342 if (symbol__parse_objdump_line(sym, map, file, privsize) < 0)
352 static void insert_source_line(struct rb_root *root, struct source_line *src_line)
354 struct source_line *iter;
355 struct rb_node **p = &root->rb_node;
356 struct rb_node *parent = NULL;
360 iter = rb_entry(parent, struct source_line, node);
362 if (src_line->percent > iter->percent)
368 rb_link_node(&src_line->node, parent, p);
369 rb_insert_color(&src_line->node, root);
372 static void symbol__free_source_line(struct symbol *sym, int len)
374 struct annotation *notes = symbol__annotation(sym);
375 struct source_line *src_line = notes->src->lines;
378 for (i = 0; i < len; i++)
379 free(src_line[i].path);
382 notes->src->lines = NULL;
385 /* Get the filename:line for the colored entries */
386 static int symbol__get_source_line(struct symbol *sym, struct map *map,
387 int evidx, struct rb_root *root, int len,
388 const char *filename)
392 char cmd[PATH_MAX * 2];
393 struct source_line *src_line;
394 struct annotation *notes = symbol__annotation(sym);
395 struct sym_hist *h = annotation__histogram(notes, evidx);
400 src_line = notes->src->lines = calloc(len, sizeof(struct source_line));
401 if (!notes->src->lines)
404 start = map->unmap_ip(map, sym->start);
406 for (i = 0; i < len; i++) {
412 src_line[i].percent = 100.0 * h->addr[i] / h->sum;
413 if (src_line[i].percent <= 0.5)
417 sprintf(cmd, "addr2line -e %s %016" PRIx64, filename, offset);
418 fp = popen(cmd, "r");
422 if (getline(&path, &line_len, fp) < 0 || !line_len)
425 src_line[i].path = malloc(sizeof(char) * line_len + 1);
426 if (!src_line[i].path)
429 strcpy(src_line[i].path, path);
430 insert_source_line(root, &src_line[i]);
439 static void print_summary(struct rb_root *root, const char *filename)
441 struct source_line *src_line;
442 struct rb_node *node;
444 printf("\nSorted summary for file %s\n", filename);
445 printf("----------------------------------------------\n\n");
447 if (RB_EMPTY_ROOT(root)) {
448 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
452 node = rb_first(root);
458 src_line = rb_entry(node, struct source_line, node);
459 percent = src_line->percent;
460 color = get_percent_color(percent);
461 path = src_line->path;
463 color_fprintf(stdout, color, " %7.2f %s", percent, path);
464 node = rb_next(node);
468 static void symbol__annotate_hits(struct symbol *sym, int evidx)
470 struct annotation *notes = symbol__annotation(sym);
471 struct sym_hist *h = annotation__histogram(notes, evidx);
472 u64 len = sym->end - sym->start, offset;
474 for (offset = 0; offset < len; ++offset)
475 if (h->addr[offset] != 0)
476 printf("%*" PRIx64 ": %" PRIu64 "\n", BITS_PER_LONG / 2,
477 sym->start + offset, h->addr[offset]);
478 printf("%*s: %" PRIu64 "\n", BITS_PER_LONG / 2, "h->sum", h->sum);
481 int symbol__annotate_printf(struct symbol *sym, struct map *map, int evidx,
482 bool full_paths, int min_pcnt, int max_lines,
485 struct dso *dso = map->dso;
486 const char *filename = dso->long_name, *d_filename;
487 struct annotation *notes = symbol__annotation(sym);
488 struct objdump_line *pos, *queue = NULL;
489 int printed = 2, queue_len = 0;
494 d_filename = filename;
496 d_filename = basename(filename);
498 len = sym->end - sym->start;
500 printf(" Percent | Source code & Disassembly of %s\n", d_filename);
501 printf("------------------------------------------------\n");
504 symbol__annotate_hits(sym, evidx);
506 list_for_each_entry(pos, ¬es->src->source, node) {
507 if (context && queue == NULL) {
512 switch (objdump_line__print(pos, sym, evidx, len, min_pcnt,
513 printed, max_lines, queue)) {
517 printed += queue_len;
523 /* filtered by max_lines */
529 * Filtered by min_pcnt or non IP lines when
534 if (queue_len == context)
535 queue = list_entry(queue->node.next, typeof(*queue), node);
545 void symbol__annotate_zero_histogram(struct symbol *sym, int evidx)
547 struct annotation *notes = symbol__annotation(sym);
548 struct sym_hist *h = annotation__histogram(notes, evidx);
550 memset(h, 0, notes->src->sizeof_sym_hist);
553 void symbol__annotate_decay_histogram(struct symbol *sym, int evidx)
555 struct annotation *notes = symbol__annotation(sym);
556 struct sym_hist *h = annotation__histogram(notes, evidx);
557 struct objdump_line *pos;
558 int len = sym->end - sym->start;
562 list_for_each_entry(pos, ¬es->src->source, node) {
563 if (pos->offset != -1 && pos->offset < len) {
564 h->addr[pos->offset] = h->addr[pos->offset] * 7 / 8;
565 h->sum += h->addr[pos->offset];
570 void objdump_line_list__purge(struct list_head *head)
572 struct objdump_line *pos, *n;
574 list_for_each_entry_safe(pos, n, head, node) {
575 list_del(&pos->node);
576 objdump_line__free(pos);
580 int symbol__tty_annotate(struct symbol *sym, struct map *map, int evidx,
581 bool print_lines, bool full_paths, int min_pcnt,
584 struct dso *dso = map->dso;
585 const char *filename = dso->long_name;
586 struct rb_root source_line = RB_ROOT;
589 if (symbol__annotate(sym, map, 0) < 0)
592 len = sym->end - sym->start;
595 symbol__get_source_line(sym, map, evidx, &source_line,
597 print_summary(&source_line, filename);
600 symbol__annotate_printf(sym, map, evidx, full_paths,
601 min_pcnt, max_lines, 0);
603 symbol__free_source_line(sym, len);
605 objdump_line_list__purge(&symbol__annotation(sym)->src->source);