From ae8facc1f8e8a2d04b5a3519fc1901db110fe452 Mon Sep 17 00:00:00 2001 From: John Harrison Date: Tue, 20 Feb 2024 09:35:15 -0800 Subject: [PATCH] [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 --- .../test/tools/lldb-dap/lldbdap_testcase.py | 2 + .../tools/lldb-dap/launch/TestDAP_launch.py | 44 ++++++++++++++++++- lldb/tools/lldb-dap/lldb-dap.cpp | 4 +- 3 files changed, 47 insertions(+), 3 deletions(-) diff --git a/lldb/packages/Python/lldbsuite/test/tools/lldb-dap/lldbdap_testcase.py b/lldb/packages/Python/lldbsuite/test/tools/lldb-dap/lldbdap_testcase.py index 73bd037fd328cb..288cc8cf9a48c8 100644 --- a/lldb/packages/Python/lldbsuite/test/tools/lldb-dap/lldbdap_testcase.py +++ b/lldb/packages/Python/lldbsuite/test/tools/lldb-dap/lldbdap_testcase.py @@ -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 diff --git a/lldb/test/API/tools/lldb-dap/launch/TestDAP_launch.py b/lldb/test/API/tools/lldb-dap/launch/TestDAP_launch.py index 829fb3b7ba6a4c..04d741c1d47201 100644 --- a/lldb/test/API/tools/lldb-dap/launch/TestDAP_launch.py +++ b/lldb/test/API/tools/lldb-dap/launch/TestDAP_launch.py @@ -2,7 +2,6 @@ Test lldb-dap setBreakpoints request """ - import dap_server from lldbsuite.test.decorators import * from lldbsuite.test.lldbtest import * @@ -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 @@ -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"]) diff --git a/lldb/tools/lldb-dap/lldb-dap.cpp b/lldb/tools/lldb-dap/lldb-dap.cpp index 67022347e6d624..78b0b4078706aa 100644 --- a/lldb/tools/lldb-dap/lldb-dap.cpp +++ b/lldb/tools/lldb-dap/lldb-dap.cpp @@ -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();