1"""Test stepping over watchpoints."""
2
3
4
5import lldb
6from lldbsuite.test.decorators import *
7from lldbsuite.test.lldbtest import *
8from lldbsuite.test import lldbutil
9
10
11class TestStepOverWatchpoint(TestBase):
12
13    mydir = TestBase.compute_mydir(__file__)
14    NO_DEBUG_INFO_TESTCASE = True
15
16    @expectedFailureAll(
17        oslist=["freebsd", "linux"],
18        archs=[
19            'aarch64',
20            'arm'],
21        bugnumber="llvm.org/pr26031")
22    # Read-write watchpoints not supported on SystemZ
23    @expectedFailureAll(archs=['s390x'])
24    @expectedFailureAll(
25        oslist=["ios", "watchos", "tvos", "bridgeos", "macosx"],
26        archs=['aarch64', 'arm'],
27        bugnumber="<rdar://problem/34027183>")
28    @add_test_categories(["basic_process"])
29    def test(self):
30        """Test stepping over watchpoints."""
31        self.build()
32        target = self.createTestTarget()
33
34        lldbutil.run_break_set_by_symbol(self, 'main')
35
36        process = target.LaunchSimple(None, None,
37                                      self.get_process_working_directory())
38        self.assertTrue(process.IsValid(), PROCESS_IS_VALID)
39        self.assertState(process.GetState(), lldb.eStateStopped,
40                         PROCESS_STOPPED)
41
42        thread = lldbutil.get_stopped_thread(process,
43                                             lldb.eStopReasonBreakpoint)
44        self.assertTrue(thread.IsValid(), "Failed to get thread.")
45
46        frame = thread.GetFrameAtIndex(0)
47        self.assertTrue(frame.IsValid(), "Failed to get frame.")
48
49        read_value = frame.FindValue('g_watch_me_read',
50                                     lldb.eValueTypeVariableGlobal)
51        self.assertTrue(read_value.IsValid(), "Failed to find read value.")
52
53        error = lldb.SBError()
54
55        # resolve_location=True, read=True, write=False
56        read_watchpoint = read_value.Watch(True, True, False, error)
57        self.assertSuccess(error, "Error while setting watchpoint")
58        self.assertTrue(read_watchpoint, "Failed to set read watchpoint.")
59
60        thread.StepOver()
61        self.assertEquals(thread.GetStopReason(), lldb.eStopReasonWatchpoint,
62                        STOPPED_DUE_TO_WATCHPOINT)
63        self.assertEquals(thread.GetStopDescription(20), 'watchpoint 1')
64
65        process.Continue()
66        self.assertState(process.GetState(), lldb.eStateStopped,
67                         PROCESS_STOPPED)
68        self.assertEquals(thread.GetStopDescription(20), 'step over')
69
70        self.step_inst_for_watchpoint(1)
71
72        write_value = frame.FindValue('g_watch_me_write',
73                                      lldb.eValueTypeVariableGlobal)
74        self.assertTrue(write_value, "Failed to find write value.")
75
76        # Most of the MIPS boards provide only one H/W watchpoints, and S/W
77        # watchpoints are not supported yet
78        arch = self.getArchitecture()
79        if re.match("^mips", arch) or re.match("powerpc64le", arch):
80            self.runCmd("watchpoint delete 1")
81
82        # resolve_location=True, read=False, write=True
83        write_watchpoint = write_value.Watch(True, False, True, error)
84        self.assertTrue(write_watchpoint, "Failed to set write watchpoint.")
85        self.assertSuccess(error, "Error while setting watchpoint")
86
87        thread.StepOver()
88        self.assertEquals(thread.GetStopReason(), lldb.eStopReasonWatchpoint,
89                        STOPPED_DUE_TO_WATCHPOINT)
90        self.assertEquals(thread.GetStopDescription(20), 'watchpoint 2')
91
92        process.Continue()
93        self.assertState(process.GetState(), lldb.eStateStopped,
94                         PROCESS_STOPPED)
95        self.assertEquals(thread.GetStopDescription(20), 'step over')
96
97        self.step_inst_for_watchpoint(2)
98
99    def step_inst_for_watchpoint(self, wp_id):
100        watchpoint_hit = False
101        current_line = self.frame().GetLineEntry().GetLine()
102        while self.frame().GetLineEntry().GetLine() == current_line:
103            self.thread().StepInstruction(False)  # step_over=False
104            stop_reason = self.thread().GetStopReason()
105            if stop_reason == lldb.eStopReasonWatchpoint:
106                self.assertFalse(watchpoint_hit, "Watchpoint already hit.")
107                expected_stop_desc = "watchpoint %d" % wp_id
108                actual_stop_desc = self.thread().GetStopDescription(20)
109                self.assertEquals(actual_stop_desc, expected_stop_desc,
110                                "Watchpoint ID didn't match.")
111                watchpoint_hit = True
112            else:
113                self.assertEquals(stop_reason, lldb.eStopReasonPlanComplete,
114                                STOPPED_DUE_TO_STEP_IN)
115        self.assertTrue(watchpoint_hit, "Watchpoint never hit.")
116