Home Technology Program debugging

Program debugging


The first step is to use the editing program to send the compiled source program to the computer in a certain writing format. The editing program will add or add to the source program according to the user’s intentions. Delete or modify.

The second step is to translate the input source program into machine language, that is, use the compiler to check the grammar of the source program and translate the source program statements that meet the grammatical rules into a "language" that the computer can recognize . If a grammatical error is found after checking by the compiler, you must use an editor to modify the grammatical error in the source program, and then compile it until there is no grammatical error.

The third step is to use the connection program in the computer to connect the translated computer language program and build it into a program that the computer can actually run. During the connection process, connection errors generally do not occur. If a connection error occurs, it indicates that there are problems such as confusion of subroutine calls or parameter passing errors in the source program. At this time, the source program must be modified with the editing program, and then compiled and connected, and so on, until there is no connection error.

The fourth step is to perform trial calculations on the modified program. At this time, you can assume several simulation data to test run, and compare the output results with the correct results of manual processing. If there is a difference, it indicates that there is a logical error in the computer program. If the program is not large, you can use artificial methods to simulate the computer to modify the data of the source program; if the program is relatively large, manual simulation obviously does not work, then you can only set the computer to a single step execution mode, one step Step tracking program operation. Once you find the problem, you still have to edit the program to modify the source program, and then you still have to compile, connect and execute until there is no logic error. You can also compile after completion.


One, use your head to analyze and think about the information related to the error symptoms.

Second, avoid dead ends.

Three, only use debugging tools as a means. Using debugging tools can help thinking, but not a substitute for thinking, because debugging tools give an irregular debugging method.

Fourth, avoid heuristics, at best you can only use it as a last resort.

Fifth, where there is an error, there may be other errors.

Six. A common mistake in correcting errors is that only the symptoms of the error or the performance of the error are corrected, but the error itself is not corrected. If the proposed modification does not explain all the clues related to the error, it means that only a part of the error has been modified.

Seven, pay attention to correcting an error while introducing new errors.

Eight, the process of correcting errors will force people to temporarily return to the programming stage. Correcting errors is also a form of program design.

Nine, modify the source code program, do not change the target code.


One, simple debugging method: steps

1, insert a print statement in the program, the advantage is that it can display the dynamic process of the program, which is easier to check Information about the source program. The disadvantage is that the efficiency is low, a large amount of irrelevant data may be input, and the error is found to be accidental.

2, run part of the program. Sometimes in order to test some program segments that are suspected of being wrong, the entire program is executed many times. In this case, try to make the program under test execute only the program segments that need to be checked to improve efficiency.

3, with the help of debugging tools. Most programming languages ​​have special debugging tools that can be used to analyze the dynamic behavior of the program.

Second, troubleshooting with retrospective method. Determine the place where the error symptom is first discovered, and manually trace the source code back along the control flow of the program until the error or range is found.

Third, troubleshooting by induction. It is a systematic way of thinking, which is a method of inferring the whole from the individual. This method starts from the clues (error symptoms) and finds out the fault by analyzing the relationship between these clues. There are four main steps:

(1) Collect relevant data. Collect test cases to find out which error symptoms are observed in the test cases, and under what circumstances the error occurs.

(2) Organize data. Organize and analyze the data in order to find the rules, that is, under what conditions there are errors and under what conditions there are no errors.

(3) Derive the hypothesis. Analyze and study the relationship between the clues, and strive to find out their laws, so as to put forward one or more hypotheses about the error. If the hypothesis cannot be made, more test cases should be designed and executed in order to obtain more data.

(4) Prove the hypothesis. Assumptions are not equal to facts. It is extremely important to prove the rationality of assumptions. Eliminating errors based on assumptions without proof can often only eliminate the signs of errors or can only correct some of the errors. The way to prove a hypothesis is to use it to explain all the original test results. If all phenomena can be explained satisfactorily, the hypothesis is proved, otherwise either the hypothesis is invalid or incomplete, or there are multiple errors at the same time.

Fourth, troubleshooting by deductive method. Imagine possible causes, use existing data to eliminate incorrect assumptions, refine and prove the remaining assumptions.

Fifth, the bisection search method. If you know the correct value of each variable at several key points in the program, you can use assignment statements or input statements to "inject" the correct values ​​of these variables near the key points in the program, and then check the output of the program. If the output result is correct, it means that the error occurred in the first half, otherwise, it may be considered that the error is in the second half. This is repeated many times, gradually approaching the wrong position.


Static debugging

The following two methods can be used:

(1) Output the contents of the register. If there is a problem in the test, try to keep the field information. Print out the contents of all registers and relevant parts of the main memory (usually printed in octal or hexadecimal format) for analysis and research. Debugging in this way, the output is the static state of the program (the state of the program at a certain moment), the efficiency is very low, and it is only used as a last resort.

(2) To obtain the dynamic value of the key variable, insert a print statement in the program. This is a simple way to obtain dynamic information, and can check whether a certain variable has changed as expected after a certain time. The disadvantage of this method is that it may output a large amount of information that needs to be analyzed, and the source program must be modified to insert the print statement, which may change the key timing relationship and introduce new errors.

Dynamic debugging

Usually use the debugging function provided by the programming language or special debugging tools to analyze the dynamic behavior of the program. The debugging functions provided by general programming languages ​​and tools include checking main memory and registers; setting breakpoints, that is, when a specific statement is executed or the value of a specific variable is changed, the program stops executing in order to analyze the state of the program at this time.

This article is from the network, does not represent the position of this station. Please indicate the origin of reprint