Search code examples
c++crashbacktrace

C++ backtrace doesn't print functions and so files


I have multiple projects. each project create its own so file. For some reason backtrace doesn't print function and so file on crash.

I compiled with -rdynamic. for example: -std=c++14 -pthread -pedantic -rdynamic -fPIC -g -c -fmessage-length=0 -llibtcmalloc

This is the backtrace that I'm getting on program crash:

Error: signal 11:
./libs/BaseCppProjectRun[0x402a50]
/lib/x86_64-linux-gnu/libc.so.6(+0x354b0)[0x7fb9aa1db4b0]
./libs/BaseCppProjectRun[0x403013]
./libs/BaseCppProjectRun[0x402b95]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7fb9aa1c6830]
./libs/BaseCppProjectRun[0x402669]

backtrace function:

void PrintCallStackOnError(int sig)
{
    void *array[10];
    size_t size;
    size = backtrace(array, 10);
    fprintf(stderr, "Error: signal %d:\n", sig);
    backtrace_symbols_fd(array, size, STDERR_FILENO);
    exit(1);
}

(this function will be call by: signal(SIGSEGV, PrintCallStackOnError) that defined on main function).

Can someone please help to print the so file and the function name on backtrace please?

thanks.


Solution

  • OK, I found what was the problem. Because I am using make file, I should also add -g -rdynamic to linker. like this:

    all: main.o
        g++ -Wall -g -rdynamic -o prog main.o
    
    main.o: main.cpp 
        g++ -Wall -g -c -rdynamic main.cpp
    

    Now it's working :)