1"""
2Test lldb-vscode setBreakpoints request
3"""
4
5
6import unittest2
7import vscode
8from lldbsuite.test.decorators import *
9from lldbsuite.test.lldbtest import *
10from lldbsuite.test import lldbutil
11import lldbvscode_testcase
12
13
14class TestVSCode_setFunctionBreakpoints(
15        lldbvscode_testcase.VSCodeTestCaseBase):
16
17    mydir = TestBase.compute_mydir(__file__)
18
19    @skipIfWindows
20    def test_set_and_clear(self):
21        '''Tests setting and clearing function breakpoints.
22           This packet is a bit tricky on the debug adaptor side since there
23           is no "clearFunction Breakpoints" packet. Function breakpoints
24           are set by sending a "setFunctionBreakpoints" packet with zero or
25           more function names. If function breakpoints have been set before,
26           any exising breakpoints must remain set, and any new breakpoints
27           must be created, and any breakpoints that were in previous requests
28           and are not in the current request must be removed. This function
29           tests this setting and clearing and makes sure things happen
30           correctly. It doesn't test hitting breakpoints and the functionality
31           of each breakpoint, like 'conditions' and 'hitCondition' settings.
32        '''
33        # Visual Studio Code Debug Adaptors have no way to specify the file
34        # without launching or attaching to a process, so we must start a
35        # process in order to be able to set breakpoints.
36        program = self.getBuildArtifact("a.out")
37        self.build_and_launch(program)
38        bp_id_12 = None
39        functions = ['twelve']
40        # Set a function breakpoint at 'twelve'
41        response = self.vscode.request_setFunctionBreakpoints(functions)
42        if response:
43            breakpoints = response['body']['breakpoints']
44            self.assertEquals(len(breakpoints), len(functions),
45                            "expect %u source breakpoints" % (len(functions)))
46            for breakpoint in breakpoints:
47                bp_id_12 = breakpoint['id']
48                self.assertTrue(breakpoint['verified'],
49                                "expect breakpoint verified")
50
51        # Add an extra name and make sure we have two breakpoints after this
52        functions.append('thirteen')
53        response = self.vscode.request_setFunctionBreakpoints(functions)
54        if response:
55            breakpoints = response['body']['breakpoints']
56            self.assertEquals(len(breakpoints), len(functions),
57                            "expect %u source breakpoints" % (len(functions)))
58            for breakpoint in breakpoints:
59                self.assertTrue(breakpoint['verified'],
60                                "expect breakpoint verified")
61
62        # There is no breakpoint delete packet, clients just send another
63        # setFunctionBreakpoints packet with the different function names.
64        functions.remove('thirteen')
65        response = self.vscode.request_setFunctionBreakpoints(functions)
66        if response:
67            breakpoints = response['body']['breakpoints']
68            self.assertEquals(len(breakpoints), len(functions),
69                            "expect %u source breakpoints" % (len(functions)))
70            for breakpoint in breakpoints:
71                bp_id = breakpoint['id']
72                self.assertEquals(bp_id, bp_id_12,
73                                'verify "twelve" breakpoint ID is same')
74                self.assertTrue(breakpoint['verified'],
75                                "expect breakpoint still verified")
76
77        # Now get the full list of breakpoints set in the target and verify
78        # we have only 1 breakpoints set. The response above could have told
79        # us about 1 breakpoints, but we want to make sure we don't have the
80        # second one still set in the target
81        response = self.vscode.request_testGetTargetBreakpoints()
82        if response:
83            breakpoints = response['body']['breakpoints']
84            self.assertEquals(len(breakpoints), len(functions),
85                            "expect %u source breakpoints" % (len(functions)))
86            for breakpoint in breakpoints:
87                bp_id = breakpoint['id']
88                self.assertEquals(bp_id, bp_id_12,
89                                'verify "twelve" breakpoint ID is same')
90                self.assertTrue(breakpoint['verified'],
91                                "expect breakpoint still verified")
92
93        # Now clear all breakpoints for the source file by passing down an
94        # empty lines array
95        functions = []
96        response = self.vscode.request_setFunctionBreakpoints(functions)
97        if response:
98            breakpoints = response['body']['breakpoints']
99            self.assertEquals(len(breakpoints), len(functions),
100                            "expect %u source breakpoints" % (len(functions)))
101
102        # Verify with the target that all breakpoints have been cleared
103        response = self.vscode.request_testGetTargetBreakpoints()
104        if response:
105            breakpoints = response['body']['breakpoints']
106            self.assertEquals(len(breakpoints), len(functions),
107                            "expect %u source breakpoints" % (len(functions)))
108
109    @skipIfWindows
110    def test_functionality(self):
111        '''Tests hitting breakpoints and the functionality of a single
112           breakpoint, like 'conditions' and 'hitCondition' settings.'''
113
114        program = self.getBuildArtifact("a.out")
115        self.build_and_launch(program)
116        # Set a breakpoint on "twelve" with no condition and no hitCondition
117        functions = ['twelve']
118        breakpoint_ids = self.set_function_breakpoints(functions)
119
120        self.assertEquals(len(breakpoint_ids), len(functions),
121                        "expect one breakpoint")
122
123        # Verify we hit the breakpoint we just set
124        self.continue_to_breakpoints(breakpoint_ids)
125
126        # Make sure i is zero at first breakpoint
127        i = int(self.vscode.get_local_variable_value('i'))
128        self.assertEquals(i, 0, 'i != 0 after hitting breakpoint')
129
130        # Update the condition on our breakpoint
131        new_breakpoint_ids = self.set_function_breakpoints(functions,
132                                                           condition="i==4")
133        self.assertEquals(breakpoint_ids, new_breakpoint_ids,
134                        "existing breakpoint should have its condition "
135                        "updated")
136
137        self.continue_to_breakpoints(breakpoint_ids)
138        i = int(self.vscode.get_local_variable_value('i'))
139        self.assertEquals(i, 4,
140                        'i != 4 showing conditional works')
141        new_breakpoint_ids = self.set_function_breakpoints(functions,
142                                                           hitCondition="2")
143
144        self.assertEquals(breakpoint_ids, new_breakpoint_ids,
145                        "existing breakpoint should have its condition "
146                        "updated")
147
148        # Continue with a hitContidtion of 2 and expect it to skip 1 value
149        self.continue_to_breakpoints(breakpoint_ids)
150        i = int(self.vscode.get_local_variable_value('i'))
151        self.assertEquals(i, 6,
152                        'i != 6 showing hitCondition works')
153
154        # continue after hitting our hitCondition and make sure it only goes
155        # up by 1
156        self.continue_to_breakpoints(breakpoint_ids)
157        i = int(self.vscode.get_local_variable_value('i'))
158        self.assertEquals(i, 7,
159                        'i != 7 showing post hitCondition hits every time')
160