public void HandleEventFallbackToMainThread() { const ulong mainThreadId = 1; _mockRemoteThread.GetThreadId().Returns(mainThreadId); var mockWorkerThread = Substitute.For <RemoteThread>(); MockThread(_mockRemoteThread, StopReason.NONE, new List <ulong>()); MockThread(mockWorkerThread, StopReason.NONE, new List <ulong>()); MockProcess(new List <RemoteThread> { _mockRemoteThread, mockWorkerThread }); _mockSbProcess.GetSelectedThread().Returns((RemoteThread)null); RaiseSingleStateChanged(); _mockDebugEngineHandler.Received(1).SendEvent(Arg.Is <DebugEvent>(x => x is BreakEvent), _mockProgram, _mockRemoteThread); _mockSbProcess.Received(1).SetSelectedThreadById(mainThreadId); }
public void ContinueInBreakModeSignal() { var selectedThread = Substitute.For <RemoteThread>(); _process.GetSelectedThread().Returns(selectedThread); selectedThread.GetStopReason().Returns(StopReason.SIGNAL); selectedThread.GetStopReasonDataCount().Returns <uint>(1); ulong sigAbort = 6; selectedThread.GetStopReasonDataAtIndex(0).Returns(sigAbort); _attachedProgram.ContinueInBreakMode(); EXCEPTION_INFO[] info = null; Predicate <DebugEvent> matchExceptionEvent = e => { if (!(e is ExceptionEvent exceptionEvent)) { return(false); } info = new EXCEPTION_INFO[1]; exceptionEvent.GetException(info); return(true); }; Received.InOrder(() => { _debugEngineHandler.SendEvent(Arg.Is <DebugEvent>(e => matchExceptionEvent(e)), _debugProgram,selectedThread); _lldbShell.AddDebugger(_debugger); _debugProgram.Received().EnumModules(out _); }); Assert.That(info[0].dwState, Is.EqualTo(enum_EXCEPTION_STATE.EXCEPTION_STOP_ALL | enum_EXCEPTION_STATE.EXCEPTION_CANNOT_BE_CONTINUED)); Assert.That(info[0].bstrExceptionName,Is.EqualTo("SIGABRT")); }
/// <summary> /// Send an exception event to the SDM to trigger breakmode. /// </summary> public void ContinueInBreakMode() { RemoteThread thread = _process.GetSelectedThread(); ExceptionEvent exceptionEvent; if (thread.GetStopReason() == StopReason.SIGNAL && thread.GetStopReasonDataCount() > 0) { var signalNumber = thread.GetStopReasonDataAtIndex(0); (string name,string description) = SignalMap.Map[signalNumber]; enum_EXCEPTION_STATE exceptionState = enum_EXCEPTION_STATE.EXCEPTION_STOP_ALL | enum_EXCEPTION_STATE.EXCEPTION_CANNOT_BE_CONTINUED; exceptionEvent = new ExceptionEvent(name,(uint)signalNumber,exceptionState,description); } else { exceptionEvent = new ExceptionEvent("",0,enum_EXCEPTION_STATE.EXCEPTION_NONE,""); } _debugEngineHandler.SendEvent(exceptionEvent,_debugProgram,thread); _lldbShell.AddDebugger(_debugger); UpdateModulesList(); }
// Called when we receive a state changed LLDB event. void OnStateChangedEvent(SbEvent sbEvent) { if (sbEvent == null) { return; } var type = sbEvent.GetStateType(); Debug.WriteLine("Received LLDB event: " + Enum.GetName(type.GetType(), type)); switch (type) { case StateType.STOPPED: if (sbEvent.GetProcessRestarted()) { break; } var currentThread = _lldbProcess.GetSelectedThread(); var currentStopReason = StopReason.INVALID; if (currentThread != null) { currentStopReason = currentThread.GetStopReason(); } // When stopping pick the most relevant thread based on the stop reason. if (currentThread == null || currentStopReason == StopReason.INVALID || currentStopReason == StopReason.NONE) { int numThreads = _lldbProcess.GetNumThreads(); RemoteThread planThread = null; RemoteThread otherThread = null; for (int i = 0; i < numThreads; ++i) { RemoteThread thread = _lldbProcess.GetThreadAtIndex(i); switch (thread.GetStopReason()) { case StopReason.INVALID: // fall-through case StopReason.NONE: break; case StopReason.SIGNAL: if (otherThread == null) { var signalNumber = thread.GetStopReasonDataAtIndex(0); var unixSignals = _lldbProcess.GetUnixSignals(); if (unixSignals != null && unixSignals.GetShouldStop((int)signalNumber)) { otherThread = thread; } } break; case StopReason.TRACE: // fall-through case StopReason.BREAKPOINT: // fall-through case StopReason.WATCHPOINT: // fall-through case StopReason.EXCEPTION: // fall-through case StopReason.EXEC: // fall-through case StopReason.EXITING: // fall-through case StopReason.INSTRUMENTATION: if (otherThread == null) { otherThread = thread; } break; case StopReason.PLAN_COMPLETE: if (planThread == null) { planThread = thread; } break; } } if (planThread != null) { currentThread = planThread; } else if (otherThread != null) { currentThread = otherThread; } else if (currentThread == null) { currentThread = _lldbProcess.GetThreadAtIndex(0); } if (currentThread == null) { Trace.WriteLine("Error: Cannot handle event. No thread found."); return; } _lldbProcess.SetSelectedThreadById(currentThread.GetThreadId()); currentStopReason = currentThread.GetStopReason(); } // Log specific information about the stop event. string message = "Received stop event. Reason: " + currentStopReason; var stopReasonDataCount = currentThread.GetStopReasonDataCount(); if (stopReasonDataCount > 0) { message += " Data:"; for (uint i = 0; i < stopReasonDataCount; i++) { message += " " + currentThread.GetStopReasonDataAtIndex(i); } } Trace.WriteLine(message); _taskContext.Factory.Run(async() => { // We run the event resolution on the main thread to make sure // we do not race with concurrent modifications in the breakpoint // manager class (we could just have hit breakpoint that is being // added by the main thread!). await _taskContext.Factory.SwitchToMainThreadAsync(); DebugEvent eventToSend = null; switch (currentStopReason) { case StopReason.BREAKPOINT: eventToSend = HandleBreakpointStop(currentThread); break; case StopReason.WATCHPOINT: eventToSend = HandleWatchpointStop(currentThread); break; case StopReason.SIGNAL: eventToSend = HandleSignalStop(currentThread); break; case StopReason.PLAN_COMPLETE: eventToSend = new StepCompleteEvent(); break; default: break; } if (eventToSend == null) { eventToSend = new BreakEvent(); } _debugEngineHandler.SendEvent(eventToSend,_program,currentThread); }); break; case StateType.EXITED: { // There are two ways to exit a debug session without an error: // - We call program.Terminate, which causes LLDB to send this event. // - Program exits by itself, resulting in this event. // We distinguish these events by checking if we called Terminate. ExitReason exitReason = _program.TerminationRequested ? ExitReason.DebuggerTerminated : ExitReason.ProcessExited; _debugEngineHandler.Abort(_program,ExitInfo.Normal(exitReason)); } break; case StateType.DETACHED: { // Normally the only way to detach the process is by program.Detach. // However, this check was retained to mirror the EXITED case and to // record unexpected instances of detaching through some other path. ExitReason exitReason = _program.DetachRequested ? ExitReason.DebuggerDetached : ExitReason.ProcessDetached; _debugEngineHandler.Abort(_program,ExitInfo.Normal(exitReason)); } break; } }