Skip to content

Commit 058acc1

Browse files
committed
Two InitializeDebugger functions
1 parent a5866e6 commit 058acc1

File tree

3 files changed

+41
-32
lines changed

3 files changed

+41
-32
lines changed

lldb/tools/lldb-dap/DAP.cpp

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1323,38 +1323,29 @@ void DAP::StartEventThreads() {
13231323
StartEventThread();
13241324
}
13251325

1326-
llvm::Error DAP::InitializeDebugger(std::optional<int> debugger_id,
1327-
std::optional<lldb::user_id_t> target_id) {
1328-
// Validate that both debugger_id and target_id are provided together.
1329-
if (debugger_id.has_value() != target_id.has_value()) {
1326+
llvm::Error DAP::InitializeDebugger(int debugger_id,
1327+
lldb::user_id_t target_id) {
1328+
// Find the existing debugger by ID
1329+
debugger = lldb::SBDebugger::FindDebuggerWithID(debugger_id);
1330+
if (!debugger.IsValid()) {
13301331
return llvm::createStringError(
1331-
"Both debuggerId and targetId must be specified together for debugger "
1332-
"reuse, or both must be omitted to create a new debugger");
1332+
"Unable to find existing debugger for debugger ID");
13331333
}
13341334

1335-
// Initialize debugger instance (shared or individual).
1336-
if (debugger_id && target_id) {
1337-
// Find the existing debugger by ID
1338-
debugger = lldb::SBDebugger::FindDebuggerWithID(*debugger_id);
1339-
if (!debugger.IsValid()) {
1340-
return llvm::createStringError(
1341-
"Unable to find existing debugger for debugger ID");
1342-
}
1343-
1344-
// Find the target within the debugger by its globally unique ID
1345-
lldb::SBTarget shared_target =
1346-
debugger.FindTargetByGloballyUniqueID(*target_id);
1347-
if (!shared_target.IsValid()) {
1348-
return llvm::createStringError(
1349-
"Unable to find existing target for target ID");
1350-
}
1351-
1352-
// Set the target for this DAP session.
1353-
SetTarget(shared_target);
1354-
StartEventThreads();
1355-
return llvm::Error::success();
1335+
// Find the target within the debugger by its globally unique ID
1336+
lldb::SBTarget target = debugger.FindTargetByGloballyUniqueID(target_id);
1337+
if (!target.IsValid()) {
1338+
return llvm::createStringError(
1339+
"Unable to find existing target for target ID");
13561340
}
13571341

1342+
// Set the target for this DAP session.
1343+
SetTarget(target);
1344+
StartEventThreads();
1345+
return llvm::Error::success();
1346+
}
1347+
1348+
llvm::Error DAP::InitializeDebugger() {
13581349
debugger = lldb::SBDebugger::Create(/*argument_name=*/false);
13591350

13601351
// Configure input/output/error file descriptors.

lldb/tools/lldb-dap/DAP.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -426,10 +426,18 @@ struct DAP final : public DAPTransport::MessageHandler {
426426
/// DAP debugger initialization functions.
427427
/// @{
428428

429-
/// Perform complete DAP initialization in one call.
430-
llvm::Error
431-
InitializeDebugger(std::optional<int> debugger_id = std::nullopt,
432-
std::optional<lldb::user_id_t> target_id = std::nullopt);
429+
/// Perform complete DAP initialization for a new debugger.
430+
llvm::Error InitializeDebugger();
431+
432+
/// Perform complete DAP initialization by reusing an existing debugger and
433+
/// target.
434+
///
435+
/// \param[in] debugger_id
436+
/// The ID of the existing debugger to reuse.
437+
///
438+
/// \param[in] target_id
439+
/// The globally unique ID of the existing target to reuse.
440+
llvm::Error InitializeDebugger(int debugger_id, lldb::user_id_t target_id);
433441

434442
/// Start event handling threads based on client capabilities.
435443
void StartEventThreads();

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,17 @@ Error AttachRequestHandler::Run(const AttachRequestArguments &args) const {
3434
// launched debugger.
3535
std::optional<int> debugger_id = args.debuggerId;
3636
std::optional<lldb::user_id_t> target_id = args.targetId;
37-
if (Error err = dap.InitializeDebugger(debugger_id, target_id))
37+
38+
// Validate that both debugger_id and target_id are provided together.
39+
if (debugger_id.has_value() != target_id.has_value()) {
40+
return llvm::createStringError(
41+
"Both debuggerId and targetId must be specified together for debugger "
42+
"reuse, or both must be omitted to create a new debugger");
43+
}
44+
45+
if (Error err = debugger_id && target_id
46+
? dap.InitializeDebugger(*debugger_id, *target_id)
47+
: dap.InitializeDebugger())
3848
return err;
3949

4050
// Validate that we have a well formed attach request.

0 commit comments

Comments
 (0)