Path: blob/master/test/hotspot/jtreg/serviceability/jvmti/CanGenerateAllClassHook/CanGenerateAllClassHook.java
41155 views
/*1* Copyright (c) 2018, 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*/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* @library /test/lib32* @compile CanGenerateAllClassHook.java33* @run main/othervm/native CanGenerateAllClassHook34*/3536import jdk.test.lib.cds.CDSTestUtils;37import jdk.test.lib.process.OutputAnalyzer;38import jdk.test.lib.process.ProcessTools;39import java.io.File;40import java.io.IOException;4142/*43* The simplest way to test is to use system classes.jsa,44* but we cannot rely on tested JRE/JDK has it.45* So the test runs 2 java processes -46* 1st to generate custom shared archive file:47* java -XX:+UnlockDiagnosticVMOptions -XX:SharedArchiveFile=<jsa_file> -Xshare:dump48* and 2nd to perform the actual testing using generated shared archive:49* java -XX:+UnlockDiagnosticVMOptions -XX:SharedArchiveFile=<jsa_file> -Xshare:on50* -agentlib:<agent> CanGenerateAllClassHook51*/52public class CanGenerateAllClassHook {5354private static final String agentLib = "CanGenerateAllClassHook";5556private static native int getClassHookAvail();57private static native int getOnLoadClassHookAvail();5859public static void main(String[] args) throws Exception {60if (args.length == 0) {61// this is master run6263final File jsaFile = File.createTempFile(agentLib, ".jsa");64jsaFile.deleteOnExit();65final String jsaPath = jsaFile.getAbsolutePath();6667log("generating CDS archive...");68execJava(69"-XX:+UnlockDiagnosticVMOptions",70"-XX:SharedArchiveFile=" + jsaPath,71"-Xshare:dump")72.shouldHaveExitValue(0);73log("CDS generation completed.");7475OutputAnalyzer output = execJava(76"-XX:+UnlockDiagnosticVMOptions",77"-XX:SharedArchiveFile=" + jsaPath,78"-Xshare:on",79"-agentlib:" + agentLib,80// copy java.library.path81"-Djava.library.path=" + System.getProperty("java.library.path"),82// specify "-showversion" to ensure the test runs in shared mode83"-showversion",84// class to run85CanGenerateAllClassHook.class.getCanonicalName(),86// and arg87"test");88// Xshare:on can cause intermittent failure89// checkExec handles this.90CDSTestUtils.checkExec(output);9192log("Test PASSED.");93} else {94// this is test run95try {96System.loadLibrary(agentLib);97} catch (UnsatisfiedLinkError ex) {98System.err.println("Failed to load " + agentLib + " lib");99System.err.println("java.library.path: " + System.getProperty("java.library.path"));100throw ex;101}102103final int onLoadValue = getOnLoadClassHookAvail();104final int liveValue = getClassHookAvail();105// Possible values returned:106// 1 - the capability is supported;107// 0 - the capability is not supported;108// -1 - error occured.109110log("can_generate_all_class_hook_events value capability:");111log("ONLOAD phase: " + (onLoadValue < 0 ? "Failed to read" : onLoadValue));112log("LIVE phase: " + (liveValue < 0 ? "Failed to read" : liveValue));113if (onLoadValue != 1 || liveValue != 1) {114throw new RuntimeException("The can_generate_all_class_hook_events capability "115+ " is expected to be available in both ONLOAD and LIVE phases");116}117}118}119120private static void log(String msg) {121System.out.println(msg);122System.out.flush();123}124125private static OutputAnalyzer execJava(String... args) throws IOException {126ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(args);127128OutputAnalyzer output = new OutputAnalyzer(pb.start());129130log("[STDERR]\n" + output.getStderr());131log("[STDOUT]\n" + output.getStdout());132133return output;134}135136}137138139