Programmatically Generating a Dump File

May 28, 2008

no comments

I’ve discussed dump analysis on several occasions, including detailed walkthroughs of native and managed issues – crashes, hangs, deadlocks and the rest of the bug zoo.

(Alon has just recently been to this bug zoo and promised me pictures.)

Oftentimes it’s useful to generate a dump programmatically.  For example, if your application fails and you want to record the data and send it over the network or persist it to some durable storage, you can do so without relying on Windows Error Reporting or other mechanisms.

[DllImportAttribute("dbghelp.dll")]

[return: MarshalAsAttribute(UnmanagedType.Bool)]

private static extern bool MiniDumpWriteDump(

    [In] IntPtr hProcess,

    uint ProcessId,

    SafeFileHandle hFile,

    DumpType DumpType,

    [In] IntPtr ExceptionParam,

    [In] IntPtr UserStreamParam,

    [In] IntPtr CallbackParam);

The way to go is the MiniDumpWriteDump function, exposed from dbghelp.dll.  I’ve written a simple helper class for using this API from C#, which can be downloaded from here.  Sample usage would be:

DumpHelper.WriteTinyDumpForThisProcess(

    @"D:\temp\mini.dmp");

 

Process process = Process.Start("notepad.exe");

process.WaitForInputIdle();

DumpHelper.WriteFullDumpForProcess(

    process, @"D:\temp\full.dmp");

 

process.Kill();

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>