Debugging of the Delphi Application (vi) Step-code debugging

Source: Internet
Author: User
Tags one more line win32

Stepping code debugging (stepping Through Your codes)

Stepping code debugging is one of the most basic debugging operations, but is still to be described here. It is often easy to make trees trees mistakes. Reviewing basic knowledge often helps readers understand what they didn't know before.

Debug Groove symbol (Debugging gutter Symbols)

Before you begin, let's talk about symbols that appear in the Groove of Code Editor during a debugging session. As explained in the previous tutorial, when a user sets a breakpoint on a line of code, a red circle appears in the Groove.

There is also a green arrow symbol in the groove indicating where the execution point is located. Such as:

One thing that has not been said is that there are some blue dots in the grooves that correspond to some lines of code. These blue dots indicate that the source code of the assembly code is actually generated. For example, the debugger stops at a breakpoint when the Code Editor is located. There is a blue dot indicating the source code line that generated the assembly code, an arrow symbol indicating the execution point, and a breakpoint symbol. A check mark on the breakpoint symbol indicates that the breakpoint is a valid breakpoint.

Take a closer look at the code above, and the lines without dots produce no compiled code. For example the above:

123 var  S: string;  X: Integer;

Why don't these lines produce code? Because they are variable declaration lines. Look at one more line:

1 X := 20;

Why doesn't this line produce code? This is because of code optimization. When the compiler compiles the code, it finds that the variable x is not actually used, and optimizes it out. Take a look at the following lines:

123 {$IFNDEF WIN32}S := ‘Something‘s very wrong here...‘;{$ENDIF}

The compiler does not compile the source line between the compiler pseudo-directives, because the symbol WIN32 is defined in a DELPHI7 program. The compiler pseudo-directive {$IFNDEF WIN32} tells the compiler to compile this line if the target platform is not 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-up debugging and tracing (step-over and trace-into)

Well, go back to "stepping debug code". When a program pauses at a breakpoint, the user can do a lot of things: set the variables to watch in the watch list, check the object with Debug inspector, or view the call stack. You can also step through the code and monitor changes to variables and objects as each line of code executes.

When you continue with stepping debugging, you see that the source code in the next step to execute is highlighted in blue. Such as:

If you open the Watch List window and the Debug inspector window, they will be updated as each code executes. Changes to variables and objects are immediately visible in the watch list and the Debug inspector window. The IDE debugger has two main step commands: Step over and trace into.

1. Step over

Step over refers to executing a line of source code and pausing on its next line. "Step over" is a bit of a misnomer in this sense, because it literally means skipping over a source code line. However, step over is not literally understood here. In fact, Step over is the execution of the current source code line, and if the line code calls a function or procedure, the function or procedure that is called is completed completely. For example, a line of code in a program calls a method that sets a breakpoint on that line of code, and when the debugger step over the line of code, the debugger executes the called method and stops at the next line of code.

To use Step over debugger, you can press the F8 key, or you can select "Run |" from the main menu. Step over, or select from the toolbar.

Note

The Code Editor automatically loads and displays the required source code units (if they are not yet open) when the user steps through each source unit in the debugger.

2. Trace into

The trace into command allows the user to track the execution of the function or procedure when the user encounters a function or procedure in the course of the stepping-debug code. Trace into does not return to the next line after executing a function or procedure like step over, but instead places the execution point on the first line of the source code of the called function or procedure, and then the user can use step over or trace into to step through debugging the function or procedure. The shortcut key for Trace into is F7.

After the user has checked the variables and done other debugging work, you can click the Run button to run the program, and the program will run until the next breakpoint is encountered.

Note

If the user has a professional version of Delphi or client/server version of Delphi, then you can track the VCL source code. When a VCL method is encountered, Trace into allows the user to enter the VCL source code for the method, and the user can view any variables that need to be viewed. You must add the path to the directory where the VCL source code is located in the Search Path field on the Project Options dialog box (Directories/conditionals page), such as:

OK to recompile and link after confirmation. Tracking VCL source code may not mean much to most programmers, but it is very useful for experienced programmers.

3. Trace to Next Source line

Another debug command is "Trace to Next Source line" shortcut is "Shift + F7". From the main menu, you can select Run | The Trace to Next Source line menu item opens the command.

Users do not use this command frequently, especially users who are not familiar with debugging and Windows programming. Some Windows API functions use callback functions, meaning that Windows functions call the user's function to perform some action.

If the execution point is on a Windows API function, and the function uses a callback function, the trace to Next Source command skips the execution point to the first line of the callback function. The effect is similar to trace into (but in some specific cases, the effect is completely different.) )。

When stepping through a method, the execution point eventually reaches the end statement of the method. If the method you are debugging returns control to Windows after it has run, you can exit the method and return control to the program being debugged when the execution point presses the F8 key at the end statement of the method. You are not sure if the program will pause again because the IDE has input focus. To cut back to the program, activate it. (Click on its button on the Windows taskbar).

Stepping code debugging is a basic debugging technique that users often use when debugging a program, so the user remembers the keyboard shortcut:

"F7" Trace into"F8" Step over"F9" Run

Debugging of the Delphi Application (vi) Step-code debugging

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.