Path: blob/master/test/hotspot/jtreg/serviceability/jdwp/AllModulesCommandTest.java
41149 views
/*1* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.IOException;24import java.util.Arrays;25import java.util.concurrent.CountDownLatch;26import java.util.Set;27import java.util.HashSet;28import static jdk.test.lib.Asserts.assertTrue;2930/**31* @test32* @summary Tests the modules-related JDWP commands33* @library /test/lib34* @modules jdk.jdwp.agent35* @modules java.base/jdk.internal.misc36* @requires vm.jvmti37* @compile AllModulesCommandTestDebuggee.java38* @run main/othervm AllModulesCommandTest39*/40public class AllModulesCommandTest implements DebuggeeLauncher.Listener {4142private DebuggeeLauncher launcher;43private JdwpChannel channel;44private CountDownLatch jdwpLatch = new CountDownLatch(1);45private Set<String> jdwpModuleNames = new HashSet<>();46private Set<String> javaModuleNames = new HashSet<>();4748public static void main(String[] args) throws Throwable {49new AllModulesCommandTest().doTest();50}5152private void doTest() throws Throwable {53launcher = new DebuggeeLauncher(this);54launcher.launchDebuggee();55// Await till the debuggee sends all the necessary modules info to check against56// then start the JDWP session57jdwpLatch.await();58doJdwp();59}6061@Override62public void onDebuggeeModuleInfo(String modName) {63// The debuggee has sent out info about a loaded module64javaModuleNames.add(modName);65}6667@Override68public void onDebuggeeSendingCompleted() {69// The debuggee has completed sending all the info70// We can start the JDWP session71jdwpLatch.countDown();72}7374@Override75public void onDebuggeeError(String message) {76System.err.println("Debuggee error: '" + message + "'");77System.exit(1);78}7980private void doJdwp() throws Exception {81try {82// Establish JDWP socket connection83channel = new JdwpChannel();84channel.connect();85// Send out ALLMODULES JDWP command86// and verify the reply87JdwpAllModulesReply reply = new JdwpAllModulesCmd().send(channel);88assertReply(reply);89for (int i = 0; i < reply.getModulesCount(); ++i) {90long modId = reply.getModuleId(i);91// For each module reported by JDWP get its name using the JDWP NAME command92// and store the reply93String modName = getModuleName(modId);94System.out.println("i=" + i + ", modId=" + modId + ", modName=" + modName);95if (modName != null) { // JDWP reports unnamed modules, ignore them96jdwpModuleNames.add(modName);97}98// Assert the CLASSLOADER commands99assertClassLoader(modId, modName);100}101102System.out.println("Module names reported by JDWP: " + Arrays.toString(jdwpModuleNames.toArray()));103System.out.println("Module names reported by Java: " + Arrays.toString(javaModuleNames.toArray()));104105// Modules reported by the JDWP should be the same as reported by the Java API106if (!jdwpModuleNames.equals(javaModuleNames)) {107throw new RuntimeException("Modules info reported by Java API differs from that reported by JDWP.");108} else {109System.out.println("Test passed!");110}111112} finally {113launcher.terminateDebuggee();114try {115new JdwpExitCmd(0).send(channel);116channel.disconnect();117} catch (Exception x) {118}119}120}121122private String getModuleName(long modId) throws IOException {123JdwpModNameReply reply = new JdwpModNameCmd(modId).send(channel);124assertReply(reply);125return reply.getModuleName();126}127128private void assertReply(JdwpReply reply) {129// Simple assert for any JDWP reply130if (reply.getErrorCode() != 0) {131throw new RuntimeException("Unexpected reply error code " + reply.getErrorCode() + " for reply " + reply);132}133}134135private void assertClassLoader(long modId, String modName) throws IOException {136// Verify that the module classloader id is valid137JdwpClassLoaderReply reply = new JdwpClassLoaderCmd(modId).send(channel);138assertReply(reply);139long moduleClassLoader = reply.getClassLoaderId();140assertTrue(moduleClassLoader >= 0, "bad classloader refId " + moduleClassLoader + " for module '" + modName + "', moduleId=" + modId);141142String clsModName = getModuleName(modId);143if ("java.base".equals(clsModName)) {144// For the java.base module, because there will be some loaded classes, we can verify145// that some of the loaded classes do report the java.base module as the module they belong to146assertGetModule(moduleClassLoader, modId);147}148}149150private void assertGetModule(long moduleClassLoader, long modId) throws IOException {151// Get all the visible classes for the module classloader152JdwpVisibleClassesReply visibleClasses = new JdwpVisibleClassesCmd(moduleClassLoader).send(channel);153assertReply(visibleClasses);154155boolean moduleFound = false;156for (long clsId : visibleClasses.getVisibleClasses()) {157// For each visible class get the module the class belongs to158JdwpModuleReply modReply = new JdwpModuleCmd(clsId).send(channel);159assertReply(modReply);160long clsModId = modReply.getModuleId();161162// At least one of the visible classes should belong to our module163if (modId == clsModId) {164moduleFound = true;165break;166}167}168assertTrue(moduleFound, "None of the visible classes for the classloader of the module " + getModuleName(modId) + " reports the module as its own");169}170171}172173174