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 import java.util.ArrayList;
  25 import java.util.List;
  26 
  27 import sun.jvm.hotspot.HotSpotAgent;
  28 import sun.jvm.hotspot.utilities.ReversePtrsAnalysis;
  29 
  30 import jdk.test.lib.apps.LingeredApp;
  31 import jdk.test.lib.JDKToolLauncher;
  32 import jdk.test.lib.JDKToolFinder;
  33 import jdk.test.lib.Platform;
  34 import jdk.test.lib.process.ProcessTools;
  35 import jdk.test.lib.process.OutputAnalyzer;
  36 import jdk.test.lib.Utils;
  37 import jdk.test.lib.Asserts;
  38 
  39 /*
  40  * @test
  41  * @library /test/lib
  42  * @requires os.family != "mac"
  43  * @modules java.base/jdk.internal.misc
  44  *          jdk.hotspot.agent/sun.jvm.hotspot
  45  *          jdk.hotspot.agent/sun.jvm.hotspot.utilities
  46  * @run main/othervm TestRevPtrsForInvokeDynamic
  47  */
  48 
  49 public class TestRevPtrsForInvokeDynamic {
  50 
  51     private static LingeredAppWithInvokeDynamic theApp = null;
  52 
  53     private static void computeReversePointers(String pid) throws Exception {
  54         HotSpotAgent agent = new HotSpotAgent();
  55 
  56         try {
  57             agent.attach(Integer.parseInt(pid));
  58             ReversePtrsAnalysis analysis = new ReversePtrsAnalysis();
  59             analysis.run();
  60         } finally {
  61             agent.detach();
  62         }
  63     }
  64 
  65     private static void createAnotherToAttach(long lingeredAppPid)
  66                                                          throws Exception {
  67         String[] toolArgs = {
  68             "--add-modules=jdk.hotspot.agent",
  69             "--add-exports=jdk.hotspot.agent/sun.jvm.hotspot=ALL-UNNAMED",
  70             "--add-exports=jdk.hotspot.agent/sun.jvm.hotspot.utilities=ALL-UNNAMED",
  71             "TestRevPtrsForInvokeDynamic",
  72             Long.toString(lingeredAppPid)
  73         };
  74 
  75         // Start a new process to attach to the lingered app
  76         ProcessBuilder processBuilder = ProcessTools.createJavaProcessBuilder(toolArgs);
  77         OutputAnalyzer SAOutput = ProcessTools.executeProcess(processBuilder);
  78         SAOutput.shouldHaveExitValue(0);
  79         System.out.println(SAOutput.getOutput());
  80     }
  81 
  82     public static void main (String... args) throws Exception {
  83         if (!Platform.shouldSAAttach()) {
  84             System.out.println(
  85                "SA attach not expected to work - test skipped.");
  86             return;
  87         }
  88 
  89         if (args == null || args.length == 0) {
  90             try {
  91                 List<String> vmArgs = new ArrayList<String>();
  92                 vmArgs.add("-XX:+UsePerfData");
  93                 vmArgs.addAll(Utils.getVmOptions());
  94 
  95                 theApp = new LingeredAppWithInvokeDynamic();
  96                 LingeredApp.startApp(vmArgs, theApp);
  97                 createAnotherToAttach(theApp.getPid());
  98             } finally {
  99                 LingeredApp.stopApp(theApp);
 100             }
 101         } else {
 102             computeReversePointers(args[0]);
 103         }
 104     }
 105 }