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

相关文章

C#如何调用C++库

《C#如何调用C++库》:本文主要介绍C#如何调用C++库方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录方法一:使用P/Invoke1. 导出C++函数2. 定义P/Invoke签名3. 调用C++函数方法二:使用C++/CLI作为桥接1. 创建C++/CL

Java 中的 @SneakyThrows 注解使用方法(简化异常处理的利与弊)

《Java中的@SneakyThrows注解使用方法(简化异常处理的利与弊)》为了简化异常处理,Lombok提供了一个强大的注解@SneakyThrows,本文将详细介绍@SneakyThro... 目录1. @SneakyThrows 简介 1.1 什么是 Lombok?2. @SneakyThrows

在 Spring Boot 中实现异常处理最佳实践

《在SpringBoot中实现异常处理最佳实践》本文介绍如何在SpringBoot中实现异常处理,涵盖核心概念、实现方法、与先前查询的集成、性能分析、常见问题和最佳实践,感兴趣的朋友一起看看吧... 目录一、Spring Boot 异常处理的背景与核心概念1.1 为什么需要异常处理?1.2 Spring B

python处理带有时区的日期和时间数据

《python处理带有时区的日期和时间数据》这篇文章主要为大家详细介绍了如何在Python中使用pytz库处理时区信息,包括获取当前UTC时间,转换为特定时区等,有需要的小伙伴可以参考一下... 目录时区基本信息python datetime使用timezonepandas处理时区数据知识延展时区基本信息

利用Python调试串口的示例代码

《利用Python调试串口的示例代码》在嵌入式开发、物联网设备调试过程中,串口通信是最基础的调试手段本文将带你用Python+ttkbootstrap打造一款高颜值、多功能的串口调试助手,需要的可以了... 目录概述:为什么需要专业的串口调试工具项目架构设计1.1 技术栈选型1.2 关键类说明1.3 线程模

关于MongoDB图片URL存储异常问题以及解决

《关于MongoDB图片URL存储异常问题以及解决》:本文主要介绍关于MongoDB图片URL存储异常问题以及解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录MongoDB图片URL存储异常问题项目场景问题描述原因分析解决方案预防措施js总结MongoDB图

Python Transformers库(NLP处理库)案例代码讲解

《PythonTransformers库(NLP处理库)案例代码讲解》本文介绍transformers库的全面讲解,包含基础知识、高级用法、案例代码及学习路径,内容经过组织,适合不同阶段的学习者,对... 目录一、基础知识1. Transformers 库简介2. 安装与环境配置3. 快速上手示例二、核心模

一文详解Java异常处理你都了解哪些知识

《一文详解Java异常处理你都了解哪些知识》:本文主要介绍Java异常处理的相关资料,包括异常的分类、捕获和处理异常的语法、常见的异常类型以及自定义异常的实现,文中通过代码介绍的非常详细,需要的朋... 目录前言一、什么是异常二、异常的分类2.1 受检异常2.2 非受检异常三、异常处理的语法3.1 try-

Python使用getopt处理命令行参数示例解析(最佳实践)

《Python使用getopt处理命令行参数示例解析(最佳实践)》getopt模块是Python标准库中一个简单但强大的命令行参数处理工具,它特别适合那些需要快速实现基本命令行参数解析的场景,或者需要... 目录为什么需要处理命令行参数?getopt模块基础实际应用示例与其他参数处理方式的比较常见问http

Java Response返回值的最佳处理方案

《JavaResponse返回值的最佳处理方案》在开发Web应用程序时,我们经常需要通过HTTP请求从服务器获取响应数据,这些数据可以是JSON、XML、甚至是文件,本篇文章将详细解析Java中处理... 目录摘要概述核心问题:关键技术点:源码解析示例 1:使用HttpURLConnection获取Resp