internal static void SkipInitialDebugBreak(uint dwProcessId) { if (!NativeDebuggingMethods.DebugActiveProcess(dwProcessId)) { throw new LastWin32Exception(); } try { bool done = false; uint mainThread = 0; while (!done) { dynamic debugEvent = null; if (!WaitForDebugEvent(ref debugEvent, (int)TimeSpan.FromSeconds(10).TotalMilliseconds)) { throw new LastWin32Exception(); } switch ((NativeDebugEventCode)debugEvent.header.dwDebugEventCode) { case NativeDebugEventCode.CREATE_PROCESS_DEBUG_EVENT: new SafeFileHandle(debugEvent.union.CreateProcess.hFile, true).Dispose(); break; case NativeDebugEventCode.LOAD_DLL_DEBUG_EVENT: new SafeFileHandle(debugEvent.union.LoadDll.hFile, true).Dispose(); break; case NativeDebugEventCode.EXCEPTION_DEBUG_EVENT: case NativeDebugEventCode.EXIT_PROCESS_DEBUG_EVENT: mainThread = debugEvent.header.dwThreadId; done = true; break; } if (!NativeDebuggingMethods.ContinueDebugEvent(debugEvent.header.dwProcessId, debugEvent.header.dwThreadId, NativeDebuggingMethods.ContinueStatus.DBG_CONTINUE)) { throw new LastWin32Exception(); } } SuspendThread(new IntPtr(mainThread)); } finally { if (!NativeDebuggingMethods.DebugActiveProcessStop(dwProcessId)) { throw new LastWin32Exception(); } } }
/// <summary> /// Attach to the given process. Throws on error. /// </summary> /// <param name="processId">process ID of target process to attach to</param> /// <returns>process object representing process being debugged</returns> public NativeDbgProcess Attach(int processId) { EnsureIsOnWin32EventThread(); bool fAttached = NativeMethods.DebugActiveProcess((uint)processId); if (!fAttached) { int err = Marshal.GetLastWin32Error(); throw new InvalidOperationException("Failed to attach to process id " + processId + "error=" + err); } return(CreateNew(processId)); }