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 not_so_fancy_bkpt = self.target.BreakpointCreateBySourceRegex( 58 "Set break point at this line.", self.main_source_spec) 59 self.assertTrue(not_so_fancy_bkpt, VALID_BREAKPOINT) 60 61 # Also test that setting a source regex breakpoint with an empty file 62 # spec list sets it on all files: 63 no_files_bkpt = self.target.BreakpointCreateBySourceRegex( 64 "Set a breakpoint here", lldb.SBFileSpecList(), lldb.SBFileSpecList()) 65 self.assertTrue(no_files_bkpt, VALID_BREAKPOINT) 66 num_locations = no_files_bkpt.GetNumLocations() 67 self.assertTrue( 68 num_locations >= 2, 69 "Got at least two breakpoint locations") 70 got_one_in_A = False 71 got_one_in_B = False 72 for idx in range(0, num_locations): 73 comp_unit = no_files_bkpt.GetLocationAtIndex(idx).GetAddress().GetSymbolContext( 74 lldb.eSymbolContextCompUnit).GetCompileUnit().GetFileSpec() 75 print("Got comp unit: ", comp_unit.GetFilename()) 76 if comp_unit.GetFilename() == "a.c": 77 got_one_in_A = True 78 elif comp_unit.GetFilename() == "b.c": 79 got_one_in_B = True 80 81 self.assertTrue(got_one_in_A, "Failed to match the pattern in A") 82 self.assertTrue(got_one_in_B, "Failed to match the pattern in B") 83 self.target.BreakpointDelete(no_files_bkpt.GetID()) 84 85 error = lldb.SBError() 86 error = body_bkpt.SetScriptCallbackBody( 87 "import side_effect; side_effect.callback = 'callback was here'") 88 self.assertTrue( 89 error.Success(), 90 "Failed to set the script callback body: %s." % 91 (error.GetCString())) 92 93 self.expect("command script import --allow-reload ./bktptcmd.py") 94 95 func_bkpt.SetScriptCallbackFunction("bktptcmd.function") 96 97 extra_args = lldb.SBStructuredData() 98 stream = lldb.SBStream() 99 stream.Print('{"side_effect" : "I am fancy"}') 100 extra_args.SetFromJSON(stream) 101 error = fancy_bkpt.SetScriptCallbackFunction("bktptcmd.another_function", extra_args) 102 self.assertTrue(error.Success(), "Failed to add callback %s"%(error.GetCString())) 103 104 stream.Clear() 105 stream.Print('{"side_effect" : "I am so much fancier"}') 106 extra_args.SetFromJSON(stream) 107 108 # Fancier's callback is set up from the command line 109 id = fancier_bkpt.GetID() 110 self.expect("breakpoint command add -F bktptcmd.a_third_function -k side_effect -v 'I am fancier' %d"%(id)) 111 112 # Not so fancy gets an empty extra_args: 113 empty_args = lldb.SBStructuredData() 114 error = not_so_fancy_bkpt.SetScriptCallbackFunction("bktptcmd.empty_extra_args", empty_args) 115 self.assertTrue(error.Success(), "Failed to add callback %s"%(error.GetCString())) 116 117 # Clear out canary variables 118 side_effect.bktptcmd = None 119 side_effect.callback = None 120 side_effect.fancy = None 121 side_effect.fancier = None 122 side_effect.not_so_fancy = None 123 124 # Now launch the process, and do not stop at entry point. 125 self.process = self.target.LaunchSimple( 126 None, None, self.get_process_working_directory()) 127 128 self.assertTrue(self.process, PROCESS_IS_VALID) 129 130 # Now finish, and make sure the return value is correct. 131 threads = lldbutil.get_threads_stopped_at_breakpoint( 132 self.process, body_bkpt) 133 self.assertEquals(len(threads), 1, "Stopped at inner breakpoint.") 134 self.thread = threads[0] 135 136 self.assertEquals("callback was here", side_effect.callback) 137 self.assertEquals("function was here", side_effect.bktptcmd) 138 self.assertEquals("I am fancy", side_effect.fancy) 139 self.assertEquals("I am fancier", side_effect.fancier) 140 self.assertEquals("Not so fancy", side_effect.not_so_fancy) 141 142 def do_bad_args_to_python_command(self): 143 error = lldb.SBError() 144 145 self.target = self.createTestTarget() 146 147 self.expect("command script import --allow-reload ./bktptcmd.py") 148 149 bkpt = self.target.BreakpointCreateBySourceRegex( 150 "Set break point at this line.", self.main_source_spec) 151 self.assertTrue(bkpt, VALID_BREAKPOINT) 152 153 # Pass a breakpoint command function that doesn't take extra_args, 154 # but pass it extra args: 155 156 extra_args = lldb.SBStructuredData() 157 stream = lldb.SBStream() 158 stream.Print('{"side_effect" : "I am fancy"}') 159 extra_args.SetFromJSON(stream) 160 161 error = bkpt.SetScriptCallbackFunction("bktptcmd.function", extra_args) 162 self.assertTrue(error.Fail(), "Can't pass extra args if the function doesn't take them") 163 164 error = bkpt.SetScriptCallbackFunction("bktptcmd.useless_function", extra_args) 165 self.assertTrue(error.Fail(), "Can't pass extra args if the function has wrong number of args.") 166 167 error = bkpt.SetScriptCallbackFunction("bktptcmd.nosuch_function", extra_args) 168 self.assertTrue(error.Fail(), "Can't pass extra args if the function doesn't exist.") 169 170