98

Linux 特定backtrace()backtrace_symbols()允许您生成程序的调用跟踪。但是,它只打印函数地址,而不是我的程序的名称。我怎样才能让他们也打印函数名称?我试过用 和 编译-g程序-ggdb。下面的测试用例只是打印这个:

    回溯 ------------
    ./a.out() [0x8048616]
    ./a.out() [0x8048623]
    /lib/libc.so.6(__libc_start_main+0xf3) [0x4a937413]
    ./a.out() [0x8048421]
    ----------------------
    

我希望前两项也显示函数名称,foo并且main

代码:

#include <execinfo.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>

static void full_write(int fd, const char *buf, size_t len)
{
        while (len > 0) {
                ssize_t ret = write(fd, buf, len);

                if ((ret == -1) && (errno != EINTR))
                        break;

                buf += (size_t) ret;
                len -= (size_t) ret;
        }
}

void print_backtrace(void)
{
        static const char start[] = "BACKTRACE ------------\n";
        static const char end[] = "----------------------\n";

        void *bt[1024];
        int bt_size;
        char **bt_syms;
        int i;

        bt_size = backtrace(bt, 1024);
        bt_syms = backtrace_symbols(bt, bt_size);
        full_write(STDERR_FILENO, start, strlen(start));
        for (i = 1; i < bt_size; i++) {
                size_t len = strlen(bt_syms[i]);
                full_write(STDERR_FILENO, bt_syms[i], len);
                full_write(STDERR_FILENO, "\n", 1);
        }
        full_write(STDERR_FILENO, end, strlen(end));
    free(bt_syms);
}
void foo()
{
    print_backtrace();
}

int main()
{
    foo();
    return 0;
}
4

5 回答 5

67

The symbols are taken from the dynamic symbol table; you need the -rdynamic option to gcc, which makes it pass a flag to the linker which ensures that all symbols are placed in the table.

(See the Link Options page of the GCC manual, and / or the Backtraces page of the glibc manual.)

于 2011-08-04T00:23:33.317 回答
30

使用addr2line 命令将可执行地址映射到源代码文件名+行号。-f提供获取函数名称的选项。

或者,尝试libunwind

于 2011-08-04T00:49:51.630 回答
14

Ian Lance Taylor 出色的 Libbacktrace 解决了这个问题。它处理堆栈展开并支持普通 ELF 符号和 DWARF 调试符号。

Libbacktrace 不需要导出所有符号,这会很丑陋,ASLR 不会破坏它。

Libbacktrace 最初是 GCC 发行版的一部分。现在,可以在 Github 上找到一个独立版本:

https://github.com/ianlancetaylor/libbacktrace

于 2015-05-27T10:09:09.857 回答
2

如果 ret == -1 并且 errno 是 EINTER,则顶部的答案有一个错误,您应该再试一次,但不要将 ret 视为已复制(如果您不喜欢它,则不会为此创建一个帐户)

static void full_write(int fd, const char *buf, size_t len)
{
        while (len > 0) {
                ssize_t ret = write(fd, buf, len);

                if ((ret == -1) {
                        if (errno != EINTR))
                                break;
                        //else
                        continue;
                }
                buf += (size_t) ret;
                len -= (size_t) ret;
        }
}
于 2017-11-16T18:22:13.270 回答
1

提升回溯

非常方便,因为它同时打印:

  • 未修改的 C++ 函数名称
  • 行号

自动为你。

使用总结:

#define BOOST_STACKTRACE_USE_ADDR2LINE
#include <boost/stacktrace.hpp>

std::cout << boost::stacktrace::stacktrace() << std::endl;

我在以下位置提供了一个最小的可运行示例和许多其他方法:print call stack in C or C++

于 2019-10-14T12:21:38.233 回答