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    # on arm64 targets, lldb has incorrect hit-count / ignore-counts
29    # for watchpoints when they are hit with multiple threads at
30    # the same time.  Tracked as llvm.org/pr49433
31    # or rdar://93863107 inside Apple.
32    def affected_by_radar_93863107(self):
33        return (self.getArchitecture() in ['arm64', 'arm64e']) and self.platformIsDarwin()
34
35    # Read-write watchpoints not supported on SystemZ
36    @expectedFailureAll(archs=['s390x'])
37    def test_set_watch_ignore_count(self):
38        """Test SBWatchpoint.SetIgnoreCount() API."""
39        self.build()
40        exe = self.getBuildArtifact("a.out")
41
42        # Create a target by the debugger.
43        target = self.dbg.CreateTarget(exe)
44        self.assertTrue(target, VALID_TARGET)
45
46        # Create a breakpoint on main.c in order to set our watchpoint later.
47        breakpoint = target.BreakpointCreateByLocation(self.source, self.line)
48        self.assertTrue(breakpoint and
49                        breakpoint.GetNumLocations() == 1,
50                        VALID_BREAKPOINT)
51
52        # Now launch the process, and do not stop at the entry point.
53        process = target.LaunchSimple(
54            None, None, self.get_process_working_directory())
55
56        # We should be stopped due to the breakpoint.  Get frame #0.
57        process = target.GetProcess()
58        self.assertEqual(process.GetState(), lldb.eStateStopped,
59                        PROCESS_STOPPED)
60        thread = lldbutil.get_stopped_thread(
61            process, lldb.eStopReasonBreakpoint)
62        frame0 = thread.GetFrameAtIndex(0)
63
64        # Watch 'global' for read and write.
65        value = frame0.FindValue('global', lldb.eValueTypeVariableGlobal)
66        error = lldb.SBError()
67        watchpoint = value.Watch(True, True, True, error)
68        self.assertTrue(value and watchpoint,
69                        "Successfully found the variable and set a watchpoint")
70        self.DebugSBValue(value)
71
72        # Hide stdout if not running with '-t' option.
73        if not self.TraceOn():
74            self.HideStdout()
75
76        # There should be only 1 watchpoint location under the target.
77        self.assertEqual(target.GetNumWatchpoints(), 1)
78        watchpoint = target.GetWatchpointAtIndex(0)
79        self.assertTrue(watchpoint.IsEnabled())
80        self.assertEqual(watchpoint.GetIgnoreCount(), 0)
81        watch_id = watchpoint.GetID()
82        self.assertNotEqual(watch_id, 0)
83        print(watchpoint)
84
85        # Now immediately set the ignore count to 2.  When we continue, expect the
86        # inferior to run to its completion without stopping due to watchpoint.
87        watchpoint.SetIgnoreCount(2)
88        print(watchpoint)
89        process.Continue()
90
91        # At this point, the inferior process should have exited.
92        self.assertEqual(process.GetState(), lldb.eStateExited, PROCESS_EXITED)
93
94        # Verify some vital statistics.
95        self.assertTrue(watchpoint)
96        self.assertEqual(watchpoint.GetWatchSize(), 4)
97        if not self.affected_by_radar_93863107():
98          self.assertEqual(watchpoint.GetHitCount(), 2)
99        print(watchpoint)
100