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 NO_DEBUG_INFO_TESTCASE = True 17 18 # The port used by debugserver. 19 PORT = 54637 20 21 # The number of attempts. 22 ATTEMPTS = 10 23 24 # Time given to the binary to launch and to debugserver to attach to it for 25 # every attempt. We'll wait a maximum of 10 times 2 seconds while the 26 # inferior will wait 10 times 10 seconds. 27 TIMEOUT = 2 28 29 def no_debugserver(self): 30 if get_debugserver_exe() is None: 31 return 'no debugserver' 32 return None 33 34 def port_not_available(self): 35 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 36 if s.connect_ex(('127.0.0.1', self.PORT)) == 0: 37 return '{} not available'.format(self.PORT) 38 return None 39 40 @no_debug_info_test 41 @skipUnlessDarwin 42 @expectedFailureIfFn(no_debugserver) 43 @expectedFailureIfFn(port_not_available) 44 @skipIfRemote 45 def test_macos_sdk(self): 46 self.build() 47 48 exe = self.getBuildArtifact('a.out') 49 token = self.getBuildArtifact('token') 50 51 # Remove the old token. 52 try: 53 os.remove(token) 54 except: 55 pass 56 57 # Create a fake 'SDK' directory. 58 test_home = os.path.join(self.getBuildDir(), 'fake_home.noindex') 59 test_home = os.path.realpath(test_home) 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(os.path.realpath(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