xref: /llvm-project/lldb/packages/Python/lldbsuite/test/lldbplatformutil.py (revision 56f9cfe30c4488aade888905bb6280ecb952a613)
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 getHostPlatform():
102    """Returns the host platform running the test suite."""
103    # Attempts to return a platform name matching a target Triple platform.
104    if sys.platform.startswith('linux'):
105        return 'linux'
106    elif sys.platform.startswith('win32') or sys.platform.startswith('cygwin'):
107        return 'windows'
108    elif sys.platform.startswith('darwin'):
109        return 'macosx'
110    elif sys.platform.startswith('freebsd'):
111        return 'freebsd'
112    elif sys.platform.startswith('netbsd'):
113        return 'netbsd'
114    else:
115        return sys.platform
116
117
118def getDarwinOSTriples():
119    return lldbplatform.translate(lldbplatform.darwin_all)
120
121def getPlatform():
122    """Returns the target platform which the tests are running on."""
123    # Use the Apple SDK to determine the platform if set.
124    if configuration.apple_sdk:
125        platform = configuration.apple_sdk
126        dot = platform.find('.')
127        if dot != -1:
128            platform = platform[:dot]
129        if platform == 'iphoneos':
130            platform = 'ios'
131        return platform
132
133    platform = configuration.lldb_platform_name
134    if platform is None:
135        platform = "host"
136    if platform == "qemu-user":
137        platform = "host"
138    if platform == "host":
139        return getHostPlatform()
140    if platform.startswith("remote-"):
141        return platform[7:]
142    return platform
143
144
145def platformIsDarwin():
146    """Returns true if the OS triple for the selected platform is any valid apple OS"""
147    return getPlatform() in getDarwinOSTriples()
148
149
150def findMainThreadCheckerDylib():
151    if not platformIsDarwin():
152        return ""
153
154    if getPlatform() in lldbplatform.translate(lldbplatform.darwin_embedded):
155        return "/Developer/usr/lib/libMainThreadChecker.dylib"
156
157    with os.popen('xcode-select -p') as output:
158        xcode_developer_path = output.read().strip()
159        mtc_dylib_path = '%s/usr/lib/libMainThreadChecker.dylib' % xcode_developer_path
160        if os.path.isfile(mtc_dylib_path):
161            return mtc_dylib_path
162
163    return ""
164
165
166class _PlatformContext(object):
167    """Value object class which contains platform-specific options."""
168
169    def __init__(self, shlib_environment_var, shlib_path_separator, shlib_prefix, shlib_extension):
170        self.shlib_environment_var = shlib_environment_var
171        self.shlib_path_separator = shlib_path_separator
172        self.shlib_prefix = shlib_prefix
173        self.shlib_extension = shlib_extension
174
175
176def createPlatformContext():
177    if platformIsDarwin():
178        return _PlatformContext('DYLD_LIBRARY_PATH', ':', 'lib', 'dylib')
179    elif getPlatform() in ("freebsd", "linux", "netbsd"):
180        return _PlatformContext('LD_LIBRARY_PATH', ':', 'lib', 'so')
181    else:
182        return _PlatformContext('PATH', ';', '', 'dll')
183
184
185def hasChattyStderr(test_case):
186    """Some targets produce garbage on the standard error output. This utility function
187    determines whether the tests can be strict about the expected stderr contents."""
188    if match_android_device(test_case.getArchitecture(), ['aarch64'], range(22, 25+1)):
189        return True  # The dynamic linker on the device will complain about unknown DT entries
190    return False
191