• Qt程序Core dump实现


    了解

    • core dump :内核转储,dump,进程当前云心的信息的导出。
    • 进程对应着操作系统,程序放内存中,分配堆栈空间、进程空间。dump对这些信息导出,至少包括堆栈信息、寄存器信息。
    • dump文件用winDbg文件打开,vs其实也可以。
    • dump文件:后缀.dmp,程序崩溃时,内存转储文件
    • pdb文件:后缀.pdb,程序的符号文件
    • 开源的有Breakpad,C++崩溃检测库,捕获发布给用户的应用程序的崩溃,并记录软件崩溃的调试信息到minidump中,生成.dmp文件,崩溃记录文件格式是二进制,体积小。
    • qBreakPad(用qt封装过的)
    • CreateFile()函数
    CreateFile(文件名,访问模式,共享模式,指向安全属性行号(NULL,如何创建,文件属性,用于复制文件句柄)
    
    • 1

    代码

    MiniDumper.h

    #ifndef MINIDUMP_H
    #define MINIDUMP_H
    #include 
    #include 
    // based on dbghelp.h
    typedef BOOL (WINAPI *MINIDUMPWRITEDUMP)(HANDLE hProcess, DWORD dwPid, HANDLE hFile, MINIDUMP_TYPE DumpType,
                                             CONST PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam,
                                             CONST PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam,
                                             CONST PMINIDUMP_CALLBACK_INFORMATION CallbackParam
                                             );
    #define MAX_WARNING_MESSAGE_PATH 1024
    class MiniDumper
    {
    private:
        static LPCWSTR m_szAppName;
        static LPWSTR m_szAppVersion;
        static LPWSTR m_szAppBuildNumber;
     
        static WCHAR m_szMessageText[MAX_WARNING_MESSAGE_PATH];
     
        static LPWSTR m_szDumpFilePath;
     
        static LONG WINAPI TopLevelFilter( struct _EXCEPTION_POINTERS *pExceptionInfo );
     
    public:
        MiniDumper( );
        ~MiniDumper();
        static void SetVersion(LPCWSTR szVersion);
        static void SetBuildNumber(LPCWSTR szBuildNumber);
        static void SetDumpFilePath(LPCWSTR szFilePath);
        static int SetWarningMessage(LPCWSTR szMessageText)
        {
            if(szMessageText)
            {
                int iLen = wcslen(szMessageText);
                if(iLen < MAX_WARNING_MESSAGE_PATH - MAX_PATH)
                {
                    wcscpy(m_szMessageText,szMessageText);
                    return 0;
                }
            }
            return 1;
        }
    };
     
     
    #endif
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47

    MiniDumper.cpp

    #include 
    #include "MiniDumper.h"
    #include 
     
    LPCWSTR MiniDumper::m_szAppName;
     
    LPWSTR MiniDumper::m_szAppVersion;
     
    LPWSTR MiniDumper::m_szAppBuildNumber;
     
    WCHAR MiniDumper::m_szMessageText[MAX_WARNING_MESSAGE_PATH];
     
    LPWSTR MiniDumper::m_szDumpFilePath;
     
    #define DEFAULT_ENGLISH_MESSAGE_TEXT L"%s experienced an unknown error and had to exit. \nHowever, some error information has been saved in %s. \nPlease, email this file to  if you would like to help us debug the problem."
     
    #define MAX_DUMP_FILE_NUMBER 9999
     
     
    //static int DUMP_TYPE_MINI = MiniDumpWithUnloadedModules;
     
    //static int DUMP_TYPE_MIDD = MiniDumpWithUnloadedModules | MiniDumpWithIndirectlyReferencedMemory;
     
    static int DUMP_TYPE_FULL = MiniDumpNormal | MiniDumpWithFullMemory | MiniDumpWithDataSegs | MiniDumpWithIndirectlyReferencedMemory | MiniDumpWithHandleData | MiniDumpWithUnloadedModules | MiniDumpWithProcessThreadData;
     
    //static int DUMP_TYPE_FULL = MiniDumpNormal;
    //默认值是只有 MiniDumpNormal 文件比较小
    //如果把所有的选项都加上, 文件很大, 本机调试也没关系
     
     
    MiniDumper::MiniDumper()
    {
        // if this assert fires then you have two instances of MiniDumper
        // which is not allowed
        Q_ASSERT( m_szAppName==NULL );
     
        m_szAppName = wcsdup(L"iLadarDataCollect");
        m_szAppVersion = wcsdup( L"CrashDump");
        m_szAppBuildNumber = wcsdup( L"0000");
     
        wcscpy(m_szMessageText,DEFAULT_ENGLISH_MESSAGE_TEXT);
     
     
        m_szDumpFilePath = NULL;
        ::SetUnhandledExceptionFilter( TopLevelFilter );
    }
     
    MiniDumper::~MiniDumper()
    {
    }
     
    void MiniDumper::SetVersion(LPCWSTR szVersion)
    {
        if(szVersion)
        {
            free(m_szAppVersion);
            m_szAppVersion = wcsdup(szVersion);
        }
    }
     
    void MiniDumper::SetBuildNumber(LPCWSTR szBuildNumber)
    {
        if(szBuildNumber)
        {
            free(m_szAppBuildNumber);
            m_szAppBuildNumber = wcsdup(szBuildNumber);
        }
    }
     
    void MiniDumper::SetDumpFilePath(LPCWSTR szFilePath)
    {
        free(m_szDumpFilePath);
        m_szDumpFilePath = NULL;
        if(szFilePath != NULL)
        {
            m_szDumpFilePath = wcsdup(szFilePath);
        }
    }
     
    LONG MiniDumper::TopLevelFilter( struct _EXCEPTION_POINTERS *pExceptionInfo )
    {
     
        LONG retval = EXCEPTION_CONTINUE_SEARCH;
        HWND hParent = NULL; // find a better value for your app
     
        // firstly see if dbghelp.dll is around and has the function we need
        // look next to the EXE first, as the one in System32 might be old
        // (e.g. Windows 2000)
        HMODULE hDll = NULL;
        WCHAR szDbgHelpPath[_MAX_PATH];
     
        if (GetModuleFileName( NULL, szDbgHelpPath, _MAX_PATH ))
        {
            WCHAR *pSlash = wcsrchr( szDbgHelpPath, L'\\');
            if (pSlash)
            {
                wcscpy( pSlash+1, L"DBGHELP.DLL" );
                hDll = ::LoadLibrary( szDbgHelpPath );
     
            }
     
            if (hDll==NULL)
            {
                // load any version we can
                hDll = ::LoadLibrary( L"DBGHELP.DLL");
            }
     
            LPCWSTR szResult = NULL;
     
            if (hDll)
            {
                MINIDUMPWRITEDUMP pDump = (MINIDUMPWRITEDUMP)::GetProcAddress( hDll, "MiniDumpWriteDump" );
                if (pDump)
                {
                    WCHAR szDumpPath[_MAX_PATH];
                    WCHAR szDumpRootPath[_MAX_PATH];
                    WCHAR szScratch[_MAX_PATH];
     
                    // work out a good place for the dump file
     
                    if(m_szDumpFilePath == NULL)
                    {
                        if (GetModuleFileName(NULL, szDbgHelpPath, _MAX_PATH))
                        {
                            WCHAR *pSlash = wcsrchr(szDbgHelpPath, L'\\');
                            if (pSlash)
                            {
                                wcscpy(pSlash + 1, L"");
                                wcscpy(szDumpPath, szDbgHelpPath);
                            }
                        }
                        else if (!GetTempPath( _MAX_PATH, szDumpPath ))
                            wcscpy( szDumpPath, L"c:\\temp\\" );
                    }
                    else
                    {
                        wcscpy( szDumpPath, m_szDumpFilePath );
                    }
                    wcscpy( szDumpRootPath, szDumpPath);
     
                    //PrintDebug(L"[MiniDumper] Mini Dump file:[%s]",szDumpPath);
     
                    // ask the user if they want to save a dump file
                    //if (::MessageBox( NULL, _T("Something bad happened in your program, would you like to save a diagnostic file?"), m_szAppName, MB_YESNO )==IDYES)
                    {
                        HANDLE hFile = INVALID_HANDLE_VALUE;
                        int i = 1;
                        WCHAR szFileNumber[_MAX_PATH];
                        while(hFile == INVALID_HANDLE_VALUE)
                        {
                            swprintf(szFileNumber, sizeof(szFileNumber), L"_%04d",i);
                            wcscpy( szDumpPath, szDumpRootPath);
                            wcscat( szDumpPath, m_szAppName );
                            wcscat( szDumpPath, L"_" );
                            wcscat( szDumpPath, m_szAppVersion);
                            wcscat( szDumpPath, L"_" );
                            wcscat( szDumpPath, m_szAppBuildNumber);
                            wcscat( szDumpPath, szFileNumber);
                            wcscat( szDumpPath, L".dmp" );
                            hFile = CreateFile( szDumpPath, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_NEW,
                                                FILE_ATTRIBUTE_NORMAL, NULL );
                            i++;
                            if(i > MAX_DUMP_FILE_NUMBER)
                            {
                                hFile = CreateFile( szDumpPath, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS,
                                                    FILE_ATTRIBUTE_NORMAL, NULL );
                                break;
                            }
                        }
                        // create the file
     
                        if (hFile!=INVALID_HANDLE_VALUE)
                        {
                            _MINIDUMP_EXCEPTION_INFORMATION ExInfo;
     
                            ExInfo.ThreadId = GetCurrentThreadId();
                            ExInfo.ExceptionPointers = pExceptionInfo;
                            ExInfo.ClientPointers = NULL;
     
                            // write the dump
                            BOOL bOK = pDump( GetCurrentProcess(), GetCurrentProcessId(), hFile, (MINIDUMP_TYPE)DUMP_TYPE_FULL, &ExInfo, NULL, NULL );
                            if (bOK)
                            {
                                swprintf( szScratch, sizeof(szScratch), L"Saved dump file to '%s'", szDumpPath );
                                szResult = szScratch;
                                retval = EXCEPTION_EXECUTE_HANDLER;
                            }
                            else
                            {
                                swprintf( szScratch, sizeof(szScratch),L"Failed to save dump file to '%s' (error %d)", szDumpPath, GetLastError() );
                                szResult = szScratch;
                            }
                            CloseHandle(hFile);
     
                            WCHAR csOutMessage[MAX_WARNING_MESSAGE_PATH];
                            swprintf(csOutMessage, sizeof(csOutMessage), m_szMessageText, m_szAppName, szDumpPath);
     
                            //PrintError(_T("%s"), csOutMessage);
                            //MessageBoxW( NULL, csOutMessage, m_szAppName , MB_OK );
                            qDebug() << "Dump Crash file ...";
                        }
                        else
                        {
                            swprintf( szScratch, sizeof(szScratch),L"Failed to create dump file '%s' (error %d)", szDumpPath, GetLastError() );
                            szResult = szScratch;
                        }
                    }
     
                }
                else
                {
                    szResult = L"DBGHELP.DLL too old";
                }
            }
            else
            {
                szResult = L"DBGHELP.DLL not found";
            }
     
            if (szResult)
            {
                //PrintDebug(_T("[MiniDumper] Mini Dump result:[%s]"),szResult);
            }
     
            return retval;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227

    main.cpp

    #include "MiniDumper.h"  //头文件
    MiniDumper dump;  //main里加
    
    • 1
    • 2

    错误程序(验证)

    验证是否生成,可以将下面代码放在任意位置。
    会报错:“线程试图读取或写入虚拟地址对于其不具有适应的访问。”

        char *p=NULL;
        *p=1;
    
    • 1
    • 2
  • 相关阅读:
    300. 最长递增子序列
    计算机毕业设计python基于django租房系统-房屋租赁系统
    什么是大票零担?ZETA如何实现大票零担货物追踪可视化?
    大中型体育场馆的经营模式
    Ribbon和LoadBalancer自定义负载均衡算法及配置
    如何发高质量的外链?
    tr 命令
    怎么监控上网记录?监控上网记录的软件推荐
    一维数组根据父id转为嵌套树结构
    Zabbix6.2惊喜发布!特别优化中大型环境部署的性能!
  • 原文地址:https://blog.csdn.net/qq_43211132/article/details/125905636