LLDB mainline
EmulateInstruction.cpp
Go to the documentation of this file.
1//===-- EmulateInstruction.cpp --------------------------------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
10
11#include "lldb/Core/Address.h"
16#include "lldb/Target/Process.h"
22#include "lldb/Utility/Status.h"
23#include "lldb/Utility/Stream.h"
25#include "lldb/lldb-forward.h"
27
28#include "llvm/ADT/StringRef.h"
29
30#include <cstring>
31#include <memory>
32#include <optional>
33
34#include <cinttypes>
35#include <cstdio>
36
37namespace lldb_private {
38class Target;
39}
40
41using namespace lldb;
42using namespace lldb_private;
43
46 InstructionType supported_inst_type,
47 const char *plugin_name) {
48 EmulateInstructionCreateInstance create_callback = nullptr;
49 if (plugin_name) {
50 create_callback =
52 plugin_name);
53 if (create_callback) {
54 EmulateInstruction *emulate_insn_ptr =
55 create_callback(arch, supported_inst_type);
56 if (emulate_insn_ptr)
57 return emulate_insn_ptr;
58 }
59 } else {
60 for (uint32_t idx = 0;
61 (create_callback =
63 nullptr;
64 ++idx) {
65 EmulateInstruction *emulate_insn_ptr =
66 create_callback(arch, supported_inst_type);
67 if (emulate_insn_ptr)
68 return emulate_insn_ptr;
69 }
70 }
71 return nullptr;
72}
73
75
76std::optional<RegisterValue>
78 if (m_read_reg_callback == nullptr)
79 return {};
80
81 RegisterValue reg_value;
82 bool success = m_read_reg_callback(this, m_baton, &reg_info, reg_value);
83 if (success)
84 return reg_value;
85 return {};
86}
87
89 uint32_t reg_num,
90 RegisterValue &reg_value) {
91 std::optional<RegisterInfo> reg_info = GetRegisterInfo(reg_kind, reg_num);
92 if (!reg_info)
93 return false;
94
95 std::optional<RegisterValue> value = ReadRegister(*reg_info);
96 if (value)
97 reg_value = *value;
98 return value.has_value();
99}
100
102 uint32_t reg_num,
103 uint64_t fail_value,
104 bool *success_ptr) {
105 RegisterValue reg_value;
106 if (ReadRegister(reg_kind, reg_num, reg_value))
107 return reg_value.GetAsUInt64(fail_value, success_ptr);
108 if (success_ptr)
109 *success_ptr = false;
110 return fail_value;
111}
112
114 uint64_t fail_value,
115 bool *success_ptr) {
116 std::optional<RegisterValue> reg_value = ReadRegister(reg_info);
117 if (!reg_value) {
118 if (success_ptr)
119 *success_ptr = false;
120 return fail_value;
121 }
122
123 return reg_value->GetAsUInt64(fail_value, success_ptr);
124}
125
127 const RegisterInfo &reg_info,
128 const RegisterValue &reg_value) {
129 if (m_write_reg_callback != nullptr)
130 return m_write_reg_callback(this, m_baton, context, &reg_info, reg_value);
131 return false;
132}
133
135 lldb::RegisterKind reg_kind,
136 uint32_t reg_num,
137 const RegisterValue &reg_value) {
138 std::optional<RegisterInfo> reg_info = GetRegisterInfo(reg_kind, reg_num);
139 if (reg_info)
140 return WriteRegister(context, *reg_info, reg_value);
141 return false;
142}
143
145 lldb::RegisterKind reg_kind,
146 uint32_t reg_num,
147 uint64_t uint_value) {
148 std::optional<RegisterInfo> reg_info = GetRegisterInfo(reg_kind, reg_num);
149 if (reg_info) {
150 RegisterValue reg_value;
151 if (reg_value.SetUInt(uint_value, reg_info->byte_size))
152 return WriteRegister(context, *reg_info, reg_value);
153 }
154 return false;
155}
156
158 const RegisterInfo &reg_info,
159 uint64_t uint_value) {
160 RegisterValue reg_value;
161 if (reg_value.SetUInt(uint_value, reg_info.byte_size))
162 return WriteRegister(context, reg_info, reg_value);
163 return false;
164}
165
167 void *dst, size_t dst_len) {
168 if (m_read_mem_callback != nullptr)
169 return m_read_mem_callback(this, m_baton, context, addr, dst, dst_len) ==
170 dst_len;
171 return false;
172}
173
175 lldb::addr_t addr,
176 size_t byte_size,
177 uint64_t fail_value,
178 bool *success_ptr) {
179 uint64_t uval64 = 0;
180 bool success = false;
181 if (byte_size <= 8) {
182 uint8_t buf[sizeof(uint64_t)];
183 size_t bytes_read =
184 m_read_mem_callback(this, m_baton, context, addr, buf, byte_size);
185 if (bytes_read == byte_size) {
186 lldb::offset_t offset = 0;
187 DataExtractor data(buf, byte_size, GetByteOrder(), GetAddressByteSize());
188 uval64 = data.GetMaxU64(&offset, byte_size);
189 success = true;
190 }
191 }
192
193 if (success_ptr)
194 *success_ptr = success;
195
196 if (!success)
197 uval64 = fail_value;
198 return uval64;
199}
200
202 lldb::addr_t addr, uint64_t uval,
203 size_t uval_byte_size) {
205 strm.PutMaxHex64(uval, uval_byte_size);
206
207 size_t bytes_written = m_write_mem_callback(
208 this, m_baton, context, addr, strm.GetString().data(), uval_byte_size);
209 return (bytes_written == uval_byte_size);
210}
211
213 const void *src, size_t src_len) {
214 if (m_write_mem_callback != nullptr)
215 return m_write_mem_callback(this, m_baton, context, addr, src, src_len) ==
216 src_len;
217 return false;
218}
219
220void EmulateInstruction::SetBaton(void *baton) { m_baton = baton; }
221
223 ReadMemoryCallback read_mem_callback,
224 WriteMemoryCallback write_mem_callback,
225 ReadRegisterCallback read_reg_callback,
226 WriteRegisterCallback write_reg_callback) {
227 m_read_mem_callback = read_mem_callback;
228 m_write_mem_callback = write_mem_callback;
229 m_read_reg_callback = read_reg_callback;
230 m_write_reg_callback = write_reg_callback;
231}
232
234 ReadMemoryCallback read_mem_callback) {
235 m_read_mem_callback = read_mem_callback;
236}
237
239 WriteMemoryCallback write_mem_callback) {
240 m_write_mem_callback = write_mem_callback;
241}
242
244 ReadRegisterCallback read_reg_callback) {
245 m_read_reg_callback = read_reg_callback;
246}
247
249 WriteRegisterCallback write_reg_callback) {
250 m_write_reg_callback = write_reg_callback;
251}
252
253//
254// Read & Write Memory and Registers callback functions.
255//
256
258 void *baton, const Context &context,
259 lldb::addr_t addr, void *dst,
260 size_t dst_len) {
261 if (baton == nullptr || dst == nullptr || dst_len == 0)
262 return 0;
263
264 StackFrame *frame = (StackFrame *)baton;
265
266 ProcessSP process_sp(frame->CalculateProcess());
267 if (process_sp) {
269 return process_sp->ReadMemory(addr, dst, dst_len, error);
270 }
271 return 0;
272}
273
275 void *baton, const Context &context,
276 lldb::addr_t addr, const void *src,
277 size_t src_len) {
278 if (baton == nullptr || src == nullptr || src_len == 0)
279 return 0;
280
281 StackFrame *frame = (StackFrame *)baton;
282
283 ProcessSP process_sp(frame->CalculateProcess());
284 if (process_sp) {
286 return process_sp->WriteMemory(addr, src, src_len, error);
287 }
288
289 return 0;
290}
291
293 void *baton,
294 const RegisterInfo *reg_info,
295 RegisterValue &reg_value) {
296 if (baton == nullptr)
297 return false;
298
299 StackFrame *frame = (StackFrame *)baton;
300 return frame->GetRegisterContext()->ReadRegister(reg_info, reg_value);
301}
302
304 void *baton, const Context &context,
305 const RegisterInfo *reg_info,
306 const RegisterValue &reg_value) {
307 if (baton == nullptr)
308 return false;
309
310 StackFrame *frame = (StackFrame *)baton;
311 return frame->GetRegisterContext()->WriteRegister(reg_info, reg_value);
312}
313
315 void *baton,
316 const Context &context,
317 lldb::addr_t addr, void *dst,
318 size_t length) {
319 StreamFile strm(stdout, false);
320 strm.Printf(" Read from Memory (address = 0x%" PRIx64 ", length = %" PRIu64
321 ", context = ",
322 addr, (uint64_t)length);
323 context.Dump(strm, instruction);
324 strm.EOL();
325 *((uint64_t *)dst) = 0xdeadbeef;
326 return length;
327}
328
330 void *baton,
331 const Context &context,
332 lldb::addr_t addr,
333 const void *dst, size_t length) {
334 StreamFile strm(stdout, false);
335 strm.Printf(" Write to Memory (address = 0x%" PRIx64 ", length = %" PRIu64
336 ", context = ",
337 addr, (uint64_t)length);
338 context.Dump(strm, instruction);
339 strm.EOL();
340 return length;
341}
342
344 void *baton,
345 const RegisterInfo *reg_info,
346 RegisterValue &reg_value) {
347 StreamFile strm(stdout, false);
348 strm.Printf(" Read Register (%s)\n", reg_info->name);
349 lldb::RegisterKind reg_kind;
350 uint32_t reg_num;
351 if (GetBestRegisterKindAndNumber(reg_info, reg_kind, reg_num))
352 reg_value.SetUInt64((uint64_t)reg_kind << 24 | reg_num);
353 else
354 reg_value.SetUInt64(0);
355
356 return true;
357}
358
360 void *baton,
361 const Context &context,
362 const RegisterInfo *reg_info,
363 const RegisterValue &reg_value) {
364 StreamFile strm(stdout, false);
365 strm.Printf(" Write to Register (name = %s, value = ", reg_info->name);
366 DumpRegisterValue(reg_value, strm, *reg_info, false, false, eFormatDefault);
367 strm.PutCString(", context = ");
368 context.Dump(strm, instruction);
369 strm.EOL();
370 return true;
371}
372
374 EmulateInstruction *instruction) const {
375 switch (type) {
377 strm.PutCString("reading opcode");
378 break;
379
381 strm.PutCString("immediate");
382 break;
383
385 strm.PutCString("push register");
386 break;
387
389 strm.PutCString("pop register");
390 break;
391
393 strm.PutCString("adjust sp");
394 break;
395
397 strm.PutCString("set frame pointer");
398 break;
399
401 strm.PutCString("adjusting (writing value back to) a base register");
402 break;
403
405 strm.PutCString("register + offset");
406 break;
407
409 strm.PutCString("store register");
410 break;
411
413 strm.PutCString("load register");
414 break;
415
417 strm.PutCString("relative branch immediate");
418 break;
419
421 strm.PutCString("absolute branch register");
422 break;
423
425 strm.PutCString("supervisor call");
426 break;
427
429 strm.PutCString("table branch read memory");
430 break;
431
433 strm.PutCString("write random bits to a register");
434 break;
435
437 strm.PutCString("write random bits to a memory address");
438 break;
439
441 strm.PutCString("arithmetic");
442 break;
443
445 strm.PutCString("return from exception");
446 break;
447
448 default:
449 strm.PutCString("unrecognized context.");
450 break;
451 }
452
453 switch (GetInfoType()) {
455 strm.Printf(" (reg_plus_offset = %s%+" PRId64 ")",
458 break;
459
461 strm.Printf(" (reg_plus_reg = %s + %s)",
464 break;
465
467 strm.Printf(" (base_and_imm_offset = %s%+" PRId64 ", data_reg = %s)",
471 break;
472
474 strm.Printf(" (base_and_reg_offset = %s + %s, data_reg = %s)",
478 break;
479
481 strm.Printf(" (register to register binary op: %s and %s)",
484 break;
485
486 case eInfoTypeOffset:
487 strm.Printf(" (signed_offset = %+" PRId64 ")", info.signed_offset);
488 break;
489
491 strm.Printf(" (reg = %s)", info.reg.name);
492 break;
493
495 strm.Printf(" (unsigned_immediate = %" PRIu64 " (0x%16.16" PRIx64 "))",
497 break;
498
500 strm.Printf(" (signed_immediate = %+" PRId64 " (0x%16.16" PRIx64 "))",
502 break;
503
504 case eInfoTypeAddress:
505 strm.Printf(" (address = 0x%" PRIx64 ")", info.address);
506 break;
507
509 strm.Printf(" (isa = %u, unsigned_immediate = %u (0x%8.8x))",
512 break;
513
515 strm.Printf(" (isa = %u, signed_immediate = %i (0x%8.8x))",
519 break;
520
521 case eInfoTypeISA:
522 strm.Printf(" (isa = %u)", info.isa);
523 break;
524
525 case eInfoTypeNoArgs:
526 break;
527 }
528}
529
531 const Address &inst_addr,
532 Target *target) {
533 m_opcode = opcode;
535 if (inst_addr.IsValid()) {
536 if (target != nullptr)
537 m_addr = inst_addr.GetLoadAddress(target);
539 m_addr = inst_addr.GetFileAddress();
540 }
541 return true;
542}
543
545 const RegisterInfo *reg_info, lldb::RegisterKind &reg_kind,
546 uint32_t &reg_num) {
547 // Generic and DWARF should be the two most popular register kinds when
548 // emulating instructions since they are the most platform agnostic...
549 reg_num = reg_info->kinds[eRegisterKindGeneric];
550 if (reg_num != LLDB_INVALID_REGNUM) {
551 reg_kind = eRegisterKindGeneric;
552 return true;
553 }
554
555 reg_num = reg_info->kinds[eRegisterKindDWARF];
556 if (reg_num != LLDB_INVALID_REGNUM) {
557 reg_kind = eRegisterKindDWARF;
558 return true;
559 }
560
561 reg_num = reg_info->kinds[eRegisterKindLLDB];
562 if (reg_num != LLDB_INVALID_REGNUM) {
563 reg_kind = eRegisterKindLLDB;
564 return true;
565 }
566
567 reg_num = reg_info->kinds[eRegisterKindEHFrame];
568 if (reg_num != LLDB_INVALID_REGNUM) {
569 reg_kind = eRegisterKindEHFrame;
570 return true;
571 }
572
573 reg_num = reg_info->kinds[eRegisterKindProcessPlugin];
574 if (reg_num != LLDB_INVALID_REGNUM) {
576 return true;
577 }
578 return false;
579}
580
581uint32_t
583 const RegisterInfo &reg_info) {
584 lldb::RegisterKind reg_kind;
585 uint32_t reg_num;
586 if (reg_ctx && GetBestRegisterKindAndNumber(&reg_info, reg_kind, reg_num))
587 return reg_ctx->ConvertRegisterKindToRegisterNumber(reg_kind, reg_num);
588 return LLDB_INVALID_REGNUM;
589}
590
592 unwind_plan.Clear();
593 return false;
594}
static llvm::raw_ostream & error(Stream &strm)
static bool ReadRegisterCallback(EmulateInstruction *instruction, void *baton, const RegisterInfo *reg_info, RegisterValue &reg_value)
static size_t WriteMemoryCallback(EmulateInstruction *instruction, void *baton, const EmulateInstruction::Context &context, lldb::addr_t addr, const void *dst, size_t length)
static size_t ReadMemoryCallback(EmulateInstruction *instruction, void *baton, const EmulateInstruction::Context &context, lldb::addr_t addr, void *dst, size_t length)
static bool WriteRegisterCallback(EmulateInstruction *instruction, void *baton, const EmulateInstruction::Context &context, const RegisterInfo *reg_info, const RegisterValue &reg_value)
A section + offset based address class.
Definition: Address.h:62
lldb::addr_t GetLoadAddress(Target *target) const
Get the load address.
Definition: Address.cpp:313
lldb::addr_t GetFileAddress() const
Get the file address.
Definition: Address.cpp:293
bool IsValid() const
Check if the object state is valid.
Definition: Address.h:355
An architecture specification class.
Definition: ArchSpec.h:31
An data extractor class.
Definition: DataExtractor.h:48
uint64_t GetMaxU64(lldb::offset_t *offset_ptr, size_t byte_size) const
Extract an unsigned integer of size byte_size from *offset_ptr.
"lldb/Core/EmulateInstruction.h" A class that allows emulation of CPU opcodes.
static bool GetBestRegisterKindAndNumber(const RegisterInfo *reg_info, lldb::RegisterKind &reg_kind, uint32_t &reg_num)
static bool WriteRegisterDefault(EmulateInstruction *instruction, void *baton, const Context &context, const RegisterInfo *reg_info, const RegisterValue &reg_value)
static size_t WriteMemoryFrame(EmulateInstruction *instruction, void *baton, const Context &context, lldb::addr_t addr, const void *dst, size_t length)
WriteMemoryCallback m_write_mem_callback
lldb::ByteOrder GetByteOrder() const
void SetWriteRegCallback(WriteRegisterCallback write_reg_callback)
void SetCallbacks(ReadMemoryCallback read_mem_callback, WriteMemoryCallback write_mem_callback, ReadRegisterCallback read_reg_callback, WriteRegisterCallback write_reg_callback)
static bool ReadRegisterDefault(EmulateInstruction *instruction, void *baton, const RegisterInfo *reg_info, RegisterValue &reg_value)
virtual bool CreateFunctionEntryUnwind(UnwindPlan &unwind_plan)
bool WriteMemoryUnsigned(const Context &context, lldb::addr_t addr, uint64_t uval, size_t uval_byte_size)
static uint32_t GetInternalRegisterNumber(RegisterContext *reg_ctx, const RegisterInfo &reg_info)
void SetReadRegCallback(ReadRegisterCallback read_reg_callback)
size_t ReadMemory(const Context &context, lldb::addr_t addr, void *dst, size_t dst_len)
std::optional< RegisterValue > ReadRegister(const RegisterInfo &reg_info)
ReadRegisterCallback m_read_reg_callback
static size_t ReadMemoryFrame(EmulateInstruction *instruction, void *baton, const Context &context, lldb::addr_t addr, void *dst, size_t length)
bool WriteRegister(const Context &context, const RegisterInfo &ref_info, const RegisterValue &reg_value)
bool WriteRegisterUnsigned(const Context &context, const RegisterInfo &reg_info, uint64_t reg_value)
bool WriteMemory(const Context &context, lldb::addr_t addr, const void *src, size_t src_len)
uint64_t ReadMemoryUnsigned(const Context &context, lldb::addr_t addr, size_t byte_size, uint64_t fail_value, bool *success_ptr)
WriteRegisterCallback m_write_reg_callback
EmulateInstruction(const ArchSpec &arch)
static size_t WriteMemoryDefault(EmulateInstruction *instruction, void *baton, const Context &context, lldb::addr_t addr, const void *dst, size_t length)
void SetReadMemCallback(ReadMemoryCallback read_mem_callback)
static bool WriteRegisterFrame(EmulateInstruction *instruction, void *baton, const Context &context, const RegisterInfo *reg_info, const RegisterValue &reg_value)
static bool ReadRegisterFrame(EmulateInstruction *instruction, void *baton, const RegisterInfo *reg_info, RegisterValue &reg_value)
virtual std::optional< RegisterInfo > GetRegisterInfo(lldb::RegisterKind reg_kind, uint32_t reg_num)=0
static size_t ReadMemoryDefault(EmulateInstruction *instruction, void *baton, const Context &context, lldb::addr_t addr, void *dst, size_t length)
virtual bool SetInstruction(const Opcode &insn_opcode, const Address &inst_addr, Target *target)
static EmulateInstruction * FindPlugin(const ArchSpec &arch, InstructionType supported_inst_type, const char *plugin_name)
uint64_t ReadRegisterUnsigned(const RegisterInfo &reg_info, uint64_t fail_value, bool *success_ptr)
void SetWriteMemCallback(WriteMemoryCallback write_mem_callback)
static EmulateInstructionCreateInstance GetEmulateInstructionCreateCallbackForPluginName(llvm::StringRef name)
static EmulateInstructionCreateInstance GetEmulateInstructionCreateCallbackAtIndex(uint32_t idx)
virtual uint32_t ConvertRegisterKindToRegisterNumber(lldb::RegisterKind kind, uint32_t num)
Convert from a given register numbering scheme to the lldb register numbering scheme.
void SetUInt64(uint64_t uint, Type t=eTypeUInt64)
bool SetUInt(uint64_t uint, uint32_t byte_size)
uint64_t GetAsUInt64(uint64_t fail_value=UINT64_MAX, bool *success_ptr=nullptr) const
This base class provides an interface to stack frames.
Definition: StackFrame.h:43
lldb::RegisterContextSP GetRegisterContext()
Get the RegisterContext for this frame, if possible.
lldb::ProcessSP CalculateProcess() override
An error handling class.
Definition: Status.h:44
llvm::StringRef GetString() const
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
@ eBinary
Get and put data as binary instead of as the default string mode.
Definition: Stream.h:32
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:134
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:65
size_t EOL()
Output and End of Line character to the stream.
Definition: Stream.cpp:155
size_t PutMaxHex64(uint64_t uvalue, size_t byte_size, lldb::ByteOrder byte_order=lldb::eByteOrderInvalid)
Definition: Stream.cpp:315
#define LLDB_INVALID_ADDRESS
Definition: lldb-defines.h:82
#define LLDB_INVALID_REGNUM
Definition: lldb-defines.h:87
A class that represents a running process on the host machine.
EmulateInstruction *(* EmulateInstructionCreateInstance)(const ArchSpec &arch, InstructionType inst_type)
InstructionType
Instruction types.
void DumpRegisterValue(const RegisterValue &reg_val, Stream &s, const RegisterInfo &reg_info, bool prefix_with_name, bool prefix_with_alt_name, lldb::Format format, uint32_t reg_name_right_align_at=0, ExecutionContextScope *exe_scope=nullptr, bool print_flags=false, lldb::TargetSP target_sp=nullptr)
Definition: SBAddress.h:15
uint64_t offset_t
Definition: lldb-types.h:85
std::shared_ptr< lldb_private::Process > ProcessSP
Definition: lldb-forward.h:387
uint64_t addr_t
Definition: lldb-types.h:80
RegisterKind
Register numbering types.
@ eRegisterKindGeneric
insn ptr reg, stack ptr reg, etc not specific to any particular target
@ eRegisterKindLLDB
lldb's internal register numbers
@ eRegisterKindDWARF
the register numbers seen DWARF
@ eRegisterKindEHFrame
the register numbers seen in eh_frame
@ eRegisterKindProcessPlugin
num used by the process plugin - e.g.
void Dump(Stream &s, EmulateInstruction *instruction) const
union lldb_private::EmulateInstruction::Context::ContextInfo info
Every register is described in detail including its name, alternate name (optional),...
uint32_t byte_size
Size in bytes of the register.
uint32_t kinds[lldb::kNumRegisterKinds]
Holds all of the various register numbers for all register kinds.
const char * name
Name of this register, can't be NULL.
struct lldb_private::EmulateInstruction::Context::ContextInfo::ISAAndImmediateSigned ISAAndImmediateSigned
struct lldb_private::EmulateInstruction::Context::ContextInfo::RegisterRegisterOperands RegisterRegisterOperands
struct lldb_private::EmulateInstruction::Context::ContextInfo::RegisterPlusOffset RegisterPlusOffset
struct lldb_private::EmulateInstruction::Context::ContextInfo::RegisterPlusIndirectOffset RegisterPlusIndirectOffset
struct lldb_private::EmulateInstruction::Context::ContextInfo::RegisterToRegisterPlusIndirectOffset RegisterToRegisterPlusIndirectOffset
struct lldb_private::EmulateInstruction::Context::ContextInfo::RegisterToRegisterPlusOffset RegisterToRegisterPlusOffset
struct lldb_private::EmulateInstruction::Context::ContextInfo::ISAAndImmediate ISAAndImmediate