Breakpoint techniques for Visual Studio debugging

Source: Internet
Author: User

I don't know if it can be a skill. I just wrote it out and put it on the homepage.

Function breakpoint

PreviousArticleVisual StudioDebugging to avoid single-step tracing debugging modeI have explained how to set a function breakpoint. To be honest, I personally like to set a function breakpoint, insteadCodeSet a breakpoint in the row. In general, function breakpoints are useful in the following situations:

1.For example, debugging a websiteProgramYou can analyze the website logs to find the most likely function to have an error. Open the debugger and append the debugger to the program. Set the function breakpoint and re-execute the website ...... The advantage of this is that you do not need to open the source file everywhere to find errors.Source codeLine. The debugger automatically opens the source code and interrupts the function entry (isn't it convenient ?).

 

2.For example, when you read the source code, you usually read the virtual function call, because this call is usually called through the base class pointer, at half past one, you don't know which inheritance class exists.OverloadingThe function will be called, and the function breakpoint will tell you.

 

3.Or in a special case, you want to read the source code of a program, but you cannot find the entry.MainFunction, such. NetProgram, then directlyVisual StudioPressF11You can find the entry function.-This is a special case for function breakpoint.

 

4.For example, you are debuggingWeb ServiceIt is also a quick debugging method to set function breakpoints. This skill and Skill1Similar.

Of course, some readers may not be able to successfully set the function breakpoint. If the function breakpoint fails to be set, please read my article"The checkpoint check procedure cannot be set.". If there are some terms in it (refer to the article for terms:Debugging terminology) If you do not know or do not know how to set it up, er, I will write another article to explain it.

Breakpoint Programming

sometimes you may encounter this problem. After a breakpoint is triggered, you find that you need to modify some values to make the program continue to run correctly. For example, I used to use sscli in the Chinese operating system (debug version) csc.exe when the compiler compiles source files that contain syntax errors or syntax warnings C # , csc.exe reports internal severe errors inexplicably and crashes. When I append the debugger, it is an assert error, assert (lcid = 0x409) , sscli csc.exe always runs in the English Operating System (or English environment) by default. In addition, this statement is executed many times. It is indeed troublesome to manually modify the value of lcid . I can find the source code but cannot find csc.exe Where can I obtain the lcid lcid value (of course, I finally found it, the next tip will show you how I found it), but I don't want to restart the system (uh ...... Maybe I would rather spend 1 find the person who saves 5 restart the system in minutes ......).

How nice is the value of the lcid automatically reset by the debugger? Fortunately, Visual Studio provides methods for you to do this. The following is a simplified code, because I cannot find sscli at half past one:

IntLcid = system. Globalization.Cultureinfo. Currentuiculture. lcid;

Console. Writeline ("Lcid = {0 }", Lcid );

 

under normal conditions, returns the lcid value of the current operating system language, for example, the English version is 1033 ...... I forgot. Assume that what we want to do now is, whenever lcid , it is automatically corrected to 0 . We need to:

1.InConsole. writelineSet a condition breakpoint on this line. For more information about how to set a condition breakpoint, seeVisual StudioBreakpoint for debugging (advanced tutorial):

2.ClickVisual StudioIn the menu bar (Tools)"-"Macro (Macro)"-"Macro Resource Manager (Macro Explorer)". Then create a new macro:

ImportsSystem

ImportsEnvdte

ImportsEnvdte80

ImportsEnvdte90

ImportsSystem. Diagnostics

ImportsMicrosoft. VisualBasic

ImportsMicrosoft. VisualBasic. controlchars

 

Public ModuleModule1

SubChangeexpression ()

DTE. Debugger. executestatement ("Lcid = 0 ;")

End Sub

End Module

 

AboveDTE. Debugger. executestatementAs you can understandLcid = 0;This statement.

 

3.Right-click the Set breakpoint and select"When hit...", This time in"When breakpoint is hit"Window, select"Run a macro:(Execute a macro), and then select the name of the macro you just created in the drop-down box. If you create a macro for the first time, the name should be:Macros. mymacros. module1.changeexpression.

4.Check "continue execution (Continue execution) ", Because we do not want to interrupt the program.

5.Click "OK" and run the program to view the result,LcidHas it been automatically changed0?

Data breakpoint

Note that this technique is only applicableC ++Effective program debugging (orNativeProgram), and you can only set the data breakpoint in the interrupt mode, and you can only set the data breakpoint on the local machine.

In the example in the previous section, we mentioned that sometimes, after a global variable is modified, you may not be able to find out when it was modified, so the night is deep, you are still working hard to debug which ghost has changed the value of this variable.F11,F10,......,Shift + F11,......,F5Come on, call it, come back,F11,F10,......

In this case, the data breakpoint is very useful,Visual StudioIs it cool to allow you to interrupt program execution when variables are modified?

By default, you cannot find the data breakpoint menu. You need to perform the following steps to pull it out:

1.Open the project you want to debug.

2.ClickVisual StudioIn the menu bar (Tools)"-"Custom (Customize...)". Then (Customize...In the ")" window, select "command (Commands) "Type (Categories) "Debugging (Debug), Find the new data breakpoint (New data breakpoint), Drag it to the corresponding position in the menu bar.

Then open or createC ++Project. The following source code is used as an example:

# Include "Stdafx. H"

 

IntG_variable = 0;

 

Int_ Tmain (IntArgc, _ tchar * argv [])

{

 

Printf ("Before modifying data breakpoints" N");

 

G_variable = 1;

 

Printf ("After modifying data breakpoints" N");

 

Return0;

}

 

We wantVisual StudioBefore changingG_variableInterrupt the execution of the program.

1.ClickF11In this way, the program will_ TmainWhen the function is interrupted, we have the opportunity to set data breakpoints.

2.Click "new data breakpoint (New data breakpoint)". Note that the data breakpoint is implemented by monitoring the memory address changes in a certain area. Therefore, you must provide a memory address (or pointer). HereG_variableIs an integer variable, so you need to use& G_variableTo create a data breakpoint, because the integer The size is4Bytes, so the data breakpoint monitoring area is4As shown in:

3.To continue the program execution, a dialog box will pop up, telling you that the content of a memory address has changed (indicating that our data breakpoint has taken effect ), at this time, the line of code points to the next line of code whose data is modified. Why is the next line of code? The next article will discuss:

Er, why is data breakpoint only available inC ++/CCan be set in the program? It is because the managed code has garbage collection. The principle of data breakpoint execution should beWindowsIn memory managementGuard pagesConcepts andVirtualprotectexFunction implementation. You can check this concept by yourself.MsdnMemory Management documentation.

Related Article

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.