- 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
Stuff to do
Read evaluate loop
Start up gdb and make sure you can do the first problem of the first homework assignment. 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
(Use the up-arrow key to recall commands.)
// print (void *)xxxNums // print (void *)&xxxNums // 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
- continue on to the next breakpoint
- disassemble to see the machine code
Crashing the array program
main program and so that it passes 1000000 as the
second argument to
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
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
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:
Now add a new state, say Oregon with population 4,093,465, between North Carolina and Rhode Island.
One more try
Finally, we are going to “borrow” a lab from the University of San Francisco. You can start by opening a window on GDB lab. This is really just a second pass at what we just done. In order to do this lab, you do need to execute the following two commands to download the needed files. You could do this from the browser, but that is not near as geeky. Also, this uses wget, a useful program for web spiders.
wget https://usfca.instructure.com/courses/1298668/files/54917006/download tar xfv download
Now go to the USF CS-326 GDB lab description and try it out. This will be very similar to what you did earlier, but you need to try again. Second time is a charm! By the way, there will be some differences in the names used in the description and the names of the files. The easiest way to fix this is by executing the following commands.
cd gdb_examples mv p1.c ex1.c mv p2.c segfault.c mv p3.c memory.c
By the way, pay attention to the mysterious x (from examine) command. It is frequently used by gdb experts.