-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[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
Changes from 1 commit
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
e6cbcc3
[lldb-dap] Move requests into their own object/file
JDevlieghere 92322ae
Address review feedback
JDevlieghere 86302e5
Address review feedback
JDevlieghere 9235950
Adopt BreakpointLocationsHandler
JDevlieghere 6557658
Adopt CompletionsHandler
JDevlieghere bf4e686
Adopt ContinueRequestHandler
JDevlieghere 25504a8
Adopt ConfigurationDoneRequestHandler
JDevlieghere 670701d
Adopt DisconnectRequestHandler
JDevlieghere dfb3b0c
Adopt EvaluateRequestHandler
JDevlieghere a8d212d
Adopt ExceptionInfoRequestHandler
JDevlieghere feae281
Adopt InitializeRequestHandler
JDevlieghere 4ef4724
Merge remote-tracking branch 'origin/main'
JDevlieghere 976d700
Adopt LaunchRequestHandler & RestartRequestHandler
JDevlieghere File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,211 @@ | ||
| //===-- AttachRequest.cpp -------------------------------------------------===// | ||
| // | ||
| // 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 "DAP.h" | ||
| #include "JSONUtils.h" | ||
| #include "Request.h" | ||
| #include "lldb/API/SBListener.h" | ||
| #include "llvm/Support/FileSystem.h" | ||
|
|
||
| namespace lldb_dap { | ||
| /// Prints a welcome message on the editor if the preprocessor variable | ||
| /// LLDB_DAP_WELCOME_MESSAGE is defined. | ||
| static void PrintWelcomeMessage(DAP &dap) { | ||
| #ifdef LLDB_DAP_WELCOME_MESSAGE | ||
| dap.SendOutput(OutputType::Console, LLDB_DAP_WELCOME_MESSAGE); | ||
| #endif | ||
| } | ||
|
|
||
| // "AttachRequest": { | ||
| // "allOf": [ { "$ref": "#/definitions/Request" }, { | ||
| // "type": "object", | ||
| // "description": "Attach request; value of command field is 'attach'.", | ||
| // "properties": { | ||
| // "command": { | ||
| // "type": "string", | ||
| // "enum": [ "attach" ] | ||
| // }, | ||
| // "arguments": { | ||
| // "$ref": "#/definitions/AttachRequestArguments" | ||
| // } | ||
| // }, | ||
| // "required": [ "command", "arguments" ] | ||
| // }] | ||
| // }, | ||
| // "AttachRequestArguments": { | ||
| // "type": "object", | ||
| // "description": "Arguments for 'attach' request.\nThe attach request has no | ||
| // standardized attributes." | ||
| // }, | ||
| // "AttachResponse": { | ||
| // "allOf": [ { "$ref": "#/definitions/Response" }, { | ||
| // "type": "object", | ||
| // "description": "Response to 'attach' request. This is just an | ||
| // acknowledgement, so no body field is required." | ||
| // }] | ||
| // } | ||
|
|
||
| void AttachRequest::operator()(const llvm::json::Object &request) { | ||
| dap.is_attach = true; | ||
| dap.last_launch_or_attach_request = request; | ||
| llvm::json::Object response; | ||
| lldb::SBError error; | ||
| FillResponse(request, response); | ||
| lldb::SBAttachInfo attach_info; | ||
| const int invalid_port = 0; | ||
| const auto *arguments = request.getObject("arguments"); | ||
| const lldb::pid_t pid = | ||
| GetUnsigned(arguments, "pid", LLDB_INVALID_PROCESS_ID); | ||
| const auto gdb_remote_port = | ||
| GetUnsigned(arguments, "gdb-remote-port", invalid_port); | ||
| const auto gdb_remote_hostname = | ||
| GetString(arguments, "gdb-remote-hostname", "localhost"); | ||
| if (pid != LLDB_INVALID_PROCESS_ID) | ||
| attach_info.SetProcessID(pid); | ||
| const auto wait_for = GetBoolean(arguments, "waitFor", false); | ||
| attach_info.SetWaitForLaunch(wait_for, false /*async*/); | ||
| dap.init_commands = GetStrings(arguments, "initCommands"); | ||
| dap.pre_run_commands = GetStrings(arguments, "preRunCommands"); | ||
| dap.stop_commands = GetStrings(arguments, "stopCommands"); | ||
| dap.exit_commands = GetStrings(arguments, "exitCommands"); | ||
| dap.terminate_commands = GetStrings(arguments, "terminateCommands"); | ||
| auto attachCommands = GetStrings(arguments, "attachCommands"); | ||
| llvm::StringRef core_file = GetString(arguments, "coreFile"); | ||
| const uint64_t timeout_seconds = GetUnsigned(arguments, "timeout", 30); | ||
| dap.stop_at_entry = | ||
| core_file.empty() ? GetBoolean(arguments, "stopOnEntry", false) : true; | ||
| dap.post_run_commands = GetStrings(arguments, "postRunCommands"); | ||
| const llvm::StringRef debuggerRoot = GetString(arguments, "debuggerRoot"); | ||
| dap.enable_auto_variable_summaries = | ||
| GetBoolean(arguments, "enableAutoVariableSummaries", false); | ||
| dap.enable_synthetic_child_debugging = | ||
| GetBoolean(arguments, "enableSyntheticChildDebugging", false); | ||
| dap.display_extended_backtrace = | ||
| GetBoolean(arguments, "displayExtendedBacktrace", false); | ||
| dap.command_escape_prefix = GetString(arguments, "commandEscapePrefix", "`"); | ||
| dap.SetFrameFormat(GetString(arguments, "customFrameFormat")); | ||
| dap.SetThreadFormat(GetString(arguments, "customThreadFormat")); | ||
|
|
||
| PrintWelcomeMessage(dap); | ||
|
|
||
| // This is a hack for loading DWARF in .o files on Mac where the .o files | ||
| // in the debug map of the main executable have relative paths which require | ||
| // the lldb-dap binary to have its working directory set to that relative | ||
| // root for the .o files in order to be able to load debug info. | ||
| if (!debuggerRoot.empty()) | ||
| llvm::sys::fs::set_current_path(debuggerRoot); | ||
|
|
||
| // Run any initialize LLDB commands the user specified in the launch.json | ||
| if (llvm::Error err = dap.RunInitCommands()) { | ||
| response["success"] = false; | ||
| EmplaceSafeString(response, "message", llvm::toString(std::move(err))); | ||
| dap.SendJSON(llvm::json::Value(std::move(response))); | ||
| return; | ||
| } | ||
|
|
||
| SetSourceMapFromArguments(*arguments); | ||
|
|
||
| lldb::SBError status; | ||
| dap.SetTarget(dap.CreateTargetFromArguments(*arguments, status)); | ||
| if (status.Fail()) { | ||
| response["success"] = llvm::json::Value(false); | ||
| EmplaceSafeString(response, "message", status.GetCString()); | ||
| dap.SendJSON(llvm::json::Value(std::move(response))); | ||
| return; | ||
| } | ||
|
|
||
| // Run any pre run LLDB commands the user specified in the launch.json | ||
| if (llvm::Error err = dap.RunPreRunCommands()) { | ||
| response["success"] = false; | ||
| EmplaceSafeString(response, "message", llvm::toString(std::move(err))); | ||
| dap.SendJSON(llvm::json::Value(std::move(response))); | ||
| return; | ||
| } | ||
|
|
||
| if ((pid == LLDB_INVALID_PROCESS_ID || gdb_remote_port == invalid_port) && | ||
| wait_for) { | ||
| char attach_msg[256]; | ||
| auto attach_msg_len = snprintf(attach_msg, sizeof(attach_msg), | ||
| "Waiting to attach to \"%s\"...", | ||
| dap.target.GetExecutable().GetFilename()); | ||
| dap.SendOutput(OutputType::Console, | ||
| llvm::StringRef(attach_msg, attach_msg_len)); | ||
| } | ||
| if (attachCommands.empty()) { | ||
| // No "attachCommands", just attach normally. | ||
| // Disable async events so the attach will be successful when we return from | ||
| // the launch call and the launch will happen synchronously | ||
| dap.debugger.SetAsync(false); | ||
| if (core_file.empty()) { | ||
| if ((pid != LLDB_INVALID_PROCESS_ID) && | ||
| (gdb_remote_port != invalid_port)) { | ||
| // If both pid and port numbers are specified. | ||
| error.SetErrorString("The user can't specify both pid and port"); | ||
| } else if (gdb_remote_port != invalid_port) { | ||
| // If port is specified and pid is not. | ||
| lldb::SBListener listener = dap.debugger.GetListener(); | ||
|
|
||
| // If the user hasn't provided the hostname property, default localhost | ||
| // being used. | ||
| std::string connect_url = | ||
| llvm::formatv("connect://{0}:", gdb_remote_hostname); | ||
| connect_url += std::to_string(gdb_remote_port); | ||
| dap.target.ConnectRemote(listener, connect_url.c_str(), "gdb-remote", | ||
| error); | ||
| } else { | ||
| // Attach by process name or id. | ||
| dap.target.Attach(attach_info, error); | ||
| } | ||
| } else | ||
| dap.target.LoadCore(core_file.data(), error); | ||
| // Reenable async events | ||
| dap.debugger.SetAsync(true); | ||
| } else { | ||
| // We have "attachCommands" that are a set of commands that are expected | ||
| // to execute the commands after which a process should be created. If there | ||
| // is no valid process after running these commands, we have failed. | ||
| if (llvm::Error err = dap.RunAttachCommands(attachCommands)) { | ||
| response["success"] = false; | ||
| EmplaceSafeString(response, "message", llvm::toString(std::move(err))); | ||
| dap.SendJSON(llvm::json::Value(std::move(response))); | ||
| return; | ||
| } | ||
| // The custom commands might have created a new target so we should use the | ||
| // selected target after these commands are run. | ||
| dap.target = dap.debugger.GetSelectedTarget(); | ||
|
|
||
| // Make sure the process is attached and stopped before proceeding as the | ||
| // the launch commands are not run using the synchronous mode. | ||
| error = dap.WaitForProcessToStop(timeout_seconds); | ||
| } | ||
|
|
||
| if (error.Success() && core_file.empty()) { | ||
| auto attached_pid = dap.target.GetProcess().GetProcessID(); | ||
| if (attached_pid == LLDB_INVALID_PROCESS_ID) { | ||
| if (attachCommands.empty()) | ||
| error.SetErrorString("failed to attach to a process"); | ||
| else | ||
| error.SetErrorString("attachCommands failed to attach to a process"); | ||
| } | ||
| } | ||
|
|
||
| if (error.Fail()) { | ||
| response["success"] = llvm::json::Value(false); | ||
| EmplaceSafeString(response, "message", std::string(error.GetCString())); | ||
| } else { | ||
| dap.RunPostRunCommands(); | ||
| } | ||
|
|
||
| dap.SendJSON(llvm::json::Value(std::move(response))); | ||
| if (error.Success()) { | ||
| SendProcessEvent(Attach); | ||
| dap.SendJSON(CreateEventObject("initialized")); | ||
| } | ||
| } | ||
|
|
||
| } // namespace lldb_dap |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| //===-- Request.cpp -------------------------------------------------------===// | ||
| // | ||
| // 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 "Request.h" | ||
| #include "DAP.h" | ||
| #include "JSONUtils.h" | ||
| #include "lldb/API/SBFileSpec.h" | ||
|
|
||
| namespace lldb_dap { | ||
|
|
||
| void Request::SendProcessEvent(Request::LaunchMethod launch_method) { | ||
| lldb::SBFileSpec exe_fspec = dap.target.GetExecutable(); | ||
| char exe_path[PATH_MAX]; | ||
| exe_fspec.GetPath(exe_path, sizeof(exe_path)); | ||
| llvm::json::Object event(CreateEventObject("process")); | ||
| llvm::json::Object body; | ||
| EmplaceSafeString(body, "name", std::string(exe_path)); | ||
| const auto pid = dap.target.GetProcess().GetProcessID(); | ||
| body.try_emplace("systemProcessId", (int64_t)pid); | ||
| body.try_emplace("isLocalProcess", true); | ||
| const char *startMethod = nullptr; | ||
| switch (launch_method) { | ||
| case Launch: | ||
| startMethod = "launch"; | ||
| break; | ||
| case Attach: | ||
| startMethod = "attach"; | ||
| break; | ||
| case AttachForSuspendedLaunch: | ||
| startMethod = "attachForSuspendedLaunch"; | ||
| break; | ||
| } | ||
| body.try_emplace("startMethod", startMethod); | ||
| event.try_emplace("body", std::move(body)); | ||
| dap.SendJSON(llvm::json::Value(std::move(event))); | ||
| } | ||
|
|
||
| // 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) { | ||
| const char *sourceMapHelp = | ||
| "source must be be an array of two-element arrays, " | ||
| "each containing a source and replacement path string.\n"; | ||
|
|
||
| std::string sourceMapCommand; | ||
| llvm::raw_string_ostream strm(sourceMapCommand); | ||
| strm << "settings set target.source-map "; | ||
| const auto sourcePath = GetString(arguments, "sourcePath"); | ||
|
|
||
| // sourceMap is the new, more general form of sourcePath and overrides it. | ||
| constexpr llvm::StringRef sourceMapKey = "sourceMap"; | ||
|
|
||
| if (const auto *sourceMapArray = arguments.getArray(sourceMapKey)) { | ||
| for (const auto &value : *sourceMapArray) { | ||
| const auto *mapping = value.getAsArray(); | ||
| if (mapping == nullptr || mapping->size() != 2 || | ||
| (*mapping)[0].kind() != llvm::json::Value::String || | ||
| (*mapping)[1].kind() != llvm::json::Value::String) { | ||
| dap.SendOutput(OutputType::Console, llvm::StringRef(sourceMapHelp)); | ||
| return; | ||
| } | ||
| const auto mapFrom = GetAsString((*mapping)[0]); | ||
| const auto mapTo = GetAsString((*mapping)[1]); | ||
| strm << "\"" << mapFrom << "\" \"" << mapTo << "\" "; | ||
| } | ||
| } else if (const auto *sourceMapObj = arguments.getObject(sourceMapKey)) { | ||
| for (const auto &[key, value] : *sourceMapObj) { | ||
| if (value.kind() == llvm::json::Value::String) { | ||
| strm << "\"" << key.str() << "\" \"" << GetAsString(value) << "\" "; | ||
| } | ||
| } | ||
| } else { | ||
| if (ObjectContainsKey(arguments, sourceMapKey)) { | ||
| dap.SendOutput(OutputType::Console, llvm::StringRef(sourceMapHelp)); | ||
| return; | ||
| } | ||
| if (sourcePath.empty()) | ||
| return; | ||
| // Do any source remapping needed before we create our targets | ||
| strm << "\".\" \"" << sourcePath << "\""; | ||
| } | ||
| if (!sourceMapCommand.empty()) { | ||
| dap.RunLLDBCommands("Setting source map:", {sourceMapCommand}); | ||
| } | ||
| } | ||
|
|
||
| } // namespace lldb_dap | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
launchandattachevents means we may want to move this kind of logic to theDAPobject 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.
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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