qt pro 设置dump文件机制
dump文件可以很好的捕获到程序crash的异常点。
pro文件设置如下:
#dump 调试 #DEFINES += WITH_DUMP contains(DEFINES, WITH_DUMP){ QMAKE_CXXFLAGS_RELEASE = $$QMAKE_CXXFLAGS_RELEASE_WITH_DEBUGINFO QMAKE_LFLAGS_RELEASE = $$QMAKE_LFLAGS_RELEASE_WITH_DEBUGINFO LIBS += -lDbghelp message("WITH_DUMP") }else{ message("WITHOUT_DUMP") }
main.cpp文件添加生成代码如下:
#ifdef WITH_DUMP #include <dbghelp.h> #include <exception> #endif
#ifdef WITH_DUMP long ApplicationCrashHandler(EXCEPTION_POINTERS *pException) { QString strdir = QString("%1/dump").arg(qApp->applicationDirPath()); XCore::dirCreate(strdir); QDateTime dtcur = QDateTime::currentDateTime(); QString strcur = dtcur.toString("yyyyMMddhhmmss"); QString strfile = QString("%1/%2.dmp").arg(strdir).arg(strcur); EXCEPTION_RECORD *record = pException->ExceptionRecord; XCore::writeFile(QString::number(record->ExceptionCode, 16), "dump"); XCore::writeFile(QString::number(record->ExceptionFlags, 16), "dump"); //创建 Dump 文件 HANDLE hDumpFile = CreateFile((LPCWSTR)strfile.utf16(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if(hDumpFile != INVALID_HANDLE_VALUE) { //Dump信息 MINIDUMP_EXCEPTION_INFORMATION dumpInfo; dumpInfo.ExceptionPointers = pException; dumpInfo.ThreadId = GetCurrentThreadId(); dumpInfo.ClientPointers = TRUE; //写入Dump文件内容 MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(),hDumpFile, MiniDumpNormal, &dumpInfo, NULL, NULL); CloseHandle(hDumpFile); }else{ XCore::writeFile("create dump file failed", "dump"); } return EXCEPTION_EXECUTE_HANDLER; } #endif
#ifdef WITH_DUMP //注册异常捕获函数 SetUnhandledExceptionFilter((LPTOP_LEVEL_EXCEPTION_FILTER)ApplicationCrashHandler); #endif