1"""
2Use lldb Python SBValue.WatchPointee() API to create a watchpoint for write of '*g_char_ptr'.
3"""
4
5from __future__ import print_function
6
7
8
9import lldb
10from lldbsuite.test.decorators import *
11from lldbsuite.test.lldbtest import *
12from lldbsuite.test import lldbutil
13
14
15class SetWatchlocationAPITestCase(TestBase):
16    NO_DEBUG_INFO_TESTCASE = True
17
18    def setUp(self):
19        # Call super's setUp().
20        TestBase.setUp(self)
21        # Our simple source filename.
22        self.source = 'main.cpp'
23        # Find the line number to break inside main().
24        self.line = line_number(
25            self.source, '// Set break point at this line.')
26        # This is for verifying that watch location works.
27        self.violating_func = "do_bad_thing_with_location"
28
29    @skipIfWindows # This test is flaky on Windows
30    def test_watch_location(self):
31        """Exercise SBValue.WatchPointee() 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.assertState(process.GetState(), lldb.eStateStopped,
52                         PROCESS_STOPPED)
53        thread = lldbutil.get_stopped_thread(
54            process, lldb.eStopReasonBreakpoint)
55        frame0 = thread.GetFrameAtIndex(0)
56
57        value = frame0.FindValue('g_char_ptr',
58                                 lldb.eValueTypeVariableGlobal)
59        pointee = value.CreateValueFromAddress(
60            "pointee",
61            value.GetValueAsUnsigned(0),
62            value.GetType().GetPointeeType())
63        # Watch for write to *g_char_ptr.
64        error = lldb.SBError()
65        watchpoint = value.WatchPointee(True, False, True, error)
66        self.assertTrue(value and watchpoint,
67                        "Successfully found the pointer and set a watchpoint")
68        self.DebugSBValue(value)
69        self.DebugSBValue(pointee)
70
71        # Hide stdout if not running with '-t' option.
72        if not self.TraceOn():
73            self.HideStdout()
74
75        print(watchpoint)
76
77        # Continue.  Expect the program to stop due to the variable being
78        # written to.
79        process.Continue()
80
81        if (self.TraceOn()):
82            lldbutil.print_stacktraces(process)
83
84        thread = lldbutil.get_stopped_thread(
85            process, lldb.eStopReasonWatchpoint)
86        self.assertTrue(thread, "The thread stopped due to watchpoint")
87        self.DebugSBValue(value)
88        self.DebugSBValue(pointee)
89
90        self.expect(
91            lldbutil.print_stacktrace(
92                thread,
93                string_buffer=True),
94            exe=False,
95            substrs=[
96                self.violating_func])
97
98        # This finishes our test.
99