The Art of Debugging – A Developer’s Best Friend – Lesson 5 – Using TracePoints - Repost

TracePoints

Tracepoints are a new debugger feature in Visual Studio. A tracepoint is a breakpoint with a custom action associated with it. When a tracepoint is hit, the debugger performs the specified tracepoint action instead of, or in addition to, breaking program execution.

We’ll start by setting an ordinary breakpoint in the following event procedure. Next, we will right mouse click on the break point and select “When Hit

image

Notice that the dialogue box that comes up and allows us to perform two basic tasks. One thing that we can do is to print a message. Another thing that we can do is to run a macro. Continue execution simply means that your code will continue to run after performing the task that you specified. Notice that when you print a message there are a lot of valuable line items that you can display. For example, you can display the callstack, the process ID, the thread ID, etc.

We can also add our own variables to the “print a message” section of the “when breakpoint is hit” dialog box. For example, the this keyword refers to the currently executing form, and if we want to display the public properties of the form we will add the following code to our dialogue box text: this={this}.

image

Simply open up the output window to see the result.

image

The output

Function: CSBreakPoints.MainForm.buttonAssertExample_Click(object, System.EventArgs), Thread: 0x2014 Main Thread this={CSBreakPoints.MainForm, Text: C# Breakpoints Demo}

Pam

Notice that in addition to the function name, the thread ID, and the thread name, we also displayed the results of our this object, which printed the text property for the main form.

The Watch Window (AKA Expression Evaluator)

The Watch window is one of the finest pieces of software engineering you'll see

It offers almost infinite flexibility

The most important thing is that in the Watch window and its cousins (Autos, Locals, Quick Watch, etc) you can click on the value of a variable and change it

The awesome new data tips are the Watch window in tool tips

All data is fully editable

Pressing the CTRL key makes the Data Tip window see through

Demo

Open source code to MainForm.cs and navigate to XmlManipulation method and set a breakpoint on the “XmlElement myElement = doc.DocumentElement;” line.

image

Switch to the C# Breakpoints Demo window and click the Default Views button. Move the mouse to the “doc” value on the doc.LoadXml line.

image

You can make direct modifications to variables through the data tip window. As you can see here, I went to the InnerXML element and modified the title of the book.

image