How to configure Debug Diagnostic to capture Stack Overflow exceptions

I have just found that one of my colleauges, Spike, posted a nice blog article here explaining how to create a StackOverflowException using .NET Framework. He also explained how to find the root cause of the issue with live debugging using WinDBG.

In some of cases, especially where live debugging is not possible, you may want to generate dump of the worker process (w3wp.exe) when a Stack Overflow exception happens.

How to understand if the exception is Stack Overflow

The easiest way to check the application and system event logs. I just created a web site with Spike’s sample code and when the crash happens, I can see the following is logged in the application event logs:

Application: w3wp.exe
Framework Version: v4.0.30319
Description: The process was terminated due to stack overflow.

After you suspect the process is crashing with Stack Overflow then you need to collect crash dump with a debugger. I prefer Debug Diagnostic Tool v1.2. You can install the DebugDiag 1.2 from https://www.microsoft.com/download/en/details.aspx?id=26798.

How to create a rule for Stack Overflow in DebugDiag

As Spike explained, it is difficult to capture a Stack Overflow exception:

One of the problems with StackoverflowExceptions can be found in the documentation.

“StackOverflowException Class”
https://msdn.microsoft.com/en-us/library/system.stackoverflowexception.aspx

“Starting with the .NET Framework version 2.0, a StackOverflowException object cannot be caught by a try-catch block and the corresponding process is terminated by default.”

So it is a bit hard to protect against since the process goes away when the exception occurs.

So it is not possible to catch it with a “normal” crash rule in DebugDiag, but on the other hand it is not complicated or difficult at all. All you need to do is to configure the DebugDiag to generate a dump when a Stack Overflow happens. The exception code for Stack Overflow is C00000FD.

The steps are as easy as 1-2-3:

  1. Open DebugDiag 1.2. If no wizard shows up, click Add Rule button.

  2. Choose “Crash”, click next and then choose “A specific IIS web application pool” then click next again.

  3. Select the application pool which crashes from the list and then click next. You will see the Advanced Configuration window. Click “Exceptions” button in “Advanced Settings”:

  4. You will see another window. Click Add Exception. You will see a list with exception codes. Choose “C00000FD – Stack Overflow”. “Choose Full Userdump” to generate a full dump of the process:

  5. Then click OK, Save & Close, Next, Next and Finish.

This DebugDiag rule should capture the Stack Overflow exception.

Enjoy debugging.

--
AMB