崩溃转储生成和分析问题

时间:2018-03-22 16:59:42

标签: c++ winapi crash-dumps

我使用了https://github.com/hdeldar/QtCrashDumper中的示例,并在我的应用程序中使用了相同的内容。最近对于应用程序中的实际崩溃,堆栈跟踪有点无用。

另一个问题是dmp文件生成的模块版本不匹配。我已经仔细检查了模块版本并且它是相同的但是转储文件有不同的版本。文件版本为2.8.0.4,dmp文件为2.08.0.4

Module version

我已经在stackoverflow上查看了几个答案,以便不了解我缺少的功能。 CrashDump Info

KB链接不再存在 - KB313109

我的实施

#include "CrashDumpDef.h"
#include "version.h"

#ifdef USE_MINI_DUMP
#include "CrashDump.h"
#include "FileSystem.h"


LPCWSTR CrashDump::m_szAppName;

LPWSTR CrashDump::m_szAppVersion;

LPWSTR CrashDump::m_szAppBuildNumber;

WCHAR CrashDump::m_szMessageText[MAX_WARNING_MESSAGE_PATH];

LPWSTR CrashDump::m_szDumpFilePath;

#define DEFAULT_MESSAGE_TEXT L"%s Designer has experienced an issue. \nCrash dump has been saved in %s."

#define MAX_DUMP_FILE_NUMBER 9999



CrashDump::CrashDump(LPCWSTR szAppName, LPCWSTR szVersion, LPCWSTR szBuildNumber)
{
    // if this assert fires then you have two instances of CrashDump
    // which is not allowed
    Q_ASSERT(m_szAppName == NULL);

    const char* sz = VER_PRODUCTVERSION_STR;
    std::vector<wchar_t> vec;
    size_t len = strlen(sz);
    vec.resize(len + 1);
    mbstowcs(&vec[0], sz, len);
    const wchar_t* productVersion = &vec[0];

    std::string version = VER_PRODUCTVERSION_STR;
    char build = version.back();
    const char* buildNum = new char(build);
    std::vector<wchar_t> vec1;
    size_t lenA = strlen(buildNum);
    vec1.resize(lenA + 1);
    mbstowcs(&vec1[0], buildNum, lenA);
    const wchar_t* buildNumber = &vec1[0];


    m_szAppName = szAppName ? wcsdup(szAppName) : wcsdup(L"Designer");
    m_szAppVersion = productVersion ? wcsdup(productVersion) : wcsdup(productVersion);
    m_szAppBuildNumber = buildNumber ? wcsdup(buildNumber) : wcsdup(buildNumber);

    wcscpy(m_szMessageText, DEFAULT_MESSAGE_TEXT);

    m_szDumpFilePath = NULL;

    ::SetUnhandledExceptionFilter(TopLevelFilter);
}

CrashDump::~CrashDump()
{
}

void CrashDump::SetVersion(LPCWSTR szVersion)
{
    if (szVersion)
    {
        free(m_szAppVersion);
        m_szAppVersion = wcsdup(szVersion);
    }
}

void CrashDump::SetBuildNumber(LPCWSTR szBuildNumber)
{
    if (szBuildNumber)
    {
        free(m_szAppBuildNumber);
        m_szAppBuildNumber = wcsdup(szBuildNumber);
    }
}

void CrashDump::SetDumpFilePath(LPCWSTR szFilePath)

{
    free(m_szDumpFilePath);

    {
        m_szDumpFilePath = wcsdup(szFilePath);    
    }
}

LONG CrashDump::TopLevelFilter(struct _EXCEPTION_POINTERS *pExceptionInfo)
{
    //::MessageBoxW(NULL, L"debugging", m_szAppName, MB_OK);

    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 - add the path here

            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))                   

                std::string dmpFile = filesystem::buildFilename(QStringList()
                    << QDir::toNativeSeparators(QDir::homePath()) + "\\AppData\\Roaming\\ABC\\logs\\"         
                    ).toStdString();


                std::wstring wideDmpFile;
                for (int i = 0; i < dmpFile.length(); ++i)
                    wideDmpFile += wchar_t(dmpFile[i]);

                const wchar_t* szName = wideDmpFile.c_str();                   
                    wcscpy(szDumpPath, szName);                         
            }
            else
            {
                wcscpy(szDumpPath, m_szDumpFilePath);
            }
            wcscpy(szDumpRootPath, szDumpPath);

            //PrintDebug(L"[CrashDump] Mini Dump file:[%s]",szDumpPath);    

            // ask the user if they want to save a dump file
            //if (::MessageBox( NULL, _T("Crash, 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, MiniDumpNormal, &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);
                }
                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("[CrashDump] Mini Dump result:[%s]"),szResult);     
    }

    return retval;
}
#endif

0 个答案:

没有答案