blob: e1f29a40b05e01a09c6c1b44de06ea5f2adf69e8 [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
Enrico Granata03e474b2013-11-01 00:57:53 +000015 @expectedFailureDarwin("rdar://15367566")
Chaoren Lin72b8f052015-02-03 01:51:18 +000016 @expectedFailureFreeBSD('llvm.org/pr15824')
Pavel Labathb36f9172015-06-24 14:43:20 +000017 @expectedFailureLinux("llvm.org/pr15824") # thread states not properly maintained
Zachary Turner656ef0b2015-09-02 16:47:29 +000018 @expectedFailureWindows("llvm.org/pr24668") # Breakpoints not resolved correctly
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000019 def test_state_after_breakpoint(self):
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000020 """Test thread state after breakpoint."""
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000021 self.build(dictionary=self.getBuildFlags(use_cpp11=False))
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000022 self.thread_state_after_breakpoint_test()
23
Ying Chen1135e702015-05-28 21:03:26 +000024 @skipIfDarwin # 'llvm.org/pr23669', cause Python crash randomly
25 @expectedFailureDarwin('llvm.org/pr23669')
Zachary Turnerfbb79bb2015-09-02 16:47:01 +000026 @expectedFailureWindows("llvm.org/pr24660")
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000027 def test_state_after_continue(self):
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000028 """Test thread state after continue."""
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000029 self.build(dictionary=self.getBuildFlags(use_cpp11=False))
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000030 self.thread_state_after_continue_test()
31
Ying Chen1135e702015-05-28 21:03:26 +000032 @skipIfDarwin # 'llvm.org/pr23669', cause Python crash randomly
33 @expectedFailureDarwin('llvm.org/pr23669')
Zachary Turnerfbb79bb2015-09-02 16:47:01 +000034 @expectedFailureWindows("llvm.org/pr24660")
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000035 def test_state_after_expression(self):
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000036 """Test thread state after expression."""
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000037 self.build(dictionary=self.getBuildFlags(use_cpp11=False))
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000038 self.thread_state_after_continue_test()
39
Andrew Kaylor93132f52013-05-28 23:04:25 +000040 @unittest2.expectedFailure("llvm.org/pr16712") # thread states not properly maintained
Zachary Turner656ef0b2015-09-02 16:47:29 +000041 @expectedFailureWindows("llvm.org/pr24668") # Breakpoints not resolved correctly
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000042 def test_process_interrupt(self):
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000043 """Test process interrupt."""
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000044 self.build(dictionary=self.getBuildFlags(use_cpp11=False))
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000045 self.process_interrupt_test()
46
Daniel Maleae8bdd1f2013-05-15 18:48:32 +000047 @unittest2.expectedFailure("llvm.org/pr15824") # thread states not properly maintained
Zachary Turner656ef0b2015-09-02 16:47:29 +000048 @expectedFailureWindows("llvm.org/pr24668") # Breakpoints not resolved correctly
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000049 def test_process_state(self):
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000050 """Test thread states (comprehensive)."""
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000051 self.build(dictionary=self.getBuildFlags(use_cpp11=False))
Andrew Kaylordbbe36b2013-04-23 21:42:03 +000052 self.thread_states_test()
53
54 def setUp(self):
55 # Call super's setUp().
56 TestBase.setUp(self)
57 # Find the line numbers for our breakpoints.
Zachary Turnerd1c5b6f2015-08-25 22:25:21 +000058 self.break_1 = line_number('main.cpp', '// Set first breakpoint here')
59 self.break_2 = line_number('main.cpp', '// Set second breakpoint here')
Andrew Kaylordbbe36b2013-04-23 21:42:03 +000060
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000061 def thread_state_after_breakpoint_test(self):
62 """Test thread state after breakpoint."""
63 exe = os.path.join(os.getcwd(), "a.out")
64 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
65
66 # This should create a breakpoint in the main thread.
Zachary Turnerd1c5b6f2015-08-25 22:25:21 +000067 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 +000068
69 # The breakpoint list should show 1 breakpoint with 1 location.
70 self.expect("breakpoint list -f", "Breakpoint location shown correctly",
Zachary Turnerd1c5b6f2015-08-25 22:25:21 +000071 substrs = ["1: file = 'main.cpp', line = %d, locations = 1" % self.break_1])
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000072
73 # Run the program.
Sean Callanan05834cd2015-07-01 23:56:30 +000074 self.runCmd("run", RUN_SUCCEEDED)
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000075
76 # The stop reason of the thread should be breakpoint.
77 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
78 substrs = ['stopped',
79 '* thread #1',
80 'stop reason = breakpoint'])
81
82 # Get the target process
83 target = self.dbg.GetSelectedTarget()
84 process = target.GetProcess()
85
86 # Get the number of threads
87 num_threads = process.GetNumThreads()
88
89 self.assertTrue(num_threads == 1, 'Number of expected threads and actual threads do not match.')
90
91 # Get the thread object
92 thread = process.GetThreadAtIndex(0)
93
94 # Make sure the thread is in the stopped state.
95 self.assertTrue(thread.IsStopped(), "Thread state isn't \'stopped\' during breakpoint 1.")
96 self.assertFalse(thread.IsSuspended(), "Thread state is \'suspended\' during breakpoint 1.")
97
98 # Kill the process
99 self.runCmd("process kill")
100
101 def thread_state_after_continue_test(self):
102 """Test thread state after continue."""
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.
Zachary Turnerd1c5b6f2015-08-25 22:25:21 +0000107 lldbutil.run_break_set_by_file_and_line (self, "main.cpp", self.break_1, num_expected_locations=1)
108 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 +0000109
110 # The breakpoint list should show 1 breakpoints with 1 location.
111 self.expect("breakpoint list -f", "Breakpoint location shown correctly",
Zachary Turnerd1c5b6f2015-08-25 22:25:21 +0000112 substrs = ["1: file = 'main.cpp', line = %d, exact_match = 0, locations = 1" % self.break_1])
Andrew Kaylor9f6c5352013-04-30 23:39:14 +0000113
114 # Run the program.
Sean Callanan05834cd2015-07-01 23:56:30 +0000115 self.runCmd("run", RUN_SUCCEEDED)
Andrew Kaylor9f6c5352013-04-30 23:39:14 +0000116
117 # The stop reason of the thread should be breakpoint.
118 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
119 substrs = ['stopped',
120 '* thread #1',
121 'stop reason = breakpoint'])
122
123 # Get the target process
124 target = self.dbg.GetSelectedTarget()
125 process = target.GetProcess()
126
127 # Get the number of threads
128 num_threads = process.GetNumThreads()
129
130 self.assertTrue(num_threads == 1, 'Number of expected threads and actual threads do not match.')
131
132 # Get the thread object
133 thread = process.GetThreadAtIndex(0)
134
135 # Continue, the inferior will go into an infinite loop waiting for 'g_test' to change.
136 self.dbg.SetAsync(True)
137 self.runCmd("continue")
138 time.sleep(1)
139
140 # Check the thread state. It should be running.
141 self.assertFalse(thread.IsStopped(), "Thread state is \'stopped\' when it should be running.")
142 self.assertFalse(thread.IsSuspended(), "Thread state is \'suspended\' when it should be running.")
143
144 # Go back to synchronous interactions
145 self.dbg.SetAsync(False)
146
147 # Kill the process
148 self.runCmd("process kill")
149
150 def thread_state_after_expression_test(self):
151 """Test thread state after expression."""
152 exe = os.path.join(os.getcwd(), "a.out")
153 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
154
155 # This should create a breakpoint in the main thread.
Zachary Turnerd1c5b6f2015-08-25 22:25:21 +0000156 lldbutil.run_break_set_by_file_and_line (self, "main.cpp", self.break_1, num_expected_locations=1)
157 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 +0000158
159 # The breakpoint list should show 1 breakpoints with 1 location.
160 self.expect("breakpoint list -f", "Breakpoint location shown correctly",
Zachary Turnerd1c5b6f2015-08-25 22:25:21 +0000161 substrs = ["1: file = 'main.cpp', line = %d, locations = 1" % self.break_1])
Andrew Kaylor9f6c5352013-04-30 23:39:14 +0000162
163 # Run the program.
Sean Callanan05834cd2015-07-01 23:56:30 +0000164 self.runCmd("run", RUN_SUCCEEDED)
Andrew Kaylor9f6c5352013-04-30 23:39:14 +0000165
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.
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)
Andrew Kaylor9f6c5352013-04-30 23:39:14 +0000202
203 # The breakpoint list should show 1 breakpoints with 1 location.
204 self.expect("breakpoint list -f", "Breakpoint location shown correctly",
Zachary Turnerd1c5b6f2015-08-25 22:25:21 +0000205 substrs = ["1: file = 'main.cpp', line = %d, locations = 1" % self.break_1])
Andrew Kaylor9f6c5352013-04-30 23:39:14 +0000206
207 # Run the program.
Sean Callanan05834cd2015-07-01 23:56:30 +0000208 self.runCmd("run", RUN_SUCCEEDED)
Andrew Kaylor9f6c5352013-04-30 23:39:14 +0000209
210 # The stop reason of the thread should be breakpoint.
211 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
212 substrs = ['stopped',
213 '* thread #1',
214 'stop reason = breakpoint'])
215
216 # Get the target process
217 target = self.dbg.GetSelectedTarget()
218 process = target.GetProcess()
219
220 # Get the number of threads
221 num_threads = process.GetNumThreads()
222
223 self.assertTrue(num_threads == 1, 'Number of expected threads and actual threads do not match.')
224
225 # Continue, the inferior will go into an infinite loop waiting for 'g_test' to change.
226 self.dbg.SetAsync(True)
227 self.runCmd("continue")
228 time.sleep(1)
229
230 # Go back to synchronous interactions
231 self.dbg.SetAsync(False)
232
233 # Stop the process
234 self.runCmd("process interrupt")
235
236 # The stop reason of the thread should be signal.
237 self.expect("process status", STOPPED_DUE_TO_SIGNAL,
238 substrs = ['stopped',
239 '* thread #1',
240 'stop reason = signal'])
241
242 # Get the inferior out of its loop
243 self.runCmd("expression g_test = 1")
244
245 # Run to completion
246 self.runCmd("continue")
247
Andrew Kaylordbbe36b2013-04-23 21:42:03 +0000248 def thread_states_test(self):
Andrew Kaylor9f6c5352013-04-30 23:39:14 +0000249 """Test thread states (comprehensive)."""
Andrew Kaylordbbe36b2013-04-23 21:42:03 +0000250 exe = os.path.join(os.getcwd(), "a.out")
251 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
252
253 # This should create a breakpoint in the main thread.
Zachary Turnerd1c5b6f2015-08-25 22:25:21 +0000254 lldbutil.run_break_set_by_file_and_line (self, "main.cpp", self.break_1, num_expected_locations=1)
255 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 +0000256
257 # The breakpoint list should show 2 breakpoints with 1 location each.
258 self.expect("breakpoint list -f", "Breakpoint location shown correctly",
Zachary Turnerd1c5b6f2015-08-25 22:25:21 +0000259 substrs = ["1: file = 'main.cpp', line = %d, locations = 1" % self.break_1,
260 "2: file = 'main.cpp', line = %d, locations = 1" % self.break_2])
Andrew Kaylordbbe36b2013-04-23 21:42:03 +0000261
262 # Run the program.
Sean Callanan05834cd2015-07-01 23:56:30 +0000263 self.runCmd("run", RUN_SUCCEEDED)
Andrew Kaylordbbe36b2013-04-23 21:42:03 +0000264
265 # The stop reason of the thread should be breakpoint.
266 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
267 substrs = ['stopped',
268 '* thread #1',
269 'stop reason = breakpoint'])
270
271 # Get the target process
272 target = self.dbg.GetSelectedTarget()
273 process = target.GetProcess()
274
275 # Get the number of threads
276 num_threads = process.GetNumThreads()
277
278 self.assertTrue(num_threads == 1, 'Number of expected threads and actual threads do not match.')
279
280 # Get the thread object
281 thread = process.GetThreadAtIndex(0)
282
283 # Make sure the thread is in the stopped state.
284 self.assertTrue(thread.IsStopped(), "Thread state isn't \'stopped\' during breakpoint 1.")
285 self.assertFalse(thread.IsSuspended(), "Thread state is \'suspended\' during breakpoint 1.")
286
287 # Continue, the inferior will go into an infinite loop waiting for 'g_test' to change.
288 self.dbg.SetAsync(True)
289 self.runCmd("continue")
290 time.sleep(1)
291
292 # Check the thread state. It should be running.
293 self.assertFalse(thread.IsStopped(), "Thread state is \'stopped\' when it should be running.")
294 self.assertFalse(thread.IsSuspended(), "Thread state is \'suspended\' when it should be running.")
295
Andrew Kaylordbbe36b2013-04-23 21:42:03 +0000296 # Go back to synchronous interactions
297 self.dbg.SetAsync(False)
298
299 # Stop the process
300 self.runCmd("process interrupt")
301
302 # The stop reason of the thread should be signal.
303 self.expect("process status", STOPPED_DUE_TO_SIGNAL,
304 substrs = ['stopped',
305 '* thread #1',
306 'stop reason = signal'])
307
Andrew Kaylordbbe36b2013-04-23 21:42:03 +0000308 # Check the thread state
309 self.assertTrue(thread.IsStopped(), "Thread state isn't \'stopped\' after process stop.")
310 self.assertFalse(thread.IsSuspended(), "Thread state is \'suspended\' after process stop.")
311
312 # Get the inferior out of its loop
313 self.runCmd("expression g_test = 1")
314
315 # Check the thread state
316 self.assertTrue(thread.IsStopped(), "Thread state isn't \'stopped\' after expression evaluation.")
317 self.assertFalse(thread.IsSuspended(), "Thread state is \'suspended\' after expression evaluation.")
318
Andrew Kaylor9f6c5352013-04-30 23:39:14 +0000319 # The stop reason of the thread should be signal.
320 self.expect("process status", STOPPED_DUE_TO_SIGNAL,
321 substrs = ['stopped',
322 '* thread #1',
323 'stop reason = signal'])
324
Andrew Kaylordbbe36b2013-04-23 21:42:03 +0000325 # Run to breakpoint 2
326 self.runCmd("continue")
327
Andrew Kaylor9f6c5352013-04-30 23:39:14 +0000328 # The stop reason of the thread should be breakpoint.
329 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
330 substrs = ['stopped',
331 '* thread #1',
332 'stop reason = breakpoint'])
333
Andrew Kaylordbbe36b2013-04-23 21:42:03 +0000334 # Make sure both threads are stopped
335 self.assertTrue(thread.IsStopped(), "Thread state isn't \'stopped\' during breakpoint 2.")
336 self.assertFalse(thread.IsSuspended(), "Thread state is \'suspended\' during breakpoint 2.")
337
338 # Run to completion
339 self.runCmd("continue")
340
341 # At this point, the inferior process should have exited.
342 self.assertTrue(process.GetState() == lldb.eStateExited, PROCESS_EXITED)
343
344if __name__ == '__main__':
345 import atexit
346 lldb.SBDebugger.Initialize()
347 atexit.register(lambda: lldb.SBDebugger.Terminate())
348 unittest2.main()