this.activeBinding = env.getInstrumenter().attachAllocationListener(AllocationEventFilter.ANY, new Listener());
/** * Handles exceptions from non-language instrumentation code that must not be allowed to alter * guest language execution semantics. Normal response is to log and continue. */ @TruffleBoundary static void exceptionEventForClientInstrument(EventBinding.Source<?> b, String eventName, Throwable t) { assert !b.isLanguageBinding(); if (t instanceof ThreadDeath) { // Terminates guest language execution immediately throw (ThreadDeath) t; } final Object currentVm = AccessorInstrumentHandler.engineAccess().getCurrentVM(); if (b.getInstrumenter() instanceof EngineInstrumenter || (currentVm != null && AccessorInstrumentHandler.engineAccess().isInstrumentExceptionsAreThrown(currentVm))) { throw sthrow(RuntimeException.class, t); } // Exception is a failure in (non-language) instrumentation code; log and continue InstrumentClientInstrumenter instrumenter = (InstrumentClientInstrumenter) b.getInstrumenter(); Class<?> instrumentClass = instrumenter.getInstrumentClass(); String message = String.format("Event %s failed for instrument class %s and listener/factory %s.", // eventName, instrumentClass.getName(), b.getElement()); Exception exception = new Exception(message, t); PrintStream stream = new PrintStream(instrumenter.getEnv().err()); exception.printStackTrace(stream); }
@Override @TruffleBoundary public void onReturnValue(AllocationEvent event) { if (gettingMetaObject.get()) { return; } ShadowStack.ThreadLocalStack stack = shadowStack.getStack(Thread.currentThread()); if (stack == null || stack.getStackIndex() == -1) { // nothing on the stack return; } if (stack.hasStackOverflowed()) { stackOverflowed = true; return; } LanguageInfo languageInfo = event.getLanguage(); String metaObjectString; gettingMetaObject.set(true); Object metaObject = env.findMetaObject(languageInfo, event.getValue()); if (metaObject != null) { metaObjectString = env.toString(languageInfo, metaObject); } else { metaObjectString = "null"; } gettingMetaObject.set(false); AllocationEventInfo info = new AllocationEventInfo(languageInfo, event.getNewSize() - event.getOldSize(), event.getOldSize() != 0, metaObjectString); handleEvent(stack, info); }
MaterializedFrame frame = findTruffleFrame(); try { Iterable<Scope> scopes = session.getDebugger().getEnv().findLocalScopes(node, frame); Iterator<Scope> it = scopes.iterator(); if (!it.hasNext()) {
Iterable<Scope> scopes = debugger.getEnv().findLocalScopes(node, frame); Iterator<Scope> it = scopes.iterator(); if (!it.hasNext()) {
if (languageInfo != null) { try { obj = env.findMetaObject(languageInfo, obj); if (obj != null) { return new HeapValue(getSession(), languageInfo, null, obj);
env.getInstrumenter().attachLoadSourceSectionListener( SourceSectionFilter.newBuilder().sourceIs(source).build(), sectionsCollector, true).dispose();
/** * Get a source location where this value is declared, if any. * * @return a source location of the object, or <code>null</code> * @throws DebugException when guest language code throws an exception * @since 0.22 */ public final SourceSection getSourceLocation() throws DebugException { if (!isReadable()) { return null; } Object obj = get(); if (obj == null) { return null; } TruffleInstrument.Env env = getDebugger().getEnv(); LanguageInfo languageInfo = resolveLanguage(); if (languageInfo != null) { try { SourceSection location = env.findSourceLocation(languageInfo, obj); return getSession().resolveSection(location); } catch (ThreadDeath td) { throw td; } catch (Throwable ex) { throw new DebugException(getSession(), ex, languageInfo, null, true, null); } } else { return null; } }
env.getInstrumenter().visitLoadedSourceSections( SourceSectionFilter.newBuilder().sourceIs(source).build(), sectionsCollector);
CPUSampler(Env env) { this.env = env; env.getInstrumenter().attachContextsListener(new ContextsListener() { @Override public void onContextCreated(TruffleContext context) { } @Override public void onLanguageContextCreated(TruffleContext context, LanguageInfo language) { } @Override public void onLanguageContextInitialized(TruffleContext context, LanguageInfo language) { if (!language.isInternal()) { nonInternalLanguageContextInitialized = true; } } @Override public void onLanguageContextFinalized(TruffleContext context, LanguageInfo language) { } @Override public void onLanguageContextDisposed(TruffleContext context, LanguageInfo language) { } @Override public void onContextClosed(TruffleContext context) { } }, true); }
@Override public <T> T as(Class<T> clazz) throws DebugException { if (!isReadable()) { throw new IllegalStateException("Value is not readable"); } try { if (clazz == String.class) { Object val = get(); LanguageInfo languageInfo = resolveLanguage(); String stringValue; if (languageInfo == null) { stringValue = val.toString(); } else { stringValue = getDebugger().getEnv().toString(languageInfo, val); } return clazz.cast(stringValue); } else if (clazz == Number.class || clazz == Boolean.class) { return convertToPrimitive(clazz); } } catch (ThreadDeath td) { throw td; } catch (Throwable ex) { throw new DebugException(getSession(), ex, resolveLanguage(), null, true, null); } throw new UnsupportedOperationException(); }
/** * Returns a language top scope. The top scopes have global validity and unlike * {@link DebugStackFrame#getScope()} have no relation to the suspended location. * * @throws DebugException when guest language code throws an exception * @since 0.30 */ public DebugScope getTopScope(String languageId) throws DebugException { LanguageInfo info = debugger.getEnv().getLanguages().get(languageId); if (info == null) { return null; } try { Iterable<Scope> scopes = debugger.getEnv().findTopScopes(languageId); Iterator<Scope> it = scopes.iterator(); if (!it.hasNext()) { return null; } return new DebugScope(it.next(), it, this, info); } catch (ThreadDeath td) { throw td; } catch (Throwable ex) { throw new DebugException(this, ex, info, null, true, null); } }
private HeapMonitor(TruffleInstrument.Env env) { this.env = env; env.getInstrumenter().attachContextsListener(new ContextsListener() { @Override public void onContextCreated(TruffleContext context) { } @Override public void onLanguageContextCreated(TruffleContext context, LanguageInfo language) { } @Override public void onLanguageContextInitialized(TruffleContext context, LanguageInfo language) { initializedLanguages.put(language, language); } @Override public void onLanguageContextFinalized(TruffleContext context, LanguageInfo language) { initializedLanguages.remove(language); } @Override public void onLanguageContextDisposed(TruffleContext context, LanguageInfo language) { } @Override public void onContextClosed(TruffleContext context) { } }, true); }
/** @since 0.22 */ @Test public void testValueWithSource() throws Exception { String id = valueWithSource(); if (id == null) { return; } PolyglotEngine.Value valueFunction = findGlobalSymbol(id); SourceSection sourceLocation; PolyglotRuntime.Instrument instr = vm().getRuntime().getInstruments().get(TckInstrument.ID); instr.setEnabled(true); try { Value value = valueFunction.execute(); TckInstrument tckInstrument = instr.lookup(TckInstrument.class); assertNotNull(tckInstrument); TruffleInstrument.Env env = tckInstrument.getEnvironment(); assertNotNull(env); sourceLocation = value.getSourceLocation(); assertNotNull(sourceLocation); List<SourceSection> lss = env.getInstrumenter().querySourceSections(SourceSectionFilter.ANY); assertTrue("Source section not among loaded sections", lss.contains(sourceLocation)); } finally { instr.setEnabled(false); } }
/** * Get the current inner-most scope. The scope might not provide valid information if the * execution path diverges from this trace element. * * @return the scope, or <code>null</code> when no language is associated with this frame * location, or when no local scope exists. * @since 1.0 */ public DebugScope getScope() { Node node = traceElement.getLocation(); if (node == null) { return null; } RootNode root = node.getRootNode(); if (root.getLanguageInfo() == null) { // no language, no scopes return null; } Frame elementFrame = traceElement.getFrame(); MaterializedFrame frame = (elementFrame != null) ? elementFrame.materialize() : null; Iterable<Scope> scopes = session.getDebugger().getEnv().findLocalScopes(node, frame); Iterator<Scope> it = scopes.iterator(); if (!it.hasNext()) { return null; } return new DebugScope(it.next(), it, session, null, frame, root); }
/** * Get a meta-object of this value, if any. The meta-object represents a description of the * value, reveals it's kind and it's features. * * @return a value representing the meta-object, or <code>null</code> * @since 0.22 */ public final DebugValue getMetaObject() { if (!isReadable()) { return null; } Object obj = get(); if (obj == null) { return null; } TruffleInstrument.Env env = getDebugger().getEnv(); LanguageInfo languageInfo = resolveLanguage(); if (languageInfo != null) { obj = env.findMetaObject(languageInfo, obj); if (obj != null) { return new HeapValue(getDebugger(), languageInfo, null, obj); } } return null; }
/** * Called to create the Instrument. * * @param env environment information for the instrument * @since 0.30 */ @Override protected void onCreate(Env env) { tracer = factory.create(env); if (env.getOptions().get(CPUTracerCLI.ENABLED)) { try { tracer.setFilter(getSourceSectionFilter(env)); } catch (IllegalArgumentException e) { new PrintStream(env.err()).println(ID + " error: " + e.getMessage()); env.getOptions().set(CPUTracerCLI.ENABLED, false); tracer.setCollecting(false); env.registerService(tracer); return; } tracer.setCollecting(true); } env.registerService(tracer); }
/** * Get a source location where this value is declared, if any. * * @return a source location of the object, or <code>null</code> * @since 0.22 */ public final SourceSection getSourceLocation() { if (!isReadable()) { return null; } Object obj = get(); if (obj == null) { return null; } TruffleInstrument.Env env = getDebugger().getEnv(); LanguageInfo languageInfo = resolveLanguage(); if (languageInfo != null) { return env.findSourceLocation(languageInfo, obj); } else { return null; } }
/** * Called to create the Instrument. * * @param env environment information for the instrument * @since 0.30 */ @Override protected void onCreate(Env env) { sampler = factory.create(env); if (env.getOptions().get(CPUSamplerCLI.ENABLED)) { sampler.setPeriod(env.getOptions().get(CPUSamplerCLI.SAMPLE_PERIOD)); sampler.setDelay(env.getOptions().get(CPUSamplerCLI.DELAY_PERIOD)); sampler.setStackLimit(env.getOptions().get(CPUSamplerCLI.STACK_LIMIT)); sampler.setFilter(getSourceSectionFilter(env)); sampler.setGatherSelfHitTimes(env.getOptions().get(GATHER_HIT_TIMES)); sampler.setMode(env.getOptions().get(CPUSamplerCLI.MODE)); sampler.setCollecting(true); } env.registerService(sampler); }
/** * Handles exceptions from non-language instrumentation code that must not be allowed to alter * guest language execution semantics. Normal response is to log and continue. */ @TruffleBoundary static void exceptionEventForClientInstrument(EventBinding.Source<?> b, String eventName, Throwable t) { assert !b.isLanguageBinding(); if (t instanceof ThreadDeath) { // Terminates guest language execution immediately throw (ThreadDeath) t; } // Exception is a failure in (non-language) instrumentation code; log and continue InstrumentClientInstrumenter instrumenter = (InstrumentClientInstrumenter) b.getInstrumenter(); Class<?> instrumentClass = instrumenter.getInstrumentClass(); String message = String.format("Event %s failed for instrument class %s and listener/factory %s.", // eventName, instrumentClass.getName(), b.getElement()); Exception exception = new Exception(message, t); PrintStream stream = new PrintStream(instrumenter.getEnv().err()); exception.printStackTrace(stream); }