DOCX

Debugging technique in VS2010

By Howard Ortiz,2015-09-02 07:31
10 views 0
Debugging technique in VS2010

    Debugging technique in VS2010

    This is my blog about VS 2010 and. NET 4 series of article 26.

    Today's blog post will introduce some practical debugging technique in Visual Studio.This is my friend Scott Cate (he published dozens of great VS technology article).He recently told me that many programmers under Visual Studio, and even some very experienced developers, I don't know these techniques.Hope this article can help you to master these skills.They are very simple, but it can help you save a lot of time.

    Jump to the current cursor (Ctrl + F10)

    I often see people in order to reach the target code, set breakpoints, and early in the program and then repeatedly press F10 / F11, step by step, go to the target code.When programmers do need to watch every step of the change of state, F10 / F11 is a reasonable choice.However, in most cases, people just wanted to quickly reach the code they really care about, F10 / F11, it is not the best choice at this moment.

    At this time, you should take advantage of "jump to the current cursor place" this feature.Put the cursor on the goal line of code to test, and at the same time press Ctrl and F10, tested the program will jump straight to the bank to stop.You don't have to press the F10 / F11 many times.Even if the target code in separate class or method, you can still from the current is checking the place to jump over it.

    Conditions of the interrupt

    Another common situation is that the developer to set breakpoints, run the program, using different input trigger breakpoints, and then the breakpoint manual check whether meet the certain conditions, so as to decide whether to continue investigation.If the current scene is not what they want, press F5 to continue to run the program, try other input, manually repeat process just now.

    According to the above situation, Visual Studio provides a far more convenient features, interruption of conditions.Only when the program is to satisfy the developers default conditions, conditional breakpoint will be triggered, the debugger.This will avoid frequent manual inspections/recovery program is running, a substantial reduction in the manual and chores in the process of debugging.

    How to set up a conditional breakpoint

    Breakpoint is very easy to set conditions.In a particular line, press F9 to set breakpoints.

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

    Then pop up a dialog for you to set the conditions required to activate the breakpoint.For example: we hope only when the size of the local variable paginatedDinners less than 10, debugging to interrupt.We can write the following expression:

    Now I'll run the program, the realization of the search, only when the return value is less than 10 program is running will be

    interrupted.For the value of more than 10, the breakpoint will be skipped.

    Record the breakpoint

    Sometimes you want to, only when the NTH meet the conditions for running the breakpoint, interrupt program is running.For example: when the fifth return less than 10 dinner query results, interrupt program is running. By right-clicking breakpoints, then in the pop-up menu select "Hit count..."Menu command.

    When system pop up a dialog, which allows you to specify: (1) when meet the conditions, and when you enter a breakpoint cumulative number is equal to N, breakpoint is hit again.(2) when meet the conditions, and when you enter a breakpoint cumulative number is a multiple of N, the breakpoint is hit again.(3) when meet the conditions, and when you enter a breakpoint cumulative number larger than N, hit a breakpoint at a time.

    Filtering machine/thread/process

    Set as follows: right-click the breakpoint;In the pop-up menu select "Filter..."Menu command.Then specify the specific conditions of hit a breakpoint, specified in the specified on the machine, or process, or in the specified thread.

    Trace point - into the breakpoint custom operations

    Many people don't know "tracking point (TrackPoints)" the debugging capabilities."Trace" is a kind of special breakpoint, when it is hit, it will trigger a series of custom actions.If you want to observe the behavior of the program, but don't want to interrupt debugging, this feature is especially useful.

    I will use a simple console application to demonstrate how to use the "track".The following is the Fibonacci sequence of a recursive implementation:

    The above program, we use the Console. WriteLine () output for a specific input value generated by the Fibonacci sequence in the end.If you want to watch in the debugger after each recursive operations in the operating sequence without

    actual interrupt program is run, what should you do?"Trace point" can easily implement.

    Set the trace point

    You can be in a particular line, press F9 trace point.then

    Right-click the breakpoints in the context menu select "When Hit...":

    In the pop-up dialog box, you can set hit the breakpoint, the trigger events.

    In the example above, once we set a breakpoint to print information tracking.Note that we have the value of the local variable "x" as part of the tracking information output.Local variables can be output by {variable name} syntax.You can also use the built-in commands ($CALLER, $CALLSTACK, $FUNCTION, etc.), commonly used debug output value in the tracking information.

    In the example above, we also selected at the bottom of the "continue execution" option, that means we don't want to interrupt debugging state, but continue to run.The only difference is that: each breakpoint condition is met, our custom tracking information will be output.

    Now when we run the program, you'll find the custom tracking information displayed automatically in the Visual Studio "output" window.The recursive call process is easy to see our program:

    You can also choose to add a custom application tracking information of the listener.When tracking point through which the output of the information will be output, rather than a Visual Studio "output" window.

    Trace point - run custom macros

    Last week, during a speech in London, I have asked the audience: when hit trace point, can be automatically output all of the local variable?

    Visual Studio and there is no such built-in functionality, but we can write a custom macros to implement, and then call the macro when hit trace point.The realization of the need to open the macro editor of Visual Studio (tools - > macro - > macros IDE menu command), and then under the project explorer MyMacros

    node to select a module or create a new module (such as: add a module called "UsefulThings"), and then post the VB macro code below and save the module.

    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 above macro code will be circulating current stack, put all of the local variable output to the "output" window.

    Using custom "DumpLocals" macro

    Then, we can use in the following a simple program just custom "DumpLocals" macro:

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

    Will display the following dialog.And is not the same as before, we don't choose "Print a message" option, nor the manual setting need output variables;But select the "Run a Marco" check box, and specify to we created above UsefulThings. DumpLocals macros:

    In order to make the program can still continue to run after hitting trace point, we will continue to select "continue execution" check box.

    To run the program

    Now press F5 to run the program, when the "Add" method is invoked, we will be in Visual Studio "output" window to see the results as follows.Pay attention to hit trace point, the macro will automatically list the names and values of each local variables:

    conclusion

    Visual Studio debugger function is extremely rich, I strongly recommend that you take the time to master all the features of it.These skills are all usually pay little attention to a small portion of those features.

    Before I wrote a post about VS 2010 debugger improvements (including fixed data hint window, import/export breakpoint, retain the last time the value of a variable, and so on).Later I will publish more about VS 2010 new intellisense and debugging information file dump support posts.These techniques give developers a powerful debugging ability, make debugging process (including after the release of the product) easier, more powerful.

    If you want to learn more about Visual Studio, please refer to Scott Cate of Visual Studio 2010 series of knowledge and skills.He published a series of wonderful free video and articles.

    Can also refer to Jim Griesmer Visual Studio debugging technique series.He has a wealth of knowledge available for everybody to learn.

    I hope it can help you.

Report this document

For any questions or suggestions please email
cust-service@docsford.com