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

相关文章

Windows下C++使用SQLitede的操作过程

《Windows下C++使用SQLitede的操作过程》本文介绍了Windows下C++使用SQLite的安装配置、CppSQLite库封装优势、核心功能(如数据库连接、事务管理)、跨平台支持及性能优... 目录Windows下C++使用SQLite1、安装2、代码示例CppSQLite:C++轻松操作SQ

电脑提示xlstat4.dll丢失怎么修复? xlstat4.dll文件丢失处理办法

《电脑提示xlstat4.dll丢失怎么修复?xlstat4.dll文件丢失处理办法》长时间使用电脑,大家多少都会遇到类似dll文件丢失的情况,不过,解决这一问题其实并不复杂,下面我们就来看看xls... 在Windows操作系统中,xlstat4.dll是一个重要的动态链接库文件,通常用于支持各种应用程序

C++中RAII资源获取即初始化

《C++中RAII资源获取即初始化》RAII通过构造/析构自动管理资源生命周期,确保安全释放,本文就来介绍一下C++中的RAII技术及其应用,具有一定的参考价值,感兴趣的可以了解一下... 目录一、核心原理与机制二、标准库中的RAII实现三、自定义RAII类设计原则四、常见应用场景1. 内存管理2. 文件操

C++中零拷贝的多种实现方式

《C++中零拷贝的多种实现方式》本文主要介绍了C++中零拷贝的实现示例,旨在在减少数据在内存中的不必要复制,从而提高程序性能、降低内存使用并减少CPU消耗,零拷贝技术通过多种方式实现,下面就来了解一下... 目录一、C++中零拷贝技术的核心概念二、std::string_view 简介三、std::stri

SQL Server数据库死锁处理超详细攻略

《SQLServer数据库死锁处理超详细攻略》SQLServer作为主流数据库管理系统,在高并发场景下可能面临死锁问题,影响系统性能和稳定性,这篇文章主要给大家介绍了关于SQLServer数据库死... 目录一、引言二、查询 Sqlserver 中造成死锁的 SPID三、用内置函数查询执行信息1. sp_w

C++高效内存池实现减少动态分配开销的解决方案

《C++高效内存池实现减少动态分配开销的解决方案》C++动态内存分配存在系统调用开销、碎片化和锁竞争等性能问题,内存池通过预分配、分块管理和缓存复用解决这些问题,下面就来了解一下... 目录一、C++内存分配的性能挑战二、内存池技术的核心原理三、主流内存池实现:TCMalloc与Jemalloc1. TCM

Java对异常的认识与异常的处理小结

《Java对异常的认识与异常的处理小结》Java程序在运行时可能出现的错误或非正常情况称为异常,下面给大家介绍Java对异常的认识与异常的处理,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参... 目录一、认识异常与异常类型。二、异常的处理三、总结 一、认识异常与异常类型。(1)简单定义-什么是

C++ 函数 strftime 和时间格式示例详解

《C++函数strftime和时间格式示例详解》strftime是C/C++标准库中用于格式化日期和时间的函数,定义在ctime头文件中,它将tm结构体中的时间信息转换为指定格式的字符串,是处理... 目录C++ 函数 strftipythonme 详解一、函数原型二、功能描述三、格式字符串说明四、返回值五

Python主动抛出异常的各种用法和场景分析

《Python主动抛出异常的各种用法和场景分析》在Python中,我们不仅可以捕获和处理异常,还可以主动抛出异常,也就是以类的方式自定义错误的类型和提示信息,这在编程中非常有用,下面我将详细解释主动抛... 目录一、为什么要主动抛出异常?二、基本语法:raise关键字基本示例三、raise的多种用法1. 抛

C++作用域和标识符查找规则详解

《C++作用域和标识符查找规则详解》在C++中,作用域(Scope)和标识符查找(IdentifierLookup)是理解代码行为的重要概念,本文将详细介绍这些规则,并通过实例来说明它们的工作原理,需... 目录作用域标识符查找规则1. 普通查找(Ordinary Lookup)2. 限定查找(Qualif