"""
Test thread states.
"""
import unittest
import lldb
from lldbsuite.test.decorators import *
from lldbsuite.test.lldbtest import *
from lldbsuite.test import lldbutil
class ThreadStateTestCase(TestBase):
@expectedFailureAll(
oslist=["linux"],
bugnumber="llvm.org/pr15824 thread states not properly maintained",
)
@skipIfDarwin
@expectedFailureAll(
oslist=["freebsd"],
bugnumber="llvm.org/pr18190 thread states not properly maintained",
)
@expectedFailureNetBSD
def test_state_after_breakpoint(self):
"""Test thread state after breakpoint."""
self.build()
self.thread_state_after_breakpoint_test()
@skipIfDarwin
@expectedFailureAll(
oslist=lldbplatformutil.getDarwinOSTriples(), bugnumber="llvm.org/pr23669"
)
@expectedFailureAll(oslist=["windows"], bugnumber="llvm.org/pr24660")
def test_state_after_continue(self):
"""Test thread state after continue."""
self.build()
self.thread_state_after_continue_test()
@skipIfDarwin
@expectedFailureDarwin("llvm.org/pr23669")
@expectedFailureNetBSD
@skipIfWindows
@unittest.expectedFailure
def test_state_after_expression(self):
"""Test thread state after expression."""
self.build()
self.thread_state_after_expression_test()
@unittest.expectedFailure
@expectedFailureAll(
oslist=["windows"],
bugnumber="llvm.org/pr24668: Breakpoints not resolved correctly",
)
@skipIfDarwin
@expectedFailureNetBSD
def test_process_state(self):
"""Test thread states (comprehensive)."""
self.build()
self.thread_states_test()
def setUp(self):
TestBase.setUp(self)
self.break_1 = line_number("main.cpp", "// Set first breakpoint here")
self.break_2 = line_number("main.cpp", "// Set second breakpoint here")
def thread_state_after_breakpoint_test(self):
"""Test thread state after breakpoint."""
exe = self.getBuildArtifact("a.out")
self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
bp = lldbutil.run_break_set_by_file_and_line(
self, "main.cpp", self.break_1, num_expected_locations=1
)
self.runCmd("run", RUN_SUCCEEDED)
target = self.dbg.GetSelectedTarget()
process = target.GetProcess()
thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint)
self.assertIsNotNone(thread)
self.assertTrue(
thread.IsStopped(), "Thread state isn't 'stopped' during breakpoint 1."
)
self.assertFalse(
thread.IsSuspended(), "Thread state is 'suspended' during breakpoint 1."
)
self.runCmd("process kill")
def wait_for_running_event(self, process):
listener = self.dbg.GetListener()
lldbutil.expect_state_changes(self, listener, process, [lldb.eStateRunning])
def thread_state_after_continue_test(self):
"""Test thread state after continue."""
exe = self.getBuildArtifact("a.out")
self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
lldbutil.run_break_set_by_file_and_line(
self, "main.cpp", self.break_1, num_expected_locations=1
)
lldbutil.run_break_set_by_file_and_line(
self, "main.cpp", self.break_2, num_expected_locations=1
)
self.runCmd("run", RUN_SUCCEEDED)
target = self.dbg.GetSelectedTarget()
process = target.GetProcess()
thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint)
self.assertIsNotNone(thread)
self.dbg.SetAsync(True)
self.runCmd("continue")
self.wait_for_running_event(process)
self.assertFalse(
thread.IsStopped(), "Thread state is 'stopped' when it should be running."
)
self.assertFalse(
thread.IsSuspended(),
"Thread state is 'suspended' when it should be running.",
)
self.dbg.SetAsync(False)
self.runCmd("process kill")
def thread_state_after_expression_test(self):
"""Test thread state after expression."""
exe = self.getBuildArtifact("a.out")
self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
lldbutil.run_break_set_by_file_and_line(
self, "main.cpp", self.break_1, num_expected_locations=1
)
lldbutil.run_break_set_by_file_and_line(
self, "main.cpp", self.break_2, num_expected_locations=1
)
self.runCmd("run", RUN_SUCCEEDED)
target = self.dbg.GetSelectedTarget()
process = target.GetProcess()
thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint)
self.assertIsNotNone(thread)
self.runCmd("expression g_test = 1")
self.assertTrue(
thread.IsStopped(),
"Thread state isn't 'stopped' after expression evaluation.",
)
self.assertFalse(
thread.IsSuspended(),
"Thread state is 'suspended' after expression evaluation.",
)
self.runCmd("process continue")
@expectedFailureAll(
oslist=["windows"],
bugnumber="llvm.org/pr24668: Breakpoints not resolved correctly",
)
@skipIfDarwin
@no_debug_info_test
def test_process_interrupt(self):
"""Test process interrupt and continue."""
self.build()
exe = self.getBuildArtifact("a.out")
self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
bpno = lldbutil.run_break_set_by_file_and_line(
self, "main.cpp", self.break_1, num_expected_locations=1
)
self.runCmd("run", RUN_SUCCEEDED)
target = self.dbg.GetSelectedTarget()
process = target.GetProcess()
thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint)
self.assertIsNotNone(thread)
self.assertTrue(target.BreakpointDelete(bpno))
self.dbg.SetAsync(True)
self.runCmd("continue")
self.wait_for_running_event(process)
self.dbg.SetAsync(False)
self.runCmd("process interrupt")
self.assertStopReason(thread.GetStopReason(), lldb.eStopReasonSignal)
self.runCmd("expression g_test = 1")
self.runCmd("continue")
def thread_states_test(self):
"""Test thread states (comprehensive)."""
exe = self.getBuildArtifact("a.out")
self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
lldbutil.run_break_set_by_file_and_line(
self, "main.cpp", self.break_1, num_expected_locations=1
)
lldbutil.run_break_set_by_file_and_line(
self, "main.cpp", self.break_2, num_expected_locations=1
)
self.runCmd("run", RUN_SUCCEEDED)
target = self.dbg.GetSelectedTarget()
process = target.GetProcess()
thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint)
self.assertIsNotNone(thread)
self.assertTrue(
thread.IsStopped(), "Thread state isn't 'stopped' during breakpoint 1."
)
self.assertFalse(
thread.IsSuspended(), "Thread state is 'suspended' during breakpoint 1."
)
self.dbg.SetAsync(True)
self.runCmd("continue")
self.wait_for_running_event(process)
self.assertFalse(
thread.IsStopped(), "Thread state is 'stopped' when it should be running."
)
self.assertFalse(
thread.IsSuspended(),
"Thread state is 'suspended' when it should be running.",
)
self.dbg.SetAsync(False)
self.runCmd("process interrupt")
self.assertStopReason(thread.GetState(), lldb.eStopReasonSignal)
self.assertTrue(
thread.IsStopped(), "Thread state isn't 'stopped' after process stop."
)
self.assertFalse(
thread.IsSuspended(), "Thread state is 'suspended' after process stop."
)
self.runCmd("expression g_test = 1")
self.assertTrue(
thread.IsStopped(),
"Thread state isn't 'stopped' after expression evaluation.",
)
self.assertFalse(
thread.IsSuspended(),
"Thread state is 'suspended' after expression evaluation.",
)
self.assertStopReason(thread.GetState(), lldb.eStopReasonSignal)
self.runCmd("continue")
self.assertStopReason(thread.GetState(), lldb.eStopReasonBreakpoint)
self.assertTrue(
thread.IsStopped(), "Thread state isn't 'stopped' during breakpoint 2."
)
self.assertFalse(
thread.IsSuspended(), "Thread state is 'suspended' during breakpoint 2."
)
self.runCmd("continue")
self.assertState(process.GetState(), lldb.eStateExited, PROCESS_EXITED)