10127ef0fSEd Maste //===-- NativeProcessProtocol.cpp -------------------------------*- C++ -*-===//
20127ef0fSEd Maste //
30127ef0fSEd Maste //                     The LLVM Compiler Infrastructure
40127ef0fSEd Maste //
50127ef0fSEd Maste // This file is distributed under the University of Illinois Open Source
60127ef0fSEd Maste // License. See LICENSE.TXT for details.
70127ef0fSEd Maste //
80127ef0fSEd Maste //===----------------------------------------------------------------------===//
90127ef0fSEd Maste 
101c3bbb01SEd Maste #include "lldb/Host/common/NativeProcessProtocol.h"
117aa51b79SEd Maste #include "lldb/Host/Host.h"
12*b5893f02SDimitry Andric #include "lldb/Host/common/NativeBreakpointList.h"
131c3bbb01SEd Maste #include "lldb/Host/common/NativeRegisterContext.h"
141c3bbb01SEd Maste #include "lldb/Host/common/NativeThreadProtocol.h"
15435933ddSDimitry Andric #include "lldb/Utility/LLDBAssert.h"
16f678e45dSDimitry Andric #include "lldb/Utility/Log.h"
17*b5893f02SDimitry Andric #include "lldb/Utility/State.h"
18435933ddSDimitry Andric #include "lldb/lldb-enumerations.h"
190127ef0fSEd Maste 
200127ef0fSEd Maste using namespace lldb;
210127ef0fSEd Maste using namespace lldb_private;
220127ef0fSEd Maste 
230127ef0fSEd Maste // -----------------------------------------------------------------------------
240127ef0fSEd Maste // NativeProcessProtocol Members
250127ef0fSEd Maste // -----------------------------------------------------------------------------
260127ef0fSEd Maste 
NativeProcessProtocol(lldb::pid_t pid,int terminal_fd,NativeDelegate & delegate)27c4394386SDimitry Andric NativeProcessProtocol::NativeProcessProtocol(lldb::pid_t pid, int terminal_fd,
28c4394386SDimitry Andric                                              NativeDelegate &delegate)
29c4394386SDimitry Andric     : m_pid(pid), m_terminal_fd(terminal_fd) {
30c4394386SDimitry Andric   bool registered = RegisterNativeDelegate(delegate);
31c4394386SDimitry Andric   assert(registered);
32c4394386SDimitry Andric   (void)registered;
33c4394386SDimitry Andric }
340127ef0fSEd Maste 
Interrupt()355517e702SDimitry Andric lldb_private::Status NativeProcessProtocol::Interrupt() {
365517e702SDimitry Andric   Status error;
377aa51b79SEd Maste #if !defined(SIGSTOP)
387aa51b79SEd Maste   error.SetErrorString("local host does not support signaling");
397aa51b79SEd Maste   return error;
407aa51b79SEd Maste #else
417aa51b79SEd Maste   return Signal(SIGSTOP);
427aa51b79SEd Maste #endif
437aa51b79SEd Maste }
447aa51b79SEd Maste 
IgnoreSignals(llvm::ArrayRef<int> signals)455517e702SDimitry Andric Status NativeProcessProtocol::IgnoreSignals(llvm::ArrayRef<int> signals) {
46f678e45dSDimitry Andric   m_signals_to_ignore.clear();
47f678e45dSDimitry Andric   m_signals_to_ignore.insert(signals.begin(), signals.end());
485517e702SDimitry Andric   return Status();
49f678e45dSDimitry Andric }
50f678e45dSDimitry Andric 
515517e702SDimitry Andric lldb_private::Status
GetMemoryRegionInfo(lldb::addr_t load_addr,MemoryRegionInfo & range_info)52435933ddSDimitry Andric NativeProcessProtocol::GetMemoryRegionInfo(lldb::addr_t load_addr,
53435933ddSDimitry Andric                                            MemoryRegionInfo &range_info) {
540127ef0fSEd Maste   // Default: not implemented.
555517e702SDimitry Andric   return Status("not implemented");
560127ef0fSEd Maste }
570127ef0fSEd Maste 
GetExitStatus()58edd7eaddSDimitry Andric llvm::Optional<WaitStatus> NativeProcessProtocol::GetExitStatus() {
59edd7eaddSDimitry Andric   if (m_state == lldb::eStateExited)
60edd7eaddSDimitry Andric     return m_exit_status;
61edd7eaddSDimitry Andric 
62edd7eaddSDimitry Andric   return llvm::None;
630127ef0fSEd Maste }
640127ef0fSEd Maste 
SetExitStatus(WaitStatus status,bool bNotifyStateChange)65edd7eaddSDimitry Andric bool NativeProcessProtocol::SetExitStatus(WaitStatus status,
66435933ddSDimitry Andric                                           bool bNotifyStateChange) {
670127ef0fSEd Maste   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
68edd7eaddSDimitry Andric   LLDB_LOG(log, "status = {0}, notify = {1}", status, bNotifyStateChange);
690127ef0fSEd Maste 
700127ef0fSEd Maste   // Exit status already set
71435933ddSDimitry Andric   if (m_state == lldb::eStateExited) {
72edd7eaddSDimitry Andric     if (m_exit_status)
73edd7eaddSDimitry Andric       LLDB_LOG(log, "exit status already set to {0}", *m_exit_status);
74edd7eaddSDimitry Andric     else
75edd7eaddSDimitry Andric       LLDB_LOG(log, "state is exited, but status not set");
760127ef0fSEd Maste     return false;
770127ef0fSEd Maste   }
780127ef0fSEd Maste 
790127ef0fSEd Maste   m_state = lldb::eStateExited;
800127ef0fSEd Maste   m_exit_status = status;
810127ef0fSEd Maste 
820127ef0fSEd Maste   if (bNotifyStateChange)
830127ef0fSEd Maste     SynchronouslyNotifyProcessStateChanged(lldb::eStateExited);
840127ef0fSEd Maste 
850127ef0fSEd Maste   return true;
860127ef0fSEd Maste }
870127ef0fSEd Maste 
GetThreadAtIndex(uint32_t idx)88acac075bSDimitry Andric NativeThreadProtocol *NativeProcessProtocol::GetThreadAtIndex(uint32_t idx) {
894bb0738eSEd Maste   std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
900127ef0fSEd Maste   if (idx < m_threads.size())
91acac075bSDimitry Andric     return m_threads[idx].get();
92acac075bSDimitry Andric   return nullptr;
930127ef0fSEd Maste }
940127ef0fSEd Maste 
95acac075bSDimitry Andric NativeThreadProtocol *
GetThreadByIDUnlocked(lldb::tid_t tid)96435933ddSDimitry Andric NativeProcessProtocol::GetThreadByIDUnlocked(lldb::tid_t tid) {
97acac075bSDimitry Andric   for (const auto &thread : m_threads) {
98acac075bSDimitry Andric     if (thread->GetID() == tid)
99acac075bSDimitry Andric       return thread.get();
1000127ef0fSEd Maste   }
101acac075bSDimitry Andric   return nullptr;
1020127ef0fSEd Maste }
1030127ef0fSEd Maste 
GetThreadByID(lldb::tid_t tid)104acac075bSDimitry Andric NativeThreadProtocol *NativeProcessProtocol::GetThreadByID(lldb::tid_t tid) {
1054bb0738eSEd Maste   std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
1067aa51b79SEd Maste   return GetThreadByIDUnlocked(tid);
1077aa51b79SEd Maste }
1087aa51b79SEd Maste 
IsAlive() const109435933ddSDimitry Andric bool NativeProcessProtocol::IsAlive() const {
110435933ddSDimitry Andric   return m_state != eStateDetached && m_state != eStateExited &&
111435933ddSDimitry Andric          m_state != eStateInvalid && m_state != eStateUnloaded;
1120127ef0fSEd Maste }
1130127ef0fSEd Maste 
1141c3bbb01SEd Maste const NativeWatchpointList::WatchpointMap &
GetWatchpointMap() const115435933ddSDimitry Andric NativeProcessProtocol::GetWatchpointMap() const {
1161c3bbb01SEd Maste   return m_watchpoint_list.GetWatchpointMap();
1171c3bbb01SEd Maste }
1181c3bbb01SEd Maste 
119f678e45dSDimitry Andric llvm::Optional<std::pair<uint32_t, uint32_t>>
GetHardwareDebugSupportInfo() const120f678e45dSDimitry Andric NativeProcessProtocol::GetHardwareDebugSupportInfo() const {
1210127ef0fSEd Maste   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
1220127ef0fSEd Maste 
1230127ef0fSEd Maste   // get any thread
124acac075bSDimitry Andric   NativeThreadProtocol *thread(
125435933ddSDimitry Andric       const_cast<NativeProcessProtocol *>(this)->GetThreadAtIndex(0));
126acac075bSDimitry Andric   if (!thread) {
127acac075bSDimitry Andric     LLDB_LOG(log, "failed to find a thread to grab a NativeRegisterContext!");
128f678e45dSDimitry Andric     return llvm::None;
1290127ef0fSEd Maste   }
1300127ef0fSEd Maste 
131acac075bSDimitry Andric   NativeRegisterContext &reg_ctx = thread->GetRegisterContext();
132acac075bSDimitry Andric   return std::make_pair(reg_ctx.NumSupportedHardwareBreakpoints(),
133acac075bSDimitry Andric                         reg_ctx.NumSupportedHardwareWatchpoints());
1340127ef0fSEd Maste }
1350127ef0fSEd Maste 
SetWatchpoint(lldb::addr_t addr,size_t size,uint32_t watch_flags,bool hardware)1365517e702SDimitry Andric Status NativeProcessProtocol::SetWatchpoint(lldb::addr_t addr, size_t size,
137435933ddSDimitry Andric                                             uint32_t watch_flags,
138435933ddSDimitry Andric                                             bool hardware) {
1394ba319b5SDimitry Andric   // This default implementation assumes setting the watchpoint for the process
1404ba319b5SDimitry Andric   // will require setting the watchpoint for each of the threads.  Furthermore,
1414ba319b5SDimitry Andric   // it will track watchpoints set for the process and will add them to each
1424ba319b5SDimitry Andric   // thread that is attached to via the (FIXME implement) OnThreadAttached ()
1434ba319b5SDimitry Andric   // method.
1440127ef0fSEd Maste 
1450127ef0fSEd Maste   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
1460127ef0fSEd Maste 
1470127ef0fSEd Maste   // Update the thread list
1480127ef0fSEd Maste   UpdateThreads();
1490127ef0fSEd Maste 
1504ba319b5SDimitry Andric   // Keep track of the threads we successfully set the watchpoint for.  If one
1514ba319b5SDimitry Andric   // of the thread watchpoint setting operations fails, back off and remove the
1524ba319b5SDimitry Andric   // watchpoint for all the threads that were successfully set so we get back
1534ba319b5SDimitry Andric   // to a consistent state.
154acac075bSDimitry Andric   std::vector<NativeThreadProtocol *> watchpoint_established_threads;
1550127ef0fSEd Maste 
1564ba319b5SDimitry Andric   // Tell each thread to set a watchpoint.  In the event that hardware
1574ba319b5SDimitry Andric   // watchpoints are requested but the SetWatchpoint fails, try to set a
1584ba319b5SDimitry Andric   // software watchpoint as a fallback.  It's conceivable that if there are
1594ba319b5SDimitry Andric   // more threads than hardware watchpoints available, some of the threads will
1604ba319b5SDimitry Andric   // fail to set hardware watchpoints while software ones may be available.
1614bb0738eSEd Maste   std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
162acac075bSDimitry Andric   for (const auto &thread : m_threads) {
163acac075bSDimitry Andric     assert(thread && "thread list should not have a NULL thread!");
1640127ef0fSEd Maste 
1655517e702SDimitry Andric     Status thread_error =
166acac075bSDimitry Andric         thread->SetWatchpoint(addr, size, watch_flags, hardware);
167435933ddSDimitry Andric     if (thread_error.Fail() && hardware) {
1684ba319b5SDimitry Andric       // Try software watchpoints since we failed on hardware watchpoint
1694ba319b5SDimitry Andric       // setting and we may have just run out of hardware watchpoints.
170acac075bSDimitry Andric       thread_error = thread->SetWatchpoint(addr, size, watch_flags, false);
171acac075bSDimitry Andric       if (thread_error.Success())
172acac075bSDimitry Andric         LLDB_LOG(log,
173435933ddSDimitry Andric                  "hardware watchpoint requested but software watchpoint set");
1740127ef0fSEd Maste     }
1750127ef0fSEd Maste 
176435933ddSDimitry Andric     if (thread_error.Success()) {
1774ba319b5SDimitry Andric       // Remember that we set this watchpoint successfully in case we need to
1784ba319b5SDimitry Andric       // clear it later.
179acac075bSDimitry Andric       watchpoint_established_threads.push_back(thread.get());
180435933ddSDimitry Andric     } else {
1814ba319b5SDimitry Andric       // Unset the watchpoint for each thread we successfully set so that we
1824ba319b5SDimitry Andric       // get back to a consistent state of "not set" for the watchpoint.
183435933ddSDimitry Andric       for (auto unwatch_thread_sp : watchpoint_established_threads) {
1845517e702SDimitry Andric         Status remove_error = unwatch_thread_sp->RemoveWatchpoint(addr);
185acac075bSDimitry Andric         if (remove_error.Fail())
186acac075bSDimitry Andric           LLDB_LOG(log, "RemoveWatchpoint failed for pid={0}, tid={1}: {2}",
187acac075bSDimitry Andric                    GetID(), unwatch_thread_sp->GetID(), remove_error);
1880127ef0fSEd Maste       }
1890127ef0fSEd Maste 
1900127ef0fSEd Maste       return thread_error;
1910127ef0fSEd Maste     }
1920127ef0fSEd Maste   }
1931c3bbb01SEd Maste   return m_watchpoint_list.Add(addr, size, watch_flags, hardware);
1940127ef0fSEd Maste }
1950127ef0fSEd Maste 
RemoveWatchpoint(lldb::addr_t addr)1965517e702SDimitry Andric Status NativeProcessProtocol::RemoveWatchpoint(lldb::addr_t addr) {
1970127ef0fSEd Maste   // Update the thread list
1980127ef0fSEd Maste   UpdateThreads();
1990127ef0fSEd Maste 
2005517e702SDimitry Andric   Status overall_error;
2010127ef0fSEd Maste 
2024bb0738eSEd Maste   std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
203acac075bSDimitry Andric   for (const auto &thread : m_threads) {
204acac075bSDimitry Andric     assert(thread && "thread list should not have a NULL thread!");
2050127ef0fSEd Maste 
206acac075bSDimitry Andric     const Status thread_error = thread->RemoveWatchpoint(addr);
207435933ddSDimitry Andric     if (thread_error.Fail()) {
2084ba319b5SDimitry Andric       // Keep track of the first thread error if any threads fail. We want to
2094ba319b5SDimitry Andric       // try to remove the watchpoint from every thread, though, even if one or
2104ba319b5SDimitry Andric       // more have errors.
2110127ef0fSEd Maste       if (!overall_error.Fail())
2120127ef0fSEd Maste         overall_error = thread_error;
2130127ef0fSEd Maste     }
2140127ef0fSEd Maste   }
2155517e702SDimitry Andric   const Status error = m_watchpoint_list.Remove(addr);
2161c3bbb01SEd Maste   return overall_error.Fail() ? overall_error : error;
2170127ef0fSEd Maste }
2180127ef0fSEd Maste 
219f678e45dSDimitry Andric const HardwareBreakpointMap &
GetHardwareBreakpointMap() const220f678e45dSDimitry Andric NativeProcessProtocol::GetHardwareBreakpointMap() const {
221f678e45dSDimitry Andric   return m_hw_breakpoints_map;
222f678e45dSDimitry Andric }
223f678e45dSDimitry Andric 
SetHardwareBreakpoint(lldb::addr_t addr,size_t size)2245517e702SDimitry Andric Status NativeProcessProtocol::SetHardwareBreakpoint(lldb::addr_t addr,
225f678e45dSDimitry Andric                                                     size_t size) {
2264ba319b5SDimitry Andric   // This default implementation assumes setting a hardware breakpoint for this
2274ba319b5SDimitry Andric   // process will require setting same hardware breakpoint for each of its
2284ba319b5SDimitry Andric   // existing threads. New thread will do the same once created.
229f678e45dSDimitry Andric   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
230f678e45dSDimitry Andric 
231f678e45dSDimitry Andric   // Update the thread list
232f678e45dSDimitry Andric   UpdateThreads();
233f678e45dSDimitry Andric 
234f678e45dSDimitry Andric   // Exit here if target does not have required hardware breakpoint capability.
235f678e45dSDimitry Andric   auto hw_debug_cap = GetHardwareDebugSupportInfo();
236f678e45dSDimitry Andric 
237f678e45dSDimitry Andric   if (hw_debug_cap == llvm::None || hw_debug_cap->first == 0 ||
238f678e45dSDimitry Andric       hw_debug_cap->first <= m_hw_breakpoints_map.size())
2395517e702SDimitry Andric     return Status("Target does not have required no of hardware breakpoints");
240f678e45dSDimitry Andric 
241f678e45dSDimitry Andric   // Vector below stores all thread pointer for which we have we successfully
242f678e45dSDimitry Andric   // set this hardware breakpoint. If any of the current process threads fails
243f678e45dSDimitry Andric   // to set this hardware breakpoint then roll back and remove this breakpoint
244f678e45dSDimitry Andric   // for all the threads that had already set it successfully.
245acac075bSDimitry Andric   std::vector<NativeThreadProtocol *> breakpoint_established_threads;
246f678e45dSDimitry Andric 
247f678e45dSDimitry Andric   // Request to set a hardware breakpoint for each of current process threads.
248f678e45dSDimitry Andric   std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
249acac075bSDimitry Andric   for (const auto &thread : m_threads) {
250acac075bSDimitry Andric     assert(thread && "thread list should not have a NULL thread!");
251f678e45dSDimitry Andric 
252acac075bSDimitry Andric     Status thread_error = thread->SetHardwareBreakpoint(addr, size);
253f678e45dSDimitry Andric     if (thread_error.Success()) {
2544ba319b5SDimitry Andric       // Remember that we set this breakpoint successfully in case we need to
2554ba319b5SDimitry Andric       // clear it later.
256acac075bSDimitry Andric       breakpoint_established_threads.push_back(thread.get());
257f678e45dSDimitry Andric     } else {
2584ba319b5SDimitry Andric       // Unset the breakpoint for each thread we successfully set so that we
2594ba319b5SDimitry Andric       // get back to a consistent state of "not set" for this hardware
2604ba319b5SDimitry Andric       // breakpoint.
261f678e45dSDimitry Andric       for (auto rollback_thread_sp : breakpoint_established_threads) {
2625517e702SDimitry Andric         Status remove_error =
2635517e702SDimitry Andric             rollback_thread_sp->RemoveHardwareBreakpoint(addr);
264acac075bSDimitry Andric         if (remove_error.Fail())
265acac075bSDimitry Andric           LLDB_LOG(log,
266acac075bSDimitry Andric                    "RemoveHardwareBreakpoint failed for pid={0}, tid={1}: {2}",
267acac075bSDimitry Andric                    GetID(), rollback_thread_sp->GetID(), remove_error);
268f678e45dSDimitry Andric       }
269f678e45dSDimitry Andric 
270f678e45dSDimitry Andric       return thread_error;
271f678e45dSDimitry Andric     }
272f678e45dSDimitry Andric   }
273f678e45dSDimitry Andric 
274f678e45dSDimitry Andric   // Register new hardware breakpoint into hardware breakpoints map of current
275f678e45dSDimitry Andric   // process.
276f678e45dSDimitry Andric   m_hw_breakpoints_map[addr] = {addr, size};
277f678e45dSDimitry Andric 
2785517e702SDimitry Andric   return Status();
279f678e45dSDimitry Andric }
280f678e45dSDimitry Andric 
RemoveHardwareBreakpoint(lldb::addr_t addr)2815517e702SDimitry Andric Status NativeProcessProtocol::RemoveHardwareBreakpoint(lldb::addr_t addr) {
282f678e45dSDimitry Andric   // Update the thread list
283f678e45dSDimitry Andric   UpdateThreads();
284f678e45dSDimitry Andric 
2855517e702SDimitry Andric   Status error;
286f678e45dSDimitry Andric 
287f678e45dSDimitry Andric   std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
288acac075bSDimitry Andric   for (const auto &thread : m_threads) {
289acac075bSDimitry Andric     assert(thread && "thread list should not have a NULL thread!");
290acac075bSDimitry Andric     error = thread->RemoveHardwareBreakpoint(addr);
291f678e45dSDimitry Andric   }
292f678e45dSDimitry Andric 
293f678e45dSDimitry Andric   // Also remove from hardware breakpoint map of current process.
294f678e45dSDimitry Andric   m_hw_breakpoints_map.erase(addr);
295f678e45dSDimitry Andric 
296f678e45dSDimitry Andric   return error;
297f678e45dSDimitry Andric }
298f678e45dSDimitry Andric 
RegisterNativeDelegate(NativeDelegate & native_delegate)299435933ddSDimitry Andric bool NativeProcessProtocol::RegisterNativeDelegate(
300435933ddSDimitry Andric     NativeDelegate &native_delegate) {
3014bb0738eSEd Maste   std::lock_guard<std::recursive_mutex> guard(m_delegates_mutex);
302435933ddSDimitry Andric   if (std::find(m_delegates.begin(), m_delegates.end(), &native_delegate) !=
303435933ddSDimitry Andric       m_delegates.end())
3040127ef0fSEd Maste     return false;
3050127ef0fSEd Maste 
3060127ef0fSEd Maste   m_delegates.push_back(&native_delegate);
3070127ef0fSEd Maste   native_delegate.InitializeDelegate(this);
3080127ef0fSEd Maste   return true;
3090127ef0fSEd Maste }
3100127ef0fSEd Maste 
UnregisterNativeDelegate(NativeDelegate & native_delegate)311435933ddSDimitry Andric bool NativeProcessProtocol::UnregisterNativeDelegate(
312435933ddSDimitry Andric     NativeDelegate &native_delegate) {
3134bb0738eSEd Maste   std::lock_guard<std::recursive_mutex> guard(m_delegates_mutex);
3140127ef0fSEd Maste 
3150127ef0fSEd Maste   const auto initial_size = m_delegates.size();
316435933ddSDimitry Andric   m_delegates.erase(
317435933ddSDimitry Andric       remove(m_delegates.begin(), m_delegates.end(), &native_delegate),
318435933ddSDimitry Andric       m_delegates.end());
3190127ef0fSEd Maste 
3204ba319b5SDimitry Andric   // We removed the delegate if the count of delegates shrank after removing
3214ba319b5SDimitry Andric   // all copies of the given native_delegate from the vector.
3220127ef0fSEd Maste   return m_delegates.size() < initial_size;
3230127ef0fSEd Maste }
3240127ef0fSEd Maste 
SynchronouslyNotifyProcessStateChanged(lldb::StateType state)325435933ddSDimitry Andric void NativeProcessProtocol::SynchronouslyNotifyProcessStateChanged(
326435933ddSDimitry Andric     lldb::StateType state) {
3270127ef0fSEd Maste   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
3280127ef0fSEd Maste 
3294bb0738eSEd Maste   std::lock_guard<std::recursive_mutex> guard(m_delegates_mutex);
3300127ef0fSEd Maste   for (auto native_delegate : m_delegates)
3310127ef0fSEd Maste     native_delegate->ProcessStateChanged(this, state);
3320127ef0fSEd Maste 
333435933ddSDimitry Andric   if (log) {
334435933ddSDimitry Andric     if (!m_delegates.empty()) {
335435933ddSDimitry Andric       log->Printf("NativeProcessProtocol::%s: sent state notification [%s] "
336435933ddSDimitry Andric                   "from process %" PRIu64,
3370127ef0fSEd Maste                   __FUNCTION__, lldb_private::StateAsCString(state), GetID());
338435933ddSDimitry Andric     } else {
339435933ddSDimitry Andric       log->Printf("NativeProcessProtocol::%s: would send state notification "
340435933ddSDimitry Andric                   "[%s] from process %" PRIu64 ", but no delegates",
3410127ef0fSEd Maste                   __FUNCTION__, lldb_private::StateAsCString(state), GetID());
3420127ef0fSEd Maste     }
3430127ef0fSEd Maste   }
3440127ef0fSEd Maste }
3450127ef0fSEd Maste 
NotifyDidExec()346435933ddSDimitry Andric void NativeProcessProtocol::NotifyDidExec() {
3470127ef0fSEd Maste   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
3480127ef0fSEd Maste   if (log)
349435933ddSDimitry Andric     log->Printf("NativeProcessProtocol::%s - preparing to call delegates",
350435933ddSDimitry Andric                 __FUNCTION__);
3510127ef0fSEd Maste 
3520127ef0fSEd Maste   {
3534bb0738eSEd Maste     std::lock_guard<std::recursive_mutex> guard(m_delegates_mutex);
3540127ef0fSEd Maste     for (auto native_delegate : m_delegates)
3550127ef0fSEd Maste       native_delegate->DidExec(this);
3560127ef0fSEd Maste   }
3570127ef0fSEd Maste }
3580127ef0fSEd Maste 
SetSoftwareBreakpoint(lldb::addr_t addr,uint32_t size_hint)3595517e702SDimitry Andric Status NativeProcessProtocol::SetSoftwareBreakpoint(lldb::addr_t addr,
360435933ddSDimitry Andric                                                     uint32_t size_hint) {
3610127ef0fSEd Maste   Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_BREAKPOINTS));
362*b5893f02SDimitry Andric   LLDB_LOG(log, "addr = {0:x}, size_hint = {1}", addr, size_hint);
3630127ef0fSEd Maste 
364*b5893f02SDimitry Andric   auto it = m_software_breakpoints.find(addr);
365*b5893f02SDimitry Andric   if (it != m_software_breakpoints.end()) {
366*b5893f02SDimitry Andric     ++it->second.ref_count;
367*b5893f02SDimitry Andric     return Status();
368*b5893f02SDimitry Andric   }
369*b5893f02SDimitry Andric   auto expected_bkpt = EnableSoftwareBreakpoint(addr, size_hint);
370*b5893f02SDimitry Andric   if (!expected_bkpt)
371*b5893f02SDimitry Andric     return Status(expected_bkpt.takeError());
372*b5893f02SDimitry Andric 
373*b5893f02SDimitry Andric   m_software_breakpoints.emplace(addr, std::move(*expected_bkpt));
374*b5893f02SDimitry Andric   return Status();
375*b5893f02SDimitry Andric }
376*b5893f02SDimitry Andric 
RemoveSoftwareBreakpoint(lldb::addr_t addr)377*b5893f02SDimitry Andric Status NativeProcessProtocol::RemoveSoftwareBreakpoint(lldb::addr_t addr) {
378*b5893f02SDimitry Andric   Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_BREAKPOINTS));
379*b5893f02SDimitry Andric   LLDB_LOG(log, "addr = {0:x}", addr);
380*b5893f02SDimitry Andric   auto it = m_software_breakpoints.find(addr);
381*b5893f02SDimitry Andric   if (it == m_software_breakpoints.end())
382*b5893f02SDimitry Andric     return Status("Breakpoint not found.");
383*b5893f02SDimitry Andric   assert(it->second.ref_count > 0);
384*b5893f02SDimitry Andric   if (--it->second.ref_count > 0)
385*b5893f02SDimitry Andric     return Status();
386*b5893f02SDimitry Andric 
387*b5893f02SDimitry Andric   // This is the last reference. Let's remove the breakpoint.
388*b5893f02SDimitry Andric   Status error;
389*b5893f02SDimitry Andric 
390*b5893f02SDimitry Andric   // Clear a software breakpoint instruction
391*b5893f02SDimitry Andric   llvm::SmallVector<uint8_t, 4> curr_break_op(
392*b5893f02SDimitry Andric       it->second.breakpoint_opcodes.size(), 0);
393*b5893f02SDimitry Andric 
394*b5893f02SDimitry Andric   // Read the breakpoint opcode
395*b5893f02SDimitry Andric   size_t bytes_read = 0;
396*b5893f02SDimitry Andric   error =
397*b5893f02SDimitry Andric       ReadMemory(addr, curr_break_op.data(), curr_break_op.size(), bytes_read);
398*b5893f02SDimitry Andric   if (error.Fail() || bytes_read < curr_break_op.size()) {
399*b5893f02SDimitry Andric     return Status("addr=0x%" PRIx64
400*b5893f02SDimitry Andric                   ": tried to read %zu bytes but only read %zu",
401*b5893f02SDimitry Andric                   addr, curr_break_op.size(), bytes_read);
402*b5893f02SDimitry Andric   }
403*b5893f02SDimitry Andric   const auto &saved = it->second.saved_opcodes;
404*b5893f02SDimitry Andric   // Make sure the breakpoint opcode exists at this address
405*b5893f02SDimitry Andric   if (makeArrayRef(curr_break_op) != it->second.breakpoint_opcodes) {
406*b5893f02SDimitry Andric     if (curr_break_op != it->second.saved_opcodes)
407*b5893f02SDimitry Andric       return Status("Original breakpoint trap is no longer in memory.");
408*b5893f02SDimitry Andric     LLDB_LOG(log,
409*b5893f02SDimitry Andric              "Saved opcodes ({0:@[x]}) have already been restored at {1:x}.",
410*b5893f02SDimitry Andric              llvm::make_range(saved.begin(), saved.end()), addr);
411*b5893f02SDimitry Andric   } else {
412*b5893f02SDimitry Andric     // We found a valid breakpoint opcode at this address, now restore the
413*b5893f02SDimitry Andric     // saved opcode.
414*b5893f02SDimitry Andric     size_t bytes_written = 0;
415*b5893f02SDimitry Andric     error = WriteMemory(addr, saved.data(), saved.size(), bytes_written);
416*b5893f02SDimitry Andric     if (error.Fail() || bytes_written < saved.size()) {
417*b5893f02SDimitry Andric       return Status("addr=0x%" PRIx64
418*b5893f02SDimitry Andric                     ": tried to write %zu bytes but only wrote %zu",
419*b5893f02SDimitry Andric                     addr, saved.size(), bytes_written);
420*b5893f02SDimitry Andric     }
421*b5893f02SDimitry Andric 
422*b5893f02SDimitry Andric     // Verify that our original opcode made it back to the inferior
423*b5893f02SDimitry Andric     llvm::SmallVector<uint8_t, 4> verify_opcode(saved.size(), 0);
424*b5893f02SDimitry Andric     size_t verify_bytes_read = 0;
425*b5893f02SDimitry Andric     error = ReadMemory(addr, verify_opcode.data(), verify_opcode.size(),
426*b5893f02SDimitry Andric                        verify_bytes_read);
427*b5893f02SDimitry Andric     if (error.Fail() || verify_bytes_read < verify_opcode.size()) {
428*b5893f02SDimitry Andric       return Status("addr=0x%" PRIx64
429*b5893f02SDimitry Andric                     ": tried to read %zu verification bytes but only read %zu",
430*b5893f02SDimitry Andric                     addr, verify_opcode.size(), verify_bytes_read);
431*b5893f02SDimitry Andric     }
432*b5893f02SDimitry Andric     if (verify_opcode != saved)
433*b5893f02SDimitry Andric       LLDB_LOG(log, "Restoring bytes at {0:x}: {1:@[x]}", addr,
434*b5893f02SDimitry Andric                llvm::make_range(saved.begin(), saved.end()));
435*b5893f02SDimitry Andric   }
436*b5893f02SDimitry Andric 
437*b5893f02SDimitry Andric   m_software_breakpoints.erase(it);
438*b5893f02SDimitry Andric   return Status();
439*b5893f02SDimitry Andric }
440*b5893f02SDimitry Andric 
441*b5893f02SDimitry Andric llvm::Expected<NativeProcessProtocol::SoftwareBreakpoint>
EnableSoftwareBreakpoint(lldb::addr_t addr,uint32_t size_hint)442*b5893f02SDimitry Andric NativeProcessProtocol::EnableSoftwareBreakpoint(lldb::addr_t addr,
443*b5893f02SDimitry Andric                                                 uint32_t size_hint) {
444*b5893f02SDimitry Andric   Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_BREAKPOINTS));
445*b5893f02SDimitry Andric 
446*b5893f02SDimitry Andric   auto expected_trap = GetSoftwareBreakpointTrapOpcode(size_hint);
447*b5893f02SDimitry Andric   if (!expected_trap)
448*b5893f02SDimitry Andric     return expected_trap.takeError();
449*b5893f02SDimitry Andric 
450*b5893f02SDimitry Andric   llvm::SmallVector<uint8_t, 4> saved_opcode_bytes(expected_trap->size(), 0);
451*b5893f02SDimitry Andric   // Save the original opcodes by reading them so we can restore later.
452*b5893f02SDimitry Andric   size_t bytes_read = 0;
453*b5893f02SDimitry Andric   Status error = ReadMemory(addr, saved_opcode_bytes.data(),
454*b5893f02SDimitry Andric                             saved_opcode_bytes.size(), bytes_read);
455*b5893f02SDimitry Andric   if (error.Fail())
456*b5893f02SDimitry Andric     return error.ToError();
457*b5893f02SDimitry Andric 
458*b5893f02SDimitry Andric   // Ensure we read as many bytes as we expected.
459*b5893f02SDimitry Andric   if (bytes_read != saved_opcode_bytes.size()) {
460*b5893f02SDimitry Andric     return llvm::createStringError(
461*b5893f02SDimitry Andric         llvm::inconvertibleErrorCode(),
462*b5893f02SDimitry Andric         "Failed to read memory while attempting to set breakpoint: attempted "
463*b5893f02SDimitry Andric         "to read {0} bytes but only read {1}.",
464*b5893f02SDimitry Andric         saved_opcode_bytes.size(), bytes_read);
465*b5893f02SDimitry Andric   }
466*b5893f02SDimitry Andric 
467*b5893f02SDimitry Andric   LLDB_LOG(
468*b5893f02SDimitry Andric       log, "Overwriting bytes at {0:x}: {1:@[x]}", addr,
469*b5893f02SDimitry Andric       llvm::make_range(saved_opcode_bytes.begin(), saved_opcode_bytes.end()));
470*b5893f02SDimitry Andric 
471*b5893f02SDimitry Andric   // Write a software breakpoint in place of the original opcode.
472*b5893f02SDimitry Andric   size_t bytes_written = 0;
473*b5893f02SDimitry Andric   error = WriteMemory(addr, expected_trap->data(), expected_trap->size(),
474*b5893f02SDimitry Andric                       bytes_written);
475*b5893f02SDimitry Andric   if (error.Fail())
476*b5893f02SDimitry Andric     return error.ToError();
477*b5893f02SDimitry Andric 
478*b5893f02SDimitry Andric   // Ensure we wrote as many bytes as we expected.
479*b5893f02SDimitry Andric   if (bytes_written != expected_trap->size()) {
480*b5893f02SDimitry Andric     return llvm::createStringError(
481*b5893f02SDimitry Andric         llvm::inconvertibleErrorCode(),
482*b5893f02SDimitry Andric         "Failed write memory while attempting to set "
483*b5893f02SDimitry Andric         "breakpoint: attempted to write {0} bytes but only wrote {1}",
484*b5893f02SDimitry Andric         expected_trap->size(), bytes_written);
485*b5893f02SDimitry Andric   }
486*b5893f02SDimitry Andric 
487*b5893f02SDimitry Andric   llvm::SmallVector<uint8_t, 4> verify_bp_opcode_bytes(expected_trap->size(),
488*b5893f02SDimitry Andric                                                        0);
489*b5893f02SDimitry Andric   size_t verify_bytes_read = 0;
490*b5893f02SDimitry Andric   error = ReadMemory(addr, verify_bp_opcode_bytes.data(),
491*b5893f02SDimitry Andric                      verify_bp_opcode_bytes.size(), verify_bytes_read);
492*b5893f02SDimitry Andric   if (error.Fail())
493*b5893f02SDimitry Andric     return error.ToError();
494*b5893f02SDimitry Andric 
495*b5893f02SDimitry Andric   // Ensure we read as many verification bytes as we expected.
496*b5893f02SDimitry Andric   if (verify_bytes_read != verify_bp_opcode_bytes.size()) {
497*b5893f02SDimitry Andric     return llvm::createStringError(
498*b5893f02SDimitry Andric         llvm::inconvertibleErrorCode(),
499*b5893f02SDimitry Andric         "Failed to read memory while "
500*b5893f02SDimitry Andric         "attempting to verify breakpoint: attempted to read {0} bytes "
501*b5893f02SDimitry Andric         "but only read {1}",
502*b5893f02SDimitry Andric         verify_bp_opcode_bytes.size(), verify_bytes_read);
503*b5893f02SDimitry Andric   }
504*b5893f02SDimitry Andric 
505*b5893f02SDimitry Andric   if (llvm::makeArrayRef(verify_bp_opcode_bytes.data(), verify_bytes_read) !=
506*b5893f02SDimitry Andric       *expected_trap) {
507*b5893f02SDimitry Andric     return llvm::createStringError(
508*b5893f02SDimitry Andric         llvm::inconvertibleErrorCode(),
509*b5893f02SDimitry Andric         "Verification of software breakpoint "
510*b5893f02SDimitry Andric         "writing failed - trap opcodes not successfully read back "
511*b5893f02SDimitry Andric         "after writing when setting breakpoint at {0:x}",
512*b5893f02SDimitry Andric         addr);
513*b5893f02SDimitry Andric   }
514*b5893f02SDimitry Andric 
515*b5893f02SDimitry Andric   LLDB_LOG(log, "addr = {0:x}: SUCCESS", addr);
516*b5893f02SDimitry Andric   return SoftwareBreakpoint{1, saved_opcode_bytes, *expected_trap};
517*b5893f02SDimitry Andric }
518*b5893f02SDimitry Andric 
519*b5893f02SDimitry Andric llvm::Expected<llvm::ArrayRef<uint8_t>>
GetSoftwareBreakpointTrapOpcode(size_t size_hint)520*b5893f02SDimitry Andric NativeProcessProtocol::GetSoftwareBreakpointTrapOpcode(size_t size_hint) {
521*b5893f02SDimitry Andric   static const uint8_t g_aarch64_opcode[] = {0x00, 0x00, 0x20, 0xd4};
522*b5893f02SDimitry Andric   static const uint8_t g_i386_opcode[] = {0xCC};
523*b5893f02SDimitry Andric   static const uint8_t g_mips64_opcode[] = {0x00, 0x00, 0x00, 0x0d};
524*b5893f02SDimitry Andric   static const uint8_t g_mips64el_opcode[] = {0x0d, 0x00, 0x00, 0x00};
525*b5893f02SDimitry Andric   static const uint8_t g_s390x_opcode[] = {0x00, 0x01};
526*b5893f02SDimitry Andric   static const uint8_t g_ppc64le_opcode[] = {0x08, 0x00, 0xe0, 0x7f}; // trap
527*b5893f02SDimitry Andric 
528*b5893f02SDimitry Andric   switch (GetArchitecture().GetMachine()) {
529*b5893f02SDimitry Andric   case llvm::Triple::aarch64:
530*b5893f02SDimitry Andric     return llvm::makeArrayRef(g_aarch64_opcode);
531*b5893f02SDimitry Andric 
532*b5893f02SDimitry Andric   case llvm::Triple::x86:
533*b5893f02SDimitry Andric   case llvm::Triple::x86_64:
534*b5893f02SDimitry Andric     return llvm::makeArrayRef(g_i386_opcode);
535*b5893f02SDimitry Andric 
536*b5893f02SDimitry Andric   case llvm::Triple::mips:
537*b5893f02SDimitry Andric   case llvm::Triple::mips64:
538*b5893f02SDimitry Andric     return llvm::makeArrayRef(g_mips64_opcode);
539*b5893f02SDimitry Andric 
540*b5893f02SDimitry Andric   case llvm::Triple::mipsel:
541*b5893f02SDimitry Andric   case llvm::Triple::mips64el:
542*b5893f02SDimitry Andric     return llvm::makeArrayRef(g_mips64el_opcode);
543*b5893f02SDimitry Andric 
544*b5893f02SDimitry Andric   case llvm::Triple::systemz:
545*b5893f02SDimitry Andric     return llvm::makeArrayRef(g_s390x_opcode);
546*b5893f02SDimitry Andric 
547*b5893f02SDimitry Andric   case llvm::Triple::ppc64le:
548*b5893f02SDimitry Andric     return llvm::makeArrayRef(g_ppc64le_opcode);
549*b5893f02SDimitry Andric 
550*b5893f02SDimitry Andric   default:
551*b5893f02SDimitry Andric     return llvm::createStringError(llvm::inconvertibleErrorCode(),
552*b5893f02SDimitry Andric                                    "CPU type not supported!");
553*b5893f02SDimitry Andric   }
554*b5893f02SDimitry Andric }
555*b5893f02SDimitry Andric 
GetSoftwareBreakpointPCOffset()556*b5893f02SDimitry Andric size_t NativeProcessProtocol::GetSoftwareBreakpointPCOffset() {
557*b5893f02SDimitry Andric   switch (GetArchitecture().GetMachine()) {
558*b5893f02SDimitry Andric   case llvm::Triple::x86:
559*b5893f02SDimitry Andric   case llvm::Triple::x86_64:
560*b5893f02SDimitry Andric   case llvm::Triple::systemz:
561*b5893f02SDimitry Andric     // These architectures report increment the PC after breakpoint is hit.
562*b5893f02SDimitry Andric     return cantFail(GetSoftwareBreakpointTrapOpcode(0)).size();
563*b5893f02SDimitry Andric 
564*b5893f02SDimitry Andric   case llvm::Triple::arm:
565*b5893f02SDimitry Andric   case llvm::Triple::aarch64:
566*b5893f02SDimitry Andric   case llvm::Triple::mips64:
567*b5893f02SDimitry Andric   case llvm::Triple::mips64el:
568*b5893f02SDimitry Andric   case llvm::Triple::mips:
569*b5893f02SDimitry Andric   case llvm::Triple::mipsel:
570*b5893f02SDimitry Andric   case llvm::Triple::ppc64le:
571*b5893f02SDimitry Andric     // On these architectures the PC doesn't get updated for breakpoint hits.
572*b5893f02SDimitry Andric     return 0;
573*b5893f02SDimitry Andric 
574*b5893f02SDimitry Andric   default:
575*b5893f02SDimitry Andric     llvm_unreachable("CPU type not supported!");
576*b5893f02SDimitry Andric   }
577*b5893f02SDimitry Andric }
578*b5893f02SDimitry Andric 
FixupBreakpointPCAsNeeded(NativeThreadProtocol & thread)579*b5893f02SDimitry Andric void NativeProcessProtocol::FixupBreakpointPCAsNeeded(
580*b5893f02SDimitry Andric     NativeThreadProtocol &thread) {
581*b5893f02SDimitry Andric   Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_BREAKPOINTS);
582*b5893f02SDimitry Andric 
583*b5893f02SDimitry Andric   Status error;
584*b5893f02SDimitry Andric 
585*b5893f02SDimitry Andric   // Find out the size of a breakpoint (might depend on where we are in the
586*b5893f02SDimitry Andric   // code).
587*b5893f02SDimitry Andric   NativeRegisterContext &context = thread.GetRegisterContext();
588*b5893f02SDimitry Andric 
589*b5893f02SDimitry Andric   uint32_t breakpoint_size = GetSoftwareBreakpointPCOffset();
590*b5893f02SDimitry Andric   LLDB_LOG(log, "breakpoint size: {0}", breakpoint_size);
591*b5893f02SDimitry Andric   if (breakpoint_size == 0)
592*b5893f02SDimitry Andric     return;
593*b5893f02SDimitry Andric 
594*b5893f02SDimitry Andric   // First try probing for a breakpoint at a software breakpoint location: PC -
595*b5893f02SDimitry Andric   // breakpoint size.
596*b5893f02SDimitry Andric   const lldb::addr_t initial_pc_addr = context.GetPCfromBreakpointLocation();
597*b5893f02SDimitry Andric   lldb::addr_t breakpoint_addr = initial_pc_addr;
598*b5893f02SDimitry Andric   // Do not allow breakpoint probe to wrap around.
599*b5893f02SDimitry Andric   if (breakpoint_addr >= breakpoint_size)
600*b5893f02SDimitry Andric     breakpoint_addr -= breakpoint_size;
601*b5893f02SDimitry Andric 
602*b5893f02SDimitry Andric   if (m_software_breakpoints.count(breakpoint_addr) == 0) {
603*b5893f02SDimitry Andric     // We didn't find one at a software probe location.  Nothing to do.
604*b5893f02SDimitry Andric     LLDB_LOG(log,
605*b5893f02SDimitry Andric              "pid {0} no lldb software breakpoint found at current pc with "
606*b5893f02SDimitry Andric              "adjustment: {1}",
607*b5893f02SDimitry Andric              GetID(), breakpoint_addr);
608*b5893f02SDimitry Andric     return;
609*b5893f02SDimitry Andric   }
610*b5893f02SDimitry Andric 
611*b5893f02SDimitry Andric   //
612*b5893f02SDimitry Andric   // We have a software breakpoint and need to adjust the PC.
613*b5893f02SDimitry Andric   //
614*b5893f02SDimitry Andric 
615*b5893f02SDimitry Andric   // Change the program counter.
616*b5893f02SDimitry Andric   LLDB_LOG(log, "pid {0} tid {1}: changing PC from {2:x} to {3:x}", GetID(),
617*b5893f02SDimitry Andric            thread.GetID(), initial_pc_addr, breakpoint_addr);
618*b5893f02SDimitry Andric 
619*b5893f02SDimitry Andric   error = context.SetPC(breakpoint_addr);
620*b5893f02SDimitry Andric   if (error.Fail()) {
621*b5893f02SDimitry Andric     // This can happen in case the process was killed between the time we read
622*b5893f02SDimitry Andric     // the PC and when we are updating it. There's nothing better to do than to
623*b5893f02SDimitry Andric     // swallow the error.
624*b5893f02SDimitry Andric     LLDB_LOG(log, "pid {0} tid {1}: failed to set PC: {2}", GetID(),
625*b5893f02SDimitry Andric              thread.GetID(), error);
626*b5893f02SDimitry Andric   }
6270127ef0fSEd Maste }
6280127ef0fSEd Maste 
RemoveBreakpoint(lldb::addr_t addr,bool hardware)6295517e702SDimitry Andric Status NativeProcessProtocol::RemoveBreakpoint(lldb::addr_t addr,
630f678e45dSDimitry Andric                                                bool hardware) {
631f678e45dSDimitry Andric   if (hardware)
632f678e45dSDimitry Andric     return RemoveHardwareBreakpoint(addr);
633f678e45dSDimitry Andric   else
634*b5893f02SDimitry Andric     return RemoveSoftwareBreakpoint(addr);
6350127ef0fSEd Maste }
6360127ef0fSEd Maste 
ReadMemoryWithoutTrap(lldb::addr_t addr,void * buf,size_t size,size_t & bytes_read)637*b5893f02SDimitry Andric Status NativeProcessProtocol::ReadMemoryWithoutTrap(lldb::addr_t addr,
638*b5893f02SDimitry Andric                                                     void *buf, size_t size,
639*b5893f02SDimitry Andric                                                     size_t &bytes_read) {
640*b5893f02SDimitry Andric   Status error = ReadMemory(addr, buf, size, bytes_read);
641*b5893f02SDimitry Andric   if (error.Fail())
642*b5893f02SDimitry Andric     return error;
6430127ef0fSEd Maste 
644*b5893f02SDimitry Andric   auto data =
645*b5893f02SDimitry Andric       llvm::makeMutableArrayRef(static_cast<uint8_t *>(buf), bytes_read);
646*b5893f02SDimitry Andric   for (const auto &pair : m_software_breakpoints) {
647*b5893f02SDimitry Andric     lldb::addr_t bp_addr = pair.first;
648*b5893f02SDimitry Andric     auto saved_opcodes = makeArrayRef(pair.second.saved_opcodes);
649*b5893f02SDimitry Andric 
650*b5893f02SDimitry Andric     if (bp_addr + saved_opcodes.size() < addr || addr + bytes_read <= bp_addr)
651*b5893f02SDimitry Andric       continue; // Breapoint not in range, ignore
652*b5893f02SDimitry Andric 
653*b5893f02SDimitry Andric     if (bp_addr < addr) {
654*b5893f02SDimitry Andric       saved_opcodes = saved_opcodes.drop_front(addr - bp_addr);
655*b5893f02SDimitry Andric       bp_addr = addr;
656*b5893f02SDimitry Andric     }
657*b5893f02SDimitry Andric     auto bp_data = data.drop_front(bp_addr - addr);
658*b5893f02SDimitry Andric     std::copy_n(saved_opcodes.begin(),
659*b5893f02SDimitry Andric                 std::min(saved_opcodes.size(), bp_data.size()),
660*b5893f02SDimitry Andric                 bp_data.begin());
661*b5893f02SDimitry Andric   }
662*b5893f02SDimitry Andric   return Status();
6630127ef0fSEd Maste }
6640127ef0fSEd Maste 
GetState() const665435933ddSDimitry Andric lldb::StateType NativeProcessProtocol::GetState() const {
6664bb0738eSEd Maste   std::lock_guard<std::recursive_mutex> guard(m_state_mutex);
6670127ef0fSEd Maste   return m_state;
6680127ef0fSEd Maste }
6690127ef0fSEd Maste 
SetState(lldb::StateType state,bool notify_delegates)670435933ddSDimitry Andric void NativeProcessProtocol::SetState(lldb::StateType state,
671435933ddSDimitry Andric                                      bool notify_delegates) {
6724bb0738eSEd Maste   std::lock_guard<std::recursive_mutex> guard(m_state_mutex);
6731c3bbb01SEd Maste 
6741c3bbb01SEd Maste   if (state == m_state)
6751c3bbb01SEd Maste     return;
6761c3bbb01SEd Maste 
6770127ef0fSEd Maste   m_state = state;
6780127ef0fSEd Maste 
679435933ddSDimitry Andric   if (StateIsStoppedState(state, false)) {
6800127ef0fSEd Maste     ++m_stop_id;
6810127ef0fSEd Maste 
6820127ef0fSEd Maste     // Give process a chance to do any stop id bump processing, such as
6830127ef0fSEd Maste     // clearing cached data that is invalidated each time the process runs.
6844ba319b5SDimitry Andric     // Note if/when we support some threads running, we'll end up needing to
6854ba319b5SDimitry Andric     // manage this per thread and per process.
6860127ef0fSEd Maste     DoStopIDBumped(m_stop_id);
6870127ef0fSEd Maste   }
6880127ef0fSEd Maste 
6890127ef0fSEd Maste   // Optionally notify delegates of the state change.
6900127ef0fSEd Maste   if (notify_delegates)
6910127ef0fSEd Maste     SynchronouslyNotifyProcessStateChanged(state);
6920127ef0fSEd Maste }
6930127ef0fSEd Maste 
GetStopID() const694435933ddSDimitry Andric uint32_t NativeProcessProtocol::GetStopID() const {
6954bb0738eSEd Maste   std::lock_guard<std::recursive_mutex> guard(m_state_mutex);
6960127ef0fSEd Maste   return m_stop_id;
6970127ef0fSEd Maste }
6980127ef0fSEd Maste 
DoStopIDBumped(uint32_t)699435933ddSDimitry Andric void NativeProcessProtocol::DoStopIDBumped(uint32_t /* newBumpId */) {
7000127ef0fSEd Maste   // Default implementation does nothing.
7010127ef0fSEd Maste }
7021c3bbb01SEd Maste 
703c4394386SDimitry Andric NativeProcessProtocol::Factory::~Factory() = default;
704