1"""
2Use lldb Python SBValue API to create a watchpoint for read_write of 'globl' var.
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 SetWatchpointAPITestCase(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.c'
22        # Find the line number to break inside main().
23        self.line = line_number(
24            self.source, '// Set break point at this line.')
25
26    # Read-write watchpoints not supported on SystemZ
27    @expectedFailureAll(archs=['s390x'])
28    def test_watch_val(self):
29        """Exercise SBValue.Watch() API to set a watchpoint."""
30        self.build()
31        exe = self.getBuildArtifact("a.out")
32
33        # Create a target by the debugger.
34        target = self.dbg.CreateTarget(exe)
35        self.assertTrue(target, VALID_TARGET)
36
37        # Now create a breakpoint on main.c.
38        breakpoint = target.BreakpointCreateByLocation(self.source, self.line)
39        self.assertTrue(breakpoint and
40                        breakpoint.GetNumLocations() == 1,
41                        VALID_BREAKPOINT)
42
43        # Now launch the process, and do not stop at the entry point.
44        process = target.LaunchSimple(
45            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,
50                         PROCESS_STOPPED)
51        thread = lldbutil.get_stopped_thread(
52            process, lldb.eStopReasonBreakpoint)
53        frame0 = thread.GetFrameAtIndex(0)
54
55        # Watch 'global' for read and write.
56        value = frame0.FindValue('global', lldb.eValueTypeVariableGlobal)
57        error = lldb.SBError()
58        watchpoint = value.Watch(True, True, True, error)
59        self.assertTrue(value and watchpoint,
60                        "Successfully found the variable and set a watchpoint")
61        self.DebugSBValue(value)
62
63        # Hide stdout if not running with '-t' option.
64        if not self.TraceOn():
65            self.HideStdout()
66
67        print(watchpoint)
68
69        # Continue.  Expect the program to stop due to the variable being
70        # written to.
71        process.Continue()
72
73        if (self.TraceOn()):
74            lldbutil.print_stacktraces(process)
75
76        thread = lldbutil.get_stopped_thread(
77            process, lldb.eStopReasonWatchpoint)
78        self.assertTrue(thread, "The thread stopped due to watchpoint")
79        self.DebugSBValue(value)
80
81        # Continue.  Expect the program to stop due to the variable being read
82        # from.
83        process.Continue()
84
85        if (self.TraceOn()):
86            lldbutil.print_stacktraces(process)
87
88        thread = lldbutil.get_stopped_thread(
89            process, lldb.eStopReasonWatchpoint)
90        self.assertTrue(thread, "The thread stopped due to watchpoint")
91        self.DebugSBValue(value)
92
93        # Continue the process.  We don't expect the program to be stopped
94        # again.
95        process.Continue()
96
97        # At this point, the inferior process should have exited.
98        self.assertEqual(
99            process.GetState(), lldb.eStateExited,
100            PROCESS_EXITED)
101
102        self.dbg.DeleteTarget(target)
103        self.assertFalse(watchpoint.IsValid())
104