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());
}
}
}
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());
}
}
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"));
}
}
Aggregations