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