asna.com Sign In
Getting to know Visual Studio 2010's debugger 

Visual Studio 2010’s interactive debugger is very powerful. In talking to many AVR users, however, I’ve come to realize that much of its power can elude even the most sophisticated AVR coder. This article presents a look at some very useful features of VS’s interactive debugger.

Use the Debugger toolbar
You’ll use the Debug top-level menu option from time to time, but the Debugger toolbar provides handy access to several debugger features. I keep mine displayed all of the time. Figure 1a below shows the Debugger toolbar and Figure 1b shows how to display the Debugger toolbar (or any other toolbar).

 
Figure 1a. The Debugger toolbar

 
Figure 1b. How to display the Debugger, or any other, toolbar

Enabling the debugger
To enable using the debugger on your app (which can be any kind of AVR application), you need to ensure that your project is being built in “debug” mode. The easiest way to do this is to use the “Solution Configurations” dropdown in the Standard toolbar (shown below in Figure 2a). This needs to be set to “debug” to be able to use the debugger.

 
Figure 2a. The “Solution Configurations” dropdown in Visual Studio’s Standard toolbar

As an alternative to using the Standard toolbar’s dropdown, you can also go to the Configuration Manager (right click on the solution in the Solution Explorer and click “Configuration Manager…” from the context menu shown), as shown in Figure 2b. With the Configuration Manager, set the “Active solution configuration” to “Debug” to enable debugging.

 
Figure 2b. A solution’s Configuration Manager

Do note that before you deploy your application you should always build the production version of an application with this set to “Release.” Runtime performance, especially for browser-based applications, is adversely affected by deploying “debug” applications.

Using breakpoints
A breakpoint is perhaps the most obvious thing to do with a debugger. There are several ways to set a breakpoint with Visual Studio’s debugger. A way to set a breakpoint is to right click on the line on which you want a breakpoint and select “Breakpoint-Insert Breakpoint” (as shown in Figure 3a). Other ways to set a breakpoint include putting the cursor on the line on which you want a breakpoint and press F9 and using the top-level Debug menu’s “Toggle breakpoint” option.

 
Figure 3a. Setting a breakpoint with the context menu

No matter how you set a breakpoint, when you do set a breakpoint, a red circle is shown in the left margin’s gutter to indicate a breakpoint is set on that line (Figure 3b). Also know that you can toggle the breakpoint by double-clicking this area of a line’s left-margin gutter.

 
Figure 3b. The red circle that indicates a breakpoint has been set

Once you’ve set a breakpoint, at runtime, the program pauses before executing the line of code on which the breakpoint is set.  While in this breakpoint “pause,” you can hover the mouse over variables to see their value, as shown in Figure 3c.

 
Figure 3c. A program paused on a breakpoint

When a breakpoint is hit, you can press F11 to step into a function call or F10 to step over it. If you step into a function but want to quickly get yourself back up the program call stack, press Shift+F11 to step out of a function call, as shown in Figure 3d.

 
Figure 3d. Stepping into, over, and out of code

Once you have a breakpoint set, you can also set special behaviors for that breakpoint. For example if you right-click on a breakpoint’s red circle in the left margin’s gutter and select “Condition” you can provide a Boolean test to use to indicate if the breakpoint should be fired (as shown in Figures 3e and 3f).

 
Figure 3e. Adding breakpoint behaviors

 
Figure 3f. Adding a Boolean condition for a breakpoint

Debugging record buffer contents with AVR
While the program is paused at a breakpoint, you can easily view the current record for a given file. Hover the mouse over a file name or format name in your source code and you’ll notice that record formats are displayed under the file name. Click the plus/minus sign next to the record format to see the contents of the record buffer, as shown in Figure 4. Be sure to understand that when you view any program values with the debugger, they aren’t just read-only; you can change those values right then and there. Be especially careful with this feature if you’re dealing with production data! (But no rational programmer would ever do that! Right?).

 
Figure 4. Debugging record buffer contents with AVR

Using watches
When the program is paused in debug, you can right-click a variable to open a “Watch” window that provides a modeless window showing the values of all variables for which you are “watching.” In addition to that modeless window, you can also select “Quick Watch” which opens a temporary modal window showing the value of the variable (as shown in Figure 5a).

 
Figure 5a. Using Watch and Quick Watch windows.

While watches and quick watches are handy, they are quite old fashioned. A great way to see a variable’s value is to right-click a variable (when the program is paused in debug mode) and select “Pin To Source, as shown in Figure 5b.

 
Figure 5b. Using Pin To Source

This facility puts a tiny little watch window right in your source, that floats next to the variable as you scroll the source member (as shown in Figure 5c). This is very handy. And remember, like all other watch windows, you can change that variable’s value here.

 
Figure 5c. A Pin-to-Source window

There is another very subtle way to bring up a Pin To Source window. As we’ve previously mentioned, you can hover over a variable while the program is paused in debug to see that variable’s value. In addition to that variable’s value, there is also a little map pin displayed (as shown in Figure 5d). Clicking that map pin is a shortcut to displaying the Pin To Source Window.

 
Figure 5d. Using the Pin To Source shortcut

Using tracepoints
The final VS2010 feature I’ll cover is tracepoints. Tracepoints aren’t near as universally useful as breakpoints, but in certain situations—especially debugging big loops—tracepoints are very handy. A tracepoint lets you write something, a variable’s value for example, or the call stack, to the Visual Studio output window at runtime. To set a tracepoint, right-click on the line of code on which you want the tracepoint set, as shown in Figure 6a.

 
Figure 6a. Setting a tracepoint

After selecting “tracepoint” from the context menu, the dialog in Figure 6b is displayed. You’ll notice that several macros are provided that display system-type info. You can also display a variable’s value by bracketing the variable name within braces (as shown in Figure 6b).

 
Figure 6b. Setting the values for a tracepoint to emit during runtime

After having set a tracepoint, at runtime, whatever you specified in the tracepoint is displayed in the VS output window as shown in Figure 6c.

 
Figure 6c. Tracepoint results shown in the VS output window.

This overview of the Visual Studio 2010 debugger hasn’t been exhaustive by any means. We encourage you to poke into the deep, dark corners of the debugger. Effective use of the debugger will save you time and effort.


 

     

Keywords: