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