The gdb Command: Debugging C Programs

The gdb     permits you to run a program in such a way as to interactively control its progress and watch the results of its operations; in this way you should be able to detect how bugs arise. The first step in debugging a program is to compile it with the -g flag, as explained in the gcc command article above, e.g.,

        eris% gcc -g prog.c -o prog

Following this, you can give the command gdb prog

The program is now loaded into the gdb environment. However, it is not yet running. Before starting it running, you will probably first wish to set up a "breakpoint", to make it stop when it reaches a particular source line. As an example, you can set up a breakpoint to make the program stop after it enters main() (a number of things can happen in running a program before the main() function you wrote is entered), and then start it running, taking its standard input from some file named infile:

(gdb) break main
(gdb) run < infile

To reduce confusion you probably want to provide interactive input to the program through an infile, so that all interaction during debugging is with the gdb environment. After the breakpoint is reached, gdb will print out the C language statement reached. You can now make the program progress through single steps, executing consecutive source lines of program logic in the normal flow of control, by giving the command:

(gdb) s          -- s is for "step" -- enter and step through statements
                    of function when a function call is encountered
(gdb) n          -- n is for "next" - step, but skip over function calls
(gdb) r          -- run program 
(gdb) r small    -- run program, and give it ``small'' as an argument.
(gdb) r < infile -- run program with input from file ``infile''.

As each step is executed, the corresponding source line will be printed out. You can type CR on successive lines after the first command to perform successive steps. At any time where your input is expected, you can print out the value of any variable in the scope of the current logic or an expression involving such variables (such as 3*i ), by typing:

(gdb) p 3*i       -- p is for "print" 
                  -- i is a variable in scope of current position

The type printed will be the type derived from the variables in the expression; if special output formats are needed, you can use the "p/format" form for print:

(gdb) p/x 3*i   --  x for hexadecimal, o for octal, d for decimal, f for
                     float, c for char, s for string

You can also get the (default type) values of all variables in your scope by typing:

(gdb) i lo       -- i is for "info" -- gives values of local variables and
                    current stack level
(gdb) i var      -- values of global and static variables

You can get help (relatively useful help messages) while in the   by typing:

(gdb) h         -- h is for help -- list of help topics 
(gdb) h topic   -- help on named topic 
(gdb) h p       -- help on print command (or any other command)

At any time, you can type:

(gdb) q         - q is for "quit", i.e. Leave the debugger.

next up previous
Next: The gdb Command, cont Previous: The gcc Command:

Mon Jul 27 15:20:53 EDT 1998