11e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen"""
21e8a14af674e2761f257440c46c8aca34f53b951Johnny ChenTest watchpoint modify command to set condition on a watchpoint.
31e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen"""
41e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen
51e8a14af674e2761f257440c46c8aca34f53b951Johnny Chenimport os, time
61e8a14af674e2761f257440c46c8aca34f53b951Johnny Chenimport unittest2
71e8a14af674e2761f257440c46c8aca34f53b951Johnny Chenimport lldb
81e8a14af674e2761f257440c46c8aca34f53b951Johnny Chenfrom lldbtest import *
9431d839a33e9a274e705f7a268a1c9de2ffc2da2Jim Inghamimport lldbutil
101e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen
111e8a14af674e2761f257440c46c8aca34f53b951Johnny Chenclass WatchpointConditionCmdTestCase(TestBase):
121e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen
131e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen    mydir = os.path.join("functionalities", "watchpoint", "watchpoint_commands", "condition")
141e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen
151e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen    def setUp(self):
161e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen        # Call super's setUp().
171e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen        TestBase.setUp(self)
181e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen        # Our simple source filename.
191e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen        self.source = 'main.cpp'
201e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen        # Find the line number to break inside main().
211e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen        self.line = line_number(self.source, '// Set break point at this line.')
221e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen        # And the watchpoint variable declaration line number.
231e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen        self.decl = line_number(self.source, '// Watchpoint variable declaration.')
241e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen        # Build dictionary to have unique executable names for each test method.
251e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen        self.exe_name = self.testMethodName
261e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen        self.d = {'CXX_SOURCES': self.source, 'EXE': self.exe_name}
271e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen
281e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen    @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
2921b1984e161b0cadee331d32bfd721eccfdf4b1fJohnny Chen    @dsym_test
301e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen    def test_watchpoint_cond_with_dsym(self):
311e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen        """Test watchpoint condition."""
321e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen        self.buildDsym(dictionary=self.d)
331e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen        self.setTearDownCleanup(dictionary=self.d)
341e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen        self.watchpoint_condition()
351e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen
363bf526ac0f51dda6f63be3421cd7de68720dd656Ed Maste    @expectedFailureFreeBSD('llvm.org/pr16706') # Watchpoints fail on FreeBSD
3721b1984e161b0cadee331d32bfd721eccfdf4b1fJohnny Chen    @dwarf_test
381e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen    def test_watchpoint_cond_with_dwarf(self):
391e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen        """Test watchpoint condition."""
401e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen        self.buildDwarf(dictionary=self.d)
411e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen        self.setTearDownCleanup(dictionary=self.d)
421e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen        self.watchpoint_condition()
431e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen
441e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen    def watchpoint_condition(self):
451e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen        """Do watchpoint condition 'global==5'."""
461e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen        exe = os.path.join(os.getcwd(), self.exe_name)
471e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
481e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen
491e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen        # Add a breakpoint to set a watchpoint when stopped on the breakpoint.
50431d839a33e9a274e705f7a268a1c9de2ffc2da2Jim Ingham        lldbutil.run_break_set_by_file_and_line (self, None, self.line, num_expected_locations=1)
511e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen
521e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen        # Run the program.
531e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen        self.runCmd("run", RUN_SUCCEEDED)
541e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen
551e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen        # We should be stopped again due to the breakpoint.
561e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen        # The stop reason of the thread should be breakpoint.
571e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
581e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen            substrs = ['stopped',
591e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen                       'stop reason = breakpoint'])
601e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen
611e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen        # Now let's set a write-type watchpoint for 'global'.
621e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen        # With a condition of 'global==5'.
632b05e2937e4ff82951a0e3b288acd15d2e278ff3Johnny Chen        self.expect("watchpoint set variable -w write global", WATCHPOINT_CREATED,
641e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen            substrs = ['Watchpoint created', 'size = 4', 'type = w',
659351b4ff5bd824856dd9894d2005b29a020dfdd8Johnny Chen                       '%s:%d' % (self.source, self.decl)])
661e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen
671e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen        self.runCmd("watchpoint modify -c 'global==5'")
681e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen
691e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen        # Use the '-v' option to do verbose listing of the watchpoint.
701e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen        # The hit count should be 0 initially.
711e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen        self.expect("watchpoint list -v",
721e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen            substrs = ['hit_count = 0', 'global==5'])
731e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen
741e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen        self.runCmd("process continue")
751e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen
761e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen        # We should be stopped again due to the watchpoint (write type).
771e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen        # The stop reason of the thread should be watchpoint.
781e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen        self.expect("thread backtrace", STOPPED_DUE_TO_WATCHPOINT,
791e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen            substrs = ['stop reason = watchpoint'])
806475c42148a8ea1ca86e5db465db7eca742d897dGreg Clayton        self.expect("frame variable --show-globals global",
811e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen            substrs = ['(int32_t)', 'global = 5'])
821e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen
831e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen        # Use the '-v' option to do verbose listing of the watchpoint.
841e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen        # The hit count should now be 2.
851e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen        self.expect("watchpoint list -v",
861e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen            substrs = ['hit_count = 5'])
871e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen
881e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen
891e8a14af674e2761f257440c46c8aca34f53b951Johnny Chenif __name__ == '__main__':
901e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen    import atexit
911e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen    lldb.SBDebugger.Initialize()
921e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen    atexit.register(lambda: lldb.SBDebugger.Terminate())
931e8a14af674e2761f257440c46c8aca34f53b951Johnny Chen    unittest2.main()
94