blob: 41b2c34a82b4d99e7e37f3eb5ac101c8afdc0d02 [file] [log] [blame]
Zachary Turnerfbb79bb2015-09-02 16:47:01 +00001"""
Andrew Kaylordbbe36b2013-04-23 21:42:03 +00002Test 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
Zachary Turner656ef0b2015-09-02 16:47:29 +000026 @expectedFailureWindows("llvm.org/pr24668") # Breakpoints not resolved correctly
Chaoren Lin72b8f052015-02-03 01:51:18 +000027 @dwarf_test
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000028 def test_state_after_breakpoint_with_dwarf(self):
29 """Test thread state after breakpoint."""
Andrew Kaylor93132f52013-05-28 23:04:25 +000030 self.buildDwarf(dictionary=self.getBuildFlags(use_cpp11=False))
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000031 self.thread_state_after_breakpoint_test()
32
Robert Flack13c7ad92015-03-30 14:12:17 +000033 @skipUnlessDarwin
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000034 @dsym_test
35 def test_state_after_continue_with_dsym(self):
36 """Test thread state after continue."""
Andrew Kaylor93132f52013-05-28 23:04:25 +000037 self.buildDsym(dictionary=self.getBuildFlags(use_cpp11=False))
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000038 self.thread_state_after_continue_test()
39
40 @dwarf_test
Ying Chen1135e702015-05-28 21:03:26 +000041 @skipIfDarwin # 'llvm.org/pr23669', cause Python crash randomly
42 @expectedFailureDarwin('llvm.org/pr23669')
Zachary Turnerfbb79bb2015-09-02 16:47:01 +000043 @expectedFailureWindows("llvm.org/pr24660")
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000044 def test_state_after_continue_with_dwarf(self):
45 """Test thread state after continue."""
Andrew Kaylor93132f52013-05-28 23:04:25 +000046 self.buildDwarf(dictionary=self.getBuildFlags(use_cpp11=False))
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000047 self.thread_state_after_continue_test()
48
Robert Flack13c7ad92015-03-30 14:12:17 +000049 @skipUnlessDarwin
Ying Chen1135e702015-05-28 21:03:26 +000050 @skipIfDarwin # 'llvm.org/pr23669', cause Python crash randomly
51 @expectedFailureDarwin('llvm.org/pr23669')
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000052 @dsym_test
53 def test_state_after_expression_with_dsym(self):
54 """Test thread state after expression."""
Andrew Kaylor93132f52013-05-28 23:04:25 +000055 self.buildDsym(dictionary=self.getBuildFlags(use_cpp11=False))
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000056 self.thread_state_after_continue_test()
57
Ying Chen1135e702015-05-28 21:03:26 +000058 @skipIfDarwin # 'llvm.org/pr23669', cause Python crash randomly
59 @expectedFailureDarwin('llvm.org/pr23669')
Zachary Turnerfbb79bb2015-09-02 16:47:01 +000060 @expectedFailureWindows("llvm.org/pr24660")
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000061 @dwarf_test
62 def test_state_after_expression_with_dwarf(self):
63 """Test thread state after expression."""
Andrew Kaylor93132f52013-05-28 23:04:25 +000064 self.buildDwarf(dictionary=self.getBuildFlags(use_cpp11=False))
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000065 self.thread_state_after_continue_test()
66
Robert Flack13c7ad92015-03-30 14:12:17 +000067 @skipUnlessDarwin
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000068 @dsym_test
Andrew Kaylor93132f52013-05-28 23:04:25 +000069 @unittest2.expectedFailure("llvm.org/pr16172") # thread states not properly maintained
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000070 def test_process_interrupt_with_dsym(self):
71 """Test process interrupt."""
Andrew Kaylor93132f52013-05-28 23:04:25 +000072 self.buildDsym(dictionary=self.getBuildFlags(use_cpp11=False))
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000073 self.process_interrupt_test()
74
75 @dwarf_test
Andrew Kaylor93132f52013-05-28 23:04:25 +000076 @unittest2.expectedFailure("llvm.org/pr16712") # thread states not properly maintained
Zachary Turner656ef0b2015-09-02 16:47:29 +000077 @expectedFailureWindows("llvm.org/pr24668") # Breakpoints not resolved correctly
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000078 def test_process_interrupt_with_dwarf(self):
79 """Test process interrupt."""
Andrew Kaylor93132f52013-05-28 23:04:25 +000080 self.buildDwarf(dictionary=self.getBuildFlags(use_cpp11=False))
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000081 self.process_interrupt_test()
82
Robert Flack13c7ad92015-03-30 14:12:17 +000083 @skipUnlessDarwin
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000084 @dsym_test
Daniel Maleae8bdd1f2013-05-15 18:48:32 +000085 @unittest2.expectedFailure("llvm.org/pr15824") # thread states not properly maintained
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000086 def test_process_state_with_dsym(self):
87 """Test thread states (comprehensive)."""
Andrew Kaylor93132f52013-05-28 23:04:25 +000088 self.buildDsym(dictionary=self.getBuildFlags(use_cpp11=False))
Andrew Kaylordbbe36b2013-04-23 21:42:03 +000089 self.thread_states_test()
90
91 @dwarf_test
Daniel Maleae8bdd1f2013-05-15 18:48:32 +000092 @unittest2.expectedFailure("llvm.org/pr15824") # thread states not properly maintained
Zachary Turner656ef0b2015-09-02 16:47:29 +000093 @expectedFailureWindows("llvm.org/pr24668") # Breakpoints not resolved correctly
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000094 def test_process_state_with_dwarf(self):
95 """Test thread states (comprehensive)."""
Andrew Kaylor93132f52013-05-28 23:04:25 +000096 self.buildDwarf(dictionary=self.getBuildFlags(use_cpp11=False))
Andrew Kaylordbbe36b2013-04-23 21:42:03 +000097 self.thread_states_test()
98
99 def setUp(self):
100 # Call super's setUp().
101 TestBase.setUp(self)
102 # Find the line numbers for our breakpoints.
Zachary Turnerd1c5b6f2015-08-25 22:25:21 +0000103 self.break_1 = line_number('main.cpp', '// Set first breakpoint here')
104 self.break_2 = line_number('main.cpp', '// Set second breakpoint here')
Andrew Kaylordbbe36b2013-04-23 21:42:03 +0000105
Andrew Kaylor9f6c5352013-04-30 23:39:14 +0000106 def thread_state_after_breakpoint_test(self):
107 """Test thread state after breakpoint."""
108 exe = os.path.join(os.getcwd(), "a.out")
109 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
110
111 # This should create a breakpoint in the main thread.
Zachary Turnerd1c5b6f2015-08-25 22:25:21 +0000112 lldbutil.run_break_set_by_file_and_line (self, "main.cpp", self.break_1, num_expected_locations=1)
Andrew Kaylor9f6c5352013-04-30 23:39:14 +0000113
114 # The breakpoint list should show 1 breakpoint with 1 location.
115 self.expect("breakpoint list -f", "Breakpoint location shown correctly",
Zachary Turnerd1c5b6f2015-08-25 22:25:21 +0000116 substrs = ["1: file = 'main.cpp', line = %d, locations = 1" % self.break_1])
Andrew Kaylor9f6c5352013-04-30 23:39:14 +0000117
118 # Run the program.
Sean Callanan05834cd2015-07-01 23:56:30 +0000119 self.runCmd("run", RUN_SUCCEEDED)
Andrew Kaylor9f6c5352013-04-30 23:39:14 +0000120
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 # Make sure the thread is in the stopped state.
140 self.assertTrue(thread.IsStopped(), "Thread state isn't \'stopped\' during breakpoint 1.")
141 self.assertFalse(thread.IsSuspended(), "Thread state is \'suspended\' during breakpoint 1.")
142
143 # Kill the process
144 self.runCmd("process kill")
145
146 def thread_state_after_continue_test(self):
147 """Test thread state after continue."""
148 exe = os.path.join(os.getcwd(), "a.out")
149 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
150
151 # This should create a breakpoint in the main thread.
Zachary Turnerd1c5b6f2015-08-25 22:25:21 +0000152 lldbutil.run_break_set_by_file_and_line (self, "main.cpp", self.break_1, num_expected_locations=1)
153 lldbutil.run_break_set_by_file_and_line (self, "main.cpp", self.break_2, num_expected_locations=1)
Andrew Kaylor9f6c5352013-04-30 23:39:14 +0000154
155 # The breakpoint list should show 1 breakpoints with 1 location.
156 self.expect("breakpoint list -f", "Breakpoint location shown correctly",
Zachary Turnerd1c5b6f2015-08-25 22:25:21 +0000157 substrs = ["1: file = 'main.cpp', line = %d, exact_match = 0, locations = 1" % self.break_1])
Andrew Kaylor9f6c5352013-04-30 23:39:14 +0000158
159 # Run the program.
Sean Callanan05834cd2015-07-01 23:56:30 +0000160 self.runCmd("run", RUN_SUCCEEDED)
Andrew Kaylor9f6c5352013-04-30 23:39:14 +0000161
162 # The stop reason of the thread should be breakpoint.
163 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
164 substrs = ['stopped',
165 '* thread #1',
166 'stop reason = breakpoint'])
167
168 # Get the target process
169 target = self.dbg.GetSelectedTarget()
170 process = target.GetProcess()
171
172 # Get the number of threads
173 num_threads = process.GetNumThreads()
174
175 self.assertTrue(num_threads == 1, 'Number of expected threads and actual threads do not match.')
176
177 # Get the thread object
178 thread = process.GetThreadAtIndex(0)
179
180 # Continue, the inferior will go into an infinite loop waiting for 'g_test' to change.
181 self.dbg.SetAsync(True)
182 self.runCmd("continue")
183 time.sleep(1)
184
185 # Check the thread state. It should be running.
186 self.assertFalse(thread.IsStopped(), "Thread state is \'stopped\' when it should be running.")
187 self.assertFalse(thread.IsSuspended(), "Thread state is \'suspended\' when it should be running.")
188
189 # Go back to synchronous interactions
190 self.dbg.SetAsync(False)
191
192 # Kill the process
193 self.runCmd("process kill")
194
195 def thread_state_after_expression_test(self):
196 """Test thread state after expression."""
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.
Zachary Turnerd1c5b6f2015-08-25 22:25:21 +0000201 lldbutil.run_break_set_by_file_and_line (self, "main.cpp", self.break_1, num_expected_locations=1)
202 lldbutil.run_break_set_by_file_and_line (self, "main.cpp", self.break_2, num_expected_locations=1)
Andrew Kaylor9f6c5352013-04-30 23:39:14 +0000203
204 # The breakpoint list should show 1 breakpoints with 1 location.
205 self.expect("breakpoint list -f", "Breakpoint location shown correctly",
Zachary Turnerd1c5b6f2015-08-25 22:25:21 +0000206 substrs = ["1: file = 'main.cpp', line = %d, locations = 1" % self.break_1])
Andrew Kaylor9f6c5352013-04-30 23:39:14 +0000207
208 # Run the program.
Sean Callanan05834cd2015-07-01 23:56:30 +0000209 self.runCmd("run", RUN_SUCCEEDED)
Andrew Kaylor9f6c5352013-04-30 23:39:14 +0000210
211 # The stop reason of the thread should be breakpoint.
212 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
213 substrs = ['stopped',
214 '* thread #1',
215 'stop reason = breakpoint'])
216
217 # Get the target process
218 target = self.dbg.GetSelectedTarget()
219 process = target.GetProcess()
220
221 # Get the number of threads
222 num_threads = process.GetNumThreads()
223
224 self.assertTrue(num_threads == 1, 'Number of expected threads and actual threads do not match.')
225
226 # Get the thread object
227 thread = process.GetThreadAtIndex(0)
228
229 # Get the inferior out of its loop
230 self.runCmd("expression g_test = 1")
231
232 # Check the thread state
233 self.assertTrue(thread.IsStopped(), "Thread state isn't \'stopped\' after expression evaluation.")
234 self.assertFalse(thread.IsSuspended(), "Thread state is \'suspended\' after expression evaluation.")
235
236 # Let the process run to completion
237 self.runCmd("process continue")
238
239
240 def process_interrupt_test(self):
241 """Test process interrupt and continue."""
242 exe = os.path.join(os.getcwd(), "a.out")
243 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
244
245 # This should create a breakpoint in the main thread.
Zachary Turnerd1c5b6f2015-08-25 22:25:21 +0000246 lldbutil.run_break_set_by_file_and_line (self, "main.cpp", self.break_1, num_expected_locations=1)
Andrew Kaylor9f6c5352013-04-30 23:39:14 +0000247
248 # The breakpoint list should show 1 breakpoints with 1 location.
249 self.expect("breakpoint list -f", "Breakpoint location shown correctly",
Zachary Turnerd1c5b6f2015-08-25 22:25:21 +0000250 substrs = ["1: file = 'main.cpp', line = %d, locations = 1" % self.break_1])
Andrew Kaylor9f6c5352013-04-30 23:39:14 +0000251
252 # Run the program.
Sean Callanan05834cd2015-07-01 23:56:30 +0000253 self.runCmd("run", RUN_SUCCEEDED)
Andrew Kaylor9f6c5352013-04-30 23:39:14 +0000254
255 # The stop reason of the thread should be breakpoint.
256 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
257 substrs = ['stopped',
258 '* thread #1',
259 'stop reason = breakpoint'])
260
261 # Get the target process
262 target = self.dbg.GetSelectedTarget()
263 process = target.GetProcess()
264
265 # Get the number of threads
266 num_threads = process.GetNumThreads()
267
268 self.assertTrue(num_threads == 1, 'Number of expected threads and actual threads do not match.')
269
270 # Continue, the inferior will go into an infinite loop waiting for 'g_test' to change.
271 self.dbg.SetAsync(True)
272 self.runCmd("continue")
273 time.sleep(1)
274
275 # Go back to synchronous interactions
276 self.dbg.SetAsync(False)
277
278 # Stop the process
279 self.runCmd("process interrupt")
280
281 # The stop reason of the thread should be signal.
282 self.expect("process status", STOPPED_DUE_TO_SIGNAL,
283 substrs = ['stopped',
284 '* thread #1',
285 'stop reason = signal'])
286
287 # Get the inferior out of its loop
288 self.runCmd("expression g_test = 1")
289
290 # Run to completion
291 self.runCmd("continue")
292
Andrew Kaylordbbe36b2013-04-23 21:42:03 +0000293 def thread_states_test(self):
Andrew Kaylor9f6c5352013-04-30 23:39:14 +0000294 """Test thread states (comprehensive)."""
Andrew Kaylordbbe36b2013-04-23 21:42:03 +0000295 exe = os.path.join(os.getcwd(), "a.out")
296 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
297
298 # This should create a breakpoint in the main thread.
Zachary Turnerd1c5b6f2015-08-25 22:25:21 +0000299 lldbutil.run_break_set_by_file_and_line (self, "main.cpp", self.break_1, num_expected_locations=1)
300 lldbutil.run_break_set_by_file_and_line (self, "main.cpp", self.break_2, num_expected_locations=1)
Andrew Kaylordbbe36b2013-04-23 21:42:03 +0000301
302 # The breakpoint list should show 2 breakpoints with 1 location each.
303 self.expect("breakpoint list -f", "Breakpoint location shown correctly",
Zachary Turnerd1c5b6f2015-08-25 22:25:21 +0000304 substrs = ["1: file = 'main.cpp', line = %d, locations = 1" % self.break_1,
305 "2: file = 'main.cpp', line = %d, locations = 1" % self.break_2])
Andrew Kaylordbbe36b2013-04-23 21:42:03 +0000306
307 # Run the program.
Sean Callanan05834cd2015-07-01 23:56:30 +0000308 self.runCmd("run", RUN_SUCCEEDED)
Andrew Kaylordbbe36b2013-04-23 21:42:03 +0000309
310 # The stop reason of the thread should be breakpoint.
311 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
312 substrs = ['stopped',
313 '* thread #1',
314 'stop reason = breakpoint'])
315
316 # Get the target process
317 target = self.dbg.GetSelectedTarget()
318 process = target.GetProcess()
319
320 # Get the number of threads
321 num_threads = process.GetNumThreads()
322
323 self.assertTrue(num_threads == 1, 'Number of expected threads and actual threads do not match.')
324
325 # Get the thread object
326 thread = process.GetThreadAtIndex(0)
327
328 # Make sure the thread is in the stopped state.
329 self.assertTrue(thread.IsStopped(), "Thread state isn't \'stopped\' during breakpoint 1.")
330 self.assertFalse(thread.IsSuspended(), "Thread state is \'suspended\' during breakpoint 1.")
331
332 # Continue, the inferior will go into an infinite loop waiting for 'g_test' to change.
333 self.dbg.SetAsync(True)
334 self.runCmd("continue")
335 time.sleep(1)
336
337 # Check the thread state. It should be running.
338 self.assertFalse(thread.IsStopped(), "Thread state is \'stopped\' when it should be running.")
339 self.assertFalse(thread.IsSuspended(), "Thread state is \'suspended\' when it should be running.")
340
Andrew Kaylordbbe36b2013-04-23 21:42:03 +0000341 # Go back to synchronous interactions
342 self.dbg.SetAsync(False)
343
344 # Stop the process
345 self.runCmd("process interrupt")
346
347 # The stop reason of the thread should be signal.
348 self.expect("process status", STOPPED_DUE_TO_SIGNAL,
349 substrs = ['stopped',
350 '* thread #1',
351 'stop reason = signal'])
352
Andrew Kaylordbbe36b2013-04-23 21:42:03 +0000353 # Check the thread state
354 self.assertTrue(thread.IsStopped(), "Thread state isn't \'stopped\' after process stop.")
355 self.assertFalse(thread.IsSuspended(), "Thread state is \'suspended\' after process stop.")
356
357 # Get the inferior out of its loop
358 self.runCmd("expression g_test = 1")
359
360 # Check the thread state
361 self.assertTrue(thread.IsStopped(), "Thread state isn't \'stopped\' after expression evaluation.")
362 self.assertFalse(thread.IsSuspended(), "Thread state is \'suspended\' after expression evaluation.")
363
Andrew Kaylor9f6c5352013-04-30 23:39:14 +0000364 # The stop reason of the thread should be signal.
365 self.expect("process status", STOPPED_DUE_TO_SIGNAL,
366 substrs = ['stopped',
367 '* thread #1',
368 'stop reason = signal'])
369
Andrew Kaylordbbe36b2013-04-23 21:42:03 +0000370 # Run to breakpoint 2
371 self.runCmd("continue")
372
Andrew Kaylor9f6c5352013-04-30 23:39:14 +0000373 # The stop reason of the thread should be breakpoint.
374 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
375 substrs = ['stopped',
376 '* thread #1',
377 'stop reason = breakpoint'])
378
Andrew Kaylordbbe36b2013-04-23 21:42:03 +0000379 # Make sure both threads are stopped
380 self.assertTrue(thread.IsStopped(), "Thread state isn't \'stopped\' during breakpoint 2.")
381 self.assertFalse(thread.IsSuspended(), "Thread state is \'suspended\' during breakpoint 2.")
382
383 # Run to completion
384 self.runCmd("continue")
385
386 # At this point, the inferior process should have exited.
387 self.assertTrue(process.GetState() == lldb.eStateExited, PROCESS_EXITED)
388
389if __name__ == '__main__':
390 import atexit
391 lldb.SBDebugger.Initialize()
392 atexit.register(lambda: lldb.SBDebugger.Terminate())
393 unittest2.main()