C++ specific debugging tricks with gdb – Education Career Blog

What are some of your favorite tricks to debug C++ programs with gdb ?

Interested in all tricks but also

  1. how you call methods (which may be virtual) on objects from within gdb

  2. inspecting STL objects (pretty printing them)

  3. preventing gdb from going into STL code with continue

  4. dealing with inlining, threads, tcmalloc (or custom allocators)

  5. Keeping history of gdb commands across different sessions


Try DDD when you debug C++; DDD can dynamically load source code for shared libraries, and display multiple variables while you debug.



set print object

This enables the evaluation of the object hierarchy by looking at the vtable. So you can see what type a reference/pointer to a base class is.


Get the debug infos or unstripped libraries for the system libs (most importantly: pthread and libstdc++) since otherwise debugging multithreaded apps isn’t working nicely in gdb.


Since I found out how I could use the Hooks in ~/.gdbinit to beautify the output of GDB (list, backtrace etc), I couldn’t live without that…

Here is a blogentry which shows how to beautify the code-output and to put some “divider” between your comments:
Beautify GDB

I’ve beautified my backtrace too, extremely useful to find where an error has occured now… Perhaps I’ll upload the needed files when I’ve got access to them (I’m not at work now)

Leave a Comment