hotspot/agent/src/share/classes/sun/jvm/hotspot/jdi/ObjectReferenceImpl.java
author xdono
Tue, 28 Jul 2009 12:12:40 -0700
changeset 3261 c7d5aae8d3f7
parent 3171 aa289b22b577
child 5547 f4b087cbb361
permissions -rw-r--r--
6862919: Update copyright year Summary: Update copyright for files that have been modified in 2009, up to 07/09 Reviewed-by: tbell, ohair
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
3261
c7d5aae8d3f7 6862919: Update copyright year
xdono
parents: 3171
diff changeset
     2
 * Copyright 2002-2009 Sun Microsystems, Inc.  All Rights Reserved.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
489c9b5090e2 Initial load
duke
parents:
diff changeset
    25
package sun.jvm.hotspot.jdi;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
import java.io.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
import com.sun.jdi.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
import sun.jvm.hotspot.debugger.Address;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
import sun.jvm.hotspot.debugger.OopHandle;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
import sun.jvm.hotspot.oops.Oop;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
import sun.jvm.hotspot.oops.Mark;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
import sun.jvm.hotspot.oops.Instance;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
import sun.jvm.hotspot.oops.Array;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
import sun.jvm.hotspot.oops.OopUtilities;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
import sun.jvm.hotspot.oops.Klass;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
import sun.jvm.hotspot.oops.DefaultHeapVisitor;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
import sun.jvm.hotspot.runtime.JavaThread;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
import sun.jvm.hotspot.runtime.JavaVFrame;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
import sun.jvm.hotspot.runtime.MonitorInfo;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
import sun.jvm.hotspot.runtime.ObjectMonitor;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
import sun.jvm.hotspot.runtime.Threads;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
import sun.jvm.hotspot.utilities.Assert;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
import java.util.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
public class ObjectReferenceImpl extends ValueImpl implements ObjectReference {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
    private Oop  saObject;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
    private long myID;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
    private boolean monitorInfoCached = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
    private ThreadReferenceImpl owningThread = null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
    private List waitingThreads = null; // List<ThreadReferenceImpl>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
    private int entryCount = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
    private static long nextID = 0L;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
    private static synchronized long nextID() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
        return nextID++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
    ObjectReferenceImpl(VirtualMachine aVm, sun.jvm.hotspot.oops.Oop oRef) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
        super(aVm);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
        saObject = oRef;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
        myID = nextID();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
    protected Oop ref() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
        return saObject;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
    public Type type() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
        return referenceType();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
    public ReferenceType referenceType() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
        Klass myKlass = ref().getKlass();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
        return vm.referenceType(myKlass);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
    public Value getValue(Field sig) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
        List list = new ArrayList(1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
        list.add(sig);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
        Map map = getValues(list);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
        return(Value)map.get(sig);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
    public Map getValues(List theFields) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
        //validateMirrors(theFields);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
        List staticFields = new ArrayList(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
        int size = theFields.size();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
        List instanceFields = new ArrayList(size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
        for (int i=0; i<size; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
            sun.jvm.hotspot.jdi.FieldImpl field =
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
                (sun.jvm.hotspot.jdi.FieldImpl)theFields.get(i);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
            // Make sure the field is valid
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
            ((ReferenceTypeImpl)referenceType()).validateFieldAccess(field);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
            // FIX ME! We need to do some sanity checking
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
            // here; make sure the field belongs to this
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
            // object.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
            if (field.isStatic()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
                staticFields.add(field);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
            } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
                instanceFields.add(field);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
        // Look up static field(s) first to mimic the JDI implementation
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
        Map map;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
        if (staticFields.size() > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
            map = referenceType().getValues(staticFields);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
        } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
            map = new HashMap(size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
        // Then get instance field(s)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
        size = instanceFields.size();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
        for (int ii=0; ii<size; ii++){
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
            FieldImpl fieldImpl = (FieldImpl)instanceFields.get(ii);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
            map.put(fieldImpl, fieldImpl.getValue(saObject));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
        return map;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
    public void setValue(Field field, Value value)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
                   throws InvalidTypeException, ClassNotLoadedException {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
        vm.throwNotReadOnlyException("ObjectReference.setValue(...)");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
    public Value invokeMethod(ThreadReference threadIntf, Method methodIntf,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
                              List arguments, int options)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
                              throws InvalidTypeException,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
                                     IncompatibleThreadStateException,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
                                     InvocationException,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
                                     ClassNotLoadedException {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
        vm.throwNotReadOnlyException("ObjectReference.invokeMethod(...)");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
        return null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
    public void disableCollection() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
        vm.throwNotReadOnlyException("ObjectReference.disableCollection()");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
    public void enableCollection() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
        vm.throwNotReadOnlyException("ObjectReference.enableCollection()");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
    public boolean isCollected() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
        vm.throwNotReadOnlyException("ObjectReference.isCollected()");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
        return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
    public long uniqueID() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
        return myID;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
    public List waitingThreads() throws IncompatibleThreadStateException {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
        if (vm.canGetMonitorInfo() == false) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
            throw new UnsupportedOperationException();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
        if (! monitorInfoCached) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
            computeMonitorInfo();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
        return waitingThreads;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
    public ThreadReference owningThread() throws IncompatibleThreadStateException {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
        if (vm.canGetMonitorInfo() == false) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
            throw new UnsupportedOperationException();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
        if (! monitorInfoCached) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
            computeMonitorInfo();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
        return owningThread;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
    public int entryCount() throws IncompatibleThreadStateException {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
        if (vm.canGetMonitorInfo() == false) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
            throw new UnsupportedOperationException();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
        if (! monitorInfoCached) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
            computeMonitorInfo();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
        return entryCount;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
    // new method since 1.6.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
    // Real body will be supplied later.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
    public List referringObjects(long maxReferrers) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
        if (!vm.canGetInstanceInfo()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
            throw new UnsupportedOperationException(
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
                      "target does not support getting instances");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
        if (maxReferrers < 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
            throw new IllegalArgumentException("maxReferrers is less than zero: "
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
                                              + maxReferrers);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
        final ObjectReference obj = this;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
        final List objects = new ArrayList(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
        final long max = maxReferrers;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
                vm.saObjectHeap().iterate(new DefaultHeapVisitor() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
                private long refCount = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
                public boolean doObj(Oop oop) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   213
                                        try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
                                                ObjectReference objref = vm.objectMirror(oop);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
                                                List fields = objref.referenceType().allFields();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
                                                for (int i=0; i < fields.size(); i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
                                                        Field fld = (Field)fields.get(i);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
                                                        if (objref.getValue(fld).equals(obj) && !objects.contains(objref)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
                                                                objects.add(objref);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   220
                                                                refCount++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   221
                                                        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   222
                                                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   223
                                                if (max > 0 && refCount >= max) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
                                                        return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
                                                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
                                        } catch  (RuntimeException x) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
                                          // Ignore RuntimeException thrown from vm.objectMirror(oop)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   228
                                          // for bad oop. It is possible to see some bad oop
489c9b5090e2 Initial load
duke
parents:
diff changeset
   229
                                          // because heap might be iterating at no safepoint.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
                                        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
                                        return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   232
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   234
            });
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
        return objects;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   236
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   237
489c9b5090e2 Initial load
duke
parents:
diff changeset
   238
    // refer to JvmtiEnvBase::count_locked_objects.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   239
    // Count the number of objects for a lightweight monitor. The obj
489c9b5090e2 Initial load
duke
parents:
diff changeset
   240
    // parameter is object that owns the monitor so this routine will
489c9b5090e2 Initial load
duke
parents:
diff changeset
   241
    // count the number of times the same object was locked by frames
489c9b5090e2 Initial load
duke
parents:
diff changeset
   242
    // in JavaThread. i.e., we count total number of times the same
489c9b5090e2 Initial load
duke
parents:
diff changeset
   243
    // object is (lightweight) locked by given thread.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   244
    private int countLockedObjects(JavaThread jt, Oop obj) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   245
        int res = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   246
        JavaVFrame frame = jt.getLastJavaVFrameDbg();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   247
        while (frame != null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   248
            List monitors = frame.getMonitors();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   249
            OopHandle givenHandle = obj.getHandle();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   250
            for (Iterator itr = monitors.iterator(); itr.hasNext();) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   251
                MonitorInfo mi = (MonitorInfo) itr.next();
3171
aa289b22b577 6837472: com/sun/jdi/MonitorFrameInfo.java fails with AggressiveOpts in 6u14
kvn
parents: 1
diff changeset
   252
                if (mi.eliminated() && frame.isCompiledFrame()) continue; // skip eliminated monitor
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   253
                if (givenHandle.equals(mi.owner())) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
                    res++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   255
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   256
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   257
            frame = (JavaVFrame) frame.javaSender();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   258
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   259
        return res;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   260
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   261
489c9b5090e2 Initial load
duke
parents:
diff changeset
   262
    // wrappers on same named method of Threads class
489c9b5090e2 Initial load
duke
parents:
diff changeset
   263
    // returns List<JavaThread>
489c9b5090e2 Initial load
duke
parents:
diff changeset
   264
    private List getPendingThreads(ObjectMonitor mon) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   265
        return vm.saVM().getThreads().getPendingThreads(mon);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   266
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   267
489c9b5090e2 Initial load
duke
parents:
diff changeset
   268
    // returns List<JavaThread>
489c9b5090e2 Initial load
duke
parents:
diff changeset
   269
    private List getWaitingThreads(ObjectMonitor mon) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   270
        return vm.saVM().getThreads().getWaitingThreads(mon);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   271
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   272
489c9b5090e2 Initial load
duke
parents:
diff changeset
   273
    private JavaThread owningThreadFromMonitor(Address addr) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   274
        return vm.saVM().getThreads().owningThreadFromMonitor(addr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   275
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   276
489c9b5090e2 Initial load
duke
parents:
diff changeset
   277
    // refer to JvmtiEnv::GetObjectMonitorUsage
489c9b5090e2 Initial load
duke
parents:
diff changeset
   278
    private void computeMonitorInfo() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   279
        monitorInfoCached = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   280
        Mark mark = saObject.getMark();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   281
        ObjectMonitor mon = null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   282
        Address owner = null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   283
        // check for heavyweight monitor
489c9b5090e2 Initial load
duke
parents:
diff changeset
   284
        if (! mark.hasMonitor()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   285
            // check for lightweight monitor
489c9b5090e2 Initial load
duke
parents:
diff changeset
   286
            if (mark.hasLocker()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   287
                owner = mark.locker().getAddress(); // save the address of the Lock word
489c9b5090e2 Initial load
duke
parents:
diff changeset
   288
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   289
            // implied else: no owner
489c9b5090e2 Initial load
duke
parents:
diff changeset
   290
        } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   291
            // this object has a heavyweight monitor
489c9b5090e2 Initial load
duke
parents:
diff changeset
   292
            mon = mark.monitor();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   293
489c9b5090e2 Initial load
duke
parents:
diff changeset
   294
            // The owner field of a heavyweight monitor may be NULL for no
489c9b5090e2 Initial load
duke
parents:
diff changeset
   295
            // owner, a JavaThread * or it may still be the address of the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   296
            // Lock word in a JavaThread's stack. A monitor can be inflated
489c9b5090e2 Initial load
duke
parents:
diff changeset
   297
            // by a non-owning JavaThread, but only the owning JavaThread
489c9b5090e2 Initial load
duke
parents:
diff changeset
   298
            // can change the owner field from the Lock word to the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   299
            // JavaThread * and it may not have done that yet.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   300
            owner = mon.owner();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   301
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   302
489c9b5090e2 Initial load
duke
parents:
diff changeset
   303
        // find the owning thread
489c9b5090e2 Initial load
duke
parents:
diff changeset
   304
        if (owner != null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   305
            owningThread = vm.threadMirror(owningThreadFromMonitor(owner));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   306
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   307
489c9b5090e2 Initial load
duke
parents:
diff changeset
   308
        // compute entryCount
489c9b5090e2 Initial load
duke
parents:
diff changeset
   309
        if (owningThread != null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   310
            if (owningThread.getJavaThread().getAddress().equals(owner)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   311
                // the owner field is the JavaThread *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   312
                if (Assert.ASSERTS_ENABLED) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   313
                    Assert.that(false, "must have heavyweight monitor with JavaThread * owner");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   314
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   315
                entryCount = (int) mark.monitor().recursions() + 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   316
            } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   317
                // The owner field is the Lock word on the JavaThread's stack
489c9b5090e2 Initial load
duke
parents:
diff changeset
   318
                // so the recursions field is not valid. We have to count the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   319
                // number of recursive monitor entries the hard way.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   320
                entryCount = countLockedObjects(owningThread.getJavaThread(), saObject);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   321
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   322
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   323
489c9b5090e2 Initial load
duke
parents:
diff changeset
   324
        // find the contenders & waiters
489c9b5090e2 Initial load
duke
parents:
diff changeset
   325
        waitingThreads = new ArrayList();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   326
        if (mon != null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   327
            // this object has a heavyweight monitor. threads could
489c9b5090e2 Initial load
duke
parents:
diff changeset
   328
            // be contenders or waiters
489c9b5090e2 Initial load
duke
parents:
diff changeset
   329
            // add all contenders
489c9b5090e2 Initial load
duke
parents:
diff changeset
   330
            List pendingThreads = getPendingThreads(mon);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   331
            // convert the JavaThreads to ThreadReferenceImpls
489c9b5090e2 Initial load
duke
parents:
diff changeset
   332
            for (Iterator itrPend = pendingThreads.iterator(); itrPend.hasNext();) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   333
                waitingThreads.add(vm.threadMirror((JavaThread) itrPend.next()));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   334
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   335
489c9b5090e2 Initial load
duke
parents:
diff changeset
   336
            // add all waiters (threads in Object.wait())
489c9b5090e2 Initial load
duke
parents:
diff changeset
   337
            // note that we don't do this JVMTI way. To do it JVMTI way,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   338
            // we would need to access ObjectWaiter list maintained in
489c9b5090e2 Initial load
duke
parents:
diff changeset
   339
            // ObjectMonitor::_queue. But we don't have this struct exposed
489c9b5090e2 Initial load
duke
parents:
diff changeset
   340
            // in vmStructs. We do waiters list in a way similar to getting
489c9b5090e2 Initial load
duke
parents:
diff changeset
   341
            // pending threads list
489c9b5090e2 Initial load
duke
parents:
diff changeset
   342
            List objWaitingThreads = getWaitingThreads(mon);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   343
            // convert the JavaThreads to ThreadReferenceImpls
489c9b5090e2 Initial load
duke
parents:
diff changeset
   344
            for (Iterator itrWait = objWaitingThreads.iterator(); itrWait.hasNext();) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   345
                waitingThreads.add(vm.threadMirror((JavaThread) itrWait.next()));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   346
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   347
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   348
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   349
489c9b5090e2 Initial load
duke
parents:
diff changeset
   350
    public boolean equals(Object obj) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   351
        if ((obj != null) && (obj instanceof ObjectReferenceImpl)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   352
            ObjectReferenceImpl other = (ObjectReferenceImpl)obj;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   353
            return (ref().equals(other.ref())) &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   354
                   super.equals(obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   355
        } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   356
            return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   357
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   358
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   359
489c9b5090e2 Initial load
duke
parents:
diff changeset
   360
    public int hashCode() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   361
        return saObject.hashCode();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   362
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   363
489c9b5090e2 Initial load
duke
parents:
diff changeset
   364
    public String toString() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   365
        return  "instance of " + referenceType().name() + "(id=" + uniqueID() + ")";
489c9b5090e2 Initial load
duke
parents:
diff changeset
   366
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   367
}