xref: /llvm-project/lldb/test/API/driver/batch_mode/TestBatchMode.py (revision 1b70580dd867195b0442e582eccd42abc41ee12d)
1"""
2Test that the lldb driver's batch mode works correctly.
3"""
4
5
6import lldb
7from lldbsuite.test.decorators import *
8from lldbsuite.test.lldbtest import *
9from lldbsuite.test import lldbutil
10from lldbsuite.test.lldbpexpect import PExpectTest
11
12
13class DriverBatchModeTest(PExpectTest):
14    source = "main.c"
15
16    @skipIf(macos_version=["<", "14.0"], asan=True)
17    @skipIf(oslist=["linux"], archs=["arm", "aarch64"])  # Randomly fails on buildbot
18    @expectedFlakeyFreeBSD("llvm.org/pr25172 fails rarely on the buildbot")
19    def test_batch_mode_run_crash(self):
20        """Test that the lldb driver's batch mode works correctly."""
21        self.build()
22
23        exe = self.getBuildArtifact("a.out")
24
25        # Pass CRASH so the process will crash and stop in batch mode.
26        extra_args = [
27            "-b",
28            "-o",
29            "break set -n main",
30            "-o",
31            "run",
32            "-o",
33            "continue",
34            "-k",
35            "frame var touch_me_not",
36            "--",
37            "CRASH",
38        ]
39        self.launch(executable=exe, extra_args=extra_args)
40        child = self.child
41
42        # We should see the "run":
43        child.expect_exact("run")
44        # We should have hit the breakpoint & continued:
45        child.expect_exact("continue")
46        # The App should have crashed:
47        child.expect_exact("About to crash")
48        # The -k option should have printed the frame variable once:
49        child.expect_exact("(char *) touch_me_not")
50        # Then we should have a live prompt:
51        self.expect_prompt()
52        self.expect("frame variable touch_me_not", substrs=["(char *) touch_me_not"])
53
54    @skipIf(macos_version=["<", "14.0"], asan=True)
55    @skipIf(oslist=["linux"], archs=["arm", "aarch64"])  # Randomly fails on buildbot
56    @expectedFlakeyFreeBSD("llvm.org/pr25172 fails rarely on the buildbot")
57    def test_batch_mode_run_exit(self):
58        """Test that the lldb driver's batch mode works correctly."""
59        self.build()
60
61        exe = self.getBuildArtifact("a.out")
62
63        # Now do it again, and make sure if we don't crash, we quit:
64        extra_args = [
65            "-b",
66            "-o",
67            "break set -n main",
68            "-o",
69            "run",
70            "-o",
71            "continue",
72            "--",
73            "NOCRASH",
74        ]
75        self.launch(executable=exe, extra_args=extra_args)
76        child = self.child
77
78        # We should see the "run":
79        child.expect_exact("run")
80        # We should have hit the breakpoint & continued:
81        child.expect_exact("continue")
82        # The App should have not have crashed:
83        child.expect_exact("Got there on time and it did not crash.")
84
85        # Then lldb should exit.
86        child.expect_exact("exited")
87        import pexpect
88
89        child.expect(pexpect.EOF)
90
91    @skipIf(macos_version=["<", "14.0"], asan=True)
92    @skipIf(oslist=["linux"], archs=["arm", "aarch64"])  # Randomly fails on buildbot
93    @expectedFlakeyFreeBSD("llvm.org/pr25172 fails rarely on the buildbot")
94    def test_batch_mode_launch_stop_at_entry(self):
95        """Test that the lldb driver's batch mode works correctly for process launch."""
96        self.build()
97
98        exe = self.getBuildArtifact("a.out")
99
100        # Launch with the option '--stop-at-entry' stops with a signal (usually SIGSTOP)
101        # that should be suppressed since it doesn't imply a crash and
102        # this is not a reason to exit batch mode.
103        extra_args = [
104            "-b",
105            "-o",
106            "process launch --stop-at-entry",
107            "-o",
108            "continue",
109        ]
110        self.launch(executable=exe, extra_args=extra_args)
111        child = self.child
112
113        # Check that the process has been launched:
114        child.expect("Process ([0-9]+) launched:")
115        # We should have continued:
116        child.expect_exact("continue")
117        # The App should have not have crashed:
118        child.expect_exact("Got there on time and it did not crash.")
119
120        # Then lldb should exit.
121        child.expect_exact("exited")
122        import pexpect
123
124        child.expect(pexpect.EOF)
125
126    def closeVictim(self):
127        if self.victim is not None:
128            self.victim.close()
129            self.victim = None
130
131    @skipIf(macos_version=["<", "14.0"], asan=True)
132    @skipIf(oslist=["linux"], archs=["arm", "aarch64"])  # Randomly fails on buildbot
133    @expectedFlakeyFreeBSD("llvm.org/pr25172 fails rarely on the buildbot")
134    @expectedFailureNetBSD
135    def test_batch_mode_attach_exit(self):
136        """Test that the lldb driver's batch mode works correctly."""
137        self.build()
138        self.setTearDownCleanup()
139
140        exe = self.getBuildArtifact("a.out")
141
142        # Start up the process by hand, attach to it, and wait for its completion.
143        # Attach is funny, since it looks like it stops with a signal on most Unixen so
144        # care must be taken not to treat that as a reason to exit batch mode.
145
146        # Start up the process by hand and wait for it to get to the wait loop.
147        import pexpect
148
149        self.victim = pexpect.spawn("%s WAIT" % (exe))
150        if self.victim is None:
151            self.fail("Could not spawn ", exe, ".")
152
153        self.addTearDownHook(self.closeVictim)
154
155        self.victim.expect("PID: ([0-9]+) END")
156        victim_pid = int(self.victim.match.group(1))
157
158        self.victim.expect("Waiting")
159
160        extra_args = [
161            "-b",
162            "-o",
163            "process attach -p %d" % victim_pid,
164            "-o",
165            "breakpoint set --file '%s' -p 'Stop here to unset keep_waiting' -N keep_waiting"
166            % self.source,
167            "-o",
168            "continue",
169            "-o",
170            "break delete keep_waiting",
171            "-o",
172            "expr keep_waiting = 0",
173            "-o",
174            "continue",
175        ]
176        self.launch(executable=exe, extra_args=extra_args)
177        child = self.child
178
179        child.expect_exact("attach")
180
181        child.expect_exact(self.PROMPT + "continue")
182
183        child.expect_exact(self.PROMPT + "continue")
184
185        # Then we should see the process exit:
186        child.expect_exact("Process %d exited with status" % (victim_pid))
187
188        self.victim.expect(pexpect.EOF)
189        child.expect(pexpect.EOF)
190