Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 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" |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 17 | #include <pthread.h> |
| 18 | |
Andi Kleen | f69b64f | 2011-09-15 14:31:41 -0700 | [diff] [blame] | 19 | const char *disassembler_style; |
| 20 | |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 21 | int symbol__annotate_init(struct map *map __used, struct symbol *sym) |
| 22 | { |
| 23 | struct annotation *notes = symbol__annotation(sym); |
| 24 | pthread_mutex_init(¬es->lock, NULL); |
| 25 | return 0; |
| 26 | } |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 27 | |
Arnaldo Carvalho de Melo | d04b35f | 2011-11-11 22:17:32 -0200 | [diff] [blame] | 28 | int symbol__alloc_hist(struct symbol *sym) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 29 | { |
| 30 | struct annotation *notes = symbol__annotation(sym); |
Arnaldo Carvalho de Melo | 64c17be | 2012-03-28 12:49:35 -0300 | [diff] [blame] | 31 | const size_t size = sym->end - sym->start + 1; |
| 32 | size_t sizeof_sym_hist = (sizeof(struct sym_hist) + size * sizeof(u64)); |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 33 | |
Arnaldo Carvalho de Melo | d04b35f | 2011-11-11 22:17:32 -0200 | [diff] [blame] | 34 | notes->src = zalloc(sizeof(*notes->src) + symbol_conf.nr_events * sizeof_sym_hist); |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 35 | if (notes->src == NULL) |
| 36 | return -1; |
| 37 | notes->src->sizeof_sym_hist = sizeof_sym_hist; |
Arnaldo Carvalho de Melo | d04b35f | 2011-11-11 22:17:32 -0200 | [diff] [blame] | 38 | notes->src->nr_histograms = symbol_conf.nr_events; |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 39 | INIT_LIST_HEAD(¬es->src->source); |
| 40 | return 0; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 41 | } |
| 42 | |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 43 | void symbol__annotate_zero_histograms(struct symbol *sym) |
| 44 | { |
| 45 | struct annotation *notes = symbol__annotation(sym); |
| 46 | |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 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); |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 52 | } |
| 53 | |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame] | 54 | int symbol__inc_addr_samples(struct symbol *sym, struct map *map, |
| 55 | int evidx, u64 addr) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 56 | { |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame] | 57 | unsigned offset; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 58 | struct annotation *notes; |
| 59 | struct sym_hist *h; |
| 60 | |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 61 | notes = symbol__annotation(sym); |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 62 | if (notes->src == NULL) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 63 | return -ENOMEM; |
| 64 | |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 65 | pr_debug3("%s: addr=%#" PRIx64 "\n", __func__, map->unmap_ip(map, addr)); |
| 66 | |
Arnaldo Carvalho de Melo | 31d68e7 | 2012-03-27 12:55:57 -0300 | [diff] [blame] | 67 | if (addr < sym->start || addr > sym->end) |
| 68 | return -ERANGE; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 69 | |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame] | 70 | offset = addr - sym->start; |
| 71 | h = annotation__histogram(notes, evidx); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 72 | h->sum++; |
| 73 | h->addr[offset]++; |
| 74 | |
| 75 | pr_debug3("%#" PRIx64 " %s: period++ [addr: %#" PRIx64 ", %#" PRIx64 |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame] | 76 | ", evidx=%d] => %" PRIu64 "\n", sym->start, sym->name, |
| 77 | addr, addr - sym->start, evidx, h->addr[offset]); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | static struct objdump_line *objdump_line__new(s64 offset, char *line, size_t privsize) |
| 82 | { |
| 83 | struct objdump_line *self = malloc(sizeof(*self) + privsize); |
| 84 | |
| 85 | if (self != NULL) { |
| 86 | self->offset = offset; |
Arnaldo Carvalho de Melo | 058b4cc | 2012-04-02 12:59:01 -0300 | [diff] [blame] | 87 | self->line = strdup(line); |
| 88 | if (self->line == NULL) |
| 89 | goto out_delete; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | return self; |
Arnaldo Carvalho de Melo | 058b4cc | 2012-04-02 12:59:01 -0300 | [diff] [blame] | 93 | out_delete: |
| 94 | free(self); |
| 95 | return NULL; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | void objdump_line__free(struct objdump_line *self) |
| 99 | { |
| 100 | free(self->line); |
| 101 | free(self); |
| 102 | } |
| 103 | |
| 104 | static void objdump__add_line(struct list_head *head, struct objdump_line *line) |
| 105 | { |
| 106 | list_add_tail(&line->node, head); |
| 107 | } |
| 108 | |
| 109 | struct objdump_line *objdump__get_next_ip_line(struct list_head *head, |
| 110 | struct objdump_line *pos) |
| 111 | { |
| 112 | list_for_each_entry_continue(pos, head, node) |
| 113 | if (pos->offset >= 0) |
| 114 | return pos; |
| 115 | |
| 116 | return NULL; |
| 117 | } |
| 118 | |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 119 | static int objdump_line__print(struct objdump_line *oline, struct symbol *sym, |
Arnaldo Carvalho de Melo | 058b4cc | 2012-04-02 12:59:01 -0300 | [diff] [blame] | 120 | u64 start, int evidx, u64 len, int min_pcnt, |
Arnaldo Carvalho de Melo | d5e3d74 | 2011-02-08 15:29:25 -0200 | [diff] [blame] | 121 | int printed, int max_lines, |
| 122 | struct objdump_line *queue) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 123 | { |
| 124 | static const char *prev_line; |
| 125 | static const char *prev_color; |
| 126 | |
| 127 | if (oline->offset != -1) { |
| 128 | const char *path = NULL; |
| 129 | unsigned int hits = 0; |
| 130 | double percent = 0.0; |
| 131 | const char *color; |
| 132 | struct annotation *notes = symbol__annotation(sym); |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 133 | struct source_line *src_line = notes->src->lines; |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame] | 134 | struct sym_hist *h = annotation__histogram(notes, evidx); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 135 | s64 offset = oline->offset; |
Arnaldo Carvalho de Melo | 058b4cc | 2012-04-02 12:59:01 -0300 | [diff] [blame] | 136 | const u64 addr = start + offset; |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 137 | struct objdump_line *next; |
| 138 | |
| 139 | next = objdump__get_next_ip_line(¬es->src->source, oline); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 140 | |
| 141 | while (offset < (s64)len && |
| 142 | (next == NULL || offset < next->offset)) { |
| 143 | if (src_line) { |
| 144 | if (path == NULL) |
| 145 | path = src_line[offset].path; |
| 146 | percent += src_line[offset].percent; |
| 147 | } else |
| 148 | hits += h->addr[offset]; |
| 149 | |
| 150 | ++offset; |
| 151 | } |
| 152 | |
| 153 | if (src_line == NULL && h->sum) |
| 154 | percent = 100.0 * hits / h->sum; |
| 155 | |
Arnaldo Carvalho de Melo | d040bd3 | 2011-02-05 15:37:31 -0200 | [diff] [blame] | 156 | if (percent < min_pcnt) |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 157 | return -1; |
| 158 | |
Arnaldo Carvalho de Melo | e3087b8 | 2011-02-08 15:01:39 -0200 | [diff] [blame] | 159 | if (max_lines && printed >= max_lines) |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 160 | return 1; |
Arnaldo Carvalho de Melo | d040bd3 | 2011-02-05 15:37:31 -0200 | [diff] [blame] | 161 | |
Arnaldo Carvalho de Melo | d5e3d74 | 2011-02-08 15:29:25 -0200 | [diff] [blame] | 162 | if (queue != NULL) { |
| 163 | list_for_each_entry_from(queue, ¬es->src->source, node) { |
| 164 | if (queue == oline) |
| 165 | break; |
Arnaldo Carvalho de Melo | 058b4cc | 2012-04-02 12:59:01 -0300 | [diff] [blame] | 166 | objdump_line__print(queue, sym, start, evidx, len, |
Arnaldo Carvalho de Melo | d5e3d74 | 2011-02-08 15:29:25 -0200 | [diff] [blame] | 167 | 0, 0, 1, NULL); |
| 168 | } |
| 169 | } |
| 170 | |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 171 | color = get_percent_color(percent); |
| 172 | |
| 173 | /* |
| 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 |
| 177 | */ |
| 178 | if (path) { |
| 179 | if (!prev_line || strcmp(prev_line, path) |
| 180 | || color != prev_color) { |
| 181 | color_fprintf(stdout, color, " %s", path); |
| 182 | prev_line = path; |
| 183 | prev_color = color; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | color_fprintf(stdout, color, " %7.2f", percent); |
| 188 | printf(" : "); |
Arnaldo Carvalho de Melo | 058b4cc | 2012-04-02 12:59:01 -0300 | [diff] [blame] | 189 | color_fprintf(stdout, PERF_COLOR_MAGENTA, " %" PRIx64 ":", addr); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 190 | color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", oline->line); |
Arnaldo Carvalho de Melo | e3087b8 | 2011-02-08 15:01:39 -0200 | [diff] [blame] | 191 | } else if (max_lines && printed >= max_lines) |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 192 | return 1; |
| 193 | else { |
Arnaldo Carvalho de Melo | d5e3d74 | 2011-02-08 15:29:25 -0200 | [diff] [blame] | 194 | if (queue) |
| 195 | return -1; |
| 196 | |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 197 | if (!*oline->line) |
| 198 | printf(" :\n"); |
| 199 | else |
| 200 | printf(" : %s\n", oline->line); |
| 201 | } |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 202 | |
| 203 | return 0; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 204 | } |
| 205 | |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 206 | static int symbol__parse_objdump_line(struct symbol *sym, struct map *map, |
| 207 | FILE *file, size_t privsize) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 208 | { |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 209 | struct annotation *notes = symbol__annotation(sym); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 210 | struct objdump_line *objdump_line; |
Arnaldo Carvalho de Melo | 058b4cc | 2012-04-02 12:59:01 -0300 | [diff] [blame] | 211 | char *line = NULL, *parsed_line, *tmp, *tmp2, *c; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 212 | size_t line_len; |
| 213 | s64 line_ip, offset = -1; |
| 214 | |
| 215 | if (getline(&line, &line_len, file) < 0) |
| 216 | return -1; |
| 217 | |
| 218 | if (!line) |
| 219 | return -1; |
| 220 | |
| 221 | while (line_len != 0 && isspace(line[line_len - 1])) |
| 222 | line[--line_len] = '\0'; |
| 223 | |
| 224 | c = strchr(line, '\n'); |
| 225 | if (c) |
| 226 | *c = 0; |
| 227 | |
| 228 | line_ip = -1; |
Namhyung Kim | a31b7cc | 2012-04-11 17:04:59 -0300 | [diff] [blame^] | 229 | parsed_line = line; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 230 | |
| 231 | /* |
| 232 | * Strip leading spaces: |
| 233 | */ |
| 234 | tmp = line; |
| 235 | while (*tmp) { |
| 236 | if (*tmp != ' ') |
| 237 | break; |
| 238 | tmp++; |
| 239 | } |
| 240 | |
| 241 | if (*tmp) { |
| 242 | /* |
| 243 | * Parse hexa addresses followed by ':' |
| 244 | */ |
| 245 | line_ip = strtoull(tmp, &tmp2, 16); |
| 246 | if (*tmp2 != ':' || tmp == tmp2 || tmp2[1] == '\0') |
| 247 | line_ip = -1; |
| 248 | } |
| 249 | |
| 250 | if (line_ip != -1) { |
| 251 | u64 start = map__rip_2objdump(map, sym->start), |
| 252 | end = map__rip_2objdump(map, sym->end); |
| 253 | |
| 254 | offset = line_ip - start; |
| 255 | if (offset < 0 || (u64)line_ip > end) |
| 256 | offset = -1; |
Arnaldo Carvalho de Melo | 058b4cc | 2012-04-02 12:59:01 -0300 | [diff] [blame] | 257 | else |
| 258 | parsed_line = tmp2 + 1; |
Namhyung Kim | a31b7cc | 2012-04-11 17:04:59 -0300 | [diff] [blame^] | 259 | } |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 260 | |
Arnaldo Carvalho de Melo | 058b4cc | 2012-04-02 12:59:01 -0300 | [diff] [blame] | 261 | objdump_line = objdump_line__new(offset, parsed_line, privsize); |
| 262 | free(line); |
| 263 | |
| 264 | if (objdump_line == NULL) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 265 | return -1; |
Arnaldo Carvalho de Melo | 058b4cc | 2012-04-02 12:59:01 -0300 | [diff] [blame] | 266 | |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 267 | objdump__add_line(¬es->src->source, objdump_line); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 268 | |
| 269 | return 0; |
| 270 | } |
| 271 | |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 272 | int symbol__annotate(struct symbol *sym, struct map *map, size_t privsize) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 273 | { |
| 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]; |
| 278 | FILE *file; |
| 279 | int err = 0; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 280 | char symfs_filename[PATH_MAX]; |
| 281 | |
| 282 | if (filename) { |
| 283 | snprintf(symfs_filename, sizeof(symfs_filename), "%s%s", |
| 284 | symbol_conf.symfs, filename); |
| 285 | } |
| 286 | |
| 287 | if (filename == NULL) { |
| 288 | if (dso->has_build_id) { |
| 289 | pr_err("Can't annotate %s: not enough memory\n", |
| 290 | sym->name); |
| 291 | return -ENOMEM; |
| 292 | } |
| 293 | goto fallback; |
| 294 | } else if (readlink(symfs_filename, command, sizeof(command)) < 0 || |
| 295 | strstr(command, "[kernel.kallsyms]") || |
| 296 | access(symfs_filename, R_OK)) { |
| 297 | free(filename); |
| 298 | fallback: |
| 299 | /* |
| 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. |
| 303 | */ |
| 304 | filename = dso->long_name; |
| 305 | snprintf(symfs_filename, sizeof(symfs_filename), "%s%s", |
| 306 | symbol_conf.symfs, filename); |
| 307 | free_filename = false; |
| 308 | } |
| 309 | |
Arnaldo Carvalho de Melo | 878b439 | 2011-03-11 13:13:36 -0300 | [diff] [blame] | 310 | if (dso->symtab_type == SYMTAB__KALLSYMS) { |
Arnaldo Carvalho de Melo | 170ae6b | 2011-02-23 11:08:59 -0300 | [diff] [blame] | 311 | char bf[BUILD_ID_SIZE * 2 + 16] = " with build id "; |
| 312 | char *build_id_msg = NULL; |
| 313 | |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 314 | if (dso->annotate_warned) |
| 315 | goto out_free_filename; |
Arnaldo Carvalho de Melo | 170ae6b | 2011-02-23 11:08:59 -0300 | [diff] [blame] | 316 | |
| 317 | if (dso->has_build_id) { |
| 318 | build_id__sprintf(dso->build_id, |
| 319 | sizeof(dso->build_id), bf + 15); |
| 320 | build_id_msg = bf; |
| 321 | } |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 322 | err = -ENOENT; |
| 323 | dso->annotate_warned = 1; |
Arnaldo Carvalho de Melo | ae55795 | 2011-10-26 08:00:55 -0200 | [diff] [blame] | 324 | pr_err("Can't annotate %s:\n\n" |
| 325 | "No vmlinux file%s\nwas found in the path.\n\n" |
| 326 | "Please use:\n\n" |
| 327 | " perf buildid-cache -av vmlinux\n\n" |
| 328 | "or:\n\n" |
Namhyung Kim | ff2a661 | 2012-02-23 17:46:24 +0900 | [diff] [blame] | 329 | " --vmlinux vmlinux\n", |
Arnaldo Carvalho de Melo | 170ae6b | 2011-02-23 11:08:59 -0300 | [diff] [blame] | 330 | sym->name, build_id_msg ?: ""); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 331 | goto out_free_filename; |
| 332 | } |
| 333 | |
| 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)); |
| 337 | |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 338 | pr_debug("annotating [%p] %30s : [%p] %30s\n", |
| 339 | dso, dso->long_name, sym, sym->name); |
| 340 | |
| 341 | snprintf(command, sizeof(command), |
Andi Kleen | f69b64f | 2011-09-15 14:31:41 -0700 | [diff] [blame] | 342 | "objdump %s%s --start-address=0x%016" PRIx64 |
Stephane Eranian | 3e6a2a7 | 2011-05-17 17:32:07 +0200 | [diff] [blame] | 343 | " --stop-address=0x%016" PRIx64 |
| 344 | " -d %s %s -C %s|grep -v %s|expand", |
Andi Kleen | f69b64f | 2011-09-15 14:31:41 -0700 | [diff] [blame] | 345 | disassembler_style ? "-M " : "", |
| 346 | disassembler_style ? disassembler_style : "", |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 347 | map__rip_2objdump(map, sym->start), |
Ingo Molnar | f41612f | 2011-12-23 14:08:04 +0100 | [diff] [blame] | 348 | map__rip_2objdump(map, sym->end+1), |
Stephane Eranian | 3e6a2a7 | 2011-05-17 17:32:07 +0200 | [diff] [blame] | 349 | symbol_conf.annotate_asm_raw ? "" : "--no-show-raw", |
| 350 | symbol_conf.annotate_src ? "-S" : "", |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 351 | symfs_filename, filename); |
| 352 | |
| 353 | pr_debug("Executing: %s\n", command); |
| 354 | |
| 355 | file = popen(command, "r"); |
| 356 | if (!file) |
| 357 | goto out_free_filename; |
| 358 | |
| 359 | while (!feof(file)) |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 360 | if (symbol__parse_objdump_line(sym, map, file, privsize) < 0) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 361 | break; |
| 362 | |
| 363 | pclose(file); |
| 364 | out_free_filename: |
| 365 | if (free_filename) |
| 366 | free(filename); |
| 367 | return err; |
| 368 | } |
| 369 | |
| 370 | static void insert_source_line(struct rb_root *root, struct source_line *src_line) |
| 371 | { |
| 372 | struct source_line *iter; |
| 373 | struct rb_node **p = &root->rb_node; |
| 374 | struct rb_node *parent = NULL; |
| 375 | |
| 376 | while (*p != NULL) { |
| 377 | parent = *p; |
| 378 | iter = rb_entry(parent, struct source_line, node); |
| 379 | |
| 380 | if (src_line->percent > iter->percent) |
| 381 | p = &(*p)->rb_left; |
| 382 | else |
| 383 | p = &(*p)->rb_right; |
| 384 | } |
| 385 | |
| 386 | rb_link_node(&src_line->node, parent, p); |
| 387 | rb_insert_color(&src_line->node, root); |
| 388 | } |
| 389 | |
| 390 | static void symbol__free_source_line(struct symbol *sym, int len) |
| 391 | { |
| 392 | struct annotation *notes = symbol__annotation(sym); |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 393 | struct source_line *src_line = notes->src->lines; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 394 | int i; |
| 395 | |
| 396 | for (i = 0; i < len; i++) |
| 397 | free(src_line[i].path); |
| 398 | |
| 399 | free(src_line); |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 400 | notes->src->lines = NULL; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | /* Get the filename:line for the colored entries */ |
| 404 | static int symbol__get_source_line(struct symbol *sym, struct map *map, |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame] | 405 | int evidx, struct rb_root *root, int len, |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 406 | const char *filename) |
| 407 | { |
| 408 | u64 start; |
| 409 | int i; |
| 410 | char cmd[PATH_MAX * 2]; |
| 411 | struct source_line *src_line; |
| 412 | struct annotation *notes = symbol__annotation(sym); |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame] | 413 | struct sym_hist *h = annotation__histogram(notes, evidx); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 414 | |
| 415 | if (!h->sum) |
| 416 | return 0; |
| 417 | |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 418 | src_line = notes->src->lines = calloc(len, sizeof(struct source_line)); |
| 419 | if (!notes->src->lines) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 420 | return -1; |
| 421 | |
David Miller | f40a063 | 2012-03-25 16:28:12 -0400 | [diff] [blame] | 422 | start = map__rip_2objdump(map, sym->start); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 423 | |
| 424 | for (i = 0; i < len; i++) { |
| 425 | char *path = NULL; |
| 426 | size_t line_len; |
| 427 | u64 offset; |
| 428 | FILE *fp; |
| 429 | |
| 430 | src_line[i].percent = 100.0 * h->addr[i] / h->sum; |
| 431 | if (src_line[i].percent <= 0.5) |
| 432 | continue; |
| 433 | |
| 434 | offset = start + i; |
| 435 | sprintf(cmd, "addr2line -e %s %016" PRIx64, filename, offset); |
| 436 | fp = popen(cmd, "r"); |
| 437 | if (!fp) |
| 438 | continue; |
| 439 | |
| 440 | if (getline(&path, &line_len, fp) < 0 || !line_len) |
| 441 | goto next; |
| 442 | |
| 443 | src_line[i].path = malloc(sizeof(char) * line_len + 1); |
| 444 | if (!src_line[i].path) |
| 445 | goto next; |
| 446 | |
| 447 | strcpy(src_line[i].path, path); |
| 448 | insert_source_line(root, &src_line[i]); |
| 449 | |
| 450 | next: |
| 451 | pclose(fp); |
| 452 | } |
| 453 | |
| 454 | return 0; |
| 455 | } |
| 456 | |
| 457 | static void print_summary(struct rb_root *root, const char *filename) |
| 458 | { |
| 459 | struct source_line *src_line; |
| 460 | struct rb_node *node; |
| 461 | |
| 462 | printf("\nSorted summary for file %s\n", filename); |
| 463 | printf("----------------------------------------------\n\n"); |
| 464 | |
| 465 | if (RB_EMPTY_ROOT(root)) { |
| 466 | printf(" Nothing higher than %1.1f%%\n", MIN_GREEN); |
| 467 | return; |
| 468 | } |
| 469 | |
| 470 | node = rb_first(root); |
| 471 | while (node) { |
| 472 | double percent; |
| 473 | const char *color; |
| 474 | char *path; |
| 475 | |
| 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; |
| 480 | |
| 481 | color_fprintf(stdout, color, " %7.2f %s", percent, path); |
| 482 | node = rb_next(node); |
| 483 | } |
| 484 | } |
| 485 | |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame] | 486 | static void symbol__annotate_hits(struct symbol *sym, int evidx) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 487 | { |
| 488 | struct annotation *notes = symbol__annotation(sym); |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame] | 489 | struct sym_hist *h = annotation__histogram(notes, evidx); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 490 | u64 len = sym->end - sym->start, offset; |
| 491 | |
| 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); |
| 497 | } |
| 498 | |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 499 | int symbol__annotate_printf(struct symbol *sym, struct map *map, int evidx, |
Arnaldo Carvalho de Melo | d5e3d74 | 2011-02-08 15:29:25 -0200 | [diff] [blame] | 500 | bool full_paths, int min_pcnt, int max_lines, |
| 501 | int context) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 502 | { |
| 503 | struct dso *dso = map->dso; |
| 504 | const char *filename = dso->long_name, *d_filename; |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 505 | struct annotation *notes = symbol__annotation(sym); |
Arnaldo Carvalho de Melo | d5e3d74 | 2011-02-08 15:29:25 -0200 | [diff] [blame] | 506 | struct objdump_line *pos, *queue = NULL; |
Arnaldo Carvalho de Melo | 058b4cc | 2012-04-02 12:59:01 -0300 | [diff] [blame] | 507 | u64 start = map__rip_2objdump(map, sym->start); |
Arnaldo Carvalho de Melo | d5e3d74 | 2011-02-08 15:29:25 -0200 | [diff] [blame] | 508 | int printed = 2, queue_len = 0; |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 509 | int more = 0; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 510 | u64 len; |
| 511 | |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 512 | if (full_paths) |
| 513 | d_filename = filename; |
| 514 | else |
| 515 | d_filename = basename(filename); |
| 516 | |
| 517 | len = sym->end - sym->start; |
| 518 | |
Arnaldo Carvalho de Melo | f1e2701 | 2011-02-05 18:51:38 -0200 | [diff] [blame] | 519 | printf(" Percent | Source code & Disassembly of %s\n", d_filename); |
| 520 | printf("------------------------------------------------\n"); |
| 521 | |
| 522 | if (verbose) |
| 523 | symbol__annotate_hits(sym, evidx); |
| 524 | |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 525 | list_for_each_entry(pos, ¬es->src->source, node) { |
Arnaldo Carvalho de Melo | d5e3d74 | 2011-02-08 15:29:25 -0200 | [diff] [blame] | 526 | if (context && queue == NULL) { |
| 527 | queue = pos; |
| 528 | queue_len = 0; |
| 529 | } |
| 530 | |
Arnaldo Carvalho de Melo | 058b4cc | 2012-04-02 12:59:01 -0300 | [diff] [blame] | 531 | switch (objdump_line__print(pos, sym, start, evidx, len, |
| 532 | min_pcnt, printed, max_lines, |
| 533 | queue)) { |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 534 | case 0: |
| 535 | ++printed; |
Arnaldo Carvalho de Melo | d5e3d74 | 2011-02-08 15:29:25 -0200 | [diff] [blame] | 536 | if (context) { |
| 537 | printed += queue_len; |
| 538 | queue = NULL; |
| 539 | queue_len = 0; |
| 540 | } |
Arnaldo Carvalho de Melo | f1e2701 | 2011-02-05 18:51:38 -0200 | [diff] [blame] | 541 | break; |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 542 | case 1: |
| 543 | /* filtered by max_lines */ |
| 544 | ++more; |
| 545 | break; |
| 546 | case -1: |
| 547 | default: |
Arnaldo Carvalho de Melo | d5e3d74 | 2011-02-08 15:29:25 -0200 | [diff] [blame] | 548 | /* |
| 549 | * Filtered by min_pcnt or non IP lines when |
| 550 | * context != 0 |
| 551 | */ |
| 552 | if (!context) |
| 553 | break; |
| 554 | if (queue_len == context) |
| 555 | queue = list_entry(queue->node.next, typeof(*queue), node); |
| 556 | else |
| 557 | ++queue_len; |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 558 | break; |
| 559 | } |
| 560 | } |
Arnaldo Carvalho de Melo | f1e2701 | 2011-02-05 18:51:38 -0200 | [diff] [blame] | 561 | |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 562 | return more; |
| 563 | } |
| 564 | |
| 565 | void symbol__annotate_zero_histogram(struct symbol *sym, int evidx) |
| 566 | { |
| 567 | struct annotation *notes = symbol__annotation(sym); |
| 568 | struct sym_hist *h = annotation__histogram(notes, evidx); |
| 569 | |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 570 | memset(h, 0, notes->src->sizeof_sym_hist); |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 571 | } |
| 572 | |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 573 | void symbol__annotate_decay_histogram(struct symbol *sym, int evidx) |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 574 | { |
| 575 | struct annotation *notes = symbol__annotation(sym); |
| 576 | struct sym_hist *h = annotation__histogram(notes, evidx); |
Arnaldo Carvalho de Melo | 8b84a56 | 2012-04-05 16:15:59 -0300 | [diff] [blame] | 577 | int len = sym->end - sym->start, offset; |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 578 | |
| 579 | h->sum = 0; |
Arnaldo Carvalho de Melo | 8b84a56 | 2012-04-05 16:15:59 -0300 | [diff] [blame] | 580 | for (offset = 0; offset < len; ++offset) { |
| 581 | h->addr[offset] = h->addr[offset] * 7 / 8; |
| 582 | h->sum += h->addr[offset]; |
Arnaldo Carvalho de Melo | f1e2701 | 2011-02-05 18:51:38 -0200 | [diff] [blame] | 583 | } |
| 584 | } |
| 585 | |
| 586 | void objdump_line_list__purge(struct list_head *head) |
| 587 | { |
| 588 | struct objdump_line *pos, *n; |
| 589 | |
| 590 | list_for_each_entry_safe(pos, n, head, node) { |
| 591 | list_del(&pos->node); |
| 592 | objdump_line__free(pos); |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | int symbol__tty_annotate(struct symbol *sym, struct map *map, int evidx, |
| 597 | bool print_lines, bool full_paths, int min_pcnt, |
| 598 | int max_lines) |
| 599 | { |
| 600 | struct dso *dso = map->dso; |
| 601 | const char *filename = dso->long_name; |
| 602 | struct rb_root source_line = RB_ROOT; |
Arnaldo Carvalho de Melo | f1e2701 | 2011-02-05 18:51:38 -0200 | [diff] [blame] | 603 | u64 len; |
| 604 | |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 605 | if (symbol__annotate(sym, map, 0) < 0) |
Arnaldo Carvalho de Melo | f1e2701 | 2011-02-05 18:51:38 -0200 | [diff] [blame] | 606 | return -1; |
| 607 | |
| 608 | len = sym->end - sym->start; |
| 609 | |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 610 | if (print_lines) { |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame] | 611 | symbol__get_source_line(sym, map, evidx, &source_line, |
| 612 | len, filename); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 613 | print_summary(&source_line, filename); |
| 614 | } |
| 615 | |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 616 | symbol__annotate_printf(sym, map, evidx, full_paths, |
Arnaldo Carvalho de Melo | d5e3d74 | 2011-02-08 15:29:25 -0200 | [diff] [blame] | 617 | min_pcnt, max_lines, 0); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 618 | if (print_lines) |
| 619 | symbol__free_source_line(sym, len); |
| 620 | |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 621 | objdump_line_list__purge(&symbol__annotation(sym)->src->source); |
Arnaldo Carvalho de Melo | f1e2701 | 2011-02-05 18:51:38 -0200 | [diff] [blame] | 622 | |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 623 | return 0; |
| 624 | } |