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