1 //===-- Disassembler.h ------------------------------------------*- 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 #ifndef liblldb_Disassembler_h_
11 #define liblldb_Disassembler_h_
18 // Other libraries and framework includes
20 #include "lldb/lldb-private.h"
21 #include "lldb/Core/Address.h"
22 #include "lldb/Core/ArchSpec.h"
23 #include "lldb/Core/EmulateInstruction.h"
24 #include "lldb/Core/Opcode.h"
25 #include "lldb/Core/PluginInterface.h"
26 #include "lldb/Interpreter/OptionValue.h"
28 namespace lldb_private {
33 Instruction (const Address &address,
34 lldb::AddressClass addr_class = lldb::eAddressClassInvalid);
46 GetMnemonic (const ExecutionContext* exe_ctx)
48 CalculateMnemonicOperandsAndCommentIfNeeded (exe_ctx);
49 return m_opcode_name.c_str();
52 GetOperands (const ExecutionContext* exe_ctx)
54 CalculateMnemonicOperandsAndCommentIfNeeded (exe_ctx);
55 return m_mnemonics.c_str();
59 GetComment (const ExecutionContext* exe_ctx)
61 CalculateMnemonicOperandsAndCommentIfNeeded (exe_ctx);
62 return m_comment.c_str();
66 CalculateMnemonicOperandsAndComment (const ExecutionContext* exe_ctx) = 0;
72 SetAddress (const Address &addr)
74 // Invalidate the address class to lazily discover
76 m_address_class = lldb::eAddressClassInvalid;
82 uint32_t max_opcode_byte_size,
85 const ExecutionContext* exe_ctx);
91 Decode (const Disassembler &disassembler,
92 const DataExtractor& data,
93 lldb::offset_t data_offset) = 0;
96 SetDescription (const char *) {} // May be overridden in sub-classes that have descriptions.
99 ReadArray (FILE *in_file, Stream *out_stream, OptionValue::Type data_type);
102 ReadDictionary (FILE *in_file, Stream *out_stream);
105 DumpEmulation (const ArchSpec &arch);
108 TestEmulation (Stream *stream, const char *test_file_name);
111 Emulate (const ArchSpec &arch,
112 uint32_t evaluate_options,
114 EmulateInstruction::ReadMemoryCallback read_mem_callback,
115 EmulateInstruction::WriteMemoryCallback write_mem_calback,
116 EmulateInstruction::ReadRegisterCallback read_reg_callback,
117 EmulateInstruction::WriteRegisterCallback write_reg_callback);
126 GetData (DataExtractor &data);
129 Address m_address; // The section offset address of this instruction
130 // We include an address class in the Instruction class to
131 // allow the instruction specify the eAddressClassCodeAlternateISA
132 // (currently used for thumb), and also to specify data (eAddressClassData).
133 // The usual value will be eAddressClassCode, but often when
134 // disassembling memory, you might run into data. This can
135 // help us to disassemble appropriately.
137 lldb::AddressClass m_address_class; // Use GetAddressClass () accessor function!
139 Opcode m_opcode; // The opcode for this instruction
140 std::string m_opcode_name;
141 std::string m_mnemonics;
142 std::string m_comment;
143 bool m_calculated_strings;
146 CalculateMnemonicOperandsAndCommentIfNeeded (const ExecutionContext* exe_ctx)
148 if (!m_calculated_strings)
150 m_calculated_strings = true;
151 CalculateMnemonicOperandsAndComment(exe_ctx);
157 class InstructionList
167 GetMaxOpcocdeByteSize () const;
170 GetInstructionAtIndex (size_t idx) const;
173 GetIndexOfNextBranchInstruction(uint32_t start) const;
176 GetIndexOfInstructionAtLoadAddress (lldb::addr_t load_addr, Target &target);
182 Append (lldb::InstructionSP &inst_sp);
188 const ExecutionContext* exe_ctx);
191 typedef std::vector<lldb::InstructionSP> collection;
192 typedef collection::iterator iterator;
193 typedef collection::const_iterator const_iterator;
195 collection m_instructions;
198 class PseudoInstruction :
203 PseudoInstruction ();
206 ~PseudoInstruction ();
212 CalculateMnemonicOperandsAndComment (const ExecutionContext* exe_ctx)
214 // TODO: fill this in and put opcode name into Instruction::m_opcode_name,
215 // mnemonic into Instruction::m_mnemonics, and any comment into
216 // Instruction::m_comment
220 Decode (const Disassembler &disassembler,
221 const DataExtractor &data,
222 lldb::offset_t data_offset);
225 SetOpcode (size_t opcode_size, void *opcode_data);
228 SetDescription (const char *description);
231 std::string m_description;
233 DISALLOW_COPY_AND_ASSIGN (PseudoInstruction);
237 public std::enable_shared_from_this<Disassembler>,
238 public PluginInterface
245 eOptionShowBytes = (1u << 0),
246 eOptionRawOuput = (1u << 1),
247 eOptionMarkPCSourceLine = (1u << 2), // Mark the source line that contains the current PC (mixed mode only)
248 eOptionMarkPCAddress = (1u << 3) // Mark the disassembly line the contains the PC
251 enum HexImmediateStyle
257 // FindPlugin should be lax about the flavor string (it is too annoying to have various internal uses of the
258 // disassembler fail because the global flavor string gets set wrong. Instead, if you get a flavor string you
259 // don't understand, use the default. Folks who care to check can use the FlavorValidForArchSpec method on the
260 // disassembler they got back.
261 static lldb::DisassemblerSP
262 FindPlugin (const ArchSpec &arch, const char *flavor, const char *plugin_name);
264 // This version will use the value in the Target settings if flavor is NULL;
265 static lldb::DisassemblerSP
266 FindPluginForTarget(const lldb::TargetSP target_sp, const ArchSpec &arch, const char *flavor, const char *plugin_name);
268 static lldb::DisassemblerSP
269 DisassembleRange (const ArchSpec &arch,
270 const char *plugin_name,
272 const ExecutionContext &exe_ctx,
273 const AddressRange &disasm_range);
275 static lldb::DisassemblerSP
276 DisassembleBytes (const ArchSpec &arch,
277 const char *plugin_name,
279 const Address &start,
282 uint32_t max_num_instructions,
283 bool data_from_file);
286 Disassemble (Debugger &debugger,
287 const ArchSpec &arch,
288 const char *plugin_name,
290 const ExecutionContext &exe_ctx,
291 const AddressRange &range,
292 uint32_t num_instructions,
293 uint32_t num_mixed_context_lines,
298 Disassemble (Debugger &debugger,
299 const ArchSpec &arch,
300 const char *plugin_name,
302 const ExecutionContext &exe_ctx,
303 const Address &start,
304 uint32_t num_instructions,
305 uint32_t num_mixed_context_lines,
310 Disassemble (Debugger &debugger,
311 const ArchSpec &arch,
312 const char *plugin_name,
314 const ExecutionContext &exe_ctx,
315 SymbolContextList &sc_list,
316 uint32_t num_instructions,
317 uint32_t num_mixed_context_lines,
322 Disassemble (Debugger &debugger,
323 const ArchSpec &arch,
324 const char *plugin_name,
326 const ExecutionContext &exe_ctx,
327 const ConstString &name,
329 uint32_t num_instructions,
330 uint32_t num_mixed_context_lines,
335 Disassemble (Debugger &debugger,
336 const ArchSpec &arch,
337 const char *plugin_name,
339 const ExecutionContext &exe_ctx,
340 uint32_t num_instructions,
341 uint32_t num_mixed_context_lines,
345 //------------------------------------------------------------------
346 // Constructors and Destructors
347 //------------------------------------------------------------------
348 Disassembler(const ArchSpec &arch, const char *flavor);
349 virtual ~Disassembler();
351 typedef const char * (*SummaryCallback)(const Instruction& inst, ExecutionContext *exe_context, void *user_data);
354 PrintInstructions (Disassembler *disasm_ptr,
356 const ArchSpec &arch,
357 const ExecutionContext &exe_ctx,
358 uint32_t num_instructions,
359 uint32_t num_mixed_context_lines,
364 ParseInstructions (const ExecutionContext *exe_ctx,
365 const AddressRange &range,
366 Stream *error_strm_ptr,
367 bool prefer_file_cache);
370 ParseInstructions (const ExecutionContext *exe_ctx,
371 const Address &range,
372 uint32_t num_instructions,
373 bool prefer_file_cache);
376 DecodeInstructions (const Address &base_addr,
377 const DataExtractor& data,
378 lldb::offset_t data_offset,
379 size_t num_instructions,
381 bool data_from_file) = 0;
384 GetInstructionList ();
386 const InstructionList &
387 GetInstructionList () const;
390 GetArchitecture () const
398 return m_flavor.c_str();
402 FlavorValidForArchSpec (const lldb_private::ArchSpec &arch, const char *flavor) = 0;
405 //------------------------------------------------------------------
406 // Classes that inherit from Disassembler can see and modify these
407 //------------------------------------------------------------------
408 const ArchSpec m_arch;
409 InstructionList m_instruction_list;
410 lldb::addr_t m_base_addr;
411 std::string m_flavor;
414 //------------------------------------------------------------------
415 // For Disassembler only
416 //------------------------------------------------------------------
417 DISALLOW_COPY_AND_ASSIGN (Disassembler);
420 } // namespace lldb_private
422 #endif // liblldb_Disassembler_h_