1 //===-- NativeProcessProtocol.cpp -------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Host/common/NativeProcessProtocol.h"
12 #include "lldb/lldb-enumerations.h"
13 #include "lldb/Core/ArchSpec.h"
14 #include "lldb/Core/Log.h"
15 #include "lldb/Core/State.h"
16 #include "lldb/Host/Host.h"
17 #include "lldb/Host/common/NativeRegisterContext.h"
19 #include "lldb/Host/common/NativeThreadProtocol.h"
20 #include "lldb/Host/common/SoftwareBreakpoint.h"
23 using namespace lldb_private;
25 // -----------------------------------------------------------------------------
26 // NativeProcessProtocol Members
27 // -----------------------------------------------------------------------------
29 NativeProcessProtocol::NativeProcessProtocol (lldb::pid_t pid) :
32 m_current_thread_id (LLDB_INVALID_THREAD_ID),
33 m_threads_mutex (Mutex::eMutexTypeRecursive),
34 m_state (lldb::eStateInvalid),
35 m_state_mutex (Mutex::eMutexTypeRecursive),
36 m_exit_type (eExitTypeInvalid),
38 m_exit_description (),
39 m_delegates_mutex (Mutex::eMutexTypeRecursive),
49 NativeProcessProtocol::Interrupt ()
52 #if !defined (SIGSTOP)
53 error.SetErrorString ("local host does not support signaling");
56 return Signal (SIGSTOP);
61 NativeProcessProtocol::GetMemoryRegionInfo (lldb::addr_t load_addr, MemoryRegionInfo &range_info)
63 // Default: not implemented.
64 return Error ("not implemented");
68 NativeProcessProtocol::GetExitStatus (ExitType *exit_type, int *status, std::string &exit_description)
70 if (m_state == lldb::eStateExited)
72 *exit_type = m_exit_type;
73 *status = m_exit_status;
74 exit_description = m_exit_description;
83 NativeProcessProtocol::SetExitStatus (ExitType exit_type, int status, const char *exit_description, bool bNotifyStateChange)
85 Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
87 log->Printf ("NativeProcessProtocol::%s(%d, %d, %s, %s) called",
91 exit_description ? exit_description : "nullptr",
92 bNotifyStateChange ? "true" : "false");
94 // Exit status already set
95 if (m_state == lldb::eStateExited)
98 log->Printf ("NativeProcessProtocol::%s exit status already set to %d, ignoring new set to %d", __FUNCTION__, m_exit_status, status);
102 m_state = lldb::eStateExited;
104 m_exit_type = exit_type;
105 m_exit_status = status;
106 if (exit_description && exit_description[0])
107 m_exit_description = exit_description;
109 m_exit_description.clear();
111 if (bNotifyStateChange)
112 SynchronouslyNotifyProcessStateChanged (lldb::eStateExited);
117 NativeThreadProtocolSP
118 NativeProcessProtocol::GetThreadAtIndex (uint32_t idx)
120 Mutex::Locker locker (m_threads_mutex);
121 if (idx < m_threads.size ())
122 return m_threads[idx];
123 return NativeThreadProtocolSP ();
126 NativeThreadProtocolSP
127 NativeProcessProtocol::GetThreadByIDUnlocked (lldb::tid_t tid)
129 for (auto thread_sp : m_threads)
131 if (thread_sp->GetID() == tid)
134 return NativeThreadProtocolSP ();
137 NativeThreadProtocolSP
138 NativeProcessProtocol::GetThreadByID (lldb::tid_t tid)
140 Mutex::Locker locker (m_threads_mutex);
141 return GetThreadByIDUnlocked (tid);
145 NativeProcessProtocol::IsAlive () const
147 return m_state != eStateDetached
148 && m_state != eStateExited
149 && m_state != eStateInvalid
150 && m_state != eStateUnloaded;
154 NativeProcessProtocol::GetByteOrder (lldb::ByteOrder &byte_order) const
156 ArchSpec process_arch;
157 if (!GetArchitecture (process_arch))
159 byte_order = process_arch.GetByteOrder ();
163 const NativeWatchpointList::WatchpointMap&
164 NativeProcessProtocol::GetWatchpointMap () const
166 return m_watchpoint_list.GetWatchpointMap();
170 NativeProcessProtocol::GetMaxWatchpoints () const
172 // This default implementation will return the number of
173 // *hardware* breakpoints available. MacOSX and other OS
174 // implementations that support software breakpoints will want to
175 // override this correctly for their implementation.
176 Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
179 NativeThreadProtocolSP thread_sp (const_cast<NativeProcessProtocol*> (this)->GetThreadAtIndex (0));
183 log->Warning ("NativeProcessProtocol::%s (): failed to find a thread to grab a NativeRegisterContext!", __FUNCTION__);
187 NativeRegisterContextSP reg_ctx_sp (thread_sp->GetRegisterContext ());
191 log->Warning ("NativeProcessProtocol::%s (): failed to get a RegisterContextNativeProcess from the first thread!", __FUNCTION__);
195 return reg_ctx_sp->NumSupportedHardwareWatchpoints ();
199 NativeProcessProtocol::SetWatchpoint (lldb::addr_t addr, size_t size, uint32_t watch_flags, bool hardware)
201 // This default implementation assumes setting the watchpoint for
202 // the process will require setting the watchpoint for each of the
203 // threads. Furthermore, it will track watchpoints set for the
204 // process and will add them to each thread that is attached to
205 // via the (FIXME implement) OnThreadAttached () method.
207 Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
209 // Update the thread list
212 // Keep track of the threads we successfully set the watchpoint
213 // for. If one of the thread watchpoint setting operations fails,
214 // back off and remove the watchpoint for all the threads that
215 // were successfully set so we get back to a consistent state.
216 std::vector<NativeThreadProtocolSP> watchpoint_established_threads;
218 // Tell each thread to set a watchpoint. In the event that
219 // hardware watchpoints are requested but the SetWatchpoint fails,
220 // try to set a software watchpoint as a fallback. It's
221 // conceivable that if there are more threads than hardware
222 // watchpoints available, some of the threads will fail to set
223 // hardware watchpoints while software ones may be available.
224 Mutex::Locker locker (m_threads_mutex);
225 for (auto thread_sp : m_threads)
227 assert (thread_sp && "thread list should not have a NULL thread!");
231 Error thread_error = thread_sp->SetWatchpoint (addr, size, watch_flags, hardware);
232 if (thread_error.Fail () && hardware)
234 // Try software watchpoints since we failed on hardware watchpoint setting
235 // and we may have just run out of hardware watchpoints.
236 thread_error = thread_sp->SetWatchpoint (addr, size, watch_flags, false);
237 if (thread_error.Success ())
240 log->Warning ("hardware watchpoint requested but software watchpoint set");
244 if (thread_error.Success ())
246 // Remember that we set this watchpoint successfully in
247 // case we need to clear it later.
248 watchpoint_established_threads.push_back (thread_sp);
252 // Unset the watchpoint for each thread we successfully
253 // set so that we get back to a consistent state of "not
254 // set" for the watchpoint.
255 for (auto unwatch_thread_sp : watchpoint_established_threads)
257 Error remove_error = unwatch_thread_sp->RemoveWatchpoint (addr);
258 if (remove_error.Fail () && log)
260 log->Warning ("NativeProcessProtocol::%s (): RemoveWatchpoint failed for pid=%" PRIu64 ", tid=%" PRIu64 ": %s",
261 __FUNCTION__, GetID (), unwatch_thread_sp->GetID (), remove_error.AsCString ());
268 return m_watchpoint_list.Add (addr, size, watch_flags, hardware);
272 NativeProcessProtocol::RemoveWatchpoint (lldb::addr_t addr)
274 // Update the thread list
279 Mutex::Locker locker (m_threads_mutex);
280 for (auto thread_sp : m_threads)
282 assert (thread_sp && "thread list should not have a NULL thread!");
286 const Error thread_error = thread_sp->RemoveWatchpoint (addr);
287 if (thread_error.Fail ())
289 // Keep track of the first thread error if any threads
290 // fail. We want to try to remove the watchpoint from
291 // every thread, though, even if one or more have errors.
292 if (!overall_error.Fail ())
293 overall_error = thread_error;
296 const Error error = m_watchpoint_list.Remove(addr);
297 return overall_error.Fail() ? overall_error : error;
301 NativeProcessProtocol::RegisterNativeDelegate (NativeDelegate &native_delegate)
303 Mutex::Locker locker (m_delegates_mutex);
304 if (std::find (m_delegates.begin (), m_delegates.end (), &native_delegate) != m_delegates.end ())
307 m_delegates.push_back (&native_delegate);
308 native_delegate.InitializeDelegate (this);
313 NativeProcessProtocol::UnregisterNativeDelegate (NativeDelegate &native_delegate)
315 Mutex::Locker locker (m_delegates_mutex);
317 const auto initial_size = m_delegates.size ();
318 m_delegates.erase (remove (m_delegates.begin (), m_delegates.end (), &native_delegate), m_delegates.end ());
320 // We removed the delegate if the count of delegates shrank after
321 // removing all copies of the given native_delegate from the vector.
322 return m_delegates.size () < initial_size;
326 NativeProcessProtocol::SynchronouslyNotifyProcessStateChanged (lldb::StateType state)
328 Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
330 Mutex::Locker locker (m_delegates_mutex);
331 for (auto native_delegate: m_delegates)
332 native_delegate->ProcessStateChanged (this, state);
336 if (!m_delegates.empty ())
338 log->Printf ("NativeProcessProtocol::%s: sent state notification [%s] from process %" PRIu64,
339 __FUNCTION__, lldb_private::StateAsCString (state), GetID ());
343 log->Printf ("NativeProcessProtocol::%s: would send state notification [%s] from process %" PRIu64 ", but no delegates",
344 __FUNCTION__, lldb_private::StateAsCString (state), GetID ());
350 NativeProcessProtocol::NotifyDidExec ()
352 Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
354 log->Printf ("NativeProcessProtocol::%s - preparing to call delegates", __FUNCTION__);
357 Mutex::Locker locker (m_delegates_mutex);
358 for (auto native_delegate: m_delegates)
359 native_delegate->DidExec (this);
365 NativeProcessProtocol::SetSoftwareBreakpoint (lldb::addr_t addr, uint32_t size_hint)
367 Log *log (GetLogIfAnyCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
369 log->Printf ("NativeProcessProtocol::%s addr = 0x%" PRIx64, __FUNCTION__, addr);
371 return m_breakpoint_list.AddRef (addr, size_hint, false,
372 [this] (lldb::addr_t addr, size_t size_hint, bool /* hardware */, NativeBreakpointSP &breakpoint_sp)->Error
373 { return SoftwareBreakpoint::CreateSoftwareBreakpoint (*this, addr, size_hint, breakpoint_sp); });
377 NativeProcessProtocol::RemoveBreakpoint (lldb::addr_t addr)
379 return m_breakpoint_list.DecRef (addr);
383 NativeProcessProtocol::EnableBreakpoint (lldb::addr_t addr)
385 return m_breakpoint_list.EnableBreakpoint (addr);
389 NativeProcessProtocol::DisableBreakpoint (lldb::addr_t addr)
391 return m_breakpoint_list.DisableBreakpoint (addr);
395 NativeProcessProtocol::GetState () const
397 Mutex::Locker locker (m_state_mutex);
402 NativeProcessProtocol::SetState (lldb::StateType state, bool notify_delegates)
404 Mutex::Locker locker (m_state_mutex);
406 if (state == m_state)
411 if (StateIsStoppedState (state, false))
415 // Give process a chance to do any stop id bump processing, such as
416 // clearing cached data that is invalidated each time the process runs.
417 // Note if/when we support some threads running, we'll end up needing
418 // to manage this per thread and per process.
419 DoStopIDBumped (m_stop_id);
422 // Optionally notify delegates of the state change.
423 if (notify_delegates)
424 SynchronouslyNotifyProcessStateChanged (state);
427 uint32_t NativeProcessProtocol::GetStopID () const
429 Mutex::Locker locker (m_state_mutex);
434 NativeProcessProtocol::DoStopIDBumped (uint32_t /* newBumpId */)
436 // Default implementation does nothing.
440 // These need to be implemented to support lldb-gdb-server on a given platform. Stubs are
441 // provided to make the rest of the code link on non-supported platforms.
444 NativeProcessProtocol::Launch (ProcessLaunchInfo &launch_info,
445 NativeDelegate &native_delegate,
447 NativeProcessProtocolSP &process_sp)
449 llvm_unreachable("Platform has no NativeProcessProtocol support");
453 NativeProcessProtocol::Attach (lldb::pid_t pid,
454 NativeDelegate &native_delegate,
456 NativeProcessProtocolSP &process_sp)
458 llvm_unreachable("Platform has no NativeProcessProtocol support");