8165017: Additional test coverage of the JDWP CLASSLOADER and MODULE commands
Summary: A new JDWP test
Reviewed-by: sspitsyn
--- a/hotspot/test/serviceability/jdwp/AllModulesCommandTest.java Thu Sep 15 10:19:11 2016 +0300
+++ b/hotspot/test/serviceability/jdwp/AllModulesCommandTest.java Wed Sep 14 16:20:54 2016 +0300
@@ -30,7 +30,7 @@
/**
* @test
- * @summary Tests AllModules JDWP command
+ * @summary Tests the modules-related JDWP commands
* @library /test/lib
* @modules java.base/jdk.internal.misc
* @compile AllModulesCommandTestDebuggee.java
@@ -87,8 +87,12 @@
assertReply(reply);
for (int i = 0; i < reply.getModulesCount(); ++i) {
long modId = reply.getModuleId(i);
- // For each module reported by JDWP get its name using the JDWP NAME command
- getModuleName(modId);
+ // For each module reported by JDWP get its name using the JDWP NAME command
+ // and store the reply
+ String modName = getModuleName(modId);
+ if (modName != null) { // JDWP reports unnamed modules, ignore them
+ jdwpModuleNames.add(modName);
+ }
// Assert the JDWP CANREAD and CLASSLOADER commands
assertCanRead(modId);
assertClassLoader(modId);
@@ -114,14 +118,10 @@
}
}
- private void getModuleName(long modId) throws IOException {
- // Send out the JDWP NAME command and store the reply
+ private String getModuleName(long modId) throws IOException {
JdwpModNameReply reply = new JdwpModNameCmd(modId).send(channel);
assertReply(reply);
- String modName = reply.getModuleName();
- if (modName != null) { // JDWP reports unnamed modules, ignore them
- jdwpModuleNames.add(modName);
- }
+ return reply.getModuleName();
}
private void assertReply(JdwpReply reply) {
@@ -139,11 +139,39 @@
}
private void assertClassLoader(long modId) throws IOException {
- // Simple assert for the CLASSLOADER command
+ // Verify that the module classloader id is valid
JdwpClassLoaderReply reply = new JdwpClassLoaderCmd(modId).send(channel);
assertReply(reply);
- long clId = reply.getClassLoaderId();
- assertTrue(clId >= 0, "bad classloader refId " + clId + " for module id " + modId);
+ long moduleClassLoader = reply.getClassLoaderId();
+ assertTrue(moduleClassLoader >= 0, "bad classloader refId " + moduleClassLoader + " for module id " + modId);
+
+ String clsModName = getModuleName(modId);
+ if ("java.base".equals(clsModName)) {
+ // For the java.base module, because there will be some loaded classes, we can verify
+ // that some of the loaded classes do report the java.base module as the module they belong to
+ assertGetModule(moduleClassLoader, modId);
+ }
+ }
+
+ private void assertGetModule(long moduleClassLoader, long modId) throws IOException {
+ // Get all the visible classes for the module classloader
+ JdwpVisibleClassesReply visibleClasses = new JdwpVisibleClassesCmd(moduleClassLoader).send(channel);
+ assertReply(visibleClasses);
+
+ boolean moduleFound = false;
+ for (long clsId : visibleClasses.getVisibleClasses()) {
+ // For each visible class get the module the class belongs to
+ JdwpModuleReply modReply = new JdwpModuleCmd(clsId).send(channel);
+ assertReply(modReply);
+ long clsModId = modReply.getModuleId();
+
+ // At least one of the visible classes should belong to our module
+ if (modId == clsModId) {
+ moduleFound = true;
+ break;
+ }
+ }
+ assertTrue(moduleFound, "None of the visible classes for the classloader of the module " + getModuleName(modId) + " reports the module as its own");
}
}
--- a/hotspot/test/serviceability/jdwp/JdwpCmd.java Thu Sep 15 10:19:11 2016 +0300
+++ b/hotspot/test/serviceability/jdwp/JdwpCmd.java Wed Sep 14 16:20:54 2016 +0300
@@ -70,7 +70,6 @@
}
public final T send(JdwpChannel channel) throws IOException {
- System.err.println("Sending command: " + this);
channel.write(data.array(), HEADER_LEN + getDataLength());
if (reply != null) {
reply.initFromStream(channel.getInputStream());
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/hotspot/test/serviceability/jdwp/JdwpModuleCmd.java Wed Sep 14 16:20:54 2016 +0300
@@ -0,0 +1,34 @@
+/*
+ * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/**
+ * The JDWP MODULE command
+ */
+public class JdwpModuleCmd extends JdwpCmd<JdwpModuleReply> {
+
+ public JdwpModuleCmd(long refId) {
+ super(19, 2, JdwpModuleReply.class, refLen());
+ putRefId(refId);
+ }
+
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/hotspot/test/serviceability/jdwp/JdwpModuleReply.java Wed Sep 14 16:20:54 2016 +0300
@@ -0,0 +1,42 @@
+/*
+ * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+import java.io.DataInputStream;
+import java.io.IOException;
+
+/**
+ * The reply to the JDWP MODULE command
+ */
+public class JdwpModuleReply extends JdwpReply {
+
+ private long moduleId;
+
+ protected void parseData(DataInputStream ds) throws IOException {
+ moduleId = readRefId(ds);
+ }
+
+ public long getModuleId() {
+ return moduleId;
+ }
+
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/hotspot/test/serviceability/jdwp/JdwpVisibleClassesCmd.java Wed Sep 14 16:20:54 2016 +0300
@@ -0,0 +1,34 @@
+/*
+ * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/**
+ * The JDWP VISIBLE CLASSES command
+ */
+public class JdwpVisibleClassesCmd extends JdwpCmd<JdwpVisibleClassesReply> {
+
+ public JdwpVisibleClassesCmd(long classLoaderId) {
+ super(1, 14, JdwpVisibleClassesReply.class, refLen());
+ putRefId(classLoaderId);
+ }
+
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/hotspot/test/serviceability/jdwp/JdwpVisibleClassesReply.java Wed Sep 14 16:20:54 2016 +0300
@@ -0,0 +1,49 @@
+/*
+ * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+import java.io.DataInputStream;
+import java.io.IOException;
+import java.util.Arrays;
+
+/**
+ * The reply to the JDWP VISIBLE CLASSES command
+ */
+public class JdwpVisibleClassesReply extends JdwpReply {
+
+ private long[] visibleClasses;
+
+ protected void parseData(DataInputStream ds) throws IOException {
+ int numOfClasses = ds.readInt();
+ visibleClasses = new long[numOfClasses];
+ for (int i = 0; i < numOfClasses; ++i) {
+ byte type = ds.readByte();
+ long refId = readRefId(ds);
+ visibleClasses[i] = refId;
+ }
+ }
+
+ public long[] getVisibleClasses() {
+ return Arrays.copyOf(visibleClasses, visibleClasses.length);
+ }
+
+}