Disassembler.h revision 269024
11539Srgrimes//===-- Disassembler.h ------------------------------------------*- C++ -*-===// 21539Srgrimes// 31539Srgrimes// The LLVM Compiler Infrastructure 41539Srgrimes// 51539Srgrimes// This file is distributed under the University of Illinois Open Source 61539Srgrimes// License. See LICENSE.TXT for details. 71539Srgrimes// 81539Srgrimes//===----------------------------------------------------------------------===// 91539Srgrimes 101539Srgrimes#ifndef liblldb_Disassembler_h_ 111539Srgrimes#define liblldb_Disassembler_h_ 121539Srgrimes 131539Srgrimes// C Includes 141539Srgrimes// C++ Includes 151539Srgrimes#include <vector> 161539Srgrimes#include <string> 171539Srgrimes 181539Srgrimes// Other libraries and framework includes 191539Srgrimes// Project includes 201539Srgrimes#include "lldb/lldb-private.h" 211539Srgrimes#include "lldb/Core/Address.h" 221539Srgrimes#include "lldb/Core/ArchSpec.h" 231539Srgrimes#include "lldb/Core/EmulateInstruction.h" 241539Srgrimes#include "lldb/Core/Opcode.h" 251539Srgrimes#include "lldb/Core/PluginInterface.h" 261539Srgrimes#include "lldb/Interpreter/OptionValue.h" 271539Srgrimes 281539Srgrimesnamespace lldb_private { 291539Srgrimes 301539Srgrimesclass Instruction 311539Srgrimes{ 321539Srgrimespublic: 331539Srgrimes Instruction (const Address &address, 341539Srgrimes lldb::AddressClass addr_class = lldb::eAddressClassInvalid); 351539Srgrimes 3623655Speter virtual 3750473Speter ~Instruction(); 381539Srgrimes 391539Srgrimes const Address & 401539Srgrimes GetAddress () const 411539Srgrimes { 421539Srgrimes return m_address; 431539Srgrimes } 4424897Sbde 451539Srgrimes const char * 461539Srgrimes GetMnemonic (const ExecutionContext* exe_ctx) 471539Srgrimes { 481539Srgrimes CalculateMnemonicOperandsAndCommentIfNeeded (exe_ctx); 491539Srgrimes return m_opcode_name.c_str(); 501539Srgrimes } 511539Srgrimes const char * 521539Srgrimes GetOperands (const ExecutionContext* exe_ctx) 531539Srgrimes { 541539Srgrimes CalculateMnemonicOperandsAndCommentIfNeeded (exe_ctx); 5524897Sbde return m_mnemonics.c_str(); 561539Srgrimes } 571539Srgrimes 581539Srgrimes const char * 591539Srgrimes GetComment (const ExecutionContext* exe_ctx) 601539Srgrimes { 611539Srgrimes CalculateMnemonicOperandsAndCommentIfNeeded (exe_ctx); 621539Srgrimes return m_comment.c_str(); 631539Srgrimes } 641539Srgrimes 651539Srgrimes virtual void 661539Srgrimes CalculateMnemonicOperandsAndComment (const ExecutionContext* exe_ctx) = 0; 671539Srgrimes 681539Srgrimes lldb::AddressClass 691539Srgrimes GetAddressClass (); 701539Srgrimes 7172529Simp void 7273254Sdeischen SetAddress (const Address &addr) 7372529Simp { 741539Srgrimes // Invalidate the address class to lazily discover 751539Srgrimes // it if we need to. 761539Srgrimes m_address_class = lldb::eAddressClassInvalid; 771539Srgrimes m_address = addr; 781539Srgrimes } 791539Srgrimes 801539Srgrimes virtual void 811539Srgrimes Dump (Stream *s, 821539Srgrimes uint32_t max_opcode_byte_size, 831539Srgrimes bool show_address, 841539Srgrimes bool show_bytes, 851539Srgrimes const ExecutionContext* exe_ctx); 861539Srgrimes 871539Srgrimes virtual bool 881539Srgrimes DoesBranch () = 0; 891539Srgrimes 901539Srgrimes virtual size_t 911539Srgrimes Decode (const Disassembler &disassembler, 921539Srgrimes const DataExtractor& data, 931539Srgrimes lldb::offset_t data_offset) = 0; 941539Srgrimes 951539Srgrimes virtual void 961539Srgrimes SetDescription (const char *) {} // May be overridden in sub-classes that have descriptions. 971539Srgrimes 981539Srgrimes lldb::OptionValueSP 991539Srgrimes ReadArray (FILE *in_file, Stream *out_stream, OptionValue::Type data_type); 1001539Srgrimes 1011539Srgrimes lldb::OptionValueSP 1021539Srgrimes ReadDictionary (FILE *in_file, Stream *out_stream); 1031539Srgrimes 1041539Srgrimes bool 1051539Srgrimes DumpEmulation (const ArchSpec &arch); 1061539Srgrimes 1071539Srgrimes virtual bool 1081539Srgrimes TestEmulation (Stream *stream, const char *test_file_name); 1091539Srgrimes 1101539Srgrimes bool 11193032Simp Emulate (const ArchSpec &arch, 11293032Simp uint32_t evaluate_options, 11393032Simp void *baton, 11493032Simp EmulateInstruction::ReadMemoryCallback read_mem_callback, 1151539Srgrimes EmulateInstruction::WriteMemoryCallback write_mem_calback, 1161539Srgrimes EmulateInstruction::ReadRegisterCallback read_reg_callback, 1171539Srgrimes EmulateInstruction::WriteRegisterCallback write_reg_callback); 11872529Simp 1191539Srgrimes const Opcode & 1201539Srgrimes GetOpcode () const 1211539Srgrimes { 1221539Srgrimes return m_opcode; 1231539Srgrimes } 1241539Srgrimes 1251539Srgrimes uint32_t 1261539Srgrimes GetData (DataExtractor &data); 1271539Srgrimes 1281539Srgrimesprotected: 1291539Srgrimes Address m_address; // The section offset address of this instruction 1301539Srgrimes // We include an address class in the Instruction class to 1311539Srgrimes // allow the instruction specify the eAddressClassCodeAlternateISA 1321539Srgrimes // (currently used for thumb), and also to specify data (eAddressClassData). 1331539Srgrimes // The usual value will be eAddressClassCode, but often when 13472529Simp // disassembling memory, you might run into data. This can 13581600Speter // help us to disassemble appropriately. 13681600Speterprivate: 13781600Speter lldb::AddressClass m_address_class; // Use GetAddressClass () accessor function! 1381539Srgrimesprotected: 1391539Srgrimes Opcode m_opcode; // The opcode for this instruction 1401539Srgrimes std::string m_opcode_name; 1411539Srgrimes std::string m_mnemonics; 1421539Srgrimes std::string m_comment; 1431539Srgrimes bool m_calculated_strings; 1441539Srgrimes 1451539Srgrimes void 1461539Srgrimes CalculateMnemonicOperandsAndCommentIfNeeded (const ExecutionContext* exe_ctx) 1471539Srgrimes { 1481539Srgrimes if (!m_calculated_strings) 1491539Srgrimes { 1501539Srgrimes m_calculated_strings = true; 15113771Smpp CalculateMnemonicOperandsAndComment(exe_ctx); 15213771Smpp } 1531539Srgrimes } 1541539Srgrimes}; 15537489Speter 15672372Sdeischen 1571539Srgrimesclass InstructionList 1581539Srgrimes{ 1591539Srgrimespublic: 1601539Srgrimes InstructionList(); 1611539Srgrimes ~InstructionList(); 1621539Srgrimes 1631539Srgrimes size_t 1641539Srgrimes GetSize() const; 1651539Srgrimes 1661539Srgrimes uint32_t 1671539Srgrimes GetMaxOpcocdeByteSize () const; 1681539Srgrimes 1691539Srgrimes lldb::InstructionSP 1701539Srgrimes GetInstructionAtIndex (size_t idx) const; 1711539Srgrimes 1721539Srgrimes uint32_t 1731539Srgrimes GetIndexOfNextBranchInstruction(uint32_t start) const; 1741539Srgrimes 1751539Srgrimes uint32_t 1761539Srgrimes GetIndexOfInstructionAtLoadAddress (lldb::addr_t load_addr, Target &target); 1771539Srgrimes 1781539Srgrimes uint32_t 1791539Srgrimes GetIndexOfInstructionAtAddress (const Address &addr); 1801539Srgrimes 1811539Srgrimes void 1821539Srgrimes Clear(); 1831539Srgrimes 184100133Swollman void 1851539Srgrimes Append (lldb::InstructionSP &inst_sp); 1861539Srgrimes 1871539Srgrimes void 1881539Srgrimes Dump (Stream *s, 1891539Srgrimes bool show_address, 1901539Srgrimes bool show_bytes, 1911539Srgrimes const ExecutionContext* exe_ctx); 1921539Srgrimes 1931539Srgrimesprivate: 1941539Srgrimes typedef std::vector<lldb::InstructionSP> collection; 1951539Srgrimes typedef collection::iterator iterator; 1961539Srgrimes typedef collection::const_iterator const_iterator; 1971539Srgrimes 1981539Srgrimes collection m_instructions; 1991539Srgrimes}; 20081600Speter 20183712Speterclass PseudoInstruction : 20272529Simp public Instruction 20372529Simp{ 20472529Simppublic: 20581600Speter 20681600Speter PseudoInstruction (); 20781600Speter 20881600Speter virtual 20981600Speter ~PseudoInstruction (); 2101539Srgrimes 211100133Swollman virtual bool 2121539Srgrimes DoesBranch (); 2131539Srgrimes 2141539Srgrimes virtual void 21593032Simp CalculateMnemonicOperandsAndComment (const ExecutionContext* exe_ctx) 21693032Simp { 21793032Simp // TODO: fill this in and put opcode name into Instruction::m_opcode_name, 21893032Simp // mnemonic into Instruction::m_mnemonics, and any comment into 21993032Simp // Instruction::m_comment 22093032Simp } 22193032Simp 22293032Simp virtual size_t 22393032Simp Decode (const Disassembler &disassembler, 22493032Simp const DataExtractor &data, 22593032Simp lldb::offset_t data_offset); 22693032Simp 22793032Simp void 22893032Simp SetOpcode (size_t opcode_size, void *opcode_data); 22993032Simp 23093032Simp virtual void 23193032Simp SetDescription (const char *description); 23293032Simp 23393032Simpprotected: 23493032Simp std::string m_description; 23593032Simp 23693032Simp DISALLOW_COPY_AND_ASSIGN (PseudoInstruction); 23793032Simp}; 23893032Simp 23993032Simpclass Disassembler : 24093032Simp public std::enable_shared_from_this<Disassembler>, 24193032Simp public PluginInterface 24293032Simp{ 24393032Simppublic: 24493032Simp 24593032Simp enum 24693032Simp { 24793032Simp eOptionNone = 0u, 24893032Simp eOptionShowBytes = (1u << 0), 24993032Simp eOptionRawOuput = (1u << 1), 25093032Simp eOptionMarkPCSourceLine = (1u << 2), // Mark the source line that contains the current PC (mixed mode only) 25193032Simp eOptionMarkPCAddress = (1u << 3) // Mark the disassembly line the contains the PC 25293032Simp }; 25393032Simp 25493032Simp enum HexImmediateStyle 25593032Simp { 2561539Srgrimes eHexStyleC, 257100133Swollman eHexStyleAsm, 258100133Swollman }; 259100133Swollman 260100133Swollman // FindPlugin should be lax about the flavor string (it is too annoying to have various internal uses of the 261100133Swollman // disassembler fail because the global flavor string gets set wrong. Instead, if you get a flavor string you 262100133Swollman // don't understand, use the default. Folks who care to check can use the FlavorValidForArchSpec method on the 2631539Srgrimes // disassembler they got back. 264100133Swollman static lldb::DisassemblerSP 2651539Srgrimes FindPlugin (const ArchSpec &arch, const char *flavor, const char *plugin_name); 266100133Swollman 26719211Swosch // This version will use the value in the Target settings if flavor is NULL; 26823260Sache static lldb::DisassemblerSP 2691539Srgrimes FindPluginForTarget(const lldb::TargetSP target_sp, const ArchSpec &arch, const char *flavor, const char *plugin_name); 27019211Swosch 27119211Swosch static lldb::DisassemblerSP 27293032Simp DisassembleRange (const ArchSpec &arch, 27393032Simp const char *plugin_name, 27493032Simp const char *flavor, 275100133Swollman const ExecutionContext &exe_ctx, 276100133Swollman const AddressRange &disasm_range, 277100133Swollman bool prefer_file_cache); 278100133Swollman 279100133Swollman static lldb::DisassemblerSP 280100133Swollman DisassembleBytes (const ArchSpec &arch, 281100133Swollman const char *plugin_name, 282100133Swollman const char *flavor, 28393032Simp const Address &start, 28493032Simp const void *bytes, 28593032Simp size_t length, 2861539Srgrimes uint32_t max_num_instructions, 2871539Srgrimes bool data_from_file); 288100133Swollman 289100133Swollman static bool 29024897Sbde Disassemble (Debugger &debugger, 291100133Swollman const ArchSpec &arch, 292100133Swollman const char *plugin_name, 293100133Swollman const char *flavor, 294100133Swollman const ExecutionContext &exe_ctx, 29524897Sbde const AddressRange &range, 296100133Swollman uint32_t num_instructions, 297100133Swollman uint32_t num_mixed_context_lines, 298100133Swollman uint32_t options, 299100133Swollman Stream &strm); 30024897Sbde 301100133Swollman static bool 302100133Swollman Disassemble (Debugger &debugger, 303100133Swollman const ArchSpec &arch, 304100133Swollman const char *plugin_name, 305100133Swollman const char *flavor, 306100133Swollman const ExecutionContext &exe_ctx, 307100133Swollman const Address &start, 308100133Swollman uint32_t num_instructions, 30924897Sbde uint32_t num_mixed_context_lines, 31024897Sbde uint32_t options, 31124897Sbde Stream &strm); 3121539Srgrimes 3131539Srgrimes static size_t 314100133Swollman Disassemble (Debugger &debugger, 31593032Simp const ArchSpec &arch, 31693032Simp const char *plugin_name, 31793032Simp const char *flavor, 31887369Sobrien const ExecutionContext &exe_ctx, 31987369Sobrien SymbolContextList &sc_list, 32087369Sobrien uint32_t num_instructions, 32187369Sobrien uint32_t num_mixed_context_lines, 32287369Sobrien uint32_t options, 32393032Simp Stream &strm); 32493032Simp 32593032Simp static bool 32693032Simp Disassemble (Debugger &debugger, 32793032Simp const ArchSpec &arch, 32837614Sbde const char *plugin_name, 32993032Simp const char *flavor, 33093032Simp const ExecutionContext &exe_ctx, 33137614Sbde const ConstString &name, 3321539Srgrimes Module *module, 3331539Srgrimes uint32_t num_instructions, 334100133Swollman uint32_t num_mixed_context_lines, 335100133Swollman uint32_t options, 336100133Swollman Stream &strm); 337100133Swollman 338100133Swollman static bool 339100133Swollman Disassemble (Debugger &debugger, 340100133Swollman const ArchSpec &arch, 341100133Swollman const char *plugin_name, 3421539Srgrimes const char *flavor, 34371580Sdeischen const ExecutionContext &exe_ctx, 3441539Srgrimes uint32_t num_instructions, 3451539Srgrimes uint32_t num_mixed_context_lines, 34671580Sdeischen uint32_t options, 3471539Srgrimes Stream &strm); 3481539Srgrimes 3491539Srgrimes //------------------------------------------------------------------ 3501539Srgrimes // Constructors and Destructors 35193032Simp //------------------------------------------------------------------ 35275818Sobrien Disassembler(const ArchSpec &arch, const char *flavor); 35375818Sobrien virtual ~Disassembler(); 35475818Sobrien 35593032Simp typedef const char * (*SummaryCallback)(const Instruction& inst, ExecutionContext *exe_context, void *user_data); 3561539Srgrimes 3571539Srgrimes static bool 3581539Srgrimes PrintInstructions (Disassembler *disasm_ptr, 3591539Srgrimes Debugger &debugger, 360100133Swollman const ArchSpec &arch, 361100133Swollman const ExecutionContext &exe_ctx, 362100133Swollman uint32_t num_instructions, 363100133Swollman uint32_t num_mixed_context_lines, 364100133Swollman uint32_t options, 365100133Swollman Stream &strm); 366100133Swollman 367100133Swollman size_t 368100133Swollman ParseInstructions (const ExecutionContext *exe_ctx, 369100133Swollman const AddressRange &range, 370100133Swollman Stream *error_strm_ptr, 371100133Swollman bool prefer_file_cache); 372100133Swollman 373100133Swollman size_t 374100133Swollman ParseInstructions (const ExecutionContext *exe_ctx, 375100133Swollman const Address &range, 376100133Swollman uint32_t num_instructions, 377100133Swollman bool prefer_file_cache); 378100133Swollman 379100133Swollman virtual size_t 380100133Swollman DecodeInstructions (const Address &base_addr, 3811539Srgrimes const DataExtractor& data, 3821539Srgrimes lldb::offset_t data_offset, 38393032Simp size_t num_instructions, 38493032Simp bool append, 38593032Simp bool data_from_file) = 0; 38693032Simp 3871539Srgrimes InstructionList & 3881539Srgrimes GetInstructionList (); 3898858Srgrimes 3901539Srgrimes const InstructionList & 3911539Srgrimes GetInstructionList () const; 3921539Srgrimes 3931539Srgrimes const ArchSpec & 3941539Srgrimes GetArchitecture () const 3951539Srgrimes { 3961539Srgrimes return m_arch; 3971539Srgrimes } 3981539Srgrimes 3991539Srgrimes const char * 4001539Srgrimes GetFlavor () const 4011539Srgrimes { 4021539Srgrimes return m_flavor.c_str(); 4031539Srgrimes } 4041539Srgrimes 4051539Srgrimes virtual bool 4061539Srgrimes FlavorValidForArchSpec (const lldb_private::ArchSpec &arch, const char *flavor) = 0; 4071539Srgrimes 4081539Srgrimesprotected: 4091539Srgrimes //------------------------------------------------------------------ 4101539Srgrimes // Classes that inherit from Disassembler can see and modify these 4111539Srgrimes //------------------------------------------------------------------ 4121539Srgrimes const ArchSpec m_arch; 4131539Srgrimes InstructionList m_instruction_list; 4141539Srgrimes lldb::addr_t m_base_addr; 4151539Srgrimes std::string m_flavor; 4161539Srgrimes 4171539Srgrimesprivate: 4181539Srgrimes //------------------------------------------------------------------ 41935127Sjb // For Disassembler only 42035127Sjb //------------------------------------------------------------------ 42135127Sjb DISALLOW_COPY_AND_ASSIGN (Disassembler); 42235127Sjb}; 42335127Sjb 42435127Sjb} // namespace lldb_private 42535127Sjb 42635127Sjb#endif // liblldb_Disassembler_h_ 42735127Sjb