Lab 3 - Using gdb to Debug Pointer Issues

The purpose of this lab is to learn how to use the gdb utility to debug common pointer errors. The writeup for this lab is due at 5pm Tuesday.

The gdb utility is the debugger that comes with the GNU compilers such as gcc and g++. It allows you to step through execution or debug what caused a core dump. View the HowTo Guide for some basic instructions. 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. For this lab, you will be compiling the file lab3.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 /usr/users/mdanfor/public_html/cs222-w07/lab3.cpp .
g++ -g -o lab3 lab3.cpp
You should now have an executable called lab3 which will core dump when run. 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 lab3 core
The first argument to gdb is the executable name (lab3) 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 lab3. 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.

Assignment

Modify lab3.cpp to include the appropriate new statement. Recompile the program using g++ -g -o lab3 lab3.cpp. Verify that the bug has been fixed by running the executable. Now we will run gdb in line-by-line mode. Type the following command:
gdb lab3
Notice we do not include the second option (core) this time since we are NOT debugging a core dump now.

First, we will set a breakpoint at the start of the main function. The program will run until it reaches a breakpoint and then it will return to the gdb prompt. Type the following command:

break main
Then get the program running by typing:
run
When you get the gdb prompt again, type the command
next
This will execute the next line of the code. You will see what the next line of the code is above the gdb prompt. Keep typing next until you enter the size of the array. You should now see:
(gdb) next
Enter size of array: 5
16        a = new int[size];
(gdb)
Now type the command info locals and see the current value of a before the new statement has been executed. Now, type next to execute the new statement and then type info locals to see the value of a after the new statement has executed.

Feel free to try other gdb commands as mentioned in the HowTo Guide. Not all of the commands will apply to this code.

Make a screen copy of your gdb session or write a summary of what you tried during the session and email it to me as your assignment.

FYI: vi/vim Commands

Some of you have noted that this lab room is a bit laggy on responding to commands in vi/vim when navigating your file. Here's a quick list of some handy commands you can use to move around in vi. You can see more commands by viewing the HowTo Guide and vi tricks pages on the department website. These commands work in Command mode but will not work in Insert mode. If in doubt, hit ESC to get back to Command mode.
/blah       Search for the word "blah" from the current position forward
/           Repeat the last search (finds next instance)
?blah       Search for the word "blah" backwards from the current position
?           Repeat the last search backwards (finds previous instance)
15G         Go to line 15
G           Go to the end of the file
5j          Jump down 5 lines
5k          Jump up 5 lines
5l          Move the cursor to the right 5 spaces
5h          Move the cursor to the left 5 spaces
w           Move the cursor to the start of the next word
b           Move the cursor to the start of the previous word
^           Move to the start of the current line
$           Move to the end of the current line
yy          Copy the current line
5yy         Copy the next 5 lines (includes current line)
x           Delete the character under the cursor
5x          Delete the next 5 characters (including the one under the cursor)
dw          Delete the current word
5dw         Delete the next 5 words (including current word)
dd          Delete the current line
5dd         Delete the next 5 lines (includes current line)
p           Paste after the cursor (next line for line-based pastes)
            The paste buffer contains the last delete or copy command
cw          Change the current word, hit escape when done
r           Replace the character under the cursor
.           Repeat last edit command