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