Brief introduction
Before reprinted an article Qt- generate a dump file before you let your client crashes , the article describes how to generate a crash log, not enough visual image, if we can generate a dump file vs open so much the better, let's start introduction.
Qt + MSVC compiler
This situation is actually relatively simple, adding "CONFIG + = force_debug_info" "CONFIG + = separate_debug_info" on additional parameters build configuration items directly qmale where we can, mainly used to generate pdb files.
Dump Dump function Helper.h header file:
#pragma once
#include <tchar.h>
#include <Windows.h>
#include <DbgHelp.h>
#pragma comment(lib, "user32.lib")
int GenerateMiniDump(PEXCEPTION_POINTERS pExceptionPointers)
{
// 定义函数指针
typedef BOOL(WINAPI * MiniDumpWriteDumpT)(
HANDLE,
DWORD,
HANDLE,
MINIDUMP_TYPE,
PMINIDUMP_EXCEPTION_INFORMATION,
PMINIDUMP_USER_STREAM_INFORMATION,
PMINIDUMP_CALLBACK_INFORMATION
);
// 从 "DbgHelp.dll" 库中获取 "MiniDumpWriteDump" 函数
MiniDumpWriteDumpT pfnMiniDumpWriteDump = NULL;
HMODULE hDbgHelp = LoadLibrary(_T("DbgHelp.dll"));
if (NULL == hDbgHelp)
{
return EXCEPTION_CONTINUE_EXECUTION;
}
pfnMiniDumpWriteDump = (MiniDumpWriteDumpT)GetProcAddress(hDbgHelp, "MiniDumpWriteDump");
if (NULL == pfnMiniDumpWriteDump)
{
FreeLibrary(hDbgHelp);
return EXCEPTION_CONTINUE_EXECUTION;
}
// 创建 dmp 文件件
TCHAR szFileName[MAX_PATH] = { 0 };
TCHAR szVersion[] = L"DumpFile";
SYSTEMTIME stLocalTime;
GetLocalTime(&stLocalTime);
wsprintf(szFileName, L"%s-%04d%02d%02d-%02d%02d%02d.dmp",
szVersion, stLocalTime.wYear, stLocalTime.wMonth, stLocalTime.wDay,
stLocalTime.wHour, stLocalTime.wMinute, stLocalTime.wSecond);
HANDLE hDumpFile = CreateFile(szFileName, GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_WRITE | FILE_SHARE_READ, 0, CREATE_ALWAYS, 0, 0);
if (INVALID_HANDLE_VALUE == hDumpFile)
{
FreeLibrary(hDbgHelp);
return EXCEPTION_CONTINUE_EXECUTION;
}
// 写入 dmp 文件
MINIDUMP_EXCEPTION_INFORMATION expParam;
expParam.ThreadId = GetCurrentThreadId();
expParam.ExceptionPointers = pExceptionPointers;
expParam.ClientPointers = FALSE;
pfnMiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(),
hDumpFile, MiniDumpWithDataSegs, (pExceptionPointers ? &expParam : NULL), NULL, NULL);
// 释放文件
CloseHandle(hDumpFile);
FreeLibrary(hDbgHelp);
return EXCEPTION_EXECUTE_HANDLER;
}
LONG WINAPI ExceptionFilter(LPEXCEPTION_POINTERS lpExceptionInfo)
{
// 这里做一些异常的过滤或提示
if (IsDebuggerPresent()) {
return EXCEPTION_CONTINUE_SEARCH;
}
return GenerateMiniDump(lpExceptionInfo);
}
transfer:
#include <QApplication>
#include "Helper.h"
int main(int argc, char *argv[])
{
SetUnhandledExceptionFilter(ExceptionFilter);
QApplication a(argc, argv);
int *p = nullptr;
*p = 100;
return a.exec();
}
To a null pointer assignment program will certainly crash out.
Then we will pdb file and generate dump files are copied to the source directory:
then use VS2015 to open the dump file:
Click set the symbol path, select the Microsoft Symbol Server, click to confirm, for the first time, then the time will be longer, be patient:
Then click only limit native debugging, and then navigate to the location of the code will be problems, there will be debugging information is very easy:
Use google framework of breakpad
Breakpad on the use of the Qt framework, this article is talking about well, you can see next.
https://github.com/JPNaude/dev_notes/wiki/Using-Google-Breakpad-with-Qt