]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Host/common/NativeRegisterContext.cpp
MFV r314910: 7843 get_clones_stat() is suboptimal for lots of clones
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / Host / common / NativeRegisterContext.cpp
1 //===-- NativeRegisterContext.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/Host/common/NativeRegisterContext.h"
11
12 #include "lldb/Core/Log.h"
13 #include "lldb/Core/RegisterValue.h"
14
15 #include "lldb/Host/PosixApi.h"
16 #include "lldb/Host/common/NativeProcessProtocol.h"
17 #include "lldb/Host/common/NativeThreadProtocol.h"
18
19 using namespace lldb;
20 using namespace lldb_private;
21
22 NativeRegisterContext::NativeRegisterContext(NativeThreadProtocol &thread,
23                                              uint32_t concrete_frame_idx)
24     : m_thread(thread), m_concrete_frame_idx(concrete_frame_idx) {}
25
26 //----------------------------------------------------------------------
27 // Destructor
28 //----------------------------------------------------------------------
29 NativeRegisterContext::~NativeRegisterContext() {}
30
31 // FIXME revisit invalidation, process stop ids, etc.  Right now we don't
32 // support caching in NativeRegisterContext.  We can do this later by
33 // utilizing NativeProcessProtocol::GetStopID () and adding a stop id to
34 // NativeRegisterContext.
35
36 // void
37 // NativeRegisterContext::InvalidateIfNeeded (bool force)
38 // {
39 //     ProcessSP process_sp (m_thread.GetProcess());
40 //     bool invalidate = force;
41 //     uint32_t process_stop_id = UINT32_MAX;
42
43 //     if (process_sp)
44 //         process_stop_id = process_sp->GetStopID();
45 //     else
46 //         invalidate = true;
47
48 //     if (!invalidate)
49 //         invalidate = process_stop_id != GetStopID();
50
51 //     if (invalidate)
52 //     {
53 //         InvalidateAllRegisters ();
54 //         SetStopID (process_stop_id);
55 //     }
56 // }
57
58 const RegisterInfo *
59 NativeRegisterContext::GetRegisterInfoByName(llvm::StringRef reg_name,
60                                              uint32_t start_idx) {
61   if (reg_name.empty())
62     return nullptr;
63
64   const uint32_t num_registers = GetRegisterCount();
65   for (uint32_t reg = start_idx; reg < num_registers; ++reg) {
66     const RegisterInfo *reg_info = GetRegisterInfoAtIndex(reg);
67
68     if (reg_name.equals_lower(reg_info->name) ||
69         reg_name.equals_lower(reg_info->alt_name))
70       return reg_info;
71   }
72   return nullptr;
73 }
74
75 const RegisterInfo *NativeRegisterContext::GetRegisterInfo(uint32_t kind,
76                                                            uint32_t num) {
77   const uint32_t reg_num = ConvertRegisterKindToRegisterNumber(kind, num);
78   if (reg_num == LLDB_INVALID_REGNUM)
79     return nullptr;
80   return GetRegisterInfoAtIndex(reg_num);
81 }
82
83 const char *NativeRegisterContext::GetRegisterName(uint32_t reg) {
84   const RegisterInfo *reg_info = GetRegisterInfoAtIndex(reg);
85   if (reg_info)
86     return reg_info->name;
87   return nullptr;
88 }
89
90 const char *NativeRegisterContext::GetRegisterSetNameForRegisterAtIndex(
91     uint32_t reg_index) const {
92   const RegisterInfo *const reg_info = GetRegisterInfoAtIndex(reg_index);
93   if (!reg_info)
94     return nullptr;
95
96   for (uint32_t set_index = 0; set_index < GetRegisterSetCount(); ++set_index) {
97     const RegisterSet *const reg_set = GetRegisterSet(set_index);
98     if (!reg_set)
99       continue;
100
101     for (uint32_t reg_num_index = 0; reg_num_index < reg_set->num_registers;
102          ++reg_num_index) {
103       const uint32_t reg_num = reg_set->registers[reg_num_index];
104       // FIXME double check we're checking the right register kind here.
105       if (reg_info->kinds[RegisterKind::eRegisterKindLLDB] == reg_num) {
106         // The given register is a member of this register set.  Return the
107         // register set name.
108         return reg_set->name;
109       }
110     }
111   }
112
113   // Didn't find it.
114   return nullptr;
115 }
116
117 lldb::addr_t NativeRegisterContext::GetPC(lldb::addr_t fail_value) {
118   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
119
120   uint32_t reg = ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric,
121                                                      LLDB_REGNUM_GENERIC_PC);
122   if (log)
123     log->Printf("NativeRegisterContext::%s using reg index %" PRIu32
124                 " (default %" PRIu64 ")",
125                 __FUNCTION__, reg, fail_value);
126
127   const uint64_t retval = ReadRegisterAsUnsigned(reg, fail_value);
128
129   if (log)
130     log->Printf("NativeRegisterContext::%s " PRIu32 " retval %" PRIu64,
131                 __FUNCTION__, retval);
132
133   return retval;
134 }
135
136 lldb::addr_t
137 NativeRegisterContext::GetPCfromBreakpointLocation(lldb::addr_t fail_value) {
138   return GetPC(fail_value);
139 }
140
141 Error NativeRegisterContext::SetPC(lldb::addr_t pc) {
142   uint32_t reg = ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric,
143                                                      LLDB_REGNUM_GENERIC_PC);
144   return WriteRegisterFromUnsigned(reg, pc);
145 }
146
147 lldb::addr_t NativeRegisterContext::GetSP(lldb::addr_t fail_value) {
148   uint32_t reg = ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric,
149                                                      LLDB_REGNUM_GENERIC_SP);
150   return ReadRegisterAsUnsigned(reg, fail_value);
151 }
152
153 Error NativeRegisterContext::SetSP(lldb::addr_t sp) {
154   uint32_t reg = ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric,
155                                                      LLDB_REGNUM_GENERIC_SP);
156   return WriteRegisterFromUnsigned(reg, sp);
157 }
158
159 lldb::addr_t NativeRegisterContext::GetFP(lldb::addr_t fail_value) {
160   uint32_t reg = ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric,
161                                                      LLDB_REGNUM_GENERIC_FP);
162   return ReadRegisterAsUnsigned(reg, fail_value);
163 }
164
165 Error NativeRegisterContext::SetFP(lldb::addr_t fp) {
166   uint32_t reg = ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric,
167                                                      LLDB_REGNUM_GENERIC_FP);
168   return WriteRegisterFromUnsigned(reg, fp);
169 }
170
171 lldb::addr_t NativeRegisterContext::GetReturnAddress(lldb::addr_t fail_value) {
172   uint32_t reg = ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric,
173                                                      LLDB_REGNUM_GENERIC_RA);
174   return ReadRegisterAsUnsigned(reg, fail_value);
175 }
176
177 lldb::addr_t NativeRegisterContext::GetFlags(lldb::addr_t fail_value) {
178   uint32_t reg = ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric,
179                                                      LLDB_REGNUM_GENERIC_FLAGS);
180   return ReadRegisterAsUnsigned(reg, fail_value);
181 }
182
183 lldb::addr_t
184 NativeRegisterContext::ReadRegisterAsUnsigned(uint32_t reg,
185                                               lldb::addr_t fail_value) {
186   if (reg != LLDB_INVALID_REGNUM)
187     return ReadRegisterAsUnsigned(GetRegisterInfoAtIndex(reg), fail_value);
188   return fail_value;
189 }
190
191 uint64_t
192 NativeRegisterContext::ReadRegisterAsUnsigned(const RegisterInfo *reg_info,
193                                               lldb::addr_t fail_value) {
194   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
195
196   if (reg_info) {
197     RegisterValue value;
198     Error error = ReadRegister(reg_info, value);
199     if (error.Success()) {
200       if (log)
201         log->Printf("NativeRegisterContext::%s ReadRegister() succeeded, value "
202                     "%" PRIu64,
203                     __FUNCTION__, value.GetAsUInt64());
204       return value.GetAsUInt64();
205     } else {
206       if (log)
207         log->Printf("NativeRegisterContext::%s ReadRegister() failed, error %s",
208                     __FUNCTION__, error.AsCString());
209     }
210   } else {
211     if (log)
212       log->Printf("NativeRegisterContext::%s ReadRegister() null reg_info",
213                   __FUNCTION__);
214   }
215   return fail_value;
216 }
217
218 Error NativeRegisterContext::WriteRegisterFromUnsigned(uint32_t reg,
219                                                        uint64_t uval) {
220   if (reg == LLDB_INVALID_REGNUM)
221     return Error("NativeRegisterContext::%s (): reg is invalid", __FUNCTION__);
222   return WriteRegisterFromUnsigned(GetRegisterInfoAtIndex(reg), uval);
223 }
224
225 Error NativeRegisterContext::WriteRegisterFromUnsigned(
226     const RegisterInfo *reg_info, uint64_t uval) {
227   assert(reg_info);
228   if (!reg_info)
229     return Error("reg_info is nullptr");
230
231   RegisterValue value;
232   if (!value.SetUInt(uval, reg_info->byte_size))
233     return Error("RegisterValue::SetUInt () failed");
234
235   return WriteRegister(reg_info, value);
236 }
237
238 lldb::tid_t NativeRegisterContext::GetThreadID() const {
239   return m_thread.GetID();
240 }
241
242 uint32_t NativeRegisterContext::NumSupportedHardwareBreakpoints() { return 0; }
243
244 uint32_t NativeRegisterContext::SetHardwareBreakpoint(lldb::addr_t addr,
245                                                       size_t size) {
246   return LLDB_INVALID_INDEX32;
247 }
248
249 bool NativeRegisterContext::ClearHardwareBreakpoint(uint32_t hw_idx) {
250   return false;
251 }
252
253 uint32_t NativeRegisterContext::NumSupportedHardwareWatchpoints() { return 0; }
254
255 uint32_t NativeRegisterContext::SetHardwareWatchpoint(lldb::addr_t addr,
256                                                       size_t size,
257                                                       uint32_t watch_flags) {
258   return LLDB_INVALID_INDEX32;
259 }
260
261 bool NativeRegisterContext::ClearHardwareWatchpoint(uint32_t hw_index) {
262   return false;
263 }
264
265 Error NativeRegisterContext::ClearAllHardwareWatchpoints() {
266   return Error("not implemented");
267 }
268
269 Error NativeRegisterContext::IsWatchpointHit(uint32_t wp_index, bool &is_hit) {
270   is_hit = false;
271   return Error("not implemented");
272 }
273
274 Error NativeRegisterContext::GetWatchpointHitIndex(uint32_t &wp_index,
275                                                    lldb::addr_t trap_addr) {
276   wp_index = LLDB_INVALID_INDEX32;
277   return Error("not implemented");
278 }
279
280 Error NativeRegisterContext::IsWatchpointVacant(uint32_t wp_index,
281                                                 bool &is_vacant) {
282   is_vacant = false;
283   return Error("not implemented");
284 }
285
286 lldb::addr_t NativeRegisterContext::GetWatchpointAddress(uint32_t wp_index) {
287   return LLDB_INVALID_ADDRESS;
288 }
289
290 lldb::addr_t NativeRegisterContext::GetWatchpointHitAddress(uint32_t wp_index) {
291   return LLDB_INVALID_ADDRESS;
292 }
293
294 bool NativeRegisterContext::HardwareSingleStep(bool enable) { return false; }
295
296 Error NativeRegisterContext::ReadRegisterValueFromMemory(
297     const RegisterInfo *reg_info, lldb::addr_t src_addr, size_t src_len,
298     RegisterValue &reg_value) {
299   Error error;
300   if (reg_info == nullptr) {
301     error.SetErrorString("invalid register info argument.");
302     return error;
303   }
304
305   // Moving from addr into a register
306   //
307   // Case 1: src_len == dst_len
308   //
309   //   |AABBCCDD| Address contents
310   //   |AABBCCDD| Register contents
311   //
312   // Case 2: src_len > dst_len
313   //
314   //   Error!  (The register should always be big enough to hold the data)
315   //
316   // Case 3: src_len < dst_len
317   //
318   //   |AABB| Address contents
319   //   |AABB0000| Register contents [on little-endian hardware]
320   //   |0000AABB| Register contents [on big-endian hardware]
321   if (src_len > RegisterValue::kMaxRegisterByteSize) {
322     error.SetErrorString("register too small to receive memory data");
323     return error;
324   }
325
326   const size_t dst_len = reg_info->byte_size;
327
328   if (src_len > dst_len) {
329     error.SetErrorStringWithFormat(
330         "%" PRIu64 " bytes is too big to store in register %s (%" PRIu64
331         " bytes)",
332         static_cast<uint64_t>(src_len), reg_info->name,
333         static_cast<uint64_t>(dst_len));
334     return error;
335   }
336
337   NativeProcessProtocolSP process_sp(m_thread.GetProcess());
338   if (!process_sp) {
339     error.SetErrorString("invalid process");
340     return error;
341   }
342
343   uint8_t src[RegisterValue::kMaxRegisterByteSize];
344
345   // Read the memory
346   size_t bytes_read;
347   error = process_sp->ReadMemory(src_addr, src, src_len, bytes_read);
348   if (error.Fail())
349     return error;
350
351   // Make sure the memory read succeeded...
352   if (bytes_read != src_len) {
353     // This might happen if we read _some_ bytes but not all
354     error.SetErrorStringWithFormat("read %" PRIu64 " of %" PRIu64 " bytes",
355                                    static_cast<uint64_t>(bytes_read),
356                                    static_cast<uint64_t>(src_len));
357     return error;
358   }
359
360   // We now have a memory buffer that contains the part or all of the register
361   // value. Set the register value using this memory data.
362   // TODO: we might need to add a parameter to this function in case the byte
363   // order of the memory data doesn't match the process. For now we are assuming
364   // they are the same.
365   lldb::ByteOrder byte_order;
366   if (!process_sp->GetByteOrder(byte_order)) {
367     error.SetErrorString("NativeProcessProtocol::GetByteOrder () failed");
368     return error;
369   }
370
371   reg_value.SetFromMemoryData(reg_info, src, src_len, byte_order, error);
372
373   return error;
374 }
375
376 Error NativeRegisterContext::WriteRegisterValueToMemory(
377     const RegisterInfo *reg_info, lldb::addr_t dst_addr, size_t dst_len,
378     const RegisterValue &reg_value) {
379
380   uint8_t dst[RegisterValue::kMaxRegisterByteSize];
381
382   Error error;
383
384   NativeProcessProtocolSP process_sp(m_thread.GetProcess());
385   if (process_sp) {
386
387     // TODO: we might need to add a parameter to this function in case the byte
388     // order of the memory data doesn't match the process. For now we are
389     // assuming
390     // they are the same.
391     lldb::ByteOrder byte_order;
392     if (!process_sp->GetByteOrder(byte_order))
393       return Error("NativeProcessProtocol::GetByteOrder () failed");
394
395     const size_t bytes_copied =
396         reg_value.GetAsMemoryData(reg_info, dst, dst_len, byte_order, error);
397
398     if (error.Success()) {
399       if (bytes_copied == 0) {
400         error.SetErrorString("byte copy failed.");
401       } else {
402         size_t bytes_written;
403         error =
404             process_sp->WriteMemory(dst_addr, dst, bytes_copied, bytes_written);
405         if (error.Fail())
406           return error;
407
408         if (bytes_written != bytes_copied) {
409           // This might happen if we read _some_ bytes but not all
410           error.SetErrorStringWithFormat("only wrote %" PRIu64 " of %" PRIu64
411                                          " bytes",
412                                          static_cast<uint64_t>(bytes_written),
413                                          static_cast<uint64_t>(bytes_copied));
414         }
415       }
416     }
417   } else
418     error.SetErrorString("invalid process");
419
420   return error;
421 }
422
423 uint32_t
424 NativeRegisterContext::ConvertRegisterKindToRegisterNumber(uint32_t kind,
425                                                            uint32_t num) const {
426   const uint32_t num_regs = GetRegisterCount();
427
428   assert(kind < kNumRegisterKinds);
429   for (uint32_t reg_idx = 0; reg_idx < num_regs; ++reg_idx) {
430     const RegisterInfo *reg_info = GetRegisterInfoAtIndex(reg_idx);
431
432     if (reg_info->kinds[kind] == num)
433       return reg_idx;
434   }
435
436   return LLDB_INVALID_REGNUM;
437 }