blob: 16071cdae680f54fc8cd87c9719d72b4628e65a9 [file] [log] [blame]
Todd Fialaaf245d12014-06-30 21:05:18 +00001//===-- NativeProcessProtocol.cpp -------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Chaoren Lin2fe1d0a2015-02-03 01:51:38 +000010#include "lldb/Host/common/NativeProcessProtocol.h"
Todd Fialaaf245d12014-06-30 21:05:18 +000011
Todd Fialaaf245d12014-06-30 21:05:18 +000012#include "lldb/Core/ArchSpec.h"
13#include "lldb/Core/Log.h"
Todd Fialae77fce02016-09-04 00:18:56 +000014#include "lldb/Core/ModuleSpec.h"
Todd Fialaaf245d12014-06-30 21:05:18 +000015#include "lldb/Core/State.h"
Todd Fiala511e5cd2014-09-11 23:29:14 +000016#include "lldb/Host/Host.h"
Chaoren Lin2fe1d0a2015-02-03 01:51:38 +000017#include "lldb/Host/common/NativeRegisterContext.h"
Chaoren Lin2fe1d0a2015-02-03 01:51:38 +000018#include "lldb/Host/common/NativeThreadProtocol.h"
19#include "lldb/Host/common/SoftwareBreakpoint.h"
Todd Fialae77fce02016-09-04 00:18:56 +000020#include "lldb/Symbol/ObjectFile.h"
21#include "lldb/Target/Process.h"
22#include "lldb/Utility/LLDBAssert.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000023#include "lldb/lldb-enumerations.h"
Todd Fialaaf245d12014-06-30 21:05:18 +000024
25using namespace lldb;
26using namespace lldb_private;
27
28// -----------------------------------------------------------------------------
29// NativeProcessProtocol Members
30// -----------------------------------------------------------------------------
31
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +000032NativeProcessProtocol::NativeProcessProtocol(lldb::pid_t pid)
Kate Stoneb9c1b512016-09-06 20:57:50 +000033 : m_pid(pid), m_threads(), m_current_thread_id(LLDB_INVALID_THREAD_ID),
34 m_threads_mutex(), m_state(lldb::eStateInvalid), m_state_mutex(),
35 m_exit_type(eExitTypeInvalid), m_exit_status(0), m_exit_description(),
36 m_delegates_mutex(), m_delegates(), m_breakpoint_list(),
37 m_watchpoint_list(), m_terminal_fd(-1), m_stop_id(0) {}
Todd Fialaaf245d12014-06-30 21:05:18 +000038
Kate Stoneb9c1b512016-09-06 20:57:50 +000039lldb_private::Error NativeProcessProtocol::Interrupt() {
40 Error error;
41#if !defined(SIGSTOP)
42 error.SetErrorString("local host does not support signaling");
43 return error;
Todd Fiala511e5cd2014-09-11 23:29:14 +000044#else
Kate Stoneb9c1b512016-09-06 20:57:50 +000045 return Signal(SIGSTOP);
Todd Fiala511e5cd2014-09-11 23:29:14 +000046#endif
47}
48
Pavel Labath4a705e72017-02-24 09:29:14 +000049Error NativeProcessProtocol::IgnoreSignals(llvm::ArrayRef<int> signals) {
50 m_signals_to_ignore.clear();
51 m_signals_to_ignore.insert(signals.begin(), signals.end());
52 return Error();
53}
54
Todd Fiala511e5cd2014-09-11 23:29:14 +000055lldb_private::Error
Kate Stoneb9c1b512016-09-06 20:57:50 +000056NativeProcessProtocol::GetMemoryRegionInfo(lldb::addr_t load_addr,
57 MemoryRegionInfo &range_info) {
58 // Default: not implemented.
59 return Error("not implemented");
Todd Fialaaf245d12014-06-30 21:05:18 +000060}
61
Kate Stoneb9c1b512016-09-06 20:57:50 +000062bool NativeProcessProtocol::GetExitStatus(ExitType *exit_type, int *status,
63 std::string &exit_description) {
64 if (m_state == lldb::eStateExited) {
65 *exit_type = m_exit_type;
66 *status = m_exit_status;
67 exit_description = m_exit_description;
68 return true;
69 }
Todd Fialaaf245d12014-06-30 21:05:18 +000070
Kate Stoneb9c1b512016-09-06 20:57:50 +000071 *status = 0;
72 return false;
Todd Fialaaf245d12014-06-30 21:05:18 +000073}
74
Kate Stoneb9c1b512016-09-06 20:57:50 +000075bool NativeProcessProtocol::SetExitStatus(ExitType exit_type, int status,
76 const char *exit_description,
77 bool bNotifyStateChange) {
78 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
79 if (log)
80 log->Printf("NativeProcessProtocol::%s(%d, %d, %s, %s) called",
81 __FUNCTION__, exit_type, status,
Todd Fialaaf245d12014-06-30 21:05:18 +000082 exit_description ? exit_description : "nullptr",
83 bNotifyStateChange ? "true" : "false");
84
Kate Stoneb9c1b512016-09-06 20:57:50 +000085 // Exit status already set
86 if (m_state == lldb::eStateExited) {
87 if (log)
88 log->Printf("NativeProcessProtocol::%s exit status already set to %d, "
89 "ignoring new set to %d",
90 __FUNCTION__, m_exit_status, status);
91 return false;
92 }
Todd Fialaaf245d12014-06-30 21:05:18 +000093
Kate Stoneb9c1b512016-09-06 20:57:50 +000094 m_state = lldb::eStateExited;
Todd Fialaaf245d12014-06-30 21:05:18 +000095
Kate Stoneb9c1b512016-09-06 20:57:50 +000096 m_exit_type = exit_type;
97 m_exit_status = status;
98 if (exit_description && exit_description[0])
99 m_exit_description = exit_description;
100 else
101 m_exit_description.clear();
Todd Fialaaf245d12014-06-30 21:05:18 +0000102
Kate Stoneb9c1b512016-09-06 20:57:50 +0000103 if (bNotifyStateChange)
104 SynchronouslyNotifyProcessStateChanged(lldb::eStateExited);
Todd Fialaaf245d12014-06-30 21:05:18 +0000105
Kate Stoneb9c1b512016-09-06 20:57:50 +0000106 return true;
107}
108
109NativeThreadProtocolSP NativeProcessProtocol::GetThreadAtIndex(uint32_t idx) {
110 std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
111 if (idx < m_threads.size())
112 return m_threads[idx];
113 return NativeThreadProtocolSP();
Todd Fialaaf245d12014-06-30 21:05:18 +0000114}
115
116NativeThreadProtocolSP
Kate Stoneb9c1b512016-09-06 20:57:50 +0000117NativeProcessProtocol::GetThreadByIDUnlocked(lldb::tid_t tid) {
118 for (auto thread_sp : m_threads) {
119 if (thread_sp->GetID() == tid)
120 return thread_sp;
121 }
122 return NativeThreadProtocolSP();
Todd Fialaaf245d12014-06-30 21:05:18 +0000123}
124
Kate Stoneb9c1b512016-09-06 20:57:50 +0000125NativeThreadProtocolSP NativeProcessProtocol::GetThreadByID(lldb::tid_t tid) {
126 std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
127 return GetThreadByIDUnlocked(tid);
Todd Fialaaf245d12014-06-30 21:05:18 +0000128}
129
Kate Stoneb9c1b512016-09-06 20:57:50 +0000130bool NativeProcessProtocol::IsAlive() const {
131 return m_state != eStateDetached && m_state != eStateExited &&
132 m_state != eStateInvalid && m_state != eStateUnloaded;
Todd Fiala511e5cd2014-09-11 23:29:14 +0000133}
134
Kate Stoneb9c1b512016-09-06 20:57:50 +0000135bool NativeProcessProtocol::GetByteOrder(lldb::ByteOrder &byte_order) const {
136 ArchSpec process_arch;
137 if (!GetArchitecture(process_arch))
138 return false;
139 byte_order = process_arch.GetByteOrder();
140 return true;
Todd Fialaaf245d12014-06-30 21:05:18 +0000141}
142
Kate Stoneb9c1b512016-09-06 20:57:50 +0000143const NativeWatchpointList::WatchpointMap &
144NativeProcessProtocol::GetWatchpointMap() const {
145 return m_watchpoint_list.GetWatchpointMap();
Todd Fialaaf245d12014-06-30 21:05:18 +0000146}
147
Omair Javaidd5ffbad2017-02-24 13:27:31 +0000148llvm::Optional<std::pair<uint32_t, uint32_t>>
149NativeProcessProtocol::GetHardwareDebugSupportInfo() const {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000150 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
151
152 // get any thread
153 NativeThreadProtocolSP thread_sp(
154 const_cast<NativeProcessProtocol *>(this)->GetThreadAtIndex(0));
155 if (!thread_sp) {
156 if (log)
157 log->Warning("NativeProcessProtocol::%s (): failed to find a thread to "
158 "grab a NativeRegisterContext!",
159 __FUNCTION__);
Omair Javaidd5ffbad2017-02-24 13:27:31 +0000160 return llvm::None;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000161 }
162
163 NativeRegisterContextSP reg_ctx_sp(thread_sp->GetRegisterContext());
164 if (!reg_ctx_sp) {
165 if (log)
166 log->Warning("NativeProcessProtocol::%s (): failed to get a "
167 "RegisterContextNativeProcess from the first thread!",
168 __FUNCTION__);
Omair Javaidd5ffbad2017-02-24 13:27:31 +0000169 return llvm::None;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000170 }
171
Omair Javaidd5ffbad2017-02-24 13:27:31 +0000172 return std::make_pair(reg_ctx_sp->NumSupportedHardwareBreakpoints(),
173 reg_ctx_sp->NumSupportedHardwareWatchpoints());
Chaoren Lin18fe6402015-02-03 01:51:47 +0000174}
175
Kate Stoneb9c1b512016-09-06 20:57:50 +0000176Error NativeProcessProtocol::SetWatchpoint(lldb::addr_t addr, size_t size,
177 uint32_t watch_flags,
178 bool hardware) {
179 // This default implementation assumes setting the watchpoint for
180 // the process will require setting the watchpoint for each of the
181 // threads. Furthermore, it will track watchpoints set for the
182 // process and will add them to each thread that is attached to
183 // via the (FIXME implement) OnThreadAttached () method.
Todd Fialaaf245d12014-06-30 21:05:18 +0000184
Kate Stoneb9c1b512016-09-06 20:57:50 +0000185 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
186
187 // Update the thread list
188 UpdateThreads();
189
190 // Keep track of the threads we successfully set the watchpoint
191 // for. If one of the thread watchpoint setting operations fails,
192 // back off and remove the watchpoint for all the threads that
193 // were successfully set so we get back to a consistent state.
194 std::vector<NativeThreadProtocolSP> watchpoint_established_threads;
195
196 // Tell each thread to set a watchpoint. In the event that
197 // hardware watchpoints are requested but the SetWatchpoint fails,
198 // try to set a software watchpoint as a fallback. It's
199 // conceivable that if there are more threads than hardware
200 // watchpoints available, some of the threads will fail to set
201 // hardware watchpoints while software ones may be available.
202 std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
203 for (auto thread_sp : m_threads) {
204 assert(thread_sp && "thread list should not have a NULL thread!");
Todd Fialaaf245d12014-06-30 21:05:18 +0000205 if (!thread_sp)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000206 continue;
207
208 Error thread_error =
209 thread_sp->SetWatchpoint(addr, size, watch_flags, hardware);
210 if (thread_error.Fail() && hardware) {
211 // Try software watchpoints since we failed on hardware watchpoint setting
212 // and we may have just run out of hardware watchpoints.
213 thread_error = thread_sp->SetWatchpoint(addr, size, watch_flags, false);
214 if (thread_error.Success()) {
Todd Fialaaf245d12014-06-30 21:05:18 +0000215 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000216 log->Warning(
217 "hardware watchpoint requested but software watchpoint set");
218 }
Todd Fialaaf245d12014-06-30 21:05:18 +0000219 }
220
Kate Stoneb9c1b512016-09-06 20:57:50 +0000221 if (thread_error.Success()) {
222 // Remember that we set this watchpoint successfully in
223 // case we need to clear it later.
224 watchpoint_established_threads.push_back(thread_sp);
225 } else {
226 // Unset the watchpoint for each thread we successfully
227 // set so that we get back to a consistent state of "not
228 // set" for the watchpoint.
229 for (auto unwatch_thread_sp : watchpoint_established_threads) {
230 Error remove_error = unwatch_thread_sp->RemoveWatchpoint(addr);
231 if (remove_error.Fail() && log) {
232 log->Warning("NativeProcessProtocol::%s (): RemoveWatchpoint failed "
233 "for pid=%" PRIu64 ", tid=%" PRIu64 ": %s",
234 __FUNCTION__, GetID(), unwatch_thread_sp->GetID(),
235 remove_error.AsCString());
236 }
237 }
Todd Fialaaf245d12014-06-30 21:05:18 +0000238
Kate Stoneb9c1b512016-09-06 20:57:50 +0000239 return thread_error;
240 }
241 }
242 return m_watchpoint_list.Add(addr, size, watch_flags, hardware);
Todd Fialaaf245d12014-06-30 21:05:18 +0000243}
244
Kate Stoneb9c1b512016-09-06 20:57:50 +0000245Error NativeProcessProtocol::RemoveWatchpoint(lldb::addr_t addr) {
246 // Update the thread list
247 UpdateThreads();
Todd Fialaaf245d12014-06-30 21:05:18 +0000248
Kate Stoneb9c1b512016-09-06 20:57:50 +0000249 Error overall_error;
Todd Fialaaf245d12014-06-30 21:05:18 +0000250
Kate Stoneb9c1b512016-09-06 20:57:50 +0000251 std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
252 for (auto thread_sp : m_threads) {
253 assert(thread_sp && "thread list should not have a NULL thread!");
254 if (!thread_sp)
255 continue;
Todd Fialaaf245d12014-06-30 21:05:18 +0000256
Kate Stoneb9c1b512016-09-06 20:57:50 +0000257 const Error thread_error = thread_sp->RemoveWatchpoint(addr);
258 if (thread_error.Fail()) {
259 // Keep track of the first thread error if any threads
260 // fail. We want to try to remove the watchpoint from
261 // every thread, though, even if one or more have errors.
262 if (!overall_error.Fail())
263 overall_error = thread_error;
Todd Fialaaf245d12014-06-30 21:05:18 +0000264 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000265 }
266 const Error error = m_watchpoint_list.Remove(addr);
267 return overall_error.Fail() ? overall_error : error;
Todd Fialaaf245d12014-06-30 21:05:18 +0000268}
269
Omair Javaidd5ffbad2017-02-24 13:27:31 +0000270const HardwareBreakpointMap &
271NativeProcessProtocol::GetHardwareBreakpointMap() const {
272 return m_hw_breakpoints_map;
273}
274
275Error NativeProcessProtocol::SetHardwareBreakpoint(lldb::addr_t addr,
276 size_t size) {
277 // This default implementation assumes setting a hardware breakpoint for
278 // this process will require setting same hardware breakpoint for each
279 // of its existing threads. New thread will do the same once created.
280 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
281
282 // Update the thread list
283 UpdateThreads();
284
285 // Exit here if target does not have required hardware breakpoint capability.
286 auto hw_debug_cap = GetHardwareDebugSupportInfo();
287
288 if (hw_debug_cap == llvm::None || hw_debug_cap->first == 0 ||
289 hw_debug_cap->first <= m_hw_breakpoints_map.size())
290 return Error("Target does not have required no of hardware breakpoints");
291
292 // Vector below stores all thread pointer for which we have we successfully
293 // set this hardware breakpoint. If any of the current process threads fails
294 // to set this hardware breakpoint then roll back and remove this breakpoint
295 // for all the threads that had already set it successfully.
296 std::vector<NativeThreadProtocolSP> breakpoint_established_threads;
297
298 // Request to set a hardware breakpoint for each of current process threads.
299 std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
300 for (auto thread_sp : m_threads) {
301 assert(thread_sp && "thread list should not have a NULL thread!");
302 if (!thread_sp)
303 continue;
304
305 Error thread_error = thread_sp->SetHardwareBreakpoint(addr, size);
306 if (thread_error.Success()) {
307 // Remember that we set this breakpoint successfully in
308 // case we need to clear it later.
309 breakpoint_established_threads.push_back(thread_sp);
310 } else {
311 // Unset the breakpoint for each thread we successfully
312 // set so that we get back to a consistent state of "not
313 // set" for this hardware breakpoint.
314 for (auto rollback_thread_sp : breakpoint_established_threads) {
315 Error remove_error = rollback_thread_sp->RemoveHardwareBreakpoint(addr);
316 if (remove_error.Fail() && log) {
317 log->Warning("NativeProcessProtocol::%s (): RemoveHardwareBreakpoint"
318 " failed for pid=%" PRIu64 ", tid=%" PRIu64 ": %s",
319 __FUNCTION__, GetID(), rollback_thread_sp->GetID(),
320 remove_error.AsCString());
321 }
322 }
323
324 return thread_error;
325 }
326 }
327
328 // Register new hardware breakpoint into hardware breakpoints map of current
329 // process.
330 m_hw_breakpoints_map[addr] = {addr, size};
331
332 return Error();
333}
334
335Error NativeProcessProtocol::RemoveHardwareBreakpoint(lldb::addr_t addr) {
336 // Update the thread list
337 UpdateThreads();
338
339 Error error;
340
341 std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
342 for (auto thread_sp : m_threads) {
343 assert(thread_sp && "thread list should not have a NULL thread!");
344 if (!thread_sp)
345 continue;
346
347 error = thread_sp->RemoveHardwareBreakpoint(addr);
348 }
349
350 // Also remove from hardware breakpoint map of current process.
351 m_hw_breakpoints_map.erase(addr);
352
353 return error;
354}
355
Kate Stoneb9c1b512016-09-06 20:57:50 +0000356bool NativeProcessProtocol::RegisterNativeDelegate(
357 NativeDelegate &native_delegate) {
358 std::lock_guard<std::recursive_mutex> guard(m_delegates_mutex);
359 if (std::find(m_delegates.begin(), m_delegates.end(), &native_delegate) !=
360 m_delegates.end())
361 return false;
Todd Fialaaf245d12014-06-30 21:05:18 +0000362
Kate Stoneb9c1b512016-09-06 20:57:50 +0000363 m_delegates.push_back(&native_delegate);
364 native_delegate.InitializeDelegate(this);
365 return true;
Todd Fialaaf245d12014-06-30 21:05:18 +0000366}
367
Kate Stoneb9c1b512016-09-06 20:57:50 +0000368bool NativeProcessProtocol::UnregisterNativeDelegate(
369 NativeDelegate &native_delegate) {
370 std::lock_guard<std::recursive_mutex> guard(m_delegates_mutex);
371
372 const auto initial_size = m_delegates.size();
373 m_delegates.erase(
374 remove(m_delegates.begin(), m_delegates.end(), &native_delegate),
375 m_delegates.end());
376
377 // We removed the delegate if the count of delegates shrank after
378 // removing all copies of the given native_delegate from the vector.
379 return m_delegates.size() < initial_size;
380}
381
382void NativeProcessProtocol::SynchronouslyNotifyProcessStateChanged(
383 lldb::StateType state) {
384 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
385
386 std::lock_guard<std::recursive_mutex> guard(m_delegates_mutex);
387 for (auto native_delegate : m_delegates)
388 native_delegate->ProcessStateChanged(this, state);
389
390 if (log) {
391 if (!m_delegates.empty()) {
392 log->Printf("NativeProcessProtocol::%s: sent state notification [%s] "
393 "from process %" PRIu64,
394 __FUNCTION__, lldb_private::StateAsCString(state), GetID());
395 } else {
396 log->Printf("NativeProcessProtocol::%s: would send state notification "
397 "[%s] from process %" PRIu64 ", but no delegates",
398 __FUNCTION__, lldb_private::StateAsCString(state), GetID());
399 }
400 }
401}
402
403void NativeProcessProtocol::NotifyDidExec() {
404 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
405 if (log)
406 log->Printf("NativeProcessProtocol::%s - preparing to call delegates",
407 __FUNCTION__);
408
409 {
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +0000410 std::lock_guard<std::recursive_mutex> guard(m_delegates_mutex);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000411 for (auto native_delegate : m_delegates)
412 native_delegate->DidExec(this);
413 }
Todd Fialaaf245d12014-06-30 21:05:18 +0000414}
415
Kate Stoneb9c1b512016-09-06 20:57:50 +0000416Error NativeProcessProtocol::SetSoftwareBreakpoint(lldb::addr_t addr,
417 uint32_t size_hint) {
418 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_BREAKPOINTS));
419 if (log)
420 log->Printf("NativeProcessProtocol::%s addr = 0x%" PRIx64, __FUNCTION__,
421 addr);
Todd Fialaaf245d12014-06-30 21:05:18 +0000422
Kate Stoneb9c1b512016-09-06 20:57:50 +0000423 return m_breakpoint_list.AddRef(
424 addr, size_hint, false,
425 [this](lldb::addr_t addr, size_t size_hint, bool /* hardware */,
426 NativeBreakpointSP &breakpoint_sp) -> Error {
427 return SoftwareBreakpoint::CreateSoftwareBreakpoint(
428 *this, addr, size_hint, breakpoint_sp);
429 });
Todd Fialaaf245d12014-06-30 21:05:18 +0000430}
431
Omair Javaidd5ffbad2017-02-24 13:27:31 +0000432Error NativeProcessProtocol::RemoveBreakpoint(lldb::addr_t addr,
433 bool hardware) {
434 if (hardware)
435 return RemoveHardwareBreakpoint(addr);
436 else
437 return m_breakpoint_list.DecRef(addr);
Todd Fialaaf245d12014-06-30 21:05:18 +0000438}
439
Kate Stoneb9c1b512016-09-06 20:57:50 +0000440Error NativeProcessProtocol::EnableBreakpoint(lldb::addr_t addr) {
441 return m_breakpoint_list.EnableBreakpoint(addr);
Todd Fialaa9882ce2014-08-28 15:46:54 +0000442}
443
Kate Stoneb9c1b512016-09-06 20:57:50 +0000444Error NativeProcessProtocol::DisableBreakpoint(lldb::addr_t addr) {
445 return m_breakpoint_list.DisableBreakpoint(addr);
Todd Fialaaf245d12014-06-30 21:05:18 +0000446}
447
Kate Stoneb9c1b512016-09-06 20:57:50 +0000448lldb::StateType NativeProcessProtocol::GetState() const {
449 std::lock_guard<std::recursive_mutex> guard(m_state_mutex);
450 return m_state;
Todd Fialaaf245d12014-06-30 21:05:18 +0000451}
452
Kate Stoneb9c1b512016-09-06 20:57:50 +0000453void NativeProcessProtocol::SetState(lldb::StateType state,
454 bool notify_delegates) {
455 std::lock_guard<std::recursive_mutex> guard(m_state_mutex);
456
457 if (state == m_state)
458 return;
459
460 m_state = state;
461
462 if (StateIsStoppedState(state, false)) {
463 ++m_stop_id;
464
465 // Give process a chance to do any stop id bump processing, such as
466 // clearing cached data that is invalidated each time the process runs.
467 // Note if/when we support some threads running, we'll end up needing
468 // to manage this per thread and per process.
469 DoStopIDBumped(m_stop_id);
470 }
471
472 // Optionally notify delegates of the state change.
473 if (notify_delegates)
474 SynchronouslyNotifyProcessStateChanged(state);
Todd Fialaaf245d12014-06-30 21:05:18 +0000475}
476
Kate Stoneb9c1b512016-09-06 20:57:50 +0000477uint32_t NativeProcessProtocol::GetStopID() const {
478 std::lock_guard<std::recursive_mutex> guard(m_state_mutex);
479 return m_stop_id;
Todd Fialaaf245d12014-06-30 21:05:18 +0000480}
481
Kate Stoneb9c1b512016-09-06 20:57:50 +0000482void NativeProcessProtocol::DoStopIDBumped(uint32_t /* newBumpId */) {
483 // Default implementation does nothing.
Todd Fialaaf245d12014-06-30 21:05:18 +0000484}
485
Kate Stoneb9c1b512016-09-06 20:57:50 +0000486Error NativeProcessProtocol::ResolveProcessArchitecture(lldb::pid_t pid,
487 ArchSpec &arch) {
488 // Grab process info for the running process.
489 ProcessInstanceInfo process_info;
490 if (!Host::GetProcessInfo(pid, process_info))
491 return Error("failed to get process info");
Tamas Berghammer5830aa72015-02-06 10:42:33 +0000492
Kate Stoneb9c1b512016-09-06 20:57:50 +0000493 // Resolve the executable module.
494 ModuleSpecList module_specs;
495 if (!ObjectFile::GetModuleSpecifications(process_info.GetExecutableFile(), 0,
496 0, module_specs))
497 return Error("failed to get module specifications");
498 lldbassert(module_specs.GetSize() == 1);
Tamas Berghammer5830aa72015-02-06 10:42:33 +0000499
Kate Stoneb9c1b512016-09-06 20:57:50 +0000500 arch = module_specs.GetModuleSpecRefAtIndex(0).GetArchitecture();
501 if (arch.IsValid())
Mehdi Aminic1edf562016-11-11 04:29:25 +0000502 return Error();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000503 else
504 return Error("failed to retrieve a valid architecture from the exe module");
Todd Fialae77fce02016-09-04 00:18:56 +0000505}
506
Pavel Labathd5b310f2015-07-09 11:51:11 +0000507#ifndef __linux__
Kate Stoneb9c1b512016-09-06 20:57:50 +0000508// These need to be implemented to support lldb-gdb-server on a given platform.
509// Stubs are
Pavel Labathd5b310f2015-07-09 11:51:11 +0000510// provided to make the rest of the code link on non-supported platforms.
511
Kate Stoneb9c1b512016-09-06 20:57:50 +0000512Error NativeProcessProtocol::Launch(ProcessLaunchInfo &launch_info,
513 NativeDelegate &native_delegate,
514 MainLoop &mainloop,
515 NativeProcessProtocolSP &process_sp) {
516 llvm_unreachable("Platform has no NativeProcessProtocol support");
Pavel Labathd5b310f2015-07-09 11:51:11 +0000517}
518
Kate Stoneb9c1b512016-09-06 20:57:50 +0000519Error NativeProcessProtocol::Attach(lldb::pid_t pid,
520 NativeDelegate &native_delegate,
521 MainLoop &mainloop,
522 NativeProcessProtocolSP &process_sp) {
523 llvm_unreachable("Platform has no NativeProcessProtocol support");
Pavel Labathd5b310f2015-07-09 11:51:11 +0000524}
525
526#endif