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