jdk/src/share/classes/com/sun/tools/example/debug/tty/Commands.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.tty;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import com.sun.jdi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import com.sun.jdi.connect.Connector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import com.sun.jdi.request.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import com.sun.tools.example.debug.expr.ExpressionParser;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import com.sun.tools.example.debug.expr.ParseException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.text.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
class Commands {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    abstract class AsyncExecution {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        abstract void action();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        AsyncExecution() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
            execute();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        void execute() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
             * Save current thread and stack frame. (BugId 4296031)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            final ThreadInfo threadInfo = ThreadInfo.getCurrentThreadInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            final int stackFrame = threadInfo == null? 0 : threadInfo.getCurrentFrameIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            Thread thread = new Thread("asynchronous jdb command") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                            action();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                        } catch (UnsupportedOperationException uoe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                            //(BugId 4453329)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                            MessageOutput.println("Operation is not supported on the target VM");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                            MessageOutput.println("Internal exception during operation:",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                                                  e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                             * This was an asynchronous command.  Events may have been
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                             * processed while it was running.  Restore the thread and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                             * stack frame the user was looking at.  (BugId 4296031)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                            if (threadInfo != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                                ThreadInfo.setCurrentThreadInfo(threadInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                                    threadInfo.setCurrentFrameIndex(stackFrame);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                                } catch (IncompatibleThreadStateException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                                    MessageOutput.println("Current thread isnt suspended.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                                } catch (ArrayIndexOutOfBoundsException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                                    MessageOutput.println("Requested stack frame is no longer active:",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                                                          new Object []{new Integer(stackFrame)});
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                            MessageOutput.printPrompt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            thread.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    Commands() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    private Value evaluate(String expr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        Value result = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        ExpressionParser.GetFrame frameGetter = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            final ThreadInfo threadInfo = ThreadInfo.getCurrentThreadInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            if ((threadInfo != null) && (threadInfo.getCurrentFrame() != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                frameGetter = new ExpressionParser.GetFrame() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                        public StackFrame get() throws IncompatibleThreadStateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                            return threadInfo.getCurrentFrame();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            result = ExpressionParser.evaluate(expr, Env.vm(), frameGetter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        } catch (InvocationException ie) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            MessageOutput.println("Exception in expression:",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                                  ie.exception().referenceType().name());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            String exMessage = ex.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            if (exMessage == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                MessageOutput.printException(exMessage, ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                String s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                    s = MessageOutput.format(exMessage);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                } catch (MissingResourceException mex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                    s = ex.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                MessageOutput.printDirectln(s);// Special case: use printDirectln()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        return result;
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 String getStringValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
         Value val = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
         String valStr = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
         try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
              val = ExpressionParser.getMassagedValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
              valStr = val.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
         } catch (ParseException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
              String msg = e.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
              if (msg == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                  MessageOutput.printException(msg, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
              } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                  String s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                  try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                      s = MessageOutput.format(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                  } catch (MissingResourceException mex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                      s = e.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                  MessageOutput.printDirectln(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
              }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
         return valStr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    private ThreadInfo doGetThread(String idToken) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        ThreadInfo threadInfo = ThreadInfo.getThreadInfo(idToken);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        if (threadInfo == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            MessageOutput.println("is not a valid thread id", idToken);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        return threadInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    String typedName(Method method) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        StringBuffer buf = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        buf.append(method.name());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        buf.append("(");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   160
        List<String> args = method.argumentTypeNames();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        int lastParam = args.size() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        // output param types except for the last
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        for (int ii = 0; ii < lastParam; ii++) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   164
            buf.append(args.get(ii));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            buf.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        if (lastParam >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            // output the last param
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   169
            String lastStr = args.get(lastParam);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            if (method.isVarArgs()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                // lastParam is an array.  Replace the [] with ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                buf.append(lastStr.substring(0, lastStr.length() - 2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                buf.append("...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                buf.append(lastStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        buf.append(")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        return buf.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    void commandConnectors(VirtualMachineManager vmm) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   183
        Collection<Connector> ccs = vmm.allConnectors();
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   184
        if (ccs.isEmpty()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            MessageOutput.println("Connectors available");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        }
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   187
        for (Connector cc : ccs) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            String transportName =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                cc.transport() == null ? "null" : cc.transport().name();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            MessageOutput.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            MessageOutput.println("Connector and Transport name",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                                  new Object [] {cc.name(), transportName});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            MessageOutput.println("Connector description", cc.description());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   195
            for (Connector.Argument aa : cc.defaultArguments().values()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                    MessageOutput.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                    boolean requiredArgument = aa.mustSpecify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                    if (aa.value() == null || aa.value() == "") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                        //no current value and no default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                        MessageOutput.println(requiredArgument ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                                              "Connector required argument nodefault" :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                                              "Connector argument nodefault", aa.name());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                        MessageOutput.println(requiredArgument ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                                              "Connector required argument default" :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                                              "Connector argument default",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                                              new Object [] {aa.name(), aa.value()});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                    MessageOutput.println("Connector description", aa.description());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    void commandClasses() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        StringBuffer classList = new StringBuffer();
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   219
        for (ReferenceType refType : Env.vm().allClasses()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            classList.append(refType.name());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            classList.append("\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        MessageOutput.print("** classes list **", classList.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    void commandClass(StringTokenizer t) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   227
        List<ReferenceType> list = Env.vm().allClasses();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        if (!t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            MessageOutput.println("No class specified.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        String idClass = t.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        boolean showAll = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        if (t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            if (t.nextToken().toLowerCase().equals("all")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                showAll = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                MessageOutput.println("Invalid option on class command");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        ReferenceType type = Env.getReferenceTypeFromToken(idClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        if (type == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            MessageOutput.println("is not a valid id or class name", idClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        if (type instanceof ClassType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            ClassType clazz = (ClassType)type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            MessageOutput.println("Class:", clazz.name());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            ClassType superclass = clazz.superclass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            while (superclass != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                MessageOutput.println("extends:", superclass.name());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                superclass = showAll ? superclass.superclass() : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   260
            List<InterfaceType> interfaces =
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   261
                showAll ? clazz.allInterfaces() : clazz.interfaces();
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   262
            for (InterfaceType interfaze : interfaces) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                MessageOutput.println("implements:", interfaze.name());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   266
            for (ClassType sub : clazz.subclasses()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                MessageOutput.println("subclass:", sub.name());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            }
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   269
            for (ReferenceType nest : clazz.nestedTypes()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                MessageOutput.println("nested:", nest.name());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        } else if (type instanceof InterfaceType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            InterfaceType interfaze = (InterfaceType)type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            MessageOutput.println("Interface:", interfaze.name());
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   275
            for (InterfaceType superinterface : interfaze.superinterfaces()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                MessageOutput.println("extends:", superinterface.name());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            }
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   278
            for (InterfaceType sub : interfaze.subinterfaces()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                MessageOutput.println("subinterface:", sub.name());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            }
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   281
            for (ClassType implementor : interfaze.implementors()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                MessageOutput.println("implementor:", implementor.name());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            }
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   284
            for (ReferenceType nest : interfaze.nestedTypes()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                MessageOutput.println("nested:", nest.name());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        } else {  // array type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            ArrayType array = (ArrayType)type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            MessageOutput.println("Array:", array.name());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    void commandMethods(StringTokenizer t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        if (!t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            MessageOutput.println("No class specified.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        String idClass = t.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        ReferenceType cls = Env.getReferenceTypeFromToken(idClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        if (cls != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            StringBuffer methodsList = new StringBuffer();
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   303
            for (Method method : cls.allMethods()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                methodsList.append(method.declaringType().name());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                methodsList.append(" ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                methodsList.append(typedName(method));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                methodsList.append('\n');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            MessageOutput.print("** methods list **", methodsList.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            MessageOutput.println("is not a valid id or class name", idClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    void commandFields(StringTokenizer t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        if (!t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            MessageOutput.println("No class specified.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        String idClass = t.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        ReferenceType cls = Env.getReferenceTypeFromToken(idClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        if (cls != null) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   324
            List<Field> fields = cls.allFields();
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   325
            List<Field> visible = cls.visibleFields();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            StringBuffer fieldsList = new StringBuffer();
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   327
            for (Field field : fields) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                String s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                if (!visible.contains(field)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                    s = MessageOutput.format("list field typename and name hidden",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                                             new Object [] {field.typeName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                                                            field.name()});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                } else if (!field.declaringType().equals(cls)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                    s = MessageOutput.format("list field typename and name inherited",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                                             new Object [] {field.typeName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                                                            field.name(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                                                            field.declaringType().name()});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                    s = MessageOutput.format("list field typename and name",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                                             new Object [] {field.typeName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                                                            field.name()});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                fieldsList.append(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            MessageOutput.print("** fields list **", fieldsList.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            MessageOutput.println("is not a valid id or class name", idClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        }
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 void printThreadGroup(ThreadGroupReference tg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        ThreadIterator threadIter = new ThreadIterator(tg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        MessageOutput.println("Thread Group:", tg.name());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        int maxIdLength = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        int maxNameLength = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        while (threadIter.hasNext()) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   358
            ThreadReference thr = threadIter.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            maxIdLength = Math.max(maxIdLength,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                                   Env.description(thr).length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            maxNameLength = Math.max(maxNameLength,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                                     thr.name().length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        threadIter = new ThreadIterator(tg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        while (threadIter.hasNext()) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   367
            ThreadReference thr = threadIter.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            if (thr.threadGroup() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            // Note any thread group changes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            if (!thr.threadGroup().equals(tg)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                tg = thr.threadGroup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                MessageOutput.println("Thread Group:", tg.name());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
             * Do a bit of filling with whitespace to get thread ID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
             * and thread names to line up in the listing, and also
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
             * allow for proper localization.  This also works for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
             * very long thread names, at the possible cost of lines
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
             * being wrapped by the display device.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            StringBuffer idBuffer = new StringBuffer(Env.description(thr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            for (int i = idBuffer.length(); i < maxIdLength; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                idBuffer.append(" ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            StringBuffer nameBuffer = new StringBuffer(thr.name());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            for (int i = nameBuffer.length(); i < maxNameLength; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                nameBuffer.append(" ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
             * Select the output format to use based on thread status
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
             * and breakpoint.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            String statusFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            switch (thr.status()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            case ThreadReference.THREAD_STATUS_UNKNOWN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                if (thr.isAtBreakpoint()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                    statusFormat = "Thread description name unknownStatus BP";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                    statusFormat = "Thread description name unknownStatus";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            case ThreadReference.THREAD_STATUS_ZOMBIE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                if (thr.isAtBreakpoint()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                    statusFormat = "Thread description name zombieStatus BP";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                    statusFormat = "Thread description name zombieStatus";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            case ThreadReference.THREAD_STATUS_RUNNING:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                if (thr.isAtBreakpoint()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                    statusFormat = "Thread description name runningStatus BP";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                    statusFormat = "Thread description name runningStatus";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            case ThreadReference.THREAD_STATUS_SLEEPING:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                if (thr.isAtBreakpoint()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                    statusFormat = "Thread description name sleepingStatus BP";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                    statusFormat = "Thread description name sleepingStatus";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            case ThreadReference.THREAD_STATUS_MONITOR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                if (thr.isAtBreakpoint()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                    statusFormat = "Thread description name waitingStatus BP";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                    statusFormat = "Thread description name waitingStatus";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            case ThreadReference.THREAD_STATUS_WAIT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                if (thr.isAtBreakpoint()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                    statusFormat = "Thread description name condWaitstatus BP";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                    statusFormat = "Thread description name condWaitstatus";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                throw new InternalError(MessageOutput.format("Invalid thread status."));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            MessageOutput.println(statusFormat,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                                  new Object [] {idBuffer.toString(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                                                 nameBuffer.toString()});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    void commandThreads(StringTokenizer t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        if (!t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            printThreadGroup(ThreadInfo.group());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        String name = t.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        ThreadGroupReference tg = ThreadGroupIterator.find(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        if (tg == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            MessageOutput.println("is not a valid threadgroup name", name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            printThreadGroup(tg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    void commandThreadGroups() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        ThreadGroupIterator it = new ThreadGroupIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        int cnt = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        while (it.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            ThreadGroupReference tg = it.nextThreadGroup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            ++cnt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            MessageOutput.println("thread group number description name",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                                  new Object [] { new Integer (cnt),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                                                  Env.description(tg),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                                                  tg.name()});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    void commandThread(StringTokenizer t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        if (!t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            MessageOutput.println("Thread number not specified.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        ThreadInfo threadInfo = doGetThread(t.nextToken());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        if (threadInfo != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            ThreadInfo.setCurrentThreadInfo(threadInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    void commandThreadGroup(StringTokenizer t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        if (!t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            MessageOutput.println("Threadgroup name not specified.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        String name = t.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        ThreadGroupReference tg = ThreadGroupIterator.find(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        if (tg == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            MessageOutput.println("is not a valid threadgroup name", name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            ThreadInfo.setThreadGroup(tg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    void commandRun(StringTokenizer t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
         * The 'run' command makes little sense in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
         * that doesn't support restarts or multiple VMs. However,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
         * this is an attempt to emulate the behavior of the old
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
         * JDB as much as possible. For new users and implementations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
         * it is much more straightforward to launch immedidately
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
         * with the -launch option.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        VMConnection connection = Env.connection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        if (!connection.isLaunch()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            if (!t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                commandCont();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                MessageOutput.println("run <args> command is valid only with launched VMs");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        if (connection.isOpen()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            MessageOutput.println("VM already running. use cont to continue after events.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
         * Set the main class and any arguments. Note that this will work
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
         * only with the standard launcher, "com.sun.jdi.CommandLineLauncher"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        String args;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        if (t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            args = t.nextToken("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            boolean argsSet = connection.setConnectorArg("main", args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            if (!argsSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                MessageOutput.println("Unable to set main class and arguments");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            args = connection.connectorArg("main");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            if (args.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                MessageOutput.println("Main class and arguments must be specified");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        MessageOutput.println("run", args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
         * Launch the VM.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        connection.open();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    void commandLoad(StringTokenizer t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        MessageOutput.println("The load command is no longer supported.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    private List<ThreadReference> allThreads(ThreadGroupReference group) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        List<ThreadReference> list = new ArrayList<ThreadReference>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        list.addAll(group.threads());
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   560
        for (ThreadGroupReference child : group.threadGroups()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            list.addAll(allThreads(child));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        return list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    void commandSuspend(StringTokenizer t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        if (!t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            Env.vm().suspend();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            MessageOutput.println("All threads suspended.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            while (t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                ThreadInfo threadInfo = doGetThread(t.nextToken());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                if (threadInfo != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                    threadInfo.getThread().suspend();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    void commandResume(StringTokenizer t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
         if (!t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
             ThreadInfo.invalidateAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
             Env.vm().resume();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
             MessageOutput.println("All threads resumed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
         } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
             while (t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                ThreadInfo threadInfo = doGetThread(t.nextToken());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                if (threadInfo != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                    threadInfo.invalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                    threadInfo.getThread().resume();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    void commandCont() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        if (ThreadInfo.getCurrentThreadInfo() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            MessageOutput.println("Nothing suspended.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        ThreadInfo.invalidateAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        Env.vm().resume();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    void clearPreviousStep(ThreadReference thread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
         * A previous step may not have completed on this thread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
         * if so, it gets removed here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
         EventRequestManager mgr = Env.vm().eventRequestManager();
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   611
         for (StepRequest request : mgr.stepRequests()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
             if (request.thread().equals(thread)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                 mgr.deleteEventRequest(request);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                 break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
    /* step
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
    void commandStep(StringTokenizer t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        ThreadInfo threadInfo = ThreadInfo.getCurrentThreadInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        if (threadInfo == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            MessageOutput.println("Nothing suspended.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        int depth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        if (t.hasMoreTokens() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                  t.nextToken().toLowerCase().equals("up")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
            depth = StepRequest.STEP_OUT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
            depth = StepRequest.STEP_INTO;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        clearPreviousStep(threadInfo.getThread());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        EventRequestManager reqMgr = Env.vm().eventRequestManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        StepRequest request = reqMgr.createStepRequest(threadInfo.getThread(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                                                       StepRequest.STEP_LINE, depth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        if (depth == StepRequest.STEP_INTO) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            Env.addExcludes(request);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        // We want just the next step event and no others
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        request.addCountFilter(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        request.enable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        ThreadInfo.invalidateAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        Env.vm().resume();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
    /* stepi
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * step instruction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
    void commandStepi() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        ThreadInfo threadInfo = ThreadInfo.getCurrentThreadInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        if (threadInfo == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            MessageOutput.println("Nothing suspended.");
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
        clearPreviousStep(threadInfo.getThread());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        EventRequestManager reqMgr = Env.vm().eventRequestManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        StepRequest request = reqMgr.createStepRequest(threadInfo.getThread(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                                                       StepRequest.STEP_MIN,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                                                       StepRequest.STEP_INTO);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        Env.addExcludes(request);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        // We want just the next step event and no others
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        request.addCountFilter(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        request.enable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        ThreadInfo.invalidateAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        Env.vm().resume();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
    void commandNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        ThreadInfo threadInfo = ThreadInfo.getCurrentThreadInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        if (threadInfo == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
            MessageOutput.println("Nothing suspended.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        clearPreviousStep(threadInfo.getThread());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        EventRequestManager reqMgr = Env.vm().eventRequestManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        StepRequest request = reqMgr.createStepRequest(threadInfo.getThread(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                                                       StepRequest.STEP_LINE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                                                       StepRequest.STEP_OVER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        Env.addExcludes(request);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        // We want just the next step event and no others
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        request.addCountFilter(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        request.enable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        ThreadInfo.invalidateAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        Env.vm().resume();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
    void doKill(ThreadReference thread, StringTokenizer t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        if (!t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
            MessageOutput.println("No exception object specified.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        String expr = t.nextToken("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        Value val = evaluate(expr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        if ((val != null) && (val instanceof ObjectReference)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                thread.stop((ObjectReference)val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                MessageOutput.println("killed", thread.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
            } catch (InvalidTypeException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                MessageOutput.println("Invalid exception object");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
            MessageOutput.println("Expression must evaluate to an object");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
    void doKillThread(final ThreadReference threadToKill,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                      final StringTokenizer tokenizer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        new AsyncExecution() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                void action() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                    doKill(threadToKill, tokenizer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
    void commandKill(StringTokenizer t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        if (!t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
            MessageOutput.println("Usage: kill <thread id> <throwable>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        ThreadInfo threadInfo = doGetThread(t.nextToken());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        if (threadInfo != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
            MessageOutput.println("killing thread:", threadInfo.getThread().name());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            doKillThread(threadInfo.getThread(), t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
    void listCaughtExceptions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        boolean noExceptions = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        // Print a listing of the catch patterns currently in place
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   735
        for (EventRequestSpec spec : Env.specList.eventRequestSpecs()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            if (spec instanceof ExceptionSpec) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                if (noExceptions) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                    noExceptions = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                    MessageOutput.println("Exceptions caught:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                MessageOutput.println("tab", spec.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        if (noExceptions) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
            MessageOutput.println("No exceptions caught.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
    private EventRequestSpec parseExceptionSpec(StringTokenizer t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        String notification = t.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        boolean notifyCaught = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        boolean notifyUncaught = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        EventRequestSpec spec = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        String classPattern = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        if (notification.equals("uncaught")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
            notifyCaught = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
            notifyUncaught = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        } else if (notification.equals("caught")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
            notifyCaught = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
            notifyUncaught = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        } else if (notification.equals("all")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
            notifyCaught = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            notifyUncaught = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
             * Handle the same as "all" for backward
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
             * compatibility with existing .jdbrc files.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
             * Insert an "all" and take the current token as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
             * intended classPattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            notifyCaught = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
            notifyUncaught = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
            classPattern = notification;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        if (classPattern == null && t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            classPattern = t.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        if ((classPattern != null) && (notifyCaught || notifyUncaught)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                spec = Env.specList.createExceptionCatch(classPattern,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                                                         notifyCaught,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                                                         notifyUncaught);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
            } catch (ClassNotFoundException exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                MessageOutput.println("is not a valid class name", classPattern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        return spec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
    void commandCatchException(StringTokenizer t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        if (!t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
            listCaughtExceptions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
            EventRequestSpec spec = parseExceptionSpec(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
            if (spec != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
                resolveNow(spec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
                MessageOutput.println("Usage: catch exception");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
    void commandIgnoreException(StringTokenizer t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        if (!t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
            listCaughtExceptions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
            EventRequestSpec spec = parseExceptionSpec(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
            if (Env.specList.delete(spec)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                MessageOutput.println("Removed:", spec.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                if (spec != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                    MessageOutput.println("Not found:", spec.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
                MessageOutput.println("Usage: ignore exception");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
    void commandUp(StringTokenizer t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        ThreadInfo threadInfo = ThreadInfo.getCurrentThreadInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        if (threadInfo == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
            MessageOutput.println("Current thread not set.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        int nLevels = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
        if (t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
            String idToken = t.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
            int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
                NumberFormat nf = NumberFormat.getNumberInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
                nf.setParseIntegerOnly(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
                Number n = nf.parse(idToken);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
                i = n.intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
            } catch (java.text.ParseException jtpe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
                i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
            if (i <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                MessageOutput.println("Usage: up [n frames]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
            nLevels = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
            threadInfo.up(nLevels);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        } catch (IncompatibleThreadStateException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
            MessageOutput.println("Current thread isnt suspended.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
        } catch (ArrayIndexOutOfBoundsException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
            MessageOutput.println("End of stack.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
    void commandDown(StringTokenizer t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        ThreadInfo threadInfo = ThreadInfo.getCurrentThreadInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
        if (threadInfo == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
            MessageOutput.println("Current thread not set.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        int nLevels = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
        if (t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
            String idToken = t.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
            int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
                NumberFormat nf = NumberFormat.getNumberInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
                nf.setParseIntegerOnly(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
                Number n = nf.parse(idToken);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
                i = n.intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
            } catch (java.text.ParseException jtpe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
                i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
            if (i <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
                MessageOutput.println("Usage: down [n frames]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
            nLevels = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
            threadInfo.down(nLevels);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
        } catch (IncompatibleThreadStateException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
            MessageOutput.println("Current thread isnt suspended.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        } catch (ArrayIndexOutOfBoundsException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
            MessageOutput.println("End of stack.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
    private void dumpStack(ThreadInfo threadInfo, boolean showPC) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   893
        List<StackFrame> stack = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
            stack = threadInfo.getStack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
        } catch (IncompatibleThreadStateException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
            MessageOutput.println("Current thread isnt suspended.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
        if (stack == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
            MessageOutput.println("Thread is not running (no stack).");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
            int nFrames = stack.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
            for (int i = threadInfo.getCurrentFrameIndex(); i < nFrames; i++) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   905
                StackFrame frame = stack.get(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
                dumpFrame (i, showPC, frame);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
    private void dumpFrame (int frameNumber, boolean showPC, StackFrame frame) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        Location loc = frame.location();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        long pc = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        if (showPC) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
            pc = loc.codeIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
        Method meth = loc.method();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
        long lineNumber = loc.lineNumber();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
        String methodInfo = null;
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   921
        if (meth.isNative()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
            methodInfo = MessageOutput.format("native method");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        } else if (lineNumber != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
                methodInfo = loc.sourceName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
                    MessageOutput.format("line number",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
                                         new Object [] {new Long(lineNumber)});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
            } catch (AbsentInformationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
                methodInfo = MessageOutput.format("unknown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
        if (pc != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
            MessageOutput.println("stack frame dump with pc",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
                                  new Object [] {new Integer(frameNumber + 1),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
                                                 meth.declaringType().name(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
                                                 meth.name(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
                                                 methodInfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
                                                 new Long(pc)});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
            MessageOutput.println("stack frame dump",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
                                  new Object [] {new Integer(frameNumber + 1),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
                                                 meth.declaringType().name(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
                                                 meth.name(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
                                                 methodInfo});
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
    void commandWhere(StringTokenizer t, boolean showPC) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
        if (!t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
            ThreadInfo threadInfo = ThreadInfo.getCurrentThreadInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
            if (threadInfo == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
                MessageOutput.println("No thread specified.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
            dumpStack(threadInfo, showPC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
            String token = t.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
            if (token.toLowerCase().equals("all")) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   959
                for (ThreadInfo threadInfo : ThreadInfo.threads()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
                    MessageOutput.println("Thread:",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
                                          threadInfo.getThread().name());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
                    dumpStack(threadInfo, showPC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
                ThreadInfo threadInfo = doGetThread(token);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
                if (threadInfo != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
                    ThreadInfo.setCurrentThreadInfo(threadInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
                    dumpStack(threadInfo, showPC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
    void commandInterrupt(StringTokenizer t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
        if (!t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
            ThreadInfo threadInfo = ThreadInfo.getCurrentThreadInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
            if (threadInfo == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
                MessageOutput.println("No thread specified.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
            threadInfo.getThread().interrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
            ThreadInfo threadInfo = doGetThread(t.nextToken());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
            if (threadInfo != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
                threadInfo.getThread().interrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
    void commandMemory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
        MessageOutput.println("The memory command is no longer supported.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
    void commandGC() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
        MessageOutput.println("The gc command is no longer necessary.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
     * The next two methods are used by this class and by EventHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
     * to print consistent locations and error messages.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
    static String locationString(Location loc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
        return MessageOutput.format("locationString",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
                                    new Object [] {loc.declaringType().name(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
                                                   loc.method().name(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
                                                   new Integer (loc.lineNumber()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
                                                   new Long (loc.codeIndex())});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
    void listBreakpoints() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
        boolean noBreakpoints = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
        // Print set breakpoints
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1014
        for (EventRequestSpec spec : Env.specList.eventRequestSpecs()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
            if (spec instanceof BreakpointSpec) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
                if (noBreakpoints) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
                    noBreakpoints = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
                    MessageOutput.println("Breakpoints set:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
                MessageOutput.println("tab", spec.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
        if (noBreakpoints) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
            MessageOutput.println("No breakpoints set.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
    private void printBreakpointCommandUsage(String atForm, String inForm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
        MessageOutput.println("printbreakpointcommandusage",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
                              new Object [] {atForm, inForm});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
    protected BreakpointSpec parseBreakpointSpec(StringTokenizer t,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
                                             String atForm, String inForm) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1036
        BreakpointSpec breakpoint = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
            String token = t.nextToken(":( \t\n\r");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
            // We can't use hasMoreTokens here because it will cause any leading
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
            // paren to be lost.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
            String rest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
                rest = t.nextToken("").trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
            } catch (NoSuchElementException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
                rest = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
            if ((rest != null) && rest.startsWith(":")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
                t = new StringTokenizer(rest.substring(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
                String classId = token;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
                String lineToken = t.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
                NumberFormat nf = NumberFormat.getNumberInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
                nf.setParseIntegerOnly(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
                Number n = nf.parse(lineToken);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
                int lineNumber = n.intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
                if (t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
                    printBreakpointCommandUsage(atForm, inForm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
                    breakpoint = Env.specList.createBreakpoint(classId,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
                                                               lineNumber);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
                } catch (ClassNotFoundException exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
                    MessageOutput.println("is not a valid class name", classId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
                // Try stripping method from class.method token.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
                int idot = token.lastIndexOf(".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
                if ( (idot <= 0) ||                     /* No dot or dot in first char */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
                     (idot >= token.length() - 1) ) { /* dot in last char */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
                    printBreakpointCommandUsage(atForm, inForm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
                String methodName = token.substring(idot + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
                String classId = token.substring(0, idot);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
                List<String> argumentList = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
                if (rest != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
                    if (!rest.startsWith("(") || !rest.endsWith(")")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
                        MessageOutput.println("Invalid method specification:",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
                                              methodName + rest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
                        printBreakpointCommandUsage(atForm, inForm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
                    // Trim the parens
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
                    rest = rest.substring(1, rest.length() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
                    argumentList = new ArrayList<String>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
                    t = new StringTokenizer(rest, ",");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
                    while (t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
                        argumentList.add(t.nextToken());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
                    breakpoint = Env.specList.createBreakpoint(classId,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
                                                               methodName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
                                                               argumentList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
                } catch (MalformedMemberNameException exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
                    MessageOutput.println("is not a valid method name", methodName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
                } catch (ClassNotFoundException exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
                    MessageOutput.println("is not a valid class name", classId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
            printBreakpointCommandUsage(atForm, inForm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
        }
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1110
        return breakpoint;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
    private void resolveNow(EventRequestSpec spec) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
        boolean success = Env.specList.addEagerlyResolve(spec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
        if (success && !spec.isResolved()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
            MessageOutput.println("Deferring.", spec.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
    void commandStop(StringTokenizer t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
        Location bploc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
        String atIn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
        byte suspendPolicy = EventRequest.SUSPEND_ALL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
        if (t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
            atIn = t.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
            if (atIn.equals("go") && t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
                suspendPolicy = EventRequest.SUSPEND_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
                atIn = t.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
            } else if (atIn.equals("thread") && t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
                suspendPolicy = EventRequest.SUSPEND_EVENT_THREAD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
                atIn = t.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
            listBreakpoints();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
        BreakpointSpec spec = parseBreakpointSpec(t, "stop at", "stop in");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
        if (spec != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
            // Enforcement of "at" vs. "in". The distinction is really
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
            // unnecessary and we should consider not checking for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
            // (and making "at" and "in" optional).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
            if (atIn.equals("at") && spec.isMethodBreakpoint()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
                MessageOutput.println("Use stop at to set a breakpoint at a line number");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
                printBreakpointCommandUsage("stop at", "stop in");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
            spec.suspendPolicy = suspendPolicy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
            resolveNow(spec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
    void commandClear(StringTokenizer t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
        if (!t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
            listBreakpoints();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
        BreakpointSpec spec = parseBreakpointSpec(t, "clear", "clear");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
        if (spec != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
            if (Env.specList.delete(spec)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
                MessageOutput.println("Removed:", spec.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
                MessageOutput.println("Not found:", spec.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1170
    private List<WatchpointSpec> parseWatchpointSpec(StringTokenizer t) {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1171
        List<WatchpointSpec> list = new ArrayList<WatchpointSpec>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
        boolean access = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
        boolean modification = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
        int suspendPolicy = EventRequest.SUSPEND_ALL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
        String fieldName = t.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
        if (fieldName.equals("go")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
            suspendPolicy = EventRequest.SUSPEND_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
            fieldName = t.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
        } else if (fieldName.equals("thread")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
            suspendPolicy = EventRequest.SUSPEND_EVENT_THREAD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
            fieldName = t.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
        if (fieldName.equals("access")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
            access = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
            fieldName = t.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
        } else if (fieldName.equals("all")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
            access = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
            modification = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
            fieldName = t.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
            modification = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
        int dot = fieldName.lastIndexOf('.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
        if (dot < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
            MessageOutput.println("Class containing field must be specified.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
            return list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
        String className = fieldName.substring(0, dot);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
        fieldName = fieldName.substring(dot+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
        try {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1203
            WatchpointSpec spec;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
            if (access) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
                spec = Env.specList.createAccessWatchpoint(className,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
                                                           fieldName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
                spec.suspendPolicy = suspendPolicy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
                list.add(spec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
            if (modification) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
                spec = Env.specList.createModificationWatchpoint(className,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
                                                                 fieldName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
                spec.suspendPolicy = suspendPolicy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
                list.add(spec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
        } catch (MalformedMemberNameException exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
            MessageOutput.println("is not a valid field name", fieldName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
        } catch (ClassNotFoundException exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
            MessageOutput.println("is not a valid class name", className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
        return list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
    void commandWatch(StringTokenizer t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
        if (!t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
            MessageOutput.println("Field to watch not specified");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1230
        for (WatchpointSpec spec : parseWatchpointSpec(t)) {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1231
            resolveNow(spec);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
    void commandUnwatch(StringTokenizer t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
        if (!t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
            MessageOutput.println("Field to unwatch not specified");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1241
        for (WatchpointSpec spec : parseWatchpointSpec(t)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
            if (Env.specList.delete(spec)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
                MessageOutput.println("Removed:", spec.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
                MessageOutput.println("Not found:", spec.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
    void turnOnExitTrace(ThreadInfo threadInfo, int suspendPolicy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
        EventRequestManager erm = Env.vm().eventRequestManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
        MethodExitRequest exit = erm.createMethodExitRequest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
        if (threadInfo != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
            exit.addThreadFilter(threadInfo.getThread());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
        Env.addExcludes(exit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
        exit.setSuspendPolicy(suspendPolicy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
        exit.enable();
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
    static String methodTraceCommand = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
    void commandTrace(StringTokenizer t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
        String modif;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
        int suspendPolicy = EventRequest.SUSPEND_ALL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
        ThreadInfo threadInfo = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
        String goStr = " ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
         * trace [go] methods [thread]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
         * trace [go] method exit | exits [thread]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
        if (t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
            modif = t.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
            if (modif.equals("go")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
                suspendPolicy = EventRequest.SUSPEND_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
                goStr = " go ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
                if (t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
                    modif = t.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
            } else if (modif.equals("thread")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
                // this is undocumented as it doesn't work right.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
                suspendPolicy = EventRequest.SUSPEND_EVENT_THREAD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
                if (t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
                    modif = t.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
            if  (modif.equals("method")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
                String traceCmd = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
                if (t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
                    String modif1 = t.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
                    if (modif1.equals("exits") || modif1.equals("exit")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
                        if (t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
                            threadInfo = doGetThread(t.nextToken());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
                        if (modif1.equals("exit")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
                            StackFrame frame;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
                            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
                                frame = ThreadInfo.getCurrentThreadInfo().getCurrentFrame();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
                            } catch (IncompatibleThreadStateException ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
                                MessageOutput.println("Current thread isnt suspended.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
                                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
                            Env.setAtExitMethod(frame.location().method());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
                            traceCmd = MessageOutput.format("trace" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
                                                    goStr + "method exit " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
                                                    "in effect for",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
                                                    Env.atExitMethod().toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
                            traceCmd = MessageOutput.format("trace" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
                                                   goStr + "method exits " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
                                                   "in effect");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
                        commandUntrace(new StringTokenizer("methods"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
                        turnOnExitTrace(threadInfo, suspendPolicy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
                        methodTraceCommand = traceCmd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
                   MessageOutput.println("Can only trace");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
                   return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
            if (modif.equals("methods")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
                // Turn on method entry trace
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
                MethodEntryRequest entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
                EventRequestManager erm = Env.vm().eventRequestManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
                if (t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
                    threadInfo = doGetThread(t.nextToken());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
                if (threadInfo != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
                     * To keep things simple we want each 'trace' to cancel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
                     * previous traces.  However in this case, we don't do that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
                     * to preserve backward compatibility with pre JDK 6.0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
                     * IE, you can currently do
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
                     *   trace   methods 0x21
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
                     *   trace   methods 0x22
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
                     * and you will get xxx traced just on those two threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
                     * But this feature is kind of broken because if you then do
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
                     *   untrace  0x21
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
                     * it turns off both traces instead of just the one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
                     * Another bogosity is that if you do
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
                     *   trace methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
                     *   trace methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
                     * and you will get two traces.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
                    entry = erm.createMethodEntryRequest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
                    entry.addThreadFilter(threadInfo.getThread());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
                    commandUntrace(new StringTokenizer("methods"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
                    entry = erm.createMethodEntryRequest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
                Env.addExcludes(entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
                entry.setSuspendPolicy(suspendPolicy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
                entry.enable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
                turnOnExitTrace(threadInfo, suspendPolicy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
                methodTraceCommand = MessageOutput.format("trace" + goStr +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
                                                          "methods in effect");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
            MessageOutput.println("Can only trace");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
        // trace all by itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
        if (methodTraceCommand != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
            MessageOutput.printDirectln(methodTraceCommand);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
        // More trace lines can be added here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
    void commandUntrace(StringTokenizer t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
        // untrace
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
        // untrace methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
        String modif = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
        EventRequestManager erm = Env.vm().eventRequestManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
        if (t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
            modif = t.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
        if (modif == null || modif.equals("methods")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
            erm.deleteEventRequests(erm.methodEntryRequests());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
            erm.deleteEventRequests(erm.methodExitRequests());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
            Env.setAtExitMethod(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
            methodTraceCommand = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
    void commandList(StringTokenizer t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
        StackFrame frame = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
        ThreadInfo threadInfo = ThreadInfo.getCurrentThreadInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
        if (threadInfo == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
            MessageOutput.println("No thread specified.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
            frame = threadInfo.getCurrentFrame();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
        } catch (IncompatibleThreadStateException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
            MessageOutput.println("Current thread isnt suspended.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
        if (frame == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
            MessageOutput.println("No frames on the current call stack");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
        Location loc = frame.location();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
        if (loc.method().isNative()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
            MessageOutput.println("Current method is native");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
        String sourceFileName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
            sourceFileName = loc.sourceName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
            ReferenceType refType = loc.declaringType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
            int lineno = loc.lineNumber();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
            if (t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
                String id = t.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
                // See if token is a line number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
                    NumberFormat nf = NumberFormat.getNumberInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
                    nf.setParseIntegerOnly(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
                    Number n = nf.parse(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
                    lineno = n.intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
                } catch (java.text.ParseException jtpe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
                    // It isn't -- see if it's a method name.
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1440
                        List<Method> meths = refType.methodsByName(id);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
                        if (meths == null || meths.size() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
                            MessageOutput.println("is not a valid line number or method name for",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
                                                  new Object [] {id, refType.name()});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
                            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
                        } else if (meths.size() > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
                            MessageOutput.println("is an ambiguous method name in",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
                                                  new Object [] {id, refType.name()});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
                            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
                        }
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1450
                        loc = meths.get(0).location();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
                        lineno = loc.lineNumber();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
            int startLine = Math.max(lineno - 4, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
            int endLine = startLine + 9;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
            if (lineno < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
                MessageOutput.println("Line number information not available for");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
            } else if (Env.sourceLine(loc, lineno) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
                MessageOutput.println("is an invalid line number for",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
                                      new Object [] {new Integer (lineno),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
                                                     refType.name()});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
                for (int i = startLine; i <= endLine; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
                    String sourceLine = Env.sourceLine(loc, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
                    if (sourceLine == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
                    if (i == lineno) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
                        MessageOutput.println("source line number current line and line",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
                                              new Object [] {new Integer (i),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
                                                             sourceLine});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
                        MessageOutput.println("source line number and line",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
                                              new Object [] {new Integer (i),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
                                                             sourceLine});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
        } catch (AbsentInformationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
            MessageOutput.println("No source information available for:", loc.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
        } catch(FileNotFoundException exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
            MessageOutput.println("Source file not found:", sourceFileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
        } catch(IOException exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
            MessageOutput.println("I/O exception occurred:", exc.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
    void commandLines(StringTokenizer t) { // Undocumented command: useful for testing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
        if (!t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
            MessageOutput.println("Specify class and method");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
            String idClass = t.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
            String idMethod = t.hasMoreTokens() ? t.nextToken() : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
                ReferenceType refType = Env.getReferenceTypeFromToken(idClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
                if (refType != null) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1497
                    List<Location> lines = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
                    if (idMethod == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
                        lines = refType.allLineLocations();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
                    } else {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1501
                        for (Method method : refType.allMethods()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
                            if (method.name().equals(idMethod)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
                                lines = method.allLineLocations();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
                        if (lines == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
                            MessageOutput.println("is not a valid method name", idMethod);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
                    }
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1510
                    for (Location line : lines) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
                        MessageOutput.printDirectln(line.toString());// Special case: use printDirectln()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
                    MessageOutput.println("is not a valid id or class name", idClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
            } catch (AbsentInformationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
                MessageOutput.println("Line number information not available for", idClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
    void commandClasspath(StringTokenizer t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
        if (Env.vm() instanceof PathSearchingVirtualMachine) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
            PathSearchingVirtualMachine vm = (PathSearchingVirtualMachine)Env.vm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
            MessageOutput.println("base directory:", vm.baseDirectory());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
            MessageOutput.println("classpath:", vm.classPath().toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
            MessageOutput.println("bootclasspath:", vm.bootClassPath().toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
            MessageOutput.println("The VM does not use paths");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
    /* Get or set the source file path list. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
    void commandUse(StringTokenizer t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
        if (!t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
            MessageOutput.printDirectln(Env.getSourcePath());// Special case: use printDirectln()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
             * Take the remainder of the command line, minus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
             * leading or trailing whitespace.  Embedded
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
             * whitespace is fine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
            Env.setSourcePath(t.nextToken("").trim());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
    /* Print a stack variable */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
    private void printVar(LocalVariable var, Value value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
        MessageOutput.println("expr is value",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
                              new Object [] {var.name(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
                                             value == null ? "null" : value.toString()});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
    /* Print all local variables in current stack frame. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
    void commandLocals() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
        StackFrame frame;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
        ThreadInfo threadInfo = ThreadInfo.getCurrentThreadInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
        if (threadInfo == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
            MessageOutput.println("No default thread specified:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
            frame = threadInfo.getCurrentFrame();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
            if (frame == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
                throw new AbsentInformationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
            List<LocalVariable> vars = frame.visibleVariables();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
            if (vars.size() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
                MessageOutput.println("No local variables");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
            }
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1573
            Map<LocalVariable, Value> values = frame.getValues(vars);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
            MessageOutput.println("Method arguments:");
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1576
            for (LocalVariable var : vars) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
                if (var.isArgument()) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1578
                    Value val = values.get(var);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
                    printVar(var, val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
            MessageOutput.println("Local variables:");
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1583
            for (LocalVariable var : vars) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
                if (!var.isArgument()) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1585
                    Value val = values.get(var);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
                    printVar(var, val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
        } catch (AbsentInformationException aie) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
            MessageOutput.println("Local variable information not available.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
        } catch (IncompatibleThreadStateException exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
            MessageOutput.println("Current thread isnt suspended.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
    private void dump(ObjectReference obj, ReferenceType refType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
                      ReferenceType refTypeBase) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1598
        for (Field field : refType.fields()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
            StringBuffer o = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
            o.append("    ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
            if (!refType.equals(refTypeBase)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
                o.append(refType.name());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
                o.append(".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
            o.append(field.name());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
            o.append(MessageOutput.format("colon space"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
            o.append(obj.getValue(field));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
            MessageOutput.printDirectln(o.toString()); // Special case: use printDirectln()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
        if (refType instanceof ClassType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
            ClassType sup = ((ClassType)refType).superclass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
            if (sup != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
                dump(obj, sup, refTypeBase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
        } else if (refType instanceof InterfaceType) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1616
            for (InterfaceType sup : ((InterfaceType)refType).superinterfaces()) {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1617
                dump(obj, sup, refTypeBase);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
            /* else refType is an instanceof ArrayType */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
            if (obj instanceof ArrayReference) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1622
                for (Iterator<Value> it = ((ArrayReference)obj).getValues().iterator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
                     it.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
                    MessageOutput.printDirect(it.next().toString());// Special case: use printDirect()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
                    if (it.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
                        MessageOutput.printDirect(", ");// Special case: use printDirect()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
                MessageOutput.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
    /* Print a specified reference.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
    void doPrint(StringTokenizer t, boolean dumpObject) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
        if (!t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
            MessageOutput.println("No objects specified.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
        while (t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
            String expr = t.nextToken("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
            Value val = evaluate(expr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
            if (val == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
                MessageOutput.println("expr is null", expr.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
            } else if (dumpObject && (val instanceof ObjectReference) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
                       !(val instanceof StringReference)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
                ObjectReference obj = (ObjectReference)val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
                ReferenceType refType = obj.referenceType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
                MessageOutput.println("expr is value",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
                                      new Object [] {expr.toString(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
                                                     MessageOutput.format("grouping begin character")});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
                dump(obj, refType, refType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
                MessageOutput.println("grouping end character");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
                  String strVal = getStringValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
                  if (strVal != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
                     MessageOutput.println("expr is value", new Object [] {expr.toString(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
                                                                      strVal});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
                   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
    void commandPrint(final StringTokenizer t, final boolean dumpObject) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
        new AsyncExecution() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
                void action() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
                    doPrint(t, dumpObject);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
    void commandSet(final StringTokenizer t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
        String all = t.nextToken("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
         * Bare bones error checking.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
        if (all.indexOf('=') == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
            MessageOutput.println("Invalid assignment syntax");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
            MessageOutput.printPrompt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
         * The set command is really just syntactic sugar. Pass it on to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
         * print command.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
        commandPrint(new StringTokenizer(all), false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
    void doLock(StringTokenizer t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
        if (!t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
            MessageOutput.println("No object specified.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
        String expr = t.nextToken("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
        Value val = evaluate(expr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
            if ((val != null) && (val instanceof ObjectReference)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
                ObjectReference object = (ObjectReference)val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
                String strVal = getStringValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
                if (strVal != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
                    MessageOutput.println("Monitor information for expr",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
                                      new Object [] {expr.trim(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
                                                     strVal});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
                ThreadReference owner = object.owningThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
                if (owner == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
                    MessageOutput.println("Not owned");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
                    MessageOutput.println("Owned by:",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
                                          new Object [] {owner.name(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
                                                         new Integer (object.entryCount())});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
                }
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1719
                List<ThreadReference> waiters = object.waitingThreads();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
                if (waiters.size() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
                    MessageOutput.println("No waiters");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
                } else {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1723
                    for (ThreadReference waiter : waiters) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
                        MessageOutput.println("Waiting thread:", waiter.name());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
                MessageOutput.println("Expression must evaluate to an object");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
        } catch (IncompatibleThreadStateException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
            MessageOutput.println("Threads must be suspended");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
    void commandLock(final StringTokenizer t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
        new AsyncExecution() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
                void action() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
                    doLock(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
    private void printThreadLockInfo(ThreadInfo threadInfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
        ThreadReference thread = threadInfo.getThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
            MessageOutput.println("Monitor information for thread", thread.name());
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1747
            List<ObjectReference> owned = thread.ownedMonitors();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
            if (owned.size() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
                MessageOutput.println("No monitors owned");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
            } else {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1751
                for (ObjectReference monitor : owned) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
                    MessageOutput.println("Owned monitor:", monitor.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
            ObjectReference waiting = thread.currentContendedMonitor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
            if (waiting == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
                MessageOutput.println("Not waiting for a monitor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
                MessageOutput.println("Waiting for monitor:", waiting.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
        } catch (IncompatibleThreadStateException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
            MessageOutput.println("Threads must be suspended");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
    void commandThreadlocks(final StringTokenizer t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
        if (!t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
            ThreadInfo threadInfo = ThreadInfo.getCurrentThreadInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
            if (threadInfo == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
                MessageOutput.println("Current thread not set.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
                printThreadLockInfo(threadInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
        String token = t.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
        if (token.toLowerCase().equals("all")) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1778
            for (ThreadInfo threadInfo : ThreadInfo.threads()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
                printThreadLockInfo(threadInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
            ThreadInfo threadInfo = doGetThread(token);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
            if (threadInfo != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
                ThreadInfo.setCurrentThreadInfo(threadInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
                printThreadLockInfo(threadInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
    void doDisableGC(StringTokenizer t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
        if (!t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
            MessageOutput.println("No object specified.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
        String expr = t.nextToken("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
        Value val = evaluate(expr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
        if ((val != null) && (val instanceof ObjectReference)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
            ObjectReference object = (ObjectReference)val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
            object.disableCollection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
            String strVal = getStringValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
            if (strVal != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
                 MessageOutput.println("GC Disabled for", strVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
            MessageOutput.println("Expression must evaluate to an object");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
    void commandDisableGC(final StringTokenizer t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
        new AsyncExecution() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
                void action() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
                    doDisableGC(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
    void doEnableGC(StringTokenizer t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
        if (!t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
            MessageOutput.println("No object specified.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
        String expr = t.nextToken("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
        Value val = evaluate(expr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
        if ((val != null) && (val instanceof ObjectReference)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
            ObjectReference object = (ObjectReference)val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
            object.enableCollection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
            String strVal = getStringValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
            if (strVal != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
                 MessageOutput.println("GC Enabled for", strVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
            MessageOutput.println("Expression must evaluate to an object");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
    void commandEnableGC(final StringTokenizer t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
        new AsyncExecution() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
                void action() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
                    doEnableGC(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
    void doSave(StringTokenizer t) {// Undocumented command: useful for testing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
        if (!t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
            MessageOutput.println("No save index specified.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
        String key = t.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
        if (!t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
            MessageOutput.println("No expression specified.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
        String expr = t.nextToken("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
        Value val = evaluate(expr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
        if (val != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
            Env.setSavedValue(key, val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
            String strVal = getStringValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
            if (strVal != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
                 MessageOutput.println("saved", strVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
            MessageOutput.println("Expression cannot be void");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
    void commandSave(final StringTokenizer t) { // Undocumented command: useful for testing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
        if (!t.hasMoreTokens()) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1873
            Set<String> keys = Env.getSaveKeys();
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1874
            if (keys.isEmpty()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
                MessageOutput.println("No saved values");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
            }
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1878
            for (String key : keys) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
                Value value = Env.getSavedValue(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
                if ((value instanceof ObjectReference) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
                    ((ObjectReference)value).isCollected()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
                    MessageOutput.println("expr is value <collected>",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
                                          new Object [] {key, value.toString()});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
                    if (value == null){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
                        MessageOutput.println("expr is null", key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
                        MessageOutput.println("expr is value",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
                                              new Object [] {key, value.toString()});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
            new AsyncExecution() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
                    void action() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
                        doSave(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
                };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
   void commandBytecodes(final StringTokenizer t) { // Undocumented command: useful for testing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
        if (!t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
            MessageOutput.println("No class specified.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
        String className = t.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
        if (!t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
            MessageOutput.println("No method specified.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
        // Overloading is not handled here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
        String methodName = t.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1917
        List<ReferenceType> classes = Env.vm().classesByName(className);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
        // TO DO: handle multiple classes found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
        if (classes.size() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
            if (className.indexOf('.') < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
                MessageOutput.println("not found (try the full name)", className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
                MessageOutput.println("not found", className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1928
        ReferenceType rt = classes.get(0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
        if (!(rt instanceof ClassType)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
            MessageOutput.println("not a class", className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
        byte[] bytecodes = null;
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1935
        for (Method method : rt.methodsByName(methodName)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
            if (!method.isAbstract()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
                bytecodes = method.bytecodes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
        StringBuffer line = new StringBuffer(80);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
        line.append("0000: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
        for (int i = 0; i < bytecodes.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
            if ((i > 0) && (i % 16 == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
                MessageOutput.printDirectln(line.toString());// Special case: use printDirectln()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
                line.setLength(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
                line.append(String.valueOf(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
                line.append(": ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
                int len = line.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
                for (int j = 0; j < 6 - len; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
                    line.insert(0, '0');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
            int val = 0xff & bytecodes[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
            String str = Integer.toHexString(val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
            if (str.length() == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
                line.append('0');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
            line.append(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
            line.append(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
        if (line.length() > 6) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
            MessageOutput.printDirectln(line.toString());// Special case: use printDirectln()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
    void commandExclude(StringTokenizer t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
        if (!t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
            MessageOutput.printDirectln(Env.excludesString());// Special case: use printDirectln()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
            String rest = t.nextToken("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
            if (rest.equals("none")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
                rest = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
            Env.setExcludes(rest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
    void commandRedefine(StringTokenizer t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
        if (!t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
            MessageOutput.println("Specify classes to redefine");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
            String className = t.nextToken();
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1985
            List<ReferenceType> classes = Env.vm().classesByName(className);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
            if (classes.size() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
                MessageOutput.println("No class named", className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
            if (classes.size() > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
                MessageOutput.println("More than one class named", className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
            Env.setSourcePath(Env.getSourcePath());
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1995
            ReferenceType refType = classes.get(0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
            if (!t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
                MessageOutput.println("Specify file name for class", className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
            String fileName = t.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
            File phyl = new File(fileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
            byte[] bytes = new byte[(int)phyl.length()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
                InputStream in = new FileInputStream(phyl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
                in.read(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
                in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
            } catch (Exception exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
                MessageOutput.println("Error reading file",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
                             new Object [] {fileName, exc.toString()});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
            }
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2012
            Map<ReferenceType, byte[]> map
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2013
                = new HashMap<ReferenceType, byte[]>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
            map.put(refType, bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
                Env.vm().redefineClasses(map);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
            } catch (Throwable exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
                MessageOutput.println("Error redefining class to file",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
                             new Object [] {className,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
                                            fileName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
                                            exc});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
    void commandPopFrames(StringTokenizer t, boolean reenter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
        ThreadInfo threadInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
        if (t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
            String token = t.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
            threadInfo = doGetThread(token);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
            if (threadInfo == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
            threadInfo = ThreadInfo.getCurrentThreadInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
            if (threadInfo == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
                MessageOutput.println("No thread specified.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
            StackFrame frame = threadInfo.getCurrentFrame();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
            threadInfo.getThread().popFrames(frame);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
            threadInfo = ThreadInfo.getCurrentThreadInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
            ThreadInfo.setCurrentThreadInfo(threadInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
            if (reenter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
                commandStepi();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
        } catch (Throwable exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
            MessageOutput.println("Error popping frame", exc.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
    void commandExtension(StringTokenizer t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
        if (!t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
            MessageOutput.println("No class specified.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
        String idClass = t.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
        ReferenceType cls = Env.getReferenceTypeFromToken(idClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
        String extension = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
        if (cls != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
                extension = cls.sourceDebugExtension();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
                MessageOutput.println("sourcedebugextension", extension);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
            } catch (AbsentInformationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
                MessageOutput.println("No sourcedebugextension specified");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
            MessageOutput.println("is not a valid id or class name", idClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
    void commandVersion(String debuggerName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
                        VirtualMachineManager vmm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
        MessageOutput.println("minus version",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
                              new Object [] { debuggerName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
                                              new Integer(vmm.majorInterfaceVersion()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
                                              new Integer(vmm.minorInterfaceVersion()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
                                                  System.getProperty("java.version")});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
        if (Env.connection() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
                MessageOutput.printDirectln(Env.vm().description());// Special case: use printDirectln()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
            } catch (VMNotConnectedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
                MessageOutput.println("No VM connected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
}