199451b44SJordan Rupprecht"""
299451b44SJordan RupprechtTest that we obey thread conditioned breakpoints.
399451b44SJordan Rupprecht"""
499451b44SJordan Rupprecht
599451b44SJordan Rupprecht
699451b44SJordan Rupprechtimport lldb
799451b44SJordan Rupprechtfrom lldbsuite.test.decorators import *
899451b44SJordan Rupprechtfrom lldbsuite.test.lldbtest import *
999451b44SJordan Rupprechtfrom lldbsuite.test import lldbutil
1099451b44SJordan Rupprecht
11*2238dcc3SJonas Devlieghere
12f362b05dSJim Inghamdef using_current(test, thread, breakpoint):
13f362b05dSJim Ingham    bp_id = breakpoint.GetID()
14f362b05dSJim Ingham    test.runCmd("break modify -t current {0}".format(bp_id))
15f362b05dSJim Ingham
16*2238dcc3SJonas Devlieghere
17f362b05dSJim Inghamdef set_thread_id(test, thread, breakpoint):
1899451b44SJordan Rupprecht    id = thread.id
1999451b44SJordan Rupprecht    breakpoint.SetThreadID(id)
2099451b44SJordan Rupprecht
21*2238dcc3SJonas Devlieghere
22f362b05dSJim Inghamdef set_thread_name(test, thread, breakpoint):
2399451b44SJordan Rupprecht    breakpoint.SetThreadName("main-thread")
2499451b44SJordan Rupprecht
25*2238dcc3SJonas Devlieghere
2699451b44SJordan Rupprechtclass ThreadSpecificBreakTestCase(TestBase):
2799451b44SJordan Rupprecht    NO_DEBUG_INFO_TESTCASE = True
2899451b44SJordan Rupprecht
29*2238dcc3SJonas Devlieghere    @add_test_categories(["pyapi"])
30*2238dcc3SJonas Devlieghere    @expectedFailureAll(
31*2238dcc3SJonas Devlieghere        oslist=["ios", "watchos", "tvos", "bridgeos"],
32*2238dcc3SJonas Devlieghere        archs=["armv7", "armv7k"],
33*2238dcc3SJonas Devlieghere        bugnumber="rdar://problem/34563920",
34*2238dcc3SJonas Devlieghere    )  # armv7 ios problem - breakpoint with tid qualifier isn't working
35*2238dcc3SJonas Devlieghere    @skipIf(oslist=["windows"], archs=["aarch64"])  # Flaky on buildbot
3699451b44SJordan Rupprecht    def test_thread_id(self):
3799451b44SJordan Rupprecht        self.do_test(set_thread_id)
3899451b44SJordan Rupprecht
3999451b44SJordan Rupprecht    @skipUnlessDarwin
40*2238dcc3SJonas Devlieghere    @expectedFailureAll(
41*2238dcc3SJonas Devlieghere        oslist=["ios", "watchos", "tvos", "bridgeos"],
42*2238dcc3SJonas Devlieghere        archs=["armv7", "armv7k"],
43*2238dcc3SJonas Devlieghere        bugnumber="rdar://problem/34563920",
44*2238dcc3SJonas Devlieghere    )  # armv7 ios problem - breakpoint with tid qualifier isn't working
4599451b44SJordan Rupprecht    def test_thread_name(self):
4699451b44SJordan Rupprecht        self.do_test(set_thread_name)
4799451b44SJordan Rupprecht
48*2238dcc3SJonas Devlieghere    @expectedFailureAll(
49*2238dcc3SJonas Devlieghere        oslist=["ios", "watchos", "tvos", "bridgeos"],
50*2238dcc3SJonas Devlieghere        archs=["armv7", "armv7k"],
51*2238dcc3SJonas Devlieghere        bugnumber="rdar://problem/34563920",
52*2238dcc3SJonas Devlieghere    )  # armv7 ios problem - breakpoint with tid qualifier isn't working
53*2238dcc3SJonas Devlieghere    @skipIf(oslist=["windows"], archs=["aarch64"])  # Flaky on buildbot
54f362b05dSJim Ingham    def test_current_token(self):
55f362b05dSJim Ingham        self.do_test(using_current)
56f362b05dSJim Ingham
5799451b44SJordan Rupprecht    def do_test(self, setter_method):
5899451b44SJordan Rupprecht        """Test that we obey thread conditioned breakpoints."""
5999451b44SJordan Rupprecht        self.build()
6099451b44SJordan Rupprecht        main_source_spec = lldb.SBFileSpec("main.cpp")
61*2238dcc3SJonas Devlieghere        (
62*2238dcc3SJonas Devlieghere            target,
63*2238dcc3SJonas Devlieghere            process,
64*2238dcc3SJonas Devlieghere            main_thread,
65*2238dcc3SJonas Devlieghere            main_breakpoint,
66*2238dcc3SJonas Devlieghere        ) = lldbutil.run_to_source_breakpoint(
67*2238dcc3SJonas Devlieghere            self, "Set main breakpoint here", main_source_spec
68*2238dcc3SJonas Devlieghere        )
6999451b44SJordan Rupprecht
7099451b44SJordan Rupprecht        thread_breakpoint = target.BreakpointCreateBySourceRegex(
71*2238dcc3SJonas Devlieghere            "Set thread-specific breakpoint here", main_source_spec
72*2238dcc3SJonas Devlieghere        )
7399451b44SJordan Rupprecht        self.assertGreater(
7499451b44SJordan Rupprecht            thread_breakpoint.GetNumLocations(),
7599451b44SJordan Rupprecht            0,
76*2238dcc3SJonas Devlieghere            "thread breakpoint has no locations associated with it.",
77*2238dcc3SJonas Devlieghere        )
7899451b44SJordan Rupprecht
7999451b44SJordan Rupprecht        # Set the thread-specific breakpoint to stop only on the main thread
8099451b44SJordan Rupprecht        # before the secondary thread has a chance to execute it.  The main
8199451b44SJordan Rupprecht        # thread joins the secondary thread, and then the main thread will
8299451b44SJordan Rupprecht        # execute the code at the breakpoint.  If the thread-specific
8399451b44SJordan Rupprecht        # breakpoint works, the next stop will be on the main thread.
84f362b05dSJim Ingham        setter_method(self, main_thread, thread_breakpoint)
8599451b44SJordan Rupprecht
8699451b44SJordan Rupprecht        process.Continue()
8799451b44SJordan Rupprecht        next_stop_state = process.GetState()
8899451b44SJordan Rupprecht        self.assertEqual(
8999451b44SJordan Rupprecht            next_stop_state,
9099451b44SJordan Rupprecht            lldb.eStateStopped,
91*2238dcc3SJonas Devlieghere            "We should have stopped at the thread breakpoint.",
92*2238dcc3SJonas Devlieghere        )
9399451b44SJordan Rupprecht        stopped_threads = lldbutil.get_threads_stopped_at_breakpoint(
94*2238dcc3SJonas Devlieghere            process, thread_breakpoint
95*2238dcc3SJonas Devlieghere        )
9699451b44SJordan Rupprecht        self.assertEqual(
9799451b44SJordan Rupprecht            len(stopped_threads),
9899451b44SJordan Rupprecht            1,
99*2238dcc3SJonas Devlieghere            "thread breakpoint stopped at unexpected number of threads",
100*2238dcc3SJonas Devlieghere        )
10199451b44SJordan Rupprecht        self.assertEqual(
10299451b44SJordan Rupprecht            stopped_threads[0].GetThreadID(),
10399451b44SJordan Rupprecht            main_thread.GetThreadID(),
104*2238dcc3SJonas Devlieghere            "thread breakpoint stopped at the wrong thread",
105*2238dcc3SJonas Devlieghere        )
106