]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - source/Core/EmulateInstruction.cpp
Vendor import of lldb trunk r290819:
[FreeBSD/FreeBSD.git] / source / Core / EmulateInstruction.cpp
1 //===-- EmulateInstruction.cpp ----------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "lldb/Core/EmulateInstruction.h"
11
12 // C Includes
13 // C++ Includes
14 #include <cstring>
15
16 // Other libraries and framework includes
17 // Project 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"
31
32 using namespace lldb;
33 using namespace lldb_private;
34
35 EmulateInstruction *
36 EmulateInstruction::FindPlugin(const ArchSpec &arch,
37                                InstructionType supported_inst_type,
38                                const char *plugin_name) {
39   EmulateInstructionCreateInstance create_callback = nullptr;
40   if (plugin_name) {
41     ConstString const_plugin_name(plugin_name);
42     create_callback =
43         PluginManager::GetEmulateInstructionCreateCallbackForPluginName(
44             const_plugin_name);
45     if (create_callback) {
46       EmulateInstruction *emulate_insn_ptr =
47           create_callback(arch, supported_inst_type);
48       if (emulate_insn_ptr)
49         return emulate_insn_ptr;
50     }
51   } else {
52     for (uint32_t idx = 0;
53          (create_callback =
54               PluginManager::GetEmulateInstructionCreateCallbackAtIndex(idx)) !=
55          nullptr;
56          ++idx) {
57       EmulateInstruction *emulate_insn_ptr =
58           create_callback(arch, supported_inst_type);
59       if (emulate_insn_ptr)
60         return emulate_insn_ptr;
61     }
62   }
63   return nullptr;
64 }
65
66 EmulateInstruction::EmulateInstruction(const ArchSpec &arch)
67     : m_arch(arch), m_baton(nullptr), m_read_mem_callback(&ReadMemoryDefault),
68       m_write_mem_callback(&WriteMemoryDefault),
69       m_read_reg_callback(&ReadRegisterDefault),
70       m_write_reg_callback(&WriteRegisterDefault),
71       m_addr(LLDB_INVALID_ADDRESS) {
72   ::memset(&m_opcode, 0, sizeof(m_opcode));
73 }
74
75 bool 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
82 bool 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
91 uint64_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
103 uint64_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
114 bool 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
122 bool 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
132 bool 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
145 bool 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
156 size_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
164 uint64_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
191 bool 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
202 bool 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
210 void EmulateInstruction::SetBaton(void *baton) { m_baton = baton; }
211
212 void 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
223 void EmulateInstruction::SetReadMemCallback(
224     ReadMemoryCallback read_mem_callback) {
225   m_read_mem_callback = read_mem_callback;
226 }
227
228 void EmulateInstruction::SetWriteMemCallback(
229     WriteMemoryCallback write_mem_callback) {
230   m_write_mem_callback = write_mem_callback;
231 }
232
233 void EmulateInstruction::SetReadRegCallback(
234     ReadRegisterCallback read_reg_callback) {
235   m_read_reg_callback = read_reg_callback;
236 }
237
238 void 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
247 size_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     Error error;
259     return process_sp->ReadMemory(addr, dst, dst_len, error);
260   }
261   return 0;
262 }
263
264 size_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     Error error;
276     return process_sp->WriteMemory(addr, src, src_len, error);
277   }
278
279   return 0;
280 }
281
282 bool 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
293 bool 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
304 size_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
319 size_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
333 bool 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
349 bool 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   reg_value.Dump(&strm, reg_info, false, false, eFormatDefault);
357   strm.PutCString(", context = ");
358   context.Dump(strm, instruction);
359   strm.EOL();
360   return true;
361 }
362
363 void 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
520 bool 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
534 bool 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
571 uint32_t
572 EmulateInstruction::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
581 bool EmulateInstruction::CreateFunctionEntryUnwind(UnwindPlan &unwind_plan) {
582   unwind_plan.Clear();
583   return false;
584 }