Question

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.

Was it helpful?

Solution

Try removing "stackSize = 1;"

OTHER TIPS

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.

The compiler may be inlining those functions. Could try recompiling with the -O0 option.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top