1""" 2Test stepping out from a function in a multi-threaded program. 3""" 4 5 6 7import lldb 8from lldbsuite.test.decorators import * 9from lldbsuite.test.lldbtest import * 10from lldbsuite.test import lldbutil 11 12 13class ThreadStepOutTestCase(TestBase): 14 15 @skipIfWindows # This test will hang on windows llvm.org/pr21753 16 @expectedFailureAll(oslist=["windows"]) 17 @expectedFailureNetBSD 18 def test_step_single_thread(self): 19 """Test thread step out on one thread via command interpreter. """ 20 self.build() 21 self.step_out_test(self.step_out_single_thread_with_cmd) 22 23 @skipIfWindows # This test will hang on windows llvm.org/pr21753 24 @expectedFailureAll(oslist=["windows"]) 25 @expectedFailureAll(oslist=["watchos"], archs=['armv7k'], bugnumber="rdar://problem/34674488") # stop reason is trace when it should be step-out 26 @expectedFailureNetBSD 27 def test_step_all_threads(self): 28 """Test thread step out on all threads via command interpreter. """ 29 self.build() 30 self.step_out_test(self.step_out_all_threads_with_cmd) 31 32 @skipIfWindows # This test will hang on windows llvm.org/pr21753 33 @expectedFailureAll(oslist=["windows"], bugnumber="llvm.org/pr24681") 34 @expectedFailureNetBSD 35 def test_python(self): 36 """Test thread step out on one thread via Python API (dwarf).""" 37 self.build() 38 self.step_out_test(self.step_out_with_python) 39 40 def setUp(self): 41 # Call super's setUp(). 42 TestBase.setUp(self) 43 # Find the line number for our breakpoint. 44 self.bkpt_string = '// Set breakpoint here' 45 self.breakpoint = line_number('main.cpp', self.bkpt_string) 46 self.step_in_line = line_number('main.cpp', '// But we might still be here') 47 self.step_out_dest = line_number('main.cpp', '// Expect to stop here after step-out.') 48 49 def check_stepping_thread(self): 50 zeroth_frame = self.step_out_thread.frames[0] 51 line_entry = zeroth_frame.line_entry 52 self.assertTrue(line_entry.IsValid(), "Stopped at a valid line entry") 53 self.assertEqual("main.cpp", line_entry.file.basename, "Still in main.cpp") 54 # We can't really tell whether we stay on our line 55 # or get to the next line, it depends on whether there are any 56 # instructions between the call and the return. 57 line = line_entry.line 58 self.assertTrue(line == self.step_out_dest or line == self.step_in_line, "Stepped to the wrong line: {0}".format(line)) 59 60 def step_out_single_thread_with_cmd(self): 61 other_threads = {} 62 for thread in self.process.threads: 63 if thread.GetIndexID() == self.step_out_thread.GetIndexID(): 64 continue 65 other_threads[thread.GetIndexID()] = thread.frames[0].line_entry 66 67 # There should be other threads... 68 self.assertNotEqual(len(other_threads), 0) 69 self.step_out_with_cmd("this-thread") 70 # The other threads should not have made progress: 71 for thread in self.process.threads: 72 index_id = thread.GetIndexID() 73 line_entry = other_threads.get(index_id) 74 if line_entry: 75 self.assertEqual(thread.frames[0].line_entry.file.basename, line_entry.file.basename, "Thread {0} moved by file".format(index_id)) 76 self.assertEqual(thread.frames[0].line_entry.line, line_entry.line, "Thread {0} moved by line".format(index_id)) 77 78 def step_out_all_threads_with_cmd(self): 79 self.step_out_with_cmd("all-threads") 80 81 def step_out_with_cmd(self, run_mode): 82 self.runCmd("thread select %d" % self.step_out_thread.GetIndexID()) 83 self.runCmd("thread step-out -m %s" % run_mode) 84 self.expect("process status", "Expected stop reason to be step-out", 85 substrs=["stop reason = step out"]) 86 87 selected_thread = self.process.GetSelectedThread() 88 self.assertEqual(selected_thread.GetIndexID(), self.step_out_thread.GetIndexID(), "Step out changed selected thread.") 89 self.check_stepping_thread() 90 91 def step_out_with_python(self): 92 self.step_out_thread.StepOut() 93 94 reason = self.step_out_thread.GetStopReason() 95 self.assertEqual( 96 lldb.eStopReasonPlanComplete, 97 reason, 98 "Expected thread stop reason 'plancomplete', but got '%s'" % 99 lldbutil.stop_reason_to_str(reason)) 100 self.check_stepping_thread() 101 102 103 def step_out_test(self, step_out_func): 104 """Test single thread step out of a function.""" 105 (self.inferior_target, self.process, thread, bkpt) = lldbutil.run_to_source_breakpoint( 106 self, self.bkpt_string, lldb.SBFileSpec('main.cpp'), only_one_thread = False) 107 108 # We hit the breakpoint on at least one thread. If we hit it on both threads 109 # simultaneously, we can try the step out. Otherwise, suspend the thread 110 # that hit the breakpoint, and continue till the second thread hits 111 # the breakpoint: 112 113 (breakpoint_threads, other_threads) = ([], []) 114 lldbutil.sort_stopped_threads(self.process, 115 breakpoint_threads=breakpoint_threads, 116 other_threads=other_threads) 117 if len(breakpoint_threads) == 1: 118 success = thread.Suspend() 119 self.assertTrue(success, "Couldn't suspend a thread") 120 breakpoint_threads = lldbutil.continue_to_breakpoint(self.process, 121 bkpt) 122 self.assertEqual(len(breakpoint_threads), 2, "Second thread stopped") 123 success = thread.Resume() 124 self.assertTrue(success, "Couldn't resume a thread") 125 126 self.step_out_thread = breakpoint_threads[0] 127 128 # Step out of thread stopped at breakpoint 129 step_out_func() 130