motam79
motam79

Reputation: 3824

Boost stack-trace not showing function names and line numbers

I was exploring the boost::stacktrace by trying a simple example to print the call stack of a recursive function.

#include "boost/stacktrace.hpp"

int factorial(int x){
    if (x < 2) {
        std::cout << boost::stacktrace::stacktrace();
        return 1;
    }
    return x * factorial(x - 1);
}

int main(int ac, char *av[]) {
    std::cout << factorial(4);
}

However the output of the code does not include any information about the function name and line number:

 0# 0x000055A6F6B57C0F in /home/user/myapp
 1# 0x000055A6F6B57C42 in /home/user/myapp
 2# 0x000055A6F6B57C42 in /home/user/myapp
 3# 0x000055A6F6B57C42 in /home/user/myapp
 4# 0x000055A6F6B57C9D in /home/user/myapp
 5# __libc_start_main in /lib/x86_64-linux-gnu/libc.so.6
 6# 0x000055A6F6B57AEA in /home/user/myapp

This is in contrast with the sample output from the boost website: (copied from https://www.boost.org/doc/libs/1_68_0/doc/html/stacktrace/getting_started.html#stacktrace.getting_started.how_to_print_current_call_stack)

0# bar(int) at /path/to/source/file.cpp:70
1# bar(int) at /path/to/source/file.cpp:70
2# bar(int) at /path/to/source/file.cpp:70
3# bar(int) at /path/to/source/file.cpp:70
4# main at /path/to/main.cpp:93
5# __libc_start_main in /lib/x86_64-linux-gnu/libc.so.6
6# _start

Why don't I see the source line numbers and function names in the stack-trace output?

I have enabled debugging information in my project-level CMakeList.txt:

set(CMAKE_BUILD_TYPE Debug)

I can also see the symbols are present when demangling the binary file:

nm -an myapp | c++filt | grep factorial

0000000000000f40 t _GLOBAL__sub_I__Z9factoriali
00000000000010f0 T factorial(int)

Upvotes: 26

Views: 16471

Answers (6)

kevr
kevr

Reputation: 455

For Meson, provide the flags in both compile flags and link flags in unison with --buildtype debug:

...
debug_flags = [
  '-DBOOST_STACKTRACE_USE_ADDR2LINE',
  '-ggdb',
  '-no-pie',
  '-fno-pie',
  '-rdyanmic',
  # -fPIC needed in my case
  '-fPIC',
]
...
boost_dep = dependency('boost', modules : ['stacktrace_addr2line'])
dl_dep = dependency('dl')
...
executable('exec', 'exec.cpp',
  dependencies : [boost_dep, dl_dep],
  cpp_args : debug_flags,
  link_args : debug_flags)
...

Upvotes: 0

Luca
Luca

Reputation: 1

If you use cmake this worked for me on linux machine

add_executable(main main.cpp)
target_compile_definitions(main PRIVATE BOOST_STACKTRACE_USE_ADDR2LINE)
target_compile_options(main PRIVATE "-g;-fno-inline")
target_link_options(main PRIVATE "-no-pie")
target_link_libraries(main ${Boost_LIBRARIES}  dl)

see this example

Upvotes: 0

Jacob Burckhardt
Jacob Burckhardt

Reputation: 503

This worked for me:

g++ -g backtrace.cpp -o bb -lboost_stacktrace_backtrace -D BOOST_STACKTRACE_LINK

-D BOOST_STACKTRACE_LINK was the missing option that solved the problem for me.

Upvotes: 4

Geoffrey Merck
Geoffrey Merck

Reputation: 51

This is what worked for me, I am compiling with g++ :

  • Compile with -g -ggdb -no-pie -fno-pie -rdynamic
  • define macro -DBOOST_STACKTRACE_USE_ADDR2LINE
  • Link against libdl -libl and -no-pie -fno-pie

Edit : in my case rdynamic was the missing bit to allow me to have unmagled function names and line numbers

Upvotes: 5

Pavel Bazika
Pavel Bazika

Reputation: 406

I do not have enough reputation to comment, so I'll extend jordi's answer here:

  1. Make sure that debug info is enabled: e.g., -g

  2. Link against libdl: -ldl

  3. Define one of the necessary macros (to get line numbers): e.g., -DBOOST_STACKTRACE_USE_ADDR2LINE

  4. Compile and link with -no-pie and -fno-pie options.

Upvotes: 21

jordi
jordi

Reputation: 345

You need to do a few things (this is for Linux):

  1. Make sure that debug info is enabled: e.g., -g

  2. Link against libdl: -ldl

  3. Define one of the necessary macros (to get line numbers): e.g., -DBOOST_STACKTRACE_USE_ADDR2LINE

All this info is provided here:

https://www.boost.org/doc/libs/1_69_0/doc/html/stacktrace/configuration_and_build.html

Upvotes: 17

Related Questions