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_breakpointEvents(lldbvscode_testcase.VSCodeTestCaseBase):
16
17    @skipIfWindows
18    @skipUnlessDarwin
19    @expectedFailureAll(macos_version=[">=", "13.0"])
20    def test_breakpoint_events(self):
21        '''
22            This test sets a breakpoint in a shared library and runs and stops
23            at the entry point of a program. When we stop at the entry point,
24            the shared library won't be loaded yet. At this point the
25            breakpoint should set itself, but not be verified because no
26            locations are resolved. We will then continue and expect to get a
27            breakpoint event that informs us that the breakpoint in the shared
28            library is "changed" and the correct line number should be
29            supplied. We also set a breakpoint using a LLDB command using the
30            "preRunCommands" when launching our program. Any breakpoints set via
31            the command interpreter should not be have breakpoint events sent
32            back to VS Code as the UI isn't able to add new breakpoints to
33            their UI. Code has been added that tags breakpoints set from VS Code
34            DAP packets so we know the IDE knows about them. If VS Code is ever
35            able to register breakpoints that aren't initially set in the GUI,
36            then we will need to revise this.
37        '''
38        main_source_basename = 'main.cpp'
39        main_source_path = os.path.join(os.getcwd(), main_source_basename)
40        foo_source_basename = 'foo.cpp'
41        foo_source_path = os.path.join(os.getcwd(), foo_source_basename)
42        main_bp_line = line_number('main.cpp', 'main breakpoint 1')
43        foo_bp1_line = line_number('foo.cpp', 'foo breakpoint 1')
44        foo_bp2_line = line_number('foo.cpp', 'foo breakpoint 2')
45
46        # Visual Studio Code Debug Adaptors have no way to specify the file
47        # without launching or attaching to a process, so we must start a
48        # process in order to be able to set breakpoints.
49        program = self.getBuildArtifact("a.out")
50
51        # Set a breakpoint after creating the target by running a command line
52        # command. It will eventually resolve and cause a breakpoint changed
53        # event to be sent to lldb-vscode. We want to make sure we don't send a
54        # breakpoint any breakpoints that were set from the command line.
55        # Breakpoints that are set via the VS code DAP packets will be
56        # registered and marked with a special keyword to ensure we deliver
57        # breakpoint events for these breakpoints but not for ones that are not
58        # set via the command interpreter.
59        bp_command = 'breakpoint set --file foo.cpp --line %u' % (foo_bp2_line)
60        self.build_and_launch(program, stopOnEntry=True,
61                              preRunCommands=[bp_command])
62        main_bp_id = 0
63        foo_bp_id = 0
64        # Set breakpoints and verify that they got set correctly
65        vscode_breakpoint_ids = []
66        response = self.vscode.request_setBreakpoints(main_source_path,
67                                                      [main_bp_line])
68        if response:
69            breakpoints = response['body']['breakpoints']
70            for breakpoint in breakpoints:
71                main_bp_id = breakpoint['id']
72                vscode_breakpoint_ids.append("%i" % (main_bp_id))
73                # line = breakpoint['line']
74                self.assertTrue(breakpoint['verified'],
75                                "expect main breakpoint to be verified")
76
77        response = self.vscode.request_setBreakpoints(foo_source_path,
78                                                      [foo_bp1_line])
79        if response:
80            breakpoints = response['body']['breakpoints']
81            for breakpoint in breakpoints:
82                foo_bp_id = breakpoint['id']
83                vscode_breakpoint_ids.append("%i" % (foo_bp_id))
84                self.assertFalse(breakpoint['verified'],
85                                 "expect foo breakpoint to not be verified")
86
87        # Get the stop at the entry point
88        self.continue_to_next_stop()
89
90        # We are now stopped at the entry point to the program. Shared
91        # libraries are not loaded yet (at least on macOS they aren't) and any
92        # breakpoints set in foo.cpp should not be resolved.
93        self.assertEqual(len(self.vscode.breakpoint_events), 0,
94                        "no breakpoint events when stopped at entry point")
95
96        # Continue to the breakpoint
97        self.continue_to_breakpoints(vscode_breakpoint_ids)
98
99        # Make sure we only get an event for the breakpoint we set via a call
100        # to self.vscode.request_setBreakpoints(...), not the breakpoint
101        # we set with with a LLDB command in preRunCommands.
102        self.assertEqual(len(self.vscode.breakpoint_events), 1,
103                        "make sure we got a breakpoint event")
104        event = self.vscode.breakpoint_events[0]
105        # Verify the details of the breakpoint changed notification.
106        body = event['body']
107        self.assertEqual(body['reason'], 'changed',
108                "breakpoint event is says breakpoint is changed")
109        breakpoint = body['breakpoint']
110        self.assertTrue(breakpoint['verified'],
111                "breakpoint event is says it is verified")
112        self.assertEqual(breakpoint['id'], foo_bp_id,
113                "breakpoint event is for breakpoint %i" % (foo_bp_id))
114        self.assertTrue('line' in breakpoint and breakpoint['line'] > 0,
115                "breakpoint event is has a line number")
116        self.assertNotIn("source", breakpoint,
117                "breakpoint event should not return a source object")
118