Search in sources :

Example 21 with Instrument

use of org.graalvm.polyglot.Instrument in project graal by oracle.

the class EvalLauncher method printVersions.

private static void printVersions() {
    Engine engine = Engine.create();
    System.out.println("GraalVM Polyglot Engine Version " + engine.getVersion());
    System.out.println("Installed Languages: ");
    for (Language language : engine.getLanguages().values()) {
        System.out.printf("    %-10s: %-10s Version %s%n", language.getId(), language.getName(), language.getVersion());
    }
    System.out.println("Installed Instruments: ");
    for (Instrument instrument : engine.getInstruments().values()) {
        System.out.printf("    %-10s: %-10s Version %s%n", instrument.getId(), instrument.getName(), instrument.getVersion());
    }
}
Also used : Language(org.graalvm.polyglot.Language) Instrument(org.graalvm.polyglot.Instrument) Engine(org.graalvm.polyglot.Engine)

Example 22 with Instrument

use of org.graalvm.polyglot.Instrument 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 23 with Instrument

use of org.graalvm.polyglot.Instrument in project graal by oracle.

the class SLInstrumentTest method testOutput.

@Test
public void testOutput() throws IOException {
    String code = "function main() {\n" + "  f = fac(5);\n" + "  println(f);\n" + "}\n" + "function fac(n) {\n" + "  println(n);\n" + "  if (n <= 1) {\n" + // break
    "    return 1;\n" + "  }\n" + "  return n * fac(n - 1);\n" + "}\n";
    String fullOutput = "5\n4\n3\n2\n1\n120\n";
    String fullLines = "[5, 4, 3, 2, 1, 120]";
    // Pure exec:
    Source source = Source.newBuilder("sl", code, "testing").build();
    ByteArrayOutputStream engineOut = new ByteArrayOutputStream();
    Engine engine = Engine.newBuilder().out(engineOut).build();
    Context context = Context.newBuilder().engine(engine).build();
    context.eval(source);
    String engineOutput = fullOutput;
    assertEquals(engineOutput, engineOut.toString());
    // Check output
    Instrument outInstr = engine.getInstruments().get("testEnvironmentHandlerInstrument");
    TruffleInstrument.Env env = outInstr.lookup(Environment.class).env;
    ByteArrayOutputStream consumedOut = new ByteArrayOutputStream();
    EventBinding<ByteArrayOutputStream> outputConsumerBinding = env.getInstrumenter().attachOutConsumer(consumedOut);
    assertEquals(0, consumedOut.size());
    context.eval(source);
    BufferedReader fromOutReader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(consumedOut.toByteArray())));
    engineOutput = engineOutput + fullOutput;
    assertEquals(engineOutput, engineOut.toString());
    assertTrue(fromOutReader.ready());
    assertEquals(fullLines, readLinesList(fromOutReader));
    // Check two output readers
    ByteArrayOutputStream consumedOut2 = new ByteArrayOutputStream();
    EventBinding<ByteArrayOutputStream> outputConsumerBinding2 = env.getInstrumenter().attachOutConsumer(consumedOut2);
    assertEquals(0, consumedOut2.size());
    context.eval(source);
    fromOutReader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(consumedOut.toByteArray())));
    BufferedReader fromOutReader2 = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(consumedOut2.toByteArray())));
    engineOutput = engineOutput + fullOutput;
    assertEquals(engineOutput, engineOut.toString());
    assertTrue(fromOutReader.ready());
    assertTrue(fromOutReader2.ready());
    String fullLines2x = fullLines.substring(0, fullLines.length() - 1) + ", " + fullLines.substring(1);
    assertEquals(fullLines2x, readLinesList(fromOutReader));
    assertEquals(fullLines, readLinesList(fromOutReader2));
    // One output reader closes, the other still receives the output
    outputConsumerBinding.dispose();
    consumedOut.reset();
    consumedOut2.reset();
    context.eval(source);
    engineOutput = engineOutput + fullOutput;
    assertEquals(engineOutput, engineOut.toString());
    assertEquals(0, consumedOut.size());
    assertTrue(consumedOut2.size() > 0);
    fromOutReader2 = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(consumedOut2.toByteArray())));
    assertEquals(fullLines, readLinesList(fromOutReader2));
    // Remaining closes and pure exec successful:
    consumedOut2.reset();
    outputConsumerBinding2.dispose();
    context.eval(source);
    engineOutput = engineOutput + fullOutput;
    assertEquals(engineOutput, engineOut.toString());
    assertEquals(0, consumedOut.size());
    assertEquals(0, consumedOut2.size());
}
Also used : Context(org.graalvm.polyglot.Context) EventContext(com.oracle.truffle.api.instrumentation.EventContext) TruffleInstrument(com.oracle.truffle.api.instrumentation.TruffleInstrument) InputStreamReader(java.io.InputStreamReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Source(org.graalvm.polyglot.Source) ByteArrayInputStream(java.io.ByteArrayInputStream) Instrument(org.graalvm.polyglot.Instrument) TruffleInstrument(com.oracle.truffle.api.instrumentation.TruffleInstrument) BufferedReader(java.io.BufferedReader) Engine(org.graalvm.polyglot.Engine) Test(org.junit.Test)

Example 24 with Instrument

use of org.graalvm.polyglot.Instrument in project graal by oracle.

the class InstrumentationTest method testDefaultId.

/*
     * Test that metadata is properly propagated to Instrument handles.
     */
@Test
public void testDefaultId() {
    Instrument descriptor1 = engine.getInstruments().get(MetadataInstrument2.class.getSimpleName());
    Assert.assertEquals("", descriptor1.getName());
    Assert.assertEquals("", descriptor1.getVersion());
    Assert.assertEquals(MetadataInstrument2.class.getSimpleName(), descriptor1.getId());
    Assert.assertFalse(isInitialized(descriptor1));
}
Also used : Instrument(org.graalvm.polyglot.Instrument) TruffleInstrument(com.oracle.truffle.api.instrumentation.TruffleInstrument) Test(org.junit.Test)

Example 25 with Instrument

use of org.graalvm.polyglot.Instrument in project graal by oracle.

the class InstrumentationTest method testMetadata.

/*
     * Test that metadata is properly propagated to Instrument handles.
     */
@Test
public void testMetadata() {
    Instrument instrumentHandle1 = engine.getInstruments().get("testMetadataType1");
    Assert.assertEquals("name", instrumentHandle1.getName());
    Assert.assertEquals("version", instrumentHandle1.getVersion());
    Assert.assertEquals("testMetadataType1", instrumentHandle1.getId());
    Assert.assertFalse(isInitialized(instrumentHandle1));
    Assert.assertFalse(isCreated(instrumentHandle1));
}
Also used : Instrument(org.graalvm.polyglot.Instrument) TruffleInstrument(com.oracle.truffle.api.instrumentation.TruffleInstrument) Test(org.junit.Test)

Aggregations

Instrument (org.graalvm.polyglot.Instrument)46 TruffleInstrument (com.oracle.truffle.api.instrumentation.TruffleInstrument)37 Test (org.junit.Test)33 Source (org.graalvm.polyglot.Source)14 Context (org.graalvm.polyglot.Context)12 TruffleContext (com.oracle.truffle.api.TruffleContext)11 Engine (org.graalvm.polyglot.Engine)10 EventContext (com.oracle.truffle.api.instrumentation.EventContext)7 ArrayList (java.util.ArrayList)5 Node (com.oracle.truffle.api.nodes.Node)4 RootNode (com.oracle.truffle.api.nodes.RootNode)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 Language (org.graalvm.polyglot.Language)3 ExecutionEventNode (com.oracle.truffle.api.instrumentation.ExecutionEventNode)2 InstrumentableNode (com.oracle.truffle.api.instrumentation.InstrumentableNode)2 Instrumenter (com.oracle.truffle.api.instrumentation.Instrumenter)2 DirectCallNode (com.oracle.truffle.api.nodes.DirectCallNode)2 ExecutableNode (com.oracle.truffle.api.nodes.ExecutableNode)2 List (java.util.List)2 OptionDescriptor (org.graalvm.options.OptionDescriptor)2