1"""
2Use lldb Python SBWatchpoint API to set the ignore count.
3"""
4
5from __future__ import print_function
6
7
8import lldb
9from lldbsuite.test.decorators import *
10from lldbsuite.test.lldbtest import *
11from lldbsuite.test import lldbutil
12
13
14class WatchpointIgnoreCountTestCase(TestBase):
15
16    mydir = TestBase.compute_mydir(__file__)
17    NO_DEBUG_INFO_TESTCASE = True
18
19    def setUp(self):
20        # Call super's setUp().
21        TestBase.setUp(self)
22        # Our simple source filename.
23        self.source = 'main.c'
24        # Find the line number to break inside main().
25        self.line = line_number(
26            self.source, '// Set break point at this line.')
27
28    @add_test_categories(['pyapi'])
29    # Read-write watchpoints not supported on SystemZ
30    @expectedFailureAll(archs=['s390x'])
31    def test_set_watch_ignore_count(self):
32        """Test SBWatchpoint.SetIgnoreCount() API."""
33        self.build()
34        exe = self.getBuildArtifact("a.out")
35
36        # Create a target by the debugger.
37        target = self.dbg.CreateTarget(exe)
38        self.assertTrue(target, VALID_TARGET)
39
40        # Create a breakpoint on main.c in order to set our watchpoint later.
41        breakpoint = target.BreakpointCreateByLocation(self.source, self.line)
42        self.assertTrue(breakpoint and
43                        breakpoint.GetNumLocations() == 1,
44                        VALID_BREAKPOINT)
45
46        # Now launch the process, and do not stop at the entry point.
47        process = target.LaunchSimple(
48            None, None, self.get_process_working_directory())
49
50        # We should be stopped due to the breakpoint.  Get frame #0.
51        process = target.GetProcess()
52        self.assertEqual(process.GetState(), lldb.eStateStopped,
53                        PROCESS_STOPPED)
54        thread = lldbutil.get_stopped_thread(
55            process, lldb.eStopReasonBreakpoint)
56        frame0 = thread.GetFrameAtIndex(0)
57
58        # Watch 'global' for read and write.
59        value = frame0.FindValue('global', lldb.eValueTypeVariableGlobal)
60        error = lldb.SBError()
61        watchpoint = value.Watch(True, True, True, error)
62        self.assertTrue(value and watchpoint,
63                        "Successfully found the variable and set a watchpoint")
64        self.DebugSBValue(value)
65
66        # Hide stdout if not running with '-t' option.
67        if not self.TraceOn():
68            self.HideStdout()
69
70        # There should be only 1 watchpoint location under the target.
71        self.assertEqual(target.GetNumWatchpoints(), 1)
72        watchpoint = target.GetWatchpointAtIndex(0)
73        self.assertTrue(watchpoint.IsEnabled())
74        self.assertEqual(watchpoint.GetIgnoreCount(), 0)
75        watch_id = watchpoint.GetID()
76        self.assertNotEqual(watch_id, 0)
77        print(watchpoint)
78
79        # Now immediately set the ignore count to 2.  When we continue, expect the
80        # inferior to run to its completion without stopping due to watchpoint.
81        watchpoint.SetIgnoreCount(2)
82        print(watchpoint)
83        process.Continue()
84
85        # At this point, the inferior process should have exited.
86        self.assertEqual(process.GetState(), lldb.eStateExited, PROCESS_EXITED)
87
88        # Verify some vital statistics.
89        self.assertTrue(watchpoint)
90        self.assertEqual(watchpoint.GetWatchSize(), 4)
91        self.assertEqual(watchpoint.GetHitCount(), 2)
92        print(watchpoint)
93