Author: David Spickett
Date: 2026-02-09T13:16:58Z
New Revision: 1ff4b503802b049463da4a8fd4aaac496ddd260b

URL: 
https://github.com/llvm/llvm-project/commit/1ff4b503802b049463da4a8fd4aaac496ddd260b
DIFF: 
https://github.com/llvm/llvm-project/commit/1ff4b503802b049463da4a8fd4aaac496ddd260b.diff

LOG: [lldb] Quit automatically if --batch has no commands to run (#179969)

Fixes #179700

Simple fix, if we are in batch mode, don't go into an interactive
session after checking if there are commands to run.

Testing it is more tricky. I tried a shell test as I thought it would be
simplest. However to be able to FileCheck I had to pipe and the pipe
turns off the prompt because it's non-interactive. The prompt is the
thing that must not be printed.

So I've just spawned lldb as a subprocess. If it doesn't quit quickly
then something is wrong. The timeout is high not because it should
normally take that long, but because sometimes a process will get
stalled for a while and I don't want this to be flaky.

(though in theory it can get stalled for much longer than a minute)

If it does time out, the process will be cleaned up automatically. See
https://docs.python.org/3/library/subprocess.html#subprocess.run

> A timeout may be specified in seconds, it is internally
> passed on to Popen.communicate(). If the timeout expires,
> the child process will be killed and waited for.

Added: 
    

Modified: 
    lldb/test/API/driver/batch_mode/TestBatchMode.py
    lldb/tools/driver/Driver.cpp

Removed: 
    


################################################################################
diff  --git a/lldb/test/API/driver/batch_mode/TestBatchMode.py 
b/lldb/test/API/driver/batch_mode/TestBatchMode.py
index f58128278c249..aca4b7e5e6423 100644
--- a/lldb/test/API/driver/batch_mode/TestBatchMode.py
+++ b/lldb/test/API/driver/batch_mode/TestBatchMode.py
@@ -4,6 +4,7 @@
 
 
 import lldb
+import subprocess
 from lldbsuite.test.decorators import *
 from lldbsuite.test.lldbtest import *
 from lldbsuite.test import lldbutil
@@ -13,6 +14,24 @@
 class DriverBatchModeTest(PExpectTest):
     source = "main.c"
 
+    @skipIfRemote
+    def test_batch_mode_no_commands_quits(self):
+        """--batch should immediately quit if there are no commands given."""
+        try:
+            proc = subprocess.run(
+                [lldbtest_config.lldbExec, "--batch"],
+                timeout=60,
+                stdout=subprocess.PIPE,
+                text=True,
+            )
+        except subprocess.TimeoutExpired:
+            self.fail("lldb did not quit automatically.")
+
+        # Exit succesfully.
+        self.assertEqual(proc.returncode, 0)
+        # No prompt printed.
+        self.assertEqual(proc.stdout, "")
+
     @skipIf(macos_version=["<", "14.0"], asan=True)
     @skipIf(oslist=["linux"], archs=["arm$", "aarch64"])  # Randomly fails on 
buildbot
     @expectedFlakeyFreeBSD("llvm.org/pr25172 fails rarely on the buildbot")

diff  --git a/lldb/tools/driver/Driver.cpp b/lldb/tools/driver/Driver.cpp
index 48107717abd31..72ed9d02930d4 100644
--- a/lldb/tools/driver/Driver.cpp
+++ b/lldb/tools/driver/Driver.cpp
@@ -634,7 +634,7 @@ int Driver::MainLoop() {
   // Check if we have any data in the commands stream, and if so, save it to a
   // temp file
   // so we can then run the command interpreter using the file contents.
-  bool go_interactive = true;
+  bool go_interactive = !m_option_data.m_batch;
   if ((commands_stream.GetData() != nullptr) &&
       (commands_stream.GetSize() != 0u)) {
     SBError error = m_debugger.SetInputString(commands_stream.GetData());
@@ -672,6 +672,7 @@ int Driver::MainLoop() {
     if (m_option_data.m_batch &&
         results.GetResult() == lldb::eCommandInterpreterResultInferiorCrash &&
         !m_option_data.m_after_crash_commands.empty()) {
+      go_interactive = true;
       SBStream crash_commands_stream;
       WriteCommandsForSourcing(eCommandPlacementAfterCrash,
                                crash_commands_stream);


        
_______________________________________________
lldb-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to