Skip to content

Commit e69e975

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

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
@@ -478,21 +478,21 @@ lldb::SBFrame DAP::GetLLDBFrame(const llvm::json::Object &arguments) {
478478

479479
llvm::json::Value DAP::CreateTopLevelScopes() {
480480
llvm::json::Array scopes;
481-
scopes.emplace_back(CreateScope("Locals", VARREF_LOCALS,
482-
g_dap.variables.locals.GetSize(), false));
481+
scopes.emplace_back(
482+
CreateScope("Locals", VARREF_LOCALS, variables.locals.GetSize(), false));
483483
scopes.emplace_back(CreateScope("Globals", VARREF_GLOBALS,
484-
g_dap.variables.globals.GetSize(), false));
484+
variables.globals.GetSize(), false));
485485
scopes.emplace_back(CreateScope("Registers", VARREF_REGS,
486-
g_dap.variables.registers.GetSize(), false));
486+
variables.registers.GetSize(), false));
487487
return llvm::json::Value(std::move(scopes));
488488
}
489489

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

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

538538
// Variables take preference to commands in auto, since commands can always
@@ -903,7 +903,7 @@ bool StartDebuggingRequestHandler::DoExecute(
903903
return false;
904904
}
905905

906-
g_dap.SendReverseRequest(
906+
dap.SendReverseRequest(
907907
"startDebugging",
908908
llvm::json::Object{{"request", request},
909909
{"configuration", std::move(*configuration)}},
@@ -927,7 +927,7 @@ bool ReplModeRequestHandler::DoExecute(lldb::SBDebugger debugger,
927927
// If a new mode is not specified report the current mode.
928928
if (!command || llvm::StringRef(command[0]).empty()) {
929929
std::string mode;
930-
switch (g_dap.repl_mode) {
930+
switch (dap.repl_mode) {
931931
case ReplMode::Variable:
932932
mode = "variable";
933933
break;
@@ -948,11 +948,11 @@ bool ReplModeRequestHandler::DoExecute(lldb::SBDebugger debugger,
948948
llvm::StringRef new_mode{command[0]};
949949

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

1027-
g_dap.SendJSON(llvm::json::Value(std::move(event)));
1027+
dap.SendJSON(llvm::json::Value(std::move(event)));
10281028
result.SetStatus(lldb::eReturnStatusSuccessFinishNoResult);
10291029
return true;
10301030
}
@@ -1033,29 +1033,27 @@ void DAP::SetFrameFormat(llvm::StringRef format) {
10331033
if (format.empty())
10341034
return;
10351035
lldb::SBError error;
1036-
g_dap.frame_format = lldb::SBFormat(format.str().c_str(), error);
1036+
frame_format = lldb::SBFormat(format.str().c_str(), error);
10371037
if (error.Fail()) {
1038-
g_dap.SendOutput(
1039-
OutputType::Console,
1040-
llvm::formatv(
1041-
"The provided frame format '{0}' couldn't be parsed: {1}\n", format,
1042-
error.GetCString())
1043-
.str());
1038+
SendOutput(OutputType::Console,
1039+
llvm::formatv(
1040+
"The provided frame format '{0}' couldn't be parsed: {1}\n",
1041+
format, error.GetCString())
1042+
.str());
10441043
}
10451044
}
10461045

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

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
@@ -1891,14 +1891,14 @@ void request_initialize(const llvm::json::Object &request) {
18911891
"lldb-dap", "Commands for managing lldb-dap.");
18921892
if (GetBoolean(arguments, "supportsStartDebuggingRequest", false)) {
18931893
cmd.AddCommand(
1894-
"start-debugging", new StartDebuggingRequestHandler(),
1894+
"start-debugging", new StartDebuggingRequestHandler(g_dap),
18951895
"Sends a startDebugging request from the debug adapter to the client "
18961896
"to start a child debug session of the same type as the caller.");
18971897
}
18981898
cmd.AddCommand(
1899-
"repl-mode", new ReplModeRequestHandler(),
1899+
"repl-mode", new ReplModeRequestHandler(g_dap),
19001900
"Get or set the repl behavior of lldb-dap evaluation requests.");
1901-
cmd.AddCommand("send-event", new SendEventRequestHandler(),
1901+
cmd.AddCommand("send-event", new SendEventRequestHandler(g_dap),
19021902
"Sends an DAP event to the client.");
19031903

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

0 commit comments

Comments
 (0)