1"""
2Test 'watchpoint command'.
3"""
4
5
6
7import os
8import lldb
9from lldbsuite.test.decorators import *
10from lldbsuite.test.lldbtest import *
11from lldbsuite.test import lldbutil
12
13
14class WatchpointPythonCommandTestCase(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.cpp'
24        # Find the line number to break inside main().
25        self.line = line_number(
26            self.source, '// Set break point at this line.')
27        # And the watchpoint variable declaration line number.
28        self.decl = line_number(self.source,
29                                '// Watchpoint variable declaration.')
30        # Build dictionary to have unique executable names for each test
31        # method.
32        self.exe_name = self.testMethodName
33        self.d = {'CXX_SOURCES': self.source, 'EXE': self.exe_name}
34
35    def test_watchpoint_command(self):
36        """Test 'watchpoint command'."""
37        self.build(dictionary=self.d)
38        self.setTearDownCleanup(dictionary=self.d)
39
40        exe = self.getBuildArtifact(self.exe_name)
41        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
42
43        # Add a breakpoint to set a watchpoint when stopped on the breakpoint.
44        lldbutil.run_break_set_by_file_and_line(
45            self, None, self.line, num_expected_locations=1)
46
47        # Run the program.
48        self.runCmd("run", RUN_SUCCEEDED)
49
50        # We should be stopped again due to the breakpoint.
51        # The stop reason of the thread should be breakpoint.
52        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
53                    substrs=['stopped',
54                             'stop reason = breakpoint'])
55
56        # Now let's set a write-type watchpoint for 'global'.
57        self.expect(
58            "watchpoint set variable -w write global",
59            WATCHPOINT_CREATED,
60            substrs=[
61                'Watchpoint created',
62                'size = 4',
63                'type = w',
64                '%s:%d' %
65                (self.source,
66                 self.decl)])
67
68        self.runCmd(
69            'watchpoint command add -s python 1 -o \'frame.EvaluateExpression("cookie = 777")\'')
70
71        # List the watchpoint command we just added.
72        self.expect("watchpoint command list 1",
73                    substrs=['frame.EvaluateExpression', 'cookie = 777'])
74
75        # Use the '-v' option to do verbose listing of the watchpoint.
76        # The hit count should be 0 initially.
77        self.expect("watchpoint list -v",
78                    substrs=['hit_count = 0'])
79
80        self.runCmd("process continue")
81
82        # We should be stopped again due to the watchpoint (write type).
83        # The stop reason of the thread should be watchpoint.
84        self.expect("thread backtrace", STOPPED_DUE_TO_WATCHPOINT,
85                    substrs=['stop reason = watchpoint'])
86
87        # Check that the watchpoint snapshoting mechanism is working.
88        self.expect("watchpoint list -v",
89                    substrs=['old value: 0',
90                             'new value: 1'])
91
92        # The watchpoint command "forced" our global variable 'cookie' to
93        # become 777.
94        self.expect("frame variable --show-globals cookie",
95                    substrs=['(int32_t)', 'cookie = 777'])
96
97    def test_continue_in_watchpoint_command(self):
98        """Test continue in a watchpoint command."""
99        self.build(dictionary=self.d)
100        self.setTearDownCleanup(dictionary=self.d)
101
102        exe = self.getBuildArtifact(self.exe_name)
103        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
104
105        # Add a breakpoint to set a watchpoint when stopped on the breakpoint.
106        lldbutil.run_break_set_by_file_and_line(
107            self, None, self.line, num_expected_locations=1)
108
109        # Run the program.
110        self.runCmd("run", RUN_SUCCEEDED)
111
112        # We should be stopped again due to the breakpoint.
113        # The stop reason of the thread should be breakpoint.
114        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
115                    substrs=['stopped',
116                             'stop reason = breakpoint'])
117
118        # Now let's set a write-type watchpoint for 'global'.
119        self.expect(
120            "watchpoint set variable -w write global",
121            WATCHPOINT_CREATED,
122            substrs=[
123                'Watchpoint created',
124                'size = 4',
125                'type = w',
126                '%s:%d' %
127                (self.source,
128                 self.decl)])
129
130        cmd_script_file = os.path.join(self.getSourceDir(),
131                                       "watchpoint_command.py")
132        self.runCmd("command script import '%s'" % (cmd_script_file))
133
134        self.runCmd(
135            'watchpoint command add -F watchpoint_command.watchpoint_command')
136
137        # List the watchpoint command we just added.
138        self.expect("watchpoint command list 1",
139                    substrs=['watchpoint_command.watchpoint_command'])
140
141        self.runCmd("process continue")
142
143        # We should be stopped again due to the watchpoint (write type).
144        # The stop reason of the thread should be watchpoint.
145        self.expect("thread backtrace", STOPPED_DUE_TO_WATCHPOINT,
146                    substrs=['stop reason = watchpoint'])
147
148        # We should have hit the watchpoint once, set cookie to 888, then continued to the
149        # second hit and set it to 999
150        self.expect("frame variable --show-globals cookie",
151                    substrs=['(int32_t)', 'cookie = 999'])
152
153