Skip to content

Commit d1cc643

Browse files
committed
Use lldb::user_id_t for target_id
Summary: - Changed targetId type from uint32_t to lldb::user_id_t - Added comprehensive documentation for StoreTargetById/TakeTargetById
1 parent 1c221a7 commit d1cc643

File tree

7 files changed

+31
-12
lines changed

7 files changed

+31
-12
lines changed

lldb/test/API/tools/lldb-dap/attach/TestDAP_attach.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,4 @@ def test_attach_with_invalid_targetId(self):
8282

8383
resp = self.attach(targetId=99999, expectFailure=True)
8484
self.assertFalse(resp["success"])
85-
self.assertIn("Unable to find existing target", resp["body"]["error"]["format"])
85+
self.assertIn("invalid target_id 99999 in attach config", resp["body"]["error"]["format"])

lldb/tools/lldb-dap/DAP.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,7 +1309,7 @@ void DAP::StartEventThreads() {
13091309
StartEventThread();
13101310
}
13111311

1312-
llvm::Error DAP::InitializeDebugger(std::optional<uint32_t> target_id) {
1312+
llvm::Error DAP::InitializeDebugger(std::optional<lldb::user_id_t> target_id) {
13131313
// Initialize debugger instance (shared or individual).
13141314
if (target_id) {
13151315
std::optional<lldb::SBTarget> shared_target =
@@ -1569,7 +1569,7 @@ void DAP::EventThread() {
15691569
auto target = lldb::SBTarget::GetTargetFromEvent(event);
15701570

15711571
// Generate unique target ID and store the target for handoff.
1572-
uint32_t target_id = target.GetGloballyUniqueID();
1572+
lldb::user_id_t target_id = target.GetGloballyUniqueID();
15731573
DAPSessionManager::GetInstance().StoreTargetById(target_id, target);
15741574

15751575
// We create an attach config that will select the unique

lldb/tools/lldb-dap/DAP.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ struct DAP final : public DAPTransport::MessageHandler {
431431

432432
/// Perform complete DAP initialization in one call.
433433
llvm::Error
434-
InitializeDebugger(std::optional<uint32_t> target_idx = std::nullopt);
434+
InitializeDebugger(std::optional<lldb::user_id_t> target_idx = std::nullopt);
435435

436436
/// Start event handling threads based on client capabilities.
437437
void StartEventThreads();

lldb/tools/lldb-dap/DAPSessionManager.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,14 @@ DAPSessionManager::GetEventThreadForDebugger(lldb::SBDebugger debugger,
118118
return new_thread_sp;
119119
}
120120

121-
void DAPSessionManager::StoreTargetById(uint32_t target_id,
121+
void DAPSessionManager::StoreTargetById(lldb::user_id_t target_id,
122122
lldb::SBTarget target) {
123123
std::lock_guard<std::mutex> lock(m_sessions_mutex);
124124
m_target_map[target_id] = target;
125125
}
126126

127127
std::optional<lldb::SBTarget>
128-
DAPSessionManager::TakeTargetById(uint32_t target_id) {
128+
DAPSessionManager::TakeTargetById(lldb::user_id_t target_id) {
129129
std::lock_guard<std::mutex> lock(m_sessions_mutex);
130130
auto pos = m_target_map.find(target_id);
131131
if (pos == m_target_map.end())

lldb/tools/lldb-dap/DAPSessionManager.h

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,29 @@ class DAPSessionManager {
7272
llvm::Error WaitForAllSessionsToDisconnect();
7373

7474
/// Store a target for later retrieval by another session.
75-
void StoreTargetById(uint32_t target_id, lldb::SBTarget target);
75+
///
76+
/// When a new target is dynamically created (e.g., via scripting hooks or
77+
/// child process debugging), it needs to be handed off to a new DAP session.
78+
/// This method stores the target in a temporary map using its globally unique
79+
/// ID so that the new session can retrieve it via TakeTargetById().
80+
///
81+
/// \param target_id The globally unique ID of the target (from
82+
/// SBTarget::GetGloballyUniqueID()).
83+
/// \param target The target to store.
84+
void StoreTargetById(lldb::user_id_t target_id, lldb::SBTarget target);
7685

7786
/// Retrieve and remove a stored target by its globally unique target ID.
78-
std::optional<lldb::SBTarget> TakeTargetById(uint32_t target_id);
87+
///
88+
/// This method is called during the attach request when a new DAP session
89+
/// wants to attach to a dynamically created target. The target is removed
90+
/// from the map after retrieval because:
91+
/// 1. Each target should only be attached to by one DAP session
92+
/// 2. Once attached, the DAP session takes ownership of the target
93+
/// 3. Keeping the target in the map would prevent proper cleanup
94+
///
95+
/// \param target_id The globally unique ID of the target to retrieve.
96+
/// \return The target if found, std::nullopt otherwise.
97+
std::optional<lldb::SBTarget> TakeTargetById(lldb::user_id_t target_id);
7998

8099
/// Get or create event thread for a specific debugger.
81100
std::shared_ptr<ManagedEventThread>
@@ -112,7 +131,7 @@ class DAPSessionManager {
112131

113132
/// Map from target ID to target for handing off newly created targets
114133
/// between sessions.
115-
std::map<uint32_t, lldb::SBTarget> m_target_map;
134+
std::map<lldb::user_id_t, lldb::SBTarget> m_target_map;
116135

117136
/// Map from debugger ID to its event thread, used when multiple DAP sessions
118137
/// share the same debugger instance.

lldb/tools/lldb-dap/Handler/AttachRequestHandler.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ namespace lldb_dap {
3232
Error AttachRequestHandler::Run(const AttachRequestArguments &args) const {
3333
// Initialize DAP debugger and related components if not sharing previously
3434
// launched debugger.
35-
std::optional<uint32_t> target_id = args.targetId;
35+
std::optional<lldb::user_id_t> target_id = args.targetId;
3636
if (Error err = dap.InitializeDebugger(target_id))
3737
return err;
3838

@@ -76,7 +76,7 @@ Error AttachRequestHandler::Run(const AttachRequestArguments &args) const {
7676
// Use the unique target ID to get the target.
7777
target = dap.debugger.FindTargetByGloballyUniqueID(*target_id);
7878
if (!target.IsValid()) {
79-
error.SetErrorStringWithFormat("invalid target_id %u in attach config",
79+
error.SetErrorStringWithFormat("invalid target_id %lu in attach config",
8080
*target_id);
8181
}
8282
} else {

lldb/tools/lldb-dap/Protocol/ProtocolRequests.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ struct AttachRequestArguments {
350350
std::string coreFile;
351351

352352
/// Unique ID of an existing target to attach to.
353-
std::optional<uint32_t> targetId;
353+
std::optional<lldb::user_id_t> targetId;
354354

355355
/// @}
356356
};

0 commit comments

Comments
 (0)