Search in sources :

Example 6 with Instrument

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

the class InstrumentationTest method testQueryTags2.

/*
     * Test behavior of queryTags when used with languages
     */
@Test
public void testQueryTags2() throws IOException {
    Instrument instrument = engine.getInstruments().get("testIsNodeTaggedWith1");
    assureEnabled(instrument);
    TestIsNodeTaggedWith1.expressionNode = null;
    TestIsNodeTaggedWith1.statementNode = null;
    TestIsNodeTaggedWith1Language.instrumenter = null;
    Source otherLanguageSource = Source.create("testIsNodeTaggedWith1-lang", "STATEMENT(EXPRESSION)");
    run(otherLanguageSource);
    Instrumenter instrumenter = TestIsNodeTaggedWith1Language.instrumenter;
    Node languageExpression = TestIsNodeTaggedWith1.expressionNode;
    Node languageStatement = TestIsNodeTaggedWith1.statementNode;
    assertTags(instrumenter.queryTags(languageExpression), InstrumentationTestLanguage.EXPRESSION);
    assertTags(instrumenter.queryTags(languageStatement), InstrumentationTestLanguage.STATEMENT);
    TestIsNodeTaggedWith1.expressionNode = null;
    TestIsNodeTaggedWith1.statementNode = null;
    run("EXPRESSION");
    // fail if called with nodes from a different language
    Node otherLanguageExpression = TestIsNodeTaggedWith1.expressionNode;
    try {
        instrumenter.queryTags(otherLanguageExpression);
        Assert.fail();
    } catch (IllegalArgumentException e) {
    }
}
Also used : RootNode(com.oracle.truffle.api.nodes.RootNode) DirectCallNode(com.oracle.truffle.api.nodes.DirectCallNode) ExecutableNode(com.oracle.truffle.api.nodes.ExecutableNode) Node(com.oracle.truffle.api.nodes.Node) ExecutionEventNode(com.oracle.truffle.api.instrumentation.ExecutionEventNode) InstrumentableNode(com.oracle.truffle.api.instrumentation.InstrumentableNode) Instrument(org.graalvm.polyglot.Instrument) TruffleInstrument(com.oracle.truffle.api.instrumentation.TruffleInstrument) Instrumenter(com.oracle.truffle.api.instrumentation.Instrumenter) Source(org.graalvm.polyglot.Source) Test(org.junit.Test)

Example 7 with Instrument

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

the class InstrumentationTest method testLanguageInitializedOnly.

@Test
public void testLanguageInitializedOnly() throws Exception {
    Source initSource = Source.create(InstrumentationTestLanguage.ID, "STATEMENT(EXPRESSION, EXPRESSION)");
    setupEngine(initSource, false);
    Instrument instrument = engine.getInstruments().get("testLangInitialized");
    // Events during language initialization phase are excluded:
    TestLangInitialized.initializationEvents = false;
    TestLangInitialized service = instrument.lookup(TestLangInitialized.class);
    run("LOOP(2, STATEMENT())");
    assertEquals("[FunctionRootNode, true, LoopNode, true, StatementNode, true, StatementNode, true]", service.getEnteredNodes());
    engine.close();
    engine = null;
}
Also used : Instrument(org.graalvm.polyglot.Instrument) TruffleInstrument(com.oracle.truffle.api.instrumentation.TruffleInstrument) Source(org.graalvm.polyglot.Source) Test(org.junit.Test)

Example 8 with Instrument

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

the class InstrumentationTest method testAllocation.

@Test
public void testAllocation() throws Exception {
    Instrument instrument = engine.getInstruments().get("testAllocation");
    assureEnabled(instrument);
    TestAllocation allocation = instrument.lookup(TestAllocation.class);
    run("LOOP(3, VARIABLE(a, 10))");
    engine.close();
    engine = null;
    assertEquals("[W 4 null, A 4 10, W 4 null, A 4 10, W 4 null, A 4 10]", allocation.getAllocations());
}
Also used : Instrument(org.graalvm.polyglot.Instrument) TruffleInstrument(com.oracle.truffle.api.instrumentation.TruffleInstrument) Test(org.junit.Test)

Example 9 with Instrument

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

the class InstrumentationTest method testAccessInstruments.

@Test
public void testAccessInstruments() {
    Instrument instrument = engine.getInstruments().get("testAccessInstruments");
    TestAccessInstruments access = instrument.lookup(TestAccessInstruments.class);
    InstrumentInfo info = access.env.getInstruments().get("testAccessInstruments");
    assertNotNull(info);
    assertEquals("testAccessInstruments", info.getId());
    assertEquals("name", info.getName());
    assertEquals("version", info.getVersion());
    try {
        access.env.lookup(info, TestAccessInstruments.class);
        fail();
    } catch (IllegalArgumentException e) {
    // expected
    }
    TestAccessInstrumentsOther.initializedCount = 0;
    InstrumentInfo other = access.env.getInstruments().get("testAccessInstrumentsOther");
    assertNotNull(other);
    assertEquals("testAccessInstrumentsOther", other.getId());
    assertEquals("otherName", other.getName());
    assertEquals("otherVersion", other.getVersion());
    assertEquals(0, TestAccessInstrumentsOther.initializedCount);
    // invalid service, should not trigger onCreate
    assertNull(access.env.lookup(other, Object.class));
    assertEquals(0, TestAccessInstrumentsOther.initializedCount);
    // valide service, should trigger onCreate
    assertNotNull(access.env.lookup(other, TestAccessInstrumentsOther.class));
    assertEquals(1, TestAccessInstrumentsOther.initializedCount);
}
Also used : InstrumentInfo(com.oracle.truffle.api.InstrumentInfo) Instrument(org.graalvm.polyglot.Instrument) TruffleInstrument(com.oracle.truffle.api.instrumentation.TruffleInstrument) Test(org.junit.Test)

Example 10 with Instrument

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

the class SourceListenerTest method testLoadExecuteSourceImpl.

private void testLoadExecuteSourceImpl(boolean load, int runTimes) throws IOException {
    int initialQueryCount = InstrumentationTestLanguage.getRootSourceSectionQueryCount();
    Instrument instrument = context.getEngine().getInstruments().get("testLoadExecuteSource");
    Source source1 = lines("STATEMENT(EXPRESSION, EXPRESSION)");
    // running the same source multiple times should not have any effect on the test result.
    for (int i = 0; i < runTimes; i++) {
        run(source1);
    }
    Assert.assertEquals("unexpected getSourceSection calls without source listeners", initialQueryCount, InstrumentationTestLanguage.getRootSourceSectionQueryCount());
    TestLoadExecuteSource impl = instrument.lookup(TestLoadExecuteSource.class);
    assertTrue("Lookup of registered service enables the instrument", isCreated(instrument));
    if (load) {
        impl.attachLoad();
    } else {
        impl.attachExecute();
    }
    Source source2 = lines("ROOT(DEFINE(f1, STATEMENT(EXPRESSION)), DEFINE(f2, STATEMENT)," + "BLOCK(CALL(f1), CALL(f2)))");
    for (int i = 0; i < runTimes; i++) {
        run(source2);
    }
    Assert.assertNotEquals("expecting getSourceSection calls because of source listeners", initialQueryCount, InstrumentationTestLanguage.getRootSourceSectionQueryCount());
    assertEvents(impl.onlyNewEvents, source2);
    assertEvents(impl.allEvents, source1, source2);
    // Load an internal source
    Source source3 = Source.newBuilder(InstrumentationTestLanguage.ID, "STATEMENT", "test").internal(true).build();
    for (int i = 0; i < runTimes; i++) {
        run(source3);
    }
    assertEvents(impl.onlyNewEvents, source2, source3);
    assertEvents(impl.allEvents, source1, source2, source3);
    assertEvents(impl.allNotInternalEvents, source1, source2);
    // Disable the instrument by closing the engine.
    engine.close();
    engine = null;
    engine = getEngine();
    Source source4 = lines("STATEMENT(EXPRESSION, EXPRESSION, EXPRESSION)");
    for (int i = 0; i < runTimes; i++) {
        run(source4);
    }
    assertEvents(impl.onlyNewEvents, source2, source3);
    assertEvents(impl.allEvents, source1, source2, source3);
    instrument = engine.getInstruments().get("testLoadExecuteSource");
    impl = instrument.lookup(TestLoadExecuteSource.class);
    if (load) {
        impl.attachLoad();
    } else {
        impl.attachExecute();
    }
    assertEvents(impl.onlyNewEvents);
    assertEvents(impl.allEvents, source4);
}
Also used : Instrument(org.graalvm.polyglot.Instrument) TruffleInstrument(com.oracle.truffle.api.instrumentation.TruffleInstrument) Source(org.graalvm.polyglot.Source)

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