1"""Test stepping through ObjC method dispatch in various forms.""" 2 3from __future__ import print_function 4 5 6import lldb 7from lldbsuite.test.decorators import * 8from lldbsuite.test.lldbtest import * 9from lldbsuite.test import lldbutil 10 11 12class TestObjCStepping(TestBase): 13 14 mydir = TestBase.compute_mydir(__file__) 15 16 def setUp(self): 17 # Call super's setUp(). 18 TestBase.setUp(self) 19 # Find the line numbers that we will step to in main: 20 self.main_source = "stepping-tests.m" 21 self.source_randomMethod_line = line_number( 22 self.main_source, '// Source randomMethod start line.') 23 self.sourceBase_randomMethod_line = line_number( 24 self.main_source, '// SourceBase randomMethod start line.') 25 self.source_returnsStruct_start_line = line_number( 26 self.main_source, '// Source returnsStruct start line.') 27 self.sourceBase_returnsStruct_start_line = line_number( 28 self.main_source, '// SourceBase returnsStruct start line.') 29 self.stepped_past_nil_line = line_number( 30 self.main_source, '// Step over nil should stop here.') 31 32 @skipUnlessDarwin 33 @add_test_categories(['pyapi', 'basic_process']) 34 def test_with_python_api(self): 35 """Test stepping through ObjC method dispatch in various forms.""" 36 self.build() 37 exe = self.getBuildArtifact("a.out") 38 39 target = self.dbg.CreateTarget(exe) 40 self.assertTrue(target, VALID_TARGET) 41 42 self.main_source_spec = lldb.SBFileSpec(self.main_source) 43 44 breakpoints_to_disable = [] 45 46 break1 = target.BreakpointCreateBySourceRegex( 47 "// Set first breakpoint here.", self.main_source_spec) 48 self.assertTrue(break1, VALID_BREAKPOINT) 49 breakpoints_to_disable.append(break1) 50 51 break2 = target.BreakpointCreateBySourceRegex( 52 "// Set second breakpoint here.", self.main_source_spec) 53 self.assertTrue(break2, VALID_BREAKPOINT) 54 breakpoints_to_disable.append(break2) 55 56 break3 = target.BreakpointCreateBySourceRegex( 57 '// Set third breakpoint here.', self.main_source_spec) 58 self.assertTrue(break3, VALID_BREAKPOINT) 59 breakpoints_to_disable.append(break3) 60 61 break4 = target.BreakpointCreateBySourceRegex( 62 '// Set fourth breakpoint here.', self.main_source_spec) 63 self.assertTrue(break4, VALID_BREAKPOINT) 64 breakpoints_to_disable.append(break4) 65 66 break5 = target.BreakpointCreateBySourceRegex( 67 '// Set fifth breakpoint here.', self.main_source_spec) 68 self.assertTrue(break5, VALID_BREAKPOINT) 69 breakpoints_to_disable.append(break5) 70 71 break_returnStruct_call_super = target.BreakpointCreateBySourceRegex( 72 '// Source returnsStruct call line.', self.main_source_spec) 73 self.assertTrue(break_returnStruct_call_super, VALID_BREAKPOINT) 74 breakpoints_to_disable.append(break_returnStruct_call_super) 75 76 break_step_nil = target.BreakpointCreateBySourceRegex( 77 '// Set nil step breakpoint here.', self.main_source_spec) 78 self.assertTrue(break_step_nil, VALID_BREAKPOINT) 79 80 # Now launch the process, and do not stop at entry point. 81 process = target.LaunchSimple( 82 None, None, self.get_process_working_directory()) 83 84 self.assertTrue(process, PROCESS_IS_VALID) 85 86 # The stop reason of the thread should be breakpoint. 87 threads = lldbutil.get_threads_stopped_at_breakpoint(process, break1) 88 if len(threads) != 1: 89 self.fail("Failed to stop at breakpoint 1.") 90 91 thread = threads[0] 92 93 mySource = thread.GetFrameAtIndex(0).FindVariable("mySource") 94 self.assertTrue(mySource, "Found mySource local variable.") 95 mySource_isa = mySource.GetChildMemberWithName("isa") 96 self.assertTrue(mySource_isa, "Found mySource->isa local variable.") 97 className = mySource_isa.GetSummary() 98 99 if self.TraceOn(): 100 print(mySource_isa) 101 102 # Lets delete mySource so we can check that after stepping a child variable 103 # with no parent persists and is useful. 104 del (mySource) 105 106 # Now step in, that should leave us in the Source randomMethod: 107 thread.StepInto() 108 line_number = thread.GetFrameAtIndex(0).GetLineEntry().GetLine() 109 self.assertEqual( 110 line_number, self.source_randomMethod_line, 111 "Stepped into Source randomMethod.") 112 113 # Now step in again, through the super call, and that should leave us 114 # in the SourceBase randomMethod: 115 thread.StepInto() 116 line_number = thread.GetFrameAtIndex(0).GetLineEntry().GetLine() 117 self.assertEqual( 118 line_number, self.sourceBase_randomMethod_line, 119 "Stepped through super into SourceBase randomMethod.") 120 121 threads = lldbutil.continue_to_breakpoint(process, break2) 122 self.assertEqual( 123 len(threads), 1, 124 "Continued to second breakpoint in main.") 125 126 # Again, step in twice gets us to a stret method and a stret super 127 # call: 128 thread = threads[0] 129 thread.StepInto() 130 line_number = thread.GetFrameAtIndex(0).GetLineEntry().GetLine() 131 self.assertEqual( 132 line_number, self.source_returnsStruct_start_line, 133 "Stepped into Source returnsStruct.") 134 135 threads = lldbutil.continue_to_breakpoint( 136 process, break_returnStruct_call_super) 137 self.assertEqual( 138 len(threads), 1, 139 "Stepped to the call super line in Source returnsStruct.") 140 thread = threads[0] 141 142 thread.StepInto() 143 line_number = thread.GetFrameAtIndex(0).GetLineEntry().GetLine() 144 self.assertEqual( 145 line_number, self.sourceBase_returnsStruct_start_line, 146 "Stepped through super into SourceBase returnsStruct.") 147 148 # Cool now continue to get past the call that initializes the Observer, and then do our steps in again to see that 149 # we can find our way when we're stepping through a KVO swizzled 150 # object. 151 152 threads = lldbutil.continue_to_breakpoint(process, break3) 153 self.assertEqual( 154 len(threads), 1, 155 "Continued to third breakpoint in main, our object should now be swizzled.") 156 157 newClassName = mySource_isa.GetSummary() 158 159 if self.TraceOn(): 160 print("className is %s, newClassName is %s" % (className, newClassName)) 161 print(mySource_isa) 162 163 self.assertTrue( 164 newClassName != className, 165 "The isa did indeed change, swizzled!") 166 167 # Now step in, that should leave us in the Source randomMethod: 168 thread = threads[0] 169 thread.StepInto() 170 line_number = thread.GetFrameAtIndex(0).GetLineEntry().GetLine() 171 self.assertEqual( 172 line_number, self.source_randomMethod_line, 173 "Stepped into Source randomMethod in swizzled object.") 174 175 # Now step in again, through the super call, and that should leave us 176 # in the SourceBase randomMethod: 177 thread.StepInto() 178 line_number = thread.GetFrameAtIndex(0).GetLineEntry().GetLine() 179 self.assertEqual( 180 line_number, self.sourceBase_randomMethod_line, 181 "Stepped through super into SourceBase randomMethod in swizzled object.") 182 183 threads = lldbutil.continue_to_breakpoint(process, break4) 184 self.assertEqual( 185 len(threads), 1, 186 "Continued to fourth breakpoint in main.") 187 thread = threads[0] 188 189 # Again, step in twice gets us to a stret method and a stret super 190 # call: 191 thread.StepInto() 192 line_number = thread.GetFrameAtIndex(0).GetLineEntry().GetLine() 193 self.assertEqual( 194 line_number, self.source_returnsStruct_start_line, 195 "Stepped into Source returnsStruct in swizzled object.") 196 197 threads = lldbutil.continue_to_breakpoint( 198 process, break_returnStruct_call_super) 199 self.assertEqual( 200 len(threads), 1, 201 "Stepped to the call super line in Source returnsStruct - second time.") 202 thread = threads[0] 203 204 thread.StepInto() 205 line_number = thread.GetFrameAtIndex(0).GetLineEntry().GetLine() 206 self.assertEqual( 207 line_number, self.sourceBase_returnsStruct_start_line, 208 "Stepped through super into SourceBase returnsStruct in swizzled object.") 209 210 for bkpt in breakpoints_to_disable: 211 bkpt.SetEnabled(False) 212 213 threads = lldbutil.continue_to_breakpoint(process, break_step_nil) 214 self.assertEqual(len(threads), 1, "Continued to step nil breakpoint.") 215 216 thread.StepInto() 217 line_number = thread.GetFrameAtIndex(0).GetLineEntry().GetLine() 218 self.assertTrue( 219 line_number == self.stepped_past_nil_line, 220 "Step in over dispatch to nil stepped over.") 221