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 
49511e5cdcSTodd Fiala lldb_private::Error
50b9c1b51eSKate Stone NativeProcessProtocol::GetMemoryRegionInfo(lldb::addr_t load_addr,
51b9c1b51eSKate Stone                                            MemoryRegionInfo &range_info) {
52af245d11STodd Fiala   // Default: not implemented.
53af245d11STodd Fiala   return Error("not implemented");
54af245d11STodd Fiala }
55af245d11STodd Fiala 
56b9c1b51eSKate Stone bool NativeProcessProtocol::GetExitStatus(ExitType *exit_type, int *status,
57b9c1b51eSKate Stone                                           std::string &exit_description) {
58b9c1b51eSKate Stone   if (m_state == lldb::eStateExited) {
59af245d11STodd Fiala     *exit_type = m_exit_type;
60af245d11STodd Fiala     *status = m_exit_status;
61af245d11STodd Fiala     exit_description = m_exit_description;
62af245d11STodd Fiala     return true;
63af245d11STodd Fiala   }
64af245d11STodd Fiala 
65af245d11STodd Fiala   *status = 0;
66af245d11STodd Fiala   return false;
67af245d11STodd Fiala }
68af245d11STodd Fiala 
69b9c1b51eSKate Stone bool NativeProcessProtocol::SetExitStatus(ExitType exit_type, int status,
70b9c1b51eSKate Stone                                           const char *exit_description,
71b9c1b51eSKate Stone                                           bool bNotifyStateChange) {
72af245d11STodd Fiala   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
73af245d11STodd Fiala   if (log)
74af245d11STodd Fiala     log->Printf("NativeProcessProtocol::%s(%d, %d, %s, %s) called",
75b9c1b51eSKate Stone                 __FUNCTION__, exit_type, status,
76af245d11STodd Fiala                 exit_description ? exit_description : "nullptr",
77af245d11STodd Fiala                 bNotifyStateChange ? "true" : "false");
78af245d11STodd Fiala 
79af245d11STodd Fiala   // Exit status already set
80b9c1b51eSKate Stone   if (m_state == lldb::eStateExited) {
81af245d11STodd Fiala     if (log)
82b9c1b51eSKate Stone       log->Printf("NativeProcessProtocol::%s exit status already set to %d, "
83b9c1b51eSKate Stone                   "ignoring new set to %d",
84b9c1b51eSKate Stone                   __FUNCTION__, m_exit_status, status);
85af245d11STodd Fiala     return false;
86af245d11STodd Fiala   }
87af245d11STodd Fiala 
88af245d11STodd Fiala   m_state = lldb::eStateExited;
89af245d11STodd Fiala 
90af245d11STodd Fiala   m_exit_type = exit_type;
91af245d11STodd Fiala   m_exit_status = status;
92af245d11STodd Fiala   if (exit_description && exit_description[0])
93af245d11STodd Fiala     m_exit_description = exit_description;
94af245d11STodd Fiala   else
95af245d11STodd Fiala     m_exit_description.clear();
96af245d11STodd Fiala 
97af245d11STodd Fiala   if (bNotifyStateChange)
98af245d11STodd Fiala     SynchronouslyNotifyProcessStateChanged(lldb::eStateExited);
99af245d11STodd Fiala 
100af245d11STodd Fiala   return true;
101af245d11STodd Fiala }
102af245d11STodd Fiala 
103b9c1b51eSKate Stone NativeThreadProtocolSP NativeProcessProtocol::GetThreadAtIndex(uint32_t idx) {
10416ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
105af245d11STodd Fiala   if (idx < m_threads.size())
106af245d11STodd Fiala     return m_threads[idx];
107af245d11STodd Fiala   return NativeThreadProtocolSP();
108af245d11STodd Fiala }
109af245d11STodd Fiala 
110af245d11STodd Fiala NativeThreadProtocolSP
111b9c1b51eSKate Stone NativeProcessProtocol::GetThreadByIDUnlocked(lldb::tid_t tid) {
112b9c1b51eSKate Stone   for (auto thread_sp : m_threads) {
113af245d11STodd Fiala     if (thread_sp->GetID() == tid)
114af245d11STodd Fiala       return thread_sp;
115af245d11STodd Fiala   }
116af245d11STodd Fiala   return NativeThreadProtocolSP();
117af245d11STodd Fiala }
118af245d11STodd Fiala 
119b9c1b51eSKate Stone NativeThreadProtocolSP NativeProcessProtocol::GetThreadByID(lldb::tid_t tid) {
12016ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
121511e5cdcSTodd Fiala   return GetThreadByIDUnlocked(tid);
122511e5cdcSTodd Fiala }
123511e5cdcSTodd Fiala 
124b9c1b51eSKate Stone bool NativeProcessProtocol::IsAlive() const {
125b9c1b51eSKate Stone   return m_state != eStateDetached && m_state != eStateExited &&
126b9c1b51eSKate Stone          m_state != eStateInvalid && m_state != eStateUnloaded;
127af245d11STodd Fiala }
128af245d11STodd Fiala 
129b9c1b51eSKate Stone bool NativeProcessProtocol::GetByteOrder(lldb::ByteOrder &byte_order) const {
130af245d11STodd Fiala   ArchSpec process_arch;
131af245d11STodd Fiala   if (!GetArchitecture(process_arch))
132af245d11STodd Fiala     return false;
133af245d11STodd Fiala   byte_order = process_arch.GetByteOrder();
134af245d11STodd Fiala   return true;
135af245d11STodd Fiala }
136af245d11STodd Fiala 
13718fe6404SChaoren Lin const NativeWatchpointList::WatchpointMap &
138b9c1b51eSKate Stone NativeProcessProtocol::GetWatchpointMap() const {
13918fe6404SChaoren Lin   return m_watchpoint_list.GetWatchpointMap();
14018fe6404SChaoren Lin }
14118fe6404SChaoren Lin 
142b9c1b51eSKate Stone uint32_t NativeProcessProtocol::GetMaxWatchpoints() const {
143af245d11STodd Fiala   // This default implementation will return the number of
144af245d11STodd Fiala   // *hardware* breakpoints available.  MacOSX and other OS
145af245d11STodd Fiala   // implementations that support software breakpoints will want to
146af245d11STodd Fiala   // override this correctly for their implementation.
147af245d11STodd Fiala   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
148af245d11STodd Fiala 
149af245d11STodd Fiala   // get any thread
150b9c1b51eSKate Stone   NativeThreadProtocolSP thread_sp(
151b9c1b51eSKate Stone       const_cast<NativeProcessProtocol *>(this)->GetThreadAtIndex(0));
152b9c1b51eSKate Stone   if (!thread_sp) {
153af245d11STodd Fiala     if (log)
154b9c1b51eSKate Stone       log->Warning("NativeProcessProtocol::%s (): failed to find a thread to "
155b9c1b51eSKate Stone                    "grab a NativeRegisterContext!",
156b9c1b51eSKate Stone                    __FUNCTION__);
157af245d11STodd Fiala     return 0;
158af245d11STodd Fiala   }
159af245d11STodd Fiala 
160af245d11STodd Fiala   NativeRegisterContextSP reg_ctx_sp(thread_sp->GetRegisterContext());
161b9c1b51eSKate Stone   if (!reg_ctx_sp) {
162af245d11STodd Fiala     if (log)
163b9c1b51eSKate Stone       log->Warning("NativeProcessProtocol::%s (): failed to get a "
164b9c1b51eSKate Stone                    "RegisterContextNativeProcess from the first thread!",
165b9c1b51eSKate Stone                    __FUNCTION__);
166af245d11STodd Fiala     return 0;
167af245d11STodd Fiala   }
168af245d11STodd Fiala 
169af245d11STodd Fiala   return reg_ctx_sp->NumSupportedHardwareWatchpoints();
170af245d11STodd Fiala }
171af245d11STodd Fiala 
172b9c1b51eSKate Stone Error NativeProcessProtocol::SetWatchpoint(lldb::addr_t addr, size_t size,
173b9c1b51eSKate Stone                                            uint32_t watch_flags,
174b9c1b51eSKate Stone                                            bool hardware) {
175af245d11STodd Fiala   // This default implementation assumes setting the watchpoint for
176af245d11STodd Fiala   // the process will require setting the watchpoint for each of the
177af245d11STodd Fiala   // threads.  Furthermore, it will track watchpoints set for the
178af245d11STodd Fiala   // process and will add them to each thread that is attached to
179af245d11STodd Fiala   // via the (FIXME implement) OnThreadAttached () method.
180af245d11STodd Fiala 
181af245d11STodd Fiala   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
182af245d11STodd Fiala 
183af245d11STodd Fiala   // Update the thread list
184af245d11STodd Fiala   UpdateThreads();
185af245d11STodd Fiala 
186af245d11STodd Fiala   // Keep track of the threads we successfully set the watchpoint
187af245d11STodd Fiala   // for.  If one of the thread watchpoint setting operations fails,
188af245d11STodd Fiala   // back off and remove the watchpoint for all the threads that
189af245d11STodd Fiala   // were successfully set so we get back to a consistent state.
190af245d11STodd Fiala   std::vector<NativeThreadProtocolSP> watchpoint_established_threads;
191af245d11STodd Fiala 
192af245d11STodd Fiala   // Tell each thread to set a watchpoint.  In the event that
193af245d11STodd Fiala   // hardware watchpoints are requested but the SetWatchpoint fails,
194af245d11STodd Fiala   // try to set a software watchpoint as a fallback.  It's
195af245d11STodd Fiala   // conceivable that if there are more threads than hardware
196af245d11STodd Fiala   // watchpoints available, some of the threads will fail to set
197af245d11STodd Fiala   // hardware watchpoints while software ones may be available.
19816ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
199b9c1b51eSKate Stone   for (auto thread_sp : m_threads) {
200af245d11STodd Fiala     assert(thread_sp && "thread list should not have a NULL thread!");
201af245d11STodd Fiala     if (!thread_sp)
202af245d11STodd Fiala       continue;
203af245d11STodd Fiala 
204b9c1b51eSKate Stone     Error thread_error =
205b9c1b51eSKate Stone         thread_sp->SetWatchpoint(addr, size, watch_flags, hardware);
206b9c1b51eSKate Stone     if (thread_error.Fail() && hardware) {
207af245d11STodd Fiala       // Try software watchpoints since we failed on hardware watchpoint setting
208af245d11STodd Fiala       // and we may have just run out of hardware watchpoints.
209af245d11STodd Fiala       thread_error = thread_sp->SetWatchpoint(addr, size, watch_flags, false);
210b9c1b51eSKate Stone       if (thread_error.Success()) {
211af245d11STodd Fiala         if (log)
212b9c1b51eSKate Stone           log->Warning(
213b9c1b51eSKate Stone               "hardware watchpoint requested but software watchpoint set");
214af245d11STodd Fiala       }
215af245d11STodd Fiala     }
216af245d11STodd Fiala 
217b9c1b51eSKate Stone     if (thread_error.Success()) {
218af245d11STodd Fiala       // Remember that we set this watchpoint successfully in
219af245d11STodd Fiala       // case we need to clear it later.
220af245d11STodd Fiala       watchpoint_established_threads.push_back(thread_sp);
221b9c1b51eSKate Stone     } else {
222af245d11STodd Fiala       // Unset the watchpoint for each thread we successfully
223af245d11STodd Fiala       // set so that we get back to a consistent state of "not
224af245d11STodd Fiala       // set" for the watchpoint.
225b9c1b51eSKate Stone       for (auto unwatch_thread_sp : watchpoint_established_threads) {
226af245d11STodd Fiala         Error remove_error = unwatch_thread_sp->RemoveWatchpoint(addr);
227b9c1b51eSKate Stone         if (remove_error.Fail() && log) {
228b9c1b51eSKate Stone           log->Warning("NativeProcessProtocol::%s (): RemoveWatchpoint failed "
229b9c1b51eSKate Stone                        "for pid=%" PRIu64 ", tid=%" PRIu64 ": %s",
230b9c1b51eSKate Stone                        __FUNCTION__, GetID(), unwatch_thread_sp->GetID(),
231b9c1b51eSKate Stone                        remove_error.AsCString());
232af245d11STodd Fiala         }
233af245d11STodd Fiala       }
234af245d11STodd Fiala 
235af245d11STodd Fiala       return thread_error;
236af245d11STodd Fiala     }
237af245d11STodd Fiala   }
23818fe6404SChaoren Lin   return m_watchpoint_list.Add(addr, size, watch_flags, hardware);
239af245d11STodd Fiala }
240af245d11STodd Fiala 
241b9c1b51eSKate Stone Error NativeProcessProtocol::RemoveWatchpoint(lldb::addr_t addr) {
242af245d11STodd Fiala   // Update the thread list
243af245d11STodd Fiala   UpdateThreads();
244af245d11STodd Fiala 
245af245d11STodd Fiala   Error overall_error;
246af245d11STodd Fiala 
24716ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
248b9c1b51eSKate Stone   for (auto thread_sp : m_threads) {
249af245d11STodd Fiala     assert(thread_sp && "thread list should not have a NULL thread!");
250af245d11STodd Fiala     if (!thread_sp)
251af245d11STodd Fiala       continue;
252af245d11STodd Fiala 
253af245d11STodd Fiala     const Error thread_error = thread_sp->RemoveWatchpoint(addr);
254b9c1b51eSKate Stone     if (thread_error.Fail()) {
255af245d11STodd Fiala       // Keep track of the first thread error if any threads
256af245d11STodd Fiala       // fail. We want to try to remove the watchpoint from
257af245d11STodd Fiala       // every thread, though, even if one or more have errors.
258af245d11STodd Fiala       if (!overall_error.Fail())
259af245d11STodd Fiala         overall_error = thread_error;
260af245d11STodd Fiala     }
261af245d11STodd Fiala   }
26218fe6404SChaoren Lin   const Error error = m_watchpoint_list.Remove(addr);
26318fe6404SChaoren Lin   return overall_error.Fail() ? overall_error : error;
264af245d11STodd Fiala }
265af245d11STodd Fiala 
266b9c1b51eSKate Stone bool NativeProcessProtocol::RegisterNativeDelegate(
267b9c1b51eSKate Stone     NativeDelegate &native_delegate) {
26816ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_delegates_mutex);
269b9c1b51eSKate Stone   if (std::find(m_delegates.begin(), m_delegates.end(), &native_delegate) !=
270b9c1b51eSKate Stone       m_delegates.end())
271af245d11STodd Fiala     return false;
272af245d11STodd Fiala 
273af245d11STodd Fiala   m_delegates.push_back(&native_delegate);
274af245d11STodd Fiala   native_delegate.InitializeDelegate(this);
275af245d11STodd Fiala   return true;
276af245d11STodd Fiala }
277af245d11STodd Fiala 
278b9c1b51eSKate Stone bool NativeProcessProtocol::UnregisterNativeDelegate(
279b9c1b51eSKate Stone     NativeDelegate &native_delegate) {
28016ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_delegates_mutex);
281af245d11STodd Fiala 
282af245d11STodd Fiala   const auto initial_size = m_delegates.size();
283b9c1b51eSKate Stone   m_delegates.erase(
284b9c1b51eSKate Stone       remove(m_delegates.begin(), m_delegates.end(), &native_delegate),
285b9c1b51eSKate Stone       m_delegates.end());
286af245d11STodd Fiala 
287af245d11STodd Fiala   // We removed the delegate if the count of delegates shrank after
288af245d11STodd Fiala   // removing all copies of the given native_delegate from the vector.
289af245d11STodd Fiala   return m_delegates.size() < initial_size;
290af245d11STodd Fiala }
291af245d11STodd Fiala 
292b9c1b51eSKate Stone void NativeProcessProtocol::SynchronouslyNotifyProcessStateChanged(
293b9c1b51eSKate Stone     lldb::StateType state) {
294af245d11STodd Fiala   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
295af245d11STodd Fiala 
29616ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_delegates_mutex);
297af245d11STodd Fiala   for (auto native_delegate : m_delegates)
298af245d11STodd Fiala     native_delegate->ProcessStateChanged(this, state);
299af245d11STodd Fiala 
300b9c1b51eSKate Stone   if (log) {
301b9c1b51eSKate Stone     if (!m_delegates.empty()) {
302b9c1b51eSKate Stone       log->Printf("NativeProcessProtocol::%s: sent state notification [%s] "
303b9c1b51eSKate Stone                   "from process %" PRIu64,
304af245d11STodd Fiala                   __FUNCTION__, lldb_private::StateAsCString(state), GetID());
305b9c1b51eSKate Stone     } else {
306b9c1b51eSKate Stone       log->Printf("NativeProcessProtocol::%s: would send state notification "
307b9c1b51eSKate Stone                   "[%s] from process %" PRIu64 ", but no delegates",
308af245d11STodd Fiala                   __FUNCTION__, lldb_private::StateAsCString(state), GetID());
309af245d11STodd Fiala     }
310af245d11STodd Fiala   }
311af245d11STodd Fiala }
312af245d11STodd Fiala 
313b9c1b51eSKate Stone void NativeProcessProtocol::NotifyDidExec() {
314a9882ceeSTodd Fiala   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
315a9882ceeSTodd Fiala   if (log)
316b9c1b51eSKate Stone     log->Printf("NativeProcessProtocol::%s - preparing to call delegates",
317b9c1b51eSKate Stone                 __FUNCTION__);
318a9882ceeSTodd Fiala 
319a9882ceeSTodd Fiala   {
32016ff8604SSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(m_delegates_mutex);
321a9882ceeSTodd Fiala     for (auto native_delegate : m_delegates)
322a9882ceeSTodd Fiala       native_delegate->DidExec(this);
323a9882ceeSTodd Fiala   }
324a9882ceeSTodd Fiala }
325a9882ceeSTodd Fiala 
326b9c1b51eSKate Stone Error NativeProcessProtocol::SetSoftwareBreakpoint(lldb::addr_t addr,
327b9c1b51eSKate Stone                                                    uint32_t size_hint) {
328af245d11STodd Fiala   Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_BREAKPOINTS));
329af245d11STodd Fiala   if (log)
330b9c1b51eSKate Stone     log->Printf("NativeProcessProtocol::%s addr = 0x%" PRIx64, __FUNCTION__,
331b9c1b51eSKate Stone                 addr);
332af245d11STodd Fiala 
333b9c1b51eSKate Stone   return m_breakpoint_list.AddRef(
334b9c1b51eSKate Stone       addr, size_hint, false,
335b9c1b51eSKate Stone       [this](lldb::addr_t addr, size_t size_hint, bool /* hardware */,
336b9c1b51eSKate Stone              NativeBreakpointSP &breakpoint_sp) -> Error {
337b9c1b51eSKate Stone         return SoftwareBreakpoint::CreateSoftwareBreakpoint(
338b9c1b51eSKate Stone             *this, addr, size_hint, breakpoint_sp);
339b9c1b51eSKate Stone       });
340af245d11STodd Fiala }
341af245d11STodd Fiala 
342b9c1b51eSKate Stone Error NativeProcessProtocol::RemoveBreakpoint(lldb::addr_t addr) {
343af245d11STodd Fiala   return m_breakpoint_list.DecRef(addr);
344af245d11STodd Fiala }
345af245d11STodd Fiala 
346b9c1b51eSKate Stone Error NativeProcessProtocol::EnableBreakpoint(lldb::addr_t addr) {
347af245d11STodd Fiala   return m_breakpoint_list.EnableBreakpoint(addr);
348af245d11STodd Fiala }
349af245d11STodd Fiala 
350b9c1b51eSKate Stone Error NativeProcessProtocol::DisableBreakpoint(lldb::addr_t addr) {
351af245d11STodd Fiala   return m_breakpoint_list.DisableBreakpoint(addr);
352af245d11STodd Fiala }
353af245d11STodd Fiala 
354b9c1b51eSKate Stone lldb::StateType NativeProcessProtocol::GetState() const {
35516ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_state_mutex);
356af245d11STodd Fiala   return m_state;
357af245d11STodd Fiala }
358af245d11STodd Fiala 
359b9c1b51eSKate Stone void NativeProcessProtocol::SetState(lldb::StateType state,
360b9c1b51eSKate Stone                                      bool notify_delegates) {
36116ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_state_mutex);
3625830aa75STamas Berghammer 
3635830aa75STamas Berghammer   if (state == m_state)
3645830aa75STamas Berghammer     return;
3655830aa75STamas Berghammer 
366af245d11STodd Fiala   m_state = state;
367af245d11STodd Fiala 
368b9c1b51eSKate Stone   if (StateIsStoppedState(state, false)) {
369af245d11STodd Fiala     ++m_stop_id;
370af245d11STodd Fiala 
371af245d11STodd Fiala     // Give process a chance to do any stop id bump processing, such as
372af245d11STodd Fiala     // clearing cached data that is invalidated each time the process runs.
373af245d11STodd Fiala     // Note if/when we support some threads running, we'll end up needing
374af245d11STodd Fiala     // to manage this per thread and per process.
375af245d11STodd Fiala     DoStopIDBumped(m_stop_id);
376af245d11STodd Fiala   }
377af245d11STodd Fiala 
378af245d11STodd Fiala   // Optionally notify delegates of the state change.
379af245d11STodd Fiala   if (notify_delegates)
380af245d11STodd Fiala     SynchronouslyNotifyProcessStateChanged(state);
381af245d11STodd Fiala }
382af245d11STodd Fiala 
383b9c1b51eSKate Stone uint32_t NativeProcessProtocol::GetStopID() const {
38416ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_state_mutex);
385af245d11STodd Fiala   return m_stop_id;
386af245d11STodd Fiala }
387af245d11STodd Fiala 
388b9c1b51eSKate Stone void NativeProcessProtocol::DoStopIDBumped(uint32_t /* newBumpId */) {
389af245d11STodd Fiala   // Default implementation does nothing.
390af245d11STodd Fiala }
3918bc34f4dSOleksiy Vyalov 
392b9c1b51eSKate Stone Error NativeProcessProtocol::ResolveProcessArchitecture(lldb::pid_t pid,
393b9c1b51eSKate Stone                                                         ArchSpec &arch) {
394e77fce0aSTodd Fiala   // Grab process info for the running process.
395e77fce0aSTodd Fiala   ProcessInstanceInfo process_info;
396e77fce0aSTodd Fiala   if (!Host::GetProcessInfo(pid, process_info))
397e77fce0aSTodd Fiala     return Error("failed to get process info");
398e77fce0aSTodd Fiala 
399e77fce0aSTodd Fiala   // Resolve the executable module.
400e77fce0aSTodd Fiala   ModuleSpecList module_specs;
401b9c1b51eSKate Stone   if (!ObjectFile::GetModuleSpecifications(process_info.GetExecutableFile(), 0,
402b9c1b51eSKate Stone                                            0, module_specs))
403e77fce0aSTodd Fiala     return Error("failed to get module specifications");
404e77fce0aSTodd Fiala   lldbassert(module_specs.GetSize() == 1);
405e77fce0aSTodd Fiala 
406e77fce0aSTodd Fiala   arch = module_specs.GetModuleSpecRefAtIndex(0).GetArchitecture();
407e77fce0aSTodd Fiala   if (arch.IsValid())
408*c1edf566SMehdi Amini     return Error();
409e77fce0aSTodd Fiala   else
410e77fce0aSTodd Fiala     return Error("failed to retrieve a valid architecture from the exe module");
411e77fce0aSTodd Fiala }
412e77fce0aSTodd Fiala 
413d5b310f2SPavel Labath #ifndef __linux__
414b9c1b51eSKate Stone // These need to be implemented to support lldb-gdb-server on a given platform.
415b9c1b51eSKate Stone // Stubs are
416d5b310f2SPavel Labath // provided to make the rest of the code link on non-supported platforms.
417d5b310f2SPavel Labath 
418b9c1b51eSKate Stone Error NativeProcessProtocol::Launch(ProcessLaunchInfo &launch_info,
419d5b310f2SPavel Labath                                     NativeDelegate &native_delegate,
42019cbe96aSPavel Labath                                     MainLoop &mainloop,
421b9c1b51eSKate Stone                                     NativeProcessProtocolSP &process_sp) {
422d5b310f2SPavel Labath   llvm_unreachable("Platform has no NativeProcessProtocol support");
423d5b310f2SPavel Labath }
424d5b310f2SPavel Labath 
425b9c1b51eSKate Stone Error NativeProcessProtocol::Attach(lldb::pid_t pid,
426d5b310f2SPavel Labath                                     NativeDelegate &native_delegate,
42719cbe96aSPavel Labath                                     MainLoop &mainloop,
428b9c1b51eSKate Stone                                     NativeProcessProtocolSP &process_sp) {
429d5b310f2SPavel Labath   llvm_unreachable("Platform has no NativeProcessProtocol support");
430d5b310f2SPavel Labath }
431d5b310f2SPavel Labath 
432d5b310f2SPavel Labath #endif
433