Skip to content

Commit d0e37d9

Browse files
authored
[lldb-dap] Refactor request handlers (NFC) (#128262)
Currently, all request handlers are implemented as free functions in lldb-dap.cpp. That file has grown to over 5000 lines and is starting to become hard to maintain. This PR moves the request handlers into their own class (and file), together with their documentation. This PR migrates about a third of the request handlers and the rest will be migrated in subsequent commits. I'm merging this in an incomplete state because almost any lldb-dap change is going to result in merge conflicts and migrating request handlers one by one is easier to review.
1 parent 4d928d5 commit d0e37d9

19 files changed

+2654
-2222
lines changed

lldb/tools/lldb-dap/CMakeLists.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ add_lldb_tool(lldb-dap
2323
Breakpoint.cpp
2424
BreakpointBase.cpp
2525
DAP.cpp
26+
EventHelper.cpp
2627
ExceptionBreakpoint.cpp
2728
FifoFiles.cpp
2829
FunctionBreakpoint.cpp
@@ -36,6 +37,19 @@ add_lldb_tool(lldb-dap
3637
SourceBreakpoint.cpp
3738
Watchpoint.cpp
3839

40+
Handler/AttachRequestHandler.cpp
41+
Handler/BreakpointLocationsHandler.cpp
42+
Handler/CompletionsHandler.cpp
43+
Handler/ConfigurationDoneRequestHandler.cpp
44+
Handler/ContinueRequestHandler.cpp
45+
Handler/DisconnectRequestHandler.cpp
46+
Handler/EvaluateRequestHandler.cpp
47+
Handler/ExceptionInfoRequestHandler.cpp
48+
Handler/InitializeRequestHandler.cpp
49+
Handler/LaunchRequestHandler.cpp
50+
Handler/RequestHandler.cpp
51+
Handler/RestartRequestHandler.cpp
52+
3953
LINK_LIBS
4054
liblldb
4155
lldbHost
@@ -46,6 +60,8 @@ add_lldb_tool(lldb-dap
4660
Support
4761
)
4862

63+
target_include_directories(lldb-dap PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
64+
4965
if(LLDB_DAP_WELCOME_MESSAGE)
5066
target_compile_definitions(lldb-dap
5167
PRIVATE

lldb/tools/lldb-dap/DAP.cpp

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -757,16 +757,25 @@ bool DAP::HandleObject(const llvm::json::Object &object) {
757757
const auto packet_type = GetString(object, "type");
758758
if (packet_type == "request") {
759759
const auto command = GetString(object, "command");
760+
761+
// Try the new request handler first.
762+
auto new_handler_pos = new_request_handlers.find(command);
763+
if (new_handler_pos != new_request_handlers.end()) {
764+
(*new_handler_pos->second)(object);
765+
return true; // Success
766+
}
767+
768+
// FIXME: Remove request_handlers once everything has been migrated.
760769
auto handler_pos = request_handlers.find(command);
761-
if (handler_pos == request_handlers.end()) {
762-
if (log)
763-
*log << "error: unhandled command \"" << command.data() << "\""
764-
<< std::endl;
765-
return false; // Fail
770+
if (handler_pos != request_handlers.end()) {
771+
handler_pos->second(*this, object);
772+
return true; // Success
766773
}
767774

768-
handler_pos->second(*this, object);
769-
return true; // Success
775+
if (log)
776+
*log << "error: unhandled command \"" << command.data() << "\""
777+
<< std::endl;
778+
return false; // Fail
770779
}
771780

772781
if (packet_type == "response") {

lldb/tools/lldb-dap/DAP.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "DAPForward.h"
1313
#include "ExceptionBreakpoint.h"
1414
#include "FunctionBreakpoint.h"
15+
#include "Handler/RequestHandler.h"
1516
#include "IOStream.h"
1617
#include "InstructionBreakpoint.h"
1718
#include "OutputRedirector.h"
@@ -37,6 +38,7 @@
3738
#include "llvm/Support/JSON.h"
3839
#include "llvm/Support/Threading.h"
3940
#include <map>
41+
#include <memory>
4042
#include <mutex>
4143
#include <optional>
4244
#include <thread>
@@ -185,6 +187,7 @@ struct DAP {
185187
lldb::pid_t restarting_process_id;
186188
bool configuration_done_sent;
187189
std::map<std::string, RequestCallback, std::less<>> request_handlers;
190+
llvm::StringMap<std::unique_ptr<RequestHandler>> new_request_handlers;
188191
bool waiting_for_run_in_terminal;
189192
ProgressEventReporter progress_event_reporter;
190193
// Keep track of the last stop thread index IDs as threads won't go away
@@ -342,6 +345,12 @@ struct DAP {
342345
/// IDE.
343346
void RegisterRequestCallback(std::string request, RequestCallback callback);
344347

348+
/// Registers a request handler.
349+
template <typename Handler> void RegisterRequest() {
350+
new_request_handlers[Handler::getCommand()] =
351+
std::make_unique<Handler>(*this);
352+
}
353+
345354
/// Debuggee will continue from stopped state.
346355
void WillContinue() { variables.Clear(); }
347356

lldb/tools/lldb-dap/EventHelper.cpp

Lines changed: 238 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
1+
//===-- EventHelper.h -----------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "EventHelper.h"
10+
#include "DAP.h"
11+
#include "JSONUtils.h"
12+
#include "LLDBUtils.h"
13+
#include "lldb/API/SBFileSpec.h"
14+
15+
namespace lldb_dap {
16+
17+
static void SendThreadExitedEvent(DAP &dap, lldb::tid_t tid) {
18+
llvm::json::Object event(CreateEventObject("thread"));
19+
llvm::json::Object body;
20+
body.try_emplace("reason", "exited");
21+
body.try_emplace("threadId", (int64_t)tid);
22+
event.try_emplace("body", std::move(body));
23+
dap.SendJSON(llvm::json::Value(std::move(event)));
24+
}
25+
26+
// "ProcessEvent": {
27+
// "allOf": [
28+
// { "$ref": "#/definitions/Event" },
29+
// {
30+
// "type": "object",
31+
// "description": "Event message for 'process' event type. The event
32+
// indicates that the debugger has begun debugging a
33+
// new process. Either one that it has launched, or one
34+
// that it has attached to.",
35+
// "properties": {
36+
// "event": {
37+
// "type": "string",
38+
// "enum": [ "process" ]
39+
// },
40+
// "body": {
41+
// "type": "object",
42+
// "properties": {
43+
// "name": {
44+
// "type": "string",
45+
// "description": "The logical name of the process. This is
46+
// usually the full path to process's executable
47+
// file. Example: /home/myproj/program.js."
48+
// },
49+
// "systemProcessId": {
50+
// "type": "integer",
51+
// "description": "The system process id of the debugged process.
52+
// This property will be missing for non-system
53+
// processes."
54+
// },
55+
// "isLocalProcess": {
56+
// "type": "boolean",
57+
// "description": "If true, the process is running on the same
58+
// computer as the debug adapter."
59+
// },
60+
// "startMethod": {
61+
// "type": "string",
62+
// "enum": [ "launch", "attach", "attachForSuspendedLaunch" ],
63+
// "description": "Describes how the debug engine started
64+
// debugging this process.",
65+
// "enumDescriptions": [
66+
// "Process was launched under the debugger.",
67+
// "Debugger attached to an existing process.",
68+
// "A project launcher component has launched a new process in
69+
// a suspended state and then asked the debugger to attach."
70+
// ]
71+
// }
72+
// },
73+
// "required": [ "name" ]
74+
// }
75+
// },
76+
// "required": [ "event", "body" ]
77+
// }
78+
// ]
79+
// }
80+
void SendProcessEvent(DAP &dap, LaunchMethod launch_method) {
81+
lldb::SBFileSpec exe_fspec = dap.target.GetExecutable();
82+
char exe_path[PATH_MAX];
83+
exe_fspec.GetPath(exe_path, sizeof(exe_path));
84+
llvm::json::Object event(CreateEventObject("process"));
85+
llvm::json::Object body;
86+
EmplaceSafeString(body, "name", std::string(exe_path));
87+
const auto pid = dap.target.GetProcess().GetProcessID();
88+
body.try_emplace("systemProcessId", (int64_t)pid);
89+
body.try_emplace("isLocalProcess", true);
90+
const char *startMethod = nullptr;
91+
switch (launch_method) {
92+
case Launch:
93+
startMethod = "launch";
94+
break;
95+
case Attach:
96+
startMethod = "attach";
97+
break;
98+
case AttachForSuspendedLaunch:
99+
startMethod = "attachForSuspendedLaunch";
100+
break;
101+
}
102+
body.try_emplace("startMethod", startMethod);
103+
event.try_emplace("body", std::move(body));
104+
dap.SendJSON(llvm::json::Value(std::move(event)));
105+
}
106+
107+
// Send a thread stopped event for all threads as long as the process
108+
// is stopped.
109+
void SendThreadStoppedEvent(DAP &dap) {
110+
lldb::SBProcess process = dap.target.GetProcess();
111+
if (process.IsValid()) {
112+
auto state = process.GetState();
113+
if (state == lldb::eStateStopped) {
114+
llvm::DenseSet<lldb::tid_t> old_thread_ids;
115+
old_thread_ids.swap(dap.thread_ids);
116+
uint32_t stop_id = process.GetStopID();
117+
const uint32_t num_threads = process.GetNumThreads();
118+
119+
// First make a pass through the threads to see if the focused thread
120+
// has a stop reason. In case the focus thread doesn't have a stop
121+
// reason, remember the first thread that has a stop reason so we can
122+
// set it as the focus thread if below if needed.
123+
lldb::tid_t first_tid_with_reason = LLDB_INVALID_THREAD_ID;
124+
uint32_t num_threads_with_reason = 0;
125+
bool focus_thread_exists = false;
126+
for (uint32_t thread_idx = 0; thread_idx < num_threads; ++thread_idx) {
127+
lldb::SBThread thread = process.GetThreadAtIndex(thread_idx);
128+
const lldb::tid_t tid = thread.GetThreadID();
129+
const bool has_reason = ThreadHasStopReason(thread);
130+
// If the focus thread doesn't have a stop reason, clear the thread ID
131+
if (tid == dap.focus_tid) {
132+
focus_thread_exists = true;
133+
if (!has_reason)
134+
dap.focus_tid = LLDB_INVALID_THREAD_ID;
135+
}
136+
if (has_reason) {
137+
++num_threads_with_reason;
138+
if (first_tid_with_reason == LLDB_INVALID_THREAD_ID)
139+
first_tid_with_reason = tid;
140+
}
141+
}
142+
143+
// We will have cleared dap.focus_tid if the focus thread doesn't have
144+
// a stop reason, so if it was cleared, or wasn't set, or doesn't exist,
145+
// then set the focus thread to the first thread with a stop reason.
146+
if (!focus_thread_exists || dap.focus_tid == LLDB_INVALID_THREAD_ID)
147+
dap.focus_tid = first_tid_with_reason;
148+
149+
// If no threads stopped with a reason, then report the first one so
150+
// we at least let the UI know we stopped.
151+
if (num_threads_with_reason == 0) {
152+
lldb::SBThread thread = process.GetThreadAtIndex(0);
153+
dap.focus_tid = thread.GetThreadID();
154+
dap.SendJSON(CreateThreadStopped(dap, thread, stop_id));
155+
} else {
156+
for (uint32_t thread_idx = 0; thread_idx < num_threads; ++thread_idx) {
157+
lldb::SBThread thread = process.GetThreadAtIndex(thread_idx);
158+
dap.thread_ids.insert(thread.GetThreadID());
159+
if (ThreadHasStopReason(thread)) {
160+
dap.SendJSON(CreateThreadStopped(dap, thread, stop_id));
161+
}
162+
}
163+
}
164+
165+
for (auto tid : old_thread_ids) {
166+
auto end = dap.thread_ids.end();
167+
auto pos = dap.thread_ids.find(tid);
168+
if (pos == end)
169+
SendThreadExitedEvent(dap, tid);
170+
}
171+
} else {
172+
if (dap.log)
173+
*dap.log << "error: SendThreadStoppedEvent() when process"
174+
" isn't stopped ("
175+
<< lldb::SBDebugger::StateAsCString(state) << ')' << std::endl;
176+
}
177+
} else {
178+
if (dap.log)
179+
*dap.log << "error: SendThreadStoppedEvent() invalid process"
180+
<< std::endl;
181+
}
182+
dap.RunStopCommands();
183+
}
184+
185+
// Send a "terminated" event to indicate the process is done being
186+
// debugged.
187+
void SendTerminatedEvent(DAP &dap) {
188+
// Prevent races if the process exits while we're being asked to disconnect.
189+
llvm::call_once(dap.terminated_event_flag, [&] {
190+
dap.RunTerminateCommands();
191+
// Send a "terminated" event
192+
llvm::json::Object event(CreateTerminatedEventObject(dap.target));
193+
dap.SendJSON(llvm::json::Value(std::move(event)));
194+
});
195+
}
196+
197+
// Grab any STDOUT and STDERR from the process and send it up to VS Code
198+
// via an "output" event to the "stdout" and "stderr" categories.
199+
void SendStdOutStdErr(DAP &dap, lldb::SBProcess &process) {
200+
char buffer[OutputBufferSize];
201+
size_t count;
202+
while ((count = process.GetSTDOUT(buffer, sizeof(buffer))) > 0)
203+
dap.SendOutput(OutputType::Stdout, llvm::StringRef(buffer, count));
204+
while ((count = process.GetSTDERR(buffer, sizeof(buffer))) > 0)
205+
dap.SendOutput(OutputType::Stderr, llvm::StringRef(buffer, count));
206+
}
207+
208+
// Send a "continued" event to indicate the process is in the running state.
209+
void SendContinuedEvent(DAP &dap) {
210+
lldb::SBProcess process = dap.target.GetProcess();
211+
if (!process.IsValid()) {
212+
return;
213+
}
214+
215+
// If the focus thread is not set then we haven't reported any thread status
216+
// to the client, so nothing to report.
217+
if (!dap.configuration_done_sent || dap.focus_tid == LLDB_INVALID_THREAD_ID) {
218+
return;
219+
}
220+
221+
llvm::json::Object event(CreateEventObject("continued"));
222+
llvm::json::Object body;
223+
body.try_emplace("threadId", (int64_t)dap.focus_tid);
224+
body.try_emplace("allThreadsContinued", true);
225+
event.try_emplace("body", std::move(body));
226+
dap.SendJSON(llvm::json::Value(std::move(event)));
227+
}
228+
229+
// Send a "exited" event to indicate the process has exited.
230+
void SendProcessExitedEvent(DAP &dap, lldb::SBProcess &process) {
231+
llvm::json::Object event(CreateEventObject("exited"));
232+
llvm::json::Object body;
233+
body.try_emplace("exitCode", (int64_t)process.GetExitStatus());
234+
event.try_emplace("body", std::move(body));
235+
dap.SendJSON(llvm::json::Value(std::move(event)));
236+
}
237+
238+
} // namespace lldb_dap

lldb/tools/lldb-dap/EventHelper.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//===-- EventHelper.h -----------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLDB_TOOLS_LLDB_DAP_EVENTHELPER_H
10+
#define LLDB_TOOLS_LLDB_DAP_EVENTHELPER_H
11+
12+
#include "DAPForward.h"
13+
14+
namespace lldb_dap {
15+
struct DAP;
16+
17+
enum LaunchMethod { Launch, Attach, AttachForSuspendedLaunch };
18+
19+
void SendProcessEvent(DAP &dap, LaunchMethod launch_method);
20+
21+
void SendThreadStoppedEvent(DAP &dap);
22+
23+
void SendTerminatedEvent(DAP &dap);
24+
25+
void SendStdOutStdErr(DAP &dap, lldb::SBProcess &process);
26+
27+
void SendContinuedEvent(DAP &dap);
28+
29+
void SendProcessExitedEvent(DAP &dap, lldb::SBProcess &process);
30+
31+
} // namespace lldb_dap
32+
33+
#endif

0 commit comments

Comments
 (0)