llvm-project/lldb/unittests/DAP/DAPSessionManagerTest.cpp
Janet Yang 5ab3375b2c
[lldb-dap] Add multi-session support with shared debugger instances (#163653)
## Summary:
This change introduces a `DAPSessionManager` to enable multiple DAP
sessions to share debugger instances when needed, for things like child
process debugging and some scripting hooks that create dynamically new
targets.

Changes include:
- Add `DAPSessionManager` singleton to track and coordinate all active DAP
sessions
- Support attaching to an existing target via its globally unique target
ID (targetId parameter)
- Share debugger instances across sessions when new targets are created
dynamically
- Refactor event thread management to allow sharing event threads
between sessions and move event thread and event thread handlers to `EventHelpers`
- Add `eBroadcastBitNewTargetCreated` event to notify when new targets are
created
- Extract session names from target creation events
- Defer debugger initialization from 'initialize' request to
'launch'/'attach' requests. The only time the debugger is used currently
in between its creation in `InitializeRequestHandler` and the `Launch`
or `Attach` requests is during the `TelemetryDispatcher` destruction
call at the end of the `DAP::HandleObject` call, so this is safe.

This enables scenarios when new targets are created dynamically so that
the debug adapter can automatically start a new debug session for the
spawned target while sharing the debugger instance.

## Tests:
The refactoring maintains backward compatibility. All existing DAP test
cases pass.

Also added a few basic unit tests for DAPSessionManager
```
>> ninja DAPTests
>> ./tools/lldb/unittests/DAP/DAPTests
>>./bin/llvm-lit -v ../llvm-project/lldb/test/API/tools/lldb-dap/
```
2025-11-26 10:32:25 -08:00

104 lines
3.6 KiB
C++

//===-- DAPSessionManagerTest.cpp ----------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
#include "DAPSessionManager.h"
#include "TestBase.h"
#include "lldb/API/SBDebugger.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
using namespace lldb_dap;
using namespace lldb;
using namespace lldb_dap_tests;
class DAPSessionManagerTest : public DAPTestBase {};
TEST_F(DAPSessionManagerTest, GetInstanceReturnsSameSingleton) {
DAPSessionManager &instance1 = DAPSessionManager::GetInstance();
DAPSessionManager &instance2 = DAPSessionManager::GetInstance();
EXPECT_EQ(&instance1, &instance2);
}
// UnregisterSession uses std::notify_all_at_thread_exit, so it must be called
// from a separate thread to properly release the mutex on thread exit.
TEST_F(DAPSessionManagerTest, RegisterAndUnregisterSession) {
DAPSessionManager &manager = DAPSessionManager::GetInstance();
// Initially not registered.
std::vector<DAP *> sessions_before = manager.GetActiveSessions();
EXPECT_EQ(
std::count(sessions_before.begin(), sessions_before.end(), dap.get()), 0);
manager.RegisterSession(&loop, dap.get());
// Should be in active sessions after registration.
std::vector<DAP *> sessions_after = manager.GetActiveSessions();
EXPECT_EQ(std::count(sessions_after.begin(), sessions_after.end(), dap.get()),
1);
// Unregister.
std::thread unregister_thread([&]() { manager.UnregisterSession(&loop); });
unregister_thread.join();
// There should no longer be active sessions.
std::vector<DAP *> sessions_final = manager.GetActiveSessions();
EXPECT_EQ(std::count(sessions_final.begin(), sessions_final.end(), dap.get()),
0);
}
TEST_F(DAPSessionManagerTest, DisconnectAllSessions) {
DAPSessionManager &manager = DAPSessionManager::GetInstance();
manager.RegisterSession(&loop, dap.get());
std::vector<DAP *> sessions = manager.GetActiveSessions();
EXPECT_EQ(std::count(sessions.begin(), sessions.end(), dap.get()), 1);
manager.DisconnectAllSessions();
// DisconnectAllSessions shutdown but doesn't wait for
// sessions to complete or remove them from the active sessions map.
sessions = manager.GetActiveSessions();
EXPECT_EQ(std::count(sessions.begin(), sessions.end(), dap.get()), 1);
std::thread unregister_thread([&]() { manager.UnregisterSession(&loop); });
unregister_thread.join();
}
TEST_F(DAPSessionManagerTest, WaitForAllSessionsToDisconnect) {
DAPSessionManager &manager = DAPSessionManager::GetInstance();
manager.RegisterSession(&loop, dap.get());
std::vector<DAP *> sessions = manager.GetActiveSessions();
EXPECT_EQ(std::count(sessions.begin(), sessions.end(), dap.get()), 1);
// Unregister after a delay to test blocking behavior.
std::thread unregister_thread([&]() {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
manager.UnregisterSession(&loop);
});
// WaitForAllSessionsToDisconnect should block until unregistered.
auto start = std::chrono::steady_clock::now();
llvm::Error err = manager.WaitForAllSessionsToDisconnect();
EXPECT_FALSE(err);
auto duration = std::chrono::steady_clock::now() - start;
// Verify it waited at least 100ms.
EXPECT_GE(duration, std::chrono::milliseconds(100));
// Session should be unregistered now.
sessions = manager.GetActiveSessions();
EXPECT_EQ(std::count(sessions.begin(), sessions.end(), dap.get()), 0);
unregister_thread.join();
}