MSVC 5.0 Debugger Tutorial

First, we'll have you download and compile a sample program. We'll then help you learn to use the debugger to watch the program work.

Set up a workspace.

Let's begin by doing a little setup. First, open MSVC by double-clicking the MSVC 5.0 icon on the desktop. Follow these steps to build a new workspace:

  1. Click here to download a sample workspace with a sample program for you to work on. When the 'Save as...' dialog box comes up, save the file DEBUG_1.EXE in the temp folder of the C: drive.
  2. Open the Windows Explorer, go to the temp folder where you just save the file, and double-click DEBUG_1.EXE to extract the files you need for the tutorial.
  3. When this is done, you should have 7 files in the temp folder. Double- click HW2.dsw to open MSVC with the sample program.
  4. Compile the program by choosing 'Build hw2.exe' from the 'Build' menu, or alternatively, you can simply click the build button:

Using the debugger.

To start out, just run the program a couple of times to see how it behaves. This is a simple car insurance program that isn't that hard to understand.  For this to make sense, you will want to do the insurance calculations by hand and compare with the results the program gives.  There are (at least) two bugs that you should notice right away.  First, you should notice that no matter what type of vehicle you say that you have, you are charged for the sports car rate.  Also, you notice that if you have both accidents and violations, the premium is computed correctly, but if you have only accidents or violations, then you are not penalized properly.  

Here is a quick overview of how a typical debugging session takes place. You start by telling the debugger where in your code you'd like it to interrupt your program's execution so that you can examine the state of its variables. This is done by setting break points. Next, you start your program in a special way so that the debugger has control over its execution. When your program reaches one of your breakpoints, its execution stops and your source code is displayed with an indication of which statement will be executed next.  At this point you can examine the current values of any or all variables, and then execute statements one at a time while you watch your variables change.

Here are some steps you can follow to do this yourself:

  1. Start by setting a breakpoint. A good place to start is the first executable line of main(), which is a call to the printf() function and prints the opening greeting for the program. Now, click the break point button: . Notice that a red dot appear in the left hand margin of your source code. This tells you that a breakpoint has been set.
  2. Click the Go button to run your program so that the debugger controls it. Note that you cannot click the execute button to use the debugger. If you do, your program will not be paused at the breakpoint you selected.
  3. Your program should begin to execute. You will see the console window pop up, and then your program source will be displayed with a yellow arrow pointing to the line in your code where the execution was stopped. You are now in the debugger.
  4. Start out by examining the state of the variables in main(). The fastest way to do this is to look in the debugger variable window in the lower-left corner of the MSVC window (if it is not visible, pull down the 'View' menu and select 'Debug windows' -> 'Variables').

    Take a close look at this box-- this is really useful information. At the bottom of the Variables window, select the Locals tab. It now lists all of the local variables for this function (main, at this point) along with their values. Resize this box and the box for hw2_soln.c so you can see both at the same time.   What value to these variables have?

  5. Now we'll start to use the debugger. You can now execute just one instruction, and then re-examine your variables to see what changed. You do this by "stepping " through the program. There are several flavors of step; the first one we'll learn is Step over. Do this now by clicking the Step over button once. Notice that the yellow arrow moves to the next executable statement, which is another printf().    Click the Step over button again, and we'll be at a scanf().   This statement will read in the value for vehicle_type.  Look at its current value, which hasn't been initialized to anything.  Click Step over again, which will bring up the console window asking you to enter the type of vehicle. Enter 1 for sports car, which will bring you back to the debugger. Look at the value of vehicle_type now. The debugger always displays variable that have just changed in red, to help you see that some action has taken place.
  6. Continue to use Step over to watch the program check the value of vehicle_type and ask the user whether the vehicle has anti-lock brakes (tell it yes).  Stop when the next executed statement will be an assignment to the variable premium.
  7. The program cursor (the yellow arrow) now points to an assignment statement, where premium is assigned the return value of a call to vehicle_fee(). This time, we're going to go into the function call to watch it work. Click the Step into button . You will now find yourself at the first line of the vehicle_fee() function. For the rest of this function, we'll go back to stepping with the Step over button.

    Look at the variables box again. Note that the list of variables shown has changed, and now include only the local variables and parameters for this function. Note the value of fee, then use the Step over button to watch the program find the fee for a sports car and apply the ABS discount.  When you get to the bottom of the function, click Step over one more time and you'll return to main().  Click again, and you should see the value of premium change to the value that vehicle_fee() calculated.

  8. There is another way that you can see the value of your variables.  Hold the mouse pointer over any variable name in your source code (premium is a good one to try at this point.).  A small yellow box should appear at the mouse pointer telling you the value of the variable.
  9. Click again to print out the coverage menu.
  10. The yellow arrow now points a line containing a call to to the function scanf. Since you didn't write this function and you don't have the source code, you don't need to see what it does. In fact, if you hit Step into by accident (and you will, eventually), you will be taken into some really ugly and incomprehensible machine code. We're going to show you how to recover when you do this by accident.
  11. Click Step into now.

    What happened? Depending on your computer's setup, you probably got a long delay followed by something you didn't recognize and don't understand. You might have gotten dis-assembled machine code, or if you were lucky, you got to see some of the source code that Microsoft wrote to implement the scanf function. Either way, you need to get back to Kansas where Aunty Em is keeping you program. To do this, click the Step out button . Step out finishes whatever function you're currently in and takes you back to wherever you were when the function call was made. In this case, your program window should pop up and ask you to select a coverage level.   Enter one, and you will be once again land back in your source code. 

  12. Continue stepping through the program using the tools we've shown you how to use and watching the program work. Can you see how this would help you find places where your program did not behave as expected?

    If you have stepped through enough of the program and just want it to continue running, simply click the go button again. This will cause execution to continue from the current statement until it reaches another breakpoint, or the end of the program is reached.

    Another useful button to know about it the Stop debugging button . You might use this if, as you were stepping through your code, you found the problem you were looking for. You can click Stop debugging to immediately halt the execution of your program so you can make changes, recompile, and try again.

At this point, you should have enough tools to let you make good use of the debugger. It is worth pointing out that you can place breakpoints almost anywhere you like.   Since you already know that the adjustments to the fee for accidents and traffic violations are not calculated correctly, you might guess that it would be good to put a breakpoint on the first executable line (executable lines are the ones after the variable declarations) of the function driving_adjustments().   Do this, and restart the debugger.  Use the step commands and the variable windows to discover the problem with the program. 

There are certainly other useful debugger features that you could use, but this is enough to get you going. To learn about more of these, try searching for debugging commands from the help menu.

 

Additional resources to help you learn the debugger