Share via


Debugging the .NET Framework Sample Application

In general, debugging a .NET Framework application by running it inside DbgClr requires several steps. Before running the debugging sample, you must first compile the application and its components. If you would like to stop execution at a particular statement, you should perform the following five steps.

  1. Start the debugger.
  2. Set the program to debug.
  3. Open the source code file(s).
  4. Set the breaking conditions.
  5. Begin execution with debugging.

Another approach to debugging, which is useful when you want to step through the program from the beginning, is to perform the following three steps.

  1. Start the debugger.
  2. Set the program to debug.
  3. Press the F10 key.

At this point, the debugger tries to locate the source code for you. If the source code is still at the location that it was compiled from, the debugger finds it automatically (based on the path that is written in the PDB file) and breaks on the first line of the Main procedure.

To break into the sample program using the first method, which allows you to halt execution at a particular statement, perform the following five steps.

  1. To use the Microsoft CLR Debugger, run the DbgClr.exe program, which is located in the \GuiDebug subdirectory under the installation directory for the .NET Framework SDK.
  2. Select Program to Debug from the Debug menu to display the Program to Debug dialog box. Navigate to and select either the Visual Basic or Visual C# version of Calc.exe, and click OK.
  3. Open all three source code files, either: A) calc.vb and parser.vb in the \Calc\VB subdirectory, and math.vb in the \Calc\VB\Math subdirectory or B) calc.cs and parser.cs in the \Calc\CS subdirectory, and math.cs in the \Calc\CS\Math subdirectory.
  4. Use one of four ways to set a breakpoint just inside the btnEqualsClicked method of Calc.cs: press F9, press CTRL+B, click in the right margin, or select the line and choose New Breakpoint from the Debug menu.
  5. Begin debugging the application by pressing F10 to step through Calc.vb or Calc.cs. Alternatively, you can let the program run by pressing F5. In either case, you eventually produce a small calculator form. Using the calculator keys, enter a simple formula, such as 7+8, and click the Calculate button. Execution then passes to the debugger, which will break on the breakpoint you previously set in step 4.

The following figure illustrates stepping through the Visual C# version of the Calculator program using the debugger:

You can examine variables in the Locals window. If the window is not visible, choose Windows from the Debug menu, and choose Locals. You can also rest the mouse pointer over code to see values. If a particular variable cannot be evaluated, you might see cannot perform implicit function evaluation in this context.

At this point, you can step further through the code by repeatedly pressing F10. When stepping through the following Calc.vb or equivalent Calc.cs source code statement:

txtFormula.Text = m.GetResult(Convert.ToInt32(a.Arg1), 
     a.Op, Convert.ToInt32(a.Arg2)) ' Visual Basic .NET

-or-

txtFormula.Text = m.GetResult(Convert.ToInt32(a.Arg1), 
   a.Op, Convert.ToInt32(a.Arg2)); // C#

You will also walk through code in Parser.cs/Parser.vb (for ParseUtil.Arguments) and Math.cs/Math.vb (for Math.IntegerMath).

See Also

Breaking into the Debugger | Trace and Debug Classes | Debugging and Optimization | The Microsoft CLR Debugger | Debugging ASP.NET Web Applications | Appendix A: For Additional Information | Appendix B: Runtime Debugger (CorDbg.exe)