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