C++/Qt/Qml程序使用Camel_CrashReport捕獲異常崩潰並定位問題代碼行


組件介紹:


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

聯繫方式:


作者 鄭天佐
QQ 278969898
主頁 http://www.camelstudio.cn/
郵箱 [email protected]
博客 http://blog.csdn.net/zhengtianzuo06/
github https://github.com/zhengtianzuo
QQ羣 199672080

捐贈

覺得分享的內容還不錯, 就請作者喝杯咖啡吧~~

發佈了99 篇原創文章 · 獲贊 74 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章