Skip to content

Commit

Permalink
[lldb-dap] Do not write over the existing error if launchCommands fai…
Browse files Browse the repository at this point in the history
…l 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
  • Loading branch information
ashgti committed Feb 20, 2024
1 parent 4a23ab4 commit ae8facc
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ def verify_commands(self, flavor, output, commands):
for cmd in commands:
found = False
for line in lines:
if len(cmd) > 0 and (cmd[0] == "!" or cmd[0] == "?"):
cmd = cmd[1:]
if line.startswith(prefix) and cmd in line:
found = True
break
Expand Down
44 changes: 42 additions & 2 deletions lldb/test/API/tools/lldb-dap/launch/TestDAP_launch.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
Test lldb-dap setBreakpoints request
"""


import dap_server
from lldbsuite.test.decorators import *
from lldbsuite.test.lldbtest import *
Expand Down Expand Up @@ -398,7 +397,7 @@ def test_extra_launch_commands(self):
# Verify all "preRunCommands" were found in console output
self.verify_commands("preRunCommands", output, preRunCommands)

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

@skipIfWindows
@skipIfRemote
def test_failing_launch_commands(self):
"""
Tests "launchCommands" failures prevents a launch.
"""
self.build_and_create_debug_adaptor()
program = self.getBuildArtifact("a.out")

# Run an invalid launch command, in this case a bad path.
launchCommands = ['!target create "/bad/path%s"' % (program)]

initCommands = ["target list", "platform list"]
preRunCommands = ["image list a.out", "image dump sections a.out"]
response = self.launch(
program,
initCommands=initCommands,
preRunCommands=preRunCommands,
launchCommands=launchCommands,
expectFailure=True,
)

self.assertFalse(response["success"])
self.assertRegex(
response["message"],
r"Failed to run launch commands\. See the Debug Console for more details",
)

# Get output from the console. This should contain both the
# "initCommands" and the "preRunCommands".
output = self.get_console()
# Verify all "initCommands" were found in console output
self.verify_commands("initCommands", output, initCommands)
# Verify all "preRunCommands" were found in console output
self.verify_commands("preRunCommands", output, preRunCommands)

# Verify all "launchCommands" were founc in console output
# The launch should fail due to the invalid command.
self.verify_commands("launchCommands", output, launchCommands)
self.assertRegex(output, r"unable to find executable for '/bad/path/")

@skipIfWindows
@skipIfNetBSD # Hangs on NetBSD as well
@skipIf(archs=["arm", "aarch64"], oslist=["linux"])
Expand Down
4 changes: 3 additions & 1 deletion lldb/tools/lldb-dap/lldb-dap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1779,8 +1779,10 @@ lldb::SBError LaunchProcess(const llvm::json::Object &request) {
// Set the launch info so that run commands can access the configured
// launch details.
g_dap.target.SetLaunchInfo(launch_info);
if (llvm::Error err = g_dap.RunLaunchCommands(launchCommands))
if (llvm::Error err = g_dap.RunLaunchCommands(launchCommands)) {
error.SetErrorString(llvm::toString(std::move(err)).c_str());
return error;
}
// The custom commands might have created a new target so we should use the
// selected target after these commands are run.
g_dap.target = g_dap.debugger.GetSelectedTarget();
Expand Down

0 comments on commit ae8facc

Please sign in to comment.