• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.DEFAULT_SLEEP)
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        self.assertIsNotNone(threads)
56
57        # On Windows, there could be more threads spawned. For example, DebugBreakProcess will
58        # create a new thread from the debugged process to handle an exception event. So here we
59        # assert 'GreaterEqual' condition.
60        triple = self.dbg.GetSelectedPlatform().GetTriple()
61        if re.match(".*-.*-windows", triple):
62            self.assertGreaterEqual(len(threads), thread_count)
63        else:
64            self.assertEqual(len(threads), thread_count)
65
66        # Grab stop reply for each thread via qThreadStopInfo{tid:hex}.
67        stop_replies = {}
68        thread_dicts = {}
69        for thread in threads:
70            # Run the qThreadStopInfo command.
71            self.reset_test_sequence()
72            self.test_sequence.add_log_lines(
73                [
74                    "read packet: $qThreadStopInfo{:x}#00".format(thread),
75                    {
76                        "direction": "send",
77                        "regex": r"^\$T([0-9a-fA-F]+)([^#]+)#[0-9a-fA-F]{2}$",
78                        "capture": {
79                            1: "stop_result",
80                            2: "key_vals_text"}},
81                ],
82                True)
83            context = self.expect_gdbremote_sequence()
84            self.assertIsNotNone(context)
85
86            # Parse stop reply contents.
87            key_vals_text = context.get("key_vals_text")
88            self.assertIsNotNone(key_vals_text)
89            kv_dict = self.parse_key_val_dict(key_vals_text)
90            self.assertIsNotNone(kv_dict)
91
92            # Verify there is a thread and that it matches the expected thread
93            # id.
94            kv_thread = kv_dict.get("thread")
95            self.assertIsNotNone(kv_thread)
96            kv_thread_id = int(kv_thread, 16)
97            self.assertEqual(kv_thread_id, thread)
98
99            # Grab the stop id reported.
100            stop_result_text = context.get("stop_result")
101            self.assertIsNotNone(stop_result_text)
102            stop_replies[kv_thread_id] = int(stop_result_text, 16)
103
104            # Hang on to the key-val dictionary for the thread.
105            thread_dicts[kv_thread_id] = kv_dict
106
107        return (stop_replies, thread_dicts)
108
109    def qThreadStopInfo_works_for_multiple_threads(self, thread_count):
110        (stop_replies, _) = self.gather_stop_replies_via_qThreadStopInfo(thread_count)
111        triple = self.dbg.GetSelectedPlatform().GetTriple()
112        # Consider one more thread created by calling DebugBreakProcess.
113        if re.match(".*-.*-windows", triple):
114            self.assertGreaterEqual(len(stop_replies), thread_count)
115        else:
116            self.assertEqual(len(stop_replies), thread_count)
117
118    @debugserver_test
119    def test_qThreadStopInfo_works_for_multiple_threads_debugserver(self):
120        self.init_debugserver_test()
121        self.build()
122        self.set_inferior_startup_launch()
123        self.qThreadStopInfo_works_for_multiple_threads(self.THREAD_COUNT)
124
125    @llgs_test
126    @skipIfNetBSD
127    def test_qThreadStopInfo_works_for_multiple_threads_llgs(self):
128        self.init_llgs_test()
129        self.build()
130        self.set_inferior_startup_launch()
131        self.qThreadStopInfo_works_for_multiple_threads(self.THREAD_COUNT)
132
133    def qThreadStopInfo_only_reports_one_thread_stop_reason_during_interrupt(
134            self, thread_count):
135        (stop_replies, _) = self.gather_stop_replies_via_qThreadStopInfo(thread_count)
136        self.assertIsNotNone(stop_replies)
137
138        no_stop_reason_count = sum(
139            1 for stop_reason in list(
140                stop_replies.values()) if stop_reason == 0)
141        with_stop_reason_count = sum(
142            1 for stop_reason in list(
143                stop_replies.values()) if stop_reason != 0)
144
145        # All but one thread should report no stop reason.
146        triple = self.dbg.GetSelectedPlatform().GetTriple()
147
148        # Consider one more thread created by calling DebugBreakProcess.
149        if re.match(".*-.*-windows", triple):
150            self.assertGreaterEqual(no_stop_reason_count, thread_count - 1)
151        else:
152            self.assertEqual(no_stop_reason_count, thread_count - 1)
153
154        # Only one thread should should indicate a stop reason.
155        self.assertEqual(with_stop_reason_count, 1)
156
157    @debugserver_test
158    def test_qThreadStopInfo_only_reports_one_thread_stop_reason_during_interrupt_debugserver(
159            self):
160        self.init_debugserver_test()
161        self.build()
162        self.set_inferior_startup_launch()
163        self.qThreadStopInfo_only_reports_one_thread_stop_reason_during_interrupt(
164            self.THREAD_COUNT)
165
166    @expectedFailureAll(oslist=["freebsd"], bugnumber="llvm.org/pr48418")
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