VC++ 各种未处理异常的处理,并输出DUMP崩溃转储调试文件

2024-05-11 13:44

本文主要是介绍VC++ 各种未处理异常的处理,并输出DUMP崩溃转储调试文件,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

本文包含:

信号处理器、STL标准库异常处理、STL未处理器异常处理、SEH结构化未处理异常处理、C++ 未定义虚函数异常处理、C/C++ 内存分配异常处理等。

设置异常处理器:

            // Windows platforms need to mount unhandled exception handlers so that they can print dump debug files for app crashes.
#if defined(_DEBUG)::_CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF | _CRTDBG_ALLOC_MEM_DF);
#endif::_set_abort_behavior(_CALL_REPORTFAULT, _CALL_REPORTFAULT);::_set_purecall_handler(Crt_HandlePureVirtualCall);::_set_new_handler(Crt_NewHandler); /* std::set_new_handler(...) */#if _MSC_VER >= 1400 ::_set_invalid_parameter_handler(Crt_InvalidParameterHandler);
#endif::signal(SIGABRT, Crt_SigabrtHandler);::signal(SIGINT, Crt_SigabrtHandler);::signal(SIGTERM, Crt_SigabrtHandler);::signal(SIGILL, Crt_SigabrtHandler);::set_terminate(Crt_TerminateHandler);::set_unexpected(Crt_UnexpectedHandler);::SetUnhandledExceptionFilter(Seh_UnhandledExceptionFilter);

异常处理器实现:

        static ppp::string Seh_NewDumpFileName() noexcept{ppp::string path = ppp::GetExecutionFileName();std::size_t index = path.rfind(".");if (index != ppp::string::npos){path = path.substr(0, index);}struct tm tm_;time_t datetime = time(NULL);localtime_s(&tm_, &datetime);char sz[1000];sprintf_s(sz, sizeof(sz), "%04d%02d%02d-%02d%02d%02d", 1900 + tm_.tm_year, 1 + tm_.tm_mon, tm_.tm_mday, tm_.tm_hour, tm_.tm_min, tm_.tm_sec);path = path + "-" + sz + ".dmp";path = "./" + path;path = ppp::io::File::RewritePath(path.data());path = ppp::io::File::GetFullPath(path.data());return path;}static LONG WINAPI Seh_UnhandledExceptionFilter(EXCEPTION_POINTERS* exceptionInfo) noexcept{// Give user code a chance to approve or prevent writing a minidump.  If the// filter returns false, don't handle the exception at all.  If this method// was called as a result of an exception, returning false will cause// HandleException to call any previous handler or return// EXCEPTION_CONTINUE_SEARCH on the exception thread, allowing it to appear// as though this handler were not present at all.HANDLE hFile = CreateFileA(Seh_NewDumpFileName().data(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);if (hFile != INVALID_HANDLE_VALUE){MINIDUMP_EXCEPTION_INFORMATION exceptionParam;exceptionParam.ThreadId = GetCurrentThreadId();exceptionParam.ExceptionPointers = exceptionInfo;exceptionParam.ClientPointers = TRUE;MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hFile, MiniDumpWithFullMemory, &exceptionParam, NULL, NULL);CloseHandle(hFile);}// The handler either took care of the invalid parameter problem itself,// or passed it on to another handler.  "Swallow" it by exiting, paralleling// the behavior of "swallowing" exceptions.exit(-1); /* abort(); */return EXCEPTION_EXECUTE_HANDLER;}#if _MSC_VER >= 1400 // https://chromium.googlesource.com/breakpad/breakpad/src/+/master/client/windows/handler/exception_handler.ccstatic void __CRTDECL Crt_InvalidParameterHandler(const wchar_t* expression, const wchar_t* function, const wchar_t* file, unsigned int line, uintptr_t pReserved) noexcept{std::wcerr << L"Invalid parameter detected:" << std::endl;std::wcerr << L"Expression: " << expression << std::endl;std::wcerr << L"Function: " << function << std::endl;std::wcerr << L"File: " << file << std::endl;std::wcerr << L"Line: " << line << std::endl;_CrtDumpMemoryLeaks();_CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_DEBUG);_CrtMemDumpAllObjectsSince(NULL);_CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);// Make up an exception record for the current thread and CPU context// to make it possible for the crash processor to classify these// as do regular crashes, and to make it humane for developers to// analyze them.EXCEPTION_RECORD exception_record = {};CONTEXT exception_context = {};EXCEPTION_POINTERS exception_ptrs = { &exception_record, &exception_context };::RtlCaptureContext(&exception_context);exception_record.ExceptionCode = STATUS_INVALID_PARAMETER;// We store pointers to the the expression and function strings,// and the line as exception parameters to make them easy to// access by the developer on the far side.exception_record.NumberParameters = 4;exception_record.ExceptionInformation[0] = reinterpret_cast<ULONG_PTR>(expression);exception_record.ExceptionInformation[1] = reinterpret_cast<ULONG_PTR>(file);exception_record.ExceptionInformation[2] = line;exception_record.ExceptionInformation[3] = reinterpret_cast<ULONG_PTR>(function);// Deliver exceptions to unhandled exception handler.Seh_UnhandledExceptionFilter(&exception_ptrs);}
#endifstatic int Seh_NoncontinuableException() noexcept{// Make up an exception record for the current thread and CPU context// to make it possible for the crash processor to classify these// as do regular crashes, and to make it humane for developers to// analyze them.EXCEPTION_RECORD exception_record = {};CONTEXT exception_context = {};EXCEPTION_POINTERS exception_ptrs = { &exception_record, &exception_context };::RtlCaptureContext(&exception_context);exception_record.ExceptionCode = STATUS_NONCONTINUABLE_EXCEPTION;// We store pointers to the the expression and function strings,// and the line as exception parameters to make them easy to// access by the developer on the far side.exception_record.NumberParameters = 3;exception_record.ExceptionInformation[0] = NULL;exception_record.ExceptionInformation[1] = NULL;exception_record.ExceptionInformation[2] = 0;// Deliver exceptions to unhandled exception handler.return Seh_UnhandledExceptionFilter(&exception_ptrs);}static int __CRTDECL Crt_NewHandler(size_t) noexcept{return Seh_NoncontinuableException();}static void __CRTDECL Crt_HandlePureVirtualCall() noexcept{Seh_NoncontinuableException();}static void __CRTDECL Crt_TerminateHandler() noexcept{Seh_NoncontinuableException();}static void __CRTDECL Crt_UnexpectedHandler() noexcept{Seh_NoncontinuableException();}static void __CRTDECL Crt_SigabrtHandler(int) noexcept{Seh_NoncontinuableException();}

这篇关于VC++ 各种未处理异常的处理,并输出DUMP崩溃转储调试文件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/979699

相关文章

Spring Boot @RestControllerAdvice全局异常处理最佳实践

《SpringBoot@RestControllerAdvice全局异常处理最佳实践》本文详解SpringBoot中通过@RestControllerAdvice实现全局异常处理,强调代码复用、统... 目录前言一、为什么要使用全局异常处理?二、核心注解解析1. @RestControllerAdvice2

Java进程异常故障定位及排查过程

《Java进程异常故障定位及排查过程》:本文主要介绍Java进程异常故障定位及排查过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、故障发现与初步判断1. 监控系统告警2. 日志初步分析二、核心排查工具与步骤1. 进程状态检查2. CPU 飙升问题3. 内存

从入门到精通C++11 <chrono> 库特性

《从入门到精通C++11<chrono>库特性》chrono库是C++11中一个非常强大和实用的库,它为时间处理提供了丰富的功能和类型安全的接口,通过本文的介绍,我们了解了chrono库的基本概念... 目录一、引言1.1 为什么需要<chrono>库1.2<chrono>库的基本概念二、时间段(Durat

C++20管道运算符的实现示例

《C++20管道运算符的实现示例》本文简要介绍C++20管道运算符的使用与实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录标准库的管道运算符使用自己实现类似的管道运算符我们不打算介绍太多,因为它实际属于c++20最为重要的

Visual Studio 2022 编译C++20代码的图文步骤

《VisualStudio2022编译C++20代码的图文步骤》在VisualStudio中启用C++20import功能,需设置语言标准为ISOC++20,开启扫描源查找模块依赖及实验性标... 默认创建Visual Studio桌面控制台项目代码包含C++20的import方法。右键项目的属性:

c++中的set容器介绍及操作大全

《c++中的set容器介绍及操作大全》:本文主要介绍c++中的set容器介绍及操作大全,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录​​一、核心特性​​️ ​​二、基本操作​​​​1. 初始化与赋值​​​​2. 增删查操作​​​​3. 遍历方

解析C++11 static_assert及与Boost库的关联从入门到精通

《解析C++11static_assert及与Boost库的关联从入门到精通》static_assert是C++中强大的编译时验证工具,它能够在编译阶段拦截不符合预期的类型或值,增强代码的健壮性,通... 目录一、背景知识:传统断言方法的局限性1.1 assert宏1.2 #error指令1.3 第三方解决

C++11委托构造函数和继承构造函数的实现

《C++11委托构造函数和继承构造函数的实现》C++引入了委托构造函数和继承构造函数这两个重要的特性,本文主要介绍了C++11委托构造函数和继承构造函数的实现,具有一定的参考价值,感兴趣的可以了解一下... 目录引言一、委托构造函数1.1 委托构造函数的定义与作用1.2 委托构造函数的语法1.3 委托构造函

C++11作用域枚举(Scoped Enums)的实现示例

《C++11作用域枚举(ScopedEnums)的实现示例》枚举类型是一种非常实用的工具,C++11标准引入了作用域枚举,也称为强类型枚举,本文主要介绍了C++11作用域枚举(ScopedEnums... 目录一、引言二、传统枚举类型的局限性2.1 命名空间污染2.2 整型提升问题2.3 类型转换问题三、C

C++链表的虚拟头节点实现细节及注意事项

《C++链表的虚拟头节点实现细节及注意事项》虚拟头节点是链表操作中极为实用的设计技巧,它通过在链表真实头部前添加一个特殊节点,有效简化边界条件处理,:本文主要介绍C++链表的虚拟头节点实现细节及注... 目录C++链表虚拟头节点(Dummy Head)一、虚拟头节点的本质与核心作用1. 定义2. 核心价值二