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