Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/hotspot/jtreg/serviceability/jvmti/CanGenerateAllClassHook/CanGenerateAllClassHook.java
66646 views
1
/*
2
* Copyright (c) 2018, 2021, 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
/**
25
* @test
26
* @bug 8161605
27
* @summary Tests that jvmtiEnv::GetPotentialCapabilities reports
28
* can_generate_all_class_hook_events capability with CDS (-Xshare:on)
29
* at ONLOAD and LIVE phases
30
* @requires vm.jvmti
31
* @requires vm.cds
32
* @requires vm.flagless
33
* @library /test/lib
34
* @compile CanGenerateAllClassHook.java
35
* @run main/othervm/native CanGenerateAllClassHook
36
*/
37
38
import jdk.test.lib.cds.CDSTestUtils;
39
import jdk.test.lib.process.OutputAnalyzer;
40
import jdk.test.lib.process.ProcessTools;
41
import java.io.File;
42
import java.io.IOException;
43
44
/*
45
* The simplest way to test is to use system classes.jsa,
46
* but we cannot rely on tested JRE/JDK has it.
47
* So the test runs 2 java processes -
48
* 1st to generate custom shared archive file:
49
* java -XX:+UnlockDiagnosticVMOptions -XX:SharedArchiveFile=<jsa_file> -Xshare:dump
50
* and 2nd to perform the actual testing using generated shared archive:
51
* java -XX:+UnlockDiagnosticVMOptions -XX:SharedArchiveFile=<jsa_file> -Xshare:on
52
* -agentlib:<agent> CanGenerateAllClassHook
53
*/
54
public class CanGenerateAllClassHook {
55
56
private static final String agentLib = "CanGenerateAllClassHook";
57
58
private static native int getClassHookAvail();
59
private static native int getOnLoadClassHookAvail();
60
61
public static void main(String[] args) throws Exception {
62
if (args.length == 0) {
63
// this is master run
64
65
final File jsaFile = File.createTempFile(agentLib, ".jsa");
66
jsaFile.deleteOnExit();
67
final String jsaPath = jsaFile.getAbsolutePath();
68
69
log("generating CDS archive...");
70
execJava(
71
"-XX:+UnlockDiagnosticVMOptions",
72
"-XX:SharedArchiveFile=" + jsaPath,
73
"-Xshare:dump")
74
.shouldHaveExitValue(0);
75
log("CDS generation completed.");
76
77
OutputAnalyzer output = execJava(
78
"-XX:+UnlockDiagnosticVMOptions",
79
"-XX:SharedArchiveFile=" + jsaPath,
80
"-Xshare:on",
81
"-agentlib:" + agentLib,
82
// copy java.library.path
83
"-Djava.library.path=" + System.getProperty("java.library.path"),
84
// specify "-showversion" to ensure the test runs in shared mode
85
"-showversion",
86
// class to run
87
CanGenerateAllClassHook.class.getCanonicalName(),
88
// and arg
89
"test");
90
// Xshare:on can cause intermittent failure
91
// checkExec handles this.
92
CDSTestUtils.checkExec(output);
93
94
log("Test PASSED.");
95
} else {
96
// this is test run
97
try {
98
System.loadLibrary(agentLib);
99
} catch (UnsatisfiedLinkError ex) {
100
System.err.println("Failed to load " + agentLib + " lib");
101
System.err.println("java.library.path: " + System.getProperty("java.library.path"));
102
throw ex;
103
}
104
105
final int onLoadValue = getOnLoadClassHookAvail();
106
final int liveValue = getClassHookAvail();
107
// Possible values returned:
108
// 1 - the capability is supported;
109
// 0 - the capability is not supported;
110
// -1 - error occured.
111
112
log("can_generate_all_class_hook_events value capability:");
113
log("ONLOAD phase: " + (onLoadValue < 0 ? "Failed to read" : onLoadValue));
114
log("LIVE phase: " + (liveValue < 0 ? "Failed to read" : liveValue));
115
if (onLoadValue != 1 || liveValue != 1) {
116
throw new RuntimeException("The can_generate_all_class_hook_events capability "
117
+ " is expected to be available in both ONLOAD and LIVE phases");
118
}
119
}
120
}
121
122
private static void log(String msg) {
123
System.out.println(msg);
124
System.out.flush();
125
}
126
127
private static OutputAnalyzer execJava(String... args) throws IOException {
128
ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(args);
129
130
OutputAnalyzer output = new OutputAnalyzer(pb.start());
131
132
log("[STDERR]\n" + output.getStderr());
133
log("[STDOUT]\n" + output.getStdout());
134
135
return output;
136
}
137
138
}
139
140