5 from __future__ import print_function
12 from lldbsuite.test.lldbtest import *
13 import lldbsuite.test.lldbutil as lldbutil
15 class ThreadStateTestCase(TestBase):
17 mydir = TestBase.compute_mydir(__file__)
19 @expectedFailureDarwin("rdar://15367566")
20 @expectedFailureFreeBSD('llvm.org/pr15824')
21 @expectedFailureLinux("llvm.org/pr15824") # thread states not properly maintained
22 @expectedFailureWindows("llvm.org/pr24668") # Breakpoints not resolved correctly
23 def test_state_after_breakpoint(self):
24 """Test thread state after breakpoint."""
25 self.build(dictionary=self.getBuildFlags(use_cpp11=False))
26 self.thread_state_after_breakpoint_test()
28 @skipIfDarwin # 'llvm.org/pr23669', cause Python crash randomly
29 @expectedFailureDarwin('llvm.org/pr23669')
30 @expectedFailureFreeBSD('llvm.org/pr15824')
31 @expectedFailureWindows("llvm.org/pr24660")
32 def test_state_after_continue(self):
33 """Test thread state after continue."""
34 self.build(dictionary=self.getBuildFlags(use_cpp11=False))
35 self.thread_state_after_continue_test()
37 @skipIfDarwin # 'llvm.org/pr23669', cause Python crash randomly
38 @expectedFailureDarwin('llvm.org/pr23669')
39 @expectedFailureWindows("llvm.org/pr24660")
40 @unittest2.expectedFailure("llvm.org/pr16712") # thread states not properly maintained
41 def test_state_after_expression(self):
42 """Test thread state after expression."""
43 self.build(dictionary=self.getBuildFlags(use_cpp11=False))
44 self.thread_state_after_expression_test()
46 @unittest2.expectedFailure("llvm.org/pr16712") # thread states not properly maintained
47 @expectedFailureWindows("llvm.org/pr24668") # Breakpoints not resolved correctly
48 def test_process_interrupt(self):
49 """Test process interrupt."""
50 self.build(dictionary=self.getBuildFlags(use_cpp11=False))
51 self.process_interrupt_test()
53 @unittest2.expectedFailure("llvm.org/pr15824") # thread states not properly maintained
54 @expectedFailureWindows("llvm.org/pr24668") # Breakpoints not resolved correctly
55 def test_process_state(self):
56 """Test thread states (comprehensive)."""
57 self.build(dictionary=self.getBuildFlags(use_cpp11=False))
58 self.thread_states_test()
61 # Call super's setUp().
63 # Find the line numbers for our breakpoints.
64 self.break_1 = line_number('main.cpp', '// Set first breakpoint here')
65 self.break_2 = line_number('main.cpp', '// Set second breakpoint here')
67 def thread_state_after_breakpoint_test(self):
68 """Test thread state after breakpoint."""
69 exe = os.path.join(os.getcwd(), "a.out")
70 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
72 # This should create a breakpoint in the main thread.
73 lldbutil.run_break_set_by_file_and_line (self, "main.cpp", self.break_1, num_expected_locations=1)
76 self.runCmd("run", RUN_SUCCEEDED)
78 # The stop reason of the thread should be breakpoint.
79 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
82 'stop reason = breakpoint'])
84 # Get the target process
85 target = self.dbg.GetSelectedTarget()
86 process = target.GetProcess()
88 # Get the number of threads
89 num_threads = process.GetNumThreads()
91 self.assertTrue(num_threads == 1, 'Number of expected threads and actual threads do not match.')
93 # Get the thread object
94 thread = process.GetThreadAtIndex(0)
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.")
101 self.runCmd("process kill")
103 def wait_for_running_event(self):
104 listener = self.dbg.GetListener()
105 if lldb.remote_platform:
106 lldbutil.expect_state_changes(self, listener, [lldb.eStateConnected])
107 lldbutil.expect_state_changes(self, listener, [lldb.eStateRunning])
109 def thread_state_after_continue_test(self):
110 """Test thread state after continue."""
111 exe = os.path.join(os.getcwd(), "a.out")
112 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
114 # This should create a breakpoint in the main thread.
115 lldbutil.run_break_set_by_file_and_line (self, "main.cpp", self.break_1, num_expected_locations=1)
116 lldbutil.run_break_set_by_file_and_line (self, "main.cpp", self.break_2, num_expected_locations=1)
119 self.runCmd("run", RUN_SUCCEEDED)
121 # The stop reason of the thread should be breakpoint.
122 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
123 substrs = ['stopped',
125 'stop reason = breakpoint'])
127 # Get the target process
128 target = self.dbg.GetSelectedTarget()
129 process = target.GetProcess()
131 # Get the number of threads
132 num_threads = process.GetNumThreads()
134 self.assertTrue(num_threads == 1, 'Number of expected threads and actual threads do not match.')
136 # Get the thread object
137 thread = process.GetThreadAtIndex(0)
139 # Continue, the inferior will go into an infinite loop waiting for 'g_test' to change.
140 self.dbg.SetAsync(True)
141 self.runCmd("continue")
142 self.wait_for_running_event()
144 # Check the thread state. It should be running.
145 self.assertFalse(thread.IsStopped(), "Thread state is \'stopped\' when it should be running.")
146 self.assertFalse(thread.IsSuspended(), "Thread state is \'suspended\' when it should be running.")
148 # Go back to synchronous interactions
149 self.dbg.SetAsync(False)
152 self.runCmd("process kill")
154 def thread_state_after_expression_test(self):
155 """Test thread state after expression."""
156 exe = os.path.join(os.getcwd(), "a.out")
157 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
159 # This should create a breakpoint in the main thread.
160 lldbutil.run_break_set_by_file_and_line (self, "main.cpp", self.break_1, num_expected_locations=1)
161 lldbutil.run_break_set_by_file_and_line (self, "main.cpp", self.break_2, num_expected_locations=1)
164 self.runCmd("run", RUN_SUCCEEDED)
166 # The stop reason of the thread should be breakpoint.
167 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
168 substrs = ['stopped',
170 'stop reason = breakpoint'])
172 # Get the target process
173 target = self.dbg.GetSelectedTarget()
174 process = target.GetProcess()
176 # Get the number of threads
177 num_threads = process.GetNumThreads()
179 self.assertTrue(num_threads == 1, 'Number of expected threads and actual threads do not match.')
181 # Get the thread object
182 thread = process.GetThreadAtIndex(0)
184 # Get the inferior out of its loop
185 self.runCmd("expression g_test = 1")
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.")
191 # Let the process run to completion
192 self.runCmd("process continue")
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)
200 # This should create a breakpoint in the main thread.
201 lldbutil.run_break_set_by_file_and_line (self, "main.cpp", self.break_1, num_expected_locations=1)
204 self.runCmd("run", RUN_SUCCEEDED)
206 # The stop reason of the thread should be breakpoint.
207 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
208 substrs = ['stopped',
210 'stop reason = breakpoint'])
212 # Get the target process
213 target = self.dbg.GetSelectedTarget()
214 process = target.GetProcess()
216 # Get the number of threads
217 num_threads = process.GetNumThreads()
219 self.assertTrue(num_threads == 1, 'Number of expected threads and actual threads do not match.')
221 # Continue, the inferior will go into an infinite loop waiting for 'g_test' to change.
222 self.dbg.SetAsync(True)
223 self.runCmd("continue")
224 self.wait_for_running_event()
226 # Go back to synchronous interactions
227 self.dbg.SetAsync(False)
230 self.runCmd("process interrupt")
232 # The stop reason of the thread should be signal.
233 self.expect("process status", STOPPED_DUE_TO_SIGNAL,
234 substrs = ['stopped',
236 'stop reason = signal'])
238 # Get the inferior out of its loop
239 self.runCmd("expression g_test = 1")
242 self.runCmd("continue")
244 def thread_states_test(self):
245 """Test thread states (comprehensive)."""
246 exe = os.path.join(os.getcwd(), "a.out")
247 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
249 # This should create a breakpoint in the main thread.
250 lldbutil.run_break_set_by_file_and_line (self, "main.cpp", self.break_1, num_expected_locations=1)
251 lldbutil.run_break_set_by_file_and_line (self, "main.cpp", self.break_2, num_expected_locations=1)
254 self.runCmd("run", RUN_SUCCEEDED)
256 # The stop reason of the thread should be breakpoint.
257 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
258 substrs = ['stopped',
260 'stop reason = breakpoint'])
262 # Get the target process
263 target = self.dbg.GetSelectedTarget()
264 process = target.GetProcess()
266 # Get the number of threads
267 num_threads = process.GetNumThreads()
269 self.assertTrue(num_threads == 1, 'Number of expected threads and actual threads do not match.')
271 # Get the thread object
272 thread = process.GetThreadAtIndex(0)
274 # Make sure the thread is in the stopped state.
275 self.assertTrue(thread.IsStopped(), "Thread state isn't \'stopped\' during breakpoint 1.")
276 self.assertFalse(thread.IsSuspended(), "Thread state is \'suspended\' during breakpoint 1.")
278 # Continue, the inferior will go into an infinite loop waiting for 'g_test' to change.
279 self.dbg.SetAsync(True)
280 self.runCmd("continue")
281 self.wait_for_running_event()
283 # Check the thread state. It should be running.
284 self.assertFalse(thread.IsStopped(), "Thread state is \'stopped\' when it should be running.")
285 self.assertFalse(thread.IsSuspended(), "Thread state is \'suspended\' when it should be running.")
287 # Go back to synchronous interactions
288 self.dbg.SetAsync(False)
291 self.runCmd("process interrupt")
293 # The stop reason of the thread should be signal.
294 self.expect("process status", STOPPED_DUE_TO_SIGNAL,
295 substrs = ['stopped',
297 'stop reason = signal'])
299 # Check the thread state
300 self.assertTrue(thread.IsStopped(), "Thread state isn't \'stopped\' after process stop.")
301 self.assertFalse(thread.IsSuspended(), "Thread state is \'suspended\' after process stop.")
303 # Get the inferior out of its loop
304 self.runCmd("expression g_test = 1")
306 # Check the thread state
307 self.assertTrue(thread.IsStopped(), "Thread state isn't \'stopped\' after expression evaluation.")
308 self.assertFalse(thread.IsSuspended(), "Thread state is \'suspended\' after expression evaluation.")
310 # The stop reason of the thread should be signal.
311 self.expect("process status", STOPPED_DUE_TO_SIGNAL,
312 substrs = ['stopped',
314 'stop reason = signal'])
316 # Run to breakpoint 2
317 self.runCmd("continue")
319 # The stop reason of the thread should be breakpoint.
320 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
321 substrs = ['stopped',
323 'stop reason = breakpoint'])
325 # Make sure both threads are stopped
326 self.assertTrue(thread.IsStopped(), "Thread state isn't \'stopped\' during breakpoint 2.")
327 self.assertFalse(thread.IsSuspended(), "Thread state is \'suspended\' during breakpoint 2.")
330 self.runCmd("continue")
332 # At this point, the inferior process should have exited.
333 self.assertTrue(process.GetState() == lldb.eStateExited, PROCESS_EXITED)