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