• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"""
2Test watchpoint list, enable, disable, and delete commands.
3"""
4
5import os, time
6import unittest2
7import lldb
8from lldbtest import *
9import lldbutil
10
11class WatchpointCommandsTestCase(TestBase):
12
13    mydir = os.path.join("functionalities", "watchpoint", "watchpoint_commands")
14
15    def setUp(self):
16        # Call super's setUp().
17        TestBase.setUp(self)
18        # Our simple source filename.
19        self.source = 'main.c'
20        # Find the line number to break inside main().
21        self.line = line_number(self.source, '// Set break point at this line.')
22        self.line2 = line_number(self.source, '// Set 2nd break point for disable_then_enable test case.')
23        # And the watchpoint variable declaration line number.
24        self.decl = line_number(self.source, '// Watchpoint variable declaration.')
25        # Build dictionary to have unique executable names for each test method.
26        self.exe_name = self.testMethodName
27        self.d = {'C_SOURCES': self.source, 'EXE': self.exe_name}
28
29    @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
30    @dsym_test
31    def test_rw_watchpoint_with_dsym(self):
32        """Test read_write watchpoint and expect to stop two times."""
33        self.buildDsym(dictionary=self.d)
34        self.setTearDownCleanup(dictionary=self.d)
35        self.normal_read_write_watchpoint()
36
37    @expectedFailureFreeBSD('llvm.org/pr16706') # Watchpoints fail on FreeBSD
38    @dwarf_test
39    def test_rw_watchpoint_with_dwarf(self):
40        """Test read_write watchpoint and expect to stop two times."""
41        self.buildDwarf(dictionary=self.d)
42        self.setTearDownCleanup(dictionary=self.d)
43        self.normal_read_write_watchpoint()
44
45    @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
46    @dsym_test
47    def test_rw_watchpoint_delete_with_dsym(self):
48        """Test delete watchpoint and expect not to stop for watchpoint."""
49        self.buildDsym(dictionary=self.d)
50        self.setTearDownCleanup(dictionary=self.d)
51        self.delete_read_write_watchpoint()
52
53    @expectedFailureFreeBSD('llvm.org/pr16706') # Watchpoints fail on FreeBSD
54    @dwarf_test
55    def test_rw_watchpoint_delete_with_dwarf(self):
56        """Test delete watchpoint and expect not to stop for watchpoint."""
57        self.buildDwarf(dictionary=self.d)
58        self.setTearDownCleanup(dictionary=self.d)
59        self.delete_read_write_watchpoint()
60
61    @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
62    @dsym_test
63    def test_rw_watchpoint_set_ignore_count_with_dsym(self):
64        """Test watchpoint ignore count and expect to not to stop at all."""
65        self.buildDsym(dictionary=self.d)
66        self.setTearDownCleanup(dictionary=self.d)
67        self.ignore_read_write_watchpoint()
68
69    @expectedFailureFreeBSD('llvm.org/pr16706') # Watchpoints fail on FreeBSD
70    @dwarf_test
71    def test_rw_watchpoint_set_ignore_count_with_dwarf(self):
72        """Test watchpoint ignore count and expect to not to stop at all."""
73        self.buildDwarf(dictionary=self.d)
74        self.setTearDownCleanup(dictionary=self.d)
75        self.ignore_read_write_watchpoint()
76
77    @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
78    @dsym_test
79    def test_rw_disable_after_first_stop_with_dsym(self):
80        """Test read_write watchpoint but disable it after the first stop."""
81        self.buildDsym(dictionary=self.d)
82        self.setTearDownCleanup(dictionary=self.d)
83        self.read_write_watchpoint_disable_after_first_stop()
84
85    @expectedFailureFreeBSD('llvm.org/pr16706') # Watchpoints fail on FreeBSD
86    @dwarf_test
87    def test_rw_disable_after_first_stop__with_dwarf(self):
88        """Test read_write watchpoint but disable it after the first stop."""
89        self.buildDwarf(dictionary=self.d)
90        self.setTearDownCleanup(dictionary=self.d)
91        self.read_write_watchpoint_disable_after_first_stop()
92
93    @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
94    @dsym_test
95    def test_rw_disable_then_enable_with_dsym(self):
96        """Test read_write watchpoint, disable initially, then enable it."""
97        self.buildDsym(dictionary=self.d)
98        self.setTearDownCleanup(dictionary=self.d)
99        self.read_write_watchpoint_disable_then_enable()
100
101    @expectedFailureFreeBSD('llvm.org/pr16706') # Watchpoints fail on FreeBSD
102    @dwarf_test
103    def test_rw_disable_then_enable_with_dwarf(self):
104        """Test read_write watchpoint, disable initially, then enable it."""
105        self.buildDwarf(dictionary=self.d)
106        self.setTearDownCleanup(dictionary=self.d)
107        self.read_write_watchpoint_disable_then_enable()
108
109    def normal_read_write_watchpoint(self):
110        """Do read_write watchpoint and expect to stop two times."""
111        exe = os.path.join(os.getcwd(), self.exe_name)
112        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
113
114        # Add a breakpoint to set a watchpoint when stopped on the breakpoint.
115        lldbutil.run_break_set_by_file_and_line (self, None, self.line, num_expected_locations=1)
116
117        # Run the program.
118        self.runCmd("run", RUN_SUCCEEDED)
119
120        # We should be stopped again due to the breakpoint.
121        # The stop reason of the thread should be breakpoint.
122        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
123            substrs = ['stopped',
124                       'stop reason = breakpoint'])
125
126        # Now let's set a read_write-type watchpoint for 'global'.
127        # There should be two watchpoint hits (see main.c).
128        self.expect("watchpoint set variable -w read_write global", WATCHPOINT_CREATED,
129            substrs = ['Watchpoint created', 'size = 4', 'type = rw',
130                       '%s:%d' % (self.source, self.decl)])
131
132        # Use the '-v' option to do verbose listing of the watchpoint.
133        # The hit count should be 0 initially.
134        self.expect("watchpoint list -v",
135            substrs = ['Number of supported hardware watchpoints:',
136                       'hit_count = 0'])
137
138        self.runCmd("process continue")
139
140        # We should be stopped again due to the watchpoint (read_write type).
141        # The stop reason of the thread should be watchpoint.
142        self.expect("thread backtrace", STOPPED_DUE_TO_WATCHPOINT,
143            substrs = ['stop reason = watchpoint'])
144
145        self.runCmd("process continue")
146
147        # We should be stopped again due to the watchpoint (read_write type).
148        # The stop reason of the thread should be watchpoint.
149        self.expect("thread backtrace", STOPPED_DUE_TO_WATCHPOINT,
150            substrs = ['stop reason = watchpoint'])
151
152        self.runCmd("process continue")
153
154        # There should be no more watchpoint hit and the process status should
155        # be 'exited'.
156        self.expect("process status",
157            substrs = ['exited'])
158
159        # Use the '-v' option to do verbose listing of the watchpoint.
160        # The hit count should now be 2.
161        self.expect("watchpoint list -v",
162            substrs = ['hit_count = 2'])
163
164    def delete_read_write_watchpoint(self):
165        """Do delete watchpoint immediately and expect not to stop for watchpoint."""
166        exe = os.path.join(os.getcwd(), self.exe_name)
167        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
168
169        # Add a breakpoint to set a watchpoint when stopped on the breakpoint.
170        lldbutil.run_break_set_by_file_and_line (self, None, self.line, num_expected_locations=1)
171
172        # Run the program.
173        self.runCmd("run", RUN_SUCCEEDED)
174
175        # We should be stopped again due to the breakpoint.
176        # The stop reason of the thread should be breakpoint.
177        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
178            substrs = ['stopped',
179                       'stop reason = breakpoint'])
180
181        # Now let's set a read_write-type watchpoint for 'global'.
182        # There should be two watchpoint hits (see main.c).
183        self.expect("watchpoint set variable -w read_write global", WATCHPOINT_CREATED,
184            substrs = ['Watchpoint created', 'size = 4', 'type = rw',
185                       '%s:%d' % (self.source, self.decl)])
186
187        # Delete the watchpoint immediately, but set auto-confirm to true first.
188        self.runCmd("settings set auto-confirm true")
189        self.expect("watchpoint delete",
190            substrs = ['All watchpoints removed.'])
191        # Restore the original setting of auto-confirm.
192        self.runCmd("settings clear auto-confirm")
193
194        # Use the '-v' option to do verbose listing of the watchpoint.
195        self.runCmd("watchpoint list -v")
196
197        self.runCmd("process continue")
198
199        # There should be no more watchpoint hit and the process status should
200        # be 'exited'.
201        self.expect("process status",
202            substrs = ['exited'])
203
204    def ignore_read_write_watchpoint(self):
205        """Test watchpoint ignore count and expect to not to stop at all."""
206        exe = os.path.join(os.getcwd(), self.exe_name)
207        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
208
209        # Add a breakpoint to set a watchpoint when stopped on the breakpoint.
210        lldbutil.run_break_set_by_file_and_line (self, None, self.line, num_expected_locations=1)
211
212        # Run the program.
213        self.runCmd("run", RUN_SUCCEEDED)
214
215        # We should be stopped again due to the breakpoint.
216        # The stop reason of the thread should be breakpoint.
217        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
218            substrs = ['stopped',
219                       'stop reason = breakpoint'])
220
221        # Now let's set a read_write-type watchpoint for 'global'.
222        # There should be two watchpoint hits (see main.c).
223        self.expect("watchpoint set variable -w read_write global", WATCHPOINT_CREATED,
224            substrs = ['Watchpoint created', 'size = 4', 'type = rw',
225                       '%s:%d' % (self.source, self.decl)])
226
227        # Set the ignore count of the watchpoint immediately.
228        self.expect("watchpoint ignore -i 2",
229            substrs = ['All watchpoints ignored.'])
230
231        # Use the '-v' option to do verbose listing of the watchpoint.
232        # Expect to find an ignore_count of 2.
233        self.expect("watchpoint list -v",
234            substrs = ['hit_count = 0', 'ignore_count = 2'])
235
236        self.runCmd("process continue")
237
238        # There should be no more watchpoint hit and the process status should
239        # be 'exited'.
240        self.expect("process status",
241            substrs = ['exited'])
242
243        # Use the '-v' option to do verbose listing of the watchpoint.
244        # Expect to find a hit_count of 2 as well.
245        self.expect("watchpoint list -v",
246            substrs = ['hit_count = 2', 'ignore_count = 2'])
247
248    def read_write_watchpoint_disable_after_first_stop(self):
249        """Do read_write watchpoint but disable it after the first stop."""
250        exe = os.path.join(os.getcwd(), self.exe_name)
251        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
252
253        # Add a breakpoint to set a watchpoint when stopped on the breakpoint.
254        lldbutil.run_break_set_by_file_and_line (self, "main.m")
255
256        # Run the program.
257        self.runCmd("run", RUN_SUCCEEDED)
258
259        # We should be stopped again due to the breakpoint.
260        # The stop reason of the thread should be breakpoint.
261        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
262            substrs = ['stopped',
263                       'stop reason = breakpoint'])
264
265        # Now let's set a read_write-type watchpoint for 'global'.
266        # There should be two watchpoint hits (see main.c).
267        self.expect("watchpoint set variable -w read_write global", WATCHPOINT_CREATED,
268            substrs = ['Watchpoint created', 'size = 4', 'type = rw',
269                       '%s:%d' % (self.source, self.decl)])
270
271        # Use the '-v' option to do verbose listing of the watchpoint.
272        # The hit count should be 0 initially.
273        self.expect("watchpoint list -v",
274            substrs = ['Number of supported hardware watchpoints:',
275                       'hit_count = 0'])
276
277        self.runCmd("process continue")
278
279        # We should be stopped again due to the watchpoint (read_write type).
280        # The stop reason of the thread should be watchpoint.
281        self.expect("thread backtrace", STOPPED_DUE_TO_WATCHPOINT,
282            substrs = ['stop reason = watchpoint'])
283
284        self.runCmd("process continue")
285
286        # We should be stopped again due to the watchpoint (read_write type).
287        # The stop reason of the thread should be watchpoint.
288        self.expect("thread backtrace", STOPPED_DUE_TO_WATCHPOINT,
289            substrs = ['stop reason = watchpoint'])
290
291        self.runCmd("process continue")
292
293        # There should be no more watchpoint hit and the process status should
294        # be 'exited'.
295        self.expect("process status",
296            substrs = ['exited'])
297
298        # Use the '-v' option to do verbose listing of the watchpoint.
299        # The hit count should now be 2.
300        self.expect("watchpoint list -v",
301            substrs = ['hit_count = 2'])
302
303    def delete_read_write_watchpoint(self):
304        """Do delete watchpoint immediately and expect not to stop for watchpoint."""
305        exe = os.path.join(os.getcwd(), self.exe_name)
306        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
307
308        # Add a breakpoint to set a watchpoint when stopped on the breakpoint.
309        lldbutil.run_break_set_by_file_and_line (self, None, self.line, num_expected_locations=1)
310
311        # Run the program.
312        self.runCmd("run", RUN_SUCCEEDED)
313
314        # We should be stopped again due to the breakpoint.
315        # The stop reason of the thread should be breakpoint.
316        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
317            substrs = ['stopped',
318                       'stop reason = breakpoint'])
319
320        # Now let's set a read_write-type watchpoint for 'global'.
321        # There should be two watchpoint hits (see main.c).
322        self.expect("watchpoint set variable -w read_write global", WATCHPOINT_CREATED,
323            substrs = ['Watchpoint created', 'size = 4', 'type = rw',
324                       '%s:%d' % (self.source, self.decl)])
325
326        # Delete the watchpoint immediately, but set auto-confirm to true first.
327        self.runCmd("settings set auto-confirm true")
328        self.expect("watchpoint delete",
329            substrs = ['All watchpoints removed.'])
330        # Restore the original setting of auto-confirm.
331        self.runCmd("settings clear auto-confirm")
332
333        # Use the '-v' option to do verbose listing of the watchpoint.
334        self.runCmd("watchpoint list -v")
335
336        self.runCmd("process continue")
337
338        # There should be no more watchpoint hit and the process status should
339        # be 'exited'.
340        self.expect("process status",
341            substrs = ['exited'])
342
343    def ignore_read_write_watchpoint(self):
344        """Test watchpoint ignore count and expect to not to stop at all."""
345        exe = os.path.join(os.getcwd(), self.exe_name)
346        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
347
348        # Add a breakpoint to set a watchpoint when stopped on the breakpoint.
349        lldbutil.run_break_set_by_file_and_line (self, None, self.line, num_expected_locations=1)
350
351        # Run the program.
352        self.runCmd("run", RUN_SUCCEEDED)
353
354        # We should be stopped again due to the breakpoint.
355        # The stop reason of the thread should be breakpoint.
356        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
357            substrs = ['stopped',
358                       'stop reason = breakpoint'])
359
360        # Now let's set a read_write-type watchpoint for 'global'.
361        # There should be two watchpoint hits (see main.c).
362        self.expect("watchpoint set variable -w read_write global", WATCHPOINT_CREATED,
363            substrs = ['Watchpoint created', 'size = 4', 'type = rw',
364                       '%s:%d' % (self.source, self.decl)])
365
366        # Set the ignore count of the watchpoint immediately.
367        self.expect("watchpoint ignore -i 2",
368            substrs = ['All watchpoints ignored.'])
369
370        # Use the '-v' option to do verbose listing of the watchpoint.
371        # Expect to find an ignore_count of 2.
372        self.expect("watchpoint list -v",
373            substrs = ['hit_count = 0', 'ignore_count = 2'])
374
375        self.runCmd("process continue")
376
377        # There should be no more watchpoint hit and the process status should
378        # be 'exited'.
379        self.expect("process status",
380            substrs = ['exited'])
381
382        # Use the '-v' option to do verbose listing of the watchpoint.
383        # Expect to find a hit_count of 2 as well.
384        self.expect("watchpoint list -v",
385            substrs = ['hit_count = 2', 'ignore_count = 2'])
386
387    def read_write_watchpoint_disable_after_first_stop(self):
388        """Do read_write watchpoint but disable it after the first stop."""
389        exe = os.path.join(os.getcwd(), self.exe_name)
390        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
391
392        # Add a breakpoint to set a watchpoint when stopped on the breakpoint.
393        lldbutil.run_break_set_by_file_and_line (self, None, self.line, num_expected_locations=1)
394
395        # Run the program.
396        self.runCmd("run", RUN_SUCCEEDED)
397
398        # We should be stopped again due to the breakpoint.
399        # The stop reason of the thread should be breakpoint.
400        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
401            substrs = ['stopped',
402                       'stop reason = breakpoint'])
403
404        # Now let's set a read_write-type watchpoint for 'global'.
405        # There should be two watchpoint hits (see main.c).
406        self.expect("watchpoint set variable -w read_write global", WATCHPOINT_CREATED,
407            substrs = ['Watchpoint created', 'size = 4', 'type = rw',
408                       '%s:%d' % (self.source, self.decl)])
409
410        # Use the '-v' option to do verbose listing of the watchpoint.
411        # The hit count should be 0 initially.
412        self.expect("watchpoint list -v",
413            substrs = ['state = enabled', 'hit_count = 0'])
414
415        self.runCmd("process continue")
416
417        # We should be stopped again due to the watchpoint (read_write type).
418        # The stop reason of the thread should be watchpoint.
419        self.expect("thread backtrace", STOPPED_DUE_TO_WATCHPOINT,
420            substrs = ['stop reason = watchpoint'])
421
422        # Before continuing, we'll disable the watchpoint, which means we won't
423        # stop agian after this.
424        self.runCmd("watchpoint disable")
425
426        self.expect("watchpoint list -v",
427            substrs = ['state = disabled', 'hit_count = 1'])
428
429        self.runCmd("process continue")
430
431        # There should be no more watchpoint hit and the process status should
432        # be 'exited'.
433        self.expect("process status",
434            substrs = ['exited'])
435
436        # Use the '-v' option to do verbose listing of the watchpoint.
437        # The hit count should be 1.
438        self.expect("watchpoint list -v",
439            substrs = ['hit_count = 1'])
440
441    def read_write_watchpoint_disable_then_enable(self):
442        """Do read_write watchpoint, disable initially, then enable it."""
443        exe = os.path.join(os.getcwd(), self.exe_name)
444        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
445
446        # Add a breakpoint to set a watchpoint when stopped on the breakpoint.
447        lldbutil.run_break_set_by_file_and_line (self, None, self.line, num_expected_locations=1)
448        lldbutil.run_break_set_by_file_and_line (self, None, self.line2, num_expected_locations=1)
449
450        # Run the program.
451        self.runCmd("run", RUN_SUCCEEDED)
452
453        # We should be stopped again due to the breakpoint.
454        # The stop reason of the thread should be breakpoint.
455        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
456            substrs = ['stopped',
457                       'stop reason = breakpoint'])
458
459        # Now let's set a read_write-type watchpoint for 'global'.
460        # There should be two watchpoint hits (see main.c).
461        self.expect("watchpoint set variable -w read_write global", WATCHPOINT_CREATED,
462            substrs = ['Watchpoint created', 'size = 4', 'type = rw',
463                       '%s:%d' % (self.source, self.decl)])
464
465        # Immediately, we disable the watchpoint.  We won't be stopping due to a
466        # watchpoint after this.
467        self.runCmd("watchpoint disable")
468
469        # Use the '-v' option to do verbose listing of the watchpoint.
470        # The hit count should be 0 initially.
471        self.expect("watchpoint list -v",
472            substrs = ['state = disabled', 'hit_count = 0'])
473
474        self.runCmd("process continue")
475
476        # We should be stopped again due to the breakpoint.
477        self.expect("thread backtrace", STOPPED_DUE_TO_BREAKPOINT,
478            substrs = ['stop reason = breakpoint'])
479
480        # Before continuing, we'll enable the watchpoint, which means we will
481        # stop agian after this.
482        self.runCmd("watchpoint enable")
483
484        self.expect("watchpoint list -v",
485            substrs = ['state = enabled', 'hit_count = 0'])
486
487        self.runCmd("process continue")
488
489        # We should be stopped again due to the watchpoint (read_write type).
490        # The stop reason of the thread should be watchpoint.
491        self.expect("thread backtrace", STOPPED_DUE_TO_WATCHPOINT,
492            substrs = ['stop reason = watchpoint'])
493
494        self.runCmd("process continue")
495
496        # There should be no more watchpoint hit and the process status should
497        # be 'exited'.
498        self.expect("process status",
499            substrs = ['exited'])
500
501        # Use the '-v' option to do verbose listing of the watchpoint.
502        # The hit count should be 1.
503        self.expect("watchpoint list -v",
504            substrs = ['hit_count = 1'])
505
506
507if __name__ == '__main__':
508    import atexit
509    lldb.SBDebugger.Initialize()
510    atexit.register(lambda: lldb.SBDebugger.Terminate())
511    unittest2.main()
512