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 /test/lib
  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
  35  * @run main SASymbolTableTest
  36  */
  37 
  38 import java.util.Arrays;
  39 import java.util.List;
  40 import jdk.test.lib.process.ProcessTools;
  41 import jdk.test.lib.process.OutputAnalyzer;
  42 import jdk.test.lib.JDKToolFinder;
  43 import jdk.test.lib.Platform;
  44 import jdk.test.lib.apps.LingeredApp;
  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     private static LingeredApp theApp = null;
  57 
  58     public static void main(String[] args) throws Exception {
  59         if (!Platform.shouldSAAttach()) {
  60             System.out.println("SA attach not expected to work - test skipped.");
  61             return;
  62         }
  63         createArchive();
  64         run(true);
  65         run(false);
  66     }
  67 
  68     private static void createArchive()  throws Exception {
  69         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
  70             "-XX:+UnlockDiagnosticVMOptions",
  71             "-XX:SharedArchiveFile=" + jsaName,
  72             "-Xshare:dump");
  73 
  74         OutputAnalyzer output = new OutputAnalyzer(pb.start());
  75         output.shouldContain("Loading classes to share");
  76         output.shouldHaveExitValue(0);
  77     }
  78 
  79     private static void run(boolean useArchive) throws Exception {
  80         String flag = useArchive ? "auto" : "off";
  81 
  82         try {
  83             // (1) Launch the attachee process
  84             System.out.println("Starting LingeredApp");
  85             List<String> vmOpts = Arrays.asList(
  86                     "-XX:+UnlockDiagnosticVMOptions",
  87                     "-XX:SharedArchiveFile=" + jsaName,
  88                     "-Xshare:" + flag,
  89                     "-showversion");                // so we can see "sharing" in the output
  90 
  91             theApp = LingeredApp.startApp(vmOpts);
  92 
  93             // (2) Launch the agent process
  94             long pid = theApp.getPid();
  95             System.out.println("Attaching agent to " + pid );
  96             ProcessBuilder tool = ProcessTools.createJavaProcessBuilder(
  97                     "--add-modules=jdk.hotspot.agent",
  98                     "--add-exports=jdk.hotspot.agent/sun.jvm.hotspot.oops=ALL-UNNAMED",
  99                     "--add-exports=jdk.hotspot.agent/sun.jvm.hotspot.memory=ALL-UNNAMED",
 100                     "--add-exports=jdk.hotspot.agent/sun.jvm.hotspot.runtime=ALL-UNNAMED",
 101                     "--add-exports=jdk.hotspot.agent/sun.jvm.hotspot.tools=ALL-UNNAMED",
 102                     "SASymbolTableTestAgent",
 103                     Long.toString(pid));
 104             OutputAnalyzer output = ProcessTools.executeProcess(tool);
 105             System.out.println("STDOUT[");
 106             System.out.println(output.getOutput());
 107             if (output.getStdout().contains("connected too early")) {
 108                 System.out.println("SymbolTable not created by VM - test skipped");
 109                 return;
 110             }
 111             System.out.println("]");
 112             System.out.println("STDERR[");
 113             System.out.print(output.getStderr());
 114             System.out.println("]");
 115             output.shouldHaveExitValue(0);
 116         } catch (Exception ex) {
 117             throw new RuntimeException("Test ERROR " + ex, ex);
 118         } finally {
 119             LingeredApp.stopApp(theApp);
 120         }
 121     }
 122 }