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