Elide GraalVM25.6.0-elide.4
All types
Versions and modules

DebuggerTester

Javaclassin com.oracle.truffle.tcksince 0.16

public final class DebuggerTester implements AutoCloseable
Inheritance
  1. Object
  2. DebuggerTester
Implements

Test utility class that makes it easier to test and debug debugger functionality for guest languages. Testing suspended callbacks can be cumbersome when having to assert multiple sequential events. The debugger tester allows to test suspended callbacks using sequential code that allows to assert multiple events. It does so by running the engine on a separate thread and it uses internal APIs to allow access to the SuspendedEvent from another Thread. Do not use this class for anything else than testing.

The debugger tester can print debug traces to standard output with -Dtruffle.debug.trace=true. Example usage:

public void testDebugging() {
    try (DebuggerTester tester = new DebuggerTester()) {
        // use your guest language source here
        Source source = null;
        try (DebuggerSession session = tester.startSession()) {
            session.suspendNextExecution();
            tester.startEval(source);

            tester.expectSuspended(new SuspendedCallback() {
                @Override
                public void onSuspend(SuspendedEvent event) {
                    // assert suspended event is proper here
                    event.prepareStepInto(1);

                }
            });
            tester.expectSuspended(new SuspendedCallback() {
                @Override
                public void onSuspend(SuspendedEvent event) {
                    // assert another suspended event is proper here
                    event.prepareContinue();
                }
            });

            // expect no more suspended events
            tester.expectDone();
        }
    }
}

Constructors3

DebuggerTester()

DebuggerTester

since 0.16
public DebuggerTester()

Constructs a new debugger tester instance. Boots up a new context on Thread in the background. The tester instance needs to be closed after use. Throws an AssertionError if the engine initialization fails.

DebuggerTester(Context.Builder)

DebuggerTester

since 0.31
public DebuggerTester(Context.Builder contextBuilder)

Constructs a new debugger tester instance with a pre-set context builder.

Parameters

contextBuilderContext.Builder
a pre-set context builder. Only out and err streams are set on this builder prior the Context instance creation.
DebuggerTester(Engine, Context.Builder)

DebuggerTester

since 25.0
public DebuggerTester(Engine engine, Context.Builder contextBuilder)

Constructs a new debugger tester instance with a pre-set context builder and engine.

Parameters

engineEngine
engine to use for the context builder.
contextBuilderContext.Builder
a pre-set context builder. Only out and err streams are set on this builder prior the Context instance creation.

Methods22

String getErr()

getErr

since 0.16
public String getErr()

Returns the error output of the underlying context.

Returns

String
No description.
String getOut()

getOut

since 0.16
public String getOut()

Returns the standard output of the underlying context.

Returns

String
No description.
Debugger getDebugger()

getDebugger

since 0.27
public Debugger getDebugger()

Get the Debugger instance associated with the current engine.

Returns

Debugger
No description.
DebuggerSession startSession()

startSession

since 0.16
public DebuggerSession startSession()

Starts a new debugger session in the context's engine. The debugger session allows to suspend the execution and to install breakpoints. If multiple sessions are created for one evaluation then all suspended events are delegated to this debugger tester instance.

Returns

DebuggerSession
a new debugger session
DebuggerSession startSession(SourceElement...)

startSession

since 0.33
public DebuggerSession startSession(SourceElement... sourceElements)

Starts a new debugger session in the context's engine. The debugger session allows to suspend the execution on the provided source elements and to install breakpoints. If multiple sessions are created for one evaluation then all suspended events are delegated to this debugger tester instance.

Parameters

sourceElementsSourceElement...
a list of source elements

Returns

DebuggerSession
a new debugger session
void startEval(Source)

startEval

since 0.27
public void startEval(Source s)

Starts a new evaluation on the background thread. Only one evaluation can be active at a time. Please ensure that expectDone() completed successfully before starting a new evaluation. When no source is available please refer to DebuggerTester.startExecute(Function). Throws an IllegalStateException if another evaluation is still executing or the tester is already closed.

Parameters

sSource
No description.
void startExecute(Function<Context, Value>)

startExecute

since 20.0
public void startExecute(Function<Context, Value> script)

Starts a new script evaluation on the background thread. Only one evaluation can be active at a time. Please ensure that expectDone() completed successfully before starting a new evaluation. If a Source is available please refer to DebuggerTester.startEval(Source) . Throws an IllegalStateException if another evaluation is still executing or the tester is already closed.

Parameters

scriptFunction<Context, Value>
No description.
void expectSuspended(SuspendedCallback)

expectSuspended

since 0.16
public void expectSuspended(SuspendedCallback callback)

Expects a suspended event and returns it for potential assertions. If the execution completed or was killed instead then an assertion error is thrown. The returned suspended event is only valid until on of expectKilled(), expectSuspended(SuspendedCallback) or expectDone() is called again. Throws an IllegalStateException if the tester is already closed.

Parameters

callbackSuspendedCallback
handler to be called when the execution is suspended
void expectSuspended(SuspendedCallback, Function<String, Boolean>)

expectSuspended

since 24.1
public void expectSuspended(SuspendedCallback callback, Function<String, Boolean> errorVerifier)

Expects a suspended event and returns it for potential assertions, allows to verify errors printed to the error output.

Parameters

callbackSuspendedCallback
handler to be called when the execution is suspended
errorVerifierFunction<String, Boolean>
handler, which returns true when the error is expected
Throwable expectThrowable()

expectThrowable

since 0.16
public Throwable expectThrowable()

Expects the current evaluation to be completed with an error and not be killed or to produce further suspended events. It returns a string representation of the result value to be asserted. If the evaluation caused any errors they are thrown as AssertionError. Throws an IllegalStateException if the tester is already closed.

Returns

Throwable
No description.
String expectDone()

expectDone

since 0.16
public String expectDone()

Expects the current evaluation to be completed successfully and not be killed or to produce further suspended events. It returns a string representation of the result value to be asserted. If the evaluation caused any errors they are thrown as AssertionError. Throws an IllegalStateException if the tester is already closed.

Returns

String
No description.
void expectKilled()

expectKilled

since 0.16
public void expectKilled()

Expects the current evaluation to be killed and not be completed or to produce further suspended events. Throws an IllegalStateException if the tester is already closed. If the evaluation caused any errors besides the kill exception then they are thrown as AssertionError.

Thread getEvalThread()

getEvalThread

since 0.16
public Thread getEvalThread()

Returns the thread that the execution started with startEval(Source) is running on.

Returns

Thread
the thread instance
void closeEngine()

closeEngine

since 0.30
public void closeEngine()

Close the engine. It's not possible to evaluate code after engine is closed, use it when the engine needs to be closed before the debugger session.

void close()

close

since 0.16
public void close()

Closes the current debugger tester session and all its associated resources like the background thread. The debugger tester becomes unusable after closing.

Overrides close().

void assertLineBreakpointsResolution(String, String, String)

assertLineBreakpointsResolution

since 0.33
public void assertLineBreakpointsResolution(
    String sourceWithMarks,
    String resolvedMarkName,
    String language
)

Utility method that checks proper resolution of line breakpoints. Breakpoints are submitted to every line of the source and their resolution location is checked.

The source need to contain resolution marks in the form of "R<line number>_" where <line number> is line number of the original breakpoint position and R is the resolved mark name. These marks need to be placed at the proper breakpoint resolution line/column position, for a breakpoint submitted to every line. When several submitted breakpoints resolve to the same position, an interval can be specified in the form of "R<line start number>-<line end number>_", where both start and end line numbers are inclusive. The marks are stripped off before execution.

The guest language code with marks may look like:

// test
function test(n) {
  if (R1-3_n <= 1) {
    return R4_2 * n;
  }
  return R5-7_n - 1;
}

Parameters

sourceWithMarksString
a source text, which contains the resolution marks
resolvedMarkNameString
the mark name. It is used in a regular expression, therefore the mark must not have a special meaning in a regular expression.
languageString
the source language
void assertLineBreakpointsResolution(String, DebuggerTester.PositionPredicate, String, String)No description.

assertLineBreakpointsResolution

since 19.3.0
public void assertLineBreakpointsResolution(
    String sourceWithMarks,
    DebuggerTester.PositionPredicate positionPredicate,
    String resolvedMarkName,
    String language
)

No description.

Parameters

sourceWithMarksString
No description.
positionPredicateDebuggerTester.PositionPredicate
null to test line breakpoints on all lines, or a predicate that limits the testable lines.
resolvedMarkNameString
No description.
languageString
No description.
void assertColumnBreakpointsResolution(String, String, String, String)

assertColumnBreakpointsResolution

since 0.33
public void assertColumnBreakpointsResolution(
    String sourceWithMarks,
    String breakpointMarkName,
    String resolvedMarkName,
    String language
)

Utility method that checks proper resolution of column breakpoints. Breakpoints are submitted to marked positions and their resolution location is checked.

The source need to contain both breakpoint submission marks in the form of "B<number>_" and breakpoint resolution marks in the form of "R<number>_" where <number> is an identification of the breakpoint. These marks need to be placed at the proper breakpoint submission/resolution line/column position. When several submitted breakpoints resolve to the same position, an interval can be specified in the form of "R<start number>-<end number>_", where both start and end line numbers are inclusive. The marks are stripped off before execution.

The guest language code with marks may look like:

// B1_test
function B2_test(n) {B3_
  if (R1-4_n <= B4_1) {B5_
    return R5_2 * n;
  }
  return R6_n - 1;
B6_}

Parameters

sourceWithMarksString
a source text, which contains the resolution marks
breakpointMarkNameString
the breakpoint submission mark name. It is used in a regular expression, therefore the mark must not have a special meaning in a regular expression.
resolvedMarkNameString
the resolved mark name. It is used in a regular expression, therefore the mark must not have a special meaning in a regular expression.
languageString
the source language
void assertColumnBreakpointsResolution(String, String, String, String, URI)No description.

assertColumnBreakpointsResolution

public void assertColumnBreakpointsResolution(
    String sourceWithMarks,
    String breakpointMarkName,
    String resolvedMarkName,
    String language,
    URI bpURI
)

No description.

Parameters

sourceWithMarksString
No description.
breakpointMarkNameString
No description.
resolvedMarkNameString
No description.
languageString
No description.
bpURIURI
No description.
void assertBreakpointsBreakEverywhere(Source)

assertBreakpointsBreakEverywhere

since 0.33
public void assertBreakpointsBreakEverywhere(Source source)

Utility method that tests if a breakpoint submitted to any location in the source code suspends the execution. A two-pass test is performed. In the first pass, line breakpoints are submitted to every line. In the second pass, breakpoints are submitted to every line and column combination, even outside the source scope. It is expected that the breakpoints resolve to a nearest suspendable location and it is checked that all breakpoints are hit.

Parameters

sourceSource
a source to evaluate with breakpoints submitted everywhere
void assertBreakpointsBreakEverywhere(Source, DebuggerTester.PositionPredicate)

assertBreakpointsBreakEverywhere

since 19.3.0
public void assertBreakpointsBreakEverywhere(
    Source source,
    DebuggerTester.PositionPredicate positionPredicate
)

Utility method that tests if a breakpoint submitted to any location permitted by the PositionPredicate in the source code suspends the execution. A two-pass test is performed. In the first pass, line breakpoints are submitted to every testable line. In the second pass, breakpoints are submitted to every testable line and column combination, even outside the source scope, if permitted by the PositionPredicate. It is expected that the breakpoints resolve to a nearest suspendable location and it is checked that all submitted breakpoints are hit.

Parameters

sourceSource
a source to evaluate with breakpoints submitted everywhere
positionPredicateDebuggerTester.PositionPredicate
null to submit breakpoints everywhere, or a predicate that limits the testable positions.
static Source getSourceImpl(Source)

getSourceImpl

staticsince 0.28
public static Source getSourceImpl(Source source)

Get Truffle Source that corresponds to the Polyglot Source. This is a bridge between the two Source implementations.

Parameters

sourceSource
No description.

Returns

Source
No description.

Nested types

Inherited members