]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Target/RegisterContext.cpp
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / Target / RegisterContext.cpp
1 //===-- RegisterContext.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/Target/RegisterContext.h"
11 #include "lldb/Core/Module.h"
12 #include "lldb/Core/Value.h"
13 #include "lldb/Expression/DWARFExpression.h"
14 #include "lldb/Target/ExecutionContext.h"
15 #include "lldb/Target/Process.h"
16 #include "lldb/Target/StackFrame.h"
17 #include "lldb/Target/Target.h"
18 #include "lldb/Target/Thread.h"
19 #include "lldb/Utility/DataExtractor.h"
20 #include "lldb/Utility/Endian.h"
21 #include "lldb/Utility/RegisterValue.h"
22 #include "lldb/Utility/Scalar.h"
23
24 using namespace lldb;
25 using namespace lldb_private;
26
27 RegisterContext::RegisterContext(Thread &thread, uint32_t concrete_frame_idx)
28     : m_thread(thread), m_concrete_frame_idx(concrete_frame_idx),
29       m_stop_id(thread.GetProcess()->GetStopID()) {}
30
31 RegisterContext::~RegisterContext() = default;
32
33 void RegisterContext::InvalidateIfNeeded(bool force) {
34   ProcessSP process_sp(m_thread.GetProcess());
35   bool invalidate = force;
36   uint32_t process_stop_id = UINT32_MAX;
37
38   if (process_sp)
39     process_stop_id = process_sp->GetStopID();
40   else
41     invalidate = true;
42
43   if (!invalidate)
44     invalidate = process_stop_id != GetStopID();
45
46   if (invalidate) {
47     InvalidateAllRegisters();
48     SetStopID(process_stop_id);
49   }
50 }
51
52 const RegisterInfo *
53 RegisterContext::GetRegisterInfoByName(llvm::StringRef reg_name,
54                                        uint32_t start_idx) {
55   if (reg_name.empty())
56     return nullptr;
57
58   const uint32_t num_registers = GetRegisterCount();
59   for (uint32_t reg = start_idx; reg < num_registers; ++reg) {
60     const RegisterInfo *reg_info = GetRegisterInfoAtIndex(reg);
61
62     if (reg_name.equals_lower(reg_info->name) ||
63         reg_name.equals_lower(reg_info->alt_name))
64       return reg_info;
65   }
66   return nullptr;
67 }
68
69 uint32_t
70 RegisterContext::UpdateDynamicRegisterSize(const lldb_private::ArchSpec &arch,
71                                            RegisterInfo *reg_info) {
72   ExecutionContext exe_ctx(CalculateThread());
73
74   // In MIPS, the floating point registers size is depends on FR bit of SR
75   // register. if SR.FR  == 1 then all floating point registers are 64 bits.
76   // else they are all 32 bits.
77
78   int expr_result;
79   uint32_t addr_size = arch.GetAddressByteSize();
80   const uint8_t *dwarf_opcode_ptr = reg_info->dynamic_size_dwarf_expr_bytes;
81   const size_t dwarf_opcode_len = reg_info->dynamic_size_dwarf_len;
82
83   DataExtractor dwarf_data(dwarf_opcode_ptr, dwarf_opcode_len,
84                            arch.GetByteOrder(), addr_size);
85   ModuleSP opcode_ctx;
86   DWARFExpression dwarf_expr(opcode_ctx, dwarf_data, nullptr, 0,
87                              dwarf_opcode_len);
88   Value result;
89   Status error;
90   const lldb::offset_t offset = 0;
91   if (dwarf_expr.Evaluate(&exe_ctx, this, opcode_ctx, dwarf_data, nullptr,
92                           offset, dwarf_opcode_len, eRegisterKindDWARF, nullptr,
93                           nullptr, result, &error)) {
94     expr_result = result.GetScalar().SInt(-1);
95     switch (expr_result) {
96     case 0:
97       return 4;
98     case 1:
99       return 8;
100     default:
101       return reg_info->byte_size;
102     }
103   } else {
104     printf("Error executing DwarfExpression::Evaluate %s\n", error.AsCString());
105     return reg_info->byte_size;
106   }
107 }
108
109 const RegisterInfo *RegisterContext::GetRegisterInfo(lldb::RegisterKind kind,
110                                                      uint32_t num) {
111   const uint32_t reg_num = ConvertRegisterKindToRegisterNumber(kind, num);
112   if (reg_num == LLDB_INVALID_REGNUM)
113     return nullptr;
114   return GetRegisterInfoAtIndex(reg_num);
115 }
116
117 const char *RegisterContext::GetRegisterName(uint32_t reg) {
118   const RegisterInfo *reg_info = GetRegisterInfoAtIndex(reg);
119   if (reg_info)
120     return reg_info->name;
121   return nullptr;
122 }
123
124 uint64_t RegisterContext::GetPC(uint64_t fail_value) {
125   uint32_t reg = ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric,
126                                                      LLDB_REGNUM_GENERIC_PC);
127   uint64_t pc = ReadRegisterAsUnsigned(reg, fail_value);
128
129   if (pc != fail_value) {
130     TargetSP target_sp = m_thread.CalculateTarget();
131     if (target_sp) {
132       Target *target = target_sp.get();
133       if (target)
134         pc = target->GetOpcodeLoadAddress(pc, AddressClass::eCode);
135     }
136   }
137
138   return pc;
139 }
140
141 bool RegisterContext::SetPC(uint64_t pc) {
142   uint32_t reg = ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric,
143                                                      LLDB_REGNUM_GENERIC_PC);
144   bool success = WriteRegisterFromUnsigned(reg, pc);
145   if (success) {
146     StackFrameSP frame_sp(
147         m_thread.GetFrameWithConcreteFrameIndex(m_concrete_frame_idx));
148     if (frame_sp)
149       frame_sp->ChangePC(pc);
150     else
151       m_thread.ClearStackFrames();
152   }
153   return success;
154 }
155
156 bool RegisterContext::SetPC(Address addr) {
157   TargetSP target_sp = m_thread.CalculateTarget();
158   Target *target = target_sp.get();
159
160   lldb::addr_t callAddr = addr.GetCallableLoadAddress(target);
161   if (callAddr == LLDB_INVALID_ADDRESS)
162     return false;
163
164   return SetPC(callAddr);
165 }
166
167 uint64_t RegisterContext::GetSP(uint64_t fail_value) {
168   uint32_t reg = ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric,
169                                                      LLDB_REGNUM_GENERIC_SP);
170   return ReadRegisterAsUnsigned(reg, fail_value);
171 }
172
173 bool RegisterContext::SetSP(uint64_t sp) {
174   uint32_t reg = ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric,
175                                                      LLDB_REGNUM_GENERIC_SP);
176   return WriteRegisterFromUnsigned(reg, sp);
177 }
178
179 uint64_t RegisterContext::GetFP(uint64_t fail_value) {
180   uint32_t reg = ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric,
181                                                      LLDB_REGNUM_GENERIC_FP);
182   return ReadRegisterAsUnsigned(reg, fail_value);
183 }
184
185 bool RegisterContext::SetFP(uint64_t fp) {
186   uint32_t reg = ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric,
187                                                      LLDB_REGNUM_GENERIC_FP);
188   return WriteRegisterFromUnsigned(reg, fp);
189 }
190
191 uint64_t RegisterContext::GetReturnAddress(uint64_t fail_value) {
192   uint32_t reg = ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric,
193                                                      LLDB_REGNUM_GENERIC_RA);
194   return ReadRegisterAsUnsigned(reg, fail_value);
195 }
196
197 uint64_t RegisterContext::GetFlags(uint64_t fail_value) {
198   uint32_t reg = ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric,
199                                                      LLDB_REGNUM_GENERIC_FLAGS);
200   return ReadRegisterAsUnsigned(reg, fail_value);
201 }
202
203 uint64_t RegisterContext::ReadRegisterAsUnsigned(uint32_t reg,
204                                                  uint64_t fail_value) {
205   if (reg != LLDB_INVALID_REGNUM)
206     return ReadRegisterAsUnsigned(GetRegisterInfoAtIndex(reg), fail_value);
207   return fail_value;
208 }
209
210 uint64_t RegisterContext::ReadRegisterAsUnsigned(const RegisterInfo *reg_info,
211                                                  uint64_t fail_value) {
212   if (reg_info) {
213     RegisterValue value;
214     if (ReadRegister(reg_info, value))
215       return value.GetAsUInt64();
216   }
217   return fail_value;
218 }
219
220 bool RegisterContext::WriteRegisterFromUnsigned(uint32_t reg, uint64_t uval) {
221   if (reg == LLDB_INVALID_REGNUM)
222     return false;
223   return WriteRegisterFromUnsigned(GetRegisterInfoAtIndex(reg), uval);
224 }
225
226 bool RegisterContext::WriteRegisterFromUnsigned(const RegisterInfo *reg_info,
227                                                 uint64_t uval) {
228   if (reg_info) {
229     RegisterValue value;
230     if (value.SetUInt(uval, reg_info->byte_size))
231       return WriteRegister(reg_info, value);
232   }
233   return false;
234 }
235
236 bool RegisterContext::CopyFromRegisterContext(lldb::RegisterContextSP context) {
237   uint32_t num_register_sets = context->GetRegisterSetCount();
238   // We don't know that two threads have the same register context, so require
239   // the threads to be the same.
240   if (context->GetThreadID() != GetThreadID())
241     return false;
242
243   if (num_register_sets != GetRegisterSetCount())
244     return false;
245
246   RegisterContextSP frame_zero_context = m_thread.GetRegisterContext();
247
248   for (uint32_t set_idx = 0; set_idx < num_register_sets; ++set_idx) {
249     const RegisterSet *const reg_set = GetRegisterSet(set_idx);
250
251     const uint32_t num_registers = reg_set->num_registers;
252     for (uint32_t reg_idx = 0; reg_idx < num_registers; ++reg_idx) {
253       const uint32_t reg = reg_set->registers[reg_idx];
254       const RegisterInfo *reg_info = GetRegisterInfoAtIndex(reg);
255       if (!reg_info || reg_info->value_regs)
256         continue;
257       RegisterValue reg_value;
258
259       // If we can reconstruct the register from the frame we are copying from,
260       // then do so, otherwise use the value from frame 0.
261       if (context->ReadRegister(reg_info, reg_value)) {
262         WriteRegister(reg_info, reg_value);
263       } else if (frame_zero_context->ReadRegister(reg_info, reg_value)) {
264         WriteRegister(reg_info, reg_value);
265       }
266     }
267   }
268   return true;
269 }
270
271 lldb::tid_t RegisterContext::GetThreadID() const { return m_thread.GetID(); }
272
273 uint32_t RegisterContext::NumSupportedHardwareBreakpoints() { return 0; }
274
275 uint32_t RegisterContext::SetHardwareBreakpoint(lldb::addr_t addr,
276                                                 size_t size) {
277   return LLDB_INVALID_INDEX32;
278 }
279
280 bool RegisterContext::ClearHardwareBreakpoint(uint32_t hw_idx) { return false; }
281
282 uint32_t RegisterContext::NumSupportedHardwareWatchpoints() { return 0; }
283
284 uint32_t RegisterContext::SetHardwareWatchpoint(lldb::addr_t addr, size_t size,
285                                                 bool read, bool write) {
286   return LLDB_INVALID_INDEX32;
287 }
288
289 bool RegisterContext::ClearHardwareWatchpoint(uint32_t hw_index) {
290   return false;
291 }
292
293 bool RegisterContext::HardwareSingleStep(bool enable) { return false; }
294
295 Status RegisterContext::ReadRegisterValueFromMemory(
296     const RegisterInfo *reg_info, lldb::addr_t src_addr, uint32_t src_len,
297     RegisterValue &reg_value) {
298   Status error;
299   if (reg_info == nullptr) {
300     error.SetErrorString("invalid register info argument.");
301     return error;
302   }
303
304   // Moving from addr into a register
305   //
306   // Case 1: src_len == dst_len
307   //
308   //   |AABBCCDD| Address contents
309   //   |AABBCCDD| Register contents
310   //
311   // Case 2: src_len > dst_len
312   //
313   //   Status!  (The register should always be big enough to hold the data)
314   //
315   // Case 3: src_len < dst_len
316   //
317   //   |AABB| Address contents
318   //   |AABB0000| Register contents [on little-endian hardware]
319   //   |0000AABB| Register contents [on big-endian hardware]
320   if (src_len > RegisterValue::kMaxRegisterByteSize) {
321     error.SetErrorString("register too small to receive memory data");
322     return error;
323   }
324
325   const uint32_t dst_len = reg_info->byte_size;
326
327   if (src_len > dst_len) {
328     error.SetErrorStringWithFormat(
329         "%u bytes is too big to store in register %s (%u bytes)", src_len,
330         reg_info->name, dst_len);
331     return error;
332   }
333
334   ProcessSP process_sp(m_thread.GetProcess());
335   if (process_sp) {
336     uint8_t src[RegisterValue::kMaxRegisterByteSize];
337
338     // Read the memory
339     const uint32_t bytes_read =
340         process_sp->ReadMemory(src_addr, src, src_len, error);
341
342     // Make sure the memory read succeeded...
343     if (bytes_read != src_len) {
344       if (error.Success()) {
345         // This might happen if we read _some_ bytes but not all
346         error.SetErrorStringWithFormat("read %u of %u bytes", bytes_read,
347                                        src_len);
348       }
349       return error;
350     }
351
352     // We now have a memory buffer that contains the part or all of the
353     // register value. Set the register value using this memory data.
354     // TODO: we might need to add a parameter to this function in case the byte
355     // order of the memory data doesn't match the process. For now we are
356     // assuming they are the same.
357     reg_value.SetFromMemoryData(reg_info, src, src_len,
358                                 process_sp->GetByteOrder(), error);
359   } else
360     error.SetErrorString("invalid process");
361
362   return error;
363 }
364
365 Status RegisterContext::WriteRegisterValueToMemory(
366     const RegisterInfo *reg_info, lldb::addr_t dst_addr, uint32_t dst_len,
367     const RegisterValue &reg_value) {
368   uint8_t dst[RegisterValue::kMaxRegisterByteSize];
369
370   Status error;
371
372   ProcessSP process_sp(m_thread.GetProcess());
373   if (process_sp) {
374
375     // TODO: we might need to add a parameter to this function in case the byte
376     // order of the memory data doesn't match the process. For now we are
377     // assuming they are the same.
378
379     const uint32_t bytes_copied = reg_value.GetAsMemoryData(
380         reg_info, dst, dst_len, process_sp->GetByteOrder(), error);
381
382     if (error.Success()) {
383       if (bytes_copied == 0) {
384         error.SetErrorString("byte copy failed.");
385       } else {
386         const uint32_t bytes_written =
387             process_sp->WriteMemory(dst_addr, dst, bytes_copied, error);
388         if (bytes_written != bytes_copied) {
389           if (error.Success()) {
390             // This might happen if we read _some_ bytes but not all
391             error.SetErrorStringWithFormat("only wrote %u of %u bytes",
392                                            bytes_written, bytes_copied);
393           }
394         }
395       }
396     }
397   } else
398     error.SetErrorString("invalid process");
399
400   return error;
401 }
402
403 bool RegisterContext::ReadAllRegisterValues(
404     lldb_private::RegisterCheckpoint &reg_checkpoint) {
405   return ReadAllRegisterValues(reg_checkpoint.GetData());
406 }
407
408 bool RegisterContext::WriteAllRegisterValues(
409     const lldb_private::RegisterCheckpoint &reg_checkpoint) {
410   return WriteAllRegisterValues(reg_checkpoint.GetData());
411 }
412
413 TargetSP RegisterContext::CalculateTarget() {
414   return m_thread.CalculateTarget();
415 }
416
417 ProcessSP RegisterContext::CalculateProcess() {
418   return m_thread.CalculateProcess();
419 }
420
421 ThreadSP RegisterContext::CalculateThread() {
422   return m_thread.shared_from_this();
423 }
424
425 StackFrameSP RegisterContext::CalculateStackFrame() {
426   // Register contexts might belong to many frames if we have inlined functions
427   // inside a frame since all inlined functions share the same registers, so we
428   // can't definitively say which frame we come from...
429   return StackFrameSP();
430 }
431
432 void RegisterContext::CalculateExecutionContext(ExecutionContext &exe_ctx) {
433   m_thread.CalculateExecutionContext(exe_ctx);
434 }
435
436 bool RegisterContext::ConvertBetweenRegisterKinds(lldb::RegisterKind source_rk,
437                                                   uint32_t source_regnum,
438                                                   lldb::RegisterKind target_rk,
439                                                   uint32_t &target_regnum) {
440   const uint32_t num_registers = GetRegisterCount();
441   for (uint32_t reg = 0; reg < num_registers; ++reg) {
442     const RegisterInfo *reg_info = GetRegisterInfoAtIndex(reg);
443
444     if (reg_info->kinds[source_rk] == source_regnum) {
445       target_regnum = reg_info->kinds[target_rk];
446       return (target_regnum != LLDB_INVALID_REGNUM);
447     }
448   }
449   return false;
450 }