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.assertGreaterEqual( 80 num_locations, 2, "Got at least two breakpoint locations" 81 ) 82 got_one_in_A = False 83 got_one_in_B = False 84 for idx in range(0, num_locations): 85 comp_unit = ( 86 no_files_bkpt.GetLocationAtIndex(idx) 87 .GetAddress() 88 .GetSymbolContext(lldb.eSymbolContextCompUnit) 89 .GetCompileUnit() 90 .GetFileSpec() 91 ) 92 print("Got comp unit: ", comp_unit.GetFilename()) 93 if comp_unit.GetFilename() == "a.c": 94 got_one_in_A = True 95 elif comp_unit.GetFilename() == "b.c": 96 got_one_in_B = True 97 98 self.assertTrue(got_one_in_A, "Failed to match the pattern in A") 99 self.assertTrue(got_one_in_B, "Failed to match the pattern in B") 100 self.target.BreakpointDelete(no_files_bkpt.GetID()) 101 102 error = lldb.SBError() 103 error = body_bkpt.SetScriptCallbackBody( 104 "import side_effect; side_effect.callback = 'callback was here'" 105 ) 106 self.assertTrue( 107 error.Success(), 108 "Failed to set the script callback body: %s." % (error.GetCString()), 109 ) 110 111 self.expect("command script import --allow-reload ./bktptcmd.py") 112 113 func_bkpt.SetScriptCallbackFunction("bktptcmd.function") 114 115 extra_args = lldb.SBStructuredData() 116 stream = lldb.SBStream() 117 stream.Print('{"side_effect" : "I am fancy"}') 118 extra_args.SetFromJSON(stream) 119 error = fancy_bkpt.SetScriptCallbackFunction( 120 "bktptcmd.another_function", extra_args 121 ) 122 self.assertSuccess(error, "Failed to add callback") 123 124 stream.Clear() 125 stream.Print('{"side_effect" : "I am so much fancier"}') 126 extra_args.SetFromJSON(stream) 127 128 # Fancier's callback is set up from the command line 129 id = fancier_bkpt.GetID() 130 self.expect( 131 "breakpoint command add -F bktptcmd.a_third_function -k side_effect -v 'I am fancier' %d" 132 % (id) 133 ) 134 135 # Not so fancy gets an empty extra_args: 136 empty_args = lldb.SBStructuredData() 137 error = not_so_fancy_bkpt.SetScriptCallbackFunction( 138 "bktptcmd.empty_extra_args", empty_args 139 ) 140 self.assertSuccess(error, "Failed to add callback") 141 142 # Do list breakpoint like fancy: 143 stream.Clear() 144 stream.Print('{"side_effect" : "I come from list input"}') 145 extra_args.SetFromJSON(stream) 146 error = list_bkpt.SetScriptCallbackFunction( 147 "bktptcmd.a_list_function", extra_args 148 ) 149 self.assertSuccess(error, "Failed to add callback") 150 151 # Clear out canary variables 152 side_effect.bktptcmd = None 153 side_effect.callback = None 154 side_effect.fancy = None 155 side_effect.fancier = None 156 side_effect.not_so_fancy = None 157 side_effect.a_list_function = None 158 159 # Now launch the process, and do not stop at entry point. 160 self.process = self.target.LaunchSimple( 161 None, None, self.get_process_working_directory() 162 ) 163 164 self.assertTrue(self.process, PROCESS_IS_VALID) 165 166 # Now finish, and make sure the return value is correct. 167 threads = lldbutil.get_threads_stopped_at_breakpoint(self.process, body_bkpt) 168 self.assertEqual(len(threads), 1, "Stopped at inner breakpoint.") 169 self.thread = threads[0] 170 171 print( 172 "* Num Locations: {0} ; Hit Count {1}".format( 173 list_bkpt.GetNumLocations(), list_bkpt.GetHitCount() 174 ) 175 ) 176 self.assertEqual("callback was here", side_effect.callback) 177 self.assertEqual("function was here", side_effect.bktptcmd) 178 self.assertEqual("I am fancy", side_effect.fancy) 179 self.assertEqual("I am fancier", side_effect.fancier) 180 self.assertEqual("Not so fancy", side_effect.not_so_fancy) 181 self.assertEqual("I come from list input", side_effect.from_list) 182 183 def do_bad_args_to_python_command(self): 184 error = lldb.SBError() 185 186 self.target = self.createTestTarget() 187 188 self.expect("command script import --allow-reload ./bktptcmd.py") 189 190 bkpt = self.target.BreakpointCreateBySourceRegex( 191 "Set break point at this line.", self.main_source_spec 192 ) 193 self.assertTrue(bkpt, VALID_BREAKPOINT) 194 195 # Pass a breakpoint command function that doesn't take extra_args, 196 # but pass it extra args: 197 198 extra_args = lldb.SBStructuredData() 199 stream = lldb.SBStream() 200 stream.Print('{"side_effect" : "I am fancy"}') 201 extra_args.SetFromJSON(stream) 202 203 error = bkpt.SetScriptCallbackFunction("bktptcmd.function", extra_args) 204 self.assertTrue( 205 error.Fail(), "Can't pass extra args if the function doesn't take them" 206 ) 207 208 error = bkpt.SetScriptCallbackFunction("bktptcmd.useless_function", extra_args) 209 self.assertTrue( 210 error.Fail(), 211 "Can't pass extra args if the function has wrong number of args.", 212 ) 213 214 error = bkpt.SetScriptCallbackFunction("bktptcmd.nosuch_function", extra_args) 215 self.assertTrue( 216 error.Fail(), "Can't pass extra args if the function doesn't exist." 217 ) 218