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
17    mydir = TestBase.compute_mydir(__file__)
18    NO_DEBUG_INFO_TESTCASE = True
19
20    def setUp(self):
21        # Call super's setUp().
22        TestBase.setUp(self)
23        # Our simple source filename.
24        self.source = 'main.cpp'
25        # Find the line number to break inside main().
26        self.line = line_number(
27            self.source, '// Set break point at this line.')
28        # This is for verifying that watch location works.
29        self.violating_func = "do_bad_thing_with_location"
30
31    def test_watch_location(self):
32        """Exercise SBValue.WatchPointee() API to set a watchpoint."""
33        self.build()
34        exe = self.getBuildArtifact("a.out")
35
36        # Create a target by the debugger.
37        target = self.dbg.CreateTarget(exe)
38        self.assertTrue(target, VALID_TARGET)
39
40        # Now create a breakpoint on main.c.
41        breakpoint = target.BreakpointCreateByLocation(self.source, self.line)
42        self.assertTrue(breakpoint and
43                        breakpoint.GetNumLocations() == 1,
44                        VALID_BREAKPOINT)
45
46        # Now launch the process, and do not stop at the entry point.
47        process = target.LaunchSimple(
48            None, None, self.get_process_working_directory())
49
50        # We should be stopped due to the breakpoint.  Get frame #0.
51        process = target.GetProcess()
52        self.assertEqual(process.GetState(), lldb.eStateStopped,
53                        PROCESS_STOPPED)
54        thread = lldbutil.get_stopped_thread(
55            process, lldb.eStopReasonBreakpoint)
56        frame0 = thread.GetFrameAtIndex(0)
57
58        value = frame0.FindValue('g_char_ptr',
59                                 lldb.eValueTypeVariableGlobal)
60        pointee = value.CreateValueFromAddress(
61            "pointee",
62            value.GetValueAsUnsigned(0),
63            value.GetType().GetPointeeType())
64        # Watch for write to *g_char_ptr.
65        error = lldb.SBError()
66        watchpoint = value.WatchPointee(True, False, True, error)
67        self.assertTrue(value and watchpoint,
68                        "Successfully found the pointer and set a watchpoint")
69        self.DebugSBValue(value)
70        self.DebugSBValue(pointee)
71
72        # Hide stdout if not running with '-t' option.
73        if not self.TraceOn():
74            self.HideStdout()
75
76        print(watchpoint)
77
78        # Continue.  Expect the program to stop due to the variable being
79        # written to.
80        process.Continue()
81
82        if (self.TraceOn()):
83            lldbutil.print_stacktraces(process)
84
85        thread = lldbutil.get_stopped_thread(
86            process, lldb.eStopReasonWatchpoint)
87        self.assertTrue(thread, "The thread stopped due to watchpoint")
88        self.DebugSBValue(value)
89        self.DebugSBValue(pointee)
90
91        self.expect(
92            lldbutil.print_stacktrace(
93                thread,
94                string_buffer=True),
95            exe=False,
96            substrs=[
97                self.violating_func])
98
99        # This finishes our test.
100