<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="https://www.w3.org/2005/Atom" xmlns:sy="https://purl.org/rss/1.0/modules/syndication/" xmlns:dc="https://purl.org/dc/elements/1.1/" xmlns:content="https://purl.org/rss/1.0/modules/content/" xmlns:wfw="https://wellformedweb.org/CommentAPI/" xmlns:slash="https://purl.org/rss/1.0/modules/slash/"
  version="2.0">
  <channel>
    <title>DonDu's Blog</title>
    <atom:link
      href="https://docs.microsoft.com/archive/blogs/dondu/feed.xml"
      rel="self"
      type="application/rss+xml" />
    <link>https://docs.microsoft.com/archive/blogs/dondu/feed.xml</link>
    <description>Musings on Software Development</description>
    <lastBuildDate>Wed, 17 Apr 2019 03:39:43 GMT</lastBuildDate>
    <language>en-US</language>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    <item>
      <title>Writing Minidumps from Exceptions in C#</title>
      <link>https://docs.microsoft.com/archive/blogs/dondu/writing-minidumps-from-exceptions-in-c</link>
      <pubDate>Sun, 31 Oct 2010 17:05:00 GMT</pubDate>
      <dc:creator><![CDATA[dondu]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/dondu/2010/10/31/writing-minidumps-from-exceptions-in-c/</guid>
      <description><![CDATA[&nbsp;
This is part of a related series of posts:

Writing Minidumps in C# 
Writing Minidumps...]]></description>
      <content:encoded><![CDATA[<p>&nbsp;</p>
<p>This is part of a related series of posts:</p>
<ul>
<li><a href="https://blogs.msdn.com/b/dondu/archive/2010/10/24/writing-minidumps-in-c.aspx">Writing Minidumps in C#</a> </li>
<li>Writing Minidumps from Exceptions in C# </li>
</ul>
<p>Minidumps are about debugging, and when you write in C# the vast majority of times that you need to debug, it will be because of some exception that was thrown.&nbsp; So let&rsquo;s see what is involved in capturing information about Exceptions in a minidump.</p>
<p>First a simple test method that creates a vaguely interesting callstack and throws an exception&hellip;</p>
<div id="codeSnippetWrapper" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; width: 97.5%; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; cursor: text; border: silver 1px solid; padding: 4px;">
<div id="codeSnippet" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum1" style="color: #606060">   1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> CreateStackWithException(<span style="color: #0000ff">int</span> level = 1)</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum2" style="color: #606060">   2:</span> {</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum3" style="color: #606060">   3:</span>     <span style="color: #0000ff">bool</span> cleanedUp = <span style="color: #0000ff">false</span>;</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum4" style="color: #606060">   4:</span>     <span style="color: #0000ff">try</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum5" style="color: #606060">   5:</span>     {</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum6" style="color: #606060">   6:</span>         <span style="color: #0000ff">if</span> (level == 6)</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum7" style="color: #606060">   7:</span>             <span style="color: #0000ff">throw</span> <span style="color: #0000ff">new</span> Exception();</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum8" style="color: #606060">   8:</span>         CreateStackWithException(level + 1);</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum9" style="color: #606060">   9:</span>     }</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum10" style="color: #606060">  10:</span>     <span style="color: #0000ff">finally</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum11" style="color: #606060">  11:</span>     {</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum12" style="color: #606060">  12:</span>         cleanedUp = <span style="color: #0000ff">true</span>;</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum13" style="color: #606060">  13:</span>     }</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum14" style="color: #606060">  14:</span> }</pre>
<!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></div>
</div>
<p>The <span style="font-family: Courier New;">CreateStackWithException</span> method calls itself recursively until the <span style="font-family: Courier New;">level</span> is 6, at which point is throws an <span style="font-family: Courier New;">Exception</span> object.&nbsp; (The coding guidelines say that you shouldn&rsquo;t throw plain <span style="font-family: Courier New;">Exception</span> objects but of course this is just for exploring behavior so it doesn&rsquo;t matter in this case.)&nbsp; The method also has a <span style="font-family: Courier New;">finally</span> block which cleans up some (very simple) state, to illustrate how that interacts with exception handling and&nbsp; minidumps.</p>
<p>Now a simple method to invoke <span style="font-family: Courier New;">CreateStackWithException</span><span style="font-family: Segoe UI;"> and catch the exception and write a minidump.</span></p>
<div id="codeSnippetWrapper" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; width: 97.5%; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; cursor: text; border: silver 1px solid; padding: 4px;">
<div id="codeSnippet" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum1" style="color: #606060">   1:</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> CatchTest()</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum2" style="color: #606060">   2:</span> {</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum3" style="color: #606060">   3:</span>     <span style="color: #0000ff">try</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum4" style="color: #606060">   4:</span>     {</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum5" style="color: #606060">   5:</span>         CreateStackWithException();</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum6" style="color: #606060">   6:</span>     }</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum7" style="color: #606060">   7:</span>     <span style="color: #0000ff">catch</span> (Exception ex)</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum8" style="color: #606060">   8:</span>     {</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum9" style="color: #606060">   9:</span>         <span style="color: #0000ff">string</span> stackTrace = ex.StackTrace;</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum10" style="color: #606060">  10:</span>         <span style="color: #0000ff">string</span> fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), <span style="color: #006080">"MiniDumpDemo_Catch.mdmp"</span>);</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum11" style="color: #606060">  11:</span>         <span style="color: #0000ff">using</span> (FileStream fs = <span style="color: #0000ff">new</span> FileStream(fileName, FileMode.Create, FileAccess.ReadWrite, FileShare.Write))</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum12" style="color: #606060">  12:</span>         {</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum13" style="color: #606060">  13:</span>             MiniDump.Write(fs.SafeFileHandle, MiniDump.Option.WithFullMemory, MiniDump.ExceptionInfo.Present);</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum14" style="color: #606060">  14:</span>         }</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum15" style="color: #606060">  15:</span>     }</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum16" style="color: #606060">  16:</span> }</pre>
<!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></div>
</div>
<p>The <span style="font-family: Courier New;">CatchTest</span> method calls <span style="font-family: Courier New;">CreateStackWithException</span>.&nbsp; It catches the exception, grabs a local copy of the <span style="font-family: Courier New;">Exception.StackTrace</span> property (you&rsquo;ll see why in a bit), and then writes a minidump to a file called <span style="font-family: Courier New;">MiniDumpDemo_Catch.mdmp</span> in your <span style="font-family: Courier New;">My Documents</span> folder.</p>
<p>Once we execute that and load the minidump into <strong>Visual Studio 2010</strong> and <span style="font-family: Courier New;">Debug with Mixed</span>, we see this popup&hellip;</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/41/58/metablogapi/2100.ExceptionDialog_64DAA5C7.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/CommunityServer-Blogs-Components-WeblogFiles/00-00-01-41-58-metablogapi/2100.ExceptionDialog_5F00_64DAA5C7.png"><img height="257" width="557" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/41/58/metablogapi/6087.ExceptionDialog_thumb_5008AD47.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/CommunityServer-Blogs-Components-WeblogFiles/00-00-01-41-58-metablogapi/6087.ExceptionDialog_5F00_thumb_5F00_5008AD47.png" alt="ExceptionDialog" border="0" title="ExceptionDialog" style="background-image: none; border-bottom: 0px; border-left: 0px; margin: ; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" /></a></p>
<p>We get that pop-up because we included exception pointers in the minidump.</p>
<p>Except&hellip;&nbsp; I am running Windows 7 x64, and when I run this test as a 64-bit process, I get the exception pop-up.&nbsp; But when I run it as a 32-bit process I don&rsquo;t get the pop-up.&nbsp; I investigated, and the call to <span style="font-family: Courier New;">System.Runtime.InteropServices.Marshal.GetExceptionPointers()</span> returns null in the <span style="font-family: Courier New;">catch</span> block, when running as a 32-bit process.&nbsp; The low-level mechanisms for raising exceptions are very different between 32-bit and 64-bit processes, so that is probably the cause.</p>
<p>Let&rsquo;s look at the callstack as displayed in the debugger.</p>
<div id="codeSnippetWrapper" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; width: 97.5%; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; cursor: text; border: silver 1px solid; padding: 4px;">
<div id="codeSnippet" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum1" style="color: #606060">   1:</span> ntdll.dll!NtGetContextThread()  + 0xa bytes    </pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum2" style="color: #606060">   2:</span> ntdll.dll!<span style="color: #0000ff">string</span> <span style="color: #006080">"Dereferencing"</span>()     </pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum3" style="color: #606060">   3:</span> [Managed to Native Transition]    </pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum4" style="color: #606060">   4:</span> MiniDumpDemo.exe!MiniDump.Write(System.Runtime.InteropServices.SafeHandle fileHandle = {Microsoft.Win32.SafeHandles.SafeFileHandle}, MiniDump.Option options = WithFullMemory, MiniDump.ExceptionInfo exceptionInfo = Present) Line 98 + 0x40 bytes    C#</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum5" style="color: #606060">   5:</span> MiniDumpDemo.exe!MiniDumpDemo.Program.CatchTest() Line 63 + 0x2e bytes    C#</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum6" style="color: #606060">   6:</span> MiniDumpDemo.exe!MiniDumpDemo.Program.Main(<span style="color: #0000ff">string</span>[] args = {<span style="color: #0000ff">string</span>[0]}) Line 80 + 0x5 bytes    C#</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum7" style="color: #606060">   7:</span> [Native to Managed Transition]    </pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum8" style="color: #606060">   8:</span> mscoreei.dll!_CorExeMain()  + 0x49 bytes    </pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum9" style="color: #606060">   9:</span> mscoree.dll!_CorExeMain_Exported()  + 0x69 bytes    </pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum10" style="color: #606060">  10:</span> kernel32.dll!BaseThreadInitThunk()  + 0xd bytes    </pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum11" style="color: #606060">  11:</span> ntdll.dll!RtlUserThreadStart()  + 0x21 bytes    </pre>
<!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></div>
</div>
<p>That&rsquo;s not actually a very good result:&nbsp; The exception happened within several levels of recursion inside the <span style="font-family: Courier New;">CreateStackWithException</span> method, but that method doesn&rsquo;t appear on the callstack captured in the debugger at all.&nbsp; All of that context has been lost, making it much harder for us to use the minidump to determine the cause of the exception.</p>
<p>But at least we have the actual <span style="font-family: Courier New;">Exception</span> object in the minidump, and we can inspect that.&nbsp; We&rsquo;ll just use the minidump to view the <span style="font-family: Courier New;">Exception.StackTrace</span> property&hellip;</p>
<div id="codeSnippetWrapper" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; width: 97.5%; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; cursor: text; border: silver 1px solid; padding: 4px;">
<div id="codeSnippet" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum1" style="color: #606060">   1:</span> -        $exception    {<span style="color: #006080">"Exception of type 'System.Exception' was thrown."</span>}    System.Exception</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum2" style="color: #606060">   2:</span> -        StackTrace    <span style="color: #006080">'$exception.StackTrace'</span> threw an exception of type <span style="color: #006080">'System.NotSupportedException'</span>    <span style="color: #0000ff">string</span> {System.NotSupportedException}</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum3" style="color: #606060">   3:</span> +        <span style="color: #0000ff">base</span>    {<span style="color: #006080">"Exception stack trace information is not available while minidump debugging."</span>}    System.SystemException {System.NotSupportedException}</pre>
<!--crlf--></!--crlf--></!--crlf--></!--crlf--></div>
</div>
<p>Whoa!&nbsp; It&rsquo;s a conspiracy!&nbsp; The <span style="font-family: Courier New;">Exception.StackTrace</span> property doesn&rsquo;t work inside of minidumps, and there is an explicit error message generated.</p>
<p>And that&rsquo;s why we snapped a copy of the <span style="font-family: Courier New;">Exception.StackTrace</span> property before we wrote the minidump.&nbsp; We can use the <span style="font-family: Courier New;">Call Stack</span> window to change the context to the <span style="font-family: Courier New;">CatchTest</span> method on the stack, and then we can inspect the local <span style="font-family: Courier New;">strackTrace</span> variable in that call frame.</p>
<div id="codeSnippetWrapper" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; width: 97.5%; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; cursor: text; border: silver 1px solid; padding: 4px;">
<div id="codeSnippet" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum1" style="color: #606060">   1:</span> at MiniDumpDemo.Program.CreateStackWithException(Int32 level) <span style="color: #0000ff">in</span> MiniDumpDemo\Program.cs:line 20</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum2" style="color: #606060">   2:</span> at MiniDumpDemo.Program.CreateStackWithException(Int32 level) <span style="color: #0000ff">in</span> MiniDumpDemo\Program.cs:line 20</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum3" style="color: #606060">   3:</span> at MiniDumpDemo.Program.CreateStackWithException(Int32 level) <span style="color: #0000ff">in</span> MiniDumpDemo\Program.cs:line 20</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum4" style="color: #606060">   4:</span> at MiniDumpDemo.Program.CreateStackWithException(Int32 level) <span style="color: #0000ff">in</span> MiniDumpDemo\Program.cs:line 20</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum5" style="color: #606060">   5:</span> at MiniDumpDemo.Program.CreateStackWithException(Int32 level) <span style="color: #0000ff">in</span> MiniDumpDemo\Program.cs:line 20</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum6" style="color: #606060">   6:</span> at MiniDumpDemo.Program.CreateStackWithException(Int32 level) <span style="color: #0000ff">in</span> MiniDumpDemo\Program.cs:line 20</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum7" style="color: #606060">   7:</span> at MiniDumpDemo.Program.CatchTest() <span style="color: #0000ff">in</span> MiniDumpDemo\Program.cs:line 55</pre>
<!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></div>
</div>
<p>That&rsquo;s much better.&nbsp; I&rsquo;ve personally had many many instances where I&rsquo;ve been given only a callstack like that and the bug is usually painfully obvious.&nbsp; Occasionally it takes a few minutes of thought and reading the code to figure out the bug.&nbsp; Just having the text of the callstack is useful so much of the time that the server software we develop just writes the callstack to a log file and we inspect the log file later.</p>
<p>But it&rsquo;s not actually ideal.&nbsp; Ideal would be to have the full context of the location of the exception in the minidump.&nbsp; The reason that we don&rsquo;t have that is we are writing the minidump from within a <span style="font-family: Courier New;">catch</span> handler, and the way exceptions work, the stack has been unwound and all of the <span style="font-family: Courier New;">finally</span> blocks have run before the <span style="font-family: Courier New;">catch</span> handler starts running.&nbsp; (I&rsquo;m not going to attempt to describe it in any detail here.&nbsp; Here&rsquo;s a post that provides some information:&nbsp; <a href="https://blogs.msdn.com/b/clrteam/archive/2009/02/05/catch-rethrow-and-filters-why-you-should-care.aspx" title="https://blogs.msdn.com/b/clrteam/archive/2009/02/05/catch-rethrow-and-filters-why-you-should-care.aspx">https://blogs.msdn.com/b/clrteam/archive/2009/02/05/catch-rethrow-and-filters-why-you-should-care.aspx</a>)</p>
<p>One solution would be to write the minidump from inside of <span style="font-family: Courier New;">CreateStackWithException</span> just before the exception is thrown.&nbsp; However that would require that you push a lot of the policy related to minidumps (the filename of the minidump, whether to write the minidump or not) down into code that is less likely to have information about those policies.&nbsp; You could solve those problems with dependency injection <a href="https://en.wikipedia.org/wiki/Dependency_Injection" title="https://en.wikipedia.org/wiki/Dependency_Injection">https://en.wikipedia.org/wiki/Dependency_Injection</a> but what if the location where the exception is being thrown is in a module that you can&rsquo;t rebuild?</p>
<p>This post has grown fairly long, so I am going to close.&nbsp; But first here is a taste of a solution&hellip;</p>
<div id="codeSnippetWrapper" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; width: 97.5%; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; cursor: text; border: silver 1px solid; padding: 4px;">
<div id="codeSnippet" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum1" style="color: #606060">   1:</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> FilterTest()</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum2" style="color: #606060">   2:</span> {</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum3" style="color: #606060">   3:</span>     <span style="color: #0000ff">try</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum4" style="color: #606060">   4:</span>     {</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum5" style="color: #606060">   5:</span>         ExceptionFilter.Invoke(</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum6" style="color: #606060">   6:</span>             () =&gt; {</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum7" style="color: #606060">   7:</span>                 CreateStackWithException();</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum8" style="color: #606060">   8:</span>             },</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum9" style="color: #606060">   9:</span>             (ex) =&gt; {</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum10" style="color: #606060">  10:</span>                 <span style="color: #0000ff">string</span> stackTrace = ex.StackTrace;</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum11" style="color: #606060">  11:</span>                 <span style="color: #0000ff">string</span> fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), <span style="color: #006080">"MiniDumpDemo_Filter.mdmp"</span>);</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum12" style="color: #606060">  12:</span>                 <span style="color: #0000ff">using</span> (FileStream fs = <span style="color: #0000ff">new</span> FileStream(fileName, FileMode.Create, FileAccess.ReadWrite, FileShare.Write))</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum13" style="color: #606060">  13:</span>                 {</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum14" style="color: #606060">  14:</span>                     MiniDump.Write(fs.SafeFileHandle, MiniDump.Option.WithFullMemory, MiniDump.ExceptionInfo.Present);</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum15" style="color: #606060">  15:</span>                 }</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum16" style="color: #606060">  16:</span>             });</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum17" style="color: #606060">  17:</span>     }</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum18" style="color: #606060">  18:</span>     <span style="color: #0000ff">catch</span> (Exception)</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum19" style="color: #606060">  19:</span>     {</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum20" style="color: #606060">  20:</span>         <span style="color: #008000">/* ignore */</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum21" style="color: #606060">  21:</span>     }</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum22" style="color: #606060">  22:</span> }</pre>
<!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></div>
</div>
<div id="codeSnippetWrapper" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; width: 97.5%; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; cursor: text; border: silver 1px solid; padding: 4px;">
<div id="codeSnippet" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum1" style="color: #606060">   1:</span> ntdll.dll!NtGetContextThread()  + 0xa bytes    </pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum2" style="color: #606060">   2:</span> [Managed to Native Transition]    </pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum3" style="color: #606060">   3:</span> MiniDumpDemo.exe!MiniDump.Write(System.Runtime.InteropServices.SafeHandle fileHandle = {Microsoft.Win32.SafeHandles.SafeFileHandle}, MiniDump.Option options = WithFullMemory, MiniDump.ExceptionInfo exceptionInfo = Present) Line 98 + 0x40 bytes    C#</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum4" style="color: #606060">   4:</span> MiniDumpDemo.exe!MiniDumpDemo.Program.FilterTest.AnonymousMethod__1(System.Exception ex = {<span style="color: #006080">"Exception of type 'System.Exception' was thrown."</span>}) Line 41 + 0x2e bytes    C#</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum5" style="color: #606060">   5:</span> ExceptionFilter.dll!ExceptionFilter.Invoke(System.Action body = {Method = Could not evaluate expression}, System.Action&lt;System.Exception&gt; filter = {Method = Could not evaluate expression}) Line 89    Unknown</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum6" style="color: #606060">   6:</span> [Native to Managed Transition]    </pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum7" style="color: #606060">   7:</span> ntdll.dll!RtlpExecuteHandlerForException()  + 0xd bytes    </pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum8" style="color: #606060">   8:</span> ntdll.dll!RtlDispatchException()  + 0x38c bytes    </pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum9" style="color: #606060">   9:</span> ntdll.dll!KiUserExceptionDispatcher()  + 0x2e bytes    </pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum10" style="color: #606060">  10:</span> KERNELBASE.dll!RaiseException()  + 0x3d bytes    </pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum11" style="color: #606060">  11:</span> [Managed to Native Transition]    </pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum12" style="color: #606060">  12:</span> MiniDumpDemo.exe!MiniDumpDemo.Program.CreateStackWithException(<span style="color: #0000ff">int</span> level = 6) Line 20    C#</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum13" style="color: #606060">  13:</span> MiniDumpDemo.exe!MiniDumpDemo.Program.CreateStackWithException(<span style="color: #0000ff">int</span> level = 5) Line 20 + 0xa bytes    C#</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum14" style="color: #606060">  14:</span> MiniDumpDemo.exe!MiniDumpDemo.Program.CreateStackWithException(<span style="color: #0000ff">int</span> level = 4) Line 20 + 0xa bytes    C#</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum15" style="color: #606060">  15:</span> MiniDumpDemo.exe!MiniDumpDemo.Program.CreateStackWithException(<span style="color: #0000ff">int</span> level = 3) Line 20 + 0xa bytes    C#</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum16" style="color: #606060">  16:</span> MiniDumpDemo.exe!MiniDumpDemo.Program.CreateStackWithException(<span style="color: #0000ff">int</span> level = 2) Line 20 + 0xa bytes    C#</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum17" style="color: #606060">  17:</span> MiniDumpDemo.exe!MiniDumpDemo.Program.CreateStackWithException(<span style="color: #0000ff">int</span> level = 1) Line 20 + 0xa bytes    C#</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum18" style="color: #606060">  18:</span> MiniDumpDemo.exe!MiniDumpDemo.Program.FilterTest.AnonymousMethod__0() Line 34 + 0xa bytes    C#</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum19" style="color: #606060">  19:</span> ExceptionFilter.dll!ExceptionFilter.Invoke(System.Action body = {Method = Could not evaluate expression}, System.Action&lt;System.Exception&gt; filter = {Method = Could not evaluate expression}) Line 74    Unknown</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum20" style="color: #606060">  20:</span> MiniDumpDemo.exe!MiniDumpDemo.Program.FilterTest() Line 32 + 0xe9 bytes    C#</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum21" style="color: #606060">  21:</span> MiniDumpDemo.exe!MiniDumpDemo.Program.Main(<span style="color: #0000ff">string</span>[] args = {<span style="color: #0000ff">string</span>[0]}) Line 79 + 0x5 bytes    C#</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum22" style="color: #606060">  22:</span> [Native to Managed Transition]    </pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum23" style="color: #606060">  23:</span> mscoreei.dll!_CorExeMain()  + 0x49 bytes    </pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum24" style="color: #606060">  24:</span> mscoree.dll!_CorExeMain_Exported()  + 0x69 bytes    </pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum25" style="color: #606060">  25:</span> kernel32.dll!BaseThreadInitThunk()  + 0xd bytes    </pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum26" style="color: #606060">  26:</span> ntdll.dll!RtlUserThreadStart()  + 0x21 bytes    </pre>
<!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></div>
</div>
<p>Using an exception filter, you can write the minidump before the stack is unwound.&nbsp; That gives you the full callstack from the point where the exception is thrown, without any <span style="font-family: Courier New;">finally</span> blocks having run, so you have the full context in the minidump.</p>
<p>The ExceptionFilter.Invoke method is code I wrote in IL.&nbsp; The C# language doesn&rsquo;t provide support for writing exception filters, so you have to resort to one of several mechanisms to author the code.&nbsp; In my next post, I will cover in detail my ExceptionFilter.Invoke method.</p>]]></content:encoded>
    </item>
    <item>
      <title>Writing Minidumps in C#</title>
      <link>https://docs.microsoft.com/archive/blogs/dondu/writing-minidumps-in-c</link>
      <pubDate>Sun, 24 Oct 2010 18:48:00 GMT</pubDate>
      <dc:creator><![CDATA[dondu]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/dondu/2010/10/24/writing-minidumps-in-c/</guid>
      <description><![CDATA[This is part of a related series of posts:

Writing Minidumps in C#
Writing Minidumps from...]]></description>
      <content:encoded><![CDATA[<p>This is part of a related series of posts:</p>
<ul>
<li>Writing Minidumps in C#</li>
<li><a href="https://blogs.msdn.com/b/dondu/archive/2010/10/31/writing-minidumps-from-exceptions-in-c.aspx">Writing Minidumps from Exceptions in C#</a></li>
</ul>
<p>the first in what I intend to be a group of related posts, about exceptions and error handling in C#.</p>
<p>Minidumps are a mechanism for "post-mortem debugging" - debugging your application after it is "dead".&nbsp; A minidump is a snapshot of the memory of your application, typically taken when it is has encountered a fatal error.&nbsp; 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.&nbsp; The <strong>.Net Framework 4.0</strong> and <strong>Visual Studio 2010</strong> finally bring easy minidump debugging to C# code.</p>
<p>In Windows, a minidump is created through the <span style="font-family: courier new,courier;">MiniDumpWriteDump</span> API <a href="https://msdn.microsoft.com/en-us/library/ms680360(VS.85).aspx">https://msdn.microsoft.com/en-us/library/ms680360(VS.85).aspx</a>.&nbsp; Since that is a native API, we need some interop code in order to write minidumps from C#.&nbsp; Jochen Kalmbach has done the hard work in his short but excellent post <a href="https://blog.kalmbach-software.de/2008/12/13/writing-minidumps-in-c/">https://blog.kalmbach-software.de/2008/12/13/writing-minidumps-in-c/</a>.&nbsp; I've taken his example and only made some minor changes, resulting in this...</p>
<p>&nbsp;</p>
<pre class="scroll"><code class="csharp">using System;
<br />using System.Diagnostics;
<br />using System.Runtime.InteropServices;
<br />
<br />
<br />public static class MiniDump
<br />{
<br />    // Taken almost verbatim from https://blog.kalmbach-software.de/2008/12/13/writing-minidumps-in-c/
<br />    [Flags]
<br />    public enum Option : uint
<br />    {
<br />        // From dbghelp.h:
<br />        Normal = 0x00000000,
<br />        WithDataSegs = 0x00000001,
<br />        WithFullMemory = 0x00000002,
<br />        WithHandleData = 0x00000004,
<br />        FilterMemory = 0x00000008,
<br />        ScanMemory = 0x00000010,
<br />        WithUnloadedModules = 0x00000020,
<br />        WithIndirectlyReferencedMemory = 0x00000040,
<br />        FilterModulePaths = 0x00000080,
<br />        WithProcessThreadData = 0x00000100,
<br />        WithPrivateReadWriteMemory = 0x00000200,
<br />        WithoutOptionalData = 0x00000400,
<br />        WithFullMemoryInfo = 0x00000800,
<br />        WithThreadInfo = 0x00001000,
<br />        WithCodeSegs = 0x00002000,
<br />        WithoutAuxiliaryState = 0x00004000,
<br />        WithFullAuxiliaryState = 0x00008000,
<br />        WithPrivateWriteCopyMemory = 0x00010000,
<br />        IgnoreInaccessibleMemory = 0x00020000,
<br />        ValidTypeFlags = 0x0003ffff,
<br />    };
<br />
<br />    public enum ExceptionInfo
<br />    {
<br />        None,
<br />        Present
<br />    }
<br />
<br />    //typedef struct _MINIDUMP_EXCEPTION_INFORMATION {
<br />    //    DWORD ThreadId;
<br />    //    PEXCEPTION_POINTERS ExceptionPointers;
<br />    //    BOOL ClientPointers;
<br />    //} MINIDUMP_EXCEPTION_INFORMATION, *PMINIDUMP_EXCEPTION_INFORMATION;
<br />    [StructLayout(LayoutKind.Sequential, Pack = 4)]  // Pack=4 is important! So it works also for x64!
<br />    public struct MiniDumpExceptionInformation
<br />    {
<br />        public uint ThreadId;
<br />        public IntPtr ExceptionPointers;
<br />        [MarshalAs(UnmanagedType.Bool)]
<br />        public bool ClientPointers;
<br />    }
<br />
<br />    //BOOL
<br />    //WINAPI
<br />    //MiniDumpWriteDump(
<br />    //    __in HANDLE hProcess,
<br />    //    __in DWORD ProcessId,
<br />    //    __in HANDLE hFile,
<br />    //    __in MINIDUMP_TYPE DumpType,
<br />    //    __in_opt PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam,
<br />    //    __in_opt PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam,
<br />    //    __in_opt PMINIDUMP_CALLBACK_INFORMATION CallbackParam
<br />    //    );
<br />
<br />    // Overload requiring MiniDumpExceptionInformation
<br />    [DllImport("dbghelp.dll", EntryPoint = "MiniDumpWriteDump", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = true)]
<br />    static extern bool MiniDumpWriteDump(IntPtr hProcess, uint processId, SafeHandle hFile, uint dumpType, ref MiniDumpExceptionInformation expParam, IntPtr userStreamParam, IntPtr callbackParam);
<br />
<br />    // Overload supporting MiniDumpExceptionInformation == NULL
<br />    [DllImport("dbghelp.dll", EntryPoint = "MiniDumpWriteDump", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = true)]
<br />    static extern bool MiniDumpWriteDump(IntPtr hProcess, uint processId, SafeHandle hFile, uint dumpType, IntPtr expParam, IntPtr userStreamParam, IntPtr callbackParam);
<br />
<br />    [DllImport("kernel32.dll", EntryPoint = "GetCurrentThreadId", ExactSpelling = true)]
<br />    static extern uint GetCurrentThreadId();
<br />
<br />    public static bool Write(SafeHandle fileHandle, Option options, ExceptionInfo exceptionInfo)
<br />    {
<br />        Process currentProcess = Process.GetCurrentProcess();
<br />        IntPtr currentProcessHandle = currentProcess.Handle;
<br />        uint currentProcessId = (uint)currentProcess.Id;
<br />        MiniDumpExceptionInformation exp;
<br />        exp.ThreadId = GetCurrentThreadId();
<br />        exp.ClientPointers = false;
<br />        exp.ExceptionPointers = IntPtr.Zero;
<br />        if (exceptionInfo == ExceptionInfo.Present)
<br />        {
<br />            exp.ExceptionPointers = System.Runtime.InteropServices.Marshal.GetExceptionPointers();
<br />        }
<br />        bool bRet = false;
<br />        if (exp.ExceptionPointers == IntPtr.Zero)
<br />        {
<br />            bRet = MiniDumpWriteDump(currentProcessHandle, currentProcessId, fileHandle, (uint)options, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
<br />        }
<br />        else
<br />        {
<br />            bRet = MiniDumpWriteDump(currentProcessHandle, currentProcessId, fileHandle, (uint)options, ref exp, IntPtr.Zero, IntPtr.Zero);
<br />        }
<br />        return bRet;
<br />    }
<br />
<br />    public static bool Write(SafeHandle fileHandle, Option dumpType)
<br />    {
<br />        return Write(fileHandle, dumpType, ExceptionInfo.None);
<br />    }
<br />}
<br /></code></pre>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>The most significant change I made was to add an override for the <span style="font-family: courier new,courier;">MiniDumpWriteDump</span> interop declaration, that supports passing <span style="font-family: courier new,courier;">NULL</span> (aka <span style="font-family: courier new,courier;">IntPtr.Zero</span>) for the <span style="font-family: courier new,courier;">expParam</span> parameter.&nbsp; The reason isn't applicable to this particular blog post since we aren't (yet) attempting to store&nbsp;"exception information" in the minidump, but what I found was that on my Windows 7 x64 machine, under particular circumstances the test program&nbsp;running as x86 under WOW64, the <span style="font-family: courier new,courier;">Marshall.GetExceptionPointers</span> method would return <span style="font-family: courier new,courier;">IntPtr.Zero</span>, while running the test program as x64 the method would return a well-formed exception pointer.&nbsp; And when the&nbsp;<span style="font-family: courier new,courier;">ExceptionPointers</span> field of expParam&nbsp;was <span style="font-family: courier new,courier;">IntPtr.Zero</span>, the <span style="font-family: courier new,courier;">MiniDumpWriteDump</span> call would have an Access Violation, the solution to which was to pass <span style="font-family: courier new,courier;">expParam</span>&nbsp;as <span style="font-family: courier new,courier;">IntPtr.Zero</span>.&nbsp; So the two overloads of <span style="font-family: courier new,courier;">MiniDumpWriteDump</span> support either a non-existent <span style="font-family: courier new,courier;">expParam</span> (passed as <span style="font-family: courier new,courier;">IntPtr.Zero</span>), or a well-formed <span style="font-family: courier new,courier;">expParam</span> passed as a <span style="font-family: courier new,courier;">ref MiniDumpExceptionInformation</span> with the <span style="font-family: courier new,courier;">ExceptionPointers</span> field set to the return value from <span style="font-family: courier new,courier;">Marshall.GetExceptionPointers</span>).</p>
<p>Of course we need an example of how to make the call to create a minidump...</p>
<pre class="scroll"><code class="csharp">        static void MainlineTest()
<br />        {
<br />            string fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "MiniDumpDemo_Mainline.mdmp");
<br />            using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite, FileShare.Write))
<br />            {
<br />                MiniDump.Write(fs.SafeFileHandle, MiniDump.Option.WithFullMemory);
<br />            }
<br />        }
</code></pre>
<pre class="scroll"><code class="csharp"><br /></code></pre>
<p>The result is a file called <span style="font-family: courier new,courier;">minidump_mainline.dmp</span> in your <span style="font-family: courier new,courier;">My Documents</span> folder.&nbsp; If you open that file in Visual Studio 2010, you'll get a page that displays information about the minidump.&nbsp; On the right is a link to <span style="font-family: courier new,courier;">Debug With Mixed</span>, which will start the debugger against the minidump.&nbsp; At that point you can explore the call stack and inspect variables and fields.&nbsp; The callstack will show execution as being in the middle of the call <span style="font-family: courier new,courier;">MiniDump.Write</span>...</p>
<pre class="scroll"><code class="csharp"> 	ntdll.dll!NtGetContextThread()  + 0xa bytes	
<br /> 	ntdll.dll!string "Dereferencing"() 	
<br /> 	[Managed to Native Transition]	
<br />&gt;	MiniDumpDemo.exe!MiniDump.Write(System.Runtime.InteropServices.SafeHandle fileHandle = {Microsoft.Win32.SafeHandles.SafeFileHandle}, MiniDump.Option options = WithFullMemory, MiniDump.ExceptionInfo exceptionInfo = None) Line 94 + 0x42 bytes	C#
<br /> 	MiniDumpDemo.exe!MiniDump.Write(System.Runtime.InteropServices.SafeHandle fileHandle = {Microsoft.Win32.SafeHandles.SafeFileHandle}, MiniDump.Option dumpType = WithFullMemory) Line 105 + 0x11 bytes	C#
<br /> 	MiniDumpDemo.exe!MiniDumpDemo.Program.MainlineTest() Line 73 + 0x28 bytes	C#
<br /> 	MiniDumpDemo.exe!MiniDumpDemo.Program.Main(string[] args = {string[0]}) Line 81 + 0x5 bytes	C#
<br /> 	[Native to Managed Transition]	
<br /> 	mscoreei.dll!_CorExeMain()  + 0x49 bytes	
<br /> 	mscoree.dll!_CorExeMain_Exported()  + 0x69 bytes	
<br /> 	kernel32.dll!BaseThreadInitThunk()  + 0xd bytes	
<br /> 	ntdll.dll!RtlUserThreadStart()  + 0x21 bytes	
</code></pre>
<p>In the next post, writing minidumps from inside exception handlers to capture as much information as possible about why the application is failing.</p>
<p>&nbsp;</p>]]></content:encoded>
    </item>
  </channel>
</rss>