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 #include "perf.h" 11 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 "util/debug.h" 19 20 #include "util/evlist.h" 21 #include "util/evsel.h" 22 #include "util/annotate.h" 23 #include "util/annotate-data.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 #include "util/util.h" 40 #include "ui/progress.h" 41 42 #include <dlfcn.h> 43 #include <errno.h> 44 #include <linux/bitmap.h> 45 #include <linux/err.h> 46 #include <inttypes.h> 47 48 struct perf_annotate { 49 struct perf_tool tool; 50 struct perf_session *session; 51 #ifdef HAVE_SLANG_SUPPORT 52 bool use_tui; 53 #endif 54 bool use_stdio, use_stdio2; 55 #ifdef HAVE_GTK2_SUPPORT 56 bool use_gtk; 57 #endif 58 bool skip_missing; 59 bool has_br_stack; 60 bool group_set; 61 bool data_type; 62 bool type_stat; 63 bool insn_stat; 64 float min_percent; 65 const char *sym_hist_filter; 66 const char *cpu_list; 67 const char *target_data_type; 68 DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS); 69 }; 70 71 /* 72 * Given one basic block: 73 * 74 * from to branch_i 75 * * ----> * 76 * | 77 * | block 78 * v 79 * * ----> * 80 * from to branch_i+1 81 * 82 * where the horizontal are the branches and the vertical is the executed 83 * block of instructions. 84 * 85 * We count, for each 'instruction', the number of blocks that covered it as 86 * well as count the ratio each branch is taken. 87 * 88 * We can do this without knowing the actual instruction stream by keeping 89 * track of the address ranges. We break down ranges such that there is no 90 * overlap and iterate from the start until the end. 91 * 92 * @acme: once we parse the objdump output _before_ processing the samples, 93 * we can easily fold the branch.cycles IPC bits in. 94 */ 95 static void process_basic_block(struct addr_map_symbol *start, 96 struct addr_map_symbol *end, 97 struct branch_flags *flags) 98 { 99 struct symbol *sym = start->ms.sym; 100 struct annotation *notes = sym ? symbol__annotation(sym) : NULL; 101 struct block_range_iter iter; 102 struct block_range *entry; 103 struct annotated_branch *branch; 104 105 /* 106 * Sanity; NULL isn't executable and the CPU cannot execute backwards 107 */ 108 if (!start->addr || start->addr > end->addr) 109 return; 110 111 iter = block_range__create(start->addr, end->addr); 112 if (!block_range_iter__valid(&iter)) 113 return; 114 115 branch = annotation__get_branch(notes); 116 117 /* 118 * First block in range is a branch target. 119 */ 120 entry = block_range_iter(&iter); 121 assert(entry->is_target); 122 entry->entry++; 123 124 do { 125 entry = block_range_iter(&iter); 126 127 entry->coverage++; 128 entry->sym = sym; 129 130 if (branch) 131 branch->max_coverage = max(branch->max_coverage, entry->coverage); 132 133 } while (block_range_iter__next(&iter)); 134 135 /* 136 * Last block in rage is a branch. 137 */ 138 entry = block_range_iter(&iter); 139 assert(entry->is_branch); 140 entry->taken++; 141 if (flags->predicted) 142 entry->pred++; 143 } 144 145 static void process_branch_stack(struct branch_stack *bs, struct addr_location *al, 146 struct perf_sample *sample) 147 { 148 struct addr_map_symbol *prev = NULL; 149 struct branch_info *bi; 150 int i; 151 152 if (!bs || !bs->nr) 153 return; 154 155 bi = sample__resolve_bstack(sample, al); 156 if (!bi) 157 return; 158 159 for (i = bs->nr - 1; i >= 0; i--) { 160 /* 161 * XXX filter against symbol 162 */ 163 if (prev) 164 process_basic_block(prev, &bi[i].from, &bi[i].flags); 165 prev = &bi[i].to; 166 } 167 168 free(bi); 169 } 170 171 static int hist_iter__branch_callback(struct hist_entry_iter *iter, 172 struct addr_location *al __maybe_unused, 173 bool single __maybe_unused, 174 void *arg __maybe_unused) 175 { 176 struct hist_entry *he = iter->he; 177 struct branch_info *bi; 178 struct perf_sample *sample = iter->sample; 179 int err; 180 181 bi = he->branch_info; 182 err = addr_map_symbol__inc_samples(&bi->from, sample); 183 184 if (err) 185 goto out; 186 187 err = addr_map_symbol__inc_samples(&bi->to, sample); 188 189 out: 190 return err; 191 } 192 193 static int process_branch_callback(struct perf_sample *sample, 194 struct addr_location *al, 195 struct perf_annotate *ann, 196 struct machine *machine) 197 { 198 struct hist_entry_iter iter = { 199 .sample = sample, 200 .add_entry_cb = hist_iter__branch_callback, 201 .hide_unresolved = symbol_conf.hide_unresolved, 202 .ops = &hist_iter_branch, 203 }; 204 struct addr_location a; 205 int ret; 206 207 addr_location__init(&a); 208 if (machine__resolve(machine, &a, sample) < 0) { 209 ret = -1; 210 goto out; 211 } 212 213 if (a.sym == NULL) { 214 ret = 0; 215 goto out; 216 } 217 218 if (a.map != NULL) 219 dso__set_hit(map__dso(a.map)); 220 221 hist__account_cycles(sample->branch_stack, al, sample, /*nonany_branch_mode=*/false, 222 /*total_cycles=*/NULL); 223 224 ret = hist_entry_iter__add(&iter, &a, PERF_MAX_STACK_DEPTH, ann); 225 out: 226 addr_location__exit(&a); 227 return ret; 228 } 229 230 static bool has_annotation(struct perf_annotate *ann) 231 { 232 return ui__has_annotation() || ann->use_stdio2; 233 } 234 235 static int add_sample(struct perf_sample *sample, 236 struct addr_location *al, struct perf_annotate *ann, 237 struct machine *machine) 238 { 239 struct hists *hists = evsel__hists(sample->evsel); 240 struct hist_entry *he; 241 int ret; 242 243 if ((!ann->has_br_stack || !has_annotation(ann)) && 244 ann->sym_hist_filter != NULL && 245 (al->sym == NULL || 246 strcmp(ann->sym_hist_filter, al->sym->name) != 0)) { 247 /* We're only interested in a symbol named sym_hist_filter */ 248 /* 249 * FIXME: why isn't this done in the symbol_filter when loading 250 * the DSO? 251 */ 252 if (al->sym != NULL) { 253 struct dso *dso = map__dso(al->map); 254 255 rb_erase_cached(&al->sym->rb_node, dso__symbols(dso)); 256 symbol__delete(al->sym); 257 dso__reset_find_symbol_cache(dso); 258 } 259 return 0; 260 } 261 262 /* 263 * XXX filtered samples can still have branch entries pointing into our 264 * symbol and are missed. 265 */ 266 process_branch_stack(sample->branch_stack, al, sample); 267 268 if (ann->has_br_stack && has_annotation(ann)) 269 return process_branch_callback(sample, al, ann, machine); 270 271 he = hists__add_entry(hists, al, NULL, NULL, NULL, NULL, sample, true); 272 if (he == NULL) 273 return -ENOMEM; 274 275 ret = hist_entry__inc_addr_samples(he, sample, al->addr); 276 hists__inc_nr_samples(hists, true); 277 return ret; 278 } 279 280 static int process_sample_event(const struct perf_tool *tool, 281 union perf_event *event, 282 struct perf_sample *sample, 283 struct machine *machine) 284 { 285 struct perf_annotate *ann = container_of(tool, struct perf_annotate, tool); 286 struct addr_location al; 287 int ret = 0; 288 289 addr_location__init(&al); 290 if (machine__resolve(machine, &al, sample) < 0) { 291 pr_warning("problem processing %d event, skipping it.\n", 292 event->header.type); 293 ret = -1; 294 goto out_put; 295 } 296 297 if (ann->cpu_list && !test_bit(sample->cpu, ann->cpu_bitmap)) 298 goto out_put; 299 300 if (!al.filtered && 301 add_sample(sample, &al, ann, machine)) { 302 pr_warning("problem incrementing symbol count, " 303 "skipping event\n"); 304 ret = -1; 305 } 306 out_put: 307 addr_location__exit(&al); 308 return ret; 309 } 310 311 static int hist_entry__stdio_annotate(struct hist_entry *he, 312 struct evsel *evsel, 313 struct perf_annotate *ann) 314 { 315 if (ann->use_stdio2) 316 return hist_entry__tty_annotate2(he, evsel); 317 318 return hist_entry__tty_annotate(he, evsel); 319 } 320 321 static void print_annotate_data_stat(struct annotated_data_stat *s) 322 { 323 #define PRINT_STAT(fld) if (s->fld) printf("%10d : %s\n", s->fld, #fld) 324 325 int bad = s->no_sym + 326 s->no_insn + 327 s->no_insn_ops + 328 s->no_mem_ops + 329 s->no_reg + 330 s->no_dbginfo + 331 s->no_cuinfo + 332 s->no_var + 333 s->no_typeinfo + 334 s->invalid_size + 335 s->bad_offset; 336 int ok = s->total - bad; 337 338 printf("Annotate data type stats:\n"); 339 printf("total %d, ok %d (%.1f%%), bad %d (%.1f%%)\n", 340 s->total, ok, 100.0 * ok / (s->total ?: 1), bad, 100.0 * bad / (s->total ?: 1)); 341 printf("-----------------------------------------------------------\n"); 342 PRINT_STAT(no_sym); 343 PRINT_STAT(no_insn); 344 PRINT_STAT(no_insn_ops); 345 PRINT_STAT(no_mem_ops); 346 PRINT_STAT(no_reg); 347 PRINT_STAT(no_dbginfo); 348 PRINT_STAT(no_cuinfo); 349 PRINT_STAT(no_var); 350 PRINT_STAT(no_typeinfo); 351 PRINT_STAT(invalid_size); 352 PRINT_STAT(bad_offset); 353 PRINT_STAT(insn_track); 354 printf("\n"); 355 356 #undef PRINT_STAT 357 } 358 359 static void print_annotate_item_stat(struct list_head *head, const char *title) 360 { 361 struct annotated_item_stat *istat, *pos, *iter; 362 int total_good, total_bad, total; 363 int sum1, sum2; 364 LIST_HEAD(tmp); 365 366 /* sort the list by count */ 367 list_splice_init(head, &tmp); 368 total_good = total_bad = 0; 369 370 list_for_each_entry_safe(istat, pos, &tmp, list) { 371 total_good += istat->good; 372 total_bad += istat->bad; 373 sum1 = istat->good + istat->bad; 374 375 list_for_each_entry(iter, head, list) { 376 sum2 = iter->good + iter->bad; 377 if (sum1 > sum2) 378 break; 379 } 380 list_move_tail(&istat->list, &iter->list); 381 } 382 total = total_good + total_bad; 383 384 printf("Annotate %s stats\n", title); 385 printf("total %d, ok %d (%.1f%%), bad %d (%.1f%%)\n\n", total, 386 total_good, 100.0 * total_good / (total ?: 1), 387 total_bad, 100.0 * total_bad / (total ?: 1)); 388 printf(" %-20s: %5s %5s\n", "Name/opcode", "Good", "Bad"); 389 printf("-----------------------------------------------------------\n"); 390 list_for_each_entry(istat, head, list) 391 printf(" %-20s: %5d %5d\n", istat->name, istat->good, istat->bad); 392 printf("\n"); 393 } 394 395 static void hists__find_annotations(struct hists *hists, 396 struct evsel *evsel, 397 struct perf_annotate *ann) 398 { 399 struct rb_node *nd = rb_first_cached(&hists->entries), *next; 400 int key = K_RIGHT; 401 402 if (ann->type_stat) 403 print_annotate_data_stat(&ann_data_stat); 404 if (ann->insn_stat) 405 print_annotate_item_stat(&ann_insn_stat, "Instruction"); 406 407 while (nd) { 408 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node); 409 struct annotation *notes; 410 411 if (he->ms.sym == NULL || dso__annotate_warned(map__dso(he->ms.map))) 412 goto find_next; 413 414 if (ann->sym_hist_filter && 415 (strcmp(he->ms.sym->name, ann->sym_hist_filter) != 0)) 416 goto find_next; 417 418 if (ann->min_percent) { 419 float percent = 0; 420 u64 total = hists__total_period(hists); 421 422 if (total) 423 percent = 100.0 * he->stat.period / total; 424 425 if (percent < ann->min_percent) 426 goto find_next; 427 } 428 429 notes = symbol__annotation(he->ms.sym); 430 if (notes->src == NULL) { 431 find_next: 432 if (key == K_LEFT || key == '<') 433 nd = rb_prev(nd); 434 else 435 nd = rb_next(nd); 436 continue; 437 } 438 439 if (ann->data_type) { 440 /* skip unknown type */ 441 if (he->mem_type->histograms == NULL) 442 goto find_next; 443 444 if (ann->target_data_type) { 445 const char *type_name = he->mem_type->self.type_name; 446 447 /* skip 'struct ' prefix in the type name */ 448 if (strncmp(ann->target_data_type, "struct ", 7) && 449 !strncmp(type_name, "struct ", 7)) 450 type_name += 7; 451 452 /* skip 'union ' prefix in the type name */ 453 if (strncmp(ann->target_data_type, "union ", 6) && 454 !strncmp(type_name, "union ", 6)) 455 type_name += 6; 456 457 if (strcmp(ann->target_data_type, type_name)) 458 goto find_next; 459 } 460 461 if (use_browser == 1) 462 key = hist_entry__annotate_data_tui(he, evsel, NULL); 463 else 464 key = hist_entry__annotate_data_tty(he, evsel); 465 466 switch (key) { 467 case -1: 468 if (!ann->skip_missing) 469 return; 470 /* fall through */ 471 case K_RIGHT: 472 case '>': 473 next = rb_next(nd); 474 break; 475 case K_LEFT: 476 case '<': 477 next = rb_prev(nd); 478 break; 479 default: 480 return; 481 } 482 483 if (use_browser == 0 || next != NULL) 484 nd = next; 485 486 continue; 487 } 488 489 if (use_browser == 2) { 490 int ret; 491 int (*annotate)(struct hist_entry *he, 492 struct evsel *evsel, 493 struct hist_browser_timer *hbt); 494 495 annotate = dlsym(perf_gtk_handle, 496 "hist_entry__gtk_annotate"); 497 if (annotate == NULL) { 498 ui__error("GTK browser not found!\n"); 499 return; 500 } 501 502 ret = annotate(he, evsel, NULL); 503 if (!ret || !ann->skip_missing) 504 return; 505 506 /* skip missing symbols */ 507 nd = rb_next(nd); 508 } else if (use_browser == 1) { 509 key = hist_entry__tui_annotate(he, evsel, NULL, NO_ADDR); 510 511 switch (key) { 512 case -1: 513 if (!ann->skip_missing) 514 return; 515 /* fall through */ 516 case K_RIGHT: 517 case '>': 518 next = rb_next(nd); 519 break; 520 case K_LEFT: 521 case '<': 522 next = rb_prev(nd); 523 break; 524 default: 525 return; 526 } 527 528 if (next != NULL) 529 nd = next; 530 } else { 531 hist_entry__stdio_annotate(he, evsel, ann); 532 nd = rb_next(nd); 533 } 534 } 535 } 536 537 static int __cmd_annotate(struct perf_annotate *ann) 538 { 539 int ret; 540 struct perf_session *session = ann->session; 541 struct evsel *pos; 542 u64 total_nr_samples; 543 544 if (ann->cpu_list) { 545 ret = perf_session__cpu_bitmap(session, ann->cpu_list, 546 ann->cpu_bitmap); 547 if (ret) 548 goto out; 549 } 550 551 if (!annotate_opts.objdump_path) { 552 ret = perf_env__lookup_objdump(perf_session__env(session), 553 &annotate_opts.objdump_path); 554 if (ret) 555 goto out; 556 } 557 558 ret = perf_session__process_events(session); 559 if (ret) 560 goto out; 561 562 if (dump_trace) { 563 perf_session__fprintf_nr_events(session, stdout); 564 evlist__fprintf_nr_events(session->evlist, stdout); 565 goto out; 566 } 567 568 if (verbose > 3) 569 perf_session__fprintf(session, stdout); 570 571 if (verbose > 2) 572 perf_session__fprintf_dsos(session, stdout); 573 574 total_nr_samples = 0; 575 evlist__for_each_entry(session->evlist, pos) { 576 struct hists *hists = evsel__hists(pos); 577 u32 nr_samples = hists->stats.nr_samples; 578 struct ui_progress prog; 579 580 if (nr_samples > 0) { 581 total_nr_samples += nr_samples; 582 583 ui_progress__init(&prog, nr_samples, 584 "Merging related events..."); 585 hists__collapse_resort(hists, &prog); 586 ui_progress__finish(); 587 588 /* Don't sort callchain */ 589 evsel__reset_sample_bit(pos, CALLCHAIN); 590 591 ui_progress__init(&prog, nr_samples, 592 "Sorting events for output..."); 593 evsel__output_resort(pos, &prog); 594 ui_progress__finish(); 595 596 /* 597 * An event group needs to display other events too. 598 * Let's delay printing until other events are processed. 599 */ 600 if (symbol_conf.event_group) { 601 if (!evsel__is_group_leader(pos)) { 602 struct hists *leader_hists; 603 604 leader_hists = evsel__hists(evsel__leader(pos)); 605 hists__match(leader_hists, hists); 606 hists__link(leader_hists, hists); 607 } 608 continue; 609 } 610 611 hists__find_annotations(hists, pos, ann); 612 } 613 } 614 615 if (total_nr_samples == 0) { 616 ui__error("The %s data has no samples!\n", session->data->path); 617 goto out; 618 } 619 620 /* Display group events together */ 621 evlist__for_each_entry(session->evlist, pos) { 622 struct hists *hists = evsel__hists(pos); 623 u32 nr_samples = hists->stats.nr_samples; 624 struct ui_progress prog; 625 struct evsel *evsel; 626 627 if (!symbol_conf.event_group || !evsel__is_group_leader(pos)) 628 continue; 629 630 for_each_group_member(evsel, pos) 631 nr_samples += evsel__hists(evsel)->stats.nr_samples; 632 633 if (nr_samples == 0) 634 continue; 635 636 ui_progress__init(&prog, nr_samples, 637 "Sorting group events for output..."); 638 evsel__output_resort(pos, &prog); 639 ui_progress__finish(); 640 641 hists__find_annotations(hists, pos, ann); 642 } 643 644 if (use_browser == 2) { 645 void (*show_annotations)(void); 646 647 show_annotations = dlsym(perf_gtk_handle, 648 "perf_gtk__show_annotations"); 649 if (show_annotations == NULL) { 650 ui__error("GTK browser not found!\n"); 651 goto out; 652 } 653 show_annotations(); 654 } 655 656 out: 657 return ret; 658 } 659 660 static int parse_percent_limit(const struct option *opt, const char *str, 661 int unset __maybe_unused) 662 { 663 struct perf_annotate *ann = opt->value; 664 double pcnt = strtof(str, NULL); 665 666 ann->min_percent = pcnt; 667 return 0; 668 } 669 670 static int parse_data_type(const struct option *opt, const char *str, int unset) 671 { 672 struct perf_annotate *ann = opt->value; 673 674 ann->data_type = !unset; 675 if (str) 676 ann->target_data_type = strdup(str); 677 678 return 0; 679 } 680 681 static const char * const annotate_usage[] = { 682 "perf annotate [<options>]", 683 NULL 684 }; 685 686 int cmd_annotate(int argc, const char **argv) 687 { 688 struct perf_annotate annotate = {}; 689 struct perf_data data = { 690 .mode = PERF_DATA_MODE_READ, 691 }; 692 struct itrace_synth_opts itrace_synth_opts = { 693 .set = 0, 694 }; 695 const char *disassembler_style = NULL, *objdump_path = NULL, *addr2line_path = NULL; 696 struct option options[] = { 697 OPT_STRING('i', "input", &input_name, "file", 698 "input file name"), 699 OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]", 700 "only consider symbols in these dsos"), 701 OPT_STRING('s', "symbol", &annotate.sym_hist_filter, "symbol", 702 "symbol to annotate"), 703 OPT_BOOLEAN('f', "force", &data.force, "don't complain, do it"), 704 OPT_INCR('v', "verbose", &verbose, 705 "be more verbose (show symbol address, etc)"), 706 OPT_BOOLEAN('q', "quiet", &quiet, "do now show any warnings or messages"), 707 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, 708 "dump raw trace in ASCII"), 709 #ifdef HAVE_GTK2_SUPPORT 710 OPT_BOOLEAN(0, "gtk", &annotate.use_gtk, "Use the GTK interface"), 711 #endif 712 #ifdef HAVE_SLANG_SUPPORT 713 OPT_BOOLEAN(0, "tui", &annotate.use_tui, "Use the TUI interface"), 714 #endif 715 OPT_BOOLEAN(0, "stdio", &annotate.use_stdio, "Use the stdio interface"), 716 OPT_BOOLEAN(0, "stdio2", &annotate.use_stdio2, "Use the stdio interface"), 717 OPT_BOOLEAN(0, "ignore-vmlinux", &symbol_conf.ignore_vmlinux, 718 "don't load vmlinux even if found"), 719 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name, 720 "file", "vmlinux pathname"), 721 OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules, 722 "load module symbols - WARNING: use only with -k and LIVE kernel"), 723 OPT_BOOLEAN('l', "print-line", &annotate_opts.print_lines, 724 "print matching source lines (may be slow)"), 725 OPT_BOOLEAN('P', "full-paths", &annotate_opts.full_path, 726 "Don't shorten the displayed pathnames"), 727 OPT_BOOLEAN(0, "skip-missing", &annotate.skip_missing, 728 "Skip symbols that cannot be annotated"), 729 OPT_BOOLEAN_SET(0, "group", &symbol_conf.event_group, 730 &annotate.group_set, 731 "Show event group information together"), 732 OPT_STRING('C', "cpu", &annotate.cpu_list, "cpu", "list of cpus to profile"), 733 OPT_CALLBACK(0, "symfs", NULL, "directory[,layout]", SYMFS_HELP, 734 symbol__config_symfs), 735 OPT_BOOLEAN(0, "source", &annotate_opts.annotate_src, 736 "Interleave source code with assembly code (default)"), 737 OPT_BOOLEAN(0, "asm-raw", &annotate_opts.show_asm_raw, 738 "Display raw encoding of assembly instructions (default)"), 739 OPT_STRING('M', "disassembler-style", &disassembler_style, "disassembler style", 740 "Specify disassembler style (e.g. -M intel for intel syntax)"), 741 OPT_STRING(0, "prefix", &annotate_opts.prefix, "prefix", 742 "Add prefix to source file path names in programs (with --prefix-strip)"), 743 OPT_STRING(0, "prefix-strip", &annotate_opts.prefix_strip, "N", 744 "Strip first N entries of source file path name in programs (with --prefix)"), 745 OPT_STRING(0, "objdump", &objdump_path, "path", 746 "objdump binary to use for disassembly and annotations"), 747 OPT_STRING(0, "addr2line", &addr2line_path, "path", 748 "addr2line binary to use for line numbers"), 749 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle, 750 "Enable symbol demangling"), 751 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel, 752 "Enable kernel symbol demangling"), 753 OPT_BOOLEAN(0, "show-total-period", &symbol_conf.show_total_period, 754 "Show a column with the sum of periods"), 755 OPT_BOOLEAN('n', "show-nr-samples", &symbol_conf.show_nr_samples, 756 "Show a column with the number of samples"), 757 OPT_CALLBACK_DEFAULT(0, "stdio-color", NULL, "mode", 758 "'always' (default), 'never' or 'auto' only applicable to --stdio mode", 759 stdio__config_color, "always"), 760 OPT_CALLBACK(0, "percent-type", &annotate_opts, "local-period", 761 "Set percent type local/global-period/hits", 762 annotate_parse_percent_type), 763 OPT_CALLBACK(0, "percent-limit", &annotate, "percent", 764 "Don't show entries under that percent", parse_percent_limit), 765 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts", 766 "Instruction Tracing options\n" ITRACE_HELP, 767 itrace_parse_synth_opts), 768 OPT_CALLBACK_OPTARG(0, "data-type", &annotate, NULL, "name", 769 "Show data type annotate for the memory accesses", 770 parse_data_type), 771 OPT_BOOLEAN(0, "type-stat", &annotate.type_stat, 772 "Show stats for the data type annotation"), 773 OPT_BOOLEAN(0, "insn-stat", &annotate.insn_stat, 774 "Show instruction stats for the data type annotation"), 775 OPT_BOOLEAN(0, "skip-empty", &symbol_conf.skip_empty, 776 "Do not display empty (or dummy) events in the output"), 777 OPT_BOOLEAN(0, "code-with-type", &annotate_opts.code_with_type, 778 "Show data type info in code annotation (memory instructions only)"), 779 OPT_END() 780 }; 781 int ret; 782 783 set_option_flag(options, 0, "show-total-period", PARSE_OPT_EXCLUSIVE); 784 set_option_flag(options, 0, "show-nr-samples", PARSE_OPT_EXCLUSIVE); 785 786 annotation_options__init(); 787 788 ret = hists__init(); 789 if (ret < 0) 790 return ret; 791 792 annotation_config__init(); 793 794 argc = parse_options(argc, argv, options, annotate_usage, 0); 795 if (argc) { 796 /* 797 * Special case: if there's an argument left then assume that 798 * it's a symbol filter: 799 */ 800 if (argc > 1) 801 usage_with_options(annotate_usage, options); 802 803 annotate.sym_hist_filter = argv[0]; 804 } 805 806 if (disassembler_style) { 807 annotate_opts.disassembler_style = strdup(disassembler_style); 808 if (!annotate_opts.disassembler_style) 809 return -ENOMEM; 810 } 811 if (objdump_path) { 812 annotate_opts.objdump_path = strdup(objdump_path); 813 if (!annotate_opts.objdump_path) 814 return -ENOMEM; 815 } 816 if (addr2line_path) { 817 symbol_conf.addr2line_path = strdup(addr2line_path); 818 if (!symbol_conf.addr2line_path) 819 return -ENOMEM; 820 } 821 822 if (annotate_check_args() < 0) 823 return -EINVAL; 824 825 #ifdef HAVE_GTK2_SUPPORT 826 if (symbol_conf.show_nr_samples && annotate.use_gtk) { 827 pr_err("--show-nr-samples is not available in --gtk mode at this time\n"); 828 return ret; 829 } 830 #endif 831 832 #ifndef HAVE_LIBDW_SUPPORT 833 if (annotate.data_type) { 834 pr_err("Error: Data type profiling is disabled due to missing DWARF support\n"); 835 return -ENOTSUP; 836 } 837 #endif 838 839 ret = symbol__validate_sym_arguments(); 840 if (ret) 841 return ret; 842 843 if (quiet) 844 perf_quiet_option(); 845 846 data.path = input_name; 847 848 perf_tool__init(&annotate.tool, /*ordered_events=*/true); 849 annotate.tool.sample = process_sample_event; 850 annotate.tool.mmap = perf_event__process_mmap; 851 annotate.tool.mmap2 = perf_event__process_mmap2; 852 annotate.tool.comm = perf_event__process_comm; 853 annotate.tool.exit = perf_event__process_exit; 854 annotate.tool.fork = perf_event__process_fork; 855 annotate.tool.namespaces = perf_event__process_namespaces; 856 annotate.tool.attr = perf_event__process_attr; 857 annotate.tool.build_id = perf_event__process_build_id; 858 #ifdef HAVE_LIBTRACEEVENT 859 annotate.tool.tracing_data = perf_event__process_tracing_data; 860 #endif 861 annotate.tool.id_index = perf_event__process_id_index; 862 annotate.tool.auxtrace_info = perf_event__process_auxtrace_info; 863 annotate.tool.auxtrace = perf_event__process_auxtrace; 864 annotate.tool.feature = perf_event__process_feature; 865 annotate.tool.ordering_requires_timestamps = true; 866 867 annotate.session = perf_session__new(&data, &annotate.tool); 868 if (IS_ERR(annotate.session)) 869 return PTR_ERR(annotate.session); 870 871 annotate.session->itrace_synth_opts = &itrace_synth_opts; 872 873 annotate.has_br_stack = perf_header__has_feat(&annotate.session->header, 874 HEADER_BRANCH_STACK); 875 876 if (annotate.group_set) 877 evlist__force_leader(annotate.session->evlist); 878 879 ret = symbol__annotation_init(); 880 if (ret < 0) 881 goto out_delete; 882 883 symbol_conf.try_vmlinux_path = true; 884 885 ret = symbol__init(perf_session__env(annotate.session)); 886 if (ret < 0) 887 goto out_delete; 888 889 if (annotate.use_stdio || annotate.use_stdio2) 890 use_browser = 0; 891 #ifdef HAVE_SLANG_SUPPORT 892 else if (annotate.use_tui) 893 use_browser = 1; 894 #endif 895 #ifdef HAVE_GTK2_SUPPORT 896 else if (annotate.use_gtk) 897 use_browser = 2; 898 #endif 899 900 if (annotate.data_type) { 901 annotate_opts.annotate_src = false; 902 symbol_conf.annotate_data_member = true; 903 symbol_conf.annotate_data_sample = true; 904 } else if (annotate_opts.code_with_type) { 905 symbol_conf.annotate_data_member = true; 906 } 907 908 setup_browser(true); 909 910 /* 911 * Events of different processes may correspond to the same 912 * symbol, we do not care about the processes in annotate, 913 * set sort order to avoid repeated output. 914 */ 915 if (annotate.data_type) 916 sort_order = "dso,type"; 917 else 918 sort_order = "dso,symbol"; 919 920 /* 921 * Set SORT_MODE__BRANCH so that annotate displays IPC/Cycle and 922 * branch counters, if the corresponding branch info is available 923 * in the perf data in the TUI mode. 924 */ 925 if ((use_browser == 1 || annotate.use_stdio2) && annotate.has_br_stack) { 926 sort__mode = SORT_MODE__BRANCH; 927 if (annotate.session->evlist->nr_br_cntr > 0) 928 annotate_opts.show_br_cntr = true; 929 } 930 931 if (setup_sorting(/*evlist=*/NULL, perf_session__env(annotate.session)) < 0) 932 usage_with_options(annotate_usage, options); 933 934 ret = __cmd_annotate(&annotate); 935 936 out_delete: 937 /* 938 * Speed up the exit process by only deleting for debug builds. For 939 * large files this can save time. 940 */ 941 #ifndef NDEBUG 942 perf_session__delete(annotate.session); 943 #endif 944 annotation_options__exit(); 945 946 return ret; 947 } 948