public void Attach(int processId, SafeWin32Handle attachContinuationEvent, CorRemoteTarget target) { Debug.Assert(!IsAlive); if (IsAlive) throw new InvalidOperationException("cannot call Attach on active process"); m_stopGo = new V2StopGoController(this); m_processAttaching = true; try { m_corProcess = m_corDebugger.DebugActiveProcess(processId,/*win32Attach*/ false, target); NativeProcessCreated(); } catch { // if an attach fails, we need to remove the process object from active processes. CleanAfterProcessExit(); throw; } // User's breakpoint should start with number 0. When launching, 1st breakpoint created is // special -- user entry point and it's given number 0. // In case of attach we don't create any such breakpoint but we still want // to start numbering breakpoints with 1; therefore we'll increase free breakpoint number. if (Breakpoints.m_freeBreakpointNumber == 0) { Breakpoints.m_freeBreakpointNumber = 1; } InitDebuggerCallbacks(); // resume the debuggee // for pure managed debugging this is legal as long as DebugActiveProcess has returned if (attachContinuationEvent != null) { Microsoft.Samples.Debugging.Native.NativeMethods.SetEvent(attachContinuationEvent); attachContinuationEvent.Close(); } }
public void CreateProcess(string commandLine, string commandArguments, string workingDirectory, CorRemoteTarget target) { Debug.Assert(!IsAlive); if (IsAlive) throw new InvalidOperationException("cannot call CreateProcess on active process"); m_stopGo = new V2StopGoController(this); int flags = (int)(m_engine.Options.CreateProcessWithNewConsole ? CreateProcessFlags.CREATE_NEW_CONSOLE : 0); try { m_corProcess = m_corDebugger.CreateProcess(commandLine, commandArguments, workingDirectory, flags, target); NativeProcessCreated(); } catch { CleanAfterProcessExit(); // remove process from process list in case of failure throw; } InitDebuggerCallbacks(); if (commandLine != null) m_name = commandLine; else m_name = commandArguments; }
/// <summary> /// Creates an MdbgProcess that can be compatible with dump debuggees. /// </summary> /// <param name="engine">owning engine object</param> /// <param name="stopGo">stopGo controller to provide policy for execution of process.</param> internal MDbgProcess(MDbgEngine engine, IStopGoController stopGo) { CommonInit(engine); m_stopGo = stopGo; }
/// <summary> /// Attaches the process to a dump using the specified DumpDataTarget /// </summary> /// <param name="dumpReader">A reader for the dump</param> /// <param name="clrInstanceId">The moduleBaseAddress of the CLR to debug or null /// to debug the first CLR encountered</param> public void AttachToDump(DumpReader dumpReader, long? clrInstanceId, ICorDebugDataTarget dataTarget) { Debug.Assert(!IsAlive); if (IsAlive) throw new InvalidOperationException("cannot call Attach on active process"); // set up the stopGo controller NoninvasiveStopGoController stopGo = new NoninvasiveStopGoController(); stopGo.Init(this); m_stopGo = stopGo; NativeProcessCreated(); foreach (DumpModule module in dumpReader.EnumerateModules()) { // use the selected runtime if there was one, otherwise just attach to the first // runtime we find if (clrInstanceId.HasValue && (clrInstanceId.Value != (long)module.BaseAddress)) continue; CLRDebugging clrDebugging = new CLRDebugging(); Version actualVersion; ClrDebuggingProcessFlags flags; CorProcess proc; int hr = clrDebugging.TryOpenVirtualProcess(module.BaseAddress, dataTarget, LibraryProvider, new Version(4, 0, short.MaxValue, short.MaxValue), out actualVersion, out flags, out proc); if (hr < 0) { if ((hr == (int)HResult.CORDBG_E_NOT_CLR) || (hr == (int)HResult.CORDBG_E_UNSUPPORTED_DEBUGGING_MODEL) || (hr == (int)HResult.CORDBG_E_UNSUPPORTED_FORWARD_COMPAT)) { // these errors are all potentially benign, just ignore this module. } else { Marshal.ThrowExceptionForHR(hr); // the rest are bad } } else { // We must have succeeded Debug.Assert(proc != null); BindToExistingClrInTarget(proc); m_dumpReader = dumpReader; return; // SUCCESS - done } } // If we've got here, it means there were no supported CLRs in the dump Debug.Assert(m_corProcess == null); // Should not yet be bound throw new MDbgException("Failed to open the dump - no supported CLRs found in the module list"); }