"""
Test number of threads.
"""
import lldb
from lldbsuite.test.decorators import *
from lldbsuite.test.lldbtest import *
import lldbsuite.test.lldbutil as lldbutil
class NumberOfThreadsTestCase(TestBase):
NO_DEBUG_INFO_TESTCASE = True
def setUp(self):
TestBase.setUp(self)
self.thread3_notify_all_line = line_number(
"main.cpp", "// Set thread3 break point on notify_all at this line."
)
self.thread3_before_lock_line = line_number(
"main.cpp", "// thread3-before-lock"
)
def test_number_of_threads(self):
"""Test number of threads."""
self.build()
exe = self.getBuildArtifact("a.out")
self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
lldbutil.run_break_set_by_file_and_line(
self, "main.cpp", self.thread3_notify_all_line, num_expected_locations=1
)
self.expect(
"breakpoint list -f",
"Breakpoint location shown correctly",
substrs=[
"1: file = 'main.cpp', line = %d, exact_match = 0, locations = 1"
% self.thread3_notify_all_line
],
)
self.runCmd("run", RUN_SUCCEEDED)
self.expect(
"thread list",
STOPPED_DUE_TO_BREAKPOINT,
substrs=["stop reason = breakpoint 1."],
)
target = self.dbg.GetSelectedTarget()
process = target.GetProcess()
num_threads = process.GetNumThreads()
self.assertGreaterEqual(
num_threads,
13,
"Number of expected threads and actual threads do not match.",
)
@skipIfDarwin
@skipIfWindows
def test_unique_stacks(self):
"""Test backtrace unique with multiple threads executing the same stack."""
self.build()
exe = self.getBuildArtifact("a.out")
self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
lldbutil.run_break_set_by_file_and_line(
self, "main.cpp", self.thread3_before_lock_line, num_expected_locations=1
)
self.runCmd("run", RUN_SUCCEEDED)
self.expect(
"thread list",
STOPPED_DUE_TO_BREAKPOINT,
substrs=["stop reason = breakpoint 1."],
)
process = self.process()
num_threads = process.GetNumThreads()
self.assertGreaterEqual(
num_threads,
10,
"Number of expected threads and actual threads do not match.",
)
def is_thread3(thread):
for frame in thread:
if frame.GetFunctionName() is None:
continue
if "thread3" in frame.GetFunctionName():
return True
return False
expect_threads = ""
for i in range(num_threads):
thread = process.GetThreadAtIndex(i)
self.assertTrue(thread.IsValid())
if not is_thread3(thread):
continue
if thread.GetStopReason() != lldb.eStopReasonBreakpoint:
self.runCmd("thread continue %d" % (i + 1))
self.assertStopReason(thread.GetStopReason(), lldb.eStopReasonBreakpoint)
expect_threads += " #%d" % (i + 1)
expect_string = "10 thread(s)%s" % (expect_threads)
self.expect(
"thread backtrace unique",
"Backtrace with unique stack shown correctly",
substrs=[expect_string, "main.cpp:%d" % self.thread3_before_lock_line],
)