Skip to content

Commit 8267d65

Browse files
Tomas ZezulaDoug Simon
authored andcommitted
8329564: [JVMCI] TranslatedException::debugPrintStackTrace does not work in the libjvmci compiler.
Reviewed-by: dnsimon
1 parent 16576b8 commit 8267d65

File tree

5 files changed

+31
-24
lines changed

5 files changed

+31
-24
lines changed

src/hotspot/share/classfile/vmSymbols.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -764,7 +764,7 @@ class SerializeClosure;
764764
template(decodeAndThrowThrowable_name, "decodeAndThrowThrowable") \
765765
template(encodeAnnotations_name, "encodeAnnotations") \
766766
template(encodeAnnotations_signature, "([BLjava/lang/Class;Ljdk/internal/reflect/ConstantPool;Z[Ljava/lang/Class;)[B")\
767-
template(decodeAndThrowThrowable_signature, "(IJZ)V") \
767+
template(decodeAndThrowThrowable_signature, "(IJZZ)V") \
768768
template(classRedefinedCount_name, "classRedefinedCount") \
769769
template(classLoader_name, "classLoader") \
770770
template(componentType_name, "componentType") \

src/hotspot/share/jvmci/jvmciEnv.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@
3636
#include "oops/objArrayKlass.hpp"
3737
#include "oops/typeArrayOop.inline.hpp"
3838
#include "prims/jvmtiExport.hpp"
39+
#include "runtime/arguments.hpp"
3940
#include "runtime/deoptimization.hpp"
4041
#include "runtime/fieldDescriptor.inline.hpp"
4142
#include "runtime/jniHandles.inline.hpp"
4243
#include "runtime/javaCalls.hpp"
43-
#include "runtime/thread.inline.hpp"
4444
#include "runtime/os.hpp"
4545
#include "jvmci/jniAccessMark.inline.hpp"
4646
#include "jvmci/jvmciCompiler.hpp"
@@ -415,6 +415,11 @@ class ExceptionTranslation: public StackObj {
415415
// Decodes the exception in `buffer` in `_to_env` and throws it.
416416
virtual void decode(JavaThread* THREAD, DecodeFormat format, jlong buffer) = 0;
417417

418+
static bool debug_translated_exception() {
419+
const char* prop_value = Arguments::get_property("jdk.internal.vm.TranslatedException.debug");
420+
return prop_value != nullptr && strcmp("true", prop_value) == 0;
421+
}
422+
418423
public:
419424
void doit(JavaThread* THREAD) {
420425
int buffer_size = 2048;
@@ -510,7 +515,7 @@ class HotSpotToSharedLibraryExceptionTranslation : public ExceptionTranslation {
510515
JNIAccessMark jni(_to_env, THREAD);
511516
jni()->CallStaticVoidMethod(JNIJVMCI::VMSupport::clazz(),
512517
JNIJVMCI::VMSupport::decodeAndThrowThrowable_method(),
513-
format, buffer, false);
518+
format, buffer, false, debug_translated_exception());
514519
}
515520
public:
516521
HotSpotToSharedLibraryExceptionTranslation(JVMCIEnv* hotspot_env, JVMCIEnv* jni_env, const Handle& throwable) :
@@ -543,6 +548,7 @@ class SharedLibraryToHotSpotExceptionTranslation : public ExceptionTranslation {
543548
jargs.push_int(format);
544549
jargs.push_long(buffer);
545550
jargs.push_int(true);
551+
jargs.push_int(debug_translated_exception());
546552
JavaValue result(T_VOID);
547553
JavaCalls::call_static(&result,
548554
vmSupport,

src/java.base/share/classes/jdk/internal/vm/TranslatedException.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -122,26 +122,26 @@ public String toString() {
122122
* Prints a stack trace for {@code throwable} if the system property
123123
* {@code "jdk.internal.vm.TranslatedException.debug"} is true.
124124
*/
125-
private static void debugPrintStackTrace(Throwable throwable) {
126-
if (Boolean.getBoolean("jdk.internal.vm.TranslatedException.debug")) {
125+
private static void debugPrintStackTrace(Throwable throwable, boolean debug) {
126+
if (debug) {
127127
System.err.print("DEBUG: ");
128128
throwable.printStackTrace();
129129
}
130130
}
131131

132-
private static Throwable initCause(Throwable throwable, Throwable cause) {
132+
private static Throwable initCause(Throwable throwable, Throwable cause, boolean debug) {
133133
if (cause != null) {
134134
try {
135135
throwable.initCause(cause);
136136
} catch (IllegalStateException e) {
137137
// Cause could not be set or overwritten.
138-
debugPrintStackTrace(e);
138+
debugPrintStackTrace(e, debug);
139139
}
140140
}
141141
return throwable;
142142
}
143143

144-
private static Throwable create(String className, String message, Throwable cause) {
144+
private static Throwable create(String className, String message, Throwable cause, boolean debug) {
145145
// Try create with reflection first.
146146
try {
147147
Class<?> cls = Class.forName(className);
@@ -157,13 +157,13 @@ private static Throwable create(String className, String message, Throwable caus
157157
}
158158
if (message == null) {
159159
Constructor<?> cons = cls.getConstructor();
160-
return initCause((Throwable) cons.newInstance(), cause);
160+
return initCause((Throwable) cons.newInstance(), cause, debug);
161161
}
162162
Constructor<?> cons = cls.getDeclaredConstructor(String.class);
163-
return initCause((Throwable) cons.newInstance(message), cause);
163+
return initCause((Throwable) cons.newInstance(message), cause, debug);
164164
} catch (Throwable translationFailure) {
165-
debugPrintStackTrace(translationFailure);
166-
return initCause(new TranslatedException(message, className), cause);
165+
debugPrintStackTrace(translationFailure, debug);
166+
return initCause(new TranslatedException(message, className), cause, debug);
167167
}
168168
}
169169

@@ -253,7 +253,7 @@ private static StackTraceElement[] getMyStackTrace() {
253253
* @param encodedThrowable an encoded exception in the format specified by
254254
* {@link #encodeThrowable}
255255
*/
256-
static Throwable decodeThrowable(byte[] encodedThrowable) {
256+
static Throwable decodeThrowable(byte[] encodedThrowable, boolean debug) {
257257
ByteArrayInputStream bais = new ByteArrayInputStream(encodedThrowable);
258258
try (DataInputStream dis = new DataInputStream(new GZIPInputStream(bais))) {
259259
Throwable cause = null;
@@ -262,7 +262,7 @@ static Throwable decodeThrowable(byte[] encodedThrowable) {
262262
while (dis.available() != 0) {
263263
String exceptionClassName = dis.readUTF();
264264
String exceptionMessage = emptyAsNull(dis.readUTF());
265-
throwable = create(exceptionClassName, exceptionMessage, cause);
265+
throwable = create(exceptionClassName, exceptionMessage, cause, debug);
266266
int stackTraceDepth = dis.readInt();
267267
StackTraceElement[] stackTrace = new StackTraceElement[stackTraceDepth + myStack.length];
268268
int stackTraceIndex = 0;
@@ -310,7 +310,7 @@ static Throwable decodeThrowable(byte[] encodedThrowable) {
310310
}
311311
return throwable;
312312
} catch (Throwable translationFailure) {
313-
debugPrintStackTrace(translationFailure);
313+
debugPrintStackTrace(translationFailure, debug);
314314
return new TranslatedException("Error decoding exception: " + encodedThrowable,
315315
translationFailure.getClass().getName());
316316
}

src/java.base/share/classes/jdk/internal/vm/VMSupport.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,9 @@ public static byte[] serializeAgentPropertiesToByteArray() throws IOException {
125125
* </pre>
126126
* @param buffer encoded info about the exception to throw (depends on {@code format})
127127
* @param inJVMHeap [@code true} if executing in the JVM heap, {@code false} otherwise
128+
* @param debug specifies whether debug stack traces should be enabled in case of translation failure
128129
*/
129-
public static void decodeAndThrowThrowable(int format, long buffer, boolean inJVMHeap) throws Throwable {
130+
public static void decodeAndThrowThrowable(int format, long buffer, boolean inJVMHeap, boolean debug) throws Throwable {
130131
if (format != 0) {
131132
String context = String.format("while encoding an exception to translate it %s the JVM heap",
132133
inJVMHeap ? "to" : "from");
@@ -142,7 +143,7 @@ public static void decodeAndThrowThrowable(int format, long buffer, boolean inJV
142143
}
143144
throw new InternalError("unexpected problem occurred " + context);
144145
}
145-
throw TranslatedException.decodeThrowable(bufferToBytes(buffer));
146+
throw TranslatedException.decodeThrowable(bufferToBytes(buffer), debug);
146147
}
147148

148149
private static byte[] bufferToBytes(long buffer) {

test/jdk/jdk/internal/vm/TestTranslatedException.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public void encodeDecodeTest() throws Exception {
6060
encodeDecode(throwable);
6161

6262
try {
63-
VMSupport.decodeAndThrowThrowable(0, 0L, true);
63+
VMSupport.decodeAndThrowThrowable(0, 0L, true, false);
6464
throw new AssertionError("expected decodeAndThrowThrowable to throw an exception");
6565
} catch (NullPointerException decoded) {
6666
// Expected
@@ -69,7 +69,7 @@ public void encodeDecodeTest() throws Exception {
6969
}
7070

7171
try {
72-
VMSupport.decodeAndThrowThrowable(1, 0L, true);
72+
VMSupport.decodeAndThrowThrowable(1, 0L, true, false);
7373
throw new AssertionError("expected decodeAndThrowThrowable to throw an exception");
7474
} catch (InternalError decoded) {
7575
if (!decoded.getMessage().startsWith("native buffer could not be allocated")) {
@@ -80,7 +80,7 @@ public void encodeDecodeTest() throws Exception {
8080
}
8181

8282
try {
83-
VMSupport.decodeAndThrowThrowable(2, 0L, true);
83+
VMSupport.decodeAndThrowThrowable(2, 0L, true, false);
8484
throw new AssertionError("expected decodeAndThrowThrowable to throw an exception");
8585
} catch (OutOfMemoryError decoded) {
8686
// Expected
@@ -89,7 +89,7 @@ public void encodeDecodeTest() throws Exception {
8989
}
9090

9191
try {
92-
VMSupport.decodeAndThrowThrowable(3, 0L, true);
92+
VMSupport.decodeAndThrowThrowable(3, 0L, true, false);
9393
throw new AssertionError("expected decodeAndThrowThrowable to throw an exception");
9494
} catch (InternalError decoded) {
9595
// Expected
@@ -98,7 +98,7 @@ public void encodeDecodeTest() throws Exception {
9898
}
9999

100100
try {
101-
VMSupport.decodeAndThrowThrowable(4, 0L, true);
101+
VMSupport.decodeAndThrowThrowable(4, 0L, true, false);
102102
throw new AssertionError("expected decodeAndThrowThrowable to throw an exception");
103103
} catch (InternalError decoded) {
104104
// Expected
@@ -112,7 +112,7 @@ public void encodeDecodeTest() throws Exception {
112112
try {
113113
unsafe.putInt(buffer, problem.length);
114114
unsafe.copyMemory(problem, Unsafe.ARRAY_BYTE_BASE_OFFSET, null, buffer + 4, problem.length);
115-
VMSupport.decodeAndThrowThrowable(3, buffer, true);
115+
VMSupport.decodeAndThrowThrowable(3, buffer, true, false);
116116
throw new AssertionError("expected decodeAndThrowThrowable to throw an exception");
117117
} catch (InternalError decoded) {
118118
String msg = decoded.getMessage();
@@ -139,7 +139,7 @@ private void encodeDecode(Throwable throwable) throws Exception {
139139
bufferSize = -res;
140140
} else {
141141
try {
142-
VMSupport.decodeAndThrowThrowable(format, buffer, true);
142+
VMSupport.decodeAndThrowThrowable(format, buffer, true, false);
143143
throw new AssertionError("expected decodeAndThrowThrowable to throw an exception");
144144
} catch (Throwable decoded) {
145145
assertThrowableEquals(throwable, decoded);

0 commit comments

Comments
 (0)