Skip to content

Commit ae8facc

Browse files
authored
[lldb-dap] Do not write over the existing error if launchCommands fail during debugger launch. (#82051)
This fixes an issue where the error is lost if a command while executing `launchCommands` when launching the debugger. This should fix #82048
1 parent 4a23ab4 commit ae8facc

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
lines changed

lldb/packages/Python/lldbsuite/test/tools/lldb-dap/lldbdap_testcase.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ def verify_commands(self, flavor, output, commands):
122122
for cmd in commands:
123123
found = False
124124
for line in lines:
125+
if len(cmd) > 0 and (cmd[0] == "!" or cmd[0] == "?"):
126+
cmd = cmd[1:]
125127
if line.startswith(prefix) and cmd in line:
126128
found = True
127129
break

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

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
Test lldb-dap setBreakpoints request
33
"""
44

5-
65
import dap_server
76
from lldbsuite.test.decorators import *
87
from lldbsuite.test.lldbtest import *
@@ -398,7 +397,7 @@ def test_extra_launch_commands(self):
398397
# Verify all "preRunCommands" were found in console output
399398
self.verify_commands("preRunCommands", output, preRunCommands)
400399

401-
# Verify all "launchCommands" were founc in console output
400+
# Verify all "launchCommands" were found in console output
402401
# After execution, program should launch
403402
self.verify_commands("launchCommands", output, launchCommands)
404403
# Verify the "stopCommands" here
@@ -420,6 +419,47 @@ def test_extra_launch_commands(self):
420419
output = self.get_console(timeout=1.0)
421420
self.verify_commands("exitCommands", output, exitCommands)
422421

422+
@skipIfWindows
423+
@skipIfRemote
424+
def test_failing_launch_commands(self):
425+
"""
426+
Tests "launchCommands" failures prevents a launch.
427+
"""
428+
self.build_and_create_debug_adaptor()
429+
program = self.getBuildArtifact("a.out")
430+
431+
# Run an invalid launch command, in this case a bad path.
432+
launchCommands = ['!target create "/bad/path%s"' % (program)]
433+
434+
initCommands = ["target list", "platform list"]
435+
preRunCommands = ["image list a.out", "image dump sections a.out"]
436+
response = self.launch(
437+
program,
438+
initCommands=initCommands,
439+
preRunCommands=preRunCommands,
440+
launchCommands=launchCommands,
441+
expectFailure=True,
442+
)
443+
444+
self.assertFalse(response["success"])
445+
self.assertRegex(
446+
response["message"],
447+
r"Failed to run launch commands\. See the Debug Console for more details",
448+
)
449+
450+
# Get output from the console. This should contain both the
451+
# "initCommands" and the "preRunCommands".
452+
output = self.get_console()
453+
# Verify all "initCommands" were found in console output
454+
self.verify_commands("initCommands", output, initCommands)
455+
# Verify all "preRunCommands" were found in console output
456+
self.verify_commands("preRunCommands", output, preRunCommands)
457+
458+
# Verify all "launchCommands" were founc in console output
459+
# The launch should fail due to the invalid command.
460+
self.verify_commands("launchCommands", output, launchCommands)
461+
self.assertRegex(output, r"unable to find executable for '/bad/path/")
462+
423463
@skipIfWindows
424464
@skipIfNetBSD # Hangs on NetBSD as well
425465
@skipIf(archs=["arm", "aarch64"], oslist=["linux"])

lldb/tools/lldb-dap/lldb-dap.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1779,8 +1779,10 @@ lldb::SBError LaunchProcess(const llvm::json::Object &request) {
17791779
// Set the launch info so that run commands can access the configured
17801780
// launch details.
17811781
g_dap.target.SetLaunchInfo(launch_info);
1782-
if (llvm::Error err = g_dap.RunLaunchCommands(launchCommands))
1782+
if (llvm::Error err = g_dap.RunLaunchCommands(launchCommands)) {
17831783
error.SetErrorString(llvm::toString(std::move(err)).c_str());
1784+
return error;
1785+
}
17841786
// The custom commands might have created a new target so we should use the
17851787
// selected target after these commands are run.
17861788
g_dap.target = g_dap.debugger.GetSelectedTarget();

0 commit comments

Comments
 (0)