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.init_debugserver_test()
181        self.build()
182        self.set_inferior_startup_launch()
183        self.QListThreadsInStopReply_supported()
184
185    @llgs_test
186    def test_QListThreadsInStopReply_supported_llgs(self):
187        self.init_llgs_test()
188        self.build()
189        self.set_inferior_startup_launch()
190        self.QListThreadsInStopReply_supported()
191
192    def stop_reply_reports_multiple_threads(self, thread_count):
193        # Gather threads from stop notification when QThreadsInStopReply is
194        # enabled.
195        stop_reply_threads = self.gather_stop_reply_threads(
196            self.ENABLE_THREADS_IN_STOP_REPLY_ENTRIES, thread_count)
197        self.assertEqual(len(stop_reply_threads), thread_count)
198
199    @skipIfDarwinEmbedded # <rdar://problem/34539270> lldb-server tests not updated to work on ios etc yet
200    @debugserver_test
201    def test_stop_reply_reports_multiple_threads_debugserver(self):
202        self.init_debugserver_test()
203        self.build()
204        self.set_inferior_startup_launch()
205        self.stop_reply_reports_multiple_threads(5)
206
207    # In current implementation of llgs on Windows, as a response to '\x03' packet, the debugger
208    # of the native process will trigger a call to DebugBreakProcess that will create a new thread
209    # to handle the exception debug event. So one more stop thread will be notified to the
210    # delegate, e.g. llgs.  So tests below to assert the stop threads number will all fail.
211    @expectedFailureAll(oslist=["windows"])
212    @skipIfNetBSD
213    @llgs_test
214    def test_stop_reply_reports_multiple_threads_llgs(self):
215        self.init_llgs_test()
216        self.build()
217        self.set_inferior_startup_launch()
218        self.stop_reply_reports_multiple_threads(5)
219
220    def no_QListThreadsInStopReply_supplies_no_threads(self, thread_count):
221        # Gather threads from stop notification when QThreadsInStopReply is not
222        # enabled.
223        stop_reply_threads = self.gather_stop_reply_threads(None, thread_count)
224        self.assertEqual(len(stop_reply_threads), 0)
225
226    @skipIfDarwinEmbedded # <rdar://problem/34539270> lldb-server tests not updated to work on ios etc yet
227    @debugserver_test
228    def test_no_QListThreadsInStopReply_supplies_no_threads_debugserver(self):
229        self.init_debugserver_test()
230        self.build()
231        self.set_inferior_startup_launch()
232        self.no_QListThreadsInStopReply_supplies_no_threads(5)
233
234    @expectedFailureAll(oslist=["windows"])
235    @skipIfNetBSD
236    @llgs_test
237    def test_no_QListThreadsInStopReply_supplies_no_threads_llgs(self):
238        self.init_llgs_test()
239        self.build()
240        self.set_inferior_startup_launch()
241        self.no_QListThreadsInStopReply_supplies_no_threads(5)
242
243    def stop_reply_reports_correct_threads(self, thread_count):
244        # Gather threads from stop notification when QThreadsInStopReply is
245        # enabled.
246        stop_reply_threads = self.gather_stop_reply_threads(
247            self.ENABLE_THREADS_IN_STOP_REPLY_ENTRIES, thread_count)
248        self.assertEqual(len(stop_reply_threads), thread_count)
249
250        # Gather threads from q{f,s}ThreadInfo.
251        self.reset_test_sequence()
252        self.add_threadinfo_collection_packets()
253
254        context = self.expect_gdbremote_sequence()
255        self.assertIsNotNone(context)
256
257        threads = self.parse_threadinfo_packets(context)
258        self.assertIsNotNone(threads)
259        self.assertEqual(len(threads), thread_count)
260
261        # Ensure each thread in q{f,s}ThreadInfo appears in stop reply threads
262        for tid in threads:
263            self.assertTrue(tid in stop_reply_threads)
264
265    @skipIfDarwinEmbedded # <rdar://problem/34539270> lldb-server tests not updated to work on ios etc yet
266    @debugserver_test
267    def test_stop_reply_reports_correct_threads_debugserver(self):
268        self.init_debugserver_test()
269        self.build()
270        self.set_inferior_startup_launch()
271        self.stop_reply_reports_correct_threads(5)
272
273    @expectedFailureAll(oslist=["windows"])
274    @skipIfNetBSD
275    @llgs_test
276    def test_stop_reply_reports_correct_threads_llgs(self):
277        self.init_llgs_test()
278        self.build()
279        self.set_inferior_startup_launch()
280        self.stop_reply_reports_correct_threads(5)
281
282    def stop_reply_contains_thread_pcs(self, thread_count):
283        results = self.gather_stop_reply_pcs(
284                self.ENABLE_THREADS_IN_STOP_REPLY_ENTRIES, thread_count)
285        stop_reply_pcs = results["thread_pcs"]
286        pc_register = results["pc_register"]
287        little_endian = results["little_endian"]
288        self.assertEqual(len(stop_reply_pcs), thread_count)
289
290        threads_info_pcs = self.gather_threads_info_pcs(pc_register,
291                little_endian)
292
293        self.assertEqual(len(threads_info_pcs), thread_count)
294        for thread_id in stop_reply_pcs:
295            self.assertTrue(thread_id in threads_info_pcs)
296            self.assertTrue(int(stop_reply_pcs[thread_id], 16)
297                    == int(threads_info_pcs[thread_id], 16))
298
299    @expectedFailureAll(oslist=["windows"])
300    @skipIfNetBSD
301    @llgs_test
302    def test_stop_reply_contains_thread_pcs_llgs(self):
303        self.init_llgs_test()
304        self.build()
305        self.set_inferior_startup_launch()
306        self.stop_reply_contains_thread_pcs(5)
307
308    @skipIfDarwinEmbedded # <rdar://problem/34539270> lldb-server tests not updated to work on ios etc yet
309    @debugserver_test
310    def test_stop_reply_contains_thread_pcs_debugserver(self):
311        self.init_debugserver_test()
312        self.build()
313        self.set_inferior_startup_launch()
314        self.stop_reply_contains_thread_pcs(5)
315