1af245d11STodd Fiala //===-- NativeProcessProtocol.cpp -------------------------------*- C++ -*-===//
2af245d11STodd Fiala //
3af245d11STodd Fiala //                     The LLVM Compiler Infrastructure
4af245d11STodd Fiala //
5af245d11STodd Fiala // This file is distributed under the University of Illinois Open Source
6af245d11STodd Fiala // License. See LICENSE.TXT for details.
7af245d11STodd Fiala //
8af245d11STodd Fiala //===----------------------------------------------------------------------===//
9af245d11STodd Fiala 
102fe1d0abSChaoren Lin #include "lldb/Host/common/NativeProcessProtocol.h"
11af245d11STodd Fiala 
12af245d11STodd Fiala #include "lldb/Core/ArchSpec.h"
13af245d11STodd Fiala #include "lldb/Core/Log.h"
14e77fce0aSTodd Fiala #include "lldb/Core/ModuleSpec.h"
15af245d11STodd Fiala #include "lldb/Core/State.h"
16511e5cdcSTodd Fiala #include "lldb/Host/Host.h"
172fe1d0abSChaoren Lin #include "lldb/Host/common/NativeRegisterContext.h"
182fe1d0abSChaoren Lin #include "lldb/Host/common/NativeThreadProtocol.h"
192fe1d0abSChaoren Lin #include "lldb/Host/common/SoftwareBreakpoint.h"
20e77fce0aSTodd Fiala #include "lldb/Symbol/ObjectFile.h"
21e77fce0aSTodd Fiala #include "lldb/Target/Process.h"
22e77fce0aSTodd Fiala #include "lldb/Utility/LLDBAssert.h"
23b9c1b51eSKate Stone #include "lldb/lldb-enumerations.h"
24af245d11STodd Fiala 
25af245d11STodd Fiala using namespace lldb;
26af245d11STodd Fiala using namespace lldb_private;
27af245d11STodd Fiala 
28af245d11STodd Fiala // -----------------------------------------------------------------------------
29af245d11STodd Fiala // NativeProcessProtocol Members
30af245d11STodd Fiala // -----------------------------------------------------------------------------
31af245d11STodd Fiala 
3216ff8604SSaleem Abdulrasool NativeProcessProtocol::NativeProcessProtocol(lldb::pid_t pid)
33b9c1b51eSKate Stone     : m_pid(pid), m_threads(), m_current_thread_id(LLDB_INVALID_THREAD_ID),
34b9c1b51eSKate Stone       m_threads_mutex(), m_state(lldb::eStateInvalid), m_state_mutex(),
35b9c1b51eSKate Stone       m_exit_type(eExitTypeInvalid), m_exit_status(0), m_exit_description(),
36b9c1b51eSKate Stone       m_delegates_mutex(), m_delegates(), m_breakpoint_list(),
37b9c1b51eSKate Stone       m_watchpoint_list(), m_terminal_fd(-1), m_stop_id(0) {}
38af245d11STodd Fiala 
39b9c1b51eSKate Stone lldb_private::Error NativeProcessProtocol::Interrupt() {
40511e5cdcSTodd Fiala   Error error;
41511e5cdcSTodd Fiala #if !defined(SIGSTOP)
42511e5cdcSTodd Fiala   error.SetErrorString("local host does not support signaling");
43511e5cdcSTodd Fiala   return error;
44511e5cdcSTodd Fiala #else
45511e5cdcSTodd Fiala   return Signal(SIGSTOP);
46511e5cdcSTodd Fiala #endif
47511e5cdcSTodd Fiala }
48511e5cdcSTodd Fiala 
494a705e7eSPavel Labath Error NativeProcessProtocol::IgnoreSignals(llvm::ArrayRef<int> signals) {
504a705e7eSPavel Labath   m_signals_to_ignore.clear();
514a705e7eSPavel Labath   m_signals_to_ignore.insert(signals.begin(), signals.end());
524a705e7eSPavel Labath   return Error();
534a705e7eSPavel Labath }
544a705e7eSPavel Labath 
55511e5cdcSTodd Fiala lldb_private::Error
56b9c1b51eSKate Stone NativeProcessProtocol::GetMemoryRegionInfo(lldb::addr_t load_addr,
57b9c1b51eSKate Stone                                            MemoryRegionInfo &range_info) {
58af245d11STodd Fiala   // Default: not implemented.
59af245d11STodd Fiala   return Error("not implemented");
60af245d11STodd Fiala }
61af245d11STodd Fiala 
62b9c1b51eSKate Stone bool NativeProcessProtocol::GetExitStatus(ExitType *exit_type, int *status,
63b9c1b51eSKate Stone                                           std::string &exit_description) {
64b9c1b51eSKate Stone   if (m_state == lldb::eStateExited) {
65af245d11STodd Fiala     *exit_type = m_exit_type;
66af245d11STodd Fiala     *status = m_exit_status;
67af245d11STodd Fiala     exit_description = m_exit_description;
68af245d11STodd Fiala     return true;
69af245d11STodd Fiala   }
70af245d11STodd Fiala 
71af245d11STodd Fiala   *status = 0;
72af245d11STodd Fiala   return false;
73af245d11STodd Fiala }
74af245d11STodd Fiala 
75b9c1b51eSKate Stone bool NativeProcessProtocol::SetExitStatus(ExitType exit_type, int status,
76b9c1b51eSKate Stone                                           const char *exit_description,
77b9c1b51eSKate Stone                                           bool bNotifyStateChange) {
78af245d11STodd Fiala   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
79af245d11STodd Fiala   if (log)
80af245d11STodd Fiala     log->Printf("NativeProcessProtocol::%s(%d, %d, %s, %s) called",
81b9c1b51eSKate Stone                 __FUNCTION__, exit_type, status,
82af245d11STodd Fiala                 exit_description ? exit_description : "nullptr",
83af245d11STodd Fiala                 bNotifyStateChange ? "true" : "false");
84af245d11STodd Fiala 
85af245d11STodd Fiala   // Exit status already set
86b9c1b51eSKate Stone   if (m_state == lldb::eStateExited) {
87af245d11STodd Fiala     if (log)
88b9c1b51eSKate Stone       log->Printf("NativeProcessProtocol::%s exit status already set to %d, "
89b9c1b51eSKate Stone                   "ignoring new set to %d",
90b9c1b51eSKate Stone                   __FUNCTION__, m_exit_status, status);
91af245d11STodd Fiala     return false;
92af245d11STodd Fiala   }
93af245d11STodd Fiala 
94af245d11STodd Fiala   m_state = lldb::eStateExited;
95af245d11STodd Fiala 
96af245d11STodd Fiala   m_exit_type = exit_type;
97af245d11STodd Fiala   m_exit_status = status;
98af245d11STodd Fiala   if (exit_description && exit_description[0])
99af245d11STodd Fiala     m_exit_description = exit_description;
100af245d11STodd Fiala   else
101af245d11STodd Fiala     m_exit_description.clear();
102af245d11STodd Fiala 
103af245d11STodd Fiala   if (bNotifyStateChange)
104af245d11STodd Fiala     SynchronouslyNotifyProcessStateChanged(lldb::eStateExited);
105af245d11STodd Fiala 
106af245d11STodd Fiala   return true;
107af245d11STodd Fiala }
108af245d11STodd Fiala 
109b9c1b51eSKate Stone NativeThreadProtocolSP NativeProcessProtocol::GetThreadAtIndex(uint32_t idx) {
11016ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
111af245d11STodd Fiala   if (idx < m_threads.size())
112af245d11STodd Fiala     return m_threads[idx];
113af245d11STodd Fiala   return NativeThreadProtocolSP();
114af245d11STodd Fiala }
115af245d11STodd Fiala 
116af245d11STodd Fiala NativeThreadProtocolSP
117b9c1b51eSKate Stone NativeProcessProtocol::GetThreadByIDUnlocked(lldb::tid_t tid) {
118b9c1b51eSKate Stone   for (auto thread_sp : m_threads) {
119af245d11STodd Fiala     if (thread_sp->GetID() == tid)
120af245d11STodd Fiala       return thread_sp;
121af245d11STodd Fiala   }
122af245d11STodd Fiala   return NativeThreadProtocolSP();
123af245d11STodd Fiala }
124af245d11STodd Fiala 
125b9c1b51eSKate Stone NativeThreadProtocolSP NativeProcessProtocol::GetThreadByID(lldb::tid_t tid) {
12616ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
127511e5cdcSTodd Fiala   return GetThreadByIDUnlocked(tid);
128511e5cdcSTodd Fiala }
129511e5cdcSTodd Fiala 
130b9c1b51eSKate Stone bool NativeProcessProtocol::IsAlive() const {
131b9c1b51eSKate Stone   return m_state != eStateDetached && m_state != eStateExited &&
132b9c1b51eSKate Stone          m_state != eStateInvalid && m_state != eStateUnloaded;
133af245d11STodd Fiala }
134af245d11STodd Fiala 
135b9c1b51eSKate Stone bool NativeProcessProtocol::GetByteOrder(lldb::ByteOrder &byte_order) const {
136af245d11STodd Fiala   ArchSpec process_arch;
137af245d11STodd Fiala   if (!GetArchitecture(process_arch))
138af245d11STodd Fiala     return false;
139af245d11STodd Fiala   byte_order = process_arch.GetByteOrder();
140af245d11STodd Fiala   return true;
141af245d11STodd Fiala }
142af245d11STodd Fiala 
14318fe6404SChaoren Lin const NativeWatchpointList::WatchpointMap &
144b9c1b51eSKate Stone NativeProcessProtocol::GetWatchpointMap() const {
14518fe6404SChaoren Lin   return m_watchpoint_list.GetWatchpointMap();
14618fe6404SChaoren Lin }
14718fe6404SChaoren Lin 
148*d5ffbad2SOmair Javaid llvm::Optional<std::pair<uint32_t, uint32_t>>
149*d5ffbad2SOmair Javaid NativeProcessProtocol::GetHardwareDebugSupportInfo() const {
150af245d11STodd Fiala   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
151af245d11STodd Fiala 
152af245d11STodd Fiala   // get any thread
153b9c1b51eSKate Stone   NativeThreadProtocolSP thread_sp(
154b9c1b51eSKate Stone       const_cast<NativeProcessProtocol *>(this)->GetThreadAtIndex(0));
155b9c1b51eSKate Stone   if (!thread_sp) {
156af245d11STodd Fiala     if (log)
157b9c1b51eSKate Stone       log->Warning("NativeProcessProtocol::%s (): failed to find a thread to "
158b9c1b51eSKate Stone                    "grab a NativeRegisterContext!",
159b9c1b51eSKate Stone                    __FUNCTION__);
160*d5ffbad2SOmair Javaid     return llvm::None;
161af245d11STodd Fiala   }
162af245d11STodd Fiala 
163af245d11STodd Fiala   NativeRegisterContextSP reg_ctx_sp(thread_sp->GetRegisterContext());
164b9c1b51eSKate Stone   if (!reg_ctx_sp) {
165af245d11STodd Fiala     if (log)
166b9c1b51eSKate Stone       log->Warning("NativeProcessProtocol::%s (): failed to get a "
167b9c1b51eSKate Stone                    "RegisterContextNativeProcess from the first thread!",
168b9c1b51eSKate Stone                    __FUNCTION__);
169*d5ffbad2SOmair Javaid     return llvm::None;
170af245d11STodd Fiala   }
171af245d11STodd Fiala 
172*d5ffbad2SOmair Javaid   return std::make_pair(reg_ctx_sp->NumSupportedHardwareBreakpoints(),
173*d5ffbad2SOmair Javaid                         reg_ctx_sp->NumSupportedHardwareWatchpoints());
174af245d11STodd Fiala }
175af245d11STodd Fiala 
176b9c1b51eSKate Stone Error NativeProcessProtocol::SetWatchpoint(lldb::addr_t addr, size_t size,
177b9c1b51eSKate Stone                                            uint32_t watch_flags,
178b9c1b51eSKate Stone                                            bool hardware) {
179af245d11STodd Fiala   // This default implementation assumes setting the watchpoint for
180af245d11STodd Fiala   // the process will require setting the watchpoint for each of the
181af245d11STodd Fiala   // threads.  Furthermore, it will track watchpoints set for the
182af245d11STodd Fiala   // process and will add them to each thread that is attached to
183af245d11STodd Fiala   // via the (FIXME implement) OnThreadAttached () method.
184af245d11STodd Fiala 
185af245d11STodd Fiala   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
186af245d11STodd Fiala 
187af245d11STodd Fiala   // Update the thread list
188af245d11STodd Fiala   UpdateThreads();
189af245d11STodd Fiala 
190af245d11STodd Fiala   // Keep track of the threads we successfully set the watchpoint
191af245d11STodd Fiala   // for.  If one of the thread watchpoint setting operations fails,
192af245d11STodd Fiala   // back off and remove the watchpoint for all the threads that
193af245d11STodd Fiala   // were successfully set so we get back to a consistent state.
194af245d11STodd Fiala   std::vector<NativeThreadProtocolSP> watchpoint_established_threads;
195af245d11STodd Fiala 
196af245d11STodd Fiala   // Tell each thread to set a watchpoint.  In the event that
197af245d11STodd Fiala   // hardware watchpoints are requested but the SetWatchpoint fails,
198af245d11STodd Fiala   // try to set a software watchpoint as a fallback.  It's
199af245d11STodd Fiala   // conceivable that if there are more threads than hardware
200af245d11STodd Fiala   // watchpoints available, some of the threads will fail to set
201af245d11STodd Fiala   // hardware watchpoints while software ones may be available.
20216ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
203b9c1b51eSKate Stone   for (auto thread_sp : m_threads) {
204af245d11STodd Fiala     assert(thread_sp && "thread list should not have a NULL thread!");
205af245d11STodd Fiala     if (!thread_sp)
206af245d11STodd Fiala       continue;
207af245d11STodd Fiala 
208b9c1b51eSKate Stone     Error thread_error =
209b9c1b51eSKate Stone         thread_sp->SetWatchpoint(addr, size, watch_flags, hardware);
210b9c1b51eSKate Stone     if (thread_error.Fail() && hardware) {
211af245d11STodd Fiala       // Try software watchpoints since we failed on hardware watchpoint setting
212af245d11STodd Fiala       // and we may have just run out of hardware watchpoints.
213af245d11STodd Fiala       thread_error = thread_sp->SetWatchpoint(addr, size, watch_flags, false);
214b9c1b51eSKate Stone       if (thread_error.Success()) {
215af245d11STodd Fiala         if (log)
216b9c1b51eSKate Stone           log->Warning(
217b9c1b51eSKate Stone               "hardware watchpoint requested but software watchpoint set");
218af245d11STodd Fiala       }
219af245d11STodd Fiala     }
220af245d11STodd Fiala 
221b9c1b51eSKate Stone     if (thread_error.Success()) {
222af245d11STodd Fiala       // Remember that we set this watchpoint successfully in
223af245d11STodd Fiala       // case we need to clear it later.
224af245d11STodd Fiala       watchpoint_established_threads.push_back(thread_sp);
225b9c1b51eSKate Stone     } else {
226af245d11STodd Fiala       // Unset the watchpoint for each thread we successfully
227af245d11STodd Fiala       // set so that we get back to a consistent state of "not
228af245d11STodd Fiala       // set" for the watchpoint.
229b9c1b51eSKate Stone       for (auto unwatch_thread_sp : watchpoint_established_threads) {
230af245d11STodd Fiala         Error remove_error = unwatch_thread_sp->RemoveWatchpoint(addr);
231b9c1b51eSKate Stone         if (remove_error.Fail() && log) {
232b9c1b51eSKate Stone           log->Warning("NativeProcessProtocol::%s (): RemoveWatchpoint failed "
233b9c1b51eSKate Stone                        "for pid=%" PRIu64 ", tid=%" PRIu64 ": %s",
234b9c1b51eSKate Stone                        __FUNCTION__, GetID(), unwatch_thread_sp->GetID(),
235b9c1b51eSKate Stone                        remove_error.AsCString());
236af245d11STodd Fiala         }
237af245d11STodd Fiala       }
238af245d11STodd Fiala 
239af245d11STodd Fiala       return thread_error;
240af245d11STodd Fiala     }
241af245d11STodd Fiala   }
24218fe6404SChaoren Lin   return m_watchpoint_list.Add(addr, size, watch_flags, hardware);
243af245d11STodd Fiala }
244af245d11STodd Fiala 
245b9c1b51eSKate Stone Error NativeProcessProtocol::RemoveWatchpoint(lldb::addr_t addr) {
246af245d11STodd Fiala   // Update the thread list
247af245d11STodd Fiala   UpdateThreads();
248af245d11STodd Fiala 
249af245d11STodd Fiala   Error overall_error;
250af245d11STodd Fiala 
25116ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
252b9c1b51eSKate Stone   for (auto thread_sp : m_threads) {
253af245d11STodd Fiala     assert(thread_sp && "thread list should not have a NULL thread!");
254af245d11STodd Fiala     if (!thread_sp)
255af245d11STodd Fiala       continue;
256af245d11STodd Fiala 
257af245d11STodd Fiala     const Error thread_error = thread_sp->RemoveWatchpoint(addr);
258b9c1b51eSKate Stone     if (thread_error.Fail()) {
259af245d11STodd Fiala       // Keep track of the first thread error if any threads
260af245d11STodd Fiala       // fail. We want to try to remove the watchpoint from
261af245d11STodd Fiala       // every thread, though, even if one or more have errors.
262af245d11STodd Fiala       if (!overall_error.Fail())
263af245d11STodd Fiala         overall_error = thread_error;
264af245d11STodd Fiala     }
265af245d11STodd Fiala   }
26618fe6404SChaoren Lin   const Error error = m_watchpoint_list.Remove(addr);
26718fe6404SChaoren Lin   return overall_error.Fail() ? overall_error : error;
268af245d11STodd Fiala }
269af245d11STodd Fiala 
270*d5ffbad2SOmair Javaid const HardwareBreakpointMap &
271*d5ffbad2SOmair Javaid NativeProcessProtocol::GetHardwareBreakpointMap() const {
272*d5ffbad2SOmair Javaid   return m_hw_breakpoints_map;
273*d5ffbad2SOmair Javaid }
274*d5ffbad2SOmair Javaid 
275*d5ffbad2SOmair Javaid Error NativeProcessProtocol::SetHardwareBreakpoint(lldb::addr_t addr,
276*d5ffbad2SOmair Javaid                                                    size_t size) {
277*d5ffbad2SOmair Javaid   // This default implementation assumes setting a hardware breakpoint for
278*d5ffbad2SOmair Javaid   // this process will require setting same hardware breakpoint for each
279*d5ffbad2SOmair Javaid   // of its existing threads. New thread will do the same once created.
280*d5ffbad2SOmair Javaid   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
281*d5ffbad2SOmair Javaid 
282*d5ffbad2SOmair Javaid   // Update the thread list
283*d5ffbad2SOmair Javaid   UpdateThreads();
284*d5ffbad2SOmair Javaid 
285*d5ffbad2SOmair Javaid   // Exit here if target does not have required hardware breakpoint capability.
286*d5ffbad2SOmair Javaid   auto hw_debug_cap = GetHardwareDebugSupportInfo();
287*d5ffbad2SOmair Javaid 
288*d5ffbad2SOmair Javaid   if (hw_debug_cap == llvm::None || hw_debug_cap->first == 0 ||
289*d5ffbad2SOmair Javaid       hw_debug_cap->first <= m_hw_breakpoints_map.size())
290*d5ffbad2SOmair Javaid     return Error("Target does not have required no of hardware breakpoints");
291*d5ffbad2SOmair Javaid 
292*d5ffbad2SOmair Javaid   // Vector below stores all thread pointer for which we have we successfully
293*d5ffbad2SOmair Javaid   // set this hardware breakpoint. If any of the current process threads fails
294*d5ffbad2SOmair Javaid   // to set this hardware breakpoint then roll back and remove this breakpoint
295*d5ffbad2SOmair Javaid   // for all the threads that had already set it successfully.
296*d5ffbad2SOmair Javaid   std::vector<NativeThreadProtocolSP> breakpoint_established_threads;
297*d5ffbad2SOmair Javaid 
298*d5ffbad2SOmair Javaid   // Request to set a hardware breakpoint for each of current process threads.
299*d5ffbad2SOmair Javaid   std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
300*d5ffbad2SOmair Javaid   for (auto thread_sp : m_threads) {
301*d5ffbad2SOmair Javaid     assert(thread_sp && "thread list should not have a NULL thread!");
302*d5ffbad2SOmair Javaid     if (!thread_sp)
303*d5ffbad2SOmair Javaid       continue;
304*d5ffbad2SOmair Javaid 
305*d5ffbad2SOmair Javaid     Error thread_error = thread_sp->SetHardwareBreakpoint(addr, size);
306*d5ffbad2SOmair Javaid     if (thread_error.Success()) {
307*d5ffbad2SOmair Javaid       // Remember that we set this breakpoint successfully in
308*d5ffbad2SOmair Javaid       // case we need to clear it later.
309*d5ffbad2SOmair Javaid       breakpoint_established_threads.push_back(thread_sp);
310*d5ffbad2SOmair Javaid     } else {
311*d5ffbad2SOmair Javaid       // Unset the breakpoint for each thread we successfully
312*d5ffbad2SOmair Javaid       // set so that we get back to a consistent state of "not
313*d5ffbad2SOmair Javaid       // set" for this hardware breakpoint.
314*d5ffbad2SOmair Javaid       for (auto rollback_thread_sp : breakpoint_established_threads) {
315*d5ffbad2SOmair Javaid         Error remove_error = rollback_thread_sp->RemoveHardwareBreakpoint(addr);
316*d5ffbad2SOmair Javaid         if (remove_error.Fail() && log) {
317*d5ffbad2SOmair Javaid           log->Warning("NativeProcessProtocol::%s (): RemoveHardwareBreakpoint"
318*d5ffbad2SOmair Javaid                        " failed for pid=%" PRIu64 ", tid=%" PRIu64 ": %s",
319*d5ffbad2SOmair Javaid                        __FUNCTION__, GetID(), rollback_thread_sp->GetID(),
320*d5ffbad2SOmair Javaid                        remove_error.AsCString());
321*d5ffbad2SOmair Javaid         }
322*d5ffbad2SOmair Javaid       }
323*d5ffbad2SOmair Javaid 
324*d5ffbad2SOmair Javaid       return thread_error;
325*d5ffbad2SOmair Javaid     }
326*d5ffbad2SOmair Javaid   }
327*d5ffbad2SOmair Javaid 
328*d5ffbad2SOmair Javaid   // Register new hardware breakpoint into hardware breakpoints map of current
329*d5ffbad2SOmair Javaid   // process.
330*d5ffbad2SOmair Javaid   m_hw_breakpoints_map[addr] = {addr, size};
331*d5ffbad2SOmair Javaid 
332*d5ffbad2SOmair Javaid   return Error();
333*d5ffbad2SOmair Javaid }
334*d5ffbad2SOmair Javaid 
335*d5ffbad2SOmair Javaid Error NativeProcessProtocol::RemoveHardwareBreakpoint(lldb::addr_t addr) {
336*d5ffbad2SOmair Javaid   // Update the thread list
337*d5ffbad2SOmair Javaid   UpdateThreads();
338*d5ffbad2SOmair Javaid 
339*d5ffbad2SOmair Javaid   Error error;
340*d5ffbad2SOmair Javaid 
341*d5ffbad2SOmair Javaid   std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
342*d5ffbad2SOmair Javaid   for (auto thread_sp : m_threads) {
343*d5ffbad2SOmair Javaid     assert(thread_sp && "thread list should not have a NULL thread!");
344*d5ffbad2SOmair Javaid     if (!thread_sp)
345*d5ffbad2SOmair Javaid       continue;
346*d5ffbad2SOmair Javaid 
347*d5ffbad2SOmair Javaid     error = thread_sp->RemoveHardwareBreakpoint(addr);
348*d5ffbad2SOmair Javaid   }
349*d5ffbad2SOmair Javaid 
350*d5ffbad2SOmair Javaid   // Also remove from hardware breakpoint map of current process.
351*d5ffbad2SOmair Javaid   m_hw_breakpoints_map.erase(addr);
352*d5ffbad2SOmair Javaid 
353*d5ffbad2SOmair Javaid   return error;
354*d5ffbad2SOmair Javaid }
355*d5ffbad2SOmair Javaid 
356b9c1b51eSKate Stone bool NativeProcessProtocol::RegisterNativeDelegate(
357b9c1b51eSKate Stone     NativeDelegate &native_delegate) {
35816ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_delegates_mutex);
359b9c1b51eSKate Stone   if (std::find(m_delegates.begin(), m_delegates.end(), &native_delegate) !=
360b9c1b51eSKate Stone       m_delegates.end())
361af245d11STodd Fiala     return false;
362af245d11STodd Fiala 
363af245d11STodd Fiala   m_delegates.push_back(&native_delegate);
364af245d11STodd Fiala   native_delegate.InitializeDelegate(this);
365af245d11STodd Fiala   return true;
366af245d11STodd Fiala }
367af245d11STodd Fiala 
368b9c1b51eSKate Stone bool NativeProcessProtocol::UnregisterNativeDelegate(
369b9c1b51eSKate Stone     NativeDelegate &native_delegate) {
37016ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_delegates_mutex);
371af245d11STodd Fiala 
372af245d11STodd Fiala   const auto initial_size = m_delegates.size();
373b9c1b51eSKate Stone   m_delegates.erase(
374b9c1b51eSKate Stone       remove(m_delegates.begin(), m_delegates.end(), &native_delegate),
375b9c1b51eSKate Stone       m_delegates.end());
376af245d11STodd Fiala 
377af245d11STodd Fiala   // We removed the delegate if the count of delegates shrank after
378af245d11STodd Fiala   // removing all copies of the given native_delegate from the vector.
379af245d11STodd Fiala   return m_delegates.size() < initial_size;
380af245d11STodd Fiala }
381af245d11STodd Fiala 
382b9c1b51eSKate Stone void NativeProcessProtocol::SynchronouslyNotifyProcessStateChanged(
383b9c1b51eSKate Stone     lldb::StateType state) {
384af245d11STodd Fiala   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
385af245d11STodd Fiala 
38616ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_delegates_mutex);
387af245d11STodd Fiala   for (auto native_delegate : m_delegates)
388af245d11STodd Fiala     native_delegate->ProcessStateChanged(this, state);
389af245d11STodd Fiala 
390b9c1b51eSKate Stone   if (log) {
391b9c1b51eSKate Stone     if (!m_delegates.empty()) {
392b9c1b51eSKate Stone       log->Printf("NativeProcessProtocol::%s: sent state notification [%s] "
393b9c1b51eSKate Stone                   "from process %" PRIu64,
394af245d11STodd Fiala                   __FUNCTION__, lldb_private::StateAsCString(state), GetID());
395b9c1b51eSKate Stone     } else {
396b9c1b51eSKate Stone       log->Printf("NativeProcessProtocol::%s: would send state notification "
397b9c1b51eSKate Stone                   "[%s] from process %" PRIu64 ", but no delegates",
398af245d11STodd Fiala                   __FUNCTION__, lldb_private::StateAsCString(state), GetID());
399af245d11STodd Fiala     }
400af245d11STodd Fiala   }
401af245d11STodd Fiala }
402af245d11STodd Fiala 
403b9c1b51eSKate Stone void NativeProcessProtocol::NotifyDidExec() {
404a9882ceeSTodd Fiala   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
405a9882ceeSTodd Fiala   if (log)
406b9c1b51eSKate Stone     log->Printf("NativeProcessProtocol::%s - preparing to call delegates",
407b9c1b51eSKate Stone                 __FUNCTION__);
408a9882ceeSTodd Fiala 
409a9882ceeSTodd Fiala   {
41016ff8604SSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(m_delegates_mutex);
411a9882ceeSTodd Fiala     for (auto native_delegate : m_delegates)
412a9882ceeSTodd Fiala       native_delegate->DidExec(this);
413a9882ceeSTodd Fiala   }
414a9882ceeSTodd Fiala }
415a9882ceeSTodd Fiala 
416b9c1b51eSKate Stone Error NativeProcessProtocol::SetSoftwareBreakpoint(lldb::addr_t addr,
417b9c1b51eSKate Stone                                                    uint32_t size_hint) {
418af245d11STodd Fiala   Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_BREAKPOINTS));
419af245d11STodd Fiala   if (log)
420b9c1b51eSKate Stone     log->Printf("NativeProcessProtocol::%s addr = 0x%" PRIx64, __FUNCTION__,
421b9c1b51eSKate Stone                 addr);
422af245d11STodd Fiala 
423b9c1b51eSKate Stone   return m_breakpoint_list.AddRef(
424b9c1b51eSKate Stone       addr, size_hint, false,
425b9c1b51eSKate Stone       [this](lldb::addr_t addr, size_t size_hint, bool /* hardware */,
426b9c1b51eSKate Stone              NativeBreakpointSP &breakpoint_sp) -> Error {
427b9c1b51eSKate Stone         return SoftwareBreakpoint::CreateSoftwareBreakpoint(
428b9c1b51eSKate Stone             *this, addr, size_hint, breakpoint_sp);
429b9c1b51eSKate Stone       });
430af245d11STodd Fiala }
431af245d11STodd Fiala 
432*d5ffbad2SOmair Javaid Error NativeProcessProtocol::RemoveBreakpoint(lldb::addr_t addr,
433*d5ffbad2SOmair Javaid                                               bool hardware) {
434*d5ffbad2SOmair Javaid   if (hardware)
435*d5ffbad2SOmair Javaid     return RemoveHardwareBreakpoint(addr);
436*d5ffbad2SOmair Javaid   else
437af245d11STodd Fiala     return m_breakpoint_list.DecRef(addr);
438af245d11STodd Fiala }
439af245d11STodd Fiala 
440b9c1b51eSKate Stone Error NativeProcessProtocol::EnableBreakpoint(lldb::addr_t addr) {
441af245d11STodd Fiala   return m_breakpoint_list.EnableBreakpoint(addr);
442af245d11STodd Fiala }
443af245d11STodd Fiala 
444b9c1b51eSKate Stone Error NativeProcessProtocol::DisableBreakpoint(lldb::addr_t addr) {
445af245d11STodd Fiala   return m_breakpoint_list.DisableBreakpoint(addr);
446af245d11STodd Fiala }
447af245d11STodd Fiala 
448b9c1b51eSKate Stone lldb::StateType NativeProcessProtocol::GetState() const {
44916ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_state_mutex);
450af245d11STodd Fiala   return m_state;
451af245d11STodd Fiala }
452af245d11STodd Fiala 
453b9c1b51eSKate Stone void NativeProcessProtocol::SetState(lldb::StateType state,
454b9c1b51eSKate Stone                                      bool notify_delegates) {
45516ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_state_mutex);
4565830aa75STamas Berghammer 
4575830aa75STamas Berghammer   if (state == m_state)
4585830aa75STamas Berghammer     return;
4595830aa75STamas Berghammer 
460af245d11STodd Fiala   m_state = state;
461af245d11STodd Fiala 
462b9c1b51eSKate Stone   if (StateIsStoppedState(state, false)) {
463af245d11STodd Fiala     ++m_stop_id;
464af245d11STodd Fiala 
465af245d11STodd Fiala     // Give process a chance to do any stop id bump processing, such as
466af245d11STodd Fiala     // clearing cached data that is invalidated each time the process runs.
467af245d11STodd Fiala     // Note if/when we support some threads running, we'll end up needing
468af245d11STodd Fiala     // to manage this per thread and per process.
469af245d11STodd Fiala     DoStopIDBumped(m_stop_id);
470af245d11STodd Fiala   }
471af245d11STodd Fiala 
472af245d11STodd Fiala   // Optionally notify delegates of the state change.
473af245d11STodd Fiala   if (notify_delegates)
474af245d11STodd Fiala     SynchronouslyNotifyProcessStateChanged(state);
475af245d11STodd Fiala }
476af245d11STodd Fiala 
477b9c1b51eSKate Stone uint32_t NativeProcessProtocol::GetStopID() const {
47816ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_state_mutex);
479af245d11STodd Fiala   return m_stop_id;
480af245d11STodd Fiala }
481af245d11STodd Fiala 
482b9c1b51eSKate Stone void NativeProcessProtocol::DoStopIDBumped(uint32_t /* newBumpId */) {
483af245d11STodd Fiala   // Default implementation does nothing.
484af245d11STodd Fiala }
4858bc34f4dSOleksiy Vyalov 
486b9c1b51eSKate Stone Error NativeProcessProtocol::ResolveProcessArchitecture(lldb::pid_t pid,
487b9c1b51eSKate Stone                                                         ArchSpec &arch) {
488e77fce0aSTodd Fiala   // Grab process info for the running process.
489e77fce0aSTodd Fiala   ProcessInstanceInfo process_info;
490e77fce0aSTodd Fiala   if (!Host::GetProcessInfo(pid, process_info))
491e77fce0aSTodd Fiala     return Error("failed to get process info");
492e77fce0aSTodd Fiala 
493e77fce0aSTodd Fiala   // Resolve the executable module.
494e77fce0aSTodd Fiala   ModuleSpecList module_specs;
495b9c1b51eSKate Stone   if (!ObjectFile::GetModuleSpecifications(process_info.GetExecutableFile(), 0,
496b9c1b51eSKate Stone                                            0, module_specs))
497e77fce0aSTodd Fiala     return Error("failed to get module specifications");
498e77fce0aSTodd Fiala   lldbassert(module_specs.GetSize() == 1);
499e77fce0aSTodd Fiala 
500e77fce0aSTodd Fiala   arch = module_specs.GetModuleSpecRefAtIndex(0).GetArchitecture();
501e77fce0aSTodd Fiala   if (arch.IsValid())
502c1edf566SMehdi Amini     return Error();
503e77fce0aSTodd Fiala   else
504e77fce0aSTodd Fiala     return Error("failed to retrieve a valid architecture from the exe module");
505e77fce0aSTodd Fiala }
506e77fce0aSTodd Fiala 
507d5b310f2SPavel Labath #ifndef __linux__
508b9c1b51eSKate Stone // These need to be implemented to support lldb-gdb-server on a given platform.
509b9c1b51eSKate Stone // Stubs are
510d5b310f2SPavel Labath // provided to make the rest of the code link on non-supported platforms.
511d5b310f2SPavel Labath 
512b9c1b51eSKate Stone Error NativeProcessProtocol::Launch(ProcessLaunchInfo &launch_info,
513d5b310f2SPavel Labath                                     NativeDelegate &native_delegate,
51419cbe96aSPavel Labath                                     MainLoop &mainloop,
515b9c1b51eSKate Stone                                     NativeProcessProtocolSP &process_sp) {
516d5b310f2SPavel Labath   llvm_unreachable("Platform has no NativeProcessProtocol support");
517d5b310f2SPavel Labath }
518d5b310f2SPavel Labath 
519b9c1b51eSKate Stone Error NativeProcessProtocol::Attach(lldb::pid_t pid,
520d5b310f2SPavel Labath                                     NativeDelegate &native_delegate,
52119cbe96aSPavel Labath                                     MainLoop &mainloop,
522b9c1b51eSKate Stone                                     NativeProcessProtocolSP &process_sp) {
523d5b310f2SPavel Labath   llvm_unreachable("Platform has no NativeProcessProtocol support");
524d5b310f2SPavel Labath }
525d5b310f2SPavel Labath 
526d5b310f2SPavel Labath #endif
527