def cpp_exceptions(self): """Test lldb exception breakpoint command for CPP.""" exe = os.path.join(os.getcwd(), "a.out") # Create a target from the debugger. target = self.dbg.CreateTarget(exe) self.assertTrue(target, VALID_TARGET) exception_bkpt = target.BreakpointCreateForException( lldb.eLanguageTypeC_plus_plus, True, True) self.assertTrue(exception_bkpt, "Made an exception breakpoint") # Now run, and make sure we hit our breakpoint: process = target.LaunchSimple(None, None, self.get_process_working_directory()) self.assertTrue(process, "Got a valid process") stopped_threads = [] stopped_threads = lldbutil.get_threads_stopped_at_breakpoint( process, exception_bkpt) self.assertTrue( len(stopped_threads) == 1, "Stopped at our exception breakpoint.") thread = stopped_threads[0] # Make sure our throw function is still above us on the stack: frame_functions = lldbutil.get_function_names(thread) self.assertTrue( frame_functions.count("throws_exception_on_even(int)") == 1, "Our throw function is still on the stack.") # Okay we hit our exception throw breakpoint, now make sure we get our catch breakpoint. # One potential complication is that we might hit a couple of the exception breakpoints in getting out of the throw. # so loop till we don't see the throws function on the stack. We should stop one more time for our exception breakpoint # and that should be the catch... while frame_functions.count("throws_exception_on_even(int)") == 1: stopped_threads = lldbutil.continue_to_breakpoint( process, exception_bkpt) self.assertTrue(len(stopped_threads) == 1) thread = stopped_threads[0] frame_functions = lldbutil.get_function_names(thread) self.assertTrue( frame_functions.count("throws_exception_on_even(int)") == 0, "At catch our throw function is off the stack") self.assertTrue( frame_functions.count("intervening_function(int)") == 0, "At catch our intervening function is off the stack") self.assertTrue( frame_functions.count("catches_exception(int)") == 1, "At catch our catch function is on the stack")
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(self): """Test lldb exception breakpoint command for CPP.""" self.build() exe = os.path.join(os.getcwd(), "a.out") # Create a target from the debugger. target = self.dbg.CreateTarget (exe) self.assertTrue(target, VALID_TARGET) exception_bkpt = target.BreakpointCreateForException (lldb.eLanguageTypeC_plus_plus, True, True) self.assertTrue (exception_bkpt, "Made an exception breakpoint") # Now run, and make sure we hit our breakpoint: process = target.LaunchSimple (None, None, self.get_process_working_directory()) self.assertTrue (process, "Got a valid process") stopped_threads = [] stopped_threads = lldbutil.get_threads_stopped_at_breakpoint (process, exception_bkpt) self.assertTrue (len(stopped_threads) == 1, "Stopped at our exception breakpoint.") thread = stopped_threads[0] # Make sure our throw function is still above us on the stack: frame_functions = lldbutil.get_function_names(thread) self.assertTrue (frame_functions.count ("throws_exception_on_even(int)") == 1, "Our throw function is still on the stack.") # Okay we hit our exception throw breakpoint, now make sure we get our catch breakpoint. # One potential complication is that we might hit a couple of the exception breakpoints in getting out of the throw. # so loop till we don't see the throws function on the stack. We should stop one more time for our exception breakpoint # and that should be the catch... while frame_functions.count ("throws_exception_on_even(int)") == 1: stopped_threads = lldbutil.continue_to_breakpoint (process, exception_bkpt) self.assertTrue (len(stopped_threads) == 1) thread = stopped_threads[0] frame_functions = lldbutil.get_function_names(thread) self.assertTrue (frame_functions.count ("throws_exception_on_even(int)") == 0, "At catch our throw function is off the stack") self.assertTrue (frame_functions.count ("intervening_function(int)") == 0, "At catch our intervening function is off the stack") self.assertTrue (frame_functions.count ("catches_exception(int)") == 1, "At catch our catch function is on the stack")
def test_evaluate_expression_python(self): """Test SBFrame.EvaluateExpression() API for evaluating an expression.""" self.buildDefault() exe = os.path.join(os.getcwd(), "a.out") target = self.dbg.CreateTarget(exe) self.assertTrue(target, VALID_TARGET) # Create the breakpoint. filespec = lldb.SBFileSpec("main.cpp", False) breakpoint = target.BreakpointCreateByLocation(filespec, self.line) self.assertTrue(breakpoint, VALID_BREAKPOINT) # Verify the breakpoint just created. self.expect(str(breakpoint), BREAKPOINT_CREATED, exe=False, substrs = ['main.cpp', str(self.line)]) # Launch the process, and do not stop at the entry point. # Pass 'X Y Z' as the args, which makes argc == 4. process = target.LaunchSimple (['X', 'Y', 'Z'], None, self.get_process_working_directory()) if not process: self.fail("SBTarget.LaunchProcess() failed") 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())) # The stop reason of the thread should be breakpoint. thread = process.GetThreadAtIndex(0) if thread.GetStopReason() != lldb.eStopReasonBreakpoint: from lldbutil import stop_reason_to_str self.fail(STOPPED_DUE_TO_BREAKPOINT_WITH_STOP_REASON_AS % stop_reason_to_str(thread.GetStopReason())) # The filename of frame #0 should be 'main.cpp' and function is main. self.expect(lldbutil.get_filenames(thread)[0], "Break correctly at main.cpp", exe=False, startstr = "main.cpp") self.expect(lldbutil.get_function_names(thread)[0], "Break correctly at main()", exe=False, startstr = "main") # We should be stopped on the breakpoint with a hit count of 1. self.assertTrue(breakpoint.GetHitCount() == 1, BREAKPOINT_HIT_ONCE) # # Use Python API to evaluate expressions while stopped in a stack frame. # frame = thread.GetFrameAtIndex(0) val = frame.EvaluateExpression("2.234") self.expect(val.GetValue(), "2.345 evaluated correctly", exe=False, startstr = "2.234") self.expect(val.GetTypeName(), "2.345 evaluated correctly", exe=False, startstr = "double") self.DebugSBValue(val) val = frame.EvaluateExpression("argc") self.expect(val.GetValue(), "Argc evaluated correctly", exe=False, startstr = "4") self.DebugSBValue(val) val = frame.EvaluateExpression("*argv[1]") self.expect(val.GetValue(), "Argv[1] evaluated correctly", exe=False, startstr = "'X'") self.DebugSBValue(val) val = frame.EvaluateExpression("*argv[2]") self.expect(val.GetValue(), "Argv[2] evaluated correctly", exe=False, startstr = "'Y'") self.DebugSBValue(val) val = frame.EvaluateExpression("*argv[3]") self.expect(val.GetValue(), "Argv[3] evaluated correctly", exe=False, startstr = "'Z'") self.DebugSBValue(val) callee_break = target.BreakpointCreateByName ("a_function_to_call", None) self.assertTrue(callee_break.GetNumLocations() > 0) # Make sure ignoring breakpoints works from the command line: self.expect("expression -i true -- a_function_to_call()", substrs = ['(int) $', ' 1']) self.assertTrue (callee_break.GetHitCount() == 1) # Now try ignoring breakpoints using the SB API's: options = lldb.SBExpressionOptions() options.SetIgnoreBreakpoints(True) value = frame.EvaluateExpression('a_function_to_call()', options) self.assertTrue (value.IsValid()) self.assertTrue (value.GetValueAsSigned(0) == 2) self.assertTrue (callee_break.GetHitCount() == 2)
def test_evaluate_expression_python(self): """Test SBFrame.EvaluateExpression() API for evaluating an expression.""" self.buildDefault() exe = os.path.join(os.getcwd(), "a.out") target = self.dbg.CreateTarget(exe) self.assertTrue(target, VALID_TARGET) # Create the breakpoint. filespec = lldb.SBFileSpec("main.cpp", False) breakpoint = target.BreakpointCreateByLocation(filespec, self.line) self.assertTrue(breakpoint, VALID_BREAKPOINT) # Verify the breakpoint just created. self.expect(str(breakpoint), BREAKPOINT_CREATED, exe=False, substrs=['main.cpp', str(self.line)]) # Launch the process, and do not stop at the entry point. # Pass 'X Y Z' as the args, which makes argc == 4. process = target.LaunchSimple(['X', 'Y', 'Z'], None, self.get_process_working_directory()) if not process: self.fail("SBTarget.LaunchProcess() failed") 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())) # The stop reason of the thread should be breakpoint. thread = process.GetThreadAtIndex(0) if thread.GetStopReason() != lldb.eStopReasonBreakpoint: from lldbutil import stop_reason_to_str self.fail(STOPPED_DUE_TO_BREAKPOINT_WITH_STOP_REASON_AS % stop_reason_to_str(thread.GetStopReason())) # The filename of frame #0 should be 'main.cpp' and function is main. self.expect(lldbutil.get_filenames(thread)[0], "Break correctly at main.cpp", exe=False, startstr="main.cpp") self.expect(lldbutil.get_function_names(thread)[0], "Break correctly at main()", exe=False, startstr="main") # We should be stopped on the breakpoint with a hit count of 1. self.assertTrue(breakpoint.GetHitCount() == 1, BREAKPOINT_HIT_ONCE) # # Use Python API to evaluate expressions while stopped in a stack frame. # frame = thread.GetFrameAtIndex(0) val = frame.EvaluateExpression("2.234") self.expect(val.GetValue(), "2.345 evaluated correctly", exe=False, startstr="2.234") self.expect(val.GetTypeName(), "2.345 evaluated correctly", exe=False, startstr="double") self.DebugSBValue(val) val = frame.EvaluateExpression("argc") self.expect(val.GetValue(), "Argc evaluated correctly", exe=False, startstr="4") self.DebugSBValue(val) val = frame.EvaluateExpression("*argv[1]") self.expect(val.GetValue(), "Argv[1] evaluated correctly", exe=False, startstr="'X'") self.DebugSBValue(val) val = frame.EvaluateExpression("*argv[2]") self.expect(val.GetValue(), "Argv[2] evaluated correctly", exe=False, startstr="'Y'") self.DebugSBValue(val) val = frame.EvaluateExpression("*argv[3]") self.expect(val.GetValue(), "Argv[3] evaluated correctly", exe=False, startstr="'Z'") self.DebugSBValue(val) callee_break = target.BreakpointCreateByName("a_function_to_call", None) self.assertTrue(callee_break.GetNumLocations() > 0) # Make sure ignoring breakpoints works from the command line: self.expect("expression -i true -- a_function_to_call()", substrs=['(int) $', ' 1']) self.assertTrue(callee_break.GetHitCount() == 1) # Now try ignoring breakpoints using the SB API's: options = lldb.SBExpressionOptions() options.SetIgnoreBreakpoints(True) value = frame.EvaluateExpression('a_function_to_call()', options) self.assertTrue(value.IsValid()) self.assertTrue(value.GetValueAsSigned(0) == 2) self.assertTrue(callee_break.GetHitCount() == 2)