[llvm-branch-commits] [lldb] [LLDB] Guard Process::m_instrumentation_runtimes with a mutex (PR #238466)

Dan Liew via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Tue Nov 10 18:01:23 PST 2026


https://github.com/delcypher updated https://github.com/llvm/llvm-project/pull/238466

>From 1e7e654a42e75e6426567843cbbe105b8e5357bc Mon Sep 17 00:00:00 2001
From: Dan Liew <dan at su-root.co.uk>
Date: Mon, 2 Nov 2026 16:37:18 -0800
Subject: [PATCH] [LLDB] Guard Process::m_instrumentation_runtimes with a mutex

This patch is the sixth part in a patch series that will allow
enabling/disabling InstrumentationRuntime plugins in a running debug
session.

`Process::m_instrumentation_runtimes` can be accessed from multiple
threads. For example, a plugin can be enabled/disabled in a process
from the command thread (or from another debugger when using the
`global` domain) while `Process::ModulesDidLoad()` iterates over the
plugins on another thread, or while an SB API client calls
`Process::GetInstrumentationRuntime()`. These accesses were
unsynchronized.

Now every access to `m_instrumentation_runtimes` is guarded by the new
`m_instrumentation_runtimes_mutex`:

* `Process::Finalize()` and `Process::DidExec()` clear the map while
  holding the mutex.
* `Process::ModulesDidLoad()` creates any missing plugins and informs
  the plugins of the loaded modules while holding the mutex.
* `Process::GetInstrumentationRuntime()` looks up the plugin while
  holding the mutex.
* `Process::SetInstrumentationRuntimeEnabled()` holds the mutex for the
  whole function so that checking for an existing instance of a plugin
  and creating a new one happen atomically.

Plugin code (constructors, `ModulesDidLoad()`, `Activate()` and
destructors) runs while the mutex is held. This is fine because none of
it calls back into the `Process` methods that acquire the mutex.

Assisted-by: Claude Code
---
 lldb/include/lldb/Target/Process.h |  2 ++
 lldb/source/Target/Process.cpp     | 35 +++++++++++++++++++++---------
 2 files changed, 27 insertions(+), 10 deletions(-)

diff --git a/lldb/include/lldb/Target/Process.h b/lldb/include/lldb/Target/Process.h
index 06cae7070e1dc0..0dccf0bf48ca99 100644
--- a/lldb/include/lldb/Target/Process.h
+++ b/lldb/include/lldb/Target/Process.h
@@ -3579,7 +3579,9 @@ void PruneThreadPlans();
   /// The instrumentation runtime plugin instances for this process. A null
   /// entry means the plugin has been disabled in this process. The entry is
   /// kept so that a new instance isn't created when modules are loaded.
+  /// Guarded by m_instrumentation_runtimes_mutex.
   InstrumentationRuntimeCollection m_instrumentation_runtimes;
+  std::mutex m_instrumentation_runtimes_mutex;
   std::unique_ptr<NextEventAction> m_next_event_action_up;
   std::vector<PreResumeCallbackAndBaton> m_pre_resume_actions;
   bool m_currently_handling_do_on_removals;
diff --git a/lldb/source/Target/Process.cpp b/lldb/source/Target/Process.cpp
index 3ec5131b3a9a85..d00e845fbc74f9 100644
--- a/lldb/source/Target/Process.cpp
+++ b/lldb/source/Target/Process.cpp
@@ -639,7 +639,10 @@ void Process::Finalize(bool destructing) {
     std::lock_guard<std::recursive_mutex> guard(m_language_runtimes_mutex);
     m_language_runtimes.clear();
   }
-  m_instrumentation_runtimes.clear();
+  {
+    std::lock_guard<std::mutex> guard(m_instrumentation_runtimes_mutex);
+    m_instrumentation_runtimes.clear();
+  }
   m_next_event_action_up.reset();
   // Clear the last natural stop ID since it has a strong reference to this
   // process
@@ -6376,7 +6379,10 @@ void Process::DidExec() {
     std::lock_guard<std::recursive_mutex> guard(m_language_runtimes_mutex);
     m_language_runtimes.clear();
   }
-  m_instrumentation_runtimes.clear();
+  {
+    std::lock_guard<std::mutex> guard(m_instrumentation_runtimes_mutex);
+    m_instrumentation_runtimes.clear();
+  }
   m_thread_list.DiscardThreadPlans();
   m_memory_cache.Clear(true);
   m_memory_region_infos_cache.Clear();
@@ -6432,12 +6438,15 @@ void Process::ModulesDidLoad(ModuleList &module_list) {
 
   // Give the instrumentation runtimes a chance to be created before informing
   // them of the modified modules.
-  InstrumentationRuntime::ModulesDidLoad(module_list, this,
-                                         m_instrumentation_runtimes);
-  for (auto &runtime : m_instrumentation_runtimes) {
-    // A null entry means the plugin is disabled in this process.
-    if (runtime.second)
-      runtime.second->ModulesDidLoad(module_list);
+  {
+    std::lock_guard<std::mutex> guard(m_instrumentation_runtimes_mutex);
+    InstrumentationRuntime::ModulesDidLoad(module_list, this,
+                                           m_instrumentation_runtimes);
+    for (auto &runtime : m_instrumentation_runtimes) {
+      // A null entry means the plugin is disabled in this process.
+      if (runtime.second)
+        runtime.second->ModulesDidLoad(module_list);
+    }
   }
 
   // Give the language runtimes a chance to be created before informing them of
@@ -6517,6 +6526,7 @@ ThreadCollectionSP Process::GetHistoryThreads(lldb::addr_t addr) {
 
 InstrumentationRuntimeSP
 Process::GetInstrumentationRuntime(lldb::InstrumentationRuntimeType type) {
+  std::lock_guard<std::mutex> guard(m_instrumentation_runtimes_mutex);
   InstrumentationRuntimeCollection::iterator pos;
   pos = m_instrumentation_runtimes.find(type);
   if (pos == m_instrumentation_runtimes.end()) {
@@ -6528,6 +6538,8 @@ Process::GetInstrumentationRuntime(lldb::InstrumentationRuntimeType type) {
 llvm::Error
 Process::SetInstrumentationRuntimeEnabled(InstrumentationRuntimeType irt,
                                           bool enabled) {
+  std::lock_guard<std::mutex> guard(m_instrumentation_runtimes_mutex);
+
   if (!IsAlive())
     return llvm::createStringErrorV("process {} is not alive", GetID());
 
@@ -6546,8 +6558,11 @@ Process::SetInstrumentationRuntimeEnabled(InstrumentationRuntimeType irt,
   }
 
   // This process already has an instance of this plugin so it is already
-  // enabled.
-  if (GetInstrumentationRuntime(irt))
+  // enabled. Note the code below is basically
+  // `Process::GetInstrumentationRuntime` but we can't call it here because it
+  // tries to acquire the mutex we are already holding.
+  auto pos = m_instrumentation_runtimes.find(irt);
+  if (pos != m_instrumentation_runtimes.end() && pos->second)
     return llvm::Error::success();
 
   // There's no instance of this plugin for this process so create one. This



More information about the llvm-branch-commits mailing list