13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 import java.io.IOException;
25 import java.util.Arrays;
26 import java.util.concurrent.CountDownLatch;
27 import java.util.Set;
28 import java.util.HashSet;
29 import static jdk.test.lib.Asserts.assertTrue;
30
31 /**
32 * @test
33 * @summary Tests the modules-related JDWP commands
34 * @library /test/lib
35 * @modules java.base/jdk.internal.misc
36 * @compile AllModulesCommandTestDebuggee.java
37 * @run main/othervm AllModulesCommandTest
38 */
39 public class AllModulesCommandTest implements DebuggeeLauncher.Listener {
40
41 private DebuggeeLauncher launcher;
42 private JdwpChannel channel;
43 private CountDownLatch jdwpLatch = new CountDownLatch(1);
44 private Set<String> jdwpModuleNames = new HashSet<>();
45 private Set<String> javaModuleNames = new HashSet<>();
46
47 public static void main(String[] args) throws Throwable {
48 new AllModulesCommandTest().doTest();
49 }
50
51 private void doTest() throws Throwable {
52 launcher = new DebuggeeLauncher(this);
53 launcher.launchDebuggee();
71 }
72
73 @Override
74 public void onDebuggeeError(String message) {
75 System.err.println("Debuggee error: '" + message + "'");
76 System.exit(1);
77 }
78
79 private void doJdwp() throws Exception {
80 try {
81 // Establish JDWP socket connection
82 channel = new JdwpChannel();
83 channel.connect();
84 // Send out ALLMODULES JDWP command
85 // and verify the reply
86 JdwpAllModulesReply reply = new JdwpAllModulesCmd().send(channel);
87 assertReply(reply);
88 for (int i = 0; i < reply.getModulesCount(); ++i) {
89 long modId = reply.getModuleId(i);
90 // For each module reported by JDWP get its name using the JDWP NAME command
91 // and store the reply
92 String modName = getModuleName(modId);
93 if (modName != null) { // JDWP reports unnamed modules, ignore them
94 jdwpModuleNames.add(modName);
95 }
96 // Assert the JDWP CANREAD and CLASSLOADER commands
97 assertCanRead(modId);
98 assertClassLoader(modId);
99 }
100
101 System.out.println("Module names reported by JDWP: " + Arrays.toString(jdwpModuleNames.toArray()));
102 System.out.println("Module names reported by Java: " + Arrays.toString(javaModuleNames.toArray()));
103
104 // Modules reported by the JDWP should be the same as reported by the Java API
105 if (!jdwpModuleNames.equals(javaModuleNames)) {
106 throw new RuntimeException("Modules info reported by Java API differs from that reported by JDWP.");
107 } else {
108 System.out.println("Test passed!");
109 }
110
111 } finally {
112 launcher.terminateDebuggee();
113 try {
114 new JdwpExitCmd(0).send(channel);
115 channel.disconnect();
116 } catch (Exception x) {
117 }
118 }
119 }
120
121 private String getModuleName(long modId) throws IOException {
122 JdwpModNameReply reply = new JdwpModNameCmd(modId).send(channel);
123 assertReply(reply);
124 return reply.getModuleName();
125 }
126
127 private void assertReply(JdwpReply reply) {
128 // Simple assert for any JDWP reply
129 if (reply.getErrorCode() != 0) {
130 throw new RuntimeException("Unexpected reply error code " + reply.getErrorCode() + " for reply " + reply);
131 }
132 }
133
134 private void assertCanRead(long modId) throws IOException {
135 // Simple assert for the CANREAD command
136 JdwpCanReadReply reply = new JdwpCanReadCmd(modId, modId).send(channel);
137 assertReply(reply);
138 assertTrue(reply.canRead(), "canRead() reports false for reading from the same module");
139 }
140
141 private void assertClassLoader(long modId) throws IOException {
142 // Verify that the module classloader id is valid
143 JdwpClassLoaderReply reply = new JdwpClassLoaderCmd(modId).send(channel);
144 assertReply(reply);
145 long moduleClassLoader = reply.getClassLoaderId();
146 assertTrue(moduleClassLoader >= 0, "bad classloader refId " + moduleClassLoader + " for module id " + modId);
147
148 String clsModName = getModuleName(modId);
149 if ("java.base".equals(clsModName)) {
150 // For the java.base module, because there will be some loaded classes, we can verify
151 // that some of the loaded classes do report the java.base module as the module they belong to
152 assertGetModule(moduleClassLoader, modId);
153 }
154 }
155
156 private void assertGetModule(long moduleClassLoader, long modId) throws IOException {
157 // Get all the visible classes for the module classloader
158 JdwpVisibleClassesReply visibleClasses = new JdwpVisibleClassesCmd(moduleClassLoader).send(channel);
159 assertReply(visibleClasses);
160
161 boolean moduleFound = false;
162 for (long clsId : visibleClasses.getVisibleClasses()) {
163 // For each visible class get the module the class belongs to
164 JdwpModuleReply modReply = new JdwpModuleCmd(clsId).send(channel);
165 assertReply(modReply);
166 long clsModId = modReply.getModuleId();
167
168 // At least one of the visible classes should belong to our module
169 if (modId == clsModId) {
170 moduleFound = true;
171 break;
172 }
173 }
174 assertTrue(moduleFound, "None of the visible classes for the classloader of the module " + getModuleName(modId) + " reports the module as its own");
175 }
176
177 }
|
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 import java.io.IOException;
25 import java.util.Arrays;
26 import java.util.concurrent.CountDownLatch;
27 import java.util.Set;
28 import java.util.HashSet;
29 import static jdk.test.lib.Asserts.assertTrue;
30
31 /**
32 * @test
33 * @summary Tests AllModules JDWP command
34 * @library /test/lib
35 * @modules java.base/jdk.internal.misc
36 * @compile AllModulesCommandTestDebuggee.java
37 * @run main/othervm AllModulesCommandTest
38 */
39 public class AllModulesCommandTest implements DebuggeeLauncher.Listener {
40
41 private DebuggeeLauncher launcher;
42 private JdwpChannel channel;
43 private CountDownLatch jdwpLatch = new CountDownLatch(1);
44 private Set<String> jdwpModuleNames = new HashSet<>();
45 private Set<String> javaModuleNames = new HashSet<>();
46
47 public static void main(String[] args) throws Throwable {
48 new AllModulesCommandTest().doTest();
49 }
50
51 private void doTest() throws Throwable {
52 launcher = new DebuggeeLauncher(this);
53 launcher.launchDebuggee();
71 }
72
73 @Override
74 public void onDebuggeeError(String message) {
75 System.err.println("Debuggee error: '" + message + "'");
76 System.exit(1);
77 }
78
79 private void doJdwp() throws Exception {
80 try {
81 // Establish JDWP socket connection
82 channel = new JdwpChannel();
83 channel.connect();
84 // Send out ALLMODULES JDWP command
85 // and verify the reply
86 JdwpAllModulesReply reply = new JdwpAllModulesCmd().send(channel);
87 assertReply(reply);
88 for (int i = 0; i < reply.getModulesCount(); ++i) {
89 long modId = reply.getModuleId(i);
90 // For each module reported by JDWP get its name using the JDWP NAME command
91 getModuleName(modId);
92 // Assert the JDWP CANREAD and CLASSLOADER commands
93 assertCanRead(modId);
94 assertClassLoader(modId);
95 }
96
97 System.out.println("Module names reported by JDWP: " + Arrays.toString(jdwpModuleNames.toArray()));
98 System.out.println("Module names reported by Java: " + Arrays.toString(javaModuleNames.toArray()));
99
100 // Modules reported by the JDWP should be the same as reported by the Java API
101 if (!jdwpModuleNames.equals(javaModuleNames)) {
102 throw new RuntimeException("Modules info reported by Java API differs from that reported by JDWP.");
103 } else {
104 System.out.println("Test passed!");
105 }
106
107 } finally {
108 launcher.terminateDebuggee();
109 try {
110 new JdwpExitCmd(0).send(channel);
111 channel.disconnect();
112 } catch (Exception x) {
113 }
114 }
115 }
116
117 private void getModuleName(long modId) throws IOException {
118 // Send out the JDWP NAME command and store the reply
119 JdwpModNameReply reply = new JdwpModNameCmd(modId).send(channel);
120 assertReply(reply);
121 String modName = reply.getModuleName();
122 if (modName != null) { // JDWP reports unnamed modules, ignore them
123 jdwpModuleNames.add(modName);
124 }
125 }
126
127 private void assertReply(JdwpReply reply) {
128 // Simple assert for any JDWP reply
129 if (reply.getErrorCode() != 0) {
130 throw new RuntimeException("Unexpected reply error code " + reply.getErrorCode() + " for reply " + reply);
131 }
132 }
133
134 private void assertCanRead(long modId) throws IOException {
135 // Simple assert for the CANREAD command
136 JdwpCanReadReply reply = new JdwpCanReadCmd(modId, modId).send(channel);
137 assertReply(reply);
138 assertTrue(reply.canRead(), "canRead() reports false for reading from the same module");
139 }
140
141 private void assertClassLoader(long modId) throws IOException {
142 // Simple assert for the CLASSLOADER command
143 JdwpClassLoaderReply reply = new JdwpClassLoaderCmd(modId).send(channel);
144 assertReply(reply);
145 long clId = reply.getClassLoaderId();
146 assertTrue(clId >= 0, "bad classloader refId " + clId + " for module id " + modId);
147 }
148
149 }
|