Printing STL objects while debugging C++ codes with GDB

As one can easily observe, it is not possible to print the values of STL class objects like maps, vectors, etc, while debugging a C++ code with GDB. However, it can be done by a simple hack: There is a gdb script called stl-views-1.0.3.gdb, which contains functions to print various STL container classes. One can use functions such as pmap, pvector, plist, etc, to print map, vector and list objects while debugging.

For this, we need to put the stl-views-1.0.3.gdb script file in ~/.gdbinit folder or we can load it manually from gdb by using the command:

gdb prompt>> source <some path>/stl-views-1.0.3.gdb

In the latest versions of gdb, python support is included. Hence, one can also use python scrips to perform similar tasks for which inbuilt support is not there in gdb. However, for this specific problem the above solution proves to be a quick way.

Leave a Reply