]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - packages/Python/lldbsuite/test/functionalities/thread/state/TestThreadStates.py
Vendor import of lldb trunk r256945:
[FreeBSD/FreeBSD.git] / packages / Python / lldbsuite / test / functionalities / thread / state / TestThreadStates.py
1 """
2 Test thread states.
3 """
4
5 from __future__ import print_function
6
7
8
9 import unittest2
10 import os, time
11 import lldb
12 from lldbsuite.test.lldbtest import *
13 import lldbsuite.test.lldbutil as lldbutil
14
15 class ThreadStateTestCase(TestBase):
16
17     mydir = TestBase.compute_mydir(__file__)
18
19     @expectedFailureDarwin("rdar://15367566")
20     @expectedFailureFreeBSD('llvm.org/pr15824')
21     @expectedFailureLinux("llvm.org/pr15824") # thread states not properly maintained
22     @expectedFailureWindows("llvm.org/pr24668") # Breakpoints not resolved correctly
23     def test_state_after_breakpoint(self):
24         """Test thread state after breakpoint."""
25         self.build(dictionary=self.getBuildFlags(use_cpp11=False))
26         self.thread_state_after_breakpoint_test()
27
28     @skipIfDarwin # 'llvm.org/pr23669', cause Python crash randomly
29     @expectedFailureDarwin('llvm.org/pr23669')
30     @expectedFailureFreeBSD('llvm.org/pr15824')
31     @expectedFailureWindows("llvm.org/pr24660")
32     def test_state_after_continue(self):
33         """Test thread state after continue."""
34         self.build(dictionary=self.getBuildFlags(use_cpp11=False))
35         self.thread_state_after_continue_test()
36
37     @skipIfDarwin # 'llvm.org/pr23669', cause Python crash randomly
38     @expectedFailureDarwin('llvm.org/pr23669')
39     @expectedFailureWindows("llvm.org/pr24660")
40     @unittest2.expectedFailure("llvm.org/pr16712") # thread states not properly maintained
41     def test_state_after_expression(self):
42         """Test thread state after expression."""
43         self.build(dictionary=self.getBuildFlags(use_cpp11=False))
44         self.thread_state_after_expression_test()
45
46     @unittest2.expectedFailure("llvm.org/pr16712") # thread states not properly maintained
47     @expectedFailureWindows("llvm.org/pr24668") # Breakpoints not resolved correctly
48     def test_process_interrupt(self):
49         """Test process interrupt."""
50         self.build(dictionary=self.getBuildFlags(use_cpp11=False))
51         self.process_interrupt_test()
52
53     @unittest2.expectedFailure("llvm.org/pr15824") # thread states not properly maintained
54     @expectedFailureWindows("llvm.org/pr24668") # Breakpoints not resolved correctly
55     def test_process_state(self):
56         """Test thread states (comprehensive)."""
57         self.build(dictionary=self.getBuildFlags(use_cpp11=False))
58         self.thread_states_test()
59
60     def setUp(self):
61         # Call super's setUp().
62         TestBase.setUp(self)
63         # Find the line numbers for our breakpoints.
64         self.break_1 = line_number('main.cpp', '// Set first breakpoint here')
65         self.break_2 = line_number('main.cpp', '// Set second breakpoint here')
66
67     def thread_state_after_breakpoint_test(self):
68         """Test thread state after breakpoint."""
69         exe = os.path.join(os.getcwd(), "a.out")
70         self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
71
72         # This should create a breakpoint in the main thread.
73         lldbutil.run_break_set_by_file_and_line (self, "main.cpp", self.break_1, num_expected_locations=1)
74
75         # Run the program.
76         self.runCmd("run", RUN_SUCCEEDED)
77
78         # The stop reason of the thread should be breakpoint.
79         self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
80             substrs = ['stopped',
81                        '* thread #1',
82                        'stop reason = breakpoint'])
83
84         # Get the target process
85         target = self.dbg.GetSelectedTarget()
86         process = target.GetProcess()
87
88         # Get the number of threads
89         num_threads = process.GetNumThreads()
90
91         self.assertTrue(num_threads == 1, 'Number of expected threads and actual threads do not match.')
92
93         # Get the thread object
94         thread = process.GetThreadAtIndex(0)
95
96         # Make sure the thread is in the stopped state.
97         self.assertTrue(thread.IsStopped(), "Thread state isn't \'stopped\' during breakpoint 1.")
98         self.assertFalse(thread.IsSuspended(), "Thread state is \'suspended\' during breakpoint 1.")
99
100         # Kill the process
101         self.runCmd("process kill")
102
103     def wait_for_running_event(self):
104         listener = self.dbg.GetListener()
105         if lldb.remote_platform:
106             lldbutil.expect_state_changes(self, listener, [lldb.eStateConnected])
107         lldbutil.expect_state_changes(self, listener, [lldb.eStateRunning])
108
109     def thread_state_after_continue_test(self):
110         """Test thread state after continue."""
111         exe = os.path.join(os.getcwd(), "a.out")
112         self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
113
114         # This should create a breakpoint in the main thread.
115         lldbutil.run_break_set_by_file_and_line (self, "main.cpp", self.break_1, num_expected_locations=1)
116         lldbutil.run_break_set_by_file_and_line (self, "main.cpp", self.break_2, num_expected_locations=1)
117
118         # Run the program.
119         self.runCmd("run", RUN_SUCCEEDED)
120
121         # The stop reason of the thread should be breakpoint.
122         self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
123             substrs = ['stopped',
124                        '* thread #1',
125                        'stop reason = breakpoint'])
126
127         # Get the target process
128         target = self.dbg.GetSelectedTarget()
129         process = target.GetProcess()
130
131         # Get the number of threads
132         num_threads = process.GetNumThreads()
133
134         self.assertTrue(num_threads == 1, 'Number of expected threads and actual threads do not match.')
135
136         # Get the thread object
137         thread = process.GetThreadAtIndex(0)
138
139         # Continue, the inferior will go into an infinite loop waiting for 'g_test' to change.
140         self.dbg.SetAsync(True)
141         self.runCmd("continue")
142         self.wait_for_running_event()
143
144         # Check the thread state. It should be running.
145         self.assertFalse(thread.IsStopped(), "Thread state is \'stopped\' when it should be running.")
146         self.assertFalse(thread.IsSuspended(), "Thread state is \'suspended\' when it should be running.")
147
148         # Go back to synchronous interactions
149         self.dbg.SetAsync(False)
150
151         # Kill the process
152         self.runCmd("process kill")
153
154     def thread_state_after_expression_test(self):
155         """Test thread state after expression."""
156         exe = os.path.join(os.getcwd(), "a.out")
157         self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
158
159         # This should create a breakpoint in the main thread.
160         lldbutil.run_break_set_by_file_and_line (self, "main.cpp", self.break_1, num_expected_locations=1)
161         lldbutil.run_break_set_by_file_and_line (self, "main.cpp", self.break_2, num_expected_locations=1)
162
163         # Run the program.
164         self.runCmd("run", RUN_SUCCEEDED)
165
166         # The stop reason of the thread should be breakpoint.
167         self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
168             substrs = ['stopped',
169                        '* thread #1',
170                        'stop reason = breakpoint'])
171
172         # Get the target process
173         target = self.dbg.GetSelectedTarget()
174         process = target.GetProcess()
175
176         # Get the number of threads
177         num_threads = process.GetNumThreads()
178
179         self.assertTrue(num_threads == 1, 'Number of expected threads and actual threads do not match.')
180
181         # Get the thread object
182         thread = process.GetThreadAtIndex(0)
183
184         # Get the inferior out of its loop
185         self.runCmd("expression g_test = 1")
186
187         # Check the thread state
188         self.assertTrue(thread.IsStopped(), "Thread state isn't \'stopped\' after expression evaluation.")
189         self.assertFalse(thread.IsSuspended(), "Thread state is \'suspended\' after expression evaluation.")
190
191         # Let the process run to completion
192         self.runCmd("process continue")
193
194
195     def process_interrupt_test(self):
196         """Test process interrupt and continue."""
197         exe = os.path.join(os.getcwd(), "a.out")
198         self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
199
200         # This should create a breakpoint in the main thread.
201         lldbutil.run_break_set_by_file_and_line (self, "main.cpp", self.break_1, num_expected_locations=1)
202
203         # Run the program.
204         self.runCmd("run", RUN_SUCCEEDED)
205
206         # The stop reason of the thread should be breakpoint.
207         self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
208             substrs = ['stopped',
209                        '* thread #1',
210                        'stop reason = breakpoint'])
211
212         # Get the target process
213         target = self.dbg.GetSelectedTarget()
214         process = target.GetProcess()
215
216         # Get the number of threads
217         num_threads = process.GetNumThreads()
218
219         self.assertTrue(num_threads == 1, 'Number of expected threads and actual threads do not match.')
220
221         # Continue, the inferior will go into an infinite loop waiting for 'g_test' to change.
222         self.dbg.SetAsync(True)
223         self.runCmd("continue")
224         self.wait_for_running_event()
225
226         # Go back to synchronous interactions
227         self.dbg.SetAsync(False)
228
229         # Stop the process
230         self.runCmd("process interrupt")
231
232         # The stop reason of the thread should be signal.
233         self.expect("process status", STOPPED_DUE_TO_SIGNAL,
234             substrs = ['stopped',
235                        '* thread #1',
236                        'stop reason = signal'])
237
238         # Get the inferior out of its loop
239         self.runCmd("expression g_test = 1")
240
241         # Run to completion
242         self.runCmd("continue")
243
244     def thread_states_test(self):
245         """Test thread states (comprehensive)."""
246         exe = os.path.join(os.getcwd(), "a.out")
247         self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
248
249         # This should create a breakpoint in the main thread.
250         lldbutil.run_break_set_by_file_and_line (self, "main.cpp", self.break_1, num_expected_locations=1)
251         lldbutil.run_break_set_by_file_and_line (self, "main.cpp", self.break_2, num_expected_locations=1)
252
253         # Run the program.
254         self.runCmd("run", RUN_SUCCEEDED)
255
256         # The stop reason of the thread should be breakpoint.
257         self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
258             substrs = ['stopped',
259                        '* thread #1',
260                        'stop reason = breakpoint'])
261
262         # Get the target process
263         target = self.dbg.GetSelectedTarget()
264         process = target.GetProcess()
265
266         # Get the number of threads
267         num_threads = process.GetNumThreads()
268
269         self.assertTrue(num_threads == 1, 'Number of expected threads and actual threads do not match.')
270
271         # Get the thread object
272         thread = process.GetThreadAtIndex(0)
273
274         # Make sure the thread is in the stopped state.
275         self.assertTrue(thread.IsStopped(), "Thread state isn't \'stopped\' during breakpoint 1.")
276         self.assertFalse(thread.IsSuspended(), "Thread state is \'suspended\' during breakpoint 1.")
277
278         # Continue, the inferior will go into an infinite loop waiting for 'g_test' to change.
279         self.dbg.SetAsync(True)
280         self.runCmd("continue")
281         self.wait_for_running_event()
282
283         # Check the thread state. It should be running.
284         self.assertFalse(thread.IsStopped(), "Thread state is \'stopped\' when it should be running.")
285         self.assertFalse(thread.IsSuspended(), "Thread state is \'suspended\' when it should be running.")
286
287         # Go back to synchronous interactions
288         self.dbg.SetAsync(False)
289
290         # Stop the process
291         self.runCmd("process interrupt")
292
293         # The stop reason of the thread should be signal.
294         self.expect("process status", STOPPED_DUE_TO_SIGNAL,
295             substrs = ['stopped',
296                        '* thread #1',
297                        'stop reason = signal'])
298
299         # Check the thread state
300         self.assertTrue(thread.IsStopped(), "Thread state isn't \'stopped\' after process stop.")
301         self.assertFalse(thread.IsSuspended(), "Thread state is \'suspended\' after process stop.")
302
303         # Get the inferior out of its loop
304         self.runCmd("expression g_test = 1")
305
306         # Check the thread state
307         self.assertTrue(thread.IsStopped(), "Thread state isn't \'stopped\' after expression evaluation.")
308         self.assertFalse(thread.IsSuspended(), "Thread state is \'suspended\' after expression evaluation.")
309
310         # The stop reason of the thread should be signal.
311         self.expect("process status", STOPPED_DUE_TO_SIGNAL,
312             substrs = ['stopped',
313                        '* thread #1',
314                        'stop reason = signal'])
315
316         # Run to breakpoint 2
317         self.runCmd("continue")
318
319         # The stop reason of the thread should be breakpoint.
320         self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
321             substrs = ['stopped',
322                        '* thread #1',
323                        'stop reason = breakpoint'])
324
325         # Make sure both threads are stopped
326         self.assertTrue(thread.IsStopped(), "Thread state isn't \'stopped\' during breakpoint 2.")
327         self.assertFalse(thread.IsSuspended(), "Thread state is \'suspended\' during breakpoint 2.")
328
329         # Run to completion
330         self.runCmd("continue")
331
332         # At this point, the inferior process should have exited.
333         self.assertTrue(process.GetState() == lldb.eStateExited, PROCESS_EXITED)