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.
- Make the release version generate dump files as well
In each. pro file, add:
win32:{ QMAKE_LFLAGS_RELEASE += /MAP QMAKE_CFLAGS_RELEASE += /Zi 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); MINIDUMP_EXCEPTION_INFORMATION dumpInfo; dumpInfo.ExceptionPointers = pException; dumpInfo.ThreadId = GetCurrentThreadId(); dumpInfo.ClientPointers = TRUE; MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hDumpFile, MiniDumpNormal, &dumpInfo, NULL, NULL); CloseHandle(hDumpFile); } LONG WINAPI CrashHandler(EXCEPTION_POINTERS *pException) { std::wstring strDumpPath = WCOREFILE_DIR_PATH; QDir dirSys(COREFILE_DIR_PATH); if (!dirSys.exists()) { dirSys.mkpath(COREFILE_DIR_PATH); } SYSTEMTIME time; GetLocalTime(&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); return EXCEPTION_EXECUTE_HANDLER; } #endif
- Register exception handler
#ifdef WIN32 SetUnhandledExceptionFilter((LPTOP_LEVEL_EXCEPTION_FILTER)CrashHandler); #endif
After the above steps, qt can capture and generate the dump file when it crashes abnormally under win.
- 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:
- With vs installed, you have your own windbg. If you don't have one, you can go to the next one online.
- Select symbol Search Path, add the pdb file path, and use; as the separator
- Select the source path and add all the paths containing the code
- Choose to open the dunmp file
- In the input box, type:. reload
- Reenter:! analyze – v
- 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
setCoreDumpFormat() { CORE_PATH=/corefile if [ ! -d $CORE_PATH ] then mkdir $CORE_PATH fi echo "$CORE_PATH/core-%e-%p-%t" > /proc/sys/kernel/core_pattern ulimit -c unlimited }