com.oracle.truffle.tools.agentscript.impl.InsightInstrument Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of insight Show documentation
Show all versions of insight Show documentation
The Ultimate Insights Gathering Platform
The newest version!
/*
* Copyright (c) 2019, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.oracle.truffle.tools.agentscript.impl;
import java.io.IOException;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.BitSet;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Supplier;
import org.graalvm.options.OptionCategory;
import org.graalvm.options.OptionDescriptors;
import org.graalvm.options.OptionKey;
import org.graalvm.options.OptionStability;
import org.graalvm.tools.insight.Insight;
import com.oracle.truffle.api.Assumption;
import com.oracle.truffle.api.CompilerAsserts;
import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.ContextLocal;
import com.oracle.truffle.api.InstrumentInfo;
import com.oracle.truffle.api.Option;
import com.oracle.truffle.api.Truffle;
import com.oracle.truffle.api.TruffleContext;
import com.oracle.truffle.api.TruffleFile;
import com.oracle.truffle.api.TruffleOptions;
import com.oracle.truffle.api.instrumentation.EventBinding;
import com.oracle.truffle.api.instrumentation.TruffleInstrument;
import com.oracle.truffle.api.nodes.LanguageInfo;
import com.oracle.truffle.api.source.Source;
import java.util.ArrayList;
// @formatter:off
@TruffleInstrument.Registration(
id = Insight.ID,
name = InsightInstrument.NAME,
version = Insight.VERSION,
services = { Function.class },
website = "https://www.graalvm.org/tools/graalvm-insight/"
)
// @formatter:on
public class InsightInstrument extends TruffleInstrument {
static final String NAME = "Insight";
@Option(stability = OptionStability.STABLE, name = "", help = "Use provided file as an insight script (default: no script).", usageSyntax = "", category = OptionCategory.USER) //
static final OptionKey SCRIPT = new OptionKey<>("");
final IgnoreSources ignoreSources = new IgnoreSources();
private final ContextLocal perContextData;
private Env env;
/** @GuardedBy("keys" */
private final BitSet keys = new BitSet();
/** @GuardedBy("keys" */
@CompilerDirectives.CompilationFinal private Assumption keysUnchanged;
public InsightInstrument() {
this.perContextData = createContextLocal((context) -> {
return new InsightPerContext(this);
});
this.keysUnchanged = Truffle.getRuntime().createAssumption();
}
@Override
protected OptionDescriptors getOptionDescriptors() {
return new InsightInstrumentOptionDescriptors();
}
@Override
protected void onCreate(Env tmp) {
this.env = tmp;
final Function, ?> registerScripts = registerScriptsAPI(this);
env.registerService(registerScripts);
final String path = env.getOptions().get(option());
if (path != null && path.length() > 0) {
registerAgentScript(() -> {
try {
TruffleFile file = env.getTruffleFile(null, path);
if (file == null || !file.exists()) {
throw InsightException.notFound(file);
}
String mimeType = file.detectMimeType();
String lang = null;
for (Map.Entry e : env.getLanguages().entrySet()) {
if (mimeType != null && e.getValue().getMimeTypes().contains(mimeType)) {
lang = e.getKey();
break;
}
}
if (lang == null) {
throw InsightException.notRecognized(file);
}
return Source.newBuilder(lang, file).uri(file.toUri()).name(file.getName()).build();
} catch (IOException ex) {
throw InsightException.raise(ex);
}
});
}
}
OptionKey option() {
return SCRIPT;
}
final Env env() {
return env;
}
final AutoCloseable registerAgentScript(final Supplier