Search code examples
c++windowsdllcrash-reportsgoogle-breakpad

Breakpad exception handler not used in a DLL on WIndows?


I am working on a python application that loads a C++ DLL. In such DLL we do all the heavy work and we want to add Google's breakpad crash report system into it. On Windows, we instanciate an exception handler once that DLL is loaded. However, that exception handler is never called when a crash occurs and the minidump is never written. When we use the same setup for a simple C++ console application, everything works fine. Apparently something prevents the exception handler from being notified only when it is instanciated in a DLL.

How can we make sure that Google's breakpad exception handler is called in a DLL?

Below is the setup we use. Framework is a singleton that is created just before we start to use the DLL.

#  include <client/windows/handler/exception_handler.h>

bool callback(
  const wchar_t* /*dump_path*/, const wchar_t* /*minidump_id*/, 
  void* /*context*/, EXCEPTION_POINTERS* /*exinfo*/,  MDRawAssertionInfo* /*assertion*/,
  bool succeeded )
{
  std::cout << "dump callback called" << std::endl;
  return succeeded;
}

class Framework
{
  Framework()
    : handler{ std::make_unique<google_breakpad::ExceptionHandler>(  
        L".",       // dump path
        nullptr,    // no filter
        callback,   // to call after writing the minidump
        nullptr,    // callback does not use context
        google_breakpad::ExceptionHandler::HANDLER_ALL ) }
  {
    std::cout << "Exception handler registered" << std::endl;
  }

  ~Framework()
  {
    std::cout << "Exception handler destroyed" << std::endl;
  }  

private:
  std::unique_ptr<google_breakpad::ExceptionHandler> handler;
};

P.S. : Breakpad handler works fine in our linux version of the app which has the same setup.

Thanks for your help.


Solution

  • Crashes and their causes are handled completely different on Windows and on Linux. Let's start with the Linux case and explain why that case works successfully.

    On Linux the handling of crashes is done on your program side through a signal handler. These are registered for your process with the system and are called once such a signal has been sent to your process. The signal handler runs completely independent from your normal code flow and the same signal handler will be called wherever the signal originates in your program. Breakpad installs signal handlers for typical signals like SIGSEGV, SIGILL, ...

    On Windows the handling of crashes and related problems is not using signals and instead uses a special kind of exceptions called SEH (structured exception handling). These exceptions are working quite similar to normal C++ exceptions but are normally caught through __except instead of catch. This normal kind of exception handling requires that your google_breakpad::ExceptionHandler object is destroyed by the automatic cleanup for the handler recognizing a crash. There is no equivalent solution to the Linux signal handler on Windows. In a typical application if you want to report crashes through breakpad you normally create the google_breakpad::ExceptionHandler object quite early in the start up code so that it will be destructed when an SEH uncaught exception reaches that place.