[Lldb-commits] [lldb] [lldb-dap] Do not write over the existing error if launchCommands fail during debugger launch. (PR #82051)

John Harrison via lldb-commits lldb-commits at lists.llvm.org
Fri Feb 16 15:16:11 PST 2024


https://github.com/ashgti updated https://github.com/llvm/llvm-project/pull/82051

>From f76dbe10e30c84aadaf33c597fe81bc0a285c995 Mon Sep 17 00:00:00 2001
From: John Harrison <harjohn at google.com>
Date: Fri, 16 Feb 2024 14:11:10 -0800
Subject: [PATCH] [lldb-dap] Do not write over the existing error if
 launchCommands fail during debugger launch.

This fixes an issue where the error is lost if a command while executing `launchCommands` when launching the debugger.
---
 .../test/tools/lldb-dap/lldbdap_testcase.py   |  2 +
 .../tools/lldb-dap/launch/TestDAP_launch.py   | 47 +++++++++++++++++--
 lldb/tools/lldb-dap/lldb-dap.cpp              |  4 +-
 3 files changed, 48 insertions(+), 5 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 7436b9900e98b0..264e66d7e0dab8 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 78958612253691..d0d09c5ac74b31 100644
--- a/lldb/test/API/tools/lldb-dap/launch/TestDAP_launch.py
+++ b/lldb/test/API/tools/lldb-dap/launch/TestDAP_launch.py
@@ -272,9 +272,6 @@ def test_environment(self):
 
     @skipIfWindows
     @skipIfRemote
-    @skipIf(
-        archs=["arm", "aarch64"]
-    )  # failed run https://lab.llvm.org/buildbot/#/builders/96/builds/6933
     def test_commands(self):
         """
         Tests the "initCommands", "preRunCommands", "stopCommands",
@@ -398,7 +395,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 +417,48 @@ 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 the "launchCommands" failing to launch a target.
+        """
+        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)
+
     @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();



More information about the lldb-commits mailing list