2 Test that you can set breakpoint commands successfully with the Python API's:
5 from __future__ import print_function
12 from lldbsuite.test.decorators import *
13 from lldbsuite.test.lldbtest import *
14 from lldbsuite.test import lldbutil
17 class PythonBreakpointCommandSettingTestCase(TestBase):
19 mydir = TestBase.compute_mydir(__file__)
22 @add_test_categories(['pyapi'])
23 def test_step_out_python(self):
24 """Test stepping out using avoid-no-debug with dsyms."""
26 self.do_set_python_command_from_python()
30 self.main_source = "main.c"
31 self.main_source_spec = lldb.SBFileSpec(self.main_source)
33 def do_set_python_command_from_python(self):
34 exe = os.path.join(os.getcwd(), "a.out")
35 error = lldb.SBError()
37 self.target = self.dbg.CreateTarget(exe)
38 self.assertTrue(self.target, VALID_TARGET)
40 body_bkpt = self.target.BreakpointCreateBySourceRegex(
41 "Set break point at this line.", self.main_source_spec)
42 self.assertTrue(body_bkpt, VALID_BREAKPOINT)
44 func_bkpt = self.target.BreakpointCreateBySourceRegex(
45 "Set break point at this line.", self.main_source_spec)
46 self.assertTrue(func_bkpt, VALID_BREAKPOINT)
48 # Also test that setting a source regex breakpoint with an empty file
49 # spec list sets it on all files:
50 no_files_bkpt = self.target.BreakpointCreateBySourceRegex(
51 "Set a breakpoint here", lldb.SBFileSpecList(), lldb.SBFileSpecList())
52 self.assertTrue(no_files_bkpt, VALID_BREAKPOINT)
53 num_locations = no_files_bkpt.GetNumLocations()
56 "Got at least two breakpoint locations")
59 for idx in range(0, num_locations):
60 comp_unit = no_files_bkpt.GetLocationAtIndex(idx).GetAddress().GetSymbolContext(
61 lldb.eSymbolContextCompUnit).GetCompileUnit().GetFileSpec()
62 print("Got comp unit: ", comp_unit.GetFilename())
63 if comp_unit.GetFilename() == "a.c":
65 elif comp_unit.GetFilename() == "b.c":
68 self.assertTrue(got_one_in_A, "Failed to match the pattern in A")
69 self.assertTrue(got_one_in_B, "Failed to match the pattern in B")
70 self.target.BreakpointDelete(no_files_bkpt.GetID())
72 PythonBreakpointCommandSettingTestCase.my_var = 10
73 error = lldb.SBError()
74 error = body_bkpt.SetScriptCallbackBody("\
75 import TestBreakpointCommandsFromPython\n\
76 TestBreakpointCommandsFromPython.PythonBreakpointCommandSettingTestCase.my_var = 20\n\
77 print('Hit breakpoint')")
80 "Failed to set the script callback body: %s." %
83 self.dbg.HandleCommand(
84 "command script import --allow-reload ./bktptcmd.py")
85 func_bkpt.SetScriptCallbackFunction("bktptcmd.function")
87 # We will use the function that touches a text file, so remove it
89 self.RemoveTempFile("output2.txt")
91 # Now launch the process, and do not stop at entry point.
92 self.process = self.target.LaunchSimple(
93 None, None, self.get_process_working_directory())
95 self.assertTrue(self.process, PROCESS_IS_VALID)
97 # Now finish, and make sure the return value is correct.
98 threads = lldbutil.get_threads_stopped_at_breakpoint(
99 self.process, body_bkpt)
100 self.assertTrue(len(threads) == 1, "Stopped at inner breakpoint.")
101 self.thread = threads[0]
103 self.assertTrue(PythonBreakpointCommandSettingTestCase.my_var == 20)
105 # Check for the function version as well, which produced this file:
106 # Remember to clean up after ourselves...
108 os.path.isfile("output2.txt"),
109 "'output2.txt' exists due to breakpoint command for breakpoint function.")
110 self.RemoveTempFile("output2.txt")