Is it possible to debug UnhandledExceptionFilters with a debugger?

2.3k views Asked by At

In the Microsoft Windows API, you can use SetUnhandledExceptionFilter, to set a handler for unhandled exceptions. The big catch, mentioned on that page, is:

If an exception occurs in a process that is not being debugged, and the exception makes it to the unhandled exception filter, that filter will call the exception filter function specified by the lpTopLevelExceptionFilter parameter.

(emphasis added)

Which basically means, if the process is getting debugged, the debugger gets the exception, and my filter is skipped!

I can test & debug my ExceptionFilter the old-fashioned way, with printfs and trial-n-error.

But am I missing something? Is there a good way to interactively debug an ExceptionFilter if it is disabled when in a debugger?

2

There are 2 answers

0
Roger Stewart On BEST ANSWER

Checkout the Resolution section of KB173652 which talks about placing all the code in main/WinMain in a _try/_except block like the following.

void main (int argc, char **argv)
{
   __try
   {
   // all of code normally inside of main or WinMain here...

   }
   __except (MyUnFilter (GetExceptionInformation()))
   {
      OutputDebugString ("executed filter function\n");
   }
}

Another article, Debugging custom filters for unhandled exceptions, describes a couple more techniques in addition to the one above. I personally use the one where you display a message box inside your exception filter and then attach the debugger. I use IsDebuggerPresent to determine whether to display the message box or not.

0
rrirower On

I know this post has been around for a while, but, I just happened upon it searching for something else. I’m happy to say that what user ‘abelenky’ asks is possible if the filter exists in a separate dll. You can debug an unhandled exception filter using a debugger. I’ve done it, and, here’s how:

  • The exception filter must exist in a separate dll. You’ll see why later.

You’ll need to add some code to the filter that displays a message box. I use the following code:

#ifdef _DEBUG
    AfxMessageBox (_T("At this time, you must attach the debugger to this process in order to debug the filter code."));
#endif

The #ifdef is important because you don’t want the code executing in a Release build. I placed the above code at the very top of my filter.

To debug the filter:

  1. Build a Release version of your application in Visual Studio (instance #1).
  2. Build a Debug version of your filter in a second instance of VS (#2).
  3. Copy the Debug version of the filter to the Release folder of your application.
  4. Start your Release application from the Debug menu “without debugging”.
  5. Cause a crash in your application.
  6. When the debug message box (above) appears, change to the second instance (#2) of Visual Studio.
  7. In the #2 instance, open the filter project in Debug (if it isn't open) and attach the debugger to your Application instance.
  8. Set a breakpoint in your filter code after the message box displays.
  9. Close the message box and your breakpoint should be hit.
  10. Continue to debug your code.