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 | |
| 19 | int symbol__annotate_init(struct map *map __used, struct symbol *sym) |
| 20 | { |
| 21 | struct annotation *notes = symbol__annotation(sym); |
| 22 | pthread_mutex_init(¬es->lock, NULL); |
| 23 | return 0; |
| 24 | } |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 25 | |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame] | 26 | int symbol__alloc_hist(struct symbol *sym, int nevents) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 27 | { |
| 28 | struct annotation *notes = symbol__annotation(sym); |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame^] | 29 | size_t sizeof_sym_hist = (sizeof(struct sym_hist) + |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame] | 30 | (sym->end - sym->start) * sizeof(u64)); |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame^] | 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; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 39 | } |
| 40 | |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 41 | void symbol__annotate_zero_histograms(struct symbol *sym) |
| 42 | { |
| 43 | struct annotation *notes = symbol__annotation(sym); |
| 44 | |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame^] | 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); |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 50 | } |
| 51 | |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame] | 52 | int symbol__inc_addr_samples(struct symbol *sym, struct map *map, |
| 53 | int evidx, u64 addr) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 54 | { |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame] | 55 | unsigned offset; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 56 | struct annotation *notes; |
| 57 | struct sym_hist *h; |
| 58 | |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 59 | notes = symbol__annotation(sym); |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame^] | 60 | if (notes->src == NULL) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 61 | return -ENOMEM; |
| 62 | |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 63 | pr_debug3("%s: addr=%#" PRIx64 "\n", __func__, map->unmap_ip(map, addr)); |
| 64 | |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame] | 65 | if (addr >= sym->end) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 66 | return 0; |
| 67 | |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame] | 68 | offset = addr - sym->start; |
| 69 | h = annotation__histogram(notes, evidx); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 70 | h->sum++; |
| 71 | h->addr[offset]++; |
| 72 | |
| 73 | pr_debug3("%#" PRIx64 " %s: period++ [addr: %#" PRIx64 ", %#" PRIx64 |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame] | 74 | ", evidx=%d] => %" PRIu64 "\n", sym->start, sym->name, |
| 75 | addr, addr - sym->start, evidx, h->addr[offset]); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 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 | |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame^] | 112 | static int objdump_line__print(struct objdump_line *oline, struct symbol *sym, |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 113 | int evidx, u64 len, int min_pcnt, |
| 114 | int printed, int max_lines) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 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); |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame^] | 125 | struct source_line *src_line = notes->src->lines; |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame] | 126 | struct sym_hist *h = annotation__histogram(notes, evidx); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 127 | s64 offset = oline->offset; |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame^] | 128 | struct objdump_line *next; |
| 129 | |
| 130 | next = objdump__get_next_ip_line(¬es->src->source, oline); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 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 | |
Arnaldo Carvalho de Melo | d040bd3 | 2011-02-05 15:37:31 -0200 | [diff] [blame] | 147 | if (percent < min_pcnt) |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 148 | return -1; |
| 149 | |
Arnaldo Carvalho de Melo | e3087b8 | 2011-02-08 15:01:39 -0200 | [diff] [blame] | 150 | if (max_lines && printed >= max_lines) |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 151 | return 1; |
Arnaldo Carvalho de Melo | d040bd3 | 2011-02-05 15:37:31 -0200 | [diff] [blame] | 152 | |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 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); |
Arnaldo Carvalho de Melo | e3087b8 | 2011-02-08 15:01:39 -0200 | [diff] [blame] | 172 | } else if (max_lines && printed >= max_lines) |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 173 | return 1; |
| 174 | else { |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 175 | if (!*oline->line) |
| 176 | printf(" :\n"); |
| 177 | else |
| 178 | printf(" : %s\n", oline->line); |
| 179 | } |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 180 | |
| 181 | return 0; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 182 | } |
| 183 | |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame^] | 184 | static int symbol__parse_objdump_line(struct symbol *sym, struct map *map, |
| 185 | FILE *file, size_t privsize) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 186 | { |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame^] | 187 | struct annotation *notes = symbol__annotation(sym); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 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 | } |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame^] | 241 | objdump__add_line(¬es->src->source, objdump_line); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 242 | |
| 243 | return 0; |
| 244 | } |
| 245 | |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame^] | 246 | 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] | 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; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 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 | |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 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)) |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame^] | 315 | if (symbol__parse_objdump_line(sym, map, file, privsize) < 0) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 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); |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame^] | 348 | struct source_line *src_line = notes->src->lines; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 349 | int i; |
| 350 | |
| 351 | for (i = 0; i < len; i++) |
| 352 | free(src_line[i].path); |
| 353 | |
| 354 | free(src_line); |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame^] | 355 | notes->src->lines = NULL; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | /* Get the filename:line for the colored entries */ |
| 359 | 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] | 360 | int evidx, struct rb_root *root, int len, |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 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); |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame] | 368 | struct sym_hist *h = annotation__histogram(notes, evidx); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 369 | |
| 370 | if (!h->sum) |
| 371 | return 0; |
| 372 | |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame^] | 373 | src_line = notes->src->lines = calloc(len, sizeof(struct source_line)); |
| 374 | if (!notes->src->lines) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 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 | |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame] | 441 | static void symbol__annotate_hits(struct symbol *sym, int evidx) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 442 | { |
| 443 | struct annotation *notes = symbol__annotation(sym); |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame] | 444 | struct sym_hist *h = annotation__histogram(notes, evidx); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 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 | |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame^] | 454 | int symbol__annotate_printf(struct symbol *sym, struct map *map, int evidx, |
| 455 | bool full_paths, int min_pcnt, int max_lines) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 456 | { |
| 457 | struct dso *dso = map->dso; |
| 458 | const char *filename = dso->long_name, *d_filename; |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame^] | 459 | struct annotation *notes = symbol__annotation(sym); |
Arnaldo Carvalho de Melo | f1e2701 | 2011-02-05 18:51:38 -0200 | [diff] [blame] | 460 | struct objdump_line *pos; |
Arnaldo Carvalho de Melo | d040bd3 | 2011-02-05 15:37:31 -0200 | [diff] [blame] | 461 | int printed = 2; |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 462 | int more = 0; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 463 | u64 len; |
| 464 | |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 465 | if (full_paths) |
| 466 | d_filename = filename; |
| 467 | else |
| 468 | d_filename = basename(filename); |
| 469 | |
| 470 | len = sym->end - sym->start; |
| 471 | |
Arnaldo Carvalho de Melo | f1e2701 | 2011-02-05 18:51:38 -0200 | [diff] [blame] | 472 | printf(" Percent | Source code & Disassembly of %s\n", d_filename); |
| 473 | printf("------------------------------------------------\n"); |
| 474 | |
| 475 | if (verbose) |
| 476 | symbol__annotate_hits(sym, evidx); |
| 477 | |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame^] | 478 | list_for_each_entry(pos, ¬es->src->source, node) { |
| 479 | switch (objdump_line__print(pos, sym, evidx, len, min_pcnt, |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 480 | printed, max_lines)) { |
| 481 | case 0: |
| 482 | ++printed; |
Arnaldo Carvalho de Melo | f1e2701 | 2011-02-05 18:51:38 -0200 | [diff] [blame] | 483 | break; |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 484 | case 1: |
| 485 | /* filtered by max_lines */ |
| 486 | ++more; |
| 487 | break; |
| 488 | case -1: |
| 489 | default: |
| 490 | /* filtered by min_pcnt */ |
| 491 | break; |
| 492 | } |
| 493 | } |
Arnaldo Carvalho de Melo | f1e2701 | 2011-02-05 18:51:38 -0200 | [diff] [blame] | 494 | |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 495 | return more; |
| 496 | } |
| 497 | |
| 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 | |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame^] | 503 | memset(h, 0, notes->src->sizeof_sym_hist); |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 504 | } |
| 505 | |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame^] | 506 | void symbol__annotate_decay_histogram(struct symbol *sym, int evidx) |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 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 | |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame^] | 514 | list_for_each_entry(pos, ¬es->src->source, node) { |
Arnaldo Carvalho de Melo | 3653246 | 2011-02-06 14:54:44 -0200 | [diff] [blame] | 515 | if (pos->offset != -1) { |
| 516 | h->addr[pos->offset] = h->addr[pos->offset] * 7 / 8; |
| 517 | h->sum += h->addr[pos->offset]; |
| 518 | } |
Arnaldo Carvalho de Melo | f1e2701 | 2011-02-05 18:51:38 -0200 | [diff] [blame] | 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; |
Arnaldo Carvalho de Melo | f1e2701 | 2011-02-05 18:51:38 -0200 | [diff] [blame] | 539 | u64 len; |
| 540 | |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame^] | 541 | if (symbol__annotate(sym, map, 0) < 0) |
Arnaldo Carvalho de Melo | f1e2701 | 2011-02-05 18:51:38 -0200 | [diff] [blame] | 542 | return -1; |
| 543 | |
| 544 | len = sym->end - sym->start; |
| 545 | |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 546 | if (print_lines) { |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame] | 547 | symbol__get_source_line(sym, map, evidx, &source_line, |
| 548 | len, filename); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 549 | print_summary(&source_line, filename); |
| 550 | } |
| 551 | |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame^] | 552 | symbol__annotate_printf(sym, map, evidx, full_paths, |
Arnaldo Carvalho de Melo | f1e2701 | 2011-02-05 18:51:38 -0200 | [diff] [blame] | 553 | min_pcnt, max_lines); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 554 | if (print_lines) |
| 555 | symbol__free_source_line(sym, len); |
| 556 | |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame^] | 557 | objdump_line_list__purge(&symbol__annotation(sym)->src->source); |
Arnaldo Carvalho de Melo | f1e2701 | 2011-02-05 18:51:38 -0200 | [diff] [blame] | 558 | |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 559 | return 0; |
| 560 | } |