Following some of the samples posted in Mike Stall’s blog and after actually needing something like this at work, I’ve decided to write a small program called ExceptionDbg which uses MDbg’s APIs and tracks all exceptions being thrown in an application (including those that are being swallowed by try..catch blocks).

We had a case of exceptions being thrown in the finalization code (this is C++ code that has destructors and therefore has finalizers. I’ve talked about C++ destructor in this previous post).

ExceptionDbg let you attach to a process and dump all of the exceptions being thrown live to your console and/or log file.

There are a few filters on the exception types being logged including:

  • Show only exceptions being caught by try..catch blocks
  • Show full Call Stack and if debug symbols are present, show line numbers as well as source file names.

So what is it good for, you ask?

  • Check exceptions in the finalizer thread – These exceptions are ALWAYS being caught so the finalizer thread will not die. It can mean that if you have some code that frees some unmanaged resources they will not get freed and this is a potential memory leak.
  • Got a performance problem? Check how what are all the exceptions being thrown and caught (try..catch blocks) to verify that some pieces of code doesn’t use exception throwing as a logic to handle things other than failure.

As you can see, it’s useful for a number of scenarios and it help me a lot in finding some of these problems.

You can get the binaries here and the source code here.

You need the .NET Framework 2.0 for this which can be downloaded from here.
If you want to compile it you’ll need the .NET Framework 2.0 SDK which you can download from here.

Enjoy and send me feedback!

  • Parag

    Inside try catch block i am trying to create DivideByZero excption and eating the exception in catch block. When i log the exception using ExceptionDbg.exe i keep getting the following:

    * Starting up.
    * Attaching to process “w3wp”…Done!
    * Debugging Started
    2007/07/04 11:06:08.8593

    Can’t inspect due to Exception.
    Exception Reason: The code is currently unavailable. (Exception from HRESULT: 0x80131309)

    2007/07/04 11:06:08.9687

    Can’t inspect due to Exception.
    Exception Reason: The code is currently unavailable. (Exception from HRESULT: 0x80131309)

    or sometimes simply “Object reference not set to an instance of an object”.

    Looking at the source code (ExceptionDbg.cs) it keeps getting in catch block (Line:222) as a result of which i do not get complete exception stack trace.

    Things that i have taken care of:
    – Debug mode is true.
    – Symbol file is present along with application dll.

    What i am doing wrong?

    Any help is appreciated!!

  • Hello Parag,

    Sorry for the long delay in responding.

    I looked at the code and saw the catch block you are referring to. I can’t reproduce this at the moment but I suspect it might have something to do with the debug symbols.

    I would try the following, just to be on the safe side:
    – Comment out line 205 (the line which we add the source code information to the string builder).
    – Recompile ExceptionDbg and try again.

    If it works fine when that line is commented out, there might be some issue with the debug symbols. Did you try to throw a different type of exception, just to check that it works fine with, say, ApplicationException at that same point in code?

    If it works with ApplicationException, the problem might be with the DivideByZeroException.

    Also, your DivideByZeroException might be thrown inside a catch block inside your code. In that case, the stack is already rolled due to the catch and the throw of the DivideByZeroException might mess things up causing the problem with ExceptionDbg.

  • Parag

    Hello Eran,

    Thanks for the reply. You are right. It seems to be a problem with debug symbols. After commenting line 205 i dont get the error. Though i am getting error at one more place and i suspect that is also because of debug symbols.

    Line:210 GetFullStackTrace(frame, stackTrace);

    At one point of time during recursion it throws the error below as a result of which i do not it does not print even half of the stack trace.
    To overcome the problem put this line of code inside try..catch. Any ideas on that how can i avoid the below exception. Any place where i can set the path for
    debug symbols. I have _NT_SYMBOL_PATH properly setup on my machine though.

    System.Runtime.InteropServices.COMException was caught
    Message=”The code is currently unavailable. (Exception from HRESULT: 0x80131309)”
    at Microsoft.Samples.Debugging.CorDebug.NativeApi.ICorDebugFrame.GetFunction(ICorDebugFunction& ppFunction)
    at Microsoft.Samples.Debugging.CorDebug.CorFrame.get_Function()
    at Microsoft.Samples.Debugging.MdbgEngine.MDbgILFrame.get_Function()
    at DumpExceptions.ExceptionDbg.GetFullStackTrace(MDbgFrame frame, StringBuilder sb) in D:\work\trying\ExceptionDbg-0.1-Sources\ExceptionDbg.cs:line 75
    at DumpExceptions.ExceptionDbg.GetFullStackTrace(MDbgFrame frame, StringBuilder ………..

  • Hi Parag,

    I’m assuming the debug symbols you want to use are of .NET assemblies.
    .NET will look for the symbols files exactly near the binaries themselves.

    _NT_SYMBOL_PATH won’t help you in managed debugging.

    Make sure your application doesn’t accidentally use an assebmly that was GACed, since .NET will first try to load that assembly from the GAC before trying to find it on your search path.
    If it does, make sure to remove all relevant GACed assemblies.

    I would also try to compile the application in debug mode, just in case and try to run ExceptionDbg with the debug version of the application.