use of org.graalvm.tools.insight.test.InsightObjectFactory.createConfig in project graal by oracle.
the class InsightObjectTest method onEnterCallback.
@Test
public void onEnterCallback() 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);
boolean[] program = { false };
String[] functionName = { null };
final InsightAPI.OnEventHandler listener = (ctx, frame) -> {
if (ctx.name().length() == 0) {
assertFalse("Program root is entered just once", program[0]);
program[0] = true;
return;
}
assertNull("No function entered yet", functionName[0]);
functionName[0] = ctx.name();
};
agentAPI.on("enter", listener, InsightObjectFactory.createConfig(false, false, true, null, null));
// @formatter:off
Source sampleScript = Source.newBuilder(InstrumentationTestLanguage.ID, "ROOT(\n" + " DEFINE(foo,\n" + " LOOP(10, STATEMENT(EXPRESSION,EXPRESSION))\n" + " ),\n" + " CALL(foo)\n" + ")", "sample.px").build();
// @formatter:on
c.eval(sampleScript);
assertTrue("Program started", program[0]);
assertEquals("Function foo has been called", "foo", functionName[0]);
agentAPI.off("enter", listener);
program[0] = false;
functionName[0] = null;
// @formatter:off
Source sampleScript2 = Source.newBuilder(InstrumentationTestLanguage.ID, "ROOT(\n" + " DEFINE(foo,\n" + " LOOP(10, STATEMENT(EXPRESSION,EXPRESSION))\n" + " ),\n" + " CALL(foo)\n" + ")", "sample.px").build();
// @formatter:on
c.eval(sampleScript2);
assertFalse("No listener notified", program[0]);
assertNull("No function entered", functionName[0]);
}
}
use of org.graalvm.tools.insight.test.InsightObjectFactory.createConfig in project graal by oracle.
the class InsightObjectTest method internalScriptsAreIgnored.
@Test
public void internalScriptsAreIgnored() throws Exception {
int[] closeCounter = { 0 };
try (Context c = InsightObjectFactory.newContext()) {
Value agent = InsightObjectFactory.readInsight(c, null);
InsightAPI agentAPI = agent.as(InsightAPI.class);
Assert.assertNotNull("Agent API obtained", agentAPI);
// @formatter:off
Source sampleScript = Source.newBuilder(InstrumentationTestLanguage.ID, "ROOT(\n" + " DEFINE(foo,\n" + " LOOP(10, STATEMENT(EXPRESSION,EXPRESSION))\n" + " ),\n" + " CALL(foo)\n" + ")", "sample.px").internal(true).build();
// @formatter:on
final InsightAPI.OnSourceLoadedHandler listener = (ev) -> {
if (ev.name().equals(sampleScript.getName())) {
Assert.fail("Don't load internal scripts: " + ev.uri());
}
};
agentAPI.on("source", listener);
int[] expressionCounter = { 0 };
agentAPI.on("enter", (ev, frame) -> {
expressionCounter[0]++;
}, InsightObjectFactory.createConfig(true, false, false, null, null));
agentAPI.on("return", (ev, frame) -> {
expressionCounter[0]++;
}, InsightObjectFactory.createConfig(true, false, false, null, null));
agentAPI.on("close", () -> {
closeCounter[0]++;
});
c.eval(sampleScript);
assertEquals("No expressions entered & exited", 0, expressionCounter[0]);
}
assertEquals("Close is reported", 1, closeCounter[0]);
}
use of org.graalvm.tools.insight.test.InsightObjectFactory.createConfig in project graal by oracle.
the class InsightObjectTest method evalFirstAndThenOnEnterCallbackImpl.
private static void evalFirstAndThenOnEnterCallbackImpl(Executor registerIn) throws Throwable {
try (Context c = InsightObjectFactory.newContext()) {
// @formatter:off
Source sampleScript = Source.newBuilder(InstrumentationTestLanguage.ID, "ROOT(\n" + " DEFINE(foo,\n" + " LOOP(10, STATEMENT(EXPRESSION,EXPRESSION))\n" + " ),\n" + " CALL(foo)\n" + ")", "sample.px").build();
// @formatter:on
c.eval(sampleScript);
Value agent = InsightObjectFactory.readInsight(c, null);
InsightAPI agentAPI = agent.as(InsightAPI.class);
Assert.assertNotNull("Agent API obtained", agentAPI);
String[] functionName = { null };
final InsightAPI.OnEventHandler listener = (ctx, frame) -> {
if (ctx.name().length() == 0) {
return;
}
assertNull("No function entered yet", functionName[0]);
functionName[0] = ctx.name();
assertNotEquals("Subset of the source found", -1, ctx.characters().indexOf("LOOP(10, STATEMENT(EXPRESSION,EXPRESSION))"));
assertEquals(14, ctx.column());
assertEquals(14, ctx.startColumn());
assertEquals(2, ctx.endColumn());
assertEquals(2, ctx.line());
assertEquals(2, ctx.startLine());
assertEquals(4, ctx.endLine());
String fullText = ctx.source().characters();
final int charIndex = ctx.charIndex();
final int charEndIndex = ctx.charEndIndex();
final int charLength = ctx.charLength();
final int begOffset = indexOfLine(fullText, 2) + 13;
final int endOffset = indexOfLine(fullText, 4) + 2;
assertEquals(begOffset, charIndex);
assertEquals(endOffset, charEndIndex);
assertEquals(endOffset - begOffset, charLength);
};
CountDownLatch await = new CountDownLatch(1);
Throwable[] err = { null };
registerIn.execute(() -> {
try {
agentAPI.on("enter", listener, InsightObjectFactory.createConfig(false, false, true, null, null));
} catch (Throwable t) {
err[0] = t;
} finally {
await.countDown();
}
});
await.await(10, TimeUnit.SECONDS);
if (err[0] != null) {
throw err[0];
}
// @formatter:off
Source runScript = Source.newBuilder(InstrumentationTestLanguage.ID, "ROOT(\n" + " CALL(foo)\n" + ")", "run.px").build();
// @formatter:on
c.eval(runScript);
assertEquals("Function foo has been called", "foo", functionName[0]);
}
}
use of org.graalvm.tools.insight.test.InsightObjectFactory.createConfig in project graal by oracle.
the class InsightObjectTest method accessFrameVariables.
@Test
public void accessFrameVariables() 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);
// @formatter:off
Source sampleScript = Source.newBuilder(InstrumentationTestLanguage.ID, "ROOT(\n" + " DEFINE(do_mul,\n" + " ARGUMENT(a),\n" + " ARGUMENT(b),\n" + " EXPRESSION\n" + " ),\n" + " DEFINE(mul,\n" + " ARGUMENT(a),\n" + " ARGUMENT(b),\n" + " CALL_WITH(do_mul, 1, READ_VAR(a), READ_VAR(b))\n" + " ),\n" + " CALL(mul, CONSTANT(6), CONSTANT(7))\n" + ")", "sample.px").build();
// @formatter:on
Set<String> names = new TreeSet<>();
final InsightAPI.OnEventHandler captureNames = (ctx, frame) -> {
assertTrue(names.isEmpty());
names.addAll(frame.keySet());
};
agentAPI.on("enter", captureNames, createConfig(true, false, false, "do_mul.*", null));
c.eval(sampleScript);
agentAPI.off("enter", captureNames);
Assert.assertArrayEquals("THIS, a and b found", new Object[] { "THIS", "a", "b" }, names.toArray());
Object[] values = { 0, 0, 0 };
agentAPI.on("enter", (ctx, frame) -> {
values[0] = frame.get("a");
values[1] = frame.get("b");
frame.put("a", 33);
assertEquals(Integer.valueOf(33), frame.get("a"));
frame.put("a", "ahoj");
assertEquals("ahoj", frame.get("a"));
try {
frame.put("c", 42);
fail("Expecting an exception when setting unknown variable c");
} catch (IllegalArgumentException t) {
if (!t.getMessage().contains("identifier 'c'")) {
fail(t.getMessage());
}
}
values[2] = frame.get("THIS");
try {
frame.put("THIS", 42);
fail("Expecting an exception when setting THIS");
} catch (IllegalArgumentException t) {
if (!t.getMessage().contains("identifier 'THIS'")) {
fail(t.getMessage());
}
}
}, InsightObjectFactory.createConfig(true, false, false, "do_mul", null));
Value mul = c.getBindings(InstrumentationTestLanguage.ID).getMember("mul");
assertNotNull("mul function found", mul);
assertTrue("mul function found", mul.canExecute());
Random r = new Random();
for (int i = 1; i <= 100000; i++) {
int a = r.nextInt();
int b = r.nextInt();
mul.execute(a, b);
assertEquals(i + "th: a has been read", a, values[0]);
assertEquals(i + "th: b has been read", b, values[1]);
assertEquals(i + "th: THIS has been read", 1, values[2]);
}
}
}
Aggregations