Yaron Israeli
Yaron Israeli

Reputation: 351

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.

Upvotes: 0

Views: 704

Answers (2)

Yaron Israeli
Yaron Israeli

Reputation: 351

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 :)

Upvotes: 0

Jesper Juhl
Jesper Juhl

Reputation: 31459

Compile your program (and libraries) with debug symbols enabled. -g or -ggdb. And you will get more useful backtraces - and yes, do that even for release builds. Your binaries will be larger but your runtime performance will not be impacted.

Also look into the addr2line tool.

Upvotes: 1

Related Questions