xref: /llvm-project/lldb/test/API/python_api/watchpoint/condition/TestWatchpointConditionAPI.py (revision 2238dcc39358353cac21df75c3c3286ab20b8f53)
199451b44SJordan Rupprecht"""
299451b44SJordan RupprechtTest watchpoint condition API.
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 WatchpointConditionAPITestCase(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.cpp"
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        # And the watchpoint variable declaration line number.
22*2238dcc3SJonas Devlieghere        self.decl = line_number(self.source, "// Watchpoint variable declaration.")
2399451b44SJordan Rupprecht        # Build dictionary to have unique executable names for each test
2499451b44SJordan Rupprecht        # method.
2599451b44SJordan Rupprecht        self.exe_name = self.testMethodName
26*2238dcc3SJonas Devlieghere        self.d = {"CXX_SOURCES": self.source, "EXE": self.exe_name}
2799451b44SJordan Rupprecht
2899451b44SJordan Rupprecht    def test_watchpoint_cond_api(self):
2999451b44SJordan Rupprecht        """Test watchpoint condition API."""
3099451b44SJordan Rupprecht        self.build(dictionary=self.d)
3199451b44SJordan Rupprecht        self.setTearDownCleanup(dictionary=self.d)
3299451b44SJordan Rupprecht        exe = self.getBuildArtifact(self.exe_name)
3399451b44SJordan Rupprecht
3499451b44SJordan Rupprecht        # Create a target by the debugger.
3599451b44SJordan Rupprecht        target = self.dbg.CreateTarget(exe)
3699451b44SJordan Rupprecht        self.assertTrue(target, VALID_TARGET)
3799451b44SJordan Rupprecht
3899451b44SJordan Rupprecht        # Now create a breakpoint on main.c.
3999451b44SJordan Rupprecht        breakpoint = target.BreakpointCreateByLocation(self.source, self.line)
40*2238dcc3SJonas Devlieghere        self.assertTrue(
41*2238dcc3SJonas Devlieghere            breakpoint and breakpoint.GetNumLocations() == 1, VALID_BREAKPOINT
42*2238dcc3SJonas Devlieghere        )
4399451b44SJordan Rupprecht
4499451b44SJordan Rupprecht        # Now launch the process, and do not stop at the entry point.
45*2238dcc3SJonas Devlieghere        process = target.LaunchSimple(None, None, self.get_process_working_directory())
4699451b44SJordan Rupprecht
4799451b44SJordan Rupprecht        # We should be stopped due to the breakpoint.  Get frame #0.
4899451b44SJordan Rupprecht        process = target.GetProcess()
49*2238dcc3SJonas Devlieghere        self.assertState(process.GetState(), lldb.eStateStopped, PROCESS_STOPPED)
50*2238dcc3SJonas Devlieghere        thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint)
5199451b44SJordan Rupprecht        frame0 = thread.GetFrameAtIndex(0)
5299451b44SJordan Rupprecht
5399451b44SJordan Rupprecht        # Watch 'global' for write.
54*2238dcc3SJonas Devlieghere        value = frame0.FindValue("global", lldb.eValueTypeVariableGlobal)
5599451b44SJordan Rupprecht        error = lldb.SBError()
5699451b44SJordan Rupprecht        watchpoint = value.Watch(True, False, True, error)
57*2238dcc3SJonas Devlieghere        self.assertTrue(
58*2238dcc3SJonas Devlieghere            value and watchpoint, "Successfully found the variable and set a watchpoint"
59*2238dcc3SJonas Devlieghere        )
6099451b44SJordan Rupprecht        self.DebugSBValue(value)
6199451b44SJordan Rupprecht
6299451b44SJordan Rupprecht        # Now set the condition as "global==5".
63*2238dcc3SJonas Devlieghere        watchpoint.SetCondition("global==5")
64*2238dcc3SJonas Devlieghere        self.expect(watchpoint.GetCondition(), exe=False, startstr="global==5")
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        print(watchpoint)
7199451b44SJordan Rupprecht
7299451b44SJordan Rupprecht        # Continue.  Expect the program to stop due to the variable being
7399451b44SJordan Rupprecht        # written to.
7499451b44SJordan Rupprecht        process.Continue()
7599451b44SJordan Rupprecht
76*2238dcc3SJonas Devlieghere        if self.TraceOn():
7799451b44SJordan Rupprecht            lldbutil.print_stacktraces(process)
7899451b44SJordan Rupprecht
79*2238dcc3SJonas Devlieghere        thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonWatchpoint)
8099451b44SJordan Rupprecht        self.assertTrue(thread, "The thread stopped due to watchpoint")
8199451b44SJordan Rupprecht        self.DebugSBValue(value)
8299451b44SJordan Rupprecht
8399451b44SJordan Rupprecht        # Verify that the condition is met.
84619e2e09SDave Lee        self.assertEqual(value.GetValueAsUnsigned(), 5)
85