Skip to content

Commit 6656f64

Browse files
committed
Adding some very basic validationg to both the launch and attach requests to simplify the flow later on.
1 parent 48e34e7 commit 6656f64

File tree

3 files changed

+26
-17
lines changed

3 files changed

+26
-17
lines changed

lldb/test/API/tools/lldb-dap/launch/TestDAP_launch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def test_failing_launch_commands_and_run_in_terminal(self):
5555
self.assertFalse(response["success"])
5656
self.assertTrue(self.get_dict_value(response, ["body", "error", "showUser"]))
5757
self.assertEqual(
58-
"launchCommands and runInTerminal are mutually exclusive",
58+
"'launchCommands' and 'runInTerminal' are mutually exclusive",
5959
self.get_dict_value(response, ["body", "error", "format"]),
6060
)
6161

lldb/tools/lldb-dap/Handler/AttachRequestHandler.cpp

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "LLDBUtils.h"
1313
#include "Protocol/ProtocolRequests.h"
1414
#include "RequestHandler.h"
15+
#include "lldb/API/SBAttachInfo.h"
1516
#include "lldb/API/SBListener.h"
1617
#include "lldb/lldb-defines.h"
1718
#include "llvm/Support/Error.h"
@@ -28,15 +29,24 @@ namespace lldb_dap {
2829
/// Since attaching is debugger/runtime specific, the arguments for this request
2930
/// are not part of this specification.
3031
Error AttachRequestHandler::Run(const AttachRequestArguments &args) const {
31-
dap.SetConfiguration(args.configuration, true);
32-
if (!args.coreFile.empty())
33-
dap.stop_at_entry = true;
32+
// Validate that we have a well formed attach request.
33+
if (args.attachCommands.empty() && args.coreFile.empty() &&
34+
args.configuration.program.empty() &&
35+
args.pid == LLDB_INVALID_PROCESS_ID &&
36+
args.gdbRemotePort == LLDB_DAP_INVALID_PORT)
37+
return make_error<DAPError>(
38+
"expected one of 'pid', 'program', 'attachCommands', "
39+
"'coreFile' or 'gdb-remote-port' to be specified");
3440

35-
// If both pid and port numbers are specified.
41+
// Check if we have mutually exclusive arguments.
3642
if ((args.pid != LLDB_INVALID_PROCESS_ID) &&
3743
(args.gdbRemotePort != LLDB_DAP_INVALID_PORT))
3844
return make_error<DAPError>(
39-
"pid and gdb-remote-port are mutually exclusive");
45+
"'pid' and 'gdb-remote-port' are mutually exclusive");
46+
47+
dap.SetConfiguration(args.configuration, /*is_attach=*/true);
48+
if (!args.coreFile.empty())
49+
dap.stop_at_entry = true;
4050

4151
PrintWelcomeMessage();
4252

@@ -78,20 +88,18 @@ Error AttachRequestHandler::Run(const AttachRequestArguments &args) const {
7888
// Perform the launch in synchronous mode so that we don't have to worry
7989
// about process state changes during the launch.
8090
ScopeSyncMode scope_sync_mode(dap.debugger);
91+
8192
if (!args.attachCommands.empty()) {
82-
// We have "attachCommands" that are a set of commands that are expected
83-
// to execute the commands after which a process should be created. If
84-
// there is no valid process after running these commands, we have failed.
93+
// Run the attach commands, after which we expect the debugger's selected
94+
// target to contain a valid and stopped process. Otherwise inform the
95+
// user that their command failed or the debugger is in an unexpected
96+
// state.
8597
if (llvm::Error err = dap.RunAttachCommands(args.attachCommands))
8698
return err;
87-
88-
// The custom commands might have created a new target so we should use
89-
// the selected target after these commands are run.
9099
dap.target = dap.debugger.GetSelectedTarget();
91100
} else if (!args.coreFile.empty()) {
92101
dap.target.LoadCore(args.coreFile.data(), error);
93102
} else if (args.gdbRemotePort != LLDB_DAP_INVALID_PORT) {
94-
// If port is specified and pid is not.
95103
lldb::SBListener listener = dap.debugger.GetListener();
96104

97105
// If the user hasn't provided the hostname property, default

lldb/tools/lldb-dap/Handler/LaunchRequestHandler.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@ namespace lldb_dap {
2222

2323
/// Launch request; value of command field is 'launch'.
2424
Error LaunchRequestHandler::Run(const LaunchRequestArguments &arguments) const {
25-
dap.SetConfiguration(arguments.configuration, /*is_attach=*/false);
26-
dap.last_launch_request = arguments;
27-
25+
// Validate that we have a well formed launch request.
2826
if (!arguments.launchCommands.empty() && arguments.runInTerminal)
2927
return make_error<DAPError>(
30-
"launchCommands and runInTerminal are mutually exclusive");
28+
"'launchCommands' and 'runInTerminal' are mutually exclusive");
29+
30+
dap.SetConfiguration(arguments.configuration, /*is_attach=*/false);
31+
dap.last_launch_request = arguments;
3132

3233
PrintWelcomeMessage();
3334

0 commit comments

Comments
 (0)