• 4217阅读
  • 1回复

zhengtianzuo系列-C++/Qt/Qml程序使用Camel_CrashReport捕获异常崩溃并定位问题代码行 [复制链接]

上一主题 下一主题
离线zhengtianzuo
 

只看楼主 倒序阅读 楼主  发表于: 2017-12-02
***
#### 组件介绍:
***
###### Camel_CrashReport:
程序异常崩溃时生成Dump文件和日志, **非开源** 属于CamelSoft系列基础开发组件.

Dump文件是进程的内存镜像。可以把程序的执行状态通过调试器保存到dump文件中。

支持平台:Windows
***

***
CamelCrashReportTest: 崩溃报告开发组件测试程序

Camel_CrashReport的调用非常简单, 提供了三个接口:

```
int Cls_funCrashReportInitialize();//初始化
```

```
int Cls_funCrashReportTerminate();//销毁
```

```
int Cls_funGetException(PEXCEPTION_POINTERS pExceptPtrs, WORD srtParam);//捕获崩溃
```

首先引用接口文件和定义函数指针, 这是加载dll动态库的过程

```
//Camel_CrashReport函数指针
#include <Windows.h>
#include "../../include/CamelCrashReportDll/Camel_CrashReport.h"
HINSTANCE hCrashReport;
lpCls_funCrashReportInitialize funInitialize;
lpCls_funCrashReportTerminate funTerminate;
lpCls_funGetException funGetException;
```

然后初始化组件:

```
    //初始化Camel_CrashReport
    hCrashReport = LoadLibrary(L"Camel_CrashReport.dll");
    funInitialize = NULL;
    funGetException = NULL;
    funTerminate = NULL;
    if (hCrashReport != NULL)
    {
        funInitialize = (lpCls_funCrashReportInitialize)GetProcAddress(
                            hCrashReport, "Cls_funCrashReportInitialize");
        funGetException = (lpCls_funGetException)GetProcAddress(
                              hCrashReport, "Cls_funGetException");
        funTerminate = (lpCls_funCrashReportTerminate)GetProcAddress(
                           hCrashReport, "Cls_funCrashReportTerminate");
        funInitialize();
    }

```

使用__try __except 包裹主函数

```
    __try
    {
#endif

    init(argc, argv);

#ifdef WIN32
    }
    __except (funGetException(
                  GetExceptionInformation(), clsCrashReport_intParam_Normal))
    {
        ;
    }
```

程序执行结束时卸载组件

```
    if (hCrashReport != NULL)
    {
        funTerminate();
        funInitialize = NULL;
        funGetException = NULL;
        funTerminate = NULL;
        FreeLibrary(hCrashReport);
        hCrashReport = NULL;
    }
```

就这三步就ok了
然后程序运行时, 执行异常崩溃的代码 就会触发写dump文件和日志的过程

```
    //运行异常代码
    char* chr = NULL;
    memset(chr, 0, 10);
```





生成的dump文件可以直接用VS打开并调试

异常崩溃时生成的dump文件, 在VisualStudio下调试直接定位到代码行, 当然也可以使用WinDbg调试

生成的txt日志文件包含很多有用的信息


注意: 如果是Qt/Qml工程要正确的定位到崩溃代码行, 工程需要使用VisualStudio编译
参看 Qt/Qml工程转VS工程

需要完整代码请访问 Camel_FileManagerCExamples
博客地址: https://blog.csdn.net/zhengtianzuo06
Github: https://github.com/zhengtianzuo
个人产品: https://github.com/zhengtianzuo/Silk
产品网站: http://www.camelstudio.cn
离线big_mouse

只看该作者 1楼 发表于: 2020-04-23
快速回复
限100 字节
 
上一个 下一个