hotspot/agent/src/share/classes/sun/jvm/hotspot/oops/OopUtilities.java
author coleenp
Sun, 13 Apr 2008 17:43:42 -0400
changeset 360 21d113ecbf6a
parent 1 489c9b5090e2
child 606 7efb18e28f6c
permissions -rw-r--r--
6420645: Create a vm that uses compressed oops for up to 32gb heapsizes Summary: Compressed oops in instances, arrays, and headers. Code contributors are coleenp, phh, never, swamyv Reviewed-by: jmasa, kamg, acorn, tbell, kvn, rasbold
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
489c9b5090e2 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2000-2005 Sun Microsystems, Inc.  All Rights Reserved.
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.oops;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
import java.util.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
import sun.jvm.hotspot.debugger.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
import sun.jvm.hotspot.memory.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
import sun.jvm.hotspot.runtime.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
import sun.jvm.hotspot.types.TypeDataBase;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
import sun.jvm.hotspot.utilities.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
import sun.jvm.hotspot.jdi.JVMTIThreadState;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
/** A utility class encapsulating useful oop operations */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
public class OopUtilities implements /* imports */ JVMTIThreadState {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
  // FIXME: access should be synchronized and cleared when VM is
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
  // resumed
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
  // String fields
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
  private static IntField offsetField;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
  private static IntField countField;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
  private static OopField valueField;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
  // ThreadGroup fields
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
  private static OopField threadGroupParentField;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
  private static OopField threadGroupNameField;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
  private static IntField threadGroupNThreadsField;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
  private static OopField threadGroupThreadsField;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
  private static IntField threadGroupNGroupsField;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
  private static OopField threadGroupGroupsField;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
  // Thread fields
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
  private static OopField threadNameField;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
  private static OopField threadGroupField;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
  private static LongField threadEETopField;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
  // threadStatus field is new since 1.5
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
  private static IntField threadStatusField;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
  // parkBlocker field is new since 1.6
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
  private static OopField threadParkBlockerField;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
  // possible values of java_lang_Thread::ThreadStatus
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
  private static int THREAD_STATUS_NEW;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
  /*
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
    Other enum constants are not needed as of now. Uncomment these as and when needed.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
    private static int THREAD_STATUS_RUNNABLE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
    private static int THREAD_STATUS_SLEEPING;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
    private static int THREAD_STATUS_IN_OBJECT_WAIT;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
    private static int THREAD_STATUS_IN_OBJECT_WAIT_TIMED;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
    private static int THREAD_STATUS_PARKED;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
    private static int THREAD_STATUS_PARKED_TIMED;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
    private static int THREAD_STATUS_BLOCKED_ON_MONITOR_ENTER;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
    private static int THREAD_STATUS_TERMINATED;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
  */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
  // java.lang.Class fields
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
  private static OopField hcKlassField;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
  // java.util.concurrent.locks.AbstractOwnableSynchronizer fields
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
  private static OopField absOwnSyncOwnerThreadField;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
  static {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
    VM.registerVMInitializedObserver(new Observer() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
        public void update(Observable o, Object data) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
          initialize(VM.getVM().getTypeDataBase());
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
      });
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
  private static synchronized void initialize(TypeDataBase db) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
    // FIXME: don't need this observer; however, do need a VM resumed
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
    // and suspended observer to refetch fields
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
  public static String charArrayToString(TypeArray charArray) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
    if (charArray == null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
      return null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
    return charArrayToString(charArray, 0, (int) charArray.getLength());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
  public static String charArrayToString(TypeArray charArray, int offset, int length) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
    if (charArray == null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
      return null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
    final int limit = offset + length;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
    if (Assert.ASSERTS_ENABLED) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
      Assert.that(offset >= 0 && limit <= charArray.getLength(), "out of bounds");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
    StringBuffer buf = new StringBuffer(length);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
    for (int i = offset; i < limit; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
      buf.append(charArray.getCharAt(i));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
    return buf.toString();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
  public static String stringOopToString(Oop stringOop) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
    if (offsetField == null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
      InstanceKlass k = (InstanceKlass) stringOop.getKlass();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
      offsetField = (IntField) k.findField("offset", "I");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
      countField  = (IntField) k.findField("count",  "I");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
      valueField  = (OopField) k.findField("value",  "[C");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
      if (Assert.ASSERTS_ENABLED) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
        Assert.that(offsetField != null &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
                    countField != null &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
                    valueField != null, "must find all java.lang.String fields");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
    return charArrayToString((TypeArray) valueField.getValue(stringOop),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
                             offsetField.getValue(stringOop),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
                             countField.getValue(stringOop));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
  private static void initThreadGroupFields() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
    if (threadGroupParentField == null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
      SystemDictionary sysDict = VM.getVM().getSystemDictionary();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
      InstanceKlass k = sysDict.getThreadGroupKlass();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
      threadGroupParentField   = (OopField) k.findField("parent",   "Ljava/lang/ThreadGroup;");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
      threadGroupNameField     = (OopField) k.findField("name",     "Ljava/lang/String;");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
      threadGroupNThreadsField = (IntField) k.findField("nthreads", "I");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
      threadGroupThreadsField  = (OopField) k.findField("threads",  "[Ljava/lang/Thread;");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
      threadGroupNGroupsField  = (IntField) k.findField("ngroups",  "I");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
      threadGroupGroupsField   = (OopField) k.findField("groups",   "[Ljava/lang/ThreadGroup;");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
      if (Assert.ASSERTS_ENABLED) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
        Assert.that(threadGroupParentField   != null &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
                    threadGroupNameField     != null &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
                    threadGroupNThreadsField != null &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
                    threadGroupThreadsField  != null &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
                    threadGroupNGroupsField  != null &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
                    threadGroupGroupsField   != null, "must find all java.lang.ThreadGroup fields");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
  public static Oop threadGroupOopGetParent(Oop threadGroupOop) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
    initThreadGroupFields();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
    return threadGroupParentField.getValue(threadGroupOop);
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 static String threadGroupOopGetName(Oop threadGroupOop) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
    initThreadGroupFields();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
    return stringOopToString(threadGroupNameField.getValue(threadGroupOop));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
  public static Oop[] threadGroupOopGetThreads(Oop threadGroupOop) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
    initThreadGroupFields();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
    int nthreads = threadGroupNThreadsField.getValue(threadGroupOop);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
    Oop[] result = new Oop[nthreads];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
    ObjArray threads = (ObjArray) threadGroupThreadsField.getValue(threadGroupOop);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
    for (int i = 0; i < nthreads; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
      result[i] = threads.getObjAt(i);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
    return result;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
  public static Oop[] threadGroupOopGetGroups(Oop threadGroupOop) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
    initThreadGroupFields();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
    int ngroups = threadGroupNGroupsField.getValue(threadGroupOop);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
    Oop[] result = new Oop[ngroups];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
    ObjArray groups = (ObjArray) threadGroupGroupsField.getValue(threadGroupOop);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
    for (int i = 0; i < ngroups; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
      result[i] = groups.getObjAt(i);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
    return result;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
  private static void initThreadFields() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
    if (threadNameField == null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
      SystemDictionary sysDict = VM.getVM().getSystemDictionary();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
      InstanceKlass k = sysDict.getThreadKlass();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
      threadNameField  = (OopField) k.findField("name", "[C");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
      threadGroupField = (OopField) k.findField("group", "Ljava/lang/ThreadGroup;");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
      threadEETopField = (LongField) k.findField("eetop", "J");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
      threadStatusField = (IntField) k.findField("threadStatus", "I");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
      threadParkBlockerField = (OopField) k.findField("parkBlocker",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
                                     "Ljava/lang/Object;");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
      TypeDataBase db = VM.getVM().getTypeDataBase();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
      THREAD_STATUS_NEW = db.lookupIntConstant("java_lang_Thread::NEW").intValue();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
      /*
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
        Other enum constants are not needed as of now. Uncomment these as and when needed.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
        THREAD_STATUS_RUNNABLE = db.lookupIntConstant("java_lang_Thread::RUNNABLE").intValue();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
        THREAD_STATUS_SLEEPING = db.lookupIntConstant("java_lang_Thread::SLEEPING").intValue();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
        THREAD_STATUS_IN_OBJECT_WAIT = db.lookupIntConstant("java_lang_Thread::IN_OBJECT_WAIT").intValue();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
        THREAD_STATUS_IN_OBJECT_WAIT_TIMED = db.lookupIntConstant("java_lang_Thread::IN_OBJECT_WAIT_TIMED").intValue();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
        THREAD_STATUS_PARKED = db.lookupIntConstant("java_lang_Thread::PARKED").intValue();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
        THREAD_STATUS_PARKED_TIMED = db.lookupIntConstant("java_lang_Thread::PARKED_TIMED").intValue();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
        THREAD_STATUS_BLOCKED_ON_MONITOR_ENTER = db.lookupIntConstant("java_lang_Thread::BLOCKED_ON_MONITOR_ENTER").intValue();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
        THREAD_STATUS_TERMINATED = db.lookupIntConstant("java_lang_Thread::TERMINATED").intValue();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
      */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
489c9b5090e2 Initial load
duke
parents:
diff changeset
   213
      if (Assert.ASSERTS_ENABLED) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
        // it is okay to miss threadStatusField, because this was
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
        // introduced only in 1.5 JDK.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
        Assert.that(threadNameField   != null &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
                    threadGroupField  != null &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
                    threadEETopField  != null, "must find all java.lang.Thread fields");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   220
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   221
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   222
489c9b5090e2 Initial load
duke
parents:
diff changeset
   223
  public static Oop threadOopGetThreadGroup(Oop threadOop) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
    initThreadFields();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
    return threadGroupField.getValue(threadOop);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
489c9b5090e2 Initial load
duke
parents:
diff changeset
   228
  public static String threadOopGetName(Oop threadOop) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   229
    initThreadFields();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
    return charArrayToString((TypeArray) threadNameField.getValue(threadOop));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   232
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
  /** May return null if, e.g., thread was not started */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   234
  public static JavaThread threadOopGetJavaThread(Oop threadOop) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
    initThreadFields();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   236
    Address addr = threadOop.getHandle().getAddressAt(threadEETopField.getOffset());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   237
    if (addr == null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   238
      return null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   239
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   240
    return VM.getVM().getThreads().createJavaThreadWrapper(addr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   241
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   242
489c9b5090e2 Initial load
duke
parents:
diff changeset
   243
  /** returns value of java.lang.Thread.threadStatus field */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   244
  public static int threadOopGetThreadStatus(Oop threadOop) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   245
    initThreadFields();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   246
    // The threadStatus is only present starting in 1.5
489c9b5090e2 Initial load
duke
parents:
diff changeset
   247
    if (threadStatusField != null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   248
      return (int) threadStatusField.getValue(threadOop);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   249
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   250
      // All we can easily figure out is if it is alive, but that is
489c9b5090e2 Initial load
duke
parents:
diff changeset
   251
      // enough info for a valid unknown status.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   252
      JavaThread thr = threadOopGetJavaThread(threadOop);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   253
      if (thr == null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
        // the thread hasn't run yet or is in the process of exiting
489c9b5090e2 Initial load
duke
parents:
diff changeset
   255
        return THREAD_STATUS_NEW;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   256
      } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   257
        return JVMTI_THREAD_STATE_ALIVE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   258
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   259
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   260
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   261
489c9b5090e2 Initial load
duke
parents:
diff changeset
   262
  /** returns value of java.lang.Thread.parkBlocker field */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   263
  public static Oop threadOopGetParkBlocker(Oop threadOop) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   264
    initThreadFields();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   265
    if (threadParkBlockerField != null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   266
      return threadParkBlockerField.getValue(threadOop);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   267
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   268
    return null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   269
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   270
489c9b5090e2 Initial load
duke
parents:
diff changeset
   271
  // initialize fields for java.lang.Class
489c9b5090e2 Initial load
duke
parents:
diff changeset
   272
  private static void initClassFields() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   273
    if (hcKlassField == null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   274
       // hc_klass is a HotSpot magic field and hence we can't
489c9b5090e2 Initial load
duke
parents:
diff changeset
   275
       // find it from InstanceKlass for java.lang.Class.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   276
       TypeDataBase db = VM.getVM().getTypeDataBase();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   277
       int hcKlassOffset = (int) Oop.getHeaderSize();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   278
       try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   279
          hcKlassOffset += (db.lookupIntConstant("java_lang_Class::hc_klass_offset").intValue() *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   280
                           db.getAddressSize());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   281
       } catch (RuntimeException re) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   282
          // ignore, currently java_lang_Class::hc_klass_offset is zero
489c9b5090e2 Initial load
duke
parents:
diff changeset
   283
       }
360
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   284
       if (VM.getVM().isCompressedOopsEnabled()) {
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   285
         hcKlassField = new NarrowOopField(new NamedFieldIdentifier("hc_klass"), hcKlassOffset, true);
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   286
       } else {
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   287
         hcKlassField = new OopField(new NamedFieldIdentifier("hc_klass"), hcKlassOffset, true);
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   288
       }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   289
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   290
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   291
489c9b5090e2 Initial load
duke
parents:
diff changeset
   292
  /** get klassOop field at offset hc_klass_offset from a java.lang.Class object */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   293
  public static Klass classOopToKlass(Oop aClass) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   294
    initClassFields();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   295
    return (Klass) hcKlassField.getValue(aClass);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   296
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   297
489c9b5090e2 Initial load
duke
parents:
diff changeset
   298
  // initialize fields for j.u.c.l AbstractOwnableSynchornizer class
489c9b5090e2 Initial load
duke
parents:
diff changeset
   299
  private static void initAbsOwnSyncFields() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   300
    if (absOwnSyncOwnerThreadField == null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   301
       SystemDictionary sysDict = VM.getVM().getSystemDictionary();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   302
       InstanceKlass k = sysDict.getAbstractOwnableSynchronizerKlass();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   303
       absOwnSyncOwnerThreadField =
489c9b5090e2 Initial load
duke
parents:
diff changeset
   304
           (OopField) k.findField("exclusiveOwnerThread",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   305
                                  "Ljava/lang/Thread;");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   306
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   307
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   308
489c9b5090e2 Initial load
duke
parents:
diff changeset
   309
  // return exclusiveOwnerThread field of AbstractOwnableSynchronizer class
489c9b5090e2 Initial load
duke
parents:
diff changeset
   310
  public static Oop abstractOwnableSynchronizerGetOwnerThread(Oop oop) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   311
    initAbsOwnSyncFields();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   312
    if (absOwnSyncOwnerThreadField == null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   313
      return null; // pre-1.6 VM?
489c9b5090e2 Initial load
duke
parents:
diff changeset
   314
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   315
      return absOwnSyncOwnerThreadField.getValue(oop);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   316
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   317
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   318
}