1"""
2Test that we can backtrace correctly with 'sigtramp' functions on the stack
3"""
4
5from __future__ import print_function
6
7
8import lldb
9from lldbsuite.test.decorators import *
10from lldbsuite.test.lldbtest import *
11from lldbsuite.test import lldbutil
12
13
14class SigtrampUnwind(TestBase):
15    mydir = TestBase.compute_mydir(__file__)
16
17    # On different platforms the "_sigtramp" and "__kill" frames are likely to be different.
18    # This test could probably be adapted to run on linux/*bsd easily enough.
19    @skipUnlessDarwin
20    @expectedFailureAll(oslist=["ios", "watchos", "tvos", "bridgeos"], bugnumber="<rdar://problem/34006863>")  # lldb skips 1 frame on arm64 above _sigtramp
21    def test(self):
22        """Test that we can backtrace correctly with _sigtramp on the stack"""
23        self.build()
24        self.setTearDownCleanup()
25
26        exe = self.getBuildArtifact("a.out")
27        target = self.dbg.CreateTarget(exe)
28        self.assertTrue(target, VALID_TARGET)
29
30        lldbutil.run_break_set_by_file_and_line(self, "main.c", line_number(
31            'main.c', '// Set breakpoint here'), num_expected_locations=1)
32
33        process = target.LaunchSimple(
34            None, None, self.get_process_working_directory())
35
36        if not process:
37            self.fail("SBTarget.Launch() failed")
38
39        if process.GetState() != lldb.eStateStopped:
40            self.fail("Process should be in the 'stopped' state, "
41                      "instead the actual state is: '%s'" %
42                      lldbutil.state_type_to_str(process.GetState()))
43
44        self.expect(
45            "pro handle  -n false -p true -s false SIGUSR1",
46            "Have lldb pass SIGUSR1 signals",
47            substrs=[
48                "SIGUSR1",
49                "true",
50                "false",
51                "false"])
52
53        lldbutil.run_break_set_by_symbol(
54            self,
55            "handler",
56            num_expected_locations=1,
57            module_name="a.out")
58
59        self.runCmd("continue")
60
61        thread = process.GetThreadAtIndex(0)
62
63        found_handler = False
64        found_sigtramp = False
65        found_kill = False
66        found_main = False
67
68        for f in thread.frames:
69            if f.GetFunctionName() == "handler":
70                found_handler = True
71            if f.GetFunctionName() == "_sigtramp":
72                found_sigtramp = True
73            if f.GetFunctionName() == "__kill":
74                found_kill = True
75            if f.GetFunctionName() == "main":
76                found_main = True
77
78        if self.TraceOn():
79            print("Backtrace once we're stopped:")
80            for f in thread.frames:
81                print("  %d %s" % (f.GetFrameID(), f.GetFunctionName()))
82
83        if not found_handler:
84            self.fail("Unable to find handler() in backtrace.")
85
86        if not found_sigtramp:
87            self.fail("Unable to find _sigtramp() in backtrace.")
88
89        if not found_kill:
90            self.fail("Unable to find kill() in backtrace.")
91
92        if not found_main:
93            self.fail("Unable to find main() in backtrace.")
94