Using the Debugger in Visual Studio

posted Jun 10, 2011, 9:52 AM by admin@ revitviet.vn
 
 Visual Studio and the Express editions all have a very powerful debug tool which features a step-through debugger, watch list and call stack amoung other features.
The most basic of the debug environment are the breakpoints. When the program is run in debug mode the application will pause and return to the editor when it reaches a breakpoint. This will allow you to see what the program is doing at that stage of execution. You can step through the code to see where the flow of execution moves; you can inspect variables, view the call stack and evaluate expressions.

Breakpoints

Breakpoints can be set in a number of ways, the easiest of which is to click on the gutter (the grey area to the left of the code editor). This will make the line highlight red with a circle in the gutter. You can only set a breakpoint on a valid line of executable code. You can also right click on the code line and set a breakpoint or you can use the Debug menu to add breakpoints. They can be removed in the same way as you added them.

When you run the program in debug mode (F5) the program will pause just before the breakpoint and return to the code editor window. From here you can now investigate the cause of the problem, either by interrogating the variables or stepping through the code. You can see the contents of a variable, object or class by hovering over the variable with the mouse. The editor will popup a window containing detailed information about the object. We will cover interrogating variables more in the "Watches" section below.

You can step through the code in a number of different methods, you can step through line by line using F11, step over using F10 or step out using (Shift+F11).

Step Through: Each and every line of code executed will be debugged. When a method call is invoked the flow enters the method and returns to the calling line after it has completed.

Step Over: As above, however you will not debug internal method calls. This is a better debug tool if you already know that a method is working and just wasn’t to call it without debugging.

Step Out: If you entered a method using Step Through, Step Out will return you to the point that method was called.

Have a play with the debugger using this code:

using System;
 
class Program
{
  static void Main()
  {
    int a = testMethod1();
    int b = testMethod2();
    Console.WriteLine(a + b);
  }
 
  public int testMethod1()
  {
    int x = 1;
    int y = 2;
    return (x * y);
  }
 
  public int testMethod2()
  {
    int x = 5;
    int y = 10;
    return (x + y);
  }
}

Set a breakpoint on the call to testMethod1 in Main. Start debugging using F5 and step through the program. Have a go at investigating variables with the mouse. Step through the code and see how the flow moves into testMethod1, back to main and then into testMethod2.

Close the program and start debugging again, this time use the step over and notice the difference? The debugger only stops on each line of Main, but steps over the code inside the methods.

Start debugging again (last time) and step into again. When you get inside testMethod1, use step out to return to the main method.

Conditional Breakpoints

Breakpoints can be set to only stop when a condition is met. By right clicking on the breakpoint you can add conditions so that if the condition is not met then the debugger will not stop at the breakpoint. You can also tell the debugger to perform additional actions when a breakpoint is triggered, like displaying a message or running a macro. You can also view the "hit count" which is useful when debugging a iteration or recursive method.

Watches, Auto's and Locals

While in debug mode you can add a "watch" which will display the value of the selected variable into the watches window and you can see how the value changes as you step through the code. This is particularly useful for debugging a recursive or iterative method as you can see how all the values interact, or look at the loop counter and so on.

Auto's and locals are automatically generated watch lists and contain the variables available in the current context.


Comments