Skip to content

[lldb-dap] Refactor request handlers (NFC) #128262

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Feb 24, 2025
Merged

Conversation

JDevlieghere
Copy link
Member

@JDevlieghere JDevlieghere commented Feb 22, 2025

Currently, all request handlers are implemented as free functions in lldb-dap.cpp. That file has grown to over 5000 lines and is starting to become hard to maintain. This PR moves the request handlers into their own class (and file), together with their documentation.

@JDevlieghere JDevlieghere requested a review from ashgti February 22, 2025 01:20
Copy link
Contributor

@ashgti ashgti left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a good idea to help simplify the lldb-dap.cpp file and improve code organization

void SetSourceMapFromArguments(const llvm::json::Object &arguments);

protected:
DAP &dap;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've been thinking about the naming convention of the DAP object and I wonder if we should refer to this DAP as a session?

e.g. DAP &session;

That kind of represents what this is actually managing in the context of lldb-dap. The DAP object is the debug session and is coordinating the dap.debugger / dap.target with the various requests from the client.

I'm open to other ideas, but its something I've noticed while working on lldb-dap code.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Session sounds good. Context would probably be another good option?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Context would also work, either it helps with clarity since dap is a bit vague, but this could also be done in a follow up.

Comment on lines 27 to 30
enum LaunchMethod { Launch, Attach, AttachForSuspendedLaunch };

void SendProcessEvent(LaunchMethod launch_method);
void SetSourceMapFromArguments(const llvm::json::Object &arguments);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whats the reason to have these here vs in a specific Request handler below?

Are these shared helpers between different requests?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are indeed shared, but not by many request handlers. I don't think this is a good place for them, but I'm trtying to minimize the changes.

Comment on lines 27 to 30
enum LaunchMethod { Launch, Attach, AttachForSuspendedLaunch };

void SendProcessEvent(LaunchMethod launch_method);
void SetSourceMapFromArguments(const llvm::json::Object &arguments);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this isn't part of your change specifically, but I do think it would be good for us to create richer types to represent some arguments like this arguments parameter.

That was the motivation for the creating the POD types in Protocol.h to have a clear boundary between protocol types, lldb debugger types (generally anything like lldb::SB*) and the helpers that are part of lldb-dap used for coordinating between the two.

public:
using Request::Request;
static llvm::StringLiteral getName() { return "attach"; }
void operator()(const llvm::json::Object &request) override;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is showing my lack of c++ knowledge, but if we did create specific types for these handles, like a struct AttachArguments and struct AttachResponseBody can we override those here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not directly, no, but you can do exactly what you did in your PR. Have this function take a const protocol::Request &req and then implement it int terms of types unique to the class. They become an implementation detail of the RequestHandler that nobody else needs to know about.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thats a good point, maybe with this structure my other PR will be easier for us to add.

I wonder, can the subclass do something like:

class AttachHandler: RequestHandler<AttachArguments, AttachBodyResponse> {
...

Then we could use the protocol types for serialization. I might look into that as a follow up.

Comment on lines 43 to 45
// Both attach and launch take a either a sourcePath or sourceMap
// argument (or neither), from which we need to set the target.source-map.
void Request::SetSourceMapFromArguments(const llvm::json::Object &arguments) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the fact this is present in both the launch and attach events means we may want to move this kind of logic to the DAP object or to some other helper.

I think we'd end up with a lot of various helpers in the base class, which may not be applicable to the various subclasses.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Are you okay with doing that in a separate PR? If I move stuff into the base class, it becomes obvious what needs to be moved (I'll add a FIXME).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, that is fine by me

Copy link

github-actions bot commented Feb 22, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

 Conflicts:
	lldb/tools/lldb-dap/lldb-dap.cpp
@JDevlieghere JDevlieghere marked this pull request as ready for review February 23, 2025 02:32
@JDevlieghere
Copy link
Member Author

I'm marking this as ready for review. I think the 9 requests that have been migrated are pretty representative and I intentionally made it so that the old and new way of register requests keeps working. I was running the tests between every PR. However, resolving the merge conflict with main made me realize that almost anything touching lldb-dap is going to result in a pretty tricky merge conflict so I'd prefer to continue doing this work incrementally.

Copy link
Contributor

@ashgti ashgti left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, one question about a potentially duplicated function.

continue;
// Both attach and launch take a either a sourcePath or sourceMap
// argument (or neither), from which we need to set the target.source-map.
void SetSourceMapFromArguments(DAP &dap, const llvm::json::Object &arguments) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this repeated? Or I think maybe this is still here for the existing request_launch function that hasn't moved over yet.

Should this move to the a RequestHelpers.h like we have for EventHelpers.h?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I managed to avoid dit by migrating to LaunchRequestHandler & RestartRequestHandler.

@JDevlieghere JDevlieghere changed the title [lldb-dap] Move requests into their own object/file [lldb-dap] Refactor request handlers (NFC) Feb 23, 2025
@JDevlieghere JDevlieghere merged commit d0e37d9 into llvm:main Feb 24, 2025
10 checks passed
@JDevlieghere JDevlieghere deleted the request branch February 24, 2025 02:13
JDevlieghere added a commit to JDevlieghere/llvm-project that referenced this pull request Feb 24, 2025
Copy link
Collaborator

@labath labath left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am thinking about how would this new approach work if we started executing requests in parallel (I know we're not planning to do that now, but this sort of started out from that), and it seems to me that the this design (a "singleton" object with a non-const member) isn't the best design for that. I think this could be improved in at least two ways, but the choice depends on which way we'd like to take this:

  • option one is to make operator() const. This guarantees the implementation doesn't store any information about the currently processed request in the object itself. This prevents "leaking" of data from one request into another and as a consequence, makes the object safe to use concurrently
  • option two is to create a new handler object for request. This takes the opposite approach and actually encourages to (transiently) store information about the current request in the handler object (but then makes it safe by making sure the information is compartmentalized)

I don't want to make a choice here, since it depends on where you (the people who are actually going to be working on this) want to take this, but I think it would good to make a choice here, even if it gets changed later. The first approach (make the method const) would be most similar to status quo (because a free function doesn't even have an object to store information to), so maybe that could be the default?

SquallATF pushed a commit to SquallATF/llvm-project that referenced this pull request Apr 17, 2025
Currently, all request handlers are implemented as free functions in
lldb-dap.cpp. That file has grown to over 5000 lines and is starting to
become hard to maintain. This PR moves the request handlers into their
own class (and file), together with their documentation.

This PR migrates about a third of the request handlers and the rest will
be migrated in subsequent commits. I'm merging this in an incomplete
state because almost any lldb-dap change is going to result in merge
conflicts and migrating request handlers one by one is easier to review.
SquallATF pushed a commit to SquallATF/llvm-project that referenced this pull request Apr 17, 2025
SquallATF pushed a commit to SquallATF/llvm-project that referenced this pull request Apr 17, 2025
SquallATF pushed a commit to SquallATF/llvm-project that referenced this pull request Apr 17, 2025
SquallATF pushed a commit to SquallATF/llvm-project that referenced this pull request Apr 17, 2025
… handlers (llvm#128551)

Continuation of the work started in llvm#128262. Builds on top of llvm#128550.
SquallATF pushed a commit to SquallATF/llvm-project that referenced this pull request Apr 17, 2025
Completes the work started in llvm#128262. This PR removes the
old way of register request handlers with callbacks and makes
the operator const.
SquallATF pushed a commit to SquallATF/llvm-project that referenced this pull request Apr 30, 2025
Currently, all request handlers are implemented as free functions in
lldb-dap.cpp. That file has grown to over 5000 lines and is starting to
become hard to maintain. This PR moves the request handlers into their
own class (and file), together with their documentation.

This PR migrates about a third of the request handlers and the rest will
be migrated in subsequent commits. I'm merging this in an incomplete
state because almost any lldb-dap change is going to result in merge
conflicts and migrating request handlers one by one is easier to review.
SquallATF pushed a commit to SquallATF/llvm-project that referenced this pull request Apr 30, 2025
SquallATF pushed a commit to SquallATF/llvm-project that referenced this pull request Apr 30, 2025
SquallATF pushed a commit to SquallATF/llvm-project that referenced this pull request Apr 30, 2025
SquallATF pushed a commit to SquallATF/llvm-project that referenced this pull request Apr 30, 2025
… handlers (llvm#128551)

Continuation of the work started in llvm#128262. Builds on top of llvm#128550.
SquallATF pushed a commit to SquallATF/llvm-project that referenced this pull request Apr 30, 2025
Completes the work started in llvm#128262. This PR removes the
old way of register request handlers with callbacks and makes
the operator const.
SquallATF pushed a commit to SquallATF/llvm-project that referenced this pull request May 15, 2025
Currently, all request handlers are implemented as free functions in
lldb-dap.cpp. That file has grown to over 5000 lines and is starting to
become hard to maintain. This PR moves the request handlers into their
own class (and file), together with their documentation.

This PR migrates about a third of the request handlers and the rest will
be migrated in subsequent commits. I'm merging this in an incomplete
state because almost any lldb-dap change is going to result in merge
conflicts and migrating request handlers one by one is easier to review.
SquallATF pushed a commit to SquallATF/llvm-project that referenced this pull request May 15, 2025
SquallATF pushed a commit to SquallATF/llvm-project that referenced this pull request May 15, 2025
SquallATF pushed a commit to SquallATF/llvm-project that referenced this pull request May 15, 2025
SquallATF pushed a commit to SquallATF/llvm-project that referenced this pull request May 15, 2025
… handlers (llvm#128551)

Continuation of the work started in llvm#128262. Builds on top of llvm#128550.
SquallATF pushed a commit to SquallATF/llvm-project that referenced this pull request May 15, 2025
Completes the work started in llvm#128262. This PR removes the
old way of register request handlers with callbacks and makes
the operator const.
SquallATF pushed a commit to SquallATF/llvm-project that referenced this pull request May 29, 2025
Currently, all request handlers are implemented as free functions in
lldb-dap.cpp. That file has grown to over 5000 lines and is starting to
become hard to maintain. This PR moves the request handlers into their
own class (and file), together with their documentation.

This PR migrates about a third of the request handlers and the rest will
be migrated in subsequent commits. I'm merging this in an incomplete
state because almost any lldb-dap change is going to result in merge
conflicts and migrating request handlers one by one is easier to review.
SquallATF pushed a commit to SquallATF/llvm-project that referenced this pull request May 29, 2025
SquallATF pushed a commit to SquallATF/llvm-project that referenced this pull request May 29, 2025
SquallATF pushed a commit to SquallATF/llvm-project that referenced this pull request May 29, 2025
SquallATF pushed a commit to SquallATF/llvm-project that referenced this pull request May 29, 2025
… handlers (llvm#128551)

Continuation of the work started in llvm#128262. Builds on top of llvm#128550.
SquallATF pushed a commit to SquallATF/llvm-project that referenced this pull request May 29, 2025
Completes the work started in llvm#128262. This PR removes the
old way of register request handlers with callbacks and makes
the operator const.
SquallATF pushed a commit to SquallATF/llvm-project that referenced this pull request Jun 13, 2025
Currently, all request handlers are implemented as free functions in
lldb-dap.cpp. That file has grown to over 5000 lines and is starting to
become hard to maintain. This PR moves the request handlers into their
own class (and file), together with their documentation.

This PR migrates about a third of the request handlers and the rest will
be migrated in subsequent commits. I'm merging this in an incomplete
state because almost any lldb-dap change is going to result in merge
conflicts and migrating request handlers one by one is easier to review.
SquallATF pushed a commit to SquallATF/llvm-project that referenced this pull request Jun 13, 2025
SquallATF pushed a commit to SquallATF/llvm-project that referenced this pull request Jun 13, 2025
SquallATF pushed a commit to SquallATF/llvm-project that referenced this pull request Jun 13, 2025
SquallATF pushed a commit to SquallATF/llvm-project that referenced this pull request Jun 13, 2025
… handlers (llvm#128551)

Continuation of the work started in llvm#128262. Builds on top of llvm#128550.
SquallATF pushed a commit to SquallATF/llvm-project that referenced this pull request Jun 13, 2025
Completes the work started in llvm#128262. This PR removes the
old way of register request handlers with callbacks and makes
the operator const.
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.

4 participants