我正在尝试使用以下代码捕获异常的堆栈跟踪:
#include "stdafx.h"
#include <process.h>
#include <iostream>
#include <Windows.h>
#include "dbghelp.h"
using namespace std;
#define TRACE_MAX_FUNCTION_NAME_LENGTH 1024
void function2()
{
int a = 0;
int b = 0;
throw new exception;
}
void function1()
{
int a = 0;
function2();
}
void function0()
{
function1();
}
LONG WINAPI FatalExceptionFilter(EXCEPTION_POINTERS* exception, DWORD exceptionCode)
{
CONTEXT *context = exception->ContextRecord;
HANDLE thread = GetCurrentThread();
HANDLE process = GetCurrentProcess();
STACKFRAME64 frame;
DWORD image;
#ifdef _M_IX86
image = IMAGE_FILE_MACHINE_I386;
frame.AddrPC.Offset = context->Eip;
frame.AddrPC.Mode = AddrModeFlat;
frame.AddrFrame.Offset = context->Ebp;
frame.AddrFrame.Mode = AddrModeFlat;
frame.AddrStack.Offset = context->Esp;
frame.AddrStack.Mode = AddrModeFlat;
#elif _M_X64
image = IMAGE_FILE_MACHINE_AMD64;
frame.AddrPC.Offset = context->Rip;
frame.AddrPC.Mode = AddrModeFlat;
frame.AddrFrame.Offset = context->Rsp;
frame.AddrFrame.Mode = AddrModeFlat;
frame.AddrStack.Offset = context->Rsp;
frame.AddrStack.Mode = AddrModeFlat;
#elif _M_IA64
image = IMAGE_FILE_MACHINE_IA64;
frame.AddrPC.Offset = context->StIIP;
frame.AddrPC.Mode = AddrModeFlat;
frame.AddrFrame.Offset = context->IntSp;
frame.AddrFrame.Mode = AddrModeFlat;
frame.AddrBStore.Offset = context->RsBSP;
frame.AddrBStore.Mode = AddrModeFlat;
frame.AddrStack.Offset = context->IntSp;
frame.AddrStack.Mode = AddrModeFlat;
#else
#error "This platform is not supported."
#endif
IMAGEHLP_SYMBOL64 *symbol = (IMAGEHLP_SYMBOL64 *)malloc(sizeof(IMAGEHLP_SYMBOL64)+(TRACE_MAX_FUNCTION_NAME_LENGTH - 1) * sizeof(TCHAR));
symbol->MaxNameLength = TRACE_MAX_FUNCTION_NAME_LENGTH;
symbol->SizeOfStruct = sizeof(IMAGEHLP_SYMBOL64);
IMAGEHLP_LINE64 *line = (IMAGEHLP_LINE64 *)malloc(sizeof(IMAGEHLP_LINE64));
line->SizeOfStruct = sizeof(IMAGEHLP_LINE64);
while (StackWalk64(image, process, thread, &frame, context, NULL, NULL, NULL, NULL))
{
DWORD64 displacement64;
if (SymGetSymFromAddr64(process, frame.AddrPC.Offset, &displacement64, symbol))
{
DWORD displacement;
if (SymGetLineFromAddr64(process, frame.AddrPC.Offset, &displacement, line))
{
printf("\tat %s in %s: line: %lu: address: 0x%0X\n", symbol->Name, line->FileName, line->LineNumber, symbol->Address);
}
else
{
printf("Error from SymGetLineFromAddr64: %lu.\n", GetLastError());
}
}
else
{
printf("Error from SymGetSymFromAddr64: %lu.\n", GetLastError());
}
}
return EXCEPTION_EXECUTE_HANDLER;
}
static void threadFunction(void *param)
{
__try
{
function0();
}
__except (FatalExceptionFilter(GetExceptionInformation(), GetExceptionCode()))
{ }
}
int _tmain(int argc, _TCHAR* argv[])
{
_beginthread(threadFunction, 0, NULL);
printf("Press any key to exit.\n");
cin.get();
return 0;
}
输出:
Press any key to exit.
Error from SymGetSymFromAddr64: 126.
Error from SymGetSymFromAddr64: 126.
Error from SymGetSymFromAddr64: 126.
Error from SymGetSymFromAddr64: 126.
Error from SymGetSymFromAddr64: 126.
Error from SymGetSymFromAddr64: 126.
Error from SymGetSymFromAddr64: 126.
Error from SymGetSymFromAddr64: 126.
Error from SymGetSymFromAddr64: 126.
Error from SymGetSymFromAddr64: 126.
我对SymGetSymFromAddr64失败的原因感到有些困惑。有什么想法吗?
126应该是一个缺少的依赖项,但我在相应的文件夹中有DLL ...我在使用Win32平台Debug构建的Windows 8.1 x64 PC上的MSVC ++控制台应用程序中运行这一切。那么可能导致问题的原因是什么?
更新:依赖步行者告诉我它无法找到API-MS-WIN-SERVICE-PRIVATE-L1-1-1.DLL
答案 0 :(得分:2)
您的代码存在同样的问题。
修正了:
HANDLE process = GetCurrentProcess();
SymInitialize( process,NULL,TRUE );
注意:现在,SymGetLineFromAddr64
无法使用某个大地址,错误487(ERROR_INVALID_ADDRESS
)
使用以下内容进行追踪:
printf("Error from SymGetLineFromAddr64: %lu for address 0x%I64x.\n",
GetLastError(),
frame.AddrPC.Offset);
编辑:当我在Visual Studio中使用Go to Disassembly并输入使SymGetLineFromAddr64
失败的第一个地址(此处为0x000000005a03da08
)时,它显示了源代码:
F:\ DD \ vctools \ crt_bld \ self_x86 \ CRT \预生成\诶\ throw.cpp