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/color.h" 12 #include <linux/list.h> 13 #include "util/cache.h" 14 #include <linux/rbtree.h> 15 #include <linux/zalloc.h> 16 #include "util/symbol.h" 17 18 #include "util/debug.h" 19 20 #include "util/evlist.h" 21 #include "util/evsel.h" 22 #include "util/annotate.h" 23 #include "util/event.h" 24 #include <subcmd/parse-options.h> 25 #include "util/parse-events.h" 26 #include "util/sort.h" 27 #include "util/hist.h" 28 #include "util/dso.h" 29 #include "util/machine.h" 30 #include "util/map.h" 31 #include "util/session.h" 32 #include "util/tool.h" 33 #include "util/data.h" 34 #include "arch/common.h" 35 #include "util/block-range.h" 36 #include "util/map_symbol.h" 37 #include "util/branch.h" 38 #include "util/util.h" 39 40 #include <dlfcn.h> 41 #include <errno.h> 42 #include <linux/bitmap.h> 43 #include <linux/err.h> 44 45 struct perf_annotate { 46 struct perf_tool tool; 47 struct perf_session *session; 48 #ifdef HAVE_SLANG_SUPPORT 49 bool use_tui; 50 #endif 51 bool use_stdio, use_stdio2; 52 #ifdef HAVE_GTK2_SUPPORT 53 bool use_gtk; 54 #endif 55 bool skip_missing; 56 bool has_br_stack; 57 bool group_set; 58 float min_percent; 59 const char *sym_hist_filter; 60 const char *cpu_list; 61 DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS); 62 }; 63 64 /* 65 * Given one basic block: 66 * 67 * from to branch_i 68 * * ----> * 69 * | 70 * | block 71 * v 72 * * ----> * 73 * from to branch_i+1 74 * 75 * where the horizontal are the branches and the vertical is the executed 76 * block of instructions. 77 * 78 * We count, for each 'instruction', the number of blocks that covered it as 79 * well as count the ratio each branch is taken. 80 * 81 * We can do this without knowing the actual instruction stream by keeping 82 * track of the address ranges. We break down ranges such that there is no 83 * overlap and iterate from the start until the end. 84 * 85 * @acme: once we parse the objdump output _before_ processing the samples, 86 * we can easily fold the branch.cycles IPC bits in. 87 */ 88 static void process_basic_block(struct addr_map_symbol *start, 89 struct addr_map_symbol *end, 90 struct branch_flags *flags) 91 { 92 struct symbol *sym = start->ms.sym; 93 struct annotation *notes = sym ? symbol__annotation(sym) : NULL; 94 struct block_range_iter iter; 95 struct block_range *entry; 96 97 /* 98 * Sanity; NULL isn't executable and the CPU cannot execute backwards 99 */ 100 if (!start->addr || start->addr > end->addr) 101 return; 102 103 iter = block_range__create(start->addr, end->addr); 104 if (!block_range_iter__valid(&iter)) 105 return; 106 107 /* 108 * First block in range is a branch target. 109 */ 110 entry = block_range_iter(&iter); 111 assert(entry->is_target); 112 entry->entry++; 113 114 do { 115 entry = block_range_iter(&iter); 116 117 entry->coverage++; 118 entry->sym = sym; 119 120 if (notes) 121 notes->max_coverage = max(notes->max_coverage, entry->coverage); 122 123 } while (block_range_iter__next(&iter)); 124 125 /* 126 * Last block in rage is a branch. 127 */ 128 entry = block_range_iter(&iter); 129 assert(entry->is_branch); 130 entry->taken++; 131 if (flags->predicted) 132 entry->pred++; 133 } 134 135 static void process_branch_stack(struct branch_stack *bs, struct addr_location *al, 136 struct perf_sample *sample) 137 { 138 struct addr_map_symbol *prev = NULL; 139 struct branch_info *bi; 140 int i; 141 142 if (!bs || !bs->nr) 143 return; 144 145 bi = sample__resolve_bstack(sample, al); 146 if (!bi) 147 return; 148 149 for (i = bs->nr - 1; i >= 0; i--) { 150 /* 151 * XXX filter against symbol 152 */ 153 if (prev) 154 process_basic_block(prev, &bi[i].from, &bi[i].flags); 155 prev = &bi[i].to; 156 } 157 158 free(bi); 159 } 160 161 static int hist_iter__branch_callback(struct hist_entry_iter *iter, 162 struct addr_location *al __maybe_unused, 163 bool single __maybe_unused, 164 void *arg __maybe_unused) 165 { 166 struct hist_entry *he = iter->he; 167 struct branch_info *bi; 168 struct perf_sample *sample = iter->sample; 169 struct evsel *evsel = iter->evsel; 170 int err; 171 172 bi = he->branch_info; 173 err = addr_map_symbol__inc_samples(&bi->from, sample, evsel); 174 175 if (err) 176 goto out; 177 178 err = addr_map_symbol__inc_samples(&bi->to, sample, evsel); 179 180 out: 181 return err; 182 } 183 184 static int process_branch_callback(struct evsel *evsel, 185 struct perf_sample *sample, 186 struct addr_location *al, 187 struct perf_annotate *ann, 188 struct machine *machine) 189 { 190 struct hist_entry_iter iter = { 191 .evsel = evsel, 192 .sample = sample, 193 .add_entry_cb = hist_iter__branch_callback, 194 .hide_unresolved = symbol_conf.hide_unresolved, 195 .ops = &hist_iter_branch, 196 }; 197 struct addr_location a; 198 int ret; 199 200 addr_location__init(&a); 201 if (machine__resolve(machine, &a, sample) < 0) { 202 ret = -1; 203 goto out; 204 } 205 206 if (a.sym == NULL) { 207 ret = 0; 208 goto out; 209 } 210 211 if (a.map != NULL) 212 map__dso(a.map)->hit = 1; 213 214 hist__account_cycles(sample->branch_stack, al, sample, false, NULL); 215 216 ret = hist_entry_iter__add(&iter, &a, PERF_MAX_STACK_DEPTH, ann); 217 out: 218 addr_location__exit(&a); 219 return ret; 220 } 221 222 static bool has_annotation(struct perf_annotate *ann) 223 { 224 return ui__has_annotation() || ann->use_stdio2; 225 } 226 227 static int evsel__add_sample(struct evsel *evsel, struct perf_sample *sample, 228 struct addr_location *al, struct perf_annotate *ann, 229 struct machine *machine) 230 { 231 struct hists *hists = evsel__hists(evsel); 232 struct hist_entry *he; 233 int ret; 234 235 if ((!ann->has_br_stack || !has_annotation(ann)) && 236 ann->sym_hist_filter != NULL && 237 (al->sym == NULL || 238 strcmp(ann->sym_hist_filter, al->sym->name) != 0)) { 239 /* We're only interested in a symbol named sym_hist_filter */ 240 /* 241 * FIXME: why isn't this done in the symbol_filter when loading 242 * the DSO? 243 */ 244 if (al->sym != NULL) { 245 struct dso *dso = map__dso(al->map); 246 247 rb_erase_cached(&al->sym->rb_node, &dso->symbols); 248 symbol__delete(al->sym); 249 dso__reset_find_symbol_cache(dso); 250 } 251 return 0; 252 } 253 254 /* 255 * XXX filtered samples can still have branch entries pointing into our 256 * symbol and are missed. 257 */ 258 process_branch_stack(sample->branch_stack, al, sample); 259 260 if (ann->has_br_stack && has_annotation(ann)) 261 return process_branch_callback(evsel, sample, al, ann, machine); 262 263 he = hists__add_entry(hists, al, NULL, NULL, NULL, NULL, sample, true); 264 if (he == NULL) 265 return -ENOMEM; 266 267 ret = hist_entry__inc_addr_samples(he, sample, evsel, al->addr); 268 hists__inc_nr_samples(hists, true); 269 return ret; 270 } 271 272 static int process_sample_event(struct perf_tool *tool, 273 union perf_event *event, 274 struct perf_sample *sample, 275 struct evsel *evsel, 276 struct machine *machine) 277 { 278 struct perf_annotate *ann = container_of(tool, struct perf_annotate, tool); 279 struct addr_location al; 280 int ret = 0; 281 282 addr_location__init(&al); 283 if (machine__resolve(machine, &al, sample) < 0) { 284 pr_warning("problem processing %d event, skipping it.\n", 285 event->header.type); 286 ret = -1; 287 goto out_put; 288 } 289 290 if (ann->cpu_list && !test_bit(sample->cpu, ann->cpu_bitmap)) 291 goto out_put; 292 293 if (!al.filtered && 294 evsel__add_sample(evsel, sample, &al, ann, machine)) { 295 pr_warning("problem incrementing symbol count, " 296 "skipping event\n"); 297 ret = -1; 298 } 299 out_put: 300 addr_location__exit(&al); 301 return ret; 302 } 303 304 static int process_feature_event(struct perf_session *session, 305 union perf_event *event) 306 { 307 if (event->feat.feat_id < HEADER_LAST_FEATURE) 308 return perf_event__process_feature(session, event); 309 return 0; 310 } 311 312 static int hist_entry__tty_annotate(struct hist_entry *he, 313 struct evsel *evsel, 314 struct perf_annotate *ann) 315 { 316 if (!ann->use_stdio2) 317 return symbol__tty_annotate(&he->ms, evsel); 318 319 return symbol__tty_annotate2(&he->ms, evsel); 320 } 321 322 static void hists__find_annotations(struct hists *hists, 323 struct evsel *evsel, 324 struct perf_annotate *ann) 325 { 326 struct rb_node *nd = rb_first_cached(&hists->entries), *next; 327 int key = K_RIGHT; 328 329 while (nd) { 330 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node); 331 struct annotation *notes; 332 333 if (he->ms.sym == NULL || map__dso(he->ms.map)->annotate_warned) 334 goto find_next; 335 336 if (ann->sym_hist_filter && 337 (strcmp(he->ms.sym->name, ann->sym_hist_filter) != 0)) 338 goto find_next; 339 340 if (ann->min_percent) { 341 float percent = 0; 342 u64 total = hists__total_period(hists); 343 344 if (total) 345 percent = 100.0 * he->stat.period / total; 346 347 if (percent < ann->min_percent) 348 goto find_next; 349 } 350 351 notes = symbol__annotation(he->ms.sym); 352 if (notes->src == NULL) { 353 find_next: 354 if (key == K_LEFT || key == '<') 355 nd = rb_prev(nd); 356 else 357 nd = rb_next(nd); 358 continue; 359 } 360 361 if (use_browser == 2) { 362 int ret; 363 int (*annotate)(struct hist_entry *he, 364 struct evsel *evsel, 365 struct hist_browser_timer *hbt); 366 367 annotate = dlsym(perf_gtk_handle, 368 "hist_entry__gtk_annotate"); 369 if (annotate == NULL) { 370 ui__error("GTK browser not found!\n"); 371 return; 372 } 373 374 ret = annotate(he, evsel, NULL); 375 if (!ret || !ann->skip_missing) 376 return; 377 378 /* skip missing symbols */ 379 nd = rb_next(nd); 380 } else if (use_browser == 1) { 381 key = hist_entry__tui_annotate(he, evsel, NULL, &annotate_opts); 382 383 switch (key) { 384 case -1: 385 if (!ann->skip_missing) 386 return; 387 /* fall through */ 388 case K_RIGHT: 389 case '>': 390 next = rb_next(nd); 391 break; 392 case K_LEFT: 393 case '<': 394 next = rb_prev(nd); 395 break; 396 default: 397 return; 398 } 399 400 if (next != NULL) 401 nd = next; 402 } else { 403 hist_entry__tty_annotate(he, evsel, ann); 404 nd = rb_next(nd); 405 } 406 } 407 } 408 409 static int __cmd_annotate(struct perf_annotate *ann) 410 { 411 int ret; 412 struct perf_session *session = ann->session; 413 struct evsel *pos; 414 u64 total_nr_samples; 415 416 if (ann->cpu_list) { 417 ret = perf_session__cpu_bitmap(session, ann->cpu_list, 418 ann->cpu_bitmap); 419 if (ret) 420 goto out; 421 } 422 423 if (!annotate_opts.objdump_path) { 424 ret = perf_env__lookup_objdump(&session->header.env, 425 &annotate_opts.objdump_path); 426 if (ret) 427 goto out; 428 } 429 430 ret = perf_session__process_events(session); 431 if (ret) 432 goto out; 433 434 if (dump_trace) { 435 perf_session__fprintf_nr_events(session, stdout, false); 436 evlist__fprintf_nr_events(session->evlist, stdout, false); 437 goto out; 438 } 439 440 if (verbose > 3) 441 perf_session__fprintf(session, stdout); 442 443 if (verbose > 2) 444 perf_session__fprintf_dsos(session, stdout); 445 446 total_nr_samples = 0; 447 evlist__for_each_entry(session->evlist, pos) { 448 struct hists *hists = evsel__hists(pos); 449 u32 nr_samples = hists->stats.nr_samples; 450 451 if (nr_samples > 0) { 452 total_nr_samples += nr_samples; 453 hists__collapse_resort(hists, NULL); 454 /* Don't sort callchain */ 455 evsel__reset_sample_bit(pos, CALLCHAIN); 456 evsel__output_resort(pos, NULL); 457 458 if (symbol_conf.event_group && !evsel__is_group_leader(pos)) 459 continue; 460 461 hists__find_annotations(hists, pos, ann); 462 } 463 } 464 465 if (total_nr_samples == 0) { 466 ui__error("The %s data has no samples!\n", session->data->path); 467 goto out; 468 } 469 470 if (use_browser == 2) { 471 void (*show_annotations)(void); 472 473 show_annotations = dlsym(perf_gtk_handle, 474 "perf_gtk__show_annotations"); 475 if (show_annotations == NULL) { 476 ui__error("GTK browser not found!\n"); 477 goto out; 478 } 479 show_annotations(); 480 } 481 482 out: 483 return ret; 484 } 485 486 static int parse_percent_limit(const struct option *opt, const char *str, 487 int unset __maybe_unused) 488 { 489 struct perf_annotate *ann = opt->value; 490 double pcnt = strtof(str, NULL); 491 492 ann->min_percent = pcnt; 493 return 0; 494 } 495 496 static const char * const annotate_usage[] = { 497 "perf annotate [<options>]", 498 NULL 499 }; 500 501 int cmd_annotate(int argc, const char **argv) 502 { 503 struct perf_annotate annotate = { 504 .tool = { 505 .sample = process_sample_event, 506 .mmap = perf_event__process_mmap, 507 .mmap2 = perf_event__process_mmap2, 508 .comm = perf_event__process_comm, 509 .exit = perf_event__process_exit, 510 .fork = perf_event__process_fork, 511 .namespaces = perf_event__process_namespaces, 512 .attr = perf_event__process_attr, 513 .build_id = perf_event__process_build_id, 514 #ifdef HAVE_LIBTRACEEVENT 515 .tracing_data = perf_event__process_tracing_data, 516 #endif 517 .id_index = perf_event__process_id_index, 518 .auxtrace_info = perf_event__process_auxtrace_info, 519 .auxtrace = perf_event__process_auxtrace, 520 .feature = process_feature_event, 521 .ordered_events = true, 522 .ordering_requires_timestamps = true, 523 }, 524 }; 525 struct perf_data data = { 526 .mode = PERF_DATA_MODE_READ, 527 }; 528 struct itrace_synth_opts itrace_synth_opts = { 529 .set = 0, 530 }; 531 const char *disassembler_style = NULL, *objdump_path = NULL, *addr2line_path = NULL; 532 struct option options[] = { 533 OPT_STRING('i', "input", &input_name, "file", 534 "input file name"), 535 OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]", 536 "only consider symbols in these dsos"), 537 OPT_STRING('s', "symbol", &annotate.sym_hist_filter, "symbol", 538 "symbol to annotate"), 539 OPT_BOOLEAN('f', "force", &data.force, "don't complain, do it"), 540 OPT_INCR('v', "verbose", &verbose, 541 "be more verbose (show symbol address, etc)"), 542 OPT_BOOLEAN('q', "quiet", &quiet, "do now show any warnings or messages"), 543 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, 544 "dump raw trace in ASCII"), 545 #ifdef HAVE_GTK2_SUPPORT 546 OPT_BOOLEAN(0, "gtk", &annotate.use_gtk, "Use the GTK interface"), 547 #endif 548 #ifdef HAVE_SLANG_SUPPORT 549 OPT_BOOLEAN(0, "tui", &annotate.use_tui, "Use the TUI interface"), 550 #endif 551 OPT_BOOLEAN(0, "stdio", &annotate.use_stdio, "Use the stdio interface"), 552 OPT_BOOLEAN(0, "stdio2", &annotate.use_stdio2, "Use the stdio interface"), 553 OPT_BOOLEAN(0, "ignore-vmlinux", &symbol_conf.ignore_vmlinux, 554 "don't load vmlinux even if found"), 555 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name, 556 "file", "vmlinux pathname"), 557 OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules, 558 "load module symbols - WARNING: use only with -k and LIVE kernel"), 559 OPT_BOOLEAN('l', "print-line", &annotate_opts.print_lines, 560 "print matching source lines (may be slow)"), 561 OPT_BOOLEAN('P', "full-paths", &annotate_opts.full_path, 562 "Don't shorten the displayed pathnames"), 563 OPT_BOOLEAN(0, "skip-missing", &annotate.skip_missing, 564 "Skip symbols that cannot be annotated"), 565 OPT_BOOLEAN_SET(0, "group", &symbol_conf.event_group, 566 &annotate.group_set, 567 "Show event group information together"), 568 OPT_STRING('C', "cpu", &annotate.cpu_list, "cpu", "list of cpus to profile"), 569 OPT_CALLBACK(0, "symfs", NULL, "directory", 570 "Look for files with symbols relative to this directory", 571 symbol__config_symfs), 572 OPT_BOOLEAN(0, "source", &annotate_opts.annotate_src, 573 "Interleave source code with assembly code (default)"), 574 OPT_BOOLEAN(0, "asm-raw", &annotate_opts.show_asm_raw, 575 "Display raw encoding of assembly instructions (default)"), 576 OPT_STRING('M', "disassembler-style", &disassembler_style, "disassembler style", 577 "Specify disassembler style (e.g. -M intel for intel syntax)"), 578 OPT_STRING(0, "prefix", &annotate_opts.prefix, "prefix", 579 "Add prefix to source file path names in programs (with --prefix-strip)"), 580 OPT_STRING(0, "prefix-strip", &annotate_opts.prefix_strip, "N", 581 "Strip first N entries of source file path name in programs (with --prefix)"), 582 OPT_STRING(0, "objdump", &objdump_path, "path", 583 "objdump binary to use for disassembly and annotations"), 584 OPT_STRING(0, "addr2line", &addr2line_path, "path", 585 "addr2line binary to use for line numbers"), 586 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle, 587 "Enable symbol demangling"), 588 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel, 589 "Enable kernel symbol demangling"), 590 OPT_BOOLEAN(0, "show-total-period", &symbol_conf.show_total_period, 591 "Show a column with the sum of periods"), 592 OPT_BOOLEAN('n', "show-nr-samples", &symbol_conf.show_nr_samples, 593 "Show a column with the number of samples"), 594 OPT_CALLBACK_DEFAULT(0, "stdio-color", NULL, "mode", 595 "'always' (default), 'never' or 'auto' only applicable to --stdio mode", 596 stdio__config_color, "always"), 597 OPT_CALLBACK(0, "percent-type", &annotate_opts, "local-period", 598 "Set percent type local/global-period/hits", 599 annotate_parse_percent_type), 600 OPT_CALLBACK(0, "percent-limit", &annotate, "percent", 601 "Don't show entries under that percent", parse_percent_limit), 602 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts", 603 "Instruction Tracing options\n" ITRACE_HELP, 604 itrace_parse_synth_opts), 605 606 OPT_END() 607 }; 608 int ret; 609 610 set_option_flag(options, 0, "show-total-period", PARSE_OPT_EXCLUSIVE); 611 set_option_flag(options, 0, "show-nr-samples", PARSE_OPT_EXCLUSIVE); 612 613 annotation_options__init(&annotate_opts); 614 615 ret = hists__init(); 616 if (ret < 0) 617 return ret; 618 619 annotation_config__init(&annotate_opts); 620 621 argc = parse_options(argc, argv, options, annotate_usage, 0); 622 if (argc) { 623 /* 624 * Special case: if there's an argument left then assume that 625 * it's a symbol filter: 626 */ 627 if (argc > 1) 628 usage_with_options(annotate_usage, options); 629 630 annotate.sym_hist_filter = argv[0]; 631 } 632 633 if (disassembler_style) { 634 annotate_opts.disassembler_style = strdup(disassembler_style); 635 if (!annotate_opts.disassembler_style) 636 return -ENOMEM; 637 } 638 if (objdump_path) { 639 annotate_opts.objdump_path = strdup(objdump_path); 640 if (!annotate_opts.objdump_path) 641 return -ENOMEM; 642 } 643 if (addr2line_path) { 644 symbol_conf.addr2line_path = strdup(addr2line_path); 645 if (!symbol_conf.addr2line_path) 646 return -ENOMEM; 647 } 648 649 if (annotate_check_args(&annotate_opts) < 0) 650 return -EINVAL; 651 652 #ifdef HAVE_GTK2_SUPPORT 653 if (symbol_conf.show_nr_samples && annotate.use_gtk) { 654 pr_err("--show-nr-samples is not available in --gtk mode at this time\n"); 655 return ret; 656 } 657 #endif 658 659 ret = symbol__validate_sym_arguments(); 660 if (ret) 661 return ret; 662 663 if (quiet) 664 perf_quiet_option(); 665 666 data.path = input_name; 667 668 annotate.session = perf_session__new(&data, &annotate.tool); 669 if (IS_ERR(annotate.session)) 670 return PTR_ERR(annotate.session); 671 672 annotate.session->itrace_synth_opts = &itrace_synth_opts; 673 674 annotate.has_br_stack = perf_header__has_feat(&annotate.session->header, 675 HEADER_BRANCH_STACK); 676 677 if (annotate.group_set) 678 evlist__force_leader(annotate.session->evlist); 679 680 ret = symbol__annotation_init(); 681 if (ret < 0) 682 goto out_delete; 683 684 symbol_conf.try_vmlinux_path = true; 685 686 ret = symbol__init(&annotate.session->header.env); 687 if (ret < 0) 688 goto out_delete; 689 690 if (annotate.use_stdio || annotate.use_stdio2) 691 use_browser = 0; 692 #ifdef HAVE_SLANG_SUPPORT 693 else if (annotate.use_tui) 694 use_browser = 1; 695 #endif 696 #ifdef HAVE_GTK2_SUPPORT 697 else if (annotate.use_gtk) 698 use_browser = 2; 699 #endif 700 701 setup_browser(true); 702 703 /* 704 * Events of different processes may correspond to the same 705 * symbol, we do not care about the processes in annotate, 706 * set sort order to avoid repeated output. 707 */ 708 sort_order = "dso,symbol"; 709 710 /* 711 * Set SORT_MODE__BRANCH so that annotate display IPC/Cycle 712 * if branch info is in perf data in TUI mode. 713 */ 714 if ((use_browser == 1 || annotate.use_stdio2) && annotate.has_br_stack) 715 sort__mode = SORT_MODE__BRANCH; 716 717 if (setup_sorting(NULL) < 0) 718 usage_with_options(annotate_usage, options); 719 720 ret = __cmd_annotate(&annotate); 721 722 out_delete: 723 /* 724 * Speed up the exit process by only deleting for debug builds. For 725 * large files this can save time. 726 */ 727 #ifndef NDEBUG 728 perf_session__delete(annotate.session); 729 #endif 730 annotation_options__exit(&annotate_opts); 731 732 return ret; 733 } 734