Home | History | Annotate | Download | only in hello_watchpoint
      1 """
      2 Test my first lldb watchpoint.
      3 """
      4 
      5 import os, time
      6 import unittest2
      7 import lldb
      8 from lldbtest import *
      9 import lldbutil
     10 
     11 class HelloWatchpointTestCase(TestBase):
     12 
     13     def getCategories (self):
     14         return ['basic_process']
     15 
     16     mydir = os.path.join("functionalities", "watchpoint", "hello_watchpoint")
     17 
     18     @dsym_test
     19     def test_hello_watchpoint_with_dsym_using_watchpoint_set(self):
     20         """Test a simple sequence of watchpoint creation and watchpoint hit."""
     21         self.buildDsym(dictionary=self.d)
     22         self.setTearDownCleanup(dictionary=self.d)
     23         self.hello_watchpoint()
     24 
     25     @expectedFailureFreeBSD('llvm.org/pr16706') # Watchpoints fail on FreeBSD
     26     @dwarf_test
     27     def test_hello_watchpoint_with_dwarf_using_watchpoint_set(self):
     28         """Test a simple sequence of watchpoint creation and watchpoint hit."""
     29         self.buildDwarf(dictionary=self.d)
     30         self.setTearDownCleanup(dictionary=self.d)
     31         self.hello_watchpoint()
     32 
     33     def setUp(self):
     34         # Call super's setUp().
     35         TestBase.setUp(self)
     36         # Our simple source filename.
     37         self.source = 'main.c'
     38         # Find the line number to break inside main().
     39         self.line = line_number(self.source, '// Set break point at this line.')
     40         # And the watchpoint variable declaration line number.
     41         self.decl = line_number(self.source, '// Watchpoint variable declaration.')
     42         # Build dictionary to have unique executable names for each test method.
     43         self.exe_name = self.testMethodName
     44         self.d = {'C_SOURCES': self.source, 'EXE': self.exe_name}
     45 
     46     def hello_watchpoint(self):
     47         """Test a simple sequence of watchpoint creation and watchpoint hit."""
     48         exe = os.path.join(os.getcwd(), self.exe_name)
     49         self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
     50 
     51         # Add a breakpoint to set a watchpoint when stopped on the breakpoint.
     52         lldbutil.run_break_set_by_file_and_line (self, None, self.line, num_expected_locations=1)
     53 
     54         # Run the program.
     55         self.runCmd("run", RUN_SUCCEEDED)
     56 
     57         # We should be stopped again due to the breakpoint.
     58         # The stop reason of the thread should be breakpoint.
     59         self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
     60             substrs = ['stopped',
     61                        'stop reason = breakpoint'])
     62 
     63         # Now let's set a write-type watchpoint for 'global'.
     64         # There should be only one watchpoint hit (see main.c).
     65         self.expect("watchpoint set variable -w write global", WATCHPOINT_CREATED,
     66             substrs = ['Watchpoint created', 'size = 4', 'type = w',
     67                        '%s:%d' % (self.source, self.decl)])
     68 
     69         # Use the '-v' option to do verbose listing of the watchpoint.
     70         # The hit count should be 0 initially.
     71         self.expect("watchpoint list -v",
     72             substrs = ['hit_count = 0'])
     73 
     74         self.runCmd("process continue")
     75 
     76         # We should be stopped again due to the watchpoint (write type), but
     77         # only once.  The stop reason of the thread should be watchpoint.
     78         self.expect("thread list", STOPPED_DUE_TO_WATCHPOINT,
     79             substrs = ['stopped',
     80                        'stop reason = watchpoint'])
     81 
     82         self.runCmd("process continue")
     83         # Don't expect the read of 'global' to trigger a stop exception.
     84         # The process status should be 'exited'.
     85         self.expect("process status",
     86             substrs = ['exited'])
     87 
     88         # Use the '-v' option to do verbose listing of the watchpoint.
     89         # The hit count should now be 1.
     90         self.expect("watchpoint list -v",
     91             substrs = ['hit_count = 1'])
     92 
     93 
     94 if __name__ == '__main__':
     95     import atexit
     96     lldb.SBDebugger.Initialize()
     97     atexit.register(lambda: lldb.SBDebugger.Terminate())
     98     unittest2.main()
     99