1""" 2Use lldb Python SBValue.WatchPointee() API to create a watchpoint for write of '*g_char_ptr'. 3""" 4 5import os, time 6import re 7import unittest2 8import lldb, lldbutil 9from lldbtest import * 10 11class SetWatchlocationAPITestCase(TestBase): 12 13 mydir = os.path.join("python_api", "watchpoint", "watchlocation") 14 15 def setUp(self): 16 # Call super's setUp(). 17 TestBase.setUp(self) 18 # Our simple source filename. 19 self.source = 'main.cpp' 20 # Find the line number to break inside main(). 21 self.line = line_number(self.source, '// Set break point at this line.') 22 # This is for verifying that watch location works. 23 self.violating_func = "do_bad_thing_with_location"; 24 25 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin") 26 @python_api_test 27 @dsym_test 28 def test_watch_location_with_dsym(self): 29 """Exercise SBValue.WatchPointee() API to set a watchpoint.""" 30 self.buildDsym() 31 self.do_set_watchlocation() 32 33 @expectedFailureFreeBSD('llvm.org/pr16706') # Watchpoints fail on FreeBSD 34 @skipIfLinux # Sometimes passes, sometimes not. 35 @python_api_test 36 @dwarf_test 37 def test_watch_location_with_dwarf(self): 38 """Exercise SBValue.WatchPointee() API to set a watchpoint.""" 39 self.buildDwarf() 40 self.do_set_watchlocation() 41 42 def do_set_watchlocation(self): 43 """Use SBValue.WatchPointee() to set a watchpoint and verify that the program stops later due to the watchpoint.""" 44 exe = os.path.join(os.getcwd(), "a.out") 45 46 # Create a target by the debugger. 47 target = self.dbg.CreateTarget(exe) 48 self.assertTrue(target, VALID_TARGET) 49 50 # Now create a breakpoint on main.c. 51 breakpoint = target.BreakpointCreateByLocation(self.source, self.line) 52 self.assertTrue(breakpoint and 53 breakpoint.GetNumLocations() == 1, 54 VALID_BREAKPOINT) 55 56 # Now launch the process, and do not stop at the entry point. 57 process = target.LaunchSimple(None, None, os.getcwd()) 58 59 # We should be stopped due to the breakpoint. Get frame #0. 60 process = target.GetProcess() 61 self.assertTrue(process.GetState() == lldb.eStateStopped, 62 PROCESS_STOPPED) 63 thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint) 64 frame0 = thread.GetFrameAtIndex(0) 65 66 value = frame0.FindValue('g_char_ptr', 67 lldb.eValueTypeVariableGlobal) 68 pointee = value.CreateValueFromAddress("pointee", 69 value.GetValueAsUnsigned(0), 70 value.GetType().GetPointeeType()) 71 # Watch for write to *g_char_ptr. 72 error = lldb.SBError(); 73 watchpoint = value.WatchPointee(True, False, True, error) 74 self.assertTrue(value and watchpoint, 75 "Successfully found the pointer and set a watchpoint") 76 self.DebugSBValue(value) 77 self.DebugSBValue(pointee) 78 79 # Hide stdout if not running with '-t' option. 80 if not self.TraceOn(): 81 self.HideStdout() 82 83 print watchpoint 84 85 # Continue. Expect the program to stop due to the variable being written to. 86 process.Continue() 87 88 if (self.TraceOn()): 89 lldbutil.print_stacktraces(process) 90 91 thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonWatchpoint) 92 self.assertTrue(thread, "The thread stopped due to watchpoint") 93 self.DebugSBValue(value) 94 self.DebugSBValue(pointee) 95 96 self.expect(lldbutil.print_stacktrace(thread, string_buffer=True), exe=False, 97 substrs = [self.violating_func]) 98 99 # This finishes our test. 100 101 102if __name__ == '__main__': 103 import atexit 104 lldb.SBDebugger.Initialize() 105 atexit.register(lambda: lldb.SBDebugger.Terminate()) 106 unittest2.main() 107