xref: /llvm-project/lldb/test/API/functionalities/thread/thread_exit/TestThreadExit.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 Rupprechtimport lldbsuite.test.lldbutil as lldbutil
1099451b44SJordan Rupprecht
1199451b44SJordan Rupprecht
1299451b44SJordan Rupprechtclass ThreadExitTestCase(TestBase):
1399451b44SJordan Rupprecht    def setUp(self):
1499451b44SJordan Rupprecht        # Call super's setUp().
1599451b44SJordan Rupprecht        TestBase.setUp(self)
1699451b44SJordan Rupprecht        # Find the line numbers for our breakpoints.
17*2238dcc3SJonas Devlieghere        self.break_1 = line_number("main.cpp", "// Set first breakpoint here")
18*2238dcc3SJonas Devlieghere        self.break_2 = line_number("main.cpp", "// Set second breakpoint here")
19*2238dcc3SJonas Devlieghere        self.break_3 = line_number("main.cpp", "// Set third breakpoint here")
20*2238dcc3SJonas Devlieghere        self.break_4 = line_number("main.cpp", "// Set fourth breakpoint here")
2199451b44SJordan Rupprecht
2299451b44SJordan Rupprecht    @skipIfWindows  # This is flakey on Windows: llvm.org/pr38373
2399451b44SJordan Rupprecht    def test(self):
2499451b44SJordan Rupprecht        """Test thread exit handling."""
25d7dbe2c4SPavel Labath        self.build()
2699451b44SJordan Rupprecht        exe = self.getBuildArtifact("a.out")
2799451b44SJordan Rupprecht        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
2899451b44SJordan Rupprecht
2999451b44SJordan Rupprecht        # This should create a breakpoint with 1 location.
3099451b44SJordan Rupprecht        bp1_id = lldbutil.run_break_set_by_file_and_line(
31*2238dcc3SJonas Devlieghere            self, "main.cpp", self.break_1, num_expected_locations=1
32*2238dcc3SJonas Devlieghere        )
3399451b44SJordan Rupprecht        bp2_id = lldbutil.run_break_set_by_file_and_line(
34*2238dcc3SJonas Devlieghere            self, "main.cpp", self.break_2, num_expected_locations=1
35*2238dcc3SJonas Devlieghere        )
3699451b44SJordan Rupprecht        bp3_id = lldbutil.run_break_set_by_file_and_line(
37*2238dcc3SJonas Devlieghere            self, "main.cpp", self.break_3, num_expected_locations=1
38*2238dcc3SJonas Devlieghere        )
3999451b44SJordan Rupprecht        bp4_id = lldbutil.run_break_set_by_file_and_line(
40*2238dcc3SJonas Devlieghere            self, "main.cpp", self.break_4, num_expected_locations=1
41*2238dcc3SJonas Devlieghere        )
4299451b44SJordan Rupprecht
4399451b44SJordan Rupprecht        # The breakpoint list should show 1 locations.
4499451b44SJordan Rupprecht        self.expect(
4599451b44SJordan Rupprecht            "breakpoint list -f",
4699451b44SJordan Rupprecht            "Breakpoint location shown correctly",
4799451b44SJordan Rupprecht            substrs=[
48*2238dcc3SJonas Devlieghere                "1: file = 'main.cpp', line = %d, exact_match = 0, locations = 1"
49*2238dcc3SJonas Devlieghere                % self.break_1,
50*2238dcc3SJonas Devlieghere                "2: file = 'main.cpp', line = %d, exact_match = 0, locations = 1"
51*2238dcc3SJonas Devlieghere                % self.break_2,
52*2238dcc3SJonas Devlieghere                "3: file = 'main.cpp', line = %d, exact_match = 0, locations = 1"
53*2238dcc3SJonas Devlieghere                % self.break_3,
54*2238dcc3SJonas Devlieghere                "4: file = 'main.cpp', line = %d, exact_match = 0, locations = 1"
55*2238dcc3SJonas Devlieghere                % self.break_4,
56*2238dcc3SJonas Devlieghere            ],
57*2238dcc3SJonas Devlieghere        )
5899451b44SJordan Rupprecht
5999451b44SJordan Rupprecht        # Run the program.
6099451b44SJordan Rupprecht        self.runCmd("run", RUN_SUCCEEDED)
6199451b44SJordan Rupprecht        # Get the target process
6299451b44SJordan Rupprecht        target = self.dbg.GetSelectedTarget()
6399451b44SJordan Rupprecht        process = target.GetProcess()
6499451b44SJordan Rupprecht
6599451b44SJordan Rupprecht        stopped_thread = lldbutil.get_one_thread_stopped_at_breakpoint_id(
66*2238dcc3SJonas Devlieghere            process, bp1_id
67*2238dcc3SJonas Devlieghere        )
68*2238dcc3SJonas Devlieghere        self.assertIsNotNone(stopped_thread, "Process is not stopped at breakpoint 1")
6999451b44SJordan Rupprecht
7099451b44SJordan Rupprecht        # Get the number of threads
7199451b44SJordan Rupprecht        num_threads = process.GetNumThreads()
7299451b44SJordan Rupprecht        self.assertGreaterEqual(
7399451b44SJordan Rupprecht            num_threads,
7499451b44SJordan Rupprecht            2,
75*2238dcc3SJonas Devlieghere            "Number of expected threads and actual threads do not match at breakpoint 1.",
76*2238dcc3SJonas Devlieghere        )
7799451b44SJordan Rupprecht
7899451b44SJordan Rupprecht        # Run to the second breakpoint
7999451b44SJordan Rupprecht        self.runCmd("continue")
8099451b44SJordan Rupprecht        stopped_thread = lldbutil.get_one_thread_stopped_at_breakpoint_id(
81*2238dcc3SJonas Devlieghere            process, bp2_id
82*2238dcc3SJonas Devlieghere        )
83*2238dcc3SJonas Devlieghere        self.assertIsNotNone(stopped_thread, "Process is not stopped at breakpoint 2")
8499451b44SJordan Rupprecht
8599451b44SJordan Rupprecht        # Update the number of threads
8699451b44SJordan Rupprecht        new_num_threads = process.GetNumThreads()
8799451b44SJordan Rupprecht        self.assertEqual(
8899451b44SJordan Rupprecht            new_num_threads,
8999451b44SJordan Rupprecht            num_threads + 1,
90*2238dcc3SJonas Devlieghere            "Number of expected threads did not increase by 1 at bp 2.",
91*2238dcc3SJonas Devlieghere        )
9299451b44SJordan Rupprecht
9399451b44SJordan Rupprecht        # Run to the third breakpoint
9499451b44SJordan Rupprecht        self.runCmd("continue")
9599451b44SJordan Rupprecht        stopped_thread = lldbutil.get_one_thread_stopped_at_breakpoint_id(
96*2238dcc3SJonas Devlieghere            process, bp3_id
97*2238dcc3SJonas Devlieghere        )
98*2238dcc3SJonas Devlieghere        self.assertIsNotNone(stopped_thread, "Process is not stopped at breakpoint 3")
9999451b44SJordan Rupprecht
10099451b44SJordan Rupprecht        # Update the number of threads
10199451b44SJordan Rupprecht        new_num_threads = process.GetNumThreads()
10299451b44SJordan Rupprecht        self.assertEqual(
10399451b44SJordan Rupprecht            new_num_threads,
10499451b44SJordan Rupprecht            num_threads,
105*2238dcc3SJonas Devlieghere            "Number of expected threads is not equal to original number of threads at bp 3.",
106*2238dcc3SJonas Devlieghere        )
10799451b44SJordan Rupprecht
10899451b44SJordan Rupprecht        # Run to the fourth breakpoint
10999451b44SJordan Rupprecht        self.runCmd("continue")
11099451b44SJordan Rupprecht        stopped_thread = lldbutil.get_one_thread_stopped_at_breakpoint_id(
111*2238dcc3SJonas Devlieghere            process, bp4_id
112*2238dcc3SJonas Devlieghere        )
113*2238dcc3SJonas Devlieghere        self.assertIsNotNone(stopped_thread, "Process is not stopped at breakpoint 4")
11499451b44SJordan Rupprecht
11599451b44SJordan Rupprecht        # Update the number of threads
11699451b44SJordan Rupprecht        new_num_threads = process.GetNumThreads()
11799451b44SJordan Rupprecht        self.assertEqual(
11899451b44SJordan Rupprecht            new_num_threads,
11999451b44SJordan Rupprecht            num_threads - 1,
120*2238dcc3SJonas Devlieghere            "Number of expected threads did not decrease by 1 at bp 4.",
121*2238dcc3SJonas Devlieghere        )
12299451b44SJordan Rupprecht
12399451b44SJordan Rupprecht        # Run to completion
12499451b44SJordan Rupprecht        self.runCmd("continue")
12599451b44SJordan Rupprecht
12699451b44SJordan Rupprecht        # At this point, the inferior process should have exited.
1271b8c7352SJonas Devlieghere        self.assertState(process.GetState(), lldb.eStateExited, PROCESS_EXITED)
128