1"""Test that lldb functions correctly after the inferior has crashed."""
2
3
4
5import lldb
6from lldbsuite.test import lldbutil
7from lldbsuite.test import lldbplatformutil
8from lldbsuite.test.decorators import *
9from lldbsuite.test.lldbtest import *
10
11
12class CrashingInferiorTestCase(TestBase):
13
14    mydir = TestBase.compute_mydir(__file__)
15
16    @expectedFailureAll(oslist=["windows"], bugnumber="llvm.org/pr24778")
17    @expectedFailureNetBSD
18    def test_inferior_crashing(self):
19        """Test that lldb reliably catches the inferior crashing (command)."""
20        self.build()
21        self.inferior_crashing()
22
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    def set_breakpoint(self, line):
40        lldbutil.run_break_set_by_file_and_line(
41            self, "main.c", line, num_expected_locations=1, loc_exact=True)
42
43    def check_stop_reason(self):
44        # We should have one crashing thread
45        self.assertEqual(
46            len(lldbutil.get_crashed_threads(self, self.dbg.GetSelectedTarget().GetProcess())),
47            1,
48            STOPPED_DUE_TO_EXC_BAD_ACCESS)
49
50    def get_api_stop_reason(self):
51        return lldb.eStopReasonException
52
53    def setUp(self):
54        # Call super's setUp().
55        TestBase.setUp(self)
56        # Find the line number of the crash.
57        self.line = line_number('main.c', '// Crash here.')
58
59    def inferior_crashing(self):
60        """Inferior crashes upon launching; lldb should catch the event and stop."""
61        exe = self.getBuildArtifact("a.out")
62        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
63
64        self.runCmd("run", RUN_SUCCEEDED)
65        # The exact stop reason depends on the platform
66        if self.platformIsDarwin():
67            stop_reason = 'stop reason = EXC_BAD_ACCESS'
68        elif self.getPlatform() == "linux" or self.getPlatform() == "freebsd":
69            stop_reason = 'stop reason = signal SIGSEGV'
70        else:
71            stop_reason = 'stop reason = invalid address'
72        self.expect("thread list", STOPPED_DUE_TO_EXC_BAD_ACCESS,
73                    substrs=['stopped',
74                             stop_reason])
75
76        # And it should report the correct line number.
77        self.expect("thread backtrace all",
78                    substrs=[stop_reason,
79                             'main.c:%d' % self.line])
80
81    def inferior_crashing_python(self):
82        """Inferior crashes upon launching; lldb should catch the event and stop."""
83        exe = self.getBuildArtifact("a.out")
84
85        target = self.dbg.CreateTarget(exe)
86        self.assertTrue(target, VALID_TARGET)
87
88        # Now launch the process, and do not stop at entry point.
89        # Both argv and envp are null.
90        process = target.LaunchSimple(
91            None, None, self.get_process_working_directory())
92
93        if process.GetState() != lldb.eStateStopped:
94            self.fail("Process should be in the 'stopped' state, "
95                      "instead the actual state is: '%s'" %
96                      lldbutil.state_type_to_str(process.GetState()))
97
98        threads = lldbutil.get_crashed_threads(self, process)
99        self.assertEqual(
100            len(threads),
101            1,
102            "Failed to stop the thread upon bad access exception")
103
104        if self.TraceOn():
105            lldbutil.print_stacktrace(threads[0])
106
107    def inferior_crashing_registers(self):
108        """Test that lldb can read registers after crashing."""
109        exe = self.getBuildArtifact("a.out")
110        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
111
112        self.runCmd("run", RUN_SUCCEEDED)
113        self.check_stop_reason()
114
115        # lldb should be able to read from registers from the inferior after
116        # crashing.
117        lldbplatformutil.check_first_register_readable(self)
118
119    def inferior_crashing_expr(self):
120        """Test that the lldb expression interpreter can read symbols after crashing."""
121        exe = self.getBuildArtifact("a.out")
122        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
123
124        self.runCmd("run", RUN_SUCCEEDED)
125        self.check_stop_reason()
126
127        # The lldb expression interpreter should be able to read from addresses
128        # of the inferior after a crash.
129        self.expect("p argc",
130                    startstr='(int) $0 = 1')
131
132        self.expect("p hello_world",
133                    substrs=['Hello'])
134