0

I spent much time to capture unhandled exceptions in my process (win32) using API so called setunhandledexceptionfilter().

But I haven't captured exception when WER(Windows Error Report - which is well know for DR.watson) is showed.

Is impossible to catch all of exceptions without third-party in my APP?

I think that there is method for handling, but I don't get it.

I am not accustomed to Windows DEV environment. that's why I lost my mental in googling.

Below is my test-case in vc110(Visual Studio 2012).

chat test[65];
int main() { 
 // after attaching unhandled exception call-back using setunhandledexceptionfilter()
 // die point (ACCESS_VIOLATION c0000005)
 for (int k=0; k<1000000; k++) 
     test[k]=65;

My callback isn't called after WER(windows Error Report) occurs. It doesn't work as my intend. *But strcpy(NULL, "TEST") which is okay (SUCCESS)*

Below is my source code.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/stat.h>
#include <assert.h>
#include <process.h>
#include <direct.h>
#include <conio.h>
#include <time.h>
#include <Windows.h>

#include <tchar.h>
#include <dbghelp.h>
#include <stdio.h>
#include <crtdbg.h>
#include <WinBase.h>
#pragma comment ( lib, "dbghelp.lib" )


void CreateMiniDump( EXCEPTION_POINTERS* pep ); 
BOOL CALLBACK MyMiniDumpCallback(
    PVOID                            pParam, 
    const PMINIDUMP_CALLBACK_INPUT   pInput, 
    PMINIDUMP_CALLBACK_OUTPUT        pOutput 
); 

///////////////////////////////////////////////////////////////////////////////
// Minidump creation function 
//

#if 0
LONG WINAPI lpTopLevelExceptionFilter(EXCEPTION_POINTERS* ExceptionInfo);
#endif

void CreateMiniDump( EXCEPTION_POINTERS* pep ) 
{
    time_t t;
    struct tm *tinfo;
    wchar_t dump_name[128];
    HANDLE hFile;
    time(&t);
    tinfo = localtime(&t);

    wcsftime(dump_name, 128, L"MiniDump[%Y%m%d][%H_%M_%S].dmp", tinfo);

    // file format MiniDump[YYYYMMDD][HH_MM_SEC]
    hFile = CreateFile(dump_name, GENERIC_READ | GENERIC_WRITE, 
        0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL ); 

    if( ( hFile != NULL ) && ( hFile != INVALID_HANDLE_VALUE ) ) 
    {
        // Create the minidump 

        MINIDUMP_EXCEPTION_INFORMATION mdei; 
        MINIDUMP_CALLBACK_INFORMATION mci; 
        MINIDUMP_TYPE mdt;
        BOOL rv;

        mdei.ThreadId           = GetCurrentThreadId(); 
        mdei.ExceptionPointers  = pep; 
        mdei.ClientPointers     = FALSE; 



        mci.CallbackRoutine     = (MINIDUMP_CALLBACK_ROUTINE)MyMiniDumpCallback; 
        mci.CallbackParam       = 0; 

        mdt       = (MINIDUMP_TYPE)(MiniDumpWithIndirectlyReferencedMemory | MiniDumpScanMemory| MiniDumpWithThreadInfo); 

        rv = MiniDumpWriteDump( GetCurrentProcess(), GetCurrentProcessId(), 
            hFile, mdt, (pep != 0) ? &mdei : 0, 0, &mci ); 

        if( !rv ) 
            _tprintf( _T("MiniDumpWriteDump failed. Error: %u \n"), GetLastError() ); 
        else 
            _tprintf( _T("Minidump created.\n") ); 

        // Close the file 

        CloseHandle( hFile ); 

    }
    else 
    {
        _tprintf( _T("CreateFile failed. Error: %u \n"), GetLastError() ); 
    }

}


///////////////////////////////////////////////////////////////////////////////
// Custom minidump callback 
//

BOOL CALLBACK MyMiniDumpCallback(
    PVOID                            pParam, 
    const PMINIDUMP_CALLBACK_INPUT   pInput, 
    PMINIDUMP_CALLBACK_OUTPUT        pOutput 
) 
{
    BOOL bRet = FALSE; 


    // Check parameters 

    if( pInput == 0 ) 
        return FALSE; 

    if( pOutput == 0 ) 
        return FALSE; 


    // Process the callbacks 

    switch( pInput->CallbackType ) 
    {
        case IncludeModuleCallback: 
        {
            // Include the module into the dump 
            bRet = TRUE; 
        }
        break; 

        case IncludeThreadCallback: 
        {
            // Include the thread into the dump 
            bRet = TRUE; 
        }
        break; 

        case ModuleCallback: 
        {
            // Does the module have ModuleReferencedByMemory flag set ? 

            if( !(pOutput->ModuleWriteFlags & ModuleReferencedByMemory) ) 
            {
                // No, it does not - exclude it 

                wprintf( L"Excluding module: %s \n", pInput->Module.FullPath ); 

                pOutput->ModuleWriteFlags &= (~ModuleWriteModule); 
            }

            bRet = TRUE; 
        }
        break; 

        case ThreadCallback: 
        {
            // Include all thread information into the minidump 
            bRet = TRUE;  
        }
        break; 

        case ThreadExCallback: 
        {
            // Include this information 
            bRet = TRUE;  
        }
        break; 

        case MemoryCallback: 
        {
            // We do not include any information here -> return FALSE 
            bRet = FALSE; 
        }
        break; 

        case CancelCallback: 
            break; 
    }

    return bRet; 

}

LONG WINAPI exception_filter_func(EXCEPTION_POINTERS* pep)
{
    if (pep == NULL) {
        return EXCEPTION_EXECUTE_HANDLER;
    }

    if (pep->ExceptionRecord->ExceptionCode == EXCEPTION_STACK_OVERFLOW) {
        HANDLE hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)CreateMiniDump, pep, 0, NULL);
        WaitForSingleObject(hThread, INFINITE);
        CloseHandle(hThread);
    } else {
        CreateMiniDump(pep);
    }

    return EXCEPTION_EXECUTE_HANDLER;
}
char test[65];

int main(int argc, char **argv)
{
    int k;
    SetUnhandledExceptionFilter(exception_filter_func);
    // exception occured (ACCESS_VIOLATION)
    for (k=0; k<1000000; k++) 
      test[k]=65;
}
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
raphael
  • 1
  • 4
  • Is your application *that* bad? What are the exceptions you can't handle? – Roger Rowland Dec 19 '13 at 06:19
  • Yes, you use SetUnhandledExceptionFilter. Works fine, you are doing it wrong. – Hans Passant Dec 19 '13 at 11:01
  • Nobody does not guarantee the future. as you know, there are many exceptions when I tested some-case, I have met this situation. – raphael Dec 19 '13 at 11:54
  • 1
    Please post the code you are using to try and capture the exceptions. I have experience in writing this type of a filter. – rrirower Dec 19 '13 at 13:30
  • Hi rrirower, I attached my source code which is only for unhandled exceptions. if any doubt, let me know that. thank you for your help. – raphael Dec 20 '13 at 03:54

0 Answers0