https://github.com/llvmbot created https://github.com/llvm/llvm-project/pull/181380
Backport b447f5d9763010f8c6806c578533291aef2bd484 Requested by: @da-viper >From d9aba14e1cf434642b40448d7b14d35755257536 Mon Sep 17 00:00:00 2001 From: Sergei Druzhkov <[email protected]> Date: Fri, 13 Feb 2026 16:00:57 +0300 Subject: [PATCH] [lldb-dap] Add unknown request handler (#181109) Added unknown request handler to avoid crash. Returning error in this case looks better than stopping entire debug session. (cherry picked from commit b447f5d9763010f8c6806c578533291aef2bd484) --- .../test/tools/lldb-dap/dap_server.py | 8 +++++ lldb/test/API/tools/lldb-dap/unknown/Makefile | 3 ++ .../unknown/TestDAP_unknownRequest.py | 35 +++++++++++++++++++ lldb/test/API/tools/lldb-dap/unknown/main.c | 6 ++++ lldb/tools/lldb-dap/CMakeLists.txt | 1 + lldb/tools/lldb-dap/DAP.cpp | 10 +++--- lldb/tools/lldb-dap/Handler/RequestHandler.h | 9 +++++ .../Handler/UnknownRequestHandler.cpp | 19 ++++++++++ .../lldb-dap/Protocol/ProtocolRequests.h | 5 +++ .../gn/secondary/lldb/tools/lldb-dap/BUILD.gn | 1 + 10 files changed, 91 insertions(+), 6 deletions(-) create mode 100644 lldb/test/API/tools/lldb-dap/unknown/Makefile create mode 100644 lldb/test/API/tools/lldb-dap/unknown/TestDAP_unknownRequest.py create mode 100644 lldb/test/API/tools/lldb-dap/unknown/main.c create mode 100644 lldb/tools/lldb-dap/Handler/UnknownRequestHandler.cpp diff --git a/lldb/packages/Python/lldbsuite/test/tools/lldb-dap/dap_server.py b/lldb/packages/Python/lldbsuite/test/tools/lldb-dap/dap_server.py index a79d766118b9d..919ab6566dea9 100644 --- a/lldb/packages/Python/lldbsuite/test/tools/lldb-dap/dap_server.py +++ b/lldb/packages/Python/lldbsuite/test/tools/lldb-dap/dap_server.py @@ -1586,6 +1586,14 @@ def request_testGetTargetBreakpoints(self): } return self._send_recv(command_dict) + def request_custom(self, command: str, arguments: Optional[dict[str, Any]] = None): + command_dict = { + "command": command, + "type": "request", + "arguments": {} if arguments is None else arguments, + } + return self._send_recv(command_dict) + def terminate(self): self.send.close() if self._recv_thread.is_alive(): diff --git a/lldb/test/API/tools/lldb-dap/unknown/Makefile b/lldb/test/API/tools/lldb-dap/unknown/Makefile new file mode 100644 index 0000000000000..10495940055b6 --- /dev/null +++ b/lldb/test/API/tools/lldb-dap/unknown/Makefile @@ -0,0 +1,3 @@ +C_SOURCES := main.c + +include Makefile.rules diff --git a/lldb/test/API/tools/lldb-dap/unknown/TestDAP_unknownRequest.py b/lldb/test/API/tools/lldb-dap/unknown/TestDAP_unknownRequest.py new file mode 100644 index 0000000000000..8f7b518bb0286 --- /dev/null +++ b/lldb/test/API/tools/lldb-dap/unknown/TestDAP_unknownRequest.py @@ -0,0 +1,35 @@ +""" +Test lldb-dap unknown request. +""" + +import lldbdap_testcase + + +class TestDAP_unknown_request(lldbdap_testcase.DAPTestCaseBase): + """ + Tests handling of unknown request. + """ + + def test_no_arguments(self): + program = self.getBuildArtifact("a.out") + self.build_and_launch(program, stopOnEntry=True) + self.dap_server.request_configurationDone() + self.dap_server.wait_for_stopped() + + response = self.dap_server.request_custom("unknown") + self.assertFalse(response["success"]) + self.assertEqual(response["body"]["error"]["format"], "unknown request") + + self.continue_to_exit() + + def test_with_arguments(self): + program = self.getBuildArtifact("a.out") + self.build_and_launch(program, stopOnEntry=True) + self.dap_server.request_configurationDone() + self.dap_server.wait_for_stopped() + + response = self.dap_server.request_custom("unknown", {"foo": "bar", "id": 42}) + self.assertFalse(response["success"]) + self.assertEqual(response["body"]["error"]["format"], "unknown request") + + self.continue_to_exit() diff --git a/lldb/test/API/tools/lldb-dap/unknown/main.c b/lldb/test/API/tools/lldb-dap/unknown/main.c new file mode 100644 index 0000000000000..092333773d0b6 --- /dev/null +++ b/lldb/test/API/tools/lldb-dap/unknown/main.c @@ -0,0 +1,6 @@ +#include <stdio.h> + +int main() { + printf("Hello, World!\n"); + return 0; +} diff --git a/lldb/tools/lldb-dap/CMakeLists.txt b/lldb/tools/lldb-dap/CMakeLists.txt index 237c3043dbbc7..1694af1cbddd5 100644 --- a/lldb/tools/lldb-dap/CMakeLists.txt +++ b/lldb/tools/lldb-dap/CMakeLists.txt @@ -64,6 +64,7 @@ add_lldb_library(lldbDAP Handler/StepOutRequestHandler.cpp Handler/TestGetTargetBreakpointsRequestHandler.cpp Handler/ThreadsRequestHandler.cpp + Handler/UnknownRequestHandler.cpp Handler/VariablesRequestHandler.cpp Handler/WriteMemoryRequestHandler.cpp diff --git a/lldb/tools/lldb-dap/DAP.cpp b/lldb/tools/lldb-dap/DAP.cpp index 8bc4727378df6..03caeb506d2d4 100644 --- a/lldb/tools/lldb-dap/DAP.cpp +++ b/lldb/tools/lldb-dap/DAP.cpp @@ -841,13 +841,11 @@ bool DAP::HandleObject(const Message &M) { llvm::Twine("request_command:", req->command).str()); if (handler_pos != request_handlers.end()) { handler_pos->second->Run(*req); - return true; // Success + } else { + UnknownRequestHandler handler(*this); + handler.BaseRequestHandler::Run(*req); } - - dispatcher.Set("error", - llvm::Twine("unhandled-command:" + req->command).str()); - DAP_LOG(log, "error: unhandled command '{0}'", req->command); - return false; // Fail + return true; // Success } if (const auto *resp = std::get_if<Response>(&M)) { diff --git a/lldb/tools/lldb-dap/Handler/RequestHandler.h b/lldb/tools/lldb-dap/Handler/RequestHandler.h index f435257d4dcce..8098dc5c72ff0 100644 --- a/lldb/tools/lldb-dap/Handler/RequestHandler.h +++ b/lldb/tools/lldb-dap/Handler/RequestHandler.h @@ -658,6 +658,15 @@ class WriteMemoryRequestHandler final Run(const protocol::WriteMemoryArguments &args) const override; }; +class UnknownRequestHandler final + : public RequestHandler<protocol::UnknownArguments, + protocol::UnknownResponseBody> { +public: + using RequestHandler::RequestHandler; + static llvm::StringLiteral GetCommand() { return "unknown"; } + llvm::Error Run(const protocol::UnknownArguments &args) const override; +}; + } // namespace lldb_dap #endif diff --git a/lldb/tools/lldb-dap/Handler/UnknownRequestHandler.cpp b/lldb/tools/lldb-dap/Handler/UnknownRequestHandler.cpp new file mode 100644 index 0000000000000..0acb7c3dce5bd --- /dev/null +++ b/lldb/tools/lldb-dap/Handler/UnknownRequestHandler.cpp @@ -0,0 +1,19 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "DAPError.h" +#include "Protocol/ProtocolRequests.h" +#include "RequestHandler.h" +#include "llvm/Support/Error.h" + +using namespace lldb_dap; +using namespace lldb_dap::protocol; + +llvm::Error UnknownRequestHandler::Run(const UnknownArguments &args) const { + return llvm::make_error<DAPError>("unknown request"); +} diff --git a/lldb/tools/lldb-dap/Protocol/ProtocolRequests.h b/lldb/tools/lldb-dap/Protocol/ProtocolRequests.h index f7b7bf2e4dda4..0a8425c1ae1b3 100644 --- a/lldb/tools/lldb-dap/Protocol/ProtocolRequests.h +++ b/lldb/tools/lldb-dap/Protocol/ProtocolRequests.h @@ -1312,6 +1312,11 @@ struct StackTraceResponseBody { }; llvm::json::Value toJSON(const StackTraceResponseBody &); +/// Arguments for unknown request. +using UnknownArguments = EmptyArguments; +/// Response to unknowns request. +using UnknownResponseBody = VoidResponse; + } // namespace lldb_dap::protocol #endif diff --git a/llvm/utils/gn/secondary/lldb/tools/lldb-dap/BUILD.gn b/llvm/utils/gn/secondary/lldb/tools/lldb-dap/BUILD.gn index 9c1aa88af7252..3a317c35c9b95 100644 --- a/llvm/utils/gn/secondary/lldb/tools/lldb-dap/BUILD.gn +++ b/llvm/utils/gn/secondary/lldb/tools/lldb-dap/BUILD.gn @@ -68,6 +68,7 @@ static_library("lib") { "Handler/StepOutRequestHandler.cpp", "Handler/TestGetTargetBreakpointsRequestHandler.cpp", "Handler/ThreadsRequestHandler.cpp", + "Handler/UnknownRequestHandler.cpp", "Handler/VariablesRequestHandler.cpp", "Handler/WriteMemoryRequestHandler.cpp", "InstructionBreakpoint.cpp", _______________________________________________ llvm-branch-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
