Tutorial Four: The Debugger
In this tutorial we will demonstrate use of the debugging subsystem of Forte For Java. We will use the completed code for one of the earlier tutorials - part three of Tutorial One, the advanced version of the Clock. The completed code for this tutorial (and all other tutorials) is included with Forte For Java, and can be found under
Development/tutorial/
in the Forte For Java Explorer.The Debugger allows you to set and remove breakpoints, watch variables, track the state of threads, and more. All of this can be done within the simple and intuitive graphical user interface.
Preliminary Setup:
- At a later stage in this tutorial we will need to access the included TimerBean source. To make this source accessible to the IDE, we need to mount it as a new file system. From the Tools menu in the Main Window, select Add Directory. A standard Browse dialog will open. Navigate to your Forte For Java installation directory, and select the
sources
subdirectory. Click Add, and you will see a new file system appear in under the Filesystems tab in the Explorer.
Working with Breakpoints
- Close any sources and forms you may have open, and terminate any running processes. Flip to the Editing Workspace, open an Explorer window, and expand the
Development\tutorial\clock
hierarchy. We will use the final stage of this tutorial - in the part3 subdirectory. Double-click onClockFrame
to open this object in the Editor, Form Editor, and Component Inspector.- Click the Compile icon in the Main Window, or use the keyboard shortcut F9, to compile this source. You should see the Main Window status line indicating the progress of this command. (If you get a warning about a deprecated API, ignore it - it is harmless in this case.)
- In the Editor window, find the main method, and position the cursor on the first line of the body. We will add a breakpoint to this line. Right-click on the line and choose Add/Remove Breakpoint from the contextual menu or press CTRL+F8. You will see the line highlighted in blue, indicating a breakpoint is set on that line. (Note that putting a breakpoint on the previous line, the one declaring the method, will not work for
main()
- Java calls the body of main methods in a special way for the debugger, and you cannot trace into it.)
The debugging session
- Let's start the debugging session. From the Debug menu in the Main Window, choose Start Debugging, or press F5. HotSpot users must set the
Classic
property toTrue
on the property sheet forDebugger Types / Standard Debugging/ Standard Debugging
in Project Settings (choose Project | Settings... from the main menu to open the Project Settings window). Forte For Java will switch to the Debugging Workspace, and two new windows will open - the Debugger window and the Output window.The Output window is split vertically, the left panel displaying the output of the debugged program and the right panel showing messages from the debugger itself.
The Debugger window is used to manipulate breakpoints and watch program variables and the state of threads. These are each displayed under a separate tab. Currently under the Breakpoints tab you will see the breakpoint we have just set, listed by source name and line number.
You will see several messages from the debugger in the Output window, and then the debugger will halt at the breakpoint in the main method. The blue-highlighted line in the Editor will change to pink to indicate where execution has halted.
- At this point you can continue (ALT+F5), Trace Over the current line (F8), or Trace Into the function called on the current line (F7). We wish to step into ClockFrame, so push F7, or select Trace Into from the Debug menu in the Main Window and then push CTRL+F7 and F7 again.
You will see another line of output from the Debugger in the Output Window, and the Editor window will jump to the constructor of the
ClockFrame
class and again halt. You should see a pink highlighted line where the debugger is currently stopped.- You will next break at the first of the three variable declarations manually entered when creating the tutorial (private GregorianCalendar gCal ...). Push F8 to trace over this; trace over both others by pushing F8 twice more.
- The pink line highlighting the current point in the code should now be at the line reading
initComponents ();.
Push F7 to trace into this. You are halted on the line where the instance ofcom.netbeans.timerbean.Timer
is created. Press F7, then CTRL+F7, and then F7 again to step in. Assuming you have mounted the sources directory as a file system as described in the Preliminary Setup section, the Timer source will open in the Editor window, and the Debugger session will now step into it. The pink highlighted line indicates the point in the source where the Debugger is stopped.
- Press ALT+F5 to continue. The ClockFrame will open and run.
- Find the
tmrSecondsOnTime()
method in the ClockFrame source, and set a breakpoint (CTRL+F8) on the line declaring the method. The next time the program flow goes through this point, execution will halt, and the blue breakpoint line will turn pink.Watching Variables
- Flip to the Watches tab of the Debugger window. Here you can monitor the values of individual variables during execution. To add a new watch, you can select Debug | Add Watch from the Main Window or right-click on the root item of the
Watches
tree on the Watches tab of the Debugger window, and select Add Watch. You can also right-click on the variable in the editor and select Add Watch. Now go to thetmrSecondsOnTime()
method, right-click on the variabletimeTxt
, and press OK in the Add New Watch dialog box.timeTxt
will appear in theWatches
tree in the Debugger Window.- Push ALT+F5 to continue the debugging session. After one second of execution,
tmrSecondsOnTime()
will be called again, and execution will again halt. The value oftimeTxt
displayed in the Debugger window will update when debugger moves over it (an increment of one second).
It is possible to watch multiple variables simultaneously - simply add a watch as before. All watched variables are listed in the Debugger Window. You can delete watched variables by selecting them in the
Watches
tree and pressing the DELETE key, or by selecting Delete from the popup menu. If a variable is not in the current scope, it does not display any value.As you use ALT+F5, F7, and F8 to continue, step into, and step over the code, respectively, you can monitor the values of the watched variables at each stage.
Threads
Under the threads tab of the Debugger window, the current state of all threads of the program are listed.
Ending the debugging session
To end a debugging session, select Debug | Finish Debugging from the Main Window, or use the keyboard shortcut SHIFT+F5.
Other Features
- The state of the debugging session, including breakpoint locations and watched variables, is preserved across sessions. It is not necessary to explicitly save the session.
- You can customize the Debugging subsystem from the
Debugger Types
node in the Project Settings window and theDebugger Settings
node in the Global Options window. For more information, see the User's Guide to Forte for Java, Community Edition.
Beginning | Prev |