Skip to content

Conversation

@ashgti
Copy link
Contributor

@ashgti ashgti commented Feb 22, 2025

This is a refactor of lldb-dap focused on supporting cancel requests and well defined Debug Adapter Protocol (DAP) structures.

Existing request handlers today tend to use unstructured llvm::json::Object's to represent requests and replies. The lack of structure makes it hard for us to apply some unfirom handling around requests to better support cancellation. To address this, this change includes a new Protocol.h file with a set of well defined POD structures to represent the types in the DAP and includes toJSON/fromJSON serialization support.

Building on these types, this includes a new approach to registering request handlers with these new well defined types.

template <typename Args, typename Body>
void DAP::RegisterRequest(llvm::StringLiteral command,
                          RequestHandler<Args, Body> handler);
...
llvm::Expected<SourceResponseBody> request_source(DAP &dap, const SourceArguments &args) {
  SourceResponseBody body;
  ...
  if (/*some error*/)
    return llvm::make_error<DAPError>("msg");
  return std::move(body);
}
...
  dap.RegisterRequest("source", request_source);
...

The main features of this refactor are:

  • Created a queue of pending protocol messages to allow for preemptive cancellations and active cancellations.
  • A cleaner separation between the Debug Adapter Protocol types and lldb types that include serialization support toJSON/fromJSON.
  • Improved error handling using toJSON/fromJSON.
  • Unified handling of responses and errors.
  • Unified handling of cancel requests.

This change include minimal support to implement the Debug Adapter Protocol 'source' request, 'evaluate' request and the 'exited' event.

@ashgti ashgti force-pushed the lldb-dap-async-refactor branch 2 times, most recently from 0e4cc27 to 3e8d70c Compare February 22, 2025 04:30
This is a refactor of lldb-dap focused on supporting cancel requests and well defined Debug Adapter Protocol (DAP) structures.

Existing request handlers today tend to use unstructured `llvm::json::Object`'s to represent requests and replies. The lack of structure makes it hard for us to apply some unfirom handling around requests to better support cancellation. To address this, this change includes a new `Protocol.h` file with a set of well defined POD structures to represent the types in the DAP and includes `toJSON`/`fromJSON` serialization support.

Building on these types, this includes a new approach to registering request handlers with these new well defined types.

```
template <typename Args, typename Body>
void DAP::RegisterRequest(llvm::StringLiteral command,
                          RequestHandler<Args, Body> handler);
...
llvm::Expected<SourceResponseBody> request_source(DAP &dap, const SourceArguments &args) {
  SourceResponseBody body;
  ...
  if (/*some error*/)
    return llvm::make_error<DAPError>("msg");
  return std::move(body);
}
...
  dap.RegisterRequest("source", request_source);
...
```

The main features of this refactor are:

* Created a queue of pending protocol messages to allow for preemptive cancellations and active cancellations.
* A cleaner separation between the Debug Adapter Protocol types and lldb types that include serialization support toJSON/fromJSON.
* Improved error handling using toJSON/fromJSON.
* Unified handling of responses and errors.
* Unified handling of cancel requests.

This change include minimal support to implement the Debug Adapter Protocol 'source' request, 'evaluate' request and the 'exited' event.
@ashgti ashgti force-pushed the lldb-dap-async-refactor branch from 3e8d70c to 481369f Compare February 22, 2025 21:15
@github-actions
Copy link

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:
git-clang-format --diff 998b28f19672ba49925e25f1a78c1252f017c247 481369f8107a43fd101e161c9bd9bb374e38f2af --extensions c,h,cpp -- lldb/test/API/tools/lldb-dap/cancel/main.c lldb/tools/lldb-dap/Protocol.cpp lldb/tools/lldb-dap/Protocol.h lldb/tools/lldb-dap/DAP.cpp lldb/tools/lldb-dap/DAP.h lldb/tools/lldb-dap/IOStream.cpp lldb/tools/lldb-dap/IOStream.h lldb/tools/lldb-dap/JSONUtils.cpp lldb/tools/lldb-dap/JSONUtils.h lldb/tools/lldb-dap/lldb-dap.cpp
View the diff from clang-format here.
diff --git a/lldb/tools/lldb-dap/DAP.cpp b/lldb/tools/lldb-dap/DAP.cpp
index 1bfed616bd..4ddb3491bd 100644
--- a/lldb/tools/lldb-dap/DAP.cpp
+++ b/lldb/tools/lldb-dap/DAP.cpp
@@ -930,7 +930,7 @@ llvm::Error DAP::Run() {
 
       if (protocol::Request *req = std::get_if<protocol::Request>(&next)) {
         active_request_seq = req->seq;
-        
+
         // If we have a request check if we should preemptively cancel it.
         bool cancelled = false;
         for (const auto &message : messages) {
diff --git a/lldb/tools/lldb-dap/DAP.h b/lldb/tools/lldb-dap/DAP.h
index 1bb58bff0d..2f4ab4abe5 100644
--- a/lldb/tools/lldb-dap/DAP.h
+++ b/lldb/tools/lldb-dap/DAP.h
@@ -162,21 +162,21 @@ struct Variables {
 
 struct StartDebuggingRequestHandler : public lldb::SBCommandPluginInterface {
   DAP &dap;
-  explicit StartDebuggingRequestHandler(DAP &d) : dap(d){};
+  explicit StartDebuggingRequestHandler(DAP &d) : dap(d) {};
   bool DoExecute(lldb::SBDebugger debugger, char **command,
                  lldb::SBCommandReturnObject &result) override;
 };
 
 struct ReplModeRequestHandler : public lldb::SBCommandPluginInterface {
   DAP &dap;
-  explicit ReplModeRequestHandler(DAP &d) : dap(d){};
+  explicit ReplModeRequestHandler(DAP &d) : dap(d) {};
   bool DoExecute(lldb::SBDebugger debugger, char **command,
                  lldb::SBCommandReturnObject &result) override;
 };
 
 struct SendEventRequestHandler : public lldb::SBCommandPluginInterface {
   DAP &dap;
-  explicit SendEventRequestHandler(DAP &d) : dap(d){};
+  explicit SendEventRequestHandler(DAP &d) : dap(d) {};
   bool DoExecute(lldb::SBDebugger debugger, char **command,
                  lldb::SBCommandReturnObject &result) override;
 };
diff --git a/lldb/tools/lldb-dap/Protocol.h b/lldb/tools/lldb-dap/Protocol.h
index b1c39a9a34..098180eab2 100644
--- a/lldb/tools/lldb-dap/Protocol.h
+++ b/lldb/tools/lldb-dap/Protocol.h
@@ -23,11 +23,11 @@
 #include "llvm/Support/JSON.h"
 #include <cstddef>
 #include <cstdint>
+#include <map>
 #include <optional>
 #include <string>
 #include <variant>
 #include <vector>
-#include <map>
 
 namespace lldb_dap {
 namespace protocol {

@ashgti ashgti closed this Feb 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant