1 /* 2 * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com> 3 * 4 * Parts came from builtin-annotate.c, see those files for further 5 * copyright notes. 6 * 7 * Released under the GPL v2. (and only v2, not any later version) 8 */ 9 10 #include <errno.h> 11 #include <inttypes.h> 12 #include "util.h" 13 #include "ui/ui.h" 14 #include "sort.h" 15 #include "build-id.h" 16 #include "color.h" 17 #include "config.h" 18 #include "cache.h" 19 #include "symbol.h" 20 #include "units.h" 21 #include "debug.h" 22 #include "annotate.h" 23 #include "evsel.h" 24 #include "evlist.h" 25 #include "block-range.h" 26 #include "string2.h" 27 #include "arch/common.h" 28 #include <regex.h> 29 #include <pthread.h> 30 #include <linux/bitops.h> 31 #include <linux/kernel.h> 32 33 /* FIXME: For the HE_COLORSET */ 34 #include "ui/browser.h" 35 36 /* 37 * FIXME: Using the same values as slang.h, 38 * but that header may not be available everywhere 39 */ 40 #define LARROW_CHAR ((unsigned char)',') 41 #define RARROW_CHAR ((unsigned char)'+') 42 #define DARROW_CHAR ((unsigned char)'.') 43 #define UARROW_CHAR ((unsigned char)'-') 44 45 #include "sane_ctype.h" 46 47 struct annotation_options annotation__default_options = { 48 .use_offset = true, 49 .jump_arrows = true, 50 .offset_level = ANNOTATION__OFFSET_JUMP_TARGETS, 51 }; 52 53 const char *disassembler_style; 54 const char *objdump_path; 55 static regex_t file_lineno; 56 57 static struct ins_ops *ins__find(struct arch *arch, const char *name); 58 static void ins__sort(struct arch *arch); 59 static int disasm_line__parse(char *line, const char **namep, char **rawp); 60 61 struct arch { 62 const char *name; 63 struct ins *instructions; 64 size_t nr_instructions; 65 size_t nr_instructions_allocated; 66 struct ins_ops *(*associate_instruction_ops)(struct arch *arch, const char *name); 67 bool sorted_instructions; 68 bool initialized; 69 void *priv; 70 unsigned int model; 71 unsigned int family; 72 int (*init)(struct arch *arch, char *cpuid); 73 bool (*ins_is_fused)(struct arch *arch, const char *ins1, 74 const char *ins2); 75 struct { 76 char comment_char; 77 char skip_functions_char; 78 } objdump; 79 }; 80 81 static struct ins_ops call_ops; 82 static struct ins_ops dec_ops; 83 static struct ins_ops jump_ops; 84 static struct ins_ops mov_ops; 85 static struct ins_ops nop_ops; 86 static struct ins_ops lock_ops; 87 static struct ins_ops ret_ops; 88 89 static int arch__grow_instructions(struct arch *arch) 90 { 91 struct ins *new_instructions; 92 size_t new_nr_allocated; 93 94 if (arch->nr_instructions_allocated == 0 && arch->instructions) 95 goto grow_from_non_allocated_table; 96 97 new_nr_allocated = arch->nr_instructions_allocated + 128; 98 new_instructions = realloc(arch->instructions, new_nr_allocated * sizeof(struct ins)); 99 if (new_instructions == NULL) 100 return -1; 101 102 out_update_instructions: 103 arch->instructions = new_instructions; 104 arch->nr_instructions_allocated = new_nr_allocated; 105 return 0; 106 107 grow_from_non_allocated_table: 108 new_nr_allocated = arch->nr_instructions + 128; 109 new_instructions = calloc(new_nr_allocated, sizeof(struct ins)); 110 if (new_instructions == NULL) 111 return -1; 112 113 memcpy(new_instructions, arch->instructions, arch->nr_instructions); 114 goto out_update_instructions; 115 } 116 117 static int arch__associate_ins_ops(struct arch* arch, const char *name, struct ins_ops *ops) 118 { 119 struct ins *ins; 120 121 if (arch->nr_instructions == arch->nr_instructions_allocated && 122 arch__grow_instructions(arch)) 123 return -1; 124 125 ins = &arch->instructions[arch->nr_instructions]; 126 ins->name = strdup(name); 127 if (!ins->name) 128 return -1; 129 130 ins->ops = ops; 131 arch->nr_instructions++; 132 133 ins__sort(arch); 134 return 0; 135 } 136 137 #include "arch/arm/annotate/instructions.c" 138 #include "arch/arm64/annotate/instructions.c" 139 #include "arch/x86/annotate/instructions.c" 140 #include "arch/powerpc/annotate/instructions.c" 141 #include "arch/s390/annotate/instructions.c" 142 143 static struct arch architectures[] = { 144 { 145 .name = "arm", 146 .init = arm__annotate_init, 147 }, 148 { 149 .name = "arm64", 150 .init = arm64__annotate_init, 151 }, 152 { 153 .name = "x86", 154 .init = x86__annotate_init, 155 .instructions = x86__instructions, 156 .nr_instructions = ARRAY_SIZE(x86__instructions), 157 .ins_is_fused = x86__ins_is_fused, 158 .objdump = { 159 .comment_char = '#', 160 }, 161 }, 162 { 163 .name = "powerpc", 164 .init = powerpc__annotate_init, 165 }, 166 { 167 .name = "s390", 168 .init = s390__annotate_init, 169 .objdump = { 170 .comment_char = '#', 171 }, 172 }, 173 }; 174 175 static void ins__delete(struct ins_operands *ops) 176 { 177 if (ops == NULL) 178 return; 179 zfree(&ops->source.raw); 180 zfree(&ops->source.name); 181 zfree(&ops->target.raw); 182 zfree(&ops->target.name); 183 } 184 185 static int ins__raw_scnprintf(struct ins *ins, char *bf, size_t size, 186 struct ins_operands *ops) 187 { 188 return scnprintf(bf, size, "%-6s %s", ins->name, ops->raw); 189 } 190 191 int ins__scnprintf(struct ins *ins, char *bf, size_t size, 192 struct ins_operands *ops) 193 { 194 if (ins->ops->scnprintf) 195 return ins->ops->scnprintf(ins, bf, size, ops); 196 197 return ins__raw_scnprintf(ins, bf, size, ops); 198 } 199 200 bool ins__is_fused(struct arch *arch, const char *ins1, const char *ins2) 201 { 202 if (!arch || !arch->ins_is_fused) 203 return false; 204 205 return arch->ins_is_fused(arch, ins1, ins2); 206 } 207 208 static int call__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms) 209 { 210 char *endptr, *tok, *name; 211 struct map *map = ms->map; 212 struct addr_map_symbol target = { 213 .map = map, 214 }; 215 216 ops->target.addr = strtoull(ops->raw, &endptr, 16); 217 218 name = strchr(endptr, '<'); 219 if (name == NULL) 220 goto indirect_call; 221 222 name++; 223 224 if (arch->objdump.skip_functions_char && 225 strchr(name, arch->objdump.skip_functions_char)) 226 return -1; 227 228 tok = strchr(name, '>'); 229 if (tok == NULL) 230 return -1; 231 232 *tok = '\0'; 233 ops->target.name = strdup(name); 234 *tok = '>'; 235 236 if (ops->target.name == NULL) 237 return -1; 238 find_target: 239 target.addr = map__objdump_2mem(map, ops->target.addr); 240 241 if (map_groups__find_ams(&target) == 0 && 242 map__rip_2objdump(target.map, map->map_ip(target.map, target.addr)) == ops->target.addr) 243 ops->target.sym = target.sym; 244 245 return 0; 246 247 indirect_call: 248 tok = strchr(endptr, '*'); 249 if (tok != NULL) 250 ops->target.addr = strtoull(tok + 1, NULL, 16); 251 goto find_target; 252 } 253 254 static int call__scnprintf(struct ins *ins, char *bf, size_t size, 255 struct ins_operands *ops) 256 { 257 if (ops->target.sym) 258 return scnprintf(bf, size, "%-6s %s", ins->name, ops->target.sym->name); 259 260 if (ops->target.addr == 0) 261 return ins__raw_scnprintf(ins, bf, size, ops); 262 263 if (ops->target.name) 264 return scnprintf(bf, size, "%-6s %s", ins->name, ops->target.name); 265 266 return scnprintf(bf, size, "%-6s *%" PRIx64, ins->name, ops->target.addr); 267 } 268 269 static struct ins_ops call_ops = { 270 .parse = call__parse, 271 .scnprintf = call__scnprintf, 272 }; 273 274 bool ins__is_call(const struct ins *ins) 275 { 276 return ins->ops == &call_ops || ins->ops == &s390_call_ops; 277 } 278 279 static int jump__parse(struct arch *arch __maybe_unused, struct ins_operands *ops, struct map_symbol *ms) 280 { 281 struct map *map = ms->map; 282 struct symbol *sym = ms->sym; 283 struct addr_map_symbol target = { 284 .map = map, 285 }; 286 const char *c = strchr(ops->raw, ','); 287 u64 start, end; 288 /* 289 * Examples of lines to parse for the _cpp_lex_token@@Base 290 * function: 291 * 292 * 1159e6c: jne 115aa32 <_cpp_lex_token@@Base+0xf92> 293 * 1159e8b: jne c469be <cpp_named_operator2name@@Base+0xa72> 294 * 295 * The first is a jump to an offset inside the same function, 296 * the second is to another function, i.e. that 0xa72 is an 297 * offset in the cpp_named_operator2name@@base function. 298 */ 299 /* 300 * skip over possible up to 2 operands to get to address, e.g.: 301 * tbnz w0, #26, ffff0000083cd190 <security_file_permission+0xd0> 302 */ 303 if (c++ != NULL) { 304 ops->target.addr = strtoull(c, NULL, 16); 305 if (!ops->target.addr) { 306 c = strchr(c, ','); 307 if (c++ != NULL) 308 ops->target.addr = strtoull(c, NULL, 16); 309 } 310 } else { 311 ops->target.addr = strtoull(ops->raw, NULL, 16); 312 } 313 314 target.addr = map__objdump_2mem(map, ops->target.addr); 315 start = map->unmap_ip(map, sym->start), 316 end = map->unmap_ip(map, sym->end); 317 318 ops->target.outside = target.addr < start || target.addr > end; 319 320 /* 321 * FIXME: things like this in _cpp_lex_token (gcc's cc1 program): 322 323 cpp_named_operator2name@@Base+0xa72 324 325 * Point to a place that is after the cpp_named_operator2name 326 * boundaries, i.e. in the ELF symbol table for cc1 327 * cpp_named_operator2name is marked as being 32-bytes long, but it in 328 * fact is much larger than that, so we seem to need a symbols__find() 329 * routine that looks for >= current->start and < next_symbol->start, 330 * possibly just for C++ objects? 331 * 332 * For now lets just make some progress by marking jumps to outside the 333 * current function as call like. 334 * 335 * Actual navigation will come next, with further understanding of how 336 * the symbol searching and disassembly should be done. 337 */ 338 if (map_groups__find_ams(&target) == 0 && 339 map__rip_2objdump(target.map, map->map_ip(target.map, target.addr)) == ops->target.addr) 340 ops->target.sym = target.sym; 341 342 if (!ops->target.outside) { 343 ops->target.offset = target.addr - start; 344 ops->target.offset_avail = true; 345 } else { 346 ops->target.offset_avail = false; 347 } 348 349 return 0; 350 } 351 352 static int jump__scnprintf(struct ins *ins, char *bf, size_t size, 353 struct ins_operands *ops) 354 { 355 const char *c; 356 357 if (!ops->target.addr || ops->target.offset < 0) 358 return ins__raw_scnprintf(ins, bf, size, ops); 359 360 if (ops->target.outside && ops->target.sym != NULL) 361 return scnprintf(bf, size, "%-6s %s", ins->name, ops->target.sym->name); 362 363 c = strchr(ops->raw, ','); 364 if (c != NULL) { 365 const char *c2 = strchr(c + 1, ','); 366 367 /* check for 3-op insn */ 368 if (c2 != NULL) 369 c = c2; 370 c++; 371 372 /* mirror arch objdump's space-after-comma style */ 373 if (*c == ' ') 374 c++; 375 } 376 377 return scnprintf(bf, size, "%-6s %.*s%" PRIx64, 378 ins->name, c ? c - ops->raw : 0, ops->raw, 379 ops->target.offset); 380 } 381 382 static struct ins_ops jump_ops = { 383 .parse = jump__parse, 384 .scnprintf = jump__scnprintf, 385 }; 386 387 bool ins__is_jump(const struct ins *ins) 388 { 389 return ins->ops == &jump_ops; 390 } 391 392 static int comment__symbol(char *raw, char *comment, u64 *addrp, char **namep) 393 { 394 char *endptr, *name, *t; 395 396 if (strstr(raw, "(%rip)") == NULL) 397 return 0; 398 399 *addrp = strtoull(comment, &endptr, 16); 400 if (endptr == comment) 401 return 0; 402 name = strchr(endptr, '<'); 403 if (name == NULL) 404 return -1; 405 406 name++; 407 408 t = strchr(name, '>'); 409 if (t == NULL) 410 return 0; 411 412 *t = '\0'; 413 *namep = strdup(name); 414 *t = '>'; 415 416 return 0; 417 } 418 419 static int lock__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms) 420 { 421 ops->locked.ops = zalloc(sizeof(*ops->locked.ops)); 422 if (ops->locked.ops == NULL) 423 return 0; 424 425 if (disasm_line__parse(ops->raw, &ops->locked.ins.name, &ops->locked.ops->raw) < 0) 426 goto out_free_ops; 427 428 ops->locked.ins.ops = ins__find(arch, ops->locked.ins.name); 429 430 if (ops->locked.ins.ops == NULL) 431 goto out_free_ops; 432 433 if (ops->locked.ins.ops->parse && 434 ops->locked.ins.ops->parse(arch, ops->locked.ops, ms) < 0) 435 goto out_free_ops; 436 437 return 0; 438 439 out_free_ops: 440 zfree(&ops->locked.ops); 441 return 0; 442 } 443 444 static int lock__scnprintf(struct ins *ins, char *bf, size_t size, 445 struct ins_operands *ops) 446 { 447 int printed; 448 449 if (ops->locked.ins.ops == NULL) 450 return ins__raw_scnprintf(ins, bf, size, ops); 451 452 printed = scnprintf(bf, size, "%-6s ", ins->name); 453 return printed + ins__scnprintf(&ops->locked.ins, bf + printed, 454 size - printed, ops->locked.ops); 455 } 456 457 static void lock__delete(struct ins_operands *ops) 458 { 459 struct ins *ins = &ops->locked.ins; 460 461 if (ins->ops && ins->ops->free) 462 ins->ops->free(ops->locked.ops); 463 else 464 ins__delete(ops->locked.ops); 465 466 zfree(&ops->locked.ops); 467 zfree(&ops->target.raw); 468 zfree(&ops->target.name); 469 } 470 471 static struct ins_ops lock_ops = { 472 .free = lock__delete, 473 .parse = lock__parse, 474 .scnprintf = lock__scnprintf, 475 }; 476 477 static int mov__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms __maybe_unused) 478 { 479 char *s = strchr(ops->raw, ','), *target, *comment, prev; 480 481 if (s == NULL) 482 return -1; 483 484 *s = '\0'; 485 ops->source.raw = strdup(ops->raw); 486 *s = ','; 487 488 if (ops->source.raw == NULL) 489 return -1; 490 491 target = ++s; 492 comment = strchr(s, arch->objdump.comment_char); 493 494 if (comment != NULL) 495 s = comment - 1; 496 else 497 s = strchr(s, '\0') - 1; 498 499 while (s > target && isspace(s[0])) 500 --s; 501 s++; 502 prev = *s; 503 *s = '\0'; 504 505 ops->target.raw = strdup(target); 506 *s = prev; 507 508 if (ops->target.raw == NULL) 509 goto out_free_source; 510 511 if (comment == NULL) 512 return 0; 513 514 comment = ltrim(comment); 515 comment__symbol(ops->source.raw, comment + 1, &ops->source.addr, &ops->source.name); 516 comment__symbol(ops->target.raw, comment + 1, &ops->target.addr, &ops->target.name); 517 518 return 0; 519 520 out_free_source: 521 zfree(&ops->source.raw); 522 return -1; 523 } 524 525 static int mov__scnprintf(struct ins *ins, char *bf, size_t size, 526 struct ins_operands *ops) 527 { 528 return scnprintf(bf, size, "%-6s %s,%s", ins->name, 529 ops->source.name ?: ops->source.raw, 530 ops->target.name ?: ops->target.raw); 531 } 532 533 static struct ins_ops mov_ops = { 534 .parse = mov__parse, 535 .scnprintf = mov__scnprintf, 536 }; 537 538 static int dec__parse(struct arch *arch __maybe_unused, struct ins_operands *ops, struct map_symbol *ms __maybe_unused) 539 { 540 char *target, *comment, *s, prev; 541 542 target = s = ops->raw; 543 544 while (s[0] != '\0' && !isspace(s[0])) 545 ++s; 546 prev = *s; 547 *s = '\0'; 548 549 ops->target.raw = strdup(target); 550 *s = prev; 551 552 if (ops->target.raw == NULL) 553 return -1; 554 555 comment = strchr(s, arch->objdump.comment_char); 556 if (comment == NULL) 557 return 0; 558 559 comment = ltrim(comment); 560 comment__symbol(ops->target.raw, comment + 1, &ops->target.addr, &ops->target.name); 561 562 return 0; 563 } 564 565 static int dec__scnprintf(struct ins *ins, char *bf, size_t size, 566 struct ins_operands *ops) 567 { 568 return scnprintf(bf, size, "%-6s %s", ins->name, 569 ops->target.name ?: ops->target.raw); 570 } 571 572 static struct ins_ops dec_ops = { 573 .parse = dec__parse, 574 .scnprintf = dec__scnprintf, 575 }; 576 577 static int nop__scnprintf(struct ins *ins __maybe_unused, char *bf, size_t size, 578 struct ins_operands *ops __maybe_unused) 579 { 580 return scnprintf(bf, size, "%-6s", "nop"); 581 } 582 583 static struct ins_ops nop_ops = { 584 .scnprintf = nop__scnprintf, 585 }; 586 587 static struct ins_ops ret_ops = { 588 .scnprintf = ins__raw_scnprintf, 589 }; 590 591 bool ins__is_ret(const struct ins *ins) 592 { 593 return ins->ops == &ret_ops; 594 } 595 596 bool ins__is_lock(const struct ins *ins) 597 { 598 return ins->ops == &lock_ops; 599 } 600 601 static int ins__key_cmp(const void *name, const void *insp) 602 { 603 const struct ins *ins = insp; 604 605 return strcmp(name, ins->name); 606 } 607 608 static int ins__cmp(const void *a, const void *b) 609 { 610 const struct ins *ia = a; 611 const struct ins *ib = b; 612 613 return strcmp(ia->name, ib->name); 614 } 615 616 static void ins__sort(struct arch *arch) 617 { 618 const int nmemb = arch->nr_instructions; 619 620 qsort(arch->instructions, nmemb, sizeof(struct ins), ins__cmp); 621 } 622 623 static struct ins_ops *__ins__find(struct arch *arch, const char *name) 624 { 625 struct ins *ins; 626 const int nmemb = arch->nr_instructions; 627 628 if (!arch->sorted_instructions) { 629 ins__sort(arch); 630 arch->sorted_instructions = true; 631 } 632 633 ins = bsearch(name, arch->instructions, nmemb, sizeof(struct ins), ins__key_cmp); 634 return ins ? ins->ops : NULL; 635 } 636 637 static struct ins_ops *ins__find(struct arch *arch, const char *name) 638 { 639 struct ins_ops *ops = __ins__find(arch, name); 640 641 if (!ops && arch->associate_instruction_ops) 642 ops = arch->associate_instruction_ops(arch, name); 643 644 return ops; 645 } 646 647 static int arch__key_cmp(const void *name, const void *archp) 648 { 649 const struct arch *arch = archp; 650 651 return strcmp(name, arch->name); 652 } 653 654 static int arch__cmp(const void *a, const void *b) 655 { 656 const struct arch *aa = a; 657 const struct arch *ab = b; 658 659 return strcmp(aa->name, ab->name); 660 } 661 662 static void arch__sort(void) 663 { 664 const int nmemb = ARRAY_SIZE(architectures); 665 666 qsort(architectures, nmemb, sizeof(struct arch), arch__cmp); 667 } 668 669 static struct arch *arch__find(const char *name) 670 { 671 const int nmemb = ARRAY_SIZE(architectures); 672 static bool sorted; 673 674 if (!sorted) { 675 arch__sort(); 676 sorted = true; 677 } 678 679 return bsearch(name, architectures, nmemb, sizeof(struct arch), arch__key_cmp); 680 } 681 682 static struct annotated_source *annotated_source__new(void) 683 { 684 struct annotated_source *src = zalloc(sizeof(*src)); 685 686 if (src != NULL) 687 INIT_LIST_HEAD(&src->source); 688 689 return src; 690 } 691 692 static __maybe_unused void annotated_source__delete(struct annotated_source *src) 693 { 694 if (src == NULL) 695 return; 696 zfree(&src->histograms); 697 zfree(&src->cycles_hist); 698 free(src); 699 } 700 701 static int annotated_source__alloc_histograms(struct annotated_source *src, 702 size_t size, int nr_hists) 703 { 704 size_t sizeof_sym_hist; 705 706 /* 707 * Add buffer of one element for zero length symbol. 708 * When sample is taken from first instruction of 709 * zero length symbol, perf still resolves it and 710 * shows symbol name in perf report and allows to 711 * annotate it. 712 */ 713 if (size == 0) 714 size = 1; 715 716 /* Check for overflow when calculating sizeof_sym_hist */ 717 if (size > (SIZE_MAX - sizeof(struct sym_hist)) / sizeof(struct sym_hist_entry)) 718 return -1; 719 720 sizeof_sym_hist = (sizeof(struct sym_hist) + size * sizeof(struct sym_hist_entry)); 721 722 /* Check for overflow in zalloc argument */ 723 if (sizeof_sym_hist > SIZE_MAX / nr_hists) 724 return -1; 725 726 src->sizeof_sym_hist = sizeof_sym_hist; 727 src->nr_histograms = nr_hists; 728 src->histograms = calloc(nr_hists, sizeof_sym_hist) ; 729 return src->histograms ? 0 : -1; 730 } 731 732 /* The cycles histogram is lazily allocated. */ 733 static int symbol__alloc_hist_cycles(struct symbol *sym) 734 { 735 struct annotation *notes = symbol__annotation(sym); 736 const size_t size = symbol__size(sym); 737 738 notes->src->cycles_hist = calloc(size, sizeof(struct cyc_hist)); 739 if (notes->src->cycles_hist == NULL) 740 return -1; 741 return 0; 742 } 743 744 void symbol__annotate_zero_histograms(struct symbol *sym) 745 { 746 struct annotation *notes = symbol__annotation(sym); 747 748 pthread_mutex_lock(¬es->lock); 749 if (notes->src != NULL) { 750 memset(notes->src->histograms, 0, 751 notes->src->nr_histograms * notes->src->sizeof_sym_hist); 752 if (notes->src->cycles_hist) 753 memset(notes->src->cycles_hist, 0, 754 symbol__size(sym) * sizeof(struct cyc_hist)); 755 } 756 pthread_mutex_unlock(¬es->lock); 757 } 758 759 static int __symbol__account_cycles(struct cyc_hist *ch, 760 u64 start, 761 unsigned offset, unsigned cycles, 762 unsigned have_start) 763 { 764 /* 765 * For now we can only account one basic block per 766 * final jump. But multiple could be overlapping. 767 * Always account the longest one. So when 768 * a shorter one has been already seen throw it away. 769 * 770 * We separately always account the full cycles. 771 */ 772 ch[offset].num_aggr++; 773 ch[offset].cycles_aggr += cycles; 774 775 if (cycles > ch[offset].cycles_max) 776 ch[offset].cycles_max = cycles; 777 778 if (ch[offset].cycles_min) { 779 if (cycles && cycles < ch[offset].cycles_min) 780 ch[offset].cycles_min = cycles; 781 } else 782 ch[offset].cycles_min = cycles; 783 784 if (!have_start && ch[offset].have_start) 785 return 0; 786 if (ch[offset].num) { 787 if (have_start && (!ch[offset].have_start || 788 ch[offset].start > start)) { 789 ch[offset].have_start = 0; 790 ch[offset].cycles = 0; 791 ch[offset].num = 0; 792 if (ch[offset].reset < 0xffff) 793 ch[offset].reset++; 794 } else if (have_start && 795 ch[offset].start < start) 796 return 0; 797 } 798 ch[offset].have_start = have_start; 799 ch[offset].start = start; 800 ch[offset].cycles += cycles; 801 ch[offset].num++; 802 return 0; 803 } 804 805 static int __symbol__inc_addr_samples(struct symbol *sym, struct map *map, 806 struct annotated_source *src, int evidx, u64 addr, 807 struct perf_sample *sample) 808 { 809 unsigned offset; 810 struct sym_hist *h; 811 812 pr_debug3("%s: addr=%#" PRIx64 "\n", __func__, map->unmap_ip(map, addr)); 813 814 if ((addr < sym->start || addr >= sym->end) && 815 (addr != sym->end || sym->start != sym->end)) { 816 pr_debug("%s(%d): ERANGE! sym->name=%s, start=%#" PRIx64 ", addr=%#" PRIx64 ", end=%#" PRIx64 "\n", 817 __func__, __LINE__, sym->name, sym->start, addr, sym->end); 818 return -ERANGE; 819 } 820 821 offset = addr - sym->start; 822 h = annotated_source__histogram(src, evidx); 823 h->nr_samples++; 824 h->addr[offset].nr_samples++; 825 h->period += sample->period; 826 h->addr[offset].period += sample->period; 827 828 pr_debug3("%#" PRIx64 " %s: period++ [addr: %#" PRIx64 ", %#" PRIx64 829 ", evidx=%d] => nr_samples: %" PRIu64 ", period: %" PRIu64 "\n", 830 sym->start, sym->name, addr, addr - sym->start, evidx, 831 h->addr[offset].nr_samples, h->addr[offset].period); 832 return 0; 833 } 834 835 static struct cyc_hist *symbol__cycles_hist(struct symbol *sym) 836 { 837 struct annotation *notes = symbol__annotation(sym); 838 839 if (notes->src == NULL) { 840 notes->src = annotated_source__new(); 841 if (notes->src == NULL) 842 return NULL; 843 goto alloc_cycles_hist; 844 } 845 846 if (!notes->src->cycles_hist) { 847 alloc_cycles_hist: 848 symbol__alloc_hist_cycles(sym); 849 } 850 851 return notes->src->cycles_hist; 852 } 853 854 struct annotated_source *symbol__hists(struct symbol *sym, int nr_hists) 855 { 856 struct annotation *notes = symbol__annotation(sym); 857 858 if (notes->src == NULL) { 859 notes->src = annotated_source__new(); 860 if (notes->src == NULL) 861 return NULL; 862 goto alloc_histograms; 863 } 864 865 if (notes->src->histograms == NULL) { 866 alloc_histograms: 867 annotated_source__alloc_histograms(notes->src, symbol__size(sym), 868 nr_hists); 869 } 870 871 return notes->src; 872 } 873 874 static int symbol__inc_addr_samples(struct symbol *sym, struct map *map, 875 struct perf_evsel *evsel, u64 addr, 876 struct perf_sample *sample) 877 { 878 struct annotated_source *src; 879 880 if (sym == NULL) 881 return 0; 882 src = symbol__hists(sym, evsel->evlist->nr_entries); 883 if (src == NULL) 884 return -ENOMEM; 885 return __symbol__inc_addr_samples(sym, map, src, evsel->idx, addr, sample); 886 } 887 888 static int symbol__account_cycles(u64 addr, u64 start, 889 struct symbol *sym, unsigned cycles) 890 { 891 struct cyc_hist *cycles_hist; 892 unsigned offset; 893 894 if (sym == NULL) 895 return 0; 896 cycles_hist = symbol__cycles_hist(sym); 897 if (cycles_hist == NULL) 898 return -ENOMEM; 899 if (addr < sym->start || addr >= sym->end) 900 return -ERANGE; 901 902 if (start) { 903 if (start < sym->start || start >= sym->end) 904 return -ERANGE; 905 if (start >= addr) 906 start = 0; 907 } 908 offset = addr - sym->start; 909 return __symbol__account_cycles(cycles_hist, 910 start ? start - sym->start : 0, 911 offset, cycles, 912 !!start); 913 } 914 915 int addr_map_symbol__account_cycles(struct addr_map_symbol *ams, 916 struct addr_map_symbol *start, 917 unsigned cycles) 918 { 919 u64 saddr = 0; 920 int err; 921 922 if (!cycles) 923 return 0; 924 925 /* 926 * Only set start when IPC can be computed. We can only 927 * compute it when the basic block is completely in a single 928 * function. 929 * Special case the case when the jump is elsewhere, but 930 * it starts on the function start. 931 */ 932 if (start && 933 (start->sym == ams->sym || 934 (ams->sym && 935 start->addr == ams->sym->start + ams->map->start))) 936 saddr = start->al_addr; 937 if (saddr == 0) 938 pr_debug2("BB with bad start: addr %"PRIx64" start %"PRIx64" sym %"PRIx64" saddr %"PRIx64"\n", 939 ams->addr, 940 start ? start->addr : 0, 941 ams->sym ? ams->sym->start + ams->map->start : 0, 942 saddr); 943 err = symbol__account_cycles(ams->al_addr, saddr, ams->sym, cycles); 944 if (err) 945 pr_debug2("account_cycles failed %d\n", err); 946 return err; 947 } 948 949 static unsigned annotation__count_insn(struct annotation *notes, u64 start, u64 end) 950 { 951 unsigned n_insn = 0; 952 u64 offset; 953 954 for (offset = start; offset <= end; offset++) { 955 if (notes->offsets[offset]) 956 n_insn++; 957 } 958 return n_insn; 959 } 960 961 static void annotation__count_and_fill(struct annotation *notes, u64 start, u64 end, struct cyc_hist *ch) 962 { 963 unsigned n_insn; 964 u64 offset; 965 966 n_insn = annotation__count_insn(notes, start, end); 967 if (n_insn && ch->num && ch->cycles) { 968 float ipc = n_insn / ((double)ch->cycles / (double)ch->num); 969 970 /* Hide data when there are too many overlaps. */ 971 if (ch->reset >= 0x7fff || ch->reset >= ch->num / 2) 972 return; 973 974 for (offset = start; offset <= end; offset++) { 975 struct annotation_line *al = notes->offsets[offset]; 976 977 if (al) 978 al->ipc = ipc; 979 } 980 } 981 } 982 983 void annotation__compute_ipc(struct annotation *notes, size_t size) 984 { 985 u64 offset; 986 987 if (!notes->src || !notes->src->cycles_hist) 988 return; 989 990 pthread_mutex_lock(¬es->lock); 991 for (offset = 0; offset < size; ++offset) { 992 struct cyc_hist *ch; 993 994 ch = ¬es->src->cycles_hist[offset]; 995 if (ch && ch->cycles) { 996 struct annotation_line *al; 997 998 if (ch->have_start) 999 annotation__count_and_fill(notes, ch->start, offset, ch); 1000 al = notes->offsets[offset]; 1001 if (al && ch->num_aggr) { 1002 al->cycles = ch->cycles_aggr / ch->num_aggr; 1003 al->cycles_max = ch->cycles_max; 1004 al->cycles_min = ch->cycles_min; 1005 } 1006 notes->have_cycles = true; 1007 } 1008 } 1009 pthread_mutex_unlock(¬es->lock); 1010 } 1011 1012 int addr_map_symbol__inc_samples(struct addr_map_symbol *ams, struct perf_sample *sample, 1013 struct perf_evsel *evsel) 1014 { 1015 return symbol__inc_addr_samples(ams->sym, ams->map, evsel, ams->al_addr, sample); 1016 } 1017 1018 int hist_entry__inc_addr_samples(struct hist_entry *he, struct perf_sample *sample, 1019 struct perf_evsel *evsel, u64 ip) 1020 { 1021 return symbol__inc_addr_samples(he->ms.sym, he->ms.map, evsel, ip, sample); 1022 } 1023 1024 static void disasm_line__init_ins(struct disasm_line *dl, struct arch *arch, struct map_symbol *ms) 1025 { 1026 dl->ins.ops = ins__find(arch, dl->ins.name); 1027 1028 if (!dl->ins.ops) 1029 return; 1030 1031 if (dl->ins.ops->parse && dl->ins.ops->parse(arch, &dl->ops, ms) < 0) 1032 dl->ins.ops = NULL; 1033 } 1034 1035 static int disasm_line__parse(char *line, const char **namep, char **rawp) 1036 { 1037 char tmp, *name = ltrim(line); 1038 1039 if (name[0] == '\0') 1040 return -1; 1041 1042 *rawp = name + 1; 1043 1044 while ((*rawp)[0] != '\0' && !isspace((*rawp)[0])) 1045 ++*rawp; 1046 1047 tmp = (*rawp)[0]; 1048 (*rawp)[0] = '\0'; 1049 *namep = strdup(name); 1050 1051 if (*namep == NULL) 1052 goto out_free_name; 1053 1054 (*rawp)[0] = tmp; 1055 *rawp = ltrim(*rawp); 1056 1057 return 0; 1058 1059 out_free_name: 1060 free((void *)namep); 1061 *namep = NULL; 1062 return -1; 1063 } 1064 1065 struct annotate_args { 1066 size_t privsize; 1067 struct arch *arch; 1068 struct map_symbol ms; 1069 struct perf_evsel *evsel; 1070 s64 offset; 1071 char *line; 1072 int line_nr; 1073 }; 1074 1075 static void annotation_line__delete(struct annotation_line *al) 1076 { 1077 void *ptr = (void *) al - al->privsize; 1078 1079 free_srcline(al->path); 1080 zfree(&al->line); 1081 free(ptr); 1082 } 1083 1084 /* 1085 * Allocating the annotation line data with following 1086 * structure: 1087 * 1088 * -------------------------------------- 1089 * private space | struct annotation_line 1090 * -------------------------------------- 1091 * 1092 * Size of the private space is stored in 'struct annotation_line'. 1093 * 1094 */ 1095 static struct annotation_line * 1096 annotation_line__new(struct annotate_args *args, size_t privsize) 1097 { 1098 struct annotation_line *al; 1099 struct perf_evsel *evsel = args->evsel; 1100 size_t size = privsize + sizeof(*al); 1101 int nr = 1; 1102 1103 if (perf_evsel__is_group_event(evsel)) 1104 nr = evsel->nr_members; 1105 1106 size += sizeof(al->samples[0]) * nr; 1107 1108 al = zalloc(size); 1109 if (al) { 1110 al = (void *) al + privsize; 1111 al->privsize = privsize; 1112 al->offset = args->offset; 1113 al->line = strdup(args->line); 1114 al->line_nr = args->line_nr; 1115 al->samples_nr = nr; 1116 } 1117 1118 return al; 1119 } 1120 1121 /* 1122 * Allocating the disasm annotation line data with 1123 * following structure: 1124 * 1125 * ------------------------------------------------------------ 1126 * privsize space | struct disasm_line | struct annotation_line 1127 * ------------------------------------------------------------ 1128 * 1129 * We have 'struct annotation_line' member as last member 1130 * of 'struct disasm_line' to have an easy access. 1131 * 1132 */ 1133 static struct disasm_line *disasm_line__new(struct annotate_args *args) 1134 { 1135 struct disasm_line *dl = NULL; 1136 struct annotation_line *al; 1137 size_t privsize = args->privsize + offsetof(struct disasm_line, al); 1138 1139 al = annotation_line__new(args, privsize); 1140 if (al != NULL) { 1141 dl = disasm_line(al); 1142 1143 if (dl->al.line == NULL) 1144 goto out_delete; 1145 1146 if (args->offset != -1) { 1147 if (disasm_line__parse(dl->al.line, &dl->ins.name, &dl->ops.raw) < 0) 1148 goto out_free_line; 1149 1150 disasm_line__init_ins(dl, args->arch, &args->ms); 1151 } 1152 } 1153 1154 return dl; 1155 1156 out_free_line: 1157 zfree(&dl->al.line); 1158 out_delete: 1159 free(dl); 1160 return NULL; 1161 } 1162 1163 void disasm_line__free(struct disasm_line *dl) 1164 { 1165 if (dl->ins.ops && dl->ins.ops->free) 1166 dl->ins.ops->free(&dl->ops); 1167 else 1168 ins__delete(&dl->ops); 1169 free((void *)dl->ins.name); 1170 dl->ins.name = NULL; 1171 annotation_line__delete(&dl->al); 1172 } 1173 1174 int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size, bool raw) 1175 { 1176 if (raw || !dl->ins.ops) 1177 return scnprintf(bf, size, "%-6s %s", dl->ins.name, dl->ops.raw); 1178 1179 return ins__scnprintf(&dl->ins, bf, size, &dl->ops); 1180 } 1181 1182 static void annotation_line__add(struct annotation_line *al, struct list_head *head) 1183 { 1184 list_add_tail(&al->node, head); 1185 } 1186 1187 struct annotation_line * 1188 annotation_line__next(struct annotation_line *pos, struct list_head *head) 1189 { 1190 list_for_each_entry_continue(pos, head, node) 1191 if (pos->offset >= 0) 1192 return pos; 1193 1194 return NULL; 1195 } 1196 1197 static const char *annotate__address_color(struct block_range *br) 1198 { 1199 double cov = block_range__coverage(br); 1200 1201 if (cov >= 0) { 1202 /* mark red for >75% coverage */ 1203 if (cov > 0.75) 1204 return PERF_COLOR_RED; 1205 1206 /* mark dull for <1% coverage */ 1207 if (cov < 0.01) 1208 return PERF_COLOR_NORMAL; 1209 } 1210 1211 return PERF_COLOR_MAGENTA; 1212 } 1213 1214 static const char *annotate__asm_color(struct block_range *br) 1215 { 1216 double cov = block_range__coverage(br); 1217 1218 if (cov >= 0) { 1219 /* mark dull for <1% coverage */ 1220 if (cov < 0.01) 1221 return PERF_COLOR_NORMAL; 1222 } 1223 1224 return PERF_COLOR_BLUE; 1225 } 1226 1227 static void annotate__branch_printf(struct block_range *br, u64 addr) 1228 { 1229 bool emit_comment = true; 1230 1231 if (!br) 1232 return; 1233 1234 #if 1 1235 if (br->is_target && br->start == addr) { 1236 struct block_range *branch = br; 1237 double p; 1238 1239 /* 1240 * Find matching branch to our target. 1241 */ 1242 while (!branch->is_branch) 1243 branch = block_range__next(branch); 1244 1245 p = 100 *(double)br->entry / branch->coverage; 1246 1247 if (p > 0.1) { 1248 if (emit_comment) { 1249 emit_comment = false; 1250 printf("\t#"); 1251 } 1252 1253 /* 1254 * The percentage of coverage joined at this target in relation 1255 * to the next branch. 1256 */ 1257 printf(" +%.2f%%", p); 1258 } 1259 } 1260 #endif 1261 if (br->is_branch && br->end == addr) { 1262 double p = 100*(double)br->taken / br->coverage; 1263 1264 if (p > 0.1) { 1265 if (emit_comment) { 1266 emit_comment = false; 1267 printf("\t#"); 1268 } 1269 1270 /* 1271 * The percentage of coverage leaving at this branch, and 1272 * its prediction ratio. 1273 */ 1274 printf(" -%.2f%% (p:%.2f%%)", p, 100*(double)br->pred / br->taken); 1275 } 1276 } 1277 } 1278 1279 static int disasm_line__print(struct disasm_line *dl, u64 start, int addr_fmt_width) 1280 { 1281 s64 offset = dl->al.offset; 1282 const u64 addr = start + offset; 1283 struct block_range *br; 1284 1285 br = block_range__find(addr); 1286 color_fprintf(stdout, annotate__address_color(br), " %*" PRIx64 ":", addr_fmt_width, addr); 1287 color_fprintf(stdout, annotate__asm_color(br), "%s", dl->al.line); 1288 annotate__branch_printf(br, addr); 1289 return 0; 1290 } 1291 1292 static int 1293 annotation_line__print(struct annotation_line *al, struct symbol *sym, u64 start, 1294 struct perf_evsel *evsel, u64 len, int min_pcnt, int printed, 1295 int max_lines, struct annotation_line *queue, int addr_fmt_width) 1296 { 1297 struct disasm_line *dl = container_of(al, struct disasm_line, al); 1298 static const char *prev_line; 1299 static const char *prev_color; 1300 1301 if (al->offset != -1) { 1302 double max_percent = 0.0; 1303 int i, nr_percent = 1; 1304 const char *color; 1305 struct annotation *notes = symbol__annotation(sym); 1306 1307 for (i = 0; i < al->samples_nr; i++) { 1308 struct annotation_data *sample = &al->samples[i]; 1309 1310 if (sample->percent > max_percent) 1311 max_percent = sample->percent; 1312 } 1313 1314 if (al->samples_nr > nr_percent) 1315 nr_percent = al->samples_nr; 1316 1317 if (max_percent < min_pcnt) 1318 return -1; 1319 1320 if (max_lines && printed >= max_lines) 1321 return 1; 1322 1323 if (queue != NULL) { 1324 list_for_each_entry_from(queue, ¬es->src->source, node) { 1325 if (queue == al) 1326 break; 1327 annotation_line__print(queue, sym, start, evsel, len, 1328 0, 0, 1, NULL, addr_fmt_width); 1329 } 1330 } 1331 1332 color = get_percent_color(max_percent); 1333 1334 /* 1335 * Also color the filename and line if needed, with 1336 * the same color than the percentage. Don't print it 1337 * twice for close colored addr with the same filename:line 1338 */ 1339 if (al->path) { 1340 if (!prev_line || strcmp(prev_line, al->path) 1341 || color != prev_color) { 1342 color_fprintf(stdout, color, " %s", al->path); 1343 prev_line = al->path; 1344 prev_color = color; 1345 } 1346 } 1347 1348 for (i = 0; i < nr_percent; i++) { 1349 struct annotation_data *sample = &al->samples[i]; 1350 1351 color = get_percent_color(sample->percent); 1352 1353 if (symbol_conf.show_total_period) 1354 color_fprintf(stdout, color, " %11" PRIu64, 1355 sample->he.period); 1356 else if (symbol_conf.show_nr_samples) 1357 color_fprintf(stdout, color, " %7" PRIu64, 1358 sample->he.nr_samples); 1359 else 1360 color_fprintf(stdout, color, " %7.2f", sample->percent); 1361 } 1362 1363 printf(" : "); 1364 1365 disasm_line__print(dl, start, addr_fmt_width); 1366 printf("\n"); 1367 } else if (max_lines && printed >= max_lines) 1368 return 1; 1369 else { 1370 int width = symbol_conf.show_total_period ? 12 : 8; 1371 1372 if (queue) 1373 return -1; 1374 1375 if (perf_evsel__is_group_event(evsel)) 1376 width *= evsel->nr_members; 1377 1378 if (!*al->line) 1379 printf(" %*s:\n", width, " "); 1380 else 1381 printf(" %*s: %*s %s\n", width, " ", addr_fmt_width, " ", al->line); 1382 } 1383 1384 return 0; 1385 } 1386 1387 /* 1388 * symbol__parse_objdump_line() parses objdump output (with -d --no-show-raw) 1389 * which looks like following 1390 * 1391 * 0000000000415500 <_init>: 1392 * 415500: sub $0x8,%rsp 1393 * 415504: mov 0x2f5ad5(%rip),%rax # 70afe0 <_DYNAMIC+0x2f8> 1394 * 41550b: test %rax,%rax 1395 * 41550e: je 415515 <_init+0x15> 1396 * 415510: callq 416e70 <__gmon_start__@plt> 1397 * 415515: add $0x8,%rsp 1398 * 415519: retq 1399 * 1400 * it will be parsed and saved into struct disasm_line as 1401 * <offset> <name> <ops.raw> 1402 * 1403 * The offset will be a relative offset from the start of the symbol and -1 1404 * means that it's not a disassembly line so should be treated differently. 1405 * The ops.raw part will be parsed further according to type of the instruction. 1406 */ 1407 static int symbol__parse_objdump_line(struct symbol *sym, FILE *file, 1408 struct annotate_args *args, 1409 int *line_nr) 1410 { 1411 struct map *map = args->ms.map; 1412 struct annotation *notes = symbol__annotation(sym); 1413 struct disasm_line *dl; 1414 char *line = NULL, *parsed_line, *tmp, *tmp2; 1415 size_t line_len; 1416 s64 line_ip, offset = -1; 1417 regmatch_t match[2]; 1418 1419 if (getline(&line, &line_len, file) < 0) 1420 return -1; 1421 1422 if (!line) 1423 return -1; 1424 1425 line_ip = -1; 1426 parsed_line = rtrim(line); 1427 1428 /* /filename:linenr ? Save line number and ignore. */ 1429 if (regexec(&file_lineno, parsed_line, 2, match, 0) == 0) { 1430 *line_nr = atoi(parsed_line + match[1].rm_so); 1431 return 0; 1432 } 1433 1434 tmp = ltrim(parsed_line); 1435 if (*tmp) { 1436 /* 1437 * Parse hexa addresses followed by ':' 1438 */ 1439 line_ip = strtoull(tmp, &tmp2, 16); 1440 if (*tmp2 != ':' || tmp == tmp2 || tmp2[1] == '\0') 1441 line_ip = -1; 1442 } 1443 1444 if (line_ip != -1) { 1445 u64 start = map__rip_2objdump(map, sym->start), 1446 end = map__rip_2objdump(map, sym->end); 1447 1448 offset = line_ip - start; 1449 if ((u64)line_ip < start || (u64)line_ip >= end) 1450 offset = -1; 1451 else 1452 parsed_line = tmp2 + 1; 1453 } 1454 1455 args->offset = offset; 1456 args->line = parsed_line; 1457 args->line_nr = *line_nr; 1458 args->ms.sym = sym; 1459 1460 dl = disasm_line__new(args); 1461 free(line); 1462 (*line_nr)++; 1463 1464 if (dl == NULL) 1465 return -1; 1466 1467 if (!disasm_line__has_local_offset(dl)) { 1468 dl->ops.target.offset = dl->ops.target.addr - 1469 map__rip_2objdump(map, sym->start); 1470 dl->ops.target.offset_avail = true; 1471 } 1472 1473 /* kcore has no symbols, so add the call target symbol */ 1474 if (dl->ins.ops && ins__is_call(&dl->ins) && !dl->ops.target.sym) { 1475 struct addr_map_symbol target = { 1476 .map = map, 1477 .addr = dl->ops.target.addr, 1478 }; 1479 1480 if (!map_groups__find_ams(&target) && 1481 target.sym->start == target.al_addr) 1482 dl->ops.target.sym = target.sym; 1483 } 1484 1485 annotation_line__add(&dl->al, ¬es->src->source); 1486 1487 return 0; 1488 } 1489 1490 static __attribute__((constructor)) void symbol__init_regexpr(void) 1491 { 1492 regcomp(&file_lineno, "^/[^:]+:([0-9]+)", REG_EXTENDED); 1493 } 1494 1495 static void delete_last_nop(struct symbol *sym) 1496 { 1497 struct annotation *notes = symbol__annotation(sym); 1498 struct list_head *list = ¬es->src->source; 1499 struct disasm_line *dl; 1500 1501 while (!list_empty(list)) { 1502 dl = list_entry(list->prev, struct disasm_line, al.node); 1503 1504 if (dl->ins.ops) { 1505 if (dl->ins.ops != &nop_ops) 1506 return; 1507 } else { 1508 if (!strstr(dl->al.line, " nop ") && 1509 !strstr(dl->al.line, " nopl ") && 1510 !strstr(dl->al.line, " nopw ")) 1511 return; 1512 } 1513 1514 list_del(&dl->al.node); 1515 disasm_line__free(dl); 1516 } 1517 } 1518 1519 int symbol__strerror_disassemble(struct symbol *sym __maybe_unused, struct map *map, 1520 int errnum, char *buf, size_t buflen) 1521 { 1522 struct dso *dso = map->dso; 1523 1524 BUG_ON(buflen == 0); 1525 1526 if (errnum >= 0) { 1527 str_error_r(errnum, buf, buflen); 1528 return 0; 1529 } 1530 1531 switch (errnum) { 1532 case SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX: { 1533 char bf[SBUILD_ID_SIZE + 15] = " with build id "; 1534 char *build_id_msg = NULL; 1535 1536 if (dso->has_build_id) { 1537 build_id__sprintf(dso->build_id, 1538 sizeof(dso->build_id), bf + 15); 1539 build_id_msg = bf; 1540 } 1541 scnprintf(buf, buflen, 1542 "No vmlinux file%s\nwas found in the path.\n\n" 1543 "Note that annotation using /proc/kcore requires CAP_SYS_RAWIO capability.\n\n" 1544 "Please use:\n\n" 1545 " perf buildid-cache -vu vmlinux\n\n" 1546 "or:\n\n" 1547 " --vmlinux vmlinux\n", build_id_msg ?: ""); 1548 } 1549 break; 1550 default: 1551 scnprintf(buf, buflen, "Internal error: Invalid %d error code\n", errnum); 1552 break; 1553 } 1554 1555 return 0; 1556 } 1557 1558 static int dso__disassemble_filename(struct dso *dso, char *filename, size_t filename_size) 1559 { 1560 char linkname[PATH_MAX]; 1561 char *build_id_filename; 1562 char *build_id_path = NULL; 1563 char *pos; 1564 1565 if (dso->symtab_type == DSO_BINARY_TYPE__KALLSYMS && 1566 !dso__is_kcore(dso)) 1567 return SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX; 1568 1569 build_id_filename = dso__build_id_filename(dso, NULL, 0, false); 1570 if (build_id_filename) { 1571 __symbol__join_symfs(filename, filename_size, build_id_filename); 1572 free(build_id_filename); 1573 } else { 1574 if (dso->has_build_id) 1575 return ENOMEM; 1576 goto fallback; 1577 } 1578 1579 build_id_path = strdup(filename); 1580 if (!build_id_path) 1581 return -1; 1582 1583 /* 1584 * old style build-id cache has name of XX/XXXXXXX.. while 1585 * new style has XX/XXXXXXX../{elf,kallsyms,vdso}. 1586 * extract the build-id part of dirname in the new style only. 1587 */ 1588 pos = strrchr(build_id_path, '/'); 1589 if (pos && strlen(pos) < SBUILD_ID_SIZE - 2) 1590 dirname(build_id_path); 1591 1592 if (dso__is_kcore(dso) || 1593 readlink(build_id_path, linkname, sizeof(linkname)) < 0 || 1594 strstr(linkname, DSO__NAME_KALLSYMS) || 1595 access(filename, R_OK)) { 1596 fallback: 1597 /* 1598 * If we don't have build-ids or the build-id file isn't in the 1599 * cache, or is just a kallsyms file, well, lets hope that this 1600 * DSO is the same as when 'perf record' ran. 1601 */ 1602 __symbol__join_symfs(filename, filename_size, dso->long_name); 1603 } 1604 1605 free(build_id_path); 1606 return 0; 1607 } 1608 1609 static int symbol__disassemble(struct symbol *sym, struct annotate_args *args) 1610 { 1611 struct map *map = args->ms.map; 1612 struct dso *dso = map->dso; 1613 char *command; 1614 FILE *file; 1615 char symfs_filename[PATH_MAX]; 1616 struct kcore_extract kce; 1617 bool delete_extract = false; 1618 int stdout_fd[2]; 1619 int lineno = 0; 1620 int nline; 1621 pid_t pid; 1622 int err = dso__disassemble_filename(dso, symfs_filename, sizeof(symfs_filename)); 1623 1624 if (err) 1625 return err; 1626 1627 pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__, 1628 symfs_filename, sym->name, map->unmap_ip(map, sym->start), 1629 map->unmap_ip(map, sym->end)); 1630 1631 pr_debug("annotating [%p] %30s : [%p] %30s\n", 1632 dso, dso->long_name, sym, sym->name); 1633 1634 if (dso__is_kcore(dso)) { 1635 kce.kcore_filename = symfs_filename; 1636 kce.addr = map__rip_2objdump(map, sym->start); 1637 kce.offs = sym->start; 1638 kce.len = sym->end - sym->start; 1639 if (!kcore_extract__create(&kce)) { 1640 delete_extract = true; 1641 strlcpy(symfs_filename, kce.extract_filename, 1642 sizeof(symfs_filename)); 1643 } 1644 } else if (dso__needs_decompress(dso)) { 1645 char tmp[KMOD_DECOMP_LEN]; 1646 1647 if (dso__decompress_kmodule_path(dso, symfs_filename, 1648 tmp, sizeof(tmp)) < 0) 1649 goto out; 1650 1651 strcpy(symfs_filename, tmp); 1652 } 1653 1654 err = asprintf(&command, 1655 "%s %s%s --start-address=0x%016" PRIx64 1656 " --stop-address=0x%016" PRIx64 1657 " -l -d %s %s -C \"%s\" 2>/dev/null|grep -v \"%s:\"|expand", 1658 objdump_path ? objdump_path : "objdump", 1659 disassembler_style ? "-M " : "", 1660 disassembler_style ? disassembler_style : "", 1661 map__rip_2objdump(map, sym->start), 1662 map__rip_2objdump(map, sym->end), 1663 symbol_conf.annotate_asm_raw ? "" : "--no-show-raw", 1664 symbol_conf.annotate_src ? "-S" : "", 1665 symfs_filename, symfs_filename); 1666 1667 if (err < 0) { 1668 pr_err("Failure allocating memory for the command to run\n"); 1669 goto out_remove_tmp; 1670 } 1671 1672 pr_debug("Executing: %s\n", command); 1673 1674 err = -1; 1675 if (pipe(stdout_fd) < 0) { 1676 pr_err("Failure creating the pipe to run %s\n", command); 1677 goto out_free_command; 1678 } 1679 1680 pid = fork(); 1681 if (pid < 0) { 1682 pr_err("Failure forking to run %s\n", command); 1683 goto out_close_stdout; 1684 } 1685 1686 if (pid == 0) { 1687 close(stdout_fd[0]); 1688 dup2(stdout_fd[1], 1); 1689 close(stdout_fd[1]); 1690 execl("/bin/sh", "sh", "-c", command, NULL); 1691 perror(command); 1692 exit(-1); 1693 } 1694 1695 close(stdout_fd[1]); 1696 1697 file = fdopen(stdout_fd[0], "r"); 1698 if (!file) { 1699 pr_err("Failure creating FILE stream for %s\n", command); 1700 /* 1701 * If we were using debug info should retry with 1702 * original binary. 1703 */ 1704 goto out_free_command; 1705 } 1706 1707 nline = 0; 1708 while (!feof(file)) { 1709 /* 1710 * The source code line number (lineno) needs to be kept in 1711 * accross calls to symbol__parse_objdump_line(), so that it 1712 * can associate it with the instructions till the next one. 1713 * See disasm_line__new() and struct disasm_line::line_nr. 1714 */ 1715 if (symbol__parse_objdump_line(sym, file, args, &lineno) < 0) 1716 break; 1717 nline++; 1718 } 1719 1720 if (nline == 0) 1721 pr_err("No output from %s\n", command); 1722 1723 /* 1724 * kallsyms does not have symbol sizes so there may a nop at the end. 1725 * Remove it. 1726 */ 1727 if (dso__is_kcore(dso)) 1728 delete_last_nop(sym); 1729 1730 fclose(file); 1731 err = 0; 1732 out_free_command: 1733 free(command); 1734 out_remove_tmp: 1735 close(stdout_fd[0]); 1736 1737 if (dso__needs_decompress(dso)) 1738 unlink(symfs_filename); 1739 1740 if (delete_extract) 1741 kcore_extract__delete(&kce); 1742 out: 1743 return err; 1744 1745 out_close_stdout: 1746 close(stdout_fd[1]); 1747 goto out_free_command; 1748 } 1749 1750 static void calc_percent(struct sym_hist *hist, 1751 struct annotation_data *sample, 1752 s64 offset, s64 end) 1753 { 1754 unsigned int hits = 0; 1755 u64 period = 0; 1756 1757 while (offset < end) { 1758 hits += hist->addr[offset].nr_samples; 1759 period += hist->addr[offset].period; 1760 ++offset; 1761 } 1762 1763 if (hist->nr_samples) { 1764 sample->he.period = period; 1765 sample->he.nr_samples = hits; 1766 sample->percent = 100.0 * hits / hist->nr_samples; 1767 } 1768 } 1769 1770 static void annotation__calc_percent(struct annotation *notes, 1771 struct perf_evsel *evsel, s64 len) 1772 { 1773 struct annotation_line *al, *next; 1774 1775 list_for_each_entry(al, ¬es->src->source, node) { 1776 s64 end; 1777 int i; 1778 1779 if (al->offset == -1) 1780 continue; 1781 1782 next = annotation_line__next(al, ¬es->src->source); 1783 end = next ? next->offset : len; 1784 1785 for (i = 0; i < al->samples_nr; i++) { 1786 struct annotation_data *sample; 1787 struct sym_hist *hist; 1788 1789 hist = annotation__histogram(notes, evsel->idx + i); 1790 sample = &al->samples[i]; 1791 1792 calc_percent(hist, sample, al->offset, end); 1793 } 1794 } 1795 } 1796 1797 void symbol__calc_percent(struct symbol *sym, struct perf_evsel *evsel) 1798 { 1799 struct annotation *notes = symbol__annotation(sym); 1800 1801 annotation__calc_percent(notes, evsel, symbol__size(sym)); 1802 } 1803 1804 int symbol__annotate(struct symbol *sym, struct map *map, 1805 struct perf_evsel *evsel, size_t privsize, 1806 struct arch **parch) 1807 { 1808 struct annotate_args args = { 1809 .privsize = privsize, 1810 .evsel = evsel, 1811 }; 1812 struct perf_env *env = perf_evsel__env(evsel); 1813 const char *arch_name = perf_env__arch(env); 1814 struct arch *arch; 1815 int err; 1816 1817 if (!arch_name) 1818 return -1; 1819 1820 args.arch = arch = arch__find(arch_name); 1821 if (arch == NULL) 1822 return -ENOTSUP; 1823 1824 if (parch) 1825 *parch = arch; 1826 1827 if (arch->init) { 1828 err = arch->init(arch, env ? env->cpuid : NULL); 1829 if (err) { 1830 pr_err("%s: failed to initialize %s arch priv area\n", __func__, arch->name); 1831 return err; 1832 } 1833 } 1834 1835 args.ms.map = map; 1836 args.ms.sym = sym; 1837 1838 return symbol__disassemble(sym, &args); 1839 } 1840 1841 static void insert_source_line(struct rb_root *root, struct annotation_line *al) 1842 { 1843 struct annotation_line *iter; 1844 struct rb_node **p = &root->rb_node; 1845 struct rb_node *parent = NULL; 1846 int i, ret; 1847 1848 while (*p != NULL) { 1849 parent = *p; 1850 iter = rb_entry(parent, struct annotation_line, rb_node); 1851 1852 ret = strcmp(iter->path, al->path); 1853 if (ret == 0) { 1854 for (i = 0; i < al->samples_nr; i++) 1855 iter->samples[i].percent_sum += al->samples[i].percent; 1856 return; 1857 } 1858 1859 if (ret < 0) 1860 p = &(*p)->rb_left; 1861 else 1862 p = &(*p)->rb_right; 1863 } 1864 1865 for (i = 0; i < al->samples_nr; i++) 1866 al->samples[i].percent_sum = al->samples[i].percent; 1867 1868 rb_link_node(&al->rb_node, parent, p); 1869 rb_insert_color(&al->rb_node, root); 1870 } 1871 1872 static int cmp_source_line(struct annotation_line *a, struct annotation_line *b) 1873 { 1874 int i; 1875 1876 for (i = 0; i < a->samples_nr; i++) { 1877 if (a->samples[i].percent_sum == b->samples[i].percent_sum) 1878 continue; 1879 return a->samples[i].percent_sum > b->samples[i].percent_sum; 1880 } 1881 1882 return 0; 1883 } 1884 1885 static void __resort_source_line(struct rb_root *root, struct annotation_line *al) 1886 { 1887 struct annotation_line *iter; 1888 struct rb_node **p = &root->rb_node; 1889 struct rb_node *parent = NULL; 1890 1891 while (*p != NULL) { 1892 parent = *p; 1893 iter = rb_entry(parent, struct annotation_line, rb_node); 1894 1895 if (cmp_source_line(al, iter)) 1896 p = &(*p)->rb_left; 1897 else 1898 p = &(*p)->rb_right; 1899 } 1900 1901 rb_link_node(&al->rb_node, parent, p); 1902 rb_insert_color(&al->rb_node, root); 1903 } 1904 1905 static void resort_source_line(struct rb_root *dest_root, struct rb_root *src_root) 1906 { 1907 struct annotation_line *al; 1908 struct rb_node *node; 1909 1910 node = rb_first(src_root); 1911 while (node) { 1912 struct rb_node *next; 1913 1914 al = rb_entry(node, struct annotation_line, rb_node); 1915 next = rb_next(node); 1916 rb_erase(node, src_root); 1917 1918 __resort_source_line(dest_root, al); 1919 node = next; 1920 } 1921 } 1922 1923 static void print_summary(struct rb_root *root, const char *filename) 1924 { 1925 struct annotation_line *al; 1926 struct rb_node *node; 1927 1928 printf("\nSorted summary for file %s\n", filename); 1929 printf("----------------------------------------------\n\n"); 1930 1931 if (RB_EMPTY_ROOT(root)) { 1932 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN); 1933 return; 1934 } 1935 1936 node = rb_first(root); 1937 while (node) { 1938 double percent, percent_max = 0.0; 1939 const char *color; 1940 char *path; 1941 int i; 1942 1943 al = rb_entry(node, struct annotation_line, rb_node); 1944 for (i = 0; i < al->samples_nr; i++) { 1945 percent = al->samples[i].percent_sum; 1946 color = get_percent_color(percent); 1947 color_fprintf(stdout, color, " %7.2f", percent); 1948 1949 if (percent > percent_max) 1950 percent_max = percent; 1951 } 1952 1953 path = al->path; 1954 color = get_percent_color(percent_max); 1955 color_fprintf(stdout, color, " %s\n", path); 1956 1957 node = rb_next(node); 1958 } 1959 } 1960 1961 static void symbol__annotate_hits(struct symbol *sym, struct perf_evsel *evsel) 1962 { 1963 struct annotation *notes = symbol__annotation(sym); 1964 struct sym_hist *h = annotation__histogram(notes, evsel->idx); 1965 u64 len = symbol__size(sym), offset; 1966 1967 for (offset = 0; offset < len; ++offset) 1968 if (h->addr[offset].nr_samples != 0) 1969 printf("%*" PRIx64 ": %" PRIu64 "\n", BITS_PER_LONG / 2, 1970 sym->start + offset, h->addr[offset].nr_samples); 1971 printf("%*s: %" PRIu64 "\n", BITS_PER_LONG / 2, "h->nr_samples", h->nr_samples); 1972 } 1973 1974 static int annotated_source__addr_fmt_width(struct list_head *lines, u64 start) 1975 { 1976 char bf[32]; 1977 struct annotation_line *line; 1978 1979 list_for_each_entry_reverse(line, lines, node) { 1980 if (line->offset != -1) 1981 return scnprintf(bf, sizeof(bf), "%" PRIx64, start + line->offset); 1982 } 1983 1984 return 0; 1985 } 1986 1987 int symbol__annotate_printf(struct symbol *sym, struct map *map, 1988 struct perf_evsel *evsel, 1989 struct annotation_options *opts) 1990 { 1991 struct dso *dso = map->dso; 1992 char *filename; 1993 const char *d_filename; 1994 const char *evsel_name = perf_evsel__name(evsel); 1995 struct annotation *notes = symbol__annotation(sym); 1996 struct sym_hist *h = annotation__histogram(notes, evsel->idx); 1997 struct annotation_line *pos, *queue = NULL; 1998 u64 start = map__rip_2objdump(map, sym->start); 1999 int printed = 2, queue_len = 0, addr_fmt_width; 2000 int more = 0; 2001 bool context = opts->context; 2002 u64 len; 2003 int width = symbol_conf.show_total_period ? 12 : 8; 2004 int graph_dotted_len; 2005 char buf[512]; 2006 2007 filename = strdup(dso->long_name); 2008 if (!filename) 2009 return -ENOMEM; 2010 2011 if (opts->full_path) 2012 d_filename = filename; 2013 else 2014 d_filename = basename(filename); 2015 2016 len = symbol__size(sym); 2017 2018 if (perf_evsel__is_group_event(evsel)) { 2019 width *= evsel->nr_members; 2020 perf_evsel__group_desc(evsel, buf, sizeof(buf)); 2021 evsel_name = buf; 2022 } 2023 2024 graph_dotted_len = printf(" %-*.*s| Source code & Disassembly of %s for %s (%" PRIu64 " samples)\n", 2025 width, width, symbol_conf.show_total_period ? "Period" : 2026 symbol_conf.show_nr_samples ? "Samples" : "Percent", 2027 d_filename, evsel_name, h->nr_samples); 2028 2029 printf("%-*.*s----\n", 2030 graph_dotted_len, graph_dotted_len, graph_dotted_line); 2031 2032 if (verbose > 0) 2033 symbol__annotate_hits(sym, evsel); 2034 2035 addr_fmt_width = annotated_source__addr_fmt_width(¬es->src->source, start); 2036 2037 list_for_each_entry(pos, ¬es->src->source, node) { 2038 int err; 2039 2040 if (context && queue == NULL) { 2041 queue = pos; 2042 queue_len = 0; 2043 } 2044 2045 err = annotation_line__print(pos, sym, start, evsel, len, 2046 opts->min_pcnt, printed, opts->max_lines, 2047 queue, addr_fmt_width); 2048 2049 switch (err) { 2050 case 0: 2051 ++printed; 2052 if (context) { 2053 printed += queue_len; 2054 queue = NULL; 2055 queue_len = 0; 2056 } 2057 break; 2058 case 1: 2059 /* filtered by max_lines */ 2060 ++more; 2061 break; 2062 case -1: 2063 default: 2064 /* 2065 * Filtered by min_pcnt or non IP lines when 2066 * context != 0 2067 */ 2068 if (!context) 2069 break; 2070 if (queue_len == context) 2071 queue = list_entry(queue->node.next, typeof(*queue), node); 2072 else 2073 ++queue_len; 2074 break; 2075 } 2076 } 2077 2078 free(filename); 2079 2080 return more; 2081 } 2082 2083 static void FILE__set_percent_color(void *fp __maybe_unused, 2084 double percent __maybe_unused, 2085 bool current __maybe_unused) 2086 { 2087 } 2088 2089 static int FILE__set_jumps_percent_color(void *fp __maybe_unused, 2090 int nr __maybe_unused, bool current __maybe_unused) 2091 { 2092 return 0; 2093 } 2094 2095 static int FILE__set_color(void *fp __maybe_unused, int color __maybe_unused) 2096 { 2097 return 0; 2098 } 2099 2100 static void FILE__printf(void *fp, const char *fmt, ...) 2101 { 2102 va_list args; 2103 2104 va_start(args, fmt); 2105 vfprintf(fp, fmt, args); 2106 va_end(args); 2107 } 2108 2109 static void FILE__write_graph(void *fp, int graph) 2110 { 2111 const char *s; 2112 switch (graph) { 2113 2114 case DARROW_CHAR: s = "↓"; break; 2115 case UARROW_CHAR: s = "↑"; break; 2116 case LARROW_CHAR: s = "←"; break; 2117 case RARROW_CHAR: s = "→"; break; 2118 default: s = "?"; break; 2119 } 2120 2121 fputs(s, fp); 2122 } 2123 2124 int symbol__annotate_fprintf2(struct symbol *sym, FILE *fp) 2125 { 2126 struct annotation *notes = symbol__annotation(sym); 2127 struct annotation_write_ops ops = { 2128 .first_line = true, 2129 .obj = fp, 2130 .set_color = FILE__set_color, 2131 .set_percent_color = FILE__set_percent_color, 2132 .set_jumps_percent_color = FILE__set_jumps_percent_color, 2133 .printf = FILE__printf, 2134 .write_graph = FILE__write_graph, 2135 }; 2136 struct annotation_line *al; 2137 2138 list_for_each_entry(al, ¬es->src->source, node) { 2139 if (annotation_line__filter(al, notes)) 2140 continue; 2141 annotation_line__write(al, notes, &ops); 2142 fputc('\n', fp); 2143 ops.first_line = false; 2144 } 2145 2146 return 0; 2147 } 2148 2149 int map_symbol__annotation_dump(struct map_symbol *ms, struct perf_evsel *evsel) 2150 { 2151 const char *ev_name = perf_evsel__name(evsel); 2152 char buf[1024]; 2153 char *filename; 2154 int err = -1; 2155 FILE *fp; 2156 2157 if (asprintf(&filename, "%s.annotation", ms->sym->name) < 0) 2158 return -1; 2159 2160 fp = fopen(filename, "w"); 2161 if (fp == NULL) 2162 goto out_free_filename; 2163 2164 if (perf_evsel__is_group_event(evsel)) { 2165 perf_evsel__group_desc(evsel, buf, sizeof(buf)); 2166 ev_name = buf; 2167 } 2168 2169 fprintf(fp, "%s() %s\nEvent: %s\n\n", 2170 ms->sym->name, ms->map->dso->long_name, ev_name); 2171 symbol__annotate_fprintf2(ms->sym, fp); 2172 2173 fclose(fp); 2174 err = 0; 2175 out_free_filename: 2176 free(filename); 2177 return err; 2178 } 2179 2180 void symbol__annotate_zero_histogram(struct symbol *sym, int evidx) 2181 { 2182 struct annotation *notes = symbol__annotation(sym); 2183 struct sym_hist *h = annotation__histogram(notes, evidx); 2184 2185 memset(h, 0, notes->src->sizeof_sym_hist); 2186 } 2187 2188 void symbol__annotate_decay_histogram(struct symbol *sym, int evidx) 2189 { 2190 struct annotation *notes = symbol__annotation(sym); 2191 struct sym_hist *h = annotation__histogram(notes, evidx); 2192 int len = symbol__size(sym), offset; 2193 2194 h->nr_samples = 0; 2195 for (offset = 0; offset < len; ++offset) { 2196 h->addr[offset].nr_samples = h->addr[offset].nr_samples * 7 / 8; 2197 h->nr_samples += h->addr[offset].nr_samples; 2198 } 2199 } 2200 2201 void annotated_source__purge(struct annotated_source *as) 2202 { 2203 struct annotation_line *al, *n; 2204 2205 list_for_each_entry_safe(al, n, &as->source, node) { 2206 list_del(&al->node); 2207 disasm_line__free(disasm_line(al)); 2208 } 2209 } 2210 2211 static size_t disasm_line__fprintf(struct disasm_line *dl, FILE *fp) 2212 { 2213 size_t printed; 2214 2215 if (dl->al.offset == -1) 2216 return fprintf(fp, "%s\n", dl->al.line); 2217 2218 printed = fprintf(fp, "%#" PRIx64 " %s", dl->al.offset, dl->ins.name); 2219 2220 if (dl->ops.raw[0] != '\0') { 2221 printed += fprintf(fp, "%.*s %s\n", 6 - (int)printed, " ", 2222 dl->ops.raw); 2223 } 2224 2225 return printed + fprintf(fp, "\n"); 2226 } 2227 2228 size_t disasm__fprintf(struct list_head *head, FILE *fp) 2229 { 2230 struct disasm_line *pos; 2231 size_t printed = 0; 2232 2233 list_for_each_entry(pos, head, al.node) 2234 printed += disasm_line__fprintf(pos, fp); 2235 2236 return printed; 2237 } 2238 2239 bool disasm_line__is_valid_local_jump(struct disasm_line *dl, struct symbol *sym) 2240 { 2241 if (!dl || !dl->ins.ops || !ins__is_jump(&dl->ins) || 2242 !disasm_line__has_local_offset(dl) || dl->ops.target.offset < 0 || 2243 dl->ops.target.offset >= (s64)symbol__size(sym)) 2244 return false; 2245 2246 return true; 2247 } 2248 2249 void annotation__mark_jump_targets(struct annotation *notes, struct symbol *sym) 2250 { 2251 u64 offset, size = symbol__size(sym); 2252 2253 /* PLT symbols contain external offsets */ 2254 if (strstr(sym->name, "@plt")) 2255 return; 2256 2257 for (offset = 0; offset < size; ++offset) { 2258 struct annotation_line *al = notes->offsets[offset]; 2259 struct disasm_line *dl; 2260 2261 dl = disasm_line(al); 2262 2263 if (!disasm_line__is_valid_local_jump(dl, sym)) 2264 continue; 2265 2266 al = notes->offsets[dl->ops.target.offset]; 2267 2268 /* 2269 * FIXME: Oops, no jump target? Buggy disassembler? Or do we 2270 * have to adjust to the previous offset? 2271 */ 2272 if (al == NULL) 2273 continue; 2274 2275 if (++al->jump_sources > notes->max_jump_sources) 2276 notes->max_jump_sources = al->jump_sources; 2277 2278 ++notes->nr_jumps; 2279 } 2280 } 2281 2282 void annotation__set_offsets(struct annotation *notes, s64 size) 2283 { 2284 struct annotation_line *al; 2285 2286 notes->max_line_len = 0; 2287 2288 list_for_each_entry(al, ¬es->src->source, node) { 2289 size_t line_len = strlen(al->line); 2290 2291 if (notes->max_line_len < line_len) 2292 notes->max_line_len = line_len; 2293 al->idx = notes->nr_entries++; 2294 if (al->offset != -1) { 2295 al->idx_asm = notes->nr_asm_entries++; 2296 /* 2297 * FIXME: short term bandaid to cope with assembly 2298 * routines that comes with labels in the same column 2299 * as the address in objdump, sigh. 2300 * 2301 * E.g. copy_user_generic_unrolled 2302 */ 2303 if (al->offset < size) 2304 notes->offsets[al->offset] = al; 2305 } else 2306 al->idx_asm = -1; 2307 } 2308 } 2309 2310 static inline int width_jumps(int n) 2311 { 2312 if (n >= 100) 2313 return 5; 2314 if (n / 10) 2315 return 2; 2316 return 1; 2317 } 2318 2319 void annotation__init_column_widths(struct annotation *notes, struct symbol *sym) 2320 { 2321 notes->widths.addr = notes->widths.target = 2322 notes->widths.min_addr = hex_width(symbol__size(sym)); 2323 notes->widths.max_addr = hex_width(sym->end); 2324 notes->widths.jumps = width_jumps(notes->max_jump_sources); 2325 } 2326 2327 void annotation__update_column_widths(struct annotation *notes) 2328 { 2329 if (notes->options->use_offset) 2330 notes->widths.target = notes->widths.min_addr; 2331 else 2332 notes->widths.target = notes->widths.max_addr; 2333 2334 notes->widths.addr = notes->widths.target; 2335 2336 if (notes->options->show_nr_jumps) 2337 notes->widths.addr += notes->widths.jumps + 1; 2338 } 2339 2340 static void annotation__calc_lines(struct annotation *notes, struct map *map, 2341 struct rb_root *root) 2342 { 2343 struct annotation_line *al; 2344 struct rb_root tmp_root = RB_ROOT; 2345 2346 list_for_each_entry(al, ¬es->src->source, node) { 2347 double percent_max = 0.0; 2348 int i; 2349 2350 for (i = 0; i < al->samples_nr; i++) { 2351 struct annotation_data *sample; 2352 2353 sample = &al->samples[i]; 2354 2355 if (sample->percent > percent_max) 2356 percent_max = sample->percent; 2357 } 2358 2359 if (percent_max <= 0.5) 2360 continue; 2361 2362 al->path = get_srcline(map->dso, notes->start + al->offset, NULL, 2363 false, true, notes->start + al->offset); 2364 insert_source_line(&tmp_root, al); 2365 } 2366 2367 resort_source_line(root, &tmp_root); 2368 } 2369 2370 static void symbol__calc_lines(struct symbol *sym, struct map *map, 2371 struct rb_root *root) 2372 { 2373 struct annotation *notes = symbol__annotation(sym); 2374 2375 annotation__calc_lines(notes, map, root); 2376 } 2377 2378 int symbol__tty_annotate2(struct symbol *sym, struct map *map, 2379 struct perf_evsel *evsel, 2380 struct annotation_options *opts) 2381 { 2382 struct dso *dso = map->dso; 2383 struct rb_root source_line = RB_ROOT; 2384 struct annotation *notes = symbol__annotation(sym); 2385 char buf[1024]; 2386 2387 if (symbol__annotate2(sym, map, evsel, opts, NULL) < 0) 2388 return -1; 2389 2390 if (opts->print_lines) { 2391 srcline_full_filename = opts->full_path; 2392 symbol__calc_lines(sym, map, &source_line); 2393 print_summary(&source_line, dso->long_name); 2394 } 2395 2396 annotation__scnprintf_samples_period(notes, buf, sizeof(buf), evsel); 2397 fprintf(stdout, "%s\n%s() %s\n", buf, sym->name, dso->long_name); 2398 symbol__annotate_fprintf2(sym, stdout); 2399 2400 annotated_source__purge(symbol__annotation(sym)->src); 2401 2402 return 0; 2403 } 2404 2405 int symbol__tty_annotate(struct symbol *sym, struct map *map, 2406 struct perf_evsel *evsel, 2407 struct annotation_options *opts) 2408 { 2409 struct dso *dso = map->dso; 2410 struct rb_root source_line = RB_ROOT; 2411 2412 if (symbol__annotate(sym, map, evsel, 0, NULL) < 0) 2413 return -1; 2414 2415 symbol__calc_percent(sym, evsel); 2416 2417 if (opts->print_lines) { 2418 srcline_full_filename = opts->full_path; 2419 symbol__calc_lines(sym, map, &source_line); 2420 print_summary(&source_line, dso->long_name); 2421 } 2422 2423 symbol__annotate_printf(sym, map, evsel, opts); 2424 2425 annotated_source__purge(symbol__annotation(sym)->src); 2426 2427 return 0; 2428 } 2429 2430 bool ui__has_annotation(void) 2431 { 2432 return use_browser == 1 && perf_hpp_list.sym; 2433 } 2434 2435 2436 double annotation_line__max_percent(struct annotation_line *al, struct annotation *notes) 2437 { 2438 double percent_max = 0.0; 2439 int i; 2440 2441 for (i = 0; i < notes->nr_events; i++) { 2442 if (al->samples[i].percent > percent_max) 2443 percent_max = al->samples[i].percent; 2444 } 2445 2446 return percent_max; 2447 } 2448 2449 static void disasm_line__write(struct disasm_line *dl, struct annotation *notes, 2450 void *obj, char *bf, size_t size, 2451 void (*obj__printf)(void *obj, const char *fmt, ...), 2452 void (*obj__write_graph)(void *obj, int graph)) 2453 { 2454 if (dl->ins.ops && dl->ins.ops->scnprintf) { 2455 if (ins__is_jump(&dl->ins)) { 2456 bool fwd; 2457 2458 if (dl->ops.target.outside) 2459 goto call_like; 2460 fwd = dl->ops.target.offset > dl->al.offset; 2461 obj__write_graph(obj, fwd ? DARROW_CHAR : UARROW_CHAR); 2462 obj__printf(obj, " "); 2463 } else if (ins__is_call(&dl->ins)) { 2464 call_like: 2465 obj__write_graph(obj, RARROW_CHAR); 2466 obj__printf(obj, " "); 2467 } else if (ins__is_ret(&dl->ins)) { 2468 obj__write_graph(obj, LARROW_CHAR); 2469 obj__printf(obj, " "); 2470 } else { 2471 obj__printf(obj, " "); 2472 } 2473 } else { 2474 obj__printf(obj, " "); 2475 } 2476 2477 disasm_line__scnprintf(dl, bf, size, !notes->options->use_offset); 2478 } 2479 2480 static void __annotation_line__write(struct annotation_line *al, struct annotation *notes, 2481 bool first_line, bool current_entry, bool change_color, int width, 2482 void *obj, 2483 int (*obj__set_color)(void *obj, int color), 2484 void (*obj__set_percent_color)(void *obj, double percent, bool current), 2485 int (*obj__set_jumps_percent_color)(void *obj, int nr, bool current), 2486 void (*obj__printf)(void *obj, const char *fmt, ...), 2487 void (*obj__write_graph)(void *obj, int graph)) 2488 2489 { 2490 double percent_max = annotation_line__max_percent(al, notes); 2491 int pcnt_width = annotation__pcnt_width(notes), 2492 cycles_width = annotation__cycles_width(notes); 2493 bool show_title = false; 2494 char bf[256]; 2495 int printed; 2496 2497 if (first_line && (al->offset == -1 || percent_max == 0.0)) { 2498 if (notes->have_cycles) { 2499 if (al->ipc == 0.0 && al->cycles == 0) 2500 show_title = true; 2501 } else 2502 show_title = true; 2503 } 2504 2505 if (al->offset != -1 && percent_max != 0.0) { 2506 int i; 2507 2508 for (i = 0; i < notes->nr_events; i++) { 2509 obj__set_percent_color(obj, al->samples[i].percent, current_entry); 2510 if (notes->options->show_total_period) { 2511 obj__printf(obj, "%11" PRIu64 " ", al->samples[i].he.period); 2512 } else if (notes->options->show_nr_samples) { 2513 obj__printf(obj, "%6" PRIu64 " ", 2514 al->samples[i].he.nr_samples); 2515 } else { 2516 obj__printf(obj, "%6.2f ", 2517 al->samples[i].percent); 2518 } 2519 } 2520 } else { 2521 obj__set_percent_color(obj, 0, current_entry); 2522 2523 if (!show_title) 2524 obj__printf(obj, "%-*s", pcnt_width, " "); 2525 else { 2526 obj__printf(obj, "%-*s", pcnt_width, 2527 notes->options->show_total_period ? "Period" : 2528 notes->options->show_nr_samples ? "Samples" : "Percent"); 2529 } 2530 } 2531 2532 if (notes->have_cycles) { 2533 if (al->ipc) 2534 obj__printf(obj, "%*.2f ", ANNOTATION__IPC_WIDTH - 1, al->ipc); 2535 else if (!show_title) 2536 obj__printf(obj, "%*s", ANNOTATION__IPC_WIDTH, " "); 2537 else 2538 obj__printf(obj, "%*s ", ANNOTATION__IPC_WIDTH - 1, "IPC"); 2539 2540 if (!notes->options->show_minmax_cycle) { 2541 if (al->cycles) 2542 obj__printf(obj, "%*" PRIu64 " ", 2543 ANNOTATION__CYCLES_WIDTH - 1, al->cycles); 2544 else if (!show_title) 2545 obj__printf(obj, "%*s", 2546 ANNOTATION__CYCLES_WIDTH, " "); 2547 else 2548 obj__printf(obj, "%*s ", 2549 ANNOTATION__CYCLES_WIDTH - 1, 2550 "Cycle"); 2551 } else { 2552 if (al->cycles) { 2553 char str[32]; 2554 2555 scnprintf(str, sizeof(str), 2556 "%" PRIu64 "(%" PRIu64 "/%" PRIu64 ")", 2557 al->cycles, al->cycles_min, 2558 al->cycles_max); 2559 2560 obj__printf(obj, "%*s ", 2561 ANNOTATION__MINMAX_CYCLES_WIDTH - 1, 2562 str); 2563 } else if (!show_title) 2564 obj__printf(obj, "%*s", 2565 ANNOTATION__MINMAX_CYCLES_WIDTH, 2566 " "); 2567 else 2568 obj__printf(obj, "%*s ", 2569 ANNOTATION__MINMAX_CYCLES_WIDTH - 1, 2570 "Cycle(min/max)"); 2571 } 2572 } 2573 2574 obj__printf(obj, " "); 2575 2576 if (!*al->line) 2577 obj__printf(obj, "%-*s", width - pcnt_width - cycles_width, " "); 2578 else if (al->offset == -1) { 2579 if (al->line_nr && notes->options->show_linenr) 2580 printed = scnprintf(bf, sizeof(bf), "%-*d ", notes->widths.addr + 1, al->line_nr); 2581 else 2582 printed = scnprintf(bf, sizeof(bf), "%-*s ", notes->widths.addr, " "); 2583 obj__printf(obj, bf); 2584 obj__printf(obj, "%-*s", width - printed - pcnt_width - cycles_width + 1, al->line); 2585 } else { 2586 u64 addr = al->offset; 2587 int color = -1; 2588 2589 if (!notes->options->use_offset) 2590 addr += notes->start; 2591 2592 if (!notes->options->use_offset) { 2593 printed = scnprintf(bf, sizeof(bf), "%" PRIx64 ": ", addr); 2594 } else { 2595 if (al->jump_sources && 2596 notes->options->offset_level >= ANNOTATION__OFFSET_JUMP_TARGETS) { 2597 if (notes->options->show_nr_jumps) { 2598 int prev; 2599 printed = scnprintf(bf, sizeof(bf), "%*d ", 2600 notes->widths.jumps, 2601 al->jump_sources); 2602 prev = obj__set_jumps_percent_color(obj, al->jump_sources, 2603 current_entry); 2604 obj__printf(obj, bf); 2605 obj__set_color(obj, prev); 2606 } 2607 print_addr: 2608 printed = scnprintf(bf, sizeof(bf), "%*" PRIx64 ": ", 2609 notes->widths.target, addr); 2610 } else if (ins__is_call(&disasm_line(al)->ins) && 2611 notes->options->offset_level >= ANNOTATION__OFFSET_CALL) { 2612 goto print_addr; 2613 } else if (notes->options->offset_level == ANNOTATION__MAX_OFFSET_LEVEL) { 2614 goto print_addr; 2615 } else { 2616 printed = scnprintf(bf, sizeof(bf), "%-*s ", 2617 notes->widths.addr, " "); 2618 } 2619 } 2620 2621 if (change_color) 2622 color = obj__set_color(obj, HE_COLORSET_ADDR); 2623 obj__printf(obj, bf); 2624 if (change_color) 2625 obj__set_color(obj, color); 2626 2627 disasm_line__write(disasm_line(al), notes, obj, bf, sizeof(bf), obj__printf, obj__write_graph); 2628 2629 obj__printf(obj, "%-*s", width - pcnt_width - cycles_width - 3 - printed, bf); 2630 } 2631 2632 } 2633 2634 void annotation_line__write(struct annotation_line *al, struct annotation *notes, 2635 struct annotation_write_ops *ops) 2636 { 2637 __annotation_line__write(al, notes, ops->first_line, ops->current_entry, 2638 ops->change_color, ops->width, ops->obj, 2639 ops->set_color, ops->set_percent_color, 2640 ops->set_jumps_percent_color, ops->printf, 2641 ops->write_graph); 2642 } 2643 2644 int symbol__annotate2(struct symbol *sym, struct map *map, struct perf_evsel *evsel, 2645 struct annotation_options *options, struct arch **parch) 2646 { 2647 struct annotation *notes = symbol__annotation(sym); 2648 size_t size = symbol__size(sym); 2649 int nr_pcnt = 1, err; 2650 2651 notes->offsets = zalloc(size * sizeof(struct annotation_line *)); 2652 if (notes->offsets == NULL) 2653 return -1; 2654 2655 if (perf_evsel__is_group_event(evsel)) 2656 nr_pcnt = evsel->nr_members; 2657 2658 err = symbol__annotate(sym, map, evsel, 0, parch); 2659 if (err) 2660 goto out_free_offsets; 2661 2662 notes->options = options; 2663 2664 symbol__calc_percent(sym, evsel); 2665 2666 notes->start = map__rip_2objdump(map, sym->start); 2667 2668 annotation__set_offsets(notes, size); 2669 annotation__mark_jump_targets(notes, sym); 2670 annotation__compute_ipc(notes, size); 2671 annotation__init_column_widths(notes, sym); 2672 notes->nr_events = nr_pcnt; 2673 2674 annotation__update_column_widths(notes); 2675 2676 return 0; 2677 2678 out_free_offsets: 2679 zfree(¬es->offsets); 2680 return -1; 2681 } 2682 2683 int __annotation__scnprintf_samples_period(struct annotation *notes, 2684 char *bf, size_t size, 2685 struct perf_evsel *evsel, 2686 bool show_freq) 2687 { 2688 const char *ev_name = perf_evsel__name(evsel); 2689 char buf[1024], ref[30] = " show reference callgraph, "; 2690 char sample_freq_str[64] = ""; 2691 unsigned long nr_samples = 0; 2692 int nr_members = 1; 2693 bool enable_ref = false; 2694 u64 nr_events = 0; 2695 char unit; 2696 int i; 2697 2698 if (perf_evsel__is_group_event(evsel)) { 2699 perf_evsel__group_desc(evsel, buf, sizeof(buf)); 2700 ev_name = buf; 2701 nr_members = evsel->nr_members; 2702 } 2703 2704 for (i = 0; i < nr_members; i++) { 2705 struct sym_hist *ah = annotation__histogram(notes, evsel->idx + i); 2706 2707 nr_samples += ah->nr_samples; 2708 nr_events += ah->period; 2709 } 2710 2711 if (symbol_conf.show_ref_callgraph && strstr(ev_name, "call-graph=no")) 2712 enable_ref = true; 2713 2714 if (show_freq) 2715 scnprintf(sample_freq_str, sizeof(sample_freq_str), " %d Hz,", evsel->attr.sample_freq); 2716 2717 nr_samples = convert_unit(nr_samples, &unit); 2718 return scnprintf(bf, size, "Samples: %lu%c of event%s '%s',%s%sEvent count (approx.): %" PRIu64, 2719 nr_samples, unit, evsel->nr_members > 1 ? "s" : "", 2720 ev_name, sample_freq_str, enable_ref ? ref : " ", nr_events); 2721 } 2722 2723 #define ANNOTATION__CFG(n) \ 2724 { .name = #n, .value = &annotation__default_options.n, } 2725 2726 /* 2727 * Keep the entries sorted, they are bsearch'ed 2728 */ 2729 static struct annotation_config { 2730 const char *name; 2731 void *value; 2732 } annotation__configs[] = { 2733 ANNOTATION__CFG(hide_src_code), 2734 ANNOTATION__CFG(jump_arrows), 2735 ANNOTATION__CFG(offset_level), 2736 ANNOTATION__CFG(show_linenr), 2737 ANNOTATION__CFG(show_nr_jumps), 2738 ANNOTATION__CFG(show_nr_samples), 2739 ANNOTATION__CFG(show_total_period), 2740 ANNOTATION__CFG(use_offset), 2741 }; 2742 2743 #undef ANNOTATION__CFG 2744 2745 static int annotation_config__cmp(const void *name, const void *cfgp) 2746 { 2747 const struct annotation_config *cfg = cfgp; 2748 2749 return strcmp(name, cfg->name); 2750 } 2751 2752 static int annotation__config(const char *var, const char *value, 2753 void *data __maybe_unused) 2754 { 2755 struct annotation_config *cfg; 2756 const char *name; 2757 2758 if (!strstarts(var, "annotate.")) 2759 return 0; 2760 2761 name = var + 9; 2762 cfg = bsearch(name, annotation__configs, ARRAY_SIZE(annotation__configs), 2763 sizeof(struct annotation_config), annotation_config__cmp); 2764 2765 if (cfg == NULL) 2766 pr_debug("%s variable unknown, ignoring...", var); 2767 else if (strcmp(var, "annotate.offset_level") == 0) { 2768 perf_config_int(cfg->value, name, value); 2769 2770 if (*(int *)cfg->value > ANNOTATION__MAX_OFFSET_LEVEL) 2771 *(int *)cfg->value = ANNOTATION__MAX_OFFSET_LEVEL; 2772 else if (*(int *)cfg->value < ANNOTATION__MIN_OFFSET_LEVEL) 2773 *(int *)cfg->value = ANNOTATION__MIN_OFFSET_LEVEL; 2774 } else { 2775 *(bool *)cfg->value = perf_config_bool(name, value); 2776 } 2777 return 0; 2778 } 2779 2780 void annotation_config__init(void) 2781 { 2782 perf_config(annotation__config, NULL); 2783 2784 annotation__default_options.show_total_period = symbol_conf.show_total_period; 2785 annotation__default_options.show_nr_samples = symbol_conf.show_nr_samples; 2786 } 2787