1"""
2Test number of threads.
3"""
4
5
6
7import lldb
8from lldbsuite.test.decorators import *
9from lldbsuite.test.lldbtest import *
10from lldbsuite.test import lldbutil
11
12
13class CreateDuringStepTestCase(TestBase):
14
15    mydir = TestBase.compute_mydir(__file__)
16
17    @expectedFailureAll(
18        oslist=["linux"],
19        bugnumber="llvm.org/pr15824 thread states not properly maintained")
20    @expectedFailureAll(
21        oslist=lldbplatformutil.getDarwinOSTriples(),
22        bugnumber="llvm.org/pr15824 thread states not properly maintained, <rdar://problem/28557237>")
23    @expectedFailureAll(
24        oslist=["freebsd"],
25        bugnumber="llvm.org/pr18190 thread states not properly maintained")
26    @expectedFailureAll(
27        oslist=["windows"],
28        bugnumber="llvm.org/pr24668: Breakpoints not resolved correctly")
29    @expectedFailureNetBSD
30    def test_step_inst(self):
31        """Test thread creation during step-inst handling."""
32        self.build(dictionary=self.getBuildFlags())
33        self.create_during_step_base(
34            "thread step-inst -m all-threads",
35            'stop reason = instruction step')
36
37    @expectedFailureAll(
38        oslist=["linux"],
39        bugnumber="llvm.org/pr15824 thread states not properly maintained")
40    @expectedFailureAll(
41        oslist=lldbplatformutil.getDarwinOSTriples(),
42        bugnumber="llvm.org/pr15824 thread states not properly maintained, <rdar://problem/28557237>")
43    @expectedFailureAll(
44        oslist=["freebsd"],
45        bugnumber="llvm.org/pr18190 thread states not properly maintained")
46    @expectedFailureAll(
47        oslist=["windows"],
48        bugnumber="llvm.org/pr24668: Breakpoints not resolved correctly")
49    @expectedFailureNetBSD
50    def test_step_over(self):
51        """Test thread creation during step-over handling."""
52        self.build(dictionary=self.getBuildFlags())
53        self.create_during_step_base(
54            "thread step-over -m all-threads",
55            'stop reason = step over')
56
57    @expectedFailureAll(
58        oslist=["linux"],
59        bugnumber="llvm.org/pr15824 thread states not properly maintained")
60    @expectedFailureAll(
61        oslist=lldbplatformutil.getDarwinOSTriples(),
62        bugnumber="<rdar://problem/28574077>")
63    @expectedFailureAll(
64        oslist=["freebsd"],
65        bugnumber="llvm.org/pr18190 thread states not properly maintained")
66    @expectedFailureAll(
67        oslist=["windows"],
68        bugnumber="llvm.org/pr24668: Breakpoints not resolved correctly")
69    @expectedFailureNetBSD
70    def test_step_in(self):
71        """Test thread creation during step-in handling."""
72        self.build(dictionary=self.getBuildFlags())
73        self.create_during_step_base(
74            "thread step-in -m all-threads",
75            'stop reason = step in')
76
77    def setUp(self):
78        # Call super's setUp().
79        TestBase.setUp(self)
80        # Find the line numbers to break and continue.
81        self.breakpoint = line_number('main.cpp', '// Set breakpoint here')
82        self.continuepoint = line_number('main.cpp', '// Continue from here')
83
84    def create_during_step_base(self, step_cmd, step_stop_reason):
85        """Test thread creation while using step-in."""
86        exe = self.getBuildArtifact("a.out")
87        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
88
89        # Get the target process
90        target = self.dbg.GetSelectedTarget()
91
92        # This should create a breakpoint in the stepping thread.
93        self.bkpt = target.BreakpointCreateByLocation("main.cpp", self.breakpoint)
94
95        # Run the program.
96        self.runCmd("run", RUN_SUCCEEDED)
97
98        process = target.GetProcess()
99
100        # The stop reason of the thread should be breakpoint.
101        stepping_thread = lldbutil.get_one_thread_stopped_at_breakpoint(process, self.bkpt)
102        self.assertTrue(stepping_thread.IsValid(), "We stopped at the right breakpoint")
103
104        # Get the number of threads
105        num_threads = process.GetNumThreads()
106
107        # Make sure we see only two threads
108        self.assertTrue(
109            num_threads == 2,
110            'Number of expected threads and actual threads do not match.')
111
112        # Get the thread objects
113        thread1 = process.GetThreadAtIndex(0)
114        thread2 = process.GetThreadAtIndex(1)
115
116        current_line = self.breakpoint
117        # Keep stepping until we've reached our designated continue point
118        while current_line != self.continuepoint:
119            if stepping_thread != process.GetSelectedThread():
120                process.SetSelectedThread(stepping_thread)
121
122            self.runCmd(step_cmd)
123
124            frame = stepping_thread.GetFrameAtIndex(0)
125            current_line = frame.GetLineEntry().GetLine()
126
127            # Make sure we're still where we thought we were
128            self.assertTrue(
129                current_line >= self.breakpoint,
130                "Stepped to unexpected line, " +
131                str(current_line))
132            self.assertTrue(
133                current_line <= self.continuepoint,
134                "Stepped to unexpected line, " +
135                str(current_line))
136
137        # Update the number of threads
138        num_threads = process.GetNumThreads()
139
140        # Check to see that we increased the number of threads as expected
141        self.assertTrue(
142            num_threads == 3,
143            'Number of expected threads and actual threads do not match after thread exit.')
144
145        stop_reason = stepping_thread.GetStopReason()
146        self.assertEqual(stop_reason, lldb.eStopReasonPlanComplete, "Stopped for plan completion")
147
148        # Run to completion
149        self.runCmd("process continue")
150
151        # At this point, the inferior process should have exited.
152        self.assertTrue(
153            process.GetState() == lldb.eStateExited,
154            PROCESS_EXITED)
155