1 //===-- NativeRegisterContext.cpp -------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Host/common/NativeRegisterContext.h"
12 #include "lldb/Utility/Log.h"
13 #include "lldb/Utility/RegisterValue.h"
15 #include "lldb/Host/PosixApi.h"
16 #include "lldb/Host/common/NativeProcessProtocol.h"
17 #include "lldb/Host/common/NativeThreadProtocol.h"
20 using namespace lldb_private;
22 NativeRegisterContext::NativeRegisterContext(NativeThreadProtocol &thread)
25 //----------------------------------------------------------------------
27 //----------------------------------------------------------------------
28 NativeRegisterContext::~NativeRegisterContext() {}
30 // FIXME revisit invalidation, process stop ids, etc. Right now we don't
31 // support caching in NativeRegisterContext. We can do this later by utilizing
32 // NativeProcessProtocol::GetStopID () and adding a stop id to
33 // NativeRegisterContext.
36 // NativeRegisterContext::InvalidateIfNeeded (bool force) {
37 // ProcessSP process_sp (m_thread.GetProcess());
38 // bool invalidate = force;
39 // uint32_t process_stop_id = UINT32_MAX;
42 // process_stop_id = process_sp->GetStopID();
47 // invalidate = process_stop_id != GetStopID();
51 // InvalidateAllRegisters ();
52 // SetStopID (process_stop_id);
57 NativeRegisterContext::GetRegisterInfoByName(llvm::StringRef reg_name,
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);
66 if (reg_name.equals_lower(reg_info->name) ||
67 reg_name.equals_lower(reg_info->alt_name))
73 const RegisterInfo *NativeRegisterContext::GetRegisterInfo(uint32_t kind,
75 const uint32_t reg_num = ConvertRegisterKindToRegisterNumber(kind, num);
76 if (reg_num == LLDB_INVALID_REGNUM)
78 return GetRegisterInfoAtIndex(reg_num);
81 const char *NativeRegisterContext::GetRegisterName(uint32_t reg) {
82 const RegisterInfo *reg_info = GetRegisterInfoAtIndex(reg);
84 return reg_info->name;
88 const char *NativeRegisterContext::GetRegisterSetNameForRegisterAtIndex(
89 uint32_t reg_index) const {
90 const RegisterInfo *const reg_info = GetRegisterInfoAtIndex(reg_index);
94 for (uint32_t set_index = 0; set_index < GetRegisterSetCount(); ++set_index) {
95 const RegisterSet *const reg_set = GetRegisterSet(set_index);
99 for (uint32_t reg_num_index = 0; reg_num_index < reg_set->num_registers;
101 const uint32_t reg_num = reg_set->registers[reg_num_index];
102 // FIXME double check we're checking the right register kind here.
103 if (reg_info->kinds[RegisterKind::eRegisterKindLLDB] == reg_num) {
104 // The given register is a member of this register set. Return the
105 // register set name.
106 return reg_set->name;
115 lldb::addr_t NativeRegisterContext::GetPC(lldb::addr_t fail_value) {
116 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
118 uint32_t reg = ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric,
119 LLDB_REGNUM_GENERIC_PC);
121 log->Printf("NativeRegisterContext::%s using reg index %" PRIu32
122 " (default %" PRIu64 ")",
123 __FUNCTION__, reg, fail_value);
125 const uint64_t retval = ReadRegisterAsUnsigned(reg, fail_value);
128 log->Printf("NativeRegisterContext::%s " PRIu32 " retval %" PRIu64,
129 __FUNCTION__, retval);
135 NativeRegisterContext::GetPCfromBreakpointLocation(lldb::addr_t fail_value) {
136 return GetPC(fail_value);
139 Status NativeRegisterContext::SetPC(lldb::addr_t pc) {
140 uint32_t reg = ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric,
141 LLDB_REGNUM_GENERIC_PC);
142 return WriteRegisterFromUnsigned(reg, pc);
145 lldb::addr_t NativeRegisterContext::GetSP(lldb::addr_t fail_value) {
146 uint32_t reg = ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric,
147 LLDB_REGNUM_GENERIC_SP);
148 return ReadRegisterAsUnsigned(reg, fail_value);
151 Status NativeRegisterContext::SetSP(lldb::addr_t sp) {
152 uint32_t reg = ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric,
153 LLDB_REGNUM_GENERIC_SP);
154 return WriteRegisterFromUnsigned(reg, sp);
157 lldb::addr_t NativeRegisterContext::GetFP(lldb::addr_t fail_value) {
158 uint32_t reg = ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric,
159 LLDB_REGNUM_GENERIC_FP);
160 return ReadRegisterAsUnsigned(reg, fail_value);
163 Status NativeRegisterContext::SetFP(lldb::addr_t fp) {
164 uint32_t reg = ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric,
165 LLDB_REGNUM_GENERIC_FP);
166 return WriteRegisterFromUnsigned(reg, fp);
169 lldb::addr_t NativeRegisterContext::GetReturnAddress(lldb::addr_t fail_value) {
170 uint32_t reg = ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric,
171 LLDB_REGNUM_GENERIC_RA);
172 return ReadRegisterAsUnsigned(reg, fail_value);
175 lldb::addr_t NativeRegisterContext::GetFlags(lldb::addr_t fail_value) {
176 uint32_t reg = ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric,
177 LLDB_REGNUM_GENERIC_FLAGS);
178 return ReadRegisterAsUnsigned(reg, fail_value);
182 NativeRegisterContext::ReadRegisterAsUnsigned(uint32_t reg,
183 lldb::addr_t fail_value) {
184 if (reg != LLDB_INVALID_REGNUM)
185 return ReadRegisterAsUnsigned(GetRegisterInfoAtIndex(reg), fail_value);
190 NativeRegisterContext::ReadRegisterAsUnsigned(const RegisterInfo *reg_info,
191 lldb::addr_t fail_value) {
192 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
196 Status error = ReadRegister(reg_info, value);
197 if (error.Success()) {
199 log->Printf("NativeRegisterContext::%s ReadRegister() succeeded, value "
201 __FUNCTION__, value.GetAsUInt64());
202 return value.GetAsUInt64();
205 log->Printf("NativeRegisterContext::%s ReadRegister() failed, error %s",
206 __FUNCTION__, error.AsCString());
210 log->Printf("NativeRegisterContext::%s ReadRegister() null reg_info",
216 Status NativeRegisterContext::WriteRegisterFromUnsigned(uint32_t reg,
218 if (reg == LLDB_INVALID_REGNUM)
219 return Status("NativeRegisterContext::%s (): reg is invalid", __FUNCTION__);
220 return WriteRegisterFromUnsigned(GetRegisterInfoAtIndex(reg), uval);
224 NativeRegisterContext::WriteRegisterFromUnsigned(const RegisterInfo *reg_info,
228 return Status("reg_info is nullptr");
231 if (!value.SetUInt(uval, reg_info->byte_size))
232 return Status("RegisterValue::SetUInt () failed");
234 return WriteRegister(reg_info, value);
237 lldb::tid_t NativeRegisterContext::GetThreadID() const {
238 return m_thread.GetID();
241 uint32_t NativeRegisterContext::NumSupportedHardwareBreakpoints() { return 0; }
243 uint32_t NativeRegisterContext::SetHardwareBreakpoint(lldb::addr_t addr,
245 return LLDB_INVALID_INDEX32;
248 Status NativeRegisterContext::ClearAllHardwareBreakpoints() {
249 return Status("not implemented");
252 bool NativeRegisterContext::ClearHardwareBreakpoint(uint32_t hw_idx) {
256 Status NativeRegisterContext::GetHardwareBreakHitIndex(uint32_t &bp_index,
257 lldb::addr_t trap_addr) {
258 bp_index = LLDB_INVALID_INDEX32;
259 return Status("not implemented");
262 uint32_t NativeRegisterContext::NumSupportedHardwareWatchpoints() { return 0; }
264 uint32_t NativeRegisterContext::SetHardwareWatchpoint(lldb::addr_t addr,
266 uint32_t watch_flags) {
267 return LLDB_INVALID_INDEX32;
270 bool NativeRegisterContext::ClearHardwareWatchpoint(uint32_t hw_index) {
274 Status NativeRegisterContext::ClearAllHardwareWatchpoints() {
275 return Status("not implemented");
278 Status NativeRegisterContext::IsWatchpointHit(uint32_t wp_index, bool &is_hit) {
280 return Status("not implemented");
283 Status NativeRegisterContext::GetWatchpointHitIndex(uint32_t &wp_index,
284 lldb::addr_t trap_addr) {
285 wp_index = LLDB_INVALID_INDEX32;
286 return Status("not implemented");
289 Status NativeRegisterContext::IsWatchpointVacant(uint32_t wp_index,
292 return Status("not implemented");
295 lldb::addr_t NativeRegisterContext::GetWatchpointAddress(uint32_t wp_index) {
296 return LLDB_INVALID_ADDRESS;
299 lldb::addr_t NativeRegisterContext::GetWatchpointHitAddress(uint32_t wp_index) {
300 return LLDB_INVALID_ADDRESS;
303 bool NativeRegisterContext::HardwareSingleStep(bool enable) { return false; }
305 Status NativeRegisterContext::ReadRegisterValueFromMemory(
306 const RegisterInfo *reg_info, lldb::addr_t src_addr, size_t src_len,
307 RegisterValue ®_value) {
309 if (reg_info == nullptr) {
310 error.SetErrorString("invalid register info argument.");
314 // Moving from addr into a register
316 // Case 1: src_len == dst_len
318 // |AABBCCDD| Address contents
319 // |AABBCCDD| Register contents
321 // Case 2: src_len > dst_len
323 // Status! (The register should always be big enough to hold the data)
325 // Case 3: src_len < dst_len
327 // |AABB| Address contents
328 // |AABB0000| Register contents [on little-endian hardware]
329 // |0000AABB| Register contents [on big-endian hardware]
330 if (src_len > RegisterValue::kMaxRegisterByteSize) {
331 error.SetErrorString("register too small to receive memory data");
335 const size_t dst_len = reg_info->byte_size;
337 if (src_len > dst_len) {
338 error.SetErrorStringWithFormat(
339 "%" PRIu64 " bytes is too big to store in register %s (%" PRIu64
341 static_cast<uint64_t>(src_len), reg_info->name,
342 static_cast<uint64_t>(dst_len));
346 NativeProcessProtocol &process = m_thread.GetProcess();
347 uint8_t src[RegisterValue::kMaxRegisterByteSize];
351 error = process.ReadMemory(src_addr, src, src_len, bytes_read);
355 // Make sure the memory read succeeded...
356 if (bytes_read != src_len) {
357 // This might happen if we read _some_ bytes but not all
358 error.SetErrorStringWithFormat("read %" PRIu64 " of %" PRIu64 " bytes",
359 static_cast<uint64_t>(bytes_read),
360 static_cast<uint64_t>(src_len));
364 // We now have a memory buffer that contains the part or all of the register
365 // value. Set the register value using this memory data.
366 // TODO: we might need to add a parameter to this function in case the byte
367 // order of the memory data doesn't match the process. For now we are
368 // assuming they are the same.
369 reg_value.SetFromMemoryData(reg_info, src, src_len, process.GetByteOrder(),
375 Status NativeRegisterContext::WriteRegisterValueToMemory(
376 const RegisterInfo *reg_info, lldb::addr_t dst_addr, size_t dst_len,
377 const RegisterValue ®_value) {
379 uint8_t dst[RegisterValue::kMaxRegisterByteSize];
383 NativeProcessProtocol &process = m_thread.GetProcess();
385 // TODO: we might need to add a parameter to this function in case the byte
386 // order of the memory data doesn't match the process. For now we are
387 // assuming they are the same.
388 const size_t bytes_copied = reg_value.GetAsMemoryData(
389 reg_info, dst, dst_len, process.GetByteOrder(), error);
391 if (error.Success()) {
392 if (bytes_copied == 0) {
393 error.SetErrorString("byte copy failed.");
395 size_t bytes_written;
396 error = process.WriteMemory(dst_addr, dst, bytes_copied, bytes_written);
400 if (bytes_written != bytes_copied) {
401 // This might happen if we read _some_ bytes but not all
402 error.SetErrorStringWithFormat("only wrote %" PRIu64 " of %" PRIu64
404 static_cast<uint64_t>(bytes_written),
405 static_cast<uint64_t>(bytes_copied));
414 NativeRegisterContext::ConvertRegisterKindToRegisterNumber(uint32_t kind,
415 uint32_t num) const {
416 const uint32_t num_regs = GetRegisterCount();
418 assert(kind < kNumRegisterKinds);
419 for (uint32_t reg_idx = 0; reg_idx < num_regs; ++reg_idx) {
420 const RegisterInfo *reg_info = GetRegisterInfoAtIndex(reg_idx);
422 if (reg_info->kinds[kind] == num)
426 return LLDB_INVALID_REGNUM;