1 //===-- Disassembler.cpp ----------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "lldb/Core/Disassembler.h" 11 12 // C Includes 13 // C++ Includes 14 #include <cstdio> 15 #include <cstring> 16 17 // Other libraries and framework includes 18 // Project includes 19 #include "lldb/Core/DataBufferHeap.h" 20 #include "lldb/Core/DataExtractor.h" 21 #include "lldb/Core/Debugger.h" 22 #include "lldb/Core/EmulateInstruction.h" 23 #include "lldb/Core/Error.h" 24 #include "lldb/Core/Module.h" 25 #include "lldb/Core/PluginManager.h" 26 #include "lldb/Core/RegularExpression.h" 27 #include "lldb/Core/Timer.h" 28 #include "lldb/Host/FileSystem.h" 29 #include "lldb/Interpreter/OptionValue.h" 30 #include "lldb/Interpreter/OptionValueArray.h" 31 #include "lldb/Interpreter/OptionValueDictionary.h" 32 #include "lldb/Interpreter/OptionValueString.h" 33 #include "lldb/Interpreter/OptionValueUInt64.h" 34 #include "lldb/Symbol/Function.h" 35 #include "lldb/Symbol/ObjectFile.h" 36 #include "lldb/Target/ExecutionContext.h" 37 #include "lldb/Target/Process.h" 38 #include "lldb/Target/SectionLoadList.h" 39 #include "lldb/Target/StackFrame.h" 40 #include "lldb/Target/Target.h" 41 #include "lldb/lldb-private.h" 42 43 #define DEFAULT_DISASM_BYTE_SIZE 32 44 45 using namespace lldb; 46 using namespace lldb_private; 47 48 DisassemblerSP 49 Disassembler::FindPlugin (const ArchSpec &arch, const char *flavor, const char *plugin_name) 50 { 51 Timer scoped_timer (__PRETTY_FUNCTION__, 52 "Disassembler::FindPlugin (arch = %s, plugin_name = %s)", 53 arch.GetArchitectureName(), 54 plugin_name); 55 56 DisassemblerCreateInstance create_callback = nullptr; 57 58 if (plugin_name) 59 { 60 ConstString const_plugin_name (plugin_name); 61 create_callback = PluginManager::GetDisassemblerCreateCallbackForPluginName (const_plugin_name); 62 if (create_callback) 63 { 64 DisassemblerSP disassembler_sp(create_callback(arch, flavor)); 65 66 if (disassembler_sp) 67 return disassembler_sp; 68 } 69 } 70 else 71 { 72 for (uint32_t idx = 0; (create_callback = PluginManager::GetDisassemblerCreateCallbackAtIndex(idx)) != nullptr; ++idx) 73 { 74 DisassemblerSP disassembler_sp(create_callback(arch, flavor)); 75 76 if (disassembler_sp) 77 return disassembler_sp; 78 } 79 } 80 return DisassemblerSP(); 81 } 82 83 DisassemblerSP 84 Disassembler::FindPluginForTarget(const TargetSP target_sp, const ArchSpec &arch, const char *flavor, const char *plugin_name) 85 { 86 if (target_sp && flavor == nullptr) 87 { 88 // FIXME - we don't have the mechanism in place to do per-architecture settings. But since we know that for now 89 // we only support flavors on x86 & x86_64, 90 if (arch.GetTriple().getArch() == llvm::Triple::x86 91 || arch.GetTriple().getArch() == llvm::Triple::x86_64) 92 flavor = target_sp->GetDisassemblyFlavor(); 93 } 94 return FindPlugin(arch, flavor, plugin_name); 95 } 96 97 static void 98 ResolveAddress (const ExecutionContext &exe_ctx, 99 const Address &addr, 100 Address &resolved_addr) 101 { 102 if (!addr.IsSectionOffset()) 103 { 104 // If we weren't passed in a section offset address range, 105 // try and resolve it to something 106 Target *target = exe_ctx.GetTargetPtr(); 107 if (target) 108 { 109 if (target->GetSectionLoadList().IsEmpty()) 110 { 111 target->GetImages().ResolveFileAddress (addr.GetOffset(), resolved_addr); 112 } 113 else 114 { 115 target->GetSectionLoadList().ResolveLoadAddress (addr.GetOffset(), resolved_addr); 116 } 117 // We weren't able to resolve the address, just treat it as a 118 // raw address 119 if (resolved_addr.IsValid()) 120 return; 121 } 122 } 123 resolved_addr = addr; 124 } 125 126 size_t 127 Disassembler::Disassemble(Debugger &debugger, 128 const ArchSpec &arch, 129 const char *plugin_name, 130 const char *flavor, 131 const ExecutionContext &exe_ctx, 132 SymbolContextList &sc_list, 133 uint32_t num_instructions, 134 uint32_t num_mixed_context_lines, 135 uint32_t options, 136 Stream &strm) 137 { 138 size_t success_count = 0; 139 const size_t count = sc_list.GetSize(); 140 SymbolContext sc; 141 AddressRange range; 142 const uint32_t scope = eSymbolContextBlock | eSymbolContextFunction | eSymbolContextSymbol; 143 const bool use_inline_block_range = true; 144 for (size_t i = 0; i < count; ++i) 145 { 146 if (!sc_list.GetContextAtIndex(i, sc)) 147 break; 148 for (uint32_t range_idx = 0; sc.GetAddressRange(scope, range_idx, use_inline_block_range, range); ++range_idx) 149 { 150 if (Disassemble (debugger, 151 arch, 152 plugin_name, 153 flavor, 154 exe_ctx, 155 range, 156 num_instructions, 157 num_mixed_context_lines, 158 options, 159 strm)) 160 { 161 ++success_count; 162 strm.EOL(); 163 } 164 } 165 } 166 return success_count; 167 } 168 169 bool 170 Disassembler::Disassemble(Debugger &debugger, 171 const ArchSpec &arch, 172 const char *plugin_name, 173 const char *flavor, 174 const ExecutionContext &exe_ctx, 175 const ConstString &name, 176 Module *module, 177 uint32_t num_instructions, 178 uint32_t num_mixed_context_lines, 179 uint32_t options, 180 Stream &strm) 181 { 182 SymbolContextList sc_list; 183 if (name) 184 { 185 const bool include_symbols = true; 186 const bool include_inlines = true; 187 if (module) 188 { 189 module->FindFunctions(name, 190 nullptr, 191 eFunctionNameTypeAuto, 192 include_symbols, 193 include_inlines, 194 true, 195 sc_list); 196 } 197 else if (exe_ctx.GetTargetPtr()) 198 { 199 exe_ctx.GetTargetPtr()->GetImages().FindFunctions (name, 200 eFunctionNameTypeAuto, 201 include_symbols, 202 include_inlines, 203 false, 204 sc_list); 205 } 206 } 207 208 if (sc_list.GetSize ()) 209 { 210 return Disassemble (debugger, 211 arch, 212 plugin_name, 213 flavor, 214 exe_ctx, 215 sc_list, 216 num_instructions, 217 num_mixed_context_lines, 218 options, 219 strm); 220 } 221 return false; 222 } 223 224 lldb::DisassemblerSP 225 Disassembler::DisassembleRange(const ArchSpec &arch, 226 const char *plugin_name, 227 const char *flavor, 228 const ExecutionContext &exe_ctx, 229 const AddressRange &range, 230 bool prefer_file_cache) 231 { 232 lldb::DisassemblerSP disasm_sp; 233 if (range.GetByteSize() > 0 && range.GetBaseAddress().IsValid()) 234 { 235 disasm_sp = Disassembler::FindPluginForTarget(exe_ctx.GetTargetSP(), arch, flavor, plugin_name); 236 237 if (disasm_sp) 238 { 239 size_t bytes_disassembled = disasm_sp->ParseInstructions(&exe_ctx, range, nullptr, prefer_file_cache); 240 if (bytes_disassembled == 0) 241 disasm_sp.reset(); 242 } 243 } 244 return disasm_sp; 245 } 246 247 lldb::DisassemblerSP 248 Disassembler::DisassembleBytes (const ArchSpec &arch, 249 const char *plugin_name, 250 const char *flavor, 251 const Address &start, 252 const void *src, 253 size_t src_len, 254 uint32_t num_instructions, 255 bool data_from_file) 256 { 257 lldb::DisassemblerSP disasm_sp; 258 259 if (src) 260 { 261 disasm_sp = Disassembler::FindPlugin(arch, flavor, plugin_name); 262 263 if (disasm_sp) 264 { 265 DataExtractor data(src, src_len, arch.GetByteOrder(), arch.GetAddressByteSize()); 266 267 (void)disasm_sp->DecodeInstructions (start, 268 data, 269 0, 270 num_instructions, 271 false, 272 data_from_file); 273 } 274 } 275 276 return disasm_sp; 277 } 278 279 bool 280 Disassembler::Disassemble(Debugger &debugger, 281 const ArchSpec &arch, 282 const char *plugin_name, 283 const char *flavor, 284 const ExecutionContext &exe_ctx, 285 const AddressRange &disasm_range, 286 uint32_t num_instructions, 287 uint32_t num_mixed_context_lines, 288 uint32_t options, 289 Stream &strm) 290 { 291 if (disasm_range.GetByteSize()) 292 { 293 lldb::DisassemblerSP disasm_sp (Disassembler::FindPluginForTarget(exe_ctx.GetTargetSP(), arch, flavor, plugin_name)); 294 295 if (disasm_sp) 296 { 297 AddressRange range; 298 ResolveAddress (exe_ctx, disasm_range.GetBaseAddress(), range.GetBaseAddress()); 299 range.SetByteSize (disasm_range.GetByteSize()); 300 const bool prefer_file_cache = false; 301 size_t bytes_disassembled = disasm_sp->ParseInstructions (&exe_ctx, range, &strm, prefer_file_cache); 302 if (bytes_disassembled == 0) 303 return false; 304 305 return PrintInstructions(disasm_sp.get(), debugger, arch, exe_ctx, num_instructions, 306 num_mixed_context_lines, options, strm); 307 } 308 } 309 return false; 310 } 311 312 bool 313 Disassembler::Disassemble(Debugger &debugger, 314 const ArchSpec &arch, 315 const char *plugin_name, 316 const char *flavor, 317 const ExecutionContext &exe_ctx, 318 const Address &start_address, 319 uint32_t num_instructions, 320 uint32_t num_mixed_context_lines, 321 uint32_t options, 322 Stream &strm) 323 { 324 if (num_instructions > 0) 325 { 326 lldb::DisassemblerSP disasm_sp (Disassembler::FindPluginForTarget(exe_ctx.GetTargetSP(), 327 arch, 328 flavor, 329 plugin_name)); 330 if (disasm_sp) 331 { 332 Address addr; 333 ResolveAddress (exe_ctx, start_address, addr); 334 const bool prefer_file_cache = false; 335 size_t bytes_disassembled = disasm_sp->ParseInstructions (&exe_ctx, 336 addr, 337 num_instructions, 338 prefer_file_cache); 339 if (bytes_disassembled == 0) 340 return false; 341 return PrintInstructions(disasm_sp.get(), debugger, arch, exe_ctx, num_instructions, 342 num_mixed_context_lines, options, strm); 343 } 344 } 345 return false; 346 } 347 348 bool 349 Disassembler::PrintInstructions(Disassembler *disasm_ptr, Debugger &debugger, const ArchSpec &arch, 350 const ExecutionContext &exe_ctx, uint32_t num_instructions, 351 uint32_t num_mixed_context_lines, uint32_t options, Stream &strm) 352 { 353 // We got some things disassembled... 354 size_t num_instructions_found = disasm_ptr->GetInstructionList().GetSize(); 355 356 if (num_instructions > 0 && num_instructions < num_instructions_found) 357 num_instructions_found = num_instructions; 358 359 const uint32_t max_opcode_byte_size = disasm_ptr->GetInstructionList().GetMaxOpcocdeByteSize (); 360 uint32_t offset = 0; 361 SymbolContext sc; 362 SymbolContext prev_sc; 363 AddressRange sc_range; 364 const Address *pc_addr_ptr = nullptr; 365 StackFrame *frame = exe_ctx.GetFramePtr(); 366 367 TargetSP target_sp (exe_ctx.GetTargetSP()); 368 SourceManager &source_manager = target_sp ? target_sp->GetSourceManager() : debugger.GetSourceManager(); 369 370 if (frame) 371 { 372 pc_addr_ptr = &frame->GetFrameCodeAddress(); 373 } 374 const uint32_t scope = eSymbolContextLineEntry | eSymbolContextFunction | eSymbolContextSymbol; 375 const bool use_inline_block_range = false; 376 377 const FormatEntity::Entry *disassembly_format = nullptr; 378 FormatEntity::Entry format; 379 if (exe_ctx.HasTargetScope()) 380 { 381 disassembly_format = exe_ctx.GetTargetRef().GetDebugger().GetDisassemblyFormat (); 382 } 383 else 384 { 385 FormatEntity::Parse("${addr}: ", format); 386 disassembly_format = &format; 387 } 388 389 // First pass: step through the list of instructions, 390 // find how long the initial addresses strings are, insert padding 391 // in the second pass so the opcodes all line up nicely. 392 size_t address_text_size = 0; 393 for (size_t i = 0; i < num_instructions_found; ++i) 394 { 395 Instruction *inst = disasm_ptr->GetInstructionList().GetInstructionAtIndex (i).get(); 396 if (inst) 397 { 398 const Address &addr = inst->GetAddress(); 399 ModuleSP module_sp (addr.GetModule()); 400 if (module_sp) 401 { 402 const uint32_t resolve_mask = eSymbolContextFunction | eSymbolContextSymbol; 403 uint32_t resolved_mask = module_sp->ResolveSymbolContextForAddress(addr, resolve_mask, sc); 404 if (resolved_mask) 405 { 406 StreamString strmstr; 407 Debugger::FormatDisassemblerAddress(disassembly_format, &sc, nullptr, &exe_ctx, &addr, strmstr); 408 size_t cur_line = strmstr.GetSizeOfLastLine(); 409 if (cur_line > address_text_size) 410 address_text_size = cur_line; 411 } 412 sc.Clear(false); 413 } 414 } 415 } 416 417 for (size_t i = 0; i < num_instructions_found; ++i) 418 { 419 Instruction *inst = disasm_ptr->GetInstructionList().GetInstructionAtIndex (i).get(); 420 if (inst) 421 { 422 const Address &addr = inst->GetAddress(); 423 const bool inst_is_at_pc = pc_addr_ptr && addr == *pc_addr_ptr; 424 425 prev_sc = sc; 426 427 ModuleSP module_sp (addr.GetModule()); 428 if (module_sp) 429 { 430 uint32_t resolved_mask = module_sp->ResolveSymbolContextForAddress(addr, eSymbolContextEverything, sc); 431 if (resolved_mask) 432 { 433 if (num_mixed_context_lines) 434 { 435 if (!sc_range.ContainsFileAddress (addr)) 436 { 437 sc.GetAddressRange (scope, 0, use_inline_block_range, sc_range); 438 439 if (sc != prev_sc) 440 { 441 if (offset != 0) 442 strm.EOL(); 443 444 sc.DumpStopContext(&strm, exe_ctx.GetProcessPtr(), addr, false, true, false, false, true); 445 strm.EOL(); 446 447 if (sc.comp_unit && sc.line_entry.IsValid()) 448 { 449 source_manager.DisplaySourceLinesWithLineNumbers (sc.line_entry.file, 450 sc.line_entry.line, 451 num_mixed_context_lines, 452 num_mixed_context_lines, 453 ((inst_is_at_pc && (options & eOptionMarkPCSourceLine)) ? "->" : ""), 454 &strm); 455 } 456 } 457 } 458 } 459 } 460 else 461 { 462 sc.Clear(true); 463 } 464 } 465 466 const bool show_bytes = (options & eOptionShowBytes) != 0; 467 inst->Dump(&strm, max_opcode_byte_size, true, show_bytes, &exe_ctx, &sc, &prev_sc, nullptr, address_text_size); 468 strm.EOL(); 469 } 470 else 471 { 472 break; 473 } 474 } 475 476 return true; 477 } 478 479 bool 480 Disassembler::Disassemble(Debugger &debugger, 481 const ArchSpec &arch, 482 const char *plugin_name, 483 const char *flavor, 484 const ExecutionContext &exe_ctx, 485 uint32_t num_instructions, 486 uint32_t num_mixed_context_lines, 487 uint32_t options, 488 Stream &strm) 489 { 490 AddressRange range; 491 StackFrame *frame = exe_ctx.GetFramePtr(); 492 if (frame) 493 { 494 SymbolContext sc(frame->GetSymbolContext(eSymbolContextFunction | eSymbolContextSymbol)); 495 if (sc.function) 496 { 497 range = sc.function->GetAddressRange(); 498 } 499 else if (sc.symbol && sc.symbol->ValueIsAddress()) 500 { 501 range.GetBaseAddress() = sc.symbol->GetAddressRef(); 502 range.SetByteSize (sc.symbol->GetByteSize()); 503 } 504 else 505 { 506 range.GetBaseAddress() = frame->GetFrameCodeAddress(); 507 } 508 509 if (range.GetBaseAddress().IsValid() && range.GetByteSize() == 0) 510 range.SetByteSize (DEFAULT_DISASM_BYTE_SIZE); 511 } 512 513 return Disassemble (debugger, 514 arch, 515 plugin_name, 516 flavor, 517 exe_ctx, 518 range, 519 num_instructions, 520 num_mixed_context_lines, 521 options, 522 strm); 523 } 524 525 Instruction::Instruction(const Address &address, AddressClass addr_class) : 526 m_address (address), 527 m_address_class (addr_class), 528 m_opcode(), 529 m_calculated_strings(false) 530 { 531 } 532 533 Instruction::~Instruction() = default; 534 535 AddressClass 536 Instruction::GetAddressClass () 537 { 538 if (m_address_class == eAddressClassInvalid) 539 m_address_class = m_address.GetAddressClass(); 540 return m_address_class; 541 } 542 543 void 544 Instruction::Dump (lldb_private::Stream *s, 545 uint32_t max_opcode_byte_size, 546 bool show_address, 547 bool show_bytes, 548 const ExecutionContext* exe_ctx, 549 const SymbolContext *sym_ctx, 550 const SymbolContext *prev_sym_ctx, 551 const FormatEntity::Entry *disassembly_addr_format, 552 size_t max_address_text_size) 553 { 554 size_t opcode_column_width = 7; 555 const size_t operand_column_width = 25; 556 557 CalculateMnemonicOperandsAndCommentIfNeeded (exe_ctx); 558 559 StreamString ss; 560 561 if (show_address) 562 { 563 Debugger::FormatDisassemblerAddress (disassembly_addr_format, sym_ctx, prev_sym_ctx, exe_ctx, &m_address, ss); 564 ss.FillLastLineToColumn (max_address_text_size, ' '); 565 } 566 567 if (show_bytes) 568 { 569 if (m_opcode.GetType() == Opcode::eTypeBytes) 570 { 571 // x86_64 and i386 are the only ones that use bytes right now so 572 // pad out the byte dump to be able to always show 15 bytes (3 chars each) 573 // plus a space 574 if (max_opcode_byte_size > 0) 575 m_opcode.Dump (&ss, max_opcode_byte_size * 3 + 1); 576 else 577 m_opcode.Dump (&ss, 15 * 3 + 1); 578 } 579 else 580 { 581 // Else, we have ARM or MIPS which can show up to a uint32_t 582 // 0x00000000 (10 spaces) plus two for padding... 583 if (max_opcode_byte_size > 0) 584 m_opcode.Dump (&ss, max_opcode_byte_size * 3 + 1); 585 else 586 m_opcode.Dump (&ss, 12); 587 } 588 } 589 590 const size_t opcode_pos = ss.GetSizeOfLastLine(); 591 592 // The default opcode size of 7 characters is plenty for most architectures 593 // but some like arm can pull out the occasional vqrshrun.s16. We won't get 594 // consistent column spacing in these cases, unfortunately. 595 if (m_opcode_name.length() >= opcode_column_width) 596 { 597 opcode_column_width = m_opcode_name.length() + 1; 598 } 599 600 ss.PutCString (m_opcode_name.c_str()); 601 ss.FillLastLineToColumn (opcode_pos + opcode_column_width, ' '); 602 ss.PutCString (m_mnemonics.c_str()); 603 604 if (!m_comment.empty()) 605 { 606 ss.FillLastLineToColumn (opcode_pos + opcode_column_width + operand_column_width, ' '); 607 ss.PutCString (" ; "); 608 ss.PutCString (m_comment.c_str()); 609 } 610 s->Write (ss.GetData(), ss.GetSize()); 611 } 612 613 bool 614 Instruction::DumpEmulation (const ArchSpec &arch) 615 { 616 std::unique_ptr<EmulateInstruction> insn_emulator_ap(EmulateInstruction::FindPlugin(arch, eInstructionTypeAny, nullptr)); 617 if (insn_emulator_ap) 618 { 619 insn_emulator_ap->SetInstruction(GetOpcode(), GetAddress(), nullptr); 620 return insn_emulator_ap->EvaluateInstruction (0); 621 } 622 623 return false; 624 } 625 626 bool 627 Instruction::HasDelaySlot () 628 { 629 // Default is false. 630 return false; 631 } 632 633 OptionValueSP 634 Instruction::ReadArray (FILE *in_file, Stream *out_stream, OptionValue::Type data_type) 635 { 636 bool done = false; 637 char buffer[1024]; 638 639 OptionValueSP option_value_sp (new OptionValueArray (1u << data_type)); 640 641 int idx = 0; 642 while (!done) 643 { 644 if (!fgets (buffer, 1023, in_file)) 645 { 646 out_stream->Printf ("Instruction::ReadArray: Error reading file (fgets).\n"); 647 option_value_sp.reset (); 648 return option_value_sp; 649 } 650 651 std::string line (buffer); 652 653 size_t len = line.size(); 654 if (line[len-1] == '\n') 655 { 656 line[len-1] = '\0'; 657 line.resize (len-1); 658 } 659 660 if ((line.size() == 1) && line[0] == ']') 661 { 662 done = true; 663 line.clear(); 664 } 665 666 if (!line.empty()) 667 { 668 std::string value; 669 static RegularExpression g_reg_exp ("^[ \t]*([^ \t]+)[ \t]*$"); 670 RegularExpression::Match regex_match(1); 671 bool reg_exp_success = g_reg_exp.Execute (line.c_str(), ®ex_match); 672 if (reg_exp_success) 673 regex_match.GetMatchAtIndex (line.c_str(), 1, value); 674 else 675 value = line; 676 677 OptionValueSP data_value_sp; 678 switch (data_type) 679 { 680 case OptionValue::eTypeUInt64: 681 data_value_sp.reset (new OptionValueUInt64 (0, 0)); 682 data_value_sp->SetValueFromString (value); 683 break; 684 // Other types can be added later as needed. 685 default: 686 data_value_sp.reset (new OptionValueString (value.c_str(), "")); 687 break; 688 } 689 690 option_value_sp->GetAsArray()->InsertValue (idx, data_value_sp); 691 ++idx; 692 } 693 } 694 695 return option_value_sp; 696 } 697 698 OptionValueSP 699 Instruction::ReadDictionary (FILE *in_file, Stream *out_stream) 700 { 701 bool done = false; 702 char buffer[1024]; 703 704 OptionValueSP option_value_sp (new OptionValueDictionary()); 705 static ConstString encoding_key ("data_encoding"); 706 OptionValue::Type data_type = OptionValue::eTypeInvalid; 707 708 709 while (!done) 710 { 711 // Read the next line in the file 712 if (!fgets (buffer, 1023, in_file)) 713 { 714 out_stream->Printf ("Instruction::ReadDictionary: Error reading file (fgets).\n"); 715 option_value_sp.reset (); 716 return option_value_sp; 717 } 718 719 // Check to see if the line contains the end-of-dictionary marker ("}") 720 std::string line (buffer); 721 722 size_t len = line.size(); 723 if (line[len-1] == '\n') 724 { 725 line[len-1] = '\0'; 726 line.resize (len-1); 727 } 728 729 if ((line.size() == 1) && (line[0] == '}')) 730 { 731 done = true; 732 line.clear(); 733 } 734 735 // Try to find a key-value pair in the current line and add it to the dictionary. 736 if (!line.empty()) 737 { 738 static RegularExpression g_reg_exp ("^[ \t]*([a-zA-Z_][a-zA-Z0-9_]*)[ \t]*=[ \t]*(.*)[ \t]*$"); 739 RegularExpression::Match regex_match(2); 740 741 bool reg_exp_success = g_reg_exp.Execute (line.c_str(), ®ex_match); 742 std::string key; 743 std::string value; 744 if (reg_exp_success) 745 { 746 regex_match.GetMatchAtIndex (line.c_str(), 1, key); 747 regex_match.GetMatchAtIndex (line.c_str(), 2, value); 748 } 749 else 750 { 751 out_stream->Printf ("Instruction::ReadDictionary: Failure executing regular expression.\n"); 752 option_value_sp.reset(); 753 return option_value_sp; 754 } 755 756 ConstString const_key (key.c_str()); 757 // Check value to see if it's the start of an array or dictionary. 758 759 lldb::OptionValueSP value_sp; 760 assert (value.empty() == false); 761 assert (key.empty() == false); 762 763 if (value[0] == '{') 764 { 765 assert (value.size() == 1); 766 // value is a dictionary 767 value_sp = ReadDictionary (in_file, out_stream); 768 if (!value_sp) 769 { 770 option_value_sp.reset (); 771 return option_value_sp; 772 } 773 } 774 else if (value[0] == '[') 775 { 776 assert (value.size() == 1); 777 // value is an array 778 value_sp = ReadArray (in_file, out_stream, data_type); 779 if (!value_sp) 780 { 781 option_value_sp.reset (); 782 return option_value_sp; 783 } 784 // We've used the data_type to read an array; re-set the type to Invalid 785 data_type = OptionValue::eTypeInvalid; 786 } 787 else if ((value[0] == '0') && (value[1] == 'x')) 788 { 789 value_sp.reset (new OptionValueUInt64 (0, 0)); 790 value_sp->SetValueFromString (value); 791 } 792 else 793 { 794 size_t len = value.size(); 795 if ((value[0] == '"') && (value[len-1] == '"')) 796 value = value.substr (1, len-2); 797 value_sp.reset (new OptionValueString (value.c_str(), "")); 798 } 799 800 if (const_key == encoding_key) 801 { 802 // A 'data_encoding=..." is NOT a normal key-value pair; it is meta-data indicating the 803 // data type of an upcoming array (usually the next bit of data to be read in). 804 if (strcmp (value.c_str(), "uint32_t") == 0) 805 data_type = OptionValue::eTypeUInt64; 806 } 807 else 808 option_value_sp->GetAsDictionary()->SetValueForKey (const_key, value_sp, false); 809 } 810 } 811 812 return option_value_sp; 813 } 814 815 bool 816 Instruction::TestEmulation (Stream *out_stream, const char *file_name) 817 { 818 if (!out_stream) 819 return false; 820 821 if (!file_name) 822 { 823 out_stream->Printf ("Instruction::TestEmulation: Missing file_name."); 824 return false; 825 } 826 FILE *test_file = FileSystem::Fopen(file_name, "r"); 827 if (!test_file) 828 { 829 out_stream->Printf ("Instruction::TestEmulation: Attempt to open test file failed."); 830 return false; 831 } 832 833 char buffer[256]; 834 if (!fgets (buffer, 255, test_file)) 835 { 836 out_stream->Printf ("Instruction::TestEmulation: Error reading first line of test file.\n"); 837 fclose (test_file); 838 return false; 839 } 840 841 if (strncmp (buffer, "InstructionEmulationState={", 27) != 0) 842 { 843 out_stream->Printf ("Instructin::TestEmulation: Test file does not contain emulation state dictionary\n"); 844 fclose (test_file); 845 return false; 846 } 847 848 // Read all the test information from the test file into an OptionValueDictionary. 849 850 OptionValueSP data_dictionary_sp (ReadDictionary (test_file, out_stream)); 851 if (!data_dictionary_sp) 852 { 853 out_stream->Printf ("Instruction::TestEmulation: Error reading Dictionary Object.\n"); 854 fclose (test_file); 855 return false; 856 } 857 858 fclose (test_file); 859 860 OptionValueDictionary *data_dictionary = data_dictionary_sp->GetAsDictionary(); 861 static ConstString description_key ("assembly_string"); 862 static ConstString triple_key ("triple"); 863 864 OptionValueSP value_sp = data_dictionary->GetValueForKey (description_key); 865 866 if (!value_sp) 867 { 868 out_stream->Printf ("Instruction::TestEmulation: Test file does not contain description string.\n"); 869 return false; 870 } 871 872 SetDescription (value_sp->GetStringValue()); 873 874 value_sp = data_dictionary->GetValueForKey (triple_key); 875 if (!value_sp) 876 { 877 out_stream->Printf ("Instruction::TestEmulation: Test file does not contain triple.\n"); 878 return false; 879 } 880 881 ArchSpec arch; 882 arch.SetTriple (llvm::Triple (value_sp->GetStringValue())); 883 884 bool success = false; 885 std::unique_ptr<EmulateInstruction> insn_emulator_ap(EmulateInstruction::FindPlugin(arch, eInstructionTypeAny, nullptr)); 886 if (insn_emulator_ap) 887 success = insn_emulator_ap->TestEmulation (out_stream, arch, data_dictionary); 888 889 if (success) 890 out_stream->Printf ("Emulation test succeeded."); 891 else 892 out_stream->Printf ("Emulation test failed."); 893 894 return success; 895 } 896 897 bool 898 Instruction::Emulate (const ArchSpec &arch, 899 uint32_t evaluate_options, 900 void *baton, 901 EmulateInstruction::ReadMemoryCallback read_mem_callback, 902 EmulateInstruction::WriteMemoryCallback write_mem_callback, 903 EmulateInstruction::ReadRegisterCallback read_reg_callback, 904 EmulateInstruction::WriteRegisterCallback write_reg_callback) 905 { 906 std::unique_ptr<EmulateInstruction> insn_emulator_ap(EmulateInstruction::FindPlugin(arch, eInstructionTypeAny, nullptr)); 907 if (insn_emulator_ap) 908 { 909 insn_emulator_ap->SetBaton(baton); 910 insn_emulator_ap->SetCallbacks(read_mem_callback, write_mem_callback, read_reg_callback, write_reg_callback); 911 insn_emulator_ap->SetInstruction(GetOpcode(), GetAddress(), nullptr); 912 return insn_emulator_ap->EvaluateInstruction(evaluate_options); 913 } 914 915 return false; 916 } 917 918 uint32_t 919 Instruction::GetData (DataExtractor &data) 920 { 921 return m_opcode.GetData(data); 922 } 923 924 InstructionList::InstructionList() : 925 m_instructions() 926 { 927 } 928 929 InstructionList::~InstructionList() = default; 930 931 size_t 932 InstructionList::GetSize() const 933 { 934 return m_instructions.size(); 935 } 936 937 uint32_t 938 InstructionList::GetMaxOpcocdeByteSize () const 939 { 940 uint32_t max_inst_size = 0; 941 collection::const_iterator pos, end; 942 for (pos = m_instructions.begin(), end = m_instructions.end(); 943 pos != end; 944 ++pos) 945 { 946 uint32_t inst_size = (*pos)->GetOpcode().GetByteSize(); 947 if (max_inst_size < inst_size) 948 max_inst_size = inst_size; 949 } 950 return max_inst_size; 951 } 952 953 InstructionSP 954 InstructionList::GetInstructionAtIndex (size_t idx) const 955 { 956 InstructionSP inst_sp; 957 if (idx < m_instructions.size()) 958 inst_sp = m_instructions[idx]; 959 return inst_sp; 960 } 961 962 void 963 InstructionList::Dump (Stream *s, 964 bool show_address, 965 bool show_bytes, 966 const ExecutionContext* exe_ctx) 967 { 968 const uint32_t max_opcode_byte_size = GetMaxOpcocdeByteSize(); 969 collection::const_iterator pos, begin, end; 970 971 const FormatEntity::Entry *disassembly_format = nullptr; 972 FormatEntity::Entry format; 973 if (exe_ctx && exe_ctx->HasTargetScope()) 974 { 975 disassembly_format = exe_ctx->GetTargetRef().GetDebugger().GetDisassemblyFormat (); 976 } 977 else 978 { 979 FormatEntity::Parse("${addr}: ", format); 980 disassembly_format = &format; 981 } 982 983 for (begin = m_instructions.begin(), end = m_instructions.end(), pos = begin; 984 pos != end; 985 ++pos) 986 { 987 if (pos != begin) 988 s->EOL(); 989 (*pos)->Dump(s, max_opcode_byte_size, show_address, show_bytes, exe_ctx, nullptr, nullptr, disassembly_format, 0); 990 } 991 } 992 993 void 994 InstructionList::Clear() 995 { 996 m_instructions.clear(); 997 } 998 999 void 1000 InstructionList::Append (lldb::InstructionSP &inst_sp) 1001 { 1002 if (inst_sp) 1003 m_instructions.push_back(inst_sp); 1004 } 1005 1006 uint32_t 1007 InstructionList::GetIndexOfNextBranchInstruction(uint32_t start, Target &target) const 1008 { 1009 size_t num_instructions = m_instructions.size(); 1010 1011 uint32_t next_branch = UINT32_MAX; 1012 size_t i; 1013 for (i = start; i < num_instructions; i++) 1014 { 1015 if (m_instructions[i]->DoesBranch()) 1016 { 1017 next_branch = i; 1018 break; 1019 } 1020 } 1021 1022 // Hexagon needs the first instruction of the packet with the branch. 1023 // Go backwards until we find an instruction marked end-of-packet, or 1024 // until we hit start. 1025 if (target.GetArchitecture().GetTriple().getArch() == llvm::Triple::hexagon) 1026 { 1027 // If we didn't find a branch, find the last packet start. 1028 if (next_branch == UINT32_MAX) 1029 { 1030 i = num_instructions - 1; 1031 } 1032 1033 while (i > start) 1034 { 1035 --i; 1036 1037 Error error; 1038 uint32_t inst_bytes; 1039 bool prefer_file_cache = false; // Read from process if process is running 1040 lldb::addr_t load_addr = LLDB_INVALID_ADDRESS; 1041 target.ReadMemory(m_instructions[i]->GetAddress(), 1042 prefer_file_cache, 1043 &inst_bytes, 1044 sizeof(inst_bytes), 1045 error, 1046 &load_addr); 1047 // If we have an error reading memory, return start 1048 if (!error.Success()) 1049 return start; 1050 // check if this is the last instruction in a packet 1051 // bits 15:14 will be 11b or 00b for a duplex 1052 if (((inst_bytes & 0xC000) == 0xC000) || 1053 ((inst_bytes & 0xC000) == 0x0000)) 1054 { 1055 // instruction after this should be the start of next packet 1056 next_branch = i + 1; 1057 break; 1058 } 1059 } 1060 1061 if (next_branch == UINT32_MAX) 1062 { 1063 // We couldn't find the previous packet, so return start 1064 next_branch = start; 1065 } 1066 } 1067 return next_branch; 1068 } 1069 1070 uint32_t 1071 InstructionList::GetIndexOfInstructionAtAddress (const Address &address) 1072 { 1073 size_t num_instructions = m_instructions.size(); 1074 uint32_t index = UINT32_MAX; 1075 for (size_t i = 0; i < num_instructions; i++) 1076 { 1077 if (m_instructions[i]->GetAddress() == address) 1078 { 1079 index = i; 1080 break; 1081 } 1082 } 1083 return index; 1084 } 1085 1086 uint32_t 1087 InstructionList::GetIndexOfInstructionAtLoadAddress (lldb::addr_t load_addr, Target &target) 1088 { 1089 Address address; 1090 address.SetLoadAddress(load_addr, &target); 1091 return GetIndexOfInstructionAtAddress(address); 1092 } 1093 1094 size_t 1095 Disassembler::ParseInstructions (const ExecutionContext *exe_ctx, 1096 const AddressRange &range, 1097 Stream *error_strm_ptr, 1098 bool prefer_file_cache) 1099 { 1100 if (exe_ctx) 1101 { 1102 Target *target = exe_ctx->GetTargetPtr(); 1103 const addr_t byte_size = range.GetByteSize(); 1104 if (target == nullptr || byte_size == 0 || !range.GetBaseAddress().IsValid()) 1105 return 0; 1106 1107 DataBufferHeap *heap_buffer = new DataBufferHeap (byte_size, '\0'); 1108 DataBufferSP data_sp(heap_buffer); 1109 1110 Error error; 1111 lldb::addr_t load_addr = LLDB_INVALID_ADDRESS; 1112 const size_t bytes_read = target->ReadMemory (range.GetBaseAddress(), 1113 prefer_file_cache, 1114 heap_buffer->GetBytes(), 1115 heap_buffer->GetByteSize(), 1116 error, 1117 &load_addr); 1118 1119 if (bytes_read > 0) 1120 { 1121 if (bytes_read != heap_buffer->GetByteSize()) 1122 heap_buffer->SetByteSize (bytes_read); 1123 DataExtractor data (data_sp, 1124 m_arch.GetByteOrder(), 1125 m_arch.GetAddressByteSize()); 1126 const bool data_from_file = load_addr == LLDB_INVALID_ADDRESS; 1127 return DecodeInstructions(range.GetBaseAddress(), data, 0, UINT32_MAX, false, 1128 data_from_file); 1129 } 1130 else if (error_strm_ptr) 1131 { 1132 const char *error_cstr = error.AsCString(); 1133 if (error_cstr) 1134 { 1135 error_strm_ptr->Printf("error: %s\n", error_cstr); 1136 } 1137 } 1138 } 1139 else if (error_strm_ptr) 1140 { 1141 error_strm_ptr->PutCString("error: invalid execution context\n"); 1142 } 1143 return 0; 1144 } 1145 1146 size_t 1147 Disassembler::ParseInstructions (const ExecutionContext *exe_ctx, 1148 const Address &start, 1149 uint32_t num_instructions, 1150 bool prefer_file_cache) 1151 { 1152 m_instruction_list.Clear(); 1153 1154 if (exe_ctx == nullptr || num_instructions == 0 || !start.IsValid()) 1155 return 0; 1156 1157 Target *target = exe_ctx->GetTargetPtr(); 1158 // Calculate the max buffer size we will need in order to disassemble 1159 const addr_t byte_size = num_instructions * m_arch.GetMaximumOpcodeByteSize(); 1160 1161 if (target == nullptr || byte_size == 0) 1162 return 0; 1163 1164 DataBufferHeap *heap_buffer = new DataBufferHeap (byte_size, '\0'); 1165 DataBufferSP data_sp (heap_buffer); 1166 1167 Error error; 1168 lldb::addr_t load_addr = LLDB_INVALID_ADDRESS; 1169 const size_t bytes_read = target->ReadMemory (start, 1170 prefer_file_cache, 1171 heap_buffer->GetBytes(), 1172 byte_size, 1173 error, 1174 &load_addr); 1175 1176 const bool data_from_file = load_addr == LLDB_INVALID_ADDRESS; 1177 1178 if (bytes_read == 0) 1179 return 0; 1180 DataExtractor data (data_sp, 1181 m_arch.GetByteOrder(), 1182 m_arch.GetAddressByteSize()); 1183 1184 const bool append_instructions = true; 1185 DecodeInstructions (start, 1186 data, 1187 0, 1188 num_instructions, 1189 append_instructions, 1190 data_from_file); 1191 1192 return m_instruction_list.GetSize(); 1193 } 1194 1195 //---------------------------------------------------------------------- 1196 // Disassembler copy constructor 1197 //---------------------------------------------------------------------- 1198 Disassembler::Disassembler(const ArchSpec& arch, const char *flavor) : 1199 m_arch (arch), 1200 m_instruction_list(), 1201 m_base_addr(LLDB_INVALID_ADDRESS), 1202 m_flavor () 1203 { 1204 if (flavor == nullptr) 1205 m_flavor.assign("default"); 1206 else 1207 m_flavor.assign(flavor); 1208 1209 // If this is an arm variant that can only include thumb (T16, T32) 1210 // instructions, force the arch triple to be "thumbv.." instead of 1211 // "armv..." 1212 if (arch.IsAlwaysThumbInstructions()) 1213 { 1214 std::string thumb_arch_name (arch.GetTriple().getArchName().str()); 1215 // Replace "arm" with "thumb" so we get all thumb variants correct 1216 if (thumb_arch_name.size() > 3) 1217 { 1218 thumb_arch_name.erase(0, 3); 1219 thumb_arch_name.insert(0, "thumb"); 1220 } 1221 m_arch.SetTriple (thumb_arch_name.c_str()); 1222 } 1223 } 1224 1225 Disassembler::~Disassembler() = default; 1226 1227 InstructionList & 1228 Disassembler::GetInstructionList () 1229 { 1230 return m_instruction_list; 1231 } 1232 1233 const InstructionList & 1234 Disassembler::GetInstructionList () const 1235 { 1236 return m_instruction_list; 1237 } 1238 1239 //---------------------------------------------------------------------- 1240 // Class PseudoInstruction 1241 //---------------------------------------------------------------------- 1242 1243 PseudoInstruction::PseudoInstruction () : 1244 Instruction (Address(), eAddressClassUnknown), 1245 m_description () 1246 { 1247 } 1248 1249 PseudoInstruction::~PseudoInstruction() = default; 1250 1251 bool 1252 PseudoInstruction::DoesBranch () 1253 { 1254 // This is NOT a valid question for a pseudo instruction. 1255 return false; 1256 } 1257 1258 bool 1259 PseudoInstruction::HasDelaySlot () 1260 { 1261 // This is NOT a valid question for a pseudo instruction. 1262 return false; 1263 } 1264 1265 size_t 1266 PseudoInstruction::Decode (const lldb_private::Disassembler &disassembler, 1267 const lldb_private::DataExtractor &data, 1268 lldb::offset_t data_offset) 1269 { 1270 return m_opcode.GetByteSize(); 1271 } 1272 1273 void 1274 PseudoInstruction::SetOpcode (size_t opcode_size, void *opcode_data) 1275 { 1276 if (!opcode_data) 1277 return; 1278 1279 switch (opcode_size) 1280 { 1281 case 8: 1282 { 1283 uint8_t value8 = *((uint8_t *) opcode_data); 1284 m_opcode.SetOpcode8 (value8, eByteOrderInvalid); 1285 break; 1286 } 1287 case 16: 1288 { 1289 uint16_t value16 = *((uint16_t *) opcode_data); 1290 m_opcode.SetOpcode16 (value16, eByteOrderInvalid); 1291 break; 1292 } 1293 case 32: 1294 { 1295 uint32_t value32 = *((uint32_t *) opcode_data); 1296 m_opcode.SetOpcode32 (value32, eByteOrderInvalid); 1297 break; 1298 } 1299 case 64: 1300 { 1301 uint64_t value64 = *((uint64_t *) opcode_data); 1302 m_opcode.SetOpcode64 (value64, eByteOrderInvalid); 1303 break; 1304 } 1305 default: 1306 break; 1307 } 1308 } 1309 1310 void 1311 PseudoInstruction::SetDescription (const char *description) 1312 { 1313 if (description && strlen (description) > 0) 1314 m_description = description; 1315 } 1316