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.
3031Error 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
0 commit comments