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.assertEquals(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.assertTrue(error.Success(),
58                        "Error while setting watchpoint: %s" %
59                        error.GetCString())
60        self.assertTrue(read_watchpoint, "Failed to set read watchpoint.")
61
62        thread.StepOver()
63        self.assertEquals(thread.GetStopReason(), lldb.eStopReasonWatchpoint,
64                        STOPPED_DUE_TO_WATCHPOINT)
65        self.assertEquals(thread.GetStopDescription(20), 'watchpoint 1')
66
67        process.Continue()
68        self.assertEquals(process.GetState(), lldb.eStateStopped,
69                        PROCESS_STOPPED)
70        self.assertEquals(thread.GetStopDescription(20), 'step over')
71
72        self.step_inst_for_watchpoint(1)
73
74        write_value = frame.FindValue('g_watch_me_write',
75                                      lldb.eValueTypeVariableGlobal)
76        self.assertTrue(write_value, "Failed to find write value.")
77
78        # Most of the MIPS boards provide only one H/W watchpoints, and S/W
79        # watchpoints are not supported yet
80        arch = self.getArchitecture()
81        if re.match("^mips", arch) or re.match("powerpc64le", arch):
82            self.runCmd("watchpoint delete 1")
83
84        # resolve_location=True, read=False, write=True
85        write_watchpoint = write_value.Watch(True, False, True, error)
86        self.assertTrue(write_watchpoint, "Failed to set write watchpoint.")
87        self.assertTrue(error.Success(),
88                        "Error while setting watchpoint: %s" %
89                        error.GetCString())
90
91        thread.StepOver()
92        self.assertEquals(thread.GetStopReason(), lldb.eStopReasonWatchpoint,
93                        STOPPED_DUE_TO_WATCHPOINT)
94        self.assertEquals(thread.GetStopDescription(20), 'watchpoint 2')
95
96        process.Continue()
97        self.assertEquals(process.GetState(), lldb.eStateStopped,
98                        PROCESS_STOPPED)
99        self.assertEquals(thread.GetStopDescription(20), 'step over')
100
101        self.step_inst_for_watchpoint(2)
102
103    def step_inst_for_watchpoint(self, wp_id):
104        watchpoint_hit = False
105        current_line = self.frame().GetLineEntry().GetLine()
106        while self.frame().GetLineEntry().GetLine() == current_line:
107            self.thread().StepInstruction(False)  # step_over=False
108            stop_reason = self.thread().GetStopReason()
109            if stop_reason == lldb.eStopReasonWatchpoint:
110                self.assertFalse(watchpoint_hit, "Watchpoint already hit.")
111                expected_stop_desc = "watchpoint %d" % wp_id
112                actual_stop_desc = self.thread().GetStopDescription(20)
113                self.assertEquals(actual_stop_desc, expected_stop_desc,
114                                "Watchpoint ID didn't match.")
115                watchpoint_hit = True
116            else:
117                self.assertEquals(stop_reason, lldb.eStopReasonPlanComplete,
118                                STOPPED_DUE_TO_STEP_IN)
119        self.assertTrue(watchpoint_hit, "Watchpoint never hit.")
120