8329564: [JVMCI] TranslatedException::debugPrintStackTrace does not work in the libjvmci compiler.

Reviewed-by: dnsimon
This commit is contained in:
Tomas Zezula 2024-04-03 20:10:50 +00:00 committed by Doug Simon
parent 16576b87b7
commit 8267d6565d
5 changed files with 31 additions and 24 deletions

View file

@ -122,26 +122,26 @@ final class TranslatedException extends Exception {
* Prints a stack trace for {@code throwable} if the system property
* {@code "jdk.internal.vm.TranslatedException.debug"} is true.
*/
private static void debugPrintStackTrace(Throwable throwable) {
if (Boolean.getBoolean("jdk.internal.vm.TranslatedException.debug")) {
private static void debugPrintStackTrace(Throwable throwable, boolean debug) {
if (debug) {
System.err.print("DEBUG: ");
throwable.printStackTrace();
}
}
private static Throwable initCause(Throwable throwable, Throwable cause) {
private static Throwable initCause(Throwable throwable, Throwable cause, boolean debug) {
if (cause != null) {
try {
throwable.initCause(cause);
} catch (IllegalStateException e) {
// Cause could not be set or overwritten.
debugPrintStackTrace(e);
debugPrintStackTrace(e, debug);
}
}
return throwable;
}
private static Throwable create(String className, String message, Throwable cause) {
private static Throwable create(String className, String message, Throwable cause, boolean debug) {
// Try create with reflection first.
try {
Class<?> cls = Class.forName(className);
@ -157,13 +157,13 @@ final class TranslatedException extends Exception {
}
if (message == null) {
Constructor<?> cons = cls.getConstructor();
return initCause((Throwable) cons.newInstance(), cause);
return initCause((Throwable) cons.newInstance(), cause, debug);
}
Constructor<?> cons = cls.getDeclaredConstructor(String.class);
return initCause((Throwable) cons.newInstance(message), cause);
return initCause((Throwable) cons.newInstance(message), cause, debug);
} catch (Throwable translationFailure) {
debugPrintStackTrace(translationFailure);
return initCause(new TranslatedException(message, className), cause);
debugPrintStackTrace(translationFailure, debug);
return initCause(new TranslatedException(message, className), cause, debug);
}
}
@ -253,7 +253,7 @@ final class TranslatedException extends Exception {
* @param encodedThrowable an encoded exception in the format specified by
* {@link #encodeThrowable}
*/
static Throwable decodeThrowable(byte[] encodedThrowable) {
static Throwable decodeThrowable(byte[] encodedThrowable, boolean debug) {
ByteArrayInputStream bais = new ByteArrayInputStream(encodedThrowable);
try (DataInputStream dis = new DataInputStream(new GZIPInputStream(bais))) {
Throwable cause = null;
@ -262,7 +262,7 @@ final class TranslatedException extends Exception {
while (dis.available() != 0) {
String exceptionClassName = dis.readUTF();
String exceptionMessage = emptyAsNull(dis.readUTF());
throwable = create(exceptionClassName, exceptionMessage, cause);
throwable = create(exceptionClassName, exceptionMessage, cause, debug);
int stackTraceDepth = dis.readInt();
StackTraceElement[] stackTrace = new StackTraceElement[stackTraceDepth + myStack.length];
int stackTraceIndex = 0;
@ -310,7 +310,7 @@ final class TranslatedException extends Exception {
}
return throwable;
} catch (Throwable translationFailure) {
debugPrintStackTrace(translationFailure);
debugPrintStackTrace(translationFailure, debug);
return new TranslatedException("Error decoding exception: " + encodedThrowable,
translationFailure.getClass().getName());
}

View file

@ -125,8 +125,9 @@ public class VMSupport {
* </pre>
* @param buffer encoded info about the exception to throw (depends on {@code format})
* @param inJVMHeap [@code true} if executing in the JVM heap, {@code false} otherwise
* @param debug specifies whether debug stack traces should be enabled in case of translation failure
*/
public static void decodeAndThrowThrowable(int format, long buffer, boolean inJVMHeap) throws Throwable {
public static void decodeAndThrowThrowable(int format, long buffer, boolean inJVMHeap, boolean debug) throws Throwable {
if (format != 0) {
String context = String.format("while encoding an exception to translate it %s the JVM heap",
inJVMHeap ? "to" : "from");
@ -142,7 +143,7 @@ public class VMSupport {
}
throw new InternalError("unexpected problem occurred " + context);
}
throw TranslatedException.decodeThrowable(bufferToBytes(buffer));
throw TranslatedException.decodeThrowable(bufferToBytes(buffer), debug);
}
private static byte[] bufferToBytes(long buffer) {