应用程序创建自己的奔溃转储(crash dump)文件

1、注册自定义的UnhandledExceptionFilter,C/C++ Runtime Library下需要注意自定义handler被移除(hook kernel32.dll的SetUnhandledExceptionFilter使它返回一个空指针即可)。

PTOP_LEVEL_EXCEPTION_FILTER    v_prevUnhandledExceptionFilter;

LONG WINAPI UnhandledExceptionHandler(EXCEPTION_POINTERS* ExceptionInfo);

v_prevUnhandledExceptionFilter = ::SetUnhandledExceptionFilter(UnhandledExceptionHandler);

LONG WINAPI UnhandledExceptionHandler(EXCEPTION_POINTERS* ExceptionInfo)
{
    GenerateCrashDump(ExceptionInfo);

    if (v_prevUnhandledExceptionFilter != nullptr)
        return v_prevUnhandledExceptionFilter(ExceptionInfo);

    return EXCEPTION_CONTINUE_SEARCH;
}

2、调用DbgHelp.dll的MiniDumpWriteDump函数。

void GenerateCrashDump(EXCEPTION_POINTERS* ExceptionInfo)
{
    SYSTEMTIME st = { 0 };
    GetSystemTime(&st);

    auto path = String::Format(L"%s%04u-%02u-%02u_%02u-%02u-%02u.dmp", v_logsDir.c_str(), st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);
    auto dumpType = (MINIDUMP_TYPE) (MiniDumpNormal | MiniDumpWithHandleData | MiniDumpWithUnloadedModules);
    auto hFile = ::CreateFile(path.c_str(), GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

    if (hFile == INVALID_HANDLE_VALUE)
        return;

    auto hProcess = ::GetCurrentProcess();
    auto processId = ::GetCurrentProcessId();

    MINIDUMP_EXCEPTION_INFORMATION mei = { 0 };
    mei.ThreadId = GetCurrentThreadId();
    mei.ClientPointers = FALSE;
    mei.ExceptionPointers = ExceptionInfo;

    ::MiniDumpWriteDump(hProcess, processId, hFile, dumpType, &mei, nullptr, nullptr);
    ::CloseHandle(hFile);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值