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" 1581ad6265SDimitry Andric #include "lldb/Utility/LLDBLog.h" 160b57cec5SDimitry Andric #include "lldb/Utility/Log.h" 170b57cec5SDimitry Andric #include "lldb/Utility/State.h" 180b57cec5SDimitry Andric #include "lldb/lldb-enumerations.h" 190b57cec5SDimitry Andric 209dba64beSDimitry Andric #include "llvm/Support/Process.h" 21*bdd1243dSDimitry Andric #include <optional> 229dba64beSDimitry Andric 230b57cec5SDimitry Andric using namespace lldb; 240b57cec5SDimitry Andric using namespace lldb_private; 250b57cec5SDimitry Andric 260b57cec5SDimitry Andric // NativeProcessProtocol Members 270b57cec5SDimitry Andric 280b57cec5SDimitry Andric NativeProcessProtocol::NativeProcessProtocol(lldb::pid_t pid, int terminal_fd, 290b57cec5SDimitry Andric NativeDelegate &delegate) 30fe6060f1SDimitry Andric : m_pid(pid), m_delegate(delegate), m_terminal_fd(terminal_fd) { 31fe6060f1SDimitry Andric delegate.InitializeDelegate(this); 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 57fe6060f1SDimitry Andric lldb_private::Status 58fe6060f1SDimitry Andric NativeProcessProtocol::ReadMemoryTags(int32_t type, lldb::addr_t addr, 59fe6060f1SDimitry Andric size_t len, std::vector<uint8_t> &tags) { 60fe6060f1SDimitry Andric return Status("not implemented"); 61fe6060f1SDimitry Andric } 62fe6060f1SDimitry Andric 63fe6060f1SDimitry Andric lldb_private::Status 64fe6060f1SDimitry Andric NativeProcessProtocol::WriteMemoryTags(int32_t type, lldb::addr_t addr, 65fe6060f1SDimitry Andric size_t len, 66fe6060f1SDimitry Andric const std::vector<uint8_t> &tags) { 67fe6060f1SDimitry Andric return Status("not implemented"); 68fe6060f1SDimitry Andric } 69fe6060f1SDimitry Andric 70*bdd1243dSDimitry Andric std::optional<WaitStatus> NativeProcessProtocol::GetExitStatus() { 710b57cec5SDimitry Andric if (m_state == lldb::eStateExited) 720b57cec5SDimitry Andric return m_exit_status; 730b57cec5SDimitry Andric 74*bdd1243dSDimitry Andric return std::nullopt; 750b57cec5SDimitry Andric } 760b57cec5SDimitry Andric 770b57cec5SDimitry Andric bool NativeProcessProtocol::SetExitStatus(WaitStatus status, 780b57cec5SDimitry Andric bool bNotifyStateChange) { 7981ad6265SDimitry Andric Log *log = GetLog(LLDBLog::Process); 800b57cec5SDimitry Andric LLDB_LOG(log, "status = {0}, notify = {1}", status, bNotifyStateChange); 810b57cec5SDimitry Andric 820b57cec5SDimitry Andric // Exit status already set 830b57cec5SDimitry Andric if (m_state == lldb::eStateExited) { 840b57cec5SDimitry Andric if (m_exit_status) 850b57cec5SDimitry Andric LLDB_LOG(log, "exit status already set to {0}", *m_exit_status); 860b57cec5SDimitry Andric else 870b57cec5SDimitry Andric LLDB_LOG(log, "state is exited, but status not set"); 880b57cec5SDimitry Andric return false; 890b57cec5SDimitry Andric } 900b57cec5SDimitry Andric 910b57cec5SDimitry Andric m_state = lldb::eStateExited; 920b57cec5SDimitry Andric m_exit_status = status; 930b57cec5SDimitry Andric 940b57cec5SDimitry Andric if (bNotifyStateChange) 950b57cec5SDimitry Andric SynchronouslyNotifyProcessStateChanged(lldb::eStateExited); 960b57cec5SDimitry Andric 970b57cec5SDimitry Andric return true; 980b57cec5SDimitry Andric } 990b57cec5SDimitry Andric 1000b57cec5SDimitry Andric NativeThreadProtocol *NativeProcessProtocol::GetThreadAtIndex(uint32_t idx) { 1010b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_threads_mutex); 1020b57cec5SDimitry Andric if (idx < m_threads.size()) 1030b57cec5SDimitry Andric return m_threads[idx].get(); 1040b57cec5SDimitry Andric return nullptr; 1050b57cec5SDimitry Andric } 1060b57cec5SDimitry Andric 1070b57cec5SDimitry Andric NativeThreadProtocol * 1080b57cec5SDimitry Andric NativeProcessProtocol::GetThreadByIDUnlocked(lldb::tid_t tid) { 1090b57cec5SDimitry Andric for (const auto &thread : m_threads) { 1100b57cec5SDimitry Andric if (thread->GetID() == tid) 1110b57cec5SDimitry Andric return thread.get(); 1120b57cec5SDimitry Andric } 1130b57cec5SDimitry Andric return nullptr; 1140b57cec5SDimitry Andric } 1150b57cec5SDimitry Andric 1160b57cec5SDimitry Andric NativeThreadProtocol *NativeProcessProtocol::GetThreadByID(lldb::tid_t tid) { 1170b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_threads_mutex); 1180b57cec5SDimitry Andric return GetThreadByIDUnlocked(tid); 1190b57cec5SDimitry Andric } 1200b57cec5SDimitry Andric 1210b57cec5SDimitry Andric bool NativeProcessProtocol::IsAlive() const { 1220b57cec5SDimitry Andric return m_state != eStateDetached && m_state != eStateExited && 1230b57cec5SDimitry Andric m_state != eStateInvalid && m_state != eStateUnloaded; 1240b57cec5SDimitry Andric } 1250b57cec5SDimitry Andric 1260b57cec5SDimitry Andric const NativeWatchpointList::WatchpointMap & 1270b57cec5SDimitry Andric NativeProcessProtocol::GetWatchpointMap() const { 1280b57cec5SDimitry Andric return m_watchpoint_list.GetWatchpointMap(); 1290b57cec5SDimitry Andric } 1300b57cec5SDimitry Andric 131*bdd1243dSDimitry Andric std::optional<std::pair<uint32_t, uint32_t>> 1320b57cec5SDimitry Andric NativeProcessProtocol::GetHardwareDebugSupportInfo() const { 13381ad6265SDimitry Andric Log *log = GetLog(LLDBLog::Process); 1340b57cec5SDimitry Andric 1350b57cec5SDimitry Andric // get any thread 1360b57cec5SDimitry Andric NativeThreadProtocol *thread( 1370b57cec5SDimitry Andric const_cast<NativeProcessProtocol *>(this)->GetThreadAtIndex(0)); 1380b57cec5SDimitry Andric if (!thread) { 1390b57cec5SDimitry Andric LLDB_LOG(log, "failed to find a thread to grab a NativeRegisterContext!"); 140*bdd1243dSDimitry Andric return std::nullopt; 1410b57cec5SDimitry Andric } 1420b57cec5SDimitry Andric 1430b57cec5SDimitry Andric NativeRegisterContext ®_ctx = thread->GetRegisterContext(); 1440b57cec5SDimitry Andric return std::make_pair(reg_ctx.NumSupportedHardwareBreakpoints(), 1450b57cec5SDimitry Andric reg_ctx.NumSupportedHardwareWatchpoints()); 1460b57cec5SDimitry Andric } 1470b57cec5SDimitry Andric 1480b57cec5SDimitry Andric Status NativeProcessProtocol::SetWatchpoint(lldb::addr_t addr, size_t size, 1490b57cec5SDimitry Andric uint32_t watch_flags, 1500b57cec5SDimitry Andric bool hardware) { 1510b57cec5SDimitry Andric // This default implementation assumes setting the watchpoint for the process 1520b57cec5SDimitry Andric // will require setting the watchpoint for each of the threads. Furthermore, 1530b57cec5SDimitry Andric // it will track watchpoints set for the process and will add them to each 1540b57cec5SDimitry Andric // thread that is attached to via the (FIXME implement) OnThreadAttached () 1550b57cec5SDimitry Andric // method. 1560b57cec5SDimitry Andric 15781ad6265SDimitry Andric Log *log = GetLog(LLDBLog::Process); 1580b57cec5SDimitry Andric 1590b57cec5SDimitry Andric // Update the thread list 1600b57cec5SDimitry Andric UpdateThreads(); 1610b57cec5SDimitry Andric 1620b57cec5SDimitry Andric // Keep track of the threads we successfully set the watchpoint for. If one 1630b57cec5SDimitry Andric // of the thread watchpoint setting operations fails, back off and remove the 1640b57cec5SDimitry Andric // watchpoint for all the threads that were successfully set so we get back 1650b57cec5SDimitry Andric // to a consistent state. 1660b57cec5SDimitry Andric std::vector<NativeThreadProtocol *> watchpoint_established_threads; 1670b57cec5SDimitry Andric 1680b57cec5SDimitry Andric // Tell each thread to set a watchpoint. In the event that hardware 1690b57cec5SDimitry Andric // watchpoints are requested but the SetWatchpoint fails, try to set a 1700b57cec5SDimitry Andric // software watchpoint as a fallback. It's conceivable that if there are 1710b57cec5SDimitry Andric // more threads than hardware watchpoints available, some of the threads will 1720b57cec5SDimitry Andric // fail to set hardware watchpoints while software ones may be available. 1730b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_threads_mutex); 1740b57cec5SDimitry Andric for (const auto &thread : m_threads) { 1750b57cec5SDimitry Andric assert(thread && "thread list should not have a NULL thread!"); 1760b57cec5SDimitry Andric 1770b57cec5SDimitry Andric Status thread_error = 1780b57cec5SDimitry Andric thread->SetWatchpoint(addr, size, watch_flags, hardware); 1790b57cec5SDimitry Andric if (thread_error.Fail() && hardware) { 1800b57cec5SDimitry Andric // Try software watchpoints since we failed on hardware watchpoint 1810b57cec5SDimitry Andric // setting and we may have just run out of hardware watchpoints. 1820b57cec5SDimitry Andric thread_error = thread->SetWatchpoint(addr, size, watch_flags, false); 1830b57cec5SDimitry Andric if (thread_error.Success()) 1840b57cec5SDimitry Andric LLDB_LOG(log, 1850b57cec5SDimitry Andric "hardware watchpoint requested but software watchpoint set"); 1860b57cec5SDimitry Andric } 1870b57cec5SDimitry Andric 1880b57cec5SDimitry Andric if (thread_error.Success()) { 1890b57cec5SDimitry Andric // Remember that we set this watchpoint successfully in case we need to 1900b57cec5SDimitry Andric // clear it later. 1910b57cec5SDimitry Andric watchpoint_established_threads.push_back(thread.get()); 1920b57cec5SDimitry Andric } else { 1930b57cec5SDimitry Andric // Unset the watchpoint for each thread we successfully set so that we 1940b57cec5SDimitry Andric // get back to a consistent state of "not set" for the watchpoint. 1950b57cec5SDimitry Andric for (auto unwatch_thread_sp : watchpoint_established_threads) { 1960b57cec5SDimitry Andric Status remove_error = unwatch_thread_sp->RemoveWatchpoint(addr); 1970b57cec5SDimitry Andric if (remove_error.Fail()) 1980b57cec5SDimitry Andric LLDB_LOG(log, "RemoveWatchpoint failed for pid={0}, tid={1}: {2}", 1990b57cec5SDimitry Andric GetID(), unwatch_thread_sp->GetID(), remove_error); 2000b57cec5SDimitry Andric } 2010b57cec5SDimitry Andric 2020b57cec5SDimitry Andric return thread_error; 2030b57cec5SDimitry Andric } 2040b57cec5SDimitry Andric } 2050b57cec5SDimitry Andric return m_watchpoint_list.Add(addr, size, watch_flags, hardware); 2060b57cec5SDimitry Andric } 2070b57cec5SDimitry Andric 2080b57cec5SDimitry Andric Status NativeProcessProtocol::RemoveWatchpoint(lldb::addr_t addr) { 2090b57cec5SDimitry Andric // Update the thread list 2100b57cec5SDimitry Andric UpdateThreads(); 2110b57cec5SDimitry Andric 2120b57cec5SDimitry Andric Status overall_error; 2130b57cec5SDimitry Andric 2140b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_threads_mutex); 2150b57cec5SDimitry Andric for (const auto &thread : m_threads) { 2160b57cec5SDimitry Andric assert(thread && "thread list should not have a NULL thread!"); 2170b57cec5SDimitry Andric 2180b57cec5SDimitry Andric const Status thread_error = thread->RemoveWatchpoint(addr); 2190b57cec5SDimitry Andric if (thread_error.Fail()) { 2200b57cec5SDimitry Andric // Keep track of the first thread error if any threads fail. We want to 2210b57cec5SDimitry Andric // try to remove the watchpoint from every thread, though, even if one or 2220b57cec5SDimitry Andric // more have errors. 2230b57cec5SDimitry Andric if (!overall_error.Fail()) 2240b57cec5SDimitry Andric overall_error = thread_error; 2250b57cec5SDimitry Andric } 2260b57cec5SDimitry Andric } 2270b57cec5SDimitry Andric const Status error = m_watchpoint_list.Remove(addr); 2280b57cec5SDimitry Andric return overall_error.Fail() ? overall_error : error; 2290b57cec5SDimitry Andric } 2300b57cec5SDimitry Andric 2310b57cec5SDimitry Andric const HardwareBreakpointMap & 2320b57cec5SDimitry Andric NativeProcessProtocol::GetHardwareBreakpointMap() const { 2330b57cec5SDimitry Andric return m_hw_breakpoints_map; 2340b57cec5SDimitry Andric } 2350b57cec5SDimitry Andric 2360b57cec5SDimitry Andric Status NativeProcessProtocol::SetHardwareBreakpoint(lldb::addr_t addr, 2370b57cec5SDimitry Andric size_t size) { 2380b57cec5SDimitry Andric // This default implementation assumes setting a hardware breakpoint for this 2390b57cec5SDimitry Andric // process will require setting same hardware breakpoint for each of its 2400b57cec5SDimitry Andric // existing threads. New thread will do the same once created. 24181ad6265SDimitry Andric Log *log = GetLog(LLDBLog::Process); 2420b57cec5SDimitry Andric 2430b57cec5SDimitry Andric // Update the thread list 2440b57cec5SDimitry Andric UpdateThreads(); 2450b57cec5SDimitry Andric 2460b57cec5SDimitry Andric // Exit here if target does not have required hardware breakpoint capability. 2470b57cec5SDimitry Andric auto hw_debug_cap = GetHardwareDebugSupportInfo(); 2480b57cec5SDimitry Andric 249*bdd1243dSDimitry Andric if (hw_debug_cap == std::nullopt || hw_debug_cap->first == 0 || 2500b57cec5SDimitry Andric hw_debug_cap->first <= m_hw_breakpoints_map.size()) 2510b57cec5SDimitry Andric return Status("Target does not have required no of hardware breakpoints"); 2520b57cec5SDimitry Andric 2530b57cec5SDimitry Andric // Vector below stores all thread pointer for which we have we successfully 2540b57cec5SDimitry Andric // set this hardware breakpoint. If any of the current process threads fails 2550b57cec5SDimitry Andric // to set this hardware breakpoint then roll back and remove this breakpoint 2560b57cec5SDimitry Andric // for all the threads that had already set it successfully. 2570b57cec5SDimitry Andric std::vector<NativeThreadProtocol *> breakpoint_established_threads; 2580b57cec5SDimitry Andric 2590b57cec5SDimitry Andric // Request to set a hardware breakpoint for each of current process threads. 2600b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_threads_mutex); 2610b57cec5SDimitry Andric for (const auto &thread : m_threads) { 2620b57cec5SDimitry Andric assert(thread && "thread list should not have a NULL thread!"); 2630b57cec5SDimitry Andric 2640b57cec5SDimitry Andric Status thread_error = thread->SetHardwareBreakpoint(addr, size); 2650b57cec5SDimitry Andric if (thread_error.Success()) { 2660b57cec5SDimitry Andric // Remember that we set this breakpoint successfully in case we need to 2670b57cec5SDimitry Andric // clear it later. 2680b57cec5SDimitry Andric breakpoint_established_threads.push_back(thread.get()); 2690b57cec5SDimitry Andric } else { 2700b57cec5SDimitry Andric // Unset the breakpoint for each thread we successfully set so that we 2710b57cec5SDimitry Andric // get back to a consistent state of "not set" for this hardware 2720b57cec5SDimitry Andric // breakpoint. 2730b57cec5SDimitry Andric for (auto rollback_thread_sp : breakpoint_established_threads) { 2740b57cec5SDimitry Andric Status remove_error = 2750b57cec5SDimitry Andric rollback_thread_sp->RemoveHardwareBreakpoint(addr); 2760b57cec5SDimitry Andric if (remove_error.Fail()) 2770b57cec5SDimitry Andric LLDB_LOG(log, 2780b57cec5SDimitry Andric "RemoveHardwareBreakpoint failed for pid={0}, tid={1}: {2}", 2790b57cec5SDimitry Andric GetID(), rollback_thread_sp->GetID(), remove_error); 2800b57cec5SDimitry Andric } 2810b57cec5SDimitry Andric 2820b57cec5SDimitry Andric return thread_error; 2830b57cec5SDimitry Andric } 2840b57cec5SDimitry Andric } 2850b57cec5SDimitry Andric 2860b57cec5SDimitry Andric // Register new hardware breakpoint into hardware breakpoints map of current 2870b57cec5SDimitry Andric // process. 2880b57cec5SDimitry Andric m_hw_breakpoints_map[addr] = {addr, size}; 2890b57cec5SDimitry Andric 2900b57cec5SDimitry Andric return Status(); 2910b57cec5SDimitry Andric } 2920b57cec5SDimitry Andric 2930b57cec5SDimitry Andric Status NativeProcessProtocol::RemoveHardwareBreakpoint(lldb::addr_t addr) { 2940b57cec5SDimitry Andric // Update the thread list 2950b57cec5SDimitry Andric UpdateThreads(); 2960b57cec5SDimitry Andric 2970b57cec5SDimitry Andric Status error; 2980b57cec5SDimitry Andric 2990b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_threads_mutex); 3000b57cec5SDimitry Andric for (const auto &thread : m_threads) { 3010b57cec5SDimitry Andric assert(thread && "thread list should not have a NULL thread!"); 3020b57cec5SDimitry Andric error = thread->RemoveHardwareBreakpoint(addr); 3030b57cec5SDimitry Andric } 3040b57cec5SDimitry Andric 3050b57cec5SDimitry Andric // Also remove from hardware breakpoint map of current process. 3060b57cec5SDimitry Andric m_hw_breakpoints_map.erase(addr); 3070b57cec5SDimitry Andric 3080b57cec5SDimitry Andric return error; 3090b57cec5SDimitry Andric } 3100b57cec5SDimitry Andric 3110b57cec5SDimitry Andric void NativeProcessProtocol::SynchronouslyNotifyProcessStateChanged( 3120b57cec5SDimitry Andric lldb::StateType state) { 31381ad6265SDimitry Andric Log *log = GetLog(LLDBLog::Process); 3140b57cec5SDimitry Andric 315fe6060f1SDimitry Andric m_delegate.ProcessStateChanged(this, state); 3160b57cec5SDimitry Andric 31781ad6265SDimitry Andric switch (state) { 31881ad6265SDimitry Andric case eStateStopped: 31981ad6265SDimitry Andric case eStateExited: 32081ad6265SDimitry Andric case eStateCrashed: 32181ad6265SDimitry Andric NotifyTracersProcessDidStop(); 32281ad6265SDimitry Andric break; 32381ad6265SDimitry Andric default: 32481ad6265SDimitry Andric break; 32581ad6265SDimitry Andric } 32681ad6265SDimitry Andric 327fe6060f1SDimitry Andric LLDB_LOG(log, "sent state notification [{0}] from process {1}", state, 328fe6060f1SDimitry Andric GetID()); 3290b57cec5SDimitry Andric } 3300b57cec5SDimitry Andric 3310b57cec5SDimitry Andric void NativeProcessProtocol::NotifyDidExec() { 33281ad6265SDimitry Andric Log *log = GetLog(LLDBLog::Process); 333fe6060f1SDimitry Andric LLDB_LOG(log, "process {0} exec()ed", GetID()); 3340b57cec5SDimitry Andric 33581ad6265SDimitry Andric m_software_breakpoints.clear(); 33681ad6265SDimitry Andric 337fe6060f1SDimitry Andric m_delegate.DidExec(this); 3380b57cec5SDimitry Andric } 3390b57cec5SDimitry Andric 3400b57cec5SDimitry Andric Status NativeProcessProtocol::SetSoftwareBreakpoint(lldb::addr_t addr, 3410b57cec5SDimitry Andric uint32_t size_hint) { 34281ad6265SDimitry Andric Log *log = GetLog(LLDBLog::Breakpoints); 3430b57cec5SDimitry Andric LLDB_LOG(log, "addr = {0:x}, size_hint = {1}", addr, size_hint); 3440b57cec5SDimitry Andric 3450b57cec5SDimitry Andric auto it = m_software_breakpoints.find(addr); 3460b57cec5SDimitry Andric if (it != m_software_breakpoints.end()) { 3470b57cec5SDimitry Andric ++it->second.ref_count; 3480b57cec5SDimitry Andric return Status(); 3490b57cec5SDimitry Andric } 3500b57cec5SDimitry Andric auto expected_bkpt = EnableSoftwareBreakpoint(addr, size_hint); 3510b57cec5SDimitry Andric if (!expected_bkpt) 3520b57cec5SDimitry Andric return Status(expected_bkpt.takeError()); 3530b57cec5SDimitry Andric 3540b57cec5SDimitry Andric m_software_breakpoints.emplace(addr, std::move(*expected_bkpt)); 3550b57cec5SDimitry Andric return Status(); 3560b57cec5SDimitry Andric } 3570b57cec5SDimitry Andric 3580b57cec5SDimitry Andric Status NativeProcessProtocol::RemoveSoftwareBreakpoint(lldb::addr_t addr) { 35981ad6265SDimitry Andric Log *log = GetLog(LLDBLog::Breakpoints); 3600b57cec5SDimitry Andric LLDB_LOG(log, "addr = {0:x}", addr); 3610b57cec5SDimitry Andric auto it = m_software_breakpoints.find(addr); 3620b57cec5SDimitry Andric if (it == m_software_breakpoints.end()) 3630b57cec5SDimitry Andric return Status("Breakpoint not found."); 3640b57cec5SDimitry Andric assert(it->second.ref_count > 0); 3650b57cec5SDimitry Andric if (--it->second.ref_count > 0) 3660b57cec5SDimitry Andric return Status(); 3670b57cec5SDimitry Andric 3680b57cec5SDimitry Andric // This is the last reference. Let's remove the breakpoint. 3690b57cec5SDimitry Andric Status error; 3700b57cec5SDimitry Andric 3710b57cec5SDimitry Andric // Clear a software breakpoint instruction 3720b57cec5SDimitry Andric llvm::SmallVector<uint8_t, 4> curr_break_op( 3730b57cec5SDimitry Andric it->second.breakpoint_opcodes.size(), 0); 3740b57cec5SDimitry Andric 3750b57cec5SDimitry Andric // Read the breakpoint opcode 3760b57cec5SDimitry Andric size_t bytes_read = 0; 3770b57cec5SDimitry Andric error = 3780b57cec5SDimitry Andric ReadMemory(addr, curr_break_op.data(), curr_break_op.size(), bytes_read); 3790b57cec5SDimitry Andric if (error.Fail() || bytes_read < curr_break_op.size()) { 3800b57cec5SDimitry Andric return Status("addr=0x%" PRIx64 3810b57cec5SDimitry Andric ": tried to read %zu bytes but only read %zu", 3820b57cec5SDimitry Andric addr, curr_break_op.size(), bytes_read); 3830b57cec5SDimitry Andric } 3840b57cec5SDimitry Andric const auto &saved = it->second.saved_opcodes; 3850b57cec5SDimitry Andric // Make sure the breakpoint opcode exists at this address 386*bdd1243dSDimitry Andric if (llvm::ArrayRef(curr_break_op) != it->second.breakpoint_opcodes) { 3870b57cec5SDimitry Andric if (curr_break_op != it->second.saved_opcodes) 3880b57cec5SDimitry Andric return Status("Original breakpoint trap is no longer in memory."); 3890b57cec5SDimitry Andric LLDB_LOG(log, 3900b57cec5SDimitry Andric "Saved opcodes ({0:@[x]}) have already been restored at {1:x}.", 3910b57cec5SDimitry Andric llvm::make_range(saved.begin(), saved.end()), addr); 3920b57cec5SDimitry Andric } else { 3930b57cec5SDimitry Andric // We found a valid breakpoint opcode at this address, now restore the 3940b57cec5SDimitry Andric // saved opcode. 3950b57cec5SDimitry Andric size_t bytes_written = 0; 3960b57cec5SDimitry Andric error = WriteMemory(addr, saved.data(), saved.size(), bytes_written); 3970b57cec5SDimitry Andric if (error.Fail() || bytes_written < saved.size()) { 3980b57cec5SDimitry Andric return Status("addr=0x%" PRIx64 3990b57cec5SDimitry Andric ": tried to write %zu bytes but only wrote %zu", 4000b57cec5SDimitry Andric addr, saved.size(), bytes_written); 4010b57cec5SDimitry Andric } 4020b57cec5SDimitry Andric 4030b57cec5SDimitry Andric // Verify that our original opcode made it back to the inferior 4040b57cec5SDimitry Andric llvm::SmallVector<uint8_t, 4> verify_opcode(saved.size(), 0); 4050b57cec5SDimitry Andric size_t verify_bytes_read = 0; 4060b57cec5SDimitry Andric error = ReadMemory(addr, verify_opcode.data(), verify_opcode.size(), 4070b57cec5SDimitry Andric verify_bytes_read); 4080b57cec5SDimitry Andric if (error.Fail() || verify_bytes_read < verify_opcode.size()) { 4090b57cec5SDimitry Andric return Status("addr=0x%" PRIx64 4100b57cec5SDimitry Andric ": tried to read %zu verification bytes but only read %zu", 4110b57cec5SDimitry Andric addr, verify_opcode.size(), verify_bytes_read); 4120b57cec5SDimitry Andric } 4130b57cec5SDimitry Andric if (verify_opcode != saved) 4140b57cec5SDimitry Andric LLDB_LOG(log, "Restoring bytes at {0:x}: {1:@[x]}", addr, 4150b57cec5SDimitry Andric llvm::make_range(saved.begin(), saved.end())); 4160b57cec5SDimitry Andric } 4170b57cec5SDimitry Andric 4180b57cec5SDimitry Andric m_software_breakpoints.erase(it); 4190b57cec5SDimitry Andric return Status(); 4200b57cec5SDimitry Andric } 4210b57cec5SDimitry Andric 4220b57cec5SDimitry Andric llvm::Expected<NativeProcessProtocol::SoftwareBreakpoint> 4230b57cec5SDimitry Andric NativeProcessProtocol::EnableSoftwareBreakpoint(lldb::addr_t addr, 4240b57cec5SDimitry Andric uint32_t size_hint) { 42581ad6265SDimitry Andric Log *log = GetLog(LLDBLog::Breakpoints); 4260b57cec5SDimitry Andric 4270b57cec5SDimitry Andric auto expected_trap = GetSoftwareBreakpointTrapOpcode(size_hint); 4280b57cec5SDimitry Andric if (!expected_trap) 4290b57cec5SDimitry Andric return expected_trap.takeError(); 4300b57cec5SDimitry Andric 4310b57cec5SDimitry Andric llvm::SmallVector<uint8_t, 4> saved_opcode_bytes(expected_trap->size(), 0); 4320b57cec5SDimitry Andric // Save the original opcodes by reading them so we can restore later. 4330b57cec5SDimitry Andric size_t bytes_read = 0; 4340b57cec5SDimitry Andric Status error = ReadMemory(addr, saved_opcode_bytes.data(), 4350b57cec5SDimitry Andric saved_opcode_bytes.size(), bytes_read); 4360b57cec5SDimitry Andric if (error.Fail()) 4370b57cec5SDimitry Andric return error.ToError(); 4380b57cec5SDimitry Andric 4390b57cec5SDimitry Andric // Ensure we read as many bytes as we expected. 4400b57cec5SDimitry Andric if (bytes_read != saved_opcode_bytes.size()) { 4410b57cec5SDimitry Andric return llvm::createStringError( 4420b57cec5SDimitry Andric llvm::inconvertibleErrorCode(), 4430b57cec5SDimitry Andric "Failed to read memory while attempting to set breakpoint: attempted " 4440b57cec5SDimitry Andric "to read {0} bytes but only read {1}.", 4450b57cec5SDimitry Andric saved_opcode_bytes.size(), bytes_read); 4460b57cec5SDimitry Andric } 4470b57cec5SDimitry Andric 4480b57cec5SDimitry Andric LLDB_LOG( 4490b57cec5SDimitry Andric log, "Overwriting bytes at {0:x}: {1:@[x]}", addr, 4500b57cec5SDimitry Andric llvm::make_range(saved_opcode_bytes.begin(), saved_opcode_bytes.end())); 4510b57cec5SDimitry Andric 4520b57cec5SDimitry Andric // Write a software breakpoint in place of the original opcode. 4530b57cec5SDimitry Andric size_t bytes_written = 0; 4540b57cec5SDimitry Andric error = WriteMemory(addr, expected_trap->data(), expected_trap->size(), 4550b57cec5SDimitry Andric bytes_written); 4560b57cec5SDimitry Andric if (error.Fail()) 4570b57cec5SDimitry Andric return error.ToError(); 4580b57cec5SDimitry Andric 4590b57cec5SDimitry Andric // Ensure we wrote as many bytes as we expected. 4600b57cec5SDimitry Andric if (bytes_written != expected_trap->size()) { 4610b57cec5SDimitry Andric return llvm::createStringError( 4620b57cec5SDimitry Andric llvm::inconvertibleErrorCode(), 4630b57cec5SDimitry Andric "Failed write memory while attempting to set " 4640b57cec5SDimitry Andric "breakpoint: attempted to write {0} bytes but only wrote {1}", 4650b57cec5SDimitry Andric expected_trap->size(), bytes_written); 4660b57cec5SDimitry Andric } 4670b57cec5SDimitry Andric 4680b57cec5SDimitry Andric llvm::SmallVector<uint8_t, 4> verify_bp_opcode_bytes(expected_trap->size(), 4690b57cec5SDimitry Andric 0); 4700b57cec5SDimitry Andric size_t verify_bytes_read = 0; 4710b57cec5SDimitry Andric error = ReadMemory(addr, verify_bp_opcode_bytes.data(), 4720b57cec5SDimitry Andric verify_bp_opcode_bytes.size(), verify_bytes_read); 4730b57cec5SDimitry Andric if (error.Fail()) 4740b57cec5SDimitry Andric return error.ToError(); 4750b57cec5SDimitry Andric 4760b57cec5SDimitry Andric // Ensure we read as many verification bytes as we expected. 4770b57cec5SDimitry Andric if (verify_bytes_read != verify_bp_opcode_bytes.size()) { 4780b57cec5SDimitry Andric return llvm::createStringError( 4790b57cec5SDimitry Andric llvm::inconvertibleErrorCode(), 4800b57cec5SDimitry Andric "Failed to read memory while " 4810b57cec5SDimitry Andric "attempting to verify breakpoint: attempted to read {0} bytes " 4820b57cec5SDimitry Andric "but only read {1}", 4830b57cec5SDimitry Andric verify_bp_opcode_bytes.size(), verify_bytes_read); 4840b57cec5SDimitry Andric } 4850b57cec5SDimitry Andric 486*bdd1243dSDimitry Andric if (llvm::ArrayRef(verify_bp_opcode_bytes.data(), verify_bytes_read) != 4870b57cec5SDimitry Andric *expected_trap) { 4880b57cec5SDimitry Andric return llvm::createStringError( 4890b57cec5SDimitry Andric llvm::inconvertibleErrorCode(), 4900b57cec5SDimitry Andric "Verification of software breakpoint " 4910b57cec5SDimitry Andric "writing failed - trap opcodes not successfully read back " 4920b57cec5SDimitry Andric "after writing when setting breakpoint at {0:x}", 4930b57cec5SDimitry Andric addr); 4940b57cec5SDimitry Andric } 4950b57cec5SDimitry Andric 4960b57cec5SDimitry Andric LLDB_LOG(log, "addr = {0:x}: SUCCESS", addr); 4970b57cec5SDimitry Andric return SoftwareBreakpoint{1, saved_opcode_bytes, *expected_trap}; 4980b57cec5SDimitry Andric } 4990b57cec5SDimitry Andric 5000b57cec5SDimitry Andric llvm::Expected<llvm::ArrayRef<uint8_t>> 5010b57cec5SDimitry Andric NativeProcessProtocol::GetSoftwareBreakpointTrapOpcode(size_t size_hint) { 5020b57cec5SDimitry Andric static const uint8_t g_aarch64_opcode[] = {0x00, 0x00, 0x20, 0xd4}; 5030b57cec5SDimitry Andric static const uint8_t g_i386_opcode[] = {0xCC}; 5040b57cec5SDimitry Andric static const uint8_t g_mips64_opcode[] = {0x00, 0x00, 0x00, 0x0d}; 5050b57cec5SDimitry Andric static const uint8_t g_mips64el_opcode[] = {0x0d, 0x00, 0x00, 0x00}; 5060b57cec5SDimitry Andric static const uint8_t g_s390x_opcode[] = {0x00, 0x01}; 507d409305fSDimitry Andric static const uint8_t g_ppc_opcode[] = {0x7f, 0xe0, 0x00, 0x08}; // trap 508d409305fSDimitry Andric static const uint8_t g_ppcle_opcode[] = {0x08, 0x00, 0xe0, 0x7f}; // trap 509*bdd1243dSDimitry Andric static const uint8_t g_riscv_opcode[] = {0x73, 0x00, 0x10, 0x00}; // ebreak 510*bdd1243dSDimitry Andric static const uint8_t g_riscv_opcode_c[] = {0x02, 0x90}; // c.ebreak 511*bdd1243dSDimitry Andric static const uint8_t g_loongarch_opcode[] = {0x05, 0x00, 0x2a, 512*bdd1243dSDimitry Andric 0x00}; // break 0x5 5130b57cec5SDimitry Andric 5140b57cec5SDimitry Andric switch (GetArchitecture().GetMachine()) { 5150b57cec5SDimitry Andric case llvm::Triple::aarch64: 5169dba64beSDimitry Andric case llvm::Triple::aarch64_32: 517*bdd1243dSDimitry Andric return llvm::ArrayRef(g_aarch64_opcode); 5180b57cec5SDimitry Andric 5190b57cec5SDimitry Andric case llvm::Triple::x86: 5200b57cec5SDimitry Andric case llvm::Triple::x86_64: 521*bdd1243dSDimitry Andric return llvm::ArrayRef(g_i386_opcode); 5220b57cec5SDimitry Andric 5230b57cec5SDimitry Andric case llvm::Triple::mips: 5240b57cec5SDimitry Andric case llvm::Triple::mips64: 525*bdd1243dSDimitry Andric return llvm::ArrayRef(g_mips64_opcode); 5260b57cec5SDimitry Andric 5270b57cec5SDimitry Andric case llvm::Triple::mipsel: 5280b57cec5SDimitry Andric case llvm::Triple::mips64el: 529*bdd1243dSDimitry Andric return llvm::ArrayRef(g_mips64el_opcode); 5300b57cec5SDimitry Andric 5310b57cec5SDimitry Andric case llvm::Triple::systemz: 532*bdd1243dSDimitry Andric return llvm::ArrayRef(g_s390x_opcode); 5330b57cec5SDimitry Andric 534d409305fSDimitry Andric case llvm::Triple::ppc: 535d409305fSDimitry Andric case llvm::Triple::ppc64: 536*bdd1243dSDimitry Andric return llvm::ArrayRef(g_ppc_opcode); 537d409305fSDimitry Andric 5380b57cec5SDimitry Andric case llvm::Triple::ppc64le: 539*bdd1243dSDimitry Andric return llvm::ArrayRef(g_ppcle_opcode); 540*bdd1243dSDimitry Andric 541*bdd1243dSDimitry Andric case llvm::Triple::riscv32: 542*bdd1243dSDimitry Andric case llvm::Triple::riscv64: { 543*bdd1243dSDimitry Andric return size_hint == 2 ? llvm::ArrayRef(g_riscv_opcode_c) 544*bdd1243dSDimitry Andric : llvm::ArrayRef(g_riscv_opcode); 545*bdd1243dSDimitry Andric } 546*bdd1243dSDimitry Andric 547*bdd1243dSDimitry Andric case llvm::Triple::loongarch32: 548*bdd1243dSDimitry Andric case llvm::Triple::loongarch64: 549*bdd1243dSDimitry Andric return llvm::ArrayRef(g_loongarch_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: 572d409305fSDimitry Andric case llvm::Triple::ppc: 573d409305fSDimitry Andric case llvm::Triple::ppc64: 5740b57cec5SDimitry Andric case llvm::Triple::ppc64le: 575*bdd1243dSDimitry Andric case llvm::Triple::riscv32: 576*bdd1243dSDimitry Andric case llvm::Triple::riscv64: 577*bdd1243dSDimitry Andric case llvm::Triple::loongarch32: 578*bdd1243dSDimitry Andric case llvm::Triple::loongarch64: 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) { 58981ad6265SDimitry Andric Log *log = GetLog(LLDBLog::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 652*bdd1243dSDimitry Andric llvm::MutableArrayRef data(static_cast<uint8_t *>(buf), bytes_read); 6530b57cec5SDimitry Andric for (const auto &pair : m_software_breakpoints) { 6540b57cec5SDimitry Andric lldb::addr_t bp_addr = pair.first; 655*bdd1243dSDimitry Andric auto saved_opcodes = llvm::ArrayRef(pair.second.saved_opcodes); 6560b57cec5SDimitry Andric 6570b57cec5SDimitry Andric if (bp_addr + saved_opcodes.size() < addr || addr + bytes_read <= bp_addr) 6585ffd83dbSDimitry Andric continue; // Breakpoint not in range, ignore 6590b57cec5SDimitry Andric 6600b57cec5SDimitry Andric if (bp_addr < addr) { 6610b57cec5SDimitry Andric saved_opcodes = saved_opcodes.drop_front(addr - bp_addr); 6620b57cec5SDimitry Andric bp_addr = addr; 6630b57cec5SDimitry Andric } 6640b57cec5SDimitry Andric auto bp_data = data.drop_front(bp_addr - addr); 6650b57cec5SDimitry Andric std::copy_n(saved_opcodes.begin(), 6660b57cec5SDimitry Andric std::min(saved_opcodes.size(), bp_data.size()), 6670b57cec5SDimitry Andric bp_data.begin()); 6680b57cec5SDimitry Andric } 6690b57cec5SDimitry Andric return Status(); 6700b57cec5SDimitry Andric } 6710b57cec5SDimitry Andric 6729dba64beSDimitry Andric llvm::Expected<llvm::StringRef> 6739dba64beSDimitry Andric NativeProcessProtocol::ReadCStringFromMemory(lldb::addr_t addr, char *buffer, 6749dba64beSDimitry Andric size_t max_size, 6759dba64beSDimitry Andric size_t &total_bytes_read) { 6769dba64beSDimitry Andric static const size_t cache_line_size = 6779dba64beSDimitry Andric llvm::sys::Process::getPageSizeEstimate(); 6789dba64beSDimitry Andric size_t bytes_read = 0; 6799dba64beSDimitry Andric size_t bytes_left = max_size; 6809dba64beSDimitry Andric addr_t curr_addr = addr; 6819dba64beSDimitry Andric size_t string_size; 6829dba64beSDimitry Andric char *curr_buffer = buffer; 6839dba64beSDimitry Andric total_bytes_read = 0; 6849dba64beSDimitry Andric Status status; 6859dba64beSDimitry Andric 6869dba64beSDimitry Andric while (bytes_left > 0 && status.Success()) { 6879dba64beSDimitry Andric addr_t cache_line_bytes_left = 6889dba64beSDimitry Andric cache_line_size - (curr_addr % cache_line_size); 6899dba64beSDimitry Andric addr_t bytes_to_read = std::min<addr_t>(bytes_left, cache_line_bytes_left); 690480093f4SDimitry Andric status = ReadMemory(curr_addr, static_cast<void *>(curr_buffer), 6919dba64beSDimitry Andric bytes_to_read, bytes_read); 6929dba64beSDimitry Andric 6939dba64beSDimitry Andric if (bytes_read == 0) 6949dba64beSDimitry Andric break; 6959dba64beSDimitry Andric 6969dba64beSDimitry Andric void *str_end = std::memchr(curr_buffer, '\0', bytes_read); 6979dba64beSDimitry Andric if (str_end != nullptr) { 6989dba64beSDimitry Andric total_bytes_read = 699480093f4SDimitry Andric static_cast<size_t>((static_cast<char *>(str_end) - buffer + 1)); 7009dba64beSDimitry Andric status.Clear(); 7019dba64beSDimitry Andric break; 7029dba64beSDimitry Andric } 7039dba64beSDimitry Andric 7049dba64beSDimitry Andric total_bytes_read += bytes_read; 7059dba64beSDimitry Andric curr_buffer += bytes_read; 7069dba64beSDimitry Andric curr_addr += bytes_read; 7079dba64beSDimitry Andric bytes_left -= bytes_read; 7089dba64beSDimitry Andric } 7099dba64beSDimitry Andric 7109dba64beSDimitry Andric string_size = total_bytes_read - 1; 7119dba64beSDimitry Andric 7129dba64beSDimitry Andric // Make sure we return a null terminated string. 7139dba64beSDimitry Andric if (bytes_left == 0 && max_size > 0 && buffer[max_size - 1] != '\0') { 7149dba64beSDimitry Andric buffer[max_size - 1] = '\0'; 7159dba64beSDimitry Andric total_bytes_read--; 7169dba64beSDimitry Andric } 7179dba64beSDimitry Andric 7189dba64beSDimitry Andric if (!status.Success()) 7199dba64beSDimitry Andric return status.ToError(); 7209dba64beSDimitry Andric 7219dba64beSDimitry Andric return llvm::StringRef(buffer, string_size); 7229dba64beSDimitry Andric } 7239dba64beSDimitry Andric 7240b57cec5SDimitry Andric lldb::StateType NativeProcessProtocol::GetState() const { 7250b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_state_mutex); 7260b57cec5SDimitry Andric return m_state; 7270b57cec5SDimitry Andric } 7280b57cec5SDimitry Andric 7290b57cec5SDimitry Andric void NativeProcessProtocol::SetState(lldb::StateType state, 7300b57cec5SDimitry Andric bool notify_delegates) { 7310b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_state_mutex); 7320b57cec5SDimitry Andric 7330b57cec5SDimitry Andric if (state == m_state) 7340b57cec5SDimitry Andric return; 7350b57cec5SDimitry Andric 7360b57cec5SDimitry Andric m_state = state; 7370b57cec5SDimitry Andric 7380b57cec5SDimitry Andric if (StateIsStoppedState(state, false)) { 7390b57cec5SDimitry Andric ++m_stop_id; 7400b57cec5SDimitry Andric 7410b57cec5SDimitry Andric // Give process a chance to do any stop id bump processing, such as 7420b57cec5SDimitry Andric // clearing cached data that is invalidated each time the process runs. 7430b57cec5SDimitry Andric // Note if/when we support some threads running, we'll end up needing to 7440b57cec5SDimitry Andric // manage this per thread and per process. 7450b57cec5SDimitry Andric DoStopIDBumped(m_stop_id); 7460b57cec5SDimitry Andric } 7470b57cec5SDimitry Andric 7480b57cec5SDimitry Andric // Optionally notify delegates of the state change. 7490b57cec5SDimitry Andric if (notify_delegates) 7500b57cec5SDimitry Andric SynchronouslyNotifyProcessStateChanged(state); 7510b57cec5SDimitry Andric } 7520b57cec5SDimitry Andric 7530b57cec5SDimitry Andric uint32_t NativeProcessProtocol::GetStopID() const { 7540b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_state_mutex); 7550b57cec5SDimitry Andric return m_stop_id; 7560b57cec5SDimitry Andric } 7570b57cec5SDimitry Andric 7580b57cec5SDimitry Andric void NativeProcessProtocol::DoStopIDBumped(uint32_t /* newBumpId */) { 7590b57cec5SDimitry Andric // Default implementation does nothing. 7600b57cec5SDimitry Andric } 7610b57cec5SDimitry Andric 7620b57cec5SDimitry Andric NativeProcessProtocol::Factory::~Factory() = default; 763