xref: /llvm-project/lldb/test/API/python_api/watchpoint/condition/TestWatchpointConditionAPI.py (revision 2238dcc39358353cac21df75c3c3286ab20b8f53)
1"""
2Test watchpoint condition API.
3"""
4
5import lldb
6from lldbsuite.test.decorators import *
7from lldbsuite.test.lldbtest import *
8from lldbsuite.test import lldbutil
9
10
11class WatchpointConditionAPITestCase(TestBase):
12    NO_DEBUG_INFO_TESTCASE = True
13
14    def setUp(self):
15        # Call super's setUp().
16        TestBase.setUp(self)
17        # Our simple source filename.
18        self.source = "main.cpp"
19        # Find the line number to break inside main().
20        self.line = line_number(self.source, "// Set break point at this line.")
21        # And the watchpoint variable declaration line number.
22        self.decl = line_number(self.source, "// Watchpoint variable declaration.")
23        # Build dictionary to have unique executable names for each test
24        # method.
25        self.exe_name = self.testMethodName
26        self.d = {"CXX_SOURCES": self.source, "EXE": self.exe_name}
27
28    def test_watchpoint_cond_api(self):
29        """Test watchpoint condition API."""
30        self.build(dictionary=self.d)
31        self.setTearDownCleanup(dictionary=self.d)
32        exe = self.getBuildArtifact(self.exe_name)
33
34        # Create a target by the debugger.
35        target = self.dbg.CreateTarget(exe)
36        self.assertTrue(target, VALID_TARGET)
37
38        # Now create a breakpoint on main.c.
39        breakpoint = target.BreakpointCreateByLocation(self.source, self.line)
40        self.assertTrue(
41            breakpoint and breakpoint.GetNumLocations() == 1, VALID_BREAKPOINT
42        )
43
44        # Now launch the process, and do not stop at the entry point.
45        process = target.LaunchSimple(None, None, self.get_process_working_directory())
46
47        # We should be stopped due to the breakpoint.  Get frame #0.
48        process = target.GetProcess()
49        self.assertState(process.GetState(), lldb.eStateStopped, PROCESS_STOPPED)
50        thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint)
51        frame0 = thread.GetFrameAtIndex(0)
52
53        # Watch 'global' for write.
54        value = frame0.FindValue("global", lldb.eValueTypeVariableGlobal)
55        error = lldb.SBError()
56        watchpoint = value.Watch(True, False, True, error)
57        self.assertTrue(
58            value and watchpoint, "Successfully found the variable and set a watchpoint"
59        )
60        self.DebugSBValue(value)
61
62        # Now set the condition as "global==5".
63        watchpoint.SetCondition("global==5")
64        self.expect(watchpoint.GetCondition(), exe=False, startstr="global==5")
65
66        # Hide stdout if not running with '-t' option.
67        if not self.TraceOn():
68            self.HideStdout()
69
70        print(watchpoint)
71
72        # Continue.  Expect the program to stop due to the variable being
73        # written to.
74        process.Continue()
75
76        if self.TraceOn():
77            lldbutil.print_stacktraces(process)
78
79        thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonWatchpoint)
80        self.assertTrue(thread, "The thread stopped due to watchpoint")
81        self.DebugSBValue(value)
82
83        # Verify that the condition is met.
84        self.assertEqual(value.GetValueAsUnsigned(), 5)
85