Saturday, September 1, 2012

Book review: The Art of Debugging with GDB, DDD and Eclipse (Chapter 1).

In order to become a better programmer, I am reading books.  However, sometimes, reading books thoroughly and remembering their content is not always easy.  I therefore decided to write summarizing reviews of the books that I read.  Below is the first of a set of summaries for the book `The Art of Debugging with GDB, DDD and Eclipse'.

Chapter 1

The book covers only GDB, DDD and Eclipse as debugging tools and C and C++ as programming languages.  The first chapter defines the Principle of Confirmation as `you as a programmer trying to confirm that what you believe is true, is actually true'.  Debuggers can help with this quest for confirmation.  Adding trace code (printf and friends) is discouraged: using a debugger is of course much easier and takes far less time.  Debuggers also allow you to find the location of a bug and set watchpoints.  The authors mention the following debugging principles:
  • Start small: run your program on easy, simple cases.
  • Use a top-down approach: do not step into functions when not necessary.
  • Use a debugging tool to determine the location of a segmentation fault.
  • Determine the location of an infinite loop by using your debugger's interrupt and backtrace features.
  • Use binary search.
There are some differences between the text-based GDB debugger and GUI-based debuggers like DDD and Eclipse.  Setting and clearing breakpoints and stepping through code is much easier in DDD and Eclipse.  However, using GDB also has some advantages:
  • It starts up quickly.
  • It can be used in cases where a GUI-debugger cannot be used (ssh remote sessions etc).
  • It needs less screen space.
  • Debugging a GUI with a GUI can be problematic.
GDB also has a Terminal User Interface (TUI) mode which can be invoked with the -tui option or by typing CTRL-X-A when in GDB.  In TUI-mode, use the arrow keys to scroll up and down the source code.

The main debugger operations are:
  • Stepping through code: breakpoints, single-stepping (next=next line, step=enter function), resume (continue), temporary breakpoints (tbreak, until, finish).
  • Inspecting variables: in GDB this can be done with the print command, in DDD and Eclipse by hovering over a variable with the mouse.
  • Setting watchpoints: pausing execution whenever a value of a specified variable changes.  A GDB example is watch (i>10).
  • Moving up and down the call stack: in GDB you use the frame, up, down and backtrace commands for this.
Getting help about a GDB-command can be done using the straightforward help command (e.g. help breakpoints).

The chapter ends with an introductory debugging session.  Interesting new commands that appear in this session are break and condition.  Another interesting GDB-feature that is mentioned is its .gdbinit startup files.  Somewhere in the chapter, the authors also mention cgdb as an alternative to GDB.

Stay tuned for Chapter 2!

No comments:

Post a Comment