CS61C Summer 2013 Lab 2 - C Pointers and GDB

Goals

Setup

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

$ mkdir ~/lab02
$ cp ~cs61c/labs/02/* ~/lab02/.

Exercises

Exercise 1: Debugger

For this exercise, you will find the GDB reference card useful. As a hint, PDF documents are text-searchable.

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 (or write on paper) 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 only 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 just once?
  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?

GDB in practice:

Now let's load a program into GDB to test out the above commands and get some hands-on experience! The file hello.c outputs what you'd expect it to, but also has some stuff going on in the background. 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>

To get a sense of what GDB looks like and how it works, you can start by:

  1. setting a breakpoint at main()
  2. giving GDB's run command
  3. using GDB's single-step command and testing out the different commands at different points in the program

For command reference, you can type help from within GDB or use the reference card.

For checkoff, make sure you can demonstrate the following within GDB:

  1. Load hello.c into GDB
  2. Show the value of argc in main() as an int
  3. Show the value of argv in main() as an address
  4. Stop the program at the point where ch is the comma character and show this

Check-off

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 initially give you the following result:

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

Now start the program in GDB. 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.

Check-off

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 in ll_cycle.c will tell you that ll_has_cycle() agrees with what the program expected it to output. There are SIX different tests that should match without any runtime errors.

Check-off