References
- The official (and extremely long) Debugging with GDB
- The very long Using GNU’s GDB Debugger
- Brown CSCI0330 six-page gdb Cheatsheet
- The popular Beej’s Quick Guide to GDB
- The printable two-page GDB Quick Reference
- RMS’s tutorial
Stuff to do
Read evaluate loop
Start up gdb and make sure you can do the first problems in last term’s exams. That will make the grading easier for me!
However, be sure you can do the problems without gdb for the exam.
Using a simple program
Download clect2.tar.gz, a gzip tar file illustrating some recent lectures, and untar (tar xfvz) it into your directory. Take a look at the Makefile and notice that (1) it’s simpler and (2) it has a new compiler option -g which causes your programs to be linked with debugging information. Go ahead and make the programs.
Stepping through the array program
Start up gdb on
carray and begin by performing
the “Mostly for fun” commands for appropriate
values for xxx
.
(Use the up-arrow key to recall commands.)
// print (void *)xxxNums // print (void *)&xxxNums[2] // print (void *)(xxxNums+2)
Now use each of the following commands at least once to control gdb.
- list to list the program
- break to set a breakpoint
- run to a break
- step to next statement
- fin to return from a function
- next to go to next statement and avoiding function calls i>
- continue on to the next breakpoint
- disassemble to see the machine code
Crashing the array program
Modify the main
program and so that it passes 1000000 as the
second argument to sumNumsPointers
.
Run it.
You should get a segmentation fault. Unfortunately, you have to enable core dumps to get an image of the crashed program. Do this with the following command.
ulimit -c unlimited
Now you should have core file. (Use the ls command.) You can load the core dump with the following command. After that you can examine its variables. Keep in mind that the core dump is only useful if you have compiled the program with debugging enabled.
gdb carray core
Look at the value of posV
. I’m afraid it is a
bit too large. Try to print *posV
.
Using a more complex program
Now take a look at the cstruct.c program.
Start gdb on
cstruct and set a break at the first
call to puts
.
Run it up to that point.
At this point we’re going to take a look at an old-fashioned
CSCI 202 style linked list from the days when CSCI 202
was taught using C. You should be prepared to follow along with the
instruction. This will involve printing the following values:
NC
, *NC
,
(*NC).name
, NC->name
, etc.
Now add a new state, say Oregon with population 4,093,465, between North Carolina and Rhode Island.