1"""
2Test watchpoint size cases (1-byte, 2-byte, 4-byte).
3Make sure we can watch all bytes, words or double words individually
4when they are packed in a 8-byte region.
5
6"""
7
8
9import lldb
10from lldbsuite.test.decorators import *
11from lldbsuite.test.lldbtest import *
12from lldbsuite.test import lldbutil
13
14
15class WatchpointSizeTestCase(TestBase):
16    NO_DEBUG_INFO_TESTCASE = True
17
18    def setUp(self):
19        # Call super's setUp().
20        TestBase.setUp(self)
21
22        # Source filename.
23        self.source = 'main.c'
24
25        # Output filename.
26        self.exe_name = self.getBuildArtifact("a.out")
27        self.d = {'C_SOURCES': self.source, 'EXE': self.exe_name}
28
29    # Read-write watchpoints not supported on SystemZ
30    @expectedFailureAll(archs=['s390x'])
31    def test_byte_size_watchpoints_with_byte_selection(self):
32        """Test to selectively watch different bytes in a 8-byte array."""
33        self.run_watchpoint_size_test('byteArray', 8, '1')
34
35    # Read-write watchpoints not supported on SystemZ
36    @expectedFailureAll(archs=['s390x'])
37    def test_two_byte_watchpoints_with_word_selection(self):
38        """Test to selectively watch different words in an 8-byte word array."""
39        self.run_watchpoint_size_test('wordArray', 4, '2')
40
41    # Read-write watchpoints not supported on SystemZ
42    @expectedFailureAll(archs=['s390x'])
43    def test_four_byte_watchpoints_with_dword_selection(self):
44        """Test to selectively watch two double words in an 8-byte dword array."""
45        self.run_watchpoint_size_test('dwordArray', 2, '4')
46
47    def run_watchpoint_size_test(self, arrayName, array_size, watchsize):
48        self.build(dictionary=self.d)
49        self.setTearDownCleanup(dictionary=self.d)
50
51        exe = self.getBuildArtifact(self.exe_name)
52        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
53
54        # Detect line number after which we are going to increment arrayName.
55        loc_line = line_number('main.c', '// About to write ' + arrayName)
56
57        # Set a breakpoint on the line detected above.
58        lldbutil.run_break_set_by_file_and_line(
59            self, "main.c", loc_line, num_expected_locations=1, loc_exact=True)
60
61        # Run the program.
62        self.runCmd("run", RUN_SUCCEEDED)
63
64        for i in range(array_size):
65            # We should be stopped again due to the breakpoint.
66            # The stop reason of the thread should be breakpoint.
67            self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
68                        substrs=['stopped', 'stop reason = breakpoint'])
69
70            # Set a read_write type watchpoint arrayName
71            watch_loc = arrayName + "[" + str(i) + "]"
72            self.expect(
73                "watchpoint set variable -w read_write " +
74                watch_loc,
75                WATCHPOINT_CREATED,
76                substrs=[
77                    'Watchpoint created',
78                    'size = ' +
79                    watchsize,
80                    'type = rw'])
81
82            # Use the '-v' option to do verbose listing of the watchpoint.
83            # The hit count should be 0 initially.
84            self.expect("watchpoint list -v", substrs=['hit_count = 0'])
85
86            self.runCmd("process continue")
87
88            # We should be stopped due to the watchpoint.
89            # The stop reason of the thread should be watchpoint.
90            self.expect("thread list", STOPPED_DUE_TO_WATCHPOINT,
91                        substrs=['stopped', 'stop reason = watchpoint'])
92
93            # Use the '-v' option to do verbose listing of the watchpoint.
94            # The hit count should now be 1.
95            self.expect("watchpoint list -v",
96                        substrs=['hit_count = 1'])
97
98            self.runCmd("process continue")
99
100            # We should be stopped due to the watchpoint.
101            # The stop reason of the thread should be watchpoint.
102            self.expect("thread list", STOPPED_DUE_TO_WATCHPOINT,
103                        substrs=['stopped', 'stop reason = watchpoint'])
104
105            # Use the '-v' option to do verbose listing of the watchpoint.
106            # The hit count should now be 1.
107            # Verify hit_count has been updated after value has been read.
108            self.expect("watchpoint list -v",
109                        substrs=['hit_count = 2'])
110
111            # Delete the watchpoint immediately, but set auto-confirm to true
112            # first.
113            self.runCmd("settings set auto-confirm true")
114            self.expect(
115                "watchpoint delete",
116                substrs=['All watchpoints removed.'])
117            # Restore the original setting of auto-confirm.
118            self.runCmd("settings clear auto-confirm")
119
120            self.runCmd("process continue")
121