How to enable pretty printing for STL in GDB

Printing a C++ STL container in GDB produces information about the internal structure of the container that is hard to understand:

(gdb) print foo_int_vector
$1 = {<std::_Vector_base<int, std::allocator<int> >> = {_M_impl = {<std::allocator<int>> = {<__gnu_cxx::new_allocator<int>> = {<No data fields>}, <No data fields>}, _M_start = 0x603010, _M_finish = 0x60301c, 
      _M_end_of_storage = 0x60301c}}, <No data fields>}

By adding pretty printing support, we can get information about the STL container that is easy to understand:

(gdb) print foo_int_vector
$1 = std::vector of length 3, capacity 3 = {34, 999, 345}

To enable pretty printing is easy:

  • Ensure that the GDB version is 7.0 or later. This means it has support for Python scripting.

  • Check out the Python module to perform pretty printing of STL containers:

$ svn co svn:// stlprettyprinter
  • The GDB that I had in Ubuntu was compiled to run Python 3.4. The Python file downloaded in the above step gave errors with it. I found an alternate from here that worked.

  • Open ~/.gdbinit and add these lines:

import sys
sys.path.insert(0, '/home/joe/stlprettyprinter')
from libstdcxx.v6.printers import register_libstdcxx_printers
register_libstdcxx_printers (None)
  • That is it, this gave me pretty printing of STL containers as shown above! πŸ™‚


Tried with: GDB 7.7 and Ubuntu 14.04

4 thoughts on “How to enable pretty printing for STL in GDB

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.