/* COP3014L -- Debugging Exercise, based on AT&T Network Bug */ /* See handout a
ID: 642534 • Letter: #
Question
/* COP3014L -- Debugging Exercise, based on AT&T Network Bug */
/* See handout associated with this program for more information */
/* Note: this is not intended to be a good example of coding style. */
/* Last edited: 31/8/2011. */
#include <iostream>
using namespace std;
void make_unitialized(void);
int phone_network(int i, int x, int y);
void init_values_x1(void);
void init_values_part1A(void);
void init_values_part1B(void);
void init_values_part2(void);
void init_default(void);
/* global variables that control phone network */ int val1, val2;
/*--------------------------------------------------------- main */
/* main -- a simple driver function that reads three values,
which are passed to the function phone_network, which succeeds
of fails. Certain combinations of values will call phone_network
to fail.
*/
int
main (void)
{
int i, x, y;
cout << "Enter 3 integers: i, x and y: ";
while ( cin >> i >> x >> y ) {
/* call this to fake that variables are unitialized */
make_unitialized();
if ( phone_network(i, x, y) == 1 )
cout << "Phone network OK! " << endl;
else
cout << "Phone network crashes! " << endl;
cout << "Enter 3 integers: i, x and y: ";
}
cout << "You didn't enter 3 valid integers -- program exiting." << endl;
return 0;
}
/*------------------------------------------------------route_that_call */
/* returns 1 if system intialized OK, 0 if not */ int
route_that_call(void)
{
if ( val1 >= 0 && val2 >= 0 )
return 1;
else
return 0;
/* Note: you should never write the above logic as an if statement;
instead write:
return ( val1 >= 0 && val2 >= 0 );
This does the same thing in one line. See why?
*/
}
/*-------------------------------------------------------phone_network */
/* phone_network -- initialize global control values, then route call.
* This function takes 3 values: the first, i, is used to select
* which initialization function is called. The 2nd and 3rd
* values, x and y, are used to calculate values for the global
* control values. (They're also used in calculating initial values
* for the control values.
* After doing the initialization, function route_that_call is
* called to do the "real" work of the phone network system.
*/
int
phone_network(int i, int x, int y)
{
const int i1=1, i2=2, val1=3, val2=5;
switch (i) {
case i1:
init_values_x1();
break;
case i2:
/* for this case, intialization split between several functions */
if (x == val1) {
init_values_part1A();
if (y == val2)
break; /* drop out of the if and do init_values_part2() */
init_values_part1B();
}
init_values_part2();
break;
default:
init_default();
}
/* call function to make the phone call -- it returns 1 for success */
return route_that_call();
}
/*-------------------------------------------------------make_unitialized */
/* For this exercise, we'll pretend negative values mean the system
is not initialized properly.
*/
void
make_unitialized(void)
{
val1 = val2 = -999999;
}
/*---------------------------------------------------------init_values_x1 */ void
init_values_x1(void)
{
val1 = 3;
val2 = 5;
}
/*-----------------------------------------------------init_values_part1A */ void
init_values_part1A(void)
{
val1 = 1;
}
/*-------------------------------------------------------init_values_part1B */ void
init_values_part1B(void)
{
++val1;
}
/*-------------------------------------------------------init_values_part2 */ void
init_values_part2(void)
{
val1 = 6;
val2 = 7;
}
/*-------------------------------------------------------init_default */ void
init_default(void)
{
val1 = 1;
val2 = 1;
}
2: Study the logic of the code. Answer the following questions:
Question 1: For what values does the function init_values_x1() get invoked?
Question 2: For what values does init_values_part1A() get called?
Question 3: For what values does init_values_part1B() get called?
Question 4: For what values does init_values_part2() get called?
Question 5: For what values does init_default()get called?
Step 3: Find values for i, x and y that make the phone network crash? Hint: Try each set of data values you identified in Step 2 to make the program perform each of the possible initializations. Together these sets of values are good test data, since each one causes the program to behavior differently. They cover all the behavior caused by the different initialization values.
Question 6: What set of 3 values (i,x,y) caused the phone network to crash?
Debugger Section:
Now that you know what data is causing the crash, you may still not know why those data values cause the problem. In the steps that follow you will use the MSVS 2010 debugger and answer the following questions. Build the program with the Build Solution option and then carry out the following steps.
Step 4: Put a breakpoint at the start of the function, phone_network(). Execute the program, by choosing "Debug" and then "Start Debugging".
Question 7: After entering the 3 values, what is displayed when the execution of the program stops at the breakpoint you set at the line that contain the name of the function?
Step 5: Type "Ctrl-F10" to resume execution.
Question 8: Does the program stop at the breakpoint again?
Question 9: How do you remove the breakpoint you set?
Step 6: If you removed the breakpoint, put it back. Execute the program again. When you reach the breakpoint, choose "F11". This executes the program one line at a time. Execute "F11" a few times to see how "F11" works. Type "Ctrl-F10" to continue on until the breakpoint is reached again.
Now that you are back at the initial breakpoint again, do the same thing but this time choose "F10" instead of "F11" to go to the next line. Do this a few times to observe what happens.
Question 10: What differences in the execution of the MSVS commands, "F11" and "F10", did you observe?
Step 7: Using the same breakpoint from Step 6, run the "F11" command to step "into" a function. Now enter "Shift-F11". The "Shift-F11" command terminates the execution of the function and pauses.
Question 11: Where does execution stop when you enter "Shift-F11"?
Step 8: Using the same breakpoint, stop the execution process by choosing "Stop Debugging" and then restart the execution process by choosing "Start Debugging".
Now, in the "Watch" window underneath the code window, enter the variables "val1" and "val2" per each row under Name. Once you've done this, run the "F10"command a few times by typing "F10" repeatedly. Then type "Ctrl-F10" to run the "continue" command.
Question 12: What does MSVS do with "Watch" variables?
Step 9: OK, about that bug.... Set a breakpoint at the line that contains phone_network() or at the switch statement. Enter the 3 data values for i, x, and ythat caused the phone network to crash. From the breakpoint, use the "F10" command to step through the program.
Question 13: Did you observe anything that surprises you about the behavior of the program? Was the execution of any lines skipped that you thought should have been executed?
Question 14: Can you explain the behavior you observed?
Explanation / Answer
For i=1 and any values of x and y, the function init_values_x1() get invoke, because, init_values_x1() is called inside phone_network() function under the switch case of 1 for i. For i=2,x=3 and any values of y, the function init_values_part1A() get invoked, because, init_values_part1A() is called inside phone_network() function under the switch case of 2 for i and under the condition x=3. For i=2,x=3 and y!=5, the function init_values_part1B() get invoked,because, init_values_part1B() is called inside phone_network() function under the switch case of 2 for i and under the condition x=3 and y not equal to 5. For i=2,x=3 and any values of y, the function init_values_part2() get invoked. For any value of I other than 1 and 2, the function init_default() get invoked. The set of 3 values (i, x, y) which causes the phone network to crash are i=2,x=3,y=5. After entering the three values, nothing is displayed when the execution of the program stops at the breakpoint you set at the line that contain the name of the function. The program does not stop at the breakpoint again. A breakpoint can be removed by right click on the breakpoint and delete breakpoint.To disable breakpoint ctrl+F9. The differences in the execution the MSVS commands, "F11" and "F10", is When we use F11, the control will first execute the function and then move into the DebuggerEx internal code of the header file and executes line by line. Where as while using F10, the control will be moving along the program, but does not enter into the header files. When we enter Shift-F11, the control will come back to the start of the function call. Initially val1 and val2 are initializes with the garbage values, then initializes with val1=3 and val2=5.There after val1=3 and then val2=5. There after val1=1 and val2=-999999. While executing, the function to which the breakpoint is applied is not executed and moved to the next statement while tracing by using F-10. The behaviour of the program is that, it has the two local variables, which are modifying for the execution of the different functions.The function phone_network() defined the variables val1 and val2 as constants, Hence values will have two scopes, Local scope and global scope.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.