static extern bool MiniDumpWriteDump
 (
     SafeProcessHandle hProcess,
     uint ProcessId,
     SafeFileHandle hFile,
     MINIDUMP_TYPE DumpType,
     ref MINIDUMP_EXCEPTION_INFORMATION ExceptionParam,
     IntPtr UserStreamParam,
     IntPtr CallbackParam
 );
Exemple #2
0
        public static void CreateDump(string fileName = null)
        {
            var dumpName = fileName ?? Assembly.GetEntryAssembly().GetName().Name + ".dmp";

            if (File.Exists(dumpName))
            {
                return;
            }
            var hFile = CreateFile(dumpName,
                                   EFileAccess.GenericWrite, EFileShare.None, IntPtr.Zero,
                                   ECreationDisposition.CreateAlways,
                                   EFileAttributes.Normal,
                                   IntPtr.Zero);

            if (hFile == InvalidHandleValue)
            {
                var hr = Marshal.GetHRForLastWin32Error();
                var ex = Marshal.GetExceptionForHR(hr);
                throw ex;
            }

            var process = Process.GetCurrentProcess();

            if (!process.Is32BitProcess() && IntPtr.Size == 4)
            {
                throw new InvalidOperationException(
                          "Can't create 32 bit dump of 64 bit process"
                          );
            }

            var exceptInfo = new MINIDUMP_EXCEPTION_INFORMATION();
            var result     = MiniDumpWriteDump(
                process.Handle,
                process.Id,
                hFile,
                _MINIDUMP_TYPE.MiniDumpWithFullMemory | _MINIDUMP_TYPE.MiniDumpWithProcessThreadData |
                _MINIDUMP_TYPE.MiniDumpWithThreadInfo,
                ref exceptInfo,
                IntPtr.Zero,
                IntPtr.Zero
                );

            if (result == false)
            {
                var hr = Marshal.GetHRForLastWin32Error();
                var ex = Marshal.GetExceptionForHR(hr);
                throw ex;
            }

            CloseHandle(hFile);
        }
Exemple #3
0
        public static bool Write(MINIDUMP_TYPE options )
        {
            string fileName = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, DateTime.Now.ToString("yyyyMMdd-HHmmss") + ".dmp");
            using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite, FileShare.Write))
            {
                MINIDUMP_EXCEPTION_INFORMATION Mdinfo = new MINIDUMP_EXCEPTION_INFORMATION();
                Process currentProcess = Process.GetCurrentProcess();

                IntPtr currentProcessHandle = currentProcess.Handle;

                uint currentProcessId = (uint)currentProcess.Id;
                Mdinfo.ThreadId = CrashDump.GetCurrentThreadId();
                Mdinfo.ExceptionPointers = Marshal.GetExceptionPointers();
                Mdinfo.ClientPointers = 1;
                return MiniDumpWriteDump(currentProcessHandle, currentProcessId, fs.SafeFileHandle, (uint)options, ref Mdinfo, IntPtr.Zero, IntPtr.Zero);
            }
        }
        /// <summary>
        /// Call back function that will be called when an exception has occurred. This function will create
        /// a full memory dump of the application
        /// </summary>
        /// <returns>Name of the generated dump file</returns>
        public static string GenerateMemoryDump()
        {
            string assemblyPath = Assembly.GetEntryAssembly().Location;
            string dumpFileName = assemblyPath + "_" + DateTime.Now.ToString("dd.MM.yyyy.HH.mm.ss") + ".dmp";
            FileStream file = new FileStream(dumpFileName, FileMode.Create);
            MINIDUMP_EXCEPTION_INFORMATION info = new MINIDUMP_EXCEPTION_INFORMATION();
            info.ClientPointers = 1;
            info.ExceptionPointers = Marshal.GetExceptionPointers();
            info.ThreadId = GetCurrentThreadId();

            // A full memory dump is necessary in the case of a managed application, other wise no information
            // regarding the managed code will be available
            MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), file.SafeFileHandle.DangerousGetHandle(), MiniDumpWithFullMemory, ref info, IntPtr.Zero, IntPtr.Zero);
            file.Close();

            return dumpFileName;
        }
Exemple #5
0
        public static bool Write(MINIDUMP_TYPE options)
        {
            string fileName = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, DateTime.Now.ToString("yyyyMMdd-HHmmss") + ".dmp");

            using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite, FileShare.Write))
            {
                MINIDUMP_EXCEPTION_INFORMATION Mdinfo = new MINIDUMP_EXCEPTION_INFORMATION();
                Process currentProcess = Process.GetCurrentProcess();

                IntPtr currentProcessHandle = currentProcess.Handle;

                uint currentProcessId = (uint)currentProcess.Id;
                Mdinfo.ThreadId          = CrashDump.GetCurrentThreadId();
                Mdinfo.ExceptionPointers = Marshal.GetExceptionPointers();
                Mdinfo.ClientPointers    = 1;
                return(MiniDumpWriteDump(currentProcessHandle, currentProcessId, fs.SafeFileHandle, (uint)options, ref Mdinfo, IntPtr.Zero, IntPtr.Zero));
            }
        }
Exemple #6
0
 private static void CreateMiniDump(string fName)
 {
     using (System.Diagnostics.Process process = System.Diagnostics.Process.GetCurrentProcess())
     {
         string FileName = fName;
         MINIDUMP_EXCEPTION_INFORMATION Mdinfo = new MINIDUMP_EXCEPTION_INFORMATION();
         Mdinfo.ThreadId          = GetCurrentThreadId();
         Mdinfo.ExceptionPointers = Marshal.GetExceptionPointers();
         Mdinfo.ClientPointers    = 1;
         using (FileStream fs = new FileStream(FileName, FileMode.Create))
         {
             {
                 MiniDumpWriteDump(process.Handle, (uint)process.Id, fs.SafeFileHandle.DangerousGetHandle(), MINIDUMP_TYPE.MiniDumpWithFullMemory,
                                   ref Mdinfo,
                                   IntPtr.Zero,
                                   IntPtr.Zero);
             }
         }
     }
 }
        public static bool Write(string fileName, MINIDUMP_TYPE DumpType)
        {
            using (var fs = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                var ExceptionParam = new MINIDUMP_EXCEPTION_INFORMATION
                {
                    ThreadId          = GetCurrentThreadId(),
                    ExceptionPointers = Marshal.GetExceptionPointers(),
                    ClientPointers    = false
                };

                return(MiniDumpWriteDump
                       (
                           GetCurrentProcess(),
                           GetCurrentProcessId(),
                           fs.SafeFileHandle,
                           DumpType,
                           ref ExceptionParam,
                           IntPtr.Zero,
                           IntPtr.Zero
                       ));
            }
        }
Exemple #8
0
        public static void CreateMiniDump(string FileName)
        {
            using (System.Diagnostics.Process process = System.Diagnostics.Process.GetCurrentProcess())
            {
                MINIDUMP_EXCEPTION_INFORMATION Mdinfo = new MINIDUMP_EXCEPTION_INFORMATION
                {
                    ThreadId          = GetCurrentThreadId(),
                    ExceptionPointers = Marshal.GetExceptionPointers(),
                    ClientPointers    = 1
                };

                using (FileStream fs = new FileStream(FileName, FileMode.Create))
                {
                    MiniDumpWriteDump(process.Handle,
                                      (uint)process.Id,
                                      fs.SafeFileHandle.DangerousGetHandle(),
                                      (int)(MINIDUMP_TYPE.Normal),
                                      ref Mdinfo,
                                      IntPtr.Zero,
                                      IntPtr.Zero);
                }
            }
        }
Exemple #9
0
            private static void CreateMiniDump()
            {
                using (System.Diagnostics.Process process = System.Diagnostics.Process.GetCurrentProcess())
                {
                    string FileName = string.Format(@"CRASH_DUMP_{0}_{1}.dmp", DateTime.Today.ToShortDateString(), DateTime.Now.Ticks);

                    MINIDUMP_EXCEPTION_INFORMATION Mdinfo = new MINIDUMP_EXCEPTION_INFORMATION();

                    Mdinfo.ThreadId          = GetCurrentThreadId();
                    Mdinfo.ExceptionPointers = Marshal.GetExceptionPointers();
                    Mdinfo.ClientPointers    = 1;

                    using (FileStream fs = new FileStream(FileName, FileMode.Create))
                    {
                        {
                            MiniDumpWriteDump(process.Handle, (uint)process.Id, fs.SafeFileHandle.DangerousGetHandle(), MINIDUMP_TYPE.MiniDumpNormal,
                                              ref Mdinfo,
                                              IntPtr.Zero,
                                              IntPtr.Zero);
                        }
                    }
                }
            }
Exemple #10
0
        public static void CreateMiniDump()
        {
            using (System.Diagnostics.Process process = System.Diagnostics.Process.GetCurrentProcess())
            {
                string FileName = string.Format(@"MINIDUMP_{0}_{1}_{2}.dmp", System.Environment.MachineName, DateTime.Today.ToShortDateString(), DateTime.Now.Ticks);

                MINIDUMP_EXCEPTION_INFORMATION Mdinfo = new MINIDUMP_EXCEPTION_INFORMATION();

                Mdinfo.ThreadId          = GetCurrentThreadId();
                Mdinfo.ExceptionPointers = Marshal.GetExceptionPointers();
                Mdinfo.ClientPointers    = 1;

                MessageBox.Show($"Произошла ошибка! Будет создан мини-дамп: {FileName}", "Ошибка");
                using (FileStream fs = new FileStream(FileName, FileMode.Create))
                {
                    {
                        MiniDumpWriteDump(process.Handle, (uint)process.Id, fs.SafeFileHandle.DangerousGetHandle(), MINIDUMP_TYPE.MiniDumpNormal,
                                          ref Mdinfo,
                                          IntPtr.Zero,
                                          IntPtr.Zero);
                    }
                }
            }
        }
Exemple #11
0
        public void DumpOnException(uint threadId, EXCEPTION_RECORD ev)
        {
            if (ev.ExceptionCode == BREAKPOINT_CODE)
            {
                return;
            }
            if (ev.ExceptionCode == CLRDBG_NOTIFICATION_EXCEPTION_CODE)
            {
                // based on https://social.msdn.microsoft.com/Forums/vstudio/en-US/bca092d4-d2b5-49ef-8bbc-cbce2c67aa89/net-40-firstchance-exception-0x04242420?forum=clr
                // it's a "notification exception" and can be safely ignored
                return;
            }
            if (ev.ExceptionCode == CTRL_C_EXCEPTION_CODE)
            {
                // we will also ignore CTRL+C events
                return;
            }
            // print information about the exception (decode it)
            ClrException managedException = null;

            foreach (var clrver in target.ClrVersions)
            {
                var runtime = clrver.CreateRuntime();
                var thr     = runtime.Threads.FirstOrDefault(t => t.OSThreadId == threadId);
                if (thr != null)
                {
                    managedException = thr.CurrentException;
                    break;
                }
            }
            var exceptionInfo = string.Format("{0:X}.{1} (\"{2}\")", ev.ExceptionCode,
                                              managedException != null ? managedException.Type.Name : "Native",
                                              managedException != null ? managedException.Message : "N/A");

            PrintTrace("Exception: " + exceptionInfo);

            if (rgxFilter.IsMatch(exceptionInfo))
            {
                byte[] threadContext = new byte[Native.CONTEXT_SIZE];
                target.DataReader.GetThreadContext(threadId, 0, Native.CONTEXT_SIZE, threadContext);
                IntPtr pev = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(EXCEPTION_RECORD)));
                Marshal.StructureToPtr(new EXCEPTION_RECORD
                {
                    ExceptionAddress     = ev.ExceptionAddress,
                    ExceptionFlags       = ev.ExceptionFlags,
                    ExceptionCode        = ev.ExceptionCode,
                    ExceptionRecord      = IntPtr.Zero,
                    NumberParameters     = ev.NumberParameters,
                    ExceptionInformation = ev.ExceptionInformation
                }, pev, false);
                var excpointers = new EXCEPTION_POINTERS
                {
                    ExceptionRecord = pev,
                    ContextRecord   = threadContext
                };
                IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(excpointers));
                Marshal.StructureToPtr(excpointers, ptr, false);
                var excinfo = new MINIDUMP_EXCEPTION_INFORMATION()
                {
                    ThreadId          = threadId,
                    ClientPointers    = false,
                    ExceptionPointers = ptr
                };
                var pexcinfo = Marshal.AllocHGlobal(Marshal.SizeOf(excinfo));
                Marshal.StructureToPtr(excinfo, pexcinfo, false);

                MakeActualDump(pexcinfo);

                Marshal.FreeHGlobal(pev);
                Marshal.FreeHGlobal(pexcinfo);
                Marshal.FreeHGlobal(ptr);
            }
        }
Exemple #12
0
 static extern bool MiniDumpWriteDump(IntPtr hProcess, uint ProcessId, IntPtr hFile, int DumpType, ref MINIDUMP_EXCEPTION_INFORMATION ExceptionParam, IntPtr UserStreamParam, IntPtr CallbackParam);
Exemple #13
0
 static extern bool MiniDumpWriteDump(IntPtr hProcess, uint processId, SafeHandle hFile, uint dumpType, ref MINIDUMP_EXCEPTION_INFORMATION ExceptionParam, IntPtr userStreamParam, IntPtr callbackParam);
Exemple #14
0
 static extern bool MiniDumpWriteDump(IntPtr hProcess, uint processId, SafeHandle hFile, uint dumpType, ref MINIDUMP_EXCEPTION_INFORMATION ExceptionParam, IntPtr userStreamParam, IntPtr callbackParam);
 private static extern bool MiniDumpWriteDump(IntPtr process, uint processId, IntPtr file, int dumpType, ref MINIDUMP_EXCEPTION_INFORMATION ExceptionParam, IntPtr UserStreamParam, IntPtr CallbackParam);