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