xref: /llvm-project/lldb/test/API/functionalities/thread/exit_during_step/TestExitDuringStep.py (revision 2238dcc39358353cac21df75c3c3286ab20b8f53)
199451b44SJordan Rupprecht"""
299451b44SJordan RupprechtTest number of threads.
399451b44SJordan Rupprecht"""
499451b44SJordan Rupprecht
599451b44SJordan Rupprecht
699451b44SJordan Rupprechtimport lldb
799451b44SJordan Rupprechtfrom lldbsuite.test.decorators import *
899451b44SJordan Rupprechtfrom lldbsuite.test.lldbtest import *
999451b44SJordan Rupprechtfrom lldbsuite.test import lldbutil
1099451b44SJordan Rupprecht
1199451b44SJordan Rupprecht
1299451b44SJordan Rupprechtclass ExitDuringStepTestCase(TestBase):
1399451b44SJordan Rupprecht    @skipIfWindows  # This is flakey on Windows: llvm.org/pr38373
1499451b44SJordan Rupprecht    def test(self):
1599451b44SJordan Rupprecht        """Test thread exit during step handling."""
16d7dbe2c4SPavel Labath        self.build()
1799451b44SJordan Rupprecht        self.exit_during_step_base(
18*2238dcc3SJonas Devlieghere            "thread step-inst -m all-threads", "stop reason = instruction step", True
19*2238dcc3SJonas Devlieghere        )
2099451b44SJordan Rupprecht
2199451b44SJordan Rupprecht    @skipIfWindows  # This is flakey on Windows: llvm.org/pr38373
2299451b44SJordan Rupprecht    def test_step_over(self):
2399451b44SJordan Rupprecht        """Test thread exit during step-over handling."""
24d7dbe2c4SPavel Labath        self.build()
2599451b44SJordan Rupprecht        self.exit_during_step_base(
26*2238dcc3SJonas Devlieghere            "thread step-over -m all-threads", "stop reason = step over", False
27*2238dcc3SJonas Devlieghere        )
2899451b44SJordan Rupprecht
2999451b44SJordan Rupprecht    @skipIfWindows  # This is flakey on Windows: llvm.org/pr38373
3099451b44SJordan Rupprecht    def test_step_in(self):
3199451b44SJordan Rupprecht        """Test thread exit during step-in handling."""
32d7dbe2c4SPavel Labath        self.build()
3399451b44SJordan Rupprecht        self.exit_during_step_base(
34*2238dcc3SJonas Devlieghere            "thread step-in -m all-threads", "stop reason = step in", False
35*2238dcc3SJonas Devlieghere        )
3699451b44SJordan Rupprecht
3799451b44SJordan Rupprecht    def setUp(self):
3899451b44SJordan Rupprecht        # Call super's setUp().
3999451b44SJordan Rupprecht        TestBase.setUp(self)
4099451b44SJordan Rupprecht        # Find the line numbers to break and continue.
41*2238dcc3SJonas Devlieghere        self.breakpoint = line_number("main.cpp", "// Set breakpoint here")
42*2238dcc3SJonas Devlieghere        self.continuepoint = line_number("main.cpp", "// Continue from here")
4399451b44SJordan Rupprecht
4499451b44SJordan Rupprecht    def exit_during_step_base(self, step_cmd, step_stop_reason, by_instruction):
4599451b44SJordan Rupprecht        """Test thread exit during step handling."""
4699451b44SJordan Rupprecht        exe = self.getBuildArtifact("a.out")
4799451b44SJordan Rupprecht        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
4899451b44SJordan Rupprecht
4999451b44SJordan Rupprecht        # This should create a breakpoint in the main thread.
5099451b44SJordan Rupprecht        self.bp_num = lldbutil.run_break_set_by_file_and_line(
51*2238dcc3SJonas Devlieghere            self, "main.cpp", self.breakpoint, num_expected_locations=1
52*2238dcc3SJonas Devlieghere        )
5399451b44SJordan Rupprecht
5499451b44SJordan Rupprecht        # The breakpoint list should show 1 location.
5599451b44SJordan Rupprecht        self.expect(
5699451b44SJordan Rupprecht            "breakpoint list -f",
5799451b44SJordan Rupprecht            "Breakpoint location shown correctly",
5899451b44SJordan Rupprecht            substrs=[
59*2238dcc3SJonas Devlieghere                "1: file = 'main.cpp', line = %d, exact_match = 0, locations = 1"
60*2238dcc3SJonas Devlieghere                % self.breakpoint
61*2238dcc3SJonas Devlieghere            ],
62*2238dcc3SJonas Devlieghere        )
6399451b44SJordan Rupprecht
6499451b44SJordan Rupprecht        # Run the program.
6599451b44SJordan Rupprecht        self.runCmd("run", RUN_SUCCEEDED)
6699451b44SJordan Rupprecht
6799451b44SJordan Rupprecht        # The stop reason of the thread should be breakpoint.
68*2238dcc3SJonas Devlieghere        self.expect(
69*2238dcc3SJonas Devlieghere            "thread list",
70*2238dcc3SJonas Devlieghere            STOPPED_DUE_TO_BREAKPOINT,
71*2238dcc3SJonas Devlieghere            substrs=["stopped", "stop reason = breakpoint"],
72*2238dcc3SJonas Devlieghere        )
7399451b44SJordan Rupprecht
7499451b44SJordan Rupprecht        # Get the target process
7599451b44SJordan Rupprecht        target = self.dbg.GetSelectedTarget()
7699451b44SJordan Rupprecht        process = target.GetProcess()
7799451b44SJordan Rupprecht
7899451b44SJordan Rupprecht        num_threads = process.GetNumThreads()
7999451b44SJordan Rupprecht        # Make sure we see all three threads
8099451b44SJordan Rupprecht        self.assertGreaterEqual(
8199451b44SJordan Rupprecht            num_threads,
8299451b44SJordan Rupprecht            3,
83*2238dcc3SJonas Devlieghere            "Number of expected threads and actual threads do not match.",
84*2238dcc3SJonas Devlieghere        )
8599451b44SJordan Rupprecht
8699451b44SJordan Rupprecht        stepping_thread = lldbutil.get_one_thread_stopped_at_breakpoint_id(
87*2238dcc3SJonas Devlieghere            process, self.bp_num
88*2238dcc3SJonas Devlieghere        )
8999451b44SJordan Rupprecht        self.assertIsNotNone(
90*2238dcc3SJonas Devlieghere            stepping_thread, "Could not find a thread stopped at the breakpoint"
91*2238dcc3SJonas Devlieghere        )
9299451b44SJordan Rupprecht
9399451b44SJordan Rupprecht        current_line = self.breakpoint
9499451b44SJordan Rupprecht        stepping_frame = stepping_thread.GetFrameAtIndex(0)
9599451b44SJordan Rupprecht        self.assertEqual(
9699451b44SJordan Rupprecht            current_line,
9799451b44SJordan Rupprecht            stepping_frame.GetLineEntry().GetLine(),
98*2238dcc3SJonas Devlieghere            "Starting line for stepping doesn't match breakpoint line.",
99*2238dcc3SJonas Devlieghere        )
10099451b44SJordan Rupprecht
10199451b44SJordan Rupprecht        # Keep stepping until we've reached our designated continue point
10299451b44SJordan Rupprecht        while current_line != self.continuepoint:
10399451b44SJordan Rupprecht            # Since we're using the command interpreter to issue the thread command
10499451b44SJordan Rupprecht            # (on the selected thread) we need to ensure the selected thread is the
10599451b44SJordan Rupprecht            # stepping thread.
10699451b44SJordan Rupprecht            if stepping_thread != process.GetSelectedThread():
10799451b44SJordan Rupprecht                process.SetSelectedThread(stepping_thread)
10899451b44SJordan Rupprecht
10999451b44SJordan Rupprecht            self.runCmd(step_cmd)
11099451b44SJordan Rupprecht
11199451b44SJordan Rupprecht            frame = stepping_thread.GetFrameAtIndex(0)
11299451b44SJordan Rupprecht
11399451b44SJordan Rupprecht            current_line = frame.GetLineEntry().GetLine()
11499451b44SJordan Rupprecht
11599451b44SJordan Rupprecht            if by_instruction and current_line == 0:
11699451b44SJordan Rupprecht                continue
11799451b44SJordan Rupprecht
11899451b44SJordan Rupprecht            self.assertGreaterEqual(
11999451b44SJordan Rupprecht                current_line,
12099451b44SJordan Rupprecht                self.breakpoint,
121*2238dcc3SJonas Devlieghere                "Stepped to unexpected line, " + str(current_line),
122*2238dcc3SJonas Devlieghere            )
12399451b44SJordan Rupprecht            self.assertLessEqual(
12499451b44SJordan Rupprecht                current_line,
12599451b44SJordan Rupprecht                self.continuepoint,
126*2238dcc3SJonas Devlieghere                "Stepped to unexpected line, " + str(current_line),
127*2238dcc3SJonas Devlieghere            )
12899451b44SJordan Rupprecht
12999451b44SJordan Rupprecht        self.runCmd("thread list")
13099451b44SJordan Rupprecht
13199451b44SJordan Rupprecht        # Update the number of threads
13299451b44SJordan Rupprecht        new_num_threads = process.GetNumThreads()
13399451b44SJordan Rupprecht
13499451b44SJordan Rupprecht        # Check to see that we reduced the number of threads as expected
13599451b44SJordan Rupprecht        self.assertEqual(
13699451b44SJordan Rupprecht            new_num_threads,
13799451b44SJordan Rupprecht            num_threads - 1,
138*2238dcc3SJonas Devlieghere            "Number of threads did not reduce by 1 after thread exit.",
139*2238dcc3SJonas Devlieghere        )
14099451b44SJordan Rupprecht
141*2238dcc3SJonas Devlieghere        self.expect(
142*2238dcc3SJonas Devlieghere            "thread list",
143*2238dcc3SJonas Devlieghere            "Process state is stopped due to step",
144*2238dcc3SJonas Devlieghere            substrs=["stopped", step_stop_reason],
145*2238dcc3SJonas Devlieghere        )
14699451b44SJordan Rupprecht
14799451b44SJordan Rupprecht        # Run to completion
14899451b44SJordan Rupprecht        self.runCmd("continue")
14999451b44SJordan Rupprecht
15099451b44SJordan Rupprecht        # At this point, the inferior process should have exited.
1511b8c7352SJonas Devlieghere        self.assertState(process.GetState(), lldb.eStateExited, PROCESS_EXITED)
152