Debug Dump file

dump file is a snapshot of the processs memeory. to debug it, we need use its corresponding executive to help restore the scenario
pdb and source file are also needed to help analyze it.

create dump file

// header file, to generate a dll that will cause crash
#pragma once
#ifndef _DLL_TUTORIAL_H_
#define _DLL_TUTORIAL_H_
#include <iostream>

#if defined DLL_EXPORT
#define DECLDIR __declspec(dllexport)
#else
#define DECLDIR __declspec(dllimport)
#endif

extern "C"
{
    DECLDIR int crash(int a, int b);
    DECLDIR void Function(void);
}

#endif

// source file

#define DLL_EXPORT
#include <iostream>
#include "Header.h"

void func2(int a)
{
    int * p = NULL;
    *p = a;
}

void func1(int a)
{
    func2(a);
}

extern "C"
{

    DECLDIR int crash(int a, int b)
    {
        int b1 = a * b;
        func1(b1);
        return 1;
    }

    DECLDIR void Function(void)
    {
        std::cout << "DLL Called!" << std::endl;
    }

}
// header file. call functions from above dll and create dump file
#pragma once
#ifndef _DLL_TUTORIAL_H_
#define _DLL_TUTORIAL_H_
#include <iostream>

#if defined DLL_EXPORT
#define DECLDIR __declspec(dllexport)
#else
#define DECLDIR __declspec(dllimport)
#endif

extern "C"
{
    DECLDIR int crash(int a, int b);
    DECLDIR void Function(void);
}

#endif

// source file
#include"Header.h"
#include <Windows.h>
#include <DbgHelp.h>

#pragma comment(lib, "DbgHelp.lib")
#pragma comment(lib,"dlls.lib")

void call_func(int d)
{
    int a = 7;
    crash(a, d);
}


LONG WINAPI MyUnhandledExceptionFilter(_In_ struct _EXCEPTION_POINTERS *ExceptionInfo);
void MyDumpGenerate();

void MyDumpGenerate()
{
    SetUnhandledExceptionFilter(MyUnhandledExceptionFilter);
}

LONG WINAPI MyUnhandledExceptionFilter(_In_ struct _EXCEPTION_POINTERS *ExceptionInfo)
{
    MessageBox(0, "DumpGenerate", 0, 0);

    HANDLE lhDumpFile = CreateFile("C:\\data\\test.dmp", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

    MINIDUMP_EXCEPTION_INFORMATION loExceptionInfo;
    loExceptionInfo.ExceptionPointers = ExceptionInfo;
    loExceptionInfo.ThreadId = GetCurrentThreadId();
    loExceptionInfo.ClientPointers = TRUE;
    MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), lhDumpFile, MiniDumpNormal, &loExceptionInfo, NULL, NULL);

    CloseHandle(lhDumpFile);

    return EXCEPTION_EXECUTE_HANDLER;
}


int main()
{
    MyDumpGenerate();
    int a = 6;
    call_func(a);
    return 1;
} // run this binary from command line to create dump file

Debug dump file

Visual Studio
Set symbol path : https://docs.microsoft.com/en-us/windows-hardware/drivers/debugger/setting-symbol-and-source-paths-in-visual-studio
Crash dump analysis using the Windows debuggers (WinDbg) : https://docs.microsoft.com/en-us/windows-hardware/drivers/debugger/crash-dump-files

使用VS2012调试Dump文件
http://blog.csdn.net/tojohnonly/article/details/72864694

windbg commands
.help show all internal commands
!help show all external commands
.hh open chm file
sympath //search path
symfix //fixed symbol path
reload /i xx.dll ignore versrion mis

 

posted @ 2018-03-12 15:04  HEIS老妖  阅读(892)  评论(13编辑  收藏  举报