15ffd83dbSDimitry Andric //===-- NativeProcessProtocol.cpp -----------------------------------------===// 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); 301e8d8bef9SDimitry Andric if (llvm::is_contained(m_delegates, &native_delegate)) 3020b57cec5SDimitry Andric return false; 3030b57cec5SDimitry Andric 3040b57cec5SDimitry Andric m_delegates.push_back(&native_delegate); 3050b57cec5SDimitry Andric native_delegate.InitializeDelegate(this); 3060b57cec5SDimitry Andric return true; 3070b57cec5SDimitry Andric } 3080b57cec5SDimitry Andric 3090b57cec5SDimitry Andric bool NativeProcessProtocol::UnregisterNativeDelegate( 3100b57cec5SDimitry Andric NativeDelegate &native_delegate) { 3110b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_delegates_mutex); 3120b57cec5SDimitry Andric 3130b57cec5SDimitry Andric const auto initial_size = m_delegates.size(); 3140b57cec5SDimitry Andric m_delegates.erase( 3150b57cec5SDimitry Andric remove(m_delegates.begin(), m_delegates.end(), &native_delegate), 3160b57cec5SDimitry Andric m_delegates.end()); 3170b57cec5SDimitry Andric 3180b57cec5SDimitry Andric // We removed the delegate if the count of delegates shrank after removing 3190b57cec5SDimitry Andric // all copies of the given native_delegate from the vector. 3200b57cec5SDimitry Andric return m_delegates.size() < initial_size; 3210b57cec5SDimitry Andric } 3220b57cec5SDimitry Andric 3230b57cec5SDimitry Andric void NativeProcessProtocol::SynchronouslyNotifyProcessStateChanged( 3240b57cec5SDimitry Andric lldb::StateType state) { 3250b57cec5SDimitry Andric Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 3260b57cec5SDimitry Andric 3270b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_delegates_mutex); 3280b57cec5SDimitry Andric for (auto native_delegate : m_delegates) 3290b57cec5SDimitry Andric native_delegate->ProcessStateChanged(this, state); 3300b57cec5SDimitry Andric 3310b57cec5SDimitry Andric if (log) { 3320b57cec5SDimitry Andric if (!m_delegates.empty()) { 3339dba64beSDimitry Andric LLDB_LOGF(log, 3349dba64beSDimitry Andric "NativeProcessProtocol::%s: sent state notification [%s] " 3350b57cec5SDimitry Andric "from process %" PRIu64, 3360b57cec5SDimitry Andric __FUNCTION__, lldb_private::StateAsCString(state), GetID()); 3370b57cec5SDimitry Andric } else { 3389dba64beSDimitry Andric LLDB_LOGF(log, 3399dba64beSDimitry Andric "NativeProcessProtocol::%s: would send state notification " 3400b57cec5SDimitry Andric "[%s] from process %" PRIu64 ", but no delegates", 3410b57cec5SDimitry Andric __FUNCTION__, lldb_private::StateAsCString(state), GetID()); 3420b57cec5SDimitry Andric } 3430b57cec5SDimitry Andric } 3440b57cec5SDimitry Andric } 3450b57cec5SDimitry Andric 3460b57cec5SDimitry Andric void NativeProcessProtocol::NotifyDidExec() { 3470b57cec5SDimitry Andric Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 3489dba64beSDimitry Andric LLDB_LOGF(log, "NativeProcessProtocol::%s - preparing to call delegates", 3490b57cec5SDimitry Andric __FUNCTION__); 3500b57cec5SDimitry Andric 3510b57cec5SDimitry Andric { 3520b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_delegates_mutex); 3530b57cec5SDimitry Andric for (auto native_delegate : m_delegates) 3540b57cec5SDimitry Andric native_delegate->DidExec(this); 3550b57cec5SDimitry Andric } 3560b57cec5SDimitry Andric } 3570b57cec5SDimitry Andric 3580b57cec5SDimitry Andric Status NativeProcessProtocol::SetSoftwareBreakpoint(lldb::addr_t addr, 3590b57cec5SDimitry Andric uint32_t size_hint) { 3600b57cec5SDimitry Andric Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_BREAKPOINTS)); 3610b57cec5SDimitry Andric LLDB_LOG(log, "addr = {0:x}, size_hint = {1}", addr, size_hint); 3620b57cec5SDimitry Andric 3630b57cec5SDimitry Andric auto it = m_software_breakpoints.find(addr); 3640b57cec5SDimitry Andric if (it != m_software_breakpoints.end()) { 3650b57cec5SDimitry Andric ++it->second.ref_count; 3660b57cec5SDimitry Andric return Status(); 3670b57cec5SDimitry Andric } 3680b57cec5SDimitry Andric auto expected_bkpt = EnableSoftwareBreakpoint(addr, size_hint); 3690b57cec5SDimitry Andric if (!expected_bkpt) 3700b57cec5SDimitry Andric return Status(expected_bkpt.takeError()); 3710b57cec5SDimitry Andric 3720b57cec5SDimitry Andric m_software_breakpoints.emplace(addr, std::move(*expected_bkpt)); 3730b57cec5SDimitry Andric return Status(); 3740b57cec5SDimitry Andric } 3750b57cec5SDimitry Andric 3760b57cec5SDimitry Andric Status NativeProcessProtocol::RemoveSoftwareBreakpoint(lldb::addr_t addr) { 3770b57cec5SDimitry Andric Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_BREAKPOINTS)); 3780b57cec5SDimitry Andric LLDB_LOG(log, "addr = {0:x}", addr); 3790b57cec5SDimitry Andric auto it = m_software_breakpoints.find(addr); 3800b57cec5SDimitry Andric if (it == m_software_breakpoints.end()) 3810b57cec5SDimitry Andric return Status("Breakpoint not found."); 3820b57cec5SDimitry Andric assert(it->second.ref_count > 0); 3830b57cec5SDimitry Andric if (--it->second.ref_count > 0) 3840b57cec5SDimitry Andric return Status(); 3850b57cec5SDimitry Andric 3860b57cec5SDimitry Andric // This is the last reference. Let's remove the breakpoint. 3870b57cec5SDimitry Andric Status error; 3880b57cec5SDimitry Andric 3890b57cec5SDimitry Andric // Clear a software breakpoint instruction 3900b57cec5SDimitry Andric llvm::SmallVector<uint8_t, 4> curr_break_op( 3910b57cec5SDimitry Andric it->second.breakpoint_opcodes.size(), 0); 3920b57cec5SDimitry Andric 3930b57cec5SDimitry Andric // Read the breakpoint opcode 3940b57cec5SDimitry Andric size_t bytes_read = 0; 3950b57cec5SDimitry Andric error = 3960b57cec5SDimitry Andric ReadMemory(addr, curr_break_op.data(), curr_break_op.size(), bytes_read); 3970b57cec5SDimitry Andric if (error.Fail() || bytes_read < curr_break_op.size()) { 3980b57cec5SDimitry Andric return Status("addr=0x%" PRIx64 3990b57cec5SDimitry Andric ": tried to read %zu bytes but only read %zu", 4000b57cec5SDimitry Andric addr, curr_break_op.size(), bytes_read); 4010b57cec5SDimitry Andric } 4020b57cec5SDimitry Andric const auto &saved = it->second.saved_opcodes; 4030b57cec5SDimitry Andric // Make sure the breakpoint opcode exists at this address 4040b57cec5SDimitry Andric if (makeArrayRef(curr_break_op) != it->second.breakpoint_opcodes) { 4050b57cec5SDimitry Andric if (curr_break_op != it->second.saved_opcodes) 4060b57cec5SDimitry Andric return Status("Original breakpoint trap is no longer in memory."); 4070b57cec5SDimitry Andric LLDB_LOG(log, 4080b57cec5SDimitry Andric "Saved opcodes ({0:@[x]}) have already been restored at {1:x}.", 4090b57cec5SDimitry Andric llvm::make_range(saved.begin(), saved.end()), addr); 4100b57cec5SDimitry Andric } else { 4110b57cec5SDimitry Andric // We found a valid breakpoint opcode at this address, now restore the 4120b57cec5SDimitry Andric // saved opcode. 4130b57cec5SDimitry Andric size_t bytes_written = 0; 4140b57cec5SDimitry Andric error = WriteMemory(addr, saved.data(), saved.size(), bytes_written); 4150b57cec5SDimitry Andric if (error.Fail() || bytes_written < saved.size()) { 4160b57cec5SDimitry Andric return Status("addr=0x%" PRIx64 4170b57cec5SDimitry Andric ": tried to write %zu bytes but only wrote %zu", 4180b57cec5SDimitry Andric addr, saved.size(), bytes_written); 4190b57cec5SDimitry Andric } 4200b57cec5SDimitry Andric 4210b57cec5SDimitry Andric // Verify that our original opcode made it back to the inferior 4220b57cec5SDimitry Andric llvm::SmallVector<uint8_t, 4> verify_opcode(saved.size(), 0); 4230b57cec5SDimitry Andric size_t verify_bytes_read = 0; 4240b57cec5SDimitry Andric error = ReadMemory(addr, verify_opcode.data(), verify_opcode.size(), 4250b57cec5SDimitry Andric verify_bytes_read); 4260b57cec5SDimitry Andric if (error.Fail() || verify_bytes_read < verify_opcode.size()) { 4270b57cec5SDimitry Andric return Status("addr=0x%" PRIx64 4280b57cec5SDimitry Andric ": tried to read %zu verification bytes but only read %zu", 4290b57cec5SDimitry Andric addr, verify_opcode.size(), verify_bytes_read); 4300b57cec5SDimitry Andric } 4310b57cec5SDimitry Andric if (verify_opcode != saved) 4320b57cec5SDimitry Andric LLDB_LOG(log, "Restoring bytes at {0:x}: {1:@[x]}", addr, 4330b57cec5SDimitry Andric llvm::make_range(saved.begin(), saved.end())); 4340b57cec5SDimitry Andric } 4350b57cec5SDimitry Andric 4360b57cec5SDimitry Andric m_software_breakpoints.erase(it); 4370b57cec5SDimitry Andric return Status(); 4380b57cec5SDimitry Andric } 4390b57cec5SDimitry Andric 4400b57cec5SDimitry Andric llvm::Expected<NativeProcessProtocol::SoftwareBreakpoint> 4410b57cec5SDimitry Andric NativeProcessProtocol::EnableSoftwareBreakpoint(lldb::addr_t addr, 4420b57cec5SDimitry Andric uint32_t size_hint) { 4430b57cec5SDimitry Andric Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_BREAKPOINTS)); 4440b57cec5SDimitry Andric 4450b57cec5SDimitry Andric auto expected_trap = GetSoftwareBreakpointTrapOpcode(size_hint); 4460b57cec5SDimitry Andric if (!expected_trap) 4470b57cec5SDimitry Andric return expected_trap.takeError(); 4480b57cec5SDimitry Andric 4490b57cec5SDimitry Andric llvm::SmallVector<uint8_t, 4> saved_opcode_bytes(expected_trap->size(), 0); 4500b57cec5SDimitry Andric // Save the original opcodes by reading them so we can restore later. 4510b57cec5SDimitry Andric size_t bytes_read = 0; 4520b57cec5SDimitry Andric Status error = ReadMemory(addr, saved_opcode_bytes.data(), 4530b57cec5SDimitry Andric saved_opcode_bytes.size(), bytes_read); 4540b57cec5SDimitry Andric if (error.Fail()) 4550b57cec5SDimitry Andric return error.ToError(); 4560b57cec5SDimitry Andric 4570b57cec5SDimitry Andric // Ensure we read as many bytes as we expected. 4580b57cec5SDimitry Andric if (bytes_read != saved_opcode_bytes.size()) { 4590b57cec5SDimitry Andric return llvm::createStringError( 4600b57cec5SDimitry Andric llvm::inconvertibleErrorCode(), 4610b57cec5SDimitry Andric "Failed to read memory while attempting to set breakpoint: attempted " 4620b57cec5SDimitry Andric "to read {0} bytes but only read {1}.", 4630b57cec5SDimitry Andric saved_opcode_bytes.size(), bytes_read); 4640b57cec5SDimitry Andric } 4650b57cec5SDimitry Andric 4660b57cec5SDimitry Andric LLDB_LOG( 4670b57cec5SDimitry Andric log, "Overwriting bytes at {0:x}: {1:@[x]}", addr, 4680b57cec5SDimitry Andric llvm::make_range(saved_opcode_bytes.begin(), saved_opcode_bytes.end())); 4690b57cec5SDimitry Andric 4700b57cec5SDimitry Andric // Write a software breakpoint in place of the original opcode. 4710b57cec5SDimitry Andric size_t bytes_written = 0; 4720b57cec5SDimitry Andric error = WriteMemory(addr, expected_trap->data(), expected_trap->size(), 4730b57cec5SDimitry Andric bytes_written); 4740b57cec5SDimitry Andric if (error.Fail()) 4750b57cec5SDimitry Andric return error.ToError(); 4760b57cec5SDimitry Andric 4770b57cec5SDimitry Andric // Ensure we wrote as many bytes as we expected. 4780b57cec5SDimitry Andric if (bytes_written != expected_trap->size()) { 4790b57cec5SDimitry Andric return llvm::createStringError( 4800b57cec5SDimitry Andric llvm::inconvertibleErrorCode(), 4810b57cec5SDimitry Andric "Failed write memory while attempting to set " 4820b57cec5SDimitry Andric "breakpoint: attempted to write {0} bytes but only wrote {1}", 4830b57cec5SDimitry Andric expected_trap->size(), bytes_written); 4840b57cec5SDimitry Andric } 4850b57cec5SDimitry Andric 4860b57cec5SDimitry Andric llvm::SmallVector<uint8_t, 4> verify_bp_opcode_bytes(expected_trap->size(), 4870b57cec5SDimitry Andric 0); 4880b57cec5SDimitry Andric size_t verify_bytes_read = 0; 4890b57cec5SDimitry Andric error = ReadMemory(addr, verify_bp_opcode_bytes.data(), 4900b57cec5SDimitry Andric verify_bp_opcode_bytes.size(), verify_bytes_read); 4910b57cec5SDimitry Andric if (error.Fail()) 4920b57cec5SDimitry Andric return error.ToError(); 4930b57cec5SDimitry Andric 4940b57cec5SDimitry Andric // Ensure we read as many verification bytes as we expected. 4950b57cec5SDimitry Andric if (verify_bytes_read != verify_bp_opcode_bytes.size()) { 4960b57cec5SDimitry Andric return llvm::createStringError( 4970b57cec5SDimitry Andric llvm::inconvertibleErrorCode(), 4980b57cec5SDimitry Andric "Failed to read memory while " 4990b57cec5SDimitry Andric "attempting to verify breakpoint: attempted to read {0} bytes " 5000b57cec5SDimitry Andric "but only read {1}", 5010b57cec5SDimitry Andric verify_bp_opcode_bytes.size(), verify_bytes_read); 5020b57cec5SDimitry Andric } 5030b57cec5SDimitry Andric 5040b57cec5SDimitry Andric if (llvm::makeArrayRef(verify_bp_opcode_bytes.data(), verify_bytes_read) != 5050b57cec5SDimitry Andric *expected_trap) { 5060b57cec5SDimitry Andric return llvm::createStringError( 5070b57cec5SDimitry Andric llvm::inconvertibleErrorCode(), 5080b57cec5SDimitry Andric "Verification of software breakpoint " 5090b57cec5SDimitry Andric "writing failed - trap opcodes not successfully read back " 5100b57cec5SDimitry Andric "after writing when setting breakpoint at {0:x}", 5110b57cec5SDimitry Andric addr); 5120b57cec5SDimitry Andric } 5130b57cec5SDimitry Andric 5140b57cec5SDimitry Andric LLDB_LOG(log, "addr = {0:x}: SUCCESS", addr); 5150b57cec5SDimitry Andric return SoftwareBreakpoint{1, saved_opcode_bytes, *expected_trap}; 5160b57cec5SDimitry Andric } 5170b57cec5SDimitry Andric 5180b57cec5SDimitry Andric llvm::Expected<llvm::ArrayRef<uint8_t>> 5190b57cec5SDimitry Andric NativeProcessProtocol::GetSoftwareBreakpointTrapOpcode(size_t size_hint) { 5200b57cec5SDimitry Andric static const uint8_t g_aarch64_opcode[] = {0x00, 0x00, 0x20, 0xd4}; 5210b57cec5SDimitry Andric static const uint8_t g_i386_opcode[] = {0xCC}; 5220b57cec5SDimitry Andric static const uint8_t g_mips64_opcode[] = {0x00, 0x00, 0x00, 0x0d}; 5230b57cec5SDimitry Andric static const uint8_t g_mips64el_opcode[] = {0x0d, 0x00, 0x00, 0x00}; 5240b57cec5SDimitry Andric static const uint8_t g_s390x_opcode[] = {0x00, 0x01}; 525d409305fSDimitry Andric static const uint8_t g_ppc_opcode[] = {0x7f, 0xe0, 0x00, 0x08}; // trap 526d409305fSDimitry Andric static const uint8_t g_ppcle_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 548d409305fSDimitry Andric case llvm::Triple::ppc: 549d409305fSDimitry Andric case llvm::Triple::ppc64: 550d409305fSDimitry Andric return llvm::makeArrayRef(g_ppc_opcode); 551d409305fSDimitry Andric 5520b57cec5SDimitry Andric case llvm::Triple::ppc64le: 553d409305fSDimitry Andric return llvm::makeArrayRef(g_ppcle_opcode); 5540b57cec5SDimitry Andric 5550b57cec5SDimitry Andric default: 5560b57cec5SDimitry Andric return llvm::createStringError(llvm::inconvertibleErrorCode(), 5570b57cec5SDimitry Andric "CPU type not supported!"); 5580b57cec5SDimitry Andric } 5590b57cec5SDimitry Andric } 5600b57cec5SDimitry Andric 5610b57cec5SDimitry Andric size_t NativeProcessProtocol::GetSoftwareBreakpointPCOffset() { 5620b57cec5SDimitry Andric switch (GetArchitecture().GetMachine()) { 5630b57cec5SDimitry Andric case llvm::Triple::x86: 5640b57cec5SDimitry Andric case llvm::Triple::x86_64: 5650b57cec5SDimitry Andric case llvm::Triple::systemz: 5660b57cec5SDimitry Andric // These architectures report increment the PC after breakpoint is hit. 5670b57cec5SDimitry Andric return cantFail(GetSoftwareBreakpointTrapOpcode(0)).size(); 5680b57cec5SDimitry Andric 5690b57cec5SDimitry Andric case llvm::Triple::arm: 5700b57cec5SDimitry Andric case llvm::Triple::aarch64: 5719dba64beSDimitry Andric case llvm::Triple::aarch64_32: 5720b57cec5SDimitry Andric case llvm::Triple::mips64: 5730b57cec5SDimitry Andric case llvm::Triple::mips64el: 5740b57cec5SDimitry Andric case llvm::Triple::mips: 5750b57cec5SDimitry Andric case llvm::Triple::mipsel: 576d409305fSDimitry Andric case llvm::Triple::ppc: 577d409305fSDimitry Andric case llvm::Triple::ppc64: 5780b57cec5SDimitry Andric case llvm::Triple::ppc64le: 5790b57cec5SDimitry Andric // On these architectures the PC doesn't get updated for breakpoint hits. 5800b57cec5SDimitry Andric return 0; 5810b57cec5SDimitry Andric 5820b57cec5SDimitry Andric default: 5830b57cec5SDimitry Andric llvm_unreachable("CPU type not supported!"); 5840b57cec5SDimitry Andric } 5850b57cec5SDimitry Andric } 5860b57cec5SDimitry Andric 5870b57cec5SDimitry Andric void NativeProcessProtocol::FixupBreakpointPCAsNeeded( 5880b57cec5SDimitry Andric NativeThreadProtocol &thread) { 5890b57cec5SDimitry Andric Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_BREAKPOINTS); 5900b57cec5SDimitry Andric 5910b57cec5SDimitry Andric Status error; 5920b57cec5SDimitry Andric 5930b57cec5SDimitry Andric // Find out the size of a breakpoint (might depend on where we are in the 5940b57cec5SDimitry Andric // code). 5950b57cec5SDimitry Andric NativeRegisterContext &context = thread.GetRegisterContext(); 5960b57cec5SDimitry Andric 5970b57cec5SDimitry Andric uint32_t breakpoint_size = GetSoftwareBreakpointPCOffset(); 5980b57cec5SDimitry Andric LLDB_LOG(log, "breakpoint size: {0}", breakpoint_size); 5990b57cec5SDimitry Andric if (breakpoint_size == 0) 6000b57cec5SDimitry Andric return; 6010b57cec5SDimitry Andric 6020b57cec5SDimitry Andric // First try probing for a breakpoint at a software breakpoint location: PC - 6030b57cec5SDimitry Andric // breakpoint size. 6040b57cec5SDimitry Andric const lldb::addr_t initial_pc_addr = context.GetPCfromBreakpointLocation(); 6050b57cec5SDimitry Andric lldb::addr_t breakpoint_addr = initial_pc_addr; 6060b57cec5SDimitry Andric // Do not allow breakpoint probe to wrap around. 6070b57cec5SDimitry Andric if (breakpoint_addr >= breakpoint_size) 6080b57cec5SDimitry Andric breakpoint_addr -= breakpoint_size; 6090b57cec5SDimitry Andric 6100b57cec5SDimitry Andric if (m_software_breakpoints.count(breakpoint_addr) == 0) { 6110b57cec5SDimitry Andric // We didn't find one at a software probe location. Nothing to do. 6120b57cec5SDimitry Andric LLDB_LOG(log, 6130b57cec5SDimitry Andric "pid {0} no lldb software breakpoint found at current pc with " 6140b57cec5SDimitry Andric "adjustment: {1}", 6150b57cec5SDimitry Andric GetID(), breakpoint_addr); 6160b57cec5SDimitry Andric return; 6170b57cec5SDimitry Andric } 6180b57cec5SDimitry Andric 6190b57cec5SDimitry Andric // 6200b57cec5SDimitry Andric // We have a software breakpoint and need to adjust the PC. 6210b57cec5SDimitry Andric // 6220b57cec5SDimitry Andric 6230b57cec5SDimitry Andric // Change the program counter. 6240b57cec5SDimitry Andric LLDB_LOG(log, "pid {0} tid {1}: changing PC from {2:x} to {3:x}", GetID(), 6250b57cec5SDimitry Andric thread.GetID(), initial_pc_addr, breakpoint_addr); 6260b57cec5SDimitry Andric 6270b57cec5SDimitry Andric error = context.SetPC(breakpoint_addr); 6280b57cec5SDimitry Andric if (error.Fail()) { 6290b57cec5SDimitry Andric // This can happen in case the process was killed between the time we read 6300b57cec5SDimitry Andric // the PC and when we are updating it. There's nothing better to do than to 6310b57cec5SDimitry Andric // swallow the error. 6320b57cec5SDimitry Andric LLDB_LOG(log, "pid {0} tid {1}: failed to set PC: {2}", GetID(), 6330b57cec5SDimitry Andric thread.GetID(), error); 6340b57cec5SDimitry Andric } 6350b57cec5SDimitry Andric } 6360b57cec5SDimitry Andric 6370b57cec5SDimitry Andric Status NativeProcessProtocol::RemoveBreakpoint(lldb::addr_t addr, 6380b57cec5SDimitry Andric bool hardware) { 6390b57cec5SDimitry Andric if (hardware) 6400b57cec5SDimitry Andric return RemoveHardwareBreakpoint(addr); 6410b57cec5SDimitry Andric else 6420b57cec5SDimitry Andric return RemoveSoftwareBreakpoint(addr); 6430b57cec5SDimitry Andric } 6440b57cec5SDimitry Andric 6450b57cec5SDimitry Andric Status NativeProcessProtocol::ReadMemoryWithoutTrap(lldb::addr_t addr, 6460b57cec5SDimitry Andric void *buf, size_t size, 6470b57cec5SDimitry Andric size_t &bytes_read) { 6480b57cec5SDimitry Andric Status error = ReadMemory(addr, buf, size, bytes_read); 6490b57cec5SDimitry Andric if (error.Fail()) 6500b57cec5SDimitry Andric return error; 6510b57cec5SDimitry Andric 6520b57cec5SDimitry Andric auto data = 6530b57cec5SDimitry Andric llvm::makeMutableArrayRef(static_cast<uint8_t *>(buf), bytes_read); 6540b57cec5SDimitry Andric for (const auto &pair : m_software_breakpoints) { 6550b57cec5SDimitry Andric lldb::addr_t bp_addr = pair.first; 6560b57cec5SDimitry Andric auto saved_opcodes = makeArrayRef(pair.second.saved_opcodes); 6570b57cec5SDimitry Andric 6580b57cec5SDimitry Andric if (bp_addr + saved_opcodes.size() < addr || addr + bytes_read <= bp_addr) 6595ffd83dbSDimitry Andric continue; // Breakpoint not in range, ignore 6600b57cec5SDimitry Andric 6610b57cec5SDimitry Andric if (bp_addr < addr) { 6620b57cec5SDimitry Andric saved_opcodes = saved_opcodes.drop_front(addr - bp_addr); 6630b57cec5SDimitry Andric bp_addr = addr; 6640b57cec5SDimitry Andric } 6650b57cec5SDimitry Andric auto bp_data = data.drop_front(bp_addr - addr); 6660b57cec5SDimitry Andric std::copy_n(saved_opcodes.begin(), 6670b57cec5SDimitry Andric std::min(saved_opcodes.size(), bp_data.size()), 6680b57cec5SDimitry Andric bp_data.begin()); 6690b57cec5SDimitry Andric } 6700b57cec5SDimitry Andric return Status(); 6710b57cec5SDimitry Andric } 6720b57cec5SDimitry Andric 6739dba64beSDimitry Andric llvm::Expected<llvm::StringRef> 6749dba64beSDimitry Andric NativeProcessProtocol::ReadCStringFromMemory(lldb::addr_t addr, char *buffer, 6759dba64beSDimitry Andric size_t max_size, 6769dba64beSDimitry Andric size_t &total_bytes_read) { 6779dba64beSDimitry Andric static const size_t cache_line_size = 6789dba64beSDimitry Andric llvm::sys::Process::getPageSizeEstimate(); 6799dba64beSDimitry Andric size_t bytes_read = 0; 6809dba64beSDimitry Andric size_t bytes_left = max_size; 6819dba64beSDimitry Andric addr_t curr_addr = addr; 6829dba64beSDimitry Andric size_t string_size; 6839dba64beSDimitry Andric char *curr_buffer = buffer; 6849dba64beSDimitry Andric total_bytes_read = 0; 6859dba64beSDimitry Andric Status status; 6869dba64beSDimitry Andric 6879dba64beSDimitry Andric while (bytes_left > 0 && status.Success()) { 6889dba64beSDimitry Andric addr_t cache_line_bytes_left = 6899dba64beSDimitry Andric cache_line_size - (curr_addr % cache_line_size); 6909dba64beSDimitry Andric addr_t bytes_to_read = std::min<addr_t>(bytes_left, cache_line_bytes_left); 691480093f4SDimitry Andric status = ReadMemory(curr_addr, static_cast<void *>(curr_buffer), 6929dba64beSDimitry Andric bytes_to_read, bytes_read); 6939dba64beSDimitry Andric 6949dba64beSDimitry Andric if (bytes_read == 0) 6959dba64beSDimitry Andric break; 6969dba64beSDimitry Andric 6979dba64beSDimitry Andric void *str_end = std::memchr(curr_buffer, '\0', bytes_read); 6989dba64beSDimitry Andric if (str_end != nullptr) { 6999dba64beSDimitry Andric total_bytes_read = 700480093f4SDimitry Andric static_cast<size_t>((static_cast<char *>(str_end) - buffer + 1)); 7019dba64beSDimitry Andric status.Clear(); 7029dba64beSDimitry Andric break; 7039dba64beSDimitry Andric } 7049dba64beSDimitry Andric 7059dba64beSDimitry Andric total_bytes_read += bytes_read; 7069dba64beSDimitry Andric curr_buffer += bytes_read; 7079dba64beSDimitry Andric curr_addr += bytes_read; 7089dba64beSDimitry Andric bytes_left -= bytes_read; 7099dba64beSDimitry Andric } 7109dba64beSDimitry Andric 7119dba64beSDimitry Andric string_size = total_bytes_read - 1; 7129dba64beSDimitry Andric 7139dba64beSDimitry Andric // Make sure we return a null terminated string. 7149dba64beSDimitry Andric if (bytes_left == 0 && max_size > 0 && buffer[max_size - 1] != '\0') { 7159dba64beSDimitry Andric buffer[max_size - 1] = '\0'; 7169dba64beSDimitry Andric total_bytes_read--; 7179dba64beSDimitry Andric } 7189dba64beSDimitry Andric 7199dba64beSDimitry Andric if (!status.Success()) 7209dba64beSDimitry Andric return status.ToError(); 7219dba64beSDimitry Andric 7229dba64beSDimitry Andric return llvm::StringRef(buffer, string_size); 7239dba64beSDimitry Andric } 7249dba64beSDimitry Andric 7250b57cec5SDimitry Andric lldb::StateType NativeProcessProtocol::GetState() const { 7260b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_state_mutex); 7270b57cec5SDimitry Andric return m_state; 7280b57cec5SDimitry Andric } 7290b57cec5SDimitry Andric 7300b57cec5SDimitry Andric void NativeProcessProtocol::SetState(lldb::StateType state, 7310b57cec5SDimitry Andric bool notify_delegates) { 7320b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_state_mutex); 7330b57cec5SDimitry Andric 7340b57cec5SDimitry Andric if (state == m_state) 7350b57cec5SDimitry Andric return; 7360b57cec5SDimitry Andric 7370b57cec5SDimitry Andric m_state = state; 7380b57cec5SDimitry Andric 7390b57cec5SDimitry Andric if (StateIsStoppedState(state, false)) { 7400b57cec5SDimitry Andric ++m_stop_id; 7410b57cec5SDimitry Andric 7420b57cec5SDimitry Andric // Give process a chance to do any stop id bump processing, such as 7430b57cec5SDimitry Andric // clearing cached data that is invalidated each time the process runs. 7440b57cec5SDimitry Andric // Note if/when we support some threads running, we'll end up needing to 7450b57cec5SDimitry Andric // manage this per thread and per process. 7460b57cec5SDimitry Andric DoStopIDBumped(m_stop_id); 7470b57cec5SDimitry Andric } 7480b57cec5SDimitry Andric 7490b57cec5SDimitry Andric // Optionally notify delegates of the state change. 7500b57cec5SDimitry Andric if (notify_delegates) 7510b57cec5SDimitry Andric SynchronouslyNotifyProcessStateChanged(state); 7520b57cec5SDimitry Andric } 7530b57cec5SDimitry Andric 7540b57cec5SDimitry Andric uint32_t NativeProcessProtocol::GetStopID() const { 7550b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_state_mutex); 7560b57cec5SDimitry Andric return m_stop_id; 7570b57cec5SDimitry Andric } 7580b57cec5SDimitry Andric 7590b57cec5SDimitry Andric void NativeProcessProtocol::DoStopIDBumped(uint32_t /* newBumpId */) { 7600b57cec5SDimitry Andric // Default implementation does nothing. 7610b57cec5SDimitry Andric } 7620b57cec5SDimitry Andric 7630b57cec5SDimitry Andric NativeProcessProtocol::Factory::~Factory() = default; 764