1""" This module contains functions used by the test cases to hide the 2architecture and/or the platform dependent nature of the tests. """ 3 4from __future__ import absolute_import 5 6# System modules 7import itertools 8import re 9import subprocess 10import sys 11import os 12 13# Third-party modules 14import six 15from six.moves.urllib import parse as urlparse 16 17# LLDB modules 18from . import configuration 19import lldb 20import lldbsuite.test.lldbplatform as lldbplatform 21 22 23def check_first_register_readable(test_case): 24 arch = test_case.getArchitecture() 25 26 if arch in ['x86_64', 'i386']: 27 test_case.expect("register read eax", substrs=['eax = 0x']) 28 elif arch in ['arm', 'armv7', 'armv7k', 'armv8l', 'armv7l']: 29 test_case.expect("register read r0", substrs=['r0 = 0x']) 30 elif arch in ['aarch64', 'arm64', 'arm64e', 'arm64_32']: 31 test_case.expect("register read x0", substrs=['x0 = 0x']) 32 elif re.match("mips", arch): 33 test_case.expect("register read zero", substrs=['zero = 0x']) 34 elif arch in ['s390x']: 35 test_case.expect("register read r0", substrs=['r0 = 0x']) 36 elif arch in ['powerpc64le']: 37 test_case.expect("register read r0", substrs=['r0 = 0x']) 38 else: 39 # TODO: Add check for other architectures 40 test_case.fail( 41 "Unsupported architecture for test case (arch: %s)" % 42 test_case.getArchitecture()) 43 44 45def _run_adb_command(cmd, device_id): 46 device_id_args = [] 47 if device_id: 48 device_id_args = ["-s", device_id] 49 full_cmd = ["adb"] + device_id_args + cmd 50 p = subprocess.Popen( 51 full_cmd, 52 stdout=subprocess.PIPE, 53 stderr=subprocess.PIPE) 54 stdout, stderr = p.communicate() 55 return p.returncode, stdout, stderr 56 57 58def target_is_android(): 59 return configuration.lldb_platform_name == "remote-android" 60 61def android_device_api(): 62 if not hasattr(android_device_api, 'result'): 63 assert configuration.lldb_platform_url is not None 64 device_id = None 65 parsed_url = urlparse.urlparse(configuration.lldb_platform_url) 66 host_name = parsed_url.netloc.split(":")[0] 67 if host_name != 'localhost': 68 device_id = host_name 69 if device_id.startswith('[') and device_id.endswith(']'): 70 device_id = device_id[1:-1] 71 retcode, stdout, stderr = _run_adb_command( 72 ["shell", "getprop", "ro.build.version.sdk"], device_id) 73 if retcode == 0: 74 android_device_api.result = int(stdout) 75 else: 76 raise LookupError( 77 ">>> Unable to determine the API level of the Android device.\n" 78 ">>> stdout:\n%s\n" 79 ">>> stderr:\n%s\n" % 80 (stdout, stderr)) 81 return android_device_api.result 82 83 84def match_android_device(device_arch, valid_archs=None, valid_api_levels=None): 85 if not target_is_android(): 86 return False 87 if valid_archs is not None and device_arch not in valid_archs: 88 return False 89 if valid_api_levels is not None and android_device_api() not in valid_api_levels: 90 return False 91 92 return True 93 94 95def finalize_build_dictionary(dictionary): 96 if target_is_android(): 97 if dictionary is None: 98 dictionary = {} 99 dictionary["OS"] = "Android" 100 dictionary["PIE"] = 1 101 return dictionary 102 103 104def getHostPlatform(): 105 """Returns the host platform running the test suite.""" 106 # Attempts to return a platform name matching a target Triple platform. 107 if sys.platform.startswith('linux'): 108 return 'linux' 109 elif sys.platform.startswith('win32') or sys.platform.startswith('cygwin'): 110 return 'windows' 111 elif sys.platform.startswith('darwin'): 112 return 'macosx' 113 elif sys.platform.startswith('freebsd'): 114 return 'freebsd' 115 elif sys.platform.startswith('netbsd'): 116 return 'netbsd' 117 else: 118 return sys.platform 119 120 121def getDarwinOSTriples(): 122 return lldbplatform.translate(lldbplatform.darwin_all) 123 124def getPlatform(): 125 """Returns the target platform which the tests are running on.""" 126 # Use the Apple SDK to determine the platform if set. 127 if configuration.apple_sdk: 128 platform = configuration.apple_sdk 129 dot = platform.find('.') 130 if dot != -1: 131 platform = platform[:dot] 132 if platform == 'iphoneos': 133 platform = 'ios' 134 return platform 135 136 platform = configuration.lldb_platform_name 137 if platform is None: 138 platform = "host" 139 if platform == "qemu-user": 140 platform = "host" 141 if platform == "host": 142 return getHostPlatform() 143 if platform.startswith("remote-"): 144 return platform[7:] 145 return platform 146 147 148def platformIsDarwin(): 149 """Returns true if the OS triple for the selected platform is any valid apple OS""" 150 return getPlatform() in getDarwinOSTriples() 151 152 153def findMainThreadCheckerDylib(): 154 if not platformIsDarwin(): 155 return "" 156 157 if getPlatform() in lldbplatform.translate(lldbplatform.darwin_embedded): 158 return "/Developer/usr/lib/libMainThreadChecker.dylib" 159 160 with os.popen('xcode-select -p') as output: 161 xcode_developer_path = output.read().strip() 162 mtc_dylib_path = '%s/usr/lib/libMainThreadChecker.dylib' % xcode_developer_path 163 if os.path.isfile(mtc_dylib_path): 164 return mtc_dylib_path 165 166 return "" 167 168 169class _PlatformContext(object): 170 """Value object class which contains platform-specific options.""" 171 172 def __init__(self, shlib_environment_var, shlib_path_separator, shlib_prefix, shlib_extension): 173 self.shlib_environment_var = shlib_environment_var 174 self.shlib_path_separator = shlib_path_separator 175 self.shlib_prefix = shlib_prefix 176 self.shlib_extension = shlib_extension 177 178 179def createPlatformContext(): 180 if platformIsDarwin(): 181 return _PlatformContext('DYLD_LIBRARY_PATH', ':', 'lib', 'dylib') 182 elif getPlatform() in ("freebsd", "linux", "netbsd"): 183 return _PlatformContext('LD_LIBRARY_PATH', ':', 'lib', 'so') 184 else: 185 return _PlatformContext('PATH', ';', '', 'dll') 186 187 188def hasChattyStderr(test_case): 189 """Some targets produce garbage on the standard error output. This utility function 190 determines whether the tests can be strict about the expected stderr contents.""" 191 if match_android_device(test_case.getArchitecture(), ['aarch64'], range(22, 25+1)): 192 return True # The dynamic linker on the device will complain about unknown DT entries 193 return False 194