gdb and the SGI STL
For performance reasons, SGI's STL implmentation is all inline; a naming convention is generally followed (i.e. double underscore as a prefix for local variables), but tends to create long, cryptic variable names. Unfortunately, raw gdb is not a graceful way to debug STL-heavy code, the reasons being -
- First, the 'T' in "STL" stands for "template"; while you as a programmer have full information about what is the argument to the template, gdb does not necessarily have access to this knowledge. As a result, gdb will tend to think that every (templated) pointer is a (void*), and will have no access to its internals should it point to a class.
- The traversal of some containers (hashes, trees) is complicated; obtaining the "first()" and "next()" elements of a container is a tricky task.
- Some data, such as the size of lists, is not stored explicitly - and there is simply no way to find it out in gdb.
gdb_stl_utils is a small utility that may help solving these problems.
To use it, download and compile StlStdContainers.cc (a simple g++ StlStdContainers.cc -o StlStdContainers.o will do); then, load gdb_stl_utils into gdb by using the command "source gdb_stl_utils" (in gdb). You will then have access to the following gdb function:
p_stl_tree, p_stl_tree_size (for maps and sets)
p_stl_hash, p_stl_hash_size (for hash_maps and sets)
The names are self-explanatory: the functions print the contents and the sizes of STL containers given to them.
Good luck and happy debugging!
June 2005: Tony Novak updated the tools to the latest gcc version and packaged them. Thanks!