xref: /llvm-project/lldb/test/API/functionalities/thread/num_threads/TestNumThreads.py (revision 9c2468821ec51defd09c246fea4a47886fff8c01)
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 NumberOfThreadsTestCase(TestBase):
1399451b44SJordan Rupprecht    NO_DEBUG_INFO_TESTCASE = True
1499451b44SJordan Rupprecht
1599451b44SJordan Rupprecht    def setUp(self):
1699451b44SJordan Rupprecht        # Call super's setUp().
1799451b44SJordan Rupprecht        TestBase.setUp(self)
1899451b44SJordan Rupprecht        # Find the line numbers for our break points.
192238dcc3SJonas Devlieghere        self.thread3_notify_all_line = line_number(
202238dcc3SJonas Devlieghere            "main.cpp", "// Set thread3 break point on notify_all at this line."
212238dcc3SJonas Devlieghere        )
222238dcc3SJonas Devlieghere        self.thread3_before_lock_line = line_number(
232238dcc3SJonas Devlieghere            "main.cpp", "// thread3-before-lock"
242238dcc3SJonas Devlieghere        )
2599451b44SJordan Rupprecht
2699451b44SJordan Rupprecht    def test_number_of_threads(self):
2799451b44SJordan Rupprecht        """Test number of threads."""
2899451b44SJordan Rupprecht        self.build()
2999451b44SJordan Rupprecht        exe = self.getBuildArtifact("a.out")
3099451b44SJordan Rupprecht        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
3199451b44SJordan Rupprecht
3299451b44SJordan Rupprecht        # This should create a breakpoint with 1 location.
3399451b44SJordan Rupprecht        lldbutil.run_break_set_by_file_and_line(
342238dcc3SJonas Devlieghere            self, "main.cpp", self.thread3_notify_all_line, num_expected_locations=1
352238dcc3SJonas Devlieghere        )
3699451b44SJordan Rupprecht
3799451b44SJordan Rupprecht        # The breakpoint list should show 1 location.
3899451b44SJordan Rupprecht        self.expect(
3999451b44SJordan Rupprecht            "breakpoint list -f",
4099451b44SJordan Rupprecht            "Breakpoint location shown correctly",
4199451b44SJordan Rupprecht            substrs=[
422238dcc3SJonas Devlieghere                "1: file = 'main.cpp', line = %d, exact_match = 0, locations = 1"
432238dcc3SJonas Devlieghere                % self.thread3_notify_all_line
442238dcc3SJonas Devlieghere            ],
452238dcc3SJonas Devlieghere        )
4699451b44SJordan Rupprecht
4799451b44SJordan Rupprecht        # Run the program.
4899451b44SJordan Rupprecht        self.runCmd("run", RUN_SUCCEEDED)
4999451b44SJordan Rupprecht
5099451b44SJordan Rupprecht        # Stopped once.
512238dcc3SJonas Devlieghere        self.expect(
522238dcc3SJonas Devlieghere            "thread list",
532238dcc3SJonas Devlieghere            STOPPED_DUE_TO_BREAKPOINT,
542238dcc3SJonas Devlieghere            substrs=["stop reason = breakpoint 1."],
552238dcc3SJonas Devlieghere        )
5699451b44SJordan Rupprecht
5799451b44SJordan Rupprecht        # Get the target process
5899451b44SJordan Rupprecht        target = self.dbg.GetSelectedTarget()
5999451b44SJordan Rupprecht        process = target.GetProcess()
6099451b44SJordan Rupprecht
6199451b44SJordan Rupprecht        # Get the number of threads
6299451b44SJordan Rupprecht        num_threads = process.GetNumThreads()
6399451b44SJordan Rupprecht
6499451b44SJordan Rupprecht        # Using std::thread may involve extra threads, so we assert that there are
6599451b44SJordan Rupprecht        # at least 4 rather than exactly 4.
66*9c246882SJordan Rupprecht        self.assertGreaterEqual(
67*9c246882SJordan Rupprecht            num_threads,
68*9c246882SJordan Rupprecht            13,
692238dcc3SJonas Devlieghere            "Number of expected threads and actual threads do not match.",
702238dcc3SJonas Devlieghere        )
7199451b44SJordan Rupprecht
7299451b44SJordan Rupprecht    @skipIfDarwin  # rdar://33462362
7399451b44SJordan Rupprecht    @skipIfWindows  # This is flakey on Windows: llvm.org/pr37658, llvm.org/pr38373
7499451b44SJordan Rupprecht    def test_unique_stacks(self):
7599451b44SJordan Rupprecht        """Test backtrace unique with multiple threads executing the same stack."""
7699451b44SJordan Rupprecht        self.build()
7799451b44SJordan Rupprecht        exe = self.getBuildArtifact("a.out")
7899451b44SJordan Rupprecht        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
7999451b44SJordan Rupprecht
8099451b44SJordan Rupprecht        # Set a break point on the thread3 notify all (should get hit on threads 4-13).
8199451b44SJordan Rupprecht        lldbutil.run_break_set_by_file_and_line(
822238dcc3SJonas Devlieghere            self, "main.cpp", self.thread3_before_lock_line, num_expected_locations=1
832238dcc3SJonas Devlieghere        )
8499451b44SJordan Rupprecht
8599451b44SJordan Rupprecht        # Run the program.
8699451b44SJordan Rupprecht        self.runCmd("run", RUN_SUCCEEDED)
8799451b44SJordan Rupprecht
8899451b44SJordan Rupprecht        # Stopped once.
892238dcc3SJonas Devlieghere        self.expect(
902238dcc3SJonas Devlieghere            "thread list",
912238dcc3SJonas Devlieghere            STOPPED_DUE_TO_BREAKPOINT,
922238dcc3SJonas Devlieghere            substrs=["stop reason = breakpoint 1."],
932238dcc3SJonas Devlieghere        )
9499451b44SJordan Rupprecht
9599451b44SJordan Rupprecht        process = self.process()
9699451b44SJordan Rupprecht
9799451b44SJordan Rupprecht        # Get the number of threads
9899451b44SJordan Rupprecht        num_threads = process.GetNumThreads()
9999451b44SJordan Rupprecht
10099451b44SJordan Rupprecht        # Using std::thread may involve extra threads, so we assert that there are
10199451b44SJordan Rupprecht        # at least 10 thread3's rather than exactly 10.
102*9c246882SJordan Rupprecht        self.assertGreaterEqual(
103*9c246882SJordan Rupprecht            num_threads,
104*9c246882SJordan Rupprecht            10,
1052238dcc3SJonas Devlieghere            "Number of expected threads and actual threads do not match.",
1062238dcc3SJonas Devlieghere        )
10799451b44SJordan Rupprecht
10899451b44SJordan Rupprecht        # Attempt to walk each of the thread's executing the thread3 function to
10999451b44SJordan Rupprecht        # the same breakpoint.
11099451b44SJordan Rupprecht        def is_thread3(thread):
11199451b44SJordan Rupprecht            for frame in thread:
1120796b170SMuhammad Omair Javaid                if frame.GetFunctionName() is None:
1130796b170SMuhammad Omair Javaid                    continue
1142238dcc3SJonas Devlieghere                if "thread3" in frame.GetFunctionName():
1152238dcc3SJonas Devlieghere                    return True
11699451b44SJordan Rupprecht            return False
11799451b44SJordan Rupprecht
11899451b44SJordan Rupprecht        expect_threads = ""
11999451b44SJordan Rupprecht        for i in range(num_threads):
12099451b44SJordan Rupprecht            thread = process.GetThreadAtIndex(i)
12199451b44SJordan Rupprecht            self.assertTrue(thread.IsValid())
12299451b44SJordan Rupprecht            if not is_thread3(thread):
12399451b44SJordan Rupprecht                continue
12499451b44SJordan Rupprecht
12599451b44SJordan Rupprecht            # If we aren't stopped out the thread breakpoint try to resume.
12699451b44SJordan Rupprecht            if thread.GetStopReason() != lldb.eStopReasonBreakpoint:
12799451b44SJordan Rupprecht                self.runCmd("thread continue %d" % (i + 1))
1280f821339SJonas Devlieghere            self.assertStopReason(thread.GetStopReason(), lldb.eStopReasonBreakpoint)
12999451b44SJordan Rupprecht
13099451b44SJordan Rupprecht            expect_threads += " #%d" % (i + 1)
13199451b44SJordan Rupprecht
13299451b44SJordan Rupprecht        # Construct our expected back trace string
13399451b44SJordan Rupprecht        expect_string = "10 thread(s)%s" % (expect_threads)
13499451b44SJordan Rupprecht
13599451b44SJordan Rupprecht        # Now that we are stopped, we should have 10 threads waiting in the
13699451b44SJordan Rupprecht        # thread3 function. All of these threads should show as one stack.
1372238dcc3SJonas Devlieghere        self.expect(
1382238dcc3SJonas Devlieghere            "thread backtrace unique",
13999451b44SJordan Rupprecht            "Backtrace with unique stack shown correctly",
1402238dcc3SJonas Devlieghere            substrs=[expect_string, "main.cpp:%d" % self.thread3_before_lock_line],
1412238dcc3SJonas Devlieghere        )
142