"""
Use lldb Python SBWatchpointLocation API to set the ignore count.
"""

import os, time
import re
import unittest2
import lldb, lldbutil
from lldbtest import *

class WatchpointIgnoreCountTestCase(TestBase):

    mydir = os.path.join("python_api", "watchpoint")

    def setUp(self):
        # Call super's setUp().
        TestBase.setUp(self)
        # Our simple source filename.
        self.source = 'main.c'
        # Find the line number to break inside main().
        self.line = line_number(self.source, '// Set break point at this line.')

    @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
    @python_api_test
    def test_set_watch_loc_ignore_count_with_dsym(self):
        """Test SBWatchpointLocation.SetIgnoreCount() API."""
        self.buildDsym()
        self.do_watchpoint_location_ignore_count()

    @python_api_test
    def test_set_watch_loc_ignore_count_with_dwarf(self):
        """Test SBWatchpointLocation.SetIgnoreCount() API."""
        self.buildDwarf()
        self.do_watchpoint_location_ignore_count()

    def do_watchpoint_location_ignore_count(self):
        """Test SBWatchpointLocation.SetIgnoreCount() API."""
        exe = os.path.join(os.getcwd(), "a.out")

        # Create a target by the debugger.
        target = self.dbg.CreateTarget(exe)
        self.assertTrue(target, VALID_TARGET)

        # Create a breakpoint on main.c in order to set our watchpoint later.
        breakpoint = target.BreakpointCreateByLocation(self.source, self.line)
        self.assertTrue(breakpoint and
                        breakpoint.GetNumLocations() == 1,
                        VALID_BREAKPOINT)

        # Now launch the process, and do not stop at the entry point.
        process = target.LaunchSimple(None, None, os.getcwd())

        # We should be stopped due to the breakpoint.  Get frame #0.
        process = target.GetProcess()
        self.assertTrue(process.GetState() == lldb.eStateStopped,
                        PROCESS_STOPPED)
        thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint)
        frame0 = thread.GetFrameAtIndex(0)

        # Watch 'global' for read and write.
        value = frame0.WatchValue('global',
                                  lldb.eValueTypeVariableGlobal,
                                  lldb.LLDB_WATCH_TYPE_READ|lldb.LLDB_WATCH_TYPE_WRITE)
        self.assertTrue(value, "Successfully found the variable and set a watchpoint")
        self.DebugSBValue(value)

        # Hide stdout if not running with '-t' option.
        if not self.TraceOn():
            self.HideStdout()

        # There should be only 1 watchpoint location under the target.
        self.assertTrue(target.GetNumWatchpointLocations() == 1)
        wp_loc = target.GetWatchpointLocationAtIndex(0)
        last_created = target.GetLastCreatedWatchpointLocation()
        self.assertTrue(wp_loc == last_created)
        self.assertTrue(wp_loc.IsEnabled())
        self.assertTrue(wp_loc.GetIgnoreCount() == 0)
        watch_id = wp_loc.GetID()
        self.assertTrue(watch_id != 0)
        print wp_loc

        # Now immediately set the ignore count to 2.  When we continue, expect the
        # inferior to run to its completion without stopping due to watchpoint.
        wp_loc.SetIgnoreCount(2)
        print wp_loc
        process.Continue()

        # At this point, the inferior process should have exited.
        self.assertTrue(process.GetState() == lldb.eStateExited, PROCESS_EXITED)

        # Verify some vital statistics.
        self.assertTrue(wp_loc)
        self.assertTrue(wp_loc.GetWatchSize() == 4)
        self.assertTrue(wp_loc.GetHitCount() == 2)
        print wp_loc


if __name__ == '__main__':
    import atexit
    lldb.SBDebugger.Initialize()
    atexit.register(lambda: lldb.SBDebugger.Terminate())
    unittest2.main()
