1
2import json
3import re
4
5import gdbremote_testcase
6from lldbsuite.test.decorators import *
7from lldbsuite.test.lldbtest import *
8from lldbsuite.test import lldbutil
9
10class TestGdbRemoteThreadsInStopReply(
11        gdbremote_testcase.GdbRemoteTestCaseBase):
12
13    mydir = TestBase.compute_mydir(__file__)
14
15    ENABLE_THREADS_IN_STOP_REPLY_ENTRIES = [
16        "read packet: $QListThreadsInStopReply#21",
17        "send packet: $OK#00",
18    ]
19
20    def gather_stop_reply_fields(self, post_startup_log_lines, thread_count,
21            field_names):
22        # Set up the inferior args.
23        inferior_args = []
24        for i in range(thread_count - 1):
25            inferior_args.append("thread:new")
26        inferior_args.append("sleep:10")
27        procs = self.prep_debug_monitor_and_inferior(
28            inferior_args=inferior_args)
29
30        self.add_register_info_collection_packets()
31        self.add_process_info_collection_packets()
32
33        # Assumes test_sequence has anything added needed to setup the initial state.
34        # (Like optionally enabling QThreadsInStopReply.)
35        if post_startup_log_lines:
36            self.test_sequence.add_log_lines(post_startup_log_lines, True)
37        self.test_sequence.add_log_lines([
38            "read packet: $c#63"
39        ], True)
40        context = self.expect_gdbremote_sequence()
41        self.assertIsNotNone(context)
42        hw_info = self.parse_hw_info(context)
43
44        # Give threads time to start up, then break.
45        time.sleep(self.DEFAULT_SLEEP)
46        self.reset_test_sequence()
47        self.test_sequence.add_log_lines(
48            [
49                "read packet: {}".format(
50                    chr(3)),
51                {
52                    "direction": "send",
53                    "regex": r"^\$T([0-9a-fA-F]+)([^#]+)#[0-9a-fA-F]{2}$",
54                    "capture": {
55                        1: "stop_result",
56                        2: "key_vals_text"}},
57            ],
58            True)
59        context = self.expect_gdbremote_sequence()
60        self.assertIsNotNone(context)
61
62        # Wait until all threads have started.
63        threads = self.wait_for_thread_count(thread_count)
64        self.assertIsNotNone(threads)
65        self.assertEqual(len(threads), thread_count)
66
67        # Run, then stop the process, grab the stop reply content.
68        self.reset_test_sequence()
69        self.test_sequence.add_log_lines(["read packet: $c#63",
70                                          "read packet: {}".format(chr(3)),
71                                          {"direction": "send",
72                                           "regex": r"^\$T([0-9a-fA-F]+)([^#]+)#[0-9a-fA-F]{2}$",
73                                           "capture": {1: "stop_result",
74                                                       2: "key_vals_text"}},
75                                          ],
76                                         True)
77        context = self.expect_gdbremote_sequence()
78        self.assertIsNotNone(context)
79
80        # Parse the stop reply contents.
81        key_vals_text = context.get("key_vals_text")
82        self.assertIsNotNone(key_vals_text)
83        kv_dict = self.parse_key_val_dict(key_vals_text)
84        self.assertIsNotNone(kv_dict)
85
86        result = dict();
87        result["pc_register"] = hw_info["pc_register"]
88        result["little_endian"] = hw_info["little_endian"]
89        for key_field in field_names:
90            result[key_field] = kv_dict.get(key_field)
91
92        return result
93
94    def gather_stop_reply_threads(self, post_startup_log_lines, thread_count):
95        # Pull out threads from stop response.
96        stop_reply_threads_text = self.gather_stop_reply_fields(
97                post_startup_log_lines, thread_count, ["threads"])["threads"]
98        if stop_reply_threads_text:
99            return [int(thread_id, 16)
100                    for thread_id in stop_reply_threads_text.split(",")]
101        else:
102            return []
103
104    def gather_stop_reply_pcs(self, post_startup_log_lines, thread_count):
105        results = self.gather_stop_reply_fields( post_startup_log_lines,
106                thread_count, ["threads", "thread-pcs"])
107        if not results:
108            return []
109
110        threads_text = results["threads"]
111        pcs_text = results["thread-pcs"]
112        thread_ids = threads_text.split(",")
113        pcs = pcs_text.split(",")
114        self.assertEquals(len(thread_ids), len(pcs))
115
116        thread_pcs = dict()
117        for i in range(0, len(pcs)):
118            thread_pcs[int(thread_ids[i], 16)] = pcs[i]
119
120        result = dict()
121        result["thread_pcs"] = thread_pcs
122        result["pc_register"] = results["pc_register"]
123        result["little_endian"] = results["little_endian"]
124        return result
125
126    def switch_endian(self, egg):
127        return "".join(reversed(re.findall("..", egg)))
128
129    def parse_hw_info(self, context):
130        self.assertIsNotNone(context)
131        process_info = self.parse_process_info_response(context)
132        endian = process_info.get("endian")
133        reg_info = self.parse_register_info_packets(context)
134        (pc_lldb_reg_index, pc_reg_info) = self.find_pc_reg_info(reg_info)
135
136        hw_info = dict()
137        hw_info["pc_register"] = pc_lldb_reg_index
138        hw_info["little_endian"] = (endian == "little")
139        return hw_info
140
141    def gather_threads_info_pcs(self, pc_register, little_endian):
142        self.reset_test_sequence()
143        self.test_sequence.add_log_lines(
144                [
145                    "read packet: $jThreadsInfo#c1",
146                    {
147                        "direction": "send",
148                        "regex": r"^\$(.*)#[0-9a-fA-F]{2}$",
149                        "capture": {
150                            1: "threads_info"}},
151                ],
152                True)
153
154        context = self.expect_gdbremote_sequence()
155        self.assertIsNotNone(context)
156        threads_info = context.get("threads_info")
157        register = str(pc_register)
158        # The jThreadsInfo response is not valid JSON data, so we have to
159        # clean it up first.
160        jthreads_info = json.loads(re.sub(r"}]", "}", threads_info))
161        thread_pcs = dict()
162        for thread_info in jthreads_info:
163            tid = thread_info["tid"]
164            pc = thread_info["registers"][register]
165            thread_pcs[tid] = self.switch_endian(pc) if little_endian else pc
166
167        return thread_pcs
168
169    def QListThreadsInStopReply_supported(self):
170        procs = self.prep_debug_monitor_and_inferior()
171        self.test_sequence.add_log_lines(
172            self.ENABLE_THREADS_IN_STOP_REPLY_ENTRIES, True)
173
174        context = self.expect_gdbremote_sequence()
175        self.assertIsNotNone(context)
176
177    @skipIfDarwinEmbedded # <rdar://problem/34539270> lldb-server tests not updated to work on ios etc yet
178    @debugserver_test
179    def test_QListThreadsInStopReply_supported_debugserver(self):
180        self.build()
181        self.set_inferior_startup_launch()
182        self.QListThreadsInStopReply_supported()
183
184    @llgs_test
185    def test_QListThreadsInStopReply_supported_llgs(self):
186        self.build()
187        self.set_inferior_startup_launch()
188        self.QListThreadsInStopReply_supported()
189
190    def stop_reply_reports_multiple_threads(self, thread_count):
191        # Gather threads from stop notification when QThreadsInStopReply is
192        # enabled.
193        stop_reply_threads = self.gather_stop_reply_threads(
194            self.ENABLE_THREADS_IN_STOP_REPLY_ENTRIES, thread_count)
195        self.assertEqual(len(stop_reply_threads), thread_count)
196
197    @skipIfDarwinEmbedded # <rdar://problem/34539270> lldb-server tests not updated to work on ios etc yet
198    @debugserver_test
199    def test_stop_reply_reports_multiple_threads_debugserver(self):
200        self.build()
201        self.set_inferior_startup_launch()
202        self.stop_reply_reports_multiple_threads(5)
203
204    # In current implementation of llgs on Windows, as a response to '\x03' packet, the debugger
205    # of the native process will trigger a call to DebugBreakProcess that will create a new thread
206    # to handle the exception debug event. So one more stop thread will be notified to the
207    # delegate, e.g. llgs.  So tests below to assert the stop threads number will all fail.
208    @expectedFailureAll(oslist=["windows"])
209    @skipIfNetBSD
210    @llgs_test
211    def test_stop_reply_reports_multiple_threads_llgs(self):
212        self.build()
213        self.set_inferior_startup_launch()
214        self.stop_reply_reports_multiple_threads(5)
215
216    def no_QListThreadsInStopReply_supplies_no_threads(self, thread_count):
217        # Gather threads from stop notification when QThreadsInStopReply is not
218        # enabled.
219        stop_reply_threads = self.gather_stop_reply_threads(None, thread_count)
220        self.assertEqual(len(stop_reply_threads), 0)
221
222    @skipIfDarwinEmbedded # <rdar://problem/34539270> lldb-server tests not updated to work on ios etc yet
223    @debugserver_test
224    def test_no_QListThreadsInStopReply_supplies_no_threads_debugserver(self):
225        self.build()
226        self.set_inferior_startup_launch()
227        self.no_QListThreadsInStopReply_supplies_no_threads(5)
228
229    @expectedFailureAll(oslist=["windows"])
230    @skipIfNetBSD
231    @llgs_test
232    def test_no_QListThreadsInStopReply_supplies_no_threads_llgs(self):
233        self.build()
234        self.set_inferior_startup_launch()
235        self.no_QListThreadsInStopReply_supplies_no_threads(5)
236
237    def stop_reply_reports_correct_threads(self, thread_count):
238        # Gather threads from stop notification when QThreadsInStopReply is
239        # enabled.
240        stop_reply_threads = self.gather_stop_reply_threads(
241            self.ENABLE_THREADS_IN_STOP_REPLY_ENTRIES, thread_count)
242        self.assertEqual(len(stop_reply_threads), thread_count)
243
244        # Gather threads from q{f,s}ThreadInfo.
245        self.reset_test_sequence()
246        self.add_threadinfo_collection_packets()
247
248        context = self.expect_gdbremote_sequence()
249        self.assertIsNotNone(context)
250
251        threads = self.parse_threadinfo_packets(context)
252        self.assertIsNotNone(threads)
253        self.assertEqual(len(threads), thread_count)
254
255        # Ensure each thread in q{f,s}ThreadInfo appears in stop reply threads
256        for tid in threads:
257            self.assertTrue(tid in stop_reply_threads)
258
259    @skipIfDarwinEmbedded # <rdar://problem/34539270> lldb-server tests not updated to work on ios etc yet
260    @debugserver_test
261    def test_stop_reply_reports_correct_threads_debugserver(self):
262        self.build()
263        self.set_inferior_startup_launch()
264        self.stop_reply_reports_correct_threads(5)
265
266    @expectedFailureAll(oslist=["windows"])
267    @skipIfNetBSD
268    @llgs_test
269    def test_stop_reply_reports_correct_threads_llgs(self):
270        self.build()
271        self.set_inferior_startup_launch()
272        self.stop_reply_reports_correct_threads(5)
273
274    def stop_reply_contains_thread_pcs(self, thread_count):
275        results = self.gather_stop_reply_pcs(
276                self.ENABLE_THREADS_IN_STOP_REPLY_ENTRIES, thread_count)
277        stop_reply_pcs = results["thread_pcs"]
278        pc_register = results["pc_register"]
279        little_endian = results["little_endian"]
280        self.assertEqual(len(stop_reply_pcs), thread_count)
281
282        threads_info_pcs = self.gather_threads_info_pcs(pc_register,
283                little_endian)
284
285        self.assertEqual(len(threads_info_pcs), thread_count)
286        for thread_id in stop_reply_pcs:
287            self.assertTrue(thread_id in threads_info_pcs)
288            self.assertTrue(int(stop_reply_pcs[thread_id], 16)
289                    == int(threads_info_pcs[thread_id], 16))
290
291    @expectedFailureAll(oslist=["windows"])
292    @skipIfNetBSD
293    @llgs_test
294    def test_stop_reply_contains_thread_pcs_llgs(self):
295        self.build()
296        self.set_inferior_startup_launch()
297        self.stop_reply_contains_thread_pcs(5)
298
299    @skipIfDarwinEmbedded # <rdar://problem/34539270> lldb-server tests not updated to work on ios etc yet
300    @debugserver_test
301    def test_stop_reply_contains_thread_pcs_debugserver(self):
302        self.build()
303        self.set_inferior_startup_launch()
304        self.stop_reply_contains_thread_pcs(5)
305