public static ICorDebugProcess GetDebuggerHandleFromProcessDump(string processDumpFile, long clrInstanceId = 0) { LibraryProvider libraryProvider = new LibraryProvider(); using (DumpReader reader = new DumpReader(processDumpFile)) { if ((IntPtr.Size == 8) && (reader.ProcessorArchitecture == ProcessorArchitecture.PROCESSOR_ARCHITECTURE_INTEL)) { throw new InvalidOperationException("Opening a 32 bit dump in a 64 bit process."); } if ((IntPtr.Size == 4) && (reader.ProcessorArchitecture == ProcessorArchitecture.PROCESSOR_ARCHITECTURE_AMD64)) { throw new InvalidOperationException("Opening a 64 bit dump in a 32 bit process."); } using (DumpDataTarget target = new DumpDataTarget(reader)) { foreach (DumpModule module in reader.EnumerateModules()) { if ((clrInstanceId == 0) || (clrInstanceId == (long)module.BaseAddress)) { Version version; ClrDebuggingProcessFlags flags; ICorDebugProcess process; int errorCode = new CLRDebugging().TryOpenVirtualProcess( module.BaseAddress, target, libraryProvider, new Version(4, 0, 0x7fff, 0x7fff), out version, out flags, out process); if (errorCode < 0) { if (((errorCode != -2146231228) && (errorCode != -2146231226)) && (errorCode != -2146231225)) { Marshal.ThrowExceptionForHR(errorCode); } } else { return(process); } } } } } throw new InvalidOperationException("No V4.0 .NET Runtime found in dump file."); }
/// <summary> /// Attaches the process to a dump /// </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) { // bind to a runtime in the dump DumpDataTarget dataTarget = new DumpDataTarget(dumpReader); AttachToDump(dumpReader, clrInstanceId, dataTarget); }