jdk/src/share/classes/com/sun/tools/example/debug/tty/TTY.java
author prr
Tue, 15 Jul 2014 11:22:14 -0700
changeset 25522 10d789df41bb
parent 24969 afa6934dd8e8
permissions -rw-r--r--
8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes Reviewed-by: psandoz, prr Contributed-by: otaviopolianasantana@gmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 10292
diff changeset
     2
 * Copyright (c) 1998, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
10292
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 9520
diff changeset
    26
/*
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 9520
diff changeset
    27
 * This source code is provided to illustrate the usage of a given feature
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 9520
diff changeset
    28
 * or technique and has been deliberately simplified. Additional steps
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 9520
diff changeset
    29
 * required for a production-quality application, such as security checks,
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 9520
diff changeset
    30
 * input validation and proper error handling, might not be present in
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 9520
diff changeset
    31
 * this sample code.
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 9520
diff changeset
    32
 */
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 9520
diff changeset
    33
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 9520
diff changeset
    34
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
package com.sun.tools.example.debug.tty;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import com.sun.jdi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import com.sun.jdi.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import com.sun.jdi.request.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import com.sun.jdi.connect.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
public class TTY implements EventNotifier {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    EventHandler handler = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * List of Strings to execute at each stop.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private List<String> monitorCommands = new ArrayList<String>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private int monitorCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * The name of this tool.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private static final String progname = "jdb";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
9520
99d378796e54 7029383: Refresh of non-client demos
nloodin
parents: 5506
diff changeset
    59
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    public void vmStartEvent(VMStartEvent se)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        Thread.yield();  // fetch output
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        MessageOutput.lnprint("VM Started:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
9520
99d378796e54 7029383: Refresh of non-client demos
nloodin
parents: 5506
diff changeset
    65
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    public void vmDeathEvent(VMDeathEvent e)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
9520
99d378796e54 7029383: Refresh of non-client demos
nloodin
parents: 5506
diff changeset
    69
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    public void vmDisconnectEvent(VMDisconnectEvent e)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
9520
99d378796e54 7029383: Refresh of non-client demos
nloodin
parents: 5506
diff changeset
    73
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    public void threadStartEvent(ThreadStartEvent e)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
9520
99d378796e54 7029383: Refresh of non-client demos
nloodin
parents: 5506
diff changeset
    77
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    public void threadDeathEvent(ThreadDeathEvent e)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
9520
99d378796e54 7029383: Refresh of non-client demos
nloodin
parents: 5506
diff changeset
    81
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    public void classPrepareEvent(ClassPrepareEvent e)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
9520
99d378796e54 7029383: Refresh of non-client demos
nloodin
parents: 5506
diff changeset
    85
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public void classUnloadEvent(ClassUnloadEvent e)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
9520
99d378796e54 7029383: Refresh of non-client demos
nloodin
parents: 5506
diff changeset
    89
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    public void breakpointEvent(BreakpointEvent be)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        Thread.yield();  // fetch output
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        MessageOutput.lnprint("Breakpoint hit:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
9520
99d378796e54 7029383: Refresh of non-client demos
nloodin
parents: 5506
diff changeset
    95
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    public void fieldWatchEvent(WatchpointEvent fwe)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        Field field = fwe.field();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        ObjectReference obj = fwe.object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        Thread.yield();  // fetch output
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if (fwe instanceof ModificationWatchpointEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            MessageOutput.lnprint("Field access encountered before after",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                                  new Object [] {field,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                                                 fwe.valueCurrent(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                                                 ((ModificationWatchpointEvent)fwe).valueToBe()});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            MessageOutput.lnprint("Field access encountered", field.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
9520
99d378796e54 7029383: Refresh of non-client demos
nloodin
parents: 5506
diff changeset
   111
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    public void stepEvent(StepEvent se)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        Thread.yield();  // fetch output
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        MessageOutput.lnprint("Step completed:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
9520
99d378796e54 7029383: Refresh of non-client demos
nloodin
parents: 5506
diff changeset
   117
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    public void exceptionEvent(ExceptionEvent ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        Thread.yield();  // fetch output
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        Location catchLocation = ee.catchLocation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        if (catchLocation == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            MessageOutput.lnprint("Exception occurred uncaught",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                                  ee.exception().referenceType().name());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            MessageOutput.lnprint("Exception occurred caught",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                                  new Object [] {ee.exception().referenceType().name(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                                                 Commands.locationString(catchLocation)});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
9520
99d378796e54 7029383: Refresh of non-client demos
nloodin
parents: 5506
diff changeset
   131
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    public void methodEntryEvent(MethodEntryEvent me) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        Thread.yield();  // fetch output
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
         * These can be very numerous, so be as efficient as possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
         * If we are stopping here, then we will see the normal location
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
         * info printed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        if (me.request().suspendPolicy() != EventRequest.SUSPEND_NONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            // We are stopping; the name will be shown by the normal mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            MessageOutput.lnprint("Method entered:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            // We aren't stopping, show the name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            MessageOutput.print("Method entered:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            printLocationOfEvent(me);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
9520
99d378796e54 7029383: Refresh of non-client demos
nloodin
parents: 5506
diff changeset
   149
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    public boolean methodExitEvent(MethodExitEvent me) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        Thread.yield();  // fetch output
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
         * These can be very numerous, so be as efficient as possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        Method mmm = Env.atExitMethod();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        Method meMethod = me.method();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        if (mmm == null || mmm.equals(meMethod)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            // Either we are not tracing a specific method, or we are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            // and we are exitting that method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            if (me.request().suspendPolicy() != EventRequest.SUSPEND_NONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                // We will be stopping here, so do a newline
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                MessageOutput.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            if (Env.vm().canGetMethodReturnValues()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                MessageOutput.print("Method exitedValue:", me.returnValue() + "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                MessageOutput.print("Method exited:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            if (me.request().suspendPolicy() == EventRequest.SUSPEND_NONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                // We won't be stopping here, so show the method name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                printLocationOfEvent(me);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            // In case we want to have a one shot trace exit some day, this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            // code disables the request so we don't hit it again.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            if (false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                // This is a one shot deal; we don't want to stop
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                // here the next time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                Env.setAtExitMethod(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                EventRequestManager erm = Env.vm().eventRequestManager();
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   185
                for (EventRequest eReq : erm.methodExitRequests()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                    if (eReq.equals(me.request())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                        eReq.disable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        // We are tracing a specific method, and this isn't it.  Keep going.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
9520
99d378796e54 7029383: Refresh of non-client demos
nloodin
parents: 5506
diff changeset
   198
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    public void vmInterrupted() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        Thread.yield();  // fetch output
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        printCurrentLocation();
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   202
        for (String cmd : monitorCommands) {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   203
            StringTokenizer t = new StringTokenizer(cmd);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            t.nextToken();  // get rid of monitor number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            executeCommand(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        MessageOutput.printPrompt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
9520
99d378796e54 7029383: Refresh of non-client demos
nloodin
parents: 5506
diff changeset
   210
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    public void receivedEvent(Event event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    private void printBaseLocation(String threadName, Location loc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        MessageOutput.println("location",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                              new Object [] {threadName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                                             Commands.locationString(loc)});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    private void printCurrentLocation() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        ThreadInfo threadInfo = ThreadInfo.getCurrentThreadInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        StackFrame frame;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            frame = threadInfo.getCurrentFrame();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        } catch (IncompatibleThreadStateException exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            MessageOutput.println("<location unavailable>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        if (frame == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            MessageOutput.println("No frames on the current call stack");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            Location loc = frame.location();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            printBaseLocation(threadInfo.getThread().name(), loc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            // Output the current source line, if possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            if (loc.lineNumber() != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                String line;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                    line = Env.sourceLine(loc, loc.lineNumber());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                } catch (java.io.IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                    line = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                if (line != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                    MessageOutput.println("source line number and line",
25522
10d789df41bb 8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents: 24969
diff changeset
   244
                                          new Object [] {loc.lineNumber(),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                                                         line});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        MessageOutput.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    private void printLocationOfEvent(LocatableEvent theEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        printBaseLocation(theEvent.thread().name(), theEvent.location());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    void help() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        MessageOutput.println("zz help text");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    private static final String[][] commandList = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
         * NOTE: this list must be kept sorted in ascending ASCII
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
         *       order by element [0].  Ref: isCommand() below.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
         *Command      OK when        OK when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
         * name      disconnected?   readonly?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
         *------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        {"!!",           "n",         "y"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        {"?",            "y",         "y"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        {"bytecodes",    "n",         "y"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        {"catch",        "y",         "n"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        {"class",        "n",         "y"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        {"classes",      "n",         "y"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        {"classpath",    "n",         "y"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        {"clear",        "y",         "n"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        {"connectors",   "y",         "y"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        {"cont",         "n",         "n"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        {"disablegc",    "n",         "n"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        {"down",         "n",         "y"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        {"dump",         "n",         "y"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        {"enablegc",     "n",         "n"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        {"eval",         "n",         "y"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        {"exclude",      "y",         "n"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        {"exit",         "y",         "y"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        {"extension",    "n",         "y"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        {"fields",       "n",         "y"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        {"gc",           "n",         "n"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        {"help",         "y",         "y"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        {"ignore",       "y",         "n"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        {"interrupt",    "n",         "n"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        {"kill",         "n",         "n"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        {"lines",        "n",         "y"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        {"list",         "n",         "y"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        {"load",         "n",         "y"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        {"locals",       "n",         "y"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        {"lock",         "n",         "n"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        {"memory",       "n",         "y"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        {"methods",      "n",         "y"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        {"monitor",      "n",         "n"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        {"next",         "n",         "n"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        {"pop",          "n",         "n"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        {"print",        "n",         "y"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        {"quit",         "y",         "y"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        {"read",         "y",         "y"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        {"redefine",     "n",         "n"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        {"reenter",      "n",         "n"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        {"resume",       "n",         "n"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        {"run",          "y",         "n"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        {"save",         "n",         "n"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        {"set",          "n",         "n"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        {"sourcepath",   "y",         "y"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        {"step",         "n",         "n"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        {"stepi",        "n",         "n"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        {"stop",         "y",         "n"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        {"suspend",      "n",         "n"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        {"thread",       "n",         "y"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        {"threadgroup",  "n",         "y"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        {"threadgroups", "n",         "y"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        {"threadlocks",  "n",         "y"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        {"threads",      "n",         "y"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        {"trace",        "n",         "n"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        {"unmonitor",    "n",         "n"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        {"untrace",      "n",         "n"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        {"unwatch",      "y",         "n"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        {"up",           "n",         "y"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        {"use",          "y",         "y"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        {"version",      "y",         "y"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        {"watch",        "y",         "n"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        {"where",        "n",         "y"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        {"wherei",       "n",         "y"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * Look up the command string in commandList.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * If found, return the index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * If not found, return index < 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    private int isCommand(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        //Reference: binarySearch() in java/util/Arrays.java
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        //           Adapted for use with String[][0].
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        int low = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        int high = commandList.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        while (low <= high) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            int mid = (low + high) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            String midVal = commandList[mid][0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            int compare = midVal.compareTo(key);
9520
99d378796e54 7029383: Refresh of non-client demos
nloodin
parents: 5506
diff changeset
   348
            if (compare < 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                low = mid + 1;
9520
99d378796e54 7029383: Refresh of non-client demos
nloodin
parents: 5506
diff changeset
   350
            } else if (compare > 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                high = mid - 1;
9520
99d378796e54 7029383: Refresh of non-client demos
nloodin
parents: 5506
diff changeset
   352
            }
99d378796e54 7029383: Refresh of non-client demos
nloodin
parents: 5506
diff changeset
   353
            else {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                return mid; // key found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        }
9520
99d378796e54 7029383: Refresh of non-client demos
nloodin
parents: 5506
diff changeset
   356
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        return -(low + 1);  // key not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * Return true if the command is OK when disconnected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    private boolean isDisconnectCmd(int ii) {
9520
99d378796e54 7029383: Refresh of non-client demos
nloodin
parents: 5506
diff changeset
   364
        if (ii < 0 || ii >= commandList.length) {
99d378796e54 7029383: Refresh of non-client demos
nloodin
parents: 5506
diff changeset
   365
            return false;
99d378796e54 7029383: Refresh of non-client demos
nloodin
parents: 5506
diff changeset
   366
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        return (commandList[ii][1].equals("y"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * Return true if the command is OK when readonly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    private boolean isReadOnlyCmd(int ii) {
9520
99d378796e54 7029383: Refresh of non-client demos
nloodin
parents: 5506
diff changeset
   374
        if (ii < 0 || ii >= commandList.length) {
99d378796e54 7029383: Refresh of non-client demos
nloodin
parents: 5506
diff changeset
   375
            return false;
99d378796e54 7029383: Refresh of non-client demos
nloodin
parents: 5506
diff changeset
   376
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        return (commandList[ii][2].equals("y"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    void executeCommand(StringTokenizer t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        String cmd = t.nextToken().toLowerCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        // Normally, prompt for the next command after this one is done
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        boolean showPrompt = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
         * Anything starting with # is discarded as a no-op or 'comment'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        if (!cmd.startsWith("#")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
             * Next check for an integer repetition prefix.  If found,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
             * recursively execute cmd that number of times.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            if (Character.isDigit(cmd.charAt(0)) && t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                    int repeat = Integer.parseInt(cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                    String subcom = t.nextToken("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                    while (repeat-- > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                        executeCommand(new StringTokenizer(subcom));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                        showPrompt = false; // Bypass the printPrompt() below.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                } catch (NumberFormatException exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                    MessageOutput.println("Unrecognized command.  Try help...", cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                int commandNumber = isCommand(cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                 * Check for an unknown command
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                if (commandNumber < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                    MessageOutput.println("Unrecognized command.  Try help...", cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                } else if (!Env.connection().isOpen() && !isDisconnectCmd(commandNumber)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                    MessageOutput.println("Command not valid until the VM is started with the run command",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                                          cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                } else if (Env.connection().isOpen() && !Env.vm().canBeModified() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                           !isReadOnlyCmd(commandNumber)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                    MessageOutput.println("Command is not supported on a read-only VM connection",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                                          cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                    Commands evaluator = new Commands();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                        if (cmd.equals("print")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                            evaluator.commandPrint(t, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                            showPrompt = false;        // asynchronous command
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                        } else if (cmd.equals("eval")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                            evaluator.commandPrint(t, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                            showPrompt = false;        // asynchronous command
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                        } else if (cmd.equals("set")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                            evaluator.commandSet(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                            showPrompt = false;        // asynchronous command
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                        } else if (cmd.equals("dump")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                            evaluator.commandPrint(t, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                            showPrompt = false;        // asynchronous command
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                        } else if (cmd.equals("locals")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                            evaluator.commandLocals();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                        } else if (cmd.equals("classes")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                            evaluator.commandClasses();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                        } else if (cmd.equals("class")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                            evaluator.commandClass(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                        } else if (cmd.equals("connectors")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                            evaluator.commandConnectors(Bootstrap.virtualMachineManager());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                        } else if (cmd.equals("methods")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                            evaluator.commandMethods(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                        } else if (cmd.equals("fields")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                            evaluator.commandFields(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                        } else if (cmd.equals("threads")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                            evaluator.commandThreads(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                        } else if (cmd.equals("thread")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                            evaluator.commandThread(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                        } else if (cmd.equals("suspend")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                            evaluator.commandSuspend(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                        } else if (cmd.equals("resume")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                            evaluator.commandResume(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                        } else if (cmd.equals("cont")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                            evaluator.commandCont();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                        } else if (cmd.equals("threadgroups")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                            evaluator.commandThreadGroups();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                        } else if (cmd.equals("threadgroup")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                            evaluator.commandThreadGroup(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                        } else if (cmd.equals("catch")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                            evaluator.commandCatchException(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                        } else if (cmd.equals("ignore")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                            evaluator.commandIgnoreException(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                        } else if (cmd.equals("step")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                            evaluator.commandStep(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                        } else if (cmd.equals("stepi")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                            evaluator.commandStepi();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                        } else if (cmd.equals("next")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                            evaluator.commandNext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                        } else if (cmd.equals("kill")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                            evaluator.commandKill(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                        } else if (cmd.equals("interrupt")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                            evaluator.commandInterrupt(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                        } else if (cmd.equals("trace")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                            evaluator.commandTrace(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                        } else if (cmd.equals("untrace")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                            evaluator.commandUntrace(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                        } else if (cmd.equals("where")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                            evaluator.commandWhere(t, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                        } else if (cmd.equals("wherei")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                            evaluator.commandWhere(t, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                        } else if (cmd.equals("up")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                            evaluator.commandUp(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                        } else if (cmd.equals("down")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                            evaluator.commandDown(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                        } else if (cmd.equals("load")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                            evaluator.commandLoad(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                        } else if (cmd.equals("run")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                            evaluator.commandRun(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                             * Fire up an event handler, if the connection was just
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                             * opened. Since this was done from the run command
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                             * we don't stop the VM on its VM start event (so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                             * arg 2 is false).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                            if ((handler == null) && Env.connection().isOpen()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                                handler = new EventHandler(this, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                        } else if (cmd.equals("memory")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                            evaluator.commandMemory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                        } else if (cmd.equals("gc")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                            evaluator.commandGC();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                        } else if (cmd.equals("stop")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                            evaluator.commandStop(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                        } else if (cmd.equals("clear")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                            evaluator.commandClear(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                        } else if (cmd.equals("watch")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                            evaluator.commandWatch(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                        } else if (cmd.equals("unwatch")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                            evaluator.commandUnwatch(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                        } else if (cmd.equals("list")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                            evaluator.commandList(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                        } else if (cmd.equals("lines")) { // Undocumented command: useful for testing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                            evaluator.commandLines(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                        } else if (cmd.equals("classpath")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                            evaluator.commandClasspath(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                        } else if (cmd.equals("use") || cmd.equals("sourcepath")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                            evaluator.commandUse(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                        } else if (cmd.equals("monitor")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                            monitorCommand(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                        } else if (cmd.equals("unmonitor")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                            unmonitorCommand(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                        } else if (cmd.equals("lock")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                            evaluator.commandLock(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                            showPrompt = false;        // asynchronous command
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                        } else if (cmd.equals("threadlocks")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                            evaluator.commandThreadlocks(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                        } else if (cmd.equals("disablegc")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                            evaluator.commandDisableGC(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                            showPrompt = false;        // asynchronous command
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                        } else if (cmd.equals("enablegc")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                            evaluator.commandEnableGC(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                            showPrompt = false;        // asynchronous command
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                        } else if (cmd.equals("save")) { // Undocumented command: useful for testing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                            evaluator.commandSave(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                            showPrompt = false;        // asynchronous command
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                        } else if (cmd.equals("bytecodes")) { // Undocumented command: useful for testing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                            evaluator.commandBytecodes(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                        } else if (cmd.equals("redefine")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                            evaluator.commandRedefine(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                        } else if (cmd.equals("pop")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                            evaluator.commandPopFrames(t, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                        } else if (cmd.equals("reenter")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                            evaluator.commandPopFrames(t, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                        } else if (cmd.equals("extension")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                            evaluator.commandExtension(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                        } else if (cmd.equals("exclude")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                            evaluator.commandExclude(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                        } else if (cmd.equals("read")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                            readCommand(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                        } else if (cmd.equals("help") || cmd.equals("?")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                            help();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                        } else if (cmd.equals("version")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                            evaluator.commandVersion(progname,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                                                     Bootstrap.virtualMachineManager());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                        } else if (cmd.equals("quit") || cmd.equals("exit")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                            if (handler != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                                handler.shutdown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                            Env.shutdown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                            MessageOutput.println("Unrecognized command.  Try help...", cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                    } catch (VMCannotBeModifiedException rovm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                        MessageOutput.println("Command is not supported on a read-only VM connection", cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                    } catch (UnsupportedOperationException uoe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                        MessageOutput.println("Command is not supported on the target VM", cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                    } catch (VMNotConnectedException vmnse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                        MessageOutput.println("Command not valid until the VM is started with the run command",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                                              cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                    } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                        MessageOutput.printException("Internal exception:", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        if (showPrompt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
            MessageOutput.printPrompt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * Maintain a list of commands to execute each time the VM is suspended.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    void monitorCommand(StringTokenizer t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        if (t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            ++monitorCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            monitorCommands.add(monitorCount + ": " + t.nextToken(""));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        } else {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   592
            for (String cmd : monitorCommands) {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   593
                MessageOutput.printDirectln(cmd);// Special case: use printDirectln()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    void unmonitorCommand(StringTokenizer t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        if (t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            String monTok = t.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            int monNum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                monNum = Integer.parseInt(monTok);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            } catch (NumberFormatException exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                MessageOutput.println("Not a monitor number:", monTok);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            String monStr = monTok + ":";
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   609
            for (String cmd : monitorCommands) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                StringTokenizer ct = new StringTokenizer(cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                if (ct.nextToken().equals(monStr)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                    monitorCommands.remove(cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                    MessageOutput.println("Unmonitoring", cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            MessageOutput.println("No monitor numbered:", monTok);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            MessageOutput.println("Usage: unmonitor <monitor#>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    void readCommand(StringTokenizer t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        if (t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            String cmdfname = t.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            if (!readCommandFile(new File(cmdfname))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                MessageOutput.println("Could not open:", cmdfname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            MessageOutput.println("Usage: read <command-filename>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * Read and execute a command file.  Return true if the file was read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * else false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    boolean readCommandFile(File f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        BufferedReader inFile = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            if (f.canRead()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                // Process initial commands.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                MessageOutput.println("*** Reading commands from", f.getPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                inFile = new BufferedReader(new FileReader(f));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                String ln;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                while ((ln = inFile.readLine()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                    StringTokenizer t = new StringTokenizer(ln);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                    if (t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                        executeCommand(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            if (inFile != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                    inFile.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                } catch (Exception exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        return inFile != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * Try to read commands from dir/fname, unless
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * the canonical path passed in is the same as that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * for dir/fname.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     * Return null if that file doesn't exist,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * else return the canonical path of that file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
    String readStartupCommandFile(String dir, String fname, String canonPath) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        File dotInitFile = new File(dir, fname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        if (!dotInitFile.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        String myCanonFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
            myCanonFile = dotInitFile.getCanonicalPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        } catch (IOException ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
            MessageOutput.println("Could not open:", dotInitFile.getPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        if (canonPath == null || !canonPath.equals(myCanonFile)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
            if (!readCommandFile(dotInitFile)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                MessageOutput.println("Could not open:", dotInitFile.getPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        return myCanonFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
    public TTY() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        MessageOutput.println("Initializing progname", progname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        if (Env.connection().isOpen() && Env.vm().canBeModified()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
             * Connection opened on startup. Start event handler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
             * immediately, telling it (through arg 2) to stop on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
             * VM start event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
            this.handler = new EventHandler(this, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            BufferedReader in =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                    new BufferedReader(new InputStreamReader(System.in));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
            String lastLine = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
            Thread.currentThread().setPriority(Thread.NORM_PRIORITY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
             * Read start up files.  This mimics the behavior
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
             * of gdb which will read both ~/.gdbinit and then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
             * ./.gdbinit if they exist.  We have the twist that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
             * we allow two different names, so we do this:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
             *  if ~/jdb.ini exists,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
             *      read it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
             *  else if ~/.jdbrc exists,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
             *      read it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
             *  if ./jdb.ini exists,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
             *      if it hasn't been read, read it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
             *      It could have been read above because ~ == .
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
             *      or because of symlinks, ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
             *  else if ./jdbrx exists
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
             *      if it hasn't been read, read it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                String userHome = System.getProperty("user.home");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                String canonPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
                if ((canonPath = readStartupCommandFile(userHome, "jdb.ini", null)) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                    // Doesn't exist, try alternate spelling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                    canonPath = readStartupCommandFile(userHome, ".jdbrc", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                String userDir = System.getProperty("user.dir");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                if (readStartupCommandFile(userDir, "jdb.ini", canonPath) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                    // Doesn't exist, try alternate spelling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                    readStartupCommandFile(userDir, ".jdbrc", canonPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
            // Process interactive commands.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
            MessageOutput.printPrompt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
            while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
                String ln = in.readLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                if (ln == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                    MessageOutput.println("Input stream closed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
                    ln = "quit";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
                if (ln.startsWith("!!") && lastLine != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                    ln = lastLine + ln.substring(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                    MessageOutput.printDirectln(ln);// Special case: use printDirectln()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
                StringTokenizer t = new StringTokenizer(ln);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
                if (t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
                    lastLine = ln;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                    executeCommand(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                    MessageOutput.printPrompt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        } catch (VMDisconnectedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
            handler.handleDisconnectedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
    private static void usage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        MessageOutput.println("zz usage text", new Object [] {progname,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
                                                     File.pathSeparator});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
    static void usageError(String messageKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        MessageOutput.println(messageKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        MessageOutput.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
        usage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
    static void usageError(String messageKey, String argument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        MessageOutput.println(messageKey, argument);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        MessageOutput.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        usage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
    private static boolean supportsSharedMemory() {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   794
        for (Connector connector :
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   795
                 Bootstrap.virtualMachineManager().allConnectors()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            if (connector.transport() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
            if ("dt_shmem".equals(connector.transport().name())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
    private static String addressToSocketArgs(String address) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        int index = address.indexOf(':');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        if (index != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
            String hostString = address.substring(0, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
            String portString = address.substring(index + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
            return "hostname=" + hostString + ",port=" + portString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
            return "port=" + address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
    private static boolean hasWhitespace(String string) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        int length = string.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        for (int i = 0; i < length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
            if (Character.isWhitespace(string.charAt(i))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
    private static String addArgument(String string, String argument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        if (hasWhitespace(argument) || argument.indexOf(',') != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
            // Quotes were stripped out for this argument, add 'em back.
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 14342
diff changeset
   830
            StringBuilder sb = new StringBuilder(string);
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 14342
diff changeset
   831
            sb.append('"');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
            for (int i = 0; i < argument.length(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
                char c = argument.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
                if (c == '"') {
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 14342
diff changeset
   835
                    sb.append('\\');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
                }
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 14342
diff changeset
   837
                sb.append(c);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
            }
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 14342
diff changeset
   839
            sb.append("\" ");
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 14342
diff changeset
   840
            return sb.toString();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
            return string + argument + ' ';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
    public static void main(String argv[]) throws MissingResourceException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        String cmdLine = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        String javaArgs = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        int traceFlags = VirtualMachine.TRACE_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        boolean launchImmediately = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
        String connectSpec = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
        MessageOutput.textResources = ResourceBundle.getBundle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
            ("com.sun.tools.example.debug.tty.TTYResources",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
             Locale.getDefault());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        for (int i = 0; i < argv.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
            String token = argv[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
            if (token.equals("-dbgtrace")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
                if ((i == argv.length - 1) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
                    ! Character.isDigit(argv[i+1].charAt(0))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
                    traceFlags = VirtualMachine.TRACE_ALL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
                    String flagStr = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
                        flagStr = argv[++i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
                        traceFlags = Integer.decode(flagStr).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
                    } catch (NumberFormatException nfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
                        usageError("dbgtrace flag value must be an integer:",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
                                   flagStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
            } else if (token.equals("-X")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
                usageError("Use java minus X to see");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
            } else if (
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
                   // Standard VM options passed on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
                   token.equals("-v") || token.startsWith("-v:") ||  // -v[:...]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
                   token.startsWith("-verbose") ||                  // -verbose[:...]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
                   token.startsWith("-D") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
                   // -classpath handled below
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
                   // NonStandard options passed on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
                   token.startsWith("-X") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
                   // Old-style options (These should remain in place as long as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
                   //  the standard VM accepts them)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
                   token.equals("-noasyncgc") || token.equals("-prof") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
                   token.equals("-verify") || token.equals("-noverify") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
                   token.equals("-verifyremote") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
                   token.equals("-verbosegc") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
                   token.startsWith("-ms") || token.startsWith("-mx") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
                   token.startsWith("-ss") || token.startsWith("-oss") ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
                javaArgs = addArgument(javaArgs, token);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
            } else if (token.equals("-tclassic")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                usageError("Classic VM no longer supported.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
            } else if (token.equals("-tclient")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
                // -client must be the first one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
                javaArgs = "-client " + javaArgs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
            } else if (token.equals("-tserver")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
                // -server must be the first one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
                javaArgs = "-server " + javaArgs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
            } else if (token.equals("-sourcepath")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
                if (i == (argv.length - 1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
                    usageError("No sourcepath specified.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
                Env.setSourcePath(argv[++i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
            } else if (token.equals("-classpath")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
                if (i == (argv.length - 1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
                    usageError("No classpath specified.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
                javaArgs = addArgument(javaArgs, token);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
                javaArgs = addArgument(javaArgs, argv[++i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
            } else if (token.equals("-attach")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
                if (connectSpec != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
                    usageError("cannot redefine existing connection", token);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
                if (i == (argv.length - 1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
                    usageError("No attach address specified.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
                String address = argv[++i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
                 * -attach is shorthand for one of the reference implementation's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
                 * attaching connectors. Use the shared memory attach if it's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
                 * available; otherwise, use sockets. Build a connect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
                 * specification string based on this decision.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
                if (supportsSharedMemory()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
                    connectSpec = "com.sun.jdi.SharedMemoryAttach:name=" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
                                   address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
                    String suboptions = addressToSocketArgs(address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
                    connectSpec = "com.sun.jdi.SocketAttach:" + suboptions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
            } else if (token.equals("-listen") || token.equals("-listenany")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
                if (connectSpec != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
                    usageError("cannot redefine existing connection", token);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
                String address = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
                if (token.equals("-listen")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
                    if (i == (argv.length - 1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
                        usageError("No attach address specified.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
                    address = argv[++i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
                 * -listen[any] is shorthand for one of the reference implementation's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
                 * listening connectors. Use the shared memory listen if it's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
                 * available; otherwise, use sockets. Build a connect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
                 * specification string based on this decision.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
                if (supportsSharedMemory()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
                    connectSpec = "com.sun.jdi.SharedMemoryListen:";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
                    if (address != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
                        connectSpec += ("name=" + address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
                    connectSpec = "com.sun.jdi.SocketListen:";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
                    if (address != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
                        connectSpec += addressToSocketArgs(address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
            } else if (token.equals("-launch")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
                launchImmediately = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
            } else if (token.equals("-listconnectors")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
                Commands evaluator = new Commands();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
                evaluator.commandConnectors(Bootstrap.virtualMachineManager());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
            } else if (token.equals("-connect")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
                 * -connect allows the user to pick the connector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
                 * used in bringing up the target VM. This allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
                 * use of connectors other than those in the reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
                 * implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
                if (connectSpec != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
                    usageError("cannot redefine existing connection", token);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
                if (i == (argv.length - 1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
                    usageError("No connect specification.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
                connectSpec = argv[++i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
            } else if (token.equals("-help")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
                usage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
            } else if (token.equals("-version")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
                Commands evaluator = new Commands();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
                evaluator.commandVersion(progname,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
                                         Bootstrap.virtualMachineManager());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
                System.exit(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
            } else if (token.startsWith("-")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
                usageError("invalid option", token);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
                // Everything from here is part of the command line
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
                cmdLine = addArgument("", token);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
                for (i++; i < argv.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
                    cmdLine = addArgument(cmdLine, argv[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
         * Unless otherwise specified, set the default connect spec.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
         * Here are examples of jdb command lines and how the options
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
         * are interpreted as arguments to the program being debugged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
         * arg1       arg2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
         * ----       ----
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
         * jdb hello a b       a          b
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
         * jdb hello "a b"     a b
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
         * jdb hello a,b       a,b
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
         * jdb hello a, b      a,         b
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
         * jdb hello "a, b"    a, b
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
         * jdb -connect "com.sun.jdi.CommandLineLaunch:main=hello  a,b"   illegal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
         * jdb -connect  com.sun.jdi.CommandLineLaunch:main=hello "a,b"   illegal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
         * jdb -connect 'com.sun.jdi.CommandLineLaunch:main=hello "a,b"'  arg1 = a,b
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
         * jdb -connect 'com.sun.jdi.CommandLineLaunch:main=hello "a b"'  arg1 = a b
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
         * jdb -connect 'com.sun.jdi.CommandLineLaunch:main=hello  a b'   arg1 = a  arg2 = b
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
         * jdb -connect 'com.sun.jdi.CommandLineLaunch:main=hello "a," b' arg1 = a, arg2 = b
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
        if (connectSpec == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
            connectSpec = "com.sun.jdi.CommandLineLaunch:";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
        } else if (!connectSpec.endsWith(",") && !connectSpec.endsWith(":")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
            connectSpec += ","; // (Bug ID 4285874)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
        cmdLine = cmdLine.trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
        javaArgs = javaArgs.trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
        if (cmdLine.length() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
            if (!connectSpec.startsWith("com.sun.jdi.CommandLineLaunch:")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
                usageError("Cannot specify command line with connector:",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
                           connectSpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
            connectSpec += "main=" + cmdLine + ",";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
        if (javaArgs.length() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
            if (!connectSpec.startsWith("com.sun.jdi.CommandLineLaunch:")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
                usageError("Cannot specify target vm arguments with connector:",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
                           connectSpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
            connectSpec += "options=" + javaArgs + ",";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
            if (! connectSpec.endsWith(",")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
                connectSpec += ","; // (Bug ID 4285874)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
            Env.init(connectSpec, launchImmediately, traceFlags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
            new TTY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
        } catch(Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
            MessageOutput.printException("Internal exception:", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
}