jdk/src/share/classes/com/sun/tools/example/debug/gui/CommandInterpreter.java
author xdono
Wed, 02 Jul 2008 12:55:45 -0700
changeset 715 f16baef3a20e
parent 51 6fe31bc95bbc
child 5506 202f599c92aa
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 51
diff changeset
     2
 * Copyright 1998-2008 Sun Microsystems, Inc.  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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.example.debug.gui;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import com.sun.jdi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import com.sun.jdi.request.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import com.sun.tools.example.debug.bdi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
public class CommandInterpreter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    boolean echo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    Environment env;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private ContextManager context;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private ExecutionManager runtime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private ClassManager classManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private SourceManager sourceManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private OutputSink out; //### Hack!  Should be local in each method used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private String lastCommand = "help";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    public CommandInterpreter(Environment env) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        this(env, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    public CommandInterpreter(Environment env, boolean echo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        this.env = env;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        this.echo = echo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        this.runtime = env.getExecutionManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        this.context = env.getContextManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        this.classManager = env.getClassManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        this.sourceManager = env.getSourceManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private ThreadReference[] threads = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * The numbering of threads is relative to the current set of threads,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * and may be affected by the creation and termination of new threads.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * Commands issued using such thread ids will only give reliable behavior
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * relative to what was shown earlier in 'list' commands if the VM is interrupted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * We need a better scheme.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private ThreadReference[] threads() throws NoSessionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        if (threads == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            ThreadIterator ti = new ThreadIterator(getDefaultThreadGroup());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            List<ThreadReference> tlist = new ArrayList<ThreadReference>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            while (ti.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                tlist.add(ti.nextThread());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            }
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    80
            threads = tlist.toArray(new ThreadReference[tlist.size()]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        return threads;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private ThreadReference findThread(String idToken) throws NoSessionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        String id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        ThreadReference thread = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        if (idToken.startsWith("t@")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            id = idToken.substring(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            id = idToken;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            ThreadReference[] threads = threads();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            long threadID = Long.parseLong(id, 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            for (int i = 0; i < threads.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                if (threads[i].uniqueID() == threadID) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                    thread = threads[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            if (thread == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                //env.failure("No thread for id \"" + idToken + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                env.failure("\"" + idToken + "\" is not a valid thread id.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        } catch (NumberFormatException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            env.error("Thread id \"" + idToken + "\" is ill-formed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            thread = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        return thread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    private ThreadIterator allThreads() throws NoSessionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        threads = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        //### Why not use runtime.allThreads().iterator() ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        return new ThreadIterator(runtime.topLevelThreadGroups());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    private ThreadIterator currentThreadGroupThreads() throws NoSessionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        threads = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        return new ThreadIterator(getDefaultThreadGroup());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    private ThreadGroupIterator allThreadGroups() throws NoSessionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        threads = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        return new ThreadGroupIterator(runtime.topLevelThreadGroups());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    private ThreadGroupReference defaultThreadGroup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    private ThreadGroupReference getDefaultThreadGroup() throws NoSessionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        if (defaultThreadGroup == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            defaultThreadGroup = runtime.systemThreadGroup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        return defaultThreadGroup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    private void setDefaultThreadGroup(ThreadGroupReference tg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        defaultThreadGroup = tg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * Command handlers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    // Command: classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    private void commandClasses() throws NoSessionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        OutputSink out = env.getOutputSink();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        //out.println("** classes list **");
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   151
        for (ReferenceType refType : runtime.allClasses()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            out.println(refType.name());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        out.show();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    // Command: methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    private void commandMethods(StringTokenizer t) throws NoSessionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        if (!t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            env.error("No class specified.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        String idClass = t.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        ReferenceType cls = findClass(idClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        if (cls != null) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   168
            List<Method> methods = cls.allMethods();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            OutputSink out = env.getOutputSink();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            for (int i = 0; i < methods.size(); i++) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   171
                Method method = methods.get(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                out.print(method.declaringType().name() + " " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                            method.name() + "(");
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   174
                Iterator<String> it = method.argumentTypeNames().iterator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                if (it.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                    while (true) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   177
                        out.print(it.next());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                        if (!it.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                        out.print(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                out.println(")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            out.show();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            //### Should validate class name syntax.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            env.failure("\"" + idClass + "\" is not a valid id or class name.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    private ReferenceType findClass(String pattern) throws NoSessionException {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   194
        List<ReferenceType> results = runtime.findClassesMatchingPattern(pattern);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        if (results.size() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            //### Should handle multiple results sensibly.
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   197
            return results.get(0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    // Command: threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    private void commandThreads(StringTokenizer t) throws NoSessionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        if (!t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            OutputSink out = env.getOutputSink();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            printThreadGroup(out, getDefaultThreadGroup(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            out.show();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        String name = t.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        ThreadGroupReference tg = findThreadGroup(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        if (tg == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            env.failure(name + " is not a valid threadgroup name.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            OutputSink out = env.getOutputSink();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            printThreadGroup(out, tg, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            out.show();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    private ThreadGroupReference findThreadGroup(String name) throws NoSessionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        //### Issue: Uniqueness of thread group names is not enforced.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        ThreadGroupIterator tgi = allThreadGroups();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        while (tgi.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            ThreadGroupReference tg = tgi.nextThreadGroup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            if (tg.name().equals(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                return tg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    private int printThreadGroup(OutputSink out, ThreadGroupReference tg, int iThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        out.println("Group " + tg.name() + ":");
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   236
        List<ThreadReference> tlist = tg.threads();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        int maxId = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        int maxName = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        for (int i = 0 ; i < tlist.size() ; i++) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   240
            ThreadReference thr = tlist.get(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            int len = Utils.description(thr).length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            if (len > maxId)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                maxId = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            String name = thr.name();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            int iDot = name.lastIndexOf('.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            if (iDot >= 0 && name.length() > iDot) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                name = name.substring(iDot + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            if (name.length() > maxName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                maxName = name.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        String maxNumString = String.valueOf(iThread + tlist.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        int maxNumDigits = maxNumString.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        for (int i = 0 ; i < tlist.size() ; i++) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   255
            ThreadReference thr = tlist.get(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            char buf[] = new char[80];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            for (int j = 0; j < 79; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                buf[j] = ' ';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            buf[79] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            StringBuffer sbOut = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            sbOut.append(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            // Right-justify the thread number at start of output string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            String numString = String.valueOf(iThread + i + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            sbOut.insert(maxNumDigits - numString.length(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                         numString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            sbOut.insert(maxNumDigits, ".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            int iBuf = maxNumDigits + 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            sbOut.insert(iBuf, Utils.description(thr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            iBuf += maxId + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            String name = thr.name();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            int iDot = name.lastIndexOf('.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            if (iDot >= 0 && name.length() > iDot) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                name = name.substring(iDot + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            sbOut.insert(iBuf, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            iBuf += maxName + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            sbOut.insert(iBuf, Utils.getStatus(thr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            sbOut.setLength(79);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            out.println(sbOut.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        }
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   284
        for (ThreadGroupReference tg0 : tg.threadGroups()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            if (!tg.equals(tg0)) {  // TODO ref mgt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                iThread += printThreadGroup(out, tg0, iThread + tlist.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        return tlist.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    // Command: threadgroups
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    private void commandThreadGroups() throws NoSessionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        ThreadGroupIterator it = allThreadGroups();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        int cnt = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        OutputSink out = env.getOutputSink();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        while (it.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            ThreadGroupReference tg = it.nextThreadGroup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            ++cnt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            out.println("" + cnt + ". " + Utils.description(tg) + " " + tg.name());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        out.show();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    // Command: thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    private void commandThread(StringTokenizer t) throws NoSessionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        if (!t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            env.error("Thread number not specified.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        ThreadReference thread = findThread(t.nextToken());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        if (thread != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            //### Should notify user.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            context.setCurrentThread(thread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    // Command: threadgroup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    private void commandThreadGroup(StringTokenizer t) throws NoSessionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        if (!t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            env.error("Threadgroup name not specified.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        String name = t.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        ThreadGroupReference tg = findThreadGroup(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        if (tg == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            env.failure(name + " is not a valid threadgroup name.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            //### Should notify user.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            setDefaultThreadGroup(tg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    // Command: run
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    private void commandRun(StringTokenizer t) throws NoSessionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        if (doLoad(false, t)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            env.notice("Running ...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    // Command: load
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    private void commandLoad(StringTokenizer t) throws NoSessionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        if (doLoad(true, t)) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    private boolean doLoad(boolean suspended,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                           StringTokenizer t) throws NoSessionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        String clname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        if (!t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            clname = context.getMainClassName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            if (!clname.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                // Run from prevously-set class name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                    String vmArgs = context.getVmArguments();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                    runtime.run(suspended,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                                vmArgs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                                clname,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                                context.getProgramArguments());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                } catch (VMLaunchFailureException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                    env.failure("Attempt to launch main class \"" + clname + "\" failed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                env.failure("No main class specifed and no current default defined.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            clname = t.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            StringBuffer sbuf = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            // Allow VM arguments to be specified here?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            while (t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                String tok = t.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                sbuf.append(tok);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                if (t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                    sbuf.append(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            String args = sbuf.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                String vmArgs = context.getVmArguments();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                runtime.run(suspended, vmArgs, clname, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                context.setMainClassName(clname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                //context.setVmArguments(vmArgs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                context.setProgramArguments(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            } catch (VMLaunchFailureException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                env.failure("Attempt to launch main class \"" + clname + "\" failed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    // Command: connect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    private void commandConnect(StringTokenizer t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            LaunchTool.queryAndLaunchVM(runtime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        } catch (VMLaunchFailureException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            env.failure("Attempt to connect failed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    // Command: attach
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    private void commandAttach(StringTokenizer t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        String portName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        if (!t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            portName = context.getRemotePort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            if (!portName.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                    runtime.attach(portName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                } catch (VMLaunchFailureException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                    env.failure("Attempt to attach to port \"" + portName + "\" failed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                env.failure("No port specifed and no current default defined.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            portName = t.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                runtime.attach(portName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            } catch (VMLaunchFailureException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                env.failure("Attempt to attach to port \"" + portName + "\" failed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            context.setRemotePort(portName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    // Command: detach
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    private void commandDetach(StringTokenizer t) throws NoSessionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        runtime.detach();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    // Command: interrupt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    private void commandInterrupt(StringTokenizer t) throws NoSessionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        runtime.interrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    // Command: suspend
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    private void commandSuspend(StringTokenizer t) throws NoSessionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        if (!t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            // Suspend all threads in the current thread group.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            //### Issue: help message says default is all threads.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            //### Behavior here agrees with 'jdb', however.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            ThreadIterator ti = currentThreadGroupThreads();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            while (ti.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                // TODO - don't suspend debugger threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                ti.nextThread().suspend();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            env.notice("All (non-system) threads suspended.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            while (t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                ThreadReference thread = findThread(t.nextToken());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                if (thread != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                    //thread.suspend();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                    runtime.suspendThread(thread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    // Command: resume
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    private void commandResume(StringTokenizer t) throws NoSessionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
         if (!t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            // Suspend all threads in the current thread group.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            //### Issue: help message says default is all threads.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            //### Behavior here agrees with 'jdb', however.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            ThreadIterator ti = currentThreadGroupThreads();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            while (ti.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                // TODO - don't suspend debugger threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                ti.nextThread().resume();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            env.notice("All threads resumed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
         } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
             while (t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                ThreadReference thread = findThread(t.nextToken());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                if (thread != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                    //thread.resume();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                    runtime.resumeThread(thread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    // Command: cont
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    private void commandCont() throws NoSessionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            runtime.go();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        } catch (VMNotInterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            //### failure?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            env.notice("Target VM is already running.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    // Command: step
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    private void commandStep(StringTokenizer t) throws NoSessionException{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        ThreadReference current = context.getCurrentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        if (current == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            env.failure("No current thread.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            if (t.hasMoreTokens() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                t.nextToken().toLowerCase().equals("up")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                runtime.stepOut(current);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                runtime.stepIntoLine(current);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        } catch (AbsentInformationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            env.failure("No linenumber information available -- " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                            "Try \"stepi\" to step by instructions.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    // Command: stepi
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    private void commandStepi() throws NoSessionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        ThreadReference current = context.getCurrentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        if (current == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            env.failure("No current thread.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        runtime.stepIntoInstruction(current);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    // Command: next
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    private void commandNext() throws NoSessionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        ThreadReference current = context.getCurrentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        if (current == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            env.failure("No current thread.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            runtime.stepOverLine(current);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        } catch (AbsentInformationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            env.failure("No linenumber information available -- " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                            "Try \"nexti\" to step by instructions.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    // Command: nexti  (NEW)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    private void commandNexti() throws NoSessionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        ThreadReference current = context.getCurrentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        if (current == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            env.failure("No current thread.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        runtime.stepOverInstruction(current);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
    // Command: kill
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    private void commandKill(StringTokenizer t) throws NoSessionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        //### Should change the way in which thread ids and threadgroup names
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        //### are distinguished.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
         if (!t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            env.error("Usage: kill <threadgroup name> or <thread id>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        while (t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            String idToken = t.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            ThreadReference thread = findThread(idToken);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            if (thread != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                runtime.stopThread(thread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                env.notice("Thread " + thread.name() + " killed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                /* Check for threadgroup name, NOT skipping "system". */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                //### Should skip "system"?  Classic 'jdb' does this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                //### Should deal with possible non-uniqueness of threadgroup names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                ThreadGroupIterator itg = allThreadGroups();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                while (itg.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                    ThreadGroupReference tg = itg.nextThreadGroup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                    if (tg.name().equals(idToken)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                        ThreadIterator it = new ThreadIterator(tg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                        while (it.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                            runtime.stopThread(it.nextThread());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                        env.notice("Threadgroup " + tg.name() + "killed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                env.failure("\"" + idToken +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                            "\" is not a valid threadgroup or id.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    /*************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    // TODO
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    private void commandCatchException(StringTokenizer t) throws NoSessionException {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    // TODO
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
    private void commandIgnoreException(StringTokenizer t) throws NoSessionException {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    *************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    // Command: up
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    //### Print current frame after command?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
    int readCount(StringTokenizer t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        int cnt = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        if (t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            String idToken = t.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                cnt = Integer.valueOf(idToken).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
            } catch (NumberFormatException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                cnt = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        return cnt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    void commandUp(StringTokenizer t) throws NoSessionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        ThreadReference current = context.getCurrentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        if (current == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
            env.failure("No current thread.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        int nLevels = readCount(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        if (nLevels <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            env.error("usage: up [n frames]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            int delta = context.moveCurrentFrameIndex(current, -nLevels);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            if (delta == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                env.notice("Already at top of stack.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            } else if (-delta < nLevels) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                env.notice("Moved up " + delta + " frames to top of stack.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        } catch (VMNotInterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            env.failure("Target VM must be in interrupted state.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
    private void commandDown(StringTokenizer t) throws NoSessionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        ThreadReference current = context.getCurrentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        if (current == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            env.failure("No current thread.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        int nLevels = readCount(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        if (nLevels <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            env.error("usage: down [n frames]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
            int delta = context.moveCurrentFrameIndex(current, nLevels);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
            if (delta == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                env.notice("Already at bottom of stack.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            } else if (delta < nLevels) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                env.notice("Moved down " + delta + " frames to bottom of stack.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        } catch (VMNotInterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
            env.failure("Target VM must be in interrupted state.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
    // Command: frame
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
    private void commandFrame(StringTokenizer t) throws NoSessionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        ThreadReference current = context.getCurrentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        if (current == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            env.failure("No current thread.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        if (!t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
            env.error("usage: frame <frame-index>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        String idToken = t.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            n = Integer.valueOf(idToken).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        } catch (NumberFormatException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
            n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        if (n <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            env.error("use positive frame index");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
            int delta = context.setCurrentFrameIndex(current, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
            if (delta == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                env.notice("Frame unchanged.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
            } else if (delta < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                env.notice("Moved up " + -delta + " frames.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                env.notice("Moved down " + delta + " frames.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        } catch (VMNotInterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            env.failure("Target VM must be in interrupted state.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
    // Command: where
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
    //### Should we insist that VM be interrupted here?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
    //### There is an inconsistency between the 'where' command
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
    //### and 'up' and 'down' in this respect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
    private void commandWhere(StringTokenizer t, boolean showPC)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                                                throws NoSessionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        ThreadReference current = context.getCurrentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        if (!t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
            if (current == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                env.error("No thread specified.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            dumpStack(current, showPC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
            String token = t.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
            if (token.toLowerCase().equals("all")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                ThreadIterator it = allThreads();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
                while (it.hasNext()) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   732
                    ThreadReference thread = it.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                    out.println(thread.name() + ": ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                    dumpStack(thread, showPC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                ThreadReference thread = findThread(t.nextToken());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                //### Do we want to set current thread here?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                //### Should notify user of change.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                if (thread != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                    context.setCurrentThread(thread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                dumpStack(thread, showPC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
            }
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
    private void dumpStack(ThreadReference thread, boolean showPC) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        //### Check for these.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        //env.failure("Thread no longer exists.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        //env.failure("Target VM must be in interrupted state.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        //env.failure("Current thread isn't suspended.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        //### Should handle extremely long stack traces sensibly for user.
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   754
        List<StackFrame> stack = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            stack = thread.frames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        } catch (IncompatibleThreadStateException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
            env.failure("Thread is not suspended.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        //### Fix this!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        //### Previously mishandled cases where thread was not current.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        //### Now, prints all of the stack regardless of current frame.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        int frameIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        //int frameIndex = context.getCurrentFrameIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        if (stack == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
            env.failure("Thread is not running (no stack).");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
            OutputSink out = env.getOutputSink();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
            int nFrames = stack.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            for (int i = frameIndex; i < nFrames; i++) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   771
                StackFrame frame = stack.get(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
                Location loc = frame.location();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
                Method meth = loc.method();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
                out.print("  [" + (i + 1) + "] ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
                out.print(meth.declaringType().name());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
                out.print('.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
                out.print(meth.name());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                out.print(" (");
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   779
                if (meth.isNative()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
                    out.print("native method");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
                } else if (loc.lineNumber() != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                        out.print(loc.sourceName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                    } catch (AbsentInformationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                        out.print("<unknown>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                    out.print(':');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
                    out.print(loc.lineNumber());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                out.print(')');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                if (showPC) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                    long pc = loc.codeIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
                    if (pc != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
                        out.print(", pc = " + pc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
                out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
            out.show();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
    private void listEventRequests() throws NoSessionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        // Print set breakpoints
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   805
        List<EventRequestSpec> specs = runtime.eventRequestSpecs();
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   806
        if (specs.isEmpty()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
            env.notice("No breakpoints/watchpoints/exceptions set.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
            OutputSink out = env.getOutputSink();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
            out.println("Current breakpoints/watchpoints/exceptions set:");
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   811
            for (EventRequestSpec bp : specs) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                out.println("\t" + bp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
            out.show();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
    private BreakpointSpec parseBreakpointSpec(String bptSpec) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        StringTokenizer t = new StringTokenizer(bptSpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        BreakpointSpec bpSpec = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
//        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
            String token = t.nextToken("@:( \t\n\r");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
            // We can't use hasMoreTokens here because it will cause any leading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
            // paren to be lost.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
            String rest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
                rest = t.nextToken("").trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
            } catch (NoSuchElementException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
                rest = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
            if ((rest != null) && rest.startsWith("@")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
                t = new StringTokenizer(rest.substring(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
                String sourceName = token;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
                String lineToken = t.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
                int lineNumber = Integer.valueOf(lineToken).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
                if (t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
                bpSpec = runtime.createSourceLineBreakpoint(sourceName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
                                                            lineNumber);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
            } else if ((rest != null) && rest.startsWith(":")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                t = new StringTokenizer(rest.substring(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
                String classId = token;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
                String lineToken = t.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
                int lineNumber = Integer.valueOf(lineToken).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
                if (t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
                bpSpec = runtime.createClassLineBreakpoint(classId, lineNumber);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
                // Try stripping method from class.method token.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
                int idot = token.lastIndexOf(".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
                if ( (idot <= 0) ||        /* No dot or dot in first char */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
                     (idot >= token.length() - 1) ) { /* dot in last char */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
                String methodName = token.substring(idot + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
                String classId = token.substring(0, idot);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
                List<String> argumentList = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
                if (rest != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
                    if (!rest.startsWith("(") || !rest.endsWith(")")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
                        //### Should throw exception with error message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
                        //out.println("Invalid method specification: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
                        //            + methodName + rest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
                    // Trim the parens
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
                    //### What about spaces in arglist?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
                    rest = rest.substring(1, rest.length() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
                    argumentList = new ArrayList<String>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
                    t = new StringTokenizer(rest, ",");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
                    while (t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
                        argumentList.add(t.nextToken());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
                bpSpec = runtime.createMethodBreakpoint(classId,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
                                                       methodName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
                                                       argumentList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
//        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
//            env.error("Exception attempting to create breakpoint: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
//            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
//        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
        return bpSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
    private void commandStop(StringTokenizer t) throws NoSessionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
        Location bploc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
        String token;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
        if (!t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
            listEventRequests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
            token = t.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
            // Ignore optional "at" or "in" token.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
            // Allowed for backward compatibility.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
            if (token.equals("at") || token.equals("in")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
                if (t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
                    token = t.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
                    env.error("Missing breakpoint specification.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
            BreakpointSpec bpSpec = parseBreakpointSpec(token);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
            if (bpSpec != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
                //### Add sanity-checks for deferred breakpoint.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
                runtime.install(bpSpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
                env.error("Ill-formed breakpoint specification.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
    private void commandClear(StringTokenizer t) throws NoSessionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        if (!t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
            // Print set breakpoints
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
            listEventRequests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
        //### need 'clear all'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        BreakpointSpec bpSpec = parseBreakpointSpec(t.nextToken());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        if (bpSpec != null) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   924
            List<EventRequestSpec> specs = runtime.eventRequestSpecs();
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   925
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   926
            if (specs.isEmpty()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
                env.notice("No breakpoints set.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
            } else {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   929
                List<EventRequestSpec> toDelete = new ArrayList<EventRequestSpec>();
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   930
                for (EventRequestSpec spec : specs) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
                    if (spec.equals(bpSpec)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
                        toDelete.add(spec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
                // The request used for matching should be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
                if (toDelete.size() <= 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
                    env.notice("No matching breakpoint set.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
                }
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   939
                for (EventRequestSpec spec : toDelete) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
                    runtime.delete(spec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
            env.error("Ill-formed breakpoint specification.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
    // Command: list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
    private void commandList(StringTokenizer t) throws NoSessionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
        ThreadReference current = context.getCurrentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
        if (current == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
            env.error("No thread specified.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
        Location loc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
            StackFrame frame = context.getCurrentFrame(current);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
            if (frame == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
                env.failure("Thread has not yet begun execution.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
            loc = frame.location();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
        } catch (VMNotInterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
            env.failure("Target VM must be in interrupted state.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
        SourceModel source = sourceManager.sourceForLocation(loc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
        if (source == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
            if (loc.method().isNative()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
                env.failure("Current method is native.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
            env.failure("No source available for " + Utils.locationString(loc) + ".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
        ReferenceType refType = loc.declaringType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
        int lineno = loc.lineNumber();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
        if (t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
            String id = t.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
            // See if token is a line number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
                lineno = Integer.valueOf(id).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
            } catch (NumberFormatException nfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
                // It isn't -- see if it's a method name.
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   986
                List<Method> meths = refType.methodsByName(id);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
                if (meths == null || meths.size() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
                    env.failure(id +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
                                " is not a valid line number or " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
                                "method name for class " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
                                refType.name());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
                } else if (meths.size() > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
                    env.failure(id +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
                                " is an ambiguous method name in" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
                                refType.name());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
                }
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   999
                loc = meths.get(0).location();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
                lineno = loc.lineNumber();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
        int startLine = (lineno > 4) ? lineno - 4 : 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
        int endLine = startLine + 9;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
        String sourceLine = source.sourceLine(lineno);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
        if (sourceLine == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
            env.failure("" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
                        lineno +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
                        " is an invalid line number for " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
                        refType.name());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
            OutputSink out = env.getOutputSink();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
            for (int i = startLine; i <= endLine; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
                sourceLine = source.sourceLine(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
                if (sourceLine == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
                out.print(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
                out.print("\t");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
                if (i == lineno) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
                    out.print("=> ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
                    out.print("   ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
                out.println(sourceLine);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
            out.show();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
    // Command: use
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
    // Get or set the source file path list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
    private void commandUse(StringTokenizer t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
        if (!t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
            out.println(sourceManager.getSourcePath().asString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
            //### Should throw exception for invalid path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
            //### E.g., vetoable property change.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
            sourceManager.setSourcePath(new SearchPath(t.nextToken()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
    // Command: sourcepath
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
    // Get or set the source file path list.  (Alternate to 'use'.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
    private void commandSourcepath(StringTokenizer t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
        if (!t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
            out.println(sourceManager.getSourcePath().asString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
            //### Should throw exception for invalid path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
            //### E.g., vetoable property change.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
            sourceManager.setSourcePath(new SearchPath(t.nextToken()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
    // Command: classpath
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
    // Get or set the class file path list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
    private void commandClasspath(StringTokenizer t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
        if (!t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
            out.println(classManager.getClassPath().asString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
            //### Should throw exception for invalid path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
            //### E.g., vetoable property change.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
            classManager.setClassPath(new SearchPath(t.nextToken()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
    // Command: view
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
    // Display source for source file or class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
    private void commandView(StringTokenizer t) throws NoSessionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
        if (!t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
            env.error("Argument required");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
            String name = t.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
            if (name.endsWith(".java") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
                name.indexOf(File.separatorChar) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
                env.viewSource(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
                //### JDI crashes taking line number for class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
                /*****
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
                ReferenceType cls = findClass(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
                if (cls != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
                    env.viewLocation(cls.location());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
                    env.failure("No such class");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
                *****/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
                String fileName = name.replace('.', File.separatorChar) + ".java";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
                env.viewSource(fileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
    // Command: locals
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
    // Print all local variables in current stack frame.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
    private void commandLocals() throws NoSessionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
        ThreadReference current = context.getCurrentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
        if (current == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
            env.failure("No default thread specified: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
                        "use the \"thread\" command first.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
        StackFrame frame;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
            frame = context.getCurrentFrame(current);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
            if (frame == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
                env.failure("Thread has not yet created any stack frames.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
        } catch (VMNotInterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
            env.failure("Target VM must be in interrupted state.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1119
        List<LocalVariable> vars;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
            vars = frame.visibleVariables();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
            if (vars == null || vars.size() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
                env.failure("No local variables");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
        } catch (AbsentInformationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
            env.failure("Local variable information not available." +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
                        " Compile with -g to generate variable information");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
        OutputSink out = env.getOutputSink();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
        out.println("Method arguments:");
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1134
        for (LocalVariable var : vars) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
            if (var.isArgument()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
                printVar(out, var, frame);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
        out.println("Local variables:");
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1140
        for (LocalVariable var : vars) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
            if (!var.isArgument()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
                printVar(out, var, frame);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
        out.show();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
     * Command: monitor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
     * Monitor an expression
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
    private void commandMonitor(StringTokenizer t) throws NoSessionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
        if (!t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
            env.error("Argument required");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
            env.getMonitorListModel().add(t.nextToken(""));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
     * Command: unmonitor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
     * Unmonitor an expression
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
    private void commandUnmonitor(StringTokenizer t) throws NoSessionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
        if (!t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
            env.error("Argument required");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
            env.getMonitorListModel().remove(t.nextToken(""));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
    // Print a stack variable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
    private void printVar(OutputSink out, LocalVariable var, StackFrame frame) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
        out.print("  " + var.name());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
        if (var.isVisible(frame)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
            Value val = frame.getValue(var);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
            out.println(" = " + val.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
            out.println(" is not in scope");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
    // Command: print
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
    // Evaluate an expression.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
    private void commandPrint(StringTokenizer t, boolean dumpObject) throws NoSessionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
        if (!t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
            //### Probably confused if expresion contains whitespace.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
            env.error("No expression specified.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
        ThreadReference current = context.getCurrentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
        if (current == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
            env.failure("No default thread specified: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
                        "use the \"thread\" command first.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
        StackFrame frame;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
            frame = context.getCurrentFrame(current);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
            if (frame == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
                env.failure("Thread has not yet created any stack frames.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
        } catch (VMNotInterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
            env.failure("Target VM must be in interrupted state.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
        while (t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
            String expr = t.nextToken("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
            Value val = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
                val = runtime.evaluate(frame, expr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
            } catch(Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
                env.error("Exception: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
                //### Fix this!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
            if (val == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
                return;  // Error message already printed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
            OutputSink out = env.getOutputSink();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
            if (dumpObject && (val instanceof ObjectReference) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
                                 !(val instanceof StringReference)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
                ObjectReference obj = (ObjectReference)val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
                ReferenceType refType = obj.referenceType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
                out.println(expr + " = " + val.toString() + " {");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
                dump(out, obj, refType, refType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
                out.println("}");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
                out.println(expr + " = " + val.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
            out.show();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
    private void dump(OutputSink out,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
                      ObjectReference obj, ReferenceType refType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
                      ReferenceType refTypeBase) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1241
        for (Field field : refType.fields()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
            out.print("    ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
            if (!refType.equals(refTypeBase)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
                out.print(refType.name() + ".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
            out.print(field.name() + ": ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
            Object o = obj.getValue(field);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
            out.println((o == null) ? "null" : o.toString()); // Bug ID 4374471
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
        if (refType instanceof ClassType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
            ClassType sup = ((ClassType)refType).superclass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
            if (sup != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
                dump(out, obj, sup, refTypeBase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
        } else if (refType instanceof InterfaceType) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1256
            for (InterfaceType sup : ((InterfaceType)refType).superinterfaces()) {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1257
                dump(out, obj, sup, refTypeBase);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
     * Display help message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
    private void help() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
        out.println("** command list **");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
        out.println("threads [threadgroup]     -- list threads");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
        out.println("thread <thread id>        -- set default thread");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
        out.println("suspend [thread id(s)]    -- suspend threads (default: all)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
        out.println("resume [thread id(s)]     -- resume threads (default: all)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
        out.println("where [thread id] | all   -- dump a thread's stack");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
        out.println("wherei [thread id] | all  -- dump a thread's stack, with pc info");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
        out.println("threadgroups              -- list threadgroups");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
        out.println("threadgroup <name>        -- set current threadgroup\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
//      out.println("print <expression>        -- print value of expression");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
        out.println("dump <expression>         -- print all object information\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
//      out.println("eval <expression>         -- evaluate expression (same as print)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
        out.println("locals                    -- print all local variables in current stack frame\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
        out.println("classes                   -- list currently known classes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
        out.println("methods <class id>        -- list a class's methods\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
        out.println("stop [in] <class id>.<method>[(argument_type,...)] -- set a breakpoint in a method");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
        out.println("stop [at] <class id>:<line> -- set a breakpoint at a line");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
        out.println("up [n frames]             -- move up a thread's stack");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
        out.println("down [n frames]           -- move down a thread's stack");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
        out.println("frame <frame-id>           -- to a frame");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
        out.println("clear <class id>.<method>[(argument_type,...)]   -- clear a breakpoint in a method");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
        out.println("clear <class id>:<line>   -- clear a breakpoint at a line");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
        out.println("clear                     -- list breakpoints");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
        out.println("step                      -- execute current line");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
        out.println("step up                   -- execute until the current method returns to its caller");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
        out.println("stepi                     -- execute current instruction");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
        out.println("next                      -- step one line (step OVER calls)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
        out.println("nexti                     -- step one instruction (step OVER calls)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
        out.println("cont                      -- continue execution from breakpoint\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
//      out.println("catch <class id>          -- break for the specified exception");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
//      out.println("ignore <class id>         -- ignore when the specified exception\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
        out.println("view classname|filename   -- display source file");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
        out.println("list [line number|method] -- print source code context at line or method");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
        out.println("use <source file path>    -- display or change the source path\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
//### new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
        out.println("sourcepath <source file path>    -- display or change the source path\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
//### new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
        out.println("classpath <class file path>    -- display or change the class path\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
        out.println("monitor <expression>      -- evaluate an expression each time the program stops\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
        out.println("unmonitor <monitor#>      -- delete a monitor\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
        out.println("read <filename>           -- read and execute a command file\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
//      out.println("memory                    -- report memory usage");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
//      out.println("gc                        -- free unused objects\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
        out.println("run <class> [args]        -- start execution of a Java class");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
        out.println("run                       -- re-execute last class run");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
        out.println("load <class> [args]       -- start execution of a Java class, initially suspended");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
        out.println("load                      -- re-execute last class run, initially suspended");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
        out.println("attach <portname>         -- debug existing process\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
        out.println("detach                    -- detach from debuggee process\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
        out.println("kill <thread(group)>      -- kill a thread or threadgroup\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
        out.println("!!                        -- repeat last command");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
        out.println("help (or ?)               -- list commands");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
        out.println("exit (or quit)            -- exit debugger");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
     * Execute a command.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
    public void executeCommand(String command) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
        //### Treatment of 'out' here is dirty...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
        out = env.getOutputSink();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
        if (echo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
            out.println(">>> " + command);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
        StringTokenizer t = new StringTokenizer(command);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
            String cmd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
            if (t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
                cmd = t.nextToken().toLowerCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
                lastCommand = cmd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
                cmd = lastCommand;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
            if (cmd.equals("print")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
                commandPrint(t, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
            } else if (cmd.equals("eval")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
                commandPrint(t, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
            } else if (cmd.equals("dump")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
                commandPrint(t, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
            } else if (cmd.equals("locals")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
                commandLocals();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
            } else if (cmd.equals("classes")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
                commandClasses();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
            } else if (cmd.equals("methods")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
                commandMethods(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
            } else if (cmd.equals("threads")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
                commandThreads(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
            } else if (cmd.equals("thread")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
                commandThread(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
            } else if (cmd.equals("suspend")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
                commandSuspend(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
            } else if (cmd.equals("resume")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
                commandResume(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
            } else if (cmd.equals("cont")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
                commandCont();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
            } else if (cmd.equals("threadgroups")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
                commandThreadGroups();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
            } else if (cmd.equals("threadgroup")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
                commandThreadGroup(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
            } else if (cmd.equals("run")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
                commandRun(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
            } else if (cmd.equals("load")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
                commandLoad(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
            } else if (cmd.equals("connect")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
                commandConnect(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
            } else if (cmd.equals("attach")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
                commandAttach(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
            } else if (cmd.equals("detach")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
                commandDetach(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
            } else if (cmd.equals("interrupt")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
                commandInterrupt(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
//### Not implemented.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
//          } else if (cmd.equals("catch")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
//              commandCatchException(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
//### Not implemented.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
//          } else if (cmd.equals("ignore")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
//              commandIgnoreException(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
            } else if (cmd.equals("step")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
                commandStep(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
            } else if (cmd.equals("stepi")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
                commandStepi();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
            } else if (cmd.equals("next")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
                commandNext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
            } else if (cmd.equals("nexti")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
                commandNexti();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
            } else if (cmd.equals("kill")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
                commandKill(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
            } else if (cmd.equals("where")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
                commandWhere(t, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
            } else if (cmd.equals("wherei")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
                commandWhere(t, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
            } else if (cmd.equals("up")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
                commandUp(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
            } else if (cmd.equals("down")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
                commandDown(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
            } else if (cmd.equals("frame")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
                commandFrame(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
            } else if (cmd.equals("stop")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
                commandStop(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
            } else if (cmd.equals("clear")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
                commandClear(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
            } else if (cmd.equals("list")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
                commandList(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
            } else if (cmd.equals("use")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
                commandUse(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
            } else if (cmd.equals("sourcepath")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
                commandSourcepath(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
            } else if (cmd.equals("classpath")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
                commandClasspath(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
            } else if (cmd.equals("monitor")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
                commandMonitor(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
            } else if (cmd.equals("unmonitor")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
                commandUnmonitor(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
            } else if (cmd.equals("view")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
                commandView(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
//          } else if (cmd.equals("read")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
//              readCommand(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
            } else if (cmd.equals("help") || cmd.equals("?")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
                help();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
            } else if (cmd.equals("quit") || cmd.equals("exit")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
                    runtime.detach();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
                } catch (NoSessionException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
                    // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
                env.terminate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
                //### Dubious repeat-count feature inherited from 'jdb'
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
                if (t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
                        int repeat = Integer.parseInt(cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
                        String subcom = t.nextToken("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
                        while (repeat-- > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
                            executeCommand(subcom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
                    } catch (NumberFormatException exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
                out.println("huh? Try help...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
                out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
        } catch (NoSessionException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
            out.println("There is no currently attached VM session.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
            out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
            out.println("Internal exception: " + e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
            out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
            System.out.println("JDB internal exception: " + e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
        out.show();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
}