1 /*
   2  * Copyright (c) 2017, 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.cds.CDSTestUtils;
  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 import jdk.test.lib.apps.LingeredApp;
  46 
  47 /*
  48  * The purpose of this test is to validate that we can use SA to
  49  * attach a process and walk its SymbolTable, regardless whether
  50  * the attachee process runs in CDS mode or not.
  51  *
  52  * SASymbolTableTest Just sets up the agent and attachee processes.
  53  * The SymbolTable walking is done in the SASymbolTableTestAgent class.
  54  */
  55 public class SASymbolTableTest {
  56     static String jsaName = "./SASymbolTableTest.jsa";
  57     private static LingeredApp theApp = null;
  58 
  59 
  60     public static void main(String[] args) throws Exception {
  61         if (!Platform.shouldSAAttach()) {
  62             System.out.println("SA attach not expected to work - test skipped.");
  63             return;
  64         }
  65 
  66         CDSTestUtils.createArchiveAndCheck();
  67         run(true);
  68         run(false);
  69     }
  70 
  71 
  72     private static void run(boolean useArchive) throws Exception {
  73         String flag = useArchive ? "auto" : "off";
  74 
  75         try {
  76             // (1) Launch the attachee process
  77             System.out.println("Starting LingeredApp");
  78             List<String> vmOpts = Arrays.asList(
  79                     "-XX:+UnlockDiagnosticVMOptions",
  80                     "-XX:SharedArchiveFile=" + jsaName,
  81                     "-Xshare:" + flag,
  82                     "-showversion");                // so we can see "sharing" in the output
  83 
  84             theApp = LingeredApp.startApp(vmOpts);
  85 
  86             // (2) Launch the agent process
  87             long pid = theApp.getPid();
  88             System.out.println("Attaching agent to " + pid );
  89             ProcessBuilder tool = ProcessTools.createJavaProcessBuilder(
  90                     "--add-modules=jdk.hotspot.agent",
  91                     "--add-exports=jdk.hotspot.agent/sun.jvm.hotspot.oops=ALL-UNNAMED",
  92                     "--add-exports=jdk.hotspot.agent/sun.jvm.hotspot.memory=ALL-UNNAMED",
  93                     "--add-exports=jdk.hotspot.agent/sun.jvm.hotspot.runtime=ALL-UNNAMED",
  94                     "--add-exports=jdk.hotspot.agent/sun.jvm.hotspot.tools=ALL-UNNAMED",
  95                     "SASymbolTableTestAgent",
  96                     Long.toString(pid));
  97             OutputAnalyzer output = ProcessTools.executeProcess(tool);
  98             System.out.println("STDOUT[");
  99             System.out.println(output.getOutput());
 100             if (output.getStdout().contains("connected too early")) {
 101                 System.out.println("SymbolTable not created by VM - test skipped");
 102                 return;
 103             }
 104             System.out.println("]");
 105             System.out.println("STDERR[");
 106             System.out.print(output.getStderr());
 107             System.out.println("]");
 108             output.shouldHaveExitValue(0);
 109         } catch (Exception ex) {
 110             throw new RuntimeException("Test ERROR " + ex, ex);
 111         } finally {
 112             LingeredApp.stopApp(theApp);
 113         }
 114     }
 115 }