我需要通过windbg库获取调试过程的调用堆栈跟踪。因此,也欢迎有理论帮助的答案。感谢您的帮助!
注0:我认为以这种形式可以更容易理解这个问题:如何将堆栈解析为框架以获取函数调用和参数?
注:例如,我可以在某个过程中通过断点获取ESP寄存器值,但是如何解析呢?还是有其他方法?
注2:那里存在类似的问题:How do I determine detailed call-stack information in C++?
[如果您的意思是使用dbgeng,请参见下面的原型。您可能需要进行调整,以便在我从更大的项目(NetExt)中获取它时进行编译。
#include <dbgeng.h>
IDebugClient *Client;
PDEBUG_CONTROL5 Control;
PDEBUG_SYMBOLS5 symbol;
HRESULT Hr;
Hr = S_OK;
if ((Hr = DebugCreate(__uuidof(IDebugClient),
(void **)&Client)) != S_OK)
{
return Hr;
}
if ((Hr = Client->QueryInterface(__uuidof(IDebugControl5),
(void **)&Control)) == S_OK)
{
DEBUG_STACK_FRAME_EX listFrames[100] = {0};
UINT total = 0;
Client->QueryInterface(__uuidof(IDebugSymbols5),
(void **)&symbol)
if (Control->GetStackTraceEx(0, 0, 0, &listFrames, 100, &total) == S_OK)
{
for (UINT i=0; i < total; i++)
{
printf("%i\n", i);
printf("\tfInstructionOffset = %p\n", listFrames[i].InstructionOffset);
printf("\tfReturnOffset = %p\n", listFrames[i].ReturnOffset);
printf("\tFrameOffset = %p\n", listFrames[i].FrameOffset);
printf("\tInlineFrameContext = %p\n", listFrames[i].InlineFrameContext);
std::string strSymbol('\0', 100);
UINT size, displ = 0;
if (symbol->GetNameByOffset(listFrames[i].InstructionOffset, const_cast<char*>(strSymbol.c_str()), 100, &size, &displ) == S_OK)
{
strSymbol.resize(size);
}
printf("\tFunction = %s\n", strSymbol.c_str());
}
}
}