1254721Semaste//===-- RegisterContextThreadMemory.cpp -------------------------*- C++ -*-===// 2254721Semaste// 3254721Semaste// The LLVM Compiler Infrastructure 4254721Semaste// 5254721Semaste// This file is distributed under the University of Illinois Open Source 6254721Semaste// License. See LICENSE.TXT for details. 7254721Semaste// 8254721Semaste//===----------------------------------------------------------------------===// 9254721Semaste 10254721Semaste#include "lldb/lldb-private.h" 11254721Semaste#include "lldb/Core/Error.h" 12254721Semaste#include "lldb/Target/OperatingSystem.h" 13254721Semaste#include "lldb/Target/Process.h" 14254721Semaste#include "lldb/Target/Thread.h" 15254721Semaste 16254721Semaste#include "RegisterContextThreadMemory.h" 17254721Semaste 18254721Semasteusing namespace lldb; 19254721Semasteusing namespace lldb_private; 20254721Semaste 21254721SemasteRegisterContextThreadMemory::RegisterContextThreadMemory (Thread &thread, 22254721Semaste lldb::addr_t register_data_addr) : 23254721Semaste RegisterContext (thread, 0), 24254721Semaste m_thread_wp (thread.shared_from_this()), 25254721Semaste m_reg_ctx_sp (), 26254721Semaste m_register_data_addr (register_data_addr), 27254721Semaste m_stop_id(0) 28254721Semaste{ 29254721Semaste} 30254721Semaste 31254721SemasteRegisterContextThreadMemory::~RegisterContextThreadMemory() 32254721Semaste{ 33254721Semaste} 34254721Semaste 35254721Semastevoid 36254721SemasteRegisterContextThreadMemory::UpdateRegisterContext () 37254721Semaste{ 38254721Semaste ThreadSP thread_sp (m_thread_wp.lock()); 39254721Semaste if (thread_sp) 40254721Semaste { 41254721Semaste ProcessSP process_sp (thread_sp->GetProcess()); 42254721Semaste 43254721Semaste if (process_sp) 44254721Semaste { 45254721Semaste const uint32_t stop_id = process_sp->GetModID().GetStopID(); 46254721Semaste if (m_stop_id != stop_id) 47254721Semaste { 48254721Semaste m_stop_id = stop_id; 49254721Semaste m_reg_ctx_sp.reset(); 50254721Semaste } 51254721Semaste if (!m_reg_ctx_sp) 52254721Semaste { 53254721Semaste ThreadSP backing_thread_sp (thread_sp->GetBackingThread()); 54254721Semaste if (backing_thread_sp) 55254721Semaste { 56254721Semaste m_reg_ctx_sp = backing_thread_sp->GetRegisterContext(); 57254721Semaste } 58254721Semaste else 59254721Semaste { 60254721Semaste OperatingSystem *os = process_sp->GetOperatingSystem (); 61254721Semaste if (os->IsOperatingSystemPluginThread (thread_sp)) 62254721Semaste m_reg_ctx_sp = os->CreateRegisterContextForThread (thread_sp.get(), LLDB_INVALID_ADDRESS); 63254721Semaste } 64254721Semaste } 65254721Semaste } 66254721Semaste else 67254721Semaste { 68254721Semaste m_reg_ctx_sp.reset(); 69254721Semaste } 70254721Semaste } 71254721Semaste else 72254721Semaste { 73254721Semaste m_reg_ctx_sp.reset(); 74254721Semaste } 75254721Semaste} 76254721Semaste 77254721Semaste//------------------------------------------------------------------ 78254721Semaste// Subclasses must override these functions 79254721Semaste//------------------------------------------------------------------ 80254721Semastevoid 81254721SemasteRegisterContextThreadMemory::InvalidateAllRegisters () 82254721Semaste{ 83254721Semaste UpdateRegisterContext (); 84254721Semaste if (m_reg_ctx_sp) 85254721Semaste m_reg_ctx_sp->InvalidateAllRegisters(); 86254721Semaste} 87254721Semaste 88254721Semastesize_t 89254721SemasteRegisterContextThreadMemory::GetRegisterCount () 90254721Semaste{ 91254721Semaste UpdateRegisterContext (); 92254721Semaste if (m_reg_ctx_sp) 93254721Semaste return m_reg_ctx_sp->GetRegisterCount(); 94254721Semaste return 0; 95254721Semaste} 96254721Semaste 97254721Semasteconst RegisterInfo * 98254721SemasteRegisterContextThreadMemory::GetRegisterInfoAtIndex (size_t reg) 99254721Semaste{ 100254721Semaste UpdateRegisterContext (); 101254721Semaste if (m_reg_ctx_sp) 102254721Semaste return m_reg_ctx_sp->GetRegisterInfoAtIndex(reg); 103254721Semaste return NULL; 104254721Semaste} 105254721Semaste 106254721Semastesize_t 107254721SemasteRegisterContextThreadMemory::GetRegisterSetCount () 108254721Semaste{ 109254721Semaste UpdateRegisterContext (); 110254721Semaste if (m_reg_ctx_sp) 111254721Semaste return m_reg_ctx_sp->GetRegisterSetCount(); 112254721Semaste return 0; 113254721Semaste} 114254721Semaste 115254721Semasteconst RegisterSet * 116254721SemasteRegisterContextThreadMemory::GetRegisterSet (size_t reg_set) 117254721Semaste{ 118254721Semaste UpdateRegisterContext (); 119254721Semaste if (m_reg_ctx_sp) 120254721Semaste return m_reg_ctx_sp->GetRegisterSet(reg_set); 121254721Semaste return NULL; 122254721Semaste} 123254721Semaste 124254721Semastebool 125254721SemasteRegisterContextThreadMemory::ReadRegister (const RegisterInfo *reg_info, RegisterValue ®_value) 126254721Semaste{ 127254721Semaste UpdateRegisterContext (); 128254721Semaste if (m_reg_ctx_sp) 129254721Semaste return m_reg_ctx_sp->ReadRegister(reg_info, reg_value); 130254721Semaste return false; 131254721Semaste} 132254721Semaste 133254721Semastebool 134254721SemasteRegisterContextThreadMemory::WriteRegister (const RegisterInfo *reg_info, const RegisterValue ®_value) 135254721Semaste{ 136254721Semaste UpdateRegisterContext (); 137254721Semaste if (m_reg_ctx_sp) 138254721Semaste return m_reg_ctx_sp->WriteRegister (reg_info, reg_value); 139254721Semaste return false; 140254721Semaste} 141254721Semaste 142254721Semastebool 143254721SemasteRegisterContextThreadMemory::ReadAllRegisterValues (lldb::DataBufferSP &data_sp) 144254721Semaste{ 145254721Semaste UpdateRegisterContext (); 146254721Semaste if (m_reg_ctx_sp) 147254721Semaste return m_reg_ctx_sp->ReadAllRegisterValues(data_sp); 148254721Semaste return false; 149254721Semaste} 150254721Semaste 151254721Semastebool 152254721SemasteRegisterContextThreadMemory::WriteAllRegisterValues (const lldb::DataBufferSP &data_sp) 153254721Semaste{ 154254721Semaste UpdateRegisterContext (); 155254721Semaste if (m_reg_ctx_sp) 156254721Semaste return m_reg_ctx_sp->WriteAllRegisterValues (data_sp); 157254721Semaste return false; 158254721Semaste} 159254721Semaste 160254721Semastebool 161254721SemasteRegisterContextThreadMemory::CopyFromRegisterContext (lldb::RegisterContextSP reg_ctx_sp) 162254721Semaste{ 163254721Semaste UpdateRegisterContext (); 164254721Semaste if (m_reg_ctx_sp) 165254721Semaste return m_reg_ctx_sp->CopyFromRegisterContext(reg_ctx_sp); 166254721Semaste return false; 167254721Semaste} 168254721Semaste 169254721Semasteuint32_t 170254721SemasteRegisterContextThreadMemory::ConvertRegisterKindToRegisterNumber (uint32_t kind, uint32_t num) 171254721Semaste{ 172254721Semaste UpdateRegisterContext (); 173254721Semaste if (m_reg_ctx_sp) 174254721Semaste return m_reg_ctx_sp->ConvertRegisterKindToRegisterNumber(kind, num); 175254721Semaste return false; 176254721Semaste} 177254721Semaste 178254721Semasteuint32_t 179254721SemasteRegisterContextThreadMemory::NumSupportedHardwareBreakpoints () 180254721Semaste{ 181254721Semaste UpdateRegisterContext (); 182254721Semaste if (m_reg_ctx_sp) 183254721Semaste return m_reg_ctx_sp->NumSupportedHardwareBreakpoints(); 184254721Semaste return false; 185254721Semaste} 186254721Semaste 187254721Semasteuint32_t 188254721SemasteRegisterContextThreadMemory::SetHardwareBreakpoint (lldb::addr_t addr, size_t size) 189254721Semaste{ 190254721Semaste UpdateRegisterContext (); 191254721Semaste if (m_reg_ctx_sp) 192254721Semaste return m_reg_ctx_sp->SetHardwareBreakpoint(addr, size); 193254721Semaste return 0; 194254721Semaste} 195254721Semaste 196254721Semastebool 197254721SemasteRegisterContextThreadMemory::ClearHardwareBreakpoint (uint32_t hw_idx) 198254721Semaste{ 199254721Semaste UpdateRegisterContext (); 200254721Semaste if (m_reg_ctx_sp) 201254721Semaste return m_reg_ctx_sp->ClearHardwareBreakpoint (hw_idx); 202254721Semaste return false; 203254721Semaste} 204254721Semaste 205254721Semasteuint32_t 206254721SemasteRegisterContextThreadMemory::NumSupportedHardwareWatchpoints () 207254721Semaste{ 208254721Semaste UpdateRegisterContext (); 209254721Semaste if (m_reg_ctx_sp) 210254721Semaste return m_reg_ctx_sp->NumSupportedHardwareWatchpoints(); 211254721Semaste return 0; 212254721Semaste} 213254721Semaste 214254721Semasteuint32_t 215254721SemasteRegisterContextThreadMemory::SetHardwareWatchpoint (lldb::addr_t addr, size_t size, bool read, bool write) 216254721Semaste{ 217254721Semaste UpdateRegisterContext (); 218254721Semaste if (m_reg_ctx_sp) 219254721Semaste return m_reg_ctx_sp->SetHardwareWatchpoint(addr, size, read, write); 220254721Semaste return 0; 221254721Semaste} 222254721Semaste 223254721Semastebool 224254721SemasteRegisterContextThreadMemory::ClearHardwareWatchpoint (uint32_t hw_index) 225254721Semaste{ 226254721Semaste UpdateRegisterContext (); 227254721Semaste if (m_reg_ctx_sp) 228254721Semaste return m_reg_ctx_sp->ClearHardwareWatchpoint(hw_index); 229254721Semaste return false; 230254721Semaste} 231254721Semaste 232254721Semastebool 233254721SemasteRegisterContextThreadMemory::HardwareSingleStep (bool enable) 234254721Semaste{ 235254721Semaste UpdateRegisterContext (); 236254721Semaste if (m_reg_ctx_sp) 237254721Semaste return m_reg_ctx_sp->HardwareSingleStep(enable); 238254721Semaste return false; 239254721Semaste} 240254721Semaste 241254721SemasteError 242254721SemasteRegisterContextThreadMemory::ReadRegisterValueFromMemory (const lldb_private::RegisterInfo *reg_info, lldb::addr_t src_addr, uint32_t src_len, RegisterValue ®_value) 243254721Semaste{ 244254721Semaste UpdateRegisterContext (); 245254721Semaste if (m_reg_ctx_sp) 246254721Semaste return m_reg_ctx_sp->ReadRegisterValueFromMemory (reg_info, src_addr, src_len, reg_value); 247254721Semaste Error error; 248254721Semaste error.SetErrorString("invalid register context"); 249254721Semaste return error; 250254721Semaste} 251254721Semaste 252254721SemasteError 253254721SemasteRegisterContextThreadMemory::WriteRegisterValueToMemory (const lldb_private::RegisterInfo *reg_info, lldb::addr_t dst_addr, uint32_t dst_len, const RegisterValue ®_value) 254254721Semaste{ 255254721Semaste UpdateRegisterContext (); 256254721Semaste if (m_reg_ctx_sp) 257254721Semaste return m_reg_ctx_sp->WriteRegisterValueToMemory (reg_info, dst_addr, dst_len, reg_value); 258254721Semaste Error error; 259254721Semaste error.SetErrorString("invalid register context"); 260254721Semaste return error; 261254721Semaste} 262