Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / lldb / packages / Python / lldbsuite / test / lldbplatformutil.py
blob2ecfe527e0f251ee3d24c2774473e33563e170ba
1 """ This module contains functions used by the test cases to hide the
2 architecture and/or the platform dependent nature of the tests. """
4 # System modules
5 import itertools
6 import re
7 import subprocess
8 import sys
9 import os
10 from urllib.parse import urlparse
12 # LLDB modules
13 from . import configuration
14 import lldb
15 import lldbsuite.test.lldbplatform as lldbplatform
18 def check_first_register_readable(test_case):
19 arch = test_case.getArchitecture()
21 if arch in ["x86_64", "i386"]:
22 test_case.expect("register read eax", substrs=["eax = 0x"])
23 elif arch in ["arm", "armv7", "armv7k", "armv8l", "armv7l"]:
24 test_case.expect("register read r0", substrs=["r0 = 0x"])
25 elif arch in ["aarch64", "arm64", "arm64e", "arm64_32"]:
26 test_case.expect("register read x0", substrs=["x0 = 0x"])
27 elif re.match("mips", arch):
28 test_case.expect("register read zero", substrs=["zero = 0x"])
29 elif arch in ["s390x"]:
30 test_case.expect("register read r0", substrs=["r0 = 0x"])
31 elif arch in ["powerpc64le"]:
32 test_case.expect("register read r0", substrs=["r0 = 0x"])
33 else:
34 # TODO: Add check for other architectures
35 test_case.fail(
36 "Unsupported architecture for test case (arch: %s)"
37 % test_case.getArchitecture()
41 def _run_adb_command(cmd, device_id):
42 device_id_args = []
43 if device_id:
44 device_id_args = ["-s", device_id]
45 full_cmd = ["adb"] + device_id_args + cmd
46 p = subprocess.Popen(full_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
47 stdout, stderr = p.communicate()
48 return p.returncode, stdout, stderr
51 def target_is_android():
52 return configuration.lldb_platform_name == "remote-android"
55 def android_device_api():
56 if not hasattr(android_device_api, "result"):
57 assert configuration.lldb_platform_url is not None
58 device_id = None
59 parsed_url = urlparse(configuration.lldb_platform_url)
60 host_name = parsed_url.netloc.split(":")[0]
61 if host_name != "localhost":
62 device_id = host_name
63 if device_id.startswith("[") and device_id.endswith("]"):
64 device_id = device_id[1:-1]
65 retcode, stdout, stderr = _run_adb_command(
66 ["shell", "getprop", "ro.build.version.sdk"], device_id
68 if retcode == 0:
69 android_device_api.result = int(stdout)
70 else:
71 raise LookupError(
72 ">>> Unable to determine the API level of the Android device.\n"
73 ">>> stdout:\n%s\n"
74 ">>> stderr:\n%s\n" % (stdout, stderr)
76 return android_device_api.result
79 def match_android_device(device_arch, valid_archs=None, valid_api_levels=None):
80 if not target_is_android():
81 return False
82 if valid_archs is not None and device_arch not in valid_archs:
83 return False
84 if valid_api_levels is not None and android_device_api() not in valid_api_levels:
85 return False
87 return True
90 def finalize_build_dictionary(dictionary):
91 if target_is_android():
92 if dictionary is None:
93 dictionary = {}
94 dictionary["OS"] = "Android"
95 dictionary["PIE"] = 1
96 return dictionary
99 def _get_platform_os(p):
100 # Use the triple to determine the platform if set.
101 triple = p.GetTriple()
102 if triple:
103 platform = triple.split("-")[2]
104 if platform.startswith("freebsd"):
105 platform = "freebsd"
106 elif platform.startswith("netbsd"):
107 platform = "netbsd"
108 return platform
110 return ""
113 def getHostPlatform():
114 """Returns the host platform running the test suite."""
115 return _get_platform_os(lldb.SBPlatform("host"))
118 def getDarwinOSTriples():
119 return lldbplatform.translate(lldbplatform.darwin_all)
122 def getPlatform():
123 """Returns the target platform which the tests are running on."""
124 # Use the Apple SDK to determine the platform if set.
125 if configuration.apple_sdk:
126 platform = configuration.apple_sdk
127 dot = platform.find(".")
128 if dot != -1:
129 platform = platform[:dot]
130 if platform == "iphoneos":
131 platform = "ios"
132 return platform
134 return _get_platform_os(lldb.selected_platform)
137 def platformIsDarwin():
138 """Returns true if the OS triple for the selected platform is any valid apple OS"""
139 return getPlatform() in getDarwinOSTriples()
142 def findMainThreadCheckerDylib():
143 if not platformIsDarwin():
144 return ""
146 if getPlatform() in lldbplatform.translate(lldbplatform.darwin_embedded):
147 return "/Developer/usr/lib/libMainThreadChecker.dylib"
149 with os.popen("xcode-select -p") as output:
150 xcode_developer_path = output.read().strip()
151 mtc_dylib_path = "%s/usr/lib/libMainThreadChecker.dylib" % xcode_developer_path
152 if os.path.isfile(mtc_dylib_path):
153 return mtc_dylib_path
155 return ""
158 class _PlatformContext(object):
159 """Value object class which contains platform-specific options."""
161 def __init__(
162 self, shlib_environment_var, shlib_path_separator, shlib_prefix, shlib_extension
164 self.shlib_environment_var = shlib_environment_var
165 self.shlib_path_separator = shlib_path_separator
166 self.shlib_prefix = shlib_prefix
167 self.shlib_extension = shlib_extension
170 def createPlatformContext():
171 if platformIsDarwin():
172 return _PlatformContext("DYLD_LIBRARY_PATH", ":", "lib", "dylib")
173 elif getPlatform() in ("freebsd", "linux", "netbsd"):
174 return _PlatformContext("LD_LIBRARY_PATH", ":", "lib", "so")
175 else:
176 return _PlatformContext("PATH", ";", "", "dll")
179 def hasChattyStderr(test_case):
180 """Some targets produce garbage on the standard error output. This utility function
181 determines whether the tests can be strict about the expected stderr contents."""
182 if match_android_device(
183 test_case.getArchitecture(), ["aarch64"], range(22, 25 + 1)
185 return True # The dynamic linker on the device will complain about unknown DT entries
186 return False