Using standard C++ library debug symbols? Ubuntu / Linux / libstdc++6-8-dbg?
Asked Answered
R

2

7

There is a package called libstdc++6-8-dbg on Ubuntu Linux (latest version at time of writing).

It is described as:

GNU Standard C++ Library v3 (debugging files) This package contains the shared library of libstdc++ compiled with debugging symbols.

Among other things it contains these files:

/usr/lib/x86_64-linux-gnu/debug/libstdc++.a
/usr/lib/x86_64-linux-gnu/debug/libstdc++.so.6.0.25
/usr/lib/x86_64-linux-gnu/debug/libstdc++fs.a

Normally to compile a (single translation unit) C++ program with gcc you can write:

$ g++ myprogram.cc

To add generation of debug symbols of user code you pass -g:

$ g++ -g myprogram.cc

But this doesn't include the debug versions of the standard library.

What extra options do you need to pass to g++ to tell it to use the debug versions of the standard library provided by libstdc++6-8-dbg?

Radiobroadcast answered 17/4, 2019 at 0:24 Comment(6)
What's your goal? Do you want access to the debug symbols when debugging, would that be sufficient? Or maybe you just need them available at runtime? I'd consider statically linking a debug version of libstdc++ a measure of last resort.Clement
@JohnKugelman: I want backtraces to correctly symbolize libstdc++ call frames at run-time. If you know the g++ command-line for (1) dynamically linking the debug version and (2) statically linking the debug version, I'll take both thanks.Radiobroadcast
@JohnKugelman: gdb isn't in play. I mean at run-time of the program I want it to use the debug versions of libstdc++ so that a stacktrace will correctly symbolize any libstdc++ calls within.Radiobroadcast
What do the numbers of libstdc++6-8-dbg refer to? On my system I am drawn to libstdc++6-10-dbg, but have GCC version 9.3.0 and libstdc++ version 3.4.Matchbook
Compile with all warnings and debug info, so g++ -Wall -Wextra -g myprogram.ccHrvatska
@BasileStarynkevitch: Thanks, the question uses a minimal set of options for clarity.Radiobroadcast
W
6

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.

Whereunto answered 13/6, 2021 at 7:58 Comment(2)
Does the package libstdc++6-dbgsym actually exist ? I don't find it here : packages.ubuntu.com packages.ubuntu.com/focal/allpackages packages.ubuntu.com/focal-updates/allpackagesHeel
@Tesla123 Debug symbol packages are stored in a separate archive "because these packages are used relatively rarely compared to the main repositories." I guess that's why they can't be found through the web package search. Instead, try enabling the repository according to the link and searching through apt.Whereunto
C
3

GDB automatically reads in the debug symbols once you've installed the package. You don't need to compile your program any differently.

If you want your program to load the debug version your best bet is to adjust the library search path. You could do that by setting LD_LIBRARY_PATH temporarily:

$ LD_LIBRARY_PATH=/usr/lib/x86_64-linux-gnu/debug/
$ ldd test
        libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/debug/libstdc++.so.6 (0x00007efcef670000)
        ...

Or permanently:

$ export LD_LIBRARY_PATH=/usr/lib/x86_64-linux-gnu/debug/
$ ldd test
        libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/debug/libstdc++.so.6 (0x00007efcef670000)
        ...

Or you could make it a system-wide change. You can do that in Ubuntu by adding a config entry to /etc/ld.so.conf.d/ and running ldconfig to update the cache.

$ sudoedit /etc/ld.so.conf.d/debug.conf
$ cat /etc/ld.so.conf.d/debug.conf
/usr/lib/x86_64-linux-gnu/debug
$ sudo ldconfig
$ ldd test
        libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/debug/libstdc++.so.6 (0x00007f3aced53000)
        ...

The config files are searched alphabetically so just make sure the one you write (debug.conf above) comes earlier than the default one (x86_64-linux-gnu.conf on my system).

Clement answered 17/4, 2019 at 0:54 Comment(1)
Ok, but is it possible to link against the debug versions so that at run-time of the program, stacktraces generated within the program are correctly symbolized when they contain libstdc++ calls? Or is this package only usable with gdb?Radiobroadcast

© 2022 - 2024 — McMap. All rights reserved.