1"""
2Test watchpoint modify command to set condition on a watchpoint.
3"""
4
5import os, time
6import unittest2
7import lldb
8from lldbtest import *
9import lldbutil
10
11class WatchpointConditionCmdTestCase(TestBase):
12
13    mydir = os.path.join("functionalities", "watchpoint", "watchpoint_commands", "condition")
14
15    def setUp(self):
16        # Call super's setUp().
17        TestBase.setUp(self)
18        # Our simple source filename.
19        self.source = 'main.cpp'
20        # Find the line number to break inside main().
21        self.line = line_number(self.source, '// Set break point at this line.')
22        # And the watchpoint variable declaration line number.
23        self.decl = line_number(self.source, '// Watchpoint variable declaration.')
24        # Build dictionary to have unique executable names for each test method.
25        self.exe_name = self.testMethodName
26        self.d = {'CXX_SOURCES': self.source, 'EXE': self.exe_name}
27
28    @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
29    @dsym_test
30    def test_watchpoint_cond_with_dsym(self):
31        """Test watchpoint condition."""
32        self.buildDsym(dictionary=self.d)
33        self.setTearDownCleanup(dictionary=self.d)
34        self.watchpoint_condition()
35
36    @expectedFailureFreeBSD('llvm.org/pr16706') # Watchpoints fail on FreeBSD
37    @dwarf_test
38    def test_watchpoint_cond_with_dwarf(self):
39        """Test watchpoint condition."""
40        self.buildDwarf(dictionary=self.d)
41        self.setTearDownCleanup(dictionary=self.d)
42        self.watchpoint_condition()
43
44    def watchpoint_condition(self):
45        """Do watchpoint condition 'global==5'."""
46        exe = os.path.join(os.getcwd(), self.exe_name)
47        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
48
49        # Add a breakpoint to set a watchpoint when stopped on the breakpoint.
50        lldbutil.run_break_set_by_file_and_line (self, None, self.line, num_expected_locations=1)
51
52        # Run the program.
53        self.runCmd("run", RUN_SUCCEEDED)
54
55        # We should be stopped again due to the breakpoint.
56        # The stop reason of the thread should be breakpoint.
57        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
58            substrs = ['stopped',
59                       'stop reason = breakpoint'])
60
61        # Now let's set a write-type watchpoint for 'global'.
62        # With a condition of 'global==5'.
63        self.expect("watchpoint set variable -w write global", WATCHPOINT_CREATED,
64            substrs = ['Watchpoint created', 'size = 4', 'type = w',
65                       '%s:%d' % (self.source, self.decl)])
66
67        self.runCmd("watchpoint modify -c 'global==5'")
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', 'global==5'])
73
74        self.runCmd("process continue")
75
76        # We should be stopped again due to the watchpoint (write type).
77        # The stop reason of the thread should be watchpoint.
78        self.expect("thread backtrace", STOPPED_DUE_TO_WATCHPOINT,
79            substrs = ['stop reason = watchpoint'])
80        self.expect("frame variable --show-globals global",
81            substrs = ['(int32_t)', 'global = 5'])
82
83        # Use the '-v' option to do verbose listing of the watchpoint.
84        # The hit count should now be 2.
85        self.expect("watchpoint list -v",
86            substrs = ['hit_count = 5'])
87
88
89if __name__ == '__main__':
90    import atexit
91    lldb.SBDebugger.Initialize()
92    atexit.register(lambda: lldb.SBDebugger.Terminate())
93    unittest2.main()
94