Search in sources :

Example 1 with Insight

use of org.graalvm.tools.insight.Insight in project graal by oracle.

the class InsightObjectTest method closeOfInsightHandleIsChecked.

@Test
public void closeOfInsightHandleIsChecked() throws Exception {
    try (Context c = InsightObjectFactory.newContext()) {
        AutoCloseable[] handle = { null };
        Value agent = InsightObjectFactory.readInsight(c, null, handle);
        InsightAPI agentAPI = agent.as(InsightAPI.class);
        Assert.assertNotNull("Agent API obtained", agentAPI);
        // @formatter:off
        Source sampleScript = Source.newBuilder(InstrumentationTestLanguage.ID, "ROOT(\n" + "  DEFINE(meaning,\n" + "    EXPRESSION(\n" + "      CONSTANT(6),\n" + "      CONSTANT(7)\n" + "    )\n" + "  ),\n" + "  CALL(meaning)\n" + ")", "sample.px").build();
        // @formatter:on
        final InsightAPI.OnEventHandler return42 = (ctx, frame) -> {
            try {
                ctx.returnValue(Collections.emptyMap());
            } catch (RuntimeException ex) {
                assertTrue("Expecting TruffleException: " + ex, ex instanceof AbstractTruffleException);
            }
            ctx.returnNow(42);
        };
        agentAPI.on("return", return42, createConfig(true, false, false, "meaning.*", null));
        Value fourtyTwo = c.eval(sampleScript);
        assertEquals(42, fourtyTwo.asInt());
        handle[0].close();
        Value sixSeven = c.eval(sampleScript);
        assertEquals("Hook is no longer active", "(6+7)", sixSeven.asString());
        try {
            agentAPI.on("enter", return42, createConfig(true, false, false, "meaning.*", null));
            fail("Expecting exception");
        } catch (PolyglotException ex) {
            assertEquals("insight: The script has already been closed", ex.getMessage());
        }
    }
}
Also used : Context(org.graalvm.polyglot.Context) Arrays(java.util.Arrays) AbstractTruffleException(com.oracle.truffle.api.exception.AbstractTruffleException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PolyglotException(org.graalvm.polyglot.PolyglotException) Random(java.util.Random) Function(java.util.function.Function) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) InstrumentationTestLanguage(com.oracle.truffle.api.instrumentation.test.InstrumentationTestLanguage) ProxyLanguage(com.oracle.truffle.api.test.polyglot.ProxyLanguage) Map(java.util.Map) Source(org.graalvm.polyglot.Source) InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary) Assert.fail(org.junit.Assert.fail) URI(java.net.URI) LinkedList(java.util.LinkedList) Before(org.junit.Before) Instrument(org.graalvm.polyglot.Instrument) Executor(java.util.concurrent.Executor) Predicate(java.util.function.Predicate) Value(org.graalvm.polyglot.Value) Assert.assertNotNull(org.junit.Assert.assertNotNull) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Insight(org.graalvm.tools.insight.Insight) Executors(java.util.concurrent.Executors) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) Assert.assertFalse(org.junit.Assert.assertFalse) Context(org.graalvm.polyglot.Context) Assert(org.junit.Assert) Collections(java.util.Collections) InsightObjectFactory.createConfig(org.graalvm.tools.insight.test.InsightObjectFactory.createConfig) Assert.assertEquals(org.junit.Assert.assertEquals) Value(org.graalvm.polyglot.Value) AbstractTruffleException(com.oracle.truffle.api.exception.AbstractTruffleException) PolyglotException(org.graalvm.polyglot.PolyglotException) Source(org.graalvm.polyglot.Source) Test(org.junit.Test)

Example 2 with Insight

use of org.graalvm.tools.insight.Insight in project graal by oracle.

the class InsightObjectTest method iterateFrames.

@Test
public void iterateFrames() throws Exception {
    try (Context c = InsightObjectFactory.newContext()) {
        Value insight = InsightObjectFactory.readInsight(c, null);
        InsightAPI insightAPI = insight.as(InsightAPI.class);
        Assert.assertNotNull("Agent API obtained", insightAPI);
        // @formatter:off
        Source s1 = Source.newBuilder(InstrumentationTestLanguage.ID, "ROOT(\n" + "  DEFINE(s1,\n" + "    EXPRESSION(\n" + "      CONSTANT(6),\n" + "      CONSTANT(7)\n" + "    )\n" + "  )\n" + ")", "s1.px").build();
        Source s2internal = Source.newBuilder(InstrumentationTestLanguage.ID, "ROOT(\n" + "  DEFINE(s2,\n" + "    CALL(s1)\n" + "  )\n" + ")", "s2.px").internal(true).build();
        Source s3 = Source.newBuilder(InstrumentationTestLanguage.ID, "ROOT(\n" + "  CALL(s2)\n" + ")", "s3.px").build();
        // @formatter:on
        String[] firstSourceName = { "ignore" };
        List<InsightAPI.SourceSectionInfo> stackTraces = new ArrayList<>();
        final InsightAPI.OnEventHandler threadDump = (ctx, frame) -> {
            InsightAPI.FramesIterator<String> it = (loc, vars) -> {
                stackTraces.add(loc);
                return firstSourceName[0] == null ? loc.source().name() : null;
            };
            String value = ctx.iterateFrames(it);
            if (firstSourceName[0] == null) {
                firstSourceName[0] = value;
            }
        };
        insightAPI.on("return", threadDump, createConfig(true, false, false, "s1", null));
        c.eval(s1);
        c.eval(s2internal);
        c.eval(s3);
        assertEquals("Two frames", 2, stackTraces.size());
        assertEquals("s1.px", stackTraces.get(0).source().name());
        assertEquals("s3.px", stackTraces.get(1).source().name());
        firstSourceName[0] = null;
        c.eval(s3);
        assertEquals("Third added", 3, stackTraces.size());
        assertEquals("s1.px", stackTraces.get(2).source().name());
    }
}
Also used : Context(org.graalvm.polyglot.Context) Arrays(java.util.Arrays) AbstractTruffleException(com.oracle.truffle.api.exception.AbstractTruffleException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PolyglotException(org.graalvm.polyglot.PolyglotException) Random(java.util.Random) Function(java.util.function.Function) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) InstrumentationTestLanguage(com.oracle.truffle.api.instrumentation.test.InstrumentationTestLanguage) ProxyLanguage(com.oracle.truffle.api.test.polyglot.ProxyLanguage) Map(java.util.Map) Source(org.graalvm.polyglot.Source) InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary) Assert.fail(org.junit.Assert.fail) URI(java.net.URI) LinkedList(java.util.LinkedList) Before(org.junit.Before) Instrument(org.graalvm.polyglot.Instrument) Executor(java.util.concurrent.Executor) Predicate(java.util.function.Predicate) Value(org.graalvm.polyglot.Value) Assert.assertNotNull(org.junit.Assert.assertNotNull) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Insight(org.graalvm.tools.insight.Insight) Executors(java.util.concurrent.Executors) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) Assert.assertFalse(org.junit.Assert.assertFalse) Context(org.graalvm.polyglot.Context) Assert(org.junit.Assert) Collections(java.util.Collections) InsightObjectFactory.createConfig(org.graalvm.tools.insight.test.InsightObjectFactory.createConfig) Assert.assertEquals(org.junit.Assert.assertEquals) Value(org.graalvm.polyglot.Value) ArrayList(java.util.ArrayList) Source(org.graalvm.polyglot.Source) Test(org.junit.Test)

Example 3 with Insight

use of org.graalvm.tools.insight.Insight in project graal by oracle.

the class InsightObjectTest method onErrorneousCallbackRegistration.

@Test
public void onErrorneousCallbackRegistration() throws Exception {
    try (Context c = InsightObjectFactory.newContext()) {
        Value agent = InsightObjectFactory.readInsight(c, null);
        InsightAPI agentAPI = agent.as(InsightAPI.class);
        Assert.assertNotNull("Agent API obtained", agentAPI);
        final InsightAPI.OnSourceLoadedHandler listener = (ev) -> {
        };
        agentAPI.on("enterOrLeave", listener);
        fail("Should have failed with PolyglotException");
    } catch (PolyglotException t) {
        assertTrue(t.getMessage(), t.getMessage().startsWith("insight: Unknown event type"));
    }
}
Also used : Context(org.graalvm.polyglot.Context) Arrays(java.util.Arrays) AbstractTruffleException(com.oracle.truffle.api.exception.AbstractTruffleException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PolyglotException(org.graalvm.polyglot.PolyglotException) Random(java.util.Random) Function(java.util.function.Function) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) InstrumentationTestLanguage(com.oracle.truffle.api.instrumentation.test.InstrumentationTestLanguage) ProxyLanguage(com.oracle.truffle.api.test.polyglot.ProxyLanguage) Map(java.util.Map) Source(org.graalvm.polyglot.Source) InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary) Assert.fail(org.junit.Assert.fail) URI(java.net.URI) LinkedList(java.util.LinkedList) Before(org.junit.Before) Instrument(org.graalvm.polyglot.Instrument) Executor(java.util.concurrent.Executor) Predicate(java.util.function.Predicate) Value(org.graalvm.polyglot.Value) Assert.assertNotNull(org.junit.Assert.assertNotNull) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Insight(org.graalvm.tools.insight.Insight) Executors(java.util.concurrent.Executors) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) Assert.assertFalse(org.junit.Assert.assertFalse) Context(org.graalvm.polyglot.Context) Assert(org.junit.Assert) Collections(java.util.Collections) InsightObjectFactory.createConfig(org.graalvm.tools.insight.test.InsightObjectFactory.createConfig) Assert.assertEquals(org.junit.Assert.assertEquals) Value(org.graalvm.polyglot.Value) PolyglotException(org.graalvm.polyglot.PolyglotException) Test(org.junit.Test)

Aggregations

AbstractTruffleException (com.oracle.truffle.api.exception.AbstractTruffleException)3 InstrumentationTestLanguage (com.oracle.truffle.api.instrumentation.test.InstrumentationTestLanguage)3 InteropLibrary (com.oracle.truffle.api.interop.InteropLibrary)3 ProxyLanguage (com.oracle.truffle.api.test.polyglot.ProxyLanguage)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 URI (java.net.URI)3 ArrayList (java.util.ArrayList)3 Arrays (java.util.Arrays)3 Collections (java.util.Collections)3 LinkedList (java.util.LinkedList)3 List (java.util.List)3 Map (java.util.Map)3 Random (java.util.Random)3 Set (java.util.Set)3 TreeSet (java.util.TreeSet)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 Executor (java.util.concurrent.Executor)3 Executors (java.util.concurrent.Executors)3 TimeUnit (java.util.concurrent.TimeUnit)3 Function (java.util.function.Function)3