University of California at Berkeley
College of Engineering
Department of Electrical Engineering and Computer Science
61C, Summer 2006
2: Strings and pointers; the GDB debugger
learn to use the gdb debugger to debug string and pointer programs in C.
Sections 5.1-5.5, in K&R
Reference Card (linked to class page under “resources.”)
Optional: Complete GDB
Basic tasks in GDB
There are two ways to start the debugger:
- In EMACS, type M-x
gdb, then type gdb <filename>
- Run gdb
<filename> from the command line
The following are
fundamental operations in gdb. Please make sure you know the gdb commands for
the following operations before you proceed.
- How do you run a
program in gdb?
- How do you pass
arguments to a program when using gdb?
- How do you set a
breakpoint in a program?
- How do you set a
breakpoint which which only occurs when a set of conditions is true (eg
when certain variables are a certain value)?
- How do you
execute the next line of C code in the program after a break?
- If the next line
is a function call, you'll execute the call in one step. How do you execute
the C code, line by line, inside the function call?
- How do you
continue running the program after breaking?
- How can you see
the value of a variable (or even an expression) in gdb?
- How do you print
a list of all variables and their values in the current function?
- How do you exit
out of gdb?
a short C program
Consider the C program appendTest.c.
You can copy it from ~cs61c/labs/02. Compile and run the program, and experiment with it. Try
appending a few strings, and notice that it does not always produce the
correct result. (Press Ctrl-C to exit). You will now use gdb to debug this
First, the program must be compiled with debugging information. To do
this, use the -g flag:
gcc -g -o appendTest appendTest.c
Now start gdb on the resulting program, either in emacs or on the
command line. Set a breakpoint in the
function, and run the program. When the debugger returns at the
breakpoint, step through the instructions in the
function line by line, and examine the values of the variables. In
particular, examine the value of
just before the function returns? Why is this a bug? Hint: How does C
represent strings? Fix this bug.
infamous "segmentation fault"
A segmentation fault or bus error is a common pointer error
in C programs. In general, it is caused by an invalid pointer dereference,
or by using an invalid address. In this part you will debug a program with
such an error.
Compile and run the program average.c (found in ~cs61c/labs/02). As its name suggests, the program is
supposed to compute the average of a set of numbers. Notice that
currently, the program seg faults after accepting more than one input.
Make sure the program has been compiled with debug information, and load
and run the program in gdb. Notice that gdb traps the segmentation fault,
and brings you back to the debugger.
First, verify the current location in the program. The command to do this
short), which will print a stack trace similar to the trace printed in
Java programs when an exception is not caught. Notice that the program is
currently deep in several system function calls. Since the system code is
correct (at least we hope!), use the
command to move to the last function called in our code. Note that gdb can
print the exact line of the segmentation fault. Examine this line
carefully, and fix the error.
Try recompiling and rerunning. The program now reads values correctly, but
returns an incorrect average. Use gdb to debug and fix the program by
examining the output values of
To do this, either set a breakpoint using the line number, or set a
breakpoint in the
function, and then continue execution to the end of the function and view
the return values. (To run until the end of the current function, use the