David Spickett 1ff4b50380
[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.
2026-02-09 13:16:58 +00:00

209 lines
6.8 KiB
Python

"""
Test that the lldb driver's batch mode works correctly.
"""
import lldb
import subprocess
from lldbsuite.test.decorators import *
from lldbsuite.test.lldbtest import *
from lldbsuite.test import lldbutil
from lldbsuite.test.lldbpexpect import PExpectTest
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")
def test_batch_mode_run_crash(self):
"""Test that the lldb driver's batch mode works correctly."""
self.build()
exe = self.getBuildArtifact("a.out")
# Pass CRASH so the process will crash and stop in batch mode.
extra_args = [
"-b",
"-o",
"break set -n main",
"-o",
"run",
"-o",
"continue",
"-k",
"frame var touch_me_not",
"--",
"CRASH",
]
self.launch(executable=exe, extra_args=extra_args)
child = self.child
# We should see the "run":
child.expect_exact("run")
# We should have hit the breakpoint & continued:
child.expect_exact("continue")
# The App should have crashed:
child.expect_exact("About to crash")
# The -k option should have printed the frame variable once:
child.expect_exact("(char *) touch_me_not")
# Then we should have a live prompt:
self.expect_prompt()
self.expect("frame variable touch_me_not", substrs=["(char *) touch_me_not"])
@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")
def test_batch_mode_run_exit(self):
"""Test that the lldb driver's batch mode works correctly."""
self.build()
exe = self.getBuildArtifact("a.out")
# Now do it again, and make sure if we don't crash, we quit:
extra_args = [
"-b",
"-o",
"break set -n main",
"-o",
"run",
"-o",
"continue",
"--",
"NOCRASH",
]
self.launch(executable=exe, extra_args=extra_args)
child = self.child
# We should see the "run":
child.expect_exact("run")
# We should have hit the breakpoint & continued:
child.expect_exact("continue")
# The App should have not have crashed:
child.expect_exact("Got there on time and it did not crash.")
# Then lldb should exit.
child.expect_exact("exited")
import pexpect
child.expect(pexpect.EOF)
@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")
def test_batch_mode_launch_stop_at_entry(self):
"""Test that the lldb driver's batch mode works correctly for process launch."""
self.build()
exe = self.getBuildArtifact("a.out")
# Launch with the option '--stop-at-entry' stops with a signal (usually SIGSTOP)
# that should be suppressed since it doesn't imply a crash and
# this is not a reason to exit batch mode.
extra_args = [
"-b",
"-o",
"process launch --stop-at-entry",
"-o",
"continue",
]
self.launch(executable=exe, extra_args=extra_args)
child = self.child
# Check that the process has been launched:
child.expect("Process ([0-9]+) launched:")
# We should have continued:
child.expect_exact("continue")
# The App should have not have crashed:
child.expect_exact("Got there on time and it did not crash.")
# Then lldb should exit.
child.expect_exact("exited")
import pexpect
child.expect(pexpect.EOF)
def closeVictim(self):
if self.victim is not None:
self.victim.close()
self.victim = None
@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")
@expectedFailureNetBSD
def test_batch_mode_attach_exit(self):
"""Test that the lldb driver's batch mode works correctly."""
self.build()
self.setTearDownCleanup()
exe = self.getBuildArtifact("a.out")
# Start up the process by hand, attach to it, and wait for its completion.
# Attach is funny, since it looks like it stops with a signal on most Unixen so
# care must be taken not to treat that as a reason to exit batch mode.
# Start up the process by hand and wait for it to get to the wait loop.
import pexpect
self.victim = pexpect.spawn("%s WAIT" % (exe))
if self.victim is None:
self.fail("Could not spawn ", exe, ".")
self.addTearDownHook(self.closeVictim)
self.victim.expect("PID: ([0-9]+) END")
victim_pid = int(self.victim.match.group(1))
self.victim.expect("Waiting")
extra_args = [
"-b",
"-o",
"process attach -p %d" % victim_pid,
"-o",
"breakpoint set --file '%s' -p 'Stop here to unset keep_waiting' -N keep_waiting"
% self.source,
"-o",
"continue",
"-o",
"break delete keep_waiting",
"-o",
"expr keep_waiting = 0",
"-o",
"continue",
]
self.launch(executable=exe, extra_args=extra_args)
child = self.child
child.expect_exact("attach")
child.expect_exact(self.PROMPT + "continue")
child.expect_exact(self.PROMPT + "continue")
# Then we should see the process exit:
child.expect_exact("Process %d exited with status" % (victim_pid))
self.victim.expect(pexpect.EOF)
child.expect(pexpect.EOF)