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
9#include "lldb/Core/EmulateInstruction.h"
10
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"
27
28#include "llvm/ADT/StringRef.h"
29
30#include <cstring>
31#include <memory>
32
33#include <cinttypes>
34#include <cstdio>
35
36namespace lldb_private {
37class Target;
38}
39
40using namespace lldb;
41using namespace lldb_private;
42
43EmulateInstruction *
44EmulateInstruction::FindPlugin(const ArchSpec &arch,
45 InstructionType supported_inst_type,
46 const char *plugin_name) {
47 EmulateInstructionCreateInstance create_callback = nullptr;
48 if (plugin_name) {
49 create_callback =
50 PluginManager::GetEmulateInstructionCreateCallbackForPluginName(
51 plugin_name);
52 if (create_callback) {
53 EmulateInstruction *emulate_insn_ptr =
54 create_callback(arch, supported_inst_type);
55 if (emulate_insn_ptr)
56 return emulate_insn_ptr;
57 }
58 } else {
59 for (uint32_t idx = 0;
60 (create_callback =
61 PluginManager::GetEmulateInstructionCreateCallbackAtIndex(idx)) !=
62 nullptr;
63 ++idx) {
64 EmulateInstruction *emulate_insn_ptr =
65 create_callback(arch, supported_inst_type);
66 if (emulate_insn_ptr)
67 return emulate_insn_ptr;
68 }
69 }
70 return nullptr;
71}
72
73EmulateInstruction::EmulateInstruction(const ArchSpec &arch) : m_arch(arch) {}
74
75bool EmulateInstruction::ReadRegister(const RegisterInfo *reg_info,
76 RegisterValue &reg_value) {
77 if (m_read_reg_callback != nullptr)
78 return m_read_reg_callback(this, m_baton, reg_info, reg_value);
79 return false;
80}
81
82bool EmulateInstruction::ReadRegister(lldb::RegisterKind reg_kind,
83 uint32_t reg_num,
84 RegisterValue &reg_value) {
85 RegisterInfo reg_info;
86 if (GetRegisterInfo(reg_kind, reg_num, reg_info))
87 return ReadRegister(&reg_info, reg_value);
88 return false;
89}
90
91uint64_t EmulateInstruction::ReadRegisterUnsigned(lldb::RegisterKind reg_kind,
92 uint32_t reg_num,
93 uint64_t fail_value,
94 bool *success_ptr) {
95 RegisterValue reg_value;
96 if (ReadRegister(reg_kind, reg_num, reg_value))
97 return reg_value.GetAsUInt64(fail_value, success_ptr);
98 if (success_ptr)
99 *success_ptr = false;
100 return fail_value;
101}
102
103uint64_t EmulateInstruction::ReadRegisterUnsigned(const RegisterInfo *reg_info,
104 uint64_t fail_value,
105 bool *success_ptr) {
106 RegisterValue reg_value;
107 if (ReadRegister(reg_info, reg_value))
108 return reg_value.GetAsUInt64(fail_value, success_ptr);
109 if (success_ptr)
110 *success_ptr = false;
111 return fail_value;
112}
113
114bool EmulateInstruction::WriteRegister(const Context &context,
115 const RegisterInfo *reg_info,
116 const RegisterValue &reg_value) {
117 if (m_write_reg_callback != nullptr)
118 return m_write_reg_callback(this, m_baton, context, reg_info, reg_value);
119 return false;
120}
121
122bool EmulateInstruction::WriteRegister(const Context &context,
123 lldb::RegisterKind reg_kind,
124 uint32_t reg_num,
125 const RegisterValue &reg_value) {
126 RegisterInfo reg_info;
127 if (GetRegisterInfo(reg_kind, reg_num, reg_info))
128 return WriteRegister(context, &reg_info, reg_value);
129 return false;
130}
131
132bool EmulateInstruction::WriteRegisterUnsigned(const Context &context,
133 lldb::RegisterKind reg_kind,
134 uint32_t reg_num,
135 uint64_t uint_value) {
136 RegisterInfo reg_info;
137 if (GetRegisterInfo(reg_kind, reg_num, reg_info)) {
138 RegisterValue reg_value;
139 if (reg_value.SetUInt(uint_value, reg_info.byte_size))
140 return WriteRegister(context, &reg_info, reg_value);
141 }
142 return false;
143}
144
145bool EmulateInstruction::WriteRegisterUnsigned(const Context &context,
146 const RegisterInfo *reg_info,
147 uint64_t uint_value) {
148 if (reg_info != nullptr) {
149 RegisterValue reg_value;
150 if (reg_value.SetUInt(uint_value, reg_info->byte_size))
151 return WriteRegister(context, reg_info, reg_value);
152 }
153 return false;
154}
155
156size_t EmulateInstruction::ReadMemory(const Context &context, lldb::addr_t addr,
157 void *dst, size_t dst_len) {
158 if (m_read_mem_callback != nullptr)
159 return m_read_mem_callback(this, m_baton, context, addr, dst, dst_len) ==
160 dst_len;
161 return false;
162}
163
164uint64_t EmulateInstruction::ReadMemoryUnsigned(const Context &context,
165 lldb::addr_t addr,
166 size_t byte_size,
167 uint64_t fail_value,
168 bool *success_ptr) {
169 uint64_t uval64 = 0;
170 bool success = false;
171 if (byte_size <= 8) {
172 uint8_t buf[sizeof(uint64_t)];
173 size_t bytes_read =
174 m_read_mem_callback(this, m_baton, context, addr, buf, byte_size);
175 if (bytes_read == byte_size) {
176 lldb::offset_t offset = 0;
177 DataExtractor data(buf, byte_size, GetByteOrder(), GetAddressByteSize());
178 uval64 = data.GetMaxU64(&offset, byte_size);
179 success = true;
180 }
181 }
182
183 if (success_ptr)
184 *success_ptr = success;
185
186 if (!success)
187 uval64 = fail_value;
188 return uval64;
189}
190
191bool EmulateInstruction::WriteMemoryUnsigned(const Context &context,
192 lldb::addr_t addr, uint64_t uval,
193 size_t uval_byte_size) {
194 StreamString strm(Stream::eBinary, GetAddressByteSize(), GetByteOrder());
195 strm.PutMaxHex64(uval, uval_byte_size);
196
197 size_t bytes_written = m_write_mem_callback(
198 this, m_baton, context, addr, strm.GetString().data(), uval_byte_size);
199 return (bytes_written == uval_byte_size);
200}
201
202bool EmulateInstruction::WriteMemory(const Context &context, lldb::addr_t addr,
203 const void *src, size_t src_len) {
204 if (m_write_mem_callback != nullptr)
205 return m_write_mem_callback(this, m_baton, context, addr, src, src_len) ==
206 src_len;
207 return false;
208}
209
210void EmulateInstruction::SetBaton(void *baton) { m_baton = baton; }
211
212void EmulateInstruction::SetCallbacks(
213 ReadMemoryCallback read_mem_callback,
214 WriteMemoryCallback write_mem_callback,
215 ReadRegisterCallback read_reg_callback,
216 WriteRegisterCallback write_reg_callback) {
217 m_read_mem_callback = read_mem_callback;
218 m_write_mem_callback = write_mem_callback;
219 m_read_reg_callback = read_reg_callback;
220 m_write_reg_callback = write_reg_callback;
221}
222
223void EmulateInstruction::SetReadMemCallback(
224 ReadMemoryCallback read_mem_callback) {
225 m_read_mem_callback = read_mem_callback;
226}
227
228void EmulateInstruction::SetWriteMemCallback(
229 WriteMemoryCallback write_mem_callback) {
230 m_write_mem_callback = write_mem_callback;
231}
232
233void EmulateInstruction::SetReadRegCallback(
234 ReadRegisterCallback read_reg_callback) {
235 m_read_reg_callback = read_reg_callback;
236}
237
238void EmulateInstruction::SetWriteRegCallback(
239 WriteRegisterCallback write_reg_callback) {
240 m_write_reg_callback = write_reg_callback;
241}
242
243//
244// Read & Write Memory and Registers callback functions.
245//
246
247size_t EmulateInstruction::ReadMemoryFrame(EmulateInstruction *instruction,
248 void *baton, const Context &context,
249 lldb::addr_t addr, void *dst,
250 size_t dst_len) {
251 if (baton == nullptr || dst == nullptr || dst_len == 0)
252 return 0;
253
254 StackFrame *frame = (StackFrame *)baton;
255
256 ProcessSP process_sp(frame->CalculateProcess());
257 if (process_sp) {
258 Status error;
259 return process_sp->ReadMemory(addr, dst, dst_len, error);
260 }
261 return 0;
262}
263
264size_t EmulateInstruction::WriteMemoryFrame(EmulateInstruction *instruction,
265 void *baton, const Context &context,
266 lldb::addr_t addr, const void *src,
267 size_t src_len) {
268 if (baton == nullptr || src == nullptr || src_len == 0)
269 return 0;
270
271 StackFrame *frame = (StackFrame *)baton;
272
273 ProcessSP process_sp(frame->CalculateProcess());
274 if (process_sp) {
275 Status error;
276 return process_sp->WriteMemory(addr, src, src_len, error);
277 }
278
279 return 0;
280}
281
282bool EmulateInstruction::ReadRegisterFrame(EmulateInstruction *instruction,
283 void *baton,
284 const RegisterInfo *reg_info,
285 RegisterValue &reg_value) {
286 if (baton == nullptr)
287 return false;
288
289 StackFrame *frame = (StackFrame *)baton;
290 return frame->GetRegisterContext()->ReadRegister(reg_info, reg_value);
291}
292
293bool EmulateInstruction::WriteRegisterFrame(EmulateInstruction *instruction,
294 void *baton, const Context &context,
295 const RegisterInfo *reg_info,
296 const RegisterValue &reg_value) {
297 if (baton == nullptr)
298 return false;
299
300 StackFrame *frame = (StackFrame *)baton;
301 return frame->GetRegisterContext()->WriteRegister(reg_info, reg_value);
302}
303
304size_t EmulateInstruction::ReadMemoryDefault(EmulateInstruction *instruction,
305 void *baton,
306 const Context &context,
307 lldb::addr_t addr, void *dst,
308 size_t length) {
309 StreamFile strm(stdout, false);
310 strm.Printf(" Read from Memory (address = 0x%" PRIx64 ", length = %" PRIu64
311 ", context = ",
312 addr, (uint64_t)length);
313 context.Dump(strm, instruction);
314 strm.EOL();
315 *((uint64_t *)dst) = 0xdeadbeef;
316 return length;
317}
318
319size_t EmulateInstruction::WriteMemoryDefault(EmulateInstruction *instruction,
320 void *baton,
321 const Context &context,
322 lldb::addr_t addr,
323 const void *dst, size_t length) {
324 StreamFile strm(stdout, false);
325 strm.Printf(" Write to Memory (address = 0x%" PRIx64 ", length = %" PRIu64
326 ", context = ",
327 addr, (uint64_t)length);
328 context.Dump(strm, instruction);
329 strm.EOL();
330 return length;
331}
332
333bool EmulateInstruction::ReadRegisterDefault(EmulateInstruction *instruction,
334 void *baton,
335 const RegisterInfo *reg_info,
336 RegisterValue &reg_value) {
337 StreamFile strm(stdout, false);
338 strm.Printf(" Read Register (%s)\n", reg_info->name);
339 lldb::RegisterKind reg_kind;
340 uint32_t reg_num;
341 if (GetBestRegisterKindAndNumber(reg_info, reg_kind, reg_num))
342 reg_value.SetUInt64((uint64_t)reg_kind << 24 | reg_num);
343 else
344 reg_value.SetUInt64(0);
345
346 return true;
347}
348
349bool EmulateInstruction::WriteRegisterDefault(EmulateInstruction *instruction,
350 void *baton,
351 const Context &context,
352 const RegisterInfo *reg_info,
353 const RegisterValue &reg_value) {
354 StreamFile strm(stdout, false);
355 strm.Printf(" Write to Register (name = %s, value = ", reg_info->name);
356 DumpRegisterValue(reg_value, &strm, reg_info, false, false, eFormatDefault);
357 strm.PutCString(", context = ");
358 context.Dump(strm, instruction);
359 strm.EOL();
360 return true;
361}
362
363void EmulateInstruction::Context::Dump(Stream &strm,
364 EmulateInstruction *instruction) const {
365 switch (type) {
366 case eContextReadOpcode:
367 strm.PutCString("reading opcode");
368 break;
369
370 case eContextImmediate:
371 strm.PutCString("immediate");
372 break;
373
374 case eContextPushRegisterOnStack:
375 strm.PutCString("push register");
376 break;
377
378 case eContextPopRegisterOffStack:
379 strm.PutCString("pop register");
380 break;
381
382 case eContextAdjustStackPointer:
383 strm.PutCString("adjust sp");
384 break;
385
386 case eContextSetFramePointer:
387 strm.PutCString("set frame pointer");
388 break;
389
390 case eContextAdjustBaseRegister:
391 strm.PutCString("adjusting (writing value back to) a base register");
392 break;
393
394 case eContextRegisterPlusOffset:
395 strm.PutCString("register + offset");
396 break;
397
398 case eContextRegisterStore:
399 strm.PutCString("store register");
400 break;
401
402 case eContextRegisterLoad:
403 strm.PutCString("load register");
404 break;
405
406 case eContextRelativeBranchImmediate:
407 strm.PutCString("relative branch immediate");
408 break;
409
410 case eContextAbsoluteBranchRegister:
411 strm.PutCString("absolute branch register");
412 break;
413
414 case eContextSupervisorCall:
415 strm.PutCString("supervisor call");
416 break;
417
418 case eContextTableBranchReadMemory:
419 strm.PutCString("table branch read memory");
420 break;
421
422 case eContextWriteRegisterRandomBits:
423 strm.PutCString("write random bits to a register");
424 break;
425
426 case eContextWriteMemoryRandomBits:
427 strm.PutCString("write random bits to a memory address");
428 break;
429
430 case eContextArithmetic:
431 strm.PutCString("arithmetic");
432 break;
433
434 case eContextReturnFromException:
435 strm.PutCString("return from exception");
436 break;
437
438 default:
439 strm.PutCString("unrecognized context.");
440 break;
441 }
442
443 switch (info_type) {
444 case eInfoTypeRegisterPlusOffset:
445 strm.Printf(" (reg_plus_offset = %s%+" PRId64 ")",
446 info.RegisterPlusOffset.reg.name,
447 info.RegisterPlusOffset.signed_offset);
448 break;
449
450 case eInfoTypeRegisterPlusIndirectOffset:
451 strm.Printf(" (reg_plus_reg = %s + %s)",
452 info.RegisterPlusIndirectOffset.base_reg.name,
453 info.RegisterPlusIndirectOffset.offset_reg.name);
454 break;
455
456 case eInfoTypeRegisterToRegisterPlusOffset:
457 strm.Printf(" (base_and_imm_offset = %s%+" PRId64 ", data_reg = %s)",
458 info.RegisterToRegisterPlusOffset.base_reg.name,
459 info.RegisterToRegisterPlusOffset.offset,
460 info.RegisterToRegisterPlusOffset.data_reg.name);
461 break;
462
463 case eInfoTypeRegisterToRegisterPlusIndirectOffset:
464 strm.Printf(" (base_and_reg_offset = %s + %s, data_reg = %s)",
465 info.RegisterToRegisterPlusIndirectOffset.base_reg.name,
466 info.RegisterToRegisterPlusIndirectOffset.offset_reg.name,
467 info.RegisterToRegisterPlusIndirectOffset.data_reg.name);
468 break;
469
470 case eInfoTypeRegisterRegisterOperands:
471 strm.Printf(" (register to register binary op: %s and %s)",
472 info.RegisterRegisterOperands.operand1.name,
473 info.RegisterRegisterOperands.operand2.name);
474 break;
475
476 case eInfoTypeOffset:
477 strm.Printf(" (signed_offset = %+" PRId64 ")", info.signed_offset);
478 break;
479
480 case eInfoTypeRegister:
481 strm.Printf(" (reg = %s)", info.reg.name);
482 break;
483
484 case eInfoTypeImmediate:
485 strm.Printf(" (unsigned_immediate = %" PRIu64 " (0x%16.16" PRIx64 "))",
486 info.unsigned_immediate, info.unsigned_immediate);
487 break;
488
489 case eInfoTypeImmediateSigned:
490 strm.Printf(" (signed_immediate = %+" PRId64 " (0x%16.16" PRIx64 "))",
491 info.signed_immediate, info.signed_immediate);
492 break;
493
494 case eInfoTypeAddress:
495 strm.Printf(" (address = 0x%" PRIx64 ")", info.address);
496 break;
497
498 case eInfoTypeISAAndImmediate:
499 strm.Printf(" (isa = %u, unsigned_immediate = %u (0x%8.8x))",
500 info.ISAAndImmediate.isa, info.ISAAndImmediate.unsigned_data32,
501 info.ISAAndImmediate.unsigned_data32);
502 break;
503
504 case eInfoTypeISAAndImmediateSigned:
505 strm.Printf(" (isa = %u, signed_immediate = %i (0x%8.8x))",
506 info.ISAAndImmediateSigned.isa,
507 info.ISAAndImmediateSigned.signed_data32,
508 info.ISAAndImmediateSigned.signed_data32);
509 break;
510
511 case eInfoTypeISA:
512 strm.Printf(" (isa = %u)", info.isa);
513 break;
514
515 case eInfoTypeNoArgs:
516 break;
517 }
518}
519
520bool EmulateInstruction::SetInstruction(const Opcode &opcode,
521 const Address &inst_addr,
522 Target *target) {
523 m_opcode = opcode;
524 m_addr = LLDB_INVALID_ADDRESS;
525 if (inst_addr.IsValid()) {
526 if (target != nullptr)
527 m_addr = inst_addr.GetLoadAddress(target);
528 if (m_addr == LLDB_INVALID_ADDRESS)
529 m_addr = inst_addr.GetFileAddress();
530 }
531 return true;
532}
533
534bool EmulateInstruction::GetBestRegisterKindAndNumber(
535 const RegisterInfo *reg_info, lldb::RegisterKind &reg_kind,
536 uint32_t &reg_num) {
537 // Generic and DWARF should be the two most popular register kinds when
538 // emulating instructions since they are the most platform agnostic...
539 reg_num = reg_info->kinds[eRegisterKindGeneric];
540 if (reg_num != LLDB_INVALID_REGNUM) {
541 reg_kind = eRegisterKindGeneric;
542 return true;
543 }
544
545 reg_num = reg_info->kinds[eRegisterKindDWARF];
546 if (reg_num != LLDB_INVALID_REGNUM) {
547 reg_kind = eRegisterKindDWARF;
548 return true;
549 }
550
551 reg_num = reg_info->kinds[eRegisterKindLLDB];
552 if (reg_num != LLDB_INVALID_REGNUM) {
553 reg_kind = eRegisterKindLLDB;
554 return true;
555 }
556
557 reg_num = reg_info->kinds[eRegisterKindEHFrame];
558 if (reg_num != LLDB_INVALID_REGNUM) {
559 reg_kind = eRegisterKindEHFrame;
560 return true;
561 }
562
563 reg_num = reg_info->kinds[eRegisterKindProcessPlugin];
564 if (reg_num != LLDB_INVALID_REGNUM) {
565 reg_kind = eRegisterKindProcessPlugin;
566 return true;
567 }
568 return false;
569}
570
571uint32_t
572EmulateInstruction::GetInternalRegisterNumber(RegisterContext *reg_ctx,
573 const RegisterInfo &reg_info) {
574 lldb::RegisterKind reg_kind;
575 uint32_t reg_num;
576 if (reg_ctx && GetBestRegisterKindAndNumber(&reg_info, reg_kind, reg_num))
577 return reg_ctx->ConvertRegisterKindToRegisterNumber(reg_kind, reg_num);
578 return LLDB_INVALID_REGNUM;
579}
580
581bool EmulateInstruction::CreateFunctionEntryUnwind(UnwindPlan &unwind_plan) {
582 unwind_plan.Clear();
583 return false;
584}
585

source code of lldb/source/Core/EmulateInstruction.cpp