/// <inheritdoc /> public AttachDebuggerResult AttachToProcess(Process process) { if (process == null) { throw new ArgumentNullException("process"); } AttachDebuggerResult result = AttachDebuggerResult.CouldNotAttach; try { IVisualStudio visualStudio = GetVisualStudio(true, logger); if (visualStudio != null) { visualStudio.Call(dte => { EnvDTE.Process dteProcess = FindProcess(dte.Debugger.DebuggedProcesses, process); if (dteProcess != null) { result = AttachDebuggerResult.AlreadyAttached; } else { dteProcess = FindProcess(dte.Debugger.LocalProcesses, process); if (dteProcess != null) { Process2 dteProcess2 = dteProcess as Process2; Debugger2 dteDebugger2 = dte.Debugger as Debugger2; if (dteProcess2 != null && dteDebugger2 != null) { IList <Guid> engineGuids = GetEngineGuids(); Engine[] engines = GetEngines(dteDebugger2, engineGuids); dteProcess2.Attach2(engines); } else { dteProcess.Attach(); } result = AttachDebuggerResult.Attached; } else { logger.Log(LogSeverity.Debug, "Failed to attach Visual Studio debugger to process because it was not found in the LocalProcesses list."); } } }); } } catch (VisualStudioException ex) { logger.Log(LogSeverity.Debug, "Failed to attach Visual Studio debugger to process.", ex); } return(result); }
/// <summary> /// Attaches the debugger to a process if the host settings require it. /// </summary> protected void AttachDebuggerIfNeeded(IDebuggerManager debuggerManager, Process debuggedProcess) { if (HostSetup.DebuggerSetup != null) { IDebugger debugger = debuggerManager.GetDebugger(HostSetup.DebuggerSetup, Logger); if (!Debugger.IsAttached) { Logger.Log(LogSeverity.Important, "Attaching debugger to the host."); AttachDebuggerResult result = debugger.AttachToProcess(debuggedProcess); if (result == AttachDebuggerResult.Attached) { this.debugger = debugger; this.debuggedProcess = debuggedProcess; } else if (result == AttachDebuggerResult.CouldNotAttach) { Logger.Log(LogSeverity.Warning, "Could not attach debugger to the host."); } } } }
private void RunEndpointWithDebugger(ILogger logger) { Process currentProcess = Process.GetCurrentProcess(); IDebuggerManager debuggerManager = new DefaultDebuggerManager(); // FIXME: Get from IoC var debuggerSetup = new DebuggerSetup(); IDebugger debugger = debuggerManager.GetDebugger(debuggerSetup, logger); AttachDebuggerResult attachResult = AttachDebuggerResult.CouldNotAttach; try { if (!Debugger.IsAttached) { logger.Log(LogSeverity.Important, "Attaching the debugger to the host."); attachResult = debugger.AttachToProcess(currentProcess); if (attachResult == AttachDebuggerResult.CouldNotAttach) { logger.Log(LogSeverity.Warning, "Could not attach debugger to the host."); } } RunEndpoint(logger); } finally { if (attachResult == AttachDebuggerResult.Attached) { logger.Log(LogSeverity.Important, "Detaching the debugger from the host."); DetachDebuggerResult detachResult = debugger.DetachFromProcess(currentProcess); if (detachResult == DetachDebuggerResult.CouldNotDetach) { logger.Log(LogSeverity.Warning, "Could not detach debugger from the host."); } } } }