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/ArchSpec.h"
16 #include "lldb/Core/Opcode.h"
17 #include "lldb/Core/PluginInterface.h"
19 #include "lldb/Core/Address.h" // for Address
20 #include "lldb/lldb-defines.h" // for DISALLOW_COPY_AND_ASSIGN
21 #include "lldb/lldb-enumerations.h" // for RegisterKind, ByteOrder
22 #include "lldb/lldb-private-enumerations.h" // for InstructionType
23 #include "lldb/lldb-private-types.h" // for RegisterInfo
24 #include "lldb/lldb-types.h" // for addr_t
26 #include <stddef.h> // for size_t
27 #include <stdint.h> // for uint32_t, uint64_t, int64_t
28 namespace lldb_private {
29 class OptionValueDictionary;
31 namespace lldb_private {
32 class RegisterContext;
34 namespace lldb_private {
37 namespace lldb_private {
40 namespace lldb_private {
43 namespace lldb_private {
47 namespace lldb_private {
49 //----------------------------------------------------------------------
50 /// @class EmulateInstruction EmulateInstruction.h
51 /// "lldb/Core/EmulateInstruction.h"
52 /// @brief A class that allows emulation of CPU opcodes.
54 /// This class is a plug-in interface that is accessed through the
55 /// standard static FindPlugin function call in the EmulateInstruction
56 /// class. The FindPlugin takes a target triple and returns a new object
57 /// if there is a plug-in that supports the architecture and OS. Four
58 /// callbacks and a baton are provided. The four callbacks are read
59 /// register, write register, read memory and write memory.
61 /// This class is currently designed for these main use cases:
62 /// - Auto generation of Call Frame Information (CFI) from assembly code
63 /// - Predicting single step breakpoint locations
64 /// - Emulating instructions for breakpoint traps
66 /// Objects can be asked to read an instruction which will cause a call
67 /// to the read register callback to get the PC, followed by a read
68 /// memory call to read the opcode. If ReadInstruction () returns true,
69 /// then a call to EmulateInstruction::EvaluateInstruction () can be
70 /// made. At this point the EmulateInstruction subclass will use all of
71 /// the callbacks to emulate an instruction.
73 /// Clients that provide the callbacks can either do the read/write
74 /// registers/memory to actually emulate the instruction on a real or
75 /// virtual CPU, or watch for the EmulateInstruction::Context which
76 /// is context for the read/write register/memory which explains why
77 /// the callback is being called. Examples of a context are:
78 /// "pushing register 3 onto the stack at offset -12", or "adjusting
79 /// stack pointer by -16". This extra context allows the generation of
80 /// CFI information from assembly code without having to actually do
81 /// the read/write register/memory.
83 /// Clients must be prepared that not all instructions for an
84 /// Instruction Set Architecture (ISA) will be emulated.
86 /// Subclasses at the very least should implement the instructions that
87 /// save and restore registers onto the stack and adjustment to the stack
88 /// pointer. By just implementing a few instructions for an ISA that are
89 /// the typical prologue opcodes, you can then generate CFI using a
90 /// class that will soon be available.
92 /// Implementing all of the instructions that affect the PC can then
93 /// allow single step prediction support.
95 /// Implementing all of the instructions allows for emulation of opcodes
96 /// for breakpoint traps and will pave the way for "thread centric"
97 /// debugging. The current debugging model is "process centric" where
98 /// all threads must be stopped when any thread is stopped; when
99 /// hitting software breakpoints we must disable the breakpoint by
100 /// restoring the original breakpoint opcode, single stepping and
101 /// restoring the breakpoint trap. If all threads were allowed to run
102 /// then other threads could miss the breakpoint.
104 /// This class centralizes the code that usually is done in separate
105 /// code paths in a debugger (single step prediction, finding save
106 /// restore locations of registers for unwinding stack frame variables)
107 /// and emulating the instruction is just a bonus.
108 //----------------------------------------------------------------------
110 class EmulateInstruction : public PluginInterface {
112 static EmulateInstruction *FindPlugin(const ArchSpec &arch,
113 InstructionType supported_inst_type,
114 const char *plugin_name);
118 // Read an instruction opcode from memory
121 // Usually used for writing a register value whose source value is an
125 // Exclusively used when saving a register to the stack as part of the
127 eContextPushRegisterOnStack,
129 // Exclusively used when restoring a register off the stack as part of
131 eContextPopRegisterOffStack,
133 // Add or subtract a value from the stack
134 eContextAdjustStackPointer,
136 // Adjust the frame pointer for the current frame
137 eContextSetFramePointer,
139 // Typically in an epilogue sequence. Copy the frame pointer back
140 // into the stack pointer, use SP for CFA calculations again.
141 eContextRestoreStackPointer,
143 // Add or subtract a value from a base address register (other than SP)
144 eContextAdjustBaseRegister,
146 // Add or subtract a value from the PC or store a value to the PC.
149 // Used in WriteRegister callbacks to indicate where the
150 eContextRegisterPlusOffset,
152 // Used in WriteMemory callback to indicate where the data came from
153 eContextRegisterStore,
155 eContextRegisterLoad,
157 // Used when performing a PC-relative branch where the
158 eContextRelativeBranchImmediate,
160 // Used when performing an absolute branch where the
161 eContextAbsoluteBranchRegister,
163 // Used when performing a supervisor call to an operating system to
164 // provide a service:
165 eContextSupervisorCall,
167 // Used when performing a MemU operation to read the PC-relative offset
169 eContextTableBranchReadMemory,
171 // Used when random bits are written into a register
172 eContextWriteRegisterRandomBits,
174 // Used when random bits are written to memory
175 eContextWriteMemoryRandomBits,
181 eContextReturnFromException
185 eInfoTypeRegisterPlusOffset,
186 eInfoTypeRegisterPlusIndirectOffset,
187 eInfoTypeRegisterToRegisterPlusOffset,
188 eInfoTypeRegisterToRegisterPlusIndirectOffset,
189 eInfoTypeRegisterRegisterOperands,
193 eInfoTypeImmediateSigned,
195 eInfoTypeISAAndImmediate,
196 eInfoTypeISAAndImmediateSigned,
203 enum InfoType info_type;
205 struct RegisterPlusOffset {
206 RegisterInfo reg; // base register
207 int64_t signed_offset; // signed offset added to base register
208 } RegisterPlusOffset;
210 struct RegisterPlusIndirectOffset {
211 RegisterInfo base_reg; // base register number
212 RegisterInfo offset_reg; // offset register kind
213 } RegisterPlusIndirectOffset;
215 struct RegisterToRegisterPlusOffset {
216 RegisterInfo data_reg; // source/target register for data
217 RegisterInfo base_reg; // base register for address calculation
218 int64_t offset; // offset for address calculation
219 } RegisterToRegisterPlusOffset;
221 struct RegisterToRegisterPlusIndirectOffset {
222 RegisterInfo base_reg; // base register for address calculation
223 RegisterInfo offset_reg; // offset register for address calculation
224 RegisterInfo data_reg; // source/target register for data
225 } RegisterToRegisterPlusIndirectOffset;
227 struct RegisterRegisterOperands {
229 operand1; // register containing first operand for binary op
231 operand2; // register containing second operand for binary op
232 } RegisterRegisterOperands;
234 int64_t signed_offset; // signed offset by which to adjust self (for
237 RegisterInfo reg; // plain register
239 uint64_t unsigned_immediate; // unsigned immediate value
240 int64_t signed_immediate; // signed immediate value
242 lldb::addr_t address; // direct address
244 struct ISAAndImmediate {
246 uint32_t unsigned_data32; // immediate data
249 struct ISAAndImmediateSigned {
251 int32_t signed_data32; // signed immediate data
252 } ISAAndImmediateSigned;
257 Context() : type(eContextInvalid), info_type(eInfoTypeNoArgs) {}
259 void SetRegisterPlusOffset(RegisterInfo base_reg, int64_t signed_offset) {
260 info_type = eInfoTypeRegisterPlusOffset;
261 info.RegisterPlusOffset.reg = base_reg;
262 info.RegisterPlusOffset.signed_offset = signed_offset;
265 void SetRegisterPlusIndirectOffset(RegisterInfo base_reg,
266 RegisterInfo offset_reg) {
267 info_type = eInfoTypeRegisterPlusIndirectOffset;
268 info.RegisterPlusIndirectOffset.base_reg = base_reg;
269 info.RegisterPlusIndirectOffset.offset_reg = offset_reg;
272 void SetRegisterToRegisterPlusOffset(RegisterInfo data_reg,
273 RegisterInfo base_reg,
275 info_type = eInfoTypeRegisterToRegisterPlusOffset;
276 info.RegisterToRegisterPlusOffset.data_reg = data_reg;
277 info.RegisterToRegisterPlusOffset.base_reg = base_reg;
278 info.RegisterToRegisterPlusOffset.offset = offset;
281 void SetRegisterToRegisterPlusIndirectOffset(RegisterInfo base_reg,
282 RegisterInfo offset_reg,
283 RegisterInfo data_reg) {
284 info_type = eInfoTypeRegisterToRegisterPlusIndirectOffset;
285 info.RegisterToRegisterPlusIndirectOffset.base_reg = base_reg;
286 info.RegisterToRegisterPlusIndirectOffset.offset_reg = offset_reg;
287 info.RegisterToRegisterPlusIndirectOffset.data_reg = data_reg;
290 void SetRegisterRegisterOperands(RegisterInfo op1_reg,
291 RegisterInfo op2_reg) {
292 info_type = eInfoTypeRegisterRegisterOperands;
293 info.RegisterRegisterOperands.operand1 = op1_reg;
294 info.RegisterRegisterOperands.operand2 = op2_reg;
297 void SetOffset(int64_t signed_offset) {
298 info_type = eInfoTypeOffset;
299 info.signed_offset = signed_offset;
302 void SetRegister(RegisterInfo reg) {
303 info_type = eInfoTypeRegister;
307 void SetImmediate(uint64_t immediate) {
308 info_type = eInfoTypeImmediate;
309 info.unsigned_immediate = immediate;
312 void SetImmediateSigned(int64_t signed_immediate) {
313 info_type = eInfoTypeImmediateSigned;
314 info.signed_immediate = signed_immediate;
317 void SetAddress(lldb::addr_t address) {
318 info_type = eInfoTypeAddress;
319 info.address = address;
321 void SetISAAndImmediate(uint32_t isa, uint32_t data) {
322 info_type = eInfoTypeISAAndImmediate;
323 info.ISAAndImmediate.isa = isa;
324 info.ISAAndImmediate.unsigned_data32 = data;
327 void SetISAAndImmediateSigned(uint32_t isa, int32_t data) {
328 info_type = eInfoTypeISAAndImmediateSigned;
329 info.ISAAndImmediateSigned.isa = isa;
330 info.ISAAndImmediateSigned.signed_data32 = data;
333 void SetISA(uint32_t isa) {
334 info_type = eInfoTypeISA;
338 void SetNoArgs() { info_type = eInfoTypeNoArgs; }
340 void Dump(Stream &s, EmulateInstruction *instruction) const;
343 typedef size_t (*ReadMemoryCallback)(EmulateInstruction *instruction,
344 void *baton, const Context &context,
345 lldb::addr_t addr, void *dst,
348 typedef size_t (*WriteMemoryCallback)(EmulateInstruction *instruction,
349 void *baton, const Context &context,
350 lldb::addr_t addr, const void *dst,
353 typedef bool (*ReadRegisterCallback)(EmulateInstruction *instruction,
355 const RegisterInfo *reg_info,
356 RegisterValue ®_value);
358 typedef bool (*WriteRegisterCallback)(EmulateInstruction *instruction,
359 void *baton, const Context &context,
360 const RegisterInfo *reg_info,
361 const RegisterValue ®_value);
363 // Type to represent the condition of an instruction. The UINT32 value is
365 // unconditional case and all other value can be used in an architecture
367 typedef uint32_t InstructionCondition;
368 static const InstructionCondition UnconditionalCondition = UINT32_MAX;
370 EmulateInstruction(const ArchSpec &arch);
372 ~EmulateInstruction() override = default;
374 //----------------------------------------------------------------------
375 // Mandatory overrides
376 //----------------------------------------------------------------------
378 SupportsEmulatingInstructionsOfType(InstructionType inst_type) = 0;
380 virtual bool SetTargetTriple(const ArchSpec &arch) = 0;
382 virtual bool ReadInstruction() = 0;
384 virtual bool EvaluateInstruction(uint32_t evaluate_options) = 0;
386 virtual InstructionCondition GetInstructionCondition() {
387 return UnconditionalCondition;
390 virtual bool TestEmulation(Stream *out_stream, ArchSpec &arch,
391 OptionValueDictionary *test_data) = 0;
393 virtual bool GetRegisterInfo(lldb::RegisterKind reg_kind, uint32_t reg_num,
394 RegisterInfo ®_info) = 0;
396 //----------------------------------------------------------------------
397 // Optional overrides
398 //----------------------------------------------------------------------
399 virtual bool SetInstruction(const Opcode &insn_opcode,
400 const Address &inst_addr, Target *target);
402 virtual bool CreateFunctionEntryUnwind(UnwindPlan &unwind_plan);
404 static const char *TranslateRegister(lldb::RegisterKind reg_kind,
405 uint32_t reg_num, std::string ®_name);
407 //----------------------------------------------------------------------
408 // RegisterInfo variants
409 //----------------------------------------------------------------------
410 bool ReadRegister(const RegisterInfo *reg_info, RegisterValue ®_value);
412 uint64_t ReadRegisterUnsigned(const RegisterInfo *reg_info,
413 uint64_t fail_value, bool *success_ptr);
415 bool WriteRegister(const Context &context, const RegisterInfo *ref_info,
416 const RegisterValue ®_value);
418 bool WriteRegisterUnsigned(const Context &context,
419 const RegisterInfo *reg_info, uint64_t reg_value);
421 //----------------------------------------------------------------------
422 // Register kind and number variants
423 //----------------------------------------------------------------------
424 bool ReadRegister(lldb::RegisterKind reg_kind, uint32_t reg_num,
425 RegisterValue ®_value);
427 bool WriteRegister(const Context &context, lldb::RegisterKind reg_kind,
428 uint32_t reg_num, const RegisterValue ®_value);
430 uint64_t ReadRegisterUnsigned(lldb::RegisterKind reg_kind, uint32_t reg_num,
431 uint64_t fail_value, bool *success_ptr);
433 bool WriteRegisterUnsigned(const Context &context,
434 lldb::RegisterKind reg_kind, uint32_t reg_num,
437 size_t ReadMemory(const Context &context, lldb::addr_t addr, void *dst,
440 uint64_t ReadMemoryUnsigned(const Context &context, lldb::addr_t addr,
441 size_t byte_size, uint64_t fail_value,
444 bool WriteMemory(const Context &context, lldb::addr_t addr, const void *src,
447 bool WriteMemoryUnsigned(const Context &context, lldb::addr_t addr,
448 uint64_t uval, size_t uval_byte_size);
450 uint32_t GetAddressByteSize() const { return m_arch.GetAddressByteSize(); }
452 lldb::ByteOrder GetByteOrder() const { return m_arch.GetByteOrder(); }
454 const Opcode &GetOpcode() const { return m_opcode; }
456 lldb::addr_t GetAddress() const { return m_addr; }
458 const ArchSpec &GetArchitecture() const { return m_arch; }
460 static size_t ReadMemoryFrame(EmulateInstruction *instruction, void *baton,
461 const Context &context, lldb::addr_t addr,
462 void *dst, size_t length);
464 static size_t WriteMemoryFrame(EmulateInstruction *instruction, void *baton,
465 const Context &context, lldb::addr_t addr,
466 const void *dst, size_t length);
468 static bool ReadRegisterFrame(EmulateInstruction *instruction, void *baton,
469 const RegisterInfo *reg_info,
470 RegisterValue ®_value);
472 static bool WriteRegisterFrame(EmulateInstruction *instruction, void *baton,
473 const Context &context,
474 const RegisterInfo *reg_info,
475 const RegisterValue ®_value);
477 static size_t ReadMemoryDefault(EmulateInstruction *instruction, void *baton,
478 const Context &context, lldb::addr_t addr,
479 void *dst, size_t length);
481 static size_t WriteMemoryDefault(EmulateInstruction *instruction, void *baton,
482 const Context &context, lldb::addr_t addr,
483 const void *dst, size_t length);
485 static bool ReadRegisterDefault(EmulateInstruction *instruction, void *baton,
486 const RegisterInfo *reg_info,
487 RegisterValue ®_value);
489 static bool WriteRegisterDefault(EmulateInstruction *instruction, void *baton,
490 const Context &context,
491 const RegisterInfo *reg_info,
492 const RegisterValue ®_value);
494 void SetBaton(void *baton);
496 void SetCallbacks(ReadMemoryCallback read_mem_callback,
497 WriteMemoryCallback write_mem_callback,
498 ReadRegisterCallback read_reg_callback,
499 WriteRegisterCallback write_reg_callback);
501 void SetReadMemCallback(ReadMemoryCallback read_mem_callback);
503 void SetWriteMemCallback(WriteMemoryCallback write_mem_callback);
505 void SetReadRegCallback(ReadRegisterCallback read_reg_callback);
507 void SetWriteRegCallback(WriteRegisterCallback write_reg_callback);
509 static bool GetBestRegisterKindAndNumber(const RegisterInfo *reg_info,
510 lldb::RegisterKind ®_kind,
513 static uint32_t GetInternalRegisterNumber(RegisterContext *reg_ctx,
514 const RegisterInfo ®_info);
519 ReadMemoryCallback m_read_mem_callback;
520 WriteMemoryCallback m_write_mem_callback;
521 ReadRegisterCallback m_read_reg_callback;
522 WriteRegisterCallback m_write_reg_callback;
527 //------------------------------------------------------------------
528 // For EmulateInstruction only
529 //------------------------------------------------------------------
530 DISALLOW_COPY_AND_ASSIGN(EmulateInstruction);
533 } // namespace lldb_private
535 #endif // lldb_EmulateInstruction_h_