Linux includes the GNU debugger (gdb), a command-line debugging tool for serial programs. GDB is probably the best known symbolic debugger, and it has seen wide use among programmers. But don't be fooled by its commonality, or by its seemingly rudimentary interface; it is actually a very powerful debugging aid.

For parallel programming, GDB supports multithreaded applications natively and multiprocess applications only by attaching to each process with an individual debugging instance. Although it is not suitable for debugging subtle timing events in large parallel programs, GDB is simple enough to be effective at finding and correcting most program logic defects.

GDB can be set up to debug a running process in the following ways:

  • To use GDB to launch a process and debug it: gdb <executable>
  • To attach GDB to a running process: gdb <executable> <process ID>

Basic debugging under gdb can be conducted after learning a relatively small number of commands. You'll need the commands for controlling basic program execution like setting breakpoints and stepping through lines of code and functions. You'll also want to learn how to display variables and the call stack, and reacting to fault conditions (such as segmentation faults). More detailed commands are easy to learn using gdb's online help system and of course, most of the above information can be gleaned from the man page. In general, though, the most useful gdb commands are as follows:

  • run — execute the program from beginning.
  • backtrace — produce the backtrace from the last fault
  • break <line number> or break <function-name> — break at the line number or at the use of the function
  • step — step to next line of code (step into function if possible)
  • next — step to next line of code (do not step into function)
  • print <variable name> — print the value stored by the variable
  • continue — run until next break point
 
©  |   Cornell University    |   Center for Advanced Computing    |   Copyright Statement    |   Inclusivity Statement