CS61C Summer 2012 Lab 2 - C Pointers and GDB

Goals

This lab will introduce you to the C debugger gdb. You will gain some practical experience using the gdb to debug a buggy C program, and then you'll write a C program that gdb will be helpful in debugging if you don't get it right on the first try.

Setup

Copy the contents of ~cs61c/labs/02 to a suitable location in your home directory.

$ mkdir ~/lab02
$ cp -R ~cs61c/labs/02/ ~/lab02

Exercises

Exercise 1: Debugger

For this exercise, you will find the GDB reference card useful. Compile hello.c with the "-g" flag:

$ gcc -g -o hello hello.c

This causes gcc to store information in the executable program for gdb to make sense of it. There are two ways to start the debugger:

  1. Run gdb <filename> from the command line.
  2. In EMACS, type M-x gdb, then type gdb <filename>

Single-step through the whole program by:

  1. setting a breakpoint at main
  2. giving gdb's run command
  3. using gdb's single-step command

Type help from within gdb to find out the commands to do these things, or use the reference card.

More gdb commands
Learning these commands will prove useful for the rest of this lab, and your C programming career in general. Create a text file containing answers to the following questions:

  1. How do you pass command line arguments to a program when using gdb?
  2. How do you set a breakpoint which only occurs when a set of conditions is true (e.g. when certain variables are a certain value)?
  3. How do you execute the next line of C code in the program after a break?
  4. 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?
  5. How do you continue running the program after breaking?
  6. How can you see the value of a variable (or even an expression) in gdb?
  7. How do you configure gdb so it prints the value of a variable after every step?
  8. How do you print a list of all variables and their values in the current function?
  9. How do you exit out of gdb?

Checkoff

Exercise 2: Debugging a buggy C program

You will now use your newly acquired gdb knowledge to debug a short C program! Consider the program ll_equal.c. Compile and run the program, and experiment with it. It will give you the following result:

$ gcc -g -o ll_equal ll_equal.c
$ ./ll_equal
equal test 1 result = 1
Segmentation fault

Now, start gdb on the program, following the instructions in exercise 1. Set a breakpoint in the ll_equal() 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. Pay attention to the pointers a and b in the function. Are they always pointed to the right address? Find the bug and fix it.

Checkoff

Exercise 3: Pointers and Structures in C

Here's one to help you in your interviews. In ll_cycle.c, complete the function ll_has_cycle() to implement the following algorithm for checking if a singly-linked list has a cycle.

  1. Start with two pointers at the head of the list. We'll call the first one tortoise and the second one hare.
  2. Advance hare by two nodes. If this is not possible because of a null pointer, we have found the end of the list, and therefore the list is acyclic.
  3. Advance tortoise by one node. (A null pointer check is unnecessary. Why?)
  4. If tortoise and hare point to the same node, the list is cyclic. Otherwise, go back to step 2.

After you have correctly implemented ll_has_cycle(), the program you get when you compile ll_cycle.c will tell you that ll_has_cycle() agrees with what the program expected it to output.

Checkoff