获取使用G++的应用程序上的stracktrace。在窗户上
我正在使用G ++/Mingw在Windows上开发一个应用程序。当我的应用程序崩溃时,我想记录StackTrace。我知道我不能使用“ SymetSymfromAddr”功能来检索方法名称,因为它仅适用于使用Visual Studio(PDB格式)构建的可执行文件。 因此,我的想法是仅打印“调试符号地址”,然后使用“ Addr2line”工具来检索方法名称 /行号。
这是打印stacktrace方法名称地址的代码:
LONG WINAPI signalHandler(EXCEPTION_POINTERS* exceptionInfo) {
HANDLE process = GetCurrentProcess();
HANDLE thread = GetCurrentThread();
CONTEXT* context = exceptionInfo->ContextRecord;
SymInitialize(process, nullptr, true);
STACKFRAME frame = {};
frame.AddrPC.Offset = context->Rip;
frame.AddrPC.Mode = AddrModeFlat;
frame.AddrStack.Offset = context->Rsp;
frame.AddrStack.Mode = AddrModeFlat;
frame.AddrFrame.Offset = context->Rbp;
frame.AddrFrame.Mode = AddrModeFlat;
std::stringstream ss;
while (StackWalk(IMAGE_FILE_MACHINE_AMD64, process, thread, &frame, context, nullptr, SymFunctionTableAccess, SymGetModuleBase, nullptr)) {
std::string moduleName = "[unknown]";
DWORD64 moduleBase = SymGetModuleBase(process, frame.AddrPC.Offset);
char moduleBuffer[MAX_PATH];
if (moduleBase && GetModuleFileNameA((HINSTANCE)moduleBase, moduleBuffer, MAX_PATH)) {
moduleName = moduleBuffer;
}
void* instructionShift = (char*)frame.AddrPC.Offset;
ss << "\t[bt]\t> " << "addr2line -f -e " << moduleName << " " << instructionShift << std::endl;
}
SymCleanup(process);
Logger::instance().logError(ss.str());
return EXCEPTION_EXECUTE_HANDLER;
}
这是我发生应用程序崩溃的结果的示例:
[bt] > addr2line -f -e C:\msys64\home\greg\project\myApp.exe 0x7ff611d13faf
[bt] > addr2line -f -e C:\msys64\home\greg\project\myApp.exe 0x7ff611d13763
[bt] > addr2line -f -e C:\msys64\home\greg\project\myApp.exe 0x7ff611daf325
[bt] > addr2line -f -e C:\WINDOWS\System32\USER32.dll 0x7ffde66f1c4c
[bt] > addr2line -f -e C:\WINDOWS\System32\USER32.dll 0x7ffde66f189e
[bt] > addr2line -f -e C:\WINDOWS\SYSTEM32\opengl32.dll 0x7ffd864e3554
[bt] > addr2line -f -e C:\WINDOWS\System32\USER32.dll 0x7ffde66f1c4c
[bt] > addr2line -f -e C:\WINDOWS\System32\USER32.dll 0x7ffde66f0ea6
[bt] > addr2line -f -e C:\msys64\home\greg\project\myApp.exe 0x7ff611db218f
[bt] > addr2line -f -e C:\msys64\home\greg\project\myApp.exe 0x7ff611d13a8f
[bt] > addr2line -f -e C:\msys64\home\greg\project\myApp.exe 0x7ff611d114c6
[bt] > addr2line -f -e C:\WINDOWS\System32\KERNEL32.DLL 0x7ffde57b54e0
[bt] > addr2line -f -e C:\WINDOWS\SYSTEM32\ntdll.dll 0x7ffde788485b
不幸的是,当我执行“ addr2line”命令时,我始终得到以下结果:
??
??:0
我很好地编译了我的应用程序使用调试选项(-g)。因此,为什么“ AddR2line”工具未返回方法名称?还是在使用G ++构建的应用程序上使用“ StackWalk”功能获得正确地址?
I'm developping an application on Windows using G++/MinGW. When my application crash, I would like to log the stacktrace. I understand that I cannot use "SymGetSymFromAddr" function to retrieve the method name because it works only on executable built with Visual Studio (PDB format).
Therefore, my idea is to print only the debug symbol addresses and then use "addr2line" tool to retrieve the method name / line number.
Here is the code to print the stacktrace method name addresses:
LONG WINAPI signalHandler(EXCEPTION_POINTERS* exceptionInfo) {
HANDLE process = GetCurrentProcess();
HANDLE thread = GetCurrentThread();
CONTEXT* context = exceptionInfo->ContextRecord;
SymInitialize(process, nullptr, true);
STACKFRAME frame = {};
frame.AddrPC.Offset = context->Rip;
frame.AddrPC.Mode = AddrModeFlat;
frame.AddrStack.Offset = context->Rsp;
frame.AddrStack.Mode = AddrModeFlat;
frame.AddrFrame.Offset = context->Rbp;
frame.AddrFrame.Mode = AddrModeFlat;
std::stringstream ss;
while (StackWalk(IMAGE_FILE_MACHINE_AMD64, process, thread, &frame, context, nullptr, SymFunctionTableAccess, SymGetModuleBase, nullptr)) {
std::string moduleName = "[unknown]";
DWORD64 moduleBase = SymGetModuleBase(process, frame.AddrPC.Offset);
char moduleBuffer[MAX_PATH];
if (moduleBase && GetModuleFileNameA((HINSTANCE)moduleBase, moduleBuffer, MAX_PATH)) {
moduleName = moduleBuffer;
}
void* instructionShift = (char*)frame.AddrPC.Offset;
ss << "\t[bt]\t> " << "addr2line -f -e " << moduleName << " " << instructionShift << std::endl;
}
SymCleanup(process);
Logger::instance().logError(ss.str());
return EXCEPTION_EXECUTE_HANDLER;
}
Here is an example of result when I've got an application crash:
[bt] > addr2line -f -e C:\msys64\home\greg\project\myApp.exe 0x7ff611d13faf
[bt] > addr2line -f -e C:\msys64\home\greg\project\myApp.exe 0x7ff611d13763
[bt] > addr2line -f -e C:\msys64\home\greg\project\myApp.exe 0x7ff611daf325
[bt] > addr2line -f -e C:\WINDOWS\System32\USER32.dll 0x7ffde66f1c4c
[bt] > addr2line -f -e C:\WINDOWS\System32\USER32.dll 0x7ffde66f189e
[bt] > addr2line -f -e C:\WINDOWS\SYSTEM32\opengl32.dll 0x7ffd864e3554
[bt] > addr2line -f -e C:\WINDOWS\System32\USER32.dll 0x7ffde66f1c4c
[bt] > addr2line -f -e C:\WINDOWS\System32\USER32.dll 0x7ffde66f0ea6
[bt] > addr2line -f -e C:\msys64\home\greg\project\myApp.exe 0x7ff611db218f
[bt] > addr2line -f -e C:\msys64\home\greg\project\myApp.exe 0x7ff611d13a8f
[bt] > addr2line -f -e C:\msys64\home\greg\project\myApp.exe 0x7ff611d114c6
[bt] > addr2line -f -e C:\WINDOWS\System32\KERNEL32.DLL 0x7ffde57b54e0
[bt] > addr2line -f -e C:\WINDOWS\SYSTEM32\ntdll.dll 0x7ffde788485b
Unfortunately, when I execute the "addr2line" command, I always get the following result:
??
??:0
I well compiled my application with debug option (-g). Therefore, why the method name is not returned by "addr2line" tool ? Or maybe it is not possible to get correct addresses with "StackWalk" function on application built with G++ ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论