This is a mirror of official site: http://jasper-net.blogspot.com/

Writing Minidumps in C#

| Monday, October 25, 2010
This is the first in what I intend to be a group of related posts, about exceptions and error handling in C#.

Minidumps are a mechanism for "post-mortem debugging" - debugging your application after it is "dead".  A minidump is a snapshot of the memory of your application, typically taken when it is has encountered a fatal error.  Various debuggers support loading minidumps and "debugging" with them, which really means just exploring them, since you can't do things like single-step or change the value of variables when the program isn't running any longer.  The .Net Framework 4.0 and Visual Studio 2010 finally bring easy minidump debugging to C# code.

In Windows, a minidump is created through the MiniDumpWriteDump API http://msdn.microsoft.com/en-us/library/ms680360(VS.85).aspx.  Since that is a native API, we need some interop code in order to write minidumps from C#.  Jochen Kalmbach has done the hard work in his short but excellent post http://blog.kalmbach-software.de/2008/12/13/writing-minidumps-in-c/.  I've taken his example and only made some minor changes, resulting in this...

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;


public static class MiniDump
{
   // Taken almost verbatim from http://blog.kalmbach-software.de/2008/12/13/writing-minidumps-in-c/
   [Flags]
   public enum Option : uint
   {
       // From dbghelp.h:
       Normal = 0x00000000,
       WithDataSegs = 0x00000001,
       WithFullMemory = 0x00000002,
       WithHandleData = 0x00000004,
       FilterMemory = 0x00000008,
       ScanMemory = 0x00000010,
       WithUnloadedModules = 0x00000020,
       WithIndirectlyReferencedMemory = 0x00000040,
       FilterModulePaths = 0x00000080,
       WithProcessThreadData = 0x00000100,
       WithPrivateReadWriteMemory = 0x00000200,
       WithoutOptionalData = 0x00000400,
       WithFullMemoryInfo = 0x00000800,
       WithThreadInfo = 0x00001000,
       WithCodeSegs = 0x00002000,
       WithoutAuxiliaryState = 0x00004000,
       WithFullAuxiliaryState = 0x00008000,
       WithPrivateWriteCopyMemory = 0x00010000,
       IgnoreInaccessibleMemory = 0x00020000,
       ValidTypeFlags = 0x0003ffff,
   };

   public enum ExceptionInfo
   {
       None,
       Present
   }

   //typedef struct _MINIDUMP_EXCEPTION_INFORMATION {
   //    DWORD ThreadId;
   //    PEXCEPTION_POINTERS ExceptionPointers;
   //    BOOL ClientPointers;
   //} MINIDUMP_EXCEPTION_INFORMATION, *PMINIDUMP_EXCEPTION_INFORMATION;
   [StructLayout(LayoutKind.Sequential, Pack = 4)]  // Pack=4 is important! So it works also for x64!
   public struct MiniDumpExceptionInformation
   {
       public uint ThreadId;
       public IntPtr ExceptionPointers;
       [MarshalAs(UnmanagedType.Bool)]
       public bool ClientPointers;
   }

Read more: DonDu's Blog

Posted via email from .NET Info

0 comments: