xref: /llvm-project/lldb/test/API/functionalities/breakpoint/address_breakpoints/TestAddressBreakpoints.py (revision 80fcecb13c388ff087a27a4b0e7ca3dd8c98eaa4)
199451b44SJordan Rupprecht"""
299451b44SJordan RupprechtTest address breakpoints set with shared library of SBAddress work correctly.
399451b44SJordan Rupprecht"""
499451b44SJordan Rupprecht
599451b44SJordan Rupprecht
699451b44SJordan Rupprechtimport lldb
799451b44SJordan Rupprechtimport lldbsuite.test.lldbutil as lldbutil
899451b44SJordan Rupprechtfrom lldbsuite.test.lldbtest import *
999451b44SJordan Rupprecht
1099451b44SJordan Rupprecht
1199451b44SJordan Rupprechtclass AddressBreakpointTestCase(TestBase):
1299451b44SJordan Rupprecht    NO_DEBUG_INFO_TESTCASE = True
1399451b44SJordan Rupprecht
1499451b44SJordan Rupprecht    def test_address_breakpoints(self):
1599451b44SJordan Rupprecht        """Test address breakpoints set with shared library of SBAddress work correctly."""
1699451b44SJordan Rupprecht        self.build()
1799451b44SJordan Rupprecht        self.address_breakpoints()
1899451b44SJordan Rupprecht
1999451b44SJordan Rupprecht    def address_breakpoints(self):
2099451b44SJordan Rupprecht        """Test address breakpoints set with shared library of SBAddress work correctly."""
2154c26872SRaphael Isemann        target = self.createTestTarget()
2299451b44SJordan Rupprecht
2399451b44SJordan Rupprecht        # Now create a breakpoint on main.c by name 'c'.
2499451b44SJordan Rupprecht        breakpoint = target.BreakpointCreateBySourceRegex(
252238dcc3SJonas Devlieghere            "Set a breakpoint here", lldb.SBFileSpec("main.c")
262238dcc3SJonas Devlieghere        )
272238dcc3SJonas Devlieghere        self.assertTrue(
282238dcc3SJonas Devlieghere            breakpoint and breakpoint.GetNumLocations() >= 1, VALID_BREAKPOINT
292238dcc3SJonas Devlieghere        )
3099451b44SJordan Rupprecht
3199451b44SJordan Rupprecht        # Get the breakpoint location from breakpoint after we verified that,
3299451b44SJordan Rupprecht        # indeed, it has one location.
3399451b44SJordan Rupprecht        location = breakpoint.GetLocationAtIndex(0)
342238dcc3SJonas Devlieghere        self.assertTrue(location and location.IsEnabled(), VALID_BREAKPOINT_LOCATION)
3599451b44SJordan Rupprecht
3699451b44SJordan Rupprecht        # Next get the address from the location, and create an address breakpoint using
3799451b44SJordan Rupprecht        # that address:
3899451b44SJordan Rupprecht
3999451b44SJordan Rupprecht        address = location.GetAddress()
4099451b44SJordan Rupprecht        target.BreakpointDelete(breakpoint.GetID())
4199451b44SJordan Rupprecht
4299451b44SJordan Rupprecht        breakpoint = target.BreakpointCreateBySBAddress(address)
4399451b44SJordan Rupprecht
4499451b44SJordan Rupprecht        # Disable ASLR.  This will allow us to actually test (on platforms that support this flag)
4599451b44SJordan Rupprecht        # that the breakpoint was able to track the module.
4699451b44SJordan Rupprecht
4799451b44SJordan Rupprecht        launch_info = lldb.SBLaunchInfo(None)
4899451b44SJordan Rupprecht        flags = launch_info.GetLaunchFlags()
4999451b44SJordan Rupprecht        flags &= ~lldb.eLaunchFlagDisableASLR
5086aa8e63SJonas Devlieghere        flags &= lldb.eLaunchFlagInheritTCCFromParent
5199451b44SJordan Rupprecht        launch_info.SetLaunchFlags(flags)
5299451b44SJordan Rupprecht
5399451b44SJordan Rupprecht        error = lldb.SBError()
5499451b44SJordan Rupprecht
5599451b44SJordan Rupprecht        process = target.Launch(launch_info, error)
5699451b44SJordan Rupprecht        self.assertTrue(process, PROCESS_IS_VALID)
5799451b44SJordan Rupprecht
5899451b44SJordan Rupprecht        # Did we hit our breakpoint?
5999451b44SJordan Rupprecht        from lldbsuite.test.lldbutil import get_threads_stopped_at_breakpoint
602238dcc3SJonas Devlieghere
6199451b44SJordan Rupprecht        threads = get_threads_stopped_at_breakpoint(process, breakpoint)
620ed758b2SDave Lee        self.assertEqual(
632238dcc3SJonas Devlieghere            len(threads), 1, "There should be a thread stopped at our breakpoint"
642238dcc3SJonas Devlieghere        )
6599451b44SJordan Rupprecht
6699451b44SJordan Rupprecht        # The hit count for the breakpoint should be 1.
67*80fcecb1SJonas Devlieghere        self.assertEqual(breakpoint.GetHitCount(), 1)
6899451b44SJordan Rupprecht
6999451b44SJordan Rupprecht        process.Kill()
7099451b44SJordan Rupprecht
7199451b44SJordan Rupprecht        # Now re-launch and see that we hit the breakpoint again:
7299451b44SJordan Rupprecht        launch_info.Clear()
7399451b44SJordan Rupprecht        launch_info.SetLaunchFlags(flags)
7499451b44SJordan Rupprecht
7599451b44SJordan Rupprecht        process = target.Launch(launch_info, error)
7699451b44SJordan Rupprecht        self.assertTrue(process, PROCESS_IS_VALID)
7799451b44SJordan Rupprecht
7899451b44SJordan Rupprecht        thread = get_threads_stopped_at_breakpoint(process, breakpoint)
790ed758b2SDave Lee        self.assertEqual(
802238dcc3SJonas Devlieghere            len(threads), 1, "There should be a thread stopped at our breakpoint"
812238dcc3SJonas Devlieghere        )
8299451b44SJordan Rupprecht
8397495874SFelipe de Azevedo Piovezan        # The hit count for the breakpoint should be 1, since we reset counts
8497495874SFelipe de Azevedo Piovezan        # for each run.
85*80fcecb1SJonas Devlieghere        self.assertEqual(breakpoint.GetHitCount(), 1)
86