aboutsummaryrefslogtreecommitdiff
path: root/lldb/test/API/tools/lldb-server/TestGdbRemoteHostInfo.py
blob: 1b7c963550592bb20443eec337640a63badbf8a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# lldb test suite imports
from lldbsuite.test.decorators import *
from lldbsuite.test.lldbtest import TestBase

# gdb-remote-specific imports
import lldbgdbserverutils
from gdbremote_testcase import GdbRemoteTestCaseBase


class TestGdbRemoteHostInfo(GdbRemoteTestCaseBase):
    KNOWN_HOST_INFO_KEYS = set(
        [
            "addressing_bits",
            "arch",
            "cpusubtype",
            "cputype",
            "default_packet_timeout",
            "distribution_id",
            "endian",
            "hostname",
            "maccatalyst_version",
            "os_build",
            "os_kernel",
            "os_version",
            "ostype",
            "ptrsize",
            "triple",
            "vendor",
            "vm-page-size",
            "watchpoint_exceptions_received",
        ]
    )

    DARWIN_REQUIRED_HOST_INFO_KEYS = set(
        [
            "cputype",
            "cpusubtype",
            "endian",
            "ostype",
            "ptrsize",
            "vendor",
            "watchpoint_exceptions_received",
        ]
    )

    def add_host_info_collection_packets(self):
        self.test_sequence.add_log_lines(
            [
                "read packet: $qHostInfo#9b",
                {
                    "direction": "send",
                    "regex": r"^\$(.+)#[0-9a-fA-F]{2}$",
                    "capture": {1: "host_info_raw"},
                },
            ],
            True,
        )

    def parse_host_info_response(self, context):
        # Ensure we have a host info response.
        self.assertIsNotNone(context)
        host_info_raw = context.get("host_info_raw")
        self.assertIsNotNone(host_info_raw)

        # Pull out key:value; pairs.
        host_info_dict = {
            match.group(1): match.group(2)
            for match in re.finditer(r"([^:]+):([^;]+);", host_info_raw)
        }

        import pprint

        print("\nqHostInfo response:")
        pprint.pprint(host_info_dict)

        # Validate keys are known.
        for key, val in list(host_info_dict.items()):
            self.assertIn(
                key, self.KNOWN_HOST_INFO_KEYS, "unknown qHostInfo key: " + key
            )
            self.assertIsNotNone(val)

        # Return the key:val pairs.
        return host_info_dict

    def get_qHostInfo_response(self):
        # Launch the debug monitor stub, attaching to the inferior.
        server = self.connect_to_debug_monitor()
        self.assertIsNotNone(server)
        self.do_handshake()

        # Request qHostInfo and get response
        self.add_host_info_collection_packets()
        context = self.expect_gdbremote_sequence()
        self.assertIsNotNone(context)

        # Parse qHostInfo response.
        host_info = self.parse_host_info_response(context)
        self.assertIsNotNone(host_info)
        self.assertGreater(
            len(host_info),
            0,
            "qHostInfo should have returned " "at least one key:val pair.",
        )
        return host_info

    def validate_darwin_minimum_host_info_keys(self, host_info_dict):
        self.assertIsNotNone(host_info_dict)
        missing_keys = [
            key
            for key in self.DARWIN_REQUIRED_HOST_INFO_KEYS
            if key not in host_info_dict
        ]
        self.assertEqual(
            0,
            len(missing_keys),
            "qHostInfo is missing the following required " "keys: " + str(missing_keys),
        )

    def test_qHostInfo_returns_at_least_one_key_val_pair(self):
        self.build()
        self.get_qHostInfo_response()

    @skipUnlessDarwin
    def test_qHostInfo_contains_darwin_required_keys(self):
        self.build()
        host_info_dict = self.get_qHostInfo_response()
        self.validate_darwin_minimum_host_info_keys(host_info_dict)