com.oracle.truffle.api.frame.FrameInstanceVisitor Java Examples

The following examples show how to use com.oracle.truffle.api.frame.FrameInstanceVisitor. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HashemStackTraceBuiltin.java    From mr-hashemi with Universal Permissive License v1.0 5 votes vote down vote up
@TruffleBoundary
private static String createStackTrace() {
    final StringBuilder str = new StringBuilder();

    Truffle.getRuntime().iterateFrames(new FrameInstanceVisitor<Integer>() {
        private int skip = 1; // skip stack trace builtin

        @Override
        public Integer visitFrame(FrameInstance frameInstance) {
            if (skip > 0) {
                skip--;
                return null;
            }
            CallTarget callTarget = frameInstance.getCallTarget();
            Frame frame = frameInstance.getFrame(FrameAccess.READ_ONLY);
            RootNode rn = ((RootCallTarget) callTarget).getRootNode();
            // ignore internal or interop stack frames
            if (rn.isInternal() || rn.getLanguageInfo() == null) {
                return 1;
            }
            if (str.length() > 0) {
                str.append(System.getProperty("line.separator"));
            }
            str.append("Frame: ").append(rn.toString());
            FrameDescriptor frameDescriptor = frame.getFrameDescriptor();
            for (FrameSlot s : frameDescriptor.getSlots()) {
                str.append(", ").append(s.getIdentifier()).append("=").append(frame.getValue(s));
            }
            return null;
        }
    });
    return str.toString();
}
 
Example #2
Source File: MumblerException.java    From mumbler with GNU General Public License v3.0 5 votes vote down vote up
public static Throwable fillInMumblerStackTrace(Throwable t) {
    final List<StackTraceElement> stackTrace = new ArrayList<>();
    Truffle.getRuntime().iterateFrames((FrameInstanceVisitor<Void>) frame -> {
        Node callNode = frame.getCallNode();
        if (callNode == null) {
            return null;
        }
        RootNode root = callNode.getRootNode();

        /*
         * There should be no RootNodes other than SLRootNodes on the stack. Just for the
         * case if this would change.
         */
        String methodName = "$unknownFunction";
        if (root instanceof MumblerRootNode) {
            methodName = ((MumblerRootNode) root).name;
        }

        SourceSection sourceSection = callNode.getEncapsulatingSourceSection();
        Source source = sourceSection != null ? sourceSection.getSource() : null;
        String sourceName = source != null ? source.getName() : null;
        int lineNumber;
        try {
            lineNumber = sourceSection != null ? sourceSection.getStartLine() : -1;
        } catch (UnsupportedOperationException e) {
            /*
             * SourceSection#getLineLocation() may throw an UnsupportedOperationException.
             */
            lineNumber = -1;
        }
        stackTrace.add(new StackTraceElement("mumbler", methodName, sourceName, lineNumber));
        return null;
    });
    t.setStackTrace(stackTrace.toArray(new StackTraceElement[stackTrace.size()]));
    return t;
}