def __handle_process_event(self, ev): type = ev.GetType() if type & lldb.SBProcess.eBroadcastBitSTDOUT: self.get_process_stdout() elif type & lldb.SBProcess.eBroadcastBitSTDOUT: self.get_process_stderr() elif type & lldb.SBProcess.eBroadcastBitInterrupt: debug(debugDriver, 'Got a process interrupt event!') lldbutil.get_description(ev) if self.__process_stopped_callback: self.__process_stopped_callback(self, lldb.SBProcess.GetProcessFromEvent(ev)) elif type & lldb.SBProcess.eBroadcastBitStateChanged: self.get_process_stdout() self.get_process_stderr() # only after printing the std* can we print our prompts state = lldb.SBProcess.GetStateFromEvent(ev) if state == lldb.eStateInvalid: return process = lldb.SBProcess.GetProcessFromEvent(ev) assert process.IsValid() if state == lldb.eStateInvalid \ or state == lldb.eStateUnloaded \ or state == lldb.eStateConnected \ or state == lldb.eStateAttaching \ or state == lldb.eStateLaunching \ or state == lldb.eStateStepping \ or state == lldb.eStateDetached: lldb_view_send("Process %llu %s\n", process.GetProcessID(), self.debugger.StateAsCString(state)) elif state == lldb.eStateRunning: None # Don't be too chatty elif state == lldb.eStateExited: debug(debugDriver, 'process state: ' + lldbutil.state_type_to_str(state)) r = self.interpret_command('process status') lldb_view_send(stdout_msg(r[0].GetOutput())) lldb_view_send(stderr_msg(r[0].GetError())) # Remove program counter markers if self.__process_stopped_callback: self.__process_stopped_callback(self, process, state) elif state == lldb.eStateStopped \ or state == lldb.eStateCrashed \ or state == lldb.eStateSuspended: debug(debugDriver, 'process state: ' + lldbutil.state_type_to_str(state)) if state != lldb.eStateStopped else None if lldb.SBProcess.GetRestartedFromEvent(ev): lldb_view_send('Process %llu stopped and was programmatically restarted.' % process.GetProcessID()) else: self.__update_selected_thread() if self.__process_stopped_callback: self.__process_stopped_callback(self, process, state)
def break_and_print_stacktraces(self): """Break at main.cpp:68 and do a threads dump""" 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 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 noreturn_unwind_tests(self): 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: # We use endswith() to look for abort() since some C libraries mangle the symbol into # __GI_abort or similar. if f.GetFunctionName().endswith("abort"): break abort_frame_number = abort_frame_number + 1 if self.TraceOn(): print "Backtrace once we're stopped:" for f in thread.frames: print " %d %s" % (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 == 0: 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 breakpoint_creation_by_filespec_python(self): """Use Python APIs to create a breakpoint by (filespec, line).""" 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 = 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 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 break_and_print_stacktraces(self): """Break at main.cpp:68 and do a threads dump""" 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, os.getcwd()) if not process: self.fail("SBTarget.LaunchProcess() failed") import 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 " %d %s" % (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 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 sigtramp_unwind_tests (self): 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 found_handler == False: self.fail("Unable to find handler() in backtrace.") if found_sigtramp == False: self.fail("Unable to find _sigtramp() in backtrace.") if found_kill == False: self.fail("Unable to find kill() in backtrace.") if found_main == False: self.fail("Unable to find main() in backtrace.")
def handleEvent(self, event): if isinstance(event, int): pass elif isinstance(event, lldb.SBEvent): if lldb.SBProcess.EventIsProcessEvent(event): state = lldb.SBProcess.GetStateFromEvent(event) status = lldbutil.state_type_to_str(state) self.win.erase() x = self.win.getmaxyx()[1] - len(status) - 1 self.win.addstr(0, x, status) return
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 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 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 handle_lldb_event(self, event): if lldb.SBProcess.EventIsProcessEvent(event): state = lldb.SBProcess.GetStateFromEvent(event) status = lldbutil.state_type_to_str(state) if status == 'running': self.status_attr.set_attr('running') elif status == 'stopped': self.status_attr.set_attr('stopped') elif status == 'exited': self.status_attr.set_attr('exited') else: self.status_attr.set_attr(None) self.status.set_text('%s' % status)
def class_types_constructor_name(self): """Check whether the constructor name has the class name prepended.""" 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 = 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 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 = 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 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 remote_launch_should_fail(self): """Test SBProcess.RemoteLaunch() API with a process not in eStateConnected, and it should fail.""" 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, os.getcwd()) 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 remote_launch_should_fail(self): """Test SBProcess.RemoteLaunch() API with a process not in eStateConnected, and it should fail.""" 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_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 sigtramp_unwind_tests(self): 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 found_handler == False: self.fail("Unable to find handler() in backtrace.") if found_sigtramp == False: self.fail("Unable to find _sigtramp() in backtrace.") if found_kill == False: self.fail("Unable to find kill() in backtrace.") if found_main == False: self.fail("Unable to find main() in backtrace.")
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)