Skip to content

Commit 9d0d09a

Browse files
committed
Improving error reporting for attachCommands and adding a test for attachCommands.
Applying reviewer suggestions.
1 parent 6656f64 commit 9d0d09a

File tree

5 files changed

+47
-20
lines changed

5 files changed

+47
-20
lines changed

lldb/test/API/tools/lldb-dap/attach/TestDAP_attach.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def test_by_name(self):
7979
def test_by_name_waitFor(self):
8080
"""
8181
Tests attaching to a process by process name and waiting for the
82-
next instance of a process to be launched, ingoring all current
82+
next instance of a process to be launched, ignoring all current
8383
ones.
8484
"""
8585
program = self.build_and_create_debug_adapter_for_attach()
@@ -102,7 +102,7 @@ def test_commands(self):
102102
that can be passed during attach.
103103
104104
"initCommands" are a list of LLDB commands that get executed
105-
before the targt is created.
105+
before the target is created.
106106
"preRunCommands" are a list of LLDB commands that get executed
107107
after the target has been created and before the launch.
108108
"stopCommands" are a list of LLDB commands that get executed each
@@ -180,6 +180,24 @@ def test_commands(self):
180180
self.verify_commands("exitCommands", output, exitCommands)
181181
self.verify_commands("terminateCommands", output, terminateCommands)
182182

183+
def test_attach_command_process_failures(self):
184+
"""
185+
Tests that a 'attachCommands' is expected to leave the debugger's
186+
selected target with a valid process.
187+
"""
188+
program = self.build_and_create_debug_adapter_for_attach()
189+
attachCommands = ['script print("oops, forgot to attach to a process...")']
190+
resp = self.attach(
191+
program=program,
192+
attachCommands=attachCommands,
193+
expectFailure=True,
194+
)
195+
self.assertFalse(resp["success"])
196+
self.assertIn(
197+
"attachCommands failed to attach to a process",
198+
resp["body"]["error"]["format"],
199+
)
200+
183201
@skipIfNetBSD # Hangs on NetBSD as well
184202
@skipIf(
185203
archs=["arm", "aarch64"]

lldb/tools/lldb-dap/DAP.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -675,11 +675,12 @@ lldb::SBTarget DAP::CreateTarget(lldb::SBError &error) {
675675
// enough information to determine correct arch and platform (or ELF can be
676676
// omitted at all), so it is good to leave the user an opportunity to specify
677677
// those. Any of those three can be left empty.
678-
auto target = this->debugger.CreateTarget(configuration.program.data(),
679-
configuration.targetTriple.data(),
680-
configuration.platformName.data(),
681-
true, // Add dependent modules.
682-
error);
678+
auto target = this->debugger.CreateTarget(
679+
/*filename=*/configuration.program.data(),
680+
/*target_triple=*/configuration.targetTriple.data(),
681+
/*platform_name=*/configuration.platformName.data(),
682+
/*add_dependent_modules=*/true, // Add dependent modules.
683+
error);
683684

684685
return target;
685686
}

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,10 @@ Error AttachRequestHandler::Run(const AttachRequestArguments &args) const {
7777
if ((args.pid == LLDB_INVALID_PROCESS_ID ||
7878
args.gdbRemotePort == LLDB_DAP_INVALID_PORT) &&
7979
args.waitFor) {
80-
char attach_msg[256];
81-
auto attach_msg_len = snprintf(attach_msg, sizeof(attach_msg),
82-
"Waiting to attach to \"%s\"...",
83-
dap.target.GetExecutable().GetFilename());
84-
dap.SendOutput(OutputType::Console, StringRef(attach_msg, attach_msg_len));
80+
dap.SendOutput(OutputType::Console,
81+
llvm::formatv("Waiting to attach to \"{0}\"...",
82+
dap.target.GetExecutable().GetFilename())
83+
.str());
8584
}
8685

8786
{
@@ -96,7 +95,13 @@ Error AttachRequestHandler::Run(const AttachRequestArguments &args) const {
9695
// state.
9796
if (llvm::Error err = dap.RunAttachCommands(args.attachCommands))
9897
return err;
98+
9999
dap.target = dap.debugger.GetSelectedTarget();
100+
101+
// Validate the attachCommand results.
102+
if (!dap.target.GetProcess().IsValid())
103+
return make_error<DAPError>(
104+
"attachCommands failed to attach to a process");
100105
} else if (!args.coreFile.empty()) {
101106
dap.target.LoadCore(args.coreFile.data(), error);
102107
} else if (args.gdbRemotePort != LLDB_DAP_INVALID_PORT) {
@@ -116,7 +121,7 @@ Error AttachRequestHandler::Run(const AttachRequestArguments &args) const {
116121
attach_info.SetProcessID(args.pid);
117122
else if (!dap.configuration.program.empty())
118123
attach_info.SetExecutable(dap.configuration.program.data());
119-
attach_info.SetWaitForLaunch(args.waitFor, false /*async*/);
124+
attach_info.SetWaitForLaunch(args.waitFor, /*async=*/false);
120125
dap.target.Attach(attach_info, error);
121126
}
122127
}

lldb/tools/lldb-dap/Handler/RequestHandler.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,9 @@ class RequestHandler : public BaseRequestHandler {
168168

169169
/// A hook for a request handler to run additional operations after the
170170
/// request response is sent but before the next request handler.
171+
///
172+
/// *NOTE*: PostRun will be invoked even if the `Run` operation returned an
173+
/// error.
171174
virtual void PostRun() const {};
172175

173176
protocol::ErrorResponseBody ToResponse(llvm::Error err) const {

lldb/tools/lldb-dap/Protocol/ProtocolRequests.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ struct Configuration {
151151
/// information in your executable contains relative paths, this option can be
152152
/// used so that `lldb-dap` can find source files and object files that have
153153
/// relative paths.
154-
std::string debuggerRoot = "";
154+
std::string debuggerRoot;
155155

156156
/// Enable auto generated summaries for variables when no summaries exist for
157157
/// a given type. This feature can cause performance delays in large projects
@@ -194,7 +194,7 @@ struct Configuration {
194194

195195
/// Specify a source path to remap "./" to allow full paths to be used when
196196
/// setting breakpoints in binaries that have relative source paths.
197-
std::string sourcePath = "";
197+
std::string sourcePath;
198198

199199
/// Specify an array of path re-mappings. Each element in the array must be a
200200
/// two element array containing a source and destination pathname. Overrides
@@ -230,15 +230,15 @@ struct Configuration {
230230
///
231231
/// *NOTE:* When launching, either `launchCommands` or `program` must be
232232
/// configured. If both are configured then `launchCommands` takes priority.
233-
std::string program = "";
233+
std::string program;
234234

235235
/// Target triple for the program (arch-vendor-os). If not set, inferred from
236236
/// the binary.
237-
std::string targetTriple = "";
237+
std::string targetTriple;
238238

239239
/// Specify name of the platform to use for this target, creating the platform
240240
/// if necessary.
241-
std::string platformName = "";
241+
std::string platformName;
242242
};
243243

244244
/// lldb-dap specific launch arguments.
@@ -264,7 +264,7 @@ struct LaunchRequestArguments {
264264
std::vector<std::string> launchCommands;
265265

266266
/// The program working directory.
267-
std::string cwd = "";
267+
std::string cwd;
268268

269269
/// An array of command line argument strings to be passed to the program
270270
/// being launched.
@@ -336,7 +336,7 @@ struct AttachRequestArguments {
336336
std::string gdbRemoteHostname = "localhost";
337337

338338
/// Path to the core file to debug.
339-
std::string coreFile = "";
339+
std::string coreFile;
340340

341341
/// @}
342342
};

0 commit comments

Comments
 (0)