Exemple #1
0
        public void MiniDumpCallbackOrderTest()
        {
            try
            {
                // Load an exception into the system tables
                throw new InvalidOperationException();
            }
            catch
            {
                // Test for debug exception info
                var memCallbackCalled = false;
                using var hFile = CreateFile("CallbackOrder.dmp", Kernel32.FileAccess.GENERIC_READ | Kernel32.FileAccess.GENERIC_WRITE, 0, default, FileMode.Create, FileFlagsAndAttributes.FILE_ATTRIBUTE_NORMAL);
                if (!hFile.IsInvalid)
                {
                    var mdei = new MINIDUMP_EXCEPTION_INFORMATION
                    {
                        ThreadId          = GetCurrentThreadId(),
                        ExceptionPointers = Marshal.GetExceptionPointers()
                    };

                    var mci = new MINIDUMP_CALLBACK_INFORMATION {
                        CallbackRoutine = MyMiniDumpCallback
                    };

                    Assert.That(MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hFile, MINIDUMP_TYPE.MiniDumpNormal, mdei, default, mci), ResultIs.Successful);
Exemple #2
0
 public static extern bool MiniDumpWriteDump(
     PssSnapshotSafeHandle hpss,
     uint processId,
     SafeHandle hFile,
     MINIDUMP_TYPE dumpType,
     IntPtr expParam,
     IntPtr userStreamParam,
     [In] ref MINIDUMP_CALLBACK_INFORMATION callbackParam);
        public static void SnapshotAndDump(this Process process, string dumpFile, MINIDUMP_TYPE dumpType)
        {
            using (var fs = new FileStream(dumpFile, FileMode.Create, FileAccess.ReadWrite, FileShare.None))
            {
                using (var pssSnapshot = PssSnapshotSafeHandle.CaptureSnapshot(process.Handle))
                {
                    bool PssSnapshotMinidumpCallback(IntPtr unused, ref MINIDUMP_CALLBACK_INPUT input, ref MINIDUMP_CALLBACK_OUTPUT output)
                    {
                        const int S_OK    = 0;
                        const int S_FALSE = 1;

                        switch (input.CallbackType)
                        {
                        case MINIDUMP_CALLBACK_TYPE.IsProcessSnapshotCallback:
                            // The target is always a snapshot.
                            output.Status = S_FALSE;
                            break;

                        case MINIDUMP_CALLBACK_TYPE.ReadMemoryFailureCallback:
                            // Ignore any read failures during dump generation.
                            output.Status = S_OK;
                            break;
                        }

                        return(true);
                    }

                    var callbackParam = new MINIDUMP_CALLBACK_INFORMATION
                    {
                        CallbackParam   = IntPtr.Zero,
                        CallbackRoutine = PssSnapshotMinidumpCallback
                    };

                    if (!MiniDumpWriteDump(pssSnapshot, (uint)process.Id, fs.SafeFileHandle, dumpType | MINIDUMP_TYPE.IgnoreInaccessibleMemory, IntPtr.Zero, IntPtr.Zero, ref callbackParam))
                    {
                        throw new Win32Exception(Marshal.GetLastWin32Error());
                    }
                }
            }
        }