Breakpoints and Debugging

The IDL Workbench provides robust tools for finding and correcting problems in your code.

Breakpoints

To set a breakpoint, place the cursor on the line where you want the breakpoint to appear and press Ctrl-Shift-B or select Run → Toggle breakpoint. A blue dot appears in the left-hand margin of the editor window.

breakpoint_example.gif

You can also toggle breakpoints on and off by double-clicking in the left-hand margin next to the line of code on which you want IDL to pause.

Debug Perspective

The IDL Workbench makes a distinction between editing and debugging code, and provides the ability to switch automatically to the Debug perspective when an error or breakpoint is encountered. The Debug perspective is a collection of the views most useful for debugging and analyzing code.

See Using the Debug Perspective for details.

Viewing Variable Values

When you run a routine that contains a breakpoint, IDL will halt execution when it reaches the breakpoint. When execution is halted, you can inspect the variable values in the current execution scope using the Variables view or by hovering the mouse pointer over a variable in the editor.

Stepping Through Code

When execution is halted due to a breakpoint or an error, you can execute single statements using the Step commands on the Run menu. See Stepping Through Code for details.

Debugging a Short Program

Let's walk through the process of debugging a short program. In this example, we'll create a program, set a breakpoint, inspect variable values, and step through the code.

  1. To create a new .pro file, click the new_file_icon.gif icon or select File → New → IDL Source File.
  2. A new editor window appears.

  3. Enter the following text into the editor window:
  4. PRO tinyRoutine 
    ; Create a string variable 
    myString = 'This is a tiny IDL routine' 
    PRINT, mystring 
    ; Create some other variables 
    myNumber = 4 
    myResult = STRING(myNumber * !PI) 
    ; Display the myResult variable 
    void = DIALOG_MESSAGE('Result: '+myResult) 
    END 
    
  5. To save your new .pro file, click the file_save_icon.gif icon or select File  Save.
  6. Select "Default" as the parent folder and click OK, accepting the default filename (tinyroutine.pro).
  7. To execute the program, click the run_icon.gif icon, press F8, or select Run → Run tinyroutine.
  8. The program prints a string to the Console view, displays a dialog, and ends. If you wanted to temporarily stop execution of your routine somewhere in the middle, you would set a breakpoint.

  9. Click OK on the dialog to clear it.
  10. Position the cursor on the words PRINT, myString in the editor window.
  11. Press Ctrl-Shift-B or select Run → Toggle breakpoint. A blue dot appears in the left-hand margin of the editor window.
    Breakpoints_and_Debugging-21.jpg
  12. You can also toggle breakpoints on and off by double-clicking in the left-hand margin next to the line of code on which you want IDL to pause.

  13. Run the tinyroutine program again (press F8, click the run_icon.gif icon, or select Run  Run tinyroutine).
  14. If this is the first time you have run a program with a breakpoint (or an error), you will see the Confirm Perspective Switch dialog.

  15. Click Yes to display the IDL Debug perspective.
  16. The IDL Workbench interface is rearranged to add several new views at the top of the screen: Debug, Variables, and Breakpoints.

    Notice that the Variables view contains entries for the four variables defined in the tinyroutine routine, but that only the MYSTRING variable is defined. It is also instructive to examine the contents of the Debug and Console views when IDL stops at a breakpoint.

  17. Press F6, click the stepover_icon.gif icon (on the toolbar in the Debug view), or select Run → Step Over.
  18. Note how the Debug, Console, Variables, and Editor views adjust as you repeatedly step through your code.

  19. When you have stepped to the end of tinyroutine (you will see the text "% Stepped to: $MAIN$" in the Console view), click the IDL icon on the Perspective toolbar to return to the IDL perspective.
    perspective_toolbary.gif

For additional information on debugging, see Debugging IDL Code.