[Lldb-commits] [lldb] [llvm] [lldb-dap] Add multi-session support with shared debugger instances (PR #163653)
Walter Erquinigo via lldb-commits
lldb-commits at lists.llvm.org
Tue Oct 21 09:42:16 PDT 2025
================
@@ -0,0 +1,125 @@
+//===-- DAPSessionManager.h ------------------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLDB_TOOLS_LLDB_DAP_DAPSESSIONMANAGER_H
+#define LLDB_TOOLS_LLDB_DAP_DAPSESSIONMANAGER_H
+
+#include "lldb/API/SBBroadcaster.h"
+#include "lldb/API/SBDebugger.h"
+#include "lldb/API/SBTarget.h"
+#include "lldb/Host/MainLoop.h"
+#include "lldb/lldb-types.h"
+#include "llvm/Support/Error.h"
+#include <condition_variable>
+#include <map>
+#include <memory>
+#include <mutex>
+#include <optional>
+#include <thread>
+#include <vector>
+
+namespace lldb_dap {
+
+// Forward declarations
+struct DAP;
+
+class ManagedEventThread {
+public:
+ // Constructor declaration
+ ManagedEventThread(lldb::SBBroadcaster broadcaster, std::thread t);
+
+ ~ManagedEventThread();
+
+ ManagedEventThread(const ManagedEventThread &) = delete;
+ ManagedEventThread &operator=(const ManagedEventThread &) = delete;
+
+private:
+ lldb::SBBroadcaster m_broadcaster;
+ std::thread m_event_thread;
+};
+
+/// Global DAP session manager that manages multiple concurrent DAP sessions in
+/// a single lldb-dap process. Handles session lifecycle tracking, coordinates
+/// shared debugger event threads, and facilitates target handoff between
+/// sessions for dynamically created targets.
+class DAPSessionManager {
+public:
+ /// Get the singleton instance of the DAP session manager.
+ static DAPSessionManager &GetInstance();
+
+ /// Register a DAP session.
+ void RegisterSession(lldb_private::MainLoop *loop, DAP *dap);
+
+ /// Unregister a DAP session. Called by sessions when they complete their
+ /// disconnection, which unblocks WaitForAllSessionsToDisconnect().
+ void UnregisterSession(lldb_private::MainLoop *loop);
+
+ /// Get all active DAP sessions.
+ std::vector<DAP *> GetActiveSessions();
+
+ /// Disconnect all registered sessions by calling Disconnect() on
+ /// each and requesting their event loops to terminate. Used during
+ /// shutdown to force all sessions to begin disconnecting.
+ void DisconnectAllSessions();
+
+ /// Block until all sessions disconnect and unregister. Returns an error if
+ /// DisconnectAllSessions() was called and any disconnection failed.
+ llvm::Error WaitForAllSessionsToDisconnect();
+
+ /// Store a target for later retrieval by another session.
+ void StoreTargetById(uint32_t target_id, lldb::SBTarget target);
----------------
walter-erquinigo wrote:
Now that I think of it, you don't need to this mapping.
Assuming that a DAP debug session has a single debugger instance, which I think is true 99.999% of the cases, whenever you want to attach to a child target by ID, you can get the Debugger instance of any existing target, then you can ask its debugger for its target list, and then you you do a linear scan on it finding the ID you want.
Then you can avoid doing all this tracking.
https://github.com/llvm/llvm-project/pull/163653
More information about the lldb-commits
mailing list