如何在C 程序的StackTrace中查看有用的信息(文件名,编号)

How to see useful information (filename, line no) in stacktrace of C++ program

本文关键字:有用 信息 文件名 编号 程序 StackTrace      更新时间:2023-10-16

这是一个复杂的,因为也取决于boost版本和平台。

我正在使用Boost StackTrace打印一些断言失败的回溯。有一些外部编译时间和运行时深度,具体取决于您使用的模式(链接文档〜5模式)。我更喜欢基于调试信息和出口信息的东西(我认为后者也可以在生产构建中起作用)。但是我可以开始使用默认模式或BOOST_STACKTRACE_USE_ADDR2LINEBOOST_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部分

试图删除获得的名称时,我遇到了这个。我没有跟进整个过程。