blob: 1fcb11b8b6f5d729cae4dc8dc2978fea92a6a885 [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 Fialae77fce02016-09-04 00:18:56 +000011#include "lldb/Core/ModuleSpec.h"
Todd Fialaaf245d12014-06-30 21:05:18 +000012#include "lldb/Core/State.h"
Todd Fiala511e5cd2014-09-11 23:29:14 +000013#include "lldb/Host/Host.h"
Chaoren Lin2fe1d0a2015-02-03 01:51:38 +000014#include "lldb/Host/common/NativeRegisterContext.h"
Chaoren Lin2fe1d0a2015-02-03 01:51:38 +000015#include "lldb/Host/common/NativeThreadProtocol.h"
16#include "lldb/Host/common/SoftwareBreakpoint.h"
Todd Fialae77fce02016-09-04 00:18:56 +000017#include "lldb/Symbol/ObjectFile.h"
18#include "lldb/Target/Process.h"
19#include "lldb/Utility/LLDBAssert.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000020#include "lldb/Utility/Log.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000021#include "lldb/lldb-enumerations.h"
Todd Fialaaf245d12014-06-30 21:05:18 +000022
23using namespace lldb;
24using namespace lldb_private;
25
26// -----------------------------------------------------------------------------
27// NativeProcessProtocol Members
28// -----------------------------------------------------------------------------
29
Pavel Labath96e600f2017-07-07 11:02:19 +000030NativeProcessProtocol::NativeProcessProtocol(lldb::pid_t pid, int terminal_fd,
31 NativeDelegate &delegate)
32 : m_pid(pid), m_terminal_fd(terminal_fd) {
33 bool registered = RegisterNativeDelegate(delegate);
34 assert(registered);
35 (void)registered;
36}
Todd Fialaaf245d12014-06-30 21:05:18 +000037
Zachary Turner97206d52017-05-12 04:51:55 +000038lldb_private::Status NativeProcessProtocol::Interrupt() {
39 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +000040#if !defined(SIGSTOP)
41 error.SetErrorString("local host does not support signaling");
42 return error;
Todd Fiala511e5cd2014-09-11 23:29:14 +000043#else
Kate Stoneb9c1b512016-09-06 20:57:50 +000044 return Signal(SIGSTOP);
Todd Fiala511e5cd2014-09-11 23:29:14 +000045#endif
46}
47
Zachary Turner97206d52017-05-12 04:51:55 +000048Status NativeProcessProtocol::IgnoreSignals(llvm::ArrayRef<int> signals) {
Pavel Labath4a705e72017-02-24 09:29:14 +000049 m_signals_to_ignore.clear();
50 m_signals_to_ignore.insert(signals.begin(), signals.end());
Zachary Turner97206d52017-05-12 04:51:55 +000051 return Status();
Pavel Labath4a705e72017-02-24 09:29:14 +000052}
53
Zachary Turner97206d52017-05-12 04:51:55 +000054lldb_private::Status
Kate Stoneb9c1b512016-09-06 20:57:50 +000055NativeProcessProtocol::GetMemoryRegionInfo(lldb::addr_t load_addr,
56 MemoryRegionInfo &range_info) {
57 // Default: not implemented.
Zachary Turner97206d52017-05-12 04:51:55 +000058 return Status("not implemented");
Todd Fialaaf245d12014-06-30 21:05:18 +000059}
60
Pavel Labath3508fc82017-06-19 12:47:50 +000061llvm::Optional<WaitStatus> NativeProcessProtocol::GetExitStatus() {
62 if (m_state == lldb::eStateExited)
63 return m_exit_status;
Todd Fialaaf245d12014-06-30 21:05:18 +000064
Pavel Labath3508fc82017-06-19 12:47:50 +000065 return llvm::None;
Todd Fialaaf245d12014-06-30 21:05:18 +000066}
67
Pavel Labath3508fc82017-06-19 12:47:50 +000068bool NativeProcessProtocol::SetExitStatus(WaitStatus status,
Kate Stoneb9c1b512016-09-06 20:57:50 +000069 bool bNotifyStateChange) {
70 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
Pavel Labath3508fc82017-06-19 12:47:50 +000071 LLDB_LOG(log, "status = {0}, notify = {1}", status, bNotifyStateChange);
Todd Fialaaf245d12014-06-30 21:05:18 +000072
Kate Stoneb9c1b512016-09-06 20:57:50 +000073 // Exit status already set
74 if (m_state == lldb::eStateExited) {
Pavel Labath3508fc82017-06-19 12:47:50 +000075 if (m_exit_status)
76 LLDB_LOG(log, "exit status already set to {0}", *m_exit_status);
77 else
78 LLDB_LOG(log, "state is exited, but status not set");
Kate Stoneb9c1b512016-09-06 20:57:50 +000079 return false;
80 }
Todd Fialaaf245d12014-06-30 21:05:18 +000081
Kate Stoneb9c1b512016-09-06 20:57:50 +000082 m_state = lldb::eStateExited;
Kate Stoneb9c1b512016-09-06 20:57:50 +000083 m_exit_status = status;
Todd Fialaaf245d12014-06-30 21:05:18 +000084
Kate Stoneb9c1b512016-09-06 20:57:50 +000085 if (bNotifyStateChange)
86 SynchronouslyNotifyProcessStateChanged(lldb::eStateExited);
Todd Fialaaf245d12014-06-30 21:05:18 +000087
Kate Stoneb9c1b512016-09-06 20:57:50 +000088 return true;
89}
90
Pavel Labatha5be48b2017-10-17 15:52:16 +000091NativeThreadProtocol *NativeProcessProtocol::GetThreadAtIndex(uint32_t idx) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000092 std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
93 if (idx < m_threads.size())
Pavel Labatha5be48b2017-10-17 15:52:16 +000094 return m_threads[idx].get();
95 return nullptr;
Todd Fialaaf245d12014-06-30 21:05:18 +000096}
97
Pavel Labatha5be48b2017-10-17 15:52:16 +000098NativeThreadProtocol *
Kate Stoneb9c1b512016-09-06 20:57:50 +000099NativeProcessProtocol::GetThreadByIDUnlocked(lldb::tid_t tid) {
Pavel Labatha5be48b2017-10-17 15:52:16 +0000100 for (const auto &thread : m_threads) {
101 if (thread->GetID() == tid)
102 return thread.get();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000103 }
Pavel Labatha5be48b2017-10-17 15:52:16 +0000104 return nullptr;
Todd Fialaaf245d12014-06-30 21:05:18 +0000105}
106
Pavel Labatha5be48b2017-10-17 15:52:16 +0000107NativeThreadProtocol *NativeProcessProtocol::GetThreadByID(lldb::tid_t tid) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000108 std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
109 return GetThreadByIDUnlocked(tid);
Todd Fialaaf245d12014-06-30 21:05:18 +0000110}
111
Kate Stoneb9c1b512016-09-06 20:57:50 +0000112bool NativeProcessProtocol::IsAlive() const {
113 return m_state != eStateDetached && m_state != eStateExited &&
114 m_state != eStateInvalid && m_state != eStateUnloaded;
Todd Fiala511e5cd2014-09-11 23:29:14 +0000115}
116
Kate Stoneb9c1b512016-09-06 20:57:50 +0000117const NativeWatchpointList::WatchpointMap &
118NativeProcessProtocol::GetWatchpointMap() const {
119 return m_watchpoint_list.GetWatchpointMap();
Todd Fialaaf245d12014-06-30 21:05:18 +0000120}
121
Omair Javaidd5ffbad2017-02-24 13:27:31 +0000122llvm::Optional<std::pair<uint32_t, uint32_t>>
123NativeProcessProtocol::GetHardwareDebugSupportInfo() const {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000124 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
125
126 // get any thread
Pavel Labatha5be48b2017-10-17 15:52:16 +0000127 NativeThreadProtocol *thread(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000128 const_cast<NativeProcessProtocol *>(this)->GetThreadAtIndex(0));
Pavel Labatha5be48b2017-10-17 15:52:16 +0000129 if (!thread) {
130 LLDB_LOG(log, "failed to find a thread to grab a NativeRegisterContext!");
Omair Javaidd5ffbad2017-02-24 13:27:31 +0000131 return llvm::None;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000132 }
133
Pavel Labathd37349f2017-11-10 11:05:49 +0000134 NativeRegisterContext &reg_ctx = thread->GetRegisterContext();
135 return std::make_pair(reg_ctx.NumSupportedHardwareBreakpoints(),
136 reg_ctx.NumSupportedHardwareWatchpoints());
Chaoren Lin18fe6402015-02-03 01:51:47 +0000137}
138
Zachary Turner97206d52017-05-12 04:51:55 +0000139Status NativeProcessProtocol::SetWatchpoint(lldb::addr_t addr, size_t size,
140 uint32_t watch_flags,
141 bool hardware) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000142 // This default implementation assumes setting the watchpoint for
143 // the process will require setting the watchpoint for each of the
144 // threads. Furthermore, it will track watchpoints set for the
145 // process and will add them to each thread that is attached to
146 // via the (FIXME implement) OnThreadAttached () method.
Todd Fialaaf245d12014-06-30 21:05:18 +0000147
Kate Stoneb9c1b512016-09-06 20:57:50 +0000148 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
149
150 // Update the thread list
151 UpdateThreads();
152
153 // Keep track of the threads we successfully set the watchpoint
154 // for. If one of the thread watchpoint setting operations fails,
155 // back off and remove the watchpoint for all the threads that
156 // were successfully set so we get back to a consistent state.
Pavel Labatha5be48b2017-10-17 15:52:16 +0000157 std::vector<NativeThreadProtocol *> watchpoint_established_threads;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000158
159 // Tell each thread to set a watchpoint. In the event that
160 // hardware watchpoints are requested but the SetWatchpoint fails,
161 // try to set a software watchpoint as a fallback. It's
162 // conceivable that if there are more threads than hardware
163 // watchpoints available, some of the threads will fail to set
164 // hardware watchpoints while software ones may be available.
165 std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
Pavel Labatha5be48b2017-10-17 15:52:16 +0000166 for (const auto &thread : m_threads) {
167 assert(thread && "thread list should not have a NULL thread!");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000168
Zachary Turner97206d52017-05-12 04:51:55 +0000169 Status thread_error =
Pavel Labatha5be48b2017-10-17 15:52:16 +0000170 thread->SetWatchpoint(addr, size, watch_flags, hardware);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000171 if (thread_error.Fail() && hardware) {
172 // Try software watchpoints since we failed on hardware watchpoint setting
173 // and we may have just run out of hardware watchpoints.
Pavel Labatha5be48b2017-10-17 15:52:16 +0000174 thread_error = thread->SetWatchpoint(addr, size, watch_flags, false);
175 if (thread_error.Success())
176 LLDB_LOG(log,
177 "hardware watchpoint requested but software watchpoint set");
Todd Fialaaf245d12014-06-30 21:05:18 +0000178 }
179
Kate Stoneb9c1b512016-09-06 20:57:50 +0000180 if (thread_error.Success()) {
181 // Remember that we set this watchpoint successfully in
182 // case we need to clear it later.
Pavel Labatha5be48b2017-10-17 15:52:16 +0000183 watchpoint_established_threads.push_back(thread.get());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000184 } else {
185 // Unset the watchpoint for each thread we successfully
186 // set so that we get back to a consistent state of "not
187 // set" for the watchpoint.
188 for (auto unwatch_thread_sp : watchpoint_established_threads) {
Zachary Turner97206d52017-05-12 04:51:55 +0000189 Status remove_error = unwatch_thread_sp->RemoveWatchpoint(addr);
Pavel Labatha5be48b2017-10-17 15:52:16 +0000190 if (remove_error.Fail())
191 LLDB_LOG(log, "RemoveWatchpoint failed for pid={0}, tid={1}: {2}",
192 GetID(), unwatch_thread_sp->GetID(), remove_error);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000193 }
Todd Fialaaf245d12014-06-30 21:05:18 +0000194
Kate Stoneb9c1b512016-09-06 20:57:50 +0000195 return thread_error;
196 }
197 }
198 return m_watchpoint_list.Add(addr, size, watch_flags, hardware);
Todd Fialaaf245d12014-06-30 21:05:18 +0000199}
200
Zachary Turner97206d52017-05-12 04:51:55 +0000201Status NativeProcessProtocol::RemoveWatchpoint(lldb::addr_t addr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000202 // Update the thread list
203 UpdateThreads();
Todd Fialaaf245d12014-06-30 21:05:18 +0000204
Zachary Turner97206d52017-05-12 04:51:55 +0000205 Status overall_error;
Todd Fialaaf245d12014-06-30 21:05:18 +0000206
Kate Stoneb9c1b512016-09-06 20:57:50 +0000207 std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
Pavel Labatha5be48b2017-10-17 15:52:16 +0000208 for (const auto &thread : m_threads) {
209 assert(thread && "thread list should not have a NULL thread!");
Todd Fialaaf245d12014-06-30 21:05:18 +0000210
Pavel Labatha5be48b2017-10-17 15:52:16 +0000211 const Status thread_error = thread->RemoveWatchpoint(addr);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000212 if (thread_error.Fail()) {
213 // Keep track of the first thread error if any threads
214 // fail. We want to try to remove the watchpoint from
215 // every thread, though, even if one or more have errors.
216 if (!overall_error.Fail())
217 overall_error = thread_error;
Todd Fialaaf245d12014-06-30 21:05:18 +0000218 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000219 }
Zachary Turner97206d52017-05-12 04:51:55 +0000220 const Status error = m_watchpoint_list.Remove(addr);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000221 return overall_error.Fail() ? overall_error : error;
Todd Fialaaf245d12014-06-30 21:05:18 +0000222}
223
Omair Javaidd5ffbad2017-02-24 13:27:31 +0000224const HardwareBreakpointMap &
225NativeProcessProtocol::GetHardwareBreakpointMap() const {
226 return m_hw_breakpoints_map;
227}
228
Zachary Turner97206d52017-05-12 04:51:55 +0000229Status NativeProcessProtocol::SetHardwareBreakpoint(lldb::addr_t addr,
230 size_t size) {
Omair Javaidd5ffbad2017-02-24 13:27:31 +0000231 // This default implementation assumes setting a hardware breakpoint for
232 // this process will require setting same hardware breakpoint for each
233 // of its existing threads. New thread will do the same once created.
234 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
235
236 // Update the thread list
237 UpdateThreads();
238
239 // Exit here if target does not have required hardware breakpoint capability.
240 auto hw_debug_cap = GetHardwareDebugSupportInfo();
241
242 if (hw_debug_cap == llvm::None || hw_debug_cap->first == 0 ||
243 hw_debug_cap->first <= m_hw_breakpoints_map.size())
Zachary Turner97206d52017-05-12 04:51:55 +0000244 return Status("Target does not have required no of hardware breakpoints");
Omair Javaidd5ffbad2017-02-24 13:27:31 +0000245
246 // Vector below stores all thread pointer for which we have we successfully
247 // set this hardware breakpoint. If any of the current process threads fails
248 // to set this hardware breakpoint then roll back and remove this breakpoint
249 // for all the threads that had already set it successfully.
Pavel Labatha5be48b2017-10-17 15:52:16 +0000250 std::vector<NativeThreadProtocol *> breakpoint_established_threads;
Omair Javaidd5ffbad2017-02-24 13:27:31 +0000251
252 // Request to set a hardware breakpoint for each of current process threads.
253 std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
Pavel Labatha5be48b2017-10-17 15:52:16 +0000254 for (const auto &thread : m_threads) {
255 assert(thread && "thread list should not have a NULL thread!");
Omair Javaidd5ffbad2017-02-24 13:27:31 +0000256
Pavel Labatha5be48b2017-10-17 15:52:16 +0000257 Status thread_error = thread->SetHardwareBreakpoint(addr, size);
Omair Javaidd5ffbad2017-02-24 13:27:31 +0000258 if (thread_error.Success()) {
259 // Remember that we set this breakpoint successfully in
260 // case we need to clear it later.
Pavel Labatha5be48b2017-10-17 15:52:16 +0000261 breakpoint_established_threads.push_back(thread.get());
Omair Javaidd5ffbad2017-02-24 13:27:31 +0000262 } else {
263 // Unset the breakpoint for each thread we successfully
264 // set so that we get back to a consistent state of "not
265 // set" for this hardware breakpoint.
266 for (auto rollback_thread_sp : breakpoint_established_threads) {
Zachary Turner97206d52017-05-12 04:51:55 +0000267 Status remove_error =
268 rollback_thread_sp->RemoveHardwareBreakpoint(addr);
Pavel Labatha5be48b2017-10-17 15:52:16 +0000269 if (remove_error.Fail())
270 LLDB_LOG(log,
271 "RemoveHardwareBreakpoint failed for pid={0}, tid={1}: {2}",
272 GetID(), rollback_thread_sp->GetID(), remove_error);
Omair Javaidd5ffbad2017-02-24 13:27:31 +0000273 }
274
275 return thread_error;
276 }
277 }
278
279 // Register new hardware breakpoint into hardware breakpoints map of current
280 // process.
281 m_hw_breakpoints_map[addr] = {addr, size};
282
Zachary Turner97206d52017-05-12 04:51:55 +0000283 return Status();
Omair Javaidd5ffbad2017-02-24 13:27:31 +0000284}
285
Zachary Turner97206d52017-05-12 04:51:55 +0000286Status NativeProcessProtocol::RemoveHardwareBreakpoint(lldb::addr_t addr) {
Omair Javaidd5ffbad2017-02-24 13:27:31 +0000287 // Update the thread list
288 UpdateThreads();
289
Zachary Turner97206d52017-05-12 04:51:55 +0000290 Status error;
Omair Javaidd5ffbad2017-02-24 13:27:31 +0000291
292 std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
Pavel Labatha5be48b2017-10-17 15:52:16 +0000293 for (const auto &thread : m_threads) {
294 assert(thread && "thread list should not have a NULL thread!");
295 error = thread->RemoveHardwareBreakpoint(addr);
Omair Javaidd5ffbad2017-02-24 13:27:31 +0000296 }
297
298 // Also remove from hardware breakpoint map of current process.
299 m_hw_breakpoints_map.erase(addr);
300
301 return error;
302}
303
Kate Stoneb9c1b512016-09-06 20:57:50 +0000304bool NativeProcessProtocol::RegisterNativeDelegate(
305 NativeDelegate &native_delegate) {
306 std::lock_guard<std::recursive_mutex> guard(m_delegates_mutex);
307 if (std::find(m_delegates.begin(), m_delegates.end(), &native_delegate) !=
308 m_delegates.end())
309 return false;
Todd Fialaaf245d12014-06-30 21:05:18 +0000310
Kate Stoneb9c1b512016-09-06 20:57:50 +0000311 m_delegates.push_back(&native_delegate);
312 native_delegate.InitializeDelegate(this);
313 return true;
Todd Fialaaf245d12014-06-30 21:05:18 +0000314}
315
Kate Stoneb9c1b512016-09-06 20:57:50 +0000316bool NativeProcessProtocol::UnregisterNativeDelegate(
317 NativeDelegate &native_delegate) {
318 std::lock_guard<std::recursive_mutex> guard(m_delegates_mutex);
319
320 const auto initial_size = m_delegates.size();
321 m_delegates.erase(
322 remove(m_delegates.begin(), m_delegates.end(), &native_delegate),
323 m_delegates.end());
324
325 // We removed the delegate if the count of delegates shrank after
326 // removing all copies of the given native_delegate from the vector.
327 return m_delegates.size() < initial_size;
328}
329
330void NativeProcessProtocol::SynchronouslyNotifyProcessStateChanged(
331 lldb::StateType state) {
332 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
333
334 std::lock_guard<std::recursive_mutex> guard(m_delegates_mutex);
335 for (auto native_delegate : m_delegates)
336 native_delegate->ProcessStateChanged(this, state);
337
338 if (log) {
339 if (!m_delegates.empty()) {
340 log->Printf("NativeProcessProtocol::%s: sent state notification [%s] "
341 "from process %" PRIu64,
342 __FUNCTION__, lldb_private::StateAsCString(state), GetID());
343 } else {
344 log->Printf("NativeProcessProtocol::%s: would send state notification "
345 "[%s] from process %" PRIu64 ", but no delegates",
346 __FUNCTION__, lldb_private::StateAsCString(state), GetID());
347 }
348 }
349}
350
351void NativeProcessProtocol::NotifyDidExec() {
352 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
353 if (log)
354 log->Printf("NativeProcessProtocol::%s - preparing to call delegates",
355 __FUNCTION__);
356
357 {
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +0000358 std::lock_guard<std::recursive_mutex> guard(m_delegates_mutex);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000359 for (auto native_delegate : m_delegates)
360 native_delegate->DidExec(this);
361 }
Todd Fialaaf245d12014-06-30 21:05:18 +0000362}
363
Zachary Turner97206d52017-05-12 04:51:55 +0000364Status NativeProcessProtocol::SetSoftwareBreakpoint(lldb::addr_t addr,
365 uint32_t size_hint) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000366 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_BREAKPOINTS));
367 if (log)
368 log->Printf("NativeProcessProtocol::%s addr = 0x%" PRIx64, __FUNCTION__,
369 addr);
Todd Fialaaf245d12014-06-30 21:05:18 +0000370
Kate Stoneb9c1b512016-09-06 20:57:50 +0000371 return m_breakpoint_list.AddRef(
372 addr, size_hint, false,
373 [this](lldb::addr_t addr, size_t size_hint, bool /* hardware */,
Zachary Turner97206d52017-05-12 04:51:55 +0000374 NativeBreakpointSP &breakpoint_sp) -> Status {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000375 return SoftwareBreakpoint::CreateSoftwareBreakpoint(
376 *this, addr, size_hint, breakpoint_sp);
377 });
Todd Fialaaf245d12014-06-30 21:05:18 +0000378}
379
Zachary Turner97206d52017-05-12 04:51:55 +0000380Status NativeProcessProtocol::RemoveBreakpoint(lldb::addr_t addr,
381 bool hardware) {
Omair Javaidd5ffbad2017-02-24 13:27:31 +0000382 if (hardware)
383 return RemoveHardwareBreakpoint(addr);
384 else
385 return m_breakpoint_list.DecRef(addr);
Todd Fialaaf245d12014-06-30 21:05:18 +0000386}
387
Zachary Turner97206d52017-05-12 04:51:55 +0000388Status NativeProcessProtocol::EnableBreakpoint(lldb::addr_t addr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000389 return m_breakpoint_list.EnableBreakpoint(addr);
Todd Fialaa9882ce2014-08-28 15:46:54 +0000390}
391
Zachary Turner97206d52017-05-12 04:51:55 +0000392Status NativeProcessProtocol::DisableBreakpoint(lldb::addr_t addr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000393 return m_breakpoint_list.DisableBreakpoint(addr);
Todd Fialaaf245d12014-06-30 21:05:18 +0000394}
395
Kate Stoneb9c1b512016-09-06 20:57:50 +0000396lldb::StateType NativeProcessProtocol::GetState() const {
397 std::lock_guard<std::recursive_mutex> guard(m_state_mutex);
398 return m_state;
Todd Fialaaf245d12014-06-30 21:05:18 +0000399}
400
Kate Stoneb9c1b512016-09-06 20:57:50 +0000401void NativeProcessProtocol::SetState(lldb::StateType state,
402 bool notify_delegates) {
403 std::lock_guard<std::recursive_mutex> guard(m_state_mutex);
404
405 if (state == m_state)
406 return;
407
408 m_state = state;
409
410 if (StateIsStoppedState(state, false)) {
411 ++m_stop_id;
412
413 // Give process a chance to do any stop id bump processing, such as
414 // clearing cached data that is invalidated each time the process runs.
415 // Note if/when we support some threads running, we'll end up needing
416 // to manage this per thread and per process.
417 DoStopIDBumped(m_stop_id);
418 }
419
420 // Optionally notify delegates of the state change.
421 if (notify_delegates)
422 SynchronouslyNotifyProcessStateChanged(state);
Todd Fialaaf245d12014-06-30 21:05:18 +0000423}
424
Kate Stoneb9c1b512016-09-06 20:57:50 +0000425uint32_t NativeProcessProtocol::GetStopID() const {
426 std::lock_guard<std::recursive_mutex> guard(m_state_mutex);
427 return m_stop_id;
Todd Fialaaf245d12014-06-30 21:05:18 +0000428}
429
Kate Stoneb9c1b512016-09-06 20:57:50 +0000430void NativeProcessProtocol::DoStopIDBumped(uint32_t /* newBumpId */) {
431 // Default implementation does nothing.
Todd Fialaaf245d12014-06-30 21:05:18 +0000432}
433
Zachary Turner97206d52017-05-12 04:51:55 +0000434Status NativeProcessProtocol::ResolveProcessArchitecture(lldb::pid_t pid,
435 ArchSpec &arch) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000436 // Grab process info for the running process.
437 ProcessInstanceInfo process_info;
438 if (!Host::GetProcessInfo(pid, process_info))
Zachary Turner97206d52017-05-12 04:51:55 +0000439 return Status("failed to get process info");
Tamas Berghammer5830aa72015-02-06 10:42:33 +0000440
Kate Stoneb9c1b512016-09-06 20:57:50 +0000441 // Resolve the executable module.
442 ModuleSpecList module_specs;
443 if (!ObjectFile::GetModuleSpecifications(process_info.GetExecutableFile(), 0,
444 0, module_specs))
Zachary Turner97206d52017-05-12 04:51:55 +0000445 return Status("failed to get module specifications");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000446 lldbassert(module_specs.GetSize() == 1);
Tamas Berghammer5830aa72015-02-06 10:42:33 +0000447
Kate Stoneb9c1b512016-09-06 20:57:50 +0000448 arch = module_specs.GetModuleSpecRefAtIndex(0).GetArchitecture();
449 if (arch.IsValid())
Zachary Turner97206d52017-05-12 04:51:55 +0000450 return Status();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000451 else
Zachary Turner97206d52017-05-12 04:51:55 +0000452 return Status(
453 "failed to retrieve a valid architecture from the exe module");
Todd Fialae77fce02016-09-04 00:18:56 +0000454}
455
Pavel Labath96e600f2017-07-07 11:02:19 +0000456NativeProcessProtocol::Factory::~Factory() = default;