Skip to content

Commit 1796613

Browse files
committed
[lldb][lldb-dap] Basic implementation of a deferred request.
almost one to one except not deferring breakpoints.
1 parent 9f77c26 commit 1796613

File tree

6 files changed

+30
-15
lines changed

6 files changed

+30
-15
lines changed

lldb/tools/lldb-dap/DAP.cpp

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,7 @@ void DAP::SetTarget(const lldb::SBTarget target) {
726726
}
727727
}
728728

729-
bool DAP::HandleObject(const Message &M) {
729+
bool DAP::HandleObject(const Message &M, bool &defer) {
730730
TelemetryDispatcher dispatcher(&debugger);
731731
dispatcher.Set("client_name", transport.GetClientName().str());
732732
if (const auto *req = std::get_if<Request>(&M)) {
@@ -748,7 +748,7 @@ bool DAP::HandleObject(const Message &M) {
748748
dispatcher.Set("client_data",
749749
llvm::Twine("request_command:", req->command).str());
750750
if (handler_pos != request_handlers.end()) {
751-
handler_pos->second->Run(*req);
751+
defer = handler_pos->second->Run(*req);
752752
return true; // Success
753753
}
754754

@@ -918,17 +918,11 @@ llvm::Error DAP::Loop() {
918918

919919
// The launch sequence is special and we need to carefully handle
920920
// packets in the right order. Until we've handled configurationDone,
921-
bool add_to_pending_queue = false;
922-
923921
if (const protocol::Request *req =
924922
std::get_if<protocol::Request>(&*next)) {
925923
llvm::StringRef command = req->command;
926924
if (command == "disconnect")
927925
disconnecting = true;
928-
if (!configuration_done)
929-
add_to_pending_queue =
930-
command != "initialize" && command != "configurationDone" &&
931-
command != "disconnect" && !command.ends_with("Breakpoints");
932926
}
933927

934928
const std::optional<CancelArguments> cancel_args =
@@ -956,8 +950,7 @@ llvm::Error DAP::Loop() {
956950

957951
{
958952
std::lock_guard<std::mutex> guard(m_queue_mutex);
959-
auto &queue = add_to_pending_queue ? m_pending_queue : m_queue;
960-
queue.push_back(std::move(*next));
953+
m_queue.push_back(std::move(*next));
961954
}
962955
m_queue_cv.notify_one();
963956
}
@@ -984,9 +977,13 @@ llvm::Error DAP::Loop() {
984977
// Unlock while we're processing the event.
985978
lock.unlock();
986979

987-
if (!HandleObject(next))
980+
bool defer_message = false;
981+
if (!HandleObject(next, defer_message))
988982
return llvm::createStringError(llvm::inconvertibleErrorCode(),
989983
"unhandled packet");
984+
if (defer_message) {
985+
m_pending_queue.push_back(next);
986+
}
990987
}
991988

992989
return ToError(queue_reader.get());

lldb/tools/lldb-dap/DAP.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ struct DAP {
339339
/// listeing for its breakpoint events.
340340
void SetTarget(const lldb::SBTarget target);
341341

342-
bool HandleObject(const protocol::Message &M);
342+
bool HandleObject(const protocol::Message &M, bool &defer);
343343

344344
/// Disconnect the DAP session.
345345
llvm::Error Disconnect();

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,4 +160,8 @@ void AttachRequestHandler::PostRun() const {
160160
dap.target.GetProcess().Continue();
161161
}
162162

163+
bool AttachRequestHandler::DeferRequest() const {
164+
return !dap.configuration_done;
165+
}
166+
163167
} // namespace lldb_dap

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,8 @@ void LaunchRequestHandler::PostRun() const {
8888
dap.target.GetProcess().Continue();
8989
}
9090

91+
bool LaunchRequestHandler::DeferRequest() const {
92+
return !dap.configuration_done;
93+
}
94+
9195
} // namespace lldb_dap

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ RunInTerminal(DAP &dap, const protocol::LaunchRequestArguments &arguments) {
126126
error.GetCString());
127127
}
128128

129-
void BaseRequestHandler::Run(const Request &request) {
129+
bool BaseRequestHandler::Run(const Request &request) {
130130
// If this request was cancelled, send a cancelled response.
131131
if (dap.IsCancelled(request)) {
132132
Response cancelled{/*request_seq=*/request.seq,
@@ -135,12 +135,15 @@ void BaseRequestHandler::Run(const Request &request) {
135135
/*message=*/eResponseMessageCancelled,
136136
/*body=*/std::nullopt};
137137
dap.Send(cancelled);
138-
return;
138+
return false;
139139
}
140140

141141
lldb::SBMutex lock = dap.GetAPIMutex();
142142
std::lock_guard<lldb::SBMutex> guard(lock);
143143

144+
if (DeferRequest()) {
145+
return true;
146+
}
144147
// FIXME: After all the requests have migrated from LegacyRequestHandler >
145148
// RequestHandler<> we should be able to move this into
146149
// RequestHandler<>::operator().
@@ -149,6 +152,7 @@ void BaseRequestHandler::Run(const Request &request) {
149152
// FIXME: After all the requests have migrated from LegacyRequestHandler >
150153
// RequestHandler<> we should be able to check `debugger.InterruptRequest` and
151154
// mark the response as cancelled.
155+
return false;
152156
}
153157

154158
llvm::Error BaseRequestHandler::LaunchProcess(

lldb/tools/lldb-dap/Handler/RequestHandler.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ class BaseRequestHandler {
4646

4747
virtual ~BaseRequestHandler() = default;
4848

49-
void Run(const protocol::Request &);
49+
/// Return `true` if the request should be deferred.
50+
[[nodiscard]]
51+
bool Run(const protocol::Request &);
52+
53+
virtual bool DeferRequest() const { return false; };
5054

5155
virtual void operator()(const protocol::Request &request) const = 0;
5256

@@ -203,6 +207,7 @@ class AttachRequestHandler
203207
static llvm::StringLiteral GetCommand() { return "attach"; }
204208
llvm::Error Run(const protocol::AttachRequestArguments &args) const override;
205209
void PostRun() const override;
210+
bool DeferRequest() const override;
206211
};
207212

208213
class BreakpointLocationsRequestHandler
@@ -302,6 +307,7 @@ class LaunchRequestHandler
302307
llvm::Error
303308
Run(const protocol::LaunchRequestArguments &arguments) const override;
304309
void PostRun() const override;
310+
bool DeferRequest() const override;
305311
};
306312

307313
class RestartRequestHandler : public LegacyRequestHandler {

0 commit comments

Comments
 (0)