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