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