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