Home
Categories
Dictionary
Download
Project Details
Changes Log
FAQ
License

Debugger tutorial



This is a basic tutorial which explains how to use the debugger with a Groovy script.

Overview

We will define a Script interface, and create a debugger session using the SwingDebugScriptWindow.

Define the Script interface

Our interface will be very simple. We will have the following definition for the interface.
      public interface Script {
        public int computeResult(int value) {
        }
      }

Create the debugger session

Define the script interface

First we create a Groovy wrapper with our script interface:
      DebuggingWrapper<Script> wrapper = new GroovyScriptWrapper<Script>() {
      };

Create the SwingDebugScriptWindow

Then we create a SwingDebugScriptWindow and set it as a Debug listener of the wrapper:
      SwingDebugScriptWindow debugWindow = new SwingDebugScriptWindow(20, 20);
      debugWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      wrapper.setDebugListener(debugWindow);

Create the Debug session

Finally we get the script, install it, and create the associated Debug session:
      debugWindow.setVisible(true);

      JFileChooser chooser = new JFileChooser("Open Script");
      chooser.setCurrentDirectory(new File(System.getProperty("user.dir")));
      chooser.setDialogType(JFileChooser.OPEN_DIALOG);
      chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
      File file = null;
      int ret = chooser.showOpenDialog(null);
      if (ret == JFileChooser.APPROVE_OPTION) {
      file = chooser.getSelectedFile();
      }

      wrapper.initializeDebugSession();
      wrapper.installScript(file);
      DebugSession<Script> session = wrapper.createDebugSession();
      session.setExecutionMode(ScriptWrapper.MODE_NON_BLOCKING);
      session.setSessionHook(new SessionHook<Script>() {
         public void start(Script script) {
           script.computeResult(10);
         }
      });

      session.startSession();

Complete code

The complete code is:
      public class DebugTutorial {
        public void startDebug() throws Exception {
          DebuggingWrapper<Script> wrapper = new GroovyScriptWrapper<Script>() {
          };
          SwingDebugScriptWindow debugWindow = new SwingDebugScriptWindow(20, 20);
          wrapper.setDebugListener(debugWindow);
          debugWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          debugWindow.setVisible(true);

          File file = openScript();
          if (file == null) {
            System.exit(0);
          }
          wrapper.initializeDebugSession();
          wrapper.installScript(file);      
          DebugSession<Script> session = wrapper.createDebugSession();
          session.setExecutionMode(ScriptWrapper.MODE_NON_BLOCKING);
          session.setSessionHook(new SessionHook<Script>() {
            public void start(Script script) {
              script.computeResult(10);
            }
          });
          session.startSession();
        }

        private File openScript() {
          JFileChooser chooser = new JFileChooser("Open Script");
          chooser.setCurrentDirectory(new File(System.getProperty("user.dir")));
          chooser.setDialogType(JFileChooser.OPEN_DIALOG);
          chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
          File file = null;
          int ret = chooser.showOpenDialog(null);
          if (ret == JFileChooser.APPROVE_OPTION) {
            file = chooser.getSelectedFile();
          }
            return file;
        }

        public static final void main(String[] args) throws Exception {
          DebugTutorial tutorial = new DebugTutorial();
          tutorial.startDebug();
        }
      }

Use the debugger

We will use for this tutorial the following script:
      public int computeResult(int value) {
        int a = 0;
        for (int i = 0; i < 100; i++) {
          a = a + value;
          context.echo("a = " + a);
        }
        return a;
      }

Start the debugger

Run the DebugTutorial class and select the script. You should see the following window:
debugwindow

Add a breakpoint

We will add a breakpoint on our script. First show the line numbers by left-clicking on the left border of ther window and moving it to the right:
debuggutter
Now right-click on line 5 and select "Add breakpoint":
debugbreakpoint
We can now start the debugger, for example by clicking on the continueDebug button. We will stop on the line with the breakpoint:
debugstoponbreakpoint
If we look on the "Variables" tab, we see:
debugstopvariables
If we click on the continueDebug button again, we see:
debugstopvariables2

See also


Categories: debugging | tutorials

Copyright 2019-2020 Herve Girod. All Rights Reserved. Documentation and source under the BSD licence