sh4-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 {此处的文件} -Wl,--end-group --verbose -Xlinker -lm
EDIT2:我发现了哪个标志是原因:-fno-例外
。谁能告诉我为什么?是否可以在不跳过此标志的情况下修复它?
EDIT3:好吧,没关系。看来我其实可以省略这个flag。
I'm trying to print call stack from within a program. Unfortunately, call to glibc backtrace() returns me always only one record - address of current function. I'm working on sh4-linux, which probably causes the problem. I had no problems in printing it on x86 architecture.
Example code:
#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;
}
which was compiled by:
sh4-linux-g++ test.cpp -g -c -o test.o
sh4-linux-g++ test.o -g -rdynamic -o test
EDIT: Actually this code works fine. Probably some compiler flag causes this behavior in real project.
Compiler flags are: -g -O0 -pipe -fpermissive -frtti -fno-exceptions -ffunction-sections
Linker flags: -lpthread -g -rdynamic -Wl,-gc-sections -Wl,--start-group {Files here} -Wl,--end-group --verbose -Xlinker -lm
EDIT2: I found out which flag is the cause: -fno-exceptions
. Can anyone tell me why? And if it can be repaired without skipping this flag?
EDIT3: Well, nevermind. It seems that I can actually omit this flag.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试删除“stackSize = 1;”
Try removing "stackSize = 1;"
需要 glibc 补丁。请查看此处。
如补丁中所述,使用回溯的用户应用程序需要使用“-fexceptions”进行编译。如果您想要地址的完整符号解析,您还需要“-rdynamic”。
A patch to glibc is needed. Look here.
As noted in the patch, user applications using backtrace need to be compiled with "-fexceptions". If you want full symbol resolution of addresses you need "-rdynamic" too.
编译器可能会内联这些函数。可以尝试使用 -O0 选项重新编译。
The compiler may be inlining those functions. Could try recompiling with the -O0 option.