Detailed Visual Studio debugging interrupt points a few Tips __ Project error Tuning

Source: Internet
Author: User
Tags assert

Breakpoints are important for the Visual Studio debugging process, and breakpoints are set for better debugging. This article describes some of the tips for how to debug a break point in Visual Studio.

In general, function breakpoints are useful in the following situations:

1 For example debugging a website program, you find the most likely error function by analyzing the log of the Web site, opening the debugger and attaching the debugger to the program, setting the function breakpoint, and then performing the website again ... The advantage of this is that you don't have to open the source file to find the wrong source code line, the debugger will automatically open source code, and at the entrance of the function is interrupted (not very convenient?).

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

3 or a special case, you want to read the source code of a program, but you can't find the entry main function, for example. NET program, pressing F11 directly in Visual Studio can help you find the entry function-this is a special case of a function breakpoint.

4 You're debugging a Web service function, setting a function breakpoint is also a quick way to debug, which is similar to tip 1.

Breakpoint Programming

Sometimes you may encounter this situation, after triggering a breakpoint, you will find that you need to modify some values to keep the program running correctly. For example, I used to compile some C # source files containing syntax errors or syntax warnings on the Chinese version of the operating system using the SSCLI (Debug version) of the csc.exe compiler, csc.exe always inexplicably reports internal serious errors and then crashes. When I attach the debugger, I find an assert error, assert (LCID = 0x409), which means that the csc.exe in the SSCLI always defaults to running in the English operating system (or English environment). And this statement is executed many times, and manually modifying the value of the LCID is a bit of a hassle. And then I look for the source code to find it and I don't find csc.exe where to get this LCID value.

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

int LCID = System.Globalization.CultureInfo.CurrentUICulture.LCID; Console.WriteLine ("LCID = {0}", LCID);

The code above should, under normal circumstances, return the LCID value of the current operating system language, for example, English is 1033, Chinese, uh ... I forgot it. 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: Figure 1 set a conditional breakpoint

2 Click Tools-Macros (MACRO)-Macro Explorer (Macro Explorer) in the Visual Studio's menu bar. Then create a new macro:

Imports System Imports EnvDTE Imports EnvDTE80 Imports EnvDTE90 Imports System.Diagnostics Imports Microsoft.vis Ualbasic Imports Microsoft.VisualBasic.ControlChars public Module Module1 Sub changeexpression () DTE.D Ebugger.       Executestatement ("LCID = 0;") End Sub End Module

The above DTE.Debugger.ExecuteStatement function, which you can understand as executing the LCID = 0 in the Immediate window, this statement. 3 Right click on the breakpoint just set, select "When Hit ..." in the menu, this time check "Run a macro: (Execute a macro)" In the breakpoint is Hit window, and then in the dropdown box, select the name of the macro you just created. If you are creating a macro for the first time, the name should be: Macros.MyMacros.Module1.ChangeExpression.

4 Tick "Continue Execution (Continue execution)" because we don't want to interrupt the program.

5 Click to determine after the execution of the program to see the results, is the LCID has been automatically changed to 0? Figure 2 Execution results

Data breakpoint

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 set data breakpoints only on the local computer.

In the example in the previous section, we mentioned, sometimes a global variable has been modified, you may not find when it was modified, so the night has been deep, people have been soundly, you are still hard to debug in the end is what ghost place to change the value of this variable. F11, F10,......,shift + F11,......,f5, rely on, adjusted, again, F11,F10, ...

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

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

1 Open the project you want to debug.

2 Click Tools-Custom (Customize ...) in the Visual Studio menu bar. " Then in the custom (Customize ...) window, select Debug in the category (Categories) list box in the Commands tab, and find the new data Breakpoint (breakpoint) and drag it to the appropriate location in the menu bar.

Then open or create a C + + project, we use the following source code as an example:

#include "stdafx.h" int g_variable = 0;          int _tmain (int argc, _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 that the program will be interrupted in the _tmain function, we will have the opportunity to set the data breakpoint.

2 Click on the "New Data Breakpoint (breakpoint)" inside the menu. Note that the data breakpoint is implemented by monitoring a section of the memory address change, so you must provide a memory address (or pointer bar), where g_variable is an integer variable, so you need to use the form of "&g_variable" to create a data breakpoint,  Because the size of the shape is 4 bytes, the area that the data breakpoint watches is 4 bytes. Figure 3 Breakpoint Monitoring Area

3 to continue the execution of the program, a dialog box pops up telling you that there is a change in the content of the memory address (which means that our data breakpoint is in effect), and the line of code points to the next line of code where the data is modified. Figure 4 Running Hints

Why data breakpoints can only be set in the C++/C program because managed code has garbage collection. The execution principle of data breakpoints should be the implementation of the guard pages concept and the Virtualprotectex function in Windows memory management. This concept allows you to check the MSDN memory management documentation for yourself.

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.