Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/serviceability/sa/ClhsdbLauncher.java
41152 views
1
/*
2
* Copyright (c) 2017, 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
import java.io.IOException;
25
import java.io.OutputStream;
26
import java.util.List;
27
import java.util.Map;
28
29
import jdk.test.lib.Utils;
30
import jdk.test.lib.JDKToolLauncher;
31
import jdk.test.lib.JDKToolFinder;
32
import jdk.test.lib.process.OutputAnalyzer;
33
import jdk.test.lib.SA.SATestUtils;
34
35
/**
36
* This is a framework to run 'jhsdb clhsdb' commands.
37
* See open/test/hotspot/jtreg/serviceability/sa/ClhsdbLongConstant.java for
38
* an example of how to write a test.
39
*/
40
41
public class ClhsdbLauncher {
42
43
private Process toolProcess;
44
45
public ClhsdbLauncher() {
46
toolProcess = null;
47
}
48
49
/**
50
*
51
* Launches 'jhsdb clhsdb' and attaches to the Lingered App process.
52
* @param lingeredAppPid - pid of the Lingered App or one its sub-classes.
53
*/
54
private void attach(long lingeredAppPid)
55
throws IOException {
56
JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jhsdb");
57
launcher.addVMArgs(Utils.getTestJavaOpts());
58
launcher.addToolArg("clhsdb");
59
if (lingeredAppPid != -1) {
60
launcher.addToolArg("--pid=" + Long.toString(lingeredAppPid));
61
System.out.println("Starting clhsdb against " + lingeredAppPid);
62
}
63
64
ProcessBuilder processBuilder = SATestUtils.createProcessBuilder(launcher);
65
toolProcess = processBuilder.start();
66
}
67
68
/**
69
*
70
* Launches 'jhsdb clhsdb' and loads a core file.
71
* @param coreFileName - Name of the corefile to be loaded.
72
*/
73
private void loadCore(String coreFileName)
74
throws IOException {
75
76
JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jhsdb");
77
launcher.addVMArgs(Utils.getTestJavaOpts());
78
launcher.addToolArg("clhsdb");
79
launcher.addToolArg("--core=" + coreFileName);
80
launcher.addToolArg("--exe=" + JDKToolFinder.getTestJDKTool("java"));
81
System.out.println("Starting clhsdb against corefile " + coreFileName +
82
" and exe " + JDKToolFinder.getTestJDKTool("java"));
83
84
ProcessBuilder processBuilder = new ProcessBuilder(launcher.getCommand());
85
toolProcess = processBuilder.start();
86
}
87
88
/**
89
*
90
* Runs 'jhsdb clhsdb' commands and checks for expected and unexpected strings.
91
* @param commands - clhsdb commands to execute.
92
* @param expectedStrMap - Map of expected strings per command which need to
93
* be checked in the output of the command.
94
* @param unExpectedStrMap - Map of unexpected strings per command which should
95
* not be present in the output of the command.
96
* @return Output of the commands as a String.
97
*/
98
private String runCmd(List<String> commands,
99
Map<String, List<String>> expectedStrMap,
100
Map<String, List<String>> unExpectedStrMap)
101
throws IOException, InterruptedException {
102
String output;
103
104
if (commands == null) {
105
throw new RuntimeException("CLHSDB command must be provided\n");
106
}
107
108
// We want to execute clhsdb "echo" and "verbose" commands before the
109
// requested commands. We can't just issue these commands separately
110
// because code below won't work correctly if all executed commands are
111
// not in the commands list. Since the commands list is immutable, we
112
// need to allocate a mutable one that we can add the extra commands too.
113
List<String> savedCommands = commands;
114
commands = new java.util.LinkedList<String>();
115
116
// Enable echoing of all commands so we see them in the output.
117
commands.add("echo true");
118
119
// Enable verbose exception tracing so we see the full exception backtrace
120
// when there is a failure.
121
commands.add("verbose true");
122
123
// Now add all the original commands after the "echo" and "verbose" commands.
124
commands.addAll(savedCommands);
125
126
try (OutputStream out = toolProcess.getOutputStream()) {
127
for (String cmd : commands) {
128
out.write((cmd + "\n").getBytes());
129
}
130
out.write("quit\n".getBytes());
131
out.flush();
132
}
133
134
OutputAnalyzer oa = new OutputAnalyzer(toolProcess);
135
try {
136
toolProcess.waitFor();
137
} catch (InterruptedException ie) {
138
toolProcess.destroyForcibly();
139
throw new Error("Problem awaiting the child process: " + ie);
140
}
141
142
oa.shouldHaveExitValue(0);
143
output = oa.getOutput();
144
System.out.println("Output: ");
145
System.out.println(output);
146
147
// -Xcheck:jni might be set via TEST_VM_OPTS. Make sure there are no warnings.
148
oa.shouldNotMatch("^WARNING: JNI local refs:.*$");
149
oa.shouldNotMatch("^WARNING in native method:.*$");
150
// This will detect most SA failures, including during the attach.
151
oa.shouldNotMatch("^sun.jvm.hotspot.debugger.DebuggerException:.*$");
152
// This will detect unexpected exceptions, like NPEs and asserts, that are caught
153
// by sun.jvm.hotspot.CommandProcessor.
154
oa.shouldNotMatch("^Error: .*$");
155
156
String[] parts = output.split("hsdb>");
157
for (String cmd : commands) {
158
int index = commands.indexOf(cmd) + 1;
159
OutputAnalyzer out = new OutputAnalyzer(parts[index]);
160
out.shouldNotMatch("Unrecognized command.");
161
162
if (expectedStrMap != null) {
163
List<String> expectedStr = expectedStrMap.get(cmd);
164
if (expectedStr != null) {
165
for (String exp : expectedStr) {
166
out.shouldMatch(exp);
167
}
168
}
169
}
170
171
if (unExpectedStrMap != null) {
172
List<String> unExpectedStr = unExpectedStrMap.get(cmd);
173
if (unExpectedStr != null) {
174
for (String unExp : unExpectedStr) {
175
out.shouldNotMatch(unExp);
176
}
177
}
178
}
179
}
180
return output;
181
}
182
183
/**
184
*
185
* Launches 'jhsdb clhsdb', attaches to the Lingered App, executes the commands,
186
* checks for expected and unexpected strings.
187
* @param lingeredAppPid - pid of the Lingered App or one its sub-classes.
188
* @param commands - clhsdb commands to execute.
189
* @param expectedStrMap - Map of expected strings per command which need to
190
* be checked in the output of the command.
191
* @param unExpectedStrMap - Map of unexpected strings per command which should
192
* not be present in the output of the command.
193
* @return Output of the commands as a String.
194
*/
195
public String run(long lingeredAppPid,
196
List<String> commands,
197
Map<String, List<String>> expectedStrMap,
198
Map<String, List<String>> unExpectedStrMap)
199
throws Exception {
200
201
SATestUtils.skipIfCannotAttach(); // throws SkippedException if attach not expected to work.
202
attach(lingeredAppPid);
203
return runCmd(commands, expectedStrMap, unExpectedStrMap);
204
}
205
206
/**
207
*
208
* Launches 'jhsdb clhsdb', loads a core file, executes the commands,
209
* checks for expected and unexpected strings.
210
* @param coreFileName - Name of the core file to be debugged.
211
* @param commands - clhsdb commands to execute.
212
* @param expectedStrMap - Map of expected strings per command which need to
213
* be checked in the output of the command.
214
* @param unExpectedStrMap - Map of unexpected strings per command which should
215
* not be present in the output of the command.
216
* @return Output of the commands as a String.
217
*/
218
public String runOnCore(String coreFileName,
219
List<String> commands,
220
Map<String, List<String>> expectedStrMap,
221
Map<String, List<String>> unExpectedStrMap)
222
throws IOException, InterruptedException {
223
224
loadCore(coreFileName);
225
return runCmd(commands, expectedStrMap, unExpectedStrMap);
226
}
227
}
228
229