Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/share/jdi/ConnectorTest.java
41161 views
1
/*
2
* Copyright (c) 2006, 2018, 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
package nsk.share.jdi;
24
25
import com.sun.jdi.connect.*;
26
import com.sun.jdi.*;
27
import java.io.*;
28
import java.util.*;
29
30
import nsk.share.*;
31
import nsk.share.jpda.*;
32
33
/*
34
* This class contains several common methods used by connector tests.
35
*/
36
public abstract class ConnectorTest {
37
protected Log log;
38
39
protected VirtualMachine vm;
40
41
protected int attempts; // attempts to connect to the debuggee VM
42
43
protected int delay; // delay between connection attempts
44
45
protected IORedirector outRedirector;
46
47
protected IORedirector errRedirector;
48
49
protected ArgHandler argHandler;
50
51
protected boolean isTestFailed;
52
53
// set test status 'FAILED'
54
protected void testFailed() {
55
isTestFailed = true;
56
}
57
58
// check if tested functionality implemented on current platform
59
protected boolean shouldPass() {
60
return argHandler.shouldPass(getConnectorName());
61
}
62
63
abstract protected void doTest();
64
65
abstract protected String getConnectorName();
66
67
abstract protected String getDebuggeeClass();
68
69
static public class ArgHandler extends ArgumentHandler {
70
public ArgHandler(String[] args) {
71
super(args);
72
73
}
74
75
protected boolean checkOption(String option, String value) {
76
if (super.checkOption(option, value))
77
return true;
78
79
if (option.equals("testWorkDir"))
80
return true;
81
82
if (option.equals("waitVMStartEvent"))
83
return true;
84
85
return false;
86
}
87
88
public String getTestWorkDir() {
89
String dir = options.getProperty("testWorkDir");
90
91
if (dir.endsWith(File.separator)) {
92
dir = dir.substring(0, dir.length() - 1);
93
}
94
95
return dir;
96
}
97
98
public boolean waitVMStartEvent() {
99
return options.containsKey("waitVMStartEvent");
100
}
101
}
102
103
/*
104
* Subclasses can provide another ArgumentHandlers
105
*/
106
protected ArgHandler createArgumentHandler(String[] args) {
107
return new ArgHandler(args);
108
}
109
110
protected void init(String[] args, PrintStream out) {
111
argHandler = createArgumentHandler(args);
112
113
log = new Log(out, argHandler);
114
115
delay = argHandler.getConnectionDelay();
116
117
// calculate number of connection attempts to not exceed WAITTIME
118
long timeout = argHandler.getWaitTime() * 60 * 1000;
119
attempts = (int) (timeout / delay);
120
}
121
122
protected int runIt(String argv[], PrintStream out) {
123
try {
124
init(argv, out);
125
126
if (shouldPass()) {
127
log.display("Tested functionality isn't implemented on this platform. Treat test as passed.");
128
return Consts.TEST_PASSED;
129
}
130
131
doTest();
132
133
if (isTestFailed)
134
return Consts.TEST_FAILED;
135
else
136
return Consts.TEST_PASSED;
137
138
} catch (Throwable t) {
139
out.println("Unexpected exception: " + t);
140
t.printStackTrace(out);
141
return Consts.TEST_FAILED;
142
}
143
}
144
145
protected void waitForVMInit(VirtualMachine vm) {
146
Debugee.waitForVMInit(vm, log, argHandler.getWaitTime() * 60 * 1000);
147
}
148
149
// set connector argument value with given name
150
protected void setConnectorArg(Map<String, Connector.Argument> args, String argName, String value) {
151
for (String key : args.keySet()) {
152
Connector.Argument arg = args.get(key);
153
if (arg.name().equals(argName)) {
154
arg.setValue(value);
155
return;
156
}
157
}
158
159
throw new Error("There is no argument '" + argName + "'");
160
}
161
162
// try attach to target VM using attaching connector
163
protected VirtualMachine tryAttach(AttachingConnector connector, Map<String, Connector.Argument> cArgs) {
164
// make several attempts to connect to the debuggee VM until WAITTIME exceeds
165
for (int i = 0; i < attempts; i++) {
166
try {
167
return connector.attach(cArgs);
168
} catch (IOException e) {
169
// could not connect; sleep a few and make new attempt
170
log.display("Connection attempt #" + i + " failed: " + e);
171
e.printStackTrace(log.getOutStream());
172
try {
173
Thread.sleep(delay);
174
} catch (InterruptedException ie) {
175
testFailed();
176
log.complain("TEST INCOMPLETE: interrupted sleep: " + ie);
177
ie.printStackTrace(log.getOutStream());
178
}
179
} catch (IllegalConnectorArgumentsException e) {
180
testFailed();
181
log.complain("TEST: Illegal connector arguments: " + e.getMessage());
182
return null;
183
} catch (Exception e) {
184
testFailed();
185
log.complain("TEST: Internal error: " + e.getMessage());
186
e.printStackTrace(log.getOutStream());
187
return null;
188
}
189
}
190
191
testFailed();
192
// return null after all attempts failed
193
log.complain("FAILURE: all attempts to connect to the debuggee VM failed");
194
return null;
195
}
196
197
// try find connector with given name
198
protected Connector findConnector(String connectorName) {
199
List<Connector> connectors = Bootstrap.virtualMachineManager().allConnectors();
200
Iterator<Connector> iter = connectors.iterator();
201
202
while (iter.hasNext()) {
203
Connector connector = (Connector) iter.next();
204
if (connector.name().equals(connectorName)) {
205
log.display("Connector name=" + connector.name() + "\n\tdescription=" + connector.description() + "\n\ttransport="
206
+ connector.transport().name());
207
return connector;
208
}
209
}
210
throw new Error("No appropriate connector");
211
}
212
213
// wait when debuggee process finishes and check exit code
214
protected void waitDebuggeeExit(Debugee debuggee) {
215
log.display("\nwaiting for debuggee VM exit");
216
int code = debuggee.waitFor();
217
if (code != (Consts.JCK_STATUS_BASE + Consts.TEST_PASSED)) {
218
testFailed();
219
log.complain("Debuggee VM has crashed: exit code=" + code);
220
return;
221
}
222
log.display("debuggee VM: exit code=" + code);
223
}
224
225
// wait 'READY' command from debuggee VM (this method is used by debuggers establishing socket connection with debuggee VM)
226
protected boolean waitReadyCommand(IOPipe pipe) {
227
String command = pipe.readln();
228
log.display("Command: " + command);
229
230
if (!command.equals(AbstractDebuggeeTest.COMMAND_READY)) {
231
testFailed();
232
log.complain("Unexpected debuggee answer: " + command + ", expected is " + AbstractDebuggeeTest.COMMAND_READY);
233
return false;
234
}
235
236
return true;
237
}
238
}
239
240