-
Notifications
You must be signed in to change notification settings - Fork 27
/
CanGenerateAllClassHook.java
138 lines (120 loc) · 5.32 KB
/
CanGenerateAllClassHook.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* @test
* @bug 8161605
* @summary Tests that jvmtiEnv::GetPotentialCapabilities reports
* can_generate_all_class_hook_events capability with CDS (-Xshare:on)
* at ONLOAD and LIVE phases
* @requires vm.jvmti
* @requires vm.cds
* @requires vm.flagless
* @library /test/lib
* @compile CanGenerateAllClassHook.java
* @run main/othervm/native CanGenerateAllClassHook
*/
import jdk.test.lib.cds.CDSTestUtils;
import jdk.test.lib.process.OutputAnalyzer;
import jdk.test.lib.process.ProcessTools;
import java.io.File;
import java.io.IOException;
/*
* The simplest way to test is to use system classes.jsa,
* but we cannot rely on tested JRE/JDK has it.
* So the test runs 2 java processes -
* 1st to generate custom shared archive file:
* java -XX:+UnlockDiagnosticVMOptions -XX:SharedArchiveFile=<jsa_file> -Xshare:dump
* and 2nd to perform the actual testing using generated shared archive:
* java -XX:+UnlockDiagnosticVMOptions -XX:SharedArchiveFile=<jsa_file> -Xshare:on
* -agentlib:<agent> CanGenerateAllClassHook
*/
public class CanGenerateAllClassHook {
private static final String agentLib = "CanGenerateAllClassHook";
private static native int getClassHookAvail();
private static native int getOnLoadClassHookAvail();
public static void main(String[] args) throws Exception {
if (args.length == 0) {
// this is master run
final File jsaFile = File.createTempFile(agentLib, ".jsa");
jsaFile.deleteOnExit();
final String jsaPath = jsaFile.getAbsolutePath();
log("generating CDS archive...");
execJava(
"-XX:+UnlockDiagnosticVMOptions",
"-XX:SharedArchiveFile=" + jsaPath,
"-Xshare:dump")
.shouldHaveExitValue(0);
log("CDS generation completed.");
OutputAnalyzer output = execJava(
"-XX:+UnlockDiagnosticVMOptions",
"-XX:SharedArchiveFile=" + jsaPath,
"-Xshare:on",
"-agentlib:" + agentLib,
// copy java.library.path
"-Djava.library.path=" + System.getProperty("java.library.path"),
// specify "-showversion" to ensure the test runs in shared mode
"-showversion",
// class to run
CanGenerateAllClassHook.class.getCanonicalName(),
// and arg
"test");
// Xshare:on can cause intermittent failure
// checkExec handles this.
CDSTestUtils.checkExec(output);
log("Test PASSED.");
} else {
// this is test run
try {
System.loadLibrary(agentLib);
} catch (UnsatisfiedLinkError ex) {
System.err.println("Failed to load " + agentLib + " lib");
System.err.println("java.library.path: " + System.getProperty("java.library.path"));
throw ex;
}
final int onLoadValue = getOnLoadClassHookAvail();
final int liveValue = getClassHookAvail();
// Possible values returned:
// 1 - the capability is supported;
// 0 - the capability is not supported;
// -1 - error occured.
log("can_generate_all_class_hook_events value capability:");
log("ONLOAD phase: " + (onLoadValue < 0 ? "Failed to read" : onLoadValue));
log("LIVE phase: " + (liveValue < 0 ? "Failed to read" : liveValue));
if (onLoadValue != 1 || liveValue != 1) {
throw new RuntimeException("The can_generate_all_class_hook_events capability "
+ " is expected to be available in both ONLOAD and LIVE phases");
}
}
}
private static void log(String msg) {
System.out.println(msg);
System.out.flush();
}
private static OutputAnalyzer execJava(String... args) throws IOException {
ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(args);
OutputAnalyzer output = new OutputAnalyzer(pb.start());
log("[STDERR]\n" + output.getStderr());
log("[STDOUT]\n" + output.getStdout());
return output;
}
}