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/common/NativeProcessProtocol.h"
16 #include "lldb/Host/common/NativeThreadProtocol.h"
19 using namespace lldb_private;
21 NativeRegisterContext::NativeRegisterContext (NativeThreadProtocol &thread, uint32_t concrete_frame_idx) :
23 m_concrete_frame_idx (concrete_frame_idx)
27 //----------------------------------------------------------------------
29 //----------------------------------------------------------------------
30 NativeRegisterContext::~NativeRegisterContext()
34 // FIXME revisit invalidation, process stop ids, etc. Right now we don't
35 // support caching in NativeRegisterContext. We can do this later by
36 // utilizing NativeProcessProtocol::GetStopID () and adding a stop id to
37 // NativeRegisterContext.
40 // NativeRegisterContext::InvalidateIfNeeded (bool force)
42 // ProcessSP process_sp (m_thread.GetProcess());
43 // bool invalidate = force;
44 // uint32_t process_stop_id = UINT32_MAX;
47 // process_stop_id = process_sp->GetStopID();
52 // invalidate = process_stop_id != GetStopID();
56 // InvalidateAllRegisters ();
57 // SetStopID (process_stop_id);
63 NativeRegisterContext::GetRegisterInfoByName (const char *reg_name, uint32_t start_idx)
65 if (reg_name && reg_name[0])
67 const uint32_t num_registers = GetRegisterCount();
68 for (uint32_t reg = start_idx; reg < num_registers; ++reg)
70 const RegisterInfo * reg_info = GetRegisterInfoAtIndex(reg);
72 if ((reg_info->name != nullptr && ::strcasecmp (reg_info->name, reg_name) == 0) ||
73 (reg_info->alt_name != nullptr && ::strcasecmp (reg_info->alt_name, reg_name) == 0))
83 NativeRegisterContext::GetRegisterInfo (uint32_t kind, uint32_t num)
85 const uint32_t reg_num = ConvertRegisterKindToRegisterNumber(kind, num);
86 if (reg_num == LLDB_INVALID_REGNUM)
88 return GetRegisterInfoAtIndex (reg_num);
92 NativeRegisterContext::GetRegisterName (uint32_t reg)
94 const RegisterInfo * reg_info = GetRegisterInfoAtIndex(reg);
96 return reg_info->name;
101 NativeRegisterContext::GetRegisterSetNameForRegisterAtIndex (uint32_t reg_index) const
103 const RegisterInfo *const reg_info = GetRegisterInfoAtIndex(reg_index);
107 for (uint32_t set_index = 0; set_index < GetRegisterSetCount (); ++set_index)
109 const RegisterSet *const reg_set = GetRegisterSet (set_index);
113 for (uint32_t reg_num_index = 0; reg_num_index < reg_set->num_registers; ++reg_num_index)
115 const uint32_t reg_num = reg_set->registers[reg_num_index];
116 // FIXME double check we're checking the right register kind here.
117 if (reg_info->kinds[RegisterKind::eRegisterKindLLDB] == reg_num)
119 // The given register is a member of this register set. Return the register set name.
120 return reg_set->name;
130 NativeRegisterContext::GetPC (lldb::addr_t fail_value)
132 Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
134 uint32_t reg = ConvertRegisterKindToRegisterNumber (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC);
136 log->Printf ("NativeRegisterContext::%s using reg index %" PRIu32 " (default %" PRIu64 ")", __FUNCTION__, reg, fail_value);
138 const uint64_t retval = ReadRegisterAsUnsigned (reg, fail_value);
141 log->Printf ("NativeRegisterContext::%s " PRIu32 " retval %" PRIu64, __FUNCTION__, retval);
147 NativeRegisterContext::GetPCfromBreakpointLocation (lldb::addr_t fail_value)
149 return GetPC (fail_value);
153 NativeRegisterContext::SetPC (lldb::addr_t pc)
155 uint32_t reg = ConvertRegisterKindToRegisterNumber (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC);
156 return WriteRegisterFromUnsigned (reg, pc);
160 NativeRegisterContext::GetSP (lldb::addr_t fail_value)
162 uint32_t reg = ConvertRegisterKindToRegisterNumber (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP);
163 return ReadRegisterAsUnsigned (reg, fail_value);
167 NativeRegisterContext::SetSP (lldb::addr_t sp)
169 uint32_t reg = ConvertRegisterKindToRegisterNumber (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP);
170 return WriteRegisterFromUnsigned (reg, sp);
174 NativeRegisterContext::GetFP (lldb::addr_t fail_value)
176 uint32_t reg = ConvertRegisterKindToRegisterNumber (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FP);
177 return ReadRegisterAsUnsigned (reg, fail_value);
181 NativeRegisterContext::SetFP (lldb::addr_t fp)
183 uint32_t reg = ConvertRegisterKindToRegisterNumber (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FP);
184 return WriteRegisterFromUnsigned (reg, fp);
188 NativeRegisterContext::GetReturnAddress (lldb::addr_t fail_value)
190 uint32_t reg = ConvertRegisterKindToRegisterNumber (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_RA);
191 return ReadRegisterAsUnsigned (reg, fail_value);
195 NativeRegisterContext::GetFlags (lldb::addr_t fail_value)
197 uint32_t reg = ConvertRegisterKindToRegisterNumber (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS);
198 return ReadRegisterAsUnsigned (reg, fail_value);
203 NativeRegisterContext::ReadRegisterAsUnsigned (uint32_t reg, lldb::addr_t fail_value)
205 if (reg != LLDB_INVALID_REGNUM)
206 return ReadRegisterAsUnsigned (GetRegisterInfoAtIndex (reg), fail_value);
211 NativeRegisterContext::ReadRegisterAsUnsigned (const RegisterInfo *reg_info, lldb::addr_t fail_value)
213 Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
218 Error error = ReadRegister (reg_info, value);
219 if (error.Success ())
222 log->Printf ("NativeRegisterContext::%s ReadRegister() succeeded, value %" PRIu64, __FUNCTION__, value.GetAsUInt64());
223 return value.GetAsUInt64();
228 log->Printf ("NativeRegisterContext::%s ReadRegister() failed, error %s", __FUNCTION__, error.AsCString ());
234 log->Printf ("NativeRegisterContext::%s ReadRegister() null reg_info", __FUNCTION__);
240 NativeRegisterContext::WriteRegisterFromUnsigned (uint32_t reg, uint64_t uval)
242 if (reg == LLDB_INVALID_REGNUM)
243 return Error ("NativeRegisterContext::%s (): reg is invalid", __FUNCTION__);
244 return WriteRegisterFromUnsigned (GetRegisterInfoAtIndex (reg), uval);
248 NativeRegisterContext::WriteRegisterFromUnsigned (const RegisterInfo *reg_info, uint64_t uval)
252 return Error ("reg_info is nullptr");
255 if (!value.SetUInt(uval, reg_info->byte_size))
256 return Error ("RegisterValue::SetUInt () failed");
258 return WriteRegister (reg_info, value);
262 NativeRegisterContext::GetThreadID() const
264 return m_thread.GetID();
268 NativeRegisterContext::NumSupportedHardwareBreakpoints ()
274 NativeRegisterContext::SetHardwareBreakpoint (lldb::addr_t addr, size_t size)
276 return LLDB_INVALID_INDEX32;
280 NativeRegisterContext::ClearHardwareBreakpoint (uint32_t hw_idx)
287 NativeRegisterContext::NumSupportedHardwareWatchpoints ()
293 NativeRegisterContext::SetHardwareWatchpoint (lldb::addr_t addr, size_t size, uint32_t watch_flags)
295 return LLDB_INVALID_INDEX32;
299 NativeRegisterContext::ClearHardwareWatchpoint (uint32_t hw_index)
305 NativeRegisterContext::ClearAllHardwareWatchpoints ()
307 return Error ("not implemented");
311 NativeRegisterContext::IsWatchpointHit(uint32_t wp_index, bool &is_hit)
314 return Error ("not implemented");
318 NativeRegisterContext::GetWatchpointHitIndex(uint32_t &wp_index, lldb::addr_t trap_addr)
320 wp_index = LLDB_INVALID_INDEX32;
321 return Error ("not implemented");
325 NativeRegisterContext::IsWatchpointVacant (uint32_t wp_index, bool &is_vacant)
328 return Error ("not implemented");
332 NativeRegisterContext::GetWatchpointAddress (uint32_t wp_index)
334 return LLDB_INVALID_ADDRESS;
338 NativeRegisterContext::GetWatchpointHitAddress (uint32_t wp_index)
340 return LLDB_INVALID_ADDRESS;
344 NativeRegisterContext::HardwareSingleStep (bool enable)
350 NativeRegisterContext::ReadRegisterValueFromMemory (
351 const RegisterInfo *reg_info,
352 lldb::addr_t src_addr,
354 RegisterValue ®_value)
357 if (reg_info == nullptr)
359 error.SetErrorString ("invalid register info argument.");
364 // Moving from addr into a register
366 // Case 1: src_len == dst_len
368 // |AABBCCDD| Address contents
369 // |AABBCCDD| Register contents
371 // Case 2: src_len > dst_len
373 // Error! (The register should always be big enough to hold the data)
375 // Case 3: src_len < dst_len
377 // |AABB| Address contents
378 // |AABB0000| Register contents [on little-endian hardware]
379 // |0000AABB| Register contents [on big-endian hardware]
380 if (src_len > RegisterValue::kMaxRegisterByteSize)
382 error.SetErrorString ("register too small to receive memory data");
386 const size_t dst_len = reg_info->byte_size;
388 if (src_len > dst_len)
390 error.SetErrorStringWithFormat("%" PRIu64 " bytes is too big to store in register %s (%" PRIu64 " bytes)",
391 static_cast<uint64_t>(src_len), reg_info->name, static_cast<uint64_t>(dst_len));
395 NativeProcessProtocolSP process_sp (m_thread.GetProcess ());
398 error.SetErrorString("invalid process");
402 uint8_t src[RegisterValue::kMaxRegisterByteSize];
406 error = process_sp->ReadMemory (src_addr, src, src_len, bytes_read);
410 // Make sure the memory read succeeded...
411 if (bytes_read != src_len)
413 // This might happen if we read _some_ bytes but not all
414 error.SetErrorStringWithFormat("read %" PRIu64 " of %" PRIu64 " bytes",
415 static_cast<uint64_t>(bytes_read), static_cast<uint64_t>(src_len));
419 // We now have a memory buffer that contains the part or all of the register
420 // value. Set the register value using this memory data.
421 // TODO: we might need to add a parameter to this function in case the byte
422 // order of the memory data doesn't match the process. For now we are assuming
423 // they are the same.
424 lldb::ByteOrder byte_order;
425 if (!process_sp->GetByteOrder (byte_order))
427 error.SetErrorString ( "NativeProcessProtocol::GetByteOrder () failed");
431 reg_value.SetFromMemoryData (
442 NativeRegisterContext::WriteRegisterValueToMemory (
443 const RegisterInfo *reg_info,
444 lldb::addr_t dst_addr,
446 const RegisterValue ®_value)
449 uint8_t dst[RegisterValue::kMaxRegisterByteSize];
453 NativeProcessProtocolSP process_sp (m_thread.GetProcess ());
457 // TODO: we might need to add a parameter to this function in case the byte
458 // order of the memory data doesn't match the process. For now we are assuming
459 // they are the same.
460 lldb::ByteOrder byte_order;
461 if (!process_sp->GetByteOrder (byte_order))
462 return Error ("NativeProcessProtocol::GetByteOrder () failed");
464 const size_t bytes_copied = reg_value.GetAsMemoryData (
473 if (bytes_copied == 0)
475 error.SetErrorString("byte copy failed.");
479 size_t bytes_written;
480 error = process_sp->WriteMemory(dst_addr, dst, bytes_copied, bytes_written);
484 if (bytes_written != bytes_copied)
486 // This might happen if we read _some_ bytes but not all
487 error.SetErrorStringWithFormat("only wrote %" PRIu64 " of %" PRIu64 " bytes",
488 static_cast<uint64_t>(bytes_written), static_cast<uint64_t>(bytes_copied));
494 error.SetErrorString("invalid process");
500 NativeRegisterContext::ConvertRegisterKindToRegisterNumber (uint32_t kind, uint32_t num) const
502 const uint32_t num_regs = GetRegisterCount();
504 assert (kind < kNumRegisterKinds);
505 for (uint32_t reg_idx = 0; reg_idx < num_regs; ++reg_idx)
507 const RegisterInfo *reg_info = GetRegisterInfoAtIndex (reg_idx);
509 if (reg_info->kinds[kind] == num)
513 return LLDB_INVALID_REGNUM;