Skip to content

Commit 408c3d8

Browse files
committed
Use the TruffleStackTrace when available in ExceptionTranslatingNode
* That means, even for internal Java exceptions like ArrayIndexOutOfBoundsException we get a Truffle-level stacktrace, i.e., the C-level stacktrace for Sulong.
1 parent 6598631 commit 408c3d8

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

src/main/java/org/truffleruby/language/backtrace/Backtrace.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ public Backtrace(TruffleException exception) {
5151
this.omitted = 0;
5252
this.javaThrowable = null;
5353

54+
this.activations = getActivations((Throwable) exception);
55+
}
56+
57+
public Backtrace(Throwable exception) {
58+
this.location = null;
59+
this.sourceLocation = null;
60+
this.omitted = 0;
61+
this.javaThrowable = null;
62+
5463
this.activations = getActivations(exception);
5564
}
5665

@@ -90,15 +99,15 @@ public Activation[] getActivations() {
9099
}
91100

92101
@TruffleBoundary
93-
public Activation[] getActivations(TruffleException truffleException) {
102+
public Activation[] getActivations(Throwable truffleException) {
94103
if (this.activations == null) {
95104
if (truffleException == null) {
96105
truffleException = new GetBacktraceException(location, GetBacktraceException.UNLIMITED);
97106
}
98107

99108
// The stacktrace is computed here if it was not already computed and stored in the
100109
// TruffleException with TruffleStackTraceElement.fillIn().
101-
final List<TruffleStackTraceElement> stackTrace = TruffleStackTrace.getStackTrace((Throwable) truffleException);
110+
final List<TruffleStackTraceElement> stackTrace = TruffleStackTrace.getStackTrace(truffleException);
102111

103112
final List<Activation> activations = new ArrayList<>();
104113
final RubyContext context = RubyLanguage.getCurrentContext();

src/main/java/org/truffleruby/language/methods/ExceptionTranslatingNode.java

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
1313
import com.oracle.truffle.api.TruffleException;
14+
import com.oracle.truffle.api.TruffleStackTrace;
1415
import com.oracle.truffle.api.dsl.UnsupportedSpecializationException;
1516
import com.oracle.truffle.api.frame.VirtualFrame;
1617
import com.oracle.truffle.api.nodes.ControlFlowException;
@@ -290,14 +291,14 @@ private DynamicObject translateThrowable(Throwable throwable) {
290291

291292
if (t instanceof TruffleException) {
292293
final Backtrace backtrace = new Backtrace((TruffleException) t);
293-
final BacktraceFormatter formatter = new BacktraceFormatter(getContext(), EnumSet.noneOf(FormattingFlags.class));
294-
final String formattedBacktrace = formatter.formatBacktrace(null, backtrace);
295-
builder.append(formattedBacktrace).append('\n');
294+
appendTruffleStackTrace(builder, backtrace);
296295
} else {
297-
// Print the first 10 lines of backtrace
298-
final StackTraceElement[] stackTrace = t.getStackTrace();
299-
for (int i = 0; i < Math.min(stackTrace.length, 10); i++) {
300-
stackTraceElementToRuby(builder, stackTrace[i]);
296+
if (TruffleStackTrace.getStackTrace(t) != null) {
297+
final Backtrace backtrace = new Backtrace(t);
298+
appendTruffleStackTrace(builder, backtrace);
299+
} else {
300+
// Print the first 10 lines of the Java stacktrace
301+
appendJavaStackTrace(t, builder, 10);
301302
}
302303
}
303304
}
@@ -312,8 +313,17 @@ private DynamicObject translateThrowable(Throwable throwable) {
312313
return coreExceptions().runtimeError(builder.toString(), this, throwable);
313314
}
314315

315-
private void stackTraceElementToRuby(StringBuilder builder, StackTraceElement stackTraceElement) {
316-
builder.append('\t').append("from ").append(stackTraceElement).append('\n');
316+
private void appendTruffleStackTrace(StringBuilder builder, Backtrace backtrace) {
317+
final BacktraceFormatter formatter = new BacktraceFormatter(getContext(), EnumSet.noneOf(FormattingFlags.class));
318+
final String formattedBacktrace = formatter.formatBacktrace(null, backtrace);
319+
builder.append(formattedBacktrace).append('\n');
320+
}
321+
322+
private void appendJavaStackTrace(Throwable t, StringBuilder builder, int limit) {
323+
final StackTraceElement[] stackTrace = t.getStackTrace();
324+
for (int i = 0; i < Math.min(stackTrace.length, limit); i++) {
325+
builder.append('\t').append("from ").append(stackTrace[i]).append('\n');
326+
}
317327
}
318328

319329
}

0 commit comments

Comments
 (0)