1"""Test that we can unwind out of a signal handler.
2   Which for AArch64 Linux requires a specific unwind plan."""
3
4
5import lldb
6from lldbsuite.test.decorators import *
7from lldbsuite.test.lldbtest import *
8from lldbsuite.test import lldbutil
9
10
11class UnwindSignalTestCase(TestBase):
12
13    mydir = TestBase.compute_mydir(__file__)
14
15    NO_DEBUG_INFO_TESTCASE = True
16
17    @skipUnlessArch("aarch64")
18    @skipUnlessPlatform(["linux"])
19    def test_unwind_signal(self):
20        """Inferior calls sigill() and handles the resultant SIGILL.
21           Stopped at a breakpoint in the handler, check that we can unwind
22           back to sigill() and get the expected register contents there."""
23        self.build()
24        exe = self.getBuildArtifact("a.out")
25
26        target = self.dbg.CreateTarget(exe)
27        self.assertTrue(target, VALID_TARGET)
28
29        process = target.LaunchSimple(
30            None, None, self.get_process_working_directory())
31        self.assertTrue(process, PROCESS_IS_VALID)
32        self.assertState(process.GetState(), lldb.eStateStopped)
33        signo = process.GetUnixSignals().GetSignalNumberFromName("SIGILL")
34
35        thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonSignal)
36        self.assertTrue(
37            thread and thread.IsValid(),
38            "Thread should be stopped due to a signal")
39        self.assertTrue(
40            thread.GetStopReasonDataCount() >= 1,
41            "There should be data in the event.")
42        self.assertEqual(thread.GetStopReasonDataAtIndex(0),
43                         signo, "The stop signal should be SIGILL")
44
45        # Continue to breakpoint in sigill handler
46        bkpt = target.FindBreakpointByID(
47            lldbutil.run_break_set_by_source_regexp(self, "Set a breakpoint here"))
48        threads = lldbutil.continue_to_breakpoint(process, bkpt)
49        self.assertEqual(len(threads), 1, "Expected single thread")
50        thread = threads[0]
51
52        # Expect breakpoint in 'handler'
53        frame = thread.GetFrameAtIndex(0)
54        self.assertEqual(frame.GetDisplayFunctionName(), "handler", "Unexpected break?")
55
56        # Expect that unwinding should find 'sigill'
57        found_caller = False
58        for frame in thread.get_thread_frames():
59            if frame.GetDisplayFunctionName() == "sigill":
60                # We should have ascending values in the x registers
61                regs = frame.GetRegisters().GetValueAtIndex(0)
62                err = lldb.SBError()
63
64                for i in range(31):
65                  name = 'x{}'.format(i)
66                  value = regs.GetChildMemberWithName(name).GetValueAsUnsigned(err)
67                  self.assertSuccess(err, "Failed to get register {}".format(name))
68                  self.assertEqual(value, i, "Unexpected value for register {}".format(
69                                      name))
70
71                found_caller = True
72                break
73
74        self.assertTrue(found_caller, "Unwinding did not find func that caused the SIGILL")
75
76        # Continue until we exit.
77        process.Continue()
78        self.assertEqual(process.GetState(), lldb.eStateExited)
79        self.assertEqual(process.GetExitStatus(), 0)
80