Boston University -- Computer Science Department


Introduction to debuggers: gdb

Please save the following files:

main.c

QueueTypes.h

QueueInterface.h

QueueLib.c

StackTypes.h

StackInterface.h

StackLib.c

makefile

Now get into emacs by calling

%e main.c&

Compile the program by M-x compile

Emacs will suggest using make -k for compilation, agree by hitting return.

If you try to run this program, the output you get will be pretty amazing. This program contains 2 errors.

Find the first error by using lint. In the shell window type

%lint main.c | less

less will show you the first page of its diagnostics. Use it to locate the first error and correct it. Comment this place in the program to show what mistake you discovered and how you corrected it. Do not forget to quote the relevant lint's error message.

Now, in order to find the second error you need to run gdb. In the emacs window type

M-x gdb

Emacs will inquire how to run gdb, suggesting the beginning of command as gdb. You need to complete the command by typing in main and hitting return. The emacs window will split into 2, and gdb will start waiting for your commands. By typing

run

you will run the program inside the debugger. The program will ask you for input data, give it a short string. After this the program will crash with the favorite message:

Segmentation fault (core dumped)

Besides this message it will give you information in which function and at which line it crashed. In order to analyze this information you can give a command list to gdb. gdb will show you the context of the failure: several lines around the line where the failure occured. If you want to see the stack of the function calls that led to this failure, you can use a gdb command where.

Most probably this information will be insuffient for you to determine what an error is. Now, you can use gdb to help you. First, even though you know in which function the failure occurs, this function turns out to be inside of the loop. Thus, your goal is to find out which exactly execution of this function has such a sad ending. You can do it by setting a breakpoint, i.e.

b functionname

Now the debugger will stop each time you reach this function. At this moment you can use one of the three commands

c to continue the execution.

n to go to next line, stepping over the function calls.

s to go to next line, stepping into the function calls.

If you missed some useful step and want to start anew, do not quit the debugger. Type run and confirm your wish to begin again. This can be useful when you make changes in you program and recompile it. Do not forget to recompile your program after you made the changes.

When you find the mistake, correct it. Comment this place in the program to show what mistake you discovered and how you corrected it. Run gdb to make sure your program works. Check all important cases. Now, quit gdb and then emacs.

You need to hand in a script showing the modules you corrected with the comments mentioned above, the compilation using make main and with sample runs of the program with the following data:

abcd

g

The second line represents an empty queue and this case should also be tested.

I hope that in your future life as programmers you will master these important tools and they will help you to cut debugging time.

Enjoy,

Irene


This page is under construction.

Last updated on March 7, 1996.