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    @skipIfWindows
18    @skipIfRemote
19    def test_set_and_clear(self):
20        '''Tests setting and clearing function breakpoints.
21           This packet is a bit tricky on the debug adaptor side since there
22           is no "clearFunction Breakpoints" packet. Function breakpoints
23           are set by sending a "setFunctionBreakpoints" packet with zero or
24           more function names. If function breakpoints have been set before,
25           any existing breakpoints must remain set, and any new breakpoints
26           must be created, and any breakpoints that were in previous requests
27           and are not in the current request must be removed. This function
28           tests this setting and clearing and makes sure things happen
29           correctly. It doesn't test hitting breakpoints and the functionality
30           of each breakpoint, like 'conditions' and 'hitCondition' settings.
31        '''
32        # Visual Studio Code Debug Adaptors have no way to specify the file
33        # without launching or attaching to a process, so we must start a
34        # process in order to be able to set breakpoints.
35        program = self.getBuildArtifact("a.out")
36        self.build_and_launch(program)
37        bp_id_12 = None
38        functions = ['twelve']
39        # Set a function breakpoint at 'twelve'
40        response = self.vscode.request_setFunctionBreakpoints(functions)
41        if response:
42            breakpoints = response['body']['breakpoints']
43            self.assertEquals(len(breakpoints), len(functions),
44                            "expect %u source breakpoints" % (len(functions)))
45            for breakpoint in breakpoints:
46                bp_id_12 = breakpoint['id']
47                self.assertTrue(breakpoint['verified'],
48                                "expect breakpoint verified")
49
50        # Add an extra name and make sure we have two breakpoints after this
51        functions.append('thirteen')
52        response = self.vscode.request_setFunctionBreakpoints(functions)
53        if response:
54            breakpoints = response['body']['breakpoints']
55            self.assertEquals(len(breakpoints), len(functions),
56                            "expect %u source breakpoints" % (len(functions)))
57            for breakpoint in breakpoints:
58                self.assertTrue(breakpoint['verified'],
59                                "expect breakpoint verified")
60
61        # There is no breakpoint delete packet, clients just send another
62        # setFunctionBreakpoints packet with the different function names.
63        functions.remove('thirteen')
64        response = self.vscode.request_setFunctionBreakpoints(functions)
65        if response:
66            breakpoints = response['body']['breakpoints']
67            self.assertEquals(len(breakpoints), len(functions),
68                            "expect %u source breakpoints" % (len(functions)))
69            for breakpoint in breakpoints:
70                bp_id = breakpoint['id']
71                self.assertEquals(bp_id, bp_id_12,
72                                'verify "twelve" breakpoint ID is same')
73                self.assertTrue(breakpoint['verified'],
74                                "expect breakpoint still verified")
75
76        # Now get the full list of breakpoints set in the target and verify
77        # we have only 1 breakpoints set. The response above could have told
78        # us about 1 breakpoints, but we want to make sure we don't have the
79        # second one still set in the target
80        response = self.vscode.request_testGetTargetBreakpoints()
81        if response:
82            breakpoints = response['body']['breakpoints']
83            self.assertEquals(len(breakpoints), len(functions),
84                            "expect %u source breakpoints" % (len(functions)))
85            for breakpoint in breakpoints:
86                bp_id = breakpoint['id']
87                self.assertEquals(bp_id, bp_id_12,
88                                'verify "twelve" breakpoint ID is same')
89                self.assertTrue(breakpoint['verified'],
90                                "expect breakpoint still verified")
91
92        # Now clear all breakpoints for the source file by passing down an
93        # empty lines array
94        functions = []
95        response = self.vscode.request_setFunctionBreakpoints(functions)
96        if response:
97            breakpoints = response['body']['breakpoints']
98            self.assertEquals(len(breakpoints), len(functions),
99                            "expect %u source breakpoints" % (len(functions)))
100
101        # Verify with the target that all breakpoints have been cleared
102        response = self.vscode.request_testGetTargetBreakpoints()
103        if response:
104            breakpoints = response['body']['breakpoints']
105            self.assertEquals(len(breakpoints), len(functions),
106                            "expect %u source breakpoints" % (len(functions)))
107
108    @skipIfWindows
109    @skipIfRemote
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 hitCondition 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