xref: /llvm-project/lldb/test/API/commands/platform/sdk/TestPlatformSDK.py (revision 8ee35c5558aa86e3be5f2882f1db030d6281578f)
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        test_home = os.path.realpath(test_home)
62        macos_version = platform.mac_ver()[0]
63        sdk_dir = os.path.join(test_home, 'Library', 'Developer', 'Xcode',
64                               'macOS DeviceSupport', macos_version)
65        symbols_dir = os.path.join(sdk_dir, 'Symbols')
66        lldbutil.mkdir_p(symbols_dir)
67
68        # Save the current home directory and restore it afterwards.
69        old_home = os.getenv('HOME')
70
71        def cleanup():
72            if not old_home:
73                del os.environ['HOME']
74            else:
75                os.environ['HOME'] = old_home
76
77        self.addTearDownHook(cleanup)
78        os.environ['HOME'] = test_home
79
80        # Launch our test binary.
81        inferior = self.spawnSubprocess(exe, [token])
82        pid = inferior.pid
83
84        # Wait for the binary to launch.
85        lldbutil.wait_for_file_on_target(self, token)
86
87        # Move the binary into the 'SDK'.
88        rel_exe_path = os.path.relpath(exe, '/')
89        exe_sdk_path = os.path.join(symbols_dir, rel_exe_path)
90        lldbutil.mkdir_p(os.path.dirname(exe_sdk_path))
91        shutil.move(exe, exe_sdk_path)
92
93        # Attach to it with debugserver.
94        debugserver = get_debugserver_exe()
95        debugserver_args = [
96            'localhost:{}'.format(self.PORT), '--attach={}'.format(pid)
97        ]
98        self.spawnSubprocess(debugserver, debugserver_args)
99
100        # Select the platform.
101        self.expect('platform select remote-macosx', substrs=[sdk_dir])
102
103        # Connect to debugserver
104        interpreter = self.dbg.GetCommandInterpreter()
105        connected = False
106        for i in range(self.ATTEMPTS):
107            result = lldb.SBCommandReturnObject()
108            interpreter.HandleCommand('gdb-remote {}'.format(self.PORT),
109                                      result)
110            connected = result.Succeeded()
111            if connected:
112                break
113            time.sleep(self.TIMEOUT)
114
115        self.assertTrue(connected, "could not connect to debugserver")
116
117        # Make sure the image was loaded from the 'SDK'.
118        self.expect('image list', substrs=[exe_sdk_path])
119