Author: Sergei Druzhkov
Date: 2026-02-13T16:00:57+03:00
New Revision: b447f5d9763010f8c6806c578533291aef2bd484

URL: 
https://github.com/llvm/llvm-project/commit/b447f5d9763010f8c6806c578533291aef2bd484
DIFF: 
https://github.com/llvm/llvm-project/commit/b447f5d9763010f8c6806c578533291aef2bd484.diff

LOG: [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.

Added: 
    lldb/test/API/tools/lldb-dap/unknown/Makefile
    lldb/test/API/tools/lldb-dap/unknown/TestDAP_unknownRequest.py
    lldb/test/API/tools/lldb-dap/unknown/main.c
    lldb/tools/lldb-dap/Handler/UnknownRequestHandler.cpp

Modified: 
    lldb/packages/Python/lldbsuite/test/tools/lldb-dap/dap_server.py
    lldb/tools/lldb-dap/CMakeLists.txt
    lldb/tools/lldb-dap/DAP.cpp
    lldb/tools/lldb-dap/Handler/RequestHandler.h
    lldb/tools/lldb-dap/Protocol/ProtocolRequests.h
    llvm/utils/gn/secondary/lldb/tools/lldb-dap/BUILD.gn

Removed: 
    


################################################################################
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 fdccc9eae9fe4..751832668c5d1 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
@@ -1677,6 +1677,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 8566c663fcc06..1bb7a2e498b9b 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 b76b05c5d1459..4110432c4fd00 100644
--- a/lldb/tools/lldb-dap/DAP.cpp
+++ b/lldb/tools/lldb-dap/DAP.cpp
@@ -851,13 +851,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 9feb636fd5c28..4b4f1435e7acb 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 28c9f48200e0c..91fc2a4db6f57 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 896da7df95bb1..edcbc5e31f4f1 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",


        
_______________________________________________
lldb-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to