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 #include "lldb/Core/EmulateInstruction.h"
16 // Other libraries and framework includes
18 #include "lldb/Core/Address.h"
19 #include "lldb/Core/DataExtractor.h"
20 #include "lldb/Core/Error.h"
21 #include "lldb/Core/PluginManager.h"
22 #include "lldb/Core/RegisterValue.h"
23 #include "lldb/Core/StreamFile.h"
24 #include "lldb/Core/StreamString.h"
25 #include "lldb/Host/Endian.h"
26 #include "lldb/Symbol/UnwindPlan.h"
27 #include "lldb/Target/Process.h"
28 #include "lldb/Target/RegisterContext.h"
29 #include "lldb/Target/Target.h"
30 #include "lldb/Target/Thread.h"
33 using namespace lldb_private;
36 EmulateInstruction::FindPlugin (const ArchSpec &arch, InstructionType supported_inst_type, const char *plugin_name)
38 EmulateInstructionCreateInstance create_callback = nullptr;
41 ConstString const_plugin_name (plugin_name);
42 create_callback = PluginManager::GetEmulateInstructionCreateCallbackForPluginName (const_plugin_name);
45 EmulateInstruction *emulate_insn_ptr = create_callback(arch, supported_inst_type);
47 return emulate_insn_ptr;
52 for (uint32_t idx = 0; (create_callback = PluginManager::GetEmulateInstructionCreateCallbackAtIndex(idx)) != nullptr; ++idx)
54 EmulateInstruction *emulate_insn_ptr = create_callback(arch, supported_inst_type);
56 return emulate_insn_ptr;
62 EmulateInstruction::EmulateInstruction (const ArchSpec &arch) :
65 m_read_mem_callback(&ReadMemoryDefault),
66 m_write_mem_callback(&WriteMemoryDefault),
67 m_read_reg_callback(&ReadRegisterDefault),
68 m_write_reg_callback(&WriteRegisterDefault),
69 m_addr(LLDB_INVALID_ADDRESS)
71 ::memset (&m_opcode, 0, sizeof (m_opcode));
75 EmulateInstruction::ReadRegister (const RegisterInfo *reg_info, RegisterValue& reg_value)
77 if (m_read_reg_callback != nullptr)
78 return m_read_reg_callback (this, m_baton, reg_info, reg_value);
83 EmulateInstruction::ReadRegister (lldb::RegisterKind reg_kind, uint32_t reg_num, RegisterValue& reg_value)
85 RegisterInfo reg_info;
86 if (GetRegisterInfo(reg_kind, reg_num, reg_info))
87 return ReadRegister (®_info, reg_value);
92 EmulateInstruction::ReadRegisterUnsigned (lldb::RegisterKind reg_kind,
97 RegisterValue reg_value;
98 if (ReadRegister (reg_kind, reg_num, reg_value))
99 return reg_value.GetAsUInt64(fail_value, success_ptr);
101 *success_ptr = false;
106 EmulateInstruction::ReadRegisterUnsigned (const RegisterInfo *reg_info,
110 RegisterValue reg_value;
111 if (ReadRegister (reg_info, reg_value))
112 return reg_value.GetAsUInt64(fail_value, success_ptr);
114 *success_ptr = false;
119 EmulateInstruction::WriteRegister (const Context &context,
120 const RegisterInfo *reg_info,
121 const RegisterValue& reg_value)
123 if (m_write_reg_callback != nullptr)
124 return m_write_reg_callback (this, m_baton, context, reg_info, reg_value);
129 EmulateInstruction::WriteRegister (const Context &context,
130 lldb::RegisterKind reg_kind,
132 const RegisterValue& reg_value)
134 RegisterInfo reg_info;
135 if (GetRegisterInfo(reg_kind, reg_num, reg_info))
136 return WriteRegister (context, ®_info, reg_value);
141 EmulateInstruction::WriteRegisterUnsigned (const Context &context,
142 lldb::RegisterKind reg_kind,
146 RegisterInfo reg_info;
147 if (GetRegisterInfo(reg_kind, reg_num, reg_info))
149 RegisterValue reg_value;
150 if (reg_value.SetUInt(uint_value, reg_info.byte_size))
151 return WriteRegister (context, ®_info, reg_value);
157 EmulateInstruction::WriteRegisterUnsigned (const Context &context,
158 const RegisterInfo *reg_info,
161 if (reg_info != nullptr)
163 RegisterValue reg_value;
164 if (reg_value.SetUInt(uint_value, reg_info->byte_size))
165 return WriteRegister (context, reg_info, reg_value);
171 EmulateInstruction::ReadMemory (const Context &context,
176 if (m_read_mem_callback != nullptr)
177 return m_read_mem_callback (this, m_baton, context, addr, dst, dst_len) == dst_len;
182 EmulateInstruction::ReadMemoryUnsigned (const Context &context, lldb::addr_t addr, size_t byte_size, uint64_t fail_value, bool *success_ptr)
185 bool success = false;
188 uint8_t buf[sizeof(uint64_t)];
189 size_t bytes_read = m_read_mem_callback (this, m_baton, context, addr, buf, byte_size);
190 if (bytes_read == byte_size)
192 lldb::offset_t offset = 0;
193 DataExtractor data (buf, byte_size, GetByteOrder(), GetAddressByteSize());
194 uval64 = data.GetMaxU64 (&offset, byte_size);
200 *success_ptr = success;
208 EmulateInstruction::WriteMemoryUnsigned (const Context &context,
211 size_t uval_byte_size)
213 StreamString strm(Stream::eBinary, GetAddressByteSize(), GetByteOrder());
214 strm.PutMaxHex64 (uval, uval_byte_size);
216 size_t bytes_written = m_write_mem_callback (this, m_baton, context, addr, strm.GetData(), uval_byte_size);
217 return (bytes_written == uval_byte_size);
221 EmulateInstruction::WriteMemory (const Context &context,
226 if (m_write_mem_callback != nullptr)
227 return m_write_mem_callback (this, m_baton, context, addr, src, src_len) == src_len;
232 EmulateInstruction::SetBaton (void *baton)
238 EmulateInstruction::SetCallbacks (ReadMemoryCallback read_mem_callback,
239 WriteMemoryCallback write_mem_callback,
240 ReadRegisterCallback read_reg_callback,
241 WriteRegisterCallback write_reg_callback)
243 m_read_mem_callback = read_mem_callback;
244 m_write_mem_callback = write_mem_callback;
245 m_read_reg_callback = read_reg_callback;
246 m_write_reg_callback = write_reg_callback;
250 EmulateInstruction::SetReadMemCallback (ReadMemoryCallback read_mem_callback)
252 m_read_mem_callback = read_mem_callback;
256 EmulateInstruction::SetWriteMemCallback (WriteMemoryCallback write_mem_callback)
258 m_write_mem_callback = write_mem_callback;
262 EmulateInstruction::SetReadRegCallback (ReadRegisterCallback read_reg_callback)
264 m_read_reg_callback = read_reg_callback;
268 EmulateInstruction::SetWriteRegCallback (WriteRegisterCallback write_reg_callback)
270 m_write_reg_callback = write_reg_callback;
274 // Read & Write Memory and Registers callback functions.
278 EmulateInstruction::ReadMemoryFrame (EmulateInstruction *instruction,
280 const Context &context,
285 if (baton == nullptr || dst == nullptr || dst_len == 0)
288 StackFrame *frame = (StackFrame *) baton;
290 ProcessSP process_sp (frame->CalculateProcess());
294 return process_sp->ReadMemory (addr, dst, dst_len, error);
300 EmulateInstruction::WriteMemoryFrame (EmulateInstruction *instruction,
302 const Context &context,
307 if (baton == nullptr || src == nullptr || src_len == 0)
310 StackFrame *frame = (StackFrame *) baton;
312 ProcessSP process_sp (frame->CalculateProcess());
316 return process_sp->WriteMemory (addr, src, src_len, error);
323 EmulateInstruction::ReadRegisterFrame (EmulateInstruction *instruction,
325 const RegisterInfo *reg_info,
326 RegisterValue ®_value)
328 if (baton == nullptr)
331 StackFrame *frame = (StackFrame *) baton;
332 return frame->GetRegisterContext()->ReadRegister (reg_info, reg_value);
336 EmulateInstruction::WriteRegisterFrame (EmulateInstruction *instruction,
338 const Context &context,
339 const RegisterInfo *reg_info,
340 const RegisterValue ®_value)
342 if (baton == nullptr)
345 StackFrame *frame = (StackFrame *) baton;
346 return frame->GetRegisterContext()->WriteRegister (reg_info, reg_value);
350 EmulateInstruction::ReadMemoryDefault (EmulateInstruction *instruction,
352 const Context &context,
357 StreamFile strm (stdout, false);
358 strm.Printf (" Read from Memory (address = 0x%" PRIx64 ", length = %" PRIu64 ", context = ", addr, (uint64_t)length);
359 context.Dump (strm, instruction);
361 *((uint64_t *) dst) = 0xdeadbeef;
366 EmulateInstruction::WriteMemoryDefault (EmulateInstruction *instruction,
368 const Context &context,
373 StreamFile strm (stdout, false);
374 strm.Printf (" Write to Memory (address = 0x%" PRIx64 ", length = %" PRIu64 ", context = ", addr, (uint64_t)length);
375 context.Dump (strm, instruction);
381 EmulateInstruction::ReadRegisterDefault (EmulateInstruction *instruction,
383 const RegisterInfo *reg_info,
384 RegisterValue ®_value)
386 StreamFile strm (stdout, false);
387 strm.Printf (" Read Register (%s)\n", reg_info->name);
388 lldb::RegisterKind reg_kind;
390 if (GetBestRegisterKindAndNumber (reg_info, reg_kind, reg_num))
391 reg_value.SetUInt64((uint64_t)reg_kind << 24 | reg_num);
393 reg_value.SetUInt64(0);
399 EmulateInstruction::WriteRegisterDefault (EmulateInstruction *instruction,
401 const Context &context,
402 const RegisterInfo *reg_info,
403 const RegisterValue ®_value)
405 StreamFile strm (stdout, false);
406 strm.Printf (" Write to Register (name = %s, value = " , reg_info->name);
407 reg_value.Dump(&strm, reg_info, false, false, eFormatDefault);
408 strm.PutCString (", context = ");
409 context.Dump (strm, instruction);
415 EmulateInstruction::Context::Dump (Stream &strm,
416 EmulateInstruction *instruction) const
420 case eContextReadOpcode:
421 strm.PutCString ("reading opcode");
424 case eContextImmediate:
425 strm.PutCString ("immediate");
428 case eContextPushRegisterOnStack:
429 strm.PutCString ("push register");
432 case eContextPopRegisterOffStack:
433 strm.PutCString ("pop register");
436 case eContextAdjustStackPointer:
437 strm.PutCString ("adjust sp");
440 case eContextSetFramePointer:
441 strm.PutCString ("set frame pointer");
444 case eContextAdjustBaseRegister:
445 strm.PutCString ("adjusting (writing value back to) a base register");
448 case eContextRegisterPlusOffset:
449 strm.PutCString ("register + offset");
452 case eContextRegisterStore:
453 strm.PutCString ("store register");
456 case eContextRegisterLoad:
457 strm.PutCString ("load register");
460 case eContextRelativeBranchImmediate:
461 strm.PutCString ("relative branch immediate");
464 case eContextAbsoluteBranchRegister:
465 strm.PutCString ("absolute branch register");
468 case eContextSupervisorCall:
469 strm.PutCString ("supervisor call");
472 case eContextTableBranchReadMemory:
473 strm.PutCString ("table branch read memory");
476 case eContextWriteRegisterRandomBits:
477 strm.PutCString ("write random bits to a register");
480 case eContextWriteMemoryRandomBits:
481 strm.PutCString ("write random bits to a memory address");
484 case eContextArithmetic:
485 strm.PutCString ("arithmetic");
488 case eContextReturnFromException:
489 strm.PutCString ("return from exception");
493 strm.PutCString ("unrecognized context.");
499 case eInfoTypeRegisterPlusOffset:
500 strm.Printf(" (reg_plus_offset = %s%+" PRId64 ")",
501 info.RegisterPlusOffset.reg.name,
502 info.RegisterPlusOffset.signed_offset);
505 case eInfoTypeRegisterPlusIndirectOffset:
506 strm.Printf(" (reg_plus_reg = %s + %s)",
507 info.RegisterPlusIndirectOffset.base_reg.name,
508 info.RegisterPlusIndirectOffset.offset_reg.name);
511 case eInfoTypeRegisterToRegisterPlusOffset:
512 strm.Printf(" (base_and_imm_offset = %s%+" PRId64 ", data_reg = %s)",
513 info.RegisterToRegisterPlusOffset.base_reg.name,
514 info.RegisterToRegisterPlusOffset.offset,
515 info.RegisterToRegisterPlusOffset.data_reg.name);
518 case eInfoTypeRegisterToRegisterPlusIndirectOffset:
519 strm.Printf(" (base_and_reg_offset = %s + %s, data_reg = %s)",
520 info.RegisterToRegisterPlusIndirectOffset.base_reg.name,
521 info.RegisterToRegisterPlusIndirectOffset.offset_reg.name,
522 info.RegisterToRegisterPlusIndirectOffset.data_reg.name);
525 case eInfoTypeRegisterRegisterOperands:
526 strm.Printf(" (register to register binary op: %s and %s)",
527 info.RegisterRegisterOperands.operand1.name,
528 info.RegisterRegisterOperands.operand2.name);
531 case eInfoTypeOffset:
532 strm.Printf (" (signed_offset = %+" PRId64 ")", info.signed_offset);
535 case eInfoTypeRegister:
536 strm.Printf (" (reg = %s)", info.reg.name);
539 case eInfoTypeImmediate:
540 strm.Printf (" (unsigned_immediate = %" PRIu64 " (0x%16.16" PRIx64 "))",
541 info.unsigned_immediate,
542 info.unsigned_immediate);
545 case eInfoTypeImmediateSigned:
546 strm.Printf (" (signed_immediate = %+" PRId64 " (0x%16.16" PRIx64 "))",
547 info.signed_immediate,
548 info.signed_immediate);
551 case eInfoTypeAddress:
552 strm.Printf (" (address = 0x%" PRIx64 ")", info.address);
555 case eInfoTypeISAAndImmediate:
556 strm.Printf (" (isa = %u, unsigned_immediate = %u (0x%8.8x))",
557 info.ISAAndImmediate.isa,
558 info.ISAAndImmediate.unsigned_data32,
559 info.ISAAndImmediate.unsigned_data32);
562 case eInfoTypeISAAndImmediateSigned:
563 strm.Printf (" (isa = %u, signed_immediate = %i (0x%8.8x))",
564 info.ISAAndImmediateSigned.isa,
565 info.ISAAndImmediateSigned.signed_data32,
566 info.ISAAndImmediateSigned.signed_data32);
570 strm.Printf (" (isa = %u)", info.isa);
573 case eInfoTypeNoArgs:
579 EmulateInstruction::SetInstruction (const Opcode &opcode, const Address &inst_addr, Target *target)
582 m_addr = LLDB_INVALID_ADDRESS;
583 if (inst_addr.IsValid())
585 if (target != nullptr)
586 m_addr = inst_addr.GetLoadAddress (target);
587 if (m_addr == LLDB_INVALID_ADDRESS)
588 m_addr = inst_addr.GetFileAddress ();
594 EmulateInstruction::GetBestRegisterKindAndNumber (const RegisterInfo *reg_info,
595 lldb::RegisterKind ®_kind,
598 // Generic and DWARF should be the two most popular register kinds when
599 // emulating instructions since they are the most platform agnostic...
600 reg_num = reg_info->kinds[eRegisterKindGeneric];
601 if (reg_num != LLDB_INVALID_REGNUM)
603 reg_kind = eRegisterKindGeneric;
607 reg_num = reg_info->kinds[eRegisterKindDWARF];
608 if (reg_num != LLDB_INVALID_REGNUM)
610 reg_kind = eRegisterKindDWARF;
614 reg_num = reg_info->kinds[eRegisterKindLLDB];
615 if (reg_num != LLDB_INVALID_REGNUM)
617 reg_kind = eRegisterKindLLDB;
621 reg_num = reg_info->kinds[eRegisterKindEHFrame];
622 if (reg_num != LLDB_INVALID_REGNUM)
624 reg_kind = eRegisterKindEHFrame;
628 reg_num = reg_info->kinds[eRegisterKindProcessPlugin];
629 if (reg_num != LLDB_INVALID_REGNUM)
631 reg_kind = eRegisterKindProcessPlugin;
638 EmulateInstruction::GetInternalRegisterNumber (RegisterContext *reg_ctx, const RegisterInfo ®_info)
640 lldb::RegisterKind reg_kind;
642 if (reg_ctx && GetBestRegisterKindAndNumber (®_info, reg_kind, reg_num))
643 return reg_ctx->ConvertRegisterKindToRegisterNumber (reg_kind, reg_num);
644 return LLDB_INVALID_REGNUM;
648 EmulateInstruction::CreateFunctionEntryUnwind (UnwindPlan &unwind_plan)