TestMyFirstWatchpoint.py revision 58c66e2c4273c101a82ce02e17cc10d338b825bb
158c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen"""
258c66e2c4273c101a82ce02e17cc10d338b825bbJohnny ChenTest my first lldb watchpoint.
358c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen"""
458c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen
558c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chenimport os, time
658c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chenimport unittest2
758c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chenimport lldb
858c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chenfrom lldbtest import *
958c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen
1058c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chenclass HelloWatchpointTestCase(TestBase):
1158c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen
1258c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen    mydir = os.path.join("functionalities", "watchpoint", "hello_watchpoint")
1358c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen
1458c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen    @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
1558c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen    def test_hello_watchpoint_with_dsym(self):
1658c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen        """Test a simple sequence of watchpoint creation and watchpoint hit."""
1758c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen        self.buildDsym(dictionary=self.d)
1858c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen        self.setTearDownCleanup(dictionary=self.d)
1958c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen        self.hello_watchpoint()
2058c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen
2158c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen    def test_hello_watchpoint_with_dwarf(self):
2258c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen        """Test a simple sequence of watchpoint creation and watchpoint hit."""
2358c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen        self.buildDwarf(dictionary=self.d)
2458c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen        self.setTearDownCleanup(dictionary=self.d)
2558c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen        self.hello_watchpoint()
2658c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen
2758c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen    def setUp(self):
2858c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen        # Call super's setUp().
2958c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen        TestBase.setUp(self)
3058c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen        # Our simple source filename.
3158c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen        self.source = 'main.c'
3258c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen        # Find the line number to break inside main().
3358c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen        self.line = line_number(self.source, '// Set break point at this line.')
3458c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen        # Build dictionary to have unique executable names for each test method.
3558c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen        self.exe_name = self.testMethodName
3658c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen        self.d = {'C_SOURCES': self.source, 'EXE': self.exe_name}
3758c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen
3858c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen    def hello_watchpoint(self):
3958c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen        """Test a simple sequence of watchpoint creation and watchpoint hit."""
4058c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen        exe = os.path.join(os.getcwd(), self.exe_name)
4158c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
4258c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen
4358c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen        # Add a breakpoint to set a watchpoint when stopped on the breakpoint.
4458c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen        self.expect("breakpoint set -l %d" % self.line, BREAKPOINT_CREATED,
4558c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen            startstr = "Breakpoint created: 1: file ='%s', line = %d, locations = 1" %
4658c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen                       (self.source, self.line))
4758c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen
4858c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen        # Run the program.
4958c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen        self.runCmd("run", RUN_SUCCEEDED)
5058c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen
5158c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen        # We should be stopped again due to the breakpoint.
5258c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen        # The stop reason of the thread should be breakpoint.
5358c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
5458c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen            substrs = ['stopped',
5558c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen                       'stop reason = breakpoint'])
5658c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen
5758c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen        # Now let's set a write-type watchpoint for 'global'.
5858c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen        # There should be only one watchpoint hit (see main.c).
5958c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen        self.expect("frame variable -w write -g -L global", WATCHPOINT_CREATED,
6058c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen            substrs = ['Watchpoint created', 'size = 4', 'type = w'])
6158c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen
6258c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen        self.runCmd("process continue")
6358c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen
6458c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen        # We should be stopped again due to the watchpoint (write type), but
6558c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen        # only once.  The stop reason of the thread should be watchpoint.
6658c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen        self.expect("thread list", STOPPED_DUE_TO_WATCHPOINT,
6758c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen            substrs = ['stopped',
6858c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen                       'stop reason = watchpoint'])
6958c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen
7058c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen        self.runCmd("process continue")
7158c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen        # Don't expect the read of 'global' to trigger a stop exception.
7258c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen        # The process status should be 'exited'.
7358c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen        self.expect("process status",
7458c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen            substrs = ['exited'])
7558c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen
7658c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen
7758c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chenif __name__ == '__main__':
7858c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen    import atexit
7958c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen    lldb.SBDebugger.Initialize()
8058c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen    atexit.register(lambda: lldb.SBDebugger.Terminate())
8158c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen    unittest2.main()
82