xref: /llvm-project/lldb/test/API/driver/batch_mode/TestBatchMode.py (revision 1b70580dd867195b0442e582eccd42abc41ee12d)
199451b44SJordan Rupprecht"""
299451b44SJordan RupprechtTest that the lldb driver's batch mode works correctly.
399451b44SJordan Rupprecht"""
499451b44SJordan Rupprecht
599451b44SJordan Rupprecht
699451b44SJordan Rupprechtimport lldb
799451b44SJordan Rupprechtfrom lldbsuite.test.decorators import *
899451b44SJordan Rupprechtfrom lldbsuite.test.lldbtest import *
999451b44SJordan Rupprechtfrom lldbsuite.test import lldbutil
1099451b44SJordan Rupprechtfrom lldbsuite.test.lldbpexpect import PExpectTest
1199451b44SJordan Rupprecht
1299451b44SJordan Rupprecht
1399451b44SJordan Rupprechtclass DriverBatchModeTest(PExpectTest):
142238dcc3SJonas Devlieghere    source = "main.c"
1599451b44SJordan Rupprecht
16*1b70580dSAdrian Prantl    @skipIf(macos_version=["<", "14.0"], asan=True)
178813bc02SMuhammad Omair Javaid    @skipIf(oslist=["linux"], archs=["arm", "aarch64"])  # Randomly fails on buildbot
1899451b44SJordan Rupprecht    @expectedFlakeyFreeBSD("llvm.org/pr25172 fails rarely on the buildbot")
1999451b44SJordan Rupprecht    def test_batch_mode_run_crash(self):
2099451b44SJordan Rupprecht        """Test that the lldb driver's batch mode works correctly."""
2199451b44SJordan Rupprecht        self.build()
2299451b44SJordan Rupprecht
2399451b44SJordan Rupprecht        exe = self.getBuildArtifact("a.out")
2499451b44SJordan Rupprecht
2599451b44SJordan Rupprecht        # Pass CRASH so the process will crash and stop in batch mode.
262238dcc3SJonas Devlieghere        extra_args = [
272238dcc3SJonas Devlieghere            "-b",
282238dcc3SJonas Devlieghere            "-o",
292238dcc3SJonas Devlieghere            "break set -n main",
302238dcc3SJonas Devlieghere            "-o",
312238dcc3SJonas Devlieghere            "run",
322238dcc3SJonas Devlieghere            "-o",
332238dcc3SJonas Devlieghere            "continue",
342238dcc3SJonas Devlieghere            "-k",
352238dcc3SJonas Devlieghere            "frame var touch_me_not",
362238dcc3SJonas Devlieghere            "--",
372238dcc3SJonas Devlieghere            "CRASH",
3899451b44SJordan Rupprecht        ]
3999451b44SJordan Rupprecht        self.launch(executable=exe, extra_args=extra_args)
4099451b44SJordan Rupprecht        child = self.child
4199451b44SJordan Rupprecht
4299451b44SJordan Rupprecht        # We should see the "run":
4399451b44SJordan Rupprecht        child.expect_exact("run")
4499451b44SJordan Rupprecht        # We should have hit the breakpoint & continued:
4599451b44SJordan Rupprecht        child.expect_exact("continue")
4699451b44SJordan Rupprecht        # The App should have crashed:
4799451b44SJordan Rupprecht        child.expect_exact("About to crash")
4899451b44SJordan Rupprecht        # The -k option should have printed the frame variable once:
492238dcc3SJonas Devlieghere        child.expect_exact("(char *) touch_me_not")
5099451b44SJordan Rupprecht        # Then we should have a live prompt:
5199451b44SJordan Rupprecht        self.expect_prompt()
522238dcc3SJonas Devlieghere        self.expect("frame variable touch_me_not", substrs=["(char *) touch_me_not"])
5399451b44SJordan Rupprecht
54*1b70580dSAdrian Prantl    @skipIf(macos_version=["<", "14.0"], asan=True)
558813bc02SMuhammad Omair Javaid    @skipIf(oslist=["linux"], archs=["arm", "aarch64"])  # Randomly fails on buildbot
5699451b44SJordan Rupprecht    @expectedFlakeyFreeBSD("llvm.org/pr25172 fails rarely on the buildbot")
5799451b44SJordan Rupprecht    def test_batch_mode_run_exit(self):
5899451b44SJordan Rupprecht        """Test that the lldb driver's batch mode works correctly."""
5999451b44SJordan Rupprecht        self.build()
6099451b44SJordan Rupprecht
6199451b44SJordan Rupprecht        exe = self.getBuildArtifact("a.out")
6299451b44SJordan Rupprecht
6399451b44SJordan Rupprecht        # Now do it again, and make sure if we don't crash, we quit:
642238dcc3SJonas Devlieghere        extra_args = [
652238dcc3SJonas Devlieghere            "-b",
662238dcc3SJonas Devlieghere            "-o",
672238dcc3SJonas Devlieghere            "break set -n main",
682238dcc3SJonas Devlieghere            "-o",
692238dcc3SJonas Devlieghere            "run",
702238dcc3SJonas Devlieghere            "-o",
712238dcc3SJonas Devlieghere            "continue",
722238dcc3SJonas Devlieghere            "--",
732238dcc3SJonas Devlieghere            "NOCRASH",
7499451b44SJordan Rupprecht        ]
7599451b44SJordan Rupprecht        self.launch(executable=exe, extra_args=extra_args)
7699451b44SJordan Rupprecht        child = self.child
7799451b44SJordan Rupprecht
7899451b44SJordan Rupprecht        # We should see the "run":
7999451b44SJordan Rupprecht        child.expect_exact("run")
8099451b44SJordan Rupprecht        # We should have hit the breakpoint & continued:
8199451b44SJordan Rupprecht        child.expect_exact("continue")
8299451b44SJordan Rupprecht        # The App should have not have crashed:
8399451b44SJordan Rupprecht        child.expect_exact("Got there on time and it did not crash.")
8499451b44SJordan Rupprecht
8599451b44SJordan Rupprecht        # Then lldb should exit.
8699451b44SJordan Rupprecht        child.expect_exact("exited")
8799451b44SJordan Rupprecht        import pexpect
882238dcc3SJonas Devlieghere
8999451b44SJordan Rupprecht        child.expect(pexpect.EOF)
9099451b44SJordan Rupprecht
91*1b70580dSAdrian Prantl    @skipIf(macos_version=["<", "14.0"], asan=True)
928813bc02SMuhammad Omair Javaid    @skipIf(oslist=["linux"], archs=["arm", "aarch64"])  # Randomly fails on buildbot
9399451b44SJordan Rupprecht    @expectedFlakeyFreeBSD("llvm.org/pr25172 fails rarely on the buildbot")
9499451b44SJordan Rupprecht    def test_batch_mode_launch_stop_at_entry(self):
9599451b44SJordan Rupprecht        """Test that the lldb driver's batch mode works correctly for process launch."""
9699451b44SJordan Rupprecht        self.build()
9799451b44SJordan Rupprecht
9899451b44SJordan Rupprecht        exe = self.getBuildArtifact("a.out")
9999451b44SJordan Rupprecht
10099451b44SJordan Rupprecht        # Launch with the option '--stop-at-entry' stops with a signal (usually SIGSTOP)
10199451b44SJordan Rupprecht        # that should be suppressed since it doesn't imply a crash and
10299451b44SJordan Rupprecht        # this is not a reason to exit batch mode.
1032238dcc3SJonas Devlieghere        extra_args = [
1042238dcc3SJonas Devlieghere            "-b",
1052238dcc3SJonas Devlieghere            "-o",
1062238dcc3SJonas Devlieghere            "process launch --stop-at-entry",
1072238dcc3SJonas Devlieghere            "-o",
1082238dcc3SJonas Devlieghere            "continue",
10999451b44SJordan Rupprecht        ]
11099451b44SJordan Rupprecht        self.launch(executable=exe, extra_args=extra_args)
11199451b44SJordan Rupprecht        child = self.child
11299451b44SJordan Rupprecht
11399451b44SJordan Rupprecht        # Check that the process has been launched:
11499451b44SJordan Rupprecht        child.expect("Process ([0-9]+) launched:")
11599451b44SJordan Rupprecht        # We should have continued:
11699451b44SJordan Rupprecht        child.expect_exact("continue")
11799451b44SJordan Rupprecht        # The App should have not have crashed:
11899451b44SJordan Rupprecht        child.expect_exact("Got there on time and it did not crash.")
11999451b44SJordan Rupprecht
12099451b44SJordan Rupprecht        # Then lldb should exit.
12199451b44SJordan Rupprecht        child.expect_exact("exited")
12299451b44SJordan Rupprecht        import pexpect
1232238dcc3SJonas Devlieghere
12499451b44SJordan Rupprecht        child.expect(pexpect.EOF)
12599451b44SJordan Rupprecht
12699451b44SJordan Rupprecht    def closeVictim(self):
12799451b44SJordan Rupprecht        if self.victim is not None:
12899451b44SJordan Rupprecht            self.victim.close()
12999451b44SJordan Rupprecht            self.victim = None
13099451b44SJordan Rupprecht
131*1b70580dSAdrian Prantl    @skipIf(macos_version=["<", "14.0"], asan=True)
1328813bc02SMuhammad Omair Javaid    @skipIf(oslist=["linux"], archs=["arm", "aarch64"])  # Randomly fails on buildbot
13399451b44SJordan Rupprecht    @expectedFlakeyFreeBSD("llvm.org/pr25172 fails rarely on the buildbot")
13499451b44SJordan Rupprecht    @expectedFailureNetBSD
13599451b44SJordan Rupprecht    def test_batch_mode_attach_exit(self):
13699451b44SJordan Rupprecht        """Test that the lldb driver's batch mode works correctly."""
13799451b44SJordan Rupprecht        self.build()
13899451b44SJordan Rupprecht        self.setTearDownCleanup()
13999451b44SJordan Rupprecht
14099451b44SJordan Rupprecht        exe = self.getBuildArtifact("a.out")
14199451b44SJordan Rupprecht
14299451b44SJordan Rupprecht        # Start up the process by hand, attach to it, and wait for its completion.
14399451b44SJordan Rupprecht        # Attach is funny, since it looks like it stops with a signal on most Unixen so
14499451b44SJordan Rupprecht        # care must be taken not to treat that as a reason to exit batch mode.
14599451b44SJordan Rupprecht
14699451b44SJordan Rupprecht        # Start up the process by hand and wait for it to get to the wait loop.
14799451b44SJordan Rupprecht        import pexpect
1482238dcc3SJonas Devlieghere
1492238dcc3SJonas Devlieghere        self.victim = pexpect.spawn("%s WAIT" % (exe))
15099451b44SJordan Rupprecht        if self.victim is None:
15199451b44SJordan Rupprecht            self.fail("Could not spawn ", exe, ".")
15299451b44SJordan Rupprecht
15399451b44SJordan Rupprecht        self.addTearDownHook(self.closeVictim)
15499451b44SJordan Rupprecht
15599451b44SJordan Rupprecht        self.victim.expect("PID: ([0-9]+) END")
15699451b44SJordan Rupprecht        victim_pid = int(self.victim.match.group(1))
15799451b44SJordan Rupprecht
15899451b44SJordan Rupprecht        self.victim.expect("Waiting")
15999451b44SJordan Rupprecht
16099451b44SJordan Rupprecht        extra_args = [
1612238dcc3SJonas Devlieghere            "-b",
1622238dcc3SJonas Devlieghere            "-o",
1632238dcc3SJonas Devlieghere            "process attach -p %d" % victim_pid,
1642238dcc3SJonas Devlieghere            "-o",
1652238dcc3SJonas Devlieghere            "breakpoint set --file '%s' -p 'Stop here to unset keep_waiting' -N keep_waiting"
1662238dcc3SJonas Devlieghere            % self.source,
1672238dcc3SJonas Devlieghere            "-o",
1682238dcc3SJonas Devlieghere            "continue",
1692238dcc3SJonas Devlieghere            "-o",
1702238dcc3SJonas Devlieghere            "break delete keep_waiting",
1712238dcc3SJonas Devlieghere            "-o",
1722238dcc3SJonas Devlieghere            "expr keep_waiting = 0",
1732238dcc3SJonas Devlieghere            "-o",
1742238dcc3SJonas Devlieghere            "continue",
17599451b44SJordan Rupprecht        ]
17699451b44SJordan Rupprecht        self.launch(executable=exe, extra_args=extra_args)
17799451b44SJordan Rupprecht        child = self.child
17899451b44SJordan Rupprecht
17999451b44SJordan Rupprecht        child.expect_exact("attach")
18099451b44SJordan Rupprecht
18199451b44SJordan Rupprecht        child.expect_exact(self.PROMPT + "continue")
18299451b44SJordan Rupprecht
18399451b44SJordan Rupprecht        child.expect_exact(self.PROMPT + "continue")
18499451b44SJordan Rupprecht
18599451b44SJordan Rupprecht        # Then we should see the process exit:
18699451b44SJordan Rupprecht        child.expect_exact("Process %d exited with status" % (victim_pid))
18799451b44SJordan Rupprecht
18899451b44SJordan Rupprecht        self.victim.expect(pexpect.EOF)
18999451b44SJordan Rupprecht        child.expect(pexpect.EOF)
190