1import lldb
2
3from lldbsuite.test.decorators import *
4from lldbsuite.test.lldbtest import *
5from lldbsuite.test import lldbutil
6from lldbgdbserverutils import get_debugserver_exe
7
8import os
9import platform
10import shutil
11import time
12import socket
13
14
15class PlatformSDKTestCase(TestBase):
16
17    mydir = TestBase.compute_mydir(__file__)
18    NO_DEBUG_INFO_TESTCASE = True
19
20    # The port used by debugserver.
21    PORT = 54637
22
23    # The number of attempts.
24    ATTEMPTS = 10
25
26    # Time given to the binary to launch and to debugserver to attach to it for
27    # every attempt. We'll wait a maximum of 10 times 2 seconds while the
28    # inferior will wait 10 times 10 seconds.
29    TIMEOUT = 2
30
31    def no_debugserver(self):
32        if get_debugserver_exe() is None:
33            return 'no debugserver'
34        return None
35
36    def port_not_available(self):
37        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
38        if s.connect_ex(('127.0.0.1', self.PORT)) == 0:
39            return '{} not available'.format(self.PORT)
40        return None
41
42    @no_debug_info_test
43    @skipUnlessDarwin
44    @expectedFailureIfFn(no_debugserver)
45    @expectedFailureIfFn(port_not_available)
46    @skipIfRemote
47    def test_macos_sdk(self):
48        self.build()
49
50        exe = self.getBuildArtifact('a.out')
51        token = self.getBuildArtifact('token')
52
53        # Remove the old token.
54        try:
55            os.remove(token)
56        except:
57            pass
58
59        # Create a fake 'SDK' directory.
60        test_home = os.path.join(self.getBuildDir(), 'fake_home.noindex')
61        macos_version = platform.mac_ver()[0]
62        sdk_dir = os.path.join(test_home, 'Library', 'Developer', 'Xcode',
63                               'macOS DeviceSupport', macos_version)
64        symbols_dir = os.path.join(sdk_dir, 'Symbols')
65        lldbutil.mkdir_p(symbols_dir)
66
67        # Save the current home directory and restore it afterwards.
68        old_home = os.getenv('HOME')
69
70        def cleanup():
71            if not old_home:
72                del os.environ['HOME']
73            else:
74                os.environ['HOME'] = old_home
75
76        self.addTearDownHook(cleanup)
77        os.environ['HOME'] = test_home
78
79        # Launch our test binary.
80        inferior = self.spawnSubprocess(exe, [token])
81        pid = inferior.pid
82
83        # Wait for the binary to launch.
84        lldbutil.wait_for_file_on_target(self, token)
85
86        # Move the binary into the 'SDK'.
87        rel_exe_path = os.path.relpath(exe, '/')
88        exe_sdk_path = os.path.join(symbols_dir, rel_exe_path)
89        lldbutil.mkdir_p(os.path.dirname(exe_sdk_path))
90        shutil.move(exe, exe_sdk_path)
91
92        # Attach to it with debugserver.
93        debugserver = get_debugserver_exe()
94        debugserver_args = [
95            'localhost:{}'.format(self.PORT), '--attach={}'.format(pid)
96        ]
97        self.spawnSubprocess(debugserver, debugserver_args)
98
99        # Select the platform.
100        self.expect('platform select remote-macosx', substrs=[sdk_dir])
101
102        # Connect to debugserver
103        interpreter = self.dbg.GetCommandInterpreter()
104        connected = False
105        for i in range(self.ATTEMPTS):
106            result = lldb.SBCommandReturnObject()
107            interpreter.HandleCommand('gdb-remote {}'.format(self.PORT),
108                                      result)
109            connected = result.Succeeded()
110            if connected:
111                break
112            time.sleep(self.TIMEOUT)
113
114        self.assertTrue(connected, "could not connect to debugserver")
115
116        # Make sure the image was loaded from the 'SDK'.
117        self.expect('image list', substrs=[exe_sdk_path])
118