gdb and debuggers

The gdb(1) utility is a source-level debugger. You can use gdb to move through programs systematically, one instruction at a time, and stop programs at predefined points. You can display the contents of variables and memory locations. You can also change information, such as the contents of variables or memory locations, in a running program.

The debugger works by reading debugging information stored in the object code. The debugging information includes symbol tables (often removed by strip) and line numbers. To use the line numbers, the debugger must be able to find the source code for the executable.

You can use the debugger on a program or an object file only if you have created your program with debugging information; that is, you have compiled it with the -g option. If you want the debugger to refer to the source code, the source code must be available on your system.

The basic steps for debugging a program are:

  1. Include debugging information when you compile and link your program.
  2. Start gdb.
  3. Debug the program.

Compiling for gdb

Code must be compiled with the -g option. The Interix port of gdb works best with code compiled with the Interix port of gcc. The object code produced by the Microsoft Visual C++ compiler (created by c89 -g or by cc -g) contains some debugging information, but contains less information than the object code obtained from gcc(1).

The -g option stores symbol information and line numbers. The debugger looks for the source files, by default, in the current directory.

With cc(1) and c89(1), the -g and -O options cannot be used at the same time. When compiling with the gcc compiler, both options can be used together.

Commands in gdb

This document uses the full names for gdb commands. You can abbreviate the commands, but you must type enough of a command to distinguish it from others. If the command fragment is ambiguous, gdb displays a list of possible commands.

The gdb debugger also offers command completion, which is useful for commands that take arguments. You can press the TAB key twice to complete the current command. If the command fragment is ambiguous, gdb displays a list of possible completions.

The gdb debugger also provides a help command for the various operations. To see a list of help issues, type help at the gdb prompt.