1"""
2Test 'watchpoint command'.
3"""
4
5
6
7import lldb
8from lldbsuite.test.decorators import *
9from lldbsuite.test.lldbtest import *
10from lldbsuite.test import lldbutil
11
12
13class WatchpointLLDBCommandTestCase(TestBase):
14    NO_DEBUG_INFO_TESTCASE = True
15
16    def setUp(self):
17        # Call super's setUp().
18        TestBase.setUp(self)
19        # Our simple source filename.
20        self.source = 'main.cpp'
21        # Find the line number to break inside main().
22        self.line = line_number(
23            self.source, '// Set break point at this line.')
24        # And the watchpoint variable declaration line number.
25        self.decl = line_number(self.source,
26                                '// Watchpoint variable declaration.')
27        # Build dictionary to have unique executable names for each test
28        # method.
29        self.exe_name = 'a%d.out' % self.test_number
30        self.d = {'CXX_SOURCES': self.source, 'EXE': self.exe_name}
31
32    def test_watchpoint_command(self):
33        """Test 'watchpoint command'."""
34        self.build(dictionary=self.d)
35        self.setTearDownCleanup(dictionary=self.d)
36
37        exe = self.getBuildArtifact(self.exe_name)
38        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
39
40        # Add a breakpoint to set a watchpoint when stopped on the breakpoint.
41        lldbutil.run_break_set_by_file_and_line(
42            self, None, self.line, num_expected_locations=1)
43
44        # Run the program.
45        self.runCmd("run", RUN_SUCCEEDED)
46
47        # We should be stopped again due to the breakpoint.
48        # The stop reason of the thread should be breakpoint.
49        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
50                    substrs=['stopped',
51                             'stop reason = breakpoint'])
52
53        # Now let's set a write-type watchpoint for 'global'.
54        self.expect(
55            "watchpoint set variable -w write global",
56            WATCHPOINT_CREATED,
57            substrs=[
58                'Watchpoint created',
59                'size = 4',
60                'type = w',
61                '%s:%d' %
62                (self.source,
63                 self.decl)])
64
65        self.runCmd('watchpoint command add 1 -o "expr -- cookie = 777"')
66
67        # List the watchpoint command we just added.
68        self.expect("watchpoint command list 1",
69                    substrs=['expr -- cookie = 777'])
70
71        # Use the '-v' option to do verbose listing of the watchpoint.
72        # The hit count should be 0 initially.
73        self.expect("watchpoint list -v",
74                    substrs=['hit_count = 0'])
75
76        self.runCmd("process continue")
77
78        # We should be stopped again due to the watchpoint (write type).
79        # The stop reason of the thread should be watchpoint.
80        self.expect("thread backtrace", STOPPED_DUE_TO_WATCHPOINT,
81                    substrs=['stop reason = watchpoint'])
82
83        # Check that the watchpoint snapshoting mechanism is working.
84        self.expect(
85            "watchpoint list -v",
86            substrs=[
87                'old value: 0', 'new value: 1', 'hit_count = 1',
88                'ignore_count = 0'
89            ])
90
91        # The watchpoint command "forced" our global variable 'cookie' to
92        # become 777.
93        self.expect("frame variable --show-globals cookie",
94                    substrs=['(int32_t)', 'cookie = 777'])
95
96    def test_watchpoint_command_can_disable_a_watchpoint(self):
97        """Test that 'watchpoint command' action can disable a watchpoint after it is triggered."""
98        self.build(dictionary=self.d)
99        self.setTearDownCleanup(dictionary=self.d)
100
101        exe = self.getBuildArtifact(self.exe_name)
102        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
103
104        # Add a breakpoint to set a watchpoint when stopped on the breakpoint.
105        lldbutil.run_break_set_by_file_and_line(
106            self, None, self.line, num_expected_locations=1)
107
108        # Run the program.
109        self.runCmd("run", RUN_SUCCEEDED)
110
111        # We should be stopped again due to the breakpoint.
112        # The stop reason of the thread should be breakpoint.
113        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
114                    substrs=['stopped',
115                             'stop reason = breakpoint'])
116
117        # Now let's set a write-type watchpoint for 'global'.
118        self.expect(
119            "watchpoint set variable -w write global",
120            WATCHPOINT_CREATED,
121            substrs=[
122                'Watchpoint created',
123                'size = 4',
124                'type = w',
125                '%s:%d' %
126                (self.source,
127                 self.decl)])
128
129        self.runCmd('watchpoint command add 1 -o "watchpoint disable 1"')
130
131        # List the watchpoint command we just added.
132        self.expect("watchpoint command list 1",
133                    substrs=['watchpoint disable 1'])
134
135        # Use the '-v' option to do verbose listing of the watchpoint.
136        # The hit count should be 0 initially.
137        self.expect("watchpoint list -v",
138                    substrs=['hit_count = 0'])
139
140        self.runCmd("process continue")
141
142        # We should be stopped again due to the watchpoint (write type).
143        # The stop reason of the thread should be watchpoint.
144        self.expect("thread backtrace", STOPPED_DUE_TO_WATCHPOINT,
145                    substrs=['stop reason = watchpoint'])
146
147        # Check that the watchpoint has been disabled.
148        self.expect("watchpoint list -v",
149                    substrs=['disabled'])
150
151        self.runCmd("process continue")
152
153        # There should be no more watchpoint hit and the process status should
154        # be 'exited'.
155        self.expect("process status",
156                    substrs=['exited'])
157