1 /*
  2  * Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 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 
 25 import jdk.test.lib.JDKToolFinder;
 26 import jdk.test.lib.cds.CDSTestUtils;
 27 import jdk.test.lib.process.OutputAnalyzer;
 28 import jtreg.SkippedException;
 29 
 30 /*
 31  * @test
 32  * @bug 8213445
 33  * @summary Test dumping of dynamic shared symbols using jcmd
 34  * @requires vm.cds
 35  * @library /test/lib /test/hotspot/jtreg/runtime/cds/appcds
 36  *          /test/hotspot/jtreg/runtime/cds/appcds/test-classes
 37  *          /test/hotspot/jtreg/runtime/cds/appcds/jcmd
 38  * @build JCmdTestLingeredApp Hello jdk.test.lib.apps.LingeredApp
 39  * @build jdk.test.whitebox.WhiteBox
 40  * @run driver jdk.test.lib.helpers.ClassFileInstaller -jar loadclass.jar JCmdTestLingeredApp
 41  *             jdk.test.lib.apps.LingeredApp jdk.test.lib.apps.LingeredApp$1
 42  * @run driver jdk.test.lib.helpers.ClassFileInstaller -jar WhiteBox.jar jdk.test.whitebox.WhiteBox
 43  * @run main/othervm/timeout=500 -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:./WhiteBox.jar DynamicSharedSymbols
 44  */
 45 
 46 import jdk.test.lib.apps.LingeredApp;
 47 import jdk.test.lib.helpers.ClassFileInstaller;
 48 import jdk.test.whitebox.WhiteBox;
 49 
 50 public class DynamicSharedSymbols extends DynamicArchiveTestBase {
 51 
 52     public static void main(String[] args) throws Exception {
 53         runTest(DynamicSharedSymbols::testDefaultBase);
 54     }
 55 
 56     static void testDefaultBase() throws Exception {
 57         String topArchiveName = getNewArchiveName("top");
 58         doTest(topArchiveName);
 59     }
 60 
 61     private static void doTest(String topArchiveName) throws Exception {
 62         WhiteBox wb = WhiteBox.getWhiteBox();
 63         if (!wb.isSharingEnabled()) {
 64             throw new SkippedException("Sharing is not enabled, test is skipped.");
 65         }
 66 
 67         String appJar = ClassFileInstaller.getJarPath("loadclass.jar");
 68 
 69         // Create a dynamic archive.
 70         String dynamicDumpArg = "-XX:ArchiveClassesAtExit=" + topArchiveName;
 71         JCmdTestLingeredApp theApp = new JCmdTestLingeredApp();
 72         LingeredApp.startApp(theApp, dynamicDumpArg,
 73                              "-Xlog:cds,cds+dynamic=info",
 74                              "-cp", appJar);
 75         long pid = theApp.getPid();
 76         LingeredApp.stopApp(theApp);
 77 
 78         // Run with dynamic archive.
 79         String sharedArchiveArg = "-XX:SharedArchiveFile=" + topArchiveName;
 80         theApp = new JCmdTestLingeredApp();
 81         LingeredApp.startApp(theApp,
 82                              "-Xshare:on", sharedArchiveArg,
 83                              "-Xlog:cds,class+load",
 84                              "-cp", appJar);
 85         pid = theApp.getPid();
 86 
 87 
 88         // Use jcmd to dump the symbols of the above process running
 89         // with dynamic shared archive.
 90         ProcessBuilder pb = new ProcessBuilder();
 91         pb.command(new String[] {JDKToolFinder.getJDKTool("jcmd"), Long.toString(pid), "VM.symboltable", "-verbose"});
 92         OutputAnalyzer output = CDSTestUtils.executeAndLog(pb, "jcmd-symboltable");
 93         output.shouldContain("17 3: jdk/test/lib/apps\n");  // 3 because a TempSymbol will be found in the TempSymbolCleanupDelayer queue.
 94                    // Note: we might want to drain the queue before CDS dumps but this is correct for now, unless the queue length changes.
 95         output.shouldContain("Dynamic shared symbols:\n");
 96         output.shouldContain("5 65535: Hello\n");
 97 
 98         LingeredApp.stopApp(theApp);
 99     }
100 }