1 /*
   2  * Copyright (c) 2016, 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  * @test SASymbolTableTest
  26  * @summary Walk symbol table using SA, with and without CDS.
  27  * @library /testlibrary
  28  * @modules java.base/jdk.internal.misc
  29  *          jdk.hotspot.agent/sun.jvm.hotspot.oops
  30  *          jdk.hotspot.agent/sun.jvm.hotspot.memory
  31  *          jdk.hotspot.agent/sun.jvm.hotspot.runtime
  32  *          jdk.hotspot.agent/sun.jvm.hotspot.tools
  33  *          java.management
  34  * @build SASymbolTableTestAgent SASymbolTableTestAttachee jdk.test.lib.*
  35  * @run main SASymbolTableTest
  36  */
  37 
  38 import jdk.test.lib.*;
  39 
  40 /*
  41  * The purpose of this test is to validate that we can use SA to
  42  * attach a process and walk its SymbolTable, regardless whether
  43  * the attachee process runs in CDS mode or not.
  44  *
  45  * SASymbolTableTest Just sets up the agent and attachee processes.
  46  * The SymbolTable walking is done in the SASymbolTableTestAgent class.
  47  */
  48 public class SASymbolTableTest {
  49     static String jsaName = "./SASymbolTableTest.jsa";
  50 
  51     public static void main(String[] args) throws Exception {
  52         if (!Platform.shouldSAAttach()) {
  53             System.out.println("SA attach not expected to work - test skipped.");
  54             return;
  55         }
  56         createArchive();
  57         run(true);
  58         run(false);
  59     }
  60 
  61     private static void createArchive()  throws Exception {
  62         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
  63             "-XX:+UnlockDiagnosticVMOptions",
  64             "-XX:SharedArchiveFile=" + jsaName,
  65             "-Xshare:dump");
  66 
  67         OutputAnalyzer output = new OutputAnalyzer(pb.start());
  68         output.shouldContain("Loading classes to share");
  69         output.shouldHaveExitValue(0);
  70     }
  71 
  72     private static void run(boolean useArchive) throws Exception {
  73         String flag = useArchive ? "auto" : "off";
  74 
  75         // (1) Launch the attachee process
  76         ProcessBuilder attachee = ProcessTools.createJavaProcessBuilder(
  77             "-XX:+UnlockDiagnosticVMOptions",
  78             "-XX:SharedArchiveFile=" + jsaName,
  79             "-Xshare:" + flag,
  80             "-showversion",                // so we can see "sharing" in the output
  81             "SASymbolTableTestAttachee");
  82 
  83         final Process p = attachee.start();
  84 
  85         // (2) Launch the agent process
  86         long pid = p.getPid();
  87         System.out.println("Attaching agent " + pid);
  88         ProcessBuilder tool = ProcessTools.createJavaProcessBuilder(
  89             "--add-exports=jdk.hotspot.agent/sun.jvm.hotspot.oops=ALL-UNNAMED",
  90             "--add-exports=jdk.hotspot.agent/sun.jvm.hotspot.memory=ALL-UNNAMED",
  91             "--add-exports=jdk.hotspot.agent/sun.jvm.hotspot.runtime=ALL-UNNAMED",
  92             "--add-exports=jdk.hotspot.agent/sun.jvm.hotspot.tools=ALL-UNNAMED",
  93             "SASymbolTableTestAgent",
  94             Long.toString(pid));
  95         OutputAnalyzer output = ProcessTools.executeProcess(tool);
  96         System.out.println(output.getOutput());
  97         output.shouldHaveExitValue(0);
  98 
  99         Thread t = new Thread() {
 100                 public void run() {
 101                     try {
 102                         OutputAnalyzer output = new OutputAnalyzer(p);
 103                         System.out.println("STDOUT[");
 104                         System.out.print(output.getStdout());
 105                         System.out.println("]");
 106                         System.out.println("STDERR[");
 107                         System.out.print(output.getStderr());
 108                         System.out.println("]");
 109                     } catch (Throwable t) {
 110                         t.printStackTrace();
 111                     }
 112                 }
 113             };
 114         t.start();
 115 
 116         Thread.sleep(2 * 1000);
 117         p.destroy();
 118         t.join();
 119     }
 120 }