龙岩网站建设馨烨,说说刷赞网站推广,萝岗区营销型网站建设,网络设计报告网络安全一#xff0c;为什么需要dump文件 Windows客户端应用开发时#xff0c;难免会遇到程序崩溃问题。当程序在Debug下运行崩溃时#xff0c;我们可以直接定位到崩溃点。但是当程序打包成Release发布时#xff0c;难免会遇到一些崩溃问题。一般遇到这样的崩溃#xff0c;我们就…一为什么需要dump文件 Windows客户端应用开发时难免会遇到程序崩溃问题。当程序在Debug下运行崩溃时我们可以直接定位到崩溃点。但是当程序打包成Release发布时难免会遇到一些崩溃问题。一般遇到这样的崩溃我们就需要使用 dump 文件加上符号表文件来进行调试程序。
二如何生成dump文件
工欲善其事必先利其器。这里直接给出一个CrashDump类供各位大佬使用。在main函数实例化即可。生成不了dump文件你来打我~
三CrashDump代码 CrashDump.h #pragma once#include Windows.h
#include Dbghelp.hclass CrashDump {
public:explicit CrashDump();~CrashDump();
private:static LONG WINAPI UnhandledExceptionFilter(struct _EXCEPTION_POINTERS* pExceptionInfo);
private:LPTOP_LEVEL_EXCEPTION_FILTER m_oldExceptionFilter;
};CrashDump.cpp #include CrashDump.h
#include ctime
#include string
#include sstream
#include assert.h
#include DbgHelp.h
#include shellapi.h
#include iostreamtypedef BOOL(WINAPI *getUserModeExceptionProc)(LPDWORD);
typedef BOOL(WINAPI *setUserModeExceptionProc)(DWORD);typedef BOOL(WINAPI *MINIDUMPWRITEDUMP) (HANDLE hProcess,DWORD ProcessId,HANDLE hFile,MINIDUMP_TYPE DumpType,PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam,PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam,PMINIDUMP_CALLBACK_INFORMATION CallbackParam);static std::wstring format(const wchar_t* pszFormat, ...) {wchar_t buffer[MAX_PATH] { 0 };va_list ap;va_start(ap, pszFormat);int nCount ::vswprintf_s(buffer, _countof(buffer), pszFormat, ap);va_end(ap);if (nCount 0) {assert(false);return pszFormat;}return buffer;
}CrashDump::CrashDump(): m_oldExceptionFilter(NULL) {// 堆异常BOOL bRet ::HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0);::OutputDebugStringW(format(LHeapSetInformation, bRet: %lu, %lu.\n, bRet, ::GetLastError()).c_str());// DEP策略bRet ::SetProcessDEPPolicy(PROCESS_DEP_ENABLE | PROCESS_DEP_DISABLE_ATL_THUNK_EMULATION);::OutputDebugStringW(format(LSetProcessDEPPolicy, bRet: %lu, %lu.\n, bRet, ::GetLastError()).c_str());//standard app-wide unhandled exception filterm_oldExceptionFilter ::SetUnhandledExceptionFilter(UnhandledExceptionFilter);//fix for exceptions being swallowed inside callbacks (see KB976038)HMODULE hKernel32 GetModuleHandle(TEXT(KERNEL32));if (NULL hKernel32) {::OutputDebugStringW(LGetModuleHandle faled.\n);} else {DWORD dwFlags 0;getUserModeExceptionProc procGetProcessUserModeExceptionPolicy;setUserModeExceptionProc procSetProcessUserModeExceptionPolicy;procGetProcessUserModeExceptionPolicy (getUserModeExceptionProc)::GetProcAddress(hKernel32, GetProcessUserModeExceptionPolicy);procSetProcessUserModeExceptionPolicy (setUserModeExceptionProc)::GetProcAddress(hKernel32, SetProcessUserModeExceptionPolicy);if (procGetProcessUserModeExceptionPolicy procSetProcessUserModeExceptionPolicy) {if (procGetProcessUserModeExceptionPolicy(dwFlags)) {bRet procSetProcessUserModeExceptionPolicy(dwFlags ~1);::OutputDebugStringW(format(LGetProcessUserModeExceptionPolicy, bRet: %lu.\n, bRet).c_str());}::OutputDebugStringW(format(LSetProcessUserModeExceptionPolicy, bRet: %lu, dwFlags: %lu.\n, bRet, dwFlags).c_str());}}
}CrashDump::~CrashDump() {if (NULL ! m_oldExceptionFilter) {::SetUnhandledExceptionFilter(m_oldExceptionFilter);}
}LONG WINAPI CrashDump::UnhandledExceptionFilter(struct _EXCEPTION_POINTERS* pExceptionInfo) {//always break into a debugger if one is presentif (::IsDebuggerPresent()) {::OutputDebugStringW(LIsDebuggerPresent return TRUE.\n);return EXCEPTION_CONTINUE_SEARCH;}static BOOL inExceptionHandler FALSE;if (inExceptionHandler) {::OutputDebugStringW(LCurrent function has crashed.Shit.\n);return EXCEPTION_CONTINUE_SEARCH;}inExceptionHandler TRUE;WCHAR fullPath[MAX_PATH] { 0 };DWORD pathLength ::GetModuleFileNameW(NULL, fullPath, MAX_PATH);if (0 pathLength) {::OutputDebugStringW(LGetModuleFileNameW failed.\n);return EXCEPTION_CONTINUE_SEARCH;}LPCWSTR lastSlash ::wcsrchr(fullPath, L\\);if (NULL lastSlash) {::OutputDebugStringW(Lwcsrchr return wrong.\n);return EXCEPTION_CONTINUE_SEARCH;}std::wstring exeDirPath(fullPath, lastSlash - fullPath 1);WCHAR filePath[MAX_PATH] { 0 };for (int i 0; ; i) { // 避免同名 SYSTEMTIME sys_time { 0 };::GetLocalTime(sys_time);::swprintf_s(filePath, _countof(filePath) - 1, L%s%04u_%02u_%02u_%02u_%02u_%02u_%d.dmp, exeDirPath.c_str(), sys_time.wYear, sys_time.wMonth, sys_time.wDay, sys_time.wHour, sys_time.wMinute, sys_time.wSecond, i);if (::GetFileAttributes(filePath) INVALID_FILE_ATTRIBUTES) {break;}}HANDLE hFile ::CreateFileW(filePath, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);if (INVALID_HANDLE_VALUE hFile) {::OutputDebugStringW(LCreateFileW failed.\n);return EXCEPTION_CONTINUE_SEARCH;}//load dbghelp dynamicallyHMODULE hDbgHelp LoadLibraryW(LDBGHELP);if (!hDbgHelp) {::OutputDebugStringW(LLoadLibraryW DBGHELP failed.\n);return EXCEPTION_CONTINUE_SEARCH;}MINIDUMPWRITEDUMP fnMiniDumpWriteDump (MINIDUMPWRITEDUMP)::GetProcAddress(hDbgHelp, MiniDumpWriteDump);if (!fnMiniDumpWriteDump) {::OutputDebugStringW(LGetProcAddress MiniDumpWriteDump failed.\n);::FreeLibrary(hDbgHelp);return EXCEPTION_CONTINUE_SEARCH;}MINIDUMP_TYPE dumpFlags (MINIDUMP_TYPE)(MiniDumpWithIndirectlyReferencedMemory | MiniDumpWithUnloadedModules | MiniDumpWithProcessThreadData);MINIDUMP_EXCEPTION_INFORMATION miniInfo {0};miniInfo.ClientPointers TRUE;miniInfo.ExceptionPointers pExceptionInfo;miniInfo.ThreadId ::GetCurrentThreadId();//generate a minidump if possibleif (fnMiniDumpWriteDump(::GetCurrentProcess(), ::GetCurrentProcessId(), hFile, dumpFlags, miniInfo, NULL, NULL)) {WCHAR buffer[MAX_PATH] { 0 }; ::swprintf_s(buffer, _countof(buffer) - 1, LProcess has crashed.\nMinidump was saved to: \n\\%s\n, filePath);::OutputDebugStringW(buffer);// ::MessageBoxW(NULL, buffer, NULL, MB_ICONERROR | MB_OK);} else {::OutputDebugStringW(format(LMinidump was saved failed: %hu.\n, ::GetLastError()).c_str());// ::MessageBoxW(NULL, format(LMinidump was saved failed: %hu.\n, ::GetLastError()).c_str(), NULL, MB_ICONERROR | MB_OK);}::FreeLibrary(hDbgHelp);::CloseHandle(hFile);return EXCEPTION_CONTINUE_SEARCH;
}
四在项目中调用
int main(int argc, char* argv[]) {
...CrashDump crashDump; // 直接实例化即可
...
} 注意要用exe启动不要用vs启动程序
文章转载自: http://www.morning.njnqn.cn.gov.cn.njnqn.cn http://www.morning.nhlnh.cn.gov.cn.nhlnh.cn http://www.morning.nwjd.cn.gov.cn.nwjd.cn http://www.morning.nggbf.cn.gov.cn.nggbf.cn http://www.morning.sjjq.cn.gov.cn.sjjq.cn http://www.morning.zqcgt.cn.gov.cn.zqcgt.cn http://www.morning.hclqy.cn.gov.cn.hclqy.cn http://www.morning.pfgln.cn.gov.cn.pfgln.cn http://www.morning.rwzqn.cn.gov.cn.rwzqn.cn http://www.morning.gwxwl.cn.gov.cn.gwxwl.cn http://www.morning.htjwz.cn.gov.cn.htjwz.cn http://www.morning.clgbb.cn.gov.cn.clgbb.cn http://www.morning.rmtmk.cn.gov.cn.rmtmk.cn http://www.morning.wxfgg.cn.gov.cn.wxfgg.cn http://www.morning.qlpyn.cn.gov.cn.qlpyn.cn http://www.morning.djpps.cn.gov.cn.djpps.cn http://www.morning.dtrcl.cn.gov.cn.dtrcl.cn http://www.morning.gwhjy.cn.gov.cn.gwhjy.cn http://www.morning.xfyjn.cn.gov.cn.xfyjn.cn http://www.morning.stlgg.cn.gov.cn.stlgg.cn http://www.morning.thrtt.cn.gov.cn.thrtt.cn http://www.morning.xbdrc.cn.gov.cn.xbdrc.cn http://www.morning.pluimers.cn.gov.cn.pluimers.cn http://www.morning.tbnpn.cn.gov.cn.tbnpn.cn http://www.morning.mggwr.cn.gov.cn.mggwr.cn http://www.morning.fglyb.cn.gov.cn.fglyb.cn http://www.morning.gqcd.cn.gov.cn.gqcd.cn http://www.morning.nbpqx.cn.gov.cn.nbpqx.cn http://www.morning.zlnmm.cn.gov.cn.zlnmm.cn http://www.morning.rnxs.cn.gov.cn.rnxs.cn http://www.morning.fchkc.cn.gov.cn.fchkc.cn http://www.morning.tmlhh.cn.gov.cn.tmlhh.cn http://www.morning.nkkpp.cn.gov.cn.nkkpp.cn http://www.morning.stbfy.cn.gov.cn.stbfy.cn http://www.morning.bhbxd.cn.gov.cn.bhbxd.cn http://www.morning.thmlt.cn.gov.cn.thmlt.cn http://www.morning.nzwp.cn.gov.cn.nzwp.cn http://www.morning.xqjh.cn.gov.cn.xqjh.cn http://www.morning.pwhjr.cn.gov.cn.pwhjr.cn http://www.morning.zdxss.cn.gov.cn.zdxss.cn http://www.morning.zcsch.cn.gov.cn.zcsch.cn http://www.morning.qbzfp.cn.gov.cn.qbzfp.cn http://www.morning.qdsmile.cn.gov.cn.qdsmile.cn http://www.morning.kyfnh.cn.gov.cn.kyfnh.cn http://www.morning.jlpdc.cn.gov.cn.jlpdc.cn http://www.morning.smyxl.cn.gov.cn.smyxl.cn http://www.morning.mdnnz.cn.gov.cn.mdnnz.cn http://www.morning.nfzzf.cn.gov.cn.nfzzf.cn http://www.morning.sldrd.cn.gov.cn.sldrd.cn http://www.morning.brscd.cn.gov.cn.brscd.cn http://www.morning.pdwny.cn.gov.cn.pdwny.cn http://www.morning.qggxt.cn.gov.cn.qggxt.cn http://www.morning.ldfcb.cn.gov.cn.ldfcb.cn http://www.morning.jpkk.cn.gov.cn.jpkk.cn http://www.morning.csnmd.cn.gov.cn.csnmd.cn http://www.morning.dyfmh.cn.gov.cn.dyfmh.cn http://www.morning.fhlfp.cn.gov.cn.fhlfp.cn http://www.morning.bpmtl.cn.gov.cn.bpmtl.cn http://www.morning.mbpzw.cn.gov.cn.mbpzw.cn http://www.morning.ktrh.cn.gov.cn.ktrh.cn http://www.morning.npbgj.cn.gov.cn.npbgj.cn http://www.morning.nlqgb.cn.gov.cn.nlqgb.cn http://www.morning.xrpwk.cn.gov.cn.xrpwk.cn http://www.morning.pjxw.cn.gov.cn.pjxw.cn http://www.morning.hjwxm.cn.gov.cn.hjwxm.cn http://www.morning.zxxys.cn.gov.cn.zxxys.cn http://www.morning.zrmxp.cn.gov.cn.zrmxp.cn http://www.morning.qsy39.cn.gov.cn.qsy39.cn http://www.morning.mtmnk.cn.gov.cn.mtmnk.cn http://www.morning.lffbz.cn.gov.cn.lffbz.cn http://www.morning.wjhnx.cn.gov.cn.wjhnx.cn http://www.morning.simpliq.cn.gov.cn.simpliq.cn http://www.morning.cwkcq.cn.gov.cn.cwkcq.cn http://www.morning.jsmyw.cn.gov.cn.jsmyw.cn http://www.morning.wqpsf.cn.gov.cn.wqpsf.cn http://www.morning.wjlnz.cn.gov.cn.wjlnz.cn http://www.morning.cyhlq.cn.gov.cn.cyhlq.cn http://www.morning.kqbjy.cn.gov.cn.kqbjy.cn http://www.morning.xfxlr.cn.gov.cn.xfxlr.cn http://www.morning.ztcxx.com.gov.cn.ztcxx.com