1"""
2Test that the SBWatchpoint::SetEnable API works.
3"""
4
5import lldb
6from lldbsuite.test.lldbtest import *
7from lldbsuite.test.decorators import *
8from lldbsuite.test import lldbplatform, lldbplatformutil
9
10
11class TestWatchpointSetEnable(TestBase):
12    mydir = TestBase.compute_mydir(__file__)
13    NO_DEBUG_INFO_TESTCASE = True
14
15    def test_disable_works (self):
16        """Set a watchpoint, disable it, and make sure it doesn't get hit."""
17        self.build()
18        self.do_test(False)
19
20    def test_disable_enable_works (self):
21        """Set a watchpoint, disable it, and make sure it doesn't get hit."""
22        self.build()
23        self.do_test(True)
24
25    def do_test(self, test_enable):
26        """Set a watchpoint, disable it and make sure it doesn't get hit."""
27
28        exe = self.getBuildArtifact("a.out")
29        main_file_spec = lldb.SBFileSpec("main.c")
30
31        # Create a target by the debugger.
32        self.target = self.dbg.CreateTarget(exe)
33        self.assertTrue(self.target, VALID_TARGET)
34
35        bkpt_before = self.target.BreakpointCreateBySourceRegex("Set a breakpoint here", main_file_spec)
36        self.assertEqual(bkpt_before.GetNumLocations(),  1, "Failed setting the before breakpoint.")
37
38        bkpt_after = self.target.BreakpointCreateBySourceRegex("We should have stopped", main_file_spec)
39        self.assertEqual(bkpt_after.GetNumLocations(), 1, "Failed setting the after breakpoint.")
40
41        process = self.target.LaunchSimple(
42            None, None, self.get_process_working_directory())
43        self.assertTrue(process, PROCESS_IS_VALID)
44
45        thread = lldbutil.get_one_thread_stopped_at_breakpoint(process, bkpt_before)
46        self.assertTrue(thread.IsValid(), "We didn't stop at the before breakpoint.")
47
48        ret_val = lldb.SBCommandReturnObject()
49        self.dbg.GetCommandInterpreter().HandleCommand("watchpoint set variable -w write global_var", ret_val)
50        self.assertTrue(ret_val.Succeeded(), "Watchpoint set variable did not return success.")
51
52        wp = self.target.FindWatchpointByID(1)
53        self.assertTrue(wp.IsValid(), "Didn't make a valid watchpoint.")
54        self.assertNotEqual(wp.GetWatchAddress(), lldb.LLDB_INVALID_ADDRESS, "Watch address is invalid")
55
56        wp.SetEnabled(False)
57        self.assertTrue(not wp.IsEnabled(), "The watchpoint thinks it is still enabled")
58
59        process.Continue()
60
61        stop_reason = thread.GetStopReason()
62
63        self.assertEqual(stop_reason, lldb.eStopReasonBreakpoint, "We didn't stop at our breakpoint.")
64
65        if test_enable:
66            wp.SetEnabled(True)
67            self.assertTrue(wp.IsEnabled(), "The watchpoint thinks it is still disabled.")
68            process.Continue()
69            stop_reason = thread.GetStopReason()
70            self.assertEqual(stop_reason, lldb.eStopReasonWatchpoint, "We didn't stop at our watchpoint")
71
72