[Lldb-commits] [lldb] [lldb-dap] Detect if we can automatically attach to targets. (PR #170792)

John Harrison via lldb-commits lldb-commits at lists.llvm.org
Thu Dec 4 18:12:57 PST 2025


https://github.com/ashgti updated https://github.com/llvm/llvm-project/pull/170792

>From efcdf2180b327486def24a17fcd37241c1ff1043 Mon Sep 17 00:00:00 2001
From: John Harrison <harjohn at google.com>
Date: Thu, 4 Dec 2025 17:48:45 -0800
Subject: [PATCH 1/2] [lldb-dap] Detect if we can automatically attach to
 targets.

The new support for automatically attaching to targets during a debug session is not guarded on how lldb-dap is running. If its running over stdio then this feature will not work.

Instead, I added a message to the console and skip attempting to attach.
---
 lldb/tools/lldb-dap/DAPSessionManager.h | 4 ++++
 lldb/tools/lldb-dap/EventHelper.cpp     | 7 +++++++
 lldb/tools/lldb-dap/tool/lldb-dap.cpp   | 2 ++
 3 files changed, 13 insertions(+)

diff --git a/lldb/tools/lldb-dap/DAPSessionManager.h b/lldb/tools/lldb-dap/DAPSessionManager.h
index ad76b081ad78b..193a1c7ebce5a 100644
--- a/lldb/tools/lldb-dap/DAPSessionManager.h
+++ b/lldb/tools/lldb-dap/DAPSessionManager.h
@@ -59,6 +59,9 @@ class DAPSessionManager {
   /// Get the singleton instance of the DAP session manager.
   static DAPSessionManager &GetInstance();
 
+  void SetServerMode() { m_server_mode = true; }
+  bool GetServerMode() { return m_server_mode; }
+
   /// Register a DAP session.
   void RegisterSession(lldb_private::MainLoop *loop, DAP *dap);
 
@@ -103,6 +106,7 @@ class DAPSessionManager {
   DAPSessionManager(DAPSessionManager &&) = delete;
   DAPSessionManager &operator=(DAPSessionManager &&) = delete;
 
+  bool m_server_mode = false;
   bool m_client_failed = false;
   std::mutex m_sessions_mutex;
   std::condition_variable m_sessions_condition;
diff --git a/lldb/tools/lldb-dap/EventHelper.cpp b/lldb/tools/lldb-dap/EventHelper.cpp
index bdb6bb55fe168..526e406031b7a 100644
--- a/lldb/tools/lldb-dap/EventHelper.cpp
+++ b/lldb/tools/lldb-dap/EventHelper.cpp
@@ -449,6 +449,13 @@ void HandleTargetEvent(const lldb::SBEvent &event, Log *log) {
       }
     }
   } else if (event_mask & lldb::SBTarget::eBroadcastBitNewTargetCreated) {
+    if (!DAPSessionManager::GetInstance().GetServerMode()) {
+      dap->SendOutput(OutputType::Console,
+                      "lldb-dap: Enable server mode for automatically "
+                      "attaching to new debugger targets.");
+      return;
+    }
+
     // For NewTargetCreated events, GetTargetFromEvent returns the parent
     // target, and GetCreatedTargetFromEvent returns the newly created target.
     lldb::SBTarget created_target =
diff --git a/lldb/tools/lldb-dap/tool/lldb-dap.cpp b/lldb/tools/lldb-dap/tool/lldb-dap.cpp
index 27516b2a25678..2499b4dd12e6b 100644
--- a/lldb/tools/lldb-dap/tool/lldb-dap.cpp
+++ b/lldb/tools/lldb-dap/tool/lldb-dap.cpp
@@ -425,6 +425,8 @@ static llvm::Error serveConnection(
     return status.takeError();
   }
 
+  DAPSessionManager::GetInstance().SetServerMode();
+
   std::string address = llvm::join(listener->GetListeningConnectionURI(), ", ");
   DAP_LOG(log, "started with connection listeners {0}", address);
 

>From 275689078419b7cb0b714529d5718eafc30c8372 Mon Sep 17 00:00:00 2001
From: John Harrison <harjohn at google.com>
Date: Thu, 4 Dec 2025 18:12:37 -0800
Subject: [PATCH 2/2] Adding a comment to help clarify things.

---
 lldb/tools/lldb-dap/EventHelper.cpp | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/lldb/tools/lldb-dap/EventHelper.cpp b/lldb/tools/lldb-dap/EventHelper.cpp
index 526e406031b7a..2c15dd876cdc7 100644
--- a/lldb/tools/lldb-dap/EventHelper.cpp
+++ b/lldb/tools/lldb-dap/EventHelper.cpp
@@ -28,6 +28,8 @@
 #include "llvm/Support/Error.h"
 #include "llvm/Support/FormatVariadic.h"
 #include "llvm/Support/Threading.h"
+#include "llvm/Support/WithColor.h"
+#include "llvm/Support/raw_ostream.h"
 #include <mutex>
 #include <utility>
 
@@ -449,10 +451,15 @@ void HandleTargetEvent(const lldb::SBEvent &event, Log *log) {
       }
     }
   } else if (event_mask & lldb::SBTarget::eBroadcastBitNewTargetCreated) {
+    // This feature is only supported if lldb-dap is running in server mode,
+    // otherwise multiple clients cannot connect over stdio.
     if (!DAPSessionManager::GetInstance().GetServerMode()) {
-      dap->SendOutput(OutputType::Console,
-                      "lldb-dap: Enable server mode for automatically "
-                      "attaching to new debugger targets.");
+      std::string message;
+      raw_string_ostream OS(message);
+      llvm::WithColor::remark(OS)
+          << "lldb-dap: Enable server mode for automatically attaching to new "
+             "debugger targets.";
+      dap->SendOutput(OutputType::Console, message);
       return;
     }
 



More information about the lldb-commits mailing list