Disassembler.h revision 6377f5c7a1c7ac62488abc597e12a38861fcd716
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::ReadMemoryCallback read_mem_callback,
92             EmulateInstruction::WriteMemoryCallback write_mem_calback,
93             EmulateInstruction::ReadRegisterCallback read_reg_callback,
94             EmulateInstruction::WriteRegisterCallback 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    enum
188    {
189        eOptionNone             = 0u,
190        eOptionShowBytes        = (1u << 0),
191        eOptionRawOuput         = (1u << 1),
192        eOptionMarkPCSourceLine = (1u << 2), // Mark the source line that contains the current PC (mixed mode only)
193        eOptionMarkPCAddress    = (1u << 3)  // Mark the disassembly line the contains the PC
194    };
195
196    static Disassembler*
197    FindPlugin (const ArchSpec &arch, const char *plugin_name);
198
199    static lldb::DisassemblerSP
200    DisassembleRange (const ArchSpec &arch,
201                      const char *plugin_name,
202                      const ExecutionContext &exe_ctx,
203                      const AddressRange &disasm_range);
204
205    static bool
206    Disassemble (Debugger &debugger,
207                 const ArchSpec &arch,
208                 const char *plugin_name,
209                 const ExecutionContext &exe_ctx,
210                 const AddressRange &range,
211                 uint32_t num_instructions,
212                 uint32_t num_mixed_context_lines,
213                 uint32_t options,
214                 Stream &strm);
215
216    static bool
217    Disassemble (Debugger &debugger,
218                 const ArchSpec &arch,
219                 const char *plugin_name,
220                 const ExecutionContext &exe_ctx,
221                 const Address &start,
222                 uint32_t num_instructions,
223                 uint32_t num_mixed_context_lines,
224                 uint32_t options,
225                 Stream &strm);
226
227    static size_t
228    Disassemble (Debugger &debugger,
229                 const ArchSpec &arch,
230                 const char *plugin_name,
231                 const ExecutionContext &exe_ctx,
232                 SymbolContextList &sc_list,
233                 uint32_t num_instructions,
234                 uint32_t num_mixed_context_lines,
235                 uint32_t options,
236                 Stream &strm);
237
238    static bool
239    Disassemble (Debugger &debugger,
240                 const ArchSpec &arch,
241                 const char *plugin_name,
242                 const ExecutionContext &exe_ctx,
243                 const ConstString &name,
244                 Module *module,
245                 uint32_t num_instructions,
246                 uint32_t num_mixed_context_lines,
247                 uint32_t options,
248                 Stream &strm);
249
250    static bool
251    Disassemble (Debugger &debugger,
252                 const ArchSpec &arch,
253                 const char *plugin_name,
254                 const ExecutionContext &exe_ctx,
255                 uint32_t num_instructions,
256                 uint32_t num_mixed_context_lines,
257                 uint32_t options,
258                 Stream &strm);
259
260    //------------------------------------------------------------------
261    // Constructors and Destructors
262    //------------------------------------------------------------------
263    Disassembler(const ArchSpec &arch);
264    virtual ~Disassembler();
265
266    typedef const char * (*SummaryCallback)(const Instruction& inst, ExecutionContext *exe_context, void *user_data);
267
268    static bool
269    PrintInstructions (Disassembler *disasm_ptr,
270                       Debugger &debugger,
271                       const ArchSpec &arch,
272                       const ExecutionContext &exe_ctx,
273                       uint32_t num_instructions,
274                       uint32_t num_mixed_context_lines,
275                       uint32_t options,
276                       Stream &strm);
277
278    size_t
279    ParseInstructions (const ExecutionContext *exe_ctx,
280                       const AddressRange &range);
281
282    size_t
283    ParseInstructions (const ExecutionContext *exe_ctx,
284                       const Address &range,
285                       uint32_t num_instructions);
286
287    virtual size_t
288    DecodeInstructions (const Address &base_addr,
289                        const DataExtractor& data,
290                        uint32_t data_offset,
291                        uint32_t num_instructions,
292                        bool append) = 0;
293
294    InstructionList &
295    GetInstructionList ();
296
297    const InstructionList &
298    GetInstructionList () const;
299
300    const ArchSpec &
301    GetArchitecture () const
302    {
303        return m_arch;
304    }
305
306protected:
307    //------------------------------------------------------------------
308    // Classes that inherit from Disassembler can see and modify these
309    //------------------------------------------------------------------
310    const ArchSpec m_arch;
311    InstructionList m_instruction_list;
312    lldb::addr_t m_base_addr;
313
314private:
315    //------------------------------------------------------------------
316    // For Disassembler only
317    //------------------------------------------------------------------
318    DISALLOW_COPY_AND_ASSIGN (Disassembler);
319};
320
321} // namespace lldb_private
322
323#endif  // liblldb_Disassembler_h_
324