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