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