Skip to content

Commit 2289aa1

Browse files
committed
[GR-33802] Fix logging in JDWP.
PullRequest: graal/9794
2 parents ab3a2ae + 211ea12 commit 2289aa1

File tree

4 files changed

+101
-34
lines changed

4 files changed

+101
-34
lines changed

espresso/src/com.oracle.truffle.espresso.jdwp/src/com/oracle/truffle/espresso/jdwp/impl/DebuggerConnection.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -587,8 +587,16 @@ private void processPacket(Packet packet) {
587587
}
588588
handleReply(packet, result);
589589
} catch (Throwable t) {
590-
JDWP.LOGGER.warning(() -> "[Internal error]: " + t.getClass());
591-
JDWP.LOGGER.throwing(DebuggerConnection.class.getName(), "processPacket", t);
590+
if (entered) {
591+
// we can only use the Truffle logger if we were able to enter the context
592+
JDWP.LOGGER.warning(() -> "[Internal error]");
593+
JDWP.LOGGER.throwing(DebuggerConnection.class.getName(), "processPacket", t);
594+
} else {
595+
// Checkstyle: stop allow error output
596+
System.out.println("[internal error]: " + t.getMessage());
597+
t.printStackTrace();
598+
// Checkstyle: resume allow error output
599+
}
592600
PacketStream reply = new PacketStream().replyPacket().id(packet.id);
593601
reply.errorCode(ErrorCodes.INTERNAL);
594602
handleReply(packet, new CommandResult(reply));

espresso/src/com.oracle.truffle.espresso.jdwp/src/com/oracle/truffle/espresso/jdwp/impl/JDWP.java

Lines changed: 78 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,10 +1105,28 @@ public Object call() {
11051105
new Thread(new Runnable() {
11061106
@Override
11071107
public void run() {
1108-
ThreadJob<?>.JobResult<?> result = job.getResult();
1109-
writeMethodResult(reply, context, result);
1108+
boolean entered = false;
11101109
CommandResult commandResult = new CommandResult(reply);
1111-
connection.handleReply(packet, commandResult);
1110+
try {
1111+
ThreadJob<?>.JobResult<?> result = job.getResult();
1112+
writeMethodResult(reply, context, result);
1113+
} catch (Throwable t) {
1114+
entered = controller.enterTruffleContext();
1115+
reply.errorCode(ErrorCodes.INTERNAL);
1116+
// Checkstyle: stop allow error output
1117+
if (entered) {
1118+
LOGGER.warning(() -> "Internal Espresso error: " + t);
1119+
} else {
1120+
System.err.println("Internal Espresso error: " + t.getMessage());
1121+
}
1122+
t.printStackTrace();
1123+
// Checkstyle: resume allow error output
1124+
} finally {
1125+
connection.handleReply(packet, commandResult);
1126+
if (entered) {
1127+
controller.leaveTruffleContext();
1128+
}
1129+
}
11121130
}
11131131
}).start();
11141132
} catch (Throwable t) {
@@ -1269,10 +1287,28 @@ public Object call() throws Exception {
12691287
new Thread(new Runnable() {
12701288
@Override
12711289
public void run() {
1272-
ThreadJob<?>.JobResult<?> result = job.getResult();
1273-
writeMethodResult(reply, context, result);
1290+
boolean entered = false;
12741291
CommandResult commandResult = new CommandResult(reply);
1275-
connection.handleReply(packet, commandResult);
1292+
try {
1293+
ThreadJob<?>.JobResult<?> result = job.getResult();
1294+
writeMethodResult(reply, context, result);
1295+
} catch (Throwable t) {
1296+
entered = controller.enterTruffleContext();
1297+
reply.errorCode(ErrorCodes.INTERNAL);
1298+
// Checkstyle: stop allow error output
1299+
if (entered) {
1300+
LOGGER.warning(() -> "Internal Espresso error: " + t);
1301+
} else {
1302+
System.err.println("Internal Espresso error: " + t.getMessage());
1303+
}
1304+
t.printStackTrace();
1305+
// Checkstyle: resume allow error output
1306+
} finally {
1307+
connection.handleReply(packet, commandResult);
1308+
if (entered) {
1309+
controller.leaveTruffleContext();
1310+
}
1311+
}
12761312
}
12771313
}).start();
12781314
} catch (Throwable t) {
@@ -1749,10 +1785,28 @@ public Object call() throws Exception {
17491785
new Thread(new Runnable() {
17501786
@Override
17511787
public void run() {
1752-
ThreadJob<?>.JobResult<?> result = job.getResult();
1753-
writeMethodResult(reply, context, result);
1788+
boolean entered = false;
17541789
CommandResult commandResult = new CommandResult(reply);
1755-
connection.handleReply(packet, commandResult);
1790+
try {
1791+
ThreadJob<?>.JobResult<?> result = job.getResult();
1792+
writeMethodResult(reply, context, result);
1793+
} catch (Throwable t) {
1794+
entered = controller.enterTruffleContext();
1795+
reply.errorCode(ErrorCodes.INTERNAL);
1796+
// Checkstyle: stop allow error output
1797+
if (entered) {
1798+
LOGGER.warning(() -> "Internal Espresso error: " + t);
1799+
} else {
1800+
System.err.println("Internal Espresso error: " + t.getMessage());
1801+
}
1802+
t.printStackTrace();
1803+
// Checkstyle: resume allow error output
1804+
} finally {
1805+
connection.handleReply(packet, commandResult);
1806+
if (entered) {
1807+
controller.leaveTruffleContext();
1808+
}
1809+
}
17561810
}
17571811
}).start();
17581812
} catch (Throwable t) {
@@ -2974,31 +3028,24 @@ public static void writeValue(byte tag, Object value, PacketStream reply, boolea
29743028
}
29753029

29763030
private static void writeMethodResult(PacketStream reply, JDWPContext context, ThreadJob<?>.JobResult<?> result) {
2977-
try {
2978-
if (result.getException() != null) {
2979-
LOGGER.fine(() -> "method threw exception");
2980-
reply.writeByte(TagConstants.OBJECT);
2981-
reply.writeLong(0);
2982-
reply.writeByte(TagConstants.OBJECT);
2983-
Object guestException = context.getGuestException(result.getException());
2984-
reply.writeLong(context.getIds().getIdAsLong(guestException));
2985-
} else {
2986-
Object value = context.toGuest(result.getResult());
2987-
if (value != null) {
2988-
byte tag = context.getTag(value);
2989-
writeValue(tag, value, reply, true, context);
2990-
} else { // return value is null
2991-
reply.writeByte(TagConstants.OBJECT);
2992-
reply.writeLong(0);
2993-
}
2994-
// no exception, so zero object ID
3031+
if (result.getException() != null) {
3032+
reply.writeByte(TagConstants.OBJECT);
3033+
reply.writeLong(0);
3034+
reply.writeByte(TagConstants.OBJECT);
3035+
Object guestException = context.getGuestException(result.getException());
3036+
reply.writeLong(context.getIds().getIdAsLong(guestException));
3037+
} else {
3038+
Object value = context.toGuest(result.getResult());
3039+
if (value != null) {
3040+
byte tag = context.getTag(value);
3041+
writeValue(tag, value, reply, true, context);
3042+
} else { // return value is null
29953043
reply.writeByte(TagConstants.OBJECT);
29963044
reply.writeLong(0);
29973045
}
2998-
} catch (Throwable t) {
2999-
LOGGER.warning(() -> "Internal Espresso error: " + t);
3000-
LOGGER.throwing(JDWP.class.getName(), "writeMethodResult", t);
3001-
reply.errorCode(ErrorCodes.INTERNAL);
3046+
// no exception, so zero object ID
3047+
reply.writeByte(TagConstants.OBJECT);
3048+
reply.writeLong(0);
30023049
}
30033050
}
30043051

espresso/src/com.oracle.truffle.espresso/src/com/oracle/truffle/espresso/substitutions/Target_java_lang_reflect_ProxyGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
@EspressoSubstitutions
3434
public final class Target_java_lang_reflect_ProxyGenerator {
3535

36-
@Substitution(versionFilter = VersionFilter.Java8OrEarlier.class)
36+
@Substitution(versionFilter = VersionFilter.Java9OrLater.class)
3737
abstract static class GenerateProxyClass extends SubstitutionNode {
3838

3939
abstract @JavaType(byte[].class) StaticObject execute(

espresso/src/com.oracle.truffle.espresso/src/com/oracle/truffle/espresso/substitutions/VersionFilter.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,18 @@ public boolean isValidFor(JavaVersion version) {
5353
}
5454
}
5555

56+
final class Java9OrLater implements VersionFilter {
57+
public static final Java9OrLater INSTANCE = new Java9OrLater();
58+
59+
private Java9OrLater() {
60+
}
61+
62+
@Override
63+
public boolean isValidFor(JavaVersion version) {
64+
return version.java9OrLater();
65+
}
66+
}
67+
5668
final class Java11OrEarlier implements VersionFilter {
5769
public static final Java11OrEarlier INSTANCE = new Java11OrEarlier();
5870

0 commit comments

Comments
 (0)