1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com> 4 * 5 * Parts came from builtin-annotate.c, see those files for further 6 * copyright notes. 7 */ 8 9 #include <errno.h> 10 #include <inttypes.h> 11 #include <stdlib.h> 12 #include "util.h" // hex_width() 13 #include "ui/ui.h" 14 #include "sort.h" 15 #include "build-id.h" 16 #include "color.h" 17 #include "config.h" 18 #include "disasm.h" 19 #include "dso.h" 20 #include "env.h" 21 #include "map.h" 22 #include "maps.h" 23 #include "symbol.h" 24 #include "srcline.h" 25 #include "units.h" 26 #include "debug.h" 27 #include "debuginfo.h" 28 #include "annotate.h" 29 #include "annotate-data.h" 30 #include "evsel.h" 31 #include "evlist.h" 32 #include "bpf-event.h" 33 #include "bpf-utils.h" 34 #include "block-range.h" 35 #include "string2.h" 36 #include "dwarf-regs.h" 37 #include "util/event.h" 38 #include "util/sharded_mutex.h" 39 #include "arch/common.h" 40 #include "namespaces.h" 41 #include "thread.h" 42 #include "hashmap.h" 43 #include "strbuf.h" 44 #include <regex.h> 45 #include <linux/bitops.h> 46 #include <linux/err.h> 47 #include <linux/kernel.h> 48 #include <linux/string.h> 49 #include <linux/zalloc.h> 50 #include <subcmd/parse-options.h> 51 #include <subcmd/run-command.h> 52 #include <math.h> 53 54 /* FIXME: For the HE_COLORSET */ 55 #include "ui/browser.h" 56 57 /* 58 * FIXME: Using the same values as slang.h, 59 * but that header may not be available everywhere 60 */ 61 #define LARROW_CHAR ((unsigned char)',') 62 #define RARROW_CHAR ((unsigned char)'+') 63 #define DARROW_CHAR ((unsigned char)'.') 64 #define UARROW_CHAR ((unsigned char)'-') 65 66 #include <linux/ctype.h> 67 68 /* global annotation options */ 69 struct annotation_options annotate_opts; 70 71 /* Data type collection debug statistics */ 72 struct annotated_data_stat ann_data_stat; 73 LIST_HEAD(ann_insn_stat); 74 75 /* Pseudo data types */ 76 struct annotated_data_type stackop_type = { 77 .self = { 78 .type_name = (char *)"(stack operation)", 79 .children = LIST_HEAD_INIT(stackop_type.self.children), 80 }, 81 }; 82 83 struct annotated_data_type canary_type = { 84 .self = { 85 .type_name = (char *)"(stack canary)", 86 .children = LIST_HEAD_INIT(canary_type.self.children), 87 }, 88 }; 89 90 #define NO_TYPE ((struct annotated_data_type *)-1UL) 91 92 /* symbol histogram: key = offset << 16 | evsel->core.idx */ 93 static size_t sym_hist_hash(long key, void *ctx __maybe_unused) 94 { 95 return (key >> 16) + (key & 0xffff); 96 } 97 98 static bool sym_hist_equal(long key1, long key2, void *ctx __maybe_unused) 99 { 100 return key1 == key2; 101 } 102 103 static struct annotated_source *annotated_source__new(void) 104 { 105 struct annotated_source *src = zalloc(sizeof(*src)); 106 107 if (src != NULL) 108 INIT_LIST_HEAD(&src->source); 109 110 return src; 111 } 112 113 static __maybe_unused void annotated_source__delete(struct annotated_source *src) 114 { 115 struct hashmap_entry *cur; 116 size_t bkt; 117 118 if (src == NULL) 119 return; 120 121 if (src->samples) { 122 hashmap__for_each_entry(src->samples, cur, bkt) 123 zfree(&cur->pvalue); 124 hashmap__free(src->samples); 125 } 126 zfree(&src->histograms); 127 free(src); 128 } 129 130 static int annotated_source__alloc_histograms(struct annotated_source *src, 131 int nr_hists) 132 { 133 src->nr_histograms = nr_hists; 134 src->histograms = calloc(nr_hists, sizeof(*src->histograms)); 135 136 if (src->histograms == NULL) 137 return -1; 138 139 src->samples = hashmap__new(sym_hist_hash, sym_hist_equal, NULL); 140 if (IS_ERR(src->samples)) { 141 zfree(&src->histograms); 142 src->samples = NULL; 143 } 144 145 return src->histograms ? 0 : -1; 146 } 147 148 void symbol__annotate_zero_histograms(struct symbol *sym) 149 { 150 struct annotation *notes = symbol__annotation(sym); 151 152 annotation__lock(notes); 153 if (notes->src != NULL) { 154 memset(notes->src->histograms, 0, 155 notes->src->nr_histograms * sizeof(*notes->src->histograms)); 156 hashmap__clear(notes->src->samples); 157 } 158 if (notes->branch && notes->branch->cycles_hist) { 159 memset(notes->branch->cycles_hist, 0, 160 symbol__size(sym) * sizeof(struct cyc_hist)); 161 } 162 annotation__unlock(notes); 163 } 164 165 static int __symbol__account_cycles(struct cyc_hist *ch, 166 u64 start, 167 unsigned offset, unsigned cycles, 168 unsigned have_start) 169 { 170 /* 171 * For now we can only account one basic block per 172 * final jump. But multiple could be overlapping. 173 * Always account the longest one. So when 174 * a shorter one has been already seen throw it away. 175 * 176 * We separately always account the full cycles. 177 */ 178 ch[offset].num_aggr++; 179 ch[offset].cycles_aggr += cycles; 180 181 if (cycles > ch[offset].cycles_max) 182 ch[offset].cycles_max = cycles; 183 184 if (ch[offset].cycles_min) { 185 if (cycles && cycles < ch[offset].cycles_min) 186 ch[offset].cycles_min = cycles; 187 } else 188 ch[offset].cycles_min = cycles; 189 190 if (!have_start && ch[offset].have_start) 191 return 0; 192 if (ch[offset].num) { 193 if (have_start && (!ch[offset].have_start || 194 ch[offset].start > start)) { 195 ch[offset].have_start = 0; 196 ch[offset].cycles = 0; 197 ch[offset].num = 0; 198 if (ch[offset].reset < 0xffff) 199 ch[offset].reset++; 200 } else if (have_start && 201 ch[offset].start < start) 202 return 0; 203 } 204 205 if (ch[offset].num < NUM_SPARKS) 206 ch[offset].cycles_spark[ch[offset].num] = cycles; 207 208 ch[offset].have_start = have_start; 209 ch[offset].start = start; 210 ch[offset].cycles += cycles; 211 ch[offset].num++; 212 return 0; 213 } 214 215 static int __symbol__inc_addr_samples(struct map_symbol *ms, 216 struct annotated_source *src, u64 addr, 217 struct perf_sample *sample) 218 { 219 struct evsel *evsel = sample->evsel; 220 struct symbol *sym = ms->sym; 221 long hash_key; 222 u64 offset; 223 struct sym_hist *h; 224 struct sym_hist_entry *entry; 225 226 pr_debug3("%s: addr=%#" PRIx64 "\n", __func__, map__unmap_ip(ms->map, addr)); 227 228 if ((addr < sym->start || addr >= sym->end) && 229 (addr != sym->end || sym->start != sym->end)) { 230 pr_debug("%s(%d): ERANGE! sym->name=%s, start=%#" PRIx64 ", addr=%#" PRIx64 ", end=%#" PRIx64 "\n", 231 __func__, __LINE__, sym->name, sym->start, addr, sym->end); 232 return -ERANGE; 233 } 234 235 offset = addr - sym->start; 236 h = annotated_source__histogram(src, evsel); 237 if (h == NULL) { 238 pr_debug("%s(%d): ENOMEM! sym->name=%s, start=%#" PRIx64 ", addr=%#" PRIx64 ", end=%#" PRIx64 ", func: %d\n", 239 __func__, __LINE__, sym->name, sym->start, addr, sym->end, sym->type == STT_FUNC); 240 return -ENOMEM; 241 } 242 243 hash_key = offset << 16 | evsel->core.idx; 244 if (!hashmap__find(src->samples, hash_key, &entry)) { 245 entry = zalloc(sizeof(*entry)); 246 if (entry == NULL) 247 return -ENOMEM; 248 249 if (hashmap__add(src->samples, hash_key, entry) < 0) 250 return -ENOMEM; 251 } 252 253 h->nr_samples++; 254 h->period += sample->period; 255 entry->nr_samples++; 256 entry->period += sample->period; 257 258 pr_debug3("%#" PRIx64 " %s: period++ [addr: %#" PRIx64 ", %#" PRIx64 259 ", evidx=%d] => nr_samples: %" PRIu64 ", period: %" PRIu64 "\n", 260 sym->start, sym->name, addr, addr - sym->start, evsel->core.idx, 261 entry->nr_samples, entry->period); 262 return 0; 263 } 264 265 struct annotated_branch *annotation__get_branch(struct annotation *notes) 266 { 267 if (notes == NULL) 268 return NULL; 269 270 if (notes->branch == NULL) 271 notes->branch = zalloc(sizeof(*notes->branch)); 272 273 return notes->branch; 274 } 275 276 static struct annotated_branch *symbol__find_branch_hist(struct symbol *sym, 277 unsigned int br_cntr_nr) 278 { 279 struct annotation *notes = symbol__annotation(sym); 280 struct annotated_branch *branch; 281 const size_t size = symbol__size(sym); 282 283 branch = annotation__get_branch(notes); 284 if (branch == NULL) 285 return NULL; 286 287 if (branch->cycles_hist == NULL) { 288 branch->cycles_hist = calloc(size, sizeof(struct cyc_hist)); 289 if (!branch->cycles_hist) 290 return NULL; 291 } 292 293 if (br_cntr_nr && branch->br_cntr == NULL) { 294 branch->br_cntr = calloc(br_cntr_nr * size, sizeof(u64)); 295 if (!branch->br_cntr) 296 return NULL; 297 } 298 299 return branch; 300 } 301 302 struct annotated_source *symbol__hists(struct symbol *sym, int nr_hists) 303 { 304 struct annotation *notes = symbol__annotation(sym); 305 306 if (notes->src == NULL) { 307 notes->src = annotated_source__new(); 308 if (notes->src == NULL) 309 return NULL; 310 goto alloc_histograms; 311 } 312 313 if (notes->src->histograms == NULL) { 314 alloc_histograms: 315 annotated_source__alloc_histograms(notes->src, nr_hists); 316 } 317 318 return notes->src; 319 } 320 321 static int symbol__inc_addr_samples(struct map_symbol *ms, 322 u64 addr, 323 struct perf_sample *sample) 324 { 325 struct symbol *sym = ms->sym; 326 struct annotated_source *src; 327 328 if (sym == NULL) 329 return 0; 330 src = symbol__hists(sym, sample->evsel->evlist->core.nr_entries); 331 return src ? __symbol__inc_addr_samples(ms, src, addr, sample) : 0; 332 } 333 334 static int symbol__account_br_cntr(struct annotated_branch *branch, 335 struct evsel *evsel, 336 unsigned offset, 337 u64 br_cntr) 338 { 339 unsigned int br_cntr_nr = evsel__leader(evsel)->br_cntr_nr; 340 unsigned int base = evsel__leader(evsel)->br_cntr_idx; 341 unsigned int off = offset * evsel->evlist->nr_br_cntr; 342 u64 *branch_br_cntr = branch->br_cntr; 343 unsigned int i, mask, width; 344 345 if (!br_cntr || !branch_br_cntr) 346 return 0; 347 348 perf_env__find_br_cntr_info(evsel__env(evsel), NULL, &width); 349 mask = (1L << width) - 1; 350 for (i = 0; i < br_cntr_nr; i++) { 351 u64 cntr = (br_cntr >> i * width) & mask; 352 353 branch_br_cntr[off + i + base] += cntr; 354 if (cntr == mask) 355 branch_br_cntr[off + i + base] |= ANNOTATION__BR_CNTR_SATURATED_FLAG; 356 } 357 358 return 0; 359 } 360 361 static int symbol__account_cycles(u64 addr, u64 start, struct symbol *sym, 362 unsigned cycles, struct evsel *evsel, 363 u64 br_cntr) 364 { 365 struct annotated_branch *branch; 366 unsigned offset; 367 int ret; 368 369 if (sym == NULL) 370 return 0; 371 branch = symbol__find_branch_hist(sym, evsel->evlist->nr_br_cntr); 372 if (!branch) 373 return -ENOMEM; 374 if (addr < sym->start || addr >= sym->end) 375 return -ERANGE; 376 377 if (start) { 378 if (start < sym->start || start >= sym->end) 379 return -ERANGE; 380 if (start >= addr) 381 start = 0; 382 } 383 offset = addr - sym->start; 384 ret = __symbol__account_cycles(branch->cycles_hist, 385 start ? start - sym->start : 0, 386 offset, cycles, 387 !!start); 388 389 if (ret) 390 return ret; 391 392 return symbol__account_br_cntr(branch, evsel, offset, br_cntr); 393 } 394 395 int addr_map_symbol__account_cycles(struct addr_map_symbol *ams, 396 struct addr_map_symbol *start, 397 unsigned cycles, 398 struct evsel *evsel, 399 u64 br_cntr) 400 { 401 u64 saddr = 0; 402 int err; 403 404 if (!cycles) 405 return 0; 406 407 /* 408 * Only set start when IPC can be computed. We can only 409 * compute it when the basic block is completely in a single 410 * function. 411 * Special case the case when the jump is elsewhere, but 412 * it starts on the function start. 413 */ 414 if (start && 415 (start->ms.sym == ams->ms.sym || 416 (ams->ms.sym && 417 start->addr == ams->ms.sym->start + map__start(ams->ms.map)))) 418 saddr = start->al_addr; 419 if (saddr == 0) 420 pr_debug2("BB with bad start: addr %"PRIx64" start %"PRIx64" sym %"PRIx64" saddr %"PRIx64"\n", 421 ams->addr, 422 start ? start->addr : 0, 423 ams->ms.sym ? ams->ms.sym->start + map__start(ams->ms.map) : 0, 424 saddr); 425 err = symbol__account_cycles(ams->al_addr, saddr, ams->ms.sym, cycles, evsel, br_cntr); 426 if (err) 427 pr_debug2("account_cycles failed %d\n", err); 428 return err; 429 } 430 431 struct annotation_line *annotated_source__get_line(struct annotated_source *src, 432 s64 offset) 433 { 434 struct annotation_line *al; 435 436 list_for_each_entry(al, &src->source, node) { 437 if (al->offset == offset) 438 return al; 439 } 440 return NULL; 441 } 442 443 static unsigned annotation__count_insn(struct annotation *notes, u64 start, u64 end) 444 { 445 struct annotation_line *al; 446 unsigned n_insn = 0; 447 448 al = annotated_source__get_line(notes->src, start); 449 if (al == NULL) 450 return 0; 451 452 list_for_each_entry_from(al, ¬es->src->source, node) { 453 if (al->offset == -1) 454 continue; 455 if ((u64)al->offset > end) 456 break; 457 n_insn++; 458 } 459 return n_insn; 460 } 461 462 static void annotated_branch__delete(struct annotated_branch *branch) 463 { 464 if (branch) { 465 zfree(&branch->cycles_hist); 466 free(branch->br_cntr); 467 free(branch); 468 } 469 } 470 471 static void annotation__count_and_fill(struct annotation *notes, u64 start, u64 end, struct cyc_hist *ch) 472 { 473 unsigned n_insn; 474 unsigned int cover_insn = 0; 475 476 n_insn = annotation__count_insn(notes, start, end); 477 if (n_insn && ch->num && ch->cycles) { 478 struct annotation_line *al; 479 struct annotated_branch *branch; 480 float ipc = n_insn / ((double)ch->cycles / (double)ch->num); 481 482 /* Hide data when there are too many overlaps. */ 483 if (ch->reset >= 0x7fff) 484 return; 485 486 al = annotated_source__get_line(notes->src, start); 487 if (al == NULL) 488 return; 489 490 list_for_each_entry_from(al, ¬es->src->source, node) { 491 if (al->offset == -1) 492 continue; 493 if ((u64)al->offset > end) 494 break; 495 if (al->cycles && al->cycles->ipc == 0.0) { 496 al->cycles->ipc = ipc; 497 cover_insn++; 498 } 499 } 500 501 branch = annotation__get_branch(notes); 502 if (cover_insn && branch) { 503 branch->hit_cycles += ch->cycles; 504 branch->hit_insn += n_insn * ch->num; 505 branch->cover_insn += cover_insn; 506 } 507 } 508 } 509 510 static int annotation__compute_ipc(struct annotation *notes, size_t size, 511 struct evsel *evsel) 512 { 513 unsigned int br_cntr_nr = evsel->evlist->nr_br_cntr; 514 int err = 0; 515 s64 offset; 516 517 if (!notes->branch || !notes->branch->cycles_hist) 518 return 0; 519 520 notes->branch->total_insn = annotation__count_insn(notes, 0, size - 1); 521 notes->branch->hit_cycles = 0; 522 notes->branch->hit_insn = 0; 523 notes->branch->cover_insn = 0; 524 525 annotation__lock(notes); 526 for (offset = size - 1; offset >= 0; --offset) { 527 struct cyc_hist *ch; 528 529 ch = ¬es->branch->cycles_hist[offset]; 530 if (ch && ch->cycles) { 531 struct annotation_line *al; 532 533 al = annotated_source__get_line(notes->src, offset); 534 if (al && al->cycles == NULL) { 535 al->cycles = zalloc(sizeof(*al->cycles)); 536 if (al->cycles == NULL) { 537 err = ENOMEM; 538 break; 539 } 540 } 541 if (ch->have_start) 542 annotation__count_and_fill(notes, ch->start, offset, ch); 543 if (al && ch->num_aggr) { 544 al->cycles->avg = ch->cycles_aggr / ch->num_aggr; 545 al->cycles->max = ch->cycles_max; 546 al->cycles->min = ch->cycles_min; 547 } 548 if (al && notes->branch->br_cntr) { 549 if (!al->br_cntr) { 550 al->br_cntr = calloc(br_cntr_nr, sizeof(u64)); 551 if (!al->br_cntr) { 552 err = ENOMEM; 553 break; 554 } 555 } 556 al->num_aggr = ch->num_aggr; 557 al->br_cntr_nr = br_cntr_nr; 558 al->evsel = evsel; 559 memcpy(al->br_cntr, ¬es->branch->br_cntr[offset * br_cntr_nr], 560 br_cntr_nr * sizeof(u64)); 561 } 562 } 563 } 564 565 if (err) { 566 while (++offset < (s64)size) { 567 struct cyc_hist *ch = ¬es->branch->cycles_hist[offset]; 568 569 if (ch && ch->cycles) { 570 struct annotation_line *al; 571 572 al = annotated_source__get_line(notes->src, offset); 573 if (al) { 574 zfree(&al->cycles); 575 zfree(&al->br_cntr); 576 } 577 } 578 } 579 } 580 581 annotation__unlock(notes); 582 return 0; 583 } 584 585 int addr_map_symbol__inc_samples(struct addr_map_symbol *ams, struct perf_sample *sample) 586 { 587 return symbol__inc_addr_samples(&ams->ms, ams->al_addr, sample); 588 } 589 590 int hist_entry__inc_addr_samples(struct hist_entry *he, struct perf_sample *sample, u64 ip) 591 { 592 return symbol__inc_addr_samples(&he->ms, ip, sample); 593 } 594 595 596 void annotation__exit(struct annotation *notes) 597 { 598 annotated_source__delete(notes->src); 599 annotated_branch__delete(notes->branch); 600 } 601 602 static struct sharded_mutex *sharded_mutex; 603 604 static void annotation__init_sharded_mutex(void) 605 { 606 /* As many mutexes as there are CPUs. */ 607 sharded_mutex = sharded_mutex__new(cpu__max_present_cpu().cpu); 608 } 609 610 static size_t annotation__hash(const struct annotation *notes) 611 { 612 return (size_t)notes; 613 } 614 615 static struct mutex *annotation__get_mutex(const struct annotation *notes) 616 { 617 static pthread_once_t once = PTHREAD_ONCE_INIT; 618 619 pthread_once(&once, annotation__init_sharded_mutex); 620 if (!sharded_mutex) 621 return NULL; 622 623 return sharded_mutex__get_mutex(sharded_mutex, annotation__hash(notes)); 624 } 625 626 void annotation__lock(struct annotation *notes) 627 NO_THREAD_SAFETY_ANALYSIS 628 { 629 struct mutex *mutex = annotation__get_mutex(notes); 630 631 if (mutex) 632 mutex_lock(mutex); 633 } 634 635 void annotation__unlock(struct annotation *notes) 636 NO_THREAD_SAFETY_ANALYSIS 637 { 638 struct mutex *mutex = annotation__get_mutex(notes); 639 640 if (mutex) 641 mutex_unlock(mutex); 642 } 643 644 bool annotation__trylock(struct annotation *notes) 645 { 646 struct mutex *mutex = annotation__get_mutex(notes); 647 648 if (!mutex) 649 return false; 650 651 return mutex_trylock(mutex); 652 } 653 654 void annotation_line__add(struct annotation_line *al, struct list_head *head) 655 { 656 list_add_tail(&al->node, head); 657 } 658 659 struct annotation_line * 660 annotation_line__next(struct annotation_line *pos, struct list_head *head) 661 { 662 list_for_each_entry_continue(pos, head, node) 663 if (pos->offset >= 0) 664 return pos; 665 666 return NULL; 667 } 668 669 static const char *annotate__address_color(struct block_range *br) 670 { 671 double cov = block_range__coverage(br); 672 673 if (cov >= 0) { 674 /* mark red for >75% coverage */ 675 if (cov > 0.75) 676 return PERF_COLOR_RED; 677 678 /* mark dull for <1% coverage */ 679 if (cov < 0.01) 680 return PERF_COLOR_NORMAL; 681 } 682 683 return PERF_COLOR_MAGENTA; 684 } 685 686 static const char *annotate__asm_color(struct block_range *br) 687 { 688 double cov = block_range__coverage(br); 689 690 if (cov >= 0) { 691 /* mark dull for <1% coverage */ 692 if (cov < 0.01) 693 return PERF_COLOR_NORMAL; 694 } 695 696 return PERF_COLOR_BLUE; 697 } 698 699 static void annotate__branch_printf(struct block_range *br, u64 addr) 700 { 701 bool emit_comment = true; 702 703 if (!br) 704 return; 705 706 #if 1 707 if (br->is_target && br->start == addr) { 708 struct block_range *branch = br; 709 double p; 710 711 /* 712 * Find matching branch to our target. 713 */ 714 while (!branch->is_branch) 715 branch = block_range__next(branch); 716 717 p = 100 *(double)br->entry / branch->coverage; 718 719 if (p > 0.1) { 720 if (emit_comment) { 721 emit_comment = false; 722 printf("\t#"); 723 } 724 725 /* 726 * The percentage of coverage joined at this target in relation 727 * to the next branch. 728 */ 729 printf(" +%.2f%%", p); 730 } 731 } 732 #endif 733 if (br->is_branch && br->end == addr) { 734 double p = 100*(double)br->taken / br->coverage; 735 736 if (p > 0.1) { 737 if (emit_comment) { 738 emit_comment = false; 739 printf("\t#"); 740 } 741 742 /* 743 * The percentage of coverage leaving at this branch, and 744 * its prediction ratio. 745 */ 746 printf(" -%.2f%% (p:%.2f%%)", p, 100*(double)br->pred / br->taken); 747 } 748 } 749 } 750 751 static int disasm_line__print(struct disasm_line *dl, u64 start, int addr_fmt_width) 752 { 753 s64 offset = dl->al.offset; 754 const u64 addr = start + offset; 755 struct block_range *br; 756 757 br = block_range__find(addr); 758 color_fprintf(stdout, annotate__address_color(br), " %*" PRIx64 ":", addr_fmt_width, addr); 759 color_fprintf(stdout, annotate__asm_color(br), "%s", dl->al.line); 760 annotate__branch_printf(br, addr); 761 return 0; 762 } 763 764 static struct annotated_data_type * 765 __hist_entry__get_data_type(struct hist_entry *he, const struct arch *arch, 766 struct debuginfo *dbg, struct disasm_line *dl, 767 int *type_offset); 768 769 static bool needs_type_info(struct annotated_data_type *data_type) 770 { 771 if (data_type == NULL || data_type == NO_TYPE) 772 return false; 773 774 if (verbose) 775 return true; 776 777 return (data_type != &stackop_type) && (data_type != &canary_type); 778 } 779 780 static int 781 annotation_line__print(struct annotation_line *al, struct annotation_print_data *apd, 782 struct annotation_options *opts, int printed, 783 struct annotation_line *queue) 784 { 785 struct symbol *sym = apd->he->ms.sym; 786 struct disasm_line *dl = container_of(al, struct disasm_line, al); 787 struct annotation *notes = symbol__annotation(sym); 788 static const char *prev_line; 789 int max_lines = opts->max_lines; 790 int percent_type = opts->percent_type; 791 792 if (al->offset != -1) { 793 double max_percent = 0.0; 794 int i, nr_percent = 1; 795 const char *color; 796 797 for (i = 0; i < al->data_nr; i++) { 798 double percent; 799 800 percent = annotation_data__percent(&al->data[i], 801 percent_type); 802 803 if (percent > max_percent) 804 max_percent = percent; 805 } 806 807 if (al->data_nr > nr_percent) 808 nr_percent = al->data_nr; 809 810 if (max_percent < opts->min_pcnt) 811 return -1; 812 813 if (max_lines && printed >= max_lines) 814 return 1; 815 816 if (queue != NULL) { 817 struct annotation_options queue_opts = { 818 .max_lines = 1, 819 .percent_type = percent_type, 820 }; 821 822 list_for_each_entry_from(queue, ¬es->src->source, node) { 823 if (queue == al) 824 break; 825 annotation_line__print(queue, apd, &queue_opts, 826 /*printed=*/0, /*queue=*/NULL); 827 } 828 } 829 830 color = get_percent_color(max_percent); 831 832 for (i = 0; i < nr_percent; i++) { 833 struct annotation_data *data = &al->data[i]; 834 double percent; 835 836 percent = annotation_data__percent(data, percent_type); 837 color = get_percent_color(percent); 838 839 if (symbol_conf.show_total_period) 840 color_fprintf(stdout, color, " %11" PRIu64, 841 data->he.period); 842 else if (symbol_conf.show_nr_samples) 843 color_fprintf(stdout, color, " %7" PRIu64, 844 data->he.nr_samples); 845 else 846 color_fprintf(stdout, color, " %7.2f", percent); 847 } 848 849 printf(" : "); 850 851 disasm_line__print(dl, notes->src->start, apd->addr_fmt_width); 852 853 if (opts->code_with_type && apd->dbg) { 854 struct annotated_data_type *data_type; 855 int offset = 0; 856 857 data_type = __hist_entry__get_data_type(apd->he, apd->arch, 858 apd->dbg, dl, &offset); 859 if (needs_type_info(data_type)) { 860 char buf[4096]; 861 862 printf("\t\t# data-type: %s", 863 data_type->self.type_name); 864 865 if (data_type != &stackop_type && 866 data_type != &canary_type) 867 printf(" +%#x", offset); 868 869 if (annotated_data_type__get_member_name(data_type, 870 buf, 871 sizeof(buf), 872 offset)) 873 printf(" (%s)", buf); 874 } 875 } 876 877 /* 878 * Also color the filename and line if needed, with 879 * the same color than the percentage. Don't print it 880 * twice for close colored addr with the same filename:line 881 */ 882 if (al->path) { 883 if (!prev_line || strcmp(prev_line, al->path)) { 884 color_fprintf(stdout, color, " // %s", al->path); 885 prev_line = al->path; 886 } 887 } 888 889 printf("\n"); 890 } else if (max_lines && printed >= max_lines) 891 return 1; 892 else { 893 int width = annotation__pcnt_width(notes); 894 895 if (queue) 896 return -1; 897 898 if (!*al->line) 899 printf(" %*s:\n", width, " "); 900 else 901 printf(" %*s: %-*d %s\n", width, " ", apd->addr_fmt_width, 902 al->line_nr, al->line); 903 } 904 905 return 0; 906 } 907 908 static void calc_percent(struct annotation *notes, 909 struct evsel *evsel, 910 struct annotation_data *data, 911 s64 offset, s64 end) 912 { 913 struct hists *hists = evsel__hists(evsel); 914 struct sym_hist *sym_hist = annotation__histogram(notes, evsel); 915 unsigned int hits = 0; 916 u64 period = 0; 917 918 while (offset < end) { 919 struct sym_hist_entry *entry; 920 921 entry = annotated_source__hist_entry(notes->src, evsel, offset); 922 if (entry) { 923 hits += entry->nr_samples; 924 period += entry->period; 925 } 926 ++offset; 927 } 928 929 if (sym_hist->nr_samples) { 930 data->he.period = period; 931 data->he.nr_samples = hits; 932 data->percent[PERCENT_HITS_LOCAL] = 100.0 * hits / sym_hist->nr_samples; 933 } 934 935 if (hists->stats.nr_non_filtered_samples) 936 data->percent[PERCENT_HITS_GLOBAL] = 100.0 * hits / hists->stats.nr_non_filtered_samples; 937 938 if (sym_hist->period) 939 data->percent[PERCENT_PERIOD_LOCAL] = 100.0 * period / sym_hist->period; 940 941 if (hists->stats.total_period) 942 data->percent[PERCENT_PERIOD_GLOBAL] = 100.0 * period / hists->stats.total_period; 943 } 944 945 static void annotation__calc_percent(struct annotation *notes, 946 struct evsel *leader, s64 len) 947 { 948 struct annotation_line *al, *next; 949 struct evsel *evsel; 950 951 list_for_each_entry(al, ¬es->src->source, node) { 952 s64 end; 953 int i = 0; 954 955 if (al->offset == -1) 956 continue; 957 958 next = annotation_line__next(al, ¬es->src->source); 959 end = next ? next->offset : len; 960 961 for_each_group_evsel(evsel, leader) { 962 struct annotation_data *data; 963 964 BUG_ON(i >= al->data_nr); 965 966 if (symbol_conf.skip_empty && 967 evsel__hists(evsel)->stats.nr_samples == 0) 968 continue; 969 970 data = &al->data[i++]; 971 972 calc_percent(notes, evsel, data, al->offset, end); 973 } 974 } 975 } 976 977 void symbol__calc_percent(struct symbol *sym, struct evsel *evsel) 978 { 979 struct annotation *notes = symbol__annotation(sym); 980 981 annotation__calc_percent(notes, evsel, symbol__size(sym)); 982 } 983 984 int thread__get_arch(struct thread *thread, const struct arch **parch) 985 { 986 const struct arch *arch; 987 struct machine *machine; 988 uint32_t e_flags; 989 uint16_t e_machine; 990 991 if (!thread) { 992 *parch = NULL; 993 return -1; 994 } 995 996 machine = maps__machine(thread__maps(thread)); 997 e_machine = thread__e_machine(thread, machine, &e_flags); 998 arch = arch__find(e_machine, e_flags, machine->env ? machine->env->cpuid : NULL); 999 if (arch == NULL) { 1000 pr_err("%s: unsupported arch %d\n", __func__, e_machine); 1001 return errno; 1002 } 1003 if (parch) 1004 *parch = arch; 1005 1006 return 0; 1007 } 1008 1009 int symbol__annotate(struct map_symbol *ms, struct evsel *evsel, 1010 const struct arch **parch) 1011 { 1012 struct symbol *sym = ms->sym; 1013 struct annotation *notes = symbol__annotation(sym); 1014 struct annotate_args args = { 1015 .options = &annotate_opts, 1016 }; 1017 const struct arch *arch = NULL; 1018 int err, nr; 1019 1020 err = thread__get_arch(ms->thread, &arch); 1021 if (err) 1022 return err; 1023 1024 if (parch) 1025 *parch = arch; 1026 1027 if (notes->src && !list_empty(¬es->src->source)) 1028 return 0; 1029 1030 args.arch = arch; 1031 args.ms = ms; 1032 1033 if (notes->src == NULL) { 1034 notes->src = annotated_source__new(); 1035 if (notes->src == NULL) 1036 return -1; 1037 } 1038 1039 nr = 0; 1040 if (evsel__is_group_event(evsel)) { 1041 struct evsel *pos; 1042 1043 for_each_group_evsel(pos, evsel) { 1044 if (symbol_conf.skip_empty && 1045 evsel__hists(pos)->stats.nr_samples == 0) 1046 continue; 1047 nr++; 1048 } 1049 } 1050 notes->src->nr_events = nr ? nr : 1; 1051 1052 if (annotate_opts.full_addr) 1053 notes->src->start = map__objdump_2mem(ms->map, ms->sym->start); 1054 else 1055 notes->src->start = map__rip_2objdump(ms->map, ms->sym->start); 1056 1057 return symbol__disassemble(sym, &args); 1058 } 1059 1060 static void insert_source_line(struct rb_root *root, struct annotation_line *al) 1061 { 1062 struct annotation_line *iter; 1063 struct rb_node **p = &root->rb_node; 1064 struct rb_node *parent = NULL; 1065 unsigned int percent_type = annotate_opts.percent_type; 1066 int i, ret; 1067 1068 while (*p != NULL) { 1069 parent = *p; 1070 iter = rb_entry(parent, struct annotation_line, rb_node); 1071 1072 ret = strcmp(iter->path, al->path); 1073 if (ret == 0) { 1074 for (i = 0; i < al->data_nr; i++) { 1075 iter->data[i].percent_sum += annotation_data__percent(&al->data[i], 1076 percent_type); 1077 } 1078 return; 1079 } 1080 1081 if (ret < 0) 1082 p = &(*p)->rb_left; 1083 else 1084 p = &(*p)->rb_right; 1085 } 1086 1087 for (i = 0; i < al->data_nr; i++) { 1088 al->data[i].percent_sum = annotation_data__percent(&al->data[i], 1089 percent_type); 1090 } 1091 1092 rb_link_node(&al->rb_node, parent, p); 1093 rb_insert_color(&al->rb_node, root); 1094 } 1095 1096 static int cmp_source_line(struct annotation_line *a, struct annotation_line *b) 1097 { 1098 int i; 1099 1100 for (i = 0; i < a->data_nr; i++) { 1101 if (a->data[i].percent_sum == b->data[i].percent_sum) 1102 continue; 1103 return a->data[i].percent_sum > b->data[i].percent_sum; 1104 } 1105 1106 return 0; 1107 } 1108 1109 static void __resort_source_line(struct rb_root *root, struct annotation_line *al) 1110 { 1111 struct annotation_line *iter; 1112 struct rb_node **p = &root->rb_node; 1113 struct rb_node *parent = NULL; 1114 1115 while (*p != NULL) { 1116 parent = *p; 1117 iter = rb_entry(parent, struct annotation_line, rb_node); 1118 1119 if (cmp_source_line(al, iter)) 1120 p = &(*p)->rb_left; 1121 else 1122 p = &(*p)->rb_right; 1123 } 1124 1125 rb_link_node(&al->rb_node, parent, p); 1126 rb_insert_color(&al->rb_node, root); 1127 } 1128 1129 static void resort_source_line(struct rb_root *dest_root, struct rb_root *src_root) 1130 { 1131 struct annotation_line *al; 1132 struct rb_node *node; 1133 1134 node = rb_first(src_root); 1135 while (node) { 1136 struct rb_node *next; 1137 1138 al = rb_entry(node, struct annotation_line, rb_node); 1139 next = rb_next(node); 1140 rb_erase(node, src_root); 1141 1142 __resort_source_line(dest_root, al); 1143 node = next; 1144 } 1145 } 1146 1147 static void print_summary(struct rb_root *root, const char *filename) 1148 { 1149 struct annotation_line *al; 1150 struct rb_node *node; 1151 1152 printf("\nSorted summary for file %s\n", filename); 1153 printf("----------------------------------------------\n\n"); 1154 1155 if (RB_EMPTY_ROOT(root)) { 1156 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN); 1157 return; 1158 } 1159 1160 node = rb_first(root); 1161 while (node) { 1162 double percent, percent_max = 0.0; 1163 const char *color; 1164 char *path; 1165 int i; 1166 1167 al = rb_entry(node, struct annotation_line, rb_node); 1168 for (i = 0; i < al->data_nr; i++) { 1169 percent = al->data[i].percent_sum; 1170 color = get_percent_color(percent); 1171 color_fprintf(stdout, color, " %7.2f", percent); 1172 1173 if (percent > percent_max) 1174 percent_max = percent; 1175 } 1176 1177 path = al->path; 1178 color = get_percent_color(percent_max); 1179 color_fprintf(stdout, color, " %s\n", path); 1180 1181 node = rb_next(node); 1182 } 1183 } 1184 1185 static void symbol__annotate_hits(struct symbol *sym, struct evsel *evsel) 1186 { 1187 struct annotation *notes = symbol__annotation(sym); 1188 struct sym_hist *h = annotation__histogram(notes, evsel); 1189 u64 len = symbol__size(sym), offset; 1190 1191 for (offset = 0; offset < len; ++offset) { 1192 struct sym_hist_entry *entry; 1193 1194 entry = annotated_source__hist_entry(notes->src, evsel, offset); 1195 if (entry && entry->nr_samples != 0) 1196 printf("%*" PRIx64 ": %" PRIu64 "\n", BITS_PER_LONG / 2, 1197 sym->start + offset, entry->nr_samples); 1198 } 1199 printf("%*s: %" PRIu64 "\n", BITS_PER_LONG / 2, "h->nr_samples", h->nr_samples); 1200 } 1201 1202 static int annotated_source__addr_fmt_width(struct list_head *lines, u64 start) 1203 { 1204 char bf[32]; 1205 struct annotation_line *line; 1206 1207 list_for_each_entry_reverse(line, lines, node) { 1208 if (line->offset != -1) 1209 return scnprintf(bf, sizeof(bf), "%" PRIx64, start + line->offset); 1210 } 1211 1212 return 0; 1213 } 1214 1215 int hist_entry__annotate_printf(struct hist_entry *he, struct evsel *evsel) 1216 { 1217 struct map_symbol *ms = &he->ms; 1218 struct map *map = ms->map; 1219 struct symbol *sym = ms->sym; 1220 struct dso *dso = map__dso(map); 1221 char *filename; 1222 const char *d_filename; 1223 const char *evsel_name = evsel__name(evsel); 1224 struct annotation *notes = symbol__annotation(sym); 1225 struct sym_hist *h = annotation__histogram(notes, evsel); 1226 struct annotation_line *pos, *queue = NULL; 1227 struct annotation_options *opts = &annotate_opts; 1228 struct annotation_print_data apd = { 1229 .he = he, 1230 .evsel = evsel, 1231 }; 1232 int printed = 2, queue_len = 0; 1233 int more = 0; 1234 bool context = opts->context; 1235 int width = annotation__pcnt_width(notes); 1236 int graph_dotted_len; 1237 char buf[512]; 1238 1239 filename = strdup(dso__long_name(dso)); 1240 if (!filename) 1241 return -ENOMEM; 1242 1243 if (opts->full_path) 1244 d_filename = filename; 1245 else 1246 d_filename = perf_basename(filename); 1247 1248 if (evsel__is_group_event(evsel)) { 1249 evsel__group_desc(evsel, buf, sizeof(buf)); 1250 evsel_name = buf; 1251 } 1252 1253 graph_dotted_len = printf(" %-*.*s| Source code & Disassembly of %s for %s (%" PRIu64 " samples, " 1254 "percent: %s)\n", 1255 width, width, symbol_conf.show_total_period ? "Period" : 1256 symbol_conf.show_nr_samples ? "Samples" : "Percent", 1257 d_filename, evsel_name, h->nr_samples, 1258 percent_type_str(opts->percent_type)); 1259 1260 printf("%-*.*s----\n", 1261 graph_dotted_len, graph_dotted_len, graph_dotted_line); 1262 1263 if (verbose > 0) 1264 symbol__annotate_hits(sym, evsel); 1265 1266 apd.addr_fmt_width = annotated_source__addr_fmt_width(¬es->src->source, 1267 notes->src->start); 1268 thread__get_arch(ms->thread, &apd.arch); 1269 apd.dbg = dso__debuginfo(dso); 1270 1271 list_for_each_entry(pos, ¬es->src->source, node) { 1272 int err; 1273 1274 if (context && queue == NULL) { 1275 queue = pos; 1276 queue_len = 0; 1277 } 1278 1279 err = annotation_line__print(pos, &apd, opts, printed, queue); 1280 1281 switch (err) { 1282 case 0: 1283 ++printed; 1284 if (context) { 1285 printed += queue_len; 1286 queue = NULL; 1287 queue_len = 0; 1288 } 1289 break; 1290 case 1: 1291 /* filtered by max_lines */ 1292 ++more; 1293 break; 1294 case -1: 1295 default: 1296 /* 1297 * Filtered by min_pcnt or non IP lines when 1298 * context != 0 1299 */ 1300 if (!context) 1301 break; 1302 if (queue_len == context) 1303 queue = list_entry(queue->node.next, typeof(*queue), node); 1304 else 1305 ++queue_len; 1306 break; 1307 } 1308 } 1309 1310 debuginfo__delete(apd.dbg); 1311 free(filename); 1312 1313 return more; 1314 } 1315 1316 static void FILE__set_percent_color(void *fp __maybe_unused, 1317 double percent __maybe_unused, 1318 bool current __maybe_unused) 1319 { 1320 } 1321 1322 static int FILE__set_jumps_percent_color(void *fp __maybe_unused, 1323 int nr __maybe_unused, bool current __maybe_unused) 1324 { 1325 return 0; 1326 } 1327 1328 static int FILE__set_color(void *fp __maybe_unused, int color __maybe_unused) 1329 { 1330 return 0; 1331 } 1332 1333 static void FILE__printf(void *fp, const char *fmt, ...) 1334 { 1335 va_list args; 1336 1337 va_start(args, fmt); 1338 vfprintf(fp, fmt, args); 1339 va_end(args); 1340 } 1341 1342 static void FILE__write_graph(void *fp, int graph) 1343 { 1344 const char *s; 1345 switch (graph) { 1346 1347 case DARROW_CHAR: s = "↓"; break; 1348 case UARROW_CHAR: s = "↑"; break; 1349 case LARROW_CHAR: s = "←"; break; 1350 case RARROW_CHAR: s = "→"; break; 1351 default: s = "?"; break; 1352 } 1353 1354 fputs(s, fp); 1355 } 1356 1357 static int symbol__annotate_fprintf2(struct symbol *sym, FILE *fp, 1358 struct annotation_print_data *apd) 1359 { 1360 struct annotation *notes = symbol__annotation(sym); 1361 struct annotation_write_ops wops = { 1362 .first_line = true, 1363 .obj = fp, 1364 .set_color = FILE__set_color, 1365 .set_percent_color = FILE__set_percent_color, 1366 .set_jumps_percent_color = FILE__set_jumps_percent_color, 1367 .printf = FILE__printf, 1368 .write_graph = FILE__write_graph, 1369 }; 1370 struct annotation_line *al; 1371 1372 if (annotate_opts.code_with_type) { 1373 thread__get_arch(apd->he->ms.thread, &apd->arch); 1374 apd->dbg = dso__debuginfo(map__dso(apd->he->ms.map)); 1375 } 1376 1377 list_for_each_entry(al, ¬es->src->source, node) { 1378 if (annotation_line__filter(al)) 1379 continue; 1380 annotation_line__write(al, notes, &wops, apd); 1381 fputc('\n', fp); 1382 wops.first_line = false; 1383 } 1384 1385 if (annotate_opts.code_with_type) 1386 debuginfo__delete(apd->dbg); 1387 1388 return 0; 1389 } 1390 1391 int map_symbol__annotation_dump(struct map_symbol *ms, struct evsel *evsel, 1392 struct hist_entry *he) 1393 { 1394 const char *ev_name = evsel__name(evsel); 1395 char buf[1024]; 1396 char *filename; 1397 int err = -1; 1398 FILE *fp; 1399 struct annotation_print_data apd = { 1400 .he = he, 1401 .evsel = evsel, 1402 }; 1403 1404 if (asprintf(&filename, "%s.annotation", ms->sym->name) < 0) 1405 return -1; 1406 1407 fp = fopen(filename, "w"); 1408 if (fp == NULL) 1409 goto out_free_filename; 1410 1411 if (evsel__is_group_event(evsel)) { 1412 evsel__group_desc(evsel, buf, sizeof(buf)); 1413 ev_name = buf; 1414 } 1415 1416 fprintf(fp, "%s() %s\nEvent: %s\n\n", 1417 ms->sym->name, dso__long_name(map__dso(ms->map)), ev_name); 1418 symbol__annotate_fprintf2(ms->sym, fp, &apd); 1419 1420 fclose(fp); 1421 err = 0; 1422 out_free_filename: 1423 free(filename); 1424 return err; 1425 } 1426 1427 void symbol__annotate_zero_histogram(struct symbol *sym, struct evsel *evsel) 1428 { 1429 struct annotation *notes = symbol__annotation(sym); 1430 struct sym_hist *h = annotation__histogram(notes, evsel); 1431 1432 memset(h, 0, sizeof(*notes->src->histograms) * notes->src->nr_histograms); 1433 } 1434 1435 void symbol__annotate_decay_histogram(struct symbol *sym, struct evsel *evsel) 1436 { 1437 struct annotation *notes = symbol__annotation(sym); 1438 struct sym_hist *h = annotation__histogram(notes, evsel); 1439 struct annotation_line *al; 1440 1441 h->nr_samples = 0; 1442 list_for_each_entry(al, ¬es->src->source, node) { 1443 struct sym_hist_entry *entry; 1444 1445 if (al->offset == -1) 1446 continue; 1447 1448 entry = annotated_source__hist_entry(notes->src, evsel, al->offset); 1449 if (entry == NULL) 1450 continue; 1451 1452 entry->nr_samples = entry->nr_samples * 7 / 8; 1453 h->nr_samples += entry->nr_samples; 1454 } 1455 } 1456 1457 void annotated_source__purge(struct annotated_source *as) 1458 { 1459 struct annotation_line *al, *n; 1460 1461 list_for_each_entry_safe(al, n, &as->source, node) { 1462 list_del_init(&al->node); 1463 disasm_line__free(disasm_line(al)); 1464 } 1465 as->tried_source = false; 1466 } 1467 1468 static size_t disasm_line__fprintf(struct disasm_line *dl, FILE *fp) 1469 { 1470 size_t printed; 1471 1472 if (dl->al.offset == -1) 1473 return fprintf(fp, "%s\n", dl->al.line); 1474 1475 printed = fprintf(fp, "%#" PRIx64 " %s", dl->al.offset, dl->ins.name); 1476 1477 if (dl->ops.raw[0] != '\0') { 1478 printed += fprintf(fp, "%.*s %s\n", 6 - (int)printed, " ", 1479 dl->ops.raw); 1480 } 1481 1482 return printed + fprintf(fp, "\n"); 1483 } 1484 1485 size_t disasm__fprintf(struct list_head *head, FILE *fp) 1486 { 1487 struct disasm_line *pos; 1488 size_t printed = 0; 1489 1490 list_for_each_entry(pos, head, al.node) 1491 printed += disasm_line__fprintf(pos, fp); 1492 1493 return printed; 1494 } 1495 1496 bool disasm_line__is_valid_local_jump(struct disasm_line *dl, struct symbol *sym) 1497 { 1498 if (!dl || !dl->ins.ops || !ins__is_jump(&dl->ins) || 1499 !disasm_line__has_local_offset(dl) || dl->ops.target.offset < 0 || 1500 dl->ops.target.offset >= (s64)symbol__size(sym)) 1501 return false; 1502 1503 return true; 1504 } 1505 1506 static void 1507 annotation__mark_jump_targets(struct annotation *notes, struct symbol *sym) 1508 { 1509 struct annotation_line *al; 1510 1511 /* PLT symbols contain external offsets */ 1512 if (strstr(sym->name, "@plt")) 1513 return; 1514 1515 list_for_each_entry(al, ¬es->src->source, node) { 1516 struct disasm_line *dl; 1517 struct annotation_line *target; 1518 1519 dl = disasm_line(al); 1520 1521 if (!disasm_line__is_valid_local_jump(dl, sym)) 1522 continue; 1523 1524 target = annotated_source__get_line(notes->src, 1525 dl->ops.target.offset); 1526 /* 1527 * FIXME: Oops, no jump target? Buggy disassembler? Or do we 1528 * have to adjust to the previous offset? 1529 */ 1530 if (target == NULL) 1531 continue; 1532 1533 if (++target->jump_sources > notes->src->max_jump_sources) 1534 notes->src->max_jump_sources = target->jump_sources; 1535 } 1536 } 1537 1538 static void annotation__set_index(struct annotation *notes) 1539 { 1540 struct annotation_line *al; 1541 struct annotated_source *src = notes->src; 1542 1543 src->widths.max_line_len = 0; 1544 src->nr_entries = 0; 1545 src->nr_asm_entries = 0; 1546 1547 list_for_each_entry(al, &src->source, node) { 1548 size_t line_len = strlen(al->line); 1549 1550 if (src->widths.max_line_len < line_len) 1551 src->widths.max_line_len = line_len; 1552 al->idx = src->nr_entries++; 1553 if (al->offset != -1) 1554 al->idx_asm = src->nr_asm_entries++; 1555 else 1556 al->idx_asm = -1; 1557 } 1558 } 1559 1560 static inline int width_jumps(int n) 1561 { 1562 if (n >= 100) 1563 return 5; 1564 if (n / 10) 1565 return 2; 1566 return 1; 1567 } 1568 1569 static int annotation__max_ins_name(struct annotation *notes) 1570 { 1571 int max_name = 0, len; 1572 struct annotation_line *al; 1573 1574 list_for_each_entry(al, ¬es->src->source, node) { 1575 if (al->offset == -1) 1576 continue; 1577 1578 len = strlen(disasm_line(al)->ins.name); 1579 if (max_name < len) 1580 max_name = len; 1581 } 1582 1583 return max_name; 1584 } 1585 1586 static void 1587 annotation__init_column_widths(struct annotation *notes, struct symbol *sym) 1588 { 1589 notes->src->widths.addr = notes->src->widths.target = 1590 notes->src->widths.min_addr = hex_width(symbol__size(sym)); 1591 notes->src->widths.max_addr = hex_width(sym->end); 1592 notes->src->widths.jumps = width_jumps(notes->src->max_jump_sources); 1593 notes->src->widths.max_ins_name = annotation__max_ins_name(notes); 1594 } 1595 1596 void annotation__update_column_widths(struct annotation *notes) 1597 { 1598 if (annotate_opts.use_offset) 1599 notes->src->widths.target = notes->src->widths.min_addr; 1600 else if (annotate_opts.full_addr) 1601 notes->src->widths.target = BITS_PER_LONG / 4; 1602 else 1603 notes->src->widths.target = notes->src->widths.max_addr; 1604 1605 notes->src->widths.addr = notes->src->widths.target; 1606 1607 if (annotate_opts.show_nr_jumps) 1608 notes->src->widths.addr += notes->src->widths.jumps + 1; 1609 } 1610 1611 void annotation__toggle_full_addr(struct annotation *notes, struct map_symbol *ms) 1612 { 1613 annotate_opts.full_addr = !annotate_opts.full_addr; 1614 1615 if (annotate_opts.full_addr) 1616 notes->src->start = map__objdump_2mem(ms->map, ms->sym->start); 1617 else 1618 notes->src->start = map__rip_2objdump(ms->map, ms->sym->start); 1619 1620 annotation__update_column_widths(notes); 1621 } 1622 1623 static void annotation__calc_lines(struct annotation *notes, struct map_symbol *ms, 1624 struct rb_root *root) 1625 { 1626 struct annotation_line *al; 1627 struct rb_root tmp_root = RB_ROOT; 1628 1629 list_for_each_entry(al, ¬es->src->source, node) { 1630 double percent_max = 0.0; 1631 u64 addr; 1632 int i; 1633 1634 for (i = 0; i < al->data_nr; i++) { 1635 double percent; 1636 1637 percent = annotation_data__percent(&al->data[i], 1638 annotate_opts.percent_type); 1639 1640 if (percent > percent_max) 1641 percent_max = percent; 1642 } 1643 1644 if (percent_max <= 0.5) 1645 continue; 1646 1647 addr = map__rip_2objdump(ms->map, ms->sym->start); 1648 al->path = get_srcline(map__dso(ms->map), addr + al->offset, NULL, 1649 false, true, ms->sym->start + al->offset); 1650 insert_source_line(&tmp_root, al); 1651 } 1652 1653 resort_source_line(root, &tmp_root); 1654 } 1655 1656 static void symbol__calc_lines(struct map_symbol *ms, struct rb_root *root) 1657 { 1658 struct annotation *notes = symbol__annotation(ms->sym); 1659 1660 annotation__calc_lines(notes, ms, root); 1661 } 1662 1663 int hist_entry__tty_annotate2(struct hist_entry *he, struct evsel *evsel) 1664 { 1665 struct map_symbol *ms = &he->ms; 1666 struct dso *dso = map__dso(ms->map); 1667 struct symbol *sym = ms->sym; 1668 struct rb_root source_line = RB_ROOT; 1669 struct hists *hists = evsel__hists(evsel); 1670 struct annotation_print_data apd = { 1671 .he = he, 1672 .evsel = evsel, 1673 }; 1674 char buf[1024]; 1675 int err; 1676 1677 err = symbol__annotate2(ms, evsel, NULL); 1678 if (err) { 1679 char msg[BUFSIZ]; 1680 1681 dso__set_annotate_warned(dso); 1682 symbol__strerror_disassemble(ms, err, msg, sizeof(msg)); 1683 ui__error("Couldn't annotate %s:\n%s", sym->name, msg); 1684 return -1; 1685 } 1686 1687 if (annotate_opts.print_lines) { 1688 srcline_full_filename = annotate_opts.full_path; 1689 symbol__calc_lines(ms, &source_line); 1690 print_summary(&source_line, dso__long_name(dso)); 1691 } 1692 1693 hists__scnprintf_title(hists, buf, sizeof(buf)); 1694 fprintf(stdout, "%s, [percent: %s]\n%s() %s\n", 1695 buf, percent_type_str(annotate_opts.percent_type), sym->name, dso__long_name(dso)); 1696 symbol__annotate_fprintf2(sym, stdout, &apd); 1697 1698 annotated_source__purge(symbol__annotation(sym)->src); 1699 1700 return 0; 1701 } 1702 1703 int hist_entry__tty_annotate(struct hist_entry *he, struct evsel *evsel) 1704 { 1705 struct map_symbol *ms = &he->ms; 1706 struct dso *dso = map__dso(ms->map); 1707 struct symbol *sym = ms->sym; 1708 struct rb_root source_line = RB_ROOT; 1709 int err; 1710 1711 err = symbol__annotate(ms, evsel, NULL); 1712 if (err) { 1713 char msg[BUFSIZ]; 1714 1715 dso__set_annotate_warned(dso); 1716 symbol__strerror_disassemble(ms, err, msg, sizeof(msg)); 1717 ui__error("Couldn't annotate %s:\n%s", sym->name, msg); 1718 return -1; 1719 } 1720 1721 symbol__calc_percent(sym, evsel); 1722 1723 if (annotate_opts.print_lines) { 1724 srcline_full_filename = annotate_opts.full_path; 1725 symbol__calc_lines(ms, &source_line); 1726 print_summary(&source_line, dso__long_name(dso)); 1727 } 1728 1729 hist_entry__annotate_printf(he, evsel); 1730 1731 annotated_source__purge(symbol__annotation(sym)->src); 1732 1733 return 0; 1734 } 1735 1736 bool ui__has_annotation(void) 1737 { 1738 return use_browser == 1 && perf_hpp_list.sym; 1739 } 1740 1741 1742 static double annotation_line__max_percent(struct annotation_line *al, 1743 unsigned int percent_type) 1744 { 1745 double percent_max = 0.0; 1746 int i; 1747 1748 for (i = 0; i < al->data_nr; i++) { 1749 double percent; 1750 1751 percent = annotation_data__percent(&al->data[i], 1752 percent_type); 1753 1754 if (percent > percent_max) 1755 percent_max = percent; 1756 } 1757 1758 return percent_max; 1759 } 1760 1761 static int disasm_line__write(struct disasm_line *dl, struct annotation *notes, 1762 void *obj, char *bf, size_t size, 1763 void (*obj__printf)(void *obj, const char *fmt, ...), 1764 void (*obj__write_graph)(void *obj, int graph)) 1765 { 1766 if (dl->ins.ops && dl->ins.ops->scnprintf) { 1767 if (ins__is_jump(&dl->ins)) { 1768 bool fwd; 1769 1770 if (dl->ops.target.outside) 1771 goto call_like; 1772 fwd = dl->ops.target.offset > dl->al.offset; 1773 obj__write_graph(obj, fwd ? DARROW_CHAR : UARROW_CHAR); 1774 obj__printf(obj, " "); 1775 } else if (ins__is_call(&dl->ins)) { 1776 call_like: 1777 obj__write_graph(obj, RARROW_CHAR); 1778 obj__printf(obj, " "); 1779 } else if (ins__is_ret(&dl->ins)) { 1780 obj__write_graph(obj, LARROW_CHAR); 1781 obj__printf(obj, " "); 1782 } else { 1783 obj__printf(obj, " "); 1784 } 1785 } else { 1786 obj__printf(obj, " "); 1787 } 1788 1789 return disasm_line__scnprintf(dl, bf, size, !annotate_opts.use_offset, 1790 notes->src->widths.max_ins_name) + 2; 1791 } 1792 1793 static void ipc_coverage_string(char *bf, int size, struct annotation *notes) 1794 { 1795 double ipc = 0.0, coverage = 0.0; 1796 struct annotated_branch *branch = annotation__get_branch(notes); 1797 1798 if (branch && branch->hit_cycles) 1799 ipc = branch->hit_insn / ((double)branch->hit_cycles); 1800 1801 if (branch && branch->total_insn) { 1802 coverage = branch->cover_insn * 100.0 / 1803 ((double)branch->total_insn); 1804 } 1805 1806 scnprintf(bf, size, "(Average IPC: %.2f, IPC Coverage: %.1f%%)", 1807 ipc, coverage); 1808 } 1809 1810 int annotation_br_cntr_abbr_list(char **str, struct evsel *evsel, bool header) 1811 { 1812 struct evsel *pos; 1813 struct strbuf sb; 1814 1815 if (evsel->evlist->nr_br_cntr <= 0) 1816 return -ENOTSUP; 1817 1818 strbuf_init(&sb, /*hint=*/ 0); 1819 1820 if (header && strbuf_addf(&sb, "# Branch counter abbr list:\n")) 1821 goto err; 1822 1823 evlist__for_each_entry(evsel->evlist, pos) { 1824 if (!(pos->core.attr.branch_sample_type & PERF_SAMPLE_BRANCH_COUNTERS)) 1825 continue; 1826 if (header && strbuf_addf(&sb, "#")) 1827 goto err; 1828 1829 if (strbuf_addf(&sb, " %s = %s\n", pos->name, pos->abbr_name)) 1830 goto err; 1831 } 1832 1833 if (header && strbuf_addf(&sb, "#")) 1834 goto err; 1835 if (strbuf_addf(&sb, " '-' No event occurs\n")) 1836 goto err; 1837 1838 if (header && strbuf_addf(&sb, "#")) 1839 goto err; 1840 if (strbuf_addf(&sb, " '+' Event occurrences may be lost due to branch counter saturated\n")) 1841 goto err; 1842 1843 *str = strbuf_detach(&sb, NULL); 1844 1845 return 0; 1846 err: 1847 strbuf_release(&sb); 1848 return -ENOMEM; 1849 } 1850 1851 /* Assume the branch counter saturated at 3 */ 1852 #define ANNOTATION_BR_CNTR_SATURATION 3 1853 1854 int annotation_br_cntr_entry(char **str, int br_cntr_nr, 1855 u64 *br_cntr, int num_aggr, 1856 struct evsel *evsel) 1857 { 1858 struct evsel *pos = evsel ? evlist__first(evsel->evlist) : NULL; 1859 bool saturated = false; 1860 int i, j, avg, used; 1861 struct strbuf sb; 1862 1863 strbuf_init(&sb, /*hint=*/ 0); 1864 for (i = 0; i < br_cntr_nr; i++) { 1865 used = 0; 1866 avg = ceil((double)(br_cntr[i] & ~ANNOTATION__BR_CNTR_SATURATED_FLAG) / 1867 (double)num_aggr); 1868 1869 /* 1870 * A histogram with the abbr name is displayed by default. 1871 * With -v, the exact number of branch counter is displayed. 1872 */ 1873 if (verbose) { 1874 evlist__for_each_entry_from(evsel->evlist, pos) { 1875 if ((pos->core.attr.branch_sample_type & PERF_SAMPLE_BRANCH_COUNTERS) && 1876 (pos->br_cntr_idx == i)) 1877 break; 1878 } 1879 if (strbuf_addstr(&sb, pos->abbr_name)) 1880 goto err; 1881 1882 if (!br_cntr[i]) { 1883 if (strbuf_addstr(&sb, "=-")) 1884 goto err; 1885 } else { 1886 if (strbuf_addf(&sb, "=%d", avg)) 1887 goto err; 1888 } 1889 if (br_cntr[i] & ANNOTATION__BR_CNTR_SATURATED_FLAG) { 1890 if (strbuf_addch(&sb, '+')) 1891 goto err; 1892 } else { 1893 if (strbuf_addch(&sb, ' ')) 1894 goto err; 1895 } 1896 1897 if ((i < br_cntr_nr - 1) && strbuf_addch(&sb, ',')) 1898 goto err; 1899 continue; 1900 } 1901 1902 if (strbuf_addch(&sb, '|')) 1903 goto err; 1904 1905 if (!br_cntr[i]) { 1906 if (strbuf_addch(&sb, '-')) 1907 goto err; 1908 used++; 1909 } else { 1910 evlist__for_each_entry_from(evsel->evlist, pos) { 1911 if ((pos->core.attr.branch_sample_type & PERF_SAMPLE_BRANCH_COUNTERS) && 1912 (pos->br_cntr_idx == i)) 1913 break; 1914 } 1915 if (br_cntr[i] & ANNOTATION__BR_CNTR_SATURATED_FLAG) 1916 saturated = true; 1917 1918 for (j = 0; j < avg; j++, used++) { 1919 /* Print + if the number of logged events > 3 */ 1920 if (j >= ANNOTATION_BR_CNTR_SATURATION) { 1921 saturated = true; 1922 break; 1923 } 1924 if (strbuf_addstr(&sb, pos->abbr_name)) 1925 goto err; 1926 } 1927 1928 if (saturated) { 1929 if (strbuf_addch(&sb, '+')) 1930 goto err; 1931 used++; 1932 } 1933 pos = list_next_entry(pos, core.node); 1934 } 1935 1936 for (j = used; j < ANNOTATION_BR_CNTR_SATURATION + 1; j++) { 1937 if (strbuf_addch(&sb, ' ')) 1938 goto err; 1939 } 1940 } 1941 1942 if (!verbose && strbuf_addch(&sb, br_cntr_nr ? '|' : ' ')) 1943 goto err; 1944 1945 *str = strbuf_detach(&sb, NULL); 1946 1947 return 0; 1948 err: 1949 strbuf_release(&sb); 1950 return -ENOMEM; 1951 } 1952 1953 struct type_hash_entry { 1954 struct annotated_data_type *type; 1955 int offset; 1956 }; 1957 1958 static int disasm_line__snprint_type_info(struct disasm_line *dl, 1959 char *buf, int len, 1960 struct annotation_print_data *apd) 1961 { 1962 struct annotated_data_type *data_type = NULL; 1963 struct type_hash_entry *entry = NULL; 1964 char member[256]; 1965 int offset = 0; 1966 int printed; 1967 1968 scnprintf(buf, len, " "); 1969 1970 if (!annotate_opts.code_with_type || apd->dbg == NULL) 1971 return 1; 1972 1973 if (apd->type_hash) { 1974 hashmap__find(apd->type_hash, dl->al.offset, &entry); 1975 if (entry != NULL) { 1976 data_type = entry->type; 1977 offset = entry->offset; 1978 } 1979 } 1980 1981 if (data_type == NULL) 1982 data_type = __hist_entry__get_data_type(apd->he, apd->arch, apd->dbg, dl, &offset); 1983 1984 if (apd->type_hash && entry == NULL) { 1985 entry = malloc(sizeof(*entry)); 1986 if (entry != NULL) { 1987 entry->type = data_type; 1988 entry->offset = offset; 1989 hashmap__add(apd->type_hash, dl->al.offset, entry); 1990 } 1991 } 1992 1993 if (!needs_type_info(data_type)) 1994 return 1; 1995 1996 printed = scnprintf(buf, len, "\t\t# data-type: %s", data_type->self.type_name); 1997 1998 if (data_type != &stackop_type && data_type != &canary_type && len > printed) 1999 printed += scnprintf(buf + printed, len - printed, " +%#x", offset); 2000 2001 if (annotated_data_type__get_member_name(data_type, member, sizeof(member), offset) && 2002 len > printed) { 2003 printed += scnprintf(buf + printed, len - printed, " (%s)", member); 2004 } 2005 return printed; 2006 } 2007 2008 void annotation_line__write(struct annotation_line *al, struct annotation *notes, 2009 const struct annotation_write_ops *wops, 2010 struct annotation_print_data *apd) 2011 { 2012 bool current_entry = wops->current_entry; 2013 bool change_color = wops->change_color; 2014 double percent_max = annotation_line__max_percent(al, annotate_opts.percent_type); 2015 int width = wops->width; 2016 int pcnt_width = annotation__pcnt_width(notes); 2017 int cycles_width = annotation__cycles_width(notes); 2018 bool show_title = false; 2019 char bf[256]; 2020 int printed; 2021 void *obj = wops->obj; 2022 int (*obj__set_color)(void *obj, int color) = wops->set_color; 2023 void (*obj__set_percent_color)(void *obj, double percent, bool current) = wops->set_percent_color; 2024 int (*obj__set_jumps_percent_color)(void *obj, int nr, bool current) = wops->set_jumps_percent_color; 2025 void (*obj__printf)(void *obj, const char *fmt, ...) = wops->printf; 2026 void (*obj__write_graph)(void *obj, int graph) = wops->write_graph; 2027 2028 if (wops->first_line && (al->offset == -1 || percent_max == 0.0)) { 2029 if (notes->branch && al->cycles) { 2030 if (al->cycles->ipc == 0.0 && al->cycles->avg == 0) 2031 show_title = true; 2032 } else 2033 show_title = true; 2034 } 2035 2036 if (al->offset != -1 && percent_max != 0.0) { 2037 int i; 2038 2039 for (i = 0; i < al->data_nr; i++) { 2040 double percent; 2041 2042 percent = annotation_data__percent(&al->data[i], 2043 annotate_opts.percent_type); 2044 2045 obj__set_percent_color(obj, percent, current_entry); 2046 if (symbol_conf.show_total_period) { 2047 obj__printf(obj, "%11" PRIu64 " ", al->data[i].he.period); 2048 } else if (symbol_conf.show_nr_samples) { 2049 obj__printf(obj, "%7" PRIu64 " ", 2050 al->data[i].he.nr_samples); 2051 } else { 2052 obj__printf(obj, "%7.2f ", percent); 2053 } 2054 } 2055 } else { 2056 obj__set_percent_color(obj, 0, current_entry); 2057 2058 if (!show_title) 2059 obj__printf(obj, "%-*s", pcnt_width, " "); 2060 else { 2061 obj__printf(obj, "%-*s", pcnt_width, 2062 symbol_conf.show_total_period ? "Period" : 2063 symbol_conf.show_nr_samples ? "Samples" : "Percent"); 2064 } 2065 } 2066 width -= pcnt_width; 2067 2068 if (notes->branch) { 2069 if (al->cycles && al->cycles->ipc) 2070 obj__printf(obj, "%*.2f ", ANNOTATION__IPC_WIDTH - 1, al->cycles->ipc); 2071 else if (!show_title) 2072 obj__printf(obj, "%*s", ANNOTATION__IPC_WIDTH, " "); 2073 else 2074 obj__printf(obj, "%*s ", ANNOTATION__IPC_WIDTH - 1, "IPC"); 2075 2076 if (!annotate_opts.show_minmax_cycle) { 2077 if (al->cycles && al->cycles->avg) 2078 obj__printf(obj, "%*" PRIu64 " ", 2079 ANNOTATION__CYCLES_WIDTH - 1, al->cycles->avg); 2080 else if (!show_title) 2081 obj__printf(obj, "%*s", 2082 ANNOTATION__CYCLES_WIDTH, " "); 2083 else 2084 obj__printf(obj, "%*s ", 2085 ANNOTATION__CYCLES_WIDTH - 1, 2086 "Cycle"); 2087 } else { 2088 if (al->cycles) { 2089 char str[32]; 2090 2091 scnprintf(str, sizeof(str), 2092 "%" PRIu64 "(%" PRIu64 "/%" PRIu64 ")", 2093 al->cycles->avg, al->cycles->min, 2094 al->cycles->max); 2095 2096 obj__printf(obj, "%*s ", 2097 ANNOTATION__MINMAX_CYCLES_WIDTH - 1, 2098 str); 2099 } else if (!show_title) 2100 obj__printf(obj, "%*s", 2101 ANNOTATION__MINMAX_CYCLES_WIDTH, 2102 " "); 2103 else 2104 obj__printf(obj, "%*s ", 2105 ANNOTATION__MINMAX_CYCLES_WIDTH - 1, 2106 "Cycle(min/max)"); 2107 } 2108 2109 if (annotate_opts.show_br_cntr) { 2110 if (show_title) { 2111 obj__printf(obj, "%*s ", 2112 ANNOTATION__BR_CNTR_WIDTH, 2113 "Branch Counter"); 2114 } else { 2115 char *buf; 2116 2117 if (!annotation_br_cntr_entry(&buf, al->br_cntr_nr, al->br_cntr, 2118 al->num_aggr, al->evsel)) { 2119 obj__printf(obj, "%*s ", ANNOTATION__BR_CNTR_WIDTH, buf); 2120 free(buf); 2121 } 2122 } 2123 } 2124 2125 if (show_title && !*al->line) { 2126 ipc_coverage_string(bf, sizeof(bf), notes); 2127 obj__printf(obj, "%*s", ANNOTATION__AVG_IPC_WIDTH, bf); 2128 } 2129 } 2130 width -= cycles_width; 2131 2132 obj__printf(obj, " "); 2133 width -= 1; 2134 2135 if (!*al->line) 2136 obj__printf(obj, "%-*s", width, " "); 2137 else if (al->offset == -1) { 2138 if (al->line_nr && annotate_opts.show_linenr) 2139 printed = scnprintf(bf, sizeof(bf), "%-*d ", 2140 notes->src->widths.addr + 1, al->line_nr); 2141 else 2142 printed = scnprintf(bf, sizeof(bf), "%-*s ", 2143 notes->src->widths.addr, " "); 2144 obj__printf(obj, bf); 2145 width -= printed; 2146 obj__printf(obj, "%-*s", width, al->line); 2147 } else { 2148 u64 addr = al->offset; 2149 int color = -1; 2150 2151 if (!annotate_opts.use_offset) 2152 addr += notes->src->start; 2153 2154 if (!annotate_opts.use_offset) { 2155 printed = scnprintf(bf, sizeof(bf), "%" PRIx64 ": ", addr); 2156 } else { 2157 if (al->jump_sources && 2158 annotate_opts.offset_level >= ANNOTATION__OFFSET_JUMP_TARGETS) { 2159 if (annotate_opts.show_nr_jumps) { 2160 int prev; 2161 printed = scnprintf(bf, sizeof(bf), "%*d ", 2162 notes->src->widths.jumps, 2163 al->jump_sources); 2164 prev = obj__set_jumps_percent_color(obj, al->jump_sources, 2165 current_entry); 2166 obj__printf(obj, bf); 2167 obj__set_color(obj, prev); 2168 } 2169 print_addr: 2170 printed = scnprintf(bf, sizeof(bf), "%*" PRIx64 ": ", 2171 notes->src->widths.target, addr); 2172 } else if (ins__is_call(&disasm_line(al)->ins) && 2173 annotate_opts.offset_level >= ANNOTATION__OFFSET_CALL) { 2174 goto print_addr; 2175 } else if (annotate_opts.offset_level == ANNOTATION__MAX_OFFSET_LEVEL) { 2176 goto print_addr; 2177 } else { 2178 printed = scnprintf(bf, sizeof(bf), "%-*s ", 2179 notes->src->widths.addr, " "); 2180 } 2181 } 2182 2183 if (change_color) 2184 color = obj__set_color(obj, HE_COLORSET_ADDR); 2185 obj__printf(obj, bf); 2186 if (change_color) 2187 obj__set_color(obj, color); 2188 2189 width -= printed; 2190 2191 printed = disasm_line__write(disasm_line(al), notes, obj, bf, sizeof(bf), 2192 obj__printf, obj__write_graph); 2193 2194 obj__printf(obj, "%s", bf); 2195 width -= printed; 2196 2197 disasm_line__snprint_type_info(disasm_line(al), bf, sizeof(bf), apd); 2198 obj__printf(obj, "%-*s", width, bf); 2199 } 2200 2201 } 2202 2203 int symbol__annotate2(struct map_symbol *ms, struct evsel *evsel, 2204 const struct arch **parch) 2205 { 2206 struct symbol *sym = ms->sym; 2207 struct annotation *notes = symbol__annotation(sym); 2208 size_t size = symbol__size(sym); 2209 int err; 2210 2211 err = symbol__annotate(ms, evsel, parch); 2212 if (err) 2213 return err; 2214 2215 symbol__calc_percent(sym, evsel); 2216 2217 annotation__set_index(notes); 2218 annotation__mark_jump_targets(notes, sym); 2219 2220 err = annotation__compute_ipc(notes, size, evsel); 2221 if (err) 2222 return err; 2223 2224 annotation__init_column_widths(notes, sym); 2225 annotation__update_column_widths(notes); 2226 sym->annotate2 = 1; 2227 2228 return 0; 2229 } 2230 2231 const char * const perf_disassembler__strs[] = { 2232 [PERF_DISASM_UNKNOWN] = "unknown", 2233 [PERF_DISASM_LLVM] = "llvm", 2234 [PERF_DISASM_CAPSTONE] = "capstone", 2235 [PERF_DISASM_OBJDUMP] = "objdump", 2236 }; 2237 2238 2239 static void annotation_options__add_disassembler(struct annotation_options *options, 2240 enum perf_disassembler dis) 2241 { 2242 for (u8 i = 0; i < ARRAY_SIZE(options->disassemblers); i++) { 2243 if (options->disassemblers[i] == dis) { 2244 /* Disassembler is already present then don't add again. */ 2245 return; 2246 } 2247 if (options->disassemblers[i] == PERF_DISASM_UNKNOWN) { 2248 /* Found a free slot. */ 2249 options->disassemblers[i] = dis; 2250 return; 2251 } 2252 } 2253 pr_err("Failed to add disassembler %d\n", dis); 2254 } 2255 2256 static int annotation_options__add_disassemblers_str(struct annotation_options *options, 2257 const char *str) 2258 { 2259 while (str && *str != '\0') { 2260 const char *comma = strchr(str, ','); 2261 int len = comma ? comma - str : (int)strlen(str); 2262 bool match = false; 2263 2264 for (u8 i = 0; i < ARRAY_SIZE(perf_disassembler__strs); i++) { 2265 const char *dis_str = perf_disassembler__strs[i]; 2266 2267 if (len == (int)strlen(dis_str) && !strncmp(str, dis_str, len)) { 2268 annotation_options__add_disassembler(options, i); 2269 match = true; 2270 break; 2271 } 2272 } 2273 if (!match) { 2274 pr_err("Invalid disassembler '%.*s'\n", len, str); 2275 return -1; 2276 } 2277 str = comma ? comma + 1 : NULL; 2278 } 2279 return 0; 2280 } 2281 2282 static int annotation__config(const char *var, const char *value, void *data) 2283 { 2284 struct annotation_options *opt = data; 2285 2286 if (!strstarts(var, "annotate.")) 2287 return 0; 2288 2289 if (!strcmp(var, "annotate.offset_level")) { 2290 perf_config_u8(&opt->offset_level, "offset_level", value); 2291 2292 if (opt->offset_level > ANNOTATION__MAX_OFFSET_LEVEL) 2293 opt->offset_level = ANNOTATION__MAX_OFFSET_LEVEL; 2294 else if (opt->offset_level < ANNOTATION__MIN_OFFSET_LEVEL) 2295 opt->offset_level = ANNOTATION__MIN_OFFSET_LEVEL; 2296 } else if (!strcmp(var, "annotate.disassemblers")) { 2297 int err = annotation_options__add_disassemblers_str(opt, value); 2298 2299 if (err) 2300 return err; 2301 } else if (!strcmp(var, "annotate.hide_src_code")) { 2302 opt->hide_src_code = perf_config_bool("hide_src_code", value); 2303 } else if (!strcmp(var, "annotate.jump_arrows")) { 2304 opt->jump_arrows = perf_config_bool("jump_arrows", value); 2305 } else if (!strcmp(var, "annotate.show_linenr")) { 2306 opt->show_linenr = perf_config_bool("show_linenr", value); 2307 } else if (!strcmp(var, "annotate.show_nr_jumps")) { 2308 opt->show_nr_jumps = perf_config_bool("show_nr_jumps", value); 2309 } else if (!strcmp(var, "annotate.show_nr_samples")) { 2310 symbol_conf.show_nr_samples = perf_config_bool("show_nr_samples", 2311 value); 2312 } else if (!strcmp(var, "annotate.show_total_period")) { 2313 symbol_conf.show_total_period = perf_config_bool("show_total_period", 2314 value); 2315 } else if (!strcmp(var, "annotate.use_offset")) { 2316 opt->use_offset = perf_config_bool("use_offset", value); 2317 } else if (!strcmp(var, "annotate.disassembler_style")) { 2318 opt->disassembler_style = strdup(value); 2319 if (!opt->disassembler_style) { 2320 pr_err("Not enough memory for annotate.disassembler_style\n"); 2321 return -1; 2322 } 2323 } else if (!strcmp(var, "annotate.objdump")) { 2324 opt->objdump_path = strdup(value); 2325 if (!opt->objdump_path) { 2326 pr_err("Not enough memory for annotate.objdump\n"); 2327 return -1; 2328 } 2329 } else if (!strcmp(var, "annotate.addr2line")) { 2330 symbol_conf.addr2line_path = strdup(value); 2331 if (!symbol_conf.addr2line_path) { 2332 pr_err("Not enough memory for annotate.addr2line\n"); 2333 return -1; 2334 } 2335 } else if (!strcmp(var, "annotate.demangle")) { 2336 symbol_conf.demangle = perf_config_bool("demangle", value); 2337 } else if (!strcmp(var, "annotate.demangle_kernel")) { 2338 symbol_conf.demangle_kernel = perf_config_bool("demangle_kernel", value); 2339 } else { 2340 pr_debug("%s variable unknown, ignoring...", var); 2341 } 2342 2343 return 0; 2344 } 2345 2346 void annotation_options__init(void) 2347 { 2348 struct annotation_options *opt = &annotate_opts; 2349 2350 memset(opt, 0, sizeof(*opt)); 2351 2352 /* Default values. */ 2353 opt->use_offset = true; 2354 opt->jump_arrows = true; 2355 opt->annotate_src = true; 2356 opt->offset_level = ANNOTATION__OFFSET_JUMP_TARGETS; 2357 opt->percent_type = PERCENT_PERIOD_LOCAL; 2358 opt->hide_src_code = true; 2359 opt->hide_src_code_on_title = true; 2360 } 2361 2362 void annotation_options__exit(void) 2363 { 2364 zfree(&annotate_opts.disassembler_style); 2365 zfree(&annotate_opts.objdump_path); 2366 } 2367 2368 static void annotation_options__default_init_disassemblers(struct annotation_options *options) 2369 { 2370 if (options->disassemblers[0] != PERF_DISASM_UNKNOWN) { 2371 /* Already initialized. */ 2372 return; 2373 } 2374 #ifdef HAVE_LIBLLVM_SUPPORT 2375 annotation_options__add_disassembler(options, PERF_DISASM_LLVM); 2376 #endif 2377 #ifdef HAVE_LIBCAPSTONE_SUPPORT 2378 annotation_options__add_disassembler(options, PERF_DISASM_CAPSTONE); 2379 #endif 2380 annotation_options__add_disassembler(options, PERF_DISASM_OBJDUMP); 2381 } 2382 2383 void annotation_config__init(void) 2384 { 2385 perf_config(annotation__config, &annotate_opts); 2386 annotation_options__default_init_disassemblers(&annotate_opts); 2387 } 2388 2389 static unsigned int parse_percent_type(char *str1, char *str2) 2390 { 2391 unsigned int type = (unsigned int) -1; 2392 2393 if (!strcmp("period", str1)) { 2394 if (!strcmp("local", str2)) 2395 type = PERCENT_PERIOD_LOCAL; 2396 else if (!strcmp("global", str2)) 2397 type = PERCENT_PERIOD_GLOBAL; 2398 } 2399 2400 if (!strcmp("hits", str1)) { 2401 if (!strcmp("local", str2)) 2402 type = PERCENT_HITS_LOCAL; 2403 else if (!strcmp("global", str2)) 2404 type = PERCENT_HITS_GLOBAL; 2405 } 2406 2407 return type; 2408 } 2409 2410 int annotate_parse_percent_type(const struct option *opt __maybe_unused, const char *_str, 2411 int unset __maybe_unused) 2412 { 2413 unsigned int type; 2414 char *str1, *str2; 2415 int err = -1; 2416 2417 str1 = strdup(_str); 2418 if (!str1) 2419 return -ENOMEM; 2420 2421 str2 = strchr(str1, '-'); 2422 if (!str2) 2423 goto out; 2424 2425 *str2++ = 0; 2426 2427 type = parse_percent_type(str1, str2); 2428 if (type == (unsigned int) -1) 2429 type = parse_percent_type(str2, str1); 2430 if (type != (unsigned int) -1) { 2431 annotate_opts.percent_type = type; 2432 err = 0; 2433 } 2434 2435 out: 2436 free(str1); 2437 return err; 2438 } 2439 2440 int annotate_check_args(void) 2441 { 2442 struct annotation_options *args = &annotate_opts; 2443 2444 if (args->prefix_strip && !args->prefix) { 2445 pr_err("--prefix-strip requires --prefix\n"); 2446 return -1; 2447 } 2448 return 0; 2449 } 2450 2451 static int arch__dwarf_regnum(const struct arch *arch, const char *str) 2452 { 2453 const char *p; 2454 char *regname, *q; 2455 int reg; 2456 2457 p = strchr(str, arch->objdump.register_char); 2458 if (p == NULL) 2459 return -1; 2460 2461 regname = strdup(p); 2462 if (regname == NULL) 2463 return -1; 2464 2465 q = strpbrk(regname, ",) "); 2466 if (q) 2467 *q = '\0'; 2468 2469 reg = get_dwarf_regnum(regname, arch->id.e_machine, arch->id.e_flags); 2470 free(regname); 2471 return reg; 2472 } 2473 2474 /* 2475 * Get register number and access offset from the given instruction. 2476 * It assumes AT&T x86 asm format like OFFSET(REG). Maybe it needs 2477 * to revisit the format when it handles different architecture. 2478 * Fills @reg and @offset when return 0. 2479 */ 2480 static int extract_reg_offset(const struct arch *arch, const char *str, 2481 struct annotated_op_loc *op_loc) 2482 { 2483 char *p; 2484 2485 if (arch->objdump.register_char == 0) 2486 return -1; 2487 2488 /* 2489 * It should start from offset, but it's possible to skip 0 2490 * in the asm. So 0(%rax) should be same as (%rax). 2491 * 2492 * However, it also start with a segment select register like 2493 * %gs:0x18(%rbx). In that case it should skip the part. 2494 */ 2495 if (*str == arch->objdump.register_char) { 2496 if (arch__is_x86(arch)) { 2497 /* FIXME: Handle other segment registers */ 2498 if (!strncmp(str, "%gs:", 4)) 2499 op_loc->segment = INSN_SEG_X86_GS; 2500 } 2501 2502 while (*str && !isdigit(*str) && 2503 *str != arch->objdump.memory_ref_char) 2504 str++; 2505 } 2506 2507 op_loc->offset = strtol(str, &p, 0); 2508 op_loc->reg1 = arch__dwarf_regnum(arch, p); 2509 if (op_loc->reg1 == -1) 2510 return -1; 2511 2512 /* Get the second register */ 2513 if (op_loc->multi_regs) 2514 op_loc->reg2 = arch__dwarf_regnum(arch, p + 1); 2515 2516 return 0; 2517 } 2518 2519 /** 2520 * annotate_get_insn_location - Get location of instruction 2521 * @arch: the architecture info 2522 * @dl: the target instruction 2523 * @loc: a buffer to save the data 2524 * 2525 * Get detailed location info (register and offset) in the instruction. 2526 * It needs both source and target operand and whether it accesses a 2527 * memory location. The offset field is meaningful only when the 2528 * corresponding mem flag is set. The reg2 field is meaningful only 2529 * when multi_regs flag is set. 2530 * 2531 * Some examples on x86: 2532 * 2533 * mov (%rax), %rcx # src_reg1 = rax, src_mem = 1, src_offset = 0 2534 * # dst_reg1 = rcx, dst_mem = 0 2535 * 2536 * mov 0x18, %r8 # src_reg1 = -1, src_mem = 0 2537 * # dst_reg1 = r8, dst_mem = 0 2538 * 2539 * mov %rsi, 8(%rbx,%rcx,4) # src_reg1 = rsi, src_mem = 0, src_multi_regs = 0 2540 * # dst_reg1 = rbx, dst_reg2 = rcx, dst_mem = 1 2541 * # dst_multi_regs = 1, dst_offset = 8 2542 */ 2543 int annotate_get_insn_location(const struct arch *arch, struct disasm_line *dl, 2544 struct annotated_insn_loc *loc) 2545 { 2546 struct ins_operands *ops; 2547 struct annotated_op_loc *op_loc; 2548 int i; 2549 2550 if (ins__is_lock(&dl->ins)) 2551 ops = dl->ops.locked.ops; 2552 else 2553 ops = &dl->ops; 2554 2555 if (ops == NULL) 2556 return -1; 2557 2558 memset(loc, 0, sizeof(*loc)); 2559 2560 for_each_insn_op_loc(loc, i, op_loc) { 2561 const char *insn_str = ops->source.raw; 2562 bool multi_regs = ops->source.multi_regs; 2563 bool mem_ref = ops->source.mem_ref; 2564 2565 if (i == INSN_OP_TARGET) { 2566 insn_str = ops->target.raw; 2567 multi_regs = ops->target.multi_regs; 2568 mem_ref = ops->target.mem_ref; 2569 } 2570 2571 /* Invalidate the register by default */ 2572 op_loc->reg1 = -1; 2573 op_loc->reg2 = -1; 2574 2575 if (insn_str == NULL) { 2576 if (!arch__is_powerpc(arch)) 2577 continue; 2578 } 2579 2580 /* 2581 * For powerpc, call get_powerpc_regs function which extracts the 2582 * required fields for op_loc, ie reg1, reg2, offset from the 2583 * raw instruction. 2584 */ 2585 if (arch__is_powerpc(arch)) { 2586 op_loc->mem_ref = mem_ref; 2587 op_loc->multi_regs = multi_regs; 2588 get_powerpc_regs(dl->raw.raw_insn, !i, op_loc); 2589 } else if (strchr(insn_str, arch->objdump.memory_ref_char)) { 2590 op_loc->mem_ref = true; 2591 op_loc->multi_regs = multi_regs; 2592 extract_reg_offset(arch, insn_str, op_loc); 2593 } else { 2594 const char *s = insn_str; 2595 char *p = NULL; 2596 2597 if (arch__is_x86(arch)) { 2598 /* FIXME: Handle other segment registers */ 2599 if (!strncmp(insn_str, "%gs:", 4)) { 2600 op_loc->segment = INSN_SEG_X86_GS; 2601 op_loc->offset = strtol(insn_str + 4, 2602 &p, 0); 2603 if (p && p != insn_str + 4) 2604 op_loc->imm = true; 2605 continue; 2606 } 2607 } 2608 2609 if (*s == arch->objdump.register_char) { 2610 op_loc->reg1 = arch__dwarf_regnum(arch, s); 2611 } 2612 else if (*s == arch->objdump.imm_char) { 2613 op_loc->offset = strtol(s + 1, &p, 0); 2614 if (p && p != s + 1) 2615 op_loc->imm = true; 2616 } 2617 } 2618 } 2619 2620 return 0; 2621 } 2622 2623 static struct disasm_line *find_disasm_line(struct symbol *sym, u64 ip, 2624 bool allow_update) 2625 { 2626 struct disasm_line *dl; 2627 struct annotation *notes; 2628 2629 notes = symbol__annotation(sym); 2630 2631 list_for_each_entry(dl, ¬es->src->source, al.node) { 2632 if (dl->al.offset == -1) 2633 continue; 2634 2635 if (sym->start + dl->al.offset == ip) { 2636 /* 2637 * llvm-objdump places "lock" in a separate line and 2638 * in that case, we want to get the next line. 2639 */ 2640 if (ins__is_lock(&dl->ins) && 2641 *dl->ops.raw == '\0' && allow_update) { 2642 ip++; 2643 continue; 2644 } 2645 return dl; 2646 } 2647 } 2648 return NULL; 2649 } 2650 2651 static struct annotated_item_stat *annotate_data_stat(struct list_head *head, 2652 const char *name) 2653 { 2654 struct annotated_item_stat *istat; 2655 2656 list_for_each_entry(istat, head, list) { 2657 if (!strcmp(istat->name, name)) 2658 return istat; 2659 } 2660 2661 istat = zalloc(sizeof(*istat)); 2662 if (istat == NULL) 2663 return NULL; 2664 2665 istat->name = strdup(name); 2666 if ((istat->name == NULL) || (!strlen(istat->name))) { 2667 free(istat); 2668 return NULL; 2669 } 2670 2671 list_add_tail(&istat->list, head); 2672 return istat; 2673 } 2674 2675 static bool is_stack_operation(const struct arch *arch, struct disasm_line *dl) 2676 { 2677 if (arch__is_x86(arch)) { 2678 if (!strncmp(dl->ins.name, "push", 4) || 2679 !strncmp(dl->ins.name, "pop", 3) || 2680 !strncmp(dl->ins.name, "call", 4) || 2681 !strncmp(dl->ins.name, "ret", 3)) 2682 return true; 2683 } 2684 2685 return false; 2686 } 2687 2688 static bool is_stack_canary(const struct arch *arch, struct annotated_op_loc *loc) 2689 { 2690 /* On x86_64, %gs:40 is used for stack canary */ 2691 if (arch__is_x86(arch)) { 2692 if (loc->segment == INSN_SEG_X86_GS && loc->imm && 2693 loc->offset == 40) 2694 return true; 2695 } 2696 2697 return false; 2698 } 2699 2700 /** 2701 * Returns true if the instruction has a memory operand without 2702 * performing a load/store 2703 */ 2704 static bool is_address_gen_insn(const struct arch *arch, struct disasm_line *dl) 2705 { 2706 if (arch__is_x86(arch)) { 2707 if (!strncmp(dl->ins.name, "lea", 3)) 2708 return true; 2709 } 2710 2711 return false; 2712 } 2713 2714 static struct disasm_line * 2715 annotation__prev_asm_line(struct annotation *notes, struct disasm_line *curr) 2716 { 2717 struct list_head *sources = ¬es->src->source; 2718 struct disasm_line *prev; 2719 2720 if (curr == list_first_entry(sources, struct disasm_line, al.node)) 2721 return NULL; 2722 2723 prev = list_prev_entry(curr, al.node); 2724 while (prev->al.offset == -1 && 2725 prev != list_first_entry(sources, struct disasm_line, al.node)) 2726 prev = list_prev_entry(prev, al.node); 2727 2728 if (prev->al.offset == -1) 2729 return NULL; 2730 2731 return prev; 2732 } 2733 2734 static struct disasm_line * 2735 annotation__next_asm_line(struct annotation *notes, struct disasm_line *curr) 2736 { 2737 struct list_head *sources = ¬es->src->source; 2738 struct disasm_line *next; 2739 2740 if (curr == list_last_entry(sources, struct disasm_line, al.node)) 2741 return NULL; 2742 2743 next = list_next_entry(curr, al.node); 2744 while (next->al.offset == -1 && 2745 next != list_last_entry(sources, struct disasm_line, al.node)) 2746 next = list_next_entry(next, al.node); 2747 2748 if (next->al.offset == -1) 2749 return NULL; 2750 2751 return next; 2752 } 2753 2754 u64 annotate_calc_pcrel(struct map_symbol *ms, u64 ip, int offset, 2755 struct disasm_line *dl) 2756 { 2757 struct annotation *notes; 2758 struct disasm_line *next; 2759 u64 addr; 2760 2761 notes = symbol__annotation(ms->sym); 2762 /* 2763 * PC-relative addressing starts from the next instruction address 2764 * But the IP is for the current instruction. Since disasm_line 2765 * doesn't have the instruction size, calculate it using the next 2766 * disasm_line. If it's the last one, we can use symbol's end 2767 * address directly. 2768 */ 2769 next = annotation__next_asm_line(notes, dl); 2770 if (next == NULL) 2771 addr = ms->sym->end + offset; 2772 else 2773 addr = ip + (next->al.offset - dl->al.offset) + offset; 2774 2775 return map__rip_2objdump(ms->map, addr); 2776 } 2777 2778 static struct debuginfo_cache { 2779 struct dso *dso; 2780 struct debuginfo *dbg; 2781 } di_cache; 2782 2783 void debuginfo_cache__delete(void) 2784 { 2785 dso__put(di_cache.dso); 2786 di_cache.dso = NULL; 2787 2788 debuginfo__delete(di_cache.dbg); 2789 di_cache.dbg = NULL; 2790 } 2791 2792 static struct annotated_data_type * 2793 __hist_entry__get_data_type(struct hist_entry *he, const struct arch *arch, 2794 struct debuginfo *dbg, struct disasm_line *dl, 2795 int *type_offset) 2796 { 2797 struct map_symbol *ms = &he->ms; 2798 struct annotated_insn_loc loc; 2799 struct annotated_op_loc *op_loc; 2800 struct annotated_data_type *mem_type; 2801 struct annotated_item_stat *istat; 2802 int i; 2803 2804 istat = annotate_data_stat(&ann_insn_stat, dl->ins.name); 2805 if (istat == NULL) { 2806 ann_data_stat.no_insn++; 2807 return NO_TYPE; 2808 } 2809 2810 if (annotate_get_insn_location(arch, dl, &loc) < 0) { 2811 ann_data_stat.no_insn_ops++; 2812 istat->bad++; 2813 return NO_TYPE; 2814 } 2815 2816 if (is_stack_operation(arch, dl)) { 2817 istat->good++; 2818 *type_offset = 0; 2819 return &stackop_type; 2820 } 2821 2822 if (is_address_gen_insn(arch, dl)) { 2823 istat->bad++; 2824 ann_data_stat.no_mem_ops++; 2825 return NO_TYPE; 2826 } 2827 2828 for_each_insn_op_loc(&loc, i, op_loc) { 2829 struct data_loc_info dloc = { 2830 .arch = arch, 2831 .thread = he->thread, 2832 .ms = ms, 2833 .ip = ms->sym->start + dl->al.offset, 2834 .cpumode = he->cpumode, 2835 .op = op_loc, 2836 .di = dbg, 2837 }; 2838 2839 if (!op_loc->mem_ref && op_loc->segment == INSN_SEG_NONE) 2840 continue; 2841 2842 /* PC-relative addressing */ 2843 if (op_loc->reg1 == DWARF_REG_PC) { 2844 dloc.var_addr = annotate_calc_pcrel(ms, dloc.ip, 2845 op_loc->offset, dl); 2846 } 2847 2848 /* This CPU access in kernel - pretend PC-relative addressing */ 2849 if (dso__kernel(map__dso(ms->map)) && arch__is_x86(arch) && 2850 op_loc->segment == INSN_SEG_X86_GS && op_loc->imm) { 2851 dloc.var_addr = op_loc->offset; 2852 op_loc->reg1 = DWARF_REG_PC; 2853 } 2854 2855 mem_type = find_data_type(&dloc); 2856 2857 if (mem_type == NULL && is_stack_canary(arch, op_loc)) { 2858 istat->good++; 2859 *type_offset = 0; 2860 return &canary_type; 2861 } 2862 2863 if (mem_type) 2864 istat->good++; 2865 else 2866 istat->bad++; 2867 2868 if (symbol_conf.annotate_data_sample) { 2869 struct evsel *evsel = hists_to_evsel(he->hists); 2870 2871 annotated_data_type__update_samples(mem_type, evsel, 2872 dloc.type_offset, 2873 he->stat.nr_events, 2874 he->stat.period); 2875 } 2876 *type_offset = dloc.type_offset; 2877 return mem_type ?: NO_TYPE; 2878 } 2879 2880 /* retry with a fused instruction */ 2881 return NULL; 2882 } 2883 2884 /** 2885 * hist_entry__get_data_type - find data type for given hist entry 2886 * @he: hist entry 2887 * 2888 * This function first annotates the instruction at @he->ip and extracts 2889 * register and offset info from it. Then it searches the DWARF debug 2890 * info to get a variable and type information using the address, register, 2891 * and offset. 2892 */ 2893 struct annotated_data_type *hist_entry__get_data_type(struct hist_entry *he) 2894 { 2895 struct map_symbol *ms = &he->ms; 2896 struct evsel *evsel = hists_to_evsel(he->hists); 2897 const struct arch *arch; 2898 struct disasm_line *dl; 2899 struct annotated_data_type *mem_type; 2900 struct annotated_item_stat *istat; 2901 u64 ip = he->ip; 2902 2903 ann_data_stat.total++; 2904 2905 if (ms->map == NULL || ms->sym == NULL) { 2906 ann_data_stat.no_sym++; 2907 return NULL; 2908 } 2909 2910 if (!symbol_conf.init_annotation) { 2911 ann_data_stat.no_sym++; 2912 return NULL; 2913 } 2914 2915 /* 2916 * di_cache holds a pair of values, but code below assumes 2917 * di_cache.dso can be compared/updated and di_cache.dbg can be 2918 * read/updated independently from each other. That assumption only 2919 * holds in single threaded code. 2920 */ 2921 assert(perf_singlethreaded); 2922 2923 if (map__dso(ms->map) != di_cache.dso) { 2924 dso__put(di_cache.dso); 2925 di_cache.dso = dso__get(map__dso(ms->map)); 2926 2927 debuginfo__delete(di_cache.dbg); 2928 di_cache.dbg = dso__debuginfo(di_cache.dso); 2929 } 2930 2931 if (di_cache.dbg == NULL) { 2932 ann_data_stat.no_dbginfo++; 2933 return NULL; 2934 } 2935 2936 /* Make sure it has the disasm of the function */ 2937 if (symbol__annotate(ms, evsel, &arch) < 0) { 2938 ann_data_stat.no_insn++; 2939 return NULL; 2940 } 2941 2942 /* 2943 * Get a disasm to extract the location from the insn. 2944 * This is too slow... 2945 */ 2946 dl = find_disasm_line(ms->sym, ip, /*allow_update=*/true); 2947 if (dl == NULL) { 2948 ann_data_stat.no_insn++; 2949 return NULL; 2950 } 2951 2952 retry: 2953 mem_type = __hist_entry__get_data_type(he, arch, di_cache.dbg, dl, 2954 &he->mem_type_off); 2955 if (mem_type) 2956 return mem_type == NO_TYPE ? NULL : mem_type; 2957 2958 /* 2959 * Some instructions can be fused and the actual memory access came 2960 * from the previous instruction. 2961 */ 2962 if (dl->al.offset > 0) { 2963 struct annotation *notes; 2964 struct disasm_line *prev_dl; 2965 2966 notes = symbol__annotation(ms->sym); 2967 prev_dl = annotation__prev_asm_line(notes, dl); 2968 2969 if (prev_dl && ins__is_fused(arch, prev_dl->ins.name, dl->ins.name)) { 2970 dl = prev_dl; 2971 goto retry; 2972 } 2973 } 2974 2975 ann_data_stat.no_mem_ops++; 2976 istat = annotate_data_stat(&ann_insn_stat, dl->ins.name); 2977 if (istat) 2978 istat->bad++; 2979 return NULL; 2980 } 2981 2982 /* Basic block traversal (BFS) data structure */ 2983 struct basic_block_data { 2984 struct list_head queue; 2985 struct list_head visited; 2986 }; 2987 2988 /* 2989 * During the traversal, it needs to know the parent block where the current 2990 * block block started from. Note that single basic block can be parent of 2991 * two child basic blocks (in case of condition jump). 2992 */ 2993 struct basic_block_link { 2994 struct list_head node; 2995 struct basic_block_link *parent; 2996 struct annotated_basic_block *bb; 2997 }; 2998 2999 /* Check any of basic block in the list already has the offset */ 3000 static bool basic_block_has_offset(struct list_head *head, s64 offset) 3001 { 3002 struct basic_block_link *link; 3003 3004 list_for_each_entry(link, head, node) { 3005 s64 begin_offset = link->bb->begin->al.offset; 3006 s64 end_offset = link->bb->end->al.offset; 3007 3008 if (begin_offset <= offset && offset <= end_offset) 3009 return true; 3010 } 3011 return false; 3012 } 3013 3014 static bool is_new_basic_block(struct basic_block_data *bb_data, 3015 struct disasm_line *dl) 3016 { 3017 s64 offset = dl->al.offset; 3018 3019 if (basic_block_has_offset(&bb_data->visited, offset)) 3020 return false; 3021 if (basic_block_has_offset(&bb_data->queue, offset)) 3022 return false; 3023 return true; 3024 } 3025 3026 /* Add a basic block starting from dl and link it to the parent */ 3027 static int add_basic_block(struct basic_block_data *bb_data, 3028 struct basic_block_link *parent, 3029 struct disasm_line *dl) 3030 { 3031 struct annotated_basic_block *bb; 3032 struct basic_block_link *link; 3033 3034 if (dl == NULL) 3035 return -1; 3036 3037 if (!is_new_basic_block(bb_data, dl)) 3038 return 0; 3039 3040 bb = zalloc(sizeof(*bb)); 3041 if (bb == NULL) 3042 return -1; 3043 3044 bb->begin = dl; 3045 bb->end = dl; 3046 INIT_LIST_HEAD(&bb->list); 3047 3048 link = malloc(sizeof(*link)); 3049 if (link == NULL) { 3050 free(bb); 3051 return -1; 3052 } 3053 3054 link->bb = bb; 3055 link->parent = parent; 3056 list_add_tail(&link->node, &bb_data->queue); 3057 return 0; 3058 } 3059 3060 /* Returns true when it finds the target in the current basic block */ 3061 static bool process_basic_block(struct basic_block_data *bb_data, 3062 struct basic_block_link *link, 3063 struct symbol *sym, u64 target) 3064 { 3065 struct disasm_line *dl, *next_dl, *last_dl; 3066 struct annotation *notes = symbol__annotation(sym); 3067 bool found = false; 3068 3069 dl = link->bb->begin; 3070 /* Check if it's already visited */ 3071 if (basic_block_has_offset(&bb_data->visited, dl->al.offset)) 3072 return false; 3073 3074 last_dl = list_last_entry(¬es->src->source, 3075 struct disasm_line, al.node); 3076 if (last_dl->al.offset == -1) 3077 last_dl = annotation__prev_asm_line(notes, last_dl); 3078 3079 if (last_dl == NULL) 3080 return false; 3081 3082 list_for_each_entry_from(dl, ¬es->src->source, al.node) { 3083 /* Skip comment or debug info line */ 3084 if (dl->al.offset == -1) 3085 continue; 3086 /* Found the target instruction */ 3087 if (sym->start + dl->al.offset == target) { 3088 found = true; 3089 break; 3090 } 3091 /* End of the function, finish the block */ 3092 if (dl == last_dl) 3093 break; 3094 /* 'return' instruction finishes the block */ 3095 if (ins__is_ret(&dl->ins)) 3096 break; 3097 /* normal instructions are part of the basic block */ 3098 if (!ins__is_jump(&dl->ins)) 3099 continue; 3100 /* jump to a different function, tail call or return */ 3101 if (dl->ops.target.outside) 3102 break; 3103 /* jump instruction creates new basic block(s) */ 3104 next_dl = find_disasm_line(sym, sym->start + dl->ops.target.offset, 3105 /*allow_update=*/false); 3106 if (next_dl) 3107 add_basic_block(bb_data, link, next_dl); 3108 3109 /* 3110 * FIXME: determine conditional jumps properly. 3111 * Conditional jumps create another basic block with the 3112 * next disasm line. 3113 */ 3114 if (!strstr(dl->ins.name, "jmp")) { 3115 next_dl = annotation__next_asm_line(notes, dl); 3116 if (next_dl) 3117 add_basic_block(bb_data, link, next_dl); 3118 } 3119 break; 3120 3121 } 3122 link->bb->end = dl; 3123 return found; 3124 } 3125 3126 /* 3127 * It founds a target basic block, build a proper linked list of basic blocks 3128 * by following the link recursively. 3129 */ 3130 static void link_found_basic_blocks(struct basic_block_link *link, 3131 struct list_head *head) 3132 { 3133 while (link) { 3134 struct basic_block_link *parent = link->parent; 3135 3136 list_move(&link->bb->list, head); 3137 list_del(&link->node); 3138 free(link); 3139 3140 link = parent; 3141 } 3142 } 3143 3144 static void delete_basic_blocks(struct basic_block_data *bb_data) 3145 { 3146 struct basic_block_link *link, *tmp; 3147 3148 list_for_each_entry_safe(link, tmp, &bb_data->queue, node) { 3149 list_del(&link->node); 3150 zfree(&link->bb); 3151 free(link); 3152 } 3153 3154 list_for_each_entry_safe(link, tmp, &bb_data->visited, node) { 3155 list_del(&link->node); 3156 zfree(&link->bb); 3157 free(link); 3158 } 3159 } 3160 3161 /** 3162 * annotate_get_basic_blocks - Get basic blocks for given address range 3163 * @sym: symbol to annotate 3164 * @src: source address 3165 * @dst: destination address 3166 * @head: list head to save basic blocks 3167 * 3168 * This function traverses disasm_lines from @src to @dst and save them in a 3169 * list of annotated_basic_block to @head. It uses BFS to find the shortest 3170 * path between two. The basic_block_link is to maintain parent links so 3171 * that it can build a list of blocks from the start. 3172 */ 3173 int annotate_get_basic_blocks(struct symbol *sym, s64 src, s64 dst, 3174 struct list_head *head) 3175 { 3176 struct basic_block_data bb_data = { 3177 .queue = LIST_HEAD_INIT(bb_data.queue), 3178 .visited = LIST_HEAD_INIT(bb_data.visited), 3179 }; 3180 struct basic_block_link *link; 3181 struct disasm_line *dl; 3182 int ret = -1; 3183 3184 dl = find_disasm_line(sym, src, /*allow_update=*/false); 3185 if (dl == NULL) 3186 return -1; 3187 3188 if (add_basic_block(&bb_data, /*parent=*/NULL, dl) < 0) 3189 return -1; 3190 3191 /* Find shortest path from src to dst using BFS */ 3192 while (!list_empty(&bb_data.queue)) { 3193 link = list_first_entry(&bb_data.queue, struct basic_block_link, node); 3194 3195 if (process_basic_block(&bb_data, link, sym, dst)) { 3196 link_found_basic_blocks(link, head); 3197 ret = 0; 3198 break; 3199 } 3200 list_move(&link->node, &bb_data.visited); 3201 } 3202 delete_basic_blocks(&bb_data); 3203 return ret; 3204 } 3205