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 17 mydir = TestBase.compute_mydir(__file__) 18 NO_DEBUG_INFO_TESTCASE = True 19 20 # hardware watchpoints are not reported with a hardware index # on armv7 on ios devices 21 def affected_by_radar_34564183(self): 22 return (self.getArchitecture() in ['armv7', 'armv7k', 'arm64_32']) and self.platformIsDarwin() 23 24 def setUp(self): 25 # Call super's setUp(). 26 TestBase.setUp(self) 27 # Our simple source filename. 28 self.source = 'main.c' 29 # Find the line number to break inside main(). 30 self.line = line_number( 31 self.source, '// Set break point at this line.') 32 33 @add_test_categories(['pyapi']) 34 def test_watch_iter(self): 35 """Exercise SBTarget.watchpoint_iter() API to iterate on the available watchpoints.""" 36 self.build() 37 exe = self.getBuildArtifact("a.out") 38 39 # Create a target by the debugger. 40 target = self.dbg.CreateTarget(exe) 41 self.assertTrue(target, VALID_TARGET) 42 43 # Create a breakpoint on main.c in order to set our watchpoint later. 44 breakpoint = target.BreakpointCreateByLocation(self.source, self.line) 45 self.assertTrue(breakpoint and 46 breakpoint.GetNumLocations() == 1, 47 VALID_BREAKPOINT) 48 49 # Now launch the process, and do not stop at the entry point. 50 process = target.LaunchSimple( 51 None, None, self.get_process_working_directory()) 52 53 # We should be stopped due to the breakpoint. Get frame #0. 54 process = target.GetProcess() 55 self.assertTrue(process.GetState() == lldb.eStateStopped, 56 PROCESS_STOPPED) 57 thread = lldbutil.get_stopped_thread( 58 process, lldb.eStopReasonBreakpoint) 59 frame0 = thread.GetFrameAtIndex(0) 60 61 # Watch 'global' for read and write. 62 value = frame0.FindValue('global', lldb.eValueTypeVariableGlobal) 63 error = lldb.SBError() 64 watchpoint = value.Watch(True, False, True, error) 65 self.assertTrue(value and watchpoint, 66 "Successfully found the variable and set a watchpoint") 67 self.DebugSBValue(value) 68 69 # Hide stdout if not running with '-t' option. 70 if not self.TraceOn(): 71 self.HideStdout() 72 73 # There should be only 1 watchpoint location under the target. 74 self.assertTrue(target.GetNumWatchpoints() == 1) 75 self.assertTrue(watchpoint.IsEnabled()) 76 watch_id = watchpoint.GetID() 77 self.assertTrue(watch_id != 0) 78 79 # Continue. Expect the program to stop due to the variable being 80 # written to. 81 process.Continue() 82 83 # Hide stdout if not running with '-t' option. 84 if not self.TraceOn(): 85 self.HideStdout() 86 87 # Print the stack traces. 88 lldbutil.print_stacktraces(process) 89 90 thread = lldbutil.get_stopped_thread( 91 process, lldb.eStopReasonWatchpoint) 92 self.assertTrue(thread, "The thread stopped due to watchpoint") 93 self.DebugSBValue(value) 94 95 # We currently only support hardware watchpoint. Verify that we have a 96 # meaningful hardware index at this point. Exercise the printed repr of 97 # SBWatchpointLocation. 98 print(watchpoint) 99 if not self.affected_by_radar_34564183(): 100 self.assertTrue(watchpoint.GetHardwareIndex() != -1) 101 102 # SBWatchpoint.GetDescription() takes a description level arg. 103 print(lldbutil.get_description(watchpoint, lldb.eDescriptionLevelFull)) 104 105 # Now disable the 'rw' watchpoint. The program won't stop when it reads 106 # 'global' next. 107 watchpoint.SetEnabled(False) 108 self.assertTrue(watchpoint.GetHardwareIndex() == -1) 109 self.assertFalse(watchpoint.IsEnabled()) 110 111 # Continue. The program does not stop again when the variable is being 112 # read from because the watchpoint location has been disabled. 113 process.Continue() 114 115 # At this point, the inferior process should have exited. 116 self.assertTrue( 117 process.GetState() == lldb.eStateExited, 118 PROCESS_EXITED) 119 120 # Verify some vital statistics and exercise the iterator API. 121 for watchpoint in target.watchpoint_iter(): 122 self.assertTrue(watchpoint) 123 self.assertTrue(watchpoint.GetWatchSize() == 4) 124 self.assertTrue(watchpoint.GetHitCount() == 1) 125 print(watchpoint) 126