Use Visual C debugger debugging
First, debug version and release version
Sometimes the program can run in the debug version but cannot run on the release version, and it is possible. Generally speaking, a release version means some types of optimization, and a debug version is not optimized. Let's take a look at their differences:
1. Especially for the compilation options for debug versions
(1) / MDD, / MLD or / MTD
The debug version of the runtime library has debug symbols, using debug stacks, the purpose of the debug stack is to discover memory damage and memory leaks, and report the source code from which place to report the problem. characteristic:
The debug version of the runtime library is tracked for memory allocation, allowing users to check memory leaks.
Write the 0xcd byte mode in the just allocated memory, populate the originally assigned memory with 0xCD, helps discover errors that the data is not initialized.
. In the released memory write 0xDD byte mode, helps discover the memory that has been released.
The four-byte protection data is allocated on both sides of the buffer, and is initialized with 0xFD byte mode to check the overflow and underflow of write memory.
The source code file name and line number are recorded in each memory allocation, which helps the user to position memory allocation in the source code.
(2) / OD
This option is used to turn off the optimization switch. Since the code that is not optimized directly corresponds to the source code, it is easier to read more than optimized code. Code compilation and links that are not optimized will be faster, there will be a shorter debug cycle. Due to optimization, the release version is not necessarily running more than the debug version, and the optimization code requires the compiler to do some hypothesis, remove redundancy, but sometimes this assumption is wrong, and the redundancy is also possible to hide errors. If the release version of the frame pointer (EBP register) omission (FPO) hides the error of the function prototype; in the synchronous exception mode (only by the throw statement, the compiler is default, the / gx compile option settings), exception The handler may be optimized, preventing the C exception processing code in the program, safely capturing the structure exception, in which case you must use asynchronous exception mode (taking any instructions, an exception mechanism, by / eHA Compile option settings ).
(3) / d "_debug"
Open the condition to compile the debug code switch. Only this symbol is defined, the debug code will be compiled, the MFC uses the _debug symbol to determine which version of the MFC class library is connected. In the debug version, inline defaults are turned off.
(4) / zi
Create a program database of Edit and Continue. This option opens / gf compile options, / GF compilation options will eliminate repeat strings and put strings in read-only memory. Editing Continuous Function The special information stored in the PDB file is obtained to make the code modification is valid for the debugger. If the information corresponding to the modified file is not in the PDB file, the editing continuation function cannot be performed, and the following prompt information will appear in any modification of the code during the debugging process "One or more Files Are Out of Date or Do Not Exist." .
(5) / gz
The debug version is used to find errors that those who have discovered in the release version. Its role is as follows:
Automatic (local) variables in 0xCC mode.
Check the stack pointer when modifying a function by a function pointer, confirming that there is a call rule does not match.
In the final inspection stack pointer is changed.
(6) / GM
Open minimize re-link switch, reduce link time.
2. Special for publishing options
(1) / MD, / ml or / MT
Use the release version of the runtime library.
(2) / O1 or / O2
Turn on the optimization switch so that the program will minimize or the speed will be the fastest, the optimizer may also discover potential errors in the code, and these errors may be built by the debug version.
(3) / d "ndebug"
Close condition compiles debug code switch.
(4) / GF
Eliminate repeat strings and put them in read-only memory to avoid being incorrectly modified.
(5) / zi
Create a program database that contains debug symbols.
If an error only occurs in the release, unless you are a compilation master, you need to debug symbols to prompt you what is wrong to the program, and debug symbols are saved in the program database file (PDB). Visual C AppWizard is not created for a debug symbol for the publishing version by default. To create a debug symbol, open the engineering setup dialog, select the win32 release, select the common class in the C / C tag, in debug information, if it is a release version Select Program Database, if it is a debug version Select Program Database for Edit and Continue ( Editing Continue options are incompatible with optimized links, not suitable for publishing. Select the Debug class in the Link tag, then select Debug INFO and Microsoft Format options, it is best not to select the Separate Types option, so all debug information will be merged into a separate PDB file. For published versions, select the LINK tab, the final plus "/ opt: ref" in the Project Options dialog, this option makes the function and data that are not referenced, and the data does not appear in the executable, avoiding the unnecessary increase of the file. . Do not use this option for the debug version, it will turn off the incremental link (INCREMENTAL LINKING).
Second, the "Settings" menu of the Visual C editor
When you open or create a Workspace containing at least one project, the "settings ..." command in the Project menu of Visual C will be valid, select it, or press the hotkey Alt F7, you can adjust the project settings. Box, this option will affect the establishment and debugging process of the entire project, so it is important.
In this dialog, the drop-down list box above the left is used to select an engineering configuration, including Win32 Debug, Win32 Release, and All Configurations (together with the first two configurations), some options have different engineering configurations. The default value. The tree view on the left gives all the files and classification of the current project. Let's take a look at the respective functions and meaning of the four main tabs related to the work of Win32 Debug (a total of ten tabs):
1, General tab
This tab is relatively simple, and the first option from the upward is used to change the way using the MFC class library: DLL's way or static connection. We can switch between two ways. The second option is used to specify the memory of the intermediate file and output file generated during the compiled connection. For debug versions, the default directory is the "debug" subdirectory below. The third option is used to specify whether each engineering configuration has its own document dependencies (main point files), because the most engineering debug version and publish version have the same file dependencies, so there is usually no need to change This option is.
2, Debug tab
The DEBUG tab is some options related to debugging. Since the options are more, they are divided into several classes, we can select different categories from Category, and the tab will switch to display the appropriate options. In the General category, you can specify the executable file name to debug. The other three options can specify the working directory for debugging, start debugging command line parameters sent by the program, and the path to executable files during remote debugging.
3, C / C tab
The C / C tab controls the compiler of Visual C , with more options. There is a Project Options edit box below, and the various command switches listed will be transferred to the Visual C compiler when starting compiling. These command switches will change with other options.
In the General category, WARNING LEVEL is used to specify the level of the compiler to display the warning. If WARNINGS AS ERRORS is selected, then each warning that is displayed will cause an error, so that the connector cannot be activated after compiling. . Optimizations is used to set up code optimization. Optimization is mainly two types of running speed and reduce program volume, but sometimes these two goals are contradictory. In addition, in a very small number of cases, the program can operate normally, and the program will have some inexplicable issues after the optimization measures are opened. In fact, this is mostly a potential error in the program, and the optimization measures are often only temporarily solved. DEBUG INFO is used to specify the type of debug information generated by the compiler. In order to use Visual C ie, that is, you must select the debug information of the "Program Database for Edit and Continue" type. Preprocessor definitions is some pre-defined macro names.
The options in the C Language category involve some advanced features of the C language, including representations, abnormality processing, runtime type information, generally do not have to change them. The options in the Code Generation category involve how to generate target code, in general, keep the default value. In the Customize category, the meaning of the top six options is: whether to use Microsoft to the C extension; whether the function level is allowed; whether the duplicate string is eliminated; whether the minimized reconstruction is allowed; Increment compilation; whether the compiler is allowed to output its own version information to the Output window when the compiler is started.
In the Listing Files category, we can specify the compiler to generate a browsing information and list file (Listing file), the former can generate a browsing information file by the browsing information maintenance tool Bscmake, the latter contains the C / C source file after compiling the compilation instruction. The Optimizations category allows us to make more submissive control over optimization, select which item is optimized, in Inline Function Expansion we can specify how the inline function is extended. The PrecompileD Headers category is about some options for the pre-translated header file, and no change is used in general. The Preprocessor category is about some options for pre-processing.
4, LINK tab
The LINK tab controls the Visual C connector. In the General category, you can specify the file name of the output, and some additional library files or target files that need to be used during the connection, the meaning of the five options is: generate debugging information; ignore all default library files; Allows increment connection (this way can speed up the speed of the connection); generate a MAP file; allowing performance analysis. Selected User Program Database in Customize Allows the User Database. In the Debug category, we can specify the classes of the debugging information, or the Coff format, or both, or both, the connector will be subsequently put on the PDB file in the PDB file, which is connected to the PDB file in the PDB file. Some, but the speed of debugging will be slow. Input categories are some options related to the input library file, we can specify or do not use certain library files or target files here. In the Output category, some options related to the final output executable, generally do not change. Third, Visual C debugging tool
1, debug window
(1) Observation window (Watch)
When debugging the program, you can monitor variables and expressions using the observation window.
(2) Quick watch (Quick Watch)
The function and observation window are similar.
(3) Variable window (variables)
The variable window has three tags: Auto tags show the current statement and the previous statement, the local variable displays the local variable of the current function, and the THIS label displays the object executed by the THIS pointer.
(4) Register window (Register)
You can monitor registers, flag values, and floating-point stacks of CPUs.
(5) Memory window (Memory)
Virtual memory starting from a particular address can be displayed. The AddResS box allows you to specify which virtual memory address starts to display.
(6) Call Stack Window (Call Stack)
A range of functions that causing the current source code statement execution can be displayed, and the current function is at the top of the stack.
(7) Disassembly window (Disassembly)
You can view compilers generated by the compiler corresponding to the source code.
2, debug symbol
The program database file (.pdb) contains debugging information and program information required for the Visual C debugger. The debugging information contains the names and types of variables, function prototypes, source code line numbers, classes, and structural layouts, FPO debugging information (reconstructing stack frames), and information needed to increment links. For programs set up the Program Database for Edit and Continue option, the PDB also contains the information you need to perform editing proceeds.
3, use breakpoints
BreakPoint is a mechanism that runs you to describe the debugger and let the debugger set a mechanism. If there is no breakpoint, only one step in the program uses the debugger. In Visual C , you can set three types of breakpoints: code positioning breakpoint, data breakpoint, and message breakpoint.
Fourth, improve the error of the debugger
Try to check the compile time instead of the runtime check.
1. Use the highest compilation warning level / w4
IF (x = 2) This statement, the default warning level does not display any information, but it will appear "WANING C4706: Assignment WITHIN CONDitional Expression" when it is changed to the highest warning level / w4. / W4 gives some warnings that some / W3 can't give.
2. Use the / gz compile option in the debug version
The / gz option is used to find errors that are discovered in the release, including automatic (local) variables that are not initialized, stack errors, incorrect function prototypes. 3, use the #pragma warning compiler
You can use the #pragma warning compiler to prohibit the entire program, a specific header file, a specific code file, or a specific warning of a specific row code, which looks to where you put #pragma.
4, use the compilation rule / wx without warning
This compilation option treats all warnings as an error, only after the false warning is eliminated. Sometimes compiling warnings may be reasonable, and the core of processing compilation warning is to discover errors, rather than suppressing warnings themselves. This law is very helpful for big procedures development groups. The ultimate goal is to eliminate errors, not to eliminate warnings.
V. Memory space and allocation
1, memory allocation error
Dynamic memory allocation errors have two basic types: memory error and memory leakage.
(1) Memory error
When a pointer or the memory cell pointing to the pointer becomes an invalid unit, or when the data structure allocated in the memory is destroyed, the memory error is caused. The pointer is not initialized, the pointer is initialized to an invalid address, the pointer is unclearly modified, and the pointer is used after the memory area associated with the pointer (this pointer is called a dangling "pointer), these It will make the pointer into an invalid pointer. When written by an error pointer or a virtual resuscitation, or converting the pointer to a non-match data structure, or written the data, the memory itself is destroyed. Delete the uninitial pointer, delete the non-heap pointer, delete the same pointer multiple times, or covers the internal data structure of a pointer, will cause memory allocation system errors.
(2) Memory leakage
Memory leakage is generated when the memory is allocated. There are many situations that cause memory leaks, such as release memory in all execution paths of the program, not release all memory in the destructor. The longer running before the crash is collapsed, the larger the cause of the crash and the relationship between memory leakage.
Windows reclaims the leak memory at the end of the program, so memory leak is a temporary issue. But why must I eliminate memory leaks? First, memory leaks often lead to leakage of system resources. Dynamic allocation of memory often represents a storage area, but also represents certain types of system resources, such as files, windows, device contexts, GDI objects, and more. Second, high quality procedures and specific server programs must be able to run unlimited. Finally, memory leaks are often signs of other programs or bad programming habits.
Causes leading to internal ginseng leaks: forget to release memory; constructor failed; there is a destructive function for memory leakage; there is an exception handler for memory leaks; multiple returns; use the error form.
2, about memory initialization
In the debug version, the memory that is not initialized in the bunch is populated by 0xcd byte mode, and the memory released in the pile is filled with 0xDD byte mode. The memory in the stack is initialized by the 0xcc byte mode. In the debug version and the release version, the global memory that is not initialized is initialized to 0.
3, memory virtual address space
Windows uses a set of fixed ranges to divide the 4GB virtual address space of the process, so it is sometimes determined whether the pointer is valid by viewing the return value of the pointer.
(1) WINDOWS2000 virtual address space division
0 ~ 0xffffff (64KB): Cannot be used to detect empty pools (access conflicts)
0x10000 (64KB) ~ 0x7ffeffff (2GB-64KB): Win32 process private (non-reserved), used for program code and data
0x7fff0000 (2GB-64KB) ~ 0x7ffffff (2GB): Cannot be used to prevent overwriting OS partition (access conflicts) 0x800000000 (2GB) ~ 0xffffffff (4GB): Reserved for operating system, irrevacity (access conflict)
(2) Windows2000 virtual address space usage
0x00030000 ~ 0x0012FFFF: Thread Stack
0x00130000 ~ 0x003FFFF: Pile (sometimes heap here)
0x00400000 ~ 0x005FFFF: Executive code
0x00600000 ~ 0x0fffffffffffffff: Heap (sometimes you are here)
0x10000000 ~ 0x5ffffff: App DLLS, MSVCRT.DLL, MFC42.DLL
0x77000000 ~ 0xfffffffff: Advapi32.dll, ComctL32.dll, GDi32.dll, kernel32.dll, ntdll.dll, rpcrt4.dll, shell32.dll, user32.dll
Among them, 0x00400000 is the minimum base address that all versions of Windows can use.
Six, some debugging technology
1, debugging cycle
Use the break command under the Debug menu. In Windows2000, if the program has an input request, you can use the F12 key interrupt program, then check the window's call stack, or a single step tracking code to find the cause of the dead cycle.
2, with spy debugging and message issues
The best solution for debug messages is the SPY tool provided by Visual C . SPY allows programmers to view windows, messages, and threads. SPY default message output: The first column is displayed. The second column displays the handle that accepts the message. The "S" indication message in the third column is sent by sendMessage, "P" representative message is sent by PostMessage, "R" is the return value of the message handle. The fourth column gives a decoded message name, message parameter, or return value.
3, unconventional method
(1) Reparators your app
When your program shows an exception or unexpected behavior, or the Visual C compiler fails because a internal compiler error fails, it is best to delete the debug or release folder in the project, and re-edited from the beginning.
(2) Restart Visual C
Visual C has superior capabilities, but some features of the compiler also cause strange errors. If your program is very strange, you try to clear all breakpoints, close or hide the observation window, check the engineering setup dialog box to see what modifications have been made until you restart Visual C to eliminate the Visual C environment Abnormal behavior.
(3) Restart Windows
When you find that Windows or other programs exhibit an exception or unexpected behavior, you should restart Windows to eliminate interference from the operating system to debug.
(Finish)
[references]
EVERETT N.MCKAY, MIKE WOODRING, He Jianhui, Xu Jun, Dong Wei translated. Debugging Windows Programs (Windows Program Debugging). Beijing: China Electric Press. 2002
postscript:
"
Windows
Program debugging "This book introduces many of the basic knowledge and techniques for debugging. Some problems among this, I have made me patent, if I run an application, I suddenly jump out of a dialog, saying that a certain memory is "Write".
After reading this book, you will understand the mechanism. This article has made a certain abstract summary of the content of the same topic in the book, and organizes the content related to the book in the book, and appropriately supplemented some books. Due to the aspects of your concerns, it does not involve multithreading and
COM
Therefore, the debugging technology of this is the reference source book.