1 /*
   2  * Copyright 2001-2005 Sun Microsystems, Inc.  All Rights Reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  20  * CA 95054 USA or visit www.sun.com if you need additional information or
  21  * have any questions.
  22  *
  23  */
  24 
  25 package sun.jvm.hotspot.memory;
  26 
  27 import java.util.*;
  28 import sun.jvm.hotspot.debugger.*;
  29 import sun.jvm.hotspot.oops.*;
  30 import sun.jvm.hotspot.runtime.*;
  31 import sun.jvm.hotspot.types.*;
  32 import sun.jvm.hotspot.types.OopField; // resolve ambiguity with oops.OopField
  33 
  34 // following needed for on-the-fly field construction:
  35 import sun.jvm.hotspot.types.basic.BasicOopField;
  36 import sun.jvm.hotspot.types.basic.BasicTypeDataBase;
  37 
  38 public class SystemDictionary {
  39   private static AddressField dictionaryField;
  40   private static AddressField sharedDictionaryField;
  41   private static AddressField placeholdersField;
  42   private static AddressField loaderConstraintTableField;
  43   private static OopField javaSystemLoaderField;
  44   private static int nofBuckets;
  45 
  46   private static OopField wellKnownKlasses;
  47   private static OopField objectKlassField;
  48   private static OopField classLoaderKlassField;
  49   private static OopField stringKlassField;
  50   private static OopField systemKlassField;
  51   private static OopField threadKlassField;
  52   private static OopField threadGroupKlassField;
  53 
  54   static {
  55     VM.registerVMInitializedObserver(new Observer() {
  56         public void update(Observable o, Object data) {
  57           initialize(VM.getVM().getTypeDataBase());
  58         }
  59       });
  60   }
  61 
  62   private static synchronized void initialize(TypeDataBase db) {
  63     Type type = db.lookupType("SystemDictionary");
  64 
  65     dictionaryField = type.getAddressField("_dictionary");
  66     sharedDictionaryField = type.getAddressField("_shared_dictionary");
  67     placeholdersField = type.getAddressField("_placeholders");
  68     loaderConstraintTableField = type.getAddressField("_loader_constraints");
  69     javaSystemLoaderField = type.getOopField("_java_system_loader");
  70     nofBuckets = db.lookupIntConstant("SystemDictionary::_nof_buckets").intValue();
  71 
  72     wellKnownKlasses = type.getOopField("_well_known_klasses[0]");
  73     objectKlassField = findWellKnownKlass("object_klass", type, db);
  74     classLoaderKlassField = findWellKnownKlass("classloader_klass", type, db);
  75     stringKlassField = findWellKnownKlass("string_klass", type, db);
  76     systemKlassField = findWellKnownKlass("system_klass", type, db);
  77     threadKlassField = findWellKnownKlass("thread_klass", type, db);
  78     threadGroupKlassField = findWellKnownKlass("threadGroup_klass", type, db);
  79   }
  80 
  81   private static OopField findWellKnownKlass(String indexName, Type type, TypeDataBase db) {
  82     Address wkk = wellKnownKlasses.getStaticFieldAddress();
  83     int index = db.lookupIntConstant("SystemDictionary::#"+indexName).intValue();
  84     return new BasicOopField((BasicTypeDataBase)db, type, indexName, type,
  85                              true, index * db.getAddressSize(), wkk);
  86   }
  87 
  88   public Dictionary dictionary() {
  89     Address tmp = dictionaryField.getValue();
  90     return (Dictionary) VMObjectFactory.newObject(Dictionary.class, tmp);
  91   }
  92 
  93   public Dictionary sharedDictionary() {
  94     Address tmp = sharedDictionaryField.getValue();
  95     return (Dictionary) VMObjectFactory.newObject(Dictionary.class, tmp);
  96   }
  97 
  98   public PlaceholderTable placeholders() {
  99     Address tmp = placeholdersField.getValue();
 100     return (PlaceholderTable) VMObjectFactory.newObject(PlaceholderTable.class, tmp);
 101   }
 102 
 103   public LoaderConstraintTable constraints() {
 104     Address tmp = placeholdersField.getValue();
 105     return (LoaderConstraintTable) VMObjectFactory.newObject(LoaderConstraintTable.class, tmp);
 106   }
 107 
 108   // few well known classes -- not all are added here.
 109   // add more if needed.
 110   public static InstanceKlass getThreadKlass() {
 111     return (InstanceKlass) newOop(threadKlassField.getValue());
 112   }
 113 
 114   public static InstanceKlass getThreadGroupKlass() {
 115     return (InstanceKlass) newOop(threadGroupKlassField.getValue());
 116   }
 117 
 118   public static InstanceKlass getObjectKlass() {
 119     return (InstanceKlass) newOop(objectKlassField.getValue());
 120   }
 121 
 122   public static InstanceKlass getStringKlass() {
 123     return (InstanceKlass) newOop(stringKlassField.getValue());
 124   }
 125 
 126   public static InstanceKlass getClassLoaderKlass() {
 127     return (InstanceKlass) newOop(classLoaderKlassField.getValue());
 128   }
 129 
 130   public static InstanceKlass getSystemKlass() {
 131     return (InstanceKlass) newOop(systemKlassField.getValue());
 132   }
 133 
 134   public InstanceKlass getAbstractOwnableSynchronizerKlass() {
 135     return (InstanceKlass) find("java/util/concurrent/locks/AbstractOwnableSynchronizer",
 136                                 null, null);
 137   }
 138 
 139   public static Oop javaSystemLoader() {
 140     return newOop(javaSystemLoaderField.getValue());
 141   }
 142 
 143   public static int getNumOfBuckets() {
 144     return nofBuckets;
 145   }
 146 
 147   private static Oop newOop(OopHandle handle) {
 148     return VM.getVM().getObjectHeap().newOop(handle);
 149   }
 150 
 151   /** Lookup an already loaded class. If not found null is returned. */
 152   public Klass find(String className, Oop classLoader, Oop protectionDomain) {
 153     Symbol sym = VM.getVM().getSymbolTable().probe(className);
 154     if (sym == null) return null;
 155     return find(sym, classLoader, protectionDomain);
 156   }
 157 
 158   /** Lookup an already loaded class. If not found null is returned. */
 159   public Klass find(Symbol className, Oop classLoader, Oop protectionDomain) {
 160     Dictionary dict = dictionary();
 161     long hash = dict.computeHash(className, classLoader);
 162     int index = dict.hashToIndex(hash);
 163     return dict.find(index, hash, className, classLoader, protectionDomain);
 164   }
 165 
 166   /** Interface for iterating through all classes in dictionary */
 167   public static interface ClassVisitor {
 168     public void visit(Klass k);
 169   }
 170 
 171   /** Interface for iterating through all classes and their class
 172       loaders in dictionary */
 173   public static interface ClassAndLoaderVisitor {
 174     public void visit(Klass k, Oop loader);
 175   }
 176 
 177   /** Iterate over all klasses - including object, primitive
 178       array klasses */
 179   public void allClassesDo(final ClassVisitor v) {
 180     ClassVisitor visitor = new ClassVisitor() {
 181       public void visit(Klass k) {
 182         for (Klass l = k; l != null; l = l.arrayKlassOrNull()) {
 183           v.visit(l);
 184         }
 185       }
 186     };
 187     classesDo(visitor);
 188     VM.getVM().getUniverse().basicTypeClassesDo(visitor);
 189   }
 190 
 191   /** Iterate over all klasses in dictionary; just the classes from
 192       declaring class loaders */
 193   public void classesDo(ClassVisitor v) {
 194     dictionary().classesDo(v);
 195   }
 196 
 197   /** All classes, and their class loaders */
 198   public void classesDo(ClassAndLoaderVisitor v) {
 199     dictionary().classesDo(v);
 200   }
 201 
 202   /** All array classes of primitive type, and their class loaders */
 203   public void primArrayClassesDo(ClassAndLoaderVisitor v) {
 204     placeholders().primArrayClassesDo(v);
 205   }
 206 }