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