1from __future__ import print_function
2
3# lldb test suite imports
4from lldbsuite.test.decorators import *
5from lldbsuite.test.lldbtest import TestBase
6
7# gdb-remote-specific imports
8import lldbgdbserverutils
9from gdbremote_testcase import GdbRemoteTestCaseBase
10
11
12class TestGdbRemoteHostInfo(GdbRemoteTestCaseBase):
13
14    mydir = TestBase.compute_mydir(__file__)
15
16    KNOWN_HOST_INFO_KEYS = set([
17        "addressing_bits",
18        "arch",
19        "cpusubtype",
20        "cputype",
21        "default_packet_timeout",
22        "distribution_id",
23        "endian",
24        "hostname",
25        "maccatalyst_version",
26        "os_build",
27        "os_kernel",
28        "os_version",
29        "ostype",
30        "ptrsize",
31        "triple",
32        "vendor",
33        "watchpoint_exceptions_received",
34    ])
35
36    DARWIN_REQUIRED_HOST_INFO_KEYS = set([
37        "cputype",
38        "cpusubtype",
39        "endian",
40        "ostype",
41        "ptrsize",
42        "vendor",
43        "watchpoint_exceptions_received"
44    ])
45
46    def add_host_info_collection_packets(self):
47        self.test_sequence.add_log_lines(
48            ["read packet: $qHostInfo#9b",
49             {"direction": "send", "regex": r"^\$(.+)#[0-9a-fA-F]{2}$",
50              "capture": {1: "host_info_raw"}}],
51            True)
52
53    def parse_host_info_response(self, context):
54        # Ensure we have a host info response.
55        self.assertIsNotNone(context)
56        host_info_raw = context.get("host_info_raw")
57        self.assertIsNotNone(host_info_raw)
58
59        # Pull out key:value; pairs.
60        host_info_dict = {match.group(1): match.group(2)
61                          for match in re.finditer(r"([^:]+):([^;]+);",
62                                                   host_info_raw)}
63
64        import pprint
65        print("\nqHostInfo response:")
66        pprint.pprint(host_info_dict)
67
68        # Validate keys are known.
69        for (key, val) in list(host_info_dict.items()):
70            self.assertIn(key, self.KNOWN_HOST_INFO_KEYS,
71                          "unknown qHostInfo key: " + key)
72            self.assertIsNotNone(val)
73
74        # Return the key:val pairs.
75        return host_info_dict
76
77    def get_qHostInfo_response(self):
78        # Launch the debug monitor stub, attaching to the inferior.
79        server = self.connect_to_debug_monitor()
80        self.assertIsNotNone(server)
81        self.do_handshake()
82
83        # Request qHostInfo and get response
84        self.add_host_info_collection_packets()
85        context = self.expect_gdbremote_sequence()
86        self.assertIsNotNone(context)
87
88        # Parse qHostInfo response.
89        host_info = self.parse_host_info_response(context)
90        self.assertIsNotNone(host_info)
91        self.assertGreater(len(host_info), 0, "qHostInfo should have returned "
92                           "at least one key:val pair.")
93        return host_info
94
95    def validate_darwin_minimum_host_info_keys(self, host_info_dict):
96        self.assertIsNotNone(host_info_dict)
97        missing_keys = [key for key in self.DARWIN_REQUIRED_HOST_INFO_KEYS
98                        if key not in host_info_dict]
99        self.assertEquals(0, len(missing_keys),
100                          "qHostInfo is missing the following required "
101                          "keys: " + str(missing_keys))
102
103    def test_qHostInfo_returns_at_least_one_key_val_pair(self):
104        self.build()
105        self.get_qHostInfo_response()
106
107    @skipUnlessDarwin
108    def test_qHostInfo_contains_darwin_required_keys(self):
109        self.build()
110        host_info_dict = self.get_qHostInfo_response()
111        self.validate_darwin_minimum_host_info_keys(host_info_dict)
112