Search in sources :

Example 1 with ExecutionEventListener

use of com.oracle.truffle.api.instrumentation.ExecutionEventListener in project graal by oracle.

the class InputFilterTest method assertCleanedUp.

private void assertCleanedUp(String code) {
    // first we capture all root nodes used by the code.
    Set<RootNode> rootNodes = new HashSet<>();
    EventBinding<?> binding = instrumenter.attachExecutionEventListener(SourceSectionFilter.ANY, new ExecutionEventListener() {

        public void onEnter(EventContext c, VirtualFrame frame) {
            addRoot(c);
        }

        @TruffleBoundary
        private void addRoot(EventContext c) {
            rootNodes.add(c.getInstrumentedNode().getRootNode());
        }

        public void onReturnValue(EventContext c, VirtualFrame frame, Object result) {
        }

        public void onReturnExceptional(EventContext c, VirtualFrame frame, Throwable exception) {
        }
    });
    execute(code);
    binding.dispose();
    // we execute again to let the instrumentation wrappers be cleaned up
    execute(code);
    for (RootNode root : rootNodes) {
        // all frame slots got removed
        assertEquals(new HashSet<>(), root.getFrameDescriptor().getIdentifiers());
        // no wrappers left
        root.accept(new NodeVisitor() {

            public boolean visit(Node node) {
                if (node instanceof WrapperNode) {
                    throw new AssertionError();
                }
                return true;
            }
        });
    }
}
Also used : RootNode(com.oracle.truffle.api.nodes.RootNode) WrapperNode(com.oracle.truffle.api.instrumentation.InstrumentableNode.WrapperNode) Node(com.oracle.truffle.api.nodes.Node) RootNode(com.oracle.truffle.api.nodes.RootNode) WrapperNode(com.oracle.truffle.api.instrumentation.InstrumentableNode.WrapperNode) ExecutionEventListener(com.oracle.truffle.api.instrumentation.ExecutionEventListener) NodeVisitor(com.oracle.truffle.api.nodes.NodeVisitor) EventContext(com.oracle.truffle.api.instrumentation.EventContext) VirtualFrame(com.oracle.truffle.api.frame.VirtualFrame) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary) HashSet(java.util.HashSet)

Example 2 with ExecutionEventListener

use of com.oracle.truffle.api.instrumentation.ExecutionEventListener in project graal by oracle.

the class SLInstrumentTest method testLexicalScopes.

@Test
public void testLexicalScopes() throws Exception {
    String code = "function test(n) {\n" + // 2
    "  a = 1;\n" + "  if (a > 0) {\n" + "    b = 10;\n" + // 5
    "    println(b);\n" + "  }\n" + "  if (a == 1) {\n" + "    b = 20;\n" + "    a = 0;\n" + // 10
    "    c = 1;\n" + "    if (b > 0) {\n" + "      a = 4;\n" + "      b = 5;\n" + "      c = 6;\n" + // 15
    "      d = 7;\n" + "      println(d);\n" + "    }\n" + "  }\n" + "  println(b);\n" + // 20
    "  println(a);\n" + "}\n" + "function main() {\n" + "  test(\"n_n\");\n" + "}";
    Source source = Source.newBuilder("sl", code, "testing").build();
    List<Throwable> throwables;
    try (Engine engine = Engine.newBuilder().out(new java.io.OutputStream() {

        // null output stream
        @Override
        public void write(int b) throws IOException {
        }
    }).build()) {
        Instrument envInstr = engine.getInstruments().get("testEnvironmentHandlerInstrument");
        TruffleInstrument.Env env = envInstr.lookup(Environment.class).env;
        throwables = new ArrayList<>();
        env.getInstrumenter().attachExecutionEventListener(SourceSectionFilter.newBuilder().lineIn(1, source.getLineCount()).build(), new ExecutionEventListener() {

            @Override
            public void onEnter(EventContext context, VirtualFrame frame) {
                Node node = context.getInstrumentedNode();
                Iterable<Scope> lexicalScopes = env.findLocalScopes(node, null);
                Iterable<Scope> dynamicScopes = env.findLocalScopes(node, frame);
                try {
                    verifyLexicalScopes(lexicalScopes, dynamicScopes, context.getInstrumentedSourceSection().getStartLine(), frame.materialize());
                } catch (ThreadDeath t) {
                    throw t;
                } catch (Throwable t) {
                    CompilerDirectives.transferToInterpreter();
                    PrintStream lsErr = System.err;
                    lsErr.println("Line = " + context.getInstrumentedSourceSection().getStartLine());
                    lsErr.println("Node = " + node + ", class = " + node.getClass().getName());
                    t.printStackTrace(lsErr);
                    throwables.add(t);
                }
            }

            @Override
            public void onReturnValue(EventContext context, VirtualFrame frame, Object result) {
            }

            @Override
            public void onReturnExceptional(EventContext context, VirtualFrame frame, Throwable exception) {
            }
        });
        Context.newBuilder().engine(engine).build().eval(source);
    }
    assertTrue(throwables.toString(), throwables.isEmpty());
}
Also used : TruffleInstrument(com.oracle.truffle.api.instrumentation.TruffleInstrument) PrintStream(java.io.PrintStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) RootNode(com.oracle.truffle.api.nodes.RootNode) ProbeNode(com.oracle.truffle.api.instrumentation.ProbeNode) Node(com.oracle.truffle.api.nodes.Node) Source(org.graalvm.polyglot.Source) ExecutionEventListener(com.oracle.truffle.api.instrumentation.ExecutionEventListener) EventContext(com.oracle.truffle.api.instrumentation.EventContext) VirtualFrame(com.oracle.truffle.api.frame.VirtualFrame) Instrument(org.graalvm.polyglot.Instrument) TruffleInstrument(com.oracle.truffle.api.instrumentation.TruffleInstrument) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) Engine(org.graalvm.polyglot.Engine) Test(org.junit.Test)

Example 3 with ExecutionEventListener

use of com.oracle.truffle.api.instrumentation.ExecutionEventListener in project graal by oracle.

the class InstrumentationMultiThreadingTest method createDummyBindings.

private static EventBinding<?>[] createDummyBindings(Instrumenter instrumenter) {
    EventBinding<?>[] bindings = new EventBinding<?>[5];
    int bi = 0;
    ExecutionEventListener dummyListener = new ExecutionEventListener() {

        public void onReturnValue(EventContext context, VirtualFrame frame, Object result) {
        }

        public void onReturnExceptional(EventContext context, VirtualFrame frame, Throwable exception) {
        }

        public void onEnter(EventContext context, VirtualFrame frame) {
        }
    };
    bindings[bi++] = instrumenter.attachExecutionEventListener(SourceSectionFilter.newBuilder().tagIs(InstrumentationTestLanguage.EXPRESSION).build(), dummyListener);
    bindings[bi++] = instrumenter.attachExecutionEventListener(SourceSectionFilter.newBuilder().tagIs(InstrumentationTestLanguage.STATEMENT).build(), dummyListener);
    bindings[bi++] = instrumenter.attachLoadSourceListener(SourceFilter.ANY, new LoadSourceListener() {

        public void onLoad(LoadSourceEvent event) {
        }
    }, true);
    bindings[bi++] = instrumenter.attachLoadSourceSectionListener(SourceSectionFilter.newBuilder().tagIs(InstrumentationTestLanguage.EXPRESSION).build(), new LoadSourceSectionListener() {

        public void onLoad(LoadSourceSectionEvent event) {
        }
    }, true);
    bindings[bi++] = instrumenter.attachLoadSourceSectionListener(SourceSectionFilter.newBuilder().tagIs(InstrumentationTestLanguage.STATEMENT).build(), new LoadSourceSectionListener() {

        public void onLoad(LoadSourceSectionEvent event) {
        }
    }, true);
    return bindings;
}
Also used : EventContext(com.oracle.truffle.api.instrumentation.EventContext) VirtualFrame(com.oracle.truffle.api.frame.VirtualFrame) EventBinding(com.oracle.truffle.api.instrumentation.EventBinding) LoadSourceEvent(com.oracle.truffle.api.instrumentation.LoadSourceEvent) LoadSourceSectionEvent(com.oracle.truffle.api.instrumentation.LoadSourceSectionEvent) LoadSourceListener(com.oracle.truffle.api.instrumentation.LoadSourceListener) LoadSourceSectionListener(com.oracle.truffle.api.instrumentation.LoadSourceSectionListener) ExecutionEventListener(com.oracle.truffle.api.instrumentation.ExecutionEventListener)

Example 4 with ExecutionEventListener

use of com.oracle.truffle.api.instrumentation.ExecutionEventListener in project graal by oracle.

the class InstrumentationUpdateTest method setEventFilter.

private void setEventFilter(SourceSectionFilter filter) {
    EventBinding<?> old = this.eventBinding;
    eventBinding = instrumentEnv.getInstrumenter().attachExecutionEventListener(filter, new ExecutionEventListener() {

        public void onReturnValue(EventContext ctx, VirtualFrame frame, Object result) {
        }

        public void onReturnExceptional(EventContext ctx, VirtualFrame frame, Throwable exception) {
        }

        public void onEnter(EventContext ctx, VirtualFrame frame) {
            executionEvents.add(ctx);
        }
    });
    instrumentEnv.getInstrumenter().attachLoadSourceSectionListener(filter, new LoadSourceSectionListener() {

        public void onLoad(LoadSourceSectionEvent event) {
            loadEvents.add(event);
        }
    }, true);
    // dispose afterwards to avoid disposal of instrumentation wrappers
    if (old != null) {
        old.dispose();
    }
}
Also used : EventContext(com.oracle.truffle.api.instrumentation.EventContext) VirtualFrame(com.oracle.truffle.api.frame.VirtualFrame) LoadSourceSectionEvent(com.oracle.truffle.api.instrumentation.LoadSourceSectionEvent) LoadSourceSectionListener(com.oracle.truffle.api.instrumentation.LoadSourceSectionListener) ExecutionEventListener(com.oracle.truffle.api.instrumentation.ExecutionEventListener)

Aggregations

VirtualFrame (com.oracle.truffle.api.frame.VirtualFrame)4 EventContext (com.oracle.truffle.api.instrumentation.EventContext)4 ExecutionEventListener (com.oracle.truffle.api.instrumentation.ExecutionEventListener)4 LoadSourceSectionEvent (com.oracle.truffle.api.instrumentation.LoadSourceSectionEvent)2 LoadSourceSectionListener (com.oracle.truffle.api.instrumentation.LoadSourceSectionListener)2 Node (com.oracle.truffle.api.nodes.Node)2 RootNode (com.oracle.truffle.api.nodes.RootNode)2 TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)1 EventBinding (com.oracle.truffle.api.instrumentation.EventBinding)1 WrapperNode (com.oracle.truffle.api.instrumentation.InstrumentableNode.WrapperNode)1 LoadSourceEvent (com.oracle.truffle.api.instrumentation.LoadSourceEvent)1 LoadSourceListener (com.oracle.truffle.api.instrumentation.LoadSourceListener)1 ProbeNode (com.oracle.truffle.api.instrumentation.ProbeNode)1 TruffleInstrument (com.oracle.truffle.api.instrumentation.TruffleInstrument)1 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)1 NodeVisitor (com.oracle.truffle.api.nodes.NodeVisitor)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 PrintStream (java.io.PrintStream)1 HashSet (java.util.HashSet)1 Engine (org.graalvm.polyglot.Engine)1