Skip to content

Commit 8141476

Browse files
committed
Using a few helpers to simplify the cancel responses.
1 parent 0cdd970 commit 8141476

File tree

2 files changed

+43
-41
lines changed

2 files changed

+43
-41
lines changed

lldb/test/API/tools/lldb-dap/cancel/TestDAP_cancel.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def test_pending_request(self):
5252
blocking_seq = self.async_blocking_request(duration=1.0)
5353
# Use a longer timeout to ensure we catch if the request was interrupted
5454
# properly.
55-
pending_seq = self.async_blocking_request(duration=self.timeoutval)
55+
pending_seq = self.async_blocking_request(duration=self.timeoutval / 2)
5656
cancel_seq = self.async_cancel(requestId=pending_seq)
5757

5858
blocking_resp = self.dap_server.recv_packet(filter_type=["response"])

lldb/tools/lldb-dap/DAP.cpp

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
#endif
6161

6262
using namespace lldb_dap;
63+
using namespace lldb_dap::protocol;
6364

6465
namespace {
6566
#ifdef _WIN32
@@ -69,6 +70,15 @@ const char DEV_NULL[] = "/dev/null";
6970
#endif
7071
} // namespace
7172

73+
static Response CancelledResponse(int64_t seq, std::string command) {
74+
Response response;
75+
response.request_seq = seq;
76+
response.command = command;
77+
response.success = false;
78+
response.message = Response::Message::cancelled;
79+
return response;
80+
}
81+
7282
namespace lldb_dap {
7383

7484
DAP::DAP(llvm::StringRef path, Log *log, const ReplMode default_repl_mode,
@@ -232,7 +242,7 @@ void DAP::StopEventHandlers() {
232242
void DAP::SendJSON(const llvm::json::Value &json) {
233243
// FIXME: Instead of parsing the output message from JSON, pass the `Message`
234244
// as parameter to `SendJSON`.
235-
protocol::Message message;
245+
Message message;
236246
llvm::json::Path::Root root;
237247
if (!fromJSON(json, message, root)) {
238248
DAP_LOG_ERROR(log, root.getError(), "({1}) encoding failed: {0}",
@@ -242,16 +252,12 @@ void DAP::SendJSON(const llvm::json::Value &json) {
242252
Send(message);
243253
}
244254

245-
void DAP::Send(const protocol::Message &message) {
246-
if (auto *resp = std::get_if<protocol::Response>(&message);
255+
void DAP::Send(const Message &message) {
256+
if (auto *resp = std::get_if<Response>(&message);
247257
resp && debugger.InterruptRequested()) {
248258
// If the debugger was interrupted, convert this response into a 'cancelled'
249259
// response.
250-
protocol::Response cancelled;
251-
cancelled.command = resp->command;
252-
cancelled.request_seq = resp->request_seq;
253-
cancelled.success = false;
254-
cancelled.message = protocol::Response::Message::cancelled;
260+
Response cancelled = CancelledResponse(resp->request_seq, resp->command);
255261
if (llvm::Error err = transport.Write(cancelled))
256262
DAP_LOG_ERROR(log, std::move(err), "({1}) write failed: {0}",
257263
transport.GetClientName());
@@ -689,8 +695,8 @@ void DAP::SetTarget(const lldb::SBTarget target) {
689695
}
690696
}
691697

692-
bool DAP::HandleObject(const protocol::Message &M) {
693-
if (const auto *req = std::get_if<protocol::Request>(&M)) {
698+
bool DAP::HandleObject(const Message &M) {
699+
if (const auto *req = std::get_if<Request>(&M)) {
694700
{
695701
std::lock_guard<std::mutex> lock(m_active_request_mutex);
696702
m_active_request = req;
@@ -702,14 +708,12 @@ bool DAP::HandleObject(const protocol::Message &M) {
702708
});
703709

704710
{
711+
// If there is a pending cancelled request, preempt the request and mark
712+
// it cancelled.
705713
std::lock_guard<std::mutex> lock(m_cancelled_requests_mutex);
706714
if (m_cancelled_requests.find(req->seq) != m_cancelled_requests.end()) {
707-
protocol::Response response;
708-
response.request_seq = req->seq;
709-
response.command = req->command;
710-
response.success = false;
711-
response.message = protocol::Response::Message::cancelled;
712-
Send(response);
715+
Response cancelled = CancelledResponse(req->seq, req->command);
716+
Send(cancelled);
713717
return true;
714718
}
715719
}
@@ -729,7 +733,7 @@ bool DAP::HandleObject(const protocol::Message &M) {
729733
return false; // Fail
730734
}
731735

732-
if (const auto *resp = std::get_if<protocol::Response>(&M)) {
736+
if (const auto *resp = std::get_if<Response>(&M)) {
733737
std::unique_ptr<ResponseHandler> response_handler;
734738
{
735739
std::lock_guard<std::mutex> locker(call_mutex);
@@ -750,21 +754,20 @@ bool DAP::HandleObject(const protocol::Message &M) {
750754
} else {
751755
llvm::StringRef message = "Unknown error, response failed";
752756
if (resp->message) {
753-
message =
754-
std::visit(llvm::makeVisitor(
755-
[](const std::string &message) -> llvm::StringRef {
756-
return message;
757-
},
758-
[](const protocol::Response::Message &message)
759-
-> llvm::StringRef {
760-
switch (message) {
761-
case protocol::Response::Message::cancelled:
762-
return "cancelled";
763-
case protocol::Response::Message::notStopped:
764-
return "notStopped";
765-
}
766-
}),
767-
*resp->message);
757+
message = std::visit(
758+
llvm::makeVisitor(
759+
[](const std::string &message) -> llvm::StringRef {
760+
return message;
761+
},
762+
[](const Response::Message &message) -> llvm::StringRef {
763+
switch (message) {
764+
case Response::Message::cancelled:
765+
return "cancelled";
766+
case Response::Message::notStopped:
767+
return "notStopped";
768+
}
769+
}),
770+
*resp->message);
768771
}
769772

770773
(*response_handler)(llvm::createStringError(
@@ -822,17 +825,17 @@ llvm::Error DAP::Disconnect(bool terminateDebuggee) {
822825
return ToError(error);
823826
}
824827

825-
void DAP::ClearCancelRequest(const protocol::CancelArguments &args) {
828+
void DAP::ClearCancelRequest(const CancelArguments &args) {
826829
std::lock_guard<std::mutex> cancalled_requests_lock(
827830
m_cancelled_requests_mutex);
828831
if (args.requestId)
829832
m_cancelled_requests.erase(*args.requestId);
830833
}
831834

832835
template <typename T>
833-
static std::optional<T> getArgumentsIfRequest(const protocol::Message &pm,
836+
static std::optional<T> getArgumentsIfRequest(const Message &pm,
834837
llvm::StringLiteral command) {
835-
auto *const req = std::get_if<protocol::Request>(&pm);
838+
auto *const req = std::get_if<Request>(&pm);
836839
if (!req || req->command != command)
837840
return std::nullopt;
838841

@@ -855,8 +858,7 @@ llvm::Error DAP::Loop() {
855858
});
856859

857860
while (!disconnecting) {
858-
llvm::Expected<protocol::Message> next =
859-
transport.Read(std::chrono::seconds(1));
861+
llvm::Expected<Message> next = transport.Read(std::chrono::seconds(1));
860862
bool timeout = false;
861863
bool eof = false;
862864
if (llvm::Error Err = llvm::handleErrors(
@@ -878,8 +880,8 @@ llvm::Error DAP::Loop() {
878880
if (timeout)
879881
continue;
880882

881-
const std::optional<protocol::CancelArguments> cancel_args =
882-
getArgumentsIfRequest<protocol::CancelArguments>(*next, "cancel");
883+
const std::optional<CancelArguments> cancel_args =
884+
getArgumentsIfRequest<CancelArguments>(*next, "cancel");
883885
if (cancel_args) {
884886
{
885887
std::lock_guard<std::mutex> cancalled_requests_lock(
@@ -924,7 +926,7 @@ llvm::Error DAP::Loop() {
924926
if (m_queue.empty())
925927
break;
926928

927-
protocol::Message next = m_queue.front();
929+
Message next = m_queue.front();
928930
m_queue.pop_front();
929931

930932
if (!HandleObject(next))

0 commit comments

Comments
 (0)