blob: a6d9b8faf7266827b9705cdd46da12ed7ca27da1 [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 Turner62d3a652016-02-03 19:12:30 +00007import re
8import subprocess
Zachary Turner7a5382d2016-02-04 18:03:01 +00009import sys
Zachary Turnerc1b7cd72015-11-05 19:22:28 +000010
11# Third-party modules
Zachary Turner62d3a652016-02-03 19:12:30 +000012from six.moves.urllib import parse as urlparse
Zachary Turnerc1b7cd72015-11-05 19:22:28 +000013
14# LLDB modules
Zachary Turner62d3a652016-02-03 19:12:30 +000015from . import configuration
16import use_lldb_suite
17import lldb
Sagar Thakur9bf15282015-11-19 11:01:21 +000018
Tamas Berghammer2e169022015-04-02 11:07:55 +000019def check_first_register_readable(test_case):
Sagar Thakur9bf15282015-11-19 11:01:21 +000020 arch = test_case.getArchitecture()
21
22 if arch in ['x86_64', 'i386']:
Tamas Berghammer2e169022015-04-02 11:07:55 +000023 test_case.expect("register read eax", substrs = ['eax = 0x'])
Sagar Thakur9bf15282015-11-19 11:01:21 +000024 elif arch in ['arm']:
Tamas Berghammer3215d042015-04-17 09:37:06 +000025 test_case.expect("register read r0", substrs = ['r0 = 0x'])
Sagar Thakur9bf15282015-11-19 11:01:21 +000026 elif arch in ['aarch64']:
Tamas Berghammer2e169022015-04-02 11:07:55 +000027 test_case.expect("register read x0", substrs = ['x0 = 0x'])
Sagar Thakur9bf15282015-11-19 11:01:21 +000028 elif re.match("mips",arch):
29 test_case.expect("register read zero", substrs = ['zero = 0x'])
Tamas Berghammer2e169022015-04-02 11:07:55 +000030 else:
31 # TODO: Add check for other architectures
32 test_case.fail("Unsupported architecture for test case (arch: %s)" % test_case.getArchitecture())
Zachary Turner62d3a652016-02-03 19:12:30 +000033
34def _run_adb_command(cmd, device_id):
35 device_id_args = []
36 if device_id:
37 device_id_args = ["-s", device_id]
38 full_cmd = ["adb"] + device_id_args + cmd
Zachary Turner3abbf6f2016-02-03 22:53:18 +000039 p = subprocess.Popen(full_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
Zachary Turner62d3a652016-02-03 19:12:30 +000040 stdout, stderr = p.communicate()
41 return p.returncode, stdout, stderr
42
43def _target_is_android():
44 if not hasattr(_target_is_android, 'result'):
45 triple = lldb.DBG.GetSelectedPlatform().GetTriple()
46 match = re.match(".*-.*-.*-android", triple)
47 _target_is_android.result = match is not None
48 return _target_is_android.result
49
50def android_device_api():
51 if not hasattr(android_device_api, 'result'):
52 assert configuration.lldb_platform_url is not None
53 device_id = None
54 parsed_url = urlparse.urlparse(configuration.lldb_platform_url)
55 host_name = parsed_url.netloc.split(":")[0]
56 if host_name != 'localhost':
57 device_id = host_name
58 if device_id.startswith('[') and device_id.endswith(']'):
59 device_id = device_id[1:-1]
60 retcode, stdout, stderr = _run_adb_command(
61 ["shell", "getprop", "ro.build.version.sdk"], device_id)
62 if retcode == 0:
63 android_device_api.result = int(stdout)
64 else:
65 raise LookupError(
66 ">>> Unable to determine the API level of the Android device.\n"
67 ">>> stdout:\n%s\n"
68 ">>> stderr:\n%s\n" % (stdout, stderr))
69 return android_device_api.result
70
71def match_android_device(device_arch, valid_archs=None, valid_api_levels=None):
72 if not _target_is_android():
73 return False
74 if valid_archs is not None and device_arch not in valid_archs:
75 return False
76 if valid_api_levels is not None and android_device_api() not in valid_api_levels:
77 return False
78
79 return True
80
81def finalize_build_dictionary(dictionary):
82 if _target_is_android():
83 if dictionary is None:
84 dictionary = {}
85 dictionary["OS"] = "Android"
86 if android_device_api() >= 16:
87 dictionary["PIE"] = 1
88 return dictionary
Zachary Turner7a5382d2016-02-04 18:03:01 +000089
90def getHostPlatform():
91 """Returns the host platform running the test suite."""
92 # Attempts to return a platform name matching a target Triple platform.
93 if sys.platform.startswith('linux'):
94 return 'linux'
95 elif sys.platform.startswith('win32'):
96 return 'windows'
97 elif sys.platform.startswith('darwin'):
98 return 'darwin'
99 elif sys.platform.startswith('freebsd'):
100 return 'freebsd'
101 elif sys.platform.startswith('netbsd'):
102 return 'netbsd'
103 else:
104 return sys.platform