1"""Test that adding, deleting and modifying watchpoints sends the appropriate events."""
2
3from __future__ import print_function
4
5
6import lldb
7from lldbsuite.test.decorators import *
8from lldbsuite.test.lldbtest import *
9from lldbsuite.test import lldbutil
10
11
12class TestWatchpointEvents (TestBase):
13    NO_DEBUG_INFO_TESTCASE = True
14
15    def setUp(self):
16        # Call super's setUp().
17        TestBase.setUp(self)
18        # Find the line numbers that we will step to in main:
19        self.main_source = "main.c"
20
21    @add_test_categories(['pyapi'])
22    def test_with_python_api(self):
23        """Test that adding, deleting and modifying watchpoints sends the appropriate events."""
24        self.build()
25        target = self.createTestTarget()
26
27        self.main_source_spec = lldb.SBFileSpec(self.main_source)
28
29        break_in_main = target.BreakpointCreateBySourceRegex(
30            '// Put a breakpoint here.', self.main_source_spec)
31        self.assertTrue(break_in_main, VALID_BREAKPOINT)
32
33        # Now launch the process, and do not stop at entry point.
34        process = target.LaunchSimple(
35            None, None, self.get_process_working_directory())
36
37        self.assertTrue(process, PROCESS_IS_VALID)
38
39        # The stop reason of the thread should be breakpoint.
40        threads = lldbutil.get_threads_stopped_at_breakpoint(
41            process, break_in_main)
42
43        if len(threads) != 1:
44            self.fail("Failed to stop at first breakpoint in main.")
45
46        thread = threads[0]
47        frame = thread.GetFrameAtIndex(0)
48        local_var = frame.FindVariable("local_var")
49        self.assertTrue(local_var.IsValid())
50
51        self.listener = lldb.SBListener("com.lldb.testsuite_listener")
52        self.target_bcast = target.GetBroadcaster()
53        self.target_bcast.AddListener(
54            self.listener, lldb.SBTarget.eBroadcastBitWatchpointChanged)
55        self.listener.StartListeningForEvents(
56            self.target_bcast, lldb.SBTarget.eBroadcastBitWatchpointChanged)
57
58        error = lldb.SBError()
59        local_watch = local_var.Watch(True, False, True, error)
60        if not error.Success():
61            self.fail(
62                "Failed to make watchpoint for local_var: %s" %
63                (error.GetCString()))
64
65        self.GetWatchpointEvent(lldb.eWatchpointEventTypeAdded)
66        # Now change some of the features of this watchpoint and make sure we
67        # get events:
68        local_watch.SetEnabled(False)
69        self.GetWatchpointEvent(lldb.eWatchpointEventTypeDisabled)
70
71        local_watch.SetEnabled(True)
72        self.GetWatchpointEvent(lldb.eWatchpointEventTypeEnabled)
73
74        local_watch.SetIgnoreCount(10)
75        self.GetWatchpointEvent(lldb.eWatchpointEventTypeIgnoreChanged)
76
77        condition = "1 == 2"
78        local_watch.SetCondition(condition)
79        self.GetWatchpointEvent(lldb.eWatchpointEventTypeConditionChanged)
80
81        self.assertEqual(local_watch.GetCondition(), condition,
82                        'make sure watchpoint condition is "' + condition + '"')
83
84    def GetWatchpointEvent(self, event_type):
85        # We added a watchpoint so we should get a watchpoint added event.
86        event = lldb.SBEvent()
87        success = self.listener.WaitForEvent(1, event)
88        self.assertTrue(success, "Successfully got watchpoint event")
89        self.assertTrue(
90            lldb.SBWatchpoint.EventIsWatchpointEvent(event),
91            "Event is a watchpoint event.")
92        found_type = lldb.SBWatchpoint.GetWatchpointEventTypeFromEvent(event)
93        self.assertEqual(
94            found_type, event_type,
95            "Event is not correct type, expected: %d, found: %d" %
96            (event_type,
97             found_type))
98        # There shouldn't be another event waiting around:
99        found_event = self.listener.PeekAtNextEventForBroadcasterWithType(
100            self.target_bcast, lldb.SBTarget.eBroadcastBitBreakpointChanged, event)
101        if found_event:
102            print("Found an event I didn't expect: ", event)
103
104        self.assertTrue(not found_event, "Only one event per change.")
105