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/lldb-private.h"
16 #include "lldb/lldb-public.h"
17 #include "lldb/Core/ArchSpec.h"
18 #include "lldb/Core/PluginInterface.h"
19 #include "lldb/Core/Opcode.h"
20 #include "lldb/Core/RegisterValue.h"
22 //----------------------------------------------------------------------
23 /// @class EmulateInstruction EmulateInstruction.h "lldb/Core/EmulateInstruction.h"
24 /// @brief A class that allows emulation of CPU opcodes.
26 /// This class is a plug-in interface that is accessed through the
27 /// standard static FindPlugin function call in the EmulateInstruction
28 /// class. The FindPlugin takes a target triple and returns a new object
29 /// if there is a plug-in that supports the architecture and OS. Four
30 /// callbacks and a baton are provided. The four callbacks are read
31 /// register, write register, read memory and write memory.
33 /// This class is currently designed for these main use cases:
34 /// - Auto generation of Call Frame Information (CFI) from assembly code
35 /// - Predicting single step breakpoint locations
36 /// - Emulating instructions for breakpoint traps
38 /// Objects can be asked to read an instruction which will cause a call
39 /// to the read register callback to get the PC, followed by a read
40 /// memory call to read the opcode. If ReadInstruction () returns true,
41 /// then a call to EmulateInstruction::EvaluateInstruction () can be
42 /// made. At this point the EmulateInstruction subclass will use all of
43 /// the callbacks to emulate an instruction.
45 /// Clients that provide the callbacks can either do the read/write
46 /// registers/memory to actually emulate the instruction on a real or
47 /// virtual CPU, or watch for the EmulateInstruction::Context which
48 /// is context for the read/write register/memory which explains why
49 /// the callback is being called. Examples of a context are:
50 /// "pushing register 3 onto the stack at offset -12", or "adjusting
51 /// stack pointer by -16". This extra context allows the generation of
52 /// CFI information from assembly code without having to actually do
53 /// the read/write register/memory.
55 /// Clients must be prepared that not all instructions for an
56 /// Instruction Set Architecture (ISA) will be emulated.
58 /// Subclasses at the very least should implement the instructions that
59 /// save and restore registers onto the stack and adjustment to the stack
60 /// pointer. By just implementing a few instructions for an ISA that are
61 /// the typical prologue opcodes, you can then generate CFI using a
62 /// class that will soon be available.
64 /// Implementing all of the instructions that affect the PC can then
65 /// allow single step prediction support.
67 /// Implementing all of the instructions allows for emulation of opcodes
68 /// for breakpoint traps and will pave the way for "thread centric"
69 /// debugging. The current debugging model is "process centric" where
70 /// all threads must be stopped when any thread is stopped; when
71 /// hitting software breakpoints we must disable the breakpoint by
72 /// restoring the original breakpoint opcde, single stepping and
73 /// restoring the breakpoint trap. If all threads were allowed to run
74 /// then other threads could miss the breakpoint.
76 /// This class centralizes the code that usually is done in separate
77 /// code paths in a debugger (single step prediction, finding save
78 /// restore locations of registers for unwinding stack frame variables)
79 /// and emulating the intruction is just a bonus.
80 //----------------------------------------------------------------------
82 namespace lldb_private {
84 class EmulateInstruction :
85 public PluginInterface
89 static EmulateInstruction*
90 FindPlugin (const ArchSpec &arch,
91 InstructionType supported_inst_type,
92 const char *plugin_name);
97 // Read an instruciton opcode from memory
100 // Usually used for writing a register value whose source value is an
104 // Exclusively used when saving a register to the stack as part of the
106 eContextPushRegisterOnStack,
108 // Exclusively used when restoring a register off the stack as part of
110 eContextPopRegisterOffStack,
112 // Add or subtract a value from the stack
113 eContextAdjustStackPointer,
115 // Adjust the frame pointer for the current frame
116 eContextSetFramePointer,
118 // Add or subtract a value from a base address register (other than SP)
119 eContextAdjustBaseRegister,
121 // Add or subtract a value from the PC or store a value to the PC.
124 // Used in WriteRegister callbacks to indicate where the
125 eContextRegisterPlusOffset,
127 // Used in WriteMemory callback to indicate where the data came from
128 eContextRegisterStore,
130 eContextRegisterLoad,
132 // Used when performing a PC-relative branch where the
133 eContextRelativeBranchImmediate,
135 // Used when performing an absolute branch where the
136 eContextAbsoluteBranchRegister,
138 // Used when performing a supervisor call to an operating system to
139 // provide a service:
140 eContextSupervisorCall,
142 // Used when performing a MemU operation to read the PC-relative offset
144 eContextTableBranchReadMemory,
146 // Used when random bits are written into a register
147 eContextWriteRegisterRandomBits,
149 // Used when random bits are written to memory
150 eContextWriteMemoryRandomBits,
156 eContextReturnFromException
160 eInfoTypeRegisterPlusOffset,
161 eInfoTypeRegisterPlusIndirectOffset,
162 eInfoTypeRegisterToRegisterPlusOffset,
163 eInfoTypeRegisterToRegisterPlusIndirectOffset,
164 eInfoTypeRegisterRegisterOperands,
168 eInfoTypeImmediateSigned,
170 eInfoTypeISAAndImmediate,
171 eInfoTypeISAAndImmediateSigned,
179 enum InfoType info_type;
182 struct RegisterPlusOffset
184 RegisterInfo reg; // base register
185 int64_t signed_offset; // signed offset added to base register
186 } RegisterPlusOffset;
188 struct RegisterPlusIndirectOffset
190 RegisterInfo base_reg; // base register number
191 RegisterInfo offset_reg; // offset register kind
192 } RegisterPlusIndirectOffset;
194 struct RegisterToRegisterPlusOffset
196 RegisterInfo data_reg; // source/target register for data
197 RegisterInfo base_reg; // base register for address calculation
198 int64_t offset; // offset for address calculation
199 } RegisterToRegisterPlusOffset;
201 struct RegisterToRegisterPlusIndirectOffset
203 RegisterInfo base_reg; // base register for address calculation
204 RegisterInfo offset_reg; // offset register for address calculation
205 RegisterInfo data_reg; // source/target register for data
206 } RegisterToRegisterPlusIndirectOffset;
208 struct RegisterRegisterOperands
210 RegisterInfo operand1; // register containing first operand for binary op
211 RegisterInfo operand2; // register containing second operand for binary op
212 } RegisterRegisterOperands;
214 int64_t signed_offset; // signed offset by which to adjust self (for registers only)
216 RegisterInfo reg; // plain register
218 uint64_t unsigned_immediate;// unsigned immediate value
219 int64_t signed_immediate; // signed immediate value
221 lldb::addr_t address; // direct address
223 struct ISAAndImmediate
226 uint32_t unsigned_data32; // immdiate data
229 struct ISAAndImmediateSigned
232 int32_t signed_data32; // signed immdiate data
233 } ISAAndImmediateSigned;
240 type (eContextInvalid),
241 info_type (eInfoTypeNoArgs)
246 SetRegisterPlusOffset (RegisterInfo base_reg,
247 int64_t signed_offset)
249 info_type = eInfoTypeRegisterPlusOffset;
250 info.RegisterPlusOffset.reg = base_reg;
251 info.RegisterPlusOffset.signed_offset = signed_offset;
255 SetRegisterPlusIndirectOffset (RegisterInfo base_reg,
256 RegisterInfo offset_reg)
258 info_type = eInfoTypeRegisterPlusIndirectOffset;
259 info.RegisterPlusIndirectOffset.base_reg = base_reg;
260 info.RegisterPlusIndirectOffset.offset_reg = offset_reg;
264 SetRegisterToRegisterPlusOffset (RegisterInfo data_reg,
265 RegisterInfo base_reg,
268 info_type = eInfoTypeRegisterToRegisterPlusOffset;
269 info.RegisterToRegisterPlusOffset.data_reg = data_reg;
270 info.RegisterToRegisterPlusOffset.base_reg = base_reg;
271 info.RegisterToRegisterPlusOffset.offset = offset;
275 SetRegisterToRegisterPlusIndirectOffset (RegisterInfo base_reg,
276 RegisterInfo offset_reg,
277 RegisterInfo data_reg)
279 info_type = eInfoTypeRegisterToRegisterPlusIndirectOffset;
280 info.RegisterToRegisterPlusIndirectOffset.base_reg = base_reg;
281 info.RegisterToRegisterPlusIndirectOffset.offset_reg = offset_reg;
282 info.RegisterToRegisterPlusIndirectOffset.data_reg = data_reg;
286 SetRegisterRegisterOperands (RegisterInfo op1_reg,
287 RegisterInfo op2_reg)
289 info_type = eInfoTypeRegisterRegisterOperands;
290 info.RegisterRegisterOperands.operand1 = op1_reg;
291 info.RegisterRegisterOperands.operand2 = op2_reg;
295 SetOffset (int64_t signed_offset)
297 info_type = eInfoTypeOffset;
298 info.signed_offset = signed_offset;
302 SetRegister (RegisterInfo reg)
304 info_type = eInfoTypeRegister;
309 SetImmediate (uint64_t immediate)
311 info_type = eInfoTypeImmediate;
312 info.unsigned_immediate = immediate;
316 SetImmediateSigned (int64_t signed_immediate)
318 info_type = eInfoTypeImmediateSigned;
319 info.signed_immediate = signed_immediate;
323 SetAddress (lldb::addr_t address)
325 info_type = eInfoTypeAddress;
326 info.address = address;
329 SetISAAndImmediate (uint32_t isa, uint32_t data)
331 info_type = eInfoTypeISAAndImmediate;
332 info.ISAAndImmediate.isa = isa;
333 info.ISAAndImmediate.unsigned_data32 = data;
337 SetISAAndImmediateSigned (uint32_t isa, int32_t data)
339 info_type = eInfoTypeISAAndImmediateSigned;
340 info.ISAAndImmediateSigned.isa = isa;
341 info.ISAAndImmediateSigned.signed_data32 = data;
345 SetISA (uint32_t isa)
347 info_type = eInfoTypeISA;
354 info_type = eInfoTypeNoArgs;
359 EmulateInstruction *instruction) const;
363 typedef size_t (*ReadMemoryCallback) (EmulateInstruction *instruction,
365 const Context &context,
370 typedef size_t (*WriteMemoryCallback) (EmulateInstruction *instruction,
372 const Context &context,
377 typedef bool (*ReadRegisterCallback) (EmulateInstruction *instruction,
379 const RegisterInfo *reg_info,
380 RegisterValue ®_value);
382 typedef bool (*WriteRegisterCallback) (EmulateInstruction *instruction,
384 const Context &context,
385 const RegisterInfo *reg_info,
386 const RegisterValue ®_value);
388 EmulateInstruction (const ArchSpec &arch);
390 virtual ~EmulateInstruction()
393 //----------------------------------------------------------------------
394 // Mandatory overrides
395 //----------------------------------------------------------------------
397 SupportsEmulatingIntructionsOfType (InstructionType inst_type) = 0;
400 SetTargetTriple (const ArchSpec &arch) = 0;
403 ReadInstruction () = 0;
406 EvaluateInstruction (uint32_t evaluate_options) = 0;
409 TestEmulation (Stream *out_stream, ArchSpec &arch, OptionValueDictionary *test_data) = 0;
412 GetRegisterInfo (uint32_t reg_kind, uint32_t reg_num, RegisterInfo ®_info) = 0;
414 //----------------------------------------------------------------------
415 // Optional overrides
416 //----------------------------------------------------------------------
418 SetInstruction (const Opcode &insn_opcode, const Address &inst_addr, Target *target);
421 CreateFunctionEntryUnwind (UnwindPlan &unwind_plan);
424 TranslateRegister (uint32_t reg_kind, uint32_t reg_num, std::string ®_name);
426 //----------------------------------------------------------------------
427 // RegisterInfo variants
428 //----------------------------------------------------------------------
430 ReadRegister (const RegisterInfo *reg_info,
431 RegisterValue& reg_value);
434 ReadRegisterUnsigned (const RegisterInfo *reg_info,
439 WriteRegister (const Context &context,
440 const RegisterInfo *ref_info,
441 const RegisterValue& reg_value);
444 WriteRegisterUnsigned (const Context &context,
445 const RegisterInfo *reg_info,
448 //----------------------------------------------------------------------
449 // Register kind and number variants
450 //----------------------------------------------------------------------
452 ReadRegister (uint32_t reg_kind,
454 RegisterValue& reg_value);
457 WriteRegister (const Context &context,
460 const RegisterValue& reg_value);
463 ReadRegisterUnsigned (uint32_t reg_kind,
469 WriteRegisterUnsigned (const Context &context,
476 ReadMemory (const Context &context,
482 ReadMemoryUnsigned (const Context &context,
489 WriteMemory (const Context &context,
495 WriteMemoryUnsigned (const Context &context,
498 size_t uval_byte_size);
501 GetAddressByteSize () const
503 return m_arch.GetAddressByteSize();
507 GetByteOrder () const
509 return m_arch.GetByteOrder();
525 GetArchitecture () const
532 ReadMemoryFrame (EmulateInstruction *instruction,
534 const Context &context,
540 WriteMemoryFrame (EmulateInstruction *instruction,
542 const Context &context,
548 ReadRegisterFrame (EmulateInstruction *instruction,
550 const RegisterInfo *reg_info,
551 RegisterValue ®_value);
555 WriteRegisterFrame (EmulateInstruction *instruction,
557 const Context &context,
558 const RegisterInfo *reg_info,
559 const RegisterValue ®_value);
562 ReadMemoryDefault (EmulateInstruction *instruction,
564 const Context &context,
570 WriteMemoryDefault (EmulateInstruction *instruction,
572 const Context &context,
578 ReadRegisterDefault (EmulateInstruction *instruction,
580 const RegisterInfo *reg_info,
581 RegisterValue ®_value);
585 WriteRegisterDefault (EmulateInstruction *instruction,
587 const Context &context,
588 const RegisterInfo *reg_info,
589 const RegisterValue ®_value);
592 SetBaton (void *baton);
595 SetCallbacks (ReadMemoryCallback read_mem_callback,
596 WriteMemoryCallback write_mem_callback,
597 ReadRegisterCallback read_reg_callback,
598 WriteRegisterCallback write_reg_callback);
601 SetReadMemCallback (ReadMemoryCallback read_mem_callback);
604 SetWriteMemCallback (WriteMemoryCallback write_mem_callback);
607 SetReadRegCallback (ReadRegisterCallback read_reg_callback);
610 SetWriteRegCallback (WriteRegisterCallback write_reg_callback);
613 GetBestRegisterKindAndNumber (const RegisterInfo *reg_info,
618 GetInternalRegisterNumber (RegisterContext *reg_ctx,
619 const RegisterInfo ®_info);
624 ReadMemoryCallback m_read_mem_callback;
625 WriteMemoryCallback m_write_mem_callback;
626 ReadRegisterCallback m_read_reg_callback;
627 WriteRegisterCallback m_write_reg_callback;
633 //------------------------------------------------------------------
634 // For EmulateInstruction only
635 //------------------------------------------------------------------
636 DISALLOW_COPY_AND_ASSIGN (EmulateInstruction);
639 } // namespace lldb_private
641 #endif // lldb_EmulateInstruction_h_