File: Debuggee.java

package info (click to toggle)
openjdk-11 11.0.16%2B8-1~deb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 784,576 kB
  • sloc: java: 5,128,021; xml: 1,192,224; cpp: 1,124,021; ansic: 422,433; javascript: 155,577; sh: 17,084; objc: 13,327; python: 4,522; asm: 3,570; makefile: 2,858; awk: 351; sed: 172; perl: 114; jsp: 24; csh: 3
file content (304 lines) | stat: -rw-r--r-- 9,788 bytes parent folder | download | duplicates (11)
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
 * Copyright (c) 2002, 2018, 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.
 */

package nsk.share.jdb;

import nsk.share.*;
import nsk.share.jpda.*;
import nsk.share.jdi.ArgumentHandler;

import java.io.*;

/**
 * Interface defining methods to control mirror of debuggee (i.e. debugged VM).
 */
public interface Debuggee {

    /** Default prefix for log messages. */
    public static final String LOG_PREFIX = "debuggee> ";
    public static final String DEBUGEE_STDOUT_LOG_PREFIX = "debuggee.stdout> ";
    public static final String DEBUGEE_STDERR_LOG_PREFIX = "debuggee.stderr> ";

    /**
     * Launch debuggee.
     *
     * @throws IOException
     */
    public void launch (String[] args) throws IOException;

    /** Return exit status. */
    public int getStatus ();

    /** Check whether the process has been terminated. */
    public boolean terminated();

    /** Kill the debuggee VM. */
    public void killDebuggee ();

    /** Wait until the debuggee VM shutdown or crash. */
    public int waitForDebuggee () throws InterruptedException;

    /** Get a pipe to write to the debuggee's stdin stream. */
    public OutputStream getInPipe ();

    /** Get a pipe to read the debuggee's stdout stream. */
    public InputStream getOutPipe ();

    /** Get a pipe to read the debuggee's stderr stream. */
    public InputStream getErrPipe ();

    /** Redirect stdout stream to <code>Log</code> */
    public void redirectStdout(Log log, String prefix);

    /** Redirect stderr stream to <code>Log</code> */
    public void redirectStderr(Log log, String prefix);
}

/**
 * Mirror of locally launched debuggee.
 */
final class LocalLaunchedDebuggee extends LocalProcess implements Debuggee {

    private IORedirector stdoutRedirector = null;
    private IORedirector stderrRedirector = null;
    private IORedirector stdinRedirector = null;

    /** Messages prefix. */
    protected String prefix = LOG_PREFIX;

    /** Launcher that creates this debuggee. */
    private Launcher launcher = null;

    /** Enwrap the existing <code>VM</code> mirror. */
    LocalLaunchedDebuggee (Launcher launcher) {
        super();
        this.launcher = launcher;
    }

    /**
     * Launch debuggee on local host.
     */
    public void launch(String[] args) throws IOException {
        launcher.display("Starting local debuggee.");

        super.launch(args);
        redirectStdout(launcher.getLog(), DEBUGEE_STDOUT_LOG_PREFIX );
        redirectStderr(launcher.getLog(), DEBUGEE_STDERR_LOG_PREFIX);
    }

    /** Kill the debuggee VM. */
    public void killDebuggee () {
        super.kill();
        if (stdinRedirector != null) {
           stdinRedirector.cancel();
        }
        if (stdoutRedirector != null) {
           stdoutRedirector.cancel();
        }
        if (stderrRedirector != null) {
           stderrRedirector.cancel();
        }
    }


    /**
     * Wait until the debuggee VM shutdown or crash,
     * and let finish its stdout, stderr, and stdin
     * redirectors (if any).
     *
     * @return  Debuggee process exit code.
     */
    public int waitForDebuggee () throws InterruptedException {
        int timeout = launcher.getJdbArgumentHandler().getWaitTime() * 60 * 1000;
        int exitCode;
        try {
            exitCode = super.waitFor();
            if (stdinRedirector != null) {
                if (stdinRedirector.isAlive()) {
                    stdinRedirector.join(timeout);
                    if (stdinRedirector.isAlive()) {
                        launcher.complain("Timeout for waiting STDIN redirector exceeded");
                        stdinRedirector.interrupt();
                    }
                }
                stdinRedirector = null;
            };
            if (stdoutRedirector != null) {
                if (stdoutRedirector.isAlive()) {
                    stdoutRedirector.join(timeout);
                    if (stdoutRedirector.isAlive()) {
                        launcher.complain("Timeout for waiting STDOUT redirector exceeded");
                        stdoutRedirector.interrupt();
                    }
                }
                stdoutRedirector = null;
            };
            if (stderrRedirector != null) {
                if (stderrRedirector.isAlive()) {
                    stderrRedirector.join(timeout);
                    if (stderrRedirector.isAlive()) {
                        launcher.complain("Timeout for waiting STDERR redirector exceeded");
                        stderrRedirector.interrupt();
                    }
                }
                stderrRedirector = null;
            };
        } catch (InterruptedException ie) {
            ie.printStackTrace(launcher.getLog().getOutStream());
            throw new Failure("Caught exception while waiting for LocalProcess termination: \n\t" + ie);
        }
        return exitCode;
    }

    /**
     * Get a pipe to write to the debuggee's stdin stream,
     * or throw TestBug exception is redirected.
     */
    public OutputStream getInPipe () {
        if (stdinRedirector != null)
            throw new TestBug("debuggee's stdin is redirected");
        return getStdin();
    }

    /**
     * Get a pipe to read the debuggee's stdout stream,
     * or throw TestBug exception is redirected.
     */
    public InputStream getOutPipe () {
        if (stdoutRedirector != null)
            throw new TestBug("debuggee's stdout is redirected");
        return getStdout();
    }

    /**
     * Get a pipe to read the debuggee's stderr stream,
     * or throw TestBug exception is redirected.
     */
    public InputStream getErrPipe () {
        if (stderrRedirector != null)
            throw new TestBug("debuggee's stderr is redirected");
        return getStderr();
    }

    // --------------------------------------------------- //

    /**
     * Start thread redirecting the debuggee's stdout to the
     * given <code>Log</code>. If the debuggee's stdout
     * was already redirected, the TestBug exception is thrown.
     *
     * @throws nsk.share.TestBug
     */
    public void redirectStdout(Log log, String prefix) {
        if (stdoutRedirector != null) {
            throw new TestBug("Debuggee's stdout already redirected.");
        }
        stdoutRedirector = new IORedirector(new BufferedReader(new InputStreamReader(getStdout())), log, prefix);
        stdoutRedirector.start();
    }


    /**
     * Start thread redirecting the debuggee's stderr to the
     * given <code>Log</code>. If the debuggee's stderr
     * was already redirected, the TestBug exception is thrown.
     *
     * @throws nsk.share.TestBug
     */
    public void redirectStderr(Log log, String prefix) {
        if (stderrRedirector != null) {
            throw new TestBug("Debuggee's stdout already redirected.");
        }
        stderrRedirector = new IORedirector(new BufferedReader(new InputStreamReader(getStderr())), log, prefix);
        stderrRedirector.start();
    }
}


/**
 * Mirror of remotely launched debuggee.
 */
final class RemoteLaunchedDebuggee implements Debuggee {

    /** Launcher that creates this debuggee. */
    private Launcher launcher = null;

    /** Enwrap the existing <code>VM</code> mirror. */
    RemoteLaunchedDebuggee (Launcher launcher) {
        super();
        this.launcher = launcher;
    }

    /**
     * Launch debugee on remote host via <code>Launcher</code> object.
     */
    public void launch(String[] args) throws IOException {
        String cmdLine = ArgumentHandler.joinArguments(args, "\"");
        launcher.display("Starting remote java process:\n" + cmdLine);
        launcher.launchRemoteProcess(args);
    }

    /** Return exit status of the debuggee VM. */
    public int getStatus () {
        return launcher.getRemoteProcessStatus();
    }

    /** Check whether the debuggee VM has been terminated. */
    public boolean terminated () {
        return launcher.isRemoteProcessTerminated();
    }

    // ---------------------------------------------- //

    /** Kill the debuggee VM. */
    public void killDebuggee () {
        launcher.killRemoteProcess();
    }

    /** Wait until the debuggee VM shutdown or crash. */
    public int waitForDebuggee () {
        return launcher.waitForRemoteProcess();
    }

    /** Get a pipe to write to the debuggee's stdin stream. */
    public OutputStream getInPipe () {
        return null;
    }

    /** Get a pipe to read the debuggee's stdout stream. */
    public InputStream getOutPipe () {
        return null;
    }

    /** Get a pipe to read the debuggee's stderr stream. */
    public InputStream getErrPipe () {
        return null;
    }

    public void redirectStdout(Log log, String prefix) {
    }

    public void redirectStderr(Log log, String prefix) {
    }

}