199451b44SJordan Rupprecht"""
299451b44SJordan RupprechtTest that breakpoints do not affect stepping.
399451b44SJordan RupprechtCheck for correct StopReason when stepping to the line with breakpoint
4e9264b74SKazuaki Ishizakiwhich should be eStopReasonBreakpoint in general,
599451b44SJordan Rupprechtand eStopReasonPlanComplete when breakpoint's condition fails.
699451b44SJordan Rupprecht"""
799451b44SJordan Rupprecht
899451b44SJordan Rupprecht
999451b44SJordan Rupprechtimport lldb
1099451b44SJordan Rupprechtfrom lldbsuite.test.decorators import *
1199451b44SJordan Rupprechtfrom lldbsuite.test.lldbtest import *
1299451b44SJordan Rupprechtfrom lldbsuite.test import lldbutil
1399451b44SJordan Rupprecht
1499451b44SJordan Rupprecht
152238dcc3SJonas Devlieghereclass StepOverBreakpointsTestCase(TestBase):
1699451b44SJordan Rupprecht    def setUp(self):
1799451b44SJordan Rupprecht        TestBase.setUp(self)
1899451b44SJordan Rupprecht
1999451b44SJordan Rupprecht        self.build()
2099451b44SJordan Rupprecht        exe = self.getBuildArtifact("a.out")
2199451b44SJordan Rupprecht        src = lldb.SBFileSpec("main.cpp")
2299451b44SJordan Rupprecht
2399451b44SJordan Rupprecht        # Create a target by the debugger.
2499451b44SJordan Rupprecht        self.target = self.dbg.CreateTarget(exe)
2599451b44SJordan Rupprecht        self.assertTrue(self.target, VALID_TARGET)
2699451b44SJordan Rupprecht
2799451b44SJordan Rupprecht        # Setup four breakpoints, two of them with false condition
282238dcc3SJonas Devlieghere        self.line1 = line_number("main.cpp", "breakpoint_1")
292238dcc3SJonas Devlieghere        self.line4 = line_number("main.cpp", "breakpoint_4")
3099451b44SJordan Rupprecht
3199451b44SJordan Rupprecht        self.breakpoint1 = self.target.BreakpointCreateByLocation(src, self.line1)
3299451b44SJordan Rupprecht        self.assertTrue(
3399451b44SJordan Rupprecht            self.breakpoint1 and self.breakpoint1.GetNumLocations() == 1,
342238dcc3SJonas Devlieghere            VALID_BREAKPOINT,
352238dcc3SJonas Devlieghere        )
3699451b44SJordan Rupprecht
372238dcc3SJonas Devlieghere        self.breakpoint2 = self.target.BreakpointCreateBySourceRegex(
382238dcc3SJonas Devlieghere            "breakpoint_2", src
392238dcc3SJonas Devlieghere        )
402238dcc3SJonas Devlieghere        self.breakpoint2.GetLocationAtIndex(0).SetCondition("false")
4199451b44SJordan Rupprecht
422238dcc3SJonas Devlieghere        self.breakpoint3 = self.target.BreakpointCreateBySourceRegex(
432238dcc3SJonas Devlieghere            "breakpoint_3", src
442238dcc3SJonas Devlieghere        )
452238dcc3SJonas Devlieghere        self.breakpoint3.GetLocationAtIndex(0).SetCondition("false")
4699451b44SJordan Rupprecht
4799451b44SJordan Rupprecht        self.breakpoint4 = self.target.BreakpointCreateByLocation(src, self.line4)
4899451b44SJordan Rupprecht
4999451b44SJordan Rupprecht        # Start debugging
5099451b44SJordan Rupprecht        self.process = self.target.LaunchSimple(
512238dcc3SJonas Devlieghere            None, None, self.get_process_working_directory()
522238dcc3SJonas Devlieghere        )
5399451b44SJordan Rupprecht        self.assertIsNotNone(self.process, PROCESS_IS_VALID)
542238dcc3SJonas Devlieghere        self.thread = lldbutil.get_one_thread_stopped_at_breakpoint(
552238dcc3SJonas Devlieghere            self.process, self.breakpoint1
562238dcc3SJonas Devlieghere        )
5799451b44SJordan Rupprecht        self.assertIsNotNone(self.thread, "Didn't stop at breakpoint 1.")
5899451b44SJordan Rupprecht
5999451b44SJordan Rupprecht    def test_step_instruction(self):
6099451b44SJordan Rupprecht        # Count instructions between breakpoint_1 and breakpoint_4
612238dcc3SJonas Devlieghere        contextList = self.target.FindFunctions("main", lldb.eFunctionNameTypeAuto)
6280fcecb1SJonas Devlieghere        self.assertEqual(contextList.GetSize(), 1)
6399451b44SJordan Rupprecht        symbolContext = contextList.GetContextAtIndex(0)
6499451b44SJordan Rupprecht        function = symbolContext.GetFunction()
6599451b44SJordan Rupprecht        self.assertTrue(function)
6699451b44SJordan Rupprecht        instructions = function.GetInstructions(self.target)
6799451b44SJordan Rupprecht        addr_1 = self.breakpoint1.GetLocationAtIndex(0).GetAddress()
6899451b44SJordan Rupprecht        addr_4 = self.breakpoint4.GetLocationAtIndex(0).GetAddress()
6999451b44SJordan Rupprecht
7099451b44SJordan Rupprecht        # if third argument is true then the count will be the number of
7199451b44SJordan Rupprecht        # instructions on which a breakpoint can be set.
7299451b44SJordan Rupprecht        # start = addr_1, end = addr_4, canSetBreakpoint = True
7399451b44SJordan Rupprecht        steps_expected = instructions.GetInstructionsCount(addr_1, addr_4, True)
7499451b44SJordan Rupprecht        step_count = 0
7599451b44SJordan Rupprecht        # Step from breakpoint_1 to breakpoint_4
7699451b44SJordan Rupprecht        while True:
7799451b44SJordan Rupprecht            self.thread.StepInstruction(True)
7899451b44SJordan Rupprecht            step_count = step_count + 1
79ce825e46SJonas Devlieghere            self.assertState(self.process.GetState(), lldb.eStateStopped)
802238dcc3SJonas Devlieghere            self.assertTrue(
812238dcc3SJonas Devlieghere                self.thread.GetStopReason() == lldb.eStopReasonPlanComplete
822238dcc3SJonas Devlieghere                or self.thread.GetStopReason() == lldb.eStopReasonBreakpoint
832238dcc3SJonas Devlieghere            )
842238dcc3SJonas Devlieghere            if self.thread.GetStopReason() == lldb.eStopReasonBreakpoint:
8599451b44SJordan Rupprecht                # we should not stop on breakpoint_2 and _3 because they have false condition
8680fcecb1SJonas Devlieghere                self.assertEqual(
872238dcc3SJonas Devlieghere                    self.thread.GetFrameAtIndex(0).GetLineEntry().GetLine(), self.line4
882238dcc3SJonas Devlieghere                )
8999451b44SJordan Rupprecht                # breakpoint_2 and _3 should not affect step count
90*9c246882SJordan Rupprecht                self.assertGreaterEqual(step_count, steps_expected)
9199451b44SJordan Rupprecht                break
9299451b44SJordan Rupprecht
9399451b44SJordan Rupprecht        # Run the process until termination
9499451b44SJordan Rupprecht        self.process.Continue()
95ce825e46SJonas Devlieghere        self.assertState(self.process.GetState(), lldb.eStateExited)
9699451b44SJordan Rupprecht
9799451b44SJordan Rupprecht    @skipIf(bugnumber="llvm.org/pr31972", hostoslist=["windows"])
9899451b44SJordan Rupprecht    def test_step_over(self):
9999451b44SJordan Rupprecht        self.thread.StepOver()
10099451b44SJordan Rupprecht        # We should be stopped at the breakpoint_2 line with stop plan complete reason
101ce825e46SJonas Devlieghere        self.assertState(self.process.GetState(), lldb.eStateStopped)
1020f821339SJonas Devlieghere        self.assertStopReason(self.thread.GetStopReason(), lldb.eStopReasonPlanComplete)
10399451b44SJordan Rupprecht
10499451b44SJordan Rupprecht        self.thread.StepOver()
10599451b44SJordan Rupprecht        # We should be stopped at the breakpoint_3 line with stop plan complete reason
106ce825e46SJonas Devlieghere        self.assertState(self.process.GetState(), lldb.eStateStopped)
1070f821339SJonas Devlieghere        self.assertStopReason(self.thread.GetStopReason(), lldb.eStopReasonPlanComplete)
10899451b44SJordan Rupprecht
10999451b44SJordan Rupprecht        self.thread.StepOver()
11099451b44SJordan Rupprecht        # We should be stopped at the breakpoint_4
111ce825e46SJonas Devlieghere        self.assertState(self.process.GetState(), lldb.eStateStopped)
1120f821339SJonas Devlieghere        self.assertStopReason(self.thread.GetStopReason(), lldb.eStopReasonBreakpoint)
1132238dcc3SJonas Devlieghere        thread1 = lldbutil.get_one_thread_stopped_at_breakpoint(
1142238dcc3SJonas Devlieghere            self.process, self.breakpoint4
1152238dcc3SJonas Devlieghere        )
11680fcecb1SJonas Devlieghere        self.assertEqual(self.thread, thread1, "Didn't stop at breakpoint 4.")
11799451b44SJordan Rupprecht
11899451b44SJordan Rupprecht        # Check that stepping does not affect breakpoint's hit count
11980fcecb1SJonas Devlieghere        self.assertEqual(self.breakpoint1.GetHitCount(), 1)
12080fcecb1SJonas Devlieghere        self.assertEqual(self.breakpoint2.GetHitCount(), 0)
12180fcecb1SJonas Devlieghere        self.assertEqual(self.breakpoint3.GetHitCount(), 0)
12280fcecb1SJonas Devlieghere        self.assertEqual(self.breakpoint4.GetHitCount(), 1)
12399451b44SJordan Rupprecht
12499451b44SJordan Rupprecht        # Run the process until termination
12599451b44SJordan Rupprecht        self.process.Continue()
126ce825e46SJonas Devlieghere        self.assertState(self.process.GetState(), lldb.eStateExited)
127