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