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