1 //===-- NativeRegisterContext.cpp -----------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "lldb/Host/common/NativeRegisterContext.h"
11 #include "lldb/Utility/Log.h"
12 #include "lldb/Utility/RegisterValue.h"
14 #include "lldb/Host/PosixApi.h"
15 #include "lldb/Host/common/NativeProcessProtocol.h"
16 #include "lldb/Host/common/NativeThreadProtocol.h"
19 using namespace lldb_private;
21 NativeRegisterContext::NativeRegisterContext(NativeThreadProtocol &thread)
25 NativeRegisterContext::~NativeRegisterContext() = default;
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.
33 // NativeRegisterContext::InvalidateIfNeeded (bool force) {
34 // ProcessSP process_sp (m_thread.GetProcess());
35 // bool invalidate = force;
36 // uint32_t process_stop_id = UINT32_MAX;
39 // process_stop_id = process_sp->GetStopID();
44 // invalidate = process_stop_id != GetStopID();
48 // InvalidateAllRegisters ();
49 // SetStopID (process_stop_id);
54 NativeRegisterContext::GetRegisterInfoByName(llvm::StringRef reg_name,
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);
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);
74 if (reg_name.equals_insensitive(reg_info->name) ||
75 reg_name.equals_insensitive(reg_info->alt_name))
82 const RegisterInfo *NativeRegisterContext::GetRegisterInfo(uint32_t kind,
84 const uint32_t reg_num = ConvertRegisterKindToRegisterNumber(kind, num);
85 if (reg_num == LLDB_INVALID_REGNUM)
87 return GetRegisterInfoAtIndex(reg_num);
90 const char *NativeRegisterContext::GetRegisterName(uint32_t reg) {
91 const RegisterInfo *reg_info = GetRegisterInfoAtIndex(reg);
93 return reg_info->name;
97 const char *NativeRegisterContext::GetRegisterSetNameForRegisterAtIndex(
98 uint32_t reg_index) const {
99 const RegisterInfo *const reg_info = GetRegisterInfoAtIndex(reg_index);
103 for (uint32_t set_index = 0; set_index < GetRegisterSetCount(); ++set_index) {
104 const RegisterSet *const reg_set = GetRegisterSet(set_index);
108 for (uint32_t reg_num_index = 0; reg_num_index < reg_set->num_registers;
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;
124 lldb::addr_t NativeRegisterContext::GetPC(lldb::addr_t fail_value) {
125 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
127 uint32_t reg = ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric,
128 LLDB_REGNUM_GENERIC_PC);
130 "NativeRegisterContext::%s using reg index %" PRIu32
131 " (default %" PRIu64 ")",
132 __FUNCTION__, reg, fail_value);
134 const uint64_t retval = ReadRegisterAsUnsigned(reg, fail_value);
136 LLDB_LOGF(log, "NativeRegisterContext::%s " PRIu32 " retval %" PRIu64,
137 __FUNCTION__, retval);
143 NativeRegisterContext::GetPCfromBreakpointLocation(lldb::addr_t fail_value) {
144 return GetPC(fail_value);
147 Status NativeRegisterContext::SetPC(lldb::addr_t pc) {
148 uint32_t reg = ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric,
149 LLDB_REGNUM_GENERIC_PC);
150 return WriteRegisterFromUnsigned(reg, pc);
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);
159 Status NativeRegisterContext::SetSP(lldb::addr_t sp) {
160 uint32_t reg = ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric,
161 LLDB_REGNUM_GENERIC_SP);
162 return WriteRegisterFromUnsigned(reg, sp);
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);
171 Status NativeRegisterContext::SetFP(lldb::addr_t fp) {
172 uint32_t reg = ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric,
173 LLDB_REGNUM_GENERIC_FP);
174 return WriteRegisterFromUnsigned(reg, fp);
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);
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);
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);
198 NativeRegisterContext::ReadRegisterAsUnsigned(const RegisterInfo *reg_info,
199 lldb::addr_t fail_value) {
200 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
204 Status error = ReadRegister(reg_info, value);
205 if (error.Success()) {
207 "NativeRegisterContext::%s ReadRegister() succeeded, value "
209 __FUNCTION__, value.GetAsUInt64());
210 return value.GetAsUInt64();
213 "NativeRegisterContext::%s ReadRegister() failed, error %s",
214 __FUNCTION__, error.AsCString());
217 LLDB_LOGF(log, "NativeRegisterContext::%s ReadRegister() null reg_info",
223 Status NativeRegisterContext::WriteRegisterFromUnsigned(uint32_t reg,
225 if (reg == LLDB_INVALID_REGNUM)
226 return Status("NativeRegisterContext::%s (): reg is invalid", __FUNCTION__);
227 return WriteRegisterFromUnsigned(GetRegisterInfoAtIndex(reg), uval);
231 NativeRegisterContext::WriteRegisterFromUnsigned(const RegisterInfo *reg_info,
235 return Status("reg_info is nullptr");
238 if (!value.SetUInt(uval, reg_info->byte_size))
239 return Status("RegisterValue::SetUInt () failed");
241 return WriteRegister(reg_info, value);
244 lldb::tid_t NativeRegisterContext::GetThreadID() const {
245 return m_thread.GetID();
248 uint32_t NativeRegisterContext::NumSupportedHardwareBreakpoints() { return 0; }
250 uint32_t NativeRegisterContext::SetHardwareBreakpoint(lldb::addr_t addr,
252 return LLDB_INVALID_INDEX32;
255 Status NativeRegisterContext::ClearAllHardwareBreakpoints() {
256 return Status("not implemented");
259 bool NativeRegisterContext::ClearHardwareBreakpoint(uint32_t hw_idx) {
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");
269 uint32_t NativeRegisterContext::NumSupportedHardwareWatchpoints() { return 0; }
271 uint32_t NativeRegisterContext::SetHardwareWatchpoint(lldb::addr_t addr,
273 uint32_t watch_flags) {
274 return LLDB_INVALID_INDEX32;
277 bool NativeRegisterContext::ClearHardwareWatchpoint(uint32_t hw_index) {
281 Status NativeRegisterContext::ClearWatchpointHit(uint32_t hw_index) {
282 return Status("not implemented");
285 Status NativeRegisterContext::ClearAllHardwareWatchpoints() {
286 return Status("not implemented");
289 Status NativeRegisterContext::IsWatchpointHit(uint32_t wp_index, bool &is_hit) {
291 return Status("not implemented");
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");
300 Status NativeRegisterContext::IsWatchpointVacant(uint32_t wp_index,
303 return Status("not implemented");
306 lldb::addr_t NativeRegisterContext::GetWatchpointAddress(uint32_t wp_index) {
307 return LLDB_INVALID_ADDRESS;
310 lldb::addr_t NativeRegisterContext::GetWatchpointHitAddress(uint32_t wp_index) {
311 return LLDB_INVALID_ADDRESS;
314 bool NativeRegisterContext::HardwareSingleStep(bool enable) { return false; }
316 Status NativeRegisterContext::ReadRegisterValueFromMemory(
317 const RegisterInfo *reg_info, lldb::addr_t src_addr, size_t src_len,
318 RegisterValue ®_value) {
320 if (reg_info == nullptr) {
321 error.SetErrorString("invalid register info argument.");
325 // Moving from addr into a register
327 // Case 1: src_len == dst_len
329 // |AABBCCDD| Address contents
330 // |AABBCCDD| Register contents
332 // Case 2: src_len > dst_len
334 // Status! (The register should always be big enough to hold the data)
336 // Case 3: src_len < dst_len
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");
346 const size_t dst_len = reg_info->byte_size;
348 if (src_len > dst_len) {
349 error.SetErrorStringWithFormat(
350 "%" PRIu64 " bytes is too big to store in register %s (%" PRIu64
352 static_cast<uint64_t>(src_len), reg_info->name,
353 static_cast<uint64_t>(dst_len));
357 NativeProcessProtocol &process = m_thread.GetProcess();
358 uint8_t src[RegisterValue::kMaxRegisterByteSize];
362 error = process.ReadMemory(src_addr, src, src_len, bytes_read);
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));
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(),
386 Status NativeRegisterContext::WriteRegisterValueToMemory(
387 const RegisterInfo *reg_info, lldb::addr_t dst_addr, size_t dst_len,
388 const RegisterValue ®_value) {
390 uint8_t dst[RegisterValue::kMaxRegisterByteSize];
394 NativeProcessProtocol &process = m_thread.GetProcess();
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);
402 if (error.Success()) {
403 if (bytes_copied == 0) {
404 error.SetErrorString("byte copy failed.");
406 size_t bytes_written;
407 error = process.WriteMemory(dst_addr, dst, bytes_copied, bytes_written);
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
415 static_cast<uint64_t>(bytes_written),
416 static_cast<uint64_t>(bytes_copied));
425 NativeRegisterContext::ConvertRegisterKindToRegisterNumber(uint32_t kind,
426 uint32_t num) const {
427 const uint32_t num_regs = GetRegisterCount();
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);
433 if (reg_info->kinds[kind] == num)
437 return LLDB_INVALID_REGNUM;
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};
448 std::vector<uint32_t> expedited_reg_nums;
449 for (uint32_t gen_reg : k_expedited_registers) {
451 ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric, gen_reg);
452 if (reg_num == LLDB_INVALID_REGNUM)
453 continue; // Target does not support the given register.
455 expedited_reg_nums.push_back(reg_num);
458 return expedited_reg_nums;
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);
466 return std::vector<uint32_t>();