blob: 06225f1eefc8f22e47f7a47e953c1f72b63cd4fc [file] [log] [blame]
Zachary Turnerfbb79bb2015-09-02 16:47:01 +00001"""
Andrew Kaylordbbe36b2013-04-23 21:42:03 +00002Test thread states.
3"""
4
Zachary Turner77db4a82015-10-22 20:06:20 +00005import lldb_shared
6
Andrew Kaylordbbe36b2013-04-23 21:42:03 +00007import unittest2
Zachary Turner77db4a82015-10-22 20:06:20 +00008import os, time
Andrew Kaylordbbe36b2013-04-23 21:42:03 +00009import lldb
10from lldbtest import *
11import lldbutil
12
Andrew Kaylor93132f52013-05-28 23:04:25 +000013class ThreadStateTestCase(TestBase):
Andrew Kaylordbbe36b2013-04-23 21:42:03 +000014
Greg Clayton4570d3e2013-12-10 23:19:29 +000015 mydir = TestBase.compute_mydir(__file__)
Andrew Kaylordbbe36b2013-04-23 21:42:03 +000016
Enrico Granata03e474b2013-11-01 00:57:53 +000017 @expectedFailureDarwin("rdar://15367566")
Chaoren Lin72b8f052015-02-03 01:51:18 +000018 @expectedFailureFreeBSD('llvm.org/pr15824')
Pavel Labathb36f9172015-06-24 14:43:20 +000019 @expectedFailureLinux("llvm.org/pr15824") # thread states not properly maintained
Zachary Turner656ef0b2015-09-02 16:47:29 +000020 @expectedFailureWindows("llvm.org/pr24668") # Breakpoints not resolved correctly
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000021 def test_state_after_breakpoint(self):
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000022 """Test thread state after breakpoint."""
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000023 self.build(dictionary=self.getBuildFlags(use_cpp11=False))
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000024 self.thread_state_after_breakpoint_test()
25
Ying Chen1135e702015-05-28 21:03:26 +000026 @skipIfDarwin # 'llvm.org/pr23669', cause Python crash randomly
27 @expectedFailureDarwin('llvm.org/pr23669')
Zachary Turnerfbb79bb2015-09-02 16:47:01 +000028 @expectedFailureWindows("llvm.org/pr24660")
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000029 def test_state_after_continue(self):
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000030 """Test thread state after continue."""
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000031 self.build(dictionary=self.getBuildFlags(use_cpp11=False))
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000032 self.thread_state_after_continue_test()
33
Ying Chen1135e702015-05-28 21:03:26 +000034 @skipIfDarwin # 'llvm.org/pr23669', cause Python crash randomly
35 @expectedFailureDarwin('llvm.org/pr23669')
Zachary Turnerfbb79bb2015-09-02 16:47:01 +000036 @expectedFailureWindows("llvm.org/pr24660")
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000037 def test_state_after_expression(self):
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000038 """Test thread state after expression."""
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000039 self.build(dictionary=self.getBuildFlags(use_cpp11=False))
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000040 self.thread_state_after_continue_test()
41
Andrew Kaylor93132f52013-05-28 23:04:25 +000042 @unittest2.expectedFailure("llvm.org/pr16712") # thread states not properly maintained
Zachary Turner656ef0b2015-09-02 16:47:29 +000043 @expectedFailureWindows("llvm.org/pr24668") # Breakpoints not resolved correctly
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000044 def test_process_interrupt(self):
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000045 """Test process interrupt."""
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000046 self.build(dictionary=self.getBuildFlags(use_cpp11=False))
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000047 self.process_interrupt_test()
48
Daniel Maleae8bdd1f2013-05-15 18:48:32 +000049 @unittest2.expectedFailure("llvm.org/pr15824") # thread states not properly maintained
Zachary Turner656ef0b2015-09-02 16:47:29 +000050 @expectedFailureWindows("llvm.org/pr24668") # Breakpoints not resolved correctly
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000051 def test_process_state(self):
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000052 """Test thread states (comprehensive)."""
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000053 self.build(dictionary=self.getBuildFlags(use_cpp11=False))
Andrew Kaylordbbe36b2013-04-23 21:42:03 +000054 self.thread_states_test()
55
56 def setUp(self):
57 # Call super's setUp().
58 TestBase.setUp(self)
59 # Find the line numbers for our breakpoints.
Zachary Turnerd1c5b6f2015-08-25 22:25:21 +000060 self.break_1 = line_number('main.cpp', '// Set first breakpoint here')
61 self.break_2 = line_number('main.cpp', '// Set second breakpoint here')
Andrew Kaylordbbe36b2013-04-23 21:42:03 +000062
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000063 def thread_state_after_breakpoint_test(self):
64 """Test thread state after breakpoint."""
65 exe = os.path.join(os.getcwd(), "a.out")
66 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
67
68 # This should create a breakpoint in the main thread.
Zachary Turnerd1c5b6f2015-08-25 22:25:21 +000069 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 +000070
71 # The breakpoint list should show 1 breakpoint with 1 location.
72 self.expect("breakpoint list -f", "Breakpoint location shown correctly",
Zachary Turnerd1c5b6f2015-08-25 22:25:21 +000073 substrs = ["1: file = 'main.cpp', line = %d, locations = 1" % self.break_1])
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000074
75 # Run the program.
Sean Callanan05834cd2015-07-01 23:56:30 +000076 self.runCmd("run", RUN_SUCCEEDED)
Andrew Kaylor9f6c5352013-04-30 23:39:14 +000077
78 # The stop reason of the thread should be breakpoint.
79 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
80 substrs = ['stopped',
81 '* thread #1',
82 'stop reason = breakpoint'])
83
84 # Get the target process
85 target = self.dbg.GetSelectedTarget()
86 process = target.GetProcess()
87
88 # Get the number of threads
89 num_threads = process.GetNumThreads()
90
91 self.assertTrue(num_threads == 1, 'Number of expected threads and actual threads do not match.')
92
93 # Get the thread object
94 thread = process.GetThreadAtIndex(0)
95
96 # Make sure the thread is in the stopped state.
97 self.assertTrue(thread.IsStopped(), "Thread state isn't \'stopped\' during breakpoint 1.")
98 self.assertFalse(thread.IsSuspended(), "Thread state is \'suspended\' during breakpoint 1.")
99
100 # Kill the process
101 self.runCmd("process kill")
102
103 def thread_state_after_continue_test(self):
104 """Test thread state after continue."""
105 exe = os.path.join(os.getcwd(), "a.out")
106 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
107
108 # This should create a breakpoint in the main thread.
Zachary Turnerd1c5b6f2015-08-25 22:25:21 +0000109 lldbutil.run_break_set_by_file_and_line (self, "main.cpp", self.break_1, num_expected_locations=1)
110 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 +0000111
112 # The breakpoint list should show 1 breakpoints with 1 location.
113 self.expect("breakpoint list -f", "Breakpoint location shown correctly",
Zachary Turnerd1c5b6f2015-08-25 22:25:21 +0000114 substrs = ["1: file = 'main.cpp', line = %d, exact_match = 0, locations = 1" % self.break_1])
Andrew Kaylor9f6c5352013-04-30 23:39:14 +0000115
116 # Run the program.
Sean Callanan05834cd2015-07-01 23:56:30 +0000117 self.runCmd("run", RUN_SUCCEEDED)
Andrew Kaylor9f6c5352013-04-30 23:39:14 +0000118
119 # The stop reason of the thread should be breakpoint.
120 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
121 substrs = ['stopped',
122 '* thread #1',
123 'stop reason = breakpoint'])
124
125 # Get the target process
126 target = self.dbg.GetSelectedTarget()
127 process = target.GetProcess()
128
129 # Get the number of threads
130 num_threads = process.GetNumThreads()
131
132 self.assertTrue(num_threads == 1, 'Number of expected threads and actual threads do not match.')
133
134 # Get the thread object
135 thread = process.GetThreadAtIndex(0)
136
137 # Continue, the inferior will go into an infinite loop waiting for 'g_test' to change.
138 self.dbg.SetAsync(True)
139 self.runCmd("continue")
140 time.sleep(1)
141
142 # Check the thread state. It should be running.
143 self.assertFalse(thread.IsStopped(), "Thread state is \'stopped\' when it should be running.")
144 self.assertFalse(thread.IsSuspended(), "Thread state is \'suspended\' when it should be running.")
145
146 # Go back to synchronous interactions
147 self.dbg.SetAsync(False)
148
149 # Kill the process
150 self.runCmd("process kill")
151
152 def thread_state_after_expression_test(self):
153 """Test thread state after expression."""
154 exe = os.path.join(os.getcwd(), "a.out")
155 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
156
157 # This should create a breakpoint in the main thread.
Zachary Turnerd1c5b6f2015-08-25 22:25:21 +0000158 lldbutil.run_break_set_by_file_and_line (self, "main.cpp", self.break_1, num_expected_locations=1)
159 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 +0000160
161 # The breakpoint list should show 1 breakpoints with 1 location.
162 self.expect("breakpoint list -f", "Breakpoint location shown correctly",
Zachary Turnerd1c5b6f2015-08-25 22:25:21 +0000163 substrs = ["1: file = 'main.cpp', line = %d, locations = 1" % self.break_1])
Andrew Kaylor9f6c5352013-04-30 23:39:14 +0000164
165 # Run the program.
Sean Callanan05834cd2015-07-01 23:56:30 +0000166 self.runCmd("run", RUN_SUCCEEDED)
Andrew Kaylor9f6c5352013-04-30 23:39:14 +0000167
168 # The stop reason of the thread should be breakpoint.
169 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
170 substrs = ['stopped',
171 '* thread #1',
172 'stop reason = breakpoint'])
173
174 # Get the target process
175 target = self.dbg.GetSelectedTarget()
176 process = target.GetProcess()
177
178 # Get the number of threads
179 num_threads = process.GetNumThreads()
180
181 self.assertTrue(num_threads == 1, 'Number of expected threads and actual threads do not match.')
182
183 # Get the thread object
184 thread = process.GetThreadAtIndex(0)
185
186 # Get the inferior out of its loop
187 self.runCmd("expression g_test = 1")
188
189 # Check the thread state
190 self.assertTrue(thread.IsStopped(), "Thread state isn't \'stopped\' after expression evaluation.")
191 self.assertFalse(thread.IsSuspended(), "Thread state is \'suspended\' after expression evaluation.")
192
193 # Let the process run to completion
194 self.runCmd("process continue")
195
196
197 def process_interrupt_test(self):
198 """Test process interrupt and continue."""
199 exe = os.path.join(os.getcwd(), "a.out")
200 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
201
202 # This should create a breakpoint in the main thread.
Zachary Turnerd1c5b6f2015-08-25 22:25:21 +0000203 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 +0000204
205 # The breakpoint list should show 1 breakpoints with 1 location.
206 self.expect("breakpoint list -f", "Breakpoint location shown correctly",
Zachary Turnerd1c5b6f2015-08-25 22:25:21 +0000207 substrs = ["1: file = 'main.cpp', line = %d, locations = 1" % self.break_1])
Andrew Kaylor9f6c5352013-04-30 23:39:14 +0000208
209 # Run the program.
Sean Callanan05834cd2015-07-01 23:56:30 +0000210 self.runCmd("run", RUN_SUCCEEDED)
Andrew Kaylor9f6c5352013-04-30 23:39:14 +0000211
212 # The stop reason of the thread should be breakpoint.
213 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
214 substrs = ['stopped',
215 '* thread #1',
216 'stop reason = breakpoint'])
217
218 # Get the target process
219 target = self.dbg.GetSelectedTarget()
220 process = target.GetProcess()
221
222 # Get the number of threads
223 num_threads = process.GetNumThreads()
224
225 self.assertTrue(num_threads == 1, 'Number of expected threads and actual threads do not match.')
226
227 # Continue, the inferior will go into an infinite loop waiting for 'g_test' to change.
228 self.dbg.SetAsync(True)
229 self.runCmd("continue")
230 time.sleep(1)
231
232 # Go back to synchronous interactions
233 self.dbg.SetAsync(False)
234
235 # Stop the process
236 self.runCmd("process interrupt")
237
238 # The stop reason of the thread should be signal.
239 self.expect("process status", STOPPED_DUE_TO_SIGNAL,
240 substrs = ['stopped',
241 '* thread #1',
242 'stop reason = signal'])
243
244 # Get the inferior out of its loop
245 self.runCmd("expression g_test = 1")
246
247 # Run to completion
248 self.runCmd("continue")
249
Andrew Kaylordbbe36b2013-04-23 21:42:03 +0000250 def thread_states_test(self):
Andrew Kaylor9f6c5352013-04-30 23:39:14 +0000251 """Test thread states (comprehensive)."""
Andrew Kaylordbbe36b2013-04-23 21:42:03 +0000252 exe = os.path.join(os.getcwd(), "a.out")
253 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
254
255 # This should create a breakpoint in the main thread.
Zachary Turnerd1c5b6f2015-08-25 22:25:21 +0000256 lldbutil.run_break_set_by_file_and_line (self, "main.cpp", self.break_1, num_expected_locations=1)
257 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 +0000258
259 # The breakpoint list should show 2 breakpoints with 1 location each.
260 self.expect("breakpoint list -f", "Breakpoint location shown correctly",
Zachary Turnerd1c5b6f2015-08-25 22:25:21 +0000261 substrs = ["1: file = 'main.cpp', line = %d, locations = 1" % self.break_1,
262 "2: file = 'main.cpp', line = %d, locations = 1" % self.break_2])
Andrew Kaylordbbe36b2013-04-23 21:42:03 +0000263
264 # Run the program.
Sean Callanan05834cd2015-07-01 23:56:30 +0000265 self.runCmd("run", RUN_SUCCEEDED)
Andrew Kaylordbbe36b2013-04-23 21:42:03 +0000266
267 # The stop reason of the thread should be breakpoint.
268 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
269 substrs = ['stopped',
270 '* thread #1',
271 'stop reason = breakpoint'])
272
273 # Get the target process
274 target = self.dbg.GetSelectedTarget()
275 process = target.GetProcess()
276
277 # Get the number of threads
278 num_threads = process.GetNumThreads()
279
280 self.assertTrue(num_threads == 1, 'Number of expected threads and actual threads do not match.')
281
282 # Get the thread object
283 thread = process.GetThreadAtIndex(0)
284
285 # Make sure the thread is in the stopped state.
286 self.assertTrue(thread.IsStopped(), "Thread state isn't \'stopped\' during breakpoint 1.")
287 self.assertFalse(thread.IsSuspended(), "Thread state is \'suspended\' during breakpoint 1.")
288
289 # Continue, the inferior will go into an infinite loop waiting for 'g_test' to change.
290 self.dbg.SetAsync(True)
291 self.runCmd("continue")
292 time.sleep(1)
293
294 # Check the thread state. It should be running.
295 self.assertFalse(thread.IsStopped(), "Thread state is \'stopped\' when it should be running.")
296 self.assertFalse(thread.IsSuspended(), "Thread state is \'suspended\' when it should be running.")
297
Andrew Kaylordbbe36b2013-04-23 21:42:03 +0000298 # Go back to synchronous interactions
299 self.dbg.SetAsync(False)
300
301 # Stop the process
302 self.runCmd("process interrupt")
303
304 # The stop reason of the thread should be signal.
305 self.expect("process status", STOPPED_DUE_TO_SIGNAL,
306 substrs = ['stopped',
307 '* thread #1',
308 'stop reason = signal'])
309
Andrew Kaylordbbe36b2013-04-23 21:42:03 +0000310 # Check the thread state
311 self.assertTrue(thread.IsStopped(), "Thread state isn't \'stopped\' after process stop.")
312 self.assertFalse(thread.IsSuspended(), "Thread state is \'suspended\' after process stop.")
313
314 # Get the inferior out of its loop
315 self.runCmd("expression g_test = 1")
316
317 # Check the thread state
318 self.assertTrue(thread.IsStopped(), "Thread state isn't \'stopped\' after expression evaluation.")
319 self.assertFalse(thread.IsSuspended(), "Thread state is \'suspended\' after expression evaluation.")
320
Andrew Kaylor9f6c5352013-04-30 23:39:14 +0000321 # The stop reason of the thread should be signal.
322 self.expect("process status", STOPPED_DUE_TO_SIGNAL,
323 substrs = ['stopped',
324 '* thread #1',
325 'stop reason = signal'])
326
Andrew Kaylordbbe36b2013-04-23 21:42:03 +0000327 # Run to breakpoint 2
328 self.runCmd("continue")
329
Andrew Kaylor9f6c5352013-04-30 23:39:14 +0000330 # The stop reason of the thread should be breakpoint.
331 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
332 substrs = ['stopped',
333 '* thread #1',
334 'stop reason = breakpoint'])
335
Andrew Kaylordbbe36b2013-04-23 21:42:03 +0000336 # Make sure both threads are stopped
337 self.assertTrue(thread.IsStopped(), "Thread state isn't \'stopped\' during breakpoint 2.")
338 self.assertFalse(thread.IsSuspended(), "Thread state is \'suspended\' during breakpoint 2.")
339
340 # Run to completion
341 self.runCmd("continue")
342
343 # At this point, the inferior process should have exited.
344 self.assertTrue(process.GetState() == lldb.eStateExited, PROCESS_EXITED)