Goals for this week:

  1. Practice with a C program that uses arrays.

  2. Practice with input in C: our read_file library to read from a file.

  3. High-level overview of C programs with command line arguments.

  4. Learn some basic gdb debugging commands. You should start using gdb to help with debugging your Lab 2 code. Over the course of the semester we will revisit using gdb, introducing more commands and features.

  5. Introduce and get started on Lab 2.

1. Copy Over Example Code

Start by ssh’ing into a cs lab machine (see Lab 0 for more details).

Create a week02 directory in your cs31/weeklylabs subdirectory and copy over some files:

$ cd ~/cs31/weeklylabs
$ mkdir week02
$ cd week02
$ pwd
$ cp ~newhall/public/cs31/week02/* ./
$ ls
 Makefile  readfile.c  testfile.c  types_scanf.c  values2
 arrays.c  readfile.h  testprog.c  values1

2. C Arrays

Together we are going to start by looking at arrays.c, which contains examples of how to use (and abuse) arrays in C.

vim arrays.c

Let’s compile and run it next (we can use make to compile):



3. Input in C

3.1. reading input from a file

In testfile.c are examples of reading values from a file using the C read_file library that the CS31 instrctors wrote (implemented in read_file.c and read_file.h).

Information about using the library is documented in comments in the .h file. Let’s open that in one window, and open testfile.c program in another:

vim read_file.h

vim testfile.c

testfile.c has some code to open an input file and to read in an int value from the file. And read_file.h describes the functions provided by the library, some of which are called in testfile.c.

We are going to focus on read_file library functions (read_float and read_int) that read in two different types values from a file once the file is open.

Note that read_int and read_float need to know the memory location of where to put the values read in. We we use the & (ampersand) operator on a variable name to pass this memory location to the functions. We’ll talk much more about what that ampersand means as we build up our C programming skills in future assignments, but for the next lab assignment you will need to use & to pass the memory location of program variables to these functions.

3.2. running a program with command line args

The file read by the program is given as a command line argument when the prorogram is run. To run the program testfile specify the name of the input file on the command line like this:

./testfile <inputfile>    # specify the name of an inputfile on the command line

# examples:
./testfile values1       # run testfile with values1 as its inputfile
./testfile values2       # run testfile with values2 as its inputfile

You can list the contents and see the/format of the values1 and values2 files, using the cat command:

cat values1
cat values2

Refering to the library interface commented in readfile.h, you can add calls to readfile library functions in the testfile.c program to read in the next few values from an input file. Then compile and try running.

vim testfile.c
./testfile values1
./testfile values2

4. GDB intro

gdb is the gnu debugger for C and C++ programs.

Over the course of the semester will will explore gdb features in more depth, but today we will learn just a few basics so that you can start using gdb to help you debug your C lab assignments.

To use the debugger, you usually want to compile your C program with the -g flag to add debugging information to the a.out file (this allows gdb to map binary machine code to C program code that the programmer understands).

$ gcc -g -o testprog testprog.c

The Makefile already has this rule for us, so let’s just run make.

4.1. running gdb

Next, we will run the executable file inside the gdb debugger:

$ gdb ./testprog

The first thing we get is the gdb prompt. At this point testprog has not yet started running.

4.2. example gdb session

We usually begin a debugging session by setting a break point at main before starting the program running in gdb. A breakpoint tells gdb to grab control at a certain point in the execution, in this case right before the first instruction in main is executed:

(gdb) break main
Breakpoint 1 at 0x652: file testprog.c, line 16.

Next, we will enter the run command at the gdb prompt to tell gdb to start running our program:

(gdb) run

The run command will start your program running, and gdb will only gain control again when a breakpoint is hit.

There are a few other primary gdb commands we will learn today. The first is the list command that displays the C source code around the point where we are in the execution:

(gdb) list

list with a line number lists the source code around that line:

(gdb) list 30

The next command (or just n as a shortcut) tells gdb to execute the next instruction and then grab control again:

(gdb) next    # this will execute the instruction x = 10

The print command can be used to print out the value of a program variable or expression:

(gdb) print x
$1 = 10

cont tells gdb to let the program continue running. Since we have no more breakpoints it will run until termination.

(gdb) cont
x = 10 y = 8 z = 0.00
x = 10 y = 8 z = 53.00
[Inferior 1 (process NUM) exited normally]

Now let’s add a breakpoint in the function mystery, and rerun the program:

(gdb) break mystery
Breakpoint 2 at 0x5555555546d5: file testprog.c, line 32.

The run command starts the program’s execution over from the beginning. When re-run, the breakpoint at the beginning of the main function will be hit first (and list displays the code around the breakpoint).

(gdb) run

Breakpoint 1, main () at testprog.c:16
16        x = 10;

(gdb) list

Let’s set a breakpoint at line 20, right before the call to mystery. Next, type cont to continue execution from breakpoint in main:

(gdb) break 20
Breakpoint 3 at 0x55555555468e: file testprog.c, line 20.

(gdb) cont

The program continues running until it reaches the breakpoint we just set at line 20 (Breakpoint 3). We can examine the program’s execution state at line 20 by printing out the argument values before the call to mystery (using print), and then type cont to continue the program’s execution:

x = 10 y = 8 z = 0.00

Breakpoint 3, main () at testprog.c:20
20        z = mystery(x, y);

(gdb) print x
(gdb) print y
(gdb) cont

After continuing, the breakpoint in mystery is hit next (Breakpoint 2), let’s step through some of the mystery function’s execution, and print out some of its parameters and locals.

We can use the print command to print out expressions in the program, so let’s print out the values of the arguments passed to mystery, and type cont to run until the next break point is hit:

(gdb) print a    # print out the value of the variable a
(gdb) print (a - 4) # print out the value of the expression (a - 4)
(gdb) list

The where or bt command list the call stack:

(gdb) where

When you’re done using gdb, type the command quit.

(gdb) quit

4.3. gdb and command line arguments

If you use gdb to help you debug the lab this week, you will need to give the run command a commandline argument that is the file name:

$ gdb ./testfile
(gdb) break main
(gdb) run values1

In general, for programs with command line arguments, simply list the arguments after the run command, for example to run with 3 command line arguments (6, 4, and hello), do the following:

(gdb) break main
(gdb) run 6 4 hello

We will learn more about C and gdb over the course of the semester, but these gdb basics are enough to start using gdb to help you debug your C programs.

5. Lab 2 Intro

Lets talk through the next Lab 2 Assignment, where you will implement a C program that, among other things, uses arrays, command line arguments, and reads values in from a file.

6. Handy Resources