1 """ This module contains functions used by the test cases to hide the
2 architecture and/or the platform dependent nature of the tests. """
10 from urllib
.parse
import urlparse
13 from . import configuration
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"])
34 # TODO: Add check for other architectures
36 "Unsupported architecture for test case (arch: %s)"
37 % test_case
.getArchitecture()
41 def _run_adb_command(cmd
, 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
59 parsed_url
= urlparse(configuration
.lldb_platform_url
)
60 host_name
= parsed_url
.netloc
.split(":")[0]
61 if host_name
!= "localhost":
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
69 android_device_api
.result
= int(stdout
)
72 ">>> Unable to determine the API level of the Android device.\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():
82 if valid_archs
is not None and device_arch
not in valid_archs
:
84 if valid_api_levels
is not None and android_device_api() not in valid_api_levels
:
90 def finalize_build_dictionary(dictionary
):
91 if target_is_android():
92 if dictionary
is None:
94 dictionary
["OS"] = "Android"
99 def _get_platform_os(p
):
100 # Use the triple to determine the platform if set.
101 triple
= p
.GetTriple()
103 platform
= triple
.split("-")[2]
104 if platform
.startswith("freebsd"):
106 elif platform
.startswith("netbsd"):
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
)
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(".")
129 platform
= platform
[:dot
]
130 if platform
== "iphoneos":
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():
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
158 class _PlatformContext(object):
159 """Value object class which contains platform-specific options."""
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")
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