2 Test that you can set breakpoint commands successfully with the Python API's:
5 from __future__ import print_function
9 from lldbsuite.test.decorators import *
10 from lldbsuite.test.lldbtest import *
11 from lldbsuite.test import lldbutil
15 class PythonBreakpointCommandSettingTestCase(TestBase):
17 mydir = TestBase.compute_mydir(__file__)
18 NO_DEBUG_INFO_TESTCASE = True
20 @add_test_categories(['pyapi'])
21 def test_step_out_python(self):
22 """Test stepping out using a python breakpoint command."""
24 self.do_set_python_command_from_python()
26 def test_bkpt_cmd_bad_arguments(self):
27 """Test what happens when pass structured data to a command:"""
29 self.do_bad_args_to_python_command()
33 self.main_source = "main.c"
34 self.main_source_spec = lldb.SBFileSpec(self.main_source)
36 def do_set_python_command_from_python(self):
37 exe = self.getBuildArtifact("a.out")
38 error = lldb.SBError()
40 self.target = self.dbg.CreateTarget(exe)
41 self.assertTrue(self.target, VALID_TARGET)
43 body_bkpt = self.target.BreakpointCreateBySourceRegex(
44 "Set break point at this line.", self.main_source_spec)
45 self.assertTrue(body_bkpt, VALID_BREAKPOINT)
47 func_bkpt = self.target.BreakpointCreateBySourceRegex(
48 "Set break point at this line.", self.main_source_spec)
49 self.assertTrue(func_bkpt, VALID_BREAKPOINT)
51 fancy_bkpt = self.target.BreakpointCreateBySourceRegex(
52 "Set break point at this line.", self.main_source_spec)
53 self.assertTrue(fancy_bkpt, VALID_BREAKPOINT)
55 fancier_bkpt = self.target.BreakpointCreateBySourceRegex(
56 "Set break point at this line.", self.main_source_spec)
57 self.assertTrue(fancier_bkpt, VALID_BREAKPOINT)
59 not_so_fancy_bkpt = self.target.BreakpointCreateBySourceRegex(
60 "Set break point at this line.", self.main_source_spec)
61 self.assertTrue(not_so_fancy_bkpt, VALID_BREAKPOINT)
63 # Also test that setting a source regex breakpoint with an empty file
64 # spec list sets it on all files:
65 no_files_bkpt = self.target.BreakpointCreateBySourceRegex(
66 "Set a breakpoint here", lldb.SBFileSpecList(), lldb.SBFileSpecList())
67 self.assertTrue(no_files_bkpt, VALID_BREAKPOINT)
68 num_locations = no_files_bkpt.GetNumLocations()
71 "Got at least two breakpoint locations")
74 for idx in range(0, num_locations):
75 comp_unit = no_files_bkpt.GetLocationAtIndex(idx).GetAddress().GetSymbolContext(
76 lldb.eSymbolContextCompUnit).GetCompileUnit().GetFileSpec()
77 print("Got comp unit: ", comp_unit.GetFilename())
78 if comp_unit.GetFilename() == "a.c":
80 elif comp_unit.GetFilename() == "b.c":
83 self.assertTrue(got_one_in_A, "Failed to match the pattern in A")
84 self.assertTrue(got_one_in_B, "Failed to match the pattern in B")
85 self.target.BreakpointDelete(no_files_bkpt.GetID())
87 error = lldb.SBError()
88 error = body_bkpt.SetScriptCallbackBody(
89 "import side_effect; side_effect.callback = 'callback was here'")
92 "Failed to set the script callback body: %s." %
95 self.expect("command script import --allow-reload ./bktptcmd.py")
97 func_bkpt.SetScriptCallbackFunction("bktptcmd.function")
99 extra_args = lldb.SBStructuredData()
100 stream = lldb.SBStream()
101 stream.Print('{"side_effect" : "I am fancy"}')
102 extra_args.SetFromJSON(stream)
103 error = fancy_bkpt.SetScriptCallbackFunction("bktptcmd.another_function", extra_args)
104 self.assertTrue(error.Success(), "Failed to add callback %s"%(error.GetCString()))
107 stream.Print('{"side_effect" : "I am so much fancier"}')
108 extra_args.SetFromJSON(stream)
110 # Fancier's callback is set up from the command line
111 id = fancier_bkpt.GetID()
112 self.expect("breakpoint command add -F bktptcmd.a_third_function -k side_effect -v 'I am fancier' %d"%(id))
114 # Not so fancy gets an empty extra_args:
115 empty_args = lldb.SBStructuredData()
116 error = not_so_fancy_bkpt.SetScriptCallbackFunction("bktptcmd.empty_extra_args", empty_args)
117 self.assertTrue(error.Success(), "Failed to add callback %s"%(error.GetCString()))
119 # Clear out canary variables
120 side_effect.bktptcmd = None
121 side_effect.callback = None
122 side_effect.fancy = None
123 side_effect.fancier = None
124 side_effect.not_so_fancy = None
126 # Now launch the process, and do not stop at entry point.
127 self.process = self.target.LaunchSimple(
128 None, None, self.get_process_working_directory())
130 self.assertTrue(self.process, PROCESS_IS_VALID)
132 # Now finish, and make sure the return value is correct.
133 threads = lldbutil.get_threads_stopped_at_breakpoint(
134 self.process, body_bkpt)
135 self.assertEquals(len(threads), 1, "Stopped at inner breakpoint.")
136 self.thread = threads[0]
138 self.assertEquals("callback was here", side_effect.callback)
139 self.assertEquals("function was here", side_effect.bktptcmd)
140 self.assertEquals("I am fancy", side_effect.fancy)
141 self.assertEquals("I am fancier", side_effect.fancier)
142 self.assertEquals("Not so fancy", side_effect.not_so_fancy)
144 def do_bad_args_to_python_command(self):
145 exe = self.getBuildArtifact("a.out")
146 error = lldb.SBError()
148 self.target = self.dbg.CreateTarget(exe)
149 self.assertTrue(self.target, VALID_TARGET)
152 self.expect("command script import --allow-reload ./bktptcmd.py")
154 bkpt = self.target.BreakpointCreateBySourceRegex(
155 "Set break point at this line.", self.main_source_spec)
156 self.assertTrue(bkpt, VALID_BREAKPOINT)
158 # Pass a breakpoint command function that doesn't take extra_args,
159 # but pass it extra args:
161 extra_args = lldb.SBStructuredData()
162 stream = lldb.SBStream()
163 stream.Print('{"side_effect" : "I am fancy"}')
164 extra_args.SetFromJSON(stream)
166 error = bkpt.SetScriptCallbackFunction("bktptcmd.function", extra_args)
167 self.assertTrue(error.Fail(), "Can't pass extra args if the function doesn't take them")
169 error = bkpt.SetScriptCallbackFunction("bktptcmd.useless_function", extra_args)
170 self.assertTrue(error.Fail(), "Can't pass extra args if the function has wrong number of args.")
172 error = bkpt.SetScriptCallbackFunction("bktptcmd.nosuch_function", extra_args)
173 self.assertTrue(error.Fail(), "Can't pass extra args if the function doesn't exist.")