如何在程序中生成崩潰轉儲dump文件以及如何分析dump

關於更詳細的內容,如果有興趣,可以訪問下面的視頻

https://edu.csdn.net/course/detail/28915

 

程序崩潰的時候如何生成dump文件

話不多說,直接上代碼,下面的程序會崩潰,而且會在崩潰的時候在運行目錄生成dump文件。


#include "stdafx.h"
#include <Windows.h>
#include <Dbghelp.h>
#include <stdio.h>

LONG WINAPI handle_exception(LPEXCEPTION_POINTERS lpExceptionInfo);
void throw_exception_test()
{
    throw 123;
}
void catch_test()
{
    try
    {
        throw_exception_test();
    }
    catch(...)
    {
        printf("catch exception\n");
    }
}
void exception_test()
{
    try
    {
        int x=0;
        int y = 10 / x;
    }
    catch(...)
    {
        printf("exception occurred\n");
    }

}
void crash_test()
{
    try
    {
        char* test = NULL;
        strcpy(test,"123");
    }
    catch(...)
    {
        printf("exception\n");
    }
}
int main(int argc, char *argv[])
{
    SetUnhandledExceptionFilter(handle_exception);
    //exception_test();
    crash_test();
    catch_test();
    printf("quit\n");

    return 0;
}
 
int GenerateMiniDump(PEXCEPTION_POINTERS exception_pointers)
{
    TCHAR file_name[MAX_PATH] = {0};    
    SYSTEMTIME local_time;
    GetLocalTime(&local_time);
    sprintf(file_name,"section_9_crash-%04d%02d%02d-%02d%02d%02d.dmp",local_time.wYear, local_time.wMonth, local_time.wDay,
        local_time.wHour, local_time.wMinute, local_time.wSecond);
    HANDLE h_dump_file = CreateFile(file_name, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_WRITE | FILE_SHARE_READ, 0, CREATE_ALWAYS, 0, 0);
    if (INVALID_HANDLE_VALUE == h_dump_file)
    {
        return EXCEPTION_CONTINUE_EXECUTION;
    }
    MINIDUMP_EXCEPTION_INFORMATION exception_information;
    exception_information.ThreadId = GetCurrentThreadId();
    exception_information.ExceptionPointers = exception_pointers;
    exception_information.ClientPointers = FALSE;
    //MiniDumpNormal
    //MiniDumpWithDataSegs
    //MiniDumpWithFullMemory
    MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(),
        h_dump_file, MiniDumpWithFullMemory, (exception_pointers ? &exception_information : NULL), NULL, NULL);
    CloseHandle(h_dump_file);
    return EXCEPTION_EXECUTE_HANDLER;
}
 
LONG WINAPI handle_exception(LPEXCEPTION_POINTERS lpExceptionInfo)
{
    return GenerateMiniDump(lpExceptionInfo);
}

如何調試dump文件來定位崩潰

可以用Windbg,也可以用vc,用vc打開dump文件,如圖所示

然後定位bug,如圖所示:

 

 

關於更詳細的內容,如果有興趣,可以訪問下面的視頻

https://edu.csdn.net/course/detail/28915

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章