1""" 2Use lldb Python SBtarget.WatchAddress() API to create a watchpoint for write of '*g_char_ptr'. 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 TargetWatchAddressAPITestCase(TestBase): 15 16 mydir = TestBase.compute_mydir(__file__) 17 NO_DEBUG_INFO_TESTCASE = True 18 19 def setUp(self): 20 # Call super's setUp(). 21 TestBase.setUp(self) 22 # Our simple source filename. 23 self.source = 'main.cpp' 24 # Find the line number to break inside main(). 25 self.line = line_number( 26 self.source, '// Set break point at this line.') 27 # This is for verifying that watch location works. 28 self.violating_func = "do_bad_thing_with_location" 29 30 def test_watch_address(self): 31 """Exercise SBTarget.WatchAddress() API to set a watchpoint.""" 32 self.build() 33 exe = self.getBuildArtifact("a.out") 34 35 # Create a target by the debugger. 36 target = self.dbg.CreateTarget(exe) 37 self.assertTrue(target, VALID_TARGET) 38 39 # Now create a breakpoint on main.c. 40 breakpoint = target.BreakpointCreateByLocation(self.source, self.line) 41 self.assertTrue(breakpoint and 42 breakpoint.GetNumLocations() == 1, 43 VALID_BREAKPOINT) 44 45 # Now launch the process, and do not stop at the entry point. 46 process = target.LaunchSimple( 47 None, None, self.get_process_working_directory()) 48 49 # We should be stopped due to the breakpoint. Get frame #0. 50 process = target.GetProcess() 51 self.assertEqual(process.GetState(), lldb.eStateStopped, 52 PROCESS_STOPPED) 53 thread = lldbutil.get_stopped_thread( 54 process, lldb.eStopReasonBreakpoint) 55 frame0 = thread.GetFrameAtIndex(0) 56 57 value = frame0.FindValue('g_char_ptr', 58 lldb.eValueTypeVariableGlobal) 59 pointee = value.CreateValueFromAddress( 60 "pointee", 61 value.GetValueAsUnsigned(0), 62 value.GetType().GetPointeeType()) 63 # Watch for write to *g_char_ptr. 64 error = lldb.SBError() 65 watchpoint = target.WatchAddress( 66 value.GetValueAsUnsigned(), 1, False, True, error) 67 self.assertTrue(value and watchpoint, 68 "Successfully found the pointer and set a watchpoint") 69 self.DebugSBValue(value) 70 self.DebugSBValue(pointee) 71 72 # Hide stdout if not running with '-t' option. 73 if not self.TraceOn(): 74 self.HideStdout() 75 76 print(watchpoint) 77 78 # Continue. Expect the program to stop due to the variable being 79 # written to. 80 process.Continue() 81 82 if (self.TraceOn()): 83 lldbutil.print_stacktraces(process) 84 85 thread = lldbutil.get_stopped_thread( 86 process, lldb.eStopReasonWatchpoint) 87 self.assertTrue(thread, "The thread stopped due to watchpoint") 88 self.DebugSBValue(value) 89 self.DebugSBValue(pointee) 90 91 self.expect( 92 lldbutil.print_stacktrace( 93 thread, 94 string_buffer=True), 95 exe=False, 96 substrs=[ 97 self.violating_func]) 98 99 # This finishes our test. 100 101 # No size constraint on MIPS for watches 102 @skipIf(archs=['mips', 'mipsel', 'mips64', 'mips64el']) 103 @skipIf(archs=['s390x']) # Likewise on SystemZ 104 def test_watch_address_with_invalid_watch_size(self): 105 """Exercise SBTarget.WatchAddress() API but pass an invalid watch_size.""" 106 self.build() 107 exe = self.getBuildArtifact("a.out") 108 109 # Create a target by the debugger. 110 target = self.dbg.CreateTarget(exe) 111 self.assertTrue(target, VALID_TARGET) 112 113 # Now create a breakpoint on main.c. 114 breakpoint = target.BreakpointCreateByLocation(self.source, self.line) 115 self.assertTrue(breakpoint and 116 breakpoint.GetNumLocations() == 1, 117 VALID_BREAKPOINT) 118 119 # Now launch the process, and do not stop at the entry point. 120 process = target.LaunchSimple( 121 None, None, self.get_process_working_directory()) 122 123 # We should be stopped due to the breakpoint. Get frame #0. 124 process = target.GetProcess() 125 self.assertEqual(process.GetState(), lldb.eStateStopped, 126 PROCESS_STOPPED) 127 thread = lldbutil.get_stopped_thread( 128 process, lldb.eStopReasonBreakpoint) 129 frame0 = thread.GetFrameAtIndex(0) 130 131 value = frame0.FindValue('g_char_ptr', 132 lldb.eValueTypeVariableGlobal) 133 pointee = value.CreateValueFromAddress( 134 "pointee", 135 value.GetValueAsUnsigned(0), 136 value.GetType().GetPointeeType()) 137 # Watch for write to *g_char_ptr. 138 error = lldb.SBError() 139 watchpoint = target.WatchAddress( 140 value.GetValueAsUnsigned(), 365, False, True, error) 141 self.assertFalse(watchpoint) 142 self.expect(error.GetCString(), exe=False, 143 substrs=['watch size of %d is not supported' % 365]) 144