场景
1.在Windows上用C/C++开发软件, 经常会出现软件级别的崩溃情况, 如果用户看到这种崩溃报告, 那么一般会认为软件质量不高, 从而不想用. Windows上就会有崩溃报告这种噢给你工具来生成dump文件来分析, 可以让用户发送崩溃报告过来解决问题进而提高软件质量.
2.一般情况下有些崩溃无法捕捉, 这是因为CRT调用SetUnhandledExceptionFilter(0)来移除自定义处理器.
例子
1.以下例子使用了部分[Windows][初级][Release程序的崩溃报告minidump解决方案] 代码. 大多数情况下生成的崩溃报告时需要打包的, 这样方便传输. 这里为了说明主要问题忽略打包部分. CAPIHook部分参考了 SetUnhandledExceptionFilter and the C/C++ Runtime Library.
2.这种方式能捕捉大部分异常和崩溃, 但是有个问题, 就是通过CAPIHook转发的dmp文件不能获取到准确的出错行,(有人知道的话留个言)
3.项目地址在项目源码. 注意, 这个test-crash.cpp项目代码不是最新, 用以下的.[20170103]
// test-crash.cpp : 定义控制台应用程序的入口点。
//
#include <SDKDDKVer.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <tchar.h>
#include <Windows.h>
#include <DbgHelp.h>
#include <string>
#include <vector>
#include <iostream>
#include "APIHook.h"
static std::wstring gDumpPath;
static std::wstring gDumpZipPath;
static BOOL IsDataSectionNeeded(const WCHAR* pModuleName)
{
if(pModuleName == NULL)
{
return FALSE;
}
WCHAR szFileName[_MAX_FNAME] = L"";
_wsplitpath(pModuleName, NULL, NULL, szFileName, NULL);
if(wcscmp(szFileName, L"ntdll") == 0)
return TRUE;
return FALSE;
}
static BOOL CALLBACK MiniDumpCallback(PVOID pParam,
const PMINIDUMP_CALLBACK_INPUT pInput,
PMINIDUMP_CALLBACK_OUTPUT pOutput)
{
if(pInput == 0 || pOutput == 0)
return FALSE;
switch(pInput->CallbackType)
{
case ModuleCallback:
if(pOutput->ModuleWriteFlags & ModuleWriteDataSeg)
if(!IsDataSectionNeeded(pInput->Module.FullPath))
pOutput->ModuleWriteFlags &= (~ModuleWriteDataSeg);
case IncludeModuleCallback:
case IncludeThreadCallback:
case ThreadCallback:
case ThreadExCallback:
return TRUE;
default:;
}
return FALSE;
}
static LONG WINAPI TopLevelUnhandledExceptionFilter(PEXCEPTION_POINTERS pExInfo)
{
HANDLE hFile = ::CreateFile( gDumpPath.c_str(), GENERIC_WRITE, 0, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if( hFile != INVALID_HANDLE_VALUE)
{
MINIDUMP_EXCEPTION_INFORMATION einfo;
einfo.ThreadId = ::GetCurrentThreadId();
einfo.ExceptionPointers = pExInfo;
einfo.ClientPointers = FALSE;
MINIDUMP_CALLBACK_INFORMATION mci;
mci.CallbackRoutine = (MINIDUMP_CALLBACK_ROUTINE)MiniDumpCallback;
mci.CallbackParam = NULL;
::MiniDumpWriteDump(::GetCurrentProcess(), ::GetCurrentProcessId(), hFile,MiniDumpNormal,&einfo, NULL, &mci);
::CloseHandle(hFile);
}
std::cout << "TopLevelUnhandledExceptionFilter" << std::endl;
return EXCEPTION_EXECUTE_HANDLER;
}
static LPTOP_LEVEL_EXCEPTION_FILTER WINAPI MyDummySetUnhandledExceptionFilter(
LPTOP_LEVEL_EXCEPTION_FILTER lpTopLevelExceptionFilter)
{
return NULL;
}
LONG WINAPI VectoredExceptionHandler(PEXCEPTION_POINTERS pExceptionInfo)
{
std::cout << "VectoredExceptionHandler" << std::endl;
HANDLE hFile = ::CreateFile( gDumpPath.c_str(), GENERIC_WRITE, 0, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if( hFile != INVALID_HANDLE_VALUE)
{
MINIDUMP_EXCEPTION_INFORMATION einfo;
einfo.ThreadId = ::GetCurrentThreadId();
einfo.ExceptionPointers = pExceptionInfo;
einfo.ClientPointers = FALSE;
MINIDUMP_CALLBACK_INFORMATION mci;
mci.CallbackRoutine = (MINIDUMP_CALLBACK_ROUTINE)MiniDumpCallback;
mci.CallbackParam = NULL;
::MiniDumpWriteDump(::GetCurrentProcess(), ::GetCurrentProcessId(), hFile,MiniDumpNormal,&einfo, NULL, &mci);
::CloseHandle(hFile);
}
exit(-1);
return EXCEPTION_CONTINUE_EXECUTION ;
}
LONG WINAPI RedirectedSetUnhandledExceptionFilter(EXCEPTION_POINTERS * pExceptionInfo)
{
// When the CRT calls SetUnhandledExceptionFilter with NULL parameter
// our handler will not get removed.
std::cout << "RedirectedSetUnhandledExceptionFilter" << std::endl;
return EXCEPTION_CONTINUE_SEARCH ;
}
const wchar_t* GetAppModule()
{
static wchar_t szbuf[MAX_PATH];
::GetModuleFileNameW(NULL,szbuf,MAX_PATH);
return szbuf;
}
void TestNULLPointerExceptionCrash()
{
std::cout << "TestNULLPointerExceptionCrash" << std::endl;
int* i = NULL;
*i = 9;
}
void TestRaiseExceptionCrash()
{
std::cout << "TestRaiseExceptionCrash" << std::endl;
RaiseException(0xc0000374, 0, 0, NULL);
}
void TestCorruptCrash()
{
std::cout << "TestCorruptCrash" << std::endl;
std::string* str = new std::string("hello");
delete str;
str->append("worldasdfasdfasdfasdfasdfasdfasdfasdfasdfas");
delete str;
//std::cout << str << std::endl;
}
void TestfreadCrash()
{
std::cout << "TestfreadCrash" << std::endl;
fread("hello",5,1,NULL);
}
void TestAbortCrash()
{
std::cout << "TestAbortCrash" << std::endl;
abort();
}
void TestOutOfBoundsVectorCrash()
{
std::cout << "std::vector out of bounds crash!" << std::endl;
std::vector<int> v;
v[0] = 5;
}
int _tmain(int argc, _TCHAR* argv[])
{
gDumpPath = GetAppModule();
gDumpPath.append(L"-minidump.dmp");
AddVectoredExceptionHandler(1, VectoredExceptionHandler);
SetUnhandledExceptionFilter(TopLevelUnhandledExceptionFilter);
CAPIHook apiHook("kernel32.dll",
"SetUnhandledExceptionFilter",
(PROC)RedirectedSetUnhandledExceptionFilter);
// 第一种 TopLevelUnhandledExceptionFilter
//TestNULLPointerExceptionCrash();
// 第二种 TopLevelUnhandledExceptionFilter
//TestRaiseExceptionCrash();
// 第三种 CAPIHook
// 问题事件名称: BEX
//TestfreadCrash();
// 第四种 AddVectoredExceptionHandler
TestCorruptCrash();
// 第五种 CAPIHook
// 问题事件名称: APPCRASH
//TestAbortCrash();
// 第六种 TopLevelUnhandledExceptionFilter
//TestOutOfBoundsVectorCrash();
std::cout << "End..." << std::endl;
return 0;
}
备注:
EXCEPTION_CONTINUE_EXECUTION (–1) Exception is dismissed. Continue execution at the point where the exception occurred.
EXCEPTION_CONTINUE_SEARCH (0) Exception is not recognized. Continue to search up the stack for a handler, first for containing try-except statements, then for handlers with the next highest precedence.
EXCEPTION_EXECUTE_HANDLER (1) Exception is recognized. Transfer control to the exception handler by executing the __except compound statement, then continue execution after the __except block.