1import unittest2
2import gdbremote_testcase
3from lldbsuite.test.decorators import *
4from lldbsuite.test.lldbtest import *
5from lldbsuite.test import lldbutil
6
7class TestGdbRemote_qThreadStopInfo(gdbremote_testcase.GdbRemoteTestCaseBase):
8
9    mydir = TestBase.compute_mydir(__file__)
10    THREAD_COUNT = 5
11
12    def gather_stop_replies_via_qThreadStopInfo(self, thread_count):
13        # Set up the inferior args.
14        inferior_args = []
15        for i in range(thread_count - 1):
16            inferior_args.append("thread:new")
17        inferior_args.append("sleep:10")
18        procs = self.prep_debug_monitor_and_inferior(
19            inferior_args=inferior_args)
20
21        # Assumes test_sequence has anything added needed to setup the initial state.
22        # (Like optionally enabling QThreadsInStopReply.)
23        self.test_sequence.add_log_lines([
24            "read packet: $c#63"
25        ], True)
26        context = self.expect_gdbremote_sequence()
27        self.assertIsNotNone(context)
28
29        # Give threads time to start up, then break.
30        time.sleep(self.DEFAULT_SLEEP)
31        self.reset_test_sequence()
32        self.test_sequence.add_log_lines(
33            [
34                "read packet: {}".format(
35                    chr(3)),
36                {
37                    "direction": "send",
38                    "regex": r"^\$T([0-9a-fA-F]+)([^#]+)#[0-9a-fA-F]{2}$",
39                    "capture": {
40                        1: "stop_result",
41                        2: "key_vals_text"}},
42            ],
43            True)
44        context = self.expect_gdbremote_sequence()
45        self.assertIsNotNone(context)
46
47        # Wait until all threads have started.
48        threads = self.wait_for_thread_count(thread_count)
49        self.assertIsNotNone(threads)
50
51        # On Windows, there could be more threads spawned. For example, DebugBreakProcess will
52        # create a new thread from the debugged process to handle an exception event. So here we
53        # assert 'GreaterEqual' condition.
54        triple = self.dbg.GetSelectedPlatform().GetTriple()
55        if re.match(".*-.*-windows", triple):
56            self.assertGreaterEqual(len(threads), thread_count)
57        else:
58            self.assertEqual(len(threads), thread_count)
59
60        # Grab stop reply for each thread via qThreadStopInfo{tid:hex}.
61        stop_replies = {}
62        thread_dicts = {}
63        for thread in threads:
64            # Run the qThreadStopInfo command.
65            self.reset_test_sequence()
66            self.test_sequence.add_log_lines(
67                [
68                    "read packet: $qThreadStopInfo{:x}#00".format(thread),
69                    {
70                        "direction": "send",
71                        "regex": r"^\$T([0-9a-fA-F]+)([^#]+)#[0-9a-fA-F]{2}$",
72                        "capture": {
73                            1: "stop_result",
74                            2: "key_vals_text"}},
75                ],
76                True)
77            context = self.expect_gdbremote_sequence()
78            self.assertIsNotNone(context)
79
80            # Parse 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            # Verify there is a thread and that it matches the expected thread
87            # id.
88            kv_thread = kv_dict.get("thread")
89            self.assertIsNotNone(kv_thread)
90            kv_thread_id = int(kv_thread, 16)
91            self.assertEqual(kv_thread_id, thread)
92
93            # Grab the stop id reported.
94            stop_result_text = context.get("stop_result")
95            self.assertIsNotNone(stop_result_text)
96            stop_replies[kv_thread_id] = int(stop_result_text, 16)
97
98            # Hang on to the key-val dictionary for the thread.
99            thread_dicts[kv_thread_id] = kv_dict
100
101        return (stop_replies, thread_dicts)
102
103    @skipIfNetBSD
104    def test_qThreadStopInfo_works_for_multiple_threads(self):
105        self.build()
106        self.set_inferior_startup_launch()
107        (stop_replies, _) = self.gather_stop_replies_via_qThreadStopInfo(self.THREAD_COUNT)
108        triple = self.dbg.GetSelectedPlatform().GetTriple()
109        # Consider one more thread created by calling DebugBreakProcess.
110        if re.match(".*-.*-windows", triple):
111            self.assertGreaterEqual(len(stop_replies), self.THREAD_COUNT)
112        else:
113            self.assertEqual(len(stop_replies), self.THREAD_COUNT)
114
115    @expectedFailureAll(oslist=["freebsd"], bugnumber="llvm.org/pr48418")
116    @expectedFailureNetBSD
117    def test_qThreadStopInfo_only_reports_one_thread_stop_reason_during_interrupt(self):
118        self.build()
119        self.set_inferior_startup_launch()
120
121        (stop_replies, _) = self.gather_stop_replies_via_qThreadStopInfo(self.THREAD_COUNT)
122        self.assertIsNotNone(stop_replies)
123
124        no_stop_reason_count = sum(
125            1 for stop_reason in list(
126                stop_replies.values()) if stop_reason == 0)
127        with_stop_reason_count = sum(
128            1 for stop_reason in list(
129                stop_replies.values()) if stop_reason != 0)
130
131        # All but one thread should report no stop reason.
132        triple = self.dbg.GetSelectedPlatform().GetTriple()
133
134        # Consider one more thread created by calling DebugBreakProcess.
135        if re.match(".*-.*-windows", triple):
136            self.assertGreaterEqual(no_stop_reason_count, self.THREAD_COUNT - 1)
137        else:
138            self.assertEqual(no_stop_reason_count, self.THREAD_COUNT - 1)
139
140        # Only one thread should should indicate a stop reason.
141        self.assertEqual(with_stop_reason_count, 1)
142