使用SetUnhandledExceptionFilter轉儲程序崩潰時內存DMP注意事項

使用代碼手工生成dmp文件

  • SetUnhandledExceptionFilter

爲每個線程設置SetUnhandledExceptionFilter(MyCallBack),(必須在每個線程中啓動時調用一次,否則造成無法進入回調函數中)這樣該線程中發現未處理的 SEH 異常時就會進入到MyCallBack 回調中.

無聊的是雖然MyCallBack 的參數是 SEH 異常的結構體指針,但 C++ 異常也會進入到MyCallBack 中.所以只要SetUnhandledExceptionFilter 就能抓到 C++ 的異常了.

按C++標準,未處理的C++異常應當是觸發unexpected.而MS 放出話說它的編譯器只觸發terminate.而在MFC 中居然terminate 都不觸發了,直接變成了 SEH 而進入了MyCallBack.

部分源碼:

#include "stdafx.h"
#include "DumpHandle.h"  
#include <stdlib.h>
#include <stdio.h>
#include <dbghelp.h>
#pragma comment(lib, "Dbghelp.lib")




LONG WINAPI DumpHandleFilterA(struct _EXCEPTION_POINTERS *lpExceptionInfo)
{
LONG ret = EXCEPTION_EXECUTE_HANDLER;
char szFileName[128] = {0};
SYSTEMTIME st = {0};
  
::GetLocalTime(&st); 
 
wsprintfA(szFileName, ("CrashAt[%04d-%02d-%02d-%02d-%02d-%02d-%02d-%02d].dmp"), 
st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds, rand()%100);
HANDLE hFile = ::CreateFileA(szFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
if (hFile != INVALID_HANDLE_VALUE)
{
MINIDUMP_EXCEPTION_INFORMATION ExInfo;         
ExInfo.ThreadId = ::GetCurrentThreadId();       
ExInfo.ExceptionPointers = lpExceptionInfo;        
ExInfo.ClientPointers = false;       
// write the dump        
BOOL bOK = MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hFile, MiniDumpNormal, &ExInfo, NULL, NULL );         
if (bOK)
{
DumpDbgPrintA(("Create Dump File Success!/n")); 
DumpDbgMsgboxA("Create Dump File Success!/n");
}
else
{
DumpDbgPrintA(("MiniDumpWriteDump Failed: %d/n"), GetLastError() ); 
DumpDbgMsgboxA(("MiniDumpWriteDump Failed: %d/n"), GetLastError() );
}
::CloseHandle(hFile);
}
else
{
DumpDbgPrintA(("Create File %s Failed %d/n"), szFileName, GetLastError());
DumpDbgMsgboxA(("Create File %s Failed %d/n"), szFileName, GetLastError()); 
}
 
   
return ret;
}




void DumpDbgPrintA (char *fmt, ... )
{
va_list argptr; /* Argument list pointer */
char str[1024*3] = {0}; /* Buffer to build sting into */


va_start (argptr, fmt); /* Initialize va_ functions */
wvsprintfA (str, fmt, argptr); /* prints string to buffer */
OutputDebugStringA(str);
va_end (argptr); /* Close va_ functions */
}


void DumpDbgMsgboxA (char *fmt, ... )
{
va_list argptr; /* Argument list pointer */
char str[1024*3] = {0}; /* Buffer to build sting into */


va_start (argptr, fmt); /* Initialize va_ functions */
wvsprintfA (str, fmt, argptr); /* prints string to buffer */
MessageBoxA (NULL, str, "Debug Message", 
MB_ICONINFORMATION | MB_OK);
va_end (argptr); /* Close va_ functions */
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章