Search in sources :

Example 1 with DebugValue

use of com.oracle.truffle.api.debug.DebugValue in project graal by oracle.

the class ValueLanguageTest method testValueLanguage.

@Test
public void testValueLanguage() {
    Source source1 = Source.create(ValuesLanguage1.ID, "i=10\n" + "s=test\n" + "a=null\n" + "b={}\n" + "b.a={}\n" + "b.j=100\n" + "b.k=200\n");
    Source source2 = Source.create(ValuesLanguage2.ID, "j=20\n" + "s=test2\n" + "d=null\n" + "e={}\n" + "b.c={}\n" + "e.d={}\n" + "e.k=200\n");
    try (DebuggerSession session = startSession()) {
        Breakpoint bp1 = Breakpoint.newBuilder(getSourceImpl(source1)).lineIs(7).build();
        session.install(bp1);
        startEval(source1);
        expectSuspended((SuspendedEvent event) -> {
            DebugStackFrame frame = event.getTopStackFrame();
            DebugValue value = frame.getScope().getDeclaredValue("i");
            assertNull(value.getOriginalLanguage());
            assertEquals("L1:10", value.as(String.class));
            value = frame.getScope().getDeclaredValue("s");
            assertNull(value.getOriginalLanguage());
            assertEquals("L1:test", value.as(String.class));
            value = frame.getScope().getDeclaredValue("a");
            assertNull(value.getOriginalLanguage());
            assertEquals("null", value.as(String.class));
            value = frame.getScope().getDeclaredValue("b");
            LanguageInfo lang = value.getOriginalLanguage();
            assertNotNull(lang);
            assertEquals(ValuesLanguage1.NAME, lang.getName());
            assertEquals("{a={}, j=100}", value.as(String.class));
            event.prepareContinue();
        });
        expectDone();
        Breakpoint bp2 = Breakpoint.newBuilder(getSourceImpl(source2)).lineIs(7).build();
        session.install(bp2);
        startEval(source2);
        expectSuspended((SuspendedEvent event) -> {
            DebugStackFrame frame = event.getTopStackFrame();
            DebugValue value = frame.getScope().getDeclaredValue("j");
            assertNull(value.getOriginalLanguage());
            assertEquals("L2:20", value.as(String.class));
            value = frame.getScope().getDeclaredValue("s");
            assertNull(value.getOriginalLanguage());
            assertEquals("L2:test2", value.as(String.class));
            value = frame.getScope().getDeclaredValue("e");
            LanguageInfo lang2 = value.getOriginalLanguage();
            assertNotNull(lang2);
            assertEquals(ValuesLanguage2.NAME, lang2.getName());
            assertEquals("{d={}}", value.as(String.class));
            value = frame.getScope().getDeclaredValue("b");
            LanguageInfo lang1 = value.getOriginalLanguage();
            assertNotNull(lang1);
            assertNotEquals(lang1, lang2);
            assertEquals(ValuesLanguage1.NAME, lang1.getName());
            // info from current lang2:
            assertEquals("Object", value.as(String.class));
            assertEquals("L2:Object", value.getMetaObject().as(String.class));
            // info from original lang1:
            value = value.asInLanguage(lang1);
            assertEquals("{a={}, j=100, k=200, c={}}", value.as(String.class));
            assertEquals("L1:Map", value.getMetaObject().as(String.class));
            assertEquals("L2:Map", value.getMetaObject().asInLanguage(lang2).as(String.class));
            // Properties are always in the original language:
            value = frame.getScope().getDeclaredValue("b");
            DebugValue a = value.getProperties().iterator().next();
            assertEquals(lang1, a.getOriginalLanguage());
            Iterator<DebugValue> it = value.getProperties().iterator();
            it.next();
            it.next();
            it.next();
            DebugValue c = it.next();
            assertEquals(lang2, c.getOriginalLanguage());
            value = value.asInLanguage(lang2);
            a = value.getProperties().iterator().next();
            assertEquals(lang1, a.getOriginalLanguage());
            it = value.getProperties().iterator();
            it.next();
            it.next();
            it.next();
            c = it.next();
            assertEquals(lang2, c.getOriginalLanguage());
            value = frame.getScope().getDeclaredValue("j");
            assertNull(value.getSourceLocation());
            value = value.asInLanguage(lang1);
            assertEquals("L1:20", value.as(String.class));
            assertNull(value.getSourceLocation());
            value = frame.getScope().getDeclaredValue("d");
            assertEquals("null", value.as(String.class));
            value = value.asInLanguage(lang1);
            assertEquals("null", value.as(String.class));
            value = frame.getScope().getDeclaredValue("e");
            assertEquals(getSourceImpl(source2).createSection(4, 3, 2), value.getSourceLocation());
            value = value.asInLanguage(lang1);
            assertNull(value.getSourceLocation());
            event.prepareContinue();
        });
        expectDone();
    }
}
Also used : DebugStackFrame(com.oracle.truffle.api.debug.DebugStackFrame) Breakpoint(com.oracle.truffle.api.debug.Breakpoint) LanguageInfo(com.oracle.truffle.api.nodes.LanguageInfo) DebuggerSession(com.oracle.truffle.api.debug.DebuggerSession) DebugValue(com.oracle.truffle.api.debug.DebugValue) SuspendedEvent(com.oracle.truffle.api.debug.SuspendedEvent) Source(org.graalvm.polyglot.Source) Test(org.junit.Test)

Example 2 with DebugValue

use of com.oracle.truffle.api.debug.DebugValue in project graal by oracle.

the class DebugScopeTest method testTopScope.

// Local scopes are well tested by DebugStackFrameTest and others.
@Test
public void testTopScope() {
    final Source source = testSource("ROOT(DEFINE(function1,ROOT(\n" + "  EXPRESSION()\n" + "  )\n" + "),\n" + "DEFINE(g,ROOT(\n" + "  EXPRESSION()\n" + "  )\n" + "),\n" + "STATEMENT())\n");
    try (DebuggerSession session = startSession()) {
        session.suspendNextExecution();
        startEval(source);
        expectSuspended((SuspendedEvent event) -> {
            DebugScope topScope = session.getTopScope(event.getSourceSection().getSource().getLanguage());
            assertNotNull(topScope);
            DebugValue function1 = topScope.getDeclaredValue("function1");
            assertNotNull(function1);
            assertTrue(function1.as(String.class).contains("Function"));
            DebugValue functionType = function1.getMetaObject();
            assertEquals("Function", functionType.as(String.class));
            assertEquals(function1.getOriginalLanguage(), functionType.getOriginalLanguage());
            DebugValue g = topScope.getDeclaredValue("g");
            assertNotNull(g);
            assertTrue(g.as(String.class).contains("Function"));
        });
        expectDone();
    }
}
Also used : DebugScope(com.oracle.truffle.api.debug.DebugScope) DebuggerSession(com.oracle.truffle.api.debug.DebuggerSession) DebugValue(com.oracle.truffle.api.debug.DebugValue) SuspendedEvent(com.oracle.truffle.api.debug.SuspendedEvent) Source(org.graalvm.polyglot.Source) Test(org.junit.Test)

Example 3 with DebugValue

use of com.oracle.truffle.api.debug.DebugValue in project graal by oracle.

the class DebugStackFrameTest method testEvalAndSideEffects.

@Test
public void testEvalAndSideEffects() throws Throwable {
    final Source source = testSource("ROOT(DEFINE(a,ROOT( \n" + "  VARIABLE(a, 42), \n" + "  VARIABLE(b, 43), \n" + "  VARIABLE(c, 44), \n" + // will start stepping here
    "  STATEMENT(),\n" + "  STATEMENT())\n" + "), \n" + "VARIABLE(a, 42), VARIABLE(b, 43), VARIABLE(c, 44), \n" + "CALL(a))\n");
    try (DebuggerSession session = startSession()) {
        session.suspendNextExecution();
        startEval(source);
        expectSuspended((SuspendedEvent event) -> {
            Iterator<DebugStackFrame> stackFrames = event.getStackFrames().iterator();
            // assert changes to the current frame
            DebugStackFrame frame = stackFrames.next();
            assertDynamicFrame(frame);
            DebugValue aValue = frame.getScope().getDeclaredValue("a");
            String aStringValue = aValue.as(String.class);
            // assert changes to a parent frame
            frame = stackFrames.next();
            assertDynamicFrame(frame);
            // assign from one stack frame to another one
            frame.getScope().getDeclaredValue("a").set(aValue);
            assertEquals(aStringValue, frame.getScope().getDeclaredValue("a").as(String.class));
            event.prepareContinue();
        });
        expectDone();
    }
}
Also used : DebugStackFrame(com.oracle.truffle.api.debug.DebugStackFrame) DebuggerSession(com.oracle.truffle.api.debug.DebuggerSession) DebugValue(com.oracle.truffle.api.debug.DebugValue) SuspendedEvent(com.oracle.truffle.api.debug.SuspendedEvent) Source(org.graalvm.polyglot.Source) Test(org.junit.Test)

Example 4 with DebugValue

use of com.oracle.truffle.api.debug.DebugValue in project graal by oracle.

the class DebuggerContextsTest method testInContextEval.

@Test
public void testInContextEval() {
    try (Engine engine = Engine.create()) {
        TestContextsListener contextsListener = new TestContextsListener();
        List<ContextEvent> events = contextsListener.events;
        Debugger debugger = engine.getInstruments().get("debugger").lookup(Debugger.class);
        try (DebuggerSession session = debugger.startSession(null)) {
            session.setContextsListener(contextsListener, false);
            Context context = Context.newBuilder().engine(engine).build();
            assert context != null;
            assertEquals(1, events.size());
            assertTrue(events.get(0).created);
            DebugContext dc = events.get(0).context;
            assertNotNull(dc);
            assertNull(events.get(0).language);
            // "Global" evaluation in the brand new context
            DebugValue result = dc.evaluate("VARIABLE(a, 10)", InstrumentationTestLanguage.ID);
            assertEquals(3, events.size());
            assertTrue(events.get(1).created);
            assertEquals(InstrumentationTestLanguage.ID, events.get(1).language.getId());
            assertTrue(events.get(2).languageInitialized);
            String type = dc.runInContext(() -> {
                assertEquals("10", result.as(String.class));
                DebugValue metaObj = result.getMetaObject();
                return metaObj.as(String.class);
            });
            assertEquals("Integer", type);
            assertEquals(3, events.size());
        }
    }
}
Also used : Debugger(com.oracle.truffle.api.debug.Debugger) DebugContext(com.oracle.truffle.api.debug.DebugContext) Context(org.graalvm.polyglot.Context) DebuggerSession(com.oracle.truffle.api.debug.DebuggerSession) DebugValue(com.oracle.truffle.api.debug.DebugValue) DebugContext(com.oracle.truffle.api.debug.DebugContext) Engine(org.graalvm.polyglot.Engine) Test(org.junit.Test)

Example 5 with DebugValue

use of com.oracle.truffle.api.debug.DebugValue in project graal by oracle.

the class TruffleRuntime method evaluate.

@Override
public Params evaluate(String expression, String objectGroup, boolean includeCommandLineAPI, boolean silent, int contextId, boolean returnByValue, boolean awaitPromise) throws CommandProcessException {
    if (expression == null) {
        throw new CommandProcessException("An expression required.");
    }
    JSONObject json = new JSONObject();
    DebuggerSuspendedInfo suspendedInfo = context.getSuspendedInfo();
    if (suspendedInfo != null) {
        try {
            context.executeInSuspendThread(new SuspendThreadExecutable<Void>() {

                @Override
                public Void executeCommand() throws CommandProcessException {
                    JSONObject result;
                    DebugValue value = suspendedInfo.getSuspendedEvent().getTopStackFrame().eval(expression);
                    if (returnByValue) {
                        result = RemoteObject.createJSONResultValue(value, context.getErr());
                    } else {
                        RemoteObject ro = new RemoteObject(value, context.getErr());
                        context.getRemoteObjectsHandler().register(ro);
                        result = ro.toJSON();
                    }
                    json.put("result", result);
                    return null;
                }
            });
        } catch (NoSuspendedThreadException ex) {
            JSONObject exceptionDetails = new JSONObject();
            exceptionDetails.put("text", ex.getLocalizedMessage());
            json.put("exceptionDetails", exceptionDetails);
        } catch (GuestLanguageException ex) {
            fillExceptionDetails(json, ex);
        }
    } else {
        JSONObject exceptionDetails = new JSONObject();
        exceptionDetails.put("text", "<Not suspended>");
        json.put("exceptionDetails", exceptionDetails);
    }
    return new Params(json);
}
Also used : CommandProcessException(com.oracle.truffle.tools.chromeinspector.server.CommandProcessException) RemoteObject(com.oracle.truffle.tools.chromeinspector.types.RemoteObject) JSONObject(org.json.JSONObject) DebugValue(com.oracle.truffle.api.debug.DebugValue) Params(com.oracle.truffle.tools.chromeinspector.commands.Params) NoSuspendedThreadException(com.oracle.truffle.tools.chromeinspector.TruffleExecutionContext.NoSuspendedThreadException) GuestLanguageException(com.oracle.truffle.tools.chromeinspector.TruffleExecutionContext.GuestLanguageException)

Aggregations

DebugValue (com.oracle.truffle.api.debug.DebugValue)33 DebuggerSession (com.oracle.truffle.api.debug.DebuggerSession)15 Source (org.graalvm.polyglot.Source)15 Test (org.junit.Test)15 SuspendedEvent (com.oracle.truffle.api.debug.SuspendedEvent)14 DebugStackFrame (com.oracle.truffle.api.debug.DebugStackFrame)11 DebugScope (com.oracle.truffle.api.debug.DebugScope)9 Breakpoint (com.oracle.truffle.api.debug.Breakpoint)8 JSONObject (org.json.JSONObject)6 SourceSection (com.oracle.truffle.api.source.SourceSection)5 GuestLanguageException (com.oracle.truffle.tools.chromeinspector.TruffleExecutionContext.GuestLanguageException)5 NoSuspendedThreadException (com.oracle.truffle.tools.chromeinspector.TruffleExecutionContext.NoSuspendedThreadException)5 CommandProcessException (com.oracle.truffle.tools.chromeinspector.server.CommandProcessException)5 Params (com.oracle.truffle.tools.chromeinspector.commands.Params)4 RemoteObject (com.oracle.truffle.tools.chromeinspector.types.RemoteObject)4 Debugger (com.oracle.truffle.api.debug.Debugger)3 Context (org.graalvm.polyglot.Context)3 Value (org.graalvm.polyglot.Value)3 JSONArray (org.json.JSONArray)3 LanguageInfo (com.oracle.truffle.api.nodes.LanguageInfo)2