def test_with_python_api(self): """Use Python APIs to create a breakpoint by (filespec, line).""" self.build() exe = self.getBuildArtifact("a.out") target = self.dbg.CreateTarget(exe) self.assertTrue(target, VALID_TARGET) filespec = target.GetExecutable() self.assertTrue(filespec, VALID_FILESPEC) fsDir = os.path.normpath(filespec.GetDirectory()) fsFile = filespec.GetFilename() self.assertTrue( fsDir == os.path.dirname(self.getBuildArtifact()) and fsFile == "a.out", "FileSpec matches the executable") bpfilespec = lldb.SBFileSpec("main.cpp", False) breakpoint = target.BreakpointCreateByLocation(bpfilespec, 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)]) # 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.Launch() 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 = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint) self.assertIsNotNone(thread) # The filename of frame #0 should be 'main.cpp' and the line number # should be 93. self.expect("%s:%d" % (lldbutil.get_filenames(thread)[0], lldbutil.get_line_numbers(thread)[0]), "Break correctly at main.cpp:%d" % self.line, exe=False, startstr="main.cpp:") # clang compiled code reported main.cpp:94? # startstr = "main.cpp:93") # We should be stopped on the breakpoint with a hit count of 1. self.assertEqual(breakpoint.GetHitCount(), 1, BREAKPOINT_HIT_ONCE) process.Continue()
def test_stack_traces(self): """Test SBprocess and SBThread APIs with printing of the stack traces.""" 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.line) self.assertTrue(breakpoint, VALID_BREAKPOINT) # Now launch the process, and do not stop at entry point. process = target.LaunchSimple (["abc", "xyz"], None, self.get_process_working_directory()) if not process: self.fail("SBTarget.LaunchProcess() failed") import lldbsuite.test.lldbutil as lldbutil 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())) stacktraces = lldbutil.print_stacktraces(process, string_buffer=True) self.expect(stacktraces, exe=False, substrs = ['(int)argc=3'])
def test(self): """Test that we can backtrace correctly from Non ABI functions on the stack""" self.build() self.setTearDownCleanup() exe = os.path.join(os.getcwd(), "a.out") target = self.dbg.CreateTarget(exe) self.assertTrue(target, VALID_TARGET) lldbutil.run_break_set_by_symbol(self, "func") process = target.LaunchSimple( ["abc", "xyz"], None, self.get_process_working_directory()) if not process: self.fail("SBTarget.Launch() 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())) stacktraces = lldbutil.print_stacktraces(process, string_buffer=True) self.expect(stacktraces, exe=False, substrs=['(int)argc=3']) self.runCmd("thread step-inst") stacktraces = lldbutil.print_stacktraces(process, string_buffer=True) self.expect(stacktraces, exe=False, substrs=['(int)argc=3'])
def build_and_run_to_bkpt(self): self.build() exe = os.path.join(os.getcwd(), "a.out") target = self.dbg.CreateTarget(exe) self.assertTrue(target, VALID_TARGET) # Create the breakpoint. breakpoint = target.BreakpointCreateBySourceRegex( "// Set a breakpoint here to get started", self.main_spec) self.assertTrue(breakpoint, VALID_BREAKPOINT) # Launch the process, and do not stop at the entry point. process = target.LaunchSimple( None, 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())) self.thread = lldbutil.get_one_thread_stopped_at_breakpoint( process, breakpoint) self.assertIsNotNone( self.thread, "Expected one thread to be stopped at the breakpoint") # Next set a breakpoint in this function, set up Expression options to stop on # breakpoint hits, and call the function. self.fun_bkpt = self.target().BreakpointCreateBySourceRegex( "// Stop inside the function here.", self.main_spec) self.assertTrue(self.fun_bkpt, VALID_BREAKPOINT)
def test(self): """Test that we can backtrace correctly from Non ABI functions on the stack""" self.build() self.setTearDownCleanup() exe = os.path.join(os.getcwd(), "a.out") target = self.dbg.CreateTarget(exe) self.assertTrue(target, VALID_TARGET) lldbutil.run_break_set_by_symbol(self, "func") process = target.LaunchSimple(["abc", "xyz"], None, self.get_process_working_directory()) if not process: self.fail("SBTarget.Launch() 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())) stacktraces = lldbutil.print_stacktraces(process, string_buffer=True) self.expect(stacktraces, exe=False, substrs=['(int)argc=3']) self.runCmd("thread step-inst") stacktraces = lldbutil.print_stacktraces(process, string_buffer=True) self.expect(stacktraces, exe=False, substrs=['(int)argc=3'])
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 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_stack_traces(self): """Test SBprocess and SBThread APIs with printing of the stack traces.""" 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.line) self.assertTrue(breakpoint, VALID_BREAKPOINT) # Now launch the process, and do not stop at entry point. process = target.LaunchSimple( ["abc", "xyz"], None, self.get_process_working_directory()) if not process: self.fail("SBTarget.LaunchProcess() failed") import lldbsuite.test.lldbutil as lldbutil 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())) stacktraces = lldbutil.print_stacktraces(process, string_buffer=True) self.expect(stacktraces, exe=False, substrs=['(int)argc=3'])
def test (self): """Test that we can backtrace correctly with 'noreturn' functions on the stack""" self.build() self.setTearDownCleanup() exe = os.path.join(os.getcwd(), "a.out") target = self.dbg.CreateTarget(exe) self.assertTrue(target, VALID_TARGET) process = target.LaunchSimple (None, None, self.get_process_working_directory()) if not process: self.fail("SBTarget.Launch() 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())) thread = process.GetThreadAtIndex(0) abort_frame_number = 0 for f in thread.frames: # Some C libraries mangle the abort symbol into __GI_abort. if f.GetFunctionName() in ["abort", "__GI_abort"]: break abort_frame_number = abort_frame_number + 1 if self.TraceOn(): print("Backtrace once we're stopped:") for f in thread.frames: print(" {0:d} {1!s}".format(f.GetFrameID(), f.GetFunctionName())) # I'm going to assume that abort() ends up calling/invoking another # function before halting the process. In which case if abort_frame_number # equals 0, we didn't find abort() in the backtrace. if abort_frame_number == len(thread.frames): self.fail("Unable to find abort() in backtrace.") func_c_frame_number = abort_frame_number + 1 if thread.GetFrameAtIndex (func_c_frame_number).GetFunctionName() != "func_c": self.fail("Did not find func_c() above abort().") # This depends on whether we see the func_b inlined function in the backtrace # or not. I'm not interested in testing that aspect of the backtrace here # right now. if thread.GetFrameAtIndex (func_c_frame_number + 1).GetFunctionName() == "func_b": func_a_frame_number = func_c_frame_number + 2 else: func_a_frame_number = func_c_frame_number + 1 if thread.GetFrameAtIndex (func_a_frame_number).GetFunctionName() != "func_a": self.fail("Did not find func_a() above func_c().") main_frame_number = func_a_frame_number + 1 if thread.GetFrameAtIndex (main_frame_number).GetFunctionName() != "main": self.fail("Did not find main() above func_a().")
def test_with_python_api(self): """Use Python APIs to create a breakpoint by (filespec, line).""" self.build() exe = os.path.join(os.getcwd(), "a.out") target = self.dbg.CreateTarget(exe) self.assertTrue(target, VALID_TARGET) filespec = target.GetExecutable() self.assertTrue(filespec, VALID_FILESPEC) fsDir = os.path.normpath(filespec.GetDirectory()) fsFile = filespec.GetFilename() self.assertTrue(fsDir == os.getcwd() and fsFile == "a.out", "FileSpec matches the executable") bpfilespec = lldb.SBFileSpec("main.cpp", False) breakpoint = target.BreakpointCreateByLocation(bpfilespec, 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)]) # 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.Launch() 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 lldbsuite.test.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 the line number # should be 93. self.expect("%s:%d" % (lldbutil.get_filenames(thread)[0], lldbutil.get_line_numbers(thread)[0]), "Break correctly at main.cpp:%d" % self.line, exe=False, startstr = "main.cpp:") ### clang compiled code reported main.cpp:94? ### startstr = "main.cpp:93") # We should be stopped on the breakpoint with a hit count of 1. self.assertTrue(breakpoint.GetHitCount() == 1, BREAKPOINT_HIT_ONCE) process.Continue()
def test_unwind_expression(self): """Test unwinding from an expression.""" self.build() exe = os.path.join(os.getcwd(), "a.out") target = self.dbg.CreateTarget(exe) self.assertTrue(target, VALID_TARGET) # Create the breakpoint. main_spec = lldb.SBFileSpec("main.cpp", False) breakpoint = target.BreakpointCreateBySourceRegex("// Set a breakpoint here to get started", main_spec) self.assertTrue(breakpoint, VALID_BREAKPOINT) # Launch the process, and do not stop at the entry point. process = target.LaunchSimple (None, 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())) thread = lldbutil.get_one_thread_stopped_at_breakpoint(process, breakpoint) self.assertIsNotNone(thread, "Expected one thread to be stopped at the breakpoint") # # Use Python API to evaluate expressions while stopped in a stack frame. # main_frame = thread.GetFrameAtIndex(0) # Next set a breakpoint in this function, set up Expression options to stop on # breakpoint hits, and call the function. fun_bkpt = target.BreakpointCreateBySourceRegex("// Stop inside the function here.", main_spec) self.assertTrue(fun_bkpt, VALID_BREAKPOINT) options = lldb.SBExpressionOptions() options.SetIgnoreBreakpoints(False) options.SetUnwindOnError(False) val = main_frame.EvaluateExpression("a_function_to_call()", options) self.assertTrue(val.GetError().Fail(), "We did not complete the execution.") error_str = val.GetError().GetCString() self.assertTrue("Execution was interrupted, reason: breakpoint" in error_str, "And the reason was right.") thread = lldbutil.get_one_thread_stopped_at_breakpoint(process, fun_bkpt) self.assertTrue(thread.IsValid(), "We are indeed stopped at our breakpoint") # Now unwind the expression, and make sure we got back to where we started. error = thread.UnwindInnermostExpression() self.assertTrue(error.Success(), "We succeeded in unwinding") cur_frame = thread.GetFrameAtIndex(0) self.assertTrue(cur_frame.IsEqual(main_frame), "We got back to the main frame.")
def test_with_process_launch_api(self): """Test the SBCommandInterpreter APIs.""" 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) # Retrieve the associated command interpreter from our debugger. ci = self.dbg.GetCommandInterpreter() self.assertTrue(ci, VALID_COMMAND_INTERPRETER) # Exercise some APIs.... self.assertTrue(ci.HasCommands()) self.assertTrue(ci.HasAliases()) self.assertTrue(ci.HasAliasOptions()) self.assertTrue(ci.CommandExists("breakpoint")) self.assertTrue(ci.CommandExists("target")) self.assertTrue(ci.CommandExists("platform")) self.assertTrue(ci.AliasExists("file")) self.assertTrue(ci.AliasExists("run")) self.assertTrue(ci.AliasExists("bt")) res = lldb.SBCommandReturnObject() ci.HandleCommand("breakpoint set -f main.c -l {0:d}".format(self.line), res) self.assertTrue(res.Succeeded()) ci.HandleCommand("process launch", res) self.assertTrue(res.Succeeded()) # Boundary conditions should not crash lldb! self.assertFalse(ci.CommandExists(None)) self.assertFalse(ci.AliasExists(None)) ci.HandleCommand(None, res) self.assertFalse(res.Succeeded()) res.AppendMessage("Just appended a message.") res.AppendMessage(None) if self.TraceOn(): print(res) process = ci.GetProcess() self.assertTrue(process) import lldbsuite.test.lldbutil as lldbutil 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())) if self.TraceOn(): lldbutil.print_stacktraces(process)
def test_with_process_launch_api(self): """Test the SBCommandInterpreter APIs.""" 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) # Retrieve the associated command interpreter from our debugger. ci = self.dbg.GetCommandInterpreter() self.assertTrue(ci, VALID_COMMAND_INTERPRETER) # Exercise some APIs.... self.assertTrue(ci.HasCommands()) self.assertTrue(ci.HasAliases()) self.assertTrue(ci.HasAliasOptions()) self.assertTrue(ci.CommandExists("breakpoint")) self.assertTrue(ci.CommandExists("target")) self.assertTrue(ci.CommandExists("platform")) self.assertTrue(ci.AliasExists("file")) self.assertTrue(ci.AliasExists("run")) self.assertTrue(ci.AliasExists("bt")) res = lldb.SBCommandReturnObject() ci.HandleCommand("breakpoint set -f main.c -l %d" % self.line, res) self.assertTrue(res.Succeeded()) ci.HandleCommand("process launch", res) self.assertTrue(res.Succeeded()) # Boundary conditions should not crash lldb! self.assertFalse(ci.CommandExists(None)) self.assertFalse(ci.AliasExists(None)) ci.HandleCommand(None, res) self.assertFalse(res.Succeeded()) res.AppendMessage("Just appended a message.") res.AppendMessage(None) if self.TraceOn(): print(res) process = ci.GetProcess() self.assertTrue(process) import lldbsuite.test.lldbutil as lldbutil 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())) if self.TraceOn(): lldbutil.print_stacktraces(process)
def test_with_constructor_name(self): """Test 'frame variable this' and 'expr this' when stopped inside a constructor.""" self.build() exe = os.path.join(os.getcwd(), "a.out") target = self.dbg.CreateTarget(exe) self.assertTrue(target, VALID_TARGET) filespec = target.GetExecutable() self.assertTrue(filespec, VALID_FILESPEC) fsDir = os.path.normpath(filespec.GetDirectory()) fsFile = filespec.GetFilename() self.assertTrue(fsDir == os.getcwd() and fsFile == "a.out", "FileSpec matches the executable") bpfilespec = lldb.SBFileSpec("main.cpp", False) breakpoint = target.BreakpointCreateByLocation(bpfilespec, 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)]) # 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.Launch() 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 lldbsuite.test.lldbutil import stop_reason_to_str self.fail(STOPPED_DUE_TO_BREAKPOINT_WITH_STOP_REASON_AS % stop_reason_to_str(thread.GetStopReason())) frame = thread.frames[0] self.assertTrue(frame.IsValid(), "Got a valid frame.") self.assertTrue("C::C" in frame.name, "Constructor name includes class name.")
def test_with_constructor_name(self): """Test 'frame variable this' and 'expr this' when stopped inside a constructor.""" self.build() exe = self.getBuildArtifact("a.out") target = self.dbg.CreateTarget(exe) self.assertTrue(target, VALID_TARGET) filespec = target.GetExecutable() self.assertTrue(filespec, VALID_FILESPEC) fsDir = os.path.normpath(filespec.GetDirectory()) fsFile = filespec.GetFilename() self.assertTrue(fsDir == os.path.dirname(self.getBuildArtifact()) and fsFile == "a.out", "FileSpec matches the executable") bpfilespec = lldb.SBFileSpec("main.cpp", False) breakpoint = target.BreakpointCreateByLocation(bpfilespec, 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)]) # 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.Launch() 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 = lldbutil.get_stopped_thread( process, lldb.eStopReasonBreakpoint) self.assertIsNotNone(thread) frame = thread.frames[0] self.assertTrue(frame.IsValid(), "Got a valid frame.") self.assertTrue("C::C" in frame.name, "Constructor name includes class name.")
def run(self): count = 0 # Let's only try at most 3 times to retrieve any kind of event. while not count > 3: if listener.WaitForEvent(5, event): self.trace("Got a valid event:", event) self.trace("Event data flavor:", event.GetDataFlavor()) self.trace("Event type:", lldbutil.state_type_to_str(event.GetType())) listener.Clear() return count = count + 1 print("Timeout: listener.WaitForEvent") listener.Clear() return
def test_remote_launch(self): """Test SBProcess.RemoteLaunch() API with a process not in eStateConnected, and it should fail.""" self.build() exe = os.path.join(os.getcwd(), "a.out") target = self.dbg.CreateTarget(exe) self.assertTrue(target, VALID_TARGET) # Launch the process, and do not stop at the entry point. process = target.LaunchSimple (None, None, self.get_process_working_directory()) if self.TraceOn(): print("process state:", state_type_to_str(process.GetState())) self.assertTrue(process.GetState() != lldb.eStateConnected) error = lldb.SBError() success = process.RemoteLaunch(None, None, None, None, None, None, 0, False, error) self.assertTrue(not success, "RemoteLaunch() should fail for process state != eStateConnected")
def run(self): count = 0 # Let's only try at most 4 times to retrieve any kind of event. # After that, the thread exits. while not count > 3: if traceOn: print("Try wait for event...") if listener.WaitForEvent(5, event): if traceOn: desc = lldbutil.get_description(event) print("Event description:", desc) print("Event data flavor:", event.GetDataFlavor()) print("Process state:", lldbutil.state_type_to_str(process.GetState())) print() else: if traceOn: print("timeout occurred waiting for event...") count = count + 1 return
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_unwind_expression(self): """Test unwinding from an expression.""" self.build() exe = os.path.join(os.getcwd(), "a.out") target = self.dbg.CreateTarget(exe) self.assertTrue(target, VALID_TARGET) # Create the breakpoint. main_spec = lldb.SBFileSpec("main.cpp", False) breakpoint = target.BreakpointCreateBySourceRegex( "// Set a breakpoint here to get started", main_spec) self.assertTrue(breakpoint, VALID_BREAKPOINT) # Launch the process, and do not stop at the entry point. process = target.LaunchSimple(None, 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())) thread = lldbutil.get_one_thread_stopped_at_breakpoint( process, breakpoint) self.assertIsNotNone( thread, "Expected one thread to be stopped at the breakpoint") # # Use Python API to evaluate expressions while stopped in a stack frame. # main_frame = thread.GetFrameAtIndex(0) # Next set a breakpoint in this function, set up Expression options to stop on # breakpoint hits, and call the function. fun_bkpt = target.BreakpointCreateBySourceRegex( "// Stop inside the function here.", main_spec) self.assertTrue(fun_bkpt, VALID_BREAKPOINT) options = lldb.SBExpressionOptions() options.SetIgnoreBreakpoints(False) options.SetUnwindOnError(False) val = main_frame.EvaluateExpression("a_function_to_call()", options) self.assertTrue(val.GetError().Fail(), "We did not complete the execution.") error_str = val.GetError().GetCString() self.assertTrue( "Execution was interrupted, reason: breakpoint" in error_str, "And the reason was right.") thread = lldbutil.get_one_thread_stopped_at_breakpoint( process, fun_bkpt) self.assertTrue(thread.IsValid(), "We are indeed stopped at our breakpoint") # Now unwind the expression, and make sure we got back to where we # started. error = thread.UnwindInnermostExpression() self.assertTrue(error.Success(), "We succeeded in unwinding") cur_frame = thread.GetFrameAtIndex(0) self.assertTrue(cur_frame.IsEqual(main_frame), "We got back to the main frame.")
def test_evaluate_expression_python(self): """Test SBFrame.EvaluateExpression() API for evaluating an expression.""" self.build() 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())) thread = lldbutil.get_one_thread_stopped_at_breakpoint( process, breakpoint) self.assertIsNotNone( thread, "Expected one thread to be stopped at the breakpoint") # 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(self): """Test that we can backtrace correctly with _sigtramp on the stack""" self.build() self.setTearDownCleanup() exe = os.path.join(os.getcwd(), "a.out") target = self.dbg.CreateTarget(exe) self.assertTrue(target, VALID_TARGET) lldbutil.run_break_set_by_file_and_line(self, "main.c", line_number( 'main.c', '// Set breakpoint here'), num_expected_locations=1) process = target.LaunchSimple( None, None, self.get_process_working_directory()) if not process: self.fail("SBTarget.Launch() 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())) self.expect( "pro handle -n false -p true -s false SIGUSR1", "Have lldb pass SIGUSR1 signals", substrs=[ "SIGUSR1", "true", "false", "false"]) lldbutil.run_break_set_by_symbol( self, "handler", num_expected_locations=1, module_name="a.out") self.runCmd("continue") thread = process.GetThreadAtIndex(0) found_handler = False found_sigtramp = False found_kill = False found_main = False for f in thread.frames: if f.GetFunctionName() == "handler": found_handler = True if f.GetFunctionName() == "_sigtramp": found_sigtramp = True if f.GetFunctionName() == "__kill": found_kill = True if f.GetFunctionName() == "main": found_main = True if self.TraceOn(): print("Backtrace once we're stopped:") for f in thread.frames: print(" %d %s" % (f.GetFrameID(), f.GetFunctionName())) if not found_handler: self.fail("Unable to find handler() in backtrace.") if not found_sigtramp: self.fail("Unable to find _sigtramp() in backtrace.") if not found_kill: self.fail("Unable to find kill() in backtrace.") if not found_main: self.fail("Unable to find main() in backtrace.")
def test_evaluate_expression_python(self): """Test SBFrame.EvaluateExpression() API for evaluating an expression.""" self.build() 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())) thread = lldbutil.get_one_thread_stopped_at_breakpoint(process, breakpoint) self.assertIsNotNone(thread, "Expected one thread to be stopped at the breakpoint") # 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)