1"""
2Test 'watchpoint command'.
3"""
4
5
6import lldb
7from lldbsuite.test.decorators import *
8from lldbsuite.test.lldbtest import *
9from lldbsuite.test import lldbutil
10
11
12class WatchpointLLDBCommandTestCase(TestBase):
13    NO_DEBUG_INFO_TESTCASE = True
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
25        # method.
26        self.exe_name = "a%d.out" % self.test_number
27        self.d = {"CXX_SOURCES": self.source, "EXE": self.exe_name}
28
29    def test_watchpoint_command(self):
30        """Test 'watchpoint command'."""
31        self.build(dictionary=self.d)
32        self.setTearDownCleanup(dictionary=self.d)
33
34        exe = self.getBuildArtifact(self.exe_name)
35        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
36
37        # Add a breakpoint to set a watchpoint when stopped on the breakpoint.
38        lldbutil.run_break_set_by_file_and_line(
39            self, None, self.line, num_expected_locations=1
40        )
41
42        # Run the program.
43        self.runCmd("run", RUN_SUCCEEDED)
44
45        # We should be stopped again due to the breakpoint.
46        # The stop reason of the thread should be breakpoint.
47        self.expect(
48            "thread list",
49            STOPPED_DUE_TO_BREAKPOINT,
50            substrs=["stopped", "stop reason = breakpoint"],
51        )
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" % (self.source, self.decl),
62            ],
63        )
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", substrs=["expr -- cookie = 777"])
69
70        # Use the '-v' option to do verbose listing of the watchpoint.
71        # The hit count should be 0 initially.
72        self.expect("watchpoint list -v", substrs=["hit_count = 0"])
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(
79            "thread backtrace",
80            STOPPED_DUE_TO_WATCHPOINT,
81            substrs=["stop reason = watchpoint"],
82        )
83
84        # Check that the watchpoint snapshoting mechanism is working.
85        self.expect(
86            "watchpoint list -v",
87            substrs=[
88                "old value: 0",
89                "new value: 1",
90                "hit_count = 1",
91                "ignore_count = 0",
92            ],
93        )
94
95        # The watchpoint command "forced" our global variable 'cookie' to
96        # become 777.
97        self.expect(
98            "frame variable --show-globals cookie",
99            substrs=["(int32_t)", "cookie = 777"],
100        )
101
102    def test_watchpoint_command_can_disable_a_watchpoint(self):
103        """Test that 'watchpoint command' action can disable a watchpoint after it is triggered."""
104        self.build(dictionary=self.d)
105        self.setTearDownCleanup(dictionary=self.d)
106
107        exe = self.getBuildArtifact(self.exe_name)
108        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
109
110        # Add a breakpoint to set a watchpoint when stopped on the breakpoint.
111        lldbutil.run_break_set_by_file_and_line(
112            self, None, self.line, num_expected_locations=1
113        )
114
115        # Run the program.
116        self.runCmd("run", RUN_SUCCEEDED)
117
118        # We should be stopped again due to the breakpoint.
119        # The stop reason of the thread should be breakpoint.
120        self.expect(
121            "thread list",
122            STOPPED_DUE_TO_BREAKPOINT,
123            substrs=["stopped", "stop reason = breakpoint"],
124        )
125
126        # Now let's set a write-type watchpoint for 'global'.
127        self.expect(
128            "watchpoint set variable -w write global",
129            WATCHPOINT_CREATED,
130            substrs=[
131                "Watchpoint created",
132                "size = 4",
133                "type = w",
134                "%s:%d" % (self.source, self.decl),
135            ],
136        )
137
138        self.runCmd('watchpoint command add 1 -o "watchpoint disable 1"')
139
140        # List the watchpoint command we just added.
141        self.expect("watchpoint command list 1", substrs=["watchpoint disable 1"])
142
143        # Use the '-v' option to do verbose listing of the watchpoint.
144        # The hit count should be 0 initially.
145        self.expect("watchpoint list -v", substrs=["hit_count = 0"])
146
147        self.runCmd("process continue")
148
149        # We should be stopped again due to the watchpoint (write type).
150        # The stop reason of the thread should be watchpoint.
151        self.expect(
152            "thread backtrace",
153            STOPPED_DUE_TO_WATCHPOINT,
154            substrs=["stop reason = watchpoint"],
155        )
156
157        # Check that the watchpoint has been disabled.
158        self.expect("watchpoint list -v", substrs=["disabled"])
159
160        self.runCmd("process continue")
161
162        # There should be no more watchpoint hit and the process status should
163        # be 'exited'.
164        self.expect("process status", substrs=["exited"])
165