Goals for this week:
Practice with a C program that uses arrays.
Practice with input in C: our read_file library to read from a file.
High-level overview of C programs with command line arguments.
Learn some basic
gdbdebugging 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.
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).
week02 directory in your
and copy over some files:
$ cd ~/cs31/weeklylabs $ mkdir week02 $ cd week02 $ pwd /home/you/cs31/weeklylabs/week02 $ 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.
Let’s compile and run it next (we can use
make to compile):
3. Input in C
3.1. reading input from a file
testfile.c are examples of reading values from a file using the C
read_file library that the CS31 instrctors wrote
read_file.c and read_file.h).
Information about using the library is documented in comments in
.h file. Let’s open that in one window, and open
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
We are going to focus on
read_file library functions (
read_int) that read in two different types values from a file once the
file is open.
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
./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
values2 files, using the
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 make ./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
gdb to help you debug your C lab assignments.
To use the debugger, you usually want to compile your C program with the
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
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:
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
list command that displays the C source code around the point where we
are in the execution:
list with a line number lists the source code around that line:
(gdb) list 30
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
(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 Continuing. x = 10 y = 8 z = 0.00 x = 10 y = 8 z = 53.00 [Inferior 1 (process NUM) exited normally] (gdb)
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.
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,
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
cont to continue
the program’s execution:
Continuing. 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
(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
bt command list the call stack:
When you’re done using
gdb, type the command
(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
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
(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
Chapter 3 on gdb