Sean Callanan | 95e5c63 | 2012-02-17 00:53:45 +0000 | [diff] [blame] | 1 | //===-- DisassemblerLLVMC.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_DisassemblerLLVMC_h_ |
| 11 | #define liblldb_DisassemblerLLVMC_h_ |
| 12 | |
Jim Ingham | 0f063ba | 2013-03-02 00:26:47 +0000 | [diff] [blame^] | 13 | #include <string> |
Sean Callanan | 95e5c63 | 2012-02-17 00:53:45 +0000 | [diff] [blame] | 14 | |
| 15 | #include "llvm-c/Disassembler.h" |
| 16 | |
Jim Ingham | 0f063ba | 2013-03-02 00:26:47 +0000 | [diff] [blame^] | 17 | // Opaque references to C++ Objects in LLVM's MC. |
| 18 | namespace llvm |
| 19 | { |
| 20 | class MCContext; |
| 21 | class MCInst; |
| 22 | class MCInstrInfo; |
| 23 | class MCRegisterInfo; |
| 24 | class MCDisassembler; |
| 25 | class MCInstPrinter; |
| 26 | class MCAsmInfo; |
| 27 | class MCSubtargetInfo; |
| 28 | } |
| 29 | |
Sean Callanan | 95e5c63 | 2012-02-17 00:53:45 +0000 | [diff] [blame] | 30 | #include "lldb/Core/Address.h" |
| 31 | #include "lldb/Core/Disassembler.h" |
| 32 | #include "lldb/Core/PluginManager.h" |
| 33 | #include "lldb/Host/Mutex.h" |
| 34 | |
| 35 | class InstructionLLVMC; |
| 36 | |
| 37 | class DisassemblerLLVMC : public lldb_private::Disassembler |
| 38 | { |
Jim Ingham | 0f063ba | 2013-03-02 00:26:47 +0000 | [diff] [blame^] | 39 | // Since we need to make two actual MC Disassemblers for ARM (ARM & THUMB), and there's a bit of goo to set up and own |
| 40 | // in the MC disassembler world, I added this class to manage the actual disassemblers. |
| 41 | class LLVMCDisassembler |
| 42 | { |
| 43 | public: |
| 44 | LLVMCDisassembler (const char *triple, unsigned flavor, DisassemblerLLVMC &owner); |
| 45 | |
| 46 | ~LLVMCDisassembler() {}; |
| 47 | |
| 48 | uint64_t GetMCInst (uint8_t *opcode_data, size_t opcode_data_len, lldb::addr_t pc, llvm::MCInst &mc_inst); |
| 49 | uint64_t PrintMCInst (llvm::MCInst &mc_inst, char *output_buffer, size_t out_buffer_len); |
| 50 | bool CanBranch (llvm::MCInst &mc_inst); |
| 51 | bool IsValid() |
| 52 | { |
| 53 | return m_is_valid; |
| 54 | } |
| 55 | |
| 56 | private: |
| 57 | bool m_is_valid; |
| 58 | std::auto_ptr<llvm::MCContext> m_context_ap; |
| 59 | std::auto_ptr<llvm::MCAsmInfo> m_asm_info_ap; |
| 60 | std::auto_ptr<llvm::MCSubtargetInfo> m_subtarget_info_ap; |
| 61 | std::auto_ptr<llvm::MCInstrInfo> m_instr_info_ap; |
| 62 | std::auto_ptr<llvm::MCRegisterInfo> m_reg_info_ap; |
| 63 | std::auto_ptr<llvm::MCInstPrinter> m_instr_printer_ap; |
| 64 | std::auto_ptr<llvm::MCDisassembler> m_disasm_ap; |
| 65 | }; |
| 66 | |
Sean Callanan | 95e5c63 | 2012-02-17 00:53:45 +0000 | [diff] [blame] | 67 | public: |
| 68 | //------------------------------------------------------------------ |
| 69 | // Static Functions |
| 70 | //------------------------------------------------------------------ |
| 71 | static void |
| 72 | Initialize(); |
| 73 | |
| 74 | static void |
| 75 | Terminate(); |
| 76 | |
| 77 | static const char * |
| 78 | GetPluginNameStatic(); |
| 79 | |
| 80 | static const char * |
| 81 | GetPluginDescriptionStatic(); |
| 82 | |
| 83 | static lldb_private::Disassembler * |
Jim Ingham | 0f063ba | 2013-03-02 00:26:47 +0000 | [diff] [blame^] | 84 | CreateInstance(const lldb_private::ArchSpec &arch, const char *flavor); |
| 85 | |
| 86 | DisassemblerLLVMC(const lldb_private::ArchSpec &arch, const char *flavor /* = NULL */); |
Sean Callanan | 95e5c63 | 2012-02-17 00:53:45 +0000 | [diff] [blame] | 87 | |
| 88 | virtual |
| 89 | ~DisassemblerLLVMC(); |
| 90 | |
| 91 | size_t |
| 92 | DecodeInstructions (const lldb_private::Address &base_addr, |
| 93 | const lldb_private::DataExtractor& data, |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 94 | lldb::offset_t data_offset, |
| 95 | size_t num_instructions, |
Sean Callanan | 95e5c63 | 2012-02-17 00:53:45 +0000 | [diff] [blame] | 96 | bool append); |
| 97 | |
| 98 | //------------------------------------------------------------------ |
| 99 | // PluginInterface protocol |
| 100 | //------------------------------------------------------------------ |
| 101 | virtual const char * |
| 102 | GetPluginName(); |
| 103 | |
| 104 | virtual const char * |
| 105 | GetShortPluginName(); |
| 106 | |
| 107 | virtual uint32_t |
| 108 | GetPluginVersion(); |
| 109 | |
| 110 | protected: |
| 111 | friend class InstructionLLVMC; |
| 112 | |
Jim Ingham | 0f063ba | 2013-03-02 00:26:47 +0000 | [diff] [blame^] | 113 | virtual bool |
| 114 | FlavorValidForArchSpec (const lldb_private::ArchSpec &arch, const char *flavor); |
| 115 | |
Sean Callanan | 95e5c63 | 2012-02-17 00:53:45 +0000 | [diff] [blame] | 116 | bool |
| 117 | IsValid() |
| 118 | { |
Jim Ingham | 0f063ba | 2013-03-02 00:26:47 +0000 | [diff] [blame^] | 119 | return (m_disasm_ap.get() != NULL && m_disasm_ap->IsValid()); |
Sean Callanan | 95e5c63 | 2012-02-17 00:53:45 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | int OpInfo(uint64_t PC, |
| 123 | uint64_t Offset, |
| 124 | uint64_t Size, |
| 125 | int TagType, |
| 126 | void *TagBug); |
| 127 | |
| 128 | const char *SymbolLookup (uint64_t ReferenceValue, |
| 129 | uint64_t *ReferenceType, |
| 130 | uint64_t ReferencePC, |
| 131 | const char **ReferenceName); |
| 132 | |
| 133 | static int OpInfoCallback (void *DisInfo, |
| 134 | uint64_t PC, |
| 135 | uint64_t Offset, |
| 136 | uint64_t Size, |
| 137 | int TagType, |
| 138 | void *TagBug); |
| 139 | |
| 140 | static const char *SymbolLookupCallback(void *DisInfo, |
| 141 | uint64_t ReferenceValue, |
| 142 | uint64_t *ReferenceType, |
| 143 | uint64_t ReferencePC, |
| 144 | const char **ReferenceName); |
| 145 | |
| 146 | void Lock(InstructionLLVMC *inst, |
Greg Clayton | ba812f4 | 2012-05-10 02:52:23 +0000 | [diff] [blame] | 147 | const lldb_private::ExecutionContext *exe_ctx) |
Sean Callanan | 95e5c63 | 2012-02-17 00:53:45 +0000 | [diff] [blame] | 148 | { |
| 149 | m_mutex.Lock(); |
Sean Callanan | 95e5c63 | 2012-02-17 00:53:45 +0000 | [diff] [blame] | 150 | m_inst = inst; |
Greg Clayton | ba812f4 | 2012-05-10 02:52:23 +0000 | [diff] [blame] | 151 | m_exe_ctx = exe_ctx; |
Sean Callanan | 95e5c63 | 2012-02-17 00:53:45 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | void Unlock() |
| 155 | { |
Sean Callanan | 95e5c63 | 2012-02-17 00:53:45 +0000 | [diff] [blame] | 156 | m_inst = NULL; |
Greg Clayton | ba812f4 | 2012-05-10 02:52:23 +0000 | [diff] [blame] | 157 | m_exe_ctx = NULL; |
Sean Callanan | 95e5c63 | 2012-02-17 00:53:45 +0000 | [diff] [blame] | 158 | m_mutex.Unlock(); |
| 159 | } |
| 160 | |
Greg Clayton | ba812f4 | 2012-05-10 02:52:23 +0000 | [diff] [blame] | 161 | const lldb_private::ExecutionContext *m_exe_ctx; |
| 162 | InstructionLLVMC *m_inst; |
| 163 | lldb_private::Mutex m_mutex; |
Jim Ingham | 0f063ba | 2013-03-02 00:26:47 +0000 | [diff] [blame^] | 164 | |
| 165 | std::auto_ptr<LLVMCDisassembler> m_disasm_ap; |
| 166 | std::auto_ptr<LLVMCDisassembler> m_alternate_disasm_ap; |
Sean Callanan | 95e5c63 | 2012-02-17 00:53:45 +0000 | [diff] [blame] | 167 | }; |
| 168 | |
| 169 | #endif // liblldb_DisassemblerLLVM_h_ |