1"""
2Test watchpoint list, enable, disable, and delete commands.
3"""
4
5
6
7import lldb
8from lldbsuite.test.decorators import *
9from lldbsuite.test.lldbtest import *
10from lldbsuite.test import lldbutil
11
12
13class WatchpointCommandsTestCase(TestBase):
14
15    mydir = TestBase.compute_mydir(__file__)
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.c'
23        # Find the line number to break inside main().
24        self.line = line_number(
25            self.source, '// Set break point at this line.')
26        self.line2 = line_number(
27            self.source,
28            '// Set 2nd break point for disable_then_enable test case.')
29        # And the watchpoint variable declaration line number.
30        self.decl = line_number(self.source,
31                                '// Watchpoint variable declaration.')
32        # Build dictionary to have unique executable names for each test
33        # method.
34        self.exe_name = self.testMethodName
35        self.d = {'C_SOURCES': self.source, 'EXE': self.exe_name}
36
37    # Read-write watchpoints not supported on SystemZ
38    @expectedFailureAll(archs=['s390x'])
39    def test_rw_watchpoint(self):
40        """Test read_write watchpoint and expect to stop two times."""
41        self.build(dictionary=self.d)
42        self.setTearDownCleanup(dictionary=self.d)
43
44        exe = self.getBuildArtifact(self.exe_name)
45        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
46
47        # Add a breakpoint to set a watchpoint when stopped on the breakpoint.
48        lldbutil.run_break_set_by_file_and_line(
49            self, None, self.line, num_expected_locations=1)
50
51        # Run the program.
52        self.runCmd("run", RUN_SUCCEEDED)
53
54        # We should be stopped again due to the breakpoint.
55        # The stop reason of the thread should be breakpoint.
56        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
57                    substrs=['stopped',
58                             'stop reason = breakpoint'])
59
60        # Now let's set a read_write-type watchpoint for 'global'.
61        # There should be two watchpoint hits (see main.c).
62        self.expect(
63            "watchpoint set variable -w read_write global",
64            WATCHPOINT_CREATED,
65            substrs=[
66                'Watchpoint created',
67                'size = 4',
68                'type = rw',
69                '%s:%d' %
70                (self.source,
71                 self.decl)])
72
73        # Use the '-v' option to do verbose listing of the watchpoint.
74        # The hit count should be 0 initially.
75        self.expect("watchpoint list -v",
76                    substrs=['Number of supported hardware watchpoints:',
77                             'hit_count = 0'])
78
79        self.runCmd("process continue")
80
81        # We should be stopped again due to the watchpoint (read_write type).
82        # The stop reason of the thread should be watchpoint.
83        self.expect("thread backtrace", STOPPED_DUE_TO_WATCHPOINT,
84                    substrs=['stop reason = watchpoint'])
85
86        self.runCmd("process continue")
87
88        # We should be stopped again due to the watchpoint (read_write type).
89        # The stop reason of the thread should be watchpoint.
90        self.expect("thread backtrace", STOPPED_DUE_TO_WATCHPOINT,
91                    substrs=['stop reason = watchpoint'])
92
93        self.runCmd("process continue")
94
95        # There should be no more watchpoint hit and the process status should
96        # be 'exited'.
97        self.expect("process status",
98                    substrs=['exited'])
99
100        # Use the '-v' option to do verbose listing of the watchpoint.
101        # The hit count should now be 2.
102        self.expect("watchpoint list -v",
103                    substrs=['hit_count = 2'])
104
105    # Read-write watchpoints not supported on SystemZ
106    @expectedFailureAll(archs=['s390x'])
107    def test_rw_watchpoint_delete(self):
108        """Test delete watchpoint and expect not to stop for watchpoint."""
109        self.build()
110        lldbutil.run_to_line_breakpoint(self, lldb.SBFileSpec(self.source),
111                                        self.line)
112
113        # Now let's set a read_write-type watchpoint for 'global'.
114        # There should be two watchpoint hits (see main.c).
115        self.expect(
116            "watchpoint set variable -w read_write global",
117            WATCHPOINT_CREATED,
118            substrs=[
119                'Watchpoint created',
120                'size = 4',
121                'type = rw',
122                '%s:%d' %
123                (self.source,
124                 self.decl)])
125
126        # Delete the watchpoint immediately, but set auto-confirm to true
127        # first.
128        self.runCmd("settings set auto-confirm true")
129        self.expect("watchpoint delete",
130                    substrs=['All watchpoints removed.'])
131        # Restore the original setting of auto-confirm.
132        self.runCmd("settings clear auto-confirm")
133
134        target = self.dbg.GetSelectedTarget()
135        self.assertTrue(target and not target.GetNumWatchpoints())
136
137        # Now let's set a read_write-type watchpoint for 'global'.
138        # There should be two watchpoint hits (see main.c).
139        self.expect(
140            "watchpoint set variable -w read_write global",
141            WATCHPOINT_CREATED,
142            substrs=[
143                'Watchpoint created',
144                'size = 4',
145                'type = rw',
146                '%s:%d' %
147                (self.source,
148                 self.decl)])
149
150
151        # Delete the watchpoint immediately using the force option.
152        self.expect("watchpoint delete --force",
153                    substrs=['All watchpoints removed.'])
154
155        self.assertTrue(target and not target.GetNumWatchpoints())
156
157        self.runCmd("process continue")
158
159        # There should be no more watchpoint hit and the process status should
160        # be 'exited'.
161        self.expect("process status",
162                    substrs=['exited'])
163
164    # Read-write watchpoints not supported on SystemZ
165    @expectedFailureAll(archs=['s390x'])
166    def test_rw_watchpoint_set_ignore_count(self):
167        """Test watchpoint ignore count and expect to not to stop at all."""
168        self.build(dictionary=self.d)
169        self.setTearDownCleanup(dictionary=self.d)
170
171        exe = self.getBuildArtifact(self.exe_name)
172        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
173
174        # Add a breakpoint to set a watchpoint when stopped on the breakpoint.
175        lldbutil.run_break_set_by_file_and_line(
176            self, None, self.line, num_expected_locations=1)
177
178        # Run the program.
179        self.runCmd("run", RUN_SUCCEEDED)
180
181        # We should be stopped again due to the breakpoint.
182        # The stop reason of the thread should be breakpoint.
183        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
184                    substrs=['stopped',
185                             'stop reason = breakpoint'])
186
187        # Now let's set a read_write-type watchpoint for 'global'.
188        # There should be two watchpoint hits (see main.c).
189        self.expect(
190            "watchpoint set variable -w read_write global",
191            WATCHPOINT_CREATED,
192            substrs=[
193                'Watchpoint created',
194                'size = 4',
195                'type = rw',
196                '%s:%d' %
197                (self.source,
198                 self.decl)])
199
200        # Set the ignore count of the watchpoint immediately.
201        self.expect("watchpoint ignore -i 2",
202                    substrs=['All watchpoints ignored.'])
203
204        # Use the '-v' option to do verbose listing of the watchpoint.
205        # Expect to find an ignore_count of 2.
206        self.expect("watchpoint list -v",
207                    substrs=['hit_count = 0', 'ignore_count = 2'])
208
209        self.runCmd("process continue")
210
211        # There should be no more watchpoint hit and the process status should
212        # be 'exited'.
213        self.expect("process status",
214                    substrs=['exited'])
215
216        # Use the '-v' option to do verbose listing of the watchpoint.
217        # Expect to find a hit_count of 2 as well.
218        self.expect("watchpoint list -v",
219                    substrs=['hit_count = 2', 'ignore_count = 2'])
220
221    # Read-write watchpoints not supported on SystemZ
222    @expectedFailureAll(archs=['s390x'])
223    def test_rw_disable_after_first_stop(self):
224        """Test read_write watchpoint but disable it after the first stop."""
225        self.build(dictionary=self.d)
226        self.setTearDownCleanup(dictionary=self.d)
227
228        exe = self.getBuildArtifact(self.exe_name)
229        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
230
231        # Add a breakpoint to set a watchpoint when stopped on the breakpoint.
232        lldbutil.run_break_set_by_file_and_line(
233            self, None, self.line, num_expected_locations=1)
234
235        # Run the program.
236        self.runCmd("run", RUN_SUCCEEDED)
237
238        # We should be stopped again due to the breakpoint.
239        # The stop reason of the thread should be breakpoint.
240        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
241                    substrs=['stopped',
242                             'stop reason = breakpoint'])
243
244        # Now let's set a read_write-type watchpoint for 'global'.
245        # There should be two watchpoint hits (see main.c).
246        self.expect(
247            "watchpoint set variable -w read_write global",
248            WATCHPOINT_CREATED,
249            substrs=[
250                'Watchpoint created',
251                'size = 4',
252                'type = rw',
253                '%s:%d' %
254                (self.source,
255                 self.decl)])
256
257        # Use the '-v' option to do verbose listing of the watchpoint.
258        # The hit count should be 0 initially.
259        self.expect("watchpoint list -v",
260                    substrs=['state = enabled', 'hit_count = 0'])
261
262        self.runCmd("process continue")
263
264        # We should be stopped again due to the watchpoint (read_write type).
265        # The stop reason of the thread should be watchpoint.
266        self.expect("thread backtrace", STOPPED_DUE_TO_WATCHPOINT,
267                    substrs=['stop reason = watchpoint'])
268
269        # Before continuing, we'll disable the watchpoint, which means we won't
270        # stop again after this.
271        self.runCmd("watchpoint disable")
272
273        self.expect("watchpoint list -v",
274                    substrs=['state = disabled', 'hit_count = 1'])
275
276        self.runCmd("process continue")
277
278        # There should be no more watchpoint hit and the process status should
279        # be 'exited'.
280        self.expect("process status",
281                    substrs=['exited'])
282
283        # Use the '-v' option to do verbose listing of the watchpoint.
284        # The hit count should be 1.
285        self.expect("watchpoint list -v",
286                    substrs=['hit_count = 1'])
287
288    # Read-write watchpoints not supported on SystemZ
289    @expectedFailureAll(archs=['s390x'])
290    def test_rw_disable_then_enable(self):
291        """Test read_write watchpoint, disable initially, then enable it."""
292        self.build(dictionary=self.d)
293        self.setTearDownCleanup(dictionary=self.d)
294
295        exe = self.getBuildArtifact(self.exe_name)
296        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
297
298        # Add a breakpoint to set a watchpoint when stopped on the breakpoint.
299        lldbutil.run_break_set_by_file_and_line(
300            self, None, self.line, num_expected_locations=1)
301        lldbutil.run_break_set_by_file_and_line(
302            self, None, self.line2, num_expected_locations=1)
303
304        # Run the program.
305        self.runCmd("run", RUN_SUCCEEDED)
306
307        # We should be stopped again due to the breakpoint.
308        # The stop reason of the thread should be breakpoint.
309        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
310                    substrs=['stopped',
311                             'stop reason = breakpoint'])
312
313        # Now let's set a read_write-type watchpoint for 'global'.
314        # There should be two watchpoint hits (see main.c).
315        self.expect(
316            "watchpoint set variable -w read_write global",
317            WATCHPOINT_CREATED,
318            substrs=[
319                'Watchpoint created',
320                'size = 4',
321                'type = rw',
322                '%s:%d' %
323                (self.source,
324                 self.decl)])
325
326        # Immediately, we disable the watchpoint.  We won't be stopping due to a
327        # watchpoint after this.
328        self.runCmd("watchpoint disable")
329
330        # Use the '-v' option to do verbose listing of the watchpoint.
331        # The hit count should be 0 initially.
332        self.expect("watchpoint list -v",
333                    substrs=['state = disabled', 'hit_count = 0'])
334
335        self.runCmd("process continue")
336
337        # We should be stopped again due to the breakpoint.
338        self.expect("thread backtrace", STOPPED_DUE_TO_BREAKPOINT,
339                    substrs=['stop reason = breakpoint'])
340
341        # Before continuing, we'll enable the watchpoint, which means we will
342        # stop again after this.
343        self.runCmd("watchpoint enable")
344
345        self.expect("watchpoint list -v",
346                    substrs=['state = enabled', 'hit_count = 0'])
347
348        self.runCmd("process continue")
349
350        # We should be stopped again due to the watchpoint (read_write type).
351        # The stop reason of the thread should be watchpoint.
352        self.expect("thread backtrace", STOPPED_DUE_TO_WATCHPOINT,
353                    substrs=['stop reason = watchpoint'])
354
355        self.runCmd("process continue")
356
357        # There should be no more watchpoint hit and the process status should
358        # be 'exited'.
359        self.expect("process status",
360                    substrs=['exited'])
361
362        # Use the '-v' option to do verbose listing of the watchpoint.
363        # The hit count should be 1.
364        self.expect("watchpoint list -v",
365                    substrs=['hit_count = 1'])
366