xref: /llvm-project/lldb/test/API/functionalities/thread/ignore_suspended/TestIgnoreSuspendedThread.py (revision 2238dcc39358353cac21df75c3c3286ab20b8f53)
1"""
2Test that suspended threads do not affect should-stop decisions.
3"""
4
5import lldb
6from lldbsuite.test.decorators import *
7from lldbsuite.test.lldbtest import *
8import lldbsuite.test.lldbutil as lldbutil
9
10
11class IgnoreSuspendedThreadTestCase(TestBase):
12    NO_DEBUG_INFO_TESTCASE = True
13
14    def setUp(self):
15        # Call super's setUp().
16        TestBase.setUp(self)
17        # Find the line numbers for our breakpoints.
18        self.break_1 = line_number("main.cpp", "// Set first breakpoint here")
19        self.break_2 = line_number("main.cpp", "// Set second breakpoint here")
20        self.break_3 = line_number("main.cpp", "// Set third breakpoint here")
21
22    def printThreadsStoppedByBreakpoint(self, process):
23        stopped_threads = lldbutil.get_stopped_threads(
24            process, lldb.eStopReasonBreakpoint
25        )
26        for thread in stopped_threads:
27            break_id = thread.GetStopReasonDataAtIndex(0)
28            print(
29                "Thread "
30                + str(thread.GetThreadID())
31                + " stopped at breakpoint "
32                + str(break_id)
33            )
34
35    def test(self):
36        self.build()
37        target = lldbutil.run_to_breakpoint_make_target(self)
38
39        # This should create a breakpoint with 1 location.
40        bp1_id = lldbutil.run_break_set_by_file_and_line(
41            self, "main.cpp", self.break_1, num_expected_locations=1
42        )
43
44        bp2_id = lldbutil.run_break_set_by_file_and_line(
45            self, "main.cpp", self.break_2, num_expected_locations=1
46        )
47
48        bp3_id = lldbutil.run_break_set_by_file_and_line(
49            self, "main.cpp", self.break_3, num_expected_locations=1
50        )
51
52        # Run the program.
53        self.runCmd("run", RUN_SUCCEEDED)
54        # Get the target process
55        process = target.GetProcess()
56
57        if self.TraceOn():
58            print("First stop:")
59            self.printThreadsStoppedByBreakpoint(process)
60
61        thread_to_suspend = lldbutil.get_one_thread_stopped_at_breakpoint_id(
62            process, bp1_id
63        )
64        self.assertIsNotNone(thread_to_suspend, "Should hit breakpoint 1")
65        thread_to_suspend.Suspend()
66
67        # Do not stop at bp2 and autocontinue to bp3
68        target.FindBreakpointByID(bp2_id).SetAutoContinue(True)
69
70        # Run to the third breakpoint
71        self.runCmd("continue")
72
73        if self.TraceOn():
74            print("Second stop:")
75            self.printThreadsStoppedByBreakpoint(process)
76
77        stopped_thread = lldbutil.get_one_thread_stopped_at_breakpoint_id(
78            process, bp3_id
79        )
80        self.assertIsNotNone(stopped_thread, "Should hit breakpoint 3")
81
82        thread_to_suspend.Resume()
83
84        # Run to completion
85        self.runCmd("continue")
86
87        # At this point, the inferior process should have exited.
88        self.assertEqual(process.GetState(), lldb.eStateExited, PROCESS_EXITED)
89