1"""
2Test the use of setjmp/longjmp for non-local goto operations in a single-threaded inferior.
3"""
4
5
6
7import lldb
8from lldbsuite.test.decorators import *
9from lldbsuite.test.lldbtest import *
10from lldbsuite.test import lldbutil
11
12
13class LongjmpTestCase(TestBase):
14
15    mydir = TestBase.compute_mydir(__file__)
16
17    @skipIfDarwin  # llvm.org/pr16769: LLDB on Mac OS X dies in function ReadRegisterBytes in GDBRemoteRegisterContext.cpp
18    @skipIfFreeBSD  # llvm.org/pr17214
19    @expectedFailureAll(oslist=["linux"], bugnumber="llvm.org/pr20231")
20    @expectedFailureAll(oslist=["windows"], bugnumber="llvm.org/pr24778")
21    @expectedFlakeyNetBSD
22    def test_step_out(self):
23        """Test stepping when the inferior calls setjmp/longjmp, in particular, thread step-out."""
24        self.build()
25        self.step_out()
26
27    @skipIfDarwin  # llvm.org/pr16769: LLDB on Mac OS X dies in function ReadRegisterBytes in GDBRemoteRegisterContext.cpp
28    @skipIfFreeBSD  # llvm.org/pr17214
29    @expectedFailureAll(oslist=["linux"], bugnumber="llvm.org/pr20231")
30    @expectedFailureAll(oslist=["windows"], bugnumber="llvm.org/pr24778")
31    @skipIfNetBSD
32    def test_step_over(self):
33        """Test stepping when the inferior calls setjmp/longjmp, in particular, thread step-over a longjmp."""
34        self.build()
35        self.step_over()
36
37    @skipIfDarwin  # llvm.org/pr16769: LLDB on Mac OS X dies in function ReadRegisterBytes in GDBRemoteRegisterContext.cpp
38    @skipIfFreeBSD  # llvm.org/pr17214
39    @expectedFailureAll(oslist=["linux"], bugnumber="llvm.org/pr20231")
40    @expectedFailureAll(oslist=["windows"], bugnumber="llvm.org/pr24778")
41    @expectedFlakeyNetBSD
42    def test_step_back_out(self):
43        """Test stepping when the inferior calls setjmp/longjmp, in particular, thread step-out after thread step-in."""
44        self.build()
45        self.step_back_out()
46
47    def start_test(self, symbol):
48        exe = self.getBuildArtifact("a.out")
49
50        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
51
52        # Break in main().
53        lldbutil.run_break_set_by_symbol(
54            self, symbol, num_expected_locations=-1)
55
56        self.runCmd("run", RUN_SUCCEEDED)
57
58        # The stop reason of the thread should be breakpoint.
59        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
60                    substrs=['stopped', 'stop reason = breakpoint'])
61
62    def check_status(self):
63        # Note: Depending on the generated mapping of DWARF to assembly,
64        # the process may have stopped or exited.
65        self.expect("process status", PROCESS_STOPPED,
66                    patterns=['Process .* exited with status = 0'])
67
68    def step_out(self):
69        self.start_test("do_jump")
70        self.runCmd("thread step-out", RUN_SUCCEEDED)
71        self.check_status()
72
73    def step_over(self):
74        self.start_test("do_jump")
75        self.runCmd("thread step-over", RUN_SUCCEEDED)
76        self.runCmd("thread step-over", RUN_SUCCEEDED)
77        self.check_status()
78
79    def step_back_out(self):
80        self.start_test("main")
81
82        self.runCmd("thread step-over", RUN_SUCCEEDED)
83        self.runCmd("thread step-in", RUN_SUCCEEDED)
84        self.runCmd("thread step-out", RUN_SUCCEEDED)
85        self.check_status()
86