def test_frame_utils(self): """Test utility functions for the frame object.""" self.build() exe = os.path.join(os.getcwd(), "a.out") target = self.dbg.CreateTarget(exe) self.assertTrue(target, VALID_TARGET) breakpoint = target.BreakpointCreateByLocation("main.c", self.line) self.assertTrue(breakpoint, VALID_BREAKPOINT) # Now launch the process, and do not stop at entry point. process = target.LaunchSimple (None, None, self.get_process_working_directory()) if not process: self.fail("SBTarget.LaunchProcess() failed") self.assertTrue(process.GetState() == lldb.eStateStopped, PROCESS_STOPPED) import lldbsuite.test.lldbutil as lldbutil thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint) self.assertTrue (thread) frame0 = thread.GetFrameAtIndex(0) self.assertTrue (frame0) frame1 = thread.GetFrameAtIndex(1) self.assertTrue (frame1) parent = lldbutil.get_parent_frame(frame0) self.assertTrue(parent and parent.GetFrameID() == frame1.GetFrameID()) frame0_args = lldbutil.get_args_as_string(frame0) parent_args = lldbutil.get_args_as_string(parent) self.assertTrue(frame0_args and parent_args and "(int)val=1" in frame0_args) if self.TraceOn(): lldbutil.print_stacktrace(thread) print("Current frame: %s" % frame0_args) print("Parent frame: %s" % parent_args)
def test_lldb_iter_frame(self): """Test iterator works correctly for SBProcess->SBThread->SBFrame.""" self.build() exe = self.getBuildArtifact("a.out") target = self.dbg.CreateTarget(exe) self.assertTrue(target, VALID_TARGET) breakpoint = target.BreakpointCreateByLocation("main.cpp", self.line1) self.assertTrue(breakpoint, VALID_BREAKPOINT) # Now launch the process, and do not stop at entry point. process = target.LaunchSimple( None, None, self.get_process_working_directory()) if not process: self.fail("SBTarget.LaunchProcess() failed") from lldbsuite.test.lldbutil import print_stacktrace stopped_due_to_breakpoint = False for thread in process: if self.TraceOn(): print_stacktrace(thread) ID = thread.GetThreadID() if thread.GetStopReason() == lldb.eStopReasonBreakpoint: stopped_due_to_breakpoint = True for frame in thread: self.assertTrue(frame.GetThread().GetThreadID() == ID) if self.TraceOn(): print(frame) self.assertTrue(stopped_due_to_breakpoint)
def recursive_inferior_crashing_python(self): """Inferior crashes upon launching; lldb should catch the event and stop.""" exe = os.path.join(os.getcwd(), "a.out") target = self.dbg.CreateTarget(exe) self.assertTrue(target, VALID_TARGET) # Now launch the process, and do not stop at entry point. # Both argv and envp are null. process = target.LaunchSimple( None, None, self.get_process_working_directory()) if process.GetState() != lldb.eStateStopped: self.fail("Process should be in the 'stopped' state, " "instead the actual state is: '%s'" % lldbutil.state_type_to_str(process.GetState())) threads = lldbutil.get_crashed_threads(self, process) self.assertEqual( len(threads), 1, "Failed to stop the thread upon bad access exception") if self.TraceOn(): lldbutil.print_stacktrace(threads[0])
def test_lldb_iter_frame(self): """Test iterator works correctly for SBProcess->SBThread->SBFrame.""" self.build() exe = os.path.join(os.getcwd(), "a.out") target = self.dbg.CreateTarget(exe) self.assertTrue(target, VALID_TARGET) breakpoint = target.BreakpointCreateByLocation("main.cpp", self.line1) self.assertTrue(breakpoint, VALID_BREAKPOINT) # Now launch the process, and do not stop at entry point. process = target.LaunchSimple( None, None, self.get_process_working_directory()) if not process: self.fail("SBTarget.LaunchProcess() failed") from lldbsuite.test.lldbutil import print_stacktrace stopped_due_to_breakpoint = False for thread in process: if self.TraceOn(): print_stacktrace(thread) ID = thread.GetThreadID() if thread.GetStopReason() == lldb.eStopReasonBreakpoint: stopped_due_to_breakpoint = True for frame in thread: self.assertTrue(frame.GetThread().GetThreadID() == ID) if self.TraceOn(): print(frame) self.assertTrue(stopped_due_to_breakpoint)
def inferior_crashing_python(self): """Inferior crashes upon launching; lldb should catch the event and stop.""" exe = os.path.join(os.getcwd(), "a.out") target = self.dbg.CreateTarget(exe) self.assertTrue(target, VALID_TARGET) # Now launch the process, and do not stop at entry point. # Both argv and envp are null. process = target.LaunchSimple( None, None, self.get_process_working_directory()) if process.GetState() != lldb.eStateStopped: self.fail("Process should be in the 'stopped' state, " "instead the actual state is: '%s'" % lldbutil.state_type_to_str(process.GetState())) threads = lldbutil.get_crashed_threads(self, process) self.assertEqual( len(threads), 1, "Failed to stop the thread upon bad access exception") if self.TraceOn(): lldbutil.print_stacktrace(threads[0])
def test_watch_location(self): """Exercise SBValue.WatchPointee() API to set a watchpoint.""" self.build() exe = os.path.join(os.getcwd(), "a.out") # Create a target by the debugger. target = self.dbg.CreateTarget(exe) self.assertTrue(target, VALID_TARGET) # Now create a breakpoint on main.c. breakpoint = target.BreakpointCreateByLocation(self.source, self.line) self.assertTrue(breakpoint and breakpoint.GetNumLocations() == 1, VALID_BREAKPOINT) # Now launch the process, and do not stop at the entry point. process = target.LaunchSimple(None, None, self.get_process_working_directory()) # We should be stopped due to the breakpoint. Get frame #0. process = target.GetProcess() self.assertTrue(process.GetState() == lldb.eStateStopped, PROCESS_STOPPED) thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint) frame0 = thread.GetFrameAtIndex(0) value = frame0.FindValue('g_char_ptr', lldb.eValueTypeVariableGlobal) pointee = value.CreateValueFromAddress( "pointee", value.GetValueAsUnsigned(0), value.GetType().GetPointeeType()) # Watch for write to *g_char_ptr. error = lldb.SBError() watchpoint = value.WatchPointee(True, False, True, error) self.assertTrue(value and watchpoint, "Successfully found the pointer and set a watchpoint") self.DebugSBValue(value) self.DebugSBValue(pointee) # Hide stdout if not running with '-t' option. if not self.TraceOn(): self.HideStdout() print(watchpoint) # Continue. Expect the program to stop due to the variable being # written to. process.Continue() if (self.TraceOn()): lldbutil.print_stacktraces(process) thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonWatchpoint) self.assertTrue(thread, "The thread stopped due to watchpoint") self.DebugSBValue(value) self.DebugSBValue(pointee) self.expect(lldbutil.print_stacktrace(thread, string_buffer=True), exe=False, substrs=[self.violating_func])
def test_watch_address(self): """Exercise SBTarget.WatchAddress() API to set a watchpoint.""" self.build() exe = os.path.join(os.getcwd(), "a.out") # Create a target by the debugger. target = self.dbg.CreateTarget(exe) self.assertTrue(target, VALID_TARGET) # Now create a breakpoint on main.c. breakpoint = target.BreakpointCreateByLocation(self.source, self.line) self.assertTrue(breakpoint and breakpoint.GetNumLocations() == 1, VALID_BREAKPOINT) # Now launch the process, and do not stop at the entry point. process = target.LaunchSimple (None, None, self.get_process_working_directory()) # We should be stopped due to the breakpoint. Get frame #0. process = target.GetProcess() self.assertTrue(process.GetState() == lldb.eStateStopped, PROCESS_STOPPED) thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint) frame0 = thread.GetFrameAtIndex(0) value = frame0.FindValue('g_char_ptr', lldb.eValueTypeVariableGlobal) pointee = value.CreateValueFromAddress("pointee", value.GetValueAsUnsigned(0), value.GetType().GetPointeeType()) # Watch for write to *g_char_ptr. error = lldb.SBError(); watchpoint = target.WatchAddress(value.GetValueAsUnsigned(), 1, False, True, error) self.assertTrue(value and watchpoint, "Successfully found the pointer and set a watchpoint") self.DebugSBValue(value) self.DebugSBValue(pointee) # Hide stdout if not running with '-t' option. if not self.TraceOn(): self.HideStdout() print(watchpoint) # Continue. Expect the program to stop due to the variable being written to. process.Continue() if (self.TraceOn()): lldbutil.print_stacktraces(process) thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonWatchpoint) self.assertTrue(thread, "The thread stopped due to watchpoint") self.DebugSBValue(value) self.DebugSBValue(pointee) self.expect(lldbutil.print_stacktrace(thread, string_buffer=True), exe=False, substrs = [self.violating_func])
def inferior_asserting_python(self): """Inferior asserts upon launching; lldb should catch the event and stop.""" exe = os.path.join(os.getcwd(), "a.out") target = self.dbg.CreateTarget(exe) self.assertTrue(target, VALID_TARGET) # Now launch the process, and do not stop at entry point. # Both argv and envp are null. process = target.LaunchSimple (None, None, self.get_process_working_directory()) if process.GetState() != lldb.eStateStopped: self.fail("Process should be in the 'stopped' state, " "instead the actual state is: '%s'" % lldbutil.state_type_to_str(process.GetState())) thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonSignal) if not thread: self.fail("Fail to stop the thread upon assert") if self.TraceOn(): lldbutil.print_stacktrace(thread)
def inferior_asserting_python(self): """Inferior asserts upon launching; lldb should catch the event and stop.""" exe = self.getBuildArtifact("a.out") target = self.dbg.CreateTarget(exe) self.assertTrue(target, VALID_TARGET) # Now launch the process, and do not stop at entry point. # Both argv and envp are null. process = target.LaunchSimple(None, None, self.get_process_working_directory()) if process.GetState() != lldb.eStateStopped: self.fail("Process should be in the 'stopped' state, " "instead the actual state is: '%s'" % lldbutil.state_type_to_str(process.GetState())) thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonSignal) if not thread: self.fail("Fail to stop the thread upon assert") if self.TraceOn(): lldbutil.print_stacktrace(thread)
def test_frame_api_IsEqual(self): """Exercise SBFrame API IsEqual.""" self.build() exe = os.path.join(os.getcwd(), "a.out") # Create a target by the debugger. target = self.dbg.CreateTarget(exe) self.assertTrue(target, VALID_TARGET) # Now create a breakpoint on main.c by name 'c'. breakpoint = target.BreakpointCreateByName('c', 'a.out') #print("breakpoint:", breakpoint) self.assertTrue(breakpoint and breakpoint.GetNumLocations() == 1, VALID_BREAKPOINT) # Now launch the process, and do not stop at the entry point. process = target.LaunchSimple( None, None, self.get_process_working_directory()) process = target.GetProcess() self.assertTrue(process.GetState() == lldb.eStateStopped, PROCESS_STOPPED) thread = lldbutil.get_stopped_thread( process, lldb.eStopReasonBreakpoint) self.assertIsNotNone(thread) frameEntered = thread.GetFrameAtIndex(0) if self.TraceOn(): print(frameEntered) lldbutil.print_stacktrace(thread) self.assertTrue(frameEntered) # Doing two step overs while still inside c(). thread.StepOver() thread.StepOver() self.assertTrue(thread) frameNow = thread.GetFrameAtIndex(0) if self.TraceOn(): print(frameNow) lldbutil.print_stacktrace(thread) self.assertTrue(frameNow) # The latest two frames are considered equal. self.assertTrue(frameEntered.IsEqual(frameNow)) # Now let's step out of frame c(). thread.StepOutOfFrame(frameNow) frameOutOfC = thread.GetFrameAtIndex(0) if self.TraceOn(): print(frameOutOfC) lldbutil.print_stacktrace(thread) self.assertTrue(frameOutOfC) # The latest two frames should not be equal. self.assertFalse(frameOutOfC.IsEqual(frameNow))
def test_frame_api_IsEqual(self): """Exercise SBFrame API IsEqual.""" self.build() exe = self.getBuildArtifact("a.out") # Create a target by the debugger. target = self.dbg.CreateTarget(exe) self.assertTrue(target, VALID_TARGET) # Now create a breakpoint on main.c by name 'c'. breakpoint = target.BreakpointCreateByName('c', 'a.out') self.trace("breakpoint:", breakpoint) self.assertTrue(breakpoint and breakpoint.GetNumLocations() == 1, VALID_BREAKPOINT) # Now launch the process, and do not stop at the entry point. process = target.LaunchSimple( None, None, self.get_process_working_directory()) process = target.GetProcess() self.assertEqual(process.GetState(), lldb.eStateStopped, PROCESS_STOPPED) thread = lldbutil.get_stopped_thread( process, lldb.eStopReasonBreakpoint) self.assertIsNotNone(thread) frameEntered = thread.GetFrameAtIndex(0) if self.TraceOn(): print(frameEntered) lldbutil.print_stacktrace(thread) self.assertTrue(frameEntered) # Doing two step overs while still inside c(). thread.StepOver() thread.StepOver() self.assertTrue(thread) frameNow = thread.GetFrameAtIndex(0) if self.TraceOn(): print(frameNow) lldbutil.print_stacktrace(thread) self.assertTrue(frameNow) # The latest two frames are considered equal. self.assertTrue(frameEntered.IsEqual(frameNow)) # Now let's step out of frame c(). thread.StepOutOfFrame(frameNow) frameOutOfC = thread.GetFrameAtIndex(0) if self.TraceOn(): print(frameOutOfC) lldbutil.print_stacktrace(thread) self.assertTrue(frameOutOfC) # The latest two frames should not be equal. self.assertFalse(frameOutOfC.IsEqual(frameNow))