[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:23:15 PST 2024


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

>From 61224ee2642e7fa2723e48e4a32c90f5ec04759a 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   | 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 7436b9900e98b0..8092341a449b9f 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..8ae82a01652d3b 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();



More information about the lldb-commits mailing list