xref: /llvm-project/lldb/test/API/functionalities/thread/num_threads/TestNumThreads.py (revision f21e704d4a2cc992faff46665b31daad290cc8b8)
1"""
2Test number of threads.
3"""
4
5
6
7import lldb
8from lldbsuite.test.decorators import *
9from lldbsuite.test.lldbtest import *
10import lldbsuite.test.lldbutil as lldbutil
11
12
13class NumberOfThreadsTestCase(TestBase):
14
15    mydir = TestBase.compute_mydir(__file__)
16    NO_DEBUG_INFO_TESTCASE = True
17
18    def setUp(self):
19        # Call super's setUp().
20        TestBase.setUp(self)
21        # Find the line numbers for our break points.
22        self.thread3_notify_all_line = line_number('main.cpp', '// Set thread3 break point on notify_all at this line.')
23        self.thread3_before_lock_line = line_number('main.cpp', '// thread3-before-lock')
24
25    def test_number_of_threads(self):
26        """Test number of threads."""
27        self.build()
28        exe = self.getBuildArtifact("a.out")
29        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
30
31        # This should create a breakpoint with 1 location.
32        lldbutil.run_break_set_by_file_and_line(
33            self, "main.cpp", self.thread3_notify_all_line, num_expected_locations=1)
34
35        # The breakpoint list should show 1 location.
36        self.expect(
37            "breakpoint list -f",
38            "Breakpoint location shown correctly",
39            substrs=[
40                "1: file = 'main.cpp', line = %d, exact_match = 0, locations = 1" %
41                self.thread3_notify_all_line])
42
43        # Run the program.
44        self.runCmd("run", RUN_SUCCEEDED)
45
46        # Stopped once.
47        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
48                    substrs=["stop reason = breakpoint 1."])
49
50        # Get the target process
51        target = self.dbg.GetSelectedTarget()
52        process = target.GetProcess()
53
54        # Get the number of threads
55        num_threads = process.GetNumThreads()
56
57        # Using std::thread may involve extra threads, so we assert that there are
58        # at least 4 rather than exactly 4.
59        self.assertTrue(
60            num_threads >= 13,
61            'Number of expected threads and actual threads do not match.')
62
63    @skipIfDarwin # rdar://33462362
64    @skipIfWindows # This is flakey on Windows: llvm.org/pr37658, llvm.org/pr38373
65    def test_unique_stacks(self):
66        """Test backtrace unique with multiple threads executing the same stack."""
67        self.build()
68        exe = self.getBuildArtifact("a.out")
69        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
70
71        # Set a break point on the thread3 notify all (should get hit on threads 4-13).
72        lldbutil.run_break_set_by_file_and_line(
73            self, "main.cpp", self.thread3_before_lock_line, num_expected_locations=1)
74
75        # Run the program.
76        self.runCmd("run", RUN_SUCCEEDED)
77
78        # Stopped once.
79        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
80                    substrs=["stop reason = breakpoint 1."])
81
82        process = self.process()
83
84        # Get the number of threads
85        num_threads = process.GetNumThreads()
86
87        # Using std::thread may involve extra threads, so we assert that there are
88        # at least 10 thread3's rather than exactly 10.
89        self.assertTrue(
90            num_threads >= 10,
91            'Number of expected threads and actual threads do not match.')
92
93        # Attempt to walk each of the thread's executing the thread3 function to
94        # the same breakpoint.
95        def is_thread3(thread):
96            for frame in thread:
97                if frame.GetFunctionName() is None:
98                    continue
99                if "thread3" in frame.GetFunctionName(): return True
100            return False
101
102        expect_threads = ""
103        for i in range(num_threads):
104            thread = process.GetThreadAtIndex(i)
105            self.assertTrue(thread.IsValid())
106            if not is_thread3(thread):
107                continue
108
109            # If we aren't stopped out the thread breakpoint try to resume.
110            if thread.GetStopReason() != lldb.eStopReasonBreakpoint:
111                self.runCmd("thread continue %d"%(i+1))
112            self.assertEqual(thread.GetStopReason(), lldb.eStopReasonBreakpoint)
113
114            expect_threads += " #%d"%(i+1)
115
116        # Construct our expected back trace string
117        expect_string = "10 thread(s)%s" % (expect_threads)
118
119        # Now that we are stopped, we should have 10 threads waiting in the
120        # thread3 function. All of these threads should show as one stack.
121        self.expect("thread backtrace unique",
122                    "Backtrace with unique stack shown correctly",
123                    substrs=[expect_string,
124                        "main.cpp:%d"%self.thread3_before_lock_line])
125