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