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 "dso.h" 20 #include "env.h" 21 #include "map.h" 22 #include "maps.h" 23 #include "symbol.h" 24 #include "srcline.h" 25 #include "units.h" 26 #include "debug.h" 27 #include "annotate.h" 28 #include "evsel.h" 29 #include "evlist.h" 30 #include "bpf-event.h" 31 #include "bpf-utils.h" 32 #include "block-range.h" 33 #include "string2.h" 34 #include "util/event.h" 35 #include "util/sharded_mutex.h" 36 #include "arch/common.h" 37 #include "namespaces.h" 38 #include <regex.h> 39 #include <linux/bitops.h> 40 #include <linux/kernel.h> 41 #include <linux/string.h> 42 #include <linux/zalloc.h> 43 #include <subcmd/parse-options.h> 44 #include <subcmd/run-command.h> 45 46 /* FIXME: For the HE_COLORSET */ 47 #include "ui/browser.h" 48 49 /* 50 * FIXME: Using the same values as slang.h, 51 * but that header may not be available everywhere 52 */ 53 #define LARROW_CHAR ((unsigned char)',') 54 #define RARROW_CHAR ((unsigned char)'+') 55 #define DARROW_CHAR ((unsigned char)'.') 56 #define UARROW_CHAR ((unsigned char)'-') 57 58 #include <linux/ctype.h> 59 60 static regex_t file_lineno; 61 62 static struct ins_ops *ins__find(struct arch *arch, const char *name); 63 static void ins__sort(struct arch *arch); 64 static int disasm_line__parse(char *line, const char **namep, char **rawp); 65 66 struct arch { 67 const char *name; 68 struct ins *instructions; 69 size_t nr_instructions; 70 size_t nr_instructions_allocated; 71 struct ins_ops *(*associate_instruction_ops)(struct arch *arch, const char *name); 72 bool sorted_instructions; 73 bool initialized; 74 const char *insn_suffix; 75 void *priv; 76 unsigned int model; 77 unsigned int family; 78 int (*init)(struct arch *arch, char *cpuid); 79 bool (*ins_is_fused)(struct arch *arch, const char *ins1, 80 const char *ins2); 81 struct { 82 char comment_char; 83 char skip_functions_char; 84 } objdump; 85 }; 86 87 static struct ins_ops call_ops; 88 static struct ins_ops dec_ops; 89 static struct ins_ops jump_ops; 90 static struct ins_ops mov_ops; 91 static struct ins_ops nop_ops; 92 static struct ins_ops lock_ops; 93 static struct ins_ops ret_ops; 94 95 static int arch__grow_instructions(struct arch *arch) 96 { 97 struct ins *new_instructions; 98 size_t new_nr_allocated; 99 100 if (arch->nr_instructions_allocated == 0 && arch->instructions) 101 goto grow_from_non_allocated_table; 102 103 new_nr_allocated = arch->nr_instructions_allocated + 128; 104 new_instructions = realloc(arch->instructions, new_nr_allocated * sizeof(struct ins)); 105 if (new_instructions == NULL) 106 return -1; 107 108 out_update_instructions: 109 arch->instructions = new_instructions; 110 arch->nr_instructions_allocated = new_nr_allocated; 111 return 0; 112 113 grow_from_non_allocated_table: 114 new_nr_allocated = arch->nr_instructions + 128; 115 new_instructions = calloc(new_nr_allocated, sizeof(struct ins)); 116 if (new_instructions == NULL) 117 return -1; 118 119 memcpy(new_instructions, arch->instructions, arch->nr_instructions); 120 goto out_update_instructions; 121 } 122 123 static int arch__associate_ins_ops(struct arch* arch, const char *name, struct ins_ops *ops) 124 { 125 struct ins *ins; 126 127 if (arch->nr_instructions == arch->nr_instructions_allocated && 128 arch__grow_instructions(arch)) 129 return -1; 130 131 ins = &arch->instructions[arch->nr_instructions]; 132 ins->name = strdup(name); 133 if (!ins->name) 134 return -1; 135 136 ins->ops = ops; 137 arch->nr_instructions++; 138 139 ins__sort(arch); 140 return 0; 141 } 142 143 #include "arch/arc/annotate/instructions.c" 144 #include "arch/arm/annotate/instructions.c" 145 #include "arch/arm64/annotate/instructions.c" 146 #include "arch/csky/annotate/instructions.c" 147 #include "arch/loongarch/annotate/instructions.c" 148 #include "arch/mips/annotate/instructions.c" 149 #include "arch/x86/annotate/instructions.c" 150 #include "arch/powerpc/annotate/instructions.c" 151 #include "arch/riscv64/annotate/instructions.c" 152 #include "arch/s390/annotate/instructions.c" 153 #include "arch/sparc/annotate/instructions.c" 154 155 static struct arch architectures[] = { 156 { 157 .name = "arc", 158 .init = arc__annotate_init, 159 }, 160 { 161 .name = "arm", 162 .init = arm__annotate_init, 163 }, 164 { 165 .name = "arm64", 166 .init = arm64__annotate_init, 167 }, 168 { 169 .name = "csky", 170 .init = csky__annotate_init, 171 }, 172 { 173 .name = "mips", 174 .init = mips__annotate_init, 175 .objdump = { 176 .comment_char = '#', 177 }, 178 }, 179 { 180 .name = "x86", 181 .init = x86__annotate_init, 182 .instructions = x86__instructions, 183 .nr_instructions = ARRAY_SIZE(x86__instructions), 184 .insn_suffix = "bwlq", 185 .objdump = { 186 .comment_char = '#', 187 }, 188 }, 189 { 190 .name = "powerpc", 191 .init = powerpc__annotate_init, 192 }, 193 { 194 .name = "riscv64", 195 .init = riscv64__annotate_init, 196 }, 197 { 198 .name = "s390", 199 .init = s390__annotate_init, 200 .objdump = { 201 .comment_char = '#', 202 }, 203 }, 204 { 205 .name = "sparc", 206 .init = sparc__annotate_init, 207 .objdump = { 208 .comment_char = '#', 209 }, 210 }, 211 { 212 .name = "loongarch", 213 .init = loongarch__annotate_init, 214 .objdump = { 215 .comment_char = '#', 216 }, 217 }, 218 }; 219 220 static void ins__delete(struct ins_operands *ops) 221 { 222 if (ops == NULL) 223 return; 224 zfree(&ops->source.raw); 225 zfree(&ops->source.name); 226 zfree(&ops->target.raw); 227 zfree(&ops->target.name); 228 } 229 230 static int ins__raw_scnprintf(struct ins *ins, char *bf, size_t size, 231 struct ins_operands *ops, int max_ins_name) 232 { 233 return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name, ops->raw); 234 } 235 236 int ins__scnprintf(struct ins *ins, char *bf, size_t size, 237 struct ins_operands *ops, int max_ins_name) 238 { 239 if (ins->ops->scnprintf) 240 return ins->ops->scnprintf(ins, bf, size, ops, max_ins_name); 241 242 return ins__raw_scnprintf(ins, bf, size, ops, max_ins_name); 243 } 244 245 bool ins__is_fused(struct arch *arch, const char *ins1, const char *ins2) 246 { 247 if (!arch || !arch->ins_is_fused) 248 return false; 249 250 return arch->ins_is_fused(arch, ins1, ins2); 251 } 252 253 static int call__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms) 254 { 255 char *endptr, *tok, *name; 256 struct map *map = ms->map; 257 struct addr_map_symbol target = { 258 .ms = { .map = map, }, 259 }; 260 261 ops->target.addr = strtoull(ops->raw, &endptr, 16); 262 263 name = strchr(endptr, '<'); 264 if (name == NULL) 265 goto indirect_call; 266 267 name++; 268 269 if (arch->objdump.skip_functions_char && 270 strchr(name, arch->objdump.skip_functions_char)) 271 return -1; 272 273 tok = strchr(name, '>'); 274 if (tok == NULL) 275 return -1; 276 277 *tok = '\0'; 278 ops->target.name = strdup(name); 279 *tok = '>'; 280 281 if (ops->target.name == NULL) 282 return -1; 283 find_target: 284 target.addr = map__objdump_2mem(map, ops->target.addr); 285 286 if (maps__find_ams(ms->maps, &target) == 0 && 287 map__rip_2objdump(target.ms.map, map__map_ip(target.ms.map, target.addr)) == ops->target.addr) 288 ops->target.sym = target.ms.sym; 289 290 return 0; 291 292 indirect_call: 293 tok = strchr(endptr, '*'); 294 if (tok != NULL) { 295 endptr++; 296 297 /* Indirect call can use a non-rip register and offset: callq *0x8(%rbx). 298 * Do not parse such instruction. */ 299 if (strstr(endptr, "(%r") == NULL) 300 ops->target.addr = strtoull(endptr, NULL, 16); 301 } 302 goto find_target; 303 } 304 305 static int call__scnprintf(struct ins *ins, char *bf, size_t size, 306 struct ins_operands *ops, int max_ins_name) 307 { 308 if (ops->target.sym) 309 return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name, ops->target.sym->name); 310 311 if (ops->target.addr == 0) 312 return ins__raw_scnprintf(ins, bf, size, ops, max_ins_name); 313 314 if (ops->target.name) 315 return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name, ops->target.name); 316 317 return scnprintf(bf, size, "%-*s *%" PRIx64, max_ins_name, ins->name, ops->target.addr); 318 } 319 320 static struct ins_ops call_ops = { 321 .parse = call__parse, 322 .scnprintf = call__scnprintf, 323 }; 324 325 bool ins__is_call(const struct ins *ins) 326 { 327 return ins->ops == &call_ops || ins->ops == &s390_call_ops; 328 } 329 330 /* 331 * Prevents from matching commas in the comment section, e.g.: 332 * ffff200008446e70: b.cs ffff2000084470f4 <generic_exec_single+0x314> // b.hs, b.nlast 333 * 334 * and skip comma as part of function arguments, e.g.: 335 * 1d8b4ac <linemap_lookup(line_maps const*, unsigned int)+0xcc> 336 */ 337 static inline const char *validate_comma(const char *c, struct ins_operands *ops) 338 { 339 if (ops->raw_comment && c > ops->raw_comment) 340 return NULL; 341 342 if (ops->raw_func_start && c > ops->raw_func_start) 343 return NULL; 344 345 return c; 346 } 347 348 static int jump__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms) 349 { 350 struct map *map = ms->map; 351 struct symbol *sym = ms->sym; 352 struct addr_map_symbol target = { 353 .ms = { .map = map, }, 354 }; 355 const char *c = strchr(ops->raw, ','); 356 u64 start, end; 357 358 ops->raw_comment = strchr(ops->raw, arch->objdump.comment_char); 359 ops->raw_func_start = strchr(ops->raw, '<'); 360 361 c = validate_comma(c, ops); 362 363 /* 364 * Examples of lines to parse for the _cpp_lex_token@@Base 365 * function: 366 * 367 * 1159e6c: jne 115aa32 <_cpp_lex_token@@Base+0xf92> 368 * 1159e8b: jne c469be <cpp_named_operator2name@@Base+0xa72> 369 * 370 * The first is a jump to an offset inside the same function, 371 * the second is to another function, i.e. that 0xa72 is an 372 * offset in the cpp_named_operator2name@@base function. 373 */ 374 /* 375 * skip over possible up to 2 operands to get to address, e.g.: 376 * tbnz w0, #26, ffff0000083cd190 <security_file_permission+0xd0> 377 */ 378 if (c++ != NULL) { 379 ops->target.addr = strtoull(c, NULL, 16); 380 if (!ops->target.addr) { 381 c = strchr(c, ','); 382 c = validate_comma(c, ops); 383 if (c++ != NULL) 384 ops->target.addr = strtoull(c, NULL, 16); 385 } 386 } else { 387 ops->target.addr = strtoull(ops->raw, NULL, 16); 388 } 389 390 target.addr = map__objdump_2mem(map, ops->target.addr); 391 start = map__unmap_ip(map, sym->start); 392 end = map__unmap_ip(map, sym->end); 393 394 ops->target.outside = target.addr < start || target.addr > end; 395 396 /* 397 * FIXME: things like this in _cpp_lex_token (gcc's cc1 program): 398 399 cpp_named_operator2name@@Base+0xa72 400 401 * Point to a place that is after the cpp_named_operator2name 402 * boundaries, i.e. in the ELF symbol table for cc1 403 * cpp_named_operator2name is marked as being 32-bytes long, but it in 404 * fact is much larger than that, so we seem to need a symbols__find() 405 * routine that looks for >= current->start and < next_symbol->start, 406 * possibly just for C++ objects? 407 * 408 * For now lets just make some progress by marking jumps to outside the 409 * current function as call like. 410 * 411 * Actual navigation will come next, with further understanding of how 412 * the symbol searching and disassembly should be done. 413 */ 414 if (maps__find_ams(ms->maps, &target) == 0 && 415 map__rip_2objdump(target.ms.map, map__map_ip(target.ms.map, target.addr)) == ops->target.addr) 416 ops->target.sym = target.ms.sym; 417 418 if (!ops->target.outside) { 419 ops->target.offset = target.addr - start; 420 ops->target.offset_avail = true; 421 } else { 422 ops->target.offset_avail = false; 423 } 424 425 return 0; 426 } 427 428 static int jump__scnprintf(struct ins *ins, char *bf, size_t size, 429 struct ins_operands *ops, int max_ins_name) 430 { 431 const char *c; 432 433 if (!ops->target.addr || ops->target.offset < 0) 434 return ins__raw_scnprintf(ins, bf, size, ops, max_ins_name); 435 436 if (ops->target.outside && ops->target.sym != NULL) 437 return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name, ops->target.sym->name); 438 439 c = strchr(ops->raw, ','); 440 c = validate_comma(c, ops); 441 442 if (c != NULL) { 443 const char *c2 = strchr(c + 1, ','); 444 445 c2 = validate_comma(c2, ops); 446 /* check for 3-op insn */ 447 if (c2 != NULL) 448 c = c2; 449 c++; 450 451 /* mirror arch objdump's space-after-comma style */ 452 if (*c == ' ') 453 c++; 454 } 455 456 return scnprintf(bf, size, "%-*s %.*s%" PRIx64, max_ins_name, 457 ins->name, c ? c - ops->raw : 0, ops->raw, 458 ops->target.offset); 459 } 460 461 static struct ins_ops jump_ops = { 462 .parse = jump__parse, 463 .scnprintf = jump__scnprintf, 464 }; 465 466 bool ins__is_jump(const struct ins *ins) 467 { 468 return ins->ops == &jump_ops; 469 } 470 471 static int comment__symbol(char *raw, char *comment, u64 *addrp, char **namep) 472 { 473 char *endptr, *name, *t; 474 475 if (strstr(raw, "(%rip)") == NULL) 476 return 0; 477 478 *addrp = strtoull(comment, &endptr, 16); 479 if (endptr == comment) 480 return 0; 481 name = strchr(endptr, '<'); 482 if (name == NULL) 483 return -1; 484 485 name++; 486 487 t = strchr(name, '>'); 488 if (t == NULL) 489 return 0; 490 491 *t = '\0'; 492 *namep = strdup(name); 493 *t = '>'; 494 495 return 0; 496 } 497 498 static int lock__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms) 499 { 500 ops->locked.ops = zalloc(sizeof(*ops->locked.ops)); 501 if (ops->locked.ops == NULL) 502 return 0; 503 504 if (disasm_line__parse(ops->raw, &ops->locked.ins.name, &ops->locked.ops->raw) < 0) 505 goto out_free_ops; 506 507 ops->locked.ins.ops = ins__find(arch, ops->locked.ins.name); 508 509 if (ops->locked.ins.ops == NULL) 510 goto out_free_ops; 511 512 if (ops->locked.ins.ops->parse && 513 ops->locked.ins.ops->parse(arch, ops->locked.ops, ms) < 0) 514 goto out_free_ops; 515 516 return 0; 517 518 out_free_ops: 519 zfree(&ops->locked.ops); 520 return 0; 521 } 522 523 static int lock__scnprintf(struct ins *ins, char *bf, size_t size, 524 struct ins_operands *ops, int max_ins_name) 525 { 526 int printed; 527 528 if (ops->locked.ins.ops == NULL) 529 return ins__raw_scnprintf(ins, bf, size, ops, max_ins_name); 530 531 printed = scnprintf(bf, size, "%-*s ", max_ins_name, ins->name); 532 return printed + ins__scnprintf(&ops->locked.ins, bf + printed, 533 size - printed, ops->locked.ops, max_ins_name); 534 } 535 536 static void lock__delete(struct ins_operands *ops) 537 { 538 struct ins *ins = &ops->locked.ins; 539 540 if (ins->ops && ins->ops->free) 541 ins->ops->free(ops->locked.ops); 542 else 543 ins__delete(ops->locked.ops); 544 545 zfree(&ops->locked.ops); 546 zfree(&ops->target.raw); 547 zfree(&ops->target.name); 548 } 549 550 static struct ins_ops lock_ops = { 551 .free = lock__delete, 552 .parse = lock__parse, 553 .scnprintf = lock__scnprintf, 554 }; 555 556 static int mov__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms __maybe_unused) 557 { 558 char *s = strchr(ops->raw, ','), *target, *comment, prev; 559 560 if (s == NULL) 561 return -1; 562 563 *s = '\0'; 564 565 /* 566 * x86 SIB addressing has something like 0x8(%rax, %rcx, 1) 567 * then it needs to have the closing parenthesis. 568 */ 569 if (strchr(ops->raw, '(')) { 570 *s = ','; 571 s = strchr(ops->raw, ')'); 572 if (s == NULL || s[1] != ',') 573 return -1; 574 *++s = '\0'; 575 } 576 577 ops->source.raw = strdup(ops->raw); 578 *s = ','; 579 580 if (ops->source.raw == NULL) 581 return -1; 582 583 target = skip_spaces(++s); 584 comment = strchr(s, arch->objdump.comment_char); 585 586 if (comment != NULL) 587 s = comment - 1; 588 else 589 s = strchr(s, '\0') - 1; 590 591 while (s > target && isspace(s[0])) 592 --s; 593 s++; 594 prev = *s; 595 *s = '\0'; 596 597 ops->target.raw = strdup(target); 598 *s = prev; 599 600 if (ops->target.raw == NULL) 601 goto out_free_source; 602 603 if (comment == NULL) 604 return 0; 605 606 comment = skip_spaces(comment); 607 comment__symbol(ops->source.raw, comment + 1, &ops->source.addr, &ops->source.name); 608 comment__symbol(ops->target.raw, comment + 1, &ops->target.addr, &ops->target.name); 609 610 return 0; 611 612 out_free_source: 613 zfree(&ops->source.raw); 614 return -1; 615 } 616 617 static int mov__scnprintf(struct ins *ins, char *bf, size_t size, 618 struct ins_operands *ops, int max_ins_name) 619 { 620 return scnprintf(bf, size, "%-*s %s,%s", max_ins_name, ins->name, 621 ops->source.name ?: ops->source.raw, 622 ops->target.name ?: ops->target.raw); 623 } 624 625 static struct ins_ops mov_ops = { 626 .parse = mov__parse, 627 .scnprintf = mov__scnprintf, 628 }; 629 630 static int dec__parse(struct arch *arch __maybe_unused, struct ins_operands *ops, struct map_symbol *ms __maybe_unused) 631 { 632 char *target, *comment, *s, prev; 633 634 target = s = ops->raw; 635 636 while (s[0] != '\0' && !isspace(s[0])) 637 ++s; 638 prev = *s; 639 *s = '\0'; 640 641 ops->target.raw = strdup(target); 642 *s = prev; 643 644 if (ops->target.raw == NULL) 645 return -1; 646 647 comment = strchr(s, arch->objdump.comment_char); 648 if (comment == NULL) 649 return 0; 650 651 comment = skip_spaces(comment); 652 comment__symbol(ops->target.raw, comment + 1, &ops->target.addr, &ops->target.name); 653 654 return 0; 655 } 656 657 static int dec__scnprintf(struct ins *ins, char *bf, size_t size, 658 struct ins_operands *ops, int max_ins_name) 659 { 660 return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name, 661 ops->target.name ?: ops->target.raw); 662 } 663 664 static struct ins_ops dec_ops = { 665 .parse = dec__parse, 666 .scnprintf = dec__scnprintf, 667 }; 668 669 static int nop__scnprintf(struct ins *ins __maybe_unused, char *bf, size_t size, 670 struct ins_operands *ops __maybe_unused, int max_ins_name) 671 { 672 return scnprintf(bf, size, "%-*s", max_ins_name, "nop"); 673 } 674 675 static struct ins_ops nop_ops = { 676 .scnprintf = nop__scnprintf, 677 }; 678 679 static struct ins_ops ret_ops = { 680 .scnprintf = ins__raw_scnprintf, 681 }; 682 683 bool ins__is_ret(const struct ins *ins) 684 { 685 return ins->ops == &ret_ops; 686 } 687 688 bool ins__is_lock(const struct ins *ins) 689 { 690 return ins->ops == &lock_ops; 691 } 692 693 static int ins__key_cmp(const void *name, const void *insp) 694 { 695 const struct ins *ins = insp; 696 697 return strcmp(name, ins->name); 698 } 699 700 static int ins__cmp(const void *a, const void *b) 701 { 702 const struct ins *ia = a; 703 const struct ins *ib = b; 704 705 return strcmp(ia->name, ib->name); 706 } 707 708 static void ins__sort(struct arch *arch) 709 { 710 const int nmemb = arch->nr_instructions; 711 712 qsort(arch->instructions, nmemb, sizeof(struct ins), ins__cmp); 713 } 714 715 static struct ins_ops *__ins__find(struct arch *arch, const char *name) 716 { 717 struct ins *ins; 718 const int nmemb = arch->nr_instructions; 719 720 if (!arch->sorted_instructions) { 721 ins__sort(arch); 722 arch->sorted_instructions = true; 723 } 724 725 ins = bsearch(name, arch->instructions, nmemb, sizeof(struct ins), ins__key_cmp); 726 if (ins) 727 return ins->ops; 728 729 if (arch->insn_suffix) { 730 char tmp[32]; 731 char suffix; 732 size_t len = strlen(name); 733 734 if (len == 0 || len >= sizeof(tmp)) 735 return NULL; 736 737 suffix = name[len - 1]; 738 if (strchr(arch->insn_suffix, suffix) == NULL) 739 return NULL; 740 741 strcpy(tmp, name); 742 tmp[len - 1] = '\0'; /* remove the suffix and check again */ 743 744 ins = bsearch(tmp, arch->instructions, nmemb, sizeof(struct ins), ins__key_cmp); 745 } 746 return ins ? ins->ops : NULL; 747 } 748 749 static struct ins_ops *ins__find(struct arch *arch, const char *name) 750 { 751 struct ins_ops *ops = __ins__find(arch, name); 752 753 if (!ops && arch->associate_instruction_ops) 754 ops = arch->associate_instruction_ops(arch, name); 755 756 return ops; 757 } 758 759 static int arch__key_cmp(const void *name, const void *archp) 760 { 761 const struct arch *arch = archp; 762 763 return strcmp(name, arch->name); 764 } 765 766 static int arch__cmp(const void *a, const void *b) 767 { 768 const struct arch *aa = a; 769 const struct arch *ab = b; 770 771 return strcmp(aa->name, ab->name); 772 } 773 774 static void arch__sort(void) 775 { 776 const int nmemb = ARRAY_SIZE(architectures); 777 778 qsort(architectures, nmemb, sizeof(struct arch), arch__cmp); 779 } 780 781 static struct arch *arch__find(const char *name) 782 { 783 const int nmemb = ARRAY_SIZE(architectures); 784 static bool sorted; 785 786 if (!sorted) { 787 arch__sort(); 788 sorted = true; 789 } 790 791 return bsearch(name, architectures, nmemb, sizeof(struct arch), arch__key_cmp); 792 } 793 794 static struct annotated_source *annotated_source__new(void) 795 { 796 struct annotated_source *src = zalloc(sizeof(*src)); 797 798 if (src != NULL) 799 INIT_LIST_HEAD(&src->source); 800 801 return src; 802 } 803 804 static __maybe_unused void annotated_source__delete(struct annotated_source *src) 805 { 806 if (src == NULL) 807 return; 808 zfree(&src->histograms); 809 zfree(&src->cycles_hist); 810 free(src); 811 } 812 813 static int annotated_source__alloc_histograms(struct annotated_source *src, 814 size_t size, int nr_hists) 815 { 816 size_t sizeof_sym_hist; 817 818 /* 819 * Add buffer of one element for zero length symbol. 820 * When sample is taken from first instruction of 821 * zero length symbol, perf still resolves it and 822 * shows symbol name in perf report and allows to 823 * annotate it. 824 */ 825 if (size == 0) 826 size = 1; 827 828 /* Check for overflow when calculating sizeof_sym_hist */ 829 if (size > (SIZE_MAX - sizeof(struct sym_hist)) / sizeof(struct sym_hist_entry)) 830 return -1; 831 832 sizeof_sym_hist = (sizeof(struct sym_hist) + size * sizeof(struct sym_hist_entry)); 833 834 /* Check for overflow in zalloc argument */ 835 if (sizeof_sym_hist > SIZE_MAX / nr_hists) 836 return -1; 837 838 src->sizeof_sym_hist = sizeof_sym_hist; 839 src->nr_histograms = nr_hists; 840 src->histograms = calloc(nr_hists, sizeof_sym_hist) ; 841 return src->histograms ? 0 : -1; 842 } 843 844 /* The cycles histogram is lazily allocated. */ 845 static int symbol__alloc_hist_cycles(struct symbol *sym) 846 { 847 struct annotation *notes = symbol__annotation(sym); 848 const size_t size = symbol__size(sym); 849 850 notes->src->cycles_hist = calloc(size, sizeof(struct cyc_hist)); 851 if (notes->src->cycles_hist == NULL) 852 return -1; 853 return 0; 854 } 855 856 void symbol__annotate_zero_histograms(struct symbol *sym) 857 { 858 struct annotation *notes = symbol__annotation(sym); 859 860 annotation__lock(notes); 861 if (notes->src != NULL) { 862 memset(notes->src->histograms, 0, 863 notes->src->nr_histograms * notes->src->sizeof_sym_hist); 864 if (notes->src->cycles_hist) 865 memset(notes->src->cycles_hist, 0, 866 symbol__size(sym) * sizeof(struct cyc_hist)); 867 } 868 annotation__unlock(notes); 869 } 870 871 static int __symbol__account_cycles(struct cyc_hist *ch, 872 u64 start, 873 unsigned offset, unsigned cycles, 874 unsigned have_start) 875 { 876 /* 877 * For now we can only account one basic block per 878 * final jump. But multiple could be overlapping. 879 * Always account the longest one. So when 880 * a shorter one has been already seen throw it away. 881 * 882 * We separately always account the full cycles. 883 */ 884 ch[offset].num_aggr++; 885 ch[offset].cycles_aggr += cycles; 886 887 if (cycles > ch[offset].cycles_max) 888 ch[offset].cycles_max = cycles; 889 890 if (ch[offset].cycles_min) { 891 if (cycles && cycles < ch[offset].cycles_min) 892 ch[offset].cycles_min = cycles; 893 } else 894 ch[offset].cycles_min = cycles; 895 896 if (!have_start && ch[offset].have_start) 897 return 0; 898 if (ch[offset].num) { 899 if (have_start && (!ch[offset].have_start || 900 ch[offset].start > start)) { 901 ch[offset].have_start = 0; 902 ch[offset].cycles = 0; 903 ch[offset].num = 0; 904 if (ch[offset].reset < 0xffff) 905 ch[offset].reset++; 906 } else if (have_start && 907 ch[offset].start < start) 908 return 0; 909 } 910 911 if (ch[offset].num < NUM_SPARKS) 912 ch[offset].cycles_spark[ch[offset].num] = cycles; 913 914 ch[offset].have_start = have_start; 915 ch[offset].start = start; 916 ch[offset].cycles += cycles; 917 ch[offset].num++; 918 return 0; 919 } 920 921 static int __symbol__inc_addr_samples(struct map_symbol *ms, 922 struct annotated_source *src, int evidx, u64 addr, 923 struct perf_sample *sample) 924 { 925 struct symbol *sym = ms->sym; 926 unsigned offset; 927 struct sym_hist *h; 928 929 pr_debug3("%s: addr=%#" PRIx64 "\n", __func__, map__unmap_ip(ms->map, addr)); 930 931 if ((addr < sym->start || addr >= sym->end) && 932 (addr != sym->end || sym->start != sym->end)) { 933 pr_debug("%s(%d): ERANGE! sym->name=%s, start=%#" PRIx64 ", addr=%#" PRIx64 ", end=%#" PRIx64 "\n", 934 __func__, __LINE__, sym->name, sym->start, addr, sym->end); 935 return -ERANGE; 936 } 937 938 offset = addr - sym->start; 939 h = annotated_source__histogram(src, evidx); 940 if (h == NULL) { 941 pr_debug("%s(%d): ENOMEM! sym->name=%s, start=%#" PRIx64 ", addr=%#" PRIx64 ", end=%#" PRIx64 ", func: %d\n", 942 __func__, __LINE__, sym->name, sym->start, addr, sym->end, sym->type == STT_FUNC); 943 return -ENOMEM; 944 } 945 h->nr_samples++; 946 h->addr[offset].nr_samples++; 947 h->period += sample->period; 948 h->addr[offset].period += sample->period; 949 950 pr_debug3("%#" PRIx64 " %s: period++ [addr: %#" PRIx64 ", %#" PRIx64 951 ", evidx=%d] => nr_samples: %" PRIu64 ", period: %" PRIu64 "\n", 952 sym->start, sym->name, addr, addr - sym->start, evidx, 953 h->addr[offset].nr_samples, h->addr[offset].period); 954 return 0; 955 } 956 957 static struct cyc_hist *symbol__cycles_hist(struct symbol *sym) 958 { 959 struct annotation *notes = symbol__annotation(sym); 960 961 if (notes->src == NULL) { 962 notes->src = annotated_source__new(); 963 if (notes->src == NULL) 964 return NULL; 965 goto alloc_cycles_hist; 966 } 967 968 if (!notes->src->cycles_hist) { 969 alloc_cycles_hist: 970 symbol__alloc_hist_cycles(sym); 971 } 972 973 return notes->src->cycles_hist; 974 } 975 976 struct annotated_source *symbol__hists(struct symbol *sym, int nr_hists) 977 { 978 struct annotation *notes = symbol__annotation(sym); 979 980 if (notes->src == NULL) { 981 notes->src = annotated_source__new(); 982 if (notes->src == NULL) 983 return NULL; 984 goto alloc_histograms; 985 } 986 987 if (notes->src->histograms == NULL) { 988 alloc_histograms: 989 annotated_source__alloc_histograms(notes->src, symbol__size(sym), 990 nr_hists); 991 } 992 993 return notes->src; 994 } 995 996 static int symbol__inc_addr_samples(struct map_symbol *ms, 997 struct evsel *evsel, u64 addr, 998 struct perf_sample *sample) 999 { 1000 struct symbol *sym = ms->sym; 1001 struct annotated_source *src; 1002 1003 if (sym == NULL) 1004 return 0; 1005 src = symbol__hists(sym, evsel->evlist->core.nr_entries); 1006 return src ? __symbol__inc_addr_samples(ms, src, evsel->core.idx, addr, sample) : 0; 1007 } 1008 1009 static int symbol__account_cycles(u64 addr, u64 start, 1010 struct symbol *sym, unsigned cycles) 1011 { 1012 struct cyc_hist *cycles_hist; 1013 unsigned offset; 1014 1015 if (sym == NULL) 1016 return 0; 1017 cycles_hist = symbol__cycles_hist(sym); 1018 if (cycles_hist == NULL) 1019 return -ENOMEM; 1020 if (addr < sym->start || addr >= sym->end) 1021 return -ERANGE; 1022 1023 if (start) { 1024 if (start < sym->start || start >= sym->end) 1025 return -ERANGE; 1026 if (start >= addr) 1027 start = 0; 1028 } 1029 offset = addr - sym->start; 1030 return __symbol__account_cycles(cycles_hist, 1031 start ? start - sym->start : 0, 1032 offset, cycles, 1033 !!start); 1034 } 1035 1036 int addr_map_symbol__account_cycles(struct addr_map_symbol *ams, 1037 struct addr_map_symbol *start, 1038 unsigned cycles) 1039 { 1040 u64 saddr = 0; 1041 int err; 1042 1043 if (!cycles) 1044 return 0; 1045 1046 /* 1047 * Only set start when IPC can be computed. We can only 1048 * compute it when the basic block is completely in a single 1049 * function. 1050 * Special case the case when the jump is elsewhere, but 1051 * it starts on the function start. 1052 */ 1053 if (start && 1054 (start->ms.sym == ams->ms.sym || 1055 (ams->ms.sym && 1056 start->addr == ams->ms.sym->start + map__start(ams->ms.map)))) 1057 saddr = start->al_addr; 1058 if (saddr == 0) 1059 pr_debug2("BB with bad start: addr %"PRIx64" start %"PRIx64" sym %"PRIx64" saddr %"PRIx64"\n", 1060 ams->addr, 1061 start ? start->addr : 0, 1062 ams->ms.sym ? ams->ms.sym->start + map__start(ams->ms.map) : 0, 1063 saddr); 1064 err = symbol__account_cycles(ams->al_addr, saddr, ams->ms.sym, cycles); 1065 if (err) 1066 pr_debug2("account_cycles failed %d\n", err); 1067 return err; 1068 } 1069 1070 static unsigned annotation__count_insn(struct annotation *notes, u64 start, u64 end) 1071 { 1072 unsigned n_insn = 0; 1073 u64 offset; 1074 1075 for (offset = start; offset <= end; offset++) { 1076 if (notes->offsets[offset]) 1077 n_insn++; 1078 } 1079 return n_insn; 1080 } 1081 1082 static void annotation__count_and_fill(struct annotation *notes, u64 start, u64 end, struct cyc_hist *ch) 1083 { 1084 unsigned n_insn; 1085 unsigned int cover_insn = 0; 1086 u64 offset; 1087 1088 n_insn = annotation__count_insn(notes, start, end); 1089 if (n_insn && ch->num && ch->cycles) { 1090 float ipc = n_insn / ((double)ch->cycles / (double)ch->num); 1091 1092 /* Hide data when there are too many overlaps. */ 1093 if (ch->reset >= 0x7fff) 1094 return; 1095 1096 for (offset = start; offset <= end; offset++) { 1097 struct annotation_line *al = notes->offsets[offset]; 1098 1099 if (al && al->ipc == 0.0) { 1100 al->ipc = ipc; 1101 cover_insn++; 1102 } 1103 } 1104 1105 if (cover_insn) { 1106 notes->hit_cycles += ch->cycles; 1107 notes->hit_insn += n_insn * ch->num; 1108 notes->cover_insn += cover_insn; 1109 } 1110 } 1111 } 1112 1113 void annotation__compute_ipc(struct annotation *notes, size_t size) 1114 { 1115 s64 offset; 1116 1117 if (!notes->src || !notes->src->cycles_hist) 1118 return; 1119 1120 notes->total_insn = annotation__count_insn(notes, 0, size - 1); 1121 notes->hit_cycles = 0; 1122 notes->hit_insn = 0; 1123 notes->cover_insn = 0; 1124 1125 annotation__lock(notes); 1126 for (offset = size - 1; offset >= 0; --offset) { 1127 struct cyc_hist *ch; 1128 1129 ch = ¬es->src->cycles_hist[offset]; 1130 if (ch && ch->cycles) { 1131 struct annotation_line *al; 1132 1133 if (ch->have_start) 1134 annotation__count_and_fill(notes, ch->start, offset, ch); 1135 al = notes->offsets[offset]; 1136 if (al && ch->num_aggr) { 1137 al->cycles = ch->cycles_aggr / ch->num_aggr; 1138 al->cycles_max = ch->cycles_max; 1139 al->cycles_min = ch->cycles_min; 1140 } 1141 notes->have_cycles = true; 1142 } 1143 } 1144 annotation__unlock(notes); 1145 } 1146 1147 int addr_map_symbol__inc_samples(struct addr_map_symbol *ams, struct perf_sample *sample, 1148 struct evsel *evsel) 1149 { 1150 return symbol__inc_addr_samples(&ams->ms, evsel, ams->al_addr, sample); 1151 } 1152 1153 int hist_entry__inc_addr_samples(struct hist_entry *he, struct perf_sample *sample, 1154 struct evsel *evsel, u64 ip) 1155 { 1156 return symbol__inc_addr_samples(&he->ms, evsel, ip, sample); 1157 } 1158 1159 static void disasm_line__init_ins(struct disasm_line *dl, struct arch *arch, struct map_symbol *ms) 1160 { 1161 dl->ins.ops = ins__find(arch, dl->ins.name); 1162 1163 if (!dl->ins.ops) 1164 return; 1165 1166 if (dl->ins.ops->parse && dl->ins.ops->parse(arch, &dl->ops, ms) < 0) 1167 dl->ins.ops = NULL; 1168 } 1169 1170 static int disasm_line__parse(char *line, const char **namep, char **rawp) 1171 { 1172 char tmp, *name = skip_spaces(line); 1173 1174 if (name[0] == '\0') 1175 return -1; 1176 1177 *rawp = name + 1; 1178 1179 while ((*rawp)[0] != '\0' && !isspace((*rawp)[0])) 1180 ++*rawp; 1181 1182 tmp = (*rawp)[0]; 1183 (*rawp)[0] = '\0'; 1184 *namep = strdup(name); 1185 1186 if (*namep == NULL) 1187 goto out; 1188 1189 (*rawp)[0] = tmp; 1190 *rawp = strim(*rawp); 1191 1192 return 0; 1193 1194 out: 1195 return -1; 1196 } 1197 1198 struct annotate_args { 1199 struct arch *arch; 1200 struct map_symbol ms; 1201 struct evsel *evsel; 1202 struct annotation_options *options; 1203 s64 offset; 1204 char *line; 1205 int line_nr; 1206 char *fileloc; 1207 }; 1208 1209 static void annotation_line__init(struct annotation_line *al, 1210 struct annotate_args *args, 1211 int nr) 1212 { 1213 al->offset = args->offset; 1214 al->line = strdup(args->line); 1215 al->line_nr = args->line_nr; 1216 al->fileloc = args->fileloc; 1217 al->data_nr = nr; 1218 } 1219 1220 static void annotation_line__exit(struct annotation_line *al) 1221 { 1222 zfree_srcline(&al->path); 1223 zfree(&al->line); 1224 } 1225 1226 static size_t disasm_line_size(int nr) 1227 { 1228 struct annotation_line *al; 1229 1230 return (sizeof(struct disasm_line) + (sizeof(al->data[0]) * nr)); 1231 } 1232 1233 /* 1234 * Allocating the disasm annotation line data with 1235 * following structure: 1236 * 1237 * ------------------------------------------- 1238 * struct disasm_line | struct annotation_line 1239 * ------------------------------------------- 1240 * 1241 * We have 'struct annotation_line' member as last member 1242 * of 'struct disasm_line' to have an easy access. 1243 */ 1244 static struct disasm_line *disasm_line__new(struct annotate_args *args) 1245 { 1246 struct disasm_line *dl = NULL; 1247 int nr = 1; 1248 1249 if (evsel__is_group_event(args->evsel)) 1250 nr = args->evsel->core.nr_members; 1251 1252 dl = zalloc(disasm_line_size(nr)); 1253 if (!dl) 1254 return NULL; 1255 1256 annotation_line__init(&dl->al, args, nr); 1257 if (dl->al.line == NULL) 1258 goto out_delete; 1259 1260 if (args->offset != -1) { 1261 if (disasm_line__parse(dl->al.line, &dl->ins.name, &dl->ops.raw) < 0) 1262 goto out_free_line; 1263 1264 disasm_line__init_ins(dl, args->arch, &args->ms); 1265 } 1266 1267 return dl; 1268 1269 out_free_line: 1270 zfree(&dl->al.line); 1271 out_delete: 1272 free(dl); 1273 return NULL; 1274 } 1275 1276 void disasm_line__free(struct disasm_line *dl) 1277 { 1278 if (dl->ins.ops && dl->ins.ops->free) 1279 dl->ins.ops->free(&dl->ops); 1280 else 1281 ins__delete(&dl->ops); 1282 zfree(&dl->ins.name); 1283 annotation_line__exit(&dl->al); 1284 free(dl); 1285 } 1286 1287 int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size, bool raw, int max_ins_name) 1288 { 1289 if (raw || !dl->ins.ops) 1290 return scnprintf(bf, size, "%-*s %s", max_ins_name, dl->ins.name, dl->ops.raw); 1291 1292 return ins__scnprintf(&dl->ins, bf, size, &dl->ops, max_ins_name); 1293 } 1294 1295 void annotation__exit(struct annotation *notes) 1296 { 1297 annotated_source__delete(notes->src); 1298 } 1299 1300 static struct sharded_mutex *sharded_mutex; 1301 1302 static void annotation__init_sharded_mutex(void) 1303 { 1304 /* As many mutexes as there are CPUs. */ 1305 sharded_mutex = sharded_mutex__new(cpu__max_present_cpu().cpu); 1306 } 1307 1308 static size_t annotation__hash(const struct annotation *notes) 1309 { 1310 return (size_t)notes; 1311 } 1312 1313 static struct mutex *annotation__get_mutex(const struct annotation *notes) 1314 { 1315 static pthread_once_t once = PTHREAD_ONCE_INIT; 1316 1317 pthread_once(&once, annotation__init_sharded_mutex); 1318 if (!sharded_mutex) 1319 return NULL; 1320 1321 return sharded_mutex__get_mutex(sharded_mutex, annotation__hash(notes)); 1322 } 1323 1324 void annotation__lock(struct annotation *notes) 1325 NO_THREAD_SAFETY_ANALYSIS 1326 { 1327 struct mutex *mutex = annotation__get_mutex(notes); 1328 1329 if (mutex) 1330 mutex_lock(mutex); 1331 } 1332 1333 void annotation__unlock(struct annotation *notes) 1334 NO_THREAD_SAFETY_ANALYSIS 1335 { 1336 struct mutex *mutex = annotation__get_mutex(notes); 1337 1338 if (mutex) 1339 mutex_unlock(mutex); 1340 } 1341 1342 bool annotation__trylock(struct annotation *notes) 1343 { 1344 struct mutex *mutex = annotation__get_mutex(notes); 1345 1346 if (!mutex) 1347 return false; 1348 1349 return mutex_trylock(mutex); 1350 } 1351 1352 1353 static void annotation_line__add(struct annotation_line *al, struct list_head *head) 1354 { 1355 list_add_tail(&al->node, head); 1356 } 1357 1358 struct annotation_line * 1359 annotation_line__next(struct annotation_line *pos, struct list_head *head) 1360 { 1361 list_for_each_entry_continue(pos, head, node) 1362 if (pos->offset >= 0) 1363 return pos; 1364 1365 return NULL; 1366 } 1367 1368 static const char *annotate__address_color(struct block_range *br) 1369 { 1370 double cov = block_range__coverage(br); 1371 1372 if (cov >= 0) { 1373 /* mark red for >75% coverage */ 1374 if (cov > 0.75) 1375 return PERF_COLOR_RED; 1376 1377 /* mark dull for <1% coverage */ 1378 if (cov < 0.01) 1379 return PERF_COLOR_NORMAL; 1380 } 1381 1382 return PERF_COLOR_MAGENTA; 1383 } 1384 1385 static const char *annotate__asm_color(struct block_range *br) 1386 { 1387 double cov = block_range__coverage(br); 1388 1389 if (cov >= 0) { 1390 /* mark dull for <1% coverage */ 1391 if (cov < 0.01) 1392 return PERF_COLOR_NORMAL; 1393 } 1394 1395 return PERF_COLOR_BLUE; 1396 } 1397 1398 static void annotate__branch_printf(struct block_range *br, u64 addr) 1399 { 1400 bool emit_comment = true; 1401 1402 if (!br) 1403 return; 1404 1405 #if 1 1406 if (br->is_target && br->start == addr) { 1407 struct block_range *branch = br; 1408 double p; 1409 1410 /* 1411 * Find matching branch to our target. 1412 */ 1413 while (!branch->is_branch) 1414 branch = block_range__next(branch); 1415 1416 p = 100 *(double)br->entry / branch->coverage; 1417 1418 if (p > 0.1) { 1419 if (emit_comment) { 1420 emit_comment = false; 1421 printf("\t#"); 1422 } 1423 1424 /* 1425 * The percentage of coverage joined at this target in relation 1426 * to the next branch. 1427 */ 1428 printf(" +%.2f%%", p); 1429 } 1430 } 1431 #endif 1432 if (br->is_branch && br->end == addr) { 1433 double p = 100*(double)br->taken / br->coverage; 1434 1435 if (p > 0.1) { 1436 if (emit_comment) { 1437 emit_comment = false; 1438 printf("\t#"); 1439 } 1440 1441 /* 1442 * The percentage of coverage leaving at this branch, and 1443 * its prediction ratio. 1444 */ 1445 printf(" -%.2f%% (p:%.2f%%)", p, 100*(double)br->pred / br->taken); 1446 } 1447 } 1448 } 1449 1450 static int disasm_line__print(struct disasm_line *dl, u64 start, int addr_fmt_width) 1451 { 1452 s64 offset = dl->al.offset; 1453 const u64 addr = start + offset; 1454 struct block_range *br; 1455 1456 br = block_range__find(addr); 1457 color_fprintf(stdout, annotate__address_color(br), " %*" PRIx64 ":", addr_fmt_width, addr); 1458 color_fprintf(stdout, annotate__asm_color(br), "%s", dl->al.line); 1459 annotate__branch_printf(br, addr); 1460 return 0; 1461 } 1462 1463 static int 1464 annotation_line__print(struct annotation_line *al, struct symbol *sym, u64 start, 1465 struct evsel *evsel, u64 len, int min_pcnt, int printed, 1466 int max_lines, struct annotation_line *queue, int addr_fmt_width, 1467 int percent_type) 1468 { 1469 struct disasm_line *dl = container_of(al, struct disasm_line, al); 1470 static const char *prev_line; 1471 1472 if (al->offset != -1) { 1473 double max_percent = 0.0; 1474 int i, nr_percent = 1; 1475 const char *color; 1476 struct annotation *notes = symbol__annotation(sym); 1477 1478 for (i = 0; i < al->data_nr; i++) { 1479 double percent; 1480 1481 percent = annotation_data__percent(&al->data[i], 1482 percent_type); 1483 1484 if (percent > max_percent) 1485 max_percent = percent; 1486 } 1487 1488 if (al->data_nr > nr_percent) 1489 nr_percent = al->data_nr; 1490 1491 if (max_percent < min_pcnt) 1492 return -1; 1493 1494 if (max_lines && printed >= max_lines) 1495 return 1; 1496 1497 if (queue != NULL) { 1498 list_for_each_entry_from(queue, ¬es->src->source, node) { 1499 if (queue == al) 1500 break; 1501 annotation_line__print(queue, sym, start, evsel, len, 1502 0, 0, 1, NULL, addr_fmt_width, 1503 percent_type); 1504 } 1505 } 1506 1507 color = get_percent_color(max_percent); 1508 1509 for (i = 0; i < nr_percent; i++) { 1510 struct annotation_data *data = &al->data[i]; 1511 double percent; 1512 1513 percent = annotation_data__percent(data, percent_type); 1514 color = get_percent_color(percent); 1515 1516 if (symbol_conf.show_total_period) 1517 color_fprintf(stdout, color, " %11" PRIu64, 1518 data->he.period); 1519 else if (symbol_conf.show_nr_samples) 1520 color_fprintf(stdout, color, " %7" PRIu64, 1521 data->he.nr_samples); 1522 else 1523 color_fprintf(stdout, color, " %7.2f", percent); 1524 } 1525 1526 printf(" : "); 1527 1528 disasm_line__print(dl, start, addr_fmt_width); 1529 1530 /* 1531 * Also color the filename and line if needed, with 1532 * the same color than the percentage. Don't print it 1533 * twice for close colored addr with the same filename:line 1534 */ 1535 if (al->path) { 1536 if (!prev_line || strcmp(prev_line, al->path)) { 1537 color_fprintf(stdout, color, " // %s", al->path); 1538 prev_line = al->path; 1539 } 1540 } 1541 1542 printf("\n"); 1543 } else if (max_lines && printed >= max_lines) 1544 return 1; 1545 else { 1546 int width = symbol_conf.show_total_period ? 12 : 8; 1547 1548 if (queue) 1549 return -1; 1550 1551 if (evsel__is_group_event(evsel)) 1552 width *= evsel->core.nr_members; 1553 1554 if (!*al->line) 1555 printf(" %*s:\n", width, " "); 1556 else 1557 printf(" %*s: %-*d %s\n", width, " ", addr_fmt_width, al->line_nr, al->line); 1558 } 1559 1560 return 0; 1561 } 1562 1563 /* 1564 * symbol__parse_objdump_line() parses objdump output (with -d --no-show-raw) 1565 * which looks like following 1566 * 1567 * 0000000000415500 <_init>: 1568 * 415500: sub $0x8,%rsp 1569 * 415504: mov 0x2f5ad5(%rip),%rax # 70afe0 <_DYNAMIC+0x2f8> 1570 * 41550b: test %rax,%rax 1571 * 41550e: je 415515 <_init+0x15> 1572 * 415510: callq 416e70 <__gmon_start__@plt> 1573 * 415515: add $0x8,%rsp 1574 * 415519: retq 1575 * 1576 * it will be parsed and saved into struct disasm_line as 1577 * <offset> <name> <ops.raw> 1578 * 1579 * The offset will be a relative offset from the start of the symbol and -1 1580 * means that it's not a disassembly line so should be treated differently. 1581 * The ops.raw part will be parsed further according to type of the instruction. 1582 */ 1583 static int symbol__parse_objdump_line(struct symbol *sym, 1584 struct annotate_args *args, 1585 char *parsed_line, int *line_nr, char **fileloc) 1586 { 1587 struct map *map = args->ms.map; 1588 struct annotation *notes = symbol__annotation(sym); 1589 struct disasm_line *dl; 1590 char *tmp; 1591 s64 line_ip, offset = -1; 1592 regmatch_t match[2]; 1593 1594 /* /filename:linenr ? Save line number and ignore. */ 1595 if (regexec(&file_lineno, parsed_line, 2, match, 0) == 0) { 1596 *line_nr = atoi(parsed_line + match[1].rm_so); 1597 free(*fileloc); 1598 *fileloc = strdup(parsed_line); 1599 return 0; 1600 } 1601 1602 /* Process hex address followed by ':'. */ 1603 line_ip = strtoull(parsed_line, &tmp, 16); 1604 if (parsed_line != tmp && tmp[0] == ':' && tmp[1] != '\0') { 1605 u64 start = map__rip_2objdump(map, sym->start), 1606 end = map__rip_2objdump(map, sym->end); 1607 1608 offset = line_ip - start; 1609 if ((u64)line_ip < start || (u64)line_ip >= end) 1610 offset = -1; 1611 else 1612 parsed_line = tmp + 1; 1613 } 1614 1615 args->offset = offset; 1616 args->line = parsed_line; 1617 args->line_nr = *line_nr; 1618 args->fileloc = *fileloc; 1619 args->ms.sym = sym; 1620 1621 dl = disasm_line__new(args); 1622 (*line_nr)++; 1623 1624 if (dl == NULL) 1625 return -1; 1626 1627 if (!disasm_line__has_local_offset(dl)) { 1628 dl->ops.target.offset = dl->ops.target.addr - 1629 map__rip_2objdump(map, sym->start); 1630 dl->ops.target.offset_avail = true; 1631 } 1632 1633 /* kcore has no symbols, so add the call target symbol */ 1634 if (dl->ins.ops && ins__is_call(&dl->ins) && !dl->ops.target.sym) { 1635 struct addr_map_symbol target = { 1636 .addr = dl->ops.target.addr, 1637 .ms = { .map = map, }, 1638 }; 1639 1640 if (!maps__find_ams(args->ms.maps, &target) && 1641 target.ms.sym->start == target.al_addr) 1642 dl->ops.target.sym = target.ms.sym; 1643 } 1644 1645 annotation_line__add(&dl->al, ¬es->src->source); 1646 return 0; 1647 } 1648 1649 static __attribute__((constructor)) void symbol__init_regexpr(void) 1650 { 1651 regcomp(&file_lineno, "^/[^:]+:([0-9]+)", REG_EXTENDED); 1652 } 1653 1654 static void delete_last_nop(struct symbol *sym) 1655 { 1656 struct annotation *notes = symbol__annotation(sym); 1657 struct list_head *list = ¬es->src->source; 1658 struct disasm_line *dl; 1659 1660 while (!list_empty(list)) { 1661 dl = list_entry(list->prev, struct disasm_line, al.node); 1662 1663 if (dl->ins.ops) { 1664 if (dl->ins.ops != &nop_ops) 1665 return; 1666 } else { 1667 if (!strstr(dl->al.line, " nop ") && 1668 !strstr(dl->al.line, " nopl ") && 1669 !strstr(dl->al.line, " nopw ")) 1670 return; 1671 } 1672 1673 list_del_init(&dl->al.node); 1674 disasm_line__free(dl); 1675 } 1676 } 1677 1678 int symbol__strerror_disassemble(struct map_symbol *ms, int errnum, char *buf, size_t buflen) 1679 { 1680 struct dso *dso = map__dso(ms->map); 1681 1682 BUG_ON(buflen == 0); 1683 1684 if (errnum >= 0) { 1685 str_error_r(errnum, buf, buflen); 1686 return 0; 1687 } 1688 1689 switch (errnum) { 1690 case SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX: { 1691 char bf[SBUILD_ID_SIZE + 15] = " with build id "; 1692 char *build_id_msg = NULL; 1693 1694 if (dso->has_build_id) { 1695 build_id__sprintf(&dso->bid, bf + 15); 1696 build_id_msg = bf; 1697 } 1698 scnprintf(buf, buflen, 1699 "No vmlinux file%s\nwas found in the path.\n\n" 1700 "Note that annotation using /proc/kcore requires CAP_SYS_RAWIO capability.\n\n" 1701 "Please use:\n\n" 1702 " perf buildid-cache -vu vmlinux\n\n" 1703 "or:\n\n" 1704 " --vmlinux vmlinux\n", build_id_msg ?: ""); 1705 } 1706 break; 1707 case SYMBOL_ANNOTATE_ERRNO__NO_LIBOPCODES_FOR_BPF: 1708 scnprintf(buf, buflen, "Please link with binutils's libopcode to enable BPF annotation"); 1709 break; 1710 case SYMBOL_ANNOTATE_ERRNO__ARCH_INIT_REGEXP: 1711 scnprintf(buf, buflen, "Problems with arch specific instruction name regular expressions."); 1712 break; 1713 case SYMBOL_ANNOTATE_ERRNO__ARCH_INIT_CPUID_PARSING: 1714 scnprintf(buf, buflen, "Problems while parsing the CPUID in the arch specific initialization."); 1715 break; 1716 case SYMBOL_ANNOTATE_ERRNO__BPF_INVALID_FILE: 1717 scnprintf(buf, buflen, "Invalid BPF file: %s.", dso->long_name); 1718 break; 1719 case SYMBOL_ANNOTATE_ERRNO__BPF_MISSING_BTF: 1720 scnprintf(buf, buflen, "The %s BPF file has no BTF section, compile with -g or use pahole -J.", 1721 dso->long_name); 1722 break; 1723 default: 1724 scnprintf(buf, buflen, "Internal error: Invalid %d error code\n", errnum); 1725 break; 1726 } 1727 1728 return 0; 1729 } 1730 1731 static int dso__disassemble_filename(struct dso *dso, char *filename, size_t filename_size) 1732 { 1733 char linkname[PATH_MAX]; 1734 char *build_id_filename; 1735 char *build_id_path = NULL; 1736 char *pos; 1737 int len; 1738 1739 if (dso->symtab_type == DSO_BINARY_TYPE__KALLSYMS && 1740 !dso__is_kcore(dso)) 1741 return SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX; 1742 1743 build_id_filename = dso__build_id_filename(dso, NULL, 0, false); 1744 if (build_id_filename) { 1745 __symbol__join_symfs(filename, filename_size, build_id_filename); 1746 free(build_id_filename); 1747 } else { 1748 if (dso->has_build_id) 1749 return ENOMEM; 1750 goto fallback; 1751 } 1752 1753 build_id_path = strdup(filename); 1754 if (!build_id_path) 1755 return ENOMEM; 1756 1757 /* 1758 * old style build-id cache has name of XX/XXXXXXX.. while 1759 * new style has XX/XXXXXXX../{elf,kallsyms,vdso}. 1760 * extract the build-id part of dirname in the new style only. 1761 */ 1762 pos = strrchr(build_id_path, '/'); 1763 if (pos && strlen(pos) < SBUILD_ID_SIZE - 2) 1764 dirname(build_id_path); 1765 1766 if (dso__is_kcore(dso)) 1767 goto fallback; 1768 1769 len = readlink(build_id_path, linkname, sizeof(linkname) - 1); 1770 if (len < 0) 1771 goto fallback; 1772 1773 linkname[len] = '\0'; 1774 if (strstr(linkname, DSO__NAME_KALLSYMS) || 1775 access(filename, R_OK)) { 1776 fallback: 1777 /* 1778 * If we don't have build-ids or the build-id file isn't in the 1779 * cache, or is just a kallsyms file, well, lets hope that this 1780 * DSO is the same as when 'perf record' ran. 1781 */ 1782 if (dso->kernel && dso->long_name[0] == '/') 1783 snprintf(filename, filename_size, "%s", dso->long_name); 1784 else 1785 __symbol__join_symfs(filename, filename_size, dso->long_name); 1786 1787 mutex_lock(&dso->lock); 1788 if (access(filename, R_OK) && errno == ENOENT && dso->nsinfo) { 1789 char *new_name = dso__filename_with_chroot(dso, filename); 1790 if (new_name) { 1791 strlcpy(filename, new_name, filename_size); 1792 free(new_name); 1793 } 1794 } 1795 mutex_unlock(&dso->lock); 1796 } 1797 1798 free(build_id_path); 1799 return 0; 1800 } 1801 1802 #if defined(HAVE_LIBBFD_SUPPORT) && defined(HAVE_LIBBPF_SUPPORT) 1803 #define PACKAGE "perf" 1804 #include <bfd.h> 1805 #include <dis-asm.h> 1806 #include <bpf/bpf.h> 1807 #include <bpf/btf.h> 1808 #include <bpf/libbpf.h> 1809 #include <linux/btf.h> 1810 #include <tools/dis-asm-compat.h> 1811 1812 static int symbol__disassemble_bpf(struct symbol *sym, 1813 struct annotate_args *args) 1814 { 1815 struct annotation *notes = symbol__annotation(sym); 1816 struct annotation_options *opts = args->options; 1817 struct bpf_prog_linfo *prog_linfo = NULL; 1818 struct bpf_prog_info_node *info_node; 1819 int len = sym->end - sym->start; 1820 disassembler_ftype disassemble; 1821 struct map *map = args->ms.map; 1822 struct perf_bpil *info_linear; 1823 struct disassemble_info info; 1824 struct dso *dso = map__dso(map); 1825 int pc = 0, count, sub_id; 1826 struct btf *btf = NULL; 1827 char tpath[PATH_MAX]; 1828 size_t buf_size; 1829 int nr_skip = 0; 1830 char *buf; 1831 bfd *bfdf; 1832 int ret; 1833 FILE *s; 1834 1835 if (dso->binary_type != DSO_BINARY_TYPE__BPF_PROG_INFO) 1836 return SYMBOL_ANNOTATE_ERRNO__BPF_INVALID_FILE; 1837 1838 pr_debug("%s: handling sym %s addr %" PRIx64 " len %" PRIx64 "\n", __func__, 1839 sym->name, sym->start, sym->end - sym->start); 1840 1841 memset(tpath, 0, sizeof(tpath)); 1842 perf_exe(tpath, sizeof(tpath)); 1843 1844 bfdf = bfd_openr(tpath, NULL); 1845 assert(bfdf); 1846 assert(bfd_check_format(bfdf, bfd_object)); 1847 1848 s = open_memstream(&buf, &buf_size); 1849 if (!s) { 1850 ret = errno; 1851 goto out; 1852 } 1853 init_disassemble_info_compat(&info, s, 1854 (fprintf_ftype) fprintf, 1855 fprintf_styled); 1856 info.arch = bfd_get_arch(bfdf); 1857 info.mach = bfd_get_mach(bfdf); 1858 1859 info_node = perf_env__find_bpf_prog_info(dso->bpf_prog.env, 1860 dso->bpf_prog.id); 1861 if (!info_node) { 1862 ret = SYMBOL_ANNOTATE_ERRNO__BPF_MISSING_BTF; 1863 goto out; 1864 } 1865 info_linear = info_node->info_linear; 1866 sub_id = dso->bpf_prog.sub_id; 1867 1868 info.buffer = (void *)(uintptr_t)(info_linear->info.jited_prog_insns); 1869 info.buffer_length = info_linear->info.jited_prog_len; 1870 1871 if (info_linear->info.nr_line_info) 1872 prog_linfo = bpf_prog_linfo__new(&info_linear->info); 1873 1874 if (info_linear->info.btf_id) { 1875 struct btf_node *node; 1876 1877 node = perf_env__find_btf(dso->bpf_prog.env, 1878 info_linear->info.btf_id); 1879 if (node) 1880 btf = btf__new((__u8 *)(node->data), 1881 node->data_size); 1882 } 1883 1884 disassemble_init_for_target(&info); 1885 1886 #ifdef DISASM_FOUR_ARGS_SIGNATURE 1887 disassemble = disassembler(info.arch, 1888 bfd_big_endian(bfdf), 1889 info.mach, 1890 bfdf); 1891 #else 1892 disassemble = disassembler(bfdf); 1893 #endif 1894 assert(disassemble); 1895 1896 fflush(s); 1897 do { 1898 const struct bpf_line_info *linfo = NULL; 1899 struct disasm_line *dl; 1900 size_t prev_buf_size; 1901 const char *srcline; 1902 u64 addr; 1903 1904 addr = pc + ((u64 *)(uintptr_t)(info_linear->info.jited_ksyms))[sub_id]; 1905 count = disassemble(pc, &info); 1906 1907 if (prog_linfo) 1908 linfo = bpf_prog_linfo__lfind_addr_func(prog_linfo, 1909 addr, sub_id, 1910 nr_skip); 1911 1912 if (linfo && btf) { 1913 srcline = btf__name_by_offset(btf, linfo->line_off); 1914 nr_skip++; 1915 } else 1916 srcline = NULL; 1917 1918 fprintf(s, "\n"); 1919 prev_buf_size = buf_size; 1920 fflush(s); 1921 1922 if (!opts->hide_src_code && srcline) { 1923 args->offset = -1; 1924 args->line = strdup(srcline); 1925 args->line_nr = 0; 1926 args->fileloc = NULL; 1927 args->ms.sym = sym; 1928 dl = disasm_line__new(args); 1929 if (dl) { 1930 annotation_line__add(&dl->al, 1931 ¬es->src->source); 1932 } 1933 } 1934 1935 args->offset = pc; 1936 args->line = buf + prev_buf_size; 1937 args->line_nr = 0; 1938 args->fileloc = NULL; 1939 args->ms.sym = sym; 1940 dl = disasm_line__new(args); 1941 if (dl) 1942 annotation_line__add(&dl->al, ¬es->src->source); 1943 1944 pc += count; 1945 } while (count > 0 && pc < len); 1946 1947 ret = 0; 1948 out: 1949 free(prog_linfo); 1950 btf__free(btf); 1951 fclose(s); 1952 bfd_close(bfdf); 1953 return ret; 1954 } 1955 #else // defined(HAVE_LIBBFD_SUPPORT) && defined(HAVE_LIBBPF_SUPPORT) 1956 static int symbol__disassemble_bpf(struct symbol *sym __maybe_unused, 1957 struct annotate_args *args __maybe_unused) 1958 { 1959 return SYMBOL_ANNOTATE_ERRNO__NO_LIBOPCODES_FOR_BPF; 1960 } 1961 #endif // defined(HAVE_LIBBFD_SUPPORT) && defined(HAVE_LIBBPF_SUPPORT) 1962 1963 static int 1964 symbol__disassemble_bpf_image(struct symbol *sym, 1965 struct annotate_args *args) 1966 { 1967 struct annotation *notes = symbol__annotation(sym); 1968 struct disasm_line *dl; 1969 1970 args->offset = -1; 1971 args->line = strdup("to be implemented"); 1972 args->line_nr = 0; 1973 args->fileloc = NULL; 1974 dl = disasm_line__new(args); 1975 if (dl) 1976 annotation_line__add(&dl->al, ¬es->src->source); 1977 1978 zfree(&args->line); 1979 return 0; 1980 } 1981 1982 /* 1983 * Possibly create a new version of line with tabs expanded. Returns the 1984 * existing or new line, storage is updated if a new line is allocated. If 1985 * allocation fails then NULL is returned. 1986 */ 1987 static char *expand_tabs(char *line, char **storage, size_t *storage_len) 1988 { 1989 size_t i, src, dst, len, new_storage_len, num_tabs; 1990 char *new_line; 1991 size_t line_len = strlen(line); 1992 1993 for (num_tabs = 0, i = 0; i < line_len; i++) 1994 if (line[i] == '\t') 1995 num_tabs++; 1996 1997 if (num_tabs == 0) 1998 return line; 1999 2000 /* 2001 * Space for the line and '\0', less the leading and trailing 2002 * spaces. Each tab may introduce 7 additional spaces. 2003 */ 2004 new_storage_len = line_len + 1 + (num_tabs * 7); 2005 2006 new_line = malloc(new_storage_len); 2007 if (new_line == NULL) { 2008 pr_err("Failure allocating memory for tab expansion\n"); 2009 return NULL; 2010 } 2011 2012 /* 2013 * Copy regions starting at src and expand tabs. If there are two 2014 * adjacent tabs then 'src == i', the memcpy is of size 0 and the spaces 2015 * are inserted. 2016 */ 2017 for (i = 0, src = 0, dst = 0; i < line_len && num_tabs; i++) { 2018 if (line[i] == '\t') { 2019 len = i - src; 2020 memcpy(&new_line[dst], &line[src], len); 2021 dst += len; 2022 new_line[dst++] = ' '; 2023 while (dst % 8 != 0) 2024 new_line[dst++] = ' '; 2025 src = i + 1; 2026 num_tabs--; 2027 } 2028 } 2029 2030 /* Expand the last region. */ 2031 len = line_len - src; 2032 memcpy(&new_line[dst], &line[src], len); 2033 dst += len; 2034 new_line[dst] = '\0'; 2035 2036 free(*storage); 2037 *storage = new_line; 2038 *storage_len = new_storage_len; 2039 return new_line; 2040 2041 } 2042 2043 static int symbol__disassemble(struct symbol *sym, struct annotate_args *args) 2044 { 2045 struct annotation_options *opts = args->options; 2046 struct map *map = args->ms.map; 2047 struct dso *dso = map__dso(map); 2048 char *command; 2049 FILE *file; 2050 char symfs_filename[PATH_MAX]; 2051 struct kcore_extract kce; 2052 bool delete_extract = false; 2053 bool decomp = false; 2054 int lineno = 0; 2055 char *fileloc = NULL; 2056 int nline; 2057 char *line; 2058 size_t line_len; 2059 const char *objdump_argv[] = { 2060 "/bin/sh", 2061 "-c", 2062 NULL, /* Will be the objdump command to run. */ 2063 "--", 2064 NULL, /* Will be the symfs path. */ 2065 NULL, 2066 }; 2067 struct child_process objdump_process; 2068 int err = dso__disassemble_filename(dso, symfs_filename, sizeof(symfs_filename)); 2069 2070 if (err) 2071 return err; 2072 2073 pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__, 2074 symfs_filename, sym->name, map__unmap_ip(map, sym->start), 2075 map__unmap_ip(map, sym->end)); 2076 2077 pr_debug("annotating [%p] %30s : [%p] %30s\n", 2078 dso, dso->long_name, sym, sym->name); 2079 2080 if (dso->binary_type == DSO_BINARY_TYPE__BPF_PROG_INFO) { 2081 return symbol__disassemble_bpf(sym, args); 2082 } else if (dso->binary_type == DSO_BINARY_TYPE__BPF_IMAGE) { 2083 return symbol__disassemble_bpf_image(sym, args); 2084 } else if (dso__is_kcore(dso)) { 2085 kce.kcore_filename = symfs_filename; 2086 kce.addr = map__rip_2objdump(map, sym->start); 2087 kce.offs = sym->start; 2088 kce.len = sym->end - sym->start; 2089 if (!kcore_extract__create(&kce)) { 2090 delete_extract = true; 2091 strlcpy(symfs_filename, kce.extract_filename, 2092 sizeof(symfs_filename)); 2093 } 2094 } else if (dso__needs_decompress(dso)) { 2095 char tmp[KMOD_DECOMP_LEN]; 2096 2097 if (dso__decompress_kmodule_path(dso, symfs_filename, 2098 tmp, sizeof(tmp)) < 0) 2099 return -1; 2100 2101 decomp = true; 2102 strcpy(symfs_filename, tmp); 2103 } 2104 2105 err = asprintf(&command, 2106 "%s %s%s --start-address=0x%016" PRIx64 2107 " --stop-address=0x%016" PRIx64 2108 " -l -d %s %s %s %c%s%c %s%s -C \"$1\"", 2109 opts->objdump_path ?: "objdump", 2110 opts->disassembler_style ? "-M " : "", 2111 opts->disassembler_style ?: "", 2112 map__rip_2objdump(map, sym->start), 2113 map__rip_2objdump(map, sym->end), 2114 opts->show_asm_raw ? "" : "--no-show-raw-insn", 2115 opts->annotate_src ? "-S" : "", 2116 opts->prefix ? "--prefix " : "", 2117 opts->prefix ? '"' : ' ', 2118 opts->prefix ?: "", 2119 opts->prefix ? '"' : ' ', 2120 opts->prefix_strip ? "--prefix-strip=" : "", 2121 opts->prefix_strip ?: ""); 2122 2123 if (err < 0) { 2124 pr_err("Failure allocating memory for the command to run\n"); 2125 goto out_remove_tmp; 2126 } 2127 2128 pr_debug("Executing: %s\n", command); 2129 2130 objdump_argv[2] = command; 2131 objdump_argv[4] = symfs_filename; 2132 2133 /* Create a pipe to read from for stdout */ 2134 memset(&objdump_process, 0, sizeof(objdump_process)); 2135 objdump_process.argv = objdump_argv; 2136 objdump_process.out = -1; 2137 objdump_process.err = -1; 2138 objdump_process.no_stderr = 1; 2139 if (start_command(&objdump_process)) { 2140 pr_err("Failure starting to run %s\n", command); 2141 err = -1; 2142 goto out_free_command; 2143 } 2144 2145 file = fdopen(objdump_process.out, "r"); 2146 if (!file) { 2147 pr_err("Failure creating FILE stream for %s\n", command); 2148 /* 2149 * If we were using debug info should retry with 2150 * original binary. 2151 */ 2152 err = -1; 2153 goto out_close_stdout; 2154 } 2155 2156 /* Storage for getline. */ 2157 line = NULL; 2158 line_len = 0; 2159 2160 nline = 0; 2161 while (!feof(file)) { 2162 const char *match; 2163 char *expanded_line; 2164 2165 if (getline(&line, &line_len, file) < 0 || !line) 2166 break; 2167 2168 /* Skip lines containing "filename:" */ 2169 match = strstr(line, symfs_filename); 2170 if (match && match[strlen(symfs_filename)] == ':') 2171 continue; 2172 2173 expanded_line = strim(line); 2174 expanded_line = expand_tabs(expanded_line, &line, &line_len); 2175 if (!expanded_line) 2176 break; 2177 2178 /* 2179 * The source code line number (lineno) needs to be kept in 2180 * across calls to symbol__parse_objdump_line(), so that it 2181 * can associate it with the instructions till the next one. 2182 * See disasm_line__new() and struct disasm_line::line_nr. 2183 */ 2184 if (symbol__parse_objdump_line(sym, args, expanded_line, 2185 &lineno, &fileloc) < 0) 2186 break; 2187 nline++; 2188 } 2189 free(line); 2190 free(fileloc); 2191 2192 err = finish_command(&objdump_process); 2193 if (err) 2194 pr_err("Error running %s\n", command); 2195 2196 if (nline == 0) { 2197 err = -1; 2198 pr_err("No output from %s\n", command); 2199 } 2200 2201 /* 2202 * kallsyms does not have symbol sizes so there may a nop at the end. 2203 * Remove it. 2204 */ 2205 if (dso__is_kcore(dso)) 2206 delete_last_nop(sym); 2207 2208 fclose(file); 2209 2210 out_close_stdout: 2211 close(objdump_process.out); 2212 2213 out_free_command: 2214 free(command); 2215 2216 out_remove_tmp: 2217 if (decomp) 2218 unlink(symfs_filename); 2219 2220 if (delete_extract) 2221 kcore_extract__delete(&kce); 2222 2223 return err; 2224 } 2225 2226 static void calc_percent(struct sym_hist *sym_hist, 2227 struct hists *hists, 2228 struct annotation_data *data, 2229 s64 offset, s64 end) 2230 { 2231 unsigned int hits = 0; 2232 u64 period = 0; 2233 2234 while (offset < end) { 2235 hits += sym_hist->addr[offset].nr_samples; 2236 period += sym_hist->addr[offset].period; 2237 ++offset; 2238 } 2239 2240 if (sym_hist->nr_samples) { 2241 data->he.period = period; 2242 data->he.nr_samples = hits; 2243 data->percent[PERCENT_HITS_LOCAL] = 100.0 * hits / sym_hist->nr_samples; 2244 } 2245 2246 if (hists->stats.nr_non_filtered_samples) 2247 data->percent[PERCENT_HITS_GLOBAL] = 100.0 * hits / hists->stats.nr_non_filtered_samples; 2248 2249 if (sym_hist->period) 2250 data->percent[PERCENT_PERIOD_LOCAL] = 100.0 * period / sym_hist->period; 2251 2252 if (hists->stats.total_period) 2253 data->percent[PERCENT_PERIOD_GLOBAL] = 100.0 * period / hists->stats.total_period; 2254 } 2255 2256 static void annotation__calc_percent(struct annotation *notes, 2257 struct evsel *leader, s64 len) 2258 { 2259 struct annotation_line *al, *next; 2260 struct evsel *evsel; 2261 2262 list_for_each_entry(al, ¬es->src->source, node) { 2263 s64 end; 2264 int i = 0; 2265 2266 if (al->offset == -1) 2267 continue; 2268 2269 next = annotation_line__next(al, ¬es->src->source); 2270 end = next ? next->offset : len; 2271 2272 for_each_group_evsel(evsel, leader) { 2273 struct hists *hists = evsel__hists(evsel); 2274 struct annotation_data *data; 2275 struct sym_hist *sym_hist; 2276 2277 BUG_ON(i >= al->data_nr); 2278 2279 sym_hist = annotation__histogram(notes, evsel->core.idx); 2280 data = &al->data[i++]; 2281 2282 calc_percent(sym_hist, hists, data, al->offset, end); 2283 } 2284 } 2285 } 2286 2287 void symbol__calc_percent(struct symbol *sym, struct evsel *evsel) 2288 { 2289 struct annotation *notes = symbol__annotation(sym); 2290 2291 annotation__calc_percent(notes, evsel, symbol__size(sym)); 2292 } 2293 2294 int symbol__annotate(struct map_symbol *ms, struct evsel *evsel, 2295 struct annotation_options *options, struct arch **parch) 2296 { 2297 struct symbol *sym = ms->sym; 2298 struct annotation *notes = symbol__annotation(sym); 2299 struct annotate_args args = { 2300 .evsel = evsel, 2301 .options = options, 2302 }; 2303 struct perf_env *env = evsel__env(evsel); 2304 const char *arch_name = perf_env__arch(env); 2305 struct arch *arch; 2306 int err; 2307 2308 if (!arch_name) 2309 return errno; 2310 2311 args.arch = arch = arch__find(arch_name); 2312 if (arch == NULL) { 2313 pr_err("%s: unsupported arch %s\n", __func__, arch_name); 2314 return ENOTSUP; 2315 } 2316 2317 if (parch) 2318 *parch = arch; 2319 2320 if (arch->init) { 2321 err = arch->init(arch, env ? env->cpuid : NULL); 2322 if (err) { 2323 pr_err("%s: failed to initialize %s arch priv area\n", __func__, arch->name); 2324 return err; 2325 } 2326 } 2327 2328 args.ms = *ms; 2329 if (notes->options && notes->options->full_addr) 2330 notes->start = map__objdump_2mem(ms->map, ms->sym->start); 2331 else 2332 notes->start = map__rip_2objdump(ms->map, ms->sym->start); 2333 2334 return symbol__disassemble(sym, &args); 2335 } 2336 2337 static void insert_source_line(struct rb_root *root, struct annotation_line *al, 2338 struct annotation_options *opts) 2339 { 2340 struct annotation_line *iter; 2341 struct rb_node **p = &root->rb_node; 2342 struct rb_node *parent = NULL; 2343 int i, ret; 2344 2345 while (*p != NULL) { 2346 parent = *p; 2347 iter = rb_entry(parent, struct annotation_line, rb_node); 2348 2349 ret = strcmp(iter->path, al->path); 2350 if (ret == 0) { 2351 for (i = 0; i < al->data_nr; i++) { 2352 iter->data[i].percent_sum += annotation_data__percent(&al->data[i], 2353 opts->percent_type); 2354 } 2355 return; 2356 } 2357 2358 if (ret < 0) 2359 p = &(*p)->rb_left; 2360 else 2361 p = &(*p)->rb_right; 2362 } 2363 2364 for (i = 0; i < al->data_nr; i++) { 2365 al->data[i].percent_sum = annotation_data__percent(&al->data[i], 2366 opts->percent_type); 2367 } 2368 2369 rb_link_node(&al->rb_node, parent, p); 2370 rb_insert_color(&al->rb_node, root); 2371 } 2372 2373 static int cmp_source_line(struct annotation_line *a, struct annotation_line *b) 2374 { 2375 int i; 2376 2377 for (i = 0; i < a->data_nr; i++) { 2378 if (a->data[i].percent_sum == b->data[i].percent_sum) 2379 continue; 2380 return a->data[i].percent_sum > b->data[i].percent_sum; 2381 } 2382 2383 return 0; 2384 } 2385 2386 static void __resort_source_line(struct rb_root *root, struct annotation_line *al) 2387 { 2388 struct annotation_line *iter; 2389 struct rb_node **p = &root->rb_node; 2390 struct rb_node *parent = NULL; 2391 2392 while (*p != NULL) { 2393 parent = *p; 2394 iter = rb_entry(parent, struct annotation_line, rb_node); 2395 2396 if (cmp_source_line(al, iter)) 2397 p = &(*p)->rb_left; 2398 else 2399 p = &(*p)->rb_right; 2400 } 2401 2402 rb_link_node(&al->rb_node, parent, p); 2403 rb_insert_color(&al->rb_node, root); 2404 } 2405 2406 static void resort_source_line(struct rb_root *dest_root, struct rb_root *src_root) 2407 { 2408 struct annotation_line *al; 2409 struct rb_node *node; 2410 2411 node = rb_first(src_root); 2412 while (node) { 2413 struct rb_node *next; 2414 2415 al = rb_entry(node, struct annotation_line, rb_node); 2416 next = rb_next(node); 2417 rb_erase(node, src_root); 2418 2419 __resort_source_line(dest_root, al); 2420 node = next; 2421 } 2422 } 2423 2424 static void print_summary(struct rb_root *root, const char *filename) 2425 { 2426 struct annotation_line *al; 2427 struct rb_node *node; 2428 2429 printf("\nSorted summary for file %s\n", filename); 2430 printf("----------------------------------------------\n\n"); 2431 2432 if (RB_EMPTY_ROOT(root)) { 2433 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN); 2434 return; 2435 } 2436 2437 node = rb_first(root); 2438 while (node) { 2439 double percent, percent_max = 0.0; 2440 const char *color; 2441 char *path; 2442 int i; 2443 2444 al = rb_entry(node, struct annotation_line, rb_node); 2445 for (i = 0; i < al->data_nr; i++) { 2446 percent = al->data[i].percent_sum; 2447 color = get_percent_color(percent); 2448 color_fprintf(stdout, color, " %7.2f", percent); 2449 2450 if (percent > percent_max) 2451 percent_max = percent; 2452 } 2453 2454 path = al->path; 2455 color = get_percent_color(percent_max); 2456 color_fprintf(stdout, color, " %s\n", path); 2457 2458 node = rb_next(node); 2459 } 2460 } 2461 2462 static void symbol__annotate_hits(struct symbol *sym, struct evsel *evsel) 2463 { 2464 struct annotation *notes = symbol__annotation(sym); 2465 struct sym_hist *h = annotation__histogram(notes, evsel->core.idx); 2466 u64 len = symbol__size(sym), offset; 2467 2468 for (offset = 0; offset < len; ++offset) 2469 if (h->addr[offset].nr_samples != 0) 2470 printf("%*" PRIx64 ": %" PRIu64 "\n", BITS_PER_LONG / 2, 2471 sym->start + offset, h->addr[offset].nr_samples); 2472 printf("%*s: %" PRIu64 "\n", BITS_PER_LONG / 2, "h->nr_samples", h->nr_samples); 2473 } 2474 2475 static int annotated_source__addr_fmt_width(struct list_head *lines, u64 start) 2476 { 2477 char bf[32]; 2478 struct annotation_line *line; 2479 2480 list_for_each_entry_reverse(line, lines, node) { 2481 if (line->offset != -1) 2482 return scnprintf(bf, sizeof(bf), "%" PRIx64, start + line->offset); 2483 } 2484 2485 return 0; 2486 } 2487 2488 int symbol__annotate_printf(struct map_symbol *ms, struct evsel *evsel, 2489 struct annotation_options *opts) 2490 { 2491 struct map *map = ms->map; 2492 struct symbol *sym = ms->sym; 2493 struct dso *dso = map__dso(map); 2494 char *filename; 2495 const char *d_filename; 2496 const char *evsel_name = evsel__name(evsel); 2497 struct annotation *notes = symbol__annotation(sym); 2498 struct sym_hist *h = annotation__histogram(notes, evsel->core.idx); 2499 struct annotation_line *pos, *queue = NULL; 2500 u64 start = map__rip_2objdump(map, sym->start); 2501 int printed = 2, queue_len = 0, addr_fmt_width; 2502 int more = 0; 2503 bool context = opts->context; 2504 u64 len; 2505 int width = symbol_conf.show_total_period ? 12 : 8; 2506 int graph_dotted_len; 2507 char buf[512]; 2508 2509 filename = strdup(dso->long_name); 2510 if (!filename) 2511 return -ENOMEM; 2512 2513 if (opts->full_path) 2514 d_filename = filename; 2515 else 2516 d_filename = basename(filename); 2517 2518 len = symbol__size(sym); 2519 2520 if (evsel__is_group_event(evsel)) { 2521 width *= evsel->core.nr_members; 2522 evsel__group_desc(evsel, buf, sizeof(buf)); 2523 evsel_name = buf; 2524 } 2525 2526 graph_dotted_len = printf(" %-*.*s| Source code & Disassembly of %s for %s (%" PRIu64 " samples, " 2527 "percent: %s)\n", 2528 width, width, symbol_conf.show_total_period ? "Period" : 2529 symbol_conf.show_nr_samples ? "Samples" : "Percent", 2530 d_filename, evsel_name, h->nr_samples, 2531 percent_type_str(opts->percent_type)); 2532 2533 printf("%-*.*s----\n", 2534 graph_dotted_len, graph_dotted_len, graph_dotted_line); 2535 2536 if (verbose > 0) 2537 symbol__annotate_hits(sym, evsel); 2538 2539 addr_fmt_width = annotated_source__addr_fmt_width(¬es->src->source, start); 2540 2541 list_for_each_entry(pos, ¬es->src->source, node) { 2542 int err; 2543 2544 if (context && queue == NULL) { 2545 queue = pos; 2546 queue_len = 0; 2547 } 2548 2549 err = annotation_line__print(pos, sym, start, evsel, len, 2550 opts->min_pcnt, printed, opts->max_lines, 2551 queue, addr_fmt_width, opts->percent_type); 2552 2553 switch (err) { 2554 case 0: 2555 ++printed; 2556 if (context) { 2557 printed += queue_len; 2558 queue = NULL; 2559 queue_len = 0; 2560 } 2561 break; 2562 case 1: 2563 /* filtered by max_lines */ 2564 ++more; 2565 break; 2566 case -1: 2567 default: 2568 /* 2569 * Filtered by min_pcnt or non IP lines when 2570 * context != 0 2571 */ 2572 if (!context) 2573 break; 2574 if (queue_len == context) 2575 queue = list_entry(queue->node.next, typeof(*queue), node); 2576 else 2577 ++queue_len; 2578 break; 2579 } 2580 } 2581 2582 free(filename); 2583 2584 return more; 2585 } 2586 2587 static void FILE__set_percent_color(void *fp __maybe_unused, 2588 double percent __maybe_unused, 2589 bool current __maybe_unused) 2590 { 2591 } 2592 2593 static int FILE__set_jumps_percent_color(void *fp __maybe_unused, 2594 int nr __maybe_unused, bool current __maybe_unused) 2595 { 2596 return 0; 2597 } 2598 2599 static int FILE__set_color(void *fp __maybe_unused, int color __maybe_unused) 2600 { 2601 return 0; 2602 } 2603 2604 static void FILE__printf(void *fp, const char *fmt, ...) 2605 { 2606 va_list args; 2607 2608 va_start(args, fmt); 2609 vfprintf(fp, fmt, args); 2610 va_end(args); 2611 } 2612 2613 static void FILE__write_graph(void *fp, int graph) 2614 { 2615 const char *s; 2616 switch (graph) { 2617 2618 case DARROW_CHAR: s = "↓"; break; 2619 case UARROW_CHAR: s = "↑"; break; 2620 case LARROW_CHAR: s = "←"; break; 2621 case RARROW_CHAR: s = "→"; break; 2622 default: s = "?"; break; 2623 } 2624 2625 fputs(s, fp); 2626 } 2627 2628 static int symbol__annotate_fprintf2(struct symbol *sym, FILE *fp, 2629 struct annotation_options *opts) 2630 { 2631 struct annotation *notes = symbol__annotation(sym); 2632 struct annotation_write_ops wops = { 2633 .first_line = true, 2634 .obj = fp, 2635 .set_color = FILE__set_color, 2636 .set_percent_color = FILE__set_percent_color, 2637 .set_jumps_percent_color = FILE__set_jumps_percent_color, 2638 .printf = FILE__printf, 2639 .write_graph = FILE__write_graph, 2640 }; 2641 struct annotation_line *al; 2642 2643 list_for_each_entry(al, ¬es->src->source, node) { 2644 if (annotation_line__filter(al, notes)) 2645 continue; 2646 annotation_line__write(al, notes, &wops, opts); 2647 fputc('\n', fp); 2648 wops.first_line = false; 2649 } 2650 2651 return 0; 2652 } 2653 2654 int map_symbol__annotation_dump(struct map_symbol *ms, struct evsel *evsel, 2655 struct annotation_options *opts) 2656 { 2657 const char *ev_name = evsel__name(evsel); 2658 char buf[1024]; 2659 char *filename; 2660 int err = -1; 2661 FILE *fp; 2662 2663 if (asprintf(&filename, "%s.annotation", ms->sym->name) < 0) 2664 return -1; 2665 2666 fp = fopen(filename, "w"); 2667 if (fp == NULL) 2668 goto out_free_filename; 2669 2670 if (evsel__is_group_event(evsel)) { 2671 evsel__group_desc(evsel, buf, sizeof(buf)); 2672 ev_name = buf; 2673 } 2674 2675 fprintf(fp, "%s() %s\nEvent: %s\n\n", 2676 ms->sym->name, map__dso(ms->map)->long_name, ev_name); 2677 symbol__annotate_fprintf2(ms->sym, fp, opts); 2678 2679 fclose(fp); 2680 err = 0; 2681 out_free_filename: 2682 free(filename); 2683 return err; 2684 } 2685 2686 void symbol__annotate_zero_histogram(struct symbol *sym, int evidx) 2687 { 2688 struct annotation *notes = symbol__annotation(sym); 2689 struct sym_hist *h = annotation__histogram(notes, evidx); 2690 2691 memset(h, 0, notes->src->sizeof_sym_hist); 2692 } 2693 2694 void symbol__annotate_decay_histogram(struct symbol *sym, int evidx) 2695 { 2696 struct annotation *notes = symbol__annotation(sym); 2697 struct sym_hist *h = annotation__histogram(notes, evidx); 2698 int len = symbol__size(sym), offset; 2699 2700 h->nr_samples = 0; 2701 for (offset = 0; offset < len; ++offset) { 2702 h->addr[offset].nr_samples = h->addr[offset].nr_samples * 7 / 8; 2703 h->nr_samples += h->addr[offset].nr_samples; 2704 } 2705 } 2706 2707 void annotated_source__purge(struct annotated_source *as) 2708 { 2709 struct annotation_line *al, *n; 2710 2711 list_for_each_entry_safe(al, n, &as->source, node) { 2712 list_del_init(&al->node); 2713 disasm_line__free(disasm_line(al)); 2714 } 2715 } 2716 2717 static size_t disasm_line__fprintf(struct disasm_line *dl, FILE *fp) 2718 { 2719 size_t printed; 2720 2721 if (dl->al.offset == -1) 2722 return fprintf(fp, "%s\n", dl->al.line); 2723 2724 printed = fprintf(fp, "%#" PRIx64 " %s", dl->al.offset, dl->ins.name); 2725 2726 if (dl->ops.raw[0] != '\0') { 2727 printed += fprintf(fp, "%.*s %s\n", 6 - (int)printed, " ", 2728 dl->ops.raw); 2729 } 2730 2731 return printed + fprintf(fp, "\n"); 2732 } 2733 2734 size_t disasm__fprintf(struct list_head *head, FILE *fp) 2735 { 2736 struct disasm_line *pos; 2737 size_t printed = 0; 2738 2739 list_for_each_entry(pos, head, al.node) 2740 printed += disasm_line__fprintf(pos, fp); 2741 2742 return printed; 2743 } 2744 2745 bool disasm_line__is_valid_local_jump(struct disasm_line *dl, struct symbol *sym) 2746 { 2747 if (!dl || !dl->ins.ops || !ins__is_jump(&dl->ins) || 2748 !disasm_line__has_local_offset(dl) || dl->ops.target.offset < 0 || 2749 dl->ops.target.offset >= (s64)symbol__size(sym)) 2750 return false; 2751 2752 return true; 2753 } 2754 2755 void annotation__mark_jump_targets(struct annotation *notes, struct symbol *sym) 2756 { 2757 u64 offset, size = symbol__size(sym); 2758 2759 /* PLT symbols contain external offsets */ 2760 if (strstr(sym->name, "@plt")) 2761 return; 2762 2763 for (offset = 0; offset < size; ++offset) { 2764 struct annotation_line *al = notes->offsets[offset]; 2765 struct disasm_line *dl; 2766 2767 dl = disasm_line(al); 2768 2769 if (!disasm_line__is_valid_local_jump(dl, sym)) 2770 continue; 2771 2772 al = notes->offsets[dl->ops.target.offset]; 2773 2774 /* 2775 * FIXME: Oops, no jump target? Buggy disassembler? Or do we 2776 * have to adjust to the previous offset? 2777 */ 2778 if (al == NULL) 2779 continue; 2780 2781 if (++al->jump_sources > notes->max_jump_sources) 2782 notes->max_jump_sources = al->jump_sources; 2783 } 2784 } 2785 2786 void annotation__set_offsets(struct annotation *notes, s64 size) 2787 { 2788 struct annotation_line *al; 2789 2790 notes->max_line_len = 0; 2791 notes->nr_entries = 0; 2792 notes->nr_asm_entries = 0; 2793 2794 list_for_each_entry(al, ¬es->src->source, node) { 2795 size_t line_len = strlen(al->line); 2796 2797 if (notes->max_line_len < line_len) 2798 notes->max_line_len = line_len; 2799 al->idx = notes->nr_entries++; 2800 if (al->offset != -1) { 2801 al->idx_asm = notes->nr_asm_entries++; 2802 /* 2803 * FIXME: short term bandaid to cope with assembly 2804 * routines that comes with labels in the same column 2805 * as the address in objdump, sigh. 2806 * 2807 * E.g. copy_user_generic_unrolled 2808 */ 2809 if (al->offset < size) 2810 notes->offsets[al->offset] = al; 2811 } else 2812 al->idx_asm = -1; 2813 } 2814 } 2815 2816 static inline int width_jumps(int n) 2817 { 2818 if (n >= 100) 2819 return 5; 2820 if (n / 10) 2821 return 2; 2822 return 1; 2823 } 2824 2825 static int annotation__max_ins_name(struct annotation *notes) 2826 { 2827 int max_name = 0, len; 2828 struct annotation_line *al; 2829 2830 list_for_each_entry(al, ¬es->src->source, node) { 2831 if (al->offset == -1) 2832 continue; 2833 2834 len = strlen(disasm_line(al)->ins.name); 2835 if (max_name < len) 2836 max_name = len; 2837 } 2838 2839 return max_name; 2840 } 2841 2842 void annotation__init_column_widths(struct annotation *notes, struct symbol *sym) 2843 { 2844 notes->widths.addr = notes->widths.target = 2845 notes->widths.min_addr = hex_width(symbol__size(sym)); 2846 notes->widths.max_addr = hex_width(sym->end); 2847 notes->widths.jumps = width_jumps(notes->max_jump_sources); 2848 notes->widths.max_ins_name = annotation__max_ins_name(notes); 2849 } 2850 2851 void annotation__update_column_widths(struct annotation *notes) 2852 { 2853 if (notes->options->use_offset) 2854 notes->widths.target = notes->widths.min_addr; 2855 else if (notes->options->full_addr) 2856 notes->widths.target = BITS_PER_LONG / 4; 2857 else 2858 notes->widths.target = notes->widths.max_addr; 2859 2860 notes->widths.addr = notes->widths.target; 2861 2862 if (notes->options->show_nr_jumps) 2863 notes->widths.addr += notes->widths.jumps + 1; 2864 } 2865 2866 void annotation__toggle_full_addr(struct annotation *notes, struct map_symbol *ms) 2867 { 2868 notes->options->full_addr = !notes->options->full_addr; 2869 2870 if (notes->options->full_addr) 2871 notes->start = map__objdump_2mem(ms->map, ms->sym->start); 2872 else 2873 notes->start = map__rip_2objdump(ms->map, ms->sym->start); 2874 2875 annotation__update_column_widths(notes); 2876 } 2877 2878 static void annotation__calc_lines(struct annotation *notes, struct map *map, 2879 struct rb_root *root, 2880 struct annotation_options *opts) 2881 { 2882 struct annotation_line *al; 2883 struct rb_root tmp_root = RB_ROOT; 2884 2885 list_for_each_entry(al, ¬es->src->source, node) { 2886 double percent_max = 0.0; 2887 int i; 2888 2889 for (i = 0; i < al->data_nr; i++) { 2890 double percent; 2891 2892 percent = annotation_data__percent(&al->data[i], 2893 opts->percent_type); 2894 2895 if (percent > percent_max) 2896 percent_max = percent; 2897 } 2898 2899 if (percent_max <= 0.5) 2900 continue; 2901 2902 al->path = get_srcline(map__dso(map), notes->start + al->offset, NULL, 2903 false, true, notes->start + al->offset); 2904 insert_source_line(&tmp_root, al, opts); 2905 } 2906 2907 resort_source_line(root, &tmp_root); 2908 } 2909 2910 static void symbol__calc_lines(struct map_symbol *ms, struct rb_root *root, 2911 struct annotation_options *opts) 2912 { 2913 struct annotation *notes = symbol__annotation(ms->sym); 2914 2915 annotation__calc_lines(notes, ms->map, root, opts); 2916 } 2917 2918 int symbol__tty_annotate2(struct map_symbol *ms, struct evsel *evsel, 2919 struct annotation_options *opts) 2920 { 2921 struct dso *dso = map__dso(ms->map); 2922 struct symbol *sym = ms->sym; 2923 struct rb_root source_line = RB_ROOT; 2924 struct hists *hists = evsel__hists(evsel); 2925 char buf[1024]; 2926 int err; 2927 2928 err = symbol__annotate2(ms, evsel, opts, NULL); 2929 if (err) { 2930 char msg[BUFSIZ]; 2931 2932 dso->annotate_warned = true; 2933 symbol__strerror_disassemble(ms, err, msg, sizeof(msg)); 2934 ui__error("Couldn't annotate %s:\n%s", sym->name, msg); 2935 return -1; 2936 } 2937 2938 if (opts->print_lines) { 2939 srcline_full_filename = opts->full_path; 2940 symbol__calc_lines(ms, &source_line, opts); 2941 print_summary(&source_line, dso->long_name); 2942 } 2943 2944 hists__scnprintf_title(hists, buf, sizeof(buf)); 2945 fprintf(stdout, "%s, [percent: %s]\n%s() %s\n", 2946 buf, percent_type_str(opts->percent_type), sym->name, dso->long_name); 2947 symbol__annotate_fprintf2(sym, stdout, opts); 2948 2949 annotated_source__purge(symbol__annotation(sym)->src); 2950 2951 return 0; 2952 } 2953 2954 int symbol__tty_annotate(struct map_symbol *ms, struct evsel *evsel, 2955 struct annotation_options *opts) 2956 { 2957 struct dso *dso = map__dso(ms->map); 2958 struct symbol *sym = ms->sym; 2959 struct rb_root source_line = RB_ROOT; 2960 int err; 2961 2962 err = symbol__annotate(ms, evsel, opts, NULL); 2963 if (err) { 2964 char msg[BUFSIZ]; 2965 2966 dso->annotate_warned = true; 2967 symbol__strerror_disassemble(ms, err, msg, sizeof(msg)); 2968 ui__error("Couldn't annotate %s:\n%s", sym->name, msg); 2969 return -1; 2970 } 2971 2972 symbol__calc_percent(sym, evsel); 2973 2974 if (opts->print_lines) { 2975 srcline_full_filename = opts->full_path; 2976 symbol__calc_lines(ms, &source_line, opts); 2977 print_summary(&source_line, dso->long_name); 2978 } 2979 2980 symbol__annotate_printf(ms, evsel, opts); 2981 2982 annotated_source__purge(symbol__annotation(sym)->src); 2983 2984 return 0; 2985 } 2986 2987 bool ui__has_annotation(void) 2988 { 2989 return use_browser == 1 && perf_hpp_list.sym; 2990 } 2991 2992 2993 static double annotation_line__max_percent(struct annotation_line *al, 2994 struct annotation *notes, 2995 unsigned int percent_type) 2996 { 2997 double percent_max = 0.0; 2998 int i; 2999 3000 for (i = 0; i < notes->nr_events; i++) { 3001 double percent; 3002 3003 percent = annotation_data__percent(&al->data[i], 3004 percent_type); 3005 3006 if (percent > percent_max) 3007 percent_max = percent; 3008 } 3009 3010 return percent_max; 3011 } 3012 3013 static void disasm_line__write(struct disasm_line *dl, struct annotation *notes, 3014 void *obj, char *bf, size_t size, 3015 void (*obj__printf)(void *obj, const char *fmt, ...), 3016 void (*obj__write_graph)(void *obj, int graph)) 3017 { 3018 if (dl->ins.ops && dl->ins.ops->scnprintf) { 3019 if (ins__is_jump(&dl->ins)) { 3020 bool fwd; 3021 3022 if (dl->ops.target.outside) 3023 goto call_like; 3024 fwd = dl->ops.target.offset > dl->al.offset; 3025 obj__write_graph(obj, fwd ? DARROW_CHAR : UARROW_CHAR); 3026 obj__printf(obj, " "); 3027 } else if (ins__is_call(&dl->ins)) { 3028 call_like: 3029 obj__write_graph(obj, RARROW_CHAR); 3030 obj__printf(obj, " "); 3031 } else if (ins__is_ret(&dl->ins)) { 3032 obj__write_graph(obj, LARROW_CHAR); 3033 obj__printf(obj, " "); 3034 } else { 3035 obj__printf(obj, " "); 3036 } 3037 } else { 3038 obj__printf(obj, " "); 3039 } 3040 3041 disasm_line__scnprintf(dl, bf, size, !notes->options->use_offset, notes->widths.max_ins_name); 3042 } 3043 3044 static void ipc_coverage_string(char *bf, int size, struct annotation *notes) 3045 { 3046 double ipc = 0.0, coverage = 0.0; 3047 3048 if (notes->hit_cycles) 3049 ipc = notes->hit_insn / ((double)notes->hit_cycles); 3050 3051 if (notes->total_insn) { 3052 coverage = notes->cover_insn * 100.0 / 3053 ((double)notes->total_insn); 3054 } 3055 3056 scnprintf(bf, size, "(Average IPC: %.2f, IPC Coverage: %.1f%%)", 3057 ipc, coverage); 3058 } 3059 3060 static void __annotation_line__write(struct annotation_line *al, struct annotation *notes, 3061 bool first_line, bool current_entry, bool change_color, int width, 3062 void *obj, unsigned int percent_type, 3063 int (*obj__set_color)(void *obj, int color), 3064 void (*obj__set_percent_color)(void *obj, double percent, bool current), 3065 int (*obj__set_jumps_percent_color)(void *obj, int nr, bool current), 3066 void (*obj__printf)(void *obj, const char *fmt, ...), 3067 void (*obj__write_graph)(void *obj, int graph)) 3068 3069 { 3070 double percent_max = annotation_line__max_percent(al, notes, percent_type); 3071 int pcnt_width = annotation__pcnt_width(notes), 3072 cycles_width = annotation__cycles_width(notes); 3073 bool show_title = false; 3074 char bf[256]; 3075 int printed; 3076 3077 if (first_line && (al->offset == -1 || percent_max == 0.0)) { 3078 if (notes->have_cycles) { 3079 if (al->ipc == 0.0 && al->cycles == 0) 3080 show_title = true; 3081 } else 3082 show_title = true; 3083 } 3084 3085 if (al->offset != -1 && percent_max != 0.0) { 3086 int i; 3087 3088 for (i = 0; i < notes->nr_events; i++) { 3089 double percent; 3090 3091 percent = annotation_data__percent(&al->data[i], percent_type); 3092 3093 obj__set_percent_color(obj, percent, current_entry); 3094 if (symbol_conf.show_total_period) { 3095 obj__printf(obj, "%11" PRIu64 " ", al->data[i].he.period); 3096 } else if (symbol_conf.show_nr_samples) { 3097 obj__printf(obj, "%6" PRIu64 " ", 3098 al->data[i].he.nr_samples); 3099 } else { 3100 obj__printf(obj, "%6.2f ", percent); 3101 } 3102 } 3103 } else { 3104 obj__set_percent_color(obj, 0, current_entry); 3105 3106 if (!show_title) 3107 obj__printf(obj, "%-*s", pcnt_width, " "); 3108 else { 3109 obj__printf(obj, "%-*s", pcnt_width, 3110 symbol_conf.show_total_period ? "Period" : 3111 symbol_conf.show_nr_samples ? "Samples" : "Percent"); 3112 } 3113 } 3114 3115 if (notes->have_cycles) { 3116 if (al->ipc) 3117 obj__printf(obj, "%*.2f ", ANNOTATION__IPC_WIDTH - 1, al->ipc); 3118 else if (!show_title) 3119 obj__printf(obj, "%*s", ANNOTATION__IPC_WIDTH, " "); 3120 else 3121 obj__printf(obj, "%*s ", ANNOTATION__IPC_WIDTH - 1, "IPC"); 3122 3123 if (!notes->options->show_minmax_cycle) { 3124 if (al->cycles) 3125 obj__printf(obj, "%*" PRIu64 " ", 3126 ANNOTATION__CYCLES_WIDTH - 1, al->cycles); 3127 else if (!show_title) 3128 obj__printf(obj, "%*s", 3129 ANNOTATION__CYCLES_WIDTH, " "); 3130 else 3131 obj__printf(obj, "%*s ", 3132 ANNOTATION__CYCLES_WIDTH - 1, 3133 "Cycle"); 3134 } else { 3135 if (al->cycles) { 3136 char str[32]; 3137 3138 scnprintf(str, sizeof(str), 3139 "%" PRIu64 "(%" PRIu64 "/%" PRIu64 ")", 3140 al->cycles, al->cycles_min, 3141 al->cycles_max); 3142 3143 obj__printf(obj, "%*s ", 3144 ANNOTATION__MINMAX_CYCLES_WIDTH - 1, 3145 str); 3146 } else if (!show_title) 3147 obj__printf(obj, "%*s", 3148 ANNOTATION__MINMAX_CYCLES_WIDTH, 3149 " "); 3150 else 3151 obj__printf(obj, "%*s ", 3152 ANNOTATION__MINMAX_CYCLES_WIDTH - 1, 3153 "Cycle(min/max)"); 3154 } 3155 3156 if (show_title && !*al->line) { 3157 ipc_coverage_string(bf, sizeof(bf), notes); 3158 obj__printf(obj, "%*s", ANNOTATION__AVG_IPC_WIDTH, bf); 3159 } 3160 } 3161 3162 obj__printf(obj, " "); 3163 3164 if (!*al->line) 3165 obj__printf(obj, "%-*s", width - pcnt_width - cycles_width, " "); 3166 else if (al->offset == -1) { 3167 if (al->line_nr && notes->options->show_linenr) 3168 printed = scnprintf(bf, sizeof(bf), "%-*d ", notes->widths.addr + 1, al->line_nr); 3169 else 3170 printed = scnprintf(bf, sizeof(bf), "%-*s ", notes->widths.addr, " "); 3171 obj__printf(obj, bf); 3172 obj__printf(obj, "%-*s", width - printed - pcnt_width - cycles_width + 1, al->line); 3173 } else { 3174 u64 addr = al->offset; 3175 int color = -1; 3176 3177 if (!notes->options->use_offset) 3178 addr += notes->start; 3179 3180 if (!notes->options->use_offset) { 3181 printed = scnprintf(bf, sizeof(bf), "%" PRIx64 ": ", addr); 3182 } else { 3183 if (al->jump_sources && 3184 notes->options->offset_level >= ANNOTATION__OFFSET_JUMP_TARGETS) { 3185 if (notes->options->show_nr_jumps) { 3186 int prev; 3187 printed = scnprintf(bf, sizeof(bf), "%*d ", 3188 notes->widths.jumps, 3189 al->jump_sources); 3190 prev = obj__set_jumps_percent_color(obj, al->jump_sources, 3191 current_entry); 3192 obj__printf(obj, bf); 3193 obj__set_color(obj, prev); 3194 } 3195 print_addr: 3196 printed = scnprintf(bf, sizeof(bf), "%*" PRIx64 ": ", 3197 notes->widths.target, addr); 3198 } else if (ins__is_call(&disasm_line(al)->ins) && 3199 notes->options->offset_level >= ANNOTATION__OFFSET_CALL) { 3200 goto print_addr; 3201 } else if (notes->options->offset_level == ANNOTATION__MAX_OFFSET_LEVEL) { 3202 goto print_addr; 3203 } else { 3204 printed = scnprintf(bf, sizeof(bf), "%-*s ", 3205 notes->widths.addr, " "); 3206 } 3207 } 3208 3209 if (change_color) 3210 color = obj__set_color(obj, HE_COLORSET_ADDR); 3211 obj__printf(obj, bf); 3212 if (change_color) 3213 obj__set_color(obj, color); 3214 3215 disasm_line__write(disasm_line(al), notes, obj, bf, sizeof(bf), obj__printf, obj__write_graph); 3216 3217 obj__printf(obj, "%-*s", width - pcnt_width - cycles_width - 3 - printed, bf); 3218 } 3219 3220 } 3221 3222 void annotation_line__write(struct annotation_line *al, struct annotation *notes, 3223 struct annotation_write_ops *wops, 3224 struct annotation_options *opts) 3225 { 3226 __annotation_line__write(al, notes, wops->first_line, wops->current_entry, 3227 wops->change_color, wops->width, wops->obj, 3228 opts->percent_type, 3229 wops->set_color, wops->set_percent_color, 3230 wops->set_jumps_percent_color, wops->printf, 3231 wops->write_graph); 3232 } 3233 3234 int symbol__annotate2(struct map_symbol *ms, struct evsel *evsel, 3235 struct annotation_options *options, struct arch **parch) 3236 { 3237 struct symbol *sym = ms->sym; 3238 struct annotation *notes = symbol__annotation(sym); 3239 size_t size = symbol__size(sym); 3240 int nr_pcnt = 1, err; 3241 3242 notes->offsets = zalloc(size * sizeof(struct annotation_line *)); 3243 if (notes->offsets == NULL) 3244 return ENOMEM; 3245 3246 if (evsel__is_group_event(evsel)) 3247 nr_pcnt = evsel->core.nr_members; 3248 3249 err = symbol__annotate(ms, evsel, options, parch); 3250 if (err) 3251 goto out_free_offsets; 3252 3253 notes->options = options; 3254 3255 symbol__calc_percent(sym, evsel); 3256 3257 annotation__set_offsets(notes, size); 3258 annotation__mark_jump_targets(notes, sym); 3259 annotation__compute_ipc(notes, size); 3260 annotation__init_column_widths(notes, sym); 3261 notes->nr_events = nr_pcnt; 3262 3263 annotation__update_column_widths(notes); 3264 sym->annotate2 = 1; 3265 3266 return 0; 3267 3268 out_free_offsets: 3269 zfree(¬es->offsets); 3270 return err; 3271 } 3272 3273 static int annotation__config(const char *var, const char *value, void *data) 3274 { 3275 struct annotation_options *opt = data; 3276 3277 if (!strstarts(var, "annotate.")) 3278 return 0; 3279 3280 if (!strcmp(var, "annotate.offset_level")) { 3281 perf_config_u8(&opt->offset_level, "offset_level", value); 3282 3283 if (opt->offset_level > ANNOTATION__MAX_OFFSET_LEVEL) 3284 opt->offset_level = ANNOTATION__MAX_OFFSET_LEVEL; 3285 else if (opt->offset_level < ANNOTATION__MIN_OFFSET_LEVEL) 3286 opt->offset_level = ANNOTATION__MIN_OFFSET_LEVEL; 3287 } else if (!strcmp(var, "annotate.hide_src_code")) { 3288 opt->hide_src_code = perf_config_bool("hide_src_code", value); 3289 } else if (!strcmp(var, "annotate.jump_arrows")) { 3290 opt->jump_arrows = perf_config_bool("jump_arrows", value); 3291 } else if (!strcmp(var, "annotate.show_linenr")) { 3292 opt->show_linenr = perf_config_bool("show_linenr", value); 3293 } else if (!strcmp(var, "annotate.show_nr_jumps")) { 3294 opt->show_nr_jumps = perf_config_bool("show_nr_jumps", value); 3295 } else if (!strcmp(var, "annotate.show_nr_samples")) { 3296 symbol_conf.show_nr_samples = perf_config_bool("show_nr_samples", 3297 value); 3298 } else if (!strcmp(var, "annotate.show_total_period")) { 3299 symbol_conf.show_total_period = perf_config_bool("show_total_period", 3300 value); 3301 } else if (!strcmp(var, "annotate.use_offset")) { 3302 opt->use_offset = perf_config_bool("use_offset", value); 3303 } else if (!strcmp(var, "annotate.disassembler_style")) { 3304 opt->disassembler_style = strdup(value); 3305 if (!opt->disassembler_style) { 3306 pr_err("Not enough memory for annotate.disassembler_style\n"); 3307 return -1; 3308 } 3309 } else if (!strcmp(var, "annotate.objdump")) { 3310 opt->objdump_path = strdup(value); 3311 if (!opt->objdump_path) { 3312 pr_err("Not enough memory for annotate.objdump\n"); 3313 return -1; 3314 } 3315 } else if (!strcmp(var, "annotate.addr2line")) { 3316 symbol_conf.addr2line_path = strdup(value); 3317 if (!symbol_conf.addr2line_path) { 3318 pr_err("Not enough memory for annotate.addr2line\n"); 3319 return -1; 3320 } 3321 } else if (!strcmp(var, "annotate.demangle")) { 3322 symbol_conf.demangle = perf_config_bool("demangle", value); 3323 } else if (!strcmp(var, "annotate.demangle_kernel")) { 3324 symbol_conf.demangle_kernel = perf_config_bool("demangle_kernel", value); 3325 } else { 3326 pr_debug("%s variable unknown, ignoring...", var); 3327 } 3328 3329 return 0; 3330 } 3331 3332 void annotation_options__init(struct annotation_options *opt) 3333 { 3334 memset(opt, 0, sizeof(*opt)); 3335 3336 /* Default values. */ 3337 opt->use_offset = true; 3338 opt->jump_arrows = true; 3339 opt->annotate_src = true; 3340 opt->offset_level = ANNOTATION__OFFSET_JUMP_TARGETS; 3341 opt->percent_type = PERCENT_PERIOD_LOCAL; 3342 } 3343 3344 3345 void annotation_options__exit(struct annotation_options *opt) 3346 { 3347 zfree(&opt->disassembler_style); 3348 zfree(&opt->objdump_path); 3349 } 3350 3351 void annotation_config__init(struct annotation_options *opt) 3352 { 3353 perf_config(annotation__config, opt); 3354 } 3355 3356 static unsigned int parse_percent_type(char *str1, char *str2) 3357 { 3358 unsigned int type = (unsigned int) -1; 3359 3360 if (!strcmp("period", str1)) { 3361 if (!strcmp("local", str2)) 3362 type = PERCENT_PERIOD_LOCAL; 3363 else if (!strcmp("global", str2)) 3364 type = PERCENT_PERIOD_GLOBAL; 3365 } 3366 3367 if (!strcmp("hits", str1)) { 3368 if (!strcmp("local", str2)) 3369 type = PERCENT_HITS_LOCAL; 3370 else if (!strcmp("global", str2)) 3371 type = PERCENT_HITS_GLOBAL; 3372 } 3373 3374 return type; 3375 } 3376 3377 int annotate_parse_percent_type(const struct option *opt, const char *_str, 3378 int unset __maybe_unused) 3379 { 3380 struct annotation_options *opts = opt->value; 3381 unsigned int type; 3382 char *str1, *str2; 3383 int err = -1; 3384 3385 str1 = strdup(_str); 3386 if (!str1) 3387 return -ENOMEM; 3388 3389 str2 = strchr(str1, '-'); 3390 if (!str2) 3391 goto out; 3392 3393 *str2++ = 0; 3394 3395 type = parse_percent_type(str1, str2); 3396 if (type == (unsigned int) -1) 3397 type = parse_percent_type(str2, str1); 3398 if (type != (unsigned int) -1) { 3399 opts->percent_type = type; 3400 err = 0; 3401 } 3402 3403 out: 3404 free(str1); 3405 return err; 3406 } 3407 3408 int annotate_check_args(struct annotation_options *args) 3409 { 3410 if (args->prefix_strip && !args->prefix) { 3411 pr_err("--prefix-strip requires --prefix\n"); 3412 return -1; 3413 } 3414 return 0; 3415 } 3416