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