Skip to content

Commit 7aee0d6

Browse files
committed
Show the Java stacktrace for internal errors by default
* This is very useful when something e.g. fails in CI. * All internal errors are bugs so the concern to spam the user is low here. * There is no overhead except when about to print the internal error. * Review and fix usages of isWithJavaStacktrace() to only print a stacktrace for internal errors when the value is 1, as documented.
1 parent 5bfd3fa commit 7aee0d6

File tree

5 files changed

+23
-19
lines changed

5 files changed

+23
-19
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/FaulthandlerModuleBuiltins.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ private static void dump(PythonLanguage language, PythonContext context, Object
136136
}
137137

138138
if (allThreads) {
139-
if (PythonOptions.isWithJavaStacktrace(language)) {
139+
if (PythonOptions.isPExceptionWithJavaStacktrace(language)) {
140140
PrintWriter err = new PrintWriter(context.getStandardErr());
141141
Thread[] ths = context.getThreads();
142142
for (Map.Entry<Thread, StackTraceElement[]> e : Thread.getAllStackTraces().entrySet()) {
@@ -165,7 +165,7 @@ protected void perform(ThreadLocalAction.Access access) {
165165
}
166166
});
167167
} else {
168-
if (PythonOptions.isWithJavaStacktrace(language)) {
168+
if (PythonOptions.isPExceptionWithJavaStacktrace(language)) {
169169
PrintWriter err = new PrintWriter(context.getStandardErr());
170170
err.println();
171171
err.println(Thread.currentThread());

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/exception/TopLevelExceptionHandler.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242

4343
import static com.oracle.graal.python.builtins.modules.io.IONodes.T_WRITE;
4444
import static com.oracle.graal.python.nodes.BuiltinNames.T_SYS;
45-
import static com.oracle.graal.python.runtime.exception.ExceptionUtils.printToStdErr;
4645
import static com.oracle.graal.python.runtime.exception.PythonErrorType.SystemExit;
4746
import static com.oracle.graal.python.util.PythonUtils.toTruffleStringUncached;
4847

@@ -218,11 +217,7 @@ private void handleJavaException(Throwable e) {
218217
boolean exitException = InteropLibrary.getUncached().isException(e) && InteropLibrary.getUncached().getExceptionType(e) == ExceptionType.EXIT;
219218
if (!exitException) {
220219
ExceptionUtils.printPythonLikeStackTrace(getContext(), e);
221-
boolean withJavaStacktrace = PythonOptions.isWithJavaStacktrace(getPythonLanguage());
222-
if (e instanceof AssertionError && !withJavaStacktrace) {
223-
printToStdErr("To get more information about the failed assertion rerun with --python.WithJavaStacktrace=3\n");
224-
}
225-
if (withJavaStacktrace) {
220+
if (PythonOptions.shouldPrintJavaStacktrace(getPythonLanguage(), e)) {
226221
e.printStackTrace();
227222
}
228223
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/PythonContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2191,7 +2191,7 @@ private void shutdownThreads() {
21912191
boolean exitException = InteropLibrary.getUncached().isException(e) && InteropLibrary.getUncached().getExceptionType(e) == ExceptionType.EXIT;
21922192
if (!exitException) {
21932193
ExceptionUtils.printPythonLikeStackTrace(e);
2194-
if (PythonOptions.isWithJavaStacktrace(getLanguage())) {
2194+
if (PythonOptions.shouldPrintJavaStacktrace(getLanguage(), e)) {
21952195
e.printStackTrace(new PrintWriter(getStandardErr()));
21962196
}
21972197
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/PythonOptions.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import java.util.Map;
4545
import java.util.Optional;
4646

47+
import com.oracle.truffle.api.exception.AbstractTruffleException;
4748
import org.graalvm.options.OptionCategory;
4849
import org.graalvm.options.OptionDescriptor;
4950
import org.graalvm.options.OptionDescriptors;
@@ -230,11 +231,12 @@ private PythonOptions() {
230231
@EngineOption @Option(category = OptionCategory.INTERNAL, help = "Eagerly initialize source sections.", usageSyntax = "true|false") //
231232
public static final OptionKey<Boolean> ForceInitializeSourceSections = new OptionKey<>(false);
232233

233-
@EngineOption @Option(category = OptionCategory.INTERNAL, help = "Print the java stacktrace. Possible modes:" +
234-
" 1 Print Java stacktrace for Java exceptions only." +
234+
@EngineOption @Option(category = OptionCategory.INTERNAL, help = "Print the Java stacktrace for exceptions. Possible modes:" +
235+
" 0 Do not print any Java stacktraces." +
236+
" 1 Print Java stacktrace for Java exceptions only (default)." +
235237
" 2 Print Java stacktrace for Python exceptions only (ATTENTION: this will have a notable performance impact)." +
236-
" 3 Combines 1 and 2.", usageSyntax = "1|2|3") //
237-
public static final OptionKey<Integer> WithJavaStacktrace = new OptionKey<>(0);
238+
" 3 Combines 1 and 2.", usageSyntax = "0|1|2|3") //
239+
public static final OptionKey<Integer> WithJavaStacktrace = new OptionKey<>(1);
238240

239241
@Option(category = OptionCategory.INTERNAL, usageSyntax = "true|false", help = "") //
240242
public static final OptionKey<Boolean> CatchGraalPythonExceptionForUnitTesting = new OptionKey<>(false);
@@ -522,12 +524,19 @@ public static int getNodeRecursionLimit() {
522524
return result;
523525
}
524526

525-
public static boolean isWithJavaStacktrace(PythonLanguage language) {
526-
return language.getEngineOption(WithJavaStacktrace) > 0;
527+
public static boolean isPExceptionWithJavaStacktrace(PythonLanguage language) {
528+
return language.getEngineOption(WithJavaStacktrace) >= 2;
527529
}
528530

529-
public static boolean isPExceptionWithJavaStacktrace(PythonLanguage language) {
530-
return language.getEngineOption(WithJavaStacktrace) > 1;
531+
public static boolean shouldPrintJavaStacktrace(PythonLanguage language, Throwable throwable) {
532+
int mode = language.getEngineOption(WithJavaStacktrace);
533+
if (mode == 1) {
534+
return !(throwable instanceof AbstractTruffleException);
535+
} else if (mode == 2) {
536+
return throwable instanceof AbstractTruffleException;
537+
} else {
538+
return mode == 3;
539+
}
531540
}
532541

533542
@TruffleBoundary

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/formatting/ErrorMessageFormatter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -133,7 +133,7 @@ public static String format(String format, Object... args) {
133133
@TruffleBoundary
134134
private static String getMessage(Throwable exception) {
135135
String message = exception.getClass().getSimpleName() + ": " + exception.getMessage();
136-
if (PythonOptions.isWithJavaStacktrace(PythonLanguage.get(null))) {
136+
if (PythonOptions.shouldPrintJavaStacktrace(PythonLanguage.get(null), exception)) {
137137
StringWriter writer = new StringWriter();
138138
try (PrintWriter pw = new PrintWriter(writer)) {
139139
exception.printStackTrace(pw);

0 commit comments

Comments
 (0)