1. 如何生成dump文件?
原理:通过SetUnhandledExceptionFilter设置捕获dump的入口,然后通过MiniDumpWriteDump生成dump文件;
SetUnhandledExceptionFilter:
https://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&l=EN-US&k=k(SETUNHANDLEDEXCEPTIONFILTER);k(DevLang-%22C%2B%2B%22)&rd=true
MiniDumpWriteDump:
https://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&l=EN-US&k=k(MINIDUMPWRITEDUMP);k(DevLang-%22C%2B%2B%22)&rd=true
示例:
1 #ifndef _DUMP_GENERATE_H_ 2 #define _DUMP_GENERATE_H_ 3 4 #include <Windows.h> 5 #include <DbgHelp.h> 6 #pragma comment(lib, "DbgHelp.lib") 7 8 LONG WINAPI MyUnhandledExceptionFilter(_In_ struct _EXCEPTION_POINTERS *ExceptionInfo); 9 void MyDumpGenerate(); 10 11 void MyDumpGenerate() 12 { 13 SetUnhandledExceptionFilter(MyUnhandledExceptionFilter); 14 } 15 16 LONG WINAPI MyUnhandledExceptionFilter(_In_ struct _EXCEPTION_POINTERS *ExceptionInfo) 17 { 18 MessageBox(0,L"DumpGenerate",0,0); 19 20 HANDLE lhDumpFile = CreateFile(L"D:\\test.dmp", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL, NULL); 21 22 MINIDUMP_EXCEPTION_INFORMATION loExceptionInfo; 23 loExceptionInfo.ExceptionPointers = ExceptionInfo; 24 loExceptionInfo.ThreadId = GetCurrentThreadId(); 25 loExceptionInfo.ClientPointers = TRUE; 26 MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), lhDumpFile, MiniDumpNormal, &loExceptionInfo, NULL, NULL); 27 28 CloseHandle(lhDumpFile); 29 30 /* 31 * EXCEPTION_CONTINUE_SEARCH:将异常传给调试器 32 * EXCEPTION_EXECUTE_HANDLER:不显示错误信息 33 */ 34 return EXCEPTION_EXECUTE_HANDLER; 35 } 36 37 #endif // _DUMP_GENERATE_H_ 38 39 //// 40 // main.cpp 41 int main() 42 { 43 MyDumpGenerate(); 44 int* p = NULL; 45 *p = 1; 46 // 这里异常后,会自动调用MyUnhandledExceptionFilter接口,生成dump文件:test.dmp。 47 return 0; 48 }
2. 如何使用WinDbg调试dump文件,以test.dmp为例:
2.1 准备步骤:
2.1.1 设置pdb文件路径:
2.1.2 设置源文件路径:
2.1.3 导入/拖入test.dmp文件;
2.2 通过命令(.reload -> ! analyze -v)来分析dump,然后可以定位到如下错误:
http://www.cnblogs.com/ahuo/archive/2010/05/19/1739053.html(此文转载,感谢作者的无私奉献)
3. 使用WinDbg调试程序:
待续...