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
13import shutil
14import subprocess
15import tempfile
16import threading
17import time
18
19
20def spawn_and_wait(program, delay):
21    if delay:
22        time.sleep(delay)
23    process = subprocess.Popen([program],
24                               stdin=subprocess.PIPE,
25                               stdout=subprocess.PIPE,
26                               stderr=subprocess.PIPE)
27    process.wait()
28
29
30class TestVSCode_attach(lldbvscode_testcase.VSCodeTestCaseBase):
31
32    mydir = TestBase.compute_mydir(__file__)
33
34    def set_and_hit_breakpoint(self, continueToExit=True):
35        source = 'main.c'
36        breakpoint1_line = line_number(source, '// breakpoint 1')
37        lines = [breakpoint1_line]
38        # Set breakpoint in the thread function so we can step the threads
39        breakpoint_ids = self.set_source_breakpoints(source, lines)
40        self.assertEqual(len(breakpoint_ids), len(lines),
41                         "expect correct number of breakpoints")
42        self.continue_to_breakpoints(breakpoint_ids)
43        if continueToExit:
44            self.continue_to_exit()
45
46    @skipIfWindows
47    @skipIfNetBSD # Hangs on NetBSD as well
48    @skipIfRemote
49    def test_by_pid(self):
50        '''
51            Tests attaching to a process by process ID.
52        '''
53        self.build_and_create_debug_adaptor()
54        program = self.getBuildArtifact("a.out")
55        self.process = subprocess.Popen([program],
56                                        stdin=subprocess.PIPE,
57                                        stdout=subprocess.PIPE,
58                                        stderr=subprocess.PIPE)
59        self.attach(pid=self.process.pid)
60        self.set_and_hit_breakpoint(continueToExit=True)
61
62    @skipIfWindows
63    @skipIfNetBSD # Hangs on NetBSD as well
64    @skipIfRemote
65    def test_by_name(self):
66        '''
67            Tests attaching to a process by process name.
68        '''
69        self.build_and_create_debug_adaptor()
70        orig_program = self.getBuildArtifact("a.out")
71        # Since we are going to attach by process name, we need a unique
72        # process name that has minimal chance to match a process that is
73        # already running. To do this we use tempfile.mktemp() to give us a
74        # full path to a location where we can copy our executable. We then
75        # run this copy to ensure we don't get the error "more that one
76        # process matches 'a.out'".
77        program = tempfile.mktemp()
78        shutil.copyfile(orig_program, program)
79        shutil.copymode(orig_program, program)
80
81        # Use a file as a synchronization point between test and inferior.
82        pid_file_path = lldbutil.append_to_process_working_directory(self,
83            "pid_file_%d" % (int(time.time())))
84
85        def cleanup():
86            if os.path.exists(program):
87                os.unlink(program)
88            self.run_platform_command("rm %s" % (pid_file_path))
89        # Execute the cleanup function during test case tear down.
90        self.addTearDownHook(cleanup)
91
92        popen = self.spawnSubprocess(program, [pid_file_path])
93        self.addTearDownHook(self.cleanupSubprocesses)
94
95        pid = lldbutil.wait_for_file_on_target(self, pid_file_path)
96
97        self.attach(program=program)
98        self.set_and_hit_breakpoint(continueToExit=True)
99
100    @skipUnlessDarwin
101    @skipIfDarwin
102    @skipIfNetBSD # Hangs on NetBSD as well
103    def test_by_name_waitFor(self):
104        '''
105            Tests attaching to a process by process name and waiting for the
106            next instance of a process to be launched, ingoring all current
107            ones.
108        '''
109        self.build_and_create_debug_adaptor()
110        program = self.getBuildArtifact("a.out")
111        self.spawn_thread = threading.Thread(target=spawn_and_wait,
112                                             args=(program, 1.0,))
113        self.spawn_thread.start()
114        self.attach(program=program, waitFor=True)
115        self.set_and_hit_breakpoint(continueToExit=True)
116
117    @skipIfWindows
118    @skipIfDarwin
119    @skipIfNetBSD # Hangs on NetBSD as well
120    @skipIf(archs="aarch64") # Example of a flaky run http://lab.llvm.org:8011/builders/lldb-aarch64-ubuntu/builds/5527/steps/test/logs/stdio
121    def test_commands(self):
122        '''
123            Tests the "initCommands", "preRunCommands", "stopCommands",
124            "exitCommands", "terminateCommands" and "attachCommands"
125            that can be passed during attach.
126
127            "initCommands" are a list of LLDB commands that get executed
128            before the targt is created.
129            "preRunCommands" are a list of LLDB commands that get executed
130            after the target has been created and before the launch.
131            "stopCommands" are a list of LLDB commands that get executed each
132            time the program stops.
133            "exitCommands" are a list of LLDB commands that get executed when
134            the process exits
135            "attachCommands" are a list of LLDB commands that get executed and
136            must have a valid process in the selected target in LLDB after
137            they are done executing. This allows custom commands to create any
138            kind of debug session.
139            "terminateCommands" are a list of LLDB commands that get executed when
140            the debugger session terminates.
141        '''
142        self.build_and_create_debug_adaptor()
143        program = self.getBuildArtifact("a.out")
144        # Here we just create a target and launch the process as a way to test
145        # if we are able to use attach commands to create any kind of a target
146        # and use it for debugging
147        attachCommands = [
148            'target create -d "%s"' % (program),
149            'process launch'
150        ]
151        initCommands = ['target list', 'platform list']
152        preRunCommands = ['image list a.out', 'image dump sections a.out']
153        stopCommands = ['frame variable', 'bt']
154        exitCommands = ['expr 2+3', 'expr 3+4']
155        terminateCommands = ['expr 4+2']
156        self.attach(program=program,
157                    attachCommands=attachCommands,
158                    initCommands=initCommands,
159                    preRunCommands=preRunCommands,
160                    stopCommands=stopCommands,
161                    exitCommands=exitCommands,
162                    terminateCommands=terminateCommands)
163        # Get output from the console. This should contain both the
164        # "initCommands" and the "preRunCommands".
165        output = self.get_console()
166        # Verify all "initCommands" were found in console output
167        self.verify_commands('initCommands', output, initCommands)
168        # Verify all "preRunCommands" were found in console output
169        self.verify_commands('preRunCommands', output, preRunCommands)
170
171        functions = ['main']
172        breakpoint_ids = self.set_function_breakpoints(functions)
173        self.assertEquals(len(breakpoint_ids), len(functions),
174                        "expect one breakpoint")
175        self.continue_to_breakpoints(breakpoint_ids)
176        output = self.get_console(timeout=1.0)
177        self.verify_commands('stopCommands', output, stopCommands)
178
179        # Continue after launch and hit the "pause()" call and stop the target.
180        # Get output from the console. This should contain both the
181        # "stopCommands" that were run after we stop.
182        self.vscode.request_continue()
183        time.sleep(0.5)
184        self.vscode.request_pause()
185        self.vscode.wait_for_stopped()
186        output = self.get_console(timeout=1.0)
187        self.verify_commands('stopCommands', output, stopCommands)
188
189        # Continue until the program exits
190        self.continue_to_exit()
191        # Get output from the console. This should contain both the
192        # "exitCommands" that were run after the second breakpoint was hit
193        # and the "terminateCommands" due to the debugging session ending
194        output = self.collect_console(duration=1.0)
195        self.verify_commands('exitCommands', output, exitCommands)
196        self.verify_commands('terminateCommands', output, terminateCommands)
197
198    @skipIfWindows
199    @skipIfDarwin
200    @skipIfNetBSD # Hangs on NetBSD as well
201    @skipIf(archs="aarch64") # Example of a flaky run http://lab.llvm.org:8011/builders/lldb-aarch64-ubuntu/builds/5517/steps/test/logs/stdio
202    def test_terminate_commands(self):
203        '''
204            Tests that the "terminateCommands", that can be passed during
205            attach, are run when the debugger is disconnected.
206        '''
207        self.build_and_create_debug_adaptor()
208        program = self.getBuildArtifact("a.out")
209        # Here we just create a target and launch the process as a way to test
210        # if we are able to use attach commands to create any kind of a target
211        # and use it for debugging
212        attachCommands = [
213            'target create -d "%s"' % (program),
214            'process launch'
215        ]
216        terminateCommands = ['expr 4+2']
217        self.attach(program=program,
218                    attachCommands=attachCommands,
219                    terminateCommands=terminateCommands,
220                    disconnectAutomatically=False)
221        self.get_console()
222        # Once it's disconnected the console should contain the
223        # "terminateCommands"
224        self.vscode.request_disconnect(terminateDebuggee=True)
225        output = self.collect_console(duration=1.0)
226        self.verify_commands('terminateCommands', output, terminateCommands)
227