qt client technology talks - client exception crash handling: dump, corefile 02

Keywords: Linux Qt Mac Windows

1, Foreword

It's normal for the client to have bugs, but if bugs cause the program to crash, we must pay attention to them, so in order to better locate the cause of the crash, we need to do some special handling. Because only linux and window platforms have been built, the following list only deals with these two platforms. There is no way for mac platform to forgive.

2, Collapse under win

The program crash under window can generate dump file, and then use windbg tool to display the crash in the dump file, so as to locate the problem. Next, qt is used to analyze how to use dump to analyze problems.

  1. Make the release version generate dump files as well
    In each. pro file, add:
        QMAKE_LFLAGS_RELEASE += /debug /opt:ref

2. Implement the handling function of crash exception

#ifdef WIN32
#include <windows.h>
#include <dbghelp.h>
#include <QDir>
#pragma comment(lib,"dbghelp.lib")
#define COREFILE_DIR_PATH "C:\\corefile"
#define WCOREFILE_DIR_PATH L"C:\\corefile"

void CreateDumpFile(std::wstring lpstrDumpFilePathName, EXCEPTION_POINTERS *pException)
    HANDLE hDumpFile = CreateFile(lpstrDumpFilePathName.c_str(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
    dumpInfo.ExceptionPointers = pException;
    dumpInfo.ThreadId = GetCurrentThreadId();
    dumpInfo.ClientPointers = TRUE;
    MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hDumpFile, MiniDumpNormal, &dumpInfo, NULL, NULL);

    std::wstring strDumpPath = WCOREFILE_DIR_PATH;

    if (!dirSys.exists())

    SYSTEMTIME time;

    WCHAR szTime[128];
    swprintf_s(szTime, L"%04d-%02d-%02d %02d-%02d-%02d-%03d", time.wYear, time.wMonth, time.wDay, time.wHour, time.wMinute, time.wSecond, time.wMilliseconds);
    strDumpPath += L"\\";
    strDumpPath += szTime;
    strDumpPath += L"-xxxx.dmp";
    CreateDumpFile(strDumpPath, pException);
  1. Register exception handler
#ifdef WIN32
After the above steps, qt can capture and generate the dump file when it crashes abnormally under win.
  1. windbg analysis dump file
    Because it is just analyzing the crash, the complicated usage of windbg will not be described again. Here are the specific steps of analyzing the stack:
  1. With vs installed, you have your own windbg. If you don't have one, you can go to the next one online.
  2. Select symbol Search Path, add the pdb file path, and use; as the separator
  3. Select the source path and add all the paths containing the code
  4. Choose to open the dunmp file
  5. In the input box, type:. reload
  6. Reenter:! analyze – v
  7. View stack

3, Crash under linux

linux program crashes, can be configured in the system to generate corefile files, this is the system's own mechanism, configure the following files, no code implementation, you can write a shell script, in the script to call the following functions
        if [ ! -d $CORE_PATH ]
                mkdir $CORE_PATH

        echo "$CORE_PATH/core-%e-%p-%t" > /proc/sys/kernel/core_pattern

        ulimit -c unlimited

Posted by Cheez on Wed, 17 Jun 2020 23:49:29 -0700