Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/serviceability/dcmd/jvmti/LoadAgentDcmdTest.java
41153 views
1
/*
2
* Copyright (c) 2016, 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
import java.io.*;
24
import java.nio.file.*;
25
import java.util.Arrays;
26
import java.util.jar.Attributes;
27
import java.util.jar.JarEntry;
28
import java.util.jar.JarOutputStream;
29
import java.util.jar.Manifest;
30
import java.util.List;
31
import java.util.regex.Matcher;
32
import java.util.regex.Pattern;
33
import jdk.test.lib.Platform;
34
import jdk.test.lib.process.OutputAnalyzer;
35
import jdk.test.lib.dcmd.*;
36
import org.testng.annotations.Test;
37
38
/*
39
* Test to attach JVMTI java agent.
40
*
41
* @test
42
* @bug 8147388
43
* @library /test/lib
44
* @modules java.base/jdk.internal.misc
45
* java.compiler
46
* java.instrument
47
* java.management
48
* jdk.internal.jvmstat/sun.jvmstat.monitor
49
* @build SimpleJvmtiAgent
50
* @run driver jdk.test.lib.helpers.ClassFileInstaller SimpleJvmtiAgent
51
* @run testng/othervm LoadAgentDcmdTest
52
*/
53
public class LoadAgentDcmdTest {
54
55
public String getLibInstrumentPath() throws FileNotFoundException {
56
String jdkPath = System.getProperty("test.jdk");
57
58
if (jdkPath == null) {
59
throw new RuntimeException(
60
"System property 'test.jdk' not set. " +
61
"This property is normally set by jtreg. " +
62
"When running test separately, set this property using " +
63
"'-Dtest.jdk=/path/to/jdk'.");
64
}
65
66
Path libpath = Paths.get(jdkPath, jdkLibPath(), sharedObjectName("instrument"));
67
68
if (!libpath.toFile().exists()) {
69
throw new FileNotFoundException(
70
"Could not find " + libpath.toAbsolutePath());
71
}
72
73
return libpath.toAbsolutePath().toString();
74
}
75
76
77
public void createJarFileForAgent()
78
throws IOException {
79
80
final String jarName = "agent.jar";
81
final String agentClass = "SimpleJvmtiAgent";
82
83
Manifest manifest = new Manifest();
84
85
manifest.getMainAttributes().put(
86
Attributes.Name.MANIFEST_VERSION, "1.0");
87
88
manifest.getMainAttributes().put(
89
new Attributes.Name("Agent-Class"), agentClass);
90
91
JarOutputStream target = null;
92
93
try {
94
target = new
95
JarOutputStream(new FileOutputStream(jarName), manifest);
96
JarEntry entry = new JarEntry(agentClass + ".class");
97
target.putNextEntry(entry);
98
target.closeEntry();
99
} finally {
100
target.close();
101
}
102
}
103
104
static void checkWarningsOnly(OutputAnalyzer out) {
105
// stderr should be empty except for VM warnings.
106
if (!out.getStderr().isEmpty()) {
107
List<String> lines = Arrays.asList(out.getStderr().split("(\\r\\n|\\n|\\r)"));
108
Pattern p = Pattern.compile(".*VM warning.*");
109
for (String line : lines) {
110
Matcher m = p.matcher(line);
111
if (!m.matches()) {
112
throw new RuntimeException("Stderr has output other than VM warnings");
113
}
114
}
115
}
116
}
117
118
public void run(CommandExecutor executor) {
119
try{
120
121
createJarFileForAgent();
122
123
String libpath = getLibInstrumentPath();
124
OutputAnalyzer output = null;
125
126
// Test 1: Native agent, no arguments
127
output = executor.execute("JVMTI.agent_load " +
128
libpath + " agent.jar");
129
checkWarningsOnly(output);
130
131
// Test 2: Native agent, with arguments
132
output = executor.execute("JVMTI.agent_load " +
133
libpath + " \"agent.jar=foo=bar\"");
134
checkWarningsOnly(output);
135
136
// Test 3: Java agent, no arguments
137
output = executor.execute("JVMTI.agent_load " +
138
"agent.jar");
139
checkWarningsOnly(output);
140
141
// Test 4: Java agent, with arguments
142
output = executor.execute("JVMTI.agent_load " +
143
"\"agent.jar=foo=bar\"");
144
checkWarningsOnly(output);
145
146
} catch (Exception e) {
147
throw new RuntimeException(e);
148
}
149
}
150
/**
151
* return path to library inside jdk tree
152
*/
153
public static String jdkLibPath() {
154
if (Platform.isWindows()) {
155
return "bin";
156
}
157
return "lib";
158
}
159
160
/**
161
* Build name of shared object according to platform rules
162
*/
163
public static String sharedObjectName(String name) {
164
if (Platform.isWindows()) {
165
return name + ".dll";
166
}
167
if (Platform.isOSX()) {
168
return "lib" + name + ".dylib";
169
}
170
return "lib" + name + ".so";
171
}
172
173
@Test
174
public void jmx() throws Throwable {
175
run(new JMXExecutor());
176
}
177
178
@Test
179
public void cli() throws Throwable {
180
run(new PidJcmdExecutor());
181
}
182
}
183
184