利用SetUnhandledExceptionFilter获取程序异常时的DUMP文件
2013-07-03 10:51
417 查看
利用SetUnhandledExceptionFilter实现程序异常时获取DUMP文件来分析程序异常原因,现在很多的软件都是通过这样的方法来收集异常,只不过这里少了一步向服务器传输我们的程序产生的DUMP文件,这种DUMP文件产生后其实会通过网络向指定的服务器传输。这样方便开发都收集程序目前存在的BUG。
下面是一控制台程序,直接COPY代码,编译运行,我这里用了两种方法使程序异常崩溃,从而产生DUMP文件,对于产生的DUMP文件我们可以使用WinDBG来进行分析。
#include <Windows.h>
#include <dbghelp.h>
#pragma comment(lib, "dbghelp.lib")
LONG MyUnhandledExceptionFilter(_EXCEPTION_POINTERS *ExceptionInfo)
{
CHAR strDumpFile[MAX_PATH] = { 0 };
SYSTEMTIME tm;
HANDLE hFile = NULL;
GetSystemTime(&tm);
sprintf(strDumpFile, "%04d%02d%02d%02d%02d%02d.dmp", tm.wYear, tm.wMonth, tm.wDay, tm.wHour, tm.wMinute, tm.wSecond);
hFile = CreateFileA(strDumpFile,
GENERIC_WRITE,
FILE_SHARE_WRITE,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
if(hFile != INVALID_HANDLE_VALUE)
{
MINIDUMP_EXCEPTION_INFORMATION ExInfo;
ExInfo.ThreadId = GetCurrentThreadId();
ExInfo.ExceptionPointers = ExceptionInfo;
ExInfo.ClientPointers = NULL;
BOOL bOK = MiniDumpWriteDump(
GetCurrentProcess(),
GetCurrentProcessId(),
hFile,
MiniDumpNormal,
&ExInfo,
NULL,
NULL);
CloseHandle(hFile);
}
return EXCEPTION_CONTINUE_SEARCH;
}
int _tmain(int argc, _TCHAR* argv[])
{
SetUnhandledExceptionFilter((LPTOP_LEVEL_EXCEPTION_FILTER)MyUnhandledExceptionFilter);
// 第一种产生异常的方法
//char *str = NULL;
//memcpy(str, "Hello", 5);
// 第二种产生异常的方法
int i = 10;
while(1)
{
printf("%d\n", 10/i);
i--;
}
return 0;
}
下面是一控制台程序,直接COPY代码,编译运行,我这里用了两种方法使程序异常崩溃,从而产生DUMP文件,对于产生的DUMP文件我们可以使用WinDBG来进行分析。
#include <Windows.h>
#include <dbghelp.h>
#pragma comment(lib, "dbghelp.lib")
LONG MyUnhandledExceptionFilter(_EXCEPTION_POINTERS *ExceptionInfo)
{
CHAR strDumpFile[MAX_PATH] = { 0 };
SYSTEMTIME tm;
HANDLE hFile = NULL;
GetSystemTime(&tm);
sprintf(strDumpFile, "%04d%02d%02d%02d%02d%02d.dmp", tm.wYear, tm.wMonth, tm.wDay, tm.wHour, tm.wMinute, tm.wSecond);
hFile = CreateFileA(strDumpFile,
GENERIC_WRITE,
FILE_SHARE_WRITE,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
if(hFile != INVALID_HANDLE_VALUE)
{
MINIDUMP_EXCEPTION_INFORMATION ExInfo;
ExInfo.ThreadId = GetCurrentThreadId();
ExInfo.ExceptionPointers = ExceptionInfo;
ExInfo.ClientPointers = NULL;
BOOL bOK = MiniDumpWriteDump(
GetCurrentProcess(),
GetCurrentProcessId(),
hFile,
MiniDumpNormal,
&ExInfo,
NULL,
NULL);
CloseHandle(hFile);
}
return EXCEPTION_CONTINUE_SEARCH;
}
int _tmain(int argc, _TCHAR* argv[])
{
SetUnhandledExceptionFilter((LPTOP_LEVEL_EXCEPTION_FILTER)MyUnhandledExceptionFilter);
// 第一种产生异常的方法
//char *str = NULL;
//memcpy(str, "Hello", 5);
// 第二种产生异常的方法
int i = 10;
while(1)
{
printf("%d\n", 10/i);
i--;
}
return 0;
}
相关文章推荐
- 利用SetUnhandledExceptionFilter获取程序异常时的DUMP文件
- 利用SetUnhandledExceptionFilter机制实现程序异常后自动重启
- C#调用参数为函数指针的API函数 - 以SetUnhandledExceptionFilter为例编写一个全局异常处理程序
- Qt下使用DbgHelp和SetUnhandledExceptionFilter来获取Crash log/dump文件
- 使用SetUnhandledExceptionFilter和DebugMiniDumpFilter来使程序在崩溃时生成dump文件 .
- 使用SetUnhandledExceptionFilter和DebugMiniDumpFilter来使程序在崩溃时生成dump文件
- VS2005(VC8)编译的程序,许多错误都不能在SetUnhandledExceptionFilter捕获到
- SetUnhandledExceptionFilter让程序优雅的崩溃(转)
- 使用SetUnhandledExceptionFilter让程序优雅的崩溃
- 使用SetUnhandledExceptionFilter让程序优雅的崩溃
- 定位Release程序崩溃原因 SetUnhandledExceptionFilter + StackWalker
- 使用SetUnhandledExceptionFilter转储程序崩溃时内存DMP
- SetUnhandledExceptionFilter_异常捕获
- SetUnhandledExceptionFilter处理未捕获异常
- SetUnhandledExceptionFilter以及Mini-dump文件分析
- 使用SetUnhandledExceptionFilter让程序优雅的崩溃
- [转]window下使用SetUnhandledExceptionFilter捕获让程序的崩溃
- SetUnhandledExceptionFilter 相关收集异常信息
- SetUnhandledExceptionFilter生成Mini-dump文件分析(代码整理)
- [转]使用SetUnhandledExceptionFilter让程序优雅的崩溃