Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

How to enable pretty printing for STL in GDB

📅 2014-Jul-17 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ gdb, pretty printing, stl ⬩ 📚 Archive

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:

$ svn co svn:// stlprettyprinter
import sys
sys.path.insert(0, '/home/joe/stlprettyprinter')
from libstdcxx.v6.printers import register_libstdcxx_printers
register_libstdcxx_printers (None)


Tried with: GDB 7.7 and Ubuntu 14.04