jdk/src/share/classes/com/sun/tools/example/debug/tty/ThreadInfo.java
author alanb
Fri, 02 Nov 2012 15:50:11 +0000
changeset 14342 8435a30053c1
parent 10292 ed7db6a12c2a
permissions -rw-r--r--
7197491: update copyright year to match last edit in jdk8 jdk repository Reviewed-by: chegar, ksrini
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 10292
diff changeset
     2
 * Copyright (c) 1998, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
10292
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 9520
diff changeset
    26
/*
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 9520
diff changeset
    27
 * This source code is provided to illustrate the usage of a given feature
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 9520
diff changeset
    28
 * or technique and has been deliberately simplified. Additional steps
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 9520
diff changeset
    29
 * required for a production-quality application, such as security checks,
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 9520
diff changeset
    30
 * input validation and proper error handling, might not be present in
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 9520
diff changeset
    31
 * this sample code.
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 9520
diff changeset
    32
 */
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 9520
diff changeset
    33
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 9520
diff changeset
    34
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
package com.sun.tools.example.debug.tty;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import com.sun.jdi.ThreadReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import com.sun.jdi.ThreadGroupReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import com.sun.jdi.IncompatibleThreadStateException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import com.sun.jdi.StackFrame;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.util.Collections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
class ThreadInfo {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    // This is a list of all known ThreadInfo objects. It survives
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    // ThreadInfo.invalidateAll, unlike the other static fields below.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private static List<ThreadInfo> threads = Collections.synchronizedList(new ArrayList<ThreadInfo>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private static boolean gotInitialThreads = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private static ThreadInfo current = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private static ThreadGroupReference group = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private final ThreadReference thread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private int currentFrameIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private ThreadInfo(ThreadReference thread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        this.thread = thread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        if (thread == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            MessageOutput.fatalError("Internal error: null ThreadInfo created");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private static void initThreads() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        if (!gotInitialThreads) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    66
            for (ThreadReference thread : Env.vm().allThreads()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                threads.add(new ThreadInfo(thread));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            gotInitialThreads = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    static void addThread(ThreadReference thread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        synchronized (threads) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            initThreads();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            ThreadInfo ti = new ThreadInfo(thread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            // Guard against duplicates. Duplicates can happen during
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            // initialization when a particular thread might be added both
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            // by a thread start event and by the initial call to threads()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            if (getThreadInfo(thread) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                threads.add(ti);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    static void removeThread(ThreadReference thread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        if (thread.equals(ThreadInfo.current)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            // Current thread has died.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            // Be careful getting the thread name. If its death happens
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            // as part of VM termination, it may be too late to get the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            // information, and an exception will be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            String currentThreadName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
               currentThreadName = "\"" + thread.name() + "\"";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
               currentThreadName = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            setCurrentThread(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            MessageOutput.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            MessageOutput.println("Current thread died. Execution continuing...",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                                  currentThreadName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        threads.remove(getThreadInfo(thread));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    static List<ThreadInfo> threads() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        synchronized(threads) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            initThreads();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            // Make a copy to allow iteration without synchronization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            return new ArrayList<ThreadInfo>(threads);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    static void invalidateAll() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        current = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        group = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        synchronized (threads) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   121
            for (ThreadInfo ti : threads()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                ti.invalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    static void setThreadGroup(ThreadGroupReference tg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        group = tg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    static void setCurrentThread(ThreadReference tr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        if (tr == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            setCurrentThreadInfo(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            ThreadInfo tinfo = getThreadInfo(tr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            setCurrentThreadInfo(tinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    static void setCurrentThreadInfo(ThreadInfo tinfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        current = tinfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        if (current != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            current.invalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * Get the current ThreadInfo object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @return the ThreadInfo for the current thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    static ThreadInfo getCurrentThreadInfo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        return current;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * Get the thread from this ThreadInfo object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @return the Thread wrapped by this ThreadInfo.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    ThreadReference getThread() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        return thread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    static ThreadGroupReference group() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        if (group == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            // Current thread group defaults to the first top level
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            // thread group.
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   169
            setThreadGroup(Env.vm().topLevelThreadGroups().get(0));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        return group;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    static ThreadInfo getThreadInfo(long id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        ThreadInfo retInfo = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        synchronized (threads) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   178
            for (ThreadInfo ti : threads()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                if (ti.thread.uniqueID() == id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                   retInfo = ti;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                   break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        return retInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    static ThreadInfo getThreadInfo(ThreadReference tr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        return getThreadInfo(tr.uniqueID());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    static ThreadInfo getThreadInfo(String idToken) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        ThreadInfo tinfo = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        if (idToken.startsWith("t@")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            idToken = idToken.substring(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            long threadId = Long.decode(idToken).longValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            tinfo = getThreadInfo(threadId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        } catch (NumberFormatException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            tinfo = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        return tinfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * Get the thread stack frames.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * @return a <code>List</code> of the stack frames.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   211
    List<StackFrame> getStack() throws IncompatibleThreadStateException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        return thread.frames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * Get the current stackframe.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * @return the current stackframe.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    StackFrame getCurrentFrame() throws IncompatibleThreadStateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        if (thread.frameCount() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        return thread.frame(currentFrameIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * Invalidate the current stackframe index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    void invalidate() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        currentFrameIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    /* Throw IncompatibleThreadStateException if not suspended */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    private void assureSuspended() throws IncompatibleThreadStateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        if (!thread.isSuspended()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            throw new IncompatibleThreadStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * Get the current stackframe index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * @return the number of the current stackframe.  Frame zero is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * closest to the current program counter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    int getCurrentFrameIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        return currentFrameIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * Set the current stackframe to a specific frame.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * @param nFrame    the number of the desired stackframe.  Frame zero is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * closest to the current program counter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * @exception IllegalAccessError when the thread isn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * suspended or waiting at a breakpoint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * @exception ArrayIndexOutOfBoundsException when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * requested frame is beyond the stack boundary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    void setCurrentFrameIndex(int nFrame) throws IncompatibleThreadStateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        assureSuspended();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        if ((nFrame < 0) || (nFrame >= thread.frameCount())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            throw new ArrayIndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        currentFrameIndex = nFrame;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * Change the current stackframe to be one or more frames higher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * (as in, away from the current program counter).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * @param nFrames   the number of stackframes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * @exception IllegalAccessError when the thread isn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * suspended or waiting at a breakpoint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * @exception ArrayIndexOutOfBoundsException when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * requested frame is beyond the stack boundary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    void up(int nFrames) throws IncompatibleThreadStateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        setCurrentFrameIndex(currentFrameIndex + nFrames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * Change the current stackframe to be one or more frames lower
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * (as in, toward the current program counter).     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * @param nFrames   the number of stackframes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * @exception IllegalAccessError when the thread isn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * suspended or waiting at a breakpoint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * @exception ArrayIndexOutOfBoundsException when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * requested frame is beyond the stack boundary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    void down(int nFrames) throws IncompatibleThreadStateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        setCurrentFrameIndex(currentFrameIndex - nFrames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
}