• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
16    mydir = TestBase.compute_mydir(__file__)
17    source = 'main.c'
18
19    @expectedFlakeyFreeBSD("llvm.org/pr25172 fails rarely on the buildbot")
20    def test_batch_mode_run_crash(self):
21        """Test that the lldb driver's batch mode works correctly."""
22        self.build()
23
24        exe = self.getBuildArtifact("a.out")
25
26        # Pass CRASH so the process will crash and stop in batch mode.
27        extra_args = ['-b',
28            '-o', 'break set -n main',
29            '-o', 'run',
30            '-o', 'continue',
31            '-k', 'frame var touch_me_not',
32            '--', 'CRASH',
33        ]
34        self.launch(executable=exe, extra_args=extra_args)
35        child = self.child
36
37        # We should see the "run":
38        child.expect_exact("run")
39        # We should have hit the breakpoint & continued:
40        child.expect_exact("continue")
41        # The App should have crashed:
42        child.expect_exact("About to crash")
43        # The -k option should have printed the frame variable once:
44        child.expect_exact('(char *) touch_me_not')
45        # Then we should have a live prompt:
46        self.expect_prompt()
47        self.expect("frame variable touch_me_not", substrs=['(char *) touch_me_not'])
48
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    @expectedFlakeyFreeBSD("llvm.org/pr25172 fails rarely on the buildbot")
79    def test_batch_mode_launch_stop_at_entry(self):
80        """Test that the lldb driver's batch mode works correctly for process launch."""
81        self.build()
82
83        exe = self.getBuildArtifact("a.out")
84
85        # Launch with the option '--stop-at-entry' stops with a signal (usually SIGSTOP)
86        # that should be suppressed since it doesn't imply a crash and
87        # this is not a reason to exit batch mode.
88        extra_args = ['-b',
89            '-o', 'process launch --stop-at-entry',
90            '-o', 'continue',
91        ]
92        self.launch(executable=exe, extra_args=extra_args)
93        child = self.child
94
95        # Check that the process has been launched:
96        child.expect("Process ([0-9]+) launched:")
97        # We should have continued:
98        child.expect_exact("continue")
99        # The App should have not have crashed:
100        child.expect_exact("Got there on time and it did not crash.")
101
102        # Then lldb should exit.
103        child.expect_exact("exited")
104        import pexpect
105        child.expect(pexpect.EOF)
106
107    def closeVictim(self):
108        if self.victim is not None:
109            self.victim.close()
110            self.victim = None
111
112    @expectedFlakeyFreeBSD("llvm.org/pr25172 fails rarely on the buildbot")
113    @expectedFailureNetBSD
114    def test_batch_mode_attach_exit(self):
115        """Test that the lldb driver's batch mode works correctly."""
116        self.build()
117        self.setTearDownCleanup()
118
119        exe = self.getBuildArtifact("a.out")
120
121        # Start up the process by hand, attach to it, and wait for its completion.
122        # Attach is funny, since it looks like it stops with a signal on most Unixen so
123        # care must be taken not to treat that as a reason to exit batch mode.
124
125        # Start up the process by hand and wait for it to get to the wait loop.
126        import pexpect
127        self.victim = pexpect.spawn('%s WAIT' % (exe))
128        if self.victim is None:
129            self.fail("Could not spawn ", exe, ".")
130
131        self.addTearDownHook(self.closeVictim)
132
133        self.victim.expect("PID: ([0-9]+) END")
134        victim_pid = int(self.victim.match.group(1))
135
136        self.victim.expect("Waiting")
137
138        extra_args = [
139            '-b',
140            '-o', 'process attach -p %d'%victim_pid,
141            '-o', "breakpoint set --file '%s' -p 'Stop here to unset keep_waiting' -N keep_waiting"%self.source,
142            '-o', 'continue',
143            '-o', 'break delete keep_waiting',
144            '-o', 'expr keep_waiting = 0',
145            '-o', 'continue',
146        ]
147        self.launch(executable=exe, extra_args=extra_args)
148        child = self.child
149
150        child.expect_exact("attach")
151
152        child.expect_exact(self.PROMPT + "continue")
153
154        child.expect_exact(self.PROMPT + "continue")
155
156        # Then we should see the process exit:
157        child.expect_exact("Process %d exited with status" % (victim_pid))
158
159        self.victim.expect(pexpect.EOF)
160        child.expect(pexpect.EOF)
161