dump文件生成与调试(VS2008)

  总结一下dump文件生成和调试的方法:  

  1:用SetUnhandledExceptionFilter捕获未处理的异常,包含头文件<windows.h>。函数原型为:

LPTOP_LEVEL_EXCEPTION_FILTER WINAPI SetUnhandledExceptionFilter(
    __in  LPTOP_LEVEL_EXCEPTION_FILTER lpTopLevelExceptionFilter
);

  SetUnhandledExceptionFilter返回值为:The SetUnhandledExceptionFilter function returns the address of the previous exception filter established with the function. A NULL return value means that there is no current top-level exception handler.返回回掉函数的地址。

  回掉函数原型为:

1 typedef LONG (WINAPI *PTOP_LEVEL_EXCEPTION_FILTER)(
2     __in struct _EXCEPTION_POINTERS *ExceptionInfo
3     );
4 typedef PTOP_LEVEL_EXCEPTION_FILTER LPTOP_LEVEL_EXCEPTION_FILTER;

  回掉函数须返回以下3种类型:

ValueMeaning

EXCEPTION_EXECUTE_HANDLER
0x1

Return from UnhandledExceptionFilter and execute the associated exception handler. This usually results in process termination.捕获到异常,并在异常处结束程序

EXCEPTION_CONTINUE_EXECUTION
0xffffffff

Return from UnhandledExceptionFilter and continue execution from the point of the exception. Note that the filter function is free to modify the continuation state by modifying the exception information supplied through its LPEXCEPTION_POINTERS parameter.表示错误已经被修复,从异常发生处继续执行。如果在回掉函数内部不对异常进行处理,每次回掉结束又会捕获到异常,将导致无限进入SetUnhandledExceptionFilter函数,死循环。

EXCEPTION_CONTINUE_SEARCH
0x0

Proceed with normal execution of UnhandledExceptionFilter. That means obeying the SetErrorMode flags, or invoking the Application Error pop-up message box.捕获到异常,并调用系统默认异常错误框,结束程序。

 

  2:在SetUnhandledExceptionFilter的回掉函数中,用MiniDumpWriteDump函数将异常写入dump文件。需要包含DbgHelp.h,引入#pragma comment(lib, "dbghelp.lib")。MiniDumpWriteDump函数的原型为:

1 BOOL WINAPI MiniDumpWriteDump(
2   __in          HANDLE hProcess,                       //  进程句柄,可以用GetCurrentProcess()获得
3   __in          DWORD ProcessId,                       //  进程ID,可以用GetCurrentProcessId()获得
4   __in          HANDLE hFile,                         //   待写入的dmp文件
5   __in          MINIDUMP_TYPE DumpType,                   //  写入的dump信息类型,从MINIDUMP_TYPR中选择。
6   __in          PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam,     //  指向异常信息结构的指针,如果该值是NULL,将不会写入任何异常信息  
7   __in          PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam,   //   指向用户自定义信息块,可以为NULL
8   __in          PMINIDUMP_CALLBACK_INFORMATION CallbackParam      //   指向回掉函数的扩展信息块,可以为NULL
9 );

  在MiniDumpWriteDump参数项中,比较重要的是异常信息块PMINIDUMP_EXCEPTION_INFORMATION,其结构如下:

1 typedef struct _MINIDUMP_EXCEPTION_INFORMATION {
2     DWORD ThreadId;                 //线程ID,可以用GetCurrentThreadId()获得
3     PEXCEPTION_POINTERS ExceptionPointers;   //指向异常信息指针,EXCEPTION_POINTER内包含了异常信息代码/flag等内容,异常发生时各寄存器状态和内容。用回掉函数的参数赋值即可
4     BOOL ClientPointers;              //TRUE和FALSE好像都可以,搞不清楚区别
5 } MINIDUMP_EXCEPTION_INFORMATION, *PMINIDUMP_EXCEPTION_INFORMATION;

  3:了解上述内容后,封装成类,方便移植。h文件:

 1 #pragma once
 2 #include <string>
 3 using namespace std;
 4 class CCreateDump
 5 {
 6 public:
 7     CCreateDump();
 8     ~CCreateDump(void);
 9     static CCreateDump* Instance();
10     static long __stdcall UnhandleExceptionFilter(_EXCEPTION_POINTERS* ExceptionInfo);
11     //声明Dump文件,异常时会自动生成。会自动加入.dmp文件名后缀
12     void DeclarDumpFile(std::string dmpFileName = "");
13 private:
14     static std::string    strDumpFile; 
15     static CCreateDump*    __instance;
16 };

  cpp文件:

 1 #include "StdAfx.h"
 2 #include "CreateDump.h"
 3 #include <DbgHelp.h>
 4 #pragma comment(lib,  "dbghelp.lib")
 5 
 6 CCreateDump* CCreateDump::__instance = NULL;
 7 std::string CCreateDump::strDumpFile = "";
 8 
 9 CCreateDump::CCreateDump()
10 {
11 }
12 
13 CCreateDump::~CCreateDump(void)
14 {
15     
16 }
17 
18 long  CCreateDump::UnhandleExceptionFilter(_EXCEPTION_POINTERS* ExceptionInfo)
19 {
20     HANDLE hFile   =   CreateFile(strDumpFile.c_str(),   GENERIC_WRITE,   FILE_SHARE_WRITE,   NULL,   CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,   NULL   );
21     if(hFile!=INVALID_HANDLE_VALUE)
22     {
23         MINIDUMP_EXCEPTION_INFORMATION   ExInfo; 
24         ExInfo.ThreadId   =   ::GetCurrentThreadId();
25         ExInfo.ExceptionPointers   =   ExceptionInfo;
26         ExInfo.ClientPointers   =   FALSE;
27         //   write   the   dump
28         BOOL   bOK   =   MiniDumpWriteDump(GetCurrentProcess(),   GetCurrentProcessId(),   hFile,   MiniDumpNormal,  &ExInfo,   NULL,   NULL   );
29         CloseHandle(hFile);
30         if (!bOK)
31         {
32             DWORD dw = GetLastError();
33             //写dump文件出错处理,异常交给windows处理
34             return EXCEPTION_CONTINUE_SEARCH;
35         }
36         else
37         {    //在异常处结束
38             return EXCEPTION_EXECUTE_HANDLER;
39         }
40     }
42     else
43     {
44         return EXCEPTION_CONTINUE_SEARCH;
45     }
46 }
47 
48 void CCreateDump::DeclarDumpFile(std::string dmpFileName)
49 {
50     SYSTEMTIME syt;
51     GetLocalTime(&syt);
52     char c[MAX_PATH];
53     sprintf_s(c,MAX_PATH,"[%04d-%02d-%02d %02d:%02d:%02d]",syt.wYear,syt.wMonth,syt.wDay,syt.wHour,syt.wMinute,syt.wSecond);
54     strDumpFile = std::string(c);
55     if (!dmpFileName.empty())
56     {
57         strDumpFile += dmpFileName;
58     }
59     strDumpFile += std::string(".dmp");
60     SetUnhandledExceptionFilter(UnhandleExceptionFilter);
61 }
62 
63 CCreateDump* CCreateDump::Instance()
64 {
65     if (__instance == NULL)
66     {
67         __instance = new CCreateDump;
68     }
69     return __instance;
70 }

  调用方法:加入头文件引用,在程序开始时写入

CCreateDump::Instance()->DeclarDumpFile("dumpfile");

  4:工程属性设置,VS2008工程属性->linker->debugging->Generate Debug Info选择yes,生成pdb文件。

  5:用vs2008打开dump文件,debug即可。

 

posted on 2016-08-29 16:03  炽离  阅读(5452)  评论(1编辑  收藏  举报

导航