views:

90

answers:

3

Does anybody know of a function to convert the EXCEPTION_POINTERS structure returned from GetExceptionInformation() into a string that I can log?

I don't want to roll my own if it's already been done.

EDIT: Basically, I've added the __try{} __except(){} blocks to help the app fail gracefully on a critical error. While I'm at it, I'm trying to log as detailed of an error message as possible to locate the issue for us to resolve. Ideally, I'd like to print out the file name and line it failed on, but I doubt that's possible, so I'm hoping to dump all the exception information in the hopes that we'll be able to come as close as possible to pinpointing the exact cause of the problem.

+2  A: 

There is no such function, since you'll need private symbols to write anything meaningful. The dbghelp.dll helps with some of this (specifically the StackWalk function and its 64-bit variant)

What do you want to get out of the exception record to put into the log? Just the exception code? The register context? The stack backtrace?

EDIT: Also, if you just do nothing, but register for Windows Error Reporting, you can just use Microsoft's awesome service and get the crash dumps back, bucketed by popularity. If you can, that's by-far the best way to record crash dumps.

Paul Betts
+2  A: 

From here.

#include <windows.h>
#include <iostream>
#include <string.h>
#include <eh.h>
using namespace std;

static void translateSEH(unsigned int u, EXCEPTION_POINTERS* pExcept) {
  // Translate SEH exception to a C++ exception.  NOTE: compiling with /EHa is required!!
  static char msg[256];
  sprintf_s(msg, 256, "Unhandled exception 0x%08x at 0x%08x",
    pExcept->ExceptionRecord->ExceptionCode,
    pExcept->ExceptionRecord->ExceptionAddress);
  throw exception(msg);
}

int main(){
  _set_se_translator(translateSEH);
    int p = 0;
    try {
        cout<<1 / p<<endl;   
    }
  catch (std::exception& ex) {
        cout << ex.what() << endl;
    }
}
Knowles Atchison
I would argue that *just* the code and address isn't particularly useful, especially with ASLR on Vista/Win7, since you didn't record the module base
Paul Betts
+1  A: 

There isn't much to it, you'll only be interested in the exception code and address. If the exception is an EXCEPTION_ACCESS_VIOLATION then you also want to dump the first two ExceptionInformation values. The first one indicates the operation (0=read, 1=write), the second one gives the fault address.

Hans Passant