186470930SIngo Molnar /* 286470930SIngo Molnar * builtin-annotate.c 386470930SIngo Molnar * 486470930SIngo Molnar * Builtin annotate command: Analyze the perf.data input file, 586470930SIngo Molnar * look up and read DSOs and symbol information and display 686470930SIngo Molnar * a histogram of results, along various sorting keys. 786470930SIngo Molnar */ 886470930SIngo Molnar #include "builtin.h" 986470930SIngo Molnar 1086470930SIngo Molnar #include "util/util.h" 1186470930SIngo Molnar 1286470930SIngo Molnar #include "util/color.h" 135da50258SArnaldo Carvalho de Melo #include <linux/list.h> 1486470930SIngo Molnar #include "util/cache.h" 1543cbcd8aSArnaldo Carvalho de Melo #include <linux/rbtree.h> 1686470930SIngo Molnar #include "util/symbol.h" 1786470930SIngo Molnar #include "util/string.h" 1886470930SIngo Molnar 1986470930SIngo Molnar #include "perf.h" 208f28827aSFrederic Weisbecker #include "util/debug.h" 2186470930SIngo Molnar 2262daacb5SArnaldo Carvalho de Melo #include "util/event.h" 2386470930SIngo Molnar #include "util/parse-options.h" 2486470930SIngo Molnar #include "util/parse-events.h" 256baa0a5aSFrederic Weisbecker #include "util/thread.h" 26dd68ada2SJohn Kacur #include "util/sort.h" 273d1d07ecSJohn Kacur #include "util/hist.h" 2894c744b6SArnaldo Carvalho de Melo #include "util/session.h" 2986470930SIngo Molnar 3086470930SIngo Molnar static char const *input_name = "perf.data"; 3186470930SIngo Molnar 32fa6963b2SPeter Zijlstra static int force; 3386470930SIngo Molnar 3442976487SMike Galbraith static int full_paths; 3542976487SMike Galbraith 36301406b9SFrederic Weisbecker static int print_line; 37301406b9SFrederic Weisbecker 38e4204992SArnaldo Carvalho de Melo struct sym_hist { 39e4204992SArnaldo Carvalho de Melo u64 sum; 40e4204992SArnaldo Carvalho de Melo u64 ip[0]; 41e4204992SArnaldo Carvalho de Melo }; 42e4204992SArnaldo Carvalho de Melo 43301406b9SFrederic Weisbecker struct sym_ext { 44971738f3SFrederic Weisbecker struct rb_node node; 45301406b9SFrederic Weisbecker double percent; 46301406b9SFrederic Weisbecker char *path; 47301406b9SFrederic Weisbecker }; 48301406b9SFrederic Weisbecker 49e4204992SArnaldo Carvalho de Melo struct sym_priv { 50e4204992SArnaldo Carvalho de Melo struct sym_hist *hist; 51e4204992SArnaldo Carvalho de Melo struct sym_ext *ext; 52e4204992SArnaldo Carvalho de Melo }; 53e4204992SArnaldo Carvalho de Melo 54b32d133aSArnaldo Carvalho de Melo static struct symbol_conf symbol_conf = { 55b32d133aSArnaldo Carvalho de Melo .priv_size = sizeof(struct sym_priv), 56b32d133aSArnaldo Carvalho de Melo .try_vmlinux_path = true, 57b32d133aSArnaldo Carvalho de Melo }; 58b32d133aSArnaldo Carvalho de Melo 59e4204992SArnaldo Carvalho de Melo static const char *sym_hist_filter; 60e4204992SArnaldo Carvalho de Melo 6100a192b3SArnaldo Carvalho de Melo static int symbol_filter(struct map *map __used, struct symbol *sym) 62e4204992SArnaldo Carvalho de Melo { 638f0b0373SArnaldo Carvalho de Melo if (sym_hist_filter == NULL || 648f0b0373SArnaldo Carvalho de Melo strcmp(sym->name, sym_hist_filter) == 0) { 6500a192b3SArnaldo Carvalho de Melo struct sym_priv *priv = symbol__priv(sym); 66e4204992SArnaldo Carvalho de Melo const int size = (sizeof(*priv->hist) + 67e4204992SArnaldo Carvalho de Melo (sym->end - sym->start) * sizeof(u64)); 68e4204992SArnaldo Carvalho de Melo 69e4204992SArnaldo Carvalho de Melo priv->hist = malloc(size); 70e4204992SArnaldo Carvalho de Melo if (priv->hist) 71e4204992SArnaldo Carvalho de Melo memset(priv->hist, 0, size); 72e4204992SArnaldo Carvalho de Melo return 0; 73e4204992SArnaldo Carvalho de Melo } 74e4204992SArnaldo Carvalho de Melo /* 75e4204992SArnaldo Carvalho de Melo * FIXME: We should really filter it out, as we don't want to go thru symbols 76e4204992SArnaldo Carvalho de Melo * we're not interested, and if a DSO ends up with no symbols, delete it too, 77e4204992SArnaldo Carvalho de Melo * but right now the kernel loading routines in symbol.c bail out if no symbols 78e4204992SArnaldo Carvalho de Melo * are found, fix it later. 79e4204992SArnaldo Carvalho de Melo */ 80e4204992SArnaldo Carvalho de Melo return 0; 81e4204992SArnaldo Carvalho de Melo } 8286470930SIngo Molnar 8386470930SIngo Molnar /* 8486470930SIngo Molnar * collect histogram counts 8586470930SIngo Molnar */ 869cffa8d5SPaul Mackerras static void hist_hit(struct hist_entry *he, u64 ip) 8786470930SIngo Molnar { 8886470930SIngo Molnar unsigned int sym_size, offset; 8986470930SIngo Molnar struct symbol *sym = he->sym; 90e4204992SArnaldo Carvalho de Melo struct sym_priv *priv; 91e4204992SArnaldo Carvalho de Melo struct sym_hist *h; 9286470930SIngo Molnar 9386470930SIngo Molnar he->count++; 9486470930SIngo Molnar 95e4204992SArnaldo Carvalho de Melo if (!sym || !he->map) 96e4204992SArnaldo Carvalho de Melo return; 97e4204992SArnaldo Carvalho de Melo 9800a192b3SArnaldo Carvalho de Melo priv = symbol__priv(sym); 99e4204992SArnaldo Carvalho de Melo if (!priv->hist) 10086470930SIngo Molnar return; 10186470930SIngo Molnar 10286470930SIngo Molnar sym_size = sym->end - sym->start; 10386470930SIngo Molnar offset = ip - sym->start; 10486470930SIngo Molnar 105ed52ce2eSArnaldo Carvalho de Melo if (verbose) 106ed52ce2eSArnaldo Carvalho de Melo fprintf(stderr, "%s: ip=%Lx\n", __func__, 107ed52ce2eSArnaldo Carvalho de Melo he->map->unmap_ip(he->map, ip)); 108ed52ce2eSArnaldo Carvalho de Melo 10986470930SIngo Molnar if (offset >= sym_size) 11086470930SIngo Molnar return; 11186470930SIngo Molnar 112e4204992SArnaldo Carvalho de Melo h = priv->hist; 113e4204992SArnaldo Carvalho de Melo h->sum++; 114e4204992SArnaldo Carvalho de Melo h->ip[offset]++; 11586470930SIngo Molnar 11686470930SIngo Molnar if (verbose >= 3) 11786470930SIngo Molnar printf("%p %s: count++ [ip: %p, %08Lx] => %Ld\n", 1187d37a0cbSArjan van de Ven (void *)(unsigned long)he->sym->start, 11986470930SIngo Molnar he->sym->name, 1207d37a0cbSArjan van de Ven (void *)(unsigned long)ip, ip - he->sym->start, 121e4204992SArnaldo Carvalho de Melo h->ip[offset]); 12286470930SIngo Molnar } 12386470930SIngo Molnar 1241ed091c4SArnaldo Carvalho de Melo static int hist_entry__add(struct addr_location *al, u64 count) 12586470930SIngo Molnar { 1269735abf1SArnaldo Carvalho de Melo bool hit; 1271ed091c4SArnaldo Carvalho de Melo struct hist_entry *he = __hist_entry__add(al, NULL, count, &hit); 1289735abf1SArnaldo Carvalho de Melo if (he == NULL) 12986470930SIngo Molnar return -ENOMEM; 1301ed091c4SArnaldo Carvalho de Melo hist_hit(he, al->addr); 13186470930SIngo Molnar return 0; 13286470930SIngo Molnar } 13386470930SIngo Molnar 134b3165f41SArnaldo Carvalho de Melo static int process_sample_event(event_t *event, struct perf_session *session) 13586470930SIngo Molnar { 1361ed091c4SArnaldo Carvalho de Melo struct addr_location al; 1376baa0a5aSFrederic Weisbecker 13862daacb5SArnaldo Carvalho de Melo dump_printf("(IP, %d): %d: %p\n", event->header.misc, 1391ed091c4SArnaldo Carvalho de Melo event->ip.pid, (void *)(long)event->ip.ip); 14086470930SIngo Molnar 141b3165f41SArnaldo Carvalho de Melo if (event__preprocess_sample(event, session, &al, symbol_filter) < 0) { 14286470930SIngo Molnar fprintf(stderr, "problem processing %d event, skipping it.\n", 14386470930SIngo Molnar event->header.type); 14486470930SIngo Molnar return -1; 14586470930SIngo Molnar } 14686470930SIngo Molnar 1471ed091c4SArnaldo Carvalho de Melo if (hist_entry__add(&al, 1)) { 148ec218fc4SArnaldo Carvalho de Melo fprintf(stderr, "problem incrementing symbol count, " 149ec218fc4SArnaldo Carvalho de Melo "skipping event\n"); 15086470930SIngo Molnar return -1; 15186470930SIngo Molnar } 15286470930SIngo Molnar 15386470930SIngo Molnar return 0; 15486470930SIngo Molnar } 15586470930SIngo Molnar 156ed52ce2eSArnaldo Carvalho de Melo static int parse_line(FILE *file, struct hist_entry *he, u64 len) 15786470930SIngo Molnar { 158ed52ce2eSArnaldo Carvalho de Melo struct symbol *sym = he->sym; 15986470930SIngo Molnar char *line = NULL, *tmp, *tmp2; 160301406b9SFrederic Weisbecker static const char *prev_line; 161301406b9SFrederic Weisbecker static const char *prev_color; 16286470930SIngo Molnar unsigned int offset; 16386470930SIngo Molnar size_t line_len; 164ed52ce2eSArnaldo Carvalho de Melo u64 start; 165f37a291cSIngo Molnar s64 line_ip; 16686470930SIngo Molnar int ret; 16786470930SIngo Molnar char *c; 16886470930SIngo Molnar 16986470930SIngo Molnar if (getline(&line, &line_len, file) < 0) 17086470930SIngo Molnar return -1; 17186470930SIngo Molnar if (!line) 17286470930SIngo Molnar return -1; 17386470930SIngo Molnar 17486470930SIngo Molnar c = strchr(line, '\n'); 17586470930SIngo Molnar if (c) 17686470930SIngo Molnar *c = 0; 17786470930SIngo Molnar 17886470930SIngo Molnar line_ip = -1; 17986470930SIngo Molnar offset = 0; 18086470930SIngo Molnar ret = -2; 18186470930SIngo Molnar 18286470930SIngo Molnar /* 18386470930SIngo Molnar * Strip leading spaces: 18486470930SIngo Molnar */ 18586470930SIngo Molnar tmp = line; 18686470930SIngo Molnar while (*tmp) { 18786470930SIngo Molnar if (*tmp != ' ') 18886470930SIngo Molnar break; 18986470930SIngo Molnar tmp++; 19086470930SIngo Molnar } 19186470930SIngo Molnar 19286470930SIngo Molnar if (*tmp) { 19386470930SIngo Molnar /* 19486470930SIngo Molnar * Parse hexa addresses followed by ':' 19586470930SIngo Molnar */ 19686470930SIngo Molnar line_ip = strtoull(tmp, &tmp2, 16); 19786470930SIngo Molnar if (*tmp2 != ':') 19886470930SIngo Molnar line_ip = -1; 19986470930SIngo Molnar } 20086470930SIngo Molnar 201ed52ce2eSArnaldo Carvalho de Melo start = he->map->unmap_ip(he->map, sym->start); 202ed52ce2eSArnaldo Carvalho de Melo 20386470930SIngo Molnar if (line_ip != -1) { 204301406b9SFrederic Weisbecker const char *path = NULL; 20586470930SIngo Molnar unsigned int hits = 0; 20686470930SIngo Molnar double percent = 0.0; 20783a0944fSIngo Molnar const char *color; 20800a192b3SArnaldo Carvalho de Melo struct sym_priv *priv = symbol__priv(sym); 209e4204992SArnaldo Carvalho de Melo struct sym_ext *sym_ext = priv->ext; 210e4204992SArnaldo Carvalho de Melo struct sym_hist *h = priv->hist; 21186470930SIngo Molnar 212ed52ce2eSArnaldo Carvalho de Melo offset = line_ip - start; 21386470930SIngo Molnar if (offset < len) 214e4204992SArnaldo Carvalho de Melo hits = h->ip[offset]; 21586470930SIngo Molnar 216c17c2db1SFrederic Weisbecker if (offset < len && sym_ext) { 217301406b9SFrederic Weisbecker path = sym_ext[offset].path; 218301406b9SFrederic Weisbecker percent = sym_ext[offset].percent; 219e4204992SArnaldo Carvalho de Melo } else if (h->sum) 220e4204992SArnaldo Carvalho de Melo percent = 100.0 * hits / h->sum; 22186470930SIngo Molnar 2221e11fd82SFrederic Weisbecker color = get_percent_color(percent); 22386470930SIngo Molnar 224301406b9SFrederic Weisbecker /* 225301406b9SFrederic Weisbecker * Also color the filename and line if needed, with 226301406b9SFrederic Weisbecker * the same color than the percentage. Don't print it 227301406b9SFrederic Weisbecker * twice for close colored ip with the same filename:line 228301406b9SFrederic Weisbecker */ 229301406b9SFrederic Weisbecker if (path) { 230301406b9SFrederic Weisbecker if (!prev_line || strcmp(prev_line, path) 231301406b9SFrederic Weisbecker || color != prev_color) { 232301406b9SFrederic Weisbecker color_fprintf(stdout, color, " %s", path); 233301406b9SFrederic Weisbecker prev_line = path; 234301406b9SFrederic Weisbecker prev_color = color; 235301406b9SFrederic Weisbecker } 236301406b9SFrederic Weisbecker } 237301406b9SFrederic Weisbecker 23886470930SIngo Molnar color_fprintf(stdout, color, " %7.2f", percent); 23986470930SIngo Molnar printf(" : "); 24086470930SIngo Molnar color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", line); 24186470930SIngo Molnar } else { 24286470930SIngo Molnar if (!*line) 24386470930SIngo Molnar printf(" :\n"); 24486470930SIngo Molnar else 24586470930SIngo Molnar printf(" : %s\n", line); 24686470930SIngo Molnar } 24786470930SIngo Molnar 24886470930SIngo Molnar return 0; 24986470930SIngo Molnar } 25086470930SIngo Molnar 251971738f3SFrederic Weisbecker static struct rb_root root_sym_ext; 252971738f3SFrederic Weisbecker 253971738f3SFrederic Weisbecker static void insert_source_line(struct sym_ext *sym_ext) 254971738f3SFrederic Weisbecker { 255971738f3SFrederic Weisbecker struct sym_ext *iter; 256971738f3SFrederic Weisbecker struct rb_node **p = &root_sym_ext.rb_node; 257971738f3SFrederic Weisbecker struct rb_node *parent = NULL; 258971738f3SFrederic Weisbecker 259971738f3SFrederic Weisbecker while (*p != NULL) { 260971738f3SFrederic Weisbecker parent = *p; 261971738f3SFrederic Weisbecker iter = rb_entry(parent, struct sym_ext, node); 262971738f3SFrederic Weisbecker 263971738f3SFrederic Weisbecker if (sym_ext->percent > iter->percent) 264971738f3SFrederic Weisbecker p = &(*p)->rb_left; 265971738f3SFrederic Weisbecker else 266971738f3SFrederic Weisbecker p = &(*p)->rb_right; 267971738f3SFrederic Weisbecker } 268971738f3SFrederic Weisbecker 269971738f3SFrederic Weisbecker rb_link_node(&sym_ext->node, parent, p); 270971738f3SFrederic Weisbecker rb_insert_color(&sym_ext->node, &root_sym_ext); 271971738f3SFrederic Weisbecker } 272971738f3SFrederic Weisbecker 273e4204992SArnaldo Carvalho de Melo static void free_source_line(struct hist_entry *he, int len) 274301406b9SFrederic Weisbecker { 27500a192b3SArnaldo Carvalho de Melo struct sym_priv *priv = symbol__priv(he->sym); 276e4204992SArnaldo Carvalho de Melo struct sym_ext *sym_ext = priv->ext; 277301406b9SFrederic Weisbecker int i; 278301406b9SFrederic Weisbecker 279301406b9SFrederic Weisbecker if (!sym_ext) 280301406b9SFrederic Weisbecker return; 281301406b9SFrederic Weisbecker 282301406b9SFrederic Weisbecker for (i = 0; i < len; i++) 283301406b9SFrederic Weisbecker free(sym_ext[i].path); 284301406b9SFrederic Weisbecker free(sym_ext); 285301406b9SFrederic Weisbecker 286e4204992SArnaldo Carvalho de Melo priv->ext = NULL; 287971738f3SFrederic Weisbecker root_sym_ext = RB_ROOT; 288301406b9SFrederic Weisbecker } 289301406b9SFrederic Weisbecker 290301406b9SFrederic Weisbecker /* Get the filename:line for the colored entries */ 291c17c2db1SFrederic Weisbecker static void 292ed52ce2eSArnaldo Carvalho de Melo get_source_line(struct hist_entry *he, int len, const char *filename) 293301406b9SFrederic Weisbecker { 294ed52ce2eSArnaldo Carvalho de Melo struct symbol *sym = he->sym; 295ed52ce2eSArnaldo Carvalho de Melo u64 start; 296301406b9SFrederic Weisbecker int i; 297301406b9SFrederic Weisbecker char cmd[PATH_MAX * 2]; 298301406b9SFrederic Weisbecker struct sym_ext *sym_ext; 29900a192b3SArnaldo Carvalho de Melo struct sym_priv *priv = symbol__priv(sym); 300e4204992SArnaldo Carvalho de Melo struct sym_hist *h = priv->hist; 301301406b9SFrederic Weisbecker 302e4204992SArnaldo Carvalho de Melo if (!h->sum) 303301406b9SFrederic Weisbecker return; 304301406b9SFrederic Weisbecker 305e4204992SArnaldo Carvalho de Melo sym_ext = priv->ext = calloc(len, sizeof(struct sym_ext)); 306e4204992SArnaldo Carvalho de Melo if (!priv->ext) 307301406b9SFrederic Weisbecker return; 308301406b9SFrederic Weisbecker 309ed52ce2eSArnaldo Carvalho de Melo start = he->map->unmap_ip(he->map, sym->start); 310301406b9SFrederic Weisbecker 311301406b9SFrederic Weisbecker for (i = 0; i < len; i++) { 312301406b9SFrederic Weisbecker char *path = NULL; 313301406b9SFrederic Weisbecker size_t line_len; 3149cffa8d5SPaul Mackerras u64 offset; 315301406b9SFrederic Weisbecker FILE *fp; 316301406b9SFrederic Weisbecker 317e4204992SArnaldo Carvalho de Melo sym_ext[i].percent = 100.0 * h->ip[i] / h->sum; 318301406b9SFrederic Weisbecker if (sym_ext[i].percent <= 0.5) 319301406b9SFrederic Weisbecker continue; 320301406b9SFrederic Weisbecker 321ed52ce2eSArnaldo Carvalho de Melo offset = start + i; 322c17c2db1SFrederic Weisbecker sprintf(cmd, "addr2line -e %s %016llx", filename, offset); 323301406b9SFrederic Weisbecker fp = popen(cmd, "r"); 324301406b9SFrederic Weisbecker if (!fp) 325301406b9SFrederic Weisbecker continue; 326301406b9SFrederic Weisbecker 327301406b9SFrederic Weisbecker if (getline(&path, &line_len, fp) < 0 || !line_len) 328301406b9SFrederic Weisbecker goto next; 329301406b9SFrederic Weisbecker 330c17c2db1SFrederic Weisbecker sym_ext[i].path = malloc(sizeof(char) * line_len + 1); 331301406b9SFrederic Weisbecker if (!sym_ext[i].path) 332301406b9SFrederic Weisbecker goto next; 333301406b9SFrederic Weisbecker 334301406b9SFrederic Weisbecker strcpy(sym_ext[i].path, path); 335971738f3SFrederic Weisbecker insert_source_line(&sym_ext[i]); 336301406b9SFrederic Weisbecker 337301406b9SFrederic Weisbecker next: 338301406b9SFrederic Weisbecker pclose(fp); 339301406b9SFrederic Weisbecker } 340301406b9SFrederic Weisbecker } 341301406b9SFrederic Weisbecker 34283a0944fSIngo Molnar static void print_summary(const char *filename) 343971738f3SFrederic Weisbecker { 344971738f3SFrederic Weisbecker struct sym_ext *sym_ext; 345971738f3SFrederic Weisbecker struct rb_node *node; 346971738f3SFrederic Weisbecker 347971738f3SFrederic Weisbecker printf("\nSorted summary for file %s\n", filename); 348971738f3SFrederic Weisbecker printf("----------------------------------------------\n\n"); 349971738f3SFrederic Weisbecker 350971738f3SFrederic Weisbecker if (RB_EMPTY_ROOT(&root_sym_ext)) { 351971738f3SFrederic Weisbecker printf(" Nothing higher than %1.1f%%\n", MIN_GREEN); 352971738f3SFrederic Weisbecker return; 353971738f3SFrederic Weisbecker } 354971738f3SFrederic Weisbecker 355971738f3SFrederic Weisbecker node = rb_first(&root_sym_ext); 356971738f3SFrederic Weisbecker while (node) { 357971738f3SFrederic Weisbecker double percent; 35883a0944fSIngo Molnar const char *color; 359971738f3SFrederic Weisbecker char *path; 360971738f3SFrederic Weisbecker 361971738f3SFrederic Weisbecker sym_ext = rb_entry(node, struct sym_ext, node); 362971738f3SFrederic Weisbecker percent = sym_ext->percent; 3631e11fd82SFrederic Weisbecker color = get_percent_color(percent); 364971738f3SFrederic Weisbecker path = sym_ext->path; 365971738f3SFrederic Weisbecker 366971738f3SFrederic Weisbecker color_fprintf(stdout, color, " %7.2f %s", percent, path); 367971738f3SFrederic Weisbecker node = rb_next(node); 368971738f3SFrederic Weisbecker } 369971738f3SFrederic Weisbecker } 370971738f3SFrederic Weisbecker 371ed52ce2eSArnaldo Carvalho de Melo static void annotate_sym(struct hist_entry *he) 37286470930SIngo Molnar { 373ed52ce2eSArnaldo Carvalho de Melo struct map *map = he->map; 374ed52ce2eSArnaldo Carvalho de Melo struct dso *dso = map->dso; 375ed52ce2eSArnaldo Carvalho de Melo struct symbol *sym = he->sym; 376439d473bSArnaldo Carvalho de Melo const char *filename = dso->long_name, *d_filename; 377439d473bSArnaldo Carvalho de Melo u64 len; 37886470930SIngo Molnar char command[PATH_MAX*2]; 37986470930SIngo Molnar FILE *file; 38086470930SIngo Molnar 38186470930SIngo Molnar if (!filename) 38286470930SIngo Molnar return; 38386470930SIngo Molnar 384ed52ce2eSArnaldo Carvalho de Melo if (verbose) 385ed52ce2eSArnaldo Carvalho de Melo fprintf(stderr, "%s: filename=%s, sym=%s, start=%Lx, end=%Lx\n", 386ed52ce2eSArnaldo Carvalho de Melo __func__, filename, sym->name, 387ed52ce2eSArnaldo Carvalho de Melo map->unmap_ip(map, sym->start), 388ed52ce2eSArnaldo Carvalho de Melo map->unmap_ip(map, sym->end)); 389ed52ce2eSArnaldo Carvalho de Melo 39042976487SMike Galbraith if (full_paths) 39142976487SMike Galbraith d_filename = filename; 39242976487SMike Galbraith else 39342976487SMike Galbraith d_filename = basename(filename); 39486470930SIngo Molnar 39586470930SIngo Molnar len = sym->end - sym->start; 39686470930SIngo Molnar 397971738f3SFrederic Weisbecker if (print_line) { 398ed52ce2eSArnaldo Carvalho de Melo get_source_line(he, len, filename); 399971738f3SFrederic Weisbecker print_summary(filename); 400971738f3SFrederic Weisbecker } 401971738f3SFrederic Weisbecker 402971738f3SFrederic Weisbecker printf("\n\n------------------------------------------------\n"); 40342976487SMike Galbraith printf(" Percent | Source code & Disassembly of %s\n", d_filename); 404971738f3SFrederic Weisbecker printf("------------------------------------------------\n"); 405971738f3SFrederic Weisbecker 406971738f3SFrederic Weisbecker if (verbose >= 2) 407439d473bSArnaldo Carvalho de Melo printf("annotating [%p] %30s : [%p] %30s\n", 408439d473bSArnaldo Carvalho de Melo dso, dso->long_name, sym, sym->name); 409301406b9SFrederic Weisbecker 41042976487SMike Galbraith sprintf(command, "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS %s|grep -v %s", 411ed52ce2eSArnaldo Carvalho de Melo map->unmap_ip(map, sym->start), map->unmap_ip(map, sym->end), 412ed52ce2eSArnaldo Carvalho de Melo filename, filename); 41386470930SIngo Molnar 41486470930SIngo Molnar if (verbose >= 3) 41586470930SIngo Molnar printf("doing: %s\n", command); 41686470930SIngo Molnar 41786470930SIngo Molnar file = popen(command, "r"); 41886470930SIngo Molnar if (!file) 41986470930SIngo Molnar return; 42086470930SIngo Molnar 42186470930SIngo Molnar while (!feof(file)) { 422ed52ce2eSArnaldo Carvalho de Melo if (parse_line(file, he, len) < 0) 42386470930SIngo Molnar break; 42486470930SIngo Molnar } 42586470930SIngo Molnar 42686470930SIngo Molnar pclose(file); 427971738f3SFrederic Weisbecker if (print_line) 428e4204992SArnaldo Carvalho de Melo free_source_line(he, len); 42986470930SIngo Molnar } 43086470930SIngo Molnar 43186470930SIngo Molnar static void find_annotations(void) 43286470930SIngo Molnar { 43386470930SIngo Molnar struct rb_node *nd; 43486470930SIngo Molnar 435ed52ce2eSArnaldo Carvalho de Melo for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) { 436ed52ce2eSArnaldo Carvalho de Melo struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node); 437e4204992SArnaldo Carvalho de Melo struct sym_priv *priv; 43886470930SIngo Molnar 439e4204992SArnaldo Carvalho de Melo if (he->sym == NULL) 440e4204992SArnaldo Carvalho de Melo continue; 441e4204992SArnaldo Carvalho de Melo 44200a192b3SArnaldo Carvalho de Melo priv = symbol__priv(he->sym); 443e4204992SArnaldo Carvalho de Melo if (priv->hist == NULL) 444e4204992SArnaldo Carvalho de Melo continue; 445e4204992SArnaldo Carvalho de Melo 446ed52ce2eSArnaldo Carvalho de Melo annotate_sym(he); 447ed52ce2eSArnaldo Carvalho de Melo /* 448e4204992SArnaldo Carvalho de Melo * Since we have a hist_entry per IP for the same symbol, free 449e4204992SArnaldo Carvalho de Melo * he->sym->hist to signal we already processed this symbol. 450ed52ce2eSArnaldo Carvalho de Melo */ 451e4204992SArnaldo Carvalho de Melo free(priv->hist); 452e4204992SArnaldo Carvalho de Melo priv->hist = NULL; 45386470930SIngo Molnar } 45486470930SIngo Molnar } 45586470930SIngo Molnar 456301a0b02SArnaldo Carvalho de Melo static struct perf_event_ops event_ops = { 457bab81b62SLi Zefan .process_sample_event = process_sample_event, 458bab81b62SLi Zefan .process_mmap_event = event__process_mmap, 459bab81b62SLi Zefan .process_comm_event = event__process_comm, 460bab81b62SLi Zefan .process_fork_event = event__process_task, 461bab81b62SLi Zefan }; 462bab81b62SLi Zefan 46386470930SIngo Molnar static int __cmd_annotate(void) 46486470930SIngo Molnar { 465301a0b02SArnaldo Carvalho de Melo struct perf_session *session = perf_session__new(input_name, O_RDONLY, 4664aa65636SArnaldo Carvalho de Melo force, &symbol_conf); 467bab81b62SLi Zefan int ret; 46886470930SIngo Molnar 46994c744b6SArnaldo Carvalho de Melo if (session == NULL) 47094c744b6SArnaldo Carvalho de Melo return -ENOMEM; 47194c744b6SArnaldo Carvalho de Melo 472ec913369SArnaldo Carvalho de Melo ret = perf_session__process_events(session, &event_ops); 473bab81b62SLi Zefan if (ret) 47494c744b6SArnaldo Carvalho de Melo goto out_delete; 47586470930SIngo Molnar 47662daacb5SArnaldo Carvalho de Melo if (dump_trace) { 47762daacb5SArnaldo Carvalho de Melo event__print_totals(); 47894c744b6SArnaldo Carvalho de Melo goto out_delete; 47962daacb5SArnaldo Carvalho de Melo } 48086470930SIngo Molnar 481da21d1b5SArnaldo Carvalho de Melo if (verbose > 3) 482b3165f41SArnaldo Carvalho de Melo perf_session__fprintf(session, stdout); 48386470930SIngo Molnar 484da21d1b5SArnaldo Carvalho de Melo if (verbose > 2) 48586470930SIngo Molnar dsos__fprintf(stdout); 48686470930SIngo Molnar 48786470930SIngo Molnar collapse__resort(); 48862daacb5SArnaldo Carvalho de Melo output__resort(event__total[0]); 48986470930SIngo Molnar 49086470930SIngo Molnar find_annotations(); 49194c744b6SArnaldo Carvalho de Melo out_delete: 49294c744b6SArnaldo Carvalho de Melo perf_session__delete(session); 49386470930SIngo Molnar 494bab81b62SLi Zefan return ret; 49586470930SIngo Molnar } 49686470930SIngo Molnar 49786470930SIngo Molnar static const char * const annotate_usage[] = { 49886470930SIngo Molnar "perf annotate [<options>] <command>", 49986470930SIngo Molnar NULL 50086470930SIngo Molnar }; 50186470930SIngo Molnar 50286470930SIngo Molnar static const struct option options[] = { 50386470930SIngo Molnar OPT_STRING('i', "input", &input_name, "file", 50486470930SIngo Molnar "input file name"), 50523b87116SIngo Molnar OPT_STRING('s', "symbol", &sym_hist_filter, "symbol", 50686470930SIngo Molnar "symbol to annotate"), 507fa6963b2SPeter Zijlstra OPT_BOOLEAN('f', "force", &force, "don't complain, do it"), 50886470930SIngo Molnar OPT_BOOLEAN('v', "verbose", &verbose, 50986470930SIngo Molnar "be more verbose (show symbol address, etc)"), 51086470930SIngo Molnar OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, 51186470930SIngo Molnar "dump raw trace in ASCII"), 512b32d133aSArnaldo Carvalho de Melo OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name, 513b32d133aSArnaldo Carvalho de Melo "file", "vmlinux pathname"), 514b32d133aSArnaldo Carvalho de Melo OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules, 51542976487SMike Galbraith "load module symbols - WARNING: use only with -k and LIVE kernel"), 516301406b9SFrederic Weisbecker OPT_BOOLEAN('l', "print-line", &print_line, 517301406b9SFrederic Weisbecker "print matching source lines (may be slow)"), 51842976487SMike Galbraith OPT_BOOLEAN('P', "full-paths", &full_paths, 51942976487SMike Galbraith "Don't shorten the displayed pathnames"), 52086470930SIngo Molnar OPT_END() 52186470930SIngo Molnar }; 52286470930SIngo Molnar 52386470930SIngo Molnar static void setup_sorting(void) 52486470930SIngo Molnar { 52586470930SIngo Molnar char *tmp, *tok, *str = strdup(sort_order); 52686470930SIngo Molnar 52786470930SIngo Molnar for (tok = strtok_r(str, ", ", &tmp); 52886470930SIngo Molnar tok; tok = strtok_r(NULL, ", ", &tmp)) { 52986470930SIngo Molnar if (sort_dimension__add(tok) < 0) { 53086470930SIngo Molnar error("Unknown --sort key: `%s'", tok); 53186470930SIngo Molnar usage_with_options(annotate_usage, options); 53286470930SIngo Molnar } 53386470930SIngo Molnar } 53486470930SIngo Molnar 53586470930SIngo Molnar free(str); 53686470930SIngo Molnar } 53786470930SIngo Molnar 538f37a291cSIngo Molnar int cmd_annotate(int argc, const char **argv, const char *prefix __used) 53986470930SIngo Molnar { 540b32d133aSArnaldo Carvalho de Melo if (symbol__init(&symbol_conf) < 0) 541b32d133aSArnaldo Carvalho de Melo return -1; 54286470930SIngo Molnar 54386470930SIngo Molnar argc = parse_options(argc, argv, options, annotate_usage, 0); 54486470930SIngo Molnar 54586470930SIngo Molnar setup_sorting(); 54686470930SIngo Molnar 54786470930SIngo Molnar if (argc) { 54886470930SIngo Molnar /* 54986470930SIngo Molnar * Special case: if there's an argument left then assume tha 55086470930SIngo Molnar * it's a symbol filter: 55186470930SIngo Molnar */ 55286470930SIngo Molnar if (argc > 1) 55386470930SIngo Molnar usage_with_options(annotate_usage, options); 55486470930SIngo Molnar 55586470930SIngo Molnar sym_hist_filter = argv[0]; 55686470930SIngo Molnar } 55786470930SIngo Molnar 55886470930SIngo Molnar setup_pager(); 55986470930SIngo Molnar 560dd68ada2SJohn Kacur if (field_sep && *field_sep == '.') { 561dd68ada2SJohn Kacur fputs("'.' is the only non valid --field-separator argument\n", 562dd68ada2SJohn Kacur stderr); 563dd68ada2SJohn Kacur exit(129); 564dd68ada2SJohn Kacur } 565dd68ada2SJohn Kacur 56686470930SIngo Molnar return __cmd_annotate(); 56786470930SIngo Molnar } 568