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