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