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