Thursday, September 6, 2012

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

Chapter 2: stopping to take a look around

Chapter 2 deals with breakpoints, watchpoints and catchpoints, which are collectively called breakpoints in the GDB documentation. Each breakpoint is assigned a unique identifier, starting at 1. Important to note is that if you type break 35 you will break right before line 35, so line 35 is not executed yet!

With the info breakpoints command, you get all the info you need about your breakpoints. In DDD and Eclipse, similar functionality is available using your mouse.

Setting breakpoints in DDD and Eclipse is easy with the mouse. In GDB, there are many ways to specify a breakpoint. Some examples:

  break myfunction
  break line_number
  break myfilename:line_number
  break myfilename:myfunction

With tbreak you can set a temporary breakpoint, which automatically gets deleted after the first time it is reached.

Another important remark is that internally, GDB works with machine language instructions, not lines of source code. This explains why the location of a breakpoint is sometimes different from where you intended it to be.

For C++ code, the catch command might be useful.

Note also that GDB has the concept of focus. The current focus is the source code you're currently seeing. It changes when you apply the list command to another source file, when you step into code from a different source file or when you hit a breakpoint while executing code in a different source file.

Type quit to leave GDB.

To avoid having to redefine your breakpoints, it is important not to exit GDB during a debug and recompile session. Note also that breakpoints at fixed line numbers can change after you've edited and recompiled the code! If you do want to quit GDB, you can use the .gdbinit file to save your breakpoints and other GDB commands.

There are many different ways to delete a breakpoint. Some examples are:

  delete
  delete 1 2 3
  clear
  clear myfunction
  clear myfile:myfunction
  clear linenumber
  clear myfile:linenumber

Similary for disabling breakpoints:

  disable
  disable 1 3
  enable 1 3
  enable once 1 3

In DDD and Eclipse, deleting and disabling breakpoints can easily be done with the mouse. Drag and drop of breakpoints also works in DDD, which by the way also has undo/redo functionality.

The info breakpoints command gives you the following information:

  • Identifier (Num): unique identifier
  • Type (Type): breakpoint, watchpoint or catchpoint
  • Disposition (Disp): keep, del, dis
  • Enable Status (Enb)
  • Address (Address): location in memory
  • Location (What)
and it also gives you how many times a breakpoint was hit already.

To resume execution at a breakpoint, you can use the step and continue commands.

No comments:

Post a Comment