c++ - sh4-linux 上的回溯返回一个函数

标签 c++ linux backtrace

我正在尝试从程序中打印调用堆栈。不幸的是,调用 glibc backtrace() 总是只返回一条记录——当前函数的地址。我正在研究 sh4-linux,这可能会导致问题。我在 x86 架构上打印它没有问题。

示例代码:

#include <string>
#include <iostream>
#include <execinfo.h>

const int maxCalls = 666;

void baz()
{
        void *buffer[ maxCalls ];
        int stackSize = backtrace( buffer, maxCalls );

        char **symbols = backtrace_symbols( buffer, stackSize );

        std::string str;

        for( unsigned i = 0; i < stackSize; ++i )
        {
                str+= symbols[i];
        }
        free( symbols );
        std::cout << str<< std::endl;
}

void bar()
{
        baz();
}

void foo()
{
        bar();
}

int main(int argc, char **argv)
{
        foo();
        return 0;
}

由以下人员编译:

sh4-linux-g++ test.cpp -g -c -o test.o
sh4-linux-g++ test.o -g -rdynamic -o test

编辑:实际上这段代码工作正常。可能某些编译器标志会在实际项目中导致此行为。

编译器标志是:-g -O0 -pipe -fpermissive -frtti -fno-exceptions -ffunction-sections

链接器标志:-lpthread -g -rdynamic -Wl,-gc-sections -Wl,--start-group {Files here} -Wl,--end-group --verbose -Xlinker -lm

EDIT2:我发现是哪个标志引起的:-fno-exceptions。谁能告诉我为什么?如果不跳过这个标志就可以修复它?

EDIT3:好吧,没关系。看来我真的可以省略这个标志。

最佳答案

尝试删除“stackSize = 1;”

关于c++ - sh4-linux 上的回溯返回一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4279123/

相关文章:

c++ - 如何合法地将函数指针转换为方法指针?

c++ - 调用 Singleton getInstance() 时出现 undefined symbol 错误

linux - Linux kernel panic call tr​​aces中的问号 '?'是什么意思?

c++ - 如何使用 linux 命令传递输入和配置文件

c++ - 如何在 C++ 中操作 float 的小数部分?

mysql - 如何在 RedHat Linux 8 中安装 R 和 RMySQL?

linux - 以编程方式检测处理器类型 (Linux)

linux - 连接中的套接字编程错误

c# - Visual C# 回溯 : how to know where [external code] resides?

java - dumpstack() 和 printStackTrace() 之间的区别