1af245d11STodd Fiala //===-- NativeProcessProtocol.cpp -------------------------------*- C++ -*-===// 2af245d11STodd Fiala // 3af245d11STodd Fiala // The LLVM Compiler Infrastructure 4af245d11STodd Fiala // 5af245d11STodd Fiala // This file is distributed under the University of Illinois Open Source 6af245d11STodd Fiala // License. See LICENSE.TXT for details. 7af245d11STodd Fiala // 8af245d11STodd Fiala //===----------------------------------------------------------------------===// 9af245d11STodd Fiala 102fe1d0abSChaoren Lin #include "lldb/Host/common/NativeProcessProtocol.h" 11511e5cdcSTodd Fiala #include "lldb/Host/Host.h" 122fe1d0abSChaoren Lin #include "lldb/Host/common/NativeRegisterContext.h" 132fe1d0abSChaoren Lin #include "lldb/Host/common/NativeThreadProtocol.h" 142fe1d0abSChaoren Lin #include "lldb/Host/common/SoftwareBreakpoint.h" 15e77fce0aSTodd Fiala #include "lldb/Utility/LLDBAssert.h" 166f9e6901SZachary Turner #include "lldb/Utility/Log.h" 17d821c997SPavel Labath #include "lldb/Utility/State.h" 18b9c1b51eSKate Stone #include "lldb/lldb-enumerations.h" 19af245d11STodd Fiala 20af245d11STodd Fiala using namespace lldb; 21af245d11STodd Fiala using namespace lldb_private; 22af245d11STodd Fiala 23af245d11STodd Fiala // ----------------------------------------------------------------------------- 24af245d11STodd Fiala // NativeProcessProtocol Members 25af245d11STodd Fiala // ----------------------------------------------------------------------------- 26af245d11STodd Fiala 2796e600fcSPavel Labath NativeProcessProtocol::NativeProcessProtocol(lldb::pid_t pid, int terminal_fd, 2896e600fcSPavel Labath NativeDelegate &delegate) 2996e600fcSPavel Labath : m_pid(pid), m_terminal_fd(terminal_fd) { 3096e600fcSPavel Labath bool registered = RegisterNativeDelegate(delegate); 3196e600fcSPavel Labath assert(registered); 3296e600fcSPavel Labath (void)registered; 3396e600fcSPavel Labath } 34af245d11STodd Fiala 3597206d57SZachary Turner lldb_private::Status NativeProcessProtocol::Interrupt() { 3697206d57SZachary Turner Status error; 37511e5cdcSTodd Fiala #if !defined(SIGSTOP) 38511e5cdcSTodd Fiala error.SetErrorString("local host does not support signaling"); 39511e5cdcSTodd Fiala return error; 40511e5cdcSTodd Fiala #else 41511e5cdcSTodd Fiala return Signal(SIGSTOP); 42511e5cdcSTodd Fiala #endif 43511e5cdcSTodd Fiala } 44511e5cdcSTodd Fiala 4597206d57SZachary Turner Status NativeProcessProtocol::IgnoreSignals(llvm::ArrayRef<int> signals) { 464a705e7eSPavel Labath m_signals_to_ignore.clear(); 474a705e7eSPavel Labath m_signals_to_ignore.insert(signals.begin(), signals.end()); 4897206d57SZachary Turner return Status(); 494a705e7eSPavel Labath } 504a705e7eSPavel Labath 5197206d57SZachary Turner lldb_private::Status 52b9c1b51eSKate Stone NativeProcessProtocol::GetMemoryRegionInfo(lldb::addr_t load_addr, 53b9c1b51eSKate Stone MemoryRegionInfo &range_info) { 54af245d11STodd Fiala // Default: not implemented. 5597206d57SZachary Turner return Status("not implemented"); 56af245d11STodd Fiala } 57af245d11STodd Fiala 583508fc8cSPavel Labath llvm::Optional<WaitStatus> NativeProcessProtocol::GetExitStatus() { 593508fc8cSPavel Labath if (m_state == lldb::eStateExited) 603508fc8cSPavel Labath return m_exit_status; 613508fc8cSPavel Labath 623508fc8cSPavel Labath return llvm::None; 63af245d11STodd Fiala } 64af245d11STodd Fiala 653508fc8cSPavel Labath bool NativeProcessProtocol::SetExitStatus(WaitStatus status, 66b9c1b51eSKate Stone bool bNotifyStateChange) { 67af245d11STodd Fiala Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 683508fc8cSPavel Labath LLDB_LOG(log, "status = {0}, notify = {1}", status, bNotifyStateChange); 69af245d11STodd Fiala 70af245d11STodd Fiala // Exit status already set 71b9c1b51eSKate Stone if (m_state == lldb::eStateExited) { 723508fc8cSPavel Labath if (m_exit_status) 733508fc8cSPavel Labath LLDB_LOG(log, "exit status already set to {0}", *m_exit_status); 743508fc8cSPavel Labath else 753508fc8cSPavel Labath LLDB_LOG(log, "state is exited, but status not set"); 76af245d11STodd Fiala return false; 77af245d11STodd Fiala } 78af245d11STodd Fiala 79af245d11STodd Fiala m_state = lldb::eStateExited; 80af245d11STodd Fiala m_exit_status = status; 81af245d11STodd Fiala 82af245d11STodd Fiala if (bNotifyStateChange) 83af245d11STodd Fiala SynchronouslyNotifyProcessStateChanged(lldb::eStateExited); 84af245d11STodd Fiala 85af245d11STodd Fiala return true; 86af245d11STodd Fiala } 87af245d11STodd Fiala 88a5be48b3SPavel Labath NativeThreadProtocol *NativeProcessProtocol::GetThreadAtIndex(uint32_t idx) { 8916ff8604SSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_threads_mutex); 90af245d11STodd Fiala if (idx < m_threads.size()) 91a5be48b3SPavel Labath return m_threads[idx].get(); 92a5be48b3SPavel Labath return nullptr; 93af245d11STodd Fiala } 94af245d11STodd Fiala 95a5be48b3SPavel Labath NativeThreadProtocol * 96b9c1b51eSKate Stone NativeProcessProtocol::GetThreadByIDUnlocked(lldb::tid_t tid) { 97a5be48b3SPavel Labath for (const auto &thread : m_threads) { 98a5be48b3SPavel Labath if (thread->GetID() == tid) 99a5be48b3SPavel Labath return thread.get(); 100af245d11STodd Fiala } 101a5be48b3SPavel Labath return nullptr; 102af245d11STodd Fiala } 103af245d11STodd Fiala 104a5be48b3SPavel Labath NativeThreadProtocol *NativeProcessProtocol::GetThreadByID(lldb::tid_t tid) { 10516ff8604SSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_threads_mutex); 106511e5cdcSTodd Fiala return GetThreadByIDUnlocked(tid); 107511e5cdcSTodd Fiala } 108511e5cdcSTodd Fiala 109b9c1b51eSKate Stone bool NativeProcessProtocol::IsAlive() const { 110b9c1b51eSKate Stone return m_state != eStateDetached && m_state != eStateExited && 111b9c1b51eSKate Stone m_state != eStateInvalid && m_state != eStateUnloaded; 112af245d11STodd Fiala } 113af245d11STodd Fiala 11418fe6404SChaoren Lin const NativeWatchpointList::WatchpointMap & 115b9c1b51eSKate Stone NativeProcessProtocol::GetWatchpointMap() const { 11618fe6404SChaoren Lin return m_watchpoint_list.GetWatchpointMap(); 11718fe6404SChaoren Lin } 11818fe6404SChaoren Lin 119d5ffbad2SOmair Javaid llvm::Optional<std::pair<uint32_t, uint32_t>> 120d5ffbad2SOmair Javaid NativeProcessProtocol::GetHardwareDebugSupportInfo() const { 121af245d11STodd Fiala Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 122af245d11STodd Fiala 123af245d11STodd Fiala // get any thread 124a5be48b3SPavel Labath NativeThreadProtocol *thread( 125b9c1b51eSKate Stone const_cast<NativeProcessProtocol *>(this)->GetThreadAtIndex(0)); 126a5be48b3SPavel Labath if (!thread) { 127a5be48b3SPavel Labath LLDB_LOG(log, "failed to find a thread to grab a NativeRegisterContext!"); 128d5ffbad2SOmair Javaid return llvm::None; 129af245d11STodd Fiala } 130af245d11STodd Fiala 131d37349f3SPavel Labath NativeRegisterContext ®_ctx = thread->GetRegisterContext(); 132d37349f3SPavel Labath return std::make_pair(reg_ctx.NumSupportedHardwareBreakpoints(), 133d37349f3SPavel Labath reg_ctx.NumSupportedHardwareWatchpoints()); 134af245d11STodd Fiala } 135af245d11STodd Fiala 13697206d57SZachary Turner Status NativeProcessProtocol::SetWatchpoint(lldb::addr_t addr, size_t size, 137b9c1b51eSKate Stone uint32_t watch_flags, 138b9c1b51eSKate Stone bool hardware) { 13905097246SAdrian Prantl // This default implementation assumes setting the watchpoint for the process 14005097246SAdrian Prantl // will require setting the watchpoint for each of the threads. Furthermore, 14105097246SAdrian Prantl // it will track watchpoints set for the process and will add them to each 14205097246SAdrian Prantl // thread that is attached to via the (FIXME implement) OnThreadAttached () 14305097246SAdrian Prantl // method. 144af245d11STodd Fiala 145af245d11STodd Fiala Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 146af245d11STodd Fiala 147af245d11STodd Fiala // Update the thread list 148af245d11STodd Fiala UpdateThreads(); 149af245d11STodd Fiala 15005097246SAdrian Prantl // Keep track of the threads we successfully set the watchpoint for. If one 15105097246SAdrian Prantl // of the thread watchpoint setting operations fails, back off and remove the 15205097246SAdrian Prantl // watchpoint for all the threads that were successfully set so we get back 15305097246SAdrian Prantl // to a consistent state. 154a5be48b3SPavel Labath std::vector<NativeThreadProtocol *> watchpoint_established_threads; 155af245d11STodd Fiala 15605097246SAdrian Prantl // Tell each thread to set a watchpoint. In the event that hardware 15705097246SAdrian Prantl // watchpoints are requested but the SetWatchpoint fails, try to set a 15805097246SAdrian Prantl // software watchpoint as a fallback. It's conceivable that if there are 15905097246SAdrian Prantl // more threads than hardware watchpoints available, some of the threads will 16005097246SAdrian Prantl // fail to set hardware watchpoints while software ones may be available. 16116ff8604SSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_threads_mutex); 162a5be48b3SPavel Labath for (const auto &thread : m_threads) { 163a5be48b3SPavel Labath assert(thread && "thread list should not have a NULL thread!"); 164af245d11STodd Fiala 16597206d57SZachary Turner Status thread_error = 166a5be48b3SPavel Labath thread->SetWatchpoint(addr, size, watch_flags, hardware); 167b9c1b51eSKate Stone if (thread_error.Fail() && hardware) { 16805097246SAdrian Prantl // Try software watchpoints since we failed on hardware watchpoint 16905097246SAdrian Prantl // setting and we may have just run out of hardware watchpoints. 170a5be48b3SPavel Labath thread_error = thread->SetWatchpoint(addr, size, watch_flags, false); 171a5be48b3SPavel Labath if (thread_error.Success()) 172a5be48b3SPavel Labath LLDB_LOG(log, 173b9c1b51eSKate Stone "hardware watchpoint requested but software watchpoint set"); 174af245d11STodd Fiala } 175af245d11STodd Fiala 176b9c1b51eSKate Stone if (thread_error.Success()) { 17705097246SAdrian Prantl // Remember that we set this watchpoint successfully in case we need to 17805097246SAdrian Prantl // clear it later. 179a5be48b3SPavel Labath watchpoint_established_threads.push_back(thread.get()); 180b9c1b51eSKate Stone } else { 18105097246SAdrian Prantl // Unset the watchpoint for each thread we successfully set so that we 18205097246SAdrian Prantl // get back to a consistent state of "not set" for the watchpoint. 183b9c1b51eSKate Stone for (auto unwatch_thread_sp : watchpoint_established_threads) { 18497206d57SZachary Turner Status remove_error = unwatch_thread_sp->RemoveWatchpoint(addr); 185a5be48b3SPavel Labath if (remove_error.Fail()) 186a5be48b3SPavel Labath LLDB_LOG(log, "RemoveWatchpoint failed for pid={0}, tid={1}: {2}", 187a5be48b3SPavel Labath GetID(), unwatch_thread_sp->GetID(), remove_error); 188af245d11STodd Fiala } 189af245d11STodd Fiala 190af245d11STodd Fiala return thread_error; 191af245d11STodd Fiala } 192af245d11STodd Fiala } 19318fe6404SChaoren Lin return m_watchpoint_list.Add(addr, size, watch_flags, hardware); 194af245d11STodd Fiala } 195af245d11STodd Fiala 19697206d57SZachary Turner Status NativeProcessProtocol::RemoveWatchpoint(lldb::addr_t addr) { 197af245d11STodd Fiala // Update the thread list 198af245d11STodd Fiala UpdateThreads(); 199af245d11STodd Fiala 20097206d57SZachary Turner Status overall_error; 201af245d11STodd Fiala 20216ff8604SSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_threads_mutex); 203a5be48b3SPavel Labath for (const auto &thread : m_threads) { 204a5be48b3SPavel Labath assert(thread && "thread list should not have a NULL thread!"); 205af245d11STodd Fiala 206a5be48b3SPavel Labath const Status thread_error = thread->RemoveWatchpoint(addr); 207b9c1b51eSKate Stone if (thread_error.Fail()) { 20805097246SAdrian Prantl // Keep track of the first thread error if any threads fail. We want to 20905097246SAdrian Prantl // try to remove the watchpoint from every thread, though, even if one or 21005097246SAdrian Prantl // more have errors. 211af245d11STodd Fiala if (!overall_error.Fail()) 212af245d11STodd Fiala overall_error = thread_error; 213af245d11STodd Fiala } 214af245d11STodd Fiala } 21597206d57SZachary Turner const Status error = m_watchpoint_list.Remove(addr); 21618fe6404SChaoren Lin return overall_error.Fail() ? overall_error : error; 217af245d11STodd Fiala } 218af245d11STodd Fiala 219d5ffbad2SOmair Javaid const HardwareBreakpointMap & 220d5ffbad2SOmair Javaid NativeProcessProtocol::GetHardwareBreakpointMap() const { 221d5ffbad2SOmair Javaid return m_hw_breakpoints_map; 222d5ffbad2SOmair Javaid } 223d5ffbad2SOmair Javaid 22497206d57SZachary Turner Status NativeProcessProtocol::SetHardwareBreakpoint(lldb::addr_t addr, 225d5ffbad2SOmair Javaid size_t size) { 22605097246SAdrian Prantl // This default implementation assumes setting a hardware breakpoint for this 22705097246SAdrian Prantl // process will require setting same hardware breakpoint for each of its 22805097246SAdrian Prantl // existing threads. New thread will do the same once created. 229d5ffbad2SOmair Javaid Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 230d5ffbad2SOmair Javaid 231d5ffbad2SOmair Javaid // Update the thread list 232d5ffbad2SOmair Javaid UpdateThreads(); 233d5ffbad2SOmair Javaid 234d5ffbad2SOmair Javaid // Exit here if target does not have required hardware breakpoint capability. 235d5ffbad2SOmair Javaid auto hw_debug_cap = GetHardwareDebugSupportInfo(); 236d5ffbad2SOmair Javaid 237d5ffbad2SOmair Javaid if (hw_debug_cap == llvm::None || hw_debug_cap->first == 0 || 238d5ffbad2SOmair Javaid hw_debug_cap->first <= m_hw_breakpoints_map.size()) 23997206d57SZachary Turner return Status("Target does not have required no of hardware breakpoints"); 240d5ffbad2SOmair Javaid 241d5ffbad2SOmair Javaid // Vector below stores all thread pointer for which we have we successfully 242d5ffbad2SOmair Javaid // set this hardware breakpoint. If any of the current process threads fails 243d5ffbad2SOmair Javaid // to set this hardware breakpoint then roll back and remove this breakpoint 244d5ffbad2SOmair Javaid // for all the threads that had already set it successfully. 245a5be48b3SPavel Labath std::vector<NativeThreadProtocol *> breakpoint_established_threads; 246d5ffbad2SOmair Javaid 247d5ffbad2SOmair Javaid // Request to set a hardware breakpoint for each of current process threads. 248d5ffbad2SOmair Javaid std::lock_guard<std::recursive_mutex> guard(m_threads_mutex); 249a5be48b3SPavel Labath for (const auto &thread : m_threads) { 250a5be48b3SPavel Labath assert(thread && "thread list should not have a NULL thread!"); 251d5ffbad2SOmair Javaid 252a5be48b3SPavel Labath Status thread_error = thread->SetHardwareBreakpoint(addr, size); 253d5ffbad2SOmair Javaid if (thread_error.Success()) { 25405097246SAdrian Prantl // Remember that we set this breakpoint successfully in case we need to 25505097246SAdrian Prantl // clear it later. 256a5be48b3SPavel Labath breakpoint_established_threads.push_back(thread.get()); 257d5ffbad2SOmair Javaid } else { 25805097246SAdrian Prantl // Unset the breakpoint for each thread we successfully set so that we 25905097246SAdrian Prantl // get back to a consistent state of "not set" for this hardware 26005097246SAdrian Prantl // breakpoint. 261d5ffbad2SOmair Javaid for (auto rollback_thread_sp : breakpoint_established_threads) { 26297206d57SZachary Turner Status remove_error = 26397206d57SZachary Turner rollback_thread_sp->RemoveHardwareBreakpoint(addr); 264a5be48b3SPavel Labath if (remove_error.Fail()) 265a5be48b3SPavel Labath LLDB_LOG(log, 266a5be48b3SPavel Labath "RemoveHardwareBreakpoint failed for pid={0}, tid={1}: {2}", 267a5be48b3SPavel Labath GetID(), rollback_thread_sp->GetID(), remove_error); 268d5ffbad2SOmair Javaid } 269d5ffbad2SOmair Javaid 270d5ffbad2SOmair Javaid return thread_error; 271d5ffbad2SOmair Javaid } 272d5ffbad2SOmair Javaid } 273d5ffbad2SOmair Javaid 274d5ffbad2SOmair Javaid // Register new hardware breakpoint into hardware breakpoints map of current 275d5ffbad2SOmair Javaid // process. 276d5ffbad2SOmair Javaid m_hw_breakpoints_map[addr] = {addr, size}; 277d5ffbad2SOmair Javaid 27897206d57SZachary Turner return Status(); 279d5ffbad2SOmair Javaid } 280d5ffbad2SOmair Javaid 28197206d57SZachary Turner Status NativeProcessProtocol::RemoveHardwareBreakpoint(lldb::addr_t addr) { 282d5ffbad2SOmair Javaid // Update the thread list 283d5ffbad2SOmair Javaid UpdateThreads(); 284d5ffbad2SOmair Javaid 28597206d57SZachary Turner Status error; 286d5ffbad2SOmair Javaid 287d5ffbad2SOmair Javaid std::lock_guard<std::recursive_mutex> guard(m_threads_mutex); 288a5be48b3SPavel Labath for (const auto &thread : m_threads) { 289a5be48b3SPavel Labath assert(thread && "thread list should not have a NULL thread!"); 290a5be48b3SPavel Labath error = thread->RemoveHardwareBreakpoint(addr); 291d5ffbad2SOmair Javaid } 292d5ffbad2SOmair Javaid 293d5ffbad2SOmair Javaid // Also remove from hardware breakpoint map of current process. 294d5ffbad2SOmair Javaid m_hw_breakpoints_map.erase(addr); 295d5ffbad2SOmair Javaid 296d5ffbad2SOmair Javaid return error; 297d5ffbad2SOmair Javaid } 298d5ffbad2SOmair Javaid 299b9c1b51eSKate Stone bool NativeProcessProtocol::RegisterNativeDelegate( 300b9c1b51eSKate Stone NativeDelegate &native_delegate) { 30116ff8604SSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_delegates_mutex); 302b9c1b51eSKate Stone if (std::find(m_delegates.begin(), m_delegates.end(), &native_delegate) != 303b9c1b51eSKate Stone m_delegates.end()) 304af245d11STodd Fiala return false; 305af245d11STodd Fiala 306af245d11STodd Fiala m_delegates.push_back(&native_delegate); 307af245d11STodd Fiala native_delegate.InitializeDelegate(this); 308af245d11STodd Fiala return true; 309af245d11STodd Fiala } 310af245d11STodd Fiala 311b9c1b51eSKate Stone bool NativeProcessProtocol::UnregisterNativeDelegate( 312b9c1b51eSKate Stone NativeDelegate &native_delegate) { 31316ff8604SSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_delegates_mutex); 314af245d11STodd Fiala 315af245d11STodd Fiala const auto initial_size = m_delegates.size(); 316b9c1b51eSKate Stone m_delegates.erase( 317b9c1b51eSKate Stone remove(m_delegates.begin(), m_delegates.end(), &native_delegate), 318b9c1b51eSKate Stone m_delegates.end()); 319af245d11STodd Fiala 32005097246SAdrian Prantl // We removed the delegate if the count of delegates shrank after removing 32105097246SAdrian Prantl // all copies of the given native_delegate from the vector. 322af245d11STodd Fiala return m_delegates.size() < initial_size; 323af245d11STodd Fiala } 324af245d11STodd Fiala 325b9c1b51eSKate Stone void NativeProcessProtocol::SynchronouslyNotifyProcessStateChanged( 326b9c1b51eSKate Stone lldb::StateType state) { 327af245d11STodd Fiala Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 328af245d11STodd Fiala 32916ff8604SSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_delegates_mutex); 330af245d11STodd Fiala for (auto native_delegate : m_delegates) 331af245d11STodd Fiala native_delegate->ProcessStateChanged(this, state); 332af245d11STodd Fiala 333b9c1b51eSKate Stone if (log) { 334b9c1b51eSKate Stone if (!m_delegates.empty()) { 335b9c1b51eSKate Stone log->Printf("NativeProcessProtocol::%s: sent state notification [%s] " 336b9c1b51eSKate Stone "from process %" PRIu64, 337af245d11STodd Fiala __FUNCTION__, lldb_private::StateAsCString(state), GetID()); 338b9c1b51eSKate Stone } else { 339b9c1b51eSKate Stone log->Printf("NativeProcessProtocol::%s: would send state notification " 340b9c1b51eSKate Stone "[%s] from process %" PRIu64 ", but no delegates", 341af245d11STodd Fiala __FUNCTION__, lldb_private::StateAsCString(state), GetID()); 342af245d11STodd Fiala } 343af245d11STodd Fiala } 344af245d11STodd Fiala } 345af245d11STodd Fiala 346b9c1b51eSKate Stone void NativeProcessProtocol::NotifyDidExec() { 347a9882ceeSTodd Fiala Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 348a9882ceeSTodd Fiala if (log) 349b9c1b51eSKate Stone log->Printf("NativeProcessProtocol::%s - preparing to call delegates", 350b9c1b51eSKate Stone __FUNCTION__); 351a9882ceeSTodd Fiala 352a9882ceeSTodd Fiala { 35316ff8604SSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_delegates_mutex); 354a9882ceeSTodd Fiala for (auto native_delegate : m_delegates) 355a9882ceeSTodd Fiala native_delegate->DidExec(this); 356a9882ceeSTodd Fiala } 357a9882ceeSTodd Fiala } 358a9882ceeSTodd Fiala 35997206d57SZachary Turner Status NativeProcessProtocol::SetSoftwareBreakpoint(lldb::addr_t addr, 360b9c1b51eSKate Stone uint32_t size_hint) { 361af245d11STodd Fiala Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_BREAKPOINTS)); 362af245d11STodd Fiala if (log) 363b9c1b51eSKate Stone log->Printf("NativeProcessProtocol::%s addr = 0x%" PRIx64, __FUNCTION__, 364b9c1b51eSKate Stone addr); 365af245d11STodd Fiala 366b9c1b51eSKate Stone return m_breakpoint_list.AddRef( 367b9c1b51eSKate Stone addr, size_hint, false, 368b9c1b51eSKate Stone [this](lldb::addr_t addr, size_t size_hint, bool /* hardware */, 36997206d57SZachary Turner NativeBreakpointSP &breakpoint_sp) -> Status { 370b9c1b51eSKate Stone return SoftwareBreakpoint::CreateSoftwareBreakpoint( 371b9c1b51eSKate Stone *this, addr, size_hint, breakpoint_sp); 372b9c1b51eSKate Stone }); 373af245d11STodd Fiala } 374af245d11STodd Fiala 375f8b825f6SPavel Labath llvm::Expected<llvm::ArrayRef<uint8_t>> 376f8b825f6SPavel Labath NativeProcessProtocol::GetSoftwareBreakpointTrapOpcode(size_t size_hint) { 377f8b825f6SPavel Labath static const uint8_t g_aarch64_opcode[] = {0x00, 0x00, 0x20, 0xd4}; 378f8b825f6SPavel Labath static const uint8_t g_i386_opcode[] = {0xCC}; 379f8b825f6SPavel Labath static const uint8_t g_mips64_opcode[] = {0x00, 0x00, 0x00, 0x0d}; 380f8b825f6SPavel Labath static const uint8_t g_mips64el_opcode[] = {0x0d, 0x00, 0x00, 0x00}; 381f8b825f6SPavel Labath static const uint8_t g_s390x_opcode[] = {0x00, 0x01}; 382f8b825f6SPavel Labath static const uint8_t g_ppc64le_opcode[] = {0x08, 0x00, 0xe0, 0x7f}; // trap 383f8b825f6SPavel Labath 384f8b825f6SPavel Labath switch (GetArchitecture().GetMachine()) { 385f8b825f6SPavel Labath case llvm::Triple::aarch64: 3864f545074SPavel Labath return llvm::makeArrayRef(g_aarch64_opcode); 387f8b825f6SPavel Labath 388f8b825f6SPavel Labath case llvm::Triple::x86: 389f8b825f6SPavel Labath case llvm::Triple::x86_64: 3904f545074SPavel Labath return llvm::makeArrayRef(g_i386_opcode); 391f8b825f6SPavel Labath 392f8b825f6SPavel Labath case llvm::Triple::mips: 393f8b825f6SPavel Labath case llvm::Triple::mips64: 3944f545074SPavel Labath return llvm::makeArrayRef(g_mips64_opcode); 395f8b825f6SPavel Labath 396f8b825f6SPavel Labath case llvm::Triple::mipsel: 397f8b825f6SPavel Labath case llvm::Triple::mips64el: 3984f545074SPavel Labath return llvm::makeArrayRef(g_mips64el_opcode); 399f8b825f6SPavel Labath 400f8b825f6SPavel Labath case llvm::Triple::systemz: 4014f545074SPavel Labath return llvm::makeArrayRef(g_s390x_opcode); 402f8b825f6SPavel Labath 403f8b825f6SPavel Labath case llvm::Triple::ppc64le: 4044f545074SPavel Labath return llvm::makeArrayRef(g_ppc64le_opcode); 405f8b825f6SPavel Labath 406f8b825f6SPavel Labath default: 407f8b825f6SPavel Labath return llvm::createStringError(llvm::inconvertibleErrorCode(), 408f8b825f6SPavel Labath "CPU type not supported!"); 409f8b825f6SPavel Labath } 410f8b825f6SPavel Labath } 411f8b825f6SPavel Labath 41299f436b0SPavel Labath size_t NativeProcessProtocol::GetSoftwareBreakpointPCOffset() { 41399f436b0SPavel Labath switch (GetArchitecture().GetMachine()) { 41499f436b0SPavel Labath case llvm::Triple::x86: 41599f436b0SPavel Labath case llvm::Triple::x86_64: 41699f436b0SPavel Labath case llvm::Triple::systemz: 41799f436b0SPavel Labath // These architectures report increment the PC after breakpoint is hit. 41899f436b0SPavel Labath return cantFail(GetSoftwareBreakpointTrapOpcode(0)).size(); 41999f436b0SPavel Labath 42099f436b0SPavel Labath case llvm::Triple::arm: 42199f436b0SPavel Labath case llvm::Triple::aarch64: 42299f436b0SPavel Labath case llvm::Triple::mips64: 42399f436b0SPavel Labath case llvm::Triple::mips64el: 42499f436b0SPavel Labath case llvm::Triple::mips: 42599f436b0SPavel Labath case llvm::Triple::mipsel: 42699f436b0SPavel Labath case llvm::Triple::ppc64le: 42799f436b0SPavel Labath // On these architectures the PC doesn't get updated for breakpoint hits. 42899f436b0SPavel Labath return 0; 42999f436b0SPavel Labath 43099f436b0SPavel Labath default: 43199f436b0SPavel Labath llvm_unreachable("CPU type not supported!"); 43299f436b0SPavel Labath } 43399f436b0SPavel Labath } 43499f436b0SPavel Labath 435*aef7908fSPavel Labath void NativeProcessProtocol::FixupBreakpointPCAsNeeded( 436*aef7908fSPavel Labath NativeThreadProtocol &thread) { 437*aef7908fSPavel Labath Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_BREAKPOINTS); 438*aef7908fSPavel Labath 439*aef7908fSPavel Labath Status error; 440*aef7908fSPavel Labath 441*aef7908fSPavel Labath // Find out the size of a breakpoint (might depend on where we are in the 442*aef7908fSPavel Labath // code). 443*aef7908fSPavel Labath NativeRegisterContext &context = thread.GetRegisterContext(); 444*aef7908fSPavel Labath 445*aef7908fSPavel Labath uint32_t breakpoint_size = GetSoftwareBreakpointPCOffset(); 446*aef7908fSPavel Labath LLDB_LOG(log, "breakpoint size: {0}", breakpoint_size); 447*aef7908fSPavel Labath if (breakpoint_size == 0) 448*aef7908fSPavel Labath return; 449*aef7908fSPavel Labath 450*aef7908fSPavel Labath // First try probing for a breakpoint at a software breakpoint location: PC - 451*aef7908fSPavel Labath // breakpoint size. 452*aef7908fSPavel Labath const lldb::addr_t initial_pc_addr = context.GetPCfromBreakpointLocation(); 453*aef7908fSPavel Labath lldb::addr_t breakpoint_addr = initial_pc_addr; 454*aef7908fSPavel Labath // Do not allow breakpoint probe to wrap around. 455*aef7908fSPavel Labath if (breakpoint_addr >= breakpoint_size) 456*aef7908fSPavel Labath breakpoint_addr -= breakpoint_size; 457*aef7908fSPavel Labath 458*aef7908fSPavel Labath // Check if we stopped because of a breakpoint. 459*aef7908fSPavel Labath NativeBreakpointSP breakpoint_sp; 460*aef7908fSPavel Labath error = m_breakpoint_list.GetBreakpoint(breakpoint_addr, breakpoint_sp); 461*aef7908fSPavel Labath if (!error.Success() || !breakpoint_sp) { 462*aef7908fSPavel Labath // We didn't find one at a software probe location. Nothing to do. 463*aef7908fSPavel Labath LLDB_LOG(log, 464*aef7908fSPavel Labath "pid {0} no lldb breakpoint found at current pc with " 465*aef7908fSPavel Labath "adjustment: {1}", 466*aef7908fSPavel Labath GetID(), breakpoint_addr); 467*aef7908fSPavel Labath return; 468*aef7908fSPavel Labath } 469*aef7908fSPavel Labath 470*aef7908fSPavel Labath // If the breakpoint is not a software breakpoint, nothing to do. 471*aef7908fSPavel Labath if (!breakpoint_sp->IsSoftwareBreakpoint()) { 472*aef7908fSPavel Labath LLDB_LOG( 473*aef7908fSPavel Labath log, 474*aef7908fSPavel Labath "pid {0} breakpoint found at {1:x}, not software, nothing to adjust", 475*aef7908fSPavel Labath GetID(), breakpoint_addr); 476*aef7908fSPavel Labath return; 477*aef7908fSPavel Labath } 478*aef7908fSPavel Labath 479*aef7908fSPavel Labath // 480*aef7908fSPavel Labath // We have a software breakpoint and need to adjust the PC. 481*aef7908fSPavel Labath // 482*aef7908fSPavel Labath 483*aef7908fSPavel Labath // Change the program counter. 484*aef7908fSPavel Labath LLDB_LOG(log, "pid {0} tid {1}: changing PC from {2:x} to {3:x}", GetID(), 485*aef7908fSPavel Labath thread.GetID(), initial_pc_addr, breakpoint_addr); 486*aef7908fSPavel Labath 487*aef7908fSPavel Labath error = context.SetPC(breakpoint_addr); 488*aef7908fSPavel Labath if (error.Fail()) { 489*aef7908fSPavel Labath // This can happen in case the process was killed between the time we read 490*aef7908fSPavel Labath // the PC and when we are updating it. There's nothing better to do than to 491*aef7908fSPavel Labath // swallow the error. 492*aef7908fSPavel Labath LLDB_LOG(log, "pid {0} tid {1}: failed to set PC: {2}", GetID(), 493*aef7908fSPavel Labath thread.GetID(), error); 494*aef7908fSPavel Labath } 495*aef7908fSPavel Labath } 496*aef7908fSPavel Labath 49797206d57SZachary Turner Status NativeProcessProtocol::RemoveBreakpoint(lldb::addr_t addr, 498d5ffbad2SOmair Javaid bool hardware) { 499d5ffbad2SOmair Javaid if (hardware) 500d5ffbad2SOmair Javaid return RemoveHardwareBreakpoint(addr); 501d5ffbad2SOmair Javaid else 502af245d11STodd Fiala return m_breakpoint_list.DecRef(addr); 503af245d11STodd Fiala } 504af245d11STodd Fiala 50597206d57SZachary Turner Status NativeProcessProtocol::EnableBreakpoint(lldb::addr_t addr) { 506af245d11STodd Fiala return m_breakpoint_list.EnableBreakpoint(addr); 507af245d11STodd Fiala } 508af245d11STodd Fiala 50997206d57SZachary Turner Status NativeProcessProtocol::DisableBreakpoint(lldb::addr_t addr) { 510af245d11STodd Fiala return m_breakpoint_list.DisableBreakpoint(addr); 511af245d11STodd Fiala } 512af245d11STodd Fiala 513b9c1b51eSKate Stone lldb::StateType NativeProcessProtocol::GetState() const { 51416ff8604SSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_state_mutex); 515af245d11STodd Fiala return m_state; 516af245d11STodd Fiala } 517af245d11STodd Fiala 5182ce26527SPavel Labath Status NativeProcessProtocol::ReadMemoryWithoutTrap(lldb::addr_t addr, 5192ce26527SPavel Labath void *buf, size_t size, 5202ce26527SPavel Labath size_t &bytes_read) { 5212ce26527SPavel Labath Status error = ReadMemory(addr, buf, size, bytes_read); 5222ce26527SPavel Labath if (error.Fail()) 5232ce26527SPavel Labath return error; 5242ce26527SPavel Labath return m_breakpoint_list.RemoveTrapsFromBuffer(addr, buf, size); 5252ce26527SPavel Labath } 5262ce26527SPavel Labath 527b9c1b51eSKate Stone void NativeProcessProtocol::SetState(lldb::StateType state, 528b9c1b51eSKate Stone bool notify_delegates) { 52916ff8604SSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_state_mutex); 5305830aa75STamas Berghammer 5315830aa75STamas Berghammer if (state == m_state) 5325830aa75STamas Berghammer return; 5335830aa75STamas Berghammer 534af245d11STodd Fiala m_state = state; 535af245d11STodd Fiala 536b9c1b51eSKate Stone if (StateIsStoppedState(state, false)) { 537af245d11STodd Fiala ++m_stop_id; 538af245d11STodd Fiala 539af245d11STodd Fiala // Give process a chance to do any stop id bump processing, such as 540af245d11STodd Fiala // clearing cached data that is invalidated each time the process runs. 54105097246SAdrian Prantl // Note if/when we support some threads running, we'll end up needing to 54205097246SAdrian Prantl // manage this per thread and per process. 543af245d11STodd Fiala DoStopIDBumped(m_stop_id); 544af245d11STodd Fiala } 545af245d11STodd Fiala 546af245d11STodd Fiala // Optionally notify delegates of the state change. 547af245d11STodd Fiala if (notify_delegates) 548af245d11STodd Fiala SynchronouslyNotifyProcessStateChanged(state); 549af245d11STodd Fiala } 550af245d11STodd Fiala 551b9c1b51eSKate Stone uint32_t NativeProcessProtocol::GetStopID() const { 55216ff8604SSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_state_mutex); 553af245d11STodd Fiala return m_stop_id; 554af245d11STodd Fiala } 555af245d11STodd Fiala 556b9c1b51eSKate Stone void NativeProcessProtocol::DoStopIDBumped(uint32_t /* newBumpId */) { 557af245d11STodd Fiala // Default implementation does nothing. 558af245d11STodd Fiala } 5598bc34f4dSOleksiy Vyalov 56096e600fcSPavel Labath NativeProcessProtocol::Factory::~Factory() = default; 561