1"""
2Test that a variable watchpoint should only hit when in scope.
3"""
4
5import os, time
6import unittest2
7import lldb
8from lldbtest import *
9import lldbutil
10
11class WatchedVariableHitWhenInScopeTestCase(TestBase):
12
13    mydir = os.path.join("functionalities", "watchpoint", "variable_out_of_scope")
14
15    #
16    # This test depends on not tracking watchpoint expression hits if we have
17    # left the watchpoint scope.  We will provide such an ability at some point
18    # but the way this was done was incorrect, and it is unclear that for the
19    # most part that's not what folks mostly want, so we have to provide a
20    # clearer API to express this.
21    #
22
23    @unittest2.expectedFailure
24    @dsym_test
25    def test_watched_var_should_only_hit_when_in_scope_with_dsym(self):
26        """Test that a variable watchpoint should only hit when in scope."""
27        self.buildDsym(dictionary=self.d)
28        self.setTearDownCleanup(dictionary=self.d)
29        self.watched_var()
30
31    @unittest2.expectedFailure
32    @dwarf_test
33    def test_watched_var_should_only_hit_when_in_scope_with_dwarf(self):
34        """Test that a variable watchpoint should only hit when in scope."""
35        self.buildDwarf(dictionary=self.d)
36        self.setTearDownCleanup(dictionary=self.d)
37        self.watched_var()
38
39    def setUp(self):
40        # Call super's setUp().
41        TestBase.setUp(self)
42        # Our simple source filename.
43        self.source = 'main.c'
44        self.exe_name = self.testMethodName
45        self.d = {'C_SOURCES': self.source, 'EXE': self.exe_name}
46
47    def watched_var(self):
48        """Test a simple sequence of watchpoint creation and watchpoint hit."""
49        exe = os.path.join(os.getcwd(), self.exe_name)
50        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
51
52        # Add a breakpoint to set a watchpoint when stopped in main.
53        lldbutil.run_break_set_by_symbol (self, "main", num_expected_locations=-1)
54
55        # Run the program.
56        self.runCmd("run", RUN_SUCCEEDED)
57
58        # We should be stopped again due to the breakpoint.
59        # The stop reason of the thread should be breakpoint.
60        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
61            substrs = ['stopped',
62                       'stop reason = breakpoint'])
63
64        # Now let's set a watchpoint for 'c.a'.
65        # There should be only one watchpoint hit (see main.c).
66        self.expect("watchpoint set variable c.a", WATCHPOINT_CREATED,
67            substrs = ['Watchpoint created', 'size = 4', 'type = w'])
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
94if __name__ == '__main__':
95    import atexit
96    lldb.SBDebugger.Initialize()
97    atexit.register(lambda: lldb.SBDebugger.Terminate())
98    unittest2.main()
99