def frame_utils(self): 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, os.getcwd()) if not process: self.fail("SBTarget.LaunchProcess() failed") self.assertTrue(process.GetState() == lldb.eStateStopped, PROCESS_STOPPED) import lldbutil thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint) frame0 = thread.GetFrameAtIndex(0) frame1 = thread.GetFrameAtIndex(1) 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 frame_utils(self): 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, os.getcwd()) if not process: self.fail("SBTarget.LaunchProcess() failed") self.assertTrue(process.GetState() == lldb.eStateStopped, PROCESS_STOPPED) import 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 = 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 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 stop_if_called_from_a(): # lldb.debugger_unique_id stores the id of the debugger associated with us. dbg = lldb.SBDebugger.FindDebuggerWithID(lldb.debugger_unique_id) # Perform synchronous interaction with the debugger. old_async = dbg.GetAsync() dbg.SetAsync(True) # Retrieve the target, process, and the only thread. target = dbg.GetSelectedTarget() process = target.GetProcess() thread = process.GetThreadAtIndex(0) # We check the call frames in order to stop only when the immediate caller # of the leaf function c() is a(). If it's not the right caller, we ask the # command interpreter to continue execution. print >> sys.stdout, "Checking call frames..." lldbutil.print_stacktrace(thread) should_stop = True if thread.GetNumFrames() >= 2: funcs = lldbutil.get_function_names(thread) print >> sys.stdout, funcs[0], "called from", funcs[1] if (funcs[0] == 'c' and funcs[1] == 'a'): should_stop = True else: process.Continue() should_stop = False dbg.SetAsync(old_async) return should_stop
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 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 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 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 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 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, os.getcwd()) 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 = process.GetThreadAtIndex(0) self.assertTrue(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 = 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 = process.GetThreadAtIndex(0) self.assertTrue(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))