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" |
| 17 | |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame^] | 18 | int symbol__alloc_hist(struct symbol *sym, int nevents) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 19 | { |
| 20 | struct annotation *notes = symbol__annotation(sym); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 21 | |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame^] | 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 | return notes->histograms == NULL ? -1 : 0; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 26 | } |
| 27 | |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame^] | 28 | int symbol__inc_addr_samples(struct symbol *sym, struct map *map, |
| 29 | int evidx, u64 addr) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 30 | { |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame^] | 31 | unsigned offset; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 32 | struct annotation *notes; |
| 33 | struct sym_hist *h; |
| 34 | |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 35 | notes = symbol__annotation(sym); |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame^] | 36 | if (notes->histograms == NULL) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 37 | return -ENOMEM; |
| 38 | |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 39 | pr_debug3("%s: addr=%#" PRIx64 "\n", __func__, map->unmap_ip(map, addr)); |
| 40 | |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame^] | 41 | if (addr >= sym->end) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 42 | return 0; |
| 43 | |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame^] | 44 | offset = addr - sym->start; |
| 45 | h = annotation__histogram(notes, evidx); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 46 | h->sum++; |
| 47 | h->addr[offset]++; |
| 48 | |
| 49 | pr_debug3("%#" PRIx64 " %s: period++ [addr: %#" PRIx64 ", %#" PRIx64 |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame^] | 50 | ", evidx=%d] => %" PRIu64 "\n", sym->start, sym->name, |
| 51 | addr, addr - sym->start, evidx, h->addr[offset]); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | static struct objdump_line *objdump_line__new(s64 offset, char *line, size_t privsize) |
| 56 | { |
| 57 | struct objdump_line *self = malloc(sizeof(*self) + privsize); |
| 58 | |
| 59 | if (self != NULL) { |
| 60 | self->offset = offset; |
| 61 | self->line = line; |
| 62 | } |
| 63 | |
| 64 | return self; |
| 65 | } |
| 66 | |
| 67 | void objdump_line__free(struct objdump_line *self) |
| 68 | { |
| 69 | free(self->line); |
| 70 | free(self); |
| 71 | } |
| 72 | |
| 73 | static void objdump__add_line(struct list_head *head, struct objdump_line *line) |
| 74 | { |
| 75 | list_add_tail(&line->node, head); |
| 76 | } |
| 77 | |
| 78 | struct objdump_line *objdump__get_next_ip_line(struct list_head *head, |
| 79 | struct objdump_line *pos) |
| 80 | { |
| 81 | list_for_each_entry_continue(pos, head, node) |
| 82 | if (pos->offset >= 0) |
| 83 | return pos; |
| 84 | |
| 85 | return NULL; |
| 86 | } |
| 87 | |
| 88 | static void objdump_line__print(struct objdump_line *oline, |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame^] | 89 | struct list_head *head, struct symbol *sym, |
| 90 | int evidx, u64 len) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 91 | { |
| 92 | static const char *prev_line; |
| 93 | static const char *prev_color; |
| 94 | |
| 95 | if (oline->offset != -1) { |
| 96 | const char *path = NULL; |
| 97 | unsigned int hits = 0; |
| 98 | double percent = 0.0; |
| 99 | const char *color; |
| 100 | struct annotation *notes = symbol__annotation(sym); |
| 101 | struct source_line *src_line = notes->src_line; |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame^] | 102 | struct sym_hist *h = annotation__histogram(notes, evidx); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 103 | s64 offset = oline->offset; |
| 104 | struct objdump_line *next = objdump__get_next_ip_line(head, oline); |
| 105 | |
| 106 | while (offset < (s64)len && |
| 107 | (next == NULL || offset < next->offset)) { |
| 108 | if (src_line) { |
| 109 | if (path == NULL) |
| 110 | path = src_line[offset].path; |
| 111 | percent += src_line[offset].percent; |
| 112 | } else |
| 113 | hits += h->addr[offset]; |
| 114 | |
| 115 | ++offset; |
| 116 | } |
| 117 | |
| 118 | if (src_line == NULL && h->sum) |
| 119 | percent = 100.0 * hits / h->sum; |
| 120 | |
| 121 | color = get_percent_color(percent); |
| 122 | |
| 123 | /* |
| 124 | * Also color the filename and line if needed, with |
| 125 | * the same color than the percentage. Don't print it |
| 126 | * twice for close colored addr with the same filename:line |
| 127 | */ |
| 128 | if (path) { |
| 129 | if (!prev_line || strcmp(prev_line, path) |
| 130 | || color != prev_color) { |
| 131 | color_fprintf(stdout, color, " %s", path); |
| 132 | prev_line = path; |
| 133 | prev_color = color; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | color_fprintf(stdout, color, " %7.2f", percent); |
| 138 | printf(" : "); |
| 139 | color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", oline->line); |
| 140 | } else { |
| 141 | if (!*oline->line) |
| 142 | printf(" :\n"); |
| 143 | else |
| 144 | printf(" : %s\n", oline->line); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | static int symbol__parse_objdump_line(struct symbol *sym, struct map *map, FILE *file, |
| 149 | struct list_head *head, size_t privsize) |
| 150 | { |
| 151 | struct objdump_line *objdump_line; |
| 152 | char *line = NULL, *tmp, *tmp2, *c; |
| 153 | size_t line_len; |
| 154 | s64 line_ip, offset = -1; |
| 155 | |
| 156 | if (getline(&line, &line_len, file) < 0) |
| 157 | return -1; |
| 158 | |
| 159 | if (!line) |
| 160 | return -1; |
| 161 | |
| 162 | while (line_len != 0 && isspace(line[line_len - 1])) |
| 163 | line[--line_len] = '\0'; |
| 164 | |
| 165 | c = strchr(line, '\n'); |
| 166 | if (c) |
| 167 | *c = 0; |
| 168 | |
| 169 | line_ip = -1; |
| 170 | |
| 171 | /* |
| 172 | * Strip leading spaces: |
| 173 | */ |
| 174 | tmp = line; |
| 175 | while (*tmp) { |
| 176 | if (*tmp != ' ') |
| 177 | break; |
| 178 | tmp++; |
| 179 | } |
| 180 | |
| 181 | if (*tmp) { |
| 182 | /* |
| 183 | * Parse hexa addresses followed by ':' |
| 184 | */ |
| 185 | line_ip = strtoull(tmp, &tmp2, 16); |
| 186 | if (*tmp2 != ':' || tmp == tmp2 || tmp2[1] == '\0') |
| 187 | line_ip = -1; |
| 188 | } |
| 189 | |
| 190 | if (line_ip != -1) { |
| 191 | u64 start = map__rip_2objdump(map, sym->start), |
| 192 | end = map__rip_2objdump(map, sym->end); |
| 193 | |
| 194 | offset = line_ip - start; |
| 195 | if (offset < 0 || (u64)line_ip > end) |
| 196 | offset = -1; |
| 197 | } |
| 198 | |
| 199 | objdump_line = objdump_line__new(offset, line, privsize); |
| 200 | if (objdump_line == NULL) { |
| 201 | free(line); |
| 202 | return -1; |
| 203 | } |
| 204 | objdump__add_line(head, objdump_line); |
| 205 | |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | int symbol__annotate(struct symbol *sym, struct map *map, |
| 210 | struct list_head *head, size_t privsize) |
| 211 | { |
| 212 | struct dso *dso = map->dso; |
| 213 | char *filename = dso__build_id_filename(dso, NULL, 0); |
| 214 | bool free_filename = true; |
| 215 | char command[PATH_MAX * 2]; |
| 216 | FILE *file; |
| 217 | int err = 0; |
| 218 | u64 len; |
| 219 | char symfs_filename[PATH_MAX]; |
| 220 | |
| 221 | if (filename) { |
| 222 | snprintf(symfs_filename, sizeof(symfs_filename), "%s%s", |
| 223 | symbol_conf.symfs, filename); |
| 224 | } |
| 225 | |
| 226 | if (filename == NULL) { |
| 227 | if (dso->has_build_id) { |
| 228 | pr_err("Can't annotate %s: not enough memory\n", |
| 229 | sym->name); |
| 230 | return -ENOMEM; |
| 231 | } |
| 232 | goto fallback; |
| 233 | } else if (readlink(symfs_filename, command, sizeof(command)) < 0 || |
| 234 | strstr(command, "[kernel.kallsyms]") || |
| 235 | access(symfs_filename, R_OK)) { |
| 236 | free(filename); |
| 237 | fallback: |
| 238 | /* |
| 239 | * If we don't have build-ids or the build-id file isn't in the |
| 240 | * cache, or is just a kallsyms file, well, lets hope that this |
| 241 | * DSO is the same as when 'perf record' ran. |
| 242 | */ |
| 243 | filename = dso->long_name; |
| 244 | snprintf(symfs_filename, sizeof(symfs_filename), "%s%s", |
| 245 | symbol_conf.symfs, filename); |
| 246 | free_filename = false; |
| 247 | } |
| 248 | |
| 249 | if (dso->origin == DSO__ORIG_KERNEL) { |
| 250 | if (dso->annotate_warned) |
| 251 | goto out_free_filename; |
| 252 | err = -ENOENT; |
| 253 | dso->annotate_warned = 1; |
| 254 | pr_err("Can't annotate %s: No vmlinux file was found in the " |
| 255 | "path\n", sym->name); |
| 256 | goto out_free_filename; |
| 257 | } |
| 258 | |
| 259 | pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__, |
| 260 | filename, sym->name, map->unmap_ip(map, sym->start), |
| 261 | map->unmap_ip(map, sym->end)); |
| 262 | |
| 263 | len = sym->end - sym->start; |
| 264 | |
| 265 | pr_debug("annotating [%p] %30s : [%p] %30s\n", |
| 266 | dso, dso->long_name, sym, sym->name); |
| 267 | |
| 268 | snprintf(command, sizeof(command), |
| 269 | "objdump --start-address=0x%016" PRIx64 |
| 270 | " --stop-address=0x%016" PRIx64 " -dS -C %s|grep -v %s|expand", |
| 271 | map__rip_2objdump(map, sym->start), |
| 272 | map__rip_2objdump(map, sym->end), |
| 273 | symfs_filename, filename); |
| 274 | |
| 275 | pr_debug("Executing: %s\n", command); |
| 276 | |
| 277 | file = popen(command, "r"); |
| 278 | if (!file) |
| 279 | goto out_free_filename; |
| 280 | |
| 281 | while (!feof(file)) |
| 282 | if (symbol__parse_objdump_line(sym, map, file, head, privsize) < 0) |
| 283 | break; |
| 284 | |
| 285 | pclose(file); |
| 286 | out_free_filename: |
| 287 | if (free_filename) |
| 288 | free(filename); |
| 289 | return err; |
| 290 | } |
| 291 | |
| 292 | static void insert_source_line(struct rb_root *root, struct source_line *src_line) |
| 293 | { |
| 294 | struct source_line *iter; |
| 295 | struct rb_node **p = &root->rb_node; |
| 296 | struct rb_node *parent = NULL; |
| 297 | |
| 298 | while (*p != NULL) { |
| 299 | parent = *p; |
| 300 | iter = rb_entry(parent, struct source_line, node); |
| 301 | |
| 302 | if (src_line->percent > iter->percent) |
| 303 | p = &(*p)->rb_left; |
| 304 | else |
| 305 | p = &(*p)->rb_right; |
| 306 | } |
| 307 | |
| 308 | rb_link_node(&src_line->node, parent, p); |
| 309 | rb_insert_color(&src_line->node, root); |
| 310 | } |
| 311 | |
| 312 | static void symbol__free_source_line(struct symbol *sym, int len) |
| 313 | { |
| 314 | struct annotation *notes = symbol__annotation(sym); |
| 315 | struct source_line *src_line = notes->src_line; |
| 316 | int i; |
| 317 | |
| 318 | for (i = 0; i < len; i++) |
| 319 | free(src_line[i].path); |
| 320 | |
| 321 | free(src_line); |
| 322 | notes->src_line = NULL; |
| 323 | } |
| 324 | |
| 325 | /* Get the filename:line for the colored entries */ |
| 326 | 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^] | 327 | int evidx, struct rb_root *root, int len, |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 328 | const char *filename) |
| 329 | { |
| 330 | u64 start; |
| 331 | int i; |
| 332 | char cmd[PATH_MAX * 2]; |
| 333 | struct source_line *src_line; |
| 334 | struct annotation *notes = symbol__annotation(sym); |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame^] | 335 | struct sym_hist *h = annotation__histogram(notes, evidx); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 336 | |
| 337 | if (!h->sum) |
| 338 | return 0; |
| 339 | |
| 340 | src_line = notes->src_line = calloc(len, sizeof(struct source_line)); |
| 341 | if (!notes->src_line) |
| 342 | return -1; |
| 343 | |
| 344 | start = map->unmap_ip(map, sym->start); |
| 345 | |
| 346 | for (i = 0; i < len; i++) { |
| 347 | char *path = NULL; |
| 348 | size_t line_len; |
| 349 | u64 offset; |
| 350 | FILE *fp; |
| 351 | |
| 352 | src_line[i].percent = 100.0 * h->addr[i] / h->sum; |
| 353 | if (src_line[i].percent <= 0.5) |
| 354 | continue; |
| 355 | |
| 356 | offset = start + i; |
| 357 | sprintf(cmd, "addr2line -e %s %016" PRIx64, filename, offset); |
| 358 | fp = popen(cmd, "r"); |
| 359 | if (!fp) |
| 360 | continue; |
| 361 | |
| 362 | if (getline(&path, &line_len, fp) < 0 || !line_len) |
| 363 | goto next; |
| 364 | |
| 365 | src_line[i].path = malloc(sizeof(char) * line_len + 1); |
| 366 | if (!src_line[i].path) |
| 367 | goto next; |
| 368 | |
| 369 | strcpy(src_line[i].path, path); |
| 370 | insert_source_line(root, &src_line[i]); |
| 371 | |
| 372 | next: |
| 373 | pclose(fp); |
| 374 | } |
| 375 | |
| 376 | return 0; |
| 377 | } |
| 378 | |
| 379 | static void print_summary(struct rb_root *root, const char *filename) |
| 380 | { |
| 381 | struct source_line *src_line; |
| 382 | struct rb_node *node; |
| 383 | |
| 384 | printf("\nSorted summary for file %s\n", filename); |
| 385 | printf("----------------------------------------------\n\n"); |
| 386 | |
| 387 | if (RB_EMPTY_ROOT(root)) { |
| 388 | printf(" Nothing higher than %1.1f%%\n", MIN_GREEN); |
| 389 | return; |
| 390 | } |
| 391 | |
| 392 | node = rb_first(root); |
| 393 | while (node) { |
| 394 | double percent; |
| 395 | const char *color; |
| 396 | char *path; |
| 397 | |
| 398 | src_line = rb_entry(node, struct source_line, node); |
| 399 | percent = src_line->percent; |
| 400 | color = get_percent_color(percent); |
| 401 | path = src_line->path; |
| 402 | |
| 403 | color_fprintf(stdout, color, " %7.2f %s", percent, path); |
| 404 | node = rb_next(node); |
| 405 | } |
| 406 | } |
| 407 | |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame^] | 408 | static void symbol__annotate_hits(struct symbol *sym, int evidx) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 409 | { |
| 410 | struct annotation *notes = symbol__annotation(sym); |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame^] | 411 | struct sym_hist *h = annotation__histogram(notes, evidx); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 412 | u64 len = sym->end - sym->start, offset; |
| 413 | |
| 414 | for (offset = 0; offset < len; ++offset) |
| 415 | if (h->addr[offset] != 0) |
| 416 | printf("%*" PRIx64 ": %" PRIu64 "\n", BITS_PER_LONG / 2, |
| 417 | sym->start + offset, h->addr[offset]); |
| 418 | printf("%*s: %" PRIu64 "\n", BITS_PER_LONG / 2, "h->sum", h->sum); |
| 419 | } |
| 420 | |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame^] | 421 | int symbol__tty_annotate(struct symbol *sym, struct map *map, int evidx, |
| 422 | bool print_lines, bool full_paths) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 423 | { |
| 424 | struct dso *dso = map->dso; |
| 425 | const char *filename = dso->long_name, *d_filename; |
| 426 | struct rb_root source_line = RB_ROOT; |
| 427 | struct objdump_line *pos, *n; |
| 428 | LIST_HEAD(head); |
| 429 | u64 len; |
| 430 | |
| 431 | if (symbol__annotate(sym, map, &head, 0) < 0) |
| 432 | return -1; |
| 433 | |
| 434 | if (full_paths) |
| 435 | d_filename = filename; |
| 436 | else |
| 437 | d_filename = basename(filename); |
| 438 | |
| 439 | len = sym->end - sym->start; |
| 440 | |
| 441 | if (print_lines) { |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame^] | 442 | symbol__get_source_line(sym, map, evidx, &source_line, |
| 443 | len, filename); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 444 | print_summary(&source_line, filename); |
| 445 | } |
| 446 | |
| 447 | printf("\n\n------------------------------------------------\n"); |
| 448 | printf(" Percent | Source code & Disassembly of %s\n", d_filename); |
| 449 | printf("------------------------------------------------\n"); |
| 450 | |
| 451 | if (verbose) |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame^] | 452 | symbol__annotate_hits(sym, evidx); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 453 | |
| 454 | list_for_each_entry_safe(pos, n, &head, node) { |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame^] | 455 | objdump_line__print(pos, &head, sym, evidx, len); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 456 | list_del(&pos->node); |
| 457 | objdump_line__free(pos); |
| 458 | } |
| 459 | |
| 460 | if (print_lines) |
| 461 | symbol__free_source_line(sym, len); |
| 462 | |
| 463 | return 0; |
| 464 | } |