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    def test_thread_id(self):
30        self.do_test(set_thread_id)
31
32    @skipUnlessDarwin
33    @expectedFailureAll(oslist=['ios', 'watchos', 'tvos', 'bridgeos'], archs=['armv7', 'armv7k'], bugnumber='rdar://problem/34563920') # armv7 ios problem - breakpoint with tid qualifier isn't working
34    def test_thread_name(self):
35        self.do_test(set_thread_name)
36
37    @expectedFailureAll(oslist=['ios', 'watchos', 'tvos', 'bridgeos'], archs=['armv7', 'armv7k'], bugnumber='rdar://problem/34563920') # armv7 ios problem - breakpoint with tid qualifier isn't working
38    def test_current_token(self):
39        self.do_test(using_current)
40
41    def do_test(self, setter_method):
42        """Test that we obey thread conditioned breakpoints."""
43        self.build()
44        main_source_spec = lldb.SBFileSpec("main.cpp")
45        (target, process, main_thread, main_breakpoint) = lldbutil.run_to_source_breakpoint(self,
46                "Set main breakpoint here", main_source_spec)
47
48        thread_breakpoint = target.BreakpointCreateBySourceRegex(
49            "Set thread-specific breakpoint here", main_source_spec)
50        self.assertGreater(
51            thread_breakpoint.GetNumLocations(),
52            0,
53            "thread breakpoint has no locations associated with it.")
54
55        # Set the thread-specific breakpoint to stop only on the main thread
56        # before the secondary thread has a chance to execute it.  The main
57        # thread joins the secondary thread, and then the main thread will
58        # execute the code at the breakpoint.  If the thread-specific
59        # breakpoint works, the next stop will be on the main thread.
60        setter_method(self, main_thread, thread_breakpoint)
61
62        process.Continue()
63        next_stop_state = process.GetState()
64        self.assertEqual(
65            next_stop_state,
66            lldb.eStateStopped,
67            "We should have stopped at the thread breakpoint.")
68        stopped_threads = lldbutil.get_threads_stopped_at_breakpoint(
69            process, thread_breakpoint)
70        self.assertEqual(
71            len(stopped_threads),
72            1,
73            "thread breakpoint stopped at unexpected number of threads")
74        self.assertEqual(
75            stopped_threads[0].GetThreadID(),
76            main_thread.GetThreadID(),
77            "thread breakpoint stopped at the wrong thread")
78