7.3 DebuggingTracing provides you with a snapshot of the steps your code has taken after the code has run. At times, however, you'd probably like to monitor your code while it is running. What you want is more of a CAT scan than an autopsy. The code equivalent of a CAT scan is a symbolic debugger. When you run your code in the debugger, you can literally watch your code work, step by step. As you walk through the code, you can see the variables change values, and you can watch as objects are created and destroyed. This section will provide a brief introduction to the most important parts of the debugger that accompanies the Visual Studio .NET IDE. For complete coverage of how to use the Visual Studio .NET debugger, we urge you to spend time with the documentation and to experiment freely. The debugger is one of the most powerful tools at your disposal for learning ASP.NET. 7.3.1 The Debug ToolbarThere is a Debug toolbar available in the IDE. To make it visible, click on the View/Toolbars menu commands, then click on Debug, if it is not already checked. Table 7-3 shows the icons that appear on the Debug toolbar.
7.3.2 Breakpoints
Breakpoints are at the heart of debugging. A breakpoint is an instruction to .NET to run to a specific line in your code and then to stop and wait for you to examine the current state of the application. While the execution is paused, you can:
7.3.2.1 Setting a breakpointA breakpoint is set in the Source window (any Source window, such as page file, control file, or code-behind) by single-clicking on the gray vertical bar along the left margin of the window. A red dot will appear in the left margin and the line of code will be highlighted, as shown in Figure 7-6. Figure 7-6. BreakpointAn alternative to clicking in the left margin is to select the Debug/New Breakpoint... menu command. Clicking on the File tab will bring up the dialog shown in Figure 7-7. The text boxes will already be filled in with the current location of the cursor. Figure 7-7. New breakpoint dialog boxThe four tabs in the dialog box in Figure 7-7 correspond to the four types of breakpoints, which are described in Table 7-4.
7.3.2.2 Breakpoint windowYou can see all the breakpoints currently set by looking at the Breakpoint window. To display the Breakpoint window, perform any one of the following actions:
A Breakpoint window is shown in Figure 7-8. Figure 7-8. Breakpoint windowYou can toggle a breakpoint between Enabled and Disabled by clicking on the corresponding checkbox in the Breakpoint window. 7.3.2.3 Breakpoint propertiesSometimes you don't want a breakpoint to stop execution every time the line is reached. VS.NET offers two properties that can be set to modify the behavior of a breakpoint. These properties can be set in either of two ways:
In either case, you will see the dialog box shown in Figure 7-9. Figure 7-9. Breakpoint properties dialog boxThe fields at the top of the Breakpoint Properties dialog box will default to the location of the current breakpoint. The two buttons allow access to the Condition and Hit Count properties. 7.3.2.3.1 ConditionThe Condition button brings up the dialog shown in Figure 7-10. Figure 7-10. Breakpoint Condition dialog boxYou can enter any valid expression in the edit field. This expression is evaluated when program execution reaches the breakpoint. Depending on which radio button is selected and how the Condition expression evaluates, the program execution will either pause or move on. The two radio buttons are labeled:
7.3.2.3.2 Hit countHit count is the number of times that spot in the code has been executed since either the run began or the Reset Hit Count button was pressed. The Hit Count button brings up the dialog shown in Figure 7-11. Figure 7-11. Breakpoint Hit Count dialog boxClicking on the drop-down list presents the following options:
If you click on any option other than "break always" (the default), the dialog box will add an edit field for you to enter a target hit count. Suppose this is a breakpoint set in a loop of some sort. You selected "break when the hit count is a multiple of" and entered 5 in the edit field. Then the program will pause execution every fifth time it runs. 7.3.2.4 Breakpoint iconsThere are several different breakpoint symbols, or glyphs, each conveying a different type of breakpoint. These glyphs appear in Table 7-5.
7.3.2.5 Stepping through codeGo to the code-behind file in the example (either WebForm1.aspx.cs or WebForm1.aspx.vb, depending on your language). Place a breakpoint on the call to the Add method of the DropDownList control's Items collection, the line in the Page_Load method where the items are added to the DropDownList. Set the Hit Count to be a multiple of 5 (break always when hit count is a multiple of 5). Then run the program. The breakpoint will be hit, and the program will stop execution at the line of code containing the breakpoint, which will turn yellow. The glyph in the left margin will have a yellow arrow on top of it. The VS.NET screen should look like Figure 7-12. Figure 7-12. Breakpoint hitYou can now move forward a single statement or line at a time, stepping into any methods or functions as you go, by using one of the following techniques:
You can step through the code without going through called functions or methods. That is, you can step over the calls rather than into the calls, using one of the following techniques:
Finally, if you are debugging in a called method or function, you can step out of that method or function call, using one of the following techniques:
To change the granularity of the stepping (that is, step by Line, Statement, or Function), select a different value for Step By, either in the Debug Toolbar or at the Debug/Step By menu command. 7.3.2.6 Examining variables and objectsOnce the program is stopped, you can examine the value of objects and variables currently in scope. This is incredibly intuitive and easy. Just place the mouse cursor over the top of any variable or object in the code, wait a moment, and a little pop-up window will appear with its current value If the cursor is hovering over a variable, the pop-up will contain the type of variable, its value (if relevant), and any other properties it may have. If the cursor is hovering over some other object, the pop-up window will contain information relevant to its type, including its full namespace and syntax, and a descriptive line of help. 7.3.2.7 Immediate windowThe Immediate window allows you to type almost any variable, property, or expression and immediately see its value. To open the Immediate window, do any of the following:
You can enter expressions for immediate execution in the Immediate window. If you want to see the value of an expression, prepend it with a question mark. For instance, if the breakpoint is on the line shown in Figure 7-12, you can see the value of the integer i by entering: ?i in the Immediate window and pressing Enter. Figure 7-13 shows the result of that exercise; additionally, this figure shows the process of assigning a new value to the variable i and then viewing its value again. Figure 7-13. Immediate windowYou can clear the contents of the Immediate window by right-clicking anywhere in the window and selecting Clear All. Close the window by clicking on the X in the upper-right corner. If you close the window and subsequently bring it back up in the same session, it will still have all the previous contents. 7.3.2.8 Autos windowThe Autos window shows all the variables used in the current statement and the previous statement, displayed in a hierarchical table. To open the Autos window, do any of the following:
A typical Autos window is shown in Figure 7-14. Figure 7-14. Autos windowThere are columns for the name of the object, its value, and its type. A plus sign next to an object indicates that it has child objects that are not displayed while a minus sign indicates that its child objects are visible. Clicking on a plus symbol expands out the tree and shows any children, while clicking on a minus symbol contracts the tree and displays only the parent. You can select and edit the value of any variable. The value will then display as red in the Autos window. Any changes to values take effect immediately. 7.3.2.9 Locals windowThe Locals window is exactly the same as the Autos window, except that it shows variables local to the current context. By default, the current context is the method or function containing the current execution location. To open a Locals window, do any of the following:
7.3.2.10 This/Me windowThe C# This window and the VB.NET Me window are exactly the same as the Autos window, except that they show all objects pointed to by this in C# and Managed C++ and by Me in VB.NET. To open a This/Me window, do any of the following:
7.3.2.11 Watch windowThe Watch window is exactly the same as the Autos window, except that it shows only variables, properties, or expressions that you enter into the Name field in the window or drag from another window. The advantage of using a Watch window is that it allows you to watch objects from several different source windows simultaneously. This overcomes the inability to add object types other than the specified type to any of the other debug windows. To open a Watch window, do any of the following:
In addition to typing in the name of the object you want to watch, you can also drag and drop variables, properties, or expressions from a Code window. Select the object in the code that you want to put in the Watch window, then drag it to the Name field in the open Watch window. You can also drag and drop objects from any of the following windows into the Watch window:
In order to drag something from one of these windows to the Watch window, both the source window and the Watch window must be open. Highlight a line in the source window, then drag it down over the Watch tab. The Watch window will come to the foreground. Continue dragging the object to an empty line in the Watch window. 7.3.2.12 Call Stack windowThe Call Stack window displays the names of the methods and functions on the call stack, as well as their parameter types and values. You can control which information is displayed in the Call Stack window by right-clicking anywhere in the window and toggling field names that appear in the lower portion of the pop-up menu. To open a Call Stack window, do any of the following:
7.3.2.13 Threads windowThe Threads window allows you to examine and control threads in the program you are debugging. Threads are sequences of executable instructions. Programs can be either single-threaded or multithreaded. The whole topic of threading and multiprocess programming is beyond the scope of this book. To open a Threads window, do any of the following:
7.3.2.14 Modules windowThe Modules window allows you to examine the .exe and .dll files that are being used by the program being debugged. To open a Modules window, do any of the following:
A Modules window is shown in Figure 7-15. Figure 7-15. Modules windowBy default, the modules are shown in the order in which they were loaded. You can re-sort the table by clicking on any of the column headers. 7.3.2.15 Disassembly windowThe Disassembly window shows the current program in assembly code. If you are debugging managed code, such as that which comes from VB.NET, C#, or Managed C++, this will correspond to Microsoft Intermediate Language (MSIL) code. A Disassembly window is shown in Figure 7-16. Figure 7-16. Disassembly windowUnlike the previous windows discussed in this chapter, the Disassembly window displays as a tabbed item, as part of the main Source code window. You can set breakpoints anywhere in the window, just as for any other Source code window. To open a Disassembly window, do any of the following:
7.3.2.16 Registers windowThe Registers window allows you to examine the contents of the microprocessor's registers. Values that have changed recently are displayed in red. To open a Registers window, do any of the following:
You can select which pieces of information to view by right-clicking anywhere in the Registers window and clicking on the information you would like displayed. 7.3.2.17 Memory windowsThere are four Memory windows available for viewing memory dumps of large buffers, strings, and other data that will not display well in any other window. To open a Memory window, do any of the following:
7.3.3 ConfigurationAn application can be configured to either enable or disable debugging. This is done through the web.config file, which is described more fully in Chapter 200. Web.config is an XML file, and as such it must be well-formed. The file consists of sections delimited by tags. The debugging configuration information is contained within the <compilation> section, within the <system.web> section, which in turn is contained within the <configuration> section. So a typical compilation configuration snippet will look something like Example 7-8. Example 7-8. Debug configuration code snippet from web.config<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.web> . . . <compilation debug="true" /> Note that setting debug to false improves the runtime performance of the application. |