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