1 /*
   2  * Copyright 2001-2008 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(WK_KLASS("object_klass"));
  67     classLoaderKlassField = type.getOopField(WK_KLASS("classloader_klass"));
  68     stringKlassField = type.getOopField(WK_KLASS("string_klass"));
  69     systemKlassField = type.getOopField(WK_KLASS("system_klass"));
  70     threadKlassField = type.getOopField(WK_KLASS("thread_klass"));
  71     threadGroupKlassField = type.getOopField(WK_KLASS("threadGroup_klass"));
  72   }
  73 
  74   // This WK functions must follow the definitions in systemDictionary.hpp:
  75   private static String WK_KLASS(String name) {
  76       //#define WK_KLASS(name) _well_known_klasses[SystemDictionary::WK_KLASS_ENUM_NAME(name)]
  77       return ("_well_known_klasses[SystemDictionary::"+WK_KLASS_ENUM_NAME(name)+"]");
  78   }
  79   private static String WK_KLASS_ENUM_NAME(String kname) {
  80       //#define WK_KLASS_ENUM_NAME(kname)    kname##_knum
  81       return (kname+"_knum");
  82   }
  83 
  84   public Dictionary dictionary() {
  85     Address tmp = dictionaryField.getValue();
  86     return (Dictionary) VMObjectFactory.newObject(Dictionary.class, tmp);
  87   }
  88 
  89   public Dictionary sharedDictionary() {
  90     Address tmp = sharedDictionaryField.getValue();
  91     return (Dictionary) VMObjectFactory.newObject(Dictionary.class, tmp);
  92   }
  93 
  94   public PlaceholderTable placeholders() {
  95     Address tmp = placeholdersField.getValue();
  96     return (PlaceholderTable) VMObjectFactory.newObject(PlaceholderTable.class, tmp);
  97   }
  98 
  99   public LoaderConstraintTable constraints() {
 100     Address tmp = placeholdersField.getValue();
 101     return (LoaderConstraintTable) VMObjectFactory.newObject(LoaderConstraintTable.class, tmp);
 102   }
 103 
 104   // few well known classes -- not all are added here.
 105   // add more if needed.
 106   public static InstanceKlass getThreadKlass() {
 107     return (InstanceKlass) newOop(threadKlassField.getValue());
 108   }
 109 
 110   public static InstanceKlass getThreadGroupKlass() {
 111     return (InstanceKlass) newOop(threadGroupKlassField.getValue());
 112   }
 113 
 114   public static InstanceKlass getObjectKlass() {
 115     return (InstanceKlass) newOop(objectKlassField.getValue());
 116   }
 117 
 118   public static InstanceKlass getStringKlass() {
 119     return (InstanceKlass) newOop(stringKlassField.getValue());
 120   }
 121 
 122   public static InstanceKlass getClassLoaderKlass() {
 123     return (InstanceKlass) newOop(classLoaderKlassField.getValue());
 124   }
 125 
 126   public static InstanceKlass getSystemKlass() {
 127     return (InstanceKlass) newOop(systemKlassField.getValue());
 128   }
 129 
 130   public InstanceKlass getAbstractOwnableSynchronizerKlass() {
 131     return (InstanceKlass) find("java/util/concurrent/locks/AbstractOwnableSynchronizer",
 132                                 null, null);
 133   }
 134 
 135   public static Oop javaSystemLoader() {
 136     return newOop(javaSystemLoaderField.getValue());
 137   }
 138 
 139   public static int getNumOfBuckets() {
 140     return nofBuckets;
 141   }
 142 
 143   private static Oop newOop(OopHandle handle) {
 144     return VM.getVM().getObjectHeap().newOop(handle);
 145   }
 146 
 147   /** Lookup an already loaded class. If not found null is returned. */
 148   public Klass find(String className, Oop classLoader, Oop protectionDomain) {
 149     Symbol sym = VM.getVM().getSymbolTable().probe(className);
 150     if (sym == null) return null;
 151     return find(sym, classLoader, protectionDomain);
 152   }
 153 
 154   /** Lookup an already loaded class. If not found null is returned. */
 155   public Klass find(Symbol className, Oop classLoader, Oop protectionDomain) {
 156     Dictionary dict = dictionary();
 157     long hash = dict.computeHash(className, classLoader);
 158     int index = dict.hashToIndex(hash);
 159     return dict.find(index, hash, className, classLoader, protectionDomain);
 160   }
 161 
 162   /** Interface for iterating through all classes in dictionary */
 163   public static interface ClassVisitor {
 164     public void visit(Klass k);
 165   }
 166 
 167   /** Interface for iterating through all classes and their class
 168       loaders in dictionary */
 169   public static interface ClassAndLoaderVisitor {
 170     public void visit(Klass k, Oop loader);
 171   }
 172 
 173   /** Iterate over all klasses - including object, primitive
 174       array klasses */
 175   public void allClassesDo(final ClassVisitor v) {
 176     ClassVisitor visitor = new ClassVisitor() {
 177       public void visit(Klass k) {
 178         for (Klass l = k; l != null; l = l.arrayKlassOrNull()) {
 179           v.visit(l);
 180         }
 181       }
 182     };
 183     classesDo(visitor);
 184     VM.getVM().getUniverse().basicTypeClassesDo(visitor);
 185   }
 186 
 187   /** Iterate over all klasses in dictionary; just the classes from
 188       declaring class loaders */
 189   public void classesDo(ClassVisitor v) {
 190     dictionary().classesDo(v);
 191   }
 192 
 193   /** All classes, and their class loaders */
 194   public void classesDo(ClassAndLoaderVisitor v) {
 195     dictionary().classesDo(v);
 196   }
 197 
 198   /** All array classes of primitive type, and their class loaders */
 199   public void primArrayClassesDo(ClassAndLoaderVisitor v) {
 200     placeholders().primArrayClassesDo(v);
 201   }
 202 }