我开始学习Windbg,并发现了这篇好文章:如何使用WinDbg分析VC++应用程序的崩溃转储?
现在,我想按照说明一步一步地操作。问题是:我需要编写一些可以立即崩溃并创建可供Windbg使用的转储文件的示例代码。
如何编写这样的代码?
void Example4()
{
int* i = NULL;
*i = 80;
}
以上代码会立即崩溃;但我不知道在哪里找到转储文件?
谢谢
我开始学习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;
}
*((int*) 0) = 0;
这将产生整数除以零:int a = 0; int b = 5 / a;
编辑:使用Minidumps和Visual Studio .NET对应用程序进行死后调试包含大量关于使用minidumps的示例代码和理论。如果您想查看崩溃转储,您需要创建一个。请参见Heisenbug: WinApi program crashes on some computers。虽然您可能能够在不经过WinQual的情况下获取用于WER发送的崩溃转储,但这有点混乱(基本上您可以在发送之前从临时位置复制它,具体细节取决于您的操作系统),我建议使用Win API MiniDump提供的函数创建自己的崩溃转储。所有所需的代码都可以在The CodeProject page mentioned in the linked answer找到。
自动 minidump 生成是由 post-mortem 调试器完成的,因此您需要从那里开始。最重要的是,它是由调试器完成的。因此,如果您只想生成 minidump,则可以使用您典型的调试器(可能是 Visual Studio 或 Windbg)。即使任务管理器也可以创建转储文件。
指定 post-mortem 调试器的注册表设置为 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AeDebug
查看 Debugger
字符串,您就可以找到您的 minidumps。
可以通过编程或程序错误调试工具创建转储文件。在第一种情况下,您可以使用MiniDumpWriteDump函数,在第二种情况下,您可以使用Dr. Watson(对于XP:请查看此description和这个非常详细的video;对于Vista,请查看here)。
我之前在测试WinDbg时使用了下面的代码。
#include "stdafx.h"
#include "stdio.h"
#include "malloc.h"
void Function2(int * ptr2)
{
for(int i=0; i < (2 * 1024); i++)
{
*ptr2++ = 0xCAFECAFE;
}
}
void Function1()
{
int * ptr1 = (int *)malloc(1024 * sizeof(int));
Function2(ptr1);
}
int _tmain(int argc, _TCHAR* argv[])
{
printf("Press enter to allocate and corrupt.\r\n");
getc(stdin);
printf("Allocating and corrupting...\r\n");
Function1();
printf("Done. Press enter to exit process.\r\n");
getc(stdin);
return 0;
}
试试这个:
int main()
{
int v[5];
printf("%d", v[10]);
return 0;
}
或者访问一个随机的内存位置。