Skip to content

Commit a6d299d

Browse files
authored
[lldb-dap] Refactor lldb-dap/DAP.{h,cpp} to use its own instance instead of the global instance. (#115948)
The refactor will unblock us for creating multiple DAP instances.
1 parent c658d07 commit a6d299d

File tree

3 files changed

+34
-30
lines changed

3 files changed

+34
-30
lines changed

lldb/tools/lldb-dap/DAP.cpp

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -477,21 +477,21 @@ lldb::SBFrame DAP::GetLLDBFrame(const llvm::json::Object &arguments) {
477477

478478
llvm::json::Value DAP::CreateTopLevelScopes() {
479479
llvm::json::Array scopes;
480-
scopes.emplace_back(CreateScope("Locals", VARREF_LOCALS,
481-
g_dap.variables.locals.GetSize(), false));
480+
scopes.emplace_back(
481+
CreateScope("Locals", VARREF_LOCALS, variables.locals.GetSize(), false));
482482
scopes.emplace_back(CreateScope("Globals", VARREF_GLOBALS,
483-
g_dap.variables.globals.GetSize(), false));
483+
variables.globals.GetSize(), false));
484484
scopes.emplace_back(CreateScope("Registers", VARREF_REGS,
485-
g_dap.variables.registers.GetSize(), false));
485+
variables.registers.GetSize(), false));
486486
return llvm::json::Value(std::move(scopes));
487487
}
488488

489489
ReplMode DAP::DetectReplMode(lldb::SBFrame frame, std::string &expression,
490490
bool partial_expression) {
491491
// Check for the escape hatch prefix.
492492
if (!expression.empty() &&
493-
llvm::StringRef(expression).starts_with(g_dap.command_escape_prefix)) {
494-
expression = expression.substr(g_dap.command_escape_prefix.size());
493+
llvm::StringRef(expression).starts_with(command_escape_prefix)) {
494+
expression = expression.substr(command_escape_prefix.size());
495495
return ReplMode::Command;
496496
}
497497

@@ -531,7 +531,7 @@ ReplMode DAP::DetectReplMode(lldb::SBFrame frame, std::string &expression,
531531
<< "Warning: Expression '" << term
532532
<< "' is both an LLDB command and variable. It will be evaluated as "
533533
"a variable. To evaluate the expression as an LLDB command, use '"
534-
<< g_dap.command_escape_prefix << "' as a prefix.\n";
534+
<< command_escape_prefix << "' as a prefix.\n";
535535
}
536536

537537
// Variables take preference to commands in auto, since commands can always
@@ -901,7 +901,7 @@ bool StartDebuggingRequestHandler::DoExecute(
901901
return false;
902902
}
903903

904-
g_dap.SendReverseRequest(
904+
dap.SendReverseRequest(
905905
"startDebugging",
906906
llvm::json::Object{{"request", request},
907907
{"configuration", std::move(*configuration)}},
@@ -925,7 +925,7 @@ bool ReplModeRequestHandler::DoExecute(lldb::SBDebugger debugger,
925925
// If a new mode is not specified report the current mode.
926926
if (!command || llvm::StringRef(command[0]).empty()) {
927927
std::string mode;
928-
switch (g_dap.repl_mode) {
928+
switch (dap.repl_mode) {
929929
case ReplMode::Variable:
930930
mode = "variable";
931931
break;
@@ -946,11 +946,11 @@ bool ReplModeRequestHandler::DoExecute(lldb::SBDebugger debugger,
946946
llvm::StringRef new_mode{command[0]};
947947

948948
if (new_mode == "variable") {
949-
g_dap.repl_mode = ReplMode::Variable;
949+
dap.repl_mode = ReplMode::Variable;
950950
} else if (new_mode == "command") {
951-
g_dap.repl_mode = ReplMode::Command;
951+
dap.repl_mode = ReplMode::Command;
952952
} else if (new_mode == "auto") {
953-
g_dap.repl_mode = ReplMode::Auto;
953+
dap.repl_mode = ReplMode::Auto;
954954
} else {
955955
lldb::SBStream error_message;
956956
error_message.Printf("Invalid repl-mode '%s'. Expected one of 'variable', "
@@ -1022,7 +1022,7 @@ bool SendEventRequestHandler::DoExecute(lldb::SBDebugger debugger,
10221022
event.try_emplace("body", std::move(*body));
10231023
}
10241024

1025-
g_dap.SendJSON(llvm::json::Value(std::move(event)));
1025+
dap.SendJSON(llvm::json::Value(std::move(event)));
10261026
result.SetStatus(lldb::eReturnStatusSuccessFinishNoResult);
10271027
return true;
10281028
}
@@ -1031,29 +1031,27 @@ void DAP::SetFrameFormat(llvm::StringRef format) {
10311031
if (format.empty())
10321032
return;
10331033
lldb::SBError error;
1034-
g_dap.frame_format = lldb::SBFormat(format.str().c_str(), error);
1034+
frame_format = lldb::SBFormat(format.str().c_str(), error);
10351035
if (error.Fail()) {
1036-
g_dap.SendOutput(
1037-
OutputType::Console,
1038-
llvm::formatv(
1039-
"The provided frame format '{0}' couldn't be parsed: {1}\n", format,
1040-
error.GetCString())
1041-
.str());
1036+
SendOutput(OutputType::Console,
1037+
llvm::formatv(
1038+
"The provided frame format '{0}' couldn't be parsed: {1}\n",
1039+
format, error.GetCString())
1040+
.str());
10421041
}
10431042
}
10441043

10451044
void DAP::SetThreadFormat(llvm::StringRef format) {
10461045
if (format.empty())
10471046
return;
10481047
lldb::SBError error;
1049-
g_dap.thread_format = lldb::SBFormat(format.str().c_str(), error);
1048+
thread_format = lldb::SBFormat(format.str().c_str(), error);
10501049
if (error.Fail()) {
1051-
g_dap.SendOutput(
1052-
OutputType::Console,
1053-
llvm::formatv(
1054-
"The provided thread format '{0}' couldn't be parsed: {1}\n",
1055-
format, error.GetCString())
1056-
.str());
1050+
SendOutput(OutputType::Console,
1051+
llvm::formatv(
1052+
"The provided thread format '{0}' couldn't be parsed: {1}\n",
1053+
format, error.GetCString())
1054+
.str());
10571055
}
10581056
}
10591057

lldb/tools/lldb-dap/DAP.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,16 +116,22 @@ struct Variables {
116116
};
117117

118118
struct StartDebuggingRequestHandler : public lldb::SBCommandPluginInterface {
119+
DAP &dap;
120+
explicit StartDebuggingRequestHandler(DAP &d) : dap(d) {};
119121
bool DoExecute(lldb::SBDebugger debugger, char **command,
120122
lldb::SBCommandReturnObject &result) override;
121123
};
122124

123125
struct ReplModeRequestHandler : public lldb::SBCommandPluginInterface {
126+
DAP &dap;
127+
explicit ReplModeRequestHandler(DAP &d) : dap(d) {};
124128
bool DoExecute(lldb::SBDebugger debugger, char **command,
125129
lldb::SBCommandReturnObject &result) override;
126130
};
127131

128132
struct SendEventRequestHandler : public lldb::SBCommandPluginInterface {
133+
DAP &dap;
134+
explicit SendEventRequestHandler(DAP &d) : dap(d) {};
129135
bool DoExecute(lldb::SBDebugger debugger, char **command,
130136
lldb::SBCommandReturnObject &result) override;
131137
};

lldb/tools/lldb-dap/lldb-dap.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1889,14 +1889,14 @@ void request_initialize(const llvm::json::Object &request) {
18891889
"lldb-dap", "Commands for managing lldb-dap.");
18901890
if (GetBoolean(arguments, "supportsStartDebuggingRequest", false)) {
18911891
cmd.AddCommand(
1892-
"start-debugging", new StartDebuggingRequestHandler(),
1892+
"start-debugging", new StartDebuggingRequestHandler(g_dap),
18931893
"Sends a startDebugging request from the debug adapter to the client "
18941894
"to start a child debug session of the same type as the caller.");
18951895
}
18961896
cmd.AddCommand(
1897-
"repl-mode", new ReplModeRequestHandler(),
1897+
"repl-mode", new ReplModeRequestHandler(g_dap),
18981898
"Get or set the repl behavior of lldb-dap evaluation requests.");
1899-
cmd.AddCommand("send-event", new SendEventRequestHandler(),
1899+
cmd.AddCommand("send-event", new SendEventRequestHandler(g_dap),
19001900
"Sends an DAP event to the client.");
19011901

19021902
g_dap.progress_event_thread = std::thread(ProgressEventThreadFunction);

0 commit comments

Comments
 (0)