xref: /llvm-project/lldb/test/API/functionalities/breakpoint/two_hits_one_actual/TestTwoHitsOneActual.py (revision 2238dcc39358353cac21df75c3c3286ab20b8f53)
1"""
2Test that if we hit a breakpoint on two threads at the
3same time, one of which passes the condition, one not,
4we only have a breakpoint stop reason for the one that
5passed the condition.
6"""
7
8import lldb
9import lldbsuite.test.lldbutil as lldbutil
10from lldbsuite.test.decorators import *
11from lldbsuite.test.lldbtest import *
12
13
14class TestTwoHitsOneActual(TestBase):
15    NO_DEBUG_INFO_TESTCASE = True
16
17    @skipIf(oslist=["linux"], archs=["arm", "aarch64"])
18    def test_two_hits_one_actual(self):
19        """There can be many tests in a test case - describe this test here."""
20        self.build()
21        self.main_source_file = lldb.SBFileSpec("main.cpp")
22        self.sample_test()
23
24    def sample_test(self):
25        """You might use the test implementation in several ways, say so here."""
26
27        (target, process, main_thread, _) = lldbutil.run_to_source_breakpoint(
28            self, "Set bkpt here to get started", self.main_source_file
29        )
30        # This is working around a separate bug.  If you hit a breakpoint and
31        # run an expression and it is the first expression you've ever run, on
32        # Darwin that will involve running the ObjC runtime parsing code, and we'll
33        # be in the middle of that when we do PerformAction on the other thread,
34        # which will cause the condition expression to fail.  Calling another
35        # expression first works around this.
36        val_obj = main_thread.frame[0].EvaluateExpression("main_usec==1")
37        self.assertSuccess(val_obj.GetError(), "Ran our expression successfully")
38        self.assertEqual(val_obj.value, "true", "Value was true.")
39        # Set two breakpoints just to test the multiple location logic:
40        bkpt1 = target.BreakpointCreateBySourceRegex(
41            "Break here in the helper", self.main_source_file
42        )
43        bkpt2 = target.BreakpointCreateBySourceRegex(
44            "Break here in the helper", self.main_source_file
45        )
46
47        # This one will never be hit:
48        bkpt1.SetCondition("usec == 100")
49        # This one will only be hit on the main thread:
50        bkpt2.SetCondition("usec == 1")
51
52        # This is hard to test definitively, becuase it requires hitting
53        # a breakpoint on multiple threads at the same time.  On Darwin, this
54        # will happen pretty much ever time we continue.  What we are really
55        # asserting is that we only ever stop on one thread, so we approximate that
56        # by continuing 20 times and assert we only ever hit the first thread.  Either
57        # this is a platform that only reports one hit at a time, in which case all
58        # this code is unused, or we actually didn't hit the other thread.
59
60        for idx in range(0, 20):
61            process.Continue()
62            for thread in process.threads:
63                if thread.id == main_thread.id:
64                    self.assertStopReason(
65                        thread.stop_reason, lldb.eStopReasonBreakpoint
66                    )
67                else:
68                    self.assertStopReason(thread.stop_reason, lldb.eStopReasonNone)
69