Introduction to Visual C ++ program debugging methods

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
    None None No debugging information
    /Zd Line Numbers Only The target file or executable file only contains the global and exported symbols and code line information, excluding the symbol debugging information.
    /Z7 C 7.0-Compatible The target file or executable file contains the line number and all symbol debugging information, including the variable name and type, function and prototype.
    /Zi Program Database Create a library (PDB), including type information and symbol debugging information.
    /ZI Program Database for Edit and Continue In addition to the previous/Zi functions, this 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 Continue running
F10 In a single step, if a sub-function is involved, the sub-function is not entered.
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
TRACE The usage is exactly the same as that of printf. It outputs debugging information in the output box.
ASSERT It receives an expression. If the expression is TRUE, there is no action. 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.
VERIFY Similar to the ASSERT function, 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.
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.