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