1 //===-- EmulateInstruction.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 lldb_EmulateInstruction_h_
11 #define lldb_EmulateInstruction_h_
15 #include "lldb/Core/Address.h" // for Address
16 #include "lldb/Core/Opcode.h"
17 #include "lldb/Core/PluginInterface.h"
18 #include "lldb/Utility/ArchSpec.h"
19 #include "lldb/lldb-defines.h" // for DISALLOW_COPY_AND_ASSIGN
20 #include "lldb/lldb-enumerations.h" // for RegisterKind, ByteOrder
21 #include "lldb/lldb-private-enumerations.h" // for InstructionType
22 #include "lldb/lldb-private-types.h" // for RegisterInfo
23 #include "lldb/lldb-types.h" // for addr_t
25 #include <stddef.h> // for size_t
26 #include <stdint.h> // for uint32_t, uint64_t, int64_t
27 namespace lldb_private {
28 class OptionValueDictionary;
30 namespace lldb_private {
31 class RegisterContext;
33 namespace lldb_private {
36 namespace lldb_private {
39 namespace lldb_private {
42 namespace lldb_private {
46 namespace lldb_private {
48 //----------------------------------------------------------------------
49 /// @class EmulateInstruction EmulateInstruction.h
50 /// "lldb/Core/EmulateInstruction.h"
51 /// @brief A class that allows emulation of CPU opcodes.
53 /// This class is a plug-in interface that is accessed through the
54 /// standard static FindPlugin function call in the EmulateInstruction
55 /// class. The FindPlugin takes a target triple and returns a new object
56 /// if there is a plug-in that supports the architecture and OS. Four
57 /// callbacks and a baton are provided. The four callbacks are read
58 /// register, write register, read memory and write memory.
60 /// This class is currently designed for these main use cases:
61 /// - Auto generation of Call Frame Information (CFI) from assembly code
62 /// - Predicting single step breakpoint locations
63 /// - Emulating instructions for breakpoint traps
65 /// Objects can be asked to read an instruction which will cause a call
66 /// to the read register callback to get the PC, followed by a read
67 /// memory call to read the opcode. If ReadInstruction () returns true,
68 /// then a call to EmulateInstruction::EvaluateInstruction () can be
69 /// made. At this point the EmulateInstruction subclass will use all of
70 /// the callbacks to emulate an instruction.
72 /// Clients that provide the callbacks can either do the read/write
73 /// registers/memory to actually emulate the instruction on a real or
74 /// virtual CPU, or watch for the EmulateInstruction::Context which
75 /// is context for the read/write register/memory which explains why
76 /// the callback is being called. Examples of a context are:
77 /// "pushing register 3 onto the stack at offset -12", or "adjusting
78 /// stack pointer by -16". This extra context allows the generation of
79 /// CFI information from assembly code without having to actually do
80 /// the read/write register/memory.
82 /// Clients must be prepared that not all instructions for an
83 /// Instruction Set Architecture (ISA) will be emulated.
85 /// Subclasses at the very least should implement the instructions that
86 /// save and restore registers onto the stack and adjustment to the stack
87 /// pointer. By just implementing a few instructions for an ISA that are
88 /// the typical prologue opcodes, you can then generate CFI using a
89 /// class that will soon be available.
91 /// Implementing all of the instructions that affect the PC can then
92 /// allow single step prediction support.
94 /// Implementing all of the instructions allows for emulation of opcodes
95 /// for breakpoint traps and will pave the way for "thread centric"
96 /// debugging. The current debugging model is "process centric" where
97 /// all threads must be stopped when any thread is stopped; when
98 /// hitting software breakpoints we must disable the breakpoint by
99 /// restoring the original breakpoint opcode, single stepping and
100 /// restoring the breakpoint trap. If all threads were allowed to run
101 /// then other threads could miss the breakpoint.
103 /// This class centralizes the code that usually is done in separate
104 /// code paths in a debugger (single step prediction, finding save
105 /// restore locations of registers for unwinding stack frame variables)
106 /// and emulating the instruction is just a bonus.
107 //----------------------------------------------------------------------
109 class EmulateInstruction : public PluginInterface {
111 static EmulateInstruction *FindPlugin(const ArchSpec &arch,
112 InstructionType supported_inst_type,
113 const char *plugin_name);
117 // Read an instruction opcode from memory
120 // Usually used for writing a register value whose source value is an
124 // Exclusively used when saving a register to the stack as part of the
126 eContextPushRegisterOnStack,
128 // Exclusively used when restoring a register off the stack as part of
130 eContextPopRegisterOffStack,
132 // Add or subtract a value from the stack
133 eContextAdjustStackPointer,
135 // Adjust the frame pointer for the current frame
136 eContextSetFramePointer,
138 // Typically in an epilogue sequence. Copy the frame pointer back
139 // into the stack pointer, use SP for CFA calculations again.
140 eContextRestoreStackPointer,
142 // Add or subtract a value from a base address register (other than SP)
143 eContextAdjustBaseRegister,
145 // Add or subtract a value from the PC or store a value to the PC.
148 // Used in WriteRegister callbacks to indicate where the
149 eContextRegisterPlusOffset,
151 // Used in WriteMemory callback to indicate where the data came from
152 eContextRegisterStore,
154 eContextRegisterLoad,
156 // Used when performing a PC-relative branch where the
157 eContextRelativeBranchImmediate,
159 // Used when performing an absolute branch where the
160 eContextAbsoluteBranchRegister,
162 // Used when performing a supervisor call to an operating system to
163 // provide a service:
164 eContextSupervisorCall,
166 // Used when performing a MemU operation to read the PC-relative offset
168 eContextTableBranchReadMemory,
170 // Used when random bits are written into a register
171 eContextWriteRegisterRandomBits,
173 // Used when random bits are written to memory
174 eContextWriteMemoryRandomBits,
180 eContextReturnFromException
184 eInfoTypeRegisterPlusOffset,
185 eInfoTypeRegisterPlusIndirectOffset,
186 eInfoTypeRegisterToRegisterPlusOffset,
187 eInfoTypeRegisterToRegisterPlusIndirectOffset,
188 eInfoTypeRegisterRegisterOperands,
192 eInfoTypeImmediateSigned,
194 eInfoTypeISAAndImmediate,
195 eInfoTypeISAAndImmediateSigned,
202 enum InfoType info_type;
204 struct RegisterPlusOffset {
205 RegisterInfo reg; // base register
206 int64_t signed_offset; // signed offset added to base register
207 } RegisterPlusOffset;
209 struct RegisterPlusIndirectOffset {
210 RegisterInfo base_reg; // base register number
211 RegisterInfo offset_reg; // offset register kind
212 } RegisterPlusIndirectOffset;
214 struct RegisterToRegisterPlusOffset {
215 RegisterInfo data_reg; // source/target register for data
216 RegisterInfo base_reg; // base register for address calculation
217 int64_t offset; // offset for address calculation
218 } RegisterToRegisterPlusOffset;
220 struct RegisterToRegisterPlusIndirectOffset {
221 RegisterInfo base_reg; // base register for address calculation
222 RegisterInfo offset_reg; // offset register for address calculation
223 RegisterInfo data_reg; // source/target register for data
224 } RegisterToRegisterPlusIndirectOffset;
226 struct RegisterRegisterOperands {
228 operand1; // register containing first operand for binary op
230 operand2; // register containing second operand for binary op
231 } RegisterRegisterOperands;
233 int64_t signed_offset; // signed offset by which to adjust self (for
236 RegisterInfo reg; // plain register
238 uint64_t unsigned_immediate; // unsigned immediate value
239 int64_t signed_immediate; // signed immediate value
241 lldb::addr_t address; // direct address
243 struct ISAAndImmediate {
245 uint32_t unsigned_data32; // immediate data
248 struct ISAAndImmediateSigned {
250 int32_t signed_data32; // signed immediate data
251 } ISAAndImmediateSigned;
256 Context() : type(eContextInvalid), info_type(eInfoTypeNoArgs) {}
258 void SetRegisterPlusOffset(RegisterInfo base_reg, int64_t signed_offset) {
259 info_type = eInfoTypeRegisterPlusOffset;
260 info.RegisterPlusOffset.reg = base_reg;
261 info.RegisterPlusOffset.signed_offset = signed_offset;
264 void SetRegisterPlusIndirectOffset(RegisterInfo base_reg,
265 RegisterInfo offset_reg) {
266 info_type = eInfoTypeRegisterPlusIndirectOffset;
267 info.RegisterPlusIndirectOffset.base_reg = base_reg;
268 info.RegisterPlusIndirectOffset.offset_reg = offset_reg;
271 void SetRegisterToRegisterPlusOffset(RegisterInfo data_reg,
272 RegisterInfo base_reg,
274 info_type = eInfoTypeRegisterToRegisterPlusOffset;
275 info.RegisterToRegisterPlusOffset.data_reg = data_reg;
276 info.RegisterToRegisterPlusOffset.base_reg = base_reg;
277 info.RegisterToRegisterPlusOffset.offset = offset;
280 void SetRegisterToRegisterPlusIndirectOffset(RegisterInfo base_reg,
281 RegisterInfo offset_reg,
282 RegisterInfo data_reg) {
283 info_type = eInfoTypeRegisterToRegisterPlusIndirectOffset;
284 info.RegisterToRegisterPlusIndirectOffset.base_reg = base_reg;
285 info.RegisterToRegisterPlusIndirectOffset.offset_reg = offset_reg;
286 info.RegisterToRegisterPlusIndirectOffset.data_reg = data_reg;
289 void SetRegisterRegisterOperands(RegisterInfo op1_reg,
290 RegisterInfo op2_reg) {
291 info_type = eInfoTypeRegisterRegisterOperands;
292 info.RegisterRegisterOperands.operand1 = op1_reg;
293 info.RegisterRegisterOperands.operand2 = op2_reg;
296 void SetOffset(int64_t signed_offset) {
297 info_type = eInfoTypeOffset;
298 info.signed_offset = signed_offset;
301 void SetRegister(RegisterInfo reg) {
302 info_type = eInfoTypeRegister;
306 void SetImmediate(uint64_t immediate) {
307 info_type = eInfoTypeImmediate;
308 info.unsigned_immediate = immediate;
311 void SetImmediateSigned(int64_t signed_immediate) {
312 info_type = eInfoTypeImmediateSigned;
313 info.signed_immediate = signed_immediate;
316 void SetAddress(lldb::addr_t address) {
317 info_type = eInfoTypeAddress;
318 info.address = address;
320 void SetISAAndImmediate(uint32_t isa, uint32_t data) {
321 info_type = eInfoTypeISAAndImmediate;
322 info.ISAAndImmediate.isa = isa;
323 info.ISAAndImmediate.unsigned_data32 = data;
326 void SetISAAndImmediateSigned(uint32_t isa, int32_t data) {
327 info_type = eInfoTypeISAAndImmediateSigned;
328 info.ISAAndImmediateSigned.isa = isa;
329 info.ISAAndImmediateSigned.signed_data32 = data;
332 void SetISA(uint32_t isa) {
333 info_type = eInfoTypeISA;
337 void SetNoArgs() { info_type = eInfoTypeNoArgs; }
339 void Dump(Stream &s, EmulateInstruction *instruction) const;
342 typedef size_t (*ReadMemoryCallback)(EmulateInstruction *instruction,
343 void *baton, const Context &context,
344 lldb::addr_t addr, void *dst,
347 typedef size_t (*WriteMemoryCallback)(EmulateInstruction *instruction,
348 void *baton, const Context &context,
349 lldb::addr_t addr, const void *dst,
352 typedef bool (*ReadRegisterCallback)(EmulateInstruction *instruction,
354 const RegisterInfo *reg_info,
355 RegisterValue ®_value);
357 typedef bool (*WriteRegisterCallback)(EmulateInstruction *instruction,
358 void *baton, const Context &context,
359 const RegisterInfo *reg_info,
360 const RegisterValue ®_value);
362 // Type to represent the condition of an instruction. The UINT32 value is
364 // unconditional case and all other value can be used in an architecture
366 typedef uint32_t InstructionCondition;
367 static const InstructionCondition UnconditionalCondition = UINT32_MAX;
369 EmulateInstruction(const ArchSpec &arch);
371 ~EmulateInstruction() override = default;
373 //----------------------------------------------------------------------
374 // Mandatory overrides
375 //----------------------------------------------------------------------
377 SupportsEmulatingInstructionsOfType(InstructionType inst_type) = 0;
379 virtual bool SetTargetTriple(const ArchSpec &arch) = 0;
381 virtual bool ReadInstruction() = 0;
383 virtual bool EvaluateInstruction(uint32_t evaluate_options) = 0;
385 virtual InstructionCondition GetInstructionCondition() {
386 return UnconditionalCondition;
389 virtual bool TestEmulation(Stream *out_stream, ArchSpec &arch,
390 OptionValueDictionary *test_data) = 0;
392 virtual bool GetRegisterInfo(lldb::RegisterKind reg_kind, uint32_t reg_num,
393 RegisterInfo ®_info) = 0;
395 //----------------------------------------------------------------------
396 // Optional overrides
397 //----------------------------------------------------------------------
398 virtual bool SetInstruction(const Opcode &insn_opcode,
399 const Address &inst_addr, Target *target);
401 virtual bool CreateFunctionEntryUnwind(UnwindPlan &unwind_plan);
403 static const char *TranslateRegister(lldb::RegisterKind reg_kind,
404 uint32_t reg_num, std::string ®_name);
406 //----------------------------------------------------------------------
407 // RegisterInfo variants
408 //----------------------------------------------------------------------
409 bool ReadRegister(const RegisterInfo *reg_info, RegisterValue ®_value);
411 uint64_t ReadRegisterUnsigned(const RegisterInfo *reg_info,
412 uint64_t fail_value, bool *success_ptr);
414 bool WriteRegister(const Context &context, const RegisterInfo *ref_info,
415 const RegisterValue ®_value);
417 bool WriteRegisterUnsigned(const Context &context,
418 const RegisterInfo *reg_info, uint64_t reg_value);
420 //----------------------------------------------------------------------
421 // Register kind and number variants
422 //----------------------------------------------------------------------
423 bool ReadRegister(lldb::RegisterKind reg_kind, uint32_t reg_num,
424 RegisterValue ®_value);
426 bool WriteRegister(const Context &context, lldb::RegisterKind reg_kind,
427 uint32_t reg_num, const RegisterValue ®_value);
429 uint64_t ReadRegisterUnsigned(lldb::RegisterKind reg_kind, uint32_t reg_num,
430 uint64_t fail_value, bool *success_ptr);
432 bool WriteRegisterUnsigned(const Context &context,
433 lldb::RegisterKind reg_kind, uint32_t reg_num,
436 size_t ReadMemory(const Context &context, lldb::addr_t addr, void *dst,
439 uint64_t ReadMemoryUnsigned(const Context &context, lldb::addr_t addr,
440 size_t byte_size, uint64_t fail_value,
443 bool WriteMemory(const Context &context, lldb::addr_t addr, const void *src,
446 bool WriteMemoryUnsigned(const Context &context, lldb::addr_t addr,
447 uint64_t uval, size_t uval_byte_size);
449 uint32_t GetAddressByteSize() const { return m_arch.GetAddressByteSize(); }
451 lldb::ByteOrder GetByteOrder() const { return m_arch.GetByteOrder(); }
453 const Opcode &GetOpcode() const { return m_opcode; }
455 lldb::addr_t GetAddress() const { return m_addr; }
457 const ArchSpec &GetArchitecture() const { return m_arch; }
459 static size_t ReadMemoryFrame(EmulateInstruction *instruction, void *baton,
460 const Context &context, lldb::addr_t addr,
461 void *dst, size_t length);
463 static size_t WriteMemoryFrame(EmulateInstruction *instruction, void *baton,
464 const Context &context, lldb::addr_t addr,
465 const void *dst, size_t length);
467 static bool ReadRegisterFrame(EmulateInstruction *instruction, void *baton,
468 const RegisterInfo *reg_info,
469 RegisterValue ®_value);
471 static bool WriteRegisterFrame(EmulateInstruction *instruction, void *baton,
472 const Context &context,
473 const RegisterInfo *reg_info,
474 const RegisterValue ®_value);
476 static size_t ReadMemoryDefault(EmulateInstruction *instruction, void *baton,
477 const Context &context, lldb::addr_t addr,
478 void *dst, size_t length);
480 static size_t WriteMemoryDefault(EmulateInstruction *instruction, void *baton,
481 const Context &context, lldb::addr_t addr,
482 const void *dst, size_t length);
484 static bool ReadRegisterDefault(EmulateInstruction *instruction, void *baton,
485 const RegisterInfo *reg_info,
486 RegisterValue ®_value);
488 static bool WriteRegisterDefault(EmulateInstruction *instruction, void *baton,
489 const Context &context,
490 const RegisterInfo *reg_info,
491 const RegisterValue ®_value);
493 void SetBaton(void *baton);
495 void SetCallbacks(ReadMemoryCallback read_mem_callback,
496 WriteMemoryCallback write_mem_callback,
497 ReadRegisterCallback read_reg_callback,
498 WriteRegisterCallback write_reg_callback);
500 void SetReadMemCallback(ReadMemoryCallback read_mem_callback);
502 void SetWriteMemCallback(WriteMemoryCallback write_mem_callback);
504 void SetReadRegCallback(ReadRegisterCallback read_reg_callback);
506 void SetWriteRegCallback(WriteRegisterCallback write_reg_callback);
508 static bool GetBestRegisterKindAndNumber(const RegisterInfo *reg_info,
509 lldb::RegisterKind ®_kind,
512 static uint32_t GetInternalRegisterNumber(RegisterContext *reg_ctx,
513 const RegisterInfo ®_info);
518 ReadMemoryCallback m_read_mem_callback;
519 WriteMemoryCallback m_write_mem_callback;
520 ReadRegisterCallback m_read_reg_callback;
521 WriteRegisterCallback m_write_reg_callback;
526 //------------------------------------------------------------------
527 // For EmulateInstruction only
528 //------------------------------------------------------------------
529 DISALLOW_COPY_AND_ASSIGN(EmulateInstruction);
532 } // namespace lldb_private
534 #endif // lldb_EmulateInstruction_h_