blob: 4209893e772d62c6b8a724f09a02a31e8c517d6b [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
15 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
16 @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
Ed Maste34bdbbd2013-09-13 15:34:59 +000023 @expectedFailureFreeBSD('llvm.org/pr15824')
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000024 @dwarf_test
Enrico Granata03e474b2013-11-01 00:57:53 +000025 @expectedFailureDarwin("rdar://15367566")
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000026 def test_state_after_breakpoint_with_dwarf(self):
27 """Test thread state after breakpoint."""
Andrew Kaylor93132f52013-05-28 23:04:25 +000028 self.buildDwarf(dictionary=self.getBuildFlags(use_cpp11=False))
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000029 self.thread_state_after_breakpoint_test()
30
31 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
32 @dsym_test
33 def test_state_after_continue_with_dsym(self):
34 """Test thread state after continue."""
Andrew Kaylor93132f52013-05-28 23:04:25 +000035 self.buildDsym(dictionary=self.getBuildFlags(use_cpp11=False))
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000036 self.thread_state_after_continue_test()
37
38 @dwarf_test
39 def test_state_after_continue_with_dwarf(self):
40 """Test thread state after continue."""
Andrew Kaylor93132f52013-05-28 23:04:25 +000041 self.buildDwarf(dictionary=self.getBuildFlags(use_cpp11=False))
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000042 self.thread_state_after_continue_test()
43
44 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
45 @dsym_test
46 def test_state_after_expression_with_dsym(self):
47 """Test thread state after expression."""
Andrew Kaylor93132f52013-05-28 23:04:25 +000048 self.buildDsym(dictionary=self.getBuildFlags(use_cpp11=False))
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000049 self.thread_state_after_continue_test()
50
51 @dwarf_test
52 def test_state_after_expression_with_dwarf(self):
53 """Test thread state after expression."""
Andrew Kaylor93132f52013-05-28 23:04:25 +000054 self.buildDwarf(dictionary=self.getBuildFlags(use_cpp11=False))
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000055 self.thread_state_after_continue_test()
56
57 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
58 @dsym_test
Andrew Kaylor93132f52013-05-28 23:04:25 +000059 @unittest2.expectedFailure("llvm.org/pr16172") # thread states not properly maintained
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000060 def test_process_interrupt_with_dsym(self):
61 """Test process interrupt."""
Andrew Kaylor93132f52013-05-28 23:04:25 +000062 self.buildDsym(dictionary=self.getBuildFlags(use_cpp11=False))
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000063 self.process_interrupt_test()
64
65 @dwarf_test
Andrew Kaylor93132f52013-05-28 23:04:25 +000066 @unittest2.expectedFailure("llvm.org/pr16712") # thread states not properly maintained
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000067 def test_process_interrupt_with_dwarf(self):
68 """Test process interrupt."""
Andrew Kaylor93132f52013-05-28 23:04:25 +000069 self.buildDwarf(dictionary=self.getBuildFlags(use_cpp11=False))
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000070 self.process_interrupt_test()
71
72 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
73 @dsym_test
Daniel Maleae8bdd1f2013-05-15 18:48:32 +000074 @unittest2.expectedFailure("llvm.org/pr15824") # thread states not properly maintained
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000075 def test_process_state_with_dsym(self):
76 """Test thread states (comprehensive)."""
Andrew Kaylor93132f52013-05-28 23:04:25 +000077 self.buildDsym(dictionary=self.getBuildFlags(use_cpp11=False))
Andrew Kaylordbbe36b2013-04-23 21:42:03 +000078 self.thread_states_test()
79
80 @dwarf_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_dwarf(self):
83 """Test thread states (comprehensive)."""
Andrew Kaylor93132f52013-05-28 23:04:25 +000084 self.buildDwarf(dictionary=self.getBuildFlags(use_cpp11=False))
Andrew Kaylordbbe36b2013-04-23 21:42:03 +000085 self.thread_states_test()
86
87 def setUp(self):
88 # Call super's setUp().
89 TestBase.setUp(self)
90 # Find the line numbers for our breakpoints.
91 self.break_1 = line_number('main.c', '// Set first breakpoint here')
92 self.break_2 = line_number('main.c', '// Set second breakpoint here')
93
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000094 def thread_state_after_breakpoint_test(self):
95 """Test thread state after breakpoint."""
96 exe = os.path.join(os.getcwd(), "a.out")
97 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
98
99 # This should create a breakpoint in the main thread.
100 lldbutil.run_break_set_by_file_and_line (self, "main.c", self.break_1, num_expected_locations=1)
101
102 # The breakpoint list should show 1 breakpoint with 1 location.
103 self.expect("breakpoint list -f", "Breakpoint location shown correctly",
Matt Kopec36e5a7d2013-06-17 19:00:31 +0000104 substrs = ["1: file = 'main.c', line = %d, locations = 1" % self.break_1])
Andrew Kaylor9f6c5352013-04-30 23:39:14 +0000105
106 # Run the program.
107 self.runCmd("run", RUN_SUCCEEDED)
108
109 # The stop reason of the thread should be breakpoint.
110 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
111 substrs = ['stopped',
112 '* thread #1',
113 'stop reason = breakpoint'])
114
115 # Get the target process
116 target = self.dbg.GetSelectedTarget()
117 process = target.GetProcess()
118
119 # Get the number of threads
120 num_threads = process.GetNumThreads()
121
122 self.assertTrue(num_threads == 1, 'Number of expected threads and actual threads do not match.')
123
124 # Get the thread object
125 thread = process.GetThreadAtIndex(0)
126
127 # Make sure the thread is in the stopped state.
128 self.assertTrue(thread.IsStopped(), "Thread state isn't \'stopped\' during breakpoint 1.")
129 self.assertFalse(thread.IsSuspended(), "Thread state is \'suspended\' during breakpoint 1.")
130
131 # Kill the process
132 self.runCmd("process kill")
133
134 def thread_state_after_continue_test(self):
135 """Test thread state after continue."""
136 exe = os.path.join(os.getcwd(), "a.out")
137 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
138
139 # This should create a breakpoint in the main thread.
140 lldbutil.run_break_set_by_file_and_line (self, "main.c", self.break_1, num_expected_locations=1)
141 lldbutil.run_break_set_by_file_and_line (self, "main.c", self.break_2, num_expected_locations=1)
142
143 # The breakpoint list should show 1 breakpoints with 1 location.
144 self.expect("breakpoint list -f", "Breakpoint location shown correctly",
Matt Kopec36e5a7d2013-06-17 19:00:31 +0000145 substrs = ["1: file = 'main.c', line = %d, locations = 1" % self.break_1])
Andrew Kaylor9f6c5352013-04-30 23:39:14 +0000146
147 # Run the program.
148 self.runCmd("run", RUN_SUCCEEDED)
149
150 # The stop reason of the thread should be breakpoint.
151 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
152 substrs = ['stopped',
153 '* thread #1',
154 'stop reason = breakpoint'])
155
156 # Get the target process
157 target = self.dbg.GetSelectedTarget()
158 process = target.GetProcess()
159
160 # Get the number of threads
161 num_threads = process.GetNumThreads()
162
163 self.assertTrue(num_threads == 1, 'Number of expected threads and actual threads do not match.')
164
165 # Get the thread object
166 thread = process.GetThreadAtIndex(0)
167
168 # Continue, the inferior will go into an infinite loop waiting for 'g_test' to change.
169 self.dbg.SetAsync(True)
170 self.runCmd("continue")
171 time.sleep(1)
172
173 # Check the thread state. It should be running.
174 self.assertFalse(thread.IsStopped(), "Thread state is \'stopped\' when it should be running.")
175 self.assertFalse(thread.IsSuspended(), "Thread state is \'suspended\' when it should be running.")
176
177 # Go back to synchronous interactions
178 self.dbg.SetAsync(False)
179
180 # Kill the process
181 self.runCmd("process kill")
182
183 def thread_state_after_expression_test(self):
184 """Test thread state after expression."""
185 exe = os.path.join(os.getcwd(), "a.out")
186 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
187
188 # This should create a breakpoint in the main thread.
189 lldbutil.run_break_set_by_file_and_line (self, "main.c", self.break_1, num_expected_locations=1)
190 lldbutil.run_break_set_by_file_and_line (self, "main.c", self.break_2, num_expected_locations=1)
191
192 # The breakpoint list should show 1 breakpoints with 1 location.
193 self.expect("breakpoint list -f", "Breakpoint location shown correctly",
Matt Kopec36e5a7d2013-06-17 19:00:31 +0000194 substrs = ["1: file = 'main.c', line = %d, locations = 1" % self.break_1])
Andrew Kaylor9f6c5352013-04-30 23:39:14 +0000195
196 # Run the program.
197 self.runCmd("run", RUN_SUCCEEDED)
198
199 # The stop reason of the thread should be breakpoint.
200 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
201 substrs = ['stopped',
202 '* thread #1',
203 'stop reason = breakpoint'])
204
205 # Get the target process
206 target = self.dbg.GetSelectedTarget()
207 process = target.GetProcess()
208
209 # Get the number of threads
210 num_threads = process.GetNumThreads()
211
212 self.assertTrue(num_threads == 1, 'Number of expected threads and actual threads do not match.')
213
214 # Get the thread object
215 thread = process.GetThreadAtIndex(0)
216
217 # Get the inferior out of its loop
218 self.runCmd("expression g_test = 1")
219
220 # Check the thread state
221 self.assertTrue(thread.IsStopped(), "Thread state isn't \'stopped\' after expression evaluation.")
222 self.assertFalse(thread.IsSuspended(), "Thread state is \'suspended\' after expression evaluation.")
223
224 # Let the process run to completion
225 self.runCmd("process continue")
226
227
228 def process_interrupt_test(self):
229 """Test process interrupt and continue."""
230 exe = os.path.join(os.getcwd(), "a.out")
231 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
232
233 # This should create a breakpoint in the main thread.
234 lldbutil.run_break_set_by_file_and_line (self, "main.c", self.break_1, num_expected_locations=1)
235
236 # The breakpoint list should show 1 breakpoints with 1 location.
237 self.expect("breakpoint list -f", "Breakpoint location shown correctly",
Matt Kopec36e5a7d2013-06-17 19:00:31 +0000238 substrs = ["1: file = 'main.c', line = %d, locations = 1" % self.break_1])
Andrew Kaylor9f6c5352013-04-30 23:39:14 +0000239
240 # Run the program.
241 self.runCmd("run", RUN_SUCCEEDED)
242
243 # The stop reason of the thread should be breakpoint.
244 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
245 substrs = ['stopped',
246 '* thread #1',
247 'stop reason = breakpoint'])
248
249 # Get the target process
250 target = self.dbg.GetSelectedTarget()
251 process = target.GetProcess()
252
253 # Get the number of threads
254 num_threads = process.GetNumThreads()
255
256 self.assertTrue(num_threads == 1, 'Number of expected threads and actual threads do not match.')
257
258 # Continue, the inferior will go into an infinite loop waiting for 'g_test' to change.
259 self.dbg.SetAsync(True)
260 self.runCmd("continue")
261 time.sleep(1)
262
263 # Go back to synchronous interactions
264 self.dbg.SetAsync(False)
265
266 # Stop the process
267 self.runCmd("process interrupt")
268
269 # The stop reason of the thread should be signal.
270 self.expect("process status", STOPPED_DUE_TO_SIGNAL,
271 substrs = ['stopped',
272 '* thread #1',
273 'stop reason = signal'])
274
275 # Get the inferior out of its loop
276 self.runCmd("expression g_test = 1")
277
278 # Run to completion
279 self.runCmd("continue")
280
Andrew Kaylordbbe36b2013-04-23 21:42:03 +0000281 def thread_states_test(self):
Andrew Kaylor9f6c5352013-04-30 23:39:14 +0000282 """Test thread states (comprehensive)."""
Andrew Kaylordbbe36b2013-04-23 21:42:03 +0000283 exe = os.path.join(os.getcwd(), "a.out")
284 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
285
286 # This should create a breakpoint in the main thread.
287 lldbutil.run_break_set_by_file_and_line (self, "main.c", self.break_1, num_expected_locations=1)
288 lldbutil.run_break_set_by_file_and_line (self, "main.c", self.break_2, num_expected_locations=1)
289
290 # The breakpoint list should show 2 breakpoints with 1 location each.
291 self.expect("breakpoint list -f", "Breakpoint location shown correctly",
Matt Kopec36e5a7d2013-06-17 19:00:31 +0000292 substrs = ["1: file = 'main.c', line = %d, locations = 1" % self.break_1,
293 "2: file = 'main.c', line = %d, locations = 1" % self.break_2])
Andrew Kaylordbbe36b2013-04-23 21:42:03 +0000294
295 # Run the program.
296 self.runCmd("run", RUN_SUCCEEDED)
297
298 # The stop reason of the thread should be breakpoint.
299 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
300 substrs = ['stopped',
301 '* thread #1',
302 'stop reason = breakpoint'])
303
304 # Get the target process
305 target = self.dbg.GetSelectedTarget()
306 process = target.GetProcess()
307
308 # Get the number of threads
309 num_threads = process.GetNumThreads()
310
311 self.assertTrue(num_threads == 1, 'Number of expected threads and actual threads do not match.')
312
313 # Get the thread object
314 thread = process.GetThreadAtIndex(0)
315
316 # Make sure the thread is in the stopped state.
317 self.assertTrue(thread.IsStopped(), "Thread state isn't \'stopped\' during breakpoint 1.")
318 self.assertFalse(thread.IsSuspended(), "Thread state is \'suspended\' during breakpoint 1.")
319
320 # Continue, the inferior will go into an infinite loop waiting for 'g_test' to change.
321 self.dbg.SetAsync(True)
322 self.runCmd("continue")
323 time.sleep(1)
324
325 # Check the thread state. It should be running.
326 self.assertFalse(thread.IsStopped(), "Thread state is \'stopped\' when it should be running.")
327 self.assertFalse(thread.IsSuspended(), "Thread state is \'suspended\' when it should be running.")
328
Andrew Kaylordbbe36b2013-04-23 21:42:03 +0000329 # Go back to synchronous interactions
330 self.dbg.SetAsync(False)
331
332 # Stop the process
333 self.runCmd("process interrupt")
334
335 # The stop reason of the thread should be signal.
336 self.expect("process status", STOPPED_DUE_TO_SIGNAL,
337 substrs = ['stopped',
338 '* thread #1',
339 'stop reason = signal'])
340
Andrew Kaylordbbe36b2013-04-23 21:42:03 +0000341 # Check the thread state
342 self.assertTrue(thread.IsStopped(), "Thread state isn't \'stopped\' after process stop.")
343 self.assertFalse(thread.IsSuspended(), "Thread state is \'suspended\' after process stop.")
344
345 # Get the inferior out of its loop
346 self.runCmd("expression g_test = 1")
347
348 # Check the thread state
349 self.assertTrue(thread.IsStopped(), "Thread state isn't \'stopped\' after expression evaluation.")
350 self.assertFalse(thread.IsSuspended(), "Thread state is \'suspended\' after expression evaluation.")
351
Andrew Kaylor9f6c5352013-04-30 23:39:14 +0000352 # The stop reason of the thread should be signal.
353 self.expect("process status", STOPPED_DUE_TO_SIGNAL,
354 substrs = ['stopped',
355 '* thread #1',
356 'stop reason = signal'])
357
Andrew Kaylordbbe36b2013-04-23 21:42:03 +0000358 # Run to breakpoint 2
359 self.runCmd("continue")
360
Andrew Kaylor9f6c5352013-04-30 23:39:14 +0000361 # The stop reason of the thread should be breakpoint.
362 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
363 substrs = ['stopped',
364 '* thread #1',
365 'stop reason = breakpoint'])
366
Andrew Kaylordbbe36b2013-04-23 21:42:03 +0000367 # Make sure both threads are stopped
368 self.assertTrue(thread.IsStopped(), "Thread state isn't \'stopped\' during breakpoint 2.")
369 self.assertFalse(thread.IsSuspended(), "Thread state is \'suspended\' during breakpoint 2.")
370
371 # Run to completion
372 self.runCmd("continue")
373
374 # At this point, the inferior process should have exited.
375 self.assertTrue(process.GetState() == lldb.eStateExited, PROCESS_EXITED)
376
377if __name__ == '__main__':
378 import atexit
379 lldb.SBDebugger.Initialize()
380 atexit.register(lambda: lldb.SBDebugger.Terminate())
381 unittest2.main()