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