Tamas Berghammer | 2e16902 | 2015-04-02 11:07:55 +0000 | [diff] [blame] | 1 | """ This module contains functions used by the test cases to hide the |
| 2 | architecture and/or the platform dependent nature of the tests. """ |
| 3 | |
Zachary Turner | c1b7cd7 | 2015-11-05 19:22:28 +0000 | [diff] [blame] | 4 | from __future__ import absolute_import |
| 5 | |
| 6 | # System modules |
Zachary Turner | 62d3a65 | 2016-02-03 19:12:30 +0000 | [diff] [blame] | 7 | import re |
| 8 | import subprocess |
Zachary Turner | 7a5382d | 2016-02-04 18:03:01 +0000 | [diff] [blame^] | 9 | import sys |
Zachary Turner | c1b7cd7 | 2015-11-05 19:22:28 +0000 | [diff] [blame] | 10 | |
| 11 | # Third-party modules |
Zachary Turner | 62d3a65 | 2016-02-03 19:12:30 +0000 | [diff] [blame] | 12 | from six.moves.urllib import parse as urlparse |
Zachary Turner | c1b7cd7 | 2015-11-05 19:22:28 +0000 | [diff] [blame] | 13 | |
| 14 | # LLDB modules |
Zachary Turner | 62d3a65 | 2016-02-03 19:12:30 +0000 | [diff] [blame] | 15 | from . import configuration |
| 16 | import use_lldb_suite |
| 17 | import lldb |
Sagar Thakur | 9bf1528 | 2015-11-19 11:01:21 +0000 | [diff] [blame] | 18 | |
Tamas Berghammer | 2e16902 | 2015-04-02 11:07:55 +0000 | [diff] [blame] | 19 | def check_first_register_readable(test_case): |
Sagar Thakur | 9bf1528 | 2015-11-19 11:01:21 +0000 | [diff] [blame] | 20 | arch = test_case.getArchitecture() |
| 21 | |
| 22 | if arch in ['x86_64', 'i386']: |
Tamas Berghammer | 2e16902 | 2015-04-02 11:07:55 +0000 | [diff] [blame] | 23 | test_case.expect("register read eax", substrs = ['eax = 0x']) |
Sagar Thakur | 9bf1528 | 2015-11-19 11:01:21 +0000 | [diff] [blame] | 24 | elif arch in ['arm']: |
Tamas Berghammer | 3215d04 | 2015-04-17 09:37:06 +0000 | [diff] [blame] | 25 | test_case.expect("register read r0", substrs = ['r0 = 0x']) |
Sagar Thakur | 9bf1528 | 2015-11-19 11:01:21 +0000 | [diff] [blame] | 26 | elif arch in ['aarch64']: |
Tamas Berghammer | 2e16902 | 2015-04-02 11:07:55 +0000 | [diff] [blame] | 27 | test_case.expect("register read x0", substrs = ['x0 = 0x']) |
Sagar Thakur | 9bf1528 | 2015-11-19 11:01:21 +0000 | [diff] [blame] | 28 | elif re.match("mips",arch): |
| 29 | test_case.expect("register read zero", substrs = ['zero = 0x']) |
Tamas Berghammer | 2e16902 | 2015-04-02 11:07:55 +0000 | [diff] [blame] | 30 | else: |
| 31 | # TODO: Add check for other architectures |
| 32 | test_case.fail("Unsupported architecture for test case (arch: %s)" % test_case.getArchitecture()) |
Zachary Turner | 62d3a65 | 2016-02-03 19:12:30 +0000 | [diff] [blame] | 33 | |
| 34 | def _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 Turner | 3abbf6f | 2016-02-03 22:53:18 +0000 | [diff] [blame] | 39 | p = subprocess.Popen(full_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
Zachary Turner | 62d3a65 | 2016-02-03 19:12:30 +0000 | [diff] [blame] | 40 | stdout, stderr = p.communicate() |
| 41 | return p.returncode, stdout, stderr |
| 42 | |
| 43 | def _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 | |
| 50 | def 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 | |
| 71 | def 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 | |
| 81 | def 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 Turner | 7a5382d | 2016-02-04 18:03:01 +0000 | [diff] [blame^] | 89 | |
| 90 | def 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 |