src/jdk.jdi/share/classes/com/sun/tools/jdi/ThreadReferenceImpl.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 45714 jdk/src/jdk.jdi/share/classes/com/sun/tools/jdi/ThreadReferenceImpl.java@1820d351198d
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
45714
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 28107
diff changeset
     2
 * Copyright (c) 1998, 2017, 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: 1330
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: 1330
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: 1330
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1330
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1330
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.jdi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
45714
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 28107
diff changeset
    28
import java.lang.ref.WeakReference;
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 28107
diff changeset
    29
import java.util.ArrayList;
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 28107
diff changeset
    30
import java.util.Arrays;
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 28107
diff changeset
    31
import java.util.Collections;
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 28107
diff changeset
    32
import java.util.Iterator;
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 28107
diff changeset
    33
import java.util.List;
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 28107
diff changeset
    34
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 28107
diff changeset
    35
import com.sun.jdi.ClassNotLoadedException;
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 28107
diff changeset
    36
import com.sun.jdi.IncompatibleThreadStateException;
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 28107
diff changeset
    37
import com.sun.jdi.InternalException;
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 28107
diff changeset
    38
import com.sun.jdi.InvalidStackFrameException;
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 28107
diff changeset
    39
import com.sun.jdi.InvalidTypeException;
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 28107
diff changeset
    40
import com.sun.jdi.Location;
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 28107
diff changeset
    41
import com.sun.jdi.MonitorInfo;
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 28107
diff changeset
    42
import com.sun.jdi.NativeMethodException;
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 28107
diff changeset
    43
import com.sun.jdi.ObjectReference;
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 28107
diff changeset
    44
import com.sun.jdi.ReferenceType;
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 28107
diff changeset
    45
import com.sun.jdi.StackFrame;
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 28107
diff changeset
    46
import com.sun.jdi.ThreadGroupReference;
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 28107
diff changeset
    47
import com.sun.jdi.ThreadReference;
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 28107
diff changeset
    48
import com.sun.jdi.Value;
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 28107
diff changeset
    49
import com.sun.jdi.VirtualMachine;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import com.sun.jdi.request.BreakpointRequest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
public class ThreadReferenceImpl extends ObjectReferenceImpl
45714
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 28107
diff changeset
    53
                                 implements ThreadReference {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    static final int SUSPEND_STATUS_SUSPENDED = 0x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    static final int SUSPEND_STATUS_BREAK = 0x2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private int suspendedZombieCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
836
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
    59
    /*
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
    60
     * Some objects can only be created while a thread is suspended and are valid
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
    61
     * only while the thread remains suspended.  Examples are StackFrameImpl
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
    62
     * and MonitorInfoImpl.  When the thread resumes, these objects have to be
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
    63
     * marked as invalid so that their methods can throw
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
    64
     * InvalidStackFrameException if they are called.  To do this, such objects
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
    65
     * register themselves as listeners of the associated thread.  When the
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
    66
     * thread is resumed, its listeners are notified and mark themselves
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
    67
     * invalid.
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
    68
     * Also, note that ThreadReferenceImpl itself caches some info that
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
    69
     * is valid only as long as the thread is suspended.  When the thread
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
    70
     * is resumed, that cache must be purged.
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
    71
     * Lastly, note that ThreadReferenceImpl and its super, ObjectReferenceImpl
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
    72
     * cache some info that is only valid as long as the entire VM is suspended.
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
    73
     * If _any_ thread is resumed, this cache must be purged.  To handle this,
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
    74
     * both ThreadReferenceImpl and ObjectReferenceImpl register themselves as
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
    75
     * VMListeners so that they get notified when all threads are suspended and
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
    76
     * when any thread is resumed.
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
    77
     */
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
    78
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
    79
    // This is cached for the life of the thread
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
    80
    private ThreadGroupReference threadGroup;
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
    81
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
    82
    // This is cached only while this one thread is suspended.  Each time
1326
0c330a451ed9 6751643: ThreadReference.ownedMonitors() can return null
jjh
parents: 836
diff changeset
    83
    // the thread is resumed, we abandon the current cache object and
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 14342
diff changeset
    84
    // create a new initialized one.
836
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
    85
    private static class LocalCache {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        JDWP.ThreadReference.Status status = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        List<StackFrame> frames = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        int framesStart = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        int framesLength = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        int frameCount = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        List<ObjectReference> ownedMonitors = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        List<MonitorInfo> ownedMonitorsInfo = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        ObjectReference contendedMonitor = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        boolean triedCurrentContended = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
1326
0c330a451ed9 6751643: ThreadReference.ownedMonitors() can return null
jjh
parents: 836
diff changeset
    97
    /*
0c330a451ed9 6751643: ThreadReference.ownedMonitors() can return null
jjh
parents: 836
diff changeset
    98
     * The localCache instance var is set by resetLocalCache to an initialized
0c330a451ed9 6751643: ThreadReference.ownedMonitors() can return null
jjh
parents: 836
diff changeset
    99
     * object as shown above.  This occurs when the ThreadReference
0c330a451ed9 6751643: ThreadReference.ownedMonitors() can return null
jjh
parents: 836
diff changeset
   100
     * object is created, and when the mirrored thread is resumed.
0c330a451ed9 6751643: ThreadReference.ownedMonitors() can return null
jjh
parents: 836
diff changeset
   101
     * The fields are then filled in by the relevant methods as they
0c330a451ed9 6751643: ThreadReference.ownedMonitors() can return null
jjh
parents: 836
diff changeset
   102
     * are called.  A problem can occur if resetLocalCache is called
0c330a451ed9 6751643: ThreadReference.ownedMonitors() can return null
jjh
parents: 836
diff changeset
   103
     * (ie, a resume() is executed) at certain points in the execution
0c330a451ed9 6751643: ThreadReference.ownedMonitors() can return null
jjh
parents: 836
diff changeset
   104
     * of some of these methods - see 6751643.  To avoid this, each
0c330a451ed9 6751643: ThreadReference.ownedMonitors() can return null
jjh
parents: 836
diff changeset
   105
     * method that wants to use this cache must make a local copy of
0c330a451ed9 6751643: ThreadReference.ownedMonitors() can return null
jjh
parents: 836
diff changeset
   106
     * this variable and use that.  This means that each invocation of
0c330a451ed9 6751643: ThreadReference.ownedMonitors() can return null
jjh
parents: 836
diff changeset
   107
     * these methods will use a copy of the cache object that was in
0c330a451ed9 6751643: ThreadReference.ownedMonitors() can return null
jjh
parents: 836
diff changeset
   108
     * effect at the point that the copy was made; if a racy resume
0c330a451ed9 6751643: ThreadReference.ownedMonitors() can return null
jjh
parents: 836
diff changeset
   109
     * occurs, it won't affect the method's local copy.  This means that
0c330a451ed9 6751643: ThreadReference.ownedMonitors() can return null
jjh
parents: 836
diff changeset
   110
     * the values returned by these calls may not match the state of
0c330a451ed9 6751643: ThreadReference.ownedMonitors() can return null
jjh
parents: 836
diff changeset
   111
     * the debuggee at the time the caller gets the values.  EG,
0c330a451ed9 6751643: ThreadReference.ownedMonitors() can return null
jjh
parents: 836
diff changeset
   112
     * frameCount() is called and comes up with 5 frames.  But before
0c330a451ed9 6751643: ThreadReference.ownedMonitors() can return null
jjh
parents: 836
diff changeset
   113
     * it returns this, a resume of the debuggee thread is executed in a
0c330a451ed9 6751643: ThreadReference.ownedMonitors() can return null
jjh
parents: 836
diff changeset
   114
     * different debugger thread.  The thread is resumed and running at
0c330a451ed9 6751643: ThreadReference.ownedMonitors() can return null
jjh
parents: 836
diff changeset
   115
     * the time that the value 5 is returned.  Or even worse, the thread
0c330a451ed9 6751643: ThreadReference.ownedMonitors() can return null
jjh
parents: 836
diff changeset
   116
     * could be suspended again and have a different number of frames, eg, 24,
0c330a451ed9 6751643: ThreadReference.ownedMonitors() can return null
jjh
parents: 836
diff changeset
   117
     * but this call will still return 5.
0c330a451ed9 6751643: ThreadReference.ownedMonitors() can return null
jjh
parents: 836
diff changeset
   118
     */
836
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
   119
    private LocalCache localCache;
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
   120
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
   121
    private void resetLocalCache() {
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
   122
        localCache = new LocalCache();
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
   123
    }
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
   124
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
   125
    // This is cached only while all threads in the VM are suspended
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
   126
    // Yes, someone could change the name of a thread while it is suspended.
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
   127
    private static class Cache extends ObjectReferenceImpl.Cache {
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
   128
        String name = null;
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
   129
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    protected ObjectReferenceImpl.Cache newCache() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        return new Cache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    // Listeners - synchronized on vm.state()
45714
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 28107
diff changeset
   135
    private List<WeakReference<ThreadListener>> listeners = new ArrayList<>();
836
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
   136
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    ThreadReferenceImpl(VirtualMachine aVm, long aRef) {
45714
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 28107
diff changeset
   138
        super(aVm, aRef);
836
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
   139
        resetLocalCache();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        vm.state().addListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    protected String description() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        return "ThreadReference " + uniqueID();
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
     * VMListener implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    public boolean vmNotSuspended(VMAction action) {
836
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
   151
        if (action.resumingThread() == null) {
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
   152
            // all threads are being resumed
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
   153
            synchronized (vm.state()) {
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
   154
                processThreadAction(new ThreadAction(this,
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
   155
                                            ThreadAction.THREAD_RESUMABLE));
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
   156
            }
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
   157
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
836
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
   159
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
   160
        /*
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
   161
         * Othewise, only one thread is being resumed:
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
   162
         *   if it is us,
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
   163
         *      we have already done our processThreadAction to notify our
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
   164
         *      listeners when we processed the resume.
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
   165
         *   if it is not us,
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
   166
         *      we don't want to notify our listeners
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
   167
         *       because we are not being resumed.
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
   168
         */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        return super.vmNotSuspended(action);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    /**
1326
0c330a451ed9 6751643: ThreadReference.ownedMonitors() can return null
jjh
parents: 836
diff changeset
   173
     * Note that we only cache the name string while the entire VM is suspended
0c330a451ed9 6751643: ThreadReference.ownedMonitors() can return null
jjh
parents: 836
diff changeset
   174
     * because the name can change via Thread.setName arbitrarily while this
0c330a451ed9 6751643: ThreadReference.ownedMonitors() can return null
jjh
parents: 836
diff changeset
   175
     * thread is running.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    public String name() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        String name = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            Cache local = (Cache)getCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            if (local != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                name = local.name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            if (name == null) {
45714
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 28107
diff changeset
   186
                name = JDWP.ThreadReference.Name.process(vm, this).threadName;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                if (local != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                    local.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        } catch (JDWPException exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            throw exc.toJDIException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * Sends a command to the back end which is defined to do an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * implicit vm-wide resume.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    PacketStream sendResumingCommand(CommandSender sender) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        synchronized (vm.state()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            processThreadAction(new ThreadAction(this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                                        ThreadAction.THREAD_RESUMABLE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            return sender.send();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    public void suspend() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            JDWP.ThreadReference.Suspend.process(vm, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        } catch (JDWPException exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            throw exc.toJDIException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        // Don't consider the thread suspended yet. On reply, notifySuspend()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        // will be called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    public void resume() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
         * If it's a zombie, we can just update internal state without
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
         * going to back end.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        if (suspendedZombieCount > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            suspendedZombieCount--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        PacketStream stream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        synchronized (vm.state()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            processThreadAction(new ThreadAction(this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                                      ThreadAction.THREAD_RESUMABLE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            stream = JDWP.ThreadReference.Resume.enqueueCommand(vm, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            JDWP.ThreadReference.Resume.waitForReply(vm, stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        } catch (JDWPException exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            throw exc.toJDIException();
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
    public int suspendCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
         * If it's a zombie, we maintain the count in the front end.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        if (suspendedZombieCount > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            return suspendedZombieCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            return JDWP.ThreadReference.SuspendCount.process(vm, this).suspendCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        } catch (JDWPException exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            throw exc.toJDIException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    public void stop(ObjectReference throwable) throws InvalidTypeException {
28097
fa1a96810647 6618335: ThreadReference.stop(null) throws NPE instead of InvalidTypeException
egahlin
parents: 25859
diff changeset
   258
        validateMirrorOrNull(throwable);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        // Verify that the given object is a Throwable instance
11277
e3a1c90dd439 7117053: Fix build warnings in com/sun/tools/jdi/*
jjh
parents: 5506
diff changeset
   260
        List<ReferenceType> list = vm.classesByName("java.lang.Throwable");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        ClassTypeImpl throwableClass = (ClassTypeImpl)list.get(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        if ((throwable == null) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            !throwableClass.isAssignableFrom(throwable)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
             throw new InvalidTypeException("Not an instance of Throwable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            JDWP.ThreadReference.Stop.process(vm, this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                                         (ObjectReferenceImpl)throwable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        } catch (JDWPException exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            throw exc.toJDIException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    public void interrupt() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            JDWP.ThreadReference.Interrupt.process(vm, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        } catch (JDWPException exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            throw exc.toJDIException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    private JDWP.ThreadReference.Status jdwpStatus() {
1326
0c330a451ed9 6751643: ThreadReference.ownedMonitors() can return null
jjh
parents: 836
diff changeset
   284
        LocalCache snapshot = localCache;
0c330a451ed9 6751643: ThreadReference.ownedMonitors() can return null
jjh
parents: 836
diff changeset
   285
        JDWP.ThreadReference.Status myStatus = snapshot.status;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        try {
836
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
   287
             if (myStatus == null) {
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
   288
                 myStatus = JDWP.ThreadReference.Status.process(vm, this);
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
   289
                if ((myStatus.suspendStatus & SUSPEND_STATUS_SUSPENDED) != 0) {
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
   290
                    // thread is suspended, we can cache the status.
1326
0c330a451ed9 6751643: ThreadReference.ownedMonitors() can return null
jjh
parents: 836
diff changeset
   291
                    snapshot.status = myStatus;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            }
836
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
   294
         } catch (JDWPException exc) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            throw exc.toJDIException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        }
1326
0c330a451ed9 6751643: ThreadReference.ownedMonitors() can return null
jjh
parents: 836
diff changeset
   297
        return myStatus;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    public int status() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        return jdwpStatus().threadStatus;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    public boolean isSuspended() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        return ((suspendedZombieCount > 0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                ((jdwpStatus().suspendStatus & SUSPEND_STATUS_SUSPENDED) != 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    public boolean isAtBreakpoint() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
         * TO DO: This fails to take filters into account.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            StackFrame frame = frame(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            Location location = frame.location();
11277
e3a1c90dd439 7117053: Fix build warnings in com/sun/tools/jdi/*
jjh
parents: 5506
diff changeset
   316
            List<BreakpointRequest> requests = vm.eventRequestManager().breakpointRequests();
e3a1c90dd439 7117053: Fix build warnings in com/sun/tools/jdi/*
jjh
parents: 5506
diff changeset
   317
            Iterator<BreakpointRequest> iter = requests.iterator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            while (iter.hasNext()) {
11277
e3a1c90dd439 7117053: Fix build warnings in com/sun/tools/jdi/*
jjh
parents: 5506
diff changeset
   319
                BreakpointRequest request = iter.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                if (location.equals(request.location())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        } catch (IndexOutOfBoundsException iobe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            return false;  // no frames on stack => not at breakpoint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        } catch (IncompatibleThreadStateException itse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            // Per the javadoc, not suspended => return false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    public ThreadGroupReference threadGroup() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        /*
836
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
   335
         * Thread group can't change, so it's cached once and for all.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        if (threadGroup == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                threadGroup = JDWP.ThreadReference.ThreadGroup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                    process(vm, this).group;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            } catch (JDWPException exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                throw exc.toJDIException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        return threadGroup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    public int frameCount() throws IncompatibleThreadStateException  {
1326
0c330a451ed9 6751643: ThreadReference.ownedMonitors() can return null
jjh
parents: 836
diff changeset
   349
        LocalCache snapshot = localCache;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        try {
1326
0c330a451ed9 6751643: ThreadReference.ownedMonitors() can return null
jjh
parents: 836
diff changeset
   351
            if (snapshot.frameCount == -1) {
0c330a451ed9 6751643: ThreadReference.ownedMonitors() can return null
jjh
parents: 836
diff changeset
   352
                snapshot.frameCount = JDWP.ThreadReference.FrameCount
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                                          .process(vm, this).frameCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        } catch (JDWPException exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            switch (exc.errorCode()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            case JDWP.Error.THREAD_NOT_SUSPENDED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            case JDWP.Error.INVALID_THREAD:   /* zombie */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                throw new IncompatibleThreadStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                throw exc.toJDIException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        }
1326
0c330a451ed9 6751643: ThreadReference.ownedMonitors() can return null
jjh
parents: 836
diff changeset
   364
        return snapshot.frameCount;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    public List<StackFrame> frames() throws IncompatibleThreadStateException  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        return privateFrames(0, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    public StackFrame frame(int index) throws IncompatibleThreadStateException  {
11277
e3a1c90dd439 7117053: Fix build warnings in com/sun/tools/jdi/*
jjh
parents: 5506
diff changeset
   372
        List<StackFrame> list = privateFrames(index, 1);
e3a1c90dd439 7117053: Fix build warnings in com/sun/tools/jdi/*
jjh
parents: 5506
diff changeset
   373
        return list.get(0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * Is the requested subrange within what has been retrieved?
836
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
   378
     * local is known to be non-null.  Should only be called from
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
   379
     * a sync method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     */
1326
0c330a451ed9 6751643: ThreadReference.ownedMonitors() can return null
jjh
parents: 836
diff changeset
   381
    private boolean isSubrange(LocalCache snapshot,
836
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
   382
                               int start, int length) {
1326
0c330a451ed9 6751643: ThreadReference.ownedMonitors() can return null
jjh
parents: 836
diff changeset
   383
        if (start < snapshot.framesStart) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        if (length == -1) {
1326
0c330a451ed9 6751643: ThreadReference.ownedMonitors() can return null
jjh
parents: 836
diff changeset
   387
            return (snapshot.framesLength == -1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        }
1326
0c330a451ed9 6751643: ThreadReference.ownedMonitors() can return null
jjh
parents: 836
diff changeset
   389
        if (snapshot.framesLength == -1) {
0c330a451ed9 6751643: ThreadReference.ownedMonitors() can return null
jjh
parents: 836
diff changeset
   390
            if ((start + length) > (snapshot.framesStart +
0c330a451ed9 6751643: ThreadReference.ownedMonitors() can return null
jjh
parents: 836
diff changeset
   391
                                    snapshot.frames.size())) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        }
1326
0c330a451ed9 6751643: ThreadReference.ownedMonitors() can return null
jjh
parents: 836
diff changeset
   396
        return ((start + length) <= (snapshot.framesStart + snapshot.framesLength));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    public List<StackFrame> frames(int start, int length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                              throws IncompatibleThreadStateException  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        if (length < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            throw new IndexOutOfBoundsException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                "length must be greater than or equal to zero");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        return privateFrames(start, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * Private version of frames() allows "-1" to specify all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * remaining frames.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     */
836
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
   412
    synchronized private List<StackFrame> privateFrames(int start, int length)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                              throws IncompatibleThreadStateException  {
836
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
   414
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
   415
        // Lock must be held while creating stack frames so if that two threads
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
   416
        // do this at the same time, one won't clobber the subset created by the other.
1326
0c330a451ed9 6751643: ThreadReference.ownedMonitors() can return null
jjh
parents: 836
diff changeset
   417
        LocalCache snapshot = localCache;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        try {
1326
0c330a451ed9 6751643: ThreadReference.ownedMonitors() can return null
jjh
parents: 836
diff changeset
   419
            if (snapshot.frames == null || !isSubrange(snapshot, start, length)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                JDWP.ThreadReference.Frames.Frame[] jdwpFrames
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                    = JDWP.ThreadReference.Frames.
836
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
   422
                    process(vm, this, start, length).frames;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                int count = jdwpFrames.length;
45714
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 28107
diff changeset
   424
                snapshot.frames = new ArrayList<>(count);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
836
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
   426
                for (int i = 0; i<count; i++) {
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
   427
                    if (jdwpFrames[i].location == null) {
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
   428
                        throw new InternalException("Invalid frame location");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                    }
836
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
   430
                    StackFrame frame = new StackFrameImpl(vm, this,
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
   431
                                                          jdwpFrames[i].frameID,
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
   432
                                                          jdwpFrames[i].location);
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
   433
                    // Add to the frame list
1326
0c330a451ed9 6751643: ThreadReference.ownedMonitors() can return null
jjh
parents: 836
diff changeset
   434
                    snapshot.frames.add(frame);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                }
1326
0c330a451ed9 6751643: ThreadReference.ownedMonitors() can return null
jjh
parents: 836
diff changeset
   436
                snapshot.framesStart = start;
0c330a451ed9 6751643: ThreadReference.ownedMonitors() can return null
jjh
parents: 836
diff changeset
   437
                snapshot.framesLength = length;
0c330a451ed9 6751643: ThreadReference.ownedMonitors() can return null
jjh
parents: 836
diff changeset
   438
                return Collections.unmodifiableList(snapshot.frames);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            } else {
1326
0c330a451ed9 6751643: ThreadReference.ownedMonitors() can return null
jjh
parents: 836
diff changeset
   440
                int fromIndex = start - snapshot.framesStart;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                int toIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                if (length == -1) {
1326
0c330a451ed9 6751643: ThreadReference.ownedMonitors() can return null
jjh
parents: 836
diff changeset
   443
                    toIndex = snapshot.frames.size() - fromIndex;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                    toIndex = fromIndex + length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                }
1326
0c330a451ed9 6751643: ThreadReference.ownedMonitors() can return null
jjh
parents: 836
diff changeset
   447
                return Collections.unmodifiableList(snapshot.frames.subList(fromIndex, toIndex));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        } catch (JDWPException exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            switch (exc.errorCode()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            case JDWP.Error.THREAD_NOT_SUSPENDED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            case JDWP.Error.INVALID_THREAD:   /* zombie */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                throw new IncompatibleThreadStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                throw exc.toJDIException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    public List<ObjectReference> ownedMonitors()  throws IncompatibleThreadStateException  {
1326
0c330a451ed9 6751643: ThreadReference.ownedMonitors() can return null
jjh
parents: 836
diff changeset
   461
        LocalCache snapshot = localCache;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        try {
1326
0c330a451ed9 6751643: ThreadReference.ownedMonitors() can return null
jjh
parents: 836
diff changeset
   463
            if (snapshot.ownedMonitors == null) {
0c330a451ed9 6751643: ThreadReference.ownedMonitors() can return null
jjh
parents: 836
diff changeset
   464
                snapshot.ownedMonitors = Arrays.asList(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                                 (ObjectReference[])JDWP.ThreadReference.OwnedMonitors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                                         process(vm, this).owned);
11277
e3a1c90dd439 7117053: Fix build warnings in com/sun/tools/jdi/*
jjh
parents: 5506
diff changeset
   467
                if ((vm.traceFlags & VirtualMachine.TRACE_OBJREFS) != 0) {
836
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
   468
                    vm.printTrace(description() +
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
   469
                                  " temporarily caching owned monitors"+
1326
0c330a451ed9 6751643: ThreadReference.ownedMonitors() can return null
jjh
parents: 836
diff changeset
   470
                                  " (count = " + snapshot.ownedMonitors.size() + ")");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        } catch (JDWPException exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            switch (exc.errorCode()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            case JDWP.Error.THREAD_NOT_SUSPENDED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            case JDWP.Error.INVALID_THREAD:   /* zombie */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                throw new IncompatibleThreadStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                throw exc.toJDIException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        }
1326
0c330a451ed9 6751643: ThreadReference.ownedMonitors() can return null
jjh
parents: 836
diff changeset
   482
        return snapshot.ownedMonitors;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    public ObjectReference currentContendedMonitor()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                              throws IncompatibleThreadStateException  {
1326
0c330a451ed9 6751643: ThreadReference.ownedMonitors() can return null
jjh
parents: 836
diff changeset
   487
        LocalCache snapshot = localCache;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        try {
1326
0c330a451ed9 6751643: ThreadReference.ownedMonitors() can return null
jjh
parents: 836
diff changeset
   489
            if (snapshot.contendedMonitor == null &&
0c330a451ed9 6751643: ThreadReference.ownedMonitors() can return null
jjh
parents: 836
diff changeset
   490
                !snapshot.triedCurrentContended) {
0c330a451ed9 6751643: ThreadReference.ownedMonitors() can return null
jjh
parents: 836
diff changeset
   491
                snapshot.contendedMonitor = JDWP.ThreadReference.CurrentContendedMonitor.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                    process(vm, this).monitor;
1326
0c330a451ed9 6751643: ThreadReference.ownedMonitors() can return null
jjh
parents: 836
diff changeset
   493
                snapshot.triedCurrentContended = true;
0c330a451ed9 6751643: ThreadReference.ownedMonitors() can return null
jjh
parents: 836
diff changeset
   494
                if ((snapshot.contendedMonitor != null) &&
11277
e3a1c90dd439 7117053: Fix build warnings in com/sun/tools/jdi/*
jjh
parents: 5506
diff changeset
   495
                    ((vm.traceFlags & VirtualMachine.TRACE_OBJREFS) != 0)) {
836
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
   496
                    vm.printTrace(description() +
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
   497
                                  " temporarily caching contended monitor"+
1326
0c330a451ed9 6751643: ThreadReference.ownedMonitors() can return null
jjh
parents: 836
diff changeset
   498
                                  " (id = " + snapshot.contendedMonitor.uniqueID() + ")");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        } catch (JDWPException exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            switch (exc.errorCode()) {
1326
0c330a451ed9 6751643: ThreadReference.ownedMonitors() can return null
jjh
parents: 836
diff changeset
   503
            case JDWP.Error.THREAD_NOT_SUSPENDED:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            case JDWP.Error.INVALID_THREAD:   /* zombie */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                throw new IncompatibleThreadStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                throw exc.toJDIException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        }
1326
0c330a451ed9 6751643: ThreadReference.ownedMonitors() can return null
jjh
parents: 836
diff changeset
   510
        return snapshot.contendedMonitor;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    public List<MonitorInfo> ownedMonitorsAndFrames()  throws IncompatibleThreadStateException  {
1326
0c330a451ed9 6751643: ThreadReference.ownedMonitors() can return null
jjh
parents: 836
diff changeset
   514
        LocalCache snapshot = localCache;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        try {
1326
0c330a451ed9 6751643: ThreadReference.ownedMonitors() can return null
jjh
parents: 836
diff changeset
   516
            if (snapshot.ownedMonitorsInfo == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                JDWP.ThreadReference.OwnedMonitorsStackDepthInfo.monitor[] minfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                minfo = JDWP.ThreadReference.OwnedMonitorsStackDepthInfo.process(vm, this).owned;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
45714
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 28107
diff changeset
   520
                snapshot.ownedMonitorsInfo = new ArrayList<>(minfo.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                for (int i=0; i < minfo.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                    MonitorInfo mon = new MonitorInfoImpl(vm, minfo[i].monitor, this, minfo[i].stack_depth);
1326
0c330a451ed9 6751643: ThreadReference.ownedMonitors() can return null
jjh
parents: 836
diff changeset
   524
                    snapshot.ownedMonitorsInfo.add(mon);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
11277
e3a1c90dd439 7117053: Fix build warnings in com/sun/tools/jdi/*
jjh
parents: 5506
diff changeset
   527
                if ((vm.traceFlags & VirtualMachine.TRACE_OBJREFS) != 0) {
836
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
   528
                    vm.printTrace(description() +
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
   529
                                  " temporarily caching owned monitors"+
1326
0c330a451ed9 6751643: ThreadReference.ownedMonitors() can return null
jjh
parents: 836
diff changeset
   530
                                  " (count = " + snapshot.ownedMonitorsInfo.size() + ")");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        } catch (JDWPException exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            switch (exc.errorCode()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            case JDWP.Error.THREAD_NOT_SUSPENDED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            case JDWP.Error.INVALID_THREAD:   /* zombie */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                throw new IncompatibleThreadStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                throw exc.toJDIException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        }
1326
0c330a451ed9 6751643: ThreadReference.ownedMonitors() can return null
jjh
parents: 836
diff changeset
   543
        return snapshot.ownedMonitorsInfo;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    public void popFrames(StackFrame frame) throws IncompatibleThreadStateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        // Note that interface-wise this functionality belongs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        // here in ThreadReference, but implementation-wise it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        // belongs in StackFrame, so we just forward it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        if (!frame.thread().equals(this)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            throw new IllegalArgumentException("frame does not belong to this thread");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        if (!vm.canPopFrames()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
            throw new UnsupportedOperationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                "target does not support popping frames");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        ((StackFrameImpl)frame).pop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
45714
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 28107
diff changeset
   560
    public void forceEarlyReturn(Value returnValue) throws InvalidTypeException,
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 28107
diff changeset
   561
                                                           ClassNotLoadedException,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                                             IncompatibleThreadStateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        if (!vm.canForceEarlyReturn()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            throw new UnsupportedOperationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                "target does not support the forcing of a method to return early");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        validateMirrorOrNull(returnValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        StackFrameImpl sf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
           sf = (StackFrameImpl)frame(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        } catch (IndexOutOfBoundsException exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
           throw new InvalidStackFrameException("No more frames on the stack");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        sf.validateStackFrame();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        MethodImpl meth = (MethodImpl)sf.location().method();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        ValueImpl convertedValue  = ValueImpl.prepareForAssignment(returnValue,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                                                                   meth.getReturnValueContainer());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
            JDWP.ThreadReference.ForceEarlyReturn.process(vm, this, convertedValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        } catch (JDWPException exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
            switch (exc.errorCode()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            case JDWP.Error.OPAQUE_FRAME:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                throw new NativeMethodException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            case JDWP.Error.THREAD_NOT_SUSPENDED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                throw new IncompatibleThreadStateException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                         "Thread not suspended");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            case JDWP.Error.THREAD_NOT_ALIVE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                throw new IncompatibleThreadStateException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                                     "Thread has not started or has finished");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            case JDWP.Error.NO_MORE_FRAMES:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                throw new InvalidStackFrameException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                         "No more frames on the stack");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                throw exc.toJDIException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        return "instance of " + referenceType().name() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
               "(name='" + name() + "', " + "id=" + uniqueID() + ")";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    byte typeValueKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        return JDWP.Tag.THREAD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    void addListener(ThreadListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        synchronized (vm.state()) {
45714
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 28107
diff changeset
   613
            listeners.add(new WeakReference<>(listener));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    void removeListener(ThreadListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        synchronized (vm.state()) {
11277
e3a1c90dd439 7117053: Fix build warnings in com/sun/tools/jdi/*
jjh
parents: 5506
diff changeset
   619
            Iterator<WeakReference<ThreadListener>> iter = listeners.iterator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            while (iter.hasNext()) {
11277
e3a1c90dd439 7117053: Fix build warnings in com/sun/tools/jdi/*
jjh
parents: 5506
diff changeset
   621
                WeakReference<ThreadListener> ref = iter.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                if (listener.equals(ref.get())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                    iter.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
    /**
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 25859
diff changeset
   631
     * Propagate the thread state change information
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * to registered listeners.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * Must be entered while synchronized on vm.state()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    private void processThreadAction(ThreadAction action) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        synchronized (vm.state()) {
11277
e3a1c90dd439 7117053: Fix build warnings in com/sun/tools/jdi/*
jjh
parents: 5506
diff changeset
   637
            Iterator<WeakReference<ThreadListener>> iter = listeners.iterator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            while (iter.hasNext()) {
11277
e3a1c90dd439 7117053: Fix build warnings in com/sun/tools/jdi/*
jjh
parents: 5506
diff changeset
   639
                WeakReference<ThreadListener> ref = iter.next();
e3a1c90dd439 7117053: Fix build warnings in com/sun/tools/jdi/*
jjh
parents: 5506
diff changeset
   640
                ThreadListener listener = ref.get();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                if (listener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                    switch (action.id()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                        case ThreadAction.THREAD_RESUMABLE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                            if (!listener.threadResumable(action)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                                iter.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                    // Listener is unreachable; clean up
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                    iter.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            }
836
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
   654
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
   655
            // Discard our local cache
d81b1f62fb82 6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents: 2
diff changeset
   656
            resetLocalCache();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
}