1 //===-- EmulateInstruction.cpp ----------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "lldb/Core/EmulateInstruction.h"
11 #include "lldb/Core/Address.h"
12 #include "lldb/Core/DumpRegisterValue.h"
13 #include "lldb/Core/PluginManager.h"
14 #include "lldb/Core/StreamFile.h"
15 #include "lldb/Symbol/UnwindPlan.h"
16 #include "lldb/Target/Process.h"
17 #include "lldb/Target/RegisterContext.h"
18 #include "lldb/Target/StackFrame.h"
19 #include "lldb/Utility/ConstString.h"
20 #include "lldb/Utility/DataExtractor.h"
21 #include "lldb/Utility/RegisterValue.h"
22 #include "lldb/Utility/Status.h"
23 #include "lldb/Utility/Stream.h"
24 #include "lldb/Utility/StreamString.h"
25 #include "lldb/lldb-forward.h"
26 #include "lldb/lldb-private-interfaces.h"
28 #include "llvm/ADT/StringRef.h"
36 namespace lldb_private {
41 using namespace lldb_private;
44 EmulateInstruction::FindPlugin(const ArchSpec &arch,
45 InstructionType supported_inst_type,
46 const char *plugin_name) {
47 EmulateInstructionCreateInstance create_callback = nullptr;
49 ConstString const_plugin_name(plugin_name);
51 PluginManager::GetEmulateInstructionCreateCallbackForPluginName(
53 if (create_callback) {
54 EmulateInstruction *emulate_insn_ptr =
55 create_callback(arch, supported_inst_type);
57 return emulate_insn_ptr;
60 for (uint32_t idx = 0;
62 PluginManager::GetEmulateInstructionCreateCallbackAtIndex(idx)) !=
65 EmulateInstruction *emulate_insn_ptr =
66 create_callback(arch, supported_inst_type);
68 return emulate_insn_ptr;
74 EmulateInstruction::EmulateInstruction(const ArchSpec &arch) : m_arch(arch) {}
76 bool EmulateInstruction::ReadRegister(const RegisterInfo *reg_info,
77 RegisterValue ®_value) {
78 if (m_read_reg_callback != nullptr)
79 return m_read_reg_callback(this, m_baton, reg_info, reg_value);
83 bool EmulateInstruction::ReadRegister(lldb::RegisterKind reg_kind,
85 RegisterValue ®_value) {
86 RegisterInfo reg_info;
87 if (GetRegisterInfo(reg_kind, reg_num, reg_info))
88 return ReadRegister(®_info, reg_value);
92 uint64_t EmulateInstruction::ReadRegisterUnsigned(lldb::RegisterKind reg_kind,
96 RegisterValue reg_value;
97 if (ReadRegister(reg_kind, reg_num, reg_value))
98 return reg_value.GetAsUInt64(fail_value, success_ptr);
100 *success_ptr = false;
104 uint64_t EmulateInstruction::ReadRegisterUnsigned(const RegisterInfo *reg_info,
107 RegisterValue reg_value;
108 if (ReadRegister(reg_info, reg_value))
109 return reg_value.GetAsUInt64(fail_value, success_ptr);
111 *success_ptr = false;
115 bool EmulateInstruction::WriteRegister(const Context &context,
116 const RegisterInfo *reg_info,
117 const RegisterValue ®_value) {
118 if (m_write_reg_callback != nullptr)
119 return m_write_reg_callback(this, m_baton, context, reg_info, reg_value);
123 bool EmulateInstruction::WriteRegister(const Context &context,
124 lldb::RegisterKind reg_kind,
126 const RegisterValue ®_value) {
127 RegisterInfo reg_info;
128 if (GetRegisterInfo(reg_kind, reg_num, reg_info))
129 return WriteRegister(context, ®_info, reg_value);
133 bool EmulateInstruction::WriteRegisterUnsigned(const Context &context,
134 lldb::RegisterKind reg_kind,
136 uint64_t uint_value) {
137 RegisterInfo reg_info;
138 if (GetRegisterInfo(reg_kind, reg_num, reg_info)) {
139 RegisterValue reg_value;
140 if (reg_value.SetUInt(uint_value, reg_info.byte_size))
141 return WriteRegister(context, ®_info, reg_value);
146 bool EmulateInstruction::WriteRegisterUnsigned(const Context &context,
147 const RegisterInfo *reg_info,
148 uint64_t uint_value) {
149 if (reg_info != nullptr) {
150 RegisterValue reg_value;
151 if (reg_value.SetUInt(uint_value, reg_info->byte_size))
152 return WriteRegister(context, reg_info, reg_value);
157 size_t EmulateInstruction::ReadMemory(const Context &context, lldb::addr_t addr,
158 void *dst, size_t dst_len) {
159 if (m_read_mem_callback != nullptr)
160 return m_read_mem_callback(this, m_baton, context, addr, dst, dst_len) ==
165 uint64_t EmulateInstruction::ReadMemoryUnsigned(const Context &context,
171 bool success = false;
172 if (byte_size <= 8) {
173 uint8_t buf[sizeof(uint64_t)];
175 m_read_mem_callback(this, m_baton, context, addr, buf, byte_size);
176 if (bytes_read == byte_size) {
177 lldb::offset_t offset = 0;
178 DataExtractor data(buf, byte_size, GetByteOrder(), GetAddressByteSize());
179 uval64 = data.GetMaxU64(&offset, byte_size);
185 *success_ptr = success;
192 bool EmulateInstruction::WriteMemoryUnsigned(const Context &context,
193 lldb::addr_t addr, uint64_t uval,
194 size_t uval_byte_size) {
195 StreamString strm(Stream::eBinary, GetAddressByteSize(), GetByteOrder());
196 strm.PutMaxHex64(uval, uval_byte_size);
198 size_t bytes_written = m_write_mem_callback(
199 this, m_baton, context, addr, strm.GetString().data(), uval_byte_size);
200 return (bytes_written == uval_byte_size);
203 bool EmulateInstruction::WriteMemory(const Context &context, lldb::addr_t addr,
204 const void *src, size_t src_len) {
205 if (m_write_mem_callback != nullptr)
206 return m_write_mem_callback(this, m_baton, context, addr, src, src_len) ==
211 void EmulateInstruction::SetBaton(void *baton) { m_baton = baton; }
213 void EmulateInstruction::SetCallbacks(
214 ReadMemoryCallback read_mem_callback,
215 WriteMemoryCallback write_mem_callback,
216 ReadRegisterCallback read_reg_callback,
217 WriteRegisterCallback write_reg_callback) {
218 m_read_mem_callback = read_mem_callback;
219 m_write_mem_callback = write_mem_callback;
220 m_read_reg_callback = read_reg_callback;
221 m_write_reg_callback = write_reg_callback;
224 void EmulateInstruction::SetReadMemCallback(
225 ReadMemoryCallback read_mem_callback) {
226 m_read_mem_callback = read_mem_callback;
229 void EmulateInstruction::SetWriteMemCallback(
230 WriteMemoryCallback write_mem_callback) {
231 m_write_mem_callback = write_mem_callback;
234 void EmulateInstruction::SetReadRegCallback(
235 ReadRegisterCallback read_reg_callback) {
236 m_read_reg_callback = read_reg_callback;
239 void EmulateInstruction::SetWriteRegCallback(
240 WriteRegisterCallback write_reg_callback) {
241 m_write_reg_callback = write_reg_callback;
245 // Read & Write Memory and Registers callback functions.
248 size_t EmulateInstruction::ReadMemoryFrame(EmulateInstruction *instruction,
249 void *baton, const Context &context,
250 lldb::addr_t addr, void *dst,
252 if (baton == nullptr || dst == nullptr || dst_len == 0)
255 StackFrame *frame = (StackFrame *)baton;
257 ProcessSP process_sp(frame->CalculateProcess());
260 return process_sp->ReadMemory(addr, dst, dst_len, error);
265 size_t EmulateInstruction::WriteMemoryFrame(EmulateInstruction *instruction,
266 void *baton, const Context &context,
267 lldb::addr_t addr, const void *src,
269 if (baton == nullptr || src == nullptr || src_len == 0)
272 StackFrame *frame = (StackFrame *)baton;
274 ProcessSP process_sp(frame->CalculateProcess());
277 return process_sp->WriteMemory(addr, src, src_len, error);
283 bool EmulateInstruction::ReadRegisterFrame(EmulateInstruction *instruction,
285 const RegisterInfo *reg_info,
286 RegisterValue ®_value) {
287 if (baton == nullptr)
290 StackFrame *frame = (StackFrame *)baton;
291 return frame->GetRegisterContext()->ReadRegister(reg_info, reg_value);
294 bool EmulateInstruction::WriteRegisterFrame(EmulateInstruction *instruction,
295 void *baton, const Context &context,
296 const RegisterInfo *reg_info,
297 const RegisterValue ®_value) {
298 if (baton == nullptr)
301 StackFrame *frame = (StackFrame *)baton;
302 return frame->GetRegisterContext()->WriteRegister(reg_info, reg_value);
305 size_t EmulateInstruction::ReadMemoryDefault(EmulateInstruction *instruction,
307 const Context &context,
308 lldb::addr_t addr, void *dst,
310 StreamFile strm(stdout, false);
311 strm.Printf(" Read from Memory (address = 0x%" PRIx64 ", length = %" PRIu64
313 addr, (uint64_t)length);
314 context.Dump(strm, instruction);
316 *((uint64_t *)dst) = 0xdeadbeef;
320 size_t EmulateInstruction::WriteMemoryDefault(EmulateInstruction *instruction,
322 const Context &context,
324 const void *dst, size_t length) {
325 StreamFile strm(stdout, false);
326 strm.Printf(" Write to Memory (address = 0x%" PRIx64 ", length = %" PRIu64
328 addr, (uint64_t)length);
329 context.Dump(strm, instruction);
334 bool EmulateInstruction::ReadRegisterDefault(EmulateInstruction *instruction,
336 const RegisterInfo *reg_info,
337 RegisterValue ®_value) {
338 StreamFile strm(stdout, false);
339 strm.Printf(" Read Register (%s)\n", reg_info->name);
340 lldb::RegisterKind reg_kind;
342 if (GetBestRegisterKindAndNumber(reg_info, reg_kind, reg_num))
343 reg_value.SetUInt64((uint64_t)reg_kind << 24 | reg_num);
345 reg_value.SetUInt64(0);
350 bool EmulateInstruction::WriteRegisterDefault(EmulateInstruction *instruction,
352 const Context &context,
353 const RegisterInfo *reg_info,
354 const RegisterValue ®_value) {
355 StreamFile strm(stdout, false);
356 strm.Printf(" Write to Register (name = %s, value = ", reg_info->name);
357 DumpRegisterValue(reg_value, &strm, reg_info, false, false, eFormatDefault);
358 strm.PutCString(", context = ");
359 context.Dump(strm, instruction);
364 void EmulateInstruction::Context::Dump(Stream &strm,
365 EmulateInstruction *instruction) const {
367 case eContextReadOpcode:
368 strm.PutCString("reading opcode");
371 case eContextImmediate:
372 strm.PutCString("immediate");
375 case eContextPushRegisterOnStack:
376 strm.PutCString("push register");
379 case eContextPopRegisterOffStack:
380 strm.PutCString("pop register");
383 case eContextAdjustStackPointer:
384 strm.PutCString("adjust sp");
387 case eContextSetFramePointer:
388 strm.PutCString("set frame pointer");
391 case eContextAdjustBaseRegister:
392 strm.PutCString("adjusting (writing value back to) a base register");
395 case eContextRegisterPlusOffset:
396 strm.PutCString("register + offset");
399 case eContextRegisterStore:
400 strm.PutCString("store register");
403 case eContextRegisterLoad:
404 strm.PutCString("load register");
407 case eContextRelativeBranchImmediate:
408 strm.PutCString("relative branch immediate");
411 case eContextAbsoluteBranchRegister:
412 strm.PutCString("absolute branch register");
415 case eContextSupervisorCall:
416 strm.PutCString("supervisor call");
419 case eContextTableBranchReadMemory:
420 strm.PutCString("table branch read memory");
423 case eContextWriteRegisterRandomBits:
424 strm.PutCString("write random bits to a register");
427 case eContextWriteMemoryRandomBits:
428 strm.PutCString("write random bits to a memory address");
431 case eContextArithmetic:
432 strm.PutCString("arithmetic");
435 case eContextReturnFromException:
436 strm.PutCString("return from exception");
440 strm.PutCString("unrecognized context.");
445 case eInfoTypeRegisterPlusOffset:
446 strm.Printf(" (reg_plus_offset = %s%+" PRId64 ")",
447 info.RegisterPlusOffset.reg.name,
448 info.RegisterPlusOffset.signed_offset);
451 case eInfoTypeRegisterPlusIndirectOffset:
452 strm.Printf(" (reg_plus_reg = %s + %s)",
453 info.RegisterPlusIndirectOffset.base_reg.name,
454 info.RegisterPlusIndirectOffset.offset_reg.name);
457 case eInfoTypeRegisterToRegisterPlusOffset:
458 strm.Printf(" (base_and_imm_offset = %s%+" PRId64 ", data_reg = %s)",
459 info.RegisterToRegisterPlusOffset.base_reg.name,
460 info.RegisterToRegisterPlusOffset.offset,
461 info.RegisterToRegisterPlusOffset.data_reg.name);
464 case eInfoTypeRegisterToRegisterPlusIndirectOffset:
465 strm.Printf(" (base_and_reg_offset = %s + %s, data_reg = %s)",
466 info.RegisterToRegisterPlusIndirectOffset.base_reg.name,
467 info.RegisterToRegisterPlusIndirectOffset.offset_reg.name,
468 info.RegisterToRegisterPlusIndirectOffset.data_reg.name);
471 case eInfoTypeRegisterRegisterOperands:
472 strm.Printf(" (register to register binary op: %s and %s)",
473 info.RegisterRegisterOperands.operand1.name,
474 info.RegisterRegisterOperands.operand2.name);
477 case eInfoTypeOffset:
478 strm.Printf(" (signed_offset = %+" PRId64 ")", info.signed_offset);
481 case eInfoTypeRegister:
482 strm.Printf(" (reg = %s)", info.reg.name);
485 case eInfoTypeImmediate:
486 strm.Printf(" (unsigned_immediate = %" PRIu64 " (0x%16.16" PRIx64 "))",
487 info.unsigned_immediate, info.unsigned_immediate);
490 case eInfoTypeImmediateSigned:
491 strm.Printf(" (signed_immediate = %+" PRId64 " (0x%16.16" PRIx64 "))",
492 info.signed_immediate, info.signed_immediate);
495 case eInfoTypeAddress:
496 strm.Printf(" (address = 0x%" PRIx64 ")", info.address);
499 case eInfoTypeISAAndImmediate:
500 strm.Printf(" (isa = %u, unsigned_immediate = %u (0x%8.8x))",
501 info.ISAAndImmediate.isa, info.ISAAndImmediate.unsigned_data32,
502 info.ISAAndImmediate.unsigned_data32);
505 case eInfoTypeISAAndImmediateSigned:
506 strm.Printf(" (isa = %u, signed_immediate = %i (0x%8.8x))",
507 info.ISAAndImmediateSigned.isa,
508 info.ISAAndImmediateSigned.signed_data32,
509 info.ISAAndImmediateSigned.signed_data32);
513 strm.Printf(" (isa = %u)", info.isa);
516 case eInfoTypeNoArgs:
521 bool EmulateInstruction::SetInstruction(const Opcode &opcode,
522 const Address &inst_addr,
525 m_addr = LLDB_INVALID_ADDRESS;
526 if (inst_addr.IsValid()) {
527 if (target != nullptr)
528 m_addr = inst_addr.GetLoadAddress(target);
529 if (m_addr == LLDB_INVALID_ADDRESS)
530 m_addr = inst_addr.GetFileAddress();
535 bool EmulateInstruction::GetBestRegisterKindAndNumber(
536 const RegisterInfo *reg_info, lldb::RegisterKind ®_kind,
538 // Generic and DWARF should be the two most popular register kinds when
539 // emulating instructions since they are the most platform agnostic...
540 reg_num = reg_info->kinds[eRegisterKindGeneric];
541 if (reg_num != LLDB_INVALID_REGNUM) {
542 reg_kind = eRegisterKindGeneric;
546 reg_num = reg_info->kinds[eRegisterKindDWARF];
547 if (reg_num != LLDB_INVALID_REGNUM) {
548 reg_kind = eRegisterKindDWARF;
552 reg_num = reg_info->kinds[eRegisterKindLLDB];
553 if (reg_num != LLDB_INVALID_REGNUM) {
554 reg_kind = eRegisterKindLLDB;
558 reg_num = reg_info->kinds[eRegisterKindEHFrame];
559 if (reg_num != LLDB_INVALID_REGNUM) {
560 reg_kind = eRegisterKindEHFrame;
564 reg_num = reg_info->kinds[eRegisterKindProcessPlugin];
565 if (reg_num != LLDB_INVALID_REGNUM) {
566 reg_kind = eRegisterKindProcessPlugin;
573 EmulateInstruction::GetInternalRegisterNumber(RegisterContext *reg_ctx,
574 const RegisterInfo ®_info) {
575 lldb::RegisterKind reg_kind;
577 if (reg_ctx && GetBestRegisterKindAndNumber(®_info, reg_kind, reg_num))
578 return reg_ctx->ConvertRegisterKindToRegisterNumber(reg_kind, reg_num);
579 return LLDB_INVALID_REGNUM;
582 bool EmulateInstruction::CreateFunctionEntryUnwind(UnwindPlan &unwind_plan) {