|
Application Programming: Debugging and Error-Handling |
|
When execution halts, there are several ways to see the values of program variables. These include:
When execution stops you can query the values of current variables in the program scope using the PRINT and HELP routines. For instance, suppose you have created the following program:
FUNCTION hello_who, who RETURN, 'Hello ' + who END PRO hello_main name = '' READ, name, PROMPT='Enter Name: ' str = HELLO_WHO(name) PRINT, str END
Place a breakpoint on the PRINT, str line and then compile and run the program. Enter a name at the IDL command line when prompted. When execution halts, return the value of the name variable by entering,
PRINT, name
The Output Log shows the name you have entered.
Return information about the str variable by entering:
HELP, str
The Output Log shows the variable name, data type and value. This information is also available in the Variable Watch window, described in the following section.
| Tip You can also place PRINT and HELP statements in your program to see variable values without pausing program execution. As these statements are encountered, values are printed to the Output Log. |
| Note When working in the IDLDE, you can also use the macros, Print Variable or Help on Variable, to return information on a selected variable. See Macros Menu for details. |
The Variable Watch window displays current variable values after IDL has completed execution. If the calling context changes during execution — as when stepping into a procedure or function — the variable table is replaced with a table appropriate to the new context. While IDL is at the main program level, the Variable Watch window remains active and displays any variables created.
To hide the Variable Watch window, select Window
Hide Variable Watch. Select Show Variable Watch to make it reappear. Changing the Window menu will only affect the current IDL session.
To apply your changes to future sessions, select File
Preferences and click the Layout tab. In the section labeled Show Windows, select or clear check boxes associated with the windows you want to appear. Click Apply to save your changes for future IDL sessions and OK to exit.
| Note Selection or clearing of Window menu items reflects changes in the Layout preferences and vice versa. |
The Variable Watch window is refreshed after the IDLDE has completed execution. Each Variable Watch window contains the following folders:
This tab contains descriptions of local variables. Local variables are created from IDL's main program level. For example, entering a=1 at the Command Line lists the integer a in the Locals tab.
This tab contains descriptions of parameters. The variables and expressions passed to a function or procedure are parameters. For more information, see Parameters.
This tab contains descriptions of variables contained in common blocks. The name of each common block is shown in parentheses next to the variable contained within it. For more information, see COMMON.
This tab contains descriptions of system variables. System variables are a special class of predefined variables available to all program units. For more information about system variables, see System Variables.
Each tab contains a table listing the attributes of the variables included in the category. You can size the columns by clicking on the line to the right of the title of the column you wish to expand or shrink. Drag the mouse either left or right until you are satisfied with the width of the column. For example, to change the width of the Name column, click and drag on the line separating the Name field from the Type field.
The following fields describe variable attributes:
This field shows the name of the variable. This field is read-only, except for array subscript descriptions (see example in Example: Using the Variable Watch Window below).
For compound variables such as arrays, structures, pointers, and objects, click the "+" symbol to the left of the name to show the variables included in the compound variable. Click the "-" symbol to collapse the description.
This field shows the type of the variable. This field is read-only.
This field shows the value of the variable. To edit a value in UNIX, highlight the cell by clicking on it, press the function key F2 to enter editing mode, and enter the new value. To edit a value in Windows, double click on the cell to highlight it and enter the new value.
The Name, Type, and Value fields are displayed as when using the HELP procedure. For more information about variables, see Variables.
Object references are expanded only if they reference non-null objects. Object data are expanded only if the object method has finished running. Object data are read-only and cannot be changed with the Variable Watch window.
Arrays are expanded to show one array element. Click on the "+" symbol next the name of the array to display the initial array subscript. You can change this field to display the characteristics of any other array element.
| Note To enter editing mode in Motif, press F2 after clicking on the cell to be edited. In Windows, double-click on the cell. |
To edit the subscript, highlight the cell by clicking on it, and modify the name using the arrow keys to maneuver. For example, enter the following:
; Create an array with 2 columns and 3 rows. A=MAKE_ARRAY(2,3) ; Show the values of array A in the Output Log. They will all be ;zero. PRINT, A ; Assign the value of 5 to the value in the array subscripted as 2. ; This is the same as entering A(0,1)=5. A(2)=5 ; Show the new values of array A. PRINT, A
IDL prints:
0.00000 0.00000 5.00000 0.00000 0.00000 0.00000
It is easy to manipulate variables within the Variable Watch window. Click on the "+" expansion symbol next to the array A. The subscript [0,0] will be revealed beneath the description of A. Enter editing mode and change [0,0] to [0,1].
Press Enter to effect the change. Notice that the value of the subscript is displayed as 5, as you entered from the command line. Press the Tab key to highlight the value of the subscript [0,1]. You can change it to another number. Enter [1,0] in the subscript name field. You can also change the value from 0.00000 to another number.
For more information about arrays, see Arrays.
IDL users may find that all their variables have seemingly disappeared after an error occurs inside a procedure or function. The misunderstood subtlety is that after the error occurs, IDL's context is inside the called procedure, not in the main level. All variables in procedures and functions, with the exception of parameters and common variables, are local in scope. Typing RETURN or RETALL will make the lost variables reappear.
RETALL is best suited for use when an error is detected in a procedure and it is desired to return immediately to the main program level despite nested procedure calls. RETALL issues RETURN commands until the main program level is reached.
The HELP command can be used to see the current call stack (i.e., which program unit IDL is in and which program unit called it). For more information, see HELP.
IDL Online Help (March 06, 2007)