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 = \
24            lldbutil.get_stopped_threads(process, lldb.eStopReasonBreakpoint)
25        for thread in stopped_threads:
26            break_id = thread.GetStopReasonDataAtIndex(0)
27            print('Thread ' + str(thread.GetThreadID()) + \
28                    ' stopped at breakpoint ' + str(break_id))
29
30    def test(self):
31        self.build()
32        target  = lldbutil.run_to_breakpoint_make_target(self)
33
34        #This should create a breakpoint with 1 location.
35        bp1_id = \
36            lldbutil.run_break_set_by_file_and_line(self,
37                                                    "main.cpp",
38                                                    self.break_1,
39                                                    num_expected_locations=1)
40
41        bp2_id = \
42            lldbutil.run_break_set_by_file_and_line(self,
43                                                    "main.cpp",
44                                                    self.break_2,
45                                                    num_expected_locations=1)
46
47        bp3_id = \
48            lldbutil.run_break_set_by_file_and_line(self,
49                                                    "main.cpp",
50                                                    self.break_3,
51                                                    num_expected_locations=1)
52
53        #Run the program.
54        self.runCmd("run", RUN_SUCCEEDED)
55        #Get the target process
56        process = target.GetProcess()
57
58        if self.TraceOn():
59            print('First stop:')
60            self.printThreadsStoppedByBreakpoint(process)
61
62        thread_to_suspend = \
63            lldbutil.get_one_thread_stopped_at_breakpoint_id(process,
64                                                                bp1_id)
65        self.assertIsNotNone(thread_to_suspend, "Should hit breakpoint 1")
66        thread_to_suspend.Suspend()
67
68        #Do not stop at bp2 and autocontinue to bp3
69        target.FindBreakpointByID(bp2_id).SetAutoContinue(True)
70
71        #Run to the third breakpoint
72        self.runCmd("continue")
73
74        if self.TraceOn():
75            print('Second stop:')
76            self.printThreadsStoppedByBreakpoint(process)
77
78        stopped_thread = \
79            lldbutil.get_one_thread_stopped_at_breakpoint_id(process,
80                                                                bp3_id)
81        self.assertIsNotNone(stopped_thread,
82                             "Should hit breakpoint 3")
83
84        thread_to_suspend.Resume()
85
86        #Run to completion
87        self.runCmd("continue")
88
89        #At this point, the inferior process should have exited.
90        self.assertEqual(process.GetState(),
91                            lldb.eStateExited,
92                                PROCESS_EXITED)
93