hae*_*lix 5 c++ boost stack-trace debug-symbols
这是一个复杂的过程,因为它也取决于Boost版本和平台。
我正在使用boost stacktrace在某些断言失败的地方打印backtrace。有一些外部编译时和运行时dep,具体取决于您使用的是哪种模式(链接介绍了〜5个模式)。我更喜欢基于调试信息和导出信息的东西(我认为后者也可以在生产版本中使用)。但是,我可以得到既不默认模式或工作BOOST_STACKTRACE_USE_ADDR2LINE或BOOST_STACKTRACE_USE_BACKTRACE-所有3只显示在我的实际的程序代码的调用堆栈地址-看到一个谷歌测试测试堆栈跟踪如下:
0# 0x000055E47D43BDC2 in Debug/myprog
1# 0x000055E47D489055 in Debug/myprog
2# 0x000055E47D567FDF in Debug/myprog
3# 0x000055E47D560CDE in Debug/myprog
4# void testing::internal::HandleExceptionsInMethodIfSupported<testing::Test, void>(testing::Test*, void (testing::Test::*)(), char const*) in /usr/lib/libgtest.so.1.8.1
5# testing::Test::Run() in /usr/lib/libgtest.so.1.8.1
6# testing::TestInfo::Run() in /usr/lib/libgtest.so.1.8.1
7# testing::TestCase::Run() in /usr/lib/libgtest.so.1.8.1
8# testing::internal::UnitTestImpl::RunAllTests() in /usr/lib/libgtest.so.1.8.1
Run Code Online (Sandbox Code Playgroud)
我尝试了什么:-rdynamic,-fno-pie和-fPIC,(我已经在-O0上),-ggdb3而不是默认的-g3,没有任何函数名称显示出来。
我正在使用:gcc 8.2,boost 1.69(stracktrace库的仅标头模式),Arch Linux(在该系统上,我必须手动安装未打包的libbacktrace,所以我更喜欢使用ADDR2LINE方法)
编辑:更新到最新的boost.stacktrace的链接。
Edit2:我注意到boost.stacktrace文档包含此提示
由于地址空间布局随机化,共享库中的函数名称可能无法解码。总比没有好。
...听起来很有帮助,但对我来说却是另一回事,我没有在自己的可执行文件中获取符号,但是libgtest.so例如。因此,好像调试信息设置有问题。任何想法表示赞赏。
第1部分
我整理了以下文件(改编自此处)。既然没有execinfo.h(据我所知),因此这只适用于 Linux。它不使用 boost,但无论如何它可能很有用。
backtrace_util.h:
#ifndef BACKTRACE_UTIL_H
#define BACKTRACE_UTIL_H
void backtrace_print(void);
#endif // BACKTRACE_UTIL_H
Run Code Online (Sandbox Code Playgroud)
backtrace_util.cc:
#include <execinfo.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
void backtrace_print(void) {
int j, nptrs;
#define SIZE 100
void *buffer[100];
char **strings;
nptrs = backtrace(buffer, SIZE);
printf("backtrace() returned %d addresses\n", nptrs);
/* The call backtrace_symbols_fd(buffer, nptrs, STDOUT_FILENO)
would produce similar output to the following: */
strings = backtrace_symbols(buffer, nptrs);
if (strings == NULL) {
perror("backtrace_symbols");
exit(EXIT_FAILURE);
}
for (j = 0; j < nptrs; j++)
printf("%s\n", strings[j]);
free(strings);
}
Run Code Online (Sandbox Code Playgroud)
backtrace_test.cc:
#include <stdio.h>
#include <stdlib.h>
#include "backtrace_util.h"
void myfunc(int ncalls) {
if (ncalls > 1)
myfunc(ncalls - 1);
else {
backtrace_print();
}
}
int main(int argc, char *argv[]) {
if (argc != 2) {
fprintf(stderr, "%s num-calls\n", argv[0]);
exit(EXIT_FAILURE);
}
myfunc(atoi(argv[1]));
exit(EXIT_SUCCESS);
}
Run Code Online (Sandbox Code Playgroud)
编译/链接为-std=c++17 -g,链接为-rdynamic. 输出是
$ ./backtrace_test 2
backtrace() returned 6 addresses
./backtrace_test(_Z15backtrace_printv+0x2e) [0x55c51759bc51]
./backtrace_test(_Z6myfunci+0x25) [0x55c51759bbbb]
./backtrace_test(_Z6myfunci+0x1e) [0x55c51759bbb4]
./backtrace_test(main+0x5b) [0x55c51759bc19]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xe7) [0x7f3c2c2f5b97]
./backtrace_test(_start+0x2a) [0x55c51759baaa]
Run Code Online (Sandbox Code Playgroud)
当试图分解所获得的名称时,我遇到了this和this。我没有跟进将整个事情放在一起。