Skip to content

Commit

Permalink
mini dump
Browse files Browse the repository at this point in the history
  • Loading branch information
kmvi committed Dec 19, 2014
1 parent dc56f05 commit 4a9510c
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
77 changes: 77 additions & 0 deletions minidump/MiniDump.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Diagnostics;
using System.IO;

namespace minidump
{
// http://msdn.microsoft.com/en-us/library/windows/desktop/ms680519(v=vs.85).aspx
[Flags]
public enum MINIDUMP_TYPE
{
MiniDumpNormal = 0x00000000,
MiniDumpWithDataSegs = 0x00000001,
MiniDumpWithFullMemory = 0x00000002,
MiniDumpWithHandleData = 0x00000004,
MiniDumpFilterMemory = 0x00000008,
MiniDumpScanMemory = 0x00000010,
MiniDumpWithUnloadedModules = 0x00000020,
MiniDumpWithIndirectlyReferencedMemory = 0x00000040,
MiniDumpFilterModulePaths = 0x00000080,
MiniDumpWithProcessThreadData = 0x00000100,
MiniDumpWithPrivateReadWriteMemory = 0x00000200,
MiniDumpWithoutOptionalData = 0x00000400,
MiniDumpWithFullMemoryInfo = 0x00000800,
MiniDumpWithThreadInfo = 0x00001000,
MiniDumpWithCodeSegs = 0x00002000,
MiniDumpWithoutAuxiliaryState = 0x00004000,
MiniDumpWithFullAuxiliaryState = 0x00008000,
MiniDumpWithPrivateWriteCopyMemory = 0x00010000,
MiniDumpIgnoreInaccessibleMemory = 0x00020000,
MiniDumpWithTokenInformation = 0x00040000,
MiniDumpWithModuleHeaders = 0x00080000,
MiniDumpFilterTriage = 0x00100000,
MiniDumpValidTypeFlags = 0x001fffff
}

// http://msdn.microsoft.com/en-us/library/windows/desktop/ms680366(v=vs.85).aspx
[StructLayout(LayoutKind.Sequential, Pack = 4)]
public struct MINIDUMP_EXCEPTION_INFORMATION
{
public uint ThreadId;
public IntPtr ExceptionPointers;
public int ClientPointers;
}

public class MiniDump
{
[DllImport("kernel32.dll")]
private static extern uint GetCurrentThreadId();

[DllImport("DbgHelp.dll")]
private static extern bool MiniDumpWriteDump(IntPtr hProcess, uint ProcessId,
IntPtr hFile, int DumpType, ref MINIDUMP_EXCEPTION_INFORMATION ExceptionParam,
IntPtr UserStreamParam, IntPtr CallbackParam);

public static void CreateMiniDump()
{
var proc = Process.GetCurrentProcess();
var fileName = String.Format("minidump_{0}_{1}.dmp", proc.ProcessName, DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss"));

var info = new MINIDUMP_EXCEPTION_INFORMATION()
{
ThreadId = GetCurrentThreadId(),
ExceptionPointers = Marshal.GetExceptionPointers(),
ClientPointers = 1
};

using (var fs = new FileStream(fileName, FileMode.Create)) {
MiniDumpWriteDump(proc.Handle, (uint)proc.Id, fs.SafeFileHandle.DangerousGetHandle(),
(int)MINIDUMP_TYPE.MiniDumpNormal, ref info, IntPtr.Zero, IntPtr.Zero);
}
}
}
}
1 change: 1 addition & 0 deletions minidump/minidump.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
<Compile Include="Form1.Designer.cs">
<DependentUpon>Form1.cs</DependentUpon>
</Compile>
<Compile Include="MiniDump.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<EmbeddedResource Include="Form1.resx">
Expand Down

0 comments on commit 4a9510c

Please sign in to comment.