我最近使用实现了堆栈跟踪日志记录boost 的新 stacktrace 库 https://www.boost.org/doc/libs/develop/doc/html/stacktrace/getting_started.html:
int debugErrorCallback(int status, const char* func_name, const char* err_msg, const char* file_name, int line, void* userdata)
{
boost::stacktrace::stacktrace stacktrace(4, 10); //skipped 4 frames include cv::error, this function and 2 in boost::stacktrace ctor
std::cout << boost::stacktrace::detail::to_string(stacktrace.as_vector().data(),
stacktrace.size()) << std::endl;
}
在我的开发机器上进行了测试,结果完美:
0# cv::arithm_op at d:\src\opencv_24\modules\core\src\arithm.cpp:1293
1# cv::addWeighted at d:\src\opencv_24\modules\core\src\arithm.cpp:2127
2# MyApplication::myFunction at d:\src\path\to\my\file.cpp:226
3# MyApplication::myOtherFunction at d:\src\path\to\my\other_file.cpp:146
...
我将新的应用程序版本部署到生产环境中。不幸的是,在生产机器上,我的模块中的堆栈帧仅解析为模块名称:
0# cv::addWeighted in opencv_core2413
1# cv::addWeighted in opencv_core2413
2# 0x00007FF7D0A0B56B in MyApplication
3# 0x00007FF7D0A0B2ED in MyApplication
...
我已经调试了boost的实现stacktrace
发现堆栈函数的地址是用RtlCaptureStackBackTrace https://learn.microsoft.com/en-us/windows-hardware/drivers/ddi/content/ntifs/nf-ntifs-rtlcapturestackbacktrace,这似乎工作正常。然后在中检索函数名称boost::stacktrace::detail::to_string
-> get_name_impl
函数在frame_msvc.ipp文件中,内部使用IDebugSymbols https://learn.microsoft.com/en-us/windows-hardware/drivers/ddi/content/dbgeng/nn-dbgeng-idebugsymbols来自 Dbgeng 库的对象。这个接口声明了一个函数IDebugSymbols::GetNameByOffset https://learn.microsoft.com/en-us/windows-hardware/drivers/ddi/content/dbgeng/nf-dbgeng-idebugsymbols-getnamebyoffset它应该以以下格式检索函数名称:Module!function
。不幸的是,它仅在我的开发环境中执行此操作,而仅在生产系统上返回模块名称。尽管已将 MyApplication.pdb 文件与应用程序一起部署,但由于某种原因,它似乎无法检索符号数据。
正如您可能已经注意到的,函数名称(没有源文件和行号,我可以没有它们)是为静态链接的 opencv 库检索的,但不是为我自己的源检索的。
我找不到此实施工作的要求是什么。有谁知道如何使其工作而不管运行时系统如何?
EDIT2:
事实证明,问题的根源实际上是无法加载 .pdb 文件。因为某些原因IDebugClient
, IDebugControl
or IDebugSymbols
,无论哪个应该加载符号文件(在初始化期间按顺序创建debugging_symbols
用于检索符号的类boost::stacktrace::detail::to_string
),仅在可执行文件头的“调试器目录”部分中写入的位置中查找它。这是 VS 在链接期间创建的路径(我通过将文件复制到远程系统上的同一路径来测试它,然后它才起作用)。
有没有办法让它加载可执行文件本地目录中的 .pdb 文件?或者也许可以在可执行头文件中保存符号文件的相对路径?
编辑:
添加MCVE:
#include "boost/stacktrace.hpp"
int main()
{
boost::stacktrace::stacktrace stacktrace;
std::cout << boost::stacktrace::detail::to_string(stacktrace.as_vector().data(), stacktrace.size()) << std::endl;
return 0;
}
和链接器选项:
/OUT:"path\x64\Release\MiscTest.exe" /MANIFEST /LTCG:incremental /NXCOMPAT /PDB:"path\x64\Release\MiscTest.pdb" /DYNAMICBASE "kernel32.lib" "user32.lib" "gdi32.lib" "winspool.lib" "comdlg32.lib" "advapi32.lib" "shell32.lib" "ole32.lib" "oleaut32.lib" "uuid.lib" "odbc32.lib" "odbccp32.lib" /DEBUG:FULL /MACHINE:X64 /OPT:REF /INCREMENTAL:NO /PGD:"path\x64\Release\MiscTest.pgd" /SUBSYSTEM:CONSOLE /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /ManifestFile:"x64\Release\MiscTest.exe.intermediate.manifest" /OPT:ICF /ERRORREPORT:PROMPT /NOLOGO /TLBID:1