1"""
2Test that we obey thread conditioned breakpoints.
3"""
4
5
6
7import lldb
8from lldbsuite.test.decorators import *
9from lldbsuite.test.lldbtest import *
10from lldbsuite.test import lldbutil
11
12def using_current(test, thread, breakpoint):
13    bp_id = breakpoint.GetID()
14    test.runCmd("break modify -t current {0}".format(bp_id))
15
16def set_thread_id(test, thread, breakpoint):
17    id = thread.id
18    breakpoint.SetThreadID(id)
19
20def set_thread_name(test, thread, breakpoint):
21    breakpoint.SetThreadName("main-thread")
22
23class ThreadSpecificBreakTestCase(TestBase):
24    NO_DEBUG_INFO_TESTCASE = True
25
26    @add_test_categories(['pyapi'])
27
28    @expectedFailureAll(oslist=['ios', 'watchos', 'tvos', 'bridgeos'], archs=['armv7', 'armv7k'], bugnumber='rdar://problem/34563920') # armv7 ios problem - breakpoint with tid qualifier isn't working
29    @skipIf(oslist=['windows'], archs=['aarch64']) # Flaky on buildbot
30    def test_thread_id(self):
31        self.do_test(set_thread_id)
32
33    @skipUnlessDarwin
34    @expectedFailureAll(oslist=['ios', 'watchos', 'tvos', 'bridgeos'], archs=['armv7', 'armv7k'], bugnumber='rdar://problem/34563920') # armv7 ios problem - breakpoint with tid qualifier isn't working
35    def test_thread_name(self):
36        self.do_test(set_thread_name)
37
38    @expectedFailureAll(oslist=['ios', 'watchos', 'tvos', 'bridgeos'], archs=['armv7', 'armv7k'], bugnumber='rdar://problem/34563920') # armv7 ios problem - breakpoint with tid qualifier isn't working
39    @skipIf(oslist=['windows'], archs=['aarch64']) # Flaky on buildbot
40    def test_current_token(self):
41        self.do_test(using_current)
42
43    def do_test(self, setter_method):
44        """Test that we obey thread conditioned breakpoints."""
45        self.build()
46        main_source_spec = lldb.SBFileSpec("main.cpp")
47        (target, process, main_thread, main_breakpoint) = lldbutil.run_to_source_breakpoint(self,
48                "Set main breakpoint here", main_source_spec)
49
50        thread_breakpoint = target.BreakpointCreateBySourceRegex(
51            "Set thread-specific breakpoint here", main_source_spec)
52        self.assertGreater(
53            thread_breakpoint.GetNumLocations(),
54            0,
55            "thread breakpoint has no locations associated with it.")
56
57        # Set the thread-specific breakpoint to stop only on the main thread
58        # before the secondary thread has a chance to execute it.  The main
59        # thread joins the secondary thread, and then the main thread will
60        # execute the code at the breakpoint.  If the thread-specific
61        # breakpoint works, the next stop will be on the main thread.
62        setter_method(self, main_thread, thread_breakpoint)
63
64        process.Continue()
65        next_stop_state = process.GetState()
66        self.assertEqual(
67            next_stop_state,
68            lldb.eStateStopped,
69            "We should have stopped at the thread breakpoint.")
70        stopped_threads = lldbutil.get_threads_stopped_at_breakpoint(
71            process, thread_breakpoint)
72        self.assertEqual(
73            len(stopped_threads),
74            1,
75            "thread breakpoint stopped at unexpected number of threads")
76        self.assertEqual(
77            stopped_threads[0].GetThreadID(),
78            main_thread.GetThreadID(),
79            "thread breakpoint stopped at the wrong thread")
80