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