xref: /llvm-project/lldb/test/API/python_api/watchpoint/TestWatchpointIgnoreCount.py (revision 2238dcc39358353cac21df75c3c3286ab20b8f53)
199451b44SJordan Rupprecht"""
299451b44SJordan RupprechtUse lldb Python SBWatchpoint API to set the ignore count.
399451b44SJordan Rupprecht"""
499451b44SJordan Rupprecht
599451b44SJordan Rupprechtimport lldb
699451b44SJordan Rupprechtfrom lldbsuite.test.decorators import *
799451b44SJordan Rupprechtfrom lldbsuite.test.lldbtest import *
899451b44SJordan Rupprechtfrom lldbsuite.test import lldbutil
999451b44SJordan Rupprecht
1099451b44SJordan Rupprecht
1199451b44SJordan Rupprechtclass WatchpointIgnoreCountTestCase(TestBase):
1299451b44SJordan Rupprecht    NO_DEBUG_INFO_TESTCASE = True
1399451b44SJordan Rupprecht
1499451b44SJordan Rupprecht    def setUp(self):
1599451b44SJordan Rupprecht        # Call super's setUp().
1699451b44SJordan Rupprecht        TestBase.setUp(self)
1799451b44SJordan Rupprecht        # Our simple source filename.
18*2238dcc3SJonas Devlieghere        self.source = "main.c"
1999451b44SJordan Rupprecht        # Find the line number to break inside main().
20*2238dcc3SJonas Devlieghere        self.line = line_number(self.source, "// Set break point at this line.")
2199451b44SJordan Rupprecht
22bef4da4aSJason Molenda    # on arm64 targets, lldb has incorrect hit-count / ignore-counts
23bef4da4aSJason Molenda    # for watchpoints when they are hit with multiple threads at
24bef4da4aSJason Molenda    # the same time.  Tracked as llvm.org/pr49433
25bef4da4aSJason Molenda    # or rdar://93863107 inside Apple.
26bef4da4aSJason Molenda    def affected_by_radar_93863107(self):
27*2238dcc3SJonas Devlieghere        return (
28*2238dcc3SJonas Devlieghere            self.getArchitecture() in ["arm64", "arm64e"]
29*2238dcc3SJonas Devlieghere        ) and self.platformIsDarwin()
30bef4da4aSJason Molenda
3199451b44SJordan Rupprecht    # Read-write watchpoints not supported on SystemZ
32*2238dcc3SJonas Devlieghere    @expectedFailureAll(archs=["s390x"])
3399451b44SJordan Rupprecht    def test_set_watch_ignore_count(self):
3499451b44SJordan Rupprecht        """Test SBWatchpoint.SetIgnoreCount() API."""
3599451b44SJordan Rupprecht        self.build()
3699451b44SJordan Rupprecht        exe = self.getBuildArtifact("a.out")
3799451b44SJordan Rupprecht
3899451b44SJordan Rupprecht        # Create a target by the debugger.
3999451b44SJordan Rupprecht        target = self.dbg.CreateTarget(exe)
4099451b44SJordan Rupprecht        self.assertTrue(target, VALID_TARGET)
4199451b44SJordan Rupprecht
4299451b44SJordan Rupprecht        # Create a breakpoint on main.c in order to set our watchpoint later.
4399451b44SJordan Rupprecht        breakpoint = target.BreakpointCreateByLocation(self.source, self.line)
44*2238dcc3SJonas Devlieghere        self.assertTrue(
45*2238dcc3SJonas Devlieghere            breakpoint and breakpoint.GetNumLocations() == 1, VALID_BREAKPOINT
46*2238dcc3SJonas Devlieghere        )
4799451b44SJordan Rupprecht
4899451b44SJordan Rupprecht        # Now launch the process, and do not stop at the entry point.
49*2238dcc3SJonas Devlieghere        process = target.LaunchSimple(None, None, self.get_process_working_directory())
5099451b44SJordan Rupprecht
5199451b44SJordan Rupprecht        # We should be stopped due to the breakpoint.  Get frame #0.
5299451b44SJordan Rupprecht        process = target.GetProcess()
53*2238dcc3SJonas Devlieghere        self.assertState(process.GetState(), lldb.eStateStopped, PROCESS_STOPPED)
54*2238dcc3SJonas Devlieghere        thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint)
5599451b44SJordan Rupprecht        frame0 = thread.GetFrameAtIndex(0)
5699451b44SJordan Rupprecht
5799451b44SJordan Rupprecht        # Watch 'global' for read and write.
58*2238dcc3SJonas Devlieghere        value = frame0.FindValue("global", lldb.eValueTypeVariableGlobal)
5999451b44SJordan Rupprecht        error = lldb.SBError()
6099451b44SJordan Rupprecht        watchpoint = value.Watch(True, True, True, error)
61*2238dcc3SJonas Devlieghere        self.assertTrue(
62*2238dcc3SJonas Devlieghere            value and watchpoint, "Successfully found the variable and set a watchpoint"
63*2238dcc3SJonas Devlieghere        )
6499451b44SJordan Rupprecht        self.DebugSBValue(value)
6599451b44SJordan Rupprecht
6699451b44SJordan Rupprecht        # Hide stdout if not running with '-t' option.
6799451b44SJordan Rupprecht        if not self.TraceOn():
6899451b44SJordan Rupprecht            self.HideStdout()
6999451b44SJordan Rupprecht
7099451b44SJordan Rupprecht        # There should be only 1 watchpoint location under the target.
7199451b44SJordan Rupprecht        self.assertEqual(target.GetNumWatchpoints(), 1)
7299451b44SJordan Rupprecht        watchpoint = target.GetWatchpointAtIndex(0)
7399451b44SJordan Rupprecht        self.assertTrue(watchpoint.IsEnabled())
7499451b44SJordan Rupprecht        self.assertEqual(watchpoint.GetIgnoreCount(), 0)
7599451b44SJordan Rupprecht        watch_id = watchpoint.GetID()
7699451b44SJordan Rupprecht        self.assertNotEqual(watch_id, 0)
7799451b44SJordan Rupprecht        print(watchpoint)
7899451b44SJordan Rupprecht
7999451b44SJordan Rupprecht        # Now immediately set the ignore count to 2.  When we continue, expect the
8099451b44SJordan Rupprecht        # inferior to run to its completion without stopping due to watchpoint.
8199451b44SJordan Rupprecht        watchpoint.SetIgnoreCount(2)
8299451b44SJordan Rupprecht        print(watchpoint)
8399451b44SJordan Rupprecht        process.Continue()
8499451b44SJordan Rupprecht
8599451b44SJordan Rupprecht        # At this point, the inferior process should have exited.
861b8c7352SJonas Devlieghere        self.assertState(process.GetState(), lldb.eStateExited, PROCESS_EXITED)
8799451b44SJordan Rupprecht
8899451b44SJordan Rupprecht        # Verify some vital statistics.
8999451b44SJordan Rupprecht        self.assertTrue(watchpoint)
9099451b44SJordan Rupprecht        self.assertEqual(watchpoint.GetWatchSize(), 4)
91bef4da4aSJason Molenda        if not self.affected_by_radar_93863107():
9299451b44SJordan Rupprecht            self.assertEqual(watchpoint.GetHitCount(), 2)
9399451b44SJordan Rupprecht        print(watchpoint)
94