brintos

brintos / llvm-project-archived public Read only

0
0
Text · 4.8 KiB · 6e05cf0 Raw
127 lines · python
1"""Test that adding, deleting and modifying watchpoints sends the appropriate events."""2 3import lldb4from lldbsuite.test.decorators import *5from lldbsuite.test.lldbtest import *6from lldbsuite.test import lldbutil7 8 9class TestWatchpointEvents(TestBase):10    NO_DEBUG_INFO_TESTCASE = True11 12    def setUp(self):13        # Call super's setUp().14        TestBase.setUp(self)15        # Find the line numbers that we will step to in main:16        self.main_source = "main.c"17 18    @add_test_categories(["pyapi"])19    def test_with_python_api(self):20        """Test that adding, deleting and modifying watchpoints sends the appropriate events."""21        self.build()22        target = self.createTestTarget()23 24        self.main_source_spec = lldb.SBFileSpec(self.main_source)25 26        break_in_main = target.BreakpointCreateBySourceRegex(27            "// Put a breakpoint here.", self.main_source_spec28        )29        self.assertTrue(break_in_main, VALID_BREAKPOINT)30 31        # Now launch the process, and do not stop at entry point.32        process = target.LaunchSimple(None, None, self.get_process_working_directory())33 34        self.assertTrue(process, PROCESS_IS_VALID)35 36        # The stop reason of the thread should be breakpoint.37        threads = lldbutil.get_threads_stopped_at_breakpoint(process, break_in_main)38 39        if len(threads) != 1:40            self.fail("Failed to stop at first breakpoint in main.")41 42        thread = threads[0]43        frame = thread.GetFrameAtIndex(0)44        local_var = frame.FindVariable("local_var")45        self.assertTrue(local_var.IsValid())46 47        self.listener = lldb.SBListener("com.lldb.testsuite_listener")48        self.target_bcast = target.GetBroadcaster()49        self.target_bcast.AddListener(50            self.listener, lldb.SBTarget.eBroadcastBitWatchpointChanged51        )52        self.listener.StartListeningForEvents(53            self.target_bcast, lldb.SBTarget.eBroadcastBitWatchpointChanged54        )55 56        error = lldb.SBError()57        local_watch = local_var.Watch(True, False, True, error)58        if not error.Success():59            self.fail(60                "Failed to make watchpoint for local_var: %s" % (error.GetCString())61            )62 63        self.GetWatchpointEvent(lldb.eWatchpointEventTypeAdded)64        # Now change some of the features of this watchpoint and make sure we65        # get events:66        local_watch.SetEnabled(False)67        self.GetWatchpointEvent(lldb.eWatchpointEventTypeDisabled)68 69        local_watch.SetEnabled(True)70        self.GetWatchpointEvent(lldb.eWatchpointEventTypeEnabled)71 72        local_watch.SetIgnoreCount(10)73        self.GetWatchpointEvent(lldb.eWatchpointEventTypeIgnoreChanged)74 75        condition = "1 == 2"76        local_watch.SetCondition(condition)77        self.GetWatchpointEvent(lldb.eWatchpointEventTypeConditionChanged)78 79        self.assertEqual(80            local_watch.GetCondition(),81            condition,82            'make sure watchpoint condition is "' + condition + '"',83        )84 85        target.DeleteWatchpoint(local_watch.GetID())86        self.GetWatchpointEvent(87            lldb.eWatchpointEventTypeDisabled, lldb.eWatchpointEventTypeRemoved88        )89 90        # Re-create it so that we can check DeleteAllWatchpoints91        local_watch = local_var.Watch(True, False, True, error)92        if not error.Success():93            self.fail(94                "Failed to make watchpoint for local_var: %s" % (error.GetCString())95            )96        self.GetWatchpointEvent(lldb.eWatchpointEventTypeAdded)97        target.DeleteAllWatchpoints()98        self.GetWatchpointEvent(99            lldb.eWatchpointEventTypeDisabled, lldb.eWatchpointEventTypeRemoved100        )101 102    def GetWatchpointEvent(self, *event_types):103        # We added a watchpoint so we should get a watchpoint added event.104        event = lldb.SBEvent()105        for event_type in event_types:106            success = self.listener.WaitForEvent(1, event)107            self.assertTrue(success, "Successfully got watchpoint event")108            self.assertTrue(109                lldb.SBWatchpoint.EventIsWatchpointEvent(event),110                "Event is a watchpoint event.",111            )112            found_type = lldb.SBWatchpoint.GetWatchpointEventTypeFromEvent(event)113            self.assertEqual(114                found_type,115                event_type,116                "Event is not correct type, expected: %d, found: %d"117                % (event_type, found_type),118            )119        # There shouldn't be another event waiting around:120        found_event = self.listener.PeekAtNextEventForBroadcasterWithType(121            self.target_bcast, lldb.SBTarget.eBroadcastBitWatchpointChanged, event122        )123        if found_event:124            print("Found an event I didn't expect: ", event.GetType())125 126        self.assertTrue(not found_event, f"Only expected {len(event_types)} events.")127