1""" 2Test that you can set breakpoint commands successfully with the Python API's: 3""" 4 5import lldb 6from lldbsuite.test.decorators import * 7from lldbsuite.test.lldbtest import * 8from lldbsuite.test import lldbutil 9import side_effect 10 11 12class PythonBreakpointCommandSettingTestCase(TestBase): 13 NO_DEBUG_INFO_TESTCASE = True 14 15 @add_test_categories(["pyapi"]) 16 def test_step_out_python(self): 17 """Test stepping out using a python breakpoint command.""" 18 self.build() 19 self.do_set_python_command_from_python() 20 21 def test_bkpt_cmd_bad_arguments(self): 22 """Test what happens when pass structured data to a command:""" 23 self.build() 24 self.do_bad_args_to_python_command() 25 26 def setUp(self): 27 TestBase.setUp(self) 28 self.main_source = "main.c" 29 self.main_source_spec = lldb.SBFileSpec(self.main_source) 30 31 def do_set_python_command_from_python(self): 32 error = lldb.SBError() 33 34 self.target = self.createTestTarget() 35 36 body_bkpt = self.target.BreakpointCreateBySourceRegex( 37 "Set break point at this line.", self.main_source_spec 38 ) 39 self.assertTrue(body_bkpt, VALID_BREAKPOINT) 40 41 func_bkpt = self.target.BreakpointCreateBySourceRegex( 42 "Set break point at this line.", self.main_source_spec 43 ) 44 self.assertTrue(func_bkpt, VALID_BREAKPOINT) 45 46 fancy_bkpt = self.target.BreakpointCreateBySourceRegex( 47 "Set break point at this line.", self.main_source_spec 48 ) 49 self.assertTrue(fancy_bkpt, VALID_BREAKPOINT) 50 51 fancier_bkpt = self.target.BreakpointCreateBySourceRegex( 52 "Set break point at this line.", self.main_source_spec 53 ) 54 self.assertTrue(fancier_bkpt, VALID_BREAKPOINT) 55 56 # Also test the list version of this: 57 file_list = lldb.SBFileSpecList() 58 file_list.Append(self.main_source_spec) 59 module_list = lldb.SBFileSpecList() 60 module_list.Append(self.target.GetExecutable()) 61 62 list_bkpt = self.target.BreakpointCreateBySourceRegex( 63 "Set break point at this line.", module_list, file_list 64 ) 65 self.assertTrue(list_bkpt, VALID_BREAKPOINT) 66 67 not_so_fancy_bkpt = self.target.BreakpointCreateBySourceRegex( 68 "Set break point at this line.", self.main_source_spec 69 ) 70 self.assertTrue(not_so_fancy_bkpt, VALID_BREAKPOINT) 71 72 # Also test that setting a source regex breakpoint with an empty file 73 # spec list sets it on all files: 74 no_files_bkpt = self.target.BreakpointCreateBySourceRegex( 75 "Set a breakpoint here", lldb.SBFileSpecList(), lldb.SBFileSpecList() 76 ) 77 self.assertTrue(no_files_bkpt, VALID_BREAKPOINT) 78 num_locations = no_files_bkpt.GetNumLocations() 79 self.assertTrue(num_locations >= 2, "Got at least two breakpoint locations") 80 got_one_in_A = False 81 got_one_in_B = False 82 for idx in range(0, num_locations): 83 comp_unit = ( 84 no_files_bkpt.GetLocationAtIndex(idx) 85 .GetAddress() 86 .GetSymbolContext(lldb.eSymbolContextCompUnit) 87 .GetCompileUnit() 88 .GetFileSpec() 89 ) 90 print("Got comp unit: ", comp_unit.GetFilename()) 91 if comp_unit.GetFilename() == "a.c": 92 got_one_in_A = True 93 elif comp_unit.GetFilename() == "b.c": 94 got_one_in_B = True 95 96 self.assertTrue(got_one_in_A, "Failed to match the pattern in A") 97 self.assertTrue(got_one_in_B, "Failed to match the pattern in B") 98 self.target.BreakpointDelete(no_files_bkpt.GetID()) 99 100 error = lldb.SBError() 101 error = body_bkpt.SetScriptCallbackBody( 102 "import side_effect; side_effect.callback = 'callback was here'" 103 ) 104 self.assertTrue( 105 error.Success(), 106 "Failed to set the script callback body: %s." % (error.GetCString()), 107 ) 108 109 self.expect("command script import --allow-reload ./bktptcmd.py") 110 111 func_bkpt.SetScriptCallbackFunction("bktptcmd.function") 112 113 extra_args = lldb.SBStructuredData() 114 stream = lldb.SBStream() 115 stream.Print('{"side_effect" : "I am fancy"}') 116 extra_args.SetFromJSON(stream) 117 error = fancy_bkpt.SetScriptCallbackFunction( 118 "bktptcmd.another_function", extra_args 119 ) 120 self.assertSuccess(error, "Failed to add callback") 121 122 stream.Clear() 123 stream.Print('{"side_effect" : "I am so much fancier"}') 124 extra_args.SetFromJSON(stream) 125 126 # Fancier's callback is set up from the command line 127 id = fancier_bkpt.GetID() 128 self.expect( 129 "breakpoint command add -F bktptcmd.a_third_function -k side_effect -v 'I am fancier' %d" 130 % (id) 131 ) 132 133 # Not so fancy gets an empty extra_args: 134 empty_args = lldb.SBStructuredData() 135 error = not_so_fancy_bkpt.SetScriptCallbackFunction( 136 "bktptcmd.empty_extra_args", empty_args 137 ) 138 self.assertSuccess(error, "Failed to add callback") 139 140 # Do list breakpoint like fancy: 141 stream.Clear() 142 stream.Print('{"side_effect" : "I come from list input"}') 143 extra_args.SetFromJSON(stream) 144 error = list_bkpt.SetScriptCallbackFunction( 145 "bktptcmd.a_list_function", extra_args 146 ) 147 self.assertSuccess(error, "Failed to add callback") 148 149 # Clear out canary variables 150 side_effect.bktptcmd = None 151 side_effect.callback = None 152 side_effect.fancy = None 153 side_effect.fancier = None 154 side_effect.not_so_fancy = None 155 side_effect.a_list_function = None 156 157 # Now launch the process, and do not stop at entry point. 158 self.process = self.target.LaunchSimple( 159 None, None, self.get_process_working_directory() 160 ) 161 162 self.assertTrue(self.process, PROCESS_IS_VALID) 163 164 # Now finish, and make sure the return value is correct. 165 threads = lldbutil.get_threads_stopped_at_breakpoint(self.process, body_bkpt) 166 self.assertEquals(len(threads), 1, "Stopped at inner breakpoint.") 167 self.thread = threads[0] 168 169 print( 170 "* Num Locations: {0} ; Hit Count {1}".format( 171 list_bkpt.GetNumLocations(), list_bkpt.GetHitCount() 172 ) 173 ) 174 self.assertEquals("callback was here", side_effect.callback) 175 self.assertEquals("function was here", side_effect.bktptcmd) 176 self.assertEquals("I am fancy", side_effect.fancy) 177 self.assertEquals("I am fancier", side_effect.fancier) 178 self.assertEquals("Not so fancy", side_effect.not_so_fancy) 179 self.assertEquals("I come from list input", side_effect.from_list) 180 181 def do_bad_args_to_python_command(self): 182 error = lldb.SBError() 183 184 self.target = self.createTestTarget() 185 186 self.expect("command script import --allow-reload ./bktptcmd.py") 187 188 bkpt = self.target.BreakpointCreateBySourceRegex( 189 "Set break point at this line.", self.main_source_spec 190 ) 191 self.assertTrue(bkpt, VALID_BREAKPOINT) 192 193 # Pass a breakpoint command function that doesn't take extra_args, 194 # but pass it extra args: 195 196 extra_args = lldb.SBStructuredData() 197 stream = lldb.SBStream() 198 stream.Print('{"side_effect" : "I am fancy"}') 199 extra_args.SetFromJSON(stream) 200 201 error = bkpt.SetScriptCallbackFunction("bktptcmd.function", extra_args) 202 self.assertTrue( 203 error.Fail(), "Can't pass extra args if the function doesn't take them" 204 ) 205 206 error = bkpt.SetScriptCallbackFunction("bktptcmd.useless_function", extra_args) 207 self.assertTrue( 208 error.Fail(), 209 "Can't pass extra args if the function has wrong number of args.", 210 ) 211 212 error = bkpt.SetScriptCallbackFunction("bktptcmd.nosuch_function", extra_args) 213 self.assertTrue( 214 error.Fail(), "Can't pass extra args if the function doesn't exist." 215 ) 216