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    def test_step_out_python(self):
22        """Test stepping out using a python breakpoint command."""
23        self.build()
24        self.do_set_python_command_from_python()
25
26    def test_bkpt_cmd_bad_arguments(self):
27        """Test what happens when pass structured data to a command:"""
28        self.build()
29        self.do_bad_args_to_python_command()
30
31    def setUp(self):
32        TestBase.setUp(self)
33        self.main_source = "main.c"
34        self.main_source_spec = lldb.SBFileSpec(self.main_source)
35
36    def do_set_python_command_from_python(self):
37        exe = self.getBuildArtifact("a.out")
38        error = lldb.SBError()
39
40        self.target = self.dbg.CreateTarget(exe)
41        self.assertTrue(self.target, VALID_TARGET)
42
43        body_bkpt = self.target.BreakpointCreateBySourceRegex(
44            "Set break point at this line.", self.main_source_spec)
45        self.assertTrue(body_bkpt, VALID_BREAKPOINT)
46
47        func_bkpt = self.target.BreakpointCreateBySourceRegex(
48            "Set break point at this line.", self.main_source_spec)
49        self.assertTrue(func_bkpt, VALID_BREAKPOINT)
50
51        fancy_bkpt = self.target.BreakpointCreateBySourceRegex(
52            "Set break point at this line.", self.main_source_spec)
53        self.assertTrue(fancy_bkpt, VALID_BREAKPOINT)
54
55        fancier_bkpt = self.target.BreakpointCreateBySourceRegex(
56            "Set break point at this line.", self.main_source_spec)
57        self.assertTrue(fancier_bkpt, VALID_BREAKPOINT)
58
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)
62
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()
69        self.assertTrue(
70            num_locations >= 2,
71            "Got at least two breakpoint locations")
72        got_one_in_A = False
73        got_one_in_B = False
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":
79                got_one_in_A = True
80            elif comp_unit.GetFilename() == "b.c":
81                got_one_in_B = True
82
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())
86
87        error = lldb.SBError()
88        error = body_bkpt.SetScriptCallbackBody(
89                "import side_effect; side_effect.callback = 'callback was here'")
90        self.assertTrue(
91            error.Success(),
92            "Failed to set the script callback body: %s." %
93            (error.GetCString()))
94
95        self.expect("command script import --allow-reload ./bktptcmd.py")
96
97        func_bkpt.SetScriptCallbackFunction("bktptcmd.function")
98
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()))
105
106        stream.Clear()
107        stream.Print('{"side_effect" : "I am so much fancier"}')
108        extra_args.SetFromJSON(stream)
109
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))
113
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()))
118
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
125
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())
129
130        self.assertTrue(self.process, PROCESS_IS_VALID)
131
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]
137
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)
143
144    def do_bad_args_to_python_command(self):
145        exe = self.getBuildArtifact("a.out")
146        error = lldb.SBError()
147
148        self.target = self.dbg.CreateTarget(exe)
149        self.assertTrue(self.target, VALID_TARGET)
150
151
152        self.expect("command script import --allow-reload ./bktptcmd.py")
153
154        bkpt = self.target.BreakpointCreateBySourceRegex(
155            "Set break point at this line.", self.main_source_spec)
156        self.assertTrue(bkpt, VALID_BREAKPOINT)
157
158        # Pass a breakpoint command function that doesn't take extra_args,
159        # but pass it extra args:
160
161        extra_args = lldb.SBStructuredData()
162        stream = lldb.SBStream()
163        stream.Print('{"side_effect" : "I am fancy"}')
164        extra_args.SetFromJSON(stream)
165
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")
168
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.")
171
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.")
174
175