Search in sources :

Example 1 with Frame

use of org.mvel2.debug.Frame in project mvel by mvel.

the class Analyzer method analyze.

/**
 * Analyzes the given method.
 *
 * @param owner
 *            the internal name of the class to which the method belongs.
 * @param m
 *            the method to be analyzed.
 * @return the symbolic state of the execution stack frame at each bytecode
 *         instruction of the method. The size of the returned array is
 *         equal to the number of instructions (and labels) of the method. A
 *         given frame is <tt>null</tt> if and only if the corresponding
 *         instruction cannot be reached (dead code).
 * @throws AnalyzerException
 *             if a problem occurs during the analysis.
 */
@SuppressWarnings("unchecked")
public Frame<V>[] analyze(final String owner, final MethodNode m) throws AnalyzerException {
    if ((m.access & (ACC_ABSTRACT | ACC_NATIVE)) != 0) {
        frames = (Frame<V>[]) new Frame<?>[0];
        return frames;
    }
    n = m.instructions.size();
    insns = m.instructions;
    handlers = (List<TryCatchBlockNode>[]) new List<?>[n];
    frames = (Frame<V>[]) new Frame<?>[n];
    subroutines = new Subroutine[n];
    queued = new boolean[n];
    queue = new int[n];
    top = 0;
    // computes exception handlers for each instruction
    for (int i = 0; i < m.tryCatchBlocks.size(); ++i) {
        TryCatchBlockNode tcb = m.tryCatchBlocks.get(i);
        int begin = insns.indexOf(tcb.start);
        int end = insns.indexOf(tcb.end);
        for (int j = begin; j < end; ++j) {
            List<TryCatchBlockNode> insnHandlers = handlers[j];
            if (insnHandlers == null) {
                insnHandlers = new ArrayList<TryCatchBlockNode>();
                handlers[j] = insnHandlers;
            }
            insnHandlers.add(tcb);
        }
    }
    // computes the subroutine for each instruction:
    Subroutine main = new Subroutine(null, m.maxLocals, null);
    List<AbstractInsnNode> subroutineCalls = new ArrayList<AbstractInsnNode>();
    Map<LabelNode, Subroutine> subroutineHeads = new HashMap<LabelNode, Subroutine>();
    findSubroutine(0, main, subroutineCalls);
    while (!subroutineCalls.isEmpty()) {
        JumpInsnNode jsr = (JumpInsnNode) subroutineCalls.remove(0);
        Subroutine sub = subroutineHeads.get(jsr.label);
        if (sub == null) {
            sub = new Subroutine(jsr.label, m.maxLocals, jsr);
            subroutineHeads.put(jsr.label, sub);
            findSubroutine(insns.indexOf(jsr.label), sub, subroutineCalls);
        } else {
            sub.callers.add(jsr);
        }
    }
    for (int i = 0; i < n; ++i) {
        if (subroutines[i] != null && subroutines[i].start == null) {
            subroutines[i] = null;
        }
    }
    // initializes the data structures for the control flow analysis
    Frame<V> current = newFrame(m.maxLocals, m.maxStack);
    Frame<V> handler = newFrame(m.maxLocals, m.maxStack);
    current.setReturn(interpreter.newValue(Type.getReturnType(m.desc)));
    Type[] args = Type.getArgumentTypes(m.desc);
    int local = 0;
    if ((m.access & ACC_STATIC) == 0) {
        Type ctype = Type.getObjectType(owner);
        current.setLocal(local++, interpreter.newValue(ctype));
    }
    for (int i = 0; i < args.length; ++i) {
        current.setLocal(local++, interpreter.newValue(args[i]));
        if (args[i].getSize() == 2) {
            current.setLocal(local++, interpreter.newValue(null));
        }
    }
    while (local < m.maxLocals) {
        current.setLocal(local++, interpreter.newValue(null));
    }
    merge(0, current, null);
    init(owner, m);
    // control flow analysis
    while (top > 0) {
        int insn = queue[--top];
        Frame<V> f = frames[insn];
        Subroutine subroutine = subroutines[insn];
        queued[insn] = false;
        AbstractInsnNode insnNode = null;
        try {
            insnNode = m.instructions.get(insn);
            int insnOpcode = insnNode.getOpcode();
            int insnType = insnNode.getType();
            if (insnType == AbstractInsnNode.LABEL || insnType == AbstractInsnNode.LINE || insnType == AbstractInsnNode.FRAME) {
                merge(insn + 1, f, subroutine);
                newControlFlowEdge(insn, insn + 1);
            } else {
                current.init(f).execute(insnNode, interpreter);
                subroutine = subroutine == null ? null : subroutine.copy();
                if (insnNode instanceof JumpInsnNode) {
                    JumpInsnNode j = (JumpInsnNode) insnNode;
                    if (insnOpcode != GOTO && insnOpcode != JSR) {
                        merge(insn + 1, current, subroutine);
                        newControlFlowEdge(insn, insn + 1);
                    }
                    int jump = insns.indexOf(j.label);
                    if (insnOpcode == JSR) {
                        merge(jump, current, new Subroutine(j.label, m.maxLocals, j));
                    } else {
                        merge(jump, current, subroutine);
                    }
                    newControlFlowEdge(insn, jump);
                } else if (insnNode instanceof LookupSwitchInsnNode) {
                    LookupSwitchInsnNode lsi = (LookupSwitchInsnNode) insnNode;
                    int jump = insns.indexOf(lsi.dflt);
                    merge(jump, current, subroutine);
                    newControlFlowEdge(insn, jump);
                    for (int j = 0; j < lsi.labels.size(); ++j) {
                        LabelNode label = lsi.labels.get(j);
                        jump = insns.indexOf(label);
                        merge(jump, current, subroutine);
                        newControlFlowEdge(insn, jump);
                    }
                } else if (insnNode instanceof TableSwitchInsnNode) {
                    TableSwitchInsnNode tsi = (TableSwitchInsnNode) insnNode;
                    int jump = insns.indexOf(tsi.dflt);
                    merge(jump, current, subroutine);
                    newControlFlowEdge(insn, jump);
                    for (int j = 0; j < tsi.labels.size(); ++j) {
                        LabelNode label = tsi.labels.get(j);
                        jump = insns.indexOf(label);
                        merge(jump, current, subroutine);
                        newControlFlowEdge(insn, jump);
                    }
                } else if (insnOpcode == RET) {
                    if (subroutine == null) {
                        throw new AnalyzerException(insnNode, "RET instruction outside of a sub routine");
                    }
                    for (int i = 0; i < subroutine.callers.size(); ++i) {
                        JumpInsnNode caller = subroutine.callers.get(i);
                        int call = insns.indexOf(caller);
                        if (frames[call] != null) {
                            merge(call + 1, frames[call], current, subroutines[call], subroutine.access);
                            newControlFlowEdge(insn, call + 1);
                        }
                    }
                } else if (insnOpcode != ATHROW && (insnOpcode < IRETURN || insnOpcode > RETURN)) {
                    if (subroutine != null) {
                        if (insnNode instanceof VarInsnNode) {
                            int var = ((VarInsnNode) insnNode).var;
                            subroutine.access[var] = true;
                            if (insnOpcode == LLOAD || insnOpcode == DLOAD || insnOpcode == LSTORE || insnOpcode == DSTORE) {
                                subroutine.access[var + 1] = true;
                            }
                        } else if (insnNode instanceof IincInsnNode) {
                            int var = ((IincInsnNode) insnNode).var;
                            subroutine.access[var] = true;
                        }
                    }
                    merge(insn + 1, current, subroutine);
                    newControlFlowEdge(insn, insn + 1);
                }
            }
            List<TryCatchBlockNode> insnHandlers = handlers[insn];
            if (insnHandlers != null) {
                for (int i = 0; i < insnHandlers.size(); ++i) {
                    TryCatchBlockNode tcb = insnHandlers.get(i);
                    Type type;
                    if (tcb.type == null) {
                        type = Type.getObjectType("java/lang/Throwable");
                    } else {
                        type = Type.getObjectType(tcb.type);
                    }
                    int jump = insns.indexOf(tcb.handler);
                    if (newControlFlowExceptionEdge(insn, tcb)) {
                        handler.init(f);
                        handler.clearStack();
                        handler.push(interpreter.newValue(type));
                        merge(jump, handler, subroutine);
                    }
                }
            }
        } catch (AnalyzerException e) {
            throw new AnalyzerException(e.node, "Error at instruction " + insn + ": " + e.getMessage(), e);
        } catch (Exception e) {
            throw new AnalyzerException(insnNode, "Error at instruction " + insn + ": " + e.getMessage(), e);
        }
    }
    return frames;
}
Also used : LabelNode(org.mvel2.asm.tree.LabelNode) TryCatchBlockNode(org.mvel2.asm.tree.TryCatchBlockNode) TableSwitchInsnNode(org.mvel2.asm.tree.TableSwitchInsnNode) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) AbstractInsnNode(org.mvel2.asm.tree.AbstractInsnNode) Type(org.mvel2.asm.Type) JumpInsnNode(org.mvel2.asm.tree.JumpInsnNode) IincInsnNode(org.mvel2.asm.tree.IincInsnNode) ArrayList(java.util.ArrayList) List(java.util.List) InsnList(org.mvel2.asm.tree.InsnList) LookupSwitchInsnNode(org.mvel2.asm.tree.LookupSwitchInsnNode) VarInsnNode(org.mvel2.asm.tree.VarInsnNode)

Example 2 with Frame

use of org.mvel2.debug.Frame in project mvel by mvel.

the class LocalVariablesSorter method visitFrame.

@Override
public void visitFrame(final int type, final int nLocal, final Object[] local, final int nStack, final Object[] stack) {
    if (type != Opcodes.F_NEW) {
        // uncompressed frame
        throw new IllegalStateException("ClassReader.accept() should be called with EXPAND_FRAMES flag");
    }
    // creates a copy of newLocals
    Object[] oldLocals = new Object[newLocals.length];
    System.arraycopy(newLocals, 0, oldLocals, 0, oldLocals.length);
    updateNewLocals(newLocals);
    // copies types from 'local' to 'newLocals'
    // 'newLocals' already contains the variables added with 'newLocal'
    // old local variable index
    int index = 0;
    // old local variable number
    int number = 0;
    for (; number < nLocal; ++number) {
        Object t = local[number];
        int size = t == Opcodes.LONG || t == Opcodes.DOUBLE ? 2 : 1;
        if (t != Opcodes.TOP) {
            Type typ = OBJECT_TYPE;
            if (t == Opcodes.INTEGER) {
                typ = Type.INT_TYPE;
            } else if (t == Opcodes.FLOAT) {
                typ = Type.FLOAT_TYPE;
            } else if (t == Opcodes.LONG) {
                typ = Type.LONG_TYPE;
            } else if (t == Opcodes.DOUBLE) {
                typ = Type.DOUBLE_TYPE;
            } else if (t instanceof String) {
                typ = Type.getObjectType((String) t);
            }
            setFrameLocal(remap(index, typ), t);
        }
        index += size;
    }
    // removes TOP after long and double types as well as trailing TOPs
    index = 0;
    number = 0;
    for (int i = 0; index < newLocals.length; ++i) {
        Object t = newLocals[index++];
        if (t != null && t != Opcodes.TOP) {
            newLocals[i] = t;
            number = i + 1;
            if (t == Opcodes.LONG || t == Opcodes.DOUBLE) {
                index += 1;
            }
        } else {
            newLocals[i] = Opcodes.TOP;
        }
    }
    // visits remapped frame
    mv.visitFrame(type, number, newLocals, nStack, stack);
    // restores original value of 'newLocals'
    newLocals = oldLocals;
}
Also used : Type(org.mvel2.asm.Type)

Example 3 with Frame

use of org.mvel2.debug.Frame in project mvel by mvel.

the class DebuggerTests method testDebuggerInvoke.

public void testDebuggerInvoke() {
    count = 0;
    MVELRuntime.resetDebugger();
    MVELRuntime.setThreadDebugger(new Debugger() {

        public int onBreak(Frame frame) {
            if (frame.getFactory().isResolveable("a1")) {
                a1++;
            }
            if (frame.getFactory().isResolveable("a4")) {
                a4++;
                System.out.println("HEI " + frame.getLineNumber());
            }
            count++;
            return 0;
        }
    });
    String src = "a1=7;\na2=8;\na3=9;\na4=10;\na5=11;\na6=12;\na7=13;\na8=14;";
    ParserContext ctx = new ParserContext();
    ctx.setSourceFile("mysource");
    ctx.setDebugSymbols(true);
    ExpressionCompiler c = new ExpressionCompiler(src, ctx);
    CompiledExpression compexpr = c.compile();
    System.out.println(decompile(compexpr));
    MVELRuntime.registerBreakpoint(ctx.getSourceFile(), 1);
    MVELRuntime.registerBreakpoint(ctx.getSourceFile(), 3);
    MVELRuntime.registerBreakpoint(ctx.getSourceFile(), 7);
    VariableResolverFactory factory = new DefaultLocalVariableResolverFactory();
    MVEL.executeDebugger(compexpr, null, factory);
    System.out.println(a1);
    System.out.println(a4);
    System.out.println(count);
    assertEquals(2, a1);
    // test passes but the breakpoint should be received by line 7, not by line 3
    assertEquals(1, a4);
    // three breakpoints FAILS
    assertEquals(3, count);
}
Also used : Debugger(org.mvel2.debug.Debugger) Frame(org.mvel2.debug.Frame) DefaultLocalVariableResolverFactory(org.mvel2.integration.impl.DefaultLocalVariableResolverFactory) VariableResolverFactory(org.mvel2.integration.VariableResolverFactory) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) DefaultLocalVariableResolverFactory(org.mvel2.integration.impl.DefaultLocalVariableResolverFactory) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) ParserContext(org.mvel2.ParserContext) CompiledExpression(org.mvel2.compiler.CompiledExpression)

Example 4 with Frame

use of org.mvel2.debug.Frame in project mvel by mvel.

the class DebuggerTests method testBreakpointsAcrossComments2.

public void testBreakpointsAcrossComments2() {
    ParserContext ctx = new ParserContext();
    ctx.setSourceFile("test2.mv");
    ctx.setDebugSymbols(true);
    ExpressionCompiler compiler = new ExpressionCompiler(// 1
    "// This is a comment\n" + // 2
    "//Second comment line\n" + // 3
    "//Third Comment Line\n" + // 4
    "\n" + // 5
    "//Test\n" + // 6
    "System.out.println('4');\n" + // 7
    "//System.out.println('5'); \n" + // 8
    "a = 0;\n" + // 9
    "b = 1;\n" + " a + b", // 10
    ctx);
    CompiledExpression compiled = compiler.compile();
    MVELRuntime.registerBreakpoint("test2.mv", 6);
    MVELRuntime.registerBreakpoint("test2.mv", 8);
    MVELRuntime.registerBreakpoint("test2.mv", 9);
    MVELRuntime.registerBreakpoint("test2.mv", 10);
    final Set<Integer> breaked = new HashSet<Integer>();
    Debugger testDebugger = new Debugger() {

        public int onBreak(Frame frame) {
            System.out.println("Breakpoint [source:" + frame.getSourceName() + "; line:" + frame.getLineNumber() + "]");
            breaked.add(frame.getLineNumber());
            return 0;
        }
    };
    MVELRuntime.setThreadDebugger(testDebugger);
    assertEquals(1, MVEL.executeDebugger(compiled, null, new MapVariableResolverFactory(createTestMap())));
    assertEquals("did not break at expected lines", Make.Set.<Integer>$()._(6)._(8)._(9)._(10)._(), breaked);
}
Also used : Debugger(org.mvel2.debug.Debugger) Frame(org.mvel2.debug.Frame) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) ParserContext(org.mvel2.ParserContext) CompiledExpression(org.mvel2.compiler.CompiledExpression) HashSet(java.util.HashSet)

Example 5 with Frame

use of org.mvel2.debug.Frame in project mvel by mvel.

the class DebuggerTests method testDebuggerInvoke2.

public void testDebuggerInvoke2() {
    count = 0;
    MVELRuntime.resetDebugger();
    MVELRuntime.setThreadDebugger(new Debugger() {

        public int onBreak(Frame frame) {
            count++;
            return 0;
        }
    });
    String src = "a1=7;\na2=8;\nSystem.out.println(\"h\");\nac=23;\nde=23;\nge=23;\ngef=34;";
    ParserContext ctx = new ParserContext();
    ctx.setSourceFile("mysource");
    ctx.setDebugSymbols(true);
    ExpressionCompiler c = new ExpressionCompiler(src, ctx);
    CompiledExpression compexpr = c.compile();
    System.out.println(decompile(compexpr));
    MVELRuntime.registerBreakpoint(ctx.getSourceFile(), 1);
    MVELRuntime.registerBreakpoint(ctx.getSourceFile(), 2);
    MVELRuntime.registerBreakpoint(ctx.getSourceFile(), 3);
    MVELRuntime.registerBreakpoint(ctx.getSourceFile(), 4);
    MVELRuntime.registerBreakpoint(ctx.getSourceFile(), 5);
    VariableResolverFactory factory = new DefaultLocalVariableResolverFactory();
    MVEL.executeDebugger(compexpr, null, factory);
    System.out.println(count);
    assertEquals(5, count);
}
Also used : Debugger(org.mvel2.debug.Debugger) Frame(org.mvel2.debug.Frame) DefaultLocalVariableResolverFactory(org.mvel2.integration.impl.DefaultLocalVariableResolverFactory) VariableResolverFactory(org.mvel2.integration.VariableResolverFactory) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) DefaultLocalVariableResolverFactory(org.mvel2.integration.impl.DefaultLocalVariableResolverFactory) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) ParserContext(org.mvel2.ParserContext) CompiledExpression(org.mvel2.compiler.CompiledExpression)

Aggregations

Debugger (org.mvel2.debug.Debugger)17 Frame (org.mvel2.debug.Frame)17 CompiledExpression (org.mvel2.compiler.CompiledExpression)16 ExpressionCompiler (org.mvel2.compiler.ExpressionCompiler)16 MapVariableResolverFactory (org.mvel2.integration.impl.MapVariableResolverFactory)16 ParserContext (org.mvel2.ParserContext)15 HashSet (java.util.HashSet)11 VariableResolverFactory (org.mvel2.integration.VariableResolverFactory)8 HashMap (java.util.HashMap)7 Interceptor (org.mvel2.integration.Interceptor)6 DefaultLocalVariableResolverFactory (org.mvel2.integration.impl.DefaultLocalVariableResolverFactory)6 Macro (org.mvel2.Macro)5 ASTNode (org.mvel2.ast.ASTNode)4 WithNode (org.mvel2.ast.WithNode)4 Type (org.mvel2.asm.Type)2 Foo (org.mvel2.tests.core.res.Foo)2 File (java.io.File)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Message (org.drools.compiler.Message)1