xref: /llvm-project/lldb/test/API/macosx/ignore_exceptions/TestIgnoredExceptions.py (revision f7bf9d13d50d785889952deb18cc93de0176cb96)
1"""
2Test that by turning off EXC_BAD_ACCESS catching, we can
3debug into and out of a signal handler.
4"""
5
6import lldb
7from lldbsuite.test.decorators import *
8import lldbsuite.test.lldbutil as lldbutil
9from lldbsuite.test.lldbtest import *
10
11class TestDarwinSignalHandlers(TestBase):
12
13    NO_DEBUG_INFO_TESTCASE = True
14
15    @skipIfOutOfTreeDebugserver
16    @skipUnlessDarwin
17    def test_ignored_thread(self):
18        """It isn't possible to convert an EXC_BAD_ACCESS to a signal when
19        running under the debugger, which makes debugging SIGBUS handlers
20        and so forth difficult.  This test sends QIgnoreExceptions and that
21        should get us into the signal handler and out again. """
22        self.build()
23        self.main_source_file = lldb.SBFileSpec("main.c")
24        self.suspended_thread_test()
25
26    def suspended_thread_test(self):
27        # Make sure that we don't accept bad values:
28        self.match("settings set platform.plugin.darwin.ignored-exceptions EXC_BAD_AXESS", "EXC_BAD_AXESS", error=True)
29        # Now set ourselves to ignore some exceptions.  The test depends on ignoring EXC_BAD_ACCESS, but I passed a couple
30        # to make sure they parse:
31        self.runCmd("settings set platform.plugin.darwin.ignored-exceptions EXC_BAD_ACCESS|EXC_ARITHMETIC")
32        (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(self,
33                                   "Stop here to get things going", self.main_source_file)
34
35        sig_bkpt = target.BreakpointCreateBySourceRegex("stop here in the signal handler",
36                                                        self.main_source_file)
37        self.assertEqual(sig_bkpt.GetNumLocations(), 1, "Found sig handler breakpoint")
38        return_bkpt = target.BreakpointCreateBySourceRegex("Break here to make sure we got past the signal handler",
39                                                        self.main_source_file)
40        self.assertEqual(return_bkpt.GetNumLocations(), 1, "Found return breakpoint")
41        # Now continue, and we should stop with a stop reason of SIGBUS:
42        process.Continue()
43        self.assertState(process.state, lldb.eStateStopped, "Stopped after continue to SIGBUS")
44        if thread.stop_reason == lldb.eStopReasonBreakpoint:
45            id = thread.GetStopReasonDataAtIndex(0)
46            name = thread.frame[0].name
47            self.fail("Hit breakpoint {0} in '{1}' rather than getting a SIGBUS".format(id, name))
48
49        self.assertEqual(thread.stop_reason, lldb.eStopReasonSignal)
50        self.assertEqual(thread.GetStopReasonDataAtIndex(0), 10, "Got a SIGBUS")
51
52        # Now when we continue, we'll find our way into the signal handler:
53        threads = lldbutil.continue_to_breakpoint(process, sig_bkpt)
54        self.assertEqual(len(threads), 1, "Stopped at sig breakpoint")
55
56        threads = lldbutil.continue_to_breakpoint(process, return_bkpt)
57        self.assertEqual(len(threads), 1, "Stopped at return breakpoint")
58
59        # Make sure we really changed the value:
60
61        process.Continue()
62        self.assertState(process.state, lldb.eStateExited, "Process exited")
63        self.assertEqual(process.exit_state, 20, "Got the right exit status")
64
65