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)
18 int symbol__alloc_hist(struct symbol *sym, int nevents)
20 struct annotation *notes = symbol__annotation(sym);
22 notes->sizeof_sym_hist = (sizeof(*notes->histograms) +
23 (sym->end - sym->start) * sizeof(u64));
24 notes->histograms = calloc(nevents, notes->sizeof_sym_hist);
25 notes->nr_histograms = nevents;
26 return notes->histograms == NULL ? -1 : 0;
29 void symbol__annotate_zero_histograms(struct symbol *sym)
31 struct annotation *notes = symbol__annotation(sym);
33 if (notes->histograms != NULL)
34 memset(notes->histograms, 0,
35 notes->nr_histograms * notes->sizeof_sym_hist);
38 int symbol__inc_addr_samples(struct symbol *sym, struct map *map,
42 struct annotation *notes;
45 notes = symbol__annotation(sym);
46 if (notes->histograms == NULL)
49 pr_debug3("%s: addr=%#" PRIx64 "\n", __func__, map->unmap_ip(map, addr));
54 offset = addr - sym->start;
55 h = annotation__histogram(notes, evidx);
59 pr_debug3("%#" PRIx64 " %s: period++ [addr: %#" PRIx64 ", %#" PRIx64
60 ", evidx=%d] => %" PRIu64 "\n", sym->start, sym->name,
61 addr, addr - sym->start, evidx, h->addr[offset]);
65 static struct objdump_line *objdump_line__new(s64 offset, char *line, size_t privsize)
67 struct objdump_line *self = malloc(sizeof(*self) + privsize);
70 self->offset = offset;
77 void objdump_line__free(struct objdump_line *self)
83 static void objdump__add_line(struct list_head *head, struct objdump_line *line)
85 list_add_tail(&line->node, head);
88 struct objdump_line *objdump__get_next_ip_line(struct list_head *head,
89 struct objdump_line *pos)
91 list_for_each_entry_continue(pos, head, node)
98 static int objdump_line__print(struct objdump_line *oline,
99 struct list_head *head, struct symbol *sym,
100 int evidx, u64 len, int min_pcnt,
101 int printed, int max_lines)
103 static const char *prev_line;
104 static const char *prev_color;
106 if (oline->offset != -1) {
107 const char *path = NULL;
108 unsigned int hits = 0;
109 double percent = 0.0;
111 struct annotation *notes = symbol__annotation(sym);
112 struct source_line *src_line = notes->src_line;
113 struct sym_hist *h = annotation__histogram(notes, evidx);
114 s64 offset = oline->offset;
115 struct objdump_line *next = objdump__get_next_ip_line(head, oline);
117 while (offset < (s64)len &&
118 (next == NULL || offset < next->offset)) {
121 path = src_line[offset].path;
122 percent += src_line[offset].percent;
124 hits += h->addr[offset];
129 if (src_line == NULL && h->sum)
130 percent = 100.0 * hits / h->sum;
132 if (percent < min_pcnt)
135 if (printed >= max_lines)
138 color = get_percent_color(percent);
141 * Also color the filename and line if needed, with
142 * the same color than the percentage. Don't print it
143 * twice for close colored addr with the same filename:line
146 if (!prev_line || strcmp(prev_line, path)
147 || color != prev_color) {
148 color_fprintf(stdout, color, " %s", path);
154 color_fprintf(stdout, color, " %7.2f", percent);
156 color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", oline->line);
157 } else if (printed >= max_lines)
163 printf(" : %s\n", oline->line);
169 static int symbol__parse_objdump_line(struct symbol *sym, struct map *map, FILE *file,
170 struct list_head *head, size_t privsize)
172 struct objdump_line *objdump_line;
173 char *line = NULL, *tmp, *tmp2, *c;
175 s64 line_ip, offset = -1;
177 if (getline(&line, &line_len, file) < 0)
183 while (line_len != 0 && isspace(line[line_len - 1]))
184 line[--line_len] = '\0';
186 c = strchr(line, '\n');
193 * Strip leading spaces:
204 * Parse hexa addresses followed by ':'
206 line_ip = strtoull(tmp, &tmp2, 16);
207 if (*tmp2 != ':' || tmp == tmp2 || tmp2[1] == '\0')
212 u64 start = map__rip_2objdump(map, sym->start),
213 end = map__rip_2objdump(map, sym->end);
215 offset = line_ip - start;
216 if (offset < 0 || (u64)line_ip > end)
220 objdump_line = objdump_line__new(offset, line, privsize);
221 if (objdump_line == NULL) {
225 objdump__add_line(head, objdump_line);
230 int symbol__annotate(struct symbol *sym, struct map *map,
231 struct list_head *head, size_t privsize)
233 struct dso *dso = map->dso;
234 char *filename = dso__build_id_filename(dso, NULL, 0);
235 bool free_filename = true;
236 char command[PATH_MAX * 2];
240 char symfs_filename[PATH_MAX];
243 snprintf(symfs_filename, sizeof(symfs_filename), "%s%s",
244 symbol_conf.symfs, filename);
247 if (filename == NULL) {
248 if (dso->has_build_id) {
249 pr_err("Can't annotate %s: not enough memory\n",
254 } else if (readlink(symfs_filename, command, sizeof(command)) < 0 ||
255 strstr(command, "[kernel.kallsyms]") ||
256 access(symfs_filename, R_OK)) {
260 * If we don't have build-ids or the build-id file isn't in the
261 * cache, or is just a kallsyms file, well, lets hope that this
262 * DSO is the same as when 'perf record' ran.
264 filename = dso->long_name;
265 snprintf(symfs_filename, sizeof(symfs_filename), "%s%s",
266 symbol_conf.symfs, filename);
267 free_filename = false;
270 if (dso->origin == DSO__ORIG_KERNEL) {
271 if (dso->annotate_warned)
272 goto out_free_filename;
274 dso->annotate_warned = 1;
275 pr_err("Can't annotate %s: No vmlinux file was found in the "
276 "path\n", sym->name);
277 goto out_free_filename;
280 pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__,
281 filename, sym->name, map->unmap_ip(map, sym->start),
282 map->unmap_ip(map, sym->end));
284 len = sym->end - sym->start;
286 pr_debug("annotating [%p] %30s : [%p] %30s\n",
287 dso, dso->long_name, sym, sym->name);
289 snprintf(command, sizeof(command),
290 "objdump --start-address=0x%016" PRIx64
291 " --stop-address=0x%016" PRIx64 " -dS -C %s|grep -v %s|expand",
292 map__rip_2objdump(map, sym->start),
293 map__rip_2objdump(map, sym->end),
294 symfs_filename, filename);
296 pr_debug("Executing: %s\n", command);
298 file = popen(command, "r");
300 goto out_free_filename;
303 if (symbol__parse_objdump_line(sym, map, file, head, privsize) < 0)
313 static void insert_source_line(struct rb_root *root, struct source_line *src_line)
315 struct source_line *iter;
316 struct rb_node **p = &root->rb_node;
317 struct rb_node *parent = NULL;
321 iter = rb_entry(parent, struct source_line, node);
323 if (src_line->percent > iter->percent)
329 rb_link_node(&src_line->node, parent, p);
330 rb_insert_color(&src_line->node, root);
333 static void symbol__free_source_line(struct symbol *sym, int len)
335 struct annotation *notes = symbol__annotation(sym);
336 struct source_line *src_line = notes->src_line;
339 for (i = 0; i < len; i++)
340 free(src_line[i].path);
343 notes->src_line = NULL;
346 /* Get the filename:line for the colored entries */
347 static int symbol__get_source_line(struct symbol *sym, struct map *map,
348 int evidx, struct rb_root *root, int len,
349 const char *filename)
353 char cmd[PATH_MAX * 2];
354 struct source_line *src_line;
355 struct annotation *notes = symbol__annotation(sym);
356 struct sym_hist *h = annotation__histogram(notes, evidx);
361 src_line = notes->src_line = calloc(len, sizeof(struct source_line));
362 if (!notes->src_line)
365 start = map->unmap_ip(map, sym->start);
367 for (i = 0; i < len; i++) {
373 src_line[i].percent = 100.0 * h->addr[i] / h->sum;
374 if (src_line[i].percent <= 0.5)
378 sprintf(cmd, "addr2line -e %s %016" PRIx64, filename, offset);
379 fp = popen(cmd, "r");
383 if (getline(&path, &line_len, fp) < 0 || !line_len)
386 src_line[i].path = malloc(sizeof(char) * line_len + 1);
387 if (!src_line[i].path)
390 strcpy(src_line[i].path, path);
391 insert_source_line(root, &src_line[i]);
400 static void print_summary(struct rb_root *root, const char *filename)
402 struct source_line *src_line;
403 struct rb_node *node;
405 printf("\nSorted summary for file %s\n", filename);
406 printf("----------------------------------------------\n\n");
408 if (RB_EMPTY_ROOT(root)) {
409 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
413 node = rb_first(root);
419 src_line = rb_entry(node, struct source_line, node);
420 percent = src_line->percent;
421 color = get_percent_color(percent);
422 path = src_line->path;
424 color_fprintf(stdout, color, " %7.2f %s", percent, path);
425 node = rb_next(node);
429 static void symbol__annotate_hits(struct symbol *sym, int evidx)
431 struct annotation *notes = symbol__annotation(sym);
432 struct sym_hist *h = annotation__histogram(notes, evidx);
433 u64 len = sym->end - sym->start, offset;
435 for (offset = 0; offset < len; ++offset)
436 if (h->addr[offset] != 0)
437 printf("%*" PRIx64 ": %" PRIu64 "\n", BITS_PER_LONG / 2,
438 sym->start + offset, h->addr[offset]);
439 printf("%*s: %" PRIu64 "\n", BITS_PER_LONG / 2, "h->sum", h->sum);
442 int symbol__annotate_printf(struct symbol *sym, struct map *map,
443 struct list_head *head, int evidx, bool full_paths,
444 int min_pcnt, int max_lines)
446 struct dso *dso = map->dso;
447 const char *filename = dso->long_name, *d_filename;
448 struct objdump_line *pos;
454 d_filename = filename;
456 d_filename = basename(filename);
458 len = sym->end - sym->start;
460 printf(" Percent | Source code & Disassembly of %s\n", d_filename);
461 printf("------------------------------------------------\n");
464 symbol__annotate_hits(sym, evidx);
466 list_for_each_entry(pos, head, node) {
467 switch (objdump_line__print(pos, head, sym, evidx, len, min_pcnt,
468 printed, max_lines)) {
473 /* filtered by max_lines */
478 /* filtered by min_pcnt */
486 void symbol__annotate_zero_histogram(struct symbol *sym, int evidx)
488 struct annotation *notes = symbol__annotation(sym);
489 struct sym_hist *h = annotation__histogram(notes, evidx);
491 memset(h, 0, notes->sizeof_sym_hist);
494 void symbol__annotate_decay_histogram(struct symbol *sym,
495 struct list_head *head, int evidx)
497 struct annotation *notes = symbol__annotation(sym);
498 struct sym_hist *h = annotation__histogram(notes, evidx);
499 struct objdump_line *pos;
503 list_for_each_entry(pos, head, node) {
504 if (pos->offset != -1) {
505 h->addr[pos->offset] = h->addr[pos->offset] * 7 / 8;
506 h->sum += h->addr[pos->offset];
511 void objdump_line_list__purge(struct list_head *head)
513 struct objdump_line *pos, *n;
515 list_for_each_entry_safe(pos, n, head, node) {
516 list_del(&pos->node);
517 objdump_line__free(pos);
521 int symbol__tty_annotate(struct symbol *sym, struct map *map, int evidx,
522 bool print_lines, bool full_paths, int min_pcnt,
525 struct dso *dso = map->dso;
526 const char *filename = dso->long_name;
527 struct rb_root source_line = RB_ROOT;
531 if (symbol__annotate(sym, map, &head, 0) < 0)
534 len = sym->end - sym->start;
537 symbol__get_source_line(sym, map, evidx, &source_line,
539 print_summary(&source_line, filename);
542 symbol__annotate_printf(sym, map, &head, evidx, full_paths,
543 min_pcnt, max_lines);
545 symbol__free_source_line(sym, len);
547 objdump_line_list__purge(&head);