1 /* 2 * builtin-annotate.c 3 * 4 * Builtin annotate command: Analyze the perf.data input file, 5 * look up and read DSOs and symbol information and display 6 * a histogram of results, along various sorting keys. 7 */ 8 #include "builtin.h" 9 10 #include "util/util.h" 11 #include "util/color.h" 12 /* ANDROID_CHANGE_BEGIN */ 13 #if 0 14 #include <linux/list.h> 15 #include "util/cache.h" 16 #include <linux/rbtree.h> 17 #else 18 #include "util/include/linux/list.h" 19 #include "util/cache.h" 20 #include "util/include/linux/rbtree.h" 21 #endif 22 /* ANDROID_CHANGE_END */ 23 #include "util/symbol.h" 24 25 #include "perf.h" 26 #include "util/debug.h" 27 28 #include "util/evlist.h" 29 #include "util/evsel.h" 30 #include "util/annotate.h" 31 #include "util/event.h" 32 #include "util/parse-options.h" 33 #include "util/parse-events.h" 34 #include "util/thread.h" 35 #include "util/sort.h" 36 #include "util/hist.h" 37 #include "util/session.h" 38 39 static char const *input_name = "perf.data"; 40 41 static bool force, use_tui, use_stdio; 42 43 static bool full_paths; 44 45 static bool print_line; 46 47 static const char *sym_hist_filter; 48 49 static int perf_evlist__add_sample(struct perf_evlist *evlist, 50 struct perf_sample *sample, 51 struct perf_evsel *evsel, 52 struct addr_location *al) 53 { 54 struct hist_entry *he; 55 int ret; 56 57 if (sym_hist_filter != NULL && 58 (al->sym == NULL || strcmp(sym_hist_filter, al->sym->name) != 0)) { 59 /* We're only interested in a symbol named sym_hist_filter */ 60 if (al->sym != NULL) { 61 rb_erase(&al->sym->rb_node, 62 &al->map->dso->symbols[al->map->type]); 63 symbol__delete(al->sym); 64 } 65 return 0; 66 } 67 68 he = __hists__add_entry(&evsel->hists, al, NULL, 1); 69 if (he == NULL) 70 return -ENOMEM; 71 72 ret = 0; 73 if (he->ms.sym != NULL) { 74 struct annotation *notes = symbol__annotation(he->ms.sym); 75 if (notes->src == NULL && 76 symbol__alloc_hist(he->ms.sym, evlist->nr_entries) < 0) 77 return -ENOMEM; 78 79 ret = hist_entry__inc_addr_samples(he, evsel->idx, al->addr); 80 } 81 82 evsel->hists.stats.total_period += sample->period; 83 hists__inc_nr_events(&evsel->hists, PERF_RECORD_SAMPLE); 84 return ret; 85 } 86 87 static int process_sample_event(union perf_event *event, 88 struct perf_sample *sample, 89 struct perf_evsel *evsel, 90 struct perf_session *session) 91 { 92 struct addr_location al; 93 94 if (perf_event__preprocess_sample(event, session, &al, sample, 95 symbol__annotate_init) < 0) { 96 pr_warning("problem processing %d event, skipping it.\n", 97 event->header.type); 98 return -1; 99 } 100 101 if (!al.filtered && 102 perf_evlist__add_sample(session->evlist, sample, evsel, &al)) { 103 pr_warning("problem incrementing symbol count, " 104 "skipping event\n"); 105 return -1; 106 } 107 108 return 0; 109 } 110 111 static int hist_entry__tty_annotate(struct hist_entry *he, int evidx) 112 { 113 return symbol__tty_annotate(he->ms.sym, he->ms.map, evidx, 114 print_line, full_paths, 0, 0); 115 } 116 117 static void hists__find_annotations(struct hists *self, int evidx) 118 { 119 struct rb_node *nd = rb_first(&self->entries), *next; 120 int key = KEY_RIGHT; 121 122 while (nd) { 123 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node); 124 struct annotation *notes; 125 126 if (he->ms.sym == NULL || he->ms.map->dso->annotate_warned) 127 goto find_next; 128 129 notes = symbol__annotation(he->ms.sym); 130 if (notes->src == NULL) { 131 find_next: 132 if (key == KEY_LEFT) 133 nd = rb_prev(nd); 134 else 135 nd = rb_next(nd); 136 continue; 137 } 138 139 if (use_browser > 0) { 140 key = hist_entry__tui_annotate(he, evidx); 141 switch (key) { 142 case KEY_RIGHT: 143 next = rb_next(nd); 144 break; 145 case KEY_LEFT: 146 next = rb_prev(nd); 147 break; 148 default: 149 return; 150 } 151 152 if (next != NULL) 153 nd = next; 154 } else { 155 hist_entry__tty_annotate(he, evidx); 156 nd = rb_next(nd); 157 /* 158 * Since we have a hist_entry per IP for the same 159 * symbol, free he->ms.sym->src to signal we already 160 * processed this symbol. 161 */ 162 free(notes->src); 163 notes->src = NULL; 164 } 165 } 166 } 167 168 static struct perf_event_ops event_ops = { 169 .sample = process_sample_event, 170 .mmap = perf_event__process_mmap, 171 .comm = perf_event__process_comm, 172 .fork = perf_event__process_task, 173 .ordered_samples = true, 174 .ordering_requires_timestamps = true, 175 }; 176 177 static int __cmd_annotate(void) 178 { 179 int ret; 180 struct perf_session *session; 181 struct perf_evsel *pos; 182 u64 total_nr_samples; 183 184 session = perf_session__new(input_name, O_RDONLY, force, false, &event_ops); 185 if (session == NULL) 186 return -ENOMEM; 187 188 ret = perf_session__process_events(session, &event_ops); 189 if (ret) 190 goto out_delete; 191 192 if (dump_trace) { 193 perf_session__fprintf_nr_events(session, stdout); 194 goto out_delete; 195 } 196 197 if (verbose > 3) 198 perf_session__fprintf(session, stdout); 199 200 if (verbose > 2) 201 perf_session__fprintf_dsos(session, stdout); 202 203 total_nr_samples = 0; 204 list_for_each_entry(pos, &session->evlist->entries, node) { 205 struct hists *hists = &pos->hists; 206 u32 nr_samples = hists->stats.nr_events[PERF_RECORD_SAMPLE]; 207 208 if (nr_samples > 0) { 209 total_nr_samples += nr_samples; 210 hists__collapse_resort(hists); 211 hists__output_resort(hists); 212 hists__find_annotations(hists, pos->idx); 213 } 214 } 215 216 if (total_nr_samples == 0) { 217 ui__warning("The %s file has no samples!\n", input_name); 218 goto out_delete; 219 } 220 out_delete: 221 /* 222 * Speed up the exit process, for large files this can 223 * take quite a while. 224 * 225 * XXX Enable this when using valgrind or if we ever 226 * librarize this command. 227 * 228 * Also experiment with obstacks to see how much speed 229 * up we'll get here. 230 * 231 * perf_session__delete(session); 232 */ 233 return ret; 234 } 235 236 static const char * const annotate_usage[] = { 237 "perf annotate [<options>] <command>", 238 NULL 239 }; 240 241 static const struct option options[] = { 242 OPT_STRING('i', "input", &input_name, "file", 243 "input file name"), 244 OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]", 245 "only consider symbols in these dsos"), 246 OPT_STRING('s', "symbol", &sym_hist_filter, "symbol", 247 "symbol to annotate"), 248 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"), 249 OPT_INCR('v', "verbose", &verbose, 250 "be more verbose (show symbol address, etc)"), 251 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, 252 "dump raw trace in ASCII"), 253 OPT_BOOLEAN(0, "tui", &use_tui, "Use the TUI interface"), 254 OPT_BOOLEAN(0, "stdio", &use_stdio, "Use the stdio interface"), 255 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name, 256 "file", "vmlinux pathname"), 257 OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules, 258 "load module symbols - WARNING: use only with -k and LIVE kernel"), 259 OPT_BOOLEAN('l', "print-line", &print_line, 260 "print matching source lines (may be slow)"), 261 OPT_BOOLEAN('P', "full-paths", &full_paths, 262 "Don't shorten the displayed pathnames"), 263 /* ANDROID_CHANGE_BEGIN */ 264 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory", 265 "Look for files with symbols relative to this directory"), 266 /* ANDROID_CHANGE_END */ 267 OPT_END() 268 }; 269 270 int cmd_annotate(int argc, const char **argv, const char *prefix __used) 271 { 272 argc = parse_options(argc, argv, options, annotate_usage, 0); 273 274 if (use_stdio) 275 use_browser = 0; 276 else if (use_tui) 277 use_browser = 1; 278 279 setup_browser(true); 280 281 symbol_conf.priv_size = sizeof(struct annotation); 282 symbol_conf.try_vmlinux_path = true; 283 284 if (symbol__init() < 0) 285 return -1; 286 287 setup_sorting(annotate_usage, options); 288 289 if (argc) { 290 /* 291 * Special case: if there's an argument left then assume tha 292 * it's a symbol filter: 293 */ 294 if (argc > 1) 295 usage_with_options(annotate_usage, options); 296 297 sym_hist_filter = argv[0]; 298 } 299 300 if (field_sep && *field_sep == '.') { 301 pr_err("'.' is the only non valid --field-separator argument\n"); 302 return -1; 303 } 304 305 return __cmd_annotate(); 306 } 307