The OP wants to resolve C++ standard library's symbols in the backtrace correctly. John's answer correctly states that this could be achieved by linking against the debug version of the standard libraries.
However, Ubuntu also provides debug symbol packages that, once installed, allow GDB to resolve symbols in standard libraries whose debug symbols have been stripped i.e. in the release version of the standard library. We provide a how-to example below (I'm using Ubuntu 20.04):
Suppose the generated binary is named a.out. We first find the version of libstdc++ it links against:
$ ldd a.out
...
libstdc++.so.6 => /lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007ff8dc6f7000)
...
We search for the package that provides the shared library file (/lib is a symbolic link to /usr/lib. The full path must be used here.):
$ dpkg -S /usr/lib/x86_64-linux-gnu/libstdc++.so.6
libstdc++6:amd64: /usr/lib/x86_64-linux-gnu/libstdc++.so.6
Follow the instructions to add the repo for debug symbol packages and then update the package index. The link also describes how one may search for debug symbol packages, but I directly searched with the package name:
$ apt list libstdc++6\*
...
libstdc++6-dbgsym/focal-updates 10.2.0-5ubuntu1~20.04 amd64
...
There will be tons of results, but be sure to look out for dbgsym
, rather than dbg
! After installing libstdc++6-dbgsym
, GDB should be able to resolve the symbols, even if your binary isn't linked against the debug library.
The texts above should answer the OP's question. Now I point out an issue with John's answer.
GDB automatically reads in the debug symbols once you've installed the package. You don't need to compile your program any differently.
This statement is 100% correct, but the figure included is irrelevant and doesn't prove the statement. There are three closely-releated concepts at play here:
- Debug library package: The package
libstdc++6-8-dbg
provides a version of the libstdc++ library with debug symbols.
- Debug symbol pagkage: The package
libstdc++6-dbgsym
provides the debug symbols for the libstdc++ library. That is, it doesn't include any machine instructions for functions like printf
, only the debug symbols. libstdc++6-8-dbg
bundles code and debug symbols together into one library.
- Release library package: The packages
libstdc++6-8
and libstdc++6
provide the release versions of the standard library, meaning that they don't carry debug symbols, only the code. Debug symbol packages should be used together with the release libraries.
GDB will automatically read the debug symbols in the debug symbol package, but not in the debug library. The auto-load
in John's figure simply states that the Python script /usr/share/gdb/auto-load/usr/lib/x86_64-linux-gnu/debug/libstdc++.so.6.0.25-gdb.py will be run automatically when the debug library is loaded and has nothing to do with the auto-loading of debug symbols.
libstdc++6-8-dbg
refer to? On my system I am drawn tolibstdc++6-10-dbg
, but have GCC version 9.3.0 and libstdc++ version 3.4. – Matchbookg++ -Wall -Wextra -g myprogram.cc
– Hrvatska