如何编写一个会崩溃并产生转储文件的示例代码? [英] How to write a sample code that will crash and produce dump file?

查看:149
本文介绍了如何编写一个会崩溃并产生转储文件的示例代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始学习windbg,我发现这个好的帖子
如何使用WinDbg分析VC ++应用程序的崩溃转储

现在我想按照说明进行操作步步。这里是问题:我需要写一些示例代码,可以立即崩溃,并创建一些可以由windbg使用的转储文件。



如何写这样的代码?

  void Example4()
{
int * i = NULL;
* i = 80;
}

上述代码会立即崩溃;



谢谢

解决方案

  #include< Windows.h> 
#include< Dbghelp.h>

void make_minidump(EXCEPTION_POINTERS * e)
{
auto hDbgHelp = LoadLibraryA(dbghelp);
if(hDbgHelp == nullptr)
return;
auto pMiniDumpWriteDump =(decltype(& MiniDumpWriteDump))GetProcAddress(hDbgHelp,MiniDumpWriteDump);
if(pMiniDumpWriteDump == nullptr)
return;

char name [MAX_PATH];
{
auto nameEnd = name + GetModuleFileNameA(GetModuleHandleA(0),name,MAX_PATH);
SYSTEMTIME t;
GetSystemTime(& t);
wsprintfA(nameEnd - strlen(。exe),
_%4d%02d%02d_%02d%02d%02d.dmp,
t.wYear,t.wMonth, t.wDay,t.wHour,t.wMinute,t.wSecond);
}

auto hFile = CreateFileA(name,GENERIC_WRITE,FILE_SHARE_READ,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
if(hFile == INVALID_HANDLE_VALUE)
return;

MINIDUMP_EXCEPTION_INFORMATION exceptionInfo;
exceptionInfo.ThreadId = GetCurrentThreadId();
exceptionInfo.ExceptionPointers = e;
exceptionInfo.ClientPointers = FALSE;

auto dumped = pMiniDumpWriteDump(
GetCurrentProcess(),
GetCurrentProcessId(),
hFile,
MINIDUMP_TYPE(MiniDumpWithIndirectlyReferencedMemory | MiniDumpScanMemory),
e ?& exceptionInfo:nullptr,
nullptr,
nullptr);

CloseHandle(hFile);

return;
}

LONG CALLBACK unhandled_handler(EXCEPTION_POINTERS * e)
{
make_minidump(e);
return EXCEPTION_CONTINUE_SEARCH;
}

int main()
{
SetUnhandledExceptionFilter(unhandled_handler);

return *(int *)0;
}


I started learned windbg and I found this good post How to use WinDbg to analyze the crash dump for VC++ application

Now I want to follow the instructions and do it step by step. Here is the problem: I need to write some sample code that can immediately crash, and create some dump files that can be used by windbg.

How to write such code?

void Example4()
{
    int* i = NULL;
    *i = 80;
}

The above code will crash immediately; however, I don't know where to find the dump file?

Thank you

解决方案

#include <Windows.h>
#include <Dbghelp.h>

void make_minidump(EXCEPTION_POINTERS* e)
{
    auto hDbgHelp = LoadLibraryA("dbghelp");
    if(hDbgHelp == nullptr)
        return;
    auto pMiniDumpWriteDump = (decltype(&MiniDumpWriteDump))GetProcAddress(hDbgHelp, "MiniDumpWriteDump");
    if(pMiniDumpWriteDump == nullptr)
        return;

    char name[MAX_PATH];
    {
        auto nameEnd = name + GetModuleFileNameA(GetModuleHandleA(0), name, MAX_PATH);
        SYSTEMTIME t;
        GetSystemTime(&t);
        wsprintfA(nameEnd - strlen(".exe"),
            "_%4d%02d%02d_%02d%02d%02d.dmp",
            t.wYear, t.wMonth, t.wDay, t.wHour, t.wMinute, t.wSecond);
    }

    auto hFile = CreateFileA(name, GENERIC_WRITE, FILE_SHARE_READ, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
    if(hFile == INVALID_HANDLE_VALUE)
        return;

    MINIDUMP_EXCEPTION_INFORMATION exceptionInfo;
    exceptionInfo.ThreadId = GetCurrentThreadId();
    exceptionInfo.ExceptionPointers = e;
    exceptionInfo.ClientPointers = FALSE;

    auto dumped = pMiniDumpWriteDump(
        GetCurrentProcess(),
        GetCurrentProcessId(),
        hFile,
        MINIDUMP_TYPE(MiniDumpWithIndirectlyReferencedMemory | MiniDumpScanMemory),
        e ? &exceptionInfo : nullptr,
        nullptr,
        nullptr);

    CloseHandle(hFile);

    return;
}

LONG CALLBACK unhandled_handler(EXCEPTION_POINTERS* e)
{
    make_minidump(e);
    return EXCEPTION_CONTINUE_SEARCH;
}

int main()
{
    SetUnhandledExceptionFilter(unhandled_handler);

    return *(int*)0;
}

这篇关于如何编写一个会崩溃并产生转储文件的示例代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆