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/StackFrame.h"
39 #include "lldb/Target/Target.h"
41 #define DEFAULT_DISASM_BYTE_SIZE 32
44 using namespace lldb_private;
48 Disassembler::FindPlugin (const ArchSpec &arch, const char *flavor, const char *plugin_name)
50 Timer scoped_timer (__PRETTY_FUNCTION__,
51 "Disassembler::FindPlugin (arch = %s, plugin_name = %s)",
52 arch.GetArchitectureName(),
55 DisassemblerCreateInstance create_callback = NULL;
59 ConstString const_plugin_name (plugin_name);
60 create_callback = PluginManager::GetDisassemblerCreateCallbackForPluginName (const_plugin_name);
63 DisassemblerSP disassembler_sp(create_callback(arch, flavor));
65 if (disassembler_sp.get())
66 return disassembler_sp;
71 for (uint32_t idx = 0; (create_callback = PluginManager::GetDisassemblerCreateCallbackAtIndex(idx)) != NULL; ++idx)
73 DisassemblerSP disassembler_sp(create_callback(arch, flavor));
75 if (disassembler_sp.get())
76 return disassembler_sp;
79 return DisassemblerSP();
83 Disassembler::FindPluginForTarget(const TargetSP target_sp, const ArchSpec &arch, const char *flavor, const char *plugin_name)
85 if (target_sp && flavor == NULL)
87 // FIXME - we don't have the mechanism in place to do per-architecture settings. But since we know that for now
88 // we only support flavors on x86 & x86_64,
89 if (arch.GetTriple().getArch() == llvm::Triple::x86
90 || arch.GetTriple().getArch() == llvm::Triple::x86_64)
91 flavor = target_sp->GetDisassemblyFlavor();
93 return FindPlugin(arch, flavor, plugin_name);
98 ResolveAddress (const ExecutionContext &exe_ctx,
100 Address &resolved_addr)
102 if (!addr.IsSectionOffset())
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();
109 if (target->GetSectionLoadList().IsEmpty())
111 target->GetImages().ResolveFileAddress (addr.GetOffset(), resolved_addr);
115 target->GetSectionLoadList().ResolveLoadAddress (addr.GetOffset(), resolved_addr);
117 // We weren't able to resolve the address, just treat it as a
119 if (resolved_addr.IsValid())
123 resolved_addr = addr;
127 Disassembler::Disassemble
130 const ArchSpec &arch,
131 const char *plugin_name,
133 const ExecutionContext &exe_ctx,
134 SymbolContextList &sc_list,
135 uint32_t num_instructions,
136 uint32_t num_mixed_context_lines,
141 size_t success_count = 0;
142 const size_t count = sc_list.GetSize();
145 const uint32_t scope = eSymbolContextBlock | eSymbolContextFunction | eSymbolContextSymbol;
146 const bool use_inline_block_range = true;
147 for (size_t i=0; i<count; ++i)
149 if (sc_list.GetContextAtIndex(i, sc) == false)
151 for (uint32_t range_idx = 0; sc.GetAddressRange(scope, range_idx, use_inline_block_range, range); ++range_idx)
153 if (Disassemble (debugger,
160 num_mixed_context_lines,
169 return success_count;
173 Disassembler::Disassemble
176 const ArchSpec &arch,
177 const char *plugin_name,
179 const ExecutionContext &exe_ctx,
180 const ConstString &name,
182 uint32_t num_instructions,
183 uint32_t num_mixed_context_lines,
188 SymbolContextList sc_list;
191 const bool include_symbols = true;
192 const bool include_inlines = true;
195 module->FindFunctions (name,
197 eFunctionNameTypeAuto,
203 else if (exe_ctx.GetTargetPtr())
205 exe_ctx.GetTargetPtr()->GetImages().FindFunctions (name,
206 eFunctionNameTypeAuto,
214 if (sc_list.GetSize ())
216 return Disassemble (debugger,
223 num_mixed_context_lines,
232 Disassembler::DisassembleRange
234 const ArchSpec &arch,
235 const char *plugin_name,
237 const ExecutionContext &exe_ctx,
238 const AddressRange &range
241 lldb::DisassemblerSP disasm_sp;
242 if (range.GetByteSize() > 0 && range.GetBaseAddress().IsValid())
244 disasm_sp = Disassembler::FindPluginForTarget(exe_ctx.GetTargetSP(), arch, flavor, plugin_name);
248 const bool prefer_file_cache = false;
249 size_t bytes_disassembled = disasm_sp->ParseInstructions (&exe_ctx, range, NULL, prefer_file_cache);
250 if (bytes_disassembled == 0)
258 Disassembler::DisassembleBytes (const ArchSpec &arch,
259 const char *plugin_name,
261 const Address &start,
264 uint32_t num_instructions,
267 lldb::DisassemblerSP disasm_sp;
271 disasm_sp = Disassembler::FindPlugin(arch, flavor, plugin_name);
275 DataExtractor data(src, src_len, arch.GetByteOrder(), arch.GetAddressByteSize());
277 (void)disasm_sp->DecodeInstructions (start,
291 Disassembler::Disassemble
294 const ArchSpec &arch,
295 const char *plugin_name,
297 const ExecutionContext &exe_ctx,
298 const AddressRange &disasm_range,
299 uint32_t num_instructions,
300 uint32_t num_mixed_context_lines,
305 if (disasm_range.GetByteSize())
307 lldb::DisassemblerSP disasm_sp (Disassembler::FindPluginForTarget(exe_ctx.GetTargetSP(), arch, flavor, plugin_name));
312 ResolveAddress (exe_ctx, disasm_range.GetBaseAddress(), range.GetBaseAddress());
313 range.SetByteSize (disasm_range.GetByteSize());
314 const bool prefer_file_cache = false;
315 size_t bytes_disassembled = disasm_sp->ParseInstructions (&exe_ctx, range, &strm, prefer_file_cache);
316 if (bytes_disassembled == 0)
319 bool result = PrintInstructions (disasm_sp.get(),
324 num_mixed_context_lines,
328 // FIXME: The DisassemblerLLVMC has a reference cycle and won't go away if it has any active instructions.
329 // I'll fix that but for now, just clear the list and it will go away nicely.
330 disasm_sp->GetInstructionList().Clear();
338 Disassembler::Disassemble
341 const ArchSpec &arch,
342 const char *plugin_name,
344 const ExecutionContext &exe_ctx,
345 const Address &start_address,
346 uint32_t num_instructions,
347 uint32_t num_mixed_context_lines,
352 if (num_instructions > 0)
354 lldb::DisassemblerSP disasm_sp (Disassembler::FindPluginForTarget(exe_ctx.GetTargetSP(),
361 ResolveAddress (exe_ctx, start_address, addr);
362 const bool prefer_file_cache = false;
363 size_t bytes_disassembled = disasm_sp->ParseInstructions (&exe_ctx,
367 if (bytes_disassembled == 0)
369 bool result = PrintInstructions (disasm_sp.get(),
374 num_mixed_context_lines,
378 // FIXME: The DisassemblerLLVMC has a reference cycle and won't go away if it has any active instructions.
379 // I'll fix that but for now, just clear the list and it will go away nicely.
380 disasm_sp->GetInstructionList().Clear();
388 Disassembler::PrintInstructions
390 Disassembler *disasm_ptr,
392 const ArchSpec &arch,
393 const ExecutionContext &exe_ctx,
394 uint32_t num_instructions,
395 uint32_t num_mixed_context_lines,
400 // We got some things disassembled...
401 size_t num_instructions_found = disasm_ptr->GetInstructionList().GetSize();
403 if (num_instructions > 0 && num_instructions < num_instructions_found)
404 num_instructions_found = num_instructions;
406 const uint32_t max_opcode_byte_size = disasm_ptr->GetInstructionList().GetMaxOpcocdeByteSize ();
409 SymbolContext prev_sc;
410 AddressRange sc_range;
411 const Address *pc_addr_ptr = NULL;
412 ExecutionContextScope *exe_scope = exe_ctx.GetBestExecutionContextScope();
413 StackFrame *frame = exe_ctx.GetFramePtr();
415 TargetSP target_sp (exe_ctx.GetTargetSP());
416 SourceManager &source_manager = target_sp ? target_sp->GetSourceManager() : debugger.GetSourceManager();
419 pc_addr_ptr = &frame->GetFrameCodeAddress();
420 const uint32_t scope = eSymbolContextLineEntry | eSymbolContextFunction | eSymbolContextSymbol;
421 const bool use_inline_block_range = false;
422 for (size_t i=0; i<num_instructions_found; ++i)
424 Instruction *inst = disasm_ptr->GetInstructionList().GetInstructionAtIndex (i).get();
427 const Address &addr = inst->GetAddress();
428 const bool inst_is_at_pc = pc_addr_ptr && addr == *pc_addr_ptr;
432 ModuleSP module_sp (addr.GetModule());
435 uint32_t resolved_mask = module_sp->ResolveSymbolContextForAddress(addr, eSymbolContextEverything, sc);
438 if (num_mixed_context_lines)
440 if (!sc_range.ContainsFileAddress (addr))
442 sc.GetAddressRange (scope, 0, use_inline_block_range, sc_range);
449 sc.DumpStopContext(&strm, exe_ctx.GetProcessPtr(), addr, false, true, false);
452 if (sc.comp_unit && sc.line_entry.IsValid())
454 source_manager.DisplaySourceLinesWithLineNumbers (sc.line_entry.file,
456 num_mixed_context_lines,
457 num_mixed_context_lines,
458 ((inst_is_at_pc && (options & eOptionMarkPCSourceLine)) ? "->" : ""),
464 else if ((sc.function || sc.symbol) && (sc.function != prev_sc.function || sc.symbol != prev_sc.symbol))
466 if (prev_sc.function || prev_sc.symbol)
469 bool show_fullpaths = false;
470 bool show_module = true;
471 bool show_inlined_frames = true;
472 sc.DumpStopContext (&strm,
477 show_inlined_frames);
488 if ((options & eOptionMarkPCAddress) && pc_addr_ptr)
490 strm.PutCString(inst_is_at_pc ? "-> " : " ");
492 const bool show_bytes = (options & eOptionShowBytes) != 0;
493 inst->Dump(&strm, max_opcode_byte_size, true, show_bytes, &exe_ctx);
507 Disassembler::Disassemble
510 const ArchSpec &arch,
511 const char *plugin_name,
513 const ExecutionContext &exe_ctx,
514 uint32_t num_instructions,
515 uint32_t num_mixed_context_lines,
521 StackFrame *frame = exe_ctx.GetFramePtr();
524 SymbolContext sc(frame->GetSymbolContext(eSymbolContextFunction | eSymbolContextSymbol));
527 range = sc.function->GetAddressRange();
529 else if (sc.symbol && sc.symbol->ValueIsAddress())
531 range.GetBaseAddress() = sc.symbol->GetAddress();
532 range.SetByteSize (sc.symbol->GetByteSize());
536 range.GetBaseAddress() = frame->GetFrameCodeAddress();
539 if (range.GetBaseAddress().IsValid() && range.GetByteSize() == 0)
540 range.SetByteSize (DEFAULT_DISASM_BYTE_SIZE);
543 return Disassemble (debugger,
550 num_mixed_context_lines,
555 Instruction::Instruction(const Address &address, AddressClass addr_class) :
557 m_address_class (addr_class),
559 m_calculated_strings(false)
563 Instruction::~Instruction()
568 Instruction::GetAddressClass ()
570 if (m_address_class == eAddressClassInvalid)
571 m_address_class = m_address.GetAddressClass();
572 return m_address_class;
576 Instruction::Dump (lldb_private::Stream *s,
577 uint32_t max_opcode_byte_size,
580 const ExecutionContext* exe_ctx)
582 size_t opcode_column_width = 7;
583 const size_t operand_column_width = 25;
585 CalculateMnemonicOperandsAndCommentIfNeeded (exe_ctx);
592 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL,
593 Address::DumpStyleLoadAddress,
594 Address::DumpStyleModuleWithFileAddress,
602 if (m_opcode.GetType() == Opcode::eTypeBytes)
604 // x86_64 and i386 are the only ones that use bytes right now so
605 // pad out the byte dump to be able to always show 15 bytes (3 chars each)
607 if (max_opcode_byte_size > 0)
608 m_opcode.Dump (&ss, max_opcode_byte_size * 3 + 1);
610 m_opcode.Dump (&ss, 15 * 3 + 1);
614 // Else, we have ARM which can show up to a uint32_t 0x00000000 (10 spaces)
615 // plus two for padding...
616 if (max_opcode_byte_size > 0)
617 m_opcode.Dump (&ss, max_opcode_byte_size * 3 + 1);
619 m_opcode.Dump (&ss, 12);
623 const size_t opcode_pos = ss.GetSize();
625 // The default opcode size of 7 characters is plenty for most architectures
626 // but some like arm can pull out the occasional vqrshrun.s16. We won't get
627 // consistent column spacing in these cases, unfortunately.
628 if (m_opcode_name.length() >= opcode_column_width)
630 opcode_column_width = m_opcode_name.length() + 1;
633 ss.PutCString (m_opcode_name.c_str());
634 ss.FillLastLineToColumn (opcode_pos + opcode_column_width, ' ');
635 ss.PutCString (m_mnemonics.c_str());
637 if (!m_comment.empty())
639 ss.FillLastLineToColumn (opcode_pos + opcode_column_width + operand_column_width, ' ');
640 ss.PutCString (" ; ");
641 ss.PutCString (m_comment.c_str());
643 s->Write (ss.GetData(), ss.GetSize());
647 Instruction::DumpEmulation (const ArchSpec &arch)
649 std::unique_ptr<EmulateInstruction> insn_emulator_ap (EmulateInstruction::FindPlugin (arch, eInstructionTypeAny, NULL));
650 if (insn_emulator_ap.get())
652 insn_emulator_ap->SetInstruction (GetOpcode(), GetAddress(), NULL);
653 return insn_emulator_ap->EvaluateInstruction (0);
660 Instruction::ReadArray (FILE *in_file, Stream *out_stream, OptionValue::Type data_type)
665 OptionValueSP option_value_sp (new OptionValueArray (1u << data_type));
670 if (!fgets (buffer, 1023, in_file))
672 out_stream->Printf ("Instruction::ReadArray: Error reading file (fgets).\n");
673 option_value_sp.reset ();
674 return option_value_sp;
677 std::string line (buffer);
679 size_t len = line.size();
680 if (line[len-1] == '\n')
686 if ((line.size() == 1) && line[0] == ']')
695 static RegularExpression g_reg_exp ("^[ \t]*([^ \t]+)[ \t]*$");
696 RegularExpression::Match regex_match(1);
697 bool reg_exp_success = g_reg_exp.Execute (line.c_str(), ®ex_match);
699 regex_match.GetMatchAtIndex (line.c_str(), 1, value);
703 OptionValueSP data_value_sp;
706 case OptionValue::eTypeUInt64:
707 data_value_sp.reset (new OptionValueUInt64 (0, 0));
708 data_value_sp->SetValueFromCString (value.c_str());
710 // Other types can be added later as needed.
712 data_value_sp.reset (new OptionValueString (value.c_str(), ""));
716 option_value_sp->GetAsArray()->InsertValue (idx, data_value_sp);
721 return option_value_sp;
725 Instruction::ReadDictionary (FILE *in_file, Stream *out_stream)
730 OptionValueSP option_value_sp (new OptionValueDictionary());
731 static ConstString encoding_key ("data_encoding");
732 OptionValue::Type data_type = OptionValue::eTypeInvalid;
737 // Read the next line in the file
738 if (!fgets (buffer, 1023, in_file))
740 out_stream->Printf ("Instruction::ReadDictionary: Error reading file (fgets).\n");
741 option_value_sp.reset ();
742 return option_value_sp;
745 // Check to see if the line contains the end-of-dictionary marker ("}")
746 std::string line (buffer);
748 size_t len = line.size();
749 if (line[len-1] == '\n')
755 if ((line.size() == 1) && (line[0] == '}'))
761 // Try to find a key-value pair in the current line and add it to the dictionary.
764 static RegularExpression g_reg_exp ("^[ \t]*([a-zA-Z_][a-zA-Z0-9_]*)[ \t]*=[ \t]*(.*)[ \t]*$");
765 RegularExpression::Match regex_match(2);
767 bool reg_exp_success = g_reg_exp.Execute (line.c_str(), ®ex_match);
772 regex_match.GetMatchAtIndex (line.c_str(), 1, key);
773 regex_match.GetMatchAtIndex (line.c_str(), 2, value);
777 out_stream->Printf ("Instruction::ReadDictionary: Failure executing regular expression.\n");
778 option_value_sp.reset();
779 return option_value_sp;
782 ConstString const_key (key.c_str());
783 // Check value to see if it's the start of an array or dictionary.
785 lldb::OptionValueSP value_sp;
786 assert (value.empty() == false);
787 assert (key.empty() == false);
791 assert (value.size() == 1);
792 // value is a dictionary
793 value_sp = ReadDictionary (in_file, out_stream);
794 if (value_sp.get() == NULL)
796 option_value_sp.reset ();
797 return option_value_sp;
800 else if (value[0] == '[')
802 assert (value.size() == 1);
804 value_sp = ReadArray (in_file, out_stream, data_type);
805 if (value_sp.get() == NULL)
807 option_value_sp.reset ();
808 return option_value_sp;
810 // We've used the data_type to read an array; re-set the type to Invalid
811 data_type = OptionValue::eTypeInvalid;
813 else if ((value[0] == '0') && (value[1] == 'x'))
815 value_sp.reset (new OptionValueUInt64 (0, 0));
816 value_sp->SetValueFromCString (value.c_str());
820 size_t len = value.size();
821 if ((value[0] == '"') && (value[len-1] == '"'))
822 value = value.substr (1, len-2);
823 value_sp.reset (new OptionValueString (value.c_str(), ""));
828 if (const_key == encoding_key)
830 // A 'data_encoding=..." is NOT a normal key-value pair; it is meta-data indicating the
831 // data type of an upcoming array (usually the next bit of data to be read in).
832 if (strcmp (value.c_str(), "uint32_t") == 0)
833 data_type = OptionValue::eTypeUInt64;
836 option_value_sp->GetAsDictionary()->SetValueForKey (const_key, value_sp, false);
840 return option_value_sp;
844 Instruction::TestEmulation (Stream *out_stream, const char *file_name)
851 out_stream->Printf ("Instruction::TestEmulation: Missing file_name.");
855 FILE *test_file = fopen (file_name, "r");
858 out_stream->Printf ("Instruction::TestEmulation: Attempt to open test file failed.");
863 if (!fgets (buffer, 255, test_file))
865 out_stream->Printf ("Instruction::TestEmulation: Error reading first line of test file.\n");
870 if (strncmp (buffer, "InstructionEmulationState={", 27) != 0)
872 out_stream->Printf ("Instructin::TestEmulation: Test file does not contain emulation state dictionary\n");
877 // Read all the test information from the test file into an OptionValueDictionary.
879 OptionValueSP data_dictionary_sp (ReadDictionary (test_file, out_stream));
880 if (data_dictionary_sp.get() == NULL)
882 out_stream->Printf ("Instruction::TestEmulation: Error reading Dictionary Object.\n");
889 OptionValueDictionary *data_dictionary = data_dictionary_sp->GetAsDictionary();
890 static ConstString description_key ("assembly_string");
891 static ConstString triple_key ("triple");
893 OptionValueSP value_sp = data_dictionary->GetValueForKey (description_key);
895 if (value_sp.get() == NULL)
897 out_stream->Printf ("Instruction::TestEmulation: Test file does not contain description string.\n");
901 SetDescription (value_sp->GetStringValue());
904 value_sp = data_dictionary->GetValueForKey (triple_key);
905 if (value_sp.get() == NULL)
907 out_stream->Printf ("Instruction::TestEmulation: Test file does not contain triple.\n");
912 arch.SetTriple (llvm::Triple (value_sp->GetStringValue()));
914 bool success = false;
915 std::unique_ptr<EmulateInstruction> insn_emulator_ap (EmulateInstruction::FindPlugin (arch, eInstructionTypeAny, NULL));
916 if (insn_emulator_ap.get())
917 success = insn_emulator_ap->TestEmulation (out_stream, arch, data_dictionary);
920 out_stream->Printf ("Emulation test succeeded.");
922 out_stream->Printf ("Emulation test failed.");
928 Instruction::Emulate (const ArchSpec &arch,
929 uint32_t evaluate_options,
931 EmulateInstruction::ReadMemoryCallback read_mem_callback,
932 EmulateInstruction::WriteMemoryCallback write_mem_callback,
933 EmulateInstruction::ReadRegisterCallback read_reg_callback,
934 EmulateInstruction::WriteRegisterCallback write_reg_callback)
936 std::unique_ptr<EmulateInstruction> insn_emulator_ap (EmulateInstruction::FindPlugin (arch, eInstructionTypeAny, NULL));
937 if (insn_emulator_ap.get())
939 insn_emulator_ap->SetBaton (baton);
940 insn_emulator_ap->SetCallbacks (read_mem_callback, write_mem_callback, read_reg_callback, write_reg_callback);
941 insn_emulator_ap->SetInstruction (GetOpcode(), GetAddress(), NULL);
942 return insn_emulator_ap->EvaluateInstruction (evaluate_options);
950 Instruction::GetData (DataExtractor &data)
952 return m_opcode.GetData(data);
955 InstructionList::InstructionList() :
960 InstructionList::~InstructionList()
965 InstructionList::GetSize() const
967 return m_instructions.size();
971 InstructionList::GetMaxOpcocdeByteSize () const
973 uint32_t max_inst_size = 0;
974 collection::const_iterator pos, end;
975 for (pos = m_instructions.begin(), end = m_instructions.end();
979 uint32_t inst_size = (*pos)->GetOpcode().GetByteSize();
980 if (max_inst_size < inst_size)
981 max_inst_size = inst_size;
983 return max_inst_size;
989 InstructionList::GetInstructionAtIndex (size_t idx) const
991 InstructionSP inst_sp;
992 if (idx < m_instructions.size())
993 inst_sp = m_instructions[idx];
998 InstructionList::Dump (Stream *s,
1001 const ExecutionContext* exe_ctx)
1003 const uint32_t max_opcode_byte_size = GetMaxOpcocdeByteSize();
1004 collection::const_iterator pos, begin, end;
1005 for (begin = m_instructions.begin(), end = m_instructions.end(), pos = begin;
1011 (*pos)->Dump(s, max_opcode_byte_size, show_address, show_bytes, exe_ctx);
1017 InstructionList::Clear()
1019 m_instructions.clear();
1023 InstructionList::Append (lldb::InstructionSP &inst_sp)
1026 m_instructions.push_back(inst_sp);
1030 InstructionList::GetIndexOfNextBranchInstruction(uint32_t start) const
1032 size_t num_instructions = m_instructions.size();
1034 uint32_t next_branch = UINT32_MAX;
1035 for (size_t i = start; i < num_instructions; i++)
1037 if (m_instructions[i]->DoesBranch())
1047 InstructionList::GetIndexOfInstructionAtLoadAddress (lldb::addr_t load_addr, Target &target)
1050 address.SetLoadAddress(load_addr, &target);
1051 size_t num_instructions = m_instructions.size();
1052 uint32_t index = UINT32_MAX;
1053 for (size_t i = 0; i < num_instructions; i++)
1055 if (m_instructions[i]->GetAddress() == address)
1065 Disassembler::ParseInstructions (const ExecutionContext *exe_ctx,
1066 const AddressRange &range,
1067 Stream *error_strm_ptr,
1068 bool prefer_file_cache)
1072 Target *target = exe_ctx->GetTargetPtr();
1073 const addr_t byte_size = range.GetByteSize();
1074 if (target == NULL || byte_size == 0 || !range.GetBaseAddress().IsValid())
1077 DataBufferHeap *heap_buffer = new DataBufferHeap (byte_size, '\0');
1078 DataBufferSP data_sp(heap_buffer);
1081 lldb::addr_t load_addr = LLDB_INVALID_ADDRESS;
1082 const size_t bytes_read = target->ReadMemory (range.GetBaseAddress(),
1084 heap_buffer->GetBytes(),
1085 heap_buffer->GetByteSize(),
1091 if (bytes_read != heap_buffer->GetByteSize())
1092 heap_buffer->SetByteSize (bytes_read);
1093 DataExtractor data (data_sp,
1094 m_arch.GetByteOrder(),
1095 m_arch.GetAddressByteSize());
1096 const bool data_from_file = load_addr == LLDB_INVALID_ADDRESS;
1097 return DecodeInstructions (range.GetBaseAddress(), data, 0, UINT32_MAX, false, data_from_file);
1099 else if (error_strm_ptr)
1101 const char *error_cstr = error.AsCString();
1104 error_strm_ptr->Printf("error: %s\n", error_cstr);
1108 else if (error_strm_ptr)
1110 error_strm_ptr->PutCString("error: invalid execution context\n");
1116 Disassembler::ParseInstructions (const ExecutionContext *exe_ctx,
1117 const Address &start,
1118 uint32_t num_instructions,
1119 bool prefer_file_cache)
1121 m_instruction_list.Clear();
1123 if (exe_ctx == NULL || num_instructions == 0 || !start.IsValid())
1126 Target *target = exe_ctx->GetTargetPtr();
1127 // Calculate the max buffer size we will need in order to disassemble
1128 const addr_t byte_size = num_instructions * m_arch.GetMaximumOpcodeByteSize();
1130 if (target == NULL || byte_size == 0)
1133 DataBufferHeap *heap_buffer = new DataBufferHeap (byte_size, '\0');
1134 DataBufferSP data_sp (heap_buffer);
1137 lldb::addr_t load_addr = LLDB_INVALID_ADDRESS;
1138 const size_t bytes_read = target->ReadMemory (start,
1140 heap_buffer->GetBytes(),
1145 const bool data_from_file = load_addr == LLDB_INVALID_ADDRESS;
1147 if (bytes_read == 0)
1149 DataExtractor data (data_sp,
1150 m_arch.GetByteOrder(),
1151 m_arch.GetAddressByteSize());
1153 const bool append_instructions = true;
1154 DecodeInstructions (start,
1158 append_instructions,
1161 return m_instruction_list.GetSize();
1164 //----------------------------------------------------------------------
1165 // Disassembler copy constructor
1166 //----------------------------------------------------------------------
1167 Disassembler::Disassembler(const ArchSpec& arch, const char *flavor) :
1169 m_instruction_list(),
1170 m_base_addr(LLDB_INVALID_ADDRESS),
1174 m_flavor.assign("default");
1176 m_flavor.assign(flavor);
1179 //----------------------------------------------------------------------
1181 //----------------------------------------------------------------------
1182 Disassembler::~Disassembler()
1187 Disassembler::GetInstructionList ()
1189 return m_instruction_list;
1192 const InstructionList &
1193 Disassembler::GetInstructionList () const
1195 return m_instruction_list;
1198 //----------------------------------------------------------------------
1199 // Class PseudoInstruction
1200 //----------------------------------------------------------------------
1201 PseudoInstruction::PseudoInstruction () :
1202 Instruction (Address(), eAddressClassUnknown),
1207 PseudoInstruction::~PseudoInstruction ()
1212 PseudoInstruction::DoesBranch ()
1214 // This is NOT a valid question for a pseudo instruction.
1219 PseudoInstruction::Decode (const lldb_private::Disassembler &disassembler,
1220 const lldb_private::DataExtractor &data,
1221 lldb::offset_t data_offset)
1223 return m_opcode.GetByteSize();
1228 PseudoInstruction::SetOpcode (size_t opcode_size, void *opcode_data)
1233 switch (opcode_size)
1237 uint8_t value8 = *((uint8_t *) opcode_data);
1238 m_opcode.SetOpcode8 (value8);
1243 uint16_t value16 = *((uint16_t *) opcode_data);
1244 m_opcode.SetOpcode16 (value16);
1249 uint32_t value32 = *((uint32_t *) opcode_data);
1250 m_opcode.SetOpcode32 (value32);
1255 uint64_t value64 = *((uint64_t *) opcode_data);
1256 m_opcode.SetOpcode64 (value64);
1265 PseudoInstruction::SetDescription (const char *description)
1267 if (description && strlen (description) > 0)
1268 m_description = description;