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