xref: /llvm-project/lldb/test/API/tools/lldb-server/TestGdbRemoteProcessInfo.py (revision 2238dcc39358353cac21df75c3c3286ab20b8f53)
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    def test_qProcessInfo_returns_running_process(self):
10        self.build()
11        procs = self.prep_debug_monitor_and_inferior()
12        self.add_process_info_collection_packets()
13
14        # Run the stream
15        context = self.expect_gdbremote_sequence()
16        self.assertIsNotNone(context)
17
18        # Gather process info response
19        process_info = self.parse_process_info_response(context)
20        self.assertIsNotNone(process_info)
21
22        # Ensure the process id looks reasonable.
23        pid_text = process_info.get("pid")
24        self.assertIsNotNone(pid_text)
25        pid = int(pid_text, base=16)
26        self.assertNotEqual(0, pid)
27
28        # If possible, verify that the process is running.
29        self.assertTrue(lldbgdbserverutils.process_is_running(pid, True))
30
31    def test_attach_commandline_qProcessInfo_reports_correct_pid(self):
32        self.build()
33        self.set_inferior_startup_attach()
34        procs = self.prep_debug_monitor_and_inferior()
35        self.assertIsNotNone(procs)
36        self.add_process_info_collection_packets()
37
38        # Run the stream
39        context = self.expect_gdbremote_sequence()
40        self.assertIsNotNone(context)
41
42        # Gather process info response
43        process_info = self.parse_process_info_response(context)
44        self.assertIsNotNone(process_info)
45
46        # Ensure the process id matches what we expected.
47        pid_text = process_info.get("pid", None)
48        self.assertIsNotNone(pid_text)
49        reported_pid = int(pid_text, base=16)
50        self.assertEqual(reported_pid, procs["inferior"].pid)
51
52    def test_qProcessInfo_reports_valid_endian(self):
53        self.build()
54        procs = self.prep_debug_monitor_and_inferior()
55        self.add_process_info_collection_packets()
56
57        # Run the stream
58        context = self.expect_gdbremote_sequence()
59        self.assertIsNotNone(context)
60
61        # Gather process info response
62        process_info = self.parse_process_info_response(context)
63        self.assertIsNotNone(process_info)
64
65        # Ensure the process id looks reasonable.
66        endian = process_info.get("endian")
67        self.assertIsNotNone(endian)
68        self.assertIn(endian, ["little", "big", "pdp"])
69
70    def qProcessInfo_contains_keys(self, expected_key_set):
71        procs = self.prep_debug_monitor_and_inferior()
72        self.add_process_info_collection_packets()
73
74        # Run the stream
75        context = self.expect_gdbremote_sequence()
76        self.assertIsNotNone(context)
77
78        # Gather process info response
79        process_info = self.parse_process_info_response(context)
80        self.assertIsNotNone(process_info)
81
82        # Ensure the expected keys are present and non-None within the process
83        # info.
84        missing_key_set = set()
85        for expected_key in expected_key_set:
86            if expected_key not in process_info:
87                missing_key_set.add(expected_key)
88
89        self.assertEqual(
90            missing_key_set,
91            set(),
92            "the listed keys are missing in the qProcessInfo result",
93        )
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
119    @add_test_categories(["debugserver"])
120    def test_qProcessInfo_contains_cputype_cpusubtype(self):
121        self.build()
122        self.qProcessInfo_contains_keys(set(["cputype", "cpusubtype"]))
123
124    @add_test_categories(["llgs"])
125    def test_qProcessInfo_contains_triple_ppid(self):
126        self.build()
127        self.qProcessInfo_contains_keys(set(["triple", "parent-pid"]))
128
129    @add_test_categories(["debugserver"])
130    def test_qProcessInfo_does_not_contain_triple(self):
131        self.build()
132        # We don't expect to see triple on darwin.  If we do, we'll prefer triple
133        # to cputype/cpusubtype and skip some darwin-based ProcessGDBRemote ArchSpec setup
134        # for the remote Host and Process.
135        self.qProcessInfo_does_not_contain_keys(set(["triple"]))
136
137    @add_test_categories(["llgs"])
138    def test_qProcessInfo_does_not_contain_cputype_cpusubtype(self):
139        self.build()
140        self.qProcessInfo_does_not_contain_keys(set(["cputype", "cpusubtype"]))
141