Path: blob/master/test/hotspot/jtreg/serviceability/sa/TestCpoolForInvokeDynamic.java
41152 views
/*1* Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.util.ArrayList;24import java.util.List;2526import sun.jvm.hotspot.HotSpotAgent;27import sun.jvm.hotspot.utilities.SystemDictionaryHelper;28import sun.jvm.hotspot.oops.InstanceKlass;29import sun.jvm.hotspot.debugger.*;30import sun.jvm.hotspot.oops.Method;31import sun.jvm.hotspot.utilities.MethodArray;32import sun.jvm.hotspot.ui.classbrowser.HTMLGenerator;3334import jdk.test.lib.apps.LingeredApp;35import jdk.test.lib.Asserts;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.SA.SATestUtils;42import jdk.test.lib.Utils;4344/**45* @test46* @library /test/lib47* @requires vm.hasSA48* @modules java.base/jdk.internal.misc49* jdk.hotspot.agent/sun.jvm.hotspot50* jdk.hotspot.agent/sun.jvm.hotspot.utilities51* jdk.hotspot.agent/sun.jvm.hotspot.oops52* jdk.hotspot.agent/sun.jvm.hotspot.debugger53* jdk.hotspot.agent/sun.jvm.hotspot.ui.classbrowser54* @run main TestCpoolForInvokeDynamic55*/5657public class TestCpoolForInvokeDynamic {5859private static LingeredAppWithInvokeDynamic theApp = null;6061private static void printBytecodes(String pid,62String[] instanceKlassNames) {63HotSpotAgent agent = new HotSpotAgent();64try {65agent.attach(Integer.parseInt(pid));66}67catch (DebuggerException e) {68System.out.println(e.getMessage());69System.err.println("Unable to connect to process ID: " + pid);7071agent.detach();72e.printStackTrace();73}7475for (String instanceKlassName : instanceKlassNames) {76InstanceKlass iKlass = SystemDictionaryHelper.findInstanceKlass(instanceKlassName);77MethodArray methods = iKlass.getMethods();78for (int i = 0; i < methods.length(); i++) {79Method m = methods.at(i);80System.out.println("Method: " + m.getName().asString() +81" in instance klass: " + instanceKlassName);82HTMLGenerator gen = new HTMLGenerator(false);83System.out.println(gen.genHTML(m));84}85}86agent.detach();87}8889private static void createAnotherToAttach(90String[] instanceKlassNames,91long lingeredAppPid) throws Exception {92// Start a new process to attach to the lingered app93ProcessBuilder processBuilder = ProcessTools.createJavaProcessBuilder(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",101Long.toString(lingeredAppPid));102SATestUtils.addPrivilegesIfNeeded(processBuilder);103OutputAnalyzer SAOutput = ProcessTools.executeProcess(processBuilder);104SAOutput.shouldHaveExitValue(0);105System.out.println(SAOutput.getOutput());106107SAOutput.shouldContain("invokedynamic");108SAOutput.shouldContain("Name and Type");109SAOutput.shouldContain("run:()Ljava.lang.Runnable");110SAOutput.shouldContain("compare:()LTestComparator");111SAOutput.shouldNotContain("Corrupted constant pool");112}113114public static void main (String... args) throws Exception {115SATestUtils.skipIfCannotAttach(); // throws SkippedException if attach not expected to work.116117String[] instanceKlassNames = new String[] {118"LingeredAppWithInvokeDynamic"119};120121if (args == null || args.length == 0) {122try {123theApp = new LingeredAppWithInvokeDynamic();124LingeredApp.startApp(theApp, "-XX:+UsePerfData");125createAnotherToAttach(instanceKlassNames,126theApp.getPid());127} finally {128LingeredApp.stopApp(theApp);129}130} else {131printBytecodes(args[0], instanceKlassNames);132}133}134}135136137