1"""Test the lldb public C++ api breakpoint callbacks."""
2
3from __future__ import print_function
4
5# __package__ = "lldbsuite.test"
6
7
8import os
9from lldbsuite.test.decorators import *
10from lldbsuite.test.lldbtest import *
11from lldbsuite.test import lldbutil
12
13
14class SBBreakpointCallbackCase(TestBase):
15
16    NO_DEBUG_INFO_TESTCASE = True
17
18    def setUp(self):
19        TestBase.setUp(self)
20        self.generateSource('driver.cpp')
21        self.generateSource('listener_test.cpp')
22        self.generateSource('test_breakpoint_callback.cpp')
23        self.generateSource('test_listener_event_description.cpp')
24        self.generateSource('test_listener_event_process_state.cpp')
25        self.generateSource('test_listener_resume.cpp')
26        self.generateSource('test_stop-hook.cpp')
27
28    @skipIfRemote
29    @skipIfNoSBHeaders
30    # clang-cl does not support throw or catch (llvm.org/pr24538)
31    @skipIfWindows
32    def test_python_stop_hook(self):
33        """Test that you can run a python command in a stop-hook when stdin is File based. """
34        self.build_and_test('driver.cpp test_stop-hook.cpp',
35                            'test_python_stop_hook')
36
37    @skipIfRemote
38    @skipIfNoSBHeaders
39    # clang-cl does not support throw or catch (llvm.org/pr24538)
40    @skipIfWindows
41    def test_breakpoint_callback(self):
42        """Test the that SBBreakpoint callback is invoked when a breakpoint is hit. """
43        self.build_and_test('driver.cpp test_breakpoint_callback.cpp',
44                            'test_breakpoint_callback')
45
46    @skipIfRemote
47    @skipIfNoSBHeaders
48    # clang-cl does not support throw or catch (llvm.org/pr24538)
49    @skipIfWindows
50    @expectedFlakeyFreeBSD
51    def test_sb_api_listener_event_description(self):
52        """ Test the description of an SBListener breakpoint event is valid."""
53        self.build_and_test(
54            'driver.cpp listener_test.cpp test_listener_event_description.cpp',
55            'test_listener_event_description')
56
57    @skipIfRemote
58    @skipIfNoSBHeaders
59    # clang-cl does not support throw or catch (llvm.org/pr24538)
60    @skipIfWindows
61    @expectedFlakeyFreeBSD
62    def test_sb_api_listener_event_process_state(self):
63        """ Test that a registered SBListener receives events when a process
64            changes state.
65        """
66        self.build_and_test(
67            'driver.cpp listener_test.cpp test_listener_event_process_state.cpp',
68            'test_listener_event_process_state')
69
70    @skipIfRemote
71    @skipIfNoSBHeaders
72    # clang-cl does not support throw or catch (llvm.org/pr24538)
73    @skipIfWindows
74    @expectedFlakeyFreeBSD
75    @skipIf(oslist=["linux"]) # flakey
76    def test_sb_api_listener_resume(self):
77        """ Test that a process can be resumed from a non-main thread. """
78        self.build_and_test(
79            'driver.cpp listener_test.cpp test_listener_resume.cpp',
80            'test_listener_resume')
81
82    def build_and_test(self, sources, test_name, args=None):
83        """ Build LLDB test from sources, and run expecting 0 exit code """
84
85        # These tests link against host lldb API.
86        # Compiler's target triple must match liblldb triple
87        # because remote is disabled, we can assume that the os is the same
88        # still need to check architecture
89        if self.getLldbArchitecture() != self.getArchitecture():
90            self.skipTest(
91                "This test is only run if the target arch is the same as the lldb binary arch")
92
93        self.inferior = 'inferior_program'
94        self.buildProgram('inferior.cpp', self.inferior)
95        self.addTearDownHook(lambda:
96                             os.remove(self.getBuildArtifact(self.inferior)))
97
98        self.buildDriver(sources, test_name)
99        self.addTearDownHook(lambda:
100                             os.remove(self.getBuildArtifact(test_name)))
101
102        test_exe = self.getBuildArtifact(test_name)
103        self.signBinary(test_exe)
104        exe = [test_exe, self.getBuildArtifact(self.inferior)]
105
106        env = {self.dylibPath: self.getLLDBLibraryEnvVal()}
107        if 'LLDB_DEBUGSERVER_PATH' in os.environ:
108            env['LLDB_DEBUGSERVER_PATH'] = os.environ['LLDB_DEBUGSERVER_PATH']
109        try:
110            if self.TraceOn():
111                print("Running test %s" % " ".join(exe))
112                check_call(exe, env=env)
113            else:
114                with open(os.devnull, 'w') as fnull:
115                    check_call(exe, env=env, stdout=fnull, stderr=fnull)
116        except subprocess.CalledProcessError as e:
117            self.fail(e)
118
119    def build_program(self, sources, program):
120        return self.buildDriver(sources, program)
121