tags:

views:

127

answers:

3

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

+3  A: 

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

Michael Shopsin
It was *very* buggy the last time I tried it. Not sure if it improved in the meantime.
Nemanja Trifunovic
It still is very buggy. Compared to what I see on the mac (Xcode) or Windows (Studio) it is light years behind.
Totalview is better but costs money and Xcode/Studio are platform dependent, so DDD is sometimes the only option.
Michael Shopsin
KDevelop has an integrated Debugger which is working better and better (its still under development). There also is Kdbg under Linux. I personally prefer debugging with gdb directly.
MOnsDaR
+1  A: 

1.

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.

2.

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.

rstevens
A: 

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)

MOnsDaR