Running Custom Tracepoint Macros in Visual Studio

Introduction


Debugging is the married partner of programming. Programming is hard. Debugging is hard. Doing both well is critical to having a good relationship. The most common features I use in debugging are straight forward breakpoints and the quick watch. (Writing small, Refactored functions seems to limit the amount of time needed for debugging for me, but everyone runs into tenacious bugs some times.) For tenacious bugs the more you know the easier it will be to find the bug, if easy is actually a suitable word. To that end I introduce the Tracepoint.


If you know all about Tracepoints then I am spicing it up—Bam!—with Visual Studio Macros. This article demonstrates the Tracepoint with macros.


A Tracepoint is basically a Breakpoint with options: Tracepoints are designed to print pre-defined special keywords, run a Visual Studio macro, and optionally continue or halt execution. If you uncheck the continue execution option then Tracepoints act like Breakpoints.


Reviewing Tracepoint Basics


When you set a Breakpoint in Visual Studio a small red circle icon is placed in the margin of the IDE. When you set a Tracepoint the icon will appear as a diamond. The easiest way to set a Tracepoint is to right-click on a line of code and click Breakpoint|Insert Tracepoint (see Figure 1). The Insert Tracepoint option is the same as picking the “When Hit” option from the Breakpoints window’s context menu (see Figure 2). However, you insert a Tracepoint the “When Breakpoint is Hit” dialog is used to manage the Tracepoint.

The “When Breakpoint Is Hit” dialog contains the three options for managing Tracepoints. The “Print a message” option behaves like the System.Diagnostics.Debug.Print function in Windows applications, supporting sending text to the Output window. You can use the pre-defined special keywords, like $FUNCTION, to display formatted information like the function and the thread id ($TID). By default the Continue execution option is checked allowing the code to continue running. Uncheck Continue execution (Figure 1) to make the Tracepoint behave like a Breakpoint, stopping execution.


The middle option “Run a macro” let’s you pick from a list of pre-defined macros. If you write a macro in the Macro IDE then your macros will appear in the dropdown too. It is this option that we are most interested in here.


Running a Custom Macro When a Tracepoint is Hit


Visual Studio is fully customizable through the EnvDTE, EnvDTE80, and EnvDTE90 assemblies. You can manipulate and control the IDE by writing wizards or just macros that call into the .NET Framework, especially by using the features of the EnvDTExx (DTE stands for Design Time Environment).


Tip: You can run a macro directly by select Tools|Macro Explorer and double-clicking on pre-defined macros.


Of course, Visual Studio already ships with dozens of pre-defined macros that you can pick from (in the Tracepoint dialog) or use as learning aids to help you implement custom macros. VB programmers have leg up with Macros because the Macro language is basically VB. To open the Macros IDE select Tools|Macros|Macros IDE. Another nice feature of the Macros IDE is that it is a lot like the Visual Studio so the features and uses should be readily recognizable (see Figure 3).

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read