blob: 61a85585ca8b4bc299f34e4fa2d04dc879005b51 [file] [log] [blame]
Andrew Kaylordbbe36b2013-04-23 21:42:03 +00001"""
2Test thread states.
3"""
4
5import os, time
6import unittest2
7import lldb
8from lldbtest import *
9import lldbutil
10
Andrew Kaylor93132f52013-05-28 23:04:25 +000011class ThreadStateTestCase(TestBase):
Andrew Kaylordbbe36b2013-04-23 21:42:03 +000012
Greg Clayton4570d3e2013-12-10 23:19:29 +000013 mydir = TestBase.compute_mydir(__file__)
Andrew Kaylordbbe36b2013-04-23 21:42:03 +000014
Robert Flack13c7ad92015-03-30 14:12:17 +000015 @skipUnlessDarwin
Andrew Kaylordbbe36b2013-04-23 21:42:03 +000016 @dsym_test
Enrico Granata03e474b2013-11-01 00:57:53 +000017 @expectedFailureDarwin("rdar://15367566")
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000018 def test_state_after_breakpoint_with_dsym(self):
19 """Test thread state after breakpoint."""
Andrew Kaylor93132f52013-05-28 23:04:25 +000020 self.buildDsym(dictionary=self.getBuildFlags(use_cpp11=False))
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000021 self.thread_state_after_breakpoint_test()
22
Enrico Granata03e474b2013-11-01 00:57:53 +000023 @expectedFailureDarwin("rdar://15367566")
Chaoren Lin72b8f052015-02-03 01:51:18 +000024 @expectedFailureFreeBSD('llvm.org/pr15824')
Pavel Labathb36f9172015-06-24 14:43:20 +000025 @expectedFailureLinux("llvm.org/pr15824") # thread states not properly maintained
Chaoren Lin72b8f052015-02-03 01:51:18 +000026 @dwarf_test
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000027 def test_state_after_breakpoint_with_dwarf(self):
28 """Test thread state after breakpoint."""
Andrew Kaylor93132f52013-05-28 23:04:25 +000029 self.buildDwarf(dictionary=self.getBuildFlags(use_cpp11=False))
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000030 self.thread_state_after_breakpoint_test()
31
Robert Flack13c7ad92015-03-30 14:12:17 +000032 @skipUnlessDarwin
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000033 @dsym_test
34 def test_state_after_continue_with_dsym(self):
35 """Test thread state after continue."""
Andrew Kaylor93132f52013-05-28 23:04:25 +000036 self.buildDsym(dictionary=self.getBuildFlags(use_cpp11=False))
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000037 self.thread_state_after_continue_test()
38
39 @dwarf_test
Ying Chen1135e702015-05-28 21:03:26 +000040 @skipIfDarwin # 'llvm.org/pr23669', cause Python crash randomly
41 @expectedFailureDarwin('llvm.org/pr23669')
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000042 def test_state_after_continue_with_dwarf(self):
43 """Test thread state after continue."""
Andrew Kaylor93132f52013-05-28 23:04:25 +000044 self.buildDwarf(dictionary=self.getBuildFlags(use_cpp11=False))
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000045 self.thread_state_after_continue_test()
46
Robert Flack13c7ad92015-03-30 14:12:17 +000047 @skipUnlessDarwin
Ying Chen1135e702015-05-28 21:03:26 +000048 @skipIfDarwin # 'llvm.org/pr23669', cause Python crash randomly
49 @expectedFailureDarwin('llvm.org/pr23669')
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000050 @dsym_test
51 def test_state_after_expression_with_dsym(self):
52 """Test thread state after expression."""
Andrew Kaylor93132f52013-05-28 23:04:25 +000053 self.buildDsym(dictionary=self.getBuildFlags(use_cpp11=False))
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000054 self.thread_state_after_continue_test()
55
Ying Chen1135e702015-05-28 21:03:26 +000056 @skipIfDarwin # 'llvm.org/pr23669', cause Python crash randomly
57 @expectedFailureDarwin('llvm.org/pr23669')
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000058 @dwarf_test
59 def test_state_after_expression_with_dwarf(self):
60 """Test thread state after expression."""
Andrew Kaylor93132f52013-05-28 23:04:25 +000061 self.buildDwarf(dictionary=self.getBuildFlags(use_cpp11=False))
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000062 self.thread_state_after_continue_test()
63
Robert Flack13c7ad92015-03-30 14:12:17 +000064 @skipUnlessDarwin
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000065 @dsym_test
Andrew Kaylor93132f52013-05-28 23:04:25 +000066 @unittest2.expectedFailure("llvm.org/pr16172") # thread states not properly maintained
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000067 def test_process_interrupt_with_dsym(self):
68 """Test process interrupt."""
Andrew Kaylor93132f52013-05-28 23:04:25 +000069 self.buildDsym(dictionary=self.getBuildFlags(use_cpp11=False))
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000070 self.process_interrupt_test()
71
72 @dwarf_test
Andrew Kaylor93132f52013-05-28 23:04:25 +000073 @unittest2.expectedFailure("llvm.org/pr16712") # thread states not properly maintained
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000074 def test_process_interrupt_with_dwarf(self):
75 """Test process interrupt."""
Andrew Kaylor93132f52013-05-28 23:04:25 +000076 self.buildDwarf(dictionary=self.getBuildFlags(use_cpp11=False))
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000077 self.process_interrupt_test()
78
Robert Flack13c7ad92015-03-30 14:12:17 +000079 @skipUnlessDarwin
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000080 @dsym_test
Daniel Maleae8bdd1f2013-05-15 18:48:32 +000081 @unittest2.expectedFailure("llvm.org/pr15824") # thread states not properly maintained
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000082 def test_process_state_with_dsym(self):
83 """Test thread states (comprehensive)."""
Andrew Kaylor93132f52013-05-28 23:04:25 +000084 self.buildDsym(dictionary=self.getBuildFlags(use_cpp11=False))
Andrew Kaylordbbe36b2013-04-23 21:42:03 +000085 self.thread_states_test()
86
87 @dwarf_test
Daniel Maleae8bdd1f2013-05-15 18:48:32 +000088 @unittest2.expectedFailure("llvm.org/pr15824") # thread states not properly maintained
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000089 def test_process_state_with_dwarf(self):
90 """Test thread states (comprehensive)."""
Andrew Kaylor93132f52013-05-28 23:04:25 +000091 self.buildDwarf(dictionary=self.getBuildFlags(use_cpp11=False))
Andrew Kaylordbbe36b2013-04-23 21:42:03 +000092 self.thread_states_test()
93
94 def setUp(self):
95 # Call super's setUp().
96 TestBase.setUp(self)
97 # Find the line numbers for our breakpoints.
98 self.break_1 = line_number('main.c', '// Set first breakpoint here')
99 self.break_2 = line_number('main.c', '// Set second breakpoint here')
100
Andrew Kaylor9f6c5352013-04-30 23:39:14 +0000101 def thread_state_after_breakpoint_test(self):
102 """Test thread state after breakpoint."""
103 exe = os.path.join(os.getcwd(), "a.out")
104 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
105
106 # This should create a breakpoint in the main thread.
107 lldbutil.run_break_set_by_file_and_line (self, "main.c", self.break_1, num_expected_locations=1)
108
109 # The breakpoint list should show 1 breakpoint with 1 location.
110 self.expect("breakpoint list -f", "Breakpoint location shown correctly",
Matt Kopec36e5a7d2013-06-17 19:00:31 +0000111 substrs = ["1: file = 'main.c', line = %d, locations = 1" % self.break_1])
Andrew Kaylor9f6c5352013-04-30 23:39:14 +0000112
113 # Run the program.
Siva Chandra3154aa22015-05-27 22:27:41 +0000114 self.runCmd("run", RUN_FAILED)
Andrew Kaylor9f6c5352013-04-30 23:39:14 +0000115
116 # The stop reason of the thread should be breakpoint.
117 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
118 substrs = ['stopped',
119 '* thread #1',
120 'stop reason = breakpoint'])
121
122 # Get the target process
123 target = self.dbg.GetSelectedTarget()
124 process = target.GetProcess()
125
126 # Get the number of threads
127 num_threads = process.GetNumThreads()
128
129 self.assertTrue(num_threads == 1, 'Number of expected threads and actual threads do not match.')
130
131 # Get the thread object
132 thread = process.GetThreadAtIndex(0)
133
134 # Make sure the thread is in the stopped state.
135 self.assertTrue(thread.IsStopped(), "Thread state isn't \'stopped\' during breakpoint 1.")
136 self.assertFalse(thread.IsSuspended(), "Thread state is \'suspended\' during breakpoint 1.")
137
138 # Kill the process
139 self.runCmd("process kill")
140
141 def thread_state_after_continue_test(self):
142 """Test thread state after continue."""
143 exe = os.path.join(os.getcwd(), "a.out")
144 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
145
146 # This should create a breakpoint in the main thread.
147 lldbutil.run_break_set_by_file_and_line (self, "main.c", self.break_1, num_expected_locations=1)
148 lldbutil.run_break_set_by_file_and_line (self, "main.c", self.break_2, num_expected_locations=1)
149
150 # The breakpoint list should show 1 breakpoints with 1 location.
151 self.expect("breakpoint list -f", "Breakpoint location shown correctly",
Ilia K055ad9b2015-05-18 13:41:01 +0000152 substrs = ["1: file = 'main.c', line = %d, exact_match = 0, locations = 1" % self.break_1])
Andrew Kaylor9f6c5352013-04-30 23:39:14 +0000153
154 # Run the program.
Siva Chandra3154aa22015-05-27 22:27:41 +0000155 self.runCmd("run", RUN_FAILED)
Andrew Kaylor9f6c5352013-04-30 23:39:14 +0000156
157 # The stop reason of the thread should be breakpoint.
158 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
159 substrs = ['stopped',
160 '* thread #1',
161 'stop reason = breakpoint'])
162
163 # Get the target process
164 target = self.dbg.GetSelectedTarget()
165 process = target.GetProcess()
166
167 # Get the number of threads
168 num_threads = process.GetNumThreads()
169
170 self.assertTrue(num_threads == 1, 'Number of expected threads and actual threads do not match.')
171
172 # Get the thread object
173 thread = process.GetThreadAtIndex(0)
174
175 # Continue, the inferior will go into an infinite loop waiting for 'g_test' to change.
176 self.dbg.SetAsync(True)
177 self.runCmd("continue")
178 time.sleep(1)
179
180 # Check the thread state. It should be running.
181 self.assertFalse(thread.IsStopped(), "Thread state is \'stopped\' when it should be running.")
182 self.assertFalse(thread.IsSuspended(), "Thread state is \'suspended\' when it should be running.")
183
184 # Go back to synchronous interactions
185 self.dbg.SetAsync(False)
186
187 # Kill the process
188 self.runCmd("process kill")
189
190 def thread_state_after_expression_test(self):
191 """Test thread state after expression."""
192 exe = os.path.join(os.getcwd(), "a.out")
193 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
194
195 # This should create a breakpoint in the main thread.
196 lldbutil.run_break_set_by_file_and_line (self, "main.c", self.break_1, num_expected_locations=1)
197 lldbutil.run_break_set_by_file_and_line (self, "main.c", self.break_2, num_expected_locations=1)
198
199 # The breakpoint list should show 1 breakpoints with 1 location.
200 self.expect("breakpoint list -f", "Breakpoint location shown correctly",
Matt Kopec36e5a7d2013-06-17 19:00:31 +0000201 substrs = ["1: file = 'main.c', line = %d, locations = 1" % self.break_1])
Andrew Kaylor9f6c5352013-04-30 23:39:14 +0000202
203 # Run the program.
Siva Chandra3154aa22015-05-27 22:27:41 +0000204 self.runCmd("run", RUN_FAILED)
Andrew Kaylor9f6c5352013-04-30 23:39:14 +0000205
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 # Get the thread object
222 thread = process.GetThreadAtIndex(0)
223
224 # Get the inferior out of its loop
225 self.runCmd("expression g_test = 1")
226
227 # Check the thread state
228 self.assertTrue(thread.IsStopped(), "Thread state isn't \'stopped\' after expression evaluation.")
229 self.assertFalse(thread.IsSuspended(), "Thread state is \'suspended\' after expression evaluation.")
230
231 # Let the process run to completion
232 self.runCmd("process continue")
233
234
235 def process_interrupt_test(self):
236 """Test process interrupt and continue."""
237 exe = os.path.join(os.getcwd(), "a.out")
238 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
239
240 # This should create a breakpoint in the main thread.
241 lldbutil.run_break_set_by_file_and_line (self, "main.c", self.break_1, num_expected_locations=1)
242
243 # The breakpoint list should show 1 breakpoints with 1 location.
244 self.expect("breakpoint list -f", "Breakpoint location shown correctly",
Matt Kopec36e5a7d2013-06-17 19:00:31 +0000245 substrs = ["1: file = 'main.c', line = %d, locations = 1" % self.break_1])
Andrew Kaylor9f6c5352013-04-30 23:39:14 +0000246
247 # Run the program.
Siva Chandra3154aa22015-05-27 22:27:41 +0000248 self.runCmd("run", RUN_FAILED)
Andrew Kaylor9f6c5352013-04-30 23:39:14 +0000249
250 # The stop reason of the thread should be breakpoint.
251 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
252 substrs = ['stopped',
253 '* thread #1',
254 'stop reason = breakpoint'])
255
256 # Get the target process
257 target = self.dbg.GetSelectedTarget()
258 process = target.GetProcess()
259
260 # Get the number of threads
261 num_threads = process.GetNumThreads()
262
263 self.assertTrue(num_threads == 1, 'Number of expected threads and actual threads do not match.')
264
265 # Continue, the inferior will go into an infinite loop waiting for 'g_test' to change.
266 self.dbg.SetAsync(True)
267 self.runCmd("continue")
268 time.sleep(1)
269
270 # Go back to synchronous interactions
271 self.dbg.SetAsync(False)
272
273 # Stop the process
274 self.runCmd("process interrupt")
275
276 # The stop reason of the thread should be signal.
277 self.expect("process status", STOPPED_DUE_TO_SIGNAL,
278 substrs = ['stopped',
279 '* thread #1',
280 'stop reason = signal'])
281
282 # Get the inferior out of its loop
283 self.runCmd("expression g_test = 1")
284
285 # Run to completion
286 self.runCmd("continue")
287
Andrew Kaylordbbe36b2013-04-23 21:42:03 +0000288 def thread_states_test(self):
Andrew Kaylor9f6c5352013-04-30 23:39:14 +0000289 """Test thread states (comprehensive)."""
Andrew Kaylordbbe36b2013-04-23 21:42:03 +0000290 exe = os.path.join(os.getcwd(), "a.out")
291 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
292
293 # This should create a breakpoint in the main thread.
294 lldbutil.run_break_set_by_file_and_line (self, "main.c", self.break_1, num_expected_locations=1)
295 lldbutil.run_break_set_by_file_and_line (self, "main.c", self.break_2, num_expected_locations=1)
296
297 # The breakpoint list should show 2 breakpoints with 1 location each.
298 self.expect("breakpoint list -f", "Breakpoint location shown correctly",
Matt Kopec36e5a7d2013-06-17 19:00:31 +0000299 substrs = ["1: file = 'main.c', line = %d, locations = 1" % self.break_1,
300 "2: file = 'main.c', line = %d, locations = 1" % self.break_2])
Andrew Kaylordbbe36b2013-04-23 21:42:03 +0000301
302 # Run the program.
Siva Chandra3154aa22015-05-27 22:27:41 +0000303 self.runCmd("run", RUN_FAILED)
Andrew Kaylordbbe36b2013-04-23 21:42:03 +0000304
305 # The stop reason of the thread should be breakpoint.
306 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
307 substrs = ['stopped',
308 '* thread #1',
309 'stop reason = breakpoint'])
310
311 # Get the target process
312 target = self.dbg.GetSelectedTarget()
313 process = target.GetProcess()
314
315 # Get the number of threads
316 num_threads = process.GetNumThreads()
317
318 self.assertTrue(num_threads == 1, 'Number of expected threads and actual threads do not match.')
319
320 # Get the thread object
321 thread = process.GetThreadAtIndex(0)
322
323 # Make sure the thread is in the stopped state.
324 self.assertTrue(thread.IsStopped(), "Thread state isn't \'stopped\' during breakpoint 1.")
325 self.assertFalse(thread.IsSuspended(), "Thread state is \'suspended\' during breakpoint 1.")
326
327 # Continue, the inferior will go into an infinite loop waiting for 'g_test' to change.
328 self.dbg.SetAsync(True)
329 self.runCmd("continue")
330 time.sleep(1)
331
332 # Check the thread state. It should be running.
333 self.assertFalse(thread.IsStopped(), "Thread state is \'stopped\' when it should be running.")
334 self.assertFalse(thread.IsSuspended(), "Thread state is \'suspended\' when it should be running.")
335
Andrew Kaylordbbe36b2013-04-23 21:42:03 +0000336 # Go back to synchronous interactions
337 self.dbg.SetAsync(False)
338
339 # Stop the process
340 self.runCmd("process interrupt")
341
342 # The stop reason of the thread should be signal.
343 self.expect("process status", STOPPED_DUE_TO_SIGNAL,
344 substrs = ['stopped',
345 '* thread #1',
346 'stop reason = signal'])
347
Andrew Kaylordbbe36b2013-04-23 21:42:03 +0000348 # Check the thread state
349 self.assertTrue(thread.IsStopped(), "Thread state isn't \'stopped\' after process stop.")
350 self.assertFalse(thread.IsSuspended(), "Thread state is \'suspended\' after process stop.")
351
352 # Get the inferior out of its loop
353 self.runCmd("expression g_test = 1")
354
355 # Check the thread state
356 self.assertTrue(thread.IsStopped(), "Thread state isn't \'stopped\' after expression evaluation.")
357 self.assertFalse(thread.IsSuspended(), "Thread state is \'suspended\' after expression evaluation.")
358
Andrew Kaylor9f6c5352013-04-30 23:39:14 +0000359 # The stop reason of the thread should be signal.
360 self.expect("process status", STOPPED_DUE_TO_SIGNAL,
361 substrs = ['stopped',
362 '* thread #1',
363 'stop reason = signal'])
364
Andrew Kaylordbbe36b2013-04-23 21:42:03 +0000365 # Run to breakpoint 2
366 self.runCmd("continue")
367
Andrew Kaylor9f6c5352013-04-30 23:39:14 +0000368 # The stop reason of the thread should be breakpoint.
369 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
370 substrs = ['stopped',
371 '* thread #1',
372 'stop reason = breakpoint'])
373
Andrew Kaylordbbe36b2013-04-23 21:42:03 +0000374 # Make sure both threads are stopped
375 self.assertTrue(thread.IsStopped(), "Thread state isn't \'stopped\' during breakpoint 2.")
376 self.assertFalse(thread.IsSuspended(), "Thread state is \'suspended\' during breakpoint 2.")
377
378 # Run to completion
379 self.runCmd("continue")
380
381 # At this point, the inferior process should have exited.
382 self.assertTrue(process.GetState() == lldb.eStateExited, PROCESS_EXITED)
383
384if __name__ == '__main__':
385 import atexit
386 lldb.SBDebugger.Initialize()
387 atexit.register(lambda: lldb.SBDebugger.Terminate())
388 unittest2.main()