blob: 87be542e10867f0d2e98a3afc2629984a3734dab [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')
25 @expectedFailureLLGS("llvm.org/pr15824") # thread states not properly maintained
26 @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
40 def test_state_after_continue_with_dwarf(self):
41 """Test thread state after continue."""
Andrew Kaylor93132f52013-05-28 23:04:25 +000042 self.buildDwarf(dictionary=self.getBuildFlags(use_cpp11=False))
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000043 self.thread_state_after_continue_test()
44
Robert Flack13c7ad92015-03-30 14:12:17 +000045 @skipUnlessDarwin
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000046 @dsym_test
47 def test_state_after_expression_with_dsym(self):
48 """Test thread state after expression."""
Andrew Kaylor93132f52013-05-28 23:04:25 +000049 self.buildDsym(dictionary=self.getBuildFlags(use_cpp11=False))
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000050 self.thread_state_after_continue_test()
51
52 @dwarf_test
53 def test_state_after_expression_with_dwarf(self):
54 """Test thread state after expression."""
Andrew Kaylor93132f52013-05-28 23:04:25 +000055 self.buildDwarf(dictionary=self.getBuildFlags(use_cpp11=False))
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000056 self.thread_state_after_continue_test()
57
Robert Flack13c7ad92015-03-30 14:12:17 +000058 @skipUnlessDarwin
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000059 @dsym_test
Andrew Kaylor93132f52013-05-28 23:04:25 +000060 @unittest2.expectedFailure("llvm.org/pr16172") # thread states not properly maintained
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000061 def test_process_interrupt_with_dsym(self):
62 """Test process interrupt."""
Andrew Kaylor93132f52013-05-28 23:04:25 +000063 self.buildDsym(dictionary=self.getBuildFlags(use_cpp11=False))
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000064 self.process_interrupt_test()
65
66 @dwarf_test
Andrew Kaylor93132f52013-05-28 23:04:25 +000067 @unittest2.expectedFailure("llvm.org/pr16712") # thread states not properly maintained
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000068 def test_process_interrupt_with_dwarf(self):
69 """Test process interrupt."""
Andrew Kaylor93132f52013-05-28 23:04:25 +000070 self.buildDwarf(dictionary=self.getBuildFlags(use_cpp11=False))
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000071 self.process_interrupt_test()
72
Robert Flack13c7ad92015-03-30 14:12:17 +000073 @skipUnlessDarwin
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000074 @dsym_test
Daniel Maleae8bdd1f2013-05-15 18:48:32 +000075 @unittest2.expectedFailure("llvm.org/pr15824") # thread states not properly maintained
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000076 def test_process_state_with_dsym(self):
77 """Test thread states (comprehensive)."""
Andrew Kaylor93132f52013-05-28 23:04:25 +000078 self.buildDsym(dictionary=self.getBuildFlags(use_cpp11=False))
Andrew Kaylordbbe36b2013-04-23 21:42:03 +000079 self.thread_states_test()
80
81 @dwarf_test
Daniel Maleae8bdd1f2013-05-15 18:48:32 +000082 @unittest2.expectedFailure("llvm.org/pr15824") # thread states not properly maintained
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000083 def test_process_state_with_dwarf(self):
84 """Test thread states (comprehensive)."""
Andrew Kaylor93132f52013-05-28 23:04:25 +000085 self.buildDwarf(dictionary=self.getBuildFlags(use_cpp11=False))
Andrew Kaylordbbe36b2013-04-23 21:42:03 +000086 self.thread_states_test()
87
88 def setUp(self):
89 # Call super's setUp().
90 TestBase.setUp(self)
91 # Find the line numbers for our breakpoints.
92 self.break_1 = line_number('main.c', '// Set first breakpoint here')
93 self.break_2 = line_number('main.c', '// Set second breakpoint here')
94
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000095 def thread_state_after_breakpoint_test(self):
96 """Test thread state after breakpoint."""
97 exe = os.path.join(os.getcwd(), "a.out")
98 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
99
100 # This should create a breakpoint in the main thread.
101 lldbutil.run_break_set_by_file_and_line (self, "main.c", self.break_1, num_expected_locations=1)
102
103 # The breakpoint list should show 1 breakpoint with 1 location.
104 self.expect("breakpoint list -f", "Breakpoint location shown correctly",
Matt Kopec36e5a7d2013-06-17 19:00:31 +0000105 substrs = ["1: file = 'main.c', line = %d, locations = 1" % self.break_1])
Andrew Kaylor9f6c5352013-04-30 23:39:14 +0000106
107 # Run the program.
108 self.runCmd("run", RUN_SUCCEEDED)
109
110 # The stop reason of the thread should be breakpoint.
111 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
112 substrs = ['stopped',
113 '* thread #1',
114 'stop reason = breakpoint'])
115
116 # Get the target process
117 target = self.dbg.GetSelectedTarget()
118 process = target.GetProcess()
119
120 # Get the number of threads
121 num_threads = process.GetNumThreads()
122
123 self.assertTrue(num_threads == 1, 'Number of expected threads and actual threads do not match.')
124
125 # Get the thread object
126 thread = process.GetThreadAtIndex(0)
127
128 # Make sure the thread is in the stopped state.
129 self.assertTrue(thread.IsStopped(), "Thread state isn't \'stopped\' during breakpoint 1.")
130 self.assertFalse(thread.IsSuspended(), "Thread state is \'suspended\' during breakpoint 1.")
131
132 # Kill the process
133 self.runCmd("process kill")
134
135 def thread_state_after_continue_test(self):
136 """Test thread state after continue."""
137 exe = os.path.join(os.getcwd(), "a.out")
138 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
139
140 # This should create a breakpoint in the main thread.
141 lldbutil.run_break_set_by_file_and_line (self, "main.c", self.break_1, num_expected_locations=1)
142 lldbutil.run_break_set_by_file_and_line (self, "main.c", self.break_2, num_expected_locations=1)
143
144 # The breakpoint list should show 1 breakpoints with 1 location.
145 self.expect("breakpoint list -f", "Breakpoint location shown correctly",
Ilia K055ad9b2015-05-18 13:41:01 +0000146 substrs = ["1: file = 'main.c', line = %d, exact_match = 0, locations = 1" % self.break_1])
Andrew Kaylor9f6c5352013-04-30 23:39:14 +0000147
148 # Run the program.
149 self.runCmd("run", RUN_SUCCEEDED)
150
151 # The stop reason of the thread should be breakpoint.
152 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
153 substrs = ['stopped',
154 '* thread #1',
155 'stop reason = breakpoint'])
156
157 # Get the target process
158 target = self.dbg.GetSelectedTarget()
159 process = target.GetProcess()
160
161 # Get the number of threads
162 num_threads = process.GetNumThreads()
163
164 self.assertTrue(num_threads == 1, 'Number of expected threads and actual threads do not match.')
165
166 # Get the thread object
167 thread = process.GetThreadAtIndex(0)
168
169 # Continue, the inferior will go into an infinite loop waiting for 'g_test' to change.
170 self.dbg.SetAsync(True)
171 self.runCmd("continue")
172 time.sleep(1)
173
174 # Check the thread state. It should be running.
175 self.assertFalse(thread.IsStopped(), "Thread state is \'stopped\' when it should be running.")
176 self.assertFalse(thread.IsSuspended(), "Thread state is \'suspended\' when it should be running.")
177
178 # Go back to synchronous interactions
179 self.dbg.SetAsync(False)
180
181 # Kill the process
182 self.runCmd("process kill")
183
184 def thread_state_after_expression_test(self):
185 """Test thread state after expression."""
186 exe = os.path.join(os.getcwd(), "a.out")
187 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
188
189 # This should create a breakpoint in the main thread.
190 lldbutil.run_break_set_by_file_and_line (self, "main.c", self.break_1, num_expected_locations=1)
191 lldbutil.run_break_set_by_file_and_line (self, "main.c", self.break_2, num_expected_locations=1)
192
193 # The breakpoint list should show 1 breakpoints with 1 location.
194 self.expect("breakpoint list -f", "Breakpoint location shown correctly",
Matt Kopec36e5a7d2013-06-17 19:00:31 +0000195 substrs = ["1: file = 'main.c', line = %d, locations = 1" % self.break_1])
Andrew Kaylor9f6c5352013-04-30 23:39:14 +0000196
197 # Run the program.
198 self.runCmd("run", RUN_SUCCEEDED)
199
200 # The stop reason of the thread should be breakpoint.
201 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
202 substrs = ['stopped',
203 '* thread #1',
204 'stop reason = breakpoint'])
205
206 # Get the target process
207 target = self.dbg.GetSelectedTarget()
208 process = target.GetProcess()
209
210 # Get the number of threads
211 num_threads = process.GetNumThreads()
212
213 self.assertTrue(num_threads == 1, 'Number of expected threads and actual threads do not match.')
214
215 # Get the thread object
216 thread = process.GetThreadAtIndex(0)
217
218 # Get the inferior out of its loop
219 self.runCmd("expression g_test = 1")
220
221 # Check the thread state
222 self.assertTrue(thread.IsStopped(), "Thread state isn't \'stopped\' after expression evaluation.")
223 self.assertFalse(thread.IsSuspended(), "Thread state is \'suspended\' after expression evaluation.")
224
225 # Let the process run to completion
226 self.runCmd("process continue")
227
228
229 def process_interrupt_test(self):
230 """Test process interrupt and continue."""
231 exe = os.path.join(os.getcwd(), "a.out")
232 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
233
234 # This should create a breakpoint in the main thread.
235 lldbutil.run_break_set_by_file_and_line (self, "main.c", self.break_1, num_expected_locations=1)
236
237 # The breakpoint list should show 1 breakpoints with 1 location.
238 self.expect("breakpoint list -f", "Breakpoint location shown correctly",
Matt Kopec36e5a7d2013-06-17 19:00:31 +0000239 substrs = ["1: file = 'main.c', line = %d, locations = 1" % self.break_1])
Andrew Kaylor9f6c5352013-04-30 23:39:14 +0000240
241 # Run the program.
242 self.runCmd("run", RUN_SUCCEEDED)
243
244 # The stop reason of the thread should be breakpoint.
245 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
246 substrs = ['stopped',
247 '* thread #1',
248 'stop reason = breakpoint'])
249
250 # Get the target process
251 target = self.dbg.GetSelectedTarget()
252 process = target.GetProcess()
253
254 # Get the number of threads
255 num_threads = process.GetNumThreads()
256
257 self.assertTrue(num_threads == 1, 'Number of expected threads and actual threads do not match.')
258
259 # Continue, the inferior will go into an infinite loop waiting for 'g_test' to change.
260 self.dbg.SetAsync(True)
261 self.runCmd("continue")
262 time.sleep(1)
263
264 # Go back to synchronous interactions
265 self.dbg.SetAsync(False)
266
267 # Stop the process
268 self.runCmd("process interrupt")
269
270 # The stop reason of the thread should be signal.
271 self.expect("process status", STOPPED_DUE_TO_SIGNAL,
272 substrs = ['stopped',
273 '* thread #1',
274 'stop reason = signal'])
275
276 # Get the inferior out of its loop
277 self.runCmd("expression g_test = 1")
278
279 # Run to completion
280 self.runCmd("continue")
281
Andrew Kaylordbbe36b2013-04-23 21:42:03 +0000282 def thread_states_test(self):
Andrew Kaylor9f6c5352013-04-30 23:39:14 +0000283 """Test thread states (comprehensive)."""
Andrew Kaylordbbe36b2013-04-23 21:42:03 +0000284 exe = os.path.join(os.getcwd(), "a.out")
285 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
286
287 # This should create a breakpoint in the main thread.
288 lldbutil.run_break_set_by_file_and_line (self, "main.c", self.break_1, num_expected_locations=1)
289 lldbutil.run_break_set_by_file_and_line (self, "main.c", self.break_2, num_expected_locations=1)
290
291 # The breakpoint list should show 2 breakpoints with 1 location each.
292 self.expect("breakpoint list -f", "Breakpoint location shown correctly",
Matt Kopec36e5a7d2013-06-17 19:00:31 +0000293 substrs = ["1: file = 'main.c', line = %d, locations = 1" % self.break_1,
294 "2: file = 'main.c', line = %d, locations = 1" % self.break_2])
Andrew Kaylordbbe36b2013-04-23 21:42:03 +0000295
296 # Run the program.
297 self.runCmd("run", RUN_SUCCEEDED)
298
299 # The stop reason of the thread should be breakpoint.
300 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
301 substrs = ['stopped',
302 '* thread #1',
303 'stop reason = breakpoint'])
304
305 # Get the target process
306 target = self.dbg.GetSelectedTarget()
307 process = target.GetProcess()
308
309 # Get the number of threads
310 num_threads = process.GetNumThreads()
311
312 self.assertTrue(num_threads == 1, 'Number of expected threads and actual threads do not match.')
313
314 # Get the thread object
315 thread = process.GetThreadAtIndex(0)
316
317 # Make sure the thread is in the stopped state.
318 self.assertTrue(thread.IsStopped(), "Thread state isn't \'stopped\' during breakpoint 1.")
319 self.assertFalse(thread.IsSuspended(), "Thread state is \'suspended\' during breakpoint 1.")
320
321 # Continue, the inferior will go into an infinite loop waiting for 'g_test' to change.
322 self.dbg.SetAsync(True)
323 self.runCmd("continue")
324 time.sleep(1)
325
326 # Check the thread state. It should be running.
327 self.assertFalse(thread.IsStopped(), "Thread state is \'stopped\' when it should be running.")
328 self.assertFalse(thread.IsSuspended(), "Thread state is \'suspended\' when it should be running.")
329
Andrew Kaylordbbe36b2013-04-23 21:42:03 +0000330 # Go back to synchronous interactions
331 self.dbg.SetAsync(False)
332
333 # Stop the process
334 self.runCmd("process interrupt")
335
336 # The stop reason of the thread should be signal.
337 self.expect("process status", STOPPED_DUE_TO_SIGNAL,
338 substrs = ['stopped',
339 '* thread #1',
340 'stop reason = signal'])
341
Andrew Kaylordbbe36b2013-04-23 21:42:03 +0000342 # Check the thread state
343 self.assertTrue(thread.IsStopped(), "Thread state isn't \'stopped\' after process stop.")
344 self.assertFalse(thread.IsSuspended(), "Thread state is \'suspended\' after process stop.")
345
346 # Get the inferior out of its loop
347 self.runCmd("expression g_test = 1")
348
349 # Check the thread state
350 self.assertTrue(thread.IsStopped(), "Thread state isn't \'stopped\' after expression evaluation.")
351 self.assertFalse(thread.IsSuspended(), "Thread state is \'suspended\' after expression evaluation.")
352
Andrew Kaylor9f6c5352013-04-30 23:39:14 +0000353 # The stop reason of the thread should be signal.
354 self.expect("process status", STOPPED_DUE_TO_SIGNAL,
355 substrs = ['stopped',
356 '* thread #1',
357 'stop reason = signal'])
358
Andrew Kaylordbbe36b2013-04-23 21:42:03 +0000359 # Run to breakpoint 2
360 self.runCmd("continue")
361
Andrew Kaylor9f6c5352013-04-30 23:39:14 +0000362 # The stop reason of the thread should be breakpoint.
363 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
364 substrs = ['stopped',
365 '* thread #1',
366 'stop reason = breakpoint'])
367
Andrew Kaylordbbe36b2013-04-23 21:42:03 +0000368 # Make sure both threads are stopped
369 self.assertTrue(thread.IsStopped(), "Thread state isn't \'stopped\' during breakpoint 2.")
370 self.assertFalse(thread.IsSuspended(), "Thread state is \'suspended\' during breakpoint 2.")
371
372 # Run to completion
373 self.runCmd("continue")
374
375 # At this point, the inferior process should have exited.
376 self.assertTrue(process.GetState() == lldb.eStateExited, PROCESS_EXITED)
377
378if __name__ == '__main__':
379 import atexit
380 lldb.SBDebugger.Initialize()
381 atexit.register(lambda: lldb.SBDebugger.Terminate())
382 unittest2.main()