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 11 12# Third-party modules 13import six 14from six.moves.urllib import parse as urlparse 15 16# LLDB modules 17from . import configuration 18import use_lldb_suite 19import lldb 20 21 22def check_first_register_readable(test_case): 23 arch = test_case.getArchitecture() 24 25 if arch in ['x86_64', 'i386']: 26 test_case.expect("register read eax", substrs=['eax = 0x']) 27 elif arch in ['arm']: 28 test_case.expect("register read r0", substrs=['r0 = 0x']) 29 elif arch in ['aarch64']: 30 test_case.expect("register read x0", substrs=['x0 = 0x']) 31 elif re.match("mips", arch): 32 test_case.expect("register read zero", substrs=['zero = 0x']) 33 elif arch in ['s390x']: 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 if not hasattr(_target_is_android, 'result'): 57 triple = lldb.DBG.GetSelectedPlatform().GetTriple() 58 match = re.match(".*-.*-.*-android", triple) 59 _target_is_android.result = match is not None 60 return _target_is_android.result 61 62 63def android_device_api(): 64 if not hasattr(android_device_api, 'result'): 65 assert configuration.lldb_platform_url is not None 66 device_id = None 67 parsed_url = urlparse.urlparse(configuration.lldb_platform_url) 68 host_name = parsed_url.netloc.split(":")[0] 69 if host_name != 'localhost': 70 device_id = host_name 71 if device_id.startswith('[') and device_id.endswith(']'): 72 device_id = device_id[1:-1] 73 retcode, stdout, stderr = _run_adb_command( 74 ["shell", "getprop", "ro.build.version.sdk"], device_id) 75 if retcode == 0: 76 android_device_api.result = int(stdout) 77 else: 78 raise LookupError( 79 ">>> Unable to determine the API level of the Android device.\n" 80 ">>> stdout:\n%s\n" 81 ">>> stderr:\n%s\n" % 82 (stdout, stderr)) 83 return android_device_api.result 84 85 86def match_android_device(device_arch, valid_archs=None, valid_api_levels=None): 87 if not _target_is_android(): 88 return False 89 if valid_archs is not None and device_arch not in valid_archs: 90 return False 91 if valid_api_levels is not None and android_device_api() not in valid_api_levels: 92 return False 93 94 return True 95 96 97def finalize_build_dictionary(dictionary): 98 if _target_is_android(): 99 if dictionary is None: 100 dictionary = {} 101 dictionary["OS"] = "Android" 102 if android_device_api() >= 16: 103 dictionary["PIE"] = 1 104 return dictionary 105 106 107def getHostPlatform(): 108 """Returns the host platform running the test suite.""" 109 # Attempts to return a platform name matching a target Triple platform. 110 if sys.platform.startswith('linux'): 111 return 'linux' 112 elif sys.platform.startswith('win32'): 113 return 'windows' 114 elif sys.platform.startswith('darwin'): 115 return 'darwin' 116 elif sys.platform.startswith('freebsd'): 117 return 'freebsd' 118 elif sys.platform.startswith('netbsd'): 119 return 'netbsd' 120 else: 121 return sys.platform 122 123 124def getDarwinOSTriples(): 125 return ['darwin', 'macosx', 'ios'] 126 127 128def getPlatform(): 129 """Returns the target platform which the tests are running on.""" 130 platform = lldb.DBG.GetSelectedPlatform().GetTriple().split('-')[2] 131 if platform.startswith('freebsd'): 132 platform = 'freebsd' 133 elif platform.startswith('netbsd'): 134 platform = 'netbsd' 135 return 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 143class _PlatformContext(object): 144 """Value object class which contains platform-specific options.""" 145 146 def __init__(self, shlib_environment_var, shlib_prefix, shlib_extension): 147 self.shlib_environment_var = shlib_environment_var 148 self.shlib_prefix = shlib_prefix 149 self.shlib_extension = shlib_extension 150 151 152def createPlatformContext(): 153 if platformIsDarwin(): 154 return _PlatformContext('DYLD_LIBRARY_PATH', 'lib', 'dylib') 155 elif getPlatform() in ("freebsd", "linux", "netbsd"): 156 return _PlatformContext('LD_LIBRARY_PATH', 'lib', 'so') 157 else: 158 return None 159 160 161def hasChattyStderr(test_case): 162 """Some targets produce garbage on the standard error output. This utility function 163 determines whether the tests can be strict about the expected stderr contents.""" 164 if match_android_device(test_case.getArchitecture(), ['aarch64'], [22]): 165 return True # The dynamic linker on the device will complain about unknown DT entries 166 return False 167