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=["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 self.trace("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 def step_out_of_malloc_into_function_b(self, exe_name): 142 """Test Python SBThread.StepOut() API to step out of a malloc call where the call site is at function b().""" 143 exe = self.getBuildArtifact(exe_name) 144 145 target = self.dbg.CreateTarget(exe) 146 self.assertTrue(target, VALID_TARGET) 147 148 breakpoint = target.BreakpointCreateByName('malloc') 149 self.assertTrue(breakpoint, VALID_BREAKPOINT) 150 151 # Launch the process, and do not stop at the entry point. 152 process = target.LaunchSimple( 153 None, None, self.get_process_working_directory()) 154 155 while True: 156 thread = get_stopped_thread(process, lldb.eStopReasonBreakpoint) 157 self.assertTrue( 158 thread.IsValid(), 159 "There should be a thread stopped due to breakpoint") 160 caller_symbol = get_caller_symbol(thread) 161 if not caller_symbol: 162 self.fail( 163 "Test failed: could not locate the caller symbol of malloc") 164 165 # Our top frame may be an inlined function in malloc() (e.g., on 166 # FreeBSD). Apply a simple heuristic of stepping out until we find 167 # a non-malloc caller 168 while caller_symbol.startswith("malloc"): 169 thread.StepOut() 170 self.assertTrue(thread.IsValid(), 171 "Thread valid after stepping to outer malloc") 172 caller_symbol = get_caller_symbol(thread) 173 174 if caller_symbol == "b(int)": 175 break 176 process.Continue() 177 178 # On Linux malloc calls itself in some case. Remove the breakpoint because we don't want 179 # to hit it during step-out. 180 target.BreakpointDelete(breakpoint.GetID()) 181 182 thread.StepOut() 183 self.runCmd("thread backtrace") 184 self.assertTrue( 185 thread.GetFrameAtIndex(0).GetLineEntry().GetLine() == self.step_out_of_malloc, 186 "step out of malloc into function b is successful") 187 188 def step_over_3_times(self, exe_name): 189 """Test Python SBThread.StepOver() API.""" 190 exe = self.getBuildArtifact(exe_name) 191 192 target = self.dbg.CreateTarget(exe) 193 self.assertTrue(target, VALID_TARGET) 194 195 breakpoint = target.BreakpointCreateByLocation( 196 'main2.cpp', self.step_out_of_malloc) 197 self.assertTrue(breakpoint, VALID_BREAKPOINT) 198 self.runCmd("breakpoint list") 199 200 # Launch the process, and do not stop at the entry point. 201 process = target.LaunchSimple( 202 None, None, self.get_process_working_directory()) 203 204 self.assertTrue(process, PROCESS_IS_VALID) 205 206 # Frame #0 should be on self.step_out_of_malloc. 207 self.assertTrue(process.GetState() == lldb.eStateStopped) 208 thread = get_stopped_thread(process, lldb.eStopReasonBreakpoint) 209 self.assertTrue( 210 thread.IsValid(), 211 "There should be a thread stopped due to breakpoint condition") 212 self.runCmd("thread backtrace") 213 frame0 = thread.GetFrameAtIndex(0) 214 lineEntry = frame0.GetLineEntry() 215 self.assertTrue(lineEntry.GetLine() == self.step_out_of_malloc) 216 217 thread.StepOver() 218 thread.StepOver() 219 thread.StepOver() 220 self.runCmd("thread backtrace") 221 222 # Verify that we are stopped at the correct source line number in 223 # main2.cpp. 224 frame0 = thread.GetFrameAtIndex(0) 225 lineEntry = frame0.GetLineEntry() 226 self.assertTrue(thread.GetStopReason() == lldb.eStopReasonPlanComplete) 227 # Expected failure with clang as the compiler. 228 # rdar://problem/9223880 229 # 230 # Which has been fixed on the lldb by compensating for inaccurate line 231 # table information with r140416. 232 self.assertTrue(lineEntry.GetLine() == self.after_3_step_overs) 233 234 def run_to_address(self, exe_name): 235 """Test Python SBThread.RunToAddress() API.""" 236 exe = self.getBuildArtifact(exe_name) 237 238 target = self.dbg.CreateTarget(exe) 239 self.assertTrue(target, VALID_TARGET) 240 241 breakpoint = target.BreakpointCreateByLocation( 242 'main2.cpp', self.step_out_of_malloc) 243 self.assertTrue(breakpoint, VALID_BREAKPOINT) 244 self.runCmd("breakpoint list") 245 246 # Launch the process, and do not stop at the entry point. 247 process = target.LaunchSimple( 248 None, None, self.get_process_working_directory()) 249 250 self.assertTrue(process, PROCESS_IS_VALID) 251 252 # Frame #0 should be on self.step_out_of_malloc. 253 self.assertTrue(process.GetState() == lldb.eStateStopped) 254 thread = get_stopped_thread(process, lldb.eStopReasonBreakpoint) 255 self.assertTrue( 256 thread.IsValid(), 257 "There should be a thread stopped due to breakpoint condition") 258 self.runCmd("thread backtrace") 259 frame0 = thread.GetFrameAtIndex(0) 260 lineEntry = frame0.GetLineEntry() 261 self.assertTrue(lineEntry.GetLine() == self.step_out_of_malloc) 262 263 # Get the start/end addresses for this line entry. 264 start_addr = lineEntry.GetStartAddress().GetLoadAddress(target) 265 end_addr = lineEntry.GetEndAddress().GetLoadAddress(target) 266 if self.TraceOn(): 267 print("start addr:", hex(start_addr)) 268 print("end addr:", hex(end_addr)) 269 270 # Disable the breakpoint. 271 self.assertTrue(target.DisableAllBreakpoints()) 272 self.runCmd("breakpoint list") 273 274 thread.StepOver() 275 thread.StepOver() 276 thread.StepOver() 277 self.runCmd("thread backtrace") 278 279 # Now ask SBThread to run to the address 'start_addr' we got earlier, which 280 # corresponds to self.step_out_of_malloc line entry's start address. 281 thread.RunToAddress(start_addr) 282 self.runCmd("process status") 283 #self.runCmd("thread backtrace") 284