xref: /llvm-project/lldb/test/API/functionalities/breakpoint/debugbreak/TestDebugBreak.py (revision 2238dcc39358353cac21df75c3c3286ab20b8f53)
199451b44SJordan Rupprecht"""
299451b44SJordan RupprechtTest embedded breakpoints, like `asm int 3;` in x86 or or `__debugbreak` on Windows.
399451b44SJordan Rupprecht"""
499451b44SJordan Rupprecht
599451b44SJordan Rupprecht
699451b44SJordan Rupprechtimport lldb
799451b44SJordan Rupprechtfrom lldbsuite.test.decorators import *
899451b44SJordan Rupprechtfrom lldbsuite.test.lldbtest import *
999451b44SJordan Rupprechtfrom lldbsuite.test import lldbutil
1099451b44SJordan Rupprecht
1199451b44SJordan Rupprecht
1299451b44SJordan Rupprechtclass DebugBreakTestCase(TestBase):
1399451b44SJordan Rupprecht    @skipIf(archs=no_match(["i386", "i686", "x86_64"]))
1499451b44SJordan Rupprecht    @no_debug_info_test
1599451b44SJordan Rupprecht    def test_asm_int_3(self):
1699451b44SJordan Rupprecht        """Test that intrinsics like `__debugbreak();` and `asm {"int3"}` are treated like breakpoints."""
1799451b44SJordan Rupprecht        self.build()
1899451b44SJordan Rupprecht        exe = self.getBuildArtifact("a.out")
1999451b44SJordan Rupprecht
2099451b44SJordan Rupprecht        # Run the program.
2199451b44SJordan Rupprecht        target = self.dbg.CreateTarget(exe)
22*2238dcc3SJonas Devlieghere        process = target.LaunchSimple(None, None, self.get_process_working_directory())
2399451b44SJordan Rupprecht
2499451b44SJordan Rupprecht        # We've hit the first stop, so grab the frame.
2547c4c6a7SDave Lee        self.assertState(process.GetState(), lldb.eStateStopped)
26*2238dcc3SJonas Devlieghere        stop_reason = (
27*2238dcc3SJonas Devlieghere            lldb.eStopReasonException
28*2238dcc3SJonas Devlieghere            if (
29*2238dcc3SJonas Devlieghere                lldbplatformutil.getPlatform() == "windows"
30*2238dcc3SJonas Devlieghere                or lldbplatformutil.getPlatform() == "macosx"
31*2238dcc3SJonas Devlieghere            )
32*2238dcc3SJonas Devlieghere            else lldb.eStopReasonSignal
33*2238dcc3SJonas Devlieghere        )
3499451b44SJordan Rupprecht        thread = lldbutil.get_stopped_thread(process, stop_reason)
3599451b44SJordan Rupprecht        self.assertIsNotNone(
36*2238dcc3SJonas Devlieghere            thread, "Unable to find thread stopped at the __debugbreak()"
37*2238dcc3SJonas Devlieghere        )
3899451b44SJordan Rupprecht        frame = thread.GetFrameAtIndex(0)
3999451b44SJordan Rupprecht
407240436cSGabriel Ravier        # We should be in function 'bar'.
4199451b44SJordan Rupprecht        self.assertTrue(frame.IsValid())
4299451b44SJordan Rupprecht        function_name = frame.GetFunctionName()
43*2238dcc3SJonas Devlieghere        self.assertIn(
44*2238dcc3SJonas Devlieghere            "bar", function_name, "Unexpected function name {}".format(function_name)
45*2238dcc3SJonas Devlieghere        )
4699451b44SJordan Rupprecht
4799451b44SJordan Rupprecht        # We should be able to evaluate the parameter foo.
48*2238dcc3SJonas Devlieghere        value = frame.EvaluateExpression("*foo")
4999451b44SJordan Rupprecht        self.assertEqual(value.GetValueAsSigned(), 42)
5099451b44SJordan Rupprecht
5199451b44SJordan Rupprecht        # The counter should be 1 at the first stop and increase by 2 for each
5299451b44SJordan Rupprecht        # subsequent stop.
5399451b44SJordan Rupprecht        counter = 1
5499451b44SJordan Rupprecht        while counter < 20:
55*2238dcc3SJonas Devlieghere            value = frame.EvaluateExpression("count")
5699451b44SJordan Rupprecht            self.assertEqual(value.GetValueAsSigned(), counter)
5799451b44SJordan Rupprecht            counter += 2
5899451b44SJordan Rupprecht            process.Continue()
5999451b44SJordan Rupprecht
6099451b44SJordan Rupprecht        # The inferior should exit after the last iteration.
611b8c7352SJonas Devlieghere        self.assertState(process.GetState(), lldb.eStateExited)
62