Pretty Printing in GDB

Metin Cakircali

1 min read

When you use GDB (GNU Project Debugger) to debug C++ code, the print command may display strange results for std::string and STL containers. In order to display complex types properly, GDB provides a so-called pretty-printing mechanism via Python code.

The info pretty-printer command lists the installed pretty-printers. If you see the following, it means the linux distro is not configured by default.

(gdb) info pretty-printer 
global pretty-printers:
  builtin
    mpx_bound128

missing libstdc++-v6

The output above is from GDB on openSUSE Leap 15.5, which is not a configured distro. So, we need to configure manually as described here. Basically, we need to add the following to the~/.gdbinit file.

python
import sys
sys.path.insert(0, '/usr/share/gcc-10/python')
from libstdcxx.v6.printers import register_libstdcxx_printers
register_libstdcxx_printers (None)
end

.gdbinit

Here, the /usr/share/gcc-10/python is the directory where the pretty printers are located. The output after this should look like the following:

(gdb) info pretty-printer 
global pretty-printers:
  builtin
    mpx_bound128
  libstdc++-v6
    __gnu_cxx::_Slist_iterator