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