Home | History | Annotate | Download | only in watchpoint_events
      1 """Test that adding, deleting and modifying watchpoints sends the appropriate events."""
      2 
      3 import os, time
      4 import unittest2
      5 import lldb
      6 import lldbutil
      7 from lldbtest import *
      8 
      9 class TestWatchpointEvents (TestBase):
     10 
     11     mydir = os.path.join("functionalities", "watchpoint", "watchpoint_events")
     12 
     13     @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
     14     @python_api_test
     15     @dsym_test
     16     def test_with_dsym_and_python_api(self):
     17         """Test that adding, deleting and modifying watchpoints sends the appropriate events."""
     18         self.buildDsym()
     19         self.step_over_stepping()
     20 
     21     @expectedFailureFreeBSD('llvm.org/pr16706') # Watchpoints fail on FreeBSD
     22     @python_api_test
     23     @dwarf_test
     24     def test_with_dwarf_and_python_api(self):
     25         """Test that adding, deleting and modifying watchpoints sends the appropriate events."""
     26         self.buildDwarf()
     27         self.step_over_stepping()
     28 
     29     def setUp(self):
     30         # Call super's setUp().
     31         TestBase.setUp(self)
     32         # Find the line numbers that we will step to in main:
     33         self.main_source = "main.c"
     34 
     35     def GetWatchpointEvent (self, event_type):
     36         # We added a watchpoint so we should get a watchpoint added event.
     37         event = lldb.SBEvent()
     38         success = self.listener.WaitForEvent (1, event)
     39         self.assertTrue(success == True, "Successfully got watchpoint event")
     40         self.assertTrue (lldb.SBWatchpoint.EventIsWatchpointEvent(event), "Event is a watchpoint event.")
     41         found_type = lldb.SBWatchpoint.GetWatchpointEventTypeFromEvent (event)
     42         self.assertTrue (found_type == event_type, "Event is not correct type, expected: %d, found: %d"%(event_type, found_type))
     43         # There shouldn't be another event waiting around:
     44         found_event = self.listener.PeekAtNextEventForBroadcasterWithType (self.target_bcast, lldb.SBTarget.eBroadcastBitBreakpointChanged, event)
     45         if found_event:
     46             print "Found an event I didn't expect: ", event
     47 
     48         self.assertTrue (not found_event, "Only one event per change.")
     49 
     50     def step_over_stepping(self):
     51         """Use Python APIs to test stepping over and hitting breakpoints."""
     52         exe = os.path.join(os.getcwd(), "a.out")
     53 
     54         target = self.dbg.CreateTarget(exe)
     55         self.assertTrue(target, VALID_TARGET)
     56 
     57         self.main_source_spec = lldb.SBFileSpec (self.main_source)
     58 
     59         break_in_main = target.BreakpointCreateBySourceRegex ('// Put a breakpoint here.', self.main_source_spec)
     60         self.assertTrue(break_in_main, VALID_BREAKPOINT)
     61 
     62         # Now launch the process, and do not stop at entry point.
     63         process = target.LaunchSimple (None, None, os.getcwd())
     64 
     65         self.assertTrue(process, PROCESS_IS_VALID)
     66 
     67         # The stop reason of the thread should be breakpoint.
     68         threads = lldbutil.get_threads_stopped_at_breakpoint (process, break_in_main)
     69 
     70         if len(threads) != 1:
     71             self.fail ("Failed to stop at first breakpoint in main.")
     72 
     73         thread = threads[0]
     74         frame = thread.GetFrameAtIndex(0)
     75         local_var = frame.FindVariable ("local_var")
     76         self.assertTrue (local_var.IsValid())
     77 
     78         self.listener = lldb.SBListener("com.lldb.testsuite_listener")
     79         self.target_bcast = target.GetBroadcaster()
     80         self.target_bcast.AddListener (self.listener, lldb.SBTarget.eBroadcastBitWatchpointChanged)
     81         self.listener.StartListeningForEvents (self.target_bcast, lldb.SBTarget.eBroadcastBitWatchpointChanged)
     82 
     83         error = lldb.SBError()
     84         local_watch = local_var.Watch(True, True, True, error)
     85         if not error.Success():
     86             self.fail ("Failed to make watchpoint for local_var: %s"%(error.GetCString()))
     87 
     88         self.GetWatchpointEvent (lldb.eWatchpointEventTypeAdded)
     89         # Now change some of the features of this watchpoint and make sure we get events:
     90         local_watch.SetEnabled(False)
     91         self.GetWatchpointEvent (lldb.eWatchpointEventTypeDisabled)
     92 
     93         local_watch.SetIgnoreCount(10)
     94         self.GetWatchpointEvent (lldb.eWatchpointEventTypeIgnoreChanged)
     95 
     96         local_watch.SetCondition ("1 == 2")
     97         self.GetWatchpointEvent (lldb.eWatchpointEventTypeConditionChanged)
     98 
     99 if __name__ == '__main__':
    100     import atexit
    101     lldb.SBDebugger.Initialize()
    102     atexit.register(lambda: lldb.SBDebugger.Terminate())
    103     unittest2.main()
    104