Watching Variables)
What do users do when the program stops at a breakpoint? Generally, when you stop at a breakpoint, you need to check the value of a variable. is the value of a variable the same as expected? Or the value of a variable (the value of this variable is unknown in advance ).
The watch list Window enables you to check the value of a variable. Programmers often ignore this simple and fundamental feature because they do not take the time to fully learn how to use the debugger. You can add any number of variables to the watch list. The Watch List window during the debugging session.
Both the variable name and variable value are displayed in watch list.
Tooltip expression evaluation (tooltip expression evaluation)
The debugger and code editor have a good feature that makes it easier to check variable values. This feature is the expression evaluator, Which is enabled by default, so you can use it without doing anything. If necessary, you can close the editor Properties dialog box on the Code insight page, for example:
So what is the value of the tooltip expression? It works like this: when the program stops at a breakpoint and you move the editing cursor to a variable, a prompt window is displayed, showing the current value of the variable. This makes it easier to quickly check variables. You only need to move the cursor over the variable and wait for a moment.
The tooltip evaluator calculator displays different variable types. For regular data members (integer, Char, byte, String, etc.), the actual value of the variable is displayed. For dynamically created objects (such as an instance of a class ), tooltip evaluator displays all elements of a record. As shown in:
Note
Sometimes tooltip evaluator does not seem to work properly. For example, if you move the editing cursor to a variable outside the scope, the prompt window is not displayed because the tooltip evaluator has no information to display. Variables optimized by the editor may not display the correct values.
The tooltip evaluator does not work when the cursor is in the with block. For example, read the following code:
var pp: Point;begin with pp do begin x := 20; y := 40; end; Caption := IntToStr(pp.x);end;
If you place the cursor on the variable X, the tooltip evaluator does not display the value of X because X belongs to the with statement (point variable ). If you move the cursor over the variable point, the debugger displays the point value (including the X field ). For example:
Tooltip expression Valuers are useful. Do not forget to use them.
The watch list context menu)
As with every Delphi window mentioned above, watch list also has its own shortcut menu. Description:
Edit watch --Open the watch Properties dialog box and modify the attributes of the monitoring item.
Add watch --Open the watch Properties dialog box to display new monitoring items.
Enable watch --Enable a disabled monitoring item
Disable watch --Disable an enabled monitoring item
Delete watch --Delete A monitoring item
Enable all watches --Enable all disabled monitoring items
Disable all watches --Disable all enabled monitoring items
Delete all watches --Delete all monitoring items
Add group --Add a Monitoring Group
Delete group --Delete a Monitoring Group
Move watch to group --Move monitoring item to group
Stay on top --Make the Watch List window always at the top layer
Inspect --Displays information about the selected monitoring item.
Break when changed --When the variables in the monitoring window change, the debugger will terminal. The monitoring variable is displayed in red, indicating that break when changed takes effect.
Dockable --The window is a slot.
Use the watch Properties dialog box (using the watch Properties dialog box)
When you add or edit a monitoring item, use the watch Properties dialog box. If you are editing the PP variableWatch PropertiesDialog box.
The expression field at the top of the dialog box is the place where you enter the variable name to be edited or added to the watch list. This field is a combo box, it can be used to select previously used monitoring items.
But when you want to check the array, use the [repeat count field]. For example, if there is an array of 20 integer elements, check the first 10 Integers of the array, you can enter the first element of the array (for example, array [0]) in [expression field], and then input 10 in [repeat count field. The first 10 elements of the array are displayed in the watch list. For example, PP is an integer array with 20 elements:
Note
If the array name is added to the watch list, all elements of the array are displayed in the watch list. When you want to view some elements of the array, use the repeat count field ].
Only when the floating point is checked, the [digits field] is used. When the floating point is displayed, the number of valid digits must be displayed. The remainder after the valid digit is rounded up. [Enabled field] determines whether the monitoring item is currently enabled.
The other options in the watch Properties dialog box are display options. Each data type has a default display type. When you select default, the default display type is used. When default is used to view the default status of the option, the default display type is selected. Select other viewing options to view data in other ways. For example:
To modify a monitoring item, click the item in watch list and select Edit watch… from the shortcut menu ...] Menu item, you can also double-click the monitoring item to quickly open the watch Properties dialog box.
Enabling and disabling watch items)
Like when using a breakpoint, every item in the watch list can be enabled or disabled. When a monitoring item is disabled, it becomes grayed out and its value is displayed as <disabled>. For example:
To disable a monitoring item, click the name of the item in the watch list and select the "disabled watch" menu item from the watch list shortcut menu. To enable this monitoring item, select enabled watch from the shortcut menu.
Note
You may want to disable the monitoring items that are not currently in use but will be used later. During debugging, many effective monitoring items in the watch list will slow down program execution, because every time a line of code is executed, all the Watch List variables will be updated.
Add a variable to the Watch List (adding variables to the Watch List)
You can add monitoring items to the watch list in multiple ways. The quickest way is to click the variable name in code editor and select debug | add watch at cursor from the shortcut menu of code editor or press Ctrl + F5, the monitoring item is immediately added to the watch list. If needed, you can edit the monitoring item and modify its attributes.
To add a variable to the watch list and do not want to find it in the source file, you can select the run | add watch menu item from the main menu. When the watch Properties dialog box appears, enter the variable name to be added to the watch list and click OK.
Note
Although you can add class instance variables to the watch list, the displayed values may be useless. For example:
Use DEBUG inspector to view all data members of the class.
Use watch list (using the Watch List)
When a breakpoint occurs, the Watch List displays the current values of all the variables. If the Watch List window is not opened, you can open the Watch List window from the view, debug window, and watches menu items in the main menu.
Note
Berth the Watch List window to the bottom of the code editor window, so that you can see it when debugging the code.
In some cases, a message is displayed after the variable in the watch list, instead of the variable value. For example, if a variable is not out of the scope or is not found, Watch List displays "undeclared identifier: 'X'" after the variable name. If the program is not running or does not stop at the breakpoint, the Watch List displays [process not accessible] after all the monitoring items. <Disabled> is displayed after the monitoring item is disabled. Other messages may be displayed based on the current status of the application or the current status of a variable.
As mentioned in the previous chapter, users may occasionally see the following display in the watch list: Variable 'X' inaccessible here due to optimization, this is a small drawback of the editor with optimization features. To check variables that are easily optimized, you must disable the optimization function of the compiler, that is, disable the optimization option on the compiler page in the Project Options dialog box.
Uninitialized variables display random values until they are initialized.
Note
Watch List can be used as a simple decimal/hexadecimal converter. To convert a hexadecimal number to a decimal number, select Run | add watch from the main menu. In the expression field, enter the hexadecimal number and click OK, the input hexadecimal number and the equivalent decimal number are displayed in watch list. to convert a decimal number to a hexadecimal number, except clicking the hexadecimal radio button to change the display type to hexadecimal, the other operations are the same as the previous ones. Because expression accepts mathematical expressions, you can use watch list as a hexadecimal calculator, or even use both hexadecimal and decimal values in the same expression. For example:
Using watch list has only one drawback. The application must stop at a breakpoint..
In the process of application debugging, watch list is a simple and key task. To illustrate the use of watch list, we will do the following exercises:
1) Create an application and place a button on its main form. Set the name attribute of the button to watchbtn, and change the caption attribute to watch test. Change the name attribute of the main form to debugmain and the caption attribute to debug watch list test.
2) double-click the button to display the onclick event of the button in code editor. Modify the onclick handler as follows:
procedure TDebugMain.WatchBtnClick(Sender: TObject);var S: string; X, Y: Integer;begin X := Width; S := IntToStr(X); Y := Height; X := X * Y; S := IntToStr(X); X := X div Y; S := IntToStr(X); Width := X; Height := Y;end;
3) Save the project, name the unit dbgmain, and name the project debugtest.
4) set a breakpoint on the first line after the begin Statement of The onclick handler to run the application.
5) Click the watch test button and the debugger stops at the breakpoint. When the debugger stops at the breakpoint, IDE and code editor are displayed at the top layer.
6) Add the variables S, X, and Y to the Watch List (due to code optimization, the variables X and Y are not accessible at the beginning), such:
7) arrange the positions of watch list and code editor so that you can see two windows at the same time (You may wish to move watch list to the bottom of code editor ).
8) switch the input focus to the Code Editor and press F8 to execute the next line of code. After the execution is completed, the execution point is moved to the next line. The variable X in the watch list shows a value. For example:
9) Press F8 to continue executing the program on one line and monitor the results of variables in the watch list.
10) when the execution point reaches the last line of the onclick handler, click the run button in the toolbar to continue running the application.
You can click the watch test button repeatedly to see how the Watch List works. You can also test the effects of different monitoring settings each time.
Note
In the preceding example, The onclick handler first obtains the values of the width and height attributes of the main form, then performs some calculations, and then sets the width and height to the start value. After this program is executed, nothing has changed. There is a reason to assign values to the width and height attributes at the end of this method.
If the variables X and Y are not actually used in the Code, the user cannot check the two variables, because the compiler will find that these two variables are not used during code optimization and delete them. The two variables are used at the end of the method to prevent the compiler from optimizing them.
As mentioned above, I hope you will have a basic understanding of the working process of compilers with optimized functions. In this way, when you start to debug the application, you will not be overwhelmed when encountering messages such as "variable 'y' inaccessible here due to optimization.
The above code passes the test in Delphi7. Download the instance code:Watchlisttest program .rar