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/Core/Log.h"
13 #include "lldb/Core/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,
23 uint32_t concrete_frame_idx)
24 : m_thread(thread), m_concrete_frame_idx(concrete_frame_idx) {}
26 //----------------------------------------------------------------------
28 //----------------------------------------------------------------------
29 NativeRegisterContext::~NativeRegisterContext() {}
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.
37 // NativeRegisterContext::InvalidateIfNeeded (bool force)
39 // ProcessSP process_sp (m_thread.GetProcess());
40 // bool invalidate = force;
41 // uint32_t process_stop_id = UINT32_MAX;
44 // process_stop_id = process_sp->GetStopID();
49 // invalidate = process_stop_id != GetStopID();
53 // InvalidateAllRegisters ();
54 // SetStopID (process_stop_id);
59 NativeRegisterContext::GetRegisterInfoByName(llvm::StringRef reg_name,
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);
68 if (reg_name.equals_lower(reg_info->name) ||
69 reg_name.equals_lower(reg_info->alt_name))
75 const RegisterInfo *NativeRegisterContext::GetRegisterInfo(uint32_t kind,
77 const uint32_t reg_num = ConvertRegisterKindToRegisterNumber(kind, num);
78 if (reg_num == LLDB_INVALID_REGNUM)
80 return GetRegisterInfoAtIndex(reg_num);
83 const char *NativeRegisterContext::GetRegisterName(uint32_t reg) {
84 const RegisterInfo *reg_info = GetRegisterInfoAtIndex(reg);
86 return reg_info->name;
90 const char *NativeRegisterContext::GetRegisterSetNameForRegisterAtIndex(
91 uint32_t reg_index) const {
92 const RegisterInfo *const reg_info = GetRegisterInfoAtIndex(reg_index);
96 for (uint32_t set_index = 0; set_index < GetRegisterSetCount(); ++set_index) {
97 const RegisterSet *const reg_set = GetRegisterSet(set_index);
101 for (uint32_t reg_num_index = 0; reg_num_index < reg_set->num_registers;
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;
117 lldb::addr_t NativeRegisterContext::GetPC(lldb::addr_t fail_value) {
118 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
120 uint32_t reg = ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric,
121 LLDB_REGNUM_GENERIC_PC);
123 log->Printf("NativeRegisterContext::%s using reg index %" PRIu32
124 " (default %" PRIu64 ")",
125 __FUNCTION__, reg, fail_value);
127 const uint64_t retval = ReadRegisterAsUnsigned(reg, fail_value);
130 log->Printf("NativeRegisterContext::%s " PRIu32 " retval %" PRIu64,
131 __FUNCTION__, retval);
137 NativeRegisterContext::GetPCfromBreakpointLocation(lldb::addr_t fail_value) {
138 return GetPC(fail_value);
141 Error NativeRegisterContext::SetPC(lldb::addr_t pc) {
142 uint32_t reg = ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric,
143 LLDB_REGNUM_GENERIC_PC);
144 return WriteRegisterFromUnsigned(reg, pc);
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);
153 Error NativeRegisterContext::SetSP(lldb::addr_t sp) {
154 uint32_t reg = ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric,
155 LLDB_REGNUM_GENERIC_SP);
156 return WriteRegisterFromUnsigned(reg, sp);
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);
165 Error NativeRegisterContext::SetFP(lldb::addr_t fp) {
166 uint32_t reg = ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric,
167 LLDB_REGNUM_GENERIC_FP);
168 return WriteRegisterFromUnsigned(reg, fp);
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);
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);
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);
192 NativeRegisterContext::ReadRegisterAsUnsigned(const RegisterInfo *reg_info,
193 lldb::addr_t fail_value) {
194 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
198 Error error = ReadRegister(reg_info, value);
199 if (error.Success()) {
201 log->Printf("NativeRegisterContext::%s ReadRegister() succeeded, value "
203 __FUNCTION__, value.GetAsUInt64());
204 return value.GetAsUInt64();
207 log->Printf("NativeRegisterContext::%s ReadRegister() failed, error %s",
208 __FUNCTION__, error.AsCString());
212 log->Printf("NativeRegisterContext::%s ReadRegister() null reg_info",
218 Error NativeRegisterContext::WriteRegisterFromUnsigned(uint32_t reg,
220 if (reg == LLDB_INVALID_REGNUM)
221 return Error("NativeRegisterContext::%s (): reg is invalid", __FUNCTION__);
222 return WriteRegisterFromUnsigned(GetRegisterInfoAtIndex(reg), uval);
225 Error NativeRegisterContext::WriteRegisterFromUnsigned(
226 const RegisterInfo *reg_info, uint64_t uval) {
229 return Error("reg_info is nullptr");
232 if (!value.SetUInt(uval, reg_info->byte_size))
233 return Error("RegisterValue::SetUInt () failed");
235 return WriteRegister(reg_info, value);
238 lldb::tid_t NativeRegisterContext::GetThreadID() const {
239 return m_thread.GetID();
242 uint32_t NativeRegisterContext::NumSupportedHardwareBreakpoints() { return 0; }
244 uint32_t NativeRegisterContext::SetHardwareBreakpoint(lldb::addr_t addr,
246 return LLDB_INVALID_INDEX32;
249 bool NativeRegisterContext::ClearHardwareBreakpoint(uint32_t hw_idx) {
253 uint32_t NativeRegisterContext::NumSupportedHardwareWatchpoints() { return 0; }
255 uint32_t NativeRegisterContext::SetHardwareWatchpoint(lldb::addr_t addr,
257 uint32_t watch_flags) {
258 return LLDB_INVALID_INDEX32;
261 bool NativeRegisterContext::ClearHardwareWatchpoint(uint32_t hw_index) {
265 Error NativeRegisterContext::ClearAllHardwareWatchpoints() {
266 return Error("not implemented");
269 Error NativeRegisterContext::IsWatchpointHit(uint32_t wp_index, bool &is_hit) {
271 return Error("not implemented");
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");
280 Error NativeRegisterContext::IsWatchpointVacant(uint32_t wp_index,
283 return Error("not implemented");
286 lldb::addr_t NativeRegisterContext::GetWatchpointAddress(uint32_t wp_index) {
287 return LLDB_INVALID_ADDRESS;
290 lldb::addr_t NativeRegisterContext::GetWatchpointHitAddress(uint32_t wp_index) {
291 return LLDB_INVALID_ADDRESS;
294 bool NativeRegisterContext::HardwareSingleStep(bool enable) { return false; }
296 Error NativeRegisterContext::ReadRegisterValueFromMemory(
297 const RegisterInfo *reg_info, lldb::addr_t src_addr, size_t src_len,
298 RegisterValue ®_value) {
300 if (reg_info == nullptr) {
301 error.SetErrorString("invalid register info argument.");
305 // Moving from addr into a register
307 // Case 1: src_len == dst_len
309 // |AABBCCDD| Address contents
310 // |AABBCCDD| Register contents
312 // Case 2: src_len > dst_len
314 // Error! (The register should always be big enough to hold the data)
316 // Case 3: src_len < dst_len
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");
326 const size_t dst_len = reg_info->byte_size;
328 if (src_len > dst_len) {
329 error.SetErrorStringWithFormat(
330 "%" PRIu64 " bytes is too big to store in register %s (%" PRIu64
332 static_cast<uint64_t>(src_len), reg_info->name,
333 static_cast<uint64_t>(dst_len));
337 NativeProcessProtocolSP process_sp(m_thread.GetProcess());
339 error.SetErrorString("invalid process");
343 uint8_t src[RegisterValue::kMaxRegisterByteSize];
347 error = process_sp->ReadMemory(src_addr, src, src_len, bytes_read);
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));
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");
371 reg_value.SetFromMemoryData(reg_info, src, src_len, byte_order, error);
376 Error NativeRegisterContext::WriteRegisterValueToMemory(
377 const RegisterInfo *reg_info, lldb::addr_t dst_addr, size_t dst_len,
378 const RegisterValue ®_value) {
380 uint8_t dst[RegisterValue::kMaxRegisterByteSize];
384 NativeProcessProtocolSP process_sp(m_thread.GetProcess());
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
390 // they are the same.
391 lldb::ByteOrder byte_order;
392 if (!process_sp->GetByteOrder(byte_order))
393 return Error("NativeProcessProtocol::GetByteOrder () failed");
395 const size_t bytes_copied =
396 reg_value.GetAsMemoryData(reg_info, dst, dst_len, byte_order, error);
398 if (error.Success()) {
399 if (bytes_copied == 0) {
400 error.SetErrorString("byte copy failed.");
402 size_t bytes_written;
404 process_sp->WriteMemory(dst_addr, dst, bytes_copied, bytes_written);
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
412 static_cast<uint64_t>(bytes_written),
413 static_cast<uint64_t>(bytes_copied));
418 error.SetErrorString("invalid process");
424 NativeRegisterContext::ConvertRegisterKindToRegisterNumber(uint32_t kind,
425 uint32_t num) const {
426 const uint32_t num_regs = GetRegisterCount();
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);
432 if (reg_info->kinds[kind] == num)
436 return LLDB_INVALID_REGNUM;