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 
  33 public class SystemDictionary {
  34   private static AddressField dictionaryField;
  35   private static AddressField sharedDictionaryField;
  36   private static AddressField placeholdersField;
  37   private static AddressField loaderConstraintTableField;
  38   private static sun.jvm.hotspot.types.OopField javaSystemLoaderField;
  39   private static int nofBuckets;
  40 
  41   private static sun.jvm.hotspot.types.OopField objectKlassField;
  42   private static sun.jvm.hotspot.types.OopField classLoaderKlassField;
  43   private static sun.jvm.hotspot.types.OopField stringKlassField;
  44   private static sun.jvm.hotspot.types.OopField systemKlassField;
  45   private static sun.jvm.hotspot.types.OopField threadKlassField;
  46   private static sun.jvm.hotspot.types.OopField threadGroupKlassField;
  47 
  48   static {
  49     VM.registerVMInitializedObserver(new Observer() {
  50         public void update(Observable o, Object data) {
  51           initialize(VM.getVM().getTypeDataBase());
  52         }
  53       });
  54   }
  55 
  56   private static synchronized void initialize(TypeDataBase db) {
  57     Type type = db.lookupType("SystemDictionary");
  58     
  59     dictionaryField = type.getAddressField("_dictionary");
  60     sharedDictionaryField = type.getAddressField("_shared_dictionary");
  61     placeholdersField = type.getAddressField("_placeholders");
  62     loaderConstraintTableField = type.getAddressField("_loader_constraints");
  63     javaSystemLoaderField = type.getOopField("_java_system_loader");
  64     nofBuckets = db.lookupIntConstant("SystemDictionary::_nof_buckets").intValue();
  65     
  66     objectKlassField = type.getOopField("_object_klass");
  67     classLoaderKlassField = type.getOopField("_classloader_klass");
  68     stringKlassField = type.getOopField("_string_klass");
  69     systemKlassField = type.getOopField("_system_klass");
  70     threadKlassField = type.getOopField("_thread_klass");
  71     threadGroupKlassField = type.getOopField("_threadGroup_klass");
  72   }
  73 
  74   public Dictionary dictionary() {
  75     Address tmp = dictionaryField.getValue();
  76     return (Dictionary) VMObjectFactory.newObject(Dictionary.class, tmp);
  77   }
  78 
  79   public Dictionary sharedDictionary() {
  80     Address tmp = sharedDictionaryField.getValue();
  81     return (Dictionary) VMObjectFactory.newObject(Dictionary.class, tmp);
  82   }
  83 
  84   public PlaceholderTable placeholders() {
  85     Address tmp = placeholdersField.getValue();
  86     return (PlaceholderTable) VMObjectFactory.newObject(PlaceholderTable.class, tmp);
  87   }
  88     
  89   public LoaderConstraintTable constraints() {
  90     Address tmp = placeholdersField.getValue();
  91     return (LoaderConstraintTable) VMObjectFactory.newObject(LoaderConstraintTable.class, tmp);
  92   }
  93 
  94   // few well known classes -- not all are added here.
  95   // add more if needed.
  96   public static InstanceKlass getThreadKlass() {
  97     return (InstanceKlass) newOop(threadKlassField.getValue());
  98   }
  99 
 100   public static InstanceKlass getThreadGroupKlass() {
 101     return (InstanceKlass) newOop(threadGroupKlassField.getValue());
 102   }
 103 
 104   public static InstanceKlass getObjectKlass() {
 105     return (InstanceKlass) newOop(objectKlassField.getValue());
 106   }
 107 
 108   public static InstanceKlass getStringKlass() {
 109     return (InstanceKlass) newOop(stringKlassField.getValue());
 110   }
 111 
 112   public static InstanceKlass getClassLoaderKlass() {
 113     return (InstanceKlass) newOop(classLoaderKlassField.getValue());
 114   }
 115 
 116   public static InstanceKlass getSystemKlass() {
 117     return (InstanceKlass) newOop(systemKlassField.getValue());
 118   }
 119 
 120   public InstanceKlass getAbstractOwnableSynchronizerKlass() {
 121     return (InstanceKlass) find("java/util/concurrent/locks/AbstractOwnableSynchronizer",
 122                                 null, null);
 123   }
 124  
 125   public static Oop javaSystemLoader() {
 126     return newOop(javaSystemLoaderField.getValue());
 127   }
 128 
 129   public static int getNumOfBuckets() {
 130     return nofBuckets;
 131   }
 132 
 133   private static Oop newOop(OopHandle handle) {
 134     return VM.getVM().getObjectHeap().newOop(handle);
 135   }
 136 
 137   /** Lookup an already loaded class. If not found null is returned. */
 138   public Klass find(String className, Oop classLoader, Oop protectionDomain) {
 139     Symbol sym = VM.getVM().getSymbolTable().probe(className);
 140     if (sym == null) return null;
 141     return find(sym, classLoader, protectionDomain);
 142   }
 143 
 144   /** Lookup an already loaded class. If not found null is returned. */
 145   public Klass find(Symbol className, Oop classLoader, Oop protectionDomain) {
 146     Dictionary dict = dictionary();
 147     long hash = dict.computeHash(className, classLoader);
 148     int index = dict.hashToIndex(hash);
 149     return dict.find(index, hash, className, classLoader, protectionDomain);
 150   }
 151 
 152   /** Interface for iterating through all classes in dictionary */
 153   public static interface ClassVisitor {
 154     public void visit(Klass k);
 155   }
 156 
 157   /** Interface for iterating through all classes and their class
 158       loaders in dictionary */
 159   public static interface ClassAndLoaderVisitor {
 160     public void visit(Klass k, Oop loader);
 161   }
 162 
 163   /** Iterate over all klasses - including object, primitive
 164       array klasses */
 165   public void allClassesDo(final ClassVisitor v) {
 166     ClassVisitor visitor = new ClassVisitor() {
 167       public void visit(Klass k) {
 168         for (Klass l = k; l != null; l = l.arrayKlassOrNull()) {
 169           v.visit(l);
 170         }
 171       }
 172     };
 173     classesDo(visitor);
 174     VM.getVM().getUniverse().basicTypeClassesDo(visitor);
 175   }
 176 
 177   /** Iterate over all klasses in dictionary; just the classes from
 178       declaring class loaders */
 179   public void classesDo(ClassVisitor v) {
 180     dictionary().classesDo(v);
 181   }
 182 
 183   /** All classes, and their class loaders */
 184   public void classesDo(ClassAndLoaderVisitor v) {
 185     dictionary().classesDo(v);
 186   }
 187 
 188   /** All array classes of primitive type, and their class loaders */
 189   public void primArrayClassesDo(ClassAndLoaderVisitor v) {
 190     placeholders().primArrayClassesDo(v);
 191   }
 192 }