hotspot/agent/src/share/classes/sun/jvm/hotspot/CommandProcessor.java
author never
Tue, 17 May 2011 19:11:51 -0700
changeset 9946 b3d5b50e2289
parent 8921 14bfe81f2a9d
child 10547 ea4a2ec31ae2
permissions -rw-r--r--
7045513: JSR 292 inlining causes crashes in methodHandleWalk.cpp Reviewed-by: jrose
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
8921
14bfe81f2a9d 7010070: Update all 2010 Oracle-changed OpenJDK files to have the proper copyright dates - second pass
trims
parents: 8076
diff changeset
     2
 * Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 5341
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 5341
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 5341
diff changeset
    21
 * questions.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
489c9b5090e2 Initial load
duke
parents:
diff changeset
    25
package sun.jvm.hotspot;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
import java.io.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
import java.math.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
import java.util.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
import java.util.regex.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
import sun.jvm.hotspot.types.Type;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
import sun.jvm.hotspot.types.Field;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
import sun.jvm.hotspot.HotSpotTypeDataBase;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
import sun.jvm.hotspot.types.basic.BasicType;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
import sun.jvm.hotspot.types.CIntegerType;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
import sun.jvm.hotspot.code.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
import sun.jvm.hotspot.compiler.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
import sun.jvm.hotspot.debugger.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
import sun.jvm.hotspot.interpreter.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
import sun.jvm.hotspot.memory.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
import sun.jvm.hotspot.oops.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
import sun.jvm.hotspot.runtime.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
import sun.jvm.hotspot.utilities.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
import sun.jvm.hotspot.utilities.soql.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
import sun.jvm.hotspot.ui.classbrowser.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
import sun.jvm.hotspot.ui.tree.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
import sun.jvm.hotspot.tools.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
import sun.jvm.hotspot.tools.ObjectHistogram;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
import sun.jvm.hotspot.tools.StackTrace;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
public class CommandProcessor {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
    public abstract static class DebuggerInterface {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
        public abstract HotSpotAgent getAgent();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
        public abstract boolean isAttached();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
        public abstract void attach(String pid);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
        public abstract void attach(String java, String core);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
        public abstract void detach();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
        public abstract void reattach();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
    static class Tokens {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
        final String input;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
        int i;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
        String[] tokens;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
        int length;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
        String[] splitWhitespace(String cmd) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
            String[] t = cmd.split("\\s");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
            if (t.length == 1 && t[0].length() == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
                return new String[0];
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
            return t;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
        void add(String s, ArrayList t) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
            if (s.length() > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
                t.add(s);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
        Tokens(String cmd) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
            input = cmd;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
            // check for quoting
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
            int quote = cmd.indexOf('"');
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
            ArrayList t = new ArrayList();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
            if (quote != -1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
                while (cmd.length() > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
                    if (quote != -1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
                        int endquote = cmd.indexOf('"', quote + 1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
                        if (endquote == -1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
                            throw new RuntimeException("mismatched quotes: " + input);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
                        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
                        String before = cmd.substring(0, quote).trim();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
                        String quoted = cmd.substring(quote + 1, endquote);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
                        cmd = cmd.substring(endquote + 1).trim();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
                        if (before.length() > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
                            String[] w = splitWhitespace(before);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
                            for (int i = 0; i < w.length; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
                                add(w[i], t);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
                            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
                        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
                        add(quoted, t);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
                        quote = cmd.indexOf('"');
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
                    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
                        String[] w = splitWhitespace(cmd);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
                        for (int i = 0; i < w.length; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
                            add(w[i], t);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
                        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
                        cmd = "";
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
                    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
            } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
                String[] w = splitWhitespace(cmd);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
                for (int i = 0; i < w.length; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
                    add(w[i], t);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
            tokens = (String[])t.toArray(new String[0]);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
            i = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
            length = tokens.length;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
            //for (int i = 0; i < tokens.length; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
            //    System.out.println("\"" + tokens[i] + "\"");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
            //}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
        String nextToken() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
            return tokens[i++];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
        boolean hasMoreTokens() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
            return i < length;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
        int countTokens() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
            return length - i;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
        void trim(int n) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
            if (length >= n) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
                length -= n;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
            } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
                throw new IndexOutOfBoundsException(String.valueOf(n));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
        String join(String sep) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
            StringBuffer result = new StringBuffer();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
            for (int w = i; w < length; w++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
                result.append(tokens[w]);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
                if (w + 1 < length) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
                    result.append(sep);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
            return result.toString();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
        String at(int i) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
            if (i < 0 || i >= length) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
                throw new IndexOutOfBoundsException(String.valueOf(i));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
            return tokens[i];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
    abstract class Command {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
        Command(String n, String u, boolean ok) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
            name = n;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
            usage = u;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
            okIfDisconnected = ok;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
        Command(String n, boolean ok) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
            name = n;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
            usage = n;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
            okIfDisconnected = ok;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
        final String name;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
        final String usage;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
        final boolean okIfDisconnected;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
        abstract void doit(Tokens t);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
        void usage() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
            out.println("Usage: " + usage);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
        void printOopValue(Oop oop) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
            if (oop != null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
                Klass k = oop.getKlass();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
                Symbol s = k.getName();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
                if (s != null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
                    out.print("Oop for " + s.asString() + " @ ");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
                } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
                    out.print("Oop @ ");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
                Oop.printOopAddressOn(oop, out);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
            } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
                out.print("null");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
        void printNode(SimpleTreeNode node) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
            int count = node.getChildCount();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
            for (int i = 0; i < count; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
                try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
                    SimpleTreeNode field = node.getChild(i);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
                    if (field instanceof OopTreeNodeAdapter) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
                        out.print(field);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
                        out.print(" ");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
                        printOopValue(((OopTreeNodeAdapter)field).getOop());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
                        out.println();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   213
                    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
                        out.println(field);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
                    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
                } catch (Exception e) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
                    out.println();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
                    out.println("Error: " + e);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
                    if (verboseExceptions) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   220
                        e.printStackTrace(out);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   221
                    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   222
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   223
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
    void quote(String s) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   228
        if (s.indexOf(" ") == -1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   229
            out.print(s);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
        } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
            out.print("\"");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   232
            out.print(s);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
            out.print("\"");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   234
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   236
489c9b5090e2 Initial load
duke
parents:
diff changeset
   237
    void dumpType(Type type) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   238
        out.print("type ");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   239
        quote(type.getName());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   240
        out.print(" ");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   241
        if (type.getSuperclass() != null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   242
            quote(type.getSuperclass().getName());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   243
            out.print(" ");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   244
        } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   245
            out.print("null ");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   246
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   247
        out.print(type.isOopType());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   248
        out.print(" ");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   249
        if (type.isCIntegerType()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   250
            out.print("true ");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   251
            out.print(((CIntegerType)type).isUnsigned());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   252
            out.print(" ");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   253
        } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
            out.print("false false ");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   255
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   256
        out.print(type.getSize());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   257
        out.println();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   258
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   259
489c9b5090e2 Initial load
duke
parents:
diff changeset
   260
    void dumpFields(Type type) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   261
        Iterator i = type.getFields();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   262
        while (i.hasNext()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   263
            Field f = (Field) i.next();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   264
            out.print("field ");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   265
            quote(type.getName());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   266
            out.print(" ");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   267
            out.print(f.getName());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   268
            out.print(" ");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   269
            quote(f.getType().getName());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   270
            out.print(" ");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   271
            out.print(f.isStatic());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   272
            out.print(" ");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   273
            if (f.isStatic()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   274
                out.print("0 ");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   275
                out.print(f.getStaticFieldAddress());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   276
            } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   277
                out.print(f.getOffset());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   278
                out.print(" 0x0");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   279
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   280
            out.println();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   281
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   282
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   283
489c9b5090e2 Initial load
duke
parents:
diff changeset
   284
489c9b5090e2 Initial load
duke
parents:
diff changeset
   285
    Address lookup(String symbol) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   286
        if (symbol.indexOf("::") != -1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   287
            String[] parts = symbol.split("::");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   288
            StringBuffer mangled = new StringBuffer("__1c");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   289
            for (int i = 0; i < parts.length; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   290
                int len = parts[i].length();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   291
                if (len >= 26) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   292
                    mangled.append((char)('a' + (len / 26)));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   293
                    len = len % 26;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   294
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   295
                mangled.append((char)('A' + len));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   296
                mangled.append(parts[i]);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   297
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   298
            mangled.append("_");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   299
            symbol = mangled.toString();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   300
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   301
        return VM.getVM().getDebugger().lookup(null, symbol);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   302
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   303
489c9b5090e2 Initial load
duke
parents:
diff changeset
   304
    Address parseAddress(String addr) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   305
        return VM.getVM().getDebugger().parseAddress(addr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   306
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   307
489c9b5090e2 Initial load
duke
parents:
diff changeset
   308
    private final Command[] commandList = {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   309
        new Command("reattach", true) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   310
            public void doit(Tokens t) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   311
                int tokens = t.countTokens();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   312
                if (tokens != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   313
                    usage();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   314
                    return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   315
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   316
                preAttach();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   317
                debugger.reattach();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   318
                postAttach();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   319
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   320
        },
489c9b5090e2 Initial load
duke
parents:
diff changeset
   321
        new Command("attach", "attach pid | exec core", true) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   322
            public void doit(Tokens t) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   323
                int tokens = t.countTokens();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   324
                if (tokens == 1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   325
                    preAttach();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   326
                    debugger.attach(t.nextToken());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   327
                    postAttach();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   328
                } else if (tokens == 2) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   329
                    preAttach();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   330
                    debugger.attach(t.nextToken(), t.nextToken());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   331
                    postAttach();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   332
                } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   333
                    usage();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   334
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   335
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   336
        },
489c9b5090e2 Initial load
duke
parents:
diff changeset
   337
        new Command("detach", false) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   338
            public void doit(Tokens t) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   339
                if (t.countTokens() != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   340
                    usage();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   341
                } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   342
                    debugger.detach();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   343
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   344
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   345
        },
489c9b5090e2 Initial load
duke
parents:
diff changeset
   346
        new Command("examine", "examine [ address/count ] | [ address,address]", false) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   347
            Pattern args1 = Pattern.compile("^(0x[0-9a-f]+)(/([0-9]*)([a-z]*))?$");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   348
            Pattern args2 = Pattern.compile("^(0x[0-9a-f]+),(0x[0-9a-f]+)(/[a-z]*)?$");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   349
489c9b5090e2 Initial load
duke
parents:
diff changeset
   350
            String fill(Address a, int width) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   351
                String s = "0x0";
489c9b5090e2 Initial load
duke
parents:
diff changeset
   352
                if (a != null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   353
                    s = a.toString();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   354
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   355
                if (s.length() != width) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   356
                    return s.substring(0, 2) + "000000000000000000000".substring(0, width - s.length()) + s.substring(2);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   357
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   358
                return s;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   359
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   360
489c9b5090e2 Initial load
duke
parents:
diff changeset
   361
            public void doit(Tokens t) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   362
                if (t.countTokens() != 1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   363
                    usage();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   364
                } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   365
                    String arg = t.nextToken();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   366
                    Matcher m1 = args1.matcher(arg);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   367
                    Matcher m2 = args2.matcher(arg);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   368
                    Address start = null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   369
                    Address end   = null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   370
                    String format = "";
489c9b5090e2 Initial load
duke
parents:
diff changeset
   371
                    int formatSize = (int)VM.getVM().getAddressSize();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   372
489c9b5090e2 Initial load
duke
parents:
diff changeset
   373
                    if (m1.matches()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   374
                        start = VM.getVM().getDebugger().parseAddress(m1.group(1));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   375
                        int count = 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   376
                        if (m1.group(2) != null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   377
                            count = Integer.parseInt(m1.group(3));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   378
                        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   379
                        end = start.addOffsetTo(count * formatSize);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   380
                    } else if (m2.matches()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   381
                        start = VM.getVM().getDebugger().parseAddress(m2.group(1));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   382
                        end   = VM.getVM().getDebugger().parseAddress(m2.group(2));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   383
                    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   384
                        usage();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   385
                        return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   386
                    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   387
                    int line = 80;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   388
                    int formatWidth = formatSize * 8 / 4 + 2;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   389
489c9b5090e2 Initial load
duke
parents:
diff changeset
   390
                    out.print(fill(start, formatWidth));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   391
                    out.print(": ");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   392
                    int width = line - formatWidth - 2;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   393
489c9b5090e2 Initial load
duke
parents:
diff changeset
   394
                    boolean needsPrintln = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   395
                    while (start != null && start.lessThan(end)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   396
                        Address val = start.getAddressAt(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   397
                        out.print(fill(val, formatWidth));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   398
                        needsPrintln = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   399
                        width -= formatWidth;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   400
                        start = start.addOffsetTo(formatSize);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   401
                        if (width <= formatWidth) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   402
                            out.println();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   403
                            needsPrintln = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   404
                            if (start.lessThan(end)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   405
                                out.print(fill(start, formatWidth));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   406
                                out.print(": ");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   407
                                width = line - formatWidth - 2;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   408
                            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   409
                        } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   410
                            out.print(" ");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   411
                            width -= 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   412
                        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   413
                    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   414
                    if (needsPrintln) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   415
                        out.println();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   416
                    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   417
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   418
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   419
        },
489c9b5090e2 Initial load
duke
parents:
diff changeset
   420
        new Command("findpc", "findpc address", false) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   421
            public void doit(Tokens t) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   422
                if (t.countTokens() != 1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   423
                    usage();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   424
                } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   425
                    Address a = VM.getVM().getDebugger().parseAddress(t.nextToken());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   426
                    PointerLocation loc = PointerFinder.find(a);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   427
                    loc.printOn(out);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   428
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   429
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   430
        },
8076
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 6418
diff changeset
   431
        new Command("symbol", "symbol address", false) {
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 6418
diff changeset
   432
            public void doit(Tokens t) {
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 6418
diff changeset
   433
                if (t.countTokens() != 1) {
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 6418
diff changeset
   434
                    usage();
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 6418
diff changeset
   435
                } else {
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 6418
diff changeset
   436
                    Address a = VM.getVM().getDebugger().parseAddress(t.nextToken());
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 6418
diff changeset
   437
                    Symbol.create(a).printValueOn(out);
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 6418
diff changeset
   438
                    out.println();
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 6418
diff changeset
   439
                }
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 6418
diff changeset
   440
            }
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 6418
diff changeset
   441
        },
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 6418
diff changeset
   442
        new Command("symboltable", "symboltable name", false) {
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 6418
diff changeset
   443
            public void doit(Tokens t) {
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 6418
diff changeset
   444
                if (t.countTokens() != 1) {
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 6418
diff changeset
   445
                    usage();
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 6418
diff changeset
   446
                } else {
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 6418
diff changeset
   447
                    out.println(SymbolTable.getTheTable().probe(t.nextToken()));
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 6418
diff changeset
   448
                }
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 6418
diff changeset
   449
            }
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 6418
diff changeset
   450
        },
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 6418
diff changeset
   451
        new Command("symboldump", "symboldump", false) {
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 6418
diff changeset
   452
            public void doit(Tokens t) {
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 6418
diff changeset
   453
                SymbolTable.getTheTable().symbolsDo(new SymbolTable.SymbolVisitor() {
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 6418
diff changeset
   454
                        public void visit(Symbol sym) {
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 6418
diff changeset
   455
                            sym.printValueOn(out);
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 6418
diff changeset
   456
                            out.println();
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 6418
diff changeset
   457
                        }
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 6418
diff changeset
   458
                    });
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 6418
diff changeset
   459
            }
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 6418
diff changeset
   460
        },
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   461
        new Command("flags", "flags [ flag ]", false) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   462
            public void doit(Tokens t) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   463
                int tokens = t.countTokens();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   464
                if (tokens != 0 && tokens != 1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   465
                    usage();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   466
                } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   467
                    String name = tokens > 0 ? t.nextToken() : null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   468
489c9b5090e2 Initial load
duke
parents:
diff changeset
   469
                    VM.Flag[] flags = VM.getVM().getCommandLineFlags();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   470
                    if (flags == null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   471
                        out.println("Command Flag info not available (use 1.4.1_03 or later)!");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   472
                    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   473
                        boolean printed = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   474
                        for (int f = 0; f < flags.length; f++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   475
                            VM.Flag flag = flags[f];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   476
                            if (name == null || flag.getName().equals(name)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   477
                                out.println(flag.getName() + " = " + flag.getValue());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   478
                                printed = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   479
                            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   480
                        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   481
                        if (name != null && !printed) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   482
                            out.println("Couldn't find flag: " + name);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   483
                        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   484
                    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   485
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   486
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   487
        },
489c9b5090e2 Initial load
duke
parents:
diff changeset
   488
        new Command("help", "help [ command ]", true) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   489
            public void doit(Tokens t) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   490
                int tokens = t.countTokens();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   491
                Command cmd = null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   492
                if (tokens == 1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   493
                    cmd = findCommand(t.nextToken());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   494
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   495
489c9b5090e2 Initial load
duke
parents:
diff changeset
   496
                if (cmd != null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   497
                    cmd.usage();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   498
                } else if (tokens == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   499
                    out.println("Available commands:");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   500
                    Object[] keys = commands.keySet().toArray();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   501
                    Arrays.sort(keys, new Comparator() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   502
                             public int compare(Object o1, Object o2) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   503
                                 return o1.toString().compareTo(o2.toString());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   504
                             }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   505
                          });
489c9b5090e2 Initial load
duke
parents:
diff changeset
   506
                    for (int i = 0; i < keys.length; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   507
                        out.print("  ");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   508
                        out.println(((Command)commands.get(keys[i])).usage);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   509
                    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   510
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   511
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   512
        },
489c9b5090e2 Initial load
duke
parents:
diff changeset
   513
        new Command("history", "history", true) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   514
            public void doit(Tokens t) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   515
                int tokens = t.countTokens();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   516
                if (tokens != 0 && (tokens != 1 || !t.nextToken().equals("-h"))) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   517
                    usage();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   518
                    return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   519
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   520
                boolean printIndex = tokens == 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   521
                for (int i = 0; i < history.size(); i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   522
                    if (printIndex) out.print(i + " ");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   523
                    out.println(history.get(i));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   524
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   525
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   526
        },
5341
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   527
        new Command("revptrs", "revptrs address", false) {
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   528
            public void doit(Tokens t) {
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   529
                int tokens = t.countTokens();
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   530
                if (tokens != 1 && (tokens != 2 || !t.nextToken().equals("-c"))) {
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   531
                    usage();
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   532
                    return;
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   533
                }
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   534
                boolean chase = tokens == 2;
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   535
                ReversePtrs revptrs = VM.getVM().getRevPtrs();
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   536
                if (revptrs == null) {
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   537
                    out.println("Computing reverse pointers...");
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   538
                    ReversePtrsAnalysis analysis = new ReversePtrsAnalysis();
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   539
                    final boolean[] complete = new boolean[1];
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   540
                    HeapProgressThunk thunk = new HeapProgressThunk() {
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   541
                            public void heapIterationFractionUpdate(double d) {}
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   542
                            public synchronized void heapIterationComplete() {
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   543
                                complete[0] = true;
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   544
                                notify();
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   545
                            }
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   546
                        };
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   547
                    analysis.setHeapProgressThunk(thunk);
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   548
                    analysis.run();
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   549
                    while (!complete[0]) {
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   550
                        synchronized (thunk) {
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   551
                            try {
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   552
                                thunk.wait();
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   553
                            } catch (Exception e) {
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   554
                            }
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   555
                        }
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   556
                    }
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   557
                    revptrs = VM.getVM().getRevPtrs();
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   558
                    out.println("Done.");
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   559
                }
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   560
                Address a = VM.getVM().getDebugger().parseAddress(t.nextToken());
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   561
                if (VM.getVM().getUniverse().heap().isInReserved(a)) {
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   562
                    OopHandle handle = a.addOffsetToAsOopHandle(0);
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   563
                    Oop oop = VM.getVM().getObjectHeap().newOop(handle);
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   564
                    ArrayList ptrs = revptrs.get(oop);
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   565
                    if (ptrs == null) {
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   566
                        out.println("no live references to " + a);
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   567
                    } else {
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   568
                        if (chase) {
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   569
                            while (ptrs.size() == 1) {
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   570
                                LivenessPathElement e = (LivenessPathElement)ptrs.get(0);
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   571
                                ByteArrayOutputStream bos = new ByteArrayOutputStream();
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   572
                                Oop.printOopValueOn(e.getObj(), new PrintStream(bos));
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   573
                                out.println(bos.toString());
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   574
                                ptrs = revptrs.get(e.getObj());
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   575
                            }
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   576
                        } else {
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   577
                            for (int i = 0; i < ptrs.size(); i++) {
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   578
                                LivenessPathElement e = (LivenessPathElement)ptrs.get(i);
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   579
                                ByteArrayOutputStream bos = new ByteArrayOutputStream();
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   580
                                Oop.printOopValueOn(e.getObj(), new PrintStream(bos));
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   581
                                out.println(bos.toString());
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   582
                                oop = e.getObj();
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   583
                            }
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   584
                        }
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   585
                    }
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   586
                }
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   587
            }
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   588
        },
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   589
        new Command("inspect", "inspect expression", false) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   590
            public void doit(Tokens t) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   591
                if (t.countTokens() != 1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   592
                    usage();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   593
                } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   594
                    Address a = VM.getVM().getDebugger().parseAddress(t.nextToken());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   595
                    SimpleTreeNode node = null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   596
                    if (VM.getVM().getUniverse().heap().isInReserved(a)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   597
                        OopHandle handle = a.addOffsetToAsOopHandle(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   598
                        Oop oop = VM.getVM().getObjectHeap().newOop(handle);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   599
                        node = new OopTreeNodeAdapter(oop, null);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   600
489c9b5090e2 Initial load
duke
parents:
diff changeset
   601
                        out.println("instance of " + node.getValue() + " @ " + a +
489c9b5090e2 Initial load
duke
parents:
diff changeset
   602
                                    " (size = " + oop.getObjectSize() + ")");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   603
                    } else if (VM.getVM().getCodeCache().contains(a)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   604
                        CodeBlob blob = VM.getVM().getCodeCache().findBlobUnsafe(a);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   605
                        a = blob.headerBegin();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   606
                    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   607
                    if (node == null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   608
                        Type type = VM.getVM().getTypeDataBase().guessTypeForAddress(a);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   609
                        if (type != null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   610
                            out.println("Type is " + type.getName() + " (size of " + type.getSize() + ")");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   611
                            node = new CTypeTreeNodeAdapter(a, type, null);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   612
                        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   613
                    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   614
                    if (node != null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   615
                        printNode(node);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   616
                    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   617
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   618
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   619
        },
489c9b5090e2 Initial load
duke
parents:
diff changeset
   620
        new Command("jhisto", "jhisto", false) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   621
            public void doit(Tokens t) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   622
                 ObjectHistogram histo = new ObjectHistogram();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   623
                 histo.run(out, err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   624
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   625
        },
489c9b5090e2 Initial load
duke
parents:
diff changeset
   626
        new Command("jstack", "jstack [-v]", false) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   627
            public void doit(Tokens t) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   628
                boolean verbose = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   629
                if (t.countTokens() > 0 && t.nextToken().equals("-v")) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   630
                    verbose = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   631
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   632
                StackTrace jstack = new StackTrace(verbose, true);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   633
                jstack.run(out);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   634
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   635
        },
489c9b5090e2 Initial load
duke
parents:
diff changeset
   636
        new Command("print", "print expression", false) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   637
            public void doit(Tokens t) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   638
                if (t.countTokens() != 1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   639
                    usage();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   640
                } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   641
                    Address a = VM.getVM().getDebugger().parseAddress(t.nextToken());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   642
                    HTMLGenerator gen = new HTMLGenerator(false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   643
                    out.println(gen.genHTML(a));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   644
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   645
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   646
        },
489c9b5090e2 Initial load
duke
parents:
diff changeset
   647
        new Command("printas", "printas type expression", false) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   648
            public void doit(Tokens t) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   649
                if (t.countTokens() != 2) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   650
                    usage();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   651
                } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   652
                    Type type = agent.getTypeDataBase().lookupType(t.nextToken());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   653
                    Address a = VM.getVM().getDebugger().parseAddress(t.nextToken());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   654
                    CTypeTreeNodeAdapter node = new CTypeTreeNodeAdapter(a, type, null);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   655
489c9b5090e2 Initial load
duke
parents:
diff changeset
   656
                    out.println("pointer to " + type + " @ " + a +
489c9b5090e2 Initial load
duke
parents:
diff changeset
   657
                                " (size = " + type.getSize() + ")");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   658
                    printNode(node);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   659
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   660
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   661
        },
489c9b5090e2 Initial load
duke
parents:
diff changeset
   662
        new Command("printstatics", "printstatics [ type ]", false) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   663
            public void doit(Tokens t) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   664
                if (t.countTokens() > 1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   665
                    usage();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   666
                } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   667
                    if (t.countTokens() == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   668
                        out.println("All known static fields");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   669
                        printNode(new CTypeTreeNodeAdapter(agent.getTypeDataBase().getTypes()));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   670
                    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   671
                        Type type = agent.getTypeDataBase().lookupType(t.nextToken());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   672
                        out.println("Static fields of " + type.getName());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   673
                        printNode(new CTypeTreeNodeAdapter(type));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   674
                    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   675
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   676
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   677
        },
489c9b5090e2 Initial load
duke
parents:
diff changeset
   678
        new Command("pmap", "pmap", false) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   679
            public void doit(Tokens t) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   680
                PMap pmap = new PMap();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   681
                pmap.run(out, debugger.getAgent().getDebugger());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   682
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   683
        },
489c9b5090e2 Initial load
duke
parents:
diff changeset
   684
        new Command("pstack", "pstack [-v]", false) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   685
            public void doit(Tokens t) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   686
                boolean verbose = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   687
                if (t.countTokens() > 0 && t.nextToken().equals("-v")) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   688
                    verbose = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   689
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   690
                PStack pstack = new PStack(verbose, true);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   691
                pstack.run(out, debugger.getAgent().getDebugger());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   692
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   693
        },
489c9b5090e2 Initial load
duke
parents:
diff changeset
   694
        new Command("quit", true) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   695
            public void doit(Tokens t) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   696
                if (t.countTokens() != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   697
                    usage();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   698
                } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   699
                    debugger.detach();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   700
                    System.exit(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   701
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   702
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   703
        },
489c9b5090e2 Initial load
duke
parents:
diff changeset
   704
        new Command("echo", "echo [ true | false ]", true) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   705
            public void doit(Tokens t) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   706
                if (t.countTokens() == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   707
                    out.println("echo is " + doEcho);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   708
                } else if (t.countTokens() == 1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   709
                    doEcho = Boolean.valueOf(t.nextToken()).booleanValue();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   710
                } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   711
                    usage();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   712
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   713
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   714
        },
489c9b5090e2 Initial load
duke
parents:
diff changeset
   715
        new Command("versioncheck", "versioncheck [ true | false ]", true) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   716
            public void doit(Tokens t) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   717
                if (t.countTokens() == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   718
                    out.println("versioncheck is " +
489c9b5090e2 Initial load
duke
parents:
diff changeset
   719
                                (System.getProperty("sun.jvm.hotspot.runtime.VM.disableVersionCheck") == null));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   720
                } else if (t.countTokens() == 1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   721
                    if (Boolean.valueOf(t.nextToken()).booleanValue()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   722
                        System.setProperty("sun.jvm.hotspot.runtime.VM.disableVersionCheck", null);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   723
                    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   724
                        System.setProperty("sun.jvm.hotspot.runtime.VM.disableVersionCheck", "true");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   725
                    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   726
                } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   727
                    usage();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   728
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   729
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   730
        },
489c9b5090e2 Initial load
duke
parents:
diff changeset
   731
        new Command("scanoops", "scanoops start end [ type ]", false) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   732
            public void doit(Tokens t) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   733
                if (t.countTokens() != 2 && t.countTokens() != 3) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   734
                    usage();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   735
                } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   736
                    long stride = VM.getVM().getAddressSize();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   737
                    Address base = VM.getVM().getDebugger().parseAddress(t.nextToken());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   738
                    Address end  = VM.getVM().getDebugger().parseAddress(t.nextToken());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   739
                    Klass klass = null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   740
                    if (t.countTokens() == 1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   741
                        klass = SystemDictionaryHelper.findInstanceKlass(t.nextToken());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   742
                    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   743
                    while (base != null && base.lessThan(end)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   744
                        long step = stride;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   745
                        OopHandle handle = base.addOffsetToAsOopHandle(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   746
                        if (RobustOopDeterminator.oopLooksValid(handle)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   747
                            try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   748
                                Oop oop = VM.getVM().getObjectHeap().newOop(handle);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   749
                                if (klass == null || oop.getKlass().isSubtypeOf(klass))
489c9b5090e2 Initial load
duke
parents:
diff changeset
   750
                                    out.println(handle.toString() + " " + oop.getKlass().getName().asString());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   751
                                step = oop.getObjectSize();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   752
                            } catch (UnknownOopException ex) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   753
                                // ok
489c9b5090e2 Initial load
duke
parents:
diff changeset
   754
                            } catch (RuntimeException ex) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   755
                                ex.printStackTrace();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   756
                            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   757
                        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   758
                        base = base.addOffsetTo(step);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   759
                    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   760
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   761
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   762
        },
489c9b5090e2 Initial load
duke
parents:
diff changeset
   763
        new Command("field", "field [ type [ name fieldtype isStatic offset address ] ]", true) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   764
            public void doit(Tokens t) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   765
                if (t.countTokens() != 1 && t.countTokens() != 0 && t.countTokens() != 6) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   766
                    usage();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   767
                    return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   768
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   769
                if (t.countTokens() == 1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   770
                    Type type = agent.getTypeDataBase().lookupType(t.nextToken());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   771
                    dumpFields(type);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   772
                } else if (t.countTokens() == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   773
                    Iterator i = agent.getTypeDataBase().getTypes();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   774
                    while (i.hasNext()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   775
                        dumpFields((Type)i.next());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   776
                    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   777
                } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   778
                    BasicType containingType = (BasicType)agent.getTypeDataBase().lookupType(t.nextToken());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   779
489c9b5090e2 Initial load
duke
parents:
diff changeset
   780
                    String fieldName = t.nextToken();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   781
489c9b5090e2 Initial load
duke
parents:
diff changeset
   782
                    // The field's Type must already be in the database -- no exceptions
489c9b5090e2 Initial load
duke
parents:
diff changeset
   783
                    Type fieldType = agent.getTypeDataBase().lookupType(t.nextToken());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   784
489c9b5090e2 Initial load
duke
parents:
diff changeset
   785
                    boolean isStatic = Boolean.valueOf(t.nextToken()).booleanValue();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   786
                    long offset = Long.parseLong(t.nextToken());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   787
                    Address staticAddress = parseAddress(t.nextToken());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   788
                    if (isStatic && staticAddress == null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   789
                        staticAddress = lookup(containingType.getName() + "::" + fieldName);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   790
                    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   791
489c9b5090e2 Initial load
duke
parents:
diff changeset
   792
                    // check to see if the field already exists
489c9b5090e2 Initial load
duke
parents:
diff changeset
   793
                    Iterator i = containingType.getFields();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   794
                    while (i.hasNext()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   795
                        Field f = (Field) i.next();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   796
                        if (f.getName().equals(fieldName)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   797
                            if (f.isStatic() != isStatic) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   798
                                throw new RuntimeException("static/nonstatic mismatch: " + t.input);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   799
                            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   800
                            if (!isStatic) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   801
                                if (f.getOffset() != offset) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   802
                                    throw new RuntimeException("bad redefinition of field offset: " + t.input);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   803
                                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   804
                            } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   805
                                if (!f.getStaticFieldAddress().equals(staticAddress)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   806
                                    throw new RuntimeException("bad redefinition of field location: " + t.input);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   807
                                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   808
                            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   809
                            if (f.getType() != fieldType) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   810
                                throw new RuntimeException("bad redefinition of field type: " + t.input);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   811
                            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   812
                            return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   813
                        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   814
                    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   815
489c9b5090e2 Initial load
duke
parents:
diff changeset
   816
                    // Create field by type
489c9b5090e2 Initial load
duke
parents:
diff changeset
   817
                    HotSpotTypeDataBase db = (HotSpotTypeDataBase)agent.getTypeDataBase();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   818
                    db.createField(containingType,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   819
                                   fieldName, fieldType,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   820
                                   isStatic,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   821
                                   offset,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   822
                                   staticAddress);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   823
489c9b5090e2 Initial load
duke
parents:
diff changeset
   824
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   825
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   826
489c9b5090e2 Initial load
duke
parents:
diff changeset
   827
        },
489c9b5090e2 Initial load
duke
parents:
diff changeset
   828
        new Command("tokenize", "tokenize ...", true) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   829
            public void doit(Tokens t) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   830
                while (t.hasMoreTokens()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   831
                    out.println("\"" + t.nextToken() + "\"");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   832
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   833
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   834
        },
489c9b5090e2 Initial load
duke
parents:
diff changeset
   835
        new Command("type", "type [ type [ name super isOop isInteger isUnsigned size ] ]", true) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   836
            public void doit(Tokens t) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   837
                if (t.countTokens() != 1 && t.countTokens() != 0 && t.countTokens() != 6) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   838
                    usage();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   839
                    return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   840
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   841
                if (t.countTokens() == 6) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   842
                    String typeName = t.nextToken();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   843
                    String superclassName = t.nextToken();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   844
                    if (superclassName.equals("null")) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   845
                        superclassName = null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   846
                    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   847
                    boolean isOop = Boolean.valueOf(t.nextToken()).booleanValue();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   848
                    boolean isInteger = Boolean.valueOf(t.nextToken()).booleanValue();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   849
                    boolean isUnsigned = Boolean.valueOf(t.nextToken()).booleanValue();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   850
                    long size = Long.parseLong(t.nextToken());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   851
489c9b5090e2 Initial load
duke
parents:
diff changeset
   852
                    BasicType type = null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   853
                    try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   854
                        type = (BasicType)agent.getTypeDataBase().lookupType(typeName);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   855
                    } catch (RuntimeException e) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   856
                    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   857
                    if (type != null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   858
                        if (type.isOopType() != isOop) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   859
                            throw new RuntimeException("oop mismatch in type definition: " + t.input);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   860
                        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   861
                        if (type.isCIntegerType() != isInteger) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   862
                            throw new RuntimeException("integer type mismatch in type definition: " + t.input);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   863
                        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   864
                        if (type.isCIntegerType() && (((CIntegerType)type).isUnsigned()) != isUnsigned) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   865
                            throw new RuntimeException("unsigned mismatch in type definition: " + t.input);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   866
                        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   867
                        if (type.getSuperclass() == null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   868
                            if (superclassName != null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   869
                                if (type.getSize() == -1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   870
                                    type.setSuperclass(agent.getTypeDataBase().lookupType(superclassName));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   871
                                } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   872
                                    throw new RuntimeException("unexpected superclass in type definition: " + t.input);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   873
                                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   874
                            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   875
                        } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   876
                            if (superclassName == null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   877
                                throw new RuntimeException("missing superclass in type definition: " + t.input);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   878
                            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   879
                            if (!type.getSuperclass().getName().equals(superclassName)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   880
                                throw new RuntimeException("incorrect superclass in type definition: " + t.input);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   881
                            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   882
                        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   883
                        if (type.getSize() != size) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   884
                            if (type.getSize() == -1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   885
                                type.setSize(size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   886
                            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   887
                            throw new RuntimeException("size mismatch in type definition: " + t.input);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   888
                        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   889
                        return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   890
                    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   891
489c9b5090e2 Initial load
duke
parents:
diff changeset
   892
                    // Create type
489c9b5090e2 Initial load
duke
parents:
diff changeset
   893
                    HotSpotTypeDataBase db = (HotSpotTypeDataBase)agent.getTypeDataBase();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   894
                    db.createType(typeName, superclassName, isOop, isInteger, isUnsigned, size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   895
                } else if (t.countTokens() == 1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   896
                    Type type = agent.getTypeDataBase().lookupType(t.nextToken());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   897
                    dumpType(type);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   898
                } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   899
                    Iterator i = agent.getTypeDataBase().getTypes();
5341
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   900
                    // Make sure the types are emitted in an order than can be read back in
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   901
                    HashSet emitted = new HashSet();
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   902
                    Stack pending = new Stack();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   903
                    while (i.hasNext()) {
5341
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   904
                        Type n = (Type)i.next();
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   905
                        if (emitted.contains(n.getName())) {
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   906
                            continue;
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   907
                        }
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   908
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   909
                        while (n != null && !emitted.contains(n.getName())) {
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   910
                            pending.push(n);
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   911
                            n = n.getSuperclass();
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   912
                        }
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   913
                        while (!pending.empty()) {
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   914
                            n = (Type)pending.pop();
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   915
                            dumpType(n);
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   916
                            emitted.add(n.getName());
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   917
                        }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   918
                    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   919
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   920
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   921
489c9b5090e2 Initial load
duke
parents:
diff changeset
   922
        },
489c9b5090e2 Initial load
duke
parents:
diff changeset
   923
        new Command("source", "source filename", true) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   924
            public void doit(Tokens t) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   925
                if (t.countTokens() != 1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   926
                    usage();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   927
                    return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   928
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   929
                String file = t.nextToken();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   930
                BufferedReader savedInput = in;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   931
                try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   932
                    BufferedReader input = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   933
                    in = input;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   934
                    run(false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   935
                } catch (Exception e) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   936
                    out.println("Error: " + e);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   937
                    if (verboseExceptions) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   938
                        e.printStackTrace(out);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   939
                    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   940
                } finally {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   941
                    in = savedInput;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   942
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   943
489c9b5090e2 Initial load
duke
parents:
diff changeset
   944
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   945
        },
5341
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   946
        new Command("search", "search [ heap | perm | rawheap | codecache | threads ] value", false) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   947
            public void doit(Tokens t) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   948
                if (t.countTokens() != 2) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   949
                    usage();
5341
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   950
                    return;
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   951
                }
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   952
                String type = t.nextToken();
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   953
                final Address value = VM.getVM().getDebugger().parseAddress(t.nextToken());
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   954
                final long stride = VM.getVM().getAddressSize();
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   955
                if (type.equals("threads")) {
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   956
                    Threads threads = VM.getVM().getThreads();
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   957
                    for (JavaThread thread = threads.first(); thread != null; thread = thread.next()) {
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   958
                        Address base = thread.getBaseOfStackPointer();
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   959
                        Address end = thread.getLastJavaSP();
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   960
                        if (end == null) continue;
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   961
                        if (end.lessThan(base)) {
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   962
                            Address tmp = base;
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   963
                            base = end;
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   964
                            end = tmp;
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   965
                        }
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   966
                        out.println("Searching " + base + " " + end);
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   967
                        while (base != null && base.lessThan(end)) {
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   968
                            Address val = base.getAddressAt(0);
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   969
                            if (AddressOps.equal(val, value)) {
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   970
                                out.println(base);
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   971
                            }
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   972
                            base = base.addOffsetTo(stride);
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   973
                        }
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   974
                    }
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   975
                } else if (type.equals("rawheap")) {
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   976
                    RawHeapVisitor iterator = new RawHeapVisitor() {
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   977
                            public void prologue(long used) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   978
                            }
5341
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   979
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   980
                            public void visitAddress(Address addr) {
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   981
                                Address val = addr.getAddressAt(0);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   982
                                if (AddressOps.equal(val, value)) {
5341
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   983
                                        out.println("found at " + addr);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   984
                                }
5341
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   985
                            }
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   986
                            public void visitCompOopAddress(Address addr) {
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   987
                                Address val = addr.getCompOopAddressAt(0);
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   988
                                if (AddressOps.equal(val, value)) {
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   989
                                    out.println("found at " + addr);
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   990
                                }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   991
                            }
5341
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   992
                            public void epilogue() {
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   993
                            }
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   994
                        };
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   995
                    VM.getVM().getObjectHeap().iterateRaw(iterator);
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   996
                } else if (type.equals("heap") || type.equals("perm")) {
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   997
                    HeapVisitor iterator = new DefaultHeapVisitor() {
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   998
                            public boolean doObj(Oop obj) {
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
   999
                                int index = 0;
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1000
                                Address start = obj.getHandle();
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1001
                                long end = obj.getObjectSize();
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1002
                                while (index < end) {
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1003
                                    Address val = start.getAddressAt(index);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1004
                                    if (AddressOps.equal(val, value)) {
5341
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1005
                                        out.println("found in " + obj.getHandle());
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1006
                                        break;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1007
                                    }
5341
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1008
                                    index += 4;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1009
                                }
5341
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1010
                                return false;
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1011
                            }
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1012
                        };
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1013
                    if (type.equals("heap")) {
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1014
                        VM.getVM().getObjectHeap().iterate(iterator);
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1015
                    } else {
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1016
                        VM.getVM().getObjectHeap().iteratePerm(iterator);
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1017
                    }
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1018
                } else if (type.equals("codecache")) {
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1019
                    CodeCacheVisitor v = new CodeCacheVisitor() {
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1020
                            public void prologue(Address start, Address end) {
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1021
                            }
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1022
                            public void visit(CodeBlob blob) {
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1023
                                boolean printed = false;
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1024
                                Address base = blob.getAddress();
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1025
                                Address end = base.addOffsetTo(blob.getSize());
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1026
                                while (base != null && base.lessThan(end)) {
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1027
                                    Address val = base.getAddressAt(0);
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1028
                                    if (AddressOps.equal(val, value)) {
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1029
                                        if (!printed) {
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1030
                                            printed = true;
9946
b3d5b50e2289 7045513: JSR 292 inlining causes crashes in methodHandleWalk.cpp
never
parents: 8921
diff changeset
  1031
                                            try {
b3d5b50e2289 7045513: JSR 292 inlining causes crashes in methodHandleWalk.cpp
never
parents: 8921
diff changeset
  1032
                                                blob.printOn(out);
b3d5b50e2289 7045513: JSR 292 inlining causes crashes in methodHandleWalk.cpp
never
parents: 8921
diff changeset
  1033
                                            } catch (Exception e) {
b3d5b50e2289 7045513: JSR 292 inlining causes crashes in methodHandleWalk.cpp
never
parents: 8921
diff changeset
  1034
                                                out.println("Exception printing blob at " + base);
b3d5b50e2289 7045513: JSR 292 inlining causes crashes in methodHandleWalk.cpp
never
parents: 8921
diff changeset
  1035
                                                e.printStackTrace();
b3d5b50e2289 7045513: JSR 292 inlining causes crashes in methodHandleWalk.cpp
never
parents: 8921
diff changeset
  1036
                                            }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1037
                                        }
5341
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1038
                                        out.println("found at " + base + "\n");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1039
                                    }
5341
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1040
                                    base = base.addOffsetTo(stride);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1041
                                }
5341
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1042
                            }
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1043
                            public void epilogue() {
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1044
                            }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1045
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1046
5341
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1047
                        };
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1048
                    VM.getVM().getCodeCache().iterate(v);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1049
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1050
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1051
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1052
        },
4095
6e0acfda1d47 6892186: SA does not dump debug info for scalar replaced objects
kvn
parents: 670
diff changeset
  1053
        new Command("dumpcodecache", "dumpcodecache", false) {
6e0acfda1d47 6892186: SA does not dump debug info for scalar replaced objects
kvn
parents: 670
diff changeset
  1054
            public void doit(Tokens t) {
6e0acfda1d47 6892186: SA does not dump debug info for scalar replaced objects
kvn
parents: 670
diff changeset
  1055
                if (t.countTokens() != 0) {
6e0acfda1d47 6892186: SA does not dump debug info for scalar replaced objects
kvn
parents: 670
diff changeset
  1056
                    usage();
6e0acfda1d47 6892186: SA does not dump debug info for scalar replaced objects
kvn
parents: 670
diff changeset
  1057
                } else {
6e0acfda1d47 6892186: SA does not dump debug info for scalar replaced objects
kvn
parents: 670
diff changeset
  1058
                    final PrintStream fout = out;
6e0acfda1d47 6892186: SA does not dump debug info for scalar replaced objects
kvn
parents: 670
diff changeset
  1059
                    final HTMLGenerator gen = new HTMLGenerator(false);
6e0acfda1d47 6892186: SA does not dump debug info for scalar replaced objects
kvn
parents: 670
diff changeset
  1060
                    CodeCacheVisitor v = new CodeCacheVisitor() {
6e0acfda1d47 6892186: SA does not dump debug info for scalar replaced objects
kvn
parents: 670
diff changeset
  1061
                            public void prologue(Address start, Address end) {
6e0acfda1d47 6892186: SA does not dump debug info for scalar replaced objects
kvn
parents: 670
diff changeset
  1062
                            }
6e0acfda1d47 6892186: SA does not dump debug info for scalar replaced objects
kvn
parents: 670
diff changeset
  1063
                            public void visit(CodeBlob blob) {
6418
6671edbd230e 6978355: renaming for 6961697
twisti
parents: 5547
diff changeset
  1064
                                fout.println(gen.genHTML(blob.contentBegin()));
4095
6e0acfda1d47 6892186: SA does not dump debug info for scalar replaced objects
kvn
parents: 670
diff changeset
  1065
                            }
6e0acfda1d47 6892186: SA does not dump debug info for scalar replaced objects
kvn
parents: 670
diff changeset
  1066
                            public void epilogue() {
6e0acfda1d47 6892186: SA does not dump debug info for scalar replaced objects
kvn
parents: 670
diff changeset
  1067
                            }
6e0acfda1d47 6892186: SA does not dump debug info for scalar replaced objects
kvn
parents: 670
diff changeset
  1068
6e0acfda1d47 6892186: SA does not dump debug info for scalar replaced objects
kvn
parents: 670
diff changeset
  1069
6e0acfda1d47 6892186: SA does not dump debug info for scalar replaced objects
kvn
parents: 670
diff changeset
  1070
                        };
6e0acfda1d47 6892186: SA does not dump debug info for scalar replaced objects
kvn
parents: 670
diff changeset
  1071
                    VM.getVM().getCodeCache().iterate(v);
6e0acfda1d47 6892186: SA does not dump debug info for scalar replaced objects
kvn
parents: 670
diff changeset
  1072
                }
6e0acfda1d47 6892186: SA does not dump debug info for scalar replaced objects
kvn
parents: 670
diff changeset
  1073
            }
6e0acfda1d47 6892186: SA does not dump debug info for scalar replaced objects
kvn
parents: 670
diff changeset
  1074
        },
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1075
        new Command("where", "where { -a | id }", false) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1076
            public void doit(Tokens t) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1077
                if (t.countTokens() != 1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1078
                    usage();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1079
                } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1080
                    String name = t.nextToken();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1081
                    Threads threads = VM.getVM().getThreads();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1082
                    boolean all = name.equals("-a");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1083
                    for (JavaThread thread = threads.first(); thread != null; thread = thread.next()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1084
                        ByteArrayOutputStream bos = new ByteArrayOutputStream();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1085
                        thread.printThreadIDOn(new PrintStream(bos));
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1086
                        if (all || bos.toString().equals(name)) {
5341
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1087
                            out.println(bos.toString() + " = " + thread.getAddress());
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1088
                            HTMLGenerator gen = new HTMLGenerator(false);
5341
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1089
                            try {
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1090
                                out.println(gen.genHTMLForJavaStackTrace(thread));
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1091
                            } catch (Exception e) {
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1092
                                err.println("Error: " + e);
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1093
                                if (verboseExceptions) {
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1094
                                    e.printStackTrace(err);
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1095
                                }
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1096
                            }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1097
                            if (!all) return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1098
                        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1099
                    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1100
                    if (!all) out.println("Couldn't find thread " + name);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1101
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1102
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1103
        },
5341
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1104
        new Command("thread", "thread { -a | id }", false) {
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1105
            public void doit(Tokens t) {
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1106
                if (t.countTokens() != 1) {
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1107
                    usage();
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1108
                } else {
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1109
                    String name = t.nextToken();
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1110
                    Threads threads = VM.getVM().getThreads();
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1111
                    boolean all = name.equals("-a");
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1112
                    for (JavaThread thread = threads.first(); thread != null; thread = thread.next()) {
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1113
                        ByteArrayOutputStream bos = new ByteArrayOutputStream();
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1114
                        thread.printThreadIDOn(new PrintStream(bos));
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1115
                        if (all || bos.toString().equals(name)) {
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1116
                            out.println(bos.toString() + " = " + thread.getAddress());
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1117
                            if (!all) return;
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1118
                        }
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1119
                    }
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1120
                    out.println("Couldn't find thread " + name);
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1121
                }
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1122
            }
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1123
        },
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1124
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1125
        new Command("threads", false) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1126
            public void doit(Tokens t) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1127
                if (t.countTokens() != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1128
                    usage();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1129
                } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1130
                    Threads threads = VM.getVM().getThreads();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1131
                    for (JavaThread thread = threads.first(); thread != null; thread = thread.next()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1132
                        thread.printThreadIDOn(out);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1133
                        out.println(" " + thread.getThreadName());
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1134
                    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1135
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1136
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1137
        },
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1138
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1139
        new Command("livenmethods", false) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1140
            public void doit(Tokens t) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1141
                if (t.countTokens() != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1142
                    usage();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1143
                } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1144
                    ArrayList nmethods = new ArrayList();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1145
                    Threads threads = VM.getVM().getThreads();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1146
                    HTMLGenerator gen = new HTMLGenerator(false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1147
                    for (JavaThread thread = threads.first(); thread != null; thread = thread.next()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1148
                        try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1149
                            for (JavaVFrame vf = thread.getLastJavaVFrameDbg(); vf != null; vf = vf.javaSender()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1150
                                if (vf instanceof CompiledVFrame) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1151
                                    NMethod c = ((CompiledVFrame)vf).getCode();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1152
                                    if (!nmethods.contains(c)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1153
                                        nmethods.add(c);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1154
                                        out.println(gen.genHTML(c));
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1155
                                    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1156
                                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1157
                            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1158
                        } catch (Exception e) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1159
                            e.printStackTrace();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1160
                        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1161
                    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1162
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1163
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1164
        },
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1165
        new Command("universe", false) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1166
            public void doit(Tokens t) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1167
                if (t.countTokens() != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1168
                    usage();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1169
                } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1170
                    Universe u = VM.getVM().getUniverse();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1171
                    out.println("Heap Parameters:");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1172
                    u.heap().printOn(out);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1173
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1174
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1175
        },
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1176
        new Command("verbose", "verbose true | false", true) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1177
            public void doit(Tokens t) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1178
                if (t.countTokens() != 1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1179
                    usage();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1180
                } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1181
                    verboseExceptions = Boolean.valueOf(t.nextToken()).booleanValue();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1182
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1183
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1184
        },
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1185
        new Command("assert", "assert true | false", true) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1186
            public void doit(Tokens t) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1187
                if (t.countTokens() != 1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1188
                    usage();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1189
                } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1190
                    Assert.ASSERTS_ENABLED = Boolean.valueOf(t.nextToken()).booleanValue();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1191
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1192
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1193
        },
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1194
    };
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1195
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1196
    private boolean verboseExceptions = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1197
    private ArrayList history = new ArrayList();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1198
    private HashMap commands = new HashMap();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1199
    private boolean doEcho = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1200
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1201
    private Command findCommand(String key) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1202
        return (Command)commands.get(key);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1203
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1204
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1205
    public void printPrompt() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1206
        out.print("hsdb> ");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1207
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1208
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1209
    private DebuggerInterface debugger;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1210
    private HotSpotAgent agent;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1211
    private JSJavaScriptEngine jsengine;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1212
    private BufferedReader in;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1213
    private PrintStream out;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1214
    private PrintStream err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1215
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1216
    // called before debuggee attach
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1217
    private void preAttach() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1218
        // nothing for now..
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1219
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1220
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1221
    // called after debuggee attach
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1222
    private void postAttach() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1223
        // create JavaScript engine and start it
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1224
        jsengine = new JSJavaScriptEngine() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1225
                        private ObjectReader reader = new ObjectReader();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1226
                        private JSJavaFactory factory = new JSJavaFactoryImpl();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1227
                        public ObjectReader getObjectReader() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1228
                            return reader;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1229
                        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1230
                        public JSJavaFactory getJSJavaFactory() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1231
                            return factory;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1232
                        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1233
                        protected void quit() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1234
                            debugger.detach();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1235
                            System.exit(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1236
                        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1237
                        protected BufferedReader getInputReader() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1238
                            return in;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1239
                        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1240
                        protected PrintStream getOutputStream() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1241
                            return out;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1242
                        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1243
                        protected PrintStream getErrorStream() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1244
                            return err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1245
                        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1246
                   };
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1247
        try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1248
            jsengine.defineFunction(this,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1249
                     this.getClass().getMethod("registerCommand",
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1250
                                new Class[] {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1251
                                     String.class, String.class, String.class
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1252
                                }));
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1253
        } catch (NoSuchMethodException exp) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1254
            // should not happen, see below...!!
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1255
            exp.printStackTrace();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1256
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1257
        jsengine.start();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1258
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1259
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1260
    public void registerCommand(String cmd, String usage, final String func) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1261
        commands.put(cmd, new Command(cmd, usage, false) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1262
                              public void doit(Tokens t) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1263
                                  final int len = t.countTokens();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1264
                                  Object[] args = new Object[len];
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1265
                                  for (int i = 0; i < len; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1266
                                      args[i] = t.nextToken();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1267
                                  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1268
                                  jsengine.call(func, args);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1269
                              }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1270
                          });
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1271
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1272
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1273
    public void setOutput(PrintStream o) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1274
        out = o;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1275
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1276
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1277
    public void setErr(PrintStream e) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1278
        err = e;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1279
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1280
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1281
    public CommandProcessor(DebuggerInterface debugger, BufferedReader in, PrintStream out, PrintStream err) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1282
        this.debugger = debugger;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1283
        this.agent = debugger.getAgent();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1284
        this.in = in;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1285
        this.out = out;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1286
        this.err = err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1287
        for (int i = 0; i < commandList.length; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1288
            Command c = commandList[i];
8076
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 6418
diff changeset
  1289
            if (commands.get(c.name) != null) {
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 6418
diff changeset
  1290
                throw new InternalError(c.name + " has multiple definitions");
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 6418
diff changeset
  1291
            }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1292
            commands.put(c.name, c);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1293
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1294
        if (debugger.isAttached()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1295
            postAttach();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1296
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1297
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1298
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1299
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1300
    public void run(boolean prompt) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1301
        // Process interactive commands.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1302
        while (true) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1303
            if (prompt) printPrompt();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1304
            String ln = null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1305
            try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1306
                ln = in.readLine();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1307
            } catch (IOException e) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1308
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1309
            if (ln == null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1310
                if (prompt) err.println("Input stream closed.");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1311
                return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1312
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1313
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1314
            executeCommand(ln);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1315
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1316
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1317
5341
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1318
    static Pattern historyPattern = Pattern.compile("((!\\*)|(!\\$)|(!!-?)|(!-?[0-9][0-9]*)|(![a-zA-Z][^ ]*))");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1319
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1320
    public void executeCommand(String ln) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1321
        if (ln.indexOf('!') != -1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1322
            int size = history.size();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1323
            if (size == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1324
                ln = "";
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1325
                err.println("History is empty");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1326
            } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1327
                StringBuffer result = new StringBuffer();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1328
                Matcher m = historyPattern.matcher(ln);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1329
                int start = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1330
                while (m.find()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1331
                    if (m.start() > start) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1332
                        result.append(ln.substring(start, m.start() - start));
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1333
                    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1334
                    start = m.end();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1335
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1336
                    String cmd = m.group();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1337
                    if (cmd.equals("!!")) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1338
                        result.append((String)history.get(history.size() - 1));
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1339
                    } else if (cmd.equals("!!-")) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1340
                        Tokens item = new Tokens((String)history.get(history.size() - 1));
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1341
                        item.trim(1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1342
                        result.append(item.join(" "));
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1343
                    } else if (cmd.equals("!*")) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1344
                        Tokens item = new Tokens((String)history.get(history.size() - 1));
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1345
                        item.nextToken();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1346
                        result.append(item.join(" "));
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1347
                    } else if (cmd.equals("!$")) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1348
                        Tokens item = new Tokens((String)history.get(history.size() - 1));
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1349
                        result.append(item.at(item.countTokens() - 1));
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1350
                    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1351
                        String tail = cmd.substring(1);
5341
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1352
                        switch (tail.charAt(0)) {
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1353
                        case '0':
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1354
                        case '1':
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1355
                        case '2':
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1356
                        case '3':
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1357
                        case '4':
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1358
                        case '5':
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1359
                        case '6':
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1360
                        case '7':
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1361
                        case '8':
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1362
                        case '9':
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1363
                        case '-': {
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1364
                            int index = Integer.parseInt(tail);
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1365
                            if (index < 0) {
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1366
                                index = history.size() + index;
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1367
                            }
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1368
                            if (index > size) {
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1369
                                err.println("No such history item");
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1370
                            } else {
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1371
                                result.append((String)history.get(index));
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1372
                            }
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1373
                            break;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1374
                        }
5341
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1375
                        default: {
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1376
                            for (int i = history.size() - 1; i >= 0; i--) {
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1377
                                String s = (String)history.get(i);
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1378
                                if (s.startsWith(tail)) {
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1379
                                    result.append(s);
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1380
                                }
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1381
                            }
290a02b4adb3 6945219: minor SA fixes
never
parents: 4095
diff changeset
  1382
                        }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1383
                        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1384
                    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1385
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1386
                if (result.length() == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1387
                    err.println("malformed history reference");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1388
                    ln = "";
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1389
                } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1390
                    if (start < ln.length()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1391
                        result.append(ln.substring(start));
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1392
                    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1393
                    ln = result.toString();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1394
                    if (!doEcho) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1395
                        out.println(ln);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1396
                    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1397
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1398
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1399
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1400
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1401
        if (doEcho) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1402
            out.println("+ " + ln);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1403
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1404
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1405
        PrintStream redirect = null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1406
        Tokens t = new Tokens(ln);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1407
        if (t.hasMoreTokens()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1408
            boolean error = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1409
            history.add(ln);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1410
            int len = t.countTokens();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1411
            if (len > 2) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1412
                String r = t.at(len - 2);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1413
                if (r.equals(">") || r.equals(">>")) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1414
                    boolean append = r.length() == 2;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1415
                    String file = t.at(len - 1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1416
                    try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1417
                        redirect = new PrintStream(new BufferedOutputStream(new FileOutputStream(file, append)));
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1418
                        t.trim(2);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1419
                    } catch (Exception e) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1420
                        out.println("Error: " + e);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1421
                        if (verboseExceptions) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1422
                            e.printStackTrace(out);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1423
                        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1424
                        error = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1425
                    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1426
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1427
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1428
            if (!error) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1429
                PrintStream savedout = out;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1430
                if (redirect != null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1431
                    out = redirect;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1432
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1433
                try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1434
                    executeCommand(t);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1435
                } catch (Exception e) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1436
                    err.println("Error: " + e);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1437
                    if (verboseExceptions) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1438
                        e.printStackTrace(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1439
                    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1440
                } finally {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1441
                    if (redirect != null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1442
                        out = savedout;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1443
                        redirect.close();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1444
                    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1445
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1446
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1447
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1448
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1449
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1450
    void executeCommand(Tokens args) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1451
        String cmd = args.nextToken();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1452
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1453
        Command doit = findCommand(cmd);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1454
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1455
        /*
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1456
         * Check for an unknown command
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1457
         */
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1458
        if (doit == null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1459
            out.println("Unrecognized command.  Try help...");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1460
        } else if (!debugger.isAttached() && !doit.okIfDisconnected) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1461
            out.println("Command not valid until the attached to a VM");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1462
        } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1463
            try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1464
                doit.doit(args);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1465
            } catch (Exception e) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1466
                out.println("Error: " + e);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1467
                if (verboseExceptions) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1468
                    e.printStackTrace(out);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1469
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1470
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1471
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1472
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1473
}