1"""Test that lldb steps correctly after the inferior has crashed."""
2
3
4import lldb
5from lldbsuite.test import lldbutil
6from lldbsuite.test import lldbplatformutil
7from lldbsuite.test.decorators import *
8from lldbsuite.test.lldbtest import *
9
10
11class CrashingInferiorStepTestCase(TestBase):
12
13    mydir = TestBase.compute_mydir(__file__)
14
15    @expectedFailureAll(oslist=["windows"], bugnumber="llvm.org/pr24778")
16    @expectedFailureNetBSD
17    def test_inferior_crashing(self):
18        """Test that lldb reliably catches the inferior crashing (command)."""
19        self.build()
20        self.inferior_crashing()
21
22    @expectedFailureAll(oslist=["windows"], bugnumber="llvm.org/pr24778")
23    def test_inferior_crashing_register(self):
24        """Test that lldb reliably reads registers from the inferior after crashing (command)."""
25        self.build()
26        self.inferior_crashing_registers()
27
28    @add_test_categories(['pyapi'])
29    def test_inferior_crashing_python(self):
30        """Test that lldb reliably catches the inferior crashing (Python API)."""
31        self.build()
32        self.inferior_crashing_python()
33
34    def test_inferior_crashing_expr(self):
35        """Test that the lldb expression interpreter can read from the inferior after crashing (command)."""
36        self.build()
37        self.inferior_crashing_expr()
38
39    @expectedFailureAll(oslist=["windows"], bugnumber="llvm.org/pr24778")
40    def test_inferior_crashing_step(self):
41        """Test that stepping after a crash behaves correctly."""
42        self.build()
43        self.inferior_crashing_step()
44
45    @skipIfTargetAndroid()  # debuggerd interferes with this test on Android
46    @expectedFailureAll(oslist=["windows"], bugnumber="llvm.org/pr24778")
47    def test_inferior_crashing_step_after_break(self):
48        """Test that lldb functions correctly after stepping through a crash."""
49        self.build()
50        self.inferior_crashing_step_after_break()
51
52    # Inferior exits after stepping after a segfault. This is working as
53    # intended IMHO.
54    @skipIf(oslist=["freebsd", "linux", "netbsd"])
55    def test_inferior_crashing_expr_step_and_expr(self):
56        """Test that lldb expressions work before and after stepping after a crash."""
57        self.build()
58        self.inferior_crashing_expr_step_expr()
59
60    def set_breakpoint(self, line):
61        lldbutil.run_break_set_by_file_and_line(
62            self, "main.c", line, num_expected_locations=1, loc_exact=True)
63
64    def check_stop_reason(self):
65        # We should have one crashing thread
66        self.assertEqual(
67            len(
68                lldbutil.get_crashed_threads(
69                    self,
70                    self.dbg.GetSelectedTarget().GetProcess())), 1,
71            STOPPED_DUE_TO_EXC_BAD_ACCESS)
72
73    def get_api_stop_reason(self):
74        return lldb.eStopReasonException
75
76    def setUp(self):
77        # Call super's setUp().
78        TestBase.setUp(self)
79        # Find the line number of the crash.
80        self.line = line_number('main.c', '// Crash here.')
81
82    def inferior_crashing(self):
83        """Inferior crashes upon launching; lldb should catch the event and stop."""
84        exe = self.getBuildArtifact("a.out")
85        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
86
87        self.runCmd("run", RUN_SUCCEEDED)
88        # The exact stop reason depends on the platform
89        if self.platformIsDarwin():
90            stop_reason = 'stop reason = EXC_BAD_ACCESS'
91        elif self.getPlatform() == "linux" or self.getPlatform() == "freebsd":
92            stop_reason = 'stop reason = signal SIGSEGV'
93        else:
94            stop_reason = 'stop reason = invalid address'
95        self.expect(
96            "thread list",
97            STOPPED_DUE_TO_EXC_BAD_ACCESS,
98            substrs=['stopped', stop_reason])
99
100        # And it should report the correct line number.
101        self.expect(
102            "thread backtrace all",
103            substrs=[stop_reason, 'main.c:%d' % self.line])
104
105    def inferior_crashing_python(self):
106        """Inferior crashes upon launching; lldb should catch the event and stop."""
107        exe = self.getBuildArtifact("a.out")
108
109        target = self.dbg.CreateTarget(exe)
110        self.assertTrue(target, VALID_TARGET)
111
112        # Now launch the process, and do not stop at entry point.
113        # Both argv and envp are null.
114        process = target.LaunchSimple(None, None,
115                                      self.get_process_working_directory())
116
117        if process.GetState() != lldb.eStateStopped:
118            self.fail("Process should be in the 'stopped' state, "
119                      "instead the actual state is: '%s'" %
120                      lldbutil.state_type_to_str(process.GetState()))
121
122        threads = lldbutil.get_crashed_threads(self, process)
123        self.assertEqual(
124            len(threads), 1,
125            "Failed to stop the thread upon bad access exception")
126
127        if self.TraceOn():
128            lldbutil.print_stacktrace(threads[0])
129
130    def inferior_crashing_registers(self):
131        """Test that lldb can read registers after crashing."""
132        exe = self.getBuildArtifact("a.out")
133        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
134
135        self.runCmd("run", RUN_SUCCEEDED)
136        self.check_stop_reason()
137
138        # lldb should be able to read from registers from the inferior after
139        # crashing.
140        lldbplatformutil.check_first_register_readable(self)
141
142    def inferior_crashing_expr(self):
143        """Test that the lldb expression interpreter can read symbols after crashing."""
144        exe = self.getBuildArtifact("a.out")
145        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
146
147        self.runCmd("run", RUN_SUCCEEDED)
148        self.check_stop_reason()
149
150        # The lldb expression interpreter should be able to read from addresses
151        # of the inferior after a crash.
152        self.expect("p argc", startstr='(int) $0 = 1')
153
154        self.expect("p hello_world", substrs=['Hello'])
155
156    def inferior_crashing_step(self):
157        """Test that lldb functions correctly after stepping through a crash."""
158        exe = self.getBuildArtifact("a.out")
159        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
160
161        self.set_breakpoint(self.line)
162        self.runCmd("run", RUN_SUCCEEDED)
163
164        self.expect(
165            "thread list",
166            STOPPED_DUE_TO_BREAKPOINT,
167            substrs=['main.c:%d' % self.line, 'stop reason = breakpoint'])
168
169        self.runCmd("next")
170        self.check_stop_reason()
171
172        # The lldb expression interpreter should be able to read from addresses
173        # of the inferior after a crash.
174        self.expect("p argv[0]", substrs=['a.out'])
175        self.expect("p null_ptr", substrs=['= 0x0'])
176
177        # lldb should be able to read from registers from the inferior after
178        # crashing.
179        lldbplatformutil.check_first_register_readable(self)
180
181        # And it should report the correct line number.
182        self.expect("thread backtrace all", substrs=['main.c:%d' % self.line])
183
184    @expectedFailureNetBSD
185    def inferior_crashing_step_after_break(self):
186        """Test that lldb behaves correctly when stepping after a crash."""
187        exe = self.getBuildArtifact("a.out")
188        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
189
190        self.runCmd("run", RUN_SUCCEEDED)
191        self.check_stop_reason()
192
193        expected_state = 'exited'  # Provide the exit code.
194        if self.platformIsDarwin():
195            # TODO: Determine why 'next' and 'continue' have no effect after a
196            # crash.
197            expected_state = 'stopped'
198
199        self.expect("next", substrs=['Process', expected_state])
200
201        if expected_state == 'exited':
202            self.expect(
203                "thread list",
204                error=True,
205                substrs=['Process must be launched'])
206        else:
207            self.check_stop_reason()
208
209    def inferior_crashing_expr_step_expr(self):
210        """Test that lldb expressions work before and after stepping after a crash."""
211        exe = self.getBuildArtifact("a.out")
212        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
213
214        self.runCmd("run", RUN_SUCCEEDED)
215        self.check_stop_reason()
216
217        # The lldb expression interpreter should be able to read from addresses
218        # of the inferior after a crash.
219        self.expect("p argv[0]", substrs=['a.out'])
220
221        self.runCmd("next")
222        self.check_stop_reason()
223
224        # The lldb expression interpreter should be able to read from addresses
225        # of the inferior after a crash.
226        self.expect("p argv[0]", substrs=['a.out'])
227