10b57cec5SDimitry Andric //===-- NativeProcessProtocol.cpp -------------------------------*- C++ -*-===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric 90b57cec5SDimitry Andric #include "lldb/Host/common/NativeProcessProtocol.h" 100b57cec5SDimitry Andric #include "lldb/Host/Host.h" 110b57cec5SDimitry Andric #include "lldb/Host/common/NativeBreakpointList.h" 120b57cec5SDimitry Andric #include "lldb/Host/common/NativeRegisterContext.h" 130b57cec5SDimitry Andric #include "lldb/Host/common/NativeThreadProtocol.h" 140b57cec5SDimitry Andric #include "lldb/Utility/LLDBAssert.h" 150b57cec5SDimitry Andric #include "lldb/Utility/Log.h" 160b57cec5SDimitry Andric #include "lldb/Utility/State.h" 170b57cec5SDimitry Andric #include "lldb/lldb-enumerations.h" 180b57cec5SDimitry Andric 199dba64beSDimitry Andric #include "llvm/Support/Process.h" 209dba64beSDimitry Andric 210b57cec5SDimitry Andric using namespace lldb; 220b57cec5SDimitry Andric using namespace lldb_private; 230b57cec5SDimitry Andric 240b57cec5SDimitry Andric // NativeProcessProtocol Members 250b57cec5SDimitry Andric 260b57cec5SDimitry Andric NativeProcessProtocol::NativeProcessProtocol(lldb::pid_t pid, int terminal_fd, 270b57cec5SDimitry Andric NativeDelegate &delegate) 280b57cec5SDimitry Andric : m_pid(pid), m_terminal_fd(terminal_fd) { 290b57cec5SDimitry Andric bool registered = RegisterNativeDelegate(delegate); 300b57cec5SDimitry Andric assert(registered); 310b57cec5SDimitry Andric (void)registered; 320b57cec5SDimitry Andric } 330b57cec5SDimitry Andric 340b57cec5SDimitry Andric lldb_private::Status NativeProcessProtocol::Interrupt() { 350b57cec5SDimitry Andric Status error; 360b57cec5SDimitry Andric #if !defined(SIGSTOP) 370b57cec5SDimitry Andric error.SetErrorString("local host does not support signaling"); 380b57cec5SDimitry Andric return error; 390b57cec5SDimitry Andric #else 400b57cec5SDimitry Andric return Signal(SIGSTOP); 410b57cec5SDimitry Andric #endif 420b57cec5SDimitry Andric } 430b57cec5SDimitry Andric 440b57cec5SDimitry Andric Status NativeProcessProtocol::IgnoreSignals(llvm::ArrayRef<int> signals) { 450b57cec5SDimitry Andric m_signals_to_ignore.clear(); 460b57cec5SDimitry Andric m_signals_to_ignore.insert(signals.begin(), signals.end()); 470b57cec5SDimitry Andric return Status(); 480b57cec5SDimitry Andric } 490b57cec5SDimitry Andric 500b57cec5SDimitry Andric lldb_private::Status 510b57cec5SDimitry Andric NativeProcessProtocol::GetMemoryRegionInfo(lldb::addr_t load_addr, 520b57cec5SDimitry Andric MemoryRegionInfo &range_info) { 530b57cec5SDimitry Andric // Default: not implemented. 540b57cec5SDimitry Andric return Status("not implemented"); 550b57cec5SDimitry Andric } 560b57cec5SDimitry Andric 570b57cec5SDimitry Andric llvm::Optional<WaitStatus> NativeProcessProtocol::GetExitStatus() { 580b57cec5SDimitry Andric if (m_state == lldb::eStateExited) 590b57cec5SDimitry Andric return m_exit_status; 600b57cec5SDimitry Andric 610b57cec5SDimitry Andric return llvm::None; 620b57cec5SDimitry Andric } 630b57cec5SDimitry Andric 640b57cec5SDimitry Andric bool NativeProcessProtocol::SetExitStatus(WaitStatus status, 650b57cec5SDimitry Andric bool bNotifyStateChange) { 660b57cec5SDimitry Andric Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 670b57cec5SDimitry Andric LLDB_LOG(log, "status = {0}, notify = {1}", status, bNotifyStateChange); 680b57cec5SDimitry Andric 690b57cec5SDimitry Andric // Exit status already set 700b57cec5SDimitry Andric if (m_state == lldb::eStateExited) { 710b57cec5SDimitry Andric if (m_exit_status) 720b57cec5SDimitry Andric LLDB_LOG(log, "exit status already set to {0}", *m_exit_status); 730b57cec5SDimitry Andric else 740b57cec5SDimitry Andric LLDB_LOG(log, "state is exited, but status not set"); 750b57cec5SDimitry Andric return false; 760b57cec5SDimitry Andric } 770b57cec5SDimitry Andric 780b57cec5SDimitry Andric m_state = lldb::eStateExited; 790b57cec5SDimitry Andric m_exit_status = status; 800b57cec5SDimitry Andric 810b57cec5SDimitry Andric if (bNotifyStateChange) 820b57cec5SDimitry Andric SynchronouslyNotifyProcessStateChanged(lldb::eStateExited); 830b57cec5SDimitry Andric 840b57cec5SDimitry Andric return true; 850b57cec5SDimitry Andric } 860b57cec5SDimitry Andric 870b57cec5SDimitry Andric NativeThreadProtocol *NativeProcessProtocol::GetThreadAtIndex(uint32_t idx) { 880b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_threads_mutex); 890b57cec5SDimitry Andric if (idx < m_threads.size()) 900b57cec5SDimitry Andric return m_threads[idx].get(); 910b57cec5SDimitry Andric return nullptr; 920b57cec5SDimitry Andric } 930b57cec5SDimitry Andric 940b57cec5SDimitry Andric NativeThreadProtocol * 950b57cec5SDimitry Andric NativeProcessProtocol::GetThreadByIDUnlocked(lldb::tid_t tid) { 960b57cec5SDimitry Andric for (const auto &thread : m_threads) { 970b57cec5SDimitry Andric if (thread->GetID() == tid) 980b57cec5SDimitry Andric return thread.get(); 990b57cec5SDimitry Andric } 1000b57cec5SDimitry Andric return nullptr; 1010b57cec5SDimitry Andric } 1020b57cec5SDimitry Andric 1030b57cec5SDimitry Andric NativeThreadProtocol *NativeProcessProtocol::GetThreadByID(lldb::tid_t tid) { 1040b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_threads_mutex); 1050b57cec5SDimitry Andric return GetThreadByIDUnlocked(tid); 1060b57cec5SDimitry Andric } 1070b57cec5SDimitry Andric 1080b57cec5SDimitry Andric bool NativeProcessProtocol::IsAlive() const { 1090b57cec5SDimitry Andric return m_state != eStateDetached && m_state != eStateExited && 1100b57cec5SDimitry Andric m_state != eStateInvalid && m_state != eStateUnloaded; 1110b57cec5SDimitry Andric } 1120b57cec5SDimitry Andric 1130b57cec5SDimitry Andric const NativeWatchpointList::WatchpointMap & 1140b57cec5SDimitry Andric NativeProcessProtocol::GetWatchpointMap() const { 1150b57cec5SDimitry Andric return m_watchpoint_list.GetWatchpointMap(); 1160b57cec5SDimitry Andric } 1170b57cec5SDimitry Andric 1180b57cec5SDimitry Andric llvm::Optional<std::pair<uint32_t, uint32_t>> 1190b57cec5SDimitry Andric NativeProcessProtocol::GetHardwareDebugSupportInfo() const { 1200b57cec5SDimitry Andric Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 1210b57cec5SDimitry Andric 1220b57cec5SDimitry Andric // get any thread 1230b57cec5SDimitry Andric NativeThreadProtocol *thread( 1240b57cec5SDimitry Andric const_cast<NativeProcessProtocol *>(this)->GetThreadAtIndex(0)); 1250b57cec5SDimitry Andric if (!thread) { 1260b57cec5SDimitry Andric LLDB_LOG(log, "failed to find a thread to grab a NativeRegisterContext!"); 1270b57cec5SDimitry Andric return llvm::None; 1280b57cec5SDimitry Andric } 1290b57cec5SDimitry Andric 1300b57cec5SDimitry Andric NativeRegisterContext ®_ctx = thread->GetRegisterContext(); 1310b57cec5SDimitry Andric return std::make_pair(reg_ctx.NumSupportedHardwareBreakpoints(), 1320b57cec5SDimitry Andric reg_ctx.NumSupportedHardwareWatchpoints()); 1330b57cec5SDimitry Andric } 1340b57cec5SDimitry Andric 1350b57cec5SDimitry Andric Status NativeProcessProtocol::SetWatchpoint(lldb::addr_t addr, size_t size, 1360b57cec5SDimitry Andric uint32_t watch_flags, 1370b57cec5SDimitry Andric bool hardware) { 1380b57cec5SDimitry Andric // This default implementation assumes setting the watchpoint for the process 1390b57cec5SDimitry Andric // will require setting the watchpoint for each of the threads. Furthermore, 1400b57cec5SDimitry Andric // it will track watchpoints set for the process and will add them to each 1410b57cec5SDimitry Andric // thread that is attached to via the (FIXME implement) OnThreadAttached () 1420b57cec5SDimitry Andric // method. 1430b57cec5SDimitry Andric 1440b57cec5SDimitry Andric Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 1450b57cec5SDimitry Andric 1460b57cec5SDimitry Andric // Update the thread list 1470b57cec5SDimitry Andric UpdateThreads(); 1480b57cec5SDimitry Andric 1490b57cec5SDimitry Andric // Keep track of the threads we successfully set the watchpoint for. If one 1500b57cec5SDimitry Andric // of the thread watchpoint setting operations fails, back off and remove the 1510b57cec5SDimitry Andric // watchpoint for all the threads that were successfully set so we get back 1520b57cec5SDimitry Andric // to a consistent state. 1530b57cec5SDimitry Andric std::vector<NativeThreadProtocol *> watchpoint_established_threads; 1540b57cec5SDimitry Andric 1550b57cec5SDimitry Andric // Tell each thread to set a watchpoint. In the event that hardware 1560b57cec5SDimitry Andric // watchpoints are requested but the SetWatchpoint fails, try to set a 1570b57cec5SDimitry Andric // software watchpoint as a fallback. It's conceivable that if there are 1580b57cec5SDimitry Andric // more threads than hardware watchpoints available, some of the threads will 1590b57cec5SDimitry Andric // fail to set hardware watchpoints while software ones may be available. 1600b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_threads_mutex); 1610b57cec5SDimitry Andric for (const auto &thread : m_threads) { 1620b57cec5SDimitry Andric assert(thread && "thread list should not have a NULL thread!"); 1630b57cec5SDimitry Andric 1640b57cec5SDimitry Andric Status thread_error = 1650b57cec5SDimitry Andric thread->SetWatchpoint(addr, size, watch_flags, hardware); 1660b57cec5SDimitry Andric if (thread_error.Fail() && hardware) { 1670b57cec5SDimitry Andric // Try software watchpoints since we failed on hardware watchpoint 1680b57cec5SDimitry Andric // setting and we may have just run out of hardware watchpoints. 1690b57cec5SDimitry Andric thread_error = thread->SetWatchpoint(addr, size, watch_flags, false); 1700b57cec5SDimitry Andric if (thread_error.Success()) 1710b57cec5SDimitry Andric LLDB_LOG(log, 1720b57cec5SDimitry Andric "hardware watchpoint requested but software watchpoint set"); 1730b57cec5SDimitry Andric } 1740b57cec5SDimitry Andric 1750b57cec5SDimitry Andric if (thread_error.Success()) { 1760b57cec5SDimitry Andric // Remember that we set this watchpoint successfully in case we need to 1770b57cec5SDimitry Andric // clear it later. 1780b57cec5SDimitry Andric watchpoint_established_threads.push_back(thread.get()); 1790b57cec5SDimitry Andric } else { 1800b57cec5SDimitry Andric // Unset the watchpoint for each thread we successfully set so that we 1810b57cec5SDimitry Andric // get back to a consistent state of "not set" for the watchpoint. 1820b57cec5SDimitry Andric for (auto unwatch_thread_sp : watchpoint_established_threads) { 1830b57cec5SDimitry Andric Status remove_error = unwatch_thread_sp->RemoveWatchpoint(addr); 1840b57cec5SDimitry Andric if (remove_error.Fail()) 1850b57cec5SDimitry Andric LLDB_LOG(log, "RemoveWatchpoint failed for pid={0}, tid={1}: {2}", 1860b57cec5SDimitry Andric GetID(), unwatch_thread_sp->GetID(), remove_error); 1870b57cec5SDimitry Andric } 1880b57cec5SDimitry Andric 1890b57cec5SDimitry Andric return thread_error; 1900b57cec5SDimitry Andric } 1910b57cec5SDimitry Andric } 1920b57cec5SDimitry Andric return m_watchpoint_list.Add(addr, size, watch_flags, hardware); 1930b57cec5SDimitry Andric } 1940b57cec5SDimitry Andric 1950b57cec5SDimitry Andric Status NativeProcessProtocol::RemoveWatchpoint(lldb::addr_t addr) { 1960b57cec5SDimitry Andric // Update the thread list 1970b57cec5SDimitry Andric UpdateThreads(); 1980b57cec5SDimitry Andric 1990b57cec5SDimitry Andric Status overall_error; 2000b57cec5SDimitry Andric 2010b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_threads_mutex); 2020b57cec5SDimitry Andric for (const auto &thread : m_threads) { 2030b57cec5SDimitry Andric assert(thread && "thread list should not have a NULL thread!"); 2040b57cec5SDimitry Andric 2050b57cec5SDimitry Andric const Status thread_error = thread->RemoveWatchpoint(addr); 2060b57cec5SDimitry Andric if (thread_error.Fail()) { 2070b57cec5SDimitry Andric // Keep track of the first thread error if any threads fail. We want to 2080b57cec5SDimitry Andric // try to remove the watchpoint from every thread, though, even if one or 2090b57cec5SDimitry Andric // more have errors. 2100b57cec5SDimitry Andric if (!overall_error.Fail()) 2110b57cec5SDimitry Andric overall_error = thread_error; 2120b57cec5SDimitry Andric } 2130b57cec5SDimitry Andric } 2140b57cec5SDimitry Andric const Status error = m_watchpoint_list.Remove(addr); 2150b57cec5SDimitry Andric return overall_error.Fail() ? overall_error : error; 2160b57cec5SDimitry Andric } 2170b57cec5SDimitry Andric 2180b57cec5SDimitry Andric const HardwareBreakpointMap & 2190b57cec5SDimitry Andric NativeProcessProtocol::GetHardwareBreakpointMap() const { 2200b57cec5SDimitry Andric return m_hw_breakpoints_map; 2210b57cec5SDimitry Andric } 2220b57cec5SDimitry Andric 2230b57cec5SDimitry Andric Status NativeProcessProtocol::SetHardwareBreakpoint(lldb::addr_t addr, 2240b57cec5SDimitry Andric size_t size) { 2250b57cec5SDimitry Andric // This default implementation assumes setting a hardware breakpoint for this 2260b57cec5SDimitry Andric // process will require setting same hardware breakpoint for each of its 2270b57cec5SDimitry Andric // existing threads. New thread will do the same once created. 2280b57cec5SDimitry Andric Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 2290b57cec5SDimitry Andric 2300b57cec5SDimitry Andric // Update the thread list 2310b57cec5SDimitry Andric UpdateThreads(); 2320b57cec5SDimitry Andric 2330b57cec5SDimitry Andric // Exit here if target does not have required hardware breakpoint capability. 2340b57cec5SDimitry Andric auto hw_debug_cap = GetHardwareDebugSupportInfo(); 2350b57cec5SDimitry Andric 2360b57cec5SDimitry Andric if (hw_debug_cap == llvm::None || hw_debug_cap->first == 0 || 2370b57cec5SDimitry Andric hw_debug_cap->first <= m_hw_breakpoints_map.size()) 2380b57cec5SDimitry Andric return Status("Target does not have required no of hardware breakpoints"); 2390b57cec5SDimitry Andric 2400b57cec5SDimitry Andric // Vector below stores all thread pointer for which we have we successfully 2410b57cec5SDimitry Andric // set this hardware breakpoint. If any of the current process threads fails 2420b57cec5SDimitry Andric // to set this hardware breakpoint then roll back and remove this breakpoint 2430b57cec5SDimitry Andric // for all the threads that had already set it successfully. 2440b57cec5SDimitry Andric std::vector<NativeThreadProtocol *> breakpoint_established_threads; 2450b57cec5SDimitry Andric 2460b57cec5SDimitry Andric // Request to set a hardware breakpoint for each of current process threads. 2470b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_threads_mutex); 2480b57cec5SDimitry Andric for (const auto &thread : m_threads) { 2490b57cec5SDimitry Andric assert(thread && "thread list should not have a NULL thread!"); 2500b57cec5SDimitry Andric 2510b57cec5SDimitry Andric Status thread_error = thread->SetHardwareBreakpoint(addr, size); 2520b57cec5SDimitry Andric if (thread_error.Success()) { 2530b57cec5SDimitry Andric // Remember that we set this breakpoint successfully in case we need to 2540b57cec5SDimitry Andric // clear it later. 2550b57cec5SDimitry Andric breakpoint_established_threads.push_back(thread.get()); 2560b57cec5SDimitry Andric } else { 2570b57cec5SDimitry Andric // Unset the breakpoint for each thread we successfully set so that we 2580b57cec5SDimitry Andric // get back to a consistent state of "not set" for this hardware 2590b57cec5SDimitry Andric // breakpoint. 2600b57cec5SDimitry Andric for (auto rollback_thread_sp : breakpoint_established_threads) { 2610b57cec5SDimitry Andric Status remove_error = 2620b57cec5SDimitry Andric rollback_thread_sp->RemoveHardwareBreakpoint(addr); 2630b57cec5SDimitry Andric if (remove_error.Fail()) 2640b57cec5SDimitry Andric LLDB_LOG(log, 2650b57cec5SDimitry Andric "RemoveHardwareBreakpoint failed for pid={0}, tid={1}: {2}", 2660b57cec5SDimitry Andric GetID(), rollback_thread_sp->GetID(), remove_error); 2670b57cec5SDimitry Andric } 2680b57cec5SDimitry Andric 2690b57cec5SDimitry Andric return thread_error; 2700b57cec5SDimitry Andric } 2710b57cec5SDimitry Andric } 2720b57cec5SDimitry Andric 2730b57cec5SDimitry Andric // Register new hardware breakpoint into hardware breakpoints map of current 2740b57cec5SDimitry Andric // process. 2750b57cec5SDimitry Andric m_hw_breakpoints_map[addr] = {addr, size}; 2760b57cec5SDimitry Andric 2770b57cec5SDimitry Andric return Status(); 2780b57cec5SDimitry Andric } 2790b57cec5SDimitry Andric 2800b57cec5SDimitry Andric Status NativeProcessProtocol::RemoveHardwareBreakpoint(lldb::addr_t addr) { 2810b57cec5SDimitry Andric // Update the thread list 2820b57cec5SDimitry Andric UpdateThreads(); 2830b57cec5SDimitry Andric 2840b57cec5SDimitry Andric Status error; 2850b57cec5SDimitry Andric 2860b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_threads_mutex); 2870b57cec5SDimitry Andric for (const auto &thread : m_threads) { 2880b57cec5SDimitry Andric assert(thread && "thread list should not have a NULL thread!"); 2890b57cec5SDimitry Andric error = thread->RemoveHardwareBreakpoint(addr); 2900b57cec5SDimitry Andric } 2910b57cec5SDimitry Andric 2920b57cec5SDimitry Andric // Also remove from hardware breakpoint map of current process. 2930b57cec5SDimitry Andric m_hw_breakpoints_map.erase(addr); 2940b57cec5SDimitry Andric 2950b57cec5SDimitry Andric return error; 2960b57cec5SDimitry Andric } 2970b57cec5SDimitry Andric 2980b57cec5SDimitry Andric bool NativeProcessProtocol::RegisterNativeDelegate( 2990b57cec5SDimitry Andric NativeDelegate &native_delegate) { 3000b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_delegates_mutex); 3010b57cec5SDimitry Andric if (std::find(m_delegates.begin(), m_delegates.end(), &native_delegate) != 3020b57cec5SDimitry Andric m_delegates.end()) 3030b57cec5SDimitry Andric return false; 3040b57cec5SDimitry Andric 3050b57cec5SDimitry Andric m_delegates.push_back(&native_delegate); 3060b57cec5SDimitry Andric native_delegate.InitializeDelegate(this); 3070b57cec5SDimitry Andric return true; 3080b57cec5SDimitry Andric } 3090b57cec5SDimitry Andric 3100b57cec5SDimitry Andric bool NativeProcessProtocol::UnregisterNativeDelegate( 3110b57cec5SDimitry Andric NativeDelegate &native_delegate) { 3120b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_delegates_mutex); 3130b57cec5SDimitry Andric 3140b57cec5SDimitry Andric const auto initial_size = m_delegates.size(); 3150b57cec5SDimitry Andric m_delegates.erase( 3160b57cec5SDimitry Andric remove(m_delegates.begin(), m_delegates.end(), &native_delegate), 3170b57cec5SDimitry Andric m_delegates.end()); 3180b57cec5SDimitry Andric 3190b57cec5SDimitry Andric // We removed the delegate if the count of delegates shrank after removing 3200b57cec5SDimitry Andric // all copies of the given native_delegate from the vector. 3210b57cec5SDimitry Andric return m_delegates.size() < initial_size; 3220b57cec5SDimitry Andric } 3230b57cec5SDimitry Andric 3240b57cec5SDimitry Andric void NativeProcessProtocol::SynchronouslyNotifyProcessStateChanged( 3250b57cec5SDimitry Andric lldb::StateType state) { 3260b57cec5SDimitry Andric Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 3270b57cec5SDimitry Andric 3280b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_delegates_mutex); 3290b57cec5SDimitry Andric for (auto native_delegate : m_delegates) 3300b57cec5SDimitry Andric native_delegate->ProcessStateChanged(this, state); 3310b57cec5SDimitry Andric 3320b57cec5SDimitry Andric if (log) { 3330b57cec5SDimitry Andric if (!m_delegates.empty()) { 3349dba64beSDimitry Andric LLDB_LOGF(log, 3359dba64beSDimitry Andric "NativeProcessProtocol::%s: sent state notification [%s] " 3360b57cec5SDimitry Andric "from process %" PRIu64, 3370b57cec5SDimitry Andric __FUNCTION__, lldb_private::StateAsCString(state), GetID()); 3380b57cec5SDimitry Andric } else { 3399dba64beSDimitry Andric LLDB_LOGF(log, 3409dba64beSDimitry Andric "NativeProcessProtocol::%s: would send state notification " 3410b57cec5SDimitry Andric "[%s] from process %" PRIu64 ", but no delegates", 3420b57cec5SDimitry Andric __FUNCTION__, lldb_private::StateAsCString(state), GetID()); 3430b57cec5SDimitry Andric } 3440b57cec5SDimitry Andric } 3450b57cec5SDimitry Andric } 3460b57cec5SDimitry Andric 3470b57cec5SDimitry Andric void NativeProcessProtocol::NotifyDidExec() { 3480b57cec5SDimitry Andric Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 3499dba64beSDimitry Andric LLDB_LOGF(log, "NativeProcessProtocol::%s - preparing to call delegates", 3500b57cec5SDimitry Andric __FUNCTION__); 3510b57cec5SDimitry Andric 3520b57cec5SDimitry Andric { 3530b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_delegates_mutex); 3540b57cec5SDimitry Andric for (auto native_delegate : m_delegates) 3550b57cec5SDimitry Andric native_delegate->DidExec(this); 3560b57cec5SDimitry Andric } 3570b57cec5SDimitry Andric } 3580b57cec5SDimitry Andric 3590b57cec5SDimitry Andric Status NativeProcessProtocol::SetSoftwareBreakpoint(lldb::addr_t addr, 3600b57cec5SDimitry Andric uint32_t size_hint) { 3610b57cec5SDimitry Andric Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_BREAKPOINTS)); 3620b57cec5SDimitry Andric LLDB_LOG(log, "addr = {0:x}, size_hint = {1}", addr, size_hint); 3630b57cec5SDimitry Andric 3640b57cec5SDimitry Andric auto it = m_software_breakpoints.find(addr); 3650b57cec5SDimitry Andric if (it != m_software_breakpoints.end()) { 3660b57cec5SDimitry Andric ++it->second.ref_count; 3670b57cec5SDimitry Andric return Status(); 3680b57cec5SDimitry Andric } 3690b57cec5SDimitry Andric auto expected_bkpt = EnableSoftwareBreakpoint(addr, size_hint); 3700b57cec5SDimitry Andric if (!expected_bkpt) 3710b57cec5SDimitry Andric return Status(expected_bkpt.takeError()); 3720b57cec5SDimitry Andric 3730b57cec5SDimitry Andric m_software_breakpoints.emplace(addr, std::move(*expected_bkpt)); 3740b57cec5SDimitry Andric return Status(); 3750b57cec5SDimitry Andric } 3760b57cec5SDimitry Andric 3770b57cec5SDimitry Andric Status NativeProcessProtocol::RemoveSoftwareBreakpoint(lldb::addr_t addr) { 3780b57cec5SDimitry Andric Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_BREAKPOINTS)); 3790b57cec5SDimitry Andric LLDB_LOG(log, "addr = {0:x}", addr); 3800b57cec5SDimitry Andric auto it = m_software_breakpoints.find(addr); 3810b57cec5SDimitry Andric if (it == m_software_breakpoints.end()) 3820b57cec5SDimitry Andric return Status("Breakpoint not found."); 3830b57cec5SDimitry Andric assert(it->second.ref_count > 0); 3840b57cec5SDimitry Andric if (--it->second.ref_count > 0) 3850b57cec5SDimitry Andric return Status(); 3860b57cec5SDimitry Andric 3870b57cec5SDimitry Andric // This is the last reference. Let's remove the breakpoint. 3880b57cec5SDimitry Andric Status error; 3890b57cec5SDimitry Andric 3900b57cec5SDimitry Andric // Clear a software breakpoint instruction 3910b57cec5SDimitry Andric llvm::SmallVector<uint8_t, 4> curr_break_op( 3920b57cec5SDimitry Andric it->second.breakpoint_opcodes.size(), 0); 3930b57cec5SDimitry Andric 3940b57cec5SDimitry Andric // Read the breakpoint opcode 3950b57cec5SDimitry Andric size_t bytes_read = 0; 3960b57cec5SDimitry Andric error = 3970b57cec5SDimitry Andric ReadMemory(addr, curr_break_op.data(), curr_break_op.size(), bytes_read); 3980b57cec5SDimitry Andric if (error.Fail() || bytes_read < curr_break_op.size()) { 3990b57cec5SDimitry Andric return Status("addr=0x%" PRIx64 4000b57cec5SDimitry Andric ": tried to read %zu bytes but only read %zu", 4010b57cec5SDimitry Andric addr, curr_break_op.size(), bytes_read); 4020b57cec5SDimitry Andric } 4030b57cec5SDimitry Andric const auto &saved = it->second.saved_opcodes; 4040b57cec5SDimitry Andric // Make sure the breakpoint opcode exists at this address 4050b57cec5SDimitry Andric if (makeArrayRef(curr_break_op) != it->second.breakpoint_opcodes) { 4060b57cec5SDimitry Andric if (curr_break_op != it->second.saved_opcodes) 4070b57cec5SDimitry Andric return Status("Original breakpoint trap is no longer in memory."); 4080b57cec5SDimitry Andric LLDB_LOG(log, 4090b57cec5SDimitry Andric "Saved opcodes ({0:@[x]}) have already been restored at {1:x}.", 4100b57cec5SDimitry Andric llvm::make_range(saved.begin(), saved.end()), addr); 4110b57cec5SDimitry Andric } else { 4120b57cec5SDimitry Andric // We found a valid breakpoint opcode at this address, now restore the 4130b57cec5SDimitry Andric // saved opcode. 4140b57cec5SDimitry Andric size_t bytes_written = 0; 4150b57cec5SDimitry Andric error = WriteMemory(addr, saved.data(), saved.size(), bytes_written); 4160b57cec5SDimitry Andric if (error.Fail() || bytes_written < saved.size()) { 4170b57cec5SDimitry Andric return Status("addr=0x%" PRIx64 4180b57cec5SDimitry Andric ": tried to write %zu bytes but only wrote %zu", 4190b57cec5SDimitry Andric addr, saved.size(), bytes_written); 4200b57cec5SDimitry Andric } 4210b57cec5SDimitry Andric 4220b57cec5SDimitry Andric // Verify that our original opcode made it back to the inferior 4230b57cec5SDimitry Andric llvm::SmallVector<uint8_t, 4> verify_opcode(saved.size(), 0); 4240b57cec5SDimitry Andric size_t verify_bytes_read = 0; 4250b57cec5SDimitry Andric error = ReadMemory(addr, verify_opcode.data(), verify_opcode.size(), 4260b57cec5SDimitry Andric verify_bytes_read); 4270b57cec5SDimitry Andric if (error.Fail() || verify_bytes_read < verify_opcode.size()) { 4280b57cec5SDimitry Andric return Status("addr=0x%" PRIx64 4290b57cec5SDimitry Andric ": tried to read %zu verification bytes but only read %zu", 4300b57cec5SDimitry Andric addr, verify_opcode.size(), verify_bytes_read); 4310b57cec5SDimitry Andric } 4320b57cec5SDimitry Andric if (verify_opcode != saved) 4330b57cec5SDimitry Andric LLDB_LOG(log, "Restoring bytes at {0:x}: {1:@[x]}", addr, 4340b57cec5SDimitry Andric llvm::make_range(saved.begin(), saved.end())); 4350b57cec5SDimitry Andric } 4360b57cec5SDimitry Andric 4370b57cec5SDimitry Andric m_software_breakpoints.erase(it); 4380b57cec5SDimitry Andric return Status(); 4390b57cec5SDimitry Andric } 4400b57cec5SDimitry Andric 4410b57cec5SDimitry Andric llvm::Expected<NativeProcessProtocol::SoftwareBreakpoint> 4420b57cec5SDimitry Andric NativeProcessProtocol::EnableSoftwareBreakpoint(lldb::addr_t addr, 4430b57cec5SDimitry Andric uint32_t size_hint) { 4440b57cec5SDimitry Andric Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_BREAKPOINTS)); 4450b57cec5SDimitry Andric 4460b57cec5SDimitry Andric auto expected_trap = GetSoftwareBreakpointTrapOpcode(size_hint); 4470b57cec5SDimitry Andric if (!expected_trap) 4480b57cec5SDimitry Andric return expected_trap.takeError(); 4490b57cec5SDimitry Andric 4500b57cec5SDimitry Andric llvm::SmallVector<uint8_t, 4> saved_opcode_bytes(expected_trap->size(), 0); 4510b57cec5SDimitry Andric // Save the original opcodes by reading them so we can restore later. 4520b57cec5SDimitry Andric size_t bytes_read = 0; 4530b57cec5SDimitry Andric Status error = ReadMemory(addr, saved_opcode_bytes.data(), 4540b57cec5SDimitry Andric saved_opcode_bytes.size(), bytes_read); 4550b57cec5SDimitry Andric if (error.Fail()) 4560b57cec5SDimitry Andric return error.ToError(); 4570b57cec5SDimitry Andric 4580b57cec5SDimitry Andric // Ensure we read as many bytes as we expected. 4590b57cec5SDimitry Andric if (bytes_read != saved_opcode_bytes.size()) { 4600b57cec5SDimitry Andric return llvm::createStringError( 4610b57cec5SDimitry Andric llvm::inconvertibleErrorCode(), 4620b57cec5SDimitry Andric "Failed to read memory while attempting to set breakpoint: attempted " 4630b57cec5SDimitry Andric "to read {0} bytes but only read {1}.", 4640b57cec5SDimitry Andric saved_opcode_bytes.size(), bytes_read); 4650b57cec5SDimitry Andric } 4660b57cec5SDimitry Andric 4670b57cec5SDimitry Andric LLDB_LOG( 4680b57cec5SDimitry Andric log, "Overwriting bytes at {0:x}: {1:@[x]}", addr, 4690b57cec5SDimitry Andric llvm::make_range(saved_opcode_bytes.begin(), saved_opcode_bytes.end())); 4700b57cec5SDimitry Andric 4710b57cec5SDimitry Andric // Write a software breakpoint in place of the original opcode. 4720b57cec5SDimitry Andric size_t bytes_written = 0; 4730b57cec5SDimitry Andric error = WriteMemory(addr, expected_trap->data(), expected_trap->size(), 4740b57cec5SDimitry Andric bytes_written); 4750b57cec5SDimitry Andric if (error.Fail()) 4760b57cec5SDimitry Andric return error.ToError(); 4770b57cec5SDimitry Andric 4780b57cec5SDimitry Andric // Ensure we wrote as many bytes as we expected. 4790b57cec5SDimitry Andric if (bytes_written != expected_trap->size()) { 4800b57cec5SDimitry Andric return llvm::createStringError( 4810b57cec5SDimitry Andric llvm::inconvertibleErrorCode(), 4820b57cec5SDimitry Andric "Failed write memory while attempting to set " 4830b57cec5SDimitry Andric "breakpoint: attempted to write {0} bytes but only wrote {1}", 4840b57cec5SDimitry Andric expected_trap->size(), bytes_written); 4850b57cec5SDimitry Andric } 4860b57cec5SDimitry Andric 4870b57cec5SDimitry Andric llvm::SmallVector<uint8_t, 4> verify_bp_opcode_bytes(expected_trap->size(), 4880b57cec5SDimitry Andric 0); 4890b57cec5SDimitry Andric size_t verify_bytes_read = 0; 4900b57cec5SDimitry Andric error = ReadMemory(addr, verify_bp_opcode_bytes.data(), 4910b57cec5SDimitry Andric verify_bp_opcode_bytes.size(), verify_bytes_read); 4920b57cec5SDimitry Andric if (error.Fail()) 4930b57cec5SDimitry Andric return error.ToError(); 4940b57cec5SDimitry Andric 4950b57cec5SDimitry Andric // Ensure we read as many verification bytes as we expected. 4960b57cec5SDimitry Andric if (verify_bytes_read != verify_bp_opcode_bytes.size()) { 4970b57cec5SDimitry Andric return llvm::createStringError( 4980b57cec5SDimitry Andric llvm::inconvertibleErrorCode(), 4990b57cec5SDimitry Andric "Failed to read memory while " 5000b57cec5SDimitry Andric "attempting to verify breakpoint: attempted to read {0} bytes " 5010b57cec5SDimitry Andric "but only read {1}", 5020b57cec5SDimitry Andric verify_bp_opcode_bytes.size(), verify_bytes_read); 5030b57cec5SDimitry Andric } 5040b57cec5SDimitry Andric 5050b57cec5SDimitry Andric if (llvm::makeArrayRef(verify_bp_opcode_bytes.data(), verify_bytes_read) != 5060b57cec5SDimitry Andric *expected_trap) { 5070b57cec5SDimitry Andric return llvm::createStringError( 5080b57cec5SDimitry Andric llvm::inconvertibleErrorCode(), 5090b57cec5SDimitry Andric "Verification of software breakpoint " 5100b57cec5SDimitry Andric "writing failed - trap opcodes not successfully read back " 5110b57cec5SDimitry Andric "after writing when setting breakpoint at {0:x}", 5120b57cec5SDimitry Andric addr); 5130b57cec5SDimitry Andric } 5140b57cec5SDimitry Andric 5150b57cec5SDimitry Andric LLDB_LOG(log, "addr = {0:x}: SUCCESS", addr); 5160b57cec5SDimitry Andric return SoftwareBreakpoint{1, saved_opcode_bytes, *expected_trap}; 5170b57cec5SDimitry Andric } 5180b57cec5SDimitry Andric 5190b57cec5SDimitry Andric llvm::Expected<llvm::ArrayRef<uint8_t>> 5200b57cec5SDimitry Andric NativeProcessProtocol::GetSoftwareBreakpointTrapOpcode(size_t size_hint) { 5210b57cec5SDimitry Andric static const uint8_t g_aarch64_opcode[] = {0x00, 0x00, 0x20, 0xd4}; 5220b57cec5SDimitry Andric static const uint8_t g_i386_opcode[] = {0xCC}; 5230b57cec5SDimitry Andric static const uint8_t g_mips64_opcode[] = {0x00, 0x00, 0x00, 0x0d}; 5240b57cec5SDimitry Andric static const uint8_t g_mips64el_opcode[] = {0x0d, 0x00, 0x00, 0x00}; 5250b57cec5SDimitry Andric static const uint8_t g_s390x_opcode[] = {0x00, 0x01}; 5260b57cec5SDimitry Andric static const uint8_t g_ppc64le_opcode[] = {0x08, 0x00, 0xe0, 0x7f}; // trap 5270b57cec5SDimitry Andric 5280b57cec5SDimitry Andric switch (GetArchitecture().GetMachine()) { 5290b57cec5SDimitry Andric case llvm::Triple::aarch64: 5309dba64beSDimitry Andric case llvm::Triple::aarch64_32: 5310b57cec5SDimitry Andric return llvm::makeArrayRef(g_aarch64_opcode); 5320b57cec5SDimitry Andric 5330b57cec5SDimitry Andric case llvm::Triple::x86: 5340b57cec5SDimitry Andric case llvm::Triple::x86_64: 5350b57cec5SDimitry Andric return llvm::makeArrayRef(g_i386_opcode); 5360b57cec5SDimitry Andric 5370b57cec5SDimitry Andric case llvm::Triple::mips: 5380b57cec5SDimitry Andric case llvm::Triple::mips64: 5390b57cec5SDimitry Andric return llvm::makeArrayRef(g_mips64_opcode); 5400b57cec5SDimitry Andric 5410b57cec5SDimitry Andric case llvm::Triple::mipsel: 5420b57cec5SDimitry Andric case llvm::Triple::mips64el: 5430b57cec5SDimitry Andric return llvm::makeArrayRef(g_mips64el_opcode); 5440b57cec5SDimitry Andric 5450b57cec5SDimitry Andric case llvm::Triple::systemz: 5460b57cec5SDimitry Andric return llvm::makeArrayRef(g_s390x_opcode); 5470b57cec5SDimitry Andric 5480b57cec5SDimitry Andric case llvm::Triple::ppc64le: 5490b57cec5SDimitry Andric return llvm::makeArrayRef(g_ppc64le_opcode); 5500b57cec5SDimitry Andric 5510b57cec5SDimitry Andric default: 5520b57cec5SDimitry Andric return llvm::createStringError(llvm::inconvertibleErrorCode(), 5530b57cec5SDimitry Andric "CPU type not supported!"); 5540b57cec5SDimitry Andric } 5550b57cec5SDimitry Andric } 5560b57cec5SDimitry Andric 5570b57cec5SDimitry Andric size_t NativeProcessProtocol::GetSoftwareBreakpointPCOffset() { 5580b57cec5SDimitry Andric switch (GetArchitecture().GetMachine()) { 5590b57cec5SDimitry Andric case llvm::Triple::x86: 5600b57cec5SDimitry Andric case llvm::Triple::x86_64: 5610b57cec5SDimitry Andric case llvm::Triple::systemz: 5620b57cec5SDimitry Andric // These architectures report increment the PC after breakpoint is hit. 5630b57cec5SDimitry Andric return cantFail(GetSoftwareBreakpointTrapOpcode(0)).size(); 5640b57cec5SDimitry Andric 5650b57cec5SDimitry Andric case llvm::Triple::arm: 5660b57cec5SDimitry Andric case llvm::Triple::aarch64: 5679dba64beSDimitry Andric case llvm::Triple::aarch64_32: 5680b57cec5SDimitry Andric case llvm::Triple::mips64: 5690b57cec5SDimitry Andric case llvm::Triple::mips64el: 5700b57cec5SDimitry Andric case llvm::Triple::mips: 5710b57cec5SDimitry Andric case llvm::Triple::mipsel: 5720b57cec5SDimitry Andric case llvm::Triple::ppc64le: 5730b57cec5SDimitry Andric // On these architectures the PC doesn't get updated for breakpoint hits. 5740b57cec5SDimitry Andric return 0; 5750b57cec5SDimitry Andric 5760b57cec5SDimitry Andric default: 5770b57cec5SDimitry Andric llvm_unreachable("CPU type not supported!"); 5780b57cec5SDimitry Andric } 5790b57cec5SDimitry Andric } 5800b57cec5SDimitry Andric 5810b57cec5SDimitry Andric void NativeProcessProtocol::FixupBreakpointPCAsNeeded( 5820b57cec5SDimitry Andric NativeThreadProtocol &thread) { 5830b57cec5SDimitry Andric Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_BREAKPOINTS); 5840b57cec5SDimitry Andric 5850b57cec5SDimitry Andric Status error; 5860b57cec5SDimitry Andric 5870b57cec5SDimitry Andric // Find out the size of a breakpoint (might depend on where we are in the 5880b57cec5SDimitry Andric // code). 5890b57cec5SDimitry Andric NativeRegisterContext &context = thread.GetRegisterContext(); 5900b57cec5SDimitry Andric 5910b57cec5SDimitry Andric uint32_t breakpoint_size = GetSoftwareBreakpointPCOffset(); 5920b57cec5SDimitry Andric LLDB_LOG(log, "breakpoint size: {0}", breakpoint_size); 5930b57cec5SDimitry Andric if (breakpoint_size == 0) 5940b57cec5SDimitry Andric return; 5950b57cec5SDimitry Andric 5960b57cec5SDimitry Andric // First try probing for a breakpoint at a software breakpoint location: PC - 5970b57cec5SDimitry Andric // breakpoint size. 5980b57cec5SDimitry Andric const lldb::addr_t initial_pc_addr = context.GetPCfromBreakpointLocation(); 5990b57cec5SDimitry Andric lldb::addr_t breakpoint_addr = initial_pc_addr; 6000b57cec5SDimitry Andric // Do not allow breakpoint probe to wrap around. 6010b57cec5SDimitry Andric if (breakpoint_addr >= breakpoint_size) 6020b57cec5SDimitry Andric breakpoint_addr -= breakpoint_size; 6030b57cec5SDimitry Andric 6040b57cec5SDimitry Andric if (m_software_breakpoints.count(breakpoint_addr) == 0) { 6050b57cec5SDimitry Andric // We didn't find one at a software probe location. Nothing to do. 6060b57cec5SDimitry Andric LLDB_LOG(log, 6070b57cec5SDimitry Andric "pid {0} no lldb software breakpoint found at current pc with " 6080b57cec5SDimitry Andric "adjustment: {1}", 6090b57cec5SDimitry Andric GetID(), breakpoint_addr); 6100b57cec5SDimitry Andric return; 6110b57cec5SDimitry Andric } 6120b57cec5SDimitry Andric 6130b57cec5SDimitry Andric // 6140b57cec5SDimitry Andric // We have a software breakpoint and need to adjust the PC. 6150b57cec5SDimitry Andric // 6160b57cec5SDimitry Andric 6170b57cec5SDimitry Andric // Change the program counter. 6180b57cec5SDimitry Andric LLDB_LOG(log, "pid {0} tid {1}: changing PC from {2:x} to {3:x}", GetID(), 6190b57cec5SDimitry Andric thread.GetID(), initial_pc_addr, breakpoint_addr); 6200b57cec5SDimitry Andric 6210b57cec5SDimitry Andric error = context.SetPC(breakpoint_addr); 6220b57cec5SDimitry Andric if (error.Fail()) { 6230b57cec5SDimitry Andric // This can happen in case the process was killed between the time we read 6240b57cec5SDimitry Andric // the PC and when we are updating it. There's nothing better to do than to 6250b57cec5SDimitry Andric // swallow the error. 6260b57cec5SDimitry Andric LLDB_LOG(log, "pid {0} tid {1}: failed to set PC: {2}", GetID(), 6270b57cec5SDimitry Andric thread.GetID(), error); 6280b57cec5SDimitry Andric } 6290b57cec5SDimitry Andric } 6300b57cec5SDimitry Andric 6310b57cec5SDimitry Andric Status NativeProcessProtocol::RemoveBreakpoint(lldb::addr_t addr, 6320b57cec5SDimitry Andric bool hardware) { 6330b57cec5SDimitry Andric if (hardware) 6340b57cec5SDimitry Andric return RemoveHardwareBreakpoint(addr); 6350b57cec5SDimitry Andric else 6360b57cec5SDimitry Andric return RemoveSoftwareBreakpoint(addr); 6370b57cec5SDimitry Andric } 6380b57cec5SDimitry Andric 6390b57cec5SDimitry Andric Status NativeProcessProtocol::ReadMemoryWithoutTrap(lldb::addr_t addr, 6400b57cec5SDimitry Andric void *buf, size_t size, 6410b57cec5SDimitry Andric size_t &bytes_read) { 6420b57cec5SDimitry Andric Status error = ReadMemory(addr, buf, size, bytes_read); 6430b57cec5SDimitry Andric if (error.Fail()) 6440b57cec5SDimitry Andric return error; 6450b57cec5SDimitry Andric 6460b57cec5SDimitry Andric auto data = 6470b57cec5SDimitry Andric llvm::makeMutableArrayRef(static_cast<uint8_t *>(buf), bytes_read); 6480b57cec5SDimitry Andric for (const auto &pair : m_software_breakpoints) { 6490b57cec5SDimitry Andric lldb::addr_t bp_addr = pair.first; 6500b57cec5SDimitry Andric auto saved_opcodes = makeArrayRef(pair.second.saved_opcodes); 6510b57cec5SDimitry Andric 6520b57cec5SDimitry Andric if (bp_addr + saved_opcodes.size() < addr || addr + bytes_read <= bp_addr) 6530b57cec5SDimitry Andric continue; // Breapoint not in range, ignore 6540b57cec5SDimitry Andric 6550b57cec5SDimitry Andric if (bp_addr < addr) { 6560b57cec5SDimitry Andric saved_opcodes = saved_opcodes.drop_front(addr - bp_addr); 6570b57cec5SDimitry Andric bp_addr = addr; 6580b57cec5SDimitry Andric } 6590b57cec5SDimitry Andric auto bp_data = data.drop_front(bp_addr - addr); 6600b57cec5SDimitry Andric std::copy_n(saved_opcodes.begin(), 6610b57cec5SDimitry Andric std::min(saved_opcodes.size(), bp_data.size()), 6620b57cec5SDimitry Andric bp_data.begin()); 6630b57cec5SDimitry Andric } 6640b57cec5SDimitry Andric return Status(); 6650b57cec5SDimitry Andric } 6660b57cec5SDimitry Andric 6679dba64beSDimitry Andric llvm::Expected<llvm::StringRef> 6689dba64beSDimitry Andric NativeProcessProtocol::ReadCStringFromMemory(lldb::addr_t addr, char *buffer, 6699dba64beSDimitry Andric size_t max_size, 6709dba64beSDimitry Andric size_t &total_bytes_read) { 6719dba64beSDimitry Andric static const size_t cache_line_size = 6729dba64beSDimitry Andric llvm::sys::Process::getPageSizeEstimate(); 6739dba64beSDimitry Andric size_t bytes_read = 0; 6749dba64beSDimitry Andric size_t bytes_left = max_size; 6759dba64beSDimitry Andric addr_t curr_addr = addr; 6769dba64beSDimitry Andric size_t string_size; 6779dba64beSDimitry Andric char *curr_buffer = buffer; 6789dba64beSDimitry Andric total_bytes_read = 0; 6799dba64beSDimitry Andric Status status; 6809dba64beSDimitry Andric 6819dba64beSDimitry Andric while (bytes_left > 0 && status.Success()) { 6829dba64beSDimitry Andric addr_t cache_line_bytes_left = 6839dba64beSDimitry Andric cache_line_size - (curr_addr % cache_line_size); 6849dba64beSDimitry Andric addr_t bytes_to_read = std::min<addr_t>(bytes_left, cache_line_bytes_left); 685480093f4SDimitry Andric status = ReadMemory(curr_addr, static_cast<void *>(curr_buffer), 6869dba64beSDimitry Andric bytes_to_read, bytes_read); 6879dba64beSDimitry Andric 6889dba64beSDimitry Andric if (bytes_read == 0) 6899dba64beSDimitry Andric break; 6909dba64beSDimitry Andric 6919dba64beSDimitry Andric void *str_end = std::memchr(curr_buffer, '\0', bytes_read); 6929dba64beSDimitry Andric if (str_end != nullptr) { 6939dba64beSDimitry Andric total_bytes_read = 694480093f4SDimitry Andric static_cast<size_t>((static_cast<char *>(str_end) - buffer + 1)); 6959dba64beSDimitry Andric status.Clear(); 6969dba64beSDimitry Andric break; 6979dba64beSDimitry Andric } 6989dba64beSDimitry Andric 6999dba64beSDimitry Andric total_bytes_read += bytes_read; 7009dba64beSDimitry Andric curr_buffer += bytes_read; 7019dba64beSDimitry Andric curr_addr += bytes_read; 7029dba64beSDimitry Andric bytes_left -= bytes_read; 7039dba64beSDimitry Andric } 7049dba64beSDimitry Andric 7059dba64beSDimitry Andric string_size = total_bytes_read - 1; 7069dba64beSDimitry Andric 7079dba64beSDimitry Andric // Make sure we return a null terminated string. 7089dba64beSDimitry Andric if (bytes_left == 0 && max_size > 0 && buffer[max_size - 1] != '\0') { 7099dba64beSDimitry Andric buffer[max_size - 1] = '\0'; 7109dba64beSDimitry Andric total_bytes_read--; 7119dba64beSDimitry Andric } 7129dba64beSDimitry Andric 7139dba64beSDimitry Andric if (!status.Success()) 7149dba64beSDimitry Andric return status.ToError(); 7159dba64beSDimitry Andric 7169dba64beSDimitry Andric return llvm::StringRef(buffer, string_size); 7179dba64beSDimitry Andric } 7189dba64beSDimitry Andric 7190b57cec5SDimitry Andric lldb::StateType NativeProcessProtocol::GetState() const { 7200b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_state_mutex); 7210b57cec5SDimitry Andric return m_state; 7220b57cec5SDimitry Andric } 7230b57cec5SDimitry Andric 7240b57cec5SDimitry Andric void NativeProcessProtocol::SetState(lldb::StateType state, 7250b57cec5SDimitry Andric bool notify_delegates) { 7260b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_state_mutex); 7270b57cec5SDimitry Andric 7280b57cec5SDimitry Andric if (state == m_state) 7290b57cec5SDimitry Andric return; 7300b57cec5SDimitry Andric 7310b57cec5SDimitry Andric m_state = state; 7320b57cec5SDimitry Andric 7330b57cec5SDimitry Andric if (StateIsStoppedState(state, false)) { 7340b57cec5SDimitry Andric ++m_stop_id; 7350b57cec5SDimitry Andric 7360b57cec5SDimitry Andric // Give process a chance to do any stop id bump processing, such as 7370b57cec5SDimitry Andric // clearing cached data that is invalidated each time the process runs. 7380b57cec5SDimitry Andric // Note if/when we support some threads running, we'll end up needing to 7390b57cec5SDimitry Andric // manage this per thread and per process. 7400b57cec5SDimitry Andric DoStopIDBumped(m_stop_id); 7410b57cec5SDimitry Andric } 7420b57cec5SDimitry Andric 7430b57cec5SDimitry Andric // Optionally notify delegates of the state change. 7440b57cec5SDimitry Andric if (notify_delegates) 7450b57cec5SDimitry Andric SynchronouslyNotifyProcessStateChanged(state); 7460b57cec5SDimitry Andric } 7470b57cec5SDimitry Andric 7480b57cec5SDimitry Andric uint32_t NativeProcessProtocol::GetStopID() const { 7490b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_state_mutex); 7500b57cec5SDimitry Andric return m_stop_id; 7510b57cec5SDimitry Andric } 7520b57cec5SDimitry Andric 7530b57cec5SDimitry Andric void NativeProcessProtocol::DoStopIDBumped(uint32_t /* newBumpId */) { 7540b57cec5SDimitry Andric // Default implementation does nothing. 7550b57cec5SDimitry Andric } 7560b57cec5SDimitry Andric 7570b57cec5SDimitry Andric NativeProcessProtocol::Factory::~Factory() = default; 758