26

I have a StackOverFlow occurring somewhere in my application - and I'm trying to figure out ways to track it down.

My event logs show a crash every day or so with the following information:

Faulting application name: MyApp.exe, version: 1.0.0.0, time stamp: 0x522e8317

Faulting module name: clr.dll, version: 4.0.30319.18047, time stamp: 0x515530ce

Exception code: 0xc00000fd

Fault offset: 0x000000000000c657

Faulting process id: 0x117fc

Faulting application start time: 0x01ceadf607b184d2

Faulting application path: C:\Users\Administrator\Desktop\MyApp.exe

Faulting module path: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clr.dll

Report Id: d52424aa-1a16-11e3-bc4b-002590a4ec55

I read that 0xc00000fd is a stack overflow, but I am unsure on where it could be occurring (very large codebase), and how to track it down. Any ideas?

Community
  • 1
  • 1
mariocatch
  • 8,305
  • 8
  • 50
  • 71
  • 3
    do you have any other - application level - logging? – Davin Tryon Sep 10 '13 at 14:40
  • My first guesses would be some condition that's causing an infinite loop or a significant amount of recursion. – mclark1129 Sep 10 '13 at 14:41
  • It would probably be a pain, but you could try and manually go through each `for`, `while`, and any other loop to make sure that they eventually exit. – Ming Slogar Sep 10 '13 at 15:07
  • 2
    Regarding the loop comments: infinite loops would more than likely never cause a stack overflow. Recursion is almost certainly the cause. – Joe Enos Sep 10 '13 at 15:13
  • 3
    Using WinDbg + SOS is the easiest way to know what is going on for sure. Setup ADPlus to take a minidump when your process crashes, and examine the dump to see what's going on. – vcsjones Sep 10 '13 at 15:41
  • @DavidBrabant You can't "handle" a StackOverflowException. The process is toast, so your event handler won't even get called. – vcsjones Sep 10 '13 at 15:42
  • @JoeEnos What if the loop is adding to a collection or otherwise allocating resources on each iteration? – mclark1129 Sep 10 '13 at 15:45
  • @MikeC That should result in an OutOfMemoryException, not StackOverflowException. – Joe Enos Sep 10 '13 at 15:51
  • @JoeEnos Doh, ok you win! :) – mclark1129 Sep 10 '13 at 16:11

2 Answers2

19

This is typically something I use WinDbg to track down, otherwise it's just a guessing game. Here's a quick walkthrough that should set you in the right direction.

WinDbg is a debugger for Windows, good for debugging managed and unmanaged code. It's also great for examining crash dumps. Let's start with an example program.

class Program
{
    static void Main(string[] args)
    {
        IWillStackOverflow(0);
    }

    [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
    static int IWillStackOverflow(int i)
    {
        return IWillStackOverflow(i + 1);
    }
}

This is a pretty contrived example, but let's roll with it. It does indeed stack overflow, and nor does it provide a stacktrace. This is where WinDbg comes in. First you need to install it, which is part of the Debugging Tools in the Windows SDK. There are two versions, the x64 and x86. You'll want to run the one that matches the bitness of your application.

In WinDbg, use File -> Open Executable and run your executable with WinDbg attached. The debugger will break as soon as your application loads, you can use the g command to Go and use your application until you get a StackOverflowException. Before you do that though, make sure your symbols are correct - usually running .symfix+ will correct it, then you can go.

When you get your StackOverflowException, the debugger will break on the thread that raised the exception, and the message will be something like this:

(cc0.b00): Stack overflow - code c00000fd (first chance)

Now we can load the managed debugging extensions with this command (I am assuming you are using the .NET Framework 4.0 or 4.5 here):

.loadby sos clr

And calling !clrstack. In this example, the output is:

000000d440c76040 00007ffb282b0111 StackOverflower.Program.IWillStackOverflow(Int32) [Program.cs @ 20]
000000d440c76080 00007ffb282b0111 StackOverflower.Program.IWillStackOverflow(Int32) [Program.cs @ 20]
000000d440c760c0 00007ffb282b0111 StackOverflower.Program.IWillStackOverflow(Int32) [Program.cs @ 20]
..Repeat thousands of times..

So we have our managed stack at the time of the StackOverflowException.

If your Application doesn't StackOverflow very easily, you can configure ADPlus to take a memory dump of your application at the time of the StackOverflowException. ADPlus is another big-hammer tool, but it's effective. First you need a configuration for ADPlus, here is an example one:

<ADPlus> 
   <!-- Add log entry, log faulting thread stack and dump full on first chance StackOverflow --> 
<Exceptions> 
     <Config> 
        <!-- Use sov for stack overflow exception --> 
       <Code> sov </Code> 
       <Actions1> Log;Stack;FullDump </Actions1> 
       <!-- Depending on what you intend - either stop the debugger (Q or QQ) or continue unhandled (GN) --> 
       <ReturnAction1> GN </ReturnAction1> 
     < Config> 
  </Exceptions> 
</ADPlus>

This configuration example was originally published by user jaskis on the MSDN Forums: http://blogs.msdn.com/b/jaskis/archive/2010/08/11/cwa-ends-up-with-blank-screen-on-browser-on-iis-7.aspx Then use the command line to start your application with the configuration.


This is just an example, WinDbg is a very powerful tool, though it has a bit of a learning curve. There are a lot of good resources on the internet for getting the hang of it. Tess Ferrandez has many articles in her blog that cover WinDbg and the managed debugging extensions.

vcsjones
  • 138,677
  • 31
  • 291
  • 286
  • 1
    I was hoping you would turn your comment in to an answer, I didn't really want to write this up my self either :). The only thing missing is how to set up to get a minidump of the program on crash and how to load that in to WinDbg instead of running the program inside the debugger, The OP said it crashes once a day so the debugger would have to be attached for a long time for it to see it. – Scott Chamberlain Sep 10 '13 at 16:00
  • For further reading I found [this blog posting](http://voneinem-windbg.blogspot.com/2007/03/creating-and-analyzing-minidumps-in-net.html) that goes over getting your C# code to generate a dump on crash and how to open it up in WinDbg – Scott Chamberlain Sep 10 '13 at 16:09
  • @ScottChamberlain I added a little information about ADPlus, hopefully enough to be useful. Thanks for the feedback. – vcsjones Sep 10 '13 at 16:11
  • Thanks for the answer. I had to tweak the sos loading as per [MSDN](https://learn.microsoft.com/en-us/windows-hardware/drivers/debugger/debugging-managed-code) and made sure I ran as admin, but this all enabled me to get to the bottom of my EF SO error :-) – Adam Houldsworth Nov 14 '17 at 14:19
5

This has already been answered, but I was searching for a better answer than WinDbg on the affected machine, since this is only happening on a production server machines for me where I don't want to install WinDbg. I found this article where it refers to a downloadable tool from Microsoft that allows you to configure, on a per-exception code basis, how the system should respond to different exceptions. This will (hopefully) allow debugging on a different machine than the production machine:

This DebugDiag utility can be configured to generate a crash dump when the StackOverflowException occurs. Download it here.

Appurist - Paul W
  • 1,291
  • 14
  • 22
  • 1
    Thanks! I was able to save a dump file from VS from the Debug menu, then analyze it with [DebugDiag](https://www.microsoft.com/en-us/download/details.aspx?id=58210) and found the source of the stack overflow exception in a 3rd party library. – Shahin Dohan Feb 28 '22 at 16:03