程序dmp文件生成

 1 #pragma once
 2 #include <windows.h>
 3 #include <imagehlp.h>
 4 #include <stdlib.h>
 5 #include <iostream>
 6 #pragma comment(lib, "dbghelp.lib")
 7 
 8 
 9 inline BOOL IsDataSectionNeeded(const WCHAR* pModuleName)
10 
11 {
12   if(pModuleName == 0)
13   {
14        return FALSE;
15   } 
16 
17   WCHAR szFileName[_MAX_FNAME] = L"";
18   _wsplitpath(pModuleName, NULL, NULL, szFileName, NULL); 
19 
20   if(wcsicmp(szFileName, L"ntdll") == 0)
21        return TRUE;
22   return FALSE; 
23 }
24 
25  
26 
27 inline BOOL CALLBACK MiniDumpCallback(PVOID   pParam,const PMINIDUMP_CALLBACK_INPUT pInput,PMINIDUMP_CALLBACK_OUTPUT  pOutput)
28 
29 {
30   if(pInput == 0 || pOutput == 0)
31        return FALSE; 
32 
33   switch(pInput->CallbackType)
34   {
35   case ModuleCallback: 
36        if(pOutput->ModuleWriteFlags & ModuleWriteDataSeg) 
37             if(!IsDataSectionNeeded(pInput->Module.FullPath)) 
38                 pOutput->ModuleWriteFlags &= (~ModuleWriteDataSeg); 
39   case IncludeModuleCallback:
40   case IncludeThreadCallback:
41   case ThreadCallback:
42   case ThreadExCallback:
43        return TRUE;
44   default:;
45   }
46   return FALSE;
47 }
48 
49  
50 
51 inline void CreateMiniDump(EXCEPTION_POINTERS* pep, LPCTSTR strFileName)
52 {
53   HANDLE hFile = CreateFile(strFileName, GENERIC_READ | GENERIC_WRITE,
54        0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); 
55 
56   if((hFile != NULL) && (hFile != INVALID_HANDLE_VALUE))
57   {
58        MINIDUMP_EXCEPTION_INFORMATION mdei;
59        mdei.ThreadId           = GetCurrentThreadId();
60        mdei.ExceptionPointers  = pep;
61        mdei.ClientPointers     = FALSE;
62        MINIDUMP_CALLBACK_INFORMATION mci;
63        mci.CallbackRoutine     = (MINIDUMP_CALLBACK_ROUTINE)MiniDumpCallback;
64        mci.CallbackParam       = 0;
65        MINIDUMP_TYPE mdt = (MINIDUMP_TYPE)(MiniDumpWithPrivateReadWriteMemory | 
66             MiniDumpWithDataSegs | 
67             MiniDumpWithHandleData |
68             0x00000800 /*MiniDumpWithFullMemoryInfo*/ | 
69             0x00001000 /*MiniDumpWithThreadInfo*/ | 
70             MiniDumpWithUnloadedModules);
71        MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(),
72             hFile, mdt, (pep != 0) ? &mdei : 0, 0, &mci);
73        CloseHandle(hFile); 
74   }
75 }
新建一個頭文件將上面內容粘貼到文件中
 1 std::ostream& operator<<(std::ostream& os, const EXCEPTION_RECORD& red)
 2 { 3 return os << " Thread ID:" << GetCurrentThreadId() 4 << " ExceptionCode: " << red.ExceptionCode << "\n" 5 << " ExceptionFlags: " << red.ExceptionFlags << "\n" 6 << " ExceptionAddress: " << red.ExceptionAddress << "\n" 7 << " NumberParameters: " << red.NumberParameters; 8 } 9 LONG WINAPI GPTUnhandledExceptionFilter(PEXCEPTION_POINTERS pExceptionInfo) 10 { 11 //保存現場文件 12 CreateMiniDump(pExceptionInfo, "testdmp.dmp"); 13 cerr<<"未知錯誤:"<<(*pExceptionInfo->ExceptionRecord)<<endl; 14 exit(pExceptionInfo->ExceptionRecord->ExceptionCode); 15 return EXCEPTION_EXECUTE_HANDLER; // 程序中止運行 16 }

添加上面內容到cpp文件中ios

//註冊生成dmp文件函數 此代碼應放在可能出現異常代碼前面
SetUnhandledExceptionFilter(GPTUnhandledExceptionFilter);windows

相關文章
相關標籤/搜索