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

时间:2019-04-17 00:24:50

标签: c++ linux ubuntu gcc libstdc++

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?

2 个答案:

答案 0 :(得分:1)

OP 希望正确解析回溯中 C++ 标准库的符号。 John's answer 正确地指出这可以通过链接标准库的调试版本来实现。

然而,Ubuntu 还提供了 debug symbol packages,一旦安装,它允许 GDB 解析标准库中的符号,这些标准库中的调试符号已被剥离,即在标准库的发布版本中。我们在下面提供了一个操作示例(我使用的是 Ubuntu 20.04):

假设生成的二进制文件名为 a.out。我们首先找到它链接的 libstdc++ 版本:

$ ldd a.out
        ...
        libstdc++.so.6 => /lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007ff8dc6f7000)
        ...

我们搜索提供共享库文件的包(/lib是/usr/lib的符号链接。这里必须使用完整路径。):

$ dpkg -S /usr/lib/x86_64-linux-gnu/libstdc++.so.6
libstdc++6:amd64: /usr/lib/x86_64-linux-gnu/libstdc++.so.6

按照 instructions 添加调试符号包的 repo,然后更新包索引。该链接还描述了如何搜索调试符号包,但我直接用包名搜索:

$ apt list libstdc++6\*
...
libstdc++6-dbgsym/focal-updates 10.2.0-5ubuntu1~20.04 amd64
...

会有大量的结果,但一定要注意dbgsym,而不是dbg!安装 libstdc++6-dbgsym 后,GDB 应该能够解析符号,即使您的二进制文件未链接到调试库。


以上文本应回答 OP 的问题。现在我指出约翰回答的一个问题。

<块引用>

一旦您安装了软件包,GDB 就会自动读入调试符号。您不需要以任何不同的方式编译您的程序。

这个说法是 100% 正确的,但所包含的数字是无关紧要的,也不能证明这个说法。这里涉及三个密切相关的概念:

  • 调试库包:包 libstdc++6-8-dbg 提供了带有调试符号的 libstdc++ 库版本。
  • 调试符号页面:包 libstdc++6-dbgsym 为 libstdc++ 库提供调试符号。也就是说,它不包含用于 printf 等函数的任何机器指令,仅包含调试符号。 libstdc++6-8-dbg 将代码和调试符号捆绑到一个库中。
  • 发布库包:包libstdc++6-8libstdc++6 提供标准库的发布版本,这意味着它们不携带调试符号,只携带代码。调试符号包应与发布库一起使用。

GDB 会自动读取调试符号包中的调试符号,但不会读取调试库中的调试符号。 John 图中的 auto-load 只是说明 Python 脚本 /usr/share/gdb/auto-load/usr/lib/x86_64-linux-gnu/debug/libstdc++.so.6.0 .25-gdb.py 会在调试库加载时自动运行,与调试符号的自动加载无关。

答案 1 :(得分:0)

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