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 "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/Target/NativeRegisterContext.h"
18 #include "NativeThreadProtocol.h"
19 #include "SoftwareBreakpoint.h"
22 using namespace lldb_private;
24 // -----------------------------------------------------------------------------
25 // NativeProcessProtocol Members
26 // -----------------------------------------------------------------------------
28 NativeProcessProtocol::NativeProcessProtocol (lldb::pid_t pid) :
31 m_current_thread_id (LLDB_INVALID_THREAD_ID),
32 m_threads_mutex (Mutex::eMutexTypeRecursive),
33 m_state (lldb::eStateInvalid),
34 m_state_mutex (Mutex::eMutexTypeRecursive),
35 m_exit_type (eExitTypeInvalid),
37 m_exit_description (),
38 m_delegates_mutex (Mutex::eMutexTypeRecursive),
47 NativeProcessProtocol::GetMemoryRegionInfo (lldb::addr_t load_addr, MemoryRegionInfo &range_info)
49 // Default: not implemented.
50 return Error ("not implemented");
54 NativeProcessProtocol::GetExitStatus (ExitType *exit_type, int *status, std::string &exit_description)
56 if (m_state == lldb::eStateExited)
58 *exit_type = m_exit_type;
59 *status = m_exit_status;
60 exit_description = m_exit_description;
69 NativeProcessProtocol::SetExitStatus (ExitType exit_type, int status, const char *exit_description, bool bNotifyStateChange)
71 Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
73 log->Printf ("NativeProcessProtocol::%s(%d, %d, %s, %s) called",
77 exit_description ? exit_description : "nullptr",
78 bNotifyStateChange ? "true" : "false");
80 // Exit status already set
81 if (m_state == lldb::eStateExited)
84 log->Printf ("NativeProcessProtocol::%s exit status already set to %d, ignoring new set to %d", __FUNCTION__, m_exit_status, status);
88 m_state = lldb::eStateExited;
90 m_exit_type = exit_type;
91 m_exit_status = status;
92 if (exit_description && exit_description[0])
93 m_exit_description = exit_description;
95 m_exit_description.clear();
97 if (bNotifyStateChange)
98 SynchronouslyNotifyProcessStateChanged (lldb::eStateExited);
103 NativeThreadProtocolSP
104 NativeProcessProtocol::GetThreadAtIndex (uint32_t idx)
106 Mutex::Locker locker (m_threads_mutex);
107 if (idx < m_threads.size ())
108 return m_threads[idx];
109 return NativeThreadProtocolSP ();
112 NativeThreadProtocolSP
113 NativeProcessProtocol::GetThreadByID (lldb::tid_t tid)
115 Mutex::Locker locker (m_threads_mutex);
116 for (auto thread_sp : m_threads)
118 if (thread_sp->GetID() == tid)
121 return NativeThreadProtocolSP ();
125 NativeProcessProtocol::IsAlive () const
127 return m_state != eStateDetached
128 && m_state != eStateExited
129 && m_state != eStateInvalid
130 && m_state != eStateUnloaded;
134 NativeProcessProtocol::GetByteOrder (lldb::ByteOrder &byte_order) const
136 ArchSpec process_arch;
137 if (!GetArchitecture (process_arch))
139 byte_order = process_arch.GetByteOrder ();
144 NativeProcessProtocol::GetMaxWatchpoints () const
146 // This default implementation will return the number of
147 // *hardware* breakpoints available. MacOSX and other OS
148 // implementations that support software breakpoints will want to
149 // override this correctly for their implementation.
150 Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
153 NativeThreadProtocolSP thread_sp (const_cast<NativeProcessProtocol*> (this)->GetThreadAtIndex (0));
157 log->Warning ("NativeProcessProtocol::%s (): failed to find a thread to grab a NativeRegisterContext!", __FUNCTION__);
161 NativeRegisterContextSP reg_ctx_sp (thread_sp->GetRegisterContext ());
165 log->Warning ("NativeProcessProtocol::%s (): failed to get a RegisterContextNativeProcess from the first thread!", __FUNCTION__);
169 return reg_ctx_sp->NumSupportedHardwareWatchpoints ();
173 NativeProcessProtocol::SetWatchpoint (lldb::addr_t addr, size_t size, uint32_t watch_flags, bool hardware)
175 // This default implementation assumes setting the watchpoint for
176 // the process will require setting the watchpoint for each of the
177 // threads. Furthermore, it will track watchpoints set for the
178 // process and will add them to each thread that is attached to
179 // via the (FIXME implement) OnThreadAttached () method.
181 Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
183 // FIXME save the watchpoint on the set of process watchpoint vars
184 // so we can add them to a thread each time a new thread is registered.
186 // Update the thread list
189 // Keep track of the threads we successfully set the watchpoint
190 // for. If one of the thread watchpoint setting operations fails,
191 // back off and remove the watchpoint for all the threads that
192 // were successfully set so we get back to a consistent state.
193 std::vector<NativeThreadProtocolSP> watchpoint_established_threads;
195 // Tell each thread to set a watchpoint. In the event that
196 // hardware watchpoints are requested but the SetWatchpoint fails,
197 // try to set a software watchpoint as a fallback. It's
198 // conceivable that if there are more threads than hardware
199 // watchpoints available, some of the threads will fail to set
200 // hardware watchpoints while software ones may be available.
201 Mutex::Locker locker (m_threads_mutex);
202 for (auto thread_sp : m_threads)
204 assert (thread_sp && "thread list should not have a NULL thread!");
208 Error thread_error = thread_sp->SetWatchpoint (addr, size, watch_flags, hardware);
209 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 ())
217 log->Warning ("hardware watchpoint requested but software watchpoint set");
221 if (thread_error.Success ())
223 // Remember that we set this watchpoint successfully in
224 // case we need to clear it later.
225 watchpoint_established_threads.push_back (thread_sp);
229 // Unset the watchpoint for each thread we successfully
230 // set so that we get back to a consistent state of "not
231 // set" for the watchpoint.
232 for (auto unwatch_thread_sp : watchpoint_established_threads)
234 Error remove_error = unwatch_thread_sp->RemoveWatchpoint (addr);
235 if (remove_error.Fail () && log)
237 log->Warning ("NativeProcessProtocol::%s (): RemoveWatchpoint failed for pid=%" PRIu64 ", tid=%" PRIu64 ": %s",
238 __FUNCTION__, GetID (), unwatch_thread_sp->GetID (), remove_error.AsCString ());
249 NativeProcessProtocol::RemoveWatchpoint (lldb::addr_t addr)
251 // FIXME remove the watchpoint on the set of process watchpoint vars
252 // so we can add them to a thread each time a new thread is registered.
254 // Update the thread list
259 Mutex::Locker locker (m_threads_mutex);
260 for (auto thread_sp : m_threads)
262 assert (thread_sp && "thread list should not have a NULL thread!");
266 const Error thread_error = thread_sp->RemoveWatchpoint (addr);
267 if (thread_error.Fail ())
269 // Keep track of the first thread error if any threads
270 // fail. We want to try to remove the watchpoint from
271 // every thread, though, even if one or more have errors.
272 if (!overall_error.Fail ())
273 overall_error = thread_error;
276 return overall_error;
280 NativeProcessProtocol::RegisterNativeDelegate (NativeDelegate &native_delegate)
282 Mutex::Locker locker (m_delegates_mutex);
283 if (std::find (m_delegates.begin (), m_delegates.end (), &native_delegate) != m_delegates.end ())
286 m_delegates.push_back (&native_delegate);
287 native_delegate.InitializeDelegate (this);
292 NativeProcessProtocol::UnregisterNativeDelegate (NativeDelegate &native_delegate)
294 Mutex::Locker locker (m_delegates_mutex);
296 const auto initial_size = m_delegates.size ();
297 m_delegates.erase (remove (m_delegates.begin (), m_delegates.end (), &native_delegate), m_delegates.end ());
299 // We removed the delegate if the count of delegates shrank after
300 // removing all copies of the given native_delegate from the vector.
301 return m_delegates.size () < initial_size;
305 NativeProcessProtocol::SynchronouslyNotifyProcessStateChanged (lldb::StateType state)
307 Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
309 Mutex::Locker locker (m_delegates_mutex);
310 for (auto native_delegate: m_delegates)
311 native_delegate->ProcessStateChanged (this, state);
315 if (!m_delegates.empty ())
317 log->Printf ("NativeProcessProtocol::%s: sent state notification [%s] from process %" PRIu64,
318 __FUNCTION__, lldb_private::StateAsCString (state), GetID ());
322 log->Printf ("NativeProcessProtocol::%s: would send state notification [%s] from process %" PRIu64 ", but no delegates",
323 __FUNCTION__, lldb_private::StateAsCString (state), GetID ());
329 NativeProcessProtocol::NotifyDidExec ()
331 Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
333 log->Printf ("NativeProcessProtocol::%s - preparing to call delegates", __FUNCTION__);
336 Mutex::Locker locker (m_delegates_mutex);
337 for (auto native_delegate: m_delegates)
338 native_delegate->DidExec (this);
344 NativeProcessProtocol::SetSoftwareBreakpoint (lldb::addr_t addr, uint32_t size_hint)
346 Log *log (GetLogIfAnyCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
348 log->Printf ("NativeProcessProtocol::%s addr = 0x%" PRIx64, __FUNCTION__, addr);
350 return m_breakpoint_list.AddRef (addr, size_hint, false,
351 [this] (lldb::addr_t addr, size_t size_hint, bool /* hardware */, NativeBreakpointSP &breakpoint_sp)->Error
352 { return SoftwareBreakpoint::CreateSoftwareBreakpoint (*this, addr, size_hint, breakpoint_sp); });
356 NativeProcessProtocol::RemoveBreakpoint (lldb::addr_t addr)
358 return m_breakpoint_list.DecRef (addr);
362 NativeProcessProtocol::EnableBreakpoint (lldb::addr_t addr)
364 return m_breakpoint_list.EnableBreakpoint (addr);
368 NativeProcessProtocol::DisableBreakpoint (lldb::addr_t addr)
370 return m_breakpoint_list.DisableBreakpoint (addr);
374 NativeProcessProtocol::GetState () const
376 Mutex::Locker locker (m_state_mutex);
381 NativeProcessProtocol::SetState (lldb::StateType state, bool notify_delegates)
383 Mutex::Locker locker (m_state_mutex);
386 if (StateIsStoppedState (state, false))
390 // Give process a chance to do any stop id bump processing, such as
391 // clearing cached data that is invalidated each time the process runs.
392 // Note if/when we support some threads running, we'll end up needing
393 // to manage this per thread and per process.
394 DoStopIDBumped (m_stop_id);
397 // Optionally notify delegates of the state change.
398 if (notify_delegates)
399 SynchronouslyNotifyProcessStateChanged (state);
402 uint32_t NativeProcessProtocol::GetStopID () const
404 Mutex::Locker locker (m_state_mutex);
409 NativeProcessProtocol::DoStopIDBumped (uint32_t /* newBumpId */)
411 // Default implementation does nothing.