Skip to content

Commit 3121f75

Browse files
authored
[lldb-dap] Refactor lldb-dap.cpp to not use global DAP variable. (#116272)
This removes the global DAP variable and instead allocates a DAP instance in main. This should allow us to refactor lldb-dap to enable a server mode that accepts multiple connections.
1 parent 59da1af commit 3121f75

File tree

3 files changed

+553
-558
lines changed

3 files changed

+553
-558
lines changed

lldb/tools/lldb-dap/DAP.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,18 @@ using namespace lldb_dap;
3232

3333
namespace lldb_dap {
3434

35-
DAP g_dap;
36-
37-
DAP::DAP()
38-
: broadcaster("lldb-dap"), exception_breakpoints(),
39-
focus_tid(LLDB_INVALID_THREAD_ID), stop_at_entry(false), is_attach(false),
35+
DAP::DAP(llvm::StringRef path, ReplMode repl_mode)
36+
: debug_adaptor_path(path), broadcaster("lldb-dap"),
37+
exception_breakpoints(), focus_tid(LLDB_INVALID_THREAD_ID),
38+
stop_at_entry(false), is_attach(false),
4039
enable_auto_variable_summaries(false),
4140
enable_synthetic_child_debugging(false),
4241
display_extended_backtrace(false),
4342
restarting_process_id(LLDB_INVALID_PROCESS_ID),
4443
configuration_done_sent(false), waiting_for_run_in_terminal(false),
4544
progress_event_reporter(
4645
[&](const ProgressEvent &event) { SendJSON(event.ToJSON()); }),
47-
reverse_request_seq(0), repl_mode(ReplMode::Auto) {
46+
reverse_request_seq(0), repl_mode(repl_mode) {
4847
const char *log_file_path = getenv("LLDBDAP_LOG");
4948
#if defined(_WIN32)
5049
// Windows opens stdout and stdin in text mode which converts \n to 13,10
@@ -693,15 +692,15 @@ bool DAP::HandleObject(const llvm::json::Object &object) {
693692
if (packet_type == "request") {
694693
const auto command = GetString(object, "command");
695694
auto handler_pos = request_handlers.find(command);
696-
if (handler_pos != request_handlers.end()) {
697-
handler_pos->second(object);
698-
return true; // Success
699-
} else {
695+
if (handler_pos == request_handlers.end()) {
700696
if (log)
701697
*log << "error: unhandled command \"" << command.data() << "\""
702698
<< std::endl;
703699
return false; // Fail
704700
}
701+
702+
handler_pos->second(*this, object);
703+
return true; // Success
705704
}
706705

707706
if (packet_type == "response") {

lldb/tools/lldb-dap/DAP.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ enum DAPBroadcasterBits {
6363
eBroadcastBitStopProgressThread = 1u << 1
6464
};
6565

66-
typedef void (*RequestCallback)(const llvm::json::Object &command);
66+
typedef void (*RequestCallback)(DAP &dap, const llvm::json::Object &command);
6767
typedef void (*ResponseCallback)(llvm::Expected<llvm::json::Value> value);
6868

6969
enum class PacketStatus {
@@ -137,7 +137,7 @@ struct SendEventRequestHandler : public lldb::SBCommandPluginInterface {
137137
};
138138

139139
struct DAP {
140-
std::string debug_adaptor_path;
140+
llvm::StringRef debug_adaptor_path;
141141
InputStream input;
142142
OutputStream output;
143143
lldb::SBDebugger debugger;
@@ -198,7 +198,7 @@ struct DAP {
198198
// will contain that expression.
199199
std::string last_nonempty_var_expression;
200200

201-
DAP();
201+
DAP(llvm::StringRef path, ReplMode repl_mode);
202202
~DAP();
203203
DAP(const DAP &rhs) = delete;
204204
void operator=(const DAP &rhs) = delete;
@@ -353,8 +353,6 @@ struct DAP {
353353
void SendJSON(const std::string &json_str);
354354
};
355355

356-
extern DAP g_dap;
357-
358356
} // namespace lldb_dap
359357

360358
#endif

0 commit comments

Comments
 (0)