public abstract class TruffleInstrument extends Object
Instrument implementation classes must use the TruffleInstrument.Registration annotation to provide
required metadata and to enable automatic discovery of the implementation.
An instrument is created if at least one instrument
option was specified or if a
service was looked up. The
Instrumenter available in the provided environment allows the instrument
instance to bind listeners for execution and
source events, as well as node factories for code injection at guest language code locations.
An instrument is disposed when the associated polyglot engine is disposed.
All active bindings created by a disposed instrument become disposed automatically. The
Instrumenter instance available in the provided environment may not be
used after disposal.
@TruffleInstrument.Registration(id = CoverageExample.ID, services =Object.class) public final class CoverageExample extendsTruffleInstrument{ public static finalStringID = "test-coverage"; private finalSet<SourceSection> coverage = newHashSet<>(); @Overrideprotected void onCreate(final Env env) {SourceSectionFilter.Builder builder =SourceSectionFilter.newBuilder();SourceSectionFilterfilter = builder.tagIs(StandardTags.ExpressionTag.class).build();Instrumenterinstrumenter = env.getInstrumenter(); instrumenter.attachExecutionEventFactory(filter, new CoverageExampleEventFactory(env)); } private class CoverageExampleEventFactory implementsExecutionEventNodeFactory{ private final Env env; CoverageExampleEventFactory(final Env env) { this.env = env; } publicExecutionEventNodecreate(finalEventContextec) { finalPrintStreamout = newPrintStream(env.out()); return newExecutionEventNode() { @CompilerDirectives.CompilationFinalprivate boolean visited; @Overridepublic void onReturnValue(VirtualFramevFrame,Objectresult) { if (!visited) {CompilerDirectives.transferToInterpreterAndInvalidate(); visited = true;SourceSectionsrc = ec.getInstrumentedSourceSection(); out.print(src.getCharIndex() + " "); coverage.add(src); } } }; } } }
| Modifier and Type | Class and Description |
|---|---|
static class |
TruffleInstrument.Env
Access to instrumentation services as well as input, output, and error streams.
|
static interface |
TruffleInstrument.Provider
Used to register a
TruffleInstrument using a ServiceLoader. |
static interface |
TruffleInstrument.Registration
Annotation that registers an
instrument implementations for
automatic discovery. |
| Modifier | Constructor and Description |
|---|---|
protected |
TruffleInstrument()
Constructor for subclasses.
|
| Modifier and Type | Method and Description |
|---|---|
protected org.graalvm.options.OptionDescriptors |
getOptionDescriptors()
Returns a set of option descriptors that are supported by this instrument.
|
protected abstract void |
onCreate(TruffleInstrument.Env env)
Invoked once on each newly allocated
TruffleInstrument instance. |
protected void |
onDispose(TruffleInstrument.Env env)
Invoked once on an instance when it becomes disabled, possibly
because the underlying engine has been closed.
|
protected void |
onFinalize(TruffleInstrument.Env env)
Invoked once on an instance just before all instruments and
languages are going to be disposed, possibly because the underlying
engine is going to be closed.
|
protected TruffleInstrument()
protected abstract void onCreate(TruffleInstrument.Env env)
TruffleInstrument instance.
The method may register additional
services - e.g. objects to be exposed via
lookup query. For example to expose a debugger
one could define an abstract debugger controller:
public abstract class DebuggerController {
DebuggerController() {
}
public abstract void installBreakpoint(int i, Callback callback);
public abstract void stepInto(Callback callback);
public abstract void stepOut(Callback callback);
public abstract void stepOver(Callback callback);
public interface Callback {
void halted(DebuggerController debugger, EventContext haltedAt);
}
}
and declare it as a service associated with the instrument,
implement it, instantiate and register in own's
instrument onCreate method:
@TruffleInstrument.Registration(id = DebuggerExample.ID, services = DebuggerController.class) public final class DebuggerExample extendsTruffleInstrument{ private Controller controller; @Overrideprotected void onCreate(Env env) { assert this.controller == null; this.controller = new Controller(env.getInstrumenter()); env.registerService(controller); } private static final class Controller extends DebuggerController { private finalInstrumenterinstrumenter; privateEventBinding<?> stepping; private Callback currentStatementCallback; Controller(Instrumenterinstrumenter) { this.instrumenter = instrumenter; } } }
env - environment information for the instrumentTruffleInstrument.Env.getInstrumenter()protected void onFinalize(TruffleInstrument.Env env)
TruffleInstrument.onDispose(Env) and the instrument must remain usable after finalization. The
instrument can prepare for disposal while still having other instruments not disposed yet.env - environment information for the instrumentprotected void onDispose(TruffleInstrument.Env env)
env - environment information for the instrumentprotected org.graalvm.options.OptionDescriptors getOptionDescriptors()
environment when the instrument is
created. By default no options are available for an instrument.
Options returned by this method must specify the instrument id as
name prefix for each option. For example if the id of the
instrument is "debugger" then a valid option name would be "debugger.Enabled". The instrument
will automatically be created if one of the specified options was
provided by the engine. To construct option descriptors from a list then
OptionDescriptors.create(List) can be used.For an example of declaring the option descriptor using an annotation.