blob: 4f45643241ed229d069635760442f98f079c7d5c [file] [log] [blame]
Tamas Berghammer2e169022015-04-02 11:07:55 +00001""" This module contains functions used by the test cases to hide the
2architecture and/or the platform dependent nature of the tests. """
3
Zachary Turnerc1b7cd72015-11-05 19:22:28 +00004from __future__ import absolute_import
5
6# System modules
Zachary Turner5d3b3c72016-02-18 18:50:02 +00007import itertools
Zachary Turner62d3a652016-02-03 19:12:30 +00008import re
9import subprocess
Zachary Turner7a5382d2016-02-04 18:03:01 +000010import sys
Kuba Mracekc40ca132017-07-05 16:29:36 +000011import os
Zachary Turnerc1b7cd72015-11-05 19:22:28 +000012
13# Third-party modules
Zachary Turner5d3b3c72016-02-18 18:50:02 +000014import six
Zachary Turner62d3a652016-02-03 19:12:30 +000015from six.moves.urllib import parse as urlparse
Zachary Turnerc1b7cd72015-11-05 19:22:28 +000016
17# LLDB modules
Zachary Turner62d3a652016-02-03 19:12:30 +000018from . import configuration
19import use_lldb_suite
20import lldb
Sagar Thakur9bf15282015-11-19 11:01:21 +000021
Kate Stoneb9c1b512016-09-06 20:57:50 +000022
Tamas Berghammer2e169022015-04-02 11:07:55 +000023def check_first_register_readable(test_case):
Sagar Thakur9bf15282015-11-19 11:01:21 +000024 arch = test_case.getArchitecture()
25
26 if arch in ['x86_64', 'i386']:
Kate Stoneb9c1b512016-09-06 20:57:50 +000027 test_case.expect("register read eax", substrs=['eax = 0x'])
Sagar Thakur9bf15282015-11-19 11:01:21 +000028 elif arch in ['arm']:
Kate Stoneb9c1b512016-09-06 20:57:50 +000029 test_case.expect("register read r0", substrs=['r0 = 0x'])
Sagar Thakur9bf15282015-11-19 11:01:21 +000030 elif arch in ['aarch64']:
Kate Stoneb9c1b512016-09-06 20:57:50 +000031 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'])
Ulrich Weigandbb00d0b2016-04-14 14:28:34 +000034 elif arch in ['s390x']:
Kate Stoneb9c1b512016-09-06 20:57:50 +000035 test_case.expect("register read r0", substrs=['r0 = 0x'])
Tamas Berghammer2e169022015-04-02 11:07:55 +000036 else:
37 # TODO: Add check for other architectures
Kate Stoneb9c1b512016-09-06 20:57:50 +000038 test_case.fail(
39 "Unsupported architecture for test case (arch: %s)" %
40 test_case.getArchitecture())
41
Zachary Turner62d3a652016-02-03 19:12:30 +000042
43def _run_adb_command(cmd, device_id):
44 device_id_args = []
45 if device_id:
46 device_id_args = ["-s", device_id]
47 full_cmd = ["adb"] + device_id_args + cmd
Kate Stoneb9c1b512016-09-06 20:57:50 +000048 p = subprocess.Popen(
49 full_cmd,
50 stdout=subprocess.PIPE,
51 stderr=subprocess.PIPE)
Zachary Turner62d3a652016-02-03 19:12:30 +000052 stdout, stderr = p.communicate()
53 return p.returncode, stdout, stderr
54
Kate Stoneb9c1b512016-09-06 20:57:50 +000055
Pavel Labath01a28ca2017-03-29 21:01:14 +000056def target_is_android():
57 if not hasattr(target_is_android, 'result'):
Zachary Turner62d3a652016-02-03 19:12:30 +000058 triple = lldb.DBG.GetSelectedPlatform().GetTriple()
59 match = re.match(".*-.*-.*-android", triple)
Pavel Labath01a28ca2017-03-29 21:01:14 +000060 target_is_android.result = match is not None
61 return target_is_android.result
Zachary Turner62d3a652016-02-03 19:12:30 +000062
Kate Stoneb9c1b512016-09-06 20:57:50 +000063
Zachary Turner62d3a652016-02-03 19:12:30 +000064def android_device_api():
65 if not hasattr(android_device_api, 'result'):
66 assert configuration.lldb_platform_url is not None
67 device_id = None
68 parsed_url = urlparse.urlparse(configuration.lldb_platform_url)
69 host_name = parsed_url.netloc.split(":")[0]
70 if host_name != 'localhost':
71 device_id = host_name
72 if device_id.startswith('[') and device_id.endswith(']'):
73 device_id = device_id[1:-1]
74 retcode, stdout, stderr = _run_adb_command(
75 ["shell", "getprop", "ro.build.version.sdk"], device_id)
76 if retcode == 0:
77 android_device_api.result = int(stdout)
78 else:
79 raise LookupError(
80 ">>> Unable to determine the API level of the Android device.\n"
81 ">>> stdout:\n%s\n"
Kate Stoneb9c1b512016-09-06 20:57:50 +000082 ">>> stderr:\n%s\n" %
83 (stdout, stderr))
Zachary Turner62d3a652016-02-03 19:12:30 +000084 return android_device_api.result
85
Kate Stoneb9c1b512016-09-06 20:57:50 +000086
Zachary Turner62d3a652016-02-03 19:12:30 +000087def match_android_device(device_arch, valid_archs=None, valid_api_levels=None):
Pavel Labath01a28ca2017-03-29 21:01:14 +000088 if not target_is_android():
Zachary Turner62d3a652016-02-03 19:12:30 +000089 return False
90 if valid_archs is not None and device_arch not in valid_archs:
91 return False
92 if valid_api_levels is not None and android_device_api() not in valid_api_levels:
93 return False
94
95 return True
96
Kate Stoneb9c1b512016-09-06 20:57:50 +000097
Zachary Turner62d3a652016-02-03 19:12:30 +000098def finalize_build_dictionary(dictionary):
Pavel Labath01a28ca2017-03-29 21:01:14 +000099 if target_is_android():
Zachary Turner62d3a652016-02-03 19:12:30 +0000100 if dictionary is None:
101 dictionary = {}
102 dictionary["OS"] = "Android"
103 if android_device_api() >= 16:
104 dictionary["PIE"] = 1
105 return dictionary
Zachary Turner7a5382d2016-02-04 18:03:01 +0000106
Kate Stoneb9c1b512016-09-06 20:57:50 +0000107
Zachary Turner7a5382d2016-02-04 18:03:01 +0000108def getHostPlatform():
109 """Returns the host platform running the test suite."""
110 # Attempts to return a platform name matching a target Triple platform.
111 if sys.platform.startswith('linux'):
112 return 'linux'
113 elif sys.platform.startswith('win32'):
114 return 'windows'
115 elif sys.platform.startswith('darwin'):
116 return 'darwin'
117 elif sys.platform.startswith('freebsd'):
118 return 'freebsd'
119 elif sys.platform.startswith('netbsd'):
120 return 'netbsd'
121 else:
122 return sys.platform
Zachary Turner9a1a2942016-02-04 23:04:17 +0000123
124
125def getDarwinOSTriples():
126 return ['darwin', 'macosx', 'ios']
127
Kate Stoneb9c1b512016-09-06 20:57:50 +0000128
Zachary Turner9a1a2942016-02-04 23:04:17 +0000129def getPlatform():
130 """Returns the target platform which the tests are running on."""
131 platform = lldb.DBG.GetSelectedPlatform().GetTriple().split('-')[2]
132 if platform.startswith('freebsd'):
133 platform = 'freebsd'
134 elif platform.startswith('netbsd'):
135 platform = 'netbsd'
136 return platform
137
Kate Stoneb9c1b512016-09-06 20:57:50 +0000138
Zachary Turner9a1a2942016-02-04 23:04:17 +0000139def platformIsDarwin():
140 """Returns true if the OS triple for the selected platform is any valid apple OS"""
141 return getPlatform() in getDarwinOSTriples()
142
Kate Stoneb9c1b512016-09-06 20:57:50 +0000143
Kuba Mracekc40ca132017-07-05 16:29:36 +0000144def findMainThreadCheckerDylib():
145 if not platformIsDarwin():
146 return ""
147
148 with os.popen('xcode-select -p') as output:
149 xcode_developer_path = output.read().strip()
150 mtc_dylib_path = '%s/usr/lib/libMainThreadChecker.dylib' % xcode_developer_path
151 if os.path.isfile(mtc_dylib_path):
152 return mtc_dylib_path
153
154 return ""
155
156
Zachary Turner9a1a2942016-02-04 23:04:17 +0000157class _PlatformContext(object):
158 """Value object class which contains platform-specific options."""
159
160 def __init__(self, shlib_environment_var, shlib_prefix, shlib_extension):
161 self.shlib_environment_var = shlib_environment_var
162 self.shlib_prefix = shlib_prefix
163 self.shlib_extension = shlib_extension
164
Kate Stoneb9c1b512016-09-06 20:57:50 +0000165
Zachary Turner9a1a2942016-02-04 23:04:17 +0000166def createPlatformContext():
167 if platformIsDarwin():
168 return _PlatformContext('DYLD_LIBRARY_PATH', 'lib', 'dylib')
169 elif getPlatform() in ("freebsd", "linux", "netbsd"):
170 return _PlatformContext('LD_LIBRARY_PATH', 'lib', 'so')
171 else:
172 return None
Pavel Labathe6961d02016-04-14 15:52:53 +0000173
Kate Stoneb9c1b512016-09-06 20:57:50 +0000174
Pavel Labathe6961d02016-04-14 15:52:53 +0000175def hasChattyStderr(test_case):
176 """Some targets produce garbage on the standard error output. This utility function
177 determines whether the tests can be strict about the expected stderr contents."""
178 if match_android_device(test_case.getArchitecture(), ['aarch64'], [22]):
Kate Stoneb9c1b512016-09-06 20:57:50 +0000179 return True # The dynamic linker on the device will complain about unknown DT entries
Pavel Labathe6961d02016-04-14 15:52:53 +0000180 return False