Home
Categories
Dictionary
Download
Project Details
Changes Log
FAQ
License

Inserting step instructions in debug


The debugger does not use the Java Platform Debugger Architecture. It works by inserting "shadow" step instructions in the script source code before creating a debug session, allowing to execute the debugging session in the context of the calling program, and even if the scripting language does not support "standard" debugging.

Debugging mechanism

These steps instructions have the following interface:

public class ScriptContext


Modifier and Type Method and Description
void step(int lineNumber, Object... args)
Call the step instruction with the specified line number in the original script and the values of all the variables which are accessible in the associated line

Each step instruction has the following arguments:
  • The line number of the initial script
  • The values of each variable accessible from the step line, including the fields
When the code execution goes on this instruction, the control flow is passed to the Debug session which will decide if:
  • The execution must continue through the next instruction
  • The execution must pause at the step. The values of the variables at this step can be retrieved in the debug session
It is possible to set a breakpoint at any step line, in which case the execution flow will pause, if the breakpoint conditions are met.

Example

Suppose the script interface:
      public interface Script {
        public int execute();
      }
And the script which implements this interface in Groovy:
      public int execute() {
        int a = 10;
        a = a + 1;
        return a;
      }
The code of the script after inserting the boilerplate code will be:
      import org.scripthelper.context.ScriptContext;
      import org.scripthelper.context.ScriptHelper;
      import org.scripthelper.context.DefaultScriptContext;
      import org.scripthelper.groovy.debug.parser.Script;
      class GroovyClass implements org.scripthelper.groovy.debug.parser.Script, org.scripthelper.context.ContextListener {
        DefaultScriptContext context;
        public void init(ScriptContext ctx) {
          context = (DefaultScriptContext)ctx;
        }
        public int execute() {
          int a = 10;
          a = a + 1;
          return a;
        }
      }      
And after inserting step instructions:
      import org.scripthelper.context.ScriptContext;
      import org.scripthelper.context.ScriptHelper;
      import org.scripthelper.context.DefaultScriptContext;
      import org.scripthelper.groovy.debug.parser.Script;
      class GroovyClass implements org.scripthelper.groovy.debug.parser.Script, org.scripthelper.context.ContextListener {
        DefaultScriptContext context;
        public void init(ScriptContext ctx) {
          context = (DefaultScriptContext)ctx;
        }
        public int execute() {
          context.__step__(1,this.context);
          int a = 10;
          context.__step__(2,a,this.context);
          a = a + 1;
          context.__step__(3,a,this.context);
          return a;
        }
      }      

Categories: debugging | dev

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