Introduction to VC debugging: Arong

Source: Internet
Author: User

Overview
Debugging is the most basic skill of a programmer. It is more important than learning a language. A programmer who does not debug means that, even if he can speak a language, he cannot compile any good software.
Here I will briefly list the skills that are commonly used in debugging based on my own experience and hope to be useful to you.
This article stipulates that, when selecting a menu, the hierarchical menu is represented by/. For example, file/Open indicates that the sub-menu of the top menu file is open.
 
Set
To debug a program, you must first include debugging information in the program. Generally, the debug configuration in a project created from Appwizard automatically contains debugging information, the program designer can add debugging information in any configuration, including the release version.
To add debugging information, follow these steps:

Open the Project Settings dialog box (you can use the shortcut key Alt + F7 or IDE menu Project/settings)
Select the C/C ++ page, and select general in category. A debug info drop-down list box is displayed. The debugging information options include:
Command Line Project Settings description
No none no debugging information
The/ZD line numbers only target file or executable file only contains the global and exported symbols and code line information, and does not contain the symbol debugging information.
/Z7 C 7.0-compatible target file or executable file contains line numbers and all symbol debugging information, including variable names and types, functions and prototypes.
/Zi program database creates a library (PDB), including the type information and symbol debugging information.
In addition to the previous/Zi functions, the/Zi program database for edit and continue option allows you to modify and continue the code during debugging. This option also invalidates the optimization function set by # pragma.

Select the link page and select the check box "Generate debug info". This option will allow the connector to write debugging information into the executable file and DLL
If the options above program database are set on the C/C ++ page, link incrementally can be selected. By selecting this option, the program can be compiled (that is, incremental compilation) on the basis of the previous compilation, rather than starting from the beginning each time.
Breakpoint
Breakpoint is a code position set by the debugger. When the program runs to a breakpoint, the program is interrupted and returned to the debugger. Breakpoint is the most common technique. During debugging, the program can be debugged online only when a breakpoint is set and the program is returned to the debugger.

Set a breakpoint: you can set a breakpoint in the following ways. First, move the cursor to the line of code that requires breakpoint settings, and then
Press the F9 shortcut key
In the breakpoints dialog box, press Ctrl + B or Alt + F9, or click Edit/breakpoints. Click the arrow on the right of the break at edit box and select the appropriate location information. Generally, it is enough to directly select line XXX. If you want to set a breakpoint that is not the current position, you can select Advanced and enter the function, row number, and executable file information.
Remove breakpoint: move the cursor to the row where the specified breakpoint is located, and press F9 again to cancel the breakpoint. As described above, after the breakpoints dialog box is opened, you can also remove the breakpoint as prompted.

Conditional breakpoint: you can set a condition for a breakpoint. Such a breakpoint is called a conditional breakpoint. For newly added breakpoints, you can click the conditions button to set an expression for the breakpoint. When this expression changes, the program is interrupted. The following settings include "number of elements in the Observation Array or structure". It seems that you can set the size of the memory zone pointed to by a pointer, however, I set a comparison value, but the memory area outside the change range also seems to cause the breakpoint to take effect. The last setting allows the program to be executed several times before the breakpoint is reached.

Data breakpoint: The data breakpoint can only be set in the breakpoints dialog box. Select the "data" page to display the dialog box for setting data breakpoints. Enter an expression in the edit box. When the value of the expression changes, the data breakpoint is reached. Generally, this expression should be composed of an operator and a global variable. For example, if you enter the global variable g_bflag in the editing box, if the program contains g_bflag =! G_bflag, the program stops at this statement.

Message breakpoint: VC also supports interception of Windows messages. There are two methods for interception: window message processing function and specific message interruption.
In the breakpoints dialog box, select the message page to set the message breakpoint. If the name of the message processing function is written in the dialog box above, the breakpoint will arrive every time the message is processed by this function (I think if a normal breakpoint is used to intercept the message in this function, the effect should be the same ). If you select a message from the drop-down list box, the program is interrupted every time the message arrives.

Value
Watch
VC supports viewing the values of variables, expressions, and memory. All these observations must be performed when the breakpoint is interrupted.
The viewing variable value is the simplest. When the breakpoint arrives, move the cursor over the variable and you will be able to see the value of the variable after a while.
VC provides a watch mechanism to watch the values of variables and expressions. In the breakpoint status, right-click the variable and select quick watch. A dialog box is displayed, showing the value of the variable.
Click the watch button on the debug toolbar to display a watch view (wattings, watch2, watch3, and watmethane). Enter a variable or expression in the view, you can observe the values of variables or expressions. Note: This expression does not have any side effects. For example, the ++ operator is definitely forbidden to be used in this expression, because this operator will modify the value of the variable, resulting in the destruction of the software logic.

Memory
Because the pointer points to an array, watch can only display the value of the first element. You can use the memory function to display the subsequent content of an array or a piece of memory. Click the memory button on the debug toolbar. A dialog box is displayed. Enter the address to display the memory content pointed to by the address.

Varibles
The varibles button on the debug toolbar displays the values of all variables visible in the current execution context. In particular, the variables involved in the current command are displayed in red.

Register
The reigsters button on the debug toolbar pops up to display the values of all current registers.

Process Control
VC allows the interrupted program to continue running, run in a single step, and run at the specified cursor, corresponding to the shortcut keys F5, F10/F11, and CTRL + F10 respectively. Each shortcut key has the following functions:
Shortcut Key description
F5 Continues Running
F10 in a single step. If a subfunction is involved, it is not included in the subfunction.
F11: in a single step, if a sub-function is involved, enter the sub-function.
CTRL + F10 run to the current cursor.

Call Stack
The call stack reflects the sequence in which the current breakpoint function is called. Click call stack on the debug toolbar to display the call stack dialog box. The callstack dialog box displays a call series. The top part is the current function, and the top part is the upper-level function that calls the function in sequence. Click these function names to jump to the corresponding function.

Other debugging methods
The system provides a series of special functions or macros to process information related to the debug version, as follows:

Macro name/Function Name Description
The trace usage is exactly the same as that of printf. It outputs debugging information in the output box.
Assert receives an expression. If the expression is true, no action is taken; otherwise, the current program is interrupted. For the interruption caused by this macro in the system, you should consider that your function call does not meet the prerequisites for the system to call this function. For example, setwindowtext is called for a window that has not yet been created.
The Verify and assert functions are similar. The difference is that in the release version, assert does not calculate the value of the input expression, but verify calculates the value of the expression.

Follow
A good programmer should not give all the judgment to the compiler and debugger, and should implement program protection and error locating in the program. Specific measures include:

For all functions with returned values, you should check the returned values unless you are sure that this function call will never go wrong, or you do not care if it is wrong.
Some functions return errors. You need to use other functions to obtain the error details. For example, if accept returns invalid_socket, the accept fails. To identify the cause of the failure, you should immediately use wsagetlasterror to obtain the error code and solve the problem accordingly.
Some functions throw an error through the exception mechanism and use the try-catch statement to check the error.
Programmers should handle the errors they can handle at the underlying level. If they cannot handle the errors, they should report the errors to users so that they can decide how to handle them. If an exception occurs in the program, but the error information returned by the return value and other mechanisms is not judged, the difficulty of error searching is only increased.
In addition, to compile programs in VC, you should not write CPP/H files at the beginning, but create a suitable project first. Only in this way can VC select the appropriate compilation and connection options. For CPP files added to the project, check whether the stdafx. h header file is explicitly included in the first line. This is the pre-compiled header file set by Microsoft Visual Studio to speed up compilation. All code before this # include "stdafx. H" line will be ignored, so other header files should be included after this line.
Because the. c file cannot contain stdafx. H, you can use project settings to set its pre-compilation header to "not used":
The Project Settings dialog box is displayed.
Select C/C ++
Select precompilation header for category
Select not to use the pre-compiled header.
// From vckbase

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.