C++ – How to use gdb

cgdb

I decided to find out how our C/C+ *nix practitioners use the gdb debugger.

Here is what I typically use:

  1. b – break filename.c:line #, function, filename.cpp:function, className::Member
  2. n, c, s — next continue step
  3. gdb program name => set breakpoints ==> run [parameter list] (I do this to set break points before the program starts)
  4. l – to list the surrounding source code.
  5. attach processID
    6 break [location]
  6. gdb programName corefile.core (to examine why app crashed)
  7. I also sometimes set breakpoint at exit function (break exit) to examine program stacks
  8. info b to examine all the breakpoints
  9. clear [breakpoints list ]

How do you use it?

Best Answer

Most useful gdb commands in my opinion (aside from all already listed):

  • info threads - information about threads
  • thread N - switch to thread N
  • catch throw - break on any thrown exception. Useful when you caught the bug only after the stack unwound.
  • printf,print - examine any and all expressions, printf accepts C-style formatting specifiers

Finally, if debugging over a slow link, the text UI might be of use. To use it, start gdb with the --tui command-line switch.