Skip to content
Snippets Groups Projects
Commit 028e707d authored by Lukas Stadler's avatar Lukas Stadler
Browse files

simple traceback builtin implementation

parent 2998da2f
Branches
No related tags found
No related merge requests found
......@@ -470,6 +470,7 @@ public class BasePackage extends RBuiltinPackage {
add(TempFile.class, TempFileNodeGen::create);
add(ToLower.class, ToLowerNodeGen::create);
add(ToUpper.class, ToUpperNodeGen::create);
add(Traceback.class, TracebackNodeGen::create);
add(Transpose.class, TransposeNodeGen::create);
add(TrigExpFunctions.Acos.class, TrigExpFunctionsFactory.AcosNodeGen::create);
add(TrigExpFunctions.Acosh.class, TrigExpFunctionsFactory.AcoshNodeGen::create);
......
/*
* Copyright (c) 2013, 2015, 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.
*
* 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.r.nodes.builtin.base;
import static com.oracle.truffle.r.runtime.RBuiltinKind.PRIMITIVE;
import java.util.Arrays;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.r.nodes.builtin.RBuiltinNode;
import com.oracle.truffle.r.runtime.RBuiltin;
import com.oracle.truffle.r.runtime.Utils;
import com.oracle.truffle.r.runtime.data.RDataFactory;
import com.oracle.truffle.r.runtime.data.RStringVector;
@RBuiltin(name = "traceback", kind = PRIMITIVE, parameterNames = {"x"})
public abstract class Traceback extends RBuiltinNode {
@Override
protected void createCasts(CastBuilder casts) {
casts.firstIntegerWithWarning(0, 0, "x");
}
@Specialization
public RStringVector traceback(int x) {
String[] traceback = Utils.createTraceback();
for (String s : traceback) {
System.out.println(":: " + s);
}
traceback = Arrays.copyOfRange(traceback, x, traceback.length);
return RDataFactory.createStringVector(traceback, true);
}
}
......@@ -40,6 +40,7 @@ import com.oracle.truffle.r.runtime.conn.*;
import com.oracle.truffle.r.runtime.context.*;
import com.oracle.truffle.r.runtime.data.*;
import com.oracle.truffle.r.runtime.data.model.*;
import com.oracle.truffle.r.runtime.nodes.RSyntaxNode;
public final class Utils {
......@@ -315,6 +316,37 @@ public final class Utils {
return createStackTrace(true);
}
/**
* Generate a stack trace as a string.
*/
@TruffleBoundary
public static String[] createTraceback() {
ArrayList<String> strings = new ArrayList<>();
FrameInstance current = Truffle.getRuntime().getCurrentFrame();
if (current != null) {
strings.add(traceback(current.getFrame(FrameAccess.READ_ONLY, true)));
Truffle.getRuntime().iterateFrames(frameInstance -> {
strings.add(traceback(frameInstance.getFrame(FrameAccess.READ_ONLY, true)));
return null;
});
}
return strings.toArray(new String[strings.size()]);
}
private static String traceback(Frame frame) {
Frame unwrapped = RArguments.unwrap(frame);
if (RArguments.isRFrame(frame)) {
RCaller call = RArguments.getCall(unwrapped);
if (call != null) {
RRuntimeASTAccess rASTAccess = RContext.getRRuntimeASTAccess();
RLanguage caller = rASTAccess.getSyntaxCaller(call);
RSyntaxNode sn = (RSyntaxNode) caller.getRep();
return sn.getSourceSection().getCode() + " (" + sn.getSourceSection().getShortDescription() + ")";
}
}
return "<unknown>";
}
/**
* Generate a stack trace as a string.
*/
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment