1 /*
   2  * Copyright (c) 2000, 2012, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 package sun.jvm.hotspot.code;
  26 
  27 import java.util.*;
  28 import sun.jvm.hotspot.debugger.*;
  29 import sun.jvm.hotspot.memory.*;
  30 import sun.jvm.hotspot.runtime.*;
  31 import sun.jvm.hotspot.types.*;
  32 import sun.jvm.hotspot.utilities.*;
  33 
  34 public class CodeCache {
  35   private static AddressField       heapField;
  36   private static AddressField       scavengeRootNMethodsField;
  37   private static VirtualConstructor virtualConstructor;
  38 
  39   private CodeHeap heap;
  40 
  41   static {
  42     VM.registerVMInitializedObserver(new Observer() {
  43         public void update(Observable o, Object data) {
  44           initialize(VM.getVM().getTypeDataBase());
  45         }
  46       });
  47   }
  48 
  49   private static synchronized void initialize(TypeDataBase db) {
  50     Type type = db.lookupType("CodeCache");
  51 
  52     heapField = type.getAddressField("_heap");
  53     scavengeRootNMethodsField = type.getAddressField("_scavenge_root_nmethods");
  54 
  55     virtualConstructor = new VirtualConstructor(db);
  56     // Add mappings for all possible CodeBlob subclasses
  57     virtualConstructor.addMapping("BufferBlob", BufferBlob.class);
  58     virtualConstructor.addMapping("nmethod", NMethod.class);
  59     virtualConstructor.addMapping("RuntimeStub", RuntimeStub.class);
  60     virtualConstructor.addMapping("AdapterBlob", AdapterBlob.class);
  61     virtualConstructor.addMapping("MethodHandlesAdapterBlob", MethodHandlesAdapterBlob.class);
  62     virtualConstructor.addMapping("SafepointBlob", SafepointBlob.class);
  63     virtualConstructor.addMapping("DeoptimizationBlob", DeoptimizationBlob.class);
  64     if (VM.getVM().isServerCompiler()) {
  65       virtualConstructor.addMapping("ExceptionBlob", ExceptionBlob.class);
  66       virtualConstructor.addMapping("UncommonTrapBlob", UncommonTrapBlob.class);
  67     }
  68   }
  69 
  70   public CodeCache() {
  71     heap = (CodeHeap) VMObjectFactory.newObject(CodeHeap.class, heapField.getValue());
  72   }
  73 
  74   public NMethod scavengeRootMethods() {
  75     return (NMethod) VMObjectFactory.newObject(NMethod.class, scavengeRootNMethodsField.getValue());
  76   }
  77 
  78   public boolean contains(Address p) {
  79     return getHeap().contains(p);
  80   }
  81 
  82   /** When VM.getVM().isDebugging() returns true, this behaves like
  83       findBlobUnsafe */
  84   public CodeBlob findBlob(Address start) {
  85     CodeBlob result = findBlobUnsafe(start);
  86     if (result == null) return null;
  87     if (VM.getVM().isDebugging()) {
  88       return result;
  89     }
  90     // We could potientially look up non_entrant methods
  91     // NOTE: this is effectively a "guarantee", and is slightly different from the one in the VM
  92     if (Assert.ASSERTS_ENABLED) {
  93       Assert.that(!(result.isZombie() || result.isLockedByVM()), "unsafe access to zombie method");
  94     }
  95     return result;
  96   }
  97 
  98   public CodeBlob findBlobUnsafe(Address start) {
  99     CodeBlob result = null;
 100 
 101     try {
 102       result = (CodeBlob) virtualConstructor.instantiateWrapperFor(getHeap().findStart(start));
 103     }
 104     catch (WrongTypeException wte) {
 105       Address cbAddr = null;
 106       try {
 107         cbAddr = getHeap().findStart(start);
 108       }
 109       catch (Exception findEx) {
 110         findEx.printStackTrace();
 111       }
 112 
 113       String message = "Couldn't deduce type of CodeBlob ";
 114       if (cbAddr != null) {
 115         message = message + "@" + cbAddr + " ";
 116       }
 117       message = message + "for PC=" + start;
 118 
 119       throw new RuntimeException(message, wte);
 120     }
 121     if (result == null) return null;
 122     if (Assert.ASSERTS_ENABLED) {
 123       // The HeapBlock that contains this blob is outside of the blob
 124       // but it shouldn't be an error to find a blob based on the
 125       // pointer to the HeapBlock.
 126       Assert.that(result.blobContains(start) || result.blobContains(start.addOffsetTo(8)),
 127                                                                     "found wrong CodeBlob");
 128     }
 129     return result;
 130   }
 131 
 132   public NMethod findNMethod(Address start) {
 133     CodeBlob cb = findBlob(start);
 134     if (Assert.ASSERTS_ENABLED) {
 135       Assert.that(cb == null || cb.isNMethod(), "did not find an nmethod");
 136     }
 137     return (NMethod) cb;
 138   }
 139 
 140   public NMethod findNMethodUnsafe(Address start) {
 141     CodeBlob cb = findBlobUnsafe(start);
 142     if (Assert.ASSERTS_ENABLED) {
 143       Assert.that(cb == null || cb.isNMethod(), "did not find an nmethod");
 144     }
 145     return (NMethod) cb;
 146   }
 147 
 148   /** Routine for instantiating appropriately-typed wrapper for a
 149       CodeBlob. Used by CodeCache, Runtime1, etc. */
 150   public CodeBlob createCodeBlobWrapper(Address codeBlobAddr) {
 151     try {
 152       return (CodeBlob) virtualConstructor.instantiateWrapperFor(codeBlobAddr);
 153     }
 154     catch (Exception e) {
 155       String message = "Unable to deduce type of CodeBlob from address " + codeBlobAddr +
 156                        " (expected type nmethod, RuntimeStub, ";
 157       if (VM.getVM().isClientCompiler()) {
 158         message = message + " or ";
 159       }
 160       message = message + "SafepointBlob";
 161       if (VM.getVM().isServerCompiler()) {
 162         message = message + ", DeoptimizationBlob, or ExceptionBlob";
 163       }
 164       message = message + ")";
 165       throw new RuntimeException(message);
 166     }
 167   }
 168 
 169   public void iterate(CodeCacheVisitor visitor) {
 170     CodeHeap heap = getHeap();
 171     Address ptr = heap.begin();
 172     Address end = heap.end();
 173 
 174     visitor.prologue(ptr, end);
 175     CodeBlob lastBlob = null;
 176     while (ptr != null && ptr.lessThan(end)) {
 177       try {
 178         // Use findStart to get a pointer inside blob other findBlob asserts
 179         CodeBlob blob = findBlobUnsafe(heap.findStart(ptr));
 180         if (blob != null) {
 181           visitor.visit(blob);
 182           if (blob == lastBlob) {
 183             throw new InternalError("saw same blob twice");
 184           }
 185           lastBlob = blob;
 186         }
 187       } catch (RuntimeException e) {
 188         e.printStackTrace();
 189       }
 190       Address next = heap.nextBlock(ptr);
 191       if (next != null && next.lessThan(ptr)) {
 192         throw new InternalError("pointer moved backwards");
 193       }
 194       ptr = next;
 195     }
 196     visitor.epilogue();
 197   }
 198 
 199   //--------------------------------------------------------------------------------
 200   // Internals only below this point
 201   //
 202 
 203   private CodeHeap getHeap() {
 204     return heap;
 205   }
 206 }