130 lines · python
1"""2Use lldb Python SBTarget API to set read watchpoints3"""4 5import lldb6from lldbsuite.test.decorators import *7from lldbsuite.test.lldbtest import *8from lldbsuite.test import lldbutil9 10 11class SetReadOnlyWatchpointTestCase(TestBase):12 NO_DEBUG_INFO_TESTCASE = True13 14 def setUp(self):15 # Call super's setUp().16 TestBase.setUp(self)17 # Our simple source filename.18 self.source = "main.c"19 # Find the line number to break inside main().20 self.line = line_number(self.source, "// Set break point at this line.")21 self.build()22 23 # Intel hardware does not support read-only watchpoints24 @expectedFailureAll(archs=["i386", "x86_64"])25 def test_read_watchpoint_watch_address(self):26 exe = self.getBuildArtifact("a.out")27 28 target = self.dbg.CreateTarget(exe)29 self.assertTrue(target, VALID_TARGET)30 31 # Now create a breakpoint on main.c.32 breakpoint = target.BreakpointCreateByLocation(self.source, self.line)33 self.assertTrue(34 breakpoint and breakpoint.GetNumLocations() == 1, VALID_BREAKPOINT35 )36 37 # Now launch the process, and do not stop at the entry point.38 process = target.LaunchSimple(None, None, self.get_process_working_directory())39 40 # We should be stopped due to the breakpoint. Get frame #0.41 process = target.GetProcess()42 self.assertState(process.GetState(), lldb.eStateStopped, PROCESS_STOPPED)43 thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint)44 frame0 = thread.GetFrameAtIndex(0)45 46 value = frame0.FindValue("global", lldb.eValueTypeVariableGlobal)47 local = frame0.FindValue("local", lldb.eValueTypeVariableLocal)48 error = lldb.SBError()49 50 watchpoint = target.WatchAddress(value.GetLoadAddress(), 1, True, False, error)51 self.assertTrue(52 value and local and watchpoint,53 "Successfully found the values and set a watchpoint",54 )55 self.DebugSBValue(value)56 self.DebugSBValue(local)57 58 # Hide stdout if not running with '-t' option.59 if not self.TraceOn():60 self.HideStdout()61 62 print(watchpoint)63 64 # Continue. Expect the program to stop due to the variable being65 # read, but *not* written to.66 process.Continue()67 68 if self.TraceOn():69 lldbutil.print_stacktraces(process)70 71 self.assertTrue(72 local.GetValueAsSigned() > 0, "The local variable has been incremented"73 )74 75 # Intel hardware does not support read-only watchpoints76 @expectedFailureAll(archs=["i386", "x86_64"])77 def test_read_watchpoint_watch_create_by_address(self):78 exe = self.getBuildArtifact("a.out")79 80 target = self.dbg.CreateTarget(exe)81 self.assertTrue(target, VALID_TARGET)82 83 # Now create a breakpoint on main.c.84 breakpoint = target.BreakpointCreateByLocation(self.source, self.line)85 self.assertTrue(86 breakpoint and breakpoint.GetNumLocations() == 1, VALID_BREAKPOINT87 )88 89 # Now launch the process, and do not stop at the entry point.90 process = target.LaunchSimple(None, None, self.get_process_working_directory())91 92 # We should be stopped due to the breakpoint. Get frame #0.93 process = target.GetProcess()94 self.assertState(process.GetState(), lldb.eStateStopped, PROCESS_STOPPED)95 thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint)96 frame0 = thread.GetFrameAtIndex(0)97 98 value = frame0.FindValue("global", lldb.eValueTypeVariableGlobal)99 local = frame0.FindValue("local", lldb.eValueTypeVariableLocal)100 error = lldb.SBError()101 102 wp_opts = lldb.SBWatchpointOptions()103 wp_opts.SetWatchpointTypeRead(True)104 watchpoint = target.WatchpointCreateByAddress(105 value.GetLoadAddress(), 1, wp_opts, error106 )107 self.assertTrue(108 value and local and watchpoint,109 "Successfully found the values and set a watchpoint",110 )111 self.DebugSBValue(value)112 self.DebugSBValue(local)113 114 # Hide stdout if not running with '-t' option.115 if not self.TraceOn():116 self.HideStdout()117 118 print(watchpoint)119 120 # Continue. Expect the program to stop due to the variable being121 # read, but *not* written to.122 process.Continue()123 124 if self.TraceOn():125 lldbutil.print_stacktraces(process)126 127 self.assertTrue(128 local.GetValueAsSigned() > 0, "The local variable has been incremented"129 )130