2 Use lldb Python SBTarget API to iterate on the watchpoint(s) for the target.
5 from __future__ import print_function
13 from lldbsuite.test.decorators import *
14 from lldbsuite.test.lldbtest import *
15 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(
29 self.source, '// Set break point at this line.')
31 @add_test_categories(['pyapi'])
32 # Watchpoints not supported
33 @expectedFailureAndroid(archs=['arm', 'aarch64'])
36 bugnumber="llvm.org/pr24446: WINDOWS XFAIL TRIAGE - Watchpoints not supported on Windows")
37 def test_watch_iter(self):
38 """Exercise SBTarget.watchpoint_iter() API to iterate on the available watchpoints."""
40 exe = os.path.join(os.getcwd(), "a.out")
42 # Create a target by the debugger.
43 target = self.dbg.CreateTarget(exe)
44 self.assertTrue(target, VALID_TARGET)
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,
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())
56 # We should be stopped due to the breakpoint. Get frame #0.
57 process = target.GetProcess()
58 self.assertTrue(process.GetState() == lldb.eStateStopped,
60 thread = lldbutil.get_stopped_thread(
61 process, lldb.eStopReasonBreakpoint)
62 frame0 = thread.GetFrameAtIndex(0)
64 # Watch 'global' for read and write.
65 value = frame0.FindValue('global', lldb.eValueTypeVariableGlobal)
66 error = lldb.SBError()
67 watchpoint = value.Watch(True, False, True, error)
68 self.assertTrue(value and watchpoint,
69 "Successfully found the variable and set a watchpoint")
70 self.DebugSBValue(value)
72 # Hide stdout if not running with '-t' option.
73 if not self.TraceOn():
76 # There should be only 1 watchpoint location under the target.
77 self.assertTrue(target.GetNumWatchpoints() == 1)
78 self.assertTrue(watchpoint.IsEnabled())
79 watch_id = watchpoint.GetID()
80 self.assertTrue(watch_id != 0)
82 # Continue. Expect the program to stop due to the variable being
86 # Hide stdout if not running with '-t' option.
87 if not self.TraceOn():
90 # Print the stack traces.
91 lldbutil.print_stacktraces(process)
93 thread = lldbutil.get_stopped_thread(
94 process, lldb.eStopReasonWatchpoint)
95 self.assertTrue(thread, "The thread stopped due to watchpoint")
96 self.DebugSBValue(value)
98 # We currently only support hardware watchpoint. Verify that we have a
99 # meaningful hardware index at this point. Exercise the printed repr of
100 # SBWatchpointLocation.
102 self.assertTrue(watchpoint.GetHardwareIndex() != -1)
104 # SBWatchpoint.GetDescription() takes a description level arg.
105 print(lldbutil.get_description(watchpoint, lldb.eDescriptionLevelFull))
107 # Now disable the 'rw' watchpoint. The program won't stop when it reads
109 watchpoint.SetEnabled(False)
110 self.assertTrue(watchpoint.GetHardwareIndex() == -1)
111 self.assertFalse(watchpoint.IsEnabled())
113 # Continue. The program does not stop again when the variable is being
114 # read from because the watchpoint location has been disabled.
117 # At this point, the inferior process should have exited.
119 process.GetState() == lldb.eStateExited,
122 # Verify some vital statistics and exercise the iterator API.
123 for watchpoint in target.watchpoint_iter():
124 self.assertTrue(watchpoint)
125 self.assertTrue(watchpoint.GetWatchSize() == 4)
126 self.assertTrue(watchpoint.GetHitCount() == 1)