1import gdbremote_testcase
2import lldbgdbserverutils
3from lldbsuite.test.decorators import *
4from lldbsuite.test.lldbtest import *
5from lldbsuite.test import lldbutil
6
7
8class TestGdbRemoteProcessInfo(gdbremote_testcase.GdbRemoteTestCaseBase):
9
10    def test_qProcessInfo_returns_running_process(self):
11        self.build()
12        procs = self.prep_debug_monitor_and_inferior()
13        self.add_process_info_collection_packets()
14
15        # Run the stream
16        context = self.expect_gdbremote_sequence()
17        self.assertIsNotNone(context)
18
19        # Gather process info response
20        process_info = self.parse_process_info_response(context)
21        self.assertIsNotNone(process_info)
22
23        # Ensure the process id looks reasonable.
24        pid_text = process_info.get("pid")
25        self.assertIsNotNone(pid_text)
26        pid = int(pid_text, base=16)
27        self.assertNotEqual(0, pid)
28
29        # If possible, verify that the process is running.
30        self.assertTrue(lldbgdbserverutils.process_is_running(pid, True))
31
32    def test_attach_commandline_qProcessInfo_reports_correct_pid(self):
33        self.build()
34        self.set_inferior_startup_attach()
35        procs = self.prep_debug_monitor_and_inferior()
36        self.assertIsNotNone(procs)
37        self.add_process_info_collection_packets()
38
39        # Run the stream
40        context = self.expect_gdbremote_sequence()
41        self.assertIsNotNone(context)
42
43        # Gather process info response
44        process_info = self.parse_process_info_response(context)
45        self.assertIsNotNone(process_info)
46
47        # Ensure the process id matches what we expected.
48        pid_text = process_info.get('pid', None)
49        self.assertIsNotNone(pid_text)
50        reported_pid = int(pid_text, base=16)
51        self.assertEqual(reported_pid, procs["inferior"].pid)
52
53    def test_qProcessInfo_reports_valid_endian(self):
54        self.build()
55        procs = self.prep_debug_monitor_and_inferior()
56        self.add_process_info_collection_packets()
57
58        # Run the stream
59        context = self.expect_gdbremote_sequence()
60        self.assertIsNotNone(context)
61
62        # Gather process info response
63        process_info = self.parse_process_info_response(context)
64        self.assertIsNotNone(process_info)
65
66        # Ensure the process id looks reasonable.
67        endian = process_info.get("endian")
68        self.assertIsNotNone(endian)
69        self.assertIn(endian, ["little", "big", "pdp"])
70
71    def qProcessInfo_contains_keys(self, expected_key_set):
72        procs = self.prep_debug_monitor_and_inferior()
73        self.add_process_info_collection_packets()
74
75        # Run the stream
76        context = self.expect_gdbremote_sequence()
77        self.assertIsNotNone(context)
78
79        # Gather process info response
80        process_info = self.parse_process_info_response(context)
81        self.assertIsNotNone(process_info)
82
83        # Ensure the expected keys are present and non-None within the process
84        # info.
85        missing_key_set = set()
86        for expected_key in expected_key_set:
87            if expected_key not in process_info:
88                missing_key_set.add(expected_key)
89
90        self.assertEqual(
91            missing_key_set,
92            set(),
93            "the listed keys are missing in the qProcessInfo result")
94
95    def qProcessInfo_does_not_contain_keys(self, absent_key_set):
96        procs = self.prep_debug_monitor_and_inferior()
97        self.add_process_info_collection_packets()
98
99        # Run the stream
100        context = self.expect_gdbremote_sequence()
101        self.assertIsNotNone(context)
102
103        # Gather process info response
104        process_info = self.parse_process_info_response(context)
105        self.assertIsNotNone(process_info)
106
107        # Ensure the unexpected keys are not present
108        unexpected_key_set = set()
109        for unexpected_key in absent_key_set:
110            if unexpected_key in process_info:
111                unexpected_key_set.add(unexpected_key)
112
113        self.assertEqual(
114            unexpected_key_set,
115            set(),
116            "the listed keys were present but unexpected in qProcessInfo result")
117
118    @add_test_categories(["debugserver"])
119    def test_qProcessInfo_contains_cputype_cpusubtype(self):
120        self.build()
121        self.qProcessInfo_contains_keys(set(['cputype', 'cpusubtype']))
122
123    @add_test_categories(["llgs"])
124    def test_qProcessInfo_contains_triple_ppid(self):
125        self.build()
126        self.qProcessInfo_contains_keys(set(['triple', 'parent-pid']))
127
128    @add_test_categories(["debugserver"])
129    def test_qProcessInfo_does_not_contain_triple(self):
130        self.build()
131        # We don't expect to see triple on darwin.  If we do, we'll prefer triple
132        # to cputype/cpusubtype and skip some darwin-based ProcessGDBRemote ArchSpec setup
133        # for the remote Host and Process.
134        self.qProcessInfo_does_not_contain_keys(set(['triple']))
135
136    @add_test_categories(["llgs"])
137    def test_qProcessInfo_does_not_contain_cputype_cpusubtype(self):
138        self.build()
139        self.qProcessInfo_does_not_contain_keys(set(['cputype', 'cpusubtype']))
140