如何获取崩溃的 DLL 的堆栈跟踪?
How to get the stacktrace of a DLL who crashes?
我为应用程序开发了一些 DLL,我想知道是否可以在我的 DLL 崩溃时获取堆栈跟踪(在日志文件中),然后不对主程序的代码进行任何修改申请。
这是一个 DLL 的虚拟示例,我想在它崩溃时记录堆栈跟踪:
HelloDLL.h
#pragma once
//more about this in reference 1
#ifdef DLLDIR_EX
#define DLLDIR __declspec(dllexport) // export DLL information
#else
#define DLLDIR __declspec(dllimport) // import DLL information
#endif
class DLLDIR HelloDLL
{
public:
HelloDLL(void);
~HelloDLL(void);
void crash();
private:
void danger();
};
HelloDLL.cpp
#include "stdafx.h"
#include "HelloDLL.h"
#include <iostream>
using namespace std;
HelloDLL::HelloDLL(void)
{
}
HelloDLL::~HelloDLL(void)
{
}
void HelloDLL::crash()
{
danger();
}
void HelloDLL::danger()
{
abort();
}
我无法更改的应用程序:
#include "stdafx.h"
#include "HelloDLL.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
HelloDLL helloDLL;
helloDLL.crash();
getchar();
return 0;
}
我用 this website 创建了这个例子。
换句话说,当我的DLL崩溃时,如何从我的DLL中获取最多的信息以便于调试过程?
如评论中所述,获取崩溃进程堆栈跟踪的最佳方法是生成转储文件。为此,您首先需要编辑 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\Windows Error Reporting\LocalDumps
注册表项。这是获取 Hello.exe
应用程序完整转储的示例:
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\Windows Error Reporting\LocalDumps\Hello.exe]
"DumpCount"=dword:a
"DumpType"=dword:2
可以找到有关可用值的更多信息here。默认情况下,转储文件将存储在 %LOCALAPPDATA%\CrashDumps
.
然后,您需要在没有任何优化选项的情况下编译您的应用程序,并且您必须生成 .pdb
文件(使用 /DEBUG
选项)。
您现在可以 运行 您的程序并在 CrashDumps
目录中转储文件。要阅读它,您可以使用 VisualStudio(但也可以使用其他应用程序,例如 WinDbg):
- 用VisualStudio打开
.dmp
文件(可以将文件拖放到打开的解决方案中)
- 将
.pdb
文件添加到 configuration of VisualStudio(工具 > 选项 > 调试 > 符号)
- 点击"Run with native only"
您现在可以在发生崩溃时获取所有线程的堆栈跟踪,还可以在程序停止之前读取变量的值。
我为应用程序开发了一些 DLL,我想知道是否可以在我的 DLL 崩溃时获取堆栈跟踪(在日志文件中),然后不对主程序的代码进行任何修改申请。
这是一个 DLL 的虚拟示例,我想在它崩溃时记录堆栈跟踪:
HelloDLL.h
#pragma once
//more about this in reference 1
#ifdef DLLDIR_EX
#define DLLDIR __declspec(dllexport) // export DLL information
#else
#define DLLDIR __declspec(dllimport) // import DLL information
#endif
class DLLDIR HelloDLL
{
public:
HelloDLL(void);
~HelloDLL(void);
void crash();
private:
void danger();
};
HelloDLL.cpp
#include "stdafx.h"
#include "HelloDLL.h"
#include <iostream>
using namespace std;
HelloDLL::HelloDLL(void)
{
}
HelloDLL::~HelloDLL(void)
{
}
void HelloDLL::crash()
{
danger();
}
void HelloDLL::danger()
{
abort();
}
我无法更改的应用程序:
#include "stdafx.h"
#include "HelloDLL.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
HelloDLL helloDLL;
helloDLL.crash();
getchar();
return 0;
}
我用 this website 创建了这个例子。
换句话说,当我的DLL崩溃时,如何从我的DLL中获取最多的信息以便于调试过程?
如评论中所述,获取崩溃进程堆栈跟踪的最佳方法是生成转储文件。为此,您首先需要编辑 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\Windows Error Reporting\LocalDumps
注册表项。这是获取 Hello.exe
应用程序完整转储的示例:
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\Windows Error Reporting\LocalDumps\Hello.exe]
"DumpCount"=dword:a
"DumpType"=dword:2
可以找到有关可用值的更多信息here。默认情况下,转储文件将存储在 %LOCALAPPDATA%\CrashDumps
.
然后,您需要在没有任何优化选项的情况下编译您的应用程序,并且您必须生成 .pdb
文件(使用 /DEBUG
选项)。
您现在可以 运行 您的程序并在 CrashDumps
目录中转储文件。要阅读它,您可以使用 VisualStudio(但也可以使用其他应用程序,例如 WinDbg):
- 用VisualStudio打开
.dmp
文件(可以将文件拖放到打开的解决方案中) - 将
.pdb
文件添加到 configuration of VisualStudio(工具 > 选项 > 调试 > 符号) - 点击"Run with native only"
您现在可以在发生崩溃时获取所有线程的堆栈跟踪,还可以在程序停止之前读取变量的值。