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 @skipIfAsan # The output looks different under ASAN. 41 @add_test_categories(['pyapi']) 42 @expectedFailureAll(oslist=["linux"], archs=['arm'], bugnumber="llvm.org/pr45892") 43 @expectedFailureAll(oslist=['freebsd'], bugnumber='llvm.org/pr20476') 44 @expectedFailureAll(oslist=["windows"]) 45 @expectedFailureNetBSD 46 def test_step_out_of_malloc_into_function_b(self): 47 """Test Python SBThread.StepOut() API to step out of a malloc call where the call site is at function b().""" 48 # We build a different executable than the default build() does. 49 d = {'CXX_SOURCES': 'main2.cpp', 'EXE': self.exe_name} 50 self.build(dictionary=d) 51 self.setTearDownCleanup(dictionary=d) 52 self.step_out_of_malloc_into_function_b(self.exe_name) 53 54 @add_test_categories(['pyapi']) 55 def test_step_over_3_times(self): 56 """Test Python SBThread.StepOver() API.""" 57 # We build a different executable than the default build() does. 58 d = {'CXX_SOURCES': 'main2.cpp', 'EXE': self.exe_name} 59 self.build(dictionary=d) 60 self.setTearDownCleanup(dictionary=d) 61 self.step_over_3_times(self.exe_name) 62 63 def setUp(self): 64 # Call super's setUp(). 65 TestBase.setUp(self) 66 # Find the line number within main.cpp to break inside main(). 67 self.break_line = line_number( 68 "main.cpp", "// Set break point at this line and check variable 'my_char'.") 69 # Find the line numbers within main2.cpp for 70 # step_out_of_malloc_into_function_b() and step_over_3_times(). 71 self.step_out_of_malloc = line_number( 72 "main2.cpp", "// thread step-out of malloc into function b.") 73 self.after_3_step_overs = line_number( 74 "main2.cpp", "// we should reach here after 3 step-over's.") 75 76 # We'll use the test method name as the exe_name for executable 77 # compiled from main2.cpp. 78 self.exe_name = self.testMethodName 79 80 def get_process(self): 81 """Test Python SBThread.GetProcess() API.""" 82 exe = self.getBuildArtifact("a.out") 83 84 target = self.dbg.CreateTarget(exe) 85 self.assertTrue(target, VALID_TARGET) 86 87 breakpoint = target.BreakpointCreateByLocation( 88 "main.cpp", self.break_line) 89 self.assertTrue(breakpoint, VALID_BREAKPOINT) 90 self.runCmd("breakpoint list") 91 92 # Launch the process, and do not stop at the entry point. 93 process = target.LaunchSimple( 94 None, None, self.get_process_working_directory()) 95 96 thread = get_stopped_thread(process, lldb.eStopReasonBreakpoint) 97 self.assertTrue( 98 thread.IsValid(), 99 "There should be a thread stopped due to breakpoint") 100 self.runCmd("process status") 101 102 proc_of_thread = thread.GetProcess() 103 self.trace("proc_of_thread:", proc_of_thread) 104 self.assertTrue(proc_of_thread.GetProcessID() 105 == process.GetProcessID()) 106 107 def get_stop_description(self): 108 """Test Python SBThread.GetStopDescription() API.""" 109 exe = self.getBuildArtifact("a.out") 110 111 target = self.dbg.CreateTarget(exe) 112 self.assertTrue(target, VALID_TARGET) 113 114 breakpoint = target.BreakpointCreateByLocation( 115 "main.cpp", self.break_line) 116 self.assertTrue(breakpoint, VALID_BREAKPOINT) 117 #self.runCmd("breakpoint list") 118 119 # Launch the process, and do not stop at the entry point. 120 process = target.LaunchSimple( 121 None, None, self.get_process_working_directory()) 122 123 thread = get_stopped_thread(process, lldb.eStopReasonBreakpoint) 124 self.assertTrue( 125 thread.IsValid(), 126 "There should be a thread stopped due to breakpoint") 127 128 # Get the stop reason. GetStopDescription expects that we pass in the size of the description 129 # we expect plus an additional byte for the null terminator. 130 131 # Test with a buffer that is exactly as large as the expected stop reason. 132 self.assertEqual("breakpoint 1.1", thread.GetStopDescription(len('breakpoint 1.1') + 1)) 133 134 # Test some smaller buffer sizes. 135 self.assertEqual("breakpoint", thread.GetStopDescription(len('breakpoint') + 1)) 136 self.assertEqual("break", thread.GetStopDescription(len('break') + 1)) 137 self.assertEqual("b", thread.GetStopDescription(len('b') + 1)) 138 139 # Test that we can pass in a much larger size and still get the right output. 140 self.assertEqual("breakpoint 1.1", thread.GetStopDescription(len('breakpoint 1.1') + 100)) 141 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