Disassembler.h revision 888a7334344778d1a4edbd58b5852ae4d53ffed9
1//===-- Disassembler.h ------------------------------------------*- 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#ifndef liblldb_Disassembler_h_
11#define liblldb_Disassembler_h_
12
13// C Includes
14// C++ Includes
15#include <vector>
16
17// Other libraries and framework includes
18// Project includes
19#include "lldb/lldb-private.h"
20#include "lldb/Core/Address.h"
21#include "lldb/Core/ArchSpec.h"
22#include "lldb/Core/EmulateInstruction.h"
23#include "lldb/Core/Opcode.h"
24#include "lldb/Core/PluginInterface.h"
25#include "lldb/Interpreter/NamedOptionValue.h"
26
27namespace lldb_private {
28
29class Instruction
30{
31public:
32    Instruction (const Address &address,
33                 AddressClass addr_class = eAddressClassInvalid);
34
35    virtual
36   ~Instruction();
37
38    const Address &
39    GetAddress () const
40    {
41        return m_address;
42    }
43
44    AddressClass
45    GetAddressClass ();
46
47    void
48    SetAddress (const Address &addr)
49    {
50        // Invalidate the address class to lazily discover
51        // it if we need to.
52        m_address_class = eAddressClassInvalid;
53        m_address = addr;
54    }
55
56    virtual void
57    Dump (Stream *s,
58          uint32_t max_opcode_byte_size,
59          bool show_address,
60          bool show_bytes,
61          const ExecutionContext *exe_ctx,
62          bool raw) = 0;
63
64    virtual bool
65    DoesBranch () const = 0;
66
67    virtual size_t
68    Decode (const Disassembler &disassembler,
69            const DataExtractor& data,
70            uint32_t data_offset) = 0;
71
72    virtual void
73    SetDescription (const char *) {};  // May be overridden in sub-classes that have descriptions.
74
75    lldb::OptionValueSP
76    ReadArray (FILE *in_file, Stream *out_stream, OptionValue::Type data_type);
77
78    lldb::OptionValueSP
79    ReadDictionary (FILE *in_file, Stream *out_stream);
80
81    bool
82    DumpEmulation (const ArchSpec &arch);
83
84    virtual bool
85    TestEmulation (Stream *stream, const char *test_file_name);
86
87    bool
88    Emulate (const ArchSpec &arch,
89             uint32_t evaluate_options,
90             void *baton,
91             EmulateInstruction::ReadMemory read_mem_callback,
92             EmulateInstruction::WriteMemory write_mem_calback,
93             EmulateInstruction::ReadRegister read_reg_callback,
94             EmulateInstruction::WriteRegister write_reg_callback);
95
96    const Opcode &
97    GetOpcode () const
98    {
99        return m_opcode;
100    }
101
102protected:
103    Address m_address; // The section offset address of this instruction
104    // We include an address class in the Instruction class to
105    // allow the instruction specify the eAddressClassCodeAlternateISA
106    // (currently used for thumb), and also to specify data (eAddressClassData).
107    // The usual value will be eAddressClassCode, but often when
108    // disassembling memory, you might run into data. This can
109    // help us to disassemble appropriately.
110    AddressClass m_address_class;
111    Opcode m_opcode; // The opcode for this instruction
112};
113
114
115class InstructionList
116{
117public:
118    InstructionList();
119    ~InstructionList();
120
121    size_t
122    GetSize() const;
123
124    uint32_t
125    GetMaxOpcocdeByteSize () const;
126
127    lldb::InstructionSP
128    GetInstructionAtIndex (uint32_t idx) const;
129
130    void
131    Clear();
132
133    void
134    Append (lldb::InstructionSP &inst_sp);
135
136private:
137    typedef std::vector<lldb::InstructionSP> collection;
138    typedef collection::iterator iterator;
139    typedef collection::const_iterator const_iterator;
140
141    collection m_instructions;
142};
143
144class PseudoInstruction :
145    public lldb_private::Instruction
146{
147public:
148
149    PseudoInstruction ();
150
151     virtual
152     ~PseudoInstruction ();
153
154    virtual void
155    Dump (lldb_private::Stream *s,
156          uint32_t max_opcode_byte_size,
157          bool show_address,
158          bool show_bytes,
159          const lldb_private::ExecutionContext* exe_ctx,
160          bool raw);
161
162    virtual bool
163    DoesBranch () const;
164
165    virtual size_t
166    Decode (const lldb_private::Disassembler &disassembler,
167            const lldb_private::DataExtractor &data,
168            uint32_t data_offset);
169
170    void
171    SetOpcode (size_t opcode_size, void *opcode_data);
172
173    virtual void
174    SetDescription (const char *description);
175
176protected:
177    std::string m_description;
178
179    DISALLOW_COPY_AND_ASSIGN (PseudoInstruction);
180};
181
182class Disassembler :
183    public PluginInterface
184{
185public:
186
187
188    static Disassembler*
189    FindPlugin (const ArchSpec &arch, const char *plugin_name);
190
191    static lldb::DisassemblerSP
192    DisassembleRange (const ArchSpec &arch,
193                      const char *plugin_name,
194                      const ExecutionContext &exe_ctx,
195                      const AddressRange &disasm_range);
196
197    static bool
198    Disassemble (Debugger &debugger,
199                 const ArchSpec &arch,
200                 const char *plugin_name,
201                 const ExecutionContext &exe_ctx,
202                 const AddressRange &range,
203                 uint32_t num_instructions,
204                 uint32_t num_mixed_context_lines,
205                 bool show_bytes,
206                 bool raw,
207                 Stream &strm);
208
209    static bool
210    Disassemble (Debugger &debugger,
211                 const ArchSpec &arch,
212                 const char *plugin_name,
213                 const ExecutionContext &exe_ctx,
214                 const Address &start,
215                 uint32_t num_instructions,
216                 uint32_t num_mixed_context_lines,
217                 bool show_bytes,
218                 bool raw,
219                 Stream &strm);
220
221    static size_t
222    Disassemble (Debugger &debugger,
223                 const ArchSpec &arch,
224                 const char *plugin_name,
225                 const ExecutionContext &exe_ctx,
226                 SymbolContextList &sc_list,
227                 uint32_t num_instructions,
228                 uint32_t num_mixed_context_lines,
229                 bool show_bytes,
230                 bool raw,
231                 Stream &strm);
232
233    static bool
234    Disassemble (Debugger &debugger,
235                 const ArchSpec &arch,
236                 const char *plugin_name,
237                 const ExecutionContext &exe_ctx,
238                 const ConstString &name,
239                 Module *module,
240                 uint32_t num_instructions,
241                 uint32_t num_mixed_context_lines,
242                 bool show_bytes,
243                 bool raw,
244                 Stream &strm);
245
246    static bool
247    Disassemble (Debugger &debugger,
248                 const ArchSpec &arch,
249                 const char *plugin_name,
250                 const ExecutionContext &exe_ctx,
251                 uint32_t num_instructions,
252                 uint32_t num_mixed_context_lines,
253                 bool show_bytes,
254                 bool raw,
255                 Stream &strm);
256
257    //------------------------------------------------------------------
258    // Constructors and Destructors
259    //------------------------------------------------------------------
260    Disassembler(const ArchSpec &arch);
261    virtual ~Disassembler();
262
263    typedef const char * (*SummaryCallback)(const Instruction& inst, ExecutionContext *exe_context, void *user_data);
264
265    static bool
266    PrintInstructions (Disassembler *disasm_ptr,
267                       Debugger &debugger,
268                       const ArchSpec &arch,
269                       const ExecutionContext &exe_ctx,
270                       uint32_t num_instructions,
271                       uint32_t num_mixed_context_lines,
272                       bool show_bytes,
273                       bool raw,
274                       Stream &strm);
275
276    size_t
277    ParseInstructions (const ExecutionContext *exe_ctx,
278                       const AddressRange &range);
279
280    size_t
281    ParseInstructions (const ExecutionContext *exe_ctx,
282                       const Address &range,
283                       uint32_t num_instructions);
284
285    virtual size_t
286    DecodeInstructions (const Address &base_addr,
287                        const DataExtractor& data,
288                        uint32_t data_offset,
289                        uint32_t num_instructions,
290                        bool append) = 0;
291
292    InstructionList &
293    GetInstructionList ();
294
295    const InstructionList &
296    GetInstructionList () const;
297
298    const ArchSpec &
299    GetArchitecture () const
300    {
301        return m_arch;
302    }
303
304protected:
305    //------------------------------------------------------------------
306    // Classes that inherit from Disassembler can see and modify these
307    //------------------------------------------------------------------
308    const ArchSpec m_arch;
309    InstructionList m_instruction_list;
310    lldb::addr_t m_base_addr;
311
312private:
313    //------------------------------------------------------------------
314    // For Disassembler only
315    //------------------------------------------------------------------
316    DISALLOW_COPY_AND_ASSIGN (Disassembler);
317};
318
319} // namespace lldb_private
320
321#endif  // liblldb_Disassembler_h_
322