When you press F9 in Visual Studio—or select Debug|Toggle breakpoint—a little red circle representing the breakpoint is placed in the left-hand column of the editor window adjacent to the line of code containing the break. Breakpoints used to be a literal interrupt 0x03 inserted in the code quietly. I am not sure if that is how Visual Studio ultimately does it way underneath the covers, just thought I’d throw that bit if Jeopardy trivia out there.
A variation on the Breakpoint is called a Tracepoint. A Tracepoint is represented as a red diamond in the left hand margin. You can insert a Tracepoint by right-clicking on a line of code and selecting Breakpoint|Insert Tracepoint. This option is the same as clicking an existing Breakpoint in the left margin and selecting the When Hit Count… (You can also access this feature from the Breakpoints window.) When you insert a Tracepoint the When Breakpoint Is Hit dialog is displayed (see Figure 1). If you click Print a message then you can use pre-defined constants like $FUNCTION or $TID to write trace information to the Output window. $FUNCTION represents the current function, and $TID is the thread id. (Other special keywords include $CALLER, $ADDRESS, $CALLSTACK, and $PID (process id). In the same dialog—see Figure 1—you can leave Continue execution checked, and the ‘When Breakpoint is Hit’ feature will act just like a Debug.Trace statement in your code. The final thing you can do is check Run a macro. Run a macro has a dropdownlist of existing macros you can choose from to run when the Tracepoint is hit. Visual Studio also has a Macro IDE, and you can code your own macros. If you write a macro then your macro (or macros) will also show up in the dropdownlist for the Run a macro section.
Figure 1: The Tracepoint dialog is part of the Visual Studio Debugging environment.
Given the (default) settings shown in the When Breakpoint Is Hit in Figure 1 and the code in Listing 1, the Output window in the IDE will contain the content shown in Figure 1.
Listing 1: A simple ‘Jello Mold’ style demo with the Tracepoint as configured in Figure 1.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TracePointDemo
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("This is a test!");
Console.ReadLine();
}
}
}
Figure 2: The Tracepoint writes the literal text and replaces the special keywords with their values, in this instance the function name and thread id is displayed in the Output window.