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://gcc.gnu.org/svn/gcc/trunk/libstdc++-v3/python stlprettyprinter
-
The GDB that I had in Ubuntu was compiled to run Python 3.4. The Python file
printers.py
downloaded in the above step gave errors with it. I found an alternateprinters.py
from here that worked. -
Open
~/.gdbinit
and add these lines:
python import sys sys.path.insert(0, '/home/joe/stlprettyprinter') from libstdcxx.v6.printers import register_libstdcxx_printers register_libstdcxx_printers (None) end
- That is it, this gave me pretty printing of STL containers as shown above! :-)
References:
Tried with: GDB 7.7 and Ubuntu 14.04
Tagged: gdb, pretty printing, stl
