Lab 3 Preparation

To prepare for Lab 2 on the gdb debug tool, first copy the source file for the sample debug program (found at ~allisor/Student/misc/debug.c in the lab) and compile and run it. Note that it does not run correctly; this is intentional. Try using both numeric and alphabetic arguments to see if there is any difference in its behaviour.

Now recompile it using the -g option of gcc (show the man page with man 1 gcc, then use /Produce debugging to search for how to set the debug option).

Finally, run the program under the control of gdb by entering this form of command:

gdb my-program-name

where you replace my-program-name with the name of your executable program (no ./ prefix is necessary, but it may be specified if you wish). You are now at the gdb prompt and your program is ready to run.

Here are some of the most frequently needed gdb commands (this list is adapted from the man page:

break p

  • Set a breakpoint at source line or function p in this file.

run [arg]

  • Start your program from the beginning with the argument list, if arg is specified.

bt

  • Backtrace displays the program stack. Especially useful for tracing back through your own function calls.

list

  • List source code lines in the current file.

print e

  • Display the value of expression e.

cont

  • Continue running your program after stopping at a breakpoint (also c or continue).

next

  • Execute the next program line after stopping; step over any function call in the line (also n).

step

  • Execute the next program line after stopping; step into any function call in the line (also s).

help [n]

  • Show information about a gdb command name, or general information about using gdb if n is not given.

quit

  • Exit from gdb.

A good way to start debugging is to set a breakpoint right at the beginning of the program (break main) and stepping through line by line with next or step, displaying important values with print as you move through it.

There is a good description of command-line arguments in section 5.10 of K&R (Kernighan and Ritchie's "The C Programming Language") starting on page 114.

Now read over the instructions for Lab 3 and follow them carefully.