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