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