Path: blob/master/test/hotspot/jtreg/serviceability/jvmti/CanGenerateAllClassHook/CanGenerateAllClassHook.java
66646 views
/*1* Copyright (c) 2018, 2021, 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*/2223/**24* @test25* @bug 816160526* @summary Tests that jvmtiEnv::GetPotentialCapabilities reports27* can_generate_all_class_hook_events capability with CDS (-Xshare:on)28* at ONLOAD and LIVE phases29* @requires vm.jvmti30* @requires vm.cds31* @requires vm.flagless32* @library /test/lib33* @compile CanGenerateAllClassHook.java34* @run main/othervm/native CanGenerateAllClassHook35*/3637import jdk.test.lib.cds.CDSTestUtils;38import jdk.test.lib.process.OutputAnalyzer;39import jdk.test.lib.process.ProcessTools;40import java.io.File;41import java.io.IOException;4243/*44* The simplest way to test is to use system classes.jsa,45* but we cannot rely on tested JRE/JDK has it.46* So the test runs 2 java processes -47* 1st to generate custom shared archive file:48* java -XX:+UnlockDiagnosticVMOptions -XX:SharedArchiveFile=<jsa_file> -Xshare:dump49* and 2nd to perform the actual testing using generated shared archive:50* java -XX:+UnlockDiagnosticVMOptions -XX:SharedArchiveFile=<jsa_file> -Xshare:on51* -agentlib:<agent> CanGenerateAllClassHook52*/53public class CanGenerateAllClassHook {5455private static final String agentLib = "CanGenerateAllClassHook";5657private static native int getClassHookAvail();58private static native int getOnLoadClassHookAvail();5960public static void main(String[] args) throws Exception {61if (args.length == 0) {62// this is master run6364final File jsaFile = File.createTempFile(agentLib, ".jsa");65jsaFile.deleteOnExit();66final String jsaPath = jsaFile.getAbsolutePath();6768log("generating CDS archive...");69execJava(70"-XX:+UnlockDiagnosticVMOptions",71"-XX:SharedArchiveFile=" + jsaPath,72"-Xshare:dump")73.shouldHaveExitValue(0);74log("CDS generation completed.");7576OutputAnalyzer output = execJava(77"-XX:+UnlockDiagnosticVMOptions",78"-XX:SharedArchiveFile=" + jsaPath,79"-Xshare:on",80"-agentlib:" + agentLib,81// copy java.library.path82"-Djava.library.path=" + System.getProperty("java.library.path"),83// specify "-showversion" to ensure the test runs in shared mode84"-showversion",85// class to run86CanGenerateAllClassHook.class.getCanonicalName(),87// and arg88"test");89// Xshare:on can cause intermittent failure90// checkExec handles this.91CDSTestUtils.checkExec(output);9293log("Test PASSED.");94} else {95// this is test run96try {97System.loadLibrary(agentLib);98} catch (UnsatisfiedLinkError ex) {99System.err.println("Failed to load " + agentLib + " lib");100System.err.println("java.library.path: " + System.getProperty("java.library.path"));101throw ex;102}103104final int onLoadValue = getOnLoadClassHookAvail();105final int liveValue = getClassHookAvail();106// Possible values returned:107// 1 - the capability is supported;108// 0 - the capability is not supported;109// -1 - error occured.110111log("can_generate_all_class_hook_events value capability:");112log("ONLOAD phase: " + (onLoadValue < 0 ? "Failed to read" : onLoadValue));113log("LIVE phase: " + (liveValue < 0 ? "Failed to read" : liveValue));114if (onLoadValue != 1 || liveValue != 1) {115throw new RuntimeException("The can_generate_all_class_hook_events capability "116+ " is expected to be available in both ONLOAD and LIVE phases");117}118}119}120121private static void log(String msg) {122System.out.println(msg);123System.out.flush();124}125126private static OutputAnalyzer execJava(String... args) throws IOException {127ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(args);128129OutputAnalyzer output = new OutputAnalyzer(pb.start());130131log("[STDERR]\n" + output.getStderr());132log("[STDOUT]\n" + output.getStdout());133134return output;135}136137}138139140