Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/share/jdb/Debuggee.java
41161 views
1
/*
2
* Copyright (c) 2002, 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
24
package nsk.share.jdb;
25
26
import nsk.share.*;
27
import nsk.share.jpda.*;
28
import nsk.share.jdi.ArgumentHandler;
29
30
import java.io.*;
31
32
/**
33
* Interface defining methods to control mirror of debuggee (i.e. debugged VM).
34
*/
35
public interface Debuggee {
36
37
/** Default prefix for log messages. */
38
public static final String LOG_PREFIX = "debuggee> ";
39
public static final String DEBUGEE_STDOUT_LOG_PREFIX = "debuggee.stdout> ";
40
public static final String DEBUGEE_STDERR_LOG_PREFIX = "debuggee.stderr> ";
41
42
/**
43
* Launch debuggee.
44
*
45
* @throws IOException
46
*/
47
public void launch (String[] args) throws IOException;
48
49
/** Return exit status. */
50
public int getStatus ();
51
52
/** Check whether the process has been terminated. */
53
public boolean terminated();
54
55
/** Kill the debuggee VM. */
56
public void killDebuggee ();
57
58
/** Wait until the debuggee VM shutdown or crash. */
59
public int waitForDebuggee () throws InterruptedException;
60
61
/** Get a pipe to write to the debuggee's stdin stream. */
62
public OutputStream getInPipe ();
63
64
/** Get a pipe to read the debuggee's stdout stream. */
65
public InputStream getOutPipe ();
66
67
/** Get a pipe to read the debuggee's stderr stream. */
68
public InputStream getErrPipe ();
69
70
/** Redirect stdout stream to <code>Log</code> */
71
public void redirectStdout(Log log, String prefix);
72
73
/** Redirect stderr stream to <code>Log</code> */
74
public void redirectStderr(Log log, String prefix);
75
}
76
77
/**
78
* Mirror of locally launched debuggee.
79
*/
80
final class LocalLaunchedDebuggee extends LocalProcess implements Debuggee {
81
82
private IORedirector stdoutRedirector = null;
83
private IORedirector stderrRedirector = null;
84
private IORedirector stdinRedirector = null;
85
86
/** Messages prefix. */
87
protected String prefix = LOG_PREFIX;
88
89
/** Launcher that creates this debuggee. */
90
private Launcher launcher = null;
91
92
/** Enwrap the existing <code>VM</code> mirror. */
93
LocalLaunchedDebuggee (Launcher launcher) {
94
super();
95
this.launcher = launcher;
96
}
97
98
/**
99
* Launch debuggee on local host.
100
*/
101
public void launch(String[] args) throws IOException {
102
launcher.display("Starting local debuggee.");
103
104
super.launch(args);
105
redirectStdout(launcher.getLog(), DEBUGEE_STDOUT_LOG_PREFIX );
106
redirectStderr(launcher.getLog(), DEBUGEE_STDERR_LOG_PREFIX);
107
}
108
109
/** Kill the debuggee VM. */
110
public void killDebuggee () {
111
super.kill();
112
if (stdinRedirector != null) {
113
stdinRedirector.cancel();
114
}
115
if (stdoutRedirector != null) {
116
stdoutRedirector.cancel();
117
}
118
if (stderrRedirector != null) {
119
stderrRedirector.cancel();
120
}
121
}
122
123
124
/**
125
* Wait until the debuggee VM shutdown or crash,
126
* and let finish its stdout, stderr, and stdin
127
* redirectors (if any).
128
*
129
* @return Debuggee process exit code.
130
*/
131
public int waitForDebuggee () throws InterruptedException {
132
int timeout = launcher.getJdbArgumentHandler().getWaitTime() * 60 * 1000;
133
int exitCode;
134
try {
135
exitCode = super.waitFor();
136
if (stdinRedirector != null) {
137
if (stdinRedirector.isAlive()) {
138
stdinRedirector.join(timeout);
139
if (stdinRedirector.isAlive()) {
140
launcher.complain("Timeout for waiting STDIN redirector exceeded");
141
stdinRedirector.interrupt();
142
}
143
}
144
stdinRedirector = null;
145
};
146
if (stdoutRedirector != null) {
147
if (stdoutRedirector.isAlive()) {
148
stdoutRedirector.join(timeout);
149
if (stdoutRedirector.isAlive()) {
150
launcher.complain("Timeout for waiting STDOUT redirector exceeded");
151
stdoutRedirector.interrupt();
152
}
153
}
154
stdoutRedirector = null;
155
};
156
if (stderrRedirector != null) {
157
if (stderrRedirector.isAlive()) {
158
stderrRedirector.join(timeout);
159
if (stderrRedirector.isAlive()) {
160
launcher.complain("Timeout for waiting STDERR redirector exceeded");
161
stderrRedirector.interrupt();
162
}
163
}
164
stderrRedirector = null;
165
};
166
} catch (InterruptedException ie) {
167
ie.printStackTrace(launcher.getLog().getOutStream());
168
throw new Failure("Caught exception while waiting for LocalProcess termination: \n\t" + ie);
169
}
170
return exitCode;
171
}
172
173
/**
174
* Get a pipe to write to the debuggee's stdin stream,
175
* or throw TestBug exception is redirected.
176
*/
177
public OutputStream getInPipe () {
178
if (stdinRedirector != null)
179
throw new TestBug("debuggee's stdin is redirected");
180
return getStdin();
181
}
182
183
/**
184
* Get a pipe to read the debuggee's stdout stream,
185
* or throw TestBug exception is redirected.
186
*/
187
public InputStream getOutPipe () {
188
if (stdoutRedirector != null)
189
throw new TestBug("debuggee's stdout is redirected");
190
return getStdout();
191
}
192
193
/**
194
* Get a pipe to read the debuggee's stderr stream,
195
* or throw TestBug exception is redirected.
196
*/
197
public InputStream getErrPipe () {
198
if (stderrRedirector != null)
199
throw new TestBug("debuggee's stderr is redirected");
200
return getStderr();
201
}
202
203
// --------------------------------------------------- //
204
205
/**
206
* Start thread redirecting the debuggee's stdout to the
207
* given <code>Log</code>. If the debuggee's stdout
208
* was already redirected, the TestBug exception is thrown.
209
*
210
* @throws nsk.share.TestBug
211
*/
212
public void redirectStdout(Log log, String prefix) {
213
if (stdoutRedirector != null) {
214
throw new TestBug("Debuggee's stdout already redirected.");
215
}
216
stdoutRedirector = new IORedirector(new BufferedReader(new InputStreamReader(getStdout())), log, prefix);
217
stdoutRedirector.start();
218
}
219
220
221
/**
222
* Start thread redirecting the debuggee's stderr to the
223
* given <code>Log</code>. If the debuggee's stderr
224
* was already redirected, the TestBug exception is thrown.
225
*
226
* @throws nsk.share.TestBug
227
*/
228
public void redirectStderr(Log log, String prefix) {
229
if (stderrRedirector != null) {
230
throw new TestBug("Debuggee's stdout already redirected.");
231
}
232
stderrRedirector = new IORedirector(new BufferedReader(new InputStreamReader(getStderr())), log, prefix);
233
stderrRedirector.start();
234
}
235
}
236
237
238
/**
239
* Mirror of remotely launched debuggee.
240
*/
241
final class RemoteLaunchedDebuggee implements Debuggee {
242
243
/** Launcher that creates this debuggee. */
244
private Launcher launcher = null;
245
246
/** Enwrap the existing <code>VM</code> mirror. */
247
RemoteLaunchedDebuggee (Launcher launcher) {
248
super();
249
this.launcher = launcher;
250
}
251
252
/**
253
* Launch debugee on remote host via <code>Launcher</code> object.
254
*/
255
public void launch(String[] args) throws IOException {
256
String cmdLine = ArgumentHandler.joinArguments(args, "\"");
257
launcher.display("Starting remote java process:\n" + cmdLine);
258
launcher.launchRemoteProcess(args);
259
}
260
261
/** Return exit status of the debuggee VM. */
262
public int getStatus () {
263
return launcher.getRemoteProcessStatus();
264
}
265
266
/** Check whether the debuggee VM has been terminated. */
267
public boolean terminated () {
268
return launcher.isRemoteProcessTerminated();
269
}
270
271
// ---------------------------------------------- //
272
273
/** Kill the debuggee VM. */
274
public void killDebuggee () {
275
launcher.killRemoteProcess();
276
}
277
278
/** Wait until the debuggee VM shutdown or crash. */
279
public int waitForDebuggee () {
280
return launcher.waitForRemoteProcess();
281
}
282
283
/** Get a pipe to write to the debuggee's stdin stream. */
284
public OutputStream getInPipe () {
285
return null;
286
}
287
288
/** Get a pipe to read the debuggee's stdout stream. */
289
public InputStream getOutPipe () {
290
return null;
291
}
292
293
/** Get a pipe to read the debuggee's stderr stream. */
294
public InputStream getErrPipe () {
295
return null;
296
}
297
298
public void redirectStdout(Log log, String prefix) {
299
}
300
301
public void redirectStderr(Log log, String prefix) {
302
}
303
304
}
305
306