1""" 2Test SBThread APIs. 3""" 4 5from __future__ import print_function 6 7 8import lldb 9from lldbsuite.test.decorators import * 10from lldbsuite.test.lldbtest import * 11from lldbsuite.test import lldbutil 12from lldbsuite.test.lldbutil import get_stopped_thread, get_caller_symbol 13 14 15class ThreadAPITestCase(TestBase): 16 17 mydir = TestBase.compute_mydir(__file__) 18 19 @add_test_categories(['pyapi']) 20 def test_get_process(self): 21 """Test Python SBThread.GetProcess() API.""" 22 self.build() 23 self.get_process() 24 25 @add_test_categories(['pyapi']) 26 def test_get_stop_description(self): 27 """Test Python SBThread.GetStopDescription() API.""" 28 self.build() 29 self.get_stop_description() 30 31 @add_test_categories(['pyapi']) 32 def test_run_to_address(self): 33 """Test Python SBThread.RunToAddress() API.""" 34 # We build a different executable than the default build() does. 35 d = {'CXX_SOURCES': 'main2.cpp', 'EXE': self.exe_name} 36 self.build(dictionary=d) 37 self.setTearDownCleanup(dictionary=d) 38 self.run_to_address(self.exe_name) 39 40 @add_test_categories(['pyapi']) 41 @expectedFailureAll(oslist=["linux"], archs=['arm'], bugnumber="llvm.org/pr45892") 42 @expectedFailureAll(oslist=['freebsd'], bugnumber='llvm.org/pr20476') 43 @expectedFailureAll(oslist=["windows"]) 44 @expectedFailureNetBSD 45 def test_step_out_of_malloc_into_function_b(self): 46 """Test Python SBThread.StepOut() API to step out of a malloc call where the call site is at function b().""" 47 # We build a different executable than the default build() does. 48 d = {'CXX_SOURCES': 'main2.cpp', 'EXE': self.exe_name} 49 self.build(dictionary=d) 50 self.setTearDownCleanup(dictionary=d) 51 self.step_out_of_malloc_into_function_b(self.exe_name) 52 53 @add_test_categories(['pyapi']) 54 def test_step_over_3_times(self): 55 """Test Python SBThread.StepOver() API.""" 56 # We build a different executable than the default build() does. 57 d = {'CXX_SOURCES': 'main2.cpp', 'EXE': self.exe_name} 58 self.build(dictionary=d) 59 self.setTearDownCleanup(dictionary=d) 60 self.step_over_3_times(self.exe_name) 61 62 def setUp(self): 63 # Call super's setUp(). 64 TestBase.setUp(self) 65 # Find the line number within main.cpp to break inside main(). 66 self.break_line = line_number( 67 "main.cpp", "// Set break point at this line and check variable 'my_char'.") 68 # Find the line numbers within main2.cpp for 69 # step_out_of_malloc_into_function_b() and step_over_3_times(). 70 self.step_out_of_malloc = line_number( 71 "main2.cpp", "// thread step-out of malloc into function b.") 72 self.after_3_step_overs = line_number( 73 "main2.cpp", "// we should reach here after 3 step-over's.") 74 75 # We'll use the test method name as the exe_name for executable 76 # compiled from main2.cpp. 77 self.exe_name = self.testMethodName 78 79 def get_process(self): 80 """Test Python SBThread.GetProcess() API.""" 81 exe = self.getBuildArtifact("a.out") 82 83 target = self.dbg.CreateTarget(exe) 84 self.assertTrue(target, VALID_TARGET) 85 86 breakpoint = target.BreakpointCreateByLocation( 87 "main.cpp", self.break_line) 88 self.assertTrue(breakpoint, VALID_BREAKPOINT) 89 self.runCmd("breakpoint list") 90 91 # Launch the process, and do not stop at the entry point. 92 process = target.LaunchSimple( 93 None, None, self.get_process_working_directory()) 94 95 thread = get_stopped_thread(process, lldb.eStopReasonBreakpoint) 96 self.assertTrue( 97 thread.IsValid(), 98 "There should be a thread stopped due to breakpoint") 99 self.runCmd("process status") 100 101 proc_of_thread = thread.GetProcess() 102 #print("proc_of_thread:", proc_of_thread) 103 self.assertTrue(proc_of_thread.GetProcessID() 104 == process.GetProcessID()) 105 106 def get_stop_description(self): 107 """Test Python SBThread.GetStopDescription() API.""" 108 exe = self.getBuildArtifact("a.out") 109 110 target = self.dbg.CreateTarget(exe) 111 self.assertTrue(target, VALID_TARGET) 112 113 breakpoint = target.BreakpointCreateByLocation( 114 "main.cpp", self.break_line) 115 self.assertTrue(breakpoint, VALID_BREAKPOINT) 116 #self.runCmd("breakpoint list") 117 118 # Launch the process, and do not stop at the entry point. 119 process = target.LaunchSimple( 120 None, None, self.get_process_working_directory()) 121 122 thread = get_stopped_thread(process, lldb.eStopReasonBreakpoint) 123 self.assertTrue( 124 thread.IsValid(), 125 "There should be a thread stopped due to breakpoint") 126 127 # Get the stop reason. GetStopDescription expects that we pass in the size of the description 128 # we expect plus an additional byte for the null terminator. 129 130 # Test with a buffer that is exactly as large as the expected stop reason. 131 self.assertEqual("breakpoint 1.1", thread.GetStopDescription(len('breakpoint 1.1') + 1)) 132 133 # Test some smaller buffer sizes. 134 self.assertEqual("breakpoint", thread.GetStopDescription(len('breakpoint') + 1)) 135 self.assertEqual("break", thread.GetStopDescription(len('break') + 1)) 136 self.assertEqual("b", thread.GetStopDescription(len('b') + 1)) 137 138 # Test that we can pass in a much larger size and still get the right output. 139 self.assertEqual("breakpoint 1.1", thread.GetStopDescription(len('breakpoint 1.1') + 100)) 140 141 @skipIfAsan # The output looks different under ASAN. 142 def step_out_of_malloc_into_function_b(self, exe_name): 143 """Test Python SBThread.StepOut() API to step out of a malloc call where the call site is at function b().""" 144 exe = self.getBuildArtifact(exe_name) 145 146 target = self.dbg.CreateTarget(exe) 147 self.assertTrue(target, VALID_TARGET) 148 149 breakpoint = target.BreakpointCreateByName('malloc') 150 self.assertTrue(breakpoint, VALID_BREAKPOINT) 151 152 # Launch the process, and do not stop at the entry point. 153 process = target.LaunchSimple( 154 None, None, self.get_process_working_directory()) 155 156 while True: 157 thread = get_stopped_thread(process, lldb.eStopReasonBreakpoint) 158 self.assertTrue( 159 thread.IsValid(), 160 "There should be a thread stopped due to breakpoint") 161 caller_symbol = get_caller_symbol(thread) 162 if not caller_symbol: 163 self.fail( 164 "Test failed: could not locate the caller symbol of malloc") 165 166 # Our top frame may be an inlined function in malloc() (e.g., on 167 # FreeBSD). Apply a simple heuristic of stepping out until we find 168 # a non-malloc caller 169 while caller_symbol.startswith("malloc"): 170 thread.StepOut() 171 self.assertTrue(thread.IsValid(), 172 "Thread valid after stepping to outer malloc") 173 caller_symbol = get_caller_symbol(thread) 174 175 if caller_symbol == "b(int)": 176 break 177 process.Continue() 178 179 # On Linux malloc calls itself in some case. Remove the breakpoint because we don't want 180 # to hit it during step-out. 181 target.BreakpointDelete(breakpoint.GetID()) 182 183 thread.StepOut() 184 self.runCmd("thread backtrace") 185 self.assertTrue( 186 thread.GetFrameAtIndex(0).GetLineEntry().GetLine() == self.step_out_of_malloc, 187 "step out of malloc into function b is successful") 188 189 def step_over_3_times(self, exe_name): 190 """Test Python SBThread.StepOver() API.""" 191 exe = self.getBuildArtifact(exe_name) 192 193 target = self.dbg.CreateTarget(exe) 194 self.assertTrue(target, VALID_TARGET) 195 196 breakpoint = target.BreakpointCreateByLocation( 197 'main2.cpp', self.step_out_of_malloc) 198 self.assertTrue(breakpoint, VALID_BREAKPOINT) 199 self.runCmd("breakpoint list") 200 201 # Launch the process, and do not stop at the entry point. 202 process = target.LaunchSimple( 203 None, None, self.get_process_working_directory()) 204 205 self.assertTrue(process, PROCESS_IS_VALID) 206 207 # Frame #0 should be on self.step_out_of_malloc. 208 self.assertTrue(process.GetState() == lldb.eStateStopped) 209 thread = get_stopped_thread(process, lldb.eStopReasonBreakpoint) 210 self.assertTrue( 211 thread.IsValid(), 212 "There should be a thread stopped due to breakpoint condition") 213 self.runCmd("thread backtrace") 214 frame0 = thread.GetFrameAtIndex(0) 215 lineEntry = frame0.GetLineEntry() 216 self.assertTrue(lineEntry.GetLine() == self.step_out_of_malloc) 217 218 thread.StepOver() 219 thread.StepOver() 220 thread.StepOver() 221 self.runCmd("thread backtrace") 222 223 # Verify that we are stopped at the correct source line number in 224 # main2.cpp. 225 frame0 = thread.GetFrameAtIndex(0) 226 lineEntry = frame0.GetLineEntry() 227 self.assertTrue(thread.GetStopReason() == lldb.eStopReasonPlanComplete) 228 # Expected failure with clang as the compiler. 229 # rdar://problem/9223880 230 # 231 # Which has been fixed on the lldb by compensating for inaccurate line 232 # table information with r140416. 233 self.assertTrue(lineEntry.GetLine() == self.after_3_step_overs) 234 235 def run_to_address(self, exe_name): 236 """Test Python SBThread.RunToAddress() API.""" 237 exe = self.getBuildArtifact(exe_name) 238 239 target = self.dbg.CreateTarget(exe) 240 self.assertTrue(target, VALID_TARGET) 241 242 breakpoint = target.BreakpointCreateByLocation( 243 'main2.cpp', self.step_out_of_malloc) 244 self.assertTrue(breakpoint, VALID_BREAKPOINT) 245 self.runCmd("breakpoint list") 246 247 # Launch the process, and do not stop at the entry point. 248 process = target.LaunchSimple( 249 None, None, self.get_process_working_directory()) 250 251 self.assertTrue(process, PROCESS_IS_VALID) 252 253 # Frame #0 should be on self.step_out_of_malloc. 254 self.assertTrue(process.GetState() == lldb.eStateStopped) 255 thread = get_stopped_thread(process, lldb.eStopReasonBreakpoint) 256 self.assertTrue( 257 thread.IsValid(), 258 "There should be a thread stopped due to breakpoint condition") 259 self.runCmd("thread backtrace") 260 frame0 = thread.GetFrameAtIndex(0) 261 lineEntry = frame0.GetLineEntry() 262 self.assertTrue(lineEntry.GetLine() == self.step_out_of_malloc) 263 264 # Get the start/end addresses for this line entry. 265 start_addr = lineEntry.GetStartAddress().GetLoadAddress(target) 266 end_addr = lineEntry.GetEndAddress().GetLoadAddress(target) 267 if self.TraceOn(): 268 print("start addr:", hex(start_addr)) 269 print("end addr:", hex(end_addr)) 270 271 # Disable the breakpoint. 272 self.assertTrue(target.DisableAllBreakpoints()) 273 self.runCmd("breakpoint list") 274 275 thread.StepOver() 276 thread.StepOver() 277 thread.StepOver() 278 self.runCmd("thread backtrace") 279 280 # Now ask SBThread to run to the address 'start_addr' we got earlier, which 281 # corresponds to self.step_out_of_malloc line entry's start address. 282 thread.RunToAddress(start_addr) 283 self.runCmd("process status") 284 #self.runCmd("thread backtrace") 285