一个宏命令,就可以程序崩溃时生成dump文件

在主程序初始化时加入

DeclareDumpFile();

 

  1 创建头文件DumpFile.h, 将下列代码放进文件中

  2 

  3 #pragma once

  4 #include <windows.h>

  5 #include < Dbghelp.h>

  6 #include <iostream>  

  7 #include <vector>  

  8 using namespace std; 

  9 

 10 

 11 #pragma comment(lib, "Dbghelp.lib")

 12 

 13 

 14 namespace NSDumpFile

 15 { 

 16     void CreateDumpFile(LPCWSTR lpstrDumpFilePathName, EXCEPTION_POINTERS *pException)  

 17     {  

 18         // 创建Dump文件  

 19         //  

 20         HANDLE hDumpFile = CreateFile(lpstrDumpFilePathName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);  

 21 

 22 

 23         // Dump信息  

 24         //  

 25         MINIDUMP_EXCEPTION_INFORMATION dumpInfo;  

 26         dumpInfo.ExceptionPointers = pException;  

 27         dumpInfo.ThreadId = GetCurrentThreadId();  

 28         dumpInfo.ClientPointers = TRUE;  

 29 

 30 

 31         // 写入Dump文件内容  

 32         //  

 33         MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hDumpFile, MiniDumpNormal, &dumpInfo, NULL, NULL);  

 34 

 35 

 36         CloseHandle(hDumpFile);  

 37     }  

 38 

 39 

 40     LPTOP_LEVEL_EXCEPTION_FILTER WINAPI MyDummySetUnhandledExceptionFilter(LPTOP_LEVEL_EXCEPTION_FILTER lpTopLevelExceptionFilter)

 41     {

 42         return NULL;

 43     }

 44 

 45 

 46     BOOL PreventSetUnhandledExceptionFilter()

 47     {

 48         HMODULE hKernel32 = LoadLibrary(_T("kernel32.dll"));

 49         if (hKernel32 ==   NULL)

 50             return FALSE;

 51 

 52 

 53         void *pOrgEntry = GetProcAddress(hKernel32, "SetUnhandledExceptionFilter");

 54         if(pOrgEntry == NULL)

 55             return FALSE;

 56 

 57 

 58         unsigned char newJump[ 100 ];

 59         DWORD dwOrgEntryAddr = (DWORD) pOrgEntry;

 60         dwOrgEntryAddr += 5; // add 5 for 5 op-codes for jmp far

 61 

 62 

 63         void *pNewFunc = &MyDummySetUnhandledExceptionFilter;

 64         DWORD dwNewEntryAddr = (DWORD) pNewFunc;

 65         DWORD dwRelativeAddr = dwNewEntryAddr -  dwOrgEntryAddr;

 66 

 67 

 68         newJump[ 0 ] = 0xE9;  // JMP absolute

 69         memcpy(&newJump[ 1 ], &dwRelativeAddr, sizeof(pNewFunc));

 70         SIZE_T bytesWritten;

 71         BOOL bRet = WriteProcessMemory(GetCurrentProcess(),    pOrgEntry, newJump, sizeof(pNewFunc) + 1, &bytesWritten);

 72         return bRet;

 73     }

 74 

 75 

 76     LONG WINAPI UnhandledExceptionFilterEx(struct _EXCEPTION_POINTERS *pException)

 77     {

 78         TCHAR szMbsFile[MAX_PATH] = { 0 };

 79         ::GetModuleFileName(NULL, szMbsFile, MAX_PATH);

 80         TCHAR* pFind = _tcsrchr(szMbsFile, '\\');

 81         if(pFind)

 82         {

 83             *(pFind+1) = 0;

 84             _tcscat(szMbsFile, _T("CrashDumpFile.dmp"));

 85             CreateDumpFile(szMbsFile, pException);

 86         }

 87 

 88 

 89         // TODO: MiniDumpWriteDump

 90         FatalAppExit(-1,  _T("Fatal Error"));

 91         return EXCEPTION_CONTINUE_SEARCH;

 92     }

 93 

 94 

 95     void RunCrashHandler()

 96     {

 97         SetUnhandledExceptionFilter(UnhandledExceptionFilterEx);

 98         PreventSetUnhandledExceptionFilter();

 99     }

100 };

101 

102 

103 #define DeclareDumpFile() NSDumpFile::RunCrashHandler();

 

你可能感兴趣的:(dump)