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