1""" 2Use lldb Python SBTarget API to iterate on the watchpoint(s) for the target. 3""" 4 5from __future__ import print_function 6 7 8 9import lldb 10from lldbsuite.test.decorators import * 11from lldbsuite.test.lldbtest import * 12from lldbsuite.test import lldbutil 13 14 15class WatchpointIteratorTestCase(TestBase): 16 NO_DEBUG_INFO_TESTCASE = True 17 18 # hardware watchpoints are not reported with a hardware index # on armv7 on ios devices 19 def affected_by_radar_34564183(self): 20 return (self.getArchitecture() in ['armv7', 'armv7k', 'arm64_32']) and self.platformIsDarwin() 21 22 def setUp(self): 23 # Call super's setUp(). 24 TestBase.setUp(self) 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.') 30 31 def test_watch_iter(self): 32 """Exercise SBTarget.watchpoint_iter() API to iterate on the available watchpoints.""" 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.assertState(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, False, 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 self.assertTrue(watchpoint.IsEnabled()) 73 watch_id = watchpoint.GetID() 74 self.assertTrue(watch_id != 0) 75 76 # Continue. Expect the program to stop due to the variable being 77 # written to. 78 process.Continue() 79 80 # Hide stdout if not running with '-t' option. 81 if not self.TraceOn(): 82 self.HideStdout() 83 84 # Print the stack traces. 85 lldbutil.print_stacktraces(process) 86 87 thread = lldbutil.get_stopped_thread( 88 process, lldb.eStopReasonWatchpoint) 89 self.assertTrue(thread, "The thread stopped due to watchpoint") 90 self.DebugSBValue(value) 91 92 # We currently only support hardware watchpoint. Verify that we have a 93 # meaningful hardware index at this point. Exercise the printed repr of 94 # SBWatchpointLocation. 95 print(watchpoint) 96 if not self.affected_by_radar_34564183(): 97 self.assertTrue(watchpoint.GetHardwareIndex() != -1) 98 99 # SBWatchpoint.GetDescription() takes a description level arg. 100 print(lldbutil.get_description(watchpoint, lldb.eDescriptionLevelFull)) 101 102 # Now disable the 'rw' watchpoint. The program won't stop when it reads 103 # 'global' next. 104 watchpoint.SetEnabled(False) 105 self.assertEqual(watchpoint.GetHardwareIndex(), -1) 106 self.assertFalse(watchpoint.IsEnabled()) 107 108 # Continue. The program does not stop again when the variable is being 109 # read from because the watchpoint location has been disabled. 110 process.Continue() 111 112 # At this point, the inferior process should have exited. 113 self.assertEqual( 114 process.GetState(), lldb.eStateExited, 115 PROCESS_EXITED) 116 117 # Verify some vital statistics and exercise the iterator API. 118 for watchpoint in target.watchpoint_iter(): 119 self.assertTrue(watchpoint) 120 self.assertEqual(watchpoint.GetWatchSize(), 4) 121 self.assertEqual(watchpoint.GetHitCount(), 1) 122 print(watchpoint) 123