1"""
2Test that a variable watchpoint should only hit when in scope.
3"""
4
5
6
7import unittest2
8import lldb
9from lldbsuite.test.lldbtest import *
10import lldbsuite.test.lldbutil as lldbutil
11from lldbsuite.test.decorators import *
12
13
14class WatchedVariableHitWhenInScopeTestCase(TestBase):
15    NO_DEBUG_INFO_TESTCASE = True
16
17    # This test depends on not tracking watchpoint expression hits if we have
18    # left the watchpoint scope.  We will provide such an ability at some point
19    # but the way this was done was incorrect, and it is unclear that for the
20    # most part that's not what folks mostly want, so we have to provide a
21    # clearer API to express this.
22
23    def setUp(self):
24        # Call super's setUp().
25        TestBase.setUp(self)
26        # Our simple source filename.
27        self.source = 'main.c'
28        self.exe_name = self.testMethodName
29        self.d = {'C_SOURCES': self.source, 'EXE': self.exe_name}
30
31    # Test hangs due to a kernel bug, see fdfeff0f in the linux kernel for details
32    @skipIfTargetAndroid(api_levels=list(range(25+1)), archs=["aarch64", "arm"])
33    @skipIf
34    def test_watched_var_should_only_hit_when_in_scope(self):
35        """Test that a variable watchpoint should only hit when in scope."""
36        self.build(dictionary=self.d)
37        self.setTearDownCleanup(dictionary=self.d)
38
39        exe = self.getBuildArtifact(self.exe_name)
40        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
41
42        # Add a breakpoint to set a watchpoint when stopped in main.
43        lldbutil.run_break_set_by_symbol(
44            self, "main", num_expected_locations=-1)
45
46        # Run the program.
47        self.runCmd("run", RUN_SUCCEEDED)
48
49        # We should be stopped again due to the breakpoint.
50        # The stop reason of the thread should be breakpoint.
51        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
52                    substrs=['stopped',
53                             'stop reason = breakpoint'])
54
55        # Now let's set a watchpoint for 'c.a'.
56        # There should be only one watchpoint hit (see main.c).
57        self.expect("watchpoint set variable c.a", WATCHPOINT_CREATED,
58                    substrs=['Watchpoint created', 'size = 4', 'type = w'])
59
60        # Use the '-v' option to do verbose listing of the watchpoint.
61        # The hit count should be 0 initially.
62        self.expect("watchpoint list -v",
63                    substrs=['hit_count = 0'])
64
65        self.runCmd("process continue")
66
67        # We should be stopped again due to the watchpoint (write type), but
68        # only once.  The stop reason of the thread should be watchpoint.
69        self.expect("thread list", STOPPED_DUE_TO_WATCHPOINT,
70                    substrs=['stopped',
71                             'stop reason = watchpoint'])
72
73        self.runCmd("process continue")
74        # Don't expect the read of 'global' to trigger a stop exception.
75        # The process status should be 'exited'.
76        self.expect("process status",
77                    substrs=['exited'])
78
79        # Use the '-v' option to do verbose listing of the watchpoint.
80        # The hit count should now be 1.
81        self.expect("watchpoint list -v",
82                    substrs=['hit_count = 1'])
83