Debugging Lab #3
More gdb Techniques and Debugging Code that uses Pointers

Answer Sheet


Name ______________________________                                                         Lab Section _______________

Setting a Conditional Breakpoint
A little farther down in the help text, you will see:
CONDITION is ________________________ (fill in the rest of the line).

For what value of N does the output value start to give incorrect output? _____________

Based on what you have learned so far, write the full gdb command to set a conditional breakpoint at the proper source code line.  ________________________

This time, have the breakpoint stop when num is equal to 120. Write the command to set the breakpoint: ____________________________

Set the breakpoint in gdb and continue running the code using the continue command. Write the last line output before the (gdb) prompt returns: ____________________

Use the gdb command to display the num, i and j variables.  Write all of their values:   num = ___________    i = ________    j = ______________

Assuming there is no bug, what will the value of num be after executing the statement num += j?    num = ______________
Execute a step command in gdb.  What is the actual value of num now?  num = ______________
Why is the value what it is?   _____________________________

Do you get the proper output now? _______________

Finding the Seg Fault
Notice that gdb tells you exactly the line of code where the seg fault occurs. What line is it? ___________________

Print the variable dl3 using the print command. Write the values of i and c: _________________

Now print the value of dl2 using the following command:
(gdb) print dl2
What is its value? ___________________

Note that dl2 is a pointer, so a memory address is printed. To see the contents of the memory pointed to by that address, use the following command:
(gdb) print *dl2
What are the values of i and c? ___________________

Now, let's look at the actual line of code causing the seg fault. Using gdb, print the value of dl1 using the following command:
(gdb) print dl1
What is its value? ___________________

Note that dl1 is a pointer. Now, try to dereference dl1 and print its value using the following command:
(gdb) print *dl1
Write the message given: _______________________________________________

Now its Your Turn
Exit gdb and try running the Lab9 executable using 4 as the command line parameter:
bash$ ./Lab9 4
What happens?  ______________________________

Now, use the techniques you have learned to find the error in the code using gdb.

What line does the segmentation fault occur?  ___________________

What is the value of i at the point of segmentation fault?  ___________________

Once you find the source of the problem, copy the line of code that causes the problem below:

__________________________________________

Fix the code, recompile and run the executable. Write the output of the correctly running executable below:

_______________________________________________________