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

相关文章

解决java.lang.NullPointerException问题(空指针异常)

《解决java.lang.NullPointerException问题(空指针异常)》本文详细介绍了Java中的NullPointerException异常及其常见原因,包括对象引用为null、数组元... 目录Java.lang.NullPointerException(空指针异常)NullPointer

Python自动化处理手机验证码

《Python自动化处理手机验证码》手机验证码是一种常见的身份验证手段,广泛应用于用户注册、登录、交易确认等场景,下面我们来看看如何使用Python自动化处理手机验证码吧... 目录一、获取手机验证码1.1 通过短信接收验证码1.2 使用第三方短信接收服务1.3 使用ADB读取手机短信1.4 通过API获取

Rust格式化输出方式总结

《Rust格式化输出方式总结》Rust提供了强大的格式化输出功能,通过std::fmt模块和相关的宏来实现,主要的输出宏包括println!和format!,它们支持多种格式化占位符,如{}、{:?}... 目录Rust格式化输出方式基本的格式化输出格式化占位符Format 特性总结Rust格式化输出方式

Python自动化Office文档处理全攻略

《Python自动化Office文档处理全攻略》在日常办公中,处理Word、Excel和PDF等Office文档是再常见不过的任务,手动操作这些文档不仅耗时耗力,还容易出错,幸运的是,Python提供... 目录一、自动化处理Word文档1. 安装python-docx库2. 读取Word文档内容3. 修改

C++一个数组赋值给另一个数组方式

《C++一个数组赋值给另一个数组方式》文章介绍了三种在C++中将一个数组赋值给另一个数组的方法:使用循环逐个元素赋值、使用标准库函数std::copy或std::memcpy以及使用标准库容器,每种方... 目录C++一个数组赋值给另一个数组循环遍历赋值使用标准库中的函数 std::copy 或 std::

C++使用栈实现括号匹配的代码详解

《C++使用栈实现括号匹配的代码详解》在编程中,括号匹配是一个常见问题,尤其是在处理数学表达式、编译器解析等任务时,栈是一种非常适合处理此类问题的数据结构,能够精确地管理括号的匹配问题,本文将通过C+... 目录引言问题描述代码讲解代码解析栈的状态表示测试总结引言在编程中,括号匹配是一个常见问题,尤其是在

使用C++实现链表元素的反转

《使用C++实现链表元素的反转》反转链表是链表操作中一个经典的问题,也是面试中常见的考题,本文将从思路到实现一步步地讲解如何实现链表的反转,帮助初学者理解这一操作,我们将使用C++代码演示具体实现,同... 目录问题定义思路分析代码实现带头节点的链表代码讲解其他实现方式时间和空间复杂度分析总结问题定义给定

C++初始化数组的几种常见方法(简单易懂)

《C++初始化数组的几种常见方法(简单易懂)》本文介绍了C++中数组的初始化方法,包括一维数组和二维数组的初始化,以及用new动态初始化数组,在C++11及以上版本中,还提供了使用std::array... 目录1、初始化一维数组1.1、使用列表初始化(推荐方式)1.2、初始化部分列表1.3、使用std::

C++ Primer 多维数组的使用

《C++Primer多维数组的使用》本文主要介绍了多维数组在C++语言中的定义、初始化、下标引用以及使用范围for语句处理多维数组的方法,具有一定的参考价值,感兴趣的可以了解一下... 目录多维数组多维数组的初始化多维数组的下标引用使用范围for语句处理多维数组指针和多维数组多维数组严格来说,C++语言没

c++中std::placeholders的使用方法

《c++中std::placeholders的使用方法》std::placeholders是C++标准库中的一个工具,用于在函数对象绑定时创建占位符,本文就来详细的介绍一下,具有一定的参考价值,感兴... 目录1. 基本概念2. 使用场景3. 示例示例 1:部分参数绑定示例 2:参数重排序4. 注意事项5.