2 Use lldb Python SBTarget API to iterate on the watchpoint(s) for the target.
5 from __future__ import print_function
14 from lldbsuite.test.decorators import *
15 from lldbsuite.test.lldbtest import *
16 from lldbsuite.test import lldbutil
18 class WatchpointIteratorTestCase(TestBase):
20 mydir = TestBase.compute_mydir(__file__)
23 # Call super's setUp().
25 # Our simple source filename.
26 self.source = 'main.c'
27 # Find the line number to break inside main().
28 self.line = line_number(self.source, '// Set break point at this line.')
30 @add_test_categories(['pyapi'])
31 @expectedFailureAndroid(archs=['arm', 'aarch64']) # Watchpoints not supported
32 @expectedFailureAll(oslist=["windows"], bugnumber="llvm.org/pr24446: WINDOWS XFAIL TRIAGE - Watchpoints not supported on Windows")
33 def test_watch_iter(self):
34 """Exercise SBTarget.watchpoint_iter() API to iterate on the available watchpoints."""
36 exe = os.path.join(os.getcwd(), "a.out")
38 # Create a target by the debugger.
39 target = self.dbg.CreateTarget(exe)
40 self.assertTrue(target, VALID_TARGET)
42 # Create a breakpoint on main.c in order to set our watchpoint later.
43 breakpoint = target.BreakpointCreateByLocation(self.source, self.line)
44 self.assertTrue(breakpoint and
45 breakpoint.GetNumLocations() == 1,
48 # Now launch the process, and do not stop at the entry point.
49 process = target.LaunchSimple (None, None, self.get_process_working_directory())
51 # We should be stopped due to the breakpoint. Get frame #0.
52 process = target.GetProcess()
53 self.assertTrue(process.GetState() == lldb.eStateStopped,
55 thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint)
56 frame0 = thread.GetFrameAtIndex(0)
58 # Watch 'global' for read and write.
59 value = frame0.FindValue('global', lldb.eValueTypeVariableGlobal)
60 error = lldb.SBError();
61 watchpoint = value.Watch(True, False, True, error)
62 self.assertTrue(value and watchpoint,
63 "Successfully found the variable and set a watchpoint")
64 self.DebugSBValue(value)
66 # Hide stdout if not running with '-t' option.
67 if not self.TraceOn():
70 # There should be only 1 watchpoint location under the target.
71 self.assertTrue(target.GetNumWatchpoints() == 1)
72 self.assertTrue(watchpoint.IsEnabled())
73 watch_id = watchpoint.GetID()
74 self.assertTrue(watch_id != 0)
76 # Continue. Expect the program to stop due to the variable being written to.
79 # Hide stdout if not running with '-t' option.
80 if not self.TraceOn():
83 # Print the stack traces.
84 lldbutil.print_stacktraces(process)
86 thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonWatchpoint)
87 self.assertTrue(thread, "The thread stopped due to watchpoint")
88 self.DebugSBValue(value)
90 # We currently only support hardware watchpoint. Verify that we have a
91 # meaningful hardware index at this point. Exercise the printed repr of
92 # SBWatchpointLocation.
94 self.assertTrue(watchpoint.GetHardwareIndex() != -1)
96 # SBWatchpoint.GetDescription() takes a description level arg.
97 print(lldbutil.get_description(watchpoint, lldb.eDescriptionLevelFull))
99 # Now disable the 'rw' watchpoint. The program won't stop when it reads
101 watchpoint.SetEnabled(False)
102 self.assertTrue(watchpoint.GetHardwareIndex() == -1)
103 self.assertFalse(watchpoint.IsEnabled())
105 # Continue. The program does not stop again when the variable is being
106 # read from because the watchpoint location has been disabled.
109 # At this point, the inferior process should have exited.
110 self.assertTrue(process.GetState() == lldb.eStateExited, PROCESS_EXITED)
112 # Verify some vital statistics and exercise the iterator API.
113 for watchpoint in target.watchpoint_iter():
114 self.assertTrue(watchpoint)
115 self.assertTrue(watchpoint.GetWatchSize() == 4)
116 self.assertTrue(watchpoint.GetHitCount() == 1)