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