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 
49*4a705e7eSPavel Labath Error NativeProcessProtocol::IgnoreSignals(llvm::ArrayRef<int> signals) {
50*4a705e7eSPavel Labath   m_signals_to_ignore.clear();
51*4a705e7eSPavel Labath   m_signals_to_ignore.insert(signals.begin(), signals.end());
52*4a705e7eSPavel Labath   return Error();
53*4a705e7eSPavel Labath }
54*4a705e7eSPavel 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 
148b9c1b51eSKate Stone uint32_t NativeProcessProtocol::GetMaxWatchpoints() const {
149af245d11STodd Fiala   // This default implementation will return the number of
150af245d11STodd Fiala   // *hardware* breakpoints available.  MacOSX and other OS
151af245d11STodd Fiala   // implementations that support software breakpoints will want to
152af245d11STodd Fiala   // override this correctly for their implementation.
153af245d11STodd Fiala   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
154af245d11STodd Fiala 
155af245d11STodd Fiala   // get any thread
156b9c1b51eSKate Stone   NativeThreadProtocolSP thread_sp(
157b9c1b51eSKate Stone       const_cast<NativeProcessProtocol *>(this)->GetThreadAtIndex(0));
158b9c1b51eSKate Stone   if (!thread_sp) {
159af245d11STodd Fiala     if (log)
160b9c1b51eSKate Stone       log->Warning("NativeProcessProtocol::%s (): failed to find a thread to "
161b9c1b51eSKate Stone                    "grab a NativeRegisterContext!",
162b9c1b51eSKate Stone                    __FUNCTION__);
163af245d11STodd Fiala     return 0;
164af245d11STodd Fiala   }
165af245d11STodd Fiala 
166af245d11STodd Fiala   NativeRegisterContextSP reg_ctx_sp(thread_sp->GetRegisterContext());
167b9c1b51eSKate Stone   if (!reg_ctx_sp) {
168af245d11STodd Fiala     if (log)
169b9c1b51eSKate Stone       log->Warning("NativeProcessProtocol::%s (): failed to get a "
170b9c1b51eSKate Stone                    "RegisterContextNativeProcess from the first thread!",
171b9c1b51eSKate Stone                    __FUNCTION__);
172af245d11STodd Fiala     return 0;
173af245d11STodd Fiala   }
174af245d11STodd Fiala 
175af245d11STodd Fiala   return reg_ctx_sp->NumSupportedHardwareWatchpoints();
176af245d11STodd Fiala }
177af245d11STodd Fiala 
178b9c1b51eSKate Stone Error NativeProcessProtocol::SetWatchpoint(lldb::addr_t addr, size_t size,
179b9c1b51eSKate Stone                                            uint32_t watch_flags,
180b9c1b51eSKate Stone                                            bool hardware) {
181af245d11STodd Fiala   // This default implementation assumes setting the watchpoint for
182af245d11STodd Fiala   // the process will require setting the watchpoint for each of the
183af245d11STodd Fiala   // threads.  Furthermore, it will track watchpoints set for the
184af245d11STodd Fiala   // process and will add them to each thread that is attached to
185af245d11STodd Fiala   // via the (FIXME implement) OnThreadAttached () method.
186af245d11STodd Fiala 
187af245d11STodd Fiala   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
188af245d11STodd Fiala 
189af245d11STodd Fiala   // Update the thread list
190af245d11STodd Fiala   UpdateThreads();
191af245d11STodd Fiala 
192af245d11STodd Fiala   // Keep track of the threads we successfully set the watchpoint
193af245d11STodd Fiala   // for.  If one of the thread watchpoint setting operations fails,
194af245d11STodd Fiala   // back off and remove the watchpoint for all the threads that
195af245d11STodd Fiala   // were successfully set so we get back to a consistent state.
196af245d11STodd Fiala   std::vector<NativeThreadProtocolSP> watchpoint_established_threads;
197af245d11STodd Fiala 
198af245d11STodd Fiala   // Tell each thread to set a watchpoint.  In the event that
199af245d11STodd Fiala   // hardware watchpoints are requested but the SetWatchpoint fails,
200af245d11STodd Fiala   // try to set a software watchpoint as a fallback.  It's
201af245d11STodd Fiala   // conceivable that if there are more threads than hardware
202af245d11STodd Fiala   // watchpoints available, some of the threads will fail to set
203af245d11STodd Fiala   // hardware watchpoints while software ones may be available.
20416ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
205b9c1b51eSKate Stone   for (auto thread_sp : m_threads) {
206af245d11STodd Fiala     assert(thread_sp && "thread list should not have a NULL thread!");
207af245d11STodd Fiala     if (!thread_sp)
208af245d11STodd Fiala       continue;
209af245d11STodd Fiala 
210b9c1b51eSKate Stone     Error thread_error =
211b9c1b51eSKate Stone         thread_sp->SetWatchpoint(addr, size, watch_flags, hardware);
212b9c1b51eSKate Stone     if (thread_error.Fail() && hardware) {
213af245d11STodd Fiala       // Try software watchpoints since we failed on hardware watchpoint setting
214af245d11STodd Fiala       // and we may have just run out of hardware watchpoints.
215af245d11STodd Fiala       thread_error = thread_sp->SetWatchpoint(addr, size, watch_flags, false);
216b9c1b51eSKate Stone       if (thread_error.Success()) {
217af245d11STodd Fiala         if (log)
218b9c1b51eSKate Stone           log->Warning(
219b9c1b51eSKate Stone               "hardware watchpoint requested but software watchpoint set");
220af245d11STodd Fiala       }
221af245d11STodd Fiala     }
222af245d11STodd Fiala 
223b9c1b51eSKate Stone     if (thread_error.Success()) {
224af245d11STodd Fiala       // Remember that we set this watchpoint successfully in
225af245d11STodd Fiala       // case we need to clear it later.
226af245d11STodd Fiala       watchpoint_established_threads.push_back(thread_sp);
227b9c1b51eSKate Stone     } else {
228af245d11STodd Fiala       // Unset the watchpoint for each thread we successfully
229af245d11STodd Fiala       // set so that we get back to a consistent state of "not
230af245d11STodd Fiala       // set" for the watchpoint.
231b9c1b51eSKate Stone       for (auto unwatch_thread_sp : watchpoint_established_threads) {
232af245d11STodd Fiala         Error remove_error = unwatch_thread_sp->RemoveWatchpoint(addr);
233b9c1b51eSKate Stone         if (remove_error.Fail() && log) {
234b9c1b51eSKate Stone           log->Warning("NativeProcessProtocol::%s (): RemoveWatchpoint failed "
235b9c1b51eSKate Stone                        "for pid=%" PRIu64 ", tid=%" PRIu64 ": %s",
236b9c1b51eSKate Stone                        __FUNCTION__, GetID(), unwatch_thread_sp->GetID(),
237b9c1b51eSKate Stone                        remove_error.AsCString());
238af245d11STodd Fiala         }
239af245d11STodd Fiala       }
240af245d11STodd Fiala 
241af245d11STodd Fiala       return thread_error;
242af245d11STodd Fiala     }
243af245d11STodd Fiala   }
24418fe6404SChaoren Lin   return m_watchpoint_list.Add(addr, size, watch_flags, hardware);
245af245d11STodd Fiala }
246af245d11STodd Fiala 
247b9c1b51eSKate Stone Error NativeProcessProtocol::RemoveWatchpoint(lldb::addr_t addr) {
248af245d11STodd Fiala   // Update the thread list
249af245d11STodd Fiala   UpdateThreads();
250af245d11STodd Fiala 
251af245d11STodd Fiala   Error overall_error;
252af245d11STodd Fiala 
25316ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
254b9c1b51eSKate Stone   for (auto thread_sp : m_threads) {
255af245d11STodd Fiala     assert(thread_sp && "thread list should not have a NULL thread!");
256af245d11STodd Fiala     if (!thread_sp)
257af245d11STodd Fiala       continue;
258af245d11STodd Fiala 
259af245d11STodd Fiala     const Error thread_error = thread_sp->RemoveWatchpoint(addr);
260b9c1b51eSKate Stone     if (thread_error.Fail()) {
261af245d11STodd Fiala       // Keep track of the first thread error if any threads
262af245d11STodd Fiala       // fail. We want to try to remove the watchpoint from
263af245d11STodd Fiala       // every thread, though, even if one or more have errors.
264af245d11STodd Fiala       if (!overall_error.Fail())
265af245d11STodd Fiala         overall_error = thread_error;
266af245d11STodd Fiala     }
267af245d11STodd Fiala   }
26818fe6404SChaoren Lin   const Error error = m_watchpoint_list.Remove(addr);
26918fe6404SChaoren Lin   return overall_error.Fail() ? overall_error : error;
270af245d11STodd Fiala }
271af245d11STodd Fiala 
272b9c1b51eSKate Stone bool NativeProcessProtocol::RegisterNativeDelegate(
273b9c1b51eSKate Stone     NativeDelegate &native_delegate) {
27416ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_delegates_mutex);
275b9c1b51eSKate Stone   if (std::find(m_delegates.begin(), m_delegates.end(), &native_delegate) !=
276b9c1b51eSKate Stone       m_delegates.end())
277af245d11STodd Fiala     return false;
278af245d11STodd Fiala 
279af245d11STodd Fiala   m_delegates.push_back(&native_delegate);
280af245d11STodd Fiala   native_delegate.InitializeDelegate(this);
281af245d11STodd Fiala   return true;
282af245d11STodd Fiala }
283af245d11STodd Fiala 
284b9c1b51eSKate Stone bool NativeProcessProtocol::UnregisterNativeDelegate(
285b9c1b51eSKate Stone     NativeDelegate &native_delegate) {
28616ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_delegates_mutex);
287af245d11STodd Fiala 
288af245d11STodd Fiala   const auto initial_size = m_delegates.size();
289b9c1b51eSKate Stone   m_delegates.erase(
290b9c1b51eSKate Stone       remove(m_delegates.begin(), m_delegates.end(), &native_delegate),
291b9c1b51eSKate Stone       m_delegates.end());
292af245d11STodd Fiala 
293af245d11STodd Fiala   // We removed the delegate if the count of delegates shrank after
294af245d11STodd Fiala   // removing all copies of the given native_delegate from the vector.
295af245d11STodd Fiala   return m_delegates.size() < initial_size;
296af245d11STodd Fiala }
297af245d11STodd Fiala 
298b9c1b51eSKate Stone void NativeProcessProtocol::SynchronouslyNotifyProcessStateChanged(
299b9c1b51eSKate Stone     lldb::StateType state) {
300af245d11STodd Fiala   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
301af245d11STodd Fiala 
30216ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_delegates_mutex);
303af245d11STodd Fiala   for (auto native_delegate : m_delegates)
304af245d11STodd Fiala     native_delegate->ProcessStateChanged(this, state);
305af245d11STodd Fiala 
306b9c1b51eSKate Stone   if (log) {
307b9c1b51eSKate Stone     if (!m_delegates.empty()) {
308b9c1b51eSKate Stone       log->Printf("NativeProcessProtocol::%s: sent state notification [%s] "
309b9c1b51eSKate Stone                   "from process %" PRIu64,
310af245d11STodd Fiala                   __FUNCTION__, lldb_private::StateAsCString(state), GetID());
311b9c1b51eSKate Stone     } else {
312b9c1b51eSKate Stone       log->Printf("NativeProcessProtocol::%s: would send state notification "
313b9c1b51eSKate Stone                   "[%s] from process %" PRIu64 ", but no delegates",
314af245d11STodd Fiala                   __FUNCTION__, lldb_private::StateAsCString(state), GetID());
315af245d11STodd Fiala     }
316af245d11STodd Fiala   }
317af245d11STodd Fiala }
318af245d11STodd Fiala 
319b9c1b51eSKate Stone void NativeProcessProtocol::NotifyDidExec() {
320a9882ceeSTodd Fiala   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
321a9882ceeSTodd Fiala   if (log)
322b9c1b51eSKate Stone     log->Printf("NativeProcessProtocol::%s - preparing to call delegates",
323b9c1b51eSKate Stone                 __FUNCTION__);
324a9882ceeSTodd Fiala 
325a9882ceeSTodd Fiala   {
32616ff8604SSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(m_delegates_mutex);
327a9882ceeSTodd Fiala     for (auto native_delegate : m_delegates)
328a9882ceeSTodd Fiala       native_delegate->DidExec(this);
329a9882ceeSTodd Fiala   }
330a9882ceeSTodd Fiala }
331a9882ceeSTodd Fiala 
332b9c1b51eSKate Stone Error NativeProcessProtocol::SetSoftwareBreakpoint(lldb::addr_t addr,
333b9c1b51eSKate Stone                                                    uint32_t size_hint) {
334af245d11STodd Fiala   Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_BREAKPOINTS));
335af245d11STodd Fiala   if (log)
336b9c1b51eSKate Stone     log->Printf("NativeProcessProtocol::%s addr = 0x%" PRIx64, __FUNCTION__,
337b9c1b51eSKate Stone                 addr);
338af245d11STodd Fiala 
339b9c1b51eSKate Stone   return m_breakpoint_list.AddRef(
340b9c1b51eSKate Stone       addr, size_hint, false,
341b9c1b51eSKate Stone       [this](lldb::addr_t addr, size_t size_hint, bool /* hardware */,
342b9c1b51eSKate Stone              NativeBreakpointSP &breakpoint_sp) -> Error {
343b9c1b51eSKate Stone         return SoftwareBreakpoint::CreateSoftwareBreakpoint(
344b9c1b51eSKate Stone             *this, addr, size_hint, breakpoint_sp);
345b9c1b51eSKate Stone       });
346af245d11STodd Fiala }
347af245d11STodd Fiala 
348b9c1b51eSKate Stone Error NativeProcessProtocol::RemoveBreakpoint(lldb::addr_t addr) {
349af245d11STodd Fiala   return m_breakpoint_list.DecRef(addr);
350af245d11STodd Fiala }
351af245d11STodd Fiala 
352b9c1b51eSKate Stone Error NativeProcessProtocol::EnableBreakpoint(lldb::addr_t addr) {
353af245d11STodd Fiala   return m_breakpoint_list.EnableBreakpoint(addr);
354af245d11STodd Fiala }
355af245d11STodd Fiala 
356b9c1b51eSKate Stone Error NativeProcessProtocol::DisableBreakpoint(lldb::addr_t addr) {
357af245d11STodd Fiala   return m_breakpoint_list.DisableBreakpoint(addr);
358af245d11STodd Fiala }
359af245d11STodd Fiala 
360b9c1b51eSKate Stone lldb::StateType NativeProcessProtocol::GetState() const {
36116ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_state_mutex);
362af245d11STodd Fiala   return m_state;
363af245d11STodd Fiala }
364af245d11STodd Fiala 
365b9c1b51eSKate Stone void NativeProcessProtocol::SetState(lldb::StateType state,
366b9c1b51eSKate Stone                                      bool notify_delegates) {
36716ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_state_mutex);
3685830aa75STamas Berghammer 
3695830aa75STamas Berghammer   if (state == m_state)
3705830aa75STamas Berghammer     return;
3715830aa75STamas Berghammer 
372af245d11STodd Fiala   m_state = state;
373af245d11STodd Fiala 
374b9c1b51eSKate Stone   if (StateIsStoppedState(state, false)) {
375af245d11STodd Fiala     ++m_stop_id;
376af245d11STodd Fiala 
377af245d11STodd Fiala     // Give process a chance to do any stop id bump processing, such as
378af245d11STodd Fiala     // clearing cached data that is invalidated each time the process runs.
379af245d11STodd Fiala     // Note if/when we support some threads running, we'll end up needing
380af245d11STodd Fiala     // to manage this per thread and per process.
381af245d11STodd Fiala     DoStopIDBumped(m_stop_id);
382af245d11STodd Fiala   }
383af245d11STodd Fiala 
384af245d11STodd Fiala   // Optionally notify delegates of the state change.
385af245d11STodd Fiala   if (notify_delegates)
386af245d11STodd Fiala     SynchronouslyNotifyProcessStateChanged(state);
387af245d11STodd Fiala }
388af245d11STodd Fiala 
389b9c1b51eSKate Stone uint32_t NativeProcessProtocol::GetStopID() const {
39016ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_state_mutex);
391af245d11STodd Fiala   return m_stop_id;
392af245d11STodd Fiala }
393af245d11STodd Fiala 
394b9c1b51eSKate Stone void NativeProcessProtocol::DoStopIDBumped(uint32_t /* newBumpId */) {
395af245d11STodd Fiala   // Default implementation does nothing.
396af245d11STodd Fiala }
3978bc34f4dSOleksiy Vyalov 
398b9c1b51eSKate Stone Error NativeProcessProtocol::ResolveProcessArchitecture(lldb::pid_t pid,
399b9c1b51eSKate Stone                                                         ArchSpec &arch) {
400e77fce0aSTodd Fiala   // Grab process info for the running process.
401e77fce0aSTodd Fiala   ProcessInstanceInfo process_info;
402e77fce0aSTodd Fiala   if (!Host::GetProcessInfo(pid, process_info))
403e77fce0aSTodd Fiala     return Error("failed to get process info");
404e77fce0aSTodd Fiala 
405e77fce0aSTodd Fiala   // Resolve the executable module.
406e77fce0aSTodd Fiala   ModuleSpecList module_specs;
407b9c1b51eSKate Stone   if (!ObjectFile::GetModuleSpecifications(process_info.GetExecutableFile(), 0,
408b9c1b51eSKate Stone                                            0, module_specs))
409e77fce0aSTodd Fiala     return Error("failed to get module specifications");
410e77fce0aSTodd Fiala   lldbassert(module_specs.GetSize() == 1);
411e77fce0aSTodd Fiala 
412e77fce0aSTodd Fiala   arch = module_specs.GetModuleSpecRefAtIndex(0).GetArchitecture();
413e77fce0aSTodd Fiala   if (arch.IsValid())
414c1edf566SMehdi Amini     return Error();
415e77fce0aSTodd Fiala   else
416e77fce0aSTodd Fiala     return Error("failed to retrieve a valid architecture from the exe module");
417e77fce0aSTodd Fiala }
418e77fce0aSTodd Fiala 
419d5b310f2SPavel Labath #ifndef __linux__
420b9c1b51eSKate Stone // These need to be implemented to support lldb-gdb-server on a given platform.
421b9c1b51eSKate Stone // Stubs are
422d5b310f2SPavel Labath // provided to make the rest of the code link on non-supported platforms.
423d5b310f2SPavel Labath 
424b9c1b51eSKate Stone Error NativeProcessProtocol::Launch(ProcessLaunchInfo &launch_info,
425d5b310f2SPavel Labath                                     NativeDelegate &native_delegate,
42619cbe96aSPavel Labath                                     MainLoop &mainloop,
427b9c1b51eSKate Stone                                     NativeProcessProtocolSP &process_sp) {
428d5b310f2SPavel Labath   llvm_unreachable("Platform has no NativeProcessProtocol support");
429d5b310f2SPavel Labath }
430d5b310f2SPavel Labath 
431b9c1b51eSKate Stone Error NativeProcessProtocol::Attach(lldb::pid_t pid,
432d5b310f2SPavel Labath                                     NativeDelegate &native_delegate,
43319cbe96aSPavel Labath                                     MainLoop &mainloop,
434b9c1b51eSKate Stone                                     NativeProcessProtocolSP &process_sp) {
435d5b310f2SPavel Labath   llvm_unreachable("Platform has no NativeProcessProtocol support");
436d5b310f2SPavel Labath }
437d5b310f2SPavel Labath 
438d5b310f2SPavel Labath #endif
439