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
12import os
13
14
15class TestVSCode_setBreakpoints(lldbvscode_testcase.VSCodeTestCaseBase):
16
17    mydir = TestBase.compute_mydir(__file__)
18
19    @skipIfWindows
20    @skipIfRemote
21    def test_set_and_clear(self):
22        '''Tests setting and clearing source file and line breakpoints.
23           This packet is a bit tricky on the debug adaptor side since there
24           is no "clearBreakpoints" packet. Source file and line breakpoints
25           are set by sending a "setBreakpoints" packet with a source file
26           specified and zero or more source lines. If breakpoints have been
27           set in the source file before, any exising breakpoints must remain
28           set, and any new breakpoints must be created, and any breakpoints
29           that were in previous requests and are not in the current request
30           must be removed. This function tests this setting and clearing
31           and makes sure things happen correctly. It doesn't test hitting
32           breakpoints and the functionality of each breakpoint, like
33           'conditions' and 'hitCondition' settings.'''
34        source_basename = 'main.cpp'
35        source_path = os.path.join(os.getcwd(), source_basename)
36        first_line = line_number('main.cpp', 'break 12')
37        second_line = line_number('main.cpp', 'break 13')
38        third_line = line_number('main.cpp', 'break 14')
39        lines = [first_line, third_line, second_line]
40
41        # Visual Studio Code Debug Adaptors have no way to specify the file
42        # without launching or attaching to a process, so we must start a
43        # process in order to be able to set breakpoints.
44        program = self.getBuildArtifact("a.out")
45        self.build_and_launch(program)
46
47        # Set 3 breakoints and verify that they got set correctly
48        response = self.vscode.request_setBreakpoints(source_path, lines)
49        line_to_id = {}
50        if response:
51            breakpoints = response['body']['breakpoints']
52            self.assertEquals(len(breakpoints), len(lines),
53                            "expect %u source breakpoints" % (len(lines)))
54            for (breakpoint, index) in zip(breakpoints, range(len(lines))):
55                line = breakpoint['line']
56                self.assertTrue(line, lines[index])
57                # Store the "id" of the breakpoint that was set for later
58                line_to_id[line] = breakpoint['id']
59                self.assertTrue(line in lines, "line expected in lines array")
60                self.assertTrue(breakpoint['verified'],
61                                "expect breakpoint verified")
62
63        # There is no breakpoint delete packet, clients just send another
64        # setBreakpoints packet with the same source file with fewer lines.
65        # Below we remove the second line entry and call the setBreakpoints
66        # function again. We want to verify that any breakpoints that were set
67        # before still have the same "id". This means we didn't clear the
68        # breakpoint and set it again at the same location. We also need to
69        # verify that the second line location was actually removed.
70        lines.remove(second_line)
71        # Set 2 breakoints and verify that the previous breakoints that were
72        # set above are still set.
73        response = self.vscode.request_setBreakpoints(source_path, lines)
74        if response:
75            breakpoints = response['body']['breakpoints']
76            self.assertEquals(len(breakpoints), len(lines),
77                            "expect %u source breakpoints" % (len(lines)))
78            for (breakpoint, index) in zip(breakpoints, range(len(lines))):
79                line = breakpoint['line']
80                self.assertTrue(line, lines[index])
81                # Verify the same breakpoints are still set within LLDB by
82                # making sure the breakpoint ID didn't change
83                self.assertEquals(line_to_id[line], breakpoint['id'],
84                                "verify previous breakpoints stayed the same")
85                self.assertTrue(line in lines, "line expected in lines array")
86                self.assertTrue(breakpoint['verified'],
87                                "expect breakpoint still verified")
88
89        # Now get the full list of breakpoints set in the target and verify
90        # we have only 2 breakpoints set. The response above could have told
91        # us about 2 breakpoints, but we want to make sure we don't have the
92        # third one still set in the target
93        response = self.vscode.request_testGetTargetBreakpoints()
94        if response:
95            breakpoints = response['body']['breakpoints']
96            self.assertEquals(len(breakpoints), len(lines),
97                            "expect %u source breakpoints" % (len(lines)))
98            for breakpoint in breakpoints:
99                line = breakpoint['line']
100                # Verify the same breakpoints are still set within LLDB by
101                # making sure the breakpoint ID didn't change
102                self.assertEquals(line_to_id[line], breakpoint['id'],
103                                "verify previous breakpoints stayed the same")
104                self.assertTrue(line in lines, "line expected in lines array")
105                self.assertTrue(breakpoint['verified'],
106                                "expect breakpoint still verified")
107
108        # Now clear all breakpoints for the source file by passing down an
109        # empty lines array
110        lines = []
111        response = self.vscode.request_setBreakpoints(source_path, lines)
112        if response:
113            breakpoints = response['body']['breakpoints']
114            self.assertEquals(len(breakpoints), len(lines),
115                            "expect %u source breakpoints" % (len(lines)))
116
117        # Verify with the target that all breakpoints have been cleared
118        response = self.vscode.request_testGetTargetBreakpoints()
119        if response:
120            breakpoints = response['body']['breakpoints']
121            self.assertEquals(len(breakpoints), len(lines),
122                            "expect %u source breakpoints" % (len(lines)))
123
124        # Now set a breakpoint again in the same source file and verify it
125        # was added.
126        lines = [second_line]
127        response = self.vscode.request_setBreakpoints(source_path, lines)
128        if response:
129            breakpoints = response['body']['breakpoints']
130            self.assertEquals(len(breakpoints), len(lines),
131                            "expect %u source breakpoints" % (len(lines)))
132            for breakpoint in breakpoints:
133                line = breakpoint['line']
134                self.assertTrue(line in lines, "line expected in lines array")
135                self.assertTrue(breakpoint['verified'],
136                                "expect breakpoint still verified")
137
138        # Now get the full list of breakpoints set in the target and verify
139        # we have only 2 breakpoints set. The response above could have told
140        # us about 2 breakpoints, but we want to make sure we don't have the
141        # third one still set in the target
142        response = self.vscode.request_testGetTargetBreakpoints()
143        if response:
144            breakpoints = response['body']['breakpoints']
145            self.assertEquals(len(breakpoints), len(lines),
146                            "expect %u source breakpoints" % (len(lines)))
147            for breakpoint in breakpoints:
148                line = breakpoint['line']
149                self.assertTrue(line in lines, "line expected in lines array")
150                self.assertTrue(breakpoint['verified'],
151                                "expect breakpoint still verified")
152
153    @skipIfWindows
154    @skipIfRemote
155    def test_functionality(self):
156        '''Tests hitting breakpoints and the functionality of a single
157           breakpoint, like 'conditions' and 'hitCondition' settings.'''
158        source_basename = 'main.cpp'
159        source_path = os.path.join(os.getcwd(), source_basename)
160        loop_line = line_number('main.cpp', '// break loop')
161
162        program = self.getBuildArtifact("a.out")
163        self.build_and_launch(program)
164        # Set a breakpoint at the loop line with no condition and no
165        # hitCondition
166        breakpoint_ids = self.set_source_breakpoints(source_path, [loop_line])
167        self.assertEquals(len(breakpoint_ids), 1, "expect one breakpoint")
168        self.vscode.request_continue()
169
170        # Verify we hit the breakpoint we just set
171        self.verify_breakpoint_hit(breakpoint_ids)
172
173        # Make sure i is zero at first breakpoint
174        i = int(self.vscode.get_local_variable_value('i'))
175        self.assertEquals(i, 0, 'i != 0 after hitting breakpoint')
176
177        # Update the condition on our breakpoint
178        new_breakpoint_ids = self.set_source_breakpoints(source_path,
179                                                         [loop_line],
180                                                         condition="i==4")
181        self.assertEquals(breakpoint_ids, new_breakpoint_ids,
182                        "existing breakpoint should have its condition "
183                        "updated")
184
185        self.continue_to_breakpoints(breakpoint_ids)
186        i = int(self.vscode.get_local_variable_value('i'))
187        self.assertEquals(i, 4,
188                        'i != 4 showing conditional works')
189
190        new_breakpoint_ids = self.set_source_breakpoints(source_path,
191                                                         [loop_line],
192                                                         hitCondition="2")
193
194        self.assertEquals(breakpoint_ids, new_breakpoint_ids,
195                        "existing breakpoint should have its condition "
196                        "updated")
197
198        # Continue with a hitContidtion of 2 and expect it to skip 1 value
199        self.continue_to_breakpoints(breakpoint_ids)
200        i = int(self.vscode.get_local_variable_value('i'))
201        self.assertEquals(i, 6,
202                        'i != 6 showing hitCondition works')
203
204        # continue after hitting our hitCondition and make sure it only goes
205        # up by 1
206        self.continue_to_breakpoints(breakpoint_ids)
207        i = int(self.vscode.get_local_variable_value('i'))
208        self.assertEquals(i, 7,
209                        'i != 7 showing post hitCondition hits every time')
210