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 *
9431d839a33e9a274e705f7a268a1c9de2ffc2da2Jim Inghamimport lldbutil
1058c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen
1158c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chenclass HelloWatchpointTestCase(TestBase):
1258c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen
13edc4ddbef15572e68a500bfb11ecfeaf60b90894Jim Ingham    def getCategories (self):
14edc4ddbef15572e68a500bfb11ecfeaf60b90894Jim Ingham        return ['basic_process']
15edc4ddbef15572e68a500bfb11ecfeaf60b90894Jim Ingham
1658c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen    mydir = os.path.join("functionalities", "watchpoint", "hello_watchpoint")
1758c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen
18a3ed7d834b0e0c6924ac95629e740682bbcd15baJohnny Chen    @dsym_test
1955a2d5a49d82fcfc0fe72825293f6c04d1970e21Johnny Chen    def test_hello_watchpoint_with_dsym_using_watchpoint_set(self):
2055a2d5a49d82fcfc0fe72825293f6c04d1970e21Johnny Chen        """Test a simple sequence of watchpoint creation and watchpoint hit."""
2155a2d5a49d82fcfc0fe72825293f6c04d1970e21Johnny Chen        self.buildDsym(dictionary=self.d)
2255a2d5a49d82fcfc0fe72825293f6c04d1970e21Johnny Chen        self.setTearDownCleanup(dictionary=self.d)
232b05e2937e4ff82951a0e3b288acd15d2e278ff3Johnny Chen        self.hello_watchpoint()
2455a2d5a49d82fcfc0fe72825293f6c04d1970e21Johnny Chen
253bf526ac0f51dda6f63be3421cd7de68720dd656Ed Maste    @expectedFailureFreeBSD('llvm.org/pr16706') # Watchpoints fail on FreeBSD
26a3ed7d834b0e0c6924ac95629e740682bbcd15baJohnny Chen    @dwarf_test
2755a2d5a49d82fcfc0fe72825293f6c04d1970e21Johnny Chen    def test_hello_watchpoint_with_dwarf_using_watchpoint_set(self):
2855a2d5a49d82fcfc0fe72825293f6c04d1970e21Johnny Chen        """Test a simple sequence of watchpoint creation and watchpoint hit."""
2955a2d5a49d82fcfc0fe72825293f6c04d1970e21Johnny Chen        self.buildDwarf(dictionary=self.d)
3055a2d5a49d82fcfc0fe72825293f6c04d1970e21Johnny Chen        self.setTearDownCleanup(dictionary=self.d)
312b05e2937e4ff82951a0e3b288acd15d2e278ff3Johnny Chen        self.hello_watchpoint()
3255a2d5a49d82fcfc0fe72825293f6c04d1970e21Johnny Chen
3358c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen    def setUp(self):
3458c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen        # Call super's setUp().
3558c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen        TestBase.setUp(self)
3658c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen        # Our simple source filename.
3758c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen        self.source = 'main.c'
3858c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen        # Find the line number to break inside main().
3958c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen        self.line = line_number(self.source, '// Set break point at this line.')
4010b12b3aa8542721fa9485c1d520433de0e0e720Johnny Chen        # And the watchpoint variable declaration line number.
4110b12b3aa8542721fa9485c1d520433de0e0e720Johnny Chen        self.decl = line_number(self.source, '// Watchpoint variable declaration.')
4258c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen        # Build dictionary to have unique executable names for each test method.
4358c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen        self.exe_name = self.testMethodName
4458c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen        self.d = {'C_SOURCES': self.source, 'EXE': self.exe_name}
4558c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen
462b05e2937e4ff82951a0e3b288acd15d2e278ff3Johnny Chen    def hello_watchpoint(self):
4758c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen        """Test a simple sequence of watchpoint creation and watchpoint hit."""
4858c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen        exe = os.path.join(os.getcwd(), self.exe_name)
4958c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
5058c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen
5158c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen        # Add a breakpoint to set a watchpoint when stopped on the breakpoint.
52431d839a33e9a274e705f7a268a1c9de2ffc2da2Jim Ingham        lldbutil.run_break_set_by_file_and_line (self, None, self.line, num_expected_locations=1)
5358c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen
5458c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen        # Run the program.
5558c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen        self.runCmd("run", RUN_SUCCEEDED)
5658c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen
5758c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen        # We should be stopped again due to the breakpoint.
5858c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen        # The stop reason of the thread should be breakpoint.
5958c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
6058c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen            substrs = ['stopped',
6158c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen                       'stop reason = breakpoint'])
6258c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen
6358c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen        # Now let's set a write-type watchpoint for 'global'.
6458c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen        # There should be only one watchpoint hit (see main.c).
652b05e2937e4ff82951a0e3b288acd15d2e278ff3Johnny Chen        self.expect("watchpoint set variable -w write global", WATCHPOINT_CREATED,
662b05e2937e4ff82951a0e3b288acd15d2e278ff3Johnny Chen            substrs = ['Watchpoint created', 'size = 4', 'type = w',
672b05e2937e4ff82951a0e3b288acd15d2e278ff3Johnny Chen                       '%s:%d' % (self.source, self.decl)])
6858c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen
6901acfa76010b8db2e77016c144963c4dd70f1392Johnny Chen        # Use the '-v' option to do verbose listing of the watchpoint.
7001acfa76010b8db2e77016c144963c4dd70f1392Johnny Chen        # The hit count should be 0 initially.
7101acfa76010b8db2e77016c144963c4dd70f1392Johnny Chen        self.expect("watchpoint list -v",
7201acfa76010b8db2e77016c144963c4dd70f1392Johnny Chen            substrs = ['hit_count = 0'])
7301acfa76010b8db2e77016c144963c4dd70f1392Johnny Chen
7458c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen        self.runCmd("process continue")
7558c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen
7658c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen        # We should be stopped again due to the watchpoint (write type), but
7758c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen        # only once.  The stop reason of the thread should be watchpoint.
7858c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen        self.expect("thread list", STOPPED_DUE_TO_WATCHPOINT,
7958c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen            substrs = ['stopped',
8058c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen                       'stop reason = watchpoint'])
8158c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen
8258c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen        self.runCmd("process continue")
8358c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen        # Don't expect the read of 'global' to trigger a stop exception.
8458c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen        # The process status should be 'exited'.
8558c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen        self.expect("process status",
8658c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen            substrs = ['exited'])
8758c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen
8801acfa76010b8db2e77016c144963c4dd70f1392Johnny Chen        # Use the '-v' option to do verbose listing of the watchpoint.
8901acfa76010b8db2e77016c144963c4dd70f1392Johnny Chen        # The hit count should now be 1.
9001acfa76010b8db2e77016c144963c4dd70f1392Johnny Chen        self.expect("watchpoint list -v",
9101acfa76010b8db2e77016c144963c4dd70f1392Johnny Chen            substrs = ['hit_count = 1'])
9201acfa76010b8db2e77016c144963c4dd70f1392Johnny Chen
9358c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen
9458c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chenif __name__ == '__main__':
9558c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen    import atexit
9658c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen    lldb.SBDebugger.Initialize()
9758c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen    atexit.register(lambda: lldb.SBDebugger.Terminate())
9858c66e2c4273c101a82ce02e17cc10d338b825bbJohnny Chen    unittest2.main()
99