FILE
disasm.c
File preview with syntax highlighting.
linux-6.19.14/tools/perf/util/disasm.c
1// SPDX-License-Identifier: GPL-2.0-only 2#include <ctype.h> 3#include <errno.h> 4#include <fcntl.h> 5#include <inttypes.h> 6#include <libgen.h> 7#include <regex.h> 8#include <stdlib.h> 9#include <unistd.h> 10 11#include <linux/string.h> 12#include <subcmd/run-command.h> 13 14#include "annotate.h" 15#include "annotate-data.h" 16#include "build-id.h" 17#include "capstone.h" 18#include "debug.h" 19#include "disasm.h" 20#include "dso.h" 21#include "dwarf-regs.h" 22#include "env.h" 23#include "evsel.h" 24#include "libbfd.h" 25#include "llvm.h" 26#include "map.h" 27#include "maps.h" 28#include "namespaces.h" 29#include "srcline.h" 30#include "symbol.h" 31#include "util.h" 32 33static regex_t file_lineno; 34 35/* These can be referred from the arch-dependent code */ 36static struct ins_ops call_ops; 37static struct ins_ops dec_ops; 38static struct ins_ops jump_ops; 39static struct ins_ops mov_ops; 40static struct ins_ops nop_ops; 41static struct ins_ops lock_ops; 42static struct ins_ops ret_ops; 43static struct ins_ops load_store_ops; 44static struct ins_ops arithmetic_ops; 45 46static int jump__scnprintf(struct ins *ins, char *bf, size_t size, 47 struct ins_operands *ops, int max_ins_name); 48static int call__scnprintf(struct ins *ins, char *bf, size_t size, 49 struct ins_operands *ops, int max_ins_name); 50 51static void ins__sort(struct arch *arch); 52static int disasm_line__parse(char *line, const char **namep, char **rawp); 53static int disasm_line__parse_powerpc(struct disasm_line *dl, struct annotate_args *args); 54 55static __attribute__((constructor)) void symbol__init_regexpr(void) 56{ 57 regcomp(&file_lineno, "^/[^:]+:([0-9]+)", REG_EXTENDED); 58} 59 60static int arch__grow_instructions(struct arch *arch) 61{ 62 struct ins *new_instructions; 63 size_t new_nr_allocated; 64 65 if (arch->nr_instructions_allocated == 0 && arch->instructions) 66 goto grow_from_non_allocated_table; 67 68 new_nr_allocated = arch->nr_instructions_allocated + 128; 69 new_instructions = realloc(arch->instructions, new_nr_allocated * sizeof(struct ins)); 70 if (new_instructions == NULL) 71 return -1; 72 73out_update_instructions: 74 arch->instructions = new_instructions; 75 arch->nr_instructions_allocated = new_nr_allocated; 76 return 0; 77 78grow_from_non_allocated_table: 79 new_nr_allocated = arch->nr_instructions + 128; 80 new_instructions = calloc(new_nr_allocated, sizeof(struct ins)); 81 if (new_instructions == NULL) 82 return -1; 83 84 memcpy(new_instructions, arch->instructions, arch->nr_instructions * sizeof(struct ins)); 85 goto out_update_instructions; 86} 87 88static int arch__associate_ins_ops(struct arch* arch, const char *name, struct ins_ops *ops) 89{ 90 struct ins *ins; 91 92 if (arch->nr_instructions == arch->nr_instructions_allocated && 93 arch__grow_instructions(arch)) 94 return -1; 95 96 ins = &arch->instructions[arch->nr_instructions]; 97 ins->name = strdup(name); 98 if (!ins->name) 99 return -1; 100 101 ins->ops = ops; 102 arch->nr_instructions++; 103 104 ins__sort(arch); 105 return 0; 106} 107 108#include "arch/arc/annotate/instructions.c" 109#include "arch/arm/annotate/instructions.c" 110#include "arch/arm64/annotate/instructions.c" 111#include "arch/csky/annotate/instructions.c" 112#include "arch/loongarch/annotate/instructions.c" 113#include "arch/mips/annotate/instructions.c" 114#include "arch/x86/annotate/instructions.c" 115#include "arch/powerpc/annotate/instructions.c" 116#include "arch/riscv64/annotate/instructions.c" 117#include "arch/s390/annotate/instructions.c" 118#include "arch/sparc/annotate/instructions.c" 119 120static struct arch architectures[] = { 121 { 122 .name = "arc", 123 .init = arc__annotate_init, 124 }, 125 { 126 .name = "arm", 127 .init = arm__annotate_init, 128 }, 129 { 130 .name = "arm64", 131 .init = arm64__annotate_init, 132 }, 133 { 134 .name = "csky", 135 .init = csky__annotate_init, 136 }, 137 { 138 .name = "mips", 139 .init = mips__annotate_init, 140 .objdump = { 141 .comment_char = '#', 142 }, 143 }, 144 { 145 .name = "x86", 146 .init = x86__annotate_init, 147 .instructions = x86__instructions, 148 .nr_instructions = ARRAY_SIZE(x86__instructions), 149 .insn_suffix = "bwlq", 150 .objdump = { 151 .comment_char = '#', 152 .register_char = '%', 153 .memory_ref_char = '(', 154 .imm_char = '$', 155 }, 156#ifdef HAVE_LIBDW_SUPPORT 157 .update_insn_state = update_insn_state_x86, 158#endif 159 }, 160 { 161 .name = "powerpc", 162 .init = powerpc__annotate_init, 163#ifdef HAVE_LIBDW_SUPPORT 164 .update_insn_state = update_insn_state_powerpc, 165#endif 166 }, 167 { 168 .name = "riscv64", 169 .init = riscv64__annotate_init, 170 }, 171 { 172 .name = "s390", 173 .init = s390__annotate_init, 174 .objdump = { 175 .comment_char = '#', 176 }, 177 }, 178 { 179 .name = "sparc", 180 .init = sparc__annotate_init, 181 .objdump = { 182 .comment_char = '#', 183 }, 184 }, 185 { 186 .name = "loongarch", 187 .init = loongarch__annotate_init, 188 .objdump = { 189 .comment_char = '#', 190 }, 191 }, 192}; 193 194static int arch__key_cmp(const void *name, const void *archp) 195{ 196 const struct arch *arch = archp; 197 198 return strcmp(name, arch->name); 199} 200 201static int arch__cmp(const void *a, const void *b) 202{ 203 const struct arch *aa = a; 204 const struct arch *ab = b; 205 206 return strcmp(aa->name, ab->name); 207} 208 209static void arch__sort(void) 210{ 211 const int nmemb = ARRAY_SIZE(architectures); 212 213 qsort(architectures, nmemb, sizeof(struct arch), arch__cmp); 214} 215 216struct arch *arch__find(const char *name) 217{ 218 const int nmemb = ARRAY_SIZE(architectures); 219 static bool sorted; 220 221 if (!sorted) { 222 arch__sort(); 223 sorted = true; 224 } 225 226 return bsearch(name, architectures, nmemb, sizeof(struct arch), arch__key_cmp); 227} 228 229bool arch__is(struct arch *arch, const char *name) 230{ 231 return !strcmp(arch->name, name); 232} 233 234static void ins_ops__delete(struct ins_operands *ops) 235{ 236 if (ops == NULL) 237 return; 238 zfree(&ops->source.raw); 239 zfree(&ops->source.name); 240 zfree(&ops->target.raw); 241 zfree(&ops->target.name); 242} 243 244static int ins__raw_scnprintf(struct ins *ins, char *bf, size_t size, 245 struct ins_operands *ops, int max_ins_name) 246{ 247 return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name, ops->raw); 248} 249 250static int ins__scnprintf(struct ins *ins, char *bf, size_t size, 251 struct ins_operands *ops, int max_ins_name) 252{ 253 if (ins->ops->scnprintf) 254 return ins->ops->scnprintf(ins, bf, size, ops, max_ins_name); 255 256 return ins__raw_scnprintf(ins, bf, size, ops, max_ins_name); 257} 258 259bool ins__is_fused(struct arch *arch, const char *ins1, const char *ins2) 260{ 261 if (!arch || !arch->ins_is_fused) 262 return false; 263 264 return arch->ins_is_fused(arch, ins1, ins2); 265} 266 267static int call__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms, 268 struct disasm_line *dl __maybe_unused) 269{ 270 char *endptr, *tok, *name; 271 struct map *map = ms->map; 272 struct addr_map_symbol target; 273 274 ops->target.addr = strtoull(ops->raw, &endptr, 16); 275 276 name = strchr(endptr, '<'); 277 if (name == NULL) 278 goto indirect_call; 279 280 name++; 281 282 if (arch->objdump.skip_functions_char && 283 strchr(name, arch->objdump.skip_functions_char)) 284 return -1; 285 286 tok = strchr(name, '>'); 287 if (tok == NULL) 288 return -1; 289 290 *tok = '\0'; 291 ops->target.name = strdup(name); 292 *tok = '>'; 293 294 if (ops->target.name == NULL) 295 return -1; 296find_target: 297 target = (struct addr_map_symbol) { 298 .ms = { .map = map__get(map), }, 299 .addr = map__objdump_2mem(map, ops->target.addr), 300 }; 301 302 if (maps__find_ams(ms->maps, &target) == 0 && 303 map__rip_2objdump(target.ms.map, map__map_ip(target.ms.map, target.addr)) == ops->target.addr) 304 ops->target.sym = target.ms.sym; 305 306 addr_map_symbol__exit(&target); 307 return 0; 308 309indirect_call: 310 tok = strchr(endptr, '*'); 311 if (tok != NULL) { 312 endptr++; 313 314 /* Indirect call can use a non-rip register and offset: callq *0x8(%rbx). 315 * Do not parse such instruction. */ 316 if (strstr(endptr, "(%r") == NULL) 317 ops->target.addr = strtoull(endptr, NULL, 16); 318 } 319 goto find_target; 320} 321 322static int call__scnprintf(struct ins *ins, char *bf, size_t size, 323 struct ins_operands *ops, int max_ins_name) 324{ 325 if (ops->target.sym) 326 return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name, ops->target.sym->name); 327 328 if (ops->target.addr == 0) 329 return ins__raw_scnprintf(ins, bf, size, ops, max_ins_name); 330 331 if (ops->target.name) 332 return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name, ops->target.name); 333 334 return scnprintf(bf, size, "%-*s *%" PRIx64, max_ins_name, ins->name, ops->target.addr); 335} 336 337static struct ins_ops call_ops = { 338 .parse = call__parse, 339 .scnprintf = call__scnprintf, 340}; 341 342bool ins__is_call(const struct ins *ins) 343{ 344 return ins->ops == &call_ops || ins->ops == &s390_call_ops || ins->ops == &loongarch_call_ops; 345} 346 347/* 348 * Prevents from matching commas in the comment section, e.g.: 349 * ffff200008446e70: b.cs ffff2000084470f4 <generic_exec_single+0x314> // b.hs, b.nlast 350 * 351 * and skip comma as part of function arguments, e.g.: 352 * 1d8b4ac <linemap_lookup(line_maps const*, unsigned int)+0xcc> 353 */ 354static inline const char *validate_comma(const char *c, struct ins_operands *ops) 355{ 356 if (ops->jump.raw_comment && c > ops->jump.raw_comment) 357 return NULL; 358 359 if (ops->jump.raw_func_start && c > ops->jump.raw_func_start) 360 return NULL; 361 362 return c; 363} 364 365static int jump__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms, 366 struct disasm_line *dl __maybe_unused) 367{ 368 struct map *map = ms->map; 369 struct symbol *sym = ms->sym; 370 struct addr_map_symbol target = { 371 .ms = { .map = map__get(map), }, 372 }; 373 const char *c = strchr(ops->raw, ','); 374 u64 start, end; 375 376 ops->jump.raw_comment = strchr(ops->raw, arch->objdump.comment_char); 377 ops->jump.raw_func_start = strchr(ops->raw, '<'); 378 379 c = validate_comma(c, ops); 380 381 /* 382 * Examples of lines to parse for the _cpp_lex_token@@Base 383 * function: 384 * 385 * 1159e6c: jne 115aa32 <_cpp_lex_token@@Base+0xf92> 386 * 1159e8b: jne c469be <cpp_named_operator2name@@Base+0xa72> 387 * 388 * The first is a jump to an offset inside the same function, 389 * the second is to another function, i.e. that 0xa72 is an 390 * offset in the cpp_named_operator2name@@base function. 391 */ 392 /* 393 * skip over possible up to 2 operands to get to address, e.g.: 394 * tbnz w0, #26, ffff0000083cd190 <security_file_permission+0xd0> 395 */ 396 if (c != NULL) { 397 c++; 398 ops->target.addr = strtoull(c, NULL, 16); 399 if (!ops->target.addr) { 400 c = strchr(c, ','); 401 c = validate_comma(c, ops); 402 if (c != NULL) { 403 c++; 404 ops->target.addr = strtoull(c, NULL, 16); 405 } 406 } 407 } else { 408 ops->target.addr = strtoull(ops->raw, NULL, 16); 409 } 410 411 target.addr = map__objdump_2mem(map, ops->target.addr); 412 start = map__unmap_ip(map, sym->start); 413 end = map__unmap_ip(map, sym->end); 414 415 ops->target.outside = target.addr < start || target.addr >= end; 416 417 /* 418 * FIXME: things like this in _cpp_lex_token (gcc's cc1 program): 419 420 cpp_named_operator2name@@Base+0xa72 421 422 * Point to a place that is after the cpp_named_operator2name 423 * boundaries, i.e. in the ELF symbol table for cc1 424 * cpp_named_operator2name is marked as being 32-bytes long, but it in 425 * fact is much larger than that, so we seem to need a symbols__find() 426 * routine that looks for >= current->start and < next_symbol->start, 427 * possibly just for C++ objects? 428 * 429 * For now lets just make some progress by marking jumps to outside the 430 * current function as call like. 431 * 432 * Actual navigation will come next, with further understanding of how 433 * the symbol searching and disassembly should be done. 434 */ 435 if (maps__find_ams(ms->maps, &target) == 0 && 436 map__rip_2objdump(target.ms.map, map__map_ip(target.ms.map, target.addr)) == ops->target.addr) 437 ops->target.sym = target.ms.sym; 438 439 if (!ops->target.outside) { 440 ops->target.offset = target.addr - start; 441 ops->target.offset_avail = true; 442 } else { 443 ops->target.offset_avail = false; 444 } 445 addr_map_symbol__exit(&target); 446 return 0; 447} 448 449static int jump__scnprintf(struct ins *ins, char *bf, size_t size, 450 struct ins_operands *ops, int max_ins_name) 451{ 452 const char *c; 453 454 if (!ops->target.addr || ops->target.offset < 0) 455 return ins__raw_scnprintf(ins, bf, size, ops, max_ins_name); 456 457 if (ops->target.outside && ops->target.sym != NULL) 458 return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name, ops->target.sym->name); 459 460 c = strchr(ops->raw, ','); 461 c = validate_comma(c, ops); 462 463 if (c != NULL) { 464 const char *c2 = strchr(c + 1, ','); 465 466 c2 = validate_comma(c2, ops); 467 /* check for 3-op insn */ 468 if (c2 != NULL) 469 c = c2; 470 c++; 471 472 /* mirror arch objdump's space-after-comma style */ 473 if (*c == ' ') 474 c++; 475 } 476 477 return scnprintf(bf, size, "%-*s %.*s%" PRIx64, max_ins_name, 478 ins->name, c ? c - ops->raw : 0, ops->raw, 479 ops->target.offset); 480} 481 482static void jump__delete(struct ins_operands *ops __maybe_unused) 483{ 484 /* 485 * The ops->jump.raw_comment and ops->jump.raw_func_start belong to the 486 * raw string, don't free them. 487 */ 488} 489 490static struct ins_ops jump_ops = { 491 .free = jump__delete, 492 .parse = jump__parse, 493 .scnprintf = jump__scnprintf, 494}; 495 496bool ins__is_jump(const struct ins *ins) 497{ 498 return ins->ops == &jump_ops || ins->ops == &loongarch_jump_ops; 499} 500 501static int comment__symbol(char *raw, char *comment, u64 *addrp, char **namep) 502{ 503 char *endptr, *name, *t; 504 505 if (strstr(raw, "(%rip)") == NULL) 506 return 0; 507 508 *addrp = strtoull(comment, &endptr, 16); 509 if (endptr == comment) 510 return 0; 511 name = strchr(endptr, '<'); 512 if (name == NULL) 513 return -1; 514 515 name++; 516 517 t = strchr(name, '>'); 518 if (t == NULL) 519 return 0; 520 521 *t = '\0'; 522 *namep = strdup(name); 523 *t = '>'; 524 525 return 0; 526} 527 528static int lock__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms, 529 struct disasm_line *dl __maybe_unused) 530{ 531 ops->locked.ops = zalloc(sizeof(*ops->locked.ops)); 532 if (ops->locked.ops == NULL) 533 return 0; 534 535 if (disasm_line__parse(ops->raw, &ops->locked.ins.name, &ops->locked.ops->raw) < 0) 536 goto out_free_ops; 537 538 ops->locked.ins.ops = ins__find(arch, ops->locked.ins.name, 0); 539 540 if (ops->locked.ins.ops == NULL) 541 goto out_free_ops; 542 543 if (ops->locked.ins.ops->parse && 544 ops->locked.ins.ops->parse(arch, ops->locked.ops, ms, NULL) < 0) 545 goto out_free_ops; 546 547 return 0; 548 549out_free_ops: 550 zfree(&ops->locked.ops); 551 return 0; 552} 553 554static int lock__scnprintf(struct ins *ins, char *bf, size_t size, 555 struct ins_operands *ops, int max_ins_name) 556{ 557 int printed; 558 559 if (ops->locked.ins.ops == NULL) 560 return ins__raw_scnprintf(ins, bf, size, ops, max_ins_name); 561 562 printed = scnprintf(bf, size, "%-*s ", max_ins_name, ins->name); 563 return printed + ins__scnprintf(&ops->locked.ins, bf + printed, 564 size - printed, ops->locked.ops, max_ins_name); 565} 566 567static void lock__delete(struct ins_operands *ops) 568{ 569 struct ins *ins = &ops->locked.ins; 570 571 if (ins->ops && ins->ops->free) 572 ins->ops->free(ops->locked.ops); 573 else 574 ins_ops__delete(ops->locked.ops); 575 576 zfree(&ops->locked.ops); 577 zfree(&ops->locked.ins.name); 578 zfree(&ops->target.raw); 579 zfree(&ops->target.name); 580} 581 582static struct ins_ops lock_ops = { 583 .free = lock__delete, 584 .parse = lock__parse, 585 .scnprintf = lock__scnprintf, 586}; 587 588/* 589 * Check if the operand has more than one registers like x86 SIB addressing: 590 * 0x1234(%rax, %rbx, 8) 591 * 592 * But it doesn't care segment selectors like %gs:0x5678(%rcx), so just check 593 * the input string after 'memory_ref_char' if exists. 594 */ 595static bool check_multi_regs(struct arch *arch, const char *op) 596{ 597 int count = 0; 598 599 if (arch->objdump.register_char == 0) 600 return false; 601 602 if (arch->objdump.memory_ref_char) { 603 op = strchr(op, arch->objdump.memory_ref_char); 604 if (op == NULL) 605 return false; 606 } 607 608 while ((op = strchr(op, arch->objdump.register_char)) != NULL) { 609 count++; 610 op++; 611 } 612 613 return count > 1; 614} 615 616static int mov__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms __maybe_unused, 617 struct disasm_line *dl __maybe_unused) 618{ 619 char *s = strchr(ops->raw, ','), *target, *comment, prev; 620 621 if (s == NULL) 622 return -1; 623 624 *s = '\0'; 625 626 /* 627 * x86 SIB addressing has something like 0x8(%rax, %rcx, 1) 628 * then it needs to have the closing parenthesis. 629 */ 630 if (strchr(ops->raw, '(')) { 631 *s = ','; 632 s = strchr(ops->raw, ')'); 633 if (s == NULL || s[1] != ',') 634 return -1; 635 *++s = '\0'; 636 } 637 638 ops->source.raw = strdup(ops->raw); 639 *s = ','; 640 641 if (ops->source.raw == NULL) 642 return -1; 643 644 ops->source.multi_regs = check_multi_regs(arch, ops->source.raw); 645 646 target = skip_spaces(++s); 647 comment = strchr(s, arch->objdump.comment_char); 648 649 if (comment != NULL) 650 s = comment - 1; 651 else 652 s = strchr(s, '\0') - 1; 653 654 while (s > target && isspace(s[0])) 655 --s; 656 s++; 657 prev = *s; 658 *s = '\0'; 659 660 ops->target.raw = strdup(target); 661 *s = prev; 662 663 if (ops->target.raw == NULL) 664 goto out_free_source; 665 666 ops->target.multi_regs = check_multi_regs(arch, ops->target.raw); 667 668 if (comment == NULL) 669 return 0; 670 671 comment = skip_spaces(comment); 672 comment__symbol(ops->source.raw, comment + 1, &ops->source.addr, &ops->source.name); 673 comment__symbol(ops->target.raw, comment + 1, &ops->target.addr, &ops->target.name); 674 675 return 0; 676 677out_free_source: 678 zfree(&ops->source.raw); 679 return -1; 680} 681 682static int mov__scnprintf(struct ins *ins, char *bf, size_t size, 683 struct ins_operands *ops, int max_ins_name) 684{ 685 return scnprintf(bf, size, "%-*s %s,%s", max_ins_name, ins->name, 686 ops->source.name ?: ops->source.raw, 687 ops->target.name ?: ops->target.raw); 688} 689 690static struct ins_ops mov_ops = { 691 .parse = mov__parse, 692 .scnprintf = mov__scnprintf, 693}; 694 695#define PPC_22_30(R) (((R) >> 1) & 0x1ff) 696#define MINUS_EXT_XO_FORM 234 697#define SUB_EXT_XO_FORM 232 698#define ADD_ZERO_EXT_XO_FORM 202 699#define SUB_ZERO_EXT_XO_FORM 200 700 701static int arithmetic__scnprintf(struct ins *ins, char *bf, size_t size, 702 struct ins_operands *ops, int max_ins_name) 703{ 704 return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name, 705 ops->raw); 706} 707 708/* 709 * Sets the fields: multi_regs and "mem_ref". 710 * "mem_ref" is set for ops->source which is later used to 711 * fill the objdump->memory_ref-char field. This ops is currently 712 * used by powerpc and since binary instruction code is used to 713 * extract opcode, regs and offset, no other parsing is needed here. 714 * 715 * Dont set multi regs for 4 cases since it has only one operand 716 * for source: 717 * - Add to Minus One Extended XO-form ( Ex: addme, addmeo ) 718 * - Subtract From Minus One Extended XO-form ( Ex: subfme ) 719 * - Add to Zero Extended XO-form ( Ex: addze, addzeo ) 720 * - Subtract From Zero Extended XO-form ( Ex: subfze ) 721 */ 722static int arithmetic__parse(struct arch *arch __maybe_unused, struct ins_operands *ops, 723 struct map_symbol *ms __maybe_unused, struct disasm_line *dl) 724{ 725 int opcode = PPC_OP(dl->raw.raw_insn); 726 727 ops->source.mem_ref = false; 728 if (opcode == 31) { 729 if ((opcode != MINUS_EXT_XO_FORM) && (opcode != SUB_EXT_XO_FORM) \ 730 && (opcode != ADD_ZERO_EXT_XO_FORM) && (opcode != SUB_ZERO_EXT_XO_FORM)) 731 ops->source.multi_regs = true; 732 } 733 734 ops->target.mem_ref = false; 735 ops->target.multi_regs = false; 736 737 return 0; 738} 739 740static struct ins_ops arithmetic_ops = { 741 .parse = arithmetic__parse, 742 .scnprintf = arithmetic__scnprintf, 743}; 744 745static int load_store__scnprintf(struct ins *ins, char *bf, size_t size, 746 struct ins_operands *ops, int max_ins_name) 747{ 748 return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name, 749 ops->raw); 750} 751 752/* 753 * Sets the fields: multi_regs and "mem_ref". 754 * "mem_ref" is set for ops->source which is later used to 755 * fill the objdump->memory_ref-char field. This ops is currently 756 * used by powerpc and since binary instruction code is used to 757 * extract opcode, regs and offset, no other parsing is needed here 758 */ 759static int load_store__parse(struct arch *arch __maybe_unused, struct ins_operands *ops, 760 struct map_symbol *ms __maybe_unused, struct disasm_line *dl __maybe_unused) 761{ 762 ops->source.mem_ref = true; 763 ops->source.multi_regs = false; 764 /* opcode 31 is of X form */ 765 if (PPC_OP(dl->raw.raw_insn) == 31) 766 ops->source.multi_regs = true; 767 768 ops->target.mem_ref = false; 769 ops->target.multi_regs = false; 770 771 return 0; 772} 773 774static struct ins_ops load_store_ops = { 775 .parse = load_store__parse, 776 .scnprintf = load_store__scnprintf, 777}; 778 779static int dec__parse(struct arch *arch __maybe_unused, struct ins_operands *ops, struct map_symbol *ms __maybe_unused, 780 struct disasm_line *dl __maybe_unused) 781{ 782 char *target, *comment, *s, prev; 783 784 target = s = ops->raw; 785 786 while (s[0] != '\0' && !isspace(s[0])) 787 ++s; 788 prev = *s; 789 *s = '\0'; 790 791 ops->target.raw = strdup(target); 792 *s = prev; 793 794 if (ops->target.raw == NULL) 795 return -1; 796 797 comment = strchr(s, arch->objdump.comment_char); 798 if (comment == NULL) 799 return 0; 800 801 comment = skip_spaces(comment); 802 comment__symbol(ops->target.raw, comment + 1, &ops->target.addr, &ops->target.name); 803 804 return 0; 805} 806 807static int dec__scnprintf(struct ins *ins, char *bf, size_t size, 808 struct ins_operands *ops, int max_ins_name) 809{ 810 return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name, 811 ops->target.name ?: ops->target.raw); 812} 813 814static struct ins_ops dec_ops = { 815 .parse = dec__parse, 816 .scnprintf = dec__scnprintf, 817}; 818 819static int nop__scnprintf(struct ins *ins __maybe_unused, char *bf, size_t size, 820 struct ins_operands *ops __maybe_unused, int max_ins_name) 821{ 822 return scnprintf(bf, size, "%-*s", max_ins_name, "nop"); 823} 824 825static struct ins_ops nop_ops = { 826 .scnprintf = nop__scnprintf, 827}; 828 829static struct ins_ops ret_ops = { 830 .scnprintf = ins__raw_scnprintf, 831}; 832 833static bool ins__is_nop(const struct ins *ins) 834{ 835 return ins->ops == &nop_ops; 836} 837 838bool ins__is_ret(const struct ins *ins) 839{ 840 return ins->ops == &ret_ops; 841} 842 843bool ins__is_lock(const struct ins *ins) 844{ 845 return ins->ops == &lock_ops; 846} 847 848static int ins__key_cmp(const void *name, const void *insp) 849{ 850 const struct ins *ins = insp; 851 852 return strcmp(name, ins->name); 853} 854 855static int ins__cmp(const void *a, const void *b) 856{ 857 const struct ins *ia = a; 858 const struct ins *ib = b; 859 860 return strcmp(ia->name, ib->name); 861} 862 863static void ins__sort(struct arch *arch) 864{ 865 const int nmemb = arch->nr_instructions; 866 867 qsort(arch->instructions, nmemb, sizeof(struct ins), ins__cmp); 868} 869 870static struct ins_ops *__ins__find(struct arch *arch, const char *name, struct disasm_line *dl) 871{ 872 struct ins *ins; 873 const int nmemb = arch->nr_instructions; 874 875 if (arch__is(arch, "powerpc")) { 876 /* 877 * For powerpc, identify the instruction ops 878 * from the opcode using raw_insn. 879 */ 880 struct ins_ops *ops; 881 882 ops = check_ppc_insn(dl); 883 if (ops) 884 return ops; 885 } 886 887 if (!arch->sorted_instructions) { 888 ins__sort(arch); 889 arch->sorted_instructions = true; 890 } 891 892 ins = bsearch(name, arch->instructions, nmemb, sizeof(struct ins), ins__key_cmp); 893 if (ins) 894 return ins->ops; 895 896 if (arch->insn_suffix) { 897 char tmp[32]; 898 char suffix; 899 size_t len = strlen(name); 900 901 if (len == 0 || len >= sizeof(tmp)) 902 return NULL; 903 904 suffix = name[len - 1]; 905 if (strchr(arch->insn_suffix, suffix) == NULL) 906 return NULL; 907 908 strcpy(tmp, name); 909 tmp[len - 1] = '\0'; /* remove the suffix and check again */ 910 911 ins = bsearch(tmp, arch->instructions, nmemb, sizeof(struct ins), ins__key_cmp); 912 } 913 return ins ? ins->ops : NULL; 914} 915 916struct ins_ops *ins__find(struct arch *arch, const char *name, struct disasm_line *dl) 917{ 918 struct ins_ops *ops = __ins__find(arch, name, dl); 919 920 if (!ops && arch->associate_instruction_ops) 921 ops = arch->associate_instruction_ops(arch, name); 922 923 return ops; 924} 925 926static void disasm_line__init_ins(struct disasm_line *dl, struct arch *arch, struct map_symbol *ms) 927{ 928 dl->ins.ops = ins__find(arch, dl->ins.name, dl); 929 930 if (!dl->ins.ops) 931 return; 932 933 if (dl->ins.ops->parse && dl->ins.ops->parse(arch, &dl->ops, ms, dl) < 0) 934 dl->ins.ops = NULL; 935} 936 937static int disasm_line__parse(char *line, const char **namep, char **rawp) 938{ 939 char tmp, *name = skip_spaces(line); 940 941 if (name[0] == '\0') 942 return -1; 943 944 *rawp = name + 1; 945 946 while ((*rawp)[0] != '\0' && !isspace((*rawp)[0])) 947 ++*rawp; 948 949 tmp = (*rawp)[0]; 950 (*rawp)[0] = '\0'; 951 *namep = strdup(name); 952 953 if (*namep == NULL) 954 goto out; 955 956 (*rawp)[0] = tmp; 957 *rawp = strim(*rawp); 958 959 return 0; 960 961out: 962 return -1; 963} 964 965/* 966 * Parses the result captured from symbol__disassemble_* 967 * Example, line read from DSO file in powerpc: 968 * line: 38 01 81 e8 969 * opcode: fetched from arch specific get_opcode_insn 970 * rawp_insn: e8810138 971 * 972 * rawp_insn is used later to extract the reg/offset fields 973 */ 974#define PPC_OP(op) (((op) >> 26) & 0x3F) 975#define RAW_BYTES 11 976 977static int disasm_line__parse_powerpc(struct disasm_line *dl, struct annotate_args *args) 978{ 979 char *line = dl->al.line; 980 const char **namep = &dl->ins.name; 981 char **rawp = &dl->ops.raw; 982 char *tmp_raw_insn, *name_raw_insn = skip_spaces(line); 983 char *name = skip_spaces(name_raw_insn + RAW_BYTES); 984 int disasm = 0; 985 int ret = 0; 986 987 if (args->options->disassembler_used) 988 disasm = 1; 989 990 if (name_raw_insn[0] == '\0') 991 return -1; 992 993 if (disasm) 994 ret = disasm_line__parse(name, namep, rawp); 995 else 996 *namep = ""; 997 998 tmp_raw_insn = strndup(name_raw_insn, 11); 999 if (tmp_raw_insn == NULL) 1000 return -1; 1001 1002 remove_spaces(tmp_raw_insn); 1003 1004 sscanf(tmp_raw_insn, "%x", &dl->raw.raw_insn); 1005 if (disasm) 1006 dl->raw.raw_insn = be32_to_cpu(dl->raw.raw_insn); 1007 1008 return ret; 1009} 1010 1011static void annotation_line__init(struct annotation_line *al, 1012 struct annotate_args *args, 1013 int nr) 1014{ 1015 al->offset = args->offset; 1016 al->line = strdup(args->line); 1017 al->line_nr = args->line_nr; 1018 al->fileloc = args->fileloc; 1019 al->data_nr = nr; 1020} 1021 1022static void annotation_line__exit(struct annotation_line *al) 1023{ 1024 zfree_srcline(&al->path); 1025 zfree(&al->line); 1026 zfree(&al->cycles); 1027 zfree(&al->br_cntr); 1028} 1029 1030static size_t disasm_line_size(int nr) 1031{ 1032 struct annotation_line *al; 1033 1034 return (sizeof(struct disasm_line) + (sizeof(al->data[0]) * nr)); 1035} 1036 1037/* 1038 * Allocating the disasm annotation line data with 1039 * following structure: 1040 * 1041 * ------------------------------------------- 1042 * struct disasm_line | struct annotation_line 1043 * ------------------------------------------- 1044 * 1045 * We have 'struct annotation_line' member as last member 1046 * of 'struct disasm_line' to have an easy access. 1047 */ 1048struct disasm_line *disasm_line__new(struct annotate_args *args) 1049{ 1050 struct disasm_line *dl = NULL; 1051 struct annotation *notes = symbol__annotation(args->ms->sym); 1052 int nr = notes->src->nr_events; 1053 1054 dl = zalloc(disasm_line_size(nr)); 1055 if (!dl) 1056 return NULL; 1057 1058 annotation_line__init(&dl->al, args, nr); 1059 if (dl->al.line == NULL) 1060 goto out_delete; 1061 1062 if (args->offset != -1) { 1063 if (arch__is(args->arch, "powerpc")) { 1064 if (disasm_line__parse_powerpc(dl, args) < 0) 1065 goto out_free_line; 1066 } else if (disasm_line__parse(dl->al.line, &dl->ins.name, &dl->ops.raw) < 0) 1067 goto out_free_line; 1068 1069 disasm_line__init_ins(dl, args->arch, args->ms); 1070 } 1071 1072 return dl; 1073 1074out_free_line: 1075 zfree(&dl->al.line); 1076out_delete: 1077 free(dl); 1078 return NULL; 1079} 1080 1081void disasm_line__free(struct disasm_line *dl) 1082{ 1083 if (dl->ins.ops && dl->ins.ops->free) 1084 dl->ins.ops->free(&dl->ops); 1085 else 1086 ins_ops__delete(&dl->ops); 1087 zfree(&dl->ins.name); 1088 annotation_line__exit(&dl->al); 1089 free(dl); 1090} 1091 1092int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size, bool raw, int max_ins_name) 1093{ 1094 if (raw || !dl->ins.ops) 1095 return scnprintf(bf, size, "%-*s %s", max_ins_name, dl->ins.name, dl->ops.raw); 1096 1097 return ins__scnprintf(&dl->ins, bf, size, &dl->ops, max_ins_name); 1098} 1099 1100/* 1101 * symbol__parse_objdump_line() parses objdump output (with -d --no-show-raw) 1102 * which looks like following 1103 * 1104 * 0000000000415500 <_init>: 1105 * 415500: sub $0x8,%rsp 1106 * 415504: mov 0x2f5ad5(%rip),%rax # 70afe0 <_DYNAMIC+0x2f8> 1107 * 41550b: test %rax,%rax 1108 * 41550e: je 415515 <_init+0x15> 1109 * 415510: callq 416e70 <__gmon_start__@plt> 1110 * 415515: add $0x8,%rsp 1111 * 415519: retq 1112 * 1113 * it will be parsed and saved into struct disasm_line as 1114 * <offset> <name> <ops.raw> 1115 * 1116 * The offset will be a relative offset from the start of the symbol and -1 1117 * means that it's not a disassembly line so should be treated differently. 1118 * The ops.raw part will be parsed further according to type of the instruction. 1119 */ 1120static int symbol__parse_objdump_line(struct symbol *sym, 1121 struct annotate_args *args, 1122 char *parsed_line, int *line_nr, char **fileloc) 1123{ 1124 struct map *map = args->ms->map; 1125 struct annotation *notes = symbol__annotation(sym); 1126 struct disasm_line *dl; 1127 char *tmp; 1128 s64 line_ip, offset = -1; 1129 regmatch_t match[2]; 1130 1131 /* /filename:linenr ? Save line number and ignore. */ 1132 if (regexec(&file_lineno, parsed_line, 2, match, 0) == 0) { 1133 *line_nr = atoi(parsed_line + match[1].rm_so); 1134 free(*fileloc); 1135 *fileloc = strdup(parsed_line); 1136 return 0; 1137 } 1138 1139 /* Process hex address followed by ':'. */ 1140 line_ip = strtoull(parsed_line, &tmp, 16); 1141 if (parsed_line != tmp && tmp[0] == ':' && tmp[1] != '\0') { 1142 u64 start = map__rip_2objdump(map, sym->start), 1143 end = map__rip_2objdump(map, sym->end); 1144 1145 offset = line_ip - start; 1146 if ((u64)line_ip < start || (u64)line_ip >= end) 1147 offset = -1; 1148 else 1149 parsed_line = tmp + 1; 1150 } 1151 1152 args->offset = offset; 1153 args->line = parsed_line; 1154 args->line_nr = *line_nr; 1155 args->fileloc = *fileloc; 1156 args->ms->sym = sym; 1157 1158 dl = disasm_line__new(args); 1159 (*line_nr)++; 1160 1161 if (dl == NULL) 1162 return -1; 1163 1164 if (!disasm_line__has_local_offset(dl)) { 1165 dl->ops.target.offset = dl->ops.target.addr - 1166 map__rip_2objdump(map, sym->start); 1167 dl->ops.target.offset_avail = true; 1168 } 1169 1170 /* kcore has no symbols, so add the call target symbol */ 1171 if (dl->ins.ops && ins__is_call(&dl->ins) && !dl->ops.target.sym) { 1172 struct addr_map_symbol target = { 1173 .addr = dl->ops.target.addr, 1174 .ms = { .map = map__get(map), }, 1175 }; 1176 1177 if (!maps__find_ams(args->ms->maps, &target) && 1178 target.ms.sym->start == target.al_addr) 1179 dl->ops.target.sym = target.ms.sym; 1180 1181 addr_map_symbol__exit(&target); 1182 } 1183 1184 annotation_line__add(&dl->al, ¬es->src->source); 1185 return 0; 1186} 1187 1188static void delete_last_nop(struct symbol *sym) 1189{ 1190 struct annotation *notes = symbol__annotation(sym); 1191 struct list_head *list = ¬es->src->source; 1192 struct disasm_line *dl; 1193 1194 while (!list_empty(list)) { 1195 dl = list_entry(list->prev, struct disasm_line, al.node); 1196 1197 if (dl->ins.ops) { 1198 if (!ins__is_nop(&dl->ins)) 1199 return; 1200 } else { 1201 if (!strstr(dl->al.line, " nop ") && 1202 !strstr(dl->al.line, " nopl ") && 1203 !strstr(dl->al.line, " nopw ")) 1204 return; 1205 } 1206 1207 list_del_init(&dl->al.node); 1208 disasm_line__free(dl); 1209 } 1210} 1211 1212int symbol__strerror_disassemble(struct map_symbol *ms, int errnum, char *buf, size_t buflen) 1213{ 1214 struct dso *dso = map__dso(ms->map); 1215 1216 BUG_ON(buflen == 0); 1217 1218 if (errnum >= 0) { 1219 str_error_r(errnum, buf, buflen); 1220 return 0; 1221 } 1222 1223 switch (errnum) { 1224 case SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX: { 1225 char bf[SBUILD_ID_SIZE + 15] = " with build id "; 1226 char *build_id_msg = NULL; 1227 1228 if (dso__has_build_id(dso)) { 1229 build_id__snprintf(dso__bid(dso), bf + 15, sizeof(bf) - 15); 1230 build_id_msg = bf; 1231 } 1232 scnprintf(buf, buflen, 1233 "No vmlinux file%s\nwas found in the path.\n\n" 1234 "Note that annotation using /proc/kcore requires CAP_SYS_RAWIO capability.\n\n" 1235 "Please use:\n\n" 1236 " perf buildid-cache -vu vmlinux\n\n" 1237 "or:\n\n" 1238 " --vmlinux vmlinux\n", build_id_msg ?: ""); 1239 } 1240 break; 1241 case SYMBOL_ANNOTATE_ERRNO__NO_LIBOPCODES_FOR_BPF: 1242 scnprintf(buf, buflen, "Please link with binutils's libopcode to enable BPF annotation"); 1243 break; 1244 case SYMBOL_ANNOTATE_ERRNO__ARCH_INIT_REGEXP: 1245 scnprintf(buf, buflen, "Problems with arch specific instruction name regular expressions."); 1246 break; 1247 case SYMBOL_ANNOTATE_ERRNO__ARCH_INIT_CPUID_PARSING: 1248 scnprintf(buf, buflen, "Problems while parsing the CPUID in the arch specific initialization."); 1249 break; 1250 case SYMBOL_ANNOTATE_ERRNO__BPF_INVALID_FILE: 1251 scnprintf(buf, buflen, "Invalid BPF file: %s.", dso__long_name(dso)); 1252 break; 1253 case SYMBOL_ANNOTATE_ERRNO__BPF_MISSING_BTF: 1254 scnprintf(buf, buflen, "The %s BPF file has no BTF section, compile with -g or use pahole -J.", 1255 dso__long_name(dso)); 1256 break; 1257 case SYMBOL_ANNOTATE_ERRNO__COULDNT_DETERMINE_FILE_TYPE: 1258 scnprintf(buf, buflen, "Couldn't determine the file %s type.", dso__long_name(dso)); 1259 break; 1260 default: 1261 scnprintf(buf, buflen, "Internal error: Invalid %d error code\n", errnum); 1262 break; 1263 } 1264 1265 return 0; 1266} 1267 1268static int dso__disassemble_filename(struct dso *dso, char *filename, size_t filename_size) 1269{ 1270 char linkname[PATH_MAX]; 1271 char *build_id_filename; 1272 char *build_id_path = NULL; 1273 char *pos; 1274 int len; 1275 1276 if (dso__symtab_type(dso) == DSO_BINARY_TYPE__KALLSYMS && 1277 !dso__is_kcore(dso)) 1278 return SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX; 1279 1280 build_id_filename = dso__build_id_filename(dso, NULL, 0, false); 1281 if (build_id_filename) { 1282 __symbol__join_symfs(filename, filename_size, build_id_filename); 1283 free(build_id_filename); 1284 } else { 1285 if (dso__has_build_id(dso)) 1286 return ENOMEM; 1287 goto fallback; 1288 } 1289 1290 build_id_path = strdup(filename); 1291 if (!build_id_path) 1292 return ENOMEM; 1293 1294 /* 1295 * old style build-id cache has name of XX/XXXXXXX.. while 1296 * new style has XX/XXXXXXX../{elf,kallsyms,vdso}. 1297 * extract the build-id part of dirname in the new style only. 1298 */ 1299 pos = strrchr(build_id_path, '/'); 1300 if (pos && strlen(pos) < SBUILD_ID_SIZE - 2) 1301 dirname(build_id_path); 1302 1303 if (dso__is_kcore(dso)) 1304 goto fallback; 1305 1306 len = readlink(build_id_path, linkname, sizeof(linkname) - 1); 1307 if (len < 0) 1308 goto fallback; 1309 1310 linkname[len] = '\0'; 1311 if (strstr(linkname, DSO__NAME_KALLSYMS) || 1312 access(filename, R_OK)) { 1313fallback: 1314 /* 1315 * If we don't have build-ids or the build-id file isn't in the 1316 * cache, or is just a kallsyms file, well, lets hope that this 1317 * DSO is the same as when 'perf record' ran. 1318 */ 1319 if (dso__kernel(dso) && dso__long_name(dso)[0] == '/') 1320 snprintf(filename, filename_size, "%s", dso__long_name(dso)); 1321 else 1322 __symbol__join_symfs(filename, filename_size, dso__long_name(dso)); 1323 1324 mutex_lock(dso__lock(dso)); 1325 if (access(filename, R_OK) && errno == ENOENT && dso__nsinfo(dso)) { 1326 char *new_name = dso__filename_with_chroot(dso, filename); 1327 if (new_name) { 1328 strlcpy(filename, new_name, filename_size); 1329 free(new_name); 1330 } 1331 } 1332 mutex_unlock(dso__lock(dso)); 1333 } else if (dso__binary_type(dso) == DSO_BINARY_TYPE__NOT_FOUND) { 1334 dso__set_binary_type(dso, DSO_BINARY_TYPE__BUILD_ID_CACHE); 1335 } 1336 1337 free(build_id_path); 1338 return 0; 1339} 1340 1341static int symbol__disassemble_raw(char *filename, struct symbol *sym, 1342 struct annotate_args *args) 1343{ 1344 struct annotation *notes = symbol__annotation(sym); 1345 struct map *map = args->ms->map; 1346 struct dso *dso = map__dso(map); 1347 u64 start = map__rip_2objdump(map, sym->start); 1348 u64 end = map__rip_2objdump(map, sym->end); 1349 u64 len = end - start; 1350 u64 offset; 1351 int i, count; 1352 u8 *buf = NULL; 1353 char disasm_buf[512]; 1354 struct disasm_line *dl; 1355 u32 *line; 1356 1357 /* Return if objdump is specified explicitly */ 1358 if (args->options->objdump_path) 1359 return -1; 1360 1361 pr_debug("Reading raw instruction from : %s using dso__data_read_offset\n", filename); 1362 1363 buf = malloc(len); 1364 if (buf == NULL) 1365 goto err; 1366 1367 count = dso__data_read_offset(dso, NULL, sym->start, buf, len); 1368 1369 line = (u32 *)buf; 1370 1371 if ((u64)count != len) 1372 goto err; 1373 1374 /* add the function address and name */ 1375 scnprintf(disasm_buf, sizeof(disasm_buf), "%#"PRIx64" <%s>:", 1376 start, sym->name); 1377 1378 args->offset = -1; 1379 args->line = disasm_buf; 1380 args->line_nr = 0; 1381 args->fileloc = NULL; 1382 args->ms->sym = sym; 1383 1384 dl = disasm_line__new(args); 1385 if (dl == NULL) 1386 goto err; 1387 1388 annotation_line__add(&dl->al, ¬es->src->source); 1389 1390 /* Each raw instruction is 4 byte */ 1391 count = len/4; 1392 1393 for (i = 0, offset = 0; i < count; i++) { 1394 args->offset = offset; 1395 sprintf(args->line, "%x", line[i]); 1396 dl = disasm_line__new(args); 1397 if (dl == NULL) 1398 break; 1399 1400 annotation_line__add(&dl->al, ¬es->src->source); 1401 offset += 4; 1402 } 1403 1404 /* It failed in the middle */ 1405 if (offset != len) { 1406 struct list_head *list = ¬es->src->source; 1407 1408 /* Discard all lines and fallback to objdump */ 1409 while (!list_empty(list)) { 1410 dl = list_first_entry(list, struct disasm_line, al.node); 1411 1412 list_del_init(&dl->al.node); 1413 disasm_line__free(dl); 1414 } 1415 count = -1; 1416 } 1417 1418out: 1419 free(buf); 1420 return count < 0 ? count : 0; 1421 1422err: 1423 count = -1; 1424 goto out; 1425} 1426 1427/* 1428 * Possibly create a new version of line with tabs expanded. Returns the 1429 * existing or new line, storage is updated if a new line is allocated. If 1430 * allocation fails then NULL is returned. 1431 */ 1432char *expand_tabs(char *line, char **storage, size_t *storage_len) 1433{ 1434 size_t i, src, dst, len, new_storage_len, num_tabs; 1435 char *new_line; 1436 size_t line_len = strlen(line); 1437 1438 for (num_tabs = 0, i = 0; i < line_len; i++) 1439 if (line[i] == '\t') 1440 num_tabs++; 1441 1442 if (num_tabs == 0) 1443 return line; 1444 1445 /* 1446 * Space for the line and '\0', less the leading and trailing 1447 * spaces. Each tab may introduce 7 additional spaces. 1448 */ 1449 new_storage_len = line_len + 1 + (num_tabs * 7); 1450 1451 new_line = malloc(new_storage_len); 1452 if (new_line == NULL) { 1453 pr_err("Failure allocating memory for tab expansion\n"); 1454 return NULL; 1455 } 1456 1457 /* 1458 * Copy regions starting at src and expand tabs. If there are two 1459 * adjacent tabs then 'src == i', the memcpy is of size 0 and the spaces 1460 * are inserted. 1461 */ 1462 for (i = 0, src = 0, dst = 0; i < line_len && num_tabs; i++) { 1463 if (line[i] == '\t') { 1464 len = i - src; 1465 memcpy(&new_line[dst], &line[src], len); 1466 dst += len; 1467 new_line[dst++] = ' '; 1468 while (dst % 8 != 0) 1469 new_line[dst++] = ' '; 1470 src = i + 1; 1471 num_tabs--; 1472 } 1473 } 1474 1475 /* Expand the last region. */ 1476 len = line_len - src; 1477 memcpy(&new_line[dst], &line[src], len); 1478 dst += len; 1479 new_line[dst] = '\0'; 1480 1481 free(*storage); 1482 *storage = new_line; 1483 *storage_len = new_storage_len; 1484 return new_line; 1485} 1486 1487static int symbol__disassemble_bpf_image(struct symbol *sym, struct annotate_args *args) 1488{ 1489 struct annotation *notes = symbol__annotation(sym); 1490 struct disasm_line *dl; 1491 1492 args->offset = -1; 1493 args->line = strdup("to be implemented"); 1494 args->line_nr = 0; 1495 args->fileloc = NULL; 1496 dl = disasm_line__new(args); 1497 if (dl) 1498 annotation_line__add(&dl->al, ¬es->src->source); 1499 1500 zfree(&args->line); 1501 return 0; 1502} 1503 1504static int symbol__disassemble_objdump(const char *filename, struct symbol *sym, 1505 struct annotate_args *args) 1506{ 1507 struct annotation_options *opts = &annotate_opts; 1508 struct map *map = args->ms->map; 1509 struct dso *dso = map__dso(map); 1510 char *command; 1511 FILE *file; 1512 int lineno = 0; 1513 char *fileloc = NULL; 1514 int nline; 1515 char *line; 1516 size_t line_len; 1517 const char *objdump_argv[] = { 1518 "/bin/sh", 1519 "-c", 1520 NULL, /* Will be the objdump command to run. */ 1521 "--", 1522 NULL, /* Will be the symfs path. */ 1523 NULL, 1524 }; 1525 struct child_process objdump_process; 1526 int err; 1527 1528 if (dso__binary_type(dso) == DSO_BINARY_TYPE__BPF_PROG_INFO) 1529 return symbol__disassemble_bpf_libbfd(sym, args); 1530 1531 if (dso__binary_type(dso) == DSO_BINARY_TYPE__BPF_IMAGE) 1532 return symbol__disassemble_bpf_image(sym, args); 1533 1534 err = asprintf(&command, 1535 "%s %s%s --start-address=0x%016" PRIx64 1536 " --stop-address=0x%016" PRIx64 1537 " %s -d %s %s %s %c%s%c %s%s -C \"$1\"", 1538 opts->objdump_path ?: "objdump", 1539 opts->disassembler_style ? "-M " : "", 1540 opts->disassembler_style ?: "", 1541 map__rip_2objdump(map, sym->start), 1542 map__rip_2objdump(map, sym->end), 1543 opts->show_linenr ? "-l" : "", 1544 opts->show_asm_raw ? "" : "--no-show-raw-insn", 1545 opts->annotate_src ? "-S" : "", 1546 opts->prefix ? "--prefix " : "", 1547 opts->prefix ? '"' : ' ', 1548 opts->prefix ?: "", 1549 opts->prefix ? '"' : ' ', 1550 opts->prefix_strip ? "--prefix-strip=" : "", 1551 opts->prefix_strip ?: ""); 1552 1553 if (err < 0) { 1554 pr_err("Failure allocating memory for the command to run\n"); 1555 return err; 1556 } 1557 1558 pr_debug("Executing: %s\n", command); 1559 1560 objdump_argv[2] = command; 1561 objdump_argv[4] = filename; 1562 1563 /* Create a pipe to read from for stdout */ 1564 memset(&objdump_process, 0, sizeof(objdump_process)); 1565 objdump_process.argv = objdump_argv; 1566 objdump_process.out = -1; 1567 objdump_process.err = -1; 1568 objdump_process.no_stderr = 1; 1569 if (start_command(&objdump_process)) { 1570 pr_err("Failure starting to run %s\n", command); 1571 err = -1; 1572 goto out_free_command; 1573 } 1574 1575 file = fdopen(objdump_process.out, "r"); 1576 if (!file) { 1577 pr_err("Failure creating FILE stream for %s\n", command); 1578 /* 1579 * If we were using debug info should retry with 1580 * original binary. 1581 */ 1582 err = -1; 1583 goto out_close_stdout; 1584 } 1585 1586 /* Storage for getline. */ 1587 line = NULL; 1588 line_len = 0; 1589 1590 nline = 0; 1591 while (!feof(file)) { 1592 const char *match; 1593 char *expanded_line; 1594 1595 if (getline(&line, &line_len, file) < 0 || !line) 1596 break; 1597 1598 /* Skip lines containing "filename:" */ 1599 match = strstr(line, filename); 1600 if (match && match[strlen(filename)] == ':') 1601 continue; 1602 1603 expanded_line = strim(line); 1604 expanded_line = expand_tabs(expanded_line, &line, &line_len); 1605 if (!expanded_line) 1606 break; 1607 1608 /* 1609 * The source code line number (lineno) needs to be kept in 1610 * across calls to symbol__parse_objdump_line(), so that it 1611 * can associate it with the instructions till the next one. 1612 * See disasm_line__new() and struct disasm_line::line_nr. 1613 */ 1614 if (symbol__parse_objdump_line(sym, args, expanded_line, 1615 &lineno, &fileloc) < 0) 1616 break; 1617 nline++; 1618 } 1619 free(line); 1620 free(fileloc); 1621 1622 err = finish_command(&objdump_process); 1623 if (err) 1624 pr_err("Error running %s\n", command); 1625 1626 if (nline == 0) { 1627 err = -1; 1628 pr_err("No output from %s\n", command); 1629 } 1630 1631 /* 1632 * kallsyms does not have symbol sizes so there may a nop at the end. 1633 * Remove it. 1634 */ 1635 if (dso__is_kcore(dso)) 1636 delete_last_nop(sym); 1637 1638 fclose(file); 1639 1640out_close_stdout: 1641 close(objdump_process.out); 1642 1643out_free_command: 1644 free(command); 1645 return err; 1646} 1647 1648int symbol__disassemble(struct symbol *sym, struct annotate_args *args) 1649{ 1650 struct annotation_options *options = args->options; 1651 struct map *map = args->ms->map; 1652 struct dso *dso = map__dso(map); 1653 char symfs_filename[PATH_MAX]; 1654 bool delete_extract = false; 1655 struct kcore_extract kce; 1656 bool decomp = false; 1657 int err = dso__disassemble_filename(dso, symfs_filename, sizeof(symfs_filename)); 1658 1659 if (err) 1660 return err; 1661 1662 pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__, 1663 symfs_filename, sym->name, map__unmap_ip(map, sym->start), 1664 map__unmap_ip(map, sym->end)); 1665 1666 pr_debug("annotating [%p] %30s : [%p] %30s\n", dso, dso__long_name(dso), sym, sym->name); 1667 1668 if (dso__binary_type(dso) == DSO_BINARY_TYPE__NOT_FOUND) { 1669 return SYMBOL_ANNOTATE_ERRNO__COULDNT_DETERMINE_FILE_TYPE; 1670 } else if (dso__is_kcore(dso)) { 1671 kce.addr = map__rip_2objdump(map, sym->start); 1672 kce.kcore_filename = symfs_filename; 1673 kce.len = sym->end - sym->start; 1674 kce.offs = sym->start; 1675 1676 if (!kcore_extract__create(&kce)) { 1677 delete_extract = true; 1678 strlcpy(symfs_filename, kce.extract_filename, sizeof(symfs_filename)); 1679 } 1680 } else if (dso__needs_decompress(dso)) { 1681 char tmp[KMOD_DECOMP_LEN]; 1682 1683 if (dso__decompress_kmodule_path(dso, symfs_filename, tmp, sizeof(tmp)) < 0) 1684 return -1; 1685 1686 decomp = true; 1687 strcpy(symfs_filename, tmp); 1688 } 1689 1690 /* 1691 * For powerpc data type profiling, use the dso__data_read_offset to 1692 * read raw instruction directly and interpret the binary code to 1693 * understand instructions and register fields. For sort keys as type 1694 * and typeoff, disassemble to mnemonic notation is not required in 1695 * case of powerpc. 1696 */ 1697 if (arch__is(args->arch, "powerpc")) { 1698 extern const char *sort_order; 1699 1700 if (sort_order && !strstr(sort_order, "sym")) { 1701 err = symbol__disassemble_raw(symfs_filename, sym, args); 1702 if (err == 0) 1703 goto out_remove_tmp; 1704 1705 err = symbol__disassemble_capstone_powerpc(symfs_filename, sym, args); 1706 if (err == 0) 1707 goto out_remove_tmp; 1708 } 1709 } 1710 1711 /* FIXME: LLVM and CAPSTONE should support source code */ 1712 if (options->annotate_src && !options->hide_src_code) { 1713 err = symbol__disassemble_objdump(symfs_filename, sym, args); 1714 if (err == 0) 1715 goto out_remove_tmp; 1716 } 1717 1718 err = -1; 1719 for (u8 i = 0; i < ARRAY_SIZE(options->disassemblers) && err != 0; i++) { 1720 enum perf_disassembler dis = options->disassemblers[i]; 1721 1722 switch (dis) { 1723 case PERF_DISASM_LLVM: 1724 args->options->disassembler_used = PERF_DISASM_LLVM; 1725 err = symbol__disassemble_llvm(symfs_filename, sym, args); 1726 break; 1727 case PERF_DISASM_CAPSTONE: 1728 args->options->disassembler_used = PERF_DISASM_CAPSTONE; 1729 err = symbol__disassemble_capstone(symfs_filename, sym, args); 1730 break; 1731 case PERF_DISASM_OBJDUMP: 1732 args->options->disassembler_used = PERF_DISASM_OBJDUMP; 1733 err = symbol__disassemble_objdump(symfs_filename, sym, args); 1734 break; 1735 case PERF_DISASM_UNKNOWN: /* End of disassemblers. */ 1736 default: 1737 args->options->disassembler_used = PERF_DISASM_UNKNOWN; 1738 goto out_remove_tmp; 1739 } 1740 if (err == 0) 1741 pr_debug("Disassembled with %s\n", perf_disassembler__strs[dis]); 1742 } 1743out_remove_tmp: 1744 if (decomp) 1745 unlink(symfs_filename); 1746 1747 if (delete_extract) 1748 kcore_extract__delete(&kce); 1749 1750 return err; 1751}
~/
Read-only file preview.