summaryrefslogtreecommitdiffstats
path: root/gnu/llvm/lldb/packages/Python/lldbsuite/test/tools/lldb-vscode/breakpoint/TestVSCode_setFunctionBreakpoints.py
blob: 2c62bf80fcfba62fedda0915d2d3fe301617c0db (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
"""
Test lldb-vscode setBreakpoints request
"""


import unittest2
import vscode
from lldbsuite.test.decorators import *
from lldbsuite.test.lldbtest import *
from lldbsuite.test import lldbutil
import lldbvscode_testcase


class TestVSCode_setFunctionBreakpoints(
        lldbvscode_testcase.VSCodeTestCaseBase):

    mydir = TestBase.compute_mydir(__file__)

    @skipIfWindows
    def test_set_and_clear(self):
        '''Tests setting and clearing function breakpoints.
           This packet is a bit tricky on the debug adaptor side since there
           is no "clearFunction Breakpoints" packet. Function breakpoints
           are set by sending a "setFunctionBreakpoints" packet with zero or
           more function names. If function breakpoints have been set before,
           any exising breakpoints must remain set, and any new breakpoints
           must be created, and any breakpoints that were in previous requests
           and are not in the current request must be removed. This function
           tests this setting and clearing and makes sure things happen
           correctly. It doesn't test hitting breakpoints and the functionality
           of each breakpoint, like 'conditions' and 'hitCondition' settings.
        '''
        # Visual Studio Code Debug Adaptors have no way to specify the file
        # without launching or attaching to a process, so we must start a
        # process in order to be able to set breakpoints.
        program = self.getBuildArtifact("a.out")
        self.build_and_launch(program)
        bp_id_12 = None
        functions = ['twelve']
        # Set a function breakpoint at 'twelve'
        response = self.vscode.request_setFunctionBreakpoints(functions)
        if response:
            breakpoints = response['body']['breakpoints']
            self.assertTrue(len(breakpoints) == len(functions),
                            "expect %u source breakpoints" % (len(functions)))
            for breakpoint in breakpoints:
                bp_id_12 = breakpoint['id']
                self.assertTrue(breakpoint['verified'],
                                "expect breakpoint verified")

        # Add an extra name and make sure we have two breakpoints after this
        functions.append('thirteen')
        response = self.vscode.request_setFunctionBreakpoints(functions)
        if response:
            breakpoints = response['body']['breakpoints']
            self.assertTrue(len(breakpoints) == len(functions),
                            "expect %u source breakpoints" % (len(functions)))
            for breakpoint in breakpoints:
                self.assertTrue(breakpoint['verified'],
                                "expect breakpoint verified")

        # There is no breakpoint delete packet, clients just send another
        # setFunctionBreakpoints packet with the different function names.
        functions.remove('thirteen')
        response = self.vscode.request_setFunctionBreakpoints(functions)
        if response:
            breakpoints = response['body']['breakpoints']
            self.assertTrue(len(breakpoints) == len(functions),
                            "expect %u source breakpoints" % (len(functions)))
            for breakpoint in breakpoints:
                bp_id = breakpoint['id']
                self.assertTrue(bp_id == bp_id_12,
                                'verify "twelve" breakpoint ID is same')
                self.assertTrue(breakpoint['verified'],
                                "expect breakpoint still verified")

        # Now get the full list of breakpoints set in the target and verify
        # we have only 1 breakpoints set. The response above could have told
        # us about 1 breakpoints, but we want to make sure we don't have the
        # second one still set in the target
        response = self.vscode.request_testGetTargetBreakpoints()
        if response:
            breakpoints = response['body']['breakpoints']
            self.assertTrue(len(breakpoints) == len(functions),
                            "expect %u source breakpoints" % (len(functions)))
            for breakpoint in breakpoints:
                bp_id = breakpoint['id']
                self.assertTrue(bp_id == bp_id_12,
                                'verify "twelve" breakpoint ID is same')
                self.assertTrue(breakpoint['verified'],
                                "expect breakpoint still verified")

        # Now clear all breakpoints for the source file by passing down an
        # empty lines array
        functions = []
        response = self.vscode.request_setFunctionBreakpoints(functions)
        if response:
            breakpoints = response['body']['breakpoints']
            self.assertTrue(len(breakpoints) == len(functions),
                            "expect %u source breakpoints" % (len(functions)))

        # Verify with the target that all breakpoints have been cleared
        response = self.vscode.request_testGetTargetBreakpoints()
        if response:
            breakpoints = response['body']['breakpoints']
            self.assertTrue(len(breakpoints) == len(functions),
                            "expect %u source breakpoints" % (len(functions)))

    @skipIfWindows
    def test_functionality(self):
        '''Tests hitting breakpoints and the functionality of a single
           breakpoint, like 'conditions' and 'hitCondition' settings.'''

        program = self.getBuildArtifact("a.out")
        self.build_and_launch(program)
        # Set a breakpoint on "twelve" with no condition and no hitCondition
        functions = ['twelve']
        breakpoint_ids = self.set_function_breakpoints(functions)

        self.assertTrue(len(breakpoint_ids) == len(functions),
                        "expect one breakpoint")

        # Verify we hit the breakpoint we just set
        self.continue_to_breakpoints(breakpoint_ids)

        # Make sure i is zero at first breakpoint
        i = int(self.vscode.get_local_variable_value('i'))
        self.assertTrue(i == 0, 'i != 0 after hitting breakpoint')

        # Update the condition on our breakpoint
        new_breakpoint_ids = self.set_function_breakpoints(functions,
                                                           condition="i==4")
        self.assertTrue(breakpoint_ids == new_breakpoint_ids,
                        "existing breakpoint should have its condition "
                        "updated")

        self.continue_to_breakpoints(breakpoint_ids)
        i = int(self.vscode.get_local_variable_value('i'))
        self.assertTrue(i == 4,
                        'i != 4 showing conditional works')
        new_breakpoint_ids = self.set_function_breakpoints(functions,
                                                           hitCondition="2")

        self.assertTrue(breakpoint_ids == new_breakpoint_ids,
                        "existing breakpoint should have its condition "
                        "updated")

        # Continue with a hitContidtion of 2 and expect it to skip 1 value
        self.continue_to_breakpoints(breakpoint_ids)
        i = int(self.vscode.get_local_variable_value('i'))
        self.assertTrue(i == 6,
                        'i != 6 showing hitCondition works')

        # continue after hitting our hitCondition and make sure it only goes
        # up by 1
        self.continue_to_breakpoints(breakpoint_ids)
        i = int(self.vscode.get_local_variable_value('i'))
        self.assertTrue(i == 7,
                        'i != 7 showing post hitCondition hits every time')