1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * builtin-annotate.c 4 * 5 * Builtin annotate command: Analyze the perf.data input file, 6 * look up and read DSOs and symbol information and display 7 * a histogram of results, along various sorting keys. 8 */ 9 #include "builtin.h" 10 11 #include "util/util.h" 12 #include "util/color.h" 13 #include <linux/list.h> 14 #include "util/cache.h" 15 #include <linux/rbtree.h> 16 #include "util/symbol.h" 17 18 #include "perf.h" 19 #include "util/debug.h" 20 21 #include "util/evlist.h" 22 #include "util/evsel.h" 23 #include "util/annotate.h" 24 #include "util/event.h" 25 #include <subcmd/parse-options.h> 26 #include "util/parse-events.h" 27 #include "util/thread.h" 28 #include "util/sort.h" 29 #include "util/hist.h" 30 #include "util/session.h" 31 #include "util/tool.h" 32 #include "util/data.h" 33 #include "arch/common.h" 34 #include "util/block-range.h" 35 36 #include <dlfcn.h> 37 #include <errno.h> 38 #include <linux/bitmap.h> 39 40 struct perf_annotate { 41 struct perf_tool tool; 42 struct perf_session *session; 43 bool use_tui, use_stdio, use_gtk; 44 bool full_paths; 45 bool print_line; 46 bool skip_missing; 47 bool has_br_stack; 48 const char *sym_hist_filter; 49 const char *cpu_list; 50 DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS); 51 }; 52 53 /* 54 * Given one basic block: 55 * 56 * from to branch_i 57 * * ----> * 58 * | 59 * | block 60 * v 61 * * ----> * 62 * from to branch_i+1 63 * 64 * where the horizontal are the branches and the vertical is the executed 65 * block of instructions. 66 * 67 * We count, for each 'instruction', the number of blocks that covered it as 68 * well as count the ratio each branch is taken. 69 * 70 * We can do this without knowing the actual instruction stream by keeping 71 * track of the address ranges. We break down ranges such that there is no 72 * overlap and iterate from the start until the end. 73 * 74 * @acme: once we parse the objdump output _before_ processing the samples, 75 * we can easily fold the branch.cycles IPC bits in. 76 */ 77 static void process_basic_block(struct addr_map_symbol *start, 78 struct addr_map_symbol *end, 79 struct branch_flags *flags) 80 { 81 struct symbol *sym = start->sym; 82 struct annotation *notes = sym ? symbol__annotation(sym) : NULL; 83 struct block_range_iter iter; 84 struct block_range *entry; 85 86 /* 87 * Sanity; NULL isn't executable and the CPU cannot execute backwards 88 */ 89 if (!start->addr || start->addr > end->addr) 90 return; 91 92 iter = block_range__create(start->addr, end->addr); 93 if (!block_range_iter__valid(&iter)) 94 return; 95 96 /* 97 * First block in range is a branch target. 98 */ 99 entry = block_range_iter(&iter); 100 assert(entry->is_target); 101 entry->entry++; 102 103 do { 104 entry = block_range_iter(&iter); 105 106 entry->coverage++; 107 entry->sym = sym; 108 109 if (notes) 110 notes->max_coverage = max(notes->max_coverage, entry->coverage); 111 112 } while (block_range_iter__next(&iter)); 113 114 /* 115 * Last block in rage is a branch. 116 */ 117 entry = block_range_iter(&iter); 118 assert(entry->is_branch); 119 entry->taken++; 120 if (flags->predicted) 121 entry->pred++; 122 } 123 124 static void process_branch_stack(struct branch_stack *bs, struct addr_location *al, 125 struct perf_sample *sample) 126 { 127 struct addr_map_symbol *prev = NULL; 128 struct branch_info *bi; 129 int i; 130 131 if (!bs || !bs->nr) 132 return; 133 134 bi = sample__resolve_bstack(sample, al); 135 if (!bi) 136 return; 137 138 for (i = bs->nr - 1; i >= 0; i--) { 139 /* 140 * XXX filter against symbol 141 */ 142 if (prev) 143 process_basic_block(prev, &bi[i].from, &bi[i].flags); 144 prev = &bi[i].to; 145 } 146 147 free(bi); 148 } 149 150 static int hist_iter__branch_callback(struct hist_entry_iter *iter, 151 struct addr_location *al __maybe_unused, 152 bool single __maybe_unused, 153 void *arg __maybe_unused) 154 { 155 struct hist_entry *he = iter->he; 156 struct branch_info *bi; 157 struct perf_sample *sample = iter->sample; 158 struct perf_evsel *evsel = iter->evsel; 159 int err; 160 161 hist__account_cycles(sample->branch_stack, al, sample, false); 162 163 bi = he->branch_info; 164 err = addr_map_symbol__inc_samples(&bi->from, sample, evsel->idx); 165 166 if (err) 167 goto out; 168 169 err = addr_map_symbol__inc_samples(&bi->to, sample, evsel->idx); 170 171 out: 172 return err; 173 } 174 175 static int process_branch_callback(struct perf_evsel *evsel, 176 struct perf_sample *sample, 177 struct addr_location *al __maybe_unused, 178 struct perf_annotate *ann, 179 struct machine *machine) 180 { 181 struct hist_entry_iter iter = { 182 .evsel = evsel, 183 .sample = sample, 184 .add_entry_cb = hist_iter__branch_callback, 185 .hide_unresolved = symbol_conf.hide_unresolved, 186 .ops = &hist_iter_branch, 187 }; 188 189 struct addr_location a; 190 int ret; 191 192 if (machine__resolve(machine, &a, sample) < 0) 193 return -1; 194 195 if (a.sym == NULL) 196 return 0; 197 198 if (a.map != NULL) 199 a.map->dso->hit = 1; 200 201 ret = hist_entry_iter__add(&iter, &a, PERF_MAX_STACK_DEPTH, ann); 202 return ret; 203 } 204 205 static int perf_evsel__add_sample(struct perf_evsel *evsel, 206 struct perf_sample *sample, 207 struct addr_location *al, 208 struct perf_annotate *ann, 209 struct machine *machine) 210 { 211 struct hists *hists = evsel__hists(evsel); 212 struct hist_entry *he; 213 int ret; 214 215 if ((!ann->has_br_stack || !ui__has_annotation()) && 216 ann->sym_hist_filter != NULL && 217 (al->sym == NULL || 218 strcmp(ann->sym_hist_filter, al->sym->name) != 0)) { 219 /* We're only interested in a symbol named sym_hist_filter */ 220 /* 221 * FIXME: why isn't this done in the symbol_filter when loading 222 * the DSO? 223 */ 224 if (al->sym != NULL) { 225 rb_erase(&al->sym->rb_node, 226 &al->map->dso->symbols[al->map->type]); 227 symbol__delete(al->sym); 228 dso__reset_find_symbol_cache(al->map->dso); 229 } 230 return 0; 231 } 232 233 /* 234 * XXX filtered samples can still have branch entires pointing into our 235 * symbol and are missed. 236 */ 237 process_branch_stack(sample->branch_stack, al, sample); 238 239 if (ann->has_br_stack && ui__has_annotation()) 240 return process_branch_callback(evsel, sample, al, ann, machine); 241 242 he = hists__add_entry(hists, al, NULL, NULL, NULL, sample, true); 243 if (he == NULL) 244 return -ENOMEM; 245 246 ret = hist_entry__inc_addr_samples(he, sample, evsel->idx, al->addr); 247 hists__inc_nr_samples(hists, true); 248 return ret; 249 } 250 251 static int process_sample_event(struct perf_tool *tool, 252 union perf_event *event, 253 struct perf_sample *sample, 254 struct perf_evsel *evsel, 255 struct machine *machine) 256 { 257 struct perf_annotate *ann = container_of(tool, struct perf_annotate, tool); 258 struct addr_location al; 259 int ret = 0; 260 261 if (machine__resolve(machine, &al, sample) < 0) { 262 pr_warning("problem processing %d event, skipping it.\n", 263 event->header.type); 264 return -1; 265 } 266 267 if (ann->cpu_list && !test_bit(sample->cpu, ann->cpu_bitmap)) 268 goto out_put; 269 270 if (!al.filtered && 271 perf_evsel__add_sample(evsel, sample, &al, ann, machine)) { 272 pr_warning("problem incrementing symbol count, " 273 "skipping event\n"); 274 ret = -1; 275 } 276 out_put: 277 addr_location__put(&al); 278 return ret; 279 } 280 281 static int hist_entry__tty_annotate(struct hist_entry *he, 282 struct perf_evsel *evsel, 283 struct perf_annotate *ann) 284 { 285 return symbol__tty_annotate(he->ms.sym, he->ms.map, evsel, 286 ann->print_line, ann->full_paths, 0, 0); 287 } 288 289 static void hists__find_annotations(struct hists *hists, 290 struct perf_evsel *evsel, 291 struct perf_annotate *ann) 292 { 293 struct rb_node *nd = rb_first(&hists->entries), *next; 294 int key = K_RIGHT; 295 296 while (nd) { 297 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node); 298 struct annotation *notes; 299 300 if (he->ms.sym == NULL || he->ms.map->dso->annotate_warned) 301 goto find_next; 302 303 if (ann->sym_hist_filter && 304 (strcmp(he->ms.sym->name, ann->sym_hist_filter) != 0)) 305 goto find_next; 306 307 notes = symbol__annotation(he->ms.sym); 308 if (notes->src == NULL) { 309 find_next: 310 if (key == K_LEFT) 311 nd = rb_prev(nd); 312 else 313 nd = rb_next(nd); 314 continue; 315 } 316 317 if (use_browser == 2) { 318 int ret; 319 int (*annotate)(struct hist_entry *he, 320 struct perf_evsel *evsel, 321 struct hist_browser_timer *hbt); 322 323 annotate = dlsym(perf_gtk_handle, 324 "hist_entry__gtk_annotate"); 325 if (annotate == NULL) { 326 ui__error("GTK browser not found!\n"); 327 return; 328 } 329 330 ret = annotate(he, evsel, NULL); 331 if (!ret || !ann->skip_missing) 332 return; 333 334 /* skip missing symbols */ 335 nd = rb_next(nd); 336 } else if (use_browser == 1) { 337 key = hist_entry__tui_annotate(he, evsel, NULL); 338 339 switch (key) { 340 case -1: 341 if (!ann->skip_missing) 342 return; 343 /* fall through */ 344 case K_RIGHT: 345 next = rb_next(nd); 346 break; 347 case K_LEFT: 348 next = rb_prev(nd); 349 break; 350 default: 351 return; 352 } 353 354 if (next != NULL) 355 nd = next; 356 } else { 357 hist_entry__tty_annotate(he, evsel, ann); 358 nd = rb_next(nd); 359 /* 360 * Since we have a hist_entry per IP for the same 361 * symbol, free he->ms.sym->src to signal we already 362 * processed this symbol. 363 */ 364 zfree(¬es->src->cycles_hist); 365 zfree(¬es->src); 366 } 367 } 368 } 369 370 static int __cmd_annotate(struct perf_annotate *ann) 371 { 372 int ret; 373 struct perf_session *session = ann->session; 374 struct perf_evsel *pos; 375 u64 total_nr_samples; 376 377 if (ann->cpu_list) { 378 ret = perf_session__cpu_bitmap(session, ann->cpu_list, 379 ann->cpu_bitmap); 380 if (ret) 381 goto out; 382 } 383 384 if (!objdump_path) { 385 ret = perf_env__lookup_objdump(&session->header.env); 386 if (ret) 387 goto out; 388 } 389 390 ret = perf_session__process_events(session); 391 if (ret) 392 goto out; 393 394 if (dump_trace) { 395 perf_session__fprintf_nr_events(session, stdout); 396 perf_evlist__fprintf_nr_events(session->evlist, stdout); 397 goto out; 398 } 399 400 if (verbose > 3) 401 perf_session__fprintf(session, stdout); 402 403 if (verbose > 2) 404 perf_session__fprintf_dsos(session, stdout); 405 406 total_nr_samples = 0; 407 evlist__for_each_entry(session->evlist, pos) { 408 struct hists *hists = evsel__hists(pos); 409 u32 nr_samples = hists->stats.nr_events[PERF_RECORD_SAMPLE]; 410 411 if (nr_samples > 0) { 412 total_nr_samples += nr_samples; 413 hists__collapse_resort(hists, NULL); 414 /* Don't sort callchain */ 415 perf_evsel__reset_sample_bit(pos, CALLCHAIN); 416 perf_evsel__output_resort(pos, NULL); 417 418 if (symbol_conf.event_group && 419 !perf_evsel__is_group_leader(pos)) 420 continue; 421 422 hists__find_annotations(hists, pos, ann); 423 } 424 } 425 426 if (total_nr_samples == 0) { 427 ui__error("The %s file has no samples!\n", session->data->file.path); 428 goto out; 429 } 430 431 if (use_browser == 2) { 432 void (*show_annotations)(void); 433 434 show_annotations = dlsym(perf_gtk_handle, 435 "perf_gtk__show_annotations"); 436 if (show_annotations == NULL) { 437 ui__error("GTK browser not found!\n"); 438 goto out; 439 } 440 show_annotations(); 441 } 442 443 out: 444 return ret; 445 } 446 447 static const char * const annotate_usage[] = { 448 "perf annotate [<options>]", 449 NULL 450 }; 451 452 int cmd_annotate(int argc, const char **argv) 453 { 454 struct perf_annotate annotate = { 455 .tool = { 456 .sample = process_sample_event, 457 .mmap = perf_event__process_mmap, 458 .mmap2 = perf_event__process_mmap2, 459 .comm = perf_event__process_comm, 460 .exit = perf_event__process_exit, 461 .fork = perf_event__process_fork, 462 .namespaces = perf_event__process_namespaces, 463 .attr = perf_event__process_attr, 464 .build_id = perf_event__process_build_id, 465 .tracing_data = perf_event__process_tracing_data, 466 .feature = perf_event__process_feature, 467 .ordered_events = true, 468 .ordering_requires_timestamps = true, 469 }, 470 }; 471 struct perf_data data = { 472 .mode = PERF_DATA_MODE_READ, 473 }; 474 struct option options[] = { 475 OPT_STRING('i', "input", &input_name, "file", 476 "input file name"), 477 OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]", 478 "only consider symbols in these dsos"), 479 OPT_STRING('s', "symbol", &annotate.sym_hist_filter, "symbol", 480 "symbol to annotate"), 481 OPT_BOOLEAN('f', "force", &data.force, "don't complain, do it"), 482 OPT_INCR('v', "verbose", &verbose, 483 "be more verbose (show symbol address, etc)"), 484 OPT_BOOLEAN('q', "quiet", &quiet, "do now show any message"), 485 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, 486 "dump raw trace in ASCII"), 487 OPT_BOOLEAN(0, "gtk", &annotate.use_gtk, "Use the GTK interface"), 488 OPT_BOOLEAN(0, "tui", &annotate.use_tui, "Use the TUI interface"), 489 OPT_BOOLEAN(0, "stdio", &annotate.use_stdio, "Use the stdio interface"), 490 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name, 491 "file", "vmlinux pathname"), 492 OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules, 493 "load module symbols - WARNING: use only with -k and LIVE kernel"), 494 OPT_BOOLEAN('l', "print-line", &annotate.print_line, 495 "print matching source lines (may be slow)"), 496 OPT_BOOLEAN('P', "full-paths", &annotate.full_paths, 497 "Don't shorten the displayed pathnames"), 498 OPT_BOOLEAN(0, "skip-missing", &annotate.skip_missing, 499 "Skip symbols that cannot be annotated"), 500 OPT_STRING('C', "cpu", &annotate.cpu_list, "cpu", "list of cpus to profile"), 501 OPT_CALLBACK(0, "symfs", NULL, "directory", 502 "Look for files with symbols relative to this directory", 503 symbol__config_symfs), 504 OPT_BOOLEAN(0, "source", &symbol_conf.annotate_src, 505 "Interleave source code with assembly code (default)"), 506 OPT_BOOLEAN(0, "asm-raw", &symbol_conf.annotate_asm_raw, 507 "Display raw encoding of assembly instructions (default)"), 508 OPT_STRING('M', "disassembler-style", &disassembler_style, "disassembler style", 509 "Specify disassembler style (e.g. -M intel for intel syntax)"), 510 OPT_STRING(0, "objdump", &objdump_path, "path", 511 "objdump binary to use for disassembly and annotations"), 512 OPT_BOOLEAN(0, "group", &symbol_conf.event_group, 513 "Show event group information together"), 514 OPT_BOOLEAN(0, "show-total-period", &symbol_conf.show_total_period, 515 "Show a column with the sum of periods"), 516 OPT_BOOLEAN('n', "show-nr-samples", &symbol_conf.show_nr_samples, 517 "Show a column with the number of samples"), 518 OPT_CALLBACK_DEFAULT(0, "stdio-color", NULL, "mode", 519 "'always' (default), 'never' or 'auto' only applicable to --stdio mode", 520 stdio__config_color, "always"), 521 OPT_END() 522 }; 523 int ret; 524 525 set_option_flag(options, 0, "show-total-period", PARSE_OPT_EXCLUSIVE); 526 set_option_flag(options, 0, "show-nr-samples", PARSE_OPT_EXCLUSIVE); 527 528 529 ret = hists__init(); 530 if (ret < 0) 531 return ret; 532 533 argc = parse_options(argc, argv, options, annotate_usage, 0); 534 if (argc) { 535 /* 536 * Special case: if there's an argument left then assume that 537 * it's a symbol filter: 538 */ 539 if (argc > 1) 540 usage_with_options(annotate_usage, options); 541 542 annotate.sym_hist_filter = argv[0]; 543 } 544 545 if (symbol_conf.show_nr_samples && annotate.use_gtk) { 546 pr_err("--show-nr-samples is not available in --gtk mode at this time\n"); 547 return ret; 548 } 549 550 if (quiet) 551 perf_quiet_option(); 552 553 data.file.path = input_name; 554 555 annotate.session = perf_session__new(&data, false, &annotate.tool); 556 if (annotate.session == NULL) 557 return -1; 558 559 annotate.has_br_stack = perf_header__has_feat(&annotate.session->header, 560 HEADER_BRANCH_STACK); 561 562 ret = symbol__annotation_init(); 563 if (ret < 0) 564 goto out_delete; 565 566 symbol_conf.try_vmlinux_path = true; 567 568 ret = symbol__init(&annotate.session->header.env); 569 if (ret < 0) 570 goto out_delete; 571 572 if (annotate.use_stdio) 573 use_browser = 0; 574 else if (annotate.use_tui) 575 use_browser = 1; 576 else if (annotate.use_gtk) 577 use_browser = 2; 578 579 setup_browser(true); 580 581 if (use_browser == 1 && annotate.has_br_stack) { 582 sort__mode = SORT_MODE__BRANCH; 583 if (setup_sorting(annotate.session->evlist) < 0) 584 usage_with_options(annotate_usage, options); 585 } else { 586 if (setup_sorting(NULL) < 0) 587 usage_with_options(annotate_usage, options); 588 } 589 590 ret = __cmd_annotate(&annotate); 591 592 out_delete: 593 /* 594 * Speed up the exit process, for large files this can 595 * take quite a while. 596 * 597 * XXX Enable this when using valgrind or if we ever 598 * librarize this command. 599 * 600 * Also experiment with obstacks to see how much speed 601 * up we'll get here. 602 * 603 * perf_session__delete(session); 604 */ 605 return ret; 606 } 607