[Lldb-commits] [lldb] [lldb] Quit automatically if --batch has no commands to run (PR #179969)

David Spickett via lldb-commits lldb-commits at lists.llvm.org
Thu Feb 5 09:40:33 PST 2026


https://github.com/DavidSpickett updated https://github.com/llvm/llvm-project/pull/179969

>From bf47f822088491d8785a932ea8d32e7fe5ae7c21 Mon Sep 17 00:00:00 2001
From: David Spickett <david.spickett at arm.com>
Date: Wed, 4 Feb 2026 16:28:23 +0000
Subject: [PATCH 1/2] [lldb] Quit automatically if --batch has no commands to
 run

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.
---
 .../API/driver/batch_mode/TestBatchMode.py    | 19 +++++++++++++++++++
 lldb/tools/driver/Driver.cpp                  |  2 +-
 2 files changed, 20 insertions(+), 1 deletion(-)

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..92a837a390a54 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());

>From 7493d7c226c75e99a6418ccf88e2f17083c72d2d Mon Sep 17 00:00:00 2001
From: David Spickett <david.spickett at arm.com>
Date: Thu, 5 Feb 2026 17:38:27 +0000
Subject: [PATCH 2/2] go interactive on crash

---
 lldb/tools/driver/Driver.cpp | 1 +
 1 file changed, 1 insertion(+)

diff --git a/lldb/tools/driver/Driver.cpp b/lldb/tools/driver/Driver.cpp
index 92a837a390a54..72ed9d02930d4 100644
--- a/lldb/tools/driver/Driver.cpp
+++ b/lldb/tools/driver/Driver.cpp
@@ -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);



More information about the lldb-commits mailing list