File: Launcher.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 (386 lines) | stat: -rw-r--r-- 13,508 bytes parent folder | download | duplicates (7)
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
 * 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.*;
import java.util.*;

/**
 * This class provides launching of <code>jdb</code> and debuggee in local
 * or remote mode according to test command line options.
 */

public class Launcher extends DebugeeBinder {

    /* Delay in milliseconds after launching jdb.*/
    static final long DEBUGGEE_START_DELAY = 5 * 1000;

    protected static Jdb jdb;

    protected static Debuggee debuggee;

    /** Pattern for message of jdb has started. */
    protected static String JDB_STARTED = "Initializing jdb";

    /**
     * Get version string.
     */
    public static String getVersion () {
        return "@(#)Launcher.java %I% %E%";
    }

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

    /**
     * Handler of command line arguments.
     */
    protected static JdbArgumentHandler argumentHandler = null;

    /**
     * Return <code>argumentHandler</code> of this binder.
     */
    public static JdbArgumentHandler getJdbArgumentHandler() {
        return argumentHandler;
    }

    /**
     * Return <code>jdb</code> mirror of this binder.
     */
    public static Jdb getJdb() {
        return jdb;
    }

    /**
     * Return debuggee mirror of this binder.
     */
    public static Debuggee getDebuggee() {
        return debuggee;
    }

    /**
     * Incarnate new Launcher obeying the given
     * <code>argumentHandler</code>; and assign the given
     * <code>log</code>.
     */
    public Launcher (JdbArgumentHandler argumentHandler, Log log) {
        super(argumentHandler, log);
        setLogPrefix("launcher > ");
        this.argumentHandler = argumentHandler;
    }

    /**
     * Defines mode (local or remote) and type of connector (default, launching,
     * raw launching, attaching or listening) according to options
     * parsed by <code>JdbArgumentHandler</code>. And then launches <code>jdb</code>
     * and debuggee in defined mode.
     */
    public void launchJdbAndDebuggee (String classToExecute) throws IOException {

        String[] jdbCmdArgs = makeJdbCmdLine(classToExecute);

        if (argumentHandler.isLaunchedLocally()) {

            if (argumentHandler.isDefaultConnector()) {

                localDefaultLaunch(jdbCmdArgs, classToExecute);

            } else if (argumentHandler.isRawLaunchingConnector()) {

                localRawLaunch(jdbCmdArgs, classToExecute);

            } else if (argumentHandler.isLaunchingConnector()) {

                localLaunch(jdbCmdArgs, classToExecute);

            } else if (argumentHandler.isAttachingConnector()) {

                localLaunchAndAttach(jdbCmdArgs, classToExecute);

            } else if (argumentHandler.isListeningConnector()) {

                localLaunchAndListen(jdbCmdArgs, classToExecute);

            } else {
                throw new TestBug("Unexpected connector type for local launch mode"
                                  + argumentHandler.getConnectorType());
            }

        } else if (argumentHandler.isLaunchedRemotely()) {

            connectToBindServer(classToExecute);

            if (argumentHandler.isAttachingConnector()) {

                remoteLaunchAndAttach(jdbCmdArgs, classToExecute);

            } else if (argumentHandler.isListeningConnector()) {

                remoteLaunchAndListen(jdbCmdArgs, classToExecute);

            } else {
                throw new TestBug("Unexpected connector type for remote launch mode"
                                  + argumentHandler.getConnectorType());
            }
        } else {
            throw new Failure("Unexpected launching mode: " + argumentHandler.getLaunchMode());
        }
    }

    /**
     * Creates String array to launch <code>jdb</code> according to options
     * parsed by <code>JdbArgumentHandler</code>.
     */
    private String[] makeJdbCmdLine (String classToExecute) {

        Vector<String> args = new Vector<String>();

        String jdbExecPath = argumentHandler.getJdbExecPath();
        args.add(jdbExecPath.trim());
        args.addAll(argumentHandler.enwrapJavaOptions(argumentHandler.getJavaOptions()));

        String jdbOptions = argumentHandler.getJdbOptions();
        if (jdbOptions.trim().length() > 0) {
            StringTokenizer tokenizer = new StringTokenizer(jdbOptions);
            while (tokenizer.hasMoreTokens()) {
                String option = tokenizer.nextToken();
                args.add(option);
            }
        }
        if (classToExecute == null)
            return args.toArray(new String[args.size()]);
        args.add("-connect");
        StringBuffer connect = new StringBuffer();

        if (argumentHandler.isLaunchingConnector()) {

// Do not need to use quote symbol.
//            String quote = '\"';
//            connect.append(quote + argumentHandler.getConnectorName() + ":");
            connect.append(argumentHandler.getConnectorName() + ":");

            String connectorAddress;
            String vmAddress = makeTransportAddress();;

            if (argumentHandler.isRawLaunchingConnector()) {

                if (argumentHandler.isSocketTransport()) {
                    if (argumentHandler.isLaunchedLocally()) {
                        connectorAddress = argumentHandler.getTransportPort();
                    } else {
                        connectorAddress = argumentHandler.getDebugeeHost() + ":" + argumentHandler.getTransportPort();
                    }
                } else if (argumentHandler.isShmemTransport() ) {
                    connectorAddress = argumentHandler.getTransportSharedName();
                } else {
                    throw new TestBug("Launcher: Undefined transport type for RawLaunchingConnector");
                }

                connect.append("address=" + connectorAddress.trim());
                connect.append(",command=" + makeCommandLineString(classToExecute, vmAddress, " ").trim());

            } else /* LaunchingConnector or DefaultConnector */ {

                connect.append("vmexec=" + argumentHandler.getLaunchExecName().trim());
                String debuggeeOpts = argumentHandler.getDebuggeeOptions();
                if (debuggeeOpts.trim().length() > 0) {
                    //connect.append(",options=" + debuggeeOpts.trim());
                    connect.append(",options=");
                    for (String arg : debuggeeOpts.split("\\s+")) {
                       connect.append(" \"");
                       connect.append(arg);
                       connect.append("\"");
                    }
                }
                String cmdline = classToExecute + " " + ArgumentHandler.joinArguments(argumentHandler.getArguments(), " ");
                connect.append(",main=" + cmdline.trim());

            }

//            connect.append(quote);

        } else {

            connect.append(argumentHandler.getConnectorName() + ":");

            if (argumentHandler.isAttachingConnector()) {

                if (argumentHandler.isSocketTransport()) {
                    connect.append("port=" + argumentHandler.getTransportPort().trim());
                    if (argumentHandler.isLaunchedRemotely())
                        connect.append(",hostname=" + argumentHandler.getDebugeeHost().trim());
                } else if (argumentHandler.isShmemTransport()) {
                    connect.append("name=" + argumentHandler.getTransportSharedName().trim());
                } else {
                    throw new TestBug("Launcher: Undefined transport type for AttachingConnector");
                }


            } else if (argumentHandler.isListeningConnector()) {

                if (!argumentHandler.isTransportAddressDynamic()) {
                    if (argumentHandler.isSocketTransport()) {
                        connect.append("port=" + argumentHandler.getTransportPort().trim());
                    } else if (argumentHandler.isShmemTransport()) {
                        connect.append("name=" + argumentHandler.getTransportSharedName().trim());
                    } else {
                        throw new TestBug("Launcher: Undefined transport type for AttachingConnector");
                    }
                }

            } else {
                throw new TestBug("Launcher: Undefined connector type");
            }

        }

        args.add(connect.toString().trim());

        String[] argsArray = new String[args.size()];
        for (int i = 0; i < args.size(); i++) {
            argsArray[i] = (String) args.elementAt(i);
        }

        return argsArray;
    }

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

    /**
     * Run test in local mode using default connector.
     */
    private void localDefaultLaunch
       (String[] jdbCmdArgs, String classToExecute) throws IOException {
        localLaunch(jdbCmdArgs, classToExecute);
    }

    /**
     * Run test in local mode using raw launching connector.
     */
    private void localRawLaunch
       (String[] jdbCmdArgs, String classToExecute) throws IOException {
        localLaunch(jdbCmdArgs, classToExecute);
    }

    /**
     * Run test in local mode using launching connector.
     */
    private void localLaunch
       (String[] jdbCmdArgs, String classToExecute) throws IOException {

        jdb = new Jdb(this);
        display("Starting jdb launching local debuggee");
        jdb.launch(jdbCmdArgs);

        if (classToExecute != null)
            jdb.waitForMessage(0, JDB_STARTED);
//        jdb.waitForPrompt(0, false);

    }

    /**
     * Run test in local mode using attaching connector.
     */
    private void localLaunchAndAttach
       (String[] jdbCmdArgs, String classToExecute) throws IOException {

        debuggee = new LocalLaunchedDebuggee(this);
        String address = makeTransportAddress();
        String[] javaCmdArgs = makeCommandLineArgs(classToExecute, address);
        debuggee.launch(javaCmdArgs);

        display("Start jdb attaching to local debuggee");
        jdb = Jdb.startAttachingJdb (this, jdbCmdArgs, JDB_STARTED);
//        jdb.waitForPrompt(0, false);
    }

    /**
     * Run test in local mode using listening connector.
     */
    private void localLaunchAndListen
       (String[] jdbCmdArgs, String classToExecute) throws IOException {

        jdb = new Jdb(this);
        display("Starting jdb listening to local debuggee");
        jdb.launch(jdbCmdArgs);
        String address = jdb.waitForListeningJdb();
        display("Listening address found: " + address);

        debuggee = new LocalLaunchedDebuggee(this);
        String[] javaCmdArgs = makeCommandLineArgs(classToExecute, address);
        debuggee.launch(javaCmdArgs);

//        jdb.waitForPrompt(0, false);
    }

    /**
     * Run test in remote mode using attaching connector.
     */
    private void remoteLaunchAndAttach
       (String[] jdbCmdArgs, String classToExecute) throws IOException {

        debuggee = new RemoteLaunchedDebuggee(this);
        String address = makeTransportAddress();
        String[] javaCmdArgs = makeCommandLineArgs(classToExecute, address);
        try {
            debuggee.launch(javaCmdArgs);
        } catch (IOException e) {
            throw new Failure("Caught exception while launching debuggee VM process:\n\t"
                            + e);
        };

        display("Start jdb attaching to remote debuggee");
        jdb = Jdb.startAttachingJdb (this, jdbCmdArgs, JDB_STARTED);
//        jdb.waitForPrompt(0, false);
    }

    /**
     * Run test in remote mode using listening connector.
     */
    private void remoteLaunchAndListen
       (String[] jdbCmdArgs, String classToExecute) throws IOException {

        jdb = new Jdb(this);
        display("Starting jdb listening to remote debuggee");
        jdb.launch(jdbCmdArgs);
        String address = jdb.waitForListeningJdb();
        display("Listening address found: " + address);

        debuggee = new RemoteLaunchedDebuggee(this);
        String[] javaCmdArgs = makeCommandLineArgs(classToExecute);
        try {
            debuggee.launch(javaCmdArgs);
        } catch (IOException e) {
            throw new Failure("Caught exception while launching debuggee VM process:\n\t"
                            + e);
        };

        jdb.waitForMessage(0, JDB_STARTED);
//        jdb.waitForPrompt(0, false);
    }

} // End of Launcher