VS2010 Breakpoint Setting Tips

Source: Internet
Author: User

Many programmers under Visual Studio, and even some very experienced developers, are unaware of these techniques. Hopefully this article will help you master these skills. They are simple, but they can help you save a lot of time.

First, jump to the current cursor (CTRL+F10)

I often see people in order to get to the target code location, and in the program to set a breakpoint early, and then repeatedly press F10/F11, step-to-point to the target code. F10/F11 is a reasonable choice when programmers really need to look closely at each step's state change. In most cases, however, people just want to get to the code where they really care, and f10/f11 is not the best choice.

At this point, you should take advantage of the "jump to the current cursor" feature. First position the cursor on the target code line to be measured, and then press CTRL and F10, the program will jump directly to the line to stop. You don't have to press f10/f11 many times anymore. Even if the target code is in a separate class or method, you can still jump from where you are currently checking.

Ii. condition Interruption

Another common scenario is when a developer sets a breakpoint, runs a program, triggers a breakpoint with different inputs, and then manually checks at a breakpoint to see if certain conditions are met, thus deciding whether to continue the investigation. If the current scene is not what they want, press F5 to continue running the program, try another input, and manually repeat the process just now.

For these scenarios, Visual Studio provides a much more convenient feature-"conditional interrupts". A conditional breakpoint is triggered only if the program satisfies the developer's preset conditions, and the debugger breaks. This avoids the frequent manual Check/recovery program running, which greatly reduces the manual and cumbersome work during the commissioning process.

How to set conditional breakpoints

Setting conditional breakpoints is easy. On a specific line, press F9 to set the breakpoint.

Then right-click on the breakpoint – edit the red dot on the left side of the window and select "Condition ..." on the context menu.


A dialog box pops up to set the conditions you need to activate the breakpoint. For example: We want the debug to be interrupted only if the size of the local variable paginateddinners is less than 10 o'clock. We can write the following expression:


Now I run this program, the implementation of the search, only the return value of less than 10 o'clock, the program will be run to be interrupted. For a value greater than 10, the breakpoint is skipped.

Third, the number of records reached the breakpoint

Sometimes you want the program to run only if it reaches the breakpoint when the nth-time run satisfies the condition. For example, if the query results of less than 10 dinners are returned for the fifth time, the interrupt program runs.
You can do this by right-clicking the breakpoint and selecting the Hit count ... menu command on the pop-up menu.

The system pops up a dialog box that allows you to specify: (1) When the condition is met and the cumulative number of points entering the breakpoint equals N, the breakpoint is hit once. (2) When the condition is met, and the cumulative number of times the breakpoint is entered is a multiple of n, the breakpoint is hit once. (3) When the condition is met, and the cumulative number of points entering the breakpoint is greater than N, each time it is broken.

Iv. machine/thread/process filtering

Set the following: Right-click the breakpoint, select the "Filter ..." menu command on the pop-up menu, and then specify the specific condition to hit: On the specified machine, in the specified process, or in the specified thread.

Track points-Custom actions when entering a breakpoint

Many people do not know "track point (trackpoints)" This debugging function. A "track point" is a special breakpoint that triggers a series of custom actions when it is hit. This is especially useful if you want to observe the behavior of the program and do not want to interrupt debugging.

I'll use a simple console program to demonstrate how to work with track points. The following is a recursive implementation of the Fibonacci sequence:

In the above procedure, we use the Console.WriteLine () output to produce the final Fibonacci sequence for a particular input value. What if you want to see the sequence in the debugger for each recursive operation and not actually interrupt the program? Track points can be easily implemented.

Set up track points

You can press F9 plus track points on a specific line. And then
Right-click on the breakpoint and select "When hits ..." In the context menu:

On the popup dialog box, you can set the event that is triggered when the breakpoint is hit.

In the example above, we set the tracking information to be printed once the breakpoint is hit. Note that we have already put the value of the local variable "x" as part of the trace information output. Local variables can be output through the {variable name} syntax. You can also use the system's built-in commands ($CALLER, $CALLSTACK, $FUNCTION, and so on) to output common debug values in your tracking information.

In the example above, we also checked the "Continue execution" option at the bottom, which means that we do not want the program to break the debug state, but to continue running. The only difference is that our custom tracking information is output each time the breakpoint condition is met.

Now when we run the program, we find that the custom tracking information is automatically displayed in the Output window of Visual Studio. This makes it easy to see the recursive invocation process of the program:

You can also choose to add a listener to your application to customize your tracking information. The output information for the trace point is then output through it, not the Output window of Visual Studio.

V. Track Point-run a custom macro

Can I output all local variables automatically when I hit a track point?

There is no such built-in functionality in Visual Studio, but we can write a custom macro to implement and then call the macro when the trace point is hit. This implementation requires that you first open the macro editor for Visual Studio (tools, macros, macros, IDE menu commands), and then select a module under the MyMacros node of the Project Explorer or create a new module (for example, add a module named "Usefulthings") , and then paste the following VB macro code into the module and save.
Sub Dumplocals ()
Dim OutputWindow as Envdte.outputwindow
OutputWindow = DTE. Windows.item (EnvDTE.Constants.vsWindowKindOutput). Object

Dim Currentstackframe as Envdte.stackframe
Currentstackframe = DTE. Debugger.currentstackframe

OutputWindow.ActivePane.OutputString ("*dumping Local variables*" + vbCrLf)
For each exp as envdte.expression in Currentstackframe.locals
OutputWindow.ActivePane.OutputString (exp. Name + "=" + exp. Value.tostring () + vbCrLf)
Next
End Sub
The macro code above loops the current stack, outputting all local variables to the Output window.

Use a custom "dumplocals" macro

We can then use the newly customized "dumplocals" macro in one of the following simple programs:

In the above code, we use F9 to add a breakpoint at the return value of the "Add" method, then right-click on the breakpoint and select "When hit" on the pop-up menu.

The following dialog box appears. Unlike before, we do not choose the "Print a message" option, nor do we manually set the variables that need to be output, but instead select the "Run a Marco" checkbox and assign it to the Usefulthings.dumplocals macro we created above:

In order for the program to continue running after hitting the trace point, we will continue to select the Continue execution check box.

Run the program

Now press F5 to run the program, and when the "Add" method is called, we will see the following results in the Output window of Visual Studio. Note When you hit a trace point, the macro automatically lists the name and value of each local variable:

Reprinted from: http://blog.csdn.net/jcx5083761/article/details/7770107

VS2010 Breakpoint Setting Tips

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.