利用SetUnhandledExceptionFilter擷取程式異常時的DUMP檔案

來源:互聯網
上載者:User

利用SetUnhandledExceptionFilter實現程式異常時擷取DUMP檔案來剖析器異常原因,現在很多的軟體都是通過這樣的方法來收集異常,只不過這裡少了一步向伺服器傳輸我們的程式產生的DUMP檔案,這種DUMP檔案產生後其實會通過網路向指定的伺服器傳輸。這樣方便開發都收集程式目前存在的BUG。

下面是一控制台程式,直接COPY代碼,編譯運行,我這裡用了兩種方法使程式異常崩潰,從而產生DUMP檔案,對於產生的DUMP檔案我們可以使用WinDBG來進行分析。

#include <Windows.h>

#include <dbghelp.h>

#pragma comment(lib, "dbghelp.lib")

LONG MyUnhandledExceptionFilter(_EXCEPTION_POINTERS *ExceptionInfo)
{
CHAR strDumpFile[MAX_PATH] = { 0 };
SYSTEMTIME tm;

HANDLE hFile = NULL;

GetSystemTime(&tm);
sprintf(strDumpFile, "%04d%02d%02d%02d%02d%02d.dmp", tm.wYear, tm.wMonth, tm.wDay, tm.wHour, tm.wMinute, tm.wSecond);

hFile = CreateFileA(strDumpFile, 
GENERIC_WRITE, 
FILE_SHARE_WRITE,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
if(hFile != INVALID_HANDLE_VALUE)
{
MINIDUMP_EXCEPTION_INFORMATION ExInfo;
ExInfo.ThreadId = GetCurrentThreadId();
ExInfo.ExceptionPointers = ExceptionInfo;
ExInfo.ClientPointers = NULL;
BOOL bOK = MiniDumpWriteDump(
GetCurrentProcess(),
GetCurrentProcessId(),
hFile,
MiniDumpNormal,
&ExInfo,
NULL,
NULL);

CloseHandle(hFile);
}

return EXCEPTION_CONTINUE_SEARCH;
}

int _tmain(int argc, _TCHAR* argv[])
{

SetUnhandledExceptionFilter((LPTOP_LEVEL_EXCEPTION_FILTER)MyUnhandledExceptionFilter);

// 第一種產生異常的方法
//char *str = NULL;
//memcpy(str, "Hello", 5);


// 第二種產生異常的方法
int i = 10;
while(1)
{
printf("%d\n", 10/i);
i--;
}

return 0;
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.