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