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