Lab 15 - Programming Tools Part II: Using gdb

Material Covered

Part I: Background

The gdb utility is the debugger that comes with the GNU compilers such as gcc and g++. It allows you to step through a program's execution one line at a time or debug what caused a core dump. In order to get the most information from gdb, you must compile the program with debugging info. This is achieved by using the -g option. If the program is compiled with debugging info, gdb can give you information about the program such as line numbers, function names, statements being executed and so on.

Part II: Debugging a core dump

A core dump occurs when the program encounters a run-time error such as divide-by-zero or using a pointer that has not been initialized. For this part, you will be compiling the file lab15.cpp, which has an intentional bug that will cause a core dump. Use the following commands to copy the file over to your current directory and compile it with debugging info:
cp /home/fac/melissa/public_html/cs215/lab15.cpp .
g++ -g -o lab15 lab15.cpp
ulimit -c unlimited
You should now have an executable called lab15 which will core dump when run. The ulimit command will enable the core dump file (named core) to be created. Run the program and verify that you have a core dump by looking for the file core in the current directory. To invoke the debugger type:
gdb lab15 core
The first argument to gdb is the executable name (lab15) and the second argument is the core dump filename (core).

You will now be at the command prompt for gdb. Type the command

bt
This will show you all the function calls that lead to the problem code. Sometimes, as with this example, your code may crash while executing a library function. The bt command will eventually print out line numbers from your code that you'll wish to investigate. At the start of each line, there will be a frame number such as #0, #1, etc.

Find the frame number for the line of code you wish to look further at. In this example, we're interested in looking at frame 1. We can look at frame 1 by typing the command

frame 1

Now we want to see the value for all variables local to the function. Type the command info locals. For this example, you should see something like:

(gdb) info locals
i = 0
a = (int *) 0x0
size = 5
The value for size will be whatever you entered as the size when you ran lab15. Notice the value for a is 0x0. This means that the pointer has not been initialized since there was no new statement after asking for the size. Exit gdb by typing the command quit.

Lab Writeup - Stepping through code

For this part of the lab, you will be compiling lab15_2.cpp, which is a basic C++ menu program which contains functions. Copy it to your current directory, compile it and invoke gdb with the following sequence of commands:
cp /home/fac/melissa/public_html/cs215/lab15_2.cpp .
g++ -g -o lab15_2 lab15_2.cpp
gdb lab15_2
Note that this time we only gave gdb the executable name since we are not debugging a core dump.

In the "Edit my submission" portion of Moodle, copy and paste the following questions. Give the gdb command that would accomplish each task AND the output that gdb produced for lab15_2 when you issued that command. Assume each command is done one after the other in the order given.

gdb tasks

  1. set a break point at line 50
  2. set a break point when the multiply function begins
  3. start program execution
  4. step through the code by one line
  5. step through the code by five lines
  6. view your breakpoints
  7. clear your first breakpoint
  8. list the source code beginning at line 5
  9. display the value of the variable 'in1'
  10. determine the type of the variable 'c'
  11. assign the variable 'in1' the value 3
  12. continue program execution after hitting a break point
  13. print a trace of the frames in the stack
  14. quit the debugger