Visual Studio Debugging Breakpoints Tips

Source: Internet
Author: User

Original link Address: http://blog.csdn.net/Donjuan/article/details/4618717 function Breakpoint

In the previous article Visual Studio debugging avoids single-step tracking debug mode Inside I said how to set a function breakpoint, to be honest, I personally like to set a function breakpoint, rather than in the line of code to set breakpoints. In general, a function breakpoint is useful in the following situations:

1. For example, to debug a website program, you find the most likely error function by analyzing the log of the site, open the debugger and attach the debugger to the program, set a function breakpoint, re-execute the site ... The advantage of this is that instead of opening the source file everywhere to find the wrong source line, the debugger will automatically open the source code and break at the entrance of the function (wouldn't it be convenient?). )。

2. For example, when you read the source code, usually read to the virtual function call, because usually this call is called through the base class pointer, and you 1:30 will not know exactly which inherited class overloading function will be called to, function breakpoint can tell you.

3. Or a special case, you want to read the source code of a program, but just can't find the portal main function, for example. NET program, then pressing F11 directly inside Visual Studio can help you find the entry function-a special case of a function breakpoint.

4. For example, you are debugging a Web service function, setting a function breakpoint is also a quick debugging method, which is similar to the technique 1.

Of course, some readers may not be able to successfully set a function breakpoint, if the function breakpoint is set to fail, please read my article "Cannot set breakpoints Check step". If there are some noun terms (see article: Debugging terminology) If you don't know or don't know how to set it, uh, I'm going to write another article explaining it.

Breakpoint Programming

Sometimes you may encounter this situation, after triggering a breakpoint, you find that you need to modify some values in order for the program to continue to execute correctly. For example, when I used the Chinese version of the operating system, using the SSCLI inside (debug version) of the csc.exe compiler to compile some C # source files containing syntax errors or syntax warnings, csc.exe always inexplicably reported internal critical errors, and then crashed. After I attach the debugger, I find an assert error, assert (LCID = = 0x409), which means that the csc.exe inside SSCLI always runs in the English operating system (or English environment) by default. And this statement will be executed many times, and manually modifying the value of the LCID is indeed a bit of a hassle. And then I found the source code, and I couldn't find it. csc.exe where to get this LCID value (of course I finally found, the next trick will tell you how I found it), however I do not want to restart the system (UH ... Maybe I'm the kind of person who would rather spend 1 to find a way to save 5 minutes to restart the system ... )。

How good would it be if the debugger could automatically reset the LCID value for you at this time? Fortunately, Visual Studio provides a way for you to do this work. Here's a simplified code because I can't find sscli 1:30:

int LCID = System.Globalization.CultureInfo.CurrentUICulture.LCID;

Console.WriteLine ("LCID = {0}", LCID);

The above code, under normal circumstances, should return the LCID value of the current operating system language, such as English is 1033, Chinese, er ... I forgot. Suppose what we want to do now is automatically corrected to 0 whenever the value of the LCID is 1033. We need to:

1. Set a conditional breakpoint on the Console.WriteLine line, and set the conditional breakpoint in the Visual Studio Debug Breakpoint Advanced :

2. Click "Tools"-"macro"-"Macro Explorer" in the Visual Studio menu bar. Then create a new macro:

ImportsSystemImportsEnvDTEImportsEnvDTE80ImportsEnvDTE90ImportsSystem.DiagnosticsImportsMicrosoft.VisualBasicImportsMicrosoft.VisualBasic.ControlChars Public ModuleModule1Subchangeexpression () DTE. Debugger.executestatement ("LCID = 0;")End SubEnd Module

The above DTE.Debugger.ExecuteStatement function, which you can understand as executing LCID = 0 in the Immediate window;

3. Right click on the breakpoint that you just set, select "When hit ..." In the menu, this time tick "Run a macro: (Execute a macro)" in the "when Breakpoint are hits" window, then select the name of the macro you just created in the drop-down box. If you are creating a macro for the first time, the name should be: Macros.MyMacros.Module1.ChangeExpression.

4. Tick "Continue (Continue execution)" As we do not want the program to break down.

5. Click OK, after the execution of the program to see the results, the LCID has been automatically changed to 0?

Data breakpoints

Note that this technique is only valid for C + + program debugging (or native program), and you can only set data breakpoints in break mode, and you can only set data breakpoints on this computer.

In the previous section of the example, we mentioned, sometimes a global variable is modified, you may not find when it was modified, so the night has been deep, the person is awake, you are still hard to debug in the end is which ghost place to change the value of this variable. F11, F10,......,shift + f11,......,f5, leaning, tune over, back, F11,F10, ...

In this case, the data breakpoint is useful, and Visual studio allows you to break the execution of the program when the variable is modified, isn't it cool?

By default, you cannot find the data breakpoint in this menu, you need to follow the steps below to pull it out:

1. Open the project you want to debug.

2. Click "Tools"-"Customize (Customize ...) in the Visual Studio menu bar. )”。 Then in the "Custom (Customize ... ) window, select Debug (Debug) in the "Kind (Categories)" list box in the "Commands (Commands)" tab, find "New data breakpoint" and drag it to the corresponding location in the menu bar.

Then open or create a C + + project, we use the following source code 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"); return 0;}

We now want Visual Studio to interrupt the execution of the program when changing the g_variable.

1. Click F11, so the program will break in the _tmain function, and we will have the opportunity to set the data breakpoint.

2. Click "New Data Breakpoint" in the menu. Note that a data breakpoint is implemented by monitoring a section of memory address changes, so you have to provide a memory address (or pointer bar), where g_variable is an shaping variable, so you need to use "&g_variable" to create a data breakpoint, Because the size of the shaping is 4 bytes, the area monitored by the data breakpoint is 4 bytes, as shown in:

3. Continue the execution of the program, this will pop up a dialog box to tell you that there is a memory address of the content has changed (that our data breakpoint takes effect), then the line of code points to the data is modified the next line of code, why it is the next line of code, the next article will say:

Well, why are data breakpoints only set in the C++/C program? is because managed code has garbage collection. The implementation of the data breakpoint should be the guard pages concept in Windows memory management and the implementation of the Virtualprotectex function. This concept can be used to check MSDN's documentation on memory management.

Visual Studio Debugging Breakpoints Tips

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.