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 import java.util.ArrayList;
  25 import java.util.List;
  26 
  27 import sun.jvm.hotspot.HotSpotAgent;
  28 import sun.jvm.hotspot.utilities.SystemDictionaryHelper;
  29 import sun.jvm.hotspot.oops.InstanceKlass;
  30 import sun.jvm.hotspot.debugger.*;
  31 import sun.jvm.hotspot.oops.Method;
  32 import sun.jvm.hotspot.utilities.MethodArray;
  33 import sun.jvm.hotspot.ui.classbrowser.HTMLGenerator;
  34 
  35 import jdk.test.lib.apps.LingeredApp;
  36 import jdk.test.lib.JDKToolLauncher;
  37 import jdk.test.lib.JDKToolFinder;
  38 import jdk.test.lib.Platform;
  39 import jdk.test.lib.process.ProcessTools;
  40 import jdk.test.lib.process.OutputAnalyzer;
  41 import jdk.test.lib.Utils;
  42 import jdk.test.lib.Asserts;
  43 
  44 /*
  45  * @test
  46  * @library /test/lib
  47  * @requires os.family != "mac"
  48  * @modules java.base/jdk.internal.misc
  49  *          jdk.hotspot.agent/sun.jvm.hotspot
  50  *          jdk.hotspot.agent/sun.jvm.hotspot.utilities
  51  *          jdk.hotspot.agent/sun.jvm.hotspot.oops
  52  *          jdk.hotspot.agent/sun.jvm.hotspot.debugger
  53  *          jdk.hotspot.agent/sun.jvm.hotspot.ui.classbrowser
  54  * @ignore 8169232
  55  * @run main/othervm TestCpoolForInvokeDynamic
  56  */
  57 
  58 public class TestCpoolForInvokeDynamic {
  59 
  60     private static LingeredAppWithInvokeDynamic theApp = null;
  61 
  62     private static void printBytecodes(String pid,
  63                                        String[] instanceKlassNames) {
  64         HotSpotAgent agent = new HotSpotAgent();
  65         try {
  66             agent.attach(Integer.parseInt(pid));
  67         }
  68         catch (DebuggerException e) {
  69             System.out.println(e.getMessage());
  70             System.err.println("Unable to connect to process ID: " + pid);
  71 
  72             agent.detach();
  73             e.printStackTrace();
  74         }
  75 
  76         for (String instanceKlassName : instanceKlassNames) {
  77             InstanceKlass iKlass = SystemDictionaryHelper.findInstanceKlass(instanceKlassName);
  78             MethodArray methods = iKlass.getMethods();
  79             for (int i = 0; i < methods.length(); i++) {
  80                 Method m = methods.at(i);
  81                 System.out.println("Method: " + m.getName().asString() +
  82                                    " in instance klass: " + instanceKlassName);
  83                 HTMLGenerator gen = new HTMLGenerator(false);
  84                 System.out.println(gen.genHTML(m));
  85             }
  86         }
  87         agent.detach();
  88     }
  89 
  90     private static void createAnotherToAttach(
  91                             String[] instanceKlassNames,
  92                             long lingeredAppPid) throws Exception {
  93 
  94         String[] toolArgs = {
  95             "--add-modules=jdk.hotspot.agent",
  96             "--add-exports=jdk.hotspot.agent/sun.jvm.hotspot=ALL-UNNAMED",
  97             "--add-exports=jdk.hotspot.agent/sun.jvm.hotspot.utilities=ALL-UNNAMED",
  98             "--add-exports=jdk.hotspot.agent/sun.jvm.hotspot.oops=ALL-UNNAMED",
  99             "--add-exports=jdk.hotspot.agent/sun.jvm.hotspot.debugger=ALL-UNNAMED",
 100             "--add-exports=jdk.hotspot.agent/sun.jvm.hotspot.ui.classbrowser=ALL-UNNAMED",
 101             "TestCpoolForInvokeDynamic",
 102             Long.toString(lingeredAppPid)
 103         };
 104 
 105         // Start a new process to attach to the lingered app
 106         ProcessBuilder processBuilder = ProcessTools.createJavaProcessBuilder(toolArgs);
 107         OutputAnalyzer SAOutput = ProcessTools.executeProcess(processBuilder);
 108         SAOutput.shouldHaveExitValue(0);
 109         System.out.println(SAOutput.getOutput());
 110 
 111         SAOutput.shouldContain("invokedynamic");
 112         SAOutput.shouldContain("Name and Type");
 113         SAOutput.shouldContain("run:()Ljava.lang.Runnable");
 114         SAOutput.shouldContain("compare:()LTestComparator");
 115         SAOutput.shouldNotContain("Corrupted constant pool");
 116     }
 117 
 118     public static void main (String... args) throws Exception {
 119 
 120         String[] instanceKlassNames = new String[] {
 121                                           "LingeredAppWithInvokeDynamic"
 122                                       };
 123 
 124         if (!Platform.shouldSAAttach()) {
 125             System.out.println(
 126                "SA attach not expected to work - test skipped.");
 127             return;
 128         }
 129 
 130         if (args == null || args.length == 0) {
 131             try {
 132                 List<String> vmArgs = new ArrayList<String>();
 133                 vmArgs.add("-XX:+UsePerfData");
 134                 vmArgs.addAll(Utils.getVmOptions());
 135 
 136                 theApp = new LingeredAppWithInvokeDynamic();
 137                 LingeredApp.startApp(vmArgs, theApp);
 138                 createAnotherToAttach(instanceKlassNames,
 139                                       theApp.getPid());
 140             } finally {
 141                 LingeredApp.stopApp(theApp);
 142             }
 143         } else {
 144             printBytecodes(args[0], instanceKlassNames);
 145         }
 146     }
 147 }