windows程序崩溃时产生dmp

2017-12-30 C++

需求:windows程序崩溃时产生dmp

解决方案:

stdafx.h增加如下内容:

#include <Dbghelp.h>

//异常dmp捕获
void SetMyUnhandledExceptionFilter();

stdafx.cpp增加如下内容:

//生产DUMP文件
int GenerateMiniDump(HANDLE hFile, PEXCEPTION_POINTERS pExceptionPointers, PTCHAR pwAppName)
{
BOOL bOwnDumpFile = FALSE;
HANDLE hDumpFile = hFile;
MINIDUMP_EXCEPTION_INFORMATION ExpParam;

typedef BOOL(WINAPI * MiniDumpWriteDumpT)(
HANDLE,
DWORD,
HANDLE,
MINIDUMP_TYPE,
PMINIDUMP_EXCEPTION_INFORMATION,
PMINIDUMP_USER_STREAM_INFORMATION,
PMINIDUMP_CALLBACK_INFORMATION
);

MiniDumpWriteDumpT pfnMiniDumpWriteDump = NULL;
HMODULE hDbgHelp = LoadLibrary(_T("DbgHelp.dll"));
if(NULL == hDbgHelp)
{
return EXCEPTION_EXECUTE_HANDLER;
}

pfnMiniDumpWriteDump = (MiniDumpWriteDumpT)GetProcAddress(hDbgHelp, "MiniDumpWriteDump");
if(NULL == pfnMiniDumpWriteDump)
{
return EXCEPTION_EXECUTE_HANDLER;
}

if(hDumpFile == NULL || hDumpFile == INVALID_HANDLE_VALUE)
{
TCHAR szFileName[MAX_PATH] = { 0 };
TCHAR* szVersion = _T("v1.0");
int dwBufferSize = MAX_PATH;
SYSTEMTIME stLocalTime = { 0 };

GetLocalTime(&stLocalTime);

//wsprintf(szFileName, L"%s%s", szPath, szAppName);
//CreateDirectory(szFileName, NULL);
_stprintf_s(szFileName, MAX_PATH, _T("%s-%s-%04d%02d%02d-%02d%02d%02d-%ld-%ld.dmp"),
pAppName,
szVersion,
stLocalTime.wYear, stLocalTime.wMonth, stLocalTime.wDay,
stLocalTime.wHour, stLocalTime.wMinute, stLocalTime.wSecond,
GetCurrentProcessId(), GetCurrentThreadId());
hDumpFile = CreateFile(szFileName, GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_WRITE | FILE_SHARE_READ, 0, CREATE_ALWAYS, 0, 0);
}

if(hDumpFile != INVALID_HANDLE_VALUE)
{
ExpParam.ThreadId = GetCurrentThreadId();
ExpParam.ExceptionPointers = pExceptionPointers;
ExpParam.ClientPointers = FALSE;

pfnMiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(),
hDumpFile, MiniDumpWithDataSegs, (pExceptionPointers ? &ExpParam : NULL), NULL, NULL);

CloseHandle(hDumpFile);
}

if(hDbgHelp != NULL)
{
FreeLibrary(hDbgHelp);
}
return EXCEPTION_EXECUTE_HANDLER;
}

LONG WINAPI ExceptionFilter(LPEXCEPTION_POINTERS lpExceptionInfo)
{
if (IsDebuggerPresent())
{
return EXCEPTION_CONTINUE_SEARCH;
}

return GenerateMiniDump(NULL, lpExceptionInfo, _T("XXXXX"));
}



void DisableSetUnhandledExceptionFilter()
{
 void *addr = (void*)GetProcAddress(LoadLibrary(_T("kernel32.dll")) ,"SetUnhandledExceptionFilter");
if(addr)
{
unsigned char code[16] = { 0 };
int size = 0;
code[size++] = 0x33;
code[size++] = 0xC0;
code[size++] = 0xC2;
code[size++] = 0x04;
code[size++] = 0x00;

DWORD dwOldFlag,dwTempFlag;
VirtualProtect(addr, size, PAGE_READWRITE, &dwOldFlag);
WriteProcessMemory(GetCurrentProcess(), addr, code, size, NULL);
VirtualProtect(addr, size, dwOldFlag ,&dwTempFlag);
}
}


void SetMyUnhandledExceptionFilter()
{
SetUnhandledExceptionFilter(NULL);
SetUnhandledExceptionFilter(&ExceptionFilter);
DisableSetUnhandledExceptionFilter();
}

程序入口添加如下内容:

SetMyUnhandledExceptionFilter();

程序所在目录添加:DbgHelp.dll文件



标签: MFC dmp windows

评论(0) 浏览(15845)

Powered by EMLOG Copyright @ 狼酒 版权所有. 闽ICP备14012694号-2