[Lldb-commits] [lldb] [lldb] Keep a stop private until it has been handled (PR #234789)

via lldb-commits lldb-commits at lists.llvm.org
Thu Oct 29 14:54:22 PDT 2026


================
@@ -1335,6 +1335,98 @@ StateType Process::GetState() {
   return GetPublicState();
 }
 
+StateType Process::GetPublicState() const {
+  if (!m_current_private_state_thread_sp)
+    return eStateUnloaded;
+  if (StopHandlingContextSP context_sp =
+          GetStopHandlingContextForCurrentThread())
+    return context_sp->m_public_state;
+  return m_current_private_state_thread_sp->GetPublicState();
+}
+
+bool Process::SetPublicRunLockToStopped() {
+  if (StopHandlingContextSP context_sp =
+          GetStopHandlingContextForCurrentThread()) {
+    context_sp->m_run_lock_running = false;
+    return context_sp->m_public_run_lock.SetStopped();
+  }
+  assert(m_current_private_state_thread_sp);
+  if (m_current_private_state_thread_sp)
+    return m_current_private_state_thread_sp->SetPublicRunLockToStopped();
+  return false;
+}
+
+bool Process::SetPublicRunLockToRunning() {
+  if (StopHandlingContextSP context_sp =
+          GetStopHandlingContextForCurrentThread()) {
+    const bool was_stopped = context_sp->m_public_run_lock.SetRunning();
+    context_sp->m_run_lock_running = true;
+    context_sp->m_resumed |= was_stopped;
+    return was_stopped;
+  }
+  assert(m_current_private_state_thread_sp);
+  if (m_current_private_state_thread_sp)
+    return m_current_private_state_thread_sp->SetPublicRunLockToRunning();
+  return false;
+}
+
+Process::StopHandlingContextSP Process::GetStopHandlingContext() const {
+  std::lock_guard<std::mutex> guard(m_stop_handling_mutex);
+  return m_stop_handling_context_sp;
+}
+
+Process::StopHandlingContextSP
+Process::GetStopHandlingContextForCurrentThread() const {
+  if (PolicyStack::Get().Current().view != Policy::View::HandlingStop)
+    return nullptr;
+  StopHandlingContextSP context_sp = GetStopHandlingContext();
+  // The policy stack belongs to the host thread, and one host thread can
+  // handle stops of several processes: the debugger's event handler thread
+  // pulls the stop events of every process in the debugger. While it handles
+  // a stop of one process, its view is HandlingStop even when an action calls
+  // into another process. So the view alone isn't enough: return this
+  // process's context only if this thread opened it. Otherwise this process's
+  // public state and run lock come from its PrivateStateThread, as for any
+  // thread that isn't handling its stop.
+  if (context_sp && context_sp->m_owner == std::this_thread::get_id())
+    return context_sp;
+  return nullptr;
+}
+
+bool Process::IsHandlingStopOnOtherThread() const {
+  StopHandlingContextSP context_sp = GetStopHandlingContext();
+  return context_sp && context_sp->m_owner != std::this_thread::get_id();
+}
+
+Process::StopHandlingScope::StopHandlingScope(Process &process)
+    : m_process(process), m_context_sp(std::make_shared<StopHandlingContext>(
+                              std::this_thread::get_id())),
+      m_policy_guard(PolicyStack::Get().PushHandlingStop()) {
+  std::lock_guard<std::mutex> guard(m_process.m_stop_handling_mutex);
+  m_enclosing_context_sp =
+      std::exchange(m_process.m_stop_handling_context_sp, m_context_sp);
+}
+
+Process::StopHandlingScope::~StopHandlingScope() {
+  std::lock_guard<std::mutex> guard(m_process.m_stop_handling_mutex);
+  // The slot only holds another thread's context if two threads pull this
----------------
jimingham wrote:

I don't understand when this would happen.  When would "two threads pull this process's events from its primary listener"?  Do you mean you have two threads each of which is calling "WaitForEvent" on the SAME listener.  That seems to me clearly racy and not something we are interested in supporting.  If we don't explicitly disallow it currently, we should.  You can multiplex by having multiple Listeners for the same Broadcaster, but running concurrent WaitForEvent's on the same Listener seems wrong.

Is there a scenario that motivated this comment?

https://github.com/llvm/llvm-project/pull/234789


More information about the lldb-commits mailing list