1
2
3
4import unittest2
5import gdbremote_testcase
6from lldbsuite.test.decorators import *
7from lldbsuite.test.lldbtest import *
8from lldbsuite.test import lldbutil
9
10
11class TestGdbRemote_qThreadStopInfo(gdbremote_testcase.GdbRemoteTestCaseBase):
12
13    mydir = TestBase.compute_mydir(__file__)
14    THREAD_COUNT = 5
15
16    @skipIfDarwinEmbedded # <rdar://problem/34539270> lldb-server tests not updated to work on ios etc yet
17    @skipIfDarwinEmbedded # <rdar://problem/27005337>
18    def gather_stop_replies_via_qThreadStopInfo(self, thread_count):
19        # Set up the inferior args.
20        inferior_args = []
21        for i in range(thread_count - 1):
22            inferior_args.append("thread:new")
23        inferior_args.append("sleep:10")
24        procs = self.prep_debug_monitor_and_inferior(
25            inferior_args=inferior_args)
26
27        # Assumes test_sequence has anything added needed to setup the initial state.
28        # (Like optionally enabling QThreadsInStopReply.)
29        self.test_sequence.add_log_lines([
30            "read packet: $c#63"
31        ], True)
32        context = self.expect_gdbremote_sequence()
33        self.assertIsNotNone(context)
34
35        # Give threads time to start up, then break.
36        time.sleep(self._WAIT_TIMEOUT)
37        self.reset_test_sequence()
38        self.test_sequence.add_log_lines(
39            [
40                "read packet: {}".format(
41                    chr(3)),
42                {
43                    "direction": "send",
44                    "regex": r"^\$T([0-9a-fA-F]+)([^#]+)#[0-9a-fA-F]{2}$",
45                    "capture": {
46                        1: "stop_result",
47                        2: "key_vals_text"}},
48            ],
49            True)
50        context = self.expect_gdbremote_sequence()
51        self.assertIsNotNone(context)
52
53        # Wait until all threads have started.
54        threads = self.wait_for_thread_count(thread_count,
55                                             timeout_seconds=self._WAIT_TIMEOUT)
56        self.assertIsNotNone(threads)
57
58        # On Windows, there could be more threads spawned. For example, DebugBreakProcess will
59        # create a new thread from the debugged process to handle an exception event. So here we
60        # assert 'GreaterEqual' condition.
61        triple = self.dbg.GetSelectedPlatform().GetTriple()
62        if re.match(".*-.*-windows", triple):
63            self.assertGreaterEqual(len(threads), thread_count)
64        else:
65            self.assertEqual(len(threads), thread_count)
66
67        # Grab stop reply for each thread via qThreadStopInfo{tid:hex}.
68        stop_replies = {}
69        thread_dicts = {}
70        for thread in threads:
71            # Run the qThreadStopInfo command.
72            self.reset_test_sequence()
73            self.test_sequence.add_log_lines(
74                [
75                    "read packet: $qThreadStopInfo{:x}#00".format(thread),
76                    {
77                        "direction": "send",
78                        "regex": r"^\$T([0-9a-fA-F]+)([^#]+)#[0-9a-fA-F]{2}$",
79                        "capture": {
80                            1: "stop_result",
81                            2: "key_vals_text"}},
82                ],
83                True)
84            context = self.expect_gdbremote_sequence()
85            self.assertIsNotNone(context)
86
87            # Parse stop reply contents.
88            key_vals_text = context.get("key_vals_text")
89            self.assertIsNotNone(key_vals_text)
90            kv_dict = self.parse_key_val_dict(key_vals_text)
91            self.assertIsNotNone(kv_dict)
92
93            # Verify there is a thread and that it matches the expected thread
94            # id.
95            kv_thread = kv_dict.get("thread")
96            self.assertIsNotNone(kv_thread)
97            kv_thread_id = int(kv_thread, 16)
98            self.assertEqual(kv_thread_id, thread)
99
100            # Grab the stop id reported.
101            stop_result_text = context.get("stop_result")
102            self.assertIsNotNone(stop_result_text)
103            stop_replies[kv_thread_id] = int(stop_result_text, 16)
104
105            # Hang on to the key-val dictionary for the thread.
106            thread_dicts[kv_thread_id] = kv_dict
107
108        return (stop_replies, thread_dicts)
109
110    def qThreadStopInfo_works_for_multiple_threads(self, thread_count):
111        (stop_replies, _) = self.gather_stop_replies_via_qThreadStopInfo(thread_count)
112        triple = self.dbg.GetSelectedPlatform().GetTriple()
113        # Consider one more thread created by calling DebugBreakProcess.
114        if re.match(".*-.*-windows", triple):
115            self.assertGreaterEqual(len(stop_replies), thread_count)
116        else:
117            self.assertEqual(len(stop_replies), thread_count)
118
119    @debugserver_test
120    def test_qThreadStopInfo_works_for_multiple_threads_debugserver(self):
121        self.init_debugserver_test()
122        self.build()
123        self.set_inferior_startup_launch()
124        self.qThreadStopInfo_works_for_multiple_threads(self.THREAD_COUNT)
125
126    @llgs_test
127    @skipIfNetBSD
128    def test_qThreadStopInfo_works_for_multiple_threads_llgs(self):
129        self.init_llgs_test()
130        self.build()
131        self.set_inferior_startup_launch()
132        self.qThreadStopInfo_works_for_multiple_threads(self.THREAD_COUNT)
133
134    def qThreadStopInfo_only_reports_one_thread_stop_reason_during_interrupt(
135            self, thread_count):
136        (stop_replies, _) = self.gather_stop_replies_via_qThreadStopInfo(thread_count)
137        self.assertIsNotNone(stop_replies)
138
139        no_stop_reason_count = sum(
140            1 for stop_reason in list(
141                stop_replies.values()) if stop_reason == 0)
142        with_stop_reason_count = sum(
143            1 for stop_reason in list(
144                stop_replies.values()) if stop_reason != 0)
145
146        # All but one thread should report no stop reason.
147        triple = self.dbg.GetSelectedPlatform().GetTriple()
148
149        # Consider one more thread created by calling DebugBreakProcess.
150        if re.match(".*-.*-windows", triple):
151            self.assertGreaterEqual(no_stop_reason_count, thread_count - 1)
152        else:
153            self.assertEqual(no_stop_reason_count, thread_count - 1)
154
155        # Only one thread should should indicate a stop reason.
156        self.assertEqual(with_stop_reason_count, 1)
157
158    @debugserver_test
159    def test_qThreadStopInfo_only_reports_one_thread_stop_reason_during_interrupt_debugserver(
160            self):
161        self.init_debugserver_test()
162        self.build()
163        self.set_inferior_startup_launch()
164        self.qThreadStopInfo_only_reports_one_thread_stop_reason_during_interrupt(
165            self.THREAD_COUNT)
166
167    @expectedFailureNetBSD
168    @llgs_test
169    def test_qThreadStopInfo_only_reports_one_thread_stop_reason_during_interrupt_llgs(
170            self):
171        self.init_llgs_test()
172        self.build()
173        self.set_inferior_startup_launch()
174        self.qThreadStopInfo_only_reports_one_thread_stop_reason_during_interrupt(
175            self.THREAD_COUNT)
176
177    def qThreadStopInfo_has_valid_thread_names(
178            self, thread_count, expected_thread_name):
179        (_, thread_dicts) = self.gather_stop_replies_via_qThreadStopInfo(thread_count)
180        self.assertIsNotNone(thread_dicts)
181
182        for thread_dict in list(thread_dicts.values()):
183            name = thread_dict.get("name")
184            self.assertIsNotNone(name)
185            self.assertEqual(name, expected_thread_name)
186
187    @unittest2.skip("MacOSX doesn't have a default thread name")
188    @debugserver_test
189    def test_qThreadStopInfo_has_valid_thread_names_debugserver(self):
190        self.init_debugserver_test()
191        self.build()
192        self.set_inferior_startup_launch()
193        self.qThreadStopInfo_has_valid_thread_names(self.THREAD_COUNT, "a.out")
194
195    # test requires OS with set, equal thread names by default.
196    # Windows thread does not have name property, equal names as the process's by default.
197    @skipUnlessPlatform(["linux", "windows"])
198    @llgs_test
199    def test_qThreadStopInfo_has_valid_thread_names_llgs(self):
200        self.init_llgs_test()
201        self.build()
202        self.set_inferior_startup_launch()
203        self.qThreadStopInfo_has_valid_thread_names(self.THREAD_COUNT, "a.out")
204