Delphi application debugging (6) step-by-step code debugging

Source: Internet
Author: User
Step-by-Step Code Debug (stepping through your code)

Step-by-Step code debugging is one of the most basic debugging operations. People often make mistakes that only identify trees without forests. Regular review of basic knowledge helps readers understand things they did not know before.

Debug the debugging gutter symbols)

Before starting, let's talk about the symbols that appear in the code editor groove during the debugging session. As mentioned earlier, when you set a breakpoint on a code line, a red circle will appear in the groove.

There is also a Green Arrow symbol in the groove to indicate the position of the execution point. For example:

One thing I have not mentioned is that there are some blue dots in the positions corresponding to some code lines in the groove. These blue points indicate the actual generation of assembly codeSource code. For example, the code editor when the debugger stops at a breakpoint. There is a blue dot indicating the source code line that generates the assembly code, an arrow symbol indicating the execution point, and a breakpoint symbol. The checkpoint mark on the breakpoint symbol indicates that the breakpoint is a valid breakpoint.

Take a closer look at the above Code, without a small number of lines of code that do not generate any compilation code. For example:

 
VaR S: string; X: integer;

Why are these lines of code not generated? Because they are variable declaration rows. Let's look at another line:

 
X: = 20;

Why does this line not generate code? This is because of code optimization. When compiling the code, the compiler will find that the variable X has not been actually used to optimize it. Let's look at the following lines:

 
{$ Ifndef Win32} s: = 'something' s very wrong here... '; {$ endif}

The compiler does not compile source code lines between pseudo commands, because the symbol Win32 is defined in a Delphi7Program. The pseudo-instruction {$ ifndef Win32} of the compiler tells the compiler to compile this line if the target platform is not a 32-bit windows. Because Delphi7 is a 32-bit compiler, this line of code is not compiled. If it is in delphi1 (16-bit environment), this line of source code will be compiled.

Step over and trace)

Now, let's go back to step-by-step debugging code. When a program is paused at a breakpoint, You can do many things: Set the variables to be monitored in the watch list, use DEBUG inspector to check objects or view the call stack. You can also step-by-step code debugging to monitor changes in variables and objects when each line of code is executed.

When you continue step-by-step debugging, the source code to be executed in the source code is highlighted in blue. For example:

If the Watch List window and debug Inspector window are opened, they will be updated as each code is executed. In the watch list and debug Inspector windows, you can immediately see changes in variables and objects. The IDE debugger has two main step commands: Step over and trace.

1. Step over

Step over is to execute a line of source code and pause it in the next line. "Step over" is a bit inappropriate here, because literally, it means skipping a source code line. However, step over cannot be understood literally. In fact, step over refers to executing the current source code line. If this line of code calls a function or process, the called function or process must be fully executed. For example, a line of code of a program calls a method and sets a breakpoint on the line of code. When the debugger step over the line of code, the debugger stops the called method on the next line of code.

To debug the program with step over, press F8 or choose run | step over from the main menu or from the toolbar.

Note

When you step-by-step debugging of various source code units in the program, the Code Editor automatically loads and displays the required source code units (if these units have not been opened ).

2. Trace

When a user encounters a function or process during step-by-step code debugging, the trace into command allows the user to track the execution of the function or process. Trace into does not return to the next line after the function or process is executed as step over. Instead, it places the execution point on the source code of the first line of the called function or process, then, you can use step over or trace into to debug the function or process step by step. The shortcut for trace into is F7.

After checking the variables and completing other debugging tasks, you can click the run button to run the program. The program runs normally until the next breakpoint is reached.

Note

If you have Professional Edition Delphi or Client/Server Edition Delphi, you can trace the VCL source code. When you encounter a VCL method, trace into allows you to access the VCL source code of the method. You can view any variables to be viewed. You must add the path of the VCL source code directory to the search path field in the Project Options dialog box (directories/conditionals page), for example:

OK, and then re-compile and link. Tracking VCL source code may be of little significance to most programmers, but it is very useful to experienced programmers.

3. Trace to next source line

Another debugging command is the "trace to next source line" shortcut key [SHIFT + F7 ]. You can choose run | trace to next source line from the main menu to open this command.

Users will not frequently use this command, especially those who are not familiar with debugging and windows programming. Some Windows API functions Use callback functions, which means Windows functions call user functions to perform some operations.

If the execution point is on a Windows API function and the function uses a callback function, the trace to next source line command will skip the execution point to the first line of the callback function. The effect is similar to trace into (but in some specific cases, the effects of the two are completely different .).

When you debug a method in step-by-step mode, the execution point will eventually reach the end statement of the method. If the method to be debugged returns control to Windows after it finishes running, press F8 when the execution point is in the end statement of the method, you can exit this method and return the control to the program being debugged. At this time, you cannot determine whether the program will be paused, because the IDE has the input focus. Switch back to the program and activate it. (Click the button on the Windows taskbar ).

Step-by-Step code debugging is a basic debugging technology that is frequently used by users when debugging programs. Therefore, you must remember the related keyboard shortcuts:

[F7] Trace into [F8] Step over [F9] Run

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.