Counter Debugger Example

To follow this example, import the Counter Debugger sample plug-in into your workspace.  Build and launch an Eclipse Application with this plug-in.

Counter Debug Model

This example models a single thread of execution that can execute a simple instruction to increment a value. The value is initialized with a minimum value (1) and is incremented to a maximum value (100). When the value overflows, it is reset to the minimum value. The debug model consists of a debug target with a single thread. The thread's stack frames are based on the current count value. The number of frames (stack depth) corresponds to the current count value with the highest number on the top of the stack.

Example Counter debug session

For simplicity, counting executes the same process as the Eclipse SDK, in a separate thread. The increment instruction increments a value and sleeps for 100ms (just so each instruction takes a non-zero amount of time). The model allows the user to suspend, resume, and step. A step executes a single instruction. Resuming causes the counter to execute instructions until suspended, terminated, or a breakpoint is hit. A breakpoint can be set for a specific value.

To see how the debug model is implemented, look at the following classes in the org.eclipse.debug.examples.counter.model package. The majority of the model's logic is implemented in the thread object. The target and stack frame implementations delegate to the thread to perform all suspend, resume, step, and terminate actions.

Launching the Counter Debugger

For a quick demonstration of the Counter Debugger, launch it in your target Eclipse Application:

  1. Open the Debug Configurations dialog (Run->Debug Configurations...)
  2. Create a new Counter launch configuration.
  3. Select Debug button to launch the debugger.
  4. Use the suspend, resume, step, and terminate buttons in the Debug view to control execution.

Counter Launch Configuration
Image of the launch configuration dialog editing a Counter launch.

To see how the Counter Debugger launch logic is implemented, look at the following (in the Counter example plug-in). The launch delegate simply instantiates a debug target and adds it to a launch object.

Counter Breakpoints

The Counter debugger supports a limit breakpoint that stops execution when the counter reaches a specific value. Limit breakpoints support standard enable and disable actions.

Breakpoints view with Limit breakpoints
Image of the Breakpoints view with Limit breakpoints.

To see the limit breakpoint implementation look at the following:

A limit breakpoint is created by selecting a Counter debug target, thread, or stack frame and selecting the Run > Toggle Breakpoint (Ctrl+Shift+B) action. A dialog prompts for the limit value.

Dialog to create a Limit breakpoint
Image of Limit breakpoint dialog that prompts for breakpoint value..

An adapter is contributed for the Counter debug elements to retarget the Toggle Breakpoint action. To see how this is implemented, look at the following: