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