1 //===-- Disassembler.cpp ----------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/lldb-python.h"
12 #include "lldb/Core/Disassembler.h"
16 // Other libraries and framework includes
18 #include "lldb/lldb-private.h"
19 #include "lldb/Core/Error.h"
20 #include "lldb/Core/DataBufferHeap.h"
21 #include "lldb/Core/DataExtractor.h"
22 #include "lldb/Core/Debugger.h"
23 #include "lldb/Core/EmulateInstruction.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/Interpreter/OptionValue.h"
29 #include "lldb/Interpreter/OptionValueArray.h"
30 #include "lldb/Interpreter/OptionValueDictionary.h"
31 #include "lldb/Interpreter/OptionValueString.h"
32 #include "lldb/Interpreter/OptionValueUInt64.h"
33 #include "lldb/Symbol/ClangNamespaceDecl.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"
42 #define DEFAULT_DISASM_BYTE_SIZE 32
45 using namespace lldb_private;
49 Disassembler::FindPlugin (const ArchSpec &arch, const char *flavor, const char *plugin_name)
51 Timer scoped_timer (__PRETTY_FUNCTION__,
52 "Disassembler::FindPlugin (arch = %s, plugin_name = %s)",
53 arch.GetArchitectureName(),
56 DisassemblerCreateInstance create_callback = NULL;
60 ConstString const_plugin_name (plugin_name);
61 create_callback = PluginManager::GetDisassemblerCreateCallbackForPluginName (const_plugin_name);
64 DisassemblerSP disassembler_sp(create_callback(arch, flavor));
66 if (disassembler_sp.get())
67 return disassembler_sp;
72 for (uint32_t idx = 0; (create_callback = PluginManager::GetDisassemblerCreateCallbackAtIndex(idx)) != NULL; ++idx)
74 DisassemblerSP disassembler_sp(create_callback(arch, flavor));
76 if (disassembler_sp.get())
77 return disassembler_sp;
80 return DisassemblerSP();
84 Disassembler::FindPluginForTarget(const TargetSP target_sp, const ArchSpec &arch, const char *flavor, const char *plugin_name)
86 if (target_sp && flavor == NULL)
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();
94 return FindPlugin(arch, flavor, plugin_name);
99 ResolveAddress (const ExecutionContext &exe_ctx,
101 Address &resolved_addr)
103 if (!addr.IsSectionOffset())
105 // If we weren't passed in a section offset address range,
106 // try and resolve it to something
107 Target *target = exe_ctx.GetTargetPtr();
110 if (target->GetSectionLoadList().IsEmpty())
112 target->GetImages().ResolveFileAddress (addr.GetOffset(), resolved_addr);
116 target->GetSectionLoadList().ResolveLoadAddress (addr.GetOffset(), resolved_addr);
118 // We weren't able to resolve the address, just treat it as a
120 if (resolved_addr.IsValid())
124 resolved_addr = addr;
128 Disassembler::Disassemble
131 const ArchSpec &arch,
132 const char *plugin_name,
134 const ExecutionContext &exe_ctx,
135 SymbolContextList &sc_list,
136 uint32_t num_instructions,
137 uint32_t num_mixed_context_lines,
142 size_t success_count = 0;
143 const size_t count = sc_list.GetSize();
146 const uint32_t scope = eSymbolContextBlock | eSymbolContextFunction | eSymbolContextSymbol;
147 const bool use_inline_block_range = true;
148 for (size_t i=0; i<count; ++i)
150 if (sc_list.GetContextAtIndex(i, sc) == false)
152 for (uint32_t range_idx = 0; sc.GetAddressRange(scope, range_idx, use_inline_block_range, range); ++range_idx)
154 if (Disassemble (debugger,
161 num_mixed_context_lines,
170 return success_count;
174 Disassembler::Disassemble
177 const ArchSpec &arch,
178 const char *plugin_name,
180 const ExecutionContext &exe_ctx,
181 const ConstString &name,
183 uint32_t num_instructions,
184 uint32_t num_mixed_context_lines,
189 SymbolContextList sc_list;
192 const bool include_symbols = true;
193 const bool include_inlines = true;
196 module->FindFunctions (name,
198 eFunctionNameTypeAuto,
204 else if (exe_ctx.GetTargetPtr())
206 exe_ctx.GetTargetPtr()->GetImages().FindFunctions (name,
207 eFunctionNameTypeAuto,
215 if (sc_list.GetSize ())
217 return Disassemble (debugger,
224 num_mixed_context_lines,
233 Disassembler::DisassembleRange
235 const ArchSpec &arch,
236 const char *plugin_name,
238 const ExecutionContext &exe_ctx,
239 const AddressRange &range,
240 bool prefer_file_cache
243 lldb::DisassemblerSP disasm_sp;
244 if (range.GetByteSize() > 0 && range.GetBaseAddress().IsValid())
246 disasm_sp = Disassembler::FindPluginForTarget(exe_ctx.GetTargetSP(), arch, flavor, plugin_name);
250 size_t bytes_disassembled = disasm_sp->ParseInstructions (&exe_ctx, range, NULL, prefer_file_cache);
251 if (bytes_disassembled == 0)
259 Disassembler::DisassembleBytes (const ArchSpec &arch,
260 const char *plugin_name,
262 const Address &start,
265 uint32_t num_instructions,
268 lldb::DisassemblerSP disasm_sp;
272 disasm_sp = Disassembler::FindPlugin(arch, flavor, plugin_name);
276 DataExtractor data(src, src_len, arch.GetByteOrder(), arch.GetAddressByteSize());
278 (void)disasm_sp->DecodeInstructions (start,
292 Disassembler::Disassemble
295 const ArchSpec &arch,
296 const char *plugin_name,
298 const ExecutionContext &exe_ctx,
299 const AddressRange &disasm_range,
300 uint32_t num_instructions,
301 uint32_t num_mixed_context_lines,
306 if (disasm_range.GetByteSize())
308 lldb::DisassemblerSP disasm_sp (Disassembler::FindPluginForTarget(exe_ctx.GetTargetSP(), arch, flavor, plugin_name));
313 ResolveAddress (exe_ctx, disasm_range.GetBaseAddress(), range.GetBaseAddress());
314 range.SetByteSize (disasm_range.GetByteSize());
315 const bool prefer_file_cache = false;
316 size_t bytes_disassembled = disasm_sp->ParseInstructions (&exe_ctx, range, &strm, prefer_file_cache);
317 if (bytes_disassembled == 0)
320 bool result = PrintInstructions (disasm_sp.get(),
325 num_mixed_context_lines,
329 // FIXME: The DisassemblerLLVMC has a reference cycle and won't go away if it has any active instructions.
330 // I'll fix that but for now, just clear the list and it will go away nicely.
331 disasm_sp->GetInstructionList().Clear();
339 Disassembler::Disassemble
342 const ArchSpec &arch,
343 const char *plugin_name,
345 const ExecutionContext &exe_ctx,
346 const Address &start_address,
347 uint32_t num_instructions,
348 uint32_t num_mixed_context_lines,
353 if (num_instructions > 0)
355 lldb::DisassemblerSP disasm_sp (Disassembler::FindPluginForTarget(exe_ctx.GetTargetSP(),
362 ResolveAddress (exe_ctx, start_address, addr);
363 const bool prefer_file_cache = false;
364 size_t bytes_disassembled = disasm_sp->ParseInstructions (&exe_ctx,
368 if (bytes_disassembled == 0)
370 bool result = PrintInstructions (disasm_sp.get(),
375 num_mixed_context_lines,
379 // FIXME: The DisassemblerLLVMC has a reference cycle and won't go away if it has any active instructions.
380 // I'll fix that but for now, just clear the list and it will go away nicely.
381 disasm_sp->GetInstructionList().Clear();
389 Disassembler::PrintInstructions
391 Disassembler *disasm_ptr,
393 const ArchSpec &arch,
394 const ExecutionContext &exe_ctx,
395 uint32_t num_instructions,
396 uint32_t num_mixed_context_lines,
401 // We got some things disassembled...
402 size_t num_instructions_found = disasm_ptr->GetInstructionList().GetSize();
404 if (num_instructions > 0 && num_instructions < num_instructions_found)
405 num_instructions_found = num_instructions;
407 const uint32_t max_opcode_byte_size = disasm_ptr->GetInstructionList().GetMaxOpcocdeByteSize ();
410 SymbolContext prev_sc;
411 AddressRange sc_range;
412 const Address *pc_addr_ptr = NULL;
413 ExecutionContextScope *exe_scope = exe_ctx.GetBestExecutionContextScope();
414 StackFrame *frame = exe_ctx.GetFramePtr();
416 TargetSP target_sp (exe_ctx.GetTargetSP());
417 SourceManager &source_manager = target_sp ? target_sp->GetSourceManager() : debugger.GetSourceManager();
420 pc_addr_ptr = &frame->GetFrameCodeAddress();
421 const uint32_t scope = eSymbolContextLineEntry | eSymbolContextFunction | eSymbolContextSymbol;
422 const bool use_inline_block_range = false;
423 for (size_t i=0; i<num_instructions_found; ++i)
425 Instruction *inst = disasm_ptr->GetInstructionList().GetInstructionAtIndex (i).get();
428 const Address &addr = inst->GetAddress();
429 const bool inst_is_at_pc = pc_addr_ptr && addr == *pc_addr_ptr;
433 ModuleSP module_sp (addr.GetModule());
436 uint32_t resolved_mask = module_sp->ResolveSymbolContextForAddress(addr, eSymbolContextEverything, sc);
439 if (num_mixed_context_lines)
441 if (!sc_range.ContainsFileAddress (addr))
443 sc.GetAddressRange (scope, 0, use_inline_block_range, sc_range);
450 sc.DumpStopContext(&strm, exe_ctx.GetProcessPtr(), addr, false, true, false);
453 if (sc.comp_unit && sc.line_entry.IsValid())
455 source_manager.DisplaySourceLinesWithLineNumbers (sc.line_entry.file,
457 num_mixed_context_lines,
458 num_mixed_context_lines,
459 ((inst_is_at_pc && (options & eOptionMarkPCSourceLine)) ? "->" : ""),
465 else if ((sc.function || sc.symbol) && (sc.function != prev_sc.function || sc.symbol != prev_sc.symbol))
467 if (prev_sc.function || prev_sc.symbol)
470 bool show_fullpaths = false;
471 bool show_module = true;
472 bool show_inlined_frames = true;
473 sc.DumpStopContext (&strm,
478 show_inlined_frames);
489 if ((options & eOptionMarkPCAddress) && pc_addr_ptr)
491 strm.PutCString(inst_is_at_pc ? "-> " : " ");
493 const bool show_bytes = (options & eOptionShowBytes) != 0;
494 inst->Dump(&strm, max_opcode_byte_size, true, show_bytes, &exe_ctx);
508 Disassembler::Disassemble
511 const ArchSpec &arch,
512 const char *plugin_name,
514 const ExecutionContext &exe_ctx,
515 uint32_t num_instructions,
516 uint32_t num_mixed_context_lines,
522 StackFrame *frame = exe_ctx.GetFramePtr();
525 SymbolContext sc(frame->GetSymbolContext(eSymbolContextFunction | eSymbolContextSymbol));
528 range = sc.function->GetAddressRange();
530 else if (sc.symbol && sc.symbol->ValueIsAddress())
532 range.GetBaseAddress() = sc.symbol->GetAddress();
533 range.SetByteSize (sc.symbol->GetByteSize());
537 range.GetBaseAddress() = frame->GetFrameCodeAddress();
540 if (range.GetBaseAddress().IsValid() && range.GetByteSize() == 0)
541 range.SetByteSize (DEFAULT_DISASM_BYTE_SIZE);
544 return Disassemble (debugger,
551 num_mixed_context_lines,
556 Instruction::Instruction(const Address &address, AddressClass addr_class) :
558 m_address_class (addr_class),
560 m_calculated_strings(false)
564 Instruction::~Instruction()
569 Instruction::GetAddressClass ()
571 if (m_address_class == eAddressClassInvalid)
572 m_address_class = m_address.GetAddressClass();
573 return m_address_class;
577 Instruction::Dump (lldb_private::Stream *s,
578 uint32_t max_opcode_byte_size,
581 const ExecutionContext* exe_ctx)
583 size_t opcode_column_width = 7;
584 const size_t operand_column_width = 25;
586 CalculateMnemonicOperandsAndCommentIfNeeded (exe_ctx);
593 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL,
594 Address::DumpStyleLoadAddress,
595 Address::DumpStyleModuleWithFileAddress,
603 if (m_opcode.GetType() == Opcode::eTypeBytes)
605 // x86_64 and i386 are the only ones that use bytes right now so
606 // pad out the byte dump to be able to always show 15 bytes (3 chars each)
608 if (max_opcode_byte_size > 0)
609 m_opcode.Dump (&ss, max_opcode_byte_size * 3 + 1);
611 m_opcode.Dump (&ss, 15 * 3 + 1);
615 // Else, we have ARM or MIPS which can show up to a uint32_t
616 // 0x00000000 (10 spaces) plus two for padding...
617 if (max_opcode_byte_size > 0)
618 m_opcode.Dump (&ss, max_opcode_byte_size * 3 + 1);
620 m_opcode.Dump (&ss, 12);
624 const size_t opcode_pos = ss.GetSize();
626 // The default opcode size of 7 characters is plenty for most architectures
627 // but some like arm can pull out the occasional vqrshrun.s16. We won't get
628 // consistent column spacing in these cases, unfortunately.
629 if (m_opcode_name.length() >= opcode_column_width)
631 opcode_column_width = m_opcode_name.length() + 1;
634 ss.PutCString (m_opcode_name.c_str());
635 ss.FillLastLineToColumn (opcode_pos + opcode_column_width, ' ');
636 ss.PutCString (m_mnemonics.c_str());
638 if (!m_comment.empty())
640 ss.FillLastLineToColumn (opcode_pos + opcode_column_width + operand_column_width, ' ');
641 ss.PutCString (" ; ");
642 ss.PutCString (m_comment.c_str());
644 s->Write (ss.GetData(), ss.GetSize());
648 Instruction::DumpEmulation (const ArchSpec &arch)
650 std::unique_ptr<EmulateInstruction> insn_emulator_ap (EmulateInstruction::FindPlugin (arch, eInstructionTypeAny, NULL));
651 if (insn_emulator_ap.get())
653 insn_emulator_ap->SetInstruction (GetOpcode(), GetAddress(), NULL);
654 return insn_emulator_ap->EvaluateInstruction (0);
661 Instruction::ReadArray (FILE *in_file, Stream *out_stream, OptionValue::Type data_type)
666 OptionValueSP option_value_sp (new OptionValueArray (1u << data_type));
671 if (!fgets (buffer, 1023, in_file))
673 out_stream->Printf ("Instruction::ReadArray: Error reading file (fgets).\n");
674 option_value_sp.reset ();
675 return option_value_sp;
678 std::string line (buffer);
680 size_t len = line.size();
681 if (line[len-1] == '\n')
687 if ((line.size() == 1) && line[0] == ']')
696 static RegularExpression g_reg_exp ("^[ \t]*([^ \t]+)[ \t]*$");
697 RegularExpression::Match regex_match(1);
698 bool reg_exp_success = g_reg_exp.Execute (line.c_str(), ®ex_match);
700 regex_match.GetMatchAtIndex (line.c_str(), 1, value);
704 OptionValueSP data_value_sp;
707 case OptionValue::eTypeUInt64:
708 data_value_sp.reset (new OptionValueUInt64 (0, 0));
709 data_value_sp->SetValueFromCString (value.c_str());
711 // Other types can be added later as needed.
713 data_value_sp.reset (new OptionValueString (value.c_str(), ""));
717 option_value_sp->GetAsArray()->InsertValue (idx, data_value_sp);
722 return option_value_sp;
726 Instruction::ReadDictionary (FILE *in_file, Stream *out_stream)
731 OptionValueSP option_value_sp (new OptionValueDictionary());
732 static ConstString encoding_key ("data_encoding");
733 OptionValue::Type data_type = OptionValue::eTypeInvalid;
738 // Read the next line in the file
739 if (!fgets (buffer, 1023, in_file))
741 out_stream->Printf ("Instruction::ReadDictionary: Error reading file (fgets).\n");
742 option_value_sp.reset ();
743 return option_value_sp;
746 // Check to see if the line contains the end-of-dictionary marker ("}")
747 std::string line (buffer);
749 size_t len = line.size();
750 if (line[len-1] == '\n')
756 if ((line.size() == 1) && (line[0] == '}'))
762 // Try to find a key-value pair in the current line and add it to the dictionary.
765 static RegularExpression g_reg_exp ("^[ \t]*([a-zA-Z_][a-zA-Z0-9_]*)[ \t]*=[ \t]*(.*)[ \t]*$");
766 RegularExpression::Match regex_match(2);
768 bool reg_exp_success = g_reg_exp.Execute (line.c_str(), ®ex_match);
773 regex_match.GetMatchAtIndex (line.c_str(), 1, key);
774 regex_match.GetMatchAtIndex (line.c_str(), 2, value);
778 out_stream->Printf ("Instruction::ReadDictionary: Failure executing regular expression.\n");
779 option_value_sp.reset();
780 return option_value_sp;
783 ConstString const_key (key.c_str());
784 // Check value to see if it's the start of an array or dictionary.
786 lldb::OptionValueSP value_sp;
787 assert (value.empty() == false);
788 assert (key.empty() == false);
792 assert (value.size() == 1);
793 // value is a dictionary
794 value_sp = ReadDictionary (in_file, out_stream);
795 if (value_sp.get() == NULL)
797 option_value_sp.reset ();
798 return option_value_sp;
801 else if (value[0] == '[')
803 assert (value.size() == 1);
805 value_sp = ReadArray (in_file, out_stream, data_type);
806 if (value_sp.get() == NULL)
808 option_value_sp.reset ();
809 return option_value_sp;
811 // We've used the data_type to read an array; re-set the type to Invalid
812 data_type = OptionValue::eTypeInvalid;
814 else if ((value[0] == '0') && (value[1] == 'x'))
816 value_sp.reset (new OptionValueUInt64 (0, 0));
817 value_sp->SetValueFromCString (value.c_str());
821 size_t len = value.size();
822 if ((value[0] == '"') && (value[len-1] == '"'))
823 value = value.substr (1, len-2);
824 value_sp.reset (new OptionValueString (value.c_str(), ""));
829 if (const_key == encoding_key)
831 // A 'data_encoding=..." is NOT a normal key-value pair; it is meta-data indicating the
832 // data type of an upcoming array (usually the next bit of data to be read in).
833 if (strcmp (value.c_str(), "uint32_t") == 0)
834 data_type = OptionValue::eTypeUInt64;
837 option_value_sp->GetAsDictionary()->SetValueForKey (const_key, value_sp, false);
841 return option_value_sp;
845 Instruction::TestEmulation (Stream *out_stream, const char *file_name)
852 out_stream->Printf ("Instruction::TestEmulation: Missing file_name.");
856 FILE *test_file = fopen (file_name, "r");
859 out_stream->Printf ("Instruction::TestEmulation: Attempt to open test file failed.");
864 if (!fgets (buffer, 255, test_file))
866 out_stream->Printf ("Instruction::TestEmulation: Error reading first line of test file.\n");
871 if (strncmp (buffer, "InstructionEmulationState={", 27) != 0)
873 out_stream->Printf ("Instructin::TestEmulation: Test file does not contain emulation state dictionary\n");
878 // Read all the test information from the test file into an OptionValueDictionary.
880 OptionValueSP data_dictionary_sp (ReadDictionary (test_file, out_stream));
881 if (data_dictionary_sp.get() == NULL)
883 out_stream->Printf ("Instruction::TestEmulation: Error reading Dictionary Object.\n");
890 OptionValueDictionary *data_dictionary = data_dictionary_sp->GetAsDictionary();
891 static ConstString description_key ("assembly_string");
892 static ConstString triple_key ("triple");
894 OptionValueSP value_sp = data_dictionary->GetValueForKey (description_key);
896 if (value_sp.get() == NULL)
898 out_stream->Printf ("Instruction::TestEmulation: Test file does not contain description string.\n");
902 SetDescription (value_sp->GetStringValue());
905 value_sp = data_dictionary->GetValueForKey (triple_key);
906 if (value_sp.get() == NULL)
908 out_stream->Printf ("Instruction::TestEmulation: Test file does not contain triple.\n");
913 arch.SetTriple (llvm::Triple (value_sp->GetStringValue()));
915 bool success = false;
916 std::unique_ptr<EmulateInstruction> insn_emulator_ap (EmulateInstruction::FindPlugin (arch, eInstructionTypeAny, NULL));
917 if (insn_emulator_ap.get())
918 success = insn_emulator_ap->TestEmulation (out_stream, arch, data_dictionary);
921 out_stream->Printf ("Emulation test succeeded.");
923 out_stream->Printf ("Emulation test failed.");
929 Instruction::Emulate (const ArchSpec &arch,
930 uint32_t evaluate_options,
932 EmulateInstruction::ReadMemoryCallback read_mem_callback,
933 EmulateInstruction::WriteMemoryCallback write_mem_callback,
934 EmulateInstruction::ReadRegisterCallback read_reg_callback,
935 EmulateInstruction::WriteRegisterCallback write_reg_callback)
937 std::unique_ptr<EmulateInstruction> insn_emulator_ap (EmulateInstruction::FindPlugin (arch, eInstructionTypeAny, NULL));
938 if (insn_emulator_ap.get())
940 insn_emulator_ap->SetBaton (baton);
941 insn_emulator_ap->SetCallbacks (read_mem_callback, write_mem_callback, read_reg_callback, write_reg_callback);
942 insn_emulator_ap->SetInstruction (GetOpcode(), GetAddress(), NULL);
943 return insn_emulator_ap->EvaluateInstruction (evaluate_options);
951 Instruction::GetData (DataExtractor &data)
953 return m_opcode.GetData(data);
956 InstructionList::InstructionList() :
961 InstructionList::~InstructionList()
966 InstructionList::GetSize() const
968 return m_instructions.size();
972 InstructionList::GetMaxOpcocdeByteSize () const
974 uint32_t max_inst_size = 0;
975 collection::const_iterator pos, end;
976 for (pos = m_instructions.begin(), end = m_instructions.end();
980 uint32_t inst_size = (*pos)->GetOpcode().GetByteSize();
981 if (max_inst_size < inst_size)
982 max_inst_size = inst_size;
984 return max_inst_size;
990 InstructionList::GetInstructionAtIndex (size_t idx) const
992 InstructionSP inst_sp;
993 if (idx < m_instructions.size())
994 inst_sp = m_instructions[idx];
999 InstructionList::Dump (Stream *s,
1002 const ExecutionContext* exe_ctx)
1004 const uint32_t max_opcode_byte_size = GetMaxOpcocdeByteSize();
1005 collection::const_iterator pos, begin, end;
1006 for (begin = m_instructions.begin(), end = m_instructions.end(), pos = begin;
1012 (*pos)->Dump(s, max_opcode_byte_size, show_address, show_bytes, exe_ctx);
1018 InstructionList::Clear()
1020 m_instructions.clear();
1024 InstructionList::Append (lldb::InstructionSP &inst_sp)
1027 m_instructions.push_back(inst_sp);
1031 InstructionList::GetIndexOfNextBranchInstruction(uint32_t start) const
1033 size_t num_instructions = m_instructions.size();
1035 uint32_t next_branch = UINT32_MAX;
1036 for (size_t i = start; i < num_instructions; i++)
1038 if (m_instructions[i]->DoesBranch())
1048 InstructionList::GetIndexOfInstructionAtAddress (const Address &address)
1050 size_t num_instructions = m_instructions.size();
1051 uint32_t index = UINT32_MAX;
1052 for (size_t i = 0; i < num_instructions; i++)
1054 if (m_instructions[i]->GetAddress() == address)
1065 InstructionList::GetIndexOfInstructionAtLoadAddress (lldb::addr_t load_addr, Target &target)
1068 address.SetLoadAddress(load_addr, &target);
1069 return GetIndexOfInstructionAtAddress(address);
1073 Disassembler::ParseInstructions (const ExecutionContext *exe_ctx,
1074 const AddressRange &range,
1075 Stream *error_strm_ptr,
1076 bool prefer_file_cache)
1080 Target *target = exe_ctx->GetTargetPtr();
1081 const addr_t byte_size = range.GetByteSize();
1082 if (target == NULL || byte_size == 0 || !range.GetBaseAddress().IsValid())
1085 DataBufferHeap *heap_buffer = new DataBufferHeap (byte_size, '\0');
1086 DataBufferSP data_sp(heap_buffer);
1089 lldb::addr_t load_addr = LLDB_INVALID_ADDRESS;
1090 const size_t bytes_read = target->ReadMemory (range.GetBaseAddress(),
1092 heap_buffer->GetBytes(),
1093 heap_buffer->GetByteSize(),
1099 if (bytes_read != heap_buffer->GetByteSize())
1100 heap_buffer->SetByteSize (bytes_read);
1101 DataExtractor data (data_sp,
1102 m_arch.GetByteOrder(),
1103 m_arch.GetAddressByteSize());
1104 const bool data_from_file = load_addr == LLDB_INVALID_ADDRESS;
1105 return DecodeInstructions (range.GetBaseAddress(), data, 0, UINT32_MAX, false, data_from_file);
1107 else if (error_strm_ptr)
1109 const char *error_cstr = error.AsCString();
1112 error_strm_ptr->Printf("error: %s\n", error_cstr);
1116 else if (error_strm_ptr)
1118 error_strm_ptr->PutCString("error: invalid execution context\n");
1124 Disassembler::ParseInstructions (const ExecutionContext *exe_ctx,
1125 const Address &start,
1126 uint32_t num_instructions,
1127 bool prefer_file_cache)
1129 m_instruction_list.Clear();
1131 if (exe_ctx == NULL || num_instructions == 0 || !start.IsValid())
1134 Target *target = exe_ctx->GetTargetPtr();
1135 // Calculate the max buffer size we will need in order to disassemble
1136 const addr_t byte_size = num_instructions * m_arch.GetMaximumOpcodeByteSize();
1138 if (target == NULL || byte_size == 0)
1141 DataBufferHeap *heap_buffer = new DataBufferHeap (byte_size, '\0');
1142 DataBufferSP data_sp (heap_buffer);
1145 lldb::addr_t load_addr = LLDB_INVALID_ADDRESS;
1146 const size_t bytes_read = target->ReadMemory (start,
1148 heap_buffer->GetBytes(),
1153 const bool data_from_file = load_addr == LLDB_INVALID_ADDRESS;
1155 if (bytes_read == 0)
1157 DataExtractor data (data_sp,
1158 m_arch.GetByteOrder(),
1159 m_arch.GetAddressByteSize());
1161 const bool append_instructions = true;
1162 DecodeInstructions (start,
1166 append_instructions,
1169 return m_instruction_list.GetSize();
1172 //----------------------------------------------------------------------
1173 // Disassembler copy constructor
1174 //----------------------------------------------------------------------
1175 Disassembler::Disassembler(const ArchSpec& arch, const char *flavor) :
1177 m_instruction_list(),
1178 m_base_addr(LLDB_INVALID_ADDRESS),
1182 m_flavor.assign("default");
1184 m_flavor.assign(flavor);
1187 //----------------------------------------------------------------------
1189 //----------------------------------------------------------------------
1190 Disassembler::~Disassembler()
1195 Disassembler::GetInstructionList ()
1197 return m_instruction_list;
1200 const InstructionList &
1201 Disassembler::GetInstructionList () const
1203 return m_instruction_list;
1206 //----------------------------------------------------------------------
1207 // Class PseudoInstruction
1208 //----------------------------------------------------------------------
1209 PseudoInstruction::PseudoInstruction () :
1210 Instruction (Address(), eAddressClassUnknown),
1215 PseudoInstruction::~PseudoInstruction ()
1220 PseudoInstruction::DoesBranch ()
1222 // This is NOT a valid question for a pseudo instruction.
1227 PseudoInstruction::Decode (const lldb_private::Disassembler &disassembler,
1228 const lldb_private::DataExtractor &data,
1229 lldb::offset_t data_offset)
1231 return m_opcode.GetByteSize();
1236 PseudoInstruction::SetOpcode (size_t opcode_size, void *opcode_data)
1241 switch (opcode_size)
1245 uint8_t value8 = *((uint8_t *) opcode_data);
1246 m_opcode.SetOpcode8 (value8, eByteOrderInvalid);
1251 uint16_t value16 = *((uint16_t *) opcode_data);
1252 m_opcode.SetOpcode16 (value16, eByteOrderInvalid);
1257 uint32_t value32 = *((uint32_t *) opcode_data);
1258 m_opcode.SetOpcode32 (value32, eByteOrderInvalid);
1263 uint64_t value64 = *((uint64_t *) opcode_data);
1264 m_opcode.SetOpcode64 (value64, eByteOrderInvalid);
1273 PseudoInstruction::SetDescription (const char *description)
1275 if (description && strlen (description) > 0)
1276 m_description = description;