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    if not hasattr(target_is_android, 'result'):
60        triple = lldb.selected_platform.GetTriple()
61        match = re.match(".*-.*-.*-android", triple)
62        target_is_android.result = match is not None
63    return target_is_android.result
64
65
66def android_device_api():
67    if not hasattr(android_device_api, 'result'):
68        assert configuration.lldb_platform_url is not None
69        device_id = None
70        parsed_url = urlparse.urlparse(configuration.lldb_platform_url)
71        host_name = parsed_url.netloc.split(":")[0]
72        if host_name != 'localhost':
73            device_id = host_name
74            if device_id.startswith('[') and device_id.endswith(']'):
75                device_id = device_id[1:-1]
76        retcode, stdout, stderr = _run_adb_command(
77            ["shell", "getprop", "ro.build.version.sdk"], device_id)
78        if retcode == 0:
79            android_device_api.result = int(stdout)
80        else:
81            raise LookupError(
82                ">>> Unable to determine the API level of the Android device.\n"
83                ">>> stdout:\n%s\n"
84                ">>> stderr:\n%s\n" %
85                (stdout, stderr))
86    return android_device_api.result
87
88
89def match_android_device(device_arch, valid_archs=None, valid_api_levels=None):
90    if not target_is_android():
91        return False
92    if valid_archs is not None and device_arch not in valid_archs:
93        return False
94    if valid_api_levels is not None and android_device_api() not in valid_api_levels:
95        return False
96
97    return True
98
99
100def finalize_build_dictionary(dictionary):
101    if target_is_android():
102        if dictionary is None:
103            dictionary = {}
104        dictionary["OS"] = "Android"
105        dictionary["PIE"] = 1
106    return dictionary
107
108
109def getHostPlatform():
110    """Returns the host platform running the test suite."""
111    # Attempts to return a platform name matching a target Triple platform.
112    if sys.platform.startswith('linux'):
113        return 'linux'
114    elif sys.platform.startswith('win32') or sys.platform.startswith('cygwin'):
115        return 'windows'
116    elif sys.platform.startswith('darwin'):
117        return 'darwin'
118    elif sys.platform.startswith('freebsd'):
119        return 'freebsd'
120    elif sys.platform.startswith('netbsd'):
121        return 'netbsd'
122    else:
123        return sys.platform
124
125
126def getDarwinOSTriples():
127    return lldbplatform.translate(lldbplatform.darwin_all)
128
129def getPlatform():
130    """Returns the target platform which the tests are running on."""
131    # Use the Apple SDK to determine the platform if set.
132    if configuration.apple_sdk:
133        platform = configuration.apple_sdk
134        dot = platform.find('.')
135        if dot != -1:
136            platform = platform[:dot]
137        if platform == 'iphoneos':
138            platform = 'ios'
139        return platform
140
141    # Use the triple to determine the platform if set.
142    triple = lldb.selected_platform.GetTriple()
143    if triple:
144        platform = triple.split('-')[2]
145        if platform.startswith('freebsd'):
146            platform = 'freebsd'
147        elif platform.startswith('netbsd'):
148            platform = 'netbsd'
149        return platform
150
151    # It still might be an unconnected remote platform.
152    return ''
153
154
155def platformIsDarwin():
156    """Returns true if the OS triple for the selected platform is any valid apple OS"""
157    return getPlatform() in getDarwinOSTriples()
158
159
160def findMainThreadCheckerDylib():
161    if not platformIsDarwin():
162        return ""
163
164    if getPlatform() in lldbplatform.translate(lldbplatform.darwin_embedded):
165        return "/Developer/usr/lib/libMainThreadChecker.dylib"
166
167    with os.popen('xcode-select -p') as output:
168        xcode_developer_path = output.read().strip()
169        mtc_dylib_path = '%s/usr/lib/libMainThreadChecker.dylib' % xcode_developer_path
170        if os.path.isfile(mtc_dylib_path):
171            return mtc_dylib_path
172
173    return ""
174
175
176class _PlatformContext(object):
177    """Value object class which contains platform-specific options."""
178
179    def __init__(self, shlib_environment_var, shlib_path_separator, shlib_prefix, shlib_extension):
180        self.shlib_environment_var = shlib_environment_var
181        self.shlib_path_separator = shlib_path_separator
182        self.shlib_prefix = shlib_prefix
183        self.shlib_extension = shlib_extension
184
185
186def createPlatformContext():
187    if platformIsDarwin():
188        return _PlatformContext('DYLD_LIBRARY_PATH', ':', 'lib', 'dylib')
189    elif getPlatform() in ("freebsd", "linux", "netbsd"):
190        return _PlatformContext('LD_LIBRARY_PATH', ':', 'lib', 'so')
191    else:
192        return _PlatformContext('PATH', ';', '', 'dll')
193
194
195def hasChattyStderr(test_case):
196    """Some targets produce garbage on the standard error output. This utility function
197    determines whether the tests can be strict about the expected stderr contents."""
198    if match_android_device(test_case.getArchitecture(), ['aarch64'], range(22, 25+1)):
199        return True  # The dynamic linker on the device will complain about unknown DT entries
200    return False
201