CSCI 235 — GDB


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 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.

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.

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.

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.