1import json
2import re
3
4import gdbremote_testcase
5from lldbsuite.test.decorators import *
6from lldbsuite.test.lldbtest import *
7from lldbsuite.test import lldbutil
8
9class TestGdbRemoteThreadsInStopReply(
10        gdbremote_testcase.GdbRemoteTestCaseBase):
11
12    mydir = TestBase.compute_mydir(__file__)
13
14    ENABLE_THREADS_IN_STOP_REPLY_ENTRIES = [
15        "read packet: $QListThreadsInStopReply#21",
16        "send packet: $OK#00",
17    ]
18
19    def gather_stop_reply_fields(self, thread_count, field_names):
20        context, threads = self.launch_with_threads(thread_count)
21        key_vals_text = context.get("stop_reply_kv")
22        self.assertIsNotNone(key_vals_text)
23
24        self.reset_test_sequence()
25        self.add_register_info_collection_packets()
26        self.add_process_info_collection_packets()
27
28        context = self.expect_gdbremote_sequence()
29        self.assertIsNotNone(context)
30        hw_info = self.parse_hw_info(context)
31
32        # Parse the stop reply contents.
33        kv_dict = self.parse_key_val_dict(key_vals_text)
34
35        result = dict();
36        result["pc_register"] = hw_info["pc_register"]
37        result["little_endian"] = hw_info["little_endian"]
38        for key_field in field_names:
39            result[key_field] = kv_dict.get(key_field)
40
41        return result
42
43    def gather_stop_reply_threads(self, thread_count):
44        # Pull out threads from stop response.
45        stop_reply_threads_text = self.gather_stop_reply_fields(
46                thread_count, ["threads"])["threads"]
47        if stop_reply_threads_text:
48            return [int(thread_id, 16)
49                    for thread_id in stop_reply_threads_text.split(",")]
50        else:
51            return []
52
53    def gather_stop_reply_pcs(self, thread_count):
54        results = self.gather_stop_reply_fields(thread_count, ["threads", "thread-pcs"])
55        if not results:
56            return []
57
58        threads_text = results["threads"]
59        pcs_text = results["thread-pcs"]
60        thread_ids = threads_text.split(",")
61        pcs = pcs_text.split(",")
62        self.assertEquals(len(thread_ids), len(pcs))
63
64        thread_pcs = dict()
65        for i in range(0, len(pcs)):
66            thread_pcs[int(thread_ids[i], 16)] = pcs[i]
67
68        result = dict()
69        result["thread_pcs"] = thread_pcs
70        result["pc_register"] = results["pc_register"]
71        result["little_endian"] = results["little_endian"]
72        return result
73
74    def switch_endian(self, egg):
75        return "".join(reversed(re.findall("..", egg)))
76
77    def parse_hw_info(self, context):
78        self.assertIsNotNone(context)
79        process_info = self.parse_process_info_response(context)
80        endian = process_info.get("endian")
81        reg_info = self.parse_register_info_packets(context)
82        (pc_lldb_reg_index, pc_reg_info) = self.find_pc_reg_info(reg_info)
83
84        hw_info = dict()
85        hw_info["pc_register"] = pc_lldb_reg_index
86        hw_info["little_endian"] = (endian == "little")
87        return hw_info
88
89    def gather_threads_info_pcs(self, pc_register, little_endian):
90        self.reset_test_sequence()
91        self.test_sequence.add_log_lines(
92                [
93                    "read packet: $jThreadsInfo#c1",
94                    {
95                        "direction": "send",
96                        "regex": r"^\$(.*)#[0-9a-fA-F]{2}$",
97                        "capture": {
98                            1: "threads_info"}},
99                ],
100                True)
101
102        context = self.expect_gdbremote_sequence()
103        self.assertIsNotNone(context)
104        threads_info = context.get("threads_info")
105        register = str(pc_register)
106        # The jThreadsInfo response is not valid JSON data, so we have to
107        # clean it up first.
108        jthreads_info = json.loads(re.sub(r"}]", "}", threads_info))
109        thread_pcs = dict()
110        for thread_info in jthreads_info:
111            tid = thread_info["tid"]
112            pc = thread_info["registers"][register]
113            thread_pcs[tid] = self.switch_endian(pc) if little_endian else pc
114
115        return thread_pcs
116
117
118    def test_QListThreadsInStopReply_supported(self):
119        self.build()
120        self.set_inferior_startup_launch()
121        procs = self.prep_debug_monitor_and_inferior()
122        self.test_sequence.add_log_lines(
123            self.ENABLE_THREADS_IN_STOP_REPLY_ENTRIES, True)
124
125        context = self.expect_gdbremote_sequence()
126        self.assertIsNotNone(context)
127
128    @skipIfNetBSD
129    @expectedFailureAll(oslist=["windows"]) # Extra threads present
130    def test_stop_reply_reports_multiple_threads(self):
131        self.build()
132        self.set_inferior_startup_launch()
133        # Gather threads from stop notification when QThreadsInStopReply is
134        # enabled.
135        self.test_sequence.add_log_lines(
136            self.ENABLE_THREADS_IN_STOP_REPLY_ENTRIES, True)
137        stop_reply_threads = self.gather_stop_reply_threads(5)
138        self.assertEqual(len(stop_reply_threads), 5)
139
140    @skipIfNetBSD
141    def test_no_QListThreadsInStopReply_supplies_no_threads(self):
142        self.build()
143        self.set_inferior_startup_launch()
144        # Gather threads from stop notification when QThreadsInStopReply is not
145        # enabled.
146        stop_reply_threads = self.gather_stop_reply_threads(5)
147        self.assertEqual(len(stop_reply_threads), 0)
148
149    @skipIfNetBSD
150    def test_stop_reply_reports_correct_threads(self):
151        self.build()
152        self.set_inferior_startup_launch()
153        # Gather threads from stop notification when QThreadsInStopReply is
154        # enabled.
155        thread_count = 5
156        self.test_sequence.add_log_lines(
157            self.ENABLE_THREADS_IN_STOP_REPLY_ENTRIES, True)
158        stop_reply_threads = self.gather_stop_reply_threads(thread_count)
159
160        # Gather threads from q{f,s}ThreadInfo.
161        self.reset_test_sequence()
162        self.add_threadinfo_collection_packets()
163
164        context = self.expect_gdbremote_sequence()
165        self.assertIsNotNone(context)
166
167        threads = self.parse_threadinfo_packets(context)
168        self.assertIsNotNone(threads)
169        self.assertGreaterEqual(len(threads), thread_count)
170
171        # Ensure each thread in q{f,s}ThreadInfo appears in stop reply threads
172        for tid in threads:
173            self.assertIn(tid, stop_reply_threads)
174
175    @skipIfNetBSD
176    def test_stop_reply_contains_thread_pcs(self):
177        self.build()
178        self.set_inferior_startup_launch()
179        thread_count = 5
180        self.test_sequence.add_log_lines(
181            self.ENABLE_THREADS_IN_STOP_REPLY_ENTRIES, True)
182        results = self.gather_stop_reply_pcs(thread_count)
183        stop_reply_pcs = results["thread_pcs"]
184        pc_register = results["pc_register"]
185        little_endian = results["little_endian"]
186        self.assertGreaterEqual(len(stop_reply_pcs), thread_count)
187
188        threads_info_pcs = self.gather_threads_info_pcs(pc_register,
189                little_endian)
190
191        self.assertEqual(len(threads_info_pcs), len(stop_reply_pcs))
192        for thread_id in stop_reply_pcs:
193            self.assertIn(thread_id, threads_info_pcs)
194            self.assertEqual(int(stop_reply_pcs[thread_id], 16),
195                    int(threads_info_pcs[thread_id], 16))
196