如何在C 程序的StackTrace中查看有用的信息(文件名,编号)
How to see useful information (filename, line no) in stacktrace of C++ program
这是一个复杂的,因为也取决于boost版本和平台。
我正在使用Boost StackTrace打印一些断言失败的回溯。有一些外部编译时间和运行时深度,具体取决于您使用的模式(链接文档〜5模式)。我更喜欢基于调试信息和出口信息的东西(我认为后者也可以在生产构建中起作用)。但是我可以开始使用默认模式或BOOST_STACKTRACE_USE_ADDR2LINE
或BOOST_STACKTRACE_USE_BACKTRACE
-所有3个显示我实际程序代码的呼叫堆栈中的所有3个显示 - 请参见Google检验测试中的stackTrace:
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
我尝试过的内容:-rdynamic,-fno -pie and -fpic,(我已经在-O0上),-ggdb3而不是默认-g3, nothing 将函数名称用于出现。
相关:这个,
我打开:GCC 8.2,Boost 1.69(stracktrace lib的仅标头模式),Arch Linux(在此系统上,我必须手动安装不包装的Libbacktrace,所以我更喜欢使用addr2line方法)
编辑:更新到最新boost.stacktrace的链接。
edit2:我注意到boost.stacktrace doc包含此提示
由于地址空间布局随机化,可能不会解码共享库中的函数名称。仍然总比没有好。
...这听起来很有帮助,但对我来说,这是相反的,我没有在自己的可执行文件中获得符号,但我适用于libgtest.so
。因此,调试信息设置似乎有问题。任何想法都赞赏。
第1部分
我将以下文件(从此处改编)。由于Windows中没有execinfo.h
(我知道),因此仅在Linux中起作用。它不使用Boost,但无论如何都可能很有用。
backtrace_util.h:
#ifndef BACKTRACE_UTIL_H
#define BACKTRACE_UTIL_H
void backtrace_print(void);
#endif // BACKTRACE_UTIL_H
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 addressesn", 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("%sn", strings[j]);
free(strings);
}
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-callsn", argv[0]);
exit(EXIT_FAILURE);
}
myfunc(atoi(argv[1]));
exit(EXIT_SUCCESS);
}
与-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]
第2部分
试图删除获得的名称时,我遇到了这个。我没有跟进整个过程。
- 正在查找文档以获得PS4平台的C++中的设备信息
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 奇怪的(对我来说)返回声明 - 在谷歌上找不到任何关于它的信息
- 为什么需要复制构造函数,在哪些情况下它们非常有用
- 如何设置一个范围来提取我想要获得的信息
- 其中降频广播实际上是有用的
- 系统参数信息A 与 SPI_GETMOUSE 返回 0
- 为什么 gcovr 会生成空覆盖率统计信息?
- 我已经阅读了很多关于 2d 数组的信息,但我在作业中使用它时遇到了麻烦
- 将信息输入到下面显示的结构向量中的正确语法/格式是什么
- 调试符号中缺少 QObject 类信息(编辑但存在其他 Qt 类)
- 为什么数组大小信息可用于"sizeof"运算符和 delete[] 运算符,但在将数组作为参数传递到
- Pisarze - 来自波兰奥林匹克信息学的数据分析任务
- 将数组信息存储到 c++ 向量中有一个"Access violation reading location"
- C++20 个模块在 clang (Windows):在最简单的示例中键入信息错误
- C++ 如何暂停控制台输出,以便用户可以在继续之前阅读信息
- 如何从WebAssembly模块检测浏览器信息?
- Visual Studio 2017 C++,不能使用 typeid() 获取信息对象,缺少指针?;
- 如何实现具有多个平台__FILE__和__LINE__信息的 C/C++ 可变参数日志记录宏?
- 如何在C 程序的StackTrace中查看有用的信息(文件名,编号)