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