package com.oracle.truffle.tools.agentscript.impl;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.TruffleFile;
import com.oracle.truffle.api.TruffleStackTrace;
import com.oracle.truffle.api.exception.AbstractTruffleException;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.instrumentation.EventBinding;
import com.oracle.truffle.api.instrumentation.EventContext;
import com.oracle.truffle.api.instrumentation.ExecutionEventListener;
import com.oracle.truffle.api.instrumentation.Instrumenter;
import com.oracle.truffle.api.instrumentation.SourceSectionFilter;
import com.oracle.truffle.api.interop.ExceptionType;
import com.oracle.truffle.api.interop.InteropLibrary;
import com.oracle.truffle.api.interop.UnsupportedMessageException;
import com.oracle.truffle.api.library.ExportLibrary;
import com.oracle.truffle.api.library.ExportMessage;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.source.Source;
import java.util.Locale;
@ExportLibrary(InteropLibrary.class)
final class InsightException extends AbstractTruffleException {
static final long serialVersionUID = 1L;
private final int exitCode;
@TruffleBoundary
private InsightException(String msg, Throwable cause, int exitCode) {
super("insight: " + msg, cause, UNLIMITED_STACK_TRACE, null);
this.exitCode = exitCode;
}
@TruffleBoundary
private InsightException(Throwable cause, Node node) {
super(cause.getMessage(), node);
this.exitCode = -1;
}
@ExportMessage
ExceptionType getExceptionType() {
return exitCode < 0 ? ExceptionType.RUNTIME_ERROR : ExceptionType.EXIT;
}
@ExportMessage
int getExceptionExitStatus() throws UnsupportedMessageException {
if (exitCode < 0) {
throw UnsupportedMessageException.create();
}
return exitCode;
}
@TruffleBoundary
static InsightException raise(Exception ex) throws InsightException {
final String msg;
if (ex.getMessage() == null) {
msg = "Unexpected " + ex.getClass().getSimpleName();
} else {
msg = ex.getMessage().replace(System.lineSeparator(), ": ");
}
throw new InsightException(msg, ex, -1);
}
@TruffleBoundary
static InsightException notFound(TruffleFile file) {
throw new InsightException(file.getName() + ": No such file or directory", null, 1);
}
@TruffleBoundary
static InsightException notRecognized(TruffleFile file) {
throw new InsightException(file.getName() + ": No language to process the file. Try --polyglot", null, 1);
}
@TruffleBoundary
static InsightException unknownAttribute(String type) {
throw new InsightException("Unknown attribute " + type, null, 1);
}
@TruffleBoundary
static InsightException unknownType(Throwable originalError, String str, AgentType[] values) {
StringBuilder sb = new StringBuilder();
sb.append("Unknown event type '").append(str).append("'. Known types are:");
String sep = " ";
for (AgentType t : values) {
sb.append(sep).append("'").append(t.toString().toLowerCase(Locale.ENGLISH)).append("'");
sep = ", ";
}
throw new InsightException(sb.toString(), originalError, 1);
}
@TruffleBoundary
static void throwWhenExecuted(Instrumenter instrumenter, Source source, Exception ex) {
TruffleStackTrace.getStackTrace(ex);
SourceSectionFilter filter = SourceSectionFilter.newBuilder().sourceIs(source).build();
EventBinding<?>[] waitForSourceBeingExecuted = {null};
waitForSourceBeingExecuted[0] = instrumenter.attachExecutionEventListener(filter, new ExecutionEventListener() {
@Override
@TruffleBoundary
public void onEnter(EventContext context, VirtualFrame frame) {
waitForSourceBeingExecuted[0].dispose();
EventContextObject obj = new EventContextObject(context);
InteropLibrary interopLib = InteropLibrary.getUncached();
if (interopLib.isException(ex)) {
throw obj.rethrow((RuntimeException) ex, interopLib);
}
InsightException wrapper = new InsightException(ex, context.getInstrumentedNode());
throw obj.rethrow(wrapper, interopLib);
}
@Override
public void onReturnValue(EventContext context, VirtualFrame frame, Object result) {
}
@Override
public void onReturnExceptional(EventContext context, VirtualFrame frame, Throwable exception) {
}
});
}
}