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