UNIX(Linux) 에서는 core 파일이 생성되어 debug에 사용될수 있다.
그런데, Windows에서는 상대적으로 이런 부분에 대해서는 별도의 제어가 필요하다.
다음의 코드는 Exception이 발생되었을때 간단히 사용할수 있는 Source이다.
#include <windows.h>
#include <dbghelp.h>
#include <tchar.h>
///////////////////////////////////////////////////////////////////////////////
// Minidump creation function
//
typedef BOOL (WINAPI *MINIDUMPWRITEDUMP)(
HANDLE hProcess, DWORD dwPid, HANDLE hFile, MINIDUMP_TYPE DumpType,
CONST PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam,
CONST PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam,
CONST PMINIDUMP_CALLBACK_INFORMATION CallbackParam
);
// ::SetUnhandledExceptionFilter( (LPTOP_LEVEL_EXCEPTION_FILTER)ExceptionMiniDump);
#pragma warning(disable: 4996)
LONG __stdcall ExceptionMiniDump( EXCEPTION_POINTERS *pExp)
{
HMODULE hDll = NULL;
if (!(hDll = LoadLibrary( _T("DBGHELP.DLL"))))
;
else {
MINIDUMPWRITEDUMP pfnMiniDumpWriteDump =
(MINIDUMPWRITEDUMP)GetProcAddress( hDll, "MiniDumpWriteDump" );
if (pfnMiniDumpWriteDump)
{
TCHAR dumpFile[MAX_PATH];
HANDLE hFile;
GetModuleFileName( NULL, dumpFile, sizeof(dumpFile)); {
TCHAR *ext = _tcsrchr( dumpFile, '.');
_stprintf( ext ?
ext: dumpFile + _tcsclen( dumpFile), _T("_%d.dump"), GetCurrentProcessId());
} hFile = CreateFile( dumpFile,
GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile && (hFile != INVALID_HANDLE_VALUE))
{
// Create the minidump
MINIDUMP_EXCEPTION_INFORMATION ExInfo;
ExInfo.ThreadId = GetCurrentThreadId();
ExInfo.ExceptionPointers = pExp;
ExInfo.ClientPointers = FALSE;
pfnMiniDumpWriteDump( GetCurrentProcess(),
GetCurrentProcessId(), hFile, MiniDumpNormal, pExp? &ExInfo: NULL, NULL, NULL);
// Close the file
CloseHandle( hFile );
return EXCEPTION_EXECUTE_HANDLER;
}
}
} return EXCEPTION_CONTINUE_SEARCH;
}
#pragma warning(default: 4996)
void InstallException()
{
SetUnhandledExceptionFilter( (LPTOP_LEVEL_EXCEPTION_FILTER)ExceptionMiniDump);
}