1"""
2Test require hardware breakpoints.
3
4Some of these tests require a target that does not have hardware breakpoints.
5So that we can check we fail when required to use them.
6"""
7
8
9import lldb
10from lldbsuite.test.decorators import *
11from lldbsuite.test.lldbtest import *
12from lldbsuite.test import lldbutil
13
14from functionalities.breakpoint.hardware_breakpoints.base import *
15
16
17class BreakpointLocationsTestCase(HardwareBreakpointTestBase):
18    def test_breakpoint(self):
19        """Test regular breakpoints when hardware breakpoints are required."""
20        self.build()
21        exe = self.getBuildArtifact("a.out")
22        target = self.dbg.CreateTarget(exe)
23
24        self.runCmd("settings set target.require-hardware-breakpoint true")
25
26        breakpoint = target.BreakpointCreateByLocation("main.c", 1)
27        self.assertTrue(breakpoint.IsHardware())
28
29    @skipTestIfFn(HardwareBreakpointTestBase.supports_hw_breakpoints)
30    def test_step_range(self):
31        """Test stepping when hardware breakpoints are required."""
32        self.build()
33
34        _, _, thread, _ = lldbutil.run_to_line_breakpoint(
35            self, lldb.SBFileSpec("main.c"), 1
36        )
37
38        self.runCmd("settings set target.require-hardware-breakpoint true")
39
40        # Ensure we fail in the interpreter.
41        self.expect("thread step-in")
42        self.expect("thread step-in", error=True)
43
44        # Ensure we fail when stepping through the API.
45        error = lldb.SBError()
46        thread.StepInto("", 4, error)
47        self.assertTrue(error.Fail())
48        self.assertIn(
49            "Could not create hardware breakpoint for thread plan", error.GetCString()
50        )
51
52    @skipTestIfFn(HardwareBreakpointTestBase.supports_hw_breakpoints)
53    def test_step_out(self):
54        """Test stepping out when hardware breakpoints are required."""
55        self.build()
56
57        _, _, thread, _ = lldbutil.run_to_line_breakpoint(
58            self, lldb.SBFileSpec("main.c"), 1
59        )
60
61        self.runCmd("settings set target.require-hardware-breakpoint true")
62
63        # Ensure this fails in the command interpreter.
64        self.expect("thread step-out", error=True)
65
66        # Ensure we fail when stepping through the API.
67        error = lldb.SBError()
68        thread.StepOut(error)
69        self.assertTrue(error.Fail())
70        self.assertIn(
71            "Could not create hardware breakpoint for thread plan", error.GetCString()
72        )
73
74    @skipTestIfFn(HardwareBreakpointTestBase.supports_hw_breakpoints)
75    def test_step_over(self):
76        """Test stepping over when hardware breakpoints are required."""
77        self.build()
78
79        _, _, thread, _ = lldbutil.run_to_line_breakpoint(
80            self, lldb.SBFileSpec("main.c"), 7
81        )
82
83        self.runCmd("settings set target.require-hardware-breakpoint true")
84
85        # Step over doesn't fail immediately but fails later on.
86        self.expect(
87            "thread step-over",
88            error=True,
89            substrs=["error: Could not create hardware breakpoint for thread plan."],
90        )
91
92    # Was reported to sometimes pass on certain hardware.
93    @skipIf(oslist=["linux"], archs=["arm"])
94    @skipTestIfFn(HardwareBreakpointTestBase.supports_hw_breakpoints)
95    def test_step_until(self):
96        """Test stepping until when hardware breakpoints are required."""
97        self.build()
98
99        _, _, thread, _ = lldbutil.run_to_line_breakpoint(
100            self, lldb.SBFileSpec("main.c"), 7
101        )
102
103        self.runCmd("settings set target.require-hardware-breakpoint true")
104
105        self.expect("thread until 5", error=True)
106
107        # Ensure we fail when stepping through the API.
108        error = thread.StepOverUntil(lldb.SBFrame(), lldb.SBFileSpec(), 5)
109        self.assertTrue(error.Fail())
110        self.assertIn(
111            "Could not create hardware breakpoint for thread plan", error.GetCString()
112        )
113